blob: 803579053c2fe012dbf4854b13c12baa156a8f86 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * drivers/s390/cio/device.c
3 * bus driver for ccw devices
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
5 * Copyright (C) 2002 IBM Deutschland Entwicklung GmbH,
6 * IBM Corporation
7 * Author(s): Arnd Bergmann (arndb@de.ibm.com)
Cornelia Huck4ce3b302006-01-14 13:21:04 -08008 * Cornelia Huck (cornelia.huck@de.ibm.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 * Martin Schwidefsky (schwidefsky@de.ibm.com)
10 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/module.h>
12#include <linux/init.h>
13#include <linux/spinlock.h>
14#include <linux/errno.h>
15#include <linux/err.h>
16#include <linux/slab.h>
17#include <linux/list.h>
18#include <linux/device.h>
19#include <linux/workqueue.h>
20
21#include <asm/ccwdev.h>
22#include <asm/cio.h>
Tim Schmielau4e57b682005-10-30 15:03:48 -080023#include <asm/param.h> /* HZ */
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
25#include "cio.h"
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +010026#include "cio_debug.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include "css.h"
28#include "device.h"
29#include "ioasm.h"
30
31/******************* bus type handling ***********************/
32
33/* The Linux driver model distinguishes between a bus type and
34 * the bus itself. Of course we only have one channel
35 * subsystem driver and one channel system per machine, but
36 * we still use the abstraction. T.R. says it's a good idea. */
37static int
38ccw_bus_match (struct device * dev, struct device_driver * drv)
39{
40 struct ccw_device *cdev = to_ccwdev(dev);
41 struct ccw_driver *cdrv = to_ccwdrv(drv);
42 const struct ccw_device_id *ids = cdrv->ids, *found;
43
44 if (!ids)
45 return 0;
46
47 found = ccw_device_id_match(ids, &cdev->id);
48 if (!found)
49 return 0;
50
51 cdev->id.driver_info = found->driver_info;
52
53 return 1;
54}
55
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +020056/* Store modalias string delimited by prefix/suffix string into buffer with
57 * specified size. Return length of resulting string (excluding trailing '\0')
58 * even if string doesn't fit buffer (snprintf semantics). */
59static int snprint_alias(char *buf, size_t size, const char *prefix,
60 struct ccw_device_id *id, const char *suffix)
61{
62 int len;
63
64 len = snprintf(buf, size, "%sccw:t%04Xm%02X", prefix, id->cu_type,
65 id->cu_model);
66 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;
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +020088 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +020090 /* CU_TYPE= */
91 len = snprintf(buffer, buffer_size, "CU_TYPE=%04X", id->cu_type) + 1;
92 if (len > buffer_size || i >= num_envp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 envp[i++] = buffer;
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +020095 buffer += len;
96 buffer_size -= len;
97
98 /* CU_MODEL= */
99 len = snprintf(buffer, buffer_size, "CU_MODEL=%02X", id->cu_model) + 1;
100 if (len > buffer_size || i >= num_envp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 return -ENOMEM;
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +0200102 envp[i++] = buffer;
103 buffer += len;
104 buffer_size -= len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
106 /* The next two can be zero, that's ok for us */
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +0200107 /* DEV_TYPE= */
108 len = snprintf(buffer, buffer_size, "DEV_TYPE=%04X", id->dev_type) + 1;
109 if (len > buffer_size || i >= num_envp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 return -ENOMEM;
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +0200111 envp[i++] = buffer;
112 buffer += len;
113 buffer_size -= len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +0200115 /* DEV_MODEL= */
116 len = snprintf(buffer, buffer_size, "DEV_MODEL=%02X",
117 (unsigned char) id->dev_model) + 1;
118 if (len > buffer_size || i >= num_envp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 return -ENOMEM;
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +0200120 envp[i++] = buffer;
121 buffer += len;
122 buffer_size -= len;
123
124 /* MODALIAS= */
125 len = snprint_alias(buffer, buffer_size, "MODALIAS=", id, "") + 1;
126 if (len > buffer_size || i >= num_envp)
127 return -ENOMEM;
128 envp[i++] = buffer;
129 buffer += len;
130 buffer_size -= len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
Heiko Carstensd2c993d2006-07-12 16:41:55 +0200132 envp[i] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
134 return 0;
135}
136
Cornelia Huck8bbace72006-01-11 10:56:22 +0100137struct bus_type ccw_bus_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
Cornelia Huck8bbace72006-01-11 10:56:22 +0100139static int io_subchannel_probe (struct subchannel *);
140static int io_subchannel_remove (struct subchannel *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141void io_subchannel_irq (struct device *);
142static int io_subchannel_notify(struct device *, int);
143static void io_subchannel_verify(struct device *);
144static void io_subchannel_ioterm(struct device *);
Cornelia Huck8bbace72006-01-11 10:56:22 +0100145static void io_subchannel_shutdown(struct subchannel *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
147struct css_driver io_subchannel_driver = {
148 .subchannel_type = SUBCHANNEL_TYPE_IO,
149 .drv = {
150 .name = "io_subchannel",
151 .bus = &css_bus_type,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 },
153 .irq = io_subchannel_irq,
154 .notify = io_subchannel_notify,
155 .verify = io_subchannel_verify,
156 .termination = io_subchannel_ioterm,
Cornelia Huck8bbace72006-01-11 10:56:22 +0100157 .probe = io_subchannel_probe,
158 .remove = io_subchannel_remove,
159 .shutdown = io_subchannel_shutdown,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160};
161
162struct workqueue_struct *ccw_device_work;
163struct workqueue_struct *ccw_device_notify_work;
Peter Oberparleiter40154b82006-06-29 14:57:03 +0200164wait_queue_head_t ccw_device_init_wq;
165atomic_t ccw_device_init_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
167static int __init
168init_ccw_bus_type (void)
169{
170 int ret;
171
172 init_waitqueue_head(&ccw_device_init_wq);
173 atomic_set(&ccw_device_init_count, 0);
174
175 ccw_device_work = create_singlethread_workqueue("cio");
176 if (!ccw_device_work)
177 return -ENOMEM; /* FIXME: better errno ? */
178 ccw_device_notify_work = create_singlethread_workqueue("cio_notify");
179 if (!ccw_device_notify_work) {
180 ret = -ENOMEM; /* FIXME: better errno ? */
181 goto out_err;
182 }
183 slow_path_wq = create_singlethread_workqueue("kslowcrw");
184 if (!slow_path_wq) {
185 ret = -ENOMEM; /* FIXME: better errno ? */
186 goto out_err;
187 }
188 if ((ret = bus_register (&ccw_bus_type)))
189 goto out_err;
190
191 if ((ret = driver_register(&io_subchannel_driver.drv)))
192 goto out_err;
193
194 wait_event(ccw_device_init_wq,
195 atomic_read(&ccw_device_init_count) == 0);
196 flush_workqueue(ccw_device_work);
197 return 0;
198out_err:
199 if (ccw_device_work)
200 destroy_workqueue(ccw_device_work);
201 if (ccw_device_notify_work)
202 destroy_workqueue(ccw_device_notify_work);
203 if (slow_path_wq)
204 destroy_workqueue(slow_path_wq);
205 return ret;
206}
207
208static void __exit
209cleanup_ccw_bus_type (void)
210{
211 driver_unregister(&io_subchannel_driver.drv);
212 bus_unregister(&ccw_bus_type);
213 destroy_workqueue(ccw_device_notify_work);
214 destroy_workqueue(ccw_device_work);
215}
216
217subsys_initcall(init_ccw_bus_type);
218module_exit(cleanup_ccw_bus_type);
219
220/************************ device handling **************************/
221
222/*
223 * A ccw_device has some interfaces in sysfs in addition to the
224 * standard ones.
225 * The following entries are designed to export the information which
226 * resided in 2.4 in /proc/subchannels. Subchannel and device number
227 * are obvious, so they don't have an entry :)
228 * TODO: Split chpids and pimpampom up? Where is "in use" in the tree?
229 */
230static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400231chpids_show (struct device * dev, struct device_attribute *attr, char * buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232{
233 struct subchannel *sch = to_subchannel(dev);
234 struct ssd_info *ssd = &sch->ssd_info;
235 ssize_t ret = 0;
236 int chp;
237
Cornelia Huck529192f2006-12-08 15:55:57 +0100238 if (ssd)
239 for (chp = 0; chp < 8; chp++)
240 ret += sprintf (buf+ret, "%02x ", ssd->chpid[chp]);
241 else
242 ret += sprintf (buf, "n/a");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 ret += sprintf (buf+ret, "\n");
244 return min((ssize_t)PAGE_SIZE, ret);
245}
246
247static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400248pimpampom_show (struct device * dev, struct device_attribute *attr, char * buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249{
250 struct subchannel *sch = to_subchannel(dev);
251 struct pmcw *pmcw = &sch->schib.pmcw;
252
253 return sprintf (buf, "%02x %02x %02x\n",
254 pmcw->pim, pmcw->pam, pmcw->pom);
255}
256
257static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400258devtype_show (struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259{
260 struct ccw_device *cdev = to_ccwdev(dev);
261 struct ccw_device_id *id = &(cdev->id);
262
263 if (id->dev_type != 0)
264 return sprintf(buf, "%04x/%02x\n",
265 id->dev_type, id->dev_model);
266 else
267 return sprintf(buf, "n/a\n");
268}
269
270static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400271cutype_show (struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272{
273 struct ccw_device *cdev = to_ccwdev(dev);
274 struct ccw_device_id *id = &(cdev->id);
275
276 return sprintf(buf, "%04x/%02x\n",
277 id->cu_type, id->cu_model);
278}
279
280static ssize_t
Bastian Blankf1fc78a2005-10-30 15:00:12 -0800281modalias_show (struct device *dev, struct device_attribute *attr, char *buf)
282{
283 struct ccw_device *cdev = to_ccwdev(dev);
284 struct ccw_device_id *id = &(cdev->id);
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +0200285 int len;
Bastian Blankf1fc78a2005-10-30 15:00:12 -0800286
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +0200287 len = snprint_alias(buf, PAGE_SIZE, "", id, "\n") + 1;
288
289 return len > PAGE_SIZE ? PAGE_SIZE : len;
Bastian Blankf1fc78a2005-10-30 15:00:12 -0800290}
291
292static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400293online_show (struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294{
295 struct ccw_device *cdev = to_ccwdev(dev);
296
297 return sprintf(buf, cdev->online ? "1\n" : "0\n");
298}
299
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100300int ccw_device_is_orphan(struct ccw_device *cdev)
301{
302 return sch_is_pseudo_sch(to_subchannel(cdev->dev.parent));
303}
304
Cornelia Huck7674da72006-12-08 15:54:21 +0100305static void ccw_device_unregister(struct work_struct *work)
306{
307 struct ccw_device_private *priv;
308 struct ccw_device *cdev;
309
310 priv = container_of(work, struct ccw_device_private, kick_work);
311 cdev = priv->cdev;
312 if (test_and_clear_bit(1, &cdev->private->registered))
313 device_unregister(&cdev->dev);
314 put_device(&cdev->dev);
315}
316
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317static void
318ccw_device_remove_disconnected(struct ccw_device *cdev)
319{
320 struct subchannel *sch;
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100321 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 /*
323 * Forced offline in disconnected state means
324 * 'throw away device'.
325 */
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100326 if (ccw_device_is_orphan(cdev)) {
327 /* Deregister ccw device. */
328 spin_lock_irqsave(cdev->ccwlock, flags);
329 cdev->private->state = DEV_STATE_NOT_OPER;
330 spin_unlock_irqrestore(cdev->ccwlock, flags);
331 if (get_device(&cdev->dev)) {
332 PREPARE_WORK(&cdev->private->kick_work,
333 ccw_device_unregister);
334 queue_work(ccw_device_work, &cdev->private->kick_work);
335 }
336 return ;
337 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 sch = to_subchannel(cdev->dev.parent);
Cornelia Huck6ab48792006-07-12 16:39:50 +0200339 css_sch_device_unregister(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 /* Reset intparm to zeroes. */
341 sch->schib.pmcw.intparm = 0;
342 cio_modify(sch);
343 put_device(&sch->dev);
344}
345
346int
347ccw_device_set_offline(struct ccw_device *cdev)
348{
349 int ret;
350
351 if (!cdev)
352 return -ENODEV;
353 if (!cdev->online || !cdev->drv)
354 return -EINVAL;
355
356 if (cdev->drv->set_offline) {
357 ret = cdev->drv->set_offline(cdev);
358 if (ret != 0)
359 return ret;
360 }
361 cdev->online = 0;
362 spin_lock_irq(cdev->ccwlock);
363 ret = ccw_device_offline(cdev);
364 if (ret == -ENODEV) {
365 if (cdev->private->state != DEV_STATE_NOT_OPER) {
366 cdev->private->state = DEV_STATE_OFFLINE;
367 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
368 }
369 spin_unlock_irq(cdev->ccwlock);
370 return ret;
371 }
372 spin_unlock_irq(cdev->ccwlock);
373 if (ret == 0)
374 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
375 else {
376 pr_debug("ccw_device_offline returned %d, device %s\n",
377 ret, cdev->dev.bus_id);
378 cdev->online = 1;
379 }
380 return ret;
381}
382
383int
384ccw_device_set_online(struct ccw_device *cdev)
385{
386 int ret;
387
388 if (!cdev)
389 return -ENODEV;
390 if (cdev->online || !cdev->drv)
391 return -EINVAL;
392
393 spin_lock_irq(cdev->ccwlock);
394 ret = ccw_device_online(cdev);
395 spin_unlock_irq(cdev->ccwlock);
396 if (ret == 0)
397 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
398 else {
399 pr_debug("ccw_device_online returned %d, device %s\n",
400 ret, cdev->dev.bus_id);
401 return ret;
402 }
403 if (cdev->private->state != DEV_STATE_ONLINE)
404 return -ENODEV;
405 if (!cdev->drv->set_online || cdev->drv->set_online(cdev) == 0) {
406 cdev->online = 1;
407 return 0;
408 }
409 spin_lock_irq(cdev->ccwlock);
410 ret = ccw_device_offline(cdev);
411 spin_unlock_irq(cdev->ccwlock);
412 if (ret == 0)
413 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
414 else
415 pr_debug("ccw_device_offline returned %d, device %s\n",
416 ret, cdev->dev.bus_id);
Cornelia Huck6cadb782006-02-17 13:52:49 -0800417 return (ret == 0) ? -ENODEV : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418}
419
420static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400421online_store (struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422{
423 struct ccw_device *cdev = to_ccwdev(dev);
424 int i, force, ret;
425 char *tmp;
426
Martin Schwidefsky973bd992006-01-06 00:19:07 -0800427 if (atomic_cmpxchg(&cdev->private->onoff, 0, 1) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 return -EAGAIN;
429
430 if (cdev->drv && !try_module_get(cdev->drv->owner)) {
431 atomic_set(&cdev->private->onoff, 0);
432 return -EINVAL;
433 }
434 if (!strncmp(buf, "force\n", count)) {
435 force = 1;
436 i = 1;
437 } else {
438 force = 0;
439 i = simple_strtoul(buf, &tmp, 16);
440 }
441 if (i == 1) {
442 /* Do device recognition, if needed. */
443 if (cdev->id.cu_type == 0) {
444 ret = ccw_device_recognition(cdev);
445 if (ret) {
446 printk(KERN_WARNING"Couldn't start recognition "
447 "for device %s (ret=%d)\n",
448 cdev->dev.bus_id, ret);
449 goto out;
450 }
451 wait_event(cdev->private->wait_q,
452 cdev->private->flags.recog_done);
453 }
454 if (cdev->drv && cdev->drv->set_online)
455 ccw_device_set_online(cdev);
456 } else if (i == 0) {
457 if (cdev->private->state == DEV_STATE_DISCONNECTED)
458 ccw_device_remove_disconnected(cdev);
459 else if (cdev->drv && cdev->drv->set_offline)
460 ccw_device_set_offline(cdev);
461 }
462 if (force && cdev->private->state == DEV_STATE_BOXED) {
463 ret = ccw_device_stlck(cdev);
464 if (ret) {
465 printk(KERN_WARNING"ccw_device_stlck for device %s "
466 "returned %d!\n", cdev->dev.bus_id, ret);
467 goto out;
468 }
469 /* Do device recognition, if needed. */
470 if (cdev->id.cu_type == 0) {
471 cdev->private->state = DEV_STATE_NOT_OPER;
472 ret = ccw_device_recognition(cdev);
473 if (ret) {
474 printk(KERN_WARNING"Couldn't start recognition "
475 "for device %s (ret=%d)\n",
476 cdev->dev.bus_id, ret);
477 goto out;
478 }
479 wait_event(cdev->private->wait_q,
480 cdev->private->flags.recog_done);
481 }
482 if (cdev->drv && cdev->drv->set_online)
483 ccw_device_set_online(cdev);
484 }
485 out:
486 if (cdev->drv)
487 module_put(cdev->drv->owner);
488 atomic_set(&cdev->private->onoff, 0);
489 return count;
490}
491
492static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400493available_show (struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494{
495 struct ccw_device *cdev = to_ccwdev(dev);
496 struct subchannel *sch;
497
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100498 if (ccw_device_is_orphan(cdev))
499 return sprintf(buf, "no device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 switch (cdev->private->state) {
501 case DEV_STATE_BOXED:
502 return sprintf(buf, "boxed\n");
503 case DEV_STATE_DISCONNECTED:
504 case DEV_STATE_DISCONNECTED_SENSE_ID:
505 case DEV_STATE_NOT_OPER:
506 sch = to_subchannel(dev->parent);
507 if (!sch->lpm)
508 return sprintf(buf, "no path\n");
509 else
510 return sprintf(buf, "no device\n");
511 default:
512 /* All other states considered fine. */
513 return sprintf(buf, "good\n");
514 }
515}
516
517static DEVICE_ATTR(chpids, 0444, chpids_show, NULL);
518static DEVICE_ATTR(pimpampom, 0444, pimpampom_show, NULL);
519static DEVICE_ATTR(devtype, 0444, devtype_show, NULL);
520static DEVICE_ATTR(cutype, 0444, cutype_show, NULL);
Bastian Blankf1fc78a2005-10-30 15:00:12 -0800521static DEVICE_ATTR(modalias, 0444, modalias_show, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522static DEVICE_ATTR(online, 0644, online_show, online_store);
523extern struct device_attribute dev_attr_cmb_enable;
524static DEVICE_ATTR(availability, 0444, available_show, NULL);
525
526static struct attribute * subch_attrs[] = {
527 &dev_attr_chpids.attr,
528 &dev_attr_pimpampom.attr,
529 NULL,
530};
531
532static struct attribute_group subch_attr_group = {
533 .attrs = subch_attrs,
534};
535
Cornelia Huck529192f2006-12-08 15:55:57 +0100536struct attribute_group *subch_attr_groups[] = {
537 &subch_attr_group,
538 NULL,
539};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
541static struct attribute * ccwdev_attrs[] = {
542 &dev_attr_devtype.attr,
543 &dev_attr_cutype.attr,
Bastian Blankf1fc78a2005-10-30 15:00:12 -0800544 &dev_attr_modalias.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 &dev_attr_online.attr,
546 &dev_attr_cmb_enable.attr,
547 &dev_attr_availability.attr,
548 NULL,
549};
550
551static struct attribute_group ccwdev_attr_group = {
552 .attrs = ccwdev_attrs,
553};
554
555static inline int
556device_add_files (struct device *dev)
557{
558 return sysfs_create_group(&dev->kobj, &ccwdev_attr_group);
559}
560
561static inline void
562device_remove_files(struct device *dev)
563{
564 sysfs_remove_group(&dev->kobj, &ccwdev_attr_group);
565}
566
567/* this is a simple abstraction for device_register that sets the
568 * correct bus type and adds the bus specific files */
Cornelia Huck3c9da7b2006-10-27 12:39:33 +0200569static int ccw_device_register(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570{
571 struct device *dev = &cdev->dev;
572 int ret;
573
574 dev->bus = &ccw_bus_type;
575
576 if ((ret = device_add(dev)))
577 return ret;
578
579 set_bit(1, &cdev->private->registered);
580 if ((ret = device_add_files(dev))) {
581 if (test_and_clear_bit(1, &cdev->private->registered))
582 device_del(dev);
583 }
584 return ret;
585}
586
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700587struct match_data {
Cornelia Huck78964262006-10-11 15:31:38 +0200588 struct ccw_dev_id dev_id;
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700589 struct ccw_device * sibling;
590};
591
592static int
593match_devno(struct device * dev, void * data)
594{
Cornelia Huck78964262006-10-11 15:31:38 +0200595 struct match_data * d = data;
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700596 struct ccw_device * cdev;
597
598 cdev = to_ccwdev(dev);
599 if ((cdev->private->state == DEV_STATE_DISCONNECTED) &&
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100600 !ccw_device_is_orphan(cdev) &&
Cornelia Huck78964262006-10-11 15:31:38 +0200601 ccw_dev_id_is_equal(&cdev->private->dev_id, &d->dev_id) &&
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100602 (cdev != d->sibling))
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700603 return 1;
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700604 return 0;
605}
606
Cornelia Huck78964262006-10-11 15:31:38 +0200607static struct ccw_device * get_disc_ccwdev_by_dev_id(struct ccw_dev_id *dev_id,
608 struct ccw_device *sibling)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 struct device *dev;
Heiko Carstens292888c2006-08-30 14:33:35 +0200611 struct match_data data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
Cornelia Huck78964262006-10-11 15:31:38 +0200613 data.dev_id = *dev_id;
Heiko Carstens292888c2006-08-30 14:33:35 +0200614 data.sibling = sibling;
Cornelia Hucke5945b42005-10-11 08:28:59 -0700615 dev = bus_find_device(&ccw_bus_type, NULL, &data, match_devno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700617 return dev ? to_ccwdev(dev) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618}
619
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100620static int match_orphan(struct device *dev, void *data)
621{
622 struct ccw_dev_id *dev_id;
623 struct ccw_device *cdev;
624
625 dev_id = data;
626 cdev = to_ccwdev(dev);
627 return ccw_dev_id_is_equal(&cdev->private->dev_id, dev_id);
628}
629
630static struct ccw_device *
631get_orphaned_ccwdev_by_dev_id(struct channel_subsystem *css,
632 struct ccw_dev_id *dev_id)
633{
634 struct device *dev;
635
636 dev = device_find_child(&css->pseudo_subchannel->dev, dev_id,
637 match_orphan);
638
639 return dev ? to_ccwdev(dev) : NULL;
640}
641
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642static void
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100643ccw_device_add_changed(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644{
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100645 struct ccw_device_private *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 struct ccw_device *cdev;
647
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100648 priv = container_of(work, struct ccw_device_private, kick_work);
649 cdev = priv->cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 if (device_add(&cdev->dev)) {
651 put_device(&cdev->dev);
652 return;
653 }
654 set_bit(1, &cdev->private->registered);
655 if (device_add_files(&cdev->dev)) {
656 if (test_and_clear_bit(1, &cdev->private->registered))
657 device_unregister(&cdev->dev);
658 }
659}
660
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100661void ccw_device_do_unreg_rereg(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662{
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100663 struct ccw_device_private *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 struct ccw_device *cdev;
665 struct subchannel *sch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100667 priv = container_of(work, struct ccw_device_private, kick_work);
668 cdev = priv->cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 sch = to_subchannel(cdev->dev.parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 device_remove_files(&cdev->dev);
672 if (test_and_clear_bit(1, &cdev->private->registered))
673 device_del(&cdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 PREPARE_WORK(&cdev->private->kick_work,
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100675 ccw_device_add_changed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 queue_work(ccw_device_work, &cdev->private->kick_work);
677}
678
679static void
680ccw_device_release(struct device *dev)
681{
682 struct ccw_device *cdev;
683
684 cdev = to_ccwdev(dev);
685 kfree(cdev->private);
686 kfree(cdev);
687}
688
Cornelia Huck7674da72006-12-08 15:54:21 +0100689static struct ccw_device * io_subchannel_allocate_dev(struct subchannel *sch)
690{
691 struct ccw_device *cdev;
692
693 cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
694 if (cdev) {
695 cdev->private = kzalloc(sizeof(struct ccw_device_private),
696 GFP_KERNEL | GFP_DMA);
697 if (cdev->private)
698 return cdev;
699 }
700 kfree(cdev);
701 return ERR_PTR(-ENOMEM);
702}
703
704static int io_subchannel_initialize_dev(struct subchannel *sch,
705 struct ccw_device *cdev)
706{
707 cdev->private->cdev = cdev;
708 atomic_set(&cdev->private->onoff, 0);
709 cdev->dev.parent = &sch->dev;
710 cdev->dev.release = ccw_device_release;
711 INIT_LIST_HEAD(&cdev->private->kick_work.entry);
712 /* Do first half of device_register. */
713 device_initialize(&cdev->dev);
714 if (!get_device(&sch->dev)) {
715 if (cdev->dev.release)
716 cdev->dev.release(&cdev->dev);
717 return -ENODEV;
718 }
719 return 0;
720}
721
722static struct ccw_device * io_subchannel_create_ccwdev(struct subchannel *sch)
723{
724 struct ccw_device *cdev;
725 int ret;
726
727 cdev = io_subchannel_allocate_dev(sch);
728 if (!IS_ERR(cdev)) {
729 ret = io_subchannel_initialize_dev(sch, cdev);
730 if (ret) {
731 kfree(cdev);
732 cdev = ERR_PTR(ret);
733 }
734 }
735 return cdev;
736}
737
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100738static int io_subchannel_recog(struct ccw_device *, struct subchannel *);
739
740static void sch_attach_device(struct subchannel *sch,
741 struct ccw_device *cdev)
742{
743 spin_lock_irq(sch->lock);
744 sch->dev.driver_data = cdev;
745 cdev->private->schid = sch->schid;
746 cdev->ccwlock = sch->lock;
747 device_trigger_reprobe(sch);
748 spin_unlock_irq(sch->lock);
749}
750
751static void sch_attach_disconnected_device(struct subchannel *sch,
752 struct ccw_device *cdev)
753{
754 struct subchannel *other_sch;
755 int ret;
756
757 other_sch = to_subchannel(get_device(cdev->dev.parent));
758 ret = device_move(&cdev->dev, &sch->dev);
759 if (ret) {
760 CIO_MSG_EVENT(2, "Moving disconnected device 0.%x.%04x failed "
761 "(ret=%d)!\n", cdev->private->dev_id.ssid,
762 cdev->private->dev_id.devno, ret);
763 put_device(&other_sch->dev);
764 return;
765 }
766 other_sch->dev.driver_data = NULL;
767 /* No need to keep a subchannel without ccw device around. */
768 css_sch_device_unregister(other_sch);
769 put_device(&other_sch->dev);
770 sch_attach_device(sch, cdev);
771}
772
773static void sch_attach_orphaned_device(struct subchannel *sch,
774 struct ccw_device *cdev)
775{
776 int ret;
777
778 /* Try to move the ccw device to its new subchannel. */
779 ret = device_move(&cdev->dev, &sch->dev);
780 if (ret) {
781 CIO_MSG_EVENT(0, "Moving device 0.%x.%04x from orphanage "
782 "failed (ret=%d)!\n",
783 cdev->private->dev_id.ssid,
784 cdev->private->dev_id.devno, ret);
785 return;
786 }
787 sch_attach_device(sch, cdev);
788}
789
790static void sch_create_and_recog_new_device(struct subchannel *sch)
791{
792 struct ccw_device *cdev;
793
794 /* Need to allocate a new ccw device. */
795 cdev = io_subchannel_create_ccwdev(sch);
796 if (IS_ERR(cdev)) {
797 /* OK, we did everything we could... */
798 css_sch_device_unregister(sch);
799 return;
800 }
801 spin_lock_irq(sch->lock);
802 sch->dev.driver_data = cdev;
803 spin_unlock_irq(sch->lock);
804 /* Start recognition for the new ccw device. */
805 if (io_subchannel_recog(cdev, sch)) {
806 spin_lock_irq(sch->lock);
807 sch->dev.driver_data = NULL;
808 spin_unlock_irq(sch->lock);
809 if (cdev->dev.release)
810 cdev->dev.release(&cdev->dev);
811 css_sch_device_unregister(sch);
812 }
813}
814
815
816void ccw_device_move_to_orphanage(struct work_struct *work)
817{
818 struct ccw_device_private *priv;
819 struct ccw_device *cdev;
820 struct ccw_device *replacing_cdev;
821 struct subchannel *sch;
822 int ret;
823 struct channel_subsystem *css;
824 struct ccw_dev_id dev_id;
825
826 priv = container_of(work, struct ccw_device_private, kick_work);
827 cdev = priv->cdev;
828 sch = to_subchannel(cdev->dev.parent);
829 css = to_css(sch->dev.parent);
830 dev_id.devno = sch->schib.pmcw.dev;
831 dev_id.ssid = sch->schid.ssid;
832
833 /*
834 * Move the orphaned ccw device to the orphanage so the replacing
835 * ccw device can take its place on the subchannel.
836 */
837 ret = device_move(&cdev->dev, &css->pseudo_subchannel->dev);
838 if (ret) {
839 CIO_MSG_EVENT(0, "Moving device 0.%x.%04x to orphanage failed "
840 "(ret=%d)!\n", cdev->private->dev_id.ssid,
841 cdev->private->dev_id.devno, ret);
842 return;
843 }
844 cdev->ccwlock = css->pseudo_subchannel->lock;
845 /*
846 * Search for the replacing ccw device
847 * - among the disconnected devices
848 * - in the orphanage
849 */
850 replacing_cdev = get_disc_ccwdev_by_dev_id(&dev_id, cdev);
851 if (replacing_cdev) {
852 sch_attach_disconnected_device(sch, replacing_cdev);
853 return;
854 }
855 replacing_cdev = get_orphaned_ccwdev_by_dev_id(css, &dev_id);
856 if (replacing_cdev) {
857 sch_attach_orphaned_device(sch, replacing_cdev);
858 return;
859 }
860 sch_create_and_recog_new_device(sch);
861}
862
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863/*
864 * Register recognized device.
865 */
866static void
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100867io_subchannel_register(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868{
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100869 struct ccw_device_private *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 struct ccw_device *cdev;
871 struct subchannel *sch;
872 int ret;
873 unsigned long flags;
874
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100875 priv = container_of(work, struct ccw_device_private, kick_work);
876 cdev = priv->cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 sch = to_subchannel(cdev->dev.parent);
878
Cornelia Huck47af5512006-12-04 15:41:07 +0100879 /*
880 * io_subchannel_register() will also be called after device
881 * recognition has been done for a boxed device (which will already
882 * be registered). We need to reprobe since we may now have sense id
883 * information.
884 */
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700885 if (klist_node_attached(&cdev->dev.knode_parent)) {
Cornelia Huck47af5512006-12-04 15:41:07 +0100886 if (!cdev->drv) {
887 ret = device_reprobe(&cdev->dev);
888 if (ret)
889 /* We can't do much here. */
890 dev_info(&cdev->dev, "device_reprobe() returned"
891 " %d\n", ret);
892 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 goto out;
894 }
895 /* make it known to the system */
896 ret = ccw_device_register(cdev);
897 if (ret) {
898 printk (KERN_WARNING "%s: could not register %s\n",
899 __func__, cdev->dev.bus_id);
900 put_device(&cdev->dev);
Cornelia Huck2ec22982006-12-08 15:54:26 +0100901 spin_lock_irqsave(sch->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 sch->dev.driver_data = NULL;
Cornelia Huck2ec22982006-12-08 15:54:26 +0100903 spin_unlock_irqrestore(sch->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 kfree (cdev->private);
905 kfree (cdev);
906 put_device(&sch->dev);
907 if (atomic_dec_and_test(&ccw_device_init_count))
908 wake_up(&ccw_device_init_wq);
909 return;
910 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 put_device(&cdev->dev);
912out:
913 cdev->private->flags.recog_done = 1;
914 put_device(&sch->dev);
915 wake_up(&cdev->private->wait_q);
916 if (atomic_dec_and_test(&ccw_device_init_count))
917 wake_up(&ccw_device_init_wq);
918}
919
920void
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100921ccw_device_call_sch_unregister(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922{
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100923 struct ccw_device_private *priv;
924 struct ccw_device *cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 struct subchannel *sch;
926
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100927 priv = container_of(work, struct ccw_device_private, kick_work);
928 cdev = priv->cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 sch = to_subchannel(cdev->dev.parent);
Cornelia Huck6ab48792006-07-12 16:39:50 +0200930 css_sch_device_unregister(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 /* Reset intparm to zeroes. */
932 sch->schib.pmcw.intparm = 0;
933 cio_modify(sch);
934 put_device(&cdev->dev);
935 put_device(&sch->dev);
936}
937
938/*
939 * subchannel recognition done. Called from the state machine.
940 */
941void
942io_subchannel_recog_done(struct ccw_device *cdev)
943{
944 struct subchannel *sch;
945
946 if (css_init_done == 0) {
947 cdev->private->flags.recog_done = 1;
948 return;
949 }
950 switch (cdev->private->state) {
951 case DEV_STATE_NOT_OPER:
952 cdev->private->flags.recog_done = 1;
953 /* Remove device found not operational. */
954 if (!get_device(&cdev->dev))
955 break;
956 sch = to_subchannel(cdev->dev.parent);
957 PREPARE_WORK(&cdev->private->kick_work,
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100958 ccw_device_call_sch_unregister);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 queue_work(slow_path_wq, &cdev->private->kick_work);
960 if (atomic_dec_and_test(&ccw_device_init_count))
961 wake_up(&ccw_device_init_wq);
962 break;
963 case DEV_STATE_BOXED:
964 /* Device did not respond in time. */
965 case DEV_STATE_OFFLINE:
966 /*
967 * We can't register the device in interrupt context so
968 * we schedule a work item.
969 */
970 if (!get_device(&cdev->dev))
971 break;
972 PREPARE_WORK(&cdev->private->kick_work,
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100973 io_subchannel_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 queue_work(slow_path_wq, &cdev->private->kick_work);
975 break;
976 }
977}
978
979static int
980io_subchannel_recog(struct ccw_device *cdev, struct subchannel *sch)
981{
982 int rc;
983 struct ccw_device_private *priv;
984
985 sch->dev.driver_data = cdev;
986 sch->driver = &io_subchannel_driver;
Cornelia Huck2ec22982006-12-08 15:54:26 +0100987 cdev->ccwlock = sch->lock;
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800988
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 /* Init private data. */
990 priv = cdev->private;
Cornelia Huck78964262006-10-11 15:31:38 +0200991 priv->dev_id.devno = sch->schib.pmcw.dev;
992 priv->dev_id.ssid = sch->schid.ssid;
993 priv->schid = sch->schid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 priv->state = DEV_STATE_NOT_OPER;
995 INIT_LIST_HEAD(&priv->cmb_list);
996 init_waitqueue_head(&priv->wait_q);
997 init_timer(&priv->timer);
998
999 /* Set an initial name for the device. */
Cornelia Huckfb6958a2006-01-06 00:19:25 -08001000 snprintf (cdev->dev.bus_id, BUS_ID_SIZE, "0.%x.%04x",
1001 sch->schid.ssid, sch->schib.pmcw.dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002
1003 /* Increase counter of devices currently in recognition. */
1004 atomic_inc(&ccw_device_init_count);
1005
1006 /* Start async. device sensing. */
Cornelia Huck2ec22982006-12-08 15:54:26 +01001007 spin_lock_irq(sch->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 rc = ccw_device_recognition(cdev);
Cornelia Huck2ec22982006-12-08 15:54:26 +01001009 spin_unlock_irq(sch->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 if (rc) {
1011 if (atomic_dec_and_test(&ccw_device_init_count))
1012 wake_up(&ccw_device_init_wq);
1013 }
1014 return rc;
1015}
1016
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +01001017static void ccw_device_move_to_sch(struct work_struct *work)
1018{
1019 struct ccw_device_private *priv;
1020 int rc;
1021 struct subchannel *sch;
1022 struct ccw_device *cdev;
1023 struct subchannel *former_parent;
1024
1025 priv = container_of(work, struct ccw_device_private, kick_work);
1026 sch = priv->sch;
1027 cdev = priv->cdev;
1028 former_parent = ccw_device_is_orphan(cdev) ?
1029 NULL : to_subchannel(get_device(cdev->dev.parent));
1030 mutex_lock(&sch->reg_mutex);
1031 /* Try to move the ccw device to its new subchannel. */
1032 rc = device_move(&cdev->dev, &sch->dev);
1033 mutex_unlock(&sch->reg_mutex);
1034 if (rc) {
1035 CIO_MSG_EVENT(2, "Moving device 0.%x.%04x to subchannel "
1036 "0.%x.%04x failed (ret=%d)!\n",
1037 cdev->private->dev_id.ssid,
1038 cdev->private->dev_id.devno, sch->schid.ssid,
1039 sch->schid.sch_no, rc);
1040 css_sch_device_unregister(sch);
1041 goto out;
1042 }
1043 if (former_parent) {
1044 spin_lock_irq(former_parent->lock);
1045 former_parent->dev.driver_data = NULL;
1046 spin_unlock_irq(former_parent->lock);
1047 css_sch_device_unregister(former_parent);
1048 /* Reset intparm to zeroes. */
1049 former_parent->schib.pmcw.intparm = 0;
1050 cio_modify(former_parent);
1051 }
1052 sch_attach_device(sch, cdev);
1053out:
1054 if (former_parent)
1055 put_device(&former_parent->dev);
1056 put_device(&cdev->dev);
1057}
1058
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059static int
Cornelia Huck8bbace72006-01-11 10:56:22 +01001060io_subchannel_probe (struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 struct ccw_device *cdev;
1063 int rc;
1064 unsigned long flags;
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +01001065 struct ccw_dev_id dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 if (sch->dev.driver_data) {
1068 /*
1069 * This subchannel already has an associated ccw_device.
1070 * Register it and exit. This happens for all early
1071 * device, e.g. the console.
1072 */
1073 cdev = sch->dev.driver_data;
1074 device_initialize(&cdev->dev);
1075 ccw_device_register(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 /*
1077 * Check if the device is already online. If it is
1078 * the reference count needs to be corrected
1079 * (see ccw_device_online and css_init_done for the
1080 * ugly details).
1081 */
1082 if (cdev->private->state != DEV_STATE_NOT_OPER &&
1083 cdev->private->state != DEV_STATE_OFFLINE &&
1084 cdev->private->state != DEV_STATE_BOXED)
1085 get_device(&cdev->dev);
1086 return 0;
1087 }
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +01001088 /*
1089 * First check if a fitting device may be found amongst the
1090 * disconnected devices or in the orphanage.
1091 */
1092 dev_id.devno = sch->schib.pmcw.dev;
1093 dev_id.ssid = sch->schid.ssid;
1094 cdev = get_disc_ccwdev_by_dev_id(&dev_id, NULL);
1095 if (!cdev)
1096 cdev = get_orphaned_ccwdev_by_dev_id(to_css(sch->dev.parent),
1097 &dev_id);
1098 if (cdev) {
1099 /*
1100 * Schedule moving the device until when we have a registered
1101 * subchannel to move to and succeed the probe. We can
1102 * unregister later again, when the probe is through.
1103 */
1104 cdev->private->sch = sch;
1105 PREPARE_WORK(&cdev->private->kick_work,
1106 ccw_device_move_to_sch);
1107 queue_work(slow_path_wq, &cdev->private->kick_work);
1108 return 0;
1109 }
Cornelia Huck7674da72006-12-08 15:54:21 +01001110 cdev = io_subchannel_create_ccwdev(sch);
1111 if (IS_ERR(cdev))
1112 return PTR_ERR(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113
Cornelia Huck8bbace72006-01-11 10:56:22 +01001114 rc = io_subchannel_recog(cdev, sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 if (rc) {
Cornelia Huck2ec22982006-12-08 15:54:26 +01001116 spin_lock_irqsave(sch->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 sch->dev.driver_data = NULL;
Cornelia Huck2ec22982006-12-08 15:54:26 +01001118 spin_unlock_irqrestore(sch->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 if (cdev->dev.release)
1120 cdev->dev.release(&cdev->dev);
1121 }
1122
1123 return rc;
1124}
1125
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126static int
Cornelia Huck8bbace72006-01-11 10:56:22 +01001127io_subchannel_remove (struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128{
1129 struct ccw_device *cdev;
1130 unsigned long flags;
1131
Cornelia Huck8bbace72006-01-11 10:56:22 +01001132 if (!sch->dev.driver_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 return 0;
Cornelia Huck8bbace72006-01-11 10:56:22 +01001134 cdev = sch->dev.driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 /* Set ccw device to not operational and drop reference. */
1136 spin_lock_irqsave(cdev->ccwlock, flags);
Cornelia Huck8bbace72006-01-11 10:56:22 +01001137 sch->dev.driver_data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 cdev->private->state = DEV_STATE_NOT_OPER;
1139 spin_unlock_irqrestore(cdev->ccwlock, flags);
1140 /*
1141 * Put unregistration on workqueue to avoid livelocks on the css bus
1142 * semaphore.
1143 */
1144 if (get_device(&cdev->dev)) {
1145 PREPARE_WORK(&cdev->private->kick_work,
Martin Schwidefskyc1637532006-12-08 15:53:57 +01001146 ccw_device_unregister);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 queue_work(ccw_device_work, &cdev->private->kick_work);
1148 }
1149 return 0;
1150}
1151
1152static int
1153io_subchannel_notify(struct device *dev, int event)
1154{
1155 struct ccw_device *cdev;
1156
1157 cdev = dev->driver_data;
1158 if (!cdev)
1159 return 0;
1160 if (!cdev->drv)
1161 return 0;
1162 if (!cdev->online)
1163 return 0;
1164 return cdev->drv->notify ? cdev->drv->notify(cdev, event) : 0;
1165}
1166
1167static void
1168io_subchannel_verify(struct device *dev)
1169{
1170 struct ccw_device *cdev;
1171
1172 cdev = dev->driver_data;
1173 if (cdev)
1174 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1175}
1176
1177static void
1178io_subchannel_ioterm(struct device *dev)
1179{
1180 struct ccw_device *cdev;
1181
1182 cdev = dev->driver_data;
1183 if (!cdev)
1184 return;
Cornelia Huckd23861f2006-12-04 15:41:04 +01001185 /* Internal I/O will be retried by the interrupt handler. */
1186 if (cdev->private->flags.intretry)
1187 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 cdev->private->state = DEV_STATE_CLEAR_VERIFY;
1189 if (cdev->handler)
1190 cdev->handler(cdev, cdev->private->intparm,
1191 ERR_PTR(-EIO));
1192}
1193
1194static void
Cornelia Huck8bbace72006-01-11 10:56:22 +01001195io_subchannel_shutdown(struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 struct ccw_device *cdev;
1198 int ret;
1199
Cornelia Huck8bbace72006-01-11 10:56:22 +01001200 cdev = sch->dev.driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201
Cornelia Hucka8237fc2006-01-06 00:19:21 -08001202 if (cio_is_console(sch->schid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 return;
1204 if (!sch->schib.pmcw.ena)
1205 /* Nothing to do. */
1206 return;
1207 ret = cio_disable_subchannel(sch);
1208 if (ret != -EBUSY)
1209 /* Subchannel is disabled, we're done. */
1210 return;
1211 cdev->private->state = DEV_STATE_QUIESCE;
1212 if (cdev->handler)
1213 cdev->handler(cdev, cdev->private->intparm,
1214 ERR_PTR(-EIO));
1215 ret = ccw_device_cancel_halt_clear(cdev);
1216 if (ret == -EBUSY) {
1217 ccw_device_set_timeout(cdev, HZ/10);
1218 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
1219 }
1220 cio_disable_subchannel(sch);
1221}
1222
1223#ifdef CONFIG_CCW_CONSOLE
1224static struct ccw_device console_cdev;
1225static struct ccw_device_private console_private;
1226static int console_cdev_in_use;
1227
Cornelia Huck2ec22982006-12-08 15:54:26 +01001228static DEFINE_SPINLOCK(ccw_console_lock);
1229
1230spinlock_t * cio_get_console_lock(void)
1231{
1232 return &ccw_console_lock;
1233}
1234
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235static int
1236ccw_device_console_enable (struct ccw_device *cdev, struct subchannel *sch)
1237{
1238 int rc;
1239
1240 /* Initialize the ccw_device structure. */
Heiko Carstens292888c2006-08-30 14:33:35 +02001241 cdev->dev.parent= &sch->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 rc = io_subchannel_recog(cdev, sch);
1243 if (rc)
1244 return rc;
1245
1246 /* Now wait for the async. recognition to come to an end. */
1247 spin_lock_irq(cdev->ccwlock);
1248 while (!dev_fsm_final_state(cdev))
1249 wait_cons_dev();
1250 rc = -EIO;
1251 if (cdev->private->state != DEV_STATE_OFFLINE)
1252 goto out_unlock;
1253 ccw_device_online(cdev);
1254 while (!dev_fsm_final_state(cdev))
1255 wait_cons_dev();
1256 if (cdev->private->state != DEV_STATE_ONLINE)
1257 goto out_unlock;
1258 rc = 0;
1259out_unlock:
1260 spin_unlock_irq(cdev->ccwlock);
1261 return 0;
1262}
1263
1264struct ccw_device *
1265ccw_device_probe_console(void)
1266{
1267 struct subchannel *sch;
1268 int ret;
1269
1270 if (xchg(&console_cdev_in_use, 1) != 0)
Peter Oberparleiter600b5d12006-02-01 03:06:35 -08001271 return ERR_PTR(-EBUSY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 sch = cio_probe_console();
1273 if (IS_ERR(sch)) {
1274 console_cdev_in_use = 0;
1275 return (void *) sch;
1276 }
1277 memset(&console_cdev, 0, sizeof(struct ccw_device));
1278 memset(&console_private, 0, sizeof(struct ccw_device_private));
1279 console_cdev.private = &console_private;
Martin Schwidefskyc1637532006-12-08 15:53:57 +01001280 console_private.cdev = &console_cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 ret = ccw_device_console_enable(&console_cdev, sch);
1282 if (ret) {
1283 cio_release_console();
1284 console_cdev_in_use = 0;
1285 return ERR_PTR(ret);
1286 }
1287 console_cdev.online = 1;
1288 return &console_cdev;
1289}
1290#endif
1291
1292/*
1293 * get ccw_device matching the busid, but only if owned by cdrv
1294 */
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001295static int
1296__ccwdev_check_busid(struct device *dev, void *id)
1297{
1298 char *bus_id;
1299
Cornelia Huck12975ae2006-10-11 15:31:47 +02001300 bus_id = id;
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001301
1302 return (strncmp(bus_id, dev->bus_id, BUS_ID_SIZE) == 0);
1303}
1304
1305
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306struct ccw_device *
1307get_ccwdev_by_busid(struct ccw_driver *cdrv, const char *bus_id)
1308{
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001309 struct device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 struct device_driver *drv;
1311
1312 drv = get_driver(&cdrv->driver);
1313 if (!drv)
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001314 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001316 dev = driver_find_device(drv, NULL, (void *)bus_id,
1317 __ccwdev_check_busid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 put_driver(drv);
1319
Heiko Carstensd2c993d2006-07-12 16:41:55 +02001320 return dev ? to_ccwdev(dev) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321}
1322
1323/************************** device driver handling ************************/
1324
1325/* This is the implementation of the ccw_driver class. The probe, remove
1326 * and release methods are initially very similar to the device_driver
1327 * implementations, with the difference that they have ccw_device
1328 * arguments.
1329 *
1330 * A ccw driver also contains the information that is needed for
1331 * device matching.
1332 */
1333static int
1334ccw_device_probe (struct device *dev)
1335{
1336 struct ccw_device *cdev = to_ccwdev(dev);
1337 struct ccw_driver *cdrv = to_ccwdrv(dev->driver);
1338 int ret;
1339
1340 cdev->drv = cdrv; /* to let the driver call _set_online */
1341
1342 ret = cdrv->probe ? cdrv->probe(cdev) : -ENODEV;
1343
1344 if (ret) {
Heiko Carstensd2c993d2006-07-12 16:41:55 +02001345 cdev->drv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 return ret;
1347 }
1348
1349 return 0;
1350}
1351
1352static int
1353ccw_device_remove (struct device *dev)
1354{
1355 struct ccw_device *cdev = to_ccwdev(dev);
1356 struct ccw_driver *cdrv = cdev->drv;
1357 int ret;
1358
1359 pr_debug("removing device %s\n", cdev->dev.bus_id);
1360 if (cdrv->remove)
1361 cdrv->remove(cdev);
1362 if (cdev->online) {
1363 cdev->online = 0;
1364 spin_lock_irq(cdev->ccwlock);
1365 ret = ccw_device_offline(cdev);
1366 spin_unlock_irq(cdev->ccwlock);
1367 if (ret == 0)
1368 wait_event(cdev->private->wait_q,
1369 dev_fsm_final_state(cdev));
1370 else
1371 //FIXME: we can't fail!
1372 pr_debug("ccw_device_offline returned %d, device %s\n",
1373 ret, cdev->dev.bus_id);
1374 }
1375 ccw_device_set_timeout(cdev, 0);
Heiko Carstensd2c993d2006-07-12 16:41:55 +02001376 cdev->drv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 return 0;
1378}
1379
Cornelia Huck8bbace72006-01-11 10:56:22 +01001380struct bus_type ccw_bus_type = {
1381 .name = "ccw",
1382 .match = ccw_bus_match,
1383 .uevent = ccw_uevent,
1384 .probe = ccw_device_probe,
1385 .remove = ccw_device_remove,
1386};
1387
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388int
1389ccw_driver_register (struct ccw_driver *cdriver)
1390{
1391 struct device_driver *drv = &cdriver->driver;
1392
1393 drv->bus = &ccw_bus_type;
1394 drv->name = cdriver->name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395
1396 return driver_register(drv);
1397}
1398
1399void
1400ccw_driver_unregister (struct ccw_driver *cdriver)
1401{
1402 driver_unregister(&cdriver->driver);
1403}
1404
Cornelia Hucka8237fc2006-01-06 00:19:21 -08001405/* Helper func for qdio. */
1406struct subchannel_id
1407ccw_device_get_subchannel_id(struct ccw_device *cdev)
1408{
1409 struct subchannel *sch;
1410
1411 sch = to_subchannel(cdev->dev.parent);
1412 return sch->schid;
1413}
1414
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415MODULE_LICENSE("GPL");
1416EXPORT_SYMBOL(ccw_device_set_online);
1417EXPORT_SYMBOL(ccw_device_set_offline);
1418EXPORT_SYMBOL(ccw_driver_register);
1419EXPORT_SYMBOL(ccw_driver_unregister);
1420EXPORT_SYMBOL(get_ccwdev_by_busid);
1421EXPORT_SYMBOL(ccw_bus_type);
1422EXPORT_SYMBOL(ccw_device_work);
1423EXPORT_SYMBOL(ccw_device_notify_work);
Cornelia Hucka8237fc2006-01-06 00:19:21 -08001424EXPORT_SYMBOL_GPL(ccw_device_get_subchannel_id);