blob: 4e78c82194b47520c271be8617ac5202644d395c [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 Huckf7e5d672007-05-10 15:45:43 +0200141static struct css_driver io_subchannel_driver = {
Cornelia Huck4beee642008-01-26 14:10:47 +0100142 .owner = THIS_MODULE,
Cornelia Huckf08adc02008-07-14 09:59:03 +0200143 .subchannel_type = io_subchannel_ids,
Cornelia Huck25b7bb52008-01-26 14:10:41 +0100144 .name = "io_subchannel",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 .irq = io_subchannel_irq,
Cornelia Huckc820de32008-07-14 09:58:45 +0200146 .sch_event = io_subchannel_sch_event,
147 .chp_event = io_subchannel_chp_event,
Cornelia Huck8bbace72006-01-11 10:56:22 +0100148 .probe = io_subchannel_probe,
149 .remove = io_subchannel_remove,
150 .shutdown = io_subchannel_shutdown,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151};
152
153struct workqueue_struct *ccw_device_work;
Peter Oberparleiter40154b82006-06-29 14:57:03 +0200154wait_queue_head_t ccw_device_init_wq;
155atomic_t ccw_device_init_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
Peter Oberparleiter90ab1332008-01-26 14:10:52 +0100157static void recovery_func(unsigned long data);
158
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159static int __init
160init_ccw_bus_type (void)
161{
162 int ret;
163
164 init_waitqueue_head(&ccw_device_init_wq);
165 atomic_set(&ccw_device_init_count, 0);
Peter Oberparleiter90ab1332008-01-26 14:10:52 +0100166 setup_timer(&recovery_timer, recovery_func, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
168 ccw_device_work = create_singlethread_workqueue("cio");
169 if (!ccw_device_work)
170 return -ENOMEM; /* FIXME: better errno ? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 slow_path_wq = create_singlethread_workqueue("kslowcrw");
172 if (!slow_path_wq) {
173 ret = -ENOMEM; /* FIXME: better errno ? */
174 goto out_err;
175 }
176 if ((ret = bus_register (&ccw_bus_type)))
177 goto out_err;
178
Cornelia Huck25b7bb52008-01-26 14:10:41 +0100179 ret = css_driver_register(&io_subchannel_driver);
180 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 goto out_err;
182
183 wait_event(ccw_device_init_wq,
184 atomic_read(&ccw_device_init_count) == 0);
185 flush_workqueue(ccw_device_work);
186 return 0;
187out_err:
188 if (ccw_device_work)
189 destroy_workqueue(ccw_device_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 if (slow_path_wq)
191 destroy_workqueue(slow_path_wq);
192 return ret;
193}
194
195static void __exit
196cleanup_ccw_bus_type (void)
197{
Cornelia Huck25b7bb52008-01-26 14:10:41 +0100198 css_driver_unregister(&io_subchannel_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 bus_unregister(&ccw_bus_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 destroy_workqueue(ccw_device_work);
201}
202
203subsys_initcall(init_ccw_bus_type);
204module_exit(cleanup_ccw_bus_type);
205
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{
Cornelia Huck7674da72006-12-08 15:54:21 +0100296 if (test_and_clear_bit(1, &cdev->private->registered))
Cornelia Huckef995162007-04-27 16:01:39 +0200297 device_del(&cdev->dev);
Cornelia Huck7674da72006-12-08 15:54:21 +0100298}
299
Peter Oberparleiter46fbe4e2008-10-10 21:33:05 +0200300static void ccw_device_remove_orphan_cb(struct work_struct *work)
Cornelia Huck59a8a6e2007-05-31 17:38:06 +0200301{
Peter Oberparleiter46fbe4e2008-10-10 21:33:05 +0200302 struct ccw_device_private *priv;
303 struct ccw_device *cdev;
Cornelia Huck59a8a6e2007-05-31 17:38:06 +0200304
Peter Oberparleiter46fbe4e2008-10-10 21:33:05 +0200305 priv = container_of(work, struct ccw_device_private, kick_work);
306 cdev = priv->cdev;
Cornelia Huck59a8a6e2007-05-31 17:38:06 +0200307 ccw_device_unregister(cdev);
308 put_device(&cdev->dev);
Peter Oberparleiter46fbe4e2008-10-10 21:33:05 +0200309 /* Release cdev reference for workqueue processing. */
310 put_device(&cdev->dev);
Cornelia Huck59a8a6e2007-05-31 17:38:06 +0200311}
312
Peter Oberparleiter46fbe4e2008-10-10 21:33:05 +0200313static void ccw_device_call_sch_unregister(struct work_struct *work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
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 */
Peter Oberparleiter46fbe4e2008-10-10 21:33:05 +0200324 /* Get cdev reference for workqueue processing. */
325 if (!get_device(&cdev->dev))
326 return;
Cornelia Huck59a8a6e2007-05-31 17:38:06 +0200327 if (ccw_device_is_orphan(cdev)) {
328 /*
329 * Deregister ccw device.
330 * Unfortunately, we cannot do this directly from the
331 * attribute method.
332 */
333 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);
338 } else
339 /* Deregister subchannel, which will kill the ccw device. */
340 PREPARE_WORK(&cdev->private->kick_work,
341 ccw_device_call_sch_unregister);
342 queue_work(slow_path_wq, &cdev->private->kick_work);
Cornelia Huck59a8a6e2007-05-31 17:38:06 +0200343}
344
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +0200345/**
346 * ccw_device_set_offline() - disable a ccw device for I/O
347 * @cdev: target ccw device
348 *
349 * This function calls the driver's set_offline() function for @cdev, if
350 * given, and then disables @cdev.
351 * Returns:
352 * %0 on success and a negative error value on failure.
353 * Context:
354 * enabled, ccw device lock not held
355 */
356int ccw_device_set_offline(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357{
358 int ret;
359
360 if (!cdev)
361 return -ENODEV;
362 if (!cdev->online || !cdev->drv)
363 return -EINVAL;
364
365 if (cdev->drv->set_offline) {
366 ret = cdev->drv->set_offline(cdev);
367 if (ret != 0)
368 return ret;
369 }
370 cdev->online = 0;
371 spin_lock_irq(cdev->ccwlock);
372 ret = ccw_device_offline(cdev);
373 if (ret == -ENODEV) {
374 if (cdev->private->state != DEV_STATE_NOT_OPER) {
375 cdev->private->state = DEV_STATE_OFFLINE;
376 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
377 }
378 spin_unlock_irq(cdev->ccwlock);
379 return ret;
380 }
381 spin_unlock_irq(cdev->ccwlock);
382 if (ret == 0)
383 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
384 else {
Michael Ernst139b83d2008-05-07 09:22:54 +0200385 CIO_MSG_EVENT(0, "ccw_device_offline returned %d, "
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200386 "device 0.%x.%04x\n",
387 ret, cdev->private->dev_id.ssid,
388 cdev->private->dev_id.devno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 cdev->online = 1;
390 }
391 return ret;
392}
393
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +0200394/**
395 * ccw_device_set_online() - enable a ccw device for I/O
396 * @cdev: target ccw device
397 *
398 * This function first enables @cdev and then calls the driver's set_online()
399 * function for @cdev, if given. If set_online() returns an error, @cdev is
400 * disabled again.
401 * Returns:
402 * %0 on success and a negative error value on failure.
403 * Context:
404 * enabled, ccw device lock not held
405 */
406int ccw_device_set_online(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407{
408 int ret;
409
410 if (!cdev)
411 return -ENODEV;
412 if (cdev->online || !cdev->drv)
413 return -EINVAL;
414
415 spin_lock_irq(cdev->ccwlock);
416 ret = ccw_device_online(cdev);
417 spin_unlock_irq(cdev->ccwlock);
418 if (ret == 0)
419 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
420 else {
Michael Ernst139b83d2008-05-07 09:22:54 +0200421 CIO_MSG_EVENT(0, "ccw_device_online returned %d, "
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200422 "device 0.%x.%04x\n",
423 ret, cdev->private->dev_id.ssid,
424 cdev->private->dev_id.devno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 return ret;
426 }
427 if (cdev->private->state != DEV_STATE_ONLINE)
428 return -ENODEV;
429 if (!cdev->drv->set_online || cdev->drv->set_online(cdev) == 0) {
430 cdev->online = 1;
431 return 0;
432 }
433 spin_lock_irq(cdev->ccwlock);
434 ret = ccw_device_offline(cdev);
435 spin_unlock_irq(cdev->ccwlock);
436 if (ret == 0)
437 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200438 else
Michael Ernst139b83d2008-05-07 09:22:54 +0200439 CIO_MSG_EVENT(0, "ccw_device_offline 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 Huck6cadb782006-02-17 13:52:49 -0800443 return (ret == 0) ? -ENODEV : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444}
445
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200446static void online_store_handle_offline(struct ccw_device *cdev)
447{
448 if (cdev->private->state == DEV_STATE_DISCONNECTED)
449 ccw_device_remove_disconnected(cdev);
450 else if (cdev->drv && cdev->drv->set_offline)
451 ccw_device_set_offline(cdev);
452}
453
454static int online_store_recog_and_online(struct ccw_device *cdev)
455{
456 int ret;
457
458 /* Do device recognition, if needed. */
459 if (cdev->id.cu_type == 0) {
460 ret = ccw_device_recognition(cdev);
461 if (ret) {
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200462 CIO_MSG_EVENT(0, "Couldn't start recognition "
463 "for device 0.%x.%04x (ret=%d)\n",
464 cdev->private->dev_id.ssid,
465 cdev->private->dev_id.devno, ret);
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200466 return ret;
467 }
468 wait_event(cdev->private->wait_q,
469 cdev->private->flags.recog_done);
470 }
471 if (cdev->drv && cdev->drv->set_online)
472 ccw_device_set_online(cdev);
473 return 0;
474}
Michael Ernstc78aa6c2008-07-14 09:59:22 +0200475static int online_store_handle_online(struct ccw_device *cdev, int force)
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200476{
477 int ret;
478
479 ret = online_store_recog_and_online(cdev);
480 if (ret)
Michael Ernstc78aa6c2008-07-14 09:59:22 +0200481 return ret;
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200482 if (force && cdev->private->state == DEV_STATE_BOXED) {
483 ret = ccw_device_stlck(cdev);
Michael Ernstc78aa6c2008-07-14 09:59:22 +0200484 if (ret)
485 return ret;
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200486 if (cdev->id.cu_type == 0)
487 cdev->private->state = DEV_STATE_NOT_OPER;
488 online_store_recog_and_online(cdev);
489 }
Michael Ernstc78aa6c2008-07-14 09:59:22 +0200490 return 0;
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200491}
492
493static ssize_t online_store (struct device *dev, struct device_attribute *attr,
494 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495{
496 struct ccw_device *cdev = to_ccwdev(dev);
Cornelia Huck2f972202008-04-30 13:38:33 +0200497 int force, ret;
498 unsigned long i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
Martin Schwidefsky973bd992006-01-06 00:19:07 -0800500 if (atomic_cmpxchg(&cdev->private->onoff, 0, 1) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 return -EAGAIN;
502
503 if (cdev->drv && !try_module_get(cdev->drv->owner)) {
504 atomic_set(&cdev->private->onoff, 0);
505 return -EINVAL;
506 }
507 if (!strncmp(buf, "force\n", count)) {
508 force = 1;
509 i = 1;
Cornelia Huck2f972202008-04-30 13:38:33 +0200510 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 } else {
512 force = 0;
Cornelia Huck2f972202008-04-30 13:38:33 +0200513 ret = strict_strtoul(buf, 16, &i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 }
Cornelia Huck2f972202008-04-30 13:38:33 +0200515 if (ret)
516 goto out;
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200517 switch (i) {
518 case 0:
519 online_store_handle_offline(cdev);
Cornelia Huck2f972202008-04-30 13:38:33 +0200520 ret = count;
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200521 break;
522 case 1:
Michael Ernstc78aa6c2008-07-14 09:59:22 +0200523 ret = online_store_handle_online(cdev, force);
524 if (!ret)
525 ret = count;
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200526 break;
527 default:
Cornelia Huck2f972202008-04-30 13:38:33 +0200528 ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 }
Cornelia Huck2f972202008-04-30 13:38:33 +0200530out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 if (cdev->drv)
532 module_put(cdev->drv->owner);
533 atomic_set(&cdev->private->onoff, 0);
Cornelia Huck2f972202008-04-30 13:38:33 +0200534 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535}
536
537static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400538available_show (struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539{
540 struct ccw_device *cdev = to_ccwdev(dev);
541 struct subchannel *sch;
542
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100543 if (ccw_device_is_orphan(cdev))
544 return sprintf(buf, "no device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 switch (cdev->private->state) {
546 case DEV_STATE_BOXED:
547 return sprintf(buf, "boxed\n");
548 case DEV_STATE_DISCONNECTED:
549 case DEV_STATE_DISCONNECTED_SENSE_ID:
550 case DEV_STATE_NOT_OPER:
551 sch = to_subchannel(dev->parent);
552 if (!sch->lpm)
553 return sprintf(buf, "no path\n");
554 else
555 return sprintf(buf, "no device\n");
556 default:
557 /* All other states considered fine. */
558 return sprintf(buf, "good\n");
559 }
560}
561
562static DEVICE_ATTR(chpids, 0444, chpids_show, NULL);
563static DEVICE_ATTR(pimpampom, 0444, pimpampom_show, NULL);
564static DEVICE_ATTR(devtype, 0444, devtype_show, NULL);
565static DEVICE_ATTR(cutype, 0444, cutype_show, NULL);
Bastian Blankf1fc78a2005-10-30 15:00:12 -0800566static DEVICE_ATTR(modalias, 0444, modalias_show, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567static DEVICE_ATTR(online, 0644, online_show, online_store);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568static DEVICE_ATTR(availability, 0444, available_show, NULL);
569
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200570static struct attribute *io_subchannel_attrs[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 &dev_attr_chpids.attr,
572 &dev_attr_pimpampom.attr,
573 NULL,
574};
575
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200576static struct attribute_group io_subchannel_attr_group = {
577 .attrs = io_subchannel_attrs,
Cornelia Huck529192f2006-12-08 15:55:57 +0100578};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
580static struct attribute * ccwdev_attrs[] = {
581 &dev_attr_devtype.attr,
582 &dev_attr_cutype.attr,
Bastian Blankf1fc78a2005-10-30 15:00:12 -0800583 &dev_attr_modalias.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 &dev_attr_online.attr,
585 &dev_attr_cmb_enable.attr,
586 &dev_attr_availability.attr,
587 NULL,
588};
589
590static struct attribute_group ccwdev_attr_group = {
591 .attrs = ccwdev_attrs,
592};
593
Cornelia Huckf7e5d672007-05-10 15:45:43 +0200594static struct attribute_group *ccwdev_attr_groups[] = {
Cornelia Huckef995162007-04-27 16:01:39 +0200595 &ccwdev_attr_group,
596 NULL,
597};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598
599/* this is a simple abstraction for device_register that sets the
600 * correct bus type and adds the bus specific files */
Cornelia Huck3c9da7b2006-10-27 12:39:33 +0200601static int ccw_device_register(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602{
603 struct device *dev = &cdev->dev;
604 int ret;
605
606 dev->bus = &ccw_bus_type;
607
608 if ((ret = device_add(dev)))
609 return ret;
610
611 set_bit(1, &cdev->private->registered);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 return ret;
613}
614
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700615struct match_data {
Cornelia Huck78964262006-10-11 15:31:38 +0200616 struct ccw_dev_id dev_id;
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700617 struct ccw_device * sibling;
618};
619
620static int
621match_devno(struct device * dev, void * data)
622{
Cornelia Huck78964262006-10-11 15:31:38 +0200623 struct match_data * d = data;
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700624 struct ccw_device * cdev;
625
626 cdev = to_ccwdev(dev);
627 if ((cdev->private->state == DEV_STATE_DISCONNECTED) &&
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100628 !ccw_device_is_orphan(cdev) &&
Cornelia Huck78964262006-10-11 15:31:38 +0200629 ccw_dev_id_is_equal(&cdev->private->dev_id, &d->dev_id) &&
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100630 (cdev != d->sibling))
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700631 return 1;
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700632 return 0;
633}
634
Cornelia Huck78964262006-10-11 15:31:38 +0200635static struct ccw_device * get_disc_ccwdev_by_dev_id(struct ccw_dev_id *dev_id,
636 struct ccw_device *sibling)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 struct device *dev;
Heiko Carstens292888c2006-08-30 14:33:35 +0200639 struct match_data data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
Cornelia Huck78964262006-10-11 15:31:38 +0200641 data.dev_id = *dev_id;
Heiko Carstens292888c2006-08-30 14:33:35 +0200642 data.sibling = sibling;
Cornelia Hucke5945b42005-10-11 08:28:59 -0700643 dev = bus_find_device(&ccw_bus_type, NULL, &data, match_devno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700645 return dev ? to_ccwdev(dev) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646}
647
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100648static int match_orphan(struct device *dev, void *data)
649{
650 struct ccw_dev_id *dev_id;
651 struct ccw_device *cdev;
652
653 dev_id = data;
654 cdev = to_ccwdev(dev);
655 return ccw_dev_id_is_equal(&cdev->private->dev_id, dev_id);
656}
657
658static struct ccw_device *
659get_orphaned_ccwdev_by_dev_id(struct channel_subsystem *css,
660 struct ccw_dev_id *dev_id)
661{
662 struct device *dev;
663
664 dev = device_find_child(&css->pseudo_subchannel->dev, dev_id,
665 match_orphan);
666
667 return dev ? to_ccwdev(dev) : NULL;
668}
669
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670static void
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100671ccw_device_add_changed(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672{
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100673 struct ccw_device_private *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 struct ccw_device *cdev;
675
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100676 priv = container_of(work, struct ccw_device_private, kick_work);
677 cdev = priv->cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 if (device_add(&cdev->dev)) {
679 put_device(&cdev->dev);
680 return;
681 }
682 set_bit(1, &cdev->private->registered);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683}
684
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100685void ccw_device_do_unreg_rereg(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686{
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100687 struct ccw_device_private *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 struct ccw_device *cdev;
689 struct subchannel *sch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100691 priv = container_of(work, struct ccw_device_private, kick_work);
692 cdev = priv->cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 sch = to_subchannel(cdev->dev.parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
Cornelia Huckef995162007-04-27 16:01:39 +0200695 ccw_device_unregister(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 PREPARE_WORK(&cdev->private->kick_work,
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100697 ccw_device_add_changed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 queue_work(ccw_device_work, &cdev->private->kick_work);
699}
700
701static void
702ccw_device_release(struct device *dev)
703{
704 struct ccw_device *cdev;
705
706 cdev = to_ccwdev(dev);
707 kfree(cdev->private);
708 kfree(cdev);
709}
710
Cornelia Huck7674da72006-12-08 15:54:21 +0100711static struct ccw_device * io_subchannel_allocate_dev(struct subchannel *sch)
712{
713 struct ccw_device *cdev;
714
715 cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
716 if (cdev) {
717 cdev->private = kzalloc(sizeof(struct ccw_device_private),
718 GFP_KERNEL | GFP_DMA);
719 if (cdev->private)
720 return cdev;
721 }
722 kfree(cdev);
723 return ERR_PTR(-ENOMEM);
724}
725
726static int io_subchannel_initialize_dev(struct subchannel *sch,
727 struct ccw_device *cdev)
728{
729 cdev->private->cdev = cdev;
730 atomic_set(&cdev->private->onoff, 0);
731 cdev->dev.parent = &sch->dev;
732 cdev->dev.release = ccw_device_release;
Heiko Carstens33583c32007-11-05 11:10:07 +0100733 INIT_WORK(&cdev->private->kick_work, NULL);
Cornelia Huckef995162007-04-27 16:01:39 +0200734 cdev->dev.groups = ccwdev_attr_groups;
Cornelia Huck7674da72006-12-08 15:54:21 +0100735 /* Do first half of device_register. */
736 device_initialize(&cdev->dev);
737 if (!get_device(&sch->dev)) {
738 if (cdev->dev.release)
739 cdev->dev.release(&cdev->dev);
740 return -ENODEV;
741 }
742 return 0;
743}
744
745static struct ccw_device * io_subchannel_create_ccwdev(struct subchannel *sch)
746{
747 struct ccw_device *cdev;
748 int ret;
749
750 cdev = io_subchannel_allocate_dev(sch);
751 if (!IS_ERR(cdev)) {
752 ret = io_subchannel_initialize_dev(sch, cdev);
753 if (ret) {
754 kfree(cdev);
755 cdev = ERR_PTR(ret);
756 }
757 }
758 return cdev;
759}
760
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100761static int io_subchannel_recog(struct ccw_device *, struct subchannel *);
762
763static void sch_attach_device(struct subchannel *sch,
764 struct ccw_device *cdev)
765{
Cornelia Huck82b7ac02007-04-27 16:01:36 +0200766 css_update_ssd_info(sch);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100767 spin_lock_irq(sch->lock);
Cornelia Huckdb6a6422008-01-26 14:10:46 +0100768 sch_set_cdev(sch, cdev);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100769 cdev->private->schid = sch->schid;
770 cdev->ccwlock = sch->lock;
Cornelia Huckc820de32008-07-14 09:58:45 +0200771 ccw_device_trigger_reprobe(cdev);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100772 spin_unlock_irq(sch->lock);
773}
774
775static void sch_attach_disconnected_device(struct subchannel *sch,
776 struct ccw_device *cdev)
777{
778 struct subchannel *other_sch;
779 int ret;
780
781 other_sch = to_subchannel(get_device(cdev->dev.parent));
782 ret = device_move(&cdev->dev, &sch->dev);
783 if (ret) {
Michael Ernst139b83d2008-05-07 09:22:54 +0200784 CIO_MSG_EVENT(0, "Moving disconnected device 0.%x.%04x failed "
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100785 "(ret=%d)!\n", cdev->private->dev_id.ssid,
786 cdev->private->dev_id.devno, ret);
787 put_device(&other_sch->dev);
788 return;
789 }
Cornelia Huckdb6a6422008-01-26 14:10:46 +0100790 sch_set_cdev(other_sch, NULL);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100791 /* No need to keep a subchannel without ccw device around. */
792 css_sch_device_unregister(other_sch);
793 put_device(&other_sch->dev);
794 sch_attach_device(sch, cdev);
795}
796
797static void sch_attach_orphaned_device(struct subchannel *sch,
798 struct ccw_device *cdev)
799{
800 int ret;
801
802 /* Try to move the ccw device to its new subchannel. */
803 ret = device_move(&cdev->dev, &sch->dev);
804 if (ret) {
805 CIO_MSG_EVENT(0, "Moving device 0.%x.%04x from orphanage "
806 "failed (ret=%d)!\n",
807 cdev->private->dev_id.ssid,
808 cdev->private->dev_id.devno, ret);
809 return;
810 }
811 sch_attach_device(sch, cdev);
812}
813
814static void sch_create_and_recog_new_device(struct subchannel *sch)
815{
816 struct ccw_device *cdev;
817
818 /* Need to allocate a new ccw device. */
819 cdev = io_subchannel_create_ccwdev(sch);
820 if (IS_ERR(cdev)) {
821 /* OK, we did everything we could... */
822 css_sch_device_unregister(sch);
823 return;
824 }
825 spin_lock_irq(sch->lock);
Cornelia Huckdb6a6422008-01-26 14:10:46 +0100826 sch_set_cdev(sch, cdev);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100827 spin_unlock_irq(sch->lock);
828 /* Start recognition for the new ccw device. */
829 if (io_subchannel_recog(cdev, sch)) {
830 spin_lock_irq(sch->lock);
Cornelia Huckdb6a6422008-01-26 14:10:46 +0100831 sch_set_cdev(sch, NULL);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100832 spin_unlock_irq(sch->lock);
833 if (cdev->dev.release)
834 cdev->dev.release(&cdev->dev);
835 css_sch_device_unregister(sch);
836 }
837}
838
839
840void ccw_device_move_to_orphanage(struct work_struct *work)
841{
842 struct ccw_device_private *priv;
843 struct ccw_device *cdev;
844 struct ccw_device *replacing_cdev;
845 struct subchannel *sch;
846 int ret;
847 struct channel_subsystem *css;
848 struct ccw_dev_id dev_id;
849
850 priv = container_of(work, struct ccw_device_private, kick_work);
851 cdev = priv->cdev;
852 sch = to_subchannel(cdev->dev.parent);
853 css = to_css(sch->dev.parent);
854 dev_id.devno = sch->schib.pmcw.dev;
855 dev_id.ssid = sch->schid.ssid;
856
857 /*
858 * Move the orphaned ccw device to the orphanage so the replacing
859 * ccw device can take its place on the subchannel.
860 */
861 ret = device_move(&cdev->dev, &css->pseudo_subchannel->dev);
862 if (ret) {
863 CIO_MSG_EVENT(0, "Moving device 0.%x.%04x to orphanage failed "
864 "(ret=%d)!\n", cdev->private->dev_id.ssid,
865 cdev->private->dev_id.devno, ret);
866 return;
867 }
868 cdev->ccwlock = css->pseudo_subchannel->lock;
869 /*
870 * Search for the replacing ccw device
871 * - among the disconnected devices
872 * - in the orphanage
873 */
874 replacing_cdev = get_disc_ccwdev_by_dev_id(&dev_id, cdev);
875 if (replacing_cdev) {
876 sch_attach_disconnected_device(sch, replacing_cdev);
877 return;
878 }
879 replacing_cdev = get_orphaned_ccwdev_by_dev_id(css, &dev_id);
880 if (replacing_cdev) {
881 sch_attach_orphaned_device(sch, replacing_cdev);
882 return;
883 }
884 sch_create_and_recog_new_device(sch);
885}
886
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887/*
888 * Register recognized device.
889 */
890static void
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100891io_subchannel_register(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892{
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100893 struct ccw_device_private *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 struct ccw_device *cdev;
895 struct subchannel *sch;
896 int ret;
897 unsigned long flags;
898
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100899 priv = container_of(work, struct ccw_device_private, kick_work);
900 cdev = priv->cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 sch = to_subchannel(cdev->dev.parent);
Cornelia Huck82b7ac02007-04-27 16:01:36 +0200902 css_update_ssd_info(sch);
Cornelia Huck47af5512006-12-04 15:41:07 +0100903 /*
904 * io_subchannel_register() will also be called after device
905 * recognition has been done for a boxed device (which will already
906 * be registered). We need to reprobe since we may now have sense id
907 * information.
908 */
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700909 if (klist_node_attached(&cdev->dev.knode_parent)) {
Cornelia Huck47af5512006-12-04 15:41:07 +0100910 if (!cdev->drv) {
911 ret = device_reprobe(&cdev->dev);
912 if (ret)
913 /* We can't do much here. */
Michael Ernst139b83d2008-05-07 09:22:54 +0200914 CIO_MSG_EVENT(0, "device_reprobe() returned"
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200915 " %d for 0.%x.%04x\n", ret,
916 cdev->private->dev_id.ssid,
917 cdev->private->dev_id.devno);
Cornelia Huck47af5512006-12-04 15:41:07 +0100918 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 goto out;
920 }
Cornelia Huckfa1a8c22007-04-26 00:12:03 -0700921 /*
922 * Now we know this subchannel will stay, we can throw
923 * our delayed uevent.
924 */
925 sch->dev.uevent_suppress = 0;
926 kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 /* make it known to the system */
928 ret = ccw_device_register(cdev);
929 if (ret) {
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200930 CIO_MSG_EVENT(0, "Could not register ccw dev 0.%x.%04x: %d\n",
931 cdev->private->dev_id.ssid,
932 cdev->private->dev_id.devno, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 put_device(&cdev->dev);
Cornelia Huck2ec22982006-12-08 15:54:26 +0100934 spin_lock_irqsave(sch->lock, flags);
Cornelia Huckdb6a6422008-01-26 14:10:46 +0100935 sch_set_cdev(sch, NULL);
Cornelia Huck2ec22982006-12-08 15:54:26 +0100936 spin_unlock_irqrestore(sch->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 kfree (cdev->private);
938 kfree (cdev);
939 put_device(&sch->dev);
940 if (atomic_dec_and_test(&ccw_device_init_count))
941 wake_up(&ccw_device_init_wq);
942 return;
943 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 put_device(&cdev->dev);
945out:
946 cdev->private->flags.recog_done = 1;
947 put_device(&sch->dev);
948 wake_up(&cdev->private->wait_q);
949 if (atomic_dec_and_test(&ccw_device_init_count))
950 wake_up(&ccw_device_init_wq);
951}
952
Cornelia Huck3f4cf6e2007-10-12 16:11:26 +0200953static void ccw_device_call_sch_unregister(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954{
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100955 struct ccw_device_private *priv;
956 struct ccw_device *cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 struct subchannel *sch;
958
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100959 priv = container_of(work, struct ccw_device_private, kick_work);
960 cdev = priv->cdev;
Peter Oberparleiter46fbe4e2008-10-10 21:33:05 +0200961 /* Get subchannel reference for local processing. */
962 if (!get_device(cdev->dev.parent))
963 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 sch = to_subchannel(cdev->dev.parent);
Cornelia Huck6ab48792006-07-12 16:39:50 +0200965 css_sch_device_unregister(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 /* Reset intparm to zeroes. */
967 sch->schib.pmcw.intparm = 0;
968 cio_modify(sch);
Peter Oberparleiter46fbe4e2008-10-10 21:33:05 +0200969 /* Release cdev reference for workqueue processing.*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 put_device(&cdev->dev);
Peter Oberparleiter46fbe4e2008-10-10 21:33:05 +0200971 /* Release subchannel reference for local processing. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 put_device(&sch->dev);
973}
974
975/*
976 * subchannel recognition done. Called from the state machine.
977 */
978void
979io_subchannel_recog_done(struct ccw_device *cdev)
980{
981 struct subchannel *sch;
982
983 if (css_init_done == 0) {
984 cdev->private->flags.recog_done = 1;
985 return;
986 }
987 switch (cdev->private->state) {
988 case DEV_STATE_NOT_OPER:
989 cdev->private->flags.recog_done = 1;
990 /* Remove device found not operational. */
991 if (!get_device(&cdev->dev))
992 break;
993 sch = to_subchannel(cdev->dev.parent);
994 PREPARE_WORK(&cdev->private->kick_work,
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100995 ccw_device_call_sch_unregister);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 queue_work(slow_path_wq, &cdev->private->kick_work);
Peter Oberparleiter46fbe4e2008-10-10 21:33:05 +0200997 /* Release subchannel reference for asynchronous recognition. */
998 put_device(&sch->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 if (atomic_dec_and_test(&ccw_device_init_count))
1000 wake_up(&ccw_device_init_wq);
1001 break;
1002 case DEV_STATE_BOXED:
1003 /* Device did not respond in time. */
1004 case DEV_STATE_OFFLINE:
1005 /*
1006 * We can't register the device in interrupt context so
1007 * we schedule a work item.
1008 */
1009 if (!get_device(&cdev->dev))
1010 break;
1011 PREPARE_WORK(&cdev->private->kick_work,
Martin Schwidefskyc1637532006-12-08 15:53:57 +01001012 io_subchannel_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 queue_work(slow_path_wq, &cdev->private->kick_work);
1014 break;
1015 }
1016}
1017
1018static int
1019io_subchannel_recog(struct ccw_device *cdev, struct subchannel *sch)
1020{
1021 int rc;
1022 struct ccw_device_private *priv;
1023
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001024 sch_set_cdev(sch, cdev);
Cornelia Huck2ec22982006-12-08 15:54:26 +01001025 cdev->ccwlock = sch->lock;
Cornelia Huckfb6958a2006-01-06 00:19:25 -08001026
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 /* Init private data. */
1028 priv = cdev->private;
Cornelia Huck78964262006-10-11 15:31:38 +02001029 priv->dev_id.devno = sch->schib.pmcw.dev;
1030 priv->dev_id.ssid = sch->schid.ssid;
1031 priv->schid = sch->schid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 priv->state = DEV_STATE_NOT_OPER;
1033 INIT_LIST_HEAD(&priv->cmb_list);
1034 init_waitqueue_head(&priv->wait_q);
1035 init_timer(&priv->timer);
1036
1037 /* Set an initial name for the device. */
Cornelia Huck1f4e7ed2008-10-10 21:33:14 +02001038 if (cio_is_console(sch->schid))
1039 cdev->dev.init_name = cio_get_console_cdev_name(sch);
1040 else
1041 dev_set_name(&cdev->dev, "0.%x.%04x",
1042 sch->schid.ssid, sch->schib.pmcw.dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043
1044 /* Increase counter of devices currently in recognition. */
1045 atomic_inc(&ccw_device_init_count);
1046
1047 /* Start async. device sensing. */
Cornelia Huck2ec22982006-12-08 15:54:26 +01001048 spin_lock_irq(sch->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 rc = ccw_device_recognition(cdev);
Cornelia Huck2ec22982006-12-08 15:54:26 +01001050 spin_unlock_irq(sch->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 if (rc) {
1052 if (atomic_dec_and_test(&ccw_device_init_count))
1053 wake_up(&ccw_device_init_wq);
1054 }
1055 return rc;
1056}
1057
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +01001058static void ccw_device_move_to_sch(struct work_struct *work)
1059{
1060 struct ccw_device_private *priv;
1061 int rc;
1062 struct subchannel *sch;
1063 struct ccw_device *cdev;
1064 struct subchannel *former_parent;
1065
1066 priv = container_of(work, struct ccw_device_private, kick_work);
1067 sch = priv->sch;
1068 cdev = priv->cdev;
1069 former_parent = ccw_device_is_orphan(cdev) ?
1070 NULL : to_subchannel(get_device(cdev->dev.parent));
1071 mutex_lock(&sch->reg_mutex);
1072 /* Try to move the ccw device to its new subchannel. */
1073 rc = device_move(&cdev->dev, &sch->dev);
1074 mutex_unlock(&sch->reg_mutex);
1075 if (rc) {
Michael Ernst139b83d2008-05-07 09:22:54 +02001076 CIO_MSG_EVENT(0, "Moving device 0.%x.%04x to subchannel "
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +01001077 "0.%x.%04x failed (ret=%d)!\n",
1078 cdev->private->dev_id.ssid,
1079 cdev->private->dev_id.devno, sch->schid.ssid,
1080 sch->schid.sch_no, rc);
1081 css_sch_device_unregister(sch);
1082 goto out;
1083 }
1084 if (former_parent) {
1085 spin_lock_irq(former_parent->lock);
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001086 sch_set_cdev(former_parent, NULL);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +01001087 spin_unlock_irq(former_parent->lock);
1088 css_sch_device_unregister(former_parent);
1089 /* Reset intparm to zeroes. */
1090 former_parent->schib.pmcw.intparm = 0;
1091 cio_modify(former_parent);
1092 }
1093 sch_attach_device(sch, cdev);
1094out:
1095 if (former_parent)
1096 put_device(&former_parent->dev);
1097 put_device(&cdev->dev);
1098}
1099
Cornelia Huck602b20f2008-01-26 14:10:39 +01001100static void io_subchannel_irq(struct subchannel *sch)
1101{
1102 struct ccw_device *cdev;
1103
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001104 cdev = sch_get_cdev(sch);
Cornelia Huck602b20f2008-01-26 14:10:39 +01001105
1106 CIO_TRACE_EVENT(3, "IRQ");
Kay Sievers2a0217d2008-10-10 21:33:09 +02001107 CIO_TRACE_EVENT(3, dev_name(&sch->dev));
Cornelia Huck602b20f2008-01-26 14:10:39 +01001108 if (cdev)
1109 dev_fsm_event(cdev, DEV_EVENT_INTERRUPT);
1110}
1111
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001112static void io_subchannel_init_fields(struct subchannel *sch)
1113{
1114 if (cio_is_console(sch->schid))
1115 sch->opm = 0xff;
1116 else
1117 sch->opm = chp_get_sch_opm(sch);
1118 sch->lpm = sch->schib.pmcw.pam & sch->opm;
Cornelia Huck3a3fc292008-07-14 09:58:58 +02001119 sch->isc = cio_is_console(sch->schid) ? CONSOLE_ISC : IO_SCH_ISC;
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001120
1121 CIO_MSG_EVENT(6, "Detected device %04x on subchannel 0.%x.%04X"
1122 " - PIM = %02X, PAM = %02X, POM = %02X\n",
1123 sch->schib.pmcw.dev, sch->schid.ssid,
1124 sch->schid.sch_no, sch->schib.pmcw.pim,
1125 sch->schib.pmcw.pam, sch->schib.pmcw.pom);
1126 /* Initially set up some fields in the pmcw. */
1127 sch->schib.pmcw.ena = 0;
1128 sch->schib.pmcw.csense = 1; /* concurrent sense */
1129 if ((sch->lpm & (sch->lpm - 1)) != 0)
1130 sch->schib.pmcw.mp = 1; /* multipath mode */
1131 /* clean up possible residual cmf stuff */
1132 sch->schib.pmcw.mme = 0;
1133 sch->schib.pmcw.mbfc = 0;
1134 sch->schib.pmcw.mbi = 0;
1135 sch->schib.mba = 0;
1136}
1137
1138static int io_subchannel_probe(struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 struct ccw_device *cdev;
1141 int rc;
1142 unsigned long flags;
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +01001143 struct ccw_dev_id dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001145 cdev = sch_get_cdev(sch);
1146 if (cdev) {
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001147 rc = sysfs_create_group(&sch->dev.kobj,
1148 &io_subchannel_attr_group);
1149 if (rc)
1150 CIO_MSG_EVENT(0, "Failed to create io subchannel "
1151 "attributes for subchannel "
1152 "0.%x.%04x (rc=%d)\n",
1153 sch->schid.ssid, sch->schid.sch_no, rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 /*
1155 * This subchannel already has an associated ccw_device.
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001156 * Throw the delayed uevent for the subchannel, register
1157 * the ccw_device and exit. This happens for all early
1158 * devices, e.g. the console.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 */
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001160 sch->dev.uevent_suppress = 0;
1161 kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
Cornelia Hucke1031782007-10-12 16:11:23 +02001162 cdev->dev.groups = ccwdev_attr_groups;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 device_initialize(&cdev->dev);
1164 ccw_device_register(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 /*
1166 * Check if the device is already online. If it is
1167 * the reference count needs to be corrected
1168 * (see ccw_device_online and css_init_done for the
1169 * ugly details).
1170 */
1171 if (cdev->private->state != DEV_STATE_NOT_OPER &&
1172 cdev->private->state != DEV_STATE_OFFLINE &&
1173 cdev->private->state != DEV_STATE_BOXED)
1174 get_device(&cdev->dev);
1175 return 0;
1176 }
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001177 io_subchannel_init_fields(sch);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +01001178 /*
1179 * First check if a fitting device may be found amongst the
1180 * disconnected devices or in the orphanage.
1181 */
1182 dev_id.devno = sch->schib.pmcw.dev;
1183 dev_id.ssid = sch->schid.ssid;
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001184 rc = sysfs_create_group(&sch->dev.kobj,
1185 &io_subchannel_attr_group);
1186 if (rc)
1187 return rc;
Cornelia Huckcd6b4f22008-01-26 14:10:43 +01001188 /* Allocate I/O subchannel private data. */
1189 sch->private = kzalloc(sizeof(struct io_subchannel_private),
1190 GFP_KERNEL | GFP_DMA);
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001191 if (!sch->private) {
1192 rc = -ENOMEM;
1193 goto out_err;
1194 }
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +01001195 cdev = get_disc_ccwdev_by_dev_id(&dev_id, NULL);
1196 if (!cdev)
1197 cdev = get_orphaned_ccwdev_by_dev_id(to_css(sch->dev.parent),
1198 &dev_id);
1199 if (cdev) {
1200 /*
1201 * Schedule moving the device until when we have a registered
1202 * subchannel to move to and succeed the probe. We can
1203 * unregister later again, when the probe is through.
1204 */
1205 cdev->private->sch = sch;
1206 PREPARE_WORK(&cdev->private->kick_work,
1207 ccw_device_move_to_sch);
1208 queue_work(slow_path_wq, &cdev->private->kick_work);
1209 return 0;
1210 }
Cornelia Huck7674da72006-12-08 15:54:21 +01001211 cdev = io_subchannel_create_ccwdev(sch);
Cornelia Huckcd6b4f22008-01-26 14:10:43 +01001212 if (IS_ERR(cdev)) {
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001213 rc = PTR_ERR(cdev);
1214 goto out_err;
Cornelia Huckcd6b4f22008-01-26 14:10:43 +01001215 }
Cornelia Huck8bbace72006-01-11 10:56:22 +01001216 rc = io_subchannel_recog(cdev, sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 if (rc) {
Cornelia Huck2ec22982006-12-08 15:54:26 +01001218 spin_lock_irqsave(sch->lock, flags);
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001219 sch_set_cdev(sch, NULL);
Cornelia Huck2ec22982006-12-08 15:54:26 +01001220 spin_unlock_irqrestore(sch->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 if (cdev->dev.release)
1222 cdev->dev.release(&cdev->dev);
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001223 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 }
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001225 return 0;
1226out_err:
1227 kfree(sch->private);
1228 sysfs_remove_group(&sch->dev.kobj, &io_subchannel_attr_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 return rc;
1230}
1231
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232static int
Cornelia Huck8bbace72006-01-11 10:56:22 +01001233io_subchannel_remove (struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234{
1235 struct ccw_device *cdev;
1236 unsigned long flags;
1237
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001238 cdev = sch_get_cdev(sch);
1239 if (!cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 /* Set ccw device to not operational and drop reference. */
1242 spin_lock_irqsave(cdev->ccwlock, flags);
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001243 sch_set_cdev(sch, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 cdev->private->state = DEV_STATE_NOT_OPER;
1245 spin_unlock_irqrestore(cdev->ccwlock, flags);
Cornelia Huckef995162007-04-27 16:01:39 +02001246 ccw_device_unregister(cdev);
1247 put_device(&cdev->dev);
Cornelia Huckcd6b4f22008-01-26 14:10:43 +01001248 kfree(sch->private);
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001249 sysfs_remove_group(&sch->dev.kobj, &io_subchannel_attr_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 return 0;
1251}
1252
Cornelia Huck602b20f2008-01-26 14:10:39 +01001253static int io_subchannel_notify(struct subchannel *sch, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254{
1255 struct ccw_device *cdev;
1256
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001257 cdev = sch_get_cdev(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 if (!cdev)
1259 return 0;
Cornelia Huckc820de32008-07-14 09:58:45 +02001260 return ccw_device_notify(cdev, event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261}
1262
Cornelia Huck602b20f2008-01-26 14:10:39 +01001263static void io_subchannel_verify(struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264{
1265 struct ccw_device *cdev;
1266
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001267 cdev = sch_get_cdev(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 if (cdev)
1269 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1270}
1271
Cornelia Huckc820de32008-07-14 09:58:45 +02001272static int check_for_io_on_path(struct subchannel *sch, int mask)
1273{
1274 int cc;
1275
1276 cc = stsch(sch->schid, &sch->schib);
1277 if (cc)
1278 return 0;
Peter Oberparleiter23d805b2008-07-14 09:58:50 +02001279 if (scsw_actl(&sch->schib.scsw) && sch->schib.pmcw.lpum == mask)
Cornelia Huckc820de32008-07-14 09:58:45 +02001280 return 1;
1281 return 0;
1282}
1283
1284static void terminate_internal_io(struct subchannel *sch,
1285 struct ccw_device *cdev)
1286{
1287 if (cio_clear(sch)) {
1288 /* Recheck device in case clear failed. */
1289 sch->lpm = 0;
1290 if (cdev->online)
1291 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1292 else
1293 css_schedule_eval(sch->schid);
1294 return;
1295 }
1296 cdev->private->state = DEV_STATE_CLEAR_VERIFY;
1297 /* Request retry of internal operation. */
1298 cdev->private->flags.intretry = 1;
1299 /* Call handler. */
1300 if (cdev->handler)
1301 cdev->handler(cdev, cdev->private->intparm,
1302 ERR_PTR(-EIO));
1303}
1304
1305static void io_subchannel_terminate_path(struct subchannel *sch, u8 mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306{
1307 struct ccw_device *cdev;
1308
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001309 cdev = sch_get_cdev(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 if (!cdev)
1311 return;
Cornelia Huckc820de32008-07-14 09:58:45 +02001312 if (check_for_io_on_path(sch, mask)) {
1313 if (cdev->private->state == DEV_STATE_ONLINE)
1314 ccw_device_kill_io(cdev);
1315 else {
1316 terminate_internal_io(sch, cdev);
1317 /* Re-start path verification. */
1318 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1319 }
1320 } else
1321 /* trigger path verification. */
1322 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1323
1324}
1325
Cornelia Huck99611f82008-07-14 09:59:02 +02001326static int io_subchannel_chp_event(struct subchannel *sch,
1327 struct chp_link *link, int event)
Cornelia Huckc820de32008-07-14 09:58:45 +02001328{
1329 int mask;
Cornelia Huckc820de32008-07-14 09:58:45 +02001330
Cornelia Huck99611f82008-07-14 09:59:02 +02001331 mask = chp_ssd_get_mask(&sch->ssd_info, link);
Cornelia Huckc820de32008-07-14 09:58:45 +02001332 if (!mask)
1333 return 0;
1334 switch (event) {
1335 case CHP_VARY_OFF:
1336 sch->opm &= ~mask;
1337 sch->lpm &= ~mask;
1338 io_subchannel_terminate_path(sch, mask);
1339 break;
1340 case CHP_VARY_ON:
1341 sch->opm |= mask;
1342 sch->lpm |= mask;
1343 io_subchannel_verify(sch);
1344 break;
1345 case CHP_OFFLINE:
1346 if (stsch(sch->schid, &sch->schib))
1347 return -ENXIO;
1348 if (!css_sch_is_valid(&sch->schib))
1349 return -ENODEV;
1350 io_subchannel_terminate_path(sch, mask);
1351 break;
1352 case CHP_ONLINE:
1353 if (stsch(sch->schid, &sch->schib))
1354 return -ENXIO;
1355 sch->lpm |= mask & sch->opm;
1356 io_subchannel_verify(sch);
1357 break;
1358 }
1359 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360}
1361
1362static void
Cornelia Huck8bbace72006-01-11 10:56:22 +01001363io_subchannel_shutdown(struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 struct ccw_device *cdev;
1366 int ret;
1367
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001368 cdev = sch_get_cdev(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369
Cornelia Hucka8237fc2006-01-06 00:19:21 -08001370 if (cio_is_console(sch->schid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 return;
1372 if (!sch->schib.pmcw.ena)
1373 /* Nothing to do. */
1374 return;
1375 ret = cio_disable_subchannel(sch);
1376 if (ret != -EBUSY)
1377 /* Subchannel is disabled, we're done. */
1378 return;
1379 cdev->private->state = DEV_STATE_QUIESCE;
1380 if (cdev->handler)
1381 cdev->handler(cdev, cdev->private->intparm,
1382 ERR_PTR(-EIO));
1383 ret = ccw_device_cancel_halt_clear(cdev);
1384 if (ret == -EBUSY) {
1385 ccw_device_set_timeout(cdev, HZ/10);
1386 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
1387 }
1388 cio_disable_subchannel(sch);
1389}
1390
Cornelia Huckc820de32008-07-14 09:58:45 +02001391static int io_subchannel_get_status(struct subchannel *sch)
1392{
1393 struct schib schib;
1394
1395 if (stsch(sch->schid, &schib) || !schib.pmcw.dnv)
1396 return CIO_GONE;
1397 if (sch->schib.pmcw.dnv && (schib.pmcw.dev != sch->schib.pmcw.dev))
1398 return CIO_REVALIDATE;
1399 if (!sch->lpm)
1400 return CIO_NO_PATH;
1401 return CIO_OPER;
1402}
1403
1404static int device_is_disconnected(struct ccw_device *cdev)
1405{
1406 if (!cdev)
1407 return 0;
1408 return (cdev->private->state == DEV_STATE_DISCONNECTED ||
1409 cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID);
1410}
1411
1412static int recovery_check(struct device *dev, void *data)
1413{
1414 struct ccw_device *cdev = to_ccwdev(dev);
1415 int *redo = data;
1416
1417 spin_lock_irq(cdev->ccwlock);
1418 switch (cdev->private->state) {
1419 case DEV_STATE_DISCONNECTED:
1420 CIO_MSG_EVENT(3, "recovery: trigger 0.%x.%04x\n",
1421 cdev->private->dev_id.ssid,
1422 cdev->private->dev_id.devno);
1423 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1424 *redo = 1;
1425 break;
1426 case DEV_STATE_DISCONNECTED_SENSE_ID:
1427 *redo = 1;
1428 break;
1429 }
1430 spin_unlock_irq(cdev->ccwlock);
1431
1432 return 0;
1433}
1434
1435static void recovery_work_func(struct work_struct *unused)
1436{
1437 int redo = 0;
1438
1439 bus_for_each_dev(&ccw_bus_type, NULL, &redo, recovery_check);
1440 if (redo) {
1441 spin_lock_irq(&recovery_lock);
1442 if (!timer_pending(&recovery_timer)) {
1443 if (recovery_phase < ARRAY_SIZE(recovery_delay) - 1)
1444 recovery_phase++;
1445 mod_timer(&recovery_timer, jiffies +
1446 recovery_delay[recovery_phase] * HZ);
1447 }
1448 spin_unlock_irq(&recovery_lock);
1449 } else
1450 CIO_MSG_EVENT(4, "recovery: end\n");
1451}
1452
1453static DECLARE_WORK(recovery_work, recovery_work_func);
1454
1455static void recovery_func(unsigned long data)
1456{
1457 /*
1458 * We can't do our recovery in softirq context and it's not
1459 * performance critical, so we schedule it.
1460 */
1461 schedule_work(&recovery_work);
1462}
1463
1464static void ccw_device_schedule_recovery(void)
1465{
1466 unsigned long flags;
1467
1468 CIO_MSG_EVENT(4, "recovery: schedule\n");
1469 spin_lock_irqsave(&recovery_lock, flags);
1470 if (!timer_pending(&recovery_timer) || (recovery_phase != 0)) {
1471 recovery_phase = 0;
1472 mod_timer(&recovery_timer, jiffies + recovery_delay[0] * HZ);
1473 }
1474 spin_unlock_irqrestore(&recovery_lock, flags);
1475}
1476
Peter Oberparleiterecf5d9e2008-10-10 21:33:06 +02001477static int purge_fn(struct device *dev, void *data)
1478{
1479 struct ccw_device *cdev = to_ccwdev(dev);
1480 struct ccw_device_private *priv = cdev->private;
1481 int unreg;
1482
1483 spin_lock_irq(cdev->ccwlock);
1484 unreg = is_blacklisted(priv->dev_id.ssid, priv->dev_id.devno) &&
1485 (priv->state == DEV_STATE_OFFLINE);
1486 spin_unlock_irq(cdev->ccwlock);
1487 if (!unreg)
1488 goto out;
1489 if (!get_device(&cdev->dev))
1490 goto out;
1491 CIO_MSG_EVENT(3, "ccw: purging 0.%x.%04x\n", priv->dev_id.ssid,
1492 priv->dev_id.devno);
1493 PREPARE_WORK(&cdev->private->kick_work, ccw_device_call_sch_unregister);
1494 queue_work(slow_path_wq, &cdev->private->kick_work);
1495
1496out:
1497 /* Abort loop in case of pending signal. */
1498 if (signal_pending(current))
1499 return -EINTR;
1500
1501 return 0;
1502}
1503
1504/**
1505 * ccw_purge_blacklisted - purge unused, blacklisted devices
1506 *
1507 * Unregister all ccw devices that are offline and on the blacklist.
1508 */
1509int ccw_purge_blacklisted(void)
1510{
1511 CIO_MSG_EVENT(2, "ccw: purging blacklisted devices\n");
1512 bus_for_each_dev(&ccw_bus_type, NULL, NULL, purge_fn);
1513 return 0;
1514}
1515
Cornelia Huckc820de32008-07-14 09:58:45 +02001516static void device_set_disconnected(struct ccw_device *cdev)
1517{
1518 if (!cdev)
1519 return;
1520 ccw_device_set_timeout(cdev, 0);
1521 cdev->private->flags.fake_irb = 0;
1522 cdev->private->state = DEV_STATE_DISCONNECTED;
1523 if (cdev->online)
1524 ccw_device_schedule_recovery();
1525}
1526
Peter Oberparleiter91c36912008-08-21 19:46:39 +02001527void ccw_device_set_notoper(struct ccw_device *cdev)
1528{
1529 struct subchannel *sch = to_subchannel(cdev->dev.parent);
1530
1531 CIO_TRACE_EVENT(2, "notoper");
Cornelia Huckb9d3aed2008-10-10 21:33:11 +02001532 CIO_TRACE_EVENT(2, dev_name(&sch->dev));
Peter Oberparleiter91c36912008-08-21 19:46:39 +02001533 ccw_device_set_timeout(cdev, 0);
1534 cio_disable_subchannel(sch);
1535 cdev->private->state = DEV_STATE_NOT_OPER;
1536}
1537
Cornelia Huckc820de32008-07-14 09:58:45 +02001538static int io_subchannel_sch_event(struct subchannel *sch, int slow)
1539{
1540 int event, ret, disc;
1541 unsigned long flags;
Peter Oberparleiter91c36912008-08-21 19:46:39 +02001542 enum { NONE, UNREGISTER, UNREGISTER_PROBE, REPROBE, DISC } action;
Cornelia Huckc820de32008-07-14 09:58:45 +02001543 struct ccw_device *cdev;
1544
1545 spin_lock_irqsave(sch->lock, flags);
1546 cdev = sch_get_cdev(sch);
1547 disc = device_is_disconnected(cdev);
1548 if (disc && slow) {
1549 /* Disconnected devices are evaluated directly only.*/
1550 spin_unlock_irqrestore(sch->lock, flags);
1551 return 0;
1552 }
1553 /* No interrupt after machine check - kill pending timers. */
1554 if (cdev)
1555 ccw_device_set_timeout(cdev, 0);
1556 if (!disc && !slow) {
1557 /* Non-disconnected devices are evaluated on the slow path. */
1558 spin_unlock_irqrestore(sch->lock, flags);
1559 return -EAGAIN;
1560 }
1561 event = io_subchannel_get_status(sch);
1562 CIO_MSG_EVENT(4, "Evaluating schid 0.%x.%04x, event %d, %s, %s path.\n",
1563 sch->schid.ssid, sch->schid.sch_no, event,
1564 disc ? "disconnected" : "normal",
1565 slow ? "slow" : "fast");
1566 /* Analyze subchannel status. */
1567 action = NONE;
1568 switch (event) {
1569 case CIO_NO_PATH:
1570 if (disc) {
1571 /* Check if paths have become available. */
1572 action = REPROBE;
1573 break;
1574 }
1575 /* fall through */
1576 case CIO_GONE:
Cornelia Huckc820de32008-07-14 09:58:45 +02001577 /* Ask driver what to do with device. */
Peter Oberparleiter91c36912008-08-21 19:46:39 +02001578 if (io_subchannel_notify(sch, event))
1579 action = DISC;
1580 else
1581 action = UNREGISTER;
Cornelia Huckc820de32008-07-14 09:58:45 +02001582 break;
1583 case CIO_REVALIDATE:
1584 /* Device will be removed, so no notify necessary. */
1585 if (disc)
1586 /* Reprobe because immediate unregister might block. */
1587 action = REPROBE;
1588 else
1589 action = UNREGISTER_PROBE;
1590 break;
1591 case CIO_OPER:
1592 if (disc)
1593 /* Get device operational again. */
1594 action = REPROBE;
1595 break;
1596 }
1597 /* Perform action. */
1598 ret = 0;
1599 switch (action) {
1600 case UNREGISTER:
1601 case UNREGISTER_PROBE:
Peter Oberparleiter91c36912008-08-21 19:46:39 +02001602 ccw_device_set_notoper(cdev);
Cornelia Huckc820de32008-07-14 09:58:45 +02001603 /* Unregister device (will use subchannel lock). */
1604 spin_unlock_irqrestore(sch->lock, flags);
1605 css_sch_device_unregister(sch);
1606 spin_lock_irqsave(sch->lock, flags);
1607
1608 /* Reset intparm to zeroes. */
1609 sch->schib.pmcw.intparm = 0;
1610 cio_modify(sch);
1611 break;
1612 case REPROBE:
1613 ccw_device_trigger_reprobe(cdev);
1614 break;
Peter Oberparleiter91c36912008-08-21 19:46:39 +02001615 case DISC:
1616 device_set_disconnected(cdev);
1617 break;
Cornelia Huckc820de32008-07-14 09:58:45 +02001618 default:
1619 break;
1620 }
1621 spin_unlock_irqrestore(sch->lock, flags);
1622 /* Probe if necessary. */
1623 if (action == UNREGISTER_PROBE)
1624 ret = css_probe_device(sch->schid);
1625
1626 return ret;
1627}
1628
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629#ifdef CONFIG_CCW_CONSOLE
1630static struct ccw_device console_cdev;
Cornelia Huck1f4e7ed2008-10-10 21:33:14 +02001631static char console_cdev_name[10] = "0.x.xxxx";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632static struct ccw_device_private console_private;
1633static int console_cdev_in_use;
1634
Cornelia Huck2ec22982006-12-08 15:54:26 +01001635static DEFINE_SPINLOCK(ccw_console_lock);
1636
1637spinlock_t * cio_get_console_lock(void)
1638{
1639 return &ccw_console_lock;
1640}
1641
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001642static int ccw_device_console_enable(struct ccw_device *cdev,
1643 struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644{
1645 int rc;
1646
Cornelia Huckcd6b4f22008-01-26 14:10:43 +01001647 /* Attach subchannel private data. */
1648 sch->private = cio_get_console_priv();
1649 memset(sch->private, 0, sizeof(struct io_subchannel_private));
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001650 io_subchannel_init_fields(sch);
1651 sch->driver = &io_subchannel_driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 /* Initialize the ccw_device structure. */
Heiko Carstens292888c2006-08-30 14:33:35 +02001653 cdev->dev.parent= &sch->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 rc = io_subchannel_recog(cdev, sch);
1655 if (rc)
1656 return rc;
1657
1658 /* Now wait for the async. recognition to come to an end. */
1659 spin_lock_irq(cdev->ccwlock);
1660 while (!dev_fsm_final_state(cdev))
1661 wait_cons_dev();
1662 rc = -EIO;
1663 if (cdev->private->state != DEV_STATE_OFFLINE)
1664 goto out_unlock;
1665 ccw_device_online(cdev);
1666 while (!dev_fsm_final_state(cdev))
1667 wait_cons_dev();
1668 if (cdev->private->state != DEV_STATE_ONLINE)
1669 goto out_unlock;
1670 rc = 0;
1671out_unlock:
1672 spin_unlock_irq(cdev->ccwlock);
1673 return 0;
1674}
1675
1676struct ccw_device *
1677ccw_device_probe_console(void)
1678{
1679 struct subchannel *sch;
1680 int ret;
1681
1682 if (xchg(&console_cdev_in_use, 1) != 0)
Peter Oberparleiter600b5d12006-02-01 03:06:35 -08001683 return ERR_PTR(-EBUSY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 sch = cio_probe_console();
1685 if (IS_ERR(sch)) {
1686 console_cdev_in_use = 0;
1687 return (void *) sch;
1688 }
1689 memset(&console_cdev, 0, sizeof(struct ccw_device));
1690 memset(&console_private, 0, sizeof(struct ccw_device_private));
1691 console_cdev.private = &console_private;
Martin Schwidefskyc1637532006-12-08 15:53:57 +01001692 console_private.cdev = &console_cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 ret = ccw_device_console_enable(&console_cdev, sch);
1694 if (ret) {
1695 cio_release_console();
1696 console_cdev_in_use = 0;
1697 return ERR_PTR(ret);
1698 }
1699 console_cdev.online = 1;
1700 return &console_cdev;
1701}
Cornelia Huck1f4e7ed2008-10-10 21:33:14 +02001702
1703
1704const char *cio_get_console_cdev_name(struct subchannel *sch)
1705{
1706 snprintf(console_cdev_name, 10, "0.%x.%04x",
1707 sch->schid.ssid, sch->schib.pmcw.dev);
1708 return (const char *)console_cdev_name;
1709}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710#endif
1711
1712/*
1713 * get ccw_device matching the busid, but only if owned by cdrv
1714 */
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001715static int
1716__ccwdev_check_busid(struct device *dev, void *id)
1717{
1718 char *bus_id;
1719
Cornelia Huck12975ae2006-10-11 15:31:47 +02001720 bus_id = id;
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001721
Kay Sievers2a0217d2008-10-10 21:33:09 +02001722 return (strncmp(bus_id, dev_name(dev), BUS_ID_SIZE) == 0);
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001723}
1724
1725
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +02001726/**
1727 * get_ccwdev_by_busid() - obtain device from a bus id
1728 * @cdrv: driver the device is owned by
1729 * @bus_id: bus id of the device to be searched
1730 *
1731 * This function searches all devices owned by @cdrv for a device with a bus
1732 * id matching @bus_id.
1733 * Returns:
1734 * If a match is found, its reference count of the found device is increased
1735 * and it is returned; else %NULL is returned.
1736 */
1737struct ccw_device *get_ccwdev_by_busid(struct ccw_driver *cdrv,
1738 const char *bus_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739{
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001740 struct device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 struct device_driver *drv;
1742
1743 drv = get_driver(&cdrv->driver);
1744 if (!drv)
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001745 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001747 dev = driver_find_device(drv, NULL, (void *)bus_id,
1748 __ccwdev_check_busid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 put_driver(drv);
1750
Heiko Carstensd2c993d2006-07-12 16:41:55 +02001751 return dev ? to_ccwdev(dev) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752}
1753
1754/************************** device driver handling ************************/
1755
1756/* This is the implementation of the ccw_driver class. The probe, remove
1757 * and release methods are initially very similar to the device_driver
1758 * implementations, with the difference that they have ccw_device
1759 * arguments.
1760 *
1761 * A ccw driver also contains the information that is needed for
1762 * device matching.
1763 */
1764static int
1765ccw_device_probe (struct device *dev)
1766{
1767 struct ccw_device *cdev = to_ccwdev(dev);
1768 struct ccw_driver *cdrv = to_ccwdrv(dev->driver);
1769 int ret;
1770
1771 cdev->drv = cdrv; /* to let the driver call _set_online */
1772
1773 ret = cdrv->probe ? cdrv->probe(cdev) : -ENODEV;
1774
1775 if (ret) {
Heiko Carstensd2c993d2006-07-12 16:41:55 +02001776 cdev->drv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 return ret;
1778 }
1779
1780 return 0;
1781}
1782
1783static int
1784ccw_device_remove (struct device *dev)
1785{
1786 struct ccw_device *cdev = to_ccwdev(dev);
1787 struct ccw_driver *cdrv = cdev->drv;
1788 int ret;
1789
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 if (cdrv->remove)
1791 cdrv->remove(cdev);
1792 if (cdev->online) {
1793 cdev->online = 0;
1794 spin_lock_irq(cdev->ccwlock);
1795 ret = ccw_device_offline(cdev);
1796 spin_unlock_irq(cdev->ccwlock);
1797 if (ret == 0)
1798 wait_event(cdev->private->wait_q,
1799 dev_fsm_final_state(cdev));
1800 else
Michael Ernst139b83d2008-05-07 09:22:54 +02001801 CIO_MSG_EVENT(0, "ccw_device_offline returned %d, "
Cornelia Hucke556bbb2007-07-27 12:29:19 +02001802 "device 0.%x.%04x\n",
1803 ret, cdev->private->dev_id.ssid,
1804 cdev->private->dev_id.devno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 }
1806 ccw_device_set_timeout(cdev, 0);
Heiko Carstensd2c993d2006-07-12 16:41:55 +02001807 cdev->drv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 return 0;
1809}
1810
Cornelia Huck958974fb2007-10-12 16:11:21 +02001811static void ccw_device_shutdown(struct device *dev)
1812{
1813 struct ccw_device *cdev;
1814
1815 cdev = to_ccwdev(dev);
1816 if (cdev->drv && cdev->drv->shutdown)
1817 cdev->drv->shutdown(cdev);
Cornelia Huck1842f2b2007-10-12 16:11:22 +02001818 disable_cmf(cdev);
Cornelia Huck958974fb2007-10-12 16:11:21 +02001819}
1820
Cornelia Huck8bbace72006-01-11 10:56:22 +01001821struct bus_type ccw_bus_type = {
1822 .name = "ccw",
1823 .match = ccw_bus_match,
1824 .uevent = ccw_uevent,
1825 .probe = ccw_device_probe,
1826 .remove = ccw_device_remove,
Cornelia Huck958974fb2007-10-12 16:11:21 +02001827 .shutdown = ccw_device_shutdown,
Cornelia Huck8bbace72006-01-11 10:56:22 +01001828};
1829
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +02001830/**
1831 * ccw_driver_register() - register a ccw driver
1832 * @cdriver: driver to be registered
1833 *
1834 * This function is mainly a wrapper around driver_register().
1835 * Returns:
1836 * %0 on success and a negative error value on failure.
1837 */
1838int ccw_driver_register(struct ccw_driver *cdriver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839{
1840 struct device_driver *drv = &cdriver->driver;
1841
1842 drv->bus = &ccw_bus_type;
1843 drv->name = cdriver->name;
Cornelia Huck4beee642008-01-26 14:10:47 +01001844 drv->owner = cdriver->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845
1846 return driver_register(drv);
1847}
1848
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +02001849/**
1850 * ccw_driver_unregister() - deregister a ccw driver
1851 * @cdriver: driver to be deregistered
1852 *
1853 * This function is mainly a wrapper around driver_unregister().
1854 */
1855void ccw_driver_unregister(struct ccw_driver *cdriver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856{
1857 driver_unregister(&cdriver->driver);
1858}
1859
Cornelia Hucka8237fc2006-01-06 00:19:21 -08001860/* Helper func for qdio. */
1861struct subchannel_id
1862ccw_device_get_subchannel_id(struct ccw_device *cdev)
1863{
1864 struct subchannel *sch;
1865
1866 sch = to_subchannel(cdev->dev.parent);
1867 return sch->schid;
1868}
1869
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870MODULE_LICENSE("GPL");
1871EXPORT_SYMBOL(ccw_device_set_online);
1872EXPORT_SYMBOL(ccw_device_set_offline);
1873EXPORT_SYMBOL(ccw_driver_register);
1874EXPORT_SYMBOL(ccw_driver_unregister);
1875EXPORT_SYMBOL(get_ccwdev_by_busid);
1876EXPORT_SYMBOL(ccw_bus_type);
1877EXPORT_SYMBOL(ccw_device_work);
Cornelia Hucka8237fc2006-01-06 00:19:21 -08001878EXPORT_SYMBOL_GPL(ccw_device_get_subchannel_id);