blob: 606bb53e9faef9ad8fba8320a6c624b06965cff7 [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. */
Kay Sievers7eff2e72007-08-14 15:15:12 +020081static int ccw_uevent(struct device *dev, struct kobj_uevent_env *env)
Linus Torvalds1da177e2005-04-16 15:20:36 -070082{
83 struct ccw_device *cdev = to_ccwdev(dev);
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +020084 struct ccw_device_id *id = &(cdev->id);
Cornelia Huckcfbe9bb2007-04-27 16:01:32 +020085 int ret;
86 char modalias_buf[30];
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +020088 /* CU_TYPE= */
Kay Sievers7eff2e72007-08-14 15:15:12 +020089 ret = add_uevent_var(env, "CU_TYPE=%04X", id->cu_type);
Cornelia Huckcfbe9bb2007-04-27 16:01:32 +020090 if (ret)
91 return ret;
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +020092
93 /* CU_MODEL= */
Kay Sievers7eff2e72007-08-14 15:15:12 +020094 ret = add_uevent_var(env, "CU_MODEL=%02X", id->cu_model);
Cornelia Huckcfbe9bb2007-04-27 16:01:32 +020095 if (ret)
96 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
98 /* The next two can be zero, that's ok for us */
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +020099 /* DEV_TYPE= */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200100 ret = add_uevent_var(env, "DEV_TYPE=%04X", id->dev_type);
Cornelia Huckcfbe9bb2007-04-27 16:01:32 +0200101 if (ret)
102 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +0200104 /* DEV_MODEL= */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200105 ret = add_uevent_var(env, "DEV_MODEL=%02X", id->dev_model);
Cornelia Huckcfbe9bb2007-04-27 16:01:32 +0200106 if (ret)
107 return ret;
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +0200108
109 /* MODALIAS= */
Cornelia Huckcfbe9bb2007-04-27 16:01:32 +0200110 snprint_alias(modalias_buf, sizeof(modalias_buf), id, "");
Kay Sievers7eff2e72007-08-14 15:15:12 +0200111 ret = add_uevent_var(env, "MODALIAS=%s", modalias_buf);
112 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113}
114
Cornelia Huck8bbace72006-01-11 10:56:22 +0100115struct bus_type ccw_bus_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
Cornelia Huck8bbace72006-01-11 10:56:22 +0100117static int io_subchannel_probe (struct subchannel *);
118static int io_subchannel_remove (struct subchannel *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119static int io_subchannel_notify(struct device *, int);
120static void io_subchannel_verify(struct device *);
121static void io_subchannel_ioterm(struct device *);
Cornelia Huck8bbace72006-01-11 10:56:22 +0100122static void io_subchannel_shutdown(struct subchannel *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
Cornelia Huckf7e5d672007-05-10 15:45:43 +0200124static struct css_driver io_subchannel_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 .subchannel_type = SUBCHANNEL_TYPE_IO,
126 .drv = {
127 .name = "io_subchannel",
128 .bus = &css_bus_type,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 },
130 .irq = io_subchannel_irq,
131 .notify = io_subchannel_notify,
132 .verify = io_subchannel_verify,
133 .termination = io_subchannel_ioterm,
Cornelia Huck8bbace72006-01-11 10:56:22 +0100134 .probe = io_subchannel_probe,
135 .remove = io_subchannel_remove,
136 .shutdown = io_subchannel_shutdown,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137};
138
139struct workqueue_struct *ccw_device_work;
140struct workqueue_struct *ccw_device_notify_work;
Peter Oberparleiter40154b82006-06-29 14:57:03 +0200141wait_queue_head_t ccw_device_init_wq;
142atomic_t ccw_device_init_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
144static int __init
145init_ccw_bus_type (void)
146{
147 int ret;
148
149 init_waitqueue_head(&ccw_device_init_wq);
150 atomic_set(&ccw_device_init_count, 0);
151
152 ccw_device_work = create_singlethread_workqueue("cio");
153 if (!ccw_device_work)
154 return -ENOMEM; /* FIXME: better errno ? */
155 ccw_device_notify_work = create_singlethread_workqueue("cio_notify");
156 if (!ccw_device_notify_work) {
157 ret = -ENOMEM; /* FIXME: better errno ? */
158 goto out_err;
159 }
160 slow_path_wq = create_singlethread_workqueue("kslowcrw");
161 if (!slow_path_wq) {
162 ret = -ENOMEM; /* FIXME: better errno ? */
163 goto out_err;
164 }
165 if ((ret = bus_register (&ccw_bus_type)))
166 goto out_err;
167
168 if ((ret = driver_register(&io_subchannel_driver.drv)))
169 goto out_err;
170
171 wait_event(ccw_device_init_wq,
172 atomic_read(&ccw_device_init_count) == 0);
173 flush_workqueue(ccw_device_work);
174 return 0;
175out_err:
176 if (ccw_device_work)
177 destroy_workqueue(ccw_device_work);
178 if (ccw_device_notify_work)
179 destroy_workqueue(ccw_device_notify_work);
180 if (slow_path_wq)
181 destroy_workqueue(slow_path_wq);
182 return ret;
183}
184
185static void __exit
186cleanup_ccw_bus_type (void)
187{
188 driver_unregister(&io_subchannel_driver.drv);
189 bus_unregister(&ccw_bus_type);
190 destroy_workqueue(ccw_device_notify_work);
191 destroy_workqueue(ccw_device_work);
192}
193
194subsys_initcall(init_ccw_bus_type);
195module_exit(cleanup_ccw_bus_type);
196
197/************************ device handling **************************/
198
199/*
200 * A ccw_device has some interfaces in sysfs in addition to the
201 * standard ones.
202 * The following entries are designed to export the information which
203 * resided in 2.4 in /proc/subchannels. Subchannel and device number
204 * are obvious, so they don't have an entry :)
205 * TODO: Split chpids and pimpampom up? Where is "in use" in the tree?
206 */
207static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400208chpids_show (struct device * dev, struct device_attribute *attr, char * buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209{
210 struct subchannel *sch = to_subchannel(dev);
Peter Oberparleiter7ad6a242007-04-27 16:01:35 +0200211 struct chsc_ssd_info *ssd = &sch->ssd_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 ssize_t ret = 0;
213 int chp;
Peter Oberparleiter7ad6a242007-04-27 16:01:35 +0200214 int mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
Peter Oberparleiter7ad6a242007-04-27 16:01:35 +0200216 for (chp = 0; chp < 8; chp++) {
217 mask = 0x80 >> chp;
218 if (ssd->path_mask & mask)
219 ret += sprintf(buf + ret, "%02x ", ssd->chpid[chp].id);
220 else
221 ret += sprintf(buf + ret, "00 ");
222 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 ret += sprintf (buf+ret, "\n");
224 return min((ssize_t)PAGE_SIZE, ret);
225}
226
227static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400228pimpampom_show (struct device * dev, struct device_attribute *attr, char * buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229{
230 struct subchannel *sch = to_subchannel(dev);
231 struct pmcw *pmcw = &sch->schib.pmcw;
232
233 return sprintf (buf, "%02x %02x %02x\n",
234 pmcw->pim, pmcw->pam, pmcw->pom);
235}
236
237static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400238devtype_show (struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239{
240 struct ccw_device *cdev = to_ccwdev(dev);
241 struct ccw_device_id *id = &(cdev->id);
242
243 if (id->dev_type != 0)
244 return sprintf(buf, "%04x/%02x\n",
245 id->dev_type, id->dev_model);
246 else
247 return sprintf(buf, "n/a\n");
248}
249
250static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400251cutype_show (struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252{
253 struct ccw_device *cdev = to_ccwdev(dev);
254 struct ccw_device_id *id = &(cdev->id);
255
256 return sprintf(buf, "%04x/%02x\n",
257 id->cu_type, id->cu_model);
258}
259
260static ssize_t
Bastian Blankf1fc78a2005-10-30 15:00:12 -0800261modalias_show (struct device *dev, struct device_attribute *attr, char *buf)
262{
263 struct ccw_device *cdev = to_ccwdev(dev);
264 struct ccw_device_id *id = &(cdev->id);
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +0200265 int len;
Bastian Blankf1fc78a2005-10-30 15:00:12 -0800266
Cornelia Huck086a6c62007-07-17 13:36:08 +0200267 len = snprint_alias(buf, PAGE_SIZE, id, "\n");
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +0200268
269 return len > PAGE_SIZE ? PAGE_SIZE : len;
Bastian Blankf1fc78a2005-10-30 15:00:12 -0800270}
271
272static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400273online_show (struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274{
275 struct ccw_device *cdev = to_ccwdev(dev);
276
277 return sprintf(buf, cdev->online ? "1\n" : "0\n");
278}
279
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100280int ccw_device_is_orphan(struct ccw_device *cdev)
281{
282 return sch_is_pseudo_sch(to_subchannel(cdev->dev.parent));
283}
284
Cornelia Huckef995162007-04-27 16:01:39 +0200285static void ccw_device_unregister(struct ccw_device *cdev)
Cornelia Huck7674da72006-12-08 15:54:21 +0100286{
Cornelia Huck7674da72006-12-08 15:54:21 +0100287 if (test_and_clear_bit(1, &cdev->private->registered))
Cornelia Huckef995162007-04-27 16:01:39 +0200288 device_del(&cdev->dev);
Cornelia Huck7674da72006-12-08 15:54:21 +0100289}
290
Cornelia Huck59a8a6e2007-05-31 17:38:06 +0200291static void ccw_device_remove_orphan_cb(struct device *dev)
292{
293 struct ccw_device *cdev = to_ccwdev(dev);
294
295 ccw_device_unregister(cdev);
296 put_device(&cdev->dev);
297}
298
299static void ccw_device_remove_sch_cb(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300{
301 struct subchannel *sch;
Cornelia Huck59a8a6e2007-05-31 17:38:06 +0200302
303 sch = to_subchannel(dev);
Cornelia Huck6ab48792006-07-12 16:39:50 +0200304 css_sch_device_unregister(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 /* Reset intparm to zeroes. */
306 sch->schib.pmcw.intparm = 0;
307 cio_modify(sch);
308 put_device(&sch->dev);
309}
310
Cornelia Huck59a8a6e2007-05-31 17:38:06 +0200311static void
312ccw_device_remove_disconnected(struct ccw_device *cdev)
313{
314 unsigned long flags;
315 int rc;
316
317 /*
318 * Forced offline in disconnected state means
319 * 'throw away device'.
320 */
321 if (ccw_device_is_orphan(cdev)) {
322 /*
323 * Deregister ccw device.
324 * Unfortunately, we cannot do this directly from the
325 * attribute method.
326 */
327 spin_lock_irqsave(cdev->ccwlock, flags);
328 cdev->private->state = DEV_STATE_NOT_OPER;
329 spin_unlock_irqrestore(cdev->ccwlock, flags);
330 rc = device_schedule_callback(&cdev->dev,
331 ccw_device_remove_orphan_cb);
332 if (rc)
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200333 CIO_MSG_EVENT(2, "Couldn't unregister orphan "
334 "0.%x.%04x\n",
335 cdev->private->dev_id.ssid,
336 cdev->private->dev_id.devno);
Cornelia Huck59a8a6e2007-05-31 17:38:06 +0200337 return;
338 }
339 /* Deregister subchannel, which will kill the ccw device. */
340 rc = device_schedule_callback(cdev->dev.parent,
341 ccw_device_remove_sch_cb);
342 if (rc)
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200343 CIO_MSG_EVENT(2, "Couldn't unregister disconnected device "
344 "0.%x.%04x\n",
345 cdev->private->dev_id.ssid,
346 cdev->private->dev_id.devno);
Cornelia Huck59a8a6e2007-05-31 17:38:06 +0200347}
348
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349int
350ccw_device_set_offline(struct ccw_device *cdev)
351{
352 int ret;
353
354 if (!cdev)
355 return -ENODEV;
356 if (!cdev->online || !cdev->drv)
357 return -EINVAL;
358
359 if (cdev->drv->set_offline) {
360 ret = cdev->drv->set_offline(cdev);
361 if (ret != 0)
362 return ret;
363 }
364 cdev->online = 0;
365 spin_lock_irq(cdev->ccwlock);
366 ret = ccw_device_offline(cdev);
367 if (ret == -ENODEV) {
368 if (cdev->private->state != DEV_STATE_NOT_OPER) {
369 cdev->private->state = DEV_STATE_OFFLINE;
370 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
371 }
372 spin_unlock_irq(cdev->ccwlock);
373 return ret;
374 }
375 spin_unlock_irq(cdev->ccwlock);
376 if (ret == 0)
377 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
378 else {
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200379 CIO_MSG_EVENT(2, "ccw_device_offline returned %d, "
380 "device 0.%x.%04x\n",
381 ret, cdev->private->dev_id.ssid,
382 cdev->private->dev_id.devno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 cdev->online = 1;
384 }
385 return ret;
386}
387
388int
389ccw_device_set_online(struct ccw_device *cdev)
390{
391 int ret;
392
393 if (!cdev)
394 return -ENODEV;
395 if (cdev->online || !cdev->drv)
396 return -EINVAL;
397
398 spin_lock_irq(cdev->ccwlock);
399 ret = ccw_device_online(cdev);
400 spin_unlock_irq(cdev->ccwlock);
401 if (ret == 0)
402 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
403 else {
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200404 CIO_MSG_EVENT(2, "ccw_device_online returned %d, "
405 "device 0.%x.%04x\n",
406 ret, cdev->private->dev_id.ssid,
407 cdev->private->dev_id.devno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 return ret;
409 }
410 if (cdev->private->state != DEV_STATE_ONLINE)
411 return -ENODEV;
412 if (!cdev->drv->set_online || cdev->drv->set_online(cdev) == 0) {
413 cdev->online = 1;
414 return 0;
415 }
416 spin_lock_irq(cdev->ccwlock);
417 ret = ccw_device_offline(cdev);
418 spin_unlock_irq(cdev->ccwlock);
419 if (ret == 0)
420 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200421 else
422 CIO_MSG_EVENT(2, "ccw_device_offline returned %d, "
423 "device 0.%x.%04x\n",
424 ret, cdev->private->dev_id.ssid,
425 cdev->private->dev_id.devno);
Cornelia Huck6cadb782006-02-17 13:52:49 -0800426 return (ret == 0) ? -ENODEV : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427}
428
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200429static void online_store_handle_offline(struct ccw_device *cdev)
430{
431 if (cdev->private->state == DEV_STATE_DISCONNECTED)
432 ccw_device_remove_disconnected(cdev);
433 else if (cdev->drv && cdev->drv->set_offline)
434 ccw_device_set_offline(cdev);
435}
436
437static int online_store_recog_and_online(struct ccw_device *cdev)
438{
439 int ret;
440
441 /* Do device recognition, if needed. */
442 if (cdev->id.cu_type == 0) {
443 ret = ccw_device_recognition(cdev);
444 if (ret) {
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200445 CIO_MSG_EVENT(0, "Couldn't start recognition "
446 "for device 0.%x.%04x (ret=%d)\n",
447 cdev->private->dev_id.ssid,
448 cdev->private->dev_id.devno, ret);
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200449 return ret;
450 }
451 wait_event(cdev->private->wait_q,
452 cdev->private->flags.recog_done);
453 }
454 if (cdev->drv && cdev->drv->set_online)
455 ccw_device_set_online(cdev);
456 return 0;
457}
458static void online_store_handle_online(struct ccw_device *cdev, int force)
459{
460 int ret;
461
462 ret = online_store_recog_and_online(cdev);
463 if (ret)
464 return;
465 if (force && cdev->private->state == DEV_STATE_BOXED) {
466 ret = ccw_device_stlck(cdev);
467 if (ret) {
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200468 dev_warn(&cdev->dev,
469 "ccw_device_stlck returned %d!\n", ret);
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200470 return;
471 }
472 if (cdev->id.cu_type == 0)
473 cdev->private->state = DEV_STATE_NOT_OPER;
474 online_store_recog_and_online(cdev);
475 }
476
477}
478
479static ssize_t online_store (struct device *dev, struct device_attribute *attr,
480 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481{
482 struct ccw_device *cdev = to_ccwdev(dev);
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200483 int i, force;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 char *tmp;
485
Martin Schwidefsky973bd992006-01-06 00:19:07 -0800486 if (atomic_cmpxchg(&cdev->private->onoff, 0, 1) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 return -EAGAIN;
488
489 if (cdev->drv && !try_module_get(cdev->drv->owner)) {
490 atomic_set(&cdev->private->onoff, 0);
491 return -EINVAL;
492 }
493 if (!strncmp(buf, "force\n", count)) {
494 force = 1;
495 i = 1;
496 } else {
497 force = 0;
498 i = simple_strtoul(buf, &tmp, 16);
499 }
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200500
501 switch (i) {
502 case 0:
503 online_store_handle_offline(cdev);
504 break;
505 case 1:
506 online_store_handle_online(cdev, force);
507 break;
508 default:
509 count = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 if (cdev->drv)
512 module_put(cdev->drv->owner);
513 atomic_set(&cdev->private->onoff, 0);
514 return count;
515}
516
517static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400518available_show (struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519{
520 struct ccw_device *cdev = to_ccwdev(dev);
521 struct subchannel *sch;
522
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100523 if (ccw_device_is_orphan(cdev))
524 return sprintf(buf, "no device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 switch (cdev->private->state) {
526 case DEV_STATE_BOXED:
527 return sprintf(buf, "boxed\n");
528 case DEV_STATE_DISCONNECTED:
529 case DEV_STATE_DISCONNECTED_SENSE_ID:
530 case DEV_STATE_NOT_OPER:
531 sch = to_subchannel(dev->parent);
532 if (!sch->lpm)
533 return sprintf(buf, "no path\n");
534 else
535 return sprintf(buf, "no device\n");
536 default:
537 /* All other states considered fine. */
538 return sprintf(buf, "good\n");
539 }
540}
541
542static DEVICE_ATTR(chpids, 0444, chpids_show, NULL);
543static DEVICE_ATTR(pimpampom, 0444, pimpampom_show, NULL);
544static DEVICE_ATTR(devtype, 0444, devtype_show, NULL);
545static DEVICE_ATTR(cutype, 0444, cutype_show, NULL);
Bastian Blankf1fc78a2005-10-30 15:00:12 -0800546static DEVICE_ATTR(modalias, 0444, modalias_show, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547static DEVICE_ATTR(online, 0644, online_show, online_store);
548extern struct device_attribute dev_attr_cmb_enable;
549static DEVICE_ATTR(availability, 0444, available_show, NULL);
550
551static struct attribute * subch_attrs[] = {
552 &dev_attr_chpids.attr,
553 &dev_attr_pimpampom.attr,
554 NULL,
555};
556
557static struct attribute_group subch_attr_group = {
558 .attrs = subch_attrs,
559};
560
Cornelia Huck529192f2006-12-08 15:55:57 +0100561struct attribute_group *subch_attr_groups[] = {
562 &subch_attr_group,
563 NULL,
564};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
566static struct attribute * ccwdev_attrs[] = {
567 &dev_attr_devtype.attr,
568 &dev_attr_cutype.attr,
Bastian Blankf1fc78a2005-10-30 15:00:12 -0800569 &dev_attr_modalias.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 &dev_attr_online.attr,
571 &dev_attr_cmb_enable.attr,
572 &dev_attr_availability.attr,
573 NULL,
574};
575
576static struct attribute_group ccwdev_attr_group = {
577 .attrs = ccwdev_attrs,
578};
579
Cornelia Huckf7e5d672007-05-10 15:45:43 +0200580static struct attribute_group *ccwdev_attr_groups[] = {
Cornelia Huckef995162007-04-27 16:01:39 +0200581 &ccwdev_attr_group,
582 NULL,
583};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
585/* this is a simple abstraction for device_register that sets the
586 * correct bus type and adds the bus specific files */
Cornelia Huck3c9da7b2006-10-27 12:39:33 +0200587static int ccw_device_register(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588{
589 struct device *dev = &cdev->dev;
590 int ret;
591
592 dev->bus = &ccw_bus_type;
593
594 if ((ret = device_add(dev)))
595 return ret;
596
597 set_bit(1, &cdev->private->registered);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 return ret;
599}
600
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700601struct match_data {
Cornelia Huck78964262006-10-11 15:31:38 +0200602 struct ccw_dev_id dev_id;
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700603 struct ccw_device * sibling;
604};
605
606static int
607match_devno(struct device * dev, void * data)
608{
Cornelia Huck78964262006-10-11 15:31:38 +0200609 struct match_data * d = data;
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700610 struct ccw_device * cdev;
611
612 cdev = to_ccwdev(dev);
613 if ((cdev->private->state == DEV_STATE_DISCONNECTED) &&
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100614 !ccw_device_is_orphan(cdev) &&
Cornelia Huck78964262006-10-11 15:31:38 +0200615 ccw_dev_id_is_equal(&cdev->private->dev_id, &d->dev_id) &&
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100616 (cdev != d->sibling))
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700617 return 1;
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700618 return 0;
619}
620
Cornelia Huck78964262006-10-11 15:31:38 +0200621static struct ccw_device * get_disc_ccwdev_by_dev_id(struct ccw_dev_id *dev_id,
622 struct ccw_device *sibling)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 struct device *dev;
Heiko Carstens292888c2006-08-30 14:33:35 +0200625 struct match_data data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
Cornelia Huck78964262006-10-11 15:31:38 +0200627 data.dev_id = *dev_id;
Heiko Carstens292888c2006-08-30 14:33:35 +0200628 data.sibling = sibling;
Cornelia Hucke5945b42005-10-11 08:28:59 -0700629 dev = bus_find_device(&ccw_bus_type, NULL, &data, match_devno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700631 return dev ? to_ccwdev(dev) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632}
633
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100634static int match_orphan(struct device *dev, void *data)
635{
636 struct ccw_dev_id *dev_id;
637 struct ccw_device *cdev;
638
639 dev_id = data;
640 cdev = to_ccwdev(dev);
641 return ccw_dev_id_is_equal(&cdev->private->dev_id, dev_id);
642}
643
644static struct ccw_device *
645get_orphaned_ccwdev_by_dev_id(struct channel_subsystem *css,
646 struct ccw_dev_id *dev_id)
647{
648 struct device *dev;
649
650 dev = device_find_child(&css->pseudo_subchannel->dev, dev_id,
651 match_orphan);
652
653 return dev ? to_ccwdev(dev) : NULL;
654}
655
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656static void
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100657ccw_device_add_changed(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658{
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100659 struct ccw_device_private *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 struct ccw_device *cdev;
661
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100662 priv = container_of(work, struct ccw_device_private, kick_work);
663 cdev = priv->cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 if (device_add(&cdev->dev)) {
665 put_device(&cdev->dev);
666 return;
667 }
668 set_bit(1, &cdev->private->registered);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669}
670
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100671void ccw_device_do_unreg_rereg(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672{
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100673 struct ccw_device_private *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 struct ccw_device *cdev;
675 struct subchannel *sch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100677 priv = container_of(work, struct ccw_device_private, kick_work);
678 cdev = priv->cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 sch = to_subchannel(cdev->dev.parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
Cornelia Huckef995162007-04-27 16:01:39 +0200681 ccw_device_unregister(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 PREPARE_WORK(&cdev->private->kick_work,
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100683 ccw_device_add_changed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 queue_work(ccw_device_work, &cdev->private->kick_work);
685}
686
687static void
688ccw_device_release(struct device *dev)
689{
690 struct ccw_device *cdev;
691
692 cdev = to_ccwdev(dev);
693 kfree(cdev->private);
694 kfree(cdev);
695}
696
Cornelia Huck7674da72006-12-08 15:54:21 +0100697static struct ccw_device * io_subchannel_allocate_dev(struct subchannel *sch)
698{
699 struct ccw_device *cdev;
700
701 cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
702 if (cdev) {
703 cdev->private = kzalloc(sizeof(struct ccw_device_private),
704 GFP_KERNEL | GFP_DMA);
705 if (cdev->private)
706 return cdev;
707 }
708 kfree(cdev);
709 return ERR_PTR(-ENOMEM);
710}
711
712static int io_subchannel_initialize_dev(struct subchannel *sch,
713 struct ccw_device *cdev)
714{
715 cdev->private->cdev = cdev;
716 atomic_set(&cdev->private->onoff, 0);
717 cdev->dev.parent = &sch->dev;
718 cdev->dev.release = ccw_device_release;
719 INIT_LIST_HEAD(&cdev->private->kick_work.entry);
Cornelia Huckef995162007-04-27 16:01:39 +0200720 cdev->dev.groups = ccwdev_attr_groups;
Cornelia Huck7674da72006-12-08 15:54:21 +0100721 /* Do first half of device_register. */
722 device_initialize(&cdev->dev);
723 if (!get_device(&sch->dev)) {
724 if (cdev->dev.release)
725 cdev->dev.release(&cdev->dev);
726 return -ENODEV;
727 }
728 return 0;
729}
730
731static struct ccw_device * io_subchannel_create_ccwdev(struct subchannel *sch)
732{
733 struct ccw_device *cdev;
734 int ret;
735
736 cdev = io_subchannel_allocate_dev(sch);
737 if (!IS_ERR(cdev)) {
738 ret = io_subchannel_initialize_dev(sch, cdev);
739 if (ret) {
740 kfree(cdev);
741 cdev = ERR_PTR(ret);
742 }
743 }
744 return cdev;
745}
746
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100747static int io_subchannel_recog(struct ccw_device *, struct subchannel *);
748
749static void sch_attach_device(struct subchannel *sch,
750 struct ccw_device *cdev)
751{
Cornelia Huck82b7ac02007-04-27 16:01:36 +0200752 css_update_ssd_info(sch);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100753 spin_lock_irq(sch->lock);
754 sch->dev.driver_data = cdev;
755 cdev->private->schid = sch->schid;
756 cdev->ccwlock = sch->lock;
757 device_trigger_reprobe(sch);
758 spin_unlock_irq(sch->lock);
759}
760
761static void sch_attach_disconnected_device(struct subchannel *sch,
762 struct ccw_device *cdev)
763{
764 struct subchannel *other_sch;
765 int ret;
766
767 other_sch = to_subchannel(get_device(cdev->dev.parent));
768 ret = device_move(&cdev->dev, &sch->dev);
769 if (ret) {
770 CIO_MSG_EVENT(2, "Moving disconnected device 0.%x.%04x failed "
771 "(ret=%d)!\n", cdev->private->dev_id.ssid,
772 cdev->private->dev_id.devno, ret);
773 put_device(&other_sch->dev);
774 return;
775 }
776 other_sch->dev.driver_data = NULL;
777 /* No need to keep a subchannel without ccw device around. */
778 css_sch_device_unregister(other_sch);
779 put_device(&other_sch->dev);
780 sch_attach_device(sch, cdev);
781}
782
783static void sch_attach_orphaned_device(struct subchannel *sch,
784 struct ccw_device *cdev)
785{
786 int ret;
787
788 /* Try to move the ccw device to its new subchannel. */
789 ret = device_move(&cdev->dev, &sch->dev);
790 if (ret) {
791 CIO_MSG_EVENT(0, "Moving device 0.%x.%04x from orphanage "
792 "failed (ret=%d)!\n",
793 cdev->private->dev_id.ssid,
794 cdev->private->dev_id.devno, ret);
795 return;
796 }
797 sch_attach_device(sch, cdev);
798}
799
800static void sch_create_and_recog_new_device(struct subchannel *sch)
801{
802 struct ccw_device *cdev;
803
804 /* Need to allocate a new ccw device. */
805 cdev = io_subchannel_create_ccwdev(sch);
806 if (IS_ERR(cdev)) {
807 /* OK, we did everything we could... */
808 css_sch_device_unregister(sch);
809 return;
810 }
811 spin_lock_irq(sch->lock);
812 sch->dev.driver_data = cdev;
813 spin_unlock_irq(sch->lock);
814 /* Start recognition for the new ccw device. */
815 if (io_subchannel_recog(cdev, sch)) {
816 spin_lock_irq(sch->lock);
817 sch->dev.driver_data = NULL;
818 spin_unlock_irq(sch->lock);
819 if (cdev->dev.release)
820 cdev->dev.release(&cdev->dev);
821 css_sch_device_unregister(sch);
822 }
823}
824
825
826void ccw_device_move_to_orphanage(struct work_struct *work)
827{
828 struct ccw_device_private *priv;
829 struct ccw_device *cdev;
830 struct ccw_device *replacing_cdev;
831 struct subchannel *sch;
832 int ret;
833 struct channel_subsystem *css;
834 struct ccw_dev_id dev_id;
835
836 priv = container_of(work, struct ccw_device_private, kick_work);
837 cdev = priv->cdev;
838 sch = to_subchannel(cdev->dev.parent);
839 css = to_css(sch->dev.parent);
840 dev_id.devno = sch->schib.pmcw.dev;
841 dev_id.ssid = sch->schid.ssid;
842
843 /*
844 * Move the orphaned ccw device to the orphanage so the replacing
845 * ccw device can take its place on the subchannel.
846 */
847 ret = device_move(&cdev->dev, &css->pseudo_subchannel->dev);
848 if (ret) {
849 CIO_MSG_EVENT(0, "Moving device 0.%x.%04x to orphanage failed "
850 "(ret=%d)!\n", cdev->private->dev_id.ssid,
851 cdev->private->dev_id.devno, ret);
852 return;
853 }
854 cdev->ccwlock = css->pseudo_subchannel->lock;
855 /*
856 * Search for the replacing ccw device
857 * - among the disconnected devices
858 * - in the orphanage
859 */
860 replacing_cdev = get_disc_ccwdev_by_dev_id(&dev_id, cdev);
861 if (replacing_cdev) {
862 sch_attach_disconnected_device(sch, replacing_cdev);
863 return;
864 }
865 replacing_cdev = get_orphaned_ccwdev_by_dev_id(css, &dev_id);
866 if (replacing_cdev) {
867 sch_attach_orphaned_device(sch, replacing_cdev);
868 return;
869 }
870 sch_create_and_recog_new_device(sch);
871}
872
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873/*
874 * Register recognized device.
875 */
876static void
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100877io_subchannel_register(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878{
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100879 struct ccw_device_private *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 struct ccw_device *cdev;
881 struct subchannel *sch;
882 int ret;
883 unsigned long flags;
884
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100885 priv = container_of(work, struct ccw_device_private, kick_work);
886 cdev = priv->cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 sch = to_subchannel(cdev->dev.parent);
Cornelia Huck82b7ac02007-04-27 16:01:36 +0200888 css_update_ssd_info(sch);
Cornelia Huck47af5512006-12-04 15:41:07 +0100889 /*
890 * io_subchannel_register() will also be called after device
891 * recognition has been done for a boxed device (which will already
892 * be registered). We need to reprobe since we may now have sense id
893 * information.
894 */
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700895 if (klist_node_attached(&cdev->dev.knode_parent)) {
Cornelia Huck47af5512006-12-04 15:41:07 +0100896 if (!cdev->drv) {
897 ret = device_reprobe(&cdev->dev);
898 if (ret)
899 /* We can't do much here. */
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200900 CIO_MSG_EVENT(2, "device_reprobe() returned"
901 " %d for 0.%x.%04x\n", ret,
902 cdev->private->dev_id.ssid,
903 cdev->private->dev_id.devno);
Cornelia Huck47af5512006-12-04 15:41:07 +0100904 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 goto out;
906 }
Cornelia Huckfa1a8c22007-04-26 00:12:03 -0700907 /*
908 * Now we know this subchannel will stay, we can throw
909 * our delayed uevent.
910 */
911 sch->dev.uevent_suppress = 0;
912 kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 /* make it known to the system */
914 ret = ccw_device_register(cdev);
915 if (ret) {
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200916 CIO_MSG_EVENT(0, "Could not register ccw dev 0.%x.%04x: %d\n",
917 cdev->private->dev_id.ssid,
918 cdev->private->dev_id.devno, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 put_device(&cdev->dev);
Cornelia Huck2ec22982006-12-08 15:54:26 +0100920 spin_lock_irqsave(sch->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 sch->dev.driver_data = NULL;
Cornelia Huck2ec22982006-12-08 15:54:26 +0100922 spin_unlock_irqrestore(sch->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 kfree (cdev->private);
924 kfree (cdev);
925 put_device(&sch->dev);
926 if (atomic_dec_and_test(&ccw_device_init_count))
927 wake_up(&ccw_device_init_wq);
928 return;
929 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 put_device(&cdev->dev);
931out:
932 cdev->private->flags.recog_done = 1;
933 put_device(&sch->dev);
934 wake_up(&cdev->private->wait_q);
935 if (atomic_dec_and_test(&ccw_device_init_count))
936 wake_up(&ccw_device_init_wq);
937}
938
939void
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100940ccw_device_call_sch_unregister(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941{
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100942 struct ccw_device_private *priv;
943 struct ccw_device *cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 struct subchannel *sch;
945
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100946 priv = container_of(work, struct ccw_device_private, kick_work);
947 cdev = priv->cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 sch = to_subchannel(cdev->dev.parent);
Cornelia Huck6ab48792006-07-12 16:39:50 +0200949 css_sch_device_unregister(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 /* Reset intparm to zeroes. */
951 sch->schib.pmcw.intparm = 0;
952 cio_modify(sch);
953 put_device(&cdev->dev);
954 put_device(&sch->dev);
955}
956
957/*
958 * subchannel recognition done. Called from the state machine.
959 */
960void
961io_subchannel_recog_done(struct ccw_device *cdev)
962{
963 struct subchannel *sch;
964
965 if (css_init_done == 0) {
966 cdev->private->flags.recog_done = 1;
967 return;
968 }
969 switch (cdev->private->state) {
970 case DEV_STATE_NOT_OPER:
971 cdev->private->flags.recog_done = 1;
972 /* Remove device found not operational. */
973 if (!get_device(&cdev->dev))
974 break;
975 sch = to_subchannel(cdev->dev.parent);
976 PREPARE_WORK(&cdev->private->kick_work,
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100977 ccw_device_call_sch_unregister);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 queue_work(slow_path_wq, &cdev->private->kick_work);
979 if (atomic_dec_and_test(&ccw_device_init_count))
980 wake_up(&ccw_device_init_wq);
981 break;
982 case DEV_STATE_BOXED:
983 /* Device did not respond in time. */
984 case DEV_STATE_OFFLINE:
985 /*
986 * We can't register the device in interrupt context so
987 * we schedule a work item.
988 */
989 if (!get_device(&cdev->dev))
990 break;
991 PREPARE_WORK(&cdev->private->kick_work,
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100992 io_subchannel_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 queue_work(slow_path_wq, &cdev->private->kick_work);
994 break;
995 }
996}
997
998static int
999io_subchannel_recog(struct ccw_device *cdev, struct subchannel *sch)
1000{
1001 int rc;
1002 struct ccw_device_private *priv;
1003
1004 sch->dev.driver_data = cdev;
1005 sch->driver = &io_subchannel_driver;
Cornelia Huck2ec22982006-12-08 15:54:26 +01001006 cdev->ccwlock = sch->lock;
Cornelia Huckfb6958a2006-01-06 00:19:25 -08001007
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 /* Init private data. */
1009 priv = cdev->private;
Cornelia Huck78964262006-10-11 15:31:38 +02001010 priv->dev_id.devno = sch->schib.pmcw.dev;
1011 priv->dev_id.ssid = sch->schid.ssid;
1012 priv->schid = sch->schid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 priv->state = DEV_STATE_NOT_OPER;
1014 INIT_LIST_HEAD(&priv->cmb_list);
1015 init_waitqueue_head(&priv->wait_q);
1016 init_timer(&priv->timer);
1017
1018 /* Set an initial name for the device. */
Cornelia Huckfb6958a2006-01-06 00:19:25 -08001019 snprintf (cdev->dev.bus_id, BUS_ID_SIZE, "0.%x.%04x",
1020 sch->schid.ssid, sch->schib.pmcw.dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021
1022 /* Increase counter of devices currently in recognition. */
1023 atomic_inc(&ccw_device_init_count);
1024
1025 /* Start async. device sensing. */
Cornelia Huck2ec22982006-12-08 15:54:26 +01001026 spin_lock_irq(sch->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 rc = ccw_device_recognition(cdev);
Cornelia Huck2ec22982006-12-08 15:54:26 +01001028 spin_unlock_irq(sch->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 if (rc) {
1030 if (atomic_dec_and_test(&ccw_device_init_count))
1031 wake_up(&ccw_device_init_wq);
1032 }
1033 return rc;
1034}
1035
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +01001036static void ccw_device_move_to_sch(struct work_struct *work)
1037{
1038 struct ccw_device_private *priv;
1039 int rc;
1040 struct subchannel *sch;
1041 struct ccw_device *cdev;
1042 struct subchannel *former_parent;
1043
1044 priv = container_of(work, struct ccw_device_private, kick_work);
1045 sch = priv->sch;
1046 cdev = priv->cdev;
1047 former_parent = ccw_device_is_orphan(cdev) ?
1048 NULL : to_subchannel(get_device(cdev->dev.parent));
1049 mutex_lock(&sch->reg_mutex);
1050 /* Try to move the ccw device to its new subchannel. */
1051 rc = device_move(&cdev->dev, &sch->dev);
1052 mutex_unlock(&sch->reg_mutex);
1053 if (rc) {
1054 CIO_MSG_EVENT(2, "Moving device 0.%x.%04x to subchannel "
1055 "0.%x.%04x failed (ret=%d)!\n",
1056 cdev->private->dev_id.ssid,
1057 cdev->private->dev_id.devno, sch->schid.ssid,
1058 sch->schid.sch_no, rc);
1059 css_sch_device_unregister(sch);
1060 goto out;
1061 }
1062 if (former_parent) {
1063 spin_lock_irq(former_parent->lock);
1064 former_parent->dev.driver_data = NULL;
1065 spin_unlock_irq(former_parent->lock);
1066 css_sch_device_unregister(former_parent);
1067 /* Reset intparm to zeroes. */
1068 former_parent->schib.pmcw.intparm = 0;
1069 cio_modify(former_parent);
1070 }
1071 sch_attach_device(sch, cdev);
1072out:
1073 if (former_parent)
1074 put_device(&former_parent->dev);
1075 put_device(&cdev->dev);
1076}
1077
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078static int
Cornelia Huck8bbace72006-01-11 10:56:22 +01001079io_subchannel_probe (struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 struct ccw_device *cdev;
1082 int rc;
1083 unsigned long flags;
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +01001084 struct ccw_dev_id dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 if (sch->dev.driver_data) {
1087 /*
1088 * This subchannel already has an associated ccw_device.
1089 * Register it and exit. This happens for all early
1090 * device, e.g. the console.
1091 */
1092 cdev = sch->dev.driver_data;
1093 device_initialize(&cdev->dev);
1094 ccw_device_register(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 /*
1096 * Check if the device is already online. If it is
1097 * the reference count needs to be corrected
1098 * (see ccw_device_online and css_init_done for the
1099 * ugly details).
1100 */
1101 if (cdev->private->state != DEV_STATE_NOT_OPER &&
1102 cdev->private->state != DEV_STATE_OFFLINE &&
1103 cdev->private->state != DEV_STATE_BOXED)
1104 get_device(&cdev->dev);
1105 return 0;
1106 }
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +01001107 /*
1108 * First check if a fitting device may be found amongst the
1109 * disconnected devices or in the orphanage.
1110 */
1111 dev_id.devno = sch->schib.pmcw.dev;
1112 dev_id.ssid = sch->schid.ssid;
1113 cdev = get_disc_ccwdev_by_dev_id(&dev_id, NULL);
1114 if (!cdev)
1115 cdev = get_orphaned_ccwdev_by_dev_id(to_css(sch->dev.parent),
1116 &dev_id);
1117 if (cdev) {
1118 /*
1119 * Schedule moving the device until when we have a registered
1120 * subchannel to move to and succeed the probe. We can
1121 * unregister later again, when the probe is through.
1122 */
1123 cdev->private->sch = sch;
1124 PREPARE_WORK(&cdev->private->kick_work,
1125 ccw_device_move_to_sch);
1126 queue_work(slow_path_wq, &cdev->private->kick_work);
1127 return 0;
1128 }
Cornelia Huck7674da72006-12-08 15:54:21 +01001129 cdev = io_subchannel_create_ccwdev(sch);
1130 if (IS_ERR(cdev))
1131 return PTR_ERR(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132
Cornelia Huck8bbace72006-01-11 10:56:22 +01001133 rc = io_subchannel_recog(cdev, sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 if (rc) {
Cornelia Huck2ec22982006-12-08 15:54:26 +01001135 spin_lock_irqsave(sch->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 sch->dev.driver_data = NULL;
Cornelia Huck2ec22982006-12-08 15:54:26 +01001137 spin_unlock_irqrestore(sch->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 if (cdev->dev.release)
1139 cdev->dev.release(&cdev->dev);
1140 }
1141
1142 return rc;
1143}
1144
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145static int
Cornelia Huck8bbace72006-01-11 10:56:22 +01001146io_subchannel_remove (struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147{
1148 struct ccw_device *cdev;
1149 unsigned long flags;
1150
Cornelia Huck8bbace72006-01-11 10:56:22 +01001151 if (!sch->dev.driver_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 return 0;
Cornelia Huck8bbace72006-01-11 10:56:22 +01001153 cdev = sch->dev.driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 /* Set ccw device to not operational and drop reference. */
1155 spin_lock_irqsave(cdev->ccwlock, flags);
Cornelia Huck8bbace72006-01-11 10:56:22 +01001156 sch->dev.driver_data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 cdev->private->state = DEV_STATE_NOT_OPER;
1158 spin_unlock_irqrestore(cdev->ccwlock, flags);
Cornelia Huckef995162007-04-27 16:01:39 +02001159 ccw_device_unregister(cdev);
1160 put_device(&cdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 return 0;
1162}
1163
1164static int
1165io_subchannel_notify(struct device *dev, int event)
1166{
1167 struct ccw_device *cdev;
1168
1169 cdev = dev->driver_data;
1170 if (!cdev)
1171 return 0;
1172 if (!cdev->drv)
1173 return 0;
1174 if (!cdev->online)
1175 return 0;
1176 return cdev->drv->notify ? cdev->drv->notify(cdev, event) : 0;
1177}
1178
1179static void
1180io_subchannel_verify(struct device *dev)
1181{
1182 struct ccw_device *cdev;
1183
1184 cdev = dev->driver_data;
1185 if (cdev)
1186 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1187}
1188
1189static void
1190io_subchannel_ioterm(struct device *dev)
1191{
1192 struct ccw_device *cdev;
1193
1194 cdev = dev->driver_data;
1195 if (!cdev)
1196 return;
Cornelia Huckd23861f2006-12-04 15:41:04 +01001197 /* Internal I/O will be retried by the interrupt handler. */
1198 if (cdev->private->flags.intretry)
1199 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 cdev->private->state = DEV_STATE_CLEAR_VERIFY;
1201 if (cdev->handler)
1202 cdev->handler(cdev, cdev->private->intparm,
1203 ERR_PTR(-EIO));
1204}
1205
1206static void
Cornelia Huck8bbace72006-01-11 10:56:22 +01001207io_subchannel_shutdown(struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 struct ccw_device *cdev;
1210 int ret;
1211
Cornelia Huck8bbace72006-01-11 10:56:22 +01001212 cdev = sch->dev.driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213
Cornelia Hucka8237fc2006-01-06 00:19:21 -08001214 if (cio_is_console(sch->schid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 return;
1216 if (!sch->schib.pmcw.ena)
1217 /* Nothing to do. */
1218 return;
1219 ret = cio_disable_subchannel(sch);
1220 if (ret != -EBUSY)
1221 /* Subchannel is disabled, we're done. */
1222 return;
1223 cdev->private->state = DEV_STATE_QUIESCE;
1224 if (cdev->handler)
1225 cdev->handler(cdev, cdev->private->intparm,
1226 ERR_PTR(-EIO));
1227 ret = ccw_device_cancel_halt_clear(cdev);
1228 if (ret == -EBUSY) {
1229 ccw_device_set_timeout(cdev, HZ/10);
1230 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
1231 }
1232 cio_disable_subchannel(sch);
1233}
1234
1235#ifdef CONFIG_CCW_CONSOLE
1236static struct ccw_device console_cdev;
1237static struct ccw_device_private console_private;
1238static int console_cdev_in_use;
1239
Cornelia Huck2ec22982006-12-08 15:54:26 +01001240static DEFINE_SPINLOCK(ccw_console_lock);
1241
1242spinlock_t * cio_get_console_lock(void)
1243{
1244 return &ccw_console_lock;
1245}
1246
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247static int
1248ccw_device_console_enable (struct ccw_device *cdev, struct subchannel *sch)
1249{
1250 int rc;
1251
1252 /* Initialize the ccw_device structure. */
Heiko Carstens292888c2006-08-30 14:33:35 +02001253 cdev->dev.parent= &sch->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 rc = io_subchannel_recog(cdev, sch);
1255 if (rc)
1256 return rc;
1257
1258 /* Now wait for the async. recognition to come to an end. */
1259 spin_lock_irq(cdev->ccwlock);
1260 while (!dev_fsm_final_state(cdev))
1261 wait_cons_dev();
1262 rc = -EIO;
1263 if (cdev->private->state != DEV_STATE_OFFLINE)
1264 goto out_unlock;
1265 ccw_device_online(cdev);
1266 while (!dev_fsm_final_state(cdev))
1267 wait_cons_dev();
1268 if (cdev->private->state != DEV_STATE_ONLINE)
1269 goto out_unlock;
1270 rc = 0;
1271out_unlock:
1272 spin_unlock_irq(cdev->ccwlock);
1273 return 0;
1274}
1275
1276struct ccw_device *
1277ccw_device_probe_console(void)
1278{
1279 struct subchannel *sch;
1280 int ret;
1281
1282 if (xchg(&console_cdev_in_use, 1) != 0)
Peter Oberparleiter600b5d12006-02-01 03:06:35 -08001283 return ERR_PTR(-EBUSY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 sch = cio_probe_console();
1285 if (IS_ERR(sch)) {
1286 console_cdev_in_use = 0;
1287 return (void *) sch;
1288 }
1289 memset(&console_cdev, 0, sizeof(struct ccw_device));
1290 memset(&console_private, 0, sizeof(struct ccw_device_private));
1291 console_cdev.private = &console_private;
Martin Schwidefskyc1637532006-12-08 15:53:57 +01001292 console_private.cdev = &console_cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 ret = ccw_device_console_enable(&console_cdev, sch);
1294 if (ret) {
1295 cio_release_console();
1296 console_cdev_in_use = 0;
1297 return ERR_PTR(ret);
1298 }
1299 console_cdev.online = 1;
1300 return &console_cdev;
1301}
1302#endif
1303
1304/*
1305 * get ccw_device matching the busid, but only if owned by cdrv
1306 */
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001307static int
1308__ccwdev_check_busid(struct device *dev, void *id)
1309{
1310 char *bus_id;
1311
Cornelia Huck12975ae2006-10-11 15:31:47 +02001312 bus_id = id;
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001313
1314 return (strncmp(bus_id, dev->bus_id, BUS_ID_SIZE) == 0);
1315}
1316
1317
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318struct ccw_device *
1319get_ccwdev_by_busid(struct ccw_driver *cdrv, const char *bus_id)
1320{
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001321 struct device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 struct device_driver *drv;
1323
1324 drv = get_driver(&cdrv->driver);
1325 if (!drv)
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001326 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001328 dev = driver_find_device(drv, NULL, (void *)bus_id,
1329 __ccwdev_check_busid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 put_driver(drv);
1331
Heiko Carstensd2c993d2006-07-12 16:41:55 +02001332 return dev ? to_ccwdev(dev) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333}
1334
1335/************************** device driver handling ************************/
1336
1337/* This is the implementation of the ccw_driver class. The probe, remove
1338 * and release methods are initially very similar to the device_driver
1339 * implementations, with the difference that they have ccw_device
1340 * arguments.
1341 *
1342 * A ccw driver also contains the information that is needed for
1343 * device matching.
1344 */
1345static int
1346ccw_device_probe (struct device *dev)
1347{
1348 struct ccw_device *cdev = to_ccwdev(dev);
1349 struct ccw_driver *cdrv = to_ccwdrv(dev->driver);
1350 int ret;
1351
1352 cdev->drv = cdrv; /* to let the driver call _set_online */
1353
1354 ret = cdrv->probe ? cdrv->probe(cdev) : -ENODEV;
1355
1356 if (ret) {
Heiko Carstensd2c993d2006-07-12 16:41:55 +02001357 cdev->drv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 return ret;
1359 }
1360
1361 return 0;
1362}
1363
1364static int
1365ccw_device_remove (struct device *dev)
1366{
1367 struct ccw_device *cdev = to_ccwdev(dev);
1368 struct ccw_driver *cdrv = cdev->drv;
1369 int ret;
1370
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 if (cdrv->remove)
1372 cdrv->remove(cdev);
1373 if (cdev->online) {
1374 cdev->online = 0;
1375 spin_lock_irq(cdev->ccwlock);
1376 ret = ccw_device_offline(cdev);
1377 spin_unlock_irq(cdev->ccwlock);
1378 if (ret == 0)
1379 wait_event(cdev->private->wait_q,
1380 dev_fsm_final_state(cdev));
1381 else
1382 //FIXME: we can't fail!
Cornelia Hucke556bbb2007-07-27 12:29:19 +02001383 CIO_MSG_EVENT(2, "ccw_device_offline returned %d, "
1384 "device 0.%x.%04x\n",
1385 ret, cdev->private->dev_id.ssid,
1386 cdev->private->dev_id.devno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 }
1388 ccw_device_set_timeout(cdev, 0);
Heiko Carstensd2c993d2006-07-12 16:41:55 +02001389 cdev->drv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 return 0;
1391}
1392
Cornelia Huck8bbace72006-01-11 10:56:22 +01001393struct bus_type ccw_bus_type = {
1394 .name = "ccw",
1395 .match = ccw_bus_match,
1396 .uevent = ccw_uevent,
1397 .probe = ccw_device_probe,
1398 .remove = ccw_device_remove,
1399};
1400
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401int
1402ccw_driver_register (struct ccw_driver *cdriver)
1403{
1404 struct device_driver *drv = &cdriver->driver;
1405
1406 drv->bus = &ccw_bus_type;
1407 drv->name = cdriver->name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408
1409 return driver_register(drv);
1410}
1411
1412void
1413ccw_driver_unregister (struct ccw_driver *cdriver)
1414{
1415 driver_unregister(&cdriver->driver);
1416}
1417
Cornelia Hucka8237fc2006-01-06 00:19:21 -08001418/* Helper func for qdio. */
1419struct subchannel_id
1420ccw_device_get_subchannel_id(struct ccw_device *cdev)
1421{
1422 struct subchannel *sch;
1423
1424 sch = to_subchannel(cdev->dev.parent);
1425 return sch->schid;
1426}
1427
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428MODULE_LICENSE("GPL");
1429EXPORT_SYMBOL(ccw_device_set_online);
1430EXPORT_SYMBOL(ccw_device_set_offline);
1431EXPORT_SYMBOL(ccw_driver_register);
1432EXPORT_SYMBOL(ccw_driver_unregister);
1433EXPORT_SYMBOL(get_ccwdev_by_busid);
1434EXPORT_SYMBOL(ccw_bus_type);
1435EXPORT_SYMBOL(ccw_device_work);
1436EXPORT_SYMBOL(ccw_device_notify_work);
Cornelia Hucka8237fc2006-01-06 00:19:21 -08001437EXPORT_SYMBOL_GPL(ccw_device_get_subchannel_id);