blob: 646da56404015d7d614b3c469c1305962ee8e085 [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"
26#include "css.h"
27#include "device.h"
28#include "ioasm.h"
29
30/******************* bus type handling ***********************/
31
32/* The Linux driver model distinguishes between a bus type and
33 * the bus itself. Of course we only have one channel
34 * subsystem driver and one channel system per machine, but
35 * we still use the abstraction. T.R. says it's a good idea. */
36static int
37ccw_bus_match (struct device * dev, struct device_driver * drv)
38{
39 struct ccw_device *cdev = to_ccwdev(dev);
40 struct ccw_driver *cdrv = to_ccwdrv(drv);
41 const struct ccw_device_id *ids = cdrv->ids, *found;
42
43 if (!ids)
44 return 0;
45
46 found = ccw_device_id_match(ids, &cdev->id);
47 if (!found)
48 return 0;
49
50 cdev->id.driver_info = found->driver_info;
51
52 return 1;
53}
54
55/*
56 * Hotplugging interface for ccw devices.
57 * Heavily modeled on pci and usb hotplug.
58 */
59static int
Paul Jackson6d20b032005-11-25 20:04:26 -080060ccw_uevent (struct device *dev, char **envp, int num_envp,
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 char *buffer, int buffer_size)
62{
63 struct ccw_device *cdev = to_ccwdev(dev);
64 int i = 0;
65 int length = 0;
66
67 if (!cdev)
68 return -ENODEV;
69
70 /* what we want to pass to /sbin/hotplug */
71
72 envp[i++] = buffer;
73 length += scnprintf(buffer, buffer_size - length, "CU_TYPE=%04X",
74 cdev->id.cu_type);
75 if ((buffer_size - length <= 0) || (i >= num_envp))
76 return -ENOMEM;
77 ++length;
78 buffer += length;
79
80 envp[i++] = buffer;
81 length += scnprintf(buffer, buffer_size - length, "CU_MODEL=%02X",
82 cdev->id.cu_model);
83 if ((buffer_size - length <= 0) || (i >= num_envp))
84 return -ENOMEM;
85 ++length;
86 buffer += length;
87
88 /* The next two can be zero, that's ok for us */
89 envp[i++] = buffer;
90 length += scnprintf(buffer, buffer_size - length, "DEV_TYPE=%04X",
91 cdev->id.dev_type);
92 if ((buffer_size - length <= 0) || (i >= num_envp))
93 return -ENOMEM;
94 ++length;
95 buffer += length;
96
97 envp[i++] = buffer;
98 length += scnprintf(buffer, buffer_size - length, "DEV_MODEL=%02X",
99 cdev->id.dev_model);
100 if ((buffer_size - length <= 0) || (i >= num_envp))
101 return -ENOMEM;
102
Heiko Carstensd2c993d2006-07-12 16:41:55 +0200103 envp[i] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
105 return 0;
106}
107
Cornelia Huck8bbace72006-01-11 10:56:22 +0100108struct bus_type ccw_bus_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
Cornelia Huck8bbace72006-01-11 10:56:22 +0100110static int io_subchannel_probe (struct subchannel *);
111static int io_subchannel_remove (struct subchannel *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112void io_subchannel_irq (struct device *);
113static int io_subchannel_notify(struct device *, int);
114static void io_subchannel_verify(struct device *);
115static void io_subchannel_ioterm(struct device *);
Cornelia Huck8bbace72006-01-11 10:56:22 +0100116static void io_subchannel_shutdown(struct subchannel *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
118struct css_driver io_subchannel_driver = {
119 .subchannel_type = SUBCHANNEL_TYPE_IO,
120 .drv = {
121 .name = "io_subchannel",
122 .bus = &css_bus_type,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 },
124 .irq = io_subchannel_irq,
125 .notify = io_subchannel_notify,
126 .verify = io_subchannel_verify,
127 .termination = io_subchannel_ioterm,
Cornelia Huck8bbace72006-01-11 10:56:22 +0100128 .probe = io_subchannel_probe,
129 .remove = io_subchannel_remove,
130 .shutdown = io_subchannel_shutdown,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131};
132
133struct workqueue_struct *ccw_device_work;
134struct workqueue_struct *ccw_device_notify_work;
Peter Oberparleiter40154b82006-06-29 14:57:03 +0200135wait_queue_head_t ccw_device_init_wq;
136atomic_t ccw_device_init_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
138static int __init
139init_ccw_bus_type (void)
140{
141 int ret;
142
143 init_waitqueue_head(&ccw_device_init_wq);
144 atomic_set(&ccw_device_init_count, 0);
145
146 ccw_device_work = create_singlethread_workqueue("cio");
147 if (!ccw_device_work)
148 return -ENOMEM; /* FIXME: better errno ? */
149 ccw_device_notify_work = create_singlethread_workqueue("cio_notify");
150 if (!ccw_device_notify_work) {
151 ret = -ENOMEM; /* FIXME: better errno ? */
152 goto out_err;
153 }
154 slow_path_wq = create_singlethread_workqueue("kslowcrw");
155 if (!slow_path_wq) {
156 ret = -ENOMEM; /* FIXME: better errno ? */
157 goto out_err;
158 }
159 if ((ret = bus_register (&ccw_bus_type)))
160 goto out_err;
161
162 if ((ret = driver_register(&io_subchannel_driver.drv)))
163 goto out_err;
164
165 wait_event(ccw_device_init_wq,
166 atomic_read(&ccw_device_init_count) == 0);
167 flush_workqueue(ccw_device_work);
168 return 0;
169out_err:
170 if (ccw_device_work)
171 destroy_workqueue(ccw_device_work);
172 if (ccw_device_notify_work)
173 destroy_workqueue(ccw_device_notify_work);
174 if (slow_path_wq)
175 destroy_workqueue(slow_path_wq);
176 return ret;
177}
178
179static void __exit
180cleanup_ccw_bus_type (void)
181{
182 driver_unregister(&io_subchannel_driver.drv);
183 bus_unregister(&ccw_bus_type);
184 destroy_workqueue(ccw_device_notify_work);
185 destroy_workqueue(ccw_device_work);
186}
187
188subsys_initcall(init_ccw_bus_type);
189module_exit(cleanup_ccw_bus_type);
190
191/************************ device handling **************************/
192
193/*
194 * A ccw_device has some interfaces in sysfs in addition to the
195 * standard ones.
196 * The following entries are designed to export the information which
197 * resided in 2.4 in /proc/subchannels. Subchannel and device number
198 * are obvious, so they don't have an entry :)
199 * TODO: Split chpids and pimpampom up? Where is "in use" in the tree?
200 */
201static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400202chpids_show (struct device * dev, struct device_attribute *attr, char * buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203{
204 struct subchannel *sch = to_subchannel(dev);
205 struct ssd_info *ssd = &sch->ssd_info;
206 ssize_t ret = 0;
207 int chp;
208
209 for (chp = 0; chp < 8; chp++)
210 ret += sprintf (buf+ret, "%02x ", ssd->chpid[chp]);
211
212 ret += sprintf (buf+ret, "\n");
213 return min((ssize_t)PAGE_SIZE, ret);
214}
215
216static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400217pimpampom_show (struct device * dev, struct device_attribute *attr, char * buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218{
219 struct subchannel *sch = to_subchannel(dev);
220 struct pmcw *pmcw = &sch->schib.pmcw;
221
222 return sprintf (buf, "%02x %02x %02x\n",
223 pmcw->pim, pmcw->pam, pmcw->pom);
224}
225
226static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400227devtype_show (struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228{
229 struct ccw_device *cdev = to_ccwdev(dev);
230 struct ccw_device_id *id = &(cdev->id);
231
232 if (id->dev_type != 0)
233 return sprintf(buf, "%04x/%02x\n",
234 id->dev_type, id->dev_model);
235 else
236 return sprintf(buf, "n/a\n");
237}
238
239static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400240cutype_show (struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241{
242 struct ccw_device *cdev = to_ccwdev(dev);
243 struct ccw_device_id *id = &(cdev->id);
244
245 return sprintf(buf, "%04x/%02x\n",
246 id->cu_type, id->cu_model);
247}
248
249static ssize_t
Bastian Blankf1fc78a2005-10-30 15:00:12 -0800250modalias_show (struct device *dev, struct device_attribute *attr, char *buf)
251{
252 struct ccw_device *cdev = to_ccwdev(dev);
253 struct ccw_device_id *id = &(cdev->id);
254 int ret;
255
Cornelia Huckb1969fa2006-02-01 03:06:39 -0800256 ret = sprintf(buf, "ccw:t%04Xm%02X",
Bastian Blankf1fc78a2005-10-30 15:00:12 -0800257 id->cu_type, id->cu_model);
258 if (id->dev_type != 0)
259 ret += sprintf(buf + ret, "dt%04Xdm%02X\n",
260 id->dev_type, id->dev_model);
261 else
262 ret += sprintf(buf + ret, "dtdm\n");
263 return ret;
264}
265
266static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400267online_show (struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268{
269 struct ccw_device *cdev = to_ccwdev(dev);
270
271 return sprintf(buf, cdev->online ? "1\n" : "0\n");
272}
273
274static void
275ccw_device_remove_disconnected(struct ccw_device *cdev)
276{
277 struct subchannel *sch;
278 /*
279 * Forced offline in disconnected state means
280 * 'throw away device'.
281 */
282 sch = to_subchannel(cdev->dev.parent);
Cornelia Huck6ab48792006-07-12 16:39:50 +0200283 css_sch_device_unregister(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 /* Reset intparm to zeroes. */
285 sch->schib.pmcw.intparm = 0;
286 cio_modify(sch);
287 put_device(&sch->dev);
288}
289
290int
291ccw_device_set_offline(struct ccw_device *cdev)
292{
293 int ret;
294
295 if (!cdev)
296 return -ENODEV;
297 if (!cdev->online || !cdev->drv)
298 return -EINVAL;
299
300 if (cdev->drv->set_offline) {
301 ret = cdev->drv->set_offline(cdev);
302 if (ret != 0)
303 return ret;
304 }
305 cdev->online = 0;
306 spin_lock_irq(cdev->ccwlock);
307 ret = ccw_device_offline(cdev);
308 if (ret == -ENODEV) {
309 if (cdev->private->state != DEV_STATE_NOT_OPER) {
310 cdev->private->state = DEV_STATE_OFFLINE;
311 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
312 }
313 spin_unlock_irq(cdev->ccwlock);
314 return ret;
315 }
316 spin_unlock_irq(cdev->ccwlock);
317 if (ret == 0)
318 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
319 else {
320 pr_debug("ccw_device_offline returned %d, device %s\n",
321 ret, cdev->dev.bus_id);
322 cdev->online = 1;
323 }
324 return ret;
325}
326
327int
328ccw_device_set_online(struct ccw_device *cdev)
329{
330 int ret;
331
332 if (!cdev)
333 return -ENODEV;
334 if (cdev->online || !cdev->drv)
335 return -EINVAL;
336
337 spin_lock_irq(cdev->ccwlock);
338 ret = ccw_device_online(cdev);
339 spin_unlock_irq(cdev->ccwlock);
340 if (ret == 0)
341 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
342 else {
343 pr_debug("ccw_device_online returned %d, device %s\n",
344 ret, cdev->dev.bus_id);
345 return ret;
346 }
347 if (cdev->private->state != DEV_STATE_ONLINE)
348 return -ENODEV;
349 if (!cdev->drv->set_online || cdev->drv->set_online(cdev) == 0) {
350 cdev->online = 1;
351 return 0;
352 }
353 spin_lock_irq(cdev->ccwlock);
354 ret = ccw_device_offline(cdev);
355 spin_unlock_irq(cdev->ccwlock);
356 if (ret == 0)
357 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
358 else
359 pr_debug("ccw_device_offline returned %d, device %s\n",
360 ret, cdev->dev.bus_id);
Cornelia Huck6cadb782006-02-17 13:52:49 -0800361 return (ret == 0) ? -ENODEV : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362}
363
364static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400365online_store (struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366{
367 struct ccw_device *cdev = to_ccwdev(dev);
368 int i, force, ret;
369 char *tmp;
370
Martin Schwidefsky973bd992006-01-06 00:19:07 -0800371 if (atomic_cmpxchg(&cdev->private->onoff, 0, 1) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 return -EAGAIN;
373
374 if (cdev->drv && !try_module_get(cdev->drv->owner)) {
375 atomic_set(&cdev->private->onoff, 0);
376 return -EINVAL;
377 }
378 if (!strncmp(buf, "force\n", count)) {
379 force = 1;
380 i = 1;
381 } else {
382 force = 0;
383 i = simple_strtoul(buf, &tmp, 16);
384 }
385 if (i == 1) {
386 /* Do device recognition, if needed. */
387 if (cdev->id.cu_type == 0) {
388 ret = ccw_device_recognition(cdev);
389 if (ret) {
390 printk(KERN_WARNING"Couldn't start recognition "
391 "for device %s (ret=%d)\n",
392 cdev->dev.bus_id, ret);
393 goto out;
394 }
395 wait_event(cdev->private->wait_q,
396 cdev->private->flags.recog_done);
397 }
398 if (cdev->drv && cdev->drv->set_online)
399 ccw_device_set_online(cdev);
400 } else if (i == 0) {
401 if (cdev->private->state == DEV_STATE_DISCONNECTED)
402 ccw_device_remove_disconnected(cdev);
403 else if (cdev->drv && cdev->drv->set_offline)
404 ccw_device_set_offline(cdev);
405 }
406 if (force && cdev->private->state == DEV_STATE_BOXED) {
407 ret = ccw_device_stlck(cdev);
408 if (ret) {
409 printk(KERN_WARNING"ccw_device_stlck for device %s "
410 "returned %d!\n", cdev->dev.bus_id, ret);
411 goto out;
412 }
413 /* Do device recognition, if needed. */
414 if (cdev->id.cu_type == 0) {
415 cdev->private->state = DEV_STATE_NOT_OPER;
416 ret = ccw_device_recognition(cdev);
417 if (ret) {
418 printk(KERN_WARNING"Couldn't start recognition "
419 "for device %s (ret=%d)\n",
420 cdev->dev.bus_id, ret);
421 goto out;
422 }
423 wait_event(cdev->private->wait_q,
424 cdev->private->flags.recog_done);
425 }
426 if (cdev->drv && cdev->drv->set_online)
427 ccw_device_set_online(cdev);
428 }
429 out:
430 if (cdev->drv)
431 module_put(cdev->drv->owner);
432 atomic_set(&cdev->private->onoff, 0);
433 return count;
434}
435
436static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400437available_show (struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438{
439 struct ccw_device *cdev = to_ccwdev(dev);
440 struct subchannel *sch;
441
442 switch (cdev->private->state) {
443 case DEV_STATE_BOXED:
444 return sprintf(buf, "boxed\n");
445 case DEV_STATE_DISCONNECTED:
446 case DEV_STATE_DISCONNECTED_SENSE_ID:
447 case DEV_STATE_NOT_OPER:
448 sch = to_subchannel(dev->parent);
449 if (!sch->lpm)
450 return sprintf(buf, "no path\n");
451 else
452 return sprintf(buf, "no device\n");
453 default:
454 /* All other states considered fine. */
455 return sprintf(buf, "good\n");
456 }
457}
458
459static DEVICE_ATTR(chpids, 0444, chpids_show, NULL);
460static DEVICE_ATTR(pimpampom, 0444, pimpampom_show, NULL);
461static DEVICE_ATTR(devtype, 0444, devtype_show, NULL);
462static DEVICE_ATTR(cutype, 0444, cutype_show, NULL);
Bastian Blankf1fc78a2005-10-30 15:00:12 -0800463static DEVICE_ATTR(modalias, 0444, modalias_show, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464static DEVICE_ATTR(online, 0644, online_show, online_store);
465extern struct device_attribute dev_attr_cmb_enable;
466static DEVICE_ATTR(availability, 0444, available_show, NULL);
467
468static struct attribute * subch_attrs[] = {
469 &dev_attr_chpids.attr,
470 &dev_attr_pimpampom.attr,
471 NULL,
472};
473
474static struct attribute_group subch_attr_group = {
475 .attrs = subch_attrs,
476};
477
478static inline int
479subchannel_add_files (struct device *dev)
480{
481 return sysfs_create_group(&dev->kobj, &subch_attr_group);
482}
483
484static struct attribute * ccwdev_attrs[] = {
485 &dev_attr_devtype.attr,
486 &dev_attr_cutype.attr,
Bastian Blankf1fc78a2005-10-30 15:00:12 -0800487 &dev_attr_modalias.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 &dev_attr_online.attr,
489 &dev_attr_cmb_enable.attr,
490 &dev_attr_availability.attr,
491 NULL,
492};
493
494static struct attribute_group ccwdev_attr_group = {
495 .attrs = ccwdev_attrs,
496};
497
498static inline int
499device_add_files (struct device *dev)
500{
501 return sysfs_create_group(&dev->kobj, &ccwdev_attr_group);
502}
503
504static inline void
505device_remove_files(struct device *dev)
506{
507 sysfs_remove_group(&dev->kobj, &ccwdev_attr_group);
508}
509
510/* this is a simple abstraction for device_register that sets the
511 * correct bus type and adds the bus specific files */
512int
513ccw_device_register(struct ccw_device *cdev)
514{
515 struct device *dev = &cdev->dev;
516 int ret;
517
518 dev->bus = &ccw_bus_type;
519
520 if ((ret = device_add(dev)))
521 return ret;
522
523 set_bit(1, &cdev->private->registered);
524 if ((ret = device_add_files(dev))) {
525 if (test_and_clear_bit(1, &cdev->private->registered))
526 device_del(dev);
527 }
528 return ret;
529}
530
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700531struct match_data {
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800532 unsigned int devno;
533 unsigned int ssid;
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700534 struct ccw_device * sibling;
535};
536
537static int
538match_devno(struct device * dev, void * data)
539{
540 struct match_data * d = (struct match_data *)data;
541 struct ccw_device * cdev;
542
543 cdev = to_ccwdev(dev);
544 if ((cdev->private->state == DEV_STATE_DISCONNECTED) &&
545 (cdev->private->devno == d->devno) &&
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800546 (cdev->private->ssid == d->ssid) &&
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700547 (cdev != d->sibling)) {
548 cdev->private->state = DEV_STATE_NOT_OPER;
549 return 1;
550 }
551 return 0;
552}
553
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554static struct ccw_device *
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800555get_disc_ccwdev_by_devno(unsigned int devno, unsigned int ssid,
556 struct ccw_device *sibling)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 struct device *dev;
Heiko Carstens292888c2006-08-30 14:33:35 +0200559 struct match_data data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
Heiko Carstens292888c2006-08-30 14:33:35 +0200561 data.devno = devno;
562 data.ssid = ssid;
563 data.sibling = sibling;
Cornelia Hucke5945b42005-10-11 08:28:59 -0700564 dev = bus_find_device(&ccw_bus_type, NULL, &data, match_devno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700566 return dev ? to_ccwdev(dev) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567}
568
569static void
570ccw_device_add_changed(void *data)
571{
572
573 struct ccw_device *cdev;
574
575 cdev = (struct ccw_device *)data;
576 if (device_add(&cdev->dev)) {
577 put_device(&cdev->dev);
578 return;
579 }
580 set_bit(1, &cdev->private->registered);
581 if (device_add_files(&cdev->dev)) {
582 if (test_and_clear_bit(1, &cdev->private->registered))
583 device_unregister(&cdev->dev);
584 }
585}
586
587extern int css_get_ssd_info(struct subchannel *sch);
588
589void
590ccw_device_do_unreg_rereg(void *data)
591{
592 struct ccw_device *cdev;
593 struct subchannel *sch;
594 int need_rename;
595
596 cdev = (struct ccw_device *)data;
597 sch = to_subchannel(cdev->dev.parent);
598 if (cdev->private->devno != sch->schib.pmcw.dev) {
599 /*
600 * The device number has changed. This is usually only when
601 * a device has been detached under VM and then re-appeared
602 * on another subchannel because of a different attachment
603 * order than before. Ideally, we should should just switch
604 * subchannels, but unfortunately, this is not possible with
605 * the current implementation.
606 * Instead, we search for the old subchannel for this device
607 * number and deregister so there are no collisions with the
608 * newly registered ccw_device.
609 * FIXME: Find another solution so the block layer doesn't
610 * get possibly sick...
611 */
612 struct ccw_device *other_cdev;
613
614 need_rename = 1;
615 other_cdev = get_disc_ccwdev_by_devno(sch->schib.pmcw.dev,
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800616 sch->schid.ssid, cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 if (other_cdev) {
618 struct subchannel *other_sch;
619
620 other_sch = to_subchannel(other_cdev->dev.parent);
621 if (get_device(&other_sch->dev)) {
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800622 stsch(other_sch->schid, &other_sch->schib);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 if (other_sch->schib.pmcw.dnv) {
624 other_sch->schib.pmcw.intparm = 0;
625 cio_modify(other_sch);
626 }
Cornelia Huck6ab48792006-07-12 16:39:50 +0200627 css_sch_device_unregister(other_sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 }
629 }
630 /* Update ssd info here. */
631 css_get_ssd_info(sch);
632 cdev->private->devno = sch->schib.pmcw.dev;
633 } else
634 need_rename = 0;
635 device_remove_files(&cdev->dev);
636 if (test_and_clear_bit(1, &cdev->private->registered))
637 device_del(&cdev->dev);
638 if (need_rename)
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800639 snprintf (cdev->dev.bus_id, BUS_ID_SIZE, "0.%x.%04x",
640 sch->schid.ssid, sch->schib.pmcw.dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 PREPARE_WORK(&cdev->private->kick_work,
642 ccw_device_add_changed, (void *)cdev);
643 queue_work(ccw_device_work, &cdev->private->kick_work);
644}
645
646static void
647ccw_device_release(struct device *dev)
648{
649 struct ccw_device *cdev;
650
651 cdev = to_ccwdev(dev);
652 kfree(cdev->private);
653 kfree(cdev);
654}
655
656/*
657 * Register recognized device.
658 */
659static void
660io_subchannel_register(void *data)
661{
662 struct ccw_device *cdev;
663 struct subchannel *sch;
664 int ret;
665 unsigned long flags;
666
667 cdev = (struct ccw_device *) data;
668 sch = to_subchannel(cdev->dev.parent);
669
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700670 if (klist_node_attached(&cdev->dev.knode_parent)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 bus_rescan_devices(&ccw_bus_type);
672 goto out;
673 }
674 /* make it known to the system */
675 ret = ccw_device_register(cdev);
676 if (ret) {
677 printk (KERN_WARNING "%s: could not register %s\n",
678 __func__, cdev->dev.bus_id);
679 put_device(&cdev->dev);
680 spin_lock_irqsave(&sch->lock, flags);
681 sch->dev.driver_data = NULL;
682 spin_unlock_irqrestore(&sch->lock, flags);
683 kfree (cdev->private);
684 kfree (cdev);
685 put_device(&sch->dev);
686 if (atomic_dec_and_test(&ccw_device_init_count))
687 wake_up(&ccw_device_init_wq);
688 return;
689 }
690
691 ret = subchannel_add_files(cdev->dev.parent);
692 if (ret)
693 printk(KERN_WARNING "%s: could not add attributes to %s\n",
694 __func__, sch->dev.bus_id);
695 put_device(&cdev->dev);
696out:
697 cdev->private->flags.recog_done = 1;
698 put_device(&sch->dev);
699 wake_up(&cdev->private->wait_q);
700 if (atomic_dec_and_test(&ccw_device_init_count))
701 wake_up(&ccw_device_init_wq);
702}
703
704void
705ccw_device_call_sch_unregister(void *data)
706{
707 struct ccw_device *cdev = data;
708 struct subchannel *sch;
709
710 sch = to_subchannel(cdev->dev.parent);
Cornelia Huck6ab48792006-07-12 16:39:50 +0200711 css_sch_device_unregister(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 /* Reset intparm to zeroes. */
713 sch->schib.pmcw.intparm = 0;
714 cio_modify(sch);
715 put_device(&cdev->dev);
716 put_device(&sch->dev);
717}
718
719/*
720 * subchannel recognition done. Called from the state machine.
721 */
722void
723io_subchannel_recog_done(struct ccw_device *cdev)
724{
725 struct subchannel *sch;
726
727 if (css_init_done == 0) {
728 cdev->private->flags.recog_done = 1;
729 return;
730 }
731 switch (cdev->private->state) {
732 case DEV_STATE_NOT_OPER:
733 cdev->private->flags.recog_done = 1;
734 /* Remove device found not operational. */
735 if (!get_device(&cdev->dev))
736 break;
737 sch = to_subchannel(cdev->dev.parent);
738 PREPARE_WORK(&cdev->private->kick_work,
739 ccw_device_call_sch_unregister, (void *) cdev);
740 queue_work(slow_path_wq, &cdev->private->kick_work);
741 if (atomic_dec_and_test(&ccw_device_init_count))
742 wake_up(&ccw_device_init_wq);
743 break;
744 case DEV_STATE_BOXED:
745 /* Device did not respond in time. */
746 case DEV_STATE_OFFLINE:
747 /*
748 * We can't register the device in interrupt context so
749 * we schedule a work item.
750 */
751 if (!get_device(&cdev->dev))
752 break;
753 PREPARE_WORK(&cdev->private->kick_work,
754 io_subchannel_register, (void *) cdev);
755 queue_work(slow_path_wq, &cdev->private->kick_work);
756 break;
757 }
758}
759
760static int
761io_subchannel_recog(struct ccw_device *cdev, struct subchannel *sch)
762{
763 int rc;
764 struct ccw_device_private *priv;
765
766 sch->dev.driver_data = cdev;
767 sch->driver = &io_subchannel_driver;
768 cdev->ccwlock = &sch->lock;
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800769
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 /* Init private data. */
771 priv = cdev->private;
772 priv->devno = sch->schib.pmcw.dev;
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800773 priv->ssid = sch->schid.ssid;
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800774 priv->sch_no = sch->schid.sch_no;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 priv->state = DEV_STATE_NOT_OPER;
776 INIT_LIST_HEAD(&priv->cmb_list);
777 init_waitqueue_head(&priv->wait_q);
778 init_timer(&priv->timer);
779
780 /* Set an initial name for the device. */
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800781 snprintf (cdev->dev.bus_id, BUS_ID_SIZE, "0.%x.%04x",
782 sch->schid.ssid, sch->schib.pmcw.dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783
784 /* Increase counter of devices currently in recognition. */
785 atomic_inc(&ccw_device_init_count);
786
787 /* Start async. device sensing. */
788 spin_lock_irq(&sch->lock);
789 rc = ccw_device_recognition(cdev);
790 spin_unlock_irq(&sch->lock);
791 if (rc) {
792 if (atomic_dec_and_test(&ccw_device_init_count))
793 wake_up(&ccw_device_init_wq);
794 }
795 return rc;
796}
797
798static int
Cornelia Huck8bbace72006-01-11 10:56:22 +0100799io_subchannel_probe (struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 struct ccw_device *cdev;
802 int rc;
803 unsigned long flags;
804
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 if (sch->dev.driver_data) {
806 /*
807 * This subchannel already has an associated ccw_device.
808 * Register it and exit. This happens for all early
809 * device, e.g. the console.
810 */
811 cdev = sch->dev.driver_data;
812 device_initialize(&cdev->dev);
813 ccw_device_register(cdev);
814 subchannel_add_files(&sch->dev);
815 /*
816 * Check if the device is already online. If it is
817 * the reference count needs to be corrected
818 * (see ccw_device_online and css_init_done for the
819 * ugly details).
820 */
821 if (cdev->private->state != DEV_STATE_NOT_OPER &&
822 cdev->private->state != DEV_STATE_OFFLINE &&
823 cdev->private->state != DEV_STATE_BOXED)
824 get_device(&cdev->dev);
825 return 0;
826 }
Eric Sesterhenn88abaab2006-03-24 03:15:31 -0800827 cdev = kzalloc (sizeof(*cdev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 if (!cdev)
829 return -ENOMEM;
Eric Sesterhenn88abaab2006-03-24 03:15:31 -0800830 cdev->private = kzalloc(sizeof(struct ccw_device_private),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 GFP_KERNEL | GFP_DMA);
832 if (!cdev->private) {
833 kfree(cdev);
834 return -ENOMEM;
835 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 atomic_set(&cdev->private->onoff, 0);
Heiko Carstens292888c2006-08-30 14:33:35 +0200837 cdev->dev.parent = &sch->dev;
838 cdev->dev.release = ccw_device_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 INIT_LIST_HEAD(&cdev->private->kick_work.entry);
840 /* Do first half of device_register. */
841 device_initialize(&cdev->dev);
842
843 if (!get_device(&sch->dev)) {
844 if (cdev->dev.release)
845 cdev->dev.release(&cdev->dev);
846 return -ENODEV;
847 }
848
Cornelia Huck8bbace72006-01-11 10:56:22 +0100849 rc = io_subchannel_recog(cdev, sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 if (rc) {
851 spin_lock_irqsave(&sch->lock, flags);
852 sch->dev.driver_data = NULL;
853 spin_unlock_irqrestore(&sch->lock, flags);
854 if (cdev->dev.release)
855 cdev->dev.release(&cdev->dev);
856 }
857
858 return rc;
859}
860
861static void
862ccw_device_unregister(void *data)
863{
864 struct ccw_device *cdev;
865
866 cdev = (struct ccw_device *)data;
867 if (test_and_clear_bit(1, &cdev->private->registered))
868 device_unregister(&cdev->dev);
869 put_device(&cdev->dev);
870}
871
872static int
Cornelia Huck8bbace72006-01-11 10:56:22 +0100873io_subchannel_remove (struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874{
875 struct ccw_device *cdev;
876 unsigned long flags;
877
Cornelia Huck8bbace72006-01-11 10:56:22 +0100878 if (!sch->dev.driver_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 return 0;
Cornelia Huck8bbace72006-01-11 10:56:22 +0100880 cdev = sch->dev.driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 /* Set ccw device to not operational and drop reference. */
882 spin_lock_irqsave(cdev->ccwlock, flags);
Cornelia Huck8bbace72006-01-11 10:56:22 +0100883 sch->dev.driver_data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 cdev->private->state = DEV_STATE_NOT_OPER;
885 spin_unlock_irqrestore(cdev->ccwlock, flags);
886 /*
887 * Put unregistration on workqueue to avoid livelocks on the css bus
888 * semaphore.
889 */
890 if (get_device(&cdev->dev)) {
891 PREPARE_WORK(&cdev->private->kick_work,
892 ccw_device_unregister, (void *) cdev);
893 queue_work(ccw_device_work, &cdev->private->kick_work);
894 }
895 return 0;
896}
897
898static int
899io_subchannel_notify(struct device *dev, int event)
900{
901 struct ccw_device *cdev;
902
903 cdev = dev->driver_data;
904 if (!cdev)
905 return 0;
906 if (!cdev->drv)
907 return 0;
908 if (!cdev->online)
909 return 0;
910 return cdev->drv->notify ? cdev->drv->notify(cdev, event) : 0;
911}
912
913static void
914io_subchannel_verify(struct device *dev)
915{
916 struct ccw_device *cdev;
917
918 cdev = dev->driver_data;
919 if (cdev)
920 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
921}
922
923static void
924io_subchannel_ioterm(struct device *dev)
925{
926 struct ccw_device *cdev;
927
928 cdev = dev->driver_data;
929 if (!cdev)
930 return;
931 cdev->private->state = DEV_STATE_CLEAR_VERIFY;
932 if (cdev->handler)
933 cdev->handler(cdev, cdev->private->intparm,
934 ERR_PTR(-EIO));
935}
936
937static void
Cornelia Huck8bbace72006-01-11 10:56:22 +0100938io_subchannel_shutdown(struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 struct ccw_device *cdev;
941 int ret;
942
Cornelia Huck8bbace72006-01-11 10:56:22 +0100943 cdev = sch->dev.driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800945 if (cio_is_console(sch->schid))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 return;
947 if (!sch->schib.pmcw.ena)
948 /* Nothing to do. */
949 return;
950 ret = cio_disable_subchannel(sch);
951 if (ret != -EBUSY)
952 /* Subchannel is disabled, we're done. */
953 return;
954 cdev->private->state = DEV_STATE_QUIESCE;
955 if (cdev->handler)
956 cdev->handler(cdev, cdev->private->intparm,
957 ERR_PTR(-EIO));
958 ret = ccw_device_cancel_halt_clear(cdev);
959 if (ret == -EBUSY) {
960 ccw_device_set_timeout(cdev, HZ/10);
961 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
962 }
963 cio_disable_subchannel(sch);
964}
965
966#ifdef CONFIG_CCW_CONSOLE
967static struct ccw_device console_cdev;
968static struct ccw_device_private console_private;
969static int console_cdev_in_use;
970
971static int
972ccw_device_console_enable (struct ccw_device *cdev, struct subchannel *sch)
973{
974 int rc;
975
976 /* Initialize the ccw_device structure. */
Heiko Carstens292888c2006-08-30 14:33:35 +0200977 cdev->dev.parent= &sch->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 rc = io_subchannel_recog(cdev, sch);
979 if (rc)
980 return rc;
981
982 /* Now wait for the async. recognition to come to an end. */
983 spin_lock_irq(cdev->ccwlock);
984 while (!dev_fsm_final_state(cdev))
985 wait_cons_dev();
986 rc = -EIO;
987 if (cdev->private->state != DEV_STATE_OFFLINE)
988 goto out_unlock;
989 ccw_device_online(cdev);
990 while (!dev_fsm_final_state(cdev))
991 wait_cons_dev();
992 if (cdev->private->state != DEV_STATE_ONLINE)
993 goto out_unlock;
994 rc = 0;
995out_unlock:
996 spin_unlock_irq(cdev->ccwlock);
997 return 0;
998}
999
1000struct ccw_device *
1001ccw_device_probe_console(void)
1002{
1003 struct subchannel *sch;
1004 int ret;
1005
1006 if (xchg(&console_cdev_in_use, 1) != 0)
Peter Oberparleiter600b5d12006-02-01 03:06:35 -08001007 return ERR_PTR(-EBUSY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 sch = cio_probe_console();
1009 if (IS_ERR(sch)) {
1010 console_cdev_in_use = 0;
1011 return (void *) sch;
1012 }
1013 memset(&console_cdev, 0, sizeof(struct ccw_device));
1014 memset(&console_private, 0, sizeof(struct ccw_device_private));
1015 console_cdev.private = &console_private;
1016 ret = ccw_device_console_enable(&console_cdev, sch);
1017 if (ret) {
1018 cio_release_console();
1019 console_cdev_in_use = 0;
1020 return ERR_PTR(ret);
1021 }
1022 console_cdev.online = 1;
1023 return &console_cdev;
1024}
1025#endif
1026
1027/*
1028 * get ccw_device matching the busid, but only if owned by cdrv
1029 */
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001030static int
1031__ccwdev_check_busid(struct device *dev, void *id)
1032{
1033 char *bus_id;
1034
1035 bus_id = (char *)id;
1036
1037 return (strncmp(bus_id, dev->bus_id, BUS_ID_SIZE) == 0);
1038}
1039
1040
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041struct ccw_device *
1042get_ccwdev_by_busid(struct ccw_driver *cdrv, const char *bus_id)
1043{
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001044 struct device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 struct device_driver *drv;
1046
1047 drv = get_driver(&cdrv->driver);
1048 if (!drv)
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001049 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001051 dev = driver_find_device(drv, NULL, (void *)bus_id,
1052 __ccwdev_check_busid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 put_driver(drv);
1054
Heiko Carstensd2c993d2006-07-12 16:41:55 +02001055 return dev ? to_ccwdev(dev) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056}
1057
1058/************************** device driver handling ************************/
1059
1060/* This is the implementation of the ccw_driver class. The probe, remove
1061 * and release methods are initially very similar to the device_driver
1062 * implementations, with the difference that they have ccw_device
1063 * arguments.
1064 *
1065 * A ccw driver also contains the information that is needed for
1066 * device matching.
1067 */
1068static int
1069ccw_device_probe (struct device *dev)
1070{
1071 struct ccw_device *cdev = to_ccwdev(dev);
1072 struct ccw_driver *cdrv = to_ccwdrv(dev->driver);
1073 int ret;
1074
1075 cdev->drv = cdrv; /* to let the driver call _set_online */
1076
1077 ret = cdrv->probe ? cdrv->probe(cdev) : -ENODEV;
1078
1079 if (ret) {
Heiko Carstensd2c993d2006-07-12 16:41:55 +02001080 cdev->drv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 return ret;
1082 }
1083
1084 return 0;
1085}
1086
1087static int
1088ccw_device_remove (struct device *dev)
1089{
1090 struct ccw_device *cdev = to_ccwdev(dev);
1091 struct ccw_driver *cdrv = cdev->drv;
1092 int ret;
1093
1094 pr_debug("removing device %s\n", cdev->dev.bus_id);
1095 if (cdrv->remove)
1096 cdrv->remove(cdev);
1097 if (cdev->online) {
1098 cdev->online = 0;
1099 spin_lock_irq(cdev->ccwlock);
1100 ret = ccw_device_offline(cdev);
1101 spin_unlock_irq(cdev->ccwlock);
1102 if (ret == 0)
1103 wait_event(cdev->private->wait_q,
1104 dev_fsm_final_state(cdev));
1105 else
1106 //FIXME: we can't fail!
1107 pr_debug("ccw_device_offline returned %d, device %s\n",
1108 ret, cdev->dev.bus_id);
1109 }
1110 ccw_device_set_timeout(cdev, 0);
Heiko Carstensd2c993d2006-07-12 16:41:55 +02001111 cdev->drv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 return 0;
1113}
1114
Cornelia Huck8bbace72006-01-11 10:56:22 +01001115struct bus_type ccw_bus_type = {
1116 .name = "ccw",
1117 .match = ccw_bus_match,
1118 .uevent = ccw_uevent,
1119 .probe = ccw_device_probe,
1120 .remove = ccw_device_remove,
1121};
1122
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123int
1124ccw_driver_register (struct ccw_driver *cdriver)
1125{
1126 struct device_driver *drv = &cdriver->driver;
1127
1128 drv->bus = &ccw_bus_type;
1129 drv->name = cdriver->name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130
1131 return driver_register(drv);
1132}
1133
1134void
1135ccw_driver_unregister (struct ccw_driver *cdriver)
1136{
1137 driver_unregister(&cdriver->driver);
1138}
1139
Cornelia Hucka8237fc2006-01-06 00:19:21 -08001140/* Helper func for qdio. */
1141struct subchannel_id
1142ccw_device_get_subchannel_id(struct ccw_device *cdev)
1143{
1144 struct subchannel *sch;
1145
1146 sch = to_subchannel(cdev->dev.parent);
1147 return sch->schid;
1148}
1149
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150MODULE_LICENSE("GPL");
1151EXPORT_SYMBOL(ccw_device_set_online);
1152EXPORT_SYMBOL(ccw_device_set_offline);
1153EXPORT_SYMBOL(ccw_driver_register);
1154EXPORT_SYMBOL(ccw_driver_unregister);
1155EXPORT_SYMBOL(get_ccwdev_by_busid);
1156EXPORT_SYMBOL(ccw_bus_type);
1157EXPORT_SYMBOL(ccw_device_work);
1158EXPORT_SYMBOL(ccw_device_notify_work);
Cornelia Hucka8237fc2006-01-06 00:19:21 -08001159EXPORT_SYMBOL_GPL(ccw_device_get_subchannel_id);