blob: 6a9ac85065ed4c0e80f10912d9ed13a1ff538fd2 [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 Ernst139b83d2008-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
Sebastian Ottb5cd99e2009-03-31 19:16:06 +0200532 if ((cdev->private->state != DEV_STATE_OFFLINE &&
533 cdev->private->state != DEV_STATE_ONLINE &&
534 cdev->private->state != DEV_STATE_BOXED &&
535 cdev->private->state != DEV_STATE_DISCONNECTED) ||
536 atomic_cmpxchg(&cdev->private->onoff, 0, 1) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 return -EAGAIN;
538
539 if (cdev->drv && !try_module_get(cdev->drv->owner)) {
540 atomic_set(&cdev->private->onoff, 0);
541 return -EINVAL;
542 }
543 if (!strncmp(buf, "force\n", count)) {
544 force = 1;
545 i = 1;
Cornelia Huck2f972202008-04-30 13:38:33 +0200546 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 } else {
548 force = 0;
Cornelia Huck2f972202008-04-30 13:38:33 +0200549 ret = strict_strtoul(buf, 16, &i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 }
Cornelia Huck2f972202008-04-30 13:38:33 +0200551 if (ret)
552 goto out;
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200553 switch (i) {
554 case 0:
Sebastian Otte74fe0c2009-03-26 15:24:08 +0100555 ret = online_store_handle_offline(cdev);
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200556 break;
557 case 1:
Michael Ernstc78aa6c2008-07-14 09:59:22 +0200558 ret = online_store_handle_online(cdev, force);
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200559 break;
560 default:
Cornelia Huck2f972202008-04-30 13:38:33 +0200561 ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 }
Cornelia Huck2f972202008-04-30 13:38:33 +0200563out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 if (cdev->drv)
565 module_put(cdev->drv->owner);
566 atomic_set(&cdev->private->onoff, 0);
Sebastian Otte74fe0c2009-03-26 15:24:08 +0100567 return (ret < 0) ? ret : count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568}
569
570static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400571available_show (struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572{
573 struct ccw_device *cdev = to_ccwdev(dev);
574 struct subchannel *sch;
575
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100576 if (ccw_device_is_orphan(cdev))
577 return sprintf(buf, "no device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 switch (cdev->private->state) {
579 case DEV_STATE_BOXED:
580 return sprintf(buf, "boxed\n");
581 case DEV_STATE_DISCONNECTED:
582 case DEV_STATE_DISCONNECTED_SENSE_ID:
583 case DEV_STATE_NOT_OPER:
584 sch = to_subchannel(dev->parent);
585 if (!sch->lpm)
586 return sprintf(buf, "no path\n");
587 else
588 return sprintf(buf, "no device\n");
589 default:
590 /* All other states considered fine. */
591 return sprintf(buf, "good\n");
592 }
593}
594
595static DEVICE_ATTR(chpids, 0444, chpids_show, NULL);
596static DEVICE_ATTR(pimpampom, 0444, pimpampom_show, NULL);
597static DEVICE_ATTR(devtype, 0444, devtype_show, NULL);
598static DEVICE_ATTR(cutype, 0444, cutype_show, NULL);
Bastian Blankf1fc78a2005-10-30 15:00:12 -0800599static DEVICE_ATTR(modalias, 0444, modalias_show, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600static DEVICE_ATTR(online, 0644, online_show, online_store);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601static DEVICE_ATTR(availability, 0444, available_show, NULL);
602
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200603static struct attribute *io_subchannel_attrs[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 &dev_attr_chpids.attr,
605 &dev_attr_pimpampom.attr,
606 NULL,
607};
608
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200609static struct attribute_group io_subchannel_attr_group = {
610 .attrs = io_subchannel_attrs,
Cornelia Huck529192f2006-12-08 15:55:57 +0100611};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
613static struct attribute * ccwdev_attrs[] = {
614 &dev_attr_devtype.attr,
615 &dev_attr_cutype.attr,
Bastian Blankf1fc78a2005-10-30 15:00:12 -0800616 &dev_attr_modalias.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 &dev_attr_online.attr,
618 &dev_attr_cmb_enable.attr,
619 &dev_attr_availability.attr,
620 NULL,
621};
622
623static struct attribute_group ccwdev_attr_group = {
624 .attrs = ccwdev_attrs,
625};
626
David Brownella4dbd672009-06-24 10:06:31 -0700627static const struct attribute_group *ccwdev_attr_groups[] = {
Cornelia Huckef995162007-04-27 16:01:39 +0200628 &ccwdev_attr_group,
629 NULL,
630};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
632/* this is a simple abstraction for device_register that sets the
633 * correct bus type and adds the bus specific files */
Cornelia Huck3c9da7b2006-10-27 12:39:33 +0200634static int ccw_device_register(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635{
636 struct device *dev = &cdev->dev;
637 int ret;
638
639 dev->bus = &ccw_bus_type;
Sebastian Ott3ac276f2009-09-11 10:28:27 +0200640 ret = dev_set_name(&cdev->dev, "0.%x.%04x", cdev->private->dev_id.ssid,
641 cdev->private->dev_id.devno);
642 if (ret)
643 return ret;
644 ret = device_add(dev);
645 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 return ret;
647
648 set_bit(1, &cdev->private->registered);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 return ret;
650}
651
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100652static int match_dev_id(struct device *dev, void *data)
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700653{
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100654 struct ccw_device *cdev = to_ccwdev(dev);
655 struct ccw_dev_id *dev_id = data;
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700656
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100657 return ccw_dev_id_is_equal(&cdev->private->dev_id, dev_id);
658}
659
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100660static struct ccw_device *get_ccwdev_by_dev_id(struct ccw_dev_id *dev_id)
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100661{
662 struct device *dev;
663
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100664 dev = bus_find_device(&ccw_bus_type, NULL, dev_id, match_dev_id);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100665
666 return dev ? to_ccwdev(dev) : NULL;
667}
668
Peter Oberparleiter37de53b2009-12-07 12:51:19 +0100669static void ccw_device_do_unbind_bind(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670{
Cornelia Huckeb32ae82009-03-26 15:24:05 +0100671 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
Cornelia Huckeb32ae82009-03-26 15:24:05 +0100673 if (test_bit(1, &cdev->private->registered)) {
674 device_release_driver(&cdev->dev);
675 ret = device_attach(&cdev->dev);
676 WARN_ON(ret == -ENODEV);
677 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678}
679
680static void
681ccw_device_release(struct device *dev)
682{
683 struct ccw_device *cdev;
684
685 cdev = to_ccwdev(dev);
Cornelia Huck6eff2082008-12-25 13:39:07 +0100686 /* Release reference of parent subchannel. */
687 put_device(cdev->dev.parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 kfree(cdev->private);
689 kfree(cdev);
690}
691
Cornelia Huck7674da72006-12-08 15:54:21 +0100692static struct ccw_device * io_subchannel_allocate_dev(struct subchannel *sch)
693{
694 struct ccw_device *cdev;
695
696 cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
697 if (cdev) {
698 cdev->private = kzalloc(sizeof(struct ccw_device_private),
699 GFP_KERNEL | GFP_DMA);
700 if (cdev->private)
701 return cdev;
702 }
703 kfree(cdev);
704 return ERR_PTR(-ENOMEM);
705}
706
Peter Oberparleiter37de53b2009-12-07 12:51:19 +0100707static void ccw_device_todo(struct work_struct *work);
708
Cornelia Huck7674da72006-12-08 15:54:21 +0100709static int io_subchannel_initialize_dev(struct subchannel *sch,
710 struct ccw_device *cdev)
711{
712 cdev->private->cdev = cdev;
713 atomic_set(&cdev->private->onoff, 0);
714 cdev->dev.parent = &sch->dev;
715 cdev->dev.release = ccw_device_release;
Peter Oberparleiter37de53b2009-12-07 12:51:19 +0100716 INIT_WORK(&cdev->private->todo_work, ccw_device_todo);
Cornelia Huckef995162007-04-27 16:01:39 +0200717 cdev->dev.groups = ccwdev_attr_groups;
Cornelia Huck7674da72006-12-08 15:54:21 +0100718 /* Do first half of device_register. */
719 device_initialize(&cdev->dev);
720 if (!get_device(&sch->dev)) {
Cornelia Huck283fdd02008-12-25 13:39:10 +0100721 /* Release reference from device_initialize(). */
722 put_device(&cdev->dev);
Cornelia Huck7674da72006-12-08 15:54:21 +0100723 return -ENODEV;
724 }
725 return 0;
726}
727
728static struct ccw_device * io_subchannel_create_ccwdev(struct subchannel *sch)
729{
730 struct ccw_device *cdev;
731 int ret;
732
733 cdev = io_subchannel_allocate_dev(sch);
734 if (!IS_ERR(cdev)) {
735 ret = io_subchannel_initialize_dev(sch, cdev);
Sebastian Ott06739a82009-08-23 18:09:04 +0200736 if (ret)
Cornelia Huck7674da72006-12-08 15:54:21 +0100737 cdev = ERR_PTR(ret);
Cornelia Huck7674da72006-12-08 15:54:21 +0100738 }
739 return cdev;
740}
741
Peter Oberparleiter736b5db2009-12-07 12:51:21 +0100742static void io_subchannel_recog(struct ccw_device *, struct subchannel *);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100743
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100744static void sch_create_and_recog_new_device(struct subchannel *sch)
745{
746 struct ccw_device *cdev;
747
748 /* Need to allocate a new ccw device. */
749 cdev = io_subchannel_create_ccwdev(sch);
750 if (IS_ERR(cdev)) {
751 /* OK, we did everything we could... */
752 css_sch_device_unregister(sch);
753 return;
754 }
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100755 /* Start recognition for the new ccw device. */
Peter Oberparleiter736b5db2009-12-07 12:51:21 +0100756 io_subchannel_recog(cdev, sch);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100757}
758
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759/*
760 * Register recognized device.
761 */
Peter Oberparleiter37de53b2009-12-07 12:51:19 +0100762static void io_subchannel_register(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 struct subchannel *sch;
765 int ret;
766 unsigned long flags;
767
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 sch = to_subchannel(cdev->dev.parent);
Cornelia Huck5fb6b852008-12-25 13:39:08 +0100769 /*
770 * Check if subchannel is still registered. It may have become
771 * unregistered if a machine check hit us after finishing
772 * device recognition but before the register work could be
773 * queued.
774 */
775 if (!device_is_registered(&sch->dev))
776 goto out_err;
Cornelia Huck82b7ac02007-04-27 16:01:36 +0200777 css_update_ssd_info(sch);
Cornelia Huck47af5512006-12-04 15:41:07 +0100778 /*
779 * io_subchannel_register() will also be called after device
780 * recognition has been done for a boxed device (which will already
781 * be registered). We need to reprobe since we may now have sense id
782 * information.
783 */
Cornelia Huckd6a30762008-12-25 13:39:11 +0100784 if (device_is_registered(&cdev->dev)) {
Cornelia Huck47af5512006-12-04 15:41:07 +0100785 if (!cdev->drv) {
786 ret = device_reprobe(&cdev->dev);
787 if (ret)
788 /* We can't do much here. */
Michael Ernst139b83d2008-05-07 09:22:54 +0200789 CIO_MSG_EVENT(0, "device_reprobe() returned"
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200790 " %d for 0.%x.%04x\n", ret,
791 cdev->private->dev_id.ssid,
792 cdev->private->dev_id.devno);
Cornelia Huck47af5512006-12-04 15:41:07 +0100793 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 goto out;
795 }
Cornelia Huckfa1a8c22007-04-26 00:12:03 -0700796 /*
797 * Now we know this subchannel will stay, we can throw
798 * our delayed uevent.
799 */
Ming Leif67f1292009-03-01 21:10:49 +0800800 dev_set_uevent_suppress(&sch->dev, 0);
Cornelia Huckfa1a8c22007-04-26 00:12:03 -0700801 kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 /* make it known to the system */
803 ret = ccw_device_register(cdev);
804 if (ret) {
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200805 CIO_MSG_EVENT(0, "Could not register ccw dev 0.%x.%04x: %d\n",
806 cdev->private->dev_id.ssid,
807 cdev->private->dev_id.devno, ret);
Cornelia Huck2ec22982006-12-08 15:54:26 +0100808 spin_lock_irqsave(sch->lock, flags);
Cornelia Huckdb6a6422008-01-26 14:10:46 +0100809 sch_set_cdev(sch, NULL);
Cornelia Huck2ec22982006-12-08 15:54:26 +0100810 spin_unlock_irqrestore(sch->lock, flags);
Cornelia Huck6eff2082008-12-25 13:39:07 +0100811 /* Release initial device reference. */
812 put_device(&cdev->dev);
Cornelia Huck5fb6b852008-12-25 13:39:08 +0100813 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815out:
816 cdev->private->flags.recog_done = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 wake_up(&cdev->private->wait_q);
Cornelia Huck5fb6b852008-12-25 13:39:08 +0100818out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 if (atomic_dec_and_test(&ccw_device_init_count))
820 wake_up(&ccw_device_init_wq);
821}
822
Peter Oberparleiter37de53b2009-12-07 12:51:19 +0100823static void ccw_device_call_sch_unregister(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 struct subchannel *sch;
826
Peter Oberparleiter46fbe4e2008-10-10 21:33:05 +0200827 /* Get subchannel reference for local processing. */
828 if (!get_device(cdev->dev.parent))
829 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 sch = to_subchannel(cdev->dev.parent);
Cornelia Huck6ab48792006-07-12 16:39:50 +0200831 css_sch_device_unregister(sch);
Peter Oberparleiter46fbe4e2008-10-10 21:33:05 +0200832 /* Release subchannel reference for local processing. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 put_device(&sch->dev);
834}
835
836/*
837 * subchannel recognition done. Called from the state machine.
838 */
839void
840io_subchannel_recog_done(struct ccw_device *cdev)
841{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 if (css_init_done == 0) {
843 cdev->private->flags.recog_done = 1;
844 return;
845 }
846 switch (cdev->private->state) {
Sebastian Ott47593bf2009-03-31 19:16:05 +0200847 case DEV_STATE_BOXED:
848 /* Device did not respond in time. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 case DEV_STATE_NOT_OPER:
850 cdev->private->flags.recog_done = 1;
Peter Oberparleiter37de53b2009-12-07 12:51:19 +0100851 /* Remove device found not operational. */
852 ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 if (atomic_dec_and_test(&ccw_device_init_count))
854 wake_up(&ccw_device_init_wq);
855 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 case DEV_STATE_OFFLINE:
857 /*
858 * We can't register the device in interrupt context so
859 * we schedule a work item.
860 */
Peter Oberparleiter37de53b2009-12-07 12:51:19 +0100861 ccw_device_sched_todo(cdev, CDEV_TODO_REGISTER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 break;
863 }
864}
865
Peter Oberparleiter736b5db2009-12-07 12:51:21 +0100866static void io_subchannel_recog(struct ccw_device *cdev, struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 struct ccw_device_private *priv;
869
Cornelia Huck2ec22982006-12-08 15:54:26 +0100870 cdev->ccwlock = sch->lock;
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800871
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 /* Init private data. */
873 priv = cdev->private;
Cornelia Huck78964262006-10-11 15:31:38 +0200874 priv->dev_id.devno = sch->schib.pmcw.dev;
875 priv->dev_id.ssid = sch->schid.ssid;
876 priv->schid = sch->schid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 priv->state = DEV_STATE_NOT_OPER;
878 INIT_LIST_HEAD(&priv->cmb_list);
879 init_waitqueue_head(&priv->wait_q);
880 init_timer(&priv->timer);
881
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 /* Increase counter of devices currently in recognition. */
883 atomic_inc(&ccw_device_init_count);
884
885 /* Start async. device sensing. */
Cornelia Huck2ec22982006-12-08 15:54:26 +0100886 spin_lock_irq(sch->lock);
Peter Oberparleiter60e4dac2009-12-07 12:51:16 +0100887 sch_set_cdev(sch, cdev);
Peter Oberparleiter736b5db2009-12-07 12:51:21 +0100888 ccw_device_recognition(cdev);
Cornelia Huck2ec22982006-12-08 15:54:26 +0100889 spin_unlock_irq(sch->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890}
891
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100892static int ccw_device_move_to_sch(struct ccw_device *cdev,
893 struct subchannel *sch)
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100894{
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100895 struct subchannel *old_sch;
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100896 int rc;
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100897
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100898 old_sch = to_subchannel(cdev->dev.parent);
899 /* Obtain child reference for new parent. */
Cornelia Huck6eff2082008-12-25 13:39:07 +0100900 if (!get_device(&sch->dev))
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100901 return -ENODEV;
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100902 mutex_lock(&sch->reg_mutex);
Cornelia Huckffa6a702009-03-04 12:44:00 +0100903 rc = device_move(&cdev->dev, &sch->dev, DPM_ORDER_PARENT_BEFORE_DEV);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100904 mutex_unlock(&sch->reg_mutex);
905 if (rc) {
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100906 CIO_MSG_EVENT(0, "device_move(0.%x.%04x,0.%x.%04x)=%d\n",
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100907 cdev->private->dev_id.ssid,
908 cdev->private->dev_id.devno, sch->schid.ssid,
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100909 sch->schib.pmcw.dev, rc);
910 /* Release child reference for new parent. */
Cornelia Huck6eff2082008-12-25 13:39:07 +0100911 put_device(&sch->dev);
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100912 return rc;
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100913 }
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100914 /* Clean up old subchannel. */
915 if (!sch_is_pseudo_sch(old_sch)) {
916 spin_lock_irq(old_sch->lock);
917 sch_set_cdev(old_sch, NULL);
918 cio_disable_subchannel(old_sch);
919 spin_unlock_irq(old_sch->lock);
920 css_schedule_eval(old_sch->schid);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100921 }
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100922 /* Release child reference for old parent. */
923 put_device(&old_sch->dev);
924 /* Initialize new subchannel. */
925 spin_lock_irq(sch->lock);
926 cdev->private->schid = sch->schid;
927 cdev->ccwlock = sch->lock;
928 if (!sch_is_pseudo_sch(sch))
929 sch_set_cdev(sch, cdev);
930 spin_unlock_irq(sch->lock);
931 if (!sch_is_pseudo_sch(sch))
932 css_update_ssd_info(sch);
933 return 0;
934}
935
936static int ccw_device_move_to_orph(struct ccw_device *cdev)
937{
938 struct subchannel *sch = to_subchannel(cdev->dev.parent);
939 struct channel_subsystem *css = to_css(sch->dev.parent);
940
941 return ccw_device_move_to_sch(cdev, css->pseudo_subchannel);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100942}
943
Cornelia Huck602b20f2008-01-26 14:10:39 +0100944static void io_subchannel_irq(struct subchannel *sch)
945{
946 struct ccw_device *cdev;
947
Cornelia Huckdb6a6422008-01-26 14:10:46 +0100948 cdev = sch_get_cdev(sch);
Cornelia Huck602b20f2008-01-26 14:10:39 +0100949
Sebastian Ottefd986d2009-09-11 10:28:18 +0200950 CIO_TRACE_EVENT(6, "IRQ");
951 CIO_TRACE_EVENT(6, dev_name(&sch->dev));
Cornelia Huck602b20f2008-01-26 14:10:39 +0100952 if (cdev)
953 dev_fsm_event(cdev, DEV_EVENT_INTERRUPT);
954}
955
Sebastian Ott13952ec2008-12-25 13:39:13 +0100956void io_subchannel_init_config(struct subchannel *sch)
957{
958 memset(&sch->config, 0, sizeof(sch->config));
959 sch->config.csense = 1;
Sebastian Ott13952ec2008-12-25 13:39:13 +0100960}
961
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +0200962static void io_subchannel_init_fields(struct subchannel *sch)
963{
964 if (cio_is_console(sch->schid))
965 sch->opm = 0xff;
966 else
967 sch->opm = chp_get_sch_opm(sch);
968 sch->lpm = sch->schib.pmcw.pam & sch->opm;
Cornelia Huck3a3fc292008-07-14 09:58:58 +0200969 sch->isc = cio_is_console(sch->schid) ? CONSOLE_ISC : IO_SCH_ISC;
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +0200970
971 CIO_MSG_EVENT(6, "Detected device %04x on subchannel 0.%x.%04X"
972 " - PIM = %02X, PAM = %02X, POM = %02X\n",
973 sch->schib.pmcw.dev, sch->schid.ssid,
974 sch->schid.sch_no, sch->schib.pmcw.pim,
975 sch->schib.pmcw.pam, sch->schib.pmcw.pom);
Sebastian Ott13952ec2008-12-25 13:39:13 +0100976
977 io_subchannel_init_config(sch);
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +0200978}
979
Cornelia Huck90ed2b62008-12-25 13:39:09 +0100980/*
981 * Note: We always return 0 so that we bind to the device even on error.
982 * This is needed so that our remove function is called on unregister.
983 */
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +0200984static int io_subchannel_probe(struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 struct ccw_device *cdev;
987 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988
Peter Oberparleiter6d7c5af2009-10-14 12:43:50 +0200989 if (cio_is_console(sch->schid)) {
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200990 rc = sysfs_create_group(&sch->dev.kobj,
991 &io_subchannel_attr_group);
992 if (rc)
993 CIO_MSG_EVENT(0, "Failed to create io subchannel "
994 "attributes for subchannel "
995 "0.%x.%04x (rc=%d)\n",
996 sch->schid.ssid, sch->schid.sch_no, rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 /*
Peter Oberparleiter6d7c5af2009-10-14 12:43:50 +0200998 * The console subchannel already has an associated ccw_device.
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200999 * Throw the delayed uevent for the subchannel, register
Peter Oberparleiter6d7c5af2009-10-14 12:43:50 +02001000 * the ccw_device and exit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 */
Ming Leif67f1292009-03-01 21:10:49 +08001002 dev_set_uevent_suppress(&sch->dev, 0);
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001003 kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
Peter Oberparleiter6d7c5af2009-10-14 12:43:50 +02001004 cdev = sch_get_cdev(sch);
Cornelia Hucke1031782007-10-12 16:11:23 +02001005 cdev->dev.groups = ccwdev_attr_groups;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 device_initialize(&cdev->dev);
1007 ccw_device_register(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 /*
1009 * Check if the device is already online. If it is
Cornelia Huck9cd67422008-12-25 13:39:06 +01001010 * the reference count needs to be corrected since we
1011 * didn't obtain a reference in ccw_device_set_online.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 */
1013 if (cdev->private->state != DEV_STATE_NOT_OPER &&
1014 cdev->private->state != DEV_STATE_OFFLINE &&
1015 cdev->private->state != DEV_STATE_BOXED)
1016 get_device(&cdev->dev);
1017 return 0;
1018 }
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001019 io_subchannel_init_fields(sch);
Sebastian Ottf444cc02008-12-25 13:39:14 +01001020 rc = cio_commit_config(sch);
1021 if (rc)
1022 goto out_schedule;
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001023 rc = sysfs_create_group(&sch->dev.kobj,
1024 &io_subchannel_attr_group);
1025 if (rc)
Cornelia Huck90ed2b62008-12-25 13:39:09 +01001026 goto out_schedule;
Cornelia Huckcd6b4f22008-01-26 14:10:43 +01001027 /* Allocate I/O subchannel private data. */
1028 sch->private = kzalloc(sizeof(struct io_subchannel_private),
1029 GFP_KERNEL | GFP_DMA);
Cornelia Huck90ed2b62008-12-25 13:39:09 +01001030 if (!sch->private)
Peter Oberparleiter48e4c382009-12-07 12:51:15 +01001031 goto out_schedule;
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001032 css_schedule_eval(sch->schid);
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001033 return 0;
Peter Oberparleiter48e4c382009-12-07 12:51:15 +01001034
Cornelia Huck90ed2b62008-12-25 13:39:09 +01001035out_schedule:
Peter Oberparleiter390935a2009-12-07 12:51:18 +01001036 spin_lock_irq(sch->lock);
1037 css_sched_sch_todo(sch, SCH_TODO_UNREG);
1038 spin_unlock_irq(sch->lock);
Cornelia Huck90ed2b62008-12-25 13:39:09 +01001039 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040}
1041
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042static int
Cornelia Huck8bbace72006-01-11 10:56:22 +01001043io_subchannel_remove (struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044{
1045 struct ccw_device *cdev;
1046 unsigned long flags;
1047
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001048 cdev = sch_get_cdev(sch);
1049 if (!cdev)
Peter Oberparleiter48e4c382009-12-07 12:51:15 +01001050 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 /* Set ccw device to not operational and drop reference. */
1052 spin_lock_irqsave(cdev->ccwlock, flags);
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001053 sch_set_cdev(sch, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 cdev->private->state = DEV_STATE_NOT_OPER;
1055 spin_unlock_irqrestore(cdev->ccwlock, flags);
Cornelia Huckef995162007-04-27 16:01:39 +02001056 ccw_device_unregister(cdev);
Peter Oberparleiter48e4c382009-12-07 12:51:15 +01001057out_free:
Cornelia Huckcd6b4f22008-01-26 14:10:43 +01001058 kfree(sch->private);
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001059 sysfs_remove_group(&sch->dev.kobj, &io_subchannel_attr_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 return 0;
1061}
1062
Cornelia Huck602b20f2008-01-26 14:10:39 +01001063static void io_subchannel_verify(struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064{
1065 struct ccw_device *cdev;
1066
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001067 cdev = sch_get_cdev(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 if (cdev)
1069 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1070}
1071
Cornelia Huckc820de32008-07-14 09:58:45 +02001072static int check_for_io_on_path(struct subchannel *sch, int mask)
1073{
Sebastian Ottcdb912a2008-12-25 13:39:12 +01001074 if (cio_update_schib(sch))
Cornelia Huckc820de32008-07-14 09:58:45 +02001075 return 0;
Peter Oberparleiter23d805b2008-07-14 09:58:50 +02001076 if (scsw_actl(&sch->schib.scsw) && sch->schib.pmcw.lpum == mask)
Cornelia Huckc820de32008-07-14 09:58:45 +02001077 return 1;
1078 return 0;
1079}
1080
1081static void terminate_internal_io(struct subchannel *sch,
1082 struct ccw_device *cdev)
1083{
1084 if (cio_clear(sch)) {
1085 /* Recheck device in case clear failed. */
1086 sch->lpm = 0;
1087 if (cdev->online)
1088 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1089 else
1090 css_schedule_eval(sch->schid);
1091 return;
1092 }
1093 cdev->private->state = DEV_STATE_CLEAR_VERIFY;
1094 /* Request retry of internal operation. */
1095 cdev->private->flags.intretry = 1;
1096 /* Call handler. */
1097 if (cdev->handler)
1098 cdev->handler(cdev, cdev->private->intparm,
1099 ERR_PTR(-EIO));
1100}
1101
1102static void io_subchannel_terminate_path(struct subchannel *sch, u8 mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103{
1104 struct ccw_device *cdev;
1105
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001106 cdev = sch_get_cdev(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 if (!cdev)
1108 return;
Cornelia Huckc820de32008-07-14 09:58:45 +02001109 if (check_for_io_on_path(sch, mask)) {
1110 if (cdev->private->state == DEV_STATE_ONLINE)
1111 ccw_device_kill_io(cdev);
1112 else {
1113 terminate_internal_io(sch, cdev);
1114 /* Re-start path verification. */
1115 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1116 }
1117 } else
1118 /* trigger path verification. */
1119 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1120
1121}
1122
Cornelia Huck99611f82008-07-14 09:59:02 +02001123static int io_subchannel_chp_event(struct subchannel *sch,
1124 struct chp_link *link, int event)
Cornelia Huckc820de32008-07-14 09:58:45 +02001125{
1126 int mask;
Cornelia Huckc820de32008-07-14 09:58:45 +02001127
Cornelia Huck99611f82008-07-14 09:59:02 +02001128 mask = chp_ssd_get_mask(&sch->ssd_info, link);
Cornelia Huckc820de32008-07-14 09:58:45 +02001129 if (!mask)
1130 return 0;
1131 switch (event) {
1132 case CHP_VARY_OFF:
1133 sch->opm &= ~mask;
1134 sch->lpm &= ~mask;
1135 io_subchannel_terminate_path(sch, mask);
1136 break;
1137 case CHP_VARY_ON:
1138 sch->opm |= mask;
1139 sch->lpm |= mask;
1140 io_subchannel_verify(sch);
1141 break;
1142 case CHP_OFFLINE:
Sebastian Ottcdb912a2008-12-25 13:39:12 +01001143 if (cio_update_schib(sch))
Cornelia Huckc820de32008-07-14 09:58:45 +02001144 return -ENODEV;
1145 io_subchannel_terminate_path(sch, mask);
1146 break;
1147 case CHP_ONLINE:
Sebastian Ottcdb912a2008-12-25 13:39:12 +01001148 if (cio_update_schib(sch))
1149 return -ENODEV;
Cornelia Huckc820de32008-07-14 09:58:45 +02001150 sch->lpm |= mask & sch->opm;
1151 io_subchannel_verify(sch);
1152 break;
1153 }
1154 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155}
1156
1157static void
Cornelia Huck8bbace72006-01-11 10:56:22 +01001158io_subchannel_shutdown(struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 struct ccw_device *cdev;
1161 int ret;
1162
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001163 cdev = sch_get_cdev(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164
Cornelia Hucka8237fc2006-01-06 00:19:21 -08001165 if (cio_is_console(sch->schid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 return;
1167 if (!sch->schib.pmcw.ena)
1168 /* Nothing to do. */
1169 return;
1170 ret = cio_disable_subchannel(sch);
1171 if (ret != -EBUSY)
1172 /* Subchannel is disabled, we're done. */
1173 return;
1174 cdev->private->state = DEV_STATE_QUIESCE;
1175 if (cdev->handler)
1176 cdev->handler(cdev, cdev->private->intparm,
1177 ERR_PTR(-EIO));
1178 ret = ccw_device_cancel_halt_clear(cdev);
1179 if (ret == -EBUSY) {
1180 ccw_device_set_timeout(cdev, HZ/10);
1181 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
1182 }
1183 cio_disable_subchannel(sch);
1184}
1185
Cornelia Huckc820de32008-07-14 09:58:45 +02001186static int device_is_disconnected(struct ccw_device *cdev)
1187{
1188 if (!cdev)
1189 return 0;
1190 return (cdev->private->state == DEV_STATE_DISCONNECTED ||
1191 cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID);
1192}
1193
1194static int recovery_check(struct device *dev, void *data)
1195{
1196 struct ccw_device *cdev = to_ccwdev(dev);
1197 int *redo = data;
1198
1199 spin_lock_irq(cdev->ccwlock);
1200 switch (cdev->private->state) {
1201 case DEV_STATE_DISCONNECTED:
1202 CIO_MSG_EVENT(3, "recovery: trigger 0.%x.%04x\n",
1203 cdev->private->dev_id.ssid,
1204 cdev->private->dev_id.devno);
1205 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1206 *redo = 1;
1207 break;
1208 case DEV_STATE_DISCONNECTED_SENSE_ID:
1209 *redo = 1;
1210 break;
1211 }
1212 spin_unlock_irq(cdev->ccwlock);
1213
1214 return 0;
1215}
1216
1217static void recovery_work_func(struct work_struct *unused)
1218{
1219 int redo = 0;
1220
1221 bus_for_each_dev(&ccw_bus_type, NULL, &redo, recovery_check);
1222 if (redo) {
1223 spin_lock_irq(&recovery_lock);
1224 if (!timer_pending(&recovery_timer)) {
1225 if (recovery_phase < ARRAY_SIZE(recovery_delay) - 1)
1226 recovery_phase++;
1227 mod_timer(&recovery_timer, jiffies +
1228 recovery_delay[recovery_phase] * HZ);
1229 }
1230 spin_unlock_irq(&recovery_lock);
1231 } else
1232 CIO_MSG_EVENT(4, "recovery: end\n");
1233}
1234
1235static DECLARE_WORK(recovery_work, recovery_work_func);
1236
1237static void recovery_func(unsigned long data)
1238{
1239 /*
1240 * We can't do our recovery in softirq context and it's not
1241 * performance critical, so we schedule it.
1242 */
1243 schedule_work(&recovery_work);
1244}
1245
1246static void ccw_device_schedule_recovery(void)
1247{
1248 unsigned long flags;
1249
1250 CIO_MSG_EVENT(4, "recovery: schedule\n");
1251 spin_lock_irqsave(&recovery_lock, flags);
1252 if (!timer_pending(&recovery_timer) || (recovery_phase != 0)) {
1253 recovery_phase = 0;
1254 mod_timer(&recovery_timer, jiffies + recovery_delay[0] * HZ);
1255 }
1256 spin_unlock_irqrestore(&recovery_lock, flags);
1257}
1258
Peter Oberparleiterecf5d9e2008-10-10 21:33:06 +02001259static int purge_fn(struct device *dev, void *data)
1260{
1261 struct ccw_device *cdev = to_ccwdev(dev);
Peter Oberparleiter37de53b2009-12-07 12:51:19 +01001262 struct ccw_dev_id *id = &cdev->private->dev_id;
Peter Oberparleiterecf5d9e2008-10-10 21:33:06 +02001263
1264 spin_lock_irq(cdev->ccwlock);
Peter Oberparleiter37de53b2009-12-07 12:51:19 +01001265 if (is_blacklisted(id->ssid, id->devno) &&
1266 (cdev->private->state == DEV_STATE_OFFLINE)) {
1267 CIO_MSG_EVENT(3, "ccw: purging 0.%x.%04x\n", id->ssid,
1268 id->devno);
1269 ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
1270 }
Peter Oberparleiterecf5d9e2008-10-10 21:33:06 +02001271 spin_unlock_irq(cdev->ccwlock);
Peter Oberparleiterecf5d9e2008-10-10 21:33:06 +02001272 /* Abort loop in case of pending signal. */
1273 if (signal_pending(current))
1274 return -EINTR;
1275
1276 return 0;
1277}
1278
1279/**
1280 * ccw_purge_blacklisted - purge unused, blacklisted devices
1281 *
1282 * Unregister all ccw devices that are offline and on the blacklist.
1283 */
1284int ccw_purge_blacklisted(void)
1285{
1286 CIO_MSG_EVENT(2, "ccw: purging blacklisted devices\n");
1287 bus_for_each_dev(&ccw_bus_type, NULL, NULL, purge_fn);
1288 return 0;
1289}
1290
Peter Oberparleiter6afcc772009-10-06 10:34:02 +02001291void ccw_device_set_disconnected(struct ccw_device *cdev)
Cornelia Huckc820de32008-07-14 09:58:45 +02001292{
1293 if (!cdev)
1294 return;
1295 ccw_device_set_timeout(cdev, 0);
1296 cdev->private->flags.fake_irb = 0;
1297 cdev->private->state = DEV_STATE_DISCONNECTED;
1298 if (cdev->online)
1299 ccw_device_schedule_recovery();
1300}
1301
Peter Oberparleiter91c36912008-08-21 19:46:39 +02001302void ccw_device_set_notoper(struct ccw_device *cdev)
1303{
1304 struct subchannel *sch = to_subchannel(cdev->dev.parent);
1305
1306 CIO_TRACE_EVENT(2, "notoper");
Cornelia Huckb9d3aed2008-10-10 21:33:11 +02001307 CIO_TRACE_EVENT(2, dev_name(&sch->dev));
Peter Oberparleiter91c36912008-08-21 19:46:39 +02001308 ccw_device_set_timeout(cdev, 0);
1309 cio_disable_subchannel(sch);
1310 cdev->private->state = DEV_STATE_NOT_OPER;
1311}
1312
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001313enum io_sch_action {
1314 IO_SCH_UNREG,
1315 IO_SCH_ORPH_UNREG,
1316 IO_SCH_ATTACH,
1317 IO_SCH_UNREG_ATTACH,
1318 IO_SCH_ORPH_ATTACH,
1319 IO_SCH_REPROBE,
1320 IO_SCH_VERIFY,
1321 IO_SCH_DISC,
1322 IO_SCH_NOP,
1323};
1324
1325static enum io_sch_action sch_get_action(struct subchannel *sch)
Cornelia Huckc820de32008-07-14 09:58:45 +02001326{
Cornelia Huckc820de32008-07-14 09:58:45 +02001327 struct ccw_device *cdev;
1328
Cornelia Huckc820de32008-07-14 09:58:45 +02001329 cdev = sch_get_cdev(sch);
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001330 if (cio_update_schib(sch)) {
1331 /* Not operational. */
1332 if (!cdev)
1333 return IO_SCH_UNREG;
1334 if (!ccw_device_notify(cdev, CIO_GONE))
1335 return IO_SCH_UNREG;
1336 return IO_SCH_ORPH_UNREG;
Cornelia Huckc820de32008-07-14 09:58:45 +02001337 }
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001338 /* Operational. */
1339 if (!cdev)
1340 return IO_SCH_ATTACH;
1341 if (sch->schib.pmcw.dev != cdev->private->dev_id.devno) {
1342 if (!ccw_device_notify(cdev, CIO_GONE))
1343 return IO_SCH_UNREG_ATTACH;
1344 return IO_SCH_ORPH_ATTACH;
Cornelia Huckc820de32008-07-14 09:58:45 +02001345 }
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001346 if ((sch->schib.pmcw.pam & sch->opm) == 0) {
1347 if (!ccw_device_notify(cdev, CIO_NO_PATH))
1348 return IO_SCH_UNREG;
1349 return IO_SCH_DISC;
Cornelia Huckc820de32008-07-14 09:58:45 +02001350 }
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001351 if (device_is_disconnected(cdev))
1352 return IO_SCH_REPROBE;
1353 if (cdev->online)
1354 return IO_SCH_VERIFY;
1355 return IO_SCH_NOP;
1356}
1357
1358/**
1359 * io_subchannel_sch_event - process subchannel event
1360 * @sch: subchannel
1361 * @process: non-zero if function is called in process context
1362 *
1363 * An unspecified event occurred for this subchannel. Adjust data according
1364 * to the current operational state of the subchannel and device. Return
1365 * zero when the event has been handled sufficiently or -EAGAIN when this
1366 * function should be called again in process context.
1367 */
1368static int io_subchannel_sch_event(struct subchannel *sch, int process)
1369{
1370 unsigned long flags;
1371 struct ccw_device *cdev;
1372 struct ccw_dev_id dev_id;
1373 enum io_sch_action action;
1374 int rc = -EAGAIN;
1375
1376 spin_lock_irqsave(sch->lock, flags);
1377 if (!device_is_registered(&sch->dev))
1378 goto out_unlock;
Peter Oberparleiter390935a2009-12-07 12:51:18 +01001379 if (work_pending(&sch->todo_work))
1380 goto out_unlock;
Peter Oberparleiter37de53b2009-12-07 12:51:19 +01001381 cdev = sch_get_cdev(sch);
1382 if (cdev && work_pending(&cdev->private->todo_work))
1383 goto out_unlock;
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001384 action = sch_get_action(sch);
1385 CIO_MSG_EVENT(2, "event: sch 0.%x.%04x, process=%d, action=%d\n",
1386 sch->schid.ssid, sch->schid.sch_no, process,
1387 action);
1388 /* Perform immediate actions while holding the lock. */
Cornelia Huckc820de32008-07-14 09:58:45 +02001389 switch (action) {
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001390 case IO_SCH_REPROBE:
1391 /* Trigger device recognition. */
Cornelia Huckc820de32008-07-14 09:58:45 +02001392 ccw_device_trigger_reprobe(cdev);
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001393 rc = 0;
1394 goto out_unlock;
1395 case IO_SCH_VERIFY:
1396 /* Trigger path verification. */
1397 io_subchannel_verify(sch);
1398 rc = 0;
1399 goto out_unlock;
1400 case IO_SCH_DISC:
1401 ccw_device_set_disconnected(cdev);
1402 rc = 0;
1403 goto out_unlock;
1404 case IO_SCH_ORPH_UNREG:
1405 case IO_SCH_ORPH_ATTACH:
Peter Oberparleiter6afcc772009-10-06 10:34:02 +02001406 ccw_device_set_disconnected(cdev);
Peter Oberparleiter91c36912008-08-21 19:46:39 +02001407 break;
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001408 case IO_SCH_UNREG_ATTACH:
1409 case IO_SCH_UNREG:
1410 if (cdev)
1411 ccw_device_set_notoper(cdev);
1412 break;
1413 case IO_SCH_NOP:
1414 rc = 0;
1415 goto out_unlock;
Cornelia Huckc820de32008-07-14 09:58:45 +02001416 default:
1417 break;
1418 }
1419 spin_unlock_irqrestore(sch->lock, flags);
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001420 /* All other actions require process context. */
1421 if (!process)
1422 goto out;
1423 /* Handle attached ccw device. */
1424 switch (action) {
1425 case IO_SCH_ORPH_UNREG:
1426 case IO_SCH_ORPH_ATTACH:
1427 /* Move ccw device to orphanage. */
1428 rc = ccw_device_move_to_orph(cdev);
1429 if (rc)
1430 goto out;
1431 break;
1432 case IO_SCH_UNREG_ATTACH:
1433 /* Unregister ccw device. */
1434 ccw_device_unregister(cdev);
1435 break;
1436 default:
1437 break;
1438 }
1439 /* Handle subchannel. */
1440 switch (action) {
1441 case IO_SCH_ORPH_UNREG:
1442 case IO_SCH_UNREG:
1443 css_sch_device_unregister(sch);
1444 break;
1445 case IO_SCH_ORPH_ATTACH:
1446 case IO_SCH_UNREG_ATTACH:
1447 case IO_SCH_ATTACH:
1448 dev_id.ssid = sch->schid.ssid;
1449 dev_id.devno = sch->schib.pmcw.dev;
1450 cdev = get_ccwdev_by_dev_id(&dev_id);
1451 if (!cdev) {
1452 sch_create_and_recog_new_device(sch);
1453 break;
1454 }
1455 rc = ccw_device_move_to_sch(cdev, sch);
1456 if (rc) {
1457 /* Release reference from get_ccwdev_by_dev_id() */
1458 put_device(&cdev->dev);
1459 goto out;
1460 }
1461 spin_lock_irqsave(sch->lock, flags);
1462 ccw_device_trigger_reprobe(cdev);
1463 spin_unlock_irqrestore(sch->lock, flags);
1464 /* Release reference from get_ccwdev_by_dev_id() */
1465 put_device(&cdev->dev);
1466 break;
1467 default:
1468 break;
1469 }
1470 return 0;
Cornelia Huckc820de32008-07-14 09:58:45 +02001471
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001472out_unlock:
1473 spin_unlock_irqrestore(sch->lock, flags);
1474out:
1475 return rc;
Cornelia Huckc820de32008-07-14 09:58:45 +02001476}
1477
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478#ifdef CONFIG_CCW_CONSOLE
1479static struct ccw_device console_cdev;
1480static struct ccw_device_private console_private;
1481static int console_cdev_in_use;
1482
Cornelia Huck2ec22982006-12-08 15:54:26 +01001483static DEFINE_SPINLOCK(ccw_console_lock);
1484
1485spinlock_t * cio_get_console_lock(void)
1486{
1487 return &ccw_console_lock;
1488}
1489
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001490static int ccw_device_console_enable(struct ccw_device *cdev,
1491 struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492{
1493 int rc;
1494
Cornelia Huckcd6b4f22008-01-26 14:10:43 +01001495 /* Attach subchannel private data. */
1496 sch->private = cio_get_console_priv();
1497 memset(sch->private, 0, sizeof(struct io_subchannel_private));
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001498 io_subchannel_init_fields(sch);
Sebastian Ottf444cc02008-12-25 13:39:14 +01001499 rc = cio_commit_config(sch);
1500 if (rc)
1501 return rc;
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001502 sch->driver = &io_subchannel_driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 /* Initialize the ccw_device structure. */
Heiko Carstens292888c2006-08-30 14:33:35 +02001504 cdev->dev.parent= &sch->dev;
Peter Oberparleiter736b5db2009-12-07 12:51:21 +01001505 io_subchannel_recog(cdev, sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 /* Now wait for the async. recognition to come to an end. */
1507 spin_lock_irq(cdev->ccwlock);
1508 while (!dev_fsm_final_state(cdev))
1509 wait_cons_dev();
1510 rc = -EIO;
1511 if (cdev->private->state != DEV_STATE_OFFLINE)
1512 goto out_unlock;
1513 ccw_device_online(cdev);
1514 while (!dev_fsm_final_state(cdev))
1515 wait_cons_dev();
1516 if (cdev->private->state != DEV_STATE_ONLINE)
1517 goto out_unlock;
1518 rc = 0;
1519out_unlock:
1520 spin_unlock_irq(cdev->ccwlock);
Peter Oberparleiter736b5db2009-12-07 12:51:21 +01001521 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522}
1523
1524struct ccw_device *
1525ccw_device_probe_console(void)
1526{
1527 struct subchannel *sch;
1528 int ret;
1529
1530 if (xchg(&console_cdev_in_use, 1) != 0)
Peter Oberparleiter600b5d12006-02-01 03:06:35 -08001531 return ERR_PTR(-EBUSY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 sch = cio_probe_console();
1533 if (IS_ERR(sch)) {
1534 console_cdev_in_use = 0;
1535 return (void *) sch;
1536 }
1537 memset(&console_cdev, 0, sizeof(struct ccw_device));
1538 memset(&console_private, 0, sizeof(struct ccw_device_private));
1539 console_cdev.private = &console_private;
Martin Schwidefskyc1637532006-12-08 15:53:57 +01001540 console_private.cdev = &console_cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 ret = ccw_device_console_enable(&console_cdev, sch);
1542 if (ret) {
1543 cio_release_console();
1544 console_cdev_in_use = 0;
1545 return ERR_PTR(ret);
1546 }
1547 console_cdev.online = 1;
1548 return &console_cdev;
1549}
Cornelia Huck1f4e7ed2008-10-10 21:33:14 +02001550
Martin Schwidefsky66648452009-06-16 10:30:28 +02001551static int ccw_device_pm_restore(struct device *dev);
1552
1553int ccw_device_force_console(void)
1554{
1555 if (!console_cdev_in_use)
1556 return -ENODEV;
1557 return ccw_device_pm_restore(&console_cdev.dev);
1558}
1559EXPORT_SYMBOL_GPL(ccw_device_force_console);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560#endif
1561
1562/*
1563 * get ccw_device matching the busid, but only if owned by cdrv
1564 */
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001565static int
1566__ccwdev_check_busid(struct device *dev, void *id)
1567{
1568 char *bus_id;
1569
Cornelia Huck12975ae2006-10-11 15:31:47 +02001570 bus_id = id;
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001571
Kay Sievers98df67b2008-12-25 13:38:55 +01001572 return (strcmp(bus_id, dev_name(dev)) == 0);
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001573}
1574
1575
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +02001576/**
1577 * get_ccwdev_by_busid() - obtain device from a bus id
1578 * @cdrv: driver the device is owned by
1579 * @bus_id: bus id of the device to be searched
1580 *
1581 * This function searches all devices owned by @cdrv for a device with a bus
1582 * id matching @bus_id.
1583 * Returns:
1584 * If a match is found, its reference count of the found device is increased
1585 * and it is returned; else %NULL is returned.
1586 */
1587struct ccw_device *get_ccwdev_by_busid(struct ccw_driver *cdrv,
1588 const char *bus_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589{
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001590 struct device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 struct device_driver *drv;
1592
1593 drv = get_driver(&cdrv->driver);
1594 if (!drv)
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001595 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001597 dev = driver_find_device(drv, NULL, (void *)bus_id,
1598 __ccwdev_check_busid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 put_driver(drv);
1600
Heiko Carstensd2c993d2006-07-12 16:41:55 +02001601 return dev ? to_ccwdev(dev) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602}
1603
1604/************************** device driver handling ************************/
1605
1606/* This is the implementation of the ccw_driver class. The probe, remove
1607 * and release methods are initially very similar to the device_driver
1608 * implementations, with the difference that they have ccw_device
1609 * arguments.
1610 *
1611 * A ccw driver also contains the information that is needed for
1612 * device matching.
1613 */
1614static int
1615ccw_device_probe (struct device *dev)
1616{
1617 struct ccw_device *cdev = to_ccwdev(dev);
1618 struct ccw_driver *cdrv = to_ccwdrv(dev->driver);
1619 int ret;
1620
1621 cdev->drv = cdrv; /* to let the driver call _set_online */
1622
1623 ret = cdrv->probe ? cdrv->probe(cdev) : -ENODEV;
1624
1625 if (ret) {
Heiko Carstensd2c993d2006-07-12 16:41:55 +02001626 cdev->drv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 return ret;
1628 }
1629
1630 return 0;
1631}
1632
1633static int
1634ccw_device_remove (struct device *dev)
1635{
1636 struct ccw_device *cdev = to_ccwdev(dev);
1637 struct ccw_driver *cdrv = cdev->drv;
1638 int ret;
1639
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 if (cdrv->remove)
1641 cdrv->remove(cdev);
1642 if (cdev->online) {
1643 cdev->online = 0;
1644 spin_lock_irq(cdev->ccwlock);
1645 ret = ccw_device_offline(cdev);
1646 spin_unlock_irq(cdev->ccwlock);
1647 if (ret == 0)
1648 wait_event(cdev->private->wait_q,
1649 dev_fsm_final_state(cdev));
1650 else
Michael Ernst139b83d2008-05-07 09:22:54 +02001651 CIO_MSG_EVENT(0, "ccw_device_offline returned %d, "
Cornelia Hucke556bbb2007-07-27 12:29:19 +02001652 "device 0.%x.%04x\n",
1653 ret, cdev->private->dev_id.ssid,
1654 cdev->private->dev_id.devno);
Cornelia Huck9cd67422008-12-25 13:39:06 +01001655 /* Give up reference obtained in ccw_device_set_online(). */
1656 put_device(&cdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 }
1658 ccw_device_set_timeout(cdev, 0);
Heiko Carstensd2c993d2006-07-12 16:41:55 +02001659 cdev->drv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 return 0;
1661}
1662
Cornelia Huck958974fb2007-10-12 16:11:21 +02001663static void ccw_device_shutdown(struct device *dev)
1664{
1665 struct ccw_device *cdev;
1666
1667 cdev = to_ccwdev(dev);
1668 if (cdev->drv && cdev->drv->shutdown)
1669 cdev->drv->shutdown(cdev);
Cornelia Huck1842f2b2007-10-12 16:11:22 +02001670 disable_cmf(cdev);
Cornelia Huck958974fb2007-10-12 16:11:21 +02001671}
1672
Sebastian Ott823d4942009-06-16 10:30:20 +02001673static int ccw_device_pm_prepare(struct device *dev)
1674{
1675 struct ccw_device *cdev = to_ccwdev(dev);
1676
Peter Oberparleiter37de53b2009-12-07 12:51:19 +01001677 if (work_pending(&cdev->private->todo_work))
Sebastian Ott823d4942009-06-16 10:30:20 +02001678 return -EAGAIN;
1679 /* Fail while device is being set online/offline. */
1680 if (atomic_read(&cdev->private->onoff))
1681 return -EAGAIN;
1682
1683 if (cdev->online && cdev->drv && cdev->drv->prepare)
1684 return cdev->drv->prepare(cdev);
1685
1686 return 0;
1687}
1688
1689static void ccw_device_pm_complete(struct device *dev)
1690{
1691 struct ccw_device *cdev = to_ccwdev(dev);
1692
1693 if (cdev->online && cdev->drv && cdev->drv->complete)
1694 cdev->drv->complete(cdev);
1695}
1696
1697static int ccw_device_pm_freeze(struct device *dev)
1698{
1699 struct ccw_device *cdev = to_ccwdev(dev);
1700 struct subchannel *sch = to_subchannel(cdev->dev.parent);
1701 int ret, cm_enabled;
1702
1703 /* Fail suspend while device is in transistional state. */
1704 if (!dev_fsm_final_state(cdev))
1705 return -EAGAIN;
1706 if (!cdev->online)
1707 return 0;
1708 if (cdev->drv && cdev->drv->freeze) {
1709 ret = cdev->drv->freeze(cdev);
1710 if (ret)
1711 return ret;
1712 }
1713
1714 spin_lock_irq(sch->lock);
1715 cm_enabled = cdev->private->cmb != NULL;
1716 spin_unlock_irq(sch->lock);
1717 if (cm_enabled) {
1718 /* Don't have the css write on memory. */
1719 ret = ccw_set_cmf(cdev, 0);
1720 if (ret)
1721 return ret;
1722 }
1723 /* From here on, disallow device driver I/O. */
1724 spin_lock_irq(sch->lock);
1725 ret = cio_disable_subchannel(sch);
1726 spin_unlock_irq(sch->lock);
1727
1728 return ret;
1729}
1730
1731static int ccw_device_pm_thaw(struct device *dev)
1732{
1733 struct ccw_device *cdev = to_ccwdev(dev);
1734 struct subchannel *sch = to_subchannel(cdev->dev.parent);
1735 int ret, cm_enabled;
1736
1737 if (!cdev->online)
1738 return 0;
1739
1740 spin_lock_irq(sch->lock);
1741 /* Allow device driver I/O again. */
1742 ret = cio_enable_subchannel(sch, (u32)(addr_t)sch);
1743 cm_enabled = cdev->private->cmb != NULL;
1744 spin_unlock_irq(sch->lock);
1745 if (ret)
1746 return ret;
1747
1748 if (cm_enabled) {
1749 ret = ccw_set_cmf(cdev, 1);
1750 if (ret)
1751 return ret;
1752 }
1753
1754 if (cdev->drv && cdev->drv->thaw)
1755 ret = cdev->drv->thaw(cdev);
1756
1757 return ret;
1758}
1759
1760static void __ccw_device_pm_restore(struct ccw_device *cdev)
1761{
1762 struct subchannel *sch = to_subchannel(cdev->dev.parent);
Sebastian Ott823d4942009-06-16 10:30:20 +02001763
1764 if (cio_is_console(sch->schid))
1765 goto out;
1766 /*
1767 * While we were sleeping, devices may have gone or become
1768 * available again. Kick re-detection.
1769 */
1770 spin_lock_irq(sch->lock);
1771 cdev->private->flags.resuming = 1;
Peter Oberparleiter736b5db2009-12-07 12:51:21 +01001772 ccw_device_recognition(cdev);
Sebastian Ott823d4942009-06-16 10:30:20 +02001773 spin_unlock_irq(sch->lock);
Sebastian Ott823d4942009-06-16 10:30:20 +02001774 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev) ||
1775 cdev->private->state == DEV_STATE_DISCONNECTED);
Sebastian Ott823d4942009-06-16 10:30:20 +02001776out:
1777 cdev->private->flags.resuming = 0;
1778}
1779
1780static int resume_handle_boxed(struct ccw_device *cdev)
1781{
1782 cdev->private->state = DEV_STATE_BOXED;
1783 if (ccw_device_notify(cdev, CIO_BOXED))
1784 return 0;
Peter Oberparleiter37de53b2009-12-07 12:51:19 +01001785 ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
Sebastian Ott823d4942009-06-16 10:30:20 +02001786 return -ENODEV;
1787}
1788
1789static int resume_handle_disc(struct ccw_device *cdev)
1790{
1791 cdev->private->state = DEV_STATE_DISCONNECTED;
1792 if (ccw_device_notify(cdev, CIO_GONE))
1793 return 0;
Peter Oberparleiter37de53b2009-12-07 12:51:19 +01001794 ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
Sebastian Ott823d4942009-06-16 10:30:20 +02001795 return -ENODEV;
1796}
1797
1798static int ccw_device_pm_restore(struct device *dev)
1799{
1800 struct ccw_device *cdev = to_ccwdev(dev);
1801 struct subchannel *sch = to_subchannel(cdev->dev.parent);
1802 int ret = 0, cm_enabled;
1803
1804 __ccw_device_pm_restore(cdev);
1805 spin_lock_irq(sch->lock);
1806 if (cio_is_console(sch->schid)) {
1807 cio_enable_subchannel(sch, (u32)(addr_t)sch);
1808 spin_unlock_irq(sch->lock);
1809 goto out_restore;
1810 }
1811 cdev->private->flags.donotify = 0;
1812 /* check recognition results */
1813 switch (cdev->private->state) {
1814 case DEV_STATE_OFFLINE:
1815 break;
1816 case DEV_STATE_BOXED:
1817 ret = resume_handle_boxed(cdev);
1818 spin_unlock_irq(sch->lock);
1819 if (ret)
1820 goto out;
1821 goto out_restore;
1822 case DEV_STATE_DISCONNECTED:
1823 goto out_disc_unlock;
1824 default:
1825 goto out_unreg_unlock;
1826 }
1827 /* check if the device id has changed */
1828 if (sch->schib.pmcw.dev != cdev->private->dev_id.devno) {
Sebastian Ottf0148242009-09-11 10:28:23 +02001829 CIO_MSG_EVENT(0, "resume: sch 0.%x.%04x: failed (devno "
1830 "changed from %04x to %04x)\n",
1831 sch->schid.ssid, sch->schid.sch_no,
Sebastian Ott823d4942009-06-16 10:30:20 +02001832 cdev->private->dev_id.devno,
1833 sch->schib.pmcw.dev);
1834 goto out_unreg_unlock;
1835 }
1836 /* check if the device type has changed */
1837 if (!ccw_device_test_sense_data(cdev)) {
1838 ccw_device_update_sense_data(cdev);
Peter Oberparleiter37de53b2009-12-07 12:51:19 +01001839 ccw_device_sched_todo(cdev, CDEV_TODO_REBIND);
Sebastian Ott823d4942009-06-16 10:30:20 +02001840 ret = -ENODEV;
1841 goto out_unlock;
1842 }
1843 if (!cdev->online) {
1844 ret = 0;
1845 goto out_unlock;
1846 }
1847 ret = ccw_device_online(cdev);
1848 if (ret)
1849 goto out_disc_unlock;
1850
1851 cm_enabled = cdev->private->cmb != NULL;
1852 spin_unlock_irq(sch->lock);
1853
1854 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
1855 if (cdev->private->state != DEV_STATE_ONLINE) {
1856 spin_lock_irq(sch->lock);
1857 goto out_disc_unlock;
1858 }
1859 if (cm_enabled) {
1860 ret = ccw_set_cmf(cdev, 1);
1861 if (ret) {
Sebastian Ottf0148242009-09-11 10:28:23 +02001862 CIO_MSG_EVENT(2, "resume: cdev 0.%x.%04x: cmf failed "
1863 "(rc=%d)\n", cdev->private->dev_id.ssid,
1864 cdev->private->dev_id.devno, ret);
Sebastian Ott823d4942009-06-16 10:30:20 +02001865 ret = 0;
1866 }
1867 }
1868
1869out_restore:
1870 if (cdev->online && cdev->drv && cdev->drv->restore)
1871 ret = cdev->drv->restore(cdev);
1872out:
1873 return ret;
1874
1875out_disc_unlock:
1876 ret = resume_handle_disc(cdev);
1877 spin_unlock_irq(sch->lock);
1878 if (ret)
1879 return ret;
1880 goto out_restore;
1881
1882out_unreg_unlock:
Peter Oberparleiter37de53b2009-12-07 12:51:19 +01001883 ccw_device_sched_todo(cdev, CDEV_TODO_UNREG_EVAL);
Sebastian Ott823d4942009-06-16 10:30:20 +02001884 ret = -ENODEV;
1885out_unlock:
1886 spin_unlock_irq(sch->lock);
1887 return ret;
1888}
1889
1890static struct dev_pm_ops ccw_pm_ops = {
1891 .prepare = ccw_device_pm_prepare,
1892 .complete = ccw_device_pm_complete,
1893 .freeze = ccw_device_pm_freeze,
1894 .thaw = ccw_device_pm_thaw,
1895 .restore = ccw_device_pm_restore,
1896};
1897
Cornelia Huck8bbace72006-01-11 10:56:22 +01001898struct bus_type ccw_bus_type = {
1899 .name = "ccw",
1900 .match = ccw_bus_match,
1901 .uevent = ccw_uevent,
1902 .probe = ccw_device_probe,
1903 .remove = ccw_device_remove,
Cornelia Huck958974fb2007-10-12 16:11:21 +02001904 .shutdown = ccw_device_shutdown,
Sebastian Ott823d4942009-06-16 10:30:20 +02001905 .pm = &ccw_pm_ops,
Cornelia Huck8bbace72006-01-11 10:56:22 +01001906};
1907
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +02001908/**
1909 * ccw_driver_register() - register a ccw driver
1910 * @cdriver: driver to be registered
1911 *
1912 * This function is mainly a wrapper around driver_register().
1913 * Returns:
1914 * %0 on success and a negative error value on failure.
1915 */
1916int ccw_driver_register(struct ccw_driver *cdriver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917{
1918 struct device_driver *drv = &cdriver->driver;
1919
1920 drv->bus = &ccw_bus_type;
1921 drv->name = cdriver->name;
Cornelia Huck4beee642008-01-26 14:10:47 +01001922 drv->owner = cdriver->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923
1924 return driver_register(drv);
1925}
1926
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +02001927/**
1928 * ccw_driver_unregister() - deregister a ccw driver
1929 * @cdriver: driver to be deregistered
1930 *
1931 * This function is mainly a wrapper around driver_unregister().
1932 */
1933void ccw_driver_unregister(struct ccw_driver *cdriver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934{
1935 driver_unregister(&cdriver->driver);
1936}
1937
Cornelia Hucka8237fc2006-01-06 00:19:21 -08001938/* Helper func for qdio. */
1939struct subchannel_id
1940ccw_device_get_subchannel_id(struct ccw_device *cdev)
1941{
1942 struct subchannel *sch;
1943
1944 sch = to_subchannel(cdev->dev.parent);
1945 return sch->schid;
1946}
1947
Peter Oberparleiter37de53b2009-12-07 12:51:19 +01001948static void ccw_device_todo(struct work_struct *work)
1949{
1950 struct ccw_device_private *priv;
1951 struct ccw_device *cdev;
1952 struct subchannel *sch;
1953 enum cdev_todo todo;
1954
1955 priv = container_of(work, struct ccw_device_private, todo_work);
1956 cdev = priv->cdev;
1957 sch = to_subchannel(cdev->dev.parent);
1958 /* Find out todo. */
1959 spin_lock_irq(cdev->ccwlock);
1960 todo = priv->todo;
1961 priv->todo = CDEV_TODO_NOTHING;
1962 CIO_MSG_EVENT(4, "cdev_todo: cdev=0.%x.%04x todo=%d\n",
1963 priv->dev_id.ssid, priv->dev_id.devno, todo);
1964 spin_unlock_irq(cdev->ccwlock);
1965 /* Perform todo. */
1966 switch (todo) {
1967 case CDEV_TODO_ENABLE_CMF:
1968 cmf_reenable(cdev);
1969 break;
1970 case CDEV_TODO_REBIND:
1971 ccw_device_do_unbind_bind(cdev);
1972 break;
1973 case CDEV_TODO_REGISTER:
1974 io_subchannel_register(cdev);
1975 break;
1976 case CDEV_TODO_UNREG_EVAL:
1977 if (!sch_is_pseudo_sch(sch))
1978 css_schedule_eval(sch->schid);
1979 /* fall-through */
1980 case CDEV_TODO_UNREG:
1981 if (sch_is_pseudo_sch(sch))
1982 ccw_device_unregister(cdev);
1983 else
1984 ccw_device_call_sch_unregister(cdev);
1985 break;
1986 default:
1987 break;
1988 }
1989 /* Release workqueue ref. */
1990 put_device(&cdev->dev);
1991}
1992
1993/**
1994 * ccw_device_sched_todo - schedule ccw device operation
1995 * @cdev: ccw device
1996 * @todo: todo
1997 *
1998 * Schedule the operation identified by @todo to be performed on the slow path
1999 * workqueue. Do nothing if another operation with higher priority is already
2000 * scheduled. Needs to be called with ccwdev lock held.
2001 */
2002void ccw_device_sched_todo(struct ccw_device *cdev, enum cdev_todo todo)
2003{
2004 CIO_MSG_EVENT(4, "cdev_todo: sched cdev=0.%x.%04x todo=%d\n",
2005 cdev->private->dev_id.ssid, cdev->private->dev_id.devno,
2006 todo);
2007 if (cdev->private->todo >= todo)
2008 return;
2009 cdev->private->todo = todo;
2010 /* Get workqueue ref. */
2011 if (!get_device(&cdev->dev))
2012 return;
2013 if (!queue_work(slow_path_wq, &cdev->private->todo_work)) {
2014 /* Already queued, release workqueue ref. */
2015 put_device(&cdev->dev);
2016 }
2017}
2018
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019MODULE_LICENSE("GPL");
2020EXPORT_SYMBOL(ccw_device_set_online);
2021EXPORT_SYMBOL(ccw_device_set_offline);
2022EXPORT_SYMBOL(ccw_driver_register);
2023EXPORT_SYMBOL(ccw_driver_unregister);
2024EXPORT_SYMBOL(get_ccwdev_by_busid);
2025EXPORT_SYMBOL(ccw_bus_type);
2026EXPORT_SYMBOL(ccw_device_work);
Cornelia Hucka8237fc2006-01-06 00:19:21 -08002027EXPORT_SYMBOL_GPL(ccw_device_get_subchannel_id);