blob: 1ab5f6c36d9b4439db490797ca62aa239f04f7fa [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * bus driver for ccw devices
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
Heiko Carstensa53c8fa2012-07-20 11:15:04 +02004 * Copyright IBM Corp. 2002, 2008
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * Author(s): Arnd Bergmann (arndb@de.ibm.com)
Cornelia Huck4ce3b302006-01-14 13:21:04 -08006 * Cornelia Huck (cornelia.huck@de.ibm.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * Martin Schwidefsky (schwidefsky@de.ibm.com)
8 */
Peter Oberparleitera7ae2c022009-12-07 12:51:20 +01009
10#define KMSG_COMPONENT "cio"
11#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
12
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/module.h>
14#include <linux/init.h>
15#include <linux/spinlock.h>
16#include <linux/errno.h>
17#include <linux/err.h>
18#include <linux/slab.h>
19#include <linux/list.h>
20#include <linux/device.h>
21#include <linux/workqueue.h>
Sebastian Ott188561a2013-04-13 12:53:21 +020022#include <linux/delay.h>
Peter Oberparleiter90ab1332008-01-26 14:10:52 +010023#include <linux/timer.h>
Peter Oberparleiterde400d62011-10-30 15:16:04 +010024#include <linux/kernel_stat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
26#include <asm/ccwdev.h>
27#include <asm/cio.h>
Tim Schmielau4e57b682005-10-30 15:03:48 -080028#include <asm/param.h> /* HZ */
Cornelia Huck1842f2b2007-10-12 16:11:22 +020029#include <asm/cmb.h>
Cornelia Huck3a3fc292008-07-14 09:58:58 +020030#include <asm/isc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +020032#include "chp.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include "cio.h"
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +010034#include "cio_debug.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include "css.h"
36#include "device.h"
37#include "ioasm.h"
Cornelia Huckcd6b4f22008-01-26 14:10:43 +010038#include "io_sch.h"
Peter Oberparleiterecf5d9e2008-10-10 21:33:06 +020039#include "blacklist.h"
Michael Ernstfd0457a2010-08-09 18:12:50 +020040#include "chsc.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Peter Oberparleiter90ab1332008-01-26 14:10:52 +010042static struct timer_list recovery_timer;
Cornelia Huck486d0a02008-02-19 15:29:23 +010043static DEFINE_SPINLOCK(recovery_lock);
Peter Oberparleiter90ab1332008-01-26 14:10:52 +010044static int recovery_phase;
45static const unsigned long recovery_delay[] = { 3, 30, 300 };
46
Sebastian Ott0ad8f714a2013-04-13 13:06:27 +020047static atomic_t ccw_device_init_count = ATOMIC_INIT(0);
48static DECLARE_WAIT_QUEUE_HEAD(ccw_device_init_wq);
49static struct bus_type ccw_bus_type;
50
Linus Torvalds1da177e2005-04-16 15:20:36 -070051/******************* bus type handling ***********************/
52
53/* The Linux driver model distinguishes between a bus type and
54 * the bus itself. Of course we only have one channel
55 * subsystem driver and one channel system per machine, but
56 * we still use the abstraction. T.R. says it's a good idea. */
57static int
58ccw_bus_match (struct device * dev, struct device_driver * drv)
59{
60 struct ccw_device *cdev = to_ccwdev(dev);
61 struct ccw_driver *cdrv = to_ccwdrv(drv);
62 const struct ccw_device_id *ids = cdrv->ids, *found;
63
64 if (!ids)
65 return 0;
66
67 found = ccw_device_id_match(ids, &cdev->id);
68 if (!found)
69 return 0;
70
71 cdev->id.driver_info = found->driver_info;
72
73 return 1;
74}
75
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +020076/* Store modalias string delimited by prefix/suffix string into buffer with
77 * specified size. Return length of resulting string (excluding trailing '\0')
78 * even if string doesn't fit buffer (snprintf semantics). */
Cornelia Huckcfbe9bb2007-04-27 16:01:32 +020079static int snprint_alias(char *buf, size_t size,
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +020080 struct ccw_device_id *id, const char *suffix)
81{
82 int len;
83
Cornelia Huckcfbe9bb2007-04-27 16:01:32 +020084 len = snprintf(buf, size, "ccw:t%04Xm%02X", id->cu_type, id->cu_model);
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +020085 if (len > size)
86 return len;
87 buf += len;
88 size -= len;
89
90 if (id->dev_type != 0)
91 len += snprintf(buf, size, "dt%04Xdm%02X%s", id->dev_type,
92 id->dev_model, suffix);
93 else
94 len += snprintf(buf, size, "dtdm%s", suffix);
95
96 return len;
97}
98
99/* Set up environment variables for ccw device uevent. Return 0 on success,
100 * non-zero otherwise. */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200101static int ccw_uevent(struct device *dev, struct kobj_uevent_env *env)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102{
103 struct ccw_device *cdev = to_ccwdev(dev);
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +0200104 struct ccw_device_id *id = &(cdev->id);
Cornelia Huckcfbe9bb2007-04-27 16:01:32 +0200105 int ret;
106 char modalias_buf[30];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +0200108 /* CU_TYPE= */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200109 ret = add_uevent_var(env, "CU_TYPE=%04X", id->cu_type);
Cornelia Huckcfbe9bb2007-04-27 16:01:32 +0200110 if (ret)
111 return ret;
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +0200112
113 /* CU_MODEL= */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200114 ret = add_uevent_var(env, "CU_MODEL=%02X", id->cu_model);
Cornelia Huckcfbe9bb2007-04-27 16:01:32 +0200115 if (ret)
116 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
118 /* The next two can be zero, that's ok for us */
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +0200119 /* DEV_TYPE= */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200120 ret = add_uevent_var(env, "DEV_TYPE=%04X", id->dev_type);
Cornelia Huckcfbe9bb2007-04-27 16:01:32 +0200121 if (ret)
122 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +0200124 /* DEV_MODEL= */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200125 ret = add_uevent_var(env, "DEV_MODEL=%02X", id->dev_model);
Cornelia Huckcfbe9bb2007-04-27 16:01:32 +0200126 if (ret)
127 return ret;
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +0200128
129 /* MODALIAS= */
Cornelia Huckcfbe9bb2007-04-27 16:01:32 +0200130 snprint_alias(modalias_buf, sizeof(modalias_buf), id, "");
Kay Sievers7eff2e72007-08-14 15:15:12 +0200131 ret = add_uevent_var(env, "MODALIAS=%s", modalias_buf);
132 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133}
134
Cornelia Huck602b20f2008-01-26 14:10:39 +0100135static void io_subchannel_irq(struct subchannel *);
136static int io_subchannel_probe(struct subchannel *);
137static int io_subchannel_remove(struct subchannel *);
Cornelia Huck8bbace72006-01-11 10:56:22 +0100138static void io_subchannel_shutdown(struct subchannel *);
Cornelia Huckc820de32008-07-14 09:58:45 +0200139static int io_subchannel_sch_event(struct subchannel *, int);
Cornelia Huck99611f82008-07-14 09:59:02 +0200140static int io_subchannel_chp_event(struct subchannel *, struct chp_link *,
141 int);
Sebastian Ott8ea7f552009-09-22 22:58:35 +0200142static void recovery_func(unsigned long data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
Cornelia Huckf08adc02008-07-14 09:59:03 +0200144static struct css_device_id io_subchannel_ids[] = {
145 { .match_flags = 0x1, .type = SUBCHANNEL_TYPE_IO, },
146 { /* end of list */ },
147};
148MODULE_DEVICE_TABLE(css, io_subchannel_ids);
149
Cornelia Huck93a27592009-06-16 10:30:23 +0200150static int io_subchannel_prepare(struct subchannel *sch)
151{
152 struct ccw_device *cdev;
153 /*
154 * Don't allow suspend while a ccw device registration
155 * is still outstanding.
156 */
157 cdev = sch_get_cdev(sch);
158 if (cdev && !device_is_registered(&cdev->dev))
159 return -EAGAIN;
160 return 0;
161}
162
Sebastian Ottb4c70722010-02-26 22:37:27 +0100163static int io_subchannel_settle(void)
Sebastian Ott8ea7f552009-09-22 22:58:35 +0200164{
Sebastian Ottb4c70722010-02-26 22:37:27 +0100165 int ret;
166
167 ret = wait_event_interruptible(ccw_device_init_wq,
168 atomic_read(&ccw_device_init_count) == 0);
169 if (ret)
170 return -EINTR;
Sebastian Ottbe5d3822010-02-26 22:37:24 +0100171 flush_workqueue(cio_work_q);
Sebastian Ottb4c70722010-02-26 22:37:27 +0100172 return 0;
Sebastian Ott8ea7f552009-09-22 22:58:35 +0200173}
174
Cornelia Huckf7e5d672007-05-10 15:45:43 +0200175static struct css_driver io_subchannel_driver = {
Sebastian Otte6aed122011-03-15 17:08:30 +0100176 .drv = {
177 .owner = THIS_MODULE,
178 .name = "io_subchannel",
179 },
Cornelia Huckf08adc02008-07-14 09:59:03 +0200180 .subchannel_type = io_subchannel_ids,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 .irq = io_subchannel_irq,
Cornelia Huckc820de32008-07-14 09:58:45 +0200182 .sch_event = io_subchannel_sch_event,
183 .chp_event = io_subchannel_chp_event,
Cornelia Huck8bbace72006-01-11 10:56:22 +0100184 .probe = io_subchannel_probe,
185 .remove = io_subchannel_remove,
186 .shutdown = io_subchannel_shutdown,
Cornelia Huck93a27592009-06-16 10:30:23 +0200187 .prepare = io_subchannel_prepare,
Sebastian Ott8ea7f552009-09-22 22:58:35 +0200188 .settle = io_subchannel_settle,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189};
190
Sebastian Ott2f176442009-09-22 22:58:33 +0200191int __init io_subchannel_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192{
193 int ret;
194
Peter Oberparleiter90ab1332008-01-26 14:10:52 +0100195 setup_timer(&recovery_timer, recovery_func, 0);
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);
Sebastian Ott7cd40312010-08-09 18:12:52 +0200490 return 0;
491 }
492 if (cdev->drv && cdev->drv->set_offline)
Sebastian Otte74fe0c2009-03-26 15:24:08 +0100493 return ccw_device_set_offline(cdev);
Sebastian Ott7cd40312010-08-09 18:12:52 +0200494 return -EINVAL;
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200495}
496
497static int online_store_recog_and_online(struct ccw_device *cdev)
498{
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200499 /* Do device recognition, if needed. */
Sebastian Ott99f6a5702009-03-31 19:16:07 +0200500 if (cdev->private->state == DEV_STATE_BOXED) {
Peter Oberparleiter1f5bd38482009-12-07 12:51:23 +0100501 spin_lock_irq(cdev->ccwlock);
Peter Oberparleiter736b5db2009-12-07 12:51:21 +0100502 ccw_device_recognition(cdev);
Peter Oberparleiter1f5bd38482009-12-07 12:51:23 +0100503 spin_unlock_irq(cdev->ccwlock);
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200504 wait_event(cdev->private->wait_q,
505 cdev->private->flags.recog_done);
Sebastian Ott156013f2009-03-31 19:16:03 +0200506 if (cdev->private->state != DEV_STATE_OFFLINE)
507 /* recognition failed */
508 return -EAGAIN;
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200509 }
510 if (cdev->drv && cdev->drv->set_online)
Sebastian Ott7cd40312010-08-09 18:12:52 +0200511 return ccw_device_set_online(cdev);
512 return -EINVAL;
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200513}
Sebastian Ott156013f2009-03-31 19:16:03 +0200514
Michael Ernstc78aa6c2008-07-14 09:59:22 +0200515static int online_store_handle_online(struct ccw_device *cdev, int force)
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200516{
517 int ret;
518
519 ret = online_store_recog_and_online(cdev);
Sebastian Ott156013f2009-03-31 19:16:03 +0200520 if (ret && !force)
Michael Ernstc78aa6c2008-07-14 09:59:22 +0200521 return ret;
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200522 if (force && cdev->private->state == DEV_STATE_BOXED) {
523 ret = ccw_device_stlck(cdev);
Michael Ernstc78aa6c2008-07-14 09:59:22 +0200524 if (ret)
525 return ret;
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200526 if (cdev->id.cu_type == 0)
527 cdev->private->state = DEV_STATE_NOT_OPER;
Sebastian Ott156013f2009-03-31 19:16:03 +0200528 ret = online_store_recog_and_online(cdev);
529 if (ret)
530 return ret;
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200531 }
Michael Ernstc78aa6c2008-07-14 09:59:22 +0200532 return 0;
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200533}
534
535static ssize_t online_store (struct device *dev, struct device_attribute *attr,
536 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537{
538 struct ccw_device *cdev = to_ccwdev(dev);
Cornelia Huck2f972202008-04-30 13:38:33 +0200539 int force, ret;
540 unsigned long i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
Peter Oberparleitera2fc8482011-04-04 09:43:32 +0200542 /* Prevent conflict between multiple on-/offline processing requests. */
Peter Oberparleiter350e9122009-12-07 12:51:28 +0100543 if (atomic_cmpxchg(&cdev->private->onoff, 0, 1) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 return -EAGAIN;
Peter Oberparleitera2fc8482011-04-04 09:43:32 +0200545 /* Prevent conflict between internal I/Os and on-/offline processing. */
546 if (!dev_fsm_final_state(cdev) &&
547 cdev->private->state != DEV_STATE_DISCONNECTED) {
548 ret = -EAGAIN;
549 goto out_onoff;
550 }
551 /* Prevent conflict between pending work and on-/offline processing.*/
552 if (work_pending(&cdev->private->todo_work)) {
553 ret = -EAGAIN;
554 goto out_onoff;
555 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
Sebastian Ott3bda0582011-03-23 10:16:02 +0100557 if (cdev->drv && !try_module_get(cdev->drv->driver.owner)) {
Peter Oberparleitera2fc8482011-04-04 09:43:32 +0200558 ret = -EINVAL;
559 goto out_onoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 }
561 if (!strncmp(buf, "force\n", count)) {
562 force = 1;
563 i = 1;
Cornelia Huck2f972202008-04-30 13:38:33 +0200564 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 } else {
566 force = 0;
Cornelia Huck2f972202008-04-30 13:38:33 +0200567 ret = strict_strtoul(buf, 16, &i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 }
Cornelia Huck2f972202008-04-30 13:38:33 +0200569 if (ret)
570 goto out;
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200571 switch (i) {
572 case 0:
Sebastian Otte74fe0c2009-03-26 15:24:08 +0100573 ret = online_store_handle_offline(cdev);
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200574 break;
575 case 1:
Michael Ernstc78aa6c2008-07-14 09:59:22 +0200576 ret = online_store_handle_online(cdev, force);
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200577 break;
578 default:
Cornelia Huck2f972202008-04-30 13:38:33 +0200579 ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 }
Cornelia Huck2f972202008-04-30 13:38:33 +0200581out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 if (cdev->drv)
Sebastian Ott3bda0582011-03-23 10:16:02 +0100583 module_put(cdev->drv->driver.owner);
Peter Oberparleitera2fc8482011-04-04 09:43:32 +0200584out_onoff:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 atomic_set(&cdev->private->onoff, 0);
Sebastian Otte74fe0c2009-03-26 15:24:08 +0100586 return (ret < 0) ? ret : count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587}
588
589static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400590available_show (struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591{
592 struct ccw_device *cdev = to_ccwdev(dev);
593 struct subchannel *sch;
594
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100595 if (ccw_device_is_orphan(cdev))
596 return sprintf(buf, "no device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 switch (cdev->private->state) {
598 case DEV_STATE_BOXED:
599 return sprintf(buf, "boxed\n");
600 case DEV_STATE_DISCONNECTED:
601 case DEV_STATE_DISCONNECTED_SENSE_ID:
602 case DEV_STATE_NOT_OPER:
603 sch = to_subchannel(dev->parent);
604 if (!sch->lpm)
605 return sprintf(buf, "no path\n");
606 else
607 return sprintf(buf, "no device\n");
608 default:
609 /* All other states considered fine. */
610 return sprintf(buf, "good\n");
611 }
612}
613
Michael Ernstfd0457a2010-08-09 18:12:50 +0200614static ssize_t
615initiate_logging(struct device *dev, struct device_attribute *attr,
616 const char *buf, size_t count)
617{
618 struct subchannel *sch = to_subchannel(dev);
619 int rc;
620
621 rc = chsc_siosl(sch->schid);
622 if (rc < 0) {
623 pr_warning("Logging for subchannel 0.%x.%04x failed with "
624 "errno=%d\n",
625 sch->schid.ssid, sch->schid.sch_no, rc);
626 return rc;
627 }
628 pr_notice("Logging for subchannel 0.%x.%04x was triggered\n",
629 sch->schid.ssid, sch->schid.sch_no);
630 return count;
631}
632
Sebastian Ott84c57ad2013-01-28 19:32:27 +0100633static ssize_t vpm_show(struct device *dev, struct device_attribute *attr,
634 char *buf)
635{
636 struct subchannel *sch = to_subchannel(dev);
637
638 return sprintf(buf, "%02x\n", sch->vpm);
639}
640
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641static DEVICE_ATTR(chpids, 0444, chpids_show, NULL);
642static DEVICE_ATTR(pimpampom, 0444, pimpampom_show, NULL);
643static DEVICE_ATTR(devtype, 0444, devtype_show, NULL);
644static DEVICE_ATTR(cutype, 0444, cutype_show, NULL);
Bastian Blankf1fc78a2005-10-30 15:00:12 -0800645static DEVICE_ATTR(modalias, 0444, modalias_show, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646static DEVICE_ATTR(online, 0644, online_show, online_store);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647static DEVICE_ATTR(availability, 0444, available_show, NULL);
Michael Ernstfd0457a2010-08-09 18:12:50 +0200648static DEVICE_ATTR(logging, 0200, NULL, initiate_logging);
Sebastian Ott84c57ad2013-01-28 19:32:27 +0100649static DEVICE_ATTR(vpm, 0444, vpm_show, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200651static struct attribute *io_subchannel_attrs[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 &dev_attr_chpids.attr,
653 &dev_attr_pimpampom.attr,
Michael Ernstfd0457a2010-08-09 18:12:50 +0200654 &dev_attr_logging.attr,
Sebastian Ott84c57ad2013-01-28 19:32:27 +0100655 &dev_attr_vpm.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 NULL,
657};
658
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200659static struct attribute_group io_subchannel_attr_group = {
660 .attrs = io_subchannel_attrs,
Cornelia Huck529192f2006-12-08 15:55:57 +0100661};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662
663static struct attribute * ccwdev_attrs[] = {
664 &dev_attr_devtype.attr,
665 &dev_attr_cutype.attr,
Bastian Blankf1fc78a2005-10-30 15:00:12 -0800666 &dev_attr_modalias.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 &dev_attr_online.attr,
668 &dev_attr_cmb_enable.attr,
669 &dev_attr_availability.attr,
670 NULL,
671};
672
673static struct attribute_group ccwdev_attr_group = {
674 .attrs = ccwdev_attrs,
675};
676
David Brownella4dbd672009-06-24 10:06:31 -0700677static const struct attribute_group *ccwdev_attr_groups[] = {
Cornelia Huckef995162007-04-27 16:01:39 +0200678 &ccwdev_attr_group,
679 NULL,
680};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
682/* this is a simple abstraction for device_register that sets the
683 * correct bus type and adds the bus specific files */
Cornelia Huck3c9da7b2006-10-27 12:39:33 +0200684static int ccw_device_register(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685{
686 struct device *dev = &cdev->dev;
687 int ret;
688
689 dev->bus = &ccw_bus_type;
Sebastian Ott3ac276f2009-09-11 10:28:27 +0200690 ret = dev_set_name(&cdev->dev, "0.%x.%04x", cdev->private->dev_id.ssid,
691 cdev->private->dev_id.devno);
692 if (ret)
693 return ret;
Sebastian Ott7d253b92009-12-07 12:51:33 +0100694 return device_add(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695}
696
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100697static int match_dev_id(struct device *dev, void *data)
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700698{
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100699 struct ccw_device *cdev = to_ccwdev(dev);
700 struct ccw_dev_id *dev_id = data;
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700701
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100702 return ccw_dev_id_is_equal(&cdev->private->dev_id, dev_id);
703}
704
Sebastian Ottb7a610f2012-05-15 17:52:07 +0200705/**
706 * get_ccwdev_by_dev_id() - obtain device from a ccw device id
707 * @dev_id: id of the device to be searched
708 *
709 * This function searches all devices attached to the ccw bus for a device
710 * matching @dev_id.
711 * Returns:
712 * If a device is found its reference count is increased and returned;
713 * else %NULL is returned.
714 */
715struct ccw_device *get_ccwdev_by_dev_id(struct ccw_dev_id *dev_id)
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100716{
717 struct device *dev;
718
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100719 dev = bus_find_device(&ccw_bus_type, NULL, dev_id, match_dev_id);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100720
721 return dev ? to_ccwdev(dev) : NULL;
722}
Sebastian Ottb7a610f2012-05-15 17:52:07 +0200723EXPORT_SYMBOL_GPL(get_ccwdev_by_dev_id);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100724
Peter Oberparleiter37de53b2009-12-07 12:51:19 +0100725static void ccw_device_do_unbind_bind(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726{
Cornelia Huckeb32ae82009-03-26 15:24:05 +0100727 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
Sebastian Ott7d253b92009-12-07 12:51:33 +0100729 if (device_is_registered(&cdev->dev)) {
Cornelia Huckeb32ae82009-03-26 15:24:05 +0100730 device_release_driver(&cdev->dev);
731 ret = device_attach(&cdev->dev);
732 WARN_ON(ret == -ENODEV);
733 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734}
735
736static void
737ccw_device_release(struct device *dev)
738{
739 struct ccw_device *cdev;
740
741 cdev = to_ccwdev(dev);
Cornelia Huck6eff2082008-12-25 13:39:07 +0100742 /* Release reference of parent subchannel. */
743 put_device(cdev->dev.parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 kfree(cdev->private);
745 kfree(cdev);
746}
747
Cornelia Huck7674da72006-12-08 15:54:21 +0100748static struct ccw_device * io_subchannel_allocate_dev(struct subchannel *sch)
749{
750 struct ccw_device *cdev;
751
752 cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
753 if (cdev) {
754 cdev->private = kzalloc(sizeof(struct ccw_device_private),
755 GFP_KERNEL | GFP_DMA);
756 if (cdev->private)
757 return cdev;
758 }
759 kfree(cdev);
760 return ERR_PTR(-ENOMEM);
761}
762
Peter Oberparleiter37de53b2009-12-07 12:51:19 +0100763static void ccw_device_todo(struct work_struct *work);
764
Cornelia Huck7674da72006-12-08 15:54:21 +0100765static int io_subchannel_initialize_dev(struct subchannel *sch,
766 struct ccw_device *cdev)
767{
768 cdev->private->cdev = cdev;
Heiko Carstens420f42e2013-01-02 15:18:18 +0100769 cdev->private->int_class = IRQIO_CIO;
Cornelia Huck7674da72006-12-08 15:54:21 +0100770 atomic_set(&cdev->private->onoff, 0);
771 cdev->dev.parent = &sch->dev;
772 cdev->dev.release = ccw_device_release;
Peter Oberparleiter37de53b2009-12-07 12:51:19 +0100773 INIT_WORK(&cdev->private->todo_work, ccw_device_todo);
Cornelia Huckef995162007-04-27 16:01:39 +0200774 cdev->dev.groups = ccwdev_attr_groups;
Cornelia Huck7674da72006-12-08 15:54:21 +0100775 /* Do first half of device_register. */
776 device_initialize(&cdev->dev);
777 if (!get_device(&sch->dev)) {
Cornelia Huck283fdd02008-12-25 13:39:10 +0100778 /* Release reference from device_initialize(). */
779 put_device(&cdev->dev);
Cornelia Huck7674da72006-12-08 15:54:21 +0100780 return -ENODEV;
781 }
Sebastian Ott24a18722009-12-07 12:51:34 +0100782 cdev->private->flags.initialized = 1;
Cornelia Huck7674da72006-12-08 15:54:21 +0100783 return 0;
784}
785
786static struct ccw_device * io_subchannel_create_ccwdev(struct subchannel *sch)
787{
788 struct ccw_device *cdev;
789 int ret;
790
791 cdev = io_subchannel_allocate_dev(sch);
792 if (!IS_ERR(cdev)) {
793 ret = io_subchannel_initialize_dev(sch, cdev);
Sebastian Ott06739a82009-08-23 18:09:04 +0200794 if (ret)
Cornelia Huck7674da72006-12-08 15:54:21 +0100795 cdev = ERR_PTR(ret);
Cornelia Huck7674da72006-12-08 15:54:21 +0100796 }
797 return cdev;
798}
799
Peter Oberparleiter736b5db2009-12-07 12:51:21 +0100800static void io_subchannel_recog(struct ccw_device *, struct subchannel *);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100801
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100802static void sch_create_and_recog_new_device(struct subchannel *sch)
803{
804 struct ccw_device *cdev;
805
806 /* Need to allocate a new ccw device. */
807 cdev = io_subchannel_create_ccwdev(sch);
808 if (IS_ERR(cdev)) {
809 /* OK, we did everything we could... */
810 css_sch_device_unregister(sch);
811 return;
812 }
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100813 /* Start recognition for the new ccw device. */
Peter Oberparleiter736b5db2009-12-07 12:51:21 +0100814 io_subchannel_recog(cdev, sch);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100815}
816
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817/*
818 * Register recognized device.
819 */
Peter Oberparleiter37de53b2009-12-07 12:51:19 +0100820static void io_subchannel_register(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 struct subchannel *sch;
Sebastian Otta2901562010-03-08 12:25:17 +0100823 int ret, adjust_init_count = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 unsigned long flags;
825
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 sch = to_subchannel(cdev->dev.parent);
Cornelia Huck5fb6b852008-12-25 13:39:08 +0100827 /*
828 * Check if subchannel is still registered. It may have become
829 * unregistered if a machine check hit us after finishing
830 * device recognition but before the register work could be
831 * queued.
832 */
833 if (!device_is_registered(&sch->dev))
834 goto out_err;
Cornelia Huck82b7ac02007-04-27 16:01:36 +0200835 css_update_ssd_info(sch);
Cornelia Huck47af5512006-12-04 15:41:07 +0100836 /*
837 * io_subchannel_register() will also be called after device
838 * recognition has been done for a boxed device (which will already
839 * be registered). We need to reprobe since we may now have sense id
840 * information.
841 */
Cornelia Huckd6a30762008-12-25 13:39:11 +0100842 if (device_is_registered(&cdev->dev)) {
Cornelia Huck47af5512006-12-04 15:41:07 +0100843 if (!cdev->drv) {
844 ret = device_reprobe(&cdev->dev);
845 if (ret)
846 /* We can't do much here. */
Michael Ernst139b83dd2008-05-07 09:22:54 +0200847 CIO_MSG_EVENT(0, "device_reprobe() returned"
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200848 " %d for 0.%x.%04x\n", ret,
849 cdev->private->dev_id.ssid,
850 cdev->private->dev_id.devno);
Cornelia Huck47af5512006-12-04 15:41:07 +0100851 }
Sebastian Otta2901562010-03-08 12:25:17 +0100852 adjust_init_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 goto out;
854 }
Cornelia Huckfa1a8c22007-04-26 00:12:03 -0700855 /*
856 * Now we know this subchannel will stay, we can throw
857 * our delayed uevent.
858 */
Ming Leif67f1292009-03-01 21:10:49 +0800859 dev_set_uevent_suppress(&sch->dev, 0);
Cornelia Huckfa1a8c22007-04-26 00:12:03 -0700860 kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 /* make it known to the system */
862 ret = ccw_device_register(cdev);
863 if (ret) {
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200864 CIO_MSG_EVENT(0, "Could not register ccw dev 0.%x.%04x: %d\n",
865 cdev->private->dev_id.ssid,
866 cdev->private->dev_id.devno, ret);
Cornelia Huck2ec22982006-12-08 15:54:26 +0100867 spin_lock_irqsave(sch->lock, flags);
Cornelia Huckdb6a6422008-01-26 14:10:46 +0100868 sch_set_cdev(sch, NULL);
Cornelia Huck2ec22982006-12-08 15:54:26 +0100869 spin_unlock_irqrestore(sch->lock, flags);
Cornelia Huck6eff2082008-12-25 13:39:07 +0100870 /* Release initial device reference. */
871 put_device(&cdev->dev);
Cornelia Huck5fb6b852008-12-25 13:39:08 +0100872 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874out:
875 cdev->private->flags.recog_done = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 wake_up(&cdev->private->wait_q);
Cornelia Huck5fb6b852008-12-25 13:39:08 +0100877out_err:
Sebastian Otta2901562010-03-08 12:25:17 +0100878 if (adjust_init_count && atomic_dec_and_test(&ccw_device_init_count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 wake_up(&ccw_device_init_wq);
880}
881
Peter Oberparleiter37de53b2009-12-07 12:51:19 +0100882static void ccw_device_call_sch_unregister(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 struct subchannel *sch;
885
Peter Oberparleiter46fbe4e2008-10-10 21:33:05 +0200886 /* Get subchannel reference for local processing. */
887 if (!get_device(cdev->dev.parent))
888 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 sch = to_subchannel(cdev->dev.parent);
Cornelia Huck6ab48792006-07-12 16:39:50 +0200890 css_sch_device_unregister(sch);
Peter Oberparleiter46fbe4e2008-10-10 21:33:05 +0200891 /* Release subchannel reference for local processing. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 put_device(&sch->dev);
893}
894
895/*
896 * subchannel recognition done. Called from the state machine.
897 */
898void
899io_subchannel_recog_done(struct ccw_device *cdev)
900{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 if (css_init_done == 0) {
902 cdev->private->flags.recog_done = 1;
903 return;
904 }
905 switch (cdev->private->state) {
Sebastian Ott47593bf2009-03-31 19:16:05 +0200906 case DEV_STATE_BOXED:
907 /* Device did not respond in time. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 case DEV_STATE_NOT_OPER:
909 cdev->private->flags.recog_done = 1;
Peter Oberparleiter37de53b2009-12-07 12:51:19 +0100910 /* Remove device found not operational. */
911 ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 if (atomic_dec_and_test(&ccw_device_init_count))
913 wake_up(&ccw_device_init_wq);
914 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 case DEV_STATE_OFFLINE:
916 /*
917 * We can't register the device in interrupt context so
918 * we schedule a work item.
919 */
Peter Oberparleiter37de53b2009-12-07 12:51:19 +0100920 ccw_device_sched_todo(cdev, CDEV_TODO_REGISTER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 break;
922 }
923}
924
Peter Oberparleiter736b5db2009-12-07 12:51:21 +0100925static void io_subchannel_recog(struct ccw_device *cdev, struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 struct ccw_device_private *priv;
928
Cornelia Huck2ec22982006-12-08 15:54:26 +0100929 cdev->ccwlock = sch->lock;
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800930
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 /* Init private data. */
932 priv = cdev->private;
Cornelia Huck78964262006-10-11 15:31:38 +0200933 priv->dev_id.devno = sch->schib.pmcw.dev;
934 priv->dev_id.ssid = sch->schid.ssid;
935 priv->schid = sch->schid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 priv->state = DEV_STATE_NOT_OPER;
937 INIT_LIST_HEAD(&priv->cmb_list);
938 init_waitqueue_head(&priv->wait_q);
939 init_timer(&priv->timer);
940
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 /* Increase counter of devices currently in recognition. */
942 atomic_inc(&ccw_device_init_count);
943
944 /* Start async. device sensing. */
Cornelia Huck2ec22982006-12-08 15:54:26 +0100945 spin_lock_irq(sch->lock);
Peter Oberparleiter60e4dac2009-12-07 12:51:16 +0100946 sch_set_cdev(sch, cdev);
Peter Oberparleiter736b5db2009-12-07 12:51:21 +0100947 ccw_device_recognition(cdev);
Cornelia Huck2ec22982006-12-08 15:54:26 +0100948 spin_unlock_irq(sch->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949}
950
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100951static int ccw_device_move_to_sch(struct ccw_device *cdev,
952 struct subchannel *sch)
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100953{
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100954 struct subchannel *old_sch;
Sebastian Ott0c609fc2009-12-07 12:51:37 +0100955 int rc, old_enabled = 0;
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100956
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100957 old_sch = to_subchannel(cdev->dev.parent);
958 /* Obtain child reference for new parent. */
Cornelia Huck6eff2082008-12-25 13:39:07 +0100959 if (!get_device(&sch->dev))
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100960 return -ENODEV;
Sebastian Ott0c609fc2009-12-07 12:51:37 +0100961
962 if (!sch_is_pseudo_sch(old_sch)) {
963 spin_lock_irq(old_sch->lock);
964 old_enabled = old_sch->schib.pmcw.ena;
965 rc = 0;
966 if (old_enabled)
967 rc = cio_disable_subchannel(old_sch);
968 spin_unlock_irq(old_sch->lock);
969 if (rc == -EBUSY) {
970 /* Release child reference for new parent. */
971 put_device(&sch->dev);
972 return rc;
973 }
974 }
975
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100976 mutex_lock(&sch->reg_mutex);
Cornelia Huckffa6a702009-03-04 12:44:00 +0100977 rc = device_move(&cdev->dev, &sch->dev, DPM_ORDER_PARENT_BEFORE_DEV);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100978 mutex_unlock(&sch->reg_mutex);
979 if (rc) {
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100980 CIO_MSG_EVENT(0, "device_move(0.%x.%04x,0.%x.%04x)=%d\n",
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100981 cdev->private->dev_id.ssid,
982 cdev->private->dev_id.devno, sch->schid.ssid,
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100983 sch->schib.pmcw.dev, rc);
Sebastian Ott0c609fc2009-12-07 12:51:37 +0100984 if (old_enabled) {
985 /* Try to reenable the old subchannel. */
986 spin_lock_irq(old_sch->lock);
987 cio_enable_subchannel(old_sch, (u32)(addr_t)old_sch);
988 spin_unlock_irq(old_sch->lock);
989 }
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100990 /* Release child reference for new parent. */
Cornelia Huck6eff2082008-12-25 13:39:07 +0100991 put_device(&sch->dev);
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100992 return rc;
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100993 }
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100994 /* Clean up old subchannel. */
995 if (!sch_is_pseudo_sch(old_sch)) {
996 spin_lock_irq(old_sch->lock);
997 sch_set_cdev(old_sch, NULL);
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100998 spin_unlock_irq(old_sch->lock);
999 css_schedule_eval(old_sch->schid);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +01001000 }
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001001 /* Release child reference for old parent. */
1002 put_device(&old_sch->dev);
1003 /* Initialize new subchannel. */
1004 spin_lock_irq(sch->lock);
1005 cdev->private->schid = sch->schid;
1006 cdev->ccwlock = sch->lock;
1007 if (!sch_is_pseudo_sch(sch))
1008 sch_set_cdev(sch, cdev);
1009 spin_unlock_irq(sch->lock);
1010 if (!sch_is_pseudo_sch(sch))
1011 css_update_ssd_info(sch);
1012 return 0;
1013}
1014
1015static int ccw_device_move_to_orph(struct ccw_device *cdev)
1016{
1017 struct subchannel *sch = to_subchannel(cdev->dev.parent);
1018 struct channel_subsystem *css = to_css(sch->dev.parent);
1019
1020 return ccw_device_move_to_sch(cdev, css->pseudo_subchannel);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +01001021}
1022
Cornelia Huck602b20f2008-01-26 14:10:39 +01001023static void io_subchannel_irq(struct subchannel *sch)
1024{
1025 struct ccw_device *cdev;
1026
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001027 cdev = sch_get_cdev(sch);
Cornelia Huck602b20f2008-01-26 14:10:39 +01001028
Sebastian Ottefd986d2009-09-11 10:28:18 +02001029 CIO_TRACE_EVENT(6, "IRQ");
1030 CIO_TRACE_EVENT(6, dev_name(&sch->dev));
Cornelia Huck602b20f2008-01-26 14:10:39 +01001031 if (cdev)
1032 dev_fsm_event(cdev, DEV_EVENT_INTERRUPT);
Peter Oberparleiterde400d62011-10-30 15:16:04 +01001033 else
Heiko Carstens420f42e2013-01-02 15:18:18 +01001034 inc_irq_stat(IRQIO_CIO);
Cornelia Huck602b20f2008-01-26 14:10:39 +01001035}
1036
Sebastian Ott13952ec2008-12-25 13:39:13 +01001037void io_subchannel_init_config(struct subchannel *sch)
1038{
1039 memset(&sch->config, 0, sizeof(sch->config));
1040 sch->config.csense = 1;
Sebastian Ott13952ec2008-12-25 13:39:13 +01001041}
1042
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001043static void io_subchannel_init_fields(struct subchannel *sch)
1044{
1045 if (cio_is_console(sch->schid))
1046 sch->opm = 0xff;
1047 else
1048 sch->opm = chp_get_sch_opm(sch);
1049 sch->lpm = sch->schib.pmcw.pam & sch->opm;
Cornelia Huck3a3fc292008-07-14 09:58:58 +02001050 sch->isc = cio_is_console(sch->schid) ? CONSOLE_ISC : IO_SCH_ISC;
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001051
1052 CIO_MSG_EVENT(6, "Detected device %04x on subchannel 0.%x.%04X"
1053 " - PIM = %02X, PAM = %02X, POM = %02X\n",
1054 sch->schib.pmcw.dev, sch->schid.ssid,
1055 sch->schid.sch_no, sch->schib.pmcw.pim,
1056 sch->schib.pmcw.pam, sch->schib.pmcw.pom);
Sebastian Ott13952ec2008-12-25 13:39:13 +01001057
1058 io_subchannel_init_config(sch);
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001059}
1060
Cornelia Huck90ed2b62008-12-25 13:39:09 +01001061/*
1062 * Note: We always return 0 so that we bind to the device even on error.
1063 * This is needed so that our remove function is called on unregister.
1064 */
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001065static int io_subchannel_probe(struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066{
Sebastian Ottf92519e2011-03-15 17:08:27 +01001067 struct io_subchannel_private *io_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 struct ccw_device *cdev;
1069 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070
Peter Oberparleiter6d7c5af2009-10-14 12:43:50 +02001071 if (cio_is_console(sch->schid)) {
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001072 rc = sysfs_create_group(&sch->dev.kobj,
1073 &io_subchannel_attr_group);
1074 if (rc)
1075 CIO_MSG_EVENT(0, "Failed to create io subchannel "
1076 "attributes for subchannel "
1077 "0.%x.%04x (rc=%d)\n",
1078 sch->schid.ssid, sch->schid.sch_no, rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 /*
Peter Oberparleiter6d7c5af2009-10-14 12:43:50 +02001080 * The console subchannel already has an associated ccw_device.
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001081 * Throw the delayed uevent for the subchannel, register
Peter Oberparleiter6d7c5af2009-10-14 12:43:50 +02001082 * the ccw_device and exit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 */
Ming Leif67f1292009-03-01 21:10:49 +08001084 dev_set_uevent_suppress(&sch->dev, 0);
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001085 kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
Peter Oberparleiter6d7c5af2009-10-14 12:43:50 +02001086 cdev = sch_get_cdev(sch);
Sebastian Ottafdfed02013-04-13 13:03:03 +02001087 rc = ccw_device_register(cdev);
1088 if (rc) {
1089 /* Release online reference. */
1090 put_device(&cdev->dev);
1091 goto out_schedule;
1092 }
Sebastian Ott0ad8f714a2013-04-13 13:06:27 +02001093 if (atomic_dec_and_test(&ccw_device_init_count))
1094 wake_up(&ccw_device_init_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 return 0;
1096 }
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001097 io_subchannel_init_fields(sch);
Sebastian Ottf444cc02008-12-25 13:39:14 +01001098 rc = cio_commit_config(sch);
1099 if (rc)
1100 goto out_schedule;
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001101 rc = sysfs_create_group(&sch->dev.kobj,
1102 &io_subchannel_attr_group);
1103 if (rc)
Cornelia Huck90ed2b62008-12-25 13:39:09 +01001104 goto out_schedule;
Cornelia Huckcd6b4f22008-01-26 14:10:43 +01001105 /* Allocate I/O subchannel private data. */
Sebastian Ottf92519e2011-03-15 17:08:27 +01001106 io_priv = kzalloc(sizeof(*io_priv), GFP_KERNEL | GFP_DMA);
1107 if (!io_priv)
Peter Oberparleiter48e4c382009-12-07 12:51:15 +01001108 goto out_schedule;
Sebastian Ottf92519e2011-03-15 17:08:27 +01001109
1110 set_io_private(sch, io_priv);
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001111 css_schedule_eval(sch->schid);
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001112 return 0;
Peter Oberparleiter48e4c382009-12-07 12:51:15 +01001113
Cornelia Huck90ed2b62008-12-25 13:39:09 +01001114out_schedule:
Peter Oberparleiter390935a2009-12-07 12:51:18 +01001115 spin_lock_irq(sch->lock);
1116 css_sched_sch_todo(sch, SCH_TODO_UNREG);
1117 spin_unlock_irq(sch->lock);
Cornelia Huck90ed2b62008-12-25 13:39:09 +01001118 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119}
1120
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121static int
Cornelia Huck8bbace72006-01-11 10:56:22 +01001122io_subchannel_remove (struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123{
Sebastian Ottf92519e2011-03-15 17:08:27 +01001124 struct io_subchannel_private *io_priv = to_io_private(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 struct ccw_device *cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001127 cdev = sch_get_cdev(sch);
1128 if (!cdev)
Peter Oberparleiter48e4c382009-12-07 12:51:15 +01001129 goto out_free;
Sebastian Ott6e9a0f62009-12-07 12:51:38 +01001130 io_subchannel_quiesce(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 /* Set ccw device to not operational and drop reference. */
Sebastian Ott7a8ad102009-12-07 12:51:39 +01001132 spin_lock_irq(cdev->ccwlock);
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001133 sch_set_cdev(sch, NULL);
Sebastian Ottf92519e2011-03-15 17:08:27 +01001134 set_io_private(sch, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 cdev->private->state = DEV_STATE_NOT_OPER;
Sebastian Ott7a8ad102009-12-07 12:51:39 +01001136 spin_unlock_irq(cdev->ccwlock);
Cornelia Huckef995162007-04-27 16:01:39 +02001137 ccw_device_unregister(cdev);
Peter Oberparleiter48e4c382009-12-07 12:51:15 +01001138out_free:
Sebastian Ottf92519e2011-03-15 17:08:27 +01001139 kfree(io_priv);
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001140 sysfs_remove_group(&sch->dev.kobj, &io_subchannel_attr_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 return 0;
1142}
1143
Cornelia Huck602b20f2008-01-26 14:10:39 +01001144static void io_subchannel_verify(struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145{
1146 struct ccw_device *cdev;
1147
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001148 cdev = sch_get_cdev(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 if (cdev)
1150 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1151}
1152
Cornelia Huckc820de32008-07-14 09:58:45 +02001153static void io_subchannel_terminate_path(struct subchannel *sch, u8 mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154{
1155 struct ccw_device *cdev;
1156
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001157 cdev = sch_get_cdev(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 if (!cdev)
1159 return;
Peter Oberparleiter4257aae2009-12-07 12:51:29 +01001160 if (cio_update_schib(sch))
1161 goto err;
1162 /* Check for I/O on path. */
1163 if (scsw_actl(&sch->schib.scsw) == 0 || sch->schib.pmcw.lpum != mask)
1164 goto out;
1165 if (cdev->private->state == DEV_STATE_ONLINE) {
1166 ccw_device_kill_io(cdev);
1167 goto out;
1168 }
1169 if (cio_clear(sch))
1170 goto err;
1171out:
1172 /* Trigger path verification. */
1173 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1174 return;
Cornelia Huckc820de32008-07-14 09:58:45 +02001175
Peter Oberparleiter4257aae2009-12-07 12:51:29 +01001176err:
1177 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
Cornelia Huckc820de32008-07-14 09:58:45 +02001178}
1179
Cornelia Huck99611f82008-07-14 09:59:02 +02001180static int io_subchannel_chp_event(struct subchannel *sch,
1181 struct chp_link *link, int event)
Cornelia Huckc820de32008-07-14 09:58:45 +02001182{
Sebastian Ott585b9542010-10-25 16:10:34 +02001183 struct ccw_device *cdev = sch_get_cdev(sch);
Cornelia Huckc820de32008-07-14 09:58:45 +02001184 int mask;
Cornelia Huckc820de32008-07-14 09:58:45 +02001185
Cornelia Huck99611f82008-07-14 09:59:02 +02001186 mask = chp_ssd_get_mask(&sch->ssd_info, link);
Cornelia Huckc820de32008-07-14 09:58:45 +02001187 if (!mask)
1188 return 0;
1189 switch (event) {
1190 case CHP_VARY_OFF:
1191 sch->opm &= ~mask;
1192 sch->lpm &= ~mask;
Sebastian Ott585b9542010-10-25 16:10:34 +02001193 if (cdev)
1194 cdev->private->path_gone_mask |= mask;
Cornelia Huckc820de32008-07-14 09:58:45 +02001195 io_subchannel_terminate_path(sch, mask);
1196 break;
1197 case CHP_VARY_ON:
1198 sch->opm |= mask;
1199 sch->lpm |= mask;
Sebastian Ott585b9542010-10-25 16:10:34 +02001200 if (cdev)
1201 cdev->private->path_new_mask |= mask;
Cornelia Huckc820de32008-07-14 09:58:45 +02001202 io_subchannel_verify(sch);
1203 break;
1204 case CHP_OFFLINE:
Sebastian Ottcdb912a2008-12-25 13:39:12 +01001205 if (cio_update_schib(sch))
Cornelia Huckc820de32008-07-14 09:58:45 +02001206 return -ENODEV;
Sebastian Ott585b9542010-10-25 16:10:34 +02001207 if (cdev)
1208 cdev->private->path_gone_mask |= mask;
Cornelia Huckc820de32008-07-14 09:58:45 +02001209 io_subchannel_terminate_path(sch, mask);
1210 break;
1211 case CHP_ONLINE:
Sebastian Ottcdb912a2008-12-25 13:39:12 +01001212 if (cio_update_schib(sch))
1213 return -ENODEV;
Cornelia Huckc820de32008-07-14 09:58:45 +02001214 sch->lpm |= mask & sch->opm;
Sebastian Ott585b9542010-10-25 16:10:34 +02001215 if (cdev)
1216 cdev->private->path_new_mask |= mask;
Cornelia Huckc820de32008-07-14 09:58:45 +02001217 io_subchannel_verify(sch);
1218 break;
1219 }
1220 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221}
1222
Sebastian Ott6e9a0f62009-12-07 12:51:38 +01001223static void io_subchannel_quiesce(struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 struct ccw_device *cdev;
1226 int ret;
1227
Sebastian Ott56e6b792009-12-07 12:51:35 +01001228 spin_lock_irq(sch->lock);
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001229 cdev = sch_get_cdev(sch);
Cornelia Hucka8237fc2006-01-06 00:19:21 -08001230 if (cio_is_console(sch->schid))
Sebastian Ott56e6b792009-12-07 12:51:35 +01001231 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 if (!sch->schib.pmcw.ena)
Sebastian Ott56e6b792009-12-07 12:51:35 +01001233 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 ret = cio_disable_subchannel(sch);
1235 if (ret != -EBUSY)
Sebastian Ott56e6b792009-12-07 12:51:35 +01001236 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 if (cdev->handler)
Sebastian Ott56e6b792009-12-07 12:51:35 +01001238 cdev->handler(cdev, cdev->private->intparm, ERR_PTR(-EIO));
1239 while (ret == -EBUSY) {
1240 cdev->private->state = DEV_STATE_QUIESCE;
Peter Oberparleiter376ae472010-10-25 16:10:44 +02001241 cdev->private->iretry = 255;
Sebastian Ott56e6b792009-12-07 12:51:35 +01001242 ret = ccw_device_cancel_halt_clear(cdev);
1243 if (ret == -EBUSY) {
1244 ccw_device_set_timeout(cdev, HZ/10);
1245 spin_unlock_irq(sch->lock);
1246 wait_event(cdev->private->wait_q,
1247 cdev->private->state != DEV_STATE_QUIESCE);
1248 spin_lock_irq(sch->lock);
1249 }
1250 ret = cio_disable_subchannel(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 }
Sebastian Ott56e6b792009-12-07 12:51:35 +01001252out_unlock:
1253 spin_unlock_irq(sch->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254}
1255
Sebastian Ott6e9a0f62009-12-07 12:51:38 +01001256static void io_subchannel_shutdown(struct subchannel *sch)
1257{
1258 io_subchannel_quiesce(sch);
1259}
1260
Cornelia Huckc820de32008-07-14 09:58:45 +02001261static int device_is_disconnected(struct ccw_device *cdev)
1262{
1263 if (!cdev)
1264 return 0;
1265 return (cdev->private->state == DEV_STATE_DISCONNECTED ||
1266 cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID);
1267}
1268
1269static int recovery_check(struct device *dev, void *data)
1270{
1271 struct ccw_device *cdev = to_ccwdev(dev);
1272 int *redo = data;
1273
1274 spin_lock_irq(cdev->ccwlock);
1275 switch (cdev->private->state) {
1276 case DEV_STATE_DISCONNECTED:
1277 CIO_MSG_EVENT(3, "recovery: trigger 0.%x.%04x\n",
1278 cdev->private->dev_id.ssid,
1279 cdev->private->dev_id.devno);
1280 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1281 *redo = 1;
1282 break;
1283 case DEV_STATE_DISCONNECTED_SENSE_ID:
1284 *redo = 1;
1285 break;
1286 }
1287 spin_unlock_irq(cdev->ccwlock);
1288
1289 return 0;
1290}
1291
1292static void recovery_work_func(struct work_struct *unused)
1293{
1294 int redo = 0;
1295
1296 bus_for_each_dev(&ccw_bus_type, NULL, &redo, recovery_check);
1297 if (redo) {
1298 spin_lock_irq(&recovery_lock);
1299 if (!timer_pending(&recovery_timer)) {
1300 if (recovery_phase < ARRAY_SIZE(recovery_delay) - 1)
1301 recovery_phase++;
1302 mod_timer(&recovery_timer, jiffies +
1303 recovery_delay[recovery_phase] * HZ);
1304 }
1305 spin_unlock_irq(&recovery_lock);
1306 } else
1307 CIO_MSG_EVENT(4, "recovery: end\n");
1308}
1309
1310static DECLARE_WORK(recovery_work, recovery_work_func);
1311
1312static void recovery_func(unsigned long data)
1313{
1314 /*
1315 * We can't do our recovery in softirq context and it's not
1316 * performance critical, so we schedule it.
1317 */
1318 schedule_work(&recovery_work);
1319}
1320
1321static void ccw_device_schedule_recovery(void)
1322{
1323 unsigned long flags;
1324
1325 CIO_MSG_EVENT(4, "recovery: schedule\n");
1326 spin_lock_irqsave(&recovery_lock, flags);
1327 if (!timer_pending(&recovery_timer) || (recovery_phase != 0)) {
1328 recovery_phase = 0;
1329 mod_timer(&recovery_timer, jiffies + recovery_delay[0] * HZ);
1330 }
1331 spin_unlock_irqrestore(&recovery_lock, flags);
1332}
1333
Peter Oberparleiterecf5d9e2008-10-10 21:33:06 +02001334static int purge_fn(struct device *dev, void *data)
1335{
1336 struct ccw_device *cdev = to_ccwdev(dev);
Peter Oberparleiter37de53b2009-12-07 12:51:19 +01001337 struct ccw_dev_id *id = &cdev->private->dev_id;
Peter Oberparleiterecf5d9e2008-10-10 21:33:06 +02001338
1339 spin_lock_irq(cdev->ccwlock);
Peter Oberparleiter37de53b2009-12-07 12:51:19 +01001340 if (is_blacklisted(id->ssid, id->devno) &&
Peter Oberparleitera2fc8482011-04-04 09:43:32 +02001341 (cdev->private->state == DEV_STATE_OFFLINE) &&
1342 (atomic_cmpxchg(&cdev->private->onoff, 0, 1) == 0)) {
Peter Oberparleiter37de53b2009-12-07 12:51:19 +01001343 CIO_MSG_EVENT(3, "ccw: purging 0.%x.%04x\n", id->ssid,
1344 id->devno);
1345 ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
Peter Oberparleitera2fc8482011-04-04 09:43:32 +02001346 atomic_set(&cdev->private->onoff, 0);
Peter Oberparleiter37de53b2009-12-07 12:51:19 +01001347 }
Peter Oberparleiterecf5d9e2008-10-10 21:33:06 +02001348 spin_unlock_irq(cdev->ccwlock);
Peter Oberparleiterecf5d9e2008-10-10 21:33:06 +02001349 /* Abort loop in case of pending signal. */
1350 if (signal_pending(current))
1351 return -EINTR;
1352
1353 return 0;
1354}
1355
1356/**
1357 * ccw_purge_blacklisted - purge unused, blacklisted devices
1358 *
1359 * Unregister all ccw devices that are offline and on the blacklist.
1360 */
1361int ccw_purge_blacklisted(void)
1362{
1363 CIO_MSG_EVENT(2, "ccw: purging blacklisted devices\n");
1364 bus_for_each_dev(&ccw_bus_type, NULL, NULL, purge_fn);
1365 return 0;
1366}
1367
Peter Oberparleiter6afcc772009-10-06 10:34:02 +02001368void ccw_device_set_disconnected(struct ccw_device *cdev)
Cornelia Huckc820de32008-07-14 09:58:45 +02001369{
1370 if (!cdev)
1371 return;
1372 ccw_device_set_timeout(cdev, 0);
1373 cdev->private->flags.fake_irb = 0;
1374 cdev->private->state = DEV_STATE_DISCONNECTED;
1375 if (cdev->online)
1376 ccw_device_schedule_recovery();
1377}
1378
Peter Oberparleiter91c36912008-08-21 19:46:39 +02001379void ccw_device_set_notoper(struct ccw_device *cdev)
1380{
1381 struct subchannel *sch = to_subchannel(cdev->dev.parent);
1382
1383 CIO_TRACE_EVENT(2, "notoper");
Cornelia Huckb9d3aed2008-10-10 21:33:11 +02001384 CIO_TRACE_EVENT(2, dev_name(&sch->dev));
Peter Oberparleiter91c36912008-08-21 19:46:39 +02001385 ccw_device_set_timeout(cdev, 0);
1386 cio_disable_subchannel(sch);
1387 cdev->private->state = DEV_STATE_NOT_OPER;
1388}
1389
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001390enum io_sch_action {
1391 IO_SCH_UNREG,
1392 IO_SCH_ORPH_UNREG,
1393 IO_SCH_ATTACH,
1394 IO_SCH_UNREG_ATTACH,
1395 IO_SCH_ORPH_ATTACH,
1396 IO_SCH_REPROBE,
1397 IO_SCH_VERIFY,
1398 IO_SCH_DISC,
1399 IO_SCH_NOP,
1400};
1401
1402static enum io_sch_action sch_get_action(struct subchannel *sch)
Cornelia Huckc820de32008-07-14 09:58:45 +02001403{
Cornelia Huckc820de32008-07-14 09:58:45 +02001404 struct ccw_device *cdev;
1405
Cornelia Huckc820de32008-07-14 09:58:45 +02001406 cdev = sch_get_cdev(sch);
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001407 if (cio_update_schib(sch)) {
1408 /* Not operational. */
1409 if (!cdev)
1410 return IO_SCH_UNREG;
Sebastian Ott76e6fb42010-02-26 22:37:28 +01001411 if (ccw_device_notify(cdev, CIO_GONE) != NOTIFY_OK)
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001412 return IO_SCH_UNREG;
1413 return IO_SCH_ORPH_UNREG;
Cornelia Huckc820de32008-07-14 09:58:45 +02001414 }
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001415 /* Operational. */
1416 if (!cdev)
1417 return IO_SCH_ATTACH;
1418 if (sch->schib.pmcw.dev != cdev->private->dev_id.devno) {
Sebastian Ott76e6fb42010-02-26 22:37:28 +01001419 if (ccw_device_notify(cdev, CIO_GONE) != NOTIFY_OK)
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001420 return IO_SCH_UNREG_ATTACH;
1421 return IO_SCH_ORPH_ATTACH;
Cornelia Huckc820de32008-07-14 09:58:45 +02001422 }
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001423 if ((sch->schib.pmcw.pam & sch->opm) == 0) {
Sebastian Ott76e6fb42010-02-26 22:37:28 +01001424 if (ccw_device_notify(cdev, CIO_NO_PATH) != NOTIFY_OK)
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001425 return IO_SCH_UNREG;
1426 return IO_SCH_DISC;
Cornelia Huckc820de32008-07-14 09:58:45 +02001427 }
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001428 if (device_is_disconnected(cdev))
1429 return IO_SCH_REPROBE;
Sebastian Ott31370f752012-10-24 11:22:52 +02001430 if (cdev->online && !cdev->private->flags.resuming)
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001431 return IO_SCH_VERIFY;
Sebastian Ott43d0be72012-09-05 14:19:42 +02001432 if (cdev->private->state == DEV_STATE_NOT_OPER)
1433 return IO_SCH_UNREG_ATTACH;
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001434 return IO_SCH_NOP;
1435}
1436
1437/**
1438 * io_subchannel_sch_event - process subchannel event
1439 * @sch: subchannel
1440 * @process: non-zero if function is called in process context
1441 *
1442 * An unspecified event occurred for this subchannel. Adjust data according
1443 * to the current operational state of the subchannel and device. Return
1444 * zero when the event has been handled sufficiently or -EAGAIN when this
1445 * function should be called again in process context.
1446 */
1447static int io_subchannel_sch_event(struct subchannel *sch, int process)
1448{
1449 unsigned long flags;
1450 struct ccw_device *cdev;
1451 struct ccw_dev_id dev_id;
1452 enum io_sch_action action;
1453 int rc = -EAGAIN;
1454
1455 spin_lock_irqsave(sch->lock, flags);
1456 if (!device_is_registered(&sch->dev))
1457 goto out_unlock;
Peter Oberparleiter390935a2009-12-07 12:51:18 +01001458 if (work_pending(&sch->todo_work))
1459 goto out_unlock;
Peter Oberparleiter37de53b2009-12-07 12:51:19 +01001460 cdev = sch_get_cdev(sch);
1461 if (cdev && work_pending(&cdev->private->todo_work))
1462 goto out_unlock;
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001463 action = sch_get_action(sch);
1464 CIO_MSG_EVENT(2, "event: sch 0.%x.%04x, process=%d, action=%d\n",
1465 sch->schid.ssid, sch->schid.sch_no, process,
1466 action);
1467 /* Perform immediate actions while holding the lock. */
Cornelia Huckc820de32008-07-14 09:58:45 +02001468 switch (action) {
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001469 case IO_SCH_REPROBE:
1470 /* Trigger device recognition. */
Cornelia Huckc820de32008-07-14 09:58:45 +02001471 ccw_device_trigger_reprobe(cdev);
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001472 rc = 0;
1473 goto out_unlock;
1474 case IO_SCH_VERIFY:
1475 /* Trigger path verification. */
1476 io_subchannel_verify(sch);
1477 rc = 0;
1478 goto out_unlock;
1479 case IO_SCH_DISC:
1480 ccw_device_set_disconnected(cdev);
1481 rc = 0;
1482 goto out_unlock;
1483 case IO_SCH_ORPH_UNREG:
1484 case IO_SCH_ORPH_ATTACH:
Peter Oberparleiter6afcc772009-10-06 10:34:02 +02001485 ccw_device_set_disconnected(cdev);
Peter Oberparleiter91c36912008-08-21 19:46:39 +02001486 break;
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001487 case IO_SCH_UNREG_ATTACH:
1488 case IO_SCH_UNREG:
Sebastian Ott16d2ce22010-11-10 10:05:53 +01001489 if (!cdev)
1490 break;
1491 if (cdev->private->state == DEV_STATE_SENSE_ID) {
1492 /*
1493 * Note: delayed work triggered by this event
1494 * and repeated calls to sch_event are synchronized
1495 * by the above check for work_pending(cdev).
1496 */
1497 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
1498 } else
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001499 ccw_device_set_notoper(cdev);
1500 break;
1501 case IO_SCH_NOP:
1502 rc = 0;
1503 goto out_unlock;
Cornelia Huckc820de32008-07-14 09:58:45 +02001504 default:
1505 break;
1506 }
1507 spin_unlock_irqrestore(sch->lock, flags);
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001508 /* All other actions require process context. */
1509 if (!process)
1510 goto out;
1511 /* Handle attached ccw device. */
1512 switch (action) {
1513 case IO_SCH_ORPH_UNREG:
1514 case IO_SCH_ORPH_ATTACH:
1515 /* Move ccw device to orphanage. */
1516 rc = ccw_device_move_to_orph(cdev);
1517 if (rc)
1518 goto out;
1519 break;
1520 case IO_SCH_UNREG_ATTACH:
Sebastian Ott3368ba22012-09-05 14:20:41 +02001521 spin_lock_irqsave(sch->lock, flags);
Sebastian Ott74b61272010-10-25 16:10:26 +02001522 if (cdev->private->flags.resuming) {
1523 /* Device will be handled later. */
1524 rc = 0;
Sebastian Ott3368ba22012-09-05 14:20:41 +02001525 goto out_unlock;
Sebastian Ott74b61272010-10-25 16:10:26 +02001526 }
Sebastian Ott3368ba22012-09-05 14:20:41 +02001527 sch_set_cdev(sch, NULL);
1528 spin_unlock_irqrestore(sch->lock, flags);
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001529 /* Unregister ccw device. */
Sebastian Ott74b61272010-10-25 16:10:26 +02001530 ccw_device_unregister(cdev);
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001531 break;
1532 default:
1533 break;
1534 }
1535 /* Handle subchannel. */
1536 switch (action) {
1537 case IO_SCH_ORPH_UNREG:
1538 case IO_SCH_UNREG:
Sebastian Ott0d01bb82010-02-26 22:37:29 +01001539 if (!cdev || !cdev->private->flags.resuming)
1540 css_sch_device_unregister(sch);
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001541 break;
1542 case IO_SCH_ORPH_ATTACH:
1543 case IO_SCH_UNREG_ATTACH:
1544 case IO_SCH_ATTACH:
1545 dev_id.ssid = sch->schid.ssid;
1546 dev_id.devno = sch->schib.pmcw.dev;
1547 cdev = get_ccwdev_by_dev_id(&dev_id);
1548 if (!cdev) {
1549 sch_create_and_recog_new_device(sch);
1550 break;
1551 }
1552 rc = ccw_device_move_to_sch(cdev, sch);
1553 if (rc) {
1554 /* Release reference from get_ccwdev_by_dev_id() */
1555 put_device(&cdev->dev);
1556 goto out;
1557 }
1558 spin_lock_irqsave(sch->lock, flags);
1559 ccw_device_trigger_reprobe(cdev);
1560 spin_unlock_irqrestore(sch->lock, flags);
1561 /* Release reference from get_ccwdev_by_dev_id() */
1562 put_device(&cdev->dev);
1563 break;
1564 default:
1565 break;
1566 }
1567 return 0;
Cornelia Huckc820de32008-07-14 09:58:45 +02001568
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001569out_unlock:
1570 spin_unlock_irqrestore(sch->lock, flags);
1571out:
1572 return rc;
Cornelia Huckc820de32008-07-14 09:58:45 +02001573}
1574
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575#ifdef CONFIG_CCW_CONSOLE
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001576static int ccw_device_console_enable(struct ccw_device *cdev,
1577 struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578{
1579 int rc;
1580
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001581 io_subchannel_init_fields(sch);
Sebastian Ottf444cc02008-12-25 13:39:14 +01001582 rc = cio_commit_config(sch);
1583 if (rc)
1584 return rc;
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001585 sch->driver = &io_subchannel_driver;
Sebastian Ottffa8d2a2009-12-18 17:43:15 +01001586 sch_set_cdev(sch, cdev);
Peter Oberparleiter736b5db2009-12-07 12:51:21 +01001587 io_subchannel_recog(cdev, sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 /* Now wait for the async. recognition to come to an end. */
1589 spin_lock_irq(cdev->ccwlock);
1590 while (!dev_fsm_final_state(cdev))
Sebastian Ott188561a2013-04-13 12:53:21 +02001591 ccw_device_wait_idle(cdev);
1592
Sebastian Ottafdfed02013-04-13 13:03:03 +02001593 /* Hold on to an extra reference while device is online. */
1594 get_device(&cdev->dev);
1595 rc = ccw_device_online(cdev);
1596 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 goto out_unlock;
Sebastian Ottafdfed02013-04-13 13:03:03 +02001598
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 while (!dev_fsm_final_state(cdev))
Sebastian Ott188561a2013-04-13 12:53:21 +02001600 ccw_device_wait_idle(cdev);
1601
Sebastian Ottafdfed02013-04-13 13:03:03 +02001602 if (cdev->private->state == DEV_STATE_ONLINE)
1603 cdev->online = 1;
1604 else
1605 rc = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606out_unlock:
1607 spin_unlock_irq(cdev->ccwlock);
Sebastian Ottafdfed02013-04-13 13:03:03 +02001608 if (rc) /* Give up online reference since onlining failed. */
1609 put_device(&cdev->dev);
Peter Oberparleiter736b5db2009-12-07 12:51:21 +01001610 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611}
1612
Sebastian Ottafdfed02013-04-13 13:03:03 +02001613struct ccw_device *ccw_device_probe_console(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614{
Sebastian Ott863fc842013-04-13 13:01:50 +02001615 struct io_subchannel_private *io_priv;
Sebastian Ottafdfed02013-04-13 13:03:03 +02001616 struct ccw_device *cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 struct subchannel *sch;
1618 int ret;
1619
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 sch = cio_probe_console();
Sebastian Ottafdfed02013-04-13 13:03:03 +02001621 if (IS_ERR(sch))
1622 return ERR_CAST(sch);
Sebastian Ott863fc842013-04-13 13:01:50 +02001623
1624 io_priv = kzalloc(sizeof(*io_priv), GFP_KERNEL | GFP_DMA);
1625 if (!io_priv) {
1626 put_device(&sch->dev);
1627 return ERR_PTR(-ENOMEM);
1628 }
Sebastian Ottafdfed02013-04-13 13:03:03 +02001629 cdev = io_subchannel_create_ccwdev(sch);
1630 if (IS_ERR(cdev)) {
1631 put_device(&sch->dev);
1632 kfree(io_priv);
1633 return cdev;
1634 }
Sebastian Ott863fc842013-04-13 13:01:50 +02001635 set_io_private(sch, io_priv);
Sebastian Ottafdfed02013-04-13 13:03:03 +02001636 ret = ccw_device_console_enable(cdev, sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 if (ret) {
Sebastian Ott863fc842013-04-13 13:01:50 +02001638 set_io_private(sch, NULL);
1639 put_device(&sch->dev);
Sebastian Ottafdfed02013-04-13 13:03:03 +02001640 put_device(&cdev->dev);
Sebastian Ott863fc842013-04-13 13:01:50 +02001641 kfree(io_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 return ERR_PTR(ret);
1643 }
Sebastian Ottafdfed02013-04-13 13:03:03 +02001644 return cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645}
Cornelia Huck1f4e7ed2008-10-10 21:33:14 +02001646
Sebastian Ott188561a2013-04-13 12:53:21 +02001647/**
1648 * ccw_device_wait_idle() - busy wait for device to become idle
1649 * @cdev: ccw device
1650 *
1651 * Poll until activity control is zero, that is, no function or data
1652 * transfer is pending/active.
1653 * Called with device lock being held.
1654 */
1655void ccw_device_wait_idle(struct ccw_device *cdev)
1656{
1657 struct subchannel *sch = to_subchannel(cdev->dev.parent);
1658
1659 while (1) {
1660 cio_tsch(sch);
1661 if (sch->schib.scsw.cmd.actl == 0)
1662 break;
1663 udelay_simple(100);
1664 }
1665}
1666
Martin Schwidefsky66648452009-06-16 10:30:28 +02001667static int ccw_device_pm_restore(struct device *dev);
1668
Sebastian Ottf10ccca2013-04-13 12:56:51 +02001669int ccw_device_force_console(struct ccw_device *cdev)
Martin Schwidefsky66648452009-06-16 10:30:28 +02001670{
Sebastian Ottf10ccca2013-04-13 12:56:51 +02001671 return ccw_device_pm_restore(&cdev->dev);
Martin Schwidefsky66648452009-06-16 10:30:28 +02001672}
1673EXPORT_SYMBOL_GPL(ccw_device_force_console);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674#endif
1675
1676/*
1677 * get ccw_device matching the busid, but only if owned by cdrv
1678 */
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001679static int
1680__ccwdev_check_busid(struct device *dev, void *id)
1681{
1682 char *bus_id;
1683
Cornelia Huck12975ae2006-10-11 15:31:47 +02001684 bus_id = id;
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001685
Kay Sievers98df67b2008-12-25 13:38:55 +01001686 return (strcmp(bus_id, dev_name(dev)) == 0);
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001687}
1688
1689
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +02001690/**
1691 * get_ccwdev_by_busid() - obtain device from a bus id
1692 * @cdrv: driver the device is owned by
1693 * @bus_id: bus id of the device to be searched
1694 *
1695 * This function searches all devices owned by @cdrv for a device with a bus
1696 * id matching @bus_id.
1697 * Returns:
1698 * If a match is found, its reference count of the found device is increased
1699 * and it is returned; else %NULL is returned.
1700 */
1701struct ccw_device *get_ccwdev_by_busid(struct ccw_driver *cdrv,
1702 const char *bus_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703{
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001704 struct device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705
Sebastian Ott9f30ea92012-01-24 13:35:02 -05001706 dev = driver_find_device(&cdrv->driver, NULL, (void *)bus_id,
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001707 __ccwdev_check_busid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708
Heiko Carstensd2c993d2006-07-12 16:41:55 +02001709 return dev ? to_ccwdev(dev) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710}
1711
1712/************************** device driver handling ************************/
1713
1714/* This is the implementation of the ccw_driver class. The probe, remove
1715 * and release methods are initially very similar to the device_driver
1716 * implementations, with the difference that they have ccw_device
1717 * arguments.
1718 *
1719 * A ccw driver also contains the information that is needed for
1720 * device matching.
1721 */
1722static int
1723ccw_device_probe (struct device *dev)
1724{
1725 struct ccw_device *cdev = to_ccwdev(dev);
1726 struct ccw_driver *cdrv = to_ccwdrv(dev->driver);
1727 int ret;
1728
1729 cdev->drv = cdrv; /* to let the driver call _set_online */
Peter Oberparleiterde400d62011-10-30 15:16:04 +01001730 /* Note: we interpret class 0 in this context as an uninitialized
1731 * field since it translates to a non-I/O interrupt class. */
1732 if (cdrv->int_class != 0)
1733 cdev->private->int_class = cdrv->int_class;
1734 else
Heiko Carstens420f42e2013-01-02 15:18:18 +01001735 cdev->private->int_class = IRQIO_CIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736
1737 ret = cdrv->probe ? cdrv->probe(cdev) : -ENODEV;
1738
1739 if (ret) {
Heiko Carstensd2c993d2006-07-12 16:41:55 +02001740 cdev->drv = NULL;
Heiko Carstens420f42e2013-01-02 15:18:18 +01001741 cdev->private->int_class = IRQIO_CIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 return ret;
1743 }
1744
1745 return 0;
1746}
1747
1748static int
1749ccw_device_remove (struct device *dev)
1750{
1751 struct ccw_device *cdev = to_ccwdev(dev);
1752 struct ccw_driver *cdrv = cdev->drv;
1753 int ret;
1754
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 if (cdrv->remove)
1756 cdrv->remove(cdev);
1757 if (cdev->online) {
1758 cdev->online = 0;
1759 spin_lock_irq(cdev->ccwlock);
1760 ret = ccw_device_offline(cdev);
1761 spin_unlock_irq(cdev->ccwlock);
1762 if (ret == 0)
1763 wait_event(cdev->private->wait_q,
1764 dev_fsm_final_state(cdev));
1765 else
Michael Ernst139b83dd2008-05-07 09:22:54 +02001766 CIO_MSG_EVENT(0, "ccw_device_offline returned %d, "
Cornelia Hucke556bbb2007-07-27 12:29:19 +02001767 "device 0.%x.%04x\n",
1768 ret, cdev->private->dev_id.ssid,
1769 cdev->private->dev_id.devno);
Cornelia Huck9cd67422008-12-25 13:39:06 +01001770 /* Give up reference obtained in ccw_device_set_online(). */
1771 put_device(&cdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 }
1773 ccw_device_set_timeout(cdev, 0);
Heiko Carstensd2c993d2006-07-12 16:41:55 +02001774 cdev->drv = NULL;
Heiko Carstens420f42e2013-01-02 15:18:18 +01001775 cdev->private->int_class = IRQIO_CIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 return 0;
1777}
1778
Cornelia Huck958974fb2007-10-12 16:11:21 +02001779static void ccw_device_shutdown(struct device *dev)
1780{
1781 struct ccw_device *cdev;
1782
1783 cdev = to_ccwdev(dev);
1784 if (cdev->drv && cdev->drv->shutdown)
1785 cdev->drv->shutdown(cdev);
Cornelia Huck1842f2b2007-10-12 16:11:22 +02001786 disable_cmf(cdev);
Cornelia Huck958974fb2007-10-12 16:11:21 +02001787}
1788
Sebastian Ott823d4942009-06-16 10:30:20 +02001789static int ccw_device_pm_prepare(struct device *dev)
1790{
1791 struct ccw_device *cdev = to_ccwdev(dev);
1792
Peter Oberparleiter37de53b2009-12-07 12:51:19 +01001793 if (work_pending(&cdev->private->todo_work))
Sebastian Ott823d4942009-06-16 10:30:20 +02001794 return -EAGAIN;
1795 /* Fail while device is being set online/offline. */
1796 if (atomic_read(&cdev->private->onoff))
1797 return -EAGAIN;
1798
1799 if (cdev->online && cdev->drv && cdev->drv->prepare)
1800 return cdev->drv->prepare(cdev);
1801
1802 return 0;
1803}
1804
1805static void ccw_device_pm_complete(struct device *dev)
1806{
1807 struct ccw_device *cdev = to_ccwdev(dev);
1808
1809 if (cdev->online && cdev->drv && cdev->drv->complete)
1810 cdev->drv->complete(cdev);
1811}
1812
1813static int ccw_device_pm_freeze(struct device *dev)
1814{
1815 struct ccw_device *cdev = to_ccwdev(dev);
1816 struct subchannel *sch = to_subchannel(cdev->dev.parent);
1817 int ret, cm_enabled;
1818
1819 /* Fail suspend while device is in transistional state. */
1820 if (!dev_fsm_final_state(cdev))
1821 return -EAGAIN;
1822 if (!cdev->online)
1823 return 0;
1824 if (cdev->drv && cdev->drv->freeze) {
1825 ret = cdev->drv->freeze(cdev);
1826 if (ret)
1827 return ret;
1828 }
1829
1830 spin_lock_irq(sch->lock);
1831 cm_enabled = cdev->private->cmb != NULL;
1832 spin_unlock_irq(sch->lock);
1833 if (cm_enabled) {
1834 /* Don't have the css write on memory. */
1835 ret = ccw_set_cmf(cdev, 0);
1836 if (ret)
1837 return ret;
1838 }
1839 /* From here on, disallow device driver I/O. */
1840 spin_lock_irq(sch->lock);
1841 ret = cio_disable_subchannel(sch);
1842 spin_unlock_irq(sch->lock);
1843
1844 return ret;
1845}
1846
1847static int ccw_device_pm_thaw(struct device *dev)
1848{
1849 struct ccw_device *cdev = to_ccwdev(dev);
1850 struct subchannel *sch = to_subchannel(cdev->dev.parent);
1851 int ret, cm_enabled;
1852
1853 if (!cdev->online)
1854 return 0;
1855
1856 spin_lock_irq(sch->lock);
1857 /* Allow device driver I/O again. */
1858 ret = cio_enable_subchannel(sch, (u32)(addr_t)sch);
1859 cm_enabled = cdev->private->cmb != NULL;
1860 spin_unlock_irq(sch->lock);
1861 if (ret)
1862 return ret;
1863
1864 if (cm_enabled) {
1865 ret = ccw_set_cmf(cdev, 1);
1866 if (ret)
1867 return ret;
1868 }
1869
1870 if (cdev->drv && cdev->drv->thaw)
1871 ret = cdev->drv->thaw(cdev);
1872
1873 return ret;
1874}
1875
1876static void __ccw_device_pm_restore(struct ccw_device *cdev)
1877{
1878 struct subchannel *sch = to_subchannel(cdev->dev.parent);
Sebastian Ott823d4942009-06-16 10:30:20 +02001879
Sebastian Ott0d01bb82010-02-26 22:37:29 +01001880 spin_lock_irq(sch->lock);
1881 if (cio_is_console(sch->schid)) {
1882 cio_enable_subchannel(sch, (u32)(addr_t)sch);
1883 goto out_unlock;
1884 }
Sebastian Ott823d4942009-06-16 10:30:20 +02001885 /*
1886 * While we were sleeping, devices may have gone or become
1887 * available again. Kick re-detection.
1888 */
Sebastian Ott823d4942009-06-16 10:30:20 +02001889 cdev->private->flags.resuming = 1;
Sebastian Ottb17295e2011-01-12 09:55:10 +01001890 cdev->private->path_new_mask = LPM_ANYPATH;
Sebastian Ott817e5002011-12-01 13:32:19 +01001891 css_sched_sch_todo(sch, SCH_TODO_EVAL);
Sebastian Ott0d01bb82010-02-26 22:37:29 +01001892 spin_unlock_irq(sch->lock);
Sebastian Ott817e5002011-12-01 13:32:19 +01001893 css_wait_for_slow_path();
Sebastian Ott0d01bb82010-02-26 22:37:29 +01001894
1895 /* cdev may have been moved to a different subchannel. */
1896 sch = to_subchannel(cdev->dev.parent);
1897 spin_lock_irq(sch->lock);
1898 if (cdev->private->state != DEV_STATE_ONLINE &&
1899 cdev->private->state != DEV_STATE_OFFLINE)
1900 goto out_unlock;
1901
Peter Oberparleiter736b5db2009-12-07 12:51:21 +01001902 ccw_device_recognition(cdev);
Sebastian Ott823d4942009-06-16 10:30:20 +02001903 spin_unlock_irq(sch->lock);
Sebastian Ott823d4942009-06-16 10:30:20 +02001904 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev) ||
1905 cdev->private->state == DEV_STATE_DISCONNECTED);
Sebastian Ott0d01bb82010-02-26 22:37:29 +01001906 spin_lock_irq(sch->lock);
1907
1908out_unlock:
Sebastian Ott823d4942009-06-16 10:30:20 +02001909 cdev->private->flags.resuming = 0;
Sebastian Ott0d01bb82010-02-26 22:37:29 +01001910 spin_unlock_irq(sch->lock);
Sebastian Ott823d4942009-06-16 10:30:20 +02001911}
1912
1913static int resume_handle_boxed(struct ccw_device *cdev)
1914{
1915 cdev->private->state = DEV_STATE_BOXED;
Sebastian Ott76e6fb42010-02-26 22:37:28 +01001916 if (ccw_device_notify(cdev, CIO_BOXED) == NOTIFY_OK)
Sebastian Ott823d4942009-06-16 10:30:20 +02001917 return 0;
Peter Oberparleiter37de53b2009-12-07 12:51:19 +01001918 ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
Sebastian Ott823d4942009-06-16 10:30:20 +02001919 return -ENODEV;
1920}
1921
1922static int resume_handle_disc(struct ccw_device *cdev)
1923{
1924 cdev->private->state = DEV_STATE_DISCONNECTED;
Sebastian Ott76e6fb42010-02-26 22:37:28 +01001925 if (ccw_device_notify(cdev, CIO_GONE) == NOTIFY_OK)
Sebastian Ott823d4942009-06-16 10:30:20 +02001926 return 0;
Peter Oberparleiter37de53b2009-12-07 12:51:19 +01001927 ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
Sebastian Ott823d4942009-06-16 10:30:20 +02001928 return -ENODEV;
1929}
1930
1931static int ccw_device_pm_restore(struct device *dev)
1932{
1933 struct ccw_device *cdev = to_ccwdev(dev);
Sebastian Ott0d01bb82010-02-26 22:37:29 +01001934 struct subchannel *sch;
1935 int ret = 0;
Sebastian Ott823d4942009-06-16 10:30:20 +02001936
1937 __ccw_device_pm_restore(cdev);
Sebastian Ott0d01bb82010-02-26 22:37:29 +01001938 sch = to_subchannel(cdev->dev.parent);
Sebastian Ott823d4942009-06-16 10:30:20 +02001939 spin_lock_irq(sch->lock);
Sebastian Ott0d01bb82010-02-26 22:37:29 +01001940 if (cio_is_console(sch->schid))
Sebastian Ott823d4942009-06-16 10:30:20 +02001941 goto out_restore;
Sebastian Ott0d01bb82010-02-26 22:37:29 +01001942
Sebastian Ott823d4942009-06-16 10:30:20 +02001943 /* check recognition results */
1944 switch (cdev->private->state) {
1945 case DEV_STATE_OFFLINE:
Sebastian Ott0d01bb82010-02-26 22:37:29 +01001946 case DEV_STATE_ONLINE:
1947 cdev->private->flags.donotify = 0;
Sebastian Ott823d4942009-06-16 10:30:20 +02001948 break;
1949 case DEV_STATE_BOXED:
1950 ret = resume_handle_boxed(cdev);
Sebastian Ott823d4942009-06-16 10:30:20 +02001951 if (ret)
Sebastian Ott0d01bb82010-02-26 22:37:29 +01001952 goto out_unlock;
Sebastian Ott823d4942009-06-16 10:30:20 +02001953 goto out_restore;
Sebastian Ott823d4942009-06-16 10:30:20 +02001954 default:
Sebastian Ott0d01bb82010-02-26 22:37:29 +01001955 ret = resume_handle_disc(cdev);
1956 if (ret)
1957 goto out_unlock;
1958 goto out_restore;
Sebastian Ott823d4942009-06-16 10:30:20 +02001959 }
1960 /* check if the device type has changed */
1961 if (!ccw_device_test_sense_data(cdev)) {
1962 ccw_device_update_sense_data(cdev);
Peter Oberparleiter37de53b2009-12-07 12:51:19 +01001963 ccw_device_sched_todo(cdev, CDEV_TODO_REBIND);
Sebastian Ott823d4942009-06-16 10:30:20 +02001964 ret = -ENODEV;
1965 goto out_unlock;
1966 }
Sebastian Ott0d01bb82010-02-26 22:37:29 +01001967 if (!cdev->online)
1968 goto out_unlock;
1969
1970 if (ccw_device_online(cdev)) {
1971 ret = resume_handle_disc(cdev);
1972 if (ret)
1973 goto out_unlock;
1974 goto out_restore;
1975 }
1976 spin_unlock_irq(sch->lock);
1977 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
1978 spin_lock_irq(sch->lock);
1979
1980 if (ccw_device_notify(cdev, CIO_OPER) == NOTIFY_BAD) {
1981 ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
1982 ret = -ENODEV;
Sebastian Ott823d4942009-06-16 10:30:20 +02001983 goto out_unlock;
1984 }
Sebastian Ott823d4942009-06-16 10:30:20 +02001985
Sebastian Ott0d01bb82010-02-26 22:37:29 +01001986 /* reenable cmf, if needed */
1987 if (cdev->private->cmb) {
1988 spin_unlock_irq(sch->lock);
Sebastian Ott823d4942009-06-16 10:30:20 +02001989 ret = ccw_set_cmf(cdev, 1);
Sebastian Ott0d01bb82010-02-26 22:37:29 +01001990 spin_lock_irq(sch->lock);
Sebastian Ott823d4942009-06-16 10:30:20 +02001991 if (ret) {
Sebastian Ottf0148242009-09-11 10:28:23 +02001992 CIO_MSG_EVENT(2, "resume: cdev 0.%x.%04x: cmf failed "
1993 "(rc=%d)\n", cdev->private->dev_id.ssid,
1994 cdev->private->dev_id.devno, ret);
Sebastian Ott823d4942009-06-16 10:30:20 +02001995 ret = 0;
1996 }
1997 }
1998
1999out_restore:
Sebastian Ott0d01bb82010-02-26 22:37:29 +01002000 spin_unlock_irq(sch->lock);
Sebastian Ott823d4942009-06-16 10:30:20 +02002001 if (cdev->online && cdev->drv && cdev->drv->restore)
2002 ret = cdev->drv->restore(cdev);
Sebastian Ott823d4942009-06-16 10:30:20 +02002003 return ret;
2004
Sebastian Ott823d4942009-06-16 10:30:20 +02002005out_unlock:
2006 spin_unlock_irq(sch->lock);
2007 return ret;
2008}
2009
Alexey Dobriyan47145212009-12-14 18:00:08 -08002010static const struct dev_pm_ops ccw_pm_ops = {
Sebastian Ott823d4942009-06-16 10:30:20 +02002011 .prepare = ccw_device_pm_prepare,
2012 .complete = ccw_device_pm_complete,
2013 .freeze = ccw_device_pm_freeze,
2014 .thaw = ccw_device_pm_thaw,
2015 .restore = ccw_device_pm_restore,
2016};
2017
Sebastian Ottd5ab5272011-03-23 10:16:03 +01002018static struct bus_type ccw_bus_type = {
Cornelia Huck8bbace72006-01-11 10:56:22 +01002019 .name = "ccw",
2020 .match = ccw_bus_match,
2021 .uevent = ccw_uevent,
2022 .probe = ccw_device_probe,
2023 .remove = ccw_device_remove,
Cornelia Huck958974fb2007-10-12 16:11:21 +02002024 .shutdown = ccw_device_shutdown,
Sebastian Ott823d4942009-06-16 10:30:20 +02002025 .pm = &ccw_pm_ops,
Cornelia Huck8bbace72006-01-11 10:56:22 +01002026};
2027
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +02002028/**
2029 * ccw_driver_register() - register a ccw driver
2030 * @cdriver: driver to be registered
2031 *
2032 * This function is mainly a wrapper around driver_register().
2033 * Returns:
2034 * %0 on success and a negative error value on failure.
2035 */
2036int ccw_driver_register(struct ccw_driver *cdriver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037{
2038 struct device_driver *drv = &cdriver->driver;
2039
2040 drv->bus = &ccw_bus_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041
2042 return driver_register(drv);
2043}
2044
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +02002045/**
2046 * ccw_driver_unregister() - deregister a ccw driver
2047 * @cdriver: driver to be deregistered
2048 *
2049 * This function is mainly a wrapper around driver_unregister().
2050 */
2051void ccw_driver_unregister(struct ccw_driver *cdriver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052{
2053 driver_unregister(&cdriver->driver);
2054}
2055
Peter Oberparleiter37de53b2009-12-07 12:51:19 +01002056static void ccw_device_todo(struct work_struct *work)
2057{
2058 struct ccw_device_private *priv;
2059 struct ccw_device *cdev;
2060 struct subchannel *sch;
2061 enum cdev_todo todo;
2062
2063 priv = container_of(work, struct ccw_device_private, todo_work);
2064 cdev = priv->cdev;
2065 sch = to_subchannel(cdev->dev.parent);
2066 /* Find out todo. */
2067 spin_lock_irq(cdev->ccwlock);
2068 todo = priv->todo;
2069 priv->todo = CDEV_TODO_NOTHING;
2070 CIO_MSG_EVENT(4, "cdev_todo: cdev=0.%x.%04x todo=%d\n",
2071 priv->dev_id.ssid, priv->dev_id.devno, todo);
2072 spin_unlock_irq(cdev->ccwlock);
2073 /* Perform todo. */
2074 switch (todo) {
2075 case CDEV_TODO_ENABLE_CMF:
2076 cmf_reenable(cdev);
2077 break;
2078 case CDEV_TODO_REBIND:
2079 ccw_device_do_unbind_bind(cdev);
2080 break;
2081 case CDEV_TODO_REGISTER:
2082 io_subchannel_register(cdev);
2083 break;
2084 case CDEV_TODO_UNREG_EVAL:
2085 if (!sch_is_pseudo_sch(sch))
2086 css_schedule_eval(sch->schid);
2087 /* fall-through */
2088 case CDEV_TODO_UNREG:
2089 if (sch_is_pseudo_sch(sch))
2090 ccw_device_unregister(cdev);
2091 else
2092 ccw_device_call_sch_unregister(cdev);
2093 break;
2094 default:
2095 break;
2096 }
2097 /* Release workqueue ref. */
2098 put_device(&cdev->dev);
2099}
2100
2101/**
2102 * ccw_device_sched_todo - schedule ccw device operation
2103 * @cdev: ccw device
2104 * @todo: todo
2105 *
2106 * Schedule the operation identified by @todo to be performed on the slow path
2107 * workqueue. Do nothing if another operation with higher priority is already
2108 * scheduled. Needs to be called with ccwdev lock held.
2109 */
2110void ccw_device_sched_todo(struct ccw_device *cdev, enum cdev_todo todo)
2111{
2112 CIO_MSG_EVENT(4, "cdev_todo: sched cdev=0.%x.%04x todo=%d\n",
2113 cdev->private->dev_id.ssid, cdev->private->dev_id.devno,
2114 todo);
2115 if (cdev->private->todo >= todo)
2116 return;
2117 cdev->private->todo = todo;
2118 /* Get workqueue ref. */
2119 if (!get_device(&cdev->dev))
2120 return;
Sebastian Ottbe5d3822010-02-26 22:37:24 +01002121 if (!queue_work(cio_work_q, &cdev->private->todo_work)) {
Peter Oberparleiter37de53b2009-12-07 12:51:19 +01002122 /* Already queued, release workqueue ref. */
2123 put_device(&cdev->dev);
2124 }
2125}
2126
Michael Ernstfd0457a2010-08-09 18:12:50 +02002127/**
2128 * ccw_device_siosl() - initiate logging
2129 * @cdev: ccw device
2130 *
2131 * This function is used to invoke model-dependent logging within the channel
2132 * subsystem.
2133 */
2134int ccw_device_siosl(struct ccw_device *cdev)
2135{
2136 struct subchannel *sch = to_subchannel(cdev->dev.parent);
2137
2138 return chsc_siosl(sch->schid);
2139}
2140EXPORT_SYMBOL_GPL(ccw_device_siosl);
2141
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142MODULE_LICENSE("GPL");
2143EXPORT_SYMBOL(ccw_device_set_online);
2144EXPORT_SYMBOL(ccw_device_set_offline);
2145EXPORT_SYMBOL(ccw_driver_register);
2146EXPORT_SYMBOL(ccw_driver_unregister);
2147EXPORT_SYMBOL(get_ccwdev_by_busid);