blob: 64bd79ac25a78e861afe8201b8ee4379ef8bed63 [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 Huckd7b5a4c2006-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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173static int __init
174init_ccw_bus_type (void)
175{
176 int ret;
177
178 init_waitqueue_head(&ccw_device_init_wq);
179 atomic_set(&ccw_device_init_count, 0);
Peter Oberparleiter90ab1332008-01-26 14:10:52 +0100180 setup_timer(&recovery_timer, recovery_func, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
182 ccw_device_work = create_singlethread_workqueue("cio");
183 if (!ccw_device_work)
184 return -ENOMEM; /* FIXME: better errno ? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 slow_path_wq = create_singlethread_workqueue("kslowcrw");
186 if (!slow_path_wq) {
187 ret = -ENOMEM; /* FIXME: better errno ? */
188 goto out_err;
189 }
190 if ((ret = bus_register (&ccw_bus_type)))
191 goto out_err;
192
Cornelia Huck25b7bb52008-01-26 14:10:41 +0100193 ret = css_driver_register(&io_subchannel_driver);
194 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 goto out_err;
196
197 wait_event(ccw_device_init_wq,
198 atomic_read(&ccw_device_init_count) == 0);
199 flush_workqueue(ccw_device_work);
200 return 0;
201out_err:
202 if (ccw_device_work)
203 destroy_workqueue(ccw_device_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 if (slow_path_wq)
205 destroy_workqueue(slow_path_wq);
206 return ret;
207}
208
209static void __exit
210cleanup_ccw_bus_type (void)
211{
Cornelia Huck25b7bb52008-01-26 14:10:41 +0100212 css_driver_unregister(&io_subchannel_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 bus_unregister(&ccw_bus_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 destroy_workqueue(ccw_device_work);
215}
216
217subsys_initcall(init_ccw_bus_type);
218module_exit(cleanup_ccw_bus_type);
219
220/************************ device handling **************************/
221
222/*
223 * A ccw_device has some interfaces in sysfs in addition to the
224 * standard ones.
225 * The following entries are designed to export the information which
226 * resided in 2.4 in /proc/subchannels. Subchannel and device number
227 * are obvious, so they don't have an entry :)
228 * TODO: Split chpids and pimpampom up? Where is "in use" in the tree?
229 */
230static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400231chpids_show (struct device * dev, struct device_attribute *attr, char * buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232{
233 struct subchannel *sch = to_subchannel(dev);
Peter Oberparleiter7ad6a242007-04-27 16:01:35 +0200234 struct chsc_ssd_info *ssd = &sch->ssd_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 ssize_t ret = 0;
236 int chp;
Peter Oberparleiter7ad6a242007-04-27 16:01:35 +0200237 int mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
Peter Oberparleiter7ad6a242007-04-27 16:01:35 +0200239 for (chp = 0; chp < 8; chp++) {
240 mask = 0x80 >> chp;
241 if (ssd->path_mask & mask)
242 ret += sprintf(buf + ret, "%02x ", ssd->chpid[chp].id);
243 else
244 ret += sprintf(buf + ret, "00 ");
245 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 ret += sprintf (buf+ret, "\n");
247 return min((ssize_t)PAGE_SIZE, ret);
248}
249
250static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400251pimpampom_show (struct device * dev, struct device_attribute *attr, char * buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252{
253 struct subchannel *sch = to_subchannel(dev);
254 struct pmcw *pmcw = &sch->schib.pmcw;
255
256 return sprintf (buf, "%02x %02x %02x\n",
257 pmcw->pim, pmcw->pam, pmcw->pom);
258}
259
260static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400261devtype_show (struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262{
263 struct ccw_device *cdev = to_ccwdev(dev);
264 struct ccw_device_id *id = &(cdev->id);
265
266 if (id->dev_type != 0)
267 return sprintf(buf, "%04x/%02x\n",
268 id->dev_type, id->dev_model);
269 else
270 return sprintf(buf, "n/a\n");
271}
272
273static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400274cutype_show (struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275{
276 struct ccw_device *cdev = to_ccwdev(dev);
277 struct ccw_device_id *id = &(cdev->id);
278
279 return sprintf(buf, "%04x/%02x\n",
280 id->cu_type, id->cu_model);
281}
282
283static ssize_t
Bastian Blankf1fc78a2005-10-30 15:00:12 -0800284modalias_show (struct device *dev, struct device_attribute *attr, char *buf)
285{
286 struct ccw_device *cdev = to_ccwdev(dev);
287 struct ccw_device_id *id = &(cdev->id);
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +0200288 int len;
Bastian Blankf1fc78a2005-10-30 15:00:12 -0800289
Cornelia Huck086a6c62007-07-17 13:36:08 +0200290 len = snprint_alias(buf, PAGE_SIZE, id, "\n");
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +0200291
292 return len > PAGE_SIZE ? PAGE_SIZE : len;
Bastian Blankf1fc78a2005-10-30 15:00:12 -0800293}
294
295static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400296online_show (struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297{
298 struct ccw_device *cdev = to_ccwdev(dev);
299
300 return sprintf(buf, cdev->online ? "1\n" : "0\n");
301}
302
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100303int ccw_device_is_orphan(struct ccw_device *cdev)
304{
305 return sch_is_pseudo_sch(to_subchannel(cdev->dev.parent));
306}
307
Cornelia Huckef995162007-04-27 16:01:39 +0200308static void ccw_device_unregister(struct ccw_device *cdev)
Cornelia Huck7674da72006-12-08 15:54:21 +0100309{
Cornelia Huck7674da72006-12-08 15:54:21 +0100310 if (test_and_clear_bit(1, &cdev->private->registered))
Cornelia Huckef995162007-04-27 16:01:39 +0200311 device_del(&cdev->dev);
Cornelia Huck7674da72006-12-08 15:54:21 +0100312}
313
Peter Oberparleiter46fbe4e2008-10-10 21:33:05 +0200314static void ccw_device_remove_orphan_cb(struct work_struct *work)
Cornelia Huck59a8a6e2007-05-31 17:38:06 +0200315{
Peter Oberparleiter46fbe4e2008-10-10 21:33:05 +0200316 struct ccw_device_private *priv;
317 struct ccw_device *cdev;
Cornelia Huck59a8a6e2007-05-31 17:38:06 +0200318
Peter Oberparleiter46fbe4e2008-10-10 21:33:05 +0200319 priv = container_of(work, struct ccw_device_private, kick_work);
320 cdev = priv->cdev;
Cornelia Huck59a8a6e2007-05-31 17:38:06 +0200321 ccw_device_unregister(cdev);
322 put_device(&cdev->dev);
Peter Oberparleiter46fbe4e2008-10-10 21:33:05 +0200323 /* Release cdev reference for workqueue processing. */
324 put_device(&cdev->dev);
Cornelia Huck59a8a6e2007-05-31 17:38:06 +0200325}
326
Cornelia Huck59a8a6e2007-05-31 17:38:06 +0200327static void
328ccw_device_remove_disconnected(struct ccw_device *cdev)
329{
330 unsigned long flags;
Cornelia Huck59a8a6e2007-05-31 17:38:06 +0200331
332 /*
333 * Forced offline in disconnected state means
334 * 'throw away device'.
335 */
Peter Oberparleiter46fbe4e2008-10-10 21:33:05 +0200336 /* Get cdev reference for workqueue processing. */
337 if (!get_device(&cdev->dev))
338 return;
Cornelia Huck59a8a6e2007-05-31 17:38:06 +0200339 if (ccw_device_is_orphan(cdev)) {
340 /*
341 * Deregister ccw device.
342 * Unfortunately, we cannot do this directly from the
343 * attribute method.
344 */
345 spin_lock_irqsave(cdev->ccwlock, flags);
346 cdev->private->state = DEV_STATE_NOT_OPER;
347 spin_unlock_irqrestore(cdev->ccwlock, flags);
Peter Oberparleiter46fbe4e2008-10-10 21:33:05 +0200348 PREPARE_WORK(&cdev->private->kick_work,
349 ccw_device_remove_orphan_cb);
Sebastian Ottc4621a62009-03-31 19:16:04 +0200350 queue_work(slow_path_wq, &cdev->private->kick_work);
Peter Oberparleiter46fbe4e2008-10-10 21:33:05 +0200351 } else
352 /* Deregister subchannel, which will kill the ccw device. */
Sebastian Ottc4621a62009-03-31 19:16:04 +0200353 ccw_device_schedule_sch_unregister(cdev);
Cornelia Huck59a8a6e2007-05-31 17:38:06 +0200354}
355
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +0200356/**
357 * ccw_device_set_offline() - disable a ccw device for I/O
358 * @cdev: target ccw device
359 *
360 * This function calls the driver's set_offline() function for @cdev, if
361 * given, and then disables @cdev.
362 * Returns:
363 * %0 on success and a negative error value on failure.
364 * Context:
365 * enabled, ccw device lock not held
366 */
367int ccw_device_set_offline(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368{
369 int ret;
370
371 if (!cdev)
372 return -ENODEV;
373 if (!cdev->online || !cdev->drv)
374 return -EINVAL;
375
376 if (cdev->drv->set_offline) {
377 ret = cdev->drv->set_offline(cdev);
378 if (ret != 0)
379 return ret;
380 }
381 cdev->online = 0;
382 spin_lock_irq(cdev->ccwlock);
383 ret = ccw_device_offline(cdev);
384 if (ret == -ENODEV) {
385 if (cdev->private->state != DEV_STATE_NOT_OPER) {
386 cdev->private->state = DEV_STATE_OFFLINE;
387 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
388 }
389 spin_unlock_irq(cdev->ccwlock);
Cornelia Huck9cd67422008-12-25 13:39:06 +0100390 /* Give up reference from ccw_device_set_online(). */
391 put_device(&cdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 return ret;
393 }
394 spin_unlock_irq(cdev->ccwlock);
Cornelia Huck9cd67422008-12-25 13:39:06 +0100395 if (ret == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
Cornelia Huck9cd67422008-12-25 13:39:06 +0100397 /* Give up reference from ccw_device_set_online(). */
398 put_device(&cdev->dev);
399 } else {
Michael Ernst139b83d2008-05-07 09:22:54 +0200400 CIO_MSG_EVENT(0, "ccw_device_offline returned %d, "
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200401 "device 0.%x.%04x\n",
402 ret, cdev->private->dev_id.ssid,
403 cdev->private->dev_id.devno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 cdev->online = 1;
405 }
Cornelia Huck9cd67422008-12-25 13:39:06 +0100406 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407}
408
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +0200409/**
410 * ccw_device_set_online() - enable a ccw device for I/O
411 * @cdev: target ccw device
412 *
413 * This function first enables @cdev and then calls the driver's set_online()
414 * function for @cdev, if given. If set_online() returns an error, @cdev is
415 * disabled again.
416 * Returns:
417 * %0 on success and a negative error value on failure.
418 * Context:
419 * enabled, ccw device lock not held
420 */
421int ccw_device_set_online(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422{
423 int ret;
424
425 if (!cdev)
426 return -ENODEV;
427 if (cdev->online || !cdev->drv)
428 return -EINVAL;
Cornelia Huck9cd67422008-12-25 13:39:06 +0100429 /* Hold on to an extra reference while device is online. */
430 if (!get_device(&cdev->dev))
431 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
433 spin_lock_irq(cdev->ccwlock);
434 ret = ccw_device_online(cdev);
435 spin_unlock_irq(cdev->ccwlock);
436 if (ret == 0)
437 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
438 else {
Michael Ernst139b83d2008-05-07 09:22:54 +0200439 CIO_MSG_EVENT(0, "ccw_device_online returned %d, "
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200440 "device 0.%x.%04x\n",
441 ret, cdev->private->dev_id.ssid,
442 cdev->private->dev_id.devno);
Cornelia Huck9cd67422008-12-25 13:39:06 +0100443 /* Give up online reference since onlining failed. */
444 put_device(&cdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 return ret;
446 }
Cornelia Huck9cd67422008-12-25 13:39:06 +0100447 if (cdev->private->state != DEV_STATE_ONLINE) {
448 /* Give up online reference since onlining failed. */
449 put_device(&cdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 return -ENODEV;
Cornelia Huck9cd67422008-12-25 13:39:06 +0100451 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 if (!cdev->drv->set_online || cdev->drv->set_online(cdev) == 0) {
453 cdev->online = 1;
454 return 0;
455 }
456 spin_lock_irq(cdev->ccwlock);
457 ret = ccw_device_offline(cdev);
458 spin_unlock_irq(cdev->ccwlock);
459 if (ret == 0)
460 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200461 else
Michael Ernst139b83d2008-05-07 09:22:54 +0200462 CIO_MSG_EVENT(0, "ccw_device_offline returned %d, "
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200463 "device 0.%x.%04x\n",
464 ret, cdev->private->dev_id.ssid,
465 cdev->private->dev_id.devno);
Cornelia Huck9cd67422008-12-25 13:39:06 +0100466 /* Give up online reference since onlining failed. */
467 put_device(&cdev->dev);
Cornelia Huck6cadb782006-02-17 13:52:49 -0800468 return (ret == 0) ? -ENODEV : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469}
470
Sebastian Otte74fe0c2009-03-26 15:24:08 +0100471static int online_store_handle_offline(struct ccw_device *cdev)
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200472{
473 if (cdev->private->state == DEV_STATE_DISCONNECTED)
474 ccw_device_remove_disconnected(cdev);
Sebastian Otte74fe0c2009-03-26 15:24:08 +0100475 else if (cdev->online && cdev->drv && cdev->drv->set_offline)
476 return ccw_device_set_offline(cdev);
477 return 0;
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200478}
479
480static int online_store_recog_and_online(struct ccw_device *cdev)
481{
482 int ret;
483
484 /* Do device recognition, if needed. */
Sebastian Ott99f6a5702009-03-31 19:16:07 +0200485 if (cdev->private->state == DEV_STATE_BOXED) {
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200486 ret = ccw_device_recognition(cdev);
487 if (ret) {
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200488 CIO_MSG_EVENT(0, "Couldn't start recognition "
489 "for device 0.%x.%04x (ret=%d)\n",
490 cdev->private->dev_id.ssid,
491 cdev->private->dev_id.devno, ret);
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200492 return ret;
493 }
494 wait_event(cdev->private->wait_q,
495 cdev->private->flags.recog_done);
Sebastian Ott156013f2009-03-31 19:16:03 +0200496 if (cdev->private->state != DEV_STATE_OFFLINE)
497 /* recognition failed */
498 return -EAGAIN;
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200499 }
500 if (cdev->drv && cdev->drv->set_online)
501 ccw_device_set_online(cdev);
502 return 0;
503}
Sebastian Ott156013f2009-03-31 19:16:03 +0200504
Michael Ernstc78aa6c2008-07-14 09:59:22 +0200505static int online_store_handle_online(struct ccw_device *cdev, int force)
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200506{
507 int ret;
508
509 ret = online_store_recog_and_online(cdev);
Sebastian Ott156013f2009-03-31 19:16:03 +0200510 if (ret && !force)
Michael Ernstc78aa6c2008-07-14 09:59:22 +0200511 return ret;
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200512 if (force && cdev->private->state == DEV_STATE_BOXED) {
513 ret = ccw_device_stlck(cdev);
Michael Ernstc78aa6c2008-07-14 09:59:22 +0200514 if (ret)
515 return ret;
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200516 if (cdev->id.cu_type == 0)
517 cdev->private->state = DEV_STATE_NOT_OPER;
Sebastian Ott156013f2009-03-31 19:16:03 +0200518 ret = online_store_recog_and_online(cdev);
519 if (ret)
520 return ret;
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200521 }
Michael Ernstc78aa6c2008-07-14 09:59:22 +0200522 return 0;
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200523}
524
525static ssize_t online_store (struct device *dev, struct device_attribute *attr,
526 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527{
528 struct ccw_device *cdev = to_ccwdev(dev);
Cornelia Huck2f972202008-04-30 13:38:33 +0200529 int force, ret;
530 unsigned long i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
Sebastian Ottb5cd99e2009-03-31 19:16:06 +0200532 if ((cdev->private->state != DEV_STATE_OFFLINE &&
533 cdev->private->state != DEV_STATE_ONLINE &&
534 cdev->private->state != DEV_STATE_BOXED &&
535 cdev->private->state != DEV_STATE_DISCONNECTED) ||
536 atomic_cmpxchg(&cdev->private->onoff, 0, 1) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 return -EAGAIN;
538
539 if (cdev->drv && !try_module_get(cdev->drv->owner)) {
540 atomic_set(&cdev->private->onoff, 0);
541 return -EINVAL;
542 }
543 if (!strncmp(buf, "force\n", count)) {
544 force = 1;
545 i = 1;
Cornelia Huck2f972202008-04-30 13:38:33 +0200546 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 } else {
548 force = 0;
Cornelia Huck2f972202008-04-30 13:38:33 +0200549 ret = strict_strtoul(buf, 16, &i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 }
Cornelia Huck2f972202008-04-30 13:38:33 +0200551 if (ret)
552 goto out;
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200553 switch (i) {
554 case 0:
Sebastian Otte74fe0c2009-03-26 15:24:08 +0100555 ret = online_store_handle_offline(cdev);
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200556 break;
557 case 1:
Michael Ernstc78aa6c2008-07-14 09:59:22 +0200558 ret = online_store_handle_online(cdev, force);
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200559 break;
560 default:
Cornelia Huck2f972202008-04-30 13:38:33 +0200561 ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 }
Cornelia Huck2f972202008-04-30 13:38:33 +0200563out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 if (cdev->drv)
565 module_put(cdev->drv->owner);
566 atomic_set(&cdev->private->onoff, 0);
Sebastian Otte74fe0c2009-03-26 15:24:08 +0100567 return (ret < 0) ? ret : count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568}
569
570static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400571available_show (struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572{
573 struct ccw_device *cdev = to_ccwdev(dev);
574 struct subchannel *sch;
575
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100576 if (ccw_device_is_orphan(cdev))
577 return sprintf(buf, "no device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 switch (cdev->private->state) {
579 case DEV_STATE_BOXED:
580 return sprintf(buf, "boxed\n");
581 case DEV_STATE_DISCONNECTED:
582 case DEV_STATE_DISCONNECTED_SENSE_ID:
583 case DEV_STATE_NOT_OPER:
584 sch = to_subchannel(dev->parent);
585 if (!sch->lpm)
586 return sprintf(buf, "no path\n");
587 else
588 return sprintf(buf, "no device\n");
589 default:
590 /* All other states considered fine. */
591 return sprintf(buf, "good\n");
592 }
593}
594
595static DEVICE_ATTR(chpids, 0444, chpids_show, NULL);
596static DEVICE_ATTR(pimpampom, 0444, pimpampom_show, NULL);
597static DEVICE_ATTR(devtype, 0444, devtype_show, NULL);
598static DEVICE_ATTR(cutype, 0444, cutype_show, NULL);
Bastian Blankf1fc78a2005-10-30 15:00:12 -0800599static DEVICE_ATTR(modalias, 0444, modalias_show, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600static DEVICE_ATTR(online, 0644, online_show, online_store);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601static DEVICE_ATTR(availability, 0444, available_show, NULL);
602
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200603static struct attribute *io_subchannel_attrs[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 &dev_attr_chpids.attr,
605 &dev_attr_pimpampom.attr,
606 NULL,
607};
608
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200609static struct attribute_group io_subchannel_attr_group = {
610 .attrs = io_subchannel_attrs,
Cornelia Huck529192f2006-12-08 15:55:57 +0100611};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
613static struct attribute * ccwdev_attrs[] = {
614 &dev_attr_devtype.attr,
615 &dev_attr_cutype.attr,
Bastian Blankf1fc78a2005-10-30 15:00:12 -0800616 &dev_attr_modalias.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 &dev_attr_online.attr,
618 &dev_attr_cmb_enable.attr,
619 &dev_attr_availability.attr,
620 NULL,
621};
622
623static struct attribute_group ccwdev_attr_group = {
624 .attrs = ccwdev_attrs,
625};
626
Cornelia Huckf7e5d672007-05-10 15:45:43 +0200627static struct attribute_group *ccwdev_attr_groups[] = {
Cornelia Huckef995162007-04-27 16:01:39 +0200628 &ccwdev_attr_group,
629 NULL,
630};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
632/* this is a simple abstraction for device_register that sets the
633 * correct bus type and adds the bus specific files */
Cornelia Huck3c9da7b2006-10-27 12:39:33 +0200634static int ccw_device_register(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635{
636 struct device *dev = &cdev->dev;
637 int ret;
638
639 dev->bus = &ccw_bus_type;
640
641 if ((ret = device_add(dev)))
642 return ret;
643
644 set_bit(1, &cdev->private->registered);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 return ret;
646}
647
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700648struct match_data {
Cornelia Huck78964262006-10-11 15:31:38 +0200649 struct ccw_dev_id dev_id;
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700650 struct ccw_device * sibling;
651};
652
653static int
654match_devno(struct device * dev, void * data)
655{
Cornelia Huck78964262006-10-11 15:31:38 +0200656 struct match_data * d = data;
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700657 struct ccw_device * cdev;
658
659 cdev = to_ccwdev(dev);
660 if ((cdev->private->state == DEV_STATE_DISCONNECTED) &&
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100661 !ccw_device_is_orphan(cdev) &&
Cornelia Huck78964262006-10-11 15:31:38 +0200662 ccw_dev_id_is_equal(&cdev->private->dev_id, &d->dev_id) &&
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100663 (cdev != d->sibling))
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700664 return 1;
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700665 return 0;
666}
667
Cornelia Huck78964262006-10-11 15:31:38 +0200668static struct ccw_device * get_disc_ccwdev_by_dev_id(struct ccw_dev_id *dev_id,
669 struct ccw_device *sibling)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 struct device *dev;
Heiko Carstens292888c2006-08-30 14:33:35 +0200672 struct match_data data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
Cornelia Huck78964262006-10-11 15:31:38 +0200674 data.dev_id = *dev_id;
Heiko Carstens292888c2006-08-30 14:33:35 +0200675 data.sibling = sibling;
Cornelia Hucke5945b42005-10-11 08:28:59 -0700676 dev = bus_find_device(&ccw_bus_type, NULL, &data, match_devno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700678 return dev ? to_ccwdev(dev) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679}
680
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100681static int match_orphan(struct device *dev, void *data)
682{
683 struct ccw_dev_id *dev_id;
684 struct ccw_device *cdev;
685
686 dev_id = data;
687 cdev = to_ccwdev(dev);
688 return ccw_dev_id_is_equal(&cdev->private->dev_id, dev_id);
689}
690
691static struct ccw_device *
692get_orphaned_ccwdev_by_dev_id(struct channel_subsystem *css,
693 struct ccw_dev_id *dev_id)
694{
695 struct device *dev;
696
697 dev = device_find_child(&css->pseudo_subchannel->dev, dev_id,
698 match_orphan);
699
700 return dev ? to_ccwdev(dev) : NULL;
701}
702
Cornelia Huckeb32ae82009-03-26 15:24:05 +0100703void ccw_device_do_unbind_bind(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704{
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100705 struct ccw_device_private *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 struct ccw_device *cdev;
707 struct subchannel *sch;
Cornelia Huckeb32ae82009-03-26 15:24:05 +0100708 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100710 priv = container_of(work, struct ccw_device_private, kick_work);
711 cdev = priv->cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 sch = to_subchannel(cdev->dev.parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
Cornelia Huckeb32ae82009-03-26 15:24:05 +0100714 if (test_bit(1, &cdev->private->registered)) {
715 device_release_driver(&cdev->dev);
716 ret = device_attach(&cdev->dev);
717 WARN_ON(ret == -ENODEV);
718 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719}
720
721static void
722ccw_device_release(struct device *dev)
723{
724 struct ccw_device *cdev;
725
726 cdev = to_ccwdev(dev);
Cornelia Huck6eff2082008-12-25 13:39:07 +0100727 /* Release reference of parent subchannel. */
728 put_device(cdev->dev.parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 kfree(cdev->private);
730 kfree(cdev);
731}
732
Cornelia Huck7674da72006-12-08 15:54:21 +0100733static struct ccw_device * io_subchannel_allocate_dev(struct subchannel *sch)
734{
735 struct ccw_device *cdev;
736
737 cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
738 if (cdev) {
739 cdev->private = kzalloc(sizeof(struct ccw_device_private),
740 GFP_KERNEL | GFP_DMA);
741 if (cdev->private)
742 return cdev;
743 }
744 kfree(cdev);
745 return ERR_PTR(-ENOMEM);
746}
747
748static int io_subchannel_initialize_dev(struct subchannel *sch,
749 struct ccw_device *cdev)
750{
751 cdev->private->cdev = cdev;
752 atomic_set(&cdev->private->onoff, 0);
753 cdev->dev.parent = &sch->dev;
754 cdev->dev.release = ccw_device_release;
Heiko Carstens33583c32007-11-05 11:10:07 +0100755 INIT_WORK(&cdev->private->kick_work, NULL);
Cornelia Huckef995162007-04-27 16:01:39 +0200756 cdev->dev.groups = ccwdev_attr_groups;
Cornelia Huck7674da72006-12-08 15:54:21 +0100757 /* Do first half of device_register. */
758 device_initialize(&cdev->dev);
759 if (!get_device(&sch->dev)) {
Cornelia Huck283fdd02008-12-25 13:39:10 +0100760 /* Release reference from device_initialize(). */
761 put_device(&cdev->dev);
Cornelia Huck7674da72006-12-08 15:54:21 +0100762 return -ENODEV;
763 }
764 return 0;
765}
766
767static struct ccw_device * io_subchannel_create_ccwdev(struct subchannel *sch)
768{
769 struct ccw_device *cdev;
770 int ret;
771
772 cdev = io_subchannel_allocate_dev(sch);
773 if (!IS_ERR(cdev)) {
774 ret = io_subchannel_initialize_dev(sch, cdev);
775 if (ret) {
776 kfree(cdev);
777 cdev = ERR_PTR(ret);
778 }
779 }
780 return cdev;
781}
782
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100783static int io_subchannel_recog(struct ccw_device *, struct subchannel *);
784
785static void sch_attach_device(struct subchannel *sch,
786 struct ccw_device *cdev)
787{
Cornelia Huck82b7ac02007-04-27 16:01:36 +0200788 css_update_ssd_info(sch);
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100789 spin_lock_irq(sch->lock);
Cornelia Huckdb6a6422008-01-26 14:10:46 +0100790 sch_set_cdev(sch, cdev);
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100791 cdev->private->schid = sch->schid;
792 cdev->ccwlock = sch->lock;
Cornelia Huckc820de32008-07-14 09:58:45 +0200793 ccw_device_trigger_reprobe(cdev);
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100794 spin_unlock_irq(sch->lock);
795}
796
797static void sch_attach_disconnected_device(struct subchannel *sch,
798 struct ccw_device *cdev)
799{
800 struct subchannel *other_sch;
801 int ret;
802
Cornelia Huck6eff2082008-12-25 13:39:07 +0100803 /* Get reference for new parent. */
804 if (!get_device(&sch->dev))
805 return;
806 other_sch = to_subchannel(cdev->dev.parent);
807 /* Note: device_move() changes cdev->dev.parent */
Cornelia Huckffa6a702009-03-04 12:44:00 +0100808 ret = device_move(&cdev->dev, &sch->dev, DPM_ORDER_PARENT_BEFORE_DEV);
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100809 if (ret) {
Michael Ernst139b83d2008-05-07 09:22:54 +0200810 CIO_MSG_EVENT(0, "Moving disconnected device 0.%x.%04x failed "
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100811 "(ret=%d)!\n", cdev->private->dev_id.ssid,
812 cdev->private->dev_id.devno, ret);
Cornelia Huck6eff2082008-12-25 13:39:07 +0100813 /* Put reference for new parent. */
814 put_device(&sch->dev);
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100815 return;
816 }
Cornelia Huckdb6a6422008-01-26 14:10:46 +0100817 sch_set_cdev(other_sch, NULL);
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100818 /* No need to keep a subchannel without ccw device around. */
819 css_sch_device_unregister(other_sch);
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100820 sch_attach_device(sch, cdev);
Cornelia Huck6eff2082008-12-25 13:39:07 +0100821 /* Put reference for old parent. */
822 put_device(&other_sch->dev);
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100823}
824
825static void sch_attach_orphaned_device(struct subchannel *sch,
826 struct ccw_device *cdev)
827{
828 int ret;
Cornelia Huck6eff2082008-12-25 13:39:07 +0100829 struct subchannel *pseudo_sch;
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100830
Cornelia Huck6eff2082008-12-25 13:39:07 +0100831 /* Get reference for new parent. */
832 if (!get_device(&sch->dev))
833 return;
834 pseudo_sch = to_subchannel(cdev->dev.parent);
835 /*
836 * Try to move the ccw device to its new subchannel.
837 * Note: device_move() changes cdev->dev.parent
838 */
Cornelia Huckffa6a702009-03-04 12:44:00 +0100839 ret = device_move(&cdev->dev, &sch->dev, DPM_ORDER_PARENT_BEFORE_DEV);
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100840 if (ret) {
841 CIO_MSG_EVENT(0, "Moving device 0.%x.%04x from orphanage "
842 "failed (ret=%d)!\n",
843 cdev->private->dev_id.ssid,
844 cdev->private->dev_id.devno, ret);
Cornelia Huck6eff2082008-12-25 13:39:07 +0100845 /* Put reference for new parent. */
846 put_device(&sch->dev);
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100847 return;
848 }
849 sch_attach_device(sch, cdev);
Cornelia Huck6eff2082008-12-25 13:39:07 +0100850 /* Put reference on pseudo subchannel. */
851 put_device(&pseudo_sch->dev);
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100852}
853
854static void sch_create_and_recog_new_device(struct subchannel *sch)
855{
856 struct ccw_device *cdev;
857
858 /* Need to allocate a new ccw device. */
859 cdev = io_subchannel_create_ccwdev(sch);
860 if (IS_ERR(cdev)) {
861 /* OK, we did everything we could... */
862 css_sch_device_unregister(sch);
863 return;
864 }
865 spin_lock_irq(sch->lock);
Cornelia Huckdb6a6422008-01-26 14:10:46 +0100866 sch_set_cdev(sch, cdev);
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100867 spin_unlock_irq(sch->lock);
868 /* Start recognition for the new ccw device. */
869 if (io_subchannel_recog(cdev, sch)) {
870 spin_lock_irq(sch->lock);
Cornelia Huckdb6a6422008-01-26 14:10:46 +0100871 sch_set_cdev(sch, NULL);
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100872 spin_unlock_irq(sch->lock);
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100873 css_sch_device_unregister(sch);
Cornelia Huck6eff2082008-12-25 13:39:07 +0100874 /* Put reference from io_subchannel_create_ccwdev(). */
875 put_device(&sch->dev);
876 /* Give up initial reference. */
877 put_device(&cdev->dev);
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100878 }
879}
880
881
882void ccw_device_move_to_orphanage(struct work_struct *work)
883{
884 struct ccw_device_private *priv;
885 struct ccw_device *cdev;
886 struct ccw_device *replacing_cdev;
887 struct subchannel *sch;
888 int ret;
889 struct channel_subsystem *css;
890 struct ccw_dev_id dev_id;
891
892 priv = container_of(work, struct ccw_device_private, kick_work);
893 cdev = priv->cdev;
894 sch = to_subchannel(cdev->dev.parent);
895 css = to_css(sch->dev.parent);
896 dev_id.devno = sch->schib.pmcw.dev;
897 dev_id.ssid = sch->schid.ssid;
898
Cornelia Huck6eff2082008-12-25 13:39:07 +0100899 /* Increase refcount for pseudo subchannel. */
900 get_device(&css->pseudo_subchannel->dev);
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100901 /*
902 * Move the orphaned ccw device to the orphanage so the replacing
903 * ccw device can take its place on the subchannel.
Cornelia Huck6eff2082008-12-25 13:39:07 +0100904 * Note: device_move() changes cdev->dev.parent
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100905 */
Cornelia Huckffa6a702009-03-04 12:44:00 +0100906 ret = device_move(&cdev->dev, &css->pseudo_subchannel->dev,
907 DPM_ORDER_NONE);
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100908 if (ret) {
909 CIO_MSG_EVENT(0, "Moving device 0.%x.%04x to orphanage failed "
910 "(ret=%d)!\n", cdev->private->dev_id.ssid,
911 cdev->private->dev_id.devno, ret);
Cornelia Huck6eff2082008-12-25 13:39:07 +0100912 /* Decrease refcount for pseudo subchannel again. */
913 put_device(&css->pseudo_subchannel->dev);
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100914 return;
915 }
916 cdev->ccwlock = css->pseudo_subchannel->lock;
917 /*
918 * Search for the replacing ccw device
919 * - among the disconnected devices
920 * - in the orphanage
921 */
922 replacing_cdev = get_disc_ccwdev_by_dev_id(&dev_id, cdev);
923 if (replacing_cdev) {
924 sch_attach_disconnected_device(sch, replacing_cdev);
Cornelia Huck85acc402008-11-14 18:18:06 +0100925 /* Release reference from get_disc_ccwdev_by_dev_id() */
Cornelia Huck97166f52008-12-25 13:39:05 +0100926 put_device(&replacing_cdev->dev);
Cornelia Huck6eff2082008-12-25 13:39:07 +0100927 /* Release reference of subchannel from old cdev. */
928 put_device(&sch->dev);
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100929 return;
930 }
931 replacing_cdev = get_orphaned_ccwdev_by_dev_id(css, &dev_id);
932 if (replacing_cdev) {
933 sch_attach_orphaned_device(sch, replacing_cdev);
Cornelia Huck85acc402008-11-14 18:18:06 +0100934 /* Release reference from get_orphaned_ccwdev_by_dev_id() */
Cornelia Huck97166f52008-12-25 13:39:05 +0100935 put_device(&replacing_cdev->dev);
Cornelia Huck6eff2082008-12-25 13:39:07 +0100936 /* Release reference of subchannel from old cdev. */
937 put_device(&sch->dev);
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100938 return;
939 }
940 sch_create_and_recog_new_device(sch);
Cornelia Huck6eff2082008-12-25 13:39:07 +0100941 /* Release reference of subchannel from old cdev. */
942 put_device(&sch->dev);
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100943}
944
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945/*
946 * Register recognized device.
947 */
948static void
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100949io_subchannel_register(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950{
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100951 struct ccw_device_private *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 struct ccw_device *cdev;
953 struct subchannel *sch;
954 int ret;
955 unsigned long flags;
956
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100957 priv = container_of(work, struct ccw_device_private, kick_work);
958 cdev = priv->cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 sch = to_subchannel(cdev->dev.parent);
Cornelia Huck5fb6b852008-12-25 13:39:08 +0100960 /*
961 * Check if subchannel is still registered. It may have become
962 * unregistered if a machine check hit us after finishing
963 * device recognition but before the register work could be
964 * queued.
965 */
966 if (!device_is_registered(&sch->dev))
967 goto out_err;
Cornelia Huck82b7ac02007-04-27 16:01:36 +0200968 css_update_ssd_info(sch);
Cornelia Huck47af5512006-12-04 15:41:07 +0100969 /*
970 * io_subchannel_register() will also be called after device
971 * recognition has been done for a boxed device (which will already
972 * be registered). We need to reprobe since we may now have sense id
973 * information.
974 */
Cornelia Huckd6a30762008-12-25 13:39:11 +0100975 if (device_is_registered(&cdev->dev)) {
Cornelia Huck47af5512006-12-04 15:41:07 +0100976 if (!cdev->drv) {
977 ret = device_reprobe(&cdev->dev);
978 if (ret)
979 /* We can't do much here. */
Michael Ernst139b83d2008-05-07 09:22:54 +0200980 CIO_MSG_EVENT(0, "device_reprobe() returned"
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200981 " %d for 0.%x.%04x\n", ret,
982 cdev->private->dev_id.ssid,
983 cdev->private->dev_id.devno);
Cornelia Huck47af5512006-12-04 15:41:07 +0100984 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 goto out;
986 }
Cornelia Huckfa1a8c22007-04-26 00:12:03 -0700987 /*
988 * Now we know this subchannel will stay, we can throw
989 * our delayed uevent.
990 */
Ming Leif67f1292009-03-01 21:10:49 +0800991 dev_set_uevent_suppress(&sch->dev, 0);
Cornelia Huckfa1a8c22007-04-26 00:12:03 -0700992 kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 /* make it known to the system */
994 ret = ccw_device_register(cdev);
995 if (ret) {
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200996 CIO_MSG_EVENT(0, "Could not register ccw dev 0.%x.%04x: %d\n",
997 cdev->private->dev_id.ssid,
998 cdev->private->dev_id.devno, ret);
Cornelia Huck2ec22982006-12-08 15:54:26 +0100999 spin_lock_irqsave(sch->lock, flags);
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001000 sch_set_cdev(sch, NULL);
Cornelia Huck2ec22982006-12-08 15:54:26 +01001001 spin_unlock_irqrestore(sch->lock, flags);
Cornelia Huck6eff2082008-12-25 13:39:07 +01001002 /* Release initial device reference. */
1003 put_device(&cdev->dev);
Cornelia Huck5fb6b852008-12-25 13:39:08 +01001004 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006out:
1007 cdev->private->flags.recog_done = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 wake_up(&cdev->private->wait_q);
Cornelia Huck5fb6b852008-12-25 13:39:08 +01001009out_err:
1010 /* Release reference for workqueue processing. */
1011 put_device(&cdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 if (atomic_dec_and_test(&ccw_device_init_count))
1013 wake_up(&ccw_device_init_wq);
1014}
1015
Cornelia Huck3f4cf6e2007-10-12 16:11:26 +02001016static void ccw_device_call_sch_unregister(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017{
Martin Schwidefskyc1637532006-12-08 15:53:57 +01001018 struct ccw_device_private *priv;
1019 struct ccw_device *cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 struct subchannel *sch;
1021
Martin Schwidefskyc1637532006-12-08 15:53:57 +01001022 priv = container_of(work, struct ccw_device_private, kick_work);
1023 cdev = priv->cdev;
Peter Oberparleiter46fbe4e2008-10-10 21:33:05 +02001024 /* Get subchannel reference for local processing. */
1025 if (!get_device(cdev->dev.parent))
1026 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 sch = to_subchannel(cdev->dev.parent);
Cornelia Huck6ab48792006-07-12 16:39:50 +02001028 css_sch_device_unregister(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 /* Reset intparm to zeroes. */
Sebastian Ott13952ec12008-12-25 13:39:13 +01001030 sch->config.intparm = 0;
1031 cio_commit_config(sch);
Peter Oberparleiter46fbe4e2008-10-10 21:33:05 +02001032 /* Release cdev reference for workqueue processing.*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 put_device(&cdev->dev);
Peter Oberparleiter46fbe4e2008-10-10 21:33:05 +02001034 /* Release subchannel reference for local processing. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 put_device(&sch->dev);
1036}
1037
Sebastian Ottc4621a62009-03-31 19:16:04 +02001038void ccw_device_schedule_sch_unregister(struct ccw_device *cdev)
1039{
1040 PREPARE_WORK(&cdev->private->kick_work,
1041 ccw_device_call_sch_unregister);
1042 queue_work(slow_path_wq, &cdev->private->kick_work);
1043}
1044
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045/*
1046 * subchannel recognition done. Called from the state machine.
1047 */
1048void
1049io_subchannel_recog_done(struct ccw_device *cdev)
1050{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 if (css_init_done == 0) {
1052 cdev->private->flags.recog_done = 1;
1053 return;
1054 }
1055 switch (cdev->private->state) {
Sebastian Ott47593bf2009-03-31 19:16:05 +02001056 case DEV_STATE_BOXED:
1057 /* Device did not respond in time. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 case DEV_STATE_NOT_OPER:
1059 cdev->private->flags.recog_done = 1;
1060 /* Remove device found not operational. */
1061 if (!get_device(&cdev->dev))
1062 break;
Sebastian Ottc4621a62009-03-31 19:16:04 +02001063 ccw_device_schedule_sch_unregister(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 if (atomic_dec_and_test(&ccw_device_init_count))
1065 wake_up(&ccw_device_init_wq);
1066 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 case DEV_STATE_OFFLINE:
1068 /*
1069 * We can't register the device in interrupt context so
1070 * we schedule a work item.
1071 */
1072 if (!get_device(&cdev->dev))
1073 break;
1074 PREPARE_WORK(&cdev->private->kick_work,
Martin Schwidefskyc1637532006-12-08 15:53:57 +01001075 io_subchannel_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 queue_work(slow_path_wq, &cdev->private->kick_work);
1077 break;
1078 }
1079}
1080
1081static int
1082io_subchannel_recog(struct ccw_device *cdev, struct subchannel *sch)
1083{
1084 int rc;
1085 struct ccw_device_private *priv;
1086
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001087 sch_set_cdev(sch, cdev);
Cornelia Huck2ec22982006-12-08 15:54:26 +01001088 cdev->ccwlock = sch->lock;
Cornelia Huckfb6958a2006-01-06 00:19:25 -08001089
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 /* Init private data. */
1091 priv = cdev->private;
Cornelia Huck78964262006-10-11 15:31:38 +02001092 priv->dev_id.devno = sch->schib.pmcw.dev;
1093 priv->dev_id.ssid = sch->schid.ssid;
1094 priv->schid = sch->schid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 priv->state = DEV_STATE_NOT_OPER;
1096 INIT_LIST_HEAD(&priv->cmb_list);
1097 init_waitqueue_head(&priv->wait_q);
1098 init_timer(&priv->timer);
1099
1100 /* Set an initial name for the device. */
Cornelia Huck1f4e7ed2008-10-10 21:33:14 +02001101 if (cio_is_console(sch->schid))
1102 cdev->dev.init_name = cio_get_console_cdev_name(sch);
1103 else
1104 dev_set_name(&cdev->dev, "0.%x.%04x",
1105 sch->schid.ssid, sch->schib.pmcw.dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106
1107 /* Increase counter of devices currently in recognition. */
1108 atomic_inc(&ccw_device_init_count);
1109
1110 /* Start async. device sensing. */
Cornelia Huck2ec22982006-12-08 15:54:26 +01001111 spin_lock_irq(sch->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 rc = ccw_device_recognition(cdev);
Cornelia Huck2ec22982006-12-08 15:54:26 +01001113 spin_unlock_irq(sch->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 if (rc) {
1115 if (atomic_dec_and_test(&ccw_device_init_count))
1116 wake_up(&ccw_device_init_wq);
1117 }
1118 return rc;
1119}
1120
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +01001121static void ccw_device_move_to_sch(struct work_struct *work)
1122{
1123 struct ccw_device_private *priv;
1124 int rc;
1125 struct subchannel *sch;
1126 struct ccw_device *cdev;
1127 struct subchannel *former_parent;
1128
1129 priv = container_of(work, struct ccw_device_private, kick_work);
1130 sch = priv->sch;
1131 cdev = priv->cdev;
Cornelia Huck6eff2082008-12-25 13:39:07 +01001132 former_parent = to_subchannel(cdev->dev.parent);
1133 /* Get reference for new parent. */
1134 if (!get_device(&sch->dev))
1135 return;
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +01001136 mutex_lock(&sch->reg_mutex);
Cornelia Huck6eff2082008-12-25 13:39:07 +01001137 /*
1138 * Try to move the ccw device to its new subchannel.
1139 * Note: device_move() changes cdev->dev.parent
1140 */
Cornelia Huckffa6a702009-03-04 12:44:00 +01001141 rc = device_move(&cdev->dev, &sch->dev, DPM_ORDER_PARENT_BEFORE_DEV);
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +01001142 mutex_unlock(&sch->reg_mutex);
1143 if (rc) {
Michael Ernst139b83d2008-05-07 09:22:54 +02001144 CIO_MSG_EVENT(0, "Moving device 0.%x.%04x to subchannel "
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +01001145 "0.%x.%04x failed (ret=%d)!\n",
1146 cdev->private->dev_id.ssid,
1147 cdev->private->dev_id.devno, sch->schid.ssid,
1148 sch->schid.sch_no, rc);
1149 css_sch_device_unregister(sch);
Cornelia Huck6eff2082008-12-25 13:39:07 +01001150 /* Put reference for new parent again. */
1151 put_device(&sch->dev);
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +01001152 goto out;
1153 }
Cornelia Huck6eff2082008-12-25 13:39:07 +01001154 if (!sch_is_pseudo_sch(former_parent)) {
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +01001155 spin_lock_irq(former_parent->lock);
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001156 sch_set_cdev(former_parent, NULL);
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +01001157 spin_unlock_irq(former_parent->lock);
1158 css_sch_device_unregister(former_parent);
1159 /* Reset intparm to zeroes. */
Sebastian Ott13952ec12008-12-25 13:39:13 +01001160 former_parent->config.intparm = 0;
1161 cio_commit_config(former_parent);
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +01001162 }
1163 sch_attach_device(sch, cdev);
1164out:
Cornelia Huck6eff2082008-12-25 13:39:07 +01001165 /* Put reference for old parent. */
1166 put_device(&former_parent->dev);
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +01001167 put_device(&cdev->dev);
1168}
1169
Cornelia Huck602b20f2008-01-26 14:10:39 +01001170static void io_subchannel_irq(struct subchannel *sch)
1171{
1172 struct ccw_device *cdev;
1173
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001174 cdev = sch_get_cdev(sch);
Cornelia Huck602b20f2008-01-26 14:10:39 +01001175
1176 CIO_TRACE_EVENT(3, "IRQ");
Kay Sievers2a0217d2008-10-10 21:33:09 +02001177 CIO_TRACE_EVENT(3, dev_name(&sch->dev));
Cornelia Huck602b20f2008-01-26 14:10:39 +01001178 if (cdev)
1179 dev_fsm_event(cdev, DEV_EVENT_INTERRUPT);
1180}
1181
Sebastian Ott13952ec12008-12-25 13:39:13 +01001182void io_subchannel_init_config(struct subchannel *sch)
1183{
1184 memset(&sch->config, 0, sizeof(sch->config));
1185 sch->config.csense = 1;
Sebastian Ottd36f0c62008-12-25 13:39:15 +01001186 /* Use subchannel mp mode when there is more than 1 installed CHPID. */
1187 if ((sch->schib.pmcw.pim & (sch->schib.pmcw.pim - 1)) != 0)
Sebastian Ott13952ec12008-12-25 13:39:13 +01001188 sch->config.mp = 1;
1189}
1190
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001191static void io_subchannel_init_fields(struct subchannel *sch)
1192{
1193 if (cio_is_console(sch->schid))
1194 sch->opm = 0xff;
1195 else
1196 sch->opm = chp_get_sch_opm(sch);
1197 sch->lpm = sch->schib.pmcw.pam & sch->opm;
Cornelia Huck3a3fc292008-07-14 09:58:58 +02001198 sch->isc = cio_is_console(sch->schid) ? CONSOLE_ISC : IO_SCH_ISC;
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001199
1200 CIO_MSG_EVENT(6, "Detected device %04x on subchannel 0.%x.%04X"
1201 " - PIM = %02X, PAM = %02X, POM = %02X\n",
1202 sch->schib.pmcw.dev, sch->schid.ssid,
1203 sch->schid.sch_no, sch->schib.pmcw.pim,
1204 sch->schib.pmcw.pam, sch->schib.pmcw.pom);
Sebastian Ott13952ec12008-12-25 13:39:13 +01001205
1206 io_subchannel_init_config(sch);
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001207}
1208
Cornelia Huck90ed2b62008-12-25 13:39:09 +01001209static void io_subchannel_do_unreg(struct work_struct *work)
1210{
1211 struct subchannel *sch;
1212
1213 sch = container_of(work, struct subchannel, work);
1214 css_sch_device_unregister(sch);
1215 /* Reset intparm to zeroes. */
Sebastian Ott13952ec12008-12-25 13:39:13 +01001216 sch->config.intparm = 0;
1217 cio_commit_config(sch);
Cornelia Huck90ed2b62008-12-25 13:39:09 +01001218 put_device(&sch->dev);
1219}
1220
1221/* Schedule unregister if we have no cdev. */
1222static void io_subchannel_schedule_removal(struct subchannel *sch)
1223{
1224 get_device(&sch->dev);
1225 INIT_WORK(&sch->work, io_subchannel_do_unreg);
1226 queue_work(slow_path_wq, &sch->work);
1227}
1228
1229/*
1230 * Note: We always return 0 so that we bind to the device even on error.
1231 * This is needed so that our remove function is called on unregister.
1232 */
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001233static int io_subchannel_probe(struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 struct ccw_device *cdev;
1236 int rc;
1237 unsigned long flags;
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +01001238 struct ccw_dev_id dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001240 cdev = sch_get_cdev(sch);
1241 if (cdev) {
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001242 rc = sysfs_create_group(&sch->dev.kobj,
1243 &io_subchannel_attr_group);
1244 if (rc)
1245 CIO_MSG_EVENT(0, "Failed to create io subchannel "
1246 "attributes for subchannel "
1247 "0.%x.%04x (rc=%d)\n",
1248 sch->schid.ssid, sch->schid.sch_no, rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 /*
1250 * This subchannel already has an associated ccw_device.
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001251 * Throw the delayed uevent for the subchannel, register
1252 * the ccw_device and exit. This happens for all early
1253 * devices, e.g. the console.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 */
Ming Leif67f1292009-03-01 21:10:49 +08001255 dev_set_uevent_suppress(&sch->dev, 0);
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001256 kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
Cornelia Hucke1031782007-10-12 16:11:23 +02001257 cdev->dev.groups = ccwdev_attr_groups;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 device_initialize(&cdev->dev);
1259 ccw_device_register(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 /*
1261 * Check if the device is already online. If it is
Cornelia Huck9cd67422008-12-25 13:39:06 +01001262 * the reference count needs to be corrected since we
1263 * didn't obtain a reference in ccw_device_set_online.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 */
1265 if (cdev->private->state != DEV_STATE_NOT_OPER &&
1266 cdev->private->state != DEV_STATE_OFFLINE &&
1267 cdev->private->state != DEV_STATE_BOXED)
1268 get_device(&cdev->dev);
1269 return 0;
1270 }
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001271 io_subchannel_init_fields(sch);
Sebastian Ottf444cc02008-12-25 13:39:14 +01001272 rc = cio_commit_config(sch);
1273 if (rc)
1274 goto out_schedule;
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001275 rc = sysfs_create_group(&sch->dev.kobj,
1276 &io_subchannel_attr_group);
1277 if (rc)
Cornelia Huck90ed2b62008-12-25 13:39:09 +01001278 goto out_schedule;
Cornelia Huckcd6b4f22008-01-26 14:10:43 +01001279 /* Allocate I/O subchannel private data. */
1280 sch->private = kzalloc(sizeof(struct io_subchannel_private),
1281 GFP_KERNEL | GFP_DMA);
Cornelia Huck90ed2b62008-12-25 13:39:09 +01001282 if (!sch->private)
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001283 goto out_err;
Sebastian Ott111e95a2008-12-25 13:39:03 +01001284 /*
1285 * First check if a fitting device may be found amongst the
1286 * disconnected devices or in the orphanage.
1287 */
1288 dev_id.devno = sch->schib.pmcw.dev;
1289 dev_id.ssid = sch->schid.ssid;
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +01001290 cdev = get_disc_ccwdev_by_dev_id(&dev_id, NULL);
1291 if (!cdev)
1292 cdev = get_orphaned_ccwdev_by_dev_id(to_css(sch->dev.parent),
1293 &dev_id);
1294 if (cdev) {
1295 /*
1296 * Schedule moving the device until when we have a registered
1297 * subchannel to move to and succeed the probe. We can
1298 * unregister later again, when the probe is through.
1299 */
1300 cdev->private->sch = sch;
1301 PREPARE_WORK(&cdev->private->kick_work,
1302 ccw_device_move_to_sch);
1303 queue_work(slow_path_wq, &cdev->private->kick_work);
1304 return 0;
1305 }
Cornelia Huck7674da72006-12-08 15:54:21 +01001306 cdev = io_subchannel_create_ccwdev(sch);
Cornelia Huck90ed2b62008-12-25 13:39:09 +01001307 if (IS_ERR(cdev))
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001308 goto out_err;
Cornelia Huck8bbace72006-01-11 10:56:22 +01001309 rc = io_subchannel_recog(cdev, sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 if (rc) {
Cornelia Huck2ec22982006-12-08 15:54:26 +01001311 spin_lock_irqsave(sch->lock, flags);
Cornelia Huck90ed2b62008-12-25 13:39:09 +01001312 io_subchannel_recog_done(cdev);
Cornelia Huck2ec22982006-12-08 15:54:26 +01001313 spin_unlock_irqrestore(sch->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 }
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001315 return 0;
1316out_err:
1317 kfree(sch->private);
1318 sysfs_remove_group(&sch->dev.kobj, &io_subchannel_attr_group);
Cornelia Huck90ed2b62008-12-25 13:39:09 +01001319out_schedule:
1320 io_subchannel_schedule_removal(sch);
1321 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322}
1323
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324static int
Cornelia Huck8bbace72006-01-11 10:56:22 +01001325io_subchannel_remove (struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326{
1327 struct ccw_device *cdev;
1328 unsigned long flags;
1329
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001330 cdev = sch_get_cdev(sch);
1331 if (!cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 /* Set ccw device to not operational and drop reference. */
1334 spin_lock_irqsave(cdev->ccwlock, flags);
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001335 sch_set_cdev(sch, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 cdev->private->state = DEV_STATE_NOT_OPER;
1337 spin_unlock_irqrestore(cdev->ccwlock, flags);
Cornelia Huckef995162007-04-27 16:01:39 +02001338 ccw_device_unregister(cdev);
1339 put_device(&cdev->dev);
Cornelia Huckcd6b4f22008-01-26 14:10:43 +01001340 kfree(sch->private);
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001341 sysfs_remove_group(&sch->dev.kobj, &io_subchannel_attr_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 return 0;
1343}
1344
Cornelia Huck602b20f2008-01-26 14:10:39 +01001345static int io_subchannel_notify(struct subchannel *sch, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346{
1347 struct ccw_device *cdev;
1348
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001349 cdev = sch_get_cdev(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 if (!cdev)
1351 return 0;
Cornelia Huckc820de32008-07-14 09:58:45 +02001352 return ccw_device_notify(cdev, event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353}
1354
Cornelia Huck602b20f2008-01-26 14:10:39 +01001355static void io_subchannel_verify(struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356{
1357 struct ccw_device *cdev;
1358
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001359 cdev = sch_get_cdev(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 if (cdev)
1361 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1362}
1363
Cornelia Huckc820de32008-07-14 09:58:45 +02001364static int check_for_io_on_path(struct subchannel *sch, int mask)
1365{
Sebastian Ottcdb912a2008-12-25 13:39:12 +01001366 if (cio_update_schib(sch))
Cornelia Huckc820de32008-07-14 09:58:45 +02001367 return 0;
Peter Oberparleiter23d805b2008-07-14 09:58:50 +02001368 if (scsw_actl(&sch->schib.scsw) && sch->schib.pmcw.lpum == mask)
Cornelia Huckc820de32008-07-14 09:58:45 +02001369 return 1;
1370 return 0;
1371}
1372
1373static void terminate_internal_io(struct subchannel *sch,
1374 struct ccw_device *cdev)
1375{
1376 if (cio_clear(sch)) {
1377 /* Recheck device in case clear failed. */
1378 sch->lpm = 0;
1379 if (cdev->online)
1380 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1381 else
1382 css_schedule_eval(sch->schid);
1383 return;
1384 }
1385 cdev->private->state = DEV_STATE_CLEAR_VERIFY;
1386 /* Request retry of internal operation. */
1387 cdev->private->flags.intretry = 1;
1388 /* Call handler. */
1389 if (cdev->handler)
1390 cdev->handler(cdev, cdev->private->intparm,
1391 ERR_PTR(-EIO));
1392}
1393
1394static void io_subchannel_terminate_path(struct subchannel *sch, u8 mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395{
1396 struct ccw_device *cdev;
1397
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001398 cdev = sch_get_cdev(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 if (!cdev)
1400 return;
Cornelia Huckc820de32008-07-14 09:58:45 +02001401 if (check_for_io_on_path(sch, mask)) {
1402 if (cdev->private->state == DEV_STATE_ONLINE)
1403 ccw_device_kill_io(cdev);
1404 else {
1405 terminate_internal_io(sch, cdev);
1406 /* Re-start path verification. */
1407 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1408 }
1409 } else
1410 /* trigger path verification. */
1411 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1412
1413}
1414
Cornelia Huck99611f82008-07-14 09:59:02 +02001415static int io_subchannel_chp_event(struct subchannel *sch,
1416 struct chp_link *link, int event)
Cornelia Huckc820de32008-07-14 09:58:45 +02001417{
1418 int mask;
Cornelia Huckc820de32008-07-14 09:58:45 +02001419
Cornelia Huck99611f82008-07-14 09:59:02 +02001420 mask = chp_ssd_get_mask(&sch->ssd_info, link);
Cornelia Huckc820de32008-07-14 09:58:45 +02001421 if (!mask)
1422 return 0;
1423 switch (event) {
1424 case CHP_VARY_OFF:
1425 sch->opm &= ~mask;
1426 sch->lpm &= ~mask;
1427 io_subchannel_terminate_path(sch, mask);
1428 break;
1429 case CHP_VARY_ON:
1430 sch->opm |= mask;
1431 sch->lpm |= mask;
1432 io_subchannel_verify(sch);
1433 break;
1434 case CHP_OFFLINE:
Sebastian Ottcdb912a2008-12-25 13:39:12 +01001435 if (cio_update_schib(sch))
Cornelia Huckc820de32008-07-14 09:58:45 +02001436 return -ENODEV;
1437 io_subchannel_terminate_path(sch, mask);
1438 break;
1439 case CHP_ONLINE:
Sebastian Ottcdb912a2008-12-25 13:39:12 +01001440 if (cio_update_schib(sch))
1441 return -ENODEV;
Cornelia Huckc820de32008-07-14 09:58:45 +02001442 sch->lpm |= mask & sch->opm;
1443 io_subchannel_verify(sch);
1444 break;
1445 }
1446 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447}
1448
1449static void
Cornelia Huck8bbace72006-01-11 10:56:22 +01001450io_subchannel_shutdown(struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 struct ccw_device *cdev;
1453 int ret;
1454
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001455 cdev = sch_get_cdev(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456
Cornelia Hucka8237fc2006-01-06 00:19:21 -08001457 if (cio_is_console(sch->schid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 return;
1459 if (!sch->schib.pmcw.ena)
1460 /* Nothing to do. */
1461 return;
1462 ret = cio_disable_subchannel(sch);
1463 if (ret != -EBUSY)
1464 /* Subchannel is disabled, we're done. */
1465 return;
1466 cdev->private->state = DEV_STATE_QUIESCE;
1467 if (cdev->handler)
1468 cdev->handler(cdev, cdev->private->intparm,
1469 ERR_PTR(-EIO));
1470 ret = ccw_device_cancel_halt_clear(cdev);
1471 if (ret == -EBUSY) {
1472 ccw_device_set_timeout(cdev, HZ/10);
1473 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
1474 }
1475 cio_disable_subchannel(sch);
1476}
1477
Cornelia Huckc820de32008-07-14 09:58:45 +02001478static int io_subchannel_get_status(struct subchannel *sch)
1479{
1480 struct schib schib;
1481
1482 if (stsch(sch->schid, &schib) || !schib.pmcw.dnv)
1483 return CIO_GONE;
1484 if (sch->schib.pmcw.dnv && (schib.pmcw.dev != sch->schib.pmcw.dev))
1485 return CIO_REVALIDATE;
1486 if (!sch->lpm)
1487 return CIO_NO_PATH;
1488 return CIO_OPER;
1489}
1490
1491static int device_is_disconnected(struct ccw_device *cdev)
1492{
1493 if (!cdev)
1494 return 0;
1495 return (cdev->private->state == DEV_STATE_DISCONNECTED ||
1496 cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID);
1497}
1498
1499static int recovery_check(struct device *dev, void *data)
1500{
1501 struct ccw_device *cdev = to_ccwdev(dev);
1502 int *redo = data;
1503
1504 spin_lock_irq(cdev->ccwlock);
1505 switch (cdev->private->state) {
1506 case DEV_STATE_DISCONNECTED:
1507 CIO_MSG_EVENT(3, "recovery: trigger 0.%x.%04x\n",
1508 cdev->private->dev_id.ssid,
1509 cdev->private->dev_id.devno);
1510 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1511 *redo = 1;
1512 break;
1513 case DEV_STATE_DISCONNECTED_SENSE_ID:
1514 *redo = 1;
1515 break;
1516 }
1517 spin_unlock_irq(cdev->ccwlock);
1518
1519 return 0;
1520}
1521
1522static void recovery_work_func(struct work_struct *unused)
1523{
1524 int redo = 0;
1525
1526 bus_for_each_dev(&ccw_bus_type, NULL, &redo, recovery_check);
1527 if (redo) {
1528 spin_lock_irq(&recovery_lock);
1529 if (!timer_pending(&recovery_timer)) {
1530 if (recovery_phase < ARRAY_SIZE(recovery_delay) - 1)
1531 recovery_phase++;
1532 mod_timer(&recovery_timer, jiffies +
1533 recovery_delay[recovery_phase] * HZ);
1534 }
1535 spin_unlock_irq(&recovery_lock);
1536 } else
1537 CIO_MSG_EVENT(4, "recovery: end\n");
1538}
1539
1540static DECLARE_WORK(recovery_work, recovery_work_func);
1541
1542static void recovery_func(unsigned long data)
1543{
1544 /*
1545 * We can't do our recovery in softirq context and it's not
1546 * performance critical, so we schedule it.
1547 */
1548 schedule_work(&recovery_work);
1549}
1550
1551static void ccw_device_schedule_recovery(void)
1552{
1553 unsigned long flags;
1554
1555 CIO_MSG_EVENT(4, "recovery: schedule\n");
1556 spin_lock_irqsave(&recovery_lock, flags);
1557 if (!timer_pending(&recovery_timer) || (recovery_phase != 0)) {
1558 recovery_phase = 0;
1559 mod_timer(&recovery_timer, jiffies + recovery_delay[0] * HZ);
1560 }
1561 spin_unlock_irqrestore(&recovery_lock, flags);
1562}
1563
Peter Oberparleiterecf5d9e2008-10-10 21:33:06 +02001564static int purge_fn(struct device *dev, void *data)
1565{
1566 struct ccw_device *cdev = to_ccwdev(dev);
1567 struct ccw_device_private *priv = cdev->private;
1568 int unreg;
1569
1570 spin_lock_irq(cdev->ccwlock);
1571 unreg = is_blacklisted(priv->dev_id.ssid, priv->dev_id.devno) &&
1572 (priv->state == DEV_STATE_OFFLINE);
1573 spin_unlock_irq(cdev->ccwlock);
1574 if (!unreg)
1575 goto out;
1576 if (!get_device(&cdev->dev))
1577 goto out;
1578 CIO_MSG_EVENT(3, "ccw: purging 0.%x.%04x\n", priv->dev_id.ssid,
1579 priv->dev_id.devno);
Sebastian Ottc4621a62009-03-31 19:16:04 +02001580 ccw_device_schedule_sch_unregister(cdev);
Peter Oberparleiterecf5d9e2008-10-10 21:33:06 +02001581
1582out:
1583 /* Abort loop in case of pending signal. */
1584 if (signal_pending(current))
1585 return -EINTR;
1586
1587 return 0;
1588}
1589
1590/**
1591 * ccw_purge_blacklisted - purge unused, blacklisted devices
1592 *
1593 * Unregister all ccw devices that are offline and on the blacklist.
1594 */
1595int ccw_purge_blacklisted(void)
1596{
1597 CIO_MSG_EVENT(2, "ccw: purging blacklisted devices\n");
1598 bus_for_each_dev(&ccw_bus_type, NULL, NULL, purge_fn);
1599 return 0;
1600}
1601
Cornelia Huckc820de32008-07-14 09:58:45 +02001602static void device_set_disconnected(struct ccw_device *cdev)
1603{
1604 if (!cdev)
1605 return;
1606 ccw_device_set_timeout(cdev, 0);
1607 cdev->private->flags.fake_irb = 0;
1608 cdev->private->state = DEV_STATE_DISCONNECTED;
1609 if (cdev->online)
1610 ccw_device_schedule_recovery();
1611}
1612
Peter Oberparleiter91c36912008-08-21 19:46:39 +02001613void ccw_device_set_notoper(struct ccw_device *cdev)
1614{
1615 struct subchannel *sch = to_subchannel(cdev->dev.parent);
1616
1617 CIO_TRACE_EVENT(2, "notoper");
Cornelia Huckb9d3aed2008-10-10 21:33:11 +02001618 CIO_TRACE_EVENT(2, dev_name(&sch->dev));
Peter Oberparleiter91c36912008-08-21 19:46:39 +02001619 ccw_device_set_timeout(cdev, 0);
1620 cio_disable_subchannel(sch);
1621 cdev->private->state = DEV_STATE_NOT_OPER;
1622}
1623
Cornelia Huckc820de32008-07-14 09:58:45 +02001624static int io_subchannel_sch_event(struct subchannel *sch, int slow)
1625{
1626 int event, ret, disc;
1627 unsigned long flags;
Peter Oberparleiter91c36912008-08-21 19:46:39 +02001628 enum { NONE, UNREGISTER, UNREGISTER_PROBE, REPROBE, DISC } action;
Cornelia Huckc820de32008-07-14 09:58:45 +02001629 struct ccw_device *cdev;
1630
1631 spin_lock_irqsave(sch->lock, flags);
1632 cdev = sch_get_cdev(sch);
1633 disc = device_is_disconnected(cdev);
1634 if (disc && slow) {
1635 /* Disconnected devices are evaluated directly only.*/
1636 spin_unlock_irqrestore(sch->lock, flags);
1637 return 0;
1638 }
1639 /* No interrupt after machine check - kill pending timers. */
1640 if (cdev)
1641 ccw_device_set_timeout(cdev, 0);
1642 if (!disc && !slow) {
1643 /* Non-disconnected devices are evaluated on the slow path. */
1644 spin_unlock_irqrestore(sch->lock, flags);
1645 return -EAGAIN;
1646 }
1647 event = io_subchannel_get_status(sch);
1648 CIO_MSG_EVENT(4, "Evaluating schid 0.%x.%04x, event %d, %s, %s path.\n",
1649 sch->schid.ssid, sch->schid.sch_no, event,
1650 disc ? "disconnected" : "normal",
1651 slow ? "slow" : "fast");
1652 /* Analyze subchannel status. */
1653 action = NONE;
1654 switch (event) {
1655 case CIO_NO_PATH:
1656 if (disc) {
1657 /* Check if paths have become available. */
1658 action = REPROBE;
1659 break;
1660 }
1661 /* fall through */
1662 case CIO_GONE:
Cornelia Huckc820de32008-07-14 09:58:45 +02001663 /* Ask driver what to do with device. */
Peter Oberparleiter91c36912008-08-21 19:46:39 +02001664 if (io_subchannel_notify(sch, event))
1665 action = DISC;
1666 else
1667 action = UNREGISTER;
Cornelia Huckc820de32008-07-14 09:58:45 +02001668 break;
1669 case CIO_REVALIDATE:
1670 /* Device will be removed, so no notify necessary. */
1671 if (disc)
1672 /* Reprobe because immediate unregister might block. */
1673 action = REPROBE;
1674 else
1675 action = UNREGISTER_PROBE;
1676 break;
1677 case CIO_OPER:
1678 if (disc)
1679 /* Get device operational again. */
1680 action = REPROBE;
1681 break;
1682 }
1683 /* Perform action. */
1684 ret = 0;
1685 switch (action) {
1686 case UNREGISTER:
1687 case UNREGISTER_PROBE:
Peter Oberparleiter91c36912008-08-21 19:46:39 +02001688 ccw_device_set_notoper(cdev);
Cornelia Huckc820de32008-07-14 09:58:45 +02001689 /* Unregister device (will use subchannel lock). */
1690 spin_unlock_irqrestore(sch->lock, flags);
1691 css_sch_device_unregister(sch);
1692 spin_lock_irqsave(sch->lock, flags);
1693
1694 /* Reset intparm to zeroes. */
Sebastian Ott13952ec12008-12-25 13:39:13 +01001695 sch->config.intparm = 0;
1696 cio_commit_config(sch);
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;
Cornelia Huck1f4e7ed2008-10-10 21:33:14 +02001717static char console_cdev_name[10] = "0.x.xxxx";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718static struct ccw_device_private console_private;
1719static int console_cdev_in_use;
1720
Cornelia Huck2ec22982006-12-08 15:54:26 +01001721static DEFINE_SPINLOCK(ccw_console_lock);
1722
1723spinlock_t * cio_get_console_lock(void)
1724{
1725 return &ccw_console_lock;
1726}
1727
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001728static int ccw_device_console_enable(struct ccw_device *cdev,
1729 struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730{
1731 int rc;
1732
Cornelia Huckcd6b4f22008-01-26 14:10:43 +01001733 /* Attach subchannel private data. */
1734 sch->private = cio_get_console_priv();
1735 memset(sch->private, 0, sizeof(struct io_subchannel_private));
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001736 io_subchannel_init_fields(sch);
Sebastian Ottf444cc02008-12-25 13:39:14 +01001737 rc = cio_commit_config(sch);
1738 if (rc)
1739 return rc;
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001740 sch->driver = &io_subchannel_driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 /* Initialize the ccw_device structure. */
Heiko Carstens292888c2006-08-30 14:33:35 +02001742 cdev->dev.parent= &sch->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 rc = io_subchannel_recog(cdev, sch);
1744 if (rc)
1745 return rc;
1746
1747 /* Now wait for the async. recognition to come to an end. */
1748 spin_lock_irq(cdev->ccwlock);
1749 while (!dev_fsm_final_state(cdev))
1750 wait_cons_dev();
1751 rc = -EIO;
1752 if (cdev->private->state != DEV_STATE_OFFLINE)
1753 goto out_unlock;
1754 ccw_device_online(cdev);
1755 while (!dev_fsm_final_state(cdev))
1756 wait_cons_dev();
1757 if (cdev->private->state != DEV_STATE_ONLINE)
1758 goto out_unlock;
1759 rc = 0;
1760out_unlock:
1761 spin_unlock_irq(cdev->ccwlock);
1762 return 0;
1763}
1764
1765struct ccw_device *
1766ccw_device_probe_console(void)
1767{
1768 struct subchannel *sch;
1769 int ret;
1770
1771 if (xchg(&console_cdev_in_use, 1) != 0)
Peter Oberparleiter600b5d12006-02-01 03:06:35 -08001772 return ERR_PTR(-EBUSY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 sch = cio_probe_console();
1774 if (IS_ERR(sch)) {
1775 console_cdev_in_use = 0;
1776 return (void *) sch;
1777 }
1778 memset(&console_cdev, 0, sizeof(struct ccw_device));
1779 memset(&console_private, 0, sizeof(struct ccw_device_private));
1780 console_cdev.private = &console_private;
Martin Schwidefskyc1637532006-12-08 15:53:57 +01001781 console_private.cdev = &console_cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 ret = ccw_device_console_enable(&console_cdev, sch);
1783 if (ret) {
1784 cio_release_console();
1785 console_cdev_in_use = 0;
1786 return ERR_PTR(ret);
1787 }
1788 console_cdev.online = 1;
1789 return &console_cdev;
1790}
Cornelia Huck1f4e7ed2008-10-10 21:33:14 +02001791
1792
1793const char *cio_get_console_cdev_name(struct subchannel *sch)
1794{
1795 snprintf(console_cdev_name, 10, "0.%x.%04x",
1796 sch->schid.ssid, sch->schib.pmcw.dev);
1797 return (const char *)console_cdev_name;
1798}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799#endif
1800
1801/*
1802 * get ccw_device matching the busid, but only if owned by cdrv
1803 */
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001804static int
1805__ccwdev_check_busid(struct device *dev, void *id)
1806{
1807 char *bus_id;
1808
Cornelia Huck12975ae2006-10-11 15:31:47 +02001809 bus_id = id;
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001810
Kay Sievers98df67b2008-12-25 13:38:55 +01001811 return (strcmp(bus_id, dev_name(dev)) == 0);
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001812}
1813
1814
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +02001815/**
1816 * get_ccwdev_by_busid() - obtain device from a bus id
1817 * @cdrv: driver the device is owned by
1818 * @bus_id: bus id of the device to be searched
1819 *
1820 * This function searches all devices owned by @cdrv for a device with a bus
1821 * id matching @bus_id.
1822 * Returns:
1823 * If a match is found, its reference count of the found device is increased
1824 * and it is returned; else %NULL is returned.
1825 */
1826struct ccw_device *get_ccwdev_by_busid(struct ccw_driver *cdrv,
1827 const char *bus_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828{
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001829 struct device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 struct device_driver *drv;
1831
1832 drv = get_driver(&cdrv->driver);
1833 if (!drv)
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001834 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001836 dev = driver_find_device(drv, NULL, (void *)bus_id,
1837 __ccwdev_check_busid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 put_driver(drv);
1839
Heiko Carstensd2c993d2006-07-12 16:41:55 +02001840 return dev ? to_ccwdev(dev) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841}
1842
1843/************************** device driver handling ************************/
1844
1845/* This is the implementation of the ccw_driver class. The probe, remove
1846 * and release methods are initially very similar to the device_driver
1847 * implementations, with the difference that they have ccw_device
1848 * arguments.
1849 *
1850 * A ccw driver also contains the information that is needed for
1851 * device matching.
1852 */
1853static int
1854ccw_device_probe (struct device *dev)
1855{
1856 struct ccw_device *cdev = to_ccwdev(dev);
1857 struct ccw_driver *cdrv = to_ccwdrv(dev->driver);
1858 int ret;
1859
1860 cdev->drv = cdrv; /* to let the driver call _set_online */
1861
1862 ret = cdrv->probe ? cdrv->probe(cdev) : -ENODEV;
1863
1864 if (ret) {
Heiko Carstensd2c993d2006-07-12 16:41:55 +02001865 cdev->drv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 return ret;
1867 }
1868
1869 return 0;
1870}
1871
1872static int
1873ccw_device_remove (struct device *dev)
1874{
1875 struct ccw_device *cdev = to_ccwdev(dev);
1876 struct ccw_driver *cdrv = cdev->drv;
1877 int ret;
1878
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 if (cdrv->remove)
1880 cdrv->remove(cdev);
1881 if (cdev->online) {
1882 cdev->online = 0;
1883 spin_lock_irq(cdev->ccwlock);
1884 ret = ccw_device_offline(cdev);
1885 spin_unlock_irq(cdev->ccwlock);
1886 if (ret == 0)
1887 wait_event(cdev->private->wait_q,
1888 dev_fsm_final_state(cdev));
1889 else
Michael Ernst139b83d2008-05-07 09:22:54 +02001890 CIO_MSG_EVENT(0, "ccw_device_offline returned %d, "
Cornelia Hucke556bbb2007-07-27 12:29:19 +02001891 "device 0.%x.%04x\n",
1892 ret, cdev->private->dev_id.ssid,
1893 cdev->private->dev_id.devno);
Cornelia Huck9cd67422008-12-25 13:39:06 +01001894 /* Give up reference obtained in ccw_device_set_online(). */
1895 put_device(&cdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 }
1897 ccw_device_set_timeout(cdev, 0);
Heiko Carstensd2c993d2006-07-12 16:41:55 +02001898 cdev->drv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 return 0;
1900}
1901
Cornelia Huck958974f2007-10-12 16:11:21 +02001902static void ccw_device_shutdown(struct device *dev)
1903{
1904 struct ccw_device *cdev;
1905
1906 cdev = to_ccwdev(dev);
1907 if (cdev->drv && cdev->drv->shutdown)
1908 cdev->drv->shutdown(cdev);
Cornelia Huck1842f2b2007-10-12 16:11:22 +02001909 disable_cmf(cdev);
Cornelia Huck958974f2007-10-12 16:11:21 +02001910}
1911
Sebastian Ott823d4942009-06-16 10:30:20 +02001912static int ccw_device_pm_prepare(struct device *dev)
1913{
1914 struct ccw_device *cdev = to_ccwdev(dev);
1915
1916 if (work_pending(&cdev->private->kick_work))
1917 return -EAGAIN;
1918 /* Fail while device is being set online/offline. */
1919 if (atomic_read(&cdev->private->onoff))
1920 return -EAGAIN;
1921
1922 if (cdev->online && cdev->drv && cdev->drv->prepare)
1923 return cdev->drv->prepare(cdev);
1924
1925 return 0;
1926}
1927
1928static void ccw_device_pm_complete(struct device *dev)
1929{
1930 struct ccw_device *cdev = to_ccwdev(dev);
1931
1932 if (cdev->online && cdev->drv && cdev->drv->complete)
1933 cdev->drv->complete(cdev);
1934}
1935
1936static int ccw_device_pm_freeze(struct device *dev)
1937{
1938 struct ccw_device *cdev = to_ccwdev(dev);
1939 struct subchannel *sch = to_subchannel(cdev->dev.parent);
1940 int ret, cm_enabled;
1941
1942 /* Fail suspend while device is in transistional state. */
1943 if (!dev_fsm_final_state(cdev))
1944 return -EAGAIN;
1945 if (!cdev->online)
1946 return 0;
1947 if (cdev->drv && cdev->drv->freeze) {
1948 ret = cdev->drv->freeze(cdev);
1949 if (ret)
1950 return ret;
1951 }
1952
1953 spin_lock_irq(sch->lock);
1954 cm_enabled = cdev->private->cmb != NULL;
1955 spin_unlock_irq(sch->lock);
1956 if (cm_enabled) {
1957 /* Don't have the css write on memory. */
1958 ret = ccw_set_cmf(cdev, 0);
1959 if (ret)
1960 return ret;
1961 }
1962 /* From here on, disallow device driver I/O. */
1963 spin_lock_irq(sch->lock);
1964 ret = cio_disable_subchannel(sch);
1965 spin_unlock_irq(sch->lock);
1966
1967 return ret;
1968}
1969
1970static int ccw_device_pm_thaw(struct device *dev)
1971{
1972 struct ccw_device *cdev = to_ccwdev(dev);
1973 struct subchannel *sch = to_subchannel(cdev->dev.parent);
1974 int ret, cm_enabled;
1975
1976 if (!cdev->online)
1977 return 0;
1978
1979 spin_lock_irq(sch->lock);
1980 /* Allow device driver I/O again. */
1981 ret = cio_enable_subchannel(sch, (u32)(addr_t)sch);
1982 cm_enabled = cdev->private->cmb != NULL;
1983 spin_unlock_irq(sch->lock);
1984 if (ret)
1985 return ret;
1986
1987 if (cm_enabled) {
1988 ret = ccw_set_cmf(cdev, 1);
1989 if (ret)
1990 return ret;
1991 }
1992
1993 if (cdev->drv && cdev->drv->thaw)
1994 ret = cdev->drv->thaw(cdev);
1995
1996 return ret;
1997}
1998
1999static void __ccw_device_pm_restore(struct ccw_device *cdev)
2000{
2001 struct subchannel *sch = to_subchannel(cdev->dev.parent);
2002 int ret;
2003
2004 if (cio_is_console(sch->schid))
2005 goto out;
2006 /*
2007 * While we were sleeping, devices may have gone or become
2008 * available again. Kick re-detection.
2009 */
2010 spin_lock_irq(sch->lock);
2011 cdev->private->flags.resuming = 1;
2012 ret = ccw_device_recognition(cdev);
2013 spin_unlock_irq(sch->lock);
2014 if (ret) {
2015 CIO_MSG_EVENT(0, "Couldn't start recognition for device "
2016 "%s (ret=%d)\n", dev_name(&cdev->dev), ret);
2017 spin_lock_irq(sch->lock);
2018 cdev->private->state = DEV_STATE_DISCONNECTED;
2019 spin_unlock_irq(sch->lock);
2020 /* notify driver after the resume cb */
2021 goto out;
2022 }
2023 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev) ||
2024 cdev->private->state == DEV_STATE_DISCONNECTED);
2025
2026out:
2027 cdev->private->flags.resuming = 0;
2028}
2029
2030static int resume_handle_boxed(struct ccw_device *cdev)
2031{
2032 cdev->private->state = DEV_STATE_BOXED;
2033 if (ccw_device_notify(cdev, CIO_BOXED))
2034 return 0;
2035 ccw_device_schedule_sch_unregister(cdev);
2036 return -ENODEV;
2037}
2038
2039static int resume_handle_disc(struct ccw_device *cdev)
2040{
2041 cdev->private->state = DEV_STATE_DISCONNECTED;
2042 if (ccw_device_notify(cdev, CIO_GONE))
2043 return 0;
2044 ccw_device_schedule_sch_unregister(cdev);
2045 return -ENODEV;
2046}
2047
2048static int ccw_device_pm_restore(struct device *dev)
2049{
2050 struct ccw_device *cdev = to_ccwdev(dev);
2051 struct subchannel *sch = to_subchannel(cdev->dev.parent);
2052 int ret = 0, cm_enabled;
2053
2054 __ccw_device_pm_restore(cdev);
2055 spin_lock_irq(sch->lock);
2056 if (cio_is_console(sch->schid)) {
2057 cio_enable_subchannel(sch, (u32)(addr_t)sch);
2058 spin_unlock_irq(sch->lock);
2059 goto out_restore;
2060 }
2061 cdev->private->flags.donotify = 0;
2062 /* check recognition results */
2063 switch (cdev->private->state) {
2064 case DEV_STATE_OFFLINE:
2065 break;
2066 case DEV_STATE_BOXED:
2067 ret = resume_handle_boxed(cdev);
2068 spin_unlock_irq(sch->lock);
2069 if (ret)
2070 goto out;
2071 goto out_restore;
2072 case DEV_STATE_DISCONNECTED:
2073 goto out_disc_unlock;
2074 default:
2075 goto out_unreg_unlock;
2076 }
2077 /* check if the device id has changed */
2078 if (sch->schib.pmcw.dev != cdev->private->dev_id.devno) {
2079 CIO_MSG_EVENT(0, "resume: sch %s: failed (devno changed from "
2080 "%04x to %04x)\n", dev_name(&sch->dev),
2081 cdev->private->dev_id.devno,
2082 sch->schib.pmcw.dev);
2083 goto out_unreg_unlock;
2084 }
2085 /* check if the device type has changed */
2086 if (!ccw_device_test_sense_data(cdev)) {
2087 ccw_device_update_sense_data(cdev);
2088 PREPARE_WORK(&cdev->private->kick_work,
2089 ccw_device_do_unbind_bind);
2090 queue_work(ccw_device_work, &cdev->private->kick_work);
2091 ret = -ENODEV;
2092 goto out_unlock;
2093 }
2094 if (!cdev->online) {
2095 ret = 0;
2096 goto out_unlock;
2097 }
2098 ret = ccw_device_online(cdev);
2099 if (ret)
2100 goto out_disc_unlock;
2101
2102 cm_enabled = cdev->private->cmb != NULL;
2103 spin_unlock_irq(sch->lock);
2104
2105 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
2106 if (cdev->private->state != DEV_STATE_ONLINE) {
2107 spin_lock_irq(sch->lock);
2108 goto out_disc_unlock;
2109 }
2110 if (cm_enabled) {
2111 ret = ccw_set_cmf(cdev, 1);
2112 if (ret) {
2113 CIO_MSG_EVENT(2, "resume: cdev %s: cmf failed "
2114 "(rc=%d)\n", dev_name(&cdev->dev), ret);
2115 ret = 0;
2116 }
2117 }
2118
2119out_restore:
2120 if (cdev->online && cdev->drv && cdev->drv->restore)
2121 ret = cdev->drv->restore(cdev);
2122out:
2123 return ret;
2124
2125out_disc_unlock:
2126 ret = resume_handle_disc(cdev);
2127 spin_unlock_irq(sch->lock);
2128 if (ret)
2129 return ret;
2130 goto out_restore;
2131
2132out_unreg_unlock:
2133 ccw_device_schedule_sch_unregister(cdev);
2134 ret = -ENODEV;
2135out_unlock:
2136 spin_unlock_irq(sch->lock);
2137 return ret;
2138}
2139
2140static struct dev_pm_ops ccw_pm_ops = {
2141 .prepare = ccw_device_pm_prepare,
2142 .complete = ccw_device_pm_complete,
2143 .freeze = ccw_device_pm_freeze,
2144 .thaw = ccw_device_pm_thaw,
2145 .restore = ccw_device_pm_restore,
2146};
2147
Cornelia Huck8bbace72006-01-11 10:56:22 +01002148struct bus_type ccw_bus_type = {
2149 .name = "ccw",
2150 .match = ccw_bus_match,
2151 .uevent = ccw_uevent,
2152 .probe = ccw_device_probe,
2153 .remove = ccw_device_remove,
Cornelia Huck958974f2007-10-12 16:11:21 +02002154 .shutdown = ccw_device_shutdown,
Sebastian Ott823d4942009-06-16 10:30:20 +02002155 .pm = &ccw_pm_ops,
Cornelia Huck8bbace72006-01-11 10:56:22 +01002156};
2157
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +02002158/**
2159 * ccw_driver_register() - register a ccw driver
2160 * @cdriver: driver to be registered
2161 *
2162 * This function is mainly a wrapper around driver_register().
2163 * Returns:
2164 * %0 on success and a negative error value on failure.
2165 */
2166int ccw_driver_register(struct ccw_driver *cdriver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167{
2168 struct device_driver *drv = &cdriver->driver;
2169
2170 drv->bus = &ccw_bus_type;
2171 drv->name = cdriver->name;
Cornelia Huck4beee642008-01-26 14:10:47 +01002172 drv->owner = cdriver->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173
2174 return driver_register(drv);
2175}
2176
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +02002177/**
2178 * ccw_driver_unregister() - deregister a ccw driver
2179 * @cdriver: driver to be deregistered
2180 *
2181 * This function is mainly a wrapper around driver_unregister().
2182 */
2183void ccw_driver_unregister(struct ccw_driver *cdriver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184{
2185 driver_unregister(&cdriver->driver);
2186}
2187
Cornelia Hucka8237fc2006-01-06 00:19:21 -08002188/* Helper func for qdio. */
2189struct subchannel_id
2190ccw_device_get_subchannel_id(struct ccw_device *cdev)
2191{
2192 struct subchannel *sch;
2193
2194 sch = to_subchannel(cdev->dev.parent);
2195 return sch->schid;
2196}
2197
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198MODULE_LICENSE("GPL");
2199EXPORT_SYMBOL(ccw_device_set_online);
2200EXPORT_SYMBOL(ccw_device_set_offline);
2201EXPORT_SYMBOL(ccw_driver_register);
2202EXPORT_SYMBOL(ccw_driver_unregister);
2203EXPORT_SYMBOL(get_ccwdev_by_busid);
2204EXPORT_SYMBOL(ccw_bus_type);
2205EXPORT_SYMBOL(ccw_device_work);
Cornelia Hucka8237fc2006-01-06 00:19:21 -08002206EXPORT_SYMBOL_GPL(ccw_device_get_subchannel_id);