blob: 7582a1e35eb8ded7299900c48dbfcc7753115075 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * drivers/s390/cio/device.c
3 * bus driver for ccw devices
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Cornelia Huckc820de32008-07-14 09:58:45 +02005 * Copyright IBM Corp. 2002,2008
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * Author(s): Arnd Bergmann (arndb@de.ibm.com)
Cornelia Huck4ce3b302006-01-14 13:21:04 -08007 * Cornelia Huck (cornelia.huck@de.ibm.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * Martin Schwidefsky (schwidefsky@de.ibm.com)
9 */
Peter Oberparleitera7ae2c02009-12-07 12:51:20 +010010
11#define KMSG_COMPONENT "cio"
12#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
13
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/module.h>
15#include <linux/init.h>
16#include <linux/spinlock.h>
17#include <linux/errno.h>
18#include <linux/err.h>
19#include <linux/slab.h>
20#include <linux/list.h>
21#include <linux/device.h>
22#include <linux/workqueue.h>
Peter Oberparleiter90ab1332008-01-26 14:10:52 +010023#include <linux/timer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
25#include <asm/ccwdev.h>
26#include <asm/cio.h>
Tim Schmielau4e57b682005-10-30 15:03:48 -080027#include <asm/param.h> /* HZ */
Cornelia Huck1842f2b2007-10-12 16:11:22 +020028#include <asm/cmb.h>
Cornelia Huck3a3fc292008-07-14 09:58:58 +020029#include <asm/isc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +020031#include "chp.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include "cio.h"
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +010033#include "cio_debug.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include "css.h"
35#include "device.h"
36#include "ioasm.h"
Cornelia Huckcd6b4f22008-01-26 14:10:43 +010037#include "io_sch.h"
Peter Oberparleiterecf5d9e2008-10-10 21:33:06 +020038#include "blacklist.h"
Michael Ernstfd0457a2010-08-09 18:12:50 +020039#include "chsc.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Peter Oberparleiter90ab1332008-01-26 14:10:52 +010041static struct timer_list recovery_timer;
Cornelia Huck486d0a02008-02-19 15:29:23 +010042static DEFINE_SPINLOCK(recovery_lock);
Peter Oberparleiter90ab1332008-01-26 14:10:52 +010043static int recovery_phase;
44static const unsigned long recovery_delay[] = { 3, 30, 300 };
45
Linus Torvalds1da177e2005-04-16 15:20:36 -070046/******************* bus type handling ***********************/
47
48/* The Linux driver model distinguishes between a bus type and
49 * the bus itself. Of course we only have one channel
50 * subsystem driver and one channel system per machine, but
51 * we still use the abstraction. T.R. says it's a good idea. */
52static int
53ccw_bus_match (struct device * dev, struct device_driver * drv)
54{
55 struct ccw_device *cdev = to_ccwdev(dev);
56 struct ccw_driver *cdrv = to_ccwdrv(drv);
57 const struct ccw_device_id *ids = cdrv->ids, *found;
58
59 if (!ids)
60 return 0;
61
62 found = ccw_device_id_match(ids, &cdev->id);
63 if (!found)
64 return 0;
65
66 cdev->id.driver_info = found->driver_info;
67
68 return 1;
69}
70
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +020071/* Store modalias string delimited by prefix/suffix string into buffer with
72 * specified size. Return length of resulting string (excluding trailing '\0')
73 * even if string doesn't fit buffer (snprintf semantics). */
Cornelia Huckcfbe9bb2007-04-27 16:01:32 +020074static int snprint_alias(char *buf, size_t size,
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +020075 struct ccw_device_id *id, const char *suffix)
76{
77 int len;
78
Cornelia Huckcfbe9bb2007-04-27 16:01:32 +020079 len = snprintf(buf, size, "ccw:t%04Xm%02X", id->cu_type, id->cu_model);
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +020080 if (len > size)
81 return len;
82 buf += len;
83 size -= len;
84
85 if (id->dev_type != 0)
86 len += snprintf(buf, size, "dt%04Xdm%02X%s", id->dev_type,
87 id->dev_model, suffix);
88 else
89 len += snprintf(buf, size, "dtdm%s", suffix);
90
91 return len;
92}
93
94/* Set up environment variables for ccw device uevent. Return 0 on success,
95 * non-zero otherwise. */
Kay Sievers7eff2e72007-08-14 15:15:12 +020096static int ccw_uevent(struct device *dev, struct kobj_uevent_env *env)
Linus Torvalds1da177e2005-04-16 15:20:36 -070097{
98 struct ccw_device *cdev = to_ccwdev(dev);
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +020099 struct ccw_device_id *id = &(cdev->id);
Cornelia Huckcfbe9bb2007-04-27 16:01:32 +0200100 int ret;
101 char modalias_buf[30];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +0200103 /* CU_TYPE= */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200104 ret = add_uevent_var(env, "CU_TYPE=%04X", id->cu_type);
Cornelia Huckcfbe9bb2007-04-27 16:01:32 +0200105 if (ret)
106 return ret;
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +0200107
108 /* CU_MODEL= */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200109 ret = add_uevent_var(env, "CU_MODEL=%02X", id->cu_model);
Cornelia Huckcfbe9bb2007-04-27 16:01:32 +0200110 if (ret)
111 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
113 /* The next two can be zero, that's ok for us */
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +0200114 /* DEV_TYPE= */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200115 ret = add_uevent_var(env, "DEV_TYPE=%04X", id->dev_type);
Cornelia Huckcfbe9bb2007-04-27 16:01:32 +0200116 if (ret)
117 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +0200119 /* DEV_MODEL= */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200120 ret = add_uevent_var(env, "DEV_MODEL=%02X", id->dev_model);
Cornelia Huckcfbe9bb2007-04-27 16:01:32 +0200121 if (ret)
122 return ret;
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +0200123
124 /* MODALIAS= */
Cornelia Huckcfbe9bb2007-04-27 16:01:32 +0200125 snprint_alias(modalias_buf, sizeof(modalias_buf), id, "");
Kay Sievers7eff2e72007-08-14 15:15:12 +0200126 ret = add_uevent_var(env, "MODALIAS=%s", modalias_buf);
127 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128}
129
Cornelia Huck8bbace72006-01-11 10:56:22 +0100130struct bus_type ccw_bus_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
Cornelia Huck602b20f2008-01-26 14:10:39 +0100132static void io_subchannel_irq(struct subchannel *);
133static int io_subchannel_probe(struct subchannel *);
134static int io_subchannel_remove(struct subchannel *);
Cornelia Huck8bbace72006-01-11 10:56:22 +0100135static void io_subchannel_shutdown(struct subchannel *);
Cornelia Huckc820de32008-07-14 09:58:45 +0200136static int io_subchannel_sch_event(struct subchannel *, int);
Cornelia Huck99611f82008-07-14 09:59:02 +0200137static int io_subchannel_chp_event(struct subchannel *, struct chp_link *,
138 int);
Sebastian Ott8ea7f552009-09-22 22:58:35 +0200139static void recovery_func(unsigned long data);
Sebastian Ott8ea7f552009-09-22 22:58:35 +0200140wait_queue_head_t ccw_device_init_wq;
141atomic_t ccw_device_init_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
Cornelia Huckf08adc02008-07-14 09:59:03 +0200143static struct css_device_id io_subchannel_ids[] = {
144 { .match_flags = 0x1, .type = SUBCHANNEL_TYPE_IO, },
145 { /* end of list */ },
146};
147MODULE_DEVICE_TABLE(css, io_subchannel_ids);
148
Cornelia Huck93a27592009-06-16 10:30:23 +0200149static int io_subchannel_prepare(struct subchannel *sch)
150{
151 struct ccw_device *cdev;
152 /*
153 * Don't allow suspend while a ccw device registration
154 * is still outstanding.
155 */
156 cdev = sch_get_cdev(sch);
157 if (cdev && !device_is_registered(&cdev->dev))
158 return -EAGAIN;
159 return 0;
160}
161
Sebastian Ottb4c70722010-02-26 22:37:27 +0100162static int io_subchannel_settle(void)
Sebastian Ott8ea7f552009-09-22 22:58:35 +0200163{
Sebastian Ottb4c70722010-02-26 22:37:27 +0100164 int ret;
165
166 ret = wait_event_interruptible(ccw_device_init_wq,
167 atomic_read(&ccw_device_init_count) == 0);
168 if (ret)
169 return -EINTR;
Sebastian Ottbe5d3822010-02-26 22:37:24 +0100170 flush_workqueue(cio_work_q);
Sebastian Ottb4c70722010-02-26 22:37:27 +0100171 return 0;
Sebastian Ott8ea7f552009-09-22 22:58:35 +0200172}
173
Cornelia Huckf7e5d672007-05-10 15:45:43 +0200174static struct css_driver io_subchannel_driver = {
Cornelia Huck4beee642008-01-26 14:10:47 +0100175 .owner = THIS_MODULE,
Cornelia Huckf08adc02008-07-14 09:59:03 +0200176 .subchannel_type = io_subchannel_ids,
Cornelia Huck25b7bb52008-01-26 14:10:41 +0100177 .name = "io_subchannel",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 .irq = io_subchannel_irq,
Cornelia Huckc820de32008-07-14 09:58:45 +0200179 .sch_event = io_subchannel_sch_event,
180 .chp_event = io_subchannel_chp_event,
Cornelia Huck8bbace72006-01-11 10:56:22 +0100181 .probe = io_subchannel_probe,
182 .remove = io_subchannel_remove,
183 .shutdown = io_subchannel_shutdown,
Cornelia Huck93a27592009-06-16 10:30:23 +0200184 .prepare = io_subchannel_prepare,
Sebastian Ott8ea7f552009-09-22 22:58:35 +0200185 .settle = io_subchannel_settle,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186};
187
Sebastian Ott2f176442009-09-22 22:58:33 +0200188int __init io_subchannel_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189{
190 int ret;
191
192 init_waitqueue_head(&ccw_device_init_wq);
193 atomic_set(&ccw_device_init_count, 0);
Peter Oberparleiter90ab1332008-01-26 14:10:52 +0100194 setup_timer(&recovery_timer, recovery_func, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
Sebastian Ottbe5d3822010-02-26 22:37:24 +0100196 ret = bus_register(&ccw_bus_type);
197 if (ret)
198 return ret;
Cornelia Huck25b7bb52008-01-26 14:10:41 +0100199 ret = css_driver_register(&io_subchannel_driver);
200 if (ret)
Sebastian Ottbe5d3822010-02-26 22:37:24 +0100201 bus_unregister(&ccw_bus_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 return ret;
204}
205
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
207/************************ device handling **************************/
208
209/*
210 * A ccw_device has some interfaces in sysfs in addition to the
211 * standard ones.
212 * The following entries are designed to export the information which
213 * resided in 2.4 in /proc/subchannels. Subchannel and device number
214 * are obvious, so they don't have an entry :)
215 * TODO: Split chpids and pimpampom up? Where is "in use" in the tree?
216 */
217static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400218chpids_show (struct device * dev, struct device_attribute *attr, char * buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219{
220 struct subchannel *sch = to_subchannel(dev);
Peter Oberparleiter7ad6a242007-04-27 16:01:35 +0200221 struct chsc_ssd_info *ssd = &sch->ssd_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 ssize_t ret = 0;
223 int chp;
Peter Oberparleiter7ad6a242007-04-27 16:01:35 +0200224 int mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
Peter Oberparleiter7ad6a242007-04-27 16:01:35 +0200226 for (chp = 0; chp < 8; chp++) {
227 mask = 0x80 >> chp;
228 if (ssd->path_mask & mask)
229 ret += sprintf(buf + ret, "%02x ", ssd->chpid[chp].id);
230 else
231 ret += sprintf(buf + ret, "00 ");
232 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 ret += sprintf (buf+ret, "\n");
234 return min((ssize_t)PAGE_SIZE, ret);
235}
236
237static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400238pimpampom_show (struct device * dev, struct device_attribute *attr, char * buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239{
240 struct subchannel *sch = to_subchannel(dev);
241 struct pmcw *pmcw = &sch->schib.pmcw;
242
243 return sprintf (buf, "%02x %02x %02x\n",
244 pmcw->pim, pmcw->pam, pmcw->pom);
245}
246
247static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400248devtype_show (struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249{
250 struct ccw_device *cdev = to_ccwdev(dev);
251 struct ccw_device_id *id = &(cdev->id);
252
253 if (id->dev_type != 0)
254 return sprintf(buf, "%04x/%02x\n",
255 id->dev_type, id->dev_model);
256 else
257 return sprintf(buf, "n/a\n");
258}
259
260static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400261cutype_show (struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262{
263 struct ccw_device *cdev = to_ccwdev(dev);
264 struct ccw_device_id *id = &(cdev->id);
265
266 return sprintf(buf, "%04x/%02x\n",
267 id->cu_type, id->cu_model);
268}
269
270static ssize_t
Bastian Blankf1fc78a2005-10-30 15:00:12 -0800271modalias_show (struct device *dev, struct device_attribute *attr, char *buf)
272{
273 struct ccw_device *cdev = to_ccwdev(dev);
274 struct ccw_device_id *id = &(cdev->id);
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +0200275 int len;
Bastian Blankf1fc78a2005-10-30 15:00:12 -0800276
Cornelia Huck086a6c62007-07-17 13:36:08 +0200277 len = snprint_alias(buf, PAGE_SIZE, id, "\n");
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +0200278
279 return len > PAGE_SIZE ? PAGE_SIZE : len;
Bastian Blankf1fc78a2005-10-30 15:00:12 -0800280}
281
282static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400283online_show (struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284{
285 struct ccw_device *cdev = to_ccwdev(dev);
286
287 return sprintf(buf, cdev->online ? "1\n" : "0\n");
288}
289
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100290int ccw_device_is_orphan(struct ccw_device *cdev)
291{
292 return sch_is_pseudo_sch(to_subchannel(cdev->dev.parent));
293}
294
Cornelia Huckef995162007-04-27 16:01:39 +0200295static void ccw_device_unregister(struct ccw_device *cdev)
Cornelia Huck7674da72006-12-08 15:54:21 +0100296{
Sebastian Ott7d253b92009-12-07 12:51:33 +0100297 if (device_is_registered(&cdev->dev)) {
Sebastian Ott24a18722009-12-07 12:51:34 +0100298 /* Undo device_add(). */
Cornelia Huckef995162007-04-27 16:01:39 +0200299 device_del(&cdev->dev);
Sebastian Ott24a18722009-12-07 12:51:34 +0100300 }
301 if (cdev->private->flags.initialized) {
302 cdev->private->flags.initialized = 0;
Sebastian Ott3b554a12009-09-11 10:28:26 +0200303 /* Release reference from device_initialize(). */
304 put_device(&cdev->dev);
305 }
Cornelia Huck7674da72006-12-08 15:54:21 +0100306}
307
Sebastian Ottd40f7b72009-12-07 12:51:41 +0100308static void io_subchannel_quiesce(struct subchannel *);
309
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +0200310/**
311 * ccw_device_set_offline() - disable a ccw device for I/O
312 * @cdev: target ccw device
313 *
314 * This function calls the driver's set_offline() function for @cdev, if
315 * given, and then disables @cdev.
316 * Returns:
317 * %0 on success and a negative error value on failure.
318 * Context:
319 * enabled, ccw device lock not held
320 */
321int ccw_device_set_offline(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322{
Sebastian Ottd40f7b72009-12-07 12:51:41 +0100323 struct subchannel *sch;
324 int ret, state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
326 if (!cdev)
327 return -ENODEV;
328 if (!cdev->online || !cdev->drv)
329 return -EINVAL;
330
331 if (cdev->drv->set_offline) {
332 ret = cdev->drv->set_offline(cdev);
333 if (ret != 0)
334 return ret;
335 }
336 cdev->online = 0;
337 spin_lock_irq(cdev->ccwlock);
Sebastian Ottd40f7b72009-12-07 12:51:41 +0100338 sch = to_subchannel(cdev->dev.parent);
Michael Ernst217ee6c2009-09-11 10:28:21 +0200339 /* Wait until a final state or DISCONNECTED is reached */
340 while (!dev_fsm_final_state(cdev) &&
341 cdev->private->state != DEV_STATE_DISCONNECTED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 spin_unlock_irq(cdev->ccwlock);
Michael Ernst217ee6c2009-09-11 10:28:21 +0200343 wait_event(cdev->private->wait_q, (dev_fsm_final_state(cdev) ||
344 cdev->private->state == DEV_STATE_DISCONNECTED));
345 spin_lock_irq(cdev->ccwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 }
Sebastian Ottd40f7b72009-12-07 12:51:41 +0100347 do {
348 ret = ccw_device_offline(cdev);
349 if (!ret)
350 break;
351 CIO_MSG_EVENT(0, "ccw_device_offline returned %d, device "
352 "0.%x.%04x\n", ret, cdev->private->dev_id.ssid,
353 cdev->private->dev_id.devno);
354 if (ret != -EBUSY)
355 goto error;
356 state = cdev->private->state;
357 spin_unlock_irq(cdev->ccwlock);
358 io_subchannel_quiesce(sch);
359 spin_lock_irq(cdev->ccwlock);
360 cdev->private->state = state;
361 } while (ret == -EBUSY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 spin_unlock_irq(cdev->ccwlock);
Michael Ernst217ee6c2009-09-11 10:28:21 +0200363 wait_event(cdev->private->wait_q, (dev_fsm_final_state(cdev) ||
364 cdev->private->state == DEV_STATE_DISCONNECTED));
Peter Oberparleitera7ae2c02009-12-07 12:51:20 +0100365 /* Inform the user if set offline failed. */
366 if (cdev->private->state == DEV_STATE_BOXED) {
367 pr_warning("%s: The device entered boxed state while "
368 "being set offline\n", dev_name(&cdev->dev));
369 } else if (cdev->private->state == DEV_STATE_NOT_OPER) {
370 pr_warning("%s: The device stopped operating while "
371 "being set offline\n", dev_name(&cdev->dev));
372 }
Michael Ernst217ee6c2009-09-11 10:28:21 +0200373 /* Give up reference from ccw_device_set_online(). */
374 put_device(&cdev->dev);
375 return 0;
376
377error:
Michael Ernst217ee6c2009-09-11 10:28:21 +0200378 cdev->private->state = DEV_STATE_OFFLINE;
379 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
380 spin_unlock_irq(cdev->ccwlock);
381 /* Give up reference from ccw_device_set_online(). */
382 put_device(&cdev->dev);
383 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384}
385
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +0200386/**
387 * ccw_device_set_online() - enable a ccw device for I/O
388 * @cdev: target ccw device
389 *
390 * This function first enables @cdev and then calls the driver's set_online()
391 * function for @cdev, if given. If set_online() returns an error, @cdev is
392 * disabled again.
393 * Returns:
394 * %0 on success and a negative error value on failure.
395 * Context:
396 * enabled, ccw device lock not held
397 */
398int ccw_device_set_online(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399{
400 int ret;
Michael Ernst217ee6c2009-09-11 10:28:21 +0200401 int ret2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
403 if (!cdev)
404 return -ENODEV;
405 if (cdev->online || !cdev->drv)
406 return -EINVAL;
Cornelia Huck9cd67422008-12-25 13:39:06 +0100407 /* Hold on to an extra reference while device is online. */
408 if (!get_device(&cdev->dev))
409 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
411 spin_lock_irq(cdev->ccwlock);
412 ret = ccw_device_online(cdev);
413 spin_unlock_irq(cdev->ccwlock);
414 if (ret == 0)
415 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
416 else {
Michael 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 Oberparleitera7ae2c02009-12-07 12:51:20 +0100430 /* Inform the user that set online failed. */
431 if (cdev->private->state == DEV_STATE_BOXED) {
432 pr_warning("%s: Setting the device online failed "
433 "because it is boxed\n",
434 dev_name(&cdev->dev));
435 } else if (cdev->private->state == DEV_STATE_NOT_OPER) {
436 pr_warning("%s: Setting the device online failed "
437 "because it is not operational\n",
438 dev_name(&cdev->dev));
439 }
Cornelia Huck9cd67422008-12-25 13:39:06 +0100440 /* Give up online reference since onlining failed. */
441 put_device(&cdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 return -ENODEV;
Cornelia Huck9cd67422008-12-25 13:39:06 +0100443 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 spin_unlock_irq(cdev->ccwlock);
Michael Ernst217ee6c2009-09-11 10:28:21 +0200445 if (cdev->drv->set_online)
446 ret = cdev->drv->set_online(cdev);
447 if (ret)
448 goto rollback;
449 cdev->online = 1;
450 return 0;
451
452rollback:
453 spin_lock_irq(cdev->ccwlock);
454 /* Wait until a final state or DISCONNECTED is reached */
455 while (!dev_fsm_final_state(cdev) &&
456 cdev->private->state != DEV_STATE_DISCONNECTED) {
457 spin_unlock_irq(cdev->ccwlock);
458 wait_event(cdev->private->wait_q, (dev_fsm_final_state(cdev) ||
459 cdev->private->state == DEV_STATE_DISCONNECTED));
460 spin_lock_irq(cdev->ccwlock);
461 }
462 ret2 = ccw_device_offline(cdev);
463 if (ret2)
464 goto error;
465 spin_unlock_irq(cdev->ccwlock);
466 wait_event(cdev->private->wait_q, (dev_fsm_final_state(cdev) ||
467 cdev->private->state == DEV_STATE_DISCONNECTED));
Cornelia Huck9cd67422008-12-25 13:39:06 +0100468 /* Give up online reference since onlining failed. */
469 put_device(&cdev->dev);
Michael Ernst217ee6c2009-09-11 10:28:21 +0200470 return ret;
471
472error:
473 CIO_MSG_EVENT(0, "rollback ccw_device_offline returned %d, "
474 "device 0.%x.%04x\n",
475 ret2, cdev->private->dev_id.ssid,
476 cdev->private->dev_id.devno);
477 cdev->private->state = DEV_STATE_OFFLINE;
478 spin_unlock_irq(cdev->ccwlock);
479 /* Give up online reference since onlining failed. */
480 put_device(&cdev->dev);
481 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482}
483
Sebastian Otte74fe0c2009-03-26 15:24:08 +0100484static int online_store_handle_offline(struct ccw_device *cdev)
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200485{
Peter Oberparleiter37de53b2009-12-07 12:51:19 +0100486 if (cdev->private->state == DEV_STATE_DISCONNECTED) {
487 spin_lock_irq(cdev->ccwlock);
488 ccw_device_sched_todo(cdev, CDEV_TODO_UNREG_EVAL);
489 spin_unlock_irq(cdev->ccwlock);
Sebastian Ott7cd40312010-08-09 18:12:52 +0200490 return 0;
491 }
492 if (cdev->drv && cdev->drv->set_offline)
Sebastian Otte74fe0c2009-03-26 15:24:08 +0100493 return ccw_device_set_offline(cdev);
Sebastian Ott7cd40312010-08-09 18:12:52 +0200494 return -EINVAL;
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200495}
496
497static int online_store_recog_and_online(struct ccw_device *cdev)
498{
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200499 /* Do device recognition, if needed. */
Sebastian Ott99f6a5702009-03-31 19:16:07 +0200500 if (cdev->private->state == DEV_STATE_BOXED) {
Peter Oberparleiter1f5bd382009-12-07 12:51:23 +0100501 spin_lock_irq(cdev->ccwlock);
Peter Oberparleiter736b5db2009-12-07 12:51:21 +0100502 ccw_device_recognition(cdev);
Peter Oberparleiter1f5bd382009-12-07 12:51:23 +0100503 spin_unlock_irq(cdev->ccwlock);
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200504 wait_event(cdev->private->wait_q,
505 cdev->private->flags.recog_done);
Sebastian Ott156013f2009-03-31 19:16:03 +0200506 if (cdev->private->state != DEV_STATE_OFFLINE)
507 /* recognition failed */
508 return -EAGAIN;
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200509 }
510 if (cdev->drv && cdev->drv->set_online)
Sebastian Ott7cd40312010-08-09 18:12:52 +0200511 return ccw_device_set_online(cdev);
512 return -EINVAL;
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200513}
Sebastian Ott156013f2009-03-31 19:16:03 +0200514
Michael Ernstc78aa6c2008-07-14 09:59:22 +0200515static int online_store_handle_online(struct ccw_device *cdev, int force)
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200516{
517 int ret;
518
519 ret = online_store_recog_and_online(cdev);
Sebastian Ott156013f2009-03-31 19:16:03 +0200520 if (ret && !force)
Michael Ernstc78aa6c2008-07-14 09:59:22 +0200521 return ret;
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200522 if (force && cdev->private->state == DEV_STATE_BOXED) {
523 ret = ccw_device_stlck(cdev);
Michael Ernstc78aa6c2008-07-14 09:59:22 +0200524 if (ret)
525 return ret;
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200526 if (cdev->id.cu_type == 0)
527 cdev->private->state = DEV_STATE_NOT_OPER;
Sebastian Ott156013f2009-03-31 19:16:03 +0200528 ret = online_store_recog_and_online(cdev);
529 if (ret)
530 return ret;
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200531 }
Michael Ernstc78aa6c2008-07-14 09:59:22 +0200532 return 0;
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200533}
534
535static ssize_t online_store (struct device *dev, struct device_attribute *attr,
536 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537{
538 struct ccw_device *cdev = to_ccwdev(dev);
Cornelia Huck2f972202008-04-30 13:38:33 +0200539 int force, ret;
540 unsigned long i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
Peter Oberparleiter350e9122009-12-07 12:51:28 +0100542 if (!dev_fsm_final_state(cdev) &&
543 cdev->private->state != DEV_STATE_DISCONNECTED)
544 return -EAGAIN;
545 if (atomic_cmpxchg(&cdev->private->onoff, 0, 1) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 return -EAGAIN;
547
548 if (cdev->drv && !try_module_get(cdev->drv->owner)) {
549 atomic_set(&cdev->private->onoff, 0);
550 return -EINVAL;
551 }
552 if (!strncmp(buf, "force\n", count)) {
553 force = 1;
554 i = 1;
Cornelia Huck2f972202008-04-30 13:38:33 +0200555 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 } else {
557 force = 0;
Cornelia Huck2f972202008-04-30 13:38:33 +0200558 ret = strict_strtoul(buf, 16, &i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 }
Cornelia Huck2f972202008-04-30 13:38:33 +0200560 if (ret)
561 goto out;
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200562 switch (i) {
563 case 0:
Sebastian Otte74fe0c2009-03-26 15:24:08 +0100564 ret = online_store_handle_offline(cdev);
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200565 break;
566 case 1:
Michael Ernstc78aa6c2008-07-14 09:59:22 +0200567 ret = online_store_handle_online(cdev, force);
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200568 break;
569 default:
Cornelia Huck2f972202008-04-30 13:38:33 +0200570 ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 }
Cornelia Huck2f972202008-04-30 13:38:33 +0200572out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 if (cdev->drv)
574 module_put(cdev->drv->owner);
575 atomic_set(&cdev->private->onoff, 0);
Sebastian Otte74fe0c2009-03-26 15:24:08 +0100576 return (ret < 0) ? ret : count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577}
578
579static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400580available_show (struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581{
582 struct ccw_device *cdev = to_ccwdev(dev);
583 struct subchannel *sch;
584
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100585 if (ccw_device_is_orphan(cdev))
586 return sprintf(buf, "no device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 switch (cdev->private->state) {
588 case DEV_STATE_BOXED:
589 return sprintf(buf, "boxed\n");
590 case DEV_STATE_DISCONNECTED:
591 case DEV_STATE_DISCONNECTED_SENSE_ID:
592 case DEV_STATE_NOT_OPER:
593 sch = to_subchannel(dev->parent);
594 if (!sch->lpm)
595 return sprintf(buf, "no path\n");
596 else
597 return sprintf(buf, "no device\n");
598 default:
599 /* All other states considered fine. */
600 return sprintf(buf, "good\n");
601 }
602}
603
Michael Ernstfd0457a2010-08-09 18:12:50 +0200604static ssize_t
605initiate_logging(struct device *dev, struct device_attribute *attr,
606 const char *buf, size_t count)
607{
608 struct subchannel *sch = to_subchannel(dev);
609 int rc;
610
611 rc = chsc_siosl(sch->schid);
612 if (rc < 0) {
613 pr_warning("Logging for subchannel 0.%x.%04x failed with "
614 "errno=%d\n",
615 sch->schid.ssid, sch->schid.sch_no, rc);
616 return rc;
617 }
618 pr_notice("Logging for subchannel 0.%x.%04x was triggered\n",
619 sch->schid.ssid, sch->schid.sch_no);
620 return count;
621}
622
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623static DEVICE_ATTR(chpids, 0444, chpids_show, NULL);
624static DEVICE_ATTR(pimpampom, 0444, pimpampom_show, NULL);
625static DEVICE_ATTR(devtype, 0444, devtype_show, NULL);
626static DEVICE_ATTR(cutype, 0444, cutype_show, NULL);
Bastian Blankf1fc78a2005-10-30 15:00:12 -0800627static DEVICE_ATTR(modalias, 0444, modalias_show, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628static DEVICE_ATTR(online, 0644, online_show, online_store);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629static DEVICE_ATTR(availability, 0444, available_show, NULL);
Michael Ernstfd0457a2010-08-09 18:12:50 +0200630static DEVICE_ATTR(logging, 0200, NULL, initiate_logging);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200632static struct attribute *io_subchannel_attrs[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 &dev_attr_chpids.attr,
634 &dev_attr_pimpampom.attr,
Michael Ernstfd0457a2010-08-09 18:12:50 +0200635 &dev_attr_logging.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 NULL,
637};
638
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200639static struct attribute_group io_subchannel_attr_group = {
640 .attrs = io_subchannel_attrs,
Cornelia Huck529192f2006-12-08 15:55:57 +0100641};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
643static struct attribute * ccwdev_attrs[] = {
644 &dev_attr_devtype.attr,
645 &dev_attr_cutype.attr,
Bastian Blankf1fc78a2005-10-30 15:00:12 -0800646 &dev_attr_modalias.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 &dev_attr_online.attr,
648 &dev_attr_cmb_enable.attr,
649 &dev_attr_availability.attr,
650 NULL,
651};
652
653static struct attribute_group ccwdev_attr_group = {
654 .attrs = ccwdev_attrs,
655};
656
David Brownella4dbd672009-06-24 10:06:31 -0700657static const struct attribute_group *ccwdev_attr_groups[] = {
Cornelia Huckef995162007-04-27 16:01:39 +0200658 &ccwdev_attr_group,
659 NULL,
660};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
662/* this is a simple abstraction for device_register that sets the
663 * correct bus type and adds the bus specific files */
Cornelia Huck3c9da7b2006-10-27 12:39:33 +0200664static int ccw_device_register(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665{
666 struct device *dev = &cdev->dev;
667 int ret;
668
669 dev->bus = &ccw_bus_type;
Sebastian Ott3ac276f2009-09-11 10:28:27 +0200670 ret = dev_set_name(&cdev->dev, "0.%x.%04x", cdev->private->dev_id.ssid,
671 cdev->private->dev_id.devno);
672 if (ret)
673 return ret;
Sebastian Ott7d253b92009-12-07 12:51:33 +0100674 return device_add(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675}
676
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100677static int match_dev_id(struct device *dev, void *data)
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700678{
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100679 struct ccw_device *cdev = to_ccwdev(dev);
680 struct ccw_dev_id *dev_id = data;
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700681
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100682 return ccw_dev_id_is_equal(&cdev->private->dev_id, dev_id);
683}
684
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100685static struct ccw_device *get_ccwdev_by_dev_id(struct ccw_dev_id *dev_id)
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100686{
687 struct device *dev;
688
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100689 dev = bus_find_device(&ccw_bus_type, NULL, dev_id, match_dev_id);
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100690
691 return dev ? to_ccwdev(dev) : NULL;
692}
693
Peter Oberparleiter37de53b2009-12-07 12:51:19 +0100694static void ccw_device_do_unbind_bind(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695{
Cornelia Huckeb32ae82009-03-26 15:24:05 +0100696 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
Sebastian Ott7d253b92009-12-07 12:51:33 +0100698 if (device_is_registered(&cdev->dev)) {
Cornelia Huckeb32ae82009-03-26 15:24:05 +0100699 device_release_driver(&cdev->dev);
700 ret = device_attach(&cdev->dev);
701 WARN_ON(ret == -ENODEV);
702 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703}
704
705static void
706ccw_device_release(struct device *dev)
707{
708 struct ccw_device *cdev;
709
710 cdev = to_ccwdev(dev);
Cornelia Huck6eff2082008-12-25 13:39:07 +0100711 /* Release reference of parent subchannel. */
712 put_device(cdev->dev.parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 kfree(cdev->private);
714 kfree(cdev);
715}
716
Cornelia Huck7674da72006-12-08 15:54:21 +0100717static struct ccw_device * io_subchannel_allocate_dev(struct subchannel *sch)
718{
719 struct ccw_device *cdev;
720
721 cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
722 if (cdev) {
723 cdev->private = kzalloc(sizeof(struct ccw_device_private),
724 GFP_KERNEL | GFP_DMA);
725 if (cdev->private)
726 return cdev;
727 }
728 kfree(cdev);
729 return ERR_PTR(-ENOMEM);
730}
731
Peter Oberparleiter37de53b2009-12-07 12:51:19 +0100732static void ccw_device_todo(struct work_struct *work);
733
Cornelia Huck7674da72006-12-08 15:54:21 +0100734static int io_subchannel_initialize_dev(struct subchannel *sch,
735 struct ccw_device *cdev)
736{
737 cdev->private->cdev = cdev;
738 atomic_set(&cdev->private->onoff, 0);
739 cdev->dev.parent = &sch->dev;
740 cdev->dev.release = ccw_device_release;
Peter Oberparleiter37de53b2009-12-07 12:51:19 +0100741 INIT_WORK(&cdev->private->todo_work, ccw_device_todo);
Cornelia Huckef995162007-04-27 16:01:39 +0200742 cdev->dev.groups = ccwdev_attr_groups;
Cornelia Huck7674da72006-12-08 15:54:21 +0100743 /* Do first half of device_register. */
744 device_initialize(&cdev->dev);
745 if (!get_device(&sch->dev)) {
Cornelia Huck283fdd02008-12-25 13:39:10 +0100746 /* Release reference from device_initialize(). */
747 put_device(&cdev->dev);
Cornelia Huck7674da72006-12-08 15:54:21 +0100748 return -ENODEV;
749 }
Sebastian Ott24a18722009-12-07 12:51:34 +0100750 cdev->private->flags.initialized = 1;
Cornelia Huck7674da72006-12-08 15:54:21 +0100751 return 0;
752}
753
754static struct ccw_device * io_subchannel_create_ccwdev(struct subchannel *sch)
755{
756 struct ccw_device *cdev;
757 int ret;
758
759 cdev = io_subchannel_allocate_dev(sch);
760 if (!IS_ERR(cdev)) {
761 ret = io_subchannel_initialize_dev(sch, cdev);
Sebastian Ott06739a82009-08-23 18:09:04 +0200762 if (ret)
Cornelia Huck7674da72006-12-08 15:54:21 +0100763 cdev = ERR_PTR(ret);
Cornelia Huck7674da72006-12-08 15:54:21 +0100764 }
765 return cdev;
766}
767
Peter Oberparleiter736b5db2009-12-07 12:51:21 +0100768static void io_subchannel_recog(struct ccw_device *, struct subchannel *);
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100769
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100770static void sch_create_and_recog_new_device(struct subchannel *sch)
771{
772 struct ccw_device *cdev;
773
774 /* Need to allocate a new ccw device. */
775 cdev = io_subchannel_create_ccwdev(sch);
776 if (IS_ERR(cdev)) {
777 /* OK, we did everything we could... */
778 css_sch_device_unregister(sch);
779 return;
780 }
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100781 /* Start recognition for the new ccw device. */
Peter Oberparleiter736b5db2009-12-07 12:51:21 +0100782 io_subchannel_recog(cdev, sch);
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100783}
784
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785/*
786 * Register recognized device.
787 */
Peter Oberparleiter37de53b2009-12-07 12:51:19 +0100788static void io_subchannel_register(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 struct subchannel *sch;
Sebastian Otta2901562010-03-08 12:25:17 +0100791 int ret, adjust_init_count = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 unsigned long flags;
793
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 sch = to_subchannel(cdev->dev.parent);
Cornelia Huck5fb6b852008-12-25 13:39:08 +0100795 /*
796 * Check if subchannel is still registered. It may have become
797 * unregistered if a machine check hit us after finishing
798 * device recognition but before the register work could be
799 * queued.
800 */
801 if (!device_is_registered(&sch->dev))
802 goto out_err;
Cornelia Huck82b7ac02007-04-27 16:01:36 +0200803 css_update_ssd_info(sch);
Cornelia Huck47af5512006-12-04 15:41:07 +0100804 /*
805 * io_subchannel_register() will also be called after device
806 * recognition has been done for a boxed device (which will already
807 * be registered). We need to reprobe since we may now have sense id
808 * information.
809 */
Cornelia Huckd6a30762008-12-25 13:39:11 +0100810 if (device_is_registered(&cdev->dev)) {
Cornelia Huck47af5512006-12-04 15:41:07 +0100811 if (!cdev->drv) {
812 ret = device_reprobe(&cdev->dev);
813 if (ret)
814 /* We can't do much here. */
Michael Ernst139b83d2008-05-07 09:22:54 +0200815 CIO_MSG_EVENT(0, "device_reprobe() returned"
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200816 " %d for 0.%x.%04x\n", ret,
817 cdev->private->dev_id.ssid,
818 cdev->private->dev_id.devno);
Cornelia Huck47af5512006-12-04 15:41:07 +0100819 }
Sebastian Otta2901562010-03-08 12:25:17 +0100820 adjust_init_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 goto out;
822 }
Cornelia Huckfa1a8c22007-04-26 00:12:03 -0700823 /*
824 * Now we know this subchannel will stay, we can throw
825 * our delayed uevent.
826 */
Ming Leif67f1292009-03-01 21:10:49 +0800827 dev_set_uevent_suppress(&sch->dev, 0);
Cornelia Huckfa1a8c22007-04-26 00:12:03 -0700828 kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 /* make it known to the system */
830 ret = ccw_device_register(cdev);
831 if (ret) {
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200832 CIO_MSG_EVENT(0, "Could not register ccw dev 0.%x.%04x: %d\n",
833 cdev->private->dev_id.ssid,
834 cdev->private->dev_id.devno, ret);
Cornelia Huck2ec22982006-12-08 15:54:26 +0100835 spin_lock_irqsave(sch->lock, flags);
Cornelia Huckdb6a6422008-01-26 14:10:46 +0100836 sch_set_cdev(sch, NULL);
Cornelia Huck2ec22982006-12-08 15:54:26 +0100837 spin_unlock_irqrestore(sch->lock, flags);
Cornelia Huck6eff2082008-12-25 13:39:07 +0100838 /* Release initial device reference. */
839 put_device(&cdev->dev);
Cornelia Huck5fb6b852008-12-25 13:39:08 +0100840 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842out:
843 cdev->private->flags.recog_done = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 wake_up(&cdev->private->wait_q);
Cornelia Huck5fb6b852008-12-25 13:39:08 +0100845out_err:
Sebastian Otta2901562010-03-08 12:25:17 +0100846 if (adjust_init_count && atomic_dec_and_test(&ccw_device_init_count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 wake_up(&ccw_device_init_wq);
848}
849
Peter Oberparleiter37de53b2009-12-07 12:51:19 +0100850static void ccw_device_call_sch_unregister(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 struct subchannel *sch;
853
Peter Oberparleiter46fbe4e2008-10-10 21:33:05 +0200854 /* Get subchannel reference for local processing. */
855 if (!get_device(cdev->dev.parent))
856 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 sch = to_subchannel(cdev->dev.parent);
Cornelia Huck6ab48792006-07-12 16:39:50 +0200858 css_sch_device_unregister(sch);
Peter Oberparleiter46fbe4e2008-10-10 21:33:05 +0200859 /* Release subchannel reference for local processing. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 put_device(&sch->dev);
861}
862
863/*
864 * subchannel recognition done. Called from the state machine.
865 */
866void
867io_subchannel_recog_done(struct ccw_device *cdev)
868{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 if (css_init_done == 0) {
870 cdev->private->flags.recog_done = 1;
871 return;
872 }
873 switch (cdev->private->state) {
Sebastian Ott47593bf2009-03-31 19:16:05 +0200874 case DEV_STATE_BOXED:
875 /* Device did not respond in time. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 case DEV_STATE_NOT_OPER:
877 cdev->private->flags.recog_done = 1;
Peter Oberparleiter37de53b2009-12-07 12:51:19 +0100878 /* Remove device found not operational. */
879 ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 if (atomic_dec_and_test(&ccw_device_init_count))
881 wake_up(&ccw_device_init_wq);
882 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 case DEV_STATE_OFFLINE:
884 /*
885 * We can't register the device in interrupt context so
886 * we schedule a work item.
887 */
Peter Oberparleiter37de53b2009-12-07 12:51:19 +0100888 ccw_device_sched_todo(cdev, CDEV_TODO_REGISTER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 break;
890 }
891}
892
Peter Oberparleiter736b5db2009-12-07 12:51:21 +0100893static void io_subchannel_recog(struct ccw_device *cdev, struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 struct ccw_device_private *priv;
896
Cornelia Huck2ec22982006-12-08 15:54:26 +0100897 cdev->ccwlock = sch->lock;
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800898
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 /* Init private data. */
900 priv = cdev->private;
Cornelia Huck78964262006-10-11 15:31:38 +0200901 priv->dev_id.devno = sch->schib.pmcw.dev;
902 priv->dev_id.ssid = sch->schid.ssid;
903 priv->schid = sch->schid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 priv->state = DEV_STATE_NOT_OPER;
905 INIT_LIST_HEAD(&priv->cmb_list);
906 init_waitqueue_head(&priv->wait_q);
907 init_timer(&priv->timer);
908
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 /* Increase counter of devices currently in recognition. */
910 atomic_inc(&ccw_device_init_count);
911
912 /* Start async. device sensing. */
Cornelia Huck2ec22982006-12-08 15:54:26 +0100913 spin_lock_irq(sch->lock);
Peter Oberparleiter60e4dac2009-12-07 12:51:16 +0100914 sch_set_cdev(sch, cdev);
Peter Oberparleiter736b5db2009-12-07 12:51:21 +0100915 ccw_device_recognition(cdev);
Cornelia Huck2ec22982006-12-08 15:54:26 +0100916 spin_unlock_irq(sch->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917}
918
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100919static int ccw_device_move_to_sch(struct ccw_device *cdev,
920 struct subchannel *sch)
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100921{
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100922 struct subchannel *old_sch;
Sebastian Ott0c609fc2009-12-07 12:51:37 +0100923 int rc, old_enabled = 0;
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100924
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100925 old_sch = to_subchannel(cdev->dev.parent);
926 /* Obtain child reference for new parent. */
Cornelia Huck6eff2082008-12-25 13:39:07 +0100927 if (!get_device(&sch->dev))
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100928 return -ENODEV;
Sebastian Ott0c609fc2009-12-07 12:51:37 +0100929
930 if (!sch_is_pseudo_sch(old_sch)) {
931 spin_lock_irq(old_sch->lock);
932 old_enabled = old_sch->schib.pmcw.ena;
933 rc = 0;
934 if (old_enabled)
935 rc = cio_disable_subchannel(old_sch);
936 spin_unlock_irq(old_sch->lock);
937 if (rc == -EBUSY) {
938 /* Release child reference for new parent. */
939 put_device(&sch->dev);
940 return rc;
941 }
942 }
943
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100944 mutex_lock(&sch->reg_mutex);
Cornelia Huckffa6a702009-03-04 12:44:00 +0100945 rc = device_move(&cdev->dev, &sch->dev, DPM_ORDER_PARENT_BEFORE_DEV);
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100946 mutex_unlock(&sch->reg_mutex);
947 if (rc) {
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100948 CIO_MSG_EVENT(0, "device_move(0.%x.%04x,0.%x.%04x)=%d\n",
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100949 cdev->private->dev_id.ssid,
950 cdev->private->dev_id.devno, sch->schid.ssid,
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100951 sch->schib.pmcw.dev, rc);
Sebastian Ott0c609fc2009-12-07 12:51:37 +0100952 if (old_enabled) {
953 /* Try to reenable the old subchannel. */
954 spin_lock_irq(old_sch->lock);
955 cio_enable_subchannel(old_sch, (u32)(addr_t)old_sch);
956 spin_unlock_irq(old_sch->lock);
957 }
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100958 /* Release child reference for new parent. */
Cornelia Huck6eff2082008-12-25 13:39:07 +0100959 put_device(&sch->dev);
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100960 return rc;
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100961 }
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100962 /* Clean up old subchannel. */
963 if (!sch_is_pseudo_sch(old_sch)) {
964 spin_lock_irq(old_sch->lock);
965 sch_set_cdev(old_sch, NULL);
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100966 spin_unlock_irq(old_sch->lock);
967 css_schedule_eval(old_sch->schid);
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100968 }
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100969 /* Release child reference for old parent. */
970 put_device(&old_sch->dev);
971 /* Initialize new subchannel. */
972 spin_lock_irq(sch->lock);
973 cdev->private->schid = sch->schid;
974 cdev->ccwlock = sch->lock;
975 if (!sch_is_pseudo_sch(sch))
976 sch_set_cdev(sch, cdev);
977 spin_unlock_irq(sch->lock);
978 if (!sch_is_pseudo_sch(sch))
979 css_update_ssd_info(sch);
980 return 0;
981}
982
983static int ccw_device_move_to_orph(struct ccw_device *cdev)
984{
985 struct subchannel *sch = to_subchannel(cdev->dev.parent);
986 struct channel_subsystem *css = to_css(sch->dev.parent);
987
988 return ccw_device_move_to_sch(cdev, css->pseudo_subchannel);
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100989}
990
Cornelia Huck602b20f2008-01-26 14:10:39 +0100991static void io_subchannel_irq(struct subchannel *sch)
992{
993 struct ccw_device *cdev;
994
Cornelia Huckdb6a6422008-01-26 14:10:46 +0100995 cdev = sch_get_cdev(sch);
Cornelia Huck602b20f2008-01-26 14:10:39 +0100996
Sebastian Ottefd986d2009-09-11 10:28:18 +0200997 CIO_TRACE_EVENT(6, "IRQ");
998 CIO_TRACE_EVENT(6, dev_name(&sch->dev));
Cornelia Huck602b20f2008-01-26 14:10:39 +0100999 if (cdev)
1000 dev_fsm_event(cdev, DEV_EVENT_INTERRUPT);
1001}
1002
Sebastian Ott13952ec2008-12-25 13:39:13 +01001003void io_subchannel_init_config(struct subchannel *sch)
1004{
1005 memset(&sch->config, 0, sizeof(sch->config));
1006 sch->config.csense = 1;
Sebastian Ott13952ec2008-12-25 13:39:13 +01001007}
1008
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001009static void io_subchannel_init_fields(struct subchannel *sch)
1010{
1011 if (cio_is_console(sch->schid))
1012 sch->opm = 0xff;
1013 else
1014 sch->opm = chp_get_sch_opm(sch);
1015 sch->lpm = sch->schib.pmcw.pam & sch->opm;
Cornelia Huck3a3fc292008-07-14 09:58:58 +02001016 sch->isc = cio_is_console(sch->schid) ? CONSOLE_ISC : IO_SCH_ISC;
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001017
1018 CIO_MSG_EVENT(6, "Detected device %04x on subchannel 0.%x.%04X"
1019 " - PIM = %02X, PAM = %02X, POM = %02X\n",
1020 sch->schib.pmcw.dev, sch->schid.ssid,
1021 sch->schid.sch_no, sch->schib.pmcw.pim,
1022 sch->schib.pmcw.pam, sch->schib.pmcw.pom);
Sebastian Ott13952ec2008-12-25 13:39:13 +01001023
1024 io_subchannel_init_config(sch);
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001025}
1026
Cornelia Huck90ed2b62008-12-25 13:39:09 +01001027/*
1028 * Note: We always return 0 so that we bind to the device even on error.
1029 * This is needed so that our remove function is called on unregister.
1030 */
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001031static int io_subchannel_probe(struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032{
Sebastian Ottf92519e2011-03-15 17:08:27 +01001033 struct io_subchannel_private *io_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 struct ccw_device *cdev;
1035 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036
Peter Oberparleiter6d7c5af2009-10-14 12:43:50 +02001037 if (cio_is_console(sch->schid)) {
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001038 rc = sysfs_create_group(&sch->dev.kobj,
1039 &io_subchannel_attr_group);
1040 if (rc)
1041 CIO_MSG_EVENT(0, "Failed to create io subchannel "
1042 "attributes for subchannel "
1043 "0.%x.%04x (rc=%d)\n",
1044 sch->schid.ssid, sch->schid.sch_no, rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 /*
Peter Oberparleiter6d7c5af2009-10-14 12:43:50 +02001046 * The console subchannel already has an associated ccw_device.
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001047 * Throw the delayed uevent for the subchannel, register
Peter Oberparleiter6d7c5af2009-10-14 12:43:50 +02001048 * the ccw_device and exit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 */
Ming Leif67f1292009-03-01 21:10:49 +08001050 dev_set_uevent_suppress(&sch->dev, 0);
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001051 kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
Peter Oberparleiter6d7c5af2009-10-14 12:43:50 +02001052 cdev = sch_get_cdev(sch);
Cornelia Hucke1031782007-10-12 16:11:23 +02001053 cdev->dev.groups = ccwdev_attr_groups;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 device_initialize(&cdev->dev);
Sebastian Ott24a18722009-12-07 12:51:34 +01001055 cdev->private->flags.initialized = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 ccw_device_register(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 /*
1058 * Check if the device is already online. If it is
Cornelia Huck9cd67422008-12-25 13:39:06 +01001059 * the reference count needs to be corrected since we
1060 * didn't obtain a reference in ccw_device_set_online.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 */
1062 if (cdev->private->state != DEV_STATE_NOT_OPER &&
1063 cdev->private->state != DEV_STATE_OFFLINE &&
1064 cdev->private->state != DEV_STATE_BOXED)
1065 get_device(&cdev->dev);
1066 return 0;
1067 }
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001068 io_subchannel_init_fields(sch);
Sebastian Ottf444cc02008-12-25 13:39:14 +01001069 rc = cio_commit_config(sch);
1070 if (rc)
1071 goto out_schedule;
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001072 rc = sysfs_create_group(&sch->dev.kobj,
1073 &io_subchannel_attr_group);
1074 if (rc)
Cornelia Huck90ed2b62008-12-25 13:39:09 +01001075 goto out_schedule;
Cornelia Huckcd6b4f22008-01-26 14:10:43 +01001076 /* Allocate I/O subchannel private data. */
Sebastian Ottf92519e2011-03-15 17:08:27 +01001077 io_priv = kzalloc(sizeof(*io_priv), GFP_KERNEL | GFP_DMA);
1078 if (!io_priv)
Peter Oberparleiter48e4c382009-12-07 12:51:15 +01001079 goto out_schedule;
Sebastian Ottf92519e2011-03-15 17:08:27 +01001080
1081 set_io_private(sch, io_priv);
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001082 css_schedule_eval(sch->schid);
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001083 return 0;
Peter Oberparleiter48e4c382009-12-07 12:51:15 +01001084
Cornelia Huck90ed2b62008-12-25 13:39:09 +01001085out_schedule:
Peter Oberparleiter390935a2009-12-07 12:51:18 +01001086 spin_lock_irq(sch->lock);
1087 css_sched_sch_todo(sch, SCH_TODO_UNREG);
1088 spin_unlock_irq(sch->lock);
Cornelia Huck90ed2b62008-12-25 13:39:09 +01001089 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090}
1091
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092static int
Cornelia Huck8bbace72006-01-11 10:56:22 +01001093io_subchannel_remove (struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094{
Sebastian Ottf92519e2011-03-15 17:08:27 +01001095 struct io_subchannel_private *io_priv = to_io_private(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 struct ccw_device *cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001098 cdev = sch_get_cdev(sch);
1099 if (!cdev)
Peter Oberparleiter48e4c382009-12-07 12:51:15 +01001100 goto out_free;
Sebastian Ott6e9a0f62009-12-07 12:51:38 +01001101 io_subchannel_quiesce(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 /* Set ccw device to not operational and drop reference. */
Sebastian Ott7a8ad102009-12-07 12:51:39 +01001103 spin_lock_irq(cdev->ccwlock);
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001104 sch_set_cdev(sch, NULL);
Sebastian Ottf92519e2011-03-15 17:08:27 +01001105 set_io_private(sch, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 cdev->private->state = DEV_STATE_NOT_OPER;
Sebastian Ott7a8ad102009-12-07 12:51:39 +01001107 spin_unlock_irq(cdev->ccwlock);
Cornelia Huckef995162007-04-27 16:01:39 +02001108 ccw_device_unregister(cdev);
Peter Oberparleiter48e4c382009-12-07 12:51:15 +01001109out_free:
Sebastian Ottf92519e2011-03-15 17:08:27 +01001110 kfree(io_priv);
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001111 sysfs_remove_group(&sch->dev.kobj, &io_subchannel_attr_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 return 0;
1113}
1114
Cornelia Huck602b20f2008-01-26 14:10:39 +01001115static void io_subchannel_verify(struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116{
1117 struct ccw_device *cdev;
1118
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001119 cdev = sch_get_cdev(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 if (cdev)
1121 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1122}
1123
Cornelia Huckc820de32008-07-14 09:58:45 +02001124static void io_subchannel_terminate_path(struct subchannel *sch, u8 mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125{
1126 struct ccw_device *cdev;
1127
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001128 cdev = sch_get_cdev(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 if (!cdev)
1130 return;
Peter Oberparleiter4257aae2009-12-07 12:51:29 +01001131 if (cio_update_schib(sch))
1132 goto err;
1133 /* Check for I/O on path. */
1134 if (scsw_actl(&sch->schib.scsw) == 0 || sch->schib.pmcw.lpum != mask)
1135 goto out;
1136 if (cdev->private->state == DEV_STATE_ONLINE) {
1137 ccw_device_kill_io(cdev);
1138 goto out;
1139 }
1140 if (cio_clear(sch))
1141 goto err;
1142out:
1143 /* Trigger path verification. */
1144 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1145 return;
Cornelia Huckc820de32008-07-14 09:58:45 +02001146
Peter Oberparleiter4257aae2009-12-07 12:51:29 +01001147err:
1148 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
Cornelia Huckc820de32008-07-14 09:58:45 +02001149}
1150
Cornelia Huck99611f82008-07-14 09:59:02 +02001151static int io_subchannel_chp_event(struct subchannel *sch,
1152 struct chp_link *link, int event)
Cornelia Huckc820de32008-07-14 09:58:45 +02001153{
Sebastian Ott585b9542010-10-25 16:10:34 +02001154 struct ccw_device *cdev = sch_get_cdev(sch);
Cornelia Huckc820de32008-07-14 09:58:45 +02001155 int mask;
Cornelia Huckc820de32008-07-14 09:58:45 +02001156
Cornelia Huck99611f82008-07-14 09:59:02 +02001157 mask = chp_ssd_get_mask(&sch->ssd_info, link);
Cornelia Huckc820de32008-07-14 09:58:45 +02001158 if (!mask)
1159 return 0;
1160 switch (event) {
1161 case CHP_VARY_OFF:
1162 sch->opm &= ~mask;
1163 sch->lpm &= ~mask;
Sebastian Ott585b9542010-10-25 16:10:34 +02001164 if (cdev)
1165 cdev->private->path_gone_mask |= mask;
Cornelia Huckc820de32008-07-14 09:58:45 +02001166 io_subchannel_terminate_path(sch, mask);
1167 break;
1168 case CHP_VARY_ON:
1169 sch->opm |= mask;
1170 sch->lpm |= mask;
Sebastian Ott585b9542010-10-25 16:10:34 +02001171 if (cdev)
1172 cdev->private->path_new_mask |= mask;
Cornelia Huckc820de32008-07-14 09:58:45 +02001173 io_subchannel_verify(sch);
1174 break;
1175 case CHP_OFFLINE:
Sebastian Ottcdb912a2008-12-25 13:39:12 +01001176 if (cio_update_schib(sch))
Cornelia Huckc820de32008-07-14 09:58:45 +02001177 return -ENODEV;
Sebastian Ott585b9542010-10-25 16:10:34 +02001178 if (cdev)
1179 cdev->private->path_gone_mask |= mask;
Cornelia Huckc820de32008-07-14 09:58:45 +02001180 io_subchannel_terminate_path(sch, mask);
1181 break;
1182 case CHP_ONLINE:
Sebastian Ottcdb912a2008-12-25 13:39:12 +01001183 if (cio_update_schib(sch))
1184 return -ENODEV;
Cornelia Huckc820de32008-07-14 09:58:45 +02001185 sch->lpm |= mask & sch->opm;
Sebastian Ott585b9542010-10-25 16:10:34 +02001186 if (cdev)
1187 cdev->private->path_new_mask |= mask;
Cornelia Huckc820de32008-07-14 09:58:45 +02001188 io_subchannel_verify(sch);
1189 break;
1190 }
1191 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192}
1193
Sebastian Ott6e9a0f62009-12-07 12:51:38 +01001194static void io_subchannel_quiesce(struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 struct ccw_device *cdev;
1197 int ret;
1198
Sebastian Ott56e6b792009-12-07 12:51:35 +01001199 spin_lock_irq(sch->lock);
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001200 cdev = sch_get_cdev(sch);
Cornelia Hucka8237fc2006-01-06 00:19:21 -08001201 if (cio_is_console(sch->schid))
Sebastian Ott56e6b792009-12-07 12:51:35 +01001202 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 if (!sch->schib.pmcw.ena)
Sebastian Ott56e6b792009-12-07 12:51:35 +01001204 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 ret = cio_disable_subchannel(sch);
1206 if (ret != -EBUSY)
Sebastian Ott56e6b792009-12-07 12:51:35 +01001207 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 if (cdev->handler)
Sebastian Ott56e6b792009-12-07 12:51:35 +01001209 cdev->handler(cdev, cdev->private->intparm, ERR_PTR(-EIO));
1210 while (ret == -EBUSY) {
1211 cdev->private->state = DEV_STATE_QUIESCE;
Peter Oberparleiter376ae472010-10-25 16:10:44 +02001212 cdev->private->iretry = 255;
Sebastian Ott56e6b792009-12-07 12:51:35 +01001213 ret = ccw_device_cancel_halt_clear(cdev);
1214 if (ret == -EBUSY) {
1215 ccw_device_set_timeout(cdev, HZ/10);
1216 spin_unlock_irq(sch->lock);
1217 wait_event(cdev->private->wait_q,
1218 cdev->private->state != DEV_STATE_QUIESCE);
1219 spin_lock_irq(sch->lock);
1220 }
1221 ret = cio_disable_subchannel(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 }
Sebastian Ott56e6b792009-12-07 12:51:35 +01001223out_unlock:
1224 spin_unlock_irq(sch->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225}
1226
Sebastian Ott6e9a0f62009-12-07 12:51:38 +01001227static void io_subchannel_shutdown(struct subchannel *sch)
1228{
1229 io_subchannel_quiesce(sch);
1230}
1231
Cornelia Huckc820de32008-07-14 09:58:45 +02001232static int device_is_disconnected(struct ccw_device *cdev)
1233{
1234 if (!cdev)
1235 return 0;
1236 return (cdev->private->state == DEV_STATE_DISCONNECTED ||
1237 cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID);
1238}
1239
1240static int recovery_check(struct device *dev, void *data)
1241{
1242 struct ccw_device *cdev = to_ccwdev(dev);
1243 int *redo = data;
1244
1245 spin_lock_irq(cdev->ccwlock);
1246 switch (cdev->private->state) {
1247 case DEV_STATE_DISCONNECTED:
1248 CIO_MSG_EVENT(3, "recovery: trigger 0.%x.%04x\n",
1249 cdev->private->dev_id.ssid,
1250 cdev->private->dev_id.devno);
1251 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1252 *redo = 1;
1253 break;
1254 case DEV_STATE_DISCONNECTED_SENSE_ID:
1255 *redo = 1;
1256 break;
1257 }
1258 spin_unlock_irq(cdev->ccwlock);
1259
1260 return 0;
1261}
1262
1263static void recovery_work_func(struct work_struct *unused)
1264{
1265 int redo = 0;
1266
1267 bus_for_each_dev(&ccw_bus_type, NULL, &redo, recovery_check);
1268 if (redo) {
1269 spin_lock_irq(&recovery_lock);
1270 if (!timer_pending(&recovery_timer)) {
1271 if (recovery_phase < ARRAY_SIZE(recovery_delay) - 1)
1272 recovery_phase++;
1273 mod_timer(&recovery_timer, jiffies +
1274 recovery_delay[recovery_phase] * HZ);
1275 }
1276 spin_unlock_irq(&recovery_lock);
1277 } else
1278 CIO_MSG_EVENT(4, "recovery: end\n");
1279}
1280
1281static DECLARE_WORK(recovery_work, recovery_work_func);
1282
1283static void recovery_func(unsigned long data)
1284{
1285 /*
1286 * We can't do our recovery in softirq context and it's not
1287 * performance critical, so we schedule it.
1288 */
1289 schedule_work(&recovery_work);
1290}
1291
1292static void ccw_device_schedule_recovery(void)
1293{
1294 unsigned long flags;
1295
1296 CIO_MSG_EVENT(4, "recovery: schedule\n");
1297 spin_lock_irqsave(&recovery_lock, flags);
1298 if (!timer_pending(&recovery_timer) || (recovery_phase != 0)) {
1299 recovery_phase = 0;
1300 mod_timer(&recovery_timer, jiffies + recovery_delay[0] * HZ);
1301 }
1302 spin_unlock_irqrestore(&recovery_lock, flags);
1303}
1304
Peter Oberparleiterecf5d9e2008-10-10 21:33:06 +02001305static int purge_fn(struct device *dev, void *data)
1306{
1307 struct ccw_device *cdev = to_ccwdev(dev);
Peter Oberparleiter37de53b2009-12-07 12:51:19 +01001308 struct ccw_dev_id *id = &cdev->private->dev_id;
Peter Oberparleiterecf5d9e2008-10-10 21:33:06 +02001309
1310 spin_lock_irq(cdev->ccwlock);
Peter Oberparleiter37de53b2009-12-07 12:51:19 +01001311 if (is_blacklisted(id->ssid, id->devno) &&
1312 (cdev->private->state == DEV_STATE_OFFLINE)) {
1313 CIO_MSG_EVENT(3, "ccw: purging 0.%x.%04x\n", id->ssid,
1314 id->devno);
1315 ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
1316 }
Peter Oberparleiterecf5d9e2008-10-10 21:33:06 +02001317 spin_unlock_irq(cdev->ccwlock);
Peter Oberparleiterecf5d9e2008-10-10 21:33:06 +02001318 /* Abort loop in case of pending signal. */
1319 if (signal_pending(current))
1320 return -EINTR;
1321
1322 return 0;
1323}
1324
1325/**
1326 * ccw_purge_blacklisted - purge unused, blacklisted devices
1327 *
1328 * Unregister all ccw devices that are offline and on the blacklist.
1329 */
1330int ccw_purge_blacklisted(void)
1331{
1332 CIO_MSG_EVENT(2, "ccw: purging blacklisted devices\n");
1333 bus_for_each_dev(&ccw_bus_type, NULL, NULL, purge_fn);
1334 return 0;
1335}
1336
Peter Oberparleiter6afcc772009-10-06 10:34:02 +02001337void ccw_device_set_disconnected(struct ccw_device *cdev)
Cornelia Huckc820de32008-07-14 09:58:45 +02001338{
1339 if (!cdev)
1340 return;
1341 ccw_device_set_timeout(cdev, 0);
1342 cdev->private->flags.fake_irb = 0;
1343 cdev->private->state = DEV_STATE_DISCONNECTED;
1344 if (cdev->online)
1345 ccw_device_schedule_recovery();
1346}
1347
Peter Oberparleiter91c36912008-08-21 19:46:39 +02001348void ccw_device_set_notoper(struct ccw_device *cdev)
1349{
1350 struct subchannel *sch = to_subchannel(cdev->dev.parent);
1351
1352 CIO_TRACE_EVENT(2, "notoper");
Cornelia Huckb9d3aed2008-10-10 21:33:11 +02001353 CIO_TRACE_EVENT(2, dev_name(&sch->dev));
Peter Oberparleiter91c36912008-08-21 19:46:39 +02001354 ccw_device_set_timeout(cdev, 0);
1355 cio_disable_subchannel(sch);
1356 cdev->private->state = DEV_STATE_NOT_OPER;
1357}
1358
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001359enum io_sch_action {
1360 IO_SCH_UNREG,
1361 IO_SCH_ORPH_UNREG,
1362 IO_SCH_ATTACH,
1363 IO_SCH_UNREG_ATTACH,
1364 IO_SCH_ORPH_ATTACH,
1365 IO_SCH_REPROBE,
1366 IO_SCH_VERIFY,
1367 IO_SCH_DISC,
1368 IO_SCH_NOP,
1369};
1370
1371static enum io_sch_action sch_get_action(struct subchannel *sch)
Cornelia Huckc820de32008-07-14 09:58:45 +02001372{
Cornelia Huckc820de32008-07-14 09:58:45 +02001373 struct ccw_device *cdev;
1374
Cornelia Huckc820de32008-07-14 09:58:45 +02001375 cdev = sch_get_cdev(sch);
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001376 if (cio_update_schib(sch)) {
1377 /* Not operational. */
1378 if (!cdev)
1379 return IO_SCH_UNREG;
Sebastian Ott76e6fb42010-02-26 22:37:28 +01001380 if (ccw_device_notify(cdev, CIO_GONE) != NOTIFY_OK)
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001381 return IO_SCH_UNREG;
1382 return IO_SCH_ORPH_UNREG;
Cornelia Huckc820de32008-07-14 09:58:45 +02001383 }
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001384 /* Operational. */
1385 if (!cdev)
1386 return IO_SCH_ATTACH;
1387 if (sch->schib.pmcw.dev != cdev->private->dev_id.devno) {
Sebastian Ott76e6fb42010-02-26 22:37:28 +01001388 if (ccw_device_notify(cdev, CIO_GONE) != NOTIFY_OK)
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001389 return IO_SCH_UNREG_ATTACH;
1390 return IO_SCH_ORPH_ATTACH;
Cornelia Huckc820de32008-07-14 09:58:45 +02001391 }
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001392 if ((sch->schib.pmcw.pam & sch->opm) == 0) {
Sebastian Ott76e6fb42010-02-26 22:37:28 +01001393 if (ccw_device_notify(cdev, CIO_NO_PATH) != NOTIFY_OK)
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001394 return IO_SCH_UNREG;
1395 return IO_SCH_DISC;
Cornelia Huckc820de32008-07-14 09:58:45 +02001396 }
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001397 if (device_is_disconnected(cdev))
1398 return IO_SCH_REPROBE;
1399 if (cdev->online)
1400 return IO_SCH_VERIFY;
1401 return IO_SCH_NOP;
1402}
1403
1404/**
1405 * io_subchannel_sch_event - process subchannel event
1406 * @sch: subchannel
1407 * @process: non-zero if function is called in process context
1408 *
1409 * An unspecified event occurred for this subchannel. Adjust data according
1410 * to the current operational state of the subchannel and device. Return
1411 * zero when the event has been handled sufficiently or -EAGAIN when this
1412 * function should be called again in process context.
1413 */
1414static int io_subchannel_sch_event(struct subchannel *sch, int process)
1415{
1416 unsigned long flags;
1417 struct ccw_device *cdev;
1418 struct ccw_dev_id dev_id;
1419 enum io_sch_action action;
1420 int rc = -EAGAIN;
1421
1422 spin_lock_irqsave(sch->lock, flags);
1423 if (!device_is_registered(&sch->dev))
1424 goto out_unlock;
Peter Oberparleiter390935a2009-12-07 12:51:18 +01001425 if (work_pending(&sch->todo_work))
1426 goto out_unlock;
Peter Oberparleiter37de53b2009-12-07 12:51:19 +01001427 cdev = sch_get_cdev(sch);
1428 if (cdev && work_pending(&cdev->private->todo_work))
1429 goto out_unlock;
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001430 action = sch_get_action(sch);
1431 CIO_MSG_EVENT(2, "event: sch 0.%x.%04x, process=%d, action=%d\n",
1432 sch->schid.ssid, sch->schid.sch_no, process,
1433 action);
1434 /* Perform immediate actions while holding the lock. */
Cornelia Huckc820de32008-07-14 09:58:45 +02001435 switch (action) {
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001436 case IO_SCH_REPROBE:
1437 /* Trigger device recognition. */
Cornelia Huckc820de32008-07-14 09:58:45 +02001438 ccw_device_trigger_reprobe(cdev);
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001439 rc = 0;
1440 goto out_unlock;
1441 case IO_SCH_VERIFY:
Sebastian Ott0d01bb82010-02-26 22:37:29 +01001442 if (cdev->private->flags.resuming == 1) {
1443 if (cio_enable_subchannel(sch, (u32)(addr_t)sch)) {
1444 ccw_device_set_notoper(cdev);
1445 break;
1446 }
1447 }
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001448 /* Trigger path verification. */
1449 io_subchannel_verify(sch);
1450 rc = 0;
1451 goto out_unlock;
1452 case IO_SCH_DISC:
1453 ccw_device_set_disconnected(cdev);
1454 rc = 0;
1455 goto out_unlock;
1456 case IO_SCH_ORPH_UNREG:
1457 case IO_SCH_ORPH_ATTACH:
Peter Oberparleiter6afcc772009-10-06 10:34:02 +02001458 ccw_device_set_disconnected(cdev);
Peter Oberparleiter91c36912008-08-21 19:46:39 +02001459 break;
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001460 case IO_SCH_UNREG_ATTACH:
1461 case IO_SCH_UNREG:
Sebastian Ott16d2ce22010-11-10 10:05:53 +01001462 if (!cdev)
1463 break;
1464 if (cdev->private->state == DEV_STATE_SENSE_ID) {
1465 /*
1466 * Note: delayed work triggered by this event
1467 * and repeated calls to sch_event are synchronized
1468 * by the above check for work_pending(cdev).
1469 */
1470 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
1471 } else
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001472 ccw_device_set_notoper(cdev);
1473 break;
1474 case IO_SCH_NOP:
1475 rc = 0;
1476 goto out_unlock;
Cornelia Huckc820de32008-07-14 09:58:45 +02001477 default:
1478 break;
1479 }
1480 spin_unlock_irqrestore(sch->lock, flags);
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001481 /* All other actions require process context. */
1482 if (!process)
1483 goto out;
1484 /* Handle attached ccw device. */
1485 switch (action) {
1486 case IO_SCH_ORPH_UNREG:
1487 case IO_SCH_ORPH_ATTACH:
1488 /* Move ccw device to orphanage. */
1489 rc = ccw_device_move_to_orph(cdev);
1490 if (rc)
1491 goto out;
1492 break;
1493 case IO_SCH_UNREG_ATTACH:
Sebastian Ott74b61272010-10-25 16:10:26 +02001494 if (cdev->private->flags.resuming) {
1495 /* Device will be handled later. */
1496 rc = 0;
1497 goto out;
1498 }
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001499 /* Unregister ccw device. */
Sebastian Ott74b61272010-10-25 16:10:26 +02001500 ccw_device_unregister(cdev);
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001501 break;
1502 default:
1503 break;
1504 }
1505 /* Handle subchannel. */
1506 switch (action) {
1507 case IO_SCH_ORPH_UNREG:
1508 case IO_SCH_UNREG:
Sebastian Ott0d01bb82010-02-26 22:37:29 +01001509 if (!cdev || !cdev->private->flags.resuming)
1510 css_sch_device_unregister(sch);
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001511 break;
1512 case IO_SCH_ORPH_ATTACH:
1513 case IO_SCH_UNREG_ATTACH:
1514 case IO_SCH_ATTACH:
1515 dev_id.ssid = sch->schid.ssid;
1516 dev_id.devno = sch->schib.pmcw.dev;
1517 cdev = get_ccwdev_by_dev_id(&dev_id);
1518 if (!cdev) {
1519 sch_create_and_recog_new_device(sch);
1520 break;
1521 }
1522 rc = ccw_device_move_to_sch(cdev, sch);
1523 if (rc) {
1524 /* Release reference from get_ccwdev_by_dev_id() */
1525 put_device(&cdev->dev);
1526 goto out;
1527 }
1528 spin_lock_irqsave(sch->lock, flags);
1529 ccw_device_trigger_reprobe(cdev);
1530 spin_unlock_irqrestore(sch->lock, flags);
1531 /* Release reference from get_ccwdev_by_dev_id() */
1532 put_device(&cdev->dev);
1533 break;
1534 default:
1535 break;
1536 }
1537 return 0;
Cornelia Huckc820de32008-07-14 09:58:45 +02001538
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001539out_unlock:
1540 spin_unlock_irqrestore(sch->lock, flags);
1541out:
1542 return rc;
Cornelia Huckc820de32008-07-14 09:58:45 +02001543}
1544
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545#ifdef CONFIG_CCW_CONSOLE
1546static struct ccw_device console_cdev;
1547static struct ccw_device_private console_private;
1548static int console_cdev_in_use;
1549
Cornelia Huck2ec22982006-12-08 15:54:26 +01001550static DEFINE_SPINLOCK(ccw_console_lock);
1551
1552spinlock_t * cio_get_console_lock(void)
1553{
1554 return &ccw_console_lock;
1555}
1556
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001557static int ccw_device_console_enable(struct ccw_device *cdev,
1558 struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559{
Sebastian Ottf92519e2011-03-15 17:08:27 +01001560 struct io_subchannel_private *io_priv = cio_get_console_priv();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 int rc;
1562
Cornelia Huckcd6b4f22008-01-26 14:10:43 +01001563 /* Attach subchannel private data. */
Sebastian Ottf92519e2011-03-15 17:08:27 +01001564 memset(io_priv, 0, sizeof(*io_priv));
1565 set_io_private(sch, io_priv);
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001566 io_subchannel_init_fields(sch);
Sebastian Ottf444cc02008-12-25 13:39:14 +01001567 rc = cio_commit_config(sch);
1568 if (rc)
1569 return rc;
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001570 sch->driver = &io_subchannel_driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 /* Initialize the ccw_device structure. */
Heiko Carstens292888c2006-08-30 14:33:35 +02001572 cdev->dev.parent= &sch->dev;
Sebastian Ottffa8d2a2009-12-18 17:43:15 +01001573 sch_set_cdev(sch, cdev);
Peter Oberparleiter736b5db2009-12-07 12:51:21 +01001574 io_subchannel_recog(cdev, sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 /* Now wait for the async. recognition to come to an end. */
1576 spin_lock_irq(cdev->ccwlock);
1577 while (!dev_fsm_final_state(cdev))
1578 wait_cons_dev();
1579 rc = -EIO;
1580 if (cdev->private->state != DEV_STATE_OFFLINE)
1581 goto out_unlock;
1582 ccw_device_online(cdev);
1583 while (!dev_fsm_final_state(cdev))
1584 wait_cons_dev();
1585 if (cdev->private->state != DEV_STATE_ONLINE)
1586 goto out_unlock;
1587 rc = 0;
1588out_unlock:
1589 spin_unlock_irq(cdev->ccwlock);
Peter Oberparleiter736b5db2009-12-07 12:51:21 +01001590 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591}
1592
1593struct ccw_device *
1594ccw_device_probe_console(void)
1595{
1596 struct subchannel *sch;
1597 int ret;
1598
1599 if (xchg(&console_cdev_in_use, 1) != 0)
Peter Oberparleiter600b5d12006-02-01 03:06:35 -08001600 return ERR_PTR(-EBUSY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 sch = cio_probe_console();
1602 if (IS_ERR(sch)) {
1603 console_cdev_in_use = 0;
1604 return (void *) sch;
1605 }
1606 memset(&console_cdev, 0, sizeof(struct ccw_device));
1607 memset(&console_private, 0, sizeof(struct ccw_device_private));
1608 console_cdev.private = &console_private;
Martin Schwidefskyc1637532006-12-08 15:53:57 +01001609 console_private.cdev = &console_cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 ret = ccw_device_console_enable(&console_cdev, sch);
1611 if (ret) {
1612 cio_release_console();
1613 console_cdev_in_use = 0;
1614 return ERR_PTR(ret);
1615 }
1616 console_cdev.online = 1;
1617 return &console_cdev;
1618}
Cornelia Huck1f4e7ed2008-10-10 21:33:14 +02001619
Martin Schwidefsky66648452009-06-16 10:30:28 +02001620static int ccw_device_pm_restore(struct device *dev);
1621
1622int ccw_device_force_console(void)
1623{
1624 if (!console_cdev_in_use)
1625 return -ENODEV;
1626 return ccw_device_pm_restore(&console_cdev.dev);
1627}
1628EXPORT_SYMBOL_GPL(ccw_device_force_console);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629#endif
1630
1631/*
1632 * get ccw_device matching the busid, but only if owned by cdrv
1633 */
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001634static int
1635__ccwdev_check_busid(struct device *dev, void *id)
1636{
1637 char *bus_id;
1638
Cornelia Huck12975ae2006-10-11 15:31:47 +02001639 bus_id = id;
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001640
Kay Sievers98df67b2008-12-25 13:38:55 +01001641 return (strcmp(bus_id, dev_name(dev)) == 0);
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001642}
1643
1644
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +02001645/**
1646 * get_ccwdev_by_busid() - obtain device from a bus id
1647 * @cdrv: driver the device is owned by
1648 * @bus_id: bus id of the device to be searched
1649 *
1650 * This function searches all devices owned by @cdrv for a device with a bus
1651 * id matching @bus_id.
1652 * Returns:
1653 * If a match is found, its reference count of the found device is increased
1654 * and it is returned; else %NULL is returned.
1655 */
1656struct ccw_device *get_ccwdev_by_busid(struct ccw_driver *cdrv,
1657 const char *bus_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658{
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001659 struct device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 struct device_driver *drv;
1661
1662 drv = get_driver(&cdrv->driver);
1663 if (!drv)
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001664 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001666 dev = driver_find_device(drv, NULL, (void *)bus_id,
1667 __ccwdev_check_busid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 put_driver(drv);
1669
Heiko Carstensd2c993d2006-07-12 16:41:55 +02001670 return dev ? to_ccwdev(dev) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671}
1672
1673/************************** device driver handling ************************/
1674
1675/* This is the implementation of the ccw_driver class. The probe, remove
1676 * and release methods are initially very similar to the device_driver
1677 * implementations, with the difference that they have ccw_device
1678 * arguments.
1679 *
1680 * A ccw driver also contains the information that is needed for
1681 * device matching.
1682 */
1683static int
1684ccw_device_probe (struct device *dev)
1685{
1686 struct ccw_device *cdev = to_ccwdev(dev);
1687 struct ccw_driver *cdrv = to_ccwdrv(dev->driver);
1688 int ret;
1689
1690 cdev->drv = cdrv; /* to let the driver call _set_online */
1691
1692 ret = cdrv->probe ? cdrv->probe(cdev) : -ENODEV;
1693
1694 if (ret) {
Heiko Carstensd2c993d2006-07-12 16:41:55 +02001695 cdev->drv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 return ret;
1697 }
1698
1699 return 0;
1700}
1701
1702static int
1703ccw_device_remove (struct device *dev)
1704{
1705 struct ccw_device *cdev = to_ccwdev(dev);
1706 struct ccw_driver *cdrv = cdev->drv;
1707 int ret;
1708
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 if (cdrv->remove)
1710 cdrv->remove(cdev);
1711 if (cdev->online) {
1712 cdev->online = 0;
1713 spin_lock_irq(cdev->ccwlock);
1714 ret = ccw_device_offline(cdev);
1715 spin_unlock_irq(cdev->ccwlock);
1716 if (ret == 0)
1717 wait_event(cdev->private->wait_q,
1718 dev_fsm_final_state(cdev));
1719 else
Michael Ernst139b83d2008-05-07 09:22:54 +02001720 CIO_MSG_EVENT(0, "ccw_device_offline returned %d, "
Cornelia Hucke556bbb2007-07-27 12:29:19 +02001721 "device 0.%x.%04x\n",
1722 ret, cdev->private->dev_id.ssid,
1723 cdev->private->dev_id.devno);
Cornelia Huck9cd67422008-12-25 13:39:06 +01001724 /* Give up reference obtained in ccw_device_set_online(). */
1725 put_device(&cdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 }
1727 ccw_device_set_timeout(cdev, 0);
Heiko Carstensd2c993d2006-07-12 16:41:55 +02001728 cdev->drv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 return 0;
1730}
1731
Cornelia Huck958974f2007-10-12 16:11:21 +02001732static void ccw_device_shutdown(struct device *dev)
1733{
1734 struct ccw_device *cdev;
1735
1736 cdev = to_ccwdev(dev);
1737 if (cdev->drv && cdev->drv->shutdown)
1738 cdev->drv->shutdown(cdev);
Cornelia Huck1842f2b2007-10-12 16:11:22 +02001739 disable_cmf(cdev);
Cornelia Huck958974f2007-10-12 16:11:21 +02001740}
1741
Sebastian Ott823d4942009-06-16 10:30:20 +02001742static int ccw_device_pm_prepare(struct device *dev)
1743{
1744 struct ccw_device *cdev = to_ccwdev(dev);
1745
Peter Oberparleiter37de53b2009-12-07 12:51:19 +01001746 if (work_pending(&cdev->private->todo_work))
Sebastian Ott823d4942009-06-16 10:30:20 +02001747 return -EAGAIN;
1748 /* Fail while device is being set online/offline. */
1749 if (atomic_read(&cdev->private->onoff))
1750 return -EAGAIN;
1751
1752 if (cdev->online && cdev->drv && cdev->drv->prepare)
1753 return cdev->drv->prepare(cdev);
1754
1755 return 0;
1756}
1757
1758static void ccw_device_pm_complete(struct device *dev)
1759{
1760 struct ccw_device *cdev = to_ccwdev(dev);
1761
1762 if (cdev->online && cdev->drv && cdev->drv->complete)
1763 cdev->drv->complete(cdev);
1764}
1765
1766static int ccw_device_pm_freeze(struct device *dev)
1767{
1768 struct ccw_device *cdev = to_ccwdev(dev);
1769 struct subchannel *sch = to_subchannel(cdev->dev.parent);
1770 int ret, cm_enabled;
1771
1772 /* Fail suspend while device is in transistional state. */
1773 if (!dev_fsm_final_state(cdev))
1774 return -EAGAIN;
1775 if (!cdev->online)
1776 return 0;
1777 if (cdev->drv && cdev->drv->freeze) {
1778 ret = cdev->drv->freeze(cdev);
1779 if (ret)
1780 return ret;
1781 }
1782
1783 spin_lock_irq(sch->lock);
1784 cm_enabled = cdev->private->cmb != NULL;
1785 spin_unlock_irq(sch->lock);
1786 if (cm_enabled) {
1787 /* Don't have the css write on memory. */
1788 ret = ccw_set_cmf(cdev, 0);
1789 if (ret)
1790 return ret;
1791 }
1792 /* From here on, disallow device driver I/O. */
1793 spin_lock_irq(sch->lock);
1794 ret = cio_disable_subchannel(sch);
1795 spin_unlock_irq(sch->lock);
1796
1797 return ret;
1798}
1799
1800static int ccw_device_pm_thaw(struct device *dev)
1801{
1802 struct ccw_device *cdev = to_ccwdev(dev);
1803 struct subchannel *sch = to_subchannel(cdev->dev.parent);
1804 int ret, cm_enabled;
1805
1806 if (!cdev->online)
1807 return 0;
1808
1809 spin_lock_irq(sch->lock);
1810 /* Allow device driver I/O again. */
1811 ret = cio_enable_subchannel(sch, (u32)(addr_t)sch);
1812 cm_enabled = cdev->private->cmb != NULL;
1813 spin_unlock_irq(sch->lock);
1814 if (ret)
1815 return ret;
1816
1817 if (cm_enabled) {
1818 ret = ccw_set_cmf(cdev, 1);
1819 if (ret)
1820 return ret;
1821 }
1822
1823 if (cdev->drv && cdev->drv->thaw)
1824 ret = cdev->drv->thaw(cdev);
1825
1826 return ret;
1827}
1828
1829static void __ccw_device_pm_restore(struct ccw_device *cdev)
1830{
1831 struct subchannel *sch = to_subchannel(cdev->dev.parent);
Sebastian Ott823d4942009-06-16 10:30:20 +02001832
Sebastian Ott0d01bb82010-02-26 22:37:29 +01001833 spin_lock_irq(sch->lock);
1834 if (cio_is_console(sch->schid)) {
1835 cio_enable_subchannel(sch, (u32)(addr_t)sch);
1836 goto out_unlock;
1837 }
Sebastian Ott823d4942009-06-16 10:30:20 +02001838 /*
1839 * While we were sleeping, devices may have gone or become
1840 * available again. Kick re-detection.
1841 */
Sebastian Ott823d4942009-06-16 10:30:20 +02001842 cdev->private->flags.resuming = 1;
Sebastian Ottb17295e2011-01-12 09:55:10 +01001843 cdev->private->path_new_mask = LPM_ANYPATH;
Sebastian Ott0d01bb82010-02-26 22:37:29 +01001844 css_schedule_eval(sch->schid);
1845 spin_unlock_irq(sch->lock);
1846 css_complete_work();
1847
1848 /* cdev may have been moved to a different subchannel. */
1849 sch = to_subchannel(cdev->dev.parent);
1850 spin_lock_irq(sch->lock);
1851 if (cdev->private->state != DEV_STATE_ONLINE &&
1852 cdev->private->state != DEV_STATE_OFFLINE)
1853 goto out_unlock;
1854
Peter Oberparleiter736b5db2009-12-07 12:51:21 +01001855 ccw_device_recognition(cdev);
Sebastian Ott823d4942009-06-16 10:30:20 +02001856 spin_unlock_irq(sch->lock);
Sebastian Ott823d4942009-06-16 10:30:20 +02001857 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev) ||
1858 cdev->private->state == DEV_STATE_DISCONNECTED);
Sebastian Ott0d01bb82010-02-26 22:37:29 +01001859 spin_lock_irq(sch->lock);
1860
1861out_unlock:
Sebastian Ott823d4942009-06-16 10:30:20 +02001862 cdev->private->flags.resuming = 0;
Sebastian Ott0d01bb82010-02-26 22:37:29 +01001863 spin_unlock_irq(sch->lock);
Sebastian Ott823d4942009-06-16 10:30:20 +02001864}
1865
1866static int resume_handle_boxed(struct ccw_device *cdev)
1867{
1868 cdev->private->state = DEV_STATE_BOXED;
Sebastian Ott76e6fb42010-02-26 22:37:28 +01001869 if (ccw_device_notify(cdev, CIO_BOXED) == NOTIFY_OK)
Sebastian Ott823d4942009-06-16 10:30:20 +02001870 return 0;
Peter Oberparleiter37de53b2009-12-07 12:51:19 +01001871 ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
Sebastian Ott823d4942009-06-16 10:30:20 +02001872 return -ENODEV;
1873}
1874
1875static int resume_handle_disc(struct ccw_device *cdev)
1876{
1877 cdev->private->state = DEV_STATE_DISCONNECTED;
Sebastian Ott76e6fb42010-02-26 22:37:28 +01001878 if (ccw_device_notify(cdev, CIO_GONE) == NOTIFY_OK)
Sebastian Ott823d4942009-06-16 10:30:20 +02001879 return 0;
Peter Oberparleiter37de53b2009-12-07 12:51:19 +01001880 ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
Sebastian Ott823d4942009-06-16 10:30:20 +02001881 return -ENODEV;
1882}
1883
1884static int ccw_device_pm_restore(struct device *dev)
1885{
1886 struct ccw_device *cdev = to_ccwdev(dev);
Sebastian Ott0d01bb82010-02-26 22:37:29 +01001887 struct subchannel *sch;
1888 int ret = 0;
Sebastian Ott823d4942009-06-16 10:30:20 +02001889
1890 __ccw_device_pm_restore(cdev);
Sebastian Ott0d01bb82010-02-26 22:37:29 +01001891 sch = to_subchannel(cdev->dev.parent);
Sebastian Ott823d4942009-06-16 10:30:20 +02001892 spin_lock_irq(sch->lock);
Sebastian Ott0d01bb82010-02-26 22:37:29 +01001893 if (cio_is_console(sch->schid))
Sebastian Ott823d4942009-06-16 10:30:20 +02001894 goto out_restore;
Sebastian Ott0d01bb82010-02-26 22:37:29 +01001895
Sebastian Ott823d4942009-06-16 10:30:20 +02001896 /* check recognition results */
1897 switch (cdev->private->state) {
1898 case DEV_STATE_OFFLINE:
Sebastian Ott0d01bb82010-02-26 22:37:29 +01001899 case DEV_STATE_ONLINE:
1900 cdev->private->flags.donotify = 0;
Sebastian Ott823d4942009-06-16 10:30:20 +02001901 break;
1902 case DEV_STATE_BOXED:
1903 ret = resume_handle_boxed(cdev);
Sebastian Ott823d4942009-06-16 10:30:20 +02001904 if (ret)
Sebastian Ott0d01bb82010-02-26 22:37:29 +01001905 goto out_unlock;
Sebastian Ott823d4942009-06-16 10:30:20 +02001906 goto out_restore;
Sebastian Ott823d4942009-06-16 10:30:20 +02001907 default:
Sebastian Ott0d01bb82010-02-26 22:37:29 +01001908 ret = resume_handle_disc(cdev);
1909 if (ret)
1910 goto out_unlock;
1911 goto out_restore;
Sebastian Ott823d4942009-06-16 10:30:20 +02001912 }
1913 /* check if the device type has changed */
1914 if (!ccw_device_test_sense_data(cdev)) {
1915 ccw_device_update_sense_data(cdev);
Peter Oberparleiter37de53b2009-12-07 12:51:19 +01001916 ccw_device_sched_todo(cdev, CDEV_TODO_REBIND);
Sebastian Ott823d4942009-06-16 10:30:20 +02001917 ret = -ENODEV;
1918 goto out_unlock;
1919 }
Sebastian Ott0d01bb82010-02-26 22:37:29 +01001920 if (!cdev->online)
1921 goto out_unlock;
1922
1923 if (ccw_device_online(cdev)) {
1924 ret = resume_handle_disc(cdev);
1925 if (ret)
1926 goto out_unlock;
1927 goto out_restore;
1928 }
1929 spin_unlock_irq(sch->lock);
1930 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
1931 spin_lock_irq(sch->lock);
1932
1933 if (ccw_device_notify(cdev, CIO_OPER) == NOTIFY_BAD) {
1934 ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
1935 ret = -ENODEV;
Sebastian Ott823d4942009-06-16 10:30:20 +02001936 goto out_unlock;
1937 }
Sebastian Ott823d4942009-06-16 10:30:20 +02001938
Sebastian Ott0d01bb82010-02-26 22:37:29 +01001939 /* reenable cmf, if needed */
1940 if (cdev->private->cmb) {
1941 spin_unlock_irq(sch->lock);
Sebastian Ott823d4942009-06-16 10:30:20 +02001942 ret = ccw_set_cmf(cdev, 1);
Sebastian Ott0d01bb82010-02-26 22:37:29 +01001943 spin_lock_irq(sch->lock);
Sebastian Ott823d4942009-06-16 10:30:20 +02001944 if (ret) {
Sebastian Ottf0148242009-09-11 10:28:23 +02001945 CIO_MSG_EVENT(2, "resume: cdev 0.%x.%04x: cmf failed "
1946 "(rc=%d)\n", cdev->private->dev_id.ssid,
1947 cdev->private->dev_id.devno, ret);
Sebastian Ott823d4942009-06-16 10:30:20 +02001948 ret = 0;
1949 }
1950 }
1951
1952out_restore:
Sebastian Ott0d01bb82010-02-26 22:37:29 +01001953 spin_unlock_irq(sch->lock);
Sebastian Ott823d4942009-06-16 10:30:20 +02001954 if (cdev->online && cdev->drv && cdev->drv->restore)
1955 ret = cdev->drv->restore(cdev);
Sebastian Ott823d4942009-06-16 10:30:20 +02001956 return ret;
1957
Sebastian Ott823d4942009-06-16 10:30:20 +02001958out_unlock:
1959 spin_unlock_irq(sch->lock);
1960 return ret;
1961}
1962
Alexey Dobriyan47145212009-12-14 18:00:08 -08001963static const struct dev_pm_ops ccw_pm_ops = {
Sebastian Ott823d4942009-06-16 10:30:20 +02001964 .prepare = ccw_device_pm_prepare,
1965 .complete = ccw_device_pm_complete,
1966 .freeze = ccw_device_pm_freeze,
1967 .thaw = ccw_device_pm_thaw,
1968 .restore = ccw_device_pm_restore,
1969};
1970
Cornelia Huck8bbace72006-01-11 10:56:22 +01001971struct bus_type ccw_bus_type = {
1972 .name = "ccw",
1973 .match = ccw_bus_match,
1974 .uevent = ccw_uevent,
1975 .probe = ccw_device_probe,
1976 .remove = ccw_device_remove,
Cornelia Huck958974f2007-10-12 16:11:21 +02001977 .shutdown = ccw_device_shutdown,
Sebastian Ott823d4942009-06-16 10:30:20 +02001978 .pm = &ccw_pm_ops,
Cornelia Huck8bbace72006-01-11 10:56:22 +01001979};
1980
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +02001981/**
1982 * ccw_driver_register() - register a ccw driver
1983 * @cdriver: driver to be registered
1984 *
1985 * This function is mainly a wrapper around driver_register().
1986 * Returns:
1987 * %0 on success and a negative error value on failure.
1988 */
1989int ccw_driver_register(struct ccw_driver *cdriver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990{
1991 struct device_driver *drv = &cdriver->driver;
1992
1993 drv->bus = &ccw_bus_type;
1994 drv->name = cdriver->name;
Cornelia Huck4beee642008-01-26 14:10:47 +01001995 drv->owner = cdriver->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996
1997 return driver_register(drv);
1998}
1999
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +02002000/**
2001 * ccw_driver_unregister() - deregister a ccw driver
2002 * @cdriver: driver to be deregistered
2003 *
2004 * This function is mainly a wrapper around driver_unregister().
2005 */
2006void ccw_driver_unregister(struct ccw_driver *cdriver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007{
2008 driver_unregister(&cdriver->driver);
2009}
2010
Cornelia Hucka8237fc2006-01-06 00:19:21 -08002011/* Helper func for qdio. */
2012struct subchannel_id
2013ccw_device_get_subchannel_id(struct ccw_device *cdev)
2014{
2015 struct subchannel *sch;
2016
2017 sch = to_subchannel(cdev->dev.parent);
2018 return sch->schid;
2019}
2020
Peter Oberparleiter37de53b2009-12-07 12:51:19 +01002021static void ccw_device_todo(struct work_struct *work)
2022{
2023 struct ccw_device_private *priv;
2024 struct ccw_device *cdev;
2025 struct subchannel *sch;
2026 enum cdev_todo todo;
2027
2028 priv = container_of(work, struct ccw_device_private, todo_work);
2029 cdev = priv->cdev;
2030 sch = to_subchannel(cdev->dev.parent);
2031 /* Find out todo. */
2032 spin_lock_irq(cdev->ccwlock);
2033 todo = priv->todo;
2034 priv->todo = CDEV_TODO_NOTHING;
2035 CIO_MSG_EVENT(4, "cdev_todo: cdev=0.%x.%04x todo=%d\n",
2036 priv->dev_id.ssid, priv->dev_id.devno, todo);
2037 spin_unlock_irq(cdev->ccwlock);
2038 /* Perform todo. */
2039 switch (todo) {
2040 case CDEV_TODO_ENABLE_CMF:
2041 cmf_reenable(cdev);
2042 break;
2043 case CDEV_TODO_REBIND:
2044 ccw_device_do_unbind_bind(cdev);
2045 break;
2046 case CDEV_TODO_REGISTER:
2047 io_subchannel_register(cdev);
2048 break;
2049 case CDEV_TODO_UNREG_EVAL:
2050 if (!sch_is_pseudo_sch(sch))
2051 css_schedule_eval(sch->schid);
2052 /* fall-through */
2053 case CDEV_TODO_UNREG:
2054 if (sch_is_pseudo_sch(sch))
2055 ccw_device_unregister(cdev);
2056 else
2057 ccw_device_call_sch_unregister(cdev);
2058 break;
2059 default:
2060 break;
2061 }
2062 /* Release workqueue ref. */
2063 put_device(&cdev->dev);
2064}
2065
2066/**
2067 * ccw_device_sched_todo - schedule ccw device operation
2068 * @cdev: ccw device
2069 * @todo: todo
2070 *
2071 * Schedule the operation identified by @todo to be performed on the slow path
2072 * workqueue. Do nothing if another operation with higher priority is already
2073 * scheduled. Needs to be called with ccwdev lock held.
2074 */
2075void ccw_device_sched_todo(struct ccw_device *cdev, enum cdev_todo todo)
2076{
2077 CIO_MSG_EVENT(4, "cdev_todo: sched cdev=0.%x.%04x todo=%d\n",
2078 cdev->private->dev_id.ssid, cdev->private->dev_id.devno,
2079 todo);
2080 if (cdev->private->todo >= todo)
2081 return;
2082 cdev->private->todo = todo;
2083 /* Get workqueue ref. */
2084 if (!get_device(&cdev->dev))
2085 return;
Sebastian Ottbe5d3822010-02-26 22:37:24 +01002086 if (!queue_work(cio_work_q, &cdev->private->todo_work)) {
Peter Oberparleiter37de53b2009-12-07 12:51:19 +01002087 /* Already queued, release workqueue ref. */
2088 put_device(&cdev->dev);
2089 }
2090}
2091
Michael Ernstfd0457a2010-08-09 18:12:50 +02002092/**
2093 * ccw_device_siosl() - initiate logging
2094 * @cdev: ccw device
2095 *
2096 * This function is used to invoke model-dependent logging within the channel
2097 * subsystem.
2098 */
2099int ccw_device_siosl(struct ccw_device *cdev)
2100{
2101 struct subchannel *sch = to_subchannel(cdev->dev.parent);
2102
2103 return chsc_siosl(sch->schid);
2104}
2105EXPORT_SYMBOL_GPL(ccw_device_siosl);
2106
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107MODULE_LICENSE("GPL");
2108EXPORT_SYMBOL(ccw_device_set_online);
2109EXPORT_SYMBOL(ccw_device_set_offline);
2110EXPORT_SYMBOL(ccw_driver_register);
2111EXPORT_SYMBOL(ccw_driver_unregister);
2112EXPORT_SYMBOL(get_ccwdev_by_busid);
2113EXPORT_SYMBOL(ccw_bus_type);
Cornelia Hucka8237fc2006-01-06 00:19:21 -08002114EXPORT_SYMBOL_GPL(ccw_device_get_subchannel_id);