blob: 297659fa0e26c80bd077092a12909b559f233f3e [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>
20
21#include <asm/ccwdev.h>
22#include <asm/cio.h>
Tim Schmielau4e57b682005-10-30 15:03:48 -080023#include <asm/param.h> /* HZ */
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
25#include "cio.h"
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +010026#include "cio_debug.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include "css.h"
28#include "device.h"
29#include "ioasm.h"
30
31/******************* bus type handling ***********************/
32
33/* The Linux driver model distinguishes between a bus type and
34 * the bus itself. Of course we only have one channel
35 * subsystem driver and one channel system per machine, but
36 * we still use the abstraction. T.R. says it's a good idea. */
37static int
38ccw_bus_match (struct device * dev, struct device_driver * drv)
39{
40 struct ccw_device *cdev = to_ccwdev(dev);
41 struct ccw_driver *cdrv = to_ccwdrv(drv);
42 const struct ccw_device_id *ids = cdrv->ids, *found;
43
44 if (!ids)
45 return 0;
46
47 found = ccw_device_id_match(ids, &cdev->id);
48 if (!found)
49 return 0;
50
51 cdev->id.driver_info = found->driver_info;
52
53 return 1;
54}
55
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +020056/* Store modalias string delimited by prefix/suffix string into buffer with
57 * specified size. Return length of resulting string (excluding trailing '\0')
58 * even if string doesn't fit buffer (snprintf semantics). */
Cornelia Huckcfbe9bb2007-04-27 16:01:32 +020059static int snprint_alias(char *buf, size_t size,
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +020060 struct ccw_device_id *id, const char *suffix)
61{
62 int len;
63
Cornelia Huckcfbe9bb2007-04-27 16:01:32 +020064 len = snprintf(buf, size, "ccw:t%04Xm%02X", id->cu_type, id->cu_model);
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +020065 if (len > size)
66 return len;
67 buf += len;
68 size -= len;
69
70 if (id->dev_type != 0)
71 len += snprintf(buf, size, "dt%04Xdm%02X%s", id->dev_type,
72 id->dev_model, suffix);
73 else
74 len += snprintf(buf, size, "dtdm%s", suffix);
75
76 return len;
77}
78
79/* Set up environment variables for ccw device uevent. Return 0 on success,
80 * non-zero otherwise. */
81static int ccw_uevent(struct device *dev, char **envp, int num_envp,
82 char *buffer, int buffer_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -070083{
84 struct ccw_device *cdev = to_ccwdev(dev);
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +020085 struct ccw_device_id *id = &(cdev->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 int i = 0;
Cornelia Huckcfbe9bb2007-04-27 16:01:32 +020087 int len = 0;
88 int ret;
89 char modalias_buf[30];
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +020091 /* CU_TYPE= */
Cornelia Huckcfbe9bb2007-04-27 16:01:32 +020092 ret = add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &len,
93 "CU_TYPE=%04X", id->cu_type);
94 if (ret)
95 return ret;
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +020096
97 /* CU_MODEL= */
Cornelia Huckcfbe9bb2007-04-27 16:01:32 +020098 ret = add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &len,
99 "CU_MODEL=%02X", id->cu_model);
100 if (ret)
101 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
103 /* The next two can be zero, that's ok for us */
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +0200104 /* DEV_TYPE= */
Cornelia Huckcfbe9bb2007-04-27 16:01:32 +0200105 ret = add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &len,
106 "DEV_TYPE=%04X", id->dev_type);
107 if (ret)
108 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +0200110 /* DEV_MODEL= */
Cornelia Huckcfbe9bb2007-04-27 16:01:32 +0200111 ret = add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &len,
112 "DEV_MODEL=%02X", id->dev_model);
113 if (ret)
114 return ret;
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +0200115
116 /* MODALIAS= */
Cornelia Huckcfbe9bb2007-04-27 16:01:32 +0200117 snprint_alias(modalias_buf, sizeof(modalias_buf), id, "");
118 ret = add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &len,
119 "MODALIAS=%s", modalias_buf);
120 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121}
122
Cornelia Huck8bbace72006-01-11 10:56:22 +0100123struct bus_type ccw_bus_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
Cornelia Huck8bbace72006-01-11 10:56:22 +0100125static int io_subchannel_probe (struct subchannel *);
126static int io_subchannel_remove (struct subchannel *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127static int io_subchannel_notify(struct device *, int);
128static void io_subchannel_verify(struct device *);
129static void io_subchannel_ioterm(struct device *);
Cornelia Huck8bbace72006-01-11 10:56:22 +0100130static void io_subchannel_shutdown(struct subchannel *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
Cornelia Huckf7e5d672007-05-10 15:45:43 +0200132static struct css_driver io_subchannel_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 .subchannel_type = SUBCHANNEL_TYPE_IO,
134 .drv = {
135 .name = "io_subchannel",
136 .bus = &css_bus_type,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 },
138 .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
152static int __init
153init_ccw_bus_type (void)
154{
155 int ret;
156
157 init_waitqueue_head(&ccw_device_init_wq);
158 atomic_set(&ccw_device_init_count, 0);
159
160 ccw_device_work = create_singlethread_workqueue("cio");
161 if (!ccw_device_work)
162 return -ENOMEM; /* FIXME: better errno ? */
163 ccw_device_notify_work = create_singlethread_workqueue("cio_notify");
164 if (!ccw_device_notify_work) {
165 ret = -ENOMEM; /* FIXME: better errno ? */
166 goto out_err;
167 }
168 slow_path_wq = create_singlethread_workqueue("kslowcrw");
169 if (!slow_path_wq) {
170 ret = -ENOMEM; /* FIXME: better errno ? */
171 goto out_err;
172 }
173 if ((ret = bus_register (&ccw_bus_type)))
174 goto out_err;
175
176 if ((ret = driver_register(&io_subchannel_driver.drv)))
177 goto out_err;
178
179 wait_event(ccw_device_init_wq,
180 atomic_read(&ccw_device_init_count) == 0);
181 flush_workqueue(ccw_device_work);
182 return 0;
183out_err:
184 if (ccw_device_work)
185 destroy_workqueue(ccw_device_work);
186 if (ccw_device_notify_work)
187 destroy_workqueue(ccw_device_notify_work);
188 if (slow_path_wq)
189 destroy_workqueue(slow_path_wq);
190 return ret;
191}
192
193static void __exit
194cleanup_ccw_bus_type (void)
195{
196 driver_unregister(&io_subchannel_driver.drv);
197 bus_unregister(&ccw_bus_type);
198 destroy_workqueue(ccw_device_notify_work);
199 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 Huckd7b5a4c92006-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)
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200341 CIO_MSG_EVENT(2, "Couldn't unregister orphan "
342 "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)
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200351 CIO_MSG_EVENT(2, "Couldn't unregister disconnected device "
352 "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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357int
358ccw_device_set_offline(struct ccw_device *cdev)
359{
360 int ret;
361
362 if (!cdev)
363 return -ENODEV;
364 if (!cdev->online || !cdev->drv)
365 return -EINVAL;
366
367 if (cdev->drv->set_offline) {
368 ret = cdev->drv->set_offline(cdev);
369 if (ret != 0)
370 return ret;
371 }
372 cdev->online = 0;
373 spin_lock_irq(cdev->ccwlock);
374 ret = ccw_device_offline(cdev);
375 if (ret == -ENODEV) {
376 if (cdev->private->state != DEV_STATE_NOT_OPER) {
377 cdev->private->state = DEV_STATE_OFFLINE;
378 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
379 }
380 spin_unlock_irq(cdev->ccwlock);
381 return ret;
382 }
383 spin_unlock_irq(cdev->ccwlock);
384 if (ret == 0)
385 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
386 else {
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200387 CIO_MSG_EVENT(2, "ccw_device_offline returned %d, "
388 "device 0.%x.%04x\n",
389 ret, cdev->private->dev_id.ssid,
390 cdev->private->dev_id.devno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 cdev->online = 1;
392 }
393 return ret;
394}
395
396int
397ccw_device_set_online(struct ccw_device *cdev)
398{
399 int ret;
400
401 if (!cdev)
402 return -ENODEV;
403 if (cdev->online || !cdev->drv)
404 return -EINVAL;
405
406 spin_lock_irq(cdev->ccwlock);
407 ret = ccw_device_online(cdev);
408 spin_unlock_irq(cdev->ccwlock);
409 if (ret == 0)
410 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
411 else {
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200412 CIO_MSG_EVENT(2, "ccw_device_online returned %d, "
413 "device 0.%x.%04x\n",
414 ret, cdev->private->dev_id.ssid,
415 cdev->private->dev_id.devno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 return ret;
417 }
418 if (cdev->private->state != DEV_STATE_ONLINE)
419 return -ENODEV;
420 if (!cdev->drv->set_online || cdev->drv->set_online(cdev) == 0) {
421 cdev->online = 1;
422 return 0;
423 }
424 spin_lock_irq(cdev->ccwlock);
425 ret = ccw_device_offline(cdev);
426 spin_unlock_irq(cdev->ccwlock);
427 if (ret == 0)
428 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200429 else
430 CIO_MSG_EVENT(2, "ccw_device_offline returned %d, "
431 "device 0.%x.%04x\n",
432 ret, cdev->private->dev_id.ssid,
433 cdev->private->dev_id.devno);
Cornelia Huck6cadb782006-02-17 13:52:49 -0800434 return (ret == 0) ? -ENODEV : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435}
436
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200437static void online_store_handle_offline(struct ccw_device *cdev)
438{
439 if (cdev->private->state == DEV_STATE_DISCONNECTED)
440 ccw_device_remove_disconnected(cdev);
441 else if (cdev->drv && cdev->drv->set_offline)
442 ccw_device_set_offline(cdev);
443}
444
445static int online_store_recog_and_online(struct ccw_device *cdev)
446{
447 int ret;
448
449 /* Do device recognition, if needed. */
450 if (cdev->id.cu_type == 0) {
451 ret = ccw_device_recognition(cdev);
452 if (ret) {
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200453 CIO_MSG_EVENT(0, "Couldn't start recognition "
454 "for device 0.%x.%04x (ret=%d)\n",
455 cdev->private->dev_id.ssid,
456 cdev->private->dev_id.devno, ret);
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200457 return ret;
458 }
459 wait_event(cdev->private->wait_q,
460 cdev->private->flags.recog_done);
461 }
462 if (cdev->drv && cdev->drv->set_online)
463 ccw_device_set_online(cdev);
464 return 0;
465}
466static void online_store_handle_online(struct ccw_device *cdev, int force)
467{
468 int ret;
469
470 ret = online_store_recog_and_online(cdev);
471 if (ret)
472 return;
473 if (force && cdev->private->state == DEV_STATE_BOXED) {
474 ret = ccw_device_stlck(cdev);
475 if (ret) {
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200476 dev_warn(&cdev->dev,
477 "ccw_device_stlck returned %d!\n", ret);
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200478 return;
479 }
480 if (cdev->id.cu_type == 0)
481 cdev->private->state = DEV_STATE_NOT_OPER;
482 online_store_recog_and_online(cdev);
483 }
484
485}
486
487static ssize_t online_store (struct device *dev, struct device_attribute *attr,
488 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489{
490 struct ccw_device *cdev = to_ccwdev(dev);
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200491 int i, force;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 char *tmp;
493
Martin Schwidefsky973bd992006-01-06 00:19:07 -0800494 if (atomic_cmpxchg(&cdev->private->onoff, 0, 1) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 return -EAGAIN;
496
497 if (cdev->drv && !try_module_get(cdev->drv->owner)) {
498 atomic_set(&cdev->private->onoff, 0);
499 return -EINVAL;
500 }
501 if (!strncmp(buf, "force\n", count)) {
502 force = 1;
503 i = 1;
504 } else {
505 force = 0;
506 i = simple_strtoul(buf, &tmp, 16);
507 }
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200508
509 switch (i) {
510 case 0:
511 online_store_handle_offline(cdev);
512 break;
513 case 1:
514 online_store_handle_online(cdev, force);
515 break;
516 default:
517 count = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 if (cdev->drv)
520 module_put(cdev->drv->owner);
521 atomic_set(&cdev->private->onoff, 0);
522 return count;
523}
524
525static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400526available_show (struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527{
528 struct ccw_device *cdev = to_ccwdev(dev);
529 struct subchannel *sch;
530
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100531 if (ccw_device_is_orphan(cdev))
532 return sprintf(buf, "no device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 switch (cdev->private->state) {
534 case DEV_STATE_BOXED:
535 return sprintf(buf, "boxed\n");
536 case DEV_STATE_DISCONNECTED:
537 case DEV_STATE_DISCONNECTED_SENSE_ID:
538 case DEV_STATE_NOT_OPER:
539 sch = to_subchannel(dev->parent);
540 if (!sch->lpm)
541 return sprintf(buf, "no path\n");
542 else
543 return sprintf(buf, "no device\n");
544 default:
545 /* All other states considered fine. */
546 return sprintf(buf, "good\n");
547 }
548}
549
550static DEVICE_ATTR(chpids, 0444, chpids_show, NULL);
551static DEVICE_ATTR(pimpampom, 0444, pimpampom_show, NULL);
552static DEVICE_ATTR(devtype, 0444, devtype_show, NULL);
553static DEVICE_ATTR(cutype, 0444, cutype_show, NULL);
Bastian Blankf1fc78a2005-10-30 15:00:12 -0800554static DEVICE_ATTR(modalias, 0444, modalias_show, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555static DEVICE_ATTR(online, 0644, online_show, online_store);
556extern struct device_attribute dev_attr_cmb_enable;
557static DEVICE_ATTR(availability, 0444, available_show, NULL);
558
559static struct attribute * subch_attrs[] = {
560 &dev_attr_chpids.attr,
561 &dev_attr_pimpampom.attr,
562 NULL,
563};
564
565static struct attribute_group subch_attr_group = {
566 .attrs = subch_attrs,
567};
568
Cornelia Huck529192f2006-12-08 15:55:57 +0100569struct attribute_group *subch_attr_groups[] = {
570 &subch_attr_group,
571 NULL,
572};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
574static struct attribute * ccwdev_attrs[] = {
575 &dev_attr_devtype.attr,
576 &dev_attr_cutype.attr,
Bastian Blankf1fc78a2005-10-30 15:00:12 -0800577 &dev_attr_modalias.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 &dev_attr_online.attr,
579 &dev_attr_cmb_enable.attr,
580 &dev_attr_availability.attr,
581 NULL,
582};
583
584static struct attribute_group ccwdev_attr_group = {
585 .attrs = ccwdev_attrs,
586};
587
Cornelia Huckf7e5d672007-05-10 15:45:43 +0200588static struct attribute_group *ccwdev_attr_groups[] = {
Cornelia Huckef995162007-04-27 16:01:39 +0200589 &ccwdev_attr_group,
590 NULL,
591};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
593/* this is a simple abstraction for device_register that sets the
594 * correct bus type and adds the bus specific files */
Cornelia Huck3c9da7b2006-10-27 12:39:33 +0200595static int ccw_device_register(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596{
597 struct device *dev = &cdev->dev;
598 int ret;
599
600 dev->bus = &ccw_bus_type;
601
602 if ((ret = device_add(dev)))
603 return ret;
604
605 set_bit(1, &cdev->private->registered);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 return ret;
607}
608
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700609struct match_data {
Cornelia Huck78964262006-10-11 15:31:38 +0200610 struct ccw_dev_id dev_id;
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700611 struct ccw_device * sibling;
612};
613
614static int
615match_devno(struct device * dev, void * data)
616{
Cornelia Huck78964262006-10-11 15:31:38 +0200617 struct match_data * d = data;
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700618 struct ccw_device * cdev;
619
620 cdev = to_ccwdev(dev);
621 if ((cdev->private->state == DEV_STATE_DISCONNECTED) &&
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100622 !ccw_device_is_orphan(cdev) &&
Cornelia Huck78964262006-10-11 15:31:38 +0200623 ccw_dev_id_is_equal(&cdev->private->dev_id, &d->dev_id) &&
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100624 (cdev != d->sibling))
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700625 return 1;
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700626 return 0;
627}
628
Cornelia Huck78964262006-10-11 15:31:38 +0200629static struct ccw_device * get_disc_ccwdev_by_dev_id(struct ccw_dev_id *dev_id,
630 struct ccw_device *sibling)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 struct device *dev;
Heiko Carstens292888c2006-08-30 14:33:35 +0200633 struct match_data data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
Cornelia Huck78964262006-10-11 15:31:38 +0200635 data.dev_id = *dev_id;
Heiko Carstens292888c2006-08-30 14:33:35 +0200636 data.sibling = sibling;
Cornelia Hucke5945b42005-10-11 08:28:59 -0700637 dev = bus_find_device(&ccw_bus_type, NULL, &data, match_devno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700639 return dev ? to_ccwdev(dev) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640}
641
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100642static int match_orphan(struct device *dev, void *data)
643{
644 struct ccw_dev_id *dev_id;
645 struct ccw_device *cdev;
646
647 dev_id = data;
648 cdev = to_ccwdev(dev);
649 return ccw_dev_id_is_equal(&cdev->private->dev_id, dev_id);
650}
651
652static struct ccw_device *
653get_orphaned_ccwdev_by_dev_id(struct channel_subsystem *css,
654 struct ccw_dev_id *dev_id)
655{
656 struct device *dev;
657
658 dev = device_find_child(&css->pseudo_subchannel->dev, dev_id,
659 match_orphan);
660
661 return dev ? to_ccwdev(dev) : NULL;
662}
663
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664static void
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100665ccw_device_add_changed(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666{
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100667 struct ccw_device_private *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 struct ccw_device *cdev;
669
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100670 priv = container_of(work, struct ccw_device_private, kick_work);
671 cdev = priv->cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 if (device_add(&cdev->dev)) {
673 put_device(&cdev->dev);
674 return;
675 }
676 set_bit(1, &cdev->private->registered);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677}
678
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100679void ccw_device_do_unreg_rereg(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680{
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100681 struct ccw_device_private *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 struct ccw_device *cdev;
683 struct subchannel *sch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100685 priv = container_of(work, struct ccw_device_private, kick_work);
686 cdev = priv->cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 sch = to_subchannel(cdev->dev.parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
Cornelia Huckef995162007-04-27 16:01:39 +0200689 ccw_device_unregister(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 PREPARE_WORK(&cdev->private->kick_work,
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100691 ccw_device_add_changed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 queue_work(ccw_device_work, &cdev->private->kick_work);
693}
694
695static void
696ccw_device_release(struct device *dev)
697{
698 struct ccw_device *cdev;
699
700 cdev = to_ccwdev(dev);
701 kfree(cdev->private);
702 kfree(cdev);
703}
704
Cornelia Huck7674da72006-12-08 15:54:21 +0100705static struct ccw_device * io_subchannel_allocate_dev(struct subchannel *sch)
706{
707 struct ccw_device *cdev;
708
709 cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
710 if (cdev) {
711 cdev->private = kzalloc(sizeof(struct ccw_device_private),
712 GFP_KERNEL | GFP_DMA);
713 if (cdev->private)
714 return cdev;
715 }
716 kfree(cdev);
717 return ERR_PTR(-ENOMEM);
718}
719
720static int io_subchannel_initialize_dev(struct subchannel *sch,
721 struct ccw_device *cdev)
722{
723 cdev->private->cdev = cdev;
724 atomic_set(&cdev->private->onoff, 0);
725 cdev->dev.parent = &sch->dev;
726 cdev->dev.release = ccw_device_release;
727 INIT_LIST_HEAD(&cdev->private->kick_work.entry);
Cornelia Huckef995162007-04-27 16:01:39 +0200728 cdev->dev.groups = ccwdev_attr_groups;
Cornelia Huck7674da72006-12-08 15:54:21 +0100729 /* Do first half of device_register. */
730 device_initialize(&cdev->dev);
731 if (!get_device(&sch->dev)) {
732 if (cdev->dev.release)
733 cdev->dev.release(&cdev->dev);
734 return -ENODEV;
735 }
736 return 0;
737}
738
739static struct ccw_device * io_subchannel_create_ccwdev(struct subchannel *sch)
740{
741 struct ccw_device *cdev;
742 int ret;
743
744 cdev = io_subchannel_allocate_dev(sch);
745 if (!IS_ERR(cdev)) {
746 ret = io_subchannel_initialize_dev(sch, cdev);
747 if (ret) {
748 kfree(cdev);
749 cdev = ERR_PTR(ret);
750 }
751 }
752 return cdev;
753}
754
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100755static int io_subchannel_recog(struct ccw_device *, struct subchannel *);
756
757static void sch_attach_device(struct subchannel *sch,
758 struct ccw_device *cdev)
759{
Cornelia Huck82b7ac02007-04-27 16:01:36 +0200760 css_update_ssd_info(sch);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100761 spin_lock_irq(sch->lock);
762 sch->dev.driver_data = cdev;
763 cdev->private->schid = sch->schid;
764 cdev->ccwlock = sch->lock;
765 device_trigger_reprobe(sch);
766 spin_unlock_irq(sch->lock);
767}
768
769static void sch_attach_disconnected_device(struct subchannel *sch,
770 struct ccw_device *cdev)
771{
772 struct subchannel *other_sch;
773 int ret;
774
775 other_sch = to_subchannel(get_device(cdev->dev.parent));
776 ret = device_move(&cdev->dev, &sch->dev);
777 if (ret) {
778 CIO_MSG_EVENT(2, "Moving disconnected device 0.%x.%04x failed "
779 "(ret=%d)!\n", cdev->private->dev_id.ssid,
780 cdev->private->dev_id.devno, ret);
781 put_device(&other_sch->dev);
782 return;
783 }
784 other_sch->dev.driver_data = NULL;
785 /* No need to keep a subchannel without ccw device around. */
786 css_sch_device_unregister(other_sch);
787 put_device(&other_sch->dev);
788 sch_attach_device(sch, cdev);
789}
790
791static void sch_attach_orphaned_device(struct subchannel *sch,
792 struct ccw_device *cdev)
793{
794 int ret;
795
796 /* Try to move the ccw device to its new subchannel. */
797 ret = device_move(&cdev->dev, &sch->dev);
798 if (ret) {
799 CIO_MSG_EVENT(0, "Moving device 0.%x.%04x from orphanage "
800 "failed (ret=%d)!\n",
801 cdev->private->dev_id.ssid,
802 cdev->private->dev_id.devno, ret);
803 return;
804 }
805 sch_attach_device(sch, cdev);
806}
807
808static void sch_create_and_recog_new_device(struct subchannel *sch)
809{
810 struct ccw_device *cdev;
811
812 /* Need to allocate a new ccw device. */
813 cdev = io_subchannel_create_ccwdev(sch);
814 if (IS_ERR(cdev)) {
815 /* OK, we did everything we could... */
816 css_sch_device_unregister(sch);
817 return;
818 }
819 spin_lock_irq(sch->lock);
820 sch->dev.driver_data = cdev;
821 spin_unlock_irq(sch->lock);
822 /* Start recognition for the new ccw device. */
823 if (io_subchannel_recog(cdev, sch)) {
824 spin_lock_irq(sch->lock);
825 sch->dev.driver_data = NULL;
826 spin_unlock_irq(sch->lock);
827 if (cdev->dev.release)
828 cdev->dev.release(&cdev->dev);
829 css_sch_device_unregister(sch);
830 }
831}
832
833
834void ccw_device_move_to_orphanage(struct work_struct *work)
835{
836 struct ccw_device_private *priv;
837 struct ccw_device *cdev;
838 struct ccw_device *replacing_cdev;
839 struct subchannel *sch;
840 int ret;
841 struct channel_subsystem *css;
842 struct ccw_dev_id dev_id;
843
844 priv = container_of(work, struct ccw_device_private, kick_work);
845 cdev = priv->cdev;
846 sch = to_subchannel(cdev->dev.parent);
847 css = to_css(sch->dev.parent);
848 dev_id.devno = sch->schib.pmcw.dev;
849 dev_id.ssid = sch->schid.ssid;
850
851 /*
852 * Move the orphaned ccw device to the orphanage so the replacing
853 * ccw device can take its place on the subchannel.
854 */
855 ret = device_move(&cdev->dev, &css->pseudo_subchannel->dev);
856 if (ret) {
857 CIO_MSG_EVENT(0, "Moving device 0.%x.%04x to orphanage failed "
858 "(ret=%d)!\n", cdev->private->dev_id.ssid,
859 cdev->private->dev_id.devno, ret);
860 return;
861 }
862 cdev->ccwlock = css->pseudo_subchannel->lock;
863 /*
864 * Search for the replacing ccw device
865 * - among the disconnected devices
866 * - in the orphanage
867 */
868 replacing_cdev = get_disc_ccwdev_by_dev_id(&dev_id, cdev);
869 if (replacing_cdev) {
870 sch_attach_disconnected_device(sch, replacing_cdev);
871 return;
872 }
873 replacing_cdev = get_orphaned_ccwdev_by_dev_id(css, &dev_id);
874 if (replacing_cdev) {
875 sch_attach_orphaned_device(sch, replacing_cdev);
876 return;
877 }
878 sch_create_and_recog_new_device(sch);
879}
880
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881/*
882 * Register recognized device.
883 */
884static void
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100885io_subchannel_register(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886{
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100887 struct ccw_device_private *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 struct ccw_device *cdev;
889 struct subchannel *sch;
890 int ret;
891 unsigned long flags;
892
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100893 priv = container_of(work, struct ccw_device_private, kick_work);
894 cdev = priv->cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 sch = to_subchannel(cdev->dev.parent);
Cornelia Huck82b7ac02007-04-27 16:01:36 +0200896 css_update_ssd_info(sch);
Cornelia Huck47af5512006-12-04 15:41:07 +0100897 /*
898 * io_subchannel_register() will also be called after device
899 * recognition has been done for a boxed device (which will already
900 * be registered). We need to reprobe since we may now have sense id
901 * information.
902 */
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700903 if (klist_node_attached(&cdev->dev.knode_parent)) {
Cornelia Huck47af5512006-12-04 15:41:07 +0100904 if (!cdev->drv) {
905 ret = device_reprobe(&cdev->dev);
906 if (ret)
907 /* We can't do much here. */
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200908 CIO_MSG_EVENT(2, "device_reprobe() returned"
909 " %d for 0.%x.%04x\n", ret,
910 cdev->private->dev_id.ssid,
911 cdev->private->dev_id.devno);
Cornelia Huck47af5512006-12-04 15:41:07 +0100912 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 goto out;
914 }
Cornelia Huckfa1a8c22007-04-26 00:12:03 -0700915 /*
916 * Now we know this subchannel will stay, we can throw
917 * our delayed uevent.
918 */
919 sch->dev.uevent_suppress = 0;
920 kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 /* make it known to the system */
922 ret = ccw_device_register(cdev);
923 if (ret) {
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200924 CIO_MSG_EVENT(0, "Could not register ccw dev 0.%x.%04x: %d\n",
925 cdev->private->dev_id.ssid,
926 cdev->private->dev_id.devno, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 put_device(&cdev->dev);
Cornelia Huck2ec22982006-12-08 15:54:26 +0100928 spin_lock_irqsave(sch->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 sch->dev.driver_data = NULL;
Cornelia Huck2ec22982006-12-08 15:54:26 +0100930 spin_unlock_irqrestore(sch->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 kfree (cdev->private);
932 kfree (cdev);
933 put_device(&sch->dev);
934 if (atomic_dec_and_test(&ccw_device_init_count))
935 wake_up(&ccw_device_init_wq);
936 return;
937 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 put_device(&cdev->dev);
939out:
940 cdev->private->flags.recog_done = 1;
941 put_device(&sch->dev);
942 wake_up(&cdev->private->wait_q);
943 if (atomic_dec_and_test(&ccw_device_init_count))
944 wake_up(&ccw_device_init_wq);
945}
946
947void
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100948ccw_device_call_sch_unregister(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949{
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100950 struct ccw_device_private *priv;
951 struct ccw_device *cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 struct subchannel *sch;
953
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100954 priv = container_of(work, struct ccw_device_private, kick_work);
955 cdev = priv->cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 sch = to_subchannel(cdev->dev.parent);
Cornelia Huck6ab48792006-07-12 16:39:50 +0200957 css_sch_device_unregister(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 /* Reset intparm to zeroes. */
959 sch->schib.pmcw.intparm = 0;
960 cio_modify(sch);
961 put_device(&cdev->dev);
962 put_device(&sch->dev);
963}
964
965/*
966 * subchannel recognition done. Called from the state machine.
967 */
968void
969io_subchannel_recog_done(struct ccw_device *cdev)
970{
971 struct subchannel *sch;
972
973 if (css_init_done == 0) {
974 cdev->private->flags.recog_done = 1;
975 return;
976 }
977 switch (cdev->private->state) {
978 case DEV_STATE_NOT_OPER:
979 cdev->private->flags.recog_done = 1;
980 /* Remove device found not operational. */
981 if (!get_device(&cdev->dev))
982 break;
983 sch = to_subchannel(cdev->dev.parent);
984 PREPARE_WORK(&cdev->private->kick_work,
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100985 ccw_device_call_sch_unregister);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 queue_work(slow_path_wq, &cdev->private->kick_work);
987 if (atomic_dec_and_test(&ccw_device_init_count))
988 wake_up(&ccw_device_init_wq);
989 break;
990 case DEV_STATE_BOXED:
991 /* Device did not respond in time. */
992 case DEV_STATE_OFFLINE:
993 /*
994 * We can't register the device in interrupt context so
995 * we schedule a work item.
996 */
997 if (!get_device(&cdev->dev))
998 break;
999 PREPARE_WORK(&cdev->private->kick_work,
Martin Schwidefskyc1637532006-12-08 15:53:57 +01001000 io_subchannel_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 queue_work(slow_path_wq, &cdev->private->kick_work);
1002 break;
1003 }
1004}
1005
1006static int
1007io_subchannel_recog(struct ccw_device *cdev, struct subchannel *sch)
1008{
1009 int rc;
1010 struct ccw_device_private *priv;
1011
1012 sch->dev.driver_data = cdev;
1013 sch->driver = &io_subchannel_driver;
Cornelia Huck2ec22982006-12-08 15:54:26 +01001014 cdev->ccwlock = sch->lock;
Cornelia Huckfb6958a2006-01-06 00:19:25 -08001015
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 /* Init private data. */
1017 priv = cdev->private;
Cornelia Huck78964262006-10-11 15:31:38 +02001018 priv->dev_id.devno = sch->schib.pmcw.dev;
1019 priv->dev_id.ssid = sch->schid.ssid;
1020 priv->schid = sch->schid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 priv->state = DEV_STATE_NOT_OPER;
1022 INIT_LIST_HEAD(&priv->cmb_list);
1023 init_waitqueue_head(&priv->wait_q);
1024 init_timer(&priv->timer);
1025
1026 /* Set an initial name for the device. */
Cornelia Huckfb6958a2006-01-06 00:19:25 -08001027 snprintf (cdev->dev.bus_id, BUS_ID_SIZE, "0.%x.%04x",
1028 sch->schid.ssid, sch->schib.pmcw.dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029
1030 /* Increase counter of devices currently in recognition. */
1031 atomic_inc(&ccw_device_init_count);
1032
1033 /* Start async. device sensing. */
Cornelia Huck2ec22982006-12-08 15:54:26 +01001034 spin_lock_irq(sch->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 rc = ccw_device_recognition(cdev);
Cornelia Huck2ec22982006-12-08 15:54:26 +01001036 spin_unlock_irq(sch->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 if (rc) {
1038 if (atomic_dec_and_test(&ccw_device_init_count))
1039 wake_up(&ccw_device_init_wq);
1040 }
1041 return rc;
1042}
1043
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +01001044static void ccw_device_move_to_sch(struct work_struct *work)
1045{
1046 struct ccw_device_private *priv;
1047 int rc;
1048 struct subchannel *sch;
1049 struct ccw_device *cdev;
1050 struct subchannel *former_parent;
1051
1052 priv = container_of(work, struct ccw_device_private, kick_work);
1053 sch = priv->sch;
1054 cdev = priv->cdev;
1055 former_parent = ccw_device_is_orphan(cdev) ?
1056 NULL : to_subchannel(get_device(cdev->dev.parent));
1057 mutex_lock(&sch->reg_mutex);
1058 /* Try to move the ccw device to its new subchannel. */
1059 rc = device_move(&cdev->dev, &sch->dev);
1060 mutex_unlock(&sch->reg_mutex);
1061 if (rc) {
1062 CIO_MSG_EVENT(2, "Moving device 0.%x.%04x to subchannel "
1063 "0.%x.%04x failed (ret=%d)!\n",
1064 cdev->private->dev_id.ssid,
1065 cdev->private->dev_id.devno, sch->schid.ssid,
1066 sch->schid.sch_no, rc);
1067 css_sch_device_unregister(sch);
1068 goto out;
1069 }
1070 if (former_parent) {
1071 spin_lock_irq(former_parent->lock);
1072 former_parent->dev.driver_data = NULL;
1073 spin_unlock_irq(former_parent->lock);
1074 css_sch_device_unregister(former_parent);
1075 /* Reset intparm to zeroes. */
1076 former_parent->schib.pmcw.intparm = 0;
1077 cio_modify(former_parent);
1078 }
1079 sch_attach_device(sch, cdev);
1080out:
1081 if (former_parent)
1082 put_device(&former_parent->dev);
1083 put_device(&cdev->dev);
1084}
1085
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086static int
Cornelia Huck8bbace72006-01-11 10:56:22 +01001087io_subchannel_probe (struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 struct ccw_device *cdev;
1090 int rc;
1091 unsigned long flags;
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +01001092 struct ccw_dev_id dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 if (sch->dev.driver_data) {
1095 /*
1096 * This subchannel already has an associated ccw_device.
1097 * Register it and exit. This happens for all early
1098 * device, e.g. the console.
1099 */
1100 cdev = sch->dev.driver_data;
1101 device_initialize(&cdev->dev);
1102 ccw_device_register(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 /*
1104 * Check if the device is already online. If it is
1105 * the reference count needs to be corrected
1106 * (see ccw_device_online and css_init_done for the
1107 * ugly details).
1108 */
1109 if (cdev->private->state != DEV_STATE_NOT_OPER &&
1110 cdev->private->state != DEV_STATE_OFFLINE &&
1111 cdev->private->state != DEV_STATE_BOXED)
1112 get_device(&cdev->dev);
1113 return 0;
1114 }
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +01001115 /*
1116 * First check if a fitting device may be found amongst the
1117 * disconnected devices or in the orphanage.
1118 */
1119 dev_id.devno = sch->schib.pmcw.dev;
1120 dev_id.ssid = sch->schid.ssid;
1121 cdev = get_disc_ccwdev_by_dev_id(&dev_id, NULL);
1122 if (!cdev)
1123 cdev = get_orphaned_ccwdev_by_dev_id(to_css(sch->dev.parent),
1124 &dev_id);
1125 if (cdev) {
1126 /*
1127 * Schedule moving the device until when we have a registered
1128 * subchannel to move to and succeed the probe. We can
1129 * unregister later again, when the probe is through.
1130 */
1131 cdev->private->sch = sch;
1132 PREPARE_WORK(&cdev->private->kick_work,
1133 ccw_device_move_to_sch);
1134 queue_work(slow_path_wq, &cdev->private->kick_work);
1135 return 0;
1136 }
Cornelia Huck7674da72006-12-08 15:54:21 +01001137 cdev = io_subchannel_create_ccwdev(sch);
1138 if (IS_ERR(cdev))
1139 return PTR_ERR(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140
Cornelia Huck8bbace72006-01-11 10:56:22 +01001141 rc = io_subchannel_recog(cdev, sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 if (rc) {
Cornelia Huck2ec22982006-12-08 15:54:26 +01001143 spin_lock_irqsave(sch->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 sch->dev.driver_data = NULL;
Cornelia Huck2ec22982006-12-08 15:54:26 +01001145 spin_unlock_irqrestore(sch->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 if (cdev->dev.release)
1147 cdev->dev.release(&cdev->dev);
1148 }
1149
1150 return rc;
1151}
1152
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153static int
Cornelia Huck8bbace72006-01-11 10:56:22 +01001154io_subchannel_remove (struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155{
1156 struct ccw_device *cdev;
1157 unsigned long flags;
1158
Cornelia Huck8bbace72006-01-11 10:56:22 +01001159 if (!sch->dev.driver_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 return 0;
Cornelia Huck8bbace72006-01-11 10:56:22 +01001161 cdev = sch->dev.driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 /* Set ccw device to not operational and drop reference. */
1163 spin_lock_irqsave(cdev->ccwlock, flags);
Cornelia Huck8bbace72006-01-11 10:56:22 +01001164 sch->dev.driver_data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 cdev->private->state = DEV_STATE_NOT_OPER;
1166 spin_unlock_irqrestore(cdev->ccwlock, flags);
Cornelia Huckef995162007-04-27 16:01:39 +02001167 ccw_device_unregister(cdev);
1168 put_device(&cdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 return 0;
1170}
1171
1172static int
1173io_subchannel_notify(struct device *dev, int event)
1174{
1175 struct ccw_device *cdev;
1176
1177 cdev = dev->driver_data;
1178 if (!cdev)
1179 return 0;
1180 if (!cdev->drv)
1181 return 0;
1182 if (!cdev->online)
1183 return 0;
1184 return cdev->drv->notify ? cdev->drv->notify(cdev, event) : 0;
1185}
1186
1187static void
1188io_subchannel_verify(struct device *dev)
1189{
1190 struct ccw_device *cdev;
1191
1192 cdev = dev->driver_data;
1193 if (cdev)
1194 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1195}
1196
1197static void
1198io_subchannel_ioterm(struct device *dev)
1199{
1200 struct ccw_device *cdev;
1201
1202 cdev = dev->driver_data;
1203 if (!cdev)
1204 return;
Cornelia Huckd23861f2006-12-04 15:41:04 +01001205 /* Internal I/O will be retried by the interrupt handler. */
1206 if (cdev->private->flags.intretry)
1207 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 cdev->private->state = DEV_STATE_CLEAR_VERIFY;
1209 if (cdev->handler)
1210 cdev->handler(cdev, cdev->private->intparm,
1211 ERR_PTR(-EIO));
1212}
1213
1214static void
Cornelia Huck8bbace72006-01-11 10:56:22 +01001215io_subchannel_shutdown(struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 struct ccw_device *cdev;
1218 int ret;
1219
Cornelia Huck8bbace72006-01-11 10:56:22 +01001220 cdev = sch->dev.driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221
Cornelia Hucka8237fc2006-01-06 00:19:21 -08001222 if (cio_is_console(sch->schid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 return;
1224 if (!sch->schib.pmcw.ena)
1225 /* Nothing to do. */
1226 return;
1227 ret = cio_disable_subchannel(sch);
1228 if (ret != -EBUSY)
1229 /* Subchannel is disabled, we're done. */
1230 return;
1231 cdev->private->state = DEV_STATE_QUIESCE;
1232 if (cdev->handler)
1233 cdev->handler(cdev, cdev->private->intparm,
1234 ERR_PTR(-EIO));
1235 ret = ccw_device_cancel_halt_clear(cdev);
1236 if (ret == -EBUSY) {
1237 ccw_device_set_timeout(cdev, HZ/10);
1238 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
1239 }
1240 cio_disable_subchannel(sch);
1241}
1242
1243#ifdef CONFIG_CCW_CONSOLE
1244static struct ccw_device console_cdev;
1245static struct ccw_device_private console_private;
1246static int console_cdev_in_use;
1247
Cornelia Huck2ec22982006-12-08 15:54:26 +01001248static DEFINE_SPINLOCK(ccw_console_lock);
1249
1250spinlock_t * cio_get_console_lock(void)
1251{
1252 return &ccw_console_lock;
1253}
1254
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255static int
1256ccw_device_console_enable (struct ccw_device *cdev, struct subchannel *sch)
1257{
1258 int rc;
1259
1260 /* Initialize the ccw_device structure. */
Heiko Carstens292888c2006-08-30 14:33:35 +02001261 cdev->dev.parent= &sch->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 rc = io_subchannel_recog(cdev, sch);
1263 if (rc)
1264 return rc;
1265
1266 /* Now wait for the async. recognition to come to an end. */
1267 spin_lock_irq(cdev->ccwlock);
1268 while (!dev_fsm_final_state(cdev))
1269 wait_cons_dev();
1270 rc = -EIO;
1271 if (cdev->private->state != DEV_STATE_OFFLINE)
1272 goto out_unlock;
1273 ccw_device_online(cdev);
1274 while (!dev_fsm_final_state(cdev))
1275 wait_cons_dev();
1276 if (cdev->private->state != DEV_STATE_ONLINE)
1277 goto out_unlock;
1278 rc = 0;
1279out_unlock:
1280 spin_unlock_irq(cdev->ccwlock);
1281 return 0;
1282}
1283
1284struct ccw_device *
1285ccw_device_probe_console(void)
1286{
1287 struct subchannel *sch;
1288 int ret;
1289
1290 if (xchg(&console_cdev_in_use, 1) != 0)
Peter Oberparleiter600b5d12006-02-01 03:06:35 -08001291 return ERR_PTR(-EBUSY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 sch = cio_probe_console();
1293 if (IS_ERR(sch)) {
1294 console_cdev_in_use = 0;
1295 return (void *) sch;
1296 }
1297 memset(&console_cdev, 0, sizeof(struct ccw_device));
1298 memset(&console_private, 0, sizeof(struct ccw_device_private));
1299 console_cdev.private = &console_private;
Martin Schwidefskyc1637532006-12-08 15:53:57 +01001300 console_private.cdev = &console_cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 ret = ccw_device_console_enable(&console_cdev, sch);
1302 if (ret) {
1303 cio_release_console();
1304 console_cdev_in_use = 0;
1305 return ERR_PTR(ret);
1306 }
1307 console_cdev.online = 1;
1308 return &console_cdev;
1309}
1310#endif
1311
1312/*
1313 * get ccw_device matching the busid, but only if owned by cdrv
1314 */
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001315static int
1316__ccwdev_check_busid(struct device *dev, void *id)
1317{
1318 char *bus_id;
1319
Cornelia Huck12975ae2006-10-11 15:31:47 +02001320 bus_id = id;
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001321
1322 return (strncmp(bus_id, dev->bus_id, BUS_ID_SIZE) == 0);
1323}
1324
1325
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326struct ccw_device *
1327get_ccwdev_by_busid(struct ccw_driver *cdrv, const char *bus_id)
1328{
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001329 struct device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 struct device_driver *drv;
1331
1332 drv = get_driver(&cdrv->driver);
1333 if (!drv)
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001334 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001336 dev = driver_find_device(drv, NULL, (void *)bus_id,
1337 __ccwdev_check_busid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 put_driver(drv);
1339
Heiko Carstensd2c993d2006-07-12 16:41:55 +02001340 return dev ? to_ccwdev(dev) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341}
1342
1343/************************** device driver handling ************************/
1344
1345/* This is the implementation of the ccw_driver class. The probe, remove
1346 * and release methods are initially very similar to the device_driver
1347 * implementations, with the difference that they have ccw_device
1348 * arguments.
1349 *
1350 * A ccw driver also contains the information that is needed for
1351 * device matching.
1352 */
1353static int
1354ccw_device_probe (struct device *dev)
1355{
1356 struct ccw_device *cdev = to_ccwdev(dev);
1357 struct ccw_driver *cdrv = to_ccwdrv(dev->driver);
1358 int ret;
1359
1360 cdev->drv = cdrv; /* to let the driver call _set_online */
1361
1362 ret = cdrv->probe ? cdrv->probe(cdev) : -ENODEV;
1363
1364 if (ret) {
Heiko Carstensd2c993d2006-07-12 16:41:55 +02001365 cdev->drv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 return ret;
1367 }
1368
1369 return 0;
1370}
1371
1372static int
1373ccw_device_remove (struct device *dev)
1374{
1375 struct ccw_device *cdev = to_ccwdev(dev);
1376 struct ccw_driver *cdrv = cdev->drv;
1377 int ret;
1378
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 if (cdrv->remove)
1380 cdrv->remove(cdev);
1381 if (cdev->online) {
1382 cdev->online = 0;
1383 spin_lock_irq(cdev->ccwlock);
1384 ret = ccw_device_offline(cdev);
1385 spin_unlock_irq(cdev->ccwlock);
1386 if (ret == 0)
1387 wait_event(cdev->private->wait_q,
1388 dev_fsm_final_state(cdev));
1389 else
1390 //FIXME: we can't fail!
Cornelia Hucke556bbb2007-07-27 12:29:19 +02001391 CIO_MSG_EVENT(2, "ccw_device_offline returned %d, "
1392 "device 0.%x.%04x\n",
1393 ret, cdev->private->dev_id.ssid,
1394 cdev->private->dev_id.devno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 }
1396 ccw_device_set_timeout(cdev, 0);
Heiko Carstensd2c993d2006-07-12 16:41:55 +02001397 cdev->drv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 return 0;
1399}
1400
Cornelia Huck8bbace72006-01-11 10:56:22 +01001401struct bus_type ccw_bus_type = {
1402 .name = "ccw",
1403 .match = ccw_bus_match,
1404 .uevent = ccw_uevent,
1405 .probe = ccw_device_probe,
1406 .remove = ccw_device_remove,
1407};
1408
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409int
1410ccw_driver_register (struct ccw_driver *cdriver)
1411{
1412 struct device_driver *drv = &cdriver->driver;
1413
1414 drv->bus = &ccw_bus_type;
1415 drv->name = cdriver->name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416
1417 return driver_register(drv);
1418}
1419
1420void
1421ccw_driver_unregister (struct ccw_driver *cdriver)
1422{
1423 driver_unregister(&cdriver->driver);
1424}
1425
Cornelia Hucka8237fc2006-01-06 00:19:21 -08001426/* Helper func for qdio. */
1427struct subchannel_id
1428ccw_device_get_subchannel_id(struct ccw_device *cdev)
1429{
1430 struct subchannel *sch;
1431
1432 sch = to_subchannel(cdev->dev.parent);
1433 return sch->schid;
1434}
1435
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436MODULE_LICENSE("GPL");
1437EXPORT_SYMBOL(ccw_device_set_online);
1438EXPORT_SYMBOL(ccw_device_set_offline);
1439EXPORT_SYMBOL(ccw_driver_register);
1440EXPORT_SYMBOL(ccw_driver_unregister);
1441EXPORT_SYMBOL(get_ccwdev_by_busid);
1442EXPORT_SYMBOL(ccw_bus_type);
1443EXPORT_SYMBOL(ccw_device_work);
1444EXPORT_SYMBOL(ccw_device_notify_work);
Cornelia Hucka8237fc2006-01-06 00:19:21 -08001445EXPORT_SYMBOL_GPL(ccw_device_get_subchannel_id);