[S390] subchannel lock conversion.
[deliverable/linux.git] / drivers / s390 / cio / device.c
1 /*
2 * drivers/s390/cio/device.c
3 * bus driver for ccw devices
4 *
5 * Copyright (C) 2002 IBM Deutschland Entwicklung GmbH,
6 * IBM Corporation
7 * Author(s): Arnd Bergmann (arndb@de.ibm.com)
8 * Cornelia Huck (cornelia.huck@de.ibm.com)
9 * Martin Schwidefsky (schwidefsky@de.ibm.com)
10 */
11 #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>
23 #include <asm/param.h> /* HZ */
24
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. */
36 static int
37 ccw_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 /* Store modalias string delimited by prefix/suffix string into buffer with
56 * specified size. Return length of resulting string (excluding trailing '\0')
57 * even if string doesn't fit buffer (snprintf semantics). */
58 static int snprint_alias(char *buf, size_t size, const char *prefix,
59 struct ccw_device_id *id, const char *suffix)
60 {
61 int len;
62
63 len = snprintf(buf, size, "%sccw:t%04Xm%02X", prefix, id->cu_type,
64 id->cu_model);
65 if (len > size)
66 return len;
67 buf += len;
68 size -= len;
69
70 if (id->dev_type != 0)
71 len += snprintf(buf, size, "dt%04Xdm%02X%s", id->dev_type,
72 id->dev_model, suffix);
73 else
74 len += snprintf(buf, size, "dtdm%s", suffix);
75
76 return len;
77 }
78
79 /* Set up environment variables for ccw device uevent. Return 0 on success,
80 * non-zero otherwise. */
81 static int ccw_uevent(struct device *dev, char **envp, int num_envp,
82 char *buffer, int buffer_size)
83 {
84 struct ccw_device *cdev = to_ccwdev(dev);
85 struct ccw_device_id *id = &(cdev->id);
86 int i = 0;
87 int len;
88
89 /* CU_TYPE= */
90 len = snprintf(buffer, buffer_size, "CU_TYPE=%04X", id->cu_type) + 1;
91 if (len > buffer_size || i >= num_envp)
92 return -ENOMEM;
93 envp[i++] = buffer;
94 buffer += len;
95 buffer_size -= len;
96
97 /* CU_MODEL= */
98 len = snprintf(buffer, buffer_size, "CU_MODEL=%02X", id->cu_model) + 1;
99 if (len > buffer_size || i >= num_envp)
100 return -ENOMEM;
101 envp[i++] = buffer;
102 buffer += len;
103 buffer_size -= len;
104
105 /* The next two can be zero, that's ok for us */
106 /* DEV_TYPE= */
107 len = snprintf(buffer, buffer_size, "DEV_TYPE=%04X", id->dev_type) + 1;
108 if (len > buffer_size || i >= num_envp)
109 return -ENOMEM;
110 envp[i++] = buffer;
111 buffer += len;
112 buffer_size -= len;
113
114 /* DEV_MODEL= */
115 len = snprintf(buffer, buffer_size, "DEV_MODEL=%02X",
116 (unsigned char) id->dev_model) + 1;
117 if (len > buffer_size || i >= num_envp)
118 return -ENOMEM;
119 envp[i++] = buffer;
120 buffer += len;
121 buffer_size -= len;
122
123 /* MODALIAS= */
124 len = snprint_alias(buffer, buffer_size, "MODALIAS=", id, "") + 1;
125 if (len > buffer_size || i >= num_envp)
126 return -ENOMEM;
127 envp[i++] = buffer;
128 buffer += len;
129 buffer_size -= len;
130
131 envp[i] = NULL;
132
133 return 0;
134 }
135
136 struct bus_type ccw_bus_type;
137
138 static int io_subchannel_probe (struct subchannel *);
139 static int io_subchannel_remove (struct subchannel *);
140 void io_subchannel_irq (struct device *);
141 static int io_subchannel_notify(struct device *, int);
142 static void io_subchannel_verify(struct device *);
143 static void io_subchannel_ioterm(struct device *);
144 static void io_subchannel_shutdown(struct subchannel *);
145
146 struct css_driver io_subchannel_driver = {
147 .subchannel_type = SUBCHANNEL_TYPE_IO,
148 .drv = {
149 .name = "io_subchannel",
150 .bus = &css_bus_type,
151 },
152 .irq = io_subchannel_irq,
153 .notify = io_subchannel_notify,
154 .verify = io_subchannel_verify,
155 .termination = io_subchannel_ioterm,
156 .probe = io_subchannel_probe,
157 .remove = io_subchannel_remove,
158 .shutdown = io_subchannel_shutdown,
159 };
160
161 struct workqueue_struct *ccw_device_work;
162 struct workqueue_struct *ccw_device_notify_work;
163 wait_queue_head_t ccw_device_init_wq;
164 atomic_t ccw_device_init_count;
165
166 static int __init
167 init_ccw_bus_type (void)
168 {
169 int ret;
170
171 init_waitqueue_head(&ccw_device_init_wq);
172 atomic_set(&ccw_device_init_count, 0);
173
174 ccw_device_work = create_singlethread_workqueue("cio");
175 if (!ccw_device_work)
176 return -ENOMEM; /* FIXME: better errno ? */
177 ccw_device_notify_work = create_singlethread_workqueue("cio_notify");
178 if (!ccw_device_notify_work) {
179 ret = -ENOMEM; /* FIXME: better errno ? */
180 goto out_err;
181 }
182 slow_path_wq = create_singlethread_workqueue("kslowcrw");
183 if (!slow_path_wq) {
184 ret = -ENOMEM; /* FIXME: better errno ? */
185 goto out_err;
186 }
187 if ((ret = bus_register (&ccw_bus_type)))
188 goto out_err;
189
190 if ((ret = driver_register(&io_subchannel_driver.drv)))
191 goto out_err;
192
193 wait_event(ccw_device_init_wq,
194 atomic_read(&ccw_device_init_count) == 0);
195 flush_workqueue(ccw_device_work);
196 return 0;
197 out_err:
198 if (ccw_device_work)
199 destroy_workqueue(ccw_device_work);
200 if (ccw_device_notify_work)
201 destroy_workqueue(ccw_device_notify_work);
202 if (slow_path_wq)
203 destroy_workqueue(slow_path_wq);
204 return ret;
205 }
206
207 static void __exit
208 cleanup_ccw_bus_type (void)
209 {
210 driver_unregister(&io_subchannel_driver.drv);
211 bus_unregister(&ccw_bus_type);
212 destroy_workqueue(ccw_device_notify_work);
213 destroy_workqueue(ccw_device_work);
214 }
215
216 subsys_initcall(init_ccw_bus_type);
217 module_exit(cleanup_ccw_bus_type);
218
219 /************************ device handling **************************/
220
221 /*
222 * A ccw_device has some interfaces in sysfs in addition to the
223 * standard ones.
224 * The following entries are designed to export the information which
225 * resided in 2.4 in /proc/subchannels. Subchannel and device number
226 * are obvious, so they don't have an entry :)
227 * TODO: Split chpids and pimpampom up? Where is "in use" in the tree?
228 */
229 static ssize_t
230 chpids_show (struct device * dev, struct device_attribute *attr, char * buf)
231 {
232 struct subchannel *sch = to_subchannel(dev);
233 struct ssd_info *ssd = &sch->ssd_info;
234 ssize_t ret = 0;
235 int chp;
236
237 for (chp = 0; chp < 8; chp++)
238 ret += sprintf (buf+ret, "%02x ", ssd->chpid[chp]);
239
240 ret += sprintf (buf+ret, "\n");
241 return min((ssize_t)PAGE_SIZE, ret);
242 }
243
244 static ssize_t
245 pimpampom_show (struct device * dev, struct device_attribute *attr, char * buf)
246 {
247 struct subchannel *sch = to_subchannel(dev);
248 struct pmcw *pmcw = &sch->schib.pmcw;
249
250 return sprintf (buf, "%02x %02x %02x\n",
251 pmcw->pim, pmcw->pam, pmcw->pom);
252 }
253
254 static ssize_t
255 devtype_show (struct device *dev, struct device_attribute *attr, char *buf)
256 {
257 struct ccw_device *cdev = to_ccwdev(dev);
258 struct ccw_device_id *id = &(cdev->id);
259
260 if (id->dev_type != 0)
261 return sprintf(buf, "%04x/%02x\n",
262 id->dev_type, id->dev_model);
263 else
264 return sprintf(buf, "n/a\n");
265 }
266
267 static ssize_t
268 cutype_show (struct device *dev, struct device_attribute *attr, char *buf)
269 {
270 struct ccw_device *cdev = to_ccwdev(dev);
271 struct ccw_device_id *id = &(cdev->id);
272
273 return sprintf(buf, "%04x/%02x\n",
274 id->cu_type, id->cu_model);
275 }
276
277 static ssize_t
278 modalias_show (struct device *dev, struct device_attribute *attr, char *buf)
279 {
280 struct ccw_device *cdev = to_ccwdev(dev);
281 struct ccw_device_id *id = &(cdev->id);
282 int len;
283
284 len = snprint_alias(buf, PAGE_SIZE, "", id, "\n") + 1;
285
286 return len > PAGE_SIZE ? PAGE_SIZE : len;
287 }
288
289 static ssize_t
290 online_show (struct device *dev, struct device_attribute *attr, char *buf)
291 {
292 struct ccw_device *cdev = to_ccwdev(dev);
293
294 return sprintf(buf, cdev->online ? "1\n" : "0\n");
295 }
296
297 static void ccw_device_unregister(struct work_struct *work)
298 {
299 struct ccw_device_private *priv;
300 struct ccw_device *cdev;
301
302 priv = container_of(work, struct ccw_device_private, kick_work);
303 cdev = priv->cdev;
304 if (test_and_clear_bit(1, &cdev->private->registered))
305 device_unregister(&cdev->dev);
306 put_device(&cdev->dev);
307 }
308
309 static void
310 ccw_device_remove_disconnected(struct ccw_device *cdev)
311 {
312 struct subchannel *sch;
313 /*
314 * Forced offline in disconnected state means
315 * 'throw away device'.
316 */
317 sch = to_subchannel(cdev->dev.parent);
318 css_sch_device_unregister(sch);
319 /* Reset intparm to zeroes. */
320 sch->schib.pmcw.intparm = 0;
321 cio_modify(sch);
322 put_device(&sch->dev);
323 }
324
325 int
326 ccw_device_set_offline(struct ccw_device *cdev)
327 {
328 int ret;
329
330 if (!cdev)
331 return -ENODEV;
332 if (!cdev->online || !cdev->drv)
333 return -EINVAL;
334
335 if (cdev->drv->set_offline) {
336 ret = cdev->drv->set_offline(cdev);
337 if (ret != 0)
338 return ret;
339 }
340 cdev->online = 0;
341 spin_lock_irq(cdev->ccwlock);
342 ret = ccw_device_offline(cdev);
343 if (ret == -ENODEV) {
344 if (cdev->private->state != DEV_STATE_NOT_OPER) {
345 cdev->private->state = DEV_STATE_OFFLINE;
346 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
347 }
348 spin_unlock_irq(cdev->ccwlock);
349 return ret;
350 }
351 spin_unlock_irq(cdev->ccwlock);
352 if (ret == 0)
353 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
354 else {
355 pr_debug("ccw_device_offline returned %d, device %s\n",
356 ret, cdev->dev.bus_id);
357 cdev->online = 1;
358 }
359 return ret;
360 }
361
362 int
363 ccw_device_set_online(struct ccw_device *cdev)
364 {
365 int ret;
366
367 if (!cdev)
368 return -ENODEV;
369 if (cdev->online || !cdev->drv)
370 return -EINVAL;
371
372 spin_lock_irq(cdev->ccwlock);
373 ret = ccw_device_online(cdev);
374 spin_unlock_irq(cdev->ccwlock);
375 if (ret == 0)
376 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
377 else {
378 pr_debug("ccw_device_online returned %d, device %s\n",
379 ret, cdev->dev.bus_id);
380 return ret;
381 }
382 if (cdev->private->state != DEV_STATE_ONLINE)
383 return -ENODEV;
384 if (!cdev->drv->set_online || cdev->drv->set_online(cdev) == 0) {
385 cdev->online = 1;
386 return 0;
387 }
388 spin_lock_irq(cdev->ccwlock);
389 ret = ccw_device_offline(cdev);
390 spin_unlock_irq(cdev->ccwlock);
391 if (ret == 0)
392 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
393 else
394 pr_debug("ccw_device_offline returned %d, device %s\n",
395 ret, cdev->dev.bus_id);
396 return (ret == 0) ? -ENODEV : ret;
397 }
398
399 static ssize_t
400 online_store (struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
401 {
402 struct ccw_device *cdev = to_ccwdev(dev);
403 int i, force, ret;
404 char *tmp;
405
406 if (atomic_cmpxchg(&cdev->private->onoff, 0, 1) != 0)
407 return -EAGAIN;
408
409 if (cdev->drv && !try_module_get(cdev->drv->owner)) {
410 atomic_set(&cdev->private->onoff, 0);
411 return -EINVAL;
412 }
413 if (!strncmp(buf, "force\n", count)) {
414 force = 1;
415 i = 1;
416 } else {
417 force = 0;
418 i = simple_strtoul(buf, &tmp, 16);
419 }
420 if (i == 1) {
421 /* Do device recognition, if needed. */
422 if (cdev->id.cu_type == 0) {
423 ret = ccw_device_recognition(cdev);
424 if (ret) {
425 printk(KERN_WARNING"Couldn't start recognition "
426 "for device %s (ret=%d)\n",
427 cdev->dev.bus_id, ret);
428 goto out;
429 }
430 wait_event(cdev->private->wait_q,
431 cdev->private->flags.recog_done);
432 }
433 if (cdev->drv && cdev->drv->set_online)
434 ccw_device_set_online(cdev);
435 } else if (i == 0) {
436 if (cdev->private->state == DEV_STATE_DISCONNECTED)
437 ccw_device_remove_disconnected(cdev);
438 else if (cdev->drv && cdev->drv->set_offline)
439 ccw_device_set_offline(cdev);
440 }
441 if (force && cdev->private->state == DEV_STATE_BOXED) {
442 ret = ccw_device_stlck(cdev);
443 if (ret) {
444 printk(KERN_WARNING"ccw_device_stlck for device %s "
445 "returned %d!\n", cdev->dev.bus_id, ret);
446 goto out;
447 }
448 /* Do device recognition, if needed. */
449 if (cdev->id.cu_type == 0) {
450 cdev->private->state = DEV_STATE_NOT_OPER;
451 ret = ccw_device_recognition(cdev);
452 if (ret) {
453 printk(KERN_WARNING"Couldn't start recognition "
454 "for device %s (ret=%d)\n",
455 cdev->dev.bus_id, ret);
456 goto out;
457 }
458 wait_event(cdev->private->wait_q,
459 cdev->private->flags.recog_done);
460 }
461 if (cdev->drv && cdev->drv->set_online)
462 ccw_device_set_online(cdev);
463 }
464 out:
465 if (cdev->drv)
466 module_put(cdev->drv->owner);
467 atomic_set(&cdev->private->onoff, 0);
468 return count;
469 }
470
471 static ssize_t
472 available_show (struct device *dev, struct device_attribute *attr, char *buf)
473 {
474 struct ccw_device *cdev = to_ccwdev(dev);
475 struct subchannel *sch;
476
477 switch (cdev->private->state) {
478 case DEV_STATE_BOXED:
479 return sprintf(buf, "boxed\n");
480 case DEV_STATE_DISCONNECTED:
481 case DEV_STATE_DISCONNECTED_SENSE_ID:
482 case DEV_STATE_NOT_OPER:
483 sch = to_subchannel(dev->parent);
484 if (!sch->lpm)
485 return sprintf(buf, "no path\n");
486 else
487 return sprintf(buf, "no device\n");
488 default:
489 /* All other states considered fine. */
490 return sprintf(buf, "good\n");
491 }
492 }
493
494 static DEVICE_ATTR(chpids, 0444, chpids_show, NULL);
495 static DEVICE_ATTR(pimpampom, 0444, pimpampom_show, NULL);
496 static DEVICE_ATTR(devtype, 0444, devtype_show, NULL);
497 static DEVICE_ATTR(cutype, 0444, cutype_show, NULL);
498 static DEVICE_ATTR(modalias, 0444, modalias_show, NULL);
499 static DEVICE_ATTR(online, 0644, online_show, online_store);
500 extern struct device_attribute dev_attr_cmb_enable;
501 static DEVICE_ATTR(availability, 0444, available_show, NULL);
502
503 static struct attribute * subch_attrs[] = {
504 &dev_attr_chpids.attr,
505 &dev_attr_pimpampom.attr,
506 NULL,
507 };
508
509 static struct attribute_group subch_attr_group = {
510 .attrs = subch_attrs,
511 };
512
513 int subchannel_add_files (struct device *dev)
514 {
515 return sysfs_create_group(&dev->kobj, &subch_attr_group);
516 }
517
518 static struct attribute * ccwdev_attrs[] = {
519 &dev_attr_devtype.attr,
520 &dev_attr_cutype.attr,
521 &dev_attr_modalias.attr,
522 &dev_attr_online.attr,
523 &dev_attr_cmb_enable.attr,
524 &dev_attr_availability.attr,
525 NULL,
526 };
527
528 static struct attribute_group ccwdev_attr_group = {
529 .attrs = ccwdev_attrs,
530 };
531
532 static inline int
533 device_add_files (struct device *dev)
534 {
535 return sysfs_create_group(&dev->kobj, &ccwdev_attr_group);
536 }
537
538 static inline void
539 device_remove_files(struct device *dev)
540 {
541 sysfs_remove_group(&dev->kobj, &ccwdev_attr_group);
542 }
543
544 /* this is a simple abstraction for device_register that sets the
545 * correct bus type and adds the bus specific files */
546 static int ccw_device_register(struct ccw_device *cdev)
547 {
548 struct device *dev = &cdev->dev;
549 int ret;
550
551 dev->bus = &ccw_bus_type;
552
553 if ((ret = device_add(dev)))
554 return ret;
555
556 set_bit(1, &cdev->private->registered);
557 if ((ret = device_add_files(dev))) {
558 if (test_and_clear_bit(1, &cdev->private->registered))
559 device_del(dev);
560 }
561 return ret;
562 }
563
564 struct match_data {
565 struct ccw_dev_id dev_id;
566 struct ccw_device * sibling;
567 };
568
569 static int
570 match_devno(struct device * dev, void * data)
571 {
572 struct match_data * d = data;
573 struct ccw_device * cdev;
574
575 cdev = to_ccwdev(dev);
576 if ((cdev->private->state == DEV_STATE_DISCONNECTED) &&
577 ccw_dev_id_is_equal(&cdev->private->dev_id, &d->dev_id) &&
578 (cdev != d->sibling)) {
579 cdev->private->state = DEV_STATE_NOT_OPER;
580 return 1;
581 }
582 return 0;
583 }
584
585 static struct ccw_device * get_disc_ccwdev_by_dev_id(struct ccw_dev_id *dev_id,
586 struct ccw_device *sibling)
587 {
588 struct device *dev;
589 struct match_data data;
590
591 data.dev_id = *dev_id;
592 data.sibling = sibling;
593 dev = bus_find_device(&ccw_bus_type, NULL, &data, match_devno);
594
595 return dev ? to_ccwdev(dev) : NULL;
596 }
597
598 static void
599 ccw_device_add_changed(struct work_struct *work)
600 {
601 struct ccw_device_private *priv;
602 struct ccw_device *cdev;
603
604 priv = container_of(work, struct ccw_device_private, kick_work);
605 cdev = priv->cdev;
606 if (device_add(&cdev->dev)) {
607 put_device(&cdev->dev);
608 return;
609 }
610 set_bit(1, &cdev->private->registered);
611 if (device_add_files(&cdev->dev)) {
612 if (test_and_clear_bit(1, &cdev->private->registered))
613 device_unregister(&cdev->dev);
614 }
615 }
616
617 extern int css_get_ssd_info(struct subchannel *sch);
618
619 void
620 ccw_device_do_unreg_rereg(struct work_struct *work)
621 {
622 struct ccw_device_private *priv;
623 struct ccw_device *cdev;
624 struct subchannel *sch;
625 int need_rename;
626
627 priv = container_of(work, struct ccw_device_private, kick_work);
628 cdev = priv->cdev;
629 sch = to_subchannel(cdev->dev.parent);
630 if (cdev->private->dev_id.devno != sch->schib.pmcw.dev) {
631 /*
632 * The device number has changed. This is usually only when
633 * a device has been detached under VM and then re-appeared
634 * on another subchannel because of a different attachment
635 * order than before. Ideally, we should should just switch
636 * subchannels, but unfortunately, this is not possible with
637 * the current implementation.
638 * Instead, we search for the old subchannel for this device
639 * number and deregister so there are no collisions with the
640 * newly registered ccw_device.
641 * FIXME: Find another solution so the block layer doesn't
642 * get possibly sick...
643 */
644 struct ccw_device *other_cdev;
645 struct ccw_dev_id dev_id;
646
647 need_rename = 1;
648 dev_id.devno = sch->schib.pmcw.dev;
649 dev_id.ssid = sch->schid.ssid;
650 other_cdev = get_disc_ccwdev_by_dev_id(&dev_id, cdev);
651 if (other_cdev) {
652 struct subchannel *other_sch;
653
654 other_sch = to_subchannel(other_cdev->dev.parent);
655 if (get_device(&other_sch->dev)) {
656 stsch(other_sch->schid, &other_sch->schib);
657 if (other_sch->schib.pmcw.dnv) {
658 other_sch->schib.pmcw.intparm = 0;
659 cio_modify(other_sch);
660 }
661 css_sch_device_unregister(other_sch);
662 }
663 }
664 /* Update ssd info here. */
665 css_get_ssd_info(sch);
666 cdev->private->dev_id.devno = sch->schib.pmcw.dev;
667 } else
668 need_rename = 0;
669 device_remove_files(&cdev->dev);
670 if (test_and_clear_bit(1, &cdev->private->registered))
671 device_del(&cdev->dev);
672 if (need_rename)
673 snprintf (cdev->dev.bus_id, BUS_ID_SIZE, "0.%x.%04x",
674 sch->schid.ssid, sch->schib.pmcw.dev);
675 PREPARE_WORK(&cdev->private->kick_work,
676 ccw_device_add_changed);
677 queue_work(ccw_device_work, &cdev->private->kick_work);
678 }
679
680 static void
681 ccw_device_release(struct device *dev)
682 {
683 struct ccw_device *cdev;
684
685 cdev = to_ccwdev(dev);
686 kfree(cdev->private);
687 kfree(cdev);
688 }
689
690 static struct ccw_device * io_subchannel_allocate_dev(struct subchannel *sch)
691 {
692 struct ccw_device *cdev;
693
694 cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
695 if (cdev) {
696 cdev->private = kzalloc(sizeof(struct ccw_device_private),
697 GFP_KERNEL | GFP_DMA);
698 if (cdev->private)
699 return cdev;
700 }
701 kfree(cdev);
702 return ERR_PTR(-ENOMEM);
703 }
704
705 static int io_subchannel_initialize_dev(struct subchannel *sch,
706 struct ccw_device *cdev)
707 {
708 cdev->private->cdev = cdev;
709 atomic_set(&cdev->private->onoff, 0);
710 cdev->dev.parent = &sch->dev;
711 cdev->dev.release = ccw_device_release;
712 INIT_LIST_HEAD(&cdev->private->kick_work.entry);
713 /* Do first half of device_register. */
714 device_initialize(&cdev->dev);
715 if (!get_device(&sch->dev)) {
716 if (cdev->dev.release)
717 cdev->dev.release(&cdev->dev);
718 return -ENODEV;
719 }
720 return 0;
721 }
722
723 static struct ccw_device * io_subchannel_create_ccwdev(struct subchannel *sch)
724 {
725 struct ccw_device *cdev;
726 int ret;
727
728 cdev = io_subchannel_allocate_dev(sch);
729 if (!IS_ERR(cdev)) {
730 ret = io_subchannel_initialize_dev(sch, cdev);
731 if (ret) {
732 kfree(cdev);
733 cdev = ERR_PTR(ret);
734 }
735 }
736 return cdev;
737 }
738
739 /*
740 * Register recognized device.
741 */
742 static void
743 io_subchannel_register(struct work_struct *work)
744 {
745 struct ccw_device_private *priv;
746 struct ccw_device *cdev;
747 struct subchannel *sch;
748 int ret;
749 unsigned long flags;
750
751 priv = container_of(work, struct ccw_device_private, kick_work);
752 cdev = priv->cdev;
753 sch = to_subchannel(cdev->dev.parent);
754
755 /*
756 * io_subchannel_register() will also be called after device
757 * recognition has been done for a boxed device (which will already
758 * be registered). We need to reprobe since we may now have sense id
759 * information.
760 */
761 if (klist_node_attached(&cdev->dev.knode_parent)) {
762 if (!cdev->drv) {
763 ret = device_reprobe(&cdev->dev);
764 if (ret)
765 /* We can't do much here. */
766 dev_info(&cdev->dev, "device_reprobe() returned"
767 " %d\n", ret);
768 }
769 goto out;
770 }
771 /* make it known to the system */
772 ret = ccw_device_register(cdev);
773 if (ret) {
774 printk (KERN_WARNING "%s: could not register %s\n",
775 __func__, cdev->dev.bus_id);
776 put_device(&cdev->dev);
777 spin_lock_irqsave(sch->lock, flags);
778 sch->dev.driver_data = NULL;
779 spin_unlock_irqrestore(sch->lock, flags);
780 kfree (cdev->private);
781 kfree (cdev);
782 put_device(&sch->dev);
783 if (atomic_dec_and_test(&ccw_device_init_count))
784 wake_up(&ccw_device_init_wq);
785 return;
786 }
787 put_device(&cdev->dev);
788 out:
789 cdev->private->flags.recog_done = 1;
790 put_device(&sch->dev);
791 wake_up(&cdev->private->wait_q);
792 if (atomic_dec_and_test(&ccw_device_init_count))
793 wake_up(&ccw_device_init_wq);
794 }
795
796 void
797 ccw_device_call_sch_unregister(struct work_struct *work)
798 {
799 struct ccw_device_private *priv;
800 struct ccw_device *cdev;
801 struct subchannel *sch;
802
803 priv = container_of(work, struct ccw_device_private, kick_work);
804 cdev = priv->cdev;
805 sch = to_subchannel(cdev->dev.parent);
806 css_sch_device_unregister(sch);
807 /* Reset intparm to zeroes. */
808 sch->schib.pmcw.intparm = 0;
809 cio_modify(sch);
810 put_device(&cdev->dev);
811 put_device(&sch->dev);
812 }
813
814 /*
815 * subchannel recognition done. Called from the state machine.
816 */
817 void
818 io_subchannel_recog_done(struct ccw_device *cdev)
819 {
820 struct subchannel *sch;
821
822 if (css_init_done == 0) {
823 cdev->private->flags.recog_done = 1;
824 return;
825 }
826 switch (cdev->private->state) {
827 case DEV_STATE_NOT_OPER:
828 cdev->private->flags.recog_done = 1;
829 /* Remove device found not operational. */
830 if (!get_device(&cdev->dev))
831 break;
832 sch = to_subchannel(cdev->dev.parent);
833 PREPARE_WORK(&cdev->private->kick_work,
834 ccw_device_call_sch_unregister);
835 queue_work(slow_path_wq, &cdev->private->kick_work);
836 if (atomic_dec_and_test(&ccw_device_init_count))
837 wake_up(&ccw_device_init_wq);
838 break;
839 case DEV_STATE_BOXED:
840 /* Device did not respond in time. */
841 case DEV_STATE_OFFLINE:
842 /*
843 * We can't register the device in interrupt context so
844 * we schedule a work item.
845 */
846 if (!get_device(&cdev->dev))
847 break;
848 PREPARE_WORK(&cdev->private->kick_work,
849 io_subchannel_register);
850 queue_work(slow_path_wq, &cdev->private->kick_work);
851 break;
852 }
853 }
854
855 static int
856 io_subchannel_recog(struct ccw_device *cdev, struct subchannel *sch)
857 {
858 int rc;
859 struct ccw_device_private *priv;
860
861 sch->dev.driver_data = cdev;
862 sch->driver = &io_subchannel_driver;
863 cdev->ccwlock = sch->lock;
864
865 /* Init private data. */
866 priv = cdev->private;
867 priv->dev_id.devno = sch->schib.pmcw.dev;
868 priv->dev_id.ssid = sch->schid.ssid;
869 priv->schid = sch->schid;
870 priv->state = DEV_STATE_NOT_OPER;
871 INIT_LIST_HEAD(&priv->cmb_list);
872 init_waitqueue_head(&priv->wait_q);
873 init_timer(&priv->timer);
874
875 /* Set an initial name for the device. */
876 snprintf (cdev->dev.bus_id, BUS_ID_SIZE, "0.%x.%04x",
877 sch->schid.ssid, sch->schib.pmcw.dev);
878
879 /* Increase counter of devices currently in recognition. */
880 atomic_inc(&ccw_device_init_count);
881
882 /* Start async. device sensing. */
883 spin_lock_irq(sch->lock);
884 rc = ccw_device_recognition(cdev);
885 spin_unlock_irq(sch->lock);
886 if (rc) {
887 if (atomic_dec_and_test(&ccw_device_init_count))
888 wake_up(&ccw_device_init_wq);
889 }
890 return rc;
891 }
892
893 static int
894 io_subchannel_probe (struct subchannel *sch)
895 {
896 struct ccw_device *cdev;
897 int rc;
898 unsigned long flags;
899
900 if (sch->dev.driver_data) {
901 /*
902 * This subchannel already has an associated ccw_device.
903 * Register it and exit. This happens for all early
904 * device, e.g. the console.
905 */
906 cdev = sch->dev.driver_data;
907 device_initialize(&cdev->dev);
908 ccw_device_register(cdev);
909 /*
910 * Check if the device is already online. If it is
911 * the reference count needs to be corrected
912 * (see ccw_device_online and css_init_done for the
913 * ugly details).
914 */
915 if (cdev->private->state != DEV_STATE_NOT_OPER &&
916 cdev->private->state != DEV_STATE_OFFLINE &&
917 cdev->private->state != DEV_STATE_BOXED)
918 get_device(&cdev->dev);
919 return 0;
920 }
921 cdev = io_subchannel_create_ccwdev(sch);
922 if (IS_ERR(cdev))
923 return PTR_ERR(cdev);
924
925 rc = io_subchannel_recog(cdev, sch);
926 if (rc) {
927 spin_lock_irqsave(sch->lock, flags);
928 sch->dev.driver_data = NULL;
929 spin_unlock_irqrestore(sch->lock, flags);
930 if (cdev->dev.release)
931 cdev->dev.release(&cdev->dev);
932 }
933
934 return rc;
935 }
936
937 static int
938 io_subchannel_remove (struct subchannel *sch)
939 {
940 struct ccw_device *cdev;
941 unsigned long flags;
942
943 if (!sch->dev.driver_data)
944 return 0;
945 cdev = sch->dev.driver_data;
946 /* Set ccw device to not operational and drop reference. */
947 spin_lock_irqsave(cdev->ccwlock, flags);
948 sch->dev.driver_data = NULL;
949 cdev->private->state = DEV_STATE_NOT_OPER;
950 spin_unlock_irqrestore(cdev->ccwlock, flags);
951 /*
952 * Put unregistration on workqueue to avoid livelocks on the css bus
953 * semaphore.
954 */
955 if (get_device(&cdev->dev)) {
956 PREPARE_WORK(&cdev->private->kick_work,
957 ccw_device_unregister);
958 queue_work(ccw_device_work, &cdev->private->kick_work);
959 }
960 return 0;
961 }
962
963 static int
964 io_subchannel_notify(struct device *dev, int event)
965 {
966 struct ccw_device *cdev;
967
968 cdev = dev->driver_data;
969 if (!cdev)
970 return 0;
971 if (!cdev->drv)
972 return 0;
973 if (!cdev->online)
974 return 0;
975 return cdev->drv->notify ? cdev->drv->notify(cdev, event) : 0;
976 }
977
978 static void
979 io_subchannel_verify(struct device *dev)
980 {
981 struct ccw_device *cdev;
982
983 cdev = dev->driver_data;
984 if (cdev)
985 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
986 }
987
988 static void
989 io_subchannel_ioterm(struct device *dev)
990 {
991 struct ccw_device *cdev;
992
993 cdev = dev->driver_data;
994 if (!cdev)
995 return;
996 /* Internal I/O will be retried by the interrupt handler. */
997 if (cdev->private->flags.intretry)
998 return;
999 cdev->private->state = DEV_STATE_CLEAR_VERIFY;
1000 if (cdev->handler)
1001 cdev->handler(cdev, cdev->private->intparm,
1002 ERR_PTR(-EIO));
1003 }
1004
1005 static void
1006 io_subchannel_shutdown(struct subchannel *sch)
1007 {
1008 struct ccw_device *cdev;
1009 int ret;
1010
1011 cdev = sch->dev.driver_data;
1012
1013 if (cio_is_console(sch->schid))
1014 return;
1015 if (!sch->schib.pmcw.ena)
1016 /* Nothing to do. */
1017 return;
1018 ret = cio_disable_subchannel(sch);
1019 if (ret != -EBUSY)
1020 /* Subchannel is disabled, we're done. */
1021 return;
1022 cdev->private->state = DEV_STATE_QUIESCE;
1023 if (cdev->handler)
1024 cdev->handler(cdev, cdev->private->intparm,
1025 ERR_PTR(-EIO));
1026 ret = ccw_device_cancel_halt_clear(cdev);
1027 if (ret == -EBUSY) {
1028 ccw_device_set_timeout(cdev, HZ/10);
1029 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
1030 }
1031 cio_disable_subchannel(sch);
1032 }
1033
1034 #ifdef CONFIG_CCW_CONSOLE
1035 static struct ccw_device console_cdev;
1036 static struct ccw_device_private console_private;
1037 static int console_cdev_in_use;
1038
1039 static DEFINE_SPINLOCK(ccw_console_lock);
1040
1041 spinlock_t * cio_get_console_lock(void)
1042 {
1043 return &ccw_console_lock;
1044 }
1045
1046 static int
1047 ccw_device_console_enable (struct ccw_device *cdev, struct subchannel *sch)
1048 {
1049 int rc;
1050
1051 /* Initialize the ccw_device structure. */
1052 cdev->dev.parent= &sch->dev;
1053 rc = io_subchannel_recog(cdev, sch);
1054 if (rc)
1055 return rc;
1056
1057 /* Now wait for the async. recognition to come to an end. */
1058 spin_lock_irq(cdev->ccwlock);
1059 while (!dev_fsm_final_state(cdev))
1060 wait_cons_dev();
1061 rc = -EIO;
1062 if (cdev->private->state != DEV_STATE_OFFLINE)
1063 goto out_unlock;
1064 ccw_device_online(cdev);
1065 while (!dev_fsm_final_state(cdev))
1066 wait_cons_dev();
1067 if (cdev->private->state != DEV_STATE_ONLINE)
1068 goto out_unlock;
1069 rc = 0;
1070 out_unlock:
1071 spin_unlock_irq(cdev->ccwlock);
1072 return 0;
1073 }
1074
1075 struct ccw_device *
1076 ccw_device_probe_console(void)
1077 {
1078 struct subchannel *sch;
1079 int ret;
1080
1081 if (xchg(&console_cdev_in_use, 1) != 0)
1082 return ERR_PTR(-EBUSY);
1083 sch = cio_probe_console();
1084 if (IS_ERR(sch)) {
1085 console_cdev_in_use = 0;
1086 return (void *) sch;
1087 }
1088 memset(&console_cdev, 0, sizeof(struct ccw_device));
1089 memset(&console_private, 0, sizeof(struct ccw_device_private));
1090 console_cdev.private = &console_private;
1091 console_private.cdev = &console_cdev;
1092 ret = ccw_device_console_enable(&console_cdev, sch);
1093 if (ret) {
1094 cio_release_console();
1095 console_cdev_in_use = 0;
1096 return ERR_PTR(ret);
1097 }
1098 console_cdev.online = 1;
1099 return &console_cdev;
1100 }
1101 #endif
1102
1103 /*
1104 * get ccw_device matching the busid, but only if owned by cdrv
1105 */
1106 static int
1107 __ccwdev_check_busid(struct device *dev, void *id)
1108 {
1109 char *bus_id;
1110
1111 bus_id = id;
1112
1113 return (strncmp(bus_id, dev->bus_id, BUS_ID_SIZE) == 0);
1114 }
1115
1116
1117 struct ccw_device *
1118 get_ccwdev_by_busid(struct ccw_driver *cdrv, const char *bus_id)
1119 {
1120 struct device *dev;
1121 struct device_driver *drv;
1122
1123 drv = get_driver(&cdrv->driver);
1124 if (!drv)
1125 return NULL;
1126
1127 dev = driver_find_device(drv, NULL, (void *)bus_id,
1128 __ccwdev_check_busid);
1129 put_driver(drv);
1130
1131 return dev ? to_ccwdev(dev) : NULL;
1132 }
1133
1134 /************************** device driver handling ************************/
1135
1136 /* This is the implementation of the ccw_driver class. The probe, remove
1137 * and release methods are initially very similar to the device_driver
1138 * implementations, with the difference that they have ccw_device
1139 * arguments.
1140 *
1141 * A ccw driver also contains the information that is needed for
1142 * device matching.
1143 */
1144 static int
1145 ccw_device_probe (struct device *dev)
1146 {
1147 struct ccw_device *cdev = to_ccwdev(dev);
1148 struct ccw_driver *cdrv = to_ccwdrv(dev->driver);
1149 int ret;
1150
1151 cdev->drv = cdrv; /* to let the driver call _set_online */
1152
1153 ret = cdrv->probe ? cdrv->probe(cdev) : -ENODEV;
1154
1155 if (ret) {
1156 cdev->drv = NULL;
1157 return ret;
1158 }
1159
1160 return 0;
1161 }
1162
1163 static int
1164 ccw_device_remove (struct device *dev)
1165 {
1166 struct ccw_device *cdev = to_ccwdev(dev);
1167 struct ccw_driver *cdrv = cdev->drv;
1168 int ret;
1169
1170 pr_debug("removing device %s\n", cdev->dev.bus_id);
1171 if (cdrv->remove)
1172 cdrv->remove(cdev);
1173 if (cdev->online) {
1174 cdev->online = 0;
1175 spin_lock_irq(cdev->ccwlock);
1176 ret = ccw_device_offline(cdev);
1177 spin_unlock_irq(cdev->ccwlock);
1178 if (ret == 0)
1179 wait_event(cdev->private->wait_q,
1180 dev_fsm_final_state(cdev));
1181 else
1182 //FIXME: we can't fail!
1183 pr_debug("ccw_device_offline returned %d, device %s\n",
1184 ret, cdev->dev.bus_id);
1185 }
1186 ccw_device_set_timeout(cdev, 0);
1187 cdev->drv = NULL;
1188 return 0;
1189 }
1190
1191 struct bus_type ccw_bus_type = {
1192 .name = "ccw",
1193 .match = ccw_bus_match,
1194 .uevent = ccw_uevent,
1195 .probe = ccw_device_probe,
1196 .remove = ccw_device_remove,
1197 };
1198
1199 int
1200 ccw_driver_register (struct ccw_driver *cdriver)
1201 {
1202 struct device_driver *drv = &cdriver->driver;
1203
1204 drv->bus = &ccw_bus_type;
1205 drv->name = cdriver->name;
1206
1207 return driver_register(drv);
1208 }
1209
1210 void
1211 ccw_driver_unregister (struct ccw_driver *cdriver)
1212 {
1213 driver_unregister(&cdriver->driver);
1214 }
1215
1216 /* Helper func for qdio. */
1217 struct subchannel_id
1218 ccw_device_get_subchannel_id(struct ccw_device *cdev)
1219 {
1220 struct subchannel *sch;
1221
1222 sch = to_subchannel(cdev->dev.parent);
1223 return sch->schid;
1224 }
1225
1226 MODULE_LICENSE("GPL");
1227 EXPORT_SYMBOL(ccw_device_set_online);
1228 EXPORT_SYMBOL(ccw_device_set_offline);
1229 EXPORT_SYMBOL(ccw_driver_register);
1230 EXPORT_SYMBOL(ccw_driver_unregister);
1231 EXPORT_SYMBOL(get_ccwdev_by_busid);
1232 EXPORT_SYMBOL(ccw_bus_type);
1233 EXPORT_SYMBOL(ccw_device_work);
1234 EXPORT_SYMBOL(ccw_device_notify_work);
1235 EXPORT_SYMBOL_GPL(ccw_device_get_subchannel_id);
This page took 0.056747 seconds and 6 git commands to generate.