Merge branch 'classmate' into release
[safe/jmp/linux-2.6] / drivers / s390 / cio / device.c
index 6a9ac85..a6c7d54 100644 (file)
@@ -303,13 +303,19 @@ int ccw_device_is_orphan(struct ccw_device *cdev)
 
 static void ccw_device_unregister(struct ccw_device *cdev)
 {
-       if (test_and_clear_bit(1, &cdev->private->registered)) {
+       if (device_is_registered(&cdev->dev)) {
+               /* Undo device_add(). */
                device_del(&cdev->dev);
+       }
+       if (cdev->private->flags.initialized) {
+               cdev->private->flags.initialized = 0;
                /* Release reference from device_initialize(). */
                put_device(&cdev->dev);
        }
 }
 
+static void io_subchannel_quiesce(struct subchannel *);
+
 /**
  * ccw_device_set_offline() - disable a ccw device for I/O
  * @cdev: target ccw device
@@ -323,7 +329,8 @@ static void ccw_device_unregister(struct ccw_device *cdev)
  */
 int ccw_device_set_offline(struct ccw_device *cdev)
 {
-       int ret;
+       struct subchannel *sch;
+       int ret, state;
 
        if (!cdev)
                return -ENODEV;
@@ -337,6 +344,7 @@ int ccw_device_set_offline(struct ccw_device *cdev)
        }
        cdev->online = 0;
        spin_lock_irq(cdev->ccwlock);
+       sch = to_subchannel(cdev->dev.parent);
        /* Wait until a final state or DISCONNECTED is reached */
        while (!dev_fsm_final_state(cdev) &&
               cdev->private->state != DEV_STATE_DISCONNECTED) {
@@ -345,9 +353,21 @@ int ccw_device_set_offline(struct ccw_device *cdev)
                           cdev->private->state == DEV_STATE_DISCONNECTED));
                spin_lock_irq(cdev->ccwlock);
        }
-       ret = ccw_device_offline(cdev);
-       if (ret)
-               goto error;
+       do {
+               ret = ccw_device_offline(cdev);
+               if (!ret)
+                       break;
+               CIO_MSG_EVENT(0, "ccw_device_offline returned %d, device "
+                             "0.%x.%04x\n", ret, cdev->private->dev_id.ssid,
+                             cdev->private->dev_id.devno);
+               if (ret != -EBUSY)
+                       goto error;
+               state = cdev->private->state;
+               spin_unlock_irq(cdev->ccwlock);
+               io_subchannel_quiesce(sch);
+               spin_lock_irq(cdev->ccwlock);
+               cdev->private->state = state;
+       } while (ret == -EBUSY);
        spin_unlock_irq(cdev->ccwlock);
        wait_event(cdev->private->wait_q, (dev_fsm_final_state(cdev) ||
                   cdev->private->state == DEV_STATE_DISCONNECTED));
@@ -364,9 +384,6 @@ int ccw_device_set_offline(struct ccw_device *cdev)
        return 0;
 
 error:
-       CIO_MSG_EVENT(0, "ccw_device_offline returned %d, device 0.%x.%04x\n",
-                     ret, cdev->private->dev_id.ssid,
-                     cdev->private->dev_id.devno);
        cdev->private->state = DEV_STATE_OFFLINE;
        dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
        spin_unlock_irq(cdev->ccwlock);
@@ -529,11 +546,10 @@ static ssize_t online_store (struct device *dev, struct device_attribute *attr,
        int force, ret;
        unsigned long i;
 
-       if ((cdev->private->state != DEV_STATE_OFFLINE &&
-            cdev->private->state != DEV_STATE_ONLINE &&
-            cdev->private->state != DEV_STATE_BOXED &&
-            cdev->private->state != DEV_STATE_DISCONNECTED) ||
-           atomic_cmpxchg(&cdev->private->onoff, 0, 1) != 0)
+       if (!dev_fsm_final_state(cdev) &&
+           cdev->private->state != DEV_STATE_DISCONNECTED)
+               return -EAGAIN;
+       if (atomic_cmpxchg(&cdev->private->onoff, 0, 1) != 0)
                return -EAGAIN;
 
        if (cdev->drv && !try_module_get(cdev->drv->owner)) {
@@ -641,12 +657,7 @@ static int ccw_device_register(struct ccw_device *cdev)
                           cdev->private->dev_id.devno);
        if (ret)
                return ret;
-       ret = device_add(dev);
-       if (ret)
-               return ret;
-
-       set_bit(1, &cdev->private->registered);
-       return ret;
+       return device_add(dev);
 }
 
 static int match_dev_id(struct device *dev, void *data)
@@ -670,7 +681,7 @@ static void ccw_device_do_unbind_bind(struct ccw_device *cdev)
 {
        int ret;
 
-       if (test_bit(1, &cdev->private->registered)) {
+       if (device_is_registered(&cdev->dev)) {
                device_release_driver(&cdev->dev);
                ret = device_attach(&cdev->dev);
                WARN_ON(ret == -ENODEV);
@@ -722,6 +733,7 @@ static int io_subchannel_initialize_dev(struct subchannel *sch,
                put_device(&cdev->dev);
                return -ENODEV;
        }
+       cdev->private->flags.initialized = 1;
        return 0;
 }
 
@@ -893,12 +905,27 @@ static int ccw_device_move_to_sch(struct ccw_device *cdev,
                                  struct subchannel *sch)
 {
        struct subchannel *old_sch;
-       int rc;
+       int rc, old_enabled = 0;
 
        old_sch = to_subchannel(cdev->dev.parent);
        /* Obtain child reference for new parent. */
        if (!get_device(&sch->dev))
                return -ENODEV;
+
+       if (!sch_is_pseudo_sch(old_sch)) {
+               spin_lock_irq(old_sch->lock);
+               old_enabled = old_sch->schib.pmcw.ena;
+               rc = 0;
+               if (old_enabled)
+                       rc = cio_disable_subchannel(old_sch);
+               spin_unlock_irq(old_sch->lock);
+               if (rc == -EBUSY) {
+                       /* Release child reference for new parent. */
+                       put_device(&sch->dev);
+                       return rc;
+               }
+       }
+
        mutex_lock(&sch->reg_mutex);
        rc = device_move(&cdev->dev, &sch->dev, DPM_ORDER_PARENT_BEFORE_DEV);
        mutex_unlock(&sch->reg_mutex);
@@ -907,6 +934,12 @@ static int ccw_device_move_to_sch(struct ccw_device *cdev,
                              cdev->private->dev_id.ssid,
                              cdev->private->dev_id.devno, sch->schid.ssid,
                              sch->schib.pmcw.dev, rc);
+               if (old_enabled) {
+                       /* Try to reenable the old subchannel. */
+                       spin_lock_irq(old_sch->lock);
+                       cio_enable_subchannel(old_sch, (u32)(addr_t)old_sch);
+                       spin_unlock_irq(old_sch->lock);
+               }
                /* Release child reference for new parent. */
                put_device(&sch->dev);
                return rc;
@@ -915,7 +948,6 @@ static int ccw_device_move_to_sch(struct ccw_device *cdev,
        if (!sch_is_pseudo_sch(old_sch)) {
                spin_lock_irq(old_sch->lock);
                sch_set_cdev(old_sch, NULL);
-               cio_disable_subchannel(old_sch);
                spin_unlock_irq(old_sch->lock);
                css_schedule_eval(old_sch->schid);
        }
@@ -1004,6 +1036,7 @@ static int io_subchannel_probe(struct subchannel *sch)
                cdev = sch_get_cdev(sch);
                cdev->dev.groups = ccwdev_attr_groups;
                device_initialize(&cdev->dev);
+               cdev->private->flags.initialized = 1;
                ccw_device_register(cdev);
                /*
                 * Check if the device is already online. If it is
@@ -1043,16 +1076,16 @@ static int
 io_subchannel_remove (struct subchannel *sch)
 {
        struct ccw_device *cdev;
-       unsigned long flags;
 
        cdev = sch_get_cdev(sch);
        if (!cdev)
                goto out_free;
+       io_subchannel_quiesce(sch);
        /* Set ccw device to not operational and drop reference. */
-       spin_lock_irqsave(cdev->ccwlock, flags);
+       spin_lock_irq(cdev->ccwlock);
        sch_set_cdev(sch, NULL);
        cdev->private->state = DEV_STATE_NOT_OPER;
-       spin_unlock_irqrestore(cdev->ccwlock, flags);
+       spin_unlock_irq(cdev->ccwlock);
        ccw_device_unregister(cdev);
 out_free:
        kfree(sch->private);
@@ -1069,36 +1102,6 @@ static void io_subchannel_verify(struct subchannel *sch)
                dev_fsm_event(cdev, DEV_EVENT_VERIFY);
 }
 
-static int check_for_io_on_path(struct subchannel *sch, int mask)
-{
-       if (cio_update_schib(sch))
-               return 0;
-       if (scsw_actl(&sch->schib.scsw) && sch->schib.pmcw.lpum == mask)
-               return 1;
-       return 0;
-}
-
-static void terminate_internal_io(struct subchannel *sch,
-                                 struct ccw_device *cdev)
-{
-       if (cio_clear(sch)) {
-               /* Recheck device in case clear failed. */
-               sch->lpm = 0;
-               if (cdev->online)
-                       dev_fsm_event(cdev, DEV_EVENT_VERIFY);
-               else
-                       css_schedule_eval(sch->schid);
-               return;
-       }
-       cdev->private->state = DEV_STATE_CLEAR_VERIFY;
-       /* Request retry of internal operation. */
-       cdev->private->flags.intretry = 1;
-       /* Call handler. */
-       if (cdev->handler)
-               cdev->handler(cdev, cdev->private->intparm,
-                             ERR_PTR(-EIO));
-}
-
 static void io_subchannel_terminate_path(struct subchannel *sch, u8 mask)
 {
        struct ccw_device *cdev;
@@ -1106,18 +1109,24 @@ static void io_subchannel_terminate_path(struct subchannel *sch, u8 mask)
        cdev = sch_get_cdev(sch);
        if (!cdev)
                return;
-       if (check_for_io_on_path(sch, mask)) {
-               if (cdev->private->state == DEV_STATE_ONLINE)
-                       ccw_device_kill_io(cdev);
-               else {
-                       terminate_internal_io(sch, cdev);
-                       /* Re-start path verification. */
-                       dev_fsm_event(cdev, DEV_EVENT_VERIFY);
-               }
-       } else
-               /* trigger path verification. */
-               dev_fsm_event(cdev, DEV_EVENT_VERIFY);
+       if (cio_update_schib(sch))
+               goto err;
+       /* Check for I/O on path. */
+       if (scsw_actl(&sch->schib.scsw) == 0 || sch->schib.pmcw.lpum != mask)
+               goto out;
+       if (cdev->private->state == DEV_STATE_ONLINE) {
+               ccw_device_kill_io(cdev);
+               goto out;
+       }
+       if (cio_clear(sch))
+               goto err;
+out:
+       /* Trigger path verification. */
+       dev_fsm_event(cdev, DEV_EVENT_VERIFY);
+       return;
 
+err:
+       dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
 }
 
 static int io_subchannel_chp_event(struct subchannel *sch,
@@ -1154,33 +1163,41 @@ static int io_subchannel_chp_event(struct subchannel *sch,
        return 0;
 }
 
-static void
-io_subchannel_shutdown(struct subchannel *sch)
+static void io_subchannel_quiesce(struct subchannel *sch)
 {
        struct ccw_device *cdev;
        int ret;
 
+       spin_lock_irq(sch->lock);
        cdev = sch_get_cdev(sch);
-
        if (cio_is_console(sch->schid))
-               return;
+               goto out_unlock;
        if (!sch->schib.pmcw.ena)
-               /* Nothing to do. */
-               return;
+               goto out_unlock;
        ret = cio_disable_subchannel(sch);
        if (ret != -EBUSY)
-               /* Subchannel is disabled, we're done. */
-               return;
-       cdev->private->state = DEV_STATE_QUIESCE;
+               goto out_unlock;
        if (cdev->handler)
-               cdev->handler(cdev, cdev->private->intparm,
-                             ERR_PTR(-EIO));
-       ret = ccw_device_cancel_halt_clear(cdev);
-       if (ret == -EBUSY) {
-               ccw_device_set_timeout(cdev, HZ/10);
-               wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
+               cdev->handler(cdev, cdev->private->intparm, ERR_PTR(-EIO));
+       while (ret == -EBUSY) {
+               cdev->private->state = DEV_STATE_QUIESCE;
+               ret = ccw_device_cancel_halt_clear(cdev);
+               if (ret == -EBUSY) {
+                       ccw_device_set_timeout(cdev, HZ/10);
+                       spin_unlock_irq(sch->lock);
+                       wait_event(cdev->private->wait_q,
+                                  cdev->private->state != DEV_STATE_QUIESCE);
+                       spin_lock_irq(sch->lock);
+               }
+               ret = cio_disable_subchannel(sch);
        }
-       cio_disable_subchannel(sch);
+out_unlock:
+       spin_unlock_irq(sch->lock);
+}
+
+static void io_subchannel_shutdown(struct subchannel *sch)
+{
+       io_subchannel_quiesce(sch);
 }
 
 static int device_is_disconnected(struct ccw_device *cdev)
@@ -1502,6 +1519,7 @@ static int ccw_device_console_enable(struct ccw_device *cdev,
        sch->driver = &io_subchannel_driver;
        /* Initialize the ccw_device structure. */
        cdev->dev.parent= &sch->dev;
+       sch_set_cdev(sch, cdev);
        io_subchannel_recog(cdev, sch);
        /* Now wait for the async. recognition to come to an end. */
        spin_lock_irq(cdev->ccwlock);
@@ -1887,7 +1905,7 @@ out_unlock:
        return ret;
 }
 
-static struct dev_pm_ops ccw_pm_ops = {
+static const struct dev_pm_ops ccw_pm_ops = {
        .prepare = ccw_device_pm_prepare,
        .complete = ccw_device_pm_complete,
        .freeze = ccw_device_pm_freeze,