blob: 39f02b48e4c7dbf46ac7cf8defe35d9cbb257f38 [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. */
82static int ccw_uevent(struct device *dev, char **envp, int num_envp,
83 char *buffer, int buffer_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -070084{
85 struct ccw_device *cdev = to_ccwdev(dev);
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +020086 struct ccw_device_id *id = &(cdev->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 int i = 0;
Cornelia Huckcfbe9bb2007-04-27 16:01:32 +020088 int len = 0;
89 int ret;
90 char modalias_buf[30];
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +020092 /* CU_TYPE= */
Cornelia Huckcfbe9bb2007-04-27 16:01:32 +020093 ret = add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &len,
94 "CU_TYPE=%04X", id->cu_type);
95 if (ret)
96 return ret;
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +020097
98 /* CU_MODEL= */
Cornelia Huckcfbe9bb2007-04-27 16:01:32 +020099 ret = add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &len,
100 "CU_MODEL=%02X", id->cu_model);
101 if (ret)
102 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
104 /* The next two can be zero, that's ok for us */
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +0200105 /* DEV_TYPE= */
Cornelia Huckcfbe9bb2007-04-27 16:01:32 +0200106 ret = add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &len,
107 "DEV_TYPE=%04X", id->dev_type);
108 if (ret)
109 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +0200111 /* DEV_MODEL= */
Cornelia Huckcfbe9bb2007-04-27 16:01:32 +0200112 ret = add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &len,
113 "DEV_MODEL=%02X", id->dev_model);
114 if (ret)
115 return ret;
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +0200116
117 /* MODALIAS= */
Cornelia Huckcfbe9bb2007-04-27 16:01:32 +0200118 snprint_alias(modalias_buf, sizeof(modalias_buf), id, "");
119 ret = add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &len,
120 "MODALIAS=%s", modalias_buf);
Cornelia Huck3520c922007-08-22 13:51:36 +0200121 if (ret)
122 return ret;
123 envp[i] = NULL;
124 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125}
126
Cornelia Huck8bbace72006-01-11 10:56:22 +0100127struct bus_type ccw_bus_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
Cornelia Huck8bbace72006-01-11 10:56:22 +0100129static int io_subchannel_probe (struct subchannel *);
130static int io_subchannel_remove (struct subchannel *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131static int io_subchannel_notify(struct device *, int);
132static void io_subchannel_verify(struct device *);
133static void io_subchannel_ioterm(struct device *);
Cornelia Huck8bbace72006-01-11 10:56:22 +0100134static void io_subchannel_shutdown(struct subchannel *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
Cornelia Huckf7e5d672007-05-10 15:45:43 +0200136static struct css_driver io_subchannel_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 .subchannel_type = SUBCHANNEL_TYPE_IO,
138 .drv = {
139 .name = "io_subchannel",
140 .bus = &css_bus_type,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 },
142 .irq = io_subchannel_irq,
143 .notify = io_subchannel_notify,
144 .verify = io_subchannel_verify,
145 .termination = io_subchannel_ioterm,
Cornelia Huck8bbace72006-01-11 10:56:22 +0100146 .probe = io_subchannel_probe,
147 .remove = io_subchannel_remove,
148 .shutdown = io_subchannel_shutdown,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149};
150
151struct workqueue_struct *ccw_device_work;
152struct workqueue_struct *ccw_device_notify_work;
Peter Oberparleiter40154b82006-06-29 14:57:03 +0200153wait_queue_head_t ccw_device_init_wq;
154atomic_t ccw_device_init_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
156static int __init
157init_ccw_bus_type (void)
158{
159 int ret;
160
161 init_waitqueue_head(&ccw_device_init_wq);
162 atomic_set(&ccw_device_init_count, 0);
163
164 ccw_device_work = create_singlethread_workqueue("cio");
165 if (!ccw_device_work)
166 return -ENOMEM; /* FIXME: better errno ? */
167 ccw_device_notify_work = create_singlethread_workqueue("cio_notify");
168 if (!ccw_device_notify_work) {
169 ret = -ENOMEM; /* FIXME: better errno ? */
170 goto out_err;
171 }
172 slow_path_wq = create_singlethread_workqueue("kslowcrw");
173 if (!slow_path_wq) {
174 ret = -ENOMEM; /* FIXME: better errno ? */
175 goto out_err;
176 }
177 if ((ret = bus_register (&ccw_bus_type)))
178 goto out_err;
179
180 if ((ret = driver_register(&io_subchannel_driver.drv)))
181 goto out_err;
182
183 wait_event(ccw_device_init_wq,
184 atomic_read(&ccw_device_init_count) == 0);
185 flush_workqueue(ccw_device_work);
186 return 0;
187out_err:
188 if (ccw_device_work)
189 destroy_workqueue(ccw_device_work);
190 if (ccw_device_notify_work)
191 destroy_workqueue(ccw_device_notify_work);
192 if (slow_path_wq)
193 destroy_workqueue(slow_path_wq);
194 return ret;
195}
196
197static void __exit
198cleanup_ccw_bus_type (void)
199{
200 driver_unregister(&io_subchannel_driver.drv);
201 bus_unregister(&ccw_bus_type);
202 destroy_workqueue(ccw_device_notify_work);
203 destroy_workqueue(ccw_device_work);
204}
205
206subsys_initcall(init_ccw_bus_type);
207module_exit(cleanup_ccw_bus_type);
208
209/************************ device handling **************************/
210
211/*
212 * A ccw_device has some interfaces in sysfs in addition to the
213 * standard ones.
214 * The following entries are designed to export the information which
215 * resided in 2.4 in /proc/subchannels. Subchannel and device number
216 * are obvious, so they don't have an entry :)
217 * TODO: Split chpids and pimpampom up? Where is "in use" in the tree?
218 */
219static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400220chpids_show (struct device * dev, struct device_attribute *attr, char * buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221{
222 struct subchannel *sch = to_subchannel(dev);
Peter Oberparleiter7ad6a242007-04-27 16:01:35 +0200223 struct chsc_ssd_info *ssd = &sch->ssd_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 ssize_t ret = 0;
225 int chp;
Peter Oberparleiter7ad6a242007-04-27 16:01:35 +0200226 int mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
Peter Oberparleiter7ad6a242007-04-27 16:01:35 +0200228 for (chp = 0; chp < 8; chp++) {
229 mask = 0x80 >> chp;
230 if (ssd->path_mask & mask)
231 ret += sprintf(buf + ret, "%02x ", ssd->chpid[chp].id);
232 else
233 ret += sprintf(buf + ret, "00 ");
234 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 ret += sprintf (buf+ret, "\n");
236 return min((ssize_t)PAGE_SIZE, ret);
237}
238
239static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400240pimpampom_show (struct device * dev, struct device_attribute *attr, char * buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241{
242 struct subchannel *sch = to_subchannel(dev);
243 struct pmcw *pmcw = &sch->schib.pmcw;
244
245 return sprintf (buf, "%02x %02x %02x\n",
246 pmcw->pim, pmcw->pam, pmcw->pom);
247}
248
249static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400250devtype_show (struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251{
252 struct ccw_device *cdev = to_ccwdev(dev);
253 struct ccw_device_id *id = &(cdev->id);
254
255 if (id->dev_type != 0)
256 return sprintf(buf, "%04x/%02x\n",
257 id->dev_type, id->dev_model);
258 else
259 return sprintf(buf, "n/a\n");
260}
261
262static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400263cutype_show (struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264{
265 struct ccw_device *cdev = to_ccwdev(dev);
266 struct ccw_device_id *id = &(cdev->id);
267
268 return sprintf(buf, "%04x/%02x\n",
269 id->cu_type, id->cu_model);
270}
271
272static ssize_t
Bastian Blankf1fc78a2005-10-30 15:00:12 -0800273modalias_show (struct device *dev, struct device_attribute *attr, char *buf)
274{
275 struct ccw_device *cdev = to_ccwdev(dev);
276 struct ccw_device_id *id = &(cdev->id);
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +0200277 int len;
Bastian Blankf1fc78a2005-10-30 15:00:12 -0800278
Cornelia Huck086a6c62007-07-17 13:36:08 +0200279 len = snprint_alias(buf, PAGE_SIZE, id, "\n");
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +0200280
281 return len > PAGE_SIZE ? PAGE_SIZE : len;
Bastian Blankf1fc78a2005-10-30 15:00:12 -0800282}
283
284static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400285online_show (struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286{
287 struct ccw_device *cdev = to_ccwdev(dev);
288
289 return sprintf(buf, cdev->online ? "1\n" : "0\n");
290}
291
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100292int ccw_device_is_orphan(struct ccw_device *cdev)
293{
294 return sch_is_pseudo_sch(to_subchannel(cdev->dev.parent));
295}
296
Cornelia Huckef995162007-04-27 16:01:39 +0200297static void ccw_device_unregister(struct ccw_device *cdev)
Cornelia Huck7674da72006-12-08 15:54:21 +0100298{
Cornelia Huck7674da72006-12-08 15:54:21 +0100299 if (test_and_clear_bit(1, &cdev->private->registered))
Cornelia Huckef995162007-04-27 16:01:39 +0200300 device_del(&cdev->dev);
Cornelia Huck7674da72006-12-08 15:54:21 +0100301}
302
Cornelia Huck59a8a6e2007-05-31 17:38:06 +0200303static void ccw_device_remove_orphan_cb(struct device *dev)
304{
305 struct ccw_device *cdev = to_ccwdev(dev);
306
307 ccw_device_unregister(cdev);
308 put_device(&cdev->dev);
309}
310
311static void ccw_device_remove_sch_cb(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312{
313 struct subchannel *sch;
Cornelia Huck59a8a6e2007-05-31 17:38:06 +0200314
315 sch = to_subchannel(dev);
Cornelia Huck6ab48792006-07-12 16:39:50 +0200316 css_sch_device_unregister(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 /* Reset intparm to zeroes. */
318 sch->schib.pmcw.intparm = 0;
319 cio_modify(sch);
320 put_device(&sch->dev);
321}
322
Cornelia Huck59a8a6e2007-05-31 17:38:06 +0200323static void
324ccw_device_remove_disconnected(struct ccw_device *cdev)
325{
326 unsigned long flags;
327 int rc;
328
329 /*
330 * Forced offline in disconnected state means
331 * 'throw away device'.
332 */
333 if (ccw_device_is_orphan(cdev)) {
334 /*
335 * Deregister ccw device.
336 * Unfortunately, we cannot do this directly from the
337 * attribute method.
338 */
339 spin_lock_irqsave(cdev->ccwlock, flags);
340 cdev->private->state = DEV_STATE_NOT_OPER;
341 spin_unlock_irqrestore(cdev->ccwlock, flags);
342 rc = device_schedule_callback(&cdev->dev,
343 ccw_device_remove_orphan_cb);
344 if (rc)
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200345 CIO_MSG_EVENT(2, "Couldn't unregister orphan "
346 "0.%x.%04x\n",
347 cdev->private->dev_id.ssid,
348 cdev->private->dev_id.devno);
Cornelia Huck59a8a6e2007-05-31 17:38:06 +0200349 return;
350 }
351 /* Deregister subchannel, which will kill the ccw device. */
352 rc = device_schedule_callback(cdev->dev.parent,
353 ccw_device_remove_sch_cb);
354 if (rc)
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200355 CIO_MSG_EVENT(2, "Couldn't unregister disconnected device "
356 "0.%x.%04x\n",
357 cdev->private->dev_id.ssid,
358 cdev->private->dev_id.devno);
Cornelia Huck59a8a6e2007-05-31 17:38:06 +0200359}
360
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +0200361/**
362 * ccw_device_set_offline() - disable a ccw device for I/O
363 * @cdev: target ccw device
364 *
365 * This function calls the driver's set_offline() function for @cdev, if
366 * given, and then disables @cdev.
367 * Returns:
368 * %0 on success and a negative error value on failure.
369 * Context:
370 * enabled, ccw device lock not held
371 */
372int ccw_device_set_offline(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373{
374 int ret;
375
376 if (!cdev)
377 return -ENODEV;
378 if (!cdev->online || !cdev->drv)
379 return -EINVAL;
380
381 if (cdev->drv->set_offline) {
382 ret = cdev->drv->set_offline(cdev);
383 if (ret != 0)
384 return ret;
385 }
386 cdev->online = 0;
387 spin_lock_irq(cdev->ccwlock);
388 ret = ccw_device_offline(cdev);
389 if (ret == -ENODEV) {
390 if (cdev->private->state != DEV_STATE_NOT_OPER) {
391 cdev->private->state = DEV_STATE_OFFLINE;
392 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
393 }
394 spin_unlock_irq(cdev->ccwlock);
395 return ret;
396 }
397 spin_unlock_irq(cdev->ccwlock);
398 if (ret == 0)
399 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
400 else {
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200401 CIO_MSG_EVENT(2, "ccw_device_offline returned %d, "
402 "device 0.%x.%04x\n",
403 ret, cdev->private->dev_id.ssid,
404 cdev->private->dev_id.devno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 cdev->online = 1;
406 }
407 return ret;
408}
409
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +0200410/**
411 * ccw_device_set_online() - enable a ccw device for I/O
412 * @cdev: target ccw device
413 *
414 * This function first enables @cdev and then calls the driver's set_online()
415 * function for @cdev, if given. If set_online() returns an error, @cdev is
416 * disabled again.
417 * Returns:
418 * %0 on success and a negative error value on failure.
419 * Context:
420 * enabled, ccw device lock not held
421 */
422int ccw_device_set_online(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423{
424 int ret;
425
426 if (!cdev)
427 return -ENODEV;
428 if (cdev->online || !cdev->drv)
429 return -EINVAL;
430
431 spin_lock_irq(cdev->ccwlock);
432 ret = ccw_device_online(cdev);
433 spin_unlock_irq(cdev->ccwlock);
434 if (ret == 0)
435 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
436 else {
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200437 CIO_MSG_EVENT(2, "ccw_device_online returned %d, "
438 "device 0.%x.%04x\n",
439 ret, cdev->private->dev_id.ssid,
440 cdev->private->dev_id.devno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 return ret;
442 }
443 if (cdev->private->state != DEV_STATE_ONLINE)
444 return -ENODEV;
445 if (!cdev->drv->set_online || cdev->drv->set_online(cdev) == 0) {
446 cdev->online = 1;
447 return 0;
448 }
449 spin_lock_irq(cdev->ccwlock);
450 ret = ccw_device_offline(cdev);
451 spin_unlock_irq(cdev->ccwlock);
452 if (ret == 0)
453 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200454 else
455 CIO_MSG_EVENT(2, "ccw_device_offline returned %d, "
456 "device 0.%x.%04x\n",
457 ret, cdev->private->dev_id.ssid,
458 cdev->private->dev_id.devno);
Cornelia Huck6cadb782006-02-17 13:52:49 -0800459 return (ret == 0) ? -ENODEV : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460}
461
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200462static void online_store_handle_offline(struct ccw_device *cdev)
463{
464 if (cdev->private->state == DEV_STATE_DISCONNECTED)
465 ccw_device_remove_disconnected(cdev);
466 else if (cdev->drv && cdev->drv->set_offline)
467 ccw_device_set_offline(cdev);
468}
469
470static int online_store_recog_and_online(struct ccw_device *cdev)
471{
472 int ret;
473
474 /* Do device recognition, if needed. */
475 if (cdev->id.cu_type == 0) {
476 ret = ccw_device_recognition(cdev);
477 if (ret) {
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200478 CIO_MSG_EVENT(0, "Couldn't start recognition "
479 "for device 0.%x.%04x (ret=%d)\n",
480 cdev->private->dev_id.ssid,
481 cdev->private->dev_id.devno, ret);
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200482 return ret;
483 }
484 wait_event(cdev->private->wait_q,
485 cdev->private->flags.recog_done);
486 }
487 if (cdev->drv && cdev->drv->set_online)
488 ccw_device_set_online(cdev);
489 return 0;
490}
491static void online_store_handle_online(struct ccw_device *cdev, int force)
492{
493 int ret;
494
495 ret = online_store_recog_and_online(cdev);
496 if (ret)
497 return;
498 if (force && cdev->private->state == DEV_STATE_BOXED) {
499 ret = ccw_device_stlck(cdev);
500 if (ret) {
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200501 dev_warn(&cdev->dev,
502 "ccw_device_stlck returned %d!\n", ret);
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200503 return;
504 }
505 if (cdev->id.cu_type == 0)
506 cdev->private->state = DEV_STATE_NOT_OPER;
507 online_store_recog_and_online(cdev);
508 }
509
510}
511
512static ssize_t online_store (struct device *dev, struct device_attribute *attr,
513 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514{
515 struct ccw_device *cdev = to_ccwdev(dev);
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200516 int i, force;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 char *tmp;
518
Martin Schwidefsky973bd992006-01-06 00:19:07 -0800519 if (atomic_cmpxchg(&cdev->private->onoff, 0, 1) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 return -EAGAIN;
521
522 if (cdev->drv && !try_module_get(cdev->drv->owner)) {
523 atomic_set(&cdev->private->onoff, 0);
524 return -EINVAL;
525 }
526 if (!strncmp(buf, "force\n", count)) {
527 force = 1;
528 i = 1;
529 } else {
530 force = 0;
531 i = simple_strtoul(buf, &tmp, 16);
532 }
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200533
534 switch (i) {
535 case 0:
536 online_store_handle_offline(cdev);
537 break;
538 case 1:
539 online_store_handle_online(cdev, force);
540 break;
541 default:
542 count = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 if (cdev->drv)
545 module_put(cdev->drv->owner);
546 atomic_set(&cdev->private->onoff, 0);
547 return count;
548}
549
550static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400551available_show (struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552{
553 struct ccw_device *cdev = to_ccwdev(dev);
554 struct subchannel *sch;
555
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100556 if (ccw_device_is_orphan(cdev))
557 return sprintf(buf, "no device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 switch (cdev->private->state) {
559 case DEV_STATE_BOXED:
560 return sprintf(buf, "boxed\n");
561 case DEV_STATE_DISCONNECTED:
562 case DEV_STATE_DISCONNECTED_SENSE_ID:
563 case DEV_STATE_NOT_OPER:
564 sch = to_subchannel(dev->parent);
565 if (!sch->lpm)
566 return sprintf(buf, "no path\n");
567 else
568 return sprintf(buf, "no device\n");
569 default:
570 /* All other states considered fine. */
571 return sprintf(buf, "good\n");
572 }
573}
574
575static DEVICE_ATTR(chpids, 0444, chpids_show, NULL);
576static DEVICE_ATTR(pimpampom, 0444, pimpampom_show, NULL);
577static DEVICE_ATTR(devtype, 0444, devtype_show, NULL);
578static DEVICE_ATTR(cutype, 0444, cutype_show, NULL);
Bastian Blankf1fc78a2005-10-30 15:00:12 -0800579static DEVICE_ATTR(modalias, 0444, modalias_show, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580static DEVICE_ATTR(online, 0644, online_show, online_store);
581extern struct device_attribute dev_attr_cmb_enable;
582static DEVICE_ATTR(availability, 0444, available_show, NULL);
583
584static struct attribute * subch_attrs[] = {
585 &dev_attr_chpids.attr,
586 &dev_attr_pimpampom.attr,
587 NULL,
588};
589
590static struct attribute_group subch_attr_group = {
591 .attrs = subch_attrs,
592};
593
Cornelia Huck529192f2006-12-08 15:55:57 +0100594struct attribute_group *subch_attr_groups[] = {
595 &subch_attr_group,
596 NULL,
597};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598
599static struct attribute * ccwdev_attrs[] = {
600 &dev_attr_devtype.attr,
601 &dev_attr_cutype.attr,
Bastian Blankf1fc78a2005-10-30 15:00:12 -0800602 &dev_attr_modalias.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 &dev_attr_online.attr,
604 &dev_attr_cmb_enable.attr,
605 &dev_attr_availability.attr,
606 NULL,
607};
608
609static struct attribute_group ccwdev_attr_group = {
610 .attrs = ccwdev_attrs,
611};
612
Cornelia Huckf7e5d672007-05-10 15:45:43 +0200613static struct attribute_group *ccwdev_attr_groups[] = {
Cornelia Huckef995162007-04-27 16:01:39 +0200614 &ccwdev_attr_group,
615 NULL,
616};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
618/* this is a simple abstraction for device_register that sets the
619 * correct bus type and adds the bus specific files */
Cornelia Huck3c9da7b2006-10-27 12:39:33 +0200620static int ccw_device_register(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621{
622 struct device *dev = &cdev->dev;
623 int ret;
624
625 dev->bus = &ccw_bus_type;
626
627 if ((ret = device_add(dev)))
628 return ret;
629
630 set_bit(1, &cdev->private->registered);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 return ret;
632}
633
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700634struct match_data {
Cornelia Huck78964262006-10-11 15:31:38 +0200635 struct ccw_dev_id dev_id;
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700636 struct ccw_device * sibling;
637};
638
639static int
640match_devno(struct device * dev, void * data)
641{
Cornelia Huck78964262006-10-11 15:31:38 +0200642 struct match_data * d = data;
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700643 struct ccw_device * cdev;
644
645 cdev = to_ccwdev(dev);
646 if ((cdev->private->state == DEV_STATE_DISCONNECTED) &&
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100647 !ccw_device_is_orphan(cdev) &&
Cornelia Huck78964262006-10-11 15:31:38 +0200648 ccw_dev_id_is_equal(&cdev->private->dev_id, &d->dev_id) &&
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100649 (cdev != d->sibling))
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700650 return 1;
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700651 return 0;
652}
653
Cornelia Huck78964262006-10-11 15:31:38 +0200654static struct ccw_device * get_disc_ccwdev_by_dev_id(struct ccw_dev_id *dev_id,
655 struct ccw_device *sibling)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 struct device *dev;
Heiko Carstens292888c2006-08-30 14:33:35 +0200658 struct match_data data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
Cornelia Huck78964262006-10-11 15:31:38 +0200660 data.dev_id = *dev_id;
Heiko Carstens292888c2006-08-30 14:33:35 +0200661 data.sibling = sibling;
Cornelia Hucke5945b42005-10-11 08:28:59 -0700662 dev = bus_find_device(&ccw_bus_type, NULL, &data, match_devno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700664 return dev ? to_ccwdev(dev) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665}
666
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100667static int match_orphan(struct device *dev, void *data)
668{
669 struct ccw_dev_id *dev_id;
670 struct ccw_device *cdev;
671
672 dev_id = data;
673 cdev = to_ccwdev(dev);
674 return ccw_dev_id_is_equal(&cdev->private->dev_id, dev_id);
675}
676
677static struct ccw_device *
678get_orphaned_ccwdev_by_dev_id(struct channel_subsystem *css,
679 struct ccw_dev_id *dev_id)
680{
681 struct device *dev;
682
683 dev = device_find_child(&css->pseudo_subchannel->dev, dev_id,
684 match_orphan);
685
686 return dev ? to_ccwdev(dev) : NULL;
687}
688
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689static void
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100690ccw_device_add_changed(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691{
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100692 struct ccw_device_private *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 struct ccw_device *cdev;
694
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100695 priv = container_of(work, struct ccw_device_private, kick_work);
696 cdev = priv->cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 if (device_add(&cdev->dev)) {
698 put_device(&cdev->dev);
699 return;
700 }
701 set_bit(1, &cdev->private->registered);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702}
703
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100704void ccw_device_do_unreg_rereg(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705{
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100706 struct ccw_device_private *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 struct ccw_device *cdev;
708 struct subchannel *sch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100710 priv = container_of(work, struct ccw_device_private, kick_work);
711 cdev = priv->cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 sch = to_subchannel(cdev->dev.parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
Cornelia Huckef995162007-04-27 16:01:39 +0200714 ccw_device_unregister(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 PREPARE_WORK(&cdev->private->kick_work,
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100716 ccw_device_add_changed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 queue_work(ccw_device_work, &cdev->private->kick_work);
718}
719
720static void
721ccw_device_release(struct device *dev)
722{
723 struct ccw_device *cdev;
724
725 cdev = to_ccwdev(dev);
726 kfree(cdev->private);
727 kfree(cdev);
728}
729
Cornelia Huck7674da72006-12-08 15:54:21 +0100730static struct ccw_device * io_subchannel_allocate_dev(struct subchannel *sch)
731{
732 struct ccw_device *cdev;
733
734 cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
735 if (cdev) {
736 cdev->private = kzalloc(sizeof(struct ccw_device_private),
737 GFP_KERNEL | GFP_DMA);
738 if (cdev->private)
739 return cdev;
740 }
741 kfree(cdev);
742 return ERR_PTR(-ENOMEM);
743}
744
745static int io_subchannel_initialize_dev(struct subchannel *sch,
746 struct ccw_device *cdev)
747{
748 cdev->private->cdev = cdev;
749 atomic_set(&cdev->private->onoff, 0);
750 cdev->dev.parent = &sch->dev;
751 cdev->dev.release = ccw_device_release;
752 INIT_LIST_HEAD(&cdev->private->kick_work.entry);
Cornelia Huckef995162007-04-27 16:01:39 +0200753 cdev->dev.groups = ccwdev_attr_groups;
Cornelia Huck7674da72006-12-08 15:54:21 +0100754 /* Do first half of device_register. */
755 device_initialize(&cdev->dev);
756 if (!get_device(&sch->dev)) {
757 if (cdev->dev.release)
758 cdev->dev.release(&cdev->dev);
759 return -ENODEV;
760 }
761 return 0;
762}
763
764static struct ccw_device * io_subchannel_create_ccwdev(struct subchannel *sch)
765{
766 struct ccw_device *cdev;
767 int ret;
768
769 cdev = io_subchannel_allocate_dev(sch);
770 if (!IS_ERR(cdev)) {
771 ret = io_subchannel_initialize_dev(sch, cdev);
772 if (ret) {
773 kfree(cdev);
774 cdev = ERR_PTR(ret);
775 }
776 }
777 return cdev;
778}
779
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100780static int io_subchannel_recog(struct ccw_device *, struct subchannel *);
781
782static void sch_attach_device(struct subchannel *sch,
783 struct ccw_device *cdev)
784{
Cornelia Huck82b7ac02007-04-27 16:01:36 +0200785 css_update_ssd_info(sch);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100786 spin_lock_irq(sch->lock);
787 sch->dev.driver_data = cdev;
788 cdev->private->schid = sch->schid;
789 cdev->ccwlock = sch->lock;
790 device_trigger_reprobe(sch);
791 spin_unlock_irq(sch->lock);
792}
793
794static void sch_attach_disconnected_device(struct subchannel *sch,
795 struct ccw_device *cdev)
796{
797 struct subchannel *other_sch;
798 int ret;
799
800 other_sch = to_subchannel(get_device(cdev->dev.parent));
801 ret = device_move(&cdev->dev, &sch->dev);
802 if (ret) {
803 CIO_MSG_EVENT(2, "Moving disconnected device 0.%x.%04x failed "
804 "(ret=%d)!\n", cdev->private->dev_id.ssid,
805 cdev->private->dev_id.devno, ret);
806 put_device(&other_sch->dev);
807 return;
808 }
809 other_sch->dev.driver_data = NULL;
810 /* No need to keep a subchannel without ccw device around. */
811 css_sch_device_unregister(other_sch);
812 put_device(&other_sch->dev);
813 sch_attach_device(sch, cdev);
814}
815
816static void sch_attach_orphaned_device(struct subchannel *sch,
817 struct ccw_device *cdev)
818{
819 int ret;
820
821 /* Try to move the ccw device to its new subchannel. */
822 ret = device_move(&cdev->dev, &sch->dev);
823 if (ret) {
824 CIO_MSG_EVENT(0, "Moving device 0.%x.%04x from orphanage "
825 "failed (ret=%d)!\n",
826 cdev->private->dev_id.ssid,
827 cdev->private->dev_id.devno, ret);
828 return;
829 }
830 sch_attach_device(sch, cdev);
831}
832
833static void sch_create_and_recog_new_device(struct subchannel *sch)
834{
835 struct ccw_device *cdev;
836
837 /* Need to allocate a new ccw device. */
838 cdev = io_subchannel_create_ccwdev(sch);
839 if (IS_ERR(cdev)) {
840 /* OK, we did everything we could... */
841 css_sch_device_unregister(sch);
842 return;
843 }
844 spin_lock_irq(sch->lock);
845 sch->dev.driver_data = cdev;
846 spin_unlock_irq(sch->lock);
847 /* Start recognition for the new ccw device. */
848 if (io_subchannel_recog(cdev, sch)) {
849 spin_lock_irq(sch->lock);
850 sch->dev.driver_data = NULL;
851 spin_unlock_irq(sch->lock);
852 if (cdev->dev.release)
853 cdev->dev.release(&cdev->dev);
854 css_sch_device_unregister(sch);
855 }
856}
857
858
859void ccw_device_move_to_orphanage(struct work_struct *work)
860{
861 struct ccw_device_private *priv;
862 struct ccw_device *cdev;
863 struct ccw_device *replacing_cdev;
864 struct subchannel *sch;
865 int ret;
866 struct channel_subsystem *css;
867 struct ccw_dev_id dev_id;
868
869 priv = container_of(work, struct ccw_device_private, kick_work);
870 cdev = priv->cdev;
871 sch = to_subchannel(cdev->dev.parent);
872 css = to_css(sch->dev.parent);
873 dev_id.devno = sch->schib.pmcw.dev;
874 dev_id.ssid = sch->schid.ssid;
875
876 /*
877 * Move the orphaned ccw device to the orphanage so the replacing
878 * ccw device can take its place on the subchannel.
879 */
880 ret = device_move(&cdev->dev, &css->pseudo_subchannel->dev);
881 if (ret) {
882 CIO_MSG_EVENT(0, "Moving device 0.%x.%04x to orphanage failed "
883 "(ret=%d)!\n", cdev->private->dev_id.ssid,
884 cdev->private->dev_id.devno, ret);
885 return;
886 }
887 cdev->ccwlock = css->pseudo_subchannel->lock;
888 /*
889 * Search for the replacing ccw device
890 * - among the disconnected devices
891 * - in the orphanage
892 */
893 replacing_cdev = get_disc_ccwdev_by_dev_id(&dev_id, cdev);
894 if (replacing_cdev) {
895 sch_attach_disconnected_device(sch, replacing_cdev);
896 return;
897 }
898 replacing_cdev = get_orphaned_ccwdev_by_dev_id(css, &dev_id);
899 if (replacing_cdev) {
900 sch_attach_orphaned_device(sch, replacing_cdev);
901 return;
902 }
903 sch_create_and_recog_new_device(sch);
904}
905
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906/*
907 * Register recognized device.
908 */
909static void
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100910io_subchannel_register(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911{
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100912 struct ccw_device_private *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 struct ccw_device *cdev;
914 struct subchannel *sch;
915 int ret;
916 unsigned long flags;
917
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100918 priv = container_of(work, struct ccw_device_private, kick_work);
919 cdev = priv->cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 sch = to_subchannel(cdev->dev.parent);
Cornelia Huck82b7ac02007-04-27 16:01:36 +0200921 css_update_ssd_info(sch);
Cornelia Huck47af5512006-12-04 15:41:07 +0100922 /*
923 * io_subchannel_register() will also be called after device
924 * recognition has been done for a boxed device (which will already
925 * be registered). We need to reprobe since we may now have sense id
926 * information.
927 */
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700928 if (klist_node_attached(&cdev->dev.knode_parent)) {
Cornelia Huck47af5512006-12-04 15:41:07 +0100929 if (!cdev->drv) {
930 ret = device_reprobe(&cdev->dev);
931 if (ret)
932 /* We can't do much here. */
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200933 CIO_MSG_EVENT(2, "device_reprobe() returned"
934 " %d for 0.%x.%04x\n", ret,
935 cdev->private->dev_id.ssid,
936 cdev->private->dev_id.devno);
Cornelia Huck47af5512006-12-04 15:41:07 +0100937 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 goto out;
939 }
Cornelia Huckfa1a8c22007-04-26 00:12:03 -0700940 /*
941 * Now we know this subchannel will stay, we can throw
942 * our delayed uevent.
943 */
944 sch->dev.uevent_suppress = 0;
945 kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 /* make it known to the system */
947 ret = ccw_device_register(cdev);
948 if (ret) {
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200949 CIO_MSG_EVENT(0, "Could not register ccw dev 0.%x.%04x: %d\n",
950 cdev->private->dev_id.ssid,
951 cdev->private->dev_id.devno, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 put_device(&cdev->dev);
Cornelia Huck2ec22982006-12-08 15:54:26 +0100953 spin_lock_irqsave(sch->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 sch->dev.driver_data = NULL;
Cornelia Huck2ec22982006-12-08 15:54:26 +0100955 spin_unlock_irqrestore(sch->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 kfree (cdev->private);
957 kfree (cdev);
958 put_device(&sch->dev);
959 if (atomic_dec_and_test(&ccw_device_init_count))
960 wake_up(&ccw_device_init_wq);
961 return;
962 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 put_device(&cdev->dev);
964out:
965 cdev->private->flags.recog_done = 1;
966 put_device(&sch->dev);
967 wake_up(&cdev->private->wait_q);
968 if (atomic_dec_and_test(&ccw_device_init_count))
969 wake_up(&ccw_device_init_wq);
970}
971
Cornelia Huck3f4cf6e2007-10-12 16:11:26 +0200972static void ccw_device_call_sch_unregister(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973{
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100974 struct ccw_device_private *priv;
975 struct ccw_device *cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 struct subchannel *sch;
977
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100978 priv = container_of(work, struct ccw_device_private, kick_work);
979 cdev = priv->cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 sch = to_subchannel(cdev->dev.parent);
Cornelia Huck6ab48792006-07-12 16:39:50 +0200981 css_sch_device_unregister(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 /* Reset intparm to zeroes. */
983 sch->schib.pmcw.intparm = 0;
984 cio_modify(sch);
985 put_device(&cdev->dev);
986 put_device(&sch->dev);
987}
988
989/*
990 * subchannel recognition done. Called from the state machine.
991 */
992void
993io_subchannel_recog_done(struct ccw_device *cdev)
994{
995 struct subchannel *sch;
996
997 if (css_init_done == 0) {
998 cdev->private->flags.recog_done = 1;
999 return;
1000 }
1001 switch (cdev->private->state) {
1002 case DEV_STATE_NOT_OPER:
1003 cdev->private->flags.recog_done = 1;
1004 /* Remove device found not operational. */
1005 if (!get_device(&cdev->dev))
1006 break;
1007 sch = to_subchannel(cdev->dev.parent);
1008 PREPARE_WORK(&cdev->private->kick_work,
Martin Schwidefskyc1637532006-12-08 15:53:57 +01001009 ccw_device_call_sch_unregister);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 queue_work(slow_path_wq, &cdev->private->kick_work);
1011 if (atomic_dec_and_test(&ccw_device_init_count))
1012 wake_up(&ccw_device_init_wq);
1013 break;
1014 case DEV_STATE_BOXED:
1015 /* Device did not respond in time. */
1016 case DEV_STATE_OFFLINE:
1017 /*
1018 * We can't register the device in interrupt context so
1019 * we schedule a work item.
1020 */
1021 if (!get_device(&cdev->dev))
1022 break;
1023 PREPARE_WORK(&cdev->private->kick_work,
Martin Schwidefskyc1637532006-12-08 15:53:57 +01001024 io_subchannel_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 queue_work(slow_path_wq, &cdev->private->kick_work);
1026 break;
1027 }
1028}
1029
1030static int
1031io_subchannel_recog(struct ccw_device *cdev, struct subchannel *sch)
1032{
1033 int rc;
1034 struct ccw_device_private *priv;
1035
1036 sch->dev.driver_data = cdev;
1037 sch->driver = &io_subchannel_driver;
Cornelia Huck2ec22982006-12-08 15:54:26 +01001038 cdev->ccwlock = sch->lock;
Cornelia Huckfb6958a2006-01-06 00:19:25 -08001039
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 /* Init private data. */
1041 priv = cdev->private;
Cornelia Huck78964262006-10-11 15:31:38 +02001042 priv->dev_id.devno = sch->schib.pmcw.dev;
1043 priv->dev_id.ssid = sch->schid.ssid;
1044 priv->schid = sch->schid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 priv->state = DEV_STATE_NOT_OPER;
1046 INIT_LIST_HEAD(&priv->cmb_list);
1047 init_waitqueue_head(&priv->wait_q);
1048 init_timer(&priv->timer);
1049
1050 /* Set an initial name for the device. */
Cornelia Huckfb6958a2006-01-06 00:19:25 -08001051 snprintf (cdev->dev.bus_id, BUS_ID_SIZE, "0.%x.%04x",
1052 sch->schid.ssid, sch->schib.pmcw.dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053
1054 /* Increase counter of devices currently in recognition. */
1055 atomic_inc(&ccw_device_init_count);
1056
1057 /* Start async. device sensing. */
Cornelia Huck2ec22982006-12-08 15:54:26 +01001058 spin_lock_irq(sch->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 rc = ccw_device_recognition(cdev);
Cornelia Huck2ec22982006-12-08 15:54:26 +01001060 spin_unlock_irq(sch->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 if (rc) {
1062 if (atomic_dec_and_test(&ccw_device_init_count))
1063 wake_up(&ccw_device_init_wq);
1064 }
1065 return rc;
1066}
1067
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +01001068static void ccw_device_move_to_sch(struct work_struct *work)
1069{
1070 struct ccw_device_private *priv;
1071 int rc;
1072 struct subchannel *sch;
1073 struct ccw_device *cdev;
1074 struct subchannel *former_parent;
1075
1076 priv = container_of(work, struct ccw_device_private, kick_work);
1077 sch = priv->sch;
1078 cdev = priv->cdev;
1079 former_parent = ccw_device_is_orphan(cdev) ?
1080 NULL : to_subchannel(get_device(cdev->dev.parent));
1081 mutex_lock(&sch->reg_mutex);
1082 /* Try to move the ccw device to its new subchannel. */
1083 rc = device_move(&cdev->dev, &sch->dev);
1084 mutex_unlock(&sch->reg_mutex);
1085 if (rc) {
1086 CIO_MSG_EVENT(2, "Moving device 0.%x.%04x to subchannel "
1087 "0.%x.%04x failed (ret=%d)!\n",
1088 cdev->private->dev_id.ssid,
1089 cdev->private->dev_id.devno, sch->schid.ssid,
1090 sch->schid.sch_no, rc);
1091 css_sch_device_unregister(sch);
1092 goto out;
1093 }
1094 if (former_parent) {
1095 spin_lock_irq(former_parent->lock);
1096 former_parent->dev.driver_data = NULL;
1097 spin_unlock_irq(former_parent->lock);
1098 css_sch_device_unregister(former_parent);
1099 /* Reset intparm to zeroes. */
1100 former_parent->schib.pmcw.intparm = 0;
1101 cio_modify(former_parent);
1102 }
1103 sch_attach_device(sch, cdev);
1104out:
1105 if (former_parent)
1106 put_device(&former_parent->dev);
1107 put_device(&cdev->dev);
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
1197static int
1198io_subchannel_notify(struct device *dev, int event)
1199{
1200 struct ccw_device *cdev;
1201
1202 cdev = dev->driver_data;
1203 if (!cdev)
1204 return 0;
1205 if (!cdev->drv)
1206 return 0;
1207 if (!cdev->online)
1208 return 0;
1209 return cdev->drv->notify ? cdev->drv->notify(cdev, event) : 0;
1210}
1211
1212static void
1213io_subchannel_verify(struct device *dev)
1214{
1215 struct ccw_device *cdev;
1216
1217 cdev = dev->driver_data;
1218 if (cdev)
1219 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1220}
1221
1222static void
1223io_subchannel_ioterm(struct device *dev)
1224{
1225 struct ccw_device *cdev;
1226
1227 cdev = dev->driver_data;
1228 if (!cdev)
1229 return;
Cornelia Huckd23861f2006-12-04 15:41:04 +01001230 /* Internal I/O will be retried by the interrupt handler. */
1231 if (cdev->private->flags.intretry)
1232 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 cdev->private->state = DEV_STATE_CLEAR_VERIFY;
1234 if (cdev->handler)
1235 cdev->handler(cdev, cdev->private->intparm,
1236 ERR_PTR(-EIO));
1237}
1238
1239static void
Cornelia Huck8bbace72006-01-11 10:56:22 +01001240io_subchannel_shutdown(struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 struct ccw_device *cdev;
1243 int ret;
1244
Cornelia Huck8bbace72006-01-11 10:56:22 +01001245 cdev = sch->dev.driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246
Cornelia Hucka8237fc2006-01-06 00:19:21 -08001247 if (cio_is_console(sch->schid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 return;
1249 if (!sch->schib.pmcw.ena)
1250 /* Nothing to do. */
1251 return;
1252 ret = cio_disable_subchannel(sch);
1253 if (ret != -EBUSY)
1254 /* Subchannel is disabled, we're done. */
1255 return;
1256 cdev->private->state = DEV_STATE_QUIESCE;
1257 if (cdev->handler)
1258 cdev->handler(cdev, cdev->private->intparm,
1259 ERR_PTR(-EIO));
1260 ret = ccw_device_cancel_halt_clear(cdev);
1261 if (ret == -EBUSY) {
1262 ccw_device_set_timeout(cdev, HZ/10);
1263 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
1264 }
1265 cio_disable_subchannel(sch);
1266}
1267
1268#ifdef CONFIG_CCW_CONSOLE
1269static struct ccw_device console_cdev;
1270static struct ccw_device_private console_private;
1271static int console_cdev_in_use;
1272
Cornelia Huck2ec22982006-12-08 15:54:26 +01001273static DEFINE_SPINLOCK(ccw_console_lock);
1274
1275spinlock_t * cio_get_console_lock(void)
1276{
1277 return &ccw_console_lock;
1278}
1279
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280static int
1281ccw_device_console_enable (struct ccw_device *cdev, struct subchannel *sch)
1282{
1283 int rc;
1284
1285 /* Initialize the ccw_device structure. */
Heiko Carstens292888c2006-08-30 14:33:35 +02001286 cdev->dev.parent= &sch->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 rc = io_subchannel_recog(cdev, sch);
1288 if (rc)
1289 return rc;
1290
1291 /* Now wait for the async. recognition to come to an end. */
1292 spin_lock_irq(cdev->ccwlock);
1293 while (!dev_fsm_final_state(cdev))
1294 wait_cons_dev();
1295 rc = -EIO;
1296 if (cdev->private->state != DEV_STATE_OFFLINE)
1297 goto out_unlock;
1298 ccw_device_online(cdev);
1299 while (!dev_fsm_final_state(cdev))
1300 wait_cons_dev();
1301 if (cdev->private->state != DEV_STATE_ONLINE)
1302 goto out_unlock;
1303 rc = 0;
1304out_unlock:
1305 spin_unlock_irq(cdev->ccwlock);
1306 return 0;
1307}
1308
1309struct ccw_device *
1310ccw_device_probe_console(void)
1311{
1312 struct subchannel *sch;
1313 int ret;
1314
1315 if (xchg(&console_cdev_in_use, 1) != 0)
Peter Oberparleiter600b5d12006-02-01 03:06:35 -08001316 return ERR_PTR(-EBUSY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 sch = cio_probe_console();
1318 if (IS_ERR(sch)) {
1319 console_cdev_in_use = 0;
1320 return (void *) sch;
1321 }
1322 memset(&console_cdev, 0, sizeof(struct ccw_device));
1323 memset(&console_private, 0, sizeof(struct ccw_device_private));
1324 console_cdev.private = &console_private;
Martin Schwidefskyc1637532006-12-08 15:53:57 +01001325 console_private.cdev = &console_cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 ret = ccw_device_console_enable(&console_cdev, sch);
1327 if (ret) {
1328 cio_release_console();
1329 console_cdev_in_use = 0;
1330 return ERR_PTR(ret);
1331 }
1332 console_cdev.online = 1;
1333 return &console_cdev;
1334}
1335#endif
1336
1337/*
1338 * get ccw_device matching the busid, but only if owned by cdrv
1339 */
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001340static int
1341__ccwdev_check_busid(struct device *dev, void *id)
1342{
1343 char *bus_id;
1344
Cornelia Huck12975ae2006-10-11 15:31:47 +02001345 bus_id = id;
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001346
1347 return (strncmp(bus_id, dev->bus_id, BUS_ID_SIZE) == 0);
1348}
1349
1350
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +02001351/**
1352 * get_ccwdev_by_busid() - obtain device from a bus id
1353 * @cdrv: driver the device is owned by
1354 * @bus_id: bus id of the device to be searched
1355 *
1356 * This function searches all devices owned by @cdrv for a device with a bus
1357 * id matching @bus_id.
1358 * Returns:
1359 * If a match is found, its reference count of the found device is increased
1360 * and it is returned; else %NULL is returned.
1361 */
1362struct ccw_device *get_ccwdev_by_busid(struct ccw_driver *cdrv,
1363 const char *bus_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364{
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001365 struct device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 struct device_driver *drv;
1367
1368 drv = get_driver(&cdrv->driver);
1369 if (!drv)
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001370 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001372 dev = driver_find_device(drv, NULL, (void *)bus_id,
1373 __ccwdev_check_busid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 put_driver(drv);
1375
Heiko Carstensd2c993d2006-07-12 16:41:55 +02001376 return dev ? to_ccwdev(dev) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377}
1378
1379/************************** device driver handling ************************/
1380
1381/* This is the implementation of the ccw_driver class. The probe, remove
1382 * and release methods are initially very similar to the device_driver
1383 * implementations, with the difference that they have ccw_device
1384 * arguments.
1385 *
1386 * A ccw driver also contains the information that is needed for
1387 * device matching.
1388 */
1389static int
1390ccw_device_probe (struct device *dev)
1391{
1392 struct ccw_device *cdev = to_ccwdev(dev);
1393 struct ccw_driver *cdrv = to_ccwdrv(dev->driver);
1394 int ret;
1395
1396 cdev->drv = cdrv; /* to let the driver call _set_online */
1397
1398 ret = cdrv->probe ? cdrv->probe(cdev) : -ENODEV;
1399
1400 if (ret) {
Heiko Carstensd2c993d2006-07-12 16:41:55 +02001401 cdev->drv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 return ret;
1403 }
1404
1405 return 0;
1406}
1407
1408static int
1409ccw_device_remove (struct device *dev)
1410{
1411 struct ccw_device *cdev = to_ccwdev(dev);
1412 struct ccw_driver *cdrv = cdev->drv;
1413 int ret;
1414
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 if (cdrv->remove)
1416 cdrv->remove(cdev);
1417 if (cdev->online) {
1418 cdev->online = 0;
1419 spin_lock_irq(cdev->ccwlock);
1420 ret = ccw_device_offline(cdev);
1421 spin_unlock_irq(cdev->ccwlock);
1422 if (ret == 0)
1423 wait_event(cdev->private->wait_q,
1424 dev_fsm_final_state(cdev));
1425 else
1426 //FIXME: we can't fail!
Cornelia Hucke556bbb2007-07-27 12:29:19 +02001427 CIO_MSG_EVENT(2, "ccw_device_offline returned %d, "
1428 "device 0.%x.%04x\n",
1429 ret, cdev->private->dev_id.ssid,
1430 cdev->private->dev_id.devno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 }
1432 ccw_device_set_timeout(cdev, 0);
Heiko Carstensd2c993d2006-07-12 16:41:55 +02001433 cdev->drv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 return 0;
1435}
1436
Cornelia Huck958974fb2007-10-12 16:11:21 +02001437static void ccw_device_shutdown(struct device *dev)
1438{
1439 struct ccw_device *cdev;
1440
1441 cdev = to_ccwdev(dev);
1442 if (cdev->drv && cdev->drv->shutdown)
1443 cdev->drv->shutdown(cdev);
Cornelia Huck1842f2b2007-10-12 16:11:22 +02001444 disable_cmf(cdev);
Cornelia Huck958974fb2007-10-12 16:11:21 +02001445}
1446
Cornelia Huck8bbace72006-01-11 10:56:22 +01001447struct bus_type ccw_bus_type = {
1448 .name = "ccw",
1449 .match = ccw_bus_match,
1450 .uevent = ccw_uevent,
1451 .probe = ccw_device_probe,
1452 .remove = ccw_device_remove,
Cornelia Huck958974fb2007-10-12 16:11:21 +02001453 .shutdown = ccw_device_shutdown,
Cornelia Huck8bbace72006-01-11 10:56:22 +01001454};
1455
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +02001456/**
1457 * ccw_driver_register() - register a ccw driver
1458 * @cdriver: driver to be registered
1459 *
1460 * This function is mainly a wrapper around driver_register().
1461 * Returns:
1462 * %0 on success and a negative error value on failure.
1463 */
1464int ccw_driver_register(struct ccw_driver *cdriver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465{
1466 struct device_driver *drv = &cdriver->driver;
1467
1468 drv->bus = &ccw_bus_type;
1469 drv->name = cdriver->name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470
1471 return driver_register(drv);
1472}
1473
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +02001474/**
1475 * ccw_driver_unregister() - deregister a ccw driver
1476 * @cdriver: driver to be deregistered
1477 *
1478 * This function is mainly a wrapper around driver_unregister().
1479 */
1480void ccw_driver_unregister(struct ccw_driver *cdriver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481{
1482 driver_unregister(&cdriver->driver);
1483}
1484
Cornelia Hucka8237fc2006-01-06 00:19:21 -08001485/* Helper func for qdio. */
1486struct subchannel_id
1487ccw_device_get_subchannel_id(struct ccw_device *cdev)
1488{
1489 struct subchannel *sch;
1490
1491 sch = to_subchannel(cdev->dev.parent);
1492 return sch->schid;
1493}
1494
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495MODULE_LICENSE("GPL");
1496EXPORT_SYMBOL(ccw_device_set_online);
1497EXPORT_SYMBOL(ccw_device_set_offline);
1498EXPORT_SYMBOL(ccw_driver_register);
1499EXPORT_SYMBOL(ccw_driver_unregister);
1500EXPORT_SYMBOL(get_ccwdev_by_busid);
1501EXPORT_SYMBOL(ccw_bus_type);
1502EXPORT_SYMBOL(ccw_device_work);
1503EXPORT_SYMBOL(ccw_device_notify_work);
Cornelia Hucka8237fc2006-01-06 00:19:21 -08001504EXPORT_SYMBOL_GPL(ccw_device_get_subchannel_id);