blob: 6aa2f069c6bed6c70819a12bfd68ad324eb4f591 [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 Oberparleitera7ae2c02009-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 Huckd7b5a4c2006-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);
Sebastian Ott8ea7f552009-09-22 22:58:35 +0200139wait_queue_head_t ccw_device_init_wq;
140atomic_t ccw_device_init_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
Cornelia Huckf08adc02008-07-14 09:59:03 +0200142static struct css_device_id io_subchannel_ids[] = {
143 { .match_flags = 0x1, .type = SUBCHANNEL_TYPE_IO, },
144 { /* end of list */ },
145};
146MODULE_DEVICE_TABLE(css, io_subchannel_ids);
147
Cornelia Huck93a27592009-06-16 10:30:23 +0200148static int io_subchannel_prepare(struct subchannel *sch)
149{
150 struct ccw_device *cdev;
151 /*
152 * Don't allow suspend while a ccw device registration
153 * is still outstanding.
154 */
155 cdev = sch_get_cdev(sch);
156 if (cdev && !device_is_registered(&cdev->dev))
157 return -EAGAIN;
158 return 0;
159}
160
Sebastian Ottb4c70722010-02-26 22:37:27 +0100161static int io_subchannel_settle(void)
Sebastian Ott8ea7f552009-09-22 22:58:35 +0200162{
Sebastian Ottb4c70722010-02-26 22:37:27 +0100163 int ret;
164
165 ret = wait_event_interruptible(ccw_device_init_wq,
166 atomic_read(&ccw_device_init_count) == 0);
167 if (ret)
168 return -EINTR;
Sebastian Ottbe5d3822010-02-26 22:37:24 +0100169 flush_workqueue(cio_work_q);
Sebastian Ottb4c70722010-02-26 22:37:27 +0100170 return 0;
Sebastian Ott8ea7f552009-09-22 22:58:35 +0200171}
172
Cornelia Huckf7e5d672007-05-10 15:45:43 +0200173static struct css_driver io_subchannel_driver = {
Cornelia Huck4beee642008-01-26 14:10:47 +0100174 .owner = THIS_MODULE,
Cornelia Huckf08adc02008-07-14 09:59:03 +0200175 .subchannel_type = io_subchannel_ids,
Cornelia Huck25b7bb52008-01-26 14:10:41 +0100176 .name = "io_subchannel",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 .irq = io_subchannel_irq,
Cornelia Huckc820de32008-07-14 09:58:45 +0200178 .sch_event = io_subchannel_sch_event,
179 .chp_event = io_subchannel_chp_event,
Cornelia Huck8bbace72006-01-11 10:56:22 +0100180 .probe = io_subchannel_probe,
181 .remove = io_subchannel_remove,
182 .shutdown = io_subchannel_shutdown,
Cornelia Huck93a27592009-06-16 10:30:23 +0200183 .prepare = io_subchannel_prepare,
Sebastian Ott8ea7f552009-09-22 22:58:35 +0200184 .settle = io_subchannel_settle,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185};
186
Sebastian Ott2f176442009-09-22 22:58:33 +0200187int __init io_subchannel_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188{
189 int ret;
190
191 init_waitqueue_head(&ccw_device_init_wq);
192 atomic_set(&ccw_device_init_count, 0);
Peter Oberparleiter90ab1332008-01-26 14:10:52 +0100193 setup_timer(&recovery_timer, recovery_func, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
Sebastian Ottbe5d3822010-02-26 22:37:24 +0100195 ret = bus_register(&ccw_bus_type);
196 if (ret)
197 return ret;
Cornelia Huck25b7bb52008-01-26 14:10:41 +0100198 ret = css_driver_register(&io_subchannel_driver);
199 if (ret)
Sebastian Ottbe5d3822010-02-26 22:37:24 +0100200 bus_unregister(&ccw_bus_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 return ret;
203}
204
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
206/************************ device handling **************************/
207
208/*
209 * A ccw_device has some interfaces in sysfs in addition to the
210 * standard ones.
211 * The following entries are designed to export the information which
212 * resided in 2.4 in /proc/subchannels. Subchannel and device number
213 * are obvious, so they don't have an entry :)
214 * TODO: Split chpids and pimpampom up? Where is "in use" in the tree?
215 */
216static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400217chpids_show (struct device * dev, struct device_attribute *attr, char * buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218{
219 struct subchannel *sch = to_subchannel(dev);
Peter Oberparleiter7ad6a242007-04-27 16:01:35 +0200220 struct chsc_ssd_info *ssd = &sch->ssd_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 ssize_t ret = 0;
222 int chp;
Peter Oberparleiter7ad6a242007-04-27 16:01:35 +0200223 int mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
Peter Oberparleiter7ad6a242007-04-27 16:01:35 +0200225 for (chp = 0; chp < 8; chp++) {
226 mask = 0x80 >> chp;
227 if (ssd->path_mask & mask)
228 ret += sprintf(buf + ret, "%02x ", ssd->chpid[chp].id);
229 else
230 ret += sprintf(buf + ret, "00 ");
231 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 ret += sprintf (buf+ret, "\n");
233 return min((ssize_t)PAGE_SIZE, ret);
234}
235
236static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400237pimpampom_show (struct device * dev, struct device_attribute *attr, char * buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238{
239 struct subchannel *sch = to_subchannel(dev);
240 struct pmcw *pmcw = &sch->schib.pmcw;
241
242 return sprintf (buf, "%02x %02x %02x\n",
243 pmcw->pim, pmcw->pam, pmcw->pom);
244}
245
246static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400247devtype_show (struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248{
249 struct ccw_device *cdev = to_ccwdev(dev);
250 struct ccw_device_id *id = &(cdev->id);
251
252 if (id->dev_type != 0)
253 return sprintf(buf, "%04x/%02x\n",
254 id->dev_type, id->dev_model);
255 else
256 return sprintf(buf, "n/a\n");
257}
258
259static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400260cutype_show (struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261{
262 struct ccw_device *cdev = to_ccwdev(dev);
263 struct ccw_device_id *id = &(cdev->id);
264
265 return sprintf(buf, "%04x/%02x\n",
266 id->cu_type, id->cu_model);
267}
268
269static ssize_t
Bastian Blankf1fc78a2005-10-30 15:00:12 -0800270modalias_show (struct device *dev, struct device_attribute *attr, char *buf)
271{
272 struct ccw_device *cdev = to_ccwdev(dev);
273 struct ccw_device_id *id = &(cdev->id);
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +0200274 int len;
Bastian Blankf1fc78a2005-10-30 15:00:12 -0800275
Cornelia Huck086a6c62007-07-17 13:36:08 +0200276 len = snprint_alias(buf, PAGE_SIZE, id, "\n");
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +0200277
278 return len > PAGE_SIZE ? PAGE_SIZE : len;
Bastian Blankf1fc78a2005-10-30 15:00:12 -0800279}
280
281static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400282online_show (struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283{
284 struct ccw_device *cdev = to_ccwdev(dev);
285
286 return sprintf(buf, cdev->online ? "1\n" : "0\n");
287}
288
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100289int ccw_device_is_orphan(struct ccw_device *cdev)
290{
291 return sch_is_pseudo_sch(to_subchannel(cdev->dev.parent));
292}
293
Cornelia Huckef995162007-04-27 16:01:39 +0200294static void ccw_device_unregister(struct ccw_device *cdev)
Cornelia Huck7674da72006-12-08 15:54:21 +0100295{
Sebastian Ott7d253b92009-12-07 12:51:33 +0100296 if (device_is_registered(&cdev->dev)) {
Sebastian Ott24a18722009-12-07 12:51:34 +0100297 /* Undo device_add(). */
Cornelia Huckef995162007-04-27 16:01:39 +0200298 device_del(&cdev->dev);
Sebastian Ott24a18722009-12-07 12:51:34 +0100299 }
300 if (cdev->private->flags.initialized) {
301 cdev->private->flags.initialized = 0;
Sebastian Ott3b554a12009-09-11 10:28:26 +0200302 /* Release reference from device_initialize(). */
303 put_device(&cdev->dev);
304 }
Cornelia Huck7674da72006-12-08 15:54:21 +0100305}
306
Sebastian Ottd40f7b72009-12-07 12:51:41 +0100307static void io_subchannel_quiesce(struct subchannel *);
308
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +0200309/**
310 * ccw_device_set_offline() - disable a ccw device for I/O
311 * @cdev: target ccw device
312 *
313 * This function calls the driver's set_offline() function for @cdev, if
314 * given, and then disables @cdev.
315 * Returns:
316 * %0 on success and a negative error value on failure.
317 * Context:
318 * enabled, ccw device lock not held
319 */
320int ccw_device_set_offline(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321{
Sebastian Ottd40f7b72009-12-07 12:51:41 +0100322 struct subchannel *sch;
323 int ret, state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
325 if (!cdev)
326 return -ENODEV;
327 if (!cdev->online || !cdev->drv)
328 return -EINVAL;
329
330 if (cdev->drv->set_offline) {
331 ret = cdev->drv->set_offline(cdev);
332 if (ret != 0)
333 return ret;
334 }
335 cdev->online = 0;
336 spin_lock_irq(cdev->ccwlock);
Sebastian Ottd40f7b72009-12-07 12:51:41 +0100337 sch = to_subchannel(cdev->dev.parent);
Michael Ernst217ee6c2009-09-11 10:28:21 +0200338 /* Wait until a final state or DISCONNECTED is reached */
339 while (!dev_fsm_final_state(cdev) &&
340 cdev->private->state != DEV_STATE_DISCONNECTED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 spin_unlock_irq(cdev->ccwlock);
Michael Ernst217ee6c2009-09-11 10:28:21 +0200342 wait_event(cdev->private->wait_q, (dev_fsm_final_state(cdev) ||
343 cdev->private->state == DEV_STATE_DISCONNECTED));
344 spin_lock_irq(cdev->ccwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 }
Sebastian Ottd40f7b72009-12-07 12:51:41 +0100346 do {
347 ret = ccw_device_offline(cdev);
348 if (!ret)
349 break;
350 CIO_MSG_EVENT(0, "ccw_device_offline returned %d, device "
351 "0.%x.%04x\n", ret, cdev->private->dev_id.ssid,
352 cdev->private->dev_id.devno);
353 if (ret != -EBUSY)
354 goto error;
355 state = cdev->private->state;
356 spin_unlock_irq(cdev->ccwlock);
357 io_subchannel_quiesce(sch);
358 spin_lock_irq(cdev->ccwlock);
359 cdev->private->state = state;
360 } while (ret == -EBUSY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 spin_unlock_irq(cdev->ccwlock);
Michael Ernst217ee6c2009-09-11 10:28:21 +0200362 wait_event(cdev->private->wait_q, (dev_fsm_final_state(cdev) ||
363 cdev->private->state == DEV_STATE_DISCONNECTED));
Peter Oberparleitera7ae2c02009-12-07 12:51:20 +0100364 /* Inform the user if set offline failed. */
365 if (cdev->private->state == DEV_STATE_BOXED) {
366 pr_warning("%s: The device entered boxed state while "
367 "being set offline\n", dev_name(&cdev->dev));
368 } else if (cdev->private->state == DEV_STATE_NOT_OPER) {
369 pr_warning("%s: The device stopped operating while "
370 "being set offline\n", dev_name(&cdev->dev));
371 }
Michael Ernst217ee6c2009-09-11 10:28:21 +0200372 /* Give up reference from ccw_device_set_online(). */
373 put_device(&cdev->dev);
374 return 0;
375
376error:
Michael Ernst217ee6c2009-09-11 10:28:21 +0200377 cdev->private->state = DEV_STATE_OFFLINE;
378 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
379 spin_unlock_irq(cdev->ccwlock);
380 /* Give up reference from ccw_device_set_online(). */
381 put_device(&cdev->dev);
382 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383}
384
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +0200385/**
386 * ccw_device_set_online() - enable a ccw device for I/O
387 * @cdev: target ccw device
388 *
389 * This function first enables @cdev and then calls the driver's set_online()
390 * function for @cdev, if given. If set_online() returns an error, @cdev is
391 * disabled again.
392 * Returns:
393 * %0 on success and a negative error value on failure.
394 * Context:
395 * enabled, ccw device lock not held
396 */
397int ccw_device_set_online(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398{
399 int ret;
Michael Ernst217ee6c2009-09-11 10:28:21 +0200400 int ret2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
402 if (!cdev)
403 return -ENODEV;
404 if (cdev->online || !cdev->drv)
405 return -EINVAL;
Cornelia Huck9cd67422008-12-25 13:39:06 +0100406 /* Hold on to an extra reference while device is online. */
407 if (!get_device(&cdev->dev))
408 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
410 spin_lock_irq(cdev->ccwlock);
411 ret = ccw_device_online(cdev);
412 spin_unlock_irq(cdev->ccwlock);
413 if (ret == 0)
414 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
415 else {
Michael Ernst139b83d2008-05-07 09:22:54 +0200416 CIO_MSG_EVENT(0, "ccw_device_online returned %d, "
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200417 "device 0.%x.%04x\n",
418 ret, cdev->private->dev_id.ssid,
419 cdev->private->dev_id.devno);
Cornelia Huck9cd67422008-12-25 13:39:06 +0100420 /* Give up online reference since onlining failed. */
421 put_device(&cdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 return ret;
423 }
Michael Ernst217ee6c2009-09-11 10:28:21 +0200424 spin_lock_irq(cdev->ccwlock);
425 /* Check if online processing was successful */
426 if ((cdev->private->state != DEV_STATE_ONLINE) &&
427 (cdev->private->state != DEV_STATE_W4SENSE)) {
428 spin_unlock_irq(cdev->ccwlock);
Peter Oberparleitera7ae2c02009-12-07 12:51:20 +0100429 /* Inform the user that set online failed. */
430 if (cdev->private->state == DEV_STATE_BOXED) {
431 pr_warning("%s: Setting the device online failed "
432 "because it is boxed\n",
433 dev_name(&cdev->dev));
434 } else if (cdev->private->state == DEV_STATE_NOT_OPER) {
435 pr_warning("%s: Setting the device online failed "
436 "because it is not operational\n",
437 dev_name(&cdev->dev));
438 }
Cornelia Huck9cd67422008-12-25 13:39:06 +0100439 /* Give up online reference since onlining failed. */
440 put_device(&cdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 return -ENODEV;
Cornelia Huck9cd67422008-12-25 13:39:06 +0100442 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 spin_unlock_irq(cdev->ccwlock);
Michael Ernst217ee6c2009-09-11 10:28:21 +0200444 if (cdev->drv->set_online)
445 ret = cdev->drv->set_online(cdev);
446 if (ret)
447 goto rollback;
448 cdev->online = 1;
449 return 0;
450
451rollback:
452 spin_lock_irq(cdev->ccwlock);
453 /* Wait until a final state or DISCONNECTED is reached */
454 while (!dev_fsm_final_state(cdev) &&
455 cdev->private->state != DEV_STATE_DISCONNECTED) {
456 spin_unlock_irq(cdev->ccwlock);
457 wait_event(cdev->private->wait_q, (dev_fsm_final_state(cdev) ||
458 cdev->private->state == DEV_STATE_DISCONNECTED));
459 spin_lock_irq(cdev->ccwlock);
460 }
461 ret2 = ccw_device_offline(cdev);
462 if (ret2)
463 goto error;
464 spin_unlock_irq(cdev->ccwlock);
465 wait_event(cdev->private->wait_q, (dev_fsm_final_state(cdev) ||
466 cdev->private->state == DEV_STATE_DISCONNECTED));
Cornelia Huck9cd67422008-12-25 13:39:06 +0100467 /* Give up online reference since onlining failed. */
468 put_device(&cdev->dev);
Michael Ernst217ee6c2009-09-11 10:28:21 +0200469 return ret;
470
471error:
472 CIO_MSG_EVENT(0, "rollback ccw_device_offline returned %d, "
473 "device 0.%x.%04x\n",
474 ret2, cdev->private->dev_id.ssid,
475 cdev->private->dev_id.devno);
476 cdev->private->state = DEV_STATE_OFFLINE;
477 spin_unlock_irq(cdev->ccwlock);
478 /* Give up online reference since onlining failed. */
479 put_device(&cdev->dev);
480 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481}
482
Sebastian Otte74fe0c2009-03-26 15:24:08 +0100483static int online_store_handle_offline(struct ccw_device *cdev)
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200484{
Peter Oberparleiter37de53b2009-12-07 12:51:19 +0100485 if (cdev->private->state == DEV_STATE_DISCONNECTED) {
486 spin_lock_irq(cdev->ccwlock);
487 ccw_device_sched_todo(cdev, CDEV_TODO_UNREG_EVAL);
488 spin_unlock_irq(cdev->ccwlock);
489 } else if (cdev->online && cdev->drv && cdev->drv->set_offline)
Sebastian Otte74fe0c2009-03-26 15:24:08 +0100490 return ccw_device_set_offline(cdev);
491 return 0;
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200492}
493
494static int online_store_recog_and_online(struct ccw_device *cdev)
495{
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200496 /* Do device recognition, if needed. */
Sebastian Ott99f6a5702009-03-31 19:16:07 +0200497 if (cdev->private->state == DEV_STATE_BOXED) {
Peter Oberparleiter1f5bd382009-12-07 12:51:23 +0100498 spin_lock_irq(cdev->ccwlock);
Peter Oberparleiter736b5db2009-12-07 12:51:21 +0100499 ccw_device_recognition(cdev);
Peter Oberparleiter1f5bd382009-12-07 12:51:23 +0100500 spin_unlock_irq(cdev->ccwlock);
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200501 wait_event(cdev->private->wait_q,
502 cdev->private->flags.recog_done);
Sebastian Ott156013f2009-03-31 19:16:03 +0200503 if (cdev->private->state != DEV_STATE_OFFLINE)
504 /* recognition failed */
505 return -EAGAIN;
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200506 }
507 if (cdev->drv && cdev->drv->set_online)
508 ccw_device_set_online(cdev);
509 return 0;
510}
Sebastian Ott156013f2009-03-31 19:16:03 +0200511
Michael Ernstc78aa6c2008-07-14 09:59:22 +0200512static int online_store_handle_online(struct ccw_device *cdev, int force)
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200513{
514 int ret;
515
516 ret = online_store_recog_and_online(cdev);
Sebastian Ott156013f2009-03-31 19:16:03 +0200517 if (ret && !force)
Michael Ernstc78aa6c2008-07-14 09:59:22 +0200518 return ret;
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200519 if (force && cdev->private->state == DEV_STATE_BOXED) {
520 ret = ccw_device_stlck(cdev);
Michael Ernstc78aa6c2008-07-14 09:59:22 +0200521 if (ret)
522 return ret;
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200523 if (cdev->id.cu_type == 0)
524 cdev->private->state = DEV_STATE_NOT_OPER;
Sebastian Ott156013f2009-03-31 19:16:03 +0200525 ret = online_store_recog_and_online(cdev);
526 if (ret)
527 return ret;
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200528 }
Michael Ernstc78aa6c2008-07-14 09:59:22 +0200529 return 0;
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200530}
531
532static ssize_t online_store (struct device *dev, struct device_attribute *attr,
533 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534{
535 struct ccw_device *cdev = to_ccwdev(dev);
Cornelia Huck2f972202008-04-30 13:38:33 +0200536 int force, ret;
537 unsigned long i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
Peter Oberparleiter350e9122009-12-07 12:51:28 +0100539 if (!dev_fsm_final_state(cdev) &&
540 cdev->private->state != DEV_STATE_DISCONNECTED)
541 return -EAGAIN;
542 if (atomic_cmpxchg(&cdev->private->onoff, 0, 1) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 return -EAGAIN;
544
545 if (cdev->drv && !try_module_get(cdev->drv->owner)) {
546 atomic_set(&cdev->private->onoff, 0);
547 return -EINVAL;
548 }
549 if (!strncmp(buf, "force\n", count)) {
550 force = 1;
551 i = 1;
Cornelia Huck2f972202008-04-30 13:38:33 +0200552 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 } else {
554 force = 0;
Cornelia Huck2f972202008-04-30 13:38:33 +0200555 ret = strict_strtoul(buf, 16, &i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 }
Cornelia Huck2f972202008-04-30 13:38:33 +0200557 if (ret)
558 goto out;
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200559 switch (i) {
560 case 0:
Sebastian Otte74fe0c2009-03-26 15:24:08 +0100561 ret = online_store_handle_offline(cdev);
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200562 break;
563 case 1:
Michael Ernstc78aa6c2008-07-14 09:59:22 +0200564 ret = online_store_handle_online(cdev, force);
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200565 break;
566 default:
Cornelia Huck2f972202008-04-30 13:38:33 +0200567 ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 }
Cornelia Huck2f972202008-04-30 13:38:33 +0200569out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 if (cdev->drv)
571 module_put(cdev->drv->owner);
572 atomic_set(&cdev->private->onoff, 0);
Sebastian Otte74fe0c2009-03-26 15:24:08 +0100573 return (ret < 0) ? ret : count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574}
575
576static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400577available_show (struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578{
579 struct ccw_device *cdev = to_ccwdev(dev);
580 struct subchannel *sch;
581
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100582 if (ccw_device_is_orphan(cdev))
583 return sprintf(buf, "no device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 switch (cdev->private->state) {
585 case DEV_STATE_BOXED:
586 return sprintf(buf, "boxed\n");
587 case DEV_STATE_DISCONNECTED:
588 case DEV_STATE_DISCONNECTED_SENSE_ID:
589 case DEV_STATE_NOT_OPER:
590 sch = to_subchannel(dev->parent);
591 if (!sch->lpm)
592 return sprintf(buf, "no path\n");
593 else
594 return sprintf(buf, "no device\n");
595 default:
596 /* All other states considered fine. */
597 return sprintf(buf, "good\n");
598 }
599}
600
601static DEVICE_ATTR(chpids, 0444, chpids_show, NULL);
602static DEVICE_ATTR(pimpampom, 0444, pimpampom_show, NULL);
603static DEVICE_ATTR(devtype, 0444, devtype_show, NULL);
604static DEVICE_ATTR(cutype, 0444, cutype_show, NULL);
Bastian Blankf1fc78a2005-10-30 15:00:12 -0800605static DEVICE_ATTR(modalias, 0444, modalias_show, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606static DEVICE_ATTR(online, 0644, online_show, online_store);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607static DEVICE_ATTR(availability, 0444, available_show, NULL);
608
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200609static struct attribute *io_subchannel_attrs[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 &dev_attr_chpids.attr,
611 &dev_attr_pimpampom.attr,
612 NULL,
613};
614
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200615static struct attribute_group io_subchannel_attr_group = {
616 .attrs = io_subchannel_attrs,
Cornelia Huck529192f2006-12-08 15:55:57 +0100617};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
619static struct attribute * ccwdev_attrs[] = {
620 &dev_attr_devtype.attr,
621 &dev_attr_cutype.attr,
Bastian Blankf1fc78a2005-10-30 15:00:12 -0800622 &dev_attr_modalias.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 &dev_attr_online.attr,
624 &dev_attr_cmb_enable.attr,
625 &dev_attr_availability.attr,
626 NULL,
627};
628
629static struct attribute_group ccwdev_attr_group = {
630 .attrs = ccwdev_attrs,
631};
632
David Brownella4dbd672009-06-24 10:06:31 -0700633static const struct attribute_group *ccwdev_attr_groups[] = {
Cornelia Huckef995162007-04-27 16:01:39 +0200634 &ccwdev_attr_group,
635 NULL,
636};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
638/* this is a simple abstraction for device_register that sets the
639 * correct bus type and adds the bus specific files */
Cornelia Huck3c9da7b2006-10-27 12:39:33 +0200640static int ccw_device_register(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641{
642 struct device *dev = &cdev->dev;
643 int ret;
644
645 dev->bus = &ccw_bus_type;
Sebastian Ott3ac276f2009-09-11 10:28:27 +0200646 ret = dev_set_name(&cdev->dev, "0.%x.%04x", cdev->private->dev_id.ssid,
647 cdev->private->dev_id.devno);
648 if (ret)
649 return ret;
Sebastian Ott7d253b92009-12-07 12:51:33 +0100650 return device_add(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651}
652
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100653static int match_dev_id(struct device *dev, void *data)
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700654{
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100655 struct ccw_device *cdev = to_ccwdev(dev);
656 struct ccw_dev_id *dev_id = data;
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700657
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100658 return ccw_dev_id_is_equal(&cdev->private->dev_id, dev_id);
659}
660
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100661static struct ccw_device *get_ccwdev_by_dev_id(struct ccw_dev_id *dev_id)
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100662{
663 struct device *dev;
664
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100665 dev = bus_find_device(&ccw_bus_type, NULL, dev_id, match_dev_id);
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100666
667 return dev ? to_ccwdev(dev) : NULL;
668}
669
Peter Oberparleiter37de53b2009-12-07 12:51:19 +0100670static void ccw_device_do_unbind_bind(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671{
Cornelia Huckeb32ae82009-03-26 15:24:05 +0100672 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
Sebastian Ott7d253b92009-12-07 12:51:33 +0100674 if (device_is_registered(&cdev->dev)) {
Cornelia Huckeb32ae82009-03-26 15:24:05 +0100675 device_release_driver(&cdev->dev);
676 ret = device_attach(&cdev->dev);
677 WARN_ON(ret == -ENODEV);
678 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679}
680
681static void
682ccw_device_release(struct device *dev)
683{
684 struct ccw_device *cdev;
685
686 cdev = to_ccwdev(dev);
Cornelia Huck6eff2082008-12-25 13:39:07 +0100687 /* Release reference of parent subchannel. */
688 put_device(cdev->dev.parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 kfree(cdev->private);
690 kfree(cdev);
691}
692
Cornelia Huck7674da72006-12-08 15:54:21 +0100693static struct ccw_device * io_subchannel_allocate_dev(struct subchannel *sch)
694{
695 struct ccw_device *cdev;
696
697 cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
698 if (cdev) {
699 cdev->private = kzalloc(sizeof(struct ccw_device_private),
700 GFP_KERNEL | GFP_DMA);
701 if (cdev->private)
702 return cdev;
703 }
704 kfree(cdev);
705 return ERR_PTR(-ENOMEM);
706}
707
Peter Oberparleiter37de53b2009-12-07 12:51:19 +0100708static void ccw_device_todo(struct work_struct *work);
709
Cornelia Huck7674da72006-12-08 15:54:21 +0100710static int io_subchannel_initialize_dev(struct subchannel *sch,
711 struct ccw_device *cdev)
712{
713 cdev->private->cdev = cdev;
714 atomic_set(&cdev->private->onoff, 0);
715 cdev->dev.parent = &sch->dev;
716 cdev->dev.release = ccw_device_release;
Peter Oberparleiter37de53b2009-12-07 12:51:19 +0100717 INIT_WORK(&cdev->private->todo_work, ccw_device_todo);
Cornelia Huckef995162007-04-27 16:01:39 +0200718 cdev->dev.groups = ccwdev_attr_groups;
Cornelia Huck7674da72006-12-08 15:54:21 +0100719 /* Do first half of device_register. */
720 device_initialize(&cdev->dev);
721 if (!get_device(&sch->dev)) {
Cornelia Huck283fdd02008-12-25 13:39:10 +0100722 /* Release reference from device_initialize(). */
723 put_device(&cdev->dev);
Cornelia Huck7674da72006-12-08 15:54:21 +0100724 return -ENODEV;
725 }
Sebastian Ott24a18722009-12-07 12:51:34 +0100726 cdev->private->flags.initialized = 1;
Cornelia Huck7674da72006-12-08 15:54:21 +0100727 return 0;
728}
729
730static struct ccw_device * io_subchannel_create_ccwdev(struct subchannel *sch)
731{
732 struct ccw_device *cdev;
733 int ret;
734
735 cdev = io_subchannel_allocate_dev(sch);
736 if (!IS_ERR(cdev)) {
737 ret = io_subchannel_initialize_dev(sch, cdev);
Sebastian Ott06739a82009-08-23 18:09:04 +0200738 if (ret)
Cornelia Huck7674da72006-12-08 15:54:21 +0100739 cdev = ERR_PTR(ret);
Cornelia Huck7674da72006-12-08 15:54:21 +0100740 }
741 return cdev;
742}
743
Peter Oberparleiter736b5db2009-12-07 12:51:21 +0100744static void io_subchannel_recog(struct ccw_device *, struct subchannel *);
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100745
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100746static void sch_create_and_recog_new_device(struct subchannel *sch)
747{
748 struct ccw_device *cdev;
749
750 /* Need to allocate a new ccw device. */
751 cdev = io_subchannel_create_ccwdev(sch);
752 if (IS_ERR(cdev)) {
753 /* OK, we did everything we could... */
754 css_sch_device_unregister(sch);
755 return;
756 }
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100757 /* Start recognition for the new ccw device. */
Peter Oberparleiter736b5db2009-12-07 12:51:21 +0100758 io_subchannel_recog(cdev, sch);
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100759}
760
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761/*
762 * Register recognized device.
763 */
Peter Oberparleiter37de53b2009-12-07 12:51:19 +0100764static void io_subchannel_register(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 struct subchannel *sch;
767 int ret;
768 unsigned long flags;
769
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 sch = to_subchannel(cdev->dev.parent);
Cornelia Huck5fb6b852008-12-25 13:39:08 +0100771 /*
772 * Check if subchannel is still registered. It may have become
773 * unregistered if a machine check hit us after finishing
774 * device recognition but before the register work could be
775 * queued.
776 */
777 if (!device_is_registered(&sch->dev))
778 goto out_err;
Cornelia Huck82b7ac02007-04-27 16:01:36 +0200779 css_update_ssd_info(sch);
Cornelia Huck47af5512006-12-04 15:41:07 +0100780 /*
781 * io_subchannel_register() will also be called after device
782 * recognition has been done for a boxed device (which will already
783 * be registered). We need to reprobe since we may now have sense id
784 * information.
785 */
Cornelia Huckd6a30762008-12-25 13:39:11 +0100786 if (device_is_registered(&cdev->dev)) {
Cornelia Huck47af5512006-12-04 15:41:07 +0100787 if (!cdev->drv) {
788 ret = device_reprobe(&cdev->dev);
789 if (ret)
790 /* We can't do much here. */
Michael Ernst139b83d2008-05-07 09:22:54 +0200791 CIO_MSG_EVENT(0, "device_reprobe() returned"
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200792 " %d for 0.%x.%04x\n", ret,
793 cdev->private->dev_id.ssid,
794 cdev->private->dev_id.devno);
Cornelia Huck47af5512006-12-04 15:41:07 +0100795 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 goto out;
797 }
Cornelia Huckfa1a8c22007-04-26 00:12:03 -0700798 /*
799 * Now we know this subchannel will stay, we can throw
800 * our delayed uevent.
801 */
Ming Leif67f1292009-03-01 21:10:49 +0800802 dev_set_uevent_suppress(&sch->dev, 0);
Cornelia Huckfa1a8c22007-04-26 00:12:03 -0700803 kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 /* make it known to the system */
805 ret = ccw_device_register(cdev);
806 if (ret) {
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200807 CIO_MSG_EVENT(0, "Could not register ccw dev 0.%x.%04x: %d\n",
808 cdev->private->dev_id.ssid,
809 cdev->private->dev_id.devno, ret);
Cornelia Huck2ec22982006-12-08 15:54:26 +0100810 spin_lock_irqsave(sch->lock, flags);
Cornelia Huckdb6a6422008-01-26 14:10:46 +0100811 sch_set_cdev(sch, NULL);
Cornelia Huck2ec22982006-12-08 15:54:26 +0100812 spin_unlock_irqrestore(sch->lock, flags);
Cornelia Huck6eff2082008-12-25 13:39:07 +0100813 /* Release initial device reference. */
814 put_device(&cdev->dev);
Cornelia Huck5fb6b852008-12-25 13:39:08 +0100815 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817out:
818 cdev->private->flags.recog_done = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 wake_up(&cdev->private->wait_q);
Cornelia Huck5fb6b852008-12-25 13:39:08 +0100820out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 if (atomic_dec_and_test(&ccw_device_init_count))
822 wake_up(&ccw_device_init_wq);
823}
824
Peter Oberparleiter37de53b2009-12-07 12:51:19 +0100825static void ccw_device_call_sch_unregister(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 struct subchannel *sch;
828
Peter Oberparleiter46fbe4e2008-10-10 21:33:05 +0200829 /* Get subchannel reference for local processing. */
830 if (!get_device(cdev->dev.parent))
831 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 sch = to_subchannel(cdev->dev.parent);
Cornelia Huck6ab48792006-07-12 16:39:50 +0200833 css_sch_device_unregister(sch);
Peter Oberparleiter46fbe4e2008-10-10 21:33:05 +0200834 /* Release subchannel reference for local processing. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 put_device(&sch->dev);
836}
837
838/*
839 * subchannel recognition done. Called from the state machine.
840 */
841void
842io_subchannel_recog_done(struct ccw_device *cdev)
843{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 if (css_init_done == 0) {
845 cdev->private->flags.recog_done = 1;
846 return;
847 }
848 switch (cdev->private->state) {
Sebastian Ott47593bf2009-03-31 19:16:05 +0200849 case DEV_STATE_BOXED:
850 /* Device did not respond in time. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 case DEV_STATE_NOT_OPER:
852 cdev->private->flags.recog_done = 1;
Peter Oberparleiter37de53b2009-12-07 12:51:19 +0100853 /* Remove device found not operational. */
854 ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 if (atomic_dec_and_test(&ccw_device_init_count))
856 wake_up(&ccw_device_init_wq);
857 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 case DEV_STATE_OFFLINE:
859 /*
860 * We can't register the device in interrupt context so
861 * we schedule a work item.
862 */
Peter Oberparleiter37de53b2009-12-07 12:51:19 +0100863 ccw_device_sched_todo(cdev, CDEV_TODO_REGISTER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 break;
865 }
866}
867
Peter Oberparleiter736b5db2009-12-07 12:51:21 +0100868static void io_subchannel_recog(struct ccw_device *cdev, struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 struct ccw_device_private *priv;
871
Cornelia Huck2ec22982006-12-08 15:54:26 +0100872 cdev->ccwlock = sch->lock;
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800873
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 /* Init private data. */
875 priv = cdev->private;
Cornelia Huck78964262006-10-11 15:31:38 +0200876 priv->dev_id.devno = sch->schib.pmcw.dev;
877 priv->dev_id.ssid = sch->schid.ssid;
878 priv->schid = sch->schid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 priv->state = DEV_STATE_NOT_OPER;
880 INIT_LIST_HEAD(&priv->cmb_list);
881 init_waitqueue_head(&priv->wait_q);
882 init_timer(&priv->timer);
883
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 /* Increase counter of devices currently in recognition. */
885 atomic_inc(&ccw_device_init_count);
886
887 /* Start async. device sensing. */
Cornelia Huck2ec22982006-12-08 15:54:26 +0100888 spin_lock_irq(sch->lock);
Peter Oberparleiter60e4dac2009-12-07 12:51:16 +0100889 sch_set_cdev(sch, cdev);
Peter Oberparleiter736b5db2009-12-07 12:51:21 +0100890 ccw_device_recognition(cdev);
Cornelia Huck2ec22982006-12-08 15:54:26 +0100891 spin_unlock_irq(sch->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892}
893
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100894static int ccw_device_move_to_sch(struct ccw_device *cdev,
895 struct subchannel *sch)
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100896{
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100897 struct subchannel *old_sch;
Sebastian Ott0c609fc2009-12-07 12:51:37 +0100898 int rc, old_enabled = 0;
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100899
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100900 old_sch = to_subchannel(cdev->dev.parent);
901 /* Obtain child reference for new parent. */
Cornelia Huck6eff2082008-12-25 13:39:07 +0100902 if (!get_device(&sch->dev))
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100903 return -ENODEV;
Sebastian Ott0c609fc2009-12-07 12:51:37 +0100904
905 if (!sch_is_pseudo_sch(old_sch)) {
906 spin_lock_irq(old_sch->lock);
907 old_enabled = old_sch->schib.pmcw.ena;
908 rc = 0;
909 if (old_enabled)
910 rc = cio_disable_subchannel(old_sch);
911 spin_unlock_irq(old_sch->lock);
912 if (rc == -EBUSY) {
913 /* Release child reference for new parent. */
914 put_device(&sch->dev);
915 return rc;
916 }
917 }
918
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100919 mutex_lock(&sch->reg_mutex);
Cornelia Huckffa6a702009-03-04 12:44:00 +0100920 rc = device_move(&cdev->dev, &sch->dev, DPM_ORDER_PARENT_BEFORE_DEV);
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100921 mutex_unlock(&sch->reg_mutex);
922 if (rc) {
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100923 CIO_MSG_EVENT(0, "device_move(0.%x.%04x,0.%x.%04x)=%d\n",
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100924 cdev->private->dev_id.ssid,
925 cdev->private->dev_id.devno, sch->schid.ssid,
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100926 sch->schib.pmcw.dev, rc);
Sebastian Ott0c609fc2009-12-07 12:51:37 +0100927 if (old_enabled) {
928 /* Try to reenable the old subchannel. */
929 spin_lock_irq(old_sch->lock);
930 cio_enable_subchannel(old_sch, (u32)(addr_t)old_sch);
931 spin_unlock_irq(old_sch->lock);
932 }
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100933 /* Release child reference for new parent. */
Cornelia Huck6eff2082008-12-25 13:39:07 +0100934 put_device(&sch->dev);
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100935 return rc;
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100936 }
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100937 /* Clean up old subchannel. */
938 if (!sch_is_pseudo_sch(old_sch)) {
939 spin_lock_irq(old_sch->lock);
940 sch_set_cdev(old_sch, NULL);
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100941 spin_unlock_irq(old_sch->lock);
942 css_schedule_eval(old_sch->schid);
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100943 }
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100944 /* Release child reference for old parent. */
945 put_device(&old_sch->dev);
946 /* Initialize new subchannel. */
947 spin_lock_irq(sch->lock);
948 cdev->private->schid = sch->schid;
949 cdev->ccwlock = sch->lock;
950 if (!sch_is_pseudo_sch(sch))
951 sch_set_cdev(sch, cdev);
952 spin_unlock_irq(sch->lock);
953 if (!sch_is_pseudo_sch(sch))
954 css_update_ssd_info(sch);
955 return 0;
956}
957
958static int ccw_device_move_to_orph(struct ccw_device *cdev)
959{
960 struct subchannel *sch = to_subchannel(cdev->dev.parent);
961 struct channel_subsystem *css = to_css(sch->dev.parent);
962
963 return ccw_device_move_to_sch(cdev, css->pseudo_subchannel);
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100964}
965
Cornelia Huck602b20f2008-01-26 14:10:39 +0100966static void io_subchannel_irq(struct subchannel *sch)
967{
968 struct ccw_device *cdev;
969
Cornelia Huckdb6a6422008-01-26 14:10:46 +0100970 cdev = sch_get_cdev(sch);
Cornelia Huck602b20f2008-01-26 14:10:39 +0100971
Sebastian Ottefd986d2009-09-11 10:28:18 +0200972 CIO_TRACE_EVENT(6, "IRQ");
973 CIO_TRACE_EVENT(6, dev_name(&sch->dev));
Cornelia Huck602b20f2008-01-26 14:10:39 +0100974 if (cdev)
975 dev_fsm_event(cdev, DEV_EVENT_INTERRUPT);
976}
977
Sebastian Ott13952ec12008-12-25 13:39:13 +0100978void io_subchannel_init_config(struct subchannel *sch)
979{
980 memset(&sch->config, 0, sizeof(sch->config));
981 sch->config.csense = 1;
Sebastian Ott13952ec12008-12-25 13:39:13 +0100982}
983
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +0200984static void io_subchannel_init_fields(struct subchannel *sch)
985{
986 if (cio_is_console(sch->schid))
987 sch->opm = 0xff;
988 else
989 sch->opm = chp_get_sch_opm(sch);
990 sch->lpm = sch->schib.pmcw.pam & sch->opm;
Cornelia Huck3a3fc292008-07-14 09:58:58 +0200991 sch->isc = cio_is_console(sch->schid) ? CONSOLE_ISC : IO_SCH_ISC;
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +0200992
993 CIO_MSG_EVENT(6, "Detected device %04x on subchannel 0.%x.%04X"
994 " - PIM = %02X, PAM = %02X, POM = %02X\n",
995 sch->schib.pmcw.dev, sch->schid.ssid,
996 sch->schid.sch_no, sch->schib.pmcw.pim,
997 sch->schib.pmcw.pam, sch->schib.pmcw.pom);
Sebastian Ott13952ec12008-12-25 13:39:13 +0100998
999 io_subchannel_init_config(sch);
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001000}
1001
Cornelia Huck90ed2b62008-12-25 13:39:09 +01001002/*
1003 * Note: We always return 0 so that we bind to the device even on error.
1004 * This is needed so that our remove function is called on unregister.
1005 */
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001006static int io_subchannel_probe(struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 struct ccw_device *cdev;
1009 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010
Peter Oberparleiter6d7c5af2009-10-14 12:43:50 +02001011 if (cio_is_console(sch->schid)) {
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001012 rc = sysfs_create_group(&sch->dev.kobj,
1013 &io_subchannel_attr_group);
1014 if (rc)
1015 CIO_MSG_EVENT(0, "Failed to create io subchannel "
1016 "attributes for subchannel "
1017 "0.%x.%04x (rc=%d)\n",
1018 sch->schid.ssid, sch->schid.sch_no, rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 /*
Peter Oberparleiter6d7c5af2009-10-14 12:43:50 +02001020 * The console subchannel already has an associated ccw_device.
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001021 * Throw the delayed uevent for the subchannel, register
Peter Oberparleiter6d7c5af2009-10-14 12:43:50 +02001022 * the ccw_device and exit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 */
Ming Leif67f1292009-03-01 21:10:49 +08001024 dev_set_uevent_suppress(&sch->dev, 0);
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001025 kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
Peter Oberparleiter6d7c5af2009-10-14 12:43:50 +02001026 cdev = sch_get_cdev(sch);
Cornelia Hucke1031782007-10-12 16:11:23 +02001027 cdev->dev.groups = ccwdev_attr_groups;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 device_initialize(&cdev->dev);
Sebastian Ott24a18722009-12-07 12:51:34 +01001029 cdev->private->flags.initialized = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 ccw_device_register(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 /*
1032 * Check if the device is already online. If it is
Cornelia Huck9cd67422008-12-25 13:39:06 +01001033 * the reference count needs to be corrected since we
1034 * didn't obtain a reference in ccw_device_set_online.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 */
1036 if (cdev->private->state != DEV_STATE_NOT_OPER &&
1037 cdev->private->state != DEV_STATE_OFFLINE &&
1038 cdev->private->state != DEV_STATE_BOXED)
1039 get_device(&cdev->dev);
1040 return 0;
1041 }
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001042 io_subchannel_init_fields(sch);
Sebastian Ottf444cc02008-12-25 13:39:14 +01001043 rc = cio_commit_config(sch);
1044 if (rc)
1045 goto out_schedule;
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001046 rc = sysfs_create_group(&sch->dev.kobj,
1047 &io_subchannel_attr_group);
1048 if (rc)
Cornelia Huck90ed2b62008-12-25 13:39:09 +01001049 goto out_schedule;
Cornelia Huckcd6b4f22008-01-26 14:10:43 +01001050 /* Allocate I/O subchannel private data. */
1051 sch->private = kzalloc(sizeof(struct io_subchannel_private),
1052 GFP_KERNEL | GFP_DMA);
Cornelia Huck90ed2b62008-12-25 13:39:09 +01001053 if (!sch->private)
Peter Oberparleiter48e4c382009-12-07 12:51:15 +01001054 goto out_schedule;
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001055 css_schedule_eval(sch->schid);
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001056 return 0;
Peter Oberparleiter48e4c382009-12-07 12:51:15 +01001057
Cornelia Huck90ed2b62008-12-25 13:39:09 +01001058out_schedule:
Peter Oberparleiter390935a2009-12-07 12:51:18 +01001059 spin_lock_irq(sch->lock);
1060 css_sched_sch_todo(sch, SCH_TODO_UNREG);
1061 spin_unlock_irq(sch->lock);
Cornelia Huck90ed2b62008-12-25 13:39:09 +01001062 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063}
1064
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065static int
Cornelia Huck8bbace72006-01-11 10:56:22 +01001066io_subchannel_remove (struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067{
1068 struct ccw_device *cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001070 cdev = sch_get_cdev(sch);
1071 if (!cdev)
Peter Oberparleiter48e4c382009-12-07 12:51:15 +01001072 goto out_free;
Sebastian Ott6e9a0f62009-12-07 12:51:38 +01001073 io_subchannel_quiesce(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 /* Set ccw device to not operational and drop reference. */
Sebastian Ott7a8ad102009-12-07 12:51:39 +01001075 spin_lock_irq(cdev->ccwlock);
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001076 sch_set_cdev(sch, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 cdev->private->state = DEV_STATE_NOT_OPER;
Sebastian Ott7a8ad102009-12-07 12:51:39 +01001078 spin_unlock_irq(cdev->ccwlock);
Cornelia Huckef995162007-04-27 16:01:39 +02001079 ccw_device_unregister(cdev);
Peter Oberparleiter48e4c382009-12-07 12:51:15 +01001080out_free:
Cornelia Huckcd6b4f22008-01-26 14:10:43 +01001081 kfree(sch->private);
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001082 sysfs_remove_group(&sch->dev.kobj, &io_subchannel_attr_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 return 0;
1084}
1085
Cornelia Huck602b20f2008-01-26 14:10:39 +01001086static void io_subchannel_verify(struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087{
1088 struct ccw_device *cdev;
1089
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001090 cdev = sch_get_cdev(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 if (cdev)
1092 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1093}
1094
Cornelia Huckc820de32008-07-14 09:58:45 +02001095static void io_subchannel_terminate_path(struct subchannel *sch, u8 mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096{
1097 struct ccw_device *cdev;
1098
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001099 cdev = sch_get_cdev(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 if (!cdev)
1101 return;
Peter Oberparleiter4257aae2009-12-07 12:51:29 +01001102 if (cio_update_schib(sch))
1103 goto err;
1104 /* Check for I/O on path. */
1105 if (scsw_actl(&sch->schib.scsw) == 0 || sch->schib.pmcw.lpum != mask)
1106 goto out;
1107 if (cdev->private->state == DEV_STATE_ONLINE) {
1108 ccw_device_kill_io(cdev);
1109 goto out;
1110 }
1111 if (cio_clear(sch))
1112 goto err;
1113out:
1114 /* Trigger path verification. */
1115 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1116 return;
Cornelia Huckc820de32008-07-14 09:58:45 +02001117
Peter Oberparleiter4257aae2009-12-07 12:51:29 +01001118err:
1119 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
Cornelia Huckc820de32008-07-14 09:58:45 +02001120}
1121
Cornelia Huck99611f82008-07-14 09:59:02 +02001122static int io_subchannel_chp_event(struct subchannel *sch,
1123 struct chp_link *link, int event)
Cornelia Huckc820de32008-07-14 09:58:45 +02001124{
1125 int mask;
Cornelia Huckc820de32008-07-14 09:58:45 +02001126
Cornelia Huck99611f82008-07-14 09:59:02 +02001127 mask = chp_ssd_get_mask(&sch->ssd_info, link);
Cornelia Huckc820de32008-07-14 09:58:45 +02001128 if (!mask)
1129 return 0;
1130 switch (event) {
1131 case CHP_VARY_OFF:
1132 sch->opm &= ~mask;
1133 sch->lpm &= ~mask;
1134 io_subchannel_terminate_path(sch, mask);
1135 break;
1136 case CHP_VARY_ON:
1137 sch->opm |= mask;
1138 sch->lpm |= mask;
1139 io_subchannel_verify(sch);
1140 break;
1141 case CHP_OFFLINE:
Sebastian Ottcdb912a2008-12-25 13:39:12 +01001142 if (cio_update_schib(sch))
Cornelia Huckc820de32008-07-14 09:58:45 +02001143 return -ENODEV;
1144 io_subchannel_terminate_path(sch, mask);
1145 break;
1146 case CHP_ONLINE:
Sebastian Ottcdb912a2008-12-25 13:39:12 +01001147 if (cio_update_schib(sch))
1148 return -ENODEV;
Cornelia Huckc820de32008-07-14 09:58:45 +02001149 sch->lpm |= mask & sch->opm;
1150 io_subchannel_verify(sch);
1151 break;
1152 }
1153 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154}
1155
Sebastian Ott6e9a0f62009-12-07 12:51:38 +01001156static void io_subchannel_quiesce(struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 struct ccw_device *cdev;
1159 int ret;
1160
Sebastian Ott56e6b792009-12-07 12:51:35 +01001161 spin_lock_irq(sch->lock);
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001162 cdev = sch_get_cdev(sch);
Cornelia Hucka8237fc2006-01-06 00:19:21 -08001163 if (cio_is_console(sch->schid))
Sebastian Ott56e6b792009-12-07 12:51:35 +01001164 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 if (!sch->schib.pmcw.ena)
Sebastian Ott56e6b792009-12-07 12:51:35 +01001166 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 ret = cio_disable_subchannel(sch);
1168 if (ret != -EBUSY)
Sebastian Ott56e6b792009-12-07 12:51:35 +01001169 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 if (cdev->handler)
Sebastian Ott56e6b792009-12-07 12:51:35 +01001171 cdev->handler(cdev, cdev->private->intparm, ERR_PTR(-EIO));
1172 while (ret == -EBUSY) {
1173 cdev->private->state = DEV_STATE_QUIESCE;
1174 ret = ccw_device_cancel_halt_clear(cdev);
1175 if (ret == -EBUSY) {
1176 ccw_device_set_timeout(cdev, HZ/10);
1177 spin_unlock_irq(sch->lock);
1178 wait_event(cdev->private->wait_q,
1179 cdev->private->state != DEV_STATE_QUIESCE);
1180 spin_lock_irq(sch->lock);
1181 }
1182 ret = cio_disable_subchannel(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 }
Sebastian Ott56e6b792009-12-07 12:51:35 +01001184out_unlock:
1185 spin_unlock_irq(sch->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186}
1187
Sebastian Ott6e9a0f62009-12-07 12:51:38 +01001188static void io_subchannel_shutdown(struct subchannel *sch)
1189{
1190 io_subchannel_quiesce(sch);
1191}
1192
Cornelia Huckc820de32008-07-14 09:58:45 +02001193static int device_is_disconnected(struct ccw_device *cdev)
1194{
1195 if (!cdev)
1196 return 0;
1197 return (cdev->private->state == DEV_STATE_DISCONNECTED ||
1198 cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID);
1199}
1200
1201static int recovery_check(struct device *dev, void *data)
1202{
1203 struct ccw_device *cdev = to_ccwdev(dev);
1204 int *redo = data;
1205
1206 spin_lock_irq(cdev->ccwlock);
1207 switch (cdev->private->state) {
1208 case DEV_STATE_DISCONNECTED:
1209 CIO_MSG_EVENT(3, "recovery: trigger 0.%x.%04x\n",
1210 cdev->private->dev_id.ssid,
1211 cdev->private->dev_id.devno);
1212 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1213 *redo = 1;
1214 break;
1215 case DEV_STATE_DISCONNECTED_SENSE_ID:
1216 *redo = 1;
1217 break;
1218 }
1219 spin_unlock_irq(cdev->ccwlock);
1220
1221 return 0;
1222}
1223
1224static void recovery_work_func(struct work_struct *unused)
1225{
1226 int redo = 0;
1227
1228 bus_for_each_dev(&ccw_bus_type, NULL, &redo, recovery_check);
1229 if (redo) {
1230 spin_lock_irq(&recovery_lock);
1231 if (!timer_pending(&recovery_timer)) {
1232 if (recovery_phase < ARRAY_SIZE(recovery_delay) - 1)
1233 recovery_phase++;
1234 mod_timer(&recovery_timer, jiffies +
1235 recovery_delay[recovery_phase] * HZ);
1236 }
1237 spin_unlock_irq(&recovery_lock);
1238 } else
1239 CIO_MSG_EVENT(4, "recovery: end\n");
1240}
1241
1242static DECLARE_WORK(recovery_work, recovery_work_func);
1243
1244static void recovery_func(unsigned long data)
1245{
1246 /*
1247 * We can't do our recovery in softirq context and it's not
1248 * performance critical, so we schedule it.
1249 */
1250 schedule_work(&recovery_work);
1251}
1252
1253static void ccw_device_schedule_recovery(void)
1254{
1255 unsigned long flags;
1256
1257 CIO_MSG_EVENT(4, "recovery: schedule\n");
1258 spin_lock_irqsave(&recovery_lock, flags);
1259 if (!timer_pending(&recovery_timer) || (recovery_phase != 0)) {
1260 recovery_phase = 0;
1261 mod_timer(&recovery_timer, jiffies + recovery_delay[0] * HZ);
1262 }
1263 spin_unlock_irqrestore(&recovery_lock, flags);
1264}
1265
Peter Oberparleiterecf5d9e2008-10-10 21:33:06 +02001266static int purge_fn(struct device *dev, void *data)
1267{
1268 struct ccw_device *cdev = to_ccwdev(dev);
Peter Oberparleiter37de53b2009-12-07 12:51:19 +01001269 struct ccw_dev_id *id = &cdev->private->dev_id;
Peter Oberparleiterecf5d9e2008-10-10 21:33:06 +02001270
1271 spin_lock_irq(cdev->ccwlock);
Peter Oberparleiter37de53b2009-12-07 12:51:19 +01001272 if (is_blacklisted(id->ssid, id->devno) &&
1273 (cdev->private->state == DEV_STATE_OFFLINE)) {
1274 CIO_MSG_EVENT(3, "ccw: purging 0.%x.%04x\n", id->ssid,
1275 id->devno);
1276 ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
1277 }
Peter Oberparleiterecf5d9e2008-10-10 21:33:06 +02001278 spin_unlock_irq(cdev->ccwlock);
Peter Oberparleiterecf5d9e2008-10-10 21:33:06 +02001279 /* Abort loop in case of pending signal. */
1280 if (signal_pending(current))
1281 return -EINTR;
1282
1283 return 0;
1284}
1285
1286/**
1287 * ccw_purge_blacklisted - purge unused, blacklisted devices
1288 *
1289 * Unregister all ccw devices that are offline and on the blacklist.
1290 */
1291int ccw_purge_blacklisted(void)
1292{
1293 CIO_MSG_EVENT(2, "ccw: purging blacklisted devices\n");
1294 bus_for_each_dev(&ccw_bus_type, NULL, NULL, purge_fn);
1295 return 0;
1296}
1297
Peter Oberparleiter6afcc772009-10-06 10:34:02 +02001298void ccw_device_set_disconnected(struct ccw_device *cdev)
Cornelia Huckc820de32008-07-14 09:58:45 +02001299{
1300 if (!cdev)
1301 return;
1302 ccw_device_set_timeout(cdev, 0);
1303 cdev->private->flags.fake_irb = 0;
1304 cdev->private->state = DEV_STATE_DISCONNECTED;
1305 if (cdev->online)
1306 ccw_device_schedule_recovery();
1307}
1308
Peter Oberparleiter91c36912008-08-21 19:46:39 +02001309void ccw_device_set_notoper(struct ccw_device *cdev)
1310{
1311 struct subchannel *sch = to_subchannel(cdev->dev.parent);
1312
1313 CIO_TRACE_EVENT(2, "notoper");
Cornelia Huckb9d3aed2008-10-10 21:33:11 +02001314 CIO_TRACE_EVENT(2, dev_name(&sch->dev));
Peter Oberparleiter91c36912008-08-21 19:46:39 +02001315 ccw_device_set_timeout(cdev, 0);
1316 cio_disable_subchannel(sch);
1317 cdev->private->state = DEV_STATE_NOT_OPER;
1318}
1319
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001320enum io_sch_action {
1321 IO_SCH_UNREG,
1322 IO_SCH_ORPH_UNREG,
1323 IO_SCH_ATTACH,
1324 IO_SCH_UNREG_ATTACH,
1325 IO_SCH_ORPH_ATTACH,
1326 IO_SCH_REPROBE,
1327 IO_SCH_VERIFY,
1328 IO_SCH_DISC,
1329 IO_SCH_NOP,
1330};
1331
1332static enum io_sch_action sch_get_action(struct subchannel *sch)
Cornelia Huckc820de32008-07-14 09:58:45 +02001333{
Cornelia Huckc820de32008-07-14 09:58:45 +02001334 struct ccw_device *cdev;
1335
Cornelia Huckc820de32008-07-14 09:58:45 +02001336 cdev = sch_get_cdev(sch);
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001337 if (cio_update_schib(sch)) {
1338 /* Not operational. */
1339 if (!cdev)
1340 return IO_SCH_UNREG;
1341 if (!ccw_device_notify(cdev, CIO_GONE))
1342 return IO_SCH_UNREG;
1343 return IO_SCH_ORPH_UNREG;
Cornelia Huckc820de32008-07-14 09:58:45 +02001344 }
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001345 /* Operational. */
1346 if (!cdev)
1347 return IO_SCH_ATTACH;
1348 if (sch->schib.pmcw.dev != cdev->private->dev_id.devno) {
1349 if (!ccw_device_notify(cdev, CIO_GONE))
1350 return IO_SCH_UNREG_ATTACH;
1351 return IO_SCH_ORPH_ATTACH;
Cornelia Huckc820de32008-07-14 09:58:45 +02001352 }
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001353 if ((sch->schib.pmcw.pam & sch->opm) == 0) {
1354 if (!ccw_device_notify(cdev, CIO_NO_PATH))
1355 return IO_SCH_UNREG;
1356 return IO_SCH_DISC;
Cornelia Huckc820de32008-07-14 09:58:45 +02001357 }
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001358 if (device_is_disconnected(cdev))
1359 return IO_SCH_REPROBE;
1360 if (cdev->online)
1361 return IO_SCH_VERIFY;
1362 return IO_SCH_NOP;
1363}
1364
1365/**
1366 * io_subchannel_sch_event - process subchannel event
1367 * @sch: subchannel
1368 * @process: non-zero if function is called in process context
1369 *
1370 * An unspecified event occurred for this subchannel. Adjust data according
1371 * to the current operational state of the subchannel and device. Return
1372 * zero when the event has been handled sufficiently or -EAGAIN when this
1373 * function should be called again in process context.
1374 */
1375static int io_subchannel_sch_event(struct subchannel *sch, int process)
1376{
1377 unsigned long flags;
1378 struct ccw_device *cdev;
1379 struct ccw_dev_id dev_id;
1380 enum io_sch_action action;
1381 int rc = -EAGAIN;
1382
1383 spin_lock_irqsave(sch->lock, flags);
1384 if (!device_is_registered(&sch->dev))
1385 goto out_unlock;
Peter Oberparleiter390935a2009-12-07 12:51:18 +01001386 if (work_pending(&sch->todo_work))
1387 goto out_unlock;
Peter Oberparleiter37de53b2009-12-07 12:51:19 +01001388 cdev = sch_get_cdev(sch);
1389 if (cdev && work_pending(&cdev->private->todo_work))
1390 goto out_unlock;
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001391 action = sch_get_action(sch);
1392 CIO_MSG_EVENT(2, "event: sch 0.%x.%04x, process=%d, action=%d\n",
1393 sch->schid.ssid, sch->schid.sch_no, process,
1394 action);
1395 /* Perform immediate actions while holding the lock. */
Cornelia Huckc820de32008-07-14 09:58:45 +02001396 switch (action) {
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001397 case IO_SCH_REPROBE:
1398 /* Trigger device recognition. */
Cornelia Huckc820de32008-07-14 09:58:45 +02001399 ccw_device_trigger_reprobe(cdev);
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001400 rc = 0;
1401 goto out_unlock;
1402 case IO_SCH_VERIFY:
1403 /* Trigger path verification. */
1404 io_subchannel_verify(sch);
1405 rc = 0;
1406 goto out_unlock;
1407 case IO_SCH_DISC:
1408 ccw_device_set_disconnected(cdev);
1409 rc = 0;
1410 goto out_unlock;
1411 case IO_SCH_ORPH_UNREG:
1412 case IO_SCH_ORPH_ATTACH:
Peter Oberparleiter6afcc772009-10-06 10:34:02 +02001413 ccw_device_set_disconnected(cdev);
Peter Oberparleiter91c36912008-08-21 19:46:39 +02001414 break;
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001415 case IO_SCH_UNREG_ATTACH:
1416 case IO_SCH_UNREG:
1417 if (cdev)
1418 ccw_device_set_notoper(cdev);
1419 break;
1420 case IO_SCH_NOP:
1421 rc = 0;
1422 goto out_unlock;
Cornelia Huckc820de32008-07-14 09:58:45 +02001423 default:
1424 break;
1425 }
1426 spin_unlock_irqrestore(sch->lock, flags);
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001427 /* All other actions require process context. */
1428 if (!process)
1429 goto out;
1430 /* Handle attached ccw device. */
1431 switch (action) {
1432 case IO_SCH_ORPH_UNREG:
1433 case IO_SCH_ORPH_ATTACH:
1434 /* Move ccw device to orphanage. */
1435 rc = ccw_device_move_to_orph(cdev);
1436 if (rc)
1437 goto out;
1438 break;
1439 case IO_SCH_UNREG_ATTACH:
1440 /* Unregister ccw device. */
1441 ccw_device_unregister(cdev);
1442 break;
1443 default:
1444 break;
1445 }
1446 /* Handle subchannel. */
1447 switch (action) {
1448 case IO_SCH_ORPH_UNREG:
1449 case IO_SCH_UNREG:
1450 css_sch_device_unregister(sch);
1451 break;
1452 case IO_SCH_ORPH_ATTACH:
1453 case IO_SCH_UNREG_ATTACH:
1454 case IO_SCH_ATTACH:
1455 dev_id.ssid = sch->schid.ssid;
1456 dev_id.devno = sch->schib.pmcw.dev;
1457 cdev = get_ccwdev_by_dev_id(&dev_id);
1458 if (!cdev) {
1459 sch_create_and_recog_new_device(sch);
1460 break;
1461 }
1462 rc = ccw_device_move_to_sch(cdev, sch);
1463 if (rc) {
1464 /* Release reference from get_ccwdev_by_dev_id() */
1465 put_device(&cdev->dev);
1466 goto out;
1467 }
1468 spin_lock_irqsave(sch->lock, flags);
1469 ccw_device_trigger_reprobe(cdev);
1470 spin_unlock_irqrestore(sch->lock, flags);
1471 /* Release reference from get_ccwdev_by_dev_id() */
1472 put_device(&cdev->dev);
1473 break;
1474 default:
1475 break;
1476 }
1477 return 0;
Cornelia Huckc820de32008-07-14 09:58:45 +02001478
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001479out_unlock:
1480 spin_unlock_irqrestore(sch->lock, flags);
1481out:
1482 return rc;
Cornelia Huckc820de32008-07-14 09:58:45 +02001483}
1484
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485#ifdef CONFIG_CCW_CONSOLE
1486static struct ccw_device console_cdev;
1487static struct ccw_device_private console_private;
1488static int console_cdev_in_use;
1489
Cornelia Huck2ec22982006-12-08 15:54:26 +01001490static DEFINE_SPINLOCK(ccw_console_lock);
1491
1492spinlock_t * cio_get_console_lock(void)
1493{
1494 return &ccw_console_lock;
1495}
1496
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001497static int ccw_device_console_enable(struct ccw_device *cdev,
1498 struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499{
1500 int rc;
1501
Cornelia Huckcd6b4f22008-01-26 14:10:43 +01001502 /* Attach subchannel private data. */
1503 sch->private = cio_get_console_priv();
1504 memset(sch->private, 0, sizeof(struct io_subchannel_private));
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001505 io_subchannel_init_fields(sch);
Sebastian Ottf444cc02008-12-25 13:39:14 +01001506 rc = cio_commit_config(sch);
1507 if (rc)
1508 return rc;
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001509 sch->driver = &io_subchannel_driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 /* Initialize the ccw_device structure. */
Heiko Carstens292888c2006-08-30 14:33:35 +02001511 cdev->dev.parent= &sch->dev;
Sebastian Ottffa8d2a32009-12-18 17:43:15 +01001512 sch_set_cdev(sch, cdev);
Peter Oberparleiter736b5db2009-12-07 12:51:21 +01001513 io_subchannel_recog(cdev, sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 /* Now wait for the async. recognition to come to an end. */
1515 spin_lock_irq(cdev->ccwlock);
1516 while (!dev_fsm_final_state(cdev))
1517 wait_cons_dev();
1518 rc = -EIO;
1519 if (cdev->private->state != DEV_STATE_OFFLINE)
1520 goto out_unlock;
1521 ccw_device_online(cdev);
1522 while (!dev_fsm_final_state(cdev))
1523 wait_cons_dev();
1524 if (cdev->private->state != DEV_STATE_ONLINE)
1525 goto out_unlock;
1526 rc = 0;
1527out_unlock:
1528 spin_unlock_irq(cdev->ccwlock);
Peter Oberparleiter736b5db2009-12-07 12:51:21 +01001529 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530}
1531
1532struct ccw_device *
1533ccw_device_probe_console(void)
1534{
1535 struct subchannel *sch;
1536 int ret;
1537
1538 if (xchg(&console_cdev_in_use, 1) != 0)
Peter Oberparleiter600b5d12006-02-01 03:06:35 -08001539 return ERR_PTR(-EBUSY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 sch = cio_probe_console();
1541 if (IS_ERR(sch)) {
1542 console_cdev_in_use = 0;
1543 return (void *) sch;
1544 }
1545 memset(&console_cdev, 0, sizeof(struct ccw_device));
1546 memset(&console_private, 0, sizeof(struct ccw_device_private));
1547 console_cdev.private = &console_private;
Martin Schwidefskyc1637532006-12-08 15:53:57 +01001548 console_private.cdev = &console_cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 ret = ccw_device_console_enable(&console_cdev, sch);
1550 if (ret) {
1551 cio_release_console();
1552 console_cdev_in_use = 0;
1553 return ERR_PTR(ret);
1554 }
1555 console_cdev.online = 1;
1556 return &console_cdev;
1557}
Cornelia Huck1f4e7ed2008-10-10 21:33:14 +02001558
Martin Schwidefsky66648452009-06-16 10:30:28 +02001559static int ccw_device_pm_restore(struct device *dev);
1560
1561int ccw_device_force_console(void)
1562{
1563 if (!console_cdev_in_use)
1564 return -ENODEV;
1565 return ccw_device_pm_restore(&console_cdev.dev);
1566}
1567EXPORT_SYMBOL_GPL(ccw_device_force_console);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568#endif
1569
1570/*
1571 * get ccw_device matching the busid, but only if owned by cdrv
1572 */
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001573static int
1574__ccwdev_check_busid(struct device *dev, void *id)
1575{
1576 char *bus_id;
1577
Cornelia Huck12975ae2006-10-11 15:31:47 +02001578 bus_id = id;
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001579
Kay Sievers98df67b2008-12-25 13:38:55 +01001580 return (strcmp(bus_id, dev_name(dev)) == 0);
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001581}
1582
1583
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +02001584/**
1585 * get_ccwdev_by_busid() - obtain device from a bus id
1586 * @cdrv: driver the device is owned by
1587 * @bus_id: bus id of the device to be searched
1588 *
1589 * This function searches all devices owned by @cdrv for a device with a bus
1590 * id matching @bus_id.
1591 * Returns:
1592 * If a match is found, its reference count of the found device is increased
1593 * and it is returned; else %NULL is returned.
1594 */
1595struct ccw_device *get_ccwdev_by_busid(struct ccw_driver *cdrv,
1596 const char *bus_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597{
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001598 struct device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 struct device_driver *drv;
1600
1601 drv = get_driver(&cdrv->driver);
1602 if (!drv)
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001603 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001605 dev = driver_find_device(drv, NULL, (void *)bus_id,
1606 __ccwdev_check_busid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 put_driver(drv);
1608
Heiko Carstensd2c993d2006-07-12 16:41:55 +02001609 return dev ? to_ccwdev(dev) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610}
1611
1612/************************** device driver handling ************************/
1613
1614/* This is the implementation of the ccw_driver class. The probe, remove
1615 * and release methods are initially very similar to the device_driver
1616 * implementations, with the difference that they have ccw_device
1617 * arguments.
1618 *
1619 * A ccw driver also contains the information that is needed for
1620 * device matching.
1621 */
1622static int
1623ccw_device_probe (struct device *dev)
1624{
1625 struct ccw_device *cdev = to_ccwdev(dev);
1626 struct ccw_driver *cdrv = to_ccwdrv(dev->driver);
1627 int ret;
1628
1629 cdev->drv = cdrv; /* to let the driver call _set_online */
1630
1631 ret = cdrv->probe ? cdrv->probe(cdev) : -ENODEV;
1632
1633 if (ret) {
Heiko Carstensd2c993d2006-07-12 16:41:55 +02001634 cdev->drv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 return ret;
1636 }
1637
1638 return 0;
1639}
1640
1641static int
1642ccw_device_remove (struct device *dev)
1643{
1644 struct ccw_device *cdev = to_ccwdev(dev);
1645 struct ccw_driver *cdrv = cdev->drv;
1646 int ret;
1647
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 if (cdrv->remove)
1649 cdrv->remove(cdev);
1650 if (cdev->online) {
1651 cdev->online = 0;
1652 spin_lock_irq(cdev->ccwlock);
1653 ret = ccw_device_offline(cdev);
1654 spin_unlock_irq(cdev->ccwlock);
1655 if (ret == 0)
1656 wait_event(cdev->private->wait_q,
1657 dev_fsm_final_state(cdev));
1658 else
Michael Ernst139b83d2008-05-07 09:22:54 +02001659 CIO_MSG_EVENT(0, "ccw_device_offline returned %d, "
Cornelia Hucke556bbb2007-07-27 12:29:19 +02001660 "device 0.%x.%04x\n",
1661 ret, cdev->private->dev_id.ssid,
1662 cdev->private->dev_id.devno);
Cornelia Huck9cd67422008-12-25 13:39:06 +01001663 /* Give up reference obtained in ccw_device_set_online(). */
1664 put_device(&cdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 }
1666 ccw_device_set_timeout(cdev, 0);
Heiko Carstensd2c993d2006-07-12 16:41:55 +02001667 cdev->drv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 return 0;
1669}
1670
Cornelia Huck958974f2007-10-12 16:11:21 +02001671static void ccw_device_shutdown(struct device *dev)
1672{
1673 struct ccw_device *cdev;
1674
1675 cdev = to_ccwdev(dev);
1676 if (cdev->drv && cdev->drv->shutdown)
1677 cdev->drv->shutdown(cdev);
Cornelia Huck1842f2b2007-10-12 16:11:22 +02001678 disable_cmf(cdev);
Cornelia Huck958974f2007-10-12 16:11:21 +02001679}
1680
Sebastian Ott823d4942009-06-16 10:30:20 +02001681static int ccw_device_pm_prepare(struct device *dev)
1682{
1683 struct ccw_device *cdev = to_ccwdev(dev);
1684
Peter Oberparleiter37de53b2009-12-07 12:51:19 +01001685 if (work_pending(&cdev->private->todo_work))
Sebastian Ott823d4942009-06-16 10:30:20 +02001686 return -EAGAIN;
1687 /* Fail while device is being set online/offline. */
1688 if (atomic_read(&cdev->private->onoff))
1689 return -EAGAIN;
1690
1691 if (cdev->online && cdev->drv && cdev->drv->prepare)
1692 return cdev->drv->prepare(cdev);
1693
1694 return 0;
1695}
1696
1697static void ccw_device_pm_complete(struct device *dev)
1698{
1699 struct ccw_device *cdev = to_ccwdev(dev);
1700
1701 if (cdev->online && cdev->drv && cdev->drv->complete)
1702 cdev->drv->complete(cdev);
1703}
1704
1705static int ccw_device_pm_freeze(struct device *dev)
1706{
1707 struct ccw_device *cdev = to_ccwdev(dev);
1708 struct subchannel *sch = to_subchannel(cdev->dev.parent);
1709 int ret, cm_enabled;
1710
1711 /* Fail suspend while device is in transistional state. */
1712 if (!dev_fsm_final_state(cdev))
1713 return -EAGAIN;
1714 if (!cdev->online)
1715 return 0;
1716 if (cdev->drv && cdev->drv->freeze) {
1717 ret = cdev->drv->freeze(cdev);
1718 if (ret)
1719 return ret;
1720 }
1721
1722 spin_lock_irq(sch->lock);
1723 cm_enabled = cdev->private->cmb != NULL;
1724 spin_unlock_irq(sch->lock);
1725 if (cm_enabled) {
1726 /* Don't have the css write on memory. */
1727 ret = ccw_set_cmf(cdev, 0);
1728 if (ret)
1729 return ret;
1730 }
1731 /* From here on, disallow device driver I/O. */
1732 spin_lock_irq(sch->lock);
1733 ret = cio_disable_subchannel(sch);
1734 spin_unlock_irq(sch->lock);
1735
1736 return ret;
1737}
1738
1739static int ccw_device_pm_thaw(struct device *dev)
1740{
1741 struct ccw_device *cdev = to_ccwdev(dev);
1742 struct subchannel *sch = to_subchannel(cdev->dev.parent);
1743 int ret, cm_enabled;
1744
1745 if (!cdev->online)
1746 return 0;
1747
1748 spin_lock_irq(sch->lock);
1749 /* Allow device driver I/O again. */
1750 ret = cio_enable_subchannel(sch, (u32)(addr_t)sch);
1751 cm_enabled = cdev->private->cmb != NULL;
1752 spin_unlock_irq(sch->lock);
1753 if (ret)
1754 return ret;
1755
1756 if (cm_enabled) {
1757 ret = ccw_set_cmf(cdev, 1);
1758 if (ret)
1759 return ret;
1760 }
1761
1762 if (cdev->drv && cdev->drv->thaw)
1763 ret = cdev->drv->thaw(cdev);
1764
1765 return ret;
1766}
1767
1768static void __ccw_device_pm_restore(struct ccw_device *cdev)
1769{
1770 struct subchannel *sch = to_subchannel(cdev->dev.parent);
Sebastian Ott823d4942009-06-16 10:30:20 +02001771
1772 if (cio_is_console(sch->schid))
1773 goto out;
1774 /*
1775 * While we were sleeping, devices may have gone or become
1776 * available again. Kick re-detection.
1777 */
1778 spin_lock_irq(sch->lock);
1779 cdev->private->flags.resuming = 1;
Peter Oberparleiter736b5db2009-12-07 12:51:21 +01001780 ccw_device_recognition(cdev);
Sebastian Ott823d4942009-06-16 10:30:20 +02001781 spin_unlock_irq(sch->lock);
Sebastian Ott823d4942009-06-16 10:30:20 +02001782 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev) ||
1783 cdev->private->state == DEV_STATE_DISCONNECTED);
Sebastian Ott823d4942009-06-16 10:30:20 +02001784out:
1785 cdev->private->flags.resuming = 0;
1786}
1787
1788static int resume_handle_boxed(struct ccw_device *cdev)
1789{
1790 cdev->private->state = DEV_STATE_BOXED;
1791 if (ccw_device_notify(cdev, CIO_BOXED))
1792 return 0;
Peter Oberparleiter37de53b2009-12-07 12:51:19 +01001793 ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
Sebastian Ott823d4942009-06-16 10:30:20 +02001794 return -ENODEV;
1795}
1796
1797static int resume_handle_disc(struct ccw_device *cdev)
1798{
1799 cdev->private->state = DEV_STATE_DISCONNECTED;
1800 if (ccw_device_notify(cdev, CIO_GONE))
1801 return 0;
Peter Oberparleiter37de53b2009-12-07 12:51:19 +01001802 ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
Sebastian Ott823d4942009-06-16 10:30:20 +02001803 return -ENODEV;
1804}
1805
1806static int ccw_device_pm_restore(struct device *dev)
1807{
1808 struct ccw_device *cdev = to_ccwdev(dev);
1809 struct subchannel *sch = to_subchannel(cdev->dev.parent);
1810 int ret = 0, cm_enabled;
1811
1812 __ccw_device_pm_restore(cdev);
1813 spin_lock_irq(sch->lock);
1814 if (cio_is_console(sch->schid)) {
1815 cio_enable_subchannel(sch, (u32)(addr_t)sch);
1816 spin_unlock_irq(sch->lock);
1817 goto out_restore;
1818 }
1819 cdev->private->flags.donotify = 0;
1820 /* check recognition results */
1821 switch (cdev->private->state) {
1822 case DEV_STATE_OFFLINE:
1823 break;
1824 case DEV_STATE_BOXED:
1825 ret = resume_handle_boxed(cdev);
1826 spin_unlock_irq(sch->lock);
1827 if (ret)
1828 goto out;
1829 goto out_restore;
1830 case DEV_STATE_DISCONNECTED:
1831 goto out_disc_unlock;
1832 default:
1833 goto out_unreg_unlock;
1834 }
1835 /* check if the device id has changed */
1836 if (sch->schib.pmcw.dev != cdev->private->dev_id.devno) {
Sebastian Ottf0148242009-09-11 10:28:23 +02001837 CIO_MSG_EVENT(0, "resume: sch 0.%x.%04x: failed (devno "
1838 "changed from %04x to %04x)\n",
1839 sch->schid.ssid, sch->schid.sch_no,
Sebastian Ott823d4942009-06-16 10:30:20 +02001840 cdev->private->dev_id.devno,
1841 sch->schib.pmcw.dev);
1842 goto out_unreg_unlock;
1843 }
1844 /* check if the device type has changed */
1845 if (!ccw_device_test_sense_data(cdev)) {
1846 ccw_device_update_sense_data(cdev);
Peter Oberparleiter37de53b2009-12-07 12:51:19 +01001847 ccw_device_sched_todo(cdev, CDEV_TODO_REBIND);
Sebastian Ott823d4942009-06-16 10:30:20 +02001848 ret = -ENODEV;
1849 goto out_unlock;
1850 }
1851 if (!cdev->online) {
1852 ret = 0;
1853 goto out_unlock;
1854 }
1855 ret = ccw_device_online(cdev);
1856 if (ret)
1857 goto out_disc_unlock;
1858
1859 cm_enabled = cdev->private->cmb != NULL;
1860 spin_unlock_irq(sch->lock);
1861
1862 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
1863 if (cdev->private->state != DEV_STATE_ONLINE) {
1864 spin_lock_irq(sch->lock);
1865 goto out_disc_unlock;
1866 }
1867 if (cm_enabled) {
1868 ret = ccw_set_cmf(cdev, 1);
1869 if (ret) {
Sebastian Ottf0148242009-09-11 10:28:23 +02001870 CIO_MSG_EVENT(2, "resume: cdev 0.%x.%04x: cmf failed "
1871 "(rc=%d)\n", cdev->private->dev_id.ssid,
1872 cdev->private->dev_id.devno, ret);
Sebastian Ott823d4942009-06-16 10:30:20 +02001873 ret = 0;
1874 }
1875 }
1876
1877out_restore:
1878 if (cdev->online && cdev->drv && cdev->drv->restore)
1879 ret = cdev->drv->restore(cdev);
1880out:
1881 return ret;
1882
1883out_disc_unlock:
1884 ret = resume_handle_disc(cdev);
1885 spin_unlock_irq(sch->lock);
1886 if (ret)
1887 return ret;
1888 goto out_restore;
1889
1890out_unreg_unlock:
Peter Oberparleiter37de53b2009-12-07 12:51:19 +01001891 ccw_device_sched_todo(cdev, CDEV_TODO_UNREG_EVAL);
Sebastian Ott823d4942009-06-16 10:30:20 +02001892 ret = -ENODEV;
1893out_unlock:
1894 spin_unlock_irq(sch->lock);
1895 return ret;
1896}
1897
Alexey Dobriyan47145212009-12-14 18:00:08 -08001898static const struct dev_pm_ops ccw_pm_ops = {
Sebastian Ott823d4942009-06-16 10:30:20 +02001899 .prepare = ccw_device_pm_prepare,
1900 .complete = ccw_device_pm_complete,
1901 .freeze = ccw_device_pm_freeze,
1902 .thaw = ccw_device_pm_thaw,
1903 .restore = ccw_device_pm_restore,
1904};
1905
Cornelia Huck8bbace72006-01-11 10:56:22 +01001906struct bus_type ccw_bus_type = {
1907 .name = "ccw",
1908 .match = ccw_bus_match,
1909 .uevent = ccw_uevent,
1910 .probe = ccw_device_probe,
1911 .remove = ccw_device_remove,
Cornelia Huck958974f2007-10-12 16:11:21 +02001912 .shutdown = ccw_device_shutdown,
Sebastian Ott823d4942009-06-16 10:30:20 +02001913 .pm = &ccw_pm_ops,
Cornelia Huck8bbace72006-01-11 10:56:22 +01001914};
1915
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +02001916/**
1917 * ccw_driver_register() - register a ccw driver
1918 * @cdriver: driver to be registered
1919 *
1920 * This function is mainly a wrapper around driver_register().
1921 * Returns:
1922 * %0 on success and a negative error value on failure.
1923 */
1924int ccw_driver_register(struct ccw_driver *cdriver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925{
1926 struct device_driver *drv = &cdriver->driver;
1927
1928 drv->bus = &ccw_bus_type;
1929 drv->name = cdriver->name;
Cornelia Huck4beee642008-01-26 14:10:47 +01001930 drv->owner = cdriver->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931
1932 return driver_register(drv);
1933}
1934
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +02001935/**
1936 * ccw_driver_unregister() - deregister a ccw driver
1937 * @cdriver: driver to be deregistered
1938 *
1939 * This function is mainly a wrapper around driver_unregister().
1940 */
1941void ccw_driver_unregister(struct ccw_driver *cdriver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942{
1943 driver_unregister(&cdriver->driver);
1944}
1945
Cornelia Hucka8237fc2006-01-06 00:19:21 -08001946/* Helper func for qdio. */
1947struct subchannel_id
1948ccw_device_get_subchannel_id(struct ccw_device *cdev)
1949{
1950 struct subchannel *sch;
1951
1952 sch = to_subchannel(cdev->dev.parent);
1953 return sch->schid;
1954}
1955
Peter Oberparleiter37de53b2009-12-07 12:51:19 +01001956static void ccw_device_todo(struct work_struct *work)
1957{
1958 struct ccw_device_private *priv;
1959 struct ccw_device *cdev;
1960 struct subchannel *sch;
1961 enum cdev_todo todo;
1962
1963 priv = container_of(work, struct ccw_device_private, todo_work);
1964 cdev = priv->cdev;
1965 sch = to_subchannel(cdev->dev.parent);
1966 /* Find out todo. */
1967 spin_lock_irq(cdev->ccwlock);
1968 todo = priv->todo;
1969 priv->todo = CDEV_TODO_NOTHING;
1970 CIO_MSG_EVENT(4, "cdev_todo: cdev=0.%x.%04x todo=%d\n",
1971 priv->dev_id.ssid, priv->dev_id.devno, todo);
1972 spin_unlock_irq(cdev->ccwlock);
1973 /* Perform todo. */
1974 switch (todo) {
1975 case CDEV_TODO_ENABLE_CMF:
1976 cmf_reenable(cdev);
1977 break;
1978 case CDEV_TODO_REBIND:
1979 ccw_device_do_unbind_bind(cdev);
1980 break;
1981 case CDEV_TODO_REGISTER:
1982 io_subchannel_register(cdev);
1983 break;
1984 case CDEV_TODO_UNREG_EVAL:
1985 if (!sch_is_pseudo_sch(sch))
1986 css_schedule_eval(sch->schid);
1987 /* fall-through */
1988 case CDEV_TODO_UNREG:
1989 if (sch_is_pseudo_sch(sch))
1990 ccw_device_unregister(cdev);
1991 else
1992 ccw_device_call_sch_unregister(cdev);
1993 break;
1994 default:
1995 break;
1996 }
1997 /* Release workqueue ref. */
1998 put_device(&cdev->dev);
1999}
2000
2001/**
2002 * ccw_device_sched_todo - schedule ccw device operation
2003 * @cdev: ccw device
2004 * @todo: todo
2005 *
2006 * Schedule the operation identified by @todo to be performed on the slow path
2007 * workqueue. Do nothing if another operation with higher priority is already
2008 * scheduled. Needs to be called with ccwdev lock held.
2009 */
2010void ccw_device_sched_todo(struct ccw_device *cdev, enum cdev_todo todo)
2011{
2012 CIO_MSG_EVENT(4, "cdev_todo: sched cdev=0.%x.%04x todo=%d\n",
2013 cdev->private->dev_id.ssid, cdev->private->dev_id.devno,
2014 todo);
2015 if (cdev->private->todo >= todo)
2016 return;
2017 cdev->private->todo = todo;
2018 /* Get workqueue ref. */
2019 if (!get_device(&cdev->dev))
2020 return;
Sebastian Ottbe5d3822010-02-26 22:37:24 +01002021 if (!queue_work(cio_work_q, &cdev->private->todo_work)) {
Peter Oberparleiter37de53b2009-12-07 12:51:19 +01002022 /* Already queued, release workqueue ref. */
2023 put_device(&cdev->dev);
2024 }
2025}
2026
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027MODULE_LICENSE("GPL");
2028EXPORT_SYMBOL(ccw_device_set_online);
2029EXPORT_SYMBOL(ccw_device_set_offline);
2030EXPORT_SYMBOL(ccw_driver_register);
2031EXPORT_SYMBOL(ccw_driver_unregister);
2032EXPORT_SYMBOL(get_ccwdev_by_busid);
2033EXPORT_SYMBOL(ccw_bus_type);
Cornelia Hucka8237fc2006-01-06 00:19:21 -08002034EXPORT_SYMBOL_GPL(ccw_device_get_subchannel_id);