Merge branch 'for-linus' of git://git.alsa-project.org/alsa-kernel
[pandora-kernel.git] / drivers / s390 / char / vmur.c
index 04b19bd..49cba9e 100644 (file)
@@ -14,6 +14,7 @@
 #include <asm/cio.h>
 #include <asm/ccwdev.h>
 #include <asm/debug.h>
+#include <asm/diag.h>
 
 #include "vmur.h"
 
@@ -68,8 +69,26 @@ static struct ccw_driver ur_driver = {
        .set_offline    = ur_set_offline,
 };
 
+static DEFINE_MUTEX(vmur_mutex);
+
 /*
  * Allocation, freeing, getting and putting of urdev structures
+ *
+ * Each ur device (urd) contains a reference to its corresponding ccw device
+ * (cdev) using the urd->cdev pointer. Each ccw device has a reference to the
+ * ur device using the cdev->dev.driver_data pointer.
+ *
+ * urd references:
+ * - ur_probe gets a urd reference, ur_remove drops the reference
+ *   (cdev->dev.driver_data)
+ * - ur_open gets a urd reference, ur_relase drops the reference
+ *   (urf->urd)
+ *
+ * cdev references:
+ * - urdev_alloc get a cdev reference (urd->cdev)
+ * - urdev_free drops the cdev reference (urd->cdev)
+ *
+ * Setting and clearing of cdev->dev.driver_data is protected by the ccwdev lock
  */
 static struct urdev *urdev_alloc(struct ccw_device *cdev)
 {
@@ -78,42 +97,62 @@ static struct urdev *urdev_alloc(struct ccw_device *cdev)
        urd = kzalloc(sizeof(struct urdev), GFP_KERNEL);
        if (!urd)
                return NULL;
-       urd->cdev = cdev;
        urd->reclen = cdev->id.driver_info;
        ccw_device_get_id(cdev, &urd->dev_id);
        mutex_init(&urd->io_mutex);
-       mutex_init(&urd->open_mutex);
+       init_waitqueue_head(&urd->wait);
+       spin_lock_init(&urd->open_lock);
+       atomic_set(&urd->ref_count,  1);
+       urd->cdev = cdev;
+       get_device(&cdev->dev);
        return urd;
 }
 
 static void urdev_free(struct urdev *urd)
 {
+       TRACE("urdev_free: %p\n", urd);
+       if (urd->cdev)
+               put_device(&urd->cdev->dev);
        kfree(urd);
 }
 
-/*
- * This is how the character device driver gets a reference to a
- * ur device. When this call returns successfully, a reference has
- * been taken (by get_device) on the underlying kobject. The recipient
- * of this urdev pointer must eventually drop it with urdev_put(urd)
- * which does the corresponding put_device().
- */
+static void urdev_get(struct urdev *urd)
+{
+       atomic_inc(&urd->ref_count);
+}
+
+static struct urdev *urdev_get_from_cdev(struct ccw_device *cdev)
+{
+       struct urdev *urd;
+       unsigned long flags;
+
+       spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
+       urd = cdev->dev.driver_data;
+       if (urd)
+               urdev_get(urd);
+       spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
+       return urd;
+}
+
 static struct urdev *urdev_get_from_devno(u16 devno)
 {
        char bus_id[16];
        struct ccw_device *cdev;
+       struct urdev *urd;
 
        sprintf(bus_id, "0.0.%04x", devno);
        cdev = get_ccwdev_by_busid(&ur_driver, bus_id);
        if (!cdev)
                return NULL;
-
-       return cdev->dev.driver_data;
+       urd = urdev_get_from_cdev(cdev);
+       put_device(&cdev->dev);
+       return urd;
 }
 
 static void urdev_put(struct urdev *urd)
 {
-       put_device(&urd->cdev->dev);
+       if (atomic_dec_and_test(&urd->ref_count))
+               urdev_free(urd);
 }
 
 /*
@@ -238,17 +277,19 @@ static void ur_int_handler(struct ccw_device *cdev, unsigned long intparm,
        struct urdev *urd;
 
        TRACE("ur_int_handler: intparm=0x%lx cstat=%02x dstat=%02x res=%u\n",
-             intparm, irb->scsw.cstat, irb->scsw.dstat, irb->scsw.count);
+             intparm, irb->scsw.cmd.cstat, irb->scsw.cmd.dstat,
+             irb->scsw.cmd.count);
 
        if (!intparm) {
                TRACE("ur_int_handler: unsolicited interrupt\n");
                return;
        }
        urd = cdev->dev.driver_data;
+       BUG_ON(!urd);
        /* On special conditions irb is an error pointer */
        if (IS_ERR(irb))
                urd->io_request_rc = PTR_ERR(irb);
-       else if (irb->scsw.dstat == (DEV_STAT_CHN_END | DEV_STAT_DEV_END))
+       else if (irb->scsw.cmd.dstat == (DEV_STAT_CHN_END | DEV_STAT_DEV_END))
                urd->io_request_rc = 0;
        else
                urd->io_request_rc = -EIO;
@@ -262,9 +303,15 @@ static void ur_int_handler(struct ccw_device *cdev, unsigned long intparm,
 static ssize_t ur_attr_reclen_show(struct device *dev,
                                   struct device_attribute *attr, char *buf)
 {
-       struct urdev *urd = dev->driver_data;
+       struct urdev *urd;
+       int rc;
 
-       return sprintf(buf, "%zu\n", urd->reclen);
+       urd = urdev_get_from_cdev(to_ccwdev(dev));
+       if (!urd)
+               return -ENODEV;
+       rc = sprintf(buf, "%zu\n", urd->reclen);
+       urdev_put(urd);
+       return rc;
 }
 
 static DEVICE_ATTR(reclen, 0444, ur_attr_reclen_show, NULL);
@@ -379,31 +426,6 @@ static ssize_t ur_write(struct file *file, const char __user *udata,
        return do_write(urf->urd, udata, count, urf->dev_reclen, ppos);
 }
 
-static int do_diag_14(unsigned long rx, unsigned long ry1,
-                     unsigned long subcode)
-{
-       register unsigned long _ry1 asm("2") = ry1;
-       register unsigned long _ry2 asm("3") = subcode;
-       int rc = 0;
-
-       asm volatile(
-#ifdef CONFIG_64BIT
-               "   sam31\n"
-               "   diag    %2,2,0x14\n"
-               "   sam64\n"
-#else
-               "   diag    %2,2,0x14\n"
-#endif
-               "   ipm     %0\n"
-               "   srl     %0,28\n"
-               : "=d" (rc), "+d" (_ry2)
-               : "d" (rx), "d" (_ry1)
-               : "cc");
-
-       TRACE("diag 14: subcode=0x%lx, cc=%i\n", subcode, rc);
-       return rc;
-}
-
 /*
  * diagnose code 0x14 subcode 0x0028 - position spool file to designated
  *                                    record
@@ -415,7 +437,7 @@ static int diag_position_to_record(int devno, int record)
 {
        int cc;
 
-       cc = do_diag_14(record, devno, 0x28);
+       cc = diag14(record, devno, 0x28);
        switch (cc) {
        case 0:
                return 0;
@@ -440,7 +462,7 @@ static int diag_read_file(int devno, char *buf)
 {
        int cc;
 
-       cc = do_diag_14((unsigned long) buf, devno, 0x00);
+       cc = diag14((unsigned long) buf, devno, 0x00);
        switch (cc) {
        case 0:
                return 0;
@@ -533,7 +555,7 @@ static int diag_read_next_file_info(struct file_control_block *buf, int spid)
 {
        int cc;
 
-       cc = do_diag_14((unsigned long) buf, spid, 0xfff);
+       cc = diag14((unsigned long) buf, spid, 0xfff);
        switch (cc) {
        case 0:
                return 0;
@@ -658,17 +680,21 @@ static int ur_open(struct inode *inode, struct file *file)
        if (!urd)
                return -ENXIO;
 
-       if (file->f_flags & O_NONBLOCK) {
-               if (!mutex_trylock(&urd->open_mutex)) {
+       spin_lock(&urd->open_lock);
+       while (urd->open_flag) {
+               spin_unlock(&urd->open_lock);
+               if (file->f_flags & O_NONBLOCK) {
                        rc = -EBUSY;
                        goto fail_put;
                }
-       } else {
-               if (mutex_lock_interruptible(&urd->open_mutex)) {
+               if (wait_event_interruptible(urd->wait, urd->open_flag == 0)) {
                        rc = -ERESTARTSYS;
                        goto fail_put;
                }
+               spin_lock(&urd->open_lock);
        }
+       urd->open_flag++;
+       spin_unlock(&urd->open_lock);
 
        TRACE("ur_open\n");
 
@@ -700,7 +726,9 @@ static int ur_open(struct inode *inode, struct file *file)
 fail_urfile_free:
        urfile_free(urf);
 fail_unlock:
-       mutex_unlock(&urd->open_mutex);
+       spin_lock(&urd->open_lock);
+       urd->open_flag--;
+       spin_unlock(&urd->open_lock);
 fail_put:
        urdev_put(urd);
        return rc;
@@ -711,7 +739,10 @@ static int ur_release(struct inode *inode, struct file *file)
        struct urfile *urf = file->private_data;
 
        TRACE("ur_release\n");
-       mutex_unlock(&urf->urd->open_mutex);
+       spin_lock(&urf->urd->open_lock);
+       urf->urd->open_flag--;
+       spin_unlock(&urf->urd->open_lock);
+       wake_up_interruptible(&urf->urd->wait);
        urdev_put(urf->urd);
        urfile_free(urf);
        return 0;
@@ -739,7 +770,7 @@ static loff_t ur_llseek(struct file *file, loff_t offset, int whence)
        return newpos;
 }
 
-static struct file_operations ur_fops = {
+static const struct file_operations ur_fops = {
        .owner   = THIS_MODULE,
        .open    = ur_open,
        .release = ur_release,
@@ -750,64 +781,63 @@ static struct file_operations ur_fops = {
 
 /*
  * ccw_device infrastructure:
- *     ur_probe gets its own ref to the device (i.e. get_device),
- *     creates the struct urdev, the device attributes, sets up
- *     the interrupt handler and validates the virtual unit record device.
- *     ur_remove removes the device attributes, frees the struct urdev
- *     and drops (put_device) the ref to the device we got in ur_probe.
+ *     ur_probe creates the struct urdev (with refcount = 1), the device
+ *     attributes, sets up the interrupt handler and validates the virtual
+ *     unit record device.
+ *     ur_remove removes the device attributes and drops the reference to
+ *     struct urdev.
+ *
+ *     ur_probe, ur_remove, ur_set_online and ur_set_offline are serialized
+ *     by the vmur_mutex lock.
+ *
+ *     urd->char_device is used as indication that the online function has
+ *     been completed successfully.
  */
 static int ur_probe(struct ccw_device *cdev)
 {
        struct urdev *urd;
        int rc;
 
-       TRACE("ur_probe: cdev=%p state=%d\n", cdev, *(int *) cdev->private);
-
-       if (!get_device(&cdev->dev))
-               return -ENODEV;
+       TRACE("ur_probe: cdev=%p\n", cdev);
 
+       mutex_lock(&vmur_mutex);
        urd = urdev_alloc(cdev);
        if (!urd) {
                rc = -ENOMEM;
-               goto fail;
+               goto fail_unlock;
        }
+
        rc = ur_create_attributes(&cdev->dev);
        if (rc) {
                rc = -ENOMEM;
-               goto fail;
+               goto fail_urdev_put;
        }
-       cdev->dev.driver_data = urd;
        cdev->handler = ur_int_handler;
 
        /* validate virtual unit record device */
        urd->class = get_urd_class(urd);
        if (urd->class < 0) {
                rc = urd->class;
-               goto fail;
+               goto fail_remove_attr;
        }
        if ((urd->class != DEV_CLASS_UR_I) && (urd->class != DEV_CLASS_UR_O)) {
                rc = -ENOTSUPP;
-               goto fail;
+               goto fail_remove_attr;
        }
+       spin_lock_irq(get_ccwdev_lock(cdev));
+       cdev->dev.driver_data = urd;
+       spin_unlock_irq(get_ccwdev_lock(cdev));
 
+       mutex_unlock(&vmur_mutex);
        return 0;
 
-fail:
-       urdev_free(urd);
-       put_device(&cdev->dev);
-       return rc;
-}
-
-static void ur_remove(struct ccw_device *cdev)
-{
-       struct urdev *urd = cdev->dev.driver_data;
-
-       TRACE("ur_remove\n");
-       if (cdev->online)
-               ur_set_offline(cdev);
+fail_remove_attr:
        ur_remove_attributes(&cdev->dev);
-       urdev_free(urd);
-       put_device(&cdev->dev);
+fail_urdev_put:
+       urdev_put(urd);
+fail_unlock:
+       mutex_unlock(&vmur_mutex);
+       return rc;
 }
 
 static int ur_set_online(struct ccw_device *cdev)
@@ -816,20 +846,29 @@ static int ur_set_online(struct ccw_device *cdev)
        int minor, major, rc;
        char node_id[16];
 
-       TRACE("ur_set_online: cdev=%p state=%d\n", cdev,
-             *(int *) cdev->private);
+       TRACE("ur_set_online: cdev=%p\n", cdev);
 
-       if (!try_module_get(ur_driver.owner))
-               return -EINVAL;
+       mutex_lock(&vmur_mutex);
+       urd = urdev_get_from_cdev(cdev);
+       if (!urd) {
+               /* ur_remove already deleted our urd */
+               rc = -ENODEV;
+               goto fail_unlock;
+       }
+
+       if (urd->char_device) {
+               /* Another ur_set_online was faster */
+               rc = -EBUSY;
+               goto fail_urdev_put;
+       }
 
-       urd = (struct urdev *) cdev->dev.driver_data;
        minor = urd->dev_id.devno;
        major = MAJOR(ur_first_dev_maj_min);
 
        urd->char_device = cdev_alloc();
        if (!urd->char_device) {
                rc = -ENOMEM;
-               goto fail_module_put;
+               goto fail_urdev_put;
        }
 
        cdev_init(urd->char_device, &ur_fops);
@@ -858,29 +897,79 @@ static int ur_set_online(struct ccw_device *cdev)
                TRACE("ur_set_online: device_create rc=%d\n", rc);
                goto fail_free_cdev;
        }
-
+       urdev_put(urd);
+       mutex_unlock(&vmur_mutex);
        return 0;
 
 fail_free_cdev:
        cdev_del(urd->char_device);
-fail_module_put:
-       module_put(ur_driver.owner);
-
+       urd->char_device = NULL;
+fail_urdev_put:
+       urdev_put(urd);
+fail_unlock:
+       mutex_unlock(&vmur_mutex);
        return rc;
 }
 
-static int ur_set_offline(struct ccw_device *cdev)
+static int ur_set_offline_force(struct ccw_device *cdev, int force)
 {
        struct urdev *urd;
+       int rc;
 
-       TRACE("ur_set_offline: cdev=%p cdev->private=%p state=%d\n",
-               cdev, cdev->private, *(int *) cdev->private);
-       urd = (struct urdev *) cdev->dev.driver_data;
+       TRACE("ur_set_offline: cdev=%p\n", cdev);
+       urd = urdev_get_from_cdev(cdev);
+       if (!urd)
+               /* ur_remove already deleted our urd */
+               return -ENODEV;
+       if (!urd->char_device) {
+               /* Another ur_set_offline was faster */
+               rc = -EBUSY;
+               goto fail_urdev_put;
+       }
+       if (!force && (atomic_read(&urd->ref_count) > 2)) {
+               /* There is still a user of urd (e.g. ur_open) */
+               TRACE("ur_set_offline: BUSY\n");
+               rc = -EBUSY;
+               goto fail_urdev_put;
+       }
        device_destroy(vmur_class, urd->char_device->dev);
        cdev_del(urd->char_device);
-       module_put(ur_driver.owner);
+       urd->char_device = NULL;
+       rc = 0;
 
-       return 0;
+fail_urdev_put:
+       urdev_put(urd);
+       return rc;
+}
+
+static int ur_set_offline(struct ccw_device *cdev)
+{
+       int rc;
+
+       mutex_lock(&vmur_mutex);
+       rc = ur_set_offline_force(cdev, 0);
+       mutex_unlock(&vmur_mutex);
+       return rc;
+}
+
+static void ur_remove(struct ccw_device *cdev)
+{
+       unsigned long flags;
+
+       TRACE("ur_remove\n");
+
+       mutex_lock(&vmur_mutex);
+
+       if (cdev->online)
+               ur_set_offline_force(cdev, 1);
+       ur_remove_attributes(&cdev->dev);
+
+       spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
+       urdev_put(cdev->dev.driver_data);
+       cdev->dev.driver_data = NULL;
+       spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
+
+       mutex_unlock(&vmur_mutex);
 }
 
 /*