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