blob: 75acec3b2d6b6416a42b723e43cb017cda02136d [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"
Michael Ernstfd0457a2010-08-09 18:12:50 +020039#include "chsc.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Peter Oberparleiter90ab1332008-01-26 14:10:52 +010041static struct timer_list recovery_timer;
Cornelia Huck486d0a02008-02-19 15:29:23 +010042static DEFINE_SPINLOCK(recovery_lock);
Peter Oberparleiter90ab1332008-01-26 14:10:52 +010043static int recovery_phase;
44static const unsigned long recovery_delay[] = { 3, 30, 300 };
45
Linus Torvalds1da177e2005-04-16 15:20:36 -070046/******************* bus type handling ***********************/
47
48/* The Linux driver model distinguishes between a bus type and
49 * the bus itself. Of course we only have one channel
50 * subsystem driver and one channel system per machine, but
51 * we still use the abstraction. T.R. says it's a good idea. */
52static int
53ccw_bus_match (struct device * dev, struct device_driver * drv)
54{
55 struct ccw_device *cdev = to_ccwdev(dev);
56 struct ccw_driver *cdrv = to_ccwdrv(drv);
57 const struct ccw_device_id *ids = cdrv->ids, *found;
58
59 if (!ids)
60 return 0;
61
62 found = ccw_device_id_match(ids, &cdev->id);
63 if (!found)
64 return 0;
65
66 cdev->id.driver_info = found->driver_info;
67
68 return 1;
69}
70
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +020071/* Store modalias string delimited by prefix/suffix string into buffer with
72 * specified size. Return length of resulting string (excluding trailing '\0')
73 * even if string doesn't fit buffer (snprintf semantics). */
Cornelia Huckcfbe9bb2007-04-27 16:01:32 +020074static int snprint_alias(char *buf, size_t size,
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +020075 struct ccw_device_id *id, const char *suffix)
76{
77 int len;
78
Cornelia Huckcfbe9bb2007-04-27 16:01:32 +020079 len = snprintf(buf, size, "ccw:t%04Xm%02X", id->cu_type, id->cu_model);
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +020080 if (len > size)
81 return len;
82 buf += len;
83 size -= len;
84
85 if (id->dev_type != 0)
86 len += snprintf(buf, size, "dt%04Xdm%02X%s", id->dev_type,
87 id->dev_model, suffix);
88 else
89 len += snprintf(buf, size, "dtdm%s", suffix);
90
91 return len;
92}
93
94/* Set up environment variables for ccw device uevent. Return 0 on success,
95 * non-zero otherwise. */
Kay Sievers7eff2e72007-08-14 15:15:12 +020096static int ccw_uevent(struct device *dev, struct kobj_uevent_env *env)
Linus Torvalds1da177e2005-04-16 15:20:36 -070097{
98 struct ccw_device *cdev = to_ccwdev(dev);
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +020099 struct ccw_device_id *id = &(cdev->id);
Cornelia Huckcfbe9bb2007-04-27 16:01:32 +0200100 int ret;
101 char modalias_buf[30];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +0200103 /* CU_TYPE= */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200104 ret = add_uevent_var(env, "CU_TYPE=%04X", id->cu_type);
Cornelia Huckcfbe9bb2007-04-27 16:01:32 +0200105 if (ret)
106 return ret;
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +0200107
108 /* CU_MODEL= */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200109 ret = add_uevent_var(env, "CU_MODEL=%02X", id->cu_model);
Cornelia Huckcfbe9bb2007-04-27 16:01:32 +0200110 if (ret)
111 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
113 /* The next two can be zero, that's ok for us */
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +0200114 /* DEV_TYPE= */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200115 ret = add_uevent_var(env, "DEV_TYPE=%04X", id->dev_type);
Cornelia Huckcfbe9bb2007-04-27 16:01:32 +0200116 if (ret)
117 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +0200119 /* DEV_MODEL= */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200120 ret = add_uevent_var(env, "DEV_MODEL=%02X", id->dev_model);
Cornelia Huckcfbe9bb2007-04-27 16:01:32 +0200121 if (ret)
122 return ret;
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +0200123
124 /* MODALIAS= */
Cornelia Huckcfbe9bb2007-04-27 16:01:32 +0200125 snprint_alias(modalias_buf, sizeof(modalias_buf), id, "");
Kay Sievers7eff2e72007-08-14 15:15:12 +0200126 ret = add_uevent_var(env, "MODALIAS=%s", modalias_buf);
127 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128}
129
Cornelia Huck8bbace72006-01-11 10:56:22 +0100130struct bus_type ccw_bus_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
Cornelia Huck602b20f2008-01-26 14:10:39 +0100132static void io_subchannel_irq(struct subchannel *);
133static int io_subchannel_probe(struct subchannel *);
134static int io_subchannel_remove(struct subchannel *);
Cornelia Huck8bbace72006-01-11 10:56:22 +0100135static void io_subchannel_shutdown(struct subchannel *);
Cornelia Huckc820de32008-07-14 09:58:45 +0200136static int io_subchannel_sch_event(struct subchannel *, int);
Cornelia Huck99611f82008-07-14 09:59:02 +0200137static int io_subchannel_chp_event(struct subchannel *, struct chp_link *,
138 int);
Sebastian Ott8ea7f552009-09-22 22:58:35 +0200139static void recovery_func(unsigned long data);
Sebastian Ott8ea7f552009-09-22 22:58:35 +0200140wait_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 Ottb4c70722010-02-26 22:37:27 +0100162static int io_subchannel_settle(void)
Sebastian Ott8ea7f552009-09-22 22:58:35 +0200163{
Sebastian Ottb4c70722010-02-26 22:37:27 +0100164 int ret;
165
166 ret = wait_event_interruptible(ccw_device_init_wq,
167 atomic_read(&ccw_device_init_count) == 0);
168 if (ret)
169 return -EINTR;
Sebastian Ottbe5d3822010-02-26 22:37:24 +0100170 flush_workqueue(cio_work_q);
Sebastian Ottb4c70722010-02-26 22:37:27 +0100171 return 0;
Sebastian Ott8ea7f552009-09-22 22:58:35 +0200172}
173
Cornelia Huckf7e5d672007-05-10 15:45:43 +0200174static struct css_driver io_subchannel_driver = {
Cornelia Huck4beee642008-01-26 14:10:47 +0100175 .owner = THIS_MODULE,
Cornelia Huckf08adc02008-07-14 09:59:03 +0200176 .subchannel_type = io_subchannel_ids,
Cornelia Huck25b7bb52008-01-26 14:10:41 +0100177 .name = "io_subchannel",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 .irq = io_subchannel_irq,
Cornelia Huckc820de32008-07-14 09:58:45 +0200179 .sch_event = io_subchannel_sch_event,
180 .chp_event = io_subchannel_chp_event,
Cornelia Huck8bbace72006-01-11 10:56:22 +0100181 .probe = io_subchannel_probe,
182 .remove = io_subchannel_remove,
183 .shutdown = io_subchannel_shutdown,
Cornelia Huck93a27592009-06-16 10:30:23 +0200184 .prepare = io_subchannel_prepare,
Sebastian Ott8ea7f552009-09-22 22:58:35 +0200185 .settle = io_subchannel_settle,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186};
187
Sebastian Ott2f176442009-09-22 22:58:33 +0200188int __init io_subchannel_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189{
190 int ret;
191
192 init_waitqueue_head(&ccw_device_init_wq);
193 atomic_set(&ccw_device_init_count, 0);
Peter Oberparleiter90ab1332008-01-26 14:10:52 +0100194 setup_timer(&recovery_timer, recovery_func, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
Sebastian Ottbe5d3822010-02-26 22:37:24 +0100196 ret = bus_register(&ccw_bus_type);
197 if (ret)
198 return ret;
Cornelia Huck25b7bb52008-01-26 14:10:41 +0100199 ret = css_driver_register(&io_subchannel_driver);
200 if (ret)
Sebastian Ottbe5d3822010-02-26 22:37:24 +0100201 bus_unregister(&ccw_bus_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 return ret;
204}
205
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
207/************************ device handling **************************/
208
209/*
210 * A ccw_device has some interfaces in sysfs in addition to the
211 * standard ones.
212 * The following entries are designed to export the information which
213 * resided in 2.4 in /proc/subchannels. Subchannel and device number
214 * are obvious, so they don't have an entry :)
215 * TODO: Split chpids and pimpampom up? Where is "in use" in the tree?
216 */
217static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400218chpids_show (struct device * dev, struct device_attribute *attr, char * buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219{
220 struct subchannel *sch = to_subchannel(dev);
Peter Oberparleiter7ad6a242007-04-27 16:01:35 +0200221 struct chsc_ssd_info *ssd = &sch->ssd_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 ssize_t ret = 0;
223 int chp;
Peter Oberparleiter7ad6a242007-04-27 16:01:35 +0200224 int mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
Peter Oberparleiter7ad6a242007-04-27 16:01:35 +0200226 for (chp = 0; chp < 8; chp++) {
227 mask = 0x80 >> chp;
228 if (ssd->path_mask & mask)
229 ret += sprintf(buf + ret, "%02x ", ssd->chpid[chp].id);
230 else
231 ret += sprintf(buf + ret, "00 ");
232 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 ret += sprintf (buf+ret, "\n");
234 return min((ssize_t)PAGE_SIZE, ret);
235}
236
237static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400238pimpampom_show (struct device * dev, struct device_attribute *attr, char * buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239{
240 struct subchannel *sch = to_subchannel(dev);
241 struct pmcw *pmcw = &sch->schib.pmcw;
242
243 return sprintf (buf, "%02x %02x %02x\n",
244 pmcw->pim, pmcw->pam, pmcw->pom);
245}
246
247static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400248devtype_show (struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249{
250 struct ccw_device *cdev = to_ccwdev(dev);
251 struct ccw_device_id *id = &(cdev->id);
252
253 if (id->dev_type != 0)
254 return sprintf(buf, "%04x/%02x\n",
255 id->dev_type, id->dev_model);
256 else
257 return sprintf(buf, "n/a\n");
258}
259
260static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400261cutype_show (struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262{
263 struct ccw_device *cdev = to_ccwdev(dev);
264 struct ccw_device_id *id = &(cdev->id);
265
266 return sprintf(buf, "%04x/%02x\n",
267 id->cu_type, id->cu_model);
268}
269
270static ssize_t
Bastian Blankf1fc78a2005-10-30 15:00:12 -0800271modalias_show (struct device *dev, struct device_attribute *attr, char *buf)
272{
273 struct ccw_device *cdev = to_ccwdev(dev);
274 struct ccw_device_id *id = &(cdev->id);
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +0200275 int len;
Bastian Blankf1fc78a2005-10-30 15:00:12 -0800276
Cornelia Huck086a6c62007-07-17 13:36:08 +0200277 len = snprint_alias(buf, PAGE_SIZE, id, "\n");
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +0200278
279 return len > PAGE_SIZE ? PAGE_SIZE : len;
Bastian Blankf1fc78a2005-10-30 15:00:12 -0800280}
281
282static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400283online_show (struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284{
285 struct ccw_device *cdev = to_ccwdev(dev);
286
287 return sprintf(buf, cdev->online ? "1\n" : "0\n");
288}
289
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100290int ccw_device_is_orphan(struct ccw_device *cdev)
291{
292 return sch_is_pseudo_sch(to_subchannel(cdev->dev.parent));
293}
294
Cornelia Huckef995162007-04-27 16:01:39 +0200295static void ccw_device_unregister(struct ccw_device *cdev)
Cornelia Huck7674da72006-12-08 15:54:21 +0100296{
Sebastian Ott7d253b92009-12-07 12:51:33 +0100297 if (device_is_registered(&cdev->dev)) {
Sebastian Ott24a18722009-12-07 12:51:34 +0100298 /* Undo device_add(). */
Cornelia Huckef995162007-04-27 16:01:39 +0200299 device_del(&cdev->dev);
Sebastian Ott24a18722009-12-07 12:51:34 +0100300 }
301 if (cdev->private->flags.initialized) {
302 cdev->private->flags.initialized = 0;
Sebastian Ott3b554a12009-09-11 10:28:26 +0200303 /* Release reference from device_initialize(). */
304 put_device(&cdev->dev);
305 }
Cornelia Huck7674da72006-12-08 15:54:21 +0100306}
307
Sebastian Ottd40f7b72009-12-07 12:51:41 +0100308static void io_subchannel_quiesce(struct subchannel *);
309
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +0200310/**
311 * ccw_device_set_offline() - disable a ccw device for I/O
312 * @cdev: target ccw device
313 *
314 * This function calls the driver's set_offline() function for @cdev, if
315 * given, and then disables @cdev.
316 * Returns:
317 * %0 on success and a negative error value on failure.
318 * Context:
319 * enabled, ccw device lock not held
320 */
321int ccw_device_set_offline(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322{
Sebastian Ottd40f7b72009-12-07 12:51:41 +0100323 struct subchannel *sch;
324 int ret, state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
326 if (!cdev)
327 return -ENODEV;
328 if (!cdev->online || !cdev->drv)
329 return -EINVAL;
330
331 if (cdev->drv->set_offline) {
332 ret = cdev->drv->set_offline(cdev);
333 if (ret != 0)
334 return ret;
335 }
336 cdev->online = 0;
337 spin_lock_irq(cdev->ccwlock);
Sebastian Ottd40f7b72009-12-07 12:51:41 +0100338 sch = to_subchannel(cdev->dev.parent);
Michael Ernst217ee6c2009-09-11 10:28:21 +0200339 /* Wait until a final state or DISCONNECTED is reached */
340 while (!dev_fsm_final_state(cdev) &&
341 cdev->private->state != DEV_STATE_DISCONNECTED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 spin_unlock_irq(cdev->ccwlock);
Michael Ernst217ee6c2009-09-11 10:28:21 +0200343 wait_event(cdev->private->wait_q, (dev_fsm_final_state(cdev) ||
344 cdev->private->state == DEV_STATE_DISCONNECTED));
345 spin_lock_irq(cdev->ccwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 }
Sebastian Ottd40f7b72009-12-07 12:51:41 +0100347 do {
348 ret = ccw_device_offline(cdev);
349 if (!ret)
350 break;
351 CIO_MSG_EVENT(0, "ccw_device_offline returned %d, device "
352 "0.%x.%04x\n", ret, cdev->private->dev_id.ssid,
353 cdev->private->dev_id.devno);
354 if (ret != -EBUSY)
355 goto error;
356 state = cdev->private->state;
357 spin_unlock_irq(cdev->ccwlock);
358 io_subchannel_quiesce(sch);
359 spin_lock_irq(cdev->ccwlock);
360 cdev->private->state = state;
361 } while (ret == -EBUSY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 spin_unlock_irq(cdev->ccwlock);
Michael Ernst217ee6c2009-09-11 10:28:21 +0200363 wait_event(cdev->private->wait_q, (dev_fsm_final_state(cdev) ||
364 cdev->private->state == DEV_STATE_DISCONNECTED));
Peter Oberparleitera7ae2c022009-12-07 12:51:20 +0100365 /* Inform the user if set offline failed. */
366 if (cdev->private->state == DEV_STATE_BOXED) {
367 pr_warning("%s: The device entered boxed state while "
368 "being set offline\n", dev_name(&cdev->dev));
369 } else if (cdev->private->state == DEV_STATE_NOT_OPER) {
370 pr_warning("%s: The device stopped operating while "
371 "being set offline\n", dev_name(&cdev->dev));
372 }
Michael Ernst217ee6c2009-09-11 10:28:21 +0200373 /* Give up reference from ccw_device_set_online(). */
374 put_device(&cdev->dev);
375 return 0;
376
377error:
Michael Ernst217ee6c2009-09-11 10:28:21 +0200378 cdev->private->state = DEV_STATE_OFFLINE;
379 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
380 spin_unlock_irq(cdev->ccwlock);
381 /* Give up reference from ccw_device_set_online(). */
382 put_device(&cdev->dev);
383 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384}
385
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +0200386/**
387 * ccw_device_set_online() - enable a ccw device for I/O
388 * @cdev: target ccw device
389 *
390 * This function first enables @cdev and then calls the driver's set_online()
391 * function for @cdev, if given. If set_online() returns an error, @cdev is
392 * disabled again.
393 * Returns:
394 * %0 on success and a negative error value on failure.
395 * Context:
396 * enabled, ccw device lock not held
397 */
398int ccw_device_set_online(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399{
400 int ret;
Michael Ernst217ee6c2009-09-11 10:28:21 +0200401 int ret2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
403 if (!cdev)
404 return -ENODEV;
405 if (cdev->online || !cdev->drv)
406 return -EINVAL;
Cornelia Huck9cd67422008-12-25 13:39:06 +0100407 /* Hold on to an extra reference while device is online. */
408 if (!get_device(&cdev->dev))
409 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
411 spin_lock_irq(cdev->ccwlock);
412 ret = ccw_device_online(cdev);
413 spin_unlock_irq(cdev->ccwlock);
414 if (ret == 0)
415 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
416 else {
Michael Ernst139b83dd2008-05-07 09:22:54 +0200417 CIO_MSG_EVENT(0, "ccw_device_online returned %d, "
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200418 "device 0.%x.%04x\n",
419 ret, cdev->private->dev_id.ssid,
420 cdev->private->dev_id.devno);
Cornelia Huck9cd67422008-12-25 13:39:06 +0100421 /* Give up online reference since onlining failed. */
422 put_device(&cdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 return ret;
424 }
Michael Ernst217ee6c2009-09-11 10:28:21 +0200425 spin_lock_irq(cdev->ccwlock);
426 /* Check if online processing was successful */
427 if ((cdev->private->state != DEV_STATE_ONLINE) &&
428 (cdev->private->state != DEV_STATE_W4SENSE)) {
429 spin_unlock_irq(cdev->ccwlock);
Peter Oberparleitera7ae2c022009-12-07 12:51:20 +0100430 /* Inform the user that set online failed. */
431 if (cdev->private->state == DEV_STATE_BOXED) {
432 pr_warning("%s: Setting the device online failed "
433 "because it is boxed\n",
434 dev_name(&cdev->dev));
435 } else if (cdev->private->state == DEV_STATE_NOT_OPER) {
436 pr_warning("%s: Setting the device online failed "
437 "because it is not operational\n",
438 dev_name(&cdev->dev));
439 }
Cornelia Huck9cd67422008-12-25 13:39:06 +0100440 /* Give up online reference since onlining failed. */
441 put_device(&cdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 return -ENODEV;
Cornelia Huck9cd67422008-12-25 13:39:06 +0100443 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 spin_unlock_irq(cdev->ccwlock);
Michael Ernst217ee6c2009-09-11 10:28:21 +0200445 if (cdev->drv->set_online)
446 ret = cdev->drv->set_online(cdev);
447 if (ret)
448 goto rollback;
449 cdev->online = 1;
450 return 0;
451
452rollback:
453 spin_lock_irq(cdev->ccwlock);
454 /* Wait until a final state or DISCONNECTED is reached */
455 while (!dev_fsm_final_state(cdev) &&
456 cdev->private->state != DEV_STATE_DISCONNECTED) {
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));
460 spin_lock_irq(cdev->ccwlock);
461 }
462 ret2 = ccw_device_offline(cdev);
463 if (ret2)
464 goto error;
465 spin_unlock_irq(cdev->ccwlock);
466 wait_event(cdev->private->wait_q, (dev_fsm_final_state(cdev) ||
467 cdev->private->state == DEV_STATE_DISCONNECTED));
Cornelia Huck9cd67422008-12-25 13:39:06 +0100468 /* Give up online reference since onlining failed. */
469 put_device(&cdev->dev);
Michael Ernst217ee6c2009-09-11 10:28:21 +0200470 return ret;
471
472error:
473 CIO_MSG_EVENT(0, "rollback ccw_device_offline returned %d, "
474 "device 0.%x.%04x\n",
475 ret2, cdev->private->dev_id.ssid,
476 cdev->private->dev_id.devno);
477 cdev->private->state = DEV_STATE_OFFLINE;
478 spin_unlock_irq(cdev->ccwlock);
479 /* Give up online reference since onlining failed. */
480 put_device(&cdev->dev);
481 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482}
483
Sebastian Otte74fe0c2009-03-26 15:24:08 +0100484static int online_store_handle_offline(struct ccw_device *cdev)
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200485{
Peter Oberparleiter37de53b2009-12-07 12:51:19 +0100486 if (cdev->private->state == DEV_STATE_DISCONNECTED) {
487 spin_lock_irq(cdev->ccwlock);
488 ccw_device_sched_todo(cdev, CDEV_TODO_UNREG_EVAL);
489 spin_unlock_irq(cdev->ccwlock);
490 } else if (cdev->online && cdev->drv && cdev->drv->set_offline)
Sebastian Otte74fe0c2009-03-26 15:24:08 +0100491 return ccw_device_set_offline(cdev);
492 return 0;
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200493}
494
495static int online_store_recog_and_online(struct ccw_device *cdev)
496{
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200497 /* Do device recognition, if needed. */
Sebastian Ott99f6a5702009-03-31 19:16:07 +0200498 if (cdev->private->state == DEV_STATE_BOXED) {
Peter Oberparleiter1f5bd38482009-12-07 12:51:23 +0100499 spin_lock_irq(cdev->ccwlock);
Peter Oberparleiter736b5db2009-12-07 12:51:21 +0100500 ccw_device_recognition(cdev);
Peter Oberparleiter1f5bd38482009-12-07 12:51:23 +0100501 spin_unlock_irq(cdev->ccwlock);
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200502 wait_event(cdev->private->wait_q,
503 cdev->private->flags.recog_done);
Sebastian Ott156013f2009-03-31 19:16:03 +0200504 if (cdev->private->state != DEV_STATE_OFFLINE)
505 /* recognition failed */
506 return -EAGAIN;
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200507 }
508 if (cdev->drv && cdev->drv->set_online)
509 ccw_device_set_online(cdev);
510 return 0;
511}
Sebastian Ott156013f2009-03-31 19:16:03 +0200512
Michael Ernstc78aa6c2008-07-14 09:59:22 +0200513static int online_store_handle_online(struct ccw_device *cdev, int force)
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200514{
515 int ret;
516
517 ret = online_store_recog_and_online(cdev);
Sebastian Ott156013f2009-03-31 19:16:03 +0200518 if (ret && !force)
Michael Ernstc78aa6c2008-07-14 09:59:22 +0200519 return ret;
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200520 if (force && cdev->private->state == DEV_STATE_BOXED) {
521 ret = ccw_device_stlck(cdev);
Michael Ernstc78aa6c2008-07-14 09:59:22 +0200522 if (ret)
523 return ret;
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200524 if (cdev->id.cu_type == 0)
525 cdev->private->state = DEV_STATE_NOT_OPER;
Sebastian Ott156013f2009-03-31 19:16:03 +0200526 ret = online_store_recog_and_online(cdev);
527 if (ret)
528 return ret;
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200529 }
Michael Ernstc78aa6c2008-07-14 09:59:22 +0200530 return 0;
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200531}
532
533static ssize_t online_store (struct device *dev, struct device_attribute *attr,
534 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535{
536 struct ccw_device *cdev = to_ccwdev(dev);
Cornelia Huck2f972202008-04-30 13:38:33 +0200537 int force, ret;
538 unsigned long i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
Peter Oberparleiter350e9122009-12-07 12:51:28 +0100540 if (!dev_fsm_final_state(cdev) &&
541 cdev->private->state != DEV_STATE_DISCONNECTED)
542 return -EAGAIN;
543 if (atomic_cmpxchg(&cdev->private->onoff, 0, 1) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 return -EAGAIN;
545
546 if (cdev->drv && !try_module_get(cdev->drv->owner)) {
547 atomic_set(&cdev->private->onoff, 0);
548 return -EINVAL;
549 }
550 if (!strncmp(buf, "force\n", count)) {
551 force = 1;
552 i = 1;
Cornelia Huck2f972202008-04-30 13:38:33 +0200553 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 } else {
555 force = 0;
Cornelia Huck2f972202008-04-30 13:38:33 +0200556 ret = strict_strtoul(buf, 16, &i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 }
Cornelia Huck2f972202008-04-30 13:38:33 +0200558 if (ret)
559 goto out;
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200560 switch (i) {
561 case 0:
Sebastian Otte74fe0c2009-03-26 15:24:08 +0100562 ret = online_store_handle_offline(cdev);
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200563 break;
564 case 1:
Michael Ernstc78aa6c2008-07-14 09:59:22 +0200565 ret = online_store_handle_online(cdev, force);
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200566 break;
567 default:
Cornelia Huck2f972202008-04-30 13:38:33 +0200568 ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 }
Cornelia Huck2f972202008-04-30 13:38:33 +0200570out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 if (cdev->drv)
572 module_put(cdev->drv->owner);
573 atomic_set(&cdev->private->onoff, 0);
Sebastian Otte74fe0c2009-03-26 15:24:08 +0100574 return (ret < 0) ? ret : count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575}
576
577static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400578available_show (struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579{
580 struct ccw_device *cdev = to_ccwdev(dev);
581 struct subchannel *sch;
582
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100583 if (ccw_device_is_orphan(cdev))
584 return sprintf(buf, "no device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 switch (cdev->private->state) {
586 case DEV_STATE_BOXED:
587 return sprintf(buf, "boxed\n");
588 case DEV_STATE_DISCONNECTED:
589 case DEV_STATE_DISCONNECTED_SENSE_ID:
590 case DEV_STATE_NOT_OPER:
591 sch = to_subchannel(dev->parent);
592 if (!sch->lpm)
593 return sprintf(buf, "no path\n");
594 else
595 return sprintf(buf, "no device\n");
596 default:
597 /* All other states considered fine. */
598 return sprintf(buf, "good\n");
599 }
600}
601
Michael Ernstfd0457a2010-08-09 18:12:50 +0200602static ssize_t
603initiate_logging(struct device *dev, struct device_attribute *attr,
604 const char *buf, size_t count)
605{
606 struct subchannel *sch = to_subchannel(dev);
607 int rc;
608
609 rc = chsc_siosl(sch->schid);
610 if (rc < 0) {
611 pr_warning("Logging for subchannel 0.%x.%04x failed with "
612 "errno=%d\n",
613 sch->schid.ssid, sch->schid.sch_no, rc);
614 return rc;
615 }
616 pr_notice("Logging for subchannel 0.%x.%04x was triggered\n",
617 sch->schid.ssid, sch->schid.sch_no);
618 return count;
619}
620
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621static DEVICE_ATTR(chpids, 0444, chpids_show, NULL);
622static DEVICE_ATTR(pimpampom, 0444, pimpampom_show, NULL);
623static DEVICE_ATTR(devtype, 0444, devtype_show, NULL);
624static DEVICE_ATTR(cutype, 0444, cutype_show, NULL);
Bastian Blankf1fc78a2005-10-30 15:00:12 -0800625static DEVICE_ATTR(modalias, 0444, modalias_show, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626static DEVICE_ATTR(online, 0644, online_show, online_store);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627static DEVICE_ATTR(availability, 0444, available_show, NULL);
Michael Ernstfd0457a2010-08-09 18:12:50 +0200628static DEVICE_ATTR(logging, 0200, NULL, initiate_logging);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200630static struct attribute *io_subchannel_attrs[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 &dev_attr_chpids.attr,
632 &dev_attr_pimpampom.attr,
Michael Ernstfd0457a2010-08-09 18:12:50 +0200633 &dev_attr_logging.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 NULL,
635};
636
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200637static struct attribute_group io_subchannel_attr_group = {
638 .attrs = io_subchannel_attrs,
Cornelia Huck529192f2006-12-08 15:55:57 +0100639};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
641static struct attribute * ccwdev_attrs[] = {
642 &dev_attr_devtype.attr,
643 &dev_attr_cutype.attr,
Bastian Blankf1fc78a2005-10-30 15:00:12 -0800644 &dev_attr_modalias.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 &dev_attr_online.attr,
646 &dev_attr_cmb_enable.attr,
647 &dev_attr_availability.attr,
648 NULL,
649};
650
651static struct attribute_group ccwdev_attr_group = {
652 .attrs = ccwdev_attrs,
653};
654
David Brownella4dbd672009-06-24 10:06:31 -0700655static const struct attribute_group *ccwdev_attr_groups[] = {
Cornelia Huckef995162007-04-27 16:01:39 +0200656 &ccwdev_attr_group,
657 NULL,
658};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
660/* this is a simple abstraction for device_register that sets the
661 * correct bus type and adds the bus specific files */
Cornelia Huck3c9da7b2006-10-27 12:39:33 +0200662static int ccw_device_register(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663{
664 struct device *dev = &cdev->dev;
665 int ret;
666
667 dev->bus = &ccw_bus_type;
Sebastian Ott3ac276f2009-09-11 10:28:27 +0200668 ret = dev_set_name(&cdev->dev, "0.%x.%04x", cdev->private->dev_id.ssid,
669 cdev->private->dev_id.devno);
670 if (ret)
671 return ret;
Sebastian Ott7d253b92009-12-07 12:51:33 +0100672 return device_add(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673}
674
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100675static int match_dev_id(struct device *dev, void *data)
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700676{
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100677 struct ccw_device *cdev = to_ccwdev(dev);
678 struct ccw_dev_id *dev_id = data;
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700679
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100680 return ccw_dev_id_is_equal(&cdev->private->dev_id, dev_id);
681}
682
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100683static struct ccw_device *get_ccwdev_by_dev_id(struct ccw_dev_id *dev_id)
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100684{
685 struct device *dev;
686
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100687 dev = bus_find_device(&ccw_bus_type, NULL, dev_id, match_dev_id);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100688
689 return dev ? to_ccwdev(dev) : NULL;
690}
691
Peter Oberparleiter37de53b2009-12-07 12:51:19 +0100692static void ccw_device_do_unbind_bind(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693{
Cornelia Huckeb32ae82009-03-26 15:24:05 +0100694 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
Sebastian Ott7d253b92009-12-07 12:51:33 +0100696 if (device_is_registered(&cdev->dev)) {
Cornelia Huckeb32ae82009-03-26 15:24:05 +0100697 device_release_driver(&cdev->dev);
698 ret = device_attach(&cdev->dev);
699 WARN_ON(ret == -ENODEV);
700 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701}
702
703static void
704ccw_device_release(struct device *dev)
705{
706 struct ccw_device *cdev;
707
708 cdev = to_ccwdev(dev);
Cornelia Huck6eff2082008-12-25 13:39:07 +0100709 /* Release reference of parent subchannel. */
710 put_device(cdev->dev.parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 kfree(cdev->private);
712 kfree(cdev);
713}
714
Cornelia Huck7674da72006-12-08 15:54:21 +0100715static struct ccw_device * io_subchannel_allocate_dev(struct subchannel *sch)
716{
717 struct ccw_device *cdev;
718
719 cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
720 if (cdev) {
721 cdev->private = kzalloc(sizeof(struct ccw_device_private),
722 GFP_KERNEL | GFP_DMA);
723 if (cdev->private)
724 return cdev;
725 }
726 kfree(cdev);
727 return ERR_PTR(-ENOMEM);
728}
729
Peter Oberparleiter37de53b2009-12-07 12:51:19 +0100730static void ccw_device_todo(struct work_struct *work);
731
Cornelia Huck7674da72006-12-08 15:54:21 +0100732static int io_subchannel_initialize_dev(struct subchannel *sch,
733 struct ccw_device *cdev)
734{
735 cdev->private->cdev = cdev;
736 atomic_set(&cdev->private->onoff, 0);
737 cdev->dev.parent = &sch->dev;
738 cdev->dev.release = ccw_device_release;
Peter Oberparleiter37de53b2009-12-07 12:51:19 +0100739 INIT_WORK(&cdev->private->todo_work, ccw_device_todo);
Cornelia Huckef995162007-04-27 16:01:39 +0200740 cdev->dev.groups = ccwdev_attr_groups;
Cornelia Huck7674da72006-12-08 15:54:21 +0100741 /* Do first half of device_register. */
742 device_initialize(&cdev->dev);
743 if (!get_device(&sch->dev)) {
Cornelia Huck283fdd02008-12-25 13:39:10 +0100744 /* Release reference from device_initialize(). */
745 put_device(&cdev->dev);
Cornelia Huck7674da72006-12-08 15:54:21 +0100746 return -ENODEV;
747 }
Sebastian Ott24a18722009-12-07 12:51:34 +0100748 cdev->private->flags.initialized = 1;
Cornelia Huck7674da72006-12-08 15:54:21 +0100749 return 0;
750}
751
752static struct ccw_device * io_subchannel_create_ccwdev(struct subchannel *sch)
753{
754 struct ccw_device *cdev;
755 int ret;
756
757 cdev = io_subchannel_allocate_dev(sch);
758 if (!IS_ERR(cdev)) {
759 ret = io_subchannel_initialize_dev(sch, cdev);
Sebastian Ott06739a82009-08-23 18:09:04 +0200760 if (ret)
Cornelia Huck7674da72006-12-08 15:54:21 +0100761 cdev = ERR_PTR(ret);
Cornelia Huck7674da72006-12-08 15:54:21 +0100762 }
763 return cdev;
764}
765
Peter Oberparleiter736b5db2009-12-07 12:51:21 +0100766static void io_subchannel_recog(struct ccw_device *, struct subchannel *);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100767
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100768static void sch_create_and_recog_new_device(struct subchannel *sch)
769{
770 struct ccw_device *cdev;
771
772 /* Need to allocate a new ccw device. */
773 cdev = io_subchannel_create_ccwdev(sch);
774 if (IS_ERR(cdev)) {
775 /* OK, we did everything we could... */
776 css_sch_device_unregister(sch);
777 return;
778 }
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100779 /* Start recognition for the new ccw device. */
Peter Oberparleiter736b5db2009-12-07 12:51:21 +0100780 io_subchannel_recog(cdev, sch);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100781}
782
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783/*
784 * Register recognized device.
785 */
Peter Oberparleiter37de53b2009-12-07 12:51:19 +0100786static void io_subchannel_register(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 struct subchannel *sch;
Sebastian Otta2901562010-03-08 12:25:17 +0100789 int ret, adjust_init_count = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 unsigned long flags;
791
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 sch = to_subchannel(cdev->dev.parent);
Cornelia Huck5fb6b852008-12-25 13:39:08 +0100793 /*
794 * Check if subchannel is still registered. It may have become
795 * unregistered if a machine check hit us after finishing
796 * device recognition but before the register work could be
797 * queued.
798 */
799 if (!device_is_registered(&sch->dev))
800 goto out_err;
Cornelia Huck82b7ac02007-04-27 16:01:36 +0200801 css_update_ssd_info(sch);
Cornelia Huck47af5512006-12-04 15:41:07 +0100802 /*
803 * io_subchannel_register() will also be called after device
804 * recognition has been done for a boxed device (which will already
805 * be registered). We need to reprobe since we may now have sense id
806 * information.
807 */
Cornelia Huckd6a30762008-12-25 13:39:11 +0100808 if (device_is_registered(&cdev->dev)) {
Cornelia Huck47af5512006-12-04 15:41:07 +0100809 if (!cdev->drv) {
810 ret = device_reprobe(&cdev->dev);
811 if (ret)
812 /* We can't do much here. */
Michael Ernst139b83dd2008-05-07 09:22:54 +0200813 CIO_MSG_EVENT(0, "device_reprobe() returned"
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200814 " %d for 0.%x.%04x\n", ret,
815 cdev->private->dev_id.ssid,
816 cdev->private->dev_id.devno);
Cornelia Huck47af5512006-12-04 15:41:07 +0100817 }
Sebastian Otta2901562010-03-08 12:25:17 +0100818 adjust_init_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 goto out;
820 }
Cornelia Huckfa1a8c22007-04-26 00:12:03 -0700821 /*
822 * Now we know this subchannel will stay, we can throw
823 * our delayed uevent.
824 */
Ming Leif67f1292009-03-01 21:10:49 +0800825 dev_set_uevent_suppress(&sch->dev, 0);
Cornelia Huckfa1a8c22007-04-26 00:12:03 -0700826 kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 /* make it known to the system */
828 ret = ccw_device_register(cdev);
829 if (ret) {
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200830 CIO_MSG_EVENT(0, "Could not register ccw dev 0.%x.%04x: %d\n",
831 cdev->private->dev_id.ssid,
832 cdev->private->dev_id.devno, ret);
Cornelia Huck2ec22982006-12-08 15:54:26 +0100833 spin_lock_irqsave(sch->lock, flags);
Cornelia Huckdb6a6422008-01-26 14:10:46 +0100834 sch_set_cdev(sch, NULL);
Cornelia Huck2ec22982006-12-08 15:54:26 +0100835 spin_unlock_irqrestore(sch->lock, flags);
Cornelia Huck6eff2082008-12-25 13:39:07 +0100836 /* Release initial device reference. */
837 put_device(&cdev->dev);
Cornelia Huck5fb6b852008-12-25 13:39:08 +0100838 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840out:
841 cdev->private->flags.recog_done = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 wake_up(&cdev->private->wait_q);
Cornelia Huck5fb6b852008-12-25 13:39:08 +0100843out_err:
Sebastian Otta2901562010-03-08 12:25:17 +0100844 if (adjust_init_count && atomic_dec_and_test(&ccw_device_init_count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 wake_up(&ccw_device_init_wq);
846}
847
Peter Oberparleiter37de53b2009-12-07 12:51:19 +0100848static void ccw_device_call_sch_unregister(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 struct subchannel *sch;
851
Peter Oberparleiter46fbe4e2008-10-10 21:33:05 +0200852 /* Get subchannel reference for local processing. */
853 if (!get_device(cdev->dev.parent))
854 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 sch = to_subchannel(cdev->dev.parent);
Cornelia Huck6ab48792006-07-12 16:39:50 +0200856 css_sch_device_unregister(sch);
Peter Oberparleiter46fbe4e2008-10-10 21:33:05 +0200857 /* Release subchannel reference for local processing. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 put_device(&sch->dev);
859}
860
861/*
862 * subchannel recognition done. Called from the state machine.
863 */
864void
865io_subchannel_recog_done(struct ccw_device *cdev)
866{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 if (css_init_done == 0) {
868 cdev->private->flags.recog_done = 1;
869 return;
870 }
871 switch (cdev->private->state) {
Sebastian Ott47593bf2009-03-31 19:16:05 +0200872 case DEV_STATE_BOXED:
873 /* Device did not respond in time. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 case DEV_STATE_NOT_OPER:
875 cdev->private->flags.recog_done = 1;
Peter Oberparleiter37de53b2009-12-07 12:51:19 +0100876 /* Remove device found not operational. */
877 ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 if (atomic_dec_and_test(&ccw_device_init_count))
879 wake_up(&ccw_device_init_wq);
880 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 case DEV_STATE_OFFLINE:
882 /*
883 * We can't register the device in interrupt context so
884 * we schedule a work item.
885 */
Peter Oberparleiter37de53b2009-12-07 12:51:19 +0100886 ccw_device_sched_todo(cdev, CDEV_TODO_REGISTER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 break;
888 }
889}
890
Peter Oberparleiter736b5db2009-12-07 12:51:21 +0100891static void io_subchannel_recog(struct ccw_device *cdev, struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 struct ccw_device_private *priv;
894
Cornelia Huck2ec22982006-12-08 15:54:26 +0100895 cdev->ccwlock = sch->lock;
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800896
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 /* Init private data. */
898 priv = cdev->private;
Cornelia Huck78964262006-10-11 15:31:38 +0200899 priv->dev_id.devno = sch->schib.pmcw.dev;
900 priv->dev_id.ssid = sch->schid.ssid;
901 priv->schid = sch->schid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 priv->state = DEV_STATE_NOT_OPER;
903 INIT_LIST_HEAD(&priv->cmb_list);
904 init_waitqueue_head(&priv->wait_q);
905 init_timer(&priv->timer);
906
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 /* Increase counter of devices currently in recognition. */
908 atomic_inc(&ccw_device_init_count);
909
910 /* Start async. device sensing. */
Cornelia Huck2ec22982006-12-08 15:54:26 +0100911 spin_lock_irq(sch->lock);
Peter Oberparleiter60e4dac2009-12-07 12:51:16 +0100912 sch_set_cdev(sch, cdev);
Peter Oberparleiter736b5db2009-12-07 12:51:21 +0100913 ccw_device_recognition(cdev);
Cornelia Huck2ec22982006-12-08 15:54:26 +0100914 spin_unlock_irq(sch->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915}
916
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100917static int ccw_device_move_to_sch(struct ccw_device *cdev,
918 struct subchannel *sch)
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100919{
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100920 struct subchannel *old_sch;
Sebastian Ott0c609fc2009-12-07 12:51:37 +0100921 int rc, old_enabled = 0;
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100922
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100923 old_sch = to_subchannel(cdev->dev.parent);
924 /* Obtain child reference for new parent. */
Cornelia Huck6eff2082008-12-25 13:39:07 +0100925 if (!get_device(&sch->dev))
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100926 return -ENODEV;
Sebastian Ott0c609fc2009-12-07 12:51:37 +0100927
928 if (!sch_is_pseudo_sch(old_sch)) {
929 spin_lock_irq(old_sch->lock);
930 old_enabled = old_sch->schib.pmcw.ena;
931 rc = 0;
932 if (old_enabled)
933 rc = cio_disable_subchannel(old_sch);
934 spin_unlock_irq(old_sch->lock);
935 if (rc == -EBUSY) {
936 /* Release child reference for new parent. */
937 put_device(&sch->dev);
938 return rc;
939 }
940 }
941
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100942 mutex_lock(&sch->reg_mutex);
Cornelia Huckffa6a702009-03-04 12:44:00 +0100943 rc = device_move(&cdev->dev, &sch->dev, DPM_ORDER_PARENT_BEFORE_DEV);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100944 mutex_unlock(&sch->reg_mutex);
945 if (rc) {
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100946 CIO_MSG_EVENT(0, "device_move(0.%x.%04x,0.%x.%04x)=%d\n",
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100947 cdev->private->dev_id.ssid,
948 cdev->private->dev_id.devno, sch->schid.ssid,
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100949 sch->schib.pmcw.dev, rc);
Sebastian Ott0c609fc2009-12-07 12:51:37 +0100950 if (old_enabled) {
951 /* Try to reenable the old subchannel. */
952 spin_lock_irq(old_sch->lock);
953 cio_enable_subchannel(old_sch, (u32)(addr_t)old_sch);
954 spin_unlock_irq(old_sch->lock);
955 }
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100956 /* Release child reference for new parent. */
Cornelia Huck6eff2082008-12-25 13:39:07 +0100957 put_device(&sch->dev);
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100958 return rc;
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100959 }
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100960 /* Clean up old subchannel. */
961 if (!sch_is_pseudo_sch(old_sch)) {
962 spin_lock_irq(old_sch->lock);
963 sch_set_cdev(old_sch, NULL);
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100964 spin_unlock_irq(old_sch->lock);
965 css_schedule_eval(old_sch->schid);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100966 }
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100967 /* Release child reference for old parent. */
968 put_device(&old_sch->dev);
969 /* Initialize new subchannel. */
970 spin_lock_irq(sch->lock);
971 cdev->private->schid = sch->schid;
972 cdev->ccwlock = sch->lock;
973 if (!sch_is_pseudo_sch(sch))
974 sch_set_cdev(sch, cdev);
975 spin_unlock_irq(sch->lock);
976 if (!sch_is_pseudo_sch(sch))
977 css_update_ssd_info(sch);
978 return 0;
979}
980
981static int ccw_device_move_to_orph(struct ccw_device *cdev)
982{
983 struct subchannel *sch = to_subchannel(cdev->dev.parent);
984 struct channel_subsystem *css = to_css(sch->dev.parent);
985
986 return ccw_device_move_to_sch(cdev, css->pseudo_subchannel);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100987}
988
Cornelia Huck602b20f2008-01-26 14:10:39 +0100989static void io_subchannel_irq(struct subchannel *sch)
990{
991 struct ccw_device *cdev;
992
Cornelia Huckdb6a6422008-01-26 14:10:46 +0100993 cdev = sch_get_cdev(sch);
Cornelia Huck602b20f2008-01-26 14:10:39 +0100994
Sebastian Ottefd986d2009-09-11 10:28:18 +0200995 CIO_TRACE_EVENT(6, "IRQ");
996 CIO_TRACE_EVENT(6, dev_name(&sch->dev));
Cornelia Huck602b20f2008-01-26 14:10:39 +0100997 if (cdev)
998 dev_fsm_event(cdev, DEV_EVENT_INTERRUPT);
999}
1000
Sebastian Ott13952ec2008-12-25 13:39:13 +01001001void io_subchannel_init_config(struct subchannel *sch)
1002{
1003 memset(&sch->config, 0, sizeof(sch->config));
1004 sch->config.csense = 1;
Sebastian Ott13952ec2008-12-25 13:39:13 +01001005}
1006
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001007static void io_subchannel_init_fields(struct subchannel *sch)
1008{
1009 if (cio_is_console(sch->schid))
1010 sch->opm = 0xff;
1011 else
1012 sch->opm = chp_get_sch_opm(sch);
1013 sch->lpm = sch->schib.pmcw.pam & sch->opm;
Cornelia Huck3a3fc292008-07-14 09:58:58 +02001014 sch->isc = cio_is_console(sch->schid) ? CONSOLE_ISC : IO_SCH_ISC;
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001015
1016 CIO_MSG_EVENT(6, "Detected device %04x on subchannel 0.%x.%04X"
1017 " - PIM = %02X, PAM = %02X, POM = %02X\n",
1018 sch->schib.pmcw.dev, sch->schid.ssid,
1019 sch->schid.sch_no, sch->schib.pmcw.pim,
1020 sch->schib.pmcw.pam, sch->schib.pmcw.pom);
Sebastian Ott13952ec2008-12-25 13:39:13 +01001021
1022 io_subchannel_init_config(sch);
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001023}
1024
Cornelia Huck90ed2b62008-12-25 13:39:09 +01001025/*
1026 * Note: We always return 0 so that we bind to the device even on error.
1027 * This is needed so that our remove function is called on unregister.
1028 */
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001029static int io_subchannel_probe(struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 struct ccw_device *cdev;
1032 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033
Peter Oberparleiter6d7c5af2009-10-14 12:43:50 +02001034 if (cio_is_console(sch->schid)) {
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001035 rc = sysfs_create_group(&sch->dev.kobj,
1036 &io_subchannel_attr_group);
1037 if (rc)
1038 CIO_MSG_EVENT(0, "Failed to create io subchannel "
1039 "attributes for subchannel "
1040 "0.%x.%04x (rc=%d)\n",
1041 sch->schid.ssid, sch->schid.sch_no, rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 /*
Peter Oberparleiter6d7c5af2009-10-14 12:43:50 +02001043 * The console subchannel already has an associated ccw_device.
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001044 * Throw the delayed uevent for the subchannel, register
Peter Oberparleiter6d7c5af2009-10-14 12:43:50 +02001045 * the ccw_device and exit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 */
Ming Leif67f1292009-03-01 21:10:49 +08001047 dev_set_uevent_suppress(&sch->dev, 0);
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001048 kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
Peter Oberparleiter6d7c5af2009-10-14 12:43:50 +02001049 cdev = sch_get_cdev(sch);
Cornelia Hucke1031782007-10-12 16:11:23 +02001050 cdev->dev.groups = ccwdev_attr_groups;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 device_initialize(&cdev->dev);
Sebastian Ott24a18722009-12-07 12:51:34 +01001052 cdev->private->flags.initialized = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 ccw_device_register(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 /*
1055 * Check if the device is already online. If it is
Cornelia Huck9cd67422008-12-25 13:39:06 +01001056 * the reference count needs to be corrected since we
1057 * didn't obtain a reference in ccw_device_set_online.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 */
1059 if (cdev->private->state != DEV_STATE_NOT_OPER &&
1060 cdev->private->state != DEV_STATE_OFFLINE &&
1061 cdev->private->state != DEV_STATE_BOXED)
1062 get_device(&cdev->dev);
1063 return 0;
1064 }
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001065 io_subchannel_init_fields(sch);
Sebastian Ottf444cc02008-12-25 13:39:14 +01001066 rc = cio_commit_config(sch);
1067 if (rc)
1068 goto out_schedule;
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001069 rc = sysfs_create_group(&sch->dev.kobj,
1070 &io_subchannel_attr_group);
1071 if (rc)
Cornelia Huck90ed2b62008-12-25 13:39:09 +01001072 goto out_schedule;
Cornelia Huckcd6b4f22008-01-26 14:10:43 +01001073 /* Allocate I/O subchannel private data. */
1074 sch->private = kzalloc(sizeof(struct io_subchannel_private),
1075 GFP_KERNEL | GFP_DMA);
Cornelia Huck90ed2b62008-12-25 13:39:09 +01001076 if (!sch->private)
Peter Oberparleiter48e4c382009-12-07 12:51:15 +01001077 goto out_schedule;
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001078 css_schedule_eval(sch->schid);
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001079 return 0;
Peter Oberparleiter48e4c382009-12-07 12:51:15 +01001080
Cornelia Huck90ed2b62008-12-25 13:39:09 +01001081out_schedule:
Peter Oberparleiter390935a2009-12-07 12:51:18 +01001082 spin_lock_irq(sch->lock);
1083 css_sched_sch_todo(sch, SCH_TODO_UNREG);
1084 spin_unlock_irq(sch->lock);
Cornelia Huck90ed2b62008-12-25 13:39:09 +01001085 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086}
1087
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088static int
Cornelia Huck8bbace72006-01-11 10:56:22 +01001089io_subchannel_remove (struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090{
1091 struct ccw_device *cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001093 cdev = sch_get_cdev(sch);
1094 if (!cdev)
Peter Oberparleiter48e4c382009-12-07 12:51:15 +01001095 goto out_free;
Sebastian Ott6e9a0f62009-12-07 12:51:38 +01001096 io_subchannel_quiesce(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 /* Set ccw device to not operational and drop reference. */
Sebastian Ott7a8ad102009-12-07 12:51:39 +01001098 spin_lock_irq(cdev->ccwlock);
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001099 sch_set_cdev(sch, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 cdev->private->state = DEV_STATE_NOT_OPER;
Sebastian Ott7a8ad102009-12-07 12:51:39 +01001101 spin_unlock_irq(cdev->ccwlock);
Cornelia Huckef995162007-04-27 16:01:39 +02001102 ccw_device_unregister(cdev);
Peter Oberparleiter48e4c382009-12-07 12:51:15 +01001103out_free:
Cornelia Huckcd6b4f22008-01-26 14:10:43 +01001104 kfree(sch->private);
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001105 sysfs_remove_group(&sch->dev.kobj, &io_subchannel_attr_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 return 0;
1107}
1108
Cornelia Huck602b20f2008-01-26 14:10:39 +01001109static void io_subchannel_verify(struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110{
1111 struct ccw_device *cdev;
1112
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001113 cdev = sch_get_cdev(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 if (cdev)
1115 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1116}
1117
Cornelia Huckc820de32008-07-14 09:58:45 +02001118static void io_subchannel_terminate_path(struct subchannel *sch, u8 mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119{
1120 struct ccw_device *cdev;
1121
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001122 cdev = sch_get_cdev(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 if (!cdev)
1124 return;
Peter Oberparleiter4257aae2009-12-07 12:51:29 +01001125 if (cio_update_schib(sch))
1126 goto err;
1127 /* Check for I/O on path. */
1128 if (scsw_actl(&sch->schib.scsw) == 0 || sch->schib.pmcw.lpum != mask)
1129 goto out;
1130 if (cdev->private->state == DEV_STATE_ONLINE) {
1131 ccw_device_kill_io(cdev);
1132 goto out;
1133 }
1134 if (cio_clear(sch))
1135 goto err;
1136out:
1137 /* Trigger path verification. */
1138 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1139 return;
Cornelia Huckc820de32008-07-14 09:58:45 +02001140
Peter Oberparleiter4257aae2009-12-07 12:51:29 +01001141err:
1142 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
Cornelia Huckc820de32008-07-14 09:58:45 +02001143}
1144
Cornelia Huck99611f82008-07-14 09:59:02 +02001145static int io_subchannel_chp_event(struct subchannel *sch,
1146 struct chp_link *link, int event)
Cornelia Huckc820de32008-07-14 09:58:45 +02001147{
1148 int mask;
Cornelia Huckc820de32008-07-14 09:58:45 +02001149
Cornelia Huck99611f82008-07-14 09:59:02 +02001150 mask = chp_ssd_get_mask(&sch->ssd_info, link);
Cornelia Huckc820de32008-07-14 09:58:45 +02001151 if (!mask)
1152 return 0;
1153 switch (event) {
1154 case CHP_VARY_OFF:
1155 sch->opm &= ~mask;
1156 sch->lpm &= ~mask;
1157 io_subchannel_terminate_path(sch, mask);
1158 break;
1159 case CHP_VARY_ON:
1160 sch->opm |= mask;
1161 sch->lpm |= mask;
1162 io_subchannel_verify(sch);
1163 break;
1164 case CHP_OFFLINE:
Sebastian Ottcdb912a2008-12-25 13:39:12 +01001165 if (cio_update_schib(sch))
Cornelia Huckc820de32008-07-14 09:58:45 +02001166 return -ENODEV;
1167 io_subchannel_terminate_path(sch, mask);
1168 break;
1169 case CHP_ONLINE:
Sebastian Ottcdb912a2008-12-25 13:39:12 +01001170 if (cio_update_schib(sch))
1171 return -ENODEV;
Cornelia Huckc820de32008-07-14 09:58:45 +02001172 sch->lpm |= mask & sch->opm;
1173 io_subchannel_verify(sch);
1174 break;
1175 }
1176 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177}
1178
Sebastian Ott6e9a0f62009-12-07 12:51:38 +01001179static void io_subchannel_quiesce(struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 struct ccw_device *cdev;
1182 int ret;
1183
Sebastian Ott56e6b792009-12-07 12:51:35 +01001184 spin_lock_irq(sch->lock);
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001185 cdev = sch_get_cdev(sch);
Cornelia Hucka8237fc2006-01-06 00:19:21 -08001186 if (cio_is_console(sch->schid))
Sebastian Ott56e6b792009-12-07 12:51:35 +01001187 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 if (!sch->schib.pmcw.ena)
Sebastian Ott56e6b792009-12-07 12:51:35 +01001189 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 ret = cio_disable_subchannel(sch);
1191 if (ret != -EBUSY)
Sebastian Ott56e6b792009-12-07 12:51:35 +01001192 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 if (cdev->handler)
Sebastian Ott56e6b792009-12-07 12:51:35 +01001194 cdev->handler(cdev, cdev->private->intparm, ERR_PTR(-EIO));
1195 while (ret == -EBUSY) {
1196 cdev->private->state = DEV_STATE_QUIESCE;
1197 ret = ccw_device_cancel_halt_clear(cdev);
1198 if (ret == -EBUSY) {
1199 ccw_device_set_timeout(cdev, HZ/10);
1200 spin_unlock_irq(sch->lock);
1201 wait_event(cdev->private->wait_q,
1202 cdev->private->state != DEV_STATE_QUIESCE);
1203 spin_lock_irq(sch->lock);
1204 }
1205 ret = cio_disable_subchannel(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 }
Sebastian Ott56e6b792009-12-07 12:51:35 +01001207out_unlock:
1208 spin_unlock_irq(sch->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209}
1210
Sebastian Ott6e9a0f62009-12-07 12:51:38 +01001211static void io_subchannel_shutdown(struct subchannel *sch)
1212{
1213 io_subchannel_quiesce(sch);
1214}
1215
Cornelia Huckc820de32008-07-14 09:58:45 +02001216static int device_is_disconnected(struct ccw_device *cdev)
1217{
1218 if (!cdev)
1219 return 0;
1220 return (cdev->private->state == DEV_STATE_DISCONNECTED ||
1221 cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID);
1222}
1223
1224static int recovery_check(struct device *dev, void *data)
1225{
1226 struct ccw_device *cdev = to_ccwdev(dev);
1227 int *redo = data;
1228
1229 spin_lock_irq(cdev->ccwlock);
1230 switch (cdev->private->state) {
1231 case DEV_STATE_DISCONNECTED:
1232 CIO_MSG_EVENT(3, "recovery: trigger 0.%x.%04x\n",
1233 cdev->private->dev_id.ssid,
1234 cdev->private->dev_id.devno);
1235 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1236 *redo = 1;
1237 break;
1238 case DEV_STATE_DISCONNECTED_SENSE_ID:
1239 *redo = 1;
1240 break;
1241 }
1242 spin_unlock_irq(cdev->ccwlock);
1243
1244 return 0;
1245}
1246
1247static void recovery_work_func(struct work_struct *unused)
1248{
1249 int redo = 0;
1250
1251 bus_for_each_dev(&ccw_bus_type, NULL, &redo, recovery_check);
1252 if (redo) {
1253 spin_lock_irq(&recovery_lock);
1254 if (!timer_pending(&recovery_timer)) {
1255 if (recovery_phase < ARRAY_SIZE(recovery_delay) - 1)
1256 recovery_phase++;
1257 mod_timer(&recovery_timer, jiffies +
1258 recovery_delay[recovery_phase] * HZ);
1259 }
1260 spin_unlock_irq(&recovery_lock);
1261 } else
1262 CIO_MSG_EVENT(4, "recovery: end\n");
1263}
1264
1265static DECLARE_WORK(recovery_work, recovery_work_func);
1266
1267static void recovery_func(unsigned long data)
1268{
1269 /*
1270 * We can't do our recovery in softirq context and it's not
1271 * performance critical, so we schedule it.
1272 */
1273 schedule_work(&recovery_work);
1274}
1275
1276static void ccw_device_schedule_recovery(void)
1277{
1278 unsigned long flags;
1279
1280 CIO_MSG_EVENT(4, "recovery: schedule\n");
1281 spin_lock_irqsave(&recovery_lock, flags);
1282 if (!timer_pending(&recovery_timer) || (recovery_phase != 0)) {
1283 recovery_phase = 0;
1284 mod_timer(&recovery_timer, jiffies + recovery_delay[0] * HZ);
1285 }
1286 spin_unlock_irqrestore(&recovery_lock, flags);
1287}
1288
Peter Oberparleiterecf5d9e2008-10-10 21:33:06 +02001289static int purge_fn(struct device *dev, void *data)
1290{
1291 struct ccw_device *cdev = to_ccwdev(dev);
Peter Oberparleiter37de53b2009-12-07 12:51:19 +01001292 struct ccw_dev_id *id = &cdev->private->dev_id;
Peter Oberparleiterecf5d9e2008-10-10 21:33:06 +02001293
1294 spin_lock_irq(cdev->ccwlock);
Peter Oberparleiter37de53b2009-12-07 12:51:19 +01001295 if (is_blacklisted(id->ssid, id->devno) &&
1296 (cdev->private->state == DEV_STATE_OFFLINE)) {
1297 CIO_MSG_EVENT(3, "ccw: purging 0.%x.%04x\n", id->ssid,
1298 id->devno);
1299 ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
1300 }
Peter Oberparleiterecf5d9e2008-10-10 21:33:06 +02001301 spin_unlock_irq(cdev->ccwlock);
Peter Oberparleiterecf5d9e2008-10-10 21:33:06 +02001302 /* Abort loop in case of pending signal. */
1303 if (signal_pending(current))
1304 return -EINTR;
1305
1306 return 0;
1307}
1308
1309/**
1310 * ccw_purge_blacklisted - purge unused, blacklisted devices
1311 *
1312 * Unregister all ccw devices that are offline and on the blacklist.
1313 */
1314int ccw_purge_blacklisted(void)
1315{
1316 CIO_MSG_EVENT(2, "ccw: purging blacklisted devices\n");
1317 bus_for_each_dev(&ccw_bus_type, NULL, NULL, purge_fn);
1318 return 0;
1319}
1320
Peter Oberparleiter6afcc772009-10-06 10:34:02 +02001321void ccw_device_set_disconnected(struct ccw_device *cdev)
Cornelia Huckc820de32008-07-14 09:58:45 +02001322{
1323 if (!cdev)
1324 return;
1325 ccw_device_set_timeout(cdev, 0);
1326 cdev->private->flags.fake_irb = 0;
1327 cdev->private->state = DEV_STATE_DISCONNECTED;
1328 if (cdev->online)
1329 ccw_device_schedule_recovery();
1330}
1331
Peter Oberparleiter91c36912008-08-21 19:46:39 +02001332void ccw_device_set_notoper(struct ccw_device *cdev)
1333{
1334 struct subchannel *sch = to_subchannel(cdev->dev.parent);
1335
1336 CIO_TRACE_EVENT(2, "notoper");
Cornelia Huckb9d3aed2008-10-10 21:33:11 +02001337 CIO_TRACE_EVENT(2, dev_name(&sch->dev));
Peter Oberparleiter91c36912008-08-21 19:46:39 +02001338 ccw_device_set_timeout(cdev, 0);
1339 cio_disable_subchannel(sch);
1340 cdev->private->state = DEV_STATE_NOT_OPER;
1341}
1342
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001343enum io_sch_action {
1344 IO_SCH_UNREG,
1345 IO_SCH_ORPH_UNREG,
1346 IO_SCH_ATTACH,
1347 IO_SCH_UNREG_ATTACH,
1348 IO_SCH_ORPH_ATTACH,
1349 IO_SCH_REPROBE,
1350 IO_SCH_VERIFY,
1351 IO_SCH_DISC,
1352 IO_SCH_NOP,
1353};
1354
1355static enum io_sch_action sch_get_action(struct subchannel *sch)
Cornelia Huckc820de32008-07-14 09:58:45 +02001356{
Cornelia Huckc820de32008-07-14 09:58:45 +02001357 struct ccw_device *cdev;
1358
Cornelia Huckc820de32008-07-14 09:58:45 +02001359 cdev = sch_get_cdev(sch);
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001360 if (cio_update_schib(sch)) {
1361 /* Not operational. */
1362 if (!cdev)
1363 return IO_SCH_UNREG;
Sebastian Ott76e6fb42010-02-26 22:37:28 +01001364 if (ccw_device_notify(cdev, CIO_GONE) != NOTIFY_OK)
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001365 return IO_SCH_UNREG;
1366 return IO_SCH_ORPH_UNREG;
Cornelia Huckc820de32008-07-14 09:58:45 +02001367 }
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001368 /* Operational. */
1369 if (!cdev)
1370 return IO_SCH_ATTACH;
1371 if (sch->schib.pmcw.dev != cdev->private->dev_id.devno) {
Sebastian Ott76e6fb42010-02-26 22:37:28 +01001372 if (ccw_device_notify(cdev, CIO_GONE) != NOTIFY_OK)
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001373 return IO_SCH_UNREG_ATTACH;
1374 return IO_SCH_ORPH_ATTACH;
Cornelia Huckc820de32008-07-14 09:58:45 +02001375 }
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001376 if ((sch->schib.pmcw.pam & sch->opm) == 0) {
Sebastian Ott76e6fb42010-02-26 22:37:28 +01001377 if (ccw_device_notify(cdev, CIO_NO_PATH) != NOTIFY_OK)
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001378 return IO_SCH_UNREG;
1379 return IO_SCH_DISC;
Cornelia Huckc820de32008-07-14 09:58:45 +02001380 }
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001381 if (device_is_disconnected(cdev))
1382 return IO_SCH_REPROBE;
1383 if (cdev->online)
1384 return IO_SCH_VERIFY;
1385 return IO_SCH_NOP;
1386}
1387
1388/**
1389 * io_subchannel_sch_event - process subchannel event
1390 * @sch: subchannel
1391 * @process: non-zero if function is called in process context
1392 *
1393 * An unspecified event occurred for this subchannel. Adjust data according
1394 * to the current operational state of the subchannel and device. Return
1395 * zero when the event has been handled sufficiently or -EAGAIN when this
1396 * function should be called again in process context.
1397 */
1398static int io_subchannel_sch_event(struct subchannel *sch, int process)
1399{
1400 unsigned long flags;
1401 struct ccw_device *cdev;
1402 struct ccw_dev_id dev_id;
1403 enum io_sch_action action;
1404 int rc = -EAGAIN;
1405
1406 spin_lock_irqsave(sch->lock, flags);
1407 if (!device_is_registered(&sch->dev))
1408 goto out_unlock;
Peter Oberparleiter390935a2009-12-07 12:51:18 +01001409 if (work_pending(&sch->todo_work))
1410 goto out_unlock;
Peter Oberparleiter37de53b2009-12-07 12:51:19 +01001411 cdev = sch_get_cdev(sch);
1412 if (cdev && work_pending(&cdev->private->todo_work))
1413 goto out_unlock;
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001414 action = sch_get_action(sch);
1415 CIO_MSG_EVENT(2, "event: sch 0.%x.%04x, process=%d, action=%d\n",
1416 sch->schid.ssid, sch->schid.sch_no, process,
1417 action);
1418 /* Perform immediate actions while holding the lock. */
Cornelia Huckc820de32008-07-14 09:58:45 +02001419 switch (action) {
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001420 case IO_SCH_REPROBE:
1421 /* Trigger device recognition. */
Cornelia Huckc820de32008-07-14 09:58:45 +02001422 ccw_device_trigger_reprobe(cdev);
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001423 rc = 0;
1424 goto out_unlock;
1425 case IO_SCH_VERIFY:
Sebastian Ott0d01bb82010-02-26 22:37:29 +01001426 if (cdev->private->flags.resuming == 1) {
1427 if (cio_enable_subchannel(sch, (u32)(addr_t)sch)) {
1428 ccw_device_set_notoper(cdev);
1429 break;
1430 }
1431 }
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001432 /* Trigger path verification. */
1433 io_subchannel_verify(sch);
1434 rc = 0;
1435 goto out_unlock;
1436 case IO_SCH_DISC:
1437 ccw_device_set_disconnected(cdev);
1438 rc = 0;
1439 goto out_unlock;
1440 case IO_SCH_ORPH_UNREG:
1441 case IO_SCH_ORPH_ATTACH:
Peter Oberparleiter6afcc772009-10-06 10:34:02 +02001442 ccw_device_set_disconnected(cdev);
Peter Oberparleiter91c36912008-08-21 19:46:39 +02001443 break;
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001444 case IO_SCH_UNREG_ATTACH:
1445 case IO_SCH_UNREG:
1446 if (cdev)
1447 ccw_device_set_notoper(cdev);
1448 break;
1449 case IO_SCH_NOP:
1450 rc = 0;
1451 goto out_unlock;
Cornelia Huckc820de32008-07-14 09:58:45 +02001452 default:
1453 break;
1454 }
1455 spin_unlock_irqrestore(sch->lock, flags);
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001456 /* All other actions require process context. */
1457 if (!process)
1458 goto out;
1459 /* Handle attached ccw device. */
1460 switch (action) {
1461 case IO_SCH_ORPH_UNREG:
1462 case IO_SCH_ORPH_ATTACH:
1463 /* Move ccw device to orphanage. */
1464 rc = ccw_device_move_to_orph(cdev);
1465 if (rc)
1466 goto out;
1467 break;
1468 case IO_SCH_UNREG_ATTACH:
1469 /* Unregister ccw device. */
Sebastian Ott0d01bb82010-02-26 22:37:29 +01001470 if (!cdev->private->flags.resuming)
1471 ccw_device_unregister(cdev);
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001472 break;
1473 default:
1474 break;
1475 }
1476 /* Handle subchannel. */
1477 switch (action) {
1478 case IO_SCH_ORPH_UNREG:
1479 case IO_SCH_UNREG:
Sebastian Ott0d01bb82010-02-26 22:37:29 +01001480 if (!cdev || !cdev->private->flags.resuming)
1481 css_sch_device_unregister(sch);
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001482 break;
1483 case IO_SCH_ORPH_ATTACH:
1484 case IO_SCH_UNREG_ATTACH:
1485 case IO_SCH_ATTACH:
1486 dev_id.ssid = sch->schid.ssid;
1487 dev_id.devno = sch->schib.pmcw.dev;
1488 cdev = get_ccwdev_by_dev_id(&dev_id);
1489 if (!cdev) {
1490 sch_create_and_recog_new_device(sch);
1491 break;
1492 }
1493 rc = ccw_device_move_to_sch(cdev, sch);
1494 if (rc) {
1495 /* Release reference from get_ccwdev_by_dev_id() */
1496 put_device(&cdev->dev);
1497 goto out;
1498 }
1499 spin_lock_irqsave(sch->lock, flags);
1500 ccw_device_trigger_reprobe(cdev);
1501 spin_unlock_irqrestore(sch->lock, flags);
1502 /* Release reference from get_ccwdev_by_dev_id() */
1503 put_device(&cdev->dev);
1504 break;
1505 default:
1506 break;
1507 }
1508 return 0;
Cornelia Huckc820de32008-07-14 09:58:45 +02001509
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001510out_unlock:
1511 spin_unlock_irqrestore(sch->lock, flags);
1512out:
1513 return rc;
Cornelia Huckc820de32008-07-14 09:58:45 +02001514}
1515
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516#ifdef CONFIG_CCW_CONSOLE
1517static struct ccw_device console_cdev;
1518static struct ccw_device_private console_private;
1519static int console_cdev_in_use;
1520
Cornelia Huck2ec22982006-12-08 15:54:26 +01001521static DEFINE_SPINLOCK(ccw_console_lock);
1522
1523spinlock_t * cio_get_console_lock(void)
1524{
1525 return &ccw_console_lock;
1526}
1527
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001528static int ccw_device_console_enable(struct ccw_device *cdev,
1529 struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530{
1531 int rc;
1532
Cornelia Huckcd6b4f22008-01-26 14:10:43 +01001533 /* Attach subchannel private data. */
1534 sch->private = cio_get_console_priv();
1535 memset(sch->private, 0, sizeof(struct io_subchannel_private));
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001536 io_subchannel_init_fields(sch);
Sebastian Ottf444cc02008-12-25 13:39:14 +01001537 rc = cio_commit_config(sch);
1538 if (rc)
1539 return rc;
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001540 sch->driver = &io_subchannel_driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 /* Initialize the ccw_device structure. */
Heiko Carstens292888c2006-08-30 14:33:35 +02001542 cdev->dev.parent= &sch->dev;
Sebastian Ottffa8d2a2009-12-18 17:43:15 +01001543 sch_set_cdev(sch, cdev);
Peter Oberparleiter736b5db2009-12-07 12:51:21 +01001544 io_subchannel_recog(cdev, sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 /* Now wait for the async. recognition to come to an end. */
1546 spin_lock_irq(cdev->ccwlock);
1547 while (!dev_fsm_final_state(cdev))
1548 wait_cons_dev();
1549 rc = -EIO;
1550 if (cdev->private->state != DEV_STATE_OFFLINE)
1551 goto out_unlock;
1552 ccw_device_online(cdev);
1553 while (!dev_fsm_final_state(cdev))
1554 wait_cons_dev();
1555 if (cdev->private->state != DEV_STATE_ONLINE)
1556 goto out_unlock;
1557 rc = 0;
1558out_unlock:
1559 spin_unlock_irq(cdev->ccwlock);
Peter Oberparleiter736b5db2009-12-07 12:51:21 +01001560 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561}
1562
1563struct ccw_device *
1564ccw_device_probe_console(void)
1565{
1566 struct subchannel *sch;
1567 int ret;
1568
1569 if (xchg(&console_cdev_in_use, 1) != 0)
Peter Oberparleiter600b5d12006-02-01 03:06:35 -08001570 return ERR_PTR(-EBUSY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 sch = cio_probe_console();
1572 if (IS_ERR(sch)) {
1573 console_cdev_in_use = 0;
1574 return (void *) sch;
1575 }
1576 memset(&console_cdev, 0, sizeof(struct ccw_device));
1577 memset(&console_private, 0, sizeof(struct ccw_device_private));
1578 console_cdev.private = &console_private;
Martin Schwidefskyc1637532006-12-08 15:53:57 +01001579 console_private.cdev = &console_cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 ret = ccw_device_console_enable(&console_cdev, sch);
1581 if (ret) {
1582 cio_release_console();
1583 console_cdev_in_use = 0;
1584 return ERR_PTR(ret);
1585 }
1586 console_cdev.online = 1;
1587 return &console_cdev;
1588}
Cornelia Huck1f4e7ed2008-10-10 21:33:14 +02001589
Martin Schwidefsky66648452009-06-16 10:30:28 +02001590static int ccw_device_pm_restore(struct device *dev);
1591
1592int ccw_device_force_console(void)
1593{
1594 if (!console_cdev_in_use)
1595 return -ENODEV;
1596 return ccw_device_pm_restore(&console_cdev.dev);
1597}
1598EXPORT_SYMBOL_GPL(ccw_device_force_console);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599#endif
1600
1601/*
1602 * get ccw_device matching the busid, but only if owned by cdrv
1603 */
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001604static int
1605__ccwdev_check_busid(struct device *dev, void *id)
1606{
1607 char *bus_id;
1608
Cornelia Huck12975ae2006-10-11 15:31:47 +02001609 bus_id = id;
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001610
Kay Sievers98df67b2008-12-25 13:38:55 +01001611 return (strcmp(bus_id, dev_name(dev)) == 0);
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001612}
1613
1614
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +02001615/**
1616 * get_ccwdev_by_busid() - obtain device from a bus id
1617 * @cdrv: driver the device is owned by
1618 * @bus_id: bus id of the device to be searched
1619 *
1620 * This function searches all devices owned by @cdrv for a device with a bus
1621 * id matching @bus_id.
1622 * Returns:
1623 * If a match is found, its reference count of the found device is increased
1624 * and it is returned; else %NULL is returned.
1625 */
1626struct ccw_device *get_ccwdev_by_busid(struct ccw_driver *cdrv,
1627 const char *bus_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628{
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001629 struct device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 struct device_driver *drv;
1631
1632 drv = get_driver(&cdrv->driver);
1633 if (!drv)
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001634 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001636 dev = driver_find_device(drv, NULL, (void *)bus_id,
1637 __ccwdev_check_busid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 put_driver(drv);
1639
Heiko Carstensd2c993d2006-07-12 16:41:55 +02001640 return dev ? to_ccwdev(dev) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641}
1642
1643/************************** device driver handling ************************/
1644
1645/* This is the implementation of the ccw_driver class. The probe, remove
1646 * and release methods are initially very similar to the device_driver
1647 * implementations, with the difference that they have ccw_device
1648 * arguments.
1649 *
1650 * A ccw driver also contains the information that is needed for
1651 * device matching.
1652 */
1653static int
1654ccw_device_probe (struct device *dev)
1655{
1656 struct ccw_device *cdev = to_ccwdev(dev);
1657 struct ccw_driver *cdrv = to_ccwdrv(dev->driver);
1658 int ret;
1659
1660 cdev->drv = cdrv; /* to let the driver call _set_online */
1661
1662 ret = cdrv->probe ? cdrv->probe(cdev) : -ENODEV;
1663
1664 if (ret) {
Heiko Carstensd2c993d2006-07-12 16:41:55 +02001665 cdev->drv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 return ret;
1667 }
1668
1669 return 0;
1670}
1671
1672static int
1673ccw_device_remove (struct device *dev)
1674{
1675 struct ccw_device *cdev = to_ccwdev(dev);
1676 struct ccw_driver *cdrv = cdev->drv;
1677 int ret;
1678
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 if (cdrv->remove)
1680 cdrv->remove(cdev);
1681 if (cdev->online) {
1682 cdev->online = 0;
1683 spin_lock_irq(cdev->ccwlock);
1684 ret = ccw_device_offline(cdev);
1685 spin_unlock_irq(cdev->ccwlock);
1686 if (ret == 0)
1687 wait_event(cdev->private->wait_q,
1688 dev_fsm_final_state(cdev));
1689 else
Michael Ernst139b83dd2008-05-07 09:22:54 +02001690 CIO_MSG_EVENT(0, "ccw_device_offline returned %d, "
Cornelia Hucke556bbb2007-07-27 12:29:19 +02001691 "device 0.%x.%04x\n",
1692 ret, cdev->private->dev_id.ssid,
1693 cdev->private->dev_id.devno);
Cornelia Huck9cd67422008-12-25 13:39:06 +01001694 /* Give up reference obtained in ccw_device_set_online(). */
1695 put_device(&cdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 }
1697 ccw_device_set_timeout(cdev, 0);
Heiko Carstensd2c993d2006-07-12 16:41:55 +02001698 cdev->drv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 return 0;
1700}
1701
Cornelia Huck958974fb2007-10-12 16:11:21 +02001702static void ccw_device_shutdown(struct device *dev)
1703{
1704 struct ccw_device *cdev;
1705
1706 cdev = to_ccwdev(dev);
1707 if (cdev->drv && cdev->drv->shutdown)
1708 cdev->drv->shutdown(cdev);
Cornelia Huck1842f2b2007-10-12 16:11:22 +02001709 disable_cmf(cdev);
Cornelia Huck958974fb2007-10-12 16:11:21 +02001710}
1711
Sebastian Ott823d4942009-06-16 10:30:20 +02001712static int ccw_device_pm_prepare(struct device *dev)
1713{
1714 struct ccw_device *cdev = to_ccwdev(dev);
1715
Peter Oberparleiter37de53b2009-12-07 12:51:19 +01001716 if (work_pending(&cdev->private->todo_work))
Sebastian Ott823d4942009-06-16 10:30:20 +02001717 return -EAGAIN;
1718 /* Fail while device is being set online/offline. */
1719 if (atomic_read(&cdev->private->onoff))
1720 return -EAGAIN;
1721
1722 if (cdev->online && cdev->drv && cdev->drv->prepare)
1723 return cdev->drv->prepare(cdev);
1724
1725 return 0;
1726}
1727
1728static void ccw_device_pm_complete(struct device *dev)
1729{
1730 struct ccw_device *cdev = to_ccwdev(dev);
1731
1732 if (cdev->online && cdev->drv && cdev->drv->complete)
1733 cdev->drv->complete(cdev);
1734}
1735
1736static int ccw_device_pm_freeze(struct device *dev)
1737{
1738 struct ccw_device *cdev = to_ccwdev(dev);
1739 struct subchannel *sch = to_subchannel(cdev->dev.parent);
1740 int ret, cm_enabled;
1741
1742 /* Fail suspend while device is in transistional state. */
1743 if (!dev_fsm_final_state(cdev))
1744 return -EAGAIN;
1745 if (!cdev->online)
1746 return 0;
1747 if (cdev->drv && cdev->drv->freeze) {
1748 ret = cdev->drv->freeze(cdev);
1749 if (ret)
1750 return ret;
1751 }
1752
1753 spin_lock_irq(sch->lock);
1754 cm_enabled = cdev->private->cmb != NULL;
1755 spin_unlock_irq(sch->lock);
1756 if (cm_enabled) {
1757 /* Don't have the css write on memory. */
1758 ret = ccw_set_cmf(cdev, 0);
1759 if (ret)
1760 return ret;
1761 }
1762 /* From here on, disallow device driver I/O. */
1763 spin_lock_irq(sch->lock);
1764 ret = cio_disable_subchannel(sch);
1765 spin_unlock_irq(sch->lock);
1766
1767 return ret;
1768}
1769
1770static int ccw_device_pm_thaw(struct device *dev)
1771{
1772 struct ccw_device *cdev = to_ccwdev(dev);
1773 struct subchannel *sch = to_subchannel(cdev->dev.parent);
1774 int ret, cm_enabled;
1775
1776 if (!cdev->online)
1777 return 0;
1778
1779 spin_lock_irq(sch->lock);
1780 /* Allow device driver I/O again. */
1781 ret = cio_enable_subchannel(sch, (u32)(addr_t)sch);
1782 cm_enabled = cdev->private->cmb != NULL;
1783 spin_unlock_irq(sch->lock);
1784 if (ret)
1785 return ret;
1786
1787 if (cm_enabled) {
1788 ret = ccw_set_cmf(cdev, 1);
1789 if (ret)
1790 return ret;
1791 }
1792
1793 if (cdev->drv && cdev->drv->thaw)
1794 ret = cdev->drv->thaw(cdev);
1795
1796 return ret;
1797}
1798
1799static void __ccw_device_pm_restore(struct ccw_device *cdev)
1800{
1801 struct subchannel *sch = to_subchannel(cdev->dev.parent);
Sebastian Ott823d4942009-06-16 10:30:20 +02001802
Sebastian Ott0d01bb82010-02-26 22:37:29 +01001803 spin_lock_irq(sch->lock);
1804 if (cio_is_console(sch->schid)) {
1805 cio_enable_subchannel(sch, (u32)(addr_t)sch);
1806 goto out_unlock;
1807 }
Sebastian Ott823d4942009-06-16 10:30:20 +02001808 /*
1809 * While we were sleeping, devices may have gone or become
1810 * available again. Kick re-detection.
1811 */
Sebastian Ott823d4942009-06-16 10:30:20 +02001812 cdev->private->flags.resuming = 1;
Sebastian Ott0d01bb82010-02-26 22:37:29 +01001813 css_schedule_eval(sch->schid);
1814 spin_unlock_irq(sch->lock);
1815 css_complete_work();
1816
1817 /* cdev may have been moved to a different subchannel. */
1818 sch = to_subchannel(cdev->dev.parent);
1819 spin_lock_irq(sch->lock);
1820 if (cdev->private->state != DEV_STATE_ONLINE &&
1821 cdev->private->state != DEV_STATE_OFFLINE)
1822 goto out_unlock;
1823
Peter Oberparleiter736b5db2009-12-07 12:51:21 +01001824 ccw_device_recognition(cdev);
Sebastian Ott823d4942009-06-16 10:30:20 +02001825 spin_unlock_irq(sch->lock);
Sebastian Ott823d4942009-06-16 10:30:20 +02001826 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev) ||
1827 cdev->private->state == DEV_STATE_DISCONNECTED);
Sebastian Ott0d01bb82010-02-26 22:37:29 +01001828 spin_lock_irq(sch->lock);
1829
1830out_unlock:
Sebastian Ott823d4942009-06-16 10:30:20 +02001831 cdev->private->flags.resuming = 0;
Sebastian Ott0d01bb82010-02-26 22:37:29 +01001832 spin_unlock_irq(sch->lock);
Sebastian Ott823d4942009-06-16 10:30:20 +02001833}
1834
1835static int resume_handle_boxed(struct ccw_device *cdev)
1836{
1837 cdev->private->state = DEV_STATE_BOXED;
Sebastian Ott76e6fb42010-02-26 22:37:28 +01001838 if (ccw_device_notify(cdev, CIO_BOXED) == NOTIFY_OK)
Sebastian Ott823d4942009-06-16 10:30:20 +02001839 return 0;
Peter Oberparleiter37de53b2009-12-07 12:51:19 +01001840 ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
Sebastian Ott823d4942009-06-16 10:30:20 +02001841 return -ENODEV;
1842}
1843
1844static int resume_handle_disc(struct ccw_device *cdev)
1845{
1846 cdev->private->state = DEV_STATE_DISCONNECTED;
Sebastian Ott76e6fb42010-02-26 22:37:28 +01001847 if (ccw_device_notify(cdev, CIO_GONE) == NOTIFY_OK)
Sebastian Ott823d4942009-06-16 10:30:20 +02001848 return 0;
Peter Oberparleiter37de53b2009-12-07 12:51:19 +01001849 ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
Sebastian Ott823d4942009-06-16 10:30:20 +02001850 return -ENODEV;
1851}
1852
1853static int ccw_device_pm_restore(struct device *dev)
1854{
1855 struct ccw_device *cdev = to_ccwdev(dev);
Sebastian Ott0d01bb82010-02-26 22:37:29 +01001856 struct subchannel *sch;
1857 int ret = 0;
Sebastian Ott823d4942009-06-16 10:30:20 +02001858
1859 __ccw_device_pm_restore(cdev);
Sebastian Ott0d01bb82010-02-26 22:37:29 +01001860 sch = to_subchannel(cdev->dev.parent);
Sebastian Ott823d4942009-06-16 10:30:20 +02001861 spin_lock_irq(sch->lock);
Sebastian Ott0d01bb82010-02-26 22:37:29 +01001862 if (cio_is_console(sch->schid))
Sebastian Ott823d4942009-06-16 10:30:20 +02001863 goto out_restore;
Sebastian Ott0d01bb82010-02-26 22:37:29 +01001864
Sebastian Ott823d4942009-06-16 10:30:20 +02001865 /* check recognition results */
1866 switch (cdev->private->state) {
1867 case DEV_STATE_OFFLINE:
Sebastian Ott0d01bb82010-02-26 22:37:29 +01001868 case DEV_STATE_ONLINE:
1869 cdev->private->flags.donotify = 0;
Sebastian Ott823d4942009-06-16 10:30:20 +02001870 break;
1871 case DEV_STATE_BOXED:
1872 ret = resume_handle_boxed(cdev);
Sebastian Ott823d4942009-06-16 10:30:20 +02001873 if (ret)
Sebastian Ott0d01bb82010-02-26 22:37:29 +01001874 goto out_unlock;
Sebastian Ott823d4942009-06-16 10:30:20 +02001875 goto out_restore;
Sebastian Ott823d4942009-06-16 10:30:20 +02001876 default:
Sebastian Ott0d01bb82010-02-26 22:37:29 +01001877 ret = resume_handle_disc(cdev);
1878 if (ret)
1879 goto out_unlock;
1880 goto out_restore;
Sebastian Ott823d4942009-06-16 10:30:20 +02001881 }
1882 /* check if the device type has changed */
1883 if (!ccw_device_test_sense_data(cdev)) {
1884 ccw_device_update_sense_data(cdev);
Peter Oberparleiter37de53b2009-12-07 12:51:19 +01001885 ccw_device_sched_todo(cdev, CDEV_TODO_REBIND);
Sebastian Ott823d4942009-06-16 10:30:20 +02001886 ret = -ENODEV;
1887 goto out_unlock;
1888 }
Sebastian Ott0d01bb82010-02-26 22:37:29 +01001889 if (!cdev->online)
1890 goto out_unlock;
1891
1892 if (ccw_device_online(cdev)) {
1893 ret = resume_handle_disc(cdev);
1894 if (ret)
1895 goto out_unlock;
1896 goto out_restore;
1897 }
1898 spin_unlock_irq(sch->lock);
1899 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
1900 spin_lock_irq(sch->lock);
1901
1902 if (ccw_device_notify(cdev, CIO_OPER) == NOTIFY_BAD) {
1903 ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
1904 ret = -ENODEV;
Sebastian Ott823d4942009-06-16 10:30:20 +02001905 goto out_unlock;
1906 }
Sebastian Ott823d4942009-06-16 10:30:20 +02001907
Sebastian Ott0d01bb82010-02-26 22:37:29 +01001908 /* reenable cmf, if needed */
1909 if (cdev->private->cmb) {
1910 spin_unlock_irq(sch->lock);
Sebastian Ott823d4942009-06-16 10:30:20 +02001911 ret = ccw_set_cmf(cdev, 1);
Sebastian Ott0d01bb82010-02-26 22:37:29 +01001912 spin_lock_irq(sch->lock);
Sebastian Ott823d4942009-06-16 10:30:20 +02001913 if (ret) {
Sebastian Ottf0148242009-09-11 10:28:23 +02001914 CIO_MSG_EVENT(2, "resume: cdev 0.%x.%04x: cmf failed "
1915 "(rc=%d)\n", cdev->private->dev_id.ssid,
1916 cdev->private->dev_id.devno, ret);
Sebastian Ott823d4942009-06-16 10:30:20 +02001917 ret = 0;
1918 }
1919 }
1920
1921out_restore:
Sebastian Ott0d01bb82010-02-26 22:37:29 +01001922 spin_unlock_irq(sch->lock);
Sebastian Ott823d4942009-06-16 10:30:20 +02001923 if (cdev->online && cdev->drv && cdev->drv->restore)
1924 ret = cdev->drv->restore(cdev);
Sebastian Ott823d4942009-06-16 10:30:20 +02001925 return ret;
1926
Sebastian Ott823d4942009-06-16 10:30:20 +02001927out_unlock:
1928 spin_unlock_irq(sch->lock);
1929 return ret;
1930}
1931
Alexey Dobriyan47145212009-12-14 18:00:08 -08001932static const struct dev_pm_ops ccw_pm_ops = {
Sebastian Ott823d4942009-06-16 10:30:20 +02001933 .prepare = ccw_device_pm_prepare,
1934 .complete = ccw_device_pm_complete,
1935 .freeze = ccw_device_pm_freeze,
1936 .thaw = ccw_device_pm_thaw,
1937 .restore = ccw_device_pm_restore,
1938};
1939
Cornelia Huck8bbace72006-01-11 10:56:22 +01001940struct bus_type ccw_bus_type = {
1941 .name = "ccw",
1942 .match = ccw_bus_match,
1943 .uevent = ccw_uevent,
1944 .probe = ccw_device_probe,
1945 .remove = ccw_device_remove,
Cornelia Huck958974fb2007-10-12 16:11:21 +02001946 .shutdown = ccw_device_shutdown,
Sebastian Ott823d4942009-06-16 10:30:20 +02001947 .pm = &ccw_pm_ops,
Cornelia Huck8bbace72006-01-11 10:56:22 +01001948};
1949
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +02001950/**
1951 * ccw_driver_register() - register a ccw driver
1952 * @cdriver: driver to be registered
1953 *
1954 * This function is mainly a wrapper around driver_register().
1955 * Returns:
1956 * %0 on success and a negative error value on failure.
1957 */
1958int ccw_driver_register(struct ccw_driver *cdriver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959{
1960 struct device_driver *drv = &cdriver->driver;
1961
1962 drv->bus = &ccw_bus_type;
1963 drv->name = cdriver->name;
Cornelia Huck4beee642008-01-26 14:10:47 +01001964 drv->owner = cdriver->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965
1966 return driver_register(drv);
1967}
1968
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +02001969/**
1970 * ccw_driver_unregister() - deregister a ccw driver
1971 * @cdriver: driver to be deregistered
1972 *
1973 * This function is mainly a wrapper around driver_unregister().
1974 */
1975void ccw_driver_unregister(struct ccw_driver *cdriver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976{
1977 driver_unregister(&cdriver->driver);
1978}
1979
Cornelia Hucka8237fc2006-01-06 00:19:21 -08001980/* Helper func for qdio. */
1981struct subchannel_id
1982ccw_device_get_subchannel_id(struct ccw_device *cdev)
1983{
1984 struct subchannel *sch;
1985
1986 sch = to_subchannel(cdev->dev.parent);
1987 return sch->schid;
1988}
1989
Peter Oberparleiter37de53b2009-12-07 12:51:19 +01001990static void ccw_device_todo(struct work_struct *work)
1991{
1992 struct ccw_device_private *priv;
1993 struct ccw_device *cdev;
1994 struct subchannel *sch;
1995 enum cdev_todo todo;
1996
1997 priv = container_of(work, struct ccw_device_private, todo_work);
1998 cdev = priv->cdev;
1999 sch = to_subchannel(cdev->dev.parent);
2000 /* Find out todo. */
2001 spin_lock_irq(cdev->ccwlock);
2002 todo = priv->todo;
2003 priv->todo = CDEV_TODO_NOTHING;
2004 CIO_MSG_EVENT(4, "cdev_todo: cdev=0.%x.%04x todo=%d\n",
2005 priv->dev_id.ssid, priv->dev_id.devno, todo);
2006 spin_unlock_irq(cdev->ccwlock);
2007 /* Perform todo. */
2008 switch (todo) {
2009 case CDEV_TODO_ENABLE_CMF:
2010 cmf_reenable(cdev);
2011 break;
2012 case CDEV_TODO_REBIND:
2013 ccw_device_do_unbind_bind(cdev);
2014 break;
2015 case CDEV_TODO_REGISTER:
2016 io_subchannel_register(cdev);
2017 break;
2018 case CDEV_TODO_UNREG_EVAL:
2019 if (!sch_is_pseudo_sch(sch))
2020 css_schedule_eval(sch->schid);
2021 /* fall-through */
2022 case CDEV_TODO_UNREG:
2023 if (sch_is_pseudo_sch(sch))
2024 ccw_device_unregister(cdev);
2025 else
2026 ccw_device_call_sch_unregister(cdev);
2027 break;
2028 default:
2029 break;
2030 }
2031 /* Release workqueue ref. */
2032 put_device(&cdev->dev);
2033}
2034
2035/**
2036 * ccw_device_sched_todo - schedule ccw device operation
2037 * @cdev: ccw device
2038 * @todo: todo
2039 *
2040 * Schedule the operation identified by @todo to be performed on the slow path
2041 * workqueue. Do nothing if another operation with higher priority is already
2042 * scheduled. Needs to be called with ccwdev lock held.
2043 */
2044void ccw_device_sched_todo(struct ccw_device *cdev, enum cdev_todo todo)
2045{
2046 CIO_MSG_EVENT(4, "cdev_todo: sched cdev=0.%x.%04x todo=%d\n",
2047 cdev->private->dev_id.ssid, cdev->private->dev_id.devno,
2048 todo);
2049 if (cdev->private->todo >= todo)
2050 return;
2051 cdev->private->todo = todo;
2052 /* Get workqueue ref. */
2053 if (!get_device(&cdev->dev))
2054 return;
Sebastian Ottbe5d3822010-02-26 22:37:24 +01002055 if (!queue_work(cio_work_q, &cdev->private->todo_work)) {
Peter Oberparleiter37de53b2009-12-07 12:51:19 +01002056 /* Already queued, release workqueue ref. */
2057 put_device(&cdev->dev);
2058 }
2059}
2060
Michael Ernstfd0457a2010-08-09 18:12:50 +02002061/**
2062 * ccw_device_siosl() - initiate logging
2063 * @cdev: ccw device
2064 *
2065 * This function is used to invoke model-dependent logging within the channel
2066 * subsystem.
2067 */
2068int ccw_device_siosl(struct ccw_device *cdev)
2069{
2070 struct subchannel *sch = to_subchannel(cdev->dev.parent);
2071
2072 return chsc_siosl(sch->schid);
2073}
2074EXPORT_SYMBOL_GPL(ccw_device_siosl);
2075
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076MODULE_LICENSE("GPL");
2077EXPORT_SYMBOL(ccw_device_set_online);
2078EXPORT_SYMBOL(ccw_device_set_offline);
2079EXPORT_SYMBOL(ccw_driver_register);
2080EXPORT_SYMBOL(ccw_driver_unregister);
2081EXPORT_SYMBOL(get_ccwdev_by_busid);
2082EXPORT_SYMBOL(ccw_bus_type);
Cornelia Hucka8237fc2006-01-06 00:19:21 -08002083EXPORT_SYMBOL_GPL(ccw_device_get_subchannel_id);