blob: 7bb44e73ea9d8af83728e3bee3119c1314c053bd [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * drivers/s390/cio/device.c
3 * bus driver for ccw devices
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
5 * Copyright (C) 2002 IBM Deutschland Entwicklung GmbH,
6 * IBM Corporation
7 * Author(s): Arnd Bergmann (arndb@de.ibm.com)
Cornelia Huck4ce3b302006-01-14 13:21:04 -08008 * Cornelia Huck (cornelia.huck@de.ibm.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 * Martin Schwidefsky (schwidefsky@de.ibm.com)
10 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/module.h>
12#include <linux/init.h>
13#include <linux/spinlock.h>
14#include <linux/errno.h>
15#include <linux/err.h>
16#include <linux/slab.h>
17#include <linux/list.h>
18#include <linux/device.h>
19#include <linux/workqueue.h>
20
21#include <asm/ccwdev.h>
22#include <asm/cio.h>
Tim Schmielau4e57b682005-10-30 15:03:48 -080023#include <asm/param.h> /* HZ */
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
25#include "cio.h"
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +010026#include "cio_debug.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include "css.h"
28#include "device.h"
29#include "ioasm.h"
30
31/******************* bus type handling ***********************/
32
33/* The Linux driver model distinguishes between a bus type and
34 * the bus itself. Of course we only have one channel
35 * subsystem driver and one channel system per machine, but
36 * we still use the abstraction. T.R. says it's a good idea. */
37static int
38ccw_bus_match (struct device * dev, struct device_driver * drv)
39{
40 struct ccw_device *cdev = to_ccwdev(dev);
41 struct ccw_driver *cdrv = to_ccwdrv(drv);
42 const struct ccw_device_id *ids = cdrv->ids, *found;
43
44 if (!ids)
45 return 0;
46
47 found = ccw_device_id_match(ids, &cdev->id);
48 if (!found)
49 return 0;
50
51 cdev->id.driver_info = found->driver_info;
52
53 return 1;
54}
55
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +020056/* Store modalias string delimited by prefix/suffix string into buffer with
57 * specified size. Return length of resulting string (excluding trailing '\0')
58 * even if string doesn't fit buffer (snprintf semantics). */
Cornelia Huckcfbe9bb2007-04-27 16:01:32 +020059static int snprint_alias(char *buf, size_t size,
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +020060 struct ccw_device_id *id, const char *suffix)
61{
62 int len;
63
Cornelia Huckcfbe9bb2007-04-27 16:01:32 +020064 len = snprintf(buf, size, "ccw:t%04Xm%02X", id->cu_type, id->cu_model);
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +020065 if (len > size)
66 return len;
67 buf += len;
68 size -= len;
69
70 if (id->dev_type != 0)
71 len += snprintf(buf, size, "dt%04Xdm%02X%s", id->dev_type,
72 id->dev_model, suffix);
73 else
74 len += snprintf(buf, size, "dtdm%s", suffix);
75
76 return len;
77}
78
79/* Set up environment variables for ccw device uevent. Return 0 on success,
80 * non-zero otherwise. */
81static int ccw_uevent(struct device *dev, char **envp, int num_envp,
82 char *buffer, int buffer_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -070083{
84 struct ccw_device *cdev = to_ccwdev(dev);
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +020085 struct ccw_device_id *id = &(cdev->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 int i = 0;
Cornelia Huckcfbe9bb2007-04-27 16:01:32 +020087 int len = 0;
88 int ret;
89 char modalias_buf[30];
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +020091 /* CU_TYPE= */
Cornelia Huckcfbe9bb2007-04-27 16:01:32 +020092 ret = add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &len,
93 "CU_TYPE=%04X", id->cu_type);
94 if (ret)
95 return ret;
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +020096
97 /* CU_MODEL= */
Cornelia Huckcfbe9bb2007-04-27 16:01:32 +020098 ret = add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &len,
99 "CU_MODEL=%02X", id->cu_model);
100 if (ret)
101 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
103 /* The next two can be zero, that's ok for us */
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +0200104 /* DEV_TYPE= */
Cornelia Huckcfbe9bb2007-04-27 16:01:32 +0200105 ret = add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &len,
106 "DEV_TYPE=%04X", id->dev_type);
107 if (ret)
108 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +0200110 /* DEV_MODEL= */
Cornelia Huckcfbe9bb2007-04-27 16:01:32 +0200111 ret = add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &len,
112 "DEV_MODEL=%02X", id->dev_model);
113 if (ret)
114 return ret;
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +0200115
116 /* MODALIAS= */
Cornelia Huckcfbe9bb2007-04-27 16:01:32 +0200117 snprint_alias(modalias_buf, sizeof(modalias_buf), id, "");
118 ret = add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &len,
119 "MODALIAS=%s", modalias_buf);
120 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121}
122
Cornelia Huck8bbace72006-01-11 10:56:22 +0100123struct bus_type ccw_bus_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
Cornelia Huck8bbace72006-01-11 10:56:22 +0100125static int io_subchannel_probe (struct subchannel *);
126static int io_subchannel_remove (struct subchannel *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127static int io_subchannel_notify(struct device *, int);
128static void io_subchannel_verify(struct device *);
129static void io_subchannel_ioterm(struct device *);
Cornelia Huck8bbace72006-01-11 10:56:22 +0100130static void io_subchannel_shutdown(struct subchannel *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
132struct css_driver io_subchannel_driver = {
133 .subchannel_type = SUBCHANNEL_TYPE_IO,
134 .drv = {
135 .name = "io_subchannel",
136 .bus = &css_bus_type,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 },
138 .irq = io_subchannel_irq,
139 .notify = io_subchannel_notify,
140 .verify = io_subchannel_verify,
141 .termination = io_subchannel_ioterm,
Cornelia Huck8bbace72006-01-11 10:56:22 +0100142 .probe = io_subchannel_probe,
143 .remove = io_subchannel_remove,
144 .shutdown = io_subchannel_shutdown,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145};
146
147struct workqueue_struct *ccw_device_work;
148struct workqueue_struct *ccw_device_notify_work;
Peter Oberparleiter40154b82006-06-29 14:57:03 +0200149wait_queue_head_t ccw_device_init_wq;
150atomic_t ccw_device_init_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
152static int __init
153init_ccw_bus_type (void)
154{
155 int ret;
156
157 init_waitqueue_head(&ccw_device_init_wq);
158 atomic_set(&ccw_device_init_count, 0);
159
160 ccw_device_work = create_singlethread_workqueue("cio");
161 if (!ccw_device_work)
162 return -ENOMEM; /* FIXME: better errno ? */
163 ccw_device_notify_work = create_singlethread_workqueue("cio_notify");
164 if (!ccw_device_notify_work) {
165 ret = -ENOMEM; /* FIXME: better errno ? */
166 goto out_err;
167 }
168 slow_path_wq = create_singlethread_workqueue("kslowcrw");
169 if (!slow_path_wq) {
170 ret = -ENOMEM; /* FIXME: better errno ? */
171 goto out_err;
172 }
173 if ((ret = bus_register (&ccw_bus_type)))
174 goto out_err;
175
176 if ((ret = driver_register(&io_subchannel_driver.drv)))
177 goto out_err;
178
179 wait_event(ccw_device_init_wq,
180 atomic_read(&ccw_device_init_count) == 0);
181 flush_workqueue(ccw_device_work);
182 return 0;
183out_err:
184 if (ccw_device_work)
185 destroy_workqueue(ccw_device_work);
186 if (ccw_device_notify_work)
187 destroy_workqueue(ccw_device_notify_work);
188 if (slow_path_wq)
189 destroy_workqueue(slow_path_wq);
190 return ret;
191}
192
193static void __exit
194cleanup_ccw_bus_type (void)
195{
196 driver_unregister(&io_subchannel_driver.drv);
197 bus_unregister(&ccw_bus_type);
198 destroy_workqueue(ccw_device_notify_work);
199 destroy_workqueue(ccw_device_work);
200}
201
202subsys_initcall(init_ccw_bus_type);
203module_exit(cleanup_ccw_bus_type);
204
205/************************ device handling **************************/
206
207/*
208 * A ccw_device has some interfaces in sysfs in addition to the
209 * standard ones.
210 * The following entries are designed to export the information which
211 * resided in 2.4 in /proc/subchannels. Subchannel and device number
212 * are obvious, so they don't have an entry :)
213 * TODO: Split chpids and pimpampom up? Where is "in use" in the tree?
214 */
215static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400216chpids_show (struct device * dev, struct device_attribute *attr, char * buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217{
218 struct subchannel *sch = to_subchannel(dev);
Peter Oberparleiter7ad6a242007-04-27 16:01:35 +0200219 struct chsc_ssd_info *ssd = &sch->ssd_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 ssize_t ret = 0;
221 int chp;
Peter Oberparleiter7ad6a242007-04-27 16:01:35 +0200222 int mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
Peter Oberparleiter7ad6a242007-04-27 16:01:35 +0200224 for (chp = 0; chp < 8; chp++) {
225 mask = 0x80 >> chp;
226 if (ssd->path_mask & mask)
227 ret += sprintf(buf + ret, "%02x ", ssd->chpid[chp].id);
228 else
229 ret += sprintf(buf + ret, "00 ");
230 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 ret += sprintf (buf+ret, "\n");
232 return min((ssize_t)PAGE_SIZE, ret);
233}
234
235static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400236pimpampom_show (struct device * dev, struct device_attribute *attr, char * buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237{
238 struct subchannel *sch = to_subchannel(dev);
239 struct pmcw *pmcw = &sch->schib.pmcw;
240
241 return sprintf (buf, "%02x %02x %02x\n",
242 pmcw->pim, pmcw->pam, pmcw->pom);
243}
244
245static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400246devtype_show (struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247{
248 struct ccw_device *cdev = to_ccwdev(dev);
249 struct ccw_device_id *id = &(cdev->id);
250
251 if (id->dev_type != 0)
252 return sprintf(buf, "%04x/%02x\n",
253 id->dev_type, id->dev_model);
254 else
255 return sprintf(buf, "n/a\n");
256}
257
258static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400259cutype_show (struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260{
261 struct ccw_device *cdev = to_ccwdev(dev);
262 struct ccw_device_id *id = &(cdev->id);
263
264 return sprintf(buf, "%04x/%02x\n",
265 id->cu_type, id->cu_model);
266}
267
268static ssize_t
Bastian Blankf1fc78a2005-10-30 15:00:12 -0800269modalias_show (struct device *dev, struct device_attribute *attr, char *buf)
270{
271 struct ccw_device *cdev = to_ccwdev(dev);
272 struct ccw_device_id *id = &(cdev->id);
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +0200273 int len;
Bastian Blankf1fc78a2005-10-30 15:00:12 -0800274
Cornelia Huckcfbe9bb2007-04-27 16:01:32 +0200275 len = snprint_alias(buf, PAGE_SIZE, id, "\n") + 1;
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +0200276
277 return len > PAGE_SIZE ? PAGE_SIZE : len;
Bastian Blankf1fc78a2005-10-30 15:00:12 -0800278}
279
280static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400281online_show (struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282{
283 struct ccw_device *cdev = to_ccwdev(dev);
284
285 return sprintf(buf, cdev->online ? "1\n" : "0\n");
286}
287
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100288int ccw_device_is_orphan(struct ccw_device *cdev)
289{
290 return sch_is_pseudo_sch(to_subchannel(cdev->dev.parent));
291}
292
Cornelia Huck7674da72006-12-08 15:54:21 +0100293static void ccw_device_unregister(struct work_struct *work)
294{
295 struct ccw_device_private *priv;
296 struct ccw_device *cdev;
297
298 priv = container_of(work, struct ccw_device_private, kick_work);
299 cdev = priv->cdev;
300 if (test_and_clear_bit(1, &cdev->private->registered))
301 device_unregister(&cdev->dev);
302 put_device(&cdev->dev);
303}
304
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305static void
306ccw_device_remove_disconnected(struct ccw_device *cdev)
307{
308 struct subchannel *sch;
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100309 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 /*
311 * Forced offline in disconnected state means
312 * 'throw away device'.
313 */
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100314 if (ccw_device_is_orphan(cdev)) {
315 /* Deregister ccw device. */
316 spin_lock_irqsave(cdev->ccwlock, flags);
317 cdev->private->state = DEV_STATE_NOT_OPER;
318 spin_unlock_irqrestore(cdev->ccwlock, flags);
319 if (get_device(&cdev->dev)) {
320 PREPARE_WORK(&cdev->private->kick_work,
321 ccw_device_unregister);
322 queue_work(ccw_device_work, &cdev->private->kick_work);
323 }
324 return ;
325 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 sch = to_subchannel(cdev->dev.parent);
Cornelia Huck6ab48792006-07-12 16:39:50 +0200327 css_sch_device_unregister(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 /* Reset intparm to zeroes. */
329 sch->schib.pmcw.intparm = 0;
330 cio_modify(sch);
331 put_device(&sch->dev);
332}
333
334int
335ccw_device_set_offline(struct ccw_device *cdev)
336{
337 int ret;
338
339 if (!cdev)
340 return -ENODEV;
341 if (!cdev->online || !cdev->drv)
342 return -EINVAL;
343
344 if (cdev->drv->set_offline) {
345 ret = cdev->drv->set_offline(cdev);
346 if (ret != 0)
347 return ret;
348 }
349 cdev->online = 0;
350 spin_lock_irq(cdev->ccwlock);
351 ret = ccw_device_offline(cdev);
352 if (ret == -ENODEV) {
353 if (cdev->private->state != DEV_STATE_NOT_OPER) {
354 cdev->private->state = DEV_STATE_OFFLINE;
355 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
356 }
357 spin_unlock_irq(cdev->ccwlock);
358 return ret;
359 }
360 spin_unlock_irq(cdev->ccwlock);
361 if (ret == 0)
362 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
363 else {
364 pr_debug("ccw_device_offline returned %d, device %s\n",
365 ret, cdev->dev.bus_id);
366 cdev->online = 1;
367 }
368 return ret;
369}
370
371int
372ccw_device_set_online(struct ccw_device *cdev)
373{
374 int ret;
375
376 if (!cdev)
377 return -ENODEV;
378 if (cdev->online || !cdev->drv)
379 return -EINVAL;
380
381 spin_lock_irq(cdev->ccwlock);
382 ret = ccw_device_online(cdev);
383 spin_unlock_irq(cdev->ccwlock);
384 if (ret == 0)
385 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
386 else {
387 pr_debug("ccw_device_online returned %d, device %s\n",
388 ret, cdev->dev.bus_id);
389 return ret;
390 }
391 if (cdev->private->state != DEV_STATE_ONLINE)
392 return -ENODEV;
393 if (!cdev->drv->set_online || cdev->drv->set_online(cdev) == 0) {
394 cdev->online = 1;
395 return 0;
396 }
397 spin_lock_irq(cdev->ccwlock);
398 ret = ccw_device_offline(cdev);
399 spin_unlock_irq(cdev->ccwlock);
400 if (ret == 0)
401 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
402 else
403 pr_debug("ccw_device_offline returned %d, device %s\n",
404 ret, cdev->dev.bus_id);
Cornelia Huck6cadb782006-02-17 13:52:49 -0800405 return (ret == 0) ? -ENODEV : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406}
407
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200408static void online_store_handle_offline(struct ccw_device *cdev)
409{
410 if (cdev->private->state == DEV_STATE_DISCONNECTED)
411 ccw_device_remove_disconnected(cdev);
412 else if (cdev->drv && cdev->drv->set_offline)
413 ccw_device_set_offline(cdev);
414}
415
416static int online_store_recog_and_online(struct ccw_device *cdev)
417{
418 int ret;
419
420 /* Do device recognition, if needed. */
421 if (cdev->id.cu_type == 0) {
422 ret = ccw_device_recognition(cdev);
423 if (ret) {
424 printk(KERN_WARNING"Couldn't start recognition "
425 "for device %s (ret=%d)\n",
426 cdev->dev.bus_id, ret);
427 return ret;
428 }
429 wait_event(cdev->private->wait_q,
430 cdev->private->flags.recog_done);
431 }
432 if (cdev->drv && cdev->drv->set_online)
433 ccw_device_set_online(cdev);
434 return 0;
435}
436static void online_store_handle_online(struct ccw_device *cdev, int force)
437{
438 int ret;
439
440 ret = online_store_recog_and_online(cdev);
441 if (ret)
442 return;
443 if (force && cdev->private->state == DEV_STATE_BOXED) {
444 ret = ccw_device_stlck(cdev);
445 if (ret) {
446 printk(KERN_WARNING"ccw_device_stlck for device %s "
447 "returned %d!\n", cdev->dev.bus_id, ret);
448 return;
449 }
450 if (cdev->id.cu_type == 0)
451 cdev->private->state = DEV_STATE_NOT_OPER;
452 online_store_recog_and_online(cdev);
453 }
454
455}
456
457static ssize_t online_store (struct device *dev, struct device_attribute *attr,
458 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459{
460 struct ccw_device *cdev = to_ccwdev(dev);
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200461 int i, force;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 char *tmp;
463
Martin Schwidefsky973bd992006-01-06 00:19:07 -0800464 if (atomic_cmpxchg(&cdev->private->onoff, 0, 1) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 return -EAGAIN;
466
467 if (cdev->drv && !try_module_get(cdev->drv->owner)) {
468 atomic_set(&cdev->private->onoff, 0);
469 return -EINVAL;
470 }
471 if (!strncmp(buf, "force\n", count)) {
472 force = 1;
473 i = 1;
474 } else {
475 force = 0;
476 i = simple_strtoul(buf, &tmp, 16);
477 }
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200478
479 switch (i) {
480 case 0:
481 online_store_handle_offline(cdev);
482 break;
483 case 1:
484 online_store_handle_online(cdev, force);
485 break;
486 default:
487 count = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 if (cdev->drv)
490 module_put(cdev->drv->owner);
491 atomic_set(&cdev->private->onoff, 0);
492 return count;
493}
494
495static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400496available_show (struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497{
498 struct ccw_device *cdev = to_ccwdev(dev);
499 struct subchannel *sch;
500
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100501 if (ccw_device_is_orphan(cdev))
502 return sprintf(buf, "no device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 switch (cdev->private->state) {
504 case DEV_STATE_BOXED:
505 return sprintf(buf, "boxed\n");
506 case DEV_STATE_DISCONNECTED:
507 case DEV_STATE_DISCONNECTED_SENSE_ID:
508 case DEV_STATE_NOT_OPER:
509 sch = to_subchannel(dev->parent);
510 if (!sch->lpm)
511 return sprintf(buf, "no path\n");
512 else
513 return sprintf(buf, "no device\n");
514 default:
515 /* All other states considered fine. */
516 return sprintf(buf, "good\n");
517 }
518}
519
520static DEVICE_ATTR(chpids, 0444, chpids_show, NULL);
521static DEVICE_ATTR(pimpampom, 0444, pimpampom_show, NULL);
522static DEVICE_ATTR(devtype, 0444, devtype_show, NULL);
523static DEVICE_ATTR(cutype, 0444, cutype_show, NULL);
Bastian Blankf1fc78a2005-10-30 15:00:12 -0800524static DEVICE_ATTR(modalias, 0444, modalias_show, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525static DEVICE_ATTR(online, 0644, online_show, online_store);
526extern struct device_attribute dev_attr_cmb_enable;
527static DEVICE_ATTR(availability, 0444, available_show, NULL);
528
529static struct attribute * subch_attrs[] = {
530 &dev_attr_chpids.attr,
531 &dev_attr_pimpampom.attr,
532 NULL,
533};
534
535static struct attribute_group subch_attr_group = {
536 .attrs = subch_attrs,
537};
538
Cornelia Huck529192f2006-12-08 15:55:57 +0100539struct attribute_group *subch_attr_groups[] = {
540 &subch_attr_group,
541 NULL,
542};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
544static struct attribute * ccwdev_attrs[] = {
545 &dev_attr_devtype.attr,
546 &dev_attr_cutype.attr,
Bastian Blankf1fc78a2005-10-30 15:00:12 -0800547 &dev_attr_modalias.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 &dev_attr_online.attr,
549 &dev_attr_cmb_enable.attr,
550 &dev_attr_availability.attr,
551 NULL,
552};
553
554static struct attribute_group ccwdev_attr_group = {
555 .attrs = ccwdev_attrs,
556};
557
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100558static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559device_add_files (struct device *dev)
560{
561 return sysfs_create_group(&dev->kobj, &ccwdev_attr_group);
562}
563
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100564static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565device_remove_files(struct device *dev)
566{
567 sysfs_remove_group(&dev->kobj, &ccwdev_attr_group);
568}
569
570/* this is a simple abstraction for device_register that sets the
571 * correct bus type and adds the bus specific files */
Cornelia Huck3c9da7b2006-10-27 12:39:33 +0200572static int ccw_device_register(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573{
574 struct device *dev = &cdev->dev;
575 int ret;
576
577 dev->bus = &ccw_bus_type;
578
579 if ((ret = device_add(dev)))
580 return ret;
581
582 set_bit(1, &cdev->private->registered);
583 if ((ret = device_add_files(dev))) {
584 if (test_and_clear_bit(1, &cdev->private->registered))
585 device_del(dev);
586 }
587 return ret;
588}
589
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700590struct match_data {
Cornelia Huck78964262006-10-11 15:31:38 +0200591 struct ccw_dev_id dev_id;
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700592 struct ccw_device * sibling;
593};
594
595static int
596match_devno(struct device * dev, void * data)
597{
Cornelia Huck78964262006-10-11 15:31:38 +0200598 struct match_data * d = data;
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700599 struct ccw_device * cdev;
600
601 cdev = to_ccwdev(dev);
602 if ((cdev->private->state == DEV_STATE_DISCONNECTED) &&
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100603 !ccw_device_is_orphan(cdev) &&
Cornelia Huck78964262006-10-11 15:31:38 +0200604 ccw_dev_id_is_equal(&cdev->private->dev_id, &d->dev_id) &&
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100605 (cdev != d->sibling))
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700606 return 1;
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700607 return 0;
608}
609
Cornelia Huck78964262006-10-11 15:31:38 +0200610static struct ccw_device * get_disc_ccwdev_by_dev_id(struct ccw_dev_id *dev_id,
611 struct ccw_device *sibling)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 struct device *dev;
Heiko Carstens292888c2006-08-30 14:33:35 +0200614 struct match_data data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
Cornelia Huck78964262006-10-11 15:31:38 +0200616 data.dev_id = *dev_id;
Heiko Carstens292888c2006-08-30 14:33:35 +0200617 data.sibling = sibling;
Cornelia Hucke5945b42005-10-11 08:28:59 -0700618 dev = bus_find_device(&ccw_bus_type, NULL, &data, match_devno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700620 return dev ? to_ccwdev(dev) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621}
622
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100623static int match_orphan(struct device *dev, void *data)
624{
625 struct ccw_dev_id *dev_id;
626 struct ccw_device *cdev;
627
628 dev_id = data;
629 cdev = to_ccwdev(dev);
630 return ccw_dev_id_is_equal(&cdev->private->dev_id, dev_id);
631}
632
633static struct ccw_device *
634get_orphaned_ccwdev_by_dev_id(struct channel_subsystem *css,
635 struct ccw_dev_id *dev_id)
636{
637 struct device *dev;
638
639 dev = device_find_child(&css->pseudo_subchannel->dev, dev_id,
640 match_orphan);
641
642 return dev ? to_ccwdev(dev) : NULL;
643}
644
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645static void
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100646ccw_device_add_changed(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647{
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100648 struct ccw_device_private *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 struct ccw_device *cdev;
650
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100651 priv = container_of(work, struct ccw_device_private, kick_work);
652 cdev = priv->cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 if (device_add(&cdev->dev)) {
654 put_device(&cdev->dev);
655 return;
656 }
657 set_bit(1, &cdev->private->registered);
658 if (device_add_files(&cdev->dev)) {
659 if (test_and_clear_bit(1, &cdev->private->registered))
660 device_unregister(&cdev->dev);
661 }
662}
663
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100664void ccw_device_do_unreg_rereg(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665{
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100666 struct ccw_device_private *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 struct ccw_device *cdev;
668 struct subchannel *sch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100670 priv = container_of(work, struct ccw_device_private, kick_work);
671 cdev = priv->cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 sch = to_subchannel(cdev->dev.parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 device_remove_files(&cdev->dev);
675 if (test_and_clear_bit(1, &cdev->private->registered))
676 device_del(&cdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 PREPARE_WORK(&cdev->private->kick_work,
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100678 ccw_device_add_changed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 queue_work(ccw_device_work, &cdev->private->kick_work);
680}
681
682static void
683ccw_device_release(struct device *dev)
684{
685 struct ccw_device *cdev;
686
687 cdev = to_ccwdev(dev);
688 kfree(cdev->private);
689 kfree(cdev);
690}
691
Cornelia Huck7674da72006-12-08 15:54:21 +0100692static struct ccw_device * io_subchannel_allocate_dev(struct subchannel *sch)
693{
694 struct ccw_device *cdev;
695
696 cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
697 if (cdev) {
698 cdev->private = kzalloc(sizeof(struct ccw_device_private),
699 GFP_KERNEL | GFP_DMA);
700 if (cdev->private)
701 return cdev;
702 }
703 kfree(cdev);
704 return ERR_PTR(-ENOMEM);
705}
706
707static int io_subchannel_initialize_dev(struct subchannel *sch,
708 struct ccw_device *cdev)
709{
710 cdev->private->cdev = cdev;
711 atomic_set(&cdev->private->onoff, 0);
712 cdev->dev.parent = &sch->dev;
713 cdev->dev.release = ccw_device_release;
714 INIT_LIST_HEAD(&cdev->private->kick_work.entry);
715 /* Do first half of device_register. */
716 device_initialize(&cdev->dev);
717 if (!get_device(&sch->dev)) {
718 if (cdev->dev.release)
719 cdev->dev.release(&cdev->dev);
720 return -ENODEV;
721 }
722 return 0;
723}
724
725static struct ccw_device * io_subchannel_create_ccwdev(struct subchannel *sch)
726{
727 struct ccw_device *cdev;
728 int ret;
729
730 cdev = io_subchannel_allocate_dev(sch);
731 if (!IS_ERR(cdev)) {
732 ret = io_subchannel_initialize_dev(sch, cdev);
733 if (ret) {
734 kfree(cdev);
735 cdev = ERR_PTR(ret);
736 }
737 }
738 return cdev;
739}
740
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100741static int io_subchannel_recog(struct ccw_device *, struct subchannel *);
742
743static void sch_attach_device(struct subchannel *sch,
744 struct ccw_device *cdev)
745{
746 spin_lock_irq(sch->lock);
747 sch->dev.driver_data = cdev;
748 cdev->private->schid = sch->schid;
749 cdev->ccwlock = sch->lock;
750 device_trigger_reprobe(sch);
751 spin_unlock_irq(sch->lock);
752}
753
754static void sch_attach_disconnected_device(struct subchannel *sch,
755 struct ccw_device *cdev)
756{
757 struct subchannel *other_sch;
758 int ret;
759
760 other_sch = to_subchannel(get_device(cdev->dev.parent));
761 ret = device_move(&cdev->dev, &sch->dev);
762 if (ret) {
763 CIO_MSG_EVENT(2, "Moving disconnected device 0.%x.%04x failed "
764 "(ret=%d)!\n", cdev->private->dev_id.ssid,
765 cdev->private->dev_id.devno, ret);
766 put_device(&other_sch->dev);
767 return;
768 }
769 other_sch->dev.driver_data = NULL;
770 /* No need to keep a subchannel without ccw device around. */
771 css_sch_device_unregister(other_sch);
772 put_device(&other_sch->dev);
773 sch_attach_device(sch, cdev);
774}
775
776static void sch_attach_orphaned_device(struct subchannel *sch,
777 struct ccw_device *cdev)
778{
779 int ret;
780
781 /* Try to move the ccw device to its new subchannel. */
782 ret = device_move(&cdev->dev, &sch->dev);
783 if (ret) {
784 CIO_MSG_EVENT(0, "Moving device 0.%x.%04x from orphanage "
785 "failed (ret=%d)!\n",
786 cdev->private->dev_id.ssid,
787 cdev->private->dev_id.devno, ret);
788 return;
789 }
790 sch_attach_device(sch, cdev);
791}
792
793static void sch_create_and_recog_new_device(struct subchannel *sch)
794{
795 struct ccw_device *cdev;
796
797 /* Need to allocate a new ccw device. */
798 cdev = io_subchannel_create_ccwdev(sch);
799 if (IS_ERR(cdev)) {
800 /* OK, we did everything we could... */
801 css_sch_device_unregister(sch);
802 return;
803 }
804 spin_lock_irq(sch->lock);
805 sch->dev.driver_data = cdev;
806 spin_unlock_irq(sch->lock);
807 /* Start recognition for the new ccw device. */
808 if (io_subchannel_recog(cdev, sch)) {
809 spin_lock_irq(sch->lock);
810 sch->dev.driver_data = NULL;
811 spin_unlock_irq(sch->lock);
812 if (cdev->dev.release)
813 cdev->dev.release(&cdev->dev);
814 css_sch_device_unregister(sch);
815 }
816}
817
818
819void ccw_device_move_to_orphanage(struct work_struct *work)
820{
821 struct ccw_device_private *priv;
822 struct ccw_device *cdev;
823 struct ccw_device *replacing_cdev;
824 struct subchannel *sch;
825 int ret;
826 struct channel_subsystem *css;
827 struct ccw_dev_id dev_id;
828
829 priv = container_of(work, struct ccw_device_private, kick_work);
830 cdev = priv->cdev;
831 sch = to_subchannel(cdev->dev.parent);
832 css = to_css(sch->dev.parent);
833 dev_id.devno = sch->schib.pmcw.dev;
834 dev_id.ssid = sch->schid.ssid;
835
836 /*
837 * Move the orphaned ccw device to the orphanage so the replacing
838 * ccw device can take its place on the subchannel.
839 */
840 ret = device_move(&cdev->dev, &css->pseudo_subchannel->dev);
841 if (ret) {
842 CIO_MSG_EVENT(0, "Moving device 0.%x.%04x to orphanage failed "
843 "(ret=%d)!\n", cdev->private->dev_id.ssid,
844 cdev->private->dev_id.devno, ret);
845 return;
846 }
847 cdev->ccwlock = css->pseudo_subchannel->lock;
848 /*
849 * Search for the replacing ccw device
850 * - among the disconnected devices
851 * - in the orphanage
852 */
853 replacing_cdev = get_disc_ccwdev_by_dev_id(&dev_id, cdev);
854 if (replacing_cdev) {
855 sch_attach_disconnected_device(sch, replacing_cdev);
856 return;
857 }
858 replacing_cdev = get_orphaned_ccwdev_by_dev_id(css, &dev_id);
859 if (replacing_cdev) {
860 sch_attach_orphaned_device(sch, replacing_cdev);
861 return;
862 }
863 sch_create_and_recog_new_device(sch);
864}
865
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866/*
867 * Register recognized device.
868 */
869static void
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100870io_subchannel_register(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871{
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100872 struct ccw_device_private *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 struct ccw_device *cdev;
874 struct subchannel *sch;
875 int ret;
876 unsigned long flags;
877
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100878 priv = container_of(work, struct ccw_device_private, kick_work);
879 cdev = priv->cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 sch = to_subchannel(cdev->dev.parent);
881
Cornelia Huck47af5512006-12-04 15:41:07 +0100882 /*
883 * io_subchannel_register() will also be called after device
884 * recognition has been done for a boxed device (which will already
885 * be registered). We need to reprobe since we may now have sense id
886 * information.
887 */
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700888 if (klist_node_attached(&cdev->dev.knode_parent)) {
Cornelia Huck47af5512006-12-04 15:41:07 +0100889 if (!cdev->drv) {
890 ret = device_reprobe(&cdev->dev);
891 if (ret)
892 /* We can't do much here. */
893 dev_info(&cdev->dev, "device_reprobe() returned"
894 " %d\n", ret);
895 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 goto out;
897 }
898 /* make it known to the system */
899 ret = ccw_device_register(cdev);
900 if (ret) {
901 printk (KERN_WARNING "%s: could not register %s\n",
902 __func__, cdev->dev.bus_id);
903 put_device(&cdev->dev);
Cornelia Huck2ec22982006-12-08 15:54:26 +0100904 spin_lock_irqsave(sch->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 sch->dev.driver_data = NULL;
Cornelia Huck2ec22982006-12-08 15:54:26 +0100906 spin_unlock_irqrestore(sch->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 kfree (cdev->private);
908 kfree (cdev);
909 put_device(&sch->dev);
910 if (atomic_dec_and_test(&ccw_device_init_count))
911 wake_up(&ccw_device_init_wq);
912 return;
913 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 put_device(&cdev->dev);
915out:
916 cdev->private->flags.recog_done = 1;
917 put_device(&sch->dev);
918 wake_up(&cdev->private->wait_q);
919 if (atomic_dec_and_test(&ccw_device_init_count))
920 wake_up(&ccw_device_init_wq);
921}
922
923void
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100924ccw_device_call_sch_unregister(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925{
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100926 struct ccw_device_private *priv;
927 struct ccw_device *cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 struct subchannel *sch;
929
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100930 priv = container_of(work, struct ccw_device_private, kick_work);
931 cdev = priv->cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 sch = to_subchannel(cdev->dev.parent);
Cornelia Huck6ab48792006-07-12 16:39:50 +0200933 css_sch_device_unregister(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 /* Reset intparm to zeroes. */
935 sch->schib.pmcw.intparm = 0;
936 cio_modify(sch);
937 put_device(&cdev->dev);
938 put_device(&sch->dev);
939}
940
941/*
942 * subchannel recognition done. Called from the state machine.
943 */
944void
945io_subchannel_recog_done(struct ccw_device *cdev)
946{
947 struct subchannel *sch;
948
949 if (css_init_done == 0) {
950 cdev->private->flags.recog_done = 1;
951 return;
952 }
953 switch (cdev->private->state) {
954 case DEV_STATE_NOT_OPER:
955 cdev->private->flags.recog_done = 1;
956 /* Remove device found not operational. */
957 if (!get_device(&cdev->dev))
958 break;
959 sch = to_subchannel(cdev->dev.parent);
960 PREPARE_WORK(&cdev->private->kick_work,
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100961 ccw_device_call_sch_unregister);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 queue_work(slow_path_wq, &cdev->private->kick_work);
963 if (atomic_dec_and_test(&ccw_device_init_count))
964 wake_up(&ccw_device_init_wq);
965 break;
966 case DEV_STATE_BOXED:
967 /* Device did not respond in time. */
968 case DEV_STATE_OFFLINE:
969 /*
970 * We can't register the device in interrupt context so
971 * we schedule a work item.
972 */
973 if (!get_device(&cdev->dev))
974 break;
975 PREPARE_WORK(&cdev->private->kick_work,
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100976 io_subchannel_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 queue_work(slow_path_wq, &cdev->private->kick_work);
978 break;
979 }
980}
981
982static int
983io_subchannel_recog(struct ccw_device *cdev, struct subchannel *sch)
984{
985 int rc;
986 struct ccw_device_private *priv;
987
988 sch->dev.driver_data = cdev;
989 sch->driver = &io_subchannel_driver;
Cornelia Huck2ec22982006-12-08 15:54:26 +0100990 cdev->ccwlock = sch->lock;
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800991
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 /* Init private data. */
993 priv = cdev->private;
Cornelia Huck78964262006-10-11 15:31:38 +0200994 priv->dev_id.devno = sch->schib.pmcw.dev;
995 priv->dev_id.ssid = sch->schid.ssid;
996 priv->schid = sch->schid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 priv->state = DEV_STATE_NOT_OPER;
998 INIT_LIST_HEAD(&priv->cmb_list);
999 init_waitqueue_head(&priv->wait_q);
1000 init_timer(&priv->timer);
1001
1002 /* Set an initial name for the device. */
Cornelia Huckfb6958a2006-01-06 00:19:25 -08001003 snprintf (cdev->dev.bus_id, BUS_ID_SIZE, "0.%x.%04x",
1004 sch->schid.ssid, sch->schib.pmcw.dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005
1006 /* Increase counter of devices currently in recognition. */
1007 atomic_inc(&ccw_device_init_count);
1008
1009 /* Start async. device sensing. */
Cornelia Huck2ec22982006-12-08 15:54:26 +01001010 spin_lock_irq(sch->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 rc = ccw_device_recognition(cdev);
Cornelia Huck2ec22982006-12-08 15:54:26 +01001012 spin_unlock_irq(sch->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 if (rc) {
1014 if (atomic_dec_and_test(&ccw_device_init_count))
1015 wake_up(&ccw_device_init_wq);
1016 }
1017 return rc;
1018}
1019
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +01001020static void ccw_device_move_to_sch(struct work_struct *work)
1021{
1022 struct ccw_device_private *priv;
1023 int rc;
1024 struct subchannel *sch;
1025 struct ccw_device *cdev;
1026 struct subchannel *former_parent;
1027
1028 priv = container_of(work, struct ccw_device_private, kick_work);
1029 sch = priv->sch;
1030 cdev = priv->cdev;
1031 former_parent = ccw_device_is_orphan(cdev) ?
1032 NULL : to_subchannel(get_device(cdev->dev.parent));
1033 mutex_lock(&sch->reg_mutex);
1034 /* Try to move the ccw device to its new subchannel. */
1035 rc = device_move(&cdev->dev, &sch->dev);
1036 mutex_unlock(&sch->reg_mutex);
1037 if (rc) {
1038 CIO_MSG_EVENT(2, "Moving device 0.%x.%04x to subchannel "
1039 "0.%x.%04x failed (ret=%d)!\n",
1040 cdev->private->dev_id.ssid,
1041 cdev->private->dev_id.devno, sch->schid.ssid,
1042 sch->schid.sch_no, rc);
1043 css_sch_device_unregister(sch);
1044 goto out;
1045 }
1046 if (former_parent) {
1047 spin_lock_irq(former_parent->lock);
1048 former_parent->dev.driver_data = NULL;
1049 spin_unlock_irq(former_parent->lock);
1050 css_sch_device_unregister(former_parent);
1051 /* Reset intparm to zeroes. */
1052 former_parent->schib.pmcw.intparm = 0;
1053 cio_modify(former_parent);
1054 }
1055 sch_attach_device(sch, cdev);
1056out:
1057 if (former_parent)
1058 put_device(&former_parent->dev);
1059 put_device(&cdev->dev);
1060}
1061
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062static int
Cornelia Huck8bbace72006-01-11 10:56:22 +01001063io_subchannel_probe (struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 struct ccw_device *cdev;
1066 int rc;
1067 unsigned long flags;
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +01001068 struct ccw_dev_id dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 if (sch->dev.driver_data) {
1071 /*
1072 * This subchannel already has an associated ccw_device.
1073 * Register it and exit. This happens for all early
1074 * device, e.g. the console.
1075 */
1076 cdev = sch->dev.driver_data;
1077 device_initialize(&cdev->dev);
1078 ccw_device_register(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 /*
1080 * Check if the device is already online. If it is
1081 * the reference count needs to be corrected
1082 * (see ccw_device_online and css_init_done for the
1083 * ugly details).
1084 */
1085 if (cdev->private->state != DEV_STATE_NOT_OPER &&
1086 cdev->private->state != DEV_STATE_OFFLINE &&
1087 cdev->private->state != DEV_STATE_BOXED)
1088 get_device(&cdev->dev);
1089 return 0;
1090 }
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +01001091 /*
1092 * First check if a fitting device may be found amongst the
1093 * disconnected devices or in the orphanage.
1094 */
1095 dev_id.devno = sch->schib.pmcw.dev;
1096 dev_id.ssid = sch->schid.ssid;
1097 cdev = get_disc_ccwdev_by_dev_id(&dev_id, NULL);
1098 if (!cdev)
1099 cdev = get_orphaned_ccwdev_by_dev_id(to_css(sch->dev.parent),
1100 &dev_id);
1101 if (cdev) {
1102 /*
1103 * Schedule moving the device until when we have a registered
1104 * subchannel to move to and succeed the probe. We can
1105 * unregister later again, when the probe is through.
1106 */
1107 cdev->private->sch = sch;
1108 PREPARE_WORK(&cdev->private->kick_work,
1109 ccw_device_move_to_sch);
1110 queue_work(slow_path_wq, &cdev->private->kick_work);
1111 return 0;
1112 }
Cornelia Huck7674da72006-12-08 15:54:21 +01001113 cdev = io_subchannel_create_ccwdev(sch);
1114 if (IS_ERR(cdev))
1115 return PTR_ERR(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116
Cornelia Huck8bbace72006-01-11 10:56:22 +01001117 rc = io_subchannel_recog(cdev, sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 if (rc) {
Cornelia Huck2ec22982006-12-08 15:54:26 +01001119 spin_lock_irqsave(sch->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 sch->dev.driver_data = NULL;
Cornelia Huck2ec22982006-12-08 15:54:26 +01001121 spin_unlock_irqrestore(sch->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 if (cdev->dev.release)
1123 cdev->dev.release(&cdev->dev);
1124 }
1125
1126 return rc;
1127}
1128
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129static int
Cornelia Huck8bbace72006-01-11 10:56:22 +01001130io_subchannel_remove (struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131{
1132 struct ccw_device *cdev;
1133 unsigned long flags;
1134
Cornelia Huck8bbace72006-01-11 10:56:22 +01001135 if (!sch->dev.driver_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 return 0;
Cornelia Huck8bbace72006-01-11 10:56:22 +01001137 cdev = sch->dev.driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 /* Set ccw device to not operational and drop reference. */
1139 spin_lock_irqsave(cdev->ccwlock, flags);
Cornelia Huck8bbace72006-01-11 10:56:22 +01001140 sch->dev.driver_data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 cdev->private->state = DEV_STATE_NOT_OPER;
1142 spin_unlock_irqrestore(cdev->ccwlock, flags);
1143 /*
1144 * Put unregistration on workqueue to avoid livelocks on the css bus
1145 * semaphore.
1146 */
1147 if (get_device(&cdev->dev)) {
1148 PREPARE_WORK(&cdev->private->kick_work,
Martin Schwidefskyc1637532006-12-08 15:53:57 +01001149 ccw_device_unregister);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 queue_work(ccw_device_work, &cdev->private->kick_work);
1151 }
1152 return 0;
1153}
1154
1155static int
1156io_subchannel_notify(struct device *dev, int event)
1157{
1158 struct ccw_device *cdev;
1159
1160 cdev = dev->driver_data;
1161 if (!cdev)
1162 return 0;
1163 if (!cdev->drv)
1164 return 0;
1165 if (!cdev->online)
1166 return 0;
1167 return cdev->drv->notify ? cdev->drv->notify(cdev, event) : 0;
1168}
1169
1170static void
1171io_subchannel_verify(struct device *dev)
1172{
1173 struct ccw_device *cdev;
1174
1175 cdev = dev->driver_data;
1176 if (cdev)
1177 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1178}
1179
1180static void
1181io_subchannel_ioterm(struct device *dev)
1182{
1183 struct ccw_device *cdev;
1184
1185 cdev = dev->driver_data;
1186 if (!cdev)
1187 return;
Cornelia Huckd23861f2006-12-04 15:41:04 +01001188 /* Internal I/O will be retried by the interrupt handler. */
1189 if (cdev->private->flags.intretry)
1190 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 cdev->private->state = DEV_STATE_CLEAR_VERIFY;
1192 if (cdev->handler)
1193 cdev->handler(cdev, cdev->private->intparm,
1194 ERR_PTR(-EIO));
1195}
1196
1197static void
Cornelia Huck8bbace72006-01-11 10:56:22 +01001198io_subchannel_shutdown(struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 struct ccw_device *cdev;
1201 int ret;
1202
Cornelia Huck8bbace72006-01-11 10:56:22 +01001203 cdev = sch->dev.driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204
Cornelia Hucka8237fc2006-01-06 00:19:21 -08001205 if (cio_is_console(sch->schid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 return;
1207 if (!sch->schib.pmcw.ena)
1208 /* Nothing to do. */
1209 return;
1210 ret = cio_disable_subchannel(sch);
1211 if (ret != -EBUSY)
1212 /* Subchannel is disabled, we're done. */
1213 return;
1214 cdev->private->state = DEV_STATE_QUIESCE;
1215 if (cdev->handler)
1216 cdev->handler(cdev, cdev->private->intparm,
1217 ERR_PTR(-EIO));
1218 ret = ccw_device_cancel_halt_clear(cdev);
1219 if (ret == -EBUSY) {
1220 ccw_device_set_timeout(cdev, HZ/10);
1221 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
1222 }
1223 cio_disable_subchannel(sch);
1224}
1225
1226#ifdef CONFIG_CCW_CONSOLE
1227static struct ccw_device console_cdev;
1228static struct ccw_device_private console_private;
1229static int console_cdev_in_use;
1230
Cornelia Huck2ec22982006-12-08 15:54:26 +01001231static DEFINE_SPINLOCK(ccw_console_lock);
1232
1233spinlock_t * cio_get_console_lock(void)
1234{
1235 return &ccw_console_lock;
1236}
1237
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238static int
1239ccw_device_console_enable (struct ccw_device *cdev, struct subchannel *sch)
1240{
1241 int rc;
1242
1243 /* Initialize the ccw_device structure. */
Heiko Carstens292888c2006-08-30 14:33:35 +02001244 cdev->dev.parent= &sch->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 rc = io_subchannel_recog(cdev, sch);
1246 if (rc)
1247 return rc;
1248
1249 /* Now wait for the async. recognition to come to an end. */
1250 spin_lock_irq(cdev->ccwlock);
1251 while (!dev_fsm_final_state(cdev))
1252 wait_cons_dev();
1253 rc = -EIO;
1254 if (cdev->private->state != DEV_STATE_OFFLINE)
1255 goto out_unlock;
1256 ccw_device_online(cdev);
1257 while (!dev_fsm_final_state(cdev))
1258 wait_cons_dev();
1259 if (cdev->private->state != DEV_STATE_ONLINE)
1260 goto out_unlock;
1261 rc = 0;
1262out_unlock:
1263 spin_unlock_irq(cdev->ccwlock);
1264 return 0;
1265}
1266
1267struct ccw_device *
1268ccw_device_probe_console(void)
1269{
1270 struct subchannel *sch;
1271 int ret;
1272
1273 if (xchg(&console_cdev_in_use, 1) != 0)
Peter Oberparleiter600b5d12006-02-01 03:06:35 -08001274 return ERR_PTR(-EBUSY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 sch = cio_probe_console();
1276 if (IS_ERR(sch)) {
1277 console_cdev_in_use = 0;
1278 return (void *) sch;
1279 }
1280 memset(&console_cdev, 0, sizeof(struct ccw_device));
1281 memset(&console_private, 0, sizeof(struct ccw_device_private));
1282 console_cdev.private = &console_private;
Martin Schwidefskyc1637532006-12-08 15:53:57 +01001283 console_private.cdev = &console_cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 ret = ccw_device_console_enable(&console_cdev, sch);
1285 if (ret) {
1286 cio_release_console();
1287 console_cdev_in_use = 0;
1288 return ERR_PTR(ret);
1289 }
1290 console_cdev.online = 1;
1291 return &console_cdev;
1292}
1293#endif
1294
1295/*
1296 * get ccw_device matching the busid, but only if owned by cdrv
1297 */
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001298static int
1299__ccwdev_check_busid(struct device *dev, void *id)
1300{
1301 char *bus_id;
1302
Cornelia Huck12975ae2006-10-11 15:31:47 +02001303 bus_id = id;
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001304
1305 return (strncmp(bus_id, dev->bus_id, BUS_ID_SIZE) == 0);
1306}
1307
1308
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309struct ccw_device *
1310get_ccwdev_by_busid(struct ccw_driver *cdrv, const char *bus_id)
1311{
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001312 struct device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 struct device_driver *drv;
1314
1315 drv = get_driver(&cdrv->driver);
1316 if (!drv)
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001317 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001319 dev = driver_find_device(drv, NULL, (void *)bus_id,
1320 __ccwdev_check_busid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 put_driver(drv);
1322
Heiko Carstensd2c993d2006-07-12 16:41:55 +02001323 return dev ? to_ccwdev(dev) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324}
1325
1326/************************** device driver handling ************************/
1327
1328/* This is the implementation of the ccw_driver class. The probe, remove
1329 * and release methods are initially very similar to the device_driver
1330 * implementations, with the difference that they have ccw_device
1331 * arguments.
1332 *
1333 * A ccw driver also contains the information that is needed for
1334 * device matching.
1335 */
1336static int
1337ccw_device_probe (struct device *dev)
1338{
1339 struct ccw_device *cdev = to_ccwdev(dev);
1340 struct ccw_driver *cdrv = to_ccwdrv(dev->driver);
1341 int ret;
1342
1343 cdev->drv = cdrv; /* to let the driver call _set_online */
1344
1345 ret = cdrv->probe ? cdrv->probe(cdev) : -ENODEV;
1346
1347 if (ret) {
Heiko Carstensd2c993d2006-07-12 16:41:55 +02001348 cdev->drv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 return ret;
1350 }
1351
1352 return 0;
1353}
1354
1355static int
1356ccw_device_remove (struct device *dev)
1357{
1358 struct ccw_device *cdev = to_ccwdev(dev);
1359 struct ccw_driver *cdrv = cdev->drv;
1360 int ret;
1361
1362 pr_debug("removing device %s\n", cdev->dev.bus_id);
1363 if (cdrv->remove)
1364 cdrv->remove(cdev);
1365 if (cdev->online) {
1366 cdev->online = 0;
1367 spin_lock_irq(cdev->ccwlock);
1368 ret = ccw_device_offline(cdev);
1369 spin_unlock_irq(cdev->ccwlock);
1370 if (ret == 0)
1371 wait_event(cdev->private->wait_q,
1372 dev_fsm_final_state(cdev));
1373 else
1374 //FIXME: we can't fail!
1375 pr_debug("ccw_device_offline returned %d, device %s\n",
1376 ret, cdev->dev.bus_id);
1377 }
1378 ccw_device_set_timeout(cdev, 0);
Heiko Carstensd2c993d2006-07-12 16:41:55 +02001379 cdev->drv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 return 0;
1381}
1382
Cornelia Huck8bbace72006-01-11 10:56:22 +01001383struct bus_type ccw_bus_type = {
1384 .name = "ccw",
1385 .match = ccw_bus_match,
1386 .uevent = ccw_uevent,
1387 .probe = ccw_device_probe,
1388 .remove = ccw_device_remove,
1389};
1390
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391int
1392ccw_driver_register (struct ccw_driver *cdriver)
1393{
1394 struct device_driver *drv = &cdriver->driver;
1395
1396 drv->bus = &ccw_bus_type;
1397 drv->name = cdriver->name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398
1399 return driver_register(drv);
1400}
1401
1402void
1403ccw_driver_unregister (struct ccw_driver *cdriver)
1404{
1405 driver_unregister(&cdriver->driver);
1406}
1407
Cornelia Hucka8237fc2006-01-06 00:19:21 -08001408/* Helper func for qdio. */
1409struct subchannel_id
1410ccw_device_get_subchannel_id(struct ccw_device *cdev)
1411{
1412 struct subchannel *sch;
1413
1414 sch = to_subchannel(cdev->dev.parent);
1415 return sch->schid;
1416}
1417
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418MODULE_LICENSE("GPL");
1419EXPORT_SYMBOL(ccw_device_set_online);
1420EXPORT_SYMBOL(ccw_device_set_offline);
1421EXPORT_SYMBOL(ccw_driver_register);
1422EXPORT_SYMBOL(ccw_driver_unregister);
1423EXPORT_SYMBOL(get_ccwdev_by_busid);
1424EXPORT_SYMBOL(ccw_bus_type);
1425EXPORT_SYMBOL(ccw_device_work);
1426EXPORT_SYMBOL(ccw_device_notify_work);
Cornelia Hucka8237fc2006-01-06 00:19:21 -08001427EXPORT_SYMBOL_GPL(ccw_device_get_subchannel_id);