aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/s390/cio/device.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/s390/cio/device.c')
-rw-r--r--drivers/s390/cio/device.c1006
1 files changed, 417 insertions, 589 deletions
diff --git a/drivers/s390/cio/device.c b/drivers/s390/cio/device.c
index 2490b741e16a..9fecfb4223a8 100644
--- a/drivers/s390/cio/device.c
+++ b/drivers/s390/cio/device.c
@@ -7,6 +7,10 @@
7 * Cornelia Huck (cornelia.huck@de.ibm.com) 7 * Cornelia Huck (cornelia.huck@de.ibm.com)
8 * Martin Schwidefsky (schwidefsky@de.ibm.com) 8 * Martin Schwidefsky (schwidefsky@de.ibm.com)
9 */ 9 */
10
11#define KMSG_COMPONENT "cio"
12#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
13
10#include <linux/module.h> 14#include <linux/module.h>
11#include <linux/init.h> 15#include <linux/init.h>
12#include <linux/spinlock.h> 16#include <linux/spinlock.h>
@@ -299,53 +303,18 @@ int ccw_device_is_orphan(struct ccw_device *cdev)
299 303
300static void ccw_device_unregister(struct ccw_device *cdev) 304static void ccw_device_unregister(struct ccw_device *cdev)
301{ 305{
302 if (test_and_clear_bit(1, &cdev->private->registered)) { 306 if (device_is_registered(&cdev->dev)) {
307 /* Undo device_add(). */
303 device_del(&cdev->dev); 308 device_del(&cdev->dev);
309 }
310 if (cdev->private->flags.initialized) {
311 cdev->private->flags.initialized = 0;
304 /* Release reference from device_initialize(). */ 312 /* Release reference from device_initialize(). */
305 put_device(&cdev->dev); 313 put_device(&cdev->dev);
306 } 314 }
307} 315}
308 316
309static void ccw_device_remove_orphan_cb(struct work_struct *work) 317static void io_subchannel_quiesce(struct subchannel *);
310{
311 struct ccw_device_private *priv;
312 struct ccw_device *cdev;
313
314 priv = container_of(work, struct ccw_device_private, kick_work);
315 cdev = priv->cdev;
316 ccw_device_unregister(cdev);
317 /* Release cdev reference for workqueue processing. */
318 put_device(&cdev->dev);
319}
320
321static void
322ccw_device_remove_disconnected(struct ccw_device *cdev)
323{
324 unsigned long flags;
325
326 /*
327 * Forced offline in disconnected state means
328 * 'throw away device'.
329 */
330 if (ccw_device_is_orphan(cdev)) {
331 /*
332 * Deregister ccw device.
333 * Unfortunately, we cannot do this directly from the
334 * attribute method.
335 */
336 /* Get cdev reference for workqueue processing. */
337 if (!get_device(&cdev->dev))
338 return;
339 spin_lock_irqsave(cdev->ccwlock, flags);
340 cdev->private->state = DEV_STATE_NOT_OPER;
341 spin_unlock_irqrestore(cdev->ccwlock, flags);
342 PREPARE_WORK(&cdev->private->kick_work,
343 ccw_device_remove_orphan_cb);
344 queue_work(slow_path_wq, &cdev->private->kick_work);
345 } else
346 /* Deregister subchannel, which will kill the ccw device. */
347 ccw_device_schedule_sch_unregister(cdev);
348}
349 318
350/** 319/**
351 * ccw_device_set_offline() - disable a ccw device for I/O 320 * ccw_device_set_offline() - disable a ccw device for I/O
@@ -360,7 +329,8 @@ ccw_device_remove_disconnected(struct ccw_device *cdev)
360 */ 329 */
361int ccw_device_set_offline(struct ccw_device *cdev) 330int ccw_device_set_offline(struct ccw_device *cdev)
362{ 331{
363 int ret; 332 struct subchannel *sch;
333 int ret, state;
364 334
365 if (!cdev) 335 if (!cdev)
366 return -ENODEV; 336 return -ENODEV;
@@ -374,6 +344,7 @@ int ccw_device_set_offline(struct ccw_device *cdev)
374 } 344 }
375 cdev->online = 0; 345 cdev->online = 0;
376 spin_lock_irq(cdev->ccwlock); 346 spin_lock_irq(cdev->ccwlock);
347 sch = to_subchannel(cdev->dev.parent);
377 /* Wait until a final state or DISCONNECTED is reached */ 348 /* Wait until a final state or DISCONNECTED is reached */
378 while (!dev_fsm_final_state(cdev) && 349 while (!dev_fsm_final_state(cdev) &&
379 cdev->private->state != DEV_STATE_DISCONNECTED) { 350 cdev->private->state != DEV_STATE_DISCONNECTED) {
@@ -382,20 +353,37 @@ int ccw_device_set_offline(struct ccw_device *cdev)
382 cdev->private->state == DEV_STATE_DISCONNECTED)); 353 cdev->private->state == DEV_STATE_DISCONNECTED));
383 spin_lock_irq(cdev->ccwlock); 354 spin_lock_irq(cdev->ccwlock);
384 } 355 }
385 ret = ccw_device_offline(cdev); 356 do {
386 if (ret) 357 ret = ccw_device_offline(cdev);
387 goto error; 358 if (!ret)
359 break;
360 CIO_MSG_EVENT(0, "ccw_device_offline returned %d, device "
361 "0.%x.%04x\n", ret, cdev->private->dev_id.ssid,
362 cdev->private->dev_id.devno);
363 if (ret != -EBUSY)
364 goto error;
365 state = cdev->private->state;
366 spin_unlock_irq(cdev->ccwlock);
367 io_subchannel_quiesce(sch);
368 spin_lock_irq(cdev->ccwlock);
369 cdev->private->state = state;
370 } while (ret == -EBUSY);
388 spin_unlock_irq(cdev->ccwlock); 371 spin_unlock_irq(cdev->ccwlock);
389 wait_event(cdev->private->wait_q, (dev_fsm_final_state(cdev) || 372 wait_event(cdev->private->wait_q, (dev_fsm_final_state(cdev) ||
390 cdev->private->state == DEV_STATE_DISCONNECTED)); 373 cdev->private->state == DEV_STATE_DISCONNECTED));
374 /* Inform the user if set offline failed. */
375 if (cdev->private->state == DEV_STATE_BOXED) {
376 pr_warning("%s: The device entered boxed state while "
377 "being set offline\n", dev_name(&cdev->dev));
378 } else if (cdev->private->state == DEV_STATE_NOT_OPER) {
379 pr_warning("%s: The device stopped operating while "
380 "being set offline\n", dev_name(&cdev->dev));
381 }
391 /* Give up reference from ccw_device_set_online(). */ 382 /* Give up reference from ccw_device_set_online(). */
392 put_device(&cdev->dev); 383 put_device(&cdev->dev);
393 return 0; 384 return 0;
394 385
395error: 386error:
396 CIO_MSG_EVENT(0, "ccw_device_offline returned %d, device 0.%x.%04x\n",
397 ret, cdev->private->dev_id.ssid,
398 cdev->private->dev_id.devno);
399 cdev->private->state = DEV_STATE_OFFLINE; 387 cdev->private->state = DEV_STATE_OFFLINE;
400 dev_fsm_event(cdev, DEV_EVENT_NOTOPER); 388 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
401 spin_unlock_irq(cdev->ccwlock); 389 spin_unlock_irq(cdev->ccwlock);
@@ -448,6 +436,16 @@ int ccw_device_set_online(struct ccw_device *cdev)
448 if ((cdev->private->state != DEV_STATE_ONLINE) && 436 if ((cdev->private->state != DEV_STATE_ONLINE) &&
449 (cdev->private->state != DEV_STATE_W4SENSE)) { 437 (cdev->private->state != DEV_STATE_W4SENSE)) {
450 spin_unlock_irq(cdev->ccwlock); 438 spin_unlock_irq(cdev->ccwlock);
439 /* Inform the user that set online failed. */
440 if (cdev->private->state == DEV_STATE_BOXED) {
441 pr_warning("%s: Setting the device online failed "
442 "because it is boxed\n",
443 dev_name(&cdev->dev));
444 } else if (cdev->private->state == DEV_STATE_NOT_OPER) {
445 pr_warning("%s: Setting the device online failed "
446 "because it is not operational\n",
447 dev_name(&cdev->dev));
448 }
451 /* Give up online reference since onlining failed. */ 449 /* Give up online reference since onlining failed. */
452 put_device(&cdev->dev); 450 put_device(&cdev->dev);
453 return -ENODEV; 451 return -ENODEV;
@@ -494,27 +492,22 @@ error:
494 492
495static int online_store_handle_offline(struct ccw_device *cdev) 493static int online_store_handle_offline(struct ccw_device *cdev)
496{ 494{
497 if (cdev->private->state == DEV_STATE_DISCONNECTED) 495 if (cdev->private->state == DEV_STATE_DISCONNECTED) {
498 ccw_device_remove_disconnected(cdev); 496 spin_lock_irq(cdev->ccwlock);
499 else if (cdev->online && cdev->drv && cdev->drv->set_offline) 497 ccw_device_sched_todo(cdev, CDEV_TODO_UNREG_EVAL);
498 spin_unlock_irq(cdev->ccwlock);
499 } else if (cdev->online && cdev->drv && cdev->drv->set_offline)
500 return ccw_device_set_offline(cdev); 500 return ccw_device_set_offline(cdev);
501 return 0; 501 return 0;
502} 502}
503 503
504static int online_store_recog_and_online(struct ccw_device *cdev) 504static int online_store_recog_and_online(struct ccw_device *cdev)
505{ 505{
506 int ret;
507
508 /* Do device recognition, if needed. */ 506 /* Do device recognition, if needed. */
509 if (cdev->private->state == DEV_STATE_BOXED) { 507 if (cdev->private->state == DEV_STATE_BOXED) {
510 ret = ccw_device_recognition(cdev); 508 spin_lock_irq(cdev->ccwlock);
511 if (ret) { 509 ccw_device_recognition(cdev);
512 CIO_MSG_EVENT(0, "Couldn't start recognition " 510 spin_unlock_irq(cdev->ccwlock);
513 "for device 0.%x.%04x (ret=%d)\n",
514 cdev->private->dev_id.ssid,
515 cdev->private->dev_id.devno, ret);
516 return ret;
517 }
518 wait_event(cdev->private->wait_q, 511 wait_event(cdev->private->wait_q,
519 cdev->private->flags.recog_done); 512 cdev->private->flags.recog_done);
520 if (cdev->private->state != DEV_STATE_OFFLINE) 513 if (cdev->private->state != DEV_STATE_OFFLINE)
@@ -553,11 +546,10 @@ static ssize_t online_store (struct device *dev, struct device_attribute *attr,
553 int force, ret; 546 int force, ret;
554 unsigned long i; 547 unsigned long i;
555 548
556 if ((cdev->private->state != DEV_STATE_OFFLINE && 549 if (!dev_fsm_final_state(cdev) &&
557 cdev->private->state != DEV_STATE_ONLINE && 550 cdev->private->state != DEV_STATE_DISCONNECTED)
558 cdev->private->state != DEV_STATE_BOXED && 551 return -EAGAIN;
559 cdev->private->state != DEV_STATE_DISCONNECTED) || 552 if (atomic_cmpxchg(&cdev->private->onoff, 0, 1) != 0)
560 atomic_cmpxchg(&cdev->private->onoff, 0, 1) != 0)
561 return -EAGAIN; 553 return -EAGAIN;
562 554
563 if (cdev->drv && !try_module_get(cdev->drv->owner)) { 555 if (cdev->drv && !try_module_get(cdev->drv->owner)) {
@@ -665,81 +657,31 @@ static int ccw_device_register(struct ccw_device *cdev)
665 cdev->private->dev_id.devno); 657 cdev->private->dev_id.devno);
666 if (ret) 658 if (ret)
667 return ret; 659 return ret;
668 ret = device_add(dev); 660 return device_add(dev);
669 if (ret)
670 return ret;
671
672 set_bit(1, &cdev->private->registered);
673 return ret;
674} 661}
675 662
676struct match_data { 663static int match_dev_id(struct device *dev, void *data)
677 struct ccw_dev_id dev_id;
678 struct ccw_device * sibling;
679};
680
681static int
682match_devno(struct device * dev, void * data)
683{
684 struct match_data * d = data;
685 struct ccw_device * cdev;
686
687 cdev = to_ccwdev(dev);
688 if ((cdev->private->state == DEV_STATE_DISCONNECTED) &&
689 !ccw_device_is_orphan(cdev) &&
690 ccw_dev_id_is_equal(&cdev->private->dev_id, &d->dev_id) &&
691 (cdev != d->sibling))
692 return 1;
693 return 0;
694}
695
696static struct ccw_device * get_disc_ccwdev_by_dev_id(struct ccw_dev_id *dev_id,
697 struct ccw_device *sibling)
698{
699 struct device *dev;
700 struct match_data data;
701
702 data.dev_id = *dev_id;
703 data.sibling = sibling;
704 dev = bus_find_device(&ccw_bus_type, NULL, &data, match_devno);
705
706 return dev ? to_ccwdev(dev) : NULL;
707}
708
709static int match_orphan(struct device *dev, void *data)
710{ 664{
711 struct ccw_dev_id *dev_id; 665 struct ccw_device *cdev = to_ccwdev(dev);
712 struct ccw_device *cdev; 666 struct ccw_dev_id *dev_id = data;
713 667
714 dev_id = data;
715 cdev = to_ccwdev(dev);
716 return ccw_dev_id_is_equal(&cdev->private->dev_id, dev_id); 668 return ccw_dev_id_is_equal(&cdev->private->dev_id, dev_id);
717} 669}
718 670
719static struct ccw_device * 671static struct ccw_device *get_ccwdev_by_dev_id(struct ccw_dev_id *dev_id)
720get_orphaned_ccwdev_by_dev_id(struct channel_subsystem *css,
721 struct ccw_dev_id *dev_id)
722{ 672{
723 struct device *dev; 673 struct device *dev;
724 674
725 dev = device_find_child(&css->pseudo_subchannel->dev, dev_id, 675 dev = bus_find_device(&ccw_bus_type, NULL, dev_id, match_dev_id);
726 match_orphan);
727 676
728 return dev ? to_ccwdev(dev) : NULL; 677 return dev ? to_ccwdev(dev) : NULL;
729} 678}
730 679
731void ccw_device_do_unbind_bind(struct work_struct *work) 680static void ccw_device_do_unbind_bind(struct ccw_device *cdev)
732{ 681{
733 struct ccw_device_private *priv;
734 struct ccw_device *cdev;
735 struct subchannel *sch;
736 int ret; 682 int ret;
737 683
738 priv = container_of(work, struct ccw_device_private, kick_work); 684 if (device_is_registered(&cdev->dev)) {
739 cdev = priv->cdev;
740 sch = to_subchannel(cdev->dev.parent);
741
742 if (test_bit(1, &cdev->private->registered)) {
743 device_release_driver(&cdev->dev); 685 device_release_driver(&cdev->dev);
744 ret = device_attach(&cdev->dev); 686 ret = device_attach(&cdev->dev);
745 WARN_ON(ret == -ENODEV); 687 WARN_ON(ret == -ENODEV);
@@ -773,6 +715,8 @@ static struct ccw_device * io_subchannel_allocate_dev(struct subchannel *sch)
773 return ERR_PTR(-ENOMEM); 715 return ERR_PTR(-ENOMEM);
774} 716}
775 717
718static void ccw_device_todo(struct work_struct *work);
719
776static int io_subchannel_initialize_dev(struct subchannel *sch, 720static int io_subchannel_initialize_dev(struct subchannel *sch,
777 struct ccw_device *cdev) 721 struct ccw_device *cdev)
778{ 722{
@@ -780,7 +724,7 @@ static int io_subchannel_initialize_dev(struct subchannel *sch,
780 atomic_set(&cdev->private->onoff, 0); 724 atomic_set(&cdev->private->onoff, 0);
781 cdev->dev.parent = &sch->dev; 725 cdev->dev.parent = &sch->dev;
782 cdev->dev.release = ccw_device_release; 726 cdev->dev.release = ccw_device_release;
783 INIT_WORK(&cdev->private->kick_work, NULL); 727 INIT_WORK(&cdev->private->todo_work, ccw_device_todo);
784 cdev->dev.groups = ccwdev_attr_groups; 728 cdev->dev.groups = ccwdev_attr_groups;
785 /* Do first half of device_register. */ 729 /* Do first half of device_register. */
786 device_initialize(&cdev->dev); 730 device_initialize(&cdev->dev);
@@ -789,6 +733,7 @@ static int io_subchannel_initialize_dev(struct subchannel *sch,
789 put_device(&cdev->dev); 733 put_device(&cdev->dev);
790 return -ENODEV; 734 return -ENODEV;
791 } 735 }
736 cdev->private->flags.initialized = 1;
792 return 0; 737 return 0;
793} 738}
794 739
@@ -806,76 +751,7 @@ static struct ccw_device * io_subchannel_create_ccwdev(struct subchannel *sch)
806 return cdev; 751 return cdev;
807} 752}
808 753
809static int io_subchannel_recog(struct ccw_device *, struct subchannel *); 754static void io_subchannel_recog(struct ccw_device *, struct subchannel *);
810
811static void sch_attach_device(struct subchannel *sch,
812 struct ccw_device *cdev)
813{
814 css_update_ssd_info(sch);
815 spin_lock_irq(sch->lock);
816 sch_set_cdev(sch, cdev);
817 cdev->private->schid = sch->schid;
818 cdev->ccwlock = sch->lock;
819 ccw_device_trigger_reprobe(cdev);
820 spin_unlock_irq(sch->lock);
821}
822
823static void sch_attach_disconnected_device(struct subchannel *sch,
824 struct ccw_device *cdev)
825{
826 struct subchannel *other_sch;
827 int ret;
828
829 /* Get reference for new parent. */
830 if (!get_device(&sch->dev))
831 return;
832 other_sch = to_subchannel(cdev->dev.parent);
833 /* Note: device_move() changes cdev->dev.parent */
834 ret = device_move(&cdev->dev, &sch->dev, DPM_ORDER_PARENT_BEFORE_DEV);
835 if (ret) {
836 CIO_MSG_EVENT(0, "Moving disconnected device 0.%x.%04x failed "
837 "(ret=%d)!\n", cdev->private->dev_id.ssid,
838 cdev->private->dev_id.devno, ret);
839 /* Put reference for new parent. */
840 put_device(&sch->dev);
841 return;
842 }
843 sch_set_cdev(other_sch, NULL);
844 /* No need to keep a subchannel without ccw device around. */
845 css_sch_device_unregister(other_sch);
846 sch_attach_device(sch, cdev);
847 /* Put reference for old parent. */
848 put_device(&other_sch->dev);
849}
850
851static void sch_attach_orphaned_device(struct subchannel *sch,
852 struct ccw_device *cdev)
853{
854 int ret;
855 struct subchannel *pseudo_sch;
856
857 /* Get reference for new parent. */
858 if (!get_device(&sch->dev))
859 return;
860 pseudo_sch = to_subchannel(cdev->dev.parent);
861 /*
862 * Try to move the ccw device to its new subchannel.
863 * Note: device_move() changes cdev->dev.parent
864 */
865 ret = device_move(&cdev->dev, &sch->dev, DPM_ORDER_PARENT_BEFORE_DEV);
866 if (ret) {
867 CIO_MSG_EVENT(0, "Moving device 0.%x.%04x from orphanage "
868 "failed (ret=%d)!\n",
869 cdev->private->dev_id.ssid,
870 cdev->private->dev_id.devno, ret);
871 /* Put reference for new parent. */
872 put_device(&sch->dev);
873 return;
874 }
875 sch_attach_device(sch, cdev);
876 /* Put reference on pseudo subchannel. */
877 put_device(&pseudo_sch->dev);
878}
879 755
880static void sch_create_and_recog_new_device(struct subchannel *sch) 756static void sch_create_and_recog_new_device(struct subchannel *sch)
881{ 757{
@@ -888,100 +764,19 @@ static void sch_create_and_recog_new_device(struct subchannel *sch)
888 css_sch_device_unregister(sch); 764 css_sch_device_unregister(sch);
889 return; 765 return;
890 } 766 }
891 spin_lock_irq(sch->lock);
892 sch_set_cdev(sch, cdev);
893 spin_unlock_irq(sch->lock);
894 /* Start recognition for the new ccw device. */ 767 /* Start recognition for the new ccw device. */
895 if (io_subchannel_recog(cdev, sch)) { 768 io_subchannel_recog(cdev, sch);
896 spin_lock_irq(sch->lock);
897 sch_set_cdev(sch, NULL);
898 spin_unlock_irq(sch->lock);
899 css_sch_device_unregister(sch);
900 /* Put reference from io_subchannel_create_ccwdev(). */
901 put_device(&sch->dev);
902 /* Give up initial reference. */
903 put_device(&cdev->dev);
904 }
905}
906
907
908void ccw_device_move_to_orphanage(struct work_struct *work)
909{
910 struct ccw_device_private *priv;
911 struct ccw_device *cdev;
912 struct ccw_device *replacing_cdev;
913 struct subchannel *sch;
914 int ret;
915 struct channel_subsystem *css;
916 struct ccw_dev_id dev_id;
917
918 priv = container_of(work, struct ccw_device_private, kick_work);
919 cdev = priv->cdev;
920 sch = to_subchannel(cdev->dev.parent);
921 css = to_css(sch->dev.parent);
922 dev_id.devno = sch->schib.pmcw.dev;
923 dev_id.ssid = sch->schid.ssid;
924
925 /* Increase refcount for pseudo subchannel. */
926 get_device(&css->pseudo_subchannel->dev);
927 /*
928 * Move the orphaned ccw device to the orphanage so the replacing
929 * ccw device can take its place on the subchannel.
930 * Note: device_move() changes cdev->dev.parent
931 */
932 ret = device_move(&cdev->dev, &css->pseudo_subchannel->dev,
933 DPM_ORDER_NONE);
934 if (ret) {
935 CIO_MSG_EVENT(0, "Moving device 0.%x.%04x to orphanage failed "
936 "(ret=%d)!\n", cdev->private->dev_id.ssid,
937 cdev->private->dev_id.devno, ret);
938 /* Decrease refcount for pseudo subchannel again. */
939 put_device(&css->pseudo_subchannel->dev);
940 return;
941 }
942 cdev->ccwlock = css->pseudo_subchannel->lock;
943 /*
944 * Search for the replacing ccw device
945 * - among the disconnected devices
946 * - in the orphanage
947 */
948 replacing_cdev = get_disc_ccwdev_by_dev_id(&dev_id, cdev);
949 if (replacing_cdev) {
950 sch_attach_disconnected_device(sch, replacing_cdev);
951 /* Release reference from get_disc_ccwdev_by_dev_id() */
952 put_device(&replacing_cdev->dev);
953 /* Release reference of subchannel from old cdev. */
954 put_device(&sch->dev);
955 return;
956 }
957 replacing_cdev = get_orphaned_ccwdev_by_dev_id(css, &dev_id);
958 if (replacing_cdev) {
959 sch_attach_orphaned_device(sch, replacing_cdev);
960 /* Release reference from get_orphaned_ccwdev_by_dev_id() */
961 put_device(&replacing_cdev->dev);
962 /* Release reference of subchannel from old cdev. */
963 put_device(&sch->dev);
964 return;
965 }
966 sch_create_and_recog_new_device(sch);
967 /* Release reference of subchannel from old cdev. */
968 put_device(&sch->dev);
969} 769}
970 770
971/* 771/*
972 * Register recognized device. 772 * Register recognized device.
973 */ 773 */
974static void 774static void io_subchannel_register(struct ccw_device *cdev)
975io_subchannel_register(struct work_struct *work)
976{ 775{
977 struct ccw_device_private *priv;
978 struct ccw_device *cdev;
979 struct subchannel *sch; 776 struct subchannel *sch;
980 int ret; 777 int ret;
981 unsigned long flags; 778 unsigned long flags;
982 779
983 priv = container_of(work, struct ccw_device_private, kick_work);
984 cdev = priv->cdev;
985 sch = to_subchannel(cdev->dev.parent); 780 sch = to_subchannel(cdev->dev.parent);
986 /* 781 /*
987 * Check if subchannel is still registered. It may have become 782 * Check if subchannel is still registered. It may have become
@@ -1033,41 +828,23 @@ out:
1033 cdev->private->flags.recog_done = 1; 828 cdev->private->flags.recog_done = 1;
1034 wake_up(&cdev->private->wait_q); 829 wake_up(&cdev->private->wait_q);
1035out_err: 830out_err:
1036 /* Release reference for workqueue processing. */
1037 put_device(&cdev->dev);
1038 if (atomic_dec_and_test(&ccw_device_init_count)) 831 if (atomic_dec_and_test(&ccw_device_init_count))
1039 wake_up(&ccw_device_init_wq); 832 wake_up(&ccw_device_init_wq);
1040} 833}
1041 834
1042static void ccw_device_call_sch_unregister(struct work_struct *work) 835static void ccw_device_call_sch_unregister(struct ccw_device *cdev)
1043{ 836{
1044 struct ccw_device_private *priv;
1045 struct ccw_device *cdev;
1046 struct subchannel *sch; 837 struct subchannel *sch;
1047 838
1048 priv = container_of(work, struct ccw_device_private, kick_work);
1049 cdev = priv->cdev;
1050 /* Get subchannel reference for local processing. */ 839 /* Get subchannel reference for local processing. */
1051 if (!get_device(cdev->dev.parent)) 840 if (!get_device(cdev->dev.parent))
1052 return; 841 return;
1053 sch = to_subchannel(cdev->dev.parent); 842 sch = to_subchannel(cdev->dev.parent);
1054 css_sch_device_unregister(sch); 843 css_sch_device_unregister(sch);
1055 /* Release cdev reference for workqueue processing.*/
1056 put_device(&cdev->dev);
1057 /* Release subchannel reference for local processing. */ 844 /* Release subchannel reference for local processing. */
1058 put_device(&sch->dev); 845 put_device(&sch->dev);
1059} 846}
1060 847
1061void ccw_device_schedule_sch_unregister(struct ccw_device *cdev)
1062{
1063 /* Get cdev reference for workqueue processing. */
1064 if (!get_device(&cdev->dev))
1065 return;
1066 PREPARE_WORK(&cdev->private->kick_work,
1067 ccw_device_call_sch_unregister);
1068 queue_work(slow_path_wq, &cdev->private->kick_work);
1069}
1070
1071/* 848/*
1072 * subchannel recognition done. Called from the state machine. 849 * subchannel recognition done. Called from the state machine.
1073 */ 850 */
@@ -1083,7 +860,8 @@ io_subchannel_recog_done(struct ccw_device *cdev)
1083 /* Device did not respond in time. */ 860 /* Device did not respond in time. */
1084 case DEV_STATE_NOT_OPER: 861 case DEV_STATE_NOT_OPER:
1085 cdev->private->flags.recog_done = 1; 862 cdev->private->flags.recog_done = 1;
1086 ccw_device_schedule_sch_unregister(cdev); 863 /* Remove device found not operational. */
864 ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
1087 if (atomic_dec_and_test(&ccw_device_init_count)) 865 if (atomic_dec_and_test(&ccw_device_init_count))
1088 wake_up(&ccw_device_init_wq); 866 wake_up(&ccw_device_init_wq);
1089 break; 867 break;
@@ -1092,22 +870,15 @@ io_subchannel_recog_done(struct ccw_device *cdev)
1092 * We can't register the device in interrupt context so 870 * We can't register the device in interrupt context so
1093 * we schedule a work item. 871 * we schedule a work item.
1094 */ 872 */
1095 if (!get_device(&cdev->dev)) 873 ccw_device_sched_todo(cdev, CDEV_TODO_REGISTER);
1096 break;
1097 PREPARE_WORK(&cdev->private->kick_work,
1098 io_subchannel_register);
1099 queue_work(slow_path_wq, &cdev->private->kick_work);
1100 break; 874 break;
1101 } 875 }
1102} 876}
1103 877
1104static int 878static void io_subchannel_recog(struct ccw_device *cdev, struct subchannel *sch)
1105io_subchannel_recog(struct ccw_device *cdev, struct subchannel *sch)
1106{ 879{
1107 int rc;
1108 struct ccw_device_private *priv; 880 struct ccw_device_private *priv;
1109 881
1110 sch_set_cdev(sch, cdev);
1111 cdev->ccwlock = sch->lock; 882 cdev->ccwlock = sch->lock;
1112 883
1113 /* Init private data. */ 884 /* Init private data. */
@@ -1125,62 +896,81 @@ io_subchannel_recog(struct ccw_device *cdev, struct subchannel *sch)
1125 896
1126 /* Start async. device sensing. */ 897 /* Start async. device sensing. */
1127 spin_lock_irq(sch->lock); 898 spin_lock_irq(sch->lock);
1128 rc = ccw_device_recognition(cdev); 899 sch_set_cdev(sch, cdev);
900 ccw_device_recognition(cdev);
1129 spin_unlock_irq(sch->lock); 901 spin_unlock_irq(sch->lock);
1130 if (rc) {
1131 if (atomic_dec_and_test(&ccw_device_init_count))
1132 wake_up(&ccw_device_init_wq);
1133 }
1134 return rc;
1135} 902}
1136 903
1137static void ccw_device_move_to_sch(struct work_struct *work) 904static int ccw_device_move_to_sch(struct ccw_device *cdev,
905 struct subchannel *sch)
1138{ 906{
1139 struct ccw_device_private *priv; 907 struct subchannel *old_sch;
1140 int rc; 908 int rc, old_enabled = 0;
1141 struct subchannel *sch;
1142 struct ccw_device *cdev;
1143 struct subchannel *former_parent;
1144 909
1145 priv = container_of(work, struct ccw_device_private, kick_work); 910 old_sch = to_subchannel(cdev->dev.parent);
1146 sch = priv->sch; 911 /* Obtain child reference for new parent. */
1147 cdev = priv->cdev;
1148 former_parent = to_subchannel(cdev->dev.parent);
1149 /* Get reference for new parent. */
1150 if (!get_device(&sch->dev)) 912 if (!get_device(&sch->dev))
1151 return; 913 return -ENODEV;
914
915 if (!sch_is_pseudo_sch(old_sch)) {
916 spin_lock_irq(old_sch->lock);
917 old_enabled = old_sch->schib.pmcw.ena;
918 rc = 0;
919 if (old_enabled)
920 rc = cio_disable_subchannel(old_sch);
921 spin_unlock_irq(old_sch->lock);
922 if (rc == -EBUSY) {
923 /* Release child reference for new parent. */
924 put_device(&sch->dev);
925 return rc;
926 }
927 }
928
1152 mutex_lock(&sch->reg_mutex); 929 mutex_lock(&sch->reg_mutex);
1153 /*
1154 * Try to move the ccw device to its new subchannel.
1155 * Note: device_move() changes cdev->dev.parent
1156 */
1157 rc = device_move(&cdev->dev, &sch->dev, DPM_ORDER_PARENT_BEFORE_DEV); 930 rc = device_move(&cdev->dev, &sch->dev, DPM_ORDER_PARENT_BEFORE_DEV);
1158 mutex_unlock(&sch->reg_mutex); 931 mutex_unlock(&sch->reg_mutex);
1159 if (rc) { 932 if (rc) {
1160 CIO_MSG_EVENT(0, "Moving device 0.%x.%04x to subchannel " 933 CIO_MSG_EVENT(0, "device_move(0.%x.%04x,0.%x.%04x)=%d\n",
1161 "0.%x.%04x failed (ret=%d)!\n",
1162 cdev->private->dev_id.ssid, 934 cdev->private->dev_id.ssid,
1163 cdev->private->dev_id.devno, sch->schid.ssid, 935 cdev->private->dev_id.devno, sch->schid.ssid,
1164 sch->schid.sch_no, rc); 936 sch->schib.pmcw.dev, rc);
1165 css_sch_device_unregister(sch); 937 if (old_enabled) {
1166 /* Put reference for new parent again. */ 938 /* Try to reenable the old subchannel. */
939 spin_lock_irq(old_sch->lock);
940 cio_enable_subchannel(old_sch, (u32)(addr_t)old_sch);
941 spin_unlock_irq(old_sch->lock);
942 }
943 /* Release child reference for new parent. */
1167 put_device(&sch->dev); 944 put_device(&sch->dev);
1168 goto out; 945 return rc;
1169 } 946 }
1170 if (!sch_is_pseudo_sch(former_parent)) { 947 /* Clean up old subchannel. */
1171 spin_lock_irq(former_parent->lock); 948 if (!sch_is_pseudo_sch(old_sch)) {
1172 sch_set_cdev(former_parent, NULL); 949 spin_lock_irq(old_sch->lock);
1173 spin_unlock_irq(former_parent->lock); 950 sch_set_cdev(old_sch, NULL);
1174 css_sch_device_unregister(former_parent); 951 spin_unlock_irq(old_sch->lock);
1175 /* Reset intparm to zeroes. */ 952 css_schedule_eval(old_sch->schid);
1176 former_parent->config.intparm = 0;
1177 cio_commit_config(former_parent);
1178 } 953 }
1179 sch_attach_device(sch, cdev); 954 /* Release child reference for old parent. */
1180out: 955 put_device(&old_sch->dev);
1181 /* Put reference for old parent. */ 956 /* Initialize new subchannel. */
1182 put_device(&former_parent->dev); 957 spin_lock_irq(sch->lock);
1183 put_device(&cdev->dev); 958 cdev->private->schid = sch->schid;
959 cdev->ccwlock = sch->lock;
960 if (!sch_is_pseudo_sch(sch))
961 sch_set_cdev(sch, cdev);
962 spin_unlock_irq(sch->lock);
963 if (!sch_is_pseudo_sch(sch))
964 css_update_ssd_info(sch);
965 return 0;
966}
967
968static int ccw_device_move_to_orph(struct ccw_device *cdev)
969{
970 struct subchannel *sch = to_subchannel(cdev->dev.parent);
971 struct channel_subsystem *css = to_css(sch->dev.parent);
972
973 return ccw_device_move_to_sch(cdev, css->pseudo_subchannel);
1184} 974}
1185 975
1186static void io_subchannel_irq(struct subchannel *sch) 976static void io_subchannel_irq(struct subchannel *sch)
@@ -1199,9 +989,6 @@ void io_subchannel_init_config(struct subchannel *sch)
1199{ 989{
1200 memset(&sch->config, 0, sizeof(sch->config)); 990 memset(&sch->config, 0, sizeof(sch->config));
1201 sch->config.csense = 1; 991 sch->config.csense = 1;
1202 /* Use subchannel mp mode when there is more than 1 installed CHPID. */
1203 if ((sch->schib.pmcw.pim & (sch->schib.pmcw.pim - 1)) != 0)
1204 sch->config.mp = 1;
1205} 992}
1206 993
1207static void io_subchannel_init_fields(struct subchannel *sch) 994static void io_subchannel_init_fields(struct subchannel *sch)
@@ -1222,23 +1009,6 @@ static void io_subchannel_init_fields(struct subchannel *sch)
1222 io_subchannel_init_config(sch); 1009 io_subchannel_init_config(sch);
1223} 1010}
1224 1011
1225static void io_subchannel_do_unreg(struct work_struct *work)
1226{
1227 struct subchannel *sch;
1228
1229 sch = container_of(work, struct subchannel, work);
1230 css_sch_device_unregister(sch);
1231 put_device(&sch->dev);
1232}
1233
1234/* Schedule unregister if we have no cdev. */
1235static void io_subchannel_schedule_removal(struct subchannel *sch)
1236{
1237 get_device(&sch->dev);
1238 INIT_WORK(&sch->work, io_subchannel_do_unreg);
1239 queue_work(slow_path_wq, &sch->work);
1240}
1241
1242/* 1012/*
1243 * Note: We always return 0 so that we bind to the device even on error. 1013 * Note: We always return 0 so that we bind to the device even on error.
1244 * This is needed so that our remove function is called on unregister. 1014 * This is needed so that our remove function is called on unregister.
@@ -1247,8 +1017,6 @@ static int io_subchannel_probe(struct subchannel *sch)
1247{ 1017{
1248 struct ccw_device *cdev; 1018 struct ccw_device *cdev;
1249 int rc; 1019 int rc;
1250 unsigned long flags;
1251 struct ccw_dev_id dev_id;
1252 1020
1253 if (cio_is_console(sch->schid)) { 1021 if (cio_is_console(sch->schid)) {
1254 rc = sysfs_create_group(&sch->dev.kobj, 1022 rc = sysfs_create_group(&sch->dev.kobj,
@@ -1268,6 +1036,7 @@ static int io_subchannel_probe(struct subchannel *sch)
1268 cdev = sch_get_cdev(sch); 1036 cdev = sch_get_cdev(sch);
1269 cdev->dev.groups = ccwdev_attr_groups; 1037 cdev->dev.groups = ccwdev_attr_groups;
1270 device_initialize(&cdev->dev); 1038 device_initialize(&cdev->dev);
1039 cdev->private->flags.initialized = 1;
1271 ccw_device_register(cdev); 1040 ccw_device_register(cdev);
1272 /* 1041 /*
1273 * Check if the device is already online. If it is 1042 * Check if the device is already online. If it is
@@ -1292,44 +1061,14 @@ static int io_subchannel_probe(struct subchannel *sch)
1292 sch->private = kzalloc(sizeof(struct io_subchannel_private), 1061 sch->private = kzalloc(sizeof(struct io_subchannel_private),
1293 GFP_KERNEL | GFP_DMA); 1062 GFP_KERNEL | GFP_DMA);
1294 if (!sch->private) 1063 if (!sch->private)
1295 goto out_err; 1064 goto out_schedule;
1296 /* 1065 css_schedule_eval(sch->schid);
1297 * First check if a fitting device may be found amongst the
1298 * disconnected devices or in the orphanage.
1299 */
1300 dev_id.devno = sch->schib.pmcw.dev;
1301 dev_id.ssid = sch->schid.ssid;
1302 cdev = get_disc_ccwdev_by_dev_id(&dev_id, NULL);
1303 if (!cdev)
1304 cdev = get_orphaned_ccwdev_by_dev_id(to_css(sch->dev.parent),
1305 &dev_id);
1306 if (cdev) {
1307 /*
1308 * Schedule moving the device until when we have a registered
1309 * subchannel to move to and succeed the probe. We can
1310 * unregister later again, when the probe is through.
1311 */
1312 cdev->private->sch = sch;
1313 PREPARE_WORK(&cdev->private->kick_work,
1314 ccw_device_move_to_sch);
1315 queue_work(slow_path_wq, &cdev->private->kick_work);
1316 return 0;
1317 }
1318 cdev = io_subchannel_create_ccwdev(sch);
1319 if (IS_ERR(cdev))
1320 goto out_err;
1321 rc = io_subchannel_recog(cdev, sch);
1322 if (rc) {
1323 spin_lock_irqsave(sch->lock, flags);
1324 io_subchannel_recog_done(cdev);
1325 spin_unlock_irqrestore(sch->lock, flags);
1326 }
1327 return 0; 1066 return 0;
1328out_err: 1067
1329 kfree(sch->private);
1330 sysfs_remove_group(&sch->dev.kobj, &io_subchannel_attr_group);
1331out_schedule: 1068out_schedule:
1332 io_subchannel_schedule_removal(sch); 1069 spin_lock_irq(sch->lock);
1070 css_sched_sch_todo(sch, SCH_TODO_UNREG);
1071 spin_unlock_irq(sch->lock);
1333 return 0; 1072 return 0;
1334} 1073}
1335 1074
@@ -1337,32 +1076,23 @@ static int
1337io_subchannel_remove (struct subchannel *sch) 1076io_subchannel_remove (struct subchannel *sch)
1338{ 1077{
1339 struct ccw_device *cdev; 1078 struct ccw_device *cdev;
1340 unsigned long flags;
1341 1079
1342 cdev = sch_get_cdev(sch); 1080 cdev = sch_get_cdev(sch);
1343 if (!cdev) 1081 if (!cdev)
1344 return 0; 1082 goto out_free;
1083 io_subchannel_quiesce(sch);
1345 /* Set ccw device to not operational and drop reference. */ 1084 /* Set ccw device to not operational and drop reference. */
1346 spin_lock_irqsave(cdev->ccwlock, flags); 1085 spin_lock_irq(cdev->ccwlock);
1347 sch_set_cdev(sch, NULL); 1086 sch_set_cdev(sch, NULL);
1348 cdev->private->state = DEV_STATE_NOT_OPER; 1087 cdev->private->state = DEV_STATE_NOT_OPER;
1349 spin_unlock_irqrestore(cdev->ccwlock, flags); 1088 spin_unlock_irq(cdev->ccwlock);
1350 ccw_device_unregister(cdev); 1089 ccw_device_unregister(cdev);
1090out_free:
1351 kfree(sch->private); 1091 kfree(sch->private);
1352 sysfs_remove_group(&sch->dev.kobj, &io_subchannel_attr_group); 1092 sysfs_remove_group(&sch->dev.kobj, &io_subchannel_attr_group);
1353 return 0; 1093 return 0;
1354} 1094}
1355 1095
1356static int io_subchannel_notify(struct subchannel *sch, int event)
1357{
1358 struct ccw_device *cdev;
1359
1360 cdev = sch_get_cdev(sch);
1361 if (!cdev)
1362 return 0;
1363 return ccw_device_notify(cdev, event);
1364}
1365
1366static void io_subchannel_verify(struct subchannel *sch) 1096static void io_subchannel_verify(struct subchannel *sch)
1367{ 1097{
1368 struct ccw_device *cdev; 1098 struct ccw_device *cdev;
@@ -1372,36 +1102,6 @@ static void io_subchannel_verify(struct subchannel *sch)
1372 dev_fsm_event(cdev, DEV_EVENT_VERIFY); 1102 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1373} 1103}
1374 1104
1375static int check_for_io_on_path(struct subchannel *sch, int mask)
1376{
1377 if (cio_update_schib(sch))
1378 return 0;
1379 if (scsw_actl(&sch->schib.scsw) && sch->schib.pmcw.lpum == mask)
1380 return 1;
1381 return 0;
1382}
1383
1384static void terminate_internal_io(struct subchannel *sch,
1385 struct ccw_device *cdev)
1386{
1387 if (cio_clear(sch)) {
1388 /* Recheck device in case clear failed. */
1389 sch->lpm = 0;
1390 if (cdev->online)
1391 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1392 else
1393 css_schedule_eval(sch->schid);
1394 return;
1395 }
1396 cdev->private->state = DEV_STATE_CLEAR_VERIFY;
1397 /* Request retry of internal operation. */
1398 cdev->private->flags.intretry = 1;
1399 /* Call handler. */
1400 if (cdev->handler)
1401 cdev->handler(cdev, cdev->private->intparm,
1402 ERR_PTR(-EIO));
1403}
1404
1405static void io_subchannel_terminate_path(struct subchannel *sch, u8 mask) 1105static void io_subchannel_terminate_path(struct subchannel *sch, u8 mask)
1406{ 1106{
1407 struct ccw_device *cdev; 1107 struct ccw_device *cdev;
@@ -1409,18 +1109,24 @@ static void io_subchannel_terminate_path(struct subchannel *sch, u8 mask)
1409 cdev = sch_get_cdev(sch); 1109 cdev = sch_get_cdev(sch);
1410 if (!cdev) 1110 if (!cdev)
1411 return; 1111 return;
1412 if (check_for_io_on_path(sch, mask)) { 1112 if (cio_update_schib(sch))
1413 if (cdev->private->state == DEV_STATE_ONLINE) 1113 goto err;
1414 ccw_device_kill_io(cdev); 1114 /* Check for I/O on path. */
1415 else { 1115 if (scsw_actl(&sch->schib.scsw) == 0 || sch->schib.pmcw.lpum != mask)
1416 terminate_internal_io(sch, cdev); 1116 goto out;
1417 /* Re-start path verification. */ 1117 if (cdev->private->state == DEV_STATE_ONLINE) {
1418 dev_fsm_event(cdev, DEV_EVENT_VERIFY); 1118 ccw_device_kill_io(cdev);
1419 } 1119 goto out;
1420 } else 1120 }
1421 /* trigger path verification. */ 1121 if (cio_clear(sch))
1422 dev_fsm_event(cdev, DEV_EVENT_VERIFY); 1122 goto err;
1123out:
1124 /* Trigger path verification. */
1125 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1126 return;
1423 1127
1128err:
1129 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
1424} 1130}
1425 1131
1426static int io_subchannel_chp_event(struct subchannel *sch, 1132static int io_subchannel_chp_event(struct subchannel *sch,
@@ -1457,46 +1163,41 @@ static int io_subchannel_chp_event(struct subchannel *sch,
1457 return 0; 1163 return 0;
1458} 1164}
1459 1165
1460static void 1166static void io_subchannel_quiesce(struct subchannel *sch)
1461io_subchannel_shutdown(struct subchannel *sch)
1462{ 1167{
1463 struct ccw_device *cdev; 1168 struct ccw_device *cdev;
1464 int ret; 1169 int ret;
1465 1170
1171 spin_lock_irq(sch->lock);
1466 cdev = sch_get_cdev(sch); 1172 cdev = sch_get_cdev(sch);
1467
1468 if (cio_is_console(sch->schid)) 1173 if (cio_is_console(sch->schid))
1469 return; 1174 goto out_unlock;
1470 if (!sch->schib.pmcw.ena) 1175 if (!sch->schib.pmcw.ena)
1471 /* Nothing to do. */ 1176 goto out_unlock;
1472 return;
1473 ret = cio_disable_subchannel(sch); 1177 ret = cio_disable_subchannel(sch);
1474 if (ret != -EBUSY) 1178 if (ret != -EBUSY)
1475 /* Subchannel is disabled, we're done. */ 1179 goto out_unlock;
1476 return;
1477 cdev->private->state = DEV_STATE_QUIESCE;
1478 if (cdev->handler) 1180 if (cdev->handler)
1479 cdev->handler(cdev, cdev->private->intparm, 1181 cdev->handler(cdev, cdev->private->intparm, ERR_PTR(-EIO));
1480 ERR_PTR(-EIO)); 1182 while (ret == -EBUSY) {
1481 ret = ccw_device_cancel_halt_clear(cdev); 1183 cdev->private->state = DEV_STATE_QUIESCE;
1482 if (ret == -EBUSY) { 1184 ret = ccw_device_cancel_halt_clear(cdev);
1483 ccw_device_set_timeout(cdev, HZ/10); 1185 if (ret == -EBUSY) {
1484 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev)); 1186 ccw_device_set_timeout(cdev, HZ/10);
1187 spin_unlock_irq(sch->lock);
1188 wait_event(cdev->private->wait_q,
1189 cdev->private->state != DEV_STATE_QUIESCE);
1190 spin_lock_irq(sch->lock);
1191 }
1192 ret = cio_disable_subchannel(sch);
1485 } 1193 }
1486 cio_disable_subchannel(sch); 1194out_unlock:
1195 spin_unlock_irq(sch->lock);
1487} 1196}
1488 1197
1489static int io_subchannel_get_status(struct subchannel *sch) 1198static void io_subchannel_shutdown(struct subchannel *sch)
1490{ 1199{
1491 struct schib schib; 1200 io_subchannel_quiesce(sch);
1492
1493 if (stsch(sch->schid, &schib) || !schib.pmcw.dnv)
1494 return CIO_GONE;
1495 if (sch->schib.pmcw.dnv && (schib.pmcw.dev != sch->schib.pmcw.dev))
1496 return CIO_REVALIDATE;
1497 if (!sch->lpm)
1498 return CIO_NO_PATH;
1499 return CIO_OPER;
1500} 1201}
1501 1202
1502static int device_is_disconnected(struct ccw_device *cdev) 1203static int device_is_disconnected(struct ccw_device *cdev)
@@ -1575,20 +1276,16 @@ static void ccw_device_schedule_recovery(void)
1575static int purge_fn(struct device *dev, void *data) 1276static int purge_fn(struct device *dev, void *data)
1576{ 1277{
1577 struct ccw_device *cdev = to_ccwdev(dev); 1278 struct ccw_device *cdev = to_ccwdev(dev);
1578 struct ccw_device_private *priv = cdev->private; 1279 struct ccw_dev_id *id = &cdev->private->dev_id;
1579 int unreg;
1580 1280
1581 spin_lock_irq(cdev->ccwlock); 1281 spin_lock_irq(cdev->ccwlock);
1582 unreg = is_blacklisted(priv->dev_id.ssid, priv->dev_id.devno) && 1282 if (is_blacklisted(id->ssid, id->devno) &&
1583 (priv->state == DEV_STATE_OFFLINE); 1283 (cdev->private->state == DEV_STATE_OFFLINE)) {
1284 CIO_MSG_EVENT(3, "ccw: purging 0.%x.%04x\n", id->ssid,
1285 id->devno);
1286 ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
1287 }
1584 spin_unlock_irq(cdev->ccwlock); 1288 spin_unlock_irq(cdev->ccwlock);
1585 if (!unreg)
1586 goto out;
1587 CIO_MSG_EVENT(3, "ccw: purging 0.%x.%04x\n", priv->dev_id.ssid,
1588 priv->dev_id.devno);
1589 ccw_device_schedule_sch_unregister(cdev);
1590
1591out:
1592 /* Abort loop in case of pending signal. */ 1289 /* Abort loop in case of pending signal. */
1593 if (signal_pending(current)) 1290 if (signal_pending(current))
1594 return -EINTR; 1291 return -EINTR;
@@ -1630,91 +1327,169 @@ void ccw_device_set_notoper(struct ccw_device *cdev)
1630 cdev->private->state = DEV_STATE_NOT_OPER; 1327 cdev->private->state = DEV_STATE_NOT_OPER;
1631} 1328}
1632 1329
1633static int io_subchannel_sch_event(struct subchannel *sch, int slow) 1330enum io_sch_action {
1331 IO_SCH_UNREG,
1332 IO_SCH_ORPH_UNREG,
1333 IO_SCH_ATTACH,
1334 IO_SCH_UNREG_ATTACH,
1335 IO_SCH_ORPH_ATTACH,
1336 IO_SCH_REPROBE,
1337 IO_SCH_VERIFY,
1338 IO_SCH_DISC,
1339 IO_SCH_NOP,
1340};
1341
1342static enum io_sch_action sch_get_action(struct subchannel *sch)
1343{
1344 struct ccw_device *cdev;
1345
1346 cdev = sch_get_cdev(sch);
1347 if (cio_update_schib(sch)) {
1348 /* Not operational. */
1349 if (!cdev)
1350 return IO_SCH_UNREG;
1351 if (!ccw_device_notify(cdev, CIO_GONE))
1352 return IO_SCH_UNREG;
1353 return IO_SCH_ORPH_UNREG;
1354 }
1355 /* Operational. */
1356 if (!cdev)
1357 return IO_SCH_ATTACH;
1358 if (sch->schib.pmcw.dev != cdev->private->dev_id.devno) {
1359 if (!ccw_device_notify(cdev, CIO_GONE))
1360 return IO_SCH_UNREG_ATTACH;
1361 return IO_SCH_ORPH_ATTACH;
1362 }
1363 if ((sch->schib.pmcw.pam & sch->opm) == 0) {
1364 if (!ccw_device_notify(cdev, CIO_NO_PATH))
1365 return IO_SCH_UNREG;
1366 return IO_SCH_DISC;
1367 }
1368 if (device_is_disconnected(cdev))
1369 return IO_SCH_REPROBE;
1370 if (cdev->online)
1371 return IO_SCH_VERIFY;
1372 return IO_SCH_NOP;
1373}
1374
1375/**
1376 * io_subchannel_sch_event - process subchannel event
1377 * @sch: subchannel
1378 * @process: non-zero if function is called in process context
1379 *
1380 * An unspecified event occurred for this subchannel. Adjust data according
1381 * to the current operational state of the subchannel and device. Return
1382 * zero when the event has been handled sufficiently or -EAGAIN when this
1383 * function should be called again in process context.
1384 */
1385static int io_subchannel_sch_event(struct subchannel *sch, int process)
1634{ 1386{
1635 int event, ret, disc;
1636 unsigned long flags; 1387 unsigned long flags;
1637 enum { NONE, UNREGISTER, UNREGISTER_PROBE, REPROBE, DISC } action;
1638 struct ccw_device *cdev; 1388 struct ccw_device *cdev;
1389 struct ccw_dev_id dev_id;
1390 enum io_sch_action action;
1391 int rc = -EAGAIN;
1639 1392
1640 spin_lock_irqsave(sch->lock, flags); 1393 spin_lock_irqsave(sch->lock, flags);
1394 if (!device_is_registered(&sch->dev))
1395 goto out_unlock;
1396 if (work_pending(&sch->todo_work))
1397 goto out_unlock;
1641 cdev = sch_get_cdev(sch); 1398 cdev = sch_get_cdev(sch);
1642 disc = device_is_disconnected(cdev); 1399 if (cdev && work_pending(&cdev->private->todo_work))
1643 if (disc && slow) { 1400 goto out_unlock;
1644 /* Disconnected devices are evaluated directly only.*/ 1401 action = sch_get_action(sch);
1645 spin_unlock_irqrestore(sch->lock, flags); 1402 CIO_MSG_EVENT(2, "event: sch 0.%x.%04x, process=%d, action=%d\n",
1646 return 0; 1403 sch->schid.ssid, sch->schid.sch_no, process,
1647 } 1404 action);
1648 /* No interrupt after machine check - kill pending timers. */ 1405 /* Perform immediate actions while holding the lock. */
1649 if (cdev) 1406 switch (action) {
1650 ccw_device_set_timeout(cdev, 0); 1407 case IO_SCH_REPROBE:
1651 if (!disc && !slow) { 1408 /* Trigger device recognition. */
1652 /* Non-disconnected devices are evaluated on the slow path. */ 1409 ccw_device_trigger_reprobe(cdev);
1653 spin_unlock_irqrestore(sch->lock, flags); 1410 rc = 0;
1654 return -EAGAIN; 1411 goto out_unlock;
1412 case IO_SCH_VERIFY:
1413 /* Trigger path verification. */
1414 io_subchannel_verify(sch);
1415 rc = 0;
1416 goto out_unlock;
1417 case IO_SCH_DISC:
1418 ccw_device_set_disconnected(cdev);
1419 rc = 0;
1420 goto out_unlock;
1421 case IO_SCH_ORPH_UNREG:
1422 case IO_SCH_ORPH_ATTACH:
1423 ccw_device_set_disconnected(cdev);
1424 break;
1425 case IO_SCH_UNREG_ATTACH:
1426 case IO_SCH_UNREG:
1427 if (cdev)
1428 ccw_device_set_notoper(cdev);
1429 break;
1430 case IO_SCH_NOP:
1431 rc = 0;
1432 goto out_unlock;
1433 default:
1434 break;
1655 } 1435 }
1656 event = io_subchannel_get_status(sch); 1436 spin_unlock_irqrestore(sch->lock, flags);
1657 CIO_MSG_EVENT(4, "Evaluating schid 0.%x.%04x, event %d, %s, %s path.\n", 1437 /* All other actions require process context. */
1658 sch->schid.ssid, sch->schid.sch_no, event, 1438 if (!process)
1659 disc ? "disconnected" : "normal", 1439 goto out;
1660 slow ? "slow" : "fast"); 1440 /* Handle attached ccw device. */
1661 /* Analyze subchannel status. */ 1441 switch (action) {
1662 action = NONE; 1442 case IO_SCH_ORPH_UNREG:
1663 switch (event) { 1443 case IO_SCH_ORPH_ATTACH:
1664 case CIO_NO_PATH: 1444 /* Move ccw device to orphanage. */
1665 if (disc) { 1445 rc = ccw_device_move_to_orph(cdev);
1666 /* Check if paths have become available. */ 1446 if (rc)
1667 action = REPROBE; 1447 goto out;
1668 break;
1669 }
1670 /* fall through */
1671 case CIO_GONE:
1672 /* Ask driver what to do with device. */
1673 if (io_subchannel_notify(sch, event))
1674 action = DISC;
1675 else
1676 action = UNREGISTER;
1677 break; 1448 break;
1678 case CIO_REVALIDATE: 1449 case IO_SCH_UNREG_ATTACH:
1679 /* Device will be removed, so no notify necessary. */ 1450 /* Unregister ccw device. */
1680 if (disc) 1451 ccw_device_unregister(cdev);
1681 /* Reprobe because immediate unregister might block. */
1682 action = REPROBE;
1683 else
1684 action = UNREGISTER_PROBE;
1685 break; 1452 break;
1686 case CIO_OPER: 1453 default:
1687 if (disc)
1688 /* Get device operational again. */
1689 action = REPROBE;
1690 break; 1454 break;
1691 } 1455 }
1692 /* Perform action. */ 1456 /* Handle subchannel. */
1693 ret = 0;
1694 switch (action) { 1457 switch (action) {
1695 case UNREGISTER: 1458 case IO_SCH_ORPH_UNREG:
1696 case UNREGISTER_PROBE: 1459 case IO_SCH_UNREG:
1697 ccw_device_set_notoper(cdev);
1698 /* Unregister device (will use subchannel lock). */
1699 spin_unlock_irqrestore(sch->lock, flags);
1700 css_sch_device_unregister(sch); 1460 css_sch_device_unregister(sch);
1701 spin_lock_irqsave(sch->lock, flags);
1702 break; 1461 break;
1703 case REPROBE: 1462 case IO_SCH_ORPH_ATTACH:
1463 case IO_SCH_UNREG_ATTACH:
1464 case IO_SCH_ATTACH:
1465 dev_id.ssid = sch->schid.ssid;
1466 dev_id.devno = sch->schib.pmcw.dev;
1467 cdev = get_ccwdev_by_dev_id(&dev_id);
1468 if (!cdev) {
1469 sch_create_and_recog_new_device(sch);
1470 break;
1471 }
1472 rc = ccw_device_move_to_sch(cdev, sch);
1473 if (rc) {
1474 /* Release reference from get_ccwdev_by_dev_id() */
1475 put_device(&cdev->dev);
1476 goto out;
1477 }
1478 spin_lock_irqsave(sch->lock, flags);
1704 ccw_device_trigger_reprobe(cdev); 1479 ccw_device_trigger_reprobe(cdev);
1705 break; 1480 spin_unlock_irqrestore(sch->lock, flags);
1706 case DISC: 1481 /* Release reference from get_ccwdev_by_dev_id() */
1707 ccw_device_set_disconnected(cdev); 1482 put_device(&cdev->dev);
1708 break; 1483 break;
1709 default: 1484 default:
1710 break; 1485 break;
1711 } 1486 }
1712 spin_unlock_irqrestore(sch->lock, flags); 1487 return 0;
1713 /* Probe if necessary. */
1714 if (action == UNREGISTER_PROBE)
1715 ret = css_probe_device(sch->schid);
1716 1488
1717 return ret; 1489out_unlock:
1490 spin_unlock_irqrestore(sch->lock, flags);
1491out:
1492 return rc;
1718} 1493}
1719 1494
1720#ifdef CONFIG_CCW_CONSOLE 1495#ifdef CONFIG_CCW_CONSOLE
@@ -1744,10 +1519,7 @@ static int ccw_device_console_enable(struct ccw_device *cdev,
1744 sch->driver = &io_subchannel_driver; 1519 sch->driver = &io_subchannel_driver;
1745 /* Initialize the ccw_device structure. */ 1520 /* Initialize the ccw_device structure. */
1746 cdev->dev.parent= &sch->dev; 1521 cdev->dev.parent= &sch->dev;
1747 rc = io_subchannel_recog(cdev, sch); 1522 io_subchannel_recog(cdev, sch);
1748 if (rc)
1749 return rc;
1750
1751 /* Now wait for the async. recognition to come to an end. */ 1523 /* Now wait for the async. recognition to come to an end. */
1752 spin_lock_irq(cdev->ccwlock); 1524 spin_lock_irq(cdev->ccwlock);
1753 while (!dev_fsm_final_state(cdev)) 1525 while (!dev_fsm_final_state(cdev))
@@ -1763,7 +1535,7 @@ static int ccw_device_console_enable(struct ccw_device *cdev,
1763 rc = 0; 1535 rc = 0;
1764out_unlock: 1536out_unlock:
1765 spin_unlock_irq(cdev->ccwlock); 1537 spin_unlock_irq(cdev->ccwlock);
1766 return 0; 1538 return rc;
1767} 1539}
1768 1540
1769struct ccw_device * 1541struct ccw_device *
@@ -1919,7 +1691,7 @@ static int ccw_device_pm_prepare(struct device *dev)
1919{ 1691{
1920 struct ccw_device *cdev = to_ccwdev(dev); 1692 struct ccw_device *cdev = to_ccwdev(dev);
1921 1693
1922 if (work_pending(&cdev->private->kick_work)) 1694 if (work_pending(&cdev->private->todo_work))
1923 return -EAGAIN; 1695 return -EAGAIN;
1924 /* Fail while device is being set online/offline. */ 1696 /* Fail while device is being set online/offline. */
1925 if (atomic_read(&cdev->private->onoff)) 1697 if (atomic_read(&cdev->private->onoff))
@@ -2005,7 +1777,6 @@ static int ccw_device_pm_thaw(struct device *dev)
2005static void __ccw_device_pm_restore(struct ccw_device *cdev) 1777static void __ccw_device_pm_restore(struct ccw_device *cdev)
2006{ 1778{
2007 struct subchannel *sch = to_subchannel(cdev->dev.parent); 1779 struct subchannel *sch = to_subchannel(cdev->dev.parent);
2008 int ret;
2009 1780
2010 if (cio_is_console(sch->schid)) 1781 if (cio_is_console(sch->schid))
2011 goto out; 1782 goto out;
@@ -2015,22 +1786,10 @@ static void __ccw_device_pm_restore(struct ccw_device *cdev)
2015 */ 1786 */
2016 spin_lock_irq(sch->lock); 1787 spin_lock_irq(sch->lock);
2017 cdev->private->flags.resuming = 1; 1788 cdev->private->flags.resuming = 1;
2018 ret = ccw_device_recognition(cdev); 1789 ccw_device_recognition(cdev);
2019 spin_unlock_irq(sch->lock); 1790 spin_unlock_irq(sch->lock);
2020 if (ret) {
2021 CIO_MSG_EVENT(0, "Couldn't start recognition for device "
2022 "0.%x.%04x (ret=%d)\n",
2023 cdev->private->dev_id.ssid,
2024 cdev->private->dev_id.devno, ret);
2025 spin_lock_irq(sch->lock);
2026 cdev->private->state = DEV_STATE_DISCONNECTED;
2027 spin_unlock_irq(sch->lock);
2028 /* notify driver after the resume cb */
2029 goto out;
2030 }
2031 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev) || 1791 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev) ||
2032 cdev->private->state == DEV_STATE_DISCONNECTED); 1792 cdev->private->state == DEV_STATE_DISCONNECTED);
2033
2034out: 1793out:
2035 cdev->private->flags.resuming = 0; 1794 cdev->private->flags.resuming = 0;
2036} 1795}
@@ -2040,7 +1799,7 @@ static int resume_handle_boxed(struct ccw_device *cdev)
2040 cdev->private->state = DEV_STATE_BOXED; 1799 cdev->private->state = DEV_STATE_BOXED;
2041 if (ccw_device_notify(cdev, CIO_BOXED)) 1800 if (ccw_device_notify(cdev, CIO_BOXED))
2042 return 0; 1801 return 0;
2043 ccw_device_schedule_sch_unregister(cdev); 1802 ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
2044 return -ENODEV; 1803 return -ENODEV;
2045} 1804}
2046 1805
@@ -2049,7 +1808,7 @@ static int resume_handle_disc(struct ccw_device *cdev)
2049 cdev->private->state = DEV_STATE_DISCONNECTED; 1808 cdev->private->state = DEV_STATE_DISCONNECTED;
2050 if (ccw_device_notify(cdev, CIO_GONE)) 1809 if (ccw_device_notify(cdev, CIO_GONE))
2051 return 0; 1810 return 0;
2052 ccw_device_schedule_sch_unregister(cdev); 1811 ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
2053 return -ENODEV; 1812 return -ENODEV;
2054} 1813}
2055 1814
@@ -2094,9 +1853,7 @@ static int ccw_device_pm_restore(struct device *dev)
2094 /* check if the device type has changed */ 1853 /* check if the device type has changed */
2095 if (!ccw_device_test_sense_data(cdev)) { 1854 if (!ccw_device_test_sense_data(cdev)) {
2096 ccw_device_update_sense_data(cdev); 1855 ccw_device_update_sense_data(cdev);
2097 PREPARE_WORK(&cdev->private->kick_work, 1856 ccw_device_sched_todo(cdev, CDEV_TODO_REBIND);
2098 ccw_device_do_unbind_bind);
2099 queue_work(ccw_device_work, &cdev->private->kick_work);
2100 ret = -ENODEV; 1857 ret = -ENODEV;
2101 goto out_unlock; 1858 goto out_unlock;
2102 } 1859 }
@@ -2140,7 +1897,7 @@ out_disc_unlock:
2140 goto out_restore; 1897 goto out_restore;
2141 1898
2142out_unreg_unlock: 1899out_unreg_unlock:
2143 ccw_device_schedule_sch_unregister(cdev); 1900 ccw_device_sched_todo(cdev, CDEV_TODO_UNREG_EVAL);
2144 ret = -ENODEV; 1901 ret = -ENODEV;
2145out_unlock: 1902out_unlock:
2146 spin_unlock_irq(sch->lock); 1903 spin_unlock_irq(sch->lock);
@@ -2205,6 +1962,77 @@ ccw_device_get_subchannel_id(struct ccw_device *cdev)
2205 return sch->schid; 1962 return sch->schid;
2206} 1963}
2207 1964
1965static void ccw_device_todo(struct work_struct *work)
1966{
1967 struct ccw_device_private *priv;
1968 struct ccw_device *cdev;
1969 struct subchannel *sch;
1970 enum cdev_todo todo;
1971
1972 priv = container_of(work, struct ccw_device_private, todo_work);
1973 cdev = priv->cdev;
1974 sch = to_subchannel(cdev->dev.parent);
1975 /* Find out todo. */
1976 spin_lock_irq(cdev->ccwlock);
1977 todo = priv->todo;
1978 priv->todo = CDEV_TODO_NOTHING;
1979 CIO_MSG_EVENT(4, "cdev_todo: cdev=0.%x.%04x todo=%d\n",
1980 priv->dev_id.ssid, priv->dev_id.devno, todo);
1981 spin_unlock_irq(cdev->ccwlock);
1982 /* Perform todo. */
1983 switch (todo) {
1984 case CDEV_TODO_ENABLE_CMF:
1985 cmf_reenable(cdev);
1986 break;
1987 case CDEV_TODO_REBIND:
1988 ccw_device_do_unbind_bind(cdev);
1989 break;
1990 case CDEV_TODO_REGISTER:
1991 io_subchannel_register(cdev);
1992 break;
1993 case CDEV_TODO_UNREG_EVAL:
1994 if (!sch_is_pseudo_sch(sch))
1995 css_schedule_eval(sch->schid);
1996 /* fall-through */
1997 case CDEV_TODO_UNREG:
1998 if (sch_is_pseudo_sch(sch))
1999 ccw_device_unregister(cdev);
2000 else
2001 ccw_device_call_sch_unregister(cdev);
2002 break;
2003 default:
2004 break;
2005 }
2006 /* Release workqueue ref. */
2007 put_device(&cdev->dev);
2008}
2009
2010/**
2011 * ccw_device_sched_todo - schedule ccw device operation
2012 * @cdev: ccw device
2013 * @todo: todo
2014 *
2015 * Schedule the operation identified by @todo to be performed on the slow path
2016 * workqueue. Do nothing if another operation with higher priority is already
2017 * scheduled. Needs to be called with ccwdev lock held.
2018 */
2019void ccw_device_sched_todo(struct ccw_device *cdev, enum cdev_todo todo)
2020{
2021 CIO_MSG_EVENT(4, "cdev_todo: sched cdev=0.%x.%04x todo=%d\n",
2022 cdev->private->dev_id.ssid, cdev->private->dev_id.devno,
2023 todo);
2024 if (cdev->private->todo >= todo)
2025 return;
2026 cdev->private->todo = todo;
2027 /* Get workqueue ref. */
2028 if (!get_device(&cdev->dev))
2029 return;
2030 if (!queue_work(slow_path_wq, &cdev->private->todo_work)) {
2031 /* Already queued, release workqueue ref. */
2032 put_device(&cdev->dev);
2033 }
2034}
2035
2208MODULE_LICENSE("GPL"); 2036MODULE_LICENSE("GPL");
2209EXPORT_SYMBOL(ccw_device_set_online); 2037EXPORT_SYMBOL(ccw_device_set_online);
2210EXPORT_SYMBOL(ccw_device_set_offline); 2038EXPORT_SYMBOL(ccw_device_set_offline);