blob: 4093adc12f2cfd999045a66bfbd217e7d371dc03 [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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/module.h>
11#include <linux/init.h>
12#include <linux/spinlock.h>
13#include <linux/errno.h>
14#include <linux/err.h>
15#include <linux/slab.h>
16#include <linux/list.h>
17#include <linux/device.h>
18#include <linux/workqueue.h>
Peter Oberparleiter90ab1332008-01-26 14:10:52 +010019#include <linux/timer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
21#include <asm/ccwdev.h>
22#include <asm/cio.h>
Tim Schmielau4e57b682005-10-30 15:03:48 -080023#include <asm/param.h> /* HZ */
Cornelia Huck1842f2b2007-10-12 16:11:22 +020024#include <asm/cmb.h>
Cornelia Huck3a3fc292008-07-14 09:58:58 +020025#include <asm/isc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +020027#include "chp.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include "cio.h"
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +010029#include "cio_debug.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include "css.h"
31#include "device.h"
32#include "ioasm.h"
Cornelia Huckcd6b4f22008-01-26 14:10:43 +010033#include "io_sch.h"
Peter Oberparleiterecf5d9e2008-10-10 21:33:06 +020034#include "blacklist.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Peter Oberparleiter90ab1332008-01-26 14:10:52 +010036static struct timer_list recovery_timer;
Cornelia Huck486d0a02008-02-19 15:29:23 +010037static DEFINE_SPINLOCK(recovery_lock);
Peter Oberparleiter90ab1332008-01-26 14:10:52 +010038static int recovery_phase;
39static const unsigned long recovery_delay[] = { 3, 30, 300 };
40
Linus Torvalds1da177e2005-04-16 15:20:36 -070041/******************* bus type handling ***********************/
42
43/* The Linux driver model distinguishes between a bus type and
44 * the bus itself. Of course we only have one channel
45 * subsystem driver and one channel system per machine, but
46 * we still use the abstraction. T.R. says it's a good idea. */
47static int
48ccw_bus_match (struct device * dev, struct device_driver * drv)
49{
50 struct ccw_device *cdev = to_ccwdev(dev);
51 struct ccw_driver *cdrv = to_ccwdrv(drv);
52 const struct ccw_device_id *ids = cdrv->ids, *found;
53
54 if (!ids)
55 return 0;
56
57 found = ccw_device_id_match(ids, &cdev->id);
58 if (!found)
59 return 0;
60
61 cdev->id.driver_info = found->driver_info;
62
63 return 1;
64}
65
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +020066/* Store modalias string delimited by prefix/suffix string into buffer with
67 * specified size. Return length of resulting string (excluding trailing '\0')
68 * even if string doesn't fit buffer (snprintf semantics). */
Cornelia Huckcfbe9bb2007-04-27 16:01:32 +020069static int snprint_alias(char *buf, size_t size,
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +020070 struct ccw_device_id *id, const char *suffix)
71{
72 int len;
73
Cornelia Huckcfbe9bb2007-04-27 16:01:32 +020074 len = snprintf(buf, size, "ccw:t%04Xm%02X", id->cu_type, id->cu_model);
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +020075 if (len > size)
76 return len;
77 buf += len;
78 size -= len;
79
80 if (id->dev_type != 0)
81 len += snprintf(buf, size, "dt%04Xdm%02X%s", id->dev_type,
82 id->dev_model, suffix);
83 else
84 len += snprintf(buf, size, "dtdm%s", suffix);
85
86 return len;
87}
88
89/* Set up environment variables for ccw device uevent. Return 0 on success,
90 * non-zero otherwise. */
Kay Sievers7eff2e72007-08-14 15:15:12 +020091static int ccw_uevent(struct device *dev, struct kobj_uevent_env *env)
Linus Torvalds1da177e2005-04-16 15:20:36 -070092{
93 struct ccw_device *cdev = to_ccwdev(dev);
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +020094 struct ccw_device_id *id = &(cdev->id);
Cornelia Huckcfbe9bb2007-04-27 16:01:32 +020095 int ret;
96 char modalias_buf[30];
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +020098 /* CU_TYPE= */
Kay Sievers7eff2e72007-08-14 15:15:12 +020099 ret = add_uevent_var(env, "CU_TYPE=%04X", id->cu_type);
Cornelia Huckcfbe9bb2007-04-27 16:01:32 +0200100 if (ret)
101 return ret;
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +0200102
103 /* CU_MODEL= */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200104 ret = add_uevent_var(env, "CU_MODEL=%02X", id->cu_model);
Cornelia Huckcfbe9bb2007-04-27 16:01:32 +0200105 if (ret)
106 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
108 /* The next two can be zero, that's ok for us */
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +0200109 /* DEV_TYPE= */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200110 ret = add_uevent_var(env, "DEV_TYPE=%04X", id->dev_type);
Cornelia Huckcfbe9bb2007-04-27 16:01:32 +0200111 if (ret)
112 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +0200114 /* DEV_MODEL= */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200115 ret = add_uevent_var(env, "DEV_MODEL=%02X", id->dev_model);
Cornelia Huckcfbe9bb2007-04-27 16:01:32 +0200116 if (ret)
117 return ret;
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +0200118
119 /* MODALIAS= */
Cornelia Huckcfbe9bb2007-04-27 16:01:32 +0200120 snprint_alias(modalias_buf, sizeof(modalias_buf), id, "");
Kay Sievers7eff2e72007-08-14 15:15:12 +0200121 ret = add_uevent_var(env, "MODALIAS=%s", modalias_buf);
122 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123}
124
Cornelia Huck8bbace72006-01-11 10:56:22 +0100125struct bus_type ccw_bus_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
Cornelia Huck602b20f2008-01-26 14:10:39 +0100127static void io_subchannel_irq(struct subchannel *);
128static int io_subchannel_probe(struct subchannel *);
129static int io_subchannel_remove(struct subchannel *);
Cornelia Huck8bbace72006-01-11 10:56:22 +0100130static void io_subchannel_shutdown(struct subchannel *);
Cornelia Huckc820de32008-07-14 09:58:45 +0200131static int io_subchannel_sch_event(struct subchannel *, int);
Cornelia Huck99611f82008-07-14 09:59:02 +0200132static int io_subchannel_chp_event(struct subchannel *, struct chp_link *,
133 int);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
Cornelia Huckf08adc02008-07-14 09:59:03 +0200135static struct css_device_id io_subchannel_ids[] = {
136 { .match_flags = 0x1, .type = SUBCHANNEL_TYPE_IO, },
137 { /* end of list */ },
138};
139MODULE_DEVICE_TABLE(css, io_subchannel_ids);
140
Cornelia Huck93a27592009-06-16 10:30:23 +0200141static int io_subchannel_prepare(struct subchannel *sch)
142{
143 struct ccw_device *cdev;
144 /*
145 * Don't allow suspend while a ccw device registration
146 * is still outstanding.
147 */
148 cdev = sch_get_cdev(sch);
149 if (cdev && !device_is_registered(&cdev->dev))
150 return -EAGAIN;
151 return 0;
152}
153
Cornelia Huckf7e5d672007-05-10 15:45:43 +0200154static struct css_driver io_subchannel_driver = {
Cornelia Huck4beee642008-01-26 14:10:47 +0100155 .owner = THIS_MODULE,
Cornelia Huckf08adc02008-07-14 09:59:03 +0200156 .subchannel_type = io_subchannel_ids,
Cornelia Huck25b7bb52008-01-26 14:10:41 +0100157 .name = "io_subchannel",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 .irq = io_subchannel_irq,
Cornelia Huckc820de32008-07-14 09:58:45 +0200159 .sch_event = io_subchannel_sch_event,
160 .chp_event = io_subchannel_chp_event,
Cornelia Huck8bbace72006-01-11 10:56:22 +0100161 .probe = io_subchannel_probe,
162 .remove = io_subchannel_remove,
163 .shutdown = io_subchannel_shutdown,
Cornelia Huck93a27592009-06-16 10:30:23 +0200164 .prepare = io_subchannel_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165};
166
167struct workqueue_struct *ccw_device_work;
Peter Oberparleiter40154b82006-06-29 14:57:03 +0200168wait_queue_head_t ccw_device_init_wq;
169atomic_t ccw_device_init_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
Peter Oberparleiter90ab1332008-01-26 14:10:52 +0100171static void recovery_func(unsigned long data);
172
Sebastian Ott2f176442009-09-22 22:58:33 +0200173int __init io_subchannel_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174{
175 int ret;
176
177 init_waitqueue_head(&ccw_device_init_wq);
178 atomic_set(&ccw_device_init_count, 0);
Peter Oberparleiter90ab1332008-01-26 14:10:52 +0100179 setup_timer(&recovery_timer, recovery_func, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
181 ccw_device_work = create_singlethread_workqueue("cio");
182 if (!ccw_device_work)
Sebastian Ott2f176442009-09-22 22:58:33 +0200183 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 slow_path_wq = create_singlethread_workqueue("kslowcrw");
185 if (!slow_path_wq) {
Sebastian Ott2f176442009-09-22 22:58:33 +0200186 ret = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 goto out_err;
188 }
189 if ((ret = bus_register (&ccw_bus_type)))
190 goto out_err;
191
Cornelia Huck25b7bb52008-01-26 14:10:41 +0100192 ret = css_driver_register(&io_subchannel_driver);
193 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 goto out_err;
195
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 return 0;
197out_err:
198 if (ccw_device_work)
199 destroy_workqueue(ccw_device_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 if (slow_path_wq)
201 destroy_workqueue(slow_path_wq);
202 return ret;
203}
204
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
206/************************ device handling **************************/
207
208/*
209 * A ccw_device has some interfaces in sysfs in addition to the
210 * standard ones.
211 * The following entries are designed to export the information which
212 * resided in 2.4 in /proc/subchannels. Subchannel and device number
213 * are obvious, so they don't have an entry :)
214 * TODO: Split chpids and pimpampom up? Where is "in use" in the tree?
215 */
216static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400217chpids_show (struct device * dev, struct device_attribute *attr, char * buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218{
219 struct subchannel *sch = to_subchannel(dev);
Peter Oberparleiter7ad6a242007-04-27 16:01:35 +0200220 struct chsc_ssd_info *ssd = &sch->ssd_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 ssize_t ret = 0;
222 int chp;
Peter Oberparleiter7ad6a242007-04-27 16:01:35 +0200223 int mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
Peter Oberparleiter7ad6a242007-04-27 16:01:35 +0200225 for (chp = 0; chp < 8; chp++) {
226 mask = 0x80 >> chp;
227 if (ssd->path_mask & mask)
228 ret += sprintf(buf + ret, "%02x ", ssd->chpid[chp].id);
229 else
230 ret += sprintf(buf + ret, "00 ");
231 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 ret += sprintf (buf+ret, "\n");
233 return min((ssize_t)PAGE_SIZE, ret);
234}
235
236static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400237pimpampom_show (struct device * dev, struct device_attribute *attr, char * buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238{
239 struct subchannel *sch = to_subchannel(dev);
240 struct pmcw *pmcw = &sch->schib.pmcw;
241
242 return sprintf (buf, "%02x %02x %02x\n",
243 pmcw->pim, pmcw->pam, pmcw->pom);
244}
245
246static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400247devtype_show (struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248{
249 struct ccw_device *cdev = to_ccwdev(dev);
250 struct ccw_device_id *id = &(cdev->id);
251
252 if (id->dev_type != 0)
253 return sprintf(buf, "%04x/%02x\n",
254 id->dev_type, id->dev_model);
255 else
256 return sprintf(buf, "n/a\n");
257}
258
259static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400260cutype_show (struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261{
262 struct ccw_device *cdev = to_ccwdev(dev);
263 struct ccw_device_id *id = &(cdev->id);
264
265 return sprintf(buf, "%04x/%02x\n",
266 id->cu_type, id->cu_model);
267}
268
269static ssize_t
Bastian Blankf1fc78a2005-10-30 15:00:12 -0800270modalias_show (struct device *dev, struct device_attribute *attr, char *buf)
271{
272 struct ccw_device *cdev = to_ccwdev(dev);
273 struct ccw_device_id *id = &(cdev->id);
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +0200274 int len;
Bastian Blankf1fc78a2005-10-30 15:00:12 -0800275
Cornelia Huck086a6c62007-07-17 13:36:08 +0200276 len = snprint_alias(buf, PAGE_SIZE, id, "\n");
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +0200277
278 return len > PAGE_SIZE ? PAGE_SIZE : len;
Bastian Blankf1fc78a2005-10-30 15:00:12 -0800279}
280
281static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400282online_show (struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283{
284 struct ccw_device *cdev = to_ccwdev(dev);
285
286 return sprintf(buf, cdev->online ? "1\n" : "0\n");
287}
288
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100289int ccw_device_is_orphan(struct ccw_device *cdev)
290{
291 return sch_is_pseudo_sch(to_subchannel(cdev->dev.parent));
292}
293
Cornelia Huckef995162007-04-27 16:01:39 +0200294static void ccw_device_unregister(struct ccw_device *cdev)
Cornelia Huck7674da72006-12-08 15:54:21 +0100295{
Sebastian Ott3b554a12009-09-11 10:28:26 +0200296 if (test_and_clear_bit(1, &cdev->private->registered)) {
Cornelia Huckef995162007-04-27 16:01:39 +0200297 device_del(&cdev->dev);
Sebastian Ott3b554a12009-09-11 10:28:26 +0200298 /* Release reference from device_initialize(). */
299 put_device(&cdev->dev);
300 }
Cornelia Huck7674da72006-12-08 15:54:21 +0100301}
302
Peter Oberparleiter46fbe4e2008-10-10 21:33:05 +0200303static void ccw_device_remove_orphan_cb(struct work_struct *work)
Cornelia Huck59a8a6e2007-05-31 17:38:06 +0200304{
Peter Oberparleiter46fbe4e2008-10-10 21:33:05 +0200305 struct ccw_device_private *priv;
306 struct ccw_device *cdev;
Cornelia Huck59a8a6e2007-05-31 17:38:06 +0200307
Peter Oberparleiter46fbe4e2008-10-10 21:33:05 +0200308 priv = container_of(work, struct ccw_device_private, kick_work);
309 cdev = priv->cdev;
Cornelia Huck59a8a6e2007-05-31 17:38:06 +0200310 ccw_device_unregister(cdev);
Peter Oberparleiter46fbe4e2008-10-10 21:33:05 +0200311 /* Release cdev reference for workqueue processing. */
312 put_device(&cdev->dev);
Cornelia Huck59a8a6e2007-05-31 17:38:06 +0200313}
314
Cornelia Huck59a8a6e2007-05-31 17:38:06 +0200315static void
316ccw_device_remove_disconnected(struct ccw_device *cdev)
317{
318 unsigned long flags;
Cornelia Huck59a8a6e2007-05-31 17:38:06 +0200319
320 /*
321 * Forced offline in disconnected state means
322 * 'throw away device'.
323 */
324 if (ccw_device_is_orphan(cdev)) {
325 /*
326 * Deregister ccw device.
327 * Unfortunately, we cannot do this directly from the
328 * attribute method.
329 */
Sebastian Ottbe7a2dd2009-09-11 10:28:20 +0200330 /* Get cdev reference for workqueue processing. */
331 if (!get_device(&cdev->dev))
332 return;
Cornelia Huck59a8a6e2007-05-31 17:38:06 +0200333 spin_lock_irqsave(cdev->ccwlock, flags);
334 cdev->private->state = DEV_STATE_NOT_OPER;
335 spin_unlock_irqrestore(cdev->ccwlock, flags);
Peter Oberparleiter46fbe4e2008-10-10 21:33:05 +0200336 PREPARE_WORK(&cdev->private->kick_work,
337 ccw_device_remove_orphan_cb);
Sebastian Ottc4621a62009-03-31 19:16:04 +0200338 queue_work(slow_path_wq, &cdev->private->kick_work);
Peter Oberparleiter46fbe4e2008-10-10 21:33:05 +0200339 } else
340 /* Deregister subchannel, which will kill the ccw device. */
Sebastian Ottc4621a62009-03-31 19:16:04 +0200341 ccw_device_schedule_sch_unregister(cdev);
Cornelia Huck59a8a6e2007-05-31 17:38:06 +0200342}
343
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +0200344/**
345 * ccw_device_set_offline() - disable a ccw device for I/O
346 * @cdev: target ccw device
347 *
348 * This function calls the driver's set_offline() function for @cdev, if
349 * given, and then disables @cdev.
350 * Returns:
351 * %0 on success and a negative error value on failure.
352 * Context:
353 * enabled, ccw device lock not held
354 */
355int ccw_device_set_offline(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356{
357 int ret;
358
359 if (!cdev)
360 return -ENODEV;
361 if (!cdev->online || !cdev->drv)
362 return -EINVAL;
363
364 if (cdev->drv->set_offline) {
365 ret = cdev->drv->set_offline(cdev);
366 if (ret != 0)
367 return ret;
368 }
369 cdev->online = 0;
370 spin_lock_irq(cdev->ccwlock);
Michael Ernst217ee6c2009-09-11 10:28:21 +0200371 /* Wait until a final state or DISCONNECTED is reached */
372 while (!dev_fsm_final_state(cdev) &&
373 cdev->private->state != DEV_STATE_DISCONNECTED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 spin_unlock_irq(cdev->ccwlock);
Michael Ernst217ee6c2009-09-11 10:28:21 +0200375 wait_event(cdev->private->wait_q, (dev_fsm_final_state(cdev) ||
376 cdev->private->state == DEV_STATE_DISCONNECTED));
377 spin_lock_irq(cdev->ccwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 }
Michael Ernst217ee6c2009-09-11 10:28:21 +0200379 ret = ccw_device_offline(cdev);
380 if (ret)
381 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 spin_unlock_irq(cdev->ccwlock);
Michael Ernst217ee6c2009-09-11 10:28:21 +0200383 wait_event(cdev->private->wait_q, (dev_fsm_final_state(cdev) ||
384 cdev->private->state == DEV_STATE_DISCONNECTED));
385 /* Give up reference from ccw_device_set_online(). */
386 put_device(&cdev->dev);
387 return 0;
388
389error:
390 CIO_MSG_EVENT(0, "ccw_device_offline returned %d, device 0.%x.%04x\n",
391 ret, cdev->private->dev_id.ssid,
392 cdev->private->dev_id.devno);
393 cdev->private->state = DEV_STATE_OFFLINE;
394 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
395 spin_unlock_irq(cdev->ccwlock);
396 /* Give up reference from ccw_device_set_online(). */
397 put_device(&cdev->dev);
398 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399}
400
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +0200401/**
402 * ccw_device_set_online() - enable a ccw device for I/O
403 * @cdev: target ccw device
404 *
405 * This function first enables @cdev and then calls the driver's set_online()
406 * function for @cdev, if given. If set_online() returns an error, @cdev is
407 * disabled again.
408 * Returns:
409 * %0 on success and a negative error value on failure.
410 * Context:
411 * enabled, ccw device lock not held
412 */
413int ccw_device_set_online(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414{
415 int ret;
Michael Ernst217ee6c2009-09-11 10:28:21 +0200416 int ret2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
418 if (!cdev)
419 return -ENODEV;
420 if (cdev->online || !cdev->drv)
421 return -EINVAL;
Cornelia Huck9cd67422008-12-25 13:39:06 +0100422 /* Hold on to an extra reference while device is online. */
423 if (!get_device(&cdev->dev))
424 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
426 spin_lock_irq(cdev->ccwlock);
427 ret = ccw_device_online(cdev);
428 spin_unlock_irq(cdev->ccwlock);
429 if (ret == 0)
430 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
431 else {
Michael Ernst139b83d2008-05-07 09:22:54 +0200432 CIO_MSG_EVENT(0, "ccw_device_online returned %d, "
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200433 "device 0.%x.%04x\n",
434 ret, cdev->private->dev_id.ssid,
435 cdev->private->dev_id.devno);
Cornelia Huck9cd67422008-12-25 13:39:06 +0100436 /* Give up online reference since onlining failed. */
437 put_device(&cdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 return ret;
439 }
Michael Ernst217ee6c2009-09-11 10:28:21 +0200440 spin_lock_irq(cdev->ccwlock);
441 /* Check if online processing was successful */
442 if ((cdev->private->state != DEV_STATE_ONLINE) &&
443 (cdev->private->state != DEV_STATE_W4SENSE)) {
444 spin_unlock_irq(cdev->ccwlock);
Cornelia Huck9cd67422008-12-25 13:39:06 +0100445 /* Give up online reference since onlining failed. */
446 put_device(&cdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 return -ENODEV;
Cornelia Huck9cd67422008-12-25 13:39:06 +0100448 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 spin_unlock_irq(cdev->ccwlock);
Michael Ernst217ee6c2009-09-11 10:28:21 +0200450 if (cdev->drv->set_online)
451 ret = cdev->drv->set_online(cdev);
452 if (ret)
453 goto rollback;
454 cdev->online = 1;
455 return 0;
456
457rollback:
458 spin_lock_irq(cdev->ccwlock);
459 /* Wait until a final state or DISCONNECTED is reached */
460 while (!dev_fsm_final_state(cdev) &&
461 cdev->private->state != DEV_STATE_DISCONNECTED) {
462 spin_unlock_irq(cdev->ccwlock);
463 wait_event(cdev->private->wait_q, (dev_fsm_final_state(cdev) ||
464 cdev->private->state == DEV_STATE_DISCONNECTED));
465 spin_lock_irq(cdev->ccwlock);
466 }
467 ret2 = ccw_device_offline(cdev);
468 if (ret2)
469 goto error;
470 spin_unlock_irq(cdev->ccwlock);
471 wait_event(cdev->private->wait_q, (dev_fsm_final_state(cdev) ||
472 cdev->private->state == DEV_STATE_DISCONNECTED));
Cornelia Huck9cd67422008-12-25 13:39:06 +0100473 /* Give up online reference since onlining failed. */
474 put_device(&cdev->dev);
Michael Ernst217ee6c2009-09-11 10:28:21 +0200475 return ret;
476
477error:
478 CIO_MSG_EVENT(0, "rollback ccw_device_offline returned %d, "
479 "device 0.%x.%04x\n",
480 ret2, cdev->private->dev_id.ssid,
481 cdev->private->dev_id.devno);
482 cdev->private->state = DEV_STATE_OFFLINE;
483 spin_unlock_irq(cdev->ccwlock);
484 /* Give up online reference since onlining failed. */
485 put_device(&cdev->dev);
486 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487}
488
Sebastian Otte74fe0c2009-03-26 15:24:08 +0100489static int online_store_handle_offline(struct ccw_device *cdev)
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200490{
491 if (cdev->private->state == DEV_STATE_DISCONNECTED)
492 ccw_device_remove_disconnected(cdev);
Sebastian Otte74fe0c2009-03-26 15:24:08 +0100493 else if (cdev->online && cdev->drv && cdev->drv->set_offline)
494 return ccw_device_set_offline(cdev);
495 return 0;
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200496}
497
498static int online_store_recog_and_online(struct ccw_device *cdev)
499{
500 int ret;
501
502 /* Do device recognition, if needed. */
Sebastian Ott99f6a5702009-03-31 19:16:07 +0200503 if (cdev->private->state == DEV_STATE_BOXED) {
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200504 ret = ccw_device_recognition(cdev);
505 if (ret) {
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200506 CIO_MSG_EVENT(0, "Couldn't start recognition "
507 "for device 0.%x.%04x (ret=%d)\n",
508 cdev->private->dev_id.ssid,
509 cdev->private->dev_id.devno, ret);
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200510 return ret;
511 }
512 wait_event(cdev->private->wait_q,
513 cdev->private->flags.recog_done);
Sebastian Ott156013f2009-03-31 19:16:03 +0200514 if (cdev->private->state != DEV_STATE_OFFLINE)
515 /* recognition failed */
516 return -EAGAIN;
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200517 }
518 if (cdev->drv && cdev->drv->set_online)
519 ccw_device_set_online(cdev);
520 return 0;
521}
Sebastian Ott156013f2009-03-31 19:16:03 +0200522
Michael Ernstc78aa6c2008-07-14 09:59:22 +0200523static int online_store_handle_online(struct ccw_device *cdev, int force)
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200524{
525 int ret;
526
527 ret = online_store_recog_and_online(cdev);
Sebastian Ott156013f2009-03-31 19:16:03 +0200528 if (ret && !force)
Michael Ernstc78aa6c2008-07-14 09:59:22 +0200529 return ret;
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200530 if (force && cdev->private->state == DEV_STATE_BOXED) {
531 ret = ccw_device_stlck(cdev);
Michael Ernstc78aa6c2008-07-14 09:59:22 +0200532 if (ret)
533 return ret;
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200534 if (cdev->id.cu_type == 0)
535 cdev->private->state = DEV_STATE_NOT_OPER;
Sebastian Ott156013f2009-03-31 19:16:03 +0200536 ret = online_store_recog_and_online(cdev);
537 if (ret)
538 return ret;
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200539 }
Michael Ernstc78aa6c2008-07-14 09:59:22 +0200540 return 0;
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200541}
542
543static ssize_t online_store (struct device *dev, struct device_attribute *attr,
544 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545{
546 struct ccw_device *cdev = to_ccwdev(dev);
Cornelia Huck2f972202008-04-30 13:38:33 +0200547 int force, ret;
548 unsigned long i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
Sebastian Ottb5cd99e2009-03-31 19:16:06 +0200550 if ((cdev->private->state != DEV_STATE_OFFLINE &&
551 cdev->private->state != DEV_STATE_ONLINE &&
552 cdev->private->state != DEV_STATE_BOXED &&
553 cdev->private->state != DEV_STATE_DISCONNECTED) ||
554 atomic_cmpxchg(&cdev->private->onoff, 0, 1) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 return -EAGAIN;
556
557 if (cdev->drv && !try_module_get(cdev->drv->owner)) {
558 atomic_set(&cdev->private->onoff, 0);
559 return -EINVAL;
560 }
561 if (!strncmp(buf, "force\n", count)) {
562 force = 1;
563 i = 1;
Cornelia Huck2f972202008-04-30 13:38:33 +0200564 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 } else {
566 force = 0;
Cornelia Huck2f972202008-04-30 13:38:33 +0200567 ret = strict_strtoul(buf, 16, &i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 }
Cornelia Huck2f972202008-04-30 13:38:33 +0200569 if (ret)
570 goto out;
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200571 switch (i) {
572 case 0:
Sebastian Otte74fe0c2009-03-26 15:24:08 +0100573 ret = online_store_handle_offline(cdev);
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200574 break;
575 case 1:
Michael Ernstc78aa6c2008-07-14 09:59:22 +0200576 ret = online_store_handle_online(cdev, force);
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200577 break;
578 default:
Cornelia Huck2f972202008-04-30 13:38:33 +0200579 ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 }
Cornelia Huck2f972202008-04-30 13:38:33 +0200581out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 if (cdev->drv)
583 module_put(cdev->drv->owner);
584 atomic_set(&cdev->private->onoff, 0);
Sebastian Otte74fe0c2009-03-26 15:24:08 +0100585 return (ret < 0) ? ret : count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586}
587
588static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400589available_show (struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590{
591 struct ccw_device *cdev = to_ccwdev(dev);
592 struct subchannel *sch;
593
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100594 if (ccw_device_is_orphan(cdev))
595 return sprintf(buf, "no device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 switch (cdev->private->state) {
597 case DEV_STATE_BOXED:
598 return sprintf(buf, "boxed\n");
599 case DEV_STATE_DISCONNECTED:
600 case DEV_STATE_DISCONNECTED_SENSE_ID:
601 case DEV_STATE_NOT_OPER:
602 sch = to_subchannel(dev->parent);
603 if (!sch->lpm)
604 return sprintf(buf, "no path\n");
605 else
606 return sprintf(buf, "no device\n");
607 default:
608 /* All other states considered fine. */
609 return sprintf(buf, "good\n");
610 }
611}
612
613static DEVICE_ATTR(chpids, 0444, chpids_show, NULL);
614static DEVICE_ATTR(pimpampom, 0444, pimpampom_show, NULL);
615static DEVICE_ATTR(devtype, 0444, devtype_show, NULL);
616static DEVICE_ATTR(cutype, 0444, cutype_show, NULL);
Bastian Blankf1fc78a2005-10-30 15:00:12 -0800617static DEVICE_ATTR(modalias, 0444, modalias_show, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618static DEVICE_ATTR(online, 0644, online_show, online_store);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619static DEVICE_ATTR(availability, 0444, available_show, NULL);
620
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200621static struct attribute *io_subchannel_attrs[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 &dev_attr_chpids.attr,
623 &dev_attr_pimpampom.attr,
624 NULL,
625};
626
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200627static struct attribute_group io_subchannel_attr_group = {
628 .attrs = io_subchannel_attrs,
Cornelia Huck529192f2006-12-08 15:55:57 +0100629};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630
631static struct attribute * ccwdev_attrs[] = {
632 &dev_attr_devtype.attr,
633 &dev_attr_cutype.attr,
Bastian Blankf1fc78a2005-10-30 15:00:12 -0800634 &dev_attr_modalias.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 &dev_attr_online.attr,
636 &dev_attr_cmb_enable.attr,
637 &dev_attr_availability.attr,
638 NULL,
639};
640
641static struct attribute_group ccwdev_attr_group = {
642 .attrs = ccwdev_attrs,
643};
644
David Brownella4dbd672009-06-24 10:06:31 -0700645static const struct attribute_group *ccwdev_attr_groups[] = {
Cornelia Huckef995162007-04-27 16:01:39 +0200646 &ccwdev_attr_group,
647 NULL,
648};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
650/* this is a simple abstraction for device_register that sets the
651 * correct bus type and adds the bus specific files */
Cornelia Huck3c9da7b2006-10-27 12:39:33 +0200652static int ccw_device_register(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653{
654 struct device *dev = &cdev->dev;
655 int ret;
656
657 dev->bus = &ccw_bus_type;
Sebastian Ott3ac276f2009-09-11 10:28:27 +0200658 ret = dev_set_name(&cdev->dev, "0.%x.%04x", cdev->private->dev_id.ssid,
659 cdev->private->dev_id.devno);
660 if (ret)
661 return ret;
662 ret = device_add(dev);
663 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 return ret;
665
666 set_bit(1, &cdev->private->registered);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 return ret;
668}
669
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700670struct match_data {
Cornelia Huck78964262006-10-11 15:31:38 +0200671 struct ccw_dev_id dev_id;
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700672 struct ccw_device * sibling;
673};
674
675static int
676match_devno(struct device * dev, void * data)
677{
Cornelia Huck78964262006-10-11 15:31:38 +0200678 struct match_data * d = data;
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700679 struct ccw_device * cdev;
680
681 cdev = to_ccwdev(dev);
682 if ((cdev->private->state == DEV_STATE_DISCONNECTED) &&
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100683 !ccw_device_is_orphan(cdev) &&
Cornelia Huck78964262006-10-11 15:31:38 +0200684 ccw_dev_id_is_equal(&cdev->private->dev_id, &d->dev_id) &&
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100685 (cdev != d->sibling))
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700686 return 1;
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700687 return 0;
688}
689
Cornelia Huck78964262006-10-11 15:31:38 +0200690static struct ccw_device * get_disc_ccwdev_by_dev_id(struct ccw_dev_id *dev_id,
691 struct ccw_device *sibling)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 struct device *dev;
Heiko Carstens292888c2006-08-30 14:33:35 +0200694 struct match_data data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
Cornelia Huck78964262006-10-11 15:31:38 +0200696 data.dev_id = *dev_id;
Heiko Carstens292888c2006-08-30 14:33:35 +0200697 data.sibling = sibling;
Cornelia Hucke5945b42005-10-11 08:28:59 -0700698 dev = bus_find_device(&ccw_bus_type, NULL, &data, match_devno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700700 return dev ? to_ccwdev(dev) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701}
702
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100703static int match_orphan(struct device *dev, void *data)
704{
705 struct ccw_dev_id *dev_id;
706 struct ccw_device *cdev;
707
708 dev_id = data;
709 cdev = to_ccwdev(dev);
710 return ccw_dev_id_is_equal(&cdev->private->dev_id, dev_id);
711}
712
713static struct ccw_device *
714get_orphaned_ccwdev_by_dev_id(struct channel_subsystem *css,
715 struct ccw_dev_id *dev_id)
716{
717 struct device *dev;
718
719 dev = device_find_child(&css->pseudo_subchannel->dev, dev_id,
720 match_orphan);
721
722 return dev ? to_ccwdev(dev) : NULL;
723}
724
Cornelia Huckeb32ae82009-03-26 15:24:05 +0100725void ccw_device_do_unbind_bind(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726{
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100727 struct ccw_device_private *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 struct ccw_device *cdev;
729 struct subchannel *sch;
Cornelia Huckeb32ae82009-03-26 15:24:05 +0100730 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100732 priv = container_of(work, struct ccw_device_private, kick_work);
733 cdev = priv->cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 sch = to_subchannel(cdev->dev.parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
Cornelia Huckeb32ae82009-03-26 15:24:05 +0100736 if (test_bit(1, &cdev->private->registered)) {
737 device_release_driver(&cdev->dev);
738 ret = device_attach(&cdev->dev);
739 WARN_ON(ret == -ENODEV);
740 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741}
742
743static void
744ccw_device_release(struct device *dev)
745{
746 struct ccw_device *cdev;
747
748 cdev = to_ccwdev(dev);
Cornelia Huck6eff2082008-12-25 13:39:07 +0100749 /* Release reference of parent subchannel. */
750 put_device(cdev->dev.parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 kfree(cdev->private);
752 kfree(cdev);
753}
754
Cornelia Huck7674da72006-12-08 15:54:21 +0100755static struct ccw_device * io_subchannel_allocate_dev(struct subchannel *sch)
756{
757 struct ccw_device *cdev;
758
759 cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
760 if (cdev) {
761 cdev->private = kzalloc(sizeof(struct ccw_device_private),
762 GFP_KERNEL | GFP_DMA);
763 if (cdev->private)
764 return cdev;
765 }
766 kfree(cdev);
767 return ERR_PTR(-ENOMEM);
768}
769
770static int io_subchannel_initialize_dev(struct subchannel *sch,
771 struct ccw_device *cdev)
772{
773 cdev->private->cdev = cdev;
774 atomic_set(&cdev->private->onoff, 0);
775 cdev->dev.parent = &sch->dev;
776 cdev->dev.release = ccw_device_release;
Heiko Carstens33583c32007-11-05 11:10:07 +0100777 INIT_WORK(&cdev->private->kick_work, NULL);
Cornelia Huckef995162007-04-27 16:01:39 +0200778 cdev->dev.groups = ccwdev_attr_groups;
Cornelia Huck7674da72006-12-08 15:54:21 +0100779 /* Do first half of device_register. */
780 device_initialize(&cdev->dev);
781 if (!get_device(&sch->dev)) {
Cornelia Huck283fdd02008-12-25 13:39:10 +0100782 /* Release reference from device_initialize(). */
783 put_device(&cdev->dev);
Cornelia Huck7674da72006-12-08 15:54:21 +0100784 return -ENODEV;
785 }
786 return 0;
787}
788
789static struct ccw_device * io_subchannel_create_ccwdev(struct subchannel *sch)
790{
791 struct ccw_device *cdev;
792 int ret;
793
794 cdev = io_subchannel_allocate_dev(sch);
795 if (!IS_ERR(cdev)) {
796 ret = io_subchannel_initialize_dev(sch, cdev);
Sebastian Ott06739a82009-08-23 18:09:04 +0200797 if (ret)
Cornelia Huck7674da72006-12-08 15:54:21 +0100798 cdev = ERR_PTR(ret);
Cornelia Huck7674da72006-12-08 15:54:21 +0100799 }
800 return cdev;
801}
802
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100803static int io_subchannel_recog(struct ccw_device *, struct subchannel *);
804
805static void sch_attach_device(struct subchannel *sch,
806 struct ccw_device *cdev)
807{
Cornelia Huck82b7ac02007-04-27 16:01:36 +0200808 css_update_ssd_info(sch);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100809 spin_lock_irq(sch->lock);
Cornelia Huckdb6a6422008-01-26 14:10:46 +0100810 sch_set_cdev(sch, cdev);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100811 cdev->private->schid = sch->schid;
812 cdev->ccwlock = sch->lock;
Cornelia Huckc820de32008-07-14 09:58:45 +0200813 ccw_device_trigger_reprobe(cdev);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100814 spin_unlock_irq(sch->lock);
815}
816
817static void sch_attach_disconnected_device(struct subchannel *sch,
818 struct ccw_device *cdev)
819{
820 struct subchannel *other_sch;
821 int ret;
822
Cornelia Huck6eff2082008-12-25 13:39:07 +0100823 /* Get reference for new parent. */
824 if (!get_device(&sch->dev))
825 return;
826 other_sch = to_subchannel(cdev->dev.parent);
827 /* Note: device_move() changes cdev->dev.parent */
Cornelia Huckffa6a702009-03-04 12:44:00 +0100828 ret = device_move(&cdev->dev, &sch->dev, DPM_ORDER_PARENT_BEFORE_DEV);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100829 if (ret) {
Michael Ernst139b83d2008-05-07 09:22:54 +0200830 CIO_MSG_EVENT(0, "Moving disconnected device 0.%x.%04x failed "
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100831 "(ret=%d)!\n", cdev->private->dev_id.ssid,
832 cdev->private->dev_id.devno, ret);
Cornelia Huck6eff2082008-12-25 13:39:07 +0100833 /* Put reference for new parent. */
834 put_device(&sch->dev);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100835 return;
836 }
Cornelia Huckdb6a6422008-01-26 14:10:46 +0100837 sch_set_cdev(other_sch, NULL);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100838 /* No need to keep a subchannel without ccw device around. */
839 css_sch_device_unregister(other_sch);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100840 sch_attach_device(sch, cdev);
Cornelia Huck6eff2082008-12-25 13:39:07 +0100841 /* Put reference for old parent. */
842 put_device(&other_sch->dev);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100843}
844
845static void sch_attach_orphaned_device(struct subchannel *sch,
846 struct ccw_device *cdev)
847{
848 int ret;
Cornelia Huck6eff2082008-12-25 13:39:07 +0100849 struct subchannel *pseudo_sch;
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100850
Cornelia Huck6eff2082008-12-25 13:39:07 +0100851 /* Get reference for new parent. */
852 if (!get_device(&sch->dev))
853 return;
854 pseudo_sch = to_subchannel(cdev->dev.parent);
855 /*
856 * Try to move the ccw device to its new subchannel.
857 * Note: device_move() changes cdev->dev.parent
858 */
Cornelia Huckffa6a702009-03-04 12:44:00 +0100859 ret = device_move(&cdev->dev, &sch->dev, DPM_ORDER_PARENT_BEFORE_DEV);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100860 if (ret) {
861 CIO_MSG_EVENT(0, "Moving device 0.%x.%04x from orphanage "
862 "failed (ret=%d)!\n",
863 cdev->private->dev_id.ssid,
864 cdev->private->dev_id.devno, ret);
Cornelia Huck6eff2082008-12-25 13:39:07 +0100865 /* Put reference for new parent. */
866 put_device(&sch->dev);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100867 return;
868 }
869 sch_attach_device(sch, cdev);
Cornelia Huck6eff2082008-12-25 13:39:07 +0100870 /* Put reference on pseudo subchannel. */
871 put_device(&pseudo_sch->dev);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100872}
873
874static void sch_create_and_recog_new_device(struct subchannel *sch)
875{
876 struct ccw_device *cdev;
877
878 /* Need to allocate a new ccw device. */
879 cdev = io_subchannel_create_ccwdev(sch);
880 if (IS_ERR(cdev)) {
881 /* OK, we did everything we could... */
882 css_sch_device_unregister(sch);
883 return;
884 }
885 spin_lock_irq(sch->lock);
Cornelia Huckdb6a6422008-01-26 14:10:46 +0100886 sch_set_cdev(sch, cdev);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100887 spin_unlock_irq(sch->lock);
888 /* Start recognition for the new ccw device. */
889 if (io_subchannel_recog(cdev, sch)) {
890 spin_lock_irq(sch->lock);
Cornelia Huckdb6a6422008-01-26 14:10:46 +0100891 sch_set_cdev(sch, NULL);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100892 spin_unlock_irq(sch->lock);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100893 css_sch_device_unregister(sch);
Cornelia Huck6eff2082008-12-25 13:39:07 +0100894 /* Put reference from io_subchannel_create_ccwdev(). */
895 put_device(&sch->dev);
896 /* Give up initial reference. */
897 put_device(&cdev->dev);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100898 }
899}
900
901
902void ccw_device_move_to_orphanage(struct work_struct *work)
903{
904 struct ccw_device_private *priv;
905 struct ccw_device *cdev;
906 struct ccw_device *replacing_cdev;
907 struct subchannel *sch;
908 int ret;
909 struct channel_subsystem *css;
910 struct ccw_dev_id dev_id;
911
912 priv = container_of(work, struct ccw_device_private, kick_work);
913 cdev = priv->cdev;
914 sch = to_subchannel(cdev->dev.parent);
915 css = to_css(sch->dev.parent);
916 dev_id.devno = sch->schib.pmcw.dev;
917 dev_id.ssid = sch->schid.ssid;
918
Cornelia Huck6eff2082008-12-25 13:39:07 +0100919 /* Increase refcount for pseudo subchannel. */
920 get_device(&css->pseudo_subchannel->dev);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100921 /*
922 * Move the orphaned ccw device to the orphanage so the replacing
923 * ccw device can take its place on the subchannel.
Cornelia Huck6eff2082008-12-25 13:39:07 +0100924 * Note: device_move() changes cdev->dev.parent
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100925 */
Cornelia Huckffa6a702009-03-04 12:44:00 +0100926 ret = device_move(&cdev->dev, &css->pseudo_subchannel->dev,
927 DPM_ORDER_NONE);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100928 if (ret) {
929 CIO_MSG_EVENT(0, "Moving device 0.%x.%04x to orphanage failed "
930 "(ret=%d)!\n", cdev->private->dev_id.ssid,
931 cdev->private->dev_id.devno, ret);
Cornelia Huck6eff2082008-12-25 13:39:07 +0100932 /* Decrease refcount for pseudo subchannel again. */
933 put_device(&css->pseudo_subchannel->dev);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100934 return;
935 }
936 cdev->ccwlock = css->pseudo_subchannel->lock;
937 /*
938 * Search for the replacing ccw device
939 * - among the disconnected devices
940 * - in the orphanage
941 */
942 replacing_cdev = get_disc_ccwdev_by_dev_id(&dev_id, cdev);
943 if (replacing_cdev) {
944 sch_attach_disconnected_device(sch, replacing_cdev);
Cornelia Huck85acc402008-11-14 18:18:06 +0100945 /* Release reference from get_disc_ccwdev_by_dev_id() */
Cornelia Huck97166f52008-12-25 13:39:05 +0100946 put_device(&replacing_cdev->dev);
Cornelia Huck6eff2082008-12-25 13:39:07 +0100947 /* Release reference of subchannel from old cdev. */
948 put_device(&sch->dev);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100949 return;
950 }
951 replacing_cdev = get_orphaned_ccwdev_by_dev_id(css, &dev_id);
952 if (replacing_cdev) {
953 sch_attach_orphaned_device(sch, replacing_cdev);
Cornelia Huck85acc402008-11-14 18:18:06 +0100954 /* Release reference from get_orphaned_ccwdev_by_dev_id() */
Cornelia Huck97166f52008-12-25 13:39:05 +0100955 put_device(&replacing_cdev->dev);
Cornelia Huck6eff2082008-12-25 13:39:07 +0100956 /* Release reference of subchannel from old cdev. */
957 put_device(&sch->dev);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100958 return;
959 }
960 sch_create_and_recog_new_device(sch);
Cornelia Huck6eff2082008-12-25 13:39:07 +0100961 /* Release reference of subchannel from old cdev. */
962 put_device(&sch->dev);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100963}
964
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965/*
966 * Register recognized device.
967 */
968static void
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100969io_subchannel_register(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970{
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100971 struct ccw_device_private *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 struct ccw_device *cdev;
973 struct subchannel *sch;
974 int ret;
975 unsigned long flags;
976
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100977 priv = container_of(work, struct ccw_device_private, kick_work);
978 cdev = priv->cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 sch = to_subchannel(cdev->dev.parent);
Cornelia Huck5fb6b852008-12-25 13:39:08 +0100980 /*
981 * Check if subchannel is still registered. It may have become
982 * unregistered if a machine check hit us after finishing
983 * device recognition but before the register work could be
984 * queued.
985 */
986 if (!device_is_registered(&sch->dev))
987 goto out_err;
Cornelia Huck82b7ac02007-04-27 16:01:36 +0200988 css_update_ssd_info(sch);
Cornelia Huck47af5512006-12-04 15:41:07 +0100989 /*
990 * io_subchannel_register() will also be called after device
991 * recognition has been done for a boxed device (which will already
992 * be registered). We need to reprobe since we may now have sense id
993 * information.
994 */
Cornelia Huckd6a30762008-12-25 13:39:11 +0100995 if (device_is_registered(&cdev->dev)) {
Cornelia Huck47af5512006-12-04 15:41:07 +0100996 if (!cdev->drv) {
997 ret = device_reprobe(&cdev->dev);
998 if (ret)
999 /* We can't do much here. */
Michael Ernst139b83d2008-05-07 09:22:54 +02001000 CIO_MSG_EVENT(0, "device_reprobe() returned"
Cornelia Hucke556bbb2007-07-27 12:29:19 +02001001 " %d for 0.%x.%04x\n", ret,
1002 cdev->private->dev_id.ssid,
1003 cdev->private->dev_id.devno);
Cornelia Huck47af5512006-12-04 15:41:07 +01001004 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 goto out;
1006 }
Cornelia Huckfa1a8c22007-04-26 00:12:03 -07001007 /*
1008 * Now we know this subchannel will stay, we can throw
1009 * our delayed uevent.
1010 */
Ming Leif67f1292009-03-01 21:10:49 +08001011 dev_set_uevent_suppress(&sch->dev, 0);
Cornelia Huckfa1a8c22007-04-26 00:12:03 -07001012 kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 /* make it known to the system */
1014 ret = ccw_device_register(cdev);
1015 if (ret) {
Cornelia Hucke556bbb2007-07-27 12:29:19 +02001016 CIO_MSG_EVENT(0, "Could not register ccw dev 0.%x.%04x: %d\n",
1017 cdev->private->dev_id.ssid,
1018 cdev->private->dev_id.devno, ret);
Cornelia Huck2ec22982006-12-08 15:54:26 +01001019 spin_lock_irqsave(sch->lock, flags);
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001020 sch_set_cdev(sch, NULL);
Cornelia Huck2ec22982006-12-08 15:54:26 +01001021 spin_unlock_irqrestore(sch->lock, flags);
Cornelia Huck6eff2082008-12-25 13:39:07 +01001022 /* Release initial device reference. */
1023 put_device(&cdev->dev);
Cornelia Huck5fb6b852008-12-25 13:39:08 +01001024 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026out:
1027 cdev->private->flags.recog_done = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 wake_up(&cdev->private->wait_q);
Cornelia Huck5fb6b852008-12-25 13:39:08 +01001029out_err:
1030 /* Release reference for workqueue processing. */
1031 put_device(&cdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 if (atomic_dec_and_test(&ccw_device_init_count))
1033 wake_up(&ccw_device_init_wq);
1034}
1035
Cornelia Huck3f4cf6e2007-10-12 16:11:26 +02001036static void ccw_device_call_sch_unregister(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037{
Martin Schwidefskyc1637532006-12-08 15:53:57 +01001038 struct ccw_device_private *priv;
1039 struct ccw_device *cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 struct subchannel *sch;
1041
Martin Schwidefskyc1637532006-12-08 15:53:57 +01001042 priv = container_of(work, struct ccw_device_private, kick_work);
1043 cdev = priv->cdev;
Peter Oberparleiter46fbe4e2008-10-10 21:33:05 +02001044 /* Get subchannel reference for local processing. */
1045 if (!get_device(cdev->dev.parent))
1046 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 sch = to_subchannel(cdev->dev.parent);
Cornelia Huck6ab48792006-07-12 16:39:50 +02001048 css_sch_device_unregister(sch);
Peter Oberparleiter46fbe4e2008-10-10 21:33:05 +02001049 /* Release cdev reference for workqueue processing.*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 put_device(&cdev->dev);
Peter Oberparleiter46fbe4e2008-10-10 21:33:05 +02001051 /* Release subchannel reference for local processing. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 put_device(&sch->dev);
1053}
1054
Sebastian Ottc4621a62009-03-31 19:16:04 +02001055void ccw_device_schedule_sch_unregister(struct ccw_device *cdev)
1056{
Sebastian Ottbe7a2dd2009-09-11 10:28:20 +02001057 /* Get cdev reference for workqueue processing. */
1058 if (!get_device(&cdev->dev))
1059 return;
Sebastian Ottc4621a62009-03-31 19:16:04 +02001060 PREPARE_WORK(&cdev->private->kick_work,
1061 ccw_device_call_sch_unregister);
1062 queue_work(slow_path_wq, &cdev->private->kick_work);
1063}
1064
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065/*
1066 * subchannel recognition done. Called from the state machine.
1067 */
1068void
1069io_subchannel_recog_done(struct ccw_device *cdev)
1070{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 if (css_init_done == 0) {
1072 cdev->private->flags.recog_done = 1;
1073 return;
1074 }
1075 switch (cdev->private->state) {
Sebastian Ott47593bf2009-03-31 19:16:05 +02001076 case DEV_STATE_BOXED:
1077 /* Device did not respond in time. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 case DEV_STATE_NOT_OPER:
1079 cdev->private->flags.recog_done = 1;
Sebastian Ottc4621a62009-03-31 19:16:04 +02001080 ccw_device_schedule_sch_unregister(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 if (atomic_dec_and_test(&ccw_device_init_count))
1082 wake_up(&ccw_device_init_wq);
1083 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 case DEV_STATE_OFFLINE:
1085 /*
1086 * We can't register the device in interrupt context so
1087 * we schedule a work item.
1088 */
1089 if (!get_device(&cdev->dev))
1090 break;
1091 PREPARE_WORK(&cdev->private->kick_work,
Martin Schwidefskyc1637532006-12-08 15:53:57 +01001092 io_subchannel_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 queue_work(slow_path_wq, &cdev->private->kick_work);
1094 break;
1095 }
1096}
1097
1098static int
1099io_subchannel_recog(struct ccw_device *cdev, struct subchannel *sch)
1100{
1101 int rc;
1102 struct ccw_device_private *priv;
1103
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001104 sch_set_cdev(sch, cdev);
Cornelia Huck2ec22982006-12-08 15:54:26 +01001105 cdev->ccwlock = sch->lock;
Cornelia Huckfb6958a2006-01-06 00:19:25 -08001106
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 /* Init private data. */
1108 priv = cdev->private;
Cornelia Huck78964262006-10-11 15:31:38 +02001109 priv->dev_id.devno = sch->schib.pmcw.dev;
1110 priv->dev_id.ssid = sch->schid.ssid;
1111 priv->schid = sch->schid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 priv->state = DEV_STATE_NOT_OPER;
1113 INIT_LIST_HEAD(&priv->cmb_list);
1114 init_waitqueue_head(&priv->wait_q);
1115 init_timer(&priv->timer);
1116
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 /* Increase counter of devices currently in recognition. */
1118 atomic_inc(&ccw_device_init_count);
1119
1120 /* Start async. device sensing. */
Cornelia Huck2ec22982006-12-08 15:54:26 +01001121 spin_lock_irq(sch->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 rc = ccw_device_recognition(cdev);
Cornelia Huck2ec22982006-12-08 15:54:26 +01001123 spin_unlock_irq(sch->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 if (rc) {
1125 if (atomic_dec_and_test(&ccw_device_init_count))
1126 wake_up(&ccw_device_init_wq);
1127 }
1128 return rc;
1129}
1130
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +01001131static void ccw_device_move_to_sch(struct work_struct *work)
1132{
1133 struct ccw_device_private *priv;
1134 int rc;
1135 struct subchannel *sch;
1136 struct ccw_device *cdev;
1137 struct subchannel *former_parent;
1138
1139 priv = container_of(work, struct ccw_device_private, kick_work);
1140 sch = priv->sch;
1141 cdev = priv->cdev;
Cornelia Huck6eff2082008-12-25 13:39:07 +01001142 former_parent = to_subchannel(cdev->dev.parent);
1143 /* Get reference for new parent. */
1144 if (!get_device(&sch->dev))
1145 return;
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +01001146 mutex_lock(&sch->reg_mutex);
Cornelia Huck6eff2082008-12-25 13:39:07 +01001147 /*
1148 * Try to move the ccw device to its new subchannel.
1149 * Note: device_move() changes cdev->dev.parent
1150 */
Cornelia Huckffa6a702009-03-04 12:44:00 +01001151 rc = device_move(&cdev->dev, &sch->dev, DPM_ORDER_PARENT_BEFORE_DEV);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +01001152 mutex_unlock(&sch->reg_mutex);
1153 if (rc) {
Michael Ernst139b83d2008-05-07 09:22:54 +02001154 CIO_MSG_EVENT(0, "Moving device 0.%x.%04x to subchannel "
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +01001155 "0.%x.%04x failed (ret=%d)!\n",
1156 cdev->private->dev_id.ssid,
1157 cdev->private->dev_id.devno, sch->schid.ssid,
1158 sch->schid.sch_no, rc);
1159 css_sch_device_unregister(sch);
Cornelia Huck6eff2082008-12-25 13:39:07 +01001160 /* Put reference for new parent again. */
1161 put_device(&sch->dev);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +01001162 goto out;
1163 }
Cornelia Huck6eff2082008-12-25 13:39:07 +01001164 if (!sch_is_pseudo_sch(former_parent)) {
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +01001165 spin_lock_irq(former_parent->lock);
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001166 sch_set_cdev(former_parent, NULL);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +01001167 spin_unlock_irq(former_parent->lock);
1168 css_sch_device_unregister(former_parent);
1169 /* Reset intparm to zeroes. */
Sebastian Ott13952ec2008-12-25 13:39:13 +01001170 former_parent->config.intparm = 0;
1171 cio_commit_config(former_parent);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +01001172 }
1173 sch_attach_device(sch, cdev);
1174out:
Cornelia Huck6eff2082008-12-25 13:39:07 +01001175 /* Put reference for old parent. */
1176 put_device(&former_parent->dev);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +01001177 put_device(&cdev->dev);
1178}
1179
Cornelia Huck602b20f2008-01-26 14:10:39 +01001180static void io_subchannel_irq(struct subchannel *sch)
1181{
1182 struct ccw_device *cdev;
1183
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001184 cdev = sch_get_cdev(sch);
Cornelia Huck602b20f2008-01-26 14:10:39 +01001185
Sebastian Ottefd986d2009-09-11 10:28:18 +02001186 CIO_TRACE_EVENT(6, "IRQ");
1187 CIO_TRACE_EVENT(6, dev_name(&sch->dev));
Cornelia Huck602b20f2008-01-26 14:10:39 +01001188 if (cdev)
1189 dev_fsm_event(cdev, DEV_EVENT_INTERRUPT);
1190}
1191
Sebastian Ott13952ec2008-12-25 13:39:13 +01001192void io_subchannel_init_config(struct subchannel *sch)
1193{
1194 memset(&sch->config, 0, sizeof(sch->config));
1195 sch->config.csense = 1;
Sebastian Ottd36f0c62008-12-25 13:39:15 +01001196 /* Use subchannel mp mode when there is more than 1 installed CHPID. */
1197 if ((sch->schib.pmcw.pim & (sch->schib.pmcw.pim - 1)) != 0)
Sebastian Ott13952ec2008-12-25 13:39:13 +01001198 sch->config.mp = 1;
1199}
1200
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001201static void io_subchannel_init_fields(struct subchannel *sch)
1202{
1203 if (cio_is_console(sch->schid))
1204 sch->opm = 0xff;
1205 else
1206 sch->opm = chp_get_sch_opm(sch);
1207 sch->lpm = sch->schib.pmcw.pam & sch->opm;
Cornelia Huck3a3fc292008-07-14 09:58:58 +02001208 sch->isc = cio_is_console(sch->schid) ? CONSOLE_ISC : IO_SCH_ISC;
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001209
1210 CIO_MSG_EVENT(6, "Detected device %04x on subchannel 0.%x.%04X"
1211 " - PIM = %02X, PAM = %02X, POM = %02X\n",
1212 sch->schib.pmcw.dev, sch->schid.ssid,
1213 sch->schid.sch_no, sch->schib.pmcw.pim,
1214 sch->schib.pmcw.pam, sch->schib.pmcw.pom);
Sebastian Ott13952ec2008-12-25 13:39:13 +01001215
1216 io_subchannel_init_config(sch);
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001217}
1218
Cornelia Huck90ed2b62008-12-25 13:39:09 +01001219static void io_subchannel_do_unreg(struct work_struct *work)
1220{
1221 struct subchannel *sch;
1222
1223 sch = container_of(work, struct subchannel, work);
1224 css_sch_device_unregister(sch);
Cornelia Huck90ed2b62008-12-25 13:39:09 +01001225 put_device(&sch->dev);
1226}
1227
1228/* Schedule unregister if we have no cdev. */
1229static void io_subchannel_schedule_removal(struct subchannel *sch)
1230{
1231 get_device(&sch->dev);
1232 INIT_WORK(&sch->work, io_subchannel_do_unreg);
1233 queue_work(slow_path_wq, &sch->work);
1234}
1235
1236/*
1237 * Note: We always return 0 so that we bind to the device even on error.
1238 * This is needed so that our remove function is called on unregister.
1239 */
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001240static int io_subchannel_probe(struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 struct ccw_device *cdev;
1243 int rc;
1244 unsigned long flags;
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +01001245 struct ccw_dev_id dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001247 cdev = sch_get_cdev(sch);
1248 if (cdev) {
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001249 rc = sysfs_create_group(&sch->dev.kobj,
1250 &io_subchannel_attr_group);
1251 if (rc)
1252 CIO_MSG_EVENT(0, "Failed to create io subchannel "
1253 "attributes for subchannel "
1254 "0.%x.%04x (rc=%d)\n",
1255 sch->schid.ssid, sch->schid.sch_no, rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 /*
1257 * This subchannel already has an associated ccw_device.
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001258 * Throw the delayed uevent for the subchannel, register
1259 * the ccw_device and exit. This happens for all early
1260 * devices, e.g. the console.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 */
Ming Leif67f1292009-03-01 21:10:49 +08001262 dev_set_uevent_suppress(&sch->dev, 0);
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001263 kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
Cornelia Hucke1031782007-10-12 16:11:23 +02001264 cdev->dev.groups = ccwdev_attr_groups;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 device_initialize(&cdev->dev);
1266 ccw_device_register(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 /*
1268 * Check if the device is already online. If it is
Cornelia Huck9cd67422008-12-25 13:39:06 +01001269 * the reference count needs to be corrected since we
1270 * didn't obtain a reference in ccw_device_set_online.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 */
1272 if (cdev->private->state != DEV_STATE_NOT_OPER &&
1273 cdev->private->state != DEV_STATE_OFFLINE &&
1274 cdev->private->state != DEV_STATE_BOXED)
1275 get_device(&cdev->dev);
1276 return 0;
1277 }
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001278 io_subchannel_init_fields(sch);
Sebastian Ottf444cc02008-12-25 13:39:14 +01001279 rc = cio_commit_config(sch);
1280 if (rc)
1281 goto out_schedule;
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001282 rc = sysfs_create_group(&sch->dev.kobj,
1283 &io_subchannel_attr_group);
1284 if (rc)
Cornelia Huck90ed2b62008-12-25 13:39:09 +01001285 goto out_schedule;
Cornelia Huckcd6b4f22008-01-26 14:10:43 +01001286 /* Allocate I/O subchannel private data. */
1287 sch->private = kzalloc(sizeof(struct io_subchannel_private),
1288 GFP_KERNEL | GFP_DMA);
Cornelia Huck90ed2b62008-12-25 13:39:09 +01001289 if (!sch->private)
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001290 goto out_err;
Sebastian Ott111e95a2008-12-25 13:39:03 +01001291 /*
1292 * First check if a fitting device may be found amongst the
1293 * disconnected devices or in the orphanage.
1294 */
1295 dev_id.devno = sch->schib.pmcw.dev;
1296 dev_id.ssid = sch->schid.ssid;
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +01001297 cdev = get_disc_ccwdev_by_dev_id(&dev_id, NULL);
1298 if (!cdev)
1299 cdev = get_orphaned_ccwdev_by_dev_id(to_css(sch->dev.parent),
1300 &dev_id);
1301 if (cdev) {
1302 /*
1303 * Schedule moving the device until when we have a registered
1304 * subchannel to move to and succeed the probe. We can
1305 * unregister later again, when the probe is through.
1306 */
1307 cdev->private->sch = sch;
1308 PREPARE_WORK(&cdev->private->kick_work,
1309 ccw_device_move_to_sch);
1310 queue_work(slow_path_wq, &cdev->private->kick_work);
1311 return 0;
1312 }
Cornelia Huck7674da72006-12-08 15:54:21 +01001313 cdev = io_subchannel_create_ccwdev(sch);
Cornelia Huck90ed2b62008-12-25 13:39:09 +01001314 if (IS_ERR(cdev))
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001315 goto out_err;
Cornelia Huck8bbace72006-01-11 10:56:22 +01001316 rc = io_subchannel_recog(cdev, sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 if (rc) {
Cornelia Huck2ec22982006-12-08 15:54:26 +01001318 spin_lock_irqsave(sch->lock, flags);
Cornelia Huck90ed2b62008-12-25 13:39:09 +01001319 io_subchannel_recog_done(cdev);
Cornelia Huck2ec22982006-12-08 15:54:26 +01001320 spin_unlock_irqrestore(sch->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 }
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001322 return 0;
1323out_err:
1324 kfree(sch->private);
1325 sysfs_remove_group(&sch->dev.kobj, &io_subchannel_attr_group);
Cornelia Huck90ed2b62008-12-25 13:39:09 +01001326out_schedule:
1327 io_subchannel_schedule_removal(sch);
1328 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329}
1330
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331static int
Cornelia Huck8bbace72006-01-11 10:56:22 +01001332io_subchannel_remove (struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333{
1334 struct ccw_device *cdev;
1335 unsigned long flags;
1336
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001337 cdev = sch_get_cdev(sch);
1338 if (!cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 /* Set ccw device to not operational and drop reference. */
1341 spin_lock_irqsave(cdev->ccwlock, flags);
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001342 sch_set_cdev(sch, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 cdev->private->state = DEV_STATE_NOT_OPER;
1344 spin_unlock_irqrestore(cdev->ccwlock, flags);
Cornelia Huckef995162007-04-27 16:01:39 +02001345 ccw_device_unregister(cdev);
Cornelia Huckcd6b4f22008-01-26 14:10:43 +01001346 kfree(sch->private);
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001347 sysfs_remove_group(&sch->dev.kobj, &io_subchannel_attr_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 return 0;
1349}
1350
Cornelia Huck602b20f2008-01-26 14:10:39 +01001351static int io_subchannel_notify(struct subchannel *sch, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352{
1353 struct ccw_device *cdev;
1354
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001355 cdev = sch_get_cdev(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 if (!cdev)
1357 return 0;
Cornelia Huckc820de32008-07-14 09:58:45 +02001358 return ccw_device_notify(cdev, event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359}
1360
Cornelia Huck602b20f2008-01-26 14:10:39 +01001361static void io_subchannel_verify(struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362{
1363 struct ccw_device *cdev;
1364
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001365 cdev = sch_get_cdev(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 if (cdev)
1367 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1368}
1369
Cornelia Huckc820de32008-07-14 09:58:45 +02001370static int check_for_io_on_path(struct subchannel *sch, int mask)
1371{
Sebastian Ottcdb912a2008-12-25 13:39:12 +01001372 if (cio_update_schib(sch))
Cornelia Huckc820de32008-07-14 09:58:45 +02001373 return 0;
Peter Oberparleiter23d805b2008-07-14 09:58:50 +02001374 if (scsw_actl(&sch->schib.scsw) && sch->schib.pmcw.lpum == mask)
Cornelia Huckc820de32008-07-14 09:58:45 +02001375 return 1;
1376 return 0;
1377}
1378
1379static void terminate_internal_io(struct subchannel *sch,
1380 struct ccw_device *cdev)
1381{
1382 if (cio_clear(sch)) {
1383 /* Recheck device in case clear failed. */
1384 sch->lpm = 0;
1385 if (cdev->online)
1386 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1387 else
1388 css_schedule_eval(sch->schid);
1389 return;
1390 }
1391 cdev->private->state = DEV_STATE_CLEAR_VERIFY;
1392 /* Request retry of internal operation. */
1393 cdev->private->flags.intretry = 1;
1394 /* Call handler. */
1395 if (cdev->handler)
1396 cdev->handler(cdev, cdev->private->intparm,
1397 ERR_PTR(-EIO));
1398}
1399
1400static void io_subchannel_terminate_path(struct subchannel *sch, u8 mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401{
1402 struct ccw_device *cdev;
1403
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001404 cdev = sch_get_cdev(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 if (!cdev)
1406 return;
Cornelia Huckc820de32008-07-14 09:58:45 +02001407 if (check_for_io_on_path(sch, mask)) {
1408 if (cdev->private->state == DEV_STATE_ONLINE)
1409 ccw_device_kill_io(cdev);
1410 else {
1411 terminate_internal_io(sch, cdev);
1412 /* Re-start path verification. */
1413 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1414 }
1415 } else
1416 /* trigger path verification. */
1417 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1418
1419}
1420
Cornelia Huck99611f82008-07-14 09:59:02 +02001421static int io_subchannel_chp_event(struct subchannel *sch,
1422 struct chp_link *link, int event)
Cornelia Huckc820de32008-07-14 09:58:45 +02001423{
1424 int mask;
Cornelia Huckc820de32008-07-14 09:58:45 +02001425
Cornelia Huck99611f82008-07-14 09:59:02 +02001426 mask = chp_ssd_get_mask(&sch->ssd_info, link);
Cornelia Huckc820de32008-07-14 09:58:45 +02001427 if (!mask)
1428 return 0;
1429 switch (event) {
1430 case CHP_VARY_OFF:
1431 sch->opm &= ~mask;
1432 sch->lpm &= ~mask;
1433 io_subchannel_terminate_path(sch, mask);
1434 break;
1435 case CHP_VARY_ON:
1436 sch->opm |= mask;
1437 sch->lpm |= mask;
1438 io_subchannel_verify(sch);
1439 break;
1440 case CHP_OFFLINE:
Sebastian Ottcdb912a2008-12-25 13:39:12 +01001441 if (cio_update_schib(sch))
Cornelia Huckc820de32008-07-14 09:58:45 +02001442 return -ENODEV;
1443 io_subchannel_terminate_path(sch, mask);
1444 break;
1445 case CHP_ONLINE:
Sebastian Ottcdb912a2008-12-25 13:39:12 +01001446 if (cio_update_schib(sch))
1447 return -ENODEV;
Cornelia Huckc820de32008-07-14 09:58:45 +02001448 sch->lpm |= mask & sch->opm;
1449 io_subchannel_verify(sch);
1450 break;
1451 }
1452 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453}
1454
1455static void
Cornelia Huck8bbace72006-01-11 10:56:22 +01001456io_subchannel_shutdown(struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 struct ccw_device *cdev;
1459 int ret;
1460
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001461 cdev = sch_get_cdev(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462
Cornelia Hucka8237fc2006-01-06 00:19:21 -08001463 if (cio_is_console(sch->schid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 return;
1465 if (!sch->schib.pmcw.ena)
1466 /* Nothing to do. */
1467 return;
1468 ret = cio_disable_subchannel(sch);
1469 if (ret != -EBUSY)
1470 /* Subchannel is disabled, we're done. */
1471 return;
1472 cdev->private->state = DEV_STATE_QUIESCE;
1473 if (cdev->handler)
1474 cdev->handler(cdev, cdev->private->intparm,
1475 ERR_PTR(-EIO));
1476 ret = ccw_device_cancel_halt_clear(cdev);
1477 if (ret == -EBUSY) {
1478 ccw_device_set_timeout(cdev, HZ/10);
1479 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
1480 }
1481 cio_disable_subchannel(sch);
1482}
1483
Cornelia Huckc820de32008-07-14 09:58:45 +02001484static int io_subchannel_get_status(struct subchannel *sch)
1485{
1486 struct schib schib;
1487
1488 if (stsch(sch->schid, &schib) || !schib.pmcw.dnv)
1489 return CIO_GONE;
1490 if (sch->schib.pmcw.dnv && (schib.pmcw.dev != sch->schib.pmcw.dev))
1491 return CIO_REVALIDATE;
1492 if (!sch->lpm)
1493 return CIO_NO_PATH;
1494 return CIO_OPER;
1495}
1496
1497static int device_is_disconnected(struct ccw_device *cdev)
1498{
1499 if (!cdev)
1500 return 0;
1501 return (cdev->private->state == DEV_STATE_DISCONNECTED ||
1502 cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID);
1503}
1504
1505static int recovery_check(struct device *dev, void *data)
1506{
1507 struct ccw_device *cdev = to_ccwdev(dev);
1508 int *redo = data;
1509
1510 spin_lock_irq(cdev->ccwlock);
1511 switch (cdev->private->state) {
1512 case DEV_STATE_DISCONNECTED:
1513 CIO_MSG_EVENT(3, "recovery: trigger 0.%x.%04x\n",
1514 cdev->private->dev_id.ssid,
1515 cdev->private->dev_id.devno);
1516 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1517 *redo = 1;
1518 break;
1519 case DEV_STATE_DISCONNECTED_SENSE_ID:
1520 *redo = 1;
1521 break;
1522 }
1523 spin_unlock_irq(cdev->ccwlock);
1524
1525 return 0;
1526}
1527
1528static void recovery_work_func(struct work_struct *unused)
1529{
1530 int redo = 0;
1531
1532 bus_for_each_dev(&ccw_bus_type, NULL, &redo, recovery_check);
1533 if (redo) {
1534 spin_lock_irq(&recovery_lock);
1535 if (!timer_pending(&recovery_timer)) {
1536 if (recovery_phase < ARRAY_SIZE(recovery_delay) - 1)
1537 recovery_phase++;
1538 mod_timer(&recovery_timer, jiffies +
1539 recovery_delay[recovery_phase] * HZ);
1540 }
1541 spin_unlock_irq(&recovery_lock);
1542 } else
1543 CIO_MSG_EVENT(4, "recovery: end\n");
1544}
1545
1546static DECLARE_WORK(recovery_work, recovery_work_func);
1547
1548static void recovery_func(unsigned long data)
1549{
1550 /*
1551 * We can't do our recovery in softirq context and it's not
1552 * performance critical, so we schedule it.
1553 */
1554 schedule_work(&recovery_work);
1555}
1556
1557static void ccw_device_schedule_recovery(void)
1558{
1559 unsigned long flags;
1560
1561 CIO_MSG_EVENT(4, "recovery: schedule\n");
1562 spin_lock_irqsave(&recovery_lock, flags);
1563 if (!timer_pending(&recovery_timer) || (recovery_phase != 0)) {
1564 recovery_phase = 0;
1565 mod_timer(&recovery_timer, jiffies + recovery_delay[0] * HZ);
1566 }
1567 spin_unlock_irqrestore(&recovery_lock, flags);
1568}
1569
Peter Oberparleiterecf5d9e2008-10-10 21:33:06 +02001570static int purge_fn(struct device *dev, void *data)
1571{
1572 struct ccw_device *cdev = to_ccwdev(dev);
1573 struct ccw_device_private *priv = cdev->private;
1574 int unreg;
1575
1576 spin_lock_irq(cdev->ccwlock);
1577 unreg = is_blacklisted(priv->dev_id.ssid, priv->dev_id.devno) &&
1578 (priv->state == DEV_STATE_OFFLINE);
1579 spin_unlock_irq(cdev->ccwlock);
1580 if (!unreg)
1581 goto out;
Peter Oberparleiterecf5d9e2008-10-10 21:33:06 +02001582 CIO_MSG_EVENT(3, "ccw: purging 0.%x.%04x\n", priv->dev_id.ssid,
1583 priv->dev_id.devno);
Sebastian Ottc4621a62009-03-31 19:16:04 +02001584 ccw_device_schedule_sch_unregister(cdev);
Peter Oberparleiterecf5d9e2008-10-10 21:33:06 +02001585
1586out:
1587 /* Abort loop in case of pending signal. */
1588 if (signal_pending(current))
1589 return -EINTR;
1590
1591 return 0;
1592}
1593
1594/**
1595 * ccw_purge_blacklisted - purge unused, blacklisted devices
1596 *
1597 * Unregister all ccw devices that are offline and on the blacklist.
1598 */
1599int ccw_purge_blacklisted(void)
1600{
1601 CIO_MSG_EVENT(2, "ccw: purging blacklisted devices\n");
1602 bus_for_each_dev(&ccw_bus_type, NULL, NULL, purge_fn);
1603 return 0;
1604}
1605
Cornelia Huckc820de32008-07-14 09:58:45 +02001606static void device_set_disconnected(struct ccw_device *cdev)
1607{
1608 if (!cdev)
1609 return;
1610 ccw_device_set_timeout(cdev, 0);
1611 cdev->private->flags.fake_irb = 0;
1612 cdev->private->state = DEV_STATE_DISCONNECTED;
1613 if (cdev->online)
1614 ccw_device_schedule_recovery();
1615}
1616
Peter Oberparleiter91c36912008-08-21 19:46:39 +02001617void ccw_device_set_notoper(struct ccw_device *cdev)
1618{
1619 struct subchannel *sch = to_subchannel(cdev->dev.parent);
1620
1621 CIO_TRACE_EVENT(2, "notoper");
Cornelia Huckb9d3aed2008-10-10 21:33:11 +02001622 CIO_TRACE_EVENT(2, dev_name(&sch->dev));
Peter Oberparleiter91c36912008-08-21 19:46:39 +02001623 ccw_device_set_timeout(cdev, 0);
1624 cio_disable_subchannel(sch);
1625 cdev->private->state = DEV_STATE_NOT_OPER;
1626}
1627
Cornelia Huckc820de32008-07-14 09:58:45 +02001628static int io_subchannel_sch_event(struct subchannel *sch, int slow)
1629{
1630 int event, ret, disc;
1631 unsigned long flags;
Peter Oberparleiter91c36912008-08-21 19:46:39 +02001632 enum { NONE, UNREGISTER, UNREGISTER_PROBE, REPROBE, DISC } action;
Cornelia Huckc820de32008-07-14 09:58:45 +02001633 struct ccw_device *cdev;
1634
1635 spin_lock_irqsave(sch->lock, flags);
1636 cdev = sch_get_cdev(sch);
1637 disc = device_is_disconnected(cdev);
1638 if (disc && slow) {
1639 /* Disconnected devices are evaluated directly only.*/
1640 spin_unlock_irqrestore(sch->lock, flags);
1641 return 0;
1642 }
1643 /* No interrupt after machine check - kill pending timers. */
1644 if (cdev)
1645 ccw_device_set_timeout(cdev, 0);
1646 if (!disc && !slow) {
1647 /* Non-disconnected devices are evaluated on the slow path. */
1648 spin_unlock_irqrestore(sch->lock, flags);
1649 return -EAGAIN;
1650 }
1651 event = io_subchannel_get_status(sch);
1652 CIO_MSG_EVENT(4, "Evaluating schid 0.%x.%04x, event %d, %s, %s path.\n",
1653 sch->schid.ssid, sch->schid.sch_no, event,
1654 disc ? "disconnected" : "normal",
1655 slow ? "slow" : "fast");
1656 /* Analyze subchannel status. */
1657 action = NONE;
1658 switch (event) {
1659 case CIO_NO_PATH:
1660 if (disc) {
1661 /* Check if paths have become available. */
1662 action = REPROBE;
1663 break;
1664 }
1665 /* fall through */
1666 case CIO_GONE:
Cornelia Huckc820de32008-07-14 09:58:45 +02001667 /* Ask driver what to do with device. */
Peter Oberparleiter91c36912008-08-21 19:46:39 +02001668 if (io_subchannel_notify(sch, event))
1669 action = DISC;
1670 else
1671 action = UNREGISTER;
Cornelia Huckc820de32008-07-14 09:58:45 +02001672 break;
1673 case CIO_REVALIDATE:
1674 /* Device will be removed, so no notify necessary. */
1675 if (disc)
1676 /* Reprobe because immediate unregister might block. */
1677 action = REPROBE;
1678 else
1679 action = UNREGISTER_PROBE;
1680 break;
1681 case CIO_OPER:
1682 if (disc)
1683 /* Get device operational again. */
1684 action = REPROBE;
1685 break;
1686 }
1687 /* Perform action. */
1688 ret = 0;
1689 switch (action) {
1690 case UNREGISTER:
1691 case UNREGISTER_PROBE:
Peter Oberparleiter91c36912008-08-21 19:46:39 +02001692 ccw_device_set_notoper(cdev);
Cornelia Huckc820de32008-07-14 09:58:45 +02001693 /* Unregister device (will use subchannel lock). */
1694 spin_unlock_irqrestore(sch->lock, flags);
1695 css_sch_device_unregister(sch);
1696 spin_lock_irqsave(sch->lock, flags);
Cornelia Huckc820de32008-07-14 09:58:45 +02001697 break;
1698 case REPROBE:
1699 ccw_device_trigger_reprobe(cdev);
1700 break;
Peter Oberparleiter91c36912008-08-21 19:46:39 +02001701 case DISC:
1702 device_set_disconnected(cdev);
1703 break;
Cornelia Huckc820de32008-07-14 09:58:45 +02001704 default:
1705 break;
1706 }
1707 spin_unlock_irqrestore(sch->lock, flags);
1708 /* Probe if necessary. */
1709 if (action == UNREGISTER_PROBE)
1710 ret = css_probe_device(sch->schid);
1711
1712 return ret;
1713}
1714
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715#ifdef CONFIG_CCW_CONSOLE
1716static struct ccw_device console_cdev;
1717static struct ccw_device_private console_private;
1718static int console_cdev_in_use;
1719
Cornelia Huck2ec22982006-12-08 15:54:26 +01001720static DEFINE_SPINLOCK(ccw_console_lock);
1721
1722spinlock_t * cio_get_console_lock(void)
1723{
1724 return &ccw_console_lock;
1725}
1726
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001727static int ccw_device_console_enable(struct ccw_device *cdev,
1728 struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729{
1730 int rc;
1731
Cornelia Huckcd6b4f22008-01-26 14:10:43 +01001732 /* Attach subchannel private data. */
1733 sch->private = cio_get_console_priv();
1734 memset(sch->private, 0, sizeof(struct io_subchannel_private));
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001735 io_subchannel_init_fields(sch);
Sebastian Ottf444cc02008-12-25 13:39:14 +01001736 rc = cio_commit_config(sch);
1737 if (rc)
1738 return rc;
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001739 sch->driver = &io_subchannel_driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 /* Initialize the ccw_device structure. */
Heiko Carstens292888c2006-08-30 14:33:35 +02001741 cdev->dev.parent= &sch->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 rc = io_subchannel_recog(cdev, sch);
1743 if (rc)
1744 return rc;
1745
1746 /* Now wait for the async. recognition to come to an end. */
1747 spin_lock_irq(cdev->ccwlock);
1748 while (!dev_fsm_final_state(cdev))
1749 wait_cons_dev();
1750 rc = -EIO;
1751 if (cdev->private->state != DEV_STATE_OFFLINE)
1752 goto out_unlock;
1753 ccw_device_online(cdev);
1754 while (!dev_fsm_final_state(cdev))
1755 wait_cons_dev();
1756 if (cdev->private->state != DEV_STATE_ONLINE)
1757 goto out_unlock;
1758 rc = 0;
1759out_unlock:
1760 spin_unlock_irq(cdev->ccwlock);
1761 return 0;
1762}
1763
1764struct ccw_device *
1765ccw_device_probe_console(void)
1766{
1767 struct subchannel *sch;
1768 int ret;
1769
1770 if (xchg(&console_cdev_in_use, 1) != 0)
Peter Oberparleiter600b5d12006-02-01 03:06:35 -08001771 return ERR_PTR(-EBUSY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 sch = cio_probe_console();
1773 if (IS_ERR(sch)) {
1774 console_cdev_in_use = 0;
1775 return (void *) sch;
1776 }
1777 memset(&console_cdev, 0, sizeof(struct ccw_device));
1778 memset(&console_private, 0, sizeof(struct ccw_device_private));
1779 console_cdev.private = &console_private;
Martin Schwidefskyc1637532006-12-08 15:53:57 +01001780 console_private.cdev = &console_cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 ret = ccw_device_console_enable(&console_cdev, sch);
1782 if (ret) {
1783 cio_release_console();
1784 console_cdev_in_use = 0;
1785 return ERR_PTR(ret);
1786 }
1787 console_cdev.online = 1;
1788 return &console_cdev;
1789}
Cornelia Huck1f4e7ed2008-10-10 21:33:14 +02001790
Martin Schwidefsky66648452009-06-16 10:30:28 +02001791static int ccw_device_pm_restore(struct device *dev);
1792
1793int ccw_device_force_console(void)
1794{
1795 if (!console_cdev_in_use)
1796 return -ENODEV;
1797 return ccw_device_pm_restore(&console_cdev.dev);
1798}
1799EXPORT_SYMBOL_GPL(ccw_device_force_console);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800#endif
1801
1802/*
1803 * get ccw_device matching the busid, but only if owned by cdrv
1804 */
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001805static int
1806__ccwdev_check_busid(struct device *dev, void *id)
1807{
1808 char *bus_id;
1809
Cornelia Huck12975ae2006-10-11 15:31:47 +02001810 bus_id = id;
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001811
Kay Sievers98df67b2008-12-25 13:38:55 +01001812 return (strcmp(bus_id, dev_name(dev)) == 0);
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001813}
1814
1815
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +02001816/**
1817 * get_ccwdev_by_busid() - obtain device from a bus id
1818 * @cdrv: driver the device is owned by
1819 * @bus_id: bus id of the device to be searched
1820 *
1821 * This function searches all devices owned by @cdrv for a device with a bus
1822 * id matching @bus_id.
1823 * Returns:
1824 * If a match is found, its reference count of the found device is increased
1825 * and it is returned; else %NULL is returned.
1826 */
1827struct ccw_device *get_ccwdev_by_busid(struct ccw_driver *cdrv,
1828 const char *bus_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829{
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001830 struct device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 struct device_driver *drv;
1832
1833 drv = get_driver(&cdrv->driver);
1834 if (!drv)
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001835 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001837 dev = driver_find_device(drv, NULL, (void *)bus_id,
1838 __ccwdev_check_busid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 put_driver(drv);
1840
Heiko Carstensd2c993d2006-07-12 16:41:55 +02001841 return dev ? to_ccwdev(dev) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842}
1843
1844/************************** device driver handling ************************/
1845
1846/* This is the implementation of the ccw_driver class. The probe, remove
1847 * and release methods are initially very similar to the device_driver
1848 * implementations, with the difference that they have ccw_device
1849 * arguments.
1850 *
1851 * A ccw driver also contains the information that is needed for
1852 * device matching.
1853 */
1854static int
1855ccw_device_probe (struct device *dev)
1856{
1857 struct ccw_device *cdev = to_ccwdev(dev);
1858 struct ccw_driver *cdrv = to_ccwdrv(dev->driver);
1859 int ret;
1860
1861 cdev->drv = cdrv; /* to let the driver call _set_online */
1862
1863 ret = cdrv->probe ? cdrv->probe(cdev) : -ENODEV;
1864
1865 if (ret) {
Heiko Carstensd2c993d2006-07-12 16:41:55 +02001866 cdev->drv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 return ret;
1868 }
1869
1870 return 0;
1871}
1872
1873static int
1874ccw_device_remove (struct device *dev)
1875{
1876 struct ccw_device *cdev = to_ccwdev(dev);
1877 struct ccw_driver *cdrv = cdev->drv;
1878 int ret;
1879
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 if (cdrv->remove)
1881 cdrv->remove(cdev);
1882 if (cdev->online) {
1883 cdev->online = 0;
1884 spin_lock_irq(cdev->ccwlock);
1885 ret = ccw_device_offline(cdev);
1886 spin_unlock_irq(cdev->ccwlock);
1887 if (ret == 0)
1888 wait_event(cdev->private->wait_q,
1889 dev_fsm_final_state(cdev));
1890 else
Michael Ernst139b83d2008-05-07 09:22:54 +02001891 CIO_MSG_EVENT(0, "ccw_device_offline returned %d, "
Cornelia Hucke556bbb2007-07-27 12:29:19 +02001892 "device 0.%x.%04x\n",
1893 ret, cdev->private->dev_id.ssid,
1894 cdev->private->dev_id.devno);
Cornelia Huck9cd67422008-12-25 13:39:06 +01001895 /* Give up reference obtained in ccw_device_set_online(). */
1896 put_device(&cdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 }
1898 ccw_device_set_timeout(cdev, 0);
Heiko Carstensd2c993d2006-07-12 16:41:55 +02001899 cdev->drv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 return 0;
1901}
1902
Cornelia Huck958974fb2007-10-12 16:11:21 +02001903static void ccw_device_shutdown(struct device *dev)
1904{
1905 struct ccw_device *cdev;
1906
1907 cdev = to_ccwdev(dev);
1908 if (cdev->drv && cdev->drv->shutdown)
1909 cdev->drv->shutdown(cdev);
Cornelia Huck1842f2b2007-10-12 16:11:22 +02001910 disable_cmf(cdev);
Cornelia Huck958974fb2007-10-12 16:11:21 +02001911}
1912
Sebastian Ott823d4942009-06-16 10:30:20 +02001913static int ccw_device_pm_prepare(struct device *dev)
1914{
1915 struct ccw_device *cdev = to_ccwdev(dev);
1916
1917 if (work_pending(&cdev->private->kick_work))
1918 return -EAGAIN;
1919 /* Fail while device is being set online/offline. */
1920 if (atomic_read(&cdev->private->onoff))
1921 return -EAGAIN;
1922
1923 if (cdev->online && cdev->drv && cdev->drv->prepare)
1924 return cdev->drv->prepare(cdev);
1925
1926 return 0;
1927}
1928
1929static void ccw_device_pm_complete(struct device *dev)
1930{
1931 struct ccw_device *cdev = to_ccwdev(dev);
1932
1933 if (cdev->online && cdev->drv && cdev->drv->complete)
1934 cdev->drv->complete(cdev);
1935}
1936
1937static int ccw_device_pm_freeze(struct device *dev)
1938{
1939 struct ccw_device *cdev = to_ccwdev(dev);
1940 struct subchannel *sch = to_subchannel(cdev->dev.parent);
1941 int ret, cm_enabled;
1942
1943 /* Fail suspend while device is in transistional state. */
1944 if (!dev_fsm_final_state(cdev))
1945 return -EAGAIN;
1946 if (!cdev->online)
1947 return 0;
1948 if (cdev->drv && cdev->drv->freeze) {
1949 ret = cdev->drv->freeze(cdev);
1950 if (ret)
1951 return ret;
1952 }
1953
1954 spin_lock_irq(sch->lock);
1955 cm_enabled = cdev->private->cmb != NULL;
1956 spin_unlock_irq(sch->lock);
1957 if (cm_enabled) {
1958 /* Don't have the css write on memory. */
1959 ret = ccw_set_cmf(cdev, 0);
1960 if (ret)
1961 return ret;
1962 }
1963 /* From here on, disallow device driver I/O. */
1964 spin_lock_irq(sch->lock);
1965 ret = cio_disable_subchannel(sch);
1966 spin_unlock_irq(sch->lock);
1967
1968 return ret;
1969}
1970
1971static int ccw_device_pm_thaw(struct device *dev)
1972{
1973 struct ccw_device *cdev = to_ccwdev(dev);
1974 struct subchannel *sch = to_subchannel(cdev->dev.parent);
1975 int ret, cm_enabled;
1976
1977 if (!cdev->online)
1978 return 0;
1979
1980 spin_lock_irq(sch->lock);
1981 /* Allow device driver I/O again. */
1982 ret = cio_enable_subchannel(sch, (u32)(addr_t)sch);
1983 cm_enabled = cdev->private->cmb != NULL;
1984 spin_unlock_irq(sch->lock);
1985 if (ret)
1986 return ret;
1987
1988 if (cm_enabled) {
1989 ret = ccw_set_cmf(cdev, 1);
1990 if (ret)
1991 return ret;
1992 }
1993
1994 if (cdev->drv && cdev->drv->thaw)
1995 ret = cdev->drv->thaw(cdev);
1996
1997 return ret;
1998}
1999
2000static void __ccw_device_pm_restore(struct ccw_device *cdev)
2001{
2002 struct subchannel *sch = to_subchannel(cdev->dev.parent);
2003 int ret;
2004
2005 if (cio_is_console(sch->schid))
2006 goto out;
2007 /*
2008 * While we were sleeping, devices may have gone or become
2009 * available again. Kick re-detection.
2010 */
2011 spin_lock_irq(sch->lock);
2012 cdev->private->flags.resuming = 1;
2013 ret = ccw_device_recognition(cdev);
2014 spin_unlock_irq(sch->lock);
2015 if (ret) {
2016 CIO_MSG_EVENT(0, "Couldn't start recognition for device "
Sebastian Ottf0148242009-09-11 10:28:23 +02002017 "0.%x.%04x (ret=%d)\n",
2018 cdev->private->dev_id.ssid,
2019 cdev->private->dev_id.devno, ret);
Sebastian Ott823d4942009-06-16 10:30:20 +02002020 spin_lock_irq(sch->lock);
2021 cdev->private->state = DEV_STATE_DISCONNECTED;
2022 spin_unlock_irq(sch->lock);
2023 /* notify driver after the resume cb */
2024 goto out;
2025 }
2026 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev) ||
2027 cdev->private->state == DEV_STATE_DISCONNECTED);
2028
2029out:
2030 cdev->private->flags.resuming = 0;
2031}
2032
2033static int resume_handle_boxed(struct ccw_device *cdev)
2034{
2035 cdev->private->state = DEV_STATE_BOXED;
2036 if (ccw_device_notify(cdev, CIO_BOXED))
2037 return 0;
2038 ccw_device_schedule_sch_unregister(cdev);
2039 return -ENODEV;
2040}
2041
2042static int resume_handle_disc(struct ccw_device *cdev)
2043{
2044 cdev->private->state = DEV_STATE_DISCONNECTED;
2045 if (ccw_device_notify(cdev, CIO_GONE))
2046 return 0;
2047 ccw_device_schedule_sch_unregister(cdev);
2048 return -ENODEV;
2049}
2050
2051static int ccw_device_pm_restore(struct device *dev)
2052{
2053 struct ccw_device *cdev = to_ccwdev(dev);
2054 struct subchannel *sch = to_subchannel(cdev->dev.parent);
2055 int ret = 0, cm_enabled;
2056
2057 __ccw_device_pm_restore(cdev);
2058 spin_lock_irq(sch->lock);
2059 if (cio_is_console(sch->schid)) {
2060 cio_enable_subchannel(sch, (u32)(addr_t)sch);
2061 spin_unlock_irq(sch->lock);
2062 goto out_restore;
2063 }
2064 cdev->private->flags.donotify = 0;
2065 /* check recognition results */
2066 switch (cdev->private->state) {
2067 case DEV_STATE_OFFLINE:
2068 break;
2069 case DEV_STATE_BOXED:
2070 ret = resume_handle_boxed(cdev);
2071 spin_unlock_irq(sch->lock);
2072 if (ret)
2073 goto out;
2074 goto out_restore;
2075 case DEV_STATE_DISCONNECTED:
2076 goto out_disc_unlock;
2077 default:
2078 goto out_unreg_unlock;
2079 }
2080 /* check if the device id has changed */
2081 if (sch->schib.pmcw.dev != cdev->private->dev_id.devno) {
Sebastian Ottf0148242009-09-11 10:28:23 +02002082 CIO_MSG_EVENT(0, "resume: sch 0.%x.%04x: failed (devno "
2083 "changed from %04x to %04x)\n",
2084 sch->schid.ssid, sch->schid.sch_no,
Sebastian Ott823d4942009-06-16 10:30:20 +02002085 cdev->private->dev_id.devno,
2086 sch->schib.pmcw.dev);
2087 goto out_unreg_unlock;
2088 }
2089 /* check if the device type has changed */
2090 if (!ccw_device_test_sense_data(cdev)) {
2091 ccw_device_update_sense_data(cdev);
2092 PREPARE_WORK(&cdev->private->kick_work,
2093 ccw_device_do_unbind_bind);
2094 queue_work(ccw_device_work, &cdev->private->kick_work);
2095 ret = -ENODEV;
2096 goto out_unlock;
2097 }
2098 if (!cdev->online) {
2099 ret = 0;
2100 goto out_unlock;
2101 }
2102 ret = ccw_device_online(cdev);
2103 if (ret)
2104 goto out_disc_unlock;
2105
2106 cm_enabled = cdev->private->cmb != NULL;
2107 spin_unlock_irq(sch->lock);
2108
2109 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
2110 if (cdev->private->state != DEV_STATE_ONLINE) {
2111 spin_lock_irq(sch->lock);
2112 goto out_disc_unlock;
2113 }
2114 if (cm_enabled) {
2115 ret = ccw_set_cmf(cdev, 1);
2116 if (ret) {
Sebastian Ottf0148242009-09-11 10:28:23 +02002117 CIO_MSG_EVENT(2, "resume: cdev 0.%x.%04x: cmf failed "
2118 "(rc=%d)\n", cdev->private->dev_id.ssid,
2119 cdev->private->dev_id.devno, ret);
Sebastian Ott823d4942009-06-16 10:30:20 +02002120 ret = 0;
2121 }
2122 }
2123
2124out_restore:
2125 if (cdev->online && cdev->drv && cdev->drv->restore)
2126 ret = cdev->drv->restore(cdev);
2127out:
2128 return ret;
2129
2130out_disc_unlock:
2131 ret = resume_handle_disc(cdev);
2132 spin_unlock_irq(sch->lock);
2133 if (ret)
2134 return ret;
2135 goto out_restore;
2136
2137out_unreg_unlock:
2138 ccw_device_schedule_sch_unregister(cdev);
2139 ret = -ENODEV;
2140out_unlock:
2141 spin_unlock_irq(sch->lock);
2142 return ret;
2143}
2144
2145static struct dev_pm_ops ccw_pm_ops = {
2146 .prepare = ccw_device_pm_prepare,
2147 .complete = ccw_device_pm_complete,
2148 .freeze = ccw_device_pm_freeze,
2149 .thaw = ccw_device_pm_thaw,
2150 .restore = ccw_device_pm_restore,
2151};
2152
Cornelia Huck8bbace72006-01-11 10:56:22 +01002153struct bus_type ccw_bus_type = {
2154 .name = "ccw",
2155 .match = ccw_bus_match,
2156 .uevent = ccw_uevent,
2157 .probe = ccw_device_probe,
2158 .remove = ccw_device_remove,
Cornelia Huck958974fb2007-10-12 16:11:21 +02002159 .shutdown = ccw_device_shutdown,
Sebastian Ott823d4942009-06-16 10:30:20 +02002160 .pm = &ccw_pm_ops,
Cornelia Huck8bbace72006-01-11 10:56:22 +01002161};
2162
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +02002163/**
2164 * ccw_driver_register() - register a ccw driver
2165 * @cdriver: driver to be registered
2166 *
2167 * This function is mainly a wrapper around driver_register().
2168 * Returns:
2169 * %0 on success and a negative error value on failure.
2170 */
2171int ccw_driver_register(struct ccw_driver *cdriver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172{
2173 struct device_driver *drv = &cdriver->driver;
2174
2175 drv->bus = &ccw_bus_type;
2176 drv->name = cdriver->name;
Cornelia Huck4beee642008-01-26 14:10:47 +01002177 drv->owner = cdriver->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178
2179 return driver_register(drv);
2180}
2181
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +02002182/**
2183 * ccw_driver_unregister() - deregister a ccw driver
2184 * @cdriver: driver to be deregistered
2185 *
2186 * This function is mainly a wrapper around driver_unregister().
2187 */
2188void ccw_driver_unregister(struct ccw_driver *cdriver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189{
2190 driver_unregister(&cdriver->driver);
2191}
2192
Cornelia Hucka8237fc2006-01-06 00:19:21 -08002193/* Helper func for qdio. */
2194struct subchannel_id
2195ccw_device_get_subchannel_id(struct ccw_device *cdev)
2196{
2197 struct subchannel *sch;
2198
2199 sch = to_subchannel(cdev->dev.parent);
2200 return sch->schid;
2201}
2202
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203MODULE_LICENSE("GPL");
2204EXPORT_SYMBOL(ccw_device_set_online);
2205EXPORT_SYMBOL(ccw_device_set_offline);
2206EXPORT_SYMBOL(ccw_driver_register);
2207EXPORT_SYMBOL(ccw_driver_unregister);
2208EXPORT_SYMBOL(get_ccwdev_by_busid);
2209EXPORT_SYMBOL(ccw_bus_type);
2210EXPORT_SYMBOL(ccw_device_work);
Cornelia Hucka8237fc2006-01-06 00:19:21 -08002211EXPORT_SYMBOL_GPL(ccw_device_get_subchannel_id);