blob: 522d47afc950d567f9338d963b91fc8da6530adc [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"
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Peter Oberparleiter90ab1332008-01-26 14:10:52 +010035static struct timer_list recovery_timer;
Cornelia Huck486d0a02008-02-19 15:29:23 +010036static DEFINE_SPINLOCK(recovery_lock);
Peter Oberparleiter90ab1332008-01-26 14:10:52 +010037static int recovery_phase;
38static const unsigned long recovery_delay[] = { 3, 30, 300 };
39
Linus Torvalds1da177e2005-04-16 15:20:36 -070040/******************* bus type handling ***********************/
41
42/* The Linux driver model distinguishes between a bus type and
43 * the bus itself. Of course we only have one channel
44 * subsystem driver and one channel system per machine, but
45 * we still use the abstraction. T.R. says it's a good idea. */
46static int
47ccw_bus_match (struct device * dev, struct device_driver * drv)
48{
49 struct ccw_device *cdev = to_ccwdev(dev);
50 struct ccw_driver *cdrv = to_ccwdrv(drv);
51 const struct ccw_device_id *ids = cdrv->ids, *found;
52
53 if (!ids)
54 return 0;
55
56 found = ccw_device_id_match(ids, &cdev->id);
57 if (!found)
58 return 0;
59
60 cdev->id.driver_info = found->driver_info;
61
62 return 1;
63}
64
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +020065/* Store modalias string delimited by prefix/suffix string into buffer with
66 * specified size. Return length of resulting string (excluding trailing '\0')
67 * even if string doesn't fit buffer (snprintf semantics). */
Cornelia Huckcfbe9bb2007-04-27 16:01:32 +020068static int snprint_alias(char *buf, size_t size,
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +020069 struct ccw_device_id *id, const char *suffix)
70{
71 int len;
72
Cornelia Huckcfbe9bb2007-04-27 16:01:32 +020073 len = snprintf(buf, size, "ccw:t%04Xm%02X", id->cu_type, id->cu_model);
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +020074 if (len > size)
75 return len;
76 buf += len;
77 size -= len;
78
79 if (id->dev_type != 0)
80 len += snprintf(buf, size, "dt%04Xdm%02X%s", id->dev_type,
81 id->dev_model, suffix);
82 else
83 len += snprintf(buf, size, "dtdm%s", suffix);
84
85 return len;
86}
87
88/* Set up environment variables for ccw device uevent. Return 0 on success,
89 * non-zero otherwise. */
Kay Sievers7eff2e72007-08-14 15:15:12 +020090static int ccw_uevent(struct device *dev, struct kobj_uevent_env *env)
Linus Torvalds1da177e2005-04-16 15:20:36 -070091{
92 struct ccw_device *cdev = to_ccwdev(dev);
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +020093 struct ccw_device_id *id = &(cdev->id);
Cornelia Huckcfbe9bb2007-04-27 16:01:32 +020094 int ret;
95 char modalias_buf[30];
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +020097 /* CU_TYPE= */
Kay Sievers7eff2e72007-08-14 15:15:12 +020098 ret = add_uevent_var(env, "CU_TYPE=%04X", id->cu_type);
Cornelia Huckcfbe9bb2007-04-27 16:01:32 +020099 if (ret)
100 return ret;
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +0200101
102 /* CU_MODEL= */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200103 ret = add_uevent_var(env, "CU_MODEL=%02X", id->cu_model);
Cornelia Huckcfbe9bb2007-04-27 16:01:32 +0200104 if (ret)
105 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
107 /* The next two can be zero, that's ok for us */
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +0200108 /* DEV_TYPE= */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200109 ret = add_uevent_var(env, "DEV_TYPE=%04X", id->dev_type);
Cornelia Huckcfbe9bb2007-04-27 16:01:32 +0200110 if (ret)
111 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +0200113 /* DEV_MODEL= */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200114 ret = add_uevent_var(env, "DEV_MODEL=%02X", id->dev_model);
Cornelia Huckcfbe9bb2007-04-27 16:01:32 +0200115 if (ret)
116 return ret;
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +0200117
118 /* MODALIAS= */
Cornelia Huckcfbe9bb2007-04-27 16:01:32 +0200119 snprint_alias(modalias_buf, sizeof(modalias_buf), id, "");
Kay Sievers7eff2e72007-08-14 15:15:12 +0200120 ret = add_uevent_var(env, "MODALIAS=%s", modalias_buf);
121 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122}
123
Cornelia Huck8bbace72006-01-11 10:56:22 +0100124struct bus_type ccw_bus_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
Cornelia Huck602b20f2008-01-26 14:10:39 +0100126static void io_subchannel_irq(struct subchannel *);
127static int io_subchannel_probe(struct subchannel *);
128static int io_subchannel_remove(struct subchannel *);
Cornelia Huck8bbace72006-01-11 10:56:22 +0100129static void io_subchannel_shutdown(struct subchannel *);
Cornelia Huckc820de32008-07-14 09:58:45 +0200130static int io_subchannel_sch_event(struct subchannel *, int);
Cornelia Huck99611f82008-07-14 09:59:02 +0200131static int io_subchannel_chp_event(struct subchannel *, struct chp_link *,
132 int);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
Cornelia Huckf7e5d672007-05-10 15:45:43 +0200134static struct css_driver io_subchannel_driver = {
Cornelia Huck4beee642008-01-26 14:10:47 +0100135 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 .subchannel_type = SUBCHANNEL_TYPE_IO,
Cornelia Huck25b7bb52008-01-26 14:10:41 +0100137 .name = "io_subchannel",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 .irq = io_subchannel_irq,
Cornelia Huckc820de32008-07-14 09:58:45 +0200139 .sch_event = io_subchannel_sch_event,
140 .chp_event = io_subchannel_chp_event,
Cornelia Huck8bbace72006-01-11 10:56:22 +0100141 .probe = io_subchannel_probe,
142 .remove = io_subchannel_remove,
143 .shutdown = io_subchannel_shutdown,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144};
145
146struct workqueue_struct *ccw_device_work;
147struct workqueue_struct *ccw_device_notify_work;
Peter Oberparleiter40154b82006-06-29 14:57:03 +0200148wait_queue_head_t ccw_device_init_wq;
149atomic_t ccw_device_init_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
Peter Oberparleiter90ab1332008-01-26 14:10:52 +0100151static void recovery_func(unsigned long data);
152
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153static int __init
154init_ccw_bus_type (void)
155{
156 int ret;
157
158 init_waitqueue_head(&ccw_device_init_wq);
159 atomic_set(&ccw_device_init_count, 0);
Peter Oberparleiter90ab1332008-01-26 14:10:52 +0100160 setup_timer(&recovery_timer, recovery_func, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
162 ccw_device_work = create_singlethread_workqueue("cio");
163 if (!ccw_device_work)
164 return -ENOMEM; /* FIXME: better errno ? */
165 ccw_device_notify_work = create_singlethread_workqueue("cio_notify");
166 if (!ccw_device_notify_work) {
167 ret = -ENOMEM; /* FIXME: better errno ? */
168 goto out_err;
169 }
170 slow_path_wq = create_singlethread_workqueue("kslowcrw");
171 if (!slow_path_wq) {
172 ret = -ENOMEM; /* FIXME: better errno ? */
173 goto out_err;
174 }
175 if ((ret = bus_register (&ccw_bus_type)))
176 goto out_err;
177
Cornelia Huck25b7bb52008-01-26 14:10:41 +0100178 ret = css_driver_register(&io_subchannel_driver);
179 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 goto out_err;
181
182 wait_event(ccw_device_init_wq,
183 atomic_read(&ccw_device_init_count) == 0);
184 flush_workqueue(ccw_device_work);
185 return 0;
186out_err:
187 if (ccw_device_work)
188 destroy_workqueue(ccw_device_work);
189 if (ccw_device_notify_work)
190 destroy_workqueue(ccw_device_notify_work);
191 if (slow_path_wq)
192 destroy_workqueue(slow_path_wq);
193 return ret;
194}
195
196static void __exit
197cleanup_ccw_bus_type (void)
198{
Cornelia Huck25b7bb52008-01-26 14:10:41 +0100199 css_driver_unregister(&io_subchannel_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 bus_unregister(&ccw_bus_type);
201 destroy_workqueue(ccw_device_notify_work);
202 destroy_workqueue(ccw_device_work);
203}
204
205subsys_initcall(init_ccw_bus_type);
206module_exit(cleanup_ccw_bus_type);
207
208/************************ device handling **************************/
209
210/*
211 * A ccw_device has some interfaces in sysfs in addition to the
212 * standard ones.
213 * The following entries are designed to export the information which
214 * resided in 2.4 in /proc/subchannels. Subchannel and device number
215 * are obvious, so they don't have an entry :)
216 * TODO: Split chpids and pimpampom up? Where is "in use" in the tree?
217 */
218static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400219chpids_show (struct device * dev, struct device_attribute *attr, char * buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220{
221 struct subchannel *sch = to_subchannel(dev);
Peter Oberparleiter7ad6a242007-04-27 16:01:35 +0200222 struct chsc_ssd_info *ssd = &sch->ssd_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 ssize_t ret = 0;
224 int chp;
Peter Oberparleiter7ad6a242007-04-27 16:01:35 +0200225 int mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
Peter Oberparleiter7ad6a242007-04-27 16:01:35 +0200227 for (chp = 0; chp < 8; chp++) {
228 mask = 0x80 >> chp;
229 if (ssd->path_mask & mask)
230 ret += sprintf(buf + ret, "%02x ", ssd->chpid[chp].id);
231 else
232 ret += sprintf(buf + ret, "00 ");
233 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 ret += sprintf (buf+ret, "\n");
235 return min((ssize_t)PAGE_SIZE, ret);
236}
237
238static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400239pimpampom_show (struct device * dev, struct device_attribute *attr, char * buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240{
241 struct subchannel *sch = to_subchannel(dev);
242 struct pmcw *pmcw = &sch->schib.pmcw;
243
244 return sprintf (buf, "%02x %02x %02x\n",
245 pmcw->pim, pmcw->pam, pmcw->pom);
246}
247
248static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400249devtype_show (struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250{
251 struct ccw_device *cdev = to_ccwdev(dev);
252 struct ccw_device_id *id = &(cdev->id);
253
254 if (id->dev_type != 0)
255 return sprintf(buf, "%04x/%02x\n",
256 id->dev_type, id->dev_model);
257 else
258 return sprintf(buf, "n/a\n");
259}
260
261static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400262cutype_show (struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263{
264 struct ccw_device *cdev = to_ccwdev(dev);
265 struct ccw_device_id *id = &(cdev->id);
266
267 return sprintf(buf, "%04x/%02x\n",
268 id->cu_type, id->cu_model);
269}
270
271static ssize_t
Bastian Blankf1fc78a2005-10-30 15:00:12 -0800272modalias_show (struct device *dev, struct device_attribute *attr, char *buf)
273{
274 struct ccw_device *cdev = to_ccwdev(dev);
275 struct ccw_device_id *id = &(cdev->id);
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +0200276 int len;
Bastian Blankf1fc78a2005-10-30 15:00:12 -0800277
Cornelia Huck086a6c62007-07-17 13:36:08 +0200278 len = snprint_alias(buf, PAGE_SIZE, id, "\n");
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +0200279
280 return len > PAGE_SIZE ? PAGE_SIZE : len;
Bastian Blankf1fc78a2005-10-30 15:00:12 -0800281}
282
283static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400284online_show (struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285{
286 struct ccw_device *cdev = to_ccwdev(dev);
287
288 return sprintf(buf, cdev->online ? "1\n" : "0\n");
289}
290
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100291int ccw_device_is_orphan(struct ccw_device *cdev)
292{
293 return sch_is_pseudo_sch(to_subchannel(cdev->dev.parent));
294}
295
Cornelia Huckef995162007-04-27 16:01:39 +0200296static void ccw_device_unregister(struct ccw_device *cdev)
Cornelia Huck7674da72006-12-08 15:54:21 +0100297{
Cornelia Huck7674da72006-12-08 15:54:21 +0100298 if (test_and_clear_bit(1, &cdev->private->registered))
Cornelia Huckef995162007-04-27 16:01:39 +0200299 device_del(&cdev->dev);
Cornelia Huck7674da72006-12-08 15:54:21 +0100300}
301
Cornelia Huck59a8a6e2007-05-31 17:38:06 +0200302static void ccw_device_remove_orphan_cb(struct device *dev)
303{
304 struct ccw_device *cdev = to_ccwdev(dev);
305
306 ccw_device_unregister(cdev);
307 put_device(&cdev->dev);
308}
309
310static void ccw_device_remove_sch_cb(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311{
312 struct subchannel *sch;
Cornelia Huck59a8a6e2007-05-31 17:38:06 +0200313
314 sch = to_subchannel(dev);
Cornelia Huck6ab48792006-07-12 16:39:50 +0200315 css_sch_device_unregister(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 /* Reset intparm to zeroes. */
317 sch->schib.pmcw.intparm = 0;
318 cio_modify(sch);
319 put_device(&sch->dev);
320}
321
Cornelia Huck59a8a6e2007-05-31 17:38:06 +0200322static void
323ccw_device_remove_disconnected(struct ccw_device *cdev)
324{
325 unsigned long flags;
326 int rc;
327
328 /*
329 * Forced offline in disconnected state means
330 * 'throw away device'.
331 */
332 if (ccw_device_is_orphan(cdev)) {
333 /*
334 * Deregister ccw device.
335 * Unfortunately, we cannot do this directly from the
336 * attribute method.
337 */
338 spin_lock_irqsave(cdev->ccwlock, flags);
339 cdev->private->state = DEV_STATE_NOT_OPER;
340 spin_unlock_irqrestore(cdev->ccwlock, flags);
341 rc = device_schedule_callback(&cdev->dev,
342 ccw_device_remove_orphan_cb);
343 if (rc)
Michael Ernst139b83d2008-05-07 09:22:54 +0200344 CIO_MSG_EVENT(0, "Couldn't unregister orphan "
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200345 "0.%x.%04x\n",
346 cdev->private->dev_id.ssid,
347 cdev->private->dev_id.devno);
Cornelia Huck59a8a6e2007-05-31 17:38:06 +0200348 return;
349 }
350 /* Deregister subchannel, which will kill the ccw device. */
351 rc = device_schedule_callback(cdev->dev.parent,
352 ccw_device_remove_sch_cb);
353 if (rc)
Michael Ernst139b83d2008-05-07 09:22:54 +0200354 CIO_MSG_EVENT(0, "Couldn't unregister disconnected device "
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200355 "0.%x.%04x\n",
356 cdev->private->dev_id.ssid,
357 cdev->private->dev_id.devno);
Cornelia Huck59a8a6e2007-05-31 17:38:06 +0200358}
359
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +0200360/**
361 * ccw_device_set_offline() - disable a ccw device for I/O
362 * @cdev: target ccw device
363 *
364 * This function calls the driver's set_offline() function for @cdev, if
365 * given, and then disables @cdev.
366 * Returns:
367 * %0 on success and a negative error value on failure.
368 * Context:
369 * enabled, ccw device lock not held
370 */
371int ccw_device_set_offline(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372{
373 int ret;
374
375 if (!cdev)
376 return -ENODEV;
377 if (!cdev->online || !cdev->drv)
378 return -EINVAL;
379
380 if (cdev->drv->set_offline) {
381 ret = cdev->drv->set_offline(cdev);
382 if (ret != 0)
383 return ret;
384 }
385 cdev->online = 0;
386 spin_lock_irq(cdev->ccwlock);
387 ret = ccw_device_offline(cdev);
388 if (ret == -ENODEV) {
389 if (cdev->private->state != DEV_STATE_NOT_OPER) {
390 cdev->private->state = DEV_STATE_OFFLINE;
391 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
392 }
393 spin_unlock_irq(cdev->ccwlock);
394 return ret;
395 }
396 spin_unlock_irq(cdev->ccwlock);
397 if (ret == 0)
398 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
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 }
406 return ret;
407}
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;
429
430 spin_lock_irq(cdev->ccwlock);
431 ret = ccw_device_online(cdev);
432 spin_unlock_irq(cdev->ccwlock);
433 if (ret == 0)
434 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
435 else {
Michael Ernst139b83d2008-05-07 09:22:54 +0200436 CIO_MSG_EVENT(0, "ccw_device_online returned %d, "
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200437 "device 0.%x.%04x\n",
438 ret, cdev->private->dev_id.ssid,
439 cdev->private->dev_id.devno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 return ret;
441 }
442 if (cdev->private->state != DEV_STATE_ONLINE)
443 return -ENODEV;
444 if (!cdev->drv->set_online || cdev->drv->set_online(cdev) == 0) {
445 cdev->online = 1;
446 return 0;
447 }
448 spin_lock_irq(cdev->ccwlock);
449 ret = ccw_device_offline(cdev);
450 spin_unlock_irq(cdev->ccwlock);
451 if (ret == 0)
452 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200453 else
Michael Ernst139b83d2008-05-07 09:22:54 +0200454 CIO_MSG_EVENT(0, "ccw_device_offline returned %d, "
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200455 "device 0.%x.%04x\n",
456 ret, cdev->private->dev_id.ssid,
457 cdev->private->dev_id.devno);
Cornelia Huck6cadb782006-02-17 13:52:49 -0800458 return (ret == 0) ? -ENODEV : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459}
460
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200461static void online_store_handle_offline(struct ccw_device *cdev)
462{
463 if (cdev->private->state == DEV_STATE_DISCONNECTED)
464 ccw_device_remove_disconnected(cdev);
465 else if (cdev->drv && cdev->drv->set_offline)
466 ccw_device_set_offline(cdev);
467}
468
469static int online_store_recog_and_online(struct ccw_device *cdev)
470{
471 int ret;
472
473 /* Do device recognition, if needed. */
474 if (cdev->id.cu_type == 0) {
475 ret = ccw_device_recognition(cdev);
476 if (ret) {
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200477 CIO_MSG_EVENT(0, "Couldn't start recognition "
478 "for device 0.%x.%04x (ret=%d)\n",
479 cdev->private->dev_id.ssid,
480 cdev->private->dev_id.devno, ret);
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200481 return ret;
482 }
483 wait_event(cdev->private->wait_q,
484 cdev->private->flags.recog_done);
485 }
486 if (cdev->drv && cdev->drv->set_online)
487 ccw_device_set_online(cdev);
488 return 0;
489}
490static void online_store_handle_online(struct ccw_device *cdev, int force)
491{
492 int ret;
493
494 ret = online_store_recog_and_online(cdev);
495 if (ret)
496 return;
497 if (force && cdev->private->state == DEV_STATE_BOXED) {
498 ret = ccw_device_stlck(cdev);
499 if (ret) {
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200500 dev_warn(&cdev->dev,
501 "ccw_device_stlck returned %d!\n", ret);
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200502 return;
503 }
504 if (cdev->id.cu_type == 0)
505 cdev->private->state = DEV_STATE_NOT_OPER;
506 online_store_recog_and_online(cdev);
507 }
508
509}
510
511static ssize_t online_store (struct device *dev, struct device_attribute *attr,
512 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513{
514 struct ccw_device *cdev = to_ccwdev(dev);
Cornelia Huck2f972202008-04-30 13:38:33 +0200515 int force, ret;
516 unsigned long i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
Martin Schwidefsky973bd992006-01-06 00:19:07 -0800518 if (atomic_cmpxchg(&cdev->private->onoff, 0, 1) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 return -EAGAIN;
520
521 if (cdev->drv && !try_module_get(cdev->drv->owner)) {
522 atomic_set(&cdev->private->onoff, 0);
523 return -EINVAL;
524 }
525 if (!strncmp(buf, "force\n", count)) {
526 force = 1;
527 i = 1;
Cornelia Huck2f972202008-04-30 13:38:33 +0200528 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 } else {
530 force = 0;
Cornelia Huck2f972202008-04-30 13:38:33 +0200531 ret = strict_strtoul(buf, 16, &i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 }
Cornelia Huck2f972202008-04-30 13:38:33 +0200533 if (ret)
534 goto out;
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200535 switch (i) {
536 case 0:
537 online_store_handle_offline(cdev);
Cornelia Huck2f972202008-04-30 13:38:33 +0200538 ret = count;
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200539 break;
540 case 1:
541 online_store_handle_online(cdev, force);
Cornelia Huck2f972202008-04-30 13:38:33 +0200542 ret = count;
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200543 break;
544 default:
Cornelia Huck2f972202008-04-30 13:38:33 +0200545 ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 }
Cornelia Huck2f972202008-04-30 13:38:33 +0200547out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 if (cdev->drv)
549 module_put(cdev->drv->owner);
550 atomic_set(&cdev->private->onoff, 0);
Cornelia Huck2f972202008-04-30 13:38:33 +0200551 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552}
553
554static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400555available_show (struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556{
557 struct ccw_device *cdev = to_ccwdev(dev);
558 struct subchannel *sch;
559
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100560 if (ccw_device_is_orphan(cdev))
561 return sprintf(buf, "no device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 switch (cdev->private->state) {
563 case DEV_STATE_BOXED:
564 return sprintf(buf, "boxed\n");
565 case DEV_STATE_DISCONNECTED:
566 case DEV_STATE_DISCONNECTED_SENSE_ID:
567 case DEV_STATE_NOT_OPER:
568 sch = to_subchannel(dev->parent);
569 if (!sch->lpm)
570 return sprintf(buf, "no path\n");
571 else
572 return sprintf(buf, "no device\n");
573 default:
574 /* All other states considered fine. */
575 return sprintf(buf, "good\n");
576 }
577}
578
579static DEVICE_ATTR(chpids, 0444, chpids_show, NULL);
580static DEVICE_ATTR(pimpampom, 0444, pimpampom_show, NULL);
581static DEVICE_ATTR(devtype, 0444, devtype_show, NULL);
582static DEVICE_ATTR(cutype, 0444, cutype_show, NULL);
Bastian Blankf1fc78a2005-10-30 15:00:12 -0800583static DEVICE_ATTR(modalias, 0444, modalias_show, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584static DEVICE_ATTR(online, 0644, online_show, online_store);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585static DEVICE_ATTR(availability, 0444, available_show, NULL);
586
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200587static struct attribute *io_subchannel_attrs[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 &dev_attr_chpids.attr,
589 &dev_attr_pimpampom.attr,
590 NULL,
591};
592
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200593static struct attribute_group io_subchannel_attr_group = {
594 .attrs = io_subchannel_attrs,
Cornelia Huck529192f2006-12-08 15:55:57 +0100595};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
597static struct attribute * ccwdev_attrs[] = {
598 &dev_attr_devtype.attr,
599 &dev_attr_cutype.attr,
Bastian Blankf1fc78a2005-10-30 15:00:12 -0800600 &dev_attr_modalias.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 &dev_attr_online.attr,
602 &dev_attr_cmb_enable.attr,
603 &dev_attr_availability.attr,
604 NULL,
605};
606
607static struct attribute_group ccwdev_attr_group = {
608 .attrs = ccwdev_attrs,
609};
610
Cornelia Huckf7e5d672007-05-10 15:45:43 +0200611static struct attribute_group *ccwdev_attr_groups[] = {
Cornelia Huckef995162007-04-27 16:01:39 +0200612 &ccwdev_attr_group,
613 NULL,
614};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
616/* this is a simple abstraction for device_register that sets the
617 * correct bus type and adds the bus specific files */
Cornelia Huck3c9da7b2006-10-27 12:39:33 +0200618static int ccw_device_register(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619{
620 struct device *dev = &cdev->dev;
621 int ret;
622
623 dev->bus = &ccw_bus_type;
624
625 if ((ret = device_add(dev)))
626 return ret;
627
628 set_bit(1, &cdev->private->registered);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 return ret;
630}
631
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700632struct match_data {
Cornelia Huck78964262006-10-11 15:31:38 +0200633 struct ccw_dev_id dev_id;
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700634 struct ccw_device * sibling;
635};
636
637static int
638match_devno(struct device * dev, void * data)
639{
Cornelia Huck78964262006-10-11 15:31:38 +0200640 struct match_data * d = data;
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700641 struct ccw_device * cdev;
642
643 cdev = to_ccwdev(dev);
644 if ((cdev->private->state == DEV_STATE_DISCONNECTED) &&
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100645 !ccw_device_is_orphan(cdev) &&
Cornelia Huck78964262006-10-11 15:31:38 +0200646 ccw_dev_id_is_equal(&cdev->private->dev_id, &d->dev_id) &&
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100647 (cdev != d->sibling))
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700648 return 1;
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700649 return 0;
650}
651
Cornelia Huck78964262006-10-11 15:31:38 +0200652static struct ccw_device * get_disc_ccwdev_by_dev_id(struct ccw_dev_id *dev_id,
653 struct ccw_device *sibling)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 struct device *dev;
Heiko Carstens292888c2006-08-30 14:33:35 +0200656 struct match_data data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
Cornelia Huck78964262006-10-11 15:31:38 +0200658 data.dev_id = *dev_id;
Heiko Carstens292888c2006-08-30 14:33:35 +0200659 data.sibling = sibling;
Cornelia Hucke5945b42005-10-11 08:28:59 -0700660 dev = bus_find_device(&ccw_bus_type, NULL, &data, match_devno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700662 return dev ? to_ccwdev(dev) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663}
664
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100665static int match_orphan(struct device *dev, void *data)
666{
667 struct ccw_dev_id *dev_id;
668 struct ccw_device *cdev;
669
670 dev_id = data;
671 cdev = to_ccwdev(dev);
672 return ccw_dev_id_is_equal(&cdev->private->dev_id, dev_id);
673}
674
675static struct ccw_device *
676get_orphaned_ccwdev_by_dev_id(struct channel_subsystem *css,
677 struct ccw_dev_id *dev_id)
678{
679 struct device *dev;
680
681 dev = device_find_child(&css->pseudo_subchannel->dev, dev_id,
682 match_orphan);
683
684 return dev ? to_ccwdev(dev) : NULL;
685}
686
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687static void
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100688ccw_device_add_changed(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689{
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100690 struct ccw_device_private *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 struct ccw_device *cdev;
692
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100693 priv = container_of(work, struct ccw_device_private, kick_work);
694 cdev = priv->cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 if (device_add(&cdev->dev)) {
696 put_device(&cdev->dev);
697 return;
698 }
699 set_bit(1, &cdev->private->registered);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700}
701
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100702void ccw_device_do_unreg_rereg(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703{
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100704 struct ccw_device_private *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 struct ccw_device *cdev;
706 struct subchannel *sch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100708 priv = container_of(work, struct ccw_device_private, kick_work);
709 cdev = priv->cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 sch = to_subchannel(cdev->dev.parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711
Cornelia Huckef995162007-04-27 16:01:39 +0200712 ccw_device_unregister(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 PREPARE_WORK(&cdev->private->kick_work,
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100714 ccw_device_add_changed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 queue_work(ccw_device_work, &cdev->private->kick_work);
716}
717
718static void
719ccw_device_release(struct device *dev)
720{
721 struct ccw_device *cdev;
722
723 cdev = to_ccwdev(dev);
724 kfree(cdev->private);
725 kfree(cdev);
726}
727
Cornelia Huck7674da72006-12-08 15:54:21 +0100728static struct ccw_device * io_subchannel_allocate_dev(struct subchannel *sch)
729{
730 struct ccw_device *cdev;
731
732 cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
733 if (cdev) {
734 cdev->private = kzalloc(sizeof(struct ccw_device_private),
735 GFP_KERNEL | GFP_DMA);
736 if (cdev->private)
737 return cdev;
738 }
739 kfree(cdev);
740 return ERR_PTR(-ENOMEM);
741}
742
743static int io_subchannel_initialize_dev(struct subchannel *sch,
744 struct ccw_device *cdev)
745{
746 cdev->private->cdev = cdev;
747 atomic_set(&cdev->private->onoff, 0);
748 cdev->dev.parent = &sch->dev;
749 cdev->dev.release = ccw_device_release;
Heiko Carstens33583c32007-11-05 11:10:07 +0100750 INIT_WORK(&cdev->private->kick_work, NULL);
Cornelia Huckef995162007-04-27 16:01:39 +0200751 cdev->dev.groups = ccwdev_attr_groups;
Cornelia Huck7674da72006-12-08 15:54:21 +0100752 /* Do first half of device_register. */
753 device_initialize(&cdev->dev);
754 if (!get_device(&sch->dev)) {
755 if (cdev->dev.release)
756 cdev->dev.release(&cdev->dev);
757 return -ENODEV;
758 }
759 return 0;
760}
761
762static struct ccw_device * io_subchannel_create_ccwdev(struct subchannel *sch)
763{
764 struct ccw_device *cdev;
765 int ret;
766
767 cdev = io_subchannel_allocate_dev(sch);
768 if (!IS_ERR(cdev)) {
769 ret = io_subchannel_initialize_dev(sch, cdev);
770 if (ret) {
771 kfree(cdev);
772 cdev = ERR_PTR(ret);
773 }
774 }
775 return cdev;
776}
777
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100778static int io_subchannel_recog(struct ccw_device *, struct subchannel *);
779
780static void sch_attach_device(struct subchannel *sch,
781 struct ccw_device *cdev)
782{
Cornelia Huck82b7ac02007-04-27 16:01:36 +0200783 css_update_ssd_info(sch);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100784 spin_lock_irq(sch->lock);
Cornelia Huckdb6a6422008-01-26 14:10:46 +0100785 sch_set_cdev(sch, cdev);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100786 cdev->private->schid = sch->schid;
787 cdev->ccwlock = sch->lock;
Cornelia Huckc820de32008-07-14 09:58:45 +0200788 ccw_device_trigger_reprobe(cdev);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100789 spin_unlock_irq(sch->lock);
790}
791
792static void sch_attach_disconnected_device(struct subchannel *sch,
793 struct ccw_device *cdev)
794{
795 struct subchannel *other_sch;
796 int ret;
797
798 other_sch = to_subchannel(get_device(cdev->dev.parent));
799 ret = device_move(&cdev->dev, &sch->dev);
800 if (ret) {
Michael Ernst139b83d2008-05-07 09:22:54 +0200801 CIO_MSG_EVENT(0, "Moving disconnected device 0.%x.%04x failed "
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100802 "(ret=%d)!\n", cdev->private->dev_id.ssid,
803 cdev->private->dev_id.devno, ret);
804 put_device(&other_sch->dev);
805 return;
806 }
Cornelia Huckdb6a6422008-01-26 14:10:46 +0100807 sch_set_cdev(other_sch, NULL);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100808 /* No need to keep a subchannel without ccw device around. */
809 css_sch_device_unregister(other_sch);
810 put_device(&other_sch->dev);
811 sch_attach_device(sch, cdev);
812}
813
814static void sch_attach_orphaned_device(struct subchannel *sch,
815 struct ccw_device *cdev)
816{
817 int ret;
818
819 /* Try to move the ccw device to its new subchannel. */
820 ret = device_move(&cdev->dev, &sch->dev);
821 if (ret) {
822 CIO_MSG_EVENT(0, "Moving device 0.%x.%04x from orphanage "
823 "failed (ret=%d)!\n",
824 cdev->private->dev_id.ssid,
825 cdev->private->dev_id.devno, ret);
826 return;
827 }
828 sch_attach_device(sch, cdev);
829}
830
831static void sch_create_and_recog_new_device(struct subchannel *sch)
832{
833 struct ccw_device *cdev;
834
835 /* Need to allocate a new ccw device. */
836 cdev = io_subchannel_create_ccwdev(sch);
837 if (IS_ERR(cdev)) {
838 /* OK, we did everything we could... */
839 css_sch_device_unregister(sch);
840 return;
841 }
842 spin_lock_irq(sch->lock);
Cornelia Huckdb6a6422008-01-26 14:10:46 +0100843 sch_set_cdev(sch, cdev);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100844 spin_unlock_irq(sch->lock);
845 /* Start recognition for the new ccw device. */
846 if (io_subchannel_recog(cdev, sch)) {
847 spin_lock_irq(sch->lock);
Cornelia Huckdb6a6422008-01-26 14:10:46 +0100848 sch_set_cdev(sch, NULL);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100849 spin_unlock_irq(sch->lock);
850 if (cdev->dev.release)
851 cdev->dev.release(&cdev->dev);
852 css_sch_device_unregister(sch);
853 }
854}
855
856
857void ccw_device_move_to_orphanage(struct work_struct *work)
858{
859 struct ccw_device_private *priv;
860 struct ccw_device *cdev;
861 struct ccw_device *replacing_cdev;
862 struct subchannel *sch;
863 int ret;
864 struct channel_subsystem *css;
865 struct ccw_dev_id dev_id;
866
867 priv = container_of(work, struct ccw_device_private, kick_work);
868 cdev = priv->cdev;
869 sch = to_subchannel(cdev->dev.parent);
870 css = to_css(sch->dev.parent);
871 dev_id.devno = sch->schib.pmcw.dev;
872 dev_id.ssid = sch->schid.ssid;
873
874 /*
875 * Move the orphaned ccw device to the orphanage so the replacing
876 * ccw device can take its place on the subchannel.
877 */
878 ret = device_move(&cdev->dev, &css->pseudo_subchannel->dev);
879 if (ret) {
880 CIO_MSG_EVENT(0, "Moving device 0.%x.%04x to orphanage failed "
881 "(ret=%d)!\n", cdev->private->dev_id.ssid,
882 cdev->private->dev_id.devno, ret);
883 return;
884 }
885 cdev->ccwlock = css->pseudo_subchannel->lock;
886 /*
887 * Search for the replacing ccw device
888 * - among the disconnected devices
889 * - in the orphanage
890 */
891 replacing_cdev = get_disc_ccwdev_by_dev_id(&dev_id, cdev);
892 if (replacing_cdev) {
893 sch_attach_disconnected_device(sch, replacing_cdev);
894 return;
895 }
896 replacing_cdev = get_orphaned_ccwdev_by_dev_id(css, &dev_id);
897 if (replacing_cdev) {
898 sch_attach_orphaned_device(sch, replacing_cdev);
899 return;
900 }
901 sch_create_and_recog_new_device(sch);
902}
903
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904/*
905 * Register recognized device.
906 */
907static void
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100908io_subchannel_register(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909{
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100910 struct ccw_device_private *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 struct ccw_device *cdev;
912 struct subchannel *sch;
913 int ret;
914 unsigned long flags;
915
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100916 priv = container_of(work, struct ccw_device_private, kick_work);
917 cdev = priv->cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 sch = to_subchannel(cdev->dev.parent);
Cornelia Huck82b7ac02007-04-27 16:01:36 +0200919 css_update_ssd_info(sch);
Cornelia Huck47af5512006-12-04 15:41:07 +0100920 /*
921 * io_subchannel_register() will also be called after device
922 * recognition has been done for a boxed device (which will already
923 * be registered). We need to reprobe since we may now have sense id
924 * information.
925 */
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700926 if (klist_node_attached(&cdev->dev.knode_parent)) {
Cornelia Huck47af5512006-12-04 15:41:07 +0100927 if (!cdev->drv) {
928 ret = device_reprobe(&cdev->dev);
929 if (ret)
930 /* We can't do much here. */
Michael Ernst139b83d2008-05-07 09:22:54 +0200931 CIO_MSG_EVENT(0, "device_reprobe() returned"
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200932 " %d for 0.%x.%04x\n", ret,
933 cdev->private->dev_id.ssid,
934 cdev->private->dev_id.devno);
Cornelia Huck47af5512006-12-04 15:41:07 +0100935 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 goto out;
937 }
Cornelia Huckfa1a8c22007-04-26 00:12:03 -0700938 /*
939 * Now we know this subchannel will stay, we can throw
940 * our delayed uevent.
941 */
942 sch->dev.uevent_suppress = 0;
943 kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 /* make it known to the system */
945 ret = ccw_device_register(cdev);
946 if (ret) {
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200947 CIO_MSG_EVENT(0, "Could not register ccw dev 0.%x.%04x: %d\n",
948 cdev->private->dev_id.ssid,
949 cdev->private->dev_id.devno, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 put_device(&cdev->dev);
Cornelia Huck2ec22982006-12-08 15:54:26 +0100951 spin_lock_irqsave(sch->lock, flags);
Cornelia Huckdb6a6422008-01-26 14:10:46 +0100952 sch_set_cdev(sch, NULL);
Cornelia Huck2ec22982006-12-08 15:54:26 +0100953 spin_unlock_irqrestore(sch->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 kfree (cdev->private);
955 kfree (cdev);
956 put_device(&sch->dev);
957 if (atomic_dec_and_test(&ccw_device_init_count))
958 wake_up(&ccw_device_init_wq);
959 return;
960 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 put_device(&cdev->dev);
962out:
963 cdev->private->flags.recog_done = 1;
964 put_device(&sch->dev);
965 wake_up(&cdev->private->wait_q);
966 if (atomic_dec_and_test(&ccw_device_init_count))
967 wake_up(&ccw_device_init_wq);
968}
969
Cornelia Huck3f4cf6e2007-10-12 16:11:26 +0200970static void ccw_device_call_sch_unregister(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971{
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100972 struct ccw_device_private *priv;
973 struct ccw_device *cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 struct subchannel *sch;
975
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100976 priv = container_of(work, struct ccw_device_private, kick_work);
977 cdev = priv->cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 sch = to_subchannel(cdev->dev.parent);
Cornelia Huck6ab48792006-07-12 16:39:50 +0200979 css_sch_device_unregister(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 /* Reset intparm to zeroes. */
981 sch->schib.pmcw.intparm = 0;
982 cio_modify(sch);
983 put_device(&cdev->dev);
984 put_device(&sch->dev);
985}
986
987/*
988 * subchannel recognition done. Called from the state machine.
989 */
990void
991io_subchannel_recog_done(struct ccw_device *cdev)
992{
993 struct subchannel *sch;
994
995 if (css_init_done == 0) {
996 cdev->private->flags.recog_done = 1;
997 return;
998 }
999 switch (cdev->private->state) {
1000 case DEV_STATE_NOT_OPER:
1001 cdev->private->flags.recog_done = 1;
1002 /* Remove device found not operational. */
1003 if (!get_device(&cdev->dev))
1004 break;
1005 sch = to_subchannel(cdev->dev.parent);
1006 PREPARE_WORK(&cdev->private->kick_work,
Martin Schwidefskyc1637532006-12-08 15:53:57 +01001007 ccw_device_call_sch_unregister);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 queue_work(slow_path_wq, &cdev->private->kick_work);
1009 if (atomic_dec_and_test(&ccw_device_init_count))
1010 wake_up(&ccw_device_init_wq);
1011 break;
1012 case DEV_STATE_BOXED:
1013 /* Device did not respond in time. */
1014 case DEV_STATE_OFFLINE:
1015 /*
1016 * We can't register the device in interrupt context so
1017 * we schedule a work item.
1018 */
1019 if (!get_device(&cdev->dev))
1020 break;
1021 PREPARE_WORK(&cdev->private->kick_work,
Martin Schwidefskyc1637532006-12-08 15:53:57 +01001022 io_subchannel_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 queue_work(slow_path_wq, &cdev->private->kick_work);
1024 break;
1025 }
1026}
1027
1028static int
1029io_subchannel_recog(struct ccw_device *cdev, struct subchannel *sch)
1030{
1031 int rc;
1032 struct ccw_device_private *priv;
1033
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001034 sch_set_cdev(sch, cdev);
Cornelia Huck2ec22982006-12-08 15:54:26 +01001035 cdev->ccwlock = sch->lock;
Cornelia Huckfb6958a2006-01-06 00:19:25 -08001036
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 /* Init private data. */
1038 priv = cdev->private;
Cornelia Huck78964262006-10-11 15:31:38 +02001039 priv->dev_id.devno = sch->schib.pmcw.dev;
1040 priv->dev_id.ssid = sch->schid.ssid;
1041 priv->schid = sch->schid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 priv->state = DEV_STATE_NOT_OPER;
1043 INIT_LIST_HEAD(&priv->cmb_list);
1044 init_waitqueue_head(&priv->wait_q);
1045 init_timer(&priv->timer);
1046
1047 /* Set an initial name for the device. */
Cornelia Huckfb6958a2006-01-06 00:19:25 -08001048 snprintf (cdev->dev.bus_id, BUS_ID_SIZE, "0.%x.%04x",
1049 sch->schid.ssid, sch->schib.pmcw.dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050
1051 /* Increase counter of devices currently in recognition. */
1052 atomic_inc(&ccw_device_init_count);
1053
1054 /* Start async. device sensing. */
Cornelia Huck2ec22982006-12-08 15:54:26 +01001055 spin_lock_irq(sch->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 rc = ccw_device_recognition(cdev);
Cornelia Huck2ec22982006-12-08 15:54:26 +01001057 spin_unlock_irq(sch->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 if (rc) {
1059 if (atomic_dec_and_test(&ccw_device_init_count))
1060 wake_up(&ccw_device_init_wq);
1061 }
1062 return rc;
1063}
1064
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +01001065static void ccw_device_move_to_sch(struct work_struct *work)
1066{
1067 struct ccw_device_private *priv;
1068 int rc;
1069 struct subchannel *sch;
1070 struct ccw_device *cdev;
1071 struct subchannel *former_parent;
1072
1073 priv = container_of(work, struct ccw_device_private, kick_work);
1074 sch = priv->sch;
1075 cdev = priv->cdev;
1076 former_parent = ccw_device_is_orphan(cdev) ?
1077 NULL : to_subchannel(get_device(cdev->dev.parent));
1078 mutex_lock(&sch->reg_mutex);
1079 /* Try to move the ccw device to its new subchannel. */
1080 rc = device_move(&cdev->dev, &sch->dev);
1081 mutex_unlock(&sch->reg_mutex);
1082 if (rc) {
Michael Ernst139b83d2008-05-07 09:22:54 +02001083 CIO_MSG_EVENT(0, "Moving device 0.%x.%04x to subchannel "
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +01001084 "0.%x.%04x failed (ret=%d)!\n",
1085 cdev->private->dev_id.ssid,
1086 cdev->private->dev_id.devno, sch->schid.ssid,
1087 sch->schid.sch_no, rc);
1088 css_sch_device_unregister(sch);
1089 goto out;
1090 }
1091 if (former_parent) {
1092 spin_lock_irq(former_parent->lock);
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001093 sch_set_cdev(former_parent, NULL);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +01001094 spin_unlock_irq(former_parent->lock);
1095 css_sch_device_unregister(former_parent);
1096 /* Reset intparm to zeroes. */
1097 former_parent->schib.pmcw.intparm = 0;
1098 cio_modify(former_parent);
1099 }
1100 sch_attach_device(sch, cdev);
1101out:
1102 if (former_parent)
1103 put_device(&former_parent->dev);
1104 put_device(&cdev->dev);
1105}
1106
Cornelia Huck602b20f2008-01-26 14:10:39 +01001107static void io_subchannel_irq(struct subchannel *sch)
1108{
1109 struct ccw_device *cdev;
1110
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001111 cdev = sch_get_cdev(sch);
Cornelia Huck602b20f2008-01-26 14:10:39 +01001112
1113 CIO_TRACE_EVENT(3, "IRQ");
1114 CIO_TRACE_EVENT(3, sch->dev.bus_id);
1115 if (cdev)
1116 dev_fsm_event(cdev, DEV_EVENT_INTERRUPT);
1117}
1118
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001119static void io_subchannel_init_fields(struct subchannel *sch)
1120{
1121 if (cio_is_console(sch->schid))
1122 sch->opm = 0xff;
1123 else
1124 sch->opm = chp_get_sch_opm(sch);
1125 sch->lpm = sch->schib.pmcw.pam & sch->opm;
Cornelia Huck3a3fc292008-07-14 09:58:58 +02001126 sch->isc = cio_is_console(sch->schid) ? CONSOLE_ISC : IO_SCH_ISC;
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001127
1128 CIO_MSG_EVENT(6, "Detected device %04x on subchannel 0.%x.%04X"
1129 " - PIM = %02X, PAM = %02X, POM = %02X\n",
1130 sch->schib.pmcw.dev, sch->schid.ssid,
1131 sch->schid.sch_no, sch->schib.pmcw.pim,
1132 sch->schib.pmcw.pam, sch->schib.pmcw.pom);
1133 /* Initially set up some fields in the pmcw. */
1134 sch->schib.pmcw.ena = 0;
1135 sch->schib.pmcw.csense = 1; /* concurrent sense */
1136 if ((sch->lpm & (sch->lpm - 1)) != 0)
1137 sch->schib.pmcw.mp = 1; /* multipath mode */
1138 /* clean up possible residual cmf stuff */
1139 sch->schib.pmcw.mme = 0;
1140 sch->schib.pmcw.mbfc = 0;
1141 sch->schib.pmcw.mbi = 0;
1142 sch->schib.mba = 0;
1143}
1144
1145static int io_subchannel_probe(struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 struct ccw_device *cdev;
1148 int rc;
1149 unsigned long flags;
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +01001150 struct ccw_dev_id dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001152 cdev = sch_get_cdev(sch);
1153 if (cdev) {
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001154 rc = sysfs_create_group(&sch->dev.kobj,
1155 &io_subchannel_attr_group);
1156 if (rc)
1157 CIO_MSG_EVENT(0, "Failed to create io subchannel "
1158 "attributes for subchannel "
1159 "0.%x.%04x (rc=%d)\n",
1160 sch->schid.ssid, sch->schid.sch_no, rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 /*
1162 * This subchannel already has an associated ccw_device.
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001163 * Throw the delayed uevent for the subchannel, register
1164 * the ccw_device and exit. This happens for all early
1165 * devices, e.g. the console.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 */
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001167 sch->dev.uevent_suppress = 0;
1168 kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
Cornelia Hucke1031782007-10-12 16:11:23 +02001169 cdev->dev.groups = ccwdev_attr_groups;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 device_initialize(&cdev->dev);
1171 ccw_device_register(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 /*
1173 * Check if the device is already online. If it is
1174 * the reference count needs to be corrected
1175 * (see ccw_device_online and css_init_done for the
1176 * ugly details).
1177 */
1178 if (cdev->private->state != DEV_STATE_NOT_OPER &&
1179 cdev->private->state != DEV_STATE_OFFLINE &&
1180 cdev->private->state != DEV_STATE_BOXED)
1181 get_device(&cdev->dev);
1182 return 0;
1183 }
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001184 io_subchannel_init_fields(sch);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +01001185 /*
1186 * First check if a fitting device may be found amongst the
1187 * disconnected devices or in the orphanage.
1188 */
1189 dev_id.devno = sch->schib.pmcw.dev;
1190 dev_id.ssid = sch->schid.ssid;
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001191 rc = sysfs_create_group(&sch->dev.kobj,
1192 &io_subchannel_attr_group);
1193 if (rc)
1194 return rc;
Cornelia Huckcd6b4f22008-01-26 14:10:43 +01001195 /* Allocate I/O subchannel private data. */
1196 sch->private = kzalloc(sizeof(struct io_subchannel_private),
1197 GFP_KERNEL | GFP_DMA);
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001198 if (!sch->private) {
1199 rc = -ENOMEM;
1200 goto out_err;
1201 }
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +01001202 cdev = get_disc_ccwdev_by_dev_id(&dev_id, NULL);
1203 if (!cdev)
1204 cdev = get_orphaned_ccwdev_by_dev_id(to_css(sch->dev.parent),
1205 &dev_id);
1206 if (cdev) {
1207 /*
1208 * Schedule moving the device until when we have a registered
1209 * subchannel to move to and succeed the probe. We can
1210 * unregister later again, when the probe is through.
1211 */
1212 cdev->private->sch = sch;
1213 PREPARE_WORK(&cdev->private->kick_work,
1214 ccw_device_move_to_sch);
1215 queue_work(slow_path_wq, &cdev->private->kick_work);
1216 return 0;
1217 }
Cornelia Huck7674da72006-12-08 15:54:21 +01001218 cdev = io_subchannel_create_ccwdev(sch);
Cornelia Huckcd6b4f22008-01-26 14:10:43 +01001219 if (IS_ERR(cdev)) {
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001220 rc = PTR_ERR(cdev);
1221 goto out_err;
Cornelia Huckcd6b4f22008-01-26 14:10:43 +01001222 }
Cornelia Huck8bbace72006-01-11 10:56:22 +01001223 rc = io_subchannel_recog(cdev, sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 if (rc) {
Cornelia Huck2ec22982006-12-08 15:54:26 +01001225 spin_lock_irqsave(sch->lock, flags);
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001226 sch_set_cdev(sch, NULL);
Cornelia Huck2ec22982006-12-08 15:54:26 +01001227 spin_unlock_irqrestore(sch->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 if (cdev->dev.release)
1229 cdev->dev.release(&cdev->dev);
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001230 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 }
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001232 return 0;
1233out_err:
1234 kfree(sch->private);
1235 sysfs_remove_group(&sch->dev.kobj, &io_subchannel_attr_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 return rc;
1237}
1238
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239static int
Cornelia Huck8bbace72006-01-11 10:56:22 +01001240io_subchannel_remove (struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241{
1242 struct ccw_device *cdev;
1243 unsigned long flags;
1244
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001245 cdev = sch_get_cdev(sch);
1246 if (!cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 /* Set ccw device to not operational and drop reference. */
1249 spin_lock_irqsave(cdev->ccwlock, flags);
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001250 sch_set_cdev(sch, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 cdev->private->state = DEV_STATE_NOT_OPER;
1252 spin_unlock_irqrestore(cdev->ccwlock, flags);
Cornelia Huckef995162007-04-27 16:01:39 +02001253 ccw_device_unregister(cdev);
1254 put_device(&cdev->dev);
Cornelia Huckcd6b4f22008-01-26 14:10:43 +01001255 kfree(sch->private);
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001256 sysfs_remove_group(&sch->dev.kobj, &io_subchannel_attr_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 return 0;
1258}
1259
Cornelia Huck602b20f2008-01-26 14:10:39 +01001260static int io_subchannel_notify(struct subchannel *sch, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261{
1262 struct ccw_device *cdev;
1263
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001264 cdev = sch_get_cdev(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 if (!cdev)
1266 return 0;
Cornelia Huckc820de32008-07-14 09:58:45 +02001267 return ccw_device_notify(cdev, event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268}
1269
Cornelia Huck602b20f2008-01-26 14:10:39 +01001270static void io_subchannel_verify(struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271{
1272 struct ccw_device *cdev;
1273
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001274 cdev = sch_get_cdev(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 if (cdev)
1276 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1277}
1278
Cornelia Huckc820de32008-07-14 09:58:45 +02001279static int check_for_io_on_path(struct subchannel *sch, int mask)
1280{
1281 int cc;
1282
1283 cc = stsch(sch->schid, &sch->schib);
1284 if (cc)
1285 return 0;
Peter Oberparleiter23d805b2008-07-14 09:58:50 +02001286 if (scsw_actl(&sch->schib.scsw) && sch->schib.pmcw.lpum == mask)
Cornelia Huckc820de32008-07-14 09:58:45 +02001287 return 1;
1288 return 0;
1289}
1290
1291static void terminate_internal_io(struct subchannel *sch,
1292 struct ccw_device *cdev)
1293{
1294 if (cio_clear(sch)) {
1295 /* Recheck device in case clear failed. */
1296 sch->lpm = 0;
1297 if (cdev->online)
1298 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1299 else
1300 css_schedule_eval(sch->schid);
1301 return;
1302 }
1303 cdev->private->state = DEV_STATE_CLEAR_VERIFY;
1304 /* Request retry of internal operation. */
1305 cdev->private->flags.intretry = 1;
1306 /* Call handler. */
1307 if (cdev->handler)
1308 cdev->handler(cdev, cdev->private->intparm,
1309 ERR_PTR(-EIO));
1310}
1311
1312static void io_subchannel_terminate_path(struct subchannel *sch, u8 mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313{
1314 struct ccw_device *cdev;
1315
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001316 cdev = sch_get_cdev(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 if (!cdev)
1318 return;
Cornelia Huckc820de32008-07-14 09:58:45 +02001319 if (check_for_io_on_path(sch, mask)) {
1320 if (cdev->private->state == DEV_STATE_ONLINE)
1321 ccw_device_kill_io(cdev);
1322 else {
1323 terminate_internal_io(sch, cdev);
1324 /* Re-start path verification. */
1325 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1326 }
1327 } else
1328 /* trigger path verification. */
1329 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1330
1331}
1332
Cornelia Huck99611f82008-07-14 09:59:02 +02001333static int io_subchannel_chp_event(struct subchannel *sch,
1334 struct chp_link *link, int event)
Cornelia Huckc820de32008-07-14 09:58:45 +02001335{
1336 int mask;
Cornelia Huckc820de32008-07-14 09:58:45 +02001337
Cornelia Huck99611f82008-07-14 09:59:02 +02001338 mask = chp_ssd_get_mask(&sch->ssd_info, link);
Cornelia Huckc820de32008-07-14 09:58:45 +02001339 if (!mask)
1340 return 0;
1341 switch (event) {
1342 case CHP_VARY_OFF:
1343 sch->opm &= ~mask;
1344 sch->lpm &= ~mask;
1345 io_subchannel_terminate_path(sch, mask);
1346 break;
1347 case CHP_VARY_ON:
1348 sch->opm |= mask;
1349 sch->lpm |= mask;
1350 io_subchannel_verify(sch);
1351 break;
1352 case CHP_OFFLINE:
1353 if (stsch(sch->schid, &sch->schib))
1354 return -ENXIO;
1355 if (!css_sch_is_valid(&sch->schib))
1356 return -ENODEV;
1357 io_subchannel_terminate_path(sch, mask);
1358 break;
1359 case CHP_ONLINE:
1360 if (stsch(sch->schid, &sch->schib))
1361 return -ENXIO;
1362 sch->lpm |= mask & sch->opm;
1363 io_subchannel_verify(sch);
1364 break;
1365 }
1366 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367}
1368
1369static void
Cornelia Huck8bbace72006-01-11 10:56:22 +01001370io_subchannel_shutdown(struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 struct ccw_device *cdev;
1373 int ret;
1374
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001375 cdev = sch_get_cdev(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376
Cornelia Hucka8237fc2006-01-06 00:19:21 -08001377 if (cio_is_console(sch->schid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 return;
1379 if (!sch->schib.pmcw.ena)
1380 /* Nothing to do. */
1381 return;
1382 ret = cio_disable_subchannel(sch);
1383 if (ret != -EBUSY)
1384 /* Subchannel is disabled, we're done. */
1385 return;
1386 cdev->private->state = DEV_STATE_QUIESCE;
1387 if (cdev->handler)
1388 cdev->handler(cdev, cdev->private->intparm,
1389 ERR_PTR(-EIO));
1390 ret = ccw_device_cancel_halt_clear(cdev);
1391 if (ret == -EBUSY) {
1392 ccw_device_set_timeout(cdev, HZ/10);
1393 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
1394 }
1395 cio_disable_subchannel(sch);
1396}
1397
Cornelia Huckc820de32008-07-14 09:58:45 +02001398static int io_subchannel_get_status(struct subchannel *sch)
1399{
1400 struct schib schib;
1401
1402 if (stsch(sch->schid, &schib) || !schib.pmcw.dnv)
1403 return CIO_GONE;
1404 if (sch->schib.pmcw.dnv && (schib.pmcw.dev != sch->schib.pmcw.dev))
1405 return CIO_REVALIDATE;
1406 if (!sch->lpm)
1407 return CIO_NO_PATH;
1408 return CIO_OPER;
1409}
1410
1411static int device_is_disconnected(struct ccw_device *cdev)
1412{
1413 if (!cdev)
1414 return 0;
1415 return (cdev->private->state == DEV_STATE_DISCONNECTED ||
1416 cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID);
1417}
1418
1419static int recovery_check(struct device *dev, void *data)
1420{
1421 struct ccw_device *cdev = to_ccwdev(dev);
1422 int *redo = data;
1423
1424 spin_lock_irq(cdev->ccwlock);
1425 switch (cdev->private->state) {
1426 case DEV_STATE_DISCONNECTED:
1427 CIO_MSG_EVENT(3, "recovery: trigger 0.%x.%04x\n",
1428 cdev->private->dev_id.ssid,
1429 cdev->private->dev_id.devno);
1430 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1431 *redo = 1;
1432 break;
1433 case DEV_STATE_DISCONNECTED_SENSE_ID:
1434 *redo = 1;
1435 break;
1436 }
1437 spin_unlock_irq(cdev->ccwlock);
1438
1439 return 0;
1440}
1441
1442static void recovery_work_func(struct work_struct *unused)
1443{
1444 int redo = 0;
1445
1446 bus_for_each_dev(&ccw_bus_type, NULL, &redo, recovery_check);
1447 if (redo) {
1448 spin_lock_irq(&recovery_lock);
1449 if (!timer_pending(&recovery_timer)) {
1450 if (recovery_phase < ARRAY_SIZE(recovery_delay) - 1)
1451 recovery_phase++;
1452 mod_timer(&recovery_timer, jiffies +
1453 recovery_delay[recovery_phase] * HZ);
1454 }
1455 spin_unlock_irq(&recovery_lock);
1456 } else
1457 CIO_MSG_EVENT(4, "recovery: end\n");
1458}
1459
1460static DECLARE_WORK(recovery_work, recovery_work_func);
1461
1462static void recovery_func(unsigned long data)
1463{
1464 /*
1465 * We can't do our recovery in softirq context and it's not
1466 * performance critical, so we schedule it.
1467 */
1468 schedule_work(&recovery_work);
1469}
1470
1471static void ccw_device_schedule_recovery(void)
1472{
1473 unsigned long flags;
1474
1475 CIO_MSG_EVENT(4, "recovery: schedule\n");
1476 spin_lock_irqsave(&recovery_lock, flags);
1477 if (!timer_pending(&recovery_timer) || (recovery_phase != 0)) {
1478 recovery_phase = 0;
1479 mod_timer(&recovery_timer, jiffies + recovery_delay[0] * HZ);
1480 }
1481 spin_unlock_irqrestore(&recovery_lock, flags);
1482}
1483
1484static void device_set_disconnected(struct ccw_device *cdev)
1485{
1486 if (!cdev)
1487 return;
1488 ccw_device_set_timeout(cdev, 0);
1489 cdev->private->flags.fake_irb = 0;
1490 cdev->private->state = DEV_STATE_DISCONNECTED;
1491 if (cdev->online)
1492 ccw_device_schedule_recovery();
1493}
1494
1495static int io_subchannel_sch_event(struct subchannel *sch, int slow)
1496{
1497 int event, ret, disc;
1498 unsigned long flags;
1499 enum { NONE, UNREGISTER, UNREGISTER_PROBE, REPROBE } action;
1500 struct ccw_device *cdev;
1501
1502 spin_lock_irqsave(sch->lock, flags);
1503 cdev = sch_get_cdev(sch);
1504 disc = device_is_disconnected(cdev);
1505 if (disc && slow) {
1506 /* Disconnected devices are evaluated directly only.*/
1507 spin_unlock_irqrestore(sch->lock, flags);
1508 return 0;
1509 }
1510 /* No interrupt after machine check - kill pending timers. */
1511 if (cdev)
1512 ccw_device_set_timeout(cdev, 0);
1513 if (!disc && !slow) {
1514 /* Non-disconnected devices are evaluated on the slow path. */
1515 spin_unlock_irqrestore(sch->lock, flags);
1516 return -EAGAIN;
1517 }
1518 event = io_subchannel_get_status(sch);
1519 CIO_MSG_EVENT(4, "Evaluating schid 0.%x.%04x, event %d, %s, %s path.\n",
1520 sch->schid.ssid, sch->schid.sch_no, event,
1521 disc ? "disconnected" : "normal",
1522 slow ? "slow" : "fast");
1523 /* Analyze subchannel status. */
1524 action = NONE;
1525 switch (event) {
1526 case CIO_NO_PATH:
1527 if (disc) {
1528 /* Check if paths have become available. */
1529 action = REPROBE;
1530 break;
1531 }
1532 /* fall through */
1533 case CIO_GONE:
1534 /* Prevent unwanted effects when opening lock. */
1535 cio_disable_subchannel(sch);
1536 device_set_disconnected(cdev);
1537 /* Ask driver what to do with device. */
1538 action = UNREGISTER;
1539 spin_unlock_irqrestore(sch->lock, flags);
1540 ret = io_subchannel_notify(sch, event);
1541 spin_lock_irqsave(sch->lock, flags);
1542 if (ret)
1543 action = NONE;
1544 break;
1545 case CIO_REVALIDATE:
1546 /* Device will be removed, so no notify necessary. */
1547 if (disc)
1548 /* Reprobe because immediate unregister might block. */
1549 action = REPROBE;
1550 else
1551 action = UNREGISTER_PROBE;
1552 break;
1553 case CIO_OPER:
1554 if (disc)
1555 /* Get device operational again. */
1556 action = REPROBE;
1557 break;
1558 }
1559 /* Perform action. */
1560 ret = 0;
1561 switch (action) {
1562 case UNREGISTER:
1563 case UNREGISTER_PROBE:
1564 /* Unregister device (will use subchannel lock). */
1565 spin_unlock_irqrestore(sch->lock, flags);
1566 css_sch_device_unregister(sch);
1567 spin_lock_irqsave(sch->lock, flags);
1568
1569 /* Reset intparm to zeroes. */
1570 sch->schib.pmcw.intparm = 0;
1571 cio_modify(sch);
1572 break;
1573 case REPROBE:
1574 ccw_device_trigger_reprobe(cdev);
1575 break;
1576 default:
1577 break;
1578 }
1579 spin_unlock_irqrestore(sch->lock, flags);
1580 /* Probe if necessary. */
1581 if (action == UNREGISTER_PROBE)
1582 ret = css_probe_device(sch->schid);
1583
1584 return ret;
1585}
1586
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587#ifdef CONFIG_CCW_CONSOLE
1588static struct ccw_device console_cdev;
1589static struct ccw_device_private console_private;
1590static int console_cdev_in_use;
1591
Cornelia Huck2ec22982006-12-08 15:54:26 +01001592static DEFINE_SPINLOCK(ccw_console_lock);
1593
1594spinlock_t * cio_get_console_lock(void)
1595{
1596 return &ccw_console_lock;
1597}
1598
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001599static int ccw_device_console_enable(struct ccw_device *cdev,
1600 struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601{
1602 int rc;
1603
Cornelia Huckcd6b4f22008-01-26 14:10:43 +01001604 /* Attach subchannel private data. */
1605 sch->private = cio_get_console_priv();
1606 memset(sch->private, 0, sizeof(struct io_subchannel_private));
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001607 io_subchannel_init_fields(sch);
1608 sch->driver = &io_subchannel_driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 /* Initialize the ccw_device structure. */
Heiko Carstens292888c2006-08-30 14:33:35 +02001610 cdev->dev.parent= &sch->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 rc = io_subchannel_recog(cdev, sch);
1612 if (rc)
1613 return rc;
1614
1615 /* Now wait for the async. recognition to come to an end. */
1616 spin_lock_irq(cdev->ccwlock);
1617 while (!dev_fsm_final_state(cdev))
1618 wait_cons_dev();
1619 rc = -EIO;
1620 if (cdev->private->state != DEV_STATE_OFFLINE)
1621 goto out_unlock;
1622 ccw_device_online(cdev);
1623 while (!dev_fsm_final_state(cdev))
1624 wait_cons_dev();
1625 if (cdev->private->state != DEV_STATE_ONLINE)
1626 goto out_unlock;
1627 rc = 0;
1628out_unlock:
1629 spin_unlock_irq(cdev->ccwlock);
1630 return 0;
1631}
1632
1633struct ccw_device *
1634ccw_device_probe_console(void)
1635{
1636 struct subchannel *sch;
1637 int ret;
1638
1639 if (xchg(&console_cdev_in_use, 1) != 0)
Peter Oberparleiter600b5d12006-02-01 03:06:35 -08001640 return ERR_PTR(-EBUSY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 sch = cio_probe_console();
1642 if (IS_ERR(sch)) {
1643 console_cdev_in_use = 0;
1644 return (void *) sch;
1645 }
1646 memset(&console_cdev, 0, sizeof(struct ccw_device));
1647 memset(&console_private, 0, sizeof(struct ccw_device_private));
1648 console_cdev.private = &console_private;
Martin Schwidefskyc1637532006-12-08 15:53:57 +01001649 console_private.cdev = &console_cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 ret = ccw_device_console_enable(&console_cdev, sch);
1651 if (ret) {
1652 cio_release_console();
1653 console_cdev_in_use = 0;
1654 return ERR_PTR(ret);
1655 }
1656 console_cdev.online = 1;
1657 return &console_cdev;
1658}
1659#endif
1660
1661/*
1662 * get ccw_device matching the busid, but only if owned by cdrv
1663 */
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001664static int
1665__ccwdev_check_busid(struct device *dev, void *id)
1666{
1667 char *bus_id;
1668
Cornelia Huck12975ae2006-10-11 15:31:47 +02001669 bus_id = id;
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001670
1671 return (strncmp(bus_id, dev->bus_id, BUS_ID_SIZE) == 0);
1672}
1673
1674
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +02001675/**
1676 * get_ccwdev_by_busid() - obtain device from a bus id
1677 * @cdrv: driver the device is owned by
1678 * @bus_id: bus id of the device to be searched
1679 *
1680 * This function searches all devices owned by @cdrv for a device with a bus
1681 * id matching @bus_id.
1682 * Returns:
1683 * If a match is found, its reference count of the found device is increased
1684 * and it is returned; else %NULL is returned.
1685 */
1686struct ccw_device *get_ccwdev_by_busid(struct ccw_driver *cdrv,
1687 const char *bus_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688{
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001689 struct device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 struct device_driver *drv;
1691
1692 drv = get_driver(&cdrv->driver);
1693 if (!drv)
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001694 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001696 dev = driver_find_device(drv, NULL, (void *)bus_id,
1697 __ccwdev_check_busid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 put_driver(drv);
1699
Heiko Carstensd2c993d2006-07-12 16:41:55 +02001700 return dev ? to_ccwdev(dev) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701}
1702
1703/************************** device driver handling ************************/
1704
1705/* This is the implementation of the ccw_driver class. The probe, remove
1706 * and release methods are initially very similar to the device_driver
1707 * implementations, with the difference that they have ccw_device
1708 * arguments.
1709 *
1710 * A ccw driver also contains the information that is needed for
1711 * device matching.
1712 */
1713static int
1714ccw_device_probe (struct device *dev)
1715{
1716 struct ccw_device *cdev = to_ccwdev(dev);
1717 struct ccw_driver *cdrv = to_ccwdrv(dev->driver);
1718 int ret;
1719
1720 cdev->drv = cdrv; /* to let the driver call _set_online */
1721
1722 ret = cdrv->probe ? cdrv->probe(cdev) : -ENODEV;
1723
1724 if (ret) {
Heiko Carstensd2c993d2006-07-12 16:41:55 +02001725 cdev->drv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 return ret;
1727 }
1728
1729 return 0;
1730}
1731
1732static int
1733ccw_device_remove (struct device *dev)
1734{
1735 struct ccw_device *cdev = to_ccwdev(dev);
1736 struct ccw_driver *cdrv = cdev->drv;
1737 int ret;
1738
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 if (cdrv->remove)
1740 cdrv->remove(cdev);
1741 if (cdev->online) {
1742 cdev->online = 0;
1743 spin_lock_irq(cdev->ccwlock);
1744 ret = ccw_device_offline(cdev);
1745 spin_unlock_irq(cdev->ccwlock);
1746 if (ret == 0)
1747 wait_event(cdev->private->wait_q,
1748 dev_fsm_final_state(cdev));
1749 else
Michael Ernst139b83d2008-05-07 09:22:54 +02001750 CIO_MSG_EVENT(0, "ccw_device_offline returned %d, "
Cornelia Hucke556bbb2007-07-27 12:29:19 +02001751 "device 0.%x.%04x\n",
1752 ret, cdev->private->dev_id.ssid,
1753 cdev->private->dev_id.devno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 }
1755 ccw_device_set_timeout(cdev, 0);
Heiko Carstensd2c993d2006-07-12 16:41:55 +02001756 cdev->drv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 return 0;
1758}
1759
Cornelia Huck958974fb2007-10-12 16:11:21 +02001760static void ccw_device_shutdown(struct device *dev)
1761{
1762 struct ccw_device *cdev;
1763
1764 cdev = to_ccwdev(dev);
1765 if (cdev->drv && cdev->drv->shutdown)
1766 cdev->drv->shutdown(cdev);
Cornelia Huck1842f2b2007-10-12 16:11:22 +02001767 disable_cmf(cdev);
Cornelia Huck958974fb2007-10-12 16:11:21 +02001768}
1769
Cornelia Huck8bbace72006-01-11 10:56:22 +01001770struct bus_type ccw_bus_type = {
1771 .name = "ccw",
1772 .match = ccw_bus_match,
1773 .uevent = ccw_uevent,
1774 .probe = ccw_device_probe,
1775 .remove = ccw_device_remove,
Cornelia Huck958974fb2007-10-12 16:11:21 +02001776 .shutdown = ccw_device_shutdown,
Cornelia Huck8bbace72006-01-11 10:56:22 +01001777};
1778
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +02001779/**
1780 * ccw_driver_register() - register a ccw driver
1781 * @cdriver: driver to be registered
1782 *
1783 * This function is mainly a wrapper around driver_register().
1784 * Returns:
1785 * %0 on success and a negative error value on failure.
1786 */
1787int ccw_driver_register(struct ccw_driver *cdriver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788{
1789 struct device_driver *drv = &cdriver->driver;
1790
1791 drv->bus = &ccw_bus_type;
1792 drv->name = cdriver->name;
Cornelia Huck4beee642008-01-26 14:10:47 +01001793 drv->owner = cdriver->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794
1795 return driver_register(drv);
1796}
1797
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +02001798/**
1799 * ccw_driver_unregister() - deregister a ccw driver
1800 * @cdriver: driver to be deregistered
1801 *
1802 * This function is mainly a wrapper around driver_unregister().
1803 */
1804void ccw_driver_unregister(struct ccw_driver *cdriver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805{
1806 driver_unregister(&cdriver->driver);
1807}
1808
Cornelia Hucka8237fc2006-01-06 00:19:21 -08001809/* Helper func for qdio. */
1810struct subchannel_id
1811ccw_device_get_subchannel_id(struct ccw_device *cdev)
1812{
1813 struct subchannel *sch;
1814
1815 sch = to_subchannel(cdev->dev.parent);
1816 return sch->schid;
1817}
1818
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819MODULE_LICENSE("GPL");
1820EXPORT_SYMBOL(ccw_device_set_online);
1821EXPORT_SYMBOL(ccw_device_set_offline);
1822EXPORT_SYMBOL(ccw_driver_register);
1823EXPORT_SYMBOL(ccw_driver_unregister);
1824EXPORT_SYMBOL(get_ccwdev_by_busid);
1825EXPORT_SYMBOL(ccw_bus_type);
1826EXPORT_SYMBOL(ccw_device_work);
1827EXPORT_SYMBOL(ccw_device_notify_work);
Cornelia Hucka8237fc2006-01-06 00:19:21 -08001828EXPORT_SYMBOL_GPL(ccw_device_get_subchannel_id);