blob: e24b9b1d1022493a559adf9673afee12922da1bc [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 */
Peter Oberparleitera7ae2c022009-12-07 12:51:20 +010010
11#define KMSG_COMPONENT "cio"
12#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
13
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/module.h>
15#include <linux/init.h>
16#include <linux/spinlock.h>
17#include <linux/errno.h>
18#include <linux/err.h>
19#include <linux/slab.h>
20#include <linux/list.h>
21#include <linux/device.h>
22#include <linux/workqueue.h>
Peter Oberparleiter90ab1332008-01-26 14:10:52 +010023#include <linux/timer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
25#include <asm/ccwdev.h>
26#include <asm/cio.h>
Tim Schmielau4e57b682005-10-30 15:03:48 -080027#include <asm/param.h> /* HZ */
Cornelia Huck1842f2b2007-10-12 16:11:22 +020028#include <asm/cmb.h>
Cornelia Huck3a3fc292008-07-14 09:58:58 +020029#include <asm/isc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +020031#include "chp.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include "cio.h"
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +010033#include "cio_debug.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include "css.h"
35#include "device.h"
36#include "ioasm.h"
Cornelia Huckcd6b4f22008-01-26 14:10:43 +010037#include "io_sch.h"
Peter Oberparleiterecf5d9e2008-10-10 21:33:06 +020038#include "blacklist.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Peter Oberparleiter90ab1332008-01-26 14:10:52 +010040static struct timer_list recovery_timer;
Cornelia Huck486d0a02008-02-19 15:29:23 +010041static DEFINE_SPINLOCK(recovery_lock);
Peter Oberparleiter90ab1332008-01-26 14:10:52 +010042static int recovery_phase;
43static const unsigned long recovery_delay[] = { 3, 30, 300 };
44
Linus Torvalds1da177e2005-04-16 15:20:36 -070045/******************* bus type handling ***********************/
46
47/* The Linux driver model distinguishes between a bus type and
48 * the bus itself. Of course we only have one channel
49 * subsystem driver and one channel system per machine, but
50 * we still use the abstraction. T.R. says it's a good idea. */
51static int
52ccw_bus_match (struct device * dev, struct device_driver * drv)
53{
54 struct ccw_device *cdev = to_ccwdev(dev);
55 struct ccw_driver *cdrv = to_ccwdrv(drv);
56 const struct ccw_device_id *ids = cdrv->ids, *found;
57
58 if (!ids)
59 return 0;
60
61 found = ccw_device_id_match(ids, &cdev->id);
62 if (!found)
63 return 0;
64
65 cdev->id.driver_info = found->driver_info;
66
67 return 1;
68}
69
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +020070/* Store modalias string delimited by prefix/suffix string into buffer with
71 * specified size. Return length of resulting string (excluding trailing '\0')
72 * even if string doesn't fit buffer (snprintf semantics). */
Cornelia Huckcfbe9bb2007-04-27 16:01:32 +020073static int snprint_alias(char *buf, size_t size,
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +020074 struct ccw_device_id *id, const char *suffix)
75{
76 int len;
77
Cornelia Huckcfbe9bb2007-04-27 16:01:32 +020078 len = snprintf(buf, size, "ccw:t%04Xm%02X", id->cu_type, id->cu_model);
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +020079 if (len > size)
80 return len;
81 buf += len;
82 size -= len;
83
84 if (id->dev_type != 0)
85 len += snprintf(buf, size, "dt%04Xdm%02X%s", id->dev_type,
86 id->dev_model, suffix);
87 else
88 len += snprintf(buf, size, "dtdm%s", suffix);
89
90 return len;
91}
92
93/* Set up environment variables for ccw device uevent. Return 0 on success,
94 * non-zero otherwise. */
Kay Sievers7eff2e72007-08-14 15:15:12 +020095static int ccw_uevent(struct device *dev, struct kobj_uevent_env *env)
Linus Torvalds1da177e2005-04-16 15:20:36 -070096{
97 struct ccw_device *cdev = to_ccwdev(dev);
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +020098 struct ccw_device_id *id = &(cdev->id);
Cornelia Huckcfbe9bb2007-04-27 16:01:32 +020099 int ret;
100 char modalias_buf[30];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +0200102 /* CU_TYPE= */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200103 ret = add_uevent_var(env, "CU_TYPE=%04X", id->cu_type);
Cornelia Huckcfbe9bb2007-04-27 16:01:32 +0200104 if (ret)
105 return ret;
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +0200106
107 /* CU_MODEL= */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200108 ret = add_uevent_var(env, "CU_MODEL=%02X", id->cu_model);
Cornelia Huckcfbe9bb2007-04-27 16:01:32 +0200109 if (ret)
110 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
112 /* The next two can be zero, that's ok for us */
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +0200113 /* DEV_TYPE= */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200114 ret = add_uevent_var(env, "DEV_TYPE=%04X", id->dev_type);
Cornelia Huckcfbe9bb2007-04-27 16:01:32 +0200115 if (ret)
116 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +0200118 /* DEV_MODEL= */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200119 ret = add_uevent_var(env, "DEV_MODEL=%02X", id->dev_model);
Cornelia Huckcfbe9bb2007-04-27 16:01:32 +0200120 if (ret)
121 return ret;
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +0200122
123 /* MODALIAS= */
Cornelia Huckcfbe9bb2007-04-27 16:01:32 +0200124 snprint_alias(modalias_buf, sizeof(modalias_buf), id, "");
Kay Sievers7eff2e72007-08-14 15:15:12 +0200125 ret = add_uevent_var(env, "MODALIAS=%s", modalias_buf);
126 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127}
128
Cornelia Huck8bbace72006-01-11 10:56:22 +0100129struct bus_type ccw_bus_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
Cornelia Huck602b20f2008-01-26 14:10:39 +0100131static void io_subchannel_irq(struct subchannel *);
132static int io_subchannel_probe(struct subchannel *);
133static int io_subchannel_remove(struct subchannel *);
Cornelia Huck8bbace72006-01-11 10:56:22 +0100134static void io_subchannel_shutdown(struct subchannel *);
Cornelia Huckc820de32008-07-14 09:58:45 +0200135static int io_subchannel_sch_event(struct subchannel *, int);
Cornelia Huck99611f82008-07-14 09:59:02 +0200136static int io_subchannel_chp_event(struct subchannel *, struct chp_link *,
137 int);
Sebastian Ott8ea7f552009-09-22 22:58:35 +0200138static void recovery_func(unsigned long data);
139struct workqueue_struct *ccw_device_work;
140wait_queue_head_t ccw_device_init_wq;
141atomic_t ccw_device_init_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
Cornelia Huckf08adc02008-07-14 09:59:03 +0200143static struct css_device_id io_subchannel_ids[] = {
144 { .match_flags = 0x1, .type = SUBCHANNEL_TYPE_IO, },
145 { /* end of list */ },
146};
147MODULE_DEVICE_TABLE(css, io_subchannel_ids);
148
Cornelia Huck93a27592009-06-16 10:30:23 +0200149static int io_subchannel_prepare(struct subchannel *sch)
150{
151 struct ccw_device *cdev;
152 /*
153 * Don't allow suspend while a ccw device registration
154 * is still outstanding.
155 */
156 cdev = sch_get_cdev(sch);
157 if (cdev && !device_is_registered(&cdev->dev))
158 return -EAGAIN;
159 return 0;
160}
161
Sebastian Ott8ea7f552009-09-22 22:58:35 +0200162static void io_subchannel_settle(void)
163{
164 wait_event(ccw_device_init_wq,
165 atomic_read(&ccw_device_init_count) == 0);
166 flush_workqueue(ccw_device_work);
167}
168
Cornelia Huckf7e5d672007-05-10 15:45:43 +0200169static struct css_driver io_subchannel_driver = {
Cornelia Huck4beee642008-01-26 14:10:47 +0100170 .owner = THIS_MODULE,
Cornelia Huckf08adc02008-07-14 09:59:03 +0200171 .subchannel_type = io_subchannel_ids,
Cornelia Huck25b7bb52008-01-26 14:10:41 +0100172 .name = "io_subchannel",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 .irq = io_subchannel_irq,
Cornelia Huckc820de32008-07-14 09:58:45 +0200174 .sch_event = io_subchannel_sch_event,
175 .chp_event = io_subchannel_chp_event,
Cornelia Huck8bbace72006-01-11 10:56:22 +0100176 .probe = io_subchannel_probe,
177 .remove = io_subchannel_remove,
178 .shutdown = io_subchannel_shutdown,
Cornelia Huck93a27592009-06-16 10:30:23 +0200179 .prepare = io_subchannel_prepare,
Sebastian Ott8ea7f552009-09-22 22:58:35 +0200180 .settle = io_subchannel_settle,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181};
182
Sebastian Ott2f176442009-09-22 22:58:33 +0200183int __init io_subchannel_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184{
185 int ret;
186
187 init_waitqueue_head(&ccw_device_init_wq);
188 atomic_set(&ccw_device_init_count, 0);
Peter Oberparleiter90ab1332008-01-26 14:10:52 +0100189 setup_timer(&recovery_timer, recovery_func, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
191 ccw_device_work = create_singlethread_workqueue("cio");
192 if (!ccw_device_work)
Sebastian Ott2f176442009-09-22 22:58:33 +0200193 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 slow_path_wq = create_singlethread_workqueue("kslowcrw");
195 if (!slow_path_wq) {
Sebastian Ott2f176442009-09-22 22:58:33 +0200196 ret = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 goto out_err;
198 }
199 if ((ret = bus_register (&ccw_bus_type)))
200 goto out_err;
201
Cornelia Huck25b7bb52008-01-26 14:10:41 +0100202 ret = css_driver_register(&io_subchannel_driver);
203 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 goto out_err;
205
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 return 0;
207out_err:
208 if (ccw_device_work)
209 destroy_workqueue(ccw_device_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 if (slow_path_wq)
211 destroy_workqueue(slow_path_wq);
212 return ret;
213}
214
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
216/************************ device handling **************************/
217
218/*
219 * A ccw_device has some interfaces in sysfs in addition to the
220 * standard ones.
221 * The following entries are designed to export the information which
222 * resided in 2.4 in /proc/subchannels. Subchannel and device number
223 * are obvious, so they don't have an entry :)
224 * TODO: Split chpids and pimpampom up? Where is "in use" in the tree?
225 */
226static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400227chpids_show (struct device * dev, struct device_attribute *attr, char * buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228{
229 struct subchannel *sch = to_subchannel(dev);
Peter Oberparleiter7ad6a242007-04-27 16:01:35 +0200230 struct chsc_ssd_info *ssd = &sch->ssd_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 ssize_t ret = 0;
232 int chp;
Peter Oberparleiter7ad6a242007-04-27 16:01:35 +0200233 int mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
Peter Oberparleiter7ad6a242007-04-27 16:01:35 +0200235 for (chp = 0; chp < 8; chp++) {
236 mask = 0x80 >> chp;
237 if (ssd->path_mask & mask)
238 ret += sprintf(buf + ret, "%02x ", ssd->chpid[chp].id);
239 else
240 ret += sprintf(buf + ret, "00 ");
241 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 ret += sprintf (buf+ret, "\n");
243 return min((ssize_t)PAGE_SIZE, ret);
244}
245
246static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400247pimpampom_show (struct device * dev, struct device_attribute *attr, char * buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248{
249 struct subchannel *sch = to_subchannel(dev);
250 struct pmcw *pmcw = &sch->schib.pmcw;
251
252 return sprintf (buf, "%02x %02x %02x\n",
253 pmcw->pim, pmcw->pam, pmcw->pom);
254}
255
256static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400257devtype_show (struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258{
259 struct ccw_device *cdev = to_ccwdev(dev);
260 struct ccw_device_id *id = &(cdev->id);
261
262 if (id->dev_type != 0)
263 return sprintf(buf, "%04x/%02x\n",
264 id->dev_type, id->dev_model);
265 else
266 return sprintf(buf, "n/a\n");
267}
268
269static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400270cutype_show (struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271{
272 struct ccw_device *cdev = to_ccwdev(dev);
273 struct ccw_device_id *id = &(cdev->id);
274
275 return sprintf(buf, "%04x/%02x\n",
276 id->cu_type, id->cu_model);
277}
278
279static ssize_t
Bastian Blankf1fc78a2005-10-30 15:00:12 -0800280modalias_show (struct device *dev, struct device_attribute *attr, char *buf)
281{
282 struct ccw_device *cdev = to_ccwdev(dev);
283 struct ccw_device_id *id = &(cdev->id);
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +0200284 int len;
Bastian Blankf1fc78a2005-10-30 15:00:12 -0800285
Cornelia Huck086a6c62007-07-17 13:36:08 +0200286 len = snprint_alias(buf, PAGE_SIZE, id, "\n");
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +0200287
288 return len > PAGE_SIZE ? PAGE_SIZE : len;
Bastian Blankf1fc78a2005-10-30 15:00:12 -0800289}
290
291static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400292online_show (struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293{
294 struct ccw_device *cdev = to_ccwdev(dev);
295
296 return sprintf(buf, cdev->online ? "1\n" : "0\n");
297}
298
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100299int ccw_device_is_orphan(struct ccw_device *cdev)
300{
301 return sch_is_pseudo_sch(to_subchannel(cdev->dev.parent));
302}
303
Cornelia Huckef995162007-04-27 16:01:39 +0200304static void ccw_device_unregister(struct ccw_device *cdev)
Cornelia Huck7674da72006-12-08 15:54:21 +0100305{
Sebastian Ott3b554a12009-09-11 10:28:26 +0200306 if (test_and_clear_bit(1, &cdev->private->registered)) {
Cornelia Huckef995162007-04-27 16:01:39 +0200307 device_del(&cdev->dev);
Sebastian Ott3b554a12009-09-11 10:28:26 +0200308 /* Release reference from device_initialize(). */
309 put_device(&cdev->dev);
310 }
Cornelia Huck7674da72006-12-08 15:54:21 +0100311}
312
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +0200313/**
314 * ccw_device_set_offline() - disable a ccw device for I/O
315 * @cdev: target ccw device
316 *
317 * This function calls the driver's set_offline() function for @cdev, if
318 * given, and then disables @cdev.
319 * Returns:
320 * %0 on success and a negative error value on failure.
321 * Context:
322 * enabled, ccw device lock not held
323 */
324int ccw_device_set_offline(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325{
326 int ret;
327
328 if (!cdev)
329 return -ENODEV;
330 if (!cdev->online || !cdev->drv)
331 return -EINVAL;
332
333 if (cdev->drv->set_offline) {
334 ret = cdev->drv->set_offline(cdev);
335 if (ret != 0)
336 return ret;
337 }
338 cdev->online = 0;
339 spin_lock_irq(cdev->ccwlock);
Michael Ernst217ee6c2009-09-11 10:28:21 +0200340 /* Wait until a final state or DISCONNECTED is reached */
341 while (!dev_fsm_final_state(cdev) &&
342 cdev->private->state != DEV_STATE_DISCONNECTED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 spin_unlock_irq(cdev->ccwlock);
Michael Ernst217ee6c2009-09-11 10:28:21 +0200344 wait_event(cdev->private->wait_q, (dev_fsm_final_state(cdev) ||
345 cdev->private->state == DEV_STATE_DISCONNECTED));
346 spin_lock_irq(cdev->ccwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 }
Michael Ernst217ee6c2009-09-11 10:28:21 +0200348 ret = ccw_device_offline(cdev);
349 if (ret)
350 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 spin_unlock_irq(cdev->ccwlock);
Michael Ernst217ee6c2009-09-11 10:28:21 +0200352 wait_event(cdev->private->wait_q, (dev_fsm_final_state(cdev) ||
353 cdev->private->state == DEV_STATE_DISCONNECTED));
Peter Oberparleitera7ae2c022009-12-07 12:51:20 +0100354 /* Inform the user if set offline failed. */
355 if (cdev->private->state == DEV_STATE_BOXED) {
356 pr_warning("%s: The device entered boxed state while "
357 "being set offline\n", dev_name(&cdev->dev));
358 } else if (cdev->private->state == DEV_STATE_NOT_OPER) {
359 pr_warning("%s: The device stopped operating while "
360 "being set offline\n", dev_name(&cdev->dev));
361 }
Michael Ernst217ee6c2009-09-11 10:28:21 +0200362 /* Give up reference from ccw_device_set_online(). */
363 put_device(&cdev->dev);
364 return 0;
365
366error:
367 CIO_MSG_EVENT(0, "ccw_device_offline returned %d, device 0.%x.%04x\n",
368 ret, cdev->private->dev_id.ssid,
369 cdev->private->dev_id.devno);
370 cdev->private->state = DEV_STATE_OFFLINE;
371 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
372 spin_unlock_irq(cdev->ccwlock);
373 /* Give up reference from ccw_device_set_online(). */
374 put_device(&cdev->dev);
375 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376}
377
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +0200378/**
379 * ccw_device_set_online() - enable a ccw device for I/O
380 * @cdev: target ccw device
381 *
382 * This function first enables @cdev and then calls the driver's set_online()
383 * function for @cdev, if given. If set_online() returns an error, @cdev is
384 * disabled again.
385 * Returns:
386 * %0 on success and a negative error value on failure.
387 * Context:
388 * enabled, ccw device lock not held
389 */
390int ccw_device_set_online(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391{
392 int ret;
Michael Ernst217ee6c2009-09-11 10:28:21 +0200393 int ret2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
395 if (!cdev)
396 return -ENODEV;
397 if (cdev->online || !cdev->drv)
398 return -EINVAL;
Cornelia Huck9cd67422008-12-25 13:39:06 +0100399 /* Hold on to an extra reference while device is online. */
400 if (!get_device(&cdev->dev))
401 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
403 spin_lock_irq(cdev->ccwlock);
404 ret = ccw_device_online(cdev);
405 spin_unlock_irq(cdev->ccwlock);
406 if (ret == 0)
407 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
408 else {
Michael Ernst139b83dd2008-05-07 09:22:54 +0200409 CIO_MSG_EVENT(0, "ccw_device_online returned %d, "
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200410 "device 0.%x.%04x\n",
411 ret, cdev->private->dev_id.ssid,
412 cdev->private->dev_id.devno);
Cornelia Huck9cd67422008-12-25 13:39:06 +0100413 /* Give up online reference since onlining failed. */
414 put_device(&cdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 return ret;
416 }
Michael Ernst217ee6c2009-09-11 10:28:21 +0200417 spin_lock_irq(cdev->ccwlock);
418 /* Check if online processing was successful */
419 if ((cdev->private->state != DEV_STATE_ONLINE) &&
420 (cdev->private->state != DEV_STATE_W4SENSE)) {
421 spin_unlock_irq(cdev->ccwlock);
Peter Oberparleitera7ae2c022009-12-07 12:51:20 +0100422 /* Inform the user that set online failed. */
423 if (cdev->private->state == DEV_STATE_BOXED) {
424 pr_warning("%s: Setting the device online failed "
425 "because it is boxed\n",
426 dev_name(&cdev->dev));
427 } else if (cdev->private->state == DEV_STATE_NOT_OPER) {
428 pr_warning("%s: Setting the device online failed "
429 "because it is not operational\n",
430 dev_name(&cdev->dev));
431 }
Cornelia Huck9cd67422008-12-25 13:39:06 +0100432 /* Give up online reference since onlining failed. */
433 put_device(&cdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 return -ENODEV;
Cornelia Huck9cd67422008-12-25 13:39:06 +0100435 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 spin_unlock_irq(cdev->ccwlock);
Michael Ernst217ee6c2009-09-11 10:28:21 +0200437 if (cdev->drv->set_online)
438 ret = cdev->drv->set_online(cdev);
439 if (ret)
440 goto rollback;
441 cdev->online = 1;
442 return 0;
443
444rollback:
445 spin_lock_irq(cdev->ccwlock);
446 /* Wait until a final state or DISCONNECTED is reached */
447 while (!dev_fsm_final_state(cdev) &&
448 cdev->private->state != DEV_STATE_DISCONNECTED) {
449 spin_unlock_irq(cdev->ccwlock);
450 wait_event(cdev->private->wait_q, (dev_fsm_final_state(cdev) ||
451 cdev->private->state == DEV_STATE_DISCONNECTED));
452 spin_lock_irq(cdev->ccwlock);
453 }
454 ret2 = ccw_device_offline(cdev);
455 if (ret2)
456 goto error;
457 spin_unlock_irq(cdev->ccwlock);
458 wait_event(cdev->private->wait_q, (dev_fsm_final_state(cdev) ||
459 cdev->private->state == DEV_STATE_DISCONNECTED));
Cornelia Huck9cd67422008-12-25 13:39:06 +0100460 /* Give up online reference since onlining failed. */
461 put_device(&cdev->dev);
Michael Ernst217ee6c2009-09-11 10:28:21 +0200462 return ret;
463
464error:
465 CIO_MSG_EVENT(0, "rollback ccw_device_offline returned %d, "
466 "device 0.%x.%04x\n",
467 ret2, cdev->private->dev_id.ssid,
468 cdev->private->dev_id.devno);
469 cdev->private->state = DEV_STATE_OFFLINE;
470 spin_unlock_irq(cdev->ccwlock);
471 /* Give up online reference since onlining failed. */
472 put_device(&cdev->dev);
473 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474}
475
Sebastian Otte74fe0c2009-03-26 15:24:08 +0100476static int online_store_handle_offline(struct ccw_device *cdev)
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200477{
Peter Oberparleiter37de53b2009-12-07 12:51:19 +0100478 if (cdev->private->state == DEV_STATE_DISCONNECTED) {
479 spin_lock_irq(cdev->ccwlock);
480 ccw_device_sched_todo(cdev, CDEV_TODO_UNREG_EVAL);
481 spin_unlock_irq(cdev->ccwlock);
482 } else if (cdev->online && cdev->drv && cdev->drv->set_offline)
Sebastian Otte74fe0c2009-03-26 15:24:08 +0100483 return ccw_device_set_offline(cdev);
484 return 0;
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200485}
486
487static int online_store_recog_and_online(struct ccw_device *cdev)
488{
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200489 /* Do device recognition, if needed. */
Sebastian Ott99f6a5702009-03-31 19:16:07 +0200490 if (cdev->private->state == DEV_STATE_BOXED) {
Peter Oberparleiter1f5bd382009-12-07 12:51:23 +0100491 spin_lock_irq(cdev->ccwlock);
Peter Oberparleiter736b5db2009-12-07 12:51:21 +0100492 ccw_device_recognition(cdev);
Peter Oberparleiter1f5bd382009-12-07 12:51:23 +0100493 spin_unlock_irq(cdev->ccwlock);
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200494 wait_event(cdev->private->wait_q,
495 cdev->private->flags.recog_done);
Sebastian Ott156013f2009-03-31 19:16:03 +0200496 if (cdev->private->state != DEV_STATE_OFFLINE)
497 /* recognition failed */
498 return -EAGAIN;
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200499 }
500 if (cdev->drv && cdev->drv->set_online)
501 ccw_device_set_online(cdev);
502 return 0;
503}
Sebastian Ott156013f2009-03-31 19:16:03 +0200504
Michael Ernstc78aa6c2008-07-14 09:59:22 +0200505static int online_store_handle_online(struct ccw_device *cdev, int force)
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200506{
507 int ret;
508
509 ret = online_store_recog_and_online(cdev);
Sebastian Ott156013f2009-03-31 19:16:03 +0200510 if (ret && !force)
Michael Ernstc78aa6c2008-07-14 09:59:22 +0200511 return ret;
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200512 if (force && cdev->private->state == DEV_STATE_BOXED) {
513 ret = ccw_device_stlck(cdev);
Michael Ernstc78aa6c2008-07-14 09:59:22 +0200514 if (ret)
515 return ret;
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200516 if (cdev->id.cu_type == 0)
517 cdev->private->state = DEV_STATE_NOT_OPER;
Sebastian Ott156013f2009-03-31 19:16:03 +0200518 ret = online_store_recog_and_online(cdev);
519 if (ret)
520 return ret;
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200521 }
Michael Ernstc78aa6c2008-07-14 09:59:22 +0200522 return 0;
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200523}
524
525static ssize_t online_store (struct device *dev, struct device_attribute *attr,
526 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527{
528 struct ccw_device *cdev = to_ccwdev(dev);
Cornelia Huck2f972202008-04-30 13:38:33 +0200529 int force, ret;
530 unsigned long i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
Peter Oberparleiter350e9122009-12-07 12:51:28 +0100532 if (!dev_fsm_final_state(cdev) &&
533 cdev->private->state != DEV_STATE_DISCONNECTED)
534 return -EAGAIN;
535 if (atomic_cmpxchg(&cdev->private->onoff, 0, 1) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 return -EAGAIN;
537
538 if (cdev->drv && !try_module_get(cdev->drv->owner)) {
539 atomic_set(&cdev->private->onoff, 0);
540 return -EINVAL;
541 }
542 if (!strncmp(buf, "force\n", count)) {
543 force = 1;
544 i = 1;
Cornelia Huck2f972202008-04-30 13:38:33 +0200545 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 } else {
547 force = 0;
Cornelia Huck2f972202008-04-30 13:38:33 +0200548 ret = strict_strtoul(buf, 16, &i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 }
Cornelia Huck2f972202008-04-30 13:38:33 +0200550 if (ret)
551 goto out;
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200552 switch (i) {
553 case 0:
Sebastian Otte74fe0c2009-03-26 15:24:08 +0100554 ret = online_store_handle_offline(cdev);
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200555 break;
556 case 1:
Michael Ernstc78aa6c2008-07-14 09:59:22 +0200557 ret = online_store_handle_online(cdev, force);
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200558 break;
559 default:
Cornelia Huck2f972202008-04-30 13:38:33 +0200560 ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 }
Cornelia Huck2f972202008-04-30 13:38:33 +0200562out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 if (cdev->drv)
564 module_put(cdev->drv->owner);
565 atomic_set(&cdev->private->onoff, 0);
Sebastian Otte74fe0c2009-03-26 15:24:08 +0100566 return (ret < 0) ? ret : count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567}
568
569static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400570available_show (struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571{
572 struct ccw_device *cdev = to_ccwdev(dev);
573 struct subchannel *sch;
574
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100575 if (ccw_device_is_orphan(cdev))
576 return sprintf(buf, "no device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 switch (cdev->private->state) {
578 case DEV_STATE_BOXED:
579 return sprintf(buf, "boxed\n");
580 case DEV_STATE_DISCONNECTED:
581 case DEV_STATE_DISCONNECTED_SENSE_ID:
582 case DEV_STATE_NOT_OPER:
583 sch = to_subchannel(dev->parent);
584 if (!sch->lpm)
585 return sprintf(buf, "no path\n");
586 else
587 return sprintf(buf, "no device\n");
588 default:
589 /* All other states considered fine. */
590 return sprintf(buf, "good\n");
591 }
592}
593
594static DEVICE_ATTR(chpids, 0444, chpids_show, NULL);
595static DEVICE_ATTR(pimpampom, 0444, pimpampom_show, NULL);
596static DEVICE_ATTR(devtype, 0444, devtype_show, NULL);
597static DEVICE_ATTR(cutype, 0444, cutype_show, NULL);
Bastian Blankf1fc78a2005-10-30 15:00:12 -0800598static DEVICE_ATTR(modalias, 0444, modalias_show, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599static DEVICE_ATTR(online, 0644, online_show, online_store);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600static DEVICE_ATTR(availability, 0444, available_show, NULL);
601
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200602static struct attribute *io_subchannel_attrs[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 &dev_attr_chpids.attr,
604 &dev_attr_pimpampom.attr,
605 NULL,
606};
607
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200608static struct attribute_group io_subchannel_attr_group = {
609 .attrs = io_subchannel_attrs,
Cornelia Huck529192f2006-12-08 15:55:57 +0100610};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
612static struct attribute * ccwdev_attrs[] = {
613 &dev_attr_devtype.attr,
614 &dev_attr_cutype.attr,
Bastian Blankf1fc78a2005-10-30 15:00:12 -0800615 &dev_attr_modalias.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 &dev_attr_online.attr,
617 &dev_attr_cmb_enable.attr,
618 &dev_attr_availability.attr,
619 NULL,
620};
621
622static struct attribute_group ccwdev_attr_group = {
623 .attrs = ccwdev_attrs,
624};
625
David Brownella4dbd672009-06-24 10:06:31 -0700626static const struct attribute_group *ccwdev_attr_groups[] = {
Cornelia Huckef995162007-04-27 16:01:39 +0200627 &ccwdev_attr_group,
628 NULL,
629};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630
631/* this is a simple abstraction for device_register that sets the
632 * correct bus type and adds the bus specific files */
Cornelia Huck3c9da7b2006-10-27 12:39:33 +0200633static int ccw_device_register(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634{
635 struct device *dev = &cdev->dev;
636 int ret;
637
638 dev->bus = &ccw_bus_type;
Sebastian Ott3ac276f2009-09-11 10:28:27 +0200639 ret = dev_set_name(&cdev->dev, "0.%x.%04x", cdev->private->dev_id.ssid,
640 cdev->private->dev_id.devno);
641 if (ret)
642 return ret;
643 ret = device_add(dev);
644 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 return ret;
646
647 set_bit(1, &cdev->private->registered);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 return ret;
649}
650
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100651static int match_dev_id(struct device *dev, void *data)
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700652{
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100653 struct ccw_device *cdev = to_ccwdev(dev);
654 struct ccw_dev_id *dev_id = data;
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700655
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100656 return ccw_dev_id_is_equal(&cdev->private->dev_id, dev_id);
657}
658
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100659static struct ccw_device *get_ccwdev_by_dev_id(struct ccw_dev_id *dev_id)
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100660{
661 struct device *dev;
662
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100663 dev = bus_find_device(&ccw_bus_type, NULL, dev_id, match_dev_id);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100664
665 return dev ? to_ccwdev(dev) : NULL;
666}
667
Peter Oberparleiter37de53b2009-12-07 12:51:19 +0100668static void ccw_device_do_unbind_bind(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669{
Cornelia Huckeb32ae82009-03-26 15:24:05 +0100670 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671
Cornelia Huckeb32ae82009-03-26 15:24:05 +0100672 if (test_bit(1, &cdev->private->registered)) {
673 device_release_driver(&cdev->dev);
674 ret = device_attach(&cdev->dev);
675 WARN_ON(ret == -ENODEV);
676 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677}
678
679static void
680ccw_device_release(struct device *dev)
681{
682 struct ccw_device *cdev;
683
684 cdev = to_ccwdev(dev);
Cornelia Huck6eff2082008-12-25 13:39:07 +0100685 /* Release reference of parent subchannel. */
686 put_device(cdev->dev.parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 kfree(cdev->private);
688 kfree(cdev);
689}
690
Cornelia Huck7674da72006-12-08 15:54:21 +0100691static struct ccw_device * io_subchannel_allocate_dev(struct subchannel *sch)
692{
693 struct ccw_device *cdev;
694
695 cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
696 if (cdev) {
697 cdev->private = kzalloc(sizeof(struct ccw_device_private),
698 GFP_KERNEL | GFP_DMA);
699 if (cdev->private)
700 return cdev;
701 }
702 kfree(cdev);
703 return ERR_PTR(-ENOMEM);
704}
705
Peter Oberparleiter37de53b2009-12-07 12:51:19 +0100706static void ccw_device_todo(struct work_struct *work);
707
Cornelia Huck7674da72006-12-08 15:54:21 +0100708static int io_subchannel_initialize_dev(struct subchannel *sch,
709 struct ccw_device *cdev)
710{
711 cdev->private->cdev = cdev;
712 atomic_set(&cdev->private->onoff, 0);
713 cdev->dev.parent = &sch->dev;
714 cdev->dev.release = ccw_device_release;
Peter Oberparleiter37de53b2009-12-07 12:51:19 +0100715 INIT_WORK(&cdev->private->todo_work, ccw_device_todo);
Cornelia Huckef995162007-04-27 16:01:39 +0200716 cdev->dev.groups = ccwdev_attr_groups;
Cornelia Huck7674da72006-12-08 15:54:21 +0100717 /* Do first half of device_register. */
718 device_initialize(&cdev->dev);
719 if (!get_device(&sch->dev)) {
Cornelia Huck283fdd02008-12-25 13:39:10 +0100720 /* Release reference from device_initialize(). */
721 put_device(&cdev->dev);
Cornelia Huck7674da72006-12-08 15:54:21 +0100722 return -ENODEV;
723 }
724 return 0;
725}
726
727static struct ccw_device * io_subchannel_create_ccwdev(struct subchannel *sch)
728{
729 struct ccw_device *cdev;
730 int ret;
731
732 cdev = io_subchannel_allocate_dev(sch);
733 if (!IS_ERR(cdev)) {
734 ret = io_subchannel_initialize_dev(sch, cdev);
Sebastian Ott06739a82009-08-23 18:09:04 +0200735 if (ret)
Cornelia Huck7674da72006-12-08 15:54:21 +0100736 cdev = ERR_PTR(ret);
Cornelia Huck7674da72006-12-08 15:54:21 +0100737 }
738 return cdev;
739}
740
Peter Oberparleiter736b5db2009-12-07 12:51:21 +0100741static void io_subchannel_recog(struct ccw_device *, struct subchannel *);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100742
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100743static void sch_create_and_recog_new_device(struct subchannel *sch)
744{
745 struct ccw_device *cdev;
746
747 /* Need to allocate a new ccw device. */
748 cdev = io_subchannel_create_ccwdev(sch);
749 if (IS_ERR(cdev)) {
750 /* OK, we did everything we could... */
751 css_sch_device_unregister(sch);
752 return;
753 }
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100754 /* Start recognition for the new ccw device. */
Peter Oberparleiter736b5db2009-12-07 12:51:21 +0100755 io_subchannel_recog(cdev, sch);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100756}
757
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758/*
759 * Register recognized device.
760 */
Peter Oberparleiter37de53b2009-12-07 12:51:19 +0100761static void io_subchannel_register(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 struct subchannel *sch;
764 int ret;
765 unsigned long flags;
766
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 sch = to_subchannel(cdev->dev.parent);
Cornelia Huck5fb6b852008-12-25 13:39:08 +0100768 /*
769 * Check if subchannel is still registered. It may have become
770 * unregistered if a machine check hit us after finishing
771 * device recognition but before the register work could be
772 * queued.
773 */
774 if (!device_is_registered(&sch->dev))
775 goto out_err;
Cornelia Huck82b7ac02007-04-27 16:01:36 +0200776 css_update_ssd_info(sch);
Cornelia Huck47af5512006-12-04 15:41:07 +0100777 /*
778 * io_subchannel_register() will also be called after device
779 * recognition has been done for a boxed device (which will already
780 * be registered). We need to reprobe since we may now have sense id
781 * information.
782 */
Cornelia Huckd6a30762008-12-25 13:39:11 +0100783 if (device_is_registered(&cdev->dev)) {
Cornelia Huck47af5512006-12-04 15:41:07 +0100784 if (!cdev->drv) {
785 ret = device_reprobe(&cdev->dev);
786 if (ret)
787 /* We can't do much here. */
Michael Ernst139b83dd2008-05-07 09:22:54 +0200788 CIO_MSG_EVENT(0, "device_reprobe() returned"
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200789 " %d for 0.%x.%04x\n", ret,
790 cdev->private->dev_id.ssid,
791 cdev->private->dev_id.devno);
Cornelia Huck47af5512006-12-04 15:41:07 +0100792 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 goto out;
794 }
Cornelia Huckfa1a8c22007-04-26 00:12:03 -0700795 /*
796 * Now we know this subchannel will stay, we can throw
797 * our delayed uevent.
798 */
Ming Leif67f1292009-03-01 21:10:49 +0800799 dev_set_uevent_suppress(&sch->dev, 0);
Cornelia Huckfa1a8c22007-04-26 00:12:03 -0700800 kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 /* make it known to the system */
802 ret = ccw_device_register(cdev);
803 if (ret) {
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200804 CIO_MSG_EVENT(0, "Could not register ccw dev 0.%x.%04x: %d\n",
805 cdev->private->dev_id.ssid,
806 cdev->private->dev_id.devno, ret);
Cornelia Huck2ec22982006-12-08 15:54:26 +0100807 spin_lock_irqsave(sch->lock, flags);
Cornelia Huckdb6a6422008-01-26 14:10:46 +0100808 sch_set_cdev(sch, NULL);
Cornelia Huck2ec22982006-12-08 15:54:26 +0100809 spin_unlock_irqrestore(sch->lock, flags);
Cornelia Huck6eff2082008-12-25 13:39:07 +0100810 /* Release initial device reference. */
811 put_device(&cdev->dev);
Cornelia Huck5fb6b852008-12-25 13:39:08 +0100812 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814out:
815 cdev->private->flags.recog_done = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 wake_up(&cdev->private->wait_q);
Cornelia Huck5fb6b852008-12-25 13:39:08 +0100817out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 if (atomic_dec_and_test(&ccw_device_init_count))
819 wake_up(&ccw_device_init_wq);
820}
821
Peter Oberparleiter37de53b2009-12-07 12:51:19 +0100822static void ccw_device_call_sch_unregister(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 struct subchannel *sch;
825
Peter Oberparleiter46fbe4e2008-10-10 21:33:05 +0200826 /* Get subchannel reference for local processing. */
827 if (!get_device(cdev->dev.parent))
828 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 sch = to_subchannel(cdev->dev.parent);
Cornelia Huck6ab48792006-07-12 16:39:50 +0200830 css_sch_device_unregister(sch);
Peter Oberparleiter46fbe4e2008-10-10 21:33:05 +0200831 /* Release subchannel reference for local processing. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 put_device(&sch->dev);
833}
834
835/*
836 * subchannel recognition done. Called from the state machine.
837 */
838void
839io_subchannel_recog_done(struct ccw_device *cdev)
840{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 if (css_init_done == 0) {
842 cdev->private->flags.recog_done = 1;
843 return;
844 }
845 switch (cdev->private->state) {
Sebastian Ott47593bf2009-03-31 19:16:05 +0200846 case DEV_STATE_BOXED:
847 /* Device did not respond in time. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 case DEV_STATE_NOT_OPER:
849 cdev->private->flags.recog_done = 1;
Peter Oberparleiter37de53b2009-12-07 12:51:19 +0100850 /* Remove device found not operational. */
851 ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 if (atomic_dec_and_test(&ccw_device_init_count))
853 wake_up(&ccw_device_init_wq);
854 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 case DEV_STATE_OFFLINE:
856 /*
857 * We can't register the device in interrupt context so
858 * we schedule a work item.
859 */
Peter Oberparleiter37de53b2009-12-07 12:51:19 +0100860 ccw_device_sched_todo(cdev, CDEV_TODO_REGISTER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 break;
862 }
863}
864
Peter Oberparleiter736b5db2009-12-07 12:51:21 +0100865static void io_subchannel_recog(struct ccw_device *cdev, struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 struct ccw_device_private *priv;
868
Cornelia Huck2ec22982006-12-08 15:54:26 +0100869 cdev->ccwlock = sch->lock;
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800870
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 /* Init private data. */
872 priv = cdev->private;
Cornelia Huck78964262006-10-11 15:31:38 +0200873 priv->dev_id.devno = sch->schib.pmcw.dev;
874 priv->dev_id.ssid = sch->schid.ssid;
875 priv->schid = sch->schid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 priv->state = DEV_STATE_NOT_OPER;
877 INIT_LIST_HEAD(&priv->cmb_list);
878 init_waitqueue_head(&priv->wait_q);
879 init_timer(&priv->timer);
880
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 /* Increase counter of devices currently in recognition. */
882 atomic_inc(&ccw_device_init_count);
883
884 /* Start async. device sensing. */
Cornelia Huck2ec22982006-12-08 15:54:26 +0100885 spin_lock_irq(sch->lock);
Peter Oberparleiter60e4dac2009-12-07 12:51:16 +0100886 sch_set_cdev(sch, cdev);
Peter Oberparleiter736b5db2009-12-07 12:51:21 +0100887 ccw_device_recognition(cdev);
Cornelia Huck2ec22982006-12-08 15:54:26 +0100888 spin_unlock_irq(sch->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889}
890
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100891static int ccw_device_move_to_sch(struct ccw_device *cdev,
892 struct subchannel *sch)
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100893{
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100894 struct subchannel *old_sch;
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100895 int rc;
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100896
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100897 old_sch = to_subchannel(cdev->dev.parent);
898 /* Obtain child reference for new parent. */
Cornelia Huck6eff2082008-12-25 13:39:07 +0100899 if (!get_device(&sch->dev))
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100900 return -ENODEV;
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100901 mutex_lock(&sch->reg_mutex);
Cornelia Huckffa6a702009-03-04 12:44:00 +0100902 rc = device_move(&cdev->dev, &sch->dev, DPM_ORDER_PARENT_BEFORE_DEV);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100903 mutex_unlock(&sch->reg_mutex);
904 if (rc) {
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100905 CIO_MSG_EVENT(0, "device_move(0.%x.%04x,0.%x.%04x)=%d\n",
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100906 cdev->private->dev_id.ssid,
907 cdev->private->dev_id.devno, sch->schid.ssid,
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100908 sch->schib.pmcw.dev, rc);
909 /* Release child reference for new parent. */
Cornelia Huck6eff2082008-12-25 13:39:07 +0100910 put_device(&sch->dev);
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100911 return rc;
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100912 }
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100913 /* Clean up old subchannel. */
914 if (!sch_is_pseudo_sch(old_sch)) {
915 spin_lock_irq(old_sch->lock);
916 sch_set_cdev(old_sch, NULL);
917 cio_disable_subchannel(old_sch);
918 spin_unlock_irq(old_sch->lock);
919 css_schedule_eval(old_sch->schid);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100920 }
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100921 /* Release child reference for old parent. */
922 put_device(&old_sch->dev);
923 /* Initialize new subchannel. */
924 spin_lock_irq(sch->lock);
925 cdev->private->schid = sch->schid;
926 cdev->ccwlock = sch->lock;
927 if (!sch_is_pseudo_sch(sch))
928 sch_set_cdev(sch, cdev);
929 spin_unlock_irq(sch->lock);
930 if (!sch_is_pseudo_sch(sch))
931 css_update_ssd_info(sch);
932 return 0;
933}
934
935static int ccw_device_move_to_orph(struct ccw_device *cdev)
936{
937 struct subchannel *sch = to_subchannel(cdev->dev.parent);
938 struct channel_subsystem *css = to_css(sch->dev.parent);
939
940 return ccw_device_move_to_sch(cdev, css->pseudo_subchannel);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100941}
942
Cornelia Huck602b20f2008-01-26 14:10:39 +0100943static void io_subchannel_irq(struct subchannel *sch)
944{
945 struct ccw_device *cdev;
946
Cornelia Huckdb6a6422008-01-26 14:10:46 +0100947 cdev = sch_get_cdev(sch);
Cornelia Huck602b20f2008-01-26 14:10:39 +0100948
Sebastian Ottefd986d2009-09-11 10:28:18 +0200949 CIO_TRACE_EVENT(6, "IRQ");
950 CIO_TRACE_EVENT(6, dev_name(&sch->dev));
Cornelia Huck602b20f2008-01-26 14:10:39 +0100951 if (cdev)
952 dev_fsm_event(cdev, DEV_EVENT_INTERRUPT);
953}
954
Sebastian Ott13952ec2008-12-25 13:39:13 +0100955void io_subchannel_init_config(struct subchannel *sch)
956{
957 memset(&sch->config, 0, sizeof(sch->config));
958 sch->config.csense = 1;
Sebastian Ott13952ec2008-12-25 13:39:13 +0100959}
960
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +0200961static void io_subchannel_init_fields(struct subchannel *sch)
962{
963 if (cio_is_console(sch->schid))
964 sch->opm = 0xff;
965 else
966 sch->opm = chp_get_sch_opm(sch);
967 sch->lpm = sch->schib.pmcw.pam & sch->opm;
Cornelia Huck3a3fc292008-07-14 09:58:58 +0200968 sch->isc = cio_is_console(sch->schid) ? CONSOLE_ISC : IO_SCH_ISC;
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +0200969
970 CIO_MSG_EVENT(6, "Detected device %04x on subchannel 0.%x.%04X"
971 " - PIM = %02X, PAM = %02X, POM = %02X\n",
972 sch->schib.pmcw.dev, sch->schid.ssid,
973 sch->schid.sch_no, sch->schib.pmcw.pim,
974 sch->schib.pmcw.pam, sch->schib.pmcw.pom);
Sebastian Ott13952ec2008-12-25 13:39:13 +0100975
976 io_subchannel_init_config(sch);
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +0200977}
978
Cornelia Huck90ed2b62008-12-25 13:39:09 +0100979/*
980 * Note: We always return 0 so that we bind to the device even on error.
981 * This is needed so that our remove function is called on unregister.
982 */
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +0200983static int io_subchannel_probe(struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 struct ccw_device *cdev;
986 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987
Peter Oberparleiter6d7c5af2009-10-14 12:43:50 +0200988 if (cio_is_console(sch->schid)) {
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200989 rc = sysfs_create_group(&sch->dev.kobj,
990 &io_subchannel_attr_group);
991 if (rc)
992 CIO_MSG_EVENT(0, "Failed to create io subchannel "
993 "attributes for subchannel "
994 "0.%x.%04x (rc=%d)\n",
995 sch->schid.ssid, sch->schid.sch_no, rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 /*
Peter Oberparleiter6d7c5af2009-10-14 12:43:50 +0200997 * The console subchannel already has an associated ccw_device.
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200998 * Throw the delayed uevent for the subchannel, register
Peter Oberparleiter6d7c5af2009-10-14 12:43:50 +0200999 * the ccw_device and exit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 */
Ming Leif67f1292009-03-01 21:10:49 +08001001 dev_set_uevent_suppress(&sch->dev, 0);
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001002 kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
Peter Oberparleiter6d7c5af2009-10-14 12:43:50 +02001003 cdev = sch_get_cdev(sch);
Cornelia Hucke1031782007-10-12 16:11:23 +02001004 cdev->dev.groups = ccwdev_attr_groups;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 device_initialize(&cdev->dev);
1006 ccw_device_register(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 /*
1008 * Check if the device is already online. If it is
Cornelia Huck9cd67422008-12-25 13:39:06 +01001009 * the reference count needs to be corrected since we
1010 * didn't obtain a reference in ccw_device_set_online.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 */
1012 if (cdev->private->state != DEV_STATE_NOT_OPER &&
1013 cdev->private->state != DEV_STATE_OFFLINE &&
1014 cdev->private->state != DEV_STATE_BOXED)
1015 get_device(&cdev->dev);
1016 return 0;
1017 }
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001018 io_subchannel_init_fields(sch);
Sebastian Ottf444cc02008-12-25 13:39:14 +01001019 rc = cio_commit_config(sch);
1020 if (rc)
1021 goto out_schedule;
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001022 rc = sysfs_create_group(&sch->dev.kobj,
1023 &io_subchannel_attr_group);
1024 if (rc)
Cornelia Huck90ed2b62008-12-25 13:39:09 +01001025 goto out_schedule;
Cornelia Huckcd6b4f22008-01-26 14:10:43 +01001026 /* Allocate I/O subchannel private data. */
1027 sch->private = kzalloc(sizeof(struct io_subchannel_private),
1028 GFP_KERNEL | GFP_DMA);
Cornelia Huck90ed2b62008-12-25 13:39:09 +01001029 if (!sch->private)
Peter Oberparleiter48e4c382009-12-07 12:51:15 +01001030 goto out_schedule;
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001031 css_schedule_eval(sch->schid);
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001032 return 0;
Peter Oberparleiter48e4c382009-12-07 12:51:15 +01001033
Cornelia Huck90ed2b62008-12-25 13:39:09 +01001034out_schedule:
Peter Oberparleiter390935a2009-12-07 12:51:18 +01001035 spin_lock_irq(sch->lock);
1036 css_sched_sch_todo(sch, SCH_TODO_UNREG);
1037 spin_unlock_irq(sch->lock);
Cornelia Huck90ed2b62008-12-25 13:39:09 +01001038 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039}
1040
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041static int
Cornelia Huck8bbace72006-01-11 10:56:22 +01001042io_subchannel_remove (struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043{
1044 struct ccw_device *cdev;
1045 unsigned long flags;
1046
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001047 cdev = sch_get_cdev(sch);
1048 if (!cdev)
Peter Oberparleiter48e4c382009-12-07 12:51:15 +01001049 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 /* Set ccw device to not operational and drop reference. */
1051 spin_lock_irqsave(cdev->ccwlock, flags);
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001052 sch_set_cdev(sch, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 cdev->private->state = DEV_STATE_NOT_OPER;
1054 spin_unlock_irqrestore(cdev->ccwlock, flags);
Cornelia Huckef995162007-04-27 16:01:39 +02001055 ccw_device_unregister(cdev);
Peter Oberparleiter48e4c382009-12-07 12:51:15 +01001056out_free:
Cornelia Huckcd6b4f22008-01-26 14:10:43 +01001057 kfree(sch->private);
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001058 sysfs_remove_group(&sch->dev.kobj, &io_subchannel_attr_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 return 0;
1060}
1061
Cornelia Huck602b20f2008-01-26 14:10:39 +01001062static void io_subchannel_verify(struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063{
1064 struct ccw_device *cdev;
1065
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001066 cdev = sch_get_cdev(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 if (cdev)
1068 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1069}
1070
Cornelia Huckc820de32008-07-14 09:58:45 +02001071static void io_subchannel_terminate_path(struct subchannel *sch, u8 mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072{
1073 struct ccw_device *cdev;
1074
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001075 cdev = sch_get_cdev(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 if (!cdev)
1077 return;
Peter Oberparleiter4257aae2009-12-07 12:51:29 +01001078 if (cio_update_schib(sch))
1079 goto err;
1080 /* Check for I/O on path. */
1081 if (scsw_actl(&sch->schib.scsw) == 0 || sch->schib.pmcw.lpum != mask)
1082 goto out;
1083 if (cdev->private->state == DEV_STATE_ONLINE) {
1084 ccw_device_kill_io(cdev);
1085 goto out;
1086 }
1087 if (cio_clear(sch))
1088 goto err;
1089out:
1090 /* Trigger path verification. */
1091 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1092 return;
Cornelia Huckc820de32008-07-14 09:58:45 +02001093
Peter Oberparleiter4257aae2009-12-07 12:51:29 +01001094err:
1095 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
Cornelia Huckc820de32008-07-14 09:58:45 +02001096}
1097
Cornelia Huck99611f82008-07-14 09:59:02 +02001098static int io_subchannel_chp_event(struct subchannel *sch,
1099 struct chp_link *link, int event)
Cornelia Huckc820de32008-07-14 09:58:45 +02001100{
1101 int mask;
Cornelia Huckc820de32008-07-14 09:58:45 +02001102
Cornelia Huck99611f82008-07-14 09:59:02 +02001103 mask = chp_ssd_get_mask(&sch->ssd_info, link);
Cornelia Huckc820de32008-07-14 09:58:45 +02001104 if (!mask)
1105 return 0;
1106 switch (event) {
1107 case CHP_VARY_OFF:
1108 sch->opm &= ~mask;
1109 sch->lpm &= ~mask;
1110 io_subchannel_terminate_path(sch, mask);
1111 break;
1112 case CHP_VARY_ON:
1113 sch->opm |= mask;
1114 sch->lpm |= mask;
1115 io_subchannel_verify(sch);
1116 break;
1117 case CHP_OFFLINE:
Sebastian Ottcdb912a2008-12-25 13:39:12 +01001118 if (cio_update_schib(sch))
Cornelia Huckc820de32008-07-14 09:58:45 +02001119 return -ENODEV;
1120 io_subchannel_terminate_path(sch, mask);
1121 break;
1122 case CHP_ONLINE:
Sebastian Ottcdb912a2008-12-25 13:39:12 +01001123 if (cio_update_schib(sch))
1124 return -ENODEV;
Cornelia Huckc820de32008-07-14 09:58:45 +02001125 sch->lpm |= mask & sch->opm;
1126 io_subchannel_verify(sch);
1127 break;
1128 }
1129 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130}
1131
1132static void
Cornelia Huck8bbace72006-01-11 10:56:22 +01001133io_subchannel_shutdown(struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 struct ccw_device *cdev;
1136 int ret;
1137
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001138 cdev = sch_get_cdev(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139
Cornelia Hucka8237fc2006-01-06 00:19:21 -08001140 if (cio_is_console(sch->schid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 return;
1142 if (!sch->schib.pmcw.ena)
1143 /* Nothing to do. */
1144 return;
1145 ret = cio_disable_subchannel(sch);
1146 if (ret != -EBUSY)
1147 /* Subchannel is disabled, we're done. */
1148 return;
1149 cdev->private->state = DEV_STATE_QUIESCE;
1150 if (cdev->handler)
1151 cdev->handler(cdev, cdev->private->intparm,
1152 ERR_PTR(-EIO));
1153 ret = ccw_device_cancel_halt_clear(cdev);
1154 if (ret == -EBUSY) {
1155 ccw_device_set_timeout(cdev, HZ/10);
1156 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
1157 }
1158 cio_disable_subchannel(sch);
1159}
1160
Cornelia Huckc820de32008-07-14 09:58:45 +02001161static int device_is_disconnected(struct ccw_device *cdev)
1162{
1163 if (!cdev)
1164 return 0;
1165 return (cdev->private->state == DEV_STATE_DISCONNECTED ||
1166 cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID);
1167}
1168
1169static int recovery_check(struct device *dev, void *data)
1170{
1171 struct ccw_device *cdev = to_ccwdev(dev);
1172 int *redo = data;
1173
1174 spin_lock_irq(cdev->ccwlock);
1175 switch (cdev->private->state) {
1176 case DEV_STATE_DISCONNECTED:
1177 CIO_MSG_EVENT(3, "recovery: trigger 0.%x.%04x\n",
1178 cdev->private->dev_id.ssid,
1179 cdev->private->dev_id.devno);
1180 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1181 *redo = 1;
1182 break;
1183 case DEV_STATE_DISCONNECTED_SENSE_ID:
1184 *redo = 1;
1185 break;
1186 }
1187 spin_unlock_irq(cdev->ccwlock);
1188
1189 return 0;
1190}
1191
1192static void recovery_work_func(struct work_struct *unused)
1193{
1194 int redo = 0;
1195
1196 bus_for_each_dev(&ccw_bus_type, NULL, &redo, recovery_check);
1197 if (redo) {
1198 spin_lock_irq(&recovery_lock);
1199 if (!timer_pending(&recovery_timer)) {
1200 if (recovery_phase < ARRAY_SIZE(recovery_delay) - 1)
1201 recovery_phase++;
1202 mod_timer(&recovery_timer, jiffies +
1203 recovery_delay[recovery_phase] * HZ);
1204 }
1205 spin_unlock_irq(&recovery_lock);
1206 } else
1207 CIO_MSG_EVENT(4, "recovery: end\n");
1208}
1209
1210static DECLARE_WORK(recovery_work, recovery_work_func);
1211
1212static void recovery_func(unsigned long data)
1213{
1214 /*
1215 * We can't do our recovery in softirq context and it's not
1216 * performance critical, so we schedule it.
1217 */
1218 schedule_work(&recovery_work);
1219}
1220
1221static void ccw_device_schedule_recovery(void)
1222{
1223 unsigned long flags;
1224
1225 CIO_MSG_EVENT(4, "recovery: schedule\n");
1226 spin_lock_irqsave(&recovery_lock, flags);
1227 if (!timer_pending(&recovery_timer) || (recovery_phase != 0)) {
1228 recovery_phase = 0;
1229 mod_timer(&recovery_timer, jiffies + recovery_delay[0] * HZ);
1230 }
1231 spin_unlock_irqrestore(&recovery_lock, flags);
1232}
1233
Peter Oberparleiterecf5d9e2008-10-10 21:33:06 +02001234static int purge_fn(struct device *dev, void *data)
1235{
1236 struct ccw_device *cdev = to_ccwdev(dev);
Peter Oberparleiter37de53b2009-12-07 12:51:19 +01001237 struct ccw_dev_id *id = &cdev->private->dev_id;
Peter Oberparleiterecf5d9e2008-10-10 21:33:06 +02001238
1239 spin_lock_irq(cdev->ccwlock);
Peter Oberparleiter37de53b2009-12-07 12:51:19 +01001240 if (is_blacklisted(id->ssid, id->devno) &&
1241 (cdev->private->state == DEV_STATE_OFFLINE)) {
1242 CIO_MSG_EVENT(3, "ccw: purging 0.%x.%04x\n", id->ssid,
1243 id->devno);
1244 ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
1245 }
Peter Oberparleiterecf5d9e2008-10-10 21:33:06 +02001246 spin_unlock_irq(cdev->ccwlock);
Peter Oberparleiterecf5d9e2008-10-10 21:33:06 +02001247 /* Abort loop in case of pending signal. */
1248 if (signal_pending(current))
1249 return -EINTR;
1250
1251 return 0;
1252}
1253
1254/**
1255 * ccw_purge_blacklisted - purge unused, blacklisted devices
1256 *
1257 * Unregister all ccw devices that are offline and on the blacklist.
1258 */
1259int ccw_purge_blacklisted(void)
1260{
1261 CIO_MSG_EVENT(2, "ccw: purging blacklisted devices\n");
1262 bus_for_each_dev(&ccw_bus_type, NULL, NULL, purge_fn);
1263 return 0;
1264}
1265
Peter Oberparleiter6afcc772009-10-06 10:34:02 +02001266void ccw_device_set_disconnected(struct ccw_device *cdev)
Cornelia Huckc820de32008-07-14 09:58:45 +02001267{
1268 if (!cdev)
1269 return;
1270 ccw_device_set_timeout(cdev, 0);
1271 cdev->private->flags.fake_irb = 0;
1272 cdev->private->state = DEV_STATE_DISCONNECTED;
1273 if (cdev->online)
1274 ccw_device_schedule_recovery();
1275}
1276
Peter Oberparleiter91c36912008-08-21 19:46:39 +02001277void ccw_device_set_notoper(struct ccw_device *cdev)
1278{
1279 struct subchannel *sch = to_subchannel(cdev->dev.parent);
1280
1281 CIO_TRACE_EVENT(2, "notoper");
Cornelia Huckb9d3aed2008-10-10 21:33:11 +02001282 CIO_TRACE_EVENT(2, dev_name(&sch->dev));
Peter Oberparleiter91c36912008-08-21 19:46:39 +02001283 ccw_device_set_timeout(cdev, 0);
1284 cio_disable_subchannel(sch);
1285 cdev->private->state = DEV_STATE_NOT_OPER;
1286}
1287
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001288enum io_sch_action {
1289 IO_SCH_UNREG,
1290 IO_SCH_ORPH_UNREG,
1291 IO_SCH_ATTACH,
1292 IO_SCH_UNREG_ATTACH,
1293 IO_SCH_ORPH_ATTACH,
1294 IO_SCH_REPROBE,
1295 IO_SCH_VERIFY,
1296 IO_SCH_DISC,
1297 IO_SCH_NOP,
1298};
1299
1300static enum io_sch_action sch_get_action(struct subchannel *sch)
Cornelia Huckc820de32008-07-14 09:58:45 +02001301{
Cornelia Huckc820de32008-07-14 09:58:45 +02001302 struct ccw_device *cdev;
1303
Cornelia Huckc820de32008-07-14 09:58:45 +02001304 cdev = sch_get_cdev(sch);
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001305 if (cio_update_schib(sch)) {
1306 /* Not operational. */
1307 if (!cdev)
1308 return IO_SCH_UNREG;
1309 if (!ccw_device_notify(cdev, CIO_GONE))
1310 return IO_SCH_UNREG;
1311 return IO_SCH_ORPH_UNREG;
Cornelia Huckc820de32008-07-14 09:58:45 +02001312 }
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001313 /* Operational. */
1314 if (!cdev)
1315 return IO_SCH_ATTACH;
1316 if (sch->schib.pmcw.dev != cdev->private->dev_id.devno) {
1317 if (!ccw_device_notify(cdev, CIO_GONE))
1318 return IO_SCH_UNREG_ATTACH;
1319 return IO_SCH_ORPH_ATTACH;
Cornelia Huckc820de32008-07-14 09:58:45 +02001320 }
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001321 if ((sch->schib.pmcw.pam & sch->opm) == 0) {
1322 if (!ccw_device_notify(cdev, CIO_NO_PATH))
1323 return IO_SCH_UNREG;
1324 return IO_SCH_DISC;
Cornelia Huckc820de32008-07-14 09:58:45 +02001325 }
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001326 if (device_is_disconnected(cdev))
1327 return IO_SCH_REPROBE;
1328 if (cdev->online)
1329 return IO_SCH_VERIFY;
1330 return IO_SCH_NOP;
1331}
1332
1333/**
1334 * io_subchannel_sch_event - process subchannel event
1335 * @sch: subchannel
1336 * @process: non-zero if function is called in process context
1337 *
1338 * An unspecified event occurred for this subchannel. Adjust data according
1339 * to the current operational state of the subchannel and device. Return
1340 * zero when the event has been handled sufficiently or -EAGAIN when this
1341 * function should be called again in process context.
1342 */
1343static int io_subchannel_sch_event(struct subchannel *sch, int process)
1344{
1345 unsigned long flags;
1346 struct ccw_device *cdev;
1347 struct ccw_dev_id dev_id;
1348 enum io_sch_action action;
1349 int rc = -EAGAIN;
1350
1351 spin_lock_irqsave(sch->lock, flags);
1352 if (!device_is_registered(&sch->dev))
1353 goto out_unlock;
Peter Oberparleiter390935a2009-12-07 12:51:18 +01001354 if (work_pending(&sch->todo_work))
1355 goto out_unlock;
Peter Oberparleiter37de53b2009-12-07 12:51:19 +01001356 cdev = sch_get_cdev(sch);
1357 if (cdev && work_pending(&cdev->private->todo_work))
1358 goto out_unlock;
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001359 action = sch_get_action(sch);
1360 CIO_MSG_EVENT(2, "event: sch 0.%x.%04x, process=%d, action=%d\n",
1361 sch->schid.ssid, sch->schid.sch_no, process,
1362 action);
1363 /* Perform immediate actions while holding the lock. */
Cornelia Huckc820de32008-07-14 09:58:45 +02001364 switch (action) {
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001365 case IO_SCH_REPROBE:
1366 /* Trigger device recognition. */
Cornelia Huckc820de32008-07-14 09:58:45 +02001367 ccw_device_trigger_reprobe(cdev);
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001368 rc = 0;
1369 goto out_unlock;
1370 case IO_SCH_VERIFY:
1371 /* Trigger path verification. */
1372 io_subchannel_verify(sch);
1373 rc = 0;
1374 goto out_unlock;
1375 case IO_SCH_DISC:
1376 ccw_device_set_disconnected(cdev);
1377 rc = 0;
1378 goto out_unlock;
1379 case IO_SCH_ORPH_UNREG:
1380 case IO_SCH_ORPH_ATTACH:
Peter Oberparleiter6afcc772009-10-06 10:34:02 +02001381 ccw_device_set_disconnected(cdev);
Peter Oberparleiter91c36912008-08-21 19:46:39 +02001382 break;
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001383 case IO_SCH_UNREG_ATTACH:
1384 case IO_SCH_UNREG:
1385 if (cdev)
1386 ccw_device_set_notoper(cdev);
1387 break;
1388 case IO_SCH_NOP:
1389 rc = 0;
1390 goto out_unlock;
Cornelia Huckc820de32008-07-14 09:58:45 +02001391 default:
1392 break;
1393 }
1394 spin_unlock_irqrestore(sch->lock, flags);
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001395 /* All other actions require process context. */
1396 if (!process)
1397 goto out;
1398 /* Handle attached ccw device. */
1399 switch (action) {
1400 case IO_SCH_ORPH_UNREG:
1401 case IO_SCH_ORPH_ATTACH:
1402 /* Move ccw device to orphanage. */
1403 rc = ccw_device_move_to_orph(cdev);
1404 if (rc)
1405 goto out;
1406 break;
1407 case IO_SCH_UNREG_ATTACH:
1408 /* Unregister ccw device. */
1409 ccw_device_unregister(cdev);
1410 break;
1411 default:
1412 break;
1413 }
1414 /* Handle subchannel. */
1415 switch (action) {
1416 case IO_SCH_ORPH_UNREG:
1417 case IO_SCH_UNREG:
1418 css_sch_device_unregister(sch);
1419 break;
1420 case IO_SCH_ORPH_ATTACH:
1421 case IO_SCH_UNREG_ATTACH:
1422 case IO_SCH_ATTACH:
1423 dev_id.ssid = sch->schid.ssid;
1424 dev_id.devno = sch->schib.pmcw.dev;
1425 cdev = get_ccwdev_by_dev_id(&dev_id);
1426 if (!cdev) {
1427 sch_create_and_recog_new_device(sch);
1428 break;
1429 }
1430 rc = ccw_device_move_to_sch(cdev, sch);
1431 if (rc) {
1432 /* Release reference from get_ccwdev_by_dev_id() */
1433 put_device(&cdev->dev);
1434 goto out;
1435 }
1436 spin_lock_irqsave(sch->lock, flags);
1437 ccw_device_trigger_reprobe(cdev);
1438 spin_unlock_irqrestore(sch->lock, flags);
1439 /* Release reference from get_ccwdev_by_dev_id() */
1440 put_device(&cdev->dev);
1441 break;
1442 default:
1443 break;
1444 }
1445 return 0;
Cornelia Huckc820de32008-07-14 09:58:45 +02001446
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001447out_unlock:
1448 spin_unlock_irqrestore(sch->lock, flags);
1449out:
1450 return rc;
Cornelia Huckc820de32008-07-14 09:58:45 +02001451}
1452
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453#ifdef CONFIG_CCW_CONSOLE
1454static struct ccw_device console_cdev;
1455static struct ccw_device_private console_private;
1456static int console_cdev_in_use;
1457
Cornelia Huck2ec22982006-12-08 15:54:26 +01001458static DEFINE_SPINLOCK(ccw_console_lock);
1459
1460spinlock_t * cio_get_console_lock(void)
1461{
1462 return &ccw_console_lock;
1463}
1464
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001465static int ccw_device_console_enable(struct ccw_device *cdev,
1466 struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467{
1468 int rc;
1469
Cornelia Huckcd6b4f22008-01-26 14:10:43 +01001470 /* Attach subchannel private data. */
1471 sch->private = cio_get_console_priv();
1472 memset(sch->private, 0, sizeof(struct io_subchannel_private));
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001473 io_subchannel_init_fields(sch);
Sebastian Ottf444cc02008-12-25 13:39:14 +01001474 rc = cio_commit_config(sch);
1475 if (rc)
1476 return rc;
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001477 sch->driver = &io_subchannel_driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 /* Initialize the ccw_device structure. */
Heiko Carstens292888c2006-08-30 14:33:35 +02001479 cdev->dev.parent= &sch->dev;
Peter Oberparleiter736b5db2009-12-07 12:51:21 +01001480 io_subchannel_recog(cdev, sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 /* Now wait for the async. recognition to come to an end. */
1482 spin_lock_irq(cdev->ccwlock);
1483 while (!dev_fsm_final_state(cdev))
1484 wait_cons_dev();
1485 rc = -EIO;
1486 if (cdev->private->state != DEV_STATE_OFFLINE)
1487 goto out_unlock;
1488 ccw_device_online(cdev);
1489 while (!dev_fsm_final_state(cdev))
1490 wait_cons_dev();
1491 if (cdev->private->state != DEV_STATE_ONLINE)
1492 goto out_unlock;
1493 rc = 0;
1494out_unlock:
1495 spin_unlock_irq(cdev->ccwlock);
Peter Oberparleiter736b5db2009-12-07 12:51:21 +01001496 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497}
1498
1499struct ccw_device *
1500ccw_device_probe_console(void)
1501{
1502 struct subchannel *sch;
1503 int ret;
1504
1505 if (xchg(&console_cdev_in_use, 1) != 0)
Peter Oberparleiter600b5d12006-02-01 03:06:35 -08001506 return ERR_PTR(-EBUSY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 sch = cio_probe_console();
1508 if (IS_ERR(sch)) {
1509 console_cdev_in_use = 0;
1510 return (void *) sch;
1511 }
1512 memset(&console_cdev, 0, sizeof(struct ccw_device));
1513 memset(&console_private, 0, sizeof(struct ccw_device_private));
1514 console_cdev.private = &console_private;
Martin Schwidefskyc1637532006-12-08 15:53:57 +01001515 console_private.cdev = &console_cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 ret = ccw_device_console_enable(&console_cdev, sch);
1517 if (ret) {
1518 cio_release_console();
1519 console_cdev_in_use = 0;
1520 return ERR_PTR(ret);
1521 }
1522 console_cdev.online = 1;
1523 return &console_cdev;
1524}
Cornelia Huck1f4e7ed2008-10-10 21:33:14 +02001525
Martin Schwidefsky66648452009-06-16 10:30:28 +02001526static int ccw_device_pm_restore(struct device *dev);
1527
1528int ccw_device_force_console(void)
1529{
1530 if (!console_cdev_in_use)
1531 return -ENODEV;
1532 return ccw_device_pm_restore(&console_cdev.dev);
1533}
1534EXPORT_SYMBOL_GPL(ccw_device_force_console);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535#endif
1536
1537/*
1538 * get ccw_device matching the busid, but only if owned by cdrv
1539 */
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001540static int
1541__ccwdev_check_busid(struct device *dev, void *id)
1542{
1543 char *bus_id;
1544
Cornelia Huck12975ae2006-10-11 15:31:47 +02001545 bus_id = id;
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001546
Kay Sievers98df67b2008-12-25 13:38:55 +01001547 return (strcmp(bus_id, dev_name(dev)) == 0);
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001548}
1549
1550
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +02001551/**
1552 * get_ccwdev_by_busid() - obtain device from a bus id
1553 * @cdrv: driver the device is owned by
1554 * @bus_id: bus id of the device to be searched
1555 *
1556 * This function searches all devices owned by @cdrv for a device with a bus
1557 * id matching @bus_id.
1558 * Returns:
1559 * If a match is found, its reference count of the found device is increased
1560 * and it is returned; else %NULL is returned.
1561 */
1562struct ccw_device *get_ccwdev_by_busid(struct ccw_driver *cdrv,
1563 const char *bus_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564{
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001565 struct device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 struct device_driver *drv;
1567
1568 drv = get_driver(&cdrv->driver);
1569 if (!drv)
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001570 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001572 dev = driver_find_device(drv, NULL, (void *)bus_id,
1573 __ccwdev_check_busid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 put_driver(drv);
1575
Heiko Carstensd2c993d2006-07-12 16:41:55 +02001576 return dev ? to_ccwdev(dev) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577}
1578
1579/************************** device driver handling ************************/
1580
1581/* This is the implementation of the ccw_driver class. The probe, remove
1582 * and release methods are initially very similar to the device_driver
1583 * implementations, with the difference that they have ccw_device
1584 * arguments.
1585 *
1586 * A ccw driver also contains the information that is needed for
1587 * device matching.
1588 */
1589static int
1590ccw_device_probe (struct device *dev)
1591{
1592 struct ccw_device *cdev = to_ccwdev(dev);
1593 struct ccw_driver *cdrv = to_ccwdrv(dev->driver);
1594 int ret;
1595
1596 cdev->drv = cdrv; /* to let the driver call _set_online */
1597
1598 ret = cdrv->probe ? cdrv->probe(cdev) : -ENODEV;
1599
1600 if (ret) {
Heiko Carstensd2c993d2006-07-12 16:41:55 +02001601 cdev->drv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 return ret;
1603 }
1604
1605 return 0;
1606}
1607
1608static int
1609ccw_device_remove (struct device *dev)
1610{
1611 struct ccw_device *cdev = to_ccwdev(dev);
1612 struct ccw_driver *cdrv = cdev->drv;
1613 int ret;
1614
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 if (cdrv->remove)
1616 cdrv->remove(cdev);
1617 if (cdev->online) {
1618 cdev->online = 0;
1619 spin_lock_irq(cdev->ccwlock);
1620 ret = ccw_device_offline(cdev);
1621 spin_unlock_irq(cdev->ccwlock);
1622 if (ret == 0)
1623 wait_event(cdev->private->wait_q,
1624 dev_fsm_final_state(cdev));
1625 else
Michael Ernst139b83dd2008-05-07 09:22:54 +02001626 CIO_MSG_EVENT(0, "ccw_device_offline returned %d, "
Cornelia Hucke556bbb2007-07-27 12:29:19 +02001627 "device 0.%x.%04x\n",
1628 ret, cdev->private->dev_id.ssid,
1629 cdev->private->dev_id.devno);
Cornelia Huck9cd67422008-12-25 13:39:06 +01001630 /* Give up reference obtained in ccw_device_set_online(). */
1631 put_device(&cdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 }
1633 ccw_device_set_timeout(cdev, 0);
Heiko Carstensd2c993d2006-07-12 16:41:55 +02001634 cdev->drv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 return 0;
1636}
1637
Cornelia Huck958974fb2007-10-12 16:11:21 +02001638static void ccw_device_shutdown(struct device *dev)
1639{
1640 struct ccw_device *cdev;
1641
1642 cdev = to_ccwdev(dev);
1643 if (cdev->drv && cdev->drv->shutdown)
1644 cdev->drv->shutdown(cdev);
Cornelia Huck1842f2b2007-10-12 16:11:22 +02001645 disable_cmf(cdev);
Cornelia Huck958974fb2007-10-12 16:11:21 +02001646}
1647
Sebastian Ott823d4942009-06-16 10:30:20 +02001648static int ccw_device_pm_prepare(struct device *dev)
1649{
1650 struct ccw_device *cdev = to_ccwdev(dev);
1651
Peter Oberparleiter37de53b2009-12-07 12:51:19 +01001652 if (work_pending(&cdev->private->todo_work))
Sebastian Ott823d4942009-06-16 10:30:20 +02001653 return -EAGAIN;
1654 /* Fail while device is being set online/offline. */
1655 if (atomic_read(&cdev->private->onoff))
1656 return -EAGAIN;
1657
1658 if (cdev->online && cdev->drv && cdev->drv->prepare)
1659 return cdev->drv->prepare(cdev);
1660
1661 return 0;
1662}
1663
1664static void ccw_device_pm_complete(struct device *dev)
1665{
1666 struct ccw_device *cdev = to_ccwdev(dev);
1667
1668 if (cdev->online && cdev->drv && cdev->drv->complete)
1669 cdev->drv->complete(cdev);
1670}
1671
1672static int ccw_device_pm_freeze(struct device *dev)
1673{
1674 struct ccw_device *cdev = to_ccwdev(dev);
1675 struct subchannel *sch = to_subchannel(cdev->dev.parent);
1676 int ret, cm_enabled;
1677
1678 /* Fail suspend while device is in transistional state. */
1679 if (!dev_fsm_final_state(cdev))
1680 return -EAGAIN;
1681 if (!cdev->online)
1682 return 0;
1683 if (cdev->drv && cdev->drv->freeze) {
1684 ret = cdev->drv->freeze(cdev);
1685 if (ret)
1686 return ret;
1687 }
1688
1689 spin_lock_irq(sch->lock);
1690 cm_enabled = cdev->private->cmb != NULL;
1691 spin_unlock_irq(sch->lock);
1692 if (cm_enabled) {
1693 /* Don't have the css write on memory. */
1694 ret = ccw_set_cmf(cdev, 0);
1695 if (ret)
1696 return ret;
1697 }
1698 /* From here on, disallow device driver I/O. */
1699 spin_lock_irq(sch->lock);
1700 ret = cio_disable_subchannel(sch);
1701 spin_unlock_irq(sch->lock);
1702
1703 return ret;
1704}
1705
1706static int ccw_device_pm_thaw(struct device *dev)
1707{
1708 struct ccw_device *cdev = to_ccwdev(dev);
1709 struct subchannel *sch = to_subchannel(cdev->dev.parent);
1710 int ret, cm_enabled;
1711
1712 if (!cdev->online)
1713 return 0;
1714
1715 spin_lock_irq(sch->lock);
1716 /* Allow device driver I/O again. */
1717 ret = cio_enable_subchannel(sch, (u32)(addr_t)sch);
1718 cm_enabled = cdev->private->cmb != NULL;
1719 spin_unlock_irq(sch->lock);
1720 if (ret)
1721 return ret;
1722
1723 if (cm_enabled) {
1724 ret = ccw_set_cmf(cdev, 1);
1725 if (ret)
1726 return ret;
1727 }
1728
1729 if (cdev->drv && cdev->drv->thaw)
1730 ret = cdev->drv->thaw(cdev);
1731
1732 return ret;
1733}
1734
1735static void __ccw_device_pm_restore(struct ccw_device *cdev)
1736{
1737 struct subchannel *sch = to_subchannel(cdev->dev.parent);
Sebastian Ott823d4942009-06-16 10:30:20 +02001738
1739 if (cio_is_console(sch->schid))
1740 goto out;
1741 /*
1742 * While we were sleeping, devices may have gone or become
1743 * available again. Kick re-detection.
1744 */
1745 spin_lock_irq(sch->lock);
1746 cdev->private->flags.resuming = 1;
Peter Oberparleiter736b5db2009-12-07 12:51:21 +01001747 ccw_device_recognition(cdev);
Sebastian Ott823d4942009-06-16 10:30:20 +02001748 spin_unlock_irq(sch->lock);
Sebastian Ott823d4942009-06-16 10:30:20 +02001749 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev) ||
1750 cdev->private->state == DEV_STATE_DISCONNECTED);
Sebastian Ott823d4942009-06-16 10:30:20 +02001751out:
1752 cdev->private->flags.resuming = 0;
1753}
1754
1755static int resume_handle_boxed(struct ccw_device *cdev)
1756{
1757 cdev->private->state = DEV_STATE_BOXED;
1758 if (ccw_device_notify(cdev, CIO_BOXED))
1759 return 0;
Peter Oberparleiter37de53b2009-12-07 12:51:19 +01001760 ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
Sebastian Ott823d4942009-06-16 10:30:20 +02001761 return -ENODEV;
1762}
1763
1764static int resume_handle_disc(struct ccw_device *cdev)
1765{
1766 cdev->private->state = DEV_STATE_DISCONNECTED;
1767 if (ccw_device_notify(cdev, CIO_GONE))
1768 return 0;
Peter Oberparleiter37de53b2009-12-07 12:51:19 +01001769 ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
Sebastian Ott823d4942009-06-16 10:30:20 +02001770 return -ENODEV;
1771}
1772
1773static int ccw_device_pm_restore(struct device *dev)
1774{
1775 struct ccw_device *cdev = to_ccwdev(dev);
1776 struct subchannel *sch = to_subchannel(cdev->dev.parent);
1777 int ret = 0, cm_enabled;
1778
1779 __ccw_device_pm_restore(cdev);
1780 spin_lock_irq(sch->lock);
1781 if (cio_is_console(sch->schid)) {
1782 cio_enable_subchannel(sch, (u32)(addr_t)sch);
1783 spin_unlock_irq(sch->lock);
1784 goto out_restore;
1785 }
1786 cdev->private->flags.donotify = 0;
1787 /* check recognition results */
1788 switch (cdev->private->state) {
1789 case DEV_STATE_OFFLINE:
1790 break;
1791 case DEV_STATE_BOXED:
1792 ret = resume_handle_boxed(cdev);
1793 spin_unlock_irq(sch->lock);
1794 if (ret)
1795 goto out;
1796 goto out_restore;
1797 case DEV_STATE_DISCONNECTED:
1798 goto out_disc_unlock;
1799 default:
1800 goto out_unreg_unlock;
1801 }
1802 /* check if the device id has changed */
1803 if (sch->schib.pmcw.dev != cdev->private->dev_id.devno) {
Sebastian Ottf0148242009-09-11 10:28:23 +02001804 CIO_MSG_EVENT(0, "resume: sch 0.%x.%04x: failed (devno "
1805 "changed from %04x to %04x)\n",
1806 sch->schid.ssid, sch->schid.sch_no,
Sebastian Ott823d4942009-06-16 10:30:20 +02001807 cdev->private->dev_id.devno,
1808 sch->schib.pmcw.dev);
1809 goto out_unreg_unlock;
1810 }
1811 /* check if the device type has changed */
1812 if (!ccw_device_test_sense_data(cdev)) {
1813 ccw_device_update_sense_data(cdev);
Peter Oberparleiter37de53b2009-12-07 12:51:19 +01001814 ccw_device_sched_todo(cdev, CDEV_TODO_REBIND);
Sebastian Ott823d4942009-06-16 10:30:20 +02001815 ret = -ENODEV;
1816 goto out_unlock;
1817 }
1818 if (!cdev->online) {
1819 ret = 0;
1820 goto out_unlock;
1821 }
1822 ret = ccw_device_online(cdev);
1823 if (ret)
1824 goto out_disc_unlock;
1825
1826 cm_enabled = cdev->private->cmb != NULL;
1827 spin_unlock_irq(sch->lock);
1828
1829 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
1830 if (cdev->private->state != DEV_STATE_ONLINE) {
1831 spin_lock_irq(sch->lock);
1832 goto out_disc_unlock;
1833 }
1834 if (cm_enabled) {
1835 ret = ccw_set_cmf(cdev, 1);
1836 if (ret) {
Sebastian Ottf0148242009-09-11 10:28:23 +02001837 CIO_MSG_EVENT(2, "resume: cdev 0.%x.%04x: cmf failed "
1838 "(rc=%d)\n", cdev->private->dev_id.ssid,
1839 cdev->private->dev_id.devno, ret);
Sebastian Ott823d4942009-06-16 10:30:20 +02001840 ret = 0;
1841 }
1842 }
1843
1844out_restore:
1845 if (cdev->online && cdev->drv && cdev->drv->restore)
1846 ret = cdev->drv->restore(cdev);
1847out:
1848 return ret;
1849
1850out_disc_unlock:
1851 ret = resume_handle_disc(cdev);
1852 spin_unlock_irq(sch->lock);
1853 if (ret)
1854 return ret;
1855 goto out_restore;
1856
1857out_unreg_unlock:
Peter Oberparleiter37de53b2009-12-07 12:51:19 +01001858 ccw_device_sched_todo(cdev, CDEV_TODO_UNREG_EVAL);
Sebastian Ott823d4942009-06-16 10:30:20 +02001859 ret = -ENODEV;
1860out_unlock:
1861 spin_unlock_irq(sch->lock);
1862 return ret;
1863}
1864
1865static struct dev_pm_ops ccw_pm_ops = {
1866 .prepare = ccw_device_pm_prepare,
1867 .complete = ccw_device_pm_complete,
1868 .freeze = ccw_device_pm_freeze,
1869 .thaw = ccw_device_pm_thaw,
1870 .restore = ccw_device_pm_restore,
1871};
1872
Cornelia Huck8bbace72006-01-11 10:56:22 +01001873struct bus_type ccw_bus_type = {
1874 .name = "ccw",
1875 .match = ccw_bus_match,
1876 .uevent = ccw_uevent,
1877 .probe = ccw_device_probe,
1878 .remove = ccw_device_remove,
Cornelia Huck958974fb2007-10-12 16:11:21 +02001879 .shutdown = ccw_device_shutdown,
Sebastian Ott823d4942009-06-16 10:30:20 +02001880 .pm = &ccw_pm_ops,
Cornelia Huck8bbace72006-01-11 10:56:22 +01001881};
1882
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +02001883/**
1884 * ccw_driver_register() - register a ccw driver
1885 * @cdriver: driver to be registered
1886 *
1887 * This function is mainly a wrapper around driver_register().
1888 * Returns:
1889 * %0 on success and a negative error value on failure.
1890 */
1891int ccw_driver_register(struct ccw_driver *cdriver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892{
1893 struct device_driver *drv = &cdriver->driver;
1894
1895 drv->bus = &ccw_bus_type;
1896 drv->name = cdriver->name;
Cornelia Huck4beee642008-01-26 14:10:47 +01001897 drv->owner = cdriver->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898
1899 return driver_register(drv);
1900}
1901
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +02001902/**
1903 * ccw_driver_unregister() - deregister a ccw driver
1904 * @cdriver: driver to be deregistered
1905 *
1906 * This function is mainly a wrapper around driver_unregister().
1907 */
1908void ccw_driver_unregister(struct ccw_driver *cdriver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909{
1910 driver_unregister(&cdriver->driver);
1911}
1912
Cornelia Hucka8237fc2006-01-06 00:19:21 -08001913/* Helper func for qdio. */
1914struct subchannel_id
1915ccw_device_get_subchannel_id(struct ccw_device *cdev)
1916{
1917 struct subchannel *sch;
1918
1919 sch = to_subchannel(cdev->dev.parent);
1920 return sch->schid;
1921}
1922
Peter Oberparleiter37de53b2009-12-07 12:51:19 +01001923static void ccw_device_todo(struct work_struct *work)
1924{
1925 struct ccw_device_private *priv;
1926 struct ccw_device *cdev;
1927 struct subchannel *sch;
1928 enum cdev_todo todo;
1929
1930 priv = container_of(work, struct ccw_device_private, todo_work);
1931 cdev = priv->cdev;
1932 sch = to_subchannel(cdev->dev.parent);
1933 /* Find out todo. */
1934 spin_lock_irq(cdev->ccwlock);
1935 todo = priv->todo;
1936 priv->todo = CDEV_TODO_NOTHING;
1937 CIO_MSG_EVENT(4, "cdev_todo: cdev=0.%x.%04x todo=%d\n",
1938 priv->dev_id.ssid, priv->dev_id.devno, todo);
1939 spin_unlock_irq(cdev->ccwlock);
1940 /* Perform todo. */
1941 switch (todo) {
1942 case CDEV_TODO_ENABLE_CMF:
1943 cmf_reenable(cdev);
1944 break;
1945 case CDEV_TODO_REBIND:
1946 ccw_device_do_unbind_bind(cdev);
1947 break;
1948 case CDEV_TODO_REGISTER:
1949 io_subchannel_register(cdev);
1950 break;
1951 case CDEV_TODO_UNREG_EVAL:
1952 if (!sch_is_pseudo_sch(sch))
1953 css_schedule_eval(sch->schid);
1954 /* fall-through */
1955 case CDEV_TODO_UNREG:
1956 if (sch_is_pseudo_sch(sch))
1957 ccw_device_unregister(cdev);
1958 else
1959 ccw_device_call_sch_unregister(cdev);
1960 break;
1961 default:
1962 break;
1963 }
1964 /* Release workqueue ref. */
1965 put_device(&cdev->dev);
1966}
1967
1968/**
1969 * ccw_device_sched_todo - schedule ccw device operation
1970 * @cdev: ccw device
1971 * @todo: todo
1972 *
1973 * Schedule the operation identified by @todo to be performed on the slow path
1974 * workqueue. Do nothing if another operation with higher priority is already
1975 * scheduled. Needs to be called with ccwdev lock held.
1976 */
1977void ccw_device_sched_todo(struct ccw_device *cdev, enum cdev_todo todo)
1978{
1979 CIO_MSG_EVENT(4, "cdev_todo: sched cdev=0.%x.%04x todo=%d\n",
1980 cdev->private->dev_id.ssid, cdev->private->dev_id.devno,
1981 todo);
1982 if (cdev->private->todo >= todo)
1983 return;
1984 cdev->private->todo = todo;
1985 /* Get workqueue ref. */
1986 if (!get_device(&cdev->dev))
1987 return;
1988 if (!queue_work(slow_path_wq, &cdev->private->todo_work)) {
1989 /* Already queued, release workqueue ref. */
1990 put_device(&cdev->dev);
1991 }
1992}
1993
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994MODULE_LICENSE("GPL");
1995EXPORT_SYMBOL(ccw_device_set_online);
1996EXPORT_SYMBOL(ccw_device_set_offline);
1997EXPORT_SYMBOL(ccw_driver_register);
1998EXPORT_SYMBOL(ccw_driver_unregister);
1999EXPORT_SYMBOL(get_ccwdev_by_busid);
2000EXPORT_SYMBOL(ccw_bus_type);
2001EXPORT_SYMBOL(ccw_device_work);
Cornelia Hucka8237fc2006-01-06 00:19:21 -08002002EXPORT_SYMBOL_GPL(ccw_device_get_subchannel_id);