blob: 7bccca9684e599bc6d753830f1875bfbcb6ca215 [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 */
Cornelia Huck1842f2b2007-10-12 16:11:22 +020024#include <asm/cmb.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
26#include "cio.h"
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +010027#include "cio_debug.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include "css.h"
29#include "device.h"
30#include "ioasm.h"
31
32/******************* bus type handling ***********************/
33
34/* The Linux driver model distinguishes between a bus type and
35 * the bus itself. Of course we only have one channel
36 * subsystem driver and one channel system per machine, but
37 * we still use the abstraction. T.R. says it's a good idea. */
38static int
39ccw_bus_match (struct device * dev, struct device_driver * drv)
40{
41 struct ccw_device *cdev = to_ccwdev(dev);
42 struct ccw_driver *cdrv = to_ccwdrv(drv);
43 const struct ccw_device_id *ids = cdrv->ids, *found;
44
45 if (!ids)
46 return 0;
47
48 found = ccw_device_id_match(ids, &cdev->id);
49 if (!found)
50 return 0;
51
52 cdev->id.driver_info = found->driver_info;
53
54 return 1;
55}
56
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +020057/* Store modalias string delimited by prefix/suffix string into buffer with
58 * specified size. Return length of resulting string (excluding trailing '\0')
59 * even if string doesn't fit buffer (snprintf semantics). */
Cornelia Huckcfbe9bb2007-04-27 16:01:32 +020060static int snprint_alias(char *buf, size_t size,
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +020061 struct ccw_device_id *id, const char *suffix)
62{
63 int len;
64
Cornelia Huckcfbe9bb2007-04-27 16:01:32 +020065 len = snprintf(buf, size, "ccw:t%04Xm%02X", id->cu_type, id->cu_model);
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +020066 if (len > size)
67 return len;
68 buf += len;
69 size -= len;
70
71 if (id->dev_type != 0)
72 len += snprintf(buf, size, "dt%04Xdm%02X%s", id->dev_type,
73 id->dev_model, suffix);
74 else
75 len += snprintf(buf, size, "dtdm%s", suffix);
76
77 return len;
78}
79
80/* Set up environment variables for ccw device uevent. Return 0 on success,
81 * non-zero otherwise. */
Kay Sievers7eff2e72007-08-14 15:15:12 +020082static int ccw_uevent(struct device *dev, struct kobj_uevent_env *env)
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);
Cornelia Huckcfbe9bb2007-04-27 16:01:32 +020086 int ret;
87 char modalias_buf[30];
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +020089 /* CU_TYPE= */
Kay Sievers7eff2e72007-08-14 15:15:12 +020090 ret = add_uevent_var(env, "CU_TYPE=%04X", id->cu_type);
Cornelia Huckcfbe9bb2007-04-27 16:01:32 +020091 if (ret)
92 return ret;
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +020093
94 /* CU_MODEL= */
Kay Sievers7eff2e72007-08-14 15:15:12 +020095 ret = add_uevent_var(env, "CU_MODEL=%02X", id->cu_model);
Cornelia Huckcfbe9bb2007-04-27 16:01:32 +020096 if (ret)
97 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
99 /* The next two can be zero, that's ok for us */
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +0200100 /* DEV_TYPE= */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200101 ret = add_uevent_var(env, "DEV_TYPE=%04X", id->dev_type);
Cornelia Huckcfbe9bb2007-04-27 16:01:32 +0200102 if (ret)
103 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +0200105 /* DEV_MODEL= */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200106 ret = add_uevent_var(env, "DEV_MODEL=%02X", id->dev_model);
Cornelia Huckcfbe9bb2007-04-27 16:01:32 +0200107 if (ret)
108 return ret;
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +0200109
110 /* MODALIAS= */
Cornelia Huckcfbe9bb2007-04-27 16:01:32 +0200111 snprint_alias(modalias_buf, sizeof(modalias_buf), id, "");
Kay Sievers7eff2e72007-08-14 15:15:12 +0200112 ret = add_uevent_var(env, "MODALIAS=%s", modalias_buf);
113 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114}
115
Cornelia Huck8bbace72006-01-11 10:56:22 +0100116struct bus_type ccw_bus_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
Cornelia Huck602b20f2008-01-26 14:10:39 +0100118static void io_subchannel_irq(struct subchannel *);
119static int io_subchannel_probe(struct subchannel *);
120static int io_subchannel_remove(struct subchannel *);
121static int io_subchannel_notify(struct subchannel *, int);
122static void io_subchannel_verify(struct subchannel *);
123static void io_subchannel_ioterm(struct subchannel *);
Cornelia Huck8bbace72006-01-11 10:56:22 +0100124static void io_subchannel_shutdown(struct subchannel *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
Cornelia Huckf7e5d672007-05-10 15:45:43 +0200126static struct css_driver io_subchannel_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 .subchannel_type = SUBCHANNEL_TYPE_IO,
Cornelia Huck25b7bb52008-01-26 14:10:41 +0100128 .name = "io_subchannel",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 .irq = io_subchannel_irq,
130 .notify = io_subchannel_notify,
131 .verify = io_subchannel_verify,
132 .termination = io_subchannel_ioterm,
Cornelia Huck8bbace72006-01-11 10:56:22 +0100133 .probe = io_subchannel_probe,
134 .remove = io_subchannel_remove,
135 .shutdown = io_subchannel_shutdown,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136};
137
138struct workqueue_struct *ccw_device_work;
139struct workqueue_struct *ccw_device_notify_work;
Peter Oberparleiter40154b82006-06-29 14:57:03 +0200140wait_queue_head_t ccw_device_init_wq;
141atomic_t ccw_device_init_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
143static int __init
144init_ccw_bus_type (void)
145{
146 int ret;
147
148 init_waitqueue_head(&ccw_device_init_wq);
149 atomic_set(&ccw_device_init_count, 0);
150
151 ccw_device_work = create_singlethread_workqueue("cio");
152 if (!ccw_device_work)
153 return -ENOMEM; /* FIXME: better errno ? */
154 ccw_device_notify_work = create_singlethread_workqueue("cio_notify");
155 if (!ccw_device_notify_work) {
156 ret = -ENOMEM; /* FIXME: better errno ? */
157 goto out_err;
158 }
159 slow_path_wq = create_singlethread_workqueue("kslowcrw");
160 if (!slow_path_wq) {
161 ret = -ENOMEM; /* FIXME: better errno ? */
162 goto out_err;
163 }
164 if ((ret = bus_register (&ccw_bus_type)))
165 goto out_err;
166
Cornelia Huck25b7bb52008-01-26 14:10:41 +0100167 ret = css_driver_register(&io_subchannel_driver);
168 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 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{
Cornelia Huck25b7bb52008-01-26 14:10:41 +0100188 css_driver_unregister(&io_subchannel_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 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
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +0200349/**
350 * ccw_device_set_offline() - disable a ccw device for I/O
351 * @cdev: target ccw device
352 *
353 * This function calls the driver's set_offline() function for @cdev, if
354 * given, and then disables @cdev.
355 * Returns:
356 * %0 on success and a negative error value on failure.
357 * Context:
358 * enabled, ccw device lock not held
359 */
360int ccw_device_set_offline(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361{
362 int ret;
363
364 if (!cdev)
365 return -ENODEV;
366 if (!cdev->online || !cdev->drv)
367 return -EINVAL;
368
369 if (cdev->drv->set_offline) {
370 ret = cdev->drv->set_offline(cdev);
371 if (ret != 0)
372 return ret;
373 }
374 cdev->online = 0;
375 spin_lock_irq(cdev->ccwlock);
376 ret = ccw_device_offline(cdev);
377 if (ret == -ENODEV) {
378 if (cdev->private->state != DEV_STATE_NOT_OPER) {
379 cdev->private->state = DEV_STATE_OFFLINE;
380 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
381 }
382 spin_unlock_irq(cdev->ccwlock);
383 return ret;
384 }
385 spin_unlock_irq(cdev->ccwlock);
386 if (ret == 0)
387 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
388 else {
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200389 CIO_MSG_EVENT(2, "ccw_device_offline returned %d, "
390 "device 0.%x.%04x\n",
391 ret, cdev->private->dev_id.ssid,
392 cdev->private->dev_id.devno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 cdev->online = 1;
394 }
395 return ret;
396}
397
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +0200398/**
399 * ccw_device_set_online() - enable a ccw device for I/O
400 * @cdev: target ccw device
401 *
402 * This function first enables @cdev and then calls the driver's set_online()
403 * function for @cdev, if given. If set_online() returns an error, @cdev is
404 * disabled again.
405 * Returns:
406 * %0 on success and a negative error value on failure.
407 * Context:
408 * enabled, ccw device lock not held
409 */
410int ccw_device_set_online(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411{
412 int ret;
413
414 if (!cdev)
415 return -ENODEV;
416 if (cdev->online || !cdev->drv)
417 return -EINVAL;
418
419 spin_lock_irq(cdev->ccwlock);
420 ret = ccw_device_online(cdev);
421 spin_unlock_irq(cdev->ccwlock);
422 if (ret == 0)
423 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
424 else {
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200425 CIO_MSG_EVENT(2, "ccw_device_online returned %d, "
426 "device 0.%x.%04x\n",
427 ret, cdev->private->dev_id.ssid,
428 cdev->private->dev_id.devno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 return ret;
430 }
431 if (cdev->private->state != DEV_STATE_ONLINE)
432 return -ENODEV;
433 if (!cdev->drv->set_online || cdev->drv->set_online(cdev) == 0) {
434 cdev->online = 1;
435 return 0;
436 }
437 spin_lock_irq(cdev->ccwlock);
438 ret = ccw_device_offline(cdev);
439 spin_unlock_irq(cdev->ccwlock);
440 if (ret == 0)
441 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200442 else
443 CIO_MSG_EVENT(2, "ccw_device_offline returned %d, "
444 "device 0.%x.%04x\n",
445 ret, cdev->private->dev_id.ssid,
446 cdev->private->dev_id.devno);
Cornelia Huck6cadb782006-02-17 13:52:49 -0800447 return (ret == 0) ? -ENODEV : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448}
449
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200450static void online_store_handle_offline(struct ccw_device *cdev)
451{
452 if (cdev->private->state == DEV_STATE_DISCONNECTED)
453 ccw_device_remove_disconnected(cdev);
454 else if (cdev->drv && cdev->drv->set_offline)
455 ccw_device_set_offline(cdev);
456}
457
458static int online_store_recog_and_online(struct ccw_device *cdev)
459{
460 int ret;
461
462 /* Do device recognition, if needed. */
463 if (cdev->id.cu_type == 0) {
464 ret = ccw_device_recognition(cdev);
465 if (ret) {
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200466 CIO_MSG_EVENT(0, "Couldn't start recognition "
467 "for device 0.%x.%04x (ret=%d)\n",
468 cdev->private->dev_id.ssid,
469 cdev->private->dev_id.devno, ret);
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200470 return ret;
471 }
472 wait_event(cdev->private->wait_q,
473 cdev->private->flags.recog_done);
474 }
475 if (cdev->drv && cdev->drv->set_online)
476 ccw_device_set_online(cdev);
477 return 0;
478}
479static void online_store_handle_online(struct ccw_device *cdev, int force)
480{
481 int ret;
482
483 ret = online_store_recog_and_online(cdev);
484 if (ret)
485 return;
486 if (force && cdev->private->state == DEV_STATE_BOXED) {
487 ret = ccw_device_stlck(cdev);
488 if (ret) {
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200489 dev_warn(&cdev->dev,
490 "ccw_device_stlck returned %d!\n", ret);
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200491 return;
492 }
493 if (cdev->id.cu_type == 0)
494 cdev->private->state = DEV_STATE_NOT_OPER;
495 online_store_recog_and_online(cdev);
496 }
497
498}
499
500static ssize_t online_store (struct device *dev, struct device_attribute *attr,
501 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502{
503 struct ccw_device *cdev = to_ccwdev(dev);
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200504 int i, force;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 char *tmp;
506
Martin Schwidefsky973bd992006-01-06 00:19:07 -0800507 if (atomic_cmpxchg(&cdev->private->onoff, 0, 1) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 return -EAGAIN;
509
510 if (cdev->drv && !try_module_get(cdev->drv->owner)) {
511 atomic_set(&cdev->private->onoff, 0);
512 return -EINVAL;
513 }
514 if (!strncmp(buf, "force\n", count)) {
515 force = 1;
516 i = 1;
517 } else {
518 force = 0;
519 i = simple_strtoul(buf, &tmp, 16);
520 }
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200521
522 switch (i) {
523 case 0:
524 online_store_handle_offline(cdev);
525 break;
526 case 1:
527 online_store_handle_online(cdev, force);
528 break;
529 default:
530 count = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 if (cdev->drv)
533 module_put(cdev->drv->owner);
534 atomic_set(&cdev->private->onoff, 0);
535 return count;
536}
537
538static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400539available_show (struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540{
541 struct ccw_device *cdev = to_ccwdev(dev);
542 struct subchannel *sch;
543
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100544 if (ccw_device_is_orphan(cdev))
545 return sprintf(buf, "no device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 switch (cdev->private->state) {
547 case DEV_STATE_BOXED:
548 return sprintf(buf, "boxed\n");
549 case DEV_STATE_DISCONNECTED:
550 case DEV_STATE_DISCONNECTED_SENSE_ID:
551 case DEV_STATE_NOT_OPER:
552 sch = to_subchannel(dev->parent);
553 if (!sch->lpm)
554 return sprintf(buf, "no path\n");
555 else
556 return sprintf(buf, "no device\n");
557 default:
558 /* All other states considered fine. */
559 return sprintf(buf, "good\n");
560 }
561}
562
563static DEVICE_ATTR(chpids, 0444, chpids_show, NULL);
564static DEVICE_ATTR(pimpampom, 0444, pimpampom_show, NULL);
565static DEVICE_ATTR(devtype, 0444, devtype_show, NULL);
566static DEVICE_ATTR(cutype, 0444, cutype_show, NULL);
Bastian Blankf1fc78a2005-10-30 15:00:12 -0800567static DEVICE_ATTR(modalias, 0444, modalias_show, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568static DEVICE_ATTR(online, 0644, online_show, online_store);
569extern struct device_attribute dev_attr_cmb_enable;
570static DEVICE_ATTR(availability, 0444, available_show, NULL);
571
572static struct attribute * subch_attrs[] = {
573 &dev_attr_chpids.attr,
574 &dev_attr_pimpampom.attr,
575 NULL,
576};
577
578static struct attribute_group subch_attr_group = {
579 .attrs = subch_attrs,
580};
581
Cornelia Huck529192f2006-12-08 15:55:57 +0100582struct attribute_group *subch_attr_groups[] = {
583 &subch_attr_group,
584 NULL,
585};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
587static struct attribute * ccwdev_attrs[] = {
588 &dev_attr_devtype.attr,
589 &dev_attr_cutype.attr,
Bastian Blankf1fc78a2005-10-30 15:00:12 -0800590 &dev_attr_modalias.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 &dev_attr_online.attr,
592 &dev_attr_cmb_enable.attr,
593 &dev_attr_availability.attr,
594 NULL,
595};
596
597static struct attribute_group ccwdev_attr_group = {
598 .attrs = ccwdev_attrs,
599};
600
Cornelia Huckf7e5d672007-05-10 15:45:43 +0200601static struct attribute_group *ccwdev_attr_groups[] = {
Cornelia Huckef995162007-04-27 16:01:39 +0200602 &ccwdev_attr_group,
603 NULL,
604};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
606/* this is a simple abstraction for device_register that sets the
607 * correct bus type and adds the bus specific files */
Cornelia Huck3c9da7b2006-10-27 12:39:33 +0200608static int ccw_device_register(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609{
610 struct device *dev = &cdev->dev;
611 int ret;
612
613 dev->bus = &ccw_bus_type;
614
615 if ((ret = device_add(dev)))
616 return ret;
617
618 set_bit(1, &cdev->private->registered);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 return ret;
620}
621
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700622struct match_data {
Cornelia Huck78964262006-10-11 15:31:38 +0200623 struct ccw_dev_id dev_id;
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700624 struct ccw_device * sibling;
625};
626
627static int
628match_devno(struct device * dev, void * data)
629{
Cornelia Huck78964262006-10-11 15:31:38 +0200630 struct match_data * d = data;
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700631 struct ccw_device * cdev;
632
633 cdev = to_ccwdev(dev);
634 if ((cdev->private->state == DEV_STATE_DISCONNECTED) &&
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100635 !ccw_device_is_orphan(cdev) &&
Cornelia Huck78964262006-10-11 15:31:38 +0200636 ccw_dev_id_is_equal(&cdev->private->dev_id, &d->dev_id) &&
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100637 (cdev != d->sibling))
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700638 return 1;
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700639 return 0;
640}
641
Cornelia Huck78964262006-10-11 15:31:38 +0200642static struct ccw_device * get_disc_ccwdev_by_dev_id(struct ccw_dev_id *dev_id,
643 struct ccw_device *sibling)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 struct device *dev;
Heiko Carstens292888c2006-08-30 14:33:35 +0200646 struct match_data data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
Cornelia Huck78964262006-10-11 15:31:38 +0200648 data.dev_id = *dev_id;
Heiko Carstens292888c2006-08-30 14:33:35 +0200649 data.sibling = sibling;
Cornelia Hucke5945b42005-10-11 08:28:59 -0700650 dev = bus_find_device(&ccw_bus_type, NULL, &data, match_devno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700652 return dev ? to_ccwdev(dev) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653}
654
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100655static int match_orphan(struct device *dev, void *data)
656{
657 struct ccw_dev_id *dev_id;
658 struct ccw_device *cdev;
659
660 dev_id = data;
661 cdev = to_ccwdev(dev);
662 return ccw_dev_id_is_equal(&cdev->private->dev_id, dev_id);
663}
664
665static struct ccw_device *
666get_orphaned_ccwdev_by_dev_id(struct channel_subsystem *css,
667 struct ccw_dev_id *dev_id)
668{
669 struct device *dev;
670
671 dev = device_find_child(&css->pseudo_subchannel->dev, dev_id,
672 match_orphan);
673
674 return dev ? to_ccwdev(dev) : NULL;
675}
676
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677static void
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100678ccw_device_add_changed(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679{
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100680 struct ccw_device_private *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 struct ccw_device *cdev;
682
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100683 priv = container_of(work, struct ccw_device_private, kick_work);
684 cdev = priv->cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 if (device_add(&cdev->dev)) {
686 put_device(&cdev->dev);
687 return;
688 }
689 set_bit(1, &cdev->private->registered);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690}
691
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100692void ccw_device_do_unreg_rereg(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693{
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100694 struct ccw_device_private *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 struct ccw_device *cdev;
696 struct subchannel *sch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100698 priv = container_of(work, struct ccw_device_private, kick_work);
699 cdev = priv->cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 sch = to_subchannel(cdev->dev.parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
Cornelia Huckef995162007-04-27 16:01:39 +0200702 ccw_device_unregister(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 PREPARE_WORK(&cdev->private->kick_work,
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100704 ccw_device_add_changed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 queue_work(ccw_device_work, &cdev->private->kick_work);
706}
707
708static void
709ccw_device_release(struct device *dev)
710{
711 struct ccw_device *cdev;
712
713 cdev = to_ccwdev(dev);
714 kfree(cdev->private);
715 kfree(cdev);
716}
717
Cornelia Huck7674da72006-12-08 15:54:21 +0100718static struct ccw_device * io_subchannel_allocate_dev(struct subchannel *sch)
719{
720 struct ccw_device *cdev;
721
722 cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
723 if (cdev) {
724 cdev->private = kzalloc(sizeof(struct ccw_device_private),
725 GFP_KERNEL | GFP_DMA);
726 if (cdev->private)
727 return cdev;
728 }
729 kfree(cdev);
730 return ERR_PTR(-ENOMEM);
731}
732
733static int io_subchannel_initialize_dev(struct subchannel *sch,
734 struct ccw_device *cdev)
735{
736 cdev->private->cdev = cdev;
737 atomic_set(&cdev->private->onoff, 0);
738 cdev->dev.parent = &sch->dev;
739 cdev->dev.release = ccw_device_release;
Heiko Carstens33583c32007-11-05 11:10:07 +0100740 INIT_WORK(&cdev->private->kick_work, NULL);
Cornelia Huckef995162007-04-27 16:01:39 +0200741 cdev->dev.groups = ccwdev_attr_groups;
Cornelia Huck7674da72006-12-08 15:54:21 +0100742 /* Do first half of device_register. */
743 device_initialize(&cdev->dev);
744 if (!get_device(&sch->dev)) {
745 if (cdev->dev.release)
746 cdev->dev.release(&cdev->dev);
747 return -ENODEV;
748 }
749 return 0;
750}
751
752static struct ccw_device * io_subchannel_create_ccwdev(struct subchannel *sch)
753{
754 struct ccw_device *cdev;
755 int ret;
756
757 cdev = io_subchannel_allocate_dev(sch);
758 if (!IS_ERR(cdev)) {
759 ret = io_subchannel_initialize_dev(sch, cdev);
760 if (ret) {
761 kfree(cdev);
762 cdev = ERR_PTR(ret);
763 }
764 }
765 return cdev;
766}
767
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100768static int io_subchannel_recog(struct ccw_device *, struct subchannel *);
769
770static void sch_attach_device(struct subchannel *sch,
771 struct ccw_device *cdev)
772{
Cornelia Huck82b7ac02007-04-27 16:01:36 +0200773 css_update_ssd_info(sch);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100774 spin_lock_irq(sch->lock);
775 sch->dev.driver_data = cdev;
776 cdev->private->schid = sch->schid;
777 cdev->ccwlock = sch->lock;
778 device_trigger_reprobe(sch);
779 spin_unlock_irq(sch->lock);
780}
781
782static void sch_attach_disconnected_device(struct subchannel *sch,
783 struct ccw_device *cdev)
784{
785 struct subchannel *other_sch;
786 int ret;
787
788 other_sch = to_subchannel(get_device(cdev->dev.parent));
789 ret = device_move(&cdev->dev, &sch->dev);
790 if (ret) {
791 CIO_MSG_EVENT(2, "Moving disconnected device 0.%x.%04x failed "
792 "(ret=%d)!\n", cdev->private->dev_id.ssid,
793 cdev->private->dev_id.devno, ret);
794 put_device(&other_sch->dev);
795 return;
796 }
797 other_sch->dev.driver_data = NULL;
798 /* No need to keep a subchannel without ccw device around. */
799 css_sch_device_unregister(other_sch);
800 put_device(&other_sch->dev);
801 sch_attach_device(sch, cdev);
802}
803
804static void sch_attach_orphaned_device(struct subchannel *sch,
805 struct ccw_device *cdev)
806{
807 int ret;
808
809 /* Try to move the ccw device to its new subchannel. */
810 ret = device_move(&cdev->dev, &sch->dev);
811 if (ret) {
812 CIO_MSG_EVENT(0, "Moving device 0.%x.%04x from orphanage "
813 "failed (ret=%d)!\n",
814 cdev->private->dev_id.ssid,
815 cdev->private->dev_id.devno, ret);
816 return;
817 }
818 sch_attach_device(sch, cdev);
819}
820
821static void sch_create_and_recog_new_device(struct subchannel *sch)
822{
823 struct ccw_device *cdev;
824
825 /* Need to allocate a new ccw device. */
826 cdev = io_subchannel_create_ccwdev(sch);
827 if (IS_ERR(cdev)) {
828 /* OK, we did everything we could... */
829 css_sch_device_unregister(sch);
830 return;
831 }
832 spin_lock_irq(sch->lock);
833 sch->dev.driver_data = cdev;
834 spin_unlock_irq(sch->lock);
835 /* Start recognition for the new ccw device. */
836 if (io_subchannel_recog(cdev, sch)) {
837 spin_lock_irq(sch->lock);
838 sch->dev.driver_data = NULL;
839 spin_unlock_irq(sch->lock);
840 if (cdev->dev.release)
841 cdev->dev.release(&cdev->dev);
842 css_sch_device_unregister(sch);
843 }
844}
845
846
847void ccw_device_move_to_orphanage(struct work_struct *work)
848{
849 struct ccw_device_private *priv;
850 struct ccw_device *cdev;
851 struct ccw_device *replacing_cdev;
852 struct subchannel *sch;
853 int ret;
854 struct channel_subsystem *css;
855 struct ccw_dev_id dev_id;
856
857 priv = container_of(work, struct ccw_device_private, kick_work);
858 cdev = priv->cdev;
859 sch = to_subchannel(cdev->dev.parent);
860 css = to_css(sch->dev.parent);
861 dev_id.devno = sch->schib.pmcw.dev;
862 dev_id.ssid = sch->schid.ssid;
863
864 /*
865 * Move the orphaned ccw device to the orphanage so the replacing
866 * ccw device can take its place on the subchannel.
867 */
868 ret = device_move(&cdev->dev, &css->pseudo_subchannel->dev);
869 if (ret) {
870 CIO_MSG_EVENT(0, "Moving device 0.%x.%04x to orphanage failed "
871 "(ret=%d)!\n", cdev->private->dev_id.ssid,
872 cdev->private->dev_id.devno, ret);
873 return;
874 }
875 cdev->ccwlock = css->pseudo_subchannel->lock;
876 /*
877 * Search for the replacing ccw device
878 * - among the disconnected devices
879 * - in the orphanage
880 */
881 replacing_cdev = get_disc_ccwdev_by_dev_id(&dev_id, cdev);
882 if (replacing_cdev) {
883 sch_attach_disconnected_device(sch, replacing_cdev);
884 return;
885 }
886 replacing_cdev = get_orphaned_ccwdev_by_dev_id(css, &dev_id);
887 if (replacing_cdev) {
888 sch_attach_orphaned_device(sch, replacing_cdev);
889 return;
890 }
891 sch_create_and_recog_new_device(sch);
892}
893
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894/*
895 * Register recognized device.
896 */
897static void
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100898io_subchannel_register(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899{
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100900 struct ccw_device_private *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 struct ccw_device *cdev;
902 struct subchannel *sch;
903 int ret;
904 unsigned long flags;
905
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100906 priv = container_of(work, struct ccw_device_private, kick_work);
907 cdev = priv->cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 sch = to_subchannel(cdev->dev.parent);
Cornelia Huck82b7ac02007-04-27 16:01:36 +0200909 css_update_ssd_info(sch);
Cornelia Huck47af5512006-12-04 15:41:07 +0100910 /*
911 * io_subchannel_register() will also be called after device
912 * recognition has been done for a boxed device (which will already
913 * be registered). We need to reprobe since we may now have sense id
914 * information.
915 */
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700916 if (klist_node_attached(&cdev->dev.knode_parent)) {
Cornelia Huck47af5512006-12-04 15:41:07 +0100917 if (!cdev->drv) {
918 ret = device_reprobe(&cdev->dev);
919 if (ret)
920 /* We can't do much here. */
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200921 CIO_MSG_EVENT(2, "device_reprobe() returned"
922 " %d for 0.%x.%04x\n", ret,
923 cdev->private->dev_id.ssid,
924 cdev->private->dev_id.devno);
Cornelia Huck47af5512006-12-04 15:41:07 +0100925 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 goto out;
927 }
Cornelia Huckfa1a8c22007-04-26 00:12:03 -0700928 /*
929 * Now we know this subchannel will stay, we can throw
930 * our delayed uevent.
931 */
932 sch->dev.uevent_suppress = 0;
933 kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 /* make it known to the system */
935 ret = ccw_device_register(cdev);
936 if (ret) {
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200937 CIO_MSG_EVENT(0, "Could not register ccw dev 0.%x.%04x: %d\n",
938 cdev->private->dev_id.ssid,
939 cdev->private->dev_id.devno, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 put_device(&cdev->dev);
Cornelia Huck2ec22982006-12-08 15:54:26 +0100941 spin_lock_irqsave(sch->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 sch->dev.driver_data = NULL;
Cornelia Huck2ec22982006-12-08 15:54:26 +0100943 spin_unlock_irqrestore(sch->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 kfree (cdev->private);
945 kfree (cdev);
946 put_device(&sch->dev);
947 if (atomic_dec_and_test(&ccw_device_init_count))
948 wake_up(&ccw_device_init_wq);
949 return;
950 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 put_device(&cdev->dev);
952out:
953 cdev->private->flags.recog_done = 1;
954 put_device(&sch->dev);
955 wake_up(&cdev->private->wait_q);
956 if (atomic_dec_and_test(&ccw_device_init_count))
957 wake_up(&ccw_device_init_wq);
958}
959
Cornelia Huck3f4cf6e2007-10-12 16:11:26 +0200960static void ccw_device_call_sch_unregister(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961{
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100962 struct ccw_device_private *priv;
963 struct ccw_device *cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 struct subchannel *sch;
965
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100966 priv = container_of(work, struct ccw_device_private, kick_work);
967 cdev = priv->cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 sch = to_subchannel(cdev->dev.parent);
Cornelia Huck6ab48792006-07-12 16:39:50 +0200969 css_sch_device_unregister(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 /* Reset intparm to zeroes. */
971 sch->schib.pmcw.intparm = 0;
972 cio_modify(sch);
973 put_device(&cdev->dev);
974 put_device(&sch->dev);
975}
976
977/*
978 * subchannel recognition done. Called from the state machine.
979 */
980void
981io_subchannel_recog_done(struct ccw_device *cdev)
982{
983 struct subchannel *sch;
984
985 if (css_init_done == 0) {
986 cdev->private->flags.recog_done = 1;
987 return;
988 }
989 switch (cdev->private->state) {
990 case DEV_STATE_NOT_OPER:
991 cdev->private->flags.recog_done = 1;
992 /* Remove device found not operational. */
993 if (!get_device(&cdev->dev))
994 break;
995 sch = to_subchannel(cdev->dev.parent);
996 PREPARE_WORK(&cdev->private->kick_work,
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100997 ccw_device_call_sch_unregister);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 queue_work(slow_path_wq, &cdev->private->kick_work);
999 if (atomic_dec_and_test(&ccw_device_init_count))
1000 wake_up(&ccw_device_init_wq);
1001 break;
1002 case DEV_STATE_BOXED:
1003 /* Device did not respond in time. */
1004 case DEV_STATE_OFFLINE:
1005 /*
1006 * We can't register the device in interrupt context so
1007 * we schedule a work item.
1008 */
1009 if (!get_device(&cdev->dev))
1010 break;
1011 PREPARE_WORK(&cdev->private->kick_work,
Martin Schwidefskyc1637532006-12-08 15:53:57 +01001012 io_subchannel_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 queue_work(slow_path_wq, &cdev->private->kick_work);
1014 break;
1015 }
1016}
1017
1018static int
1019io_subchannel_recog(struct ccw_device *cdev, struct subchannel *sch)
1020{
1021 int rc;
1022 struct ccw_device_private *priv;
1023
1024 sch->dev.driver_data = cdev;
1025 sch->driver = &io_subchannel_driver;
Cornelia Huck2ec22982006-12-08 15:54:26 +01001026 cdev->ccwlock = sch->lock;
Cornelia Huckfb6958a2006-01-06 00:19:25 -08001027
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 /* Init private data. */
1029 priv = cdev->private;
Cornelia Huck78964262006-10-11 15:31:38 +02001030 priv->dev_id.devno = sch->schib.pmcw.dev;
1031 priv->dev_id.ssid = sch->schid.ssid;
1032 priv->schid = sch->schid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 priv->state = DEV_STATE_NOT_OPER;
1034 INIT_LIST_HEAD(&priv->cmb_list);
1035 init_waitqueue_head(&priv->wait_q);
1036 init_timer(&priv->timer);
1037
1038 /* Set an initial name for the device. */
Cornelia Huckfb6958a2006-01-06 00:19:25 -08001039 snprintf (cdev->dev.bus_id, BUS_ID_SIZE, "0.%x.%04x",
1040 sch->schid.ssid, sch->schib.pmcw.dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041
1042 /* Increase counter of devices currently in recognition. */
1043 atomic_inc(&ccw_device_init_count);
1044
1045 /* Start async. device sensing. */
Cornelia Huck2ec22982006-12-08 15:54:26 +01001046 spin_lock_irq(sch->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 rc = ccw_device_recognition(cdev);
Cornelia Huck2ec22982006-12-08 15:54:26 +01001048 spin_unlock_irq(sch->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 if (rc) {
1050 if (atomic_dec_and_test(&ccw_device_init_count))
1051 wake_up(&ccw_device_init_wq);
1052 }
1053 return rc;
1054}
1055
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +01001056static void ccw_device_move_to_sch(struct work_struct *work)
1057{
1058 struct ccw_device_private *priv;
1059 int rc;
1060 struct subchannel *sch;
1061 struct ccw_device *cdev;
1062 struct subchannel *former_parent;
1063
1064 priv = container_of(work, struct ccw_device_private, kick_work);
1065 sch = priv->sch;
1066 cdev = priv->cdev;
1067 former_parent = ccw_device_is_orphan(cdev) ?
1068 NULL : to_subchannel(get_device(cdev->dev.parent));
1069 mutex_lock(&sch->reg_mutex);
1070 /* Try to move the ccw device to its new subchannel. */
1071 rc = device_move(&cdev->dev, &sch->dev);
1072 mutex_unlock(&sch->reg_mutex);
1073 if (rc) {
1074 CIO_MSG_EVENT(2, "Moving device 0.%x.%04x to subchannel "
1075 "0.%x.%04x failed (ret=%d)!\n",
1076 cdev->private->dev_id.ssid,
1077 cdev->private->dev_id.devno, sch->schid.ssid,
1078 sch->schid.sch_no, rc);
1079 css_sch_device_unregister(sch);
1080 goto out;
1081 }
1082 if (former_parent) {
1083 spin_lock_irq(former_parent->lock);
1084 former_parent->dev.driver_data = NULL;
1085 spin_unlock_irq(former_parent->lock);
1086 css_sch_device_unregister(former_parent);
1087 /* Reset intparm to zeroes. */
1088 former_parent->schib.pmcw.intparm = 0;
1089 cio_modify(former_parent);
1090 }
1091 sch_attach_device(sch, cdev);
1092out:
1093 if (former_parent)
1094 put_device(&former_parent->dev);
1095 put_device(&cdev->dev);
1096}
1097
Cornelia Huck602b20f2008-01-26 14:10:39 +01001098static void io_subchannel_irq(struct subchannel *sch)
1099{
1100 struct ccw_device *cdev;
1101
1102 cdev = sch->dev.driver_data;
1103
1104 CIO_TRACE_EVENT(3, "IRQ");
1105 CIO_TRACE_EVENT(3, sch->dev.bus_id);
1106 if (cdev)
1107 dev_fsm_event(cdev, DEV_EVENT_INTERRUPT);
1108}
1109
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110static int
Cornelia Huck8bbace72006-01-11 10:56:22 +01001111io_subchannel_probe (struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 struct ccw_device *cdev;
1114 int rc;
1115 unsigned long flags;
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +01001116 struct ccw_dev_id dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 if (sch->dev.driver_data) {
1119 /*
1120 * This subchannel already has an associated ccw_device.
1121 * Register it and exit. This happens for all early
1122 * device, e.g. the console.
1123 */
1124 cdev = sch->dev.driver_data;
Cornelia Hucke1031782007-10-12 16:11:23 +02001125 cdev->dev.groups = ccwdev_attr_groups;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 device_initialize(&cdev->dev);
1127 ccw_device_register(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 /*
1129 * Check if the device is already online. If it is
1130 * the reference count needs to be corrected
1131 * (see ccw_device_online and css_init_done for the
1132 * ugly details).
1133 */
1134 if (cdev->private->state != DEV_STATE_NOT_OPER &&
1135 cdev->private->state != DEV_STATE_OFFLINE &&
1136 cdev->private->state != DEV_STATE_BOXED)
1137 get_device(&cdev->dev);
1138 return 0;
1139 }
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +01001140 /*
1141 * First check if a fitting device may be found amongst the
1142 * disconnected devices or in the orphanage.
1143 */
1144 dev_id.devno = sch->schib.pmcw.dev;
1145 dev_id.ssid = sch->schid.ssid;
1146 cdev = get_disc_ccwdev_by_dev_id(&dev_id, NULL);
1147 if (!cdev)
1148 cdev = get_orphaned_ccwdev_by_dev_id(to_css(sch->dev.parent),
1149 &dev_id);
1150 if (cdev) {
1151 /*
1152 * Schedule moving the device until when we have a registered
1153 * subchannel to move to and succeed the probe. We can
1154 * unregister later again, when the probe is through.
1155 */
1156 cdev->private->sch = sch;
1157 PREPARE_WORK(&cdev->private->kick_work,
1158 ccw_device_move_to_sch);
1159 queue_work(slow_path_wq, &cdev->private->kick_work);
1160 return 0;
1161 }
Cornelia Huck7674da72006-12-08 15:54:21 +01001162 cdev = io_subchannel_create_ccwdev(sch);
1163 if (IS_ERR(cdev))
1164 return PTR_ERR(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165
Cornelia Huck8bbace72006-01-11 10:56:22 +01001166 rc = io_subchannel_recog(cdev, sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 if (rc) {
Cornelia Huck2ec22982006-12-08 15:54:26 +01001168 spin_lock_irqsave(sch->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 sch->dev.driver_data = NULL;
Cornelia Huck2ec22982006-12-08 15:54:26 +01001170 spin_unlock_irqrestore(sch->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 if (cdev->dev.release)
1172 cdev->dev.release(&cdev->dev);
1173 }
1174
1175 return rc;
1176}
1177
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178static int
Cornelia Huck8bbace72006-01-11 10:56:22 +01001179io_subchannel_remove (struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180{
1181 struct ccw_device *cdev;
1182 unsigned long flags;
1183
Cornelia Huck8bbace72006-01-11 10:56:22 +01001184 if (!sch->dev.driver_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 return 0;
Cornelia Huck8bbace72006-01-11 10:56:22 +01001186 cdev = sch->dev.driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 /* Set ccw device to not operational and drop reference. */
1188 spin_lock_irqsave(cdev->ccwlock, flags);
Cornelia Huck8bbace72006-01-11 10:56:22 +01001189 sch->dev.driver_data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 cdev->private->state = DEV_STATE_NOT_OPER;
1191 spin_unlock_irqrestore(cdev->ccwlock, flags);
Cornelia Huckef995162007-04-27 16:01:39 +02001192 ccw_device_unregister(cdev);
1193 put_device(&cdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 return 0;
1195}
1196
Cornelia Huck602b20f2008-01-26 14:10:39 +01001197static int io_subchannel_notify(struct subchannel *sch, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198{
1199 struct ccw_device *cdev;
1200
Cornelia Huck602b20f2008-01-26 14:10:39 +01001201 cdev = sch->dev.driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 if (!cdev)
1203 return 0;
1204 if (!cdev->drv)
1205 return 0;
1206 if (!cdev->online)
1207 return 0;
1208 return cdev->drv->notify ? cdev->drv->notify(cdev, event) : 0;
1209}
1210
Cornelia Huck602b20f2008-01-26 14:10:39 +01001211static void io_subchannel_verify(struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212{
1213 struct ccw_device *cdev;
1214
Cornelia Huck602b20f2008-01-26 14:10:39 +01001215 cdev = sch->dev.driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 if (cdev)
1217 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1218}
1219
Cornelia Huck602b20f2008-01-26 14:10:39 +01001220static void io_subchannel_ioterm(struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221{
1222 struct ccw_device *cdev;
1223
Cornelia Huck602b20f2008-01-26 14:10:39 +01001224 cdev = sch->dev.driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 if (!cdev)
1226 return;
Cornelia Huckd23861f2006-12-04 15:41:04 +01001227 /* Internal I/O will be retried by the interrupt handler. */
1228 if (cdev->private->flags.intretry)
1229 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 cdev->private->state = DEV_STATE_CLEAR_VERIFY;
1231 if (cdev->handler)
1232 cdev->handler(cdev, cdev->private->intparm,
1233 ERR_PTR(-EIO));
1234}
1235
1236static void
Cornelia Huck8bbace72006-01-11 10:56:22 +01001237io_subchannel_shutdown(struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 struct ccw_device *cdev;
1240 int ret;
1241
Cornelia Huck8bbace72006-01-11 10:56:22 +01001242 cdev = sch->dev.driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243
Cornelia Hucka8237fc2006-01-06 00:19:21 -08001244 if (cio_is_console(sch->schid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 return;
1246 if (!sch->schib.pmcw.ena)
1247 /* Nothing to do. */
1248 return;
1249 ret = cio_disable_subchannel(sch);
1250 if (ret != -EBUSY)
1251 /* Subchannel is disabled, we're done. */
1252 return;
1253 cdev->private->state = DEV_STATE_QUIESCE;
1254 if (cdev->handler)
1255 cdev->handler(cdev, cdev->private->intparm,
1256 ERR_PTR(-EIO));
1257 ret = ccw_device_cancel_halt_clear(cdev);
1258 if (ret == -EBUSY) {
1259 ccw_device_set_timeout(cdev, HZ/10);
1260 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
1261 }
1262 cio_disable_subchannel(sch);
1263}
1264
1265#ifdef CONFIG_CCW_CONSOLE
1266static struct ccw_device console_cdev;
1267static struct ccw_device_private console_private;
1268static int console_cdev_in_use;
1269
Cornelia Huck2ec22982006-12-08 15:54:26 +01001270static DEFINE_SPINLOCK(ccw_console_lock);
1271
1272spinlock_t * cio_get_console_lock(void)
1273{
1274 return &ccw_console_lock;
1275}
1276
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277static int
1278ccw_device_console_enable (struct ccw_device *cdev, struct subchannel *sch)
1279{
1280 int rc;
1281
1282 /* Initialize the ccw_device structure. */
Heiko Carstens292888c2006-08-30 14:33:35 +02001283 cdev->dev.parent= &sch->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 rc = io_subchannel_recog(cdev, sch);
1285 if (rc)
1286 return rc;
1287
1288 /* Now wait for the async. recognition to come to an end. */
1289 spin_lock_irq(cdev->ccwlock);
1290 while (!dev_fsm_final_state(cdev))
1291 wait_cons_dev();
1292 rc = -EIO;
1293 if (cdev->private->state != DEV_STATE_OFFLINE)
1294 goto out_unlock;
1295 ccw_device_online(cdev);
1296 while (!dev_fsm_final_state(cdev))
1297 wait_cons_dev();
1298 if (cdev->private->state != DEV_STATE_ONLINE)
1299 goto out_unlock;
1300 rc = 0;
1301out_unlock:
1302 spin_unlock_irq(cdev->ccwlock);
1303 return 0;
1304}
1305
1306struct ccw_device *
1307ccw_device_probe_console(void)
1308{
1309 struct subchannel *sch;
1310 int ret;
1311
1312 if (xchg(&console_cdev_in_use, 1) != 0)
Peter Oberparleiter600b5d12006-02-01 03:06:35 -08001313 return ERR_PTR(-EBUSY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 sch = cio_probe_console();
1315 if (IS_ERR(sch)) {
1316 console_cdev_in_use = 0;
1317 return (void *) sch;
1318 }
1319 memset(&console_cdev, 0, sizeof(struct ccw_device));
1320 memset(&console_private, 0, sizeof(struct ccw_device_private));
1321 console_cdev.private = &console_private;
Martin Schwidefskyc1637532006-12-08 15:53:57 +01001322 console_private.cdev = &console_cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 ret = ccw_device_console_enable(&console_cdev, sch);
1324 if (ret) {
1325 cio_release_console();
1326 console_cdev_in_use = 0;
1327 return ERR_PTR(ret);
1328 }
1329 console_cdev.online = 1;
1330 return &console_cdev;
1331}
1332#endif
1333
1334/*
1335 * get ccw_device matching the busid, but only if owned by cdrv
1336 */
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001337static int
1338__ccwdev_check_busid(struct device *dev, void *id)
1339{
1340 char *bus_id;
1341
Cornelia Huck12975ae2006-10-11 15:31:47 +02001342 bus_id = id;
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001343
1344 return (strncmp(bus_id, dev->bus_id, BUS_ID_SIZE) == 0);
1345}
1346
1347
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +02001348/**
1349 * get_ccwdev_by_busid() - obtain device from a bus id
1350 * @cdrv: driver the device is owned by
1351 * @bus_id: bus id of the device to be searched
1352 *
1353 * This function searches all devices owned by @cdrv for a device with a bus
1354 * id matching @bus_id.
1355 * Returns:
1356 * If a match is found, its reference count of the found device is increased
1357 * and it is returned; else %NULL is returned.
1358 */
1359struct ccw_device *get_ccwdev_by_busid(struct ccw_driver *cdrv,
1360 const char *bus_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361{
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001362 struct device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 struct device_driver *drv;
1364
1365 drv = get_driver(&cdrv->driver);
1366 if (!drv)
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001367 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001369 dev = driver_find_device(drv, NULL, (void *)bus_id,
1370 __ccwdev_check_busid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 put_driver(drv);
1372
Heiko Carstensd2c993d2006-07-12 16:41:55 +02001373 return dev ? to_ccwdev(dev) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374}
1375
1376/************************** device driver handling ************************/
1377
1378/* This is the implementation of the ccw_driver class. The probe, remove
1379 * and release methods are initially very similar to the device_driver
1380 * implementations, with the difference that they have ccw_device
1381 * arguments.
1382 *
1383 * A ccw driver also contains the information that is needed for
1384 * device matching.
1385 */
1386static int
1387ccw_device_probe (struct device *dev)
1388{
1389 struct ccw_device *cdev = to_ccwdev(dev);
1390 struct ccw_driver *cdrv = to_ccwdrv(dev->driver);
1391 int ret;
1392
1393 cdev->drv = cdrv; /* to let the driver call _set_online */
1394
1395 ret = cdrv->probe ? cdrv->probe(cdev) : -ENODEV;
1396
1397 if (ret) {
Heiko Carstensd2c993d2006-07-12 16:41:55 +02001398 cdev->drv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 return ret;
1400 }
1401
1402 return 0;
1403}
1404
1405static int
1406ccw_device_remove (struct device *dev)
1407{
1408 struct ccw_device *cdev = to_ccwdev(dev);
1409 struct ccw_driver *cdrv = cdev->drv;
1410 int ret;
1411
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 if (cdrv->remove)
1413 cdrv->remove(cdev);
1414 if (cdev->online) {
1415 cdev->online = 0;
1416 spin_lock_irq(cdev->ccwlock);
1417 ret = ccw_device_offline(cdev);
1418 spin_unlock_irq(cdev->ccwlock);
1419 if (ret == 0)
1420 wait_event(cdev->private->wait_q,
1421 dev_fsm_final_state(cdev));
1422 else
1423 //FIXME: we can't fail!
Cornelia Hucke556bbb2007-07-27 12:29:19 +02001424 CIO_MSG_EVENT(2, "ccw_device_offline returned %d, "
1425 "device 0.%x.%04x\n",
1426 ret, cdev->private->dev_id.ssid,
1427 cdev->private->dev_id.devno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 }
1429 ccw_device_set_timeout(cdev, 0);
Heiko Carstensd2c993d2006-07-12 16:41:55 +02001430 cdev->drv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 return 0;
1432}
1433
Cornelia Huck958974fb2007-10-12 16:11:21 +02001434static void ccw_device_shutdown(struct device *dev)
1435{
1436 struct ccw_device *cdev;
1437
1438 cdev = to_ccwdev(dev);
1439 if (cdev->drv && cdev->drv->shutdown)
1440 cdev->drv->shutdown(cdev);
Cornelia Huck1842f2b2007-10-12 16:11:22 +02001441 disable_cmf(cdev);
Cornelia Huck958974fb2007-10-12 16:11:21 +02001442}
1443
Cornelia Huck8bbace72006-01-11 10:56:22 +01001444struct bus_type ccw_bus_type = {
1445 .name = "ccw",
1446 .match = ccw_bus_match,
1447 .uevent = ccw_uevent,
1448 .probe = ccw_device_probe,
1449 .remove = ccw_device_remove,
Cornelia Huck958974fb2007-10-12 16:11:21 +02001450 .shutdown = ccw_device_shutdown,
Cornelia Huck8bbace72006-01-11 10:56:22 +01001451};
1452
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +02001453/**
1454 * ccw_driver_register() - register a ccw driver
1455 * @cdriver: driver to be registered
1456 *
1457 * This function is mainly a wrapper around driver_register().
1458 * Returns:
1459 * %0 on success and a negative error value on failure.
1460 */
1461int ccw_driver_register(struct ccw_driver *cdriver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462{
1463 struct device_driver *drv = &cdriver->driver;
1464
1465 drv->bus = &ccw_bus_type;
1466 drv->name = cdriver->name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467
1468 return driver_register(drv);
1469}
1470
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +02001471/**
1472 * ccw_driver_unregister() - deregister a ccw driver
1473 * @cdriver: driver to be deregistered
1474 *
1475 * This function is mainly a wrapper around driver_unregister().
1476 */
1477void ccw_driver_unregister(struct ccw_driver *cdriver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478{
1479 driver_unregister(&cdriver->driver);
1480}
1481
Cornelia Hucka8237fc2006-01-06 00:19:21 -08001482/* Helper func for qdio. */
1483struct subchannel_id
1484ccw_device_get_subchannel_id(struct ccw_device *cdev)
1485{
1486 struct subchannel *sch;
1487
1488 sch = to_subchannel(cdev->dev.parent);
1489 return sch->schid;
1490}
1491
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492MODULE_LICENSE("GPL");
1493EXPORT_SYMBOL(ccw_device_set_online);
1494EXPORT_SYMBOL(ccw_device_set_offline);
1495EXPORT_SYMBOL(ccw_driver_register);
1496EXPORT_SYMBOL(ccw_driver_unregister);
1497EXPORT_SYMBOL(get_ccwdev_by_busid);
1498EXPORT_SYMBOL(ccw_bus_type);
1499EXPORT_SYMBOL(ccw_device_work);
1500EXPORT_SYMBOL(ccw_device_notify_work);
Cornelia Hucka8237fc2006-01-06 00:19:21 -08001501EXPORT_SYMBOL_GPL(ccw_device_get_subchannel_id);