blob: 23b129fd4d8d32f2210798a840e0b5be21424072 [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 *
5 * Copyright (C) 2002 IBM Deutschland Entwicklung GmbH,
6 * IBM Corporation
7 * Author(s): Arnd Bergmann (arndb@de.ibm.com)
Cornelia Huck4ce3b302006-01-14 13:21:04 -08008 * Cornelia Huck (cornelia.huck@de.ibm.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 * Martin Schwidefsky (schwidefsky@de.ibm.com)
10 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/module.h>
12#include <linux/init.h>
13#include <linux/spinlock.h>
14#include <linux/errno.h>
15#include <linux/err.h>
16#include <linux/slab.h>
17#include <linux/list.h>
18#include <linux/device.h>
19#include <linux/workqueue.h>
Peter Oberparleiter90ab1332008-01-26 14:10:52 +010020#include <linux/timer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
22#include <asm/ccwdev.h>
23#include <asm/cio.h>
Tim Schmielau4e57b682005-10-30 15:03:48 -080024#include <asm/param.h> /* HZ */
Cornelia Huck1842f2b2007-10-12 16:11:22 +020025#include <asm/cmb.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 *);
129static int io_subchannel_notify(struct subchannel *, int);
130static void io_subchannel_verify(struct subchannel *);
131static void io_subchannel_ioterm(struct subchannel *);
Cornelia Huck8bbace72006-01-11 10:56:22 +0100132static void io_subchannel_shutdown(struct subchannel *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
Cornelia Huckf7e5d672007-05-10 15:45:43 +0200134static struct css_driver io_subchannel_driver = {
Cornelia Huck4beee642008-01-26 14:10:47 +0100135 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 .subchannel_type = SUBCHANNEL_TYPE_IO,
Cornelia Huck25b7bb52008-01-26 14:10:41 +0100137 .name = "io_subchannel",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 .irq = io_subchannel_irq,
139 .notify = io_subchannel_notify,
140 .verify = io_subchannel_verify,
141 .termination = io_subchannel_ioterm,
Cornelia Huck8bbace72006-01-11 10:56:22 +0100142 .probe = io_subchannel_probe,
143 .remove = io_subchannel_remove,
144 .shutdown = io_subchannel_shutdown,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145};
146
147struct workqueue_struct *ccw_device_work;
148struct workqueue_struct *ccw_device_notify_work;
Peter Oberparleiter40154b82006-06-29 14:57:03 +0200149wait_queue_head_t ccw_device_init_wq;
150atomic_t ccw_device_init_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
Peter Oberparleiter90ab1332008-01-26 14:10:52 +0100152static void recovery_func(unsigned long data);
153
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154static int __init
155init_ccw_bus_type (void)
156{
157 int ret;
158
159 init_waitqueue_head(&ccw_device_init_wq);
160 atomic_set(&ccw_device_init_count, 0);
Peter Oberparleiter90ab1332008-01-26 14:10:52 +0100161 setup_timer(&recovery_timer, recovery_func, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
163 ccw_device_work = create_singlethread_workqueue("cio");
164 if (!ccw_device_work)
165 return -ENOMEM; /* FIXME: better errno ? */
166 ccw_device_notify_work = create_singlethread_workqueue("cio_notify");
167 if (!ccw_device_notify_work) {
168 ret = -ENOMEM; /* FIXME: better errno ? */
169 goto out_err;
170 }
171 slow_path_wq = create_singlethread_workqueue("kslowcrw");
172 if (!slow_path_wq) {
173 ret = -ENOMEM; /* FIXME: better errno ? */
174 goto out_err;
175 }
176 if ((ret = bus_register (&ccw_bus_type)))
177 goto out_err;
178
Cornelia Huck25b7bb52008-01-26 14:10:41 +0100179 ret = css_driver_register(&io_subchannel_driver);
180 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 goto out_err;
182
183 wait_event(ccw_device_init_wq,
184 atomic_read(&ccw_device_init_count) == 0);
185 flush_workqueue(ccw_device_work);
186 return 0;
187out_err:
188 if (ccw_device_work)
189 destroy_workqueue(ccw_device_work);
190 if (ccw_device_notify_work)
191 destroy_workqueue(ccw_device_notify_work);
192 if (slow_path_wq)
193 destroy_workqueue(slow_path_wq);
194 return ret;
195}
196
197static void __exit
198cleanup_ccw_bus_type (void)
199{
Cornelia Huck25b7bb52008-01-26 14:10:41 +0100200 css_driver_unregister(&io_subchannel_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 bus_unregister(&ccw_bus_type);
202 destroy_workqueue(ccw_device_notify_work);
203 destroy_workqueue(ccw_device_work);
204}
205
206subsys_initcall(init_ccw_bus_type);
207module_exit(cleanup_ccw_bus_type);
208
209/************************ device handling **************************/
210
211/*
212 * A ccw_device has some interfaces in sysfs in addition to the
213 * standard ones.
214 * The following entries are designed to export the information which
215 * resided in 2.4 in /proc/subchannels. Subchannel and device number
216 * are obvious, so they don't have an entry :)
217 * TODO: Split chpids and pimpampom up? Where is "in use" in the tree?
218 */
219static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400220chpids_show (struct device * dev, struct device_attribute *attr, char * buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221{
222 struct subchannel *sch = to_subchannel(dev);
Peter Oberparleiter7ad6a242007-04-27 16:01:35 +0200223 struct chsc_ssd_info *ssd = &sch->ssd_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 ssize_t ret = 0;
225 int chp;
Peter Oberparleiter7ad6a242007-04-27 16:01:35 +0200226 int mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
Peter Oberparleiter7ad6a242007-04-27 16:01:35 +0200228 for (chp = 0; chp < 8; chp++) {
229 mask = 0x80 >> chp;
230 if (ssd->path_mask & mask)
231 ret += sprintf(buf + ret, "%02x ", ssd->chpid[chp].id);
232 else
233 ret += sprintf(buf + ret, "00 ");
234 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 ret += sprintf (buf+ret, "\n");
236 return min((ssize_t)PAGE_SIZE, ret);
237}
238
239static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400240pimpampom_show (struct device * dev, struct device_attribute *attr, char * buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241{
242 struct subchannel *sch = to_subchannel(dev);
243 struct pmcw *pmcw = &sch->schib.pmcw;
244
245 return sprintf (buf, "%02x %02x %02x\n",
246 pmcw->pim, pmcw->pam, pmcw->pom);
247}
248
249static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400250devtype_show (struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251{
252 struct ccw_device *cdev = to_ccwdev(dev);
253 struct ccw_device_id *id = &(cdev->id);
254
255 if (id->dev_type != 0)
256 return sprintf(buf, "%04x/%02x\n",
257 id->dev_type, id->dev_model);
258 else
259 return sprintf(buf, "n/a\n");
260}
261
262static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400263cutype_show (struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264{
265 struct ccw_device *cdev = to_ccwdev(dev);
266 struct ccw_device_id *id = &(cdev->id);
267
268 return sprintf(buf, "%04x/%02x\n",
269 id->cu_type, id->cu_model);
270}
271
272static ssize_t
Bastian Blankf1fc78a2005-10-30 15:00:12 -0800273modalias_show (struct device *dev, struct device_attribute *attr, char *buf)
274{
275 struct ccw_device *cdev = to_ccwdev(dev);
276 struct ccw_device_id *id = &(cdev->id);
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +0200277 int len;
Bastian Blankf1fc78a2005-10-30 15:00:12 -0800278
Cornelia Huck086a6c62007-07-17 13:36:08 +0200279 len = snprint_alias(buf, PAGE_SIZE, id, "\n");
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +0200280
281 return len > PAGE_SIZE ? PAGE_SIZE : len;
Bastian Blankf1fc78a2005-10-30 15:00:12 -0800282}
283
284static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400285online_show (struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286{
287 struct ccw_device *cdev = to_ccwdev(dev);
288
289 return sprintf(buf, cdev->online ? "1\n" : "0\n");
290}
291
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100292int ccw_device_is_orphan(struct ccw_device *cdev)
293{
294 return sch_is_pseudo_sch(to_subchannel(cdev->dev.parent));
295}
296
Cornelia Huckef995162007-04-27 16:01:39 +0200297static void ccw_device_unregister(struct ccw_device *cdev)
Cornelia Huck7674da72006-12-08 15:54:21 +0100298{
Cornelia Huck7674da72006-12-08 15:54:21 +0100299 if (test_and_clear_bit(1, &cdev->private->registered))
Cornelia Huckef995162007-04-27 16:01:39 +0200300 device_del(&cdev->dev);
Cornelia Huck7674da72006-12-08 15:54:21 +0100301}
302
Cornelia Huck59a8a6e2007-05-31 17:38:06 +0200303static void ccw_device_remove_orphan_cb(struct device *dev)
304{
305 struct ccw_device *cdev = to_ccwdev(dev);
306
307 ccw_device_unregister(cdev);
308 put_device(&cdev->dev);
309}
310
311static void ccw_device_remove_sch_cb(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312{
313 struct subchannel *sch;
Cornelia Huck59a8a6e2007-05-31 17:38:06 +0200314
315 sch = to_subchannel(dev);
Cornelia Huck6ab48792006-07-12 16:39:50 +0200316 css_sch_device_unregister(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 /* Reset intparm to zeroes. */
318 sch->schib.pmcw.intparm = 0;
319 cio_modify(sch);
320 put_device(&sch->dev);
321}
322
Cornelia Huck59a8a6e2007-05-31 17:38:06 +0200323static void
324ccw_device_remove_disconnected(struct ccw_device *cdev)
325{
326 unsigned long flags;
327 int rc;
328
329 /*
330 * Forced offline in disconnected state means
331 * 'throw away device'.
332 */
333 if (ccw_device_is_orphan(cdev)) {
334 /*
335 * Deregister ccw device.
336 * Unfortunately, we cannot do this directly from the
337 * attribute method.
338 */
339 spin_lock_irqsave(cdev->ccwlock, flags);
340 cdev->private->state = DEV_STATE_NOT_OPER;
341 spin_unlock_irqrestore(cdev->ccwlock, flags);
342 rc = device_schedule_callback(&cdev->dev,
343 ccw_device_remove_orphan_cb);
344 if (rc)
Michael Ernst139b83d2008-05-07 09:22:54 +0200345 CIO_MSG_EVENT(0, "Couldn't unregister orphan "
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200346 "0.%x.%04x\n",
347 cdev->private->dev_id.ssid,
348 cdev->private->dev_id.devno);
Cornelia Huck59a8a6e2007-05-31 17:38:06 +0200349 return;
350 }
351 /* Deregister subchannel, which will kill the ccw device. */
352 rc = device_schedule_callback(cdev->dev.parent,
353 ccw_device_remove_sch_cb);
354 if (rc)
Michael Ernst139b83d2008-05-07 09:22:54 +0200355 CIO_MSG_EVENT(0, "Couldn't unregister disconnected device "
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200356 "0.%x.%04x\n",
357 cdev->private->dev_id.ssid,
358 cdev->private->dev_id.devno);
Cornelia Huck59a8a6e2007-05-31 17:38:06 +0200359}
360
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +0200361/**
362 * ccw_device_set_offline() - disable a ccw device for I/O
363 * @cdev: target ccw device
364 *
365 * This function calls the driver's set_offline() function for @cdev, if
366 * given, and then disables @cdev.
367 * Returns:
368 * %0 on success and a negative error value on failure.
369 * Context:
370 * enabled, ccw device lock not held
371 */
372int ccw_device_set_offline(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373{
374 int ret;
375
376 if (!cdev)
377 return -ENODEV;
378 if (!cdev->online || !cdev->drv)
379 return -EINVAL;
380
381 if (cdev->drv->set_offline) {
382 ret = cdev->drv->set_offline(cdev);
383 if (ret != 0)
384 return ret;
385 }
386 cdev->online = 0;
387 spin_lock_irq(cdev->ccwlock);
388 ret = ccw_device_offline(cdev);
389 if (ret == -ENODEV) {
390 if (cdev->private->state != DEV_STATE_NOT_OPER) {
391 cdev->private->state = DEV_STATE_OFFLINE;
392 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
393 }
394 spin_unlock_irq(cdev->ccwlock);
395 return ret;
396 }
397 spin_unlock_irq(cdev->ccwlock);
398 if (ret == 0)
399 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
400 else {
Michael Ernst139b83d2008-05-07 09:22:54 +0200401 CIO_MSG_EVENT(0, "ccw_device_offline returned %d, "
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200402 "device 0.%x.%04x\n",
403 ret, cdev->private->dev_id.ssid,
404 cdev->private->dev_id.devno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 cdev->online = 1;
406 }
407 return ret;
408}
409
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +0200410/**
411 * ccw_device_set_online() - enable a ccw device for I/O
412 * @cdev: target ccw device
413 *
414 * This function first enables @cdev and then calls the driver's set_online()
415 * function for @cdev, if given. If set_online() returns an error, @cdev is
416 * disabled again.
417 * Returns:
418 * %0 on success and a negative error value on failure.
419 * Context:
420 * enabled, ccw device lock not held
421 */
422int ccw_device_set_online(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423{
424 int ret;
425
426 if (!cdev)
427 return -ENODEV;
428 if (cdev->online || !cdev->drv)
429 return -EINVAL;
430
431 spin_lock_irq(cdev->ccwlock);
432 ret = ccw_device_online(cdev);
433 spin_unlock_irq(cdev->ccwlock);
434 if (ret == 0)
435 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
436 else {
Michael Ernst139b83d2008-05-07 09:22:54 +0200437 CIO_MSG_EVENT(0, "ccw_device_online returned %d, "
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200438 "device 0.%x.%04x\n",
439 ret, cdev->private->dev_id.ssid,
440 cdev->private->dev_id.devno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 return ret;
442 }
443 if (cdev->private->state != DEV_STATE_ONLINE)
444 return -ENODEV;
445 if (!cdev->drv->set_online || cdev->drv->set_online(cdev) == 0) {
446 cdev->online = 1;
447 return 0;
448 }
449 spin_lock_irq(cdev->ccwlock);
450 ret = ccw_device_offline(cdev);
451 spin_unlock_irq(cdev->ccwlock);
452 if (ret == 0)
453 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200454 else
Michael Ernst139b83d2008-05-07 09:22:54 +0200455 CIO_MSG_EVENT(0, "ccw_device_offline returned %d, "
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200456 "device 0.%x.%04x\n",
457 ret, cdev->private->dev_id.ssid,
458 cdev->private->dev_id.devno);
Cornelia Huck6cadb782006-02-17 13:52:49 -0800459 return (ret == 0) ? -ENODEV : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460}
461
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200462static void online_store_handle_offline(struct ccw_device *cdev)
463{
464 if (cdev->private->state == DEV_STATE_DISCONNECTED)
465 ccw_device_remove_disconnected(cdev);
466 else if (cdev->drv && cdev->drv->set_offline)
467 ccw_device_set_offline(cdev);
468}
469
470static int online_store_recog_and_online(struct ccw_device *cdev)
471{
472 int ret;
473
474 /* Do device recognition, if needed. */
475 if (cdev->id.cu_type == 0) {
476 ret = ccw_device_recognition(cdev);
477 if (ret) {
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200478 CIO_MSG_EVENT(0, "Couldn't start recognition "
479 "for device 0.%x.%04x (ret=%d)\n",
480 cdev->private->dev_id.ssid,
481 cdev->private->dev_id.devno, ret);
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200482 return ret;
483 }
484 wait_event(cdev->private->wait_q,
485 cdev->private->flags.recog_done);
486 }
487 if (cdev->drv && cdev->drv->set_online)
488 ccw_device_set_online(cdev);
489 return 0;
490}
491static void online_store_handle_online(struct ccw_device *cdev, int force)
492{
493 int ret;
494
495 ret = online_store_recog_and_online(cdev);
496 if (ret)
497 return;
498 if (force && cdev->private->state == DEV_STATE_BOXED) {
499 ret = ccw_device_stlck(cdev);
500 if (ret) {
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200501 dev_warn(&cdev->dev,
502 "ccw_device_stlck returned %d!\n", ret);
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200503 return;
504 }
505 if (cdev->id.cu_type == 0)
506 cdev->private->state = DEV_STATE_NOT_OPER;
507 online_store_recog_and_online(cdev);
508 }
509
510}
511
512static ssize_t online_store (struct device *dev, struct device_attribute *attr,
513 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514{
515 struct ccw_device *cdev = to_ccwdev(dev);
Cornelia Huck2f972202008-04-30 13:38:33 +0200516 int force, ret;
517 unsigned long i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
Martin Schwidefsky973bd992006-01-06 00:19:07 -0800519 if (atomic_cmpxchg(&cdev->private->onoff, 0, 1) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 return -EAGAIN;
521
522 if (cdev->drv && !try_module_get(cdev->drv->owner)) {
523 atomic_set(&cdev->private->onoff, 0);
524 return -EINVAL;
525 }
526 if (!strncmp(buf, "force\n", count)) {
527 force = 1;
528 i = 1;
Cornelia Huck2f972202008-04-30 13:38:33 +0200529 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 } else {
531 force = 0;
Cornelia Huck2f972202008-04-30 13:38:33 +0200532 ret = strict_strtoul(buf, 16, &i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 }
Cornelia Huck2f972202008-04-30 13:38:33 +0200534 if (ret)
535 goto out;
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200536 switch (i) {
537 case 0:
538 online_store_handle_offline(cdev);
Cornelia Huck2f972202008-04-30 13:38:33 +0200539 ret = count;
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200540 break;
541 case 1:
542 online_store_handle_online(cdev, force);
Cornelia Huck2f972202008-04-30 13:38:33 +0200543 ret = count;
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200544 break;
545 default:
Cornelia Huck2f972202008-04-30 13:38:33 +0200546 ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 }
Cornelia Huck2f972202008-04-30 13:38:33 +0200548out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 if (cdev->drv)
550 module_put(cdev->drv->owner);
551 atomic_set(&cdev->private->onoff, 0);
Cornelia Huck2f972202008-04-30 13:38:33 +0200552 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553}
554
555static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400556available_show (struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557{
558 struct ccw_device *cdev = to_ccwdev(dev);
559 struct subchannel *sch;
560
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100561 if (ccw_device_is_orphan(cdev))
562 return sprintf(buf, "no device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 switch (cdev->private->state) {
564 case DEV_STATE_BOXED:
565 return sprintf(buf, "boxed\n");
566 case DEV_STATE_DISCONNECTED:
567 case DEV_STATE_DISCONNECTED_SENSE_ID:
568 case DEV_STATE_NOT_OPER:
569 sch = to_subchannel(dev->parent);
570 if (!sch->lpm)
571 return sprintf(buf, "no path\n");
572 else
573 return sprintf(buf, "no device\n");
574 default:
575 /* All other states considered fine. */
576 return sprintf(buf, "good\n");
577 }
578}
579
580static DEVICE_ATTR(chpids, 0444, chpids_show, NULL);
581static DEVICE_ATTR(pimpampom, 0444, pimpampom_show, NULL);
582static DEVICE_ATTR(devtype, 0444, devtype_show, NULL);
583static DEVICE_ATTR(cutype, 0444, cutype_show, NULL);
Bastian Blankf1fc78a2005-10-30 15:00:12 -0800584static DEVICE_ATTR(modalias, 0444, modalias_show, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585static DEVICE_ATTR(online, 0644, online_show, online_store);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586static DEVICE_ATTR(availability, 0444, available_show, NULL);
587
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200588static struct attribute *io_subchannel_attrs[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 &dev_attr_chpids.attr,
590 &dev_attr_pimpampom.attr,
591 NULL,
592};
593
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200594static struct attribute_group io_subchannel_attr_group = {
595 .attrs = io_subchannel_attrs,
Cornelia Huck529192f2006-12-08 15:55:57 +0100596};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
598static struct attribute * ccwdev_attrs[] = {
599 &dev_attr_devtype.attr,
600 &dev_attr_cutype.attr,
Bastian Blankf1fc78a2005-10-30 15:00:12 -0800601 &dev_attr_modalias.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 &dev_attr_online.attr,
603 &dev_attr_cmb_enable.attr,
604 &dev_attr_availability.attr,
605 NULL,
606};
607
608static struct attribute_group ccwdev_attr_group = {
609 .attrs = ccwdev_attrs,
610};
611
Cornelia Huckf7e5d672007-05-10 15:45:43 +0200612static struct attribute_group *ccwdev_attr_groups[] = {
Cornelia Huckef995162007-04-27 16:01:39 +0200613 &ccwdev_attr_group,
614 NULL,
615};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
617/* this is a simple abstraction for device_register that sets the
618 * correct bus type and adds the bus specific files */
Cornelia Huck3c9da7b2006-10-27 12:39:33 +0200619static int ccw_device_register(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620{
621 struct device *dev = &cdev->dev;
622 int ret;
623
624 dev->bus = &ccw_bus_type;
625
626 if ((ret = device_add(dev)))
627 return ret;
628
629 set_bit(1, &cdev->private->registered);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 return ret;
631}
632
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700633struct match_data {
Cornelia Huck78964262006-10-11 15:31:38 +0200634 struct ccw_dev_id dev_id;
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700635 struct ccw_device * sibling;
636};
637
638static int
639match_devno(struct device * dev, void * data)
640{
Cornelia Huck78964262006-10-11 15:31:38 +0200641 struct match_data * d = data;
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700642 struct ccw_device * cdev;
643
644 cdev = to_ccwdev(dev);
645 if ((cdev->private->state == DEV_STATE_DISCONNECTED) &&
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100646 !ccw_device_is_orphan(cdev) &&
Cornelia Huck78964262006-10-11 15:31:38 +0200647 ccw_dev_id_is_equal(&cdev->private->dev_id, &d->dev_id) &&
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100648 (cdev != d->sibling))
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700649 return 1;
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700650 return 0;
651}
652
Cornelia Huck78964262006-10-11 15:31:38 +0200653static struct ccw_device * get_disc_ccwdev_by_dev_id(struct ccw_dev_id *dev_id,
654 struct ccw_device *sibling)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 struct device *dev;
Heiko Carstens292888c2006-08-30 14:33:35 +0200657 struct match_data data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
Cornelia Huck78964262006-10-11 15:31:38 +0200659 data.dev_id = *dev_id;
Heiko Carstens292888c2006-08-30 14:33:35 +0200660 data.sibling = sibling;
Cornelia Hucke5945b42005-10-11 08:28:59 -0700661 dev = bus_find_device(&ccw_bus_type, NULL, &data, match_devno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700663 return dev ? to_ccwdev(dev) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664}
665
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100666static int match_orphan(struct device *dev, void *data)
667{
668 struct ccw_dev_id *dev_id;
669 struct ccw_device *cdev;
670
671 dev_id = data;
672 cdev = to_ccwdev(dev);
673 return ccw_dev_id_is_equal(&cdev->private->dev_id, dev_id);
674}
675
676static struct ccw_device *
677get_orphaned_ccwdev_by_dev_id(struct channel_subsystem *css,
678 struct ccw_dev_id *dev_id)
679{
680 struct device *dev;
681
682 dev = device_find_child(&css->pseudo_subchannel->dev, dev_id,
683 match_orphan);
684
685 return dev ? to_ccwdev(dev) : NULL;
686}
687
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688static void
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100689ccw_device_add_changed(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690{
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100691 struct ccw_device_private *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 struct ccw_device *cdev;
693
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100694 priv = container_of(work, struct ccw_device_private, kick_work);
695 cdev = priv->cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 if (device_add(&cdev->dev)) {
697 put_device(&cdev->dev);
698 return;
699 }
700 set_bit(1, &cdev->private->registered);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701}
702
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100703void ccw_device_do_unreg_rereg(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704{
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100705 struct ccw_device_private *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 struct ccw_device *cdev;
707 struct subchannel *sch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100709 priv = container_of(work, struct ccw_device_private, kick_work);
710 cdev = priv->cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 sch = to_subchannel(cdev->dev.parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712
Cornelia Huckef995162007-04-27 16:01:39 +0200713 ccw_device_unregister(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 PREPARE_WORK(&cdev->private->kick_work,
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100715 ccw_device_add_changed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 queue_work(ccw_device_work, &cdev->private->kick_work);
717}
718
719static void
720ccw_device_release(struct device *dev)
721{
722 struct ccw_device *cdev;
723
724 cdev = to_ccwdev(dev);
725 kfree(cdev->private);
726 kfree(cdev);
727}
728
Cornelia Huck7674da72006-12-08 15:54:21 +0100729static struct ccw_device * io_subchannel_allocate_dev(struct subchannel *sch)
730{
731 struct ccw_device *cdev;
732
733 cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
734 if (cdev) {
735 cdev->private = kzalloc(sizeof(struct ccw_device_private),
736 GFP_KERNEL | GFP_DMA);
737 if (cdev->private)
738 return cdev;
739 }
740 kfree(cdev);
741 return ERR_PTR(-ENOMEM);
742}
743
744static int io_subchannel_initialize_dev(struct subchannel *sch,
745 struct ccw_device *cdev)
746{
747 cdev->private->cdev = cdev;
748 atomic_set(&cdev->private->onoff, 0);
749 cdev->dev.parent = &sch->dev;
750 cdev->dev.release = ccw_device_release;
Heiko Carstens33583c32007-11-05 11:10:07 +0100751 INIT_WORK(&cdev->private->kick_work, NULL);
Cornelia Huckef995162007-04-27 16:01:39 +0200752 cdev->dev.groups = ccwdev_attr_groups;
Cornelia Huck7674da72006-12-08 15:54:21 +0100753 /* Do first half of device_register. */
754 device_initialize(&cdev->dev);
755 if (!get_device(&sch->dev)) {
756 if (cdev->dev.release)
757 cdev->dev.release(&cdev->dev);
758 return -ENODEV;
759 }
760 return 0;
761}
762
763static struct ccw_device * io_subchannel_create_ccwdev(struct subchannel *sch)
764{
765 struct ccw_device *cdev;
766 int ret;
767
768 cdev = io_subchannel_allocate_dev(sch);
769 if (!IS_ERR(cdev)) {
770 ret = io_subchannel_initialize_dev(sch, cdev);
771 if (ret) {
772 kfree(cdev);
773 cdev = ERR_PTR(ret);
774 }
775 }
776 return cdev;
777}
778
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100779static int io_subchannel_recog(struct ccw_device *, struct subchannel *);
780
781static void sch_attach_device(struct subchannel *sch,
782 struct ccw_device *cdev)
783{
Cornelia Huck82b7ac02007-04-27 16:01:36 +0200784 css_update_ssd_info(sch);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100785 spin_lock_irq(sch->lock);
Cornelia Huckdb6a6422008-01-26 14:10:46 +0100786 sch_set_cdev(sch, cdev);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100787 cdev->private->schid = sch->schid;
788 cdev->ccwlock = sch->lock;
789 device_trigger_reprobe(sch);
790 spin_unlock_irq(sch->lock);
791}
792
793static void sch_attach_disconnected_device(struct subchannel *sch,
794 struct ccw_device *cdev)
795{
796 struct subchannel *other_sch;
797 int ret;
798
799 other_sch = to_subchannel(get_device(cdev->dev.parent));
800 ret = device_move(&cdev->dev, &sch->dev);
801 if (ret) {
Michael Ernst139b83d2008-05-07 09:22:54 +0200802 CIO_MSG_EVENT(0, "Moving disconnected device 0.%x.%04x failed "
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100803 "(ret=%d)!\n", cdev->private->dev_id.ssid,
804 cdev->private->dev_id.devno, ret);
805 put_device(&other_sch->dev);
806 return;
807 }
Cornelia Huckdb6a6422008-01-26 14:10:46 +0100808 sch_set_cdev(other_sch, NULL);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100809 /* No need to keep a subchannel without ccw device around. */
810 css_sch_device_unregister(other_sch);
811 put_device(&other_sch->dev);
812 sch_attach_device(sch, cdev);
813}
814
815static void sch_attach_orphaned_device(struct subchannel *sch,
816 struct ccw_device *cdev)
817{
818 int ret;
819
820 /* Try to move the ccw device to its new subchannel. */
821 ret = device_move(&cdev->dev, &sch->dev);
822 if (ret) {
823 CIO_MSG_EVENT(0, "Moving device 0.%x.%04x from orphanage "
824 "failed (ret=%d)!\n",
825 cdev->private->dev_id.ssid,
826 cdev->private->dev_id.devno, ret);
827 return;
828 }
829 sch_attach_device(sch, cdev);
830}
831
832static void sch_create_and_recog_new_device(struct subchannel *sch)
833{
834 struct ccw_device *cdev;
835
836 /* Need to allocate a new ccw device. */
837 cdev = io_subchannel_create_ccwdev(sch);
838 if (IS_ERR(cdev)) {
839 /* OK, we did everything we could... */
840 css_sch_device_unregister(sch);
841 return;
842 }
843 spin_lock_irq(sch->lock);
Cornelia Huckdb6a6422008-01-26 14:10:46 +0100844 sch_set_cdev(sch, cdev);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100845 spin_unlock_irq(sch->lock);
846 /* Start recognition for the new ccw device. */
847 if (io_subchannel_recog(cdev, sch)) {
848 spin_lock_irq(sch->lock);
Cornelia Huckdb6a6422008-01-26 14:10:46 +0100849 sch_set_cdev(sch, NULL);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100850 spin_unlock_irq(sch->lock);
851 if (cdev->dev.release)
852 cdev->dev.release(&cdev->dev);
853 css_sch_device_unregister(sch);
854 }
855}
856
857
858void ccw_device_move_to_orphanage(struct work_struct *work)
859{
860 struct ccw_device_private *priv;
861 struct ccw_device *cdev;
862 struct ccw_device *replacing_cdev;
863 struct subchannel *sch;
864 int ret;
865 struct channel_subsystem *css;
866 struct ccw_dev_id dev_id;
867
868 priv = container_of(work, struct ccw_device_private, kick_work);
869 cdev = priv->cdev;
870 sch = to_subchannel(cdev->dev.parent);
871 css = to_css(sch->dev.parent);
872 dev_id.devno = sch->schib.pmcw.dev;
873 dev_id.ssid = sch->schid.ssid;
874
875 /*
876 * Move the orphaned ccw device to the orphanage so the replacing
877 * ccw device can take its place on the subchannel.
878 */
879 ret = device_move(&cdev->dev, &css->pseudo_subchannel->dev);
880 if (ret) {
881 CIO_MSG_EVENT(0, "Moving device 0.%x.%04x to orphanage failed "
882 "(ret=%d)!\n", cdev->private->dev_id.ssid,
883 cdev->private->dev_id.devno, ret);
884 return;
885 }
886 cdev->ccwlock = css->pseudo_subchannel->lock;
887 /*
888 * Search for the replacing ccw device
889 * - among the disconnected devices
890 * - in the orphanage
891 */
892 replacing_cdev = get_disc_ccwdev_by_dev_id(&dev_id, cdev);
893 if (replacing_cdev) {
894 sch_attach_disconnected_device(sch, replacing_cdev);
895 return;
896 }
897 replacing_cdev = get_orphaned_ccwdev_by_dev_id(css, &dev_id);
898 if (replacing_cdev) {
899 sch_attach_orphaned_device(sch, replacing_cdev);
900 return;
901 }
902 sch_create_and_recog_new_device(sch);
903}
904
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905/*
906 * Register recognized device.
907 */
908static void
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100909io_subchannel_register(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910{
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100911 struct ccw_device_private *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 struct ccw_device *cdev;
913 struct subchannel *sch;
914 int ret;
915 unsigned long flags;
916
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100917 priv = container_of(work, struct ccw_device_private, kick_work);
918 cdev = priv->cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 sch = to_subchannel(cdev->dev.parent);
Cornelia Huck82b7ac02007-04-27 16:01:36 +0200920 css_update_ssd_info(sch);
Cornelia Huck47af5512006-12-04 15:41:07 +0100921 /*
922 * io_subchannel_register() will also be called after device
923 * recognition has been done for a boxed device (which will already
924 * be registered). We need to reprobe since we may now have sense id
925 * information.
926 */
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700927 if (klist_node_attached(&cdev->dev.knode_parent)) {
Cornelia Huck47af5512006-12-04 15:41:07 +0100928 if (!cdev->drv) {
929 ret = device_reprobe(&cdev->dev);
930 if (ret)
931 /* We can't do much here. */
Michael Ernst139b83d2008-05-07 09:22:54 +0200932 CIO_MSG_EVENT(0, "device_reprobe() returned"
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200933 " %d for 0.%x.%04x\n", ret,
934 cdev->private->dev_id.ssid,
935 cdev->private->dev_id.devno);
Cornelia Huck47af5512006-12-04 15:41:07 +0100936 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 goto out;
938 }
Cornelia Huckfa1a8c22007-04-26 00:12:03 -0700939 /*
940 * Now we know this subchannel will stay, we can throw
941 * our delayed uevent.
942 */
943 sch->dev.uevent_suppress = 0;
944 kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 /* make it known to the system */
946 ret = ccw_device_register(cdev);
947 if (ret) {
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200948 CIO_MSG_EVENT(0, "Could not register ccw dev 0.%x.%04x: %d\n",
949 cdev->private->dev_id.ssid,
950 cdev->private->dev_id.devno, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 put_device(&cdev->dev);
Cornelia Huck2ec22982006-12-08 15:54:26 +0100952 spin_lock_irqsave(sch->lock, flags);
Cornelia Huckdb6a6422008-01-26 14:10:46 +0100953 sch_set_cdev(sch, NULL);
Cornelia Huck2ec22982006-12-08 15:54:26 +0100954 spin_unlock_irqrestore(sch->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 kfree (cdev->private);
956 kfree (cdev);
957 put_device(&sch->dev);
958 if (atomic_dec_and_test(&ccw_device_init_count))
959 wake_up(&ccw_device_init_wq);
960 return;
961 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 put_device(&cdev->dev);
963out:
964 cdev->private->flags.recog_done = 1;
965 put_device(&sch->dev);
966 wake_up(&cdev->private->wait_q);
967 if (atomic_dec_and_test(&ccw_device_init_count))
968 wake_up(&ccw_device_init_wq);
969}
970
Cornelia Huck3f4cf6e2007-10-12 16:11:26 +0200971static void ccw_device_call_sch_unregister(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972{
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100973 struct ccw_device_private *priv;
974 struct ccw_device *cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 struct subchannel *sch;
976
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100977 priv = container_of(work, struct ccw_device_private, kick_work);
978 cdev = priv->cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 sch = to_subchannel(cdev->dev.parent);
Cornelia Huck6ab48792006-07-12 16:39:50 +0200980 css_sch_device_unregister(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 /* Reset intparm to zeroes. */
982 sch->schib.pmcw.intparm = 0;
983 cio_modify(sch);
984 put_device(&cdev->dev);
985 put_device(&sch->dev);
986}
987
988/*
989 * subchannel recognition done. Called from the state machine.
990 */
991void
992io_subchannel_recog_done(struct ccw_device *cdev)
993{
994 struct subchannel *sch;
995
996 if (css_init_done == 0) {
997 cdev->private->flags.recog_done = 1;
998 return;
999 }
1000 switch (cdev->private->state) {
1001 case DEV_STATE_NOT_OPER:
1002 cdev->private->flags.recog_done = 1;
1003 /* Remove device found not operational. */
1004 if (!get_device(&cdev->dev))
1005 break;
1006 sch = to_subchannel(cdev->dev.parent);
1007 PREPARE_WORK(&cdev->private->kick_work,
Martin Schwidefskyc1637532006-12-08 15:53:57 +01001008 ccw_device_call_sch_unregister);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 queue_work(slow_path_wq, &cdev->private->kick_work);
1010 if (atomic_dec_and_test(&ccw_device_init_count))
1011 wake_up(&ccw_device_init_wq);
1012 break;
1013 case DEV_STATE_BOXED:
1014 /* Device did not respond in time. */
1015 case DEV_STATE_OFFLINE:
1016 /*
1017 * We can't register the device in interrupt context so
1018 * we schedule a work item.
1019 */
1020 if (!get_device(&cdev->dev))
1021 break;
1022 PREPARE_WORK(&cdev->private->kick_work,
Martin Schwidefskyc1637532006-12-08 15:53:57 +01001023 io_subchannel_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 queue_work(slow_path_wq, &cdev->private->kick_work);
1025 break;
1026 }
1027}
1028
1029static int
1030io_subchannel_recog(struct ccw_device *cdev, struct subchannel *sch)
1031{
1032 int rc;
1033 struct ccw_device_private *priv;
1034
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001035 sch_set_cdev(sch, cdev);
Cornelia Huck2ec22982006-12-08 15:54:26 +01001036 cdev->ccwlock = sch->lock;
Cornelia Huckfb6958a2006-01-06 00:19:25 -08001037
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 /* Init private data. */
1039 priv = cdev->private;
Cornelia Huck78964262006-10-11 15:31:38 +02001040 priv->dev_id.devno = sch->schib.pmcw.dev;
1041 priv->dev_id.ssid = sch->schid.ssid;
1042 priv->schid = sch->schid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 priv->state = DEV_STATE_NOT_OPER;
1044 INIT_LIST_HEAD(&priv->cmb_list);
1045 init_waitqueue_head(&priv->wait_q);
1046 init_timer(&priv->timer);
1047
1048 /* Set an initial name for the device. */
Cornelia Huckfb6958a2006-01-06 00:19:25 -08001049 snprintf (cdev->dev.bus_id, BUS_ID_SIZE, "0.%x.%04x",
1050 sch->schid.ssid, sch->schib.pmcw.dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051
1052 /* Increase counter of devices currently in recognition. */
1053 atomic_inc(&ccw_device_init_count);
1054
1055 /* Start async. device sensing. */
Cornelia Huck2ec22982006-12-08 15:54:26 +01001056 spin_lock_irq(sch->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 rc = ccw_device_recognition(cdev);
Cornelia Huck2ec22982006-12-08 15:54:26 +01001058 spin_unlock_irq(sch->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 if (rc) {
1060 if (atomic_dec_and_test(&ccw_device_init_count))
1061 wake_up(&ccw_device_init_wq);
1062 }
1063 return rc;
1064}
1065
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +01001066static void ccw_device_move_to_sch(struct work_struct *work)
1067{
1068 struct ccw_device_private *priv;
1069 int rc;
1070 struct subchannel *sch;
1071 struct ccw_device *cdev;
1072 struct subchannel *former_parent;
1073
1074 priv = container_of(work, struct ccw_device_private, kick_work);
1075 sch = priv->sch;
1076 cdev = priv->cdev;
1077 former_parent = ccw_device_is_orphan(cdev) ?
1078 NULL : to_subchannel(get_device(cdev->dev.parent));
1079 mutex_lock(&sch->reg_mutex);
1080 /* Try to move the ccw device to its new subchannel. */
1081 rc = device_move(&cdev->dev, &sch->dev);
1082 mutex_unlock(&sch->reg_mutex);
1083 if (rc) {
Michael Ernst139b83d2008-05-07 09:22:54 +02001084 CIO_MSG_EVENT(0, "Moving device 0.%x.%04x to subchannel "
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +01001085 "0.%x.%04x failed (ret=%d)!\n",
1086 cdev->private->dev_id.ssid,
1087 cdev->private->dev_id.devno, sch->schid.ssid,
1088 sch->schid.sch_no, rc);
1089 css_sch_device_unregister(sch);
1090 goto out;
1091 }
1092 if (former_parent) {
1093 spin_lock_irq(former_parent->lock);
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001094 sch_set_cdev(former_parent, NULL);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +01001095 spin_unlock_irq(former_parent->lock);
1096 css_sch_device_unregister(former_parent);
1097 /* Reset intparm to zeroes. */
1098 former_parent->schib.pmcw.intparm = 0;
1099 cio_modify(former_parent);
1100 }
1101 sch_attach_device(sch, cdev);
1102out:
1103 if (former_parent)
1104 put_device(&former_parent->dev);
1105 put_device(&cdev->dev);
1106}
1107
Cornelia Huck602b20f2008-01-26 14:10:39 +01001108static void io_subchannel_irq(struct subchannel *sch)
1109{
1110 struct ccw_device *cdev;
1111
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001112 cdev = sch_get_cdev(sch);
Cornelia Huck602b20f2008-01-26 14:10:39 +01001113
1114 CIO_TRACE_EVENT(3, "IRQ");
1115 CIO_TRACE_EVENT(3, sch->dev.bus_id);
1116 if (cdev)
1117 dev_fsm_event(cdev, DEV_EVENT_INTERRUPT);
1118}
1119
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001120static void io_subchannel_init_fields(struct subchannel *sch)
1121{
1122 if (cio_is_console(sch->schid))
1123 sch->opm = 0xff;
1124 else
1125 sch->opm = chp_get_sch_opm(sch);
1126 sch->lpm = sch->schib.pmcw.pam & sch->opm;
1127 sch->isc = cio_is_console(sch->schid) ? 1 : 3;
1128
1129 CIO_MSG_EVENT(6, "Detected device %04x on subchannel 0.%x.%04X"
1130 " - PIM = %02X, PAM = %02X, POM = %02X\n",
1131 sch->schib.pmcw.dev, sch->schid.ssid,
1132 sch->schid.sch_no, sch->schib.pmcw.pim,
1133 sch->schib.pmcw.pam, sch->schib.pmcw.pom);
1134 /* Initially set up some fields in the pmcw. */
1135 sch->schib.pmcw.ena = 0;
1136 sch->schib.pmcw.csense = 1; /* concurrent sense */
1137 if ((sch->lpm & (sch->lpm - 1)) != 0)
1138 sch->schib.pmcw.mp = 1; /* multipath mode */
1139 /* clean up possible residual cmf stuff */
1140 sch->schib.pmcw.mme = 0;
1141 sch->schib.pmcw.mbfc = 0;
1142 sch->schib.pmcw.mbi = 0;
1143 sch->schib.mba = 0;
1144}
1145
1146static int io_subchannel_probe(struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 struct ccw_device *cdev;
1149 int rc;
1150 unsigned long flags;
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +01001151 struct ccw_dev_id dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001153 cdev = sch_get_cdev(sch);
1154 if (cdev) {
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001155 rc = sysfs_create_group(&sch->dev.kobj,
1156 &io_subchannel_attr_group);
1157 if (rc)
1158 CIO_MSG_EVENT(0, "Failed to create io subchannel "
1159 "attributes for subchannel "
1160 "0.%x.%04x (rc=%d)\n",
1161 sch->schid.ssid, sch->schid.sch_no, rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 /*
1163 * This subchannel already has an associated ccw_device.
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001164 * Throw the delayed uevent for the subchannel, register
1165 * the ccw_device and exit. This happens for all early
1166 * devices, e.g. the console.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 */
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001168 sch->dev.uevent_suppress = 0;
1169 kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
Cornelia Hucke1031782007-10-12 16:11:23 +02001170 cdev->dev.groups = ccwdev_attr_groups;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 device_initialize(&cdev->dev);
1172 ccw_device_register(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 /*
1174 * Check if the device is already online. If it is
1175 * the reference count needs to be corrected
1176 * (see ccw_device_online and css_init_done for the
1177 * ugly details).
1178 */
1179 if (cdev->private->state != DEV_STATE_NOT_OPER &&
1180 cdev->private->state != DEV_STATE_OFFLINE &&
1181 cdev->private->state != DEV_STATE_BOXED)
1182 get_device(&cdev->dev);
1183 return 0;
1184 }
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001185 io_subchannel_init_fields(sch);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +01001186 /*
1187 * First check if a fitting device may be found amongst the
1188 * disconnected devices or in the orphanage.
1189 */
1190 dev_id.devno = sch->schib.pmcw.dev;
1191 dev_id.ssid = sch->schid.ssid;
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001192 rc = sysfs_create_group(&sch->dev.kobj,
1193 &io_subchannel_attr_group);
1194 if (rc)
1195 return rc;
Cornelia Huckcd6b4f22008-01-26 14:10:43 +01001196 /* Allocate I/O subchannel private data. */
1197 sch->private = kzalloc(sizeof(struct io_subchannel_private),
1198 GFP_KERNEL | GFP_DMA);
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001199 if (!sch->private) {
1200 rc = -ENOMEM;
1201 goto out_err;
1202 }
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +01001203 cdev = get_disc_ccwdev_by_dev_id(&dev_id, NULL);
1204 if (!cdev)
1205 cdev = get_orphaned_ccwdev_by_dev_id(to_css(sch->dev.parent),
1206 &dev_id);
1207 if (cdev) {
1208 /*
1209 * Schedule moving the device until when we have a registered
1210 * subchannel to move to and succeed the probe. We can
1211 * unregister later again, when the probe is through.
1212 */
1213 cdev->private->sch = sch;
1214 PREPARE_WORK(&cdev->private->kick_work,
1215 ccw_device_move_to_sch);
1216 queue_work(slow_path_wq, &cdev->private->kick_work);
1217 return 0;
1218 }
Cornelia Huck7674da72006-12-08 15:54:21 +01001219 cdev = io_subchannel_create_ccwdev(sch);
Cornelia Huckcd6b4f22008-01-26 14:10:43 +01001220 if (IS_ERR(cdev)) {
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001221 rc = PTR_ERR(cdev);
1222 goto out_err;
Cornelia Huckcd6b4f22008-01-26 14:10:43 +01001223 }
Cornelia Huck8bbace72006-01-11 10:56:22 +01001224 rc = io_subchannel_recog(cdev, sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 if (rc) {
Cornelia Huck2ec22982006-12-08 15:54:26 +01001226 spin_lock_irqsave(sch->lock, flags);
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001227 sch_set_cdev(sch, NULL);
Cornelia Huck2ec22982006-12-08 15:54:26 +01001228 spin_unlock_irqrestore(sch->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 if (cdev->dev.release)
1230 cdev->dev.release(&cdev->dev);
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001231 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 }
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001233 return 0;
1234out_err:
1235 kfree(sch->private);
1236 sysfs_remove_group(&sch->dev.kobj, &io_subchannel_attr_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 return rc;
1238}
1239
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240static int
Cornelia Huck8bbace72006-01-11 10:56:22 +01001241io_subchannel_remove (struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242{
1243 struct ccw_device *cdev;
1244 unsigned long flags;
1245
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001246 cdev = sch_get_cdev(sch);
1247 if (!cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 /* Set ccw device to not operational and drop reference. */
1250 spin_lock_irqsave(cdev->ccwlock, flags);
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001251 sch_set_cdev(sch, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 cdev->private->state = DEV_STATE_NOT_OPER;
1253 spin_unlock_irqrestore(cdev->ccwlock, flags);
Cornelia Huckef995162007-04-27 16:01:39 +02001254 ccw_device_unregister(cdev);
1255 put_device(&cdev->dev);
Cornelia Huckcd6b4f22008-01-26 14:10:43 +01001256 kfree(sch->private);
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001257 sysfs_remove_group(&sch->dev.kobj, &io_subchannel_attr_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 return 0;
1259}
1260
Cornelia Huck602b20f2008-01-26 14:10:39 +01001261static int io_subchannel_notify(struct subchannel *sch, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262{
1263 struct ccw_device *cdev;
1264
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001265 cdev = sch_get_cdev(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 if (!cdev)
1267 return 0;
1268 if (!cdev->drv)
1269 return 0;
1270 if (!cdev->online)
1271 return 0;
1272 return cdev->drv->notify ? cdev->drv->notify(cdev, event) : 0;
1273}
1274
Cornelia Huck602b20f2008-01-26 14:10:39 +01001275static void io_subchannel_verify(struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276{
1277 struct ccw_device *cdev;
1278
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001279 cdev = sch_get_cdev(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 if (cdev)
1281 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1282}
1283
Cornelia Huck602b20f2008-01-26 14:10:39 +01001284static void io_subchannel_ioterm(struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285{
1286 struct ccw_device *cdev;
1287
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001288 cdev = sch_get_cdev(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 if (!cdev)
1290 return;
Cornelia Huckd23861f2006-12-04 15:41:04 +01001291 /* Internal I/O will be retried by the interrupt handler. */
1292 if (cdev->private->flags.intretry)
1293 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 cdev->private->state = DEV_STATE_CLEAR_VERIFY;
1295 if (cdev->handler)
1296 cdev->handler(cdev, cdev->private->intparm,
1297 ERR_PTR(-EIO));
1298}
1299
1300static void
Cornelia Huck8bbace72006-01-11 10:56:22 +01001301io_subchannel_shutdown(struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 struct ccw_device *cdev;
1304 int ret;
1305
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001306 cdev = sch_get_cdev(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307
Cornelia Hucka8237fc2006-01-06 00:19:21 -08001308 if (cio_is_console(sch->schid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 return;
1310 if (!sch->schib.pmcw.ena)
1311 /* Nothing to do. */
1312 return;
1313 ret = cio_disable_subchannel(sch);
1314 if (ret != -EBUSY)
1315 /* Subchannel is disabled, we're done. */
1316 return;
1317 cdev->private->state = DEV_STATE_QUIESCE;
1318 if (cdev->handler)
1319 cdev->handler(cdev, cdev->private->intparm,
1320 ERR_PTR(-EIO));
1321 ret = ccw_device_cancel_halt_clear(cdev);
1322 if (ret == -EBUSY) {
1323 ccw_device_set_timeout(cdev, HZ/10);
1324 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
1325 }
1326 cio_disable_subchannel(sch);
1327}
1328
1329#ifdef CONFIG_CCW_CONSOLE
1330static struct ccw_device console_cdev;
1331static struct ccw_device_private console_private;
1332static int console_cdev_in_use;
1333
Cornelia Huck2ec22982006-12-08 15:54:26 +01001334static DEFINE_SPINLOCK(ccw_console_lock);
1335
1336spinlock_t * cio_get_console_lock(void)
1337{
1338 return &ccw_console_lock;
1339}
1340
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001341static int ccw_device_console_enable(struct ccw_device *cdev,
1342 struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343{
1344 int rc;
1345
Cornelia Huckcd6b4f22008-01-26 14:10:43 +01001346 /* Attach subchannel private data. */
1347 sch->private = cio_get_console_priv();
1348 memset(sch->private, 0, sizeof(struct io_subchannel_private));
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001349 io_subchannel_init_fields(sch);
1350 sch->driver = &io_subchannel_driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 /* Initialize the ccw_device structure. */
Heiko Carstens292888c2006-08-30 14:33:35 +02001352 cdev->dev.parent= &sch->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 rc = io_subchannel_recog(cdev, sch);
1354 if (rc)
1355 return rc;
1356
1357 /* Now wait for the async. recognition to come to an end. */
1358 spin_lock_irq(cdev->ccwlock);
1359 while (!dev_fsm_final_state(cdev))
1360 wait_cons_dev();
1361 rc = -EIO;
1362 if (cdev->private->state != DEV_STATE_OFFLINE)
1363 goto out_unlock;
1364 ccw_device_online(cdev);
1365 while (!dev_fsm_final_state(cdev))
1366 wait_cons_dev();
1367 if (cdev->private->state != DEV_STATE_ONLINE)
1368 goto out_unlock;
1369 rc = 0;
1370out_unlock:
1371 spin_unlock_irq(cdev->ccwlock);
1372 return 0;
1373}
1374
1375struct ccw_device *
1376ccw_device_probe_console(void)
1377{
1378 struct subchannel *sch;
1379 int ret;
1380
1381 if (xchg(&console_cdev_in_use, 1) != 0)
Peter Oberparleiter600b5d12006-02-01 03:06:35 -08001382 return ERR_PTR(-EBUSY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 sch = cio_probe_console();
1384 if (IS_ERR(sch)) {
1385 console_cdev_in_use = 0;
1386 return (void *) sch;
1387 }
1388 memset(&console_cdev, 0, sizeof(struct ccw_device));
1389 memset(&console_private, 0, sizeof(struct ccw_device_private));
1390 console_cdev.private = &console_private;
Martin Schwidefskyc1637532006-12-08 15:53:57 +01001391 console_private.cdev = &console_cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 ret = ccw_device_console_enable(&console_cdev, sch);
1393 if (ret) {
1394 cio_release_console();
1395 console_cdev_in_use = 0;
1396 return ERR_PTR(ret);
1397 }
1398 console_cdev.online = 1;
1399 return &console_cdev;
1400}
1401#endif
1402
1403/*
1404 * get ccw_device matching the busid, but only if owned by cdrv
1405 */
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001406static int
1407__ccwdev_check_busid(struct device *dev, void *id)
1408{
1409 char *bus_id;
1410
Cornelia Huck12975ae2006-10-11 15:31:47 +02001411 bus_id = id;
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001412
1413 return (strncmp(bus_id, dev->bus_id, BUS_ID_SIZE) == 0);
1414}
1415
1416
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +02001417/**
1418 * get_ccwdev_by_busid() - obtain device from a bus id
1419 * @cdrv: driver the device is owned by
1420 * @bus_id: bus id of the device to be searched
1421 *
1422 * This function searches all devices owned by @cdrv for a device with a bus
1423 * id matching @bus_id.
1424 * Returns:
1425 * If a match is found, its reference count of the found device is increased
1426 * and it is returned; else %NULL is returned.
1427 */
1428struct ccw_device *get_ccwdev_by_busid(struct ccw_driver *cdrv,
1429 const char *bus_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430{
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001431 struct device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 struct device_driver *drv;
1433
1434 drv = get_driver(&cdrv->driver);
1435 if (!drv)
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001436 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001438 dev = driver_find_device(drv, NULL, (void *)bus_id,
1439 __ccwdev_check_busid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 put_driver(drv);
1441
Heiko Carstensd2c993d2006-07-12 16:41:55 +02001442 return dev ? to_ccwdev(dev) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443}
1444
1445/************************** device driver handling ************************/
1446
1447/* This is the implementation of the ccw_driver class. The probe, remove
1448 * and release methods are initially very similar to the device_driver
1449 * implementations, with the difference that they have ccw_device
1450 * arguments.
1451 *
1452 * A ccw driver also contains the information that is needed for
1453 * device matching.
1454 */
1455static int
1456ccw_device_probe (struct device *dev)
1457{
1458 struct ccw_device *cdev = to_ccwdev(dev);
1459 struct ccw_driver *cdrv = to_ccwdrv(dev->driver);
1460 int ret;
1461
1462 cdev->drv = cdrv; /* to let the driver call _set_online */
1463
1464 ret = cdrv->probe ? cdrv->probe(cdev) : -ENODEV;
1465
1466 if (ret) {
Heiko Carstensd2c993d2006-07-12 16:41:55 +02001467 cdev->drv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 return ret;
1469 }
1470
1471 return 0;
1472}
1473
1474static int
1475ccw_device_remove (struct device *dev)
1476{
1477 struct ccw_device *cdev = to_ccwdev(dev);
1478 struct ccw_driver *cdrv = cdev->drv;
1479 int ret;
1480
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 if (cdrv->remove)
1482 cdrv->remove(cdev);
1483 if (cdev->online) {
1484 cdev->online = 0;
1485 spin_lock_irq(cdev->ccwlock);
1486 ret = ccw_device_offline(cdev);
1487 spin_unlock_irq(cdev->ccwlock);
1488 if (ret == 0)
1489 wait_event(cdev->private->wait_q,
1490 dev_fsm_final_state(cdev));
1491 else
Michael Ernst139b83d2008-05-07 09:22:54 +02001492 CIO_MSG_EVENT(0, "ccw_device_offline returned %d, "
Cornelia Hucke556bbb2007-07-27 12:29:19 +02001493 "device 0.%x.%04x\n",
1494 ret, cdev->private->dev_id.ssid,
1495 cdev->private->dev_id.devno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 }
1497 ccw_device_set_timeout(cdev, 0);
Heiko Carstensd2c993d2006-07-12 16:41:55 +02001498 cdev->drv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 return 0;
1500}
1501
Cornelia Huck958974fb2007-10-12 16:11:21 +02001502static void ccw_device_shutdown(struct device *dev)
1503{
1504 struct ccw_device *cdev;
1505
1506 cdev = to_ccwdev(dev);
1507 if (cdev->drv && cdev->drv->shutdown)
1508 cdev->drv->shutdown(cdev);
Cornelia Huck1842f2b2007-10-12 16:11:22 +02001509 disable_cmf(cdev);
Cornelia Huck958974fb2007-10-12 16:11:21 +02001510}
1511
Cornelia Huck8bbace72006-01-11 10:56:22 +01001512struct bus_type ccw_bus_type = {
1513 .name = "ccw",
1514 .match = ccw_bus_match,
1515 .uevent = ccw_uevent,
1516 .probe = ccw_device_probe,
1517 .remove = ccw_device_remove,
Cornelia Huck958974fb2007-10-12 16:11:21 +02001518 .shutdown = ccw_device_shutdown,
Cornelia Huck8bbace72006-01-11 10:56:22 +01001519};
1520
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +02001521/**
1522 * ccw_driver_register() - register a ccw driver
1523 * @cdriver: driver to be registered
1524 *
1525 * This function is mainly a wrapper around driver_register().
1526 * Returns:
1527 * %0 on success and a negative error value on failure.
1528 */
1529int ccw_driver_register(struct ccw_driver *cdriver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530{
1531 struct device_driver *drv = &cdriver->driver;
1532
1533 drv->bus = &ccw_bus_type;
1534 drv->name = cdriver->name;
Cornelia Huck4beee642008-01-26 14:10:47 +01001535 drv->owner = cdriver->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536
1537 return driver_register(drv);
1538}
1539
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +02001540/**
1541 * ccw_driver_unregister() - deregister a ccw driver
1542 * @cdriver: driver to be deregistered
1543 *
1544 * This function is mainly a wrapper around driver_unregister().
1545 */
1546void ccw_driver_unregister(struct ccw_driver *cdriver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547{
1548 driver_unregister(&cdriver->driver);
1549}
1550
Cornelia Hucka8237fc2006-01-06 00:19:21 -08001551/* Helper func for qdio. */
1552struct subchannel_id
1553ccw_device_get_subchannel_id(struct ccw_device *cdev)
1554{
1555 struct subchannel *sch;
1556
1557 sch = to_subchannel(cdev->dev.parent);
1558 return sch->schid;
1559}
1560
Peter Oberparleiter90ab1332008-01-26 14:10:52 +01001561static int recovery_check(struct device *dev, void *data)
1562{
1563 struct ccw_device *cdev = to_ccwdev(dev);
1564 int *redo = data;
1565
1566 spin_lock_irq(cdev->ccwlock);
1567 switch (cdev->private->state) {
1568 case DEV_STATE_DISCONNECTED:
Michael Ernst139b83d2008-05-07 09:22:54 +02001569 CIO_MSG_EVENT(4, "recovery: trigger 0.%x.%04x\n",
Peter Oberparleiter90ab1332008-01-26 14:10:52 +01001570 cdev->private->dev_id.ssid,
1571 cdev->private->dev_id.devno);
1572 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1573 *redo = 1;
1574 break;
1575 case DEV_STATE_DISCONNECTED_SENSE_ID:
1576 *redo = 1;
1577 break;
1578 }
1579 spin_unlock_irq(cdev->ccwlock);
1580
1581 return 0;
1582}
1583
Cornelia Huck4c629722008-02-19 15:29:24 +01001584static void recovery_work_func(struct work_struct *unused)
Peter Oberparleiter90ab1332008-01-26 14:10:52 +01001585{
1586 int redo = 0;
1587
1588 bus_for_each_dev(&ccw_bus_type, NULL, &redo, recovery_check);
1589 if (redo) {
1590 spin_lock_irq(&recovery_lock);
1591 if (!timer_pending(&recovery_timer)) {
1592 if (recovery_phase < ARRAY_SIZE(recovery_delay) - 1)
1593 recovery_phase++;
1594 mod_timer(&recovery_timer, jiffies +
1595 recovery_delay[recovery_phase] * HZ);
1596 }
1597 spin_unlock_irq(&recovery_lock);
1598 } else
Michael Ernst139b83d2008-05-07 09:22:54 +02001599 CIO_MSG_EVENT(4, "recovery: end\n");
Peter Oberparleiter90ab1332008-01-26 14:10:52 +01001600}
1601
Cornelia Huck4c629722008-02-19 15:29:24 +01001602static DECLARE_WORK(recovery_work, recovery_work_func);
1603
1604static void recovery_func(unsigned long data)
1605{
1606 /*
1607 * We can't do our recovery in softirq context and it's not
1608 * performance critical, so we schedule it.
1609 */
1610 schedule_work(&recovery_work);
1611}
1612
Peter Oberparleiter90ab1332008-01-26 14:10:52 +01001613void ccw_device_schedule_recovery(void)
1614{
1615 unsigned long flags;
1616
Michael Ernst139b83d2008-05-07 09:22:54 +02001617 CIO_MSG_EVENT(4, "recovery: schedule\n");
Peter Oberparleiter90ab1332008-01-26 14:10:52 +01001618 spin_lock_irqsave(&recovery_lock, flags);
1619 if (!timer_pending(&recovery_timer) || (recovery_phase != 0)) {
1620 recovery_phase = 0;
1621 mod_timer(&recovery_timer, jiffies + recovery_delay[0] * HZ);
1622 }
1623 spin_unlock_irqrestore(&recovery_lock, flags);
1624}
1625
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626MODULE_LICENSE("GPL");
1627EXPORT_SYMBOL(ccw_device_set_online);
1628EXPORT_SYMBOL(ccw_device_set_offline);
1629EXPORT_SYMBOL(ccw_driver_register);
1630EXPORT_SYMBOL(ccw_driver_unregister);
1631EXPORT_SYMBOL(get_ccwdev_by_busid);
1632EXPORT_SYMBOL(ccw_bus_type);
1633EXPORT_SYMBOL(ccw_device_work);
1634EXPORT_SYMBOL(ccw_device_notify_work);
Cornelia Hucka8237fc2006-01-06 00:19:21 -08001635EXPORT_SYMBOL_GPL(ccw_device_get_subchannel_id);