blob: 28221030b886cfa5ae8d794fdf176bdbfe401845 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * drivers/s390/cio/device.c
3 * bus driver for ccw devices
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Cornelia Huckc820de32008-07-14 09:58:45 +02005 * Copyright IBM Corp. 2002,2008
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * Author(s): Arnd Bergmann (arndb@de.ibm.com)
Cornelia Huck4ce3b302006-01-14 13:21:04 -08007 * Cornelia Huck (cornelia.huck@de.ibm.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * Martin Schwidefsky (schwidefsky@de.ibm.com)
9 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/module.h>
11#include <linux/init.h>
12#include <linux/spinlock.h>
13#include <linux/errno.h>
14#include <linux/err.h>
15#include <linux/slab.h>
16#include <linux/list.h>
17#include <linux/device.h>
18#include <linux/workqueue.h>
Peter Oberparleiter90ab1332008-01-26 14:10:52 +010019#include <linux/timer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
21#include <asm/ccwdev.h>
22#include <asm/cio.h>
Tim Schmielau4e57b682005-10-30 15:03:48 -080023#include <asm/param.h> /* HZ */
Cornelia Huck1842f2b2007-10-12 16:11:22 +020024#include <asm/cmb.h>
Cornelia Huck3a3fc292008-07-14 09:58:58 +020025#include <asm/isc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +020027#include "chp.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include "cio.h"
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +010029#include "cio_debug.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include "css.h"
31#include "device.h"
32#include "ioasm.h"
Cornelia Huckcd6b4f22008-01-26 14:10:43 +010033#include "io_sch.h"
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;
Peter Oberparleiter40154b82006-06-29 14:57:03 +0200153wait_queue_head_t ccw_device_init_wq;
154atomic_t ccw_device_init_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
Peter Oberparleiter90ab1332008-01-26 14:10:52 +0100156static void recovery_func(unsigned long data);
157
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158static int __init
159init_ccw_bus_type (void)
160{
161 int ret;
162
163 init_waitqueue_head(&ccw_device_init_wq);
164 atomic_set(&ccw_device_init_count, 0);
Peter Oberparleiter90ab1332008-01-26 14:10:52 +0100165 setup_timer(&recovery_timer, recovery_func, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
167 ccw_device_work = create_singlethread_workqueue("cio");
168 if (!ccw_device_work)
169 return -ENOMEM; /* FIXME: better errno ? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 if (slow_path_wq)
190 destroy_workqueue(slow_path_wq);
191 return ret;
192}
193
194static void __exit
195cleanup_ccw_bus_type (void)
196{
Cornelia Huck25b7bb52008-01-26 14:10:41 +0100197 css_driver_unregister(&io_subchannel_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 bus_unregister(&ccw_bus_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 destroy_workqueue(ccw_device_work);
200}
201
202subsys_initcall(init_ccw_bus_type);
203module_exit(cleanup_ccw_bus_type);
204
205/************************ device handling **************************/
206
207/*
208 * A ccw_device has some interfaces in sysfs in addition to the
209 * standard ones.
210 * The following entries are designed to export the information which
211 * resided in 2.4 in /proc/subchannels. Subchannel and device number
212 * are obvious, so they don't have an entry :)
213 * TODO: Split chpids and pimpampom up? Where is "in use" in the tree?
214 */
215static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400216chpids_show (struct device * dev, struct device_attribute *attr, char * buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217{
218 struct subchannel *sch = to_subchannel(dev);
Peter Oberparleiter7ad6a242007-04-27 16:01:35 +0200219 struct chsc_ssd_info *ssd = &sch->ssd_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 ssize_t ret = 0;
221 int chp;
Peter Oberparleiter7ad6a242007-04-27 16:01:35 +0200222 int mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
Peter Oberparleiter7ad6a242007-04-27 16:01:35 +0200224 for (chp = 0; chp < 8; chp++) {
225 mask = 0x80 >> chp;
226 if (ssd->path_mask & mask)
227 ret += sprintf(buf + ret, "%02x ", ssd->chpid[chp].id);
228 else
229 ret += sprintf(buf + ret, "00 ");
230 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 ret += sprintf (buf+ret, "\n");
232 return min((ssize_t)PAGE_SIZE, ret);
233}
234
235static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400236pimpampom_show (struct device * dev, struct device_attribute *attr, char * buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237{
238 struct subchannel *sch = to_subchannel(dev);
239 struct pmcw *pmcw = &sch->schib.pmcw;
240
241 return sprintf (buf, "%02x %02x %02x\n",
242 pmcw->pim, pmcw->pam, pmcw->pom);
243}
244
245static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400246devtype_show (struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247{
248 struct ccw_device *cdev = to_ccwdev(dev);
249 struct ccw_device_id *id = &(cdev->id);
250
251 if (id->dev_type != 0)
252 return sprintf(buf, "%04x/%02x\n",
253 id->dev_type, id->dev_model);
254 else
255 return sprintf(buf, "n/a\n");
256}
257
258static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400259cutype_show (struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260{
261 struct ccw_device *cdev = to_ccwdev(dev);
262 struct ccw_device_id *id = &(cdev->id);
263
264 return sprintf(buf, "%04x/%02x\n",
265 id->cu_type, id->cu_model);
266}
267
268static ssize_t
Bastian Blankf1fc78a2005-10-30 15:00:12 -0800269modalias_show (struct device *dev, struct device_attribute *attr, char *buf)
270{
271 struct ccw_device *cdev = to_ccwdev(dev);
272 struct ccw_device_id *id = &(cdev->id);
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +0200273 int len;
Bastian Blankf1fc78a2005-10-30 15:00:12 -0800274
Cornelia Huck086a6c62007-07-17 13:36:08 +0200275 len = snprint_alias(buf, PAGE_SIZE, id, "\n");
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +0200276
277 return len > PAGE_SIZE ? PAGE_SIZE : len;
Bastian Blankf1fc78a2005-10-30 15:00:12 -0800278}
279
280static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400281online_show (struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282{
283 struct ccw_device *cdev = to_ccwdev(dev);
284
285 return sprintf(buf, cdev->online ? "1\n" : "0\n");
286}
287
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100288int ccw_device_is_orphan(struct ccw_device *cdev)
289{
290 return sch_is_pseudo_sch(to_subchannel(cdev->dev.parent));
291}
292
Cornelia Huckef995162007-04-27 16:01:39 +0200293static void ccw_device_unregister(struct ccw_device *cdev)
Cornelia Huck7674da72006-12-08 15:54:21 +0100294{
Cornelia Huck7674da72006-12-08 15:54:21 +0100295 if (test_and_clear_bit(1, &cdev->private->registered))
Cornelia Huckef995162007-04-27 16:01:39 +0200296 device_del(&cdev->dev);
Cornelia Huck7674da72006-12-08 15:54:21 +0100297}
298
Cornelia Huck59a8a6e2007-05-31 17:38:06 +0200299static void ccw_device_remove_orphan_cb(struct device *dev)
300{
301 struct ccw_device *cdev = to_ccwdev(dev);
302
303 ccw_device_unregister(cdev);
304 put_device(&cdev->dev);
305}
306
307static void ccw_device_remove_sch_cb(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308{
309 struct subchannel *sch;
Cornelia Huck59a8a6e2007-05-31 17:38:06 +0200310
311 sch = to_subchannel(dev);
Cornelia Huck6ab48792006-07-12 16:39:50 +0200312 css_sch_device_unregister(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 /* Reset intparm to zeroes. */
314 sch->schib.pmcw.intparm = 0;
315 cio_modify(sch);
316 put_device(&sch->dev);
317}
318
Cornelia Huck59a8a6e2007-05-31 17:38:06 +0200319static void
320ccw_device_remove_disconnected(struct ccw_device *cdev)
321{
322 unsigned long flags;
323 int rc;
324
325 /*
326 * Forced offline in disconnected state means
327 * 'throw away device'.
328 */
329 if (ccw_device_is_orphan(cdev)) {
330 /*
331 * Deregister ccw device.
332 * Unfortunately, we cannot do this directly from the
333 * attribute method.
334 */
335 spin_lock_irqsave(cdev->ccwlock, flags);
336 cdev->private->state = DEV_STATE_NOT_OPER;
337 spin_unlock_irqrestore(cdev->ccwlock, flags);
338 rc = device_schedule_callback(&cdev->dev,
339 ccw_device_remove_orphan_cb);
340 if (rc)
Michael Ernst139b83d2008-05-07 09:22:54 +0200341 CIO_MSG_EVENT(0, "Couldn't unregister orphan "
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200342 "0.%x.%04x\n",
343 cdev->private->dev_id.ssid,
344 cdev->private->dev_id.devno);
Cornelia Huck59a8a6e2007-05-31 17:38:06 +0200345 return;
346 }
347 /* Deregister subchannel, which will kill the ccw device. */
348 rc = device_schedule_callback(cdev->dev.parent,
349 ccw_device_remove_sch_cb);
350 if (rc)
Michael Ernst139b83d2008-05-07 09:22:54 +0200351 CIO_MSG_EVENT(0, "Couldn't unregister disconnected device "
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200352 "0.%x.%04x\n",
353 cdev->private->dev_id.ssid,
354 cdev->private->dev_id.devno);
Cornelia Huck59a8a6e2007-05-31 17:38:06 +0200355}
356
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +0200357/**
358 * ccw_device_set_offline() - disable a ccw device for I/O
359 * @cdev: target ccw device
360 *
361 * This function calls the driver's set_offline() function for @cdev, if
362 * given, and then disables @cdev.
363 * Returns:
364 * %0 on success and a negative error value on failure.
365 * Context:
366 * enabled, ccw device lock not held
367 */
368int ccw_device_set_offline(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369{
370 int ret;
371
372 if (!cdev)
373 return -ENODEV;
374 if (!cdev->online || !cdev->drv)
375 return -EINVAL;
376
377 if (cdev->drv->set_offline) {
378 ret = cdev->drv->set_offline(cdev);
379 if (ret != 0)
380 return ret;
381 }
382 cdev->online = 0;
383 spin_lock_irq(cdev->ccwlock);
384 ret = ccw_device_offline(cdev);
385 if (ret == -ENODEV) {
386 if (cdev->private->state != DEV_STATE_NOT_OPER) {
387 cdev->private->state = DEV_STATE_OFFLINE;
388 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
389 }
390 spin_unlock_irq(cdev->ccwlock);
391 return ret;
392 }
393 spin_unlock_irq(cdev->ccwlock);
394 if (ret == 0)
395 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
396 else {
Michael Ernst139b83d2008-05-07 09:22:54 +0200397 CIO_MSG_EVENT(0, "ccw_device_offline returned %d, "
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200398 "device 0.%x.%04x\n",
399 ret, cdev->private->dev_id.ssid,
400 cdev->private->dev_id.devno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 cdev->online = 1;
402 }
403 return ret;
404}
405
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +0200406/**
407 * ccw_device_set_online() - enable a ccw device for I/O
408 * @cdev: target ccw device
409 *
410 * This function first enables @cdev and then calls the driver's set_online()
411 * function for @cdev, if given. If set_online() returns an error, @cdev is
412 * disabled again.
413 * Returns:
414 * %0 on success and a negative error value on failure.
415 * Context:
416 * enabled, ccw device lock not held
417 */
418int ccw_device_set_online(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419{
420 int ret;
421
422 if (!cdev)
423 return -ENODEV;
424 if (cdev->online || !cdev->drv)
425 return -EINVAL;
426
427 spin_lock_irq(cdev->ccwlock);
428 ret = ccw_device_online(cdev);
429 spin_unlock_irq(cdev->ccwlock);
430 if (ret == 0)
431 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
432 else {
Michael Ernst139b83d2008-05-07 09:22:54 +0200433 CIO_MSG_EVENT(0, "ccw_device_online returned %d, "
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200434 "device 0.%x.%04x\n",
435 ret, cdev->private->dev_id.ssid,
436 cdev->private->dev_id.devno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 return ret;
438 }
439 if (cdev->private->state != DEV_STATE_ONLINE)
440 return -ENODEV;
441 if (!cdev->drv->set_online || cdev->drv->set_online(cdev) == 0) {
442 cdev->online = 1;
443 return 0;
444 }
445 spin_lock_irq(cdev->ccwlock);
446 ret = ccw_device_offline(cdev);
447 spin_unlock_irq(cdev->ccwlock);
448 if (ret == 0)
449 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200450 else
Michael Ernst139b83d2008-05-07 09:22:54 +0200451 CIO_MSG_EVENT(0, "ccw_device_offline returned %d, "
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200452 "device 0.%x.%04x\n",
453 ret, cdev->private->dev_id.ssid,
454 cdev->private->dev_id.devno);
Cornelia Huck6cadb782006-02-17 13:52:49 -0800455 return (ret == 0) ? -ENODEV : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456}
457
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200458static void online_store_handle_offline(struct ccw_device *cdev)
459{
460 if (cdev->private->state == DEV_STATE_DISCONNECTED)
461 ccw_device_remove_disconnected(cdev);
462 else if (cdev->drv && cdev->drv->set_offline)
463 ccw_device_set_offline(cdev);
464}
465
466static int online_store_recog_and_online(struct ccw_device *cdev)
467{
468 int ret;
469
470 /* Do device recognition, if needed. */
471 if (cdev->id.cu_type == 0) {
472 ret = ccw_device_recognition(cdev);
473 if (ret) {
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200474 CIO_MSG_EVENT(0, "Couldn't start recognition "
475 "for device 0.%x.%04x (ret=%d)\n",
476 cdev->private->dev_id.ssid,
477 cdev->private->dev_id.devno, ret);
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200478 return ret;
479 }
480 wait_event(cdev->private->wait_q,
481 cdev->private->flags.recog_done);
482 }
483 if (cdev->drv && cdev->drv->set_online)
484 ccw_device_set_online(cdev);
485 return 0;
486}
Michael Ernstc78aa6c2008-07-14 09:59:22 +0200487static int online_store_handle_online(struct ccw_device *cdev, int force)
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200488{
489 int ret;
490
491 ret = online_store_recog_and_online(cdev);
492 if (ret)
Michael Ernstc78aa6c2008-07-14 09:59:22 +0200493 return ret;
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200494 if (force && cdev->private->state == DEV_STATE_BOXED) {
495 ret = ccw_device_stlck(cdev);
Michael Ernstc78aa6c2008-07-14 09:59:22 +0200496 if (ret)
497 return ret;
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200498 if (cdev->id.cu_type == 0)
499 cdev->private->state = DEV_STATE_NOT_OPER;
500 online_store_recog_and_online(cdev);
501 }
Michael Ernstc78aa6c2008-07-14 09:59:22 +0200502 return 0;
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200503}
504
505static ssize_t online_store (struct device *dev, struct device_attribute *attr,
506 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507{
508 struct ccw_device *cdev = to_ccwdev(dev);
Cornelia Huck2f972202008-04-30 13:38:33 +0200509 int force, ret;
510 unsigned long i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
Martin Schwidefsky973bd992006-01-06 00:19:07 -0800512 if (atomic_cmpxchg(&cdev->private->onoff, 0, 1) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 return -EAGAIN;
514
515 if (cdev->drv && !try_module_get(cdev->drv->owner)) {
516 atomic_set(&cdev->private->onoff, 0);
517 return -EINVAL;
518 }
519 if (!strncmp(buf, "force\n", count)) {
520 force = 1;
521 i = 1;
Cornelia Huck2f972202008-04-30 13:38:33 +0200522 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 } else {
524 force = 0;
Cornelia Huck2f972202008-04-30 13:38:33 +0200525 ret = strict_strtoul(buf, 16, &i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 }
Cornelia Huck2f972202008-04-30 13:38:33 +0200527 if (ret)
528 goto out;
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200529 switch (i) {
530 case 0:
531 online_store_handle_offline(cdev);
Cornelia Huck2f972202008-04-30 13:38:33 +0200532 ret = count;
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200533 break;
534 case 1:
Michael Ernstc78aa6c2008-07-14 09:59:22 +0200535 ret = online_store_handle_online(cdev, force);
536 if (!ret)
537 ret = count;
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200538 break;
539 default:
Cornelia Huck2f972202008-04-30 13:38:33 +0200540 ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 }
Cornelia Huck2f972202008-04-30 13:38:33 +0200542out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 if (cdev->drv)
544 module_put(cdev->drv->owner);
545 atomic_set(&cdev->private->onoff, 0);
Cornelia Huck2f972202008-04-30 13:38:33 +0200546 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547}
548
549static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400550available_show (struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551{
552 struct ccw_device *cdev = to_ccwdev(dev);
553 struct subchannel *sch;
554
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100555 if (ccw_device_is_orphan(cdev))
556 return sprintf(buf, "no device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 switch (cdev->private->state) {
558 case DEV_STATE_BOXED:
559 return sprintf(buf, "boxed\n");
560 case DEV_STATE_DISCONNECTED:
561 case DEV_STATE_DISCONNECTED_SENSE_ID:
562 case DEV_STATE_NOT_OPER:
563 sch = to_subchannel(dev->parent);
564 if (!sch->lpm)
565 return sprintf(buf, "no path\n");
566 else
567 return sprintf(buf, "no device\n");
568 default:
569 /* All other states considered fine. */
570 return sprintf(buf, "good\n");
571 }
572}
573
574static DEVICE_ATTR(chpids, 0444, chpids_show, NULL);
575static DEVICE_ATTR(pimpampom, 0444, pimpampom_show, NULL);
576static DEVICE_ATTR(devtype, 0444, devtype_show, NULL);
577static DEVICE_ATTR(cutype, 0444, cutype_show, NULL);
Bastian Blankf1fc78a2005-10-30 15:00:12 -0800578static DEVICE_ATTR(modalias, 0444, modalias_show, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579static DEVICE_ATTR(online, 0644, online_show, online_store);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580static DEVICE_ATTR(availability, 0444, available_show, NULL);
581
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200582static struct attribute *io_subchannel_attrs[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 &dev_attr_chpids.attr,
584 &dev_attr_pimpampom.attr,
585 NULL,
586};
587
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200588static struct attribute_group io_subchannel_attr_group = {
589 .attrs = io_subchannel_attrs,
Cornelia Huck529192f2006-12-08 15:55:57 +0100590};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
592static struct attribute * ccwdev_attrs[] = {
593 &dev_attr_devtype.attr,
594 &dev_attr_cutype.attr,
Bastian Blankf1fc78a2005-10-30 15:00:12 -0800595 &dev_attr_modalias.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 &dev_attr_online.attr,
597 &dev_attr_cmb_enable.attr,
598 &dev_attr_availability.attr,
599 NULL,
600};
601
602static struct attribute_group ccwdev_attr_group = {
603 .attrs = ccwdev_attrs,
604};
605
Cornelia Huckf7e5d672007-05-10 15:45:43 +0200606static struct attribute_group *ccwdev_attr_groups[] = {
Cornelia Huckef995162007-04-27 16:01:39 +0200607 &ccwdev_attr_group,
608 NULL,
609};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
611/* this is a simple abstraction for device_register that sets the
612 * correct bus type and adds the bus specific files */
Cornelia Huck3c9da7b2006-10-27 12:39:33 +0200613static int ccw_device_register(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614{
615 struct device *dev = &cdev->dev;
616 int ret;
617
618 dev->bus = &ccw_bus_type;
619
620 if ((ret = device_add(dev)))
621 return ret;
622
623 set_bit(1, &cdev->private->registered);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 return ret;
625}
626
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700627struct match_data {
Cornelia Huck78964262006-10-11 15:31:38 +0200628 struct ccw_dev_id dev_id;
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700629 struct ccw_device * sibling;
630};
631
632static int
633match_devno(struct device * dev, void * data)
634{
Cornelia Huck78964262006-10-11 15:31:38 +0200635 struct match_data * d = data;
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700636 struct ccw_device * cdev;
637
638 cdev = to_ccwdev(dev);
639 if ((cdev->private->state == DEV_STATE_DISCONNECTED) &&
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100640 !ccw_device_is_orphan(cdev) &&
Cornelia Huck78964262006-10-11 15:31:38 +0200641 ccw_dev_id_is_equal(&cdev->private->dev_id, &d->dev_id) &&
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100642 (cdev != d->sibling))
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700643 return 1;
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700644 return 0;
645}
646
Cornelia Huck78964262006-10-11 15:31:38 +0200647static struct ccw_device * get_disc_ccwdev_by_dev_id(struct ccw_dev_id *dev_id,
648 struct ccw_device *sibling)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 struct device *dev;
Heiko Carstens292888c2006-08-30 14:33:35 +0200651 struct match_data data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
Cornelia Huck78964262006-10-11 15:31:38 +0200653 data.dev_id = *dev_id;
Heiko Carstens292888c2006-08-30 14:33:35 +0200654 data.sibling = sibling;
Cornelia Hucke5945b42005-10-11 08:28:59 -0700655 dev = bus_find_device(&ccw_bus_type, NULL, &data, match_devno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700657 return dev ? to_ccwdev(dev) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658}
659
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100660static int match_orphan(struct device *dev, void *data)
661{
662 struct ccw_dev_id *dev_id;
663 struct ccw_device *cdev;
664
665 dev_id = data;
666 cdev = to_ccwdev(dev);
667 return ccw_dev_id_is_equal(&cdev->private->dev_id, dev_id);
668}
669
670static struct ccw_device *
671get_orphaned_ccwdev_by_dev_id(struct channel_subsystem *css,
672 struct ccw_dev_id *dev_id)
673{
674 struct device *dev;
675
676 dev = device_find_child(&css->pseudo_subchannel->dev, dev_id,
677 match_orphan);
678
679 return dev ? to_ccwdev(dev) : NULL;
680}
681
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682static void
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100683ccw_device_add_changed(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684{
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100685 struct ccw_device_private *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 struct ccw_device *cdev;
687
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100688 priv = container_of(work, struct ccw_device_private, kick_work);
689 cdev = priv->cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 if (device_add(&cdev->dev)) {
691 put_device(&cdev->dev);
692 return;
693 }
694 set_bit(1, &cdev->private->registered);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695}
696
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100697void ccw_device_do_unreg_rereg(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698{
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100699 struct ccw_device_private *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 struct ccw_device *cdev;
701 struct subchannel *sch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100703 priv = container_of(work, struct ccw_device_private, kick_work);
704 cdev = priv->cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 sch = to_subchannel(cdev->dev.parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
Cornelia Huckef995162007-04-27 16:01:39 +0200707 ccw_device_unregister(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 PREPARE_WORK(&cdev->private->kick_work,
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100709 ccw_device_add_changed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 queue_work(ccw_device_work, &cdev->private->kick_work);
711}
712
713static void
714ccw_device_release(struct device *dev)
715{
716 struct ccw_device *cdev;
717
718 cdev = to_ccwdev(dev);
719 kfree(cdev->private);
720 kfree(cdev);
721}
722
Cornelia Huck7674da72006-12-08 15:54:21 +0100723static struct ccw_device * io_subchannel_allocate_dev(struct subchannel *sch)
724{
725 struct ccw_device *cdev;
726
727 cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
728 if (cdev) {
729 cdev->private = kzalloc(sizeof(struct ccw_device_private),
730 GFP_KERNEL | GFP_DMA);
731 if (cdev->private)
732 return cdev;
733 }
734 kfree(cdev);
735 return ERR_PTR(-ENOMEM);
736}
737
738static int io_subchannel_initialize_dev(struct subchannel *sch,
739 struct ccw_device *cdev)
740{
741 cdev->private->cdev = cdev;
742 atomic_set(&cdev->private->onoff, 0);
743 cdev->dev.parent = &sch->dev;
744 cdev->dev.release = ccw_device_release;
Heiko Carstens33583c32007-11-05 11:10:07 +0100745 INIT_WORK(&cdev->private->kick_work, NULL);
Cornelia Huckef995162007-04-27 16:01:39 +0200746 cdev->dev.groups = ccwdev_attr_groups;
Cornelia Huck7674da72006-12-08 15:54:21 +0100747 /* Do first half of device_register. */
748 device_initialize(&cdev->dev);
749 if (!get_device(&sch->dev)) {
750 if (cdev->dev.release)
751 cdev->dev.release(&cdev->dev);
752 return -ENODEV;
753 }
754 return 0;
755}
756
757static struct ccw_device * io_subchannel_create_ccwdev(struct subchannel *sch)
758{
759 struct ccw_device *cdev;
760 int ret;
761
762 cdev = io_subchannel_allocate_dev(sch);
763 if (!IS_ERR(cdev)) {
764 ret = io_subchannel_initialize_dev(sch, cdev);
765 if (ret) {
766 kfree(cdev);
767 cdev = ERR_PTR(ret);
768 }
769 }
770 return cdev;
771}
772
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100773static int io_subchannel_recog(struct ccw_device *, struct subchannel *);
774
775static void sch_attach_device(struct subchannel *sch,
776 struct ccw_device *cdev)
777{
Cornelia Huck82b7ac02007-04-27 16:01:36 +0200778 css_update_ssd_info(sch);
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100779 spin_lock_irq(sch->lock);
Cornelia Huckdb6a6422008-01-26 14:10:46 +0100780 sch_set_cdev(sch, cdev);
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100781 cdev->private->schid = sch->schid;
782 cdev->ccwlock = sch->lock;
Cornelia Huckc820de32008-07-14 09:58:45 +0200783 ccw_device_trigger_reprobe(cdev);
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100784 spin_unlock_irq(sch->lock);
785}
786
787static void sch_attach_disconnected_device(struct subchannel *sch,
788 struct ccw_device *cdev)
789{
790 struct subchannel *other_sch;
791 int ret;
792
793 other_sch = to_subchannel(get_device(cdev->dev.parent));
794 ret = device_move(&cdev->dev, &sch->dev);
795 if (ret) {
Michael Ernst139b83d2008-05-07 09:22:54 +0200796 CIO_MSG_EVENT(0, "Moving disconnected device 0.%x.%04x failed "
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100797 "(ret=%d)!\n", cdev->private->dev_id.ssid,
798 cdev->private->dev_id.devno, ret);
799 put_device(&other_sch->dev);
800 return;
801 }
Cornelia Huckdb6a6422008-01-26 14:10:46 +0100802 sch_set_cdev(other_sch, NULL);
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100803 /* No need to keep a subchannel without ccw device around. */
804 css_sch_device_unregister(other_sch);
805 put_device(&other_sch->dev);
806 sch_attach_device(sch, cdev);
807}
808
809static void sch_attach_orphaned_device(struct subchannel *sch,
810 struct ccw_device *cdev)
811{
812 int ret;
813
814 /* Try to move the ccw device to its new subchannel. */
815 ret = device_move(&cdev->dev, &sch->dev);
816 if (ret) {
817 CIO_MSG_EVENT(0, "Moving device 0.%x.%04x from orphanage "
818 "failed (ret=%d)!\n",
819 cdev->private->dev_id.ssid,
820 cdev->private->dev_id.devno, ret);
821 return;
822 }
823 sch_attach_device(sch, cdev);
824}
825
826static void sch_create_and_recog_new_device(struct subchannel *sch)
827{
828 struct ccw_device *cdev;
829
830 /* Need to allocate a new ccw device. */
831 cdev = io_subchannel_create_ccwdev(sch);
832 if (IS_ERR(cdev)) {
833 /* OK, we did everything we could... */
834 css_sch_device_unregister(sch);
835 return;
836 }
837 spin_lock_irq(sch->lock);
Cornelia Huckdb6a6422008-01-26 14:10:46 +0100838 sch_set_cdev(sch, cdev);
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100839 spin_unlock_irq(sch->lock);
840 /* Start recognition for the new ccw device. */
841 if (io_subchannel_recog(cdev, sch)) {
842 spin_lock_irq(sch->lock);
Cornelia Huckdb6a6422008-01-26 14:10:46 +0100843 sch_set_cdev(sch, NULL);
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100844 spin_unlock_irq(sch->lock);
845 if (cdev->dev.release)
846 cdev->dev.release(&cdev->dev);
847 css_sch_device_unregister(sch);
848 }
849}
850
851
852void ccw_device_move_to_orphanage(struct work_struct *work)
853{
854 struct ccw_device_private *priv;
855 struct ccw_device *cdev;
856 struct ccw_device *replacing_cdev;
857 struct subchannel *sch;
858 int ret;
859 struct channel_subsystem *css;
860 struct ccw_dev_id dev_id;
861
862 priv = container_of(work, struct ccw_device_private, kick_work);
863 cdev = priv->cdev;
864 sch = to_subchannel(cdev->dev.parent);
865 css = to_css(sch->dev.parent);
866 dev_id.devno = sch->schib.pmcw.dev;
867 dev_id.ssid = sch->schid.ssid;
868
869 /*
870 * Move the orphaned ccw device to the orphanage so the replacing
871 * ccw device can take its place on the subchannel.
872 */
873 ret = device_move(&cdev->dev, &css->pseudo_subchannel->dev);
874 if (ret) {
875 CIO_MSG_EVENT(0, "Moving device 0.%x.%04x to orphanage failed "
876 "(ret=%d)!\n", cdev->private->dev_id.ssid,
877 cdev->private->dev_id.devno, ret);
878 return;
879 }
880 cdev->ccwlock = css->pseudo_subchannel->lock;
881 /*
882 * Search for the replacing ccw device
883 * - among the disconnected devices
884 * - in the orphanage
885 */
886 replacing_cdev = get_disc_ccwdev_by_dev_id(&dev_id, cdev);
887 if (replacing_cdev) {
888 sch_attach_disconnected_device(sch, replacing_cdev);
889 return;
890 }
891 replacing_cdev = get_orphaned_ccwdev_by_dev_id(css, &dev_id);
892 if (replacing_cdev) {
893 sch_attach_orphaned_device(sch, replacing_cdev);
894 return;
895 }
896 sch_create_and_recog_new_device(sch);
897}
898
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899/*
900 * Register recognized device.
901 */
902static void
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100903io_subchannel_register(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904{
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100905 struct ccw_device_private *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 struct ccw_device *cdev;
907 struct subchannel *sch;
908 int ret;
909 unsigned long flags;
910
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100911 priv = container_of(work, struct ccw_device_private, kick_work);
912 cdev = priv->cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 sch = to_subchannel(cdev->dev.parent);
Cornelia Huck82b7ac02007-04-27 16:01:36 +0200914 css_update_ssd_info(sch);
Cornelia Huck47af5512006-12-04 15:41:07 +0100915 /*
916 * io_subchannel_register() will also be called after device
917 * recognition has been done for a boxed device (which will already
918 * be registered). We need to reprobe since we may now have sense id
919 * information.
920 */
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700921 if (klist_node_attached(&cdev->dev.knode_parent)) {
Cornelia Huck47af5512006-12-04 15:41:07 +0100922 if (!cdev->drv) {
923 ret = device_reprobe(&cdev->dev);
924 if (ret)
925 /* We can't do much here. */
Michael Ernst139b83d2008-05-07 09:22:54 +0200926 CIO_MSG_EVENT(0, "device_reprobe() returned"
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200927 " %d for 0.%x.%04x\n", ret,
928 cdev->private->dev_id.ssid,
929 cdev->private->dev_id.devno);
Cornelia Huck47af5512006-12-04 15:41:07 +0100930 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 goto out;
932 }
Cornelia Huckfa1a8c22007-04-26 00:12:03 -0700933 /*
934 * Now we know this subchannel will stay, we can throw
935 * our delayed uevent.
936 */
937 sch->dev.uevent_suppress = 0;
938 kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 /* make it known to the system */
940 ret = ccw_device_register(cdev);
941 if (ret) {
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200942 CIO_MSG_EVENT(0, "Could not register ccw dev 0.%x.%04x: %d\n",
943 cdev->private->dev_id.ssid,
944 cdev->private->dev_id.devno, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 put_device(&cdev->dev);
Cornelia Huck2ec22982006-12-08 15:54:26 +0100946 spin_lock_irqsave(sch->lock, flags);
Cornelia Huckdb6a6422008-01-26 14:10:46 +0100947 sch_set_cdev(sch, NULL);
Cornelia Huck2ec22982006-12-08 15:54:26 +0100948 spin_unlock_irqrestore(sch->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 kfree (cdev->private);
950 kfree (cdev);
951 put_device(&sch->dev);
952 if (atomic_dec_and_test(&ccw_device_init_count))
953 wake_up(&ccw_device_init_wq);
954 return;
955 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 put_device(&cdev->dev);
957out:
958 cdev->private->flags.recog_done = 1;
959 put_device(&sch->dev);
960 wake_up(&cdev->private->wait_q);
961 if (atomic_dec_and_test(&ccw_device_init_count))
962 wake_up(&ccw_device_init_wq);
963}
964
Cornelia Huck3f4cf6e2007-10-12 16:11:26 +0200965static void ccw_device_call_sch_unregister(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966{
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100967 struct ccw_device_private *priv;
968 struct ccw_device *cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 struct subchannel *sch;
970
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100971 priv = container_of(work, struct ccw_device_private, kick_work);
972 cdev = priv->cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 sch = to_subchannel(cdev->dev.parent);
Cornelia Huck6ab48792006-07-12 16:39:50 +0200974 css_sch_device_unregister(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 /* Reset intparm to zeroes. */
976 sch->schib.pmcw.intparm = 0;
977 cio_modify(sch);
978 put_device(&cdev->dev);
979 put_device(&sch->dev);
980}
981
982/*
983 * subchannel recognition done. Called from the state machine.
984 */
985void
986io_subchannel_recog_done(struct ccw_device *cdev)
987{
988 struct subchannel *sch;
989
990 if (css_init_done == 0) {
991 cdev->private->flags.recog_done = 1;
992 return;
993 }
994 switch (cdev->private->state) {
995 case DEV_STATE_NOT_OPER:
996 cdev->private->flags.recog_done = 1;
997 /* Remove device found not operational. */
998 if (!get_device(&cdev->dev))
999 break;
1000 sch = to_subchannel(cdev->dev.parent);
1001 PREPARE_WORK(&cdev->private->kick_work,
Martin Schwidefskyc1637532006-12-08 15:53:57 +01001002 ccw_device_call_sch_unregister);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 queue_work(slow_path_wq, &cdev->private->kick_work);
1004 if (atomic_dec_and_test(&ccw_device_init_count))
1005 wake_up(&ccw_device_init_wq);
1006 break;
1007 case DEV_STATE_BOXED:
1008 /* Device did not respond in time. */
1009 case DEV_STATE_OFFLINE:
1010 /*
1011 * We can't register the device in interrupt context so
1012 * we schedule a work item.
1013 */
1014 if (!get_device(&cdev->dev))
1015 break;
1016 PREPARE_WORK(&cdev->private->kick_work,
Martin Schwidefskyc1637532006-12-08 15:53:57 +01001017 io_subchannel_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 queue_work(slow_path_wq, &cdev->private->kick_work);
1019 break;
1020 }
1021}
1022
1023static int
1024io_subchannel_recog(struct ccw_device *cdev, struct subchannel *sch)
1025{
1026 int rc;
1027 struct ccw_device_private *priv;
1028
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001029 sch_set_cdev(sch, cdev);
Cornelia Huck2ec22982006-12-08 15:54:26 +01001030 cdev->ccwlock = sch->lock;
Cornelia Huckfb6958a2006-01-06 00:19:25 -08001031
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 /* Init private data. */
1033 priv = cdev->private;
Cornelia Huck78964262006-10-11 15:31:38 +02001034 priv->dev_id.devno = sch->schib.pmcw.dev;
1035 priv->dev_id.ssid = sch->schid.ssid;
1036 priv->schid = sch->schid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 priv->state = DEV_STATE_NOT_OPER;
1038 INIT_LIST_HEAD(&priv->cmb_list);
1039 init_waitqueue_head(&priv->wait_q);
1040 init_timer(&priv->timer);
1041
1042 /* Set an initial name for the device. */
Cornelia Huckfb6958a2006-01-06 00:19:25 -08001043 snprintf (cdev->dev.bus_id, BUS_ID_SIZE, "0.%x.%04x",
1044 sch->schid.ssid, sch->schib.pmcw.dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045
1046 /* Increase counter of devices currently in recognition. */
1047 atomic_inc(&ccw_device_init_count);
1048
1049 /* Start async. device sensing. */
Cornelia Huck2ec22982006-12-08 15:54:26 +01001050 spin_lock_irq(sch->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 rc = ccw_device_recognition(cdev);
Cornelia Huck2ec22982006-12-08 15:54:26 +01001052 spin_unlock_irq(sch->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 if (rc) {
1054 if (atomic_dec_and_test(&ccw_device_init_count))
1055 wake_up(&ccw_device_init_wq);
1056 }
1057 return rc;
1058}
1059
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +01001060static void ccw_device_move_to_sch(struct work_struct *work)
1061{
1062 struct ccw_device_private *priv;
1063 int rc;
1064 struct subchannel *sch;
1065 struct ccw_device *cdev;
1066 struct subchannel *former_parent;
1067
1068 priv = container_of(work, struct ccw_device_private, kick_work);
1069 sch = priv->sch;
1070 cdev = priv->cdev;
1071 former_parent = ccw_device_is_orphan(cdev) ?
1072 NULL : to_subchannel(get_device(cdev->dev.parent));
1073 mutex_lock(&sch->reg_mutex);
1074 /* Try to move the ccw device to its new subchannel. */
1075 rc = device_move(&cdev->dev, &sch->dev);
1076 mutex_unlock(&sch->reg_mutex);
1077 if (rc) {
Michael Ernst139b83d2008-05-07 09:22:54 +02001078 CIO_MSG_EVENT(0, "Moving device 0.%x.%04x to subchannel "
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +01001079 "0.%x.%04x failed (ret=%d)!\n",
1080 cdev->private->dev_id.ssid,
1081 cdev->private->dev_id.devno, sch->schid.ssid,
1082 sch->schid.sch_no, rc);
1083 css_sch_device_unregister(sch);
1084 goto out;
1085 }
1086 if (former_parent) {
1087 spin_lock_irq(former_parent->lock);
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001088 sch_set_cdev(former_parent, NULL);
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +01001089 spin_unlock_irq(former_parent->lock);
1090 css_sch_device_unregister(former_parent);
1091 /* Reset intparm to zeroes. */
1092 former_parent->schib.pmcw.intparm = 0;
1093 cio_modify(former_parent);
1094 }
1095 sch_attach_device(sch, cdev);
1096out:
1097 if (former_parent)
1098 put_device(&former_parent->dev);
1099 put_device(&cdev->dev);
1100}
1101
Cornelia Huck602b20f2008-01-26 14:10:39 +01001102static void io_subchannel_irq(struct subchannel *sch)
1103{
1104 struct ccw_device *cdev;
1105
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001106 cdev = sch_get_cdev(sch);
Cornelia Huck602b20f2008-01-26 14:10:39 +01001107
1108 CIO_TRACE_EVENT(3, "IRQ");
1109 CIO_TRACE_EVENT(3, sch->dev.bus_id);
1110 if (cdev)
1111 dev_fsm_event(cdev, DEV_EVENT_INTERRUPT);
1112}
1113
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001114static void io_subchannel_init_fields(struct subchannel *sch)
1115{
1116 if (cio_is_console(sch->schid))
1117 sch->opm = 0xff;
1118 else
1119 sch->opm = chp_get_sch_opm(sch);
1120 sch->lpm = sch->schib.pmcw.pam & sch->opm;
Cornelia Huck3a3fc292008-07-14 09:58:58 +02001121 sch->isc = cio_is_console(sch->schid) ? CONSOLE_ISC : IO_SCH_ISC;
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001122
1123 CIO_MSG_EVENT(6, "Detected device %04x on subchannel 0.%x.%04X"
1124 " - PIM = %02X, PAM = %02X, POM = %02X\n",
1125 sch->schib.pmcw.dev, sch->schid.ssid,
1126 sch->schid.sch_no, sch->schib.pmcw.pim,
1127 sch->schib.pmcw.pam, sch->schib.pmcw.pom);
1128 /* Initially set up some fields in the pmcw. */
1129 sch->schib.pmcw.ena = 0;
1130 sch->schib.pmcw.csense = 1; /* concurrent sense */
1131 if ((sch->lpm & (sch->lpm - 1)) != 0)
1132 sch->schib.pmcw.mp = 1; /* multipath mode */
1133 /* clean up possible residual cmf stuff */
1134 sch->schib.pmcw.mme = 0;
1135 sch->schib.pmcw.mbfc = 0;
1136 sch->schib.pmcw.mbi = 0;
1137 sch->schib.mba = 0;
1138}
1139
1140static int io_subchannel_probe(struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 struct ccw_device *cdev;
1143 int rc;
1144 unsigned long flags;
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +01001145 struct ccw_dev_id dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001147 cdev = sch_get_cdev(sch);
1148 if (cdev) {
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001149 rc = sysfs_create_group(&sch->dev.kobj,
1150 &io_subchannel_attr_group);
1151 if (rc)
1152 CIO_MSG_EVENT(0, "Failed to create io subchannel "
1153 "attributes for subchannel "
1154 "0.%x.%04x (rc=%d)\n",
1155 sch->schid.ssid, sch->schid.sch_no, rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 /*
1157 * This subchannel already has an associated ccw_device.
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001158 * Throw the delayed uevent for the subchannel, register
1159 * the ccw_device and exit. This happens for all early
1160 * devices, e.g. the console.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 */
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001162 sch->dev.uevent_suppress = 0;
1163 kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
Cornelia Hucke1031782007-10-12 16:11:23 +02001164 cdev->dev.groups = ccwdev_attr_groups;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 device_initialize(&cdev->dev);
1166 ccw_device_register(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 /*
1168 * Check if the device is already online. If it is
1169 * the reference count needs to be corrected
1170 * (see ccw_device_online and css_init_done for the
1171 * ugly details).
1172 */
1173 if (cdev->private->state != DEV_STATE_NOT_OPER &&
1174 cdev->private->state != DEV_STATE_OFFLINE &&
1175 cdev->private->state != DEV_STATE_BOXED)
1176 get_device(&cdev->dev);
1177 return 0;
1178 }
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001179 io_subchannel_init_fields(sch);
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +01001180 /*
1181 * First check if a fitting device may be found amongst the
1182 * disconnected devices or in the orphanage.
1183 */
1184 dev_id.devno = sch->schib.pmcw.dev;
1185 dev_id.ssid = sch->schid.ssid;
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001186 rc = sysfs_create_group(&sch->dev.kobj,
1187 &io_subchannel_attr_group);
1188 if (rc)
1189 return rc;
Cornelia Huckcd6b4f22008-01-26 14:10:43 +01001190 /* Allocate I/O subchannel private data. */
1191 sch->private = kzalloc(sizeof(struct io_subchannel_private),
1192 GFP_KERNEL | GFP_DMA);
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001193 if (!sch->private) {
1194 rc = -ENOMEM;
1195 goto out_err;
1196 }
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +01001197 cdev = get_disc_ccwdev_by_dev_id(&dev_id, NULL);
1198 if (!cdev)
1199 cdev = get_orphaned_ccwdev_by_dev_id(to_css(sch->dev.parent),
1200 &dev_id);
1201 if (cdev) {
1202 /*
1203 * Schedule moving the device until when we have a registered
1204 * subchannel to move to and succeed the probe. We can
1205 * unregister later again, when the probe is through.
1206 */
1207 cdev->private->sch = sch;
1208 PREPARE_WORK(&cdev->private->kick_work,
1209 ccw_device_move_to_sch);
1210 queue_work(slow_path_wq, &cdev->private->kick_work);
1211 return 0;
1212 }
Cornelia Huck7674da72006-12-08 15:54:21 +01001213 cdev = io_subchannel_create_ccwdev(sch);
Cornelia Huckcd6b4f22008-01-26 14:10:43 +01001214 if (IS_ERR(cdev)) {
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001215 rc = PTR_ERR(cdev);
1216 goto out_err;
Cornelia Huckcd6b4f22008-01-26 14:10:43 +01001217 }
Cornelia Huck8bbace72006-01-11 10:56:22 +01001218 rc = io_subchannel_recog(cdev, sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 if (rc) {
Cornelia Huck2ec22982006-12-08 15:54:26 +01001220 spin_lock_irqsave(sch->lock, flags);
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001221 sch_set_cdev(sch, NULL);
Cornelia Huck2ec22982006-12-08 15:54:26 +01001222 spin_unlock_irqrestore(sch->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 if (cdev->dev.release)
1224 cdev->dev.release(&cdev->dev);
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001225 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 }
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001227 return 0;
1228out_err:
1229 kfree(sch->private);
1230 sysfs_remove_group(&sch->dev.kobj, &io_subchannel_attr_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 return rc;
1232}
1233
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234static int
Cornelia Huck8bbace72006-01-11 10:56:22 +01001235io_subchannel_remove (struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236{
1237 struct ccw_device *cdev;
1238 unsigned long flags;
1239
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001240 cdev = sch_get_cdev(sch);
1241 if (!cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 /* Set ccw device to not operational and drop reference. */
1244 spin_lock_irqsave(cdev->ccwlock, flags);
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001245 sch_set_cdev(sch, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 cdev->private->state = DEV_STATE_NOT_OPER;
1247 spin_unlock_irqrestore(cdev->ccwlock, flags);
Cornelia Huckef995162007-04-27 16:01:39 +02001248 ccw_device_unregister(cdev);
1249 put_device(&cdev->dev);
Cornelia Huckcd6b4f22008-01-26 14:10:43 +01001250 kfree(sch->private);
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001251 sysfs_remove_group(&sch->dev.kobj, &io_subchannel_attr_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 return 0;
1253}
1254
Cornelia Huck602b20f2008-01-26 14:10:39 +01001255static int io_subchannel_notify(struct subchannel *sch, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256{
1257 struct ccw_device *cdev;
1258
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001259 cdev = sch_get_cdev(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 if (!cdev)
1261 return 0;
Cornelia Huckc820de32008-07-14 09:58:45 +02001262 return ccw_device_notify(cdev, event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263}
1264
Cornelia Huck602b20f2008-01-26 14:10:39 +01001265static void io_subchannel_verify(struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266{
1267 struct ccw_device *cdev;
1268
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001269 cdev = sch_get_cdev(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 if (cdev)
1271 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1272}
1273
Cornelia Huckc820de32008-07-14 09:58:45 +02001274static int check_for_io_on_path(struct subchannel *sch, int mask)
1275{
1276 int cc;
1277
1278 cc = stsch(sch->schid, &sch->schib);
1279 if (cc)
1280 return 0;
Peter Oberparleiter23d805b2008-07-14 09:58:50 +02001281 if (scsw_actl(&sch->schib.scsw) && sch->schib.pmcw.lpum == mask)
Cornelia Huckc820de32008-07-14 09:58:45 +02001282 return 1;
1283 return 0;
1284}
1285
1286static void terminate_internal_io(struct subchannel *sch,
1287 struct ccw_device *cdev)
1288{
1289 if (cio_clear(sch)) {
1290 /* Recheck device in case clear failed. */
1291 sch->lpm = 0;
1292 if (cdev->online)
1293 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1294 else
1295 css_schedule_eval(sch->schid);
1296 return;
1297 }
1298 cdev->private->state = DEV_STATE_CLEAR_VERIFY;
1299 /* Request retry of internal operation. */
1300 cdev->private->flags.intretry = 1;
1301 /* Call handler. */
1302 if (cdev->handler)
1303 cdev->handler(cdev, cdev->private->intparm,
1304 ERR_PTR(-EIO));
1305}
1306
1307static void io_subchannel_terminate_path(struct subchannel *sch, u8 mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308{
1309 struct ccw_device *cdev;
1310
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001311 cdev = sch_get_cdev(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 if (!cdev)
1313 return;
Cornelia Huckc820de32008-07-14 09:58:45 +02001314 if (check_for_io_on_path(sch, mask)) {
1315 if (cdev->private->state == DEV_STATE_ONLINE)
1316 ccw_device_kill_io(cdev);
1317 else {
1318 terminate_internal_io(sch, cdev);
1319 /* Re-start path verification. */
1320 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1321 }
1322 } else
1323 /* trigger path verification. */
1324 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1325
1326}
1327
Cornelia Huck99611f82008-07-14 09:59:02 +02001328static int io_subchannel_chp_event(struct subchannel *sch,
1329 struct chp_link *link, int event)
Cornelia Huckc820de32008-07-14 09:58:45 +02001330{
1331 int mask;
Cornelia Huckc820de32008-07-14 09:58:45 +02001332
Cornelia Huck99611f82008-07-14 09:59:02 +02001333 mask = chp_ssd_get_mask(&sch->ssd_info, link);
Cornelia Huckc820de32008-07-14 09:58:45 +02001334 if (!mask)
1335 return 0;
1336 switch (event) {
1337 case CHP_VARY_OFF:
1338 sch->opm &= ~mask;
1339 sch->lpm &= ~mask;
1340 io_subchannel_terminate_path(sch, mask);
1341 break;
1342 case CHP_VARY_ON:
1343 sch->opm |= mask;
1344 sch->lpm |= mask;
1345 io_subchannel_verify(sch);
1346 break;
1347 case CHP_OFFLINE:
1348 if (stsch(sch->schid, &sch->schib))
1349 return -ENXIO;
1350 if (!css_sch_is_valid(&sch->schib))
1351 return -ENODEV;
1352 io_subchannel_terminate_path(sch, mask);
1353 break;
1354 case CHP_ONLINE:
1355 if (stsch(sch->schid, &sch->schib))
1356 return -ENXIO;
1357 sch->lpm |= mask & sch->opm;
1358 io_subchannel_verify(sch);
1359 break;
1360 }
1361 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362}
1363
1364static void
Cornelia Huck8bbace72006-01-11 10:56:22 +01001365io_subchannel_shutdown(struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 struct ccw_device *cdev;
1368 int ret;
1369
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001370 cdev = sch_get_cdev(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371
Cornelia Hucka8237fc2006-01-06 00:19:21 -08001372 if (cio_is_console(sch->schid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 return;
1374 if (!sch->schib.pmcw.ena)
1375 /* Nothing to do. */
1376 return;
1377 ret = cio_disable_subchannel(sch);
1378 if (ret != -EBUSY)
1379 /* Subchannel is disabled, we're done. */
1380 return;
1381 cdev->private->state = DEV_STATE_QUIESCE;
1382 if (cdev->handler)
1383 cdev->handler(cdev, cdev->private->intparm,
1384 ERR_PTR(-EIO));
1385 ret = ccw_device_cancel_halt_clear(cdev);
1386 if (ret == -EBUSY) {
1387 ccw_device_set_timeout(cdev, HZ/10);
1388 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
1389 }
1390 cio_disable_subchannel(sch);
1391}
1392
Cornelia Huckc820de32008-07-14 09:58:45 +02001393static int io_subchannel_get_status(struct subchannel *sch)
1394{
1395 struct schib schib;
1396
1397 if (stsch(sch->schid, &schib) || !schib.pmcw.dnv)
1398 return CIO_GONE;
1399 if (sch->schib.pmcw.dnv && (schib.pmcw.dev != sch->schib.pmcw.dev))
1400 return CIO_REVALIDATE;
1401 if (!sch->lpm)
1402 return CIO_NO_PATH;
1403 return CIO_OPER;
1404}
1405
1406static int device_is_disconnected(struct ccw_device *cdev)
1407{
1408 if (!cdev)
1409 return 0;
1410 return (cdev->private->state == DEV_STATE_DISCONNECTED ||
1411 cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID);
1412}
1413
1414static int recovery_check(struct device *dev, void *data)
1415{
1416 struct ccw_device *cdev = to_ccwdev(dev);
1417 int *redo = data;
1418
1419 spin_lock_irq(cdev->ccwlock);
1420 switch (cdev->private->state) {
1421 case DEV_STATE_DISCONNECTED:
1422 CIO_MSG_EVENT(3, "recovery: trigger 0.%x.%04x\n",
1423 cdev->private->dev_id.ssid,
1424 cdev->private->dev_id.devno);
1425 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1426 *redo = 1;
1427 break;
1428 case DEV_STATE_DISCONNECTED_SENSE_ID:
1429 *redo = 1;
1430 break;
1431 }
1432 spin_unlock_irq(cdev->ccwlock);
1433
1434 return 0;
1435}
1436
1437static void recovery_work_func(struct work_struct *unused)
1438{
1439 int redo = 0;
1440
1441 bus_for_each_dev(&ccw_bus_type, NULL, &redo, recovery_check);
1442 if (redo) {
1443 spin_lock_irq(&recovery_lock);
1444 if (!timer_pending(&recovery_timer)) {
1445 if (recovery_phase < ARRAY_SIZE(recovery_delay) - 1)
1446 recovery_phase++;
1447 mod_timer(&recovery_timer, jiffies +
1448 recovery_delay[recovery_phase] * HZ);
1449 }
1450 spin_unlock_irq(&recovery_lock);
1451 } else
1452 CIO_MSG_EVENT(4, "recovery: end\n");
1453}
1454
1455static DECLARE_WORK(recovery_work, recovery_work_func);
1456
1457static void recovery_func(unsigned long data)
1458{
1459 /*
1460 * We can't do our recovery in softirq context and it's not
1461 * performance critical, so we schedule it.
1462 */
1463 schedule_work(&recovery_work);
1464}
1465
1466static void ccw_device_schedule_recovery(void)
1467{
1468 unsigned long flags;
1469
1470 CIO_MSG_EVENT(4, "recovery: schedule\n");
1471 spin_lock_irqsave(&recovery_lock, flags);
1472 if (!timer_pending(&recovery_timer) || (recovery_phase != 0)) {
1473 recovery_phase = 0;
1474 mod_timer(&recovery_timer, jiffies + recovery_delay[0] * HZ);
1475 }
1476 spin_unlock_irqrestore(&recovery_lock, flags);
1477}
1478
1479static void device_set_disconnected(struct ccw_device *cdev)
1480{
1481 if (!cdev)
1482 return;
1483 ccw_device_set_timeout(cdev, 0);
1484 cdev->private->flags.fake_irb = 0;
1485 cdev->private->state = DEV_STATE_DISCONNECTED;
1486 if (cdev->online)
1487 ccw_device_schedule_recovery();
1488}
1489
Peter Oberparleiter91c36912008-08-21 19:46:39 +02001490void ccw_device_set_notoper(struct ccw_device *cdev)
1491{
1492 struct subchannel *sch = to_subchannel(cdev->dev.parent);
1493
1494 CIO_TRACE_EVENT(2, "notoper");
1495 CIO_TRACE_EVENT(2, sch->dev.bus_id);
1496 ccw_device_set_timeout(cdev, 0);
1497 cio_disable_subchannel(sch);
1498 cdev->private->state = DEV_STATE_NOT_OPER;
1499}
1500
Cornelia Huckc820de32008-07-14 09:58:45 +02001501static int io_subchannel_sch_event(struct subchannel *sch, int slow)
1502{
1503 int event, ret, disc;
1504 unsigned long flags;
Peter Oberparleiter91c36912008-08-21 19:46:39 +02001505 enum { NONE, UNREGISTER, UNREGISTER_PROBE, REPROBE, DISC } action;
Cornelia Huckc820de32008-07-14 09:58:45 +02001506 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:
Cornelia Huckc820de32008-07-14 09:58:45 +02001540 /* Ask driver what to do with device. */
Peter Oberparleiter91c36912008-08-21 19:46:39 +02001541 if (io_subchannel_notify(sch, event))
1542 action = DISC;
1543 else
1544 action = UNREGISTER;
Cornelia Huckc820de32008-07-14 09:58:45 +02001545 break;
1546 case CIO_REVALIDATE:
1547 /* Device will be removed, so no notify necessary. */
1548 if (disc)
1549 /* Reprobe because immediate unregister might block. */
1550 action = REPROBE;
1551 else
1552 action = UNREGISTER_PROBE;
1553 break;
1554 case CIO_OPER:
1555 if (disc)
1556 /* Get device operational again. */
1557 action = REPROBE;
1558 break;
1559 }
1560 /* Perform action. */
1561 ret = 0;
1562 switch (action) {
1563 case UNREGISTER:
1564 case UNREGISTER_PROBE:
Peter Oberparleiter91c36912008-08-21 19:46:39 +02001565 ccw_device_set_notoper(cdev);
Cornelia Huckc820de32008-07-14 09:58:45 +02001566 /* Unregister device (will use subchannel lock). */
1567 spin_unlock_irqrestore(sch->lock, flags);
1568 css_sch_device_unregister(sch);
1569 spin_lock_irqsave(sch->lock, flags);
1570
1571 /* Reset intparm to zeroes. */
1572 sch->schib.pmcw.intparm = 0;
1573 cio_modify(sch);
1574 break;
1575 case REPROBE:
1576 ccw_device_trigger_reprobe(cdev);
1577 break;
Peter Oberparleiter91c36912008-08-21 19:46:39 +02001578 case DISC:
1579 device_set_disconnected(cdev);
1580 break;
Cornelia Huckc820de32008-07-14 09:58:45 +02001581 default:
1582 break;
1583 }
1584 spin_unlock_irqrestore(sch->lock, flags);
1585 /* Probe if necessary. */
1586 if (action == UNREGISTER_PROBE)
1587 ret = css_probe_device(sch->schid);
1588
1589 return ret;
1590}
1591
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592#ifdef CONFIG_CCW_CONSOLE
1593static struct ccw_device console_cdev;
1594static struct ccw_device_private console_private;
1595static int console_cdev_in_use;
1596
Cornelia Huck2ec22982006-12-08 15:54:26 +01001597static DEFINE_SPINLOCK(ccw_console_lock);
1598
1599spinlock_t * cio_get_console_lock(void)
1600{
1601 return &ccw_console_lock;
1602}
1603
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001604static int ccw_device_console_enable(struct ccw_device *cdev,
1605 struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606{
1607 int rc;
1608
Cornelia Huckcd6b4f22008-01-26 14:10:43 +01001609 /* Attach subchannel private data. */
1610 sch->private = cio_get_console_priv();
1611 memset(sch->private, 0, sizeof(struct io_subchannel_private));
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001612 io_subchannel_init_fields(sch);
1613 sch->driver = &io_subchannel_driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 /* Initialize the ccw_device structure. */
Heiko Carstens292888c2006-08-30 14:33:35 +02001615 cdev->dev.parent= &sch->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 rc = io_subchannel_recog(cdev, sch);
1617 if (rc)
1618 return rc;
1619
1620 /* Now wait for the async. recognition to come to an end. */
1621 spin_lock_irq(cdev->ccwlock);
1622 while (!dev_fsm_final_state(cdev))
1623 wait_cons_dev();
1624 rc = -EIO;
1625 if (cdev->private->state != DEV_STATE_OFFLINE)
1626 goto out_unlock;
1627 ccw_device_online(cdev);
1628 while (!dev_fsm_final_state(cdev))
1629 wait_cons_dev();
1630 if (cdev->private->state != DEV_STATE_ONLINE)
1631 goto out_unlock;
1632 rc = 0;
1633out_unlock:
1634 spin_unlock_irq(cdev->ccwlock);
1635 return 0;
1636}
1637
1638struct ccw_device *
1639ccw_device_probe_console(void)
1640{
1641 struct subchannel *sch;
1642 int ret;
1643
1644 if (xchg(&console_cdev_in_use, 1) != 0)
Peter Oberparleiter600b5d12006-02-01 03:06:35 -08001645 return ERR_PTR(-EBUSY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 sch = cio_probe_console();
1647 if (IS_ERR(sch)) {
1648 console_cdev_in_use = 0;
1649 return (void *) sch;
1650 }
1651 memset(&console_cdev, 0, sizeof(struct ccw_device));
1652 memset(&console_private, 0, sizeof(struct ccw_device_private));
1653 console_cdev.private = &console_private;
Martin Schwidefskyc1637532006-12-08 15:53:57 +01001654 console_private.cdev = &console_cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 ret = ccw_device_console_enable(&console_cdev, sch);
1656 if (ret) {
1657 cio_release_console();
1658 console_cdev_in_use = 0;
1659 return ERR_PTR(ret);
1660 }
1661 console_cdev.online = 1;
1662 return &console_cdev;
1663}
1664#endif
1665
1666/*
1667 * get ccw_device matching the busid, but only if owned by cdrv
1668 */
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001669static int
1670__ccwdev_check_busid(struct device *dev, void *id)
1671{
1672 char *bus_id;
1673
Cornelia Huck12975ae2006-10-11 15:31:47 +02001674 bus_id = id;
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001675
1676 return (strncmp(bus_id, dev->bus_id, BUS_ID_SIZE) == 0);
1677}
1678
1679
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +02001680/**
1681 * get_ccwdev_by_busid() - obtain device from a bus id
1682 * @cdrv: driver the device is owned by
1683 * @bus_id: bus id of the device to be searched
1684 *
1685 * This function searches all devices owned by @cdrv for a device with a bus
1686 * id matching @bus_id.
1687 * Returns:
1688 * If a match is found, its reference count of the found device is increased
1689 * and it is returned; else %NULL is returned.
1690 */
1691struct ccw_device *get_ccwdev_by_busid(struct ccw_driver *cdrv,
1692 const char *bus_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693{
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001694 struct device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 struct device_driver *drv;
1696
1697 drv = get_driver(&cdrv->driver);
1698 if (!drv)
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001699 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001701 dev = driver_find_device(drv, NULL, (void *)bus_id,
1702 __ccwdev_check_busid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 put_driver(drv);
1704
Heiko Carstensd2c993d2006-07-12 16:41:55 +02001705 return dev ? to_ccwdev(dev) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706}
1707
1708/************************** device driver handling ************************/
1709
1710/* This is the implementation of the ccw_driver class. The probe, remove
1711 * and release methods are initially very similar to the device_driver
1712 * implementations, with the difference that they have ccw_device
1713 * arguments.
1714 *
1715 * A ccw driver also contains the information that is needed for
1716 * device matching.
1717 */
1718static int
1719ccw_device_probe (struct device *dev)
1720{
1721 struct ccw_device *cdev = to_ccwdev(dev);
1722 struct ccw_driver *cdrv = to_ccwdrv(dev->driver);
1723 int ret;
1724
1725 cdev->drv = cdrv; /* to let the driver call _set_online */
1726
1727 ret = cdrv->probe ? cdrv->probe(cdev) : -ENODEV;
1728
1729 if (ret) {
Heiko Carstensd2c993d2006-07-12 16:41:55 +02001730 cdev->drv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 return ret;
1732 }
1733
1734 return 0;
1735}
1736
1737static int
1738ccw_device_remove (struct device *dev)
1739{
1740 struct ccw_device *cdev = to_ccwdev(dev);
1741 struct ccw_driver *cdrv = cdev->drv;
1742 int ret;
1743
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 if (cdrv->remove)
1745 cdrv->remove(cdev);
1746 if (cdev->online) {
1747 cdev->online = 0;
1748 spin_lock_irq(cdev->ccwlock);
1749 ret = ccw_device_offline(cdev);
1750 spin_unlock_irq(cdev->ccwlock);
1751 if (ret == 0)
1752 wait_event(cdev->private->wait_q,
1753 dev_fsm_final_state(cdev));
1754 else
Michael Ernst139b83d2008-05-07 09:22:54 +02001755 CIO_MSG_EVENT(0, "ccw_device_offline returned %d, "
Cornelia Hucke556bbb2007-07-27 12:29:19 +02001756 "device 0.%x.%04x\n",
1757 ret, cdev->private->dev_id.ssid,
1758 cdev->private->dev_id.devno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 }
1760 ccw_device_set_timeout(cdev, 0);
Heiko Carstensd2c993d2006-07-12 16:41:55 +02001761 cdev->drv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 return 0;
1763}
1764
Cornelia Huck958974f2007-10-12 16:11:21 +02001765static void ccw_device_shutdown(struct device *dev)
1766{
1767 struct ccw_device *cdev;
1768
1769 cdev = to_ccwdev(dev);
1770 if (cdev->drv && cdev->drv->shutdown)
1771 cdev->drv->shutdown(cdev);
Cornelia Huck1842f2b2007-10-12 16:11:22 +02001772 disable_cmf(cdev);
Cornelia Huck958974f2007-10-12 16:11:21 +02001773}
1774
Cornelia Huck8bbace72006-01-11 10:56:22 +01001775struct bus_type ccw_bus_type = {
1776 .name = "ccw",
1777 .match = ccw_bus_match,
1778 .uevent = ccw_uevent,
1779 .probe = ccw_device_probe,
1780 .remove = ccw_device_remove,
Cornelia Huck958974f2007-10-12 16:11:21 +02001781 .shutdown = ccw_device_shutdown,
Cornelia Huck8bbace72006-01-11 10:56:22 +01001782};
1783
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +02001784/**
1785 * ccw_driver_register() - register a ccw driver
1786 * @cdriver: driver to be registered
1787 *
1788 * This function is mainly a wrapper around driver_register().
1789 * Returns:
1790 * %0 on success and a negative error value on failure.
1791 */
1792int ccw_driver_register(struct ccw_driver *cdriver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793{
1794 struct device_driver *drv = &cdriver->driver;
1795
1796 drv->bus = &ccw_bus_type;
1797 drv->name = cdriver->name;
Cornelia Huck4beee642008-01-26 14:10:47 +01001798 drv->owner = cdriver->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799
1800 return driver_register(drv);
1801}
1802
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +02001803/**
1804 * ccw_driver_unregister() - deregister a ccw driver
1805 * @cdriver: driver to be deregistered
1806 *
1807 * This function is mainly a wrapper around driver_unregister().
1808 */
1809void ccw_driver_unregister(struct ccw_driver *cdriver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810{
1811 driver_unregister(&cdriver->driver);
1812}
1813
Cornelia Hucka8237fc2006-01-06 00:19:21 -08001814/* Helper func for qdio. */
1815struct subchannel_id
1816ccw_device_get_subchannel_id(struct ccw_device *cdev)
1817{
1818 struct subchannel *sch;
1819
1820 sch = to_subchannel(cdev->dev.parent);
1821 return sch->schid;
1822}
1823
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824MODULE_LICENSE("GPL");
1825EXPORT_SYMBOL(ccw_device_set_online);
1826EXPORT_SYMBOL(ccw_device_set_offline);
1827EXPORT_SYMBOL(ccw_driver_register);
1828EXPORT_SYMBOL(ccw_driver_unregister);
1829EXPORT_SYMBOL(get_ccwdev_by_busid);
1830EXPORT_SYMBOL(ccw_bus_type);
1831EXPORT_SYMBOL(ccw_device_work);
Cornelia Hucka8237fc2006-01-06 00:19:21 -08001832EXPORT_SYMBOL_GPL(ccw_device_get_subchannel_id);