blob: 334459eb23c89cb65f0bf65a46a7b7d46bbaea5c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * drivers/s390/cio/device.c
3 * bus driver for ccw devices
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Cornelia Huckc820de32008-07-14 09:58:45 +02005 * Copyright IBM Corp. 2002,2008
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * Author(s): Arnd Bergmann (arndb@de.ibm.com)
Cornelia Huck4ce3b302006-01-14 13:21:04 -08007 * Cornelia Huck (cornelia.huck@de.ibm.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * Martin Schwidefsky (schwidefsky@de.ibm.com)
9 */
Peter Oberparleitera7ae2c022009-12-07 12:51:20 +010010
11#define KMSG_COMPONENT "cio"
12#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
13
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/module.h>
15#include <linux/init.h>
16#include <linux/spinlock.h>
17#include <linux/errno.h>
18#include <linux/err.h>
19#include <linux/slab.h>
20#include <linux/list.h>
21#include <linux/device.h>
22#include <linux/workqueue.h>
Peter Oberparleiter90ab1332008-01-26 14:10:52 +010023#include <linux/timer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
25#include <asm/ccwdev.h>
26#include <asm/cio.h>
Tim Schmielau4e57b682005-10-30 15:03:48 -080027#include <asm/param.h> /* HZ */
Cornelia Huck1842f2b2007-10-12 16:11:22 +020028#include <asm/cmb.h>
Cornelia Huck3a3fc292008-07-14 09:58:58 +020029#include <asm/isc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +020031#include "chp.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include "cio.h"
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +010033#include "cio_debug.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include "css.h"
35#include "device.h"
36#include "ioasm.h"
Cornelia Huckcd6b4f22008-01-26 14:10:43 +010037#include "io_sch.h"
Peter Oberparleiterecf5d9e2008-10-10 21:33:06 +020038#include "blacklist.h"
Michael Ernstfd0457a2010-08-09 18:12:50 +020039#include "chsc.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Peter Oberparleiter90ab1332008-01-26 14:10:52 +010041static struct timer_list recovery_timer;
Cornelia Huck486d0a02008-02-19 15:29:23 +010042static DEFINE_SPINLOCK(recovery_lock);
Peter Oberparleiter90ab1332008-01-26 14:10:52 +010043static int recovery_phase;
44static const unsigned long recovery_delay[] = { 3, 30, 300 };
45
Linus Torvalds1da177e2005-04-16 15:20:36 -070046/******************* bus type handling ***********************/
47
48/* The Linux driver model distinguishes between a bus type and
49 * the bus itself. Of course we only have one channel
50 * subsystem driver and one channel system per machine, but
51 * we still use the abstraction. T.R. says it's a good idea. */
52static int
53ccw_bus_match (struct device * dev, struct device_driver * drv)
54{
55 struct ccw_device *cdev = to_ccwdev(dev);
56 struct ccw_driver *cdrv = to_ccwdrv(drv);
57 const struct ccw_device_id *ids = cdrv->ids, *found;
58
59 if (!ids)
60 return 0;
61
62 found = ccw_device_id_match(ids, &cdev->id);
63 if (!found)
64 return 0;
65
66 cdev->id.driver_info = found->driver_info;
67
68 return 1;
69}
70
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +020071/* Store modalias string delimited by prefix/suffix string into buffer with
72 * specified size. Return length of resulting string (excluding trailing '\0')
73 * even if string doesn't fit buffer (snprintf semantics). */
Cornelia Huckcfbe9bb2007-04-27 16:01:32 +020074static int snprint_alias(char *buf, size_t size,
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +020075 struct ccw_device_id *id, const char *suffix)
76{
77 int len;
78
Cornelia Huckcfbe9bb2007-04-27 16:01:32 +020079 len = snprintf(buf, size, "ccw:t%04Xm%02X", id->cu_type, id->cu_model);
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +020080 if (len > size)
81 return len;
82 buf += len;
83 size -= len;
84
85 if (id->dev_type != 0)
86 len += snprintf(buf, size, "dt%04Xdm%02X%s", id->dev_type,
87 id->dev_model, suffix);
88 else
89 len += snprintf(buf, size, "dtdm%s", suffix);
90
91 return len;
92}
93
94/* Set up environment variables for ccw device uevent. Return 0 on success,
95 * non-zero otherwise. */
Kay Sievers7eff2e72007-08-14 15:15:12 +020096static int ccw_uevent(struct device *dev, struct kobj_uevent_env *env)
Linus Torvalds1da177e2005-04-16 15:20:36 -070097{
98 struct ccw_device *cdev = to_ccwdev(dev);
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +020099 struct ccw_device_id *id = &(cdev->id);
Cornelia Huckcfbe9bb2007-04-27 16:01:32 +0200100 int ret;
101 char modalias_buf[30];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +0200103 /* CU_TYPE= */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200104 ret = add_uevent_var(env, "CU_TYPE=%04X", id->cu_type);
Cornelia Huckcfbe9bb2007-04-27 16:01:32 +0200105 if (ret)
106 return ret;
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +0200107
108 /* CU_MODEL= */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200109 ret = add_uevent_var(env, "CU_MODEL=%02X", id->cu_model);
Cornelia Huckcfbe9bb2007-04-27 16:01:32 +0200110 if (ret)
111 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
113 /* The next two can be zero, that's ok for us */
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +0200114 /* DEV_TYPE= */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200115 ret = add_uevent_var(env, "DEV_TYPE=%04X", id->dev_type);
Cornelia Huckcfbe9bb2007-04-27 16:01:32 +0200116 if (ret)
117 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +0200119 /* DEV_MODEL= */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200120 ret = add_uevent_var(env, "DEV_MODEL=%02X", id->dev_model);
Cornelia Huckcfbe9bb2007-04-27 16:01:32 +0200121 if (ret)
122 return ret;
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +0200123
124 /* MODALIAS= */
Cornelia Huckcfbe9bb2007-04-27 16:01:32 +0200125 snprint_alias(modalias_buf, sizeof(modalias_buf), id, "");
Kay Sievers7eff2e72007-08-14 15:15:12 +0200126 ret = add_uevent_var(env, "MODALIAS=%s", modalias_buf);
127 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128}
129
Cornelia Huck8bbace72006-01-11 10:56:22 +0100130struct bus_type ccw_bus_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
Cornelia Huck602b20f2008-01-26 14:10:39 +0100132static void io_subchannel_irq(struct subchannel *);
133static int io_subchannel_probe(struct subchannel *);
134static int io_subchannel_remove(struct subchannel *);
Cornelia Huck8bbace72006-01-11 10:56:22 +0100135static void io_subchannel_shutdown(struct subchannel *);
Cornelia Huckc820de32008-07-14 09:58:45 +0200136static int io_subchannel_sch_event(struct subchannel *, int);
Cornelia Huck99611f82008-07-14 09:59:02 +0200137static int io_subchannel_chp_event(struct subchannel *, struct chp_link *,
138 int);
Sebastian Ott8ea7f552009-09-22 22:58:35 +0200139static void recovery_func(unsigned long data);
Sebastian Ott8ea7f552009-09-22 22:58:35 +0200140wait_queue_head_t ccw_device_init_wq;
141atomic_t ccw_device_init_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
Cornelia Huckf08adc02008-07-14 09:59:03 +0200143static struct css_device_id io_subchannel_ids[] = {
144 { .match_flags = 0x1, .type = SUBCHANNEL_TYPE_IO, },
145 { /* end of list */ },
146};
147MODULE_DEVICE_TABLE(css, io_subchannel_ids);
148
Cornelia Huck93a27592009-06-16 10:30:23 +0200149static int io_subchannel_prepare(struct subchannel *sch)
150{
151 struct ccw_device *cdev;
152 /*
153 * Don't allow suspend while a ccw device registration
154 * is still outstanding.
155 */
156 cdev = sch_get_cdev(sch);
157 if (cdev && !device_is_registered(&cdev->dev))
158 return -EAGAIN;
159 return 0;
160}
161
Sebastian Ottb4c70722010-02-26 22:37:27 +0100162static int io_subchannel_settle(void)
Sebastian Ott8ea7f552009-09-22 22:58:35 +0200163{
Sebastian Ottb4c70722010-02-26 22:37:27 +0100164 int ret;
165
166 ret = wait_event_interruptible(ccw_device_init_wq,
167 atomic_read(&ccw_device_init_count) == 0);
168 if (ret)
169 return -EINTR;
Sebastian Ottbe5d3822010-02-26 22:37:24 +0100170 flush_workqueue(cio_work_q);
Sebastian Ottb4c70722010-02-26 22:37:27 +0100171 return 0;
Sebastian Ott8ea7f552009-09-22 22:58:35 +0200172}
173
Cornelia Huckf7e5d672007-05-10 15:45:43 +0200174static struct css_driver io_subchannel_driver = {
Sebastian Otte6aed122011-03-15 17:08:30 +0100175 .drv = {
176 .owner = THIS_MODULE,
177 .name = "io_subchannel",
178 },
Cornelia Huckf08adc02008-07-14 09:59:03 +0200179 .subchannel_type = io_subchannel_ids,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 .irq = io_subchannel_irq,
Cornelia Huckc820de32008-07-14 09:58:45 +0200181 .sch_event = io_subchannel_sch_event,
182 .chp_event = io_subchannel_chp_event,
Cornelia Huck8bbace72006-01-11 10:56:22 +0100183 .probe = io_subchannel_probe,
184 .remove = io_subchannel_remove,
185 .shutdown = io_subchannel_shutdown,
Cornelia Huck93a27592009-06-16 10:30:23 +0200186 .prepare = io_subchannel_prepare,
Sebastian Ott8ea7f552009-09-22 22:58:35 +0200187 .settle = io_subchannel_settle,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188};
189
Sebastian Ott2f176442009-09-22 22:58:33 +0200190int __init io_subchannel_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191{
192 int ret;
193
194 init_waitqueue_head(&ccw_device_init_wq);
195 atomic_set(&ccw_device_init_count, 0);
Peter Oberparleiter90ab1332008-01-26 14:10:52 +0100196 setup_timer(&recovery_timer, recovery_func, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
Sebastian Ottbe5d3822010-02-26 22:37:24 +0100198 ret = bus_register(&ccw_bus_type);
199 if (ret)
200 return ret;
Cornelia Huck25b7bb52008-01-26 14:10:41 +0100201 ret = css_driver_register(&io_subchannel_driver);
202 if (ret)
Sebastian Ottbe5d3822010-02-26 22:37:24 +0100203 bus_unregister(&ccw_bus_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 return ret;
206}
207
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
209/************************ device handling **************************/
210
211/*
212 * A ccw_device has some interfaces in sysfs in addition to the
213 * standard ones.
214 * The following entries are designed to export the information which
215 * resided in 2.4 in /proc/subchannels. Subchannel and device number
216 * are obvious, so they don't have an entry :)
217 * TODO: Split chpids and pimpampom up? Where is "in use" in the tree?
218 */
219static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400220chpids_show (struct device * dev, struct device_attribute *attr, char * buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221{
222 struct subchannel *sch = to_subchannel(dev);
Peter Oberparleiter7ad6a242007-04-27 16:01:35 +0200223 struct chsc_ssd_info *ssd = &sch->ssd_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 ssize_t ret = 0;
225 int chp;
Peter Oberparleiter7ad6a242007-04-27 16:01:35 +0200226 int mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
Peter Oberparleiter7ad6a242007-04-27 16:01:35 +0200228 for (chp = 0; chp < 8; chp++) {
229 mask = 0x80 >> chp;
230 if (ssd->path_mask & mask)
231 ret += sprintf(buf + ret, "%02x ", ssd->chpid[chp].id);
232 else
233 ret += sprintf(buf + ret, "00 ");
234 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 ret += sprintf (buf+ret, "\n");
236 return min((ssize_t)PAGE_SIZE, ret);
237}
238
239static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400240pimpampom_show (struct device * dev, struct device_attribute *attr, char * buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241{
242 struct subchannel *sch = to_subchannel(dev);
243 struct pmcw *pmcw = &sch->schib.pmcw;
244
245 return sprintf (buf, "%02x %02x %02x\n",
246 pmcw->pim, pmcw->pam, pmcw->pom);
247}
248
249static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400250devtype_show (struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251{
252 struct ccw_device *cdev = to_ccwdev(dev);
253 struct ccw_device_id *id = &(cdev->id);
254
255 if (id->dev_type != 0)
256 return sprintf(buf, "%04x/%02x\n",
257 id->dev_type, id->dev_model);
258 else
259 return sprintf(buf, "n/a\n");
260}
261
262static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400263cutype_show (struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264{
265 struct ccw_device *cdev = to_ccwdev(dev);
266 struct ccw_device_id *id = &(cdev->id);
267
268 return sprintf(buf, "%04x/%02x\n",
269 id->cu_type, id->cu_model);
270}
271
272static ssize_t
Bastian Blankf1fc78a2005-10-30 15:00:12 -0800273modalias_show (struct device *dev, struct device_attribute *attr, char *buf)
274{
275 struct ccw_device *cdev = to_ccwdev(dev);
276 struct ccw_device_id *id = &(cdev->id);
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +0200277 int len;
Bastian Blankf1fc78a2005-10-30 15:00:12 -0800278
Cornelia Huck086a6c62007-07-17 13:36:08 +0200279 len = snprint_alias(buf, PAGE_SIZE, id, "\n");
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +0200280
281 return len > PAGE_SIZE ? PAGE_SIZE : len;
Bastian Blankf1fc78a2005-10-30 15:00:12 -0800282}
283
284static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400285online_show (struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286{
287 struct ccw_device *cdev = to_ccwdev(dev);
288
289 return sprintf(buf, cdev->online ? "1\n" : "0\n");
290}
291
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100292int ccw_device_is_orphan(struct ccw_device *cdev)
293{
294 return sch_is_pseudo_sch(to_subchannel(cdev->dev.parent));
295}
296
Cornelia Huckef995162007-04-27 16:01:39 +0200297static void ccw_device_unregister(struct ccw_device *cdev)
Cornelia Huck7674da72006-12-08 15:54:21 +0100298{
Sebastian Ott7d253b92009-12-07 12:51:33 +0100299 if (device_is_registered(&cdev->dev)) {
Sebastian Ott24a18722009-12-07 12:51:34 +0100300 /* Undo device_add(). */
Cornelia Huckef995162007-04-27 16:01:39 +0200301 device_del(&cdev->dev);
Sebastian Ott24a18722009-12-07 12:51:34 +0100302 }
303 if (cdev->private->flags.initialized) {
304 cdev->private->flags.initialized = 0;
Sebastian Ott3b554a12009-09-11 10:28:26 +0200305 /* Release reference from device_initialize(). */
306 put_device(&cdev->dev);
307 }
Cornelia Huck7674da72006-12-08 15:54:21 +0100308}
309
Sebastian Ottd40f7b72009-12-07 12:51:41 +0100310static void io_subchannel_quiesce(struct subchannel *);
311
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +0200312/**
313 * ccw_device_set_offline() - disable a ccw device for I/O
314 * @cdev: target ccw device
315 *
316 * This function calls the driver's set_offline() function for @cdev, if
317 * given, and then disables @cdev.
318 * Returns:
319 * %0 on success and a negative error value on failure.
320 * Context:
321 * enabled, ccw device lock not held
322 */
323int ccw_device_set_offline(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324{
Sebastian Ottd40f7b72009-12-07 12:51:41 +0100325 struct subchannel *sch;
326 int ret, state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
328 if (!cdev)
329 return -ENODEV;
330 if (!cdev->online || !cdev->drv)
331 return -EINVAL;
332
333 if (cdev->drv->set_offline) {
334 ret = cdev->drv->set_offline(cdev);
335 if (ret != 0)
336 return ret;
337 }
338 cdev->online = 0;
339 spin_lock_irq(cdev->ccwlock);
Sebastian Ottd40f7b72009-12-07 12:51:41 +0100340 sch = to_subchannel(cdev->dev.parent);
Michael Ernst217ee6c2009-09-11 10:28:21 +0200341 /* Wait until a final state or DISCONNECTED is reached */
342 while (!dev_fsm_final_state(cdev) &&
343 cdev->private->state != DEV_STATE_DISCONNECTED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 spin_unlock_irq(cdev->ccwlock);
Michael Ernst217ee6c2009-09-11 10:28:21 +0200345 wait_event(cdev->private->wait_q, (dev_fsm_final_state(cdev) ||
346 cdev->private->state == DEV_STATE_DISCONNECTED));
347 spin_lock_irq(cdev->ccwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 }
Sebastian Ottd40f7b72009-12-07 12:51:41 +0100349 do {
350 ret = ccw_device_offline(cdev);
351 if (!ret)
352 break;
353 CIO_MSG_EVENT(0, "ccw_device_offline returned %d, device "
354 "0.%x.%04x\n", ret, cdev->private->dev_id.ssid,
355 cdev->private->dev_id.devno);
356 if (ret != -EBUSY)
357 goto error;
358 state = cdev->private->state;
359 spin_unlock_irq(cdev->ccwlock);
360 io_subchannel_quiesce(sch);
361 spin_lock_irq(cdev->ccwlock);
362 cdev->private->state = state;
363 } while (ret == -EBUSY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 spin_unlock_irq(cdev->ccwlock);
Michael Ernst217ee6c2009-09-11 10:28:21 +0200365 wait_event(cdev->private->wait_q, (dev_fsm_final_state(cdev) ||
366 cdev->private->state == DEV_STATE_DISCONNECTED));
Peter Oberparleitera7ae2c022009-12-07 12:51:20 +0100367 /* Inform the user if set offline failed. */
368 if (cdev->private->state == DEV_STATE_BOXED) {
369 pr_warning("%s: The device entered boxed state while "
370 "being set offline\n", dev_name(&cdev->dev));
371 } else if (cdev->private->state == DEV_STATE_NOT_OPER) {
372 pr_warning("%s: The device stopped operating while "
373 "being set offline\n", dev_name(&cdev->dev));
374 }
Michael Ernst217ee6c2009-09-11 10:28:21 +0200375 /* Give up reference from ccw_device_set_online(). */
376 put_device(&cdev->dev);
377 return 0;
378
379error:
Michael Ernst217ee6c2009-09-11 10:28:21 +0200380 cdev->private->state = DEV_STATE_OFFLINE;
381 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
382 spin_unlock_irq(cdev->ccwlock);
383 /* Give up reference from ccw_device_set_online(). */
384 put_device(&cdev->dev);
385 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386}
387
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +0200388/**
389 * ccw_device_set_online() - enable a ccw device for I/O
390 * @cdev: target ccw device
391 *
392 * This function first enables @cdev and then calls the driver's set_online()
393 * function for @cdev, if given. If set_online() returns an error, @cdev is
394 * disabled again.
395 * Returns:
396 * %0 on success and a negative error value on failure.
397 * Context:
398 * enabled, ccw device lock not held
399 */
400int ccw_device_set_online(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401{
402 int ret;
Michael Ernst217ee6c2009-09-11 10:28:21 +0200403 int ret2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
405 if (!cdev)
406 return -ENODEV;
407 if (cdev->online || !cdev->drv)
408 return -EINVAL;
Cornelia Huck9cd67422008-12-25 13:39:06 +0100409 /* Hold on to an extra reference while device is online. */
410 if (!get_device(&cdev->dev))
411 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
413 spin_lock_irq(cdev->ccwlock);
414 ret = ccw_device_online(cdev);
415 spin_unlock_irq(cdev->ccwlock);
416 if (ret == 0)
417 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
418 else {
Michael Ernst139b83d2008-05-07 09:22:54 +0200419 CIO_MSG_EVENT(0, "ccw_device_online returned %d, "
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200420 "device 0.%x.%04x\n",
421 ret, cdev->private->dev_id.ssid,
422 cdev->private->dev_id.devno);
Cornelia Huck9cd67422008-12-25 13:39:06 +0100423 /* Give up online reference since onlining failed. */
424 put_device(&cdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 return ret;
426 }
Michael Ernst217ee6c2009-09-11 10:28:21 +0200427 spin_lock_irq(cdev->ccwlock);
428 /* Check if online processing was successful */
429 if ((cdev->private->state != DEV_STATE_ONLINE) &&
430 (cdev->private->state != DEV_STATE_W4SENSE)) {
431 spin_unlock_irq(cdev->ccwlock);
Peter Oberparleitera7ae2c022009-12-07 12:51:20 +0100432 /* Inform the user that set online failed. */
433 if (cdev->private->state == DEV_STATE_BOXED) {
434 pr_warning("%s: Setting the device online failed "
435 "because it is boxed\n",
436 dev_name(&cdev->dev));
437 } else if (cdev->private->state == DEV_STATE_NOT_OPER) {
438 pr_warning("%s: Setting the device online failed "
439 "because it is not operational\n",
440 dev_name(&cdev->dev));
441 }
Cornelia Huck9cd67422008-12-25 13:39:06 +0100442 /* Give up online reference since onlining failed. */
443 put_device(&cdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 return -ENODEV;
Cornelia Huck9cd67422008-12-25 13:39:06 +0100445 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 spin_unlock_irq(cdev->ccwlock);
Michael Ernst217ee6c2009-09-11 10:28:21 +0200447 if (cdev->drv->set_online)
448 ret = cdev->drv->set_online(cdev);
449 if (ret)
450 goto rollback;
451 cdev->online = 1;
452 return 0;
453
454rollback:
455 spin_lock_irq(cdev->ccwlock);
456 /* Wait until a final state or DISCONNECTED is reached */
457 while (!dev_fsm_final_state(cdev) &&
458 cdev->private->state != DEV_STATE_DISCONNECTED) {
459 spin_unlock_irq(cdev->ccwlock);
460 wait_event(cdev->private->wait_q, (dev_fsm_final_state(cdev) ||
461 cdev->private->state == DEV_STATE_DISCONNECTED));
462 spin_lock_irq(cdev->ccwlock);
463 }
464 ret2 = ccw_device_offline(cdev);
465 if (ret2)
466 goto error;
467 spin_unlock_irq(cdev->ccwlock);
468 wait_event(cdev->private->wait_q, (dev_fsm_final_state(cdev) ||
469 cdev->private->state == DEV_STATE_DISCONNECTED));
Cornelia Huck9cd67422008-12-25 13:39:06 +0100470 /* Give up online reference since onlining failed. */
471 put_device(&cdev->dev);
Michael Ernst217ee6c2009-09-11 10:28:21 +0200472 return ret;
473
474error:
475 CIO_MSG_EVENT(0, "rollback ccw_device_offline returned %d, "
476 "device 0.%x.%04x\n",
477 ret2, cdev->private->dev_id.ssid,
478 cdev->private->dev_id.devno);
479 cdev->private->state = DEV_STATE_OFFLINE;
480 spin_unlock_irq(cdev->ccwlock);
481 /* Give up online reference since onlining failed. */
482 put_device(&cdev->dev);
483 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484}
485
Sebastian Otte74fe0c2009-03-26 15:24:08 +0100486static int online_store_handle_offline(struct ccw_device *cdev)
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200487{
Peter Oberparleiter37de53b2009-12-07 12:51:19 +0100488 if (cdev->private->state == DEV_STATE_DISCONNECTED) {
489 spin_lock_irq(cdev->ccwlock);
490 ccw_device_sched_todo(cdev, CDEV_TODO_UNREG_EVAL);
491 spin_unlock_irq(cdev->ccwlock);
Sebastian Ott7cd40312010-08-09 18:12:52 +0200492 return 0;
493 }
494 if (cdev->drv && cdev->drv->set_offline)
Sebastian Otte74fe0c2009-03-26 15:24:08 +0100495 return ccw_device_set_offline(cdev);
Sebastian Ott7cd40312010-08-09 18:12:52 +0200496 return -EINVAL;
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200497}
498
499static int online_store_recog_and_online(struct ccw_device *cdev)
500{
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200501 /* Do device recognition, if needed. */
Sebastian Ott99f6a5702009-03-31 19:16:07 +0200502 if (cdev->private->state == DEV_STATE_BOXED) {
Peter Oberparleiter1f5bd382009-12-07 12:51:23 +0100503 spin_lock_irq(cdev->ccwlock);
Peter Oberparleiter736b5db2009-12-07 12:51:21 +0100504 ccw_device_recognition(cdev);
Peter Oberparleiter1f5bd382009-12-07 12:51:23 +0100505 spin_unlock_irq(cdev->ccwlock);
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200506 wait_event(cdev->private->wait_q,
507 cdev->private->flags.recog_done);
Sebastian Ott156013f2009-03-31 19:16:03 +0200508 if (cdev->private->state != DEV_STATE_OFFLINE)
509 /* recognition failed */
510 return -EAGAIN;
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200511 }
512 if (cdev->drv && cdev->drv->set_online)
Sebastian Ott7cd40312010-08-09 18:12:52 +0200513 return ccw_device_set_online(cdev);
514 return -EINVAL;
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200515}
Sebastian Ott156013f2009-03-31 19:16:03 +0200516
Michael Ernstc78aa6c2008-07-14 09:59:22 +0200517static int online_store_handle_online(struct ccw_device *cdev, int force)
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200518{
519 int ret;
520
521 ret = online_store_recog_and_online(cdev);
Sebastian Ott156013f2009-03-31 19:16:03 +0200522 if (ret && !force)
Michael Ernstc78aa6c2008-07-14 09:59:22 +0200523 return ret;
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200524 if (force && cdev->private->state == DEV_STATE_BOXED) {
525 ret = ccw_device_stlck(cdev);
Michael Ernstc78aa6c2008-07-14 09:59:22 +0200526 if (ret)
527 return ret;
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200528 if (cdev->id.cu_type == 0)
529 cdev->private->state = DEV_STATE_NOT_OPER;
Sebastian Ott156013f2009-03-31 19:16:03 +0200530 ret = online_store_recog_and_online(cdev);
531 if (ret)
532 return ret;
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200533 }
Michael Ernstc78aa6c2008-07-14 09:59:22 +0200534 return 0;
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200535}
536
537static ssize_t online_store (struct device *dev, struct device_attribute *attr,
538 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539{
540 struct ccw_device *cdev = to_ccwdev(dev);
Cornelia Huck2f972202008-04-30 13:38:33 +0200541 int force, ret;
542 unsigned long i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
Peter Oberparleiter350e9122009-12-07 12:51:28 +0100544 if (!dev_fsm_final_state(cdev) &&
545 cdev->private->state != DEV_STATE_DISCONNECTED)
546 return -EAGAIN;
547 if (atomic_cmpxchg(&cdev->private->onoff, 0, 1) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 return -EAGAIN;
549
Sebastian Ott3bda0582011-03-23 10:16:02 +0100550 if (cdev->drv && !try_module_get(cdev->drv->driver.owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 atomic_set(&cdev->private->onoff, 0);
552 return -EINVAL;
553 }
554 if (!strncmp(buf, "force\n", count)) {
555 force = 1;
556 i = 1;
Cornelia Huck2f972202008-04-30 13:38:33 +0200557 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 } else {
559 force = 0;
Cornelia Huck2f972202008-04-30 13:38:33 +0200560 ret = strict_strtoul(buf, 16, &i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 }
Cornelia Huck2f972202008-04-30 13:38:33 +0200562 if (ret)
563 goto out;
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200564 switch (i) {
565 case 0:
Sebastian Otte74fe0c2009-03-26 15:24:08 +0100566 ret = online_store_handle_offline(cdev);
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200567 break;
568 case 1:
Michael Ernstc78aa6c2008-07-14 09:59:22 +0200569 ret = online_store_handle_online(cdev, force);
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200570 break;
571 default:
Cornelia Huck2f972202008-04-30 13:38:33 +0200572 ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 }
Cornelia Huck2f972202008-04-30 13:38:33 +0200574out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 if (cdev->drv)
Sebastian Ott3bda0582011-03-23 10:16:02 +0100576 module_put(cdev->drv->driver.owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 atomic_set(&cdev->private->onoff, 0);
Sebastian Otte74fe0c2009-03-26 15:24:08 +0100578 return (ret < 0) ? ret : count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579}
580
581static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400582available_show (struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583{
584 struct ccw_device *cdev = to_ccwdev(dev);
585 struct subchannel *sch;
586
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100587 if (ccw_device_is_orphan(cdev))
588 return sprintf(buf, "no device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 switch (cdev->private->state) {
590 case DEV_STATE_BOXED:
591 return sprintf(buf, "boxed\n");
592 case DEV_STATE_DISCONNECTED:
593 case DEV_STATE_DISCONNECTED_SENSE_ID:
594 case DEV_STATE_NOT_OPER:
595 sch = to_subchannel(dev->parent);
596 if (!sch->lpm)
597 return sprintf(buf, "no path\n");
598 else
599 return sprintf(buf, "no device\n");
600 default:
601 /* All other states considered fine. */
602 return sprintf(buf, "good\n");
603 }
604}
605
Michael Ernstfd0457a2010-08-09 18:12:50 +0200606static ssize_t
607initiate_logging(struct device *dev, struct device_attribute *attr,
608 const char *buf, size_t count)
609{
610 struct subchannel *sch = to_subchannel(dev);
611 int rc;
612
613 rc = chsc_siosl(sch->schid);
614 if (rc < 0) {
615 pr_warning("Logging for subchannel 0.%x.%04x failed with "
616 "errno=%d\n",
617 sch->schid.ssid, sch->schid.sch_no, rc);
618 return rc;
619 }
620 pr_notice("Logging for subchannel 0.%x.%04x was triggered\n",
621 sch->schid.ssid, sch->schid.sch_no);
622 return count;
623}
624
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625static DEVICE_ATTR(chpids, 0444, chpids_show, NULL);
626static DEVICE_ATTR(pimpampom, 0444, pimpampom_show, NULL);
627static DEVICE_ATTR(devtype, 0444, devtype_show, NULL);
628static DEVICE_ATTR(cutype, 0444, cutype_show, NULL);
Bastian Blankf1fc78a2005-10-30 15:00:12 -0800629static DEVICE_ATTR(modalias, 0444, modalias_show, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630static DEVICE_ATTR(online, 0644, online_show, online_store);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631static DEVICE_ATTR(availability, 0444, available_show, NULL);
Michael Ernstfd0457a2010-08-09 18:12:50 +0200632static DEVICE_ATTR(logging, 0200, NULL, initiate_logging);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200634static struct attribute *io_subchannel_attrs[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 &dev_attr_chpids.attr,
636 &dev_attr_pimpampom.attr,
Michael Ernstfd0457a2010-08-09 18:12:50 +0200637 &dev_attr_logging.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 NULL,
639};
640
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200641static struct attribute_group io_subchannel_attr_group = {
642 .attrs = io_subchannel_attrs,
Cornelia Huck529192f2006-12-08 15:55:57 +0100643};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
645static struct attribute * ccwdev_attrs[] = {
646 &dev_attr_devtype.attr,
647 &dev_attr_cutype.attr,
Bastian Blankf1fc78a2005-10-30 15:00:12 -0800648 &dev_attr_modalias.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 &dev_attr_online.attr,
650 &dev_attr_cmb_enable.attr,
651 &dev_attr_availability.attr,
652 NULL,
653};
654
655static struct attribute_group ccwdev_attr_group = {
656 .attrs = ccwdev_attrs,
657};
658
David Brownella4dbd672009-06-24 10:06:31 -0700659static const struct attribute_group *ccwdev_attr_groups[] = {
Cornelia Huckef995162007-04-27 16:01:39 +0200660 &ccwdev_attr_group,
661 NULL,
662};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663
664/* this is a simple abstraction for device_register that sets the
665 * correct bus type and adds the bus specific files */
Cornelia Huck3c9da7b2006-10-27 12:39:33 +0200666static int ccw_device_register(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667{
668 struct device *dev = &cdev->dev;
669 int ret;
670
671 dev->bus = &ccw_bus_type;
Sebastian Ott3ac276f2009-09-11 10:28:27 +0200672 ret = dev_set_name(&cdev->dev, "0.%x.%04x", cdev->private->dev_id.ssid,
673 cdev->private->dev_id.devno);
674 if (ret)
675 return ret;
Sebastian Ott7d253b92009-12-07 12:51:33 +0100676 return device_add(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677}
678
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100679static int match_dev_id(struct device *dev, void *data)
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700680{
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100681 struct ccw_device *cdev = to_ccwdev(dev);
682 struct ccw_dev_id *dev_id = data;
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700683
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100684 return ccw_dev_id_is_equal(&cdev->private->dev_id, dev_id);
685}
686
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100687static struct ccw_device *get_ccwdev_by_dev_id(struct ccw_dev_id *dev_id)
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100688{
689 struct device *dev;
690
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100691 dev = bus_find_device(&ccw_bus_type, NULL, dev_id, match_dev_id);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100692
693 return dev ? to_ccwdev(dev) : NULL;
694}
695
Peter Oberparleiter37de53b2009-12-07 12:51:19 +0100696static void ccw_device_do_unbind_bind(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697{
Cornelia Huckeb32ae82009-03-26 15:24:05 +0100698 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
Sebastian Ott7d253b92009-12-07 12:51:33 +0100700 if (device_is_registered(&cdev->dev)) {
Cornelia Huckeb32ae82009-03-26 15:24:05 +0100701 device_release_driver(&cdev->dev);
702 ret = device_attach(&cdev->dev);
703 WARN_ON(ret == -ENODEV);
704 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705}
706
707static void
708ccw_device_release(struct device *dev)
709{
710 struct ccw_device *cdev;
711
712 cdev = to_ccwdev(dev);
Cornelia Huck6eff2082008-12-25 13:39:07 +0100713 /* Release reference of parent subchannel. */
714 put_device(cdev->dev.parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 kfree(cdev->private);
716 kfree(cdev);
717}
718
Cornelia Huck7674da72006-12-08 15:54:21 +0100719static struct ccw_device * io_subchannel_allocate_dev(struct subchannel *sch)
720{
721 struct ccw_device *cdev;
722
723 cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
724 if (cdev) {
725 cdev->private = kzalloc(sizeof(struct ccw_device_private),
726 GFP_KERNEL | GFP_DMA);
727 if (cdev->private)
728 return cdev;
729 }
730 kfree(cdev);
731 return ERR_PTR(-ENOMEM);
732}
733
Peter Oberparleiter37de53b2009-12-07 12:51:19 +0100734static void ccw_device_todo(struct work_struct *work);
735
Cornelia Huck7674da72006-12-08 15:54:21 +0100736static int io_subchannel_initialize_dev(struct subchannel *sch,
737 struct ccw_device *cdev)
738{
739 cdev->private->cdev = cdev;
740 atomic_set(&cdev->private->onoff, 0);
741 cdev->dev.parent = &sch->dev;
742 cdev->dev.release = ccw_device_release;
Peter Oberparleiter37de53b2009-12-07 12:51:19 +0100743 INIT_WORK(&cdev->private->todo_work, ccw_device_todo);
Cornelia Huckef995162007-04-27 16:01:39 +0200744 cdev->dev.groups = ccwdev_attr_groups;
Cornelia Huck7674da72006-12-08 15:54:21 +0100745 /* Do first half of device_register. */
746 device_initialize(&cdev->dev);
747 if (!get_device(&sch->dev)) {
Cornelia Huck283fdd02008-12-25 13:39:10 +0100748 /* Release reference from device_initialize(). */
749 put_device(&cdev->dev);
Cornelia Huck7674da72006-12-08 15:54:21 +0100750 return -ENODEV;
751 }
Sebastian Ott24a18722009-12-07 12:51:34 +0100752 cdev->private->flags.initialized = 1;
Cornelia Huck7674da72006-12-08 15:54:21 +0100753 return 0;
754}
755
756static struct ccw_device * io_subchannel_create_ccwdev(struct subchannel *sch)
757{
758 struct ccw_device *cdev;
759 int ret;
760
761 cdev = io_subchannel_allocate_dev(sch);
762 if (!IS_ERR(cdev)) {
763 ret = io_subchannel_initialize_dev(sch, cdev);
Sebastian Ott06739a82009-08-23 18:09:04 +0200764 if (ret)
Cornelia Huck7674da72006-12-08 15:54:21 +0100765 cdev = ERR_PTR(ret);
Cornelia Huck7674da72006-12-08 15:54:21 +0100766 }
767 return cdev;
768}
769
Peter Oberparleiter736b5db2009-12-07 12:51:21 +0100770static void io_subchannel_recog(struct ccw_device *, struct subchannel *);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100771
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100772static void sch_create_and_recog_new_device(struct subchannel *sch)
773{
774 struct ccw_device *cdev;
775
776 /* Need to allocate a new ccw device. */
777 cdev = io_subchannel_create_ccwdev(sch);
778 if (IS_ERR(cdev)) {
779 /* OK, we did everything we could... */
780 css_sch_device_unregister(sch);
781 return;
782 }
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100783 /* Start recognition for the new ccw device. */
Peter Oberparleiter736b5db2009-12-07 12:51:21 +0100784 io_subchannel_recog(cdev, sch);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100785}
786
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787/*
788 * Register recognized device.
789 */
Peter Oberparleiter37de53b2009-12-07 12:51:19 +0100790static void io_subchannel_register(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 struct subchannel *sch;
Sebastian Otta2901562010-03-08 12:25:17 +0100793 int ret, adjust_init_count = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 unsigned long flags;
795
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 sch = to_subchannel(cdev->dev.parent);
Cornelia Huck5fb6b852008-12-25 13:39:08 +0100797 /*
798 * Check if subchannel is still registered. It may have become
799 * unregistered if a machine check hit us after finishing
800 * device recognition but before the register work could be
801 * queued.
802 */
803 if (!device_is_registered(&sch->dev))
804 goto out_err;
Cornelia Huck82b7ac02007-04-27 16:01:36 +0200805 css_update_ssd_info(sch);
Cornelia Huck47af5512006-12-04 15:41:07 +0100806 /*
807 * io_subchannel_register() will also be called after device
808 * recognition has been done for a boxed device (which will already
809 * be registered). We need to reprobe since we may now have sense id
810 * information.
811 */
Cornelia Huckd6a30762008-12-25 13:39:11 +0100812 if (device_is_registered(&cdev->dev)) {
Cornelia Huck47af5512006-12-04 15:41:07 +0100813 if (!cdev->drv) {
814 ret = device_reprobe(&cdev->dev);
815 if (ret)
816 /* We can't do much here. */
Michael Ernst139b83d2008-05-07 09:22:54 +0200817 CIO_MSG_EVENT(0, "device_reprobe() returned"
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200818 " %d for 0.%x.%04x\n", ret,
819 cdev->private->dev_id.ssid,
820 cdev->private->dev_id.devno);
Cornelia Huck47af5512006-12-04 15:41:07 +0100821 }
Sebastian Otta2901562010-03-08 12:25:17 +0100822 adjust_init_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 goto out;
824 }
Cornelia Huckfa1a8c22007-04-26 00:12:03 -0700825 /*
826 * Now we know this subchannel will stay, we can throw
827 * our delayed uevent.
828 */
Ming Leif67f1292009-03-01 21:10:49 +0800829 dev_set_uevent_suppress(&sch->dev, 0);
Cornelia Huckfa1a8c22007-04-26 00:12:03 -0700830 kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 /* make it known to the system */
832 ret = ccw_device_register(cdev);
833 if (ret) {
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200834 CIO_MSG_EVENT(0, "Could not register ccw dev 0.%x.%04x: %d\n",
835 cdev->private->dev_id.ssid,
836 cdev->private->dev_id.devno, ret);
Cornelia Huck2ec22982006-12-08 15:54:26 +0100837 spin_lock_irqsave(sch->lock, flags);
Cornelia Huckdb6a6422008-01-26 14:10:46 +0100838 sch_set_cdev(sch, NULL);
Cornelia Huck2ec22982006-12-08 15:54:26 +0100839 spin_unlock_irqrestore(sch->lock, flags);
Cornelia Huck6eff2082008-12-25 13:39:07 +0100840 /* Release initial device reference. */
841 put_device(&cdev->dev);
Cornelia Huck5fb6b852008-12-25 13:39:08 +0100842 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844out:
845 cdev->private->flags.recog_done = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 wake_up(&cdev->private->wait_q);
Cornelia Huck5fb6b852008-12-25 13:39:08 +0100847out_err:
Sebastian Otta2901562010-03-08 12:25:17 +0100848 if (adjust_init_count && atomic_dec_and_test(&ccw_device_init_count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 wake_up(&ccw_device_init_wq);
850}
851
Peter Oberparleiter37de53b2009-12-07 12:51:19 +0100852static void ccw_device_call_sch_unregister(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 struct subchannel *sch;
855
Peter Oberparleiter46fbe4e2008-10-10 21:33:05 +0200856 /* Get subchannel reference for local processing. */
857 if (!get_device(cdev->dev.parent))
858 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 sch = to_subchannel(cdev->dev.parent);
Cornelia Huck6ab48792006-07-12 16:39:50 +0200860 css_sch_device_unregister(sch);
Peter Oberparleiter46fbe4e2008-10-10 21:33:05 +0200861 /* Release subchannel reference for local processing. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 put_device(&sch->dev);
863}
864
865/*
866 * subchannel recognition done. Called from the state machine.
867 */
868void
869io_subchannel_recog_done(struct ccw_device *cdev)
870{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 if (css_init_done == 0) {
872 cdev->private->flags.recog_done = 1;
873 return;
874 }
875 switch (cdev->private->state) {
Sebastian Ott47593bf2009-03-31 19:16:05 +0200876 case DEV_STATE_BOXED:
877 /* Device did not respond in time. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 case DEV_STATE_NOT_OPER:
879 cdev->private->flags.recog_done = 1;
Peter Oberparleiter37de53b2009-12-07 12:51:19 +0100880 /* Remove device found not operational. */
881 ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 if (atomic_dec_and_test(&ccw_device_init_count))
883 wake_up(&ccw_device_init_wq);
884 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 case DEV_STATE_OFFLINE:
886 /*
887 * We can't register the device in interrupt context so
888 * we schedule a work item.
889 */
Peter Oberparleiter37de53b2009-12-07 12:51:19 +0100890 ccw_device_sched_todo(cdev, CDEV_TODO_REGISTER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 break;
892 }
893}
894
Peter Oberparleiter736b5db2009-12-07 12:51:21 +0100895static void io_subchannel_recog(struct ccw_device *cdev, struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 struct ccw_device_private *priv;
898
Cornelia Huck2ec22982006-12-08 15:54:26 +0100899 cdev->ccwlock = sch->lock;
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800900
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 /* Init private data. */
902 priv = cdev->private;
Cornelia Huck78964262006-10-11 15:31:38 +0200903 priv->dev_id.devno = sch->schib.pmcw.dev;
904 priv->dev_id.ssid = sch->schid.ssid;
905 priv->schid = sch->schid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 priv->state = DEV_STATE_NOT_OPER;
907 INIT_LIST_HEAD(&priv->cmb_list);
908 init_waitqueue_head(&priv->wait_q);
909 init_timer(&priv->timer);
910
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 /* Increase counter of devices currently in recognition. */
912 atomic_inc(&ccw_device_init_count);
913
914 /* Start async. device sensing. */
Cornelia Huck2ec22982006-12-08 15:54:26 +0100915 spin_lock_irq(sch->lock);
Peter Oberparleiter60e4dac2009-12-07 12:51:16 +0100916 sch_set_cdev(sch, cdev);
Peter Oberparleiter736b5db2009-12-07 12:51:21 +0100917 ccw_device_recognition(cdev);
Cornelia Huck2ec22982006-12-08 15:54:26 +0100918 spin_unlock_irq(sch->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919}
920
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100921static int ccw_device_move_to_sch(struct ccw_device *cdev,
922 struct subchannel *sch)
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100923{
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100924 struct subchannel *old_sch;
Sebastian Ott0c609fc2009-12-07 12:51:37 +0100925 int rc, old_enabled = 0;
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100926
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100927 old_sch = to_subchannel(cdev->dev.parent);
928 /* Obtain child reference for new parent. */
Cornelia Huck6eff2082008-12-25 13:39:07 +0100929 if (!get_device(&sch->dev))
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100930 return -ENODEV;
Sebastian Ott0c609fc2009-12-07 12:51:37 +0100931
932 if (!sch_is_pseudo_sch(old_sch)) {
933 spin_lock_irq(old_sch->lock);
934 old_enabled = old_sch->schib.pmcw.ena;
935 rc = 0;
936 if (old_enabled)
937 rc = cio_disable_subchannel(old_sch);
938 spin_unlock_irq(old_sch->lock);
939 if (rc == -EBUSY) {
940 /* Release child reference for new parent. */
941 put_device(&sch->dev);
942 return rc;
943 }
944 }
945
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100946 mutex_lock(&sch->reg_mutex);
Cornelia Huckffa6a702009-03-04 12:44:00 +0100947 rc = device_move(&cdev->dev, &sch->dev, DPM_ORDER_PARENT_BEFORE_DEV);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100948 mutex_unlock(&sch->reg_mutex);
949 if (rc) {
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100950 CIO_MSG_EVENT(0, "device_move(0.%x.%04x,0.%x.%04x)=%d\n",
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100951 cdev->private->dev_id.ssid,
952 cdev->private->dev_id.devno, sch->schid.ssid,
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100953 sch->schib.pmcw.dev, rc);
Sebastian Ott0c609fc2009-12-07 12:51:37 +0100954 if (old_enabled) {
955 /* Try to reenable the old subchannel. */
956 spin_lock_irq(old_sch->lock);
957 cio_enable_subchannel(old_sch, (u32)(addr_t)old_sch);
958 spin_unlock_irq(old_sch->lock);
959 }
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100960 /* Release child reference for new parent. */
Cornelia Huck6eff2082008-12-25 13:39:07 +0100961 put_device(&sch->dev);
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100962 return rc;
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100963 }
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100964 /* Clean up old subchannel. */
965 if (!sch_is_pseudo_sch(old_sch)) {
966 spin_lock_irq(old_sch->lock);
967 sch_set_cdev(old_sch, NULL);
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100968 spin_unlock_irq(old_sch->lock);
969 css_schedule_eval(old_sch->schid);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100970 }
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100971 /* Release child reference for old parent. */
972 put_device(&old_sch->dev);
973 /* Initialize new subchannel. */
974 spin_lock_irq(sch->lock);
975 cdev->private->schid = sch->schid;
976 cdev->ccwlock = sch->lock;
977 if (!sch_is_pseudo_sch(sch))
978 sch_set_cdev(sch, cdev);
979 spin_unlock_irq(sch->lock);
980 if (!sch_is_pseudo_sch(sch))
981 css_update_ssd_info(sch);
982 return 0;
983}
984
985static int ccw_device_move_to_orph(struct ccw_device *cdev)
986{
987 struct subchannel *sch = to_subchannel(cdev->dev.parent);
988 struct channel_subsystem *css = to_css(sch->dev.parent);
989
990 return ccw_device_move_to_sch(cdev, css->pseudo_subchannel);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100991}
992
Cornelia Huck602b20f2008-01-26 14:10:39 +0100993static void io_subchannel_irq(struct subchannel *sch)
994{
995 struct ccw_device *cdev;
996
Cornelia Huckdb6a6422008-01-26 14:10:46 +0100997 cdev = sch_get_cdev(sch);
Cornelia Huck602b20f2008-01-26 14:10:39 +0100998
Sebastian Ottefd986d2009-09-11 10:28:18 +0200999 CIO_TRACE_EVENT(6, "IRQ");
1000 CIO_TRACE_EVENT(6, dev_name(&sch->dev));
Cornelia Huck602b20f2008-01-26 14:10:39 +01001001 if (cdev)
1002 dev_fsm_event(cdev, DEV_EVENT_INTERRUPT);
1003}
1004
Sebastian Ott13952ec2008-12-25 13:39:13 +01001005void io_subchannel_init_config(struct subchannel *sch)
1006{
1007 memset(&sch->config, 0, sizeof(sch->config));
1008 sch->config.csense = 1;
Sebastian Ott13952ec2008-12-25 13:39:13 +01001009}
1010
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001011static void io_subchannel_init_fields(struct subchannel *sch)
1012{
1013 if (cio_is_console(sch->schid))
1014 sch->opm = 0xff;
1015 else
1016 sch->opm = chp_get_sch_opm(sch);
1017 sch->lpm = sch->schib.pmcw.pam & sch->opm;
Cornelia Huck3a3fc292008-07-14 09:58:58 +02001018 sch->isc = cio_is_console(sch->schid) ? CONSOLE_ISC : IO_SCH_ISC;
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001019
1020 CIO_MSG_EVENT(6, "Detected device %04x on subchannel 0.%x.%04X"
1021 " - PIM = %02X, PAM = %02X, POM = %02X\n",
1022 sch->schib.pmcw.dev, sch->schid.ssid,
1023 sch->schid.sch_no, sch->schib.pmcw.pim,
1024 sch->schib.pmcw.pam, sch->schib.pmcw.pom);
Sebastian Ott13952ec2008-12-25 13:39:13 +01001025
1026 io_subchannel_init_config(sch);
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001027}
1028
Cornelia Huck90ed2b62008-12-25 13:39:09 +01001029/*
1030 * Note: We always return 0 so that we bind to the device even on error.
1031 * This is needed so that our remove function is called on unregister.
1032 */
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001033static int io_subchannel_probe(struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034{
Sebastian Ottf92519e2011-03-15 17:08:27 +01001035 struct io_subchannel_private *io_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 struct ccw_device *cdev;
1037 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038
Peter Oberparleiter6d7c5af2009-10-14 12:43:50 +02001039 if (cio_is_console(sch->schid)) {
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001040 rc = sysfs_create_group(&sch->dev.kobj,
1041 &io_subchannel_attr_group);
1042 if (rc)
1043 CIO_MSG_EVENT(0, "Failed to create io subchannel "
1044 "attributes for subchannel "
1045 "0.%x.%04x (rc=%d)\n",
1046 sch->schid.ssid, sch->schid.sch_no, rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 /*
Peter Oberparleiter6d7c5af2009-10-14 12:43:50 +02001048 * The console subchannel already has an associated ccw_device.
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001049 * Throw the delayed uevent for the subchannel, register
Peter Oberparleiter6d7c5af2009-10-14 12:43:50 +02001050 * the ccw_device and exit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 */
Ming Leif67f1292009-03-01 21:10:49 +08001052 dev_set_uevent_suppress(&sch->dev, 0);
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001053 kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
Peter Oberparleiter6d7c5af2009-10-14 12:43:50 +02001054 cdev = sch_get_cdev(sch);
Cornelia Hucke1031782007-10-12 16:11:23 +02001055 cdev->dev.groups = ccwdev_attr_groups;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 device_initialize(&cdev->dev);
Sebastian Ott24a18722009-12-07 12:51:34 +01001057 cdev->private->flags.initialized = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 ccw_device_register(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 /*
1060 * Check if the device is already online. If it is
Cornelia Huck9cd67422008-12-25 13:39:06 +01001061 * the reference count needs to be corrected since we
1062 * didn't obtain a reference in ccw_device_set_online.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 */
1064 if (cdev->private->state != DEV_STATE_NOT_OPER &&
1065 cdev->private->state != DEV_STATE_OFFLINE &&
1066 cdev->private->state != DEV_STATE_BOXED)
1067 get_device(&cdev->dev);
1068 return 0;
1069 }
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001070 io_subchannel_init_fields(sch);
Sebastian Ottf444cc02008-12-25 13:39:14 +01001071 rc = cio_commit_config(sch);
1072 if (rc)
1073 goto out_schedule;
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001074 rc = sysfs_create_group(&sch->dev.kobj,
1075 &io_subchannel_attr_group);
1076 if (rc)
Cornelia Huck90ed2b62008-12-25 13:39:09 +01001077 goto out_schedule;
Cornelia Huckcd6b4f22008-01-26 14:10:43 +01001078 /* Allocate I/O subchannel private data. */
Sebastian Ottf92519e2011-03-15 17:08:27 +01001079 io_priv = kzalloc(sizeof(*io_priv), GFP_KERNEL | GFP_DMA);
1080 if (!io_priv)
Peter Oberparleiter48e4c382009-12-07 12:51:15 +01001081 goto out_schedule;
Sebastian Ottf92519e2011-03-15 17:08:27 +01001082
1083 set_io_private(sch, io_priv);
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001084 css_schedule_eval(sch->schid);
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001085 return 0;
Peter Oberparleiter48e4c382009-12-07 12:51:15 +01001086
Cornelia Huck90ed2b62008-12-25 13:39:09 +01001087out_schedule:
Peter Oberparleiter390935a2009-12-07 12:51:18 +01001088 spin_lock_irq(sch->lock);
1089 css_sched_sch_todo(sch, SCH_TODO_UNREG);
1090 spin_unlock_irq(sch->lock);
Cornelia Huck90ed2b62008-12-25 13:39:09 +01001091 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092}
1093
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094static int
Cornelia Huck8bbace72006-01-11 10:56:22 +01001095io_subchannel_remove (struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096{
Sebastian Ottf92519e2011-03-15 17:08:27 +01001097 struct io_subchannel_private *io_priv = to_io_private(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 struct ccw_device *cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001100 cdev = sch_get_cdev(sch);
1101 if (!cdev)
Peter Oberparleiter48e4c382009-12-07 12:51:15 +01001102 goto out_free;
Sebastian Ott6e9a0f62009-12-07 12:51:38 +01001103 io_subchannel_quiesce(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 /* Set ccw device to not operational and drop reference. */
Sebastian Ott7a8ad102009-12-07 12:51:39 +01001105 spin_lock_irq(cdev->ccwlock);
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001106 sch_set_cdev(sch, NULL);
Sebastian Ottf92519e2011-03-15 17:08:27 +01001107 set_io_private(sch, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 cdev->private->state = DEV_STATE_NOT_OPER;
Sebastian Ott7a8ad102009-12-07 12:51:39 +01001109 spin_unlock_irq(cdev->ccwlock);
Cornelia Huckef995162007-04-27 16:01:39 +02001110 ccw_device_unregister(cdev);
Peter Oberparleiter48e4c382009-12-07 12:51:15 +01001111out_free:
Sebastian Ottf92519e2011-03-15 17:08:27 +01001112 kfree(io_priv);
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001113 sysfs_remove_group(&sch->dev.kobj, &io_subchannel_attr_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 return 0;
1115}
1116
Cornelia Huck602b20f2008-01-26 14:10:39 +01001117static void io_subchannel_verify(struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118{
1119 struct ccw_device *cdev;
1120
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001121 cdev = sch_get_cdev(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 if (cdev)
1123 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1124}
1125
Cornelia Huckc820de32008-07-14 09:58:45 +02001126static void io_subchannel_terminate_path(struct subchannel *sch, u8 mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127{
1128 struct ccw_device *cdev;
1129
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001130 cdev = sch_get_cdev(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 if (!cdev)
1132 return;
Peter Oberparleiter4257aae2009-12-07 12:51:29 +01001133 if (cio_update_schib(sch))
1134 goto err;
1135 /* Check for I/O on path. */
1136 if (scsw_actl(&sch->schib.scsw) == 0 || sch->schib.pmcw.lpum != mask)
1137 goto out;
1138 if (cdev->private->state == DEV_STATE_ONLINE) {
1139 ccw_device_kill_io(cdev);
1140 goto out;
1141 }
1142 if (cio_clear(sch))
1143 goto err;
1144out:
1145 /* Trigger path verification. */
1146 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1147 return;
Cornelia Huckc820de32008-07-14 09:58:45 +02001148
Peter Oberparleiter4257aae2009-12-07 12:51:29 +01001149err:
1150 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
Cornelia Huckc820de32008-07-14 09:58:45 +02001151}
1152
Cornelia Huck99611f82008-07-14 09:59:02 +02001153static int io_subchannel_chp_event(struct subchannel *sch,
1154 struct chp_link *link, int event)
Cornelia Huckc820de32008-07-14 09:58:45 +02001155{
Sebastian Ott585b9542010-10-25 16:10:34 +02001156 struct ccw_device *cdev = sch_get_cdev(sch);
Cornelia Huckc820de32008-07-14 09:58:45 +02001157 int mask;
Cornelia Huckc820de32008-07-14 09:58:45 +02001158
Cornelia Huck99611f82008-07-14 09:59:02 +02001159 mask = chp_ssd_get_mask(&sch->ssd_info, link);
Cornelia Huckc820de32008-07-14 09:58:45 +02001160 if (!mask)
1161 return 0;
1162 switch (event) {
1163 case CHP_VARY_OFF:
1164 sch->opm &= ~mask;
1165 sch->lpm &= ~mask;
Sebastian Ott585b9542010-10-25 16:10:34 +02001166 if (cdev)
1167 cdev->private->path_gone_mask |= mask;
Cornelia Huckc820de32008-07-14 09:58:45 +02001168 io_subchannel_terminate_path(sch, mask);
1169 break;
1170 case CHP_VARY_ON:
1171 sch->opm |= mask;
1172 sch->lpm |= mask;
Sebastian Ott585b9542010-10-25 16:10:34 +02001173 if (cdev)
1174 cdev->private->path_new_mask |= mask;
Cornelia Huckc820de32008-07-14 09:58:45 +02001175 io_subchannel_verify(sch);
1176 break;
1177 case CHP_OFFLINE:
Sebastian Ottcdb912a2008-12-25 13:39:12 +01001178 if (cio_update_schib(sch))
Cornelia Huckc820de32008-07-14 09:58:45 +02001179 return -ENODEV;
Sebastian Ott585b9542010-10-25 16:10:34 +02001180 if (cdev)
1181 cdev->private->path_gone_mask |= mask;
Cornelia Huckc820de32008-07-14 09:58:45 +02001182 io_subchannel_terminate_path(sch, mask);
1183 break;
1184 case CHP_ONLINE:
Sebastian Ottcdb912a2008-12-25 13:39:12 +01001185 if (cio_update_schib(sch))
1186 return -ENODEV;
Cornelia Huckc820de32008-07-14 09:58:45 +02001187 sch->lpm |= mask & sch->opm;
Sebastian Ott585b9542010-10-25 16:10:34 +02001188 if (cdev)
1189 cdev->private->path_new_mask |= mask;
Cornelia Huckc820de32008-07-14 09:58:45 +02001190 io_subchannel_verify(sch);
1191 break;
1192 }
1193 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194}
1195
Sebastian Ott6e9a0f62009-12-07 12:51:38 +01001196static void io_subchannel_quiesce(struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 struct ccw_device *cdev;
1199 int ret;
1200
Sebastian Ott56e6b792009-12-07 12:51:35 +01001201 spin_lock_irq(sch->lock);
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001202 cdev = sch_get_cdev(sch);
Cornelia Hucka8237fc2006-01-06 00:19:21 -08001203 if (cio_is_console(sch->schid))
Sebastian Ott56e6b792009-12-07 12:51:35 +01001204 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 if (!sch->schib.pmcw.ena)
Sebastian Ott56e6b792009-12-07 12:51:35 +01001206 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 ret = cio_disable_subchannel(sch);
1208 if (ret != -EBUSY)
Sebastian Ott56e6b792009-12-07 12:51:35 +01001209 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 if (cdev->handler)
Sebastian Ott56e6b792009-12-07 12:51:35 +01001211 cdev->handler(cdev, cdev->private->intparm, ERR_PTR(-EIO));
1212 while (ret == -EBUSY) {
1213 cdev->private->state = DEV_STATE_QUIESCE;
Peter Oberparleiter376ae472010-10-25 16:10:44 +02001214 cdev->private->iretry = 255;
Sebastian Ott56e6b792009-12-07 12:51:35 +01001215 ret = ccw_device_cancel_halt_clear(cdev);
1216 if (ret == -EBUSY) {
1217 ccw_device_set_timeout(cdev, HZ/10);
1218 spin_unlock_irq(sch->lock);
1219 wait_event(cdev->private->wait_q,
1220 cdev->private->state != DEV_STATE_QUIESCE);
1221 spin_lock_irq(sch->lock);
1222 }
1223 ret = cio_disable_subchannel(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 }
Sebastian Ott56e6b792009-12-07 12:51:35 +01001225out_unlock:
1226 spin_unlock_irq(sch->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227}
1228
Sebastian Ott6e9a0f62009-12-07 12:51:38 +01001229static void io_subchannel_shutdown(struct subchannel *sch)
1230{
1231 io_subchannel_quiesce(sch);
1232}
1233
Cornelia Huckc820de32008-07-14 09:58:45 +02001234static int device_is_disconnected(struct ccw_device *cdev)
1235{
1236 if (!cdev)
1237 return 0;
1238 return (cdev->private->state == DEV_STATE_DISCONNECTED ||
1239 cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID);
1240}
1241
1242static int recovery_check(struct device *dev, void *data)
1243{
1244 struct ccw_device *cdev = to_ccwdev(dev);
1245 int *redo = data;
1246
1247 spin_lock_irq(cdev->ccwlock);
1248 switch (cdev->private->state) {
1249 case DEV_STATE_DISCONNECTED:
1250 CIO_MSG_EVENT(3, "recovery: trigger 0.%x.%04x\n",
1251 cdev->private->dev_id.ssid,
1252 cdev->private->dev_id.devno);
1253 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1254 *redo = 1;
1255 break;
1256 case DEV_STATE_DISCONNECTED_SENSE_ID:
1257 *redo = 1;
1258 break;
1259 }
1260 spin_unlock_irq(cdev->ccwlock);
1261
1262 return 0;
1263}
1264
1265static void recovery_work_func(struct work_struct *unused)
1266{
1267 int redo = 0;
1268
1269 bus_for_each_dev(&ccw_bus_type, NULL, &redo, recovery_check);
1270 if (redo) {
1271 spin_lock_irq(&recovery_lock);
1272 if (!timer_pending(&recovery_timer)) {
1273 if (recovery_phase < ARRAY_SIZE(recovery_delay) - 1)
1274 recovery_phase++;
1275 mod_timer(&recovery_timer, jiffies +
1276 recovery_delay[recovery_phase] * HZ);
1277 }
1278 spin_unlock_irq(&recovery_lock);
1279 } else
1280 CIO_MSG_EVENT(4, "recovery: end\n");
1281}
1282
1283static DECLARE_WORK(recovery_work, recovery_work_func);
1284
1285static void recovery_func(unsigned long data)
1286{
1287 /*
1288 * We can't do our recovery in softirq context and it's not
1289 * performance critical, so we schedule it.
1290 */
1291 schedule_work(&recovery_work);
1292}
1293
1294static void ccw_device_schedule_recovery(void)
1295{
1296 unsigned long flags;
1297
1298 CIO_MSG_EVENT(4, "recovery: schedule\n");
1299 spin_lock_irqsave(&recovery_lock, flags);
1300 if (!timer_pending(&recovery_timer) || (recovery_phase != 0)) {
1301 recovery_phase = 0;
1302 mod_timer(&recovery_timer, jiffies + recovery_delay[0] * HZ);
1303 }
1304 spin_unlock_irqrestore(&recovery_lock, flags);
1305}
1306
Peter Oberparleiterecf5d9e2008-10-10 21:33:06 +02001307static int purge_fn(struct device *dev, void *data)
1308{
1309 struct ccw_device *cdev = to_ccwdev(dev);
Peter Oberparleiter37de53b2009-12-07 12:51:19 +01001310 struct ccw_dev_id *id = &cdev->private->dev_id;
Peter Oberparleiterecf5d9e2008-10-10 21:33:06 +02001311
1312 spin_lock_irq(cdev->ccwlock);
Peter Oberparleiter37de53b2009-12-07 12:51:19 +01001313 if (is_blacklisted(id->ssid, id->devno) &&
1314 (cdev->private->state == DEV_STATE_OFFLINE)) {
1315 CIO_MSG_EVENT(3, "ccw: purging 0.%x.%04x\n", id->ssid,
1316 id->devno);
1317 ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
1318 }
Peter Oberparleiterecf5d9e2008-10-10 21:33:06 +02001319 spin_unlock_irq(cdev->ccwlock);
Peter Oberparleiterecf5d9e2008-10-10 21:33:06 +02001320 /* Abort loop in case of pending signal. */
1321 if (signal_pending(current))
1322 return -EINTR;
1323
1324 return 0;
1325}
1326
1327/**
1328 * ccw_purge_blacklisted - purge unused, blacklisted devices
1329 *
1330 * Unregister all ccw devices that are offline and on the blacklist.
1331 */
1332int ccw_purge_blacklisted(void)
1333{
1334 CIO_MSG_EVENT(2, "ccw: purging blacklisted devices\n");
1335 bus_for_each_dev(&ccw_bus_type, NULL, NULL, purge_fn);
1336 return 0;
1337}
1338
Peter Oberparleiter6afcc772009-10-06 10:34:02 +02001339void ccw_device_set_disconnected(struct ccw_device *cdev)
Cornelia Huckc820de32008-07-14 09:58:45 +02001340{
1341 if (!cdev)
1342 return;
1343 ccw_device_set_timeout(cdev, 0);
1344 cdev->private->flags.fake_irb = 0;
1345 cdev->private->state = DEV_STATE_DISCONNECTED;
1346 if (cdev->online)
1347 ccw_device_schedule_recovery();
1348}
1349
Peter Oberparleiter91c36912008-08-21 19:46:39 +02001350void ccw_device_set_notoper(struct ccw_device *cdev)
1351{
1352 struct subchannel *sch = to_subchannel(cdev->dev.parent);
1353
1354 CIO_TRACE_EVENT(2, "notoper");
Cornelia Huckb9d3aed2008-10-10 21:33:11 +02001355 CIO_TRACE_EVENT(2, dev_name(&sch->dev));
Peter Oberparleiter91c36912008-08-21 19:46:39 +02001356 ccw_device_set_timeout(cdev, 0);
1357 cio_disable_subchannel(sch);
1358 cdev->private->state = DEV_STATE_NOT_OPER;
1359}
1360
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001361enum io_sch_action {
1362 IO_SCH_UNREG,
1363 IO_SCH_ORPH_UNREG,
1364 IO_SCH_ATTACH,
1365 IO_SCH_UNREG_ATTACH,
1366 IO_SCH_ORPH_ATTACH,
1367 IO_SCH_REPROBE,
1368 IO_SCH_VERIFY,
1369 IO_SCH_DISC,
1370 IO_SCH_NOP,
1371};
1372
1373static enum io_sch_action sch_get_action(struct subchannel *sch)
Cornelia Huckc820de32008-07-14 09:58:45 +02001374{
Cornelia Huckc820de32008-07-14 09:58:45 +02001375 struct ccw_device *cdev;
1376
Cornelia Huckc820de32008-07-14 09:58:45 +02001377 cdev = sch_get_cdev(sch);
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001378 if (cio_update_schib(sch)) {
1379 /* Not operational. */
1380 if (!cdev)
1381 return IO_SCH_UNREG;
Sebastian Ott76e6fb42010-02-26 22:37:28 +01001382 if (ccw_device_notify(cdev, CIO_GONE) != NOTIFY_OK)
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001383 return IO_SCH_UNREG;
1384 return IO_SCH_ORPH_UNREG;
Cornelia Huckc820de32008-07-14 09:58:45 +02001385 }
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001386 /* Operational. */
1387 if (!cdev)
1388 return IO_SCH_ATTACH;
1389 if (sch->schib.pmcw.dev != cdev->private->dev_id.devno) {
Sebastian Ott76e6fb42010-02-26 22:37:28 +01001390 if (ccw_device_notify(cdev, CIO_GONE) != NOTIFY_OK)
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001391 return IO_SCH_UNREG_ATTACH;
1392 return IO_SCH_ORPH_ATTACH;
Cornelia Huckc820de32008-07-14 09:58:45 +02001393 }
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001394 if ((sch->schib.pmcw.pam & sch->opm) == 0) {
Sebastian Ott76e6fb42010-02-26 22:37:28 +01001395 if (ccw_device_notify(cdev, CIO_NO_PATH) != NOTIFY_OK)
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001396 return IO_SCH_UNREG;
1397 return IO_SCH_DISC;
Cornelia Huckc820de32008-07-14 09:58:45 +02001398 }
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001399 if (device_is_disconnected(cdev))
1400 return IO_SCH_REPROBE;
1401 if (cdev->online)
1402 return IO_SCH_VERIFY;
1403 return IO_SCH_NOP;
1404}
1405
1406/**
1407 * io_subchannel_sch_event - process subchannel event
1408 * @sch: subchannel
1409 * @process: non-zero if function is called in process context
1410 *
1411 * An unspecified event occurred for this subchannel. Adjust data according
1412 * to the current operational state of the subchannel and device. Return
1413 * zero when the event has been handled sufficiently or -EAGAIN when this
1414 * function should be called again in process context.
1415 */
1416static int io_subchannel_sch_event(struct subchannel *sch, int process)
1417{
1418 unsigned long flags;
1419 struct ccw_device *cdev;
1420 struct ccw_dev_id dev_id;
1421 enum io_sch_action action;
1422 int rc = -EAGAIN;
1423
1424 spin_lock_irqsave(sch->lock, flags);
1425 if (!device_is_registered(&sch->dev))
1426 goto out_unlock;
Peter Oberparleiter390935a2009-12-07 12:51:18 +01001427 if (work_pending(&sch->todo_work))
1428 goto out_unlock;
Peter Oberparleiter37de53b2009-12-07 12:51:19 +01001429 cdev = sch_get_cdev(sch);
1430 if (cdev && work_pending(&cdev->private->todo_work))
1431 goto out_unlock;
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001432 action = sch_get_action(sch);
1433 CIO_MSG_EVENT(2, "event: sch 0.%x.%04x, process=%d, action=%d\n",
1434 sch->schid.ssid, sch->schid.sch_no, process,
1435 action);
1436 /* Perform immediate actions while holding the lock. */
Cornelia Huckc820de32008-07-14 09:58:45 +02001437 switch (action) {
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001438 case IO_SCH_REPROBE:
1439 /* Trigger device recognition. */
Cornelia Huckc820de32008-07-14 09:58:45 +02001440 ccw_device_trigger_reprobe(cdev);
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001441 rc = 0;
1442 goto out_unlock;
1443 case IO_SCH_VERIFY:
Sebastian Ott0d01bb82010-02-26 22:37:29 +01001444 if (cdev->private->flags.resuming == 1) {
1445 if (cio_enable_subchannel(sch, (u32)(addr_t)sch)) {
1446 ccw_device_set_notoper(cdev);
1447 break;
1448 }
1449 }
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001450 /* Trigger path verification. */
1451 io_subchannel_verify(sch);
1452 rc = 0;
1453 goto out_unlock;
1454 case IO_SCH_DISC:
1455 ccw_device_set_disconnected(cdev);
1456 rc = 0;
1457 goto out_unlock;
1458 case IO_SCH_ORPH_UNREG:
1459 case IO_SCH_ORPH_ATTACH:
Peter Oberparleiter6afcc772009-10-06 10:34:02 +02001460 ccw_device_set_disconnected(cdev);
Peter Oberparleiter91c36912008-08-21 19:46:39 +02001461 break;
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001462 case IO_SCH_UNREG_ATTACH:
1463 case IO_SCH_UNREG:
Sebastian Ott16d2ce22010-11-10 10:05:53 +01001464 if (!cdev)
1465 break;
1466 if (cdev->private->state == DEV_STATE_SENSE_ID) {
1467 /*
1468 * Note: delayed work triggered by this event
1469 * and repeated calls to sch_event are synchronized
1470 * by the above check for work_pending(cdev).
1471 */
1472 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
1473 } else
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001474 ccw_device_set_notoper(cdev);
1475 break;
1476 case IO_SCH_NOP:
1477 rc = 0;
1478 goto out_unlock;
Cornelia Huckc820de32008-07-14 09:58:45 +02001479 default:
1480 break;
1481 }
1482 spin_unlock_irqrestore(sch->lock, flags);
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001483 /* All other actions require process context. */
1484 if (!process)
1485 goto out;
1486 /* Handle attached ccw device. */
1487 switch (action) {
1488 case IO_SCH_ORPH_UNREG:
1489 case IO_SCH_ORPH_ATTACH:
1490 /* Move ccw device to orphanage. */
1491 rc = ccw_device_move_to_orph(cdev);
1492 if (rc)
1493 goto out;
1494 break;
1495 case IO_SCH_UNREG_ATTACH:
Sebastian Ott74b61272010-10-25 16:10:26 +02001496 if (cdev->private->flags.resuming) {
1497 /* Device will be handled later. */
1498 rc = 0;
1499 goto out;
1500 }
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001501 /* Unregister ccw device. */
Sebastian Ott74b61272010-10-25 16:10:26 +02001502 ccw_device_unregister(cdev);
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001503 break;
1504 default:
1505 break;
1506 }
1507 /* Handle subchannel. */
1508 switch (action) {
1509 case IO_SCH_ORPH_UNREG:
1510 case IO_SCH_UNREG:
Sebastian Ott0d01bb82010-02-26 22:37:29 +01001511 if (!cdev || !cdev->private->flags.resuming)
1512 css_sch_device_unregister(sch);
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001513 break;
1514 case IO_SCH_ORPH_ATTACH:
1515 case IO_SCH_UNREG_ATTACH:
1516 case IO_SCH_ATTACH:
1517 dev_id.ssid = sch->schid.ssid;
1518 dev_id.devno = sch->schib.pmcw.dev;
1519 cdev = get_ccwdev_by_dev_id(&dev_id);
1520 if (!cdev) {
1521 sch_create_and_recog_new_device(sch);
1522 break;
1523 }
1524 rc = ccw_device_move_to_sch(cdev, sch);
1525 if (rc) {
1526 /* Release reference from get_ccwdev_by_dev_id() */
1527 put_device(&cdev->dev);
1528 goto out;
1529 }
1530 spin_lock_irqsave(sch->lock, flags);
1531 ccw_device_trigger_reprobe(cdev);
1532 spin_unlock_irqrestore(sch->lock, flags);
1533 /* Release reference from get_ccwdev_by_dev_id() */
1534 put_device(&cdev->dev);
1535 break;
1536 default:
1537 break;
1538 }
1539 return 0;
Cornelia Huckc820de32008-07-14 09:58:45 +02001540
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001541out_unlock:
1542 spin_unlock_irqrestore(sch->lock, flags);
1543out:
1544 return rc;
Cornelia Huckc820de32008-07-14 09:58:45 +02001545}
1546
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547#ifdef CONFIG_CCW_CONSOLE
1548static struct ccw_device console_cdev;
1549static struct ccw_device_private console_private;
1550static int console_cdev_in_use;
1551
Cornelia Huck2ec22982006-12-08 15:54:26 +01001552static DEFINE_SPINLOCK(ccw_console_lock);
1553
1554spinlock_t * cio_get_console_lock(void)
1555{
1556 return &ccw_console_lock;
1557}
1558
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001559static int ccw_device_console_enable(struct ccw_device *cdev,
1560 struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561{
Sebastian Ottf92519e2011-03-15 17:08:27 +01001562 struct io_subchannel_private *io_priv = cio_get_console_priv();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 int rc;
1564
Cornelia Huckcd6b4f22008-01-26 14:10:43 +01001565 /* Attach subchannel private data. */
Sebastian Ottf92519e2011-03-15 17:08:27 +01001566 memset(io_priv, 0, sizeof(*io_priv));
1567 set_io_private(sch, io_priv);
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001568 io_subchannel_init_fields(sch);
Sebastian Ottf444cc02008-12-25 13:39:14 +01001569 rc = cio_commit_config(sch);
1570 if (rc)
1571 return rc;
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001572 sch->driver = &io_subchannel_driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 /* Initialize the ccw_device structure. */
Heiko Carstens292888c2006-08-30 14:33:35 +02001574 cdev->dev.parent= &sch->dev;
Sebastian Ottffa8d2a2009-12-18 17:43:15 +01001575 sch_set_cdev(sch, cdev);
Peter Oberparleiter736b5db2009-12-07 12:51:21 +01001576 io_subchannel_recog(cdev, sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 /* Now wait for the async. recognition to come to an end. */
1578 spin_lock_irq(cdev->ccwlock);
1579 while (!dev_fsm_final_state(cdev))
1580 wait_cons_dev();
1581 rc = -EIO;
1582 if (cdev->private->state != DEV_STATE_OFFLINE)
1583 goto out_unlock;
1584 ccw_device_online(cdev);
1585 while (!dev_fsm_final_state(cdev))
1586 wait_cons_dev();
1587 if (cdev->private->state != DEV_STATE_ONLINE)
1588 goto out_unlock;
1589 rc = 0;
1590out_unlock:
1591 spin_unlock_irq(cdev->ccwlock);
Peter Oberparleiter736b5db2009-12-07 12:51:21 +01001592 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593}
1594
1595struct ccw_device *
1596ccw_device_probe_console(void)
1597{
1598 struct subchannel *sch;
1599 int ret;
1600
1601 if (xchg(&console_cdev_in_use, 1) != 0)
Peter Oberparleiter600b5d12006-02-01 03:06:35 -08001602 return ERR_PTR(-EBUSY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 sch = cio_probe_console();
1604 if (IS_ERR(sch)) {
1605 console_cdev_in_use = 0;
1606 return (void *) sch;
1607 }
1608 memset(&console_cdev, 0, sizeof(struct ccw_device));
1609 memset(&console_private, 0, sizeof(struct ccw_device_private));
1610 console_cdev.private = &console_private;
Martin Schwidefskyc1637532006-12-08 15:53:57 +01001611 console_private.cdev = &console_cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 ret = ccw_device_console_enable(&console_cdev, sch);
1613 if (ret) {
1614 cio_release_console();
1615 console_cdev_in_use = 0;
1616 return ERR_PTR(ret);
1617 }
1618 console_cdev.online = 1;
1619 return &console_cdev;
1620}
Cornelia Huck1f4e7ed2008-10-10 21:33:14 +02001621
Martin Schwidefsky66648452009-06-16 10:30:28 +02001622static int ccw_device_pm_restore(struct device *dev);
1623
1624int ccw_device_force_console(void)
1625{
1626 if (!console_cdev_in_use)
1627 return -ENODEV;
1628 return ccw_device_pm_restore(&console_cdev.dev);
1629}
1630EXPORT_SYMBOL_GPL(ccw_device_force_console);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631#endif
1632
1633/*
1634 * get ccw_device matching the busid, but only if owned by cdrv
1635 */
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001636static int
1637__ccwdev_check_busid(struct device *dev, void *id)
1638{
1639 char *bus_id;
1640
Cornelia Huck12975ae2006-10-11 15:31:47 +02001641 bus_id = id;
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001642
Kay Sievers98df67b2008-12-25 13:38:55 +01001643 return (strcmp(bus_id, dev_name(dev)) == 0);
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001644}
1645
1646
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +02001647/**
1648 * get_ccwdev_by_busid() - obtain device from a bus id
1649 * @cdrv: driver the device is owned by
1650 * @bus_id: bus id of the device to be searched
1651 *
1652 * This function searches all devices owned by @cdrv for a device with a bus
1653 * id matching @bus_id.
1654 * Returns:
1655 * If a match is found, its reference count of the found device is increased
1656 * and it is returned; else %NULL is returned.
1657 */
1658struct ccw_device *get_ccwdev_by_busid(struct ccw_driver *cdrv,
1659 const char *bus_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660{
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001661 struct device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 struct device_driver *drv;
1663
1664 drv = get_driver(&cdrv->driver);
1665 if (!drv)
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001666 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001668 dev = driver_find_device(drv, NULL, (void *)bus_id,
1669 __ccwdev_check_busid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 put_driver(drv);
1671
Heiko Carstensd2c993d2006-07-12 16:41:55 +02001672 return dev ? to_ccwdev(dev) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673}
1674
1675/************************** device driver handling ************************/
1676
1677/* This is the implementation of the ccw_driver class. The probe, remove
1678 * and release methods are initially very similar to the device_driver
1679 * implementations, with the difference that they have ccw_device
1680 * arguments.
1681 *
1682 * A ccw driver also contains the information that is needed for
1683 * device matching.
1684 */
1685static int
1686ccw_device_probe (struct device *dev)
1687{
1688 struct ccw_device *cdev = to_ccwdev(dev);
1689 struct ccw_driver *cdrv = to_ccwdrv(dev->driver);
1690 int ret;
1691
1692 cdev->drv = cdrv; /* to let the driver call _set_online */
1693
1694 ret = cdrv->probe ? cdrv->probe(cdev) : -ENODEV;
1695
1696 if (ret) {
Heiko Carstensd2c993d2006-07-12 16:41:55 +02001697 cdev->drv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 return ret;
1699 }
1700
1701 return 0;
1702}
1703
1704static int
1705ccw_device_remove (struct device *dev)
1706{
1707 struct ccw_device *cdev = to_ccwdev(dev);
1708 struct ccw_driver *cdrv = cdev->drv;
1709 int ret;
1710
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 if (cdrv->remove)
1712 cdrv->remove(cdev);
1713 if (cdev->online) {
1714 cdev->online = 0;
1715 spin_lock_irq(cdev->ccwlock);
1716 ret = ccw_device_offline(cdev);
1717 spin_unlock_irq(cdev->ccwlock);
1718 if (ret == 0)
1719 wait_event(cdev->private->wait_q,
1720 dev_fsm_final_state(cdev));
1721 else
Michael Ernst139b83d2008-05-07 09:22:54 +02001722 CIO_MSG_EVENT(0, "ccw_device_offline returned %d, "
Cornelia Hucke556bbb2007-07-27 12:29:19 +02001723 "device 0.%x.%04x\n",
1724 ret, cdev->private->dev_id.ssid,
1725 cdev->private->dev_id.devno);
Cornelia Huck9cd67422008-12-25 13:39:06 +01001726 /* Give up reference obtained in ccw_device_set_online(). */
1727 put_device(&cdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 }
1729 ccw_device_set_timeout(cdev, 0);
Heiko Carstensd2c993d2006-07-12 16:41:55 +02001730 cdev->drv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 return 0;
1732}
1733
Cornelia Huck958974fb2007-10-12 16:11:21 +02001734static void ccw_device_shutdown(struct device *dev)
1735{
1736 struct ccw_device *cdev;
1737
1738 cdev = to_ccwdev(dev);
1739 if (cdev->drv && cdev->drv->shutdown)
1740 cdev->drv->shutdown(cdev);
Cornelia Huck1842f2b2007-10-12 16:11:22 +02001741 disable_cmf(cdev);
Cornelia Huck958974fb2007-10-12 16:11:21 +02001742}
1743
Sebastian Ott823d4942009-06-16 10:30:20 +02001744static int ccw_device_pm_prepare(struct device *dev)
1745{
1746 struct ccw_device *cdev = to_ccwdev(dev);
1747
Peter Oberparleiter37de53b2009-12-07 12:51:19 +01001748 if (work_pending(&cdev->private->todo_work))
Sebastian Ott823d4942009-06-16 10:30:20 +02001749 return -EAGAIN;
1750 /* Fail while device is being set online/offline. */
1751 if (atomic_read(&cdev->private->onoff))
1752 return -EAGAIN;
1753
1754 if (cdev->online && cdev->drv && cdev->drv->prepare)
1755 return cdev->drv->prepare(cdev);
1756
1757 return 0;
1758}
1759
1760static void ccw_device_pm_complete(struct device *dev)
1761{
1762 struct ccw_device *cdev = to_ccwdev(dev);
1763
1764 if (cdev->online && cdev->drv && cdev->drv->complete)
1765 cdev->drv->complete(cdev);
1766}
1767
1768static int ccw_device_pm_freeze(struct device *dev)
1769{
1770 struct ccw_device *cdev = to_ccwdev(dev);
1771 struct subchannel *sch = to_subchannel(cdev->dev.parent);
1772 int ret, cm_enabled;
1773
1774 /* Fail suspend while device is in transistional state. */
1775 if (!dev_fsm_final_state(cdev))
1776 return -EAGAIN;
1777 if (!cdev->online)
1778 return 0;
1779 if (cdev->drv && cdev->drv->freeze) {
1780 ret = cdev->drv->freeze(cdev);
1781 if (ret)
1782 return ret;
1783 }
1784
1785 spin_lock_irq(sch->lock);
1786 cm_enabled = cdev->private->cmb != NULL;
1787 spin_unlock_irq(sch->lock);
1788 if (cm_enabled) {
1789 /* Don't have the css write on memory. */
1790 ret = ccw_set_cmf(cdev, 0);
1791 if (ret)
1792 return ret;
1793 }
1794 /* From here on, disallow device driver I/O. */
1795 spin_lock_irq(sch->lock);
1796 ret = cio_disable_subchannel(sch);
1797 spin_unlock_irq(sch->lock);
1798
1799 return ret;
1800}
1801
1802static int ccw_device_pm_thaw(struct device *dev)
1803{
1804 struct ccw_device *cdev = to_ccwdev(dev);
1805 struct subchannel *sch = to_subchannel(cdev->dev.parent);
1806 int ret, cm_enabled;
1807
1808 if (!cdev->online)
1809 return 0;
1810
1811 spin_lock_irq(sch->lock);
1812 /* Allow device driver I/O again. */
1813 ret = cio_enable_subchannel(sch, (u32)(addr_t)sch);
1814 cm_enabled = cdev->private->cmb != NULL;
1815 spin_unlock_irq(sch->lock);
1816 if (ret)
1817 return ret;
1818
1819 if (cm_enabled) {
1820 ret = ccw_set_cmf(cdev, 1);
1821 if (ret)
1822 return ret;
1823 }
1824
1825 if (cdev->drv && cdev->drv->thaw)
1826 ret = cdev->drv->thaw(cdev);
1827
1828 return ret;
1829}
1830
1831static void __ccw_device_pm_restore(struct ccw_device *cdev)
1832{
1833 struct subchannel *sch = to_subchannel(cdev->dev.parent);
Sebastian Ott823d4942009-06-16 10:30:20 +02001834
Sebastian Ott0d01bb82010-02-26 22:37:29 +01001835 spin_lock_irq(sch->lock);
1836 if (cio_is_console(sch->schid)) {
1837 cio_enable_subchannel(sch, (u32)(addr_t)sch);
1838 goto out_unlock;
1839 }
Sebastian Ott823d4942009-06-16 10:30:20 +02001840 /*
1841 * While we were sleeping, devices may have gone or become
1842 * available again. Kick re-detection.
1843 */
Sebastian Ott823d4942009-06-16 10:30:20 +02001844 cdev->private->flags.resuming = 1;
Sebastian Ottb17295e2011-01-12 09:55:10 +01001845 cdev->private->path_new_mask = LPM_ANYPATH;
Sebastian Ott0d01bb82010-02-26 22:37:29 +01001846 css_schedule_eval(sch->schid);
1847 spin_unlock_irq(sch->lock);
1848 css_complete_work();
1849
1850 /* cdev may have been moved to a different subchannel. */
1851 sch = to_subchannel(cdev->dev.parent);
1852 spin_lock_irq(sch->lock);
1853 if (cdev->private->state != DEV_STATE_ONLINE &&
1854 cdev->private->state != DEV_STATE_OFFLINE)
1855 goto out_unlock;
1856
Peter Oberparleiter736b5db2009-12-07 12:51:21 +01001857 ccw_device_recognition(cdev);
Sebastian Ott823d4942009-06-16 10:30:20 +02001858 spin_unlock_irq(sch->lock);
Sebastian Ott823d4942009-06-16 10:30:20 +02001859 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev) ||
1860 cdev->private->state == DEV_STATE_DISCONNECTED);
Sebastian Ott0d01bb82010-02-26 22:37:29 +01001861 spin_lock_irq(sch->lock);
1862
1863out_unlock:
Sebastian Ott823d4942009-06-16 10:30:20 +02001864 cdev->private->flags.resuming = 0;
Sebastian Ott0d01bb82010-02-26 22:37:29 +01001865 spin_unlock_irq(sch->lock);
Sebastian Ott823d4942009-06-16 10:30:20 +02001866}
1867
1868static int resume_handle_boxed(struct ccw_device *cdev)
1869{
1870 cdev->private->state = DEV_STATE_BOXED;
Sebastian Ott76e6fb42010-02-26 22:37:28 +01001871 if (ccw_device_notify(cdev, CIO_BOXED) == NOTIFY_OK)
Sebastian Ott823d4942009-06-16 10:30:20 +02001872 return 0;
Peter Oberparleiter37de53b2009-12-07 12:51:19 +01001873 ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
Sebastian Ott823d4942009-06-16 10:30:20 +02001874 return -ENODEV;
1875}
1876
1877static int resume_handle_disc(struct ccw_device *cdev)
1878{
1879 cdev->private->state = DEV_STATE_DISCONNECTED;
Sebastian Ott76e6fb42010-02-26 22:37:28 +01001880 if (ccw_device_notify(cdev, CIO_GONE) == NOTIFY_OK)
Sebastian Ott823d4942009-06-16 10:30:20 +02001881 return 0;
Peter Oberparleiter37de53b2009-12-07 12:51:19 +01001882 ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
Sebastian Ott823d4942009-06-16 10:30:20 +02001883 return -ENODEV;
1884}
1885
1886static int ccw_device_pm_restore(struct device *dev)
1887{
1888 struct ccw_device *cdev = to_ccwdev(dev);
Sebastian Ott0d01bb82010-02-26 22:37:29 +01001889 struct subchannel *sch;
1890 int ret = 0;
Sebastian Ott823d4942009-06-16 10:30:20 +02001891
1892 __ccw_device_pm_restore(cdev);
Sebastian Ott0d01bb82010-02-26 22:37:29 +01001893 sch = to_subchannel(cdev->dev.parent);
Sebastian Ott823d4942009-06-16 10:30:20 +02001894 spin_lock_irq(sch->lock);
Sebastian Ott0d01bb82010-02-26 22:37:29 +01001895 if (cio_is_console(sch->schid))
Sebastian Ott823d4942009-06-16 10:30:20 +02001896 goto out_restore;
Sebastian Ott0d01bb82010-02-26 22:37:29 +01001897
Sebastian Ott823d4942009-06-16 10:30:20 +02001898 /* check recognition results */
1899 switch (cdev->private->state) {
1900 case DEV_STATE_OFFLINE:
Sebastian Ott0d01bb82010-02-26 22:37:29 +01001901 case DEV_STATE_ONLINE:
1902 cdev->private->flags.donotify = 0;
Sebastian Ott823d4942009-06-16 10:30:20 +02001903 break;
1904 case DEV_STATE_BOXED:
1905 ret = resume_handle_boxed(cdev);
Sebastian Ott823d4942009-06-16 10:30:20 +02001906 if (ret)
Sebastian Ott0d01bb82010-02-26 22:37:29 +01001907 goto out_unlock;
Sebastian Ott823d4942009-06-16 10:30:20 +02001908 goto out_restore;
Sebastian Ott823d4942009-06-16 10:30:20 +02001909 default:
Sebastian Ott0d01bb82010-02-26 22:37:29 +01001910 ret = resume_handle_disc(cdev);
1911 if (ret)
1912 goto out_unlock;
1913 goto out_restore;
Sebastian Ott823d4942009-06-16 10:30:20 +02001914 }
1915 /* check if the device type has changed */
1916 if (!ccw_device_test_sense_data(cdev)) {
1917 ccw_device_update_sense_data(cdev);
Peter Oberparleiter37de53b2009-12-07 12:51:19 +01001918 ccw_device_sched_todo(cdev, CDEV_TODO_REBIND);
Sebastian Ott823d4942009-06-16 10:30:20 +02001919 ret = -ENODEV;
1920 goto out_unlock;
1921 }
Sebastian Ott0d01bb82010-02-26 22:37:29 +01001922 if (!cdev->online)
1923 goto out_unlock;
1924
1925 if (ccw_device_online(cdev)) {
1926 ret = resume_handle_disc(cdev);
1927 if (ret)
1928 goto out_unlock;
1929 goto out_restore;
1930 }
1931 spin_unlock_irq(sch->lock);
1932 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
1933 spin_lock_irq(sch->lock);
1934
1935 if (ccw_device_notify(cdev, CIO_OPER) == NOTIFY_BAD) {
1936 ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
1937 ret = -ENODEV;
Sebastian Ott823d4942009-06-16 10:30:20 +02001938 goto out_unlock;
1939 }
Sebastian Ott823d4942009-06-16 10:30:20 +02001940
Sebastian Ott0d01bb82010-02-26 22:37:29 +01001941 /* reenable cmf, if needed */
1942 if (cdev->private->cmb) {
1943 spin_unlock_irq(sch->lock);
Sebastian Ott823d4942009-06-16 10:30:20 +02001944 ret = ccw_set_cmf(cdev, 1);
Sebastian Ott0d01bb82010-02-26 22:37:29 +01001945 spin_lock_irq(sch->lock);
Sebastian Ott823d4942009-06-16 10:30:20 +02001946 if (ret) {
Sebastian Ottf0148242009-09-11 10:28:23 +02001947 CIO_MSG_EVENT(2, "resume: cdev 0.%x.%04x: cmf failed "
1948 "(rc=%d)\n", cdev->private->dev_id.ssid,
1949 cdev->private->dev_id.devno, ret);
Sebastian Ott823d4942009-06-16 10:30:20 +02001950 ret = 0;
1951 }
1952 }
1953
1954out_restore:
Sebastian Ott0d01bb82010-02-26 22:37:29 +01001955 spin_unlock_irq(sch->lock);
Sebastian Ott823d4942009-06-16 10:30:20 +02001956 if (cdev->online && cdev->drv && cdev->drv->restore)
1957 ret = cdev->drv->restore(cdev);
Sebastian Ott823d4942009-06-16 10:30:20 +02001958 return ret;
1959
Sebastian Ott823d4942009-06-16 10:30:20 +02001960out_unlock:
1961 spin_unlock_irq(sch->lock);
1962 return ret;
1963}
1964
Alexey Dobriyan47145212009-12-14 18:00:08 -08001965static const struct dev_pm_ops ccw_pm_ops = {
Sebastian Ott823d4942009-06-16 10:30:20 +02001966 .prepare = ccw_device_pm_prepare,
1967 .complete = ccw_device_pm_complete,
1968 .freeze = ccw_device_pm_freeze,
1969 .thaw = ccw_device_pm_thaw,
1970 .restore = ccw_device_pm_restore,
1971};
1972
Cornelia Huck8bbace72006-01-11 10:56:22 +01001973struct bus_type ccw_bus_type = {
1974 .name = "ccw",
1975 .match = ccw_bus_match,
1976 .uevent = ccw_uevent,
1977 .probe = ccw_device_probe,
1978 .remove = ccw_device_remove,
Cornelia Huck958974fb2007-10-12 16:11:21 +02001979 .shutdown = ccw_device_shutdown,
Sebastian Ott823d4942009-06-16 10:30:20 +02001980 .pm = &ccw_pm_ops,
Cornelia Huck8bbace72006-01-11 10:56:22 +01001981};
1982
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +02001983/**
1984 * ccw_driver_register() - register a ccw driver
1985 * @cdriver: driver to be registered
1986 *
1987 * This function is mainly a wrapper around driver_register().
1988 * Returns:
1989 * %0 on success and a negative error value on failure.
1990 */
1991int ccw_driver_register(struct ccw_driver *cdriver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992{
1993 struct device_driver *drv = &cdriver->driver;
1994
1995 drv->bus = &ccw_bus_type;
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);