linux/drivers/s390/scsi/zfcp_erp.c
<<
>>
Prefs
   1/*
   2 * zfcp device driver
   3 *
   4 * Error Recovery Procedures (ERP).
   5 *
   6 * Copyright IBM Corporation 2002, 2009
   7 */
   8
   9#define KMSG_COMPONENT "zfcp"
  10#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
  11
  12#include "zfcp_ext.h"
  13
  14#define ZFCP_MAX_ERPS                   3
  15
  16enum zfcp_erp_act_flags {
  17        ZFCP_STATUS_ERP_TIMEDOUT        = 0x10000000,
  18        ZFCP_STATUS_ERP_CLOSE_ONLY      = 0x01000000,
  19        ZFCP_STATUS_ERP_DISMISSING      = 0x00100000,
  20        ZFCP_STATUS_ERP_DISMISSED       = 0x00200000,
  21        ZFCP_STATUS_ERP_LOWMEM          = 0x00400000,
  22};
  23
  24enum zfcp_erp_steps {
  25        ZFCP_ERP_STEP_UNINITIALIZED     = 0x0000,
  26        ZFCP_ERP_STEP_FSF_XCONFIG       = 0x0001,
  27        ZFCP_ERP_STEP_PHYS_PORT_CLOSING = 0x0010,
  28        ZFCP_ERP_STEP_PORT_CLOSING      = 0x0100,
  29        ZFCP_ERP_STEP_NAMESERVER_LOOKUP = 0x0400,
  30        ZFCP_ERP_STEP_PORT_OPENING      = 0x0800,
  31        ZFCP_ERP_STEP_UNIT_CLOSING      = 0x1000,
  32        ZFCP_ERP_STEP_UNIT_OPENING      = 0x2000,
  33};
  34
  35enum zfcp_erp_act_type {
  36        ZFCP_ERP_ACTION_REOPEN_UNIT        = 1,
  37        ZFCP_ERP_ACTION_REOPEN_PORT        = 2,
  38        ZFCP_ERP_ACTION_REOPEN_PORT_FORCED = 3,
  39        ZFCP_ERP_ACTION_REOPEN_ADAPTER     = 4,
  40};
  41
  42enum zfcp_erp_act_state {
  43        ZFCP_ERP_ACTION_RUNNING = 1,
  44        ZFCP_ERP_ACTION_READY   = 2,
  45};
  46
  47enum zfcp_erp_act_result {
  48        ZFCP_ERP_SUCCEEDED = 0,
  49        ZFCP_ERP_FAILED    = 1,
  50        ZFCP_ERP_CONTINUES = 2,
  51        ZFCP_ERP_EXIT      = 3,
  52        ZFCP_ERP_DISMISSED = 4,
  53        ZFCP_ERP_NOMEM     = 5,
  54};
  55
  56static void zfcp_erp_adapter_block(struct zfcp_adapter *adapter, int mask)
  57{
  58        zfcp_erp_modify_adapter_status(adapter, "erablk1", NULL,
  59                                       ZFCP_STATUS_COMMON_UNBLOCKED | mask,
  60                                       ZFCP_CLEAR);
  61}
  62
  63static int zfcp_erp_action_exists(struct zfcp_erp_action *act)
  64{
  65        struct zfcp_erp_action *curr_act;
  66
  67        list_for_each_entry(curr_act, &act->adapter->erp_running_head, list)
  68                if (act == curr_act)
  69                        return ZFCP_ERP_ACTION_RUNNING;
  70        return 0;
  71}
  72
  73static void zfcp_erp_action_ready(struct zfcp_erp_action *act)
  74{
  75        struct zfcp_adapter *adapter = act->adapter;
  76
  77        list_move(&act->list, &act->adapter->erp_ready_head);
  78        zfcp_rec_dbf_event_action("erardy1", act);
  79        up(&adapter->erp_ready_sem);
  80        zfcp_rec_dbf_event_thread("erardy2", adapter);
  81}
  82
  83static void zfcp_erp_action_dismiss(struct zfcp_erp_action *act)
  84{
  85        act->status |= ZFCP_STATUS_ERP_DISMISSED;
  86        if (zfcp_erp_action_exists(act) == ZFCP_ERP_ACTION_RUNNING)
  87                zfcp_erp_action_ready(act);
  88}
  89
  90static void zfcp_erp_action_dismiss_unit(struct zfcp_unit *unit)
  91{
  92        if (atomic_read(&unit->status) & ZFCP_STATUS_COMMON_ERP_INUSE)
  93                zfcp_erp_action_dismiss(&unit->erp_action);
  94}
  95
  96static void zfcp_erp_action_dismiss_port(struct zfcp_port *port)
  97{
  98        struct zfcp_unit *unit;
  99
 100        if (atomic_read(&port->status) & ZFCP_STATUS_COMMON_ERP_INUSE)
 101                zfcp_erp_action_dismiss(&port->erp_action);
 102        else
 103                list_for_each_entry(unit, &port->unit_list_head, list)
 104                    zfcp_erp_action_dismiss_unit(unit);
 105}
 106
 107static void zfcp_erp_action_dismiss_adapter(struct zfcp_adapter *adapter)
 108{
 109        struct zfcp_port *port;
 110
 111        if (atomic_read(&adapter->status) & ZFCP_STATUS_COMMON_ERP_INUSE)
 112                zfcp_erp_action_dismiss(&adapter->erp_action);
 113        else
 114                list_for_each_entry(port, &adapter->port_list_head, list)
 115                    zfcp_erp_action_dismiss_port(port);
 116}
 117
 118static int zfcp_erp_required_act(int want, struct zfcp_adapter *adapter,
 119                                 struct zfcp_port *port,
 120                                 struct zfcp_unit *unit)
 121{
 122        int need = want;
 123        int u_status, p_status, a_status;
 124
 125        switch (want) {
 126        case ZFCP_ERP_ACTION_REOPEN_UNIT:
 127                u_status = atomic_read(&unit->status);
 128                if (u_status & ZFCP_STATUS_COMMON_ERP_INUSE)
 129                        return 0;
 130                p_status = atomic_read(&port->status);
 131                if (!(p_status & ZFCP_STATUS_COMMON_RUNNING) ||
 132                      p_status & ZFCP_STATUS_COMMON_ERP_FAILED)
 133                        return 0;
 134                if (!(p_status & ZFCP_STATUS_COMMON_UNBLOCKED))
 135                        need = ZFCP_ERP_ACTION_REOPEN_PORT;
 136                /* fall through */
 137        case ZFCP_ERP_ACTION_REOPEN_PORT:
 138        case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
 139                p_status = atomic_read(&port->status);
 140                if (p_status & ZFCP_STATUS_COMMON_ERP_INUSE)
 141                        return 0;
 142                a_status = atomic_read(&adapter->status);
 143                if (!(a_status & ZFCP_STATUS_COMMON_RUNNING) ||
 144                      a_status & ZFCP_STATUS_COMMON_ERP_FAILED)
 145                        return 0;
 146                if (!(a_status & ZFCP_STATUS_COMMON_UNBLOCKED))
 147                        need = ZFCP_ERP_ACTION_REOPEN_ADAPTER;
 148                /* fall through */
 149        case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
 150                a_status = atomic_read(&adapter->status);
 151                if (a_status & ZFCP_STATUS_COMMON_ERP_INUSE)
 152                        return 0;
 153        }
 154
 155        return need;
 156}
 157
 158static struct zfcp_erp_action *zfcp_erp_setup_act(int need,
 159                                                  struct zfcp_adapter *adapter,
 160                                                  struct zfcp_port *port,
 161                                                  struct zfcp_unit *unit)
 162{
 163        struct zfcp_erp_action *erp_action;
 164        u32 status = 0;
 165
 166        switch (need) {
 167        case ZFCP_ERP_ACTION_REOPEN_UNIT:
 168                zfcp_unit_get(unit);
 169                atomic_set_mask(ZFCP_STATUS_COMMON_ERP_INUSE, &unit->status);
 170                erp_action = &unit->erp_action;
 171                if (!(atomic_read(&unit->status) & ZFCP_STATUS_COMMON_RUNNING))
 172                        status = ZFCP_STATUS_ERP_CLOSE_ONLY;
 173                break;
 174
 175        case ZFCP_ERP_ACTION_REOPEN_PORT:
 176        case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
 177                zfcp_port_get(port);
 178                zfcp_erp_action_dismiss_port(port);
 179                atomic_set_mask(ZFCP_STATUS_COMMON_ERP_INUSE, &port->status);
 180                erp_action = &port->erp_action;
 181                if (!(atomic_read(&port->status) & ZFCP_STATUS_COMMON_RUNNING))
 182                        status = ZFCP_STATUS_ERP_CLOSE_ONLY;
 183                break;
 184
 185        case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
 186                zfcp_adapter_get(adapter);
 187                zfcp_erp_action_dismiss_adapter(adapter);
 188                atomic_set_mask(ZFCP_STATUS_COMMON_ERP_INUSE, &adapter->status);
 189                erp_action = &adapter->erp_action;
 190                if (!(atomic_read(&adapter->status) &
 191                      ZFCP_STATUS_COMMON_RUNNING))
 192                        status = ZFCP_STATUS_ERP_CLOSE_ONLY;
 193                break;
 194
 195        default:
 196                return NULL;
 197        }
 198
 199        memset(erp_action, 0, sizeof(struct zfcp_erp_action));
 200        erp_action->adapter = adapter;
 201        erp_action->port = port;
 202        erp_action->unit = unit;
 203        erp_action->action = need;
 204        erp_action->status = status;
 205
 206        return erp_action;
 207}
 208
 209static int zfcp_erp_action_enqueue(int want, struct zfcp_adapter *adapter,
 210                                   struct zfcp_port *port,
 211                                   struct zfcp_unit *unit, char *id, void *ref)
 212{
 213        int retval = 1, need;
 214        struct zfcp_erp_action *act = NULL;
 215
 216        if (!(atomic_read(&adapter->status) &
 217              ZFCP_STATUS_ADAPTER_ERP_THREAD_UP))
 218                return -EIO;
 219
 220        need = zfcp_erp_required_act(want, adapter, port, unit);
 221        if (!need)
 222                goto out;
 223
 224        atomic_set_mask(ZFCP_STATUS_ADAPTER_ERP_PENDING, &adapter->status);
 225        act = zfcp_erp_setup_act(need, adapter, port, unit);
 226        if (!act)
 227                goto out;
 228        ++adapter->erp_total_count;
 229        list_add_tail(&act->list, &adapter->erp_ready_head);
 230        up(&adapter->erp_ready_sem);
 231        zfcp_rec_dbf_event_thread("eracte1", adapter);
 232        retval = 0;
 233 out:
 234        zfcp_rec_dbf_event_trigger(id, ref, want, need, act,
 235                                   adapter, port, unit);
 236        return retval;
 237}
 238
 239static int _zfcp_erp_adapter_reopen(struct zfcp_adapter *adapter,
 240                                    int clear_mask, char *id, void *ref)
 241{
 242        zfcp_erp_adapter_block(adapter, clear_mask);
 243        zfcp_scsi_schedule_rports_block(adapter);
 244
 245        /* ensure propagation of failed status to new devices */
 246        if (atomic_read(&adapter->status) & ZFCP_STATUS_COMMON_ERP_FAILED) {
 247                zfcp_erp_adapter_failed(adapter, "erareo1", NULL);
 248                return -EIO;
 249        }
 250        return zfcp_erp_action_enqueue(ZFCP_ERP_ACTION_REOPEN_ADAPTER,
 251                                       adapter, NULL, NULL, id, ref);
 252}
 253
 254/**
 255 * zfcp_erp_adapter_reopen - Reopen adapter.
 256 * @adapter: Adapter to reopen.
 257 * @clear: Status flags to clear.
 258 * @id: Id for debug trace event.
 259 * @ref: Reference for debug trace event.
 260 */
 261void zfcp_erp_adapter_reopen(struct zfcp_adapter *adapter, int clear,
 262                             char *id, void *ref)
 263{
 264        unsigned long flags;
 265
 266        read_lock_irqsave(&zfcp_data.config_lock, flags);
 267        write_lock(&adapter->erp_lock);
 268        _zfcp_erp_adapter_reopen(adapter, clear, id, ref);
 269        write_unlock(&adapter->erp_lock);
 270        read_unlock_irqrestore(&zfcp_data.config_lock, flags);
 271}
 272
 273/**
 274 * zfcp_erp_adapter_shutdown - Shutdown adapter.
 275 * @adapter: Adapter to shut down.
 276 * @clear: Status flags to clear.
 277 * @id: Id for debug trace event.
 278 * @ref: Reference for debug trace event.
 279 */
 280void zfcp_erp_adapter_shutdown(struct zfcp_adapter *adapter, int clear,
 281                               char *id, void *ref)
 282{
 283        int flags = ZFCP_STATUS_COMMON_RUNNING | ZFCP_STATUS_COMMON_ERP_FAILED;
 284        zfcp_erp_adapter_reopen(adapter, clear | flags, id, ref);
 285}
 286
 287/**
 288 * zfcp_erp_port_shutdown - Shutdown port
 289 * @port: Port to shut down.
 290 * @clear: Status flags to clear.
 291 * @id: Id for debug trace event.
 292 * @ref: Reference for debug trace event.
 293 */
 294void zfcp_erp_port_shutdown(struct zfcp_port *port, int clear, char *id,
 295                            void *ref)
 296{
 297        int flags = ZFCP_STATUS_COMMON_RUNNING | ZFCP_STATUS_COMMON_ERP_FAILED;
 298        zfcp_erp_port_reopen(port, clear | flags, id, ref);
 299}
 300
 301/**
 302 * zfcp_erp_unit_shutdown - Shutdown unit
 303 * @unit: Unit to shut down.
 304 * @clear: Status flags to clear.
 305 * @id: Id for debug trace event.
 306 * @ref: Reference for debug trace event.
 307 */
 308void zfcp_erp_unit_shutdown(struct zfcp_unit *unit, int clear, char *id,
 309                            void *ref)
 310{
 311        int flags = ZFCP_STATUS_COMMON_RUNNING | ZFCP_STATUS_COMMON_ERP_FAILED;
 312        zfcp_erp_unit_reopen(unit, clear | flags, id, ref);
 313}
 314
 315static void zfcp_erp_port_block(struct zfcp_port *port, int clear)
 316{
 317        zfcp_erp_modify_port_status(port, "erpblk1", NULL,
 318                                    ZFCP_STATUS_COMMON_UNBLOCKED | clear,
 319                                    ZFCP_CLEAR);
 320}
 321
 322static void _zfcp_erp_port_forced_reopen(struct zfcp_port *port,
 323                                         int clear, char *id, void *ref)
 324{
 325        zfcp_erp_port_block(port, clear);
 326        zfcp_scsi_schedule_rport_block(port);
 327
 328        if (atomic_read(&port->status) & ZFCP_STATUS_COMMON_ERP_FAILED)
 329                return;
 330
 331        zfcp_erp_action_enqueue(ZFCP_ERP_ACTION_REOPEN_PORT_FORCED,
 332                                port->adapter, port, NULL, id, ref);
 333}
 334
 335/**
 336 * zfcp_erp_port_forced_reopen - Forced close of port and open again
 337 * @port: Port to force close and to reopen.
 338 * @id: Id for debug trace event.
 339 * @ref: Reference for debug trace event.
 340 */
 341void zfcp_erp_port_forced_reopen(struct zfcp_port *port, int clear, char *id,
 342                                 void *ref)
 343{
 344        unsigned long flags;
 345        struct zfcp_adapter *adapter = port->adapter;
 346
 347        read_lock_irqsave(&zfcp_data.config_lock, flags);
 348        write_lock(&adapter->erp_lock);
 349        _zfcp_erp_port_forced_reopen(port, clear, id, ref);
 350        write_unlock(&adapter->erp_lock);
 351        read_unlock_irqrestore(&zfcp_data.config_lock, flags);
 352}
 353
 354static int _zfcp_erp_port_reopen(struct zfcp_port *port, int clear, char *id,
 355                                 void *ref)
 356{
 357        zfcp_erp_port_block(port, clear);
 358        zfcp_scsi_schedule_rport_block(port);
 359
 360        if (atomic_read(&port->status) & ZFCP_STATUS_COMMON_ERP_FAILED) {
 361                /* ensure propagation of failed status to new devices */
 362                zfcp_erp_port_failed(port, "erpreo1", NULL);
 363                return -EIO;
 364        }
 365
 366        return zfcp_erp_action_enqueue(ZFCP_ERP_ACTION_REOPEN_PORT,
 367                                       port->adapter, port, NULL, id, ref);
 368}
 369
 370/**
 371 * zfcp_erp_port_reopen - trigger remote port recovery
 372 * @port: port to recover
 373 * @clear_mask: flags in port status to be cleared
 374 *
 375 * Returns 0 if recovery has been triggered, < 0 if not.
 376 */
 377int zfcp_erp_port_reopen(struct zfcp_port *port, int clear, char *id, void *ref)
 378{
 379        unsigned long flags;
 380        int retval;
 381        struct zfcp_adapter *adapter = port->adapter;
 382
 383        read_lock_irqsave(&zfcp_data.config_lock, flags);
 384        write_lock(&adapter->erp_lock);
 385        retval = _zfcp_erp_port_reopen(port, clear, id, ref);
 386        write_unlock(&adapter->erp_lock);
 387        read_unlock_irqrestore(&zfcp_data.config_lock, flags);
 388
 389        return retval;
 390}
 391
 392static void zfcp_erp_unit_block(struct zfcp_unit *unit, int clear_mask)
 393{
 394        zfcp_erp_modify_unit_status(unit, "erublk1", NULL,
 395                                    ZFCP_STATUS_COMMON_UNBLOCKED | clear_mask,
 396                                    ZFCP_CLEAR);
 397}
 398
 399static void _zfcp_erp_unit_reopen(struct zfcp_unit *unit, int clear, char *id,
 400                                  void *ref)
 401{
 402        struct zfcp_adapter *adapter = unit->port->adapter;
 403
 404        zfcp_erp_unit_block(unit, clear);
 405
 406        if (atomic_read(&unit->status) & ZFCP_STATUS_COMMON_ERP_FAILED)
 407                return;
 408
 409        zfcp_erp_action_enqueue(ZFCP_ERP_ACTION_REOPEN_UNIT,
 410                                adapter, unit->port, unit, id, ref);
 411}
 412
 413/**
 414 * zfcp_erp_unit_reopen - initiate reopen of a unit
 415 * @unit: unit to be reopened
 416 * @clear_mask: specifies flags in unit status to be cleared
 417 * Return: 0 on success, < 0 on error
 418 */
 419void zfcp_erp_unit_reopen(struct zfcp_unit *unit, int clear, char *id,
 420                          void *ref)
 421{
 422        unsigned long flags;
 423        struct zfcp_port *port = unit->port;
 424        struct zfcp_adapter *adapter = port->adapter;
 425
 426        read_lock_irqsave(&zfcp_data.config_lock, flags);
 427        write_lock(&adapter->erp_lock);
 428        _zfcp_erp_unit_reopen(unit, clear, id, ref);
 429        write_unlock(&adapter->erp_lock);
 430        read_unlock_irqrestore(&zfcp_data.config_lock, flags);
 431}
 432
 433static int status_change_set(unsigned long mask, atomic_t *status)
 434{
 435        return (atomic_read(status) ^ mask) & mask;
 436}
 437
 438static int status_change_clear(unsigned long mask, atomic_t *status)
 439{
 440        return atomic_read(status) & mask;
 441}
 442
 443static void zfcp_erp_adapter_unblock(struct zfcp_adapter *adapter)
 444{
 445        if (status_change_set(ZFCP_STATUS_COMMON_UNBLOCKED, &adapter->status))
 446                zfcp_rec_dbf_event_adapter("eraubl1", NULL, adapter);
 447        atomic_set_mask(ZFCP_STATUS_COMMON_UNBLOCKED, &adapter->status);
 448}
 449
 450static void zfcp_erp_port_unblock(struct zfcp_port *port)
 451{
 452        if (status_change_set(ZFCP_STATUS_COMMON_UNBLOCKED, &port->status))
 453                zfcp_rec_dbf_event_port("erpubl1", NULL, port);
 454        atomic_set_mask(ZFCP_STATUS_COMMON_UNBLOCKED, &port->status);
 455}
 456
 457static void zfcp_erp_unit_unblock(struct zfcp_unit *unit)
 458{
 459        if (status_change_set(ZFCP_STATUS_COMMON_UNBLOCKED, &unit->status))
 460                zfcp_rec_dbf_event_unit("eruubl1", NULL, unit);
 461        atomic_set_mask(ZFCP_STATUS_COMMON_UNBLOCKED, &unit->status);
 462}
 463
 464static void zfcp_erp_action_to_running(struct zfcp_erp_action *erp_action)
 465{
 466        list_move(&erp_action->list, &erp_action->adapter->erp_running_head);
 467        zfcp_rec_dbf_event_action("erator1", erp_action);
 468}
 469
 470static void zfcp_erp_strategy_check_fsfreq(struct zfcp_erp_action *act)
 471{
 472        struct zfcp_adapter *adapter = act->adapter;
 473
 474        if (!act->fsf_req)
 475                return;
 476
 477        spin_lock(&adapter->req_list_lock);
 478        if (zfcp_reqlist_find_safe(adapter, act->fsf_req) &&
 479            act->fsf_req->erp_action == act) {
 480                if (act->status & (ZFCP_STATUS_ERP_DISMISSED |
 481                                   ZFCP_STATUS_ERP_TIMEDOUT)) {
 482                        act->fsf_req->status |= ZFCP_STATUS_FSFREQ_DISMISSED;
 483                        zfcp_rec_dbf_event_action("erscf_1", act);
 484                        act->fsf_req->erp_action = NULL;
 485                }
 486                if (act->status & ZFCP_STATUS_ERP_TIMEDOUT)
 487                        zfcp_rec_dbf_event_action("erscf_2", act);
 488                if (act->fsf_req->status & (ZFCP_STATUS_FSFREQ_COMPLETED |
 489                                            ZFCP_STATUS_FSFREQ_DISMISSED))
 490                        act->fsf_req = NULL;
 491        } else
 492                act->fsf_req = NULL;
 493        spin_unlock(&adapter->req_list_lock);
 494}
 495
 496/**
 497 * zfcp_erp_notify - Trigger ERP action.
 498 * @erp_action: ERP action to continue.
 499 * @set_mask: ERP action status flags to set.
 500 */
 501void zfcp_erp_notify(struct zfcp_erp_action *erp_action, unsigned long set_mask)
 502{
 503        struct zfcp_adapter *adapter = erp_action->adapter;
 504        unsigned long flags;
 505
 506        write_lock_irqsave(&adapter->erp_lock, flags);
 507        if (zfcp_erp_action_exists(erp_action) == ZFCP_ERP_ACTION_RUNNING) {
 508                erp_action->status |= set_mask;
 509                zfcp_erp_action_ready(erp_action);
 510        }
 511        write_unlock_irqrestore(&adapter->erp_lock, flags);
 512}
 513
 514/**
 515 * zfcp_erp_timeout_handler - Trigger ERP action from timed out ERP request
 516 * @data: ERP action (from timer data)
 517 */
 518void zfcp_erp_timeout_handler(unsigned long data)
 519{
 520        struct zfcp_erp_action *act = (struct zfcp_erp_action *) data;
 521        zfcp_erp_notify(act, ZFCP_STATUS_ERP_TIMEDOUT);
 522}
 523
 524static void zfcp_erp_memwait_handler(unsigned long data)
 525{
 526        zfcp_erp_notify((struct zfcp_erp_action *)data, 0);
 527}
 528
 529static void zfcp_erp_strategy_memwait(struct zfcp_erp_action *erp_action)
 530{
 531        init_timer(&erp_action->timer);
 532        erp_action->timer.function = zfcp_erp_memwait_handler;
 533        erp_action->timer.data = (unsigned long) erp_action;
 534        erp_action->timer.expires = jiffies + HZ;
 535        add_timer(&erp_action->timer);
 536}
 537
 538static void _zfcp_erp_port_reopen_all(struct zfcp_adapter *adapter,
 539                                      int clear, char *id, void *ref)
 540{
 541        struct zfcp_port *port;
 542
 543        list_for_each_entry(port, &adapter->port_list_head, list)
 544                _zfcp_erp_port_reopen(port, clear, id, ref);
 545}
 546
 547static void _zfcp_erp_unit_reopen_all(struct zfcp_port *port, int clear,
 548                                      char *id, void *ref)
 549{
 550        struct zfcp_unit *unit;
 551
 552        list_for_each_entry(unit, &port->unit_list_head, list)
 553                _zfcp_erp_unit_reopen(unit, clear, id, ref);
 554}
 555
 556static void zfcp_erp_strategy_followup_actions(struct zfcp_erp_action *act)
 557{
 558        struct zfcp_adapter *adapter = act->adapter;
 559        struct zfcp_port *port = act->port;
 560        struct zfcp_unit *unit = act->unit;
 561        u32 status = act->status;
 562
 563        /* initiate follow-up actions depending on success of finished action */
 564        switch (act->action) {
 565
 566        case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
 567                if (status == ZFCP_ERP_SUCCEEDED)
 568                        _zfcp_erp_port_reopen_all(adapter, 0, "ersfa_1", NULL);
 569                else
 570                        _zfcp_erp_adapter_reopen(adapter, 0, "ersfa_2", NULL);
 571                break;
 572
 573        case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
 574                if (status == ZFCP_ERP_SUCCEEDED)
 575                        _zfcp_erp_port_reopen(port, 0, "ersfa_3", NULL);
 576                else
 577                        _zfcp_erp_adapter_reopen(adapter, 0, "ersfa_4", NULL);
 578                break;
 579
 580        case ZFCP_ERP_ACTION_REOPEN_PORT:
 581                if (status == ZFCP_ERP_SUCCEEDED)
 582                        _zfcp_erp_unit_reopen_all(port, 0, "ersfa_5", NULL);
 583                else
 584                        _zfcp_erp_port_forced_reopen(port, 0, "ersfa_6", NULL);
 585                break;
 586
 587        case ZFCP_ERP_ACTION_REOPEN_UNIT:
 588                if (status != ZFCP_ERP_SUCCEEDED)
 589                        _zfcp_erp_port_reopen(unit->port, 0, "ersfa_7", NULL);
 590                break;
 591        }
 592}
 593
 594static void zfcp_erp_wakeup(struct zfcp_adapter *adapter)
 595{
 596        unsigned long flags;
 597
 598        read_lock_irqsave(&zfcp_data.config_lock, flags);
 599        read_lock(&adapter->erp_lock);
 600        if (list_empty(&adapter->erp_ready_head) &&
 601            list_empty(&adapter->erp_running_head)) {
 602                        atomic_clear_mask(ZFCP_STATUS_ADAPTER_ERP_PENDING,
 603                                          &adapter->status);
 604                        wake_up(&adapter->erp_done_wqh);
 605        }
 606        read_unlock(&adapter->erp_lock);
 607        read_unlock_irqrestore(&zfcp_data.config_lock, flags);
 608}
 609
 610static int zfcp_erp_adapter_strategy_open_qdio(struct zfcp_erp_action *act)
 611{
 612        if (zfcp_qdio_open(act->adapter))
 613                return ZFCP_ERP_FAILED;
 614        init_waitqueue_head(&act->adapter->request_wq);
 615        atomic_set_mask(ZFCP_STATUS_ADAPTER_QDIOUP, &act->adapter->status);
 616        return ZFCP_ERP_SUCCEEDED;
 617}
 618
 619static void zfcp_erp_enqueue_ptp_port(struct zfcp_adapter *adapter)
 620{
 621        struct zfcp_port *port;
 622        port = zfcp_port_enqueue(adapter, adapter->peer_wwpn, 0,
 623                                 adapter->peer_d_id);
 624        if (IS_ERR(port)) /* error or port already attached */
 625                return;
 626        _zfcp_erp_port_reopen(port, 0, "ereptp1", NULL);
 627}
 628
 629static int zfcp_erp_adapter_strat_fsf_xconf(struct zfcp_erp_action *erp_action)
 630{
 631        int retries;
 632        int sleep = 1;
 633        struct zfcp_adapter *adapter = erp_action->adapter;
 634
 635        atomic_clear_mask(ZFCP_STATUS_ADAPTER_XCONFIG_OK, &adapter->status);
 636
 637        for (retries = 7; retries; retries--) {
 638                atomic_clear_mask(ZFCP_STATUS_ADAPTER_HOST_CON_INIT,
 639                                  &adapter->status);
 640                write_lock_irq(&adapter->erp_lock);
 641                zfcp_erp_action_to_running(erp_action);
 642                write_unlock_irq(&adapter->erp_lock);
 643                if (zfcp_fsf_exchange_config_data(erp_action)) {
 644                        atomic_clear_mask(ZFCP_STATUS_ADAPTER_HOST_CON_INIT,
 645                                          &adapter->status);
 646                        return ZFCP_ERP_FAILED;
 647                }
 648
 649                zfcp_rec_dbf_event_thread_lock("erasfx1", adapter);
 650                down(&adapter->erp_ready_sem);
 651                zfcp_rec_dbf_event_thread_lock("erasfx2", adapter);
 652                if (erp_action->status & ZFCP_STATUS_ERP_TIMEDOUT)
 653                        break;
 654
 655                if (!(atomic_read(&adapter->status) &
 656                      ZFCP_STATUS_ADAPTER_HOST_CON_INIT))
 657                        break;
 658
 659                ssleep(sleep);
 660                sleep *= 2;
 661        }
 662
 663        atomic_clear_mask(ZFCP_STATUS_ADAPTER_HOST_CON_INIT,
 664                          &adapter->status);
 665
 666        if (!(atomic_read(&adapter->status) & ZFCP_STATUS_ADAPTER_XCONFIG_OK))
 667                return ZFCP_ERP_FAILED;
 668
 669        if (fc_host_port_type(adapter->scsi_host) == FC_PORTTYPE_PTP)
 670                zfcp_erp_enqueue_ptp_port(adapter);
 671
 672        return ZFCP_ERP_SUCCEEDED;
 673}
 674
 675static int zfcp_erp_adapter_strategy_open_fsf_xport(struct zfcp_erp_action *act)
 676{
 677        int ret;
 678        struct zfcp_adapter *adapter = act->adapter;
 679
 680        write_lock_irq(&adapter->erp_lock);
 681        zfcp_erp_action_to_running(act);
 682        write_unlock_irq(&adapter->erp_lock);
 683
 684        ret = zfcp_fsf_exchange_port_data(act);
 685        if (ret == -EOPNOTSUPP)
 686                return ZFCP_ERP_SUCCEEDED;
 687        if (ret)
 688                return ZFCP_ERP_FAILED;
 689
 690        zfcp_rec_dbf_event_thread_lock("erasox1", adapter);
 691        down(&adapter->erp_ready_sem);
 692        zfcp_rec_dbf_event_thread_lock("erasox2", adapter);
 693        if (act->status & ZFCP_STATUS_ERP_TIMEDOUT)
 694                return ZFCP_ERP_FAILED;
 695
 696        return ZFCP_ERP_SUCCEEDED;
 697}
 698
 699static int zfcp_erp_adapter_strategy_open_fsf(struct zfcp_erp_action *act)
 700{
 701        if (zfcp_erp_adapter_strat_fsf_xconf(act) == ZFCP_ERP_FAILED)
 702                return ZFCP_ERP_FAILED;
 703
 704        if (zfcp_erp_adapter_strategy_open_fsf_xport(act) == ZFCP_ERP_FAILED)
 705                return ZFCP_ERP_FAILED;
 706
 707        atomic_set(&act->adapter->stat_miss, 16);
 708        if (zfcp_status_read_refill(act->adapter))
 709                return ZFCP_ERP_FAILED;
 710
 711        return ZFCP_ERP_SUCCEEDED;
 712}
 713
 714static void zfcp_erp_adapter_strategy_close(struct zfcp_erp_action *act)
 715{
 716        struct zfcp_adapter *adapter = act->adapter;
 717
 718        /* close queues to ensure that buffers are not accessed by adapter */
 719        zfcp_qdio_close(adapter);
 720        zfcp_fsf_req_dismiss_all(adapter);
 721        adapter->fsf_req_seq_no = 0;
 722        zfcp_fc_wka_port_force_offline(&adapter->nsp);
 723        /* all ports and units are closed */
 724        zfcp_erp_modify_adapter_status(adapter, "erascl1", NULL,
 725                                       ZFCP_STATUS_COMMON_OPEN, ZFCP_CLEAR);
 726
 727        atomic_clear_mask(ZFCP_STATUS_ADAPTER_XCONFIG_OK |
 728                          ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED, &adapter->status);
 729}
 730
 731static int zfcp_erp_adapter_strategy_open(struct zfcp_erp_action *act)
 732{
 733        struct zfcp_adapter *adapter = act->adapter;
 734
 735        if (zfcp_erp_adapter_strategy_open_qdio(act)) {
 736                atomic_clear_mask(ZFCP_STATUS_ADAPTER_XCONFIG_OK |
 737                                  ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED,
 738                                  &adapter->status);
 739                return ZFCP_ERP_FAILED;
 740        }
 741
 742        if (zfcp_erp_adapter_strategy_open_fsf(act)) {
 743                zfcp_erp_adapter_strategy_close(act);
 744                return ZFCP_ERP_FAILED;
 745        }
 746
 747        atomic_set_mask(ZFCP_STATUS_COMMON_OPEN, &adapter->status);
 748
 749        return ZFCP_ERP_SUCCEEDED;
 750}
 751
 752static int zfcp_erp_adapter_strategy(struct zfcp_erp_action *act)
 753{
 754        struct zfcp_adapter *adapter = act->adapter;
 755
 756        if (atomic_read(&adapter->status) & ZFCP_STATUS_COMMON_OPEN) {
 757                zfcp_erp_adapter_strategy_close(act);
 758                if (act->status & ZFCP_STATUS_ERP_CLOSE_ONLY)
 759                        return ZFCP_ERP_EXIT;
 760        }
 761
 762        if (zfcp_erp_adapter_strategy_open(act)) {
 763                ssleep(8);
 764                return ZFCP_ERP_FAILED;
 765        }
 766
 767        return ZFCP_ERP_SUCCEEDED;
 768}
 769
 770static int zfcp_erp_port_forced_strategy_close(struct zfcp_erp_action *act)
 771{
 772        int retval;
 773
 774        retval = zfcp_fsf_close_physical_port(act);
 775        if (retval == -ENOMEM)
 776                return ZFCP_ERP_NOMEM;
 777        act->step = ZFCP_ERP_STEP_PHYS_PORT_CLOSING;
 778        if (retval)
 779                return ZFCP_ERP_FAILED;
 780
 781        return ZFCP_ERP_CONTINUES;
 782}
 783
 784static void zfcp_erp_port_strategy_clearstati(struct zfcp_port *port)
 785{
 786        atomic_clear_mask(ZFCP_STATUS_COMMON_ACCESS_DENIED, &port->status);
 787}
 788
 789static int zfcp_erp_port_forced_strategy(struct zfcp_erp_action *erp_action)
 790{
 791        struct zfcp_port *port = erp_action->port;
 792        int status = atomic_read(&port->status);
 793
 794        switch (erp_action->step) {
 795        case ZFCP_ERP_STEP_UNINITIALIZED:
 796                zfcp_erp_port_strategy_clearstati(port);
 797                if ((status & ZFCP_STATUS_PORT_PHYS_OPEN) &&
 798                    (status & ZFCP_STATUS_COMMON_OPEN))
 799                        return zfcp_erp_port_forced_strategy_close(erp_action);
 800                else
 801                        return ZFCP_ERP_FAILED;
 802
 803        case ZFCP_ERP_STEP_PHYS_PORT_CLOSING:
 804                if (status & ZFCP_STATUS_PORT_PHYS_OPEN)
 805                        return ZFCP_ERP_SUCCEEDED;
 806        }
 807        return ZFCP_ERP_FAILED;
 808}
 809
 810static int zfcp_erp_port_strategy_close(struct zfcp_erp_action *erp_action)
 811{
 812        int retval;
 813
 814        retval = zfcp_fsf_close_port(erp_action);
 815        if (retval == -ENOMEM)
 816                return ZFCP_ERP_NOMEM;
 817        erp_action->step = ZFCP_ERP_STEP_PORT_CLOSING;
 818        if (retval)
 819                return ZFCP_ERP_FAILED;
 820        return ZFCP_ERP_CONTINUES;
 821}
 822
 823static int zfcp_erp_port_strategy_open_port(struct zfcp_erp_action *erp_action)
 824{
 825        int retval;
 826
 827        retval = zfcp_fsf_open_port(erp_action);
 828        if (retval == -ENOMEM)
 829                return ZFCP_ERP_NOMEM;
 830        erp_action->step = ZFCP_ERP_STEP_PORT_OPENING;
 831        if (retval)
 832                return ZFCP_ERP_FAILED;
 833        return ZFCP_ERP_CONTINUES;
 834}
 835
 836static int zfcp_erp_open_ptp_port(struct zfcp_erp_action *act)
 837{
 838        struct zfcp_adapter *adapter = act->adapter;
 839        struct zfcp_port *port = act->port;
 840
 841        if (port->wwpn != adapter->peer_wwpn) {
 842                zfcp_erp_port_failed(port, "eroptp1", NULL);
 843                return ZFCP_ERP_FAILED;
 844        }
 845        port->d_id = adapter->peer_d_id;
 846        return zfcp_erp_port_strategy_open_port(act);
 847}
 848
 849void zfcp_erp_port_strategy_open_lookup(struct work_struct *work)
 850{
 851        int retval;
 852        struct zfcp_port *port = container_of(work, struct zfcp_port,
 853                                              gid_pn_work);
 854
 855        retval = zfcp_fc_ns_gid_pn(&port->erp_action);
 856        if (retval == -ENOMEM)
 857                zfcp_erp_notify(&port->erp_action, ZFCP_ERP_NOMEM);
 858        port->erp_action.step = ZFCP_ERP_STEP_NAMESERVER_LOOKUP;
 859        if (retval)
 860                zfcp_erp_notify(&port->erp_action, ZFCP_ERP_FAILED);
 861        zfcp_port_put(port);
 862}
 863
 864static int zfcp_erp_port_strategy_open_common(struct zfcp_erp_action *act)
 865{
 866        struct zfcp_adapter *adapter = act->adapter;
 867        struct zfcp_port *port = act->port;
 868        int p_status = atomic_read(&port->status);
 869
 870        switch (act->step) {
 871        case ZFCP_ERP_STEP_UNINITIALIZED:
 872        case ZFCP_ERP_STEP_PHYS_PORT_CLOSING:
 873        case ZFCP_ERP_STEP_PORT_CLOSING:
 874                if (fc_host_port_type(adapter->scsi_host) == FC_PORTTYPE_PTP)
 875                        return zfcp_erp_open_ptp_port(act);
 876                if (!port->d_id) {
 877                        zfcp_port_get(port);
 878                        if (!queue_work(zfcp_data.work_queue,
 879                                        &port->gid_pn_work))
 880                                zfcp_port_put(port);
 881                        return ZFCP_ERP_CONTINUES;
 882                }
 883        case ZFCP_ERP_STEP_NAMESERVER_LOOKUP:
 884                if (!port->d_id)
 885                        return ZFCP_ERP_FAILED;
 886                return zfcp_erp_port_strategy_open_port(act);
 887
 888        case ZFCP_ERP_STEP_PORT_OPENING:
 889                /* D_ID might have changed during open */
 890                if (p_status & ZFCP_STATUS_COMMON_OPEN) {
 891                        if (port->d_id)
 892                                return ZFCP_ERP_SUCCEEDED;
 893                        else {
 894                                act->step = ZFCP_ERP_STEP_PORT_CLOSING;
 895                                return ZFCP_ERP_CONTINUES;
 896                        }
 897                /* fall through otherwise */
 898                }
 899        }
 900        return ZFCP_ERP_FAILED;
 901}
 902
 903static int zfcp_erp_port_strategy(struct zfcp_erp_action *erp_action)
 904{
 905        struct zfcp_port *port = erp_action->port;
 906
 907        if (atomic_read(&port->status) & ZFCP_STATUS_COMMON_NOESC)
 908                goto close_init_done;
 909
 910        switch (erp_action->step) {
 911        case ZFCP_ERP_STEP_UNINITIALIZED:
 912                zfcp_erp_port_strategy_clearstati(port);
 913                if (atomic_read(&port->status) & ZFCP_STATUS_COMMON_OPEN)
 914                        return zfcp_erp_port_strategy_close(erp_action);
 915                break;
 916
 917        case ZFCP_ERP_STEP_PORT_CLOSING:
 918                if (atomic_read(&port->status) & ZFCP_STATUS_COMMON_OPEN)
 919                        return ZFCP_ERP_FAILED;
 920                break;
 921        }
 922
 923close_init_done:
 924        if (erp_action->status & ZFCP_STATUS_ERP_CLOSE_ONLY)
 925                return ZFCP_ERP_EXIT;
 926
 927        return zfcp_erp_port_strategy_open_common(erp_action);
 928}
 929
 930static void zfcp_erp_unit_strategy_clearstati(struct zfcp_unit *unit)
 931{
 932        atomic_clear_mask(ZFCP_STATUS_COMMON_ACCESS_DENIED |
 933                          ZFCP_STATUS_UNIT_SHARED |
 934                          ZFCP_STATUS_UNIT_READONLY,
 935                          &unit->status);
 936}
 937
 938static int zfcp_erp_unit_strategy_close(struct zfcp_erp_action *erp_action)
 939{
 940        int retval = zfcp_fsf_close_unit(erp_action);
 941        if (retval == -ENOMEM)
 942                return ZFCP_ERP_NOMEM;
 943        erp_action->step = ZFCP_ERP_STEP_UNIT_CLOSING;
 944        if (retval)
 945                return ZFCP_ERP_FAILED;
 946        return ZFCP_ERP_CONTINUES;
 947}
 948
 949static int zfcp_erp_unit_strategy_open(struct zfcp_erp_action *erp_action)
 950{
 951        int retval = zfcp_fsf_open_unit(erp_action);
 952        if (retval == -ENOMEM)
 953                return ZFCP_ERP_NOMEM;
 954        erp_action->step = ZFCP_ERP_STEP_UNIT_OPENING;
 955        if (retval)
 956                return  ZFCP_ERP_FAILED;
 957        return ZFCP_ERP_CONTINUES;
 958}
 959
 960static int zfcp_erp_unit_strategy(struct zfcp_erp_action *erp_action)
 961{
 962        struct zfcp_unit *unit = erp_action->unit;
 963
 964        switch (erp_action->step) {
 965        case ZFCP_ERP_STEP_UNINITIALIZED:
 966                zfcp_erp_unit_strategy_clearstati(unit);
 967                if (atomic_read(&unit->status) & ZFCP_STATUS_COMMON_OPEN)
 968                        return zfcp_erp_unit_strategy_close(erp_action);
 969                /* already closed, fall through */
 970        case ZFCP_ERP_STEP_UNIT_CLOSING:
 971                if (atomic_read(&unit->status) & ZFCP_STATUS_COMMON_OPEN)
 972                        return ZFCP_ERP_FAILED;
 973                if (erp_action->status & ZFCP_STATUS_ERP_CLOSE_ONLY)
 974                        return ZFCP_ERP_EXIT;
 975                return zfcp_erp_unit_strategy_open(erp_action);
 976
 977        case ZFCP_ERP_STEP_UNIT_OPENING:
 978                if (atomic_read(&unit->status) & ZFCP_STATUS_COMMON_OPEN)
 979                        return ZFCP_ERP_SUCCEEDED;
 980        }
 981        return ZFCP_ERP_FAILED;
 982}
 983
 984static int zfcp_erp_strategy_check_unit(struct zfcp_unit *unit, int result)
 985{
 986        switch (result) {
 987        case ZFCP_ERP_SUCCEEDED :
 988                atomic_set(&unit->erp_counter, 0);
 989                zfcp_erp_unit_unblock(unit);
 990                break;
 991        case ZFCP_ERP_FAILED :
 992                atomic_inc(&unit->erp_counter);
 993                if (atomic_read(&unit->erp_counter) > ZFCP_MAX_ERPS) {
 994                        dev_err(&unit->port->adapter->ccw_device->dev,
 995                                "ERP failed for unit 0x%016Lx on "
 996                                "port 0x%016Lx\n",
 997                                (unsigned long long)unit->fcp_lun,
 998                                (unsigned long long)unit->port->wwpn);
 999                        zfcp_erp_unit_failed(unit, "erusck1", NULL);
1000                }
1001                break;
1002        }
1003
1004        if (atomic_read(&unit->status) & ZFCP_STATUS_COMMON_ERP_FAILED) {
1005                zfcp_erp_unit_block(unit, 0);
1006                result = ZFCP_ERP_EXIT;
1007        }
1008        return result;
1009}
1010
1011static int zfcp_erp_strategy_check_port(struct zfcp_port *port, int result)
1012{
1013        switch (result) {
1014        case ZFCP_ERP_SUCCEEDED :
1015                atomic_set(&port->erp_counter, 0);
1016                zfcp_erp_port_unblock(port);
1017                break;
1018
1019        case ZFCP_ERP_FAILED :
1020                if (atomic_read(&port->status) & ZFCP_STATUS_COMMON_NOESC) {
1021                        zfcp_erp_port_block(port, 0);
1022                        result = ZFCP_ERP_EXIT;
1023                }
1024                atomic_inc(&port->erp_counter);
1025                if (atomic_read(&port->erp_counter) > ZFCP_MAX_ERPS) {
1026                        dev_err(&port->adapter->ccw_device->dev,
1027                                "ERP failed for remote port 0x%016Lx\n",
1028                                (unsigned long long)port->wwpn);
1029                        zfcp_erp_port_failed(port, "erpsck1", NULL);
1030                }
1031                break;
1032        }
1033
1034        if (atomic_read(&port->status) & ZFCP_STATUS_COMMON_ERP_FAILED) {
1035                zfcp_erp_port_block(port, 0);
1036                result = ZFCP_ERP_EXIT;
1037        }
1038        return result;
1039}
1040
1041static int zfcp_erp_strategy_check_adapter(struct zfcp_adapter *adapter,
1042                                           int result)
1043{
1044        switch (result) {
1045        case ZFCP_ERP_SUCCEEDED :
1046                atomic_set(&adapter->erp_counter, 0);
1047                zfcp_erp_adapter_unblock(adapter);
1048                break;
1049
1050        case ZFCP_ERP_FAILED :
1051                atomic_inc(&adapter->erp_counter);
1052                if (atomic_read(&adapter->erp_counter) > ZFCP_MAX_ERPS) {
1053                        dev_err(&adapter->ccw_device->dev,
1054                                "ERP cannot recover an error "
1055                                "on the FCP device\n");
1056                        zfcp_erp_adapter_failed(adapter, "erasck1", NULL);
1057                }
1058                break;
1059        }
1060
1061        if (atomic_read(&adapter->status) & ZFCP_STATUS_COMMON_ERP_FAILED) {
1062                zfcp_erp_adapter_block(adapter, 0);
1063                result = ZFCP_ERP_EXIT;
1064        }
1065        return result;
1066}
1067
1068static int zfcp_erp_strategy_check_target(struct zfcp_erp_action *erp_action,
1069                                          int result)
1070{
1071        struct zfcp_adapter *adapter = erp_action->adapter;
1072        struct zfcp_port *port = erp_action->port;
1073        struct zfcp_unit *unit = erp_action->unit;
1074
1075        switch (erp_action->action) {
1076
1077        case ZFCP_ERP_ACTION_REOPEN_UNIT:
1078                result = zfcp_erp_strategy_check_unit(unit, result);
1079                break;
1080
1081        case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
1082        case ZFCP_ERP_ACTION_REOPEN_PORT:
1083                result = zfcp_erp_strategy_check_port(port, result);
1084                break;
1085
1086        case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
1087                result = zfcp_erp_strategy_check_adapter(adapter, result);
1088                break;
1089        }
1090        return result;
1091}
1092
1093static int zfcp_erp_strat_change_det(atomic_t *target_status, u32 erp_status)
1094{
1095        int status = atomic_read(target_status);
1096
1097        if ((status & ZFCP_STATUS_COMMON_RUNNING) &&
1098            (erp_status & ZFCP_STATUS_ERP_CLOSE_ONLY))
1099                return 1; /* take it online */
1100
1101        if (!(status & ZFCP_STATUS_COMMON_RUNNING) &&
1102            !(erp_status & ZFCP_STATUS_ERP_CLOSE_ONLY))
1103                return 1; /* take it offline */
1104
1105        return 0;
1106}
1107
1108static int zfcp_erp_strategy_statechange(struct zfcp_erp_action *act, int ret)
1109{
1110        int action = act->action;
1111        struct zfcp_adapter *adapter = act->adapter;
1112        struct zfcp_port *port = act->port;
1113        struct zfcp_unit *unit = act->unit;
1114        u32 erp_status = act->status;
1115
1116        switch (action) {
1117        case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
1118                if (zfcp_erp_strat_change_det(&adapter->status, erp_status)) {
1119                        _zfcp_erp_adapter_reopen(adapter,
1120                                                 ZFCP_STATUS_COMMON_ERP_FAILED,
1121                                                 "ersscg1", NULL);
1122                        return ZFCP_ERP_EXIT;
1123                }
1124                break;
1125
1126        case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
1127        case ZFCP_ERP_ACTION_REOPEN_PORT:
1128                if (zfcp_erp_strat_change_det(&port->status, erp_status)) {
1129                        _zfcp_erp_port_reopen(port,
1130                                              ZFCP_STATUS_COMMON_ERP_FAILED,
1131                                              "ersscg2", NULL);
1132                        return ZFCP_ERP_EXIT;
1133                }
1134                break;
1135
1136        case ZFCP_ERP_ACTION_REOPEN_UNIT:
1137                if (zfcp_erp_strat_change_det(&unit->status, erp_status)) {
1138                        _zfcp_erp_unit_reopen(unit,
1139                                              ZFCP_STATUS_COMMON_ERP_FAILED,
1140                                              "ersscg3", NULL);
1141                        return ZFCP_ERP_EXIT;
1142                }
1143                break;
1144        }
1145        return ret;
1146}
1147
1148static void zfcp_erp_action_dequeue(struct zfcp_erp_action *erp_action)
1149{
1150        struct zfcp_adapter *adapter = erp_action->adapter;
1151
1152        adapter->erp_total_count--;
1153        if (erp_action->status & ZFCP_STATUS_ERP_LOWMEM) {
1154                adapter->erp_low_mem_count--;
1155                erp_action->status &= ~ZFCP_STATUS_ERP_LOWMEM;
1156        }
1157
1158        list_del(&erp_action->list);
1159        zfcp_rec_dbf_event_action("eractd1", erp_action);
1160
1161        switch (erp_action->action) {
1162        case ZFCP_ERP_ACTION_REOPEN_UNIT:
1163                atomic_clear_mask(ZFCP_STATUS_COMMON_ERP_INUSE,
1164                                  &erp_action->unit->status);
1165                break;
1166
1167        case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
1168        case ZFCP_ERP_ACTION_REOPEN_PORT:
1169                atomic_clear_mask(ZFCP_STATUS_COMMON_ERP_INUSE,
1170                                  &erp_action->port->status);
1171                break;
1172
1173        case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
1174                atomic_clear_mask(ZFCP_STATUS_COMMON_ERP_INUSE,
1175                                  &erp_action->adapter->status);
1176                break;
1177        }
1178}
1179
1180static void zfcp_erp_action_cleanup(struct zfcp_erp_action *act, int result)
1181{
1182        struct zfcp_adapter *adapter = act->adapter;
1183        struct zfcp_port *port = act->port;
1184        struct zfcp_unit *unit = act->unit;
1185
1186        switch (act->action) {
1187        case ZFCP_ERP_ACTION_REOPEN_UNIT:
1188                if ((result == ZFCP_ERP_SUCCEEDED) && !unit->device) {
1189                        zfcp_unit_get(unit);
1190                        if (scsi_queue_work(unit->port->adapter->scsi_host,
1191                                            &unit->scsi_work) <= 0)
1192                                zfcp_unit_put(unit);
1193                }
1194                zfcp_unit_put(unit);
1195                break;
1196
1197        case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
1198        case ZFCP_ERP_ACTION_REOPEN_PORT:
1199                if (result == ZFCP_ERP_SUCCEEDED)
1200                        zfcp_scsi_schedule_rport_register(port);
1201                zfcp_port_put(port);
1202                break;
1203
1204        case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
1205                if (result == ZFCP_ERP_SUCCEEDED) {
1206                        register_service_level(&adapter->service_level);
1207                        schedule_work(&adapter->scan_work);
1208                } else
1209                        unregister_service_level(&adapter->service_level);
1210                zfcp_adapter_put(adapter);
1211                break;
1212        }
1213}
1214
1215static int zfcp_erp_strategy_do_action(struct zfcp_erp_action *erp_action)
1216{
1217        switch (erp_action->action) {
1218        case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
1219                return zfcp_erp_adapter_strategy(erp_action);
1220        case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
1221                return zfcp_erp_port_forced_strategy(erp_action);
1222        case ZFCP_ERP_ACTION_REOPEN_PORT:
1223                return zfcp_erp_port_strategy(erp_action);
1224        case ZFCP_ERP_ACTION_REOPEN_UNIT:
1225                return zfcp_erp_unit_strategy(erp_action);
1226        }
1227        return ZFCP_ERP_FAILED;
1228}
1229
1230static int zfcp_erp_strategy(struct zfcp_erp_action *erp_action)
1231{
1232        int retval;
1233        struct zfcp_adapter *adapter = erp_action->adapter;
1234        unsigned long flags;
1235
1236        read_lock_irqsave(&zfcp_data.config_lock, flags);
1237        write_lock(&adapter->erp_lock);
1238
1239        zfcp_erp_strategy_check_fsfreq(erp_action);
1240
1241        if (erp_action->status & ZFCP_STATUS_ERP_DISMISSED) {
1242                zfcp_erp_action_dequeue(erp_action);
1243                retval = ZFCP_ERP_DISMISSED;
1244                goto unlock;
1245        }
1246
1247        zfcp_erp_action_to_running(erp_action);
1248
1249        /* no lock to allow for blocking operations */
1250        write_unlock(&adapter->erp_lock);
1251        read_unlock_irqrestore(&zfcp_data.config_lock, flags);
1252        retval = zfcp_erp_strategy_do_action(erp_action);
1253        read_lock_irqsave(&zfcp_data.config_lock, flags);
1254        write_lock(&adapter->erp_lock);
1255
1256        if (erp_action->status & ZFCP_STATUS_ERP_DISMISSED)
1257                retval = ZFCP_ERP_CONTINUES;
1258
1259        switch (retval) {
1260        case ZFCP_ERP_NOMEM:
1261                if (!(erp_action->status & ZFCP_STATUS_ERP_LOWMEM)) {
1262                        ++adapter->erp_low_mem_count;
1263                        erp_action->status |= ZFCP_STATUS_ERP_LOWMEM;
1264                }
1265                if (adapter->erp_total_count == adapter->erp_low_mem_count)
1266                        _zfcp_erp_adapter_reopen(adapter, 0, "erstgy1", NULL);
1267                else {
1268                        zfcp_erp_strategy_memwait(erp_action);
1269                        retval = ZFCP_ERP_CONTINUES;
1270                }
1271                goto unlock;
1272
1273        case ZFCP_ERP_CONTINUES:
1274                if (erp_action->status & ZFCP_STATUS_ERP_LOWMEM) {
1275                        --adapter->erp_low_mem_count;
1276                        erp_action->status &= ~ZFCP_STATUS_ERP_LOWMEM;
1277                }
1278                goto unlock;
1279        }
1280
1281        retval = zfcp_erp_strategy_check_target(erp_action, retval);
1282        zfcp_erp_action_dequeue(erp_action);
1283        retval = zfcp_erp_strategy_statechange(erp_action, retval);
1284        if (retval == ZFCP_ERP_EXIT)
1285                goto unlock;
1286        zfcp_erp_strategy_followup_actions(erp_action);
1287
1288 unlock:
1289        write_unlock(&adapter->erp_lock);
1290        read_unlock_irqrestore(&zfcp_data.config_lock, flags);
1291
1292        if (retval != ZFCP_ERP_CONTINUES)
1293                zfcp_erp_action_cleanup(erp_action, retval);
1294
1295        return retval;
1296}
1297
1298static int zfcp_erp_thread(void *data)
1299{
1300        struct zfcp_adapter *adapter = (struct zfcp_adapter *) data;
1301        struct list_head *next;
1302        struct zfcp_erp_action *act;
1303        unsigned long flags;
1304        int ignore;
1305
1306        daemonize("zfcperp%s", dev_name(&adapter->ccw_device->dev));
1307        /* Block all signals */
1308        siginitsetinv(&current->blocked, 0);
1309        atomic_set_mask(ZFCP_STATUS_ADAPTER_ERP_THREAD_UP, &adapter->status);
1310        wake_up(&adapter->erp_thread_wqh);
1311
1312        while (!(atomic_read(&adapter->status) &
1313                 ZFCP_STATUS_ADAPTER_ERP_THREAD_KILL)) {
1314
1315                zfcp_rec_dbf_event_thread_lock("erthrd1", adapter);
1316                ignore = down_interruptible(&adapter->erp_ready_sem);
1317                zfcp_rec_dbf_event_thread_lock("erthrd2", adapter);
1318
1319                write_lock_irqsave(&adapter->erp_lock, flags);
1320                next = adapter->erp_ready_head.next;
1321                write_unlock_irqrestore(&adapter->erp_lock, flags);
1322
1323                if (next != &adapter->erp_ready_head) {
1324                        act = list_entry(next, struct zfcp_erp_action, list);
1325
1326                        /* there is more to come after dismission, no notify */
1327                        if (zfcp_erp_strategy(act) != ZFCP_ERP_DISMISSED)
1328                                zfcp_erp_wakeup(adapter);
1329                }
1330        }
1331
1332        atomic_clear_mask(ZFCP_STATUS_ADAPTER_ERP_THREAD_UP, &adapter->status);
1333        wake_up(&adapter->erp_thread_wqh);
1334
1335        return 0;
1336}
1337
1338/**
1339 * zfcp_erp_thread_setup - Start ERP thread for adapter
1340 * @adapter: Adapter to start the ERP thread for
1341 *
1342 * Returns 0 on success or error code from kernel_thread()
1343 */
1344int zfcp_erp_thread_setup(struct zfcp_adapter *adapter)
1345{
1346        int retval;
1347
1348        atomic_clear_mask(ZFCP_STATUS_ADAPTER_ERP_THREAD_UP, &adapter->status);
1349        retval = kernel_thread(zfcp_erp_thread, adapter, SIGCHLD);
1350        if (retval < 0) {
1351                dev_err(&adapter->ccw_device->dev,
1352                        "Creating an ERP thread for the FCP device failed.\n");
1353                return retval;
1354        }
1355        wait_event(adapter->erp_thread_wqh,
1356                   atomic_read(&adapter->status) &
1357                        ZFCP_STATUS_ADAPTER_ERP_THREAD_UP);
1358        return 0;
1359}
1360
1361/**
1362 * zfcp_erp_thread_kill - Stop ERP thread.
1363 * @adapter: Adapter where the ERP thread should be stopped.
1364 *
1365 * The caller of this routine ensures that the specified adapter has
1366 * been shut down and that this operation has been completed. Thus,
1367 * there are no pending erp_actions which would need to be handled
1368 * here.
1369 */
1370void zfcp_erp_thread_kill(struct zfcp_adapter *adapter)
1371{
1372        atomic_set_mask(ZFCP_STATUS_ADAPTER_ERP_THREAD_KILL, &adapter->status);
1373        up(&adapter->erp_ready_sem);
1374        zfcp_rec_dbf_event_thread_lock("erthrk1", adapter);
1375
1376        wait_event(adapter->erp_thread_wqh,
1377                   !(atomic_read(&adapter->status) &
1378                                ZFCP_STATUS_ADAPTER_ERP_THREAD_UP));
1379
1380        atomic_clear_mask(ZFCP_STATUS_ADAPTER_ERP_THREAD_KILL,
1381                          &adapter->status);
1382}
1383
1384/**
1385 * zfcp_erp_adapter_failed - Set adapter status to failed.
1386 * @adapter: Failed adapter.
1387 * @id: Event id for debug trace.
1388 * @ref: Reference for debug trace.
1389 */
1390void zfcp_erp_adapter_failed(struct zfcp_adapter *adapter, char *id, void *ref)
1391{
1392        zfcp_erp_modify_adapter_status(adapter, id, ref,
1393                                       ZFCP_STATUS_COMMON_ERP_FAILED, ZFCP_SET);
1394}
1395
1396/**
1397 * zfcp_erp_port_failed - Set port status to failed.
1398 * @port: Failed port.
1399 * @id: Event id for debug trace.
1400 * @ref: Reference for debug trace.
1401 */
1402void zfcp_erp_port_failed(struct zfcp_port *port, char *id, void *ref)
1403{
1404        zfcp_erp_modify_port_status(port, id, ref,
1405                                    ZFCP_STATUS_COMMON_ERP_FAILED, ZFCP_SET);
1406}
1407
1408/**
1409 * zfcp_erp_unit_failed - Set unit status to failed.
1410 * @unit: Failed unit.
1411 * @id: Event id for debug trace.
1412 * @ref: Reference for debug trace.
1413 */
1414void zfcp_erp_unit_failed(struct zfcp_unit *unit, char *id, void *ref)
1415{
1416        zfcp_erp_modify_unit_status(unit, id, ref,
1417                                    ZFCP_STATUS_COMMON_ERP_FAILED, ZFCP_SET);
1418}
1419
1420/**
1421 * zfcp_erp_wait - wait for completion of error recovery on an adapter
1422 * @adapter: adapter for which to wait for completion of its error recovery
1423 */
1424void zfcp_erp_wait(struct zfcp_adapter *adapter)
1425{
1426        wait_event(adapter->erp_done_wqh,
1427                   !(atomic_read(&adapter->status) &
1428                        ZFCP_STATUS_ADAPTER_ERP_PENDING));
1429}
1430
1431/**
1432 * zfcp_erp_modify_adapter_status - change adapter status bits
1433 * @adapter: adapter to change the status
1434 * @id: id for the debug trace
1435 * @ref: reference for the debug trace
1436 * @mask: status bits to change
1437 * @set_or_clear: ZFCP_SET or ZFCP_CLEAR
1438 *
1439 * Changes in common status bits are propagated to attached ports and units.
1440 */
1441void zfcp_erp_modify_adapter_status(struct zfcp_adapter *adapter, char *id,
1442                                    void *ref, u32 mask, int set_or_clear)
1443{
1444        struct zfcp_port *port;
1445        u32 common_mask = mask & ZFCP_COMMON_FLAGS;
1446
1447        if (set_or_clear == ZFCP_SET) {
1448                if (status_change_set(mask, &adapter->status))
1449                        zfcp_rec_dbf_event_adapter(id, ref, adapter);
1450                atomic_set_mask(mask, &adapter->status);
1451        } else {
1452                if (status_change_clear(mask, &adapter->status))
1453                        zfcp_rec_dbf_event_adapter(id, ref, adapter);
1454                atomic_clear_mask(mask, &adapter->status);
1455                if (mask & ZFCP_STATUS_COMMON_ERP_FAILED)
1456                        atomic_set(&adapter->erp_counter, 0);
1457        }
1458
1459        if (common_mask)
1460                list_for_each_entry(port, &adapter->port_list_head, list)
1461                        zfcp_erp_modify_port_status(port, id, ref, common_mask,
1462                                                    set_or_clear);
1463}
1464
1465/**
1466 * zfcp_erp_modify_port_status - change port status bits
1467 * @port: port to change the status bits
1468 * @id: id for the debug trace
1469 * @ref: reference for the debug trace
1470 * @mask: status bits to change
1471 * @set_or_clear: ZFCP_SET or ZFCP_CLEAR
1472 *
1473 * Changes in common status bits are propagated to attached units.
1474 */
1475void zfcp_erp_modify_port_status(struct zfcp_port *port, char *id, void *ref,
1476                                 u32 mask, int set_or_clear)
1477{
1478        struct zfcp_unit *unit;
1479        u32 common_mask = mask & ZFCP_COMMON_FLAGS;
1480
1481        if (set_or_clear == ZFCP_SET) {
1482                if (status_change_set(mask, &port->status))
1483                        zfcp_rec_dbf_event_port(id, ref, port);
1484                atomic_set_mask(mask, &port->status);
1485        } else {
1486                if (status_change_clear(mask, &port->status))
1487                        zfcp_rec_dbf_event_port(id, ref, port);
1488                atomic_clear_mask(mask, &port->status);
1489                if (mask & ZFCP_STATUS_COMMON_ERP_FAILED)
1490                        atomic_set(&port->erp_counter, 0);
1491        }
1492
1493        if (common_mask)
1494                list_for_each_entry(unit, &port->unit_list_head, list)
1495                        zfcp_erp_modify_unit_status(unit, id, ref, common_mask,
1496                                                    set_or_clear);
1497}
1498
1499/**
1500 * zfcp_erp_modify_unit_status - change unit status bits
1501 * @unit: unit to change the status bits
1502 * @id: id for the debug trace
1503 * @ref: reference for the debug trace
1504 * @mask: status bits to change
1505 * @set_or_clear: ZFCP_SET or ZFCP_CLEAR
1506 */
1507void zfcp_erp_modify_unit_status(struct zfcp_unit *unit, char *id, void *ref,
1508                                 u32 mask, int set_or_clear)
1509{
1510        if (set_or_clear == ZFCP_SET) {
1511                if (status_change_set(mask, &unit->status))
1512                        zfcp_rec_dbf_event_unit(id, ref, unit);
1513                atomic_set_mask(mask, &unit->status);
1514        } else {
1515                if (status_change_clear(mask, &unit->status))
1516                        zfcp_rec_dbf_event_unit(id, ref, unit);
1517                atomic_clear_mask(mask, &unit->status);
1518                if (mask & ZFCP_STATUS_COMMON_ERP_FAILED) {
1519                        atomic_set(&unit->erp_counter, 0);
1520                }
1521        }
1522}
1523
1524/**
1525 * zfcp_erp_port_boxed - Mark port as "boxed" and start ERP
1526 * @port: The "boxed" port.
1527 * @id: The debug trace id.
1528 * @id: Reference for the debug trace.
1529 */
1530void zfcp_erp_port_boxed(struct zfcp_port *port, char *id, void *ref)
1531{
1532        unsigned long flags;
1533
1534        read_lock_irqsave(&zfcp_data.config_lock, flags);
1535        zfcp_erp_modify_port_status(port, id, ref,
1536                                    ZFCP_STATUS_COMMON_ACCESS_BOXED, ZFCP_SET);
1537        read_unlock_irqrestore(&zfcp_data.config_lock, flags);
1538        zfcp_erp_port_reopen(port, ZFCP_STATUS_COMMON_ERP_FAILED, id, ref);
1539}
1540
1541/**
1542 * zfcp_erp_unit_boxed - Mark unit as "boxed" and start ERP
1543 * @port: The "boxed" unit.
1544 * @id: The debug trace id.
1545 * @id: Reference for the debug trace.
1546 */
1547void zfcp_erp_unit_boxed(struct zfcp_unit *unit, char *id, void *ref)
1548{
1549        zfcp_erp_modify_unit_status(unit, id, ref,
1550                                    ZFCP_STATUS_COMMON_ACCESS_BOXED, ZFCP_SET);
1551        zfcp_erp_unit_reopen(unit, ZFCP_STATUS_COMMON_ERP_FAILED, id, ref);
1552}
1553
1554/**
1555 * zfcp_erp_port_access_denied - Adapter denied access to port.
1556 * @port: port where access has been denied
1557 * @id: id for debug trace
1558 * @ref: reference for debug trace
1559 *
1560 * Since the adapter has denied access, stop using the port and the
1561 * attached units.
1562 */
1563void zfcp_erp_port_access_denied(struct zfcp_port *port, char *id, void *ref)
1564{
1565        unsigned long flags;
1566
1567        read_lock_irqsave(&zfcp_data.config_lock, flags);
1568        zfcp_erp_modify_port_status(port, id, ref,
1569                                    ZFCP_STATUS_COMMON_ERP_FAILED |
1570                                    ZFCP_STATUS_COMMON_ACCESS_DENIED, ZFCP_SET);
1571        read_unlock_irqrestore(&zfcp_data.config_lock, flags);
1572}
1573
1574/**
1575 * zfcp_erp_unit_access_denied - Adapter denied access to unit.
1576 * @unit: unit where access has been denied
1577 * @id: id for debug trace
1578 * @ref: reference for debug trace
1579 *
1580 * Since the adapter has denied access, stop using the unit.
1581 */
1582void zfcp_erp_unit_access_denied(struct zfcp_unit *unit, char *id, void *ref)
1583{
1584        zfcp_erp_modify_unit_status(unit, id, ref,
1585                                    ZFCP_STATUS_COMMON_ERP_FAILED |
1586                                    ZFCP_STATUS_COMMON_ACCESS_DENIED, ZFCP_SET);
1587}
1588
1589static void zfcp_erp_unit_access_changed(struct zfcp_unit *unit, char *id,
1590                                         void *ref)
1591{
1592        int status = atomic_read(&unit->status);
1593        if (!(status & (ZFCP_STATUS_COMMON_ACCESS_DENIED |
1594                        ZFCP_STATUS_COMMON_ACCESS_BOXED)))
1595                return;
1596
1597        zfcp_erp_unit_reopen(unit, ZFCP_STATUS_COMMON_ERP_FAILED, id, ref);
1598}
1599
1600static void zfcp_erp_port_access_changed(struct zfcp_port *port, char *id,
1601                                         void *ref)
1602{
1603        struct zfcp_unit *unit;
1604        int status = atomic_read(&port->status);
1605
1606        if (!(status & (ZFCP_STATUS_COMMON_ACCESS_DENIED |
1607                        ZFCP_STATUS_COMMON_ACCESS_BOXED))) {
1608                list_for_each_entry(unit, &port->unit_list_head, list)
1609                                    zfcp_erp_unit_access_changed(unit, id, ref);
1610                return;
1611        }
1612
1613        zfcp_erp_port_reopen(port, ZFCP_STATUS_COMMON_ERP_FAILED, id, ref);
1614}
1615
1616/**
1617 * zfcp_erp_adapter_access_changed - Process change in adapter ACT
1618 * @adapter: Adapter where the Access Control Table (ACT) changed
1619 * @id: Id for debug trace
1620 * @ref: Reference for debug trace
1621 */
1622void zfcp_erp_adapter_access_changed(struct zfcp_adapter *adapter, char *id,
1623                                     void *ref)
1624{
1625        struct zfcp_port *port;
1626        unsigned long flags;
1627
1628        if (adapter->connection_features & FSF_FEATURE_NPIV_MODE)
1629                return;
1630
1631        read_lock_irqsave(&zfcp_data.config_lock, flags);
1632        list_for_each_entry(port, &adapter->port_list_head, list)
1633                zfcp_erp_port_access_changed(port, id, ref);
1634        read_unlock_irqrestore(&zfcp_data.config_lock, flags);
1635}
1636