blob: 7ada078ffdd04b09c81cc45ddfab4057f397158d [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 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 spin_lock_irq(cdev->ccwlock);
Sebastian Ottd40f7b72009-12-07 12:51:41 +0100337 sch = to_subchannel(cdev->dev.parent);
Sebastian Ott74bd0d82013-12-16 10:51:54 +0100338 cdev->online = 0;
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) {
Joe Perchesbaebc702016-03-03 20:49:57 -0800367 pr_warn("%s: The device entered boxed state while being set offline\n",
368 dev_name(&cdev->dev));
Peter Oberparleitera7ae2c022009-12-07 12:51:20 +0100369 } else if (cdev->private->state == DEV_STATE_NOT_OPER) {
Joe Perchesbaebc702016-03-03 20:49:57 -0800370 pr_warn("%s: The device stopped operating while being set offline\n",
371 dev_name(&cdev->dev));
Peter Oberparleitera7ae2c022009-12-07 12:51:20 +0100372 }
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 Ernst139b83d2008-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) {
Joe Perchesbaebc702016-03-03 20:49:57 -0800432 pr_warn("%s: Setting the device online failed because it is boxed\n",
433 dev_name(&cdev->dev));
Peter Oberparleitera7ae2c022009-12-07 12:51:20 +0100434 } else if (cdev->private->state == DEV_STATE_NOT_OPER) {
Joe Perchesbaebc702016-03-03 20:49:57 -0800435 pr_warn("%s: Setting the device online failed because it is not operational\n",
436 dev_name(&cdev->dev));
Peter Oberparleitera7ae2c022009-12-07 12:51:20 +0100437 }
Cornelia Huck9cd67422008-12-25 13:39:06 +0100438 /* Give up online reference since onlining failed. */
439 put_device(&cdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 return -ENODEV;
Cornelia Huck9cd67422008-12-25 13:39:06 +0100441 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 spin_unlock_irq(cdev->ccwlock);
Michael Ernst217ee6c2009-09-11 10:28:21 +0200443 if (cdev->drv->set_online)
444 ret = cdev->drv->set_online(cdev);
445 if (ret)
446 goto rollback;
Sebastian Ott74bd0d82013-12-16 10:51:54 +0100447
448 spin_lock_irq(cdev->ccwlock);
Michael Ernst217ee6c2009-09-11 10:28:21 +0200449 cdev->online = 1;
Sebastian Ott74bd0d82013-12-16 10:51:54 +0100450 spin_unlock_irq(cdev->ccwlock);
Michael Ernst217ee6c2009-09-11 10:28:21 +0200451 return 0;
452
453rollback:
454 spin_lock_irq(cdev->ccwlock);
455 /* Wait until a final state or DISCONNECTED is reached */
456 while (!dev_fsm_final_state(cdev) &&
457 cdev->private->state != DEV_STATE_DISCONNECTED) {
458 spin_unlock_irq(cdev->ccwlock);
459 wait_event(cdev->private->wait_q, (dev_fsm_final_state(cdev) ||
460 cdev->private->state == DEV_STATE_DISCONNECTED));
461 spin_lock_irq(cdev->ccwlock);
462 }
463 ret2 = ccw_device_offline(cdev);
464 if (ret2)
465 goto error;
466 spin_unlock_irq(cdev->ccwlock);
467 wait_event(cdev->private->wait_q, (dev_fsm_final_state(cdev) ||
468 cdev->private->state == DEV_STATE_DISCONNECTED));
Cornelia Huck9cd67422008-12-25 13:39:06 +0100469 /* Give up online reference since onlining failed. */
470 put_device(&cdev->dev);
Michael Ernst217ee6c2009-09-11 10:28:21 +0200471 return ret;
472
473error:
474 CIO_MSG_EVENT(0, "rollback ccw_device_offline returned %d, "
475 "device 0.%x.%04x\n",
476 ret2, cdev->private->dev_id.ssid,
477 cdev->private->dev_id.devno);
478 cdev->private->state = DEV_STATE_OFFLINE;
479 spin_unlock_irq(cdev->ccwlock);
480 /* Give up online reference since onlining failed. */
481 put_device(&cdev->dev);
482 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483}
484
Sebastian Otte74fe0c2009-03-26 15:24:08 +0100485static int online_store_handle_offline(struct ccw_device *cdev)
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200486{
Peter Oberparleiter37de53b2009-12-07 12:51:19 +0100487 if (cdev->private->state == DEV_STATE_DISCONNECTED) {
488 spin_lock_irq(cdev->ccwlock);
489 ccw_device_sched_todo(cdev, CDEV_TODO_UNREG_EVAL);
490 spin_unlock_irq(cdev->ccwlock);
Sebastian Ott7cd40312010-08-09 18:12:52 +0200491 return 0;
492 }
493 if (cdev->drv && cdev->drv->set_offline)
Sebastian Otte74fe0c2009-03-26 15:24:08 +0100494 return ccw_device_set_offline(cdev);
Sebastian Ott7cd40312010-08-09 18:12:52 +0200495 return -EINVAL;
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200496}
497
498static int online_store_recog_and_online(struct ccw_device *cdev)
499{
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200500 /* Do device recognition, if needed. */
Sebastian Ott99f6a5702009-03-31 19:16:07 +0200501 if (cdev->private->state == DEV_STATE_BOXED) {
Peter Oberparleiter1f5bd382009-12-07 12:51:23 +0100502 spin_lock_irq(cdev->ccwlock);
Peter Oberparleiter736b5db2009-12-07 12:51:21 +0100503 ccw_device_recognition(cdev);
Peter Oberparleiter1f5bd382009-12-07 12:51:23 +0100504 spin_unlock_irq(cdev->ccwlock);
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200505 wait_event(cdev->private->wait_q,
506 cdev->private->flags.recog_done);
Sebastian Ott156013f2009-03-31 19:16:03 +0200507 if (cdev->private->state != DEV_STATE_OFFLINE)
508 /* recognition failed */
509 return -EAGAIN;
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200510 }
511 if (cdev->drv && cdev->drv->set_online)
Sebastian Ott7cd40312010-08-09 18:12:52 +0200512 return ccw_device_set_online(cdev);
513 return -EINVAL;
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200514}
Sebastian Ott156013f2009-03-31 19:16:03 +0200515
Michael Ernstc78aa6c2008-07-14 09:59:22 +0200516static int online_store_handle_online(struct ccw_device *cdev, int force)
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200517{
518 int ret;
519
520 ret = online_store_recog_and_online(cdev);
Sebastian Ott156013f2009-03-31 19:16:03 +0200521 if (ret && !force)
Michael Ernstc78aa6c2008-07-14 09:59:22 +0200522 return ret;
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200523 if (force && cdev->private->state == DEV_STATE_BOXED) {
524 ret = ccw_device_stlck(cdev);
Michael Ernstc78aa6c2008-07-14 09:59:22 +0200525 if (ret)
526 return ret;
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200527 if (cdev->id.cu_type == 0)
528 cdev->private->state = DEV_STATE_NOT_OPER;
Sebastian Ott156013f2009-03-31 19:16:03 +0200529 ret = online_store_recog_and_online(cdev);
530 if (ret)
531 return ret;
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200532 }
Michael Ernstc78aa6c2008-07-14 09:59:22 +0200533 return 0;
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200534}
535
536static ssize_t online_store (struct device *dev, struct device_attribute *attr,
537 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538{
539 struct ccw_device *cdev = to_ccwdev(dev);
Cornelia Huck2f972202008-04-30 13:38:33 +0200540 int force, ret;
541 unsigned long i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
Peter Oberparleitera2fc8482011-04-04 09:43:32 +0200543 /* Prevent conflict between multiple on-/offline processing requests. */
Peter Oberparleiter350e9122009-12-07 12:51:28 +0100544 if (atomic_cmpxchg(&cdev->private->onoff, 0, 1) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 return -EAGAIN;
Peter Oberparleitera2fc8482011-04-04 09:43:32 +0200546 /* Prevent conflict between internal I/Os and on-/offline processing. */
547 if (!dev_fsm_final_state(cdev) &&
548 cdev->private->state != DEV_STATE_DISCONNECTED) {
549 ret = -EAGAIN;
Sebastian Ott00381ee2013-12-16 10:54:13 +0100550 goto out;
Peter Oberparleitera2fc8482011-04-04 09:43:32 +0200551 }
552 /* Prevent conflict between pending work and on-/offline processing.*/
553 if (work_pending(&cdev->private->todo_work)) {
554 ret = -EAGAIN;
Sebastian Ott00381ee2013-12-16 10:54:13 +0100555 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 }
557 if (!strncmp(buf, "force\n", count)) {
558 force = 1;
559 i = 1;
Cornelia Huck2f972202008-04-30 13:38:33 +0200560 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 } else {
562 force = 0;
Jingoo Han01787222013-07-22 10:18:15 +0900563 ret = kstrtoul(buf, 16, &i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 }
Cornelia Huck2f972202008-04-30 13:38:33 +0200565 if (ret)
566 goto out;
Sebastian Ott00381ee2013-12-16 10:54:13 +0100567
568 device_lock(dev);
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200569 switch (i) {
570 case 0:
Sebastian Otte74fe0c2009-03-26 15:24:08 +0100571 ret = online_store_handle_offline(cdev);
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200572 break;
573 case 1:
Michael Ernstc78aa6c2008-07-14 09:59:22 +0200574 ret = online_store_handle_online(cdev, force);
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200575 break;
576 default:
Cornelia Huck2f972202008-04-30 13:38:33 +0200577 ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 }
Sebastian Ott00381ee2013-12-16 10:54:13 +0100579 device_unlock(dev);
580
Cornelia Huck2f972202008-04-30 13:38:33 +0200581out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 atomic_set(&cdev->private->onoff, 0);
Sebastian Otte74fe0c2009-03-26 15:24:08 +0100583 return (ret < 0) ? ret : count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584}
585
586static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400587available_show (struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588{
589 struct ccw_device *cdev = to_ccwdev(dev);
590 struct subchannel *sch;
591
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100592 if (ccw_device_is_orphan(cdev))
593 return sprintf(buf, "no device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 switch (cdev->private->state) {
595 case DEV_STATE_BOXED:
596 return sprintf(buf, "boxed\n");
597 case DEV_STATE_DISCONNECTED:
598 case DEV_STATE_DISCONNECTED_SENSE_ID:
599 case DEV_STATE_NOT_OPER:
600 sch = to_subchannel(dev->parent);
601 if (!sch->lpm)
602 return sprintf(buf, "no path\n");
603 else
604 return sprintf(buf, "no device\n");
605 default:
606 /* All other states considered fine. */
607 return sprintf(buf, "good\n");
608 }
609}
610
Michael Ernstfd0457a2010-08-09 18:12:50 +0200611static ssize_t
612initiate_logging(struct device *dev, struct device_attribute *attr,
613 const char *buf, size_t count)
614{
615 struct subchannel *sch = to_subchannel(dev);
616 int rc;
617
618 rc = chsc_siosl(sch->schid);
619 if (rc < 0) {
Joe Perchesbaebc702016-03-03 20:49:57 -0800620 pr_warn("Logging for subchannel 0.%x.%04x failed with errno=%d\n",
621 sch->schid.ssid, sch->schid.sch_no, rc);
Michael Ernstfd0457a2010-08-09 18:12:50 +0200622 return rc;
623 }
624 pr_notice("Logging for subchannel 0.%x.%04x was triggered\n",
625 sch->schid.ssid, sch->schid.sch_no);
626 return count;
627}
628
Sebastian Ott84c57ad2013-01-28 19:32:27 +0100629static ssize_t vpm_show(struct device *dev, struct device_attribute *attr,
630 char *buf)
631{
632 struct subchannel *sch = to_subchannel(dev);
633
634 return sprintf(buf, "%02x\n", sch->vpm);
635}
636
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637static DEVICE_ATTR(chpids, 0444, chpids_show, NULL);
638static DEVICE_ATTR(pimpampom, 0444, pimpampom_show, NULL);
639static DEVICE_ATTR(devtype, 0444, devtype_show, NULL);
640static DEVICE_ATTR(cutype, 0444, cutype_show, NULL);
Bastian Blankf1fc78a2005-10-30 15:00:12 -0800641static DEVICE_ATTR(modalias, 0444, modalias_show, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642static DEVICE_ATTR(online, 0644, online_show, online_store);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643static DEVICE_ATTR(availability, 0444, available_show, NULL);
Michael Ernstfd0457a2010-08-09 18:12:50 +0200644static DEVICE_ATTR(logging, 0200, NULL, initiate_logging);
Sebastian Ott84c57ad2013-01-28 19:32:27 +0100645static DEVICE_ATTR(vpm, 0444, vpm_show, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200647static struct attribute *io_subchannel_attrs[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 &dev_attr_chpids.attr,
649 &dev_attr_pimpampom.attr,
Michael Ernstfd0457a2010-08-09 18:12:50 +0200650 &dev_attr_logging.attr,
Sebastian Ott84c57ad2013-01-28 19:32:27 +0100651 &dev_attr_vpm.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 NULL,
653};
654
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200655static struct attribute_group io_subchannel_attr_group = {
656 .attrs = io_subchannel_attrs,
Cornelia Huck529192f2006-12-08 15:55:57 +0100657};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
659static struct attribute * ccwdev_attrs[] = {
660 &dev_attr_devtype.attr,
661 &dev_attr_cutype.attr,
Bastian Blankf1fc78a2005-10-30 15:00:12 -0800662 &dev_attr_modalias.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 &dev_attr_online.attr,
664 &dev_attr_cmb_enable.attr,
665 &dev_attr_availability.attr,
666 NULL,
667};
668
669static struct attribute_group ccwdev_attr_group = {
670 .attrs = ccwdev_attrs,
671};
672
David Brownella4dbd672009-06-24 10:06:31 -0700673static const struct attribute_group *ccwdev_attr_groups[] = {
Cornelia Huckef995162007-04-27 16:01:39 +0200674 &ccwdev_attr_group,
675 NULL,
676};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
Sebastian Ott2c3e7e12014-06-11 13:06:57 +0200678static int ccw_device_add(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679{
680 struct device *dev = &cdev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
682 dev->bus = &ccw_bus_type;
Sebastian Ott7d253b92009-12-07 12:51:33 +0100683 return device_add(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684}
685
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100686static int match_dev_id(struct device *dev, void *data)
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700687{
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100688 struct ccw_device *cdev = to_ccwdev(dev);
689 struct ccw_dev_id *dev_id = data;
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700690
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100691 return ccw_dev_id_is_equal(&cdev->private->dev_id, dev_id);
692}
693
Sebastian Ottb7a610f2012-05-15 17:52:07 +0200694/**
695 * get_ccwdev_by_dev_id() - obtain device from a ccw device id
696 * @dev_id: id of the device to be searched
697 *
698 * This function searches all devices attached to the ccw bus for a device
699 * matching @dev_id.
700 * Returns:
701 * If a device is found its reference count is increased and returned;
702 * else %NULL is returned.
703 */
704struct ccw_device *get_ccwdev_by_dev_id(struct ccw_dev_id *dev_id)
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100705{
706 struct device *dev;
707
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100708 dev = bus_find_device(&ccw_bus_type, NULL, dev_id, match_dev_id);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100709
710 return dev ? to_ccwdev(dev) : NULL;
711}
Sebastian Ottb7a610f2012-05-15 17:52:07 +0200712EXPORT_SYMBOL_GPL(get_ccwdev_by_dev_id);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100713
Peter Oberparleiter37de53b2009-12-07 12:51:19 +0100714static void ccw_device_do_unbind_bind(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715{
Cornelia Huckeb32ae82009-03-26 15:24:05 +0100716 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
Sebastian Ott7d253b92009-12-07 12:51:33 +0100718 if (device_is_registered(&cdev->dev)) {
Cornelia Huckeb32ae82009-03-26 15:24:05 +0100719 device_release_driver(&cdev->dev);
720 ret = device_attach(&cdev->dev);
721 WARN_ON(ret == -ENODEV);
722 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723}
724
725static void
726ccw_device_release(struct device *dev)
727{
728 struct ccw_device *cdev;
729
730 cdev = to_ccwdev(dev);
Cornelia Huck6eff2082008-12-25 13:39:07 +0100731 /* Release reference of parent subchannel. */
732 put_device(cdev->dev.parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 kfree(cdev->private);
734 kfree(cdev);
735}
736
Cornelia Huck7674da72006-12-08 15:54:21 +0100737static struct ccw_device * io_subchannel_allocate_dev(struct subchannel *sch)
738{
739 struct ccw_device *cdev;
740
741 cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
742 if (cdev) {
743 cdev->private = kzalloc(sizeof(struct ccw_device_private),
744 GFP_KERNEL | GFP_DMA);
745 if (cdev->private)
746 return cdev;
747 }
748 kfree(cdev);
749 return ERR_PTR(-ENOMEM);
750}
751
Peter Oberparleiter37de53b2009-12-07 12:51:19 +0100752static void ccw_device_todo(struct work_struct *work);
753
Cornelia Huck7674da72006-12-08 15:54:21 +0100754static int io_subchannel_initialize_dev(struct subchannel *sch,
755 struct ccw_device *cdev)
756{
Sebastian Ott2c3e7e12014-06-11 13:06:57 +0200757 struct ccw_device_private *priv = cdev->private;
758 int ret;
759
760 priv->cdev = cdev;
761 priv->int_class = IRQIO_CIO;
762 priv->state = DEV_STATE_NOT_OPER;
763 priv->dev_id.devno = sch->schib.pmcw.dev;
764 priv->dev_id.ssid = sch->schid.ssid;
765 priv->schid = sch->schid;
766
767 INIT_WORK(&priv->todo_work, ccw_device_todo);
768 INIT_LIST_HEAD(&priv->cmb_list);
769 init_waitqueue_head(&priv->wait_q);
770 init_timer(&priv->timer);
771
772 atomic_set(&priv->onoff, 0);
773 cdev->ccwlock = sch->lock;
Cornelia Huck7674da72006-12-08 15:54:21 +0100774 cdev->dev.parent = &sch->dev;
775 cdev->dev.release = ccw_device_release;
Cornelia Huckef995162007-04-27 16:01:39 +0200776 cdev->dev.groups = ccwdev_attr_groups;
Cornelia Huck7674da72006-12-08 15:54:21 +0100777 /* Do first half of device_register. */
778 device_initialize(&cdev->dev);
Sebastian Ott2c3e7e12014-06-11 13:06:57 +0200779 ret = dev_set_name(&cdev->dev, "0.%x.%04x", cdev->private->dev_id.ssid,
780 cdev->private->dev_id.devno);
781 if (ret)
782 goto out_put;
Cornelia Huck7674da72006-12-08 15:54:21 +0100783 if (!get_device(&sch->dev)) {
Sebastian Ott2c3e7e12014-06-11 13:06:57 +0200784 ret = -ENODEV;
785 goto out_put;
Cornelia Huck7674da72006-12-08 15:54:21 +0100786 }
Sebastian Ott2c3e7e12014-06-11 13:06:57 +0200787 priv->flags.initialized = 1;
788 spin_lock_irq(sch->lock);
789 sch_set_cdev(sch, cdev);
790 spin_unlock_irq(sch->lock);
Cornelia Huck7674da72006-12-08 15:54:21 +0100791 return 0;
Sebastian Ott2c3e7e12014-06-11 13:06:57 +0200792
793out_put:
794 /* Release reference from device_initialize(). */
795 put_device(&cdev->dev);
796 return ret;
Cornelia Huck7674da72006-12-08 15:54:21 +0100797}
798
799static struct ccw_device * io_subchannel_create_ccwdev(struct subchannel *sch)
800{
801 struct ccw_device *cdev;
802 int ret;
803
804 cdev = io_subchannel_allocate_dev(sch);
805 if (!IS_ERR(cdev)) {
806 ret = io_subchannel_initialize_dev(sch, cdev);
Sebastian Ott06739a82009-08-23 18:09:04 +0200807 if (ret)
Cornelia Huck7674da72006-12-08 15:54:21 +0100808 cdev = ERR_PTR(ret);
Cornelia Huck7674da72006-12-08 15:54:21 +0100809 }
810 return cdev;
811}
812
Peter Oberparleiter736b5db2009-12-07 12:51:21 +0100813static void io_subchannel_recog(struct ccw_device *, struct subchannel *);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100814
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100815static void sch_create_and_recog_new_device(struct subchannel *sch)
816{
817 struct ccw_device *cdev;
818
819 /* Need to allocate a new ccw device. */
820 cdev = io_subchannel_create_ccwdev(sch);
821 if (IS_ERR(cdev)) {
822 /* OK, we did everything we could... */
823 css_sch_device_unregister(sch);
824 return;
825 }
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100826 /* Start recognition for the new ccw device. */
Peter Oberparleiter736b5db2009-12-07 12:51:21 +0100827 io_subchannel_recog(cdev, sch);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100828}
829
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830/*
831 * Register recognized device.
832 */
Peter Oberparleiter37de53b2009-12-07 12:51:19 +0100833static void io_subchannel_register(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 struct subchannel *sch;
Sebastian Otta2901562010-03-08 12:25:17 +0100836 int ret, adjust_init_count = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 unsigned long flags;
838
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 sch = to_subchannel(cdev->dev.parent);
Cornelia Huck5fb6b852008-12-25 13:39:08 +0100840 /*
841 * Check if subchannel is still registered. It may have become
842 * unregistered if a machine check hit us after finishing
843 * device recognition but before the register work could be
844 * queued.
845 */
846 if (!device_is_registered(&sch->dev))
847 goto out_err;
Cornelia Huck82b7ac02007-04-27 16:01:36 +0200848 css_update_ssd_info(sch);
Cornelia Huck47af5512006-12-04 15:41:07 +0100849 /*
850 * io_subchannel_register() will also be called after device
851 * recognition has been done for a boxed device (which will already
852 * be registered). We need to reprobe since we may now have sense id
853 * information.
854 */
Cornelia Huckd6a30762008-12-25 13:39:11 +0100855 if (device_is_registered(&cdev->dev)) {
Cornelia Huck47af5512006-12-04 15:41:07 +0100856 if (!cdev->drv) {
857 ret = device_reprobe(&cdev->dev);
858 if (ret)
859 /* We can't do much here. */
Michael Ernst139b83d2008-05-07 09:22:54 +0200860 CIO_MSG_EVENT(0, "device_reprobe() returned"
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200861 " %d for 0.%x.%04x\n", ret,
862 cdev->private->dev_id.ssid,
863 cdev->private->dev_id.devno);
Cornelia Huck47af5512006-12-04 15:41:07 +0100864 }
Sebastian Otta2901562010-03-08 12:25:17 +0100865 adjust_init_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 goto out;
867 }
Cornelia Huckfa1a8c22007-04-26 00:12:03 -0700868 /*
869 * Now we know this subchannel will stay, we can throw
870 * our delayed uevent.
871 */
Ming Leif67f1292009-03-01 21:10:49 +0800872 dev_set_uevent_suppress(&sch->dev, 0);
Cornelia Huckfa1a8c22007-04-26 00:12:03 -0700873 kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 /* make it known to the system */
Sebastian Ott2c3e7e12014-06-11 13:06:57 +0200875 ret = ccw_device_add(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 if (ret) {
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200877 CIO_MSG_EVENT(0, "Could not register ccw dev 0.%x.%04x: %d\n",
878 cdev->private->dev_id.ssid,
879 cdev->private->dev_id.devno, ret);
Cornelia Huck2ec22982006-12-08 15:54:26 +0100880 spin_lock_irqsave(sch->lock, flags);
Cornelia Huckdb6a6422008-01-26 14:10:46 +0100881 sch_set_cdev(sch, NULL);
Cornelia Huck2ec22982006-12-08 15:54:26 +0100882 spin_unlock_irqrestore(sch->lock, flags);
Cornelia Huck6eff2082008-12-25 13:39:07 +0100883 /* Release initial device reference. */
884 put_device(&cdev->dev);
Cornelia Huck5fb6b852008-12-25 13:39:08 +0100885 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887out:
888 cdev->private->flags.recog_done = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 wake_up(&cdev->private->wait_q);
Cornelia Huck5fb6b852008-12-25 13:39:08 +0100890out_err:
Sebastian Otta2901562010-03-08 12:25:17 +0100891 if (adjust_init_count && atomic_dec_and_test(&ccw_device_init_count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 wake_up(&ccw_device_init_wq);
893}
894
Peter Oberparleiter37de53b2009-12-07 12:51:19 +0100895static void ccw_device_call_sch_unregister(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 struct subchannel *sch;
898
Peter Oberparleiter46fbe4e2008-10-10 21:33:05 +0200899 /* Get subchannel reference for local processing. */
900 if (!get_device(cdev->dev.parent))
901 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 sch = to_subchannel(cdev->dev.parent);
Cornelia Huck6ab48792006-07-12 16:39:50 +0200903 css_sch_device_unregister(sch);
Peter Oberparleiter46fbe4e2008-10-10 21:33:05 +0200904 /* Release subchannel reference for local processing. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 put_device(&sch->dev);
906}
907
908/*
909 * subchannel recognition done. Called from the state machine.
910 */
911void
912io_subchannel_recog_done(struct ccw_device *cdev)
913{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 if (css_init_done == 0) {
915 cdev->private->flags.recog_done = 1;
916 return;
917 }
918 switch (cdev->private->state) {
Sebastian Ott47593bf2009-03-31 19:16:05 +0200919 case DEV_STATE_BOXED:
920 /* Device did not respond in time. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 case DEV_STATE_NOT_OPER:
922 cdev->private->flags.recog_done = 1;
Peter Oberparleiter37de53b2009-12-07 12:51:19 +0100923 /* Remove device found not operational. */
924 ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 if (atomic_dec_and_test(&ccw_device_init_count))
926 wake_up(&ccw_device_init_wq);
927 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 case DEV_STATE_OFFLINE:
929 /*
930 * We can't register the device in interrupt context so
931 * we schedule a work item.
932 */
Peter Oberparleiter37de53b2009-12-07 12:51:19 +0100933 ccw_device_sched_todo(cdev, CDEV_TODO_REGISTER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 break;
935 }
936}
937
Peter Oberparleiter736b5db2009-12-07 12:51:21 +0100938static void io_subchannel_recog(struct ccw_device *cdev, struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 /* Increase counter of devices currently in recognition. */
941 atomic_inc(&ccw_device_init_count);
942
943 /* Start async. device sensing. */
Cornelia Huck2ec22982006-12-08 15:54:26 +0100944 spin_lock_irq(sch->lock);
Peter Oberparleiter736b5db2009-12-07 12:51:21 +0100945 ccw_device_recognition(cdev);
Cornelia Huck2ec22982006-12-08 15:54:26 +0100946 spin_unlock_irq(sch->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947}
948
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100949static int ccw_device_move_to_sch(struct ccw_device *cdev,
950 struct subchannel *sch)
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100951{
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100952 struct subchannel *old_sch;
Sebastian Ott0c609fc2009-12-07 12:51:37 +0100953 int rc, old_enabled = 0;
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100954
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100955 old_sch = to_subchannel(cdev->dev.parent);
956 /* Obtain child reference for new parent. */
Cornelia Huck6eff2082008-12-25 13:39:07 +0100957 if (!get_device(&sch->dev))
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100958 return -ENODEV;
Sebastian Ott0c609fc2009-12-07 12:51:37 +0100959
960 if (!sch_is_pseudo_sch(old_sch)) {
961 spin_lock_irq(old_sch->lock);
962 old_enabled = old_sch->schib.pmcw.ena;
963 rc = 0;
964 if (old_enabled)
965 rc = cio_disable_subchannel(old_sch);
966 spin_unlock_irq(old_sch->lock);
967 if (rc == -EBUSY) {
968 /* Release child reference for new parent. */
969 put_device(&sch->dev);
970 return rc;
971 }
972 }
973
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100974 mutex_lock(&sch->reg_mutex);
Cornelia Huckffa6a702009-03-04 12:44:00 +0100975 rc = device_move(&cdev->dev, &sch->dev, DPM_ORDER_PARENT_BEFORE_DEV);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100976 mutex_unlock(&sch->reg_mutex);
977 if (rc) {
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100978 CIO_MSG_EVENT(0, "device_move(0.%x.%04x,0.%x.%04x)=%d\n",
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100979 cdev->private->dev_id.ssid,
980 cdev->private->dev_id.devno, sch->schid.ssid,
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100981 sch->schib.pmcw.dev, rc);
Sebastian Ott0c609fc2009-12-07 12:51:37 +0100982 if (old_enabled) {
983 /* Try to reenable the old subchannel. */
984 spin_lock_irq(old_sch->lock);
985 cio_enable_subchannel(old_sch, (u32)(addr_t)old_sch);
986 spin_unlock_irq(old_sch->lock);
987 }
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100988 /* Release child reference for new parent. */
Cornelia Huck6eff2082008-12-25 13:39:07 +0100989 put_device(&sch->dev);
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100990 return rc;
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100991 }
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100992 /* Clean up old subchannel. */
993 if (!sch_is_pseudo_sch(old_sch)) {
994 spin_lock_irq(old_sch->lock);
995 sch_set_cdev(old_sch, NULL);
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100996 spin_unlock_irq(old_sch->lock);
997 css_schedule_eval(old_sch->schid);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100998 }
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100999 /* Release child reference for old parent. */
1000 put_device(&old_sch->dev);
1001 /* Initialize new subchannel. */
1002 spin_lock_irq(sch->lock);
1003 cdev->private->schid = sch->schid;
1004 cdev->ccwlock = sch->lock;
1005 if (!sch_is_pseudo_sch(sch))
1006 sch_set_cdev(sch, cdev);
1007 spin_unlock_irq(sch->lock);
1008 if (!sch_is_pseudo_sch(sch))
1009 css_update_ssd_info(sch);
1010 return 0;
1011}
1012
1013static int ccw_device_move_to_orph(struct ccw_device *cdev)
1014{
1015 struct subchannel *sch = to_subchannel(cdev->dev.parent);
1016 struct channel_subsystem *css = to_css(sch->dev.parent);
1017
1018 return ccw_device_move_to_sch(cdev, css->pseudo_subchannel);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +01001019}
1020
Cornelia Huck602b20f2008-01-26 14:10:39 +01001021static void io_subchannel_irq(struct subchannel *sch)
1022{
1023 struct ccw_device *cdev;
1024
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001025 cdev = sch_get_cdev(sch);
Cornelia Huck602b20f2008-01-26 14:10:39 +01001026
Sebastian Ottefd986d2009-09-11 10:28:18 +02001027 CIO_TRACE_EVENT(6, "IRQ");
1028 CIO_TRACE_EVENT(6, dev_name(&sch->dev));
Cornelia Huck602b20f2008-01-26 14:10:39 +01001029 if (cdev)
1030 dev_fsm_event(cdev, DEV_EVENT_INTERRUPT);
Peter Oberparleiterde400d62011-10-30 15:16:04 +01001031 else
Heiko Carstens420f42e2013-01-02 15:18:18 +01001032 inc_irq_stat(IRQIO_CIO);
Cornelia Huck602b20f2008-01-26 14:10:39 +01001033}
1034
Sebastian Ott13952ec2008-12-25 13:39:13 +01001035void io_subchannel_init_config(struct subchannel *sch)
1036{
1037 memset(&sch->config, 0, sizeof(sch->config));
1038 sch->config.csense = 1;
Sebastian Ott13952ec2008-12-25 13:39:13 +01001039}
1040
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001041static void io_subchannel_init_fields(struct subchannel *sch)
1042{
1043 if (cio_is_console(sch->schid))
1044 sch->opm = 0xff;
1045 else
1046 sch->opm = chp_get_sch_opm(sch);
1047 sch->lpm = sch->schib.pmcw.pam & sch->opm;
Cornelia Huck3a3fc292008-07-14 09:58:58 +02001048 sch->isc = cio_is_console(sch->schid) ? CONSOLE_ISC : IO_SCH_ISC;
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001049
1050 CIO_MSG_EVENT(6, "Detected device %04x on subchannel 0.%x.%04X"
1051 " - PIM = %02X, PAM = %02X, POM = %02X\n",
1052 sch->schib.pmcw.dev, sch->schid.ssid,
1053 sch->schid.sch_no, sch->schib.pmcw.pim,
1054 sch->schib.pmcw.pam, sch->schib.pmcw.pom);
Sebastian Ott13952ec2008-12-25 13:39:13 +01001055
1056 io_subchannel_init_config(sch);
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001057}
1058
Cornelia Huck90ed2b62008-12-25 13:39:09 +01001059/*
1060 * Note: We always return 0 so that we bind to the device even on error.
1061 * This is needed so that our remove function is called on unregister.
1062 */
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001063static int io_subchannel_probe(struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064{
Sebastian Ottf92519e2011-03-15 17:08:27 +01001065 struct io_subchannel_private *io_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 struct ccw_device *cdev;
1067 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068
Peter Oberparleiter6d7c5af2009-10-14 12:43:50 +02001069 if (cio_is_console(sch->schid)) {
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001070 rc = sysfs_create_group(&sch->dev.kobj,
1071 &io_subchannel_attr_group);
1072 if (rc)
1073 CIO_MSG_EVENT(0, "Failed to create io subchannel "
1074 "attributes for subchannel "
1075 "0.%x.%04x (rc=%d)\n",
1076 sch->schid.ssid, sch->schid.sch_no, rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 /*
Peter Oberparleiter6d7c5af2009-10-14 12:43:50 +02001078 * The console subchannel already has an associated ccw_device.
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001079 * Throw the delayed uevent for the subchannel, register
Peter Oberparleiter6d7c5af2009-10-14 12:43:50 +02001080 * the ccw_device and exit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 */
Ming Leif67f1292009-03-01 21:10:49 +08001082 dev_set_uevent_suppress(&sch->dev, 0);
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001083 kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
Peter Oberparleiter6d7c5af2009-10-14 12:43:50 +02001084 cdev = sch_get_cdev(sch);
Sebastian Ott2c3e7e12014-06-11 13:06:57 +02001085 rc = ccw_device_add(cdev);
Sebastian Ottafdfed02013-04-13 13:03:03 +02001086 if (rc) {
1087 /* Release online reference. */
1088 put_device(&cdev->dev);
1089 goto out_schedule;
1090 }
Sebastian Ott0ad8f714a2013-04-13 13:06:27 +02001091 if (atomic_dec_and_test(&ccw_device_init_count))
1092 wake_up(&ccw_device_init_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 return 0;
1094 }
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001095 io_subchannel_init_fields(sch);
Sebastian Ottf444cc02008-12-25 13:39:14 +01001096 rc = cio_commit_config(sch);
1097 if (rc)
1098 goto out_schedule;
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001099 rc = sysfs_create_group(&sch->dev.kobj,
1100 &io_subchannel_attr_group);
1101 if (rc)
Cornelia Huck90ed2b62008-12-25 13:39:09 +01001102 goto out_schedule;
Cornelia Huckcd6b4f22008-01-26 14:10:43 +01001103 /* Allocate I/O subchannel private data. */
Sebastian Ottf92519e2011-03-15 17:08:27 +01001104 io_priv = kzalloc(sizeof(*io_priv), GFP_KERNEL | GFP_DMA);
1105 if (!io_priv)
Peter Oberparleiter48e4c382009-12-07 12:51:15 +01001106 goto out_schedule;
Sebastian Ottf92519e2011-03-15 17:08:27 +01001107
1108 set_io_private(sch, io_priv);
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001109 css_schedule_eval(sch->schid);
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001110 return 0;
Peter Oberparleiter48e4c382009-12-07 12:51:15 +01001111
Cornelia Huck90ed2b62008-12-25 13:39:09 +01001112out_schedule:
Peter Oberparleiter390935a2009-12-07 12:51:18 +01001113 spin_lock_irq(sch->lock);
1114 css_sched_sch_todo(sch, SCH_TODO_UNREG);
1115 spin_unlock_irq(sch->lock);
Cornelia Huck90ed2b62008-12-25 13:39:09 +01001116 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117}
1118
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119static int
Cornelia Huck8bbace72006-01-11 10:56:22 +01001120io_subchannel_remove (struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121{
Sebastian Ottf92519e2011-03-15 17:08:27 +01001122 struct io_subchannel_private *io_priv = to_io_private(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 struct ccw_device *cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001125 cdev = sch_get_cdev(sch);
1126 if (!cdev)
Peter Oberparleiter48e4c382009-12-07 12:51:15 +01001127 goto out_free;
Sebastian Ott6e9a0f62009-12-07 12:51:38 +01001128 io_subchannel_quiesce(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 /* Set ccw device to not operational and drop reference. */
Sebastian Ott7a8ad102009-12-07 12:51:39 +01001130 spin_lock_irq(cdev->ccwlock);
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001131 sch_set_cdev(sch, NULL);
Sebastian Ottf92519e2011-03-15 17:08:27 +01001132 set_io_private(sch, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 cdev->private->state = DEV_STATE_NOT_OPER;
Sebastian Ott7a8ad102009-12-07 12:51:39 +01001134 spin_unlock_irq(cdev->ccwlock);
Cornelia Huckef995162007-04-27 16:01:39 +02001135 ccw_device_unregister(cdev);
Peter Oberparleiter48e4c382009-12-07 12:51:15 +01001136out_free:
Sebastian Ottf92519e2011-03-15 17:08:27 +01001137 kfree(io_priv);
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001138 sysfs_remove_group(&sch->dev.kobj, &io_subchannel_attr_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 return 0;
1140}
1141
Cornelia Huck602b20f2008-01-26 14:10:39 +01001142static void io_subchannel_verify(struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143{
1144 struct ccw_device *cdev;
1145
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001146 cdev = sch_get_cdev(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 if (cdev)
1148 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1149}
1150
Cornelia Huckc820de32008-07-14 09:58:45 +02001151static void io_subchannel_terminate_path(struct subchannel *sch, u8 mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152{
1153 struct ccw_device *cdev;
1154
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001155 cdev = sch_get_cdev(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 if (!cdev)
1157 return;
Peter Oberparleiter4257aae2009-12-07 12:51:29 +01001158 if (cio_update_schib(sch))
1159 goto err;
1160 /* Check for I/O on path. */
1161 if (scsw_actl(&sch->schib.scsw) == 0 || sch->schib.pmcw.lpum != mask)
1162 goto out;
1163 if (cdev->private->state == DEV_STATE_ONLINE) {
1164 ccw_device_kill_io(cdev);
1165 goto out;
1166 }
1167 if (cio_clear(sch))
1168 goto err;
1169out:
1170 /* Trigger path verification. */
1171 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1172 return;
Cornelia Huckc820de32008-07-14 09:58:45 +02001173
Peter Oberparleiter4257aae2009-12-07 12:51:29 +01001174err:
1175 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
Cornelia Huckc820de32008-07-14 09:58:45 +02001176}
1177
Cornelia Huck99611f82008-07-14 09:59:02 +02001178static int io_subchannel_chp_event(struct subchannel *sch,
1179 struct chp_link *link, int event)
Cornelia Huckc820de32008-07-14 09:58:45 +02001180{
Sebastian Ott585b9542010-10-25 16:10:34 +02001181 struct ccw_device *cdev = sch_get_cdev(sch);
Cornelia Huckc820de32008-07-14 09:58:45 +02001182 int mask;
Cornelia Huckc820de32008-07-14 09:58:45 +02001183
Cornelia Huck99611f82008-07-14 09:59:02 +02001184 mask = chp_ssd_get_mask(&sch->ssd_info, link);
Cornelia Huckc820de32008-07-14 09:58:45 +02001185 if (!mask)
1186 return 0;
1187 switch (event) {
1188 case CHP_VARY_OFF:
1189 sch->opm &= ~mask;
1190 sch->lpm &= ~mask;
Sebastian Ott585b9542010-10-25 16:10:34 +02001191 if (cdev)
1192 cdev->private->path_gone_mask |= mask;
Cornelia Huckc820de32008-07-14 09:58:45 +02001193 io_subchannel_terminate_path(sch, mask);
1194 break;
1195 case CHP_VARY_ON:
1196 sch->opm |= mask;
1197 sch->lpm |= mask;
Sebastian Ott585b9542010-10-25 16:10:34 +02001198 if (cdev)
1199 cdev->private->path_new_mask |= mask;
Cornelia Huckc820de32008-07-14 09:58:45 +02001200 io_subchannel_verify(sch);
1201 break;
1202 case CHP_OFFLINE:
Sebastian Ottcdb912a2008-12-25 13:39:12 +01001203 if (cio_update_schib(sch))
Cornelia Huckc820de32008-07-14 09:58:45 +02001204 return -ENODEV;
Sebastian Ott585b9542010-10-25 16:10:34 +02001205 if (cdev)
1206 cdev->private->path_gone_mask |= mask;
Cornelia Huckc820de32008-07-14 09:58:45 +02001207 io_subchannel_terminate_path(sch, mask);
1208 break;
1209 case CHP_ONLINE:
Sebastian Ottcdb912a2008-12-25 13:39:12 +01001210 if (cio_update_schib(sch))
1211 return -ENODEV;
Cornelia Huckc820de32008-07-14 09:58:45 +02001212 sch->lpm |= mask & sch->opm;
Sebastian Ott585b9542010-10-25 16:10:34 +02001213 if (cdev)
1214 cdev->private->path_new_mask |= mask;
Cornelia Huckc820de32008-07-14 09:58:45 +02001215 io_subchannel_verify(sch);
1216 break;
1217 }
1218 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219}
1220
Sebastian Ott6e9a0f62009-12-07 12:51:38 +01001221static void io_subchannel_quiesce(struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 struct ccw_device *cdev;
1224 int ret;
1225
Sebastian Ott56e6b792009-12-07 12:51:35 +01001226 spin_lock_irq(sch->lock);
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001227 cdev = sch_get_cdev(sch);
Cornelia Hucka8237fc2006-01-06 00:19:21 -08001228 if (cio_is_console(sch->schid))
Sebastian Ott56e6b792009-12-07 12:51:35 +01001229 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 if (!sch->schib.pmcw.ena)
Sebastian Ott56e6b792009-12-07 12:51:35 +01001231 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 ret = cio_disable_subchannel(sch);
1233 if (ret != -EBUSY)
Sebastian Ott56e6b792009-12-07 12:51:35 +01001234 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 if (cdev->handler)
Sebastian Ott56e6b792009-12-07 12:51:35 +01001236 cdev->handler(cdev, cdev->private->intparm, ERR_PTR(-EIO));
1237 while (ret == -EBUSY) {
1238 cdev->private->state = DEV_STATE_QUIESCE;
Peter Oberparleiter376ae472010-10-25 16:10:44 +02001239 cdev->private->iretry = 255;
Sebastian Ott56e6b792009-12-07 12:51:35 +01001240 ret = ccw_device_cancel_halt_clear(cdev);
1241 if (ret == -EBUSY) {
1242 ccw_device_set_timeout(cdev, HZ/10);
1243 spin_unlock_irq(sch->lock);
1244 wait_event(cdev->private->wait_q,
1245 cdev->private->state != DEV_STATE_QUIESCE);
1246 spin_lock_irq(sch->lock);
1247 }
1248 ret = cio_disable_subchannel(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 }
Sebastian Ott56e6b792009-12-07 12:51:35 +01001250out_unlock:
1251 spin_unlock_irq(sch->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252}
1253
Sebastian Ott6e9a0f62009-12-07 12:51:38 +01001254static void io_subchannel_shutdown(struct subchannel *sch)
1255{
1256 io_subchannel_quiesce(sch);
1257}
1258
Cornelia Huckc820de32008-07-14 09:58:45 +02001259static int device_is_disconnected(struct ccw_device *cdev)
1260{
1261 if (!cdev)
1262 return 0;
1263 return (cdev->private->state == DEV_STATE_DISCONNECTED ||
1264 cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID);
1265}
1266
1267static int recovery_check(struct device *dev, void *data)
1268{
1269 struct ccw_device *cdev = to_ccwdev(dev);
1270 int *redo = data;
1271
1272 spin_lock_irq(cdev->ccwlock);
1273 switch (cdev->private->state) {
1274 case DEV_STATE_DISCONNECTED:
1275 CIO_MSG_EVENT(3, "recovery: trigger 0.%x.%04x\n",
1276 cdev->private->dev_id.ssid,
1277 cdev->private->dev_id.devno);
1278 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1279 *redo = 1;
1280 break;
1281 case DEV_STATE_DISCONNECTED_SENSE_ID:
1282 *redo = 1;
1283 break;
1284 }
1285 spin_unlock_irq(cdev->ccwlock);
1286
1287 return 0;
1288}
1289
1290static void recovery_work_func(struct work_struct *unused)
1291{
1292 int redo = 0;
1293
1294 bus_for_each_dev(&ccw_bus_type, NULL, &redo, recovery_check);
1295 if (redo) {
1296 spin_lock_irq(&recovery_lock);
1297 if (!timer_pending(&recovery_timer)) {
1298 if (recovery_phase < ARRAY_SIZE(recovery_delay) - 1)
1299 recovery_phase++;
1300 mod_timer(&recovery_timer, jiffies +
1301 recovery_delay[recovery_phase] * HZ);
1302 }
1303 spin_unlock_irq(&recovery_lock);
1304 } else
1305 CIO_MSG_EVENT(4, "recovery: end\n");
1306}
1307
1308static DECLARE_WORK(recovery_work, recovery_work_func);
1309
1310static void recovery_func(unsigned long data)
1311{
1312 /*
1313 * We can't do our recovery in softirq context and it's not
1314 * performance critical, so we schedule it.
1315 */
1316 schedule_work(&recovery_work);
1317}
1318
1319static void ccw_device_schedule_recovery(void)
1320{
1321 unsigned long flags;
1322
1323 CIO_MSG_EVENT(4, "recovery: schedule\n");
1324 spin_lock_irqsave(&recovery_lock, flags);
1325 if (!timer_pending(&recovery_timer) || (recovery_phase != 0)) {
1326 recovery_phase = 0;
1327 mod_timer(&recovery_timer, jiffies + recovery_delay[0] * HZ);
1328 }
1329 spin_unlock_irqrestore(&recovery_lock, flags);
1330}
1331
Peter Oberparleiterecf5d9e2008-10-10 21:33:06 +02001332static int purge_fn(struct device *dev, void *data)
1333{
1334 struct ccw_device *cdev = to_ccwdev(dev);
Peter Oberparleiter37de53b2009-12-07 12:51:19 +01001335 struct ccw_dev_id *id = &cdev->private->dev_id;
Peter Oberparleiterecf5d9e2008-10-10 21:33:06 +02001336
1337 spin_lock_irq(cdev->ccwlock);
Peter Oberparleiter37de53b2009-12-07 12:51:19 +01001338 if (is_blacklisted(id->ssid, id->devno) &&
Peter Oberparleitera2fc8482011-04-04 09:43:32 +02001339 (cdev->private->state == DEV_STATE_OFFLINE) &&
1340 (atomic_cmpxchg(&cdev->private->onoff, 0, 1) == 0)) {
Peter Oberparleiter37de53b2009-12-07 12:51:19 +01001341 CIO_MSG_EVENT(3, "ccw: purging 0.%x.%04x\n", id->ssid,
1342 id->devno);
1343 ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
Peter Oberparleitera2fc8482011-04-04 09:43:32 +02001344 atomic_set(&cdev->private->onoff, 0);
Peter Oberparleiter37de53b2009-12-07 12:51:19 +01001345 }
Peter Oberparleiterecf5d9e2008-10-10 21:33:06 +02001346 spin_unlock_irq(cdev->ccwlock);
Peter Oberparleiterecf5d9e2008-10-10 21:33:06 +02001347 /* Abort loop in case of pending signal. */
1348 if (signal_pending(current))
1349 return -EINTR;
1350
1351 return 0;
1352}
1353
1354/**
1355 * ccw_purge_blacklisted - purge unused, blacklisted devices
1356 *
1357 * Unregister all ccw devices that are offline and on the blacklist.
1358 */
1359int ccw_purge_blacklisted(void)
1360{
1361 CIO_MSG_EVENT(2, "ccw: purging blacklisted devices\n");
1362 bus_for_each_dev(&ccw_bus_type, NULL, NULL, purge_fn);
1363 return 0;
1364}
1365
Peter Oberparleiter6afcc772009-10-06 10:34:02 +02001366void ccw_device_set_disconnected(struct ccw_device *cdev)
Cornelia Huckc820de32008-07-14 09:58:45 +02001367{
1368 if (!cdev)
1369 return;
1370 ccw_device_set_timeout(cdev, 0);
1371 cdev->private->flags.fake_irb = 0;
1372 cdev->private->state = DEV_STATE_DISCONNECTED;
1373 if (cdev->online)
1374 ccw_device_schedule_recovery();
1375}
1376
Peter Oberparleiter91c36912008-08-21 19:46:39 +02001377void ccw_device_set_notoper(struct ccw_device *cdev)
1378{
1379 struct subchannel *sch = to_subchannel(cdev->dev.parent);
1380
1381 CIO_TRACE_EVENT(2, "notoper");
Cornelia Huckb9d3aed2008-10-10 21:33:11 +02001382 CIO_TRACE_EVENT(2, dev_name(&sch->dev));
Peter Oberparleiter91c36912008-08-21 19:46:39 +02001383 ccw_device_set_timeout(cdev, 0);
1384 cio_disable_subchannel(sch);
1385 cdev->private->state = DEV_STATE_NOT_OPER;
1386}
1387
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001388enum io_sch_action {
1389 IO_SCH_UNREG,
1390 IO_SCH_ORPH_UNREG,
1391 IO_SCH_ATTACH,
1392 IO_SCH_UNREG_ATTACH,
1393 IO_SCH_ORPH_ATTACH,
1394 IO_SCH_REPROBE,
1395 IO_SCH_VERIFY,
1396 IO_SCH_DISC,
1397 IO_SCH_NOP,
1398};
1399
1400static enum io_sch_action sch_get_action(struct subchannel *sch)
Cornelia Huckc820de32008-07-14 09:58:45 +02001401{
Cornelia Huckc820de32008-07-14 09:58:45 +02001402 struct ccw_device *cdev;
1403
Cornelia Huckc820de32008-07-14 09:58:45 +02001404 cdev = sch_get_cdev(sch);
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001405 if (cio_update_schib(sch)) {
1406 /* Not operational. */
1407 if (!cdev)
1408 return IO_SCH_UNREG;
Sebastian Ott76e6fb42010-02-26 22:37:28 +01001409 if (ccw_device_notify(cdev, CIO_GONE) != NOTIFY_OK)
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001410 return IO_SCH_UNREG;
1411 return IO_SCH_ORPH_UNREG;
Cornelia Huckc820de32008-07-14 09:58:45 +02001412 }
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001413 /* Operational. */
1414 if (!cdev)
1415 return IO_SCH_ATTACH;
1416 if (sch->schib.pmcw.dev != cdev->private->dev_id.devno) {
Sebastian Ott76e6fb42010-02-26 22:37:28 +01001417 if (ccw_device_notify(cdev, CIO_GONE) != NOTIFY_OK)
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001418 return IO_SCH_UNREG_ATTACH;
1419 return IO_SCH_ORPH_ATTACH;
Cornelia Huckc820de32008-07-14 09:58:45 +02001420 }
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001421 if ((sch->schib.pmcw.pam & sch->opm) == 0) {
Sebastian Ott76e6fb42010-02-26 22:37:28 +01001422 if (ccw_device_notify(cdev, CIO_NO_PATH) != NOTIFY_OK)
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001423 return IO_SCH_UNREG;
1424 return IO_SCH_DISC;
Cornelia Huckc820de32008-07-14 09:58:45 +02001425 }
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001426 if (device_is_disconnected(cdev))
1427 return IO_SCH_REPROBE;
Sebastian Ott31370f752012-10-24 11:22:52 +02001428 if (cdev->online && !cdev->private->flags.resuming)
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001429 return IO_SCH_VERIFY;
Sebastian Ott43d0be72012-09-05 14:19:42 +02001430 if (cdev->private->state == DEV_STATE_NOT_OPER)
1431 return IO_SCH_UNREG_ATTACH;
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001432 return IO_SCH_NOP;
1433}
1434
1435/**
1436 * io_subchannel_sch_event - process subchannel event
1437 * @sch: subchannel
1438 * @process: non-zero if function is called in process context
1439 *
1440 * An unspecified event occurred for this subchannel. Adjust data according
1441 * to the current operational state of the subchannel and device. Return
1442 * zero when the event has been handled sufficiently or -EAGAIN when this
1443 * function should be called again in process context.
1444 */
1445static int io_subchannel_sch_event(struct subchannel *sch, int process)
1446{
1447 unsigned long flags;
1448 struct ccw_device *cdev;
1449 struct ccw_dev_id dev_id;
1450 enum io_sch_action action;
1451 int rc = -EAGAIN;
1452
1453 spin_lock_irqsave(sch->lock, flags);
1454 if (!device_is_registered(&sch->dev))
1455 goto out_unlock;
Peter Oberparleiter390935a2009-12-07 12:51:18 +01001456 if (work_pending(&sch->todo_work))
1457 goto out_unlock;
Peter Oberparleiter37de53b2009-12-07 12:51:19 +01001458 cdev = sch_get_cdev(sch);
1459 if (cdev && work_pending(&cdev->private->todo_work))
1460 goto out_unlock;
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001461 action = sch_get_action(sch);
1462 CIO_MSG_EVENT(2, "event: sch 0.%x.%04x, process=%d, action=%d\n",
1463 sch->schid.ssid, sch->schid.sch_no, process,
1464 action);
1465 /* Perform immediate actions while holding the lock. */
Cornelia Huckc820de32008-07-14 09:58:45 +02001466 switch (action) {
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001467 case IO_SCH_REPROBE:
1468 /* Trigger device recognition. */
Cornelia Huckc820de32008-07-14 09:58:45 +02001469 ccw_device_trigger_reprobe(cdev);
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001470 rc = 0;
1471 goto out_unlock;
1472 case IO_SCH_VERIFY:
1473 /* Trigger path verification. */
1474 io_subchannel_verify(sch);
1475 rc = 0;
1476 goto out_unlock;
1477 case IO_SCH_DISC:
1478 ccw_device_set_disconnected(cdev);
1479 rc = 0;
1480 goto out_unlock;
1481 case IO_SCH_ORPH_UNREG:
1482 case IO_SCH_ORPH_ATTACH:
Peter Oberparleiter6afcc772009-10-06 10:34:02 +02001483 ccw_device_set_disconnected(cdev);
Peter Oberparleiter91c36912008-08-21 19:46:39 +02001484 break;
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001485 case IO_SCH_UNREG_ATTACH:
1486 case IO_SCH_UNREG:
Sebastian Ott16d2ce22010-11-10 10:05:53 +01001487 if (!cdev)
1488 break;
1489 if (cdev->private->state == DEV_STATE_SENSE_ID) {
1490 /*
1491 * Note: delayed work triggered by this event
1492 * and repeated calls to sch_event are synchronized
1493 * by the above check for work_pending(cdev).
1494 */
1495 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
1496 } else
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001497 ccw_device_set_notoper(cdev);
1498 break;
1499 case IO_SCH_NOP:
1500 rc = 0;
1501 goto out_unlock;
Cornelia Huckc820de32008-07-14 09:58:45 +02001502 default:
1503 break;
1504 }
1505 spin_unlock_irqrestore(sch->lock, flags);
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001506 /* All other actions require process context. */
1507 if (!process)
1508 goto out;
1509 /* Handle attached ccw device. */
1510 switch (action) {
1511 case IO_SCH_ORPH_UNREG:
1512 case IO_SCH_ORPH_ATTACH:
1513 /* Move ccw device to orphanage. */
1514 rc = ccw_device_move_to_orph(cdev);
1515 if (rc)
1516 goto out;
1517 break;
1518 case IO_SCH_UNREG_ATTACH:
Sebastian Ott3368ba22012-09-05 14:20:41 +02001519 spin_lock_irqsave(sch->lock, flags);
Sebastian Ott74b61272010-10-25 16:10:26 +02001520 if (cdev->private->flags.resuming) {
1521 /* Device will be handled later. */
1522 rc = 0;
Sebastian Ott3368ba22012-09-05 14:20:41 +02001523 goto out_unlock;
Sebastian Ott74b61272010-10-25 16:10:26 +02001524 }
Sebastian Ott3368ba22012-09-05 14:20:41 +02001525 sch_set_cdev(sch, NULL);
1526 spin_unlock_irqrestore(sch->lock, flags);
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001527 /* Unregister ccw device. */
Sebastian Ott74b61272010-10-25 16:10:26 +02001528 ccw_device_unregister(cdev);
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001529 break;
1530 default:
1531 break;
1532 }
1533 /* Handle subchannel. */
1534 switch (action) {
1535 case IO_SCH_ORPH_UNREG:
1536 case IO_SCH_UNREG:
Sebastian Ott0d01bb82010-02-26 22:37:29 +01001537 if (!cdev || !cdev->private->flags.resuming)
1538 css_sch_device_unregister(sch);
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001539 break;
1540 case IO_SCH_ORPH_ATTACH:
1541 case IO_SCH_UNREG_ATTACH:
1542 case IO_SCH_ATTACH:
1543 dev_id.ssid = sch->schid.ssid;
1544 dev_id.devno = sch->schib.pmcw.dev;
1545 cdev = get_ccwdev_by_dev_id(&dev_id);
1546 if (!cdev) {
1547 sch_create_and_recog_new_device(sch);
1548 break;
1549 }
1550 rc = ccw_device_move_to_sch(cdev, sch);
1551 if (rc) {
1552 /* Release reference from get_ccwdev_by_dev_id() */
1553 put_device(&cdev->dev);
1554 goto out;
1555 }
1556 spin_lock_irqsave(sch->lock, flags);
1557 ccw_device_trigger_reprobe(cdev);
1558 spin_unlock_irqrestore(sch->lock, flags);
1559 /* Release reference from get_ccwdev_by_dev_id() */
1560 put_device(&cdev->dev);
1561 break;
1562 default:
1563 break;
1564 }
1565 return 0;
Cornelia Huckc820de32008-07-14 09:58:45 +02001566
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001567out_unlock:
1568 spin_unlock_irqrestore(sch->lock, flags);
1569out:
1570 return rc;
Cornelia Huckc820de32008-07-14 09:58:45 +02001571}
1572
Sebastian Ott137a14f2014-01-27 13:29:15 +01001573static void ccw_device_set_int_class(struct ccw_device *cdev)
1574{
1575 struct ccw_driver *cdrv = cdev->drv;
1576
1577 /* Note: we interpret class 0 in this context as an uninitialized
1578 * field since it translates to a non-I/O interrupt class. */
1579 if (cdrv->int_class != 0)
1580 cdev->private->int_class = cdrv->int_class;
1581 else
1582 cdev->private->int_class = IRQIO_CIO;
1583}
1584
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585#ifdef CONFIG_CCW_CONSOLE
Sebastian Ott1e532092014-01-27 13:28:10 +01001586int __init ccw_device_enable_console(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587{
Sebastian Ott1e532092014-01-27 13:28:10 +01001588 struct subchannel *sch = to_subchannel(cdev->dev.parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 int rc;
1590
Sebastian Ott1e532092014-01-27 13:28:10 +01001591 if (!cdev->drv || !cdev->handler)
1592 return -EINVAL;
1593
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001594 io_subchannel_init_fields(sch);
Sebastian Ottf444cc02008-12-25 13:39:14 +01001595 rc = cio_commit_config(sch);
1596 if (rc)
1597 return rc;
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001598 sch->driver = &io_subchannel_driver;
Peter Oberparleiter736b5db2009-12-07 12:51:21 +01001599 io_subchannel_recog(cdev, sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 /* Now wait for the async. recognition to come to an end. */
1601 spin_lock_irq(cdev->ccwlock);
1602 while (!dev_fsm_final_state(cdev))
Sebastian Ott188561a2013-04-13 12:53:21 +02001603 ccw_device_wait_idle(cdev);
1604
Sebastian Ottafdfed02013-04-13 13:03:03 +02001605 /* Hold on to an extra reference while device is online. */
1606 get_device(&cdev->dev);
1607 rc = ccw_device_online(cdev);
1608 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 goto out_unlock;
Sebastian Ottafdfed02013-04-13 13:03:03 +02001610
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 while (!dev_fsm_final_state(cdev))
Sebastian Ott188561a2013-04-13 12:53:21 +02001612 ccw_device_wait_idle(cdev);
1613
Sebastian Ottafdfed02013-04-13 13:03:03 +02001614 if (cdev->private->state == DEV_STATE_ONLINE)
1615 cdev->online = 1;
1616 else
1617 rc = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618out_unlock:
1619 spin_unlock_irq(cdev->ccwlock);
Sebastian Ottafdfed02013-04-13 13:03:03 +02001620 if (rc) /* Give up online reference since onlining failed. */
1621 put_device(&cdev->dev);
Peter Oberparleiter736b5db2009-12-07 12:51:21 +01001622 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623}
1624
Sebastian Ott1e532092014-01-27 13:28:10 +01001625struct ccw_device * __init ccw_device_create_console(struct ccw_driver *drv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626{
Sebastian Ott863fc842013-04-13 13:01:50 +02001627 struct io_subchannel_private *io_priv;
Sebastian Ottafdfed02013-04-13 13:03:03 +02001628 struct ccw_device *cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 struct subchannel *sch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 sch = cio_probe_console();
Sebastian Ottafdfed02013-04-13 13:03:03 +02001632 if (IS_ERR(sch))
1633 return ERR_CAST(sch);
Sebastian Ott863fc842013-04-13 13:01:50 +02001634
1635 io_priv = kzalloc(sizeof(*io_priv), GFP_KERNEL | GFP_DMA);
1636 if (!io_priv) {
1637 put_device(&sch->dev);
1638 return ERR_PTR(-ENOMEM);
1639 }
Sebastian Ott2c3e7e12014-06-11 13:06:57 +02001640 set_io_private(sch, io_priv);
Sebastian Ottafdfed02013-04-13 13:03:03 +02001641 cdev = io_subchannel_create_ccwdev(sch);
1642 if (IS_ERR(cdev)) {
1643 put_device(&sch->dev);
1644 kfree(io_priv);
1645 return cdev;
1646 }
Sebastian Ott2253e8d2014-01-27 13:26:10 +01001647 cdev->drv = drv;
Sebastian Ott137a14f2014-01-27 13:29:15 +01001648 ccw_device_set_int_class(cdev);
Sebastian Ottafdfed02013-04-13 13:03:03 +02001649 return cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650}
Cornelia Huck1f4e7ed2008-10-10 21:33:14 +02001651
Sebastian Ott1e532092014-01-27 13:28:10 +01001652void __init ccw_device_destroy_console(struct ccw_device *cdev)
1653{
1654 struct subchannel *sch = to_subchannel(cdev->dev.parent);
1655 struct io_subchannel_private *io_priv = to_io_private(sch);
1656
1657 set_io_private(sch, NULL);
1658 put_device(&sch->dev);
1659 put_device(&cdev->dev);
1660 kfree(io_priv);
1661}
1662
Sebastian Ott188561a2013-04-13 12:53:21 +02001663/**
1664 * ccw_device_wait_idle() - busy wait for device to become idle
1665 * @cdev: ccw device
1666 *
1667 * Poll until activity control is zero, that is, no function or data
1668 * transfer is pending/active.
1669 * Called with device lock being held.
1670 */
1671void ccw_device_wait_idle(struct ccw_device *cdev)
1672{
1673 struct subchannel *sch = to_subchannel(cdev->dev.parent);
1674
1675 while (1) {
1676 cio_tsch(sch);
1677 if (sch->schib.scsw.cmd.actl == 0)
1678 break;
1679 udelay_simple(100);
1680 }
1681}
1682
Martin Schwidefsky66648452009-06-16 10:30:28 +02001683static int ccw_device_pm_restore(struct device *dev);
1684
Sebastian Ottf10ccca2013-04-13 12:56:51 +02001685int ccw_device_force_console(struct ccw_device *cdev)
Martin Schwidefsky66648452009-06-16 10:30:28 +02001686{
Sebastian Ottf10ccca2013-04-13 12:56:51 +02001687 return ccw_device_pm_restore(&cdev->dev);
Martin Schwidefsky66648452009-06-16 10:30:28 +02001688}
1689EXPORT_SYMBOL_GPL(ccw_device_force_console);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690#endif
1691
1692/*
1693 * get ccw_device matching the busid, but only if owned by cdrv
1694 */
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001695static int
1696__ccwdev_check_busid(struct device *dev, void *id)
1697{
1698 char *bus_id;
1699
Cornelia Huck12975ae2006-10-11 15:31:47 +02001700 bus_id = id;
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001701
Kay Sievers98df67b2008-12-25 13:38:55 +01001702 return (strcmp(bus_id, dev_name(dev)) == 0);
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001703}
1704
1705
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +02001706/**
1707 * get_ccwdev_by_busid() - obtain device from a bus id
1708 * @cdrv: driver the device is owned by
1709 * @bus_id: bus id of the device to be searched
1710 *
1711 * This function searches all devices owned by @cdrv for a device with a bus
1712 * id matching @bus_id.
1713 * Returns:
1714 * If a match is found, its reference count of the found device is increased
1715 * and it is returned; else %NULL is returned.
1716 */
1717struct ccw_device *get_ccwdev_by_busid(struct ccw_driver *cdrv,
1718 const char *bus_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719{
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001720 struct device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721
Sebastian Ott9f30ea92012-01-24 13:35:02 -05001722 dev = driver_find_device(&cdrv->driver, NULL, (void *)bus_id,
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001723 __ccwdev_check_busid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724
Heiko Carstensd2c993d2006-07-12 16:41:55 +02001725 return dev ? to_ccwdev(dev) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726}
1727
1728/************************** device driver handling ************************/
1729
1730/* This is the implementation of the ccw_driver class. The probe, remove
1731 * and release methods are initially very similar to the device_driver
1732 * implementations, with the difference that they have ccw_device
1733 * arguments.
1734 *
1735 * A ccw driver also contains the information that is needed for
1736 * device matching.
1737 */
1738static int
1739ccw_device_probe (struct device *dev)
1740{
1741 struct ccw_device *cdev = to_ccwdev(dev);
1742 struct ccw_driver *cdrv = to_ccwdrv(dev->driver);
1743 int ret;
1744
1745 cdev->drv = cdrv; /* to let the driver call _set_online */
Sebastian Ott137a14f2014-01-27 13:29:15 +01001746 ccw_device_set_int_class(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 ret = cdrv->probe ? cdrv->probe(cdev) : -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 if (ret) {
Heiko Carstensd2c993d2006-07-12 16:41:55 +02001749 cdev->drv = NULL;
Heiko Carstens420f42e2013-01-02 15:18:18 +01001750 cdev->private->int_class = IRQIO_CIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 return ret;
1752 }
1753
1754 return 0;
1755}
1756
Sebastian Ott74bd0d82013-12-16 10:51:54 +01001757static int ccw_device_remove(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758{
1759 struct ccw_device *cdev = to_ccwdev(dev);
1760 struct ccw_driver *cdrv = cdev->drv;
1761 int ret;
1762
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 if (cdrv->remove)
1764 cdrv->remove(cdev);
Sebastian Ott74bd0d82013-12-16 10:51:54 +01001765
1766 spin_lock_irq(cdev->ccwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 if (cdev->online) {
1768 cdev->online = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 ret = ccw_device_offline(cdev);
1770 spin_unlock_irq(cdev->ccwlock);
1771 if (ret == 0)
1772 wait_event(cdev->private->wait_q,
1773 dev_fsm_final_state(cdev));
1774 else
Michael Ernst139b83d2008-05-07 09:22:54 +02001775 CIO_MSG_EVENT(0, "ccw_device_offline returned %d, "
Cornelia Hucke556bbb2007-07-27 12:29:19 +02001776 "device 0.%x.%04x\n",
1777 ret, cdev->private->dev_id.ssid,
1778 cdev->private->dev_id.devno);
Cornelia Huck9cd67422008-12-25 13:39:06 +01001779 /* Give up reference obtained in ccw_device_set_online(). */
1780 put_device(&cdev->dev);
Sebastian Ott74bd0d82013-12-16 10:51:54 +01001781 spin_lock_irq(cdev->ccwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 }
1783 ccw_device_set_timeout(cdev, 0);
Heiko Carstensd2c993d2006-07-12 16:41:55 +02001784 cdev->drv = NULL;
Heiko Carstens420f42e2013-01-02 15:18:18 +01001785 cdev->private->int_class = IRQIO_CIO;
Sebastian Ott74bd0d82013-12-16 10:51:54 +01001786 spin_unlock_irq(cdev->ccwlock);
Sebastian Otta6ef1562015-09-07 19:51:39 +02001787 __disable_cmf(cdev);
1788
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 return 0;
1790}
1791
Cornelia Huck958974fb2007-10-12 16:11:21 +02001792static void ccw_device_shutdown(struct device *dev)
1793{
1794 struct ccw_device *cdev;
1795
1796 cdev = to_ccwdev(dev);
1797 if (cdev->drv && cdev->drv->shutdown)
1798 cdev->drv->shutdown(cdev);
Sebastian Ott1bc66642015-09-15 13:11:42 +02001799 __disable_cmf(cdev);
Cornelia Huck958974fb2007-10-12 16:11:21 +02001800}
1801
Sebastian Ott823d4942009-06-16 10:30:20 +02001802static int ccw_device_pm_prepare(struct device *dev)
1803{
1804 struct ccw_device *cdev = to_ccwdev(dev);
1805
Peter Oberparleiter37de53b2009-12-07 12:51:19 +01001806 if (work_pending(&cdev->private->todo_work))
Sebastian Ott823d4942009-06-16 10:30:20 +02001807 return -EAGAIN;
1808 /* Fail while device is being set online/offline. */
1809 if (atomic_read(&cdev->private->onoff))
1810 return -EAGAIN;
1811
1812 if (cdev->online && cdev->drv && cdev->drv->prepare)
1813 return cdev->drv->prepare(cdev);
1814
1815 return 0;
1816}
1817
1818static void ccw_device_pm_complete(struct device *dev)
1819{
1820 struct ccw_device *cdev = to_ccwdev(dev);
1821
1822 if (cdev->online && cdev->drv && cdev->drv->complete)
1823 cdev->drv->complete(cdev);
1824}
1825
1826static int ccw_device_pm_freeze(struct device *dev)
1827{
1828 struct ccw_device *cdev = to_ccwdev(dev);
1829 struct subchannel *sch = to_subchannel(cdev->dev.parent);
1830 int ret, cm_enabled;
1831
1832 /* Fail suspend while device is in transistional state. */
1833 if (!dev_fsm_final_state(cdev))
1834 return -EAGAIN;
1835 if (!cdev->online)
1836 return 0;
1837 if (cdev->drv && cdev->drv->freeze) {
1838 ret = cdev->drv->freeze(cdev);
1839 if (ret)
1840 return ret;
1841 }
1842
1843 spin_lock_irq(sch->lock);
1844 cm_enabled = cdev->private->cmb != NULL;
1845 spin_unlock_irq(sch->lock);
1846 if (cm_enabled) {
1847 /* Don't have the css write on memory. */
1848 ret = ccw_set_cmf(cdev, 0);
1849 if (ret)
1850 return ret;
1851 }
1852 /* From here on, disallow device driver I/O. */
1853 spin_lock_irq(sch->lock);
1854 ret = cio_disable_subchannel(sch);
1855 spin_unlock_irq(sch->lock);
1856
1857 return ret;
1858}
1859
1860static int ccw_device_pm_thaw(struct device *dev)
1861{
1862 struct ccw_device *cdev = to_ccwdev(dev);
1863 struct subchannel *sch = to_subchannel(cdev->dev.parent);
1864 int ret, cm_enabled;
1865
1866 if (!cdev->online)
1867 return 0;
1868
1869 spin_lock_irq(sch->lock);
1870 /* Allow device driver I/O again. */
1871 ret = cio_enable_subchannel(sch, (u32)(addr_t)sch);
1872 cm_enabled = cdev->private->cmb != NULL;
1873 spin_unlock_irq(sch->lock);
1874 if (ret)
1875 return ret;
1876
1877 if (cm_enabled) {
1878 ret = ccw_set_cmf(cdev, 1);
1879 if (ret)
1880 return ret;
1881 }
1882
1883 if (cdev->drv && cdev->drv->thaw)
1884 ret = cdev->drv->thaw(cdev);
1885
1886 return ret;
1887}
1888
1889static void __ccw_device_pm_restore(struct ccw_device *cdev)
1890{
1891 struct subchannel *sch = to_subchannel(cdev->dev.parent);
Sebastian Ott823d4942009-06-16 10:30:20 +02001892
Sebastian Ott0d01bb82010-02-26 22:37:29 +01001893 spin_lock_irq(sch->lock);
1894 if (cio_is_console(sch->schid)) {
1895 cio_enable_subchannel(sch, (u32)(addr_t)sch);
1896 goto out_unlock;
1897 }
Sebastian Ott823d4942009-06-16 10:30:20 +02001898 /*
1899 * While we were sleeping, devices may have gone or become
1900 * available again. Kick re-detection.
1901 */
Sebastian Ott823d4942009-06-16 10:30:20 +02001902 cdev->private->flags.resuming = 1;
Sebastian Ottb17295e2011-01-12 09:55:10 +01001903 cdev->private->path_new_mask = LPM_ANYPATH;
Sebastian Ott817e5002011-12-01 13:32:19 +01001904 css_sched_sch_todo(sch, SCH_TODO_EVAL);
Sebastian Ott0d01bb82010-02-26 22:37:29 +01001905 spin_unlock_irq(sch->lock);
Sebastian Ott817e5002011-12-01 13:32:19 +01001906 css_wait_for_slow_path();
Sebastian Ott0d01bb82010-02-26 22:37:29 +01001907
1908 /* cdev may have been moved to a different subchannel. */
1909 sch = to_subchannel(cdev->dev.parent);
1910 spin_lock_irq(sch->lock);
1911 if (cdev->private->state != DEV_STATE_ONLINE &&
1912 cdev->private->state != DEV_STATE_OFFLINE)
1913 goto out_unlock;
1914
Peter Oberparleiter736b5db2009-12-07 12:51:21 +01001915 ccw_device_recognition(cdev);
Sebastian Ott823d4942009-06-16 10:30:20 +02001916 spin_unlock_irq(sch->lock);
Sebastian Ott823d4942009-06-16 10:30:20 +02001917 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev) ||
1918 cdev->private->state == DEV_STATE_DISCONNECTED);
Sebastian Ott0d01bb82010-02-26 22:37:29 +01001919 spin_lock_irq(sch->lock);
1920
1921out_unlock:
Sebastian Ott823d4942009-06-16 10:30:20 +02001922 cdev->private->flags.resuming = 0;
Sebastian Ott0d01bb82010-02-26 22:37:29 +01001923 spin_unlock_irq(sch->lock);
Sebastian Ott823d4942009-06-16 10:30:20 +02001924}
1925
1926static int resume_handle_boxed(struct ccw_device *cdev)
1927{
1928 cdev->private->state = DEV_STATE_BOXED;
Sebastian Ott76e6fb42010-02-26 22:37:28 +01001929 if (ccw_device_notify(cdev, CIO_BOXED) == NOTIFY_OK)
Sebastian Ott823d4942009-06-16 10:30:20 +02001930 return 0;
Peter Oberparleiter37de53b2009-12-07 12:51:19 +01001931 ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
Sebastian Ott823d4942009-06-16 10:30:20 +02001932 return -ENODEV;
1933}
1934
1935static int resume_handle_disc(struct ccw_device *cdev)
1936{
1937 cdev->private->state = DEV_STATE_DISCONNECTED;
Sebastian Ott76e6fb42010-02-26 22:37:28 +01001938 if (ccw_device_notify(cdev, CIO_GONE) == NOTIFY_OK)
Sebastian Ott823d4942009-06-16 10:30:20 +02001939 return 0;
Peter Oberparleiter37de53b2009-12-07 12:51:19 +01001940 ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
Sebastian Ott823d4942009-06-16 10:30:20 +02001941 return -ENODEV;
1942}
1943
1944static int ccw_device_pm_restore(struct device *dev)
1945{
1946 struct ccw_device *cdev = to_ccwdev(dev);
Sebastian Ott0d01bb82010-02-26 22:37:29 +01001947 struct subchannel *sch;
1948 int ret = 0;
Sebastian Ott823d4942009-06-16 10:30:20 +02001949
1950 __ccw_device_pm_restore(cdev);
Sebastian Ott0d01bb82010-02-26 22:37:29 +01001951 sch = to_subchannel(cdev->dev.parent);
Sebastian Ott823d4942009-06-16 10:30:20 +02001952 spin_lock_irq(sch->lock);
Sebastian Ott0d01bb82010-02-26 22:37:29 +01001953 if (cio_is_console(sch->schid))
Sebastian Ott823d4942009-06-16 10:30:20 +02001954 goto out_restore;
Sebastian Ott0d01bb82010-02-26 22:37:29 +01001955
Sebastian Ott823d4942009-06-16 10:30:20 +02001956 /* check recognition results */
1957 switch (cdev->private->state) {
1958 case DEV_STATE_OFFLINE:
Sebastian Ott0d01bb82010-02-26 22:37:29 +01001959 case DEV_STATE_ONLINE:
1960 cdev->private->flags.donotify = 0;
Sebastian Ott823d4942009-06-16 10:30:20 +02001961 break;
1962 case DEV_STATE_BOXED:
1963 ret = resume_handle_boxed(cdev);
Sebastian Ott823d4942009-06-16 10:30:20 +02001964 if (ret)
Sebastian Ott0d01bb82010-02-26 22:37:29 +01001965 goto out_unlock;
Sebastian Ott823d4942009-06-16 10:30:20 +02001966 goto out_restore;
Sebastian Ott823d4942009-06-16 10:30:20 +02001967 default:
Sebastian Ott0d01bb82010-02-26 22:37:29 +01001968 ret = resume_handle_disc(cdev);
1969 if (ret)
1970 goto out_unlock;
1971 goto out_restore;
Sebastian Ott823d4942009-06-16 10:30:20 +02001972 }
1973 /* check if the device type has changed */
1974 if (!ccw_device_test_sense_data(cdev)) {
1975 ccw_device_update_sense_data(cdev);
Peter Oberparleiter37de53b2009-12-07 12:51:19 +01001976 ccw_device_sched_todo(cdev, CDEV_TODO_REBIND);
Sebastian Ott823d4942009-06-16 10:30:20 +02001977 ret = -ENODEV;
1978 goto out_unlock;
1979 }
Sebastian Ott0d01bb82010-02-26 22:37:29 +01001980 if (!cdev->online)
1981 goto out_unlock;
1982
1983 if (ccw_device_online(cdev)) {
1984 ret = resume_handle_disc(cdev);
1985 if (ret)
1986 goto out_unlock;
1987 goto out_restore;
1988 }
1989 spin_unlock_irq(sch->lock);
1990 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
1991 spin_lock_irq(sch->lock);
1992
1993 if (ccw_device_notify(cdev, CIO_OPER) == NOTIFY_BAD) {
1994 ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
1995 ret = -ENODEV;
Sebastian Ott823d4942009-06-16 10:30:20 +02001996 goto out_unlock;
1997 }
Sebastian Ott823d4942009-06-16 10:30:20 +02001998
Sebastian Ott0d01bb82010-02-26 22:37:29 +01001999 /* reenable cmf, if needed */
2000 if (cdev->private->cmb) {
2001 spin_unlock_irq(sch->lock);
Sebastian Ott823d4942009-06-16 10:30:20 +02002002 ret = ccw_set_cmf(cdev, 1);
Sebastian Ott0d01bb82010-02-26 22:37:29 +01002003 spin_lock_irq(sch->lock);
Sebastian Ott823d4942009-06-16 10:30:20 +02002004 if (ret) {
Sebastian Ottf0148242009-09-11 10:28:23 +02002005 CIO_MSG_EVENT(2, "resume: cdev 0.%x.%04x: cmf failed "
2006 "(rc=%d)\n", cdev->private->dev_id.ssid,
2007 cdev->private->dev_id.devno, ret);
Sebastian Ott823d4942009-06-16 10:30:20 +02002008 ret = 0;
2009 }
2010 }
2011
2012out_restore:
Sebastian Ott0d01bb82010-02-26 22:37:29 +01002013 spin_unlock_irq(sch->lock);
Sebastian Ott823d4942009-06-16 10:30:20 +02002014 if (cdev->online && cdev->drv && cdev->drv->restore)
2015 ret = cdev->drv->restore(cdev);
Sebastian Ott823d4942009-06-16 10:30:20 +02002016 return ret;
2017
Sebastian Ott823d4942009-06-16 10:30:20 +02002018out_unlock:
2019 spin_unlock_irq(sch->lock);
2020 return ret;
2021}
2022
Alexey Dobriyan47145212009-12-14 18:00:08 -08002023static const struct dev_pm_ops ccw_pm_ops = {
Sebastian Ott823d4942009-06-16 10:30:20 +02002024 .prepare = ccw_device_pm_prepare,
2025 .complete = ccw_device_pm_complete,
2026 .freeze = ccw_device_pm_freeze,
2027 .thaw = ccw_device_pm_thaw,
2028 .restore = ccw_device_pm_restore,
2029};
2030
Sebastian Ottd5ab5272011-03-23 10:16:03 +01002031static struct bus_type ccw_bus_type = {
Cornelia Huck8bbace72006-01-11 10:56:22 +01002032 .name = "ccw",
2033 .match = ccw_bus_match,
2034 .uevent = ccw_uevent,
2035 .probe = ccw_device_probe,
2036 .remove = ccw_device_remove,
Cornelia Huck958974fb2007-10-12 16:11:21 +02002037 .shutdown = ccw_device_shutdown,
Sebastian Ott823d4942009-06-16 10:30:20 +02002038 .pm = &ccw_pm_ops,
Cornelia Huck8bbace72006-01-11 10:56:22 +01002039};
2040
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +02002041/**
2042 * ccw_driver_register() - register a ccw driver
2043 * @cdriver: driver to be registered
2044 *
2045 * This function is mainly a wrapper around driver_register().
2046 * Returns:
2047 * %0 on success and a negative error value on failure.
2048 */
2049int ccw_driver_register(struct ccw_driver *cdriver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050{
2051 struct device_driver *drv = &cdriver->driver;
2052
2053 drv->bus = &ccw_bus_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054
2055 return driver_register(drv);
2056}
2057
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +02002058/**
2059 * ccw_driver_unregister() - deregister a ccw driver
2060 * @cdriver: driver to be deregistered
2061 *
2062 * This function is mainly a wrapper around driver_unregister().
2063 */
2064void ccw_driver_unregister(struct ccw_driver *cdriver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065{
2066 driver_unregister(&cdriver->driver);
2067}
2068
Peter Oberparleiter37de53b2009-12-07 12:51:19 +01002069static void ccw_device_todo(struct work_struct *work)
2070{
2071 struct ccw_device_private *priv;
2072 struct ccw_device *cdev;
2073 struct subchannel *sch;
2074 enum cdev_todo todo;
2075
2076 priv = container_of(work, struct ccw_device_private, todo_work);
2077 cdev = priv->cdev;
2078 sch = to_subchannel(cdev->dev.parent);
2079 /* Find out todo. */
2080 spin_lock_irq(cdev->ccwlock);
2081 todo = priv->todo;
2082 priv->todo = CDEV_TODO_NOTHING;
2083 CIO_MSG_EVENT(4, "cdev_todo: cdev=0.%x.%04x todo=%d\n",
2084 priv->dev_id.ssid, priv->dev_id.devno, todo);
2085 spin_unlock_irq(cdev->ccwlock);
2086 /* Perform todo. */
2087 switch (todo) {
2088 case CDEV_TODO_ENABLE_CMF:
2089 cmf_reenable(cdev);
2090 break;
2091 case CDEV_TODO_REBIND:
2092 ccw_device_do_unbind_bind(cdev);
2093 break;
2094 case CDEV_TODO_REGISTER:
2095 io_subchannel_register(cdev);
2096 break;
2097 case CDEV_TODO_UNREG_EVAL:
2098 if (!sch_is_pseudo_sch(sch))
2099 css_schedule_eval(sch->schid);
2100 /* fall-through */
2101 case CDEV_TODO_UNREG:
2102 if (sch_is_pseudo_sch(sch))
2103 ccw_device_unregister(cdev);
2104 else
2105 ccw_device_call_sch_unregister(cdev);
2106 break;
2107 default:
2108 break;
2109 }
2110 /* Release workqueue ref. */
2111 put_device(&cdev->dev);
2112}
2113
2114/**
2115 * ccw_device_sched_todo - schedule ccw device operation
2116 * @cdev: ccw device
2117 * @todo: todo
2118 *
2119 * Schedule the operation identified by @todo to be performed on the slow path
2120 * workqueue. Do nothing if another operation with higher priority is already
2121 * scheduled. Needs to be called with ccwdev lock held.
2122 */
2123void ccw_device_sched_todo(struct ccw_device *cdev, enum cdev_todo todo)
2124{
2125 CIO_MSG_EVENT(4, "cdev_todo: sched cdev=0.%x.%04x todo=%d\n",
2126 cdev->private->dev_id.ssid, cdev->private->dev_id.devno,
2127 todo);
2128 if (cdev->private->todo >= todo)
2129 return;
2130 cdev->private->todo = todo;
2131 /* Get workqueue ref. */
2132 if (!get_device(&cdev->dev))
2133 return;
Sebastian Ottbe5d3822010-02-26 22:37:24 +01002134 if (!queue_work(cio_work_q, &cdev->private->todo_work)) {
Peter Oberparleiter37de53b2009-12-07 12:51:19 +01002135 /* Already queued, release workqueue ref. */
2136 put_device(&cdev->dev);
2137 }
2138}
2139
Michael Ernstfd0457a2010-08-09 18:12:50 +02002140/**
2141 * ccw_device_siosl() - initiate logging
2142 * @cdev: ccw device
2143 *
2144 * This function is used to invoke model-dependent logging within the channel
2145 * subsystem.
2146 */
2147int ccw_device_siosl(struct ccw_device *cdev)
2148{
2149 struct subchannel *sch = to_subchannel(cdev->dev.parent);
2150
2151 return chsc_siosl(sch->schid);
2152}
2153EXPORT_SYMBOL_GPL(ccw_device_siosl);
2154
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155MODULE_LICENSE("GPL");
2156EXPORT_SYMBOL(ccw_device_set_online);
2157EXPORT_SYMBOL(ccw_device_set_offline);
2158EXPORT_SYMBOL(ccw_driver_register);
2159EXPORT_SYMBOL(ccw_driver_unregister);
2160EXPORT_SYMBOL(get_ccwdev_by_busid);