blob: afc4e88551ad2ee5437a1622143d95c371522659 [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 */
11#include <linux/config.h>
12#include <linux/module.h>
13#include <linux/init.h>
14#include <linux/spinlock.h>
15#include <linux/errno.h>
16#include <linux/err.h>
17#include <linux/slab.h>
18#include <linux/list.h>
19#include <linux/device.h>
20#include <linux/workqueue.h>
21
22#include <asm/ccwdev.h>
23#include <asm/cio.h>
Tim Schmielau4e57b682005-10-30 15:03:48 -080024#include <asm/param.h> /* HZ */
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
26#include "cio.h"
27#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
56/*
57 * Hotplugging interface for ccw devices.
58 * Heavily modeled on pci and usb hotplug.
59 */
60static int
Paul Jackson6d20b032005-11-25 20:04:26 -080061ccw_uevent (struct device *dev, char **envp, int num_envp,
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 char *buffer, int buffer_size)
63{
64 struct ccw_device *cdev = to_ccwdev(dev);
65 int i = 0;
66 int length = 0;
67
68 if (!cdev)
69 return -ENODEV;
70
71 /* what we want to pass to /sbin/hotplug */
72
73 envp[i++] = buffer;
74 length += scnprintf(buffer, buffer_size - length, "CU_TYPE=%04X",
75 cdev->id.cu_type);
76 if ((buffer_size - length <= 0) || (i >= num_envp))
77 return -ENOMEM;
78 ++length;
79 buffer += length;
80
81 envp[i++] = buffer;
82 length += scnprintf(buffer, buffer_size - length, "CU_MODEL=%02X",
83 cdev->id.cu_model);
84 if ((buffer_size - length <= 0) || (i >= num_envp))
85 return -ENOMEM;
86 ++length;
87 buffer += length;
88
89 /* The next two can be zero, that's ok for us */
90 envp[i++] = buffer;
91 length += scnprintf(buffer, buffer_size - length, "DEV_TYPE=%04X",
92 cdev->id.dev_type);
93 if ((buffer_size - length <= 0) || (i >= num_envp))
94 return -ENOMEM;
95 ++length;
96 buffer += length;
97
98 envp[i++] = buffer;
99 length += scnprintf(buffer, buffer_size - length, "DEV_MODEL=%02X",
100 cdev->id.dev_model);
101 if ((buffer_size - length <= 0) || (i >= num_envp))
102 return -ENOMEM;
103
104 envp[i] = 0;
105
106 return 0;
107}
108
Cornelia Huck8bbace72006-01-11 10:56:22 +0100109struct bus_type ccw_bus_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
Cornelia Huck8bbace72006-01-11 10:56:22 +0100111static int io_subchannel_probe (struct subchannel *);
112static int io_subchannel_remove (struct subchannel *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113void io_subchannel_irq (struct device *);
114static int io_subchannel_notify(struct device *, int);
115static void io_subchannel_verify(struct device *);
116static void io_subchannel_ioterm(struct device *);
Cornelia Huck8bbace72006-01-11 10:56:22 +0100117static void io_subchannel_shutdown(struct subchannel *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
119struct css_driver io_subchannel_driver = {
120 .subchannel_type = SUBCHANNEL_TYPE_IO,
121 .drv = {
122 .name = "io_subchannel",
123 .bus = &css_bus_type,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 },
125 .irq = io_subchannel_irq,
126 .notify = io_subchannel_notify,
127 .verify = io_subchannel_verify,
128 .termination = io_subchannel_ioterm,
Cornelia Huck8bbace72006-01-11 10:56:22 +0100129 .probe = io_subchannel_probe,
130 .remove = io_subchannel_remove,
131 .shutdown = io_subchannel_shutdown,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132};
133
134struct workqueue_struct *ccw_device_work;
135struct workqueue_struct *ccw_device_notify_work;
136static wait_queue_head_t ccw_device_init_wq;
137static atomic_t ccw_device_init_count;
138
139static int __init
140init_ccw_bus_type (void)
141{
142 int ret;
143
144 init_waitqueue_head(&ccw_device_init_wq);
145 atomic_set(&ccw_device_init_count, 0);
146
147 ccw_device_work = create_singlethread_workqueue("cio");
148 if (!ccw_device_work)
149 return -ENOMEM; /* FIXME: better errno ? */
150 ccw_device_notify_work = create_singlethread_workqueue("cio_notify");
151 if (!ccw_device_notify_work) {
152 ret = -ENOMEM; /* FIXME: better errno ? */
153 goto out_err;
154 }
155 slow_path_wq = create_singlethread_workqueue("kslowcrw");
156 if (!slow_path_wq) {
157 ret = -ENOMEM; /* FIXME: better errno ? */
158 goto out_err;
159 }
160 if ((ret = bus_register (&ccw_bus_type)))
161 goto out_err;
162
163 if ((ret = driver_register(&io_subchannel_driver.drv)))
164 goto out_err;
165
166 wait_event(ccw_device_init_wq,
167 atomic_read(&ccw_device_init_count) == 0);
168 flush_workqueue(ccw_device_work);
169 return 0;
170out_err:
171 if (ccw_device_work)
172 destroy_workqueue(ccw_device_work);
173 if (ccw_device_notify_work)
174 destroy_workqueue(ccw_device_notify_work);
175 if (slow_path_wq)
176 destroy_workqueue(slow_path_wq);
177 return ret;
178}
179
180static void __exit
181cleanup_ccw_bus_type (void)
182{
183 driver_unregister(&io_subchannel_driver.drv);
184 bus_unregister(&ccw_bus_type);
185 destroy_workqueue(ccw_device_notify_work);
186 destroy_workqueue(ccw_device_work);
187}
188
189subsys_initcall(init_ccw_bus_type);
190module_exit(cleanup_ccw_bus_type);
191
192/************************ device handling **************************/
193
194/*
195 * A ccw_device has some interfaces in sysfs in addition to the
196 * standard ones.
197 * The following entries are designed to export the information which
198 * resided in 2.4 in /proc/subchannels. Subchannel and device number
199 * are obvious, so they don't have an entry :)
200 * TODO: Split chpids and pimpampom up? Where is "in use" in the tree?
201 */
202static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400203chpids_show (struct device * dev, struct device_attribute *attr, char * buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204{
205 struct subchannel *sch = to_subchannel(dev);
206 struct ssd_info *ssd = &sch->ssd_info;
207 ssize_t ret = 0;
208 int chp;
209
210 for (chp = 0; chp < 8; chp++)
211 ret += sprintf (buf+ret, "%02x ", ssd->chpid[chp]);
212
213 ret += sprintf (buf+ret, "\n");
214 return min((ssize_t)PAGE_SIZE, ret);
215}
216
217static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400218pimpampom_show (struct device * dev, struct device_attribute *attr, char * buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219{
220 struct subchannel *sch = to_subchannel(dev);
221 struct pmcw *pmcw = &sch->schib.pmcw;
222
223 return sprintf (buf, "%02x %02x %02x\n",
224 pmcw->pim, pmcw->pam, pmcw->pom);
225}
226
227static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400228devtype_show (struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229{
230 struct ccw_device *cdev = to_ccwdev(dev);
231 struct ccw_device_id *id = &(cdev->id);
232
233 if (id->dev_type != 0)
234 return sprintf(buf, "%04x/%02x\n",
235 id->dev_type, id->dev_model);
236 else
237 return sprintf(buf, "n/a\n");
238}
239
240static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400241cutype_show (struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242{
243 struct ccw_device *cdev = to_ccwdev(dev);
244 struct ccw_device_id *id = &(cdev->id);
245
246 return sprintf(buf, "%04x/%02x\n",
247 id->cu_type, id->cu_model);
248}
249
250static ssize_t
Bastian Blankf1fc78a2005-10-30 15:00:12 -0800251modalias_show (struct device *dev, struct device_attribute *attr, char *buf)
252{
253 struct ccw_device *cdev = to_ccwdev(dev);
254 struct ccw_device_id *id = &(cdev->id);
255 int ret;
256
Cornelia Huckb1969fa2006-02-01 03:06:39 -0800257 ret = sprintf(buf, "ccw:t%04Xm%02X",
Bastian Blankf1fc78a2005-10-30 15:00:12 -0800258 id->cu_type, id->cu_model);
259 if (id->dev_type != 0)
260 ret += sprintf(buf + ret, "dt%04Xdm%02X\n",
261 id->dev_type, id->dev_model);
262 else
263 ret += sprintf(buf + ret, "dtdm\n");
264 return ret;
265}
266
267static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400268online_show (struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269{
270 struct ccw_device *cdev = to_ccwdev(dev);
271
272 return sprintf(buf, cdev->online ? "1\n" : "0\n");
273}
274
275static void
276ccw_device_remove_disconnected(struct ccw_device *cdev)
277{
278 struct subchannel *sch;
279 /*
280 * Forced offline in disconnected state means
281 * 'throw away device'.
282 */
283 sch = to_subchannel(cdev->dev.parent);
284 device_unregister(&sch->dev);
285 /* Reset intparm to zeroes. */
286 sch->schib.pmcw.intparm = 0;
287 cio_modify(sch);
288 put_device(&sch->dev);
289}
290
291int
292ccw_device_set_offline(struct ccw_device *cdev)
293{
294 int ret;
295
296 if (!cdev)
297 return -ENODEV;
298 if (!cdev->online || !cdev->drv)
299 return -EINVAL;
300
301 if (cdev->drv->set_offline) {
302 ret = cdev->drv->set_offline(cdev);
303 if (ret != 0)
304 return ret;
305 }
306 cdev->online = 0;
307 spin_lock_irq(cdev->ccwlock);
308 ret = ccw_device_offline(cdev);
309 if (ret == -ENODEV) {
310 if (cdev->private->state != DEV_STATE_NOT_OPER) {
311 cdev->private->state = DEV_STATE_OFFLINE;
312 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
313 }
314 spin_unlock_irq(cdev->ccwlock);
315 return ret;
316 }
317 spin_unlock_irq(cdev->ccwlock);
318 if (ret == 0)
319 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
320 else {
321 pr_debug("ccw_device_offline returned %d, device %s\n",
322 ret, cdev->dev.bus_id);
323 cdev->online = 1;
324 }
325 return ret;
326}
327
328int
329ccw_device_set_online(struct ccw_device *cdev)
330{
331 int ret;
332
333 if (!cdev)
334 return -ENODEV;
335 if (cdev->online || !cdev->drv)
336 return -EINVAL;
337
338 spin_lock_irq(cdev->ccwlock);
339 ret = ccw_device_online(cdev);
340 spin_unlock_irq(cdev->ccwlock);
341 if (ret == 0)
342 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
343 else {
344 pr_debug("ccw_device_online returned %d, device %s\n",
345 ret, cdev->dev.bus_id);
346 return ret;
347 }
348 if (cdev->private->state != DEV_STATE_ONLINE)
349 return -ENODEV;
350 if (!cdev->drv->set_online || cdev->drv->set_online(cdev) == 0) {
351 cdev->online = 1;
352 return 0;
353 }
354 spin_lock_irq(cdev->ccwlock);
355 ret = ccw_device_offline(cdev);
356 spin_unlock_irq(cdev->ccwlock);
357 if (ret == 0)
358 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
359 else
360 pr_debug("ccw_device_offline returned %d, device %s\n",
361 ret, cdev->dev.bus_id);
Cornelia Huck6cadb782006-02-17 13:52:49 -0800362 return (ret == 0) ? -ENODEV : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363}
364
365static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400366online_store (struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367{
368 struct ccw_device *cdev = to_ccwdev(dev);
369 int i, force, ret;
370 char *tmp;
371
Martin Schwidefsky973bd992006-01-06 00:19:07 -0800372 if (atomic_cmpxchg(&cdev->private->onoff, 0, 1) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 return -EAGAIN;
374
375 if (cdev->drv && !try_module_get(cdev->drv->owner)) {
376 atomic_set(&cdev->private->onoff, 0);
377 return -EINVAL;
378 }
379 if (!strncmp(buf, "force\n", count)) {
380 force = 1;
381 i = 1;
382 } else {
383 force = 0;
384 i = simple_strtoul(buf, &tmp, 16);
385 }
386 if (i == 1) {
387 /* Do device recognition, if needed. */
388 if (cdev->id.cu_type == 0) {
389 ret = ccw_device_recognition(cdev);
390 if (ret) {
391 printk(KERN_WARNING"Couldn't start recognition "
392 "for device %s (ret=%d)\n",
393 cdev->dev.bus_id, ret);
394 goto out;
395 }
396 wait_event(cdev->private->wait_q,
397 cdev->private->flags.recog_done);
398 }
399 if (cdev->drv && cdev->drv->set_online)
400 ccw_device_set_online(cdev);
401 } else if (i == 0) {
402 if (cdev->private->state == DEV_STATE_DISCONNECTED)
403 ccw_device_remove_disconnected(cdev);
404 else if (cdev->drv && cdev->drv->set_offline)
405 ccw_device_set_offline(cdev);
406 }
407 if (force && cdev->private->state == DEV_STATE_BOXED) {
408 ret = ccw_device_stlck(cdev);
409 if (ret) {
410 printk(KERN_WARNING"ccw_device_stlck for device %s "
411 "returned %d!\n", cdev->dev.bus_id, ret);
412 goto out;
413 }
414 /* Do device recognition, if needed. */
415 if (cdev->id.cu_type == 0) {
416 cdev->private->state = DEV_STATE_NOT_OPER;
417 ret = ccw_device_recognition(cdev);
418 if (ret) {
419 printk(KERN_WARNING"Couldn't start recognition "
420 "for device %s (ret=%d)\n",
421 cdev->dev.bus_id, ret);
422 goto out;
423 }
424 wait_event(cdev->private->wait_q,
425 cdev->private->flags.recog_done);
426 }
427 if (cdev->drv && cdev->drv->set_online)
428 ccw_device_set_online(cdev);
429 }
430 out:
431 if (cdev->drv)
432 module_put(cdev->drv->owner);
433 atomic_set(&cdev->private->onoff, 0);
434 return count;
435}
436
437static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400438available_show (struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439{
440 struct ccw_device *cdev = to_ccwdev(dev);
441 struct subchannel *sch;
442
443 switch (cdev->private->state) {
444 case DEV_STATE_BOXED:
445 return sprintf(buf, "boxed\n");
446 case DEV_STATE_DISCONNECTED:
447 case DEV_STATE_DISCONNECTED_SENSE_ID:
448 case DEV_STATE_NOT_OPER:
449 sch = to_subchannel(dev->parent);
450 if (!sch->lpm)
451 return sprintf(buf, "no path\n");
452 else
453 return sprintf(buf, "no device\n");
454 default:
455 /* All other states considered fine. */
456 return sprintf(buf, "good\n");
457 }
458}
459
460static DEVICE_ATTR(chpids, 0444, chpids_show, NULL);
461static DEVICE_ATTR(pimpampom, 0444, pimpampom_show, NULL);
462static DEVICE_ATTR(devtype, 0444, devtype_show, NULL);
463static DEVICE_ATTR(cutype, 0444, cutype_show, NULL);
Bastian Blankf1fc78a2005-10-30 15:00:12 -0800464static DEVICE_ATTR(modalias, 0444, modalias_show, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465static DEVICE_ATTR(online, 0644, online_show, online_store);
466extern struct device_attribute dev_attr_cmb_enable;
467static DEVICE_ATTR(availability, 0444, available_show, NULL);
468
469static struct attribute * subch_attrs[] = {
470 &dev_attr_chpids.attr,
471 &dev_attr_pimpampom.attr,
472 NULL,
473};
474
475static struct attribute_group subch_attr_group = {
476 .attrs = subch_attrs,
477};
478
479static inline int
480subchannel_add_files (struct device *dev)
481{
482 return sysfs_create_group(&dev->kobj, &subch_attr_group);
483}
484
485static struct attribute * ccwdev_attrs[] = {
486 &dev_attr_devtype.attr,
487 &dev_attr_cutype.attr,
Bastian Blankf1fc78a2005-10-30 15:00:12 -0800488 &dev_attr_modalias.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 &dev_attr_online.attr,
490 &dev_attr_cmb_enable.attr,
491 &dev_attr_availability.attr,
492 NULL,
493};
494
495static struct attribute_group ccwdev_attr_group = {
496 .attrs = ccwdev_attrs,
497};
498
499static inline int
500device_add_files (struct device *dev)
501{
502 return sysfs_create_group(&dev->kobj, &ccwdev_attr_group);
503}
504
505static inline void
506device_remove_files(struct device *dev)
507{
508 sysfs_remove_group(&dev->kobj, &ccwdev_attr_group);
509}
510
511/* this is a simple abstraction for device_register that sets the
512 * correct bus type and adds the bus specific files */
513int
514ccw_device_register(struct ccw_device *cdev)
515{
516 struct device *dev = &cdev->dev;
517 int ret;
518
519 dev->bus = &ccw_bus_type;
520
521 if ((ret = device_add(dev)))
522 return ret;
523
524 set_bit(1, &cdev->private->registered);
525 if ((ret = device_add_files(dev))) {
526 if (test_and_clear_bit(1, &cdev->private->registered))
527 device_del(dev);
528 }
529 return ret;
530}
531
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700532struct match_data {
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800533 unsigned int devno;
534 unsigned int ssid;
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700535 struct ccw_device * sibling;
536};
537
538static int
539match_devno(struct device * dev, void * data)
540{
541 struct match_data * d = (struct match_data *)data;
542 struct ccw_device * cdev;
543
544 cdev = to_ccwdev(dev);
545 if ((cdev->private->state == DEV_STATE_DISCONNECTED) &&
546 (cdev->private->devno == d->devno) &&
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800547 (cdev->private->ssid == d->ssid) &&
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700548 (cdev != d->sibling)) {
549 cdev->private->state = DEV_STATE_NOT_OPER;
550 return 1;
551 }
552 return 0;
553}
554
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555static struct ccw_device *
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800556get_disc_ccwdev_by_devno(unsigned int devno, unsigned int ssid,
557 struct ccw_device *sibling)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 struct device *dev;
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700560 struct match_data data = {
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800561 .devno = devno,
562 .ssid = ssid,
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700563 .sibling = sibling,
564 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
Cornelia Hucke5945b42005-10-11 08:28:59 -0700566 dev = bus_find_device(&ccw_bus_type, NULL, &data, match_devno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700568 return dev ? to_ccwdev(dev) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569}
570
571static void
572ccw_device_add_changed(void *data)
573{
574
575 struct ccw_device *cdev;
576
577 cdev = (struct ccw_device *)data;
578 if (device_add(&cdev->dev)) {
579 put_device(&cdev->dev);
580 return;
581 }
582 set_bit(1, &cdev->private->registered);
583 if (device_add_files(&cdev->dev)) {
584 if (test_and_clear_bit(1, &cdev->private->registered))
585 device_unregister(&cdev->dev);
586 }
587}
588
589extern int css_get_ssd_info(struct subchannel *sch);
590
591void
592ccw_device_do_unreg_rereg(void *data)
593{
594 struct ccw_device *cdev;
595 struct subchannel *sch;
596 int need_rename;
597
598 cdev = (struct ccw_device *)data;
599 sch = to_subchannel(cdev->dev.parent);
600 if (cdev->private->devno != sch->schib.pmcw.dev) {
601 /*
602 * The device number has changed. This is usually only when
603 * a device has been detached under VM and then re-appeared
604 * on another subchannel because of a different attachment
605 * order than before. Ideally, we should should just switch
606 * subchannels, but unfortunately, this is not possible with
607 * the current implementation.
608 * Instead, we search for the old subchannel for this device
609 * number and deregister so there are no collisions with the
610 * newly registered ccw_device.
611 * FIXME: Find another solution so the block layer doesn't
612 * get possibly sick...
613 */
614 struct ccw_device *other_cdev;
615
616 need_rename = 1;
617 other_cdev = get_disc_ccwdev_by_devno(sch->schib.pmcw.dev,
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800618 sch->schid.ssid, cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 if (other_cdev) {
620 struct subchannel *other_sch;
621
622 other_sch = to_subchannel(other_cdev->dev.parent);
623 if (get_device(&other_sch->dev)) {
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800624 stsch(other_sch->schid, &other_sch->schib);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 if (other_sch->schib.pmcw.dnv) {
626 other_sch->schib.pmcw.intparm = 0;
627 cio_modify(other_sch);
628 }
629 device_unregister(&other_sch->dev);
630 }
631 }
632 /* Update ssd info here. */
633 css_get_ssd_info(sch);
634 cdev->private->devno = sch->schib.pmcw.dev;
635 } else
636 need_rename = 0;
637 device_remove_files(&cdev->dev);
638 if (test_and_clear_bit(1, &cdev->private->registered))
639 device_del(&cdev->dev);
640 if (need_rename)
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800641 snprintf (cdev->dev.bus_id, BUS_ID_SIZE, "0.%x.%04x",
642 sch->schid.ssid, sch->schib.pmcw.dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 PREPARE_WORK(&cdev->private->kick_work,
644 ccw_device_add_changed, (void *)cdev);
645 queue_work(ccw_device_work, &cdev->private->kick_work);
646}
647
648static void
649ccw_device_release(struct device *dev)
650{
651 struct ccw_device *cdev;
652
653 cdev = to_ccwdev(dev);
654 kfree(cdev->private);
655 kfree(cdev);
656}
657
658/*
659 * Register recognized device.
660 */
661static void
662io_subchannel_register(void *data)
663{
664 struct ccw_device *cdev;
665 struct subchannel *sch;
666 int ret;
667 unsigned long flags;
668
669 cdev = (struct ccw_device *) data;
670 sch = to_subchannel(cdev->dev.parent);
671
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700672 if (klist_node_attached(&cdev->dev.knode_parent)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 bus_rescan_devices(&ccw_bus_type);
674 goto out;
675 }
676 /* make it known to the system */
677 ret = ccw_device_register(cdev);
678 if (ret) {
679 printk (KERN_WARNING "%s: could not register %s\n",
680 __func__, cdev->dev.bus_id);
681 put_device(&cdev->dev);
682 spin_lock_irqsave(&sch->lock, flags);
683 sch->dev.driver_data = NULL;
684 spin_unlock_irqrestore(&sch->lock, flags);
685 kfree (cdev->private);
686 kfree (cdev);
687 put_device(&sch->dev);
688 if (atomic_dec_and_test(&ccw_device_init_count))
689 wake_up(&ccw_device_init_wq);
690 return;
691 }
692
693 ret = subchannel_add_files(cdev->dev.parent);
694 if (ret)
695 printk(KERN_WARNING "%s: could not add attributes to %s\n",
696 __func__, sch->dev.bus_id);
697 put_device(&cdev->dev);
698out:
699 cdev->private->flags.recog_done = 1;
700 put_device(&sch->dev);
701 wake_up(&cdev->private->wait_q);
702 if (atomic_dec_and_test(&ccw_device_init_count))
703 wake_up(&ccw_device_init_wq);
704}
705
706void
707ccw_device_call_sch_unregister(void *data)
708{
709 struct ccw_device *cdev = data;
710 struct subchannel *sch;
711
712 sch = to_subchannel(cdev->dev.parent);
713 device_unregister(&sch->dev);
714 /* Reset intparm to zeroes. */
715 sch->schib.pmcw.intparm = 0;
716 cio_modify(sch);
717 put_device(&cdev->dev);
718 put_device(&sch->dev);
719}
720
721/*
722 * subchannel recognition done. Called from the state machine.
723 */
724void
725io_subchannel_recog_done(struct ccw_device *cdev)
726{
727 struct subchannel *sch;
728
729 if (css_init_done == 0) {
730 cdev->private->flags.recog_done = 1;
731 return;
732 }
733 switch (cdev->private->state) {
734 case DEV_STATE_NOT_OPER:
735 cdev->private->flags.recog_done = 1;
736 /* Remove device found not operational. */
737 if (!get_device(&cdev->dev))
738 break;
739 sch = to_subchannel(cdev->dev.parent);
740 PREPARE_WORK(&cdev->private->kick_work,
741 ccw_device_call_sch_unregister, (void *) cdev);
742 queue_work(slow_path_wq, &cdev->private->kick_work);
743 if (atomic_dec_and_test(&ccw_device_init_count))
744 wake_up(&ccw_device_init_wq);
745 break;
746 case DEV_STATE_BOXED:
747 /* Device did not respond in time. */
748 case DEV_STATE_OFFLINE:
749 /*
750 * We can't register the device in interrupt context so
751 * we schedule a work item.
752 */
753 if (!get_device(&cdev->dev))
754 break;
755 PREPARE_WORK(&cdev->private->kick_work,
756 io_subchannel_register, (void *) cdev);
757 queue_work(slow_path_wq, &cdev->private->kick_work);
758 break;
759 }
760}
761
762static int
763io_subchannel_recog(struct ccw_device *cdev, struct subchannel *sch)
764{
765 int rc;
766 struct ccw_device_private *priv;
767
768 sch->dev.driver_data = cdev;
769 sch->driver = &io_subchannel_driver;
770 cdev->ccwlock = &sch->lock;
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800771
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 /* Init private data. */
773 priv = cdev->private;
774 priv->devno = sch->schib.pmcw.dev;
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800775 priv->ssid = sch->schid.ssid;
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800776 priv->sch_no = sch->schid.sch_no;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 priv->state = DEV_STATE_NOT_OPER;
778 INIT_LIST_HEAD(&priv->cmb_list);
779 init_waitqueue_head(&priv->wait_q);
780 init_timer(&priv->timer);
781
782 /* Set an initial name for the device. */
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800783 snprintf (cdev->dev.bus_id, BUS_ID_SIZE, "0.%x.%04x",
784 sch->schid.ssid, sch->schib.pmcw.dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785
786 /* Increase counter of devices currently in recognition. */
787 atomic_inc(&ccw_device_init_count);
788
789 /* Start async. device sensing. */
790 spin_lock_irq(&sch->lock);
791 rc = ccw_device_recognition(cdev);
792 spin_unlock_irq(&sch->lock);
793 if (rc) {
794 if (atomic_dec_and_test(&ccw_device_init_count))
795 wake_up(&ccw_device_init_wq);
796 }
797 return rc;
798}
799
800static int
Cornelia Huck8bbace72006-01-11 10:56:22 +0100801io_subchannel_probe (struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 struct ccw_device *cdev;
804 int rc;
805 unsigned long flags;
806
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 if (sch->dev.driver_data) {
808 /*
809 * This subchannel already has an associated ccw_device.
810 * Register it and exit. This happens for all early
811 * device, e.g. the console.
812 */
813 cdev = sch->dev.driver_data;
814 device_initialize(&cdev->dev);
815 ccw_device_register(cdev);
816 subchannel_add_files(&sch->dev);
817 /*
818 * Check if the device is already online. If it is
819 * the reference count needs to be corrected
820 * (see ccw_device_online and css_init_done for the
821 * ugly details).
822 */
823 if (cdev->private->state != DEV_STATE_NOT_OPER &&
824 cdev->private->state != DEV_STATE_OFFLINE &&
825 cdev->private->state != DEV_STATE_BOXED)
826 get_device(&cdev->dev);
827 return 0;
828 }
829 cdev = kmalloc (sizeof(*cdev), GFP_KERNEL);
830 if (!cdev)
831 return -ENOMEM;
832 memset(cdev, 0, sizeof(struct ccw_device));
833 cdev->private = kmalloc(sizeof(struct ccw_device_private),
834 GFP_KERNEL | GFP_DMA);
835 if (!cdev->private) {
836 kfree(cdev);
837 return -ENOMEM;
838 }
839 memset(cdev->private, 0, sizeof(struct ccw_device_private));
840 atomic_set(&cdev->private->onoff, 0);
841 cdev->dev = (struct device) {
Cornelia Huck8bbace72006-01-11 10:56:22 +0100842 .parent = &sch->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 .release = ccw_device_release,
844 };
845 INIT_LIST_HEAD(&cdev->private->kick_work.entry);
846 /* Do first half of device_register. */
847 device_initialize(&cdev->dev);
848
849 if (!get_device(&sch->dev)) {
850 if (cdev->dev.release)
851 cdev->dev.release(&cdev->dev);
852 return -ENODEV;
853 }
854
Cornelia Huck8bbace72006-01-11 10:56:22 +0100855 rc = io_subchannel_recog(cdev, sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 if (rc) {
857 spin_lock_irqsave(&sch->lock, flags);
858 sch->dev.driver_data = NULL;
859 spin_unlock_irqrestore(&sch->lock, flags);
860 if (cdev->dev.release)
861 cdev->dev.release(&cdev->dev);
862 }
863
864 return rc;
865}
866
867static void
868ccw_device_unregister(void *data)
869{
870 struct ccw_device *cdev;
871
872 cdev = (struct ccw_device *)data;
873 if (test_and_clear_bit(1, &cdev->private->registered))
874 device_unregister(&cdev->dev);
875 put_device(&cdev->dev);
876}
877
878static int
Cornelia Huck8bbace72006-01-11 10:56:22 +0100879io_subchannel_remove (struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880{
881 struct ccw_device *cdev;
882 unsigned long flags;
883
Cornelia Huck8bbace72006-01-11 10:56:22 +0100884 if (!sch->dev.driver_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 return 0;
Cornelia Huck8bbace72006-01-11 10:56:22 +0100886 cdev = sch->dev.driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 /* Set ccw device to not operational and drop reference. */
888 spin_lock_irqsave(cdev->ccwlock, flags);
Cornelia Huck8bbace72006-01-11 10:56:22 +0100889 sch->dev.driver_data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 cdev->private->state = DEV_STATE_NOT_OPER;
891 spin_unlock_irqrestore(cdev->ccwlock, flags);
892 /*
893 * Put unregistration on workqueue to avoid livelocks on the css bus
894 * semaphore.
895 */
896 if (get_device(&cdev->dev)) {
897 PREPARE_WORK(&cdev->private->kick_work,
898 ccw_device_unregister, (void *) cdev);
899 queue_work(ccw_device_work, &cdev->private->kick_work);
900 }
901 return 0;
902}
903
904static int
905io_subchannel_notify(struct device *dev, int event)
906{
907 struct ccw_device *cdev;
908
909 cdev = dev->driver_data;
910 if (!cdev)
911 return 0;
912 if (!cdev->drv)
913 return 0;
914 if (!cdev->online)
915 return 0;
916 return cdev->drv->notify ? cdev->drv->notify(cdev, event) : 0;
917}
918
919static void
920io_subchannel_verify(struct device *dev)
921{
922 struct ccw_device *cdev;
923
924 cdev = dev->driver_data;
925 if (cdev)
926 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
927}
928
929static void
930io_subchannel_ioterm(struct device *dev)
931{
932 struct ccw_device *cdev;
933
934 cdev = dev->driver_data;
935 if (!cdev)
936 return;
937 cdev->private->state = DEV_STATE_CLEAR_VERIFY;
938 if (cdev->handler)
939 cdev->handler(cdev, cdev->private->intparm,
940 ERR_PTR(-EIO));
941}
942
943static void
Cornelia Huck8bbace72006-01-11 10:56:22 +0100944io_subchannel_shutdown(struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 struct ccw_device *cdev;
947 int ret;
948
Cornelia Huck8bbace72006-01-11 10:56:22 +0100949 cdev = sch->dev.driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800951 if (cio_is_console(sch->schid))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 return;
953 if (!sch->schib.pmcw.ena)
954 /* Nothing to do. */
955 return;
956 ret = cio_disable_subchannel(sch);
957 if (ret != -EBUSY)
958 /* Subchannel is disabled, we're done. */
959 return;
960 cdev->private->state = DEV_STATE_QUIESCE;
961 if (cdev->handler)
962 cdev->handler(cdev, cdev->private->intparm,
963 ERR_PTR(-EIO));
964 ret = ccw_device_cancel_halt_clear(cdev);
965 if (ret == -EBUSY) {
966 ccw_device_set_timeout(cdev, HZ/10);
967 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
968 }
969 cio_disable_subchannel(sch);
970}
971
972#ifdef CONFIG_CCW_CONSOLE
973static struct ccw_device console_cdev;
974static struct ccw_device_private console_private;
975static int console_cdev_in_use;
976
977static int
978ccw_device_console_enable (struct ccw_device *cdev, struct subchannel *sch)
979{
980 int rc;
981
982 /* Initialize the ccw_device structure. */
983 cdev->dev = (struct device) {
984 .parent = &sch->dev,
985 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 rc = io_subchannel_recog(cdev, sch);
987 if (rc)
988 return rc;
989
990 /* Now wait for the async. recognition to come to an end. */
991 spin_lock_irq(cdev->ccwlock);
992 while (!dev_fsm_final_state(cdev))
993 wait_cons_dev();
994 rc = -EIO;
995 if (cdev->private->state != DEV_STATE_OFFLINE)
996 goto out_unlock;
997 ccw_device_online(cdev);
998 while (!dev_fsm_final_state(cdev))
999 wait_cons_dev();
1000 if (cdev->private->state != DEV_STATE_ONLINE)
1001 goto out_unlock;
1002 rc = 0;
1003out_unlock:
1004 spin_unlock_irq(cdev->ccwlock);
1005 return 0;
1006}
1007
1008struct ccw_device *
1009ccw_device_probe_console(void)
1010{
1011 struct subchannel *sch;
1012 int ret;
1013
1014 if (xchg(&console_cdev_in_use, 1) != 0)
Peter Oberparleiter600b5d12006-02-01 03:06:35 -08001015 return ERR_PTR(-EBUSY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 sch = cio_probe_console();
1017 if (IS_ERR(sch)) {
1018 console_cdev_in_use = 0;
1019 return (void *) sch;
1020 }
1021 memset(&console_cdev, 0, sizeof(struct ccw_device));
1022 memset(&console_private, 0, sizeof(struct ccw_device_private));
1023 console_cdev.private = &console_private;
1024 ret = ccw_device_console_enable(&console_cdev, sch);
1025 if (ret) {
1026 cio_release_console();
1027 console_cdev_in_use = 0;
1028 return ERR_PTR(ret);
1029 }
1030 console_cdev.online = 1;
1031 return &console_cdev;
1032}
1033#endif
1034
1035/*
1036 * get ccw_device matching the busid, but only if owned by cdrv
1037 */
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001038static int
1039__ccwdev_check_busid(struct device *dev, void *id)
1040{
1041 char *bus_id;
1042
1043 bus_id = (char *)id;
1044
1045 return (strncmp(bus_id, dev->bus_id, BUS_ID_SIZE) == 0);
1046}
1047
1048
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049struct ccw_device *
1050get_ccwdev_by_busid(struct ccw_driver *cdrv, const char *bus_id)
1051{
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001052 struct device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 struct device_driver *drv;
1054
1055 drv = get_driver(&cdrv->driver);
1056 if (!drv)
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001057 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001059 dev = driver_find_device(drv, NULL, (void *)bus_id,
1060 __ccwdev_check_busid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 put_driver(drv);
1062
1063 return dev ? to_ccwdev(dev) : 0;
1064}
1065
1066/************************** device driver handling ************************/
1067
1068/* This is the implementation of the ccw_driver class. The probe, remove
1069 * and release methods are initially very similar to the device_driver
1070 * implementations, with the difference that they have ccw_device
1071 * arguments.
1072 *
1073 * A ccw driver also contains the information that is needed for
1074 * device matching.
1075 */
1076static int
1077ccw_device_probe (struct device *dev)
1078{
1079 struct ccw_device *cdev = to_ccwdev(dev);
1080 struct ccw_driver *cdrv = to_ccwdrv(dev->driver);
1081 int ret;
1082
1083 cdev->drv = cdrv; /* to let the driver call _set_online */
1084
1085 ret = cdrv->probe ? cdrv->probe(cdev) : -ENODEV;
1086
1087 if (ret) {
1088 cdev->drv = 0;
1089 return ret;
1090 }
1091
1092 return 0;
1093}
1094
1095static int
1096ccw_device_remove (struct device *dev)
1097{
1098 struct ccw_device *cdev = to_ccwdev(dev);
1099 struct ccw_driver *cdrv = cdev->drv;
1100 int ret;
1101
1102 pr_debug("removing device %s\n", cdev->dev.bus_id);
1103 if (cdrv->remove)
1104 cdrv->remove(cdev);
1105 if (cdev->online) {
1106 cdev->online = 0;
1107 spin_lock_irq(cdev->ccwlock);
1108 ret = ccw_device_offline(cdev);
1109 spin_unlock_irq(cdev->ccwlock);
1110 if (ret == 0)
1111 wait_event(cdev->private->wait_q,
1112 dev_fsm_final_state(cdev));
1113 else
1114 //FIXME: we can't fail!
1115 pr_debug("ccw_device_offline returned %d, device %s\n",
1116 ret, cdev->dev.bus_id);
1117 }
1118 ccw_device_set_timeout(cdev, 0);
1119 cdev->drv = 0;
1120 return 0;
1121}
1122
Cornelia Huck8bbace72006-01-11 10:56:22 +01001123struct bus_type ccw_bus_type = {
1124 .name = "ccw",
1125 .match = ccw_bus_match,
1126 .uevent = ccw_uevent,
1127 .probe = ccw_device_probe,
1128 .remove = ccw_device_remove,
1129};
1130
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131int
1132ccw_driver_register (struct ccw_driver *cdriver)
1133{
1134 struct device_driver *drv = &cdriver->driver;
1135
1136 drv->bus = &ccw_bus_type;
1137 drv->name = cdriver->name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138
1139 return driver_register(drv);
1140}
1141
1142void
1143ccw_driver_unregister (struct ccw_driver *cdriver)
1144{
1145 driver_unregister(&cdriver->driver);
1146}
1147
Cornelia Hucka8237fc2006-01-06 00:19:21 -08001148/* Helper func for qdio. */
1149struct subchannel_id
1150ccw_device_get_subchannel_id(struct ccw_device *cdev)
1151{
1152 struct subchannel *sch;
1153
1154 sch = to_subchannel(cdev->dev.parent);
1155 return sch->schid;
1156}
1157
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158MODULE_LICENSE("GPL");
1159EXPORT_SYMBOL(ccw_device_set_online);
1160EXPORT_SYMBOL(ccw_device_set_offline);
1161EXPORT_SYMBOL(ccw_driver_register);
1162EXPORT_SYMBOL(ccw_driver_unregister);
1163EXPORT_SYMBOL(get_ccwdev_by_busid);
1164EXPORT_SYMBOL(ccw_bus_type);
1165EXPORT_SYMBOL(ccw_device_work);
1166EXPORT_SYMBOL(ccw_device_notify_work);
Cornelia Hucka8237fc2006-01-06 00:19:21 -08001167EXPORT_SYMBOL_GPL(ccw_device_get_subchannel_id);