blob: c904cb84d75ea3885f09f00f4c8bb39a7fb88fc3 [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 Huckf08adc02008-07-14 09:59:03 +0200134static struct css_device_id io_subchannel_ids[] = {
135 { .match_flags = 0x1, .type = SUBCHANNEL_TYPE_IO, },
136 { /* end of list */ },
137};
138MODULE_DEVICE_TABLE(css, io_subchannel_ids);
139
Cornelia Huckf7e5d672007-05-10 15:45:43 +0200140static struct css_driver io_subchannel_driver = {
Cornelia Huck4beee642008-01-26 14:10:47 +0100141 .owner = THIS_MODULE,
Cornelia Huckf08adc02008-07-14 09:59:03 +0200142 .subchannel_type = io_subchannel_ids,
Cornelia Huck25b7bb52008-01-26 14:10:41 +0100143 .name = "io_subchannel",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 .irq = io_subchannel_irq,
Cornelia Huckc820de32008-07-14 09:58:45 +0200145 .sch_event = io_subchannel_sch_event,
146 .chp_event = io_subchannel_chp_event,
Cornelia Huck8bbace72006-01-11 10:56:22 +0100147 .probe = io_subchannel_probe,
148 .remove = io_subchannel_remove,
149 .shutdown = io_subchannel_shutdown,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150};
151
152struct workqueue_struct *ccw_device_work;
153struct workqueue_struct *ccw_device_notify_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 ? */
171 ccw_device_notify_work = create_singlethread_workqueue("cio_notify");
172 if (!ccw_device_notify_work) {
173 ret = -ENOMEM; /* FIXME: better errno ? */
174 goto out_err;
175 }
176 slow_path_wq = create_singlethread_workqueue("kslowcrw");
177 if (!slow_path_wq) {
178 ret = -ENOMEM; /* FIXME: better errno ? */
179 goto out_err;
180 }
181 if ((ret = bus_register (&ccw_bus_type)))
182 goto out_err;
183
Cornelia Huck25b7bb52008-01-26 14:10:41 +0100184 ret = css_driver_register(&io_subchannel_driver);
185 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 goto out_err;
187
188 wait_event(ccw_device_init_wq,
189 atomic_read(&ccw_device_init_count) == 0);
190 flush_workqueue(ccw_device_work);
191 return 0;
192out_err:
193 if (ccw_device_work)
194 destroy_workqueue(ccw_device_work);
195 if (ccw_device_notify_work)
196 destroy_workqueue(ccw_device_notify_work);
197 if (slow_path_wq)
198 destroy_workqueue(slow_path_wq);
199 return ret;
200}
201
202static void __exit
203cleanup_ccw_bus_type (void)
204{
Cornelia Huck25b7bb52008-01-26 14:10:41 +0100205 css_driver_unregister(&io_subchannel_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 bus_unregister(&ccw_bus_type);
207 destroy_workqueue(ccw_device_notify_work);
208 destroy_workqueue(ccw_device_work);
209}
210
211subsys_initcall(init_ccw_bus_type);
212module_exit(cleanup_ccw_bus_type);
213
214/************************ device handling **************************/
215
216/*
217 * A ccw_device has some interfaces in sysfs in addition to the
218 * standard ones.
219 * The following entries are designed to export the information which
220 * resided in 2.4 in /proc/subchannels. Subchannel and device number
221 * are obvious, so they don't have an entry :)
222 * TODO: Split chpids and pimpampom up? Where is "in use" in the tree?
223 */
224static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400225chpids_show (struct device * dev, struct device_attribute *attr, char * buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226{
227 struct subchannel *sch = to_subchannel(dev);
Peter Oberparleiter7ad6a242007-04-27 16:01:35 +0200228 struct chsc_ssd_info *ssd = &sch->ssd_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 ssize_t ret = 0;
230 int chp;
Peter Oberparleiter7ad6a242007-04-27 16:01:35 +0200231 int mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
Peter Oberparleiter7ad6a242007-04-27 16:01:35 +0200233 for (chp = 0; chp < 8; chp++) {
234 mask = 0x80 >> chp;
235 if (ssd->path_mask & mask)
236 ret += sprintf(buf + ret, "%02x ", ssd->chpid[chp].id);
237 else
238 ret += sprintf(buf + ret, "00 ");
239 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 ret += sprintf (buf+ret, "\n");
241 return min((ssize_t)PAGE_SIZE, ret);
242}
243
244static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400245pimpampom_show (struct device * dev, struct device_attribute *attr, char * buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246{
247 struct subchannel *sch = to_subchannel(dev);
248 struct pmcw *pmcw = &sch->schib.pmcw;
249
250 return sprintf (buf, "%02x %02x %02x\n",
251 pmcw->pim, pmcw->pam, pmcw->pom);
252}
253
254static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400255devtype_show (struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256{
257 struct ccw_device *cdev = to_ccwdev(dev);
258 struct ccw_device_id *id = &(cdev->id);
259
260 if (id->dev_type != 0)
261 return sprintf(buf, "%04x/%02x\n",
262 id->dev_type, id->dev_model);
263 else
264 return sprintf(buf, "n/a\n");
265}
266
267static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400268cutype_show (struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269{
270 struct ccw_device *cdev = to_ccwdev(dev);
271 struct ccw_device_id *id = &(cdev->id);
272
273 return sprintf(buf, "%04x/%02x\n",
274 id->cu_type, id->cu_model);
275}
276
277static ssize_t
Bastian Blankf1fc78a2005-10-30 15:00:12 -0800278modalias_show (struct device *dev, struct device_attribute *attr, char *buf)
279{
280 struct ccw_device *cdev = to_ccwdev(dev);
281 struct ccw_device_id *id = &(cdev->id);
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +0200282 int len;
Bastian Blankf1fc78a2005-10-30 15:00:12 -0800283
Cornelia Huck086a6c62007-07-17 13:36:08 +0200284 len = snprint_alias(buf, PAGE_SIZE, id, "\n");
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +0200285
286 return len > PAGE_SIZE ? PAGE_SIZE : len;
Bastian Blankf1fc78a2005-10-30 15:00:12 -0800287}
288
289static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400290online_show (struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291{
292 struct ccw_device *cdev = to_ccwdev(dev);
293
294 return sprintf(buf, cdev->online ? "1\n" : "0\n");
295}
296
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100297int ccw_device_is_orphan(struct ccw_device *cdev)
298{
299 return sch_is_pseudo_sch(to_subchannel(cdev->dev.parent));
300}
301
Cornelia Huckef995162007-04-27 16:01:39 +0200302static void ccw_device_unregister(struct ccw_device *cdev)
Cornelia Huck7674da72006-12-08 15:54:21 +0100303{
Cornelia Huck7674da72006-12-08 15:54:21 +0100304 if (test_and_clear_bit(1, &cdev->private->registered))
Cornelia Huckef995162007-04-27 16:01:39 +0200305 device_del(&cdev->dev);
Cornelia Huck7674da72006-12-08 15:54:21 +0100306}
307
Cornelia Huck59a8a6e2007-05-31 17:38:06 +0200308static void ccw_device_remove_orphan_cb(struct device *dev)
309{
310 struct ccw_device *cdev = to_ccwdev(dev);
311
312 ccw_device_unregister(cdev);
313 put_device(&cdev->dev);
314}
315
316static void ccw_device_remove_sch_cb(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317{
318 struct subchannel *sch;
Cornelia Huck59a8a6e2007-05-31 17:38:06 +0200319
320 sch = to_subchannel(dev);
Cornelia Huck6ab48792006-07-12 16:39:50 +0200321 css_sch_device_unregister(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 /* Reset intparm to zeroes. */
323 sch->schib.pmcw.intparm = 0;
324 cio_modify(sch);
325 put_device(&sch->dev);
326}
327
Cornelia Huck59a8a6e2007-05-31 17:38:06 +0200328static void
329ccw_device_remove_disconnected(struct ccw_device *cdev)
330{
331 unsigned long flags;
332 int rc;
333
334 /*
335 * Forced offline in disconnected state means
336 * 'throw away device'.
337 */
338 if (ccw_device_is_orphan(cdev)) {
339 /*
340 * Deregister ccw device.
341 * Unfortunately, we cannot do this directly from the
342 * attribute method.
343 */
344 spin_lock_irqsave(cdev->ccwlock, flags);
345 cdev->private->state = DEV_STATE_NOT_OPER;
346 spin_unlock_irqrestore(cdev->ccwlock, flags);
347 rc = device_schedule_callback(&cdev->dev,
348 ccw_device_remove_orphan_cb);
349 if (rc)
Michael Ernst139b83d2008-05-07 09:22:54 +0200350 CIO_MSG_EVENT(0, "Couldn't unregister orphan "
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200351 "0.%x.%04x\n",
352 cdev->private->dev_id.ssid,
353 cdev->private->dev_id.devno);
Cornelia Huck59a8a6e2007-05-31 17:38:06 +0200354 return;
355 }
356 /* Deregister subchannel, which will kill the ccw device. */
357 rc = device_schedule_callback(cdev->dev.parent,
358 ccw_device_remove_sch_cb);
359 if (rc)
Michael Ernst139b83d2008-05-07 09:22:54 +0200360 CIO_MSG_EVENT(0, "Couldn't unregister disconnected device "
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200361 "0.%x.%04x\n",
362 cdev->private->dev_id.ssid,
363 cdev->private->dev_id.devno);
Cornelia Huck59a8a6e2007-05-31 17:38:06 +0200364}
365
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +0200366/**
367 * ccw_device_set_offline() - disable a ccw device for I/O
368 * @cdev: target ccw device
369 *
370 * This function calls the driver's set_offline() function for @cdev, if
371 * given, and then disables @cdev.
372 * Returns:
373 * %0 on success and a negative error value on failure.
374 * Context:
375 * enabled, ccw device lock not held
376 */
377int ccw_device_set_offline(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378{
379 int ret;
380
381 if (!cdev)
382 return -ENODEV;
383 if (!cdev->online || !cdev->drv)
384 return -EINVAL;
385
386 if (cdev->drv->set_offline) {
387 ret = cdev->drv->set_offline(cdev);
388 if (ret != 0)
389 return ret;
390 }
391 cdev->online = 0;
392 spin_lock_irq(cdev->ccwlock);
393 ret = ccw_device_offline(cdev);
394 if (ret == -ENODEV) {
395 if (cdev->private->state != DEV_STATE_NOT_OPER) {
396 cdev->private->state = DEV_STATE_OFFLINE;
397 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
398 }
399 spin_unlock_irq(cdev->ccwlock);
400 return ret;
401 }
402 spin_unlock_irq(cdev->ccwlock);
403 if (ret == 0)
404 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
405 else {
Michael Ernst139b83d2008-05-07 09:22:54 +0200406 CIO_MSG_EVENT(0, "ccw_device_offline returned %d, "
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200407 "device 0.%x.%04x\n",
408 ret, cdev->private->dev_id.ssid,
409 cdev->private->dev_id.devno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 cdev->online = 1;
411 }
412 return ret;
413}
414
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +0200415/**
416 * ccw_device_set_online() - enable a ccw device for I/O
417 * @cdev: target ccw device
418 *
419 * This function first enables @cdev and then calls the driver's set_online()
420 * function for @cdev, if given. If set_online() returns an error, @cdev is
421 * disabled again.
422 * Returns:
423 * %0 on success and a negative error value on failure.
424 * Context:
425 * enabled, ccw device lock not held
426 */
427int ccw_device_set_online(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428{
429 int ret;
430
431 if (!cdev)
432 return -ENODEV;
433 if (cdev->online || !cdev->drv)
434 return -EINVAL;
435
436 spin_lock_irq(cdev->ccwlock);
437 ret = ccw_device_online(cdev);
438 spin_unlock_irq(cdev->ccwlock);
439 if (ret == 0)
440 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
441 else {
Michael Ernst139b83d2008-05-07 09:22:54 +0200442 CIO_MSG_EVENT(0, "ccw_device_online returned %d, "
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200443 "device 0.%x.%04x\n",
444 ret, cdev->private->dev_id.ssid,
445 cdev->private->dev_id.devno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 return ret;
447 }
448 if (cdev->private->state != DEV_STATE_ONLINE)
449 return -ENODEV;
450 if (!cdev->drv->set_online || cdev->drv->set_online(cdev) == 0) {
451 cdev->online = 1;
452 return 0;
453 }
454 spin_lock_irq(cdev->ccwlock);
455 ret = ccw_device_offline(cdev);
456 spin_unlock_irq(cdev->ccwlock);
457 if (ret == 0)
458 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200459 else
Michael Ernst139b83d2008-05-07 09:22:54 +0200460 CIO_MSG_EVENT(0, "ccw_device_offline returned %d, "
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200461 "device 0.%x.%04x\n",
462 ret, cdev->private->dev_id.ssid,
463 cdev->private->dev_id.devno);
Cornelia Huck6cadb782006-02-17 13:52:49 -0800464 return (ret == 0) ? -ENODEV : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465}
466
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200467static void online_store_handle_offline(struct ccw_device *cdev)
468{
469 if (cdev->private->state == DEV_STATE_DISCONNECTED)
470 ccw_device_remove_disconnected(cdev);
471 else if (cdev->drv && cdev->drv->set_offline)
472 ccw_device_set_offline(cdev);
473}
474
475static int online_store_recog_and_online(struct ccw_device *cdev)
476{
477 int ret;
478
479 /* Do device recognition, if needed. */
480 if (cdev->id.cu_type == 0) {
481 ret = ccw_device_recognition(cdev);
482 if (ret) {
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200483 CIO_MSG_EVENT(0, "Couldn't start recognition "
484 "for device 0.%x.%04x (ret=%d)\n",
485 cdev->private->dev_id.ssid,
486 cdev->private->dev_id.devno, ret);
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200487 return ret;
488 }
489 wait_event(cdev->private->wait_q,
490 cdev->private->flags.recog_done);
491 }
492 if (cdev->drv && cdev->drv->set_online)
493 ccw_device_set_online(cdev);
494 return 0;
495}
496static void online_store_handle_online(struct ccw_device *cdev, int force)
497{
498 int ret;
499
500 ret = online_store_recog_and_online(cdev);
501 if (ret)
502 return;
503 if (force && cdev->private->state == DEV_STATE_BOXED) {
504 ret = ccw_device_stlck(cdev);
505 if (ret) {
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200506 dev_warn(&cdev->dev,
507 "ccw_device_stlck returned %d!\n", ret);
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200508 return;
509 }
510 if (cdev->id.cu_type == 0)
511 cdev->private->state = DEV_STATE_NOT_OPER;
512 online_store_recog_and_online(cdev);
513 }
514
515}
516
517static ssize_t online_store (struct device *dev, struct device_attribute *attr,
518 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519{
520 struct ccw_device *cdev = to_ccwdev(dev);
Cornelia Huck2f972202008-04-30 13:38:33 +0200521 int force, ret;
522 unsigned long i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
Martin Schwidefsky973bd992006-01-06 00:19:07 -0800524 if (atomic_cmpxchg(&cdev->private->onoff, 0, 1) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 return -EAGAIN;
526
527 if (cdev->drv && !try_module_get(cdev->drv->owner)) {
528 atomic_set(&cdev->private->onoff, 0);
529 return -EINVAL;
530 }
531 if (!strncmp(buf, "force\n", count)) {
532 force = 1;
533 i = 1;
Cornelia Huck2f972202008-04-30 13:38:33 +0200534 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 } else {
536 force = 0;
Cornelia Huck2f972202008-04-30 13:38:33 +0200537 ret = strict_strtoul(buf, 16, &i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 }
Cornelia Huck2f972202008-04-30 13:38:33 +0200539 if (ret)
540 goto out;
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200541 switch (i) {
542 case 0:
543 online_store_handle_offline(cdev);
Cornelia Huck2f972202008-04-30 13:38:33 +0200544 ret = count;
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200545 break;
546 case 1:
547 online_store_handle_online(cdev, force);
Cornelia Huck2f972202008-04-30 13:38:33 +0200548 ret = count;
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200549 break;
550 default:
Cornelia Huck2f972202008-04-30 13:38:33 +0200551 ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 }
Cornelia Huck2f972202008-04-30 13:38:33 +0200553out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 if (cdev->drv)
555 module_put(cdev->drv->owner);
556 atomic_set(&cdev->private->onoff, 0);
Cornelia Huck2f972202008-04-30 13:38:33 +0200557 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558}
559
560static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400561available_show (struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562{
563 struct ccw_device *cdev = to_ccwdev(dev);
564 struct subchannel *sch;
565
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100566 if (ccw_device_is_orphan(cdev))
567 return sprintf(buf, "no device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 switch (cdev->private->state) {
569 case DEV_STATE_BOXED:
570 return sprintf(buf, "boxed\n");
571 case DEV_STATE_DISCONNECTED:
572 case DEV_STATE_DISCONNECTED_SENSE_ID:
573 case DEV_STATE_NOT_OPER:
574 sch = to_subchannel(dev->parent);
575 if (!sch->lpm)
576 return sprintf(buf, "no path\n");
577 else
578 return sprintf(buf, "no device\n");
579 default:
580 /* All other states considered fine. */
581 return sprintf(buf, "good\n");
582 }
583}
584
585static DEVICE_ATTR(chpids, 0444, chpids_show, NULL);
586static DEVICE_ATTR(pimpampom, 0444, pimpampom_show, NULL);
587static DEVICE_ATTR(devtype, 0444, devtype_show, NULL);
588static DEVICE_ATTR(cutype, 0444, cutype_show, NULL);
Bastian Blankf1fc78a2005-10-30 15:00:12 -0800589static DEVICE_ATTR(modalias, 0444, modalias_show, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590static DEVICE_ATTR(online, 0644, online_show, online_store);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591static DEVICE_ATTR(availability, 0444, available_show, NULL);
592
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200593static struct attribute *io_subchannel_attrs[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 &dev_attr_chpids.attr,
595 &dev_attr_pimpampom.attr,
596 NULL,
597};
598
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200599static struct attribute_group io_subchannel_attr_group = {
600 .attrs = io_subchannel_attrs,
Cornelia Huck529192f2006-12-08 15:55:57 +0100601};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
603static struct attribute * ccwdev_attrs[] = {
604 &dev_attr_devtype.attr,
605 &dev_attr_cutype.attr,
Bastian Blankf1fc78a2005-10-30 15:00:12 -0800606 &dev_attr_modalias.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 &dev_attr_online.attr,
608 &dev_attr_cmb_enable.attr,
609 &dev_attr_availability.attr,
610 NULL,
611};
612
613static struct attribute_group ccwdev_attr_group = {
614 .attrs = ccwdev_attrs,
615};
616
Cornelia Huckf7e5d672007-05-10 15:45:43 +0200617static struct attribute_group *ccwdev_attr_groups[] = {
Cornelia Huckef995162007-04-27 16:01:39 +0200618 &ccwdev_attr_group,
619 NULL,
620};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
622/* this is a simple abstraction for device_register that sets the
623 * correct bus type and adds the bus specific files */
Cornelia Huck3c9da7b2006-10-27 12:39:33 +0200624static int ccw_device_register(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625{
626 struct device *dev = &cdev->dev;
627 int ret;
628
629 dev->bus = &ccw_bus_type;
630
631 if ((ret = device_add(dev)))
632 return ret;
633
634 set_bit(1, &cdev->private->registered);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 return ret;
636}
637
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700638struct match_data {
Cornelia Huck78964262006-10-11 15:31:38 +0200639 struct ccw_dev_id dev_id;
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700640 struct ccw_device * sibling;
641};
642
643static int
644match_devno(struct device * dev, void * data)
645{
Cornelia Huck78964262006-10-11 15:31:38 +0200646 struct match_data * d = data;
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700647 struct ccw_device * cdev;
648
649 cdev = to_ccwdev(dev);
650 if ((cdev->private->state == DEV_STATE_DISCONNECTED) &&
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100651 !ccw_device_is_orphan(cdev) &&
Cornelia Huck78964262006-10-11 15:31:38 +0200652 ccw_dev_id_is_equal(&cdev->private->dev_id, &d->dev_id) &&
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100653 (cdev != d->sibling))
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700654 return 1;
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700655 return 0;
656}
657
Cornelia Huck78964262006-10-11 15:31:38 +0200658static struct ccw_device * get_disc_ccwdev_by_dev_id(struct ccw_dev_id *dev_id,
659 struct ccw_device *sibling)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 struct device *dev;
Heiko Carstens292888c2006-08-30 14:33:35 +0200662 struct match_data data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663
Cornelia Huck78964262006-10-11 15:31:38 +0200664 data.dev_id = *dev_id;
Heiko Carstens292888c2006-08-30 14:33:35 +0200665 data.sibling = sibling;
Cornelia Hucke5945b42005-10-11 08:28:59 -0700666 dev = bus_find_device(&ccw_bus_type, NULL, &data, match_devno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700668 return dev ? to_ccwdev(dev) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669}
670
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100671static int match_orphan(struct device *dev, void *data)
672{
673 struct ccw_dev_id *dev_id;
674 struct ccw_device *cdev;
675
676 dev_id = data;
677 cdev = to_ccwdev(dev);
678 return ccw_dev_id_is_equal(&cdev->private->dev_id, dev_id);
679}
680
681static struct ccw_device *
682get_orphaned_ccwdev_by_dev_id(struct channel_subsystem *css,
683 struct ccw_dev_id *dev_id)
684{
685 struct device *dev;
686
687 dev = device_find_child(&css->pseudo_subchannel->dev, dev_id,
688 match_orphan);
689
690 return dev ? to_ccwdev(dev) : NULL;
691}
692
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693static void
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100694ccw_device_add_changed(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695{
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100696 struct ccw_device_private *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 struct ccw_device *cdev;
698
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100699 priv = container_of(work, struct ccw_device_private, kick_work);
700 cdev = priv->cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 if (device_add(&cdev->dev)) {
702 put_device(&cdev->dev);
703 return;
704 }
705 set_bit(1, &cdev->private->registered);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706}
707
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100708void ccw_device_do_unreg_rereg(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709{
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100710 struct ccw_device_private *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 struct ccw_device *cdev;
712 struct subchannel *sch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100714 priv = container_of(work, struct ccw_device_private, kick_work);
715 cdev = priv->cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 sch = to_subchannel(cdev->dev.parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
Cornelia Huckef995162007-04-27 16:01:39 +0200718 ccw_device_unregister(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 PREPARE_WORK(&cdev->private->kick_work,
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100720 ccw_device_add_changed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 queue_work(ccw_device_work, &cdev->private->kick_work);
722}
723
724static void
725ccw_device_release(struct device *dev)
726{
727 struct ccw_device *cdev;
728
729 cdev = to_ccwdev(dev);
730 kfree(cdev->private);
731 kfree(cdev);
732}
733
Cornelia Huck7674da72006-12-08 15:54:21 +0100734static struct ccw_device * io_subchannel_allocate_dev(struct subchannel *sch)
735{
736 struct ccw_device *cdev;
737
738 cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
739 if (cdev) {
740 cdev->private = kzalloc(sizeof(struct ccw_device_private),
741 GFP_KERNEL | GFP_DMA);
742 if (cdev->private)
743 return cdev;
744 }
745 kfree(cdev);
746 return ERR_PTR(-ENOMEM);
747}
748
749static int io_subchannel_initialize_dev(struct subchannel *sch,
750 struct ccw_device *cdev)
751{
752 cdev->private->cdev = cdev;
753 atomic_set(&cdev->private->onoff, 0);
754 cdev->dev.parent = &sch->dev;
755 cdev->dev.release = ccw_device_release;
Heiko Carstens33583c32007-11-05 11:10:07 +0100756 INIT_WORK(&cdev->private->kick_work, NULL);
Cornelia Huckef995162007-04-27 16:01:39 +0200757 cdev->dev.groups = ccwdev_attr_groups;
Cornelia Huck7674da72006-12-08 15:54:21 +0100758 /* Do first half of device_register. */
759 device_initialize(&cdev->dev);
760 if (!get_device(&sch->dev)) {
761 if (cdev->dev.release)
762 cdev->dev.release(&cdev->dev);
763 return -ENODEV;
764 }
765 return 0;
766}
767
768static struct ccw_device * io_subchannel_create_ccwdev(struct subchannel *sch)
769{
770 struct ccw_device *cdev;
771 int ret;
772
773 cdev = io_subchannel_allocate_dev(sch);
774 if (!IS_ERR(cdev)) {
775 ret = io_subchannel_initialize_dev(sch, cdev);
776 if (ret) {
777 kfree(cdev);
778 cdev = ERR_PTR(ret);
779 }
780 }
781 return cdev;
782}
783
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100784static int io_subchannel_recog(struct ccw_device *, struct subchannel *);
785
786static void sch_attach_device(struct subchannel *sch,
787 struct ccw_device *cdev)
788{
Cornelia Huck82b7ac02007-04-27 16:01:36 +0200789 css_update_ssd_info(sch);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100790 spin_lock_irq(sch->lock);
Cornelia Huckdb6a6422008-01-26 14:10:46 +0100791 sch_set_cdev(sch, cdev);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100792 cdev->private->schid = sch->schid;
793 cdev->ccwlock = sch->lock;
Cornelia Huckc820de32008-07-14 09:58:45 +0200794 ccw_device_trigger_reprobe(cdev);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100795 spin_unlock_irq(sch->lock);
796}
797
798static void sch_attach_disconnected_device(struct subchannel *sch,
799 struct ccw_device *cdev)
800{
801 struct subchannel *other_sch;
802 int ret;
803
804 other_sch = to_subchannel(get_device(cdev->dev.parent));
805 ret = device_move(&cdev->dev, &sch->dev);
806 if (ret) {
Michael Ernst139b83d2008-05-07 09:22:54 +0200807 CIO_MSG_EVENT(0, "Moving disconnected device 0.%x.%04x failed "
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100808 "(ret=%d)!\n", cdev->private->dev_id.ssid,
809 cdev->private->dev_id.devno, ret);
810 put_device(&other_sch->dev);
811 return;
812 }
Cornelia Huckdb6a6422008-01-26 14:10:46 +0100813 sch_set_cdev(other_sch, NULL);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100814 /* No need to keep a subchannel without ccw device around. */
815 css_sch_device_unregister(other_sch);
816 put_device(&other_sch->dev);
817 sch_attach_device(sch, cdev);
818}
819
820static void sch_attach_orphaned_device(struct subchannel *sch,
821 struct ccw_device *cdev)
822{
823 int ret;
824
825 /* Try to move the ccw device to its new subchannel. */
826 ret = device_move(&cdev->dev, &sch->dev);
827 if (ret) {
828 CIO_MSG_EVENT(0, "Moving device 0.%x.%04x from orphanage "
829 "failed (ret=%d)!\n",
830 cdev->private->dev_id.ssid,
831 cdev->private->dev_id.devno, ret);
832 return;
833 }
834 sch_attach_device(sch, cdev);
835}
836
837static void sch_create_and_recog_new_device(struct subchannel *sch)
838{
839 struct ccw_device *cdev;
840
841 /* Need to allocate a new ccw device. */
842 cdev = io_subchannel_create_ccwdev(sch);
843 if (IS_ERR(cdev)) {
844 /* OK, we did everything we could... */
845 css_sch_device_unregister(sch);
846 return;
847 }
848 spin_lock_irq(sch->lock);
Cornelia Huckdb6a6422008-01-26 14:10:46 +0100849 sch_set_cdev(sch, cdev);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100850 spin_unlock_irq(sch->lock);
851 /* Start recognition for the new ccw device. */
852 if (io_subchannel_recog(cdev, sch)) {
853 spin_lock_irq(sch->lock);
Cornelia Huckdb6a6422008-01-26 14:10:46 +0100854 sch_set_cdev(sch, NULL);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100855 spin_unlock_irq(sch->lock);
856 if (cdev->dev.release)
857 cdev->dev.release(&cdev->dev);
858 css_sch_device_unregister(sch);
859 }
860}
861
862
863void ccw_device_move_to_orphanage(struct work_struct *work)
864{
865 struct ccw_device_private *priv;
866 struct ccw_device *cdev;
867 struct ccw_device *replacing_cdev;
868 struct subchannel *sch;
869 int ret;
870 struct channel_subsystem *css;
871 struct ccw_dev_id dev_id;
872
873 priv = container_of(work, struct ccw_device_private, kick_work);
874 cdev = priv->cdev;
875 sch = to_subchannel(cdev->dev.parent);
876 css = to_css(sch->dev.parent);
877 dev_id.devno = sch->schib.pmcw.dev;
878 dev_id.ssid = sch->schid.ssid;
879
880 /*
881 * Move the orphaned ccw device to the orphanage so the replacing
882 * ccw device can take its place on the subchannel.
883 */
884 ret = device_move(&cdev->dev, &css->pseudo_subchannel->dev);
885 if (ret) {
886 CIO_MSG_EVENT(0, "Moving device 0.%x.%04x to orphanage failed "
887 "(ret=%d)!\n", cdev->private->dev_id.ssid,
888 cdev->private->dev_id.devno, ret);
889 return;
890 }
891 cdev->ccwlock = css->pseudo_subchannel->lock;
892 /*
893 * Search for the replacing ccw device
894 * - among the disconnected devices
895 * - in the orphanage
896 */
897 replacing_cdev = get_disc_ccwdev_by_dev_id(&dev_id, cdev);
898 if (replacing_cdev) {
899 sch_attach_disconnected_device(sch, replacing_cdev);
900 return;
901 }
902 replacing_cdev = get_orphaned_ccwdev_by_dev_id(css, &dev_id);
903 if (replacing_cdev) {
904 sch_attach_orphaned_device(sch, replacing_cdev);
905 return;
906 }
907 sch_create_and_recog_new_device(sch);
908}
909
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910/*
911 * Register recognized device.
912 */
913static void
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100914io_subchannel_register(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915{
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100916 struct ccw_device_private *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 struct ccw_device *cdev;
918 struct subchannel *sch;
919 int ret;
920 unsigned long flags;
921
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100922 priv = container_of(work, struct ccw_device_private, kick_work);
923 cdev = priv->cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 sch = to_subchannel(cdev->dev.parent);
Cornelia Huck82b7ac02007-04-27 16:01:36 +0200925 css_update_ssd_info(sch);
Cornelia Huck47af5512006-12-04 15:41:07 +0100926 /*
927 * io_subchannel_register() will also be called after device
928 * recognition has been done for a boxed device (which will already
929 * be registered). We need to reprobe since we may now have sense id
930 * information.
931 */
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700932 if (klist_node_attached(&cdev->dev.knode_parent)) {
Cornelia Huck47af5512006-12-04 15:41:07 +0100933 if (!cdev->drv) {
934 ret = device_reprobe(&cdev->dev);
935 if (ret)
936 /* We can't do much here. */
Michael Ernst139b83d2008-05-07 09:22:54 +0200937 CIO_MSG_EVENT(0, "device_reprobe() returned"
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200938 " %d for 0.%x.%04x\n", ret,
939 cdev->private->dev_id.ssid,
940 cdev->private->dev_id.devno);
Cornelia Huck47af5512006-12-04 15:41:07 +0100941 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 goto out;
943 }
Cornelia Huckfa1a8c22007-04-26 00:12:03 -0700944 /*
945 * Now we know this subchannel will stay, we can throw
946 * our delayed uevent.
947 */
948 sch->dev.uevent_suppress = 0;
949 kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 /* make it known to the system */
951 ret = ccw_device_register(cdev);
952 if (ret) {
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200953 CIO_MSG_EVENT(0, "Could not register ccw dev 0.%x.%04x: %d\n",
954 cdev->private->dev_id.ssid,
955 cdev->private->dev_id.devno, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 put_device(&cdev->dev);
Cornelia Huck2ec22982006-12-08 15:54:26 +0100957 spin_lock_irqsave(sch->lock, flags);
Cornelia Huckdb6a6422008-01-26 14:10:46 +0100958 sch_set_cdev(sch, NULL);
Cornelia Huck2ec22982006-12-08 15:54:26 +0100959 spin_unlock_irqrestore(sch->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 kfree (cdev->private);
961 kfree (cdev);
962 put_device(&sch->dev);
963 if (atomic_dec_and_test(&ccw_device_init_count))
964 wake_up(&ccw_device_init_wq);
965 return;
966 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 put_device(&cdev->dev);
968out:
969 cdev->private->flags.recog_done = 1;
970 put_device(&sch->dev);
971 wake_up(&cdev->private->wait_q);
972 if (atomic_dec_and_test(&ccw_device_init_count))
973 wake_up(&ccw_device_init_wq);
974}
975
Cornelia Huck3f4cf6e2007-10-12 16:11:26 +0200976static void ccw_device_call_sch_unregister(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977{
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100978 struct ccw_device_private *priv;
979 struct ccw_device *cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 struct subchannel *sch;
981
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100982 priv = container_of(work, struct ccw_device_private, kick_work);
983 cdev = priv->cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 sch = to_subchannel(cdev->dev.parent);
Cornelia Huck6ab48792006-07-12 16:39:50 +0200985 css_sch_device_unregister(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 /* Reset intparm to zeroes. */
987 sch->schib.pmcw.intparm = 0;
988 cio_modify(sch);
989 put_device(&cdev->dev);
990 put_device(&sch->dev);
991}
992
993/*
994 * subchannel recognition done. Called from the state machine.
995 */
996void
997io_subchannel_recog_done(struct ccw_device *cdev)
998{
999 struct subchannel *sch;
1000
1001 if (css_init_done == 0) {
1002 cdev->private->flags.recog_done = 1;
1003 return;
1004 }
1005 switch (cdev->private->state) {
1006 case DEV_STATE_NOT_OPER:
1007 cdev->private->flags.recog_done = 1;
1008 /* Remove device found not operational. */
1009 if (!get_device(&cdev->dev))
1010 break;
1011 sch = to_subchannel(cdev->dev.parent);
1012 PREPARE_WORK(&cdev->private->kick_work,
Martin Schwidefskyc1637532006-12-08 15:53:57 +01001013 ccw_device_call_sch_unregister);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 queue_work(slow_path_wq, &cdev->private->kick_work);
1015 if (atomic_dec_and_test(&ccw_device_init_count))
1016 wake_up(&ccw_device_init_wq);
1017 break;
1018 case DEV_STATE_BOXED:
1019 /* Device did not respond in time. */
1020 case DEV_STATE_OFFLINE:
1021 /*
1022 * We can't register the device in interrupt context so
1023 * we schedule a work item.
1024 */
1025 if (!get_device(&cdev->dev))
1026 break;
1027 PREPARE_WORK(&cdev->private->kick_work,
Martin Schwidefskyc1637532006-12-08 15:53:57 +01001028 io_subchannel_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 queue_work(slow_path_wq, &cdev->private->kick_work);
1030 break;
1031 }
1032}
1033
1034static int
1035io_subchannel_recog(struct ccw_device *cdev, struct subchannel *sch)
1036{
1037 int rc;
1038 struct ccw_device_private *priv;
1039
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001040 sch_set_cdev(sch, cdev);
Cornelia Huck2ec22982006-12-08 15:54:26 +01001041 cdev->ccwlock = sch->lock;
Cornelia Huckfb6958a2006-01-06 00:19:25 -08001042
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 /* Init private data. */
1044 priv = cdev->private;
Cornelia Huck78964262006-10-11 15:31:38 +02001045 priv->dev_id.devno = sch->schib.pmcw.dev;
1046 priv->dev_id.ssid = sch->schid.ssid;
1047 priv->schid = sch->schid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 priv->state = DEV_STATE_NOT_OPER;
1049 INIT_LIST_HEAD(&priv->cmb_list);
1050 init_waitqueue_head(&priv->wait_q);
1051 init_timer(&priv->timer);
1052
1053 /* Set an initial name for the device. */
Cornelia Huckfb6958a2006-01-06 00:19:25 -08001054 snprintf (cdev->dev.bus_id, BUS_ID_SIZE, "0.%x.%04x",
1055 sch->schid.ssid, sch->schib.pmcw.dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056
1057 /* Increase counter of devices currently in recognition. */
1058 atomic_inc(&ccw_device_init_count);
1059
1060 /* Start async. device sensing. */
Cornelia Huck2ec22982006-12-08 15:54:26 +01001061 spin_lock_irq(sch->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 rc = ccw_device_recognition(cdev);
Cornelia Huck2ec22982006-12-08 15:54:26 +01001063 spin_unlock_irq(sch->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 if (rc) {
1065 if (atomic_dec_and_test(&ccw_device_init_count))
1066 wake_up(&ccw_device_init_wq);
1067 }
1068 return rc;
1069}
1070
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +01001071static void ccw_device_move_to_sch(struct work_struct *work)
1072{
1073 struct ccw_device_private *priv;
1074 int rc;
1075 struct subchannel *sch;
1076 struct ccw_device *cdev;
1077 struct subchannel *former_parent;
1078
1079 priv = container_of(work, struct ccw_device_private, kick_work);
1080 sch = priv->sch;
1081 cdev = priv->cdev;
1082 former_parent = ccw_device_is_orphan(cdev) ?
1083 NULL : to_subchannel(get_device(cdev->dev.parent));
1084 mutex_lock(&sch->reg_mutex);
1085 /* Try to move the ccw device to its new subchannel. */
1086 rc = device_move(&cdev->dev, &sch->dev);
1087 mutex_unlock(&sch->reg_mutex);
1088 if (rc) {
Michael Ernst139b83d2008-05-07 09:22:54 +02001089 CIO_MSG_EVENT(0, "Moving device 0.%x.%04x to subchannel "
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +01001090 "0.%x.%04x failed (ret=%d)!\n",
1091 cdev->private->dev_id.ssid,
1092 cdev->private->dev_id.devno, sch->schid.ssid,
1093 sch->schid.sch_no, rc);
1094 css_sch_device_unregister(sch);
1095 goto out;
1096 }
1097 if (former_parent) {
1098 spin_lock_irq(former_parent->lock);
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001099 sch_set_cdev(former_parent, NULL);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +01001100 spin_unlock_irq(former_parent->lock);
1101 css_sch_device_unregister(former_parent);
1102 /* Reset intparm to zeroes. */
1103 former_parent->schib.pmcw.intparm = 0;
1104 cio_modify(former_parent);
1105 }
1106 sch_attach_device(sch, cdev);
1107out:
1108 if (former_parent)
1109 put_device(&former_parent->dev);
1110 put_device(&cdev->dev);
1111}
1112
Cornelia Huck602b20f2008-01-26 14:10:39 +01001113static void io_subchannel_irq(struct subchannel *sch)
1114{
1115 struct ccw_device *cdev;
1116
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001117 cdev = sch_get_cdev(sch);
Cornelia Huck602b20f2008-01-26 14:10:39 +01001118
1119 CIO_TRACE_EVENT(3, "IRQ");
1120 CIO_TRACE_EVENT(3, sch->dev.bus_id);
1121 if (cdev)
1122 dev_fsm_event(cdev, DEV_EVENT_INTERRUPT);
1123}
1124
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001125static void io_subchannel_init_fields(struct subchannel *sch)
1126{
1127 if (cio_is_console(sch->schid))
1128 sch->opm = 0xff;
1129 else
1130 sch->opm = chp_get_sch_opm(sch);
1131 sch->lpm = sch->schib.pmcw.pam & sch->opm;
Cornelia Huck3a3fc292008-07-14 09:58:58 +02001132 sch->isc = cio_is_console(sch->schid) ? CONSOLE_ISC : IO_SCH_ISC;
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001133
1134 CIO_MSG_EVENT(6, "Detected device %04x on subchannel 0.%x.%04X"
1135 " - PIM = %02X, PAM = %02X, POM = %02X\n",
1136 sch->schib.pmcw.dev, sch->schid.ssid,
1137 sch->schid.sch_no, sch->schib.pmcw.pim,
1138 sch->schib.pmcw.pam, sch->schib.pmcw.pom);
1139 /* Initially set up some fields in the pmcw. */
1140 sch->schib.pmcw.ena = 0;
1141 sch->schib.pmcw.csense = 1; /* concurrent sense */
1142 if ((sch->lpm & (sch->lpm - 1)) != 0)
1143 sch->schib.pmcw.mp = 1; /* multipath mode */
1144 /* clean up possible residual cmf stuff */
1145 sch->schib.pmcw.mme = 0;
1146 sch->schib.pmcw.mbfc = 0;
1147 sch->schib.pmcw.mbi = 0;
1148 sch->schib.mba = 0;
1149}
1150
1151static int io_subchannel_probe(struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 struct ccw_device *cdev;
1154 int rc;
1155 unsigned long flags;
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +01001156 struct ccw_dev_id dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001158 cdev = sch_get_cdev(sch);
1159 if (cdev) {
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001160 rc = sysfs_create_group(&sch->dev.kobj,
1161 &io_subchannel_attr_group);
1162 if (rc)
1163 CIO_MSG_EVENT(0, "Failed to create io subchannel "
1164 "attributes for subchannel "
1165 "0.%x.%04x (rc=%d)\n",
1166 sch->schid.ssid, sch->schid.sch_no, rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 /*
1168 * This subchannel already has an associated ccw_device.
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001169 * Throw the delayed uevent for the subchannel, register
1170 * the ccw_device and exit. This happens for all early
1171 * devices, e.g. the console.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 */
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001173 sch->dev.uevent_suppress = 0;
1174 kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
Cornelia Hucke1031782007-10-12 16:11:23 +02001175 cdev->dev.groups = ccwdev_attr_groups;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 device_initialize(&cdev->dev);
1177 ccw_device_register(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 /*
1179 * Check if the device is already online. If it is
1180 * the reference count needs to be corrected
1181 * (see ccw_device_online and css_init_done for the
1182 * ugly details).
1183 */
1184 if (cdev->private->state != DEV_STATE_NOT_OPER &&
1185 cdev->private->state != DEV_STATE_OFFLINE &&
1186 cdev->private->state != DEV_STATE_BOXED)
1187 get_device(&cdev->dev);
1188 return 0;
1189 }
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001190 io_subchannel_init_fields(sch);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +01001191 /*
1192 * First check if a fitting device may be found amongst the
1193 * disconnected devices or in the orphanage.
1194 */
1195 dev_id.devno = sch->schib.pmcw.dev;
1196 dev_id.ssid = sch->schid.ssid;
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001197 rc = sysfs_create_group(&sch->dev.kobj,
1198 &io_subchannel_attr_group);
1199 if (rc)
1200 return rc;
Cornelia Huckcd6b4f22008-01-26 14:10:43 +01001201 /* Allocate I/O subchannel private data. */
1202 sch->private = kzalloc(sizeof(struct io_subchannel_private),
1203 GFP_KERNEL | GFP_DMA);
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001204 if (!sch->private) {
1205 rc = -ENOMEM;
1206 goto out_err;
1207 }
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +01001208 cdev = get_disc_ccwdev_by_dev_id(&dev_id, NULL);
1209 if (!cdev)
1210 cdev = get_orphaned_ccwdev_by_dev_id(to_css(sch->dev.parent),
1211 &dev_id);
1212 if (cdev) {
1213 /*
1214 * Schedule moving the device until when we have a registered
1215 * subchannel to move to and succeed the probe. We can
1216 * unregister later again, when the probe is through.
1217 */
1218 cdev->private->sch = sch;
1219 PREPARE_WORK(&cdev->private->kick_work,
1220 ccw_device_move_to_sch);
1221 queue_work(slow_path_wq, &cdev->private->kick_work);
1222 return 0;
1223 }
Cornelia Huck7674da72006-12-08 15:54:21 +01001224 cdev = io_subchannel_create_ccwdev(sch);
Cornelia Huckcd6b4f22008-01-26 14:10:43 +01001225 if (IS_ERR(cdev)) {
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001226 rc = PTR_ERR(cdev);
1227 goto out_err;
Cornelia Huckcd6b4f22008-01-26 14:10:43 +01001228 }
Cornelia Huck8bbace72006-01-11 10:56:22 +01001229 rc = io_subchannel_recog(cdev, sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 if (rc) {
Cornelia Huck2ec22982006-12-08 15:54:26 +01001231 spin_lock_irqsave(sch->lock, flags);
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001232 sch_set_cdev(sch, NULL);
Cornelia Huck2ec22982006-12-08 15:54:26 +01001233 spin_unlock_irqrestore(sch->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 if (cdev->dev.release)
1235 cdev->dev.release(&cdev->dev);
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001236 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 }
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001238 return 0;
1239out_err:
1240 kfree(sch->private);
1241 sysfs_remove_group(&sch->dev.kobj, &io_subchannel_attr_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 return rc;
1243}
1244
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245static int
Cornelia Huck8bbace72006-01-11 10:56:22 +01001246io_subchannel_remove (struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247{
1248 struct ccw_device *cdev;
1249 unsigned long flags;
1250
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001251 cdev = sch_get_cdev(sch);
1252 if (!cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 /* Set ccw device to not operational and drop reference. */
1255 spin_lock_irqsave(cdev->ccwlock, flags);
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001256 sch_set_cdev(sch, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 cdev->private->state = DEV_STATE_NOT_OPER;
1258 spin_unlock_irqrestore(cdev->ccwlock, flags);
Cornelia Huckef995162007-04-27 16:01:39 +02001259 ccw_device_unregister(cdev);
1260 put_device(&cdev->dev);
Cornelia Huckcd6b4f22008-01-26 14:10:43 +01001261 kfree(sch->private);
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001262 sysfs_remove_group(&sch->dev.kobj, &io_subchannel_attr_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 return 0;
1264}
1265
Cornelia Huck602b20f2008-01-26 14:10:39 +01001266static int io_subchannel_notify(struct subchannel *sch, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267{
1268 struct ccw_device *cdev;
1269
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001270 cdev = sch_get_cdev(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 if (!cdev)
1272 return 0;
Cornelia Huckc820de32008-07-14 09:58:45 +02001273 return ccw_device_notify(cdev, event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274}
1275
Cornelia Huck602b20f2008-01-26 14:10:39 +01001276static void io_subchannel_verify(struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277{
1278 struct ccw_device *cdev;
1279
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001280 cdev = sch_get_cdev(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 if (cdev)
1282 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1283}
1284
Cornelia Huckc820de32008-07-14 09:58:45 +02001285static int check_for_io_on_path(struct subchannel *sch, int mask)
1286{
1287 int cc;
1288
1289 cc = stsch(sch->schid, &sch->schib);
1290 if (cc)
1291 return 0;
Peter Oberparleiter23d805b2008-07-14 09:58:50 +02001292 if (scsw_actl(&sch->schib.scsw) && sch->schib.pmcw.lpum == mask)
Cornelia Huckc820de32008-07-14 09:58:45 +02001293 return 1;
1294 return 0;
1295}
1296
1297static void terminate_internal_io(struct subchannel *sch,
1298 struct ccw_device *cdev)
1299{
1300 if (cio_clear(sch)) {
1301 /* Recheck device in case clear failed. */
1302 sch->lpm = 0;
1303 if (cdev->online)
1304 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1305 else
1306 css_schedule_eval(sch->schid);
1307 return;
1308 }
1309 cdev->private->state = DEV_STATE_CLEAR_VERIFY;
1310 /* Request retry of internal operation. */
1311 cdev->private->flags.intretry = 1;
1312 /* Call handler. */
1313 if (cdev->handler)
1314 cdev->handler(cdev, cdev->private->intparm,
1315 ERR_PTR(-EIO));
1316}
1317
1318static void io_subchannel_terminate_path(struct subchannel *sch, u8 mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319{
1320 struct ccw_device *cdev;
1321
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001322 cdev = sch_get_cdev(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 if (!cdev)
1324 return;
Cornelia Huckc820de32008-07-14 09:58:45 +02001325 if (check_for_io_on_path(sch, mask)) {
1326 if (cdev->private->state == DEV_STATE_ONLINE)
1327 ccw_device_kill_io(cdev);
1328 else {
1329 terminate_internal_io(sch, cdev);
1330 /* Re-start path verification. */
1331 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1332 }
1333 } else
1334 /* trigger path verification. */
1335 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1336
1337}
1338
Cornelia Huck99611f82008-07-14 09:59:02 +02001339static int io_subchannel_chp_event(struct subchannel *sch,
1340 struct chp_link *link, int event)
Cornelia Huckc820de32008-07-14 09:58:45 +02001341{
1342 int mask;
Cornelia Huckc820de32008-07-14 09:58:45 +02001343
Cornelia Huck99611f82008-07-14 09:59:02 +02001344 mask = chp_ssd_get_mask(&sch->ssd_info, link);
Cornelia Huckc820de32008-07-14 09:58:45 +02001345 if (!mask)
1346 return 0;
1347 switch (event) {
1348 case CHP_VARY_OFF:
1349 sch->opm &= ~mask;
1350 sch->lpm &= ~mask;
1351 io_subchannel_terminate_path(sch, mask);
1352 break;
1353 case CHP_VARY_ON:
1354 sch->opm |= mask;
1355 sch->lpm |= mask;
1356 io_subchannel_verify(sch);
1357 break;
1358 case CHP_OFFLINE:
1359 if (stsch(sch->schid, &sch->schib))
1360 return -ENXIO;
1361 if (!css_sch_is_valid(&sch->schib))
1362 return -ENODEV;
1363 io_subchannel_terminate_path(sch, mask);
1364 break;
1365 case CHP_ONLINE:
1366 if (stsch(sch->schid, &sch->schib))
1367 return -ENXIO;
1368 sch->lpm |= mask & sch->opm;
1369 io_subchannel_verify(sch);
1370 break;
1371 }
1372 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373}
1374
1375static void
Cornelia Huck8bbace72006-01-11 10:56:22 +01001376io_subchannel_shutdown(struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 struct ccw_device *cdev;
1379 int ret;
1380
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001381 cdev = sch_get_cdev(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382
Cornelia Hucka8237fc2006-01-06 00:19:21 -08001383 if (cio_is_console(sch->schid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 return;
1385 if (!sch->schib.pmcw.ena)
1386 /* Nothing to do. */
1387 return;
1388 ret = cio_disable_subchannel(sch);
1389 if (ret != -EBUSY)
1390 /* Subchannel is disabled, we're done. */
1391 return;
1392 cdev->private->state = DEV_STATE_QUIESCE;
1393 if (cdev->handler)
1394 cdev->handler(cdev, cdev->private->intparm,
1395 ERR_PTR(-EIO));
1396 ret = ccw_device_cancel_halt_clear(cdev);
1397 if (ret == -EBUSY) {
1398 ccw_device_set_timeout(cdev, HZ/10);
1399 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
1400 }
1401 cio_disable_subchannel(sch);
1402}
1403
Cornelia Huckc820de32008-07-14 09:58:45 +02001404static int io_subchannel_get_status(struct subchannel *sch)
1405{
1406 struct schib schib;
1407
1408 if (stsch(sch->schid, &schib) || !schib.pmcw.dnv)
1409 return CIO_GONE;
1410 if (sch->schib.pmcw.dnv && (schib.pmcw.dev != sch->schib.pmcw.dev))
1411 return CIO_REVALIDATE;
1412 if (!sch->lpm)
1413 return CIO_NO_PATH;
1414 return CIO_OPER;
1415}
1416
1417static int device_is_disconnected(struct ccw_device *cdev)
1418{
1419 if (!cdev)
1420 return 0;
1421 return (cdev->private->state == DEV_STATE_DISCONNECTED ||
1422 cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID);
1423}
1424
1425static int recovery_check(struct device *dev, void *data)
1426{
1427 struct ccw_device *cdev = to_ccwdev(dev);
1428 int *redo = data;
1429
1430 spin_lock_irq(cdev->ccwlock);
1431 switch (cdev->private->state) {
1432 case DEV_STATE_DISCONNECTED:
1433 CIO_MSG_EVENT(3, "recovery: trigger 0.%x.%04x\n",
1434 cdev->private->dev_id.ssid,
1435 cdev->private->dev_id.devno);
1436 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1437 *redo = 1;
1438 break;
1439 case DEV_STATE_DISCONNECTED_SENSE_ID:
1440 *redo = 1;
1441 break;
1442 }
1443 spin_unlock_irq(cdev->ccwlock);
1444
1445 return 0;
1446}
1447
1448static void recovery_work_func(struct work_struct *unused)
1449{
1450 int redo = 0;
1451
1452 bus_for_each_dev(&ccw_bus_type, NULL, &redo, recovery_check);
1453 if (redo) {
1454 spin_lock_irq(&recovery_lock);
1455 if (!timer_pending(&recovery_timer)) {
1456 if (recovery_phase < ARRAY_SIZE(recovery_delay) - 1)
1457 recovery_phase++;
1458 mod_timer(&recovery_timer, jiffies +
1459 recovery_delay[recovery_phase] * HZ);
1460 }
1461 spin_unlock_irq(&recovery_lock);
1462 } else
1463 CIO_MSG_EVENT(4, "recovery: end\n");
1464}
1465
1466static DECLARE_WORK(recovery_work, recovery_work_func);
1467
1468static void recovery_func(unsigned long data)
1469{
1470 /*
1471 * We can't do our recovery in softirq context and it's not
1472 * performance critical, so we schedule it.
1473 */
1474 schedule_work(&recovery_work);
1475}
1476
1477static void ccw_device_schedule_recovery(void)
1478{
1479 unsigned long flags;
1480
1481 CIO_MSG_EVENT(4, "recovery: schedule\n");
1482 spin_lock_irqsave(&recovery_lock, flags);
1483 if (!timer_pending(&recovery_timer) || (recovery_phase != 0)) {
1484 recovery_phase = 0;
1485 mod_timer(&recovery_timer, jiffies + recovery_delay[0] * HZ);
1486 }
1487 spin_unlock_irqrestore(&recovery_lock, flags);
1488}
1489
1490static void device_set_disconnected(struct ccw_device *cdev)
1491{
1492 if (!cdev)
1493 return;
1494 ccw_device_set_timeout(cdev, 0);
1495 cdev->private->flags.fake_irb = 0;
1496 cdev->private->state = DEV_STATE_DISCONNECTED;
1497 if (cdev->online)
1498 ccw_device_schedule_recovery();
1499}
1500
1501static int io_subchannel_sch_event(struct subchannel *sch, int slow)
1502{
1503 int event, ret, disc;
1504 unsigned long flags;
1505 enum { NONE, UNREGISTER, UNREGISTER_PROBE, REPROBE } action;
1506 struct ccw_device *cdev;
1507
1508 spin_lock_irqsave(sch->lock, flags);
1509 cdev = sch_get_cdev(sch);
1510 disc = device_is_disconnected(cdev);
1511 if (disc && slow) {
1512 /* Disconnected devices are evaluated directly only.*/
1513 spin_unlock_irqrestore(sch->lock, flags);
1514 return 0;
1515 }
1516 /* No interrupt after machine check - kill pending timers. */
1517 if (cdev)
1518 ccw_device_set_timeout(cdev, 0);
1519 if (!disc && !slow) {
1520 /* Non-disconnected devices are evaluated on the slow path. */
1521 spin_unlock_irqrestore(sch->lock, flags);
1522 return -EAGAIN;
1523 }
1524 event = io_subchannel_get_status(sch);
1525 CIO_MSG_EVENT(4, "Evaluating schid 0.%x.%04x, event %d, %s, %s path.\n",
1526 sch->schid.ssid, sch->schid.sch_no, event,
1527 disc ? "disconnected" : "normal",
1528 slow ? "slow" : "fast");
1529 /* Analyze subchannel status. */
1530 action = NONE;
1531 switch (event) {
1532 case CIO_NO_PATH:
1533 if (disc) {
1534 /* Check if paths have become available. */
1535 action = REPROBE;
1536 break;
1537 }
1538 /* fall through */
1539 case CIO_GONE:
1540 /* Prevent unwanted effects when opening lock. */
1541 cio_disable_subchannel(sch);
1542 device_set_disconnected(cdev);
1543 /* Ask driver what to do with device. */
1544 action = UNREGISTER;
1545 spin_unlock_irqrestore(sch->lock, flags);
1546 ret = io_subchannel_notify(sch, event);
1547 spin_lock_irqsave(sch->lock, flags);
1548 if (ret)
1549 action = NONE;
1550 break;
1551 case CIO_REVALIDATE:
1552 /* Device will be removed, so no notify necessary. */
1553 if (disc)
1554 /* Reprobe because immediate unregister might block. */
1555 action = REPROBE;
1556 else
1557 action = UNREGISTER_PROBE;
1558 break;
1559 case CIO_OPER:
1560 if (disc)
1561 /* Get device operational again. */
1562 action = REPROBE;
1563 break;
1564 }
1565 /* Perform action. */
1566 ret = 0;
1567 switch (action) {
1568 case UNREGISTER:
1569 case UNREGISTER_PROBE:
1570 /* Unregister device (will use subchannel lock). */
1571 spin_unlock_irqrestore(sch->lock, flags);
1572 css_sch_device_unregister(sch);
1573 spin_lock_irqsave(sch->lock, flags);
1574
1575 /* Reset intparm to zeroes. */
1576 sch->schib.pmcw.intparm = 0;
1577 cio_modify(sch);
1578 break;
1579 case REPROBE:
1580 ccw_device_trigger_reprobe(cdev);
1581 break;
1582 default:
1583 break;
1584 }
1585 spin_unlock_irqrestore(sch->lock, flags);
1586 /* Probe if necessary. */
1587 if (action == UNREGISTER_PROBE)
1588 ret = css_probe_device(sch->schid);
1589
1590 return ret;
1591}
1592
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593#ifdef CONFIG_CCW_CONSOLE
1594static struct ccw_device console_cdev;
1595static struct ccw_device_private console_private;
1596static int console_cdev_in_use;
1597
Cornelia Huck2ec22982006-12-08 15:54:26 +01001598static DEFINE_SPINLOCK(ccw_console_lock);
1599
1600spinlock_t * cio_get_console_lock(void)
1601{
1602 return &ccw_console_lock;
1603}
1604
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001605static int ccw_device_console_enable(struct ccw_device *cdev,
1606 struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607{
1608 int rc;
1609
Cornelia Huckcd6b4f22008-01-26 14:10:43 +01001610 /* Attach subchannel private data. */
1611 sch->private = cio_get_console_priv();
1612 memset(sch->private, 0, sizeof(struct io_subchannel_private));
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001613 io_subchannel_init_fields(sch);
1614 sch->driver = &io_subchannel_driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 /* Initialize the ccw_device structure. */
Heiko Carstens292888c2006-08-30 14:33:35 +02001616 cdev->dev.parent= &sch->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 rc = io_subchannel_recog(cdev, sch);
1618 if (rc)
1619 return rc;
1620
1621 /* Now wait for the async. recognition to come to an end. */
1622 spin_lock_irq(cdev->ccwlock);
1623 while (!dev_fsm_final_state(cdev))
1624 wait_cons_dev();
1625 rc = -EIO;
1626 if (cdev->private->state != DEV_STATE_OFFLINE)
1627 goto out_unlock;
1628 ccw_device_online(cdev);
1629 while (!dev_fsm_final_state(cdev))
1630 wait_cons_dev();
1631 if (cdev->private->state != DEV_STATE_ONLINE)
1632 goto out_unlock;
1633 rc = 0;
1634out_unlock:
1635 spin_unlock_irq(cdev->ccwlock);
1636 return 0;
1637}
1638
1639struct ccw_device *
1640ccw_device_probe_console(void)
1641{
1642 struct subchannel *sch;
1643 int ret;
1644
1645 if (xchg(&console_cdev_in_use, 1) != 0)
Peter Oberparleiter600b5d12006-02-01 03:06:35 -08001646 return ERR_PTR(-EBUSY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 sch = cio_probe_console();
1648 if (IS_ERR(sch)) {
1649 console_cdev_in_use = 0;
1650 return (void *) sch;
1651 }
1652 memset(&console_cdev, 0, sizeof(struct ccw_device));
1653 memset(&console_private, 0, sizeof(struct ccw_device_private));
1654 console_cdev.private = &console_private;
Martin Schwidefskyc1637532006-12-08 15:53:57 +01001655 console_private.cdev = &console_cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 ret = ccw_device_console_enable(&console_cdev, sch);
1657 if (ret) {
1658 cio_release_console();
1659 console_cdev_in_use = 0;
1660 return ERR_PTR(ret);
1661 }
1662 console_cdev.online = 1;
1663 return &console_cdev;
1664}
1665#endif
1666
1667/*
1668 * get ccw_device matching the busid, but only if owned by cdrv
1669 */
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001670static int
1671__ccwdev_check_busid(struct device *dev, void *id)
1672{
1673 char *bus_id;
1674
Cornelia Huck12975ae2006-10-11 15:31:47 +02001675 bus_id = id;
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001676
1677 return (strncmp(bus_id, dev->bus_id, BUS_ID_SIZE) == 0);
1678}
1679
1680
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +02001681/**
1682 * get_ccwdev_by_busid() - obtain device from a bus id
1683 * @cdrv: driver the device is owned by
1684 * @bus_id: bus id of the device to be searched
1685 *
1686 * This function searches all devices owned by @cdrv for a device with a bus
1687 * id matching @bus_id.
1688 * Returns:
1689 * If a match is found, its reference count of the found device is increased
1690 * and it is returned; else %NULL is returned.
1691 */
1692struct ccw_device *get_ccwdev_by_busid(struct ccw_driver *cdrv,
1693 const char *bus_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694{
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001695 struct device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 struct device_driver *drv;
1697
1698 drv = get_driver(&cdrv->driver);
1699 if (!drv)
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001700 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001702 dev = driver_find_device(drv, NULL, (void *)bus_id,
1703 __ccwdev_check_busid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 put_driver(drv);
1705
Heiko Carstensd2c993d2006-07-12 16:41:55 +02001706 return dev ? to_ccwdev(dev) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707}
1708
1709/************************** device driver handling ************************/
1710
1711/* This is the implementation of the ccw_driver class. The probe, remove
1712 * and release methods are initially very similar to the device_driver
1713 * implementations, with the difference that they have ccw_device
1714 * arguments.
1715 *
1716 * A ccw driver also contains the information that is needed for
1717 * device matching.
1718 */
1719static int
1720ccw_device_probe (struct device *dev)
1721{
1722 struct ccw_device *cdev = to_ccwdev(dev);
1723 struct ccw_driver *cdrv = to_ccwdrv(dev->driver);
1724 int ret;
1725
1726 cdev->drv = cdrv; /* to let the driver call _set_online */
1727
1728 ret = cdrv->probe ? cdrv->probe(cdev) : -ENODEV;
1729
1730 if (ret) {
Heiko Carstensd2c993d2006-07-12 16:41:55 +02001731 cdev->drv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 return ret;
1733 }
1734
1735 return 0;
1736}
1737
1738static int
1739ccw_device_remove (struct device *dev)
1740{
1741 struct ccw_device *cdev = to_ccwdev(dev);
1742 struct ccw_driver *cdrv = cdev->drv;
1743 int ret;
1744
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 if (cdrv->remove)
1746 cdrv->remove(cdev);
1747 if (cdev->online) {
1748 cdev->online = 0;
1749 spin_lock_irq(cdev->ccwlock);
1750 ret = ccw_device_offline(cdev);
1751 spin_unlock_irq(cdev->ccwlock);
1752 if (ret == 0)
1753 wait_event(cdev->private->wait_q,
1754 dev_fsm_final_state(cdev));
1755 else
Michael Ernst139b83d2008-05-07 09:22:54 +02001756 CIO_MSG_EVENT(0, "ccw_device_offline returned %d, "
Cornelia Hucke556bbb2007-07-27 12:29:19 +02001757 "device 0.%x.%04x\n",
1758 ret, cdev->private->dev_id.ssid,
1759 cdev->private->dev_id.devno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 }
1761 ccw_device_set_timeout(cdev, 0);
Heiko Carstensd2c993d2006-07-12 16:41:55 +02001762 cdev->drv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 return 0;
1764}
1765
Cornelia Huck958974fb2007-10-12 16:11:21 +02001766static void ccw_device_shutdown(struct device *dev)
1767{
1768 struct ccw_device *cdev;
1769
1770 cdev = to_ccwdev(dev);
1771 if (cdev->drv && cdev->drv->shutdown)
1772 cdev->drv->shutdown(cdev);
Cornelia Huck1842f2b2007-10-12 16:11:22 +02001773 disable_cmf(cdev);
Cornelia Huck958974fb2007-10-12 16:11:21 +02001774}
1775
Cornelia Huck8bbace72006-01-11 10:56:22 +01001776struct bus_type ccw_bus_type = {
1777 .name = "ccw",
1778 .match = ccw_bus_match,
1779 .uevent = ccw_uevent,
1780 .probe = ccw_device_probe,
1781 .remove = ccw_device_remove,
Cornelia Huck958974fb2007-10-12 16:11:21 +02001782 .shutdown = ccw_device_shutdown,
Cornelia Huck8bbace72006-01-11 10:56:22 +01001783};
1784
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +02001785/**
1786 * ccw_driver_register() - register a ccw driver
1787 * @cdriver: driver to be registered
1788 *
1789 * This function is mainly a wrapper around driver_register().
1790 * Returns:
1791 * %0 on success and a negative error value on failure.
1792 */
1793int ccw_driver_register(struct ccw_driver *cdriver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794{
1795 struct device_driver *drv = &cdriver->driver;
1796
1797 drv->bus = &ccw_bus_type;
1798 drv->name = cdriver->name;
Cornelia Huck4beee642008-01-26 14:10:47 +01001799 drv->owner = cdriver->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800
1801 return driver_register(drv);
1802}
1803
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +02001804/**
1805 * ccw_driver_unregister() - deregister a ccw driver
1806 * @cdriver: driver to be deregistered
1807 *
1808 * This function is mainly a wrapper around driver_unregister().
1809 */
1810void ccw_driver_unregister(struct ccw_driver *cdriver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811{
1812 driver_unregister(&cdriver->driver);
1813}
1814
Cornelia Hucka8237fc2006-01-06 00:19:21 -08001815/* Helper func for qdio. */
1816struct subchannel_id
1817ccw_device_get_subchannel_id(struct ccw_device *cdev)
1818{
1819 struct subchannel *sch;
1820
1821 sch = to_subchannel(cdev->dev.parent);
1822 return sch->schid;
1823}
1824
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825MODULE_LICENSE("GPL");
1826EXPORT_SYMBOL(ccw_device_set_online);
1827EXPORT_SYMBOL(ccw_device_set_offline);
1828EXPORT_SYMBOL(ccw_driver_register);
1829EXPORT_SYMBOL(ccw_driver_unregister);
1830EXPORT_SYMBOL(get_ccwdev_by_busid);
1831EXPORT_SYMBOL(ccw_bus_type);
1832EXPORT_SYMBOL(ccw_device_work);
1833EXPORT_SYMBOL(ccw_device_notify_work);
Cornelia Hucka8237fc2006-01-06 00:19:21 -08001834EXPORT_SYMBOL_GPL(ccw_device_get_subchannel_id);