Merge branch 'fix/pcm-jiffies-check' into fix/asoc
[linux-2.6] / drivers / s390 / scsi / zfcp_erp.c
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
16 enum 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
24 enum 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
35 enum 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
42 enum zfcp_erp_act_state {
43         ZFCP_ERP_ACTION_RUNNING = 1,
44         ZFCP_ERP_ACTION_READY   = 2,
45 };
46
47 enum 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
56 static 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
63 static 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
73 static 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
83 static 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
90 static 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
96 static 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
107 static 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
118 static 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
158 static 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
209 static 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
239 static 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  */
261 void 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  */
280 void 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  */
294 void 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  */
308 void 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
315 static 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
322 static 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  */
341 void 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
354 static 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  */
377 int 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
392 static 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
399 static 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  */
419 void 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
433 static int status_change_set(unsigned long mask, atomic_t *status)
434 {
435         return (atomic_read(status) ^ mask) & mask;
436 }
437
438 static int status_change_clear(unsigned long mask, atomic_t *status)
439 {
440         return atomic_read(status) & mask;
441 }
442
443 static 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
450 static 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
457 static 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
464 static 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
470 static 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  */
501 void 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  */
518 void 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
524 static void zfcp_erp_memwait_handler(unsigned long data)
525 {
526         zfcp_erp_notify((struct zfcp_erp_action *)data, 0);
527 }
528
529 static 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
538 static 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
547 static 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
556 static 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
594 static 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
610 static 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
619 static 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
629 static 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
675 static 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
699 static 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
714 static 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         /* all ports and units are closed */
723         zfcp_erp_modify_adapter_status(adapter, "erascl1", NULL,
724                                        ZFCP_STATUS_COMMON_OPEN, ZFCP_CLEAR);
725
726         atomic_clear_mask(ZFCP_STATUS_ADAPTER_XCONFIG_OK |
727                           ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED, &adapter->status);
728 }
729
730 static int zfcp_erp_adapter_strategy_open(struct zfcp_erp_action *act)
731 {
732         struct zfcp_adapter *adapter = act->adapter;
733
734         if (zfcp_erp_adapter_strategy_open_qdio(act)) {
735                 atomic_clear_mask(ZFCP_STATUS_ADAPTER_XCONFIG_OK |
736                                   ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED,
737                                   &adapter->status);
738                 return ZFCP_ERP_FAILED;
739         }
740
741         if (zfcp_erp_adapter_strategy_open_fsf(act)) {
742                 zfcp_erp_adapter_strategy_close(act);
743                 return ZFCP_ERP_FAILED;
744         }
745
746         atomic_set_mask(ZFCP_STATUS_COMMON_OPEN, &adapter->status);
747
748         return ZFCP_ERP_SUCCEEDED;
749 }
750
751 static int zfcp_erp_adapter_strategy(struct zfcp_erp_action *act)
752 {
753         struct zfcp_adapter *adapter = act->adapter;
754
755         if (atomic_read(&adapter->status) & ZFCP_STATUS_COMMON_OPEN) {
756                 zfcp_erp_adapter_strategy_close(act);
757                 if (act->status & ZFCP_STATUS_ERP_CLOSE_ONLY)
758                         return ZFCP_ERP_EXIT;
759         }
760
761         if (zfcp_erp_adapter_strategy_open(act)) {
762                 ssleep(8);
763                 return ZFCP_ERP_FAILED;
764         }
765
766         return ZFCP_ERP_SUCCEEDED;
767 }
768
769 static int zfcp_erp_port_forced_strategy_close(struct zfcp_erp_action *act)
770 {
771         int retval;
772
773         retval = zfcp_fsf_close_physical_port(act);
774         if (retval == -ENOMEM)
775                 return ZFCP_ERP_NOMEM;
776         act->step = ZFCP_ERP_STEP_PHYS_PORT_CLOSING;
777         if (retval)
778                 return ZFCP_ERP_FAILED;
779
780         return ZFCP_ERP_CONTINUES;
781 }
782
783 static void zfcp_erp_port_strategy_clearstati(struct zfcp_port *port)
784 {
785         atomic_clear_mask(ZFCP_STATUS_COMMON_ACCESS_DENIED, &port->status);
786 }
787
788 static int zfcp_erp_port_forced_strategy(struct zfcp_erp_action *erp_action)
789 {
790         struct zfcp_port *port = erp_action->port;
791         int status = atomic_read(&port->status);
792
793         switch (erp_action->step) {
794         case ZFCP_ERP_STEP_UNINITIALIZED:
795                 zfcp_erp_port_strategy_clearstati(port);
796                 if ((status & ZFCP_STATUS_PORT_PHYS_OPEN) &&
797                     (status & ZFCP_STATUS_COMMON_OPEN))
798                         return zfcp_erp_port_forced_strategy_close(erp_action);
799                 else
800                         return ZFCP_ERP_FAILED;
801
802         case ZFCP_ERP_STEP_PHYS_PORT_CLOSING:
803                 if (status & ZFCP_STATUS_PORT_PHYS_OPEN)
804                         return ZFCP_ERP_SUCCEEDED;
805         }
806         return ZFCP_ERP_FAILED;
807 }
808
809 static int zfcp_erp_port_strategy_close(struct zfcp_erp_action *erp_action)
810 {
811         int retval;
812
813         retval = zfcp_fsf_close_port(erp_action);
814         if (retval == -ENOMEM)
815                 return ZFCP_ERP_NOMEM;
816         erp_action->step = ZFCP_ERP_STEP_PORT_CLOSING;
817         if (retval)
818                 return ZFCP_ERP_FAILED;
819         return ZFCP_ERP_CONTINUES;
820 }
821
822 static int zfcp_erp_port_strategy_open_port(struct zfcp_erp_action *erp_action)
823 {
824         int retval;
825
826         retval = zfcp_fsf_open_port(erp_action);
827         if (retval == -ENOMEM)
828                 return ZFCP_ERP_NOMEM;
829         erp_action->step = ZFCP_ERP_STEP_PORT_OPENING;
830         if (retval)
831                 return ZFCP_ERP_FAILED;
832         return ZFCP_ERP_CONTINUES;
833 }
834
835 static int zfcp_erp_open_ptp_port(struct zfcp_erp_action *act)
836 {
837         struct zfcp_adapter *adapter = act->adapter;
838         struct zfcp_port *port = act->port;
839
840         if (port->wwpn != adapter->peer_wwpn) {
841                 zfcp_erp_port_failed(port, "eroptp1", NULL);
842                 return ZFCP_ERP_FAILED;
843         }
844         port->d_id = adapter->peer_d_id;
845         return zfcp_erp_port_strategy_open_port(act);
846 }
847
848 void zfcp_erp_port_strategy_open_lookup(struct work_struct *work)
849 {
850         int retval;
851         struct zfcp_port *port = container_of(work, struct zfcp_port,
852                                               gid_pn_work);
853
854         retval = zfcp_fc_ns_gid_pn(&port->erp_action);
855         if (retval == -ENOMEM)
856                 zfcp_erp_notify(&port->erp_action, ZFCP_ERP_NOMEM);
857         port->erp_action.step = ZFCP_ERP_STEP_NAMESERVER_LOOKUP;
858         if (retval)
859                 zfcp_erp_notify(&port->erp_action, ZFCP_ERP_FAILED);
860         zfcp_port_put(port);
861 }
862
863 static int zfcp_erp_port_strategy_open_common(struct zfcp_erp_action *act)
864 {
865         struct zfcp_adapter *adapter = act->adapter;
866         struct zfcp_port *port = act->port;
867         int p_status = atomic_read(&port->status);
868
869         switch (act->step) {
870         case ZFCP_ERP_STEP_UNINITIALIZED:
871         case ZFCP_ERP_STEP_PHYS_PORT_CLOSING:
872         case ZFCP_ERP_STEP_PORT_CLOSING:
873                 if (fc_host_port_type(adapter->scsi_host) == FC_PORTTYPE_PTP)
874                         return zfcp_erp_open_ptp_port(act);
875                 if (!port->d_id) {
876                         zfcp_port_get(port);
877                         if (!queue_work(zfcp_data.work_queue,
878                                         &port->gid_pn_work))
879                                 zfcp_port_put(port);
880                         return ZFCP_ERP_CONTINUES;
881                 }
882         case ZFCP_ERP_STEP_NAMESERVER_LOOKUP:
883                 if (!port->d_id)
884                         return ZFCP_ERP_FAILED;
885                 return zfcp_erp_port_strategy_open_port(act);
886
887         case ZFCP_ERP_STEP_PORT_OPENING:
888                 /* D_ID might have changed during open */
889                 if (p_status & ZFCP_STATUS_COMMON_OPEN) {
890                         if (port->d_id)
891                                 return ZFCP_ERP_SUCCEEDED;
892                         else {
893                                 act->step = ZFCP_ERP_STEP_PORT_CLOSING;
894                                 return ZFCP_ERP_CONTINUES;
895                         }
896                 /* fall through otherwise */
897                 }
898         }
899         return ZFCP_ERP_FAILED;
900 }
901
902 static int zfcp_erp_port_strategy(struct zfcp_erp_action *erp_action)
903 {
904         struct zfcp_port *port = erp_action->port;
905
906         if (atomic_read(&port->status) & ZFCP_STATUS_COMMON_NOESC)
907                 goto close_init_done;
908
909         switch (erp_action->step) {
910         case ZFCP_ERP_STEP_UNINITIALIZED:
911                 zfcp_erp_port_strategy_clearstati(port);
912                 if (atomic_read(&port->status) & ZFCP_STATUS_COMMON_OPEN)
913                         return zfcp_erp_port_strategy_close(erp_action);
914                 break;
915
916         case ZFCP_ERP_STEP_PORT_CLOSING:
917                 if (atomic_read(&port->status) & ZFCP_STATUS_COMMON_OPEN)
918                         return ZFCP_ERP_FAILED;
919                 break;
920         }
921
922 close_init_done:
923         if (erp_action->status & ZFCP_STATUS_ERP_CLOSE_ONLY)
924                 return ZFCP_ERP_EXIT;
925
926         return zfcp_erp_port_strategy_open_common(erp_action);
927 }
928
929 static void zfcp_erp_unit_strategy_clearstati(struct zfcp_unit *unit)
930 {
931         atomic_clear_mask(ZFCP_STATUS_COMMON_ACCESS_DENIED |
932                           ZFCP_STATUS_UNIT_SHARED |
933                           ZFCP_STATUS_UNIT_READONLY,
934                           &unit->status);
935 }
936
937 static int zfcp_erp_unit_strategy_close(struct zfcp_erp_action *erp_action)
938 {
939         int retval = zfcp_fsf_close_unit(erp_action);
940         if (retval == -ENOMEM)
941                 return ZFCP_ERP_NOMEM;
942         erp_action->step = ZFCP_ERP_STEP_UNIT_CLOSING;
943         if (retval)
944                 return ZFCP_ERP_FAILED;
945         return ZFCP_ERP_CONTINUES;
946 }
947
948 static int zfcp_erp_unit_strategy_open(struct zfcp_erp_action *erp_action)
949 {
950         int retval = zfcp_fsf_open_unit(erp_action);
951         if (retval == -ENOMEM)
952                 return ZFCP_ERP_NOMEM;
953         erp_action->step = ZFCP_ERP_STEP_UNIT_OPENING;
954         if (retval)
955                 return  ZFCP_ERP_FAILED;
956         return ZFCP_ERP_CONTINUES;
957 }
958
959 static int zfcp_erp_unit_strategy(struct zfcp_erp_action *erp_action)
960 {
961         struct zfcp_unit *unit = erp_action->unit;
962
963         switch (erp_action->step) {
964         case ZFCP_ERP_STEP_UNINITIALIZED:
965                 zfcp_erp_unit_strategy_clearstati(unit);
966                 if (atomic_read(&unit->status) & ZFCP_STATUS_COMMON_OPEN)
967                         return zfcp_erp_unit_strategy_close(erp_action);
968                 /* already closed, fall through */
969         case ZFCP_ERP_STEP_UNIT_CLOSING:
970                 if (atomic_read(&unit->status) & ZFCP_STATUS_COMMON_OPEN)
971                         return ZFCP_ERP_FAILED;
972                 if (erp_action->status & ZFCP_STATUS_ERP_CLOSE_ONLY)
973                         return ZFCP_ERP_EXIT;
974                 return zfcp_erp_unit_strategy_open(erp_action);
975
976         case ZFCP_ERP_STEP_UNIT_OPENING:
977                 if (atomic_read(&unit->status) & ZFCP_STATUS_COMMON_OPEN)
978                         return ZFCP_ERP_SUCCEEDED;
979         }
980         return ZFCP_ERP_FAILED;
981 }
982
983 static int zfcp_erp_strategy_check_unit(struct zfcp_unit *unit, int result)
984 {
985         switch (result) {
986         case ZFCP_ERP_SUCCEEDED :
987                 atomic_set(&unit->erp_counter, 0);
988                 zfcp_erp_unit_unblock(unit);
989                 break;
990         case ZFCP_ERP_FAILED :
991                 atomic_inc(&unit->erp_counter);
992                 if (atomic_read(&unit->erp_counter) > ZFCP_MAX_ERPS) {
993                         dev_err(&unit->port->adapter->ccw_device->dev,
994                                 "ERP failed for unit 0x%016Lx on "
995                                 "port 0x%016Lx\n",
996                                 (unsigned long long)unit->fcp_lun,
997                                 (unsigned long long)unit->port->wwpn);
998                         zfcp_erp_unit_failed(unit, "erusck1", NULL);
999                 }
1000                 break;
1001         }
1002
1003         if (atomic_read(&unit->status) & ZFCP_STATUS_COMMON_ERP_FAILED) {
1004                 zfcp_erp_unit_block(unit, 0);
1005                 result = ZFCP_ERP_EXIT;
1006         }
1007         return result;
1008 }
1009
1010 static int zfcp_erp_strategy_check_port(struct zfcp_port *port, int result)
1011 {
1012         switch (result) {
1013         case ZFCP_ERP_SUCCEEDED :
1014                 atomic_set(&port->erp_counter, 0);
1015                 zfcp_erp_port_unblock(port);
1016                 break;
1017
1018         case ZFCP_ERP_FAILED :
1019                 if (atomic_read(&port->status) & ZFCP_STATUS_COMMON_NOESC) {
1020                         zfcp_erp_port_block(port, 0);
1021                         result = ZFCP_ERP_EXIT;
1022                 }
1023                 atomic_inc(&port->erp_counter);
1024                 if (atomic_read(&port->erp_counter) > ZFCP_MAX_ERPS) {
1025                         dev_err(&port->adapter->ccw_device->dev,
1026                                 "ERP failed for remote port 0x%016Lx\n",
1027                                 (unsigned long long)port->wwpn);
1028                         zfcp_erp_port_failed(port, "erpsck1", NULL);
1029                 }
1030                 break;
1031         }
1032
1033         if (atomic_read(&port->status) & ZFCP_STATUS_COMMON_ERP_FAILED) {
1034                 zfcp_erp_port_block(port, 0);
1035                 result = ZFCP_ERP_EXIT;
1036         }
1037         return result;
1038 }
1039
1040 static int zfcp_erp_strategy_check_adapter(struct zfcp_adapter *adapter,
1041                                            int result)
1042 {
1043         switch (result) {
1044         case ZFCP_ERP_SUCCEEDED :
1045                 atomic_set(&adapter->erp_counter, 0);
1046                 zfcp_erp_adapter_unblock(adapter);
1047                 break;
1048
1049         case ZFCP_ERP_FAILED :
1050                 atomic_inc(&adapter->erp_counter);
1051                 if (atomic_read(&adapter->erp_counter) > ZFCP_MAX_ERPS) {
1052                         dev_err(&adapter->ccw_device->dev,
1053                                 "ERP cannot recover an error "
1054                                 "on the FCP device\n");
1055                         zfcp_erp_adapter_failed(adapter, "erasck1", NULL);
1056                 }
1057                 break;
1058         }
1059
1060         if (atomic_read(&adapter->status) & ZFCP_STATUS_COMMON_ERP_FAILED) {
1061                 zfcp_erp_adapter_block(adapter, 0);
1062                 result = ZFCP_ERP_EXIT;
1063         }
1064         return result;
1065 }
1066
1067 static int zfcp_erp_strategy_check_target(struct zfcp_erp_action *erp_action,
1068                                           int result)
1069 {
1070         struct zfcp_adapter *adapter = erp_action->adapter;
1071         struct zfcp_port *port = erp_action->port;
1072         struct zfcp_unit *unit = erp_action->unit;
1073
1074         switch (erp_action->action) {
1075
1076         case ZFCP_ERP_ACTION_REOPEN_UNIT:
1077                 result = zfcp_erp_strategy_check_unit(unit, result);
1078                 break;
1079
1080         case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
1081         case ZFCP_ERP_ACTION_REOPEN_PORT:
1082                 result = zfcp_erp_strategy_check_port(port, result);
1083                 break;
1084
1085         case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
1086                 result = zfcp_erp_strategy_check_adapter(adapter, result);
1087                 break;
1088         }
1089         return result;
1090 }
1091
1092 static int zfcp_erp_strat_change_det(atomic_t *target_status, u32 erp_status)
1093 {
1094         int status = atomic_read(target_status);
1095
1096         if ((status & ZFCP_STATUS_COMMON_RUNNING) &&
1097             (erp_status & ZFCP_STATUS_ERP_CLOSE_ONLY))
1098                 return 1; /* take it online */
1099
1100         if (!(status & ZFCP_STATUS_COMMON_RUNNING) &&
1101             !(erp_status & ZFCP_STATUS_ERP_CLOSE_ONLY))
1102                 return 1; /* take it offline */
1103
1104         return 0;
1105 }
1106
1107 static int zfcp_erp_strategy_statechange(struct zfcp_erp_action *act, int ret)
1108 {
1109         int action = act->action;
1110         struct zfcp_adapter *adapter = act->adapter;
1111         struct zfcp_port *port = act->port;
1112         struct zfcp_unit *unit = act->unit;
1113         u32 erp_status = act->status;
1114
1115         switch (action) {
1116         case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
1117                 if (zfcp_erp_strat_change_det(&adapter->status, erp_status)) {
1118                         _zfcp_erp_adapter_reopen(adapter,
1119                                                  ZFCP_STATUS_COMMON_ERP_FAILED,
1120                                                  "ersscg1", NULL);
1121                         return ZFCP_ERP_EXIT;
1122                 }
1123                 break;
1124
1125         case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
1126         case ZFCP_ERP_ACTION_REOPEN_PORT:
1127                 if (zfcp_erp_strat_change_det(&port->status, erp_status)) {
1128                         _zfcp_erp_port_reopen(port,
1129                                               ZFCP_STATUS_COMMON_ERP_FAILED,
1130                                               "ersscg2", NULL);
1131                         return ZFCP_ERP_EXIT;
1132                 }
1133                 break;
1134
1135         case ZFCP_ERP_ACTION_REOPEN_UNIT:
1136                 if (zfcp_erp_strat_change_det(&unit->status, erp_status)) {
1137                         _zfcp_erp_unit_reopen(unit,
1138                                               ZFCP_STATUS_COMMON_ERP_FAILED,
1139                                               "ersscg3", NULL);
1140                         return ZFCP_ERP_EXIT;
1141                 }
1142                 break;
1143         }
1144         return ret;
1145 }
1146
1147 static void zfcp_erp_action_dequeue(struct zfcp_erp_action *erp_action)
1148 {
1149         struct zfcp_adapter *adapter = erp_action->adapter;
1150
1151         adapter->erp_total_count--;
1152         if (erp_action->status & ZFCP_STATUS_ERP_LOWMEM) {
1153                 adapter->erp_low_mem_count--;
1154                 erp_action->status &= ~ZFCP_STATUS_ERP_LOWMEM;
1155         }
1156
1157         list_del(&erp_action->list);
1158         zfcp_rec_dbf_event_action("eractd1", erp_action);
1159
1160         switch (erp_action->action) {
1161         case ZFCP_ERP_ACTION_REOPEN_UNIT:
1162                 atomic_clear_mask(ZFCP_STATUS_COMMON_ERP_INUSE,
1163                                   &erp_action->unit->status);
1164                 break;
1165
1166         case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
1167         case ZFCP_ERP_ACTION_REOPEN_PORT:
1168                 atomic_clear_mask(ZFCP_STATUS_COMMON_ERP_INUSE,
1169                                   &erp_action->port->status);
1170                 break;
1171
1172         case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
1173                 atomic_clear_mask(ZFCP_STATUS_COMMON_ERP_INUSE,
1174                                   &erp_action->adapter->status);
1175                 break;
1176         }
1177 }
1178
1179 struct zfcp_erp_add_work {
1180         struct zfcp_unit  *unit;
1181         struct work_struct work;
1182 };
1183
1184 static void zfcp_erp_scsi_scan(struct work_struct *work)
1185 {
1186         struct zfcp_erp_add_work *p =
1187                 container_of(work, struct zfcp_erp_add_work, work);
1188         struct zfcp_unit *unit = p->unit;
1189         struct fc_rport *rport = unit->port->rport;
1190
1191         if (rport && rport->port_state == FC_PORTSTATE_ONLINE)
1192                 scsi_scan_target(&rport->dev, 0, rport->scsi_target_id,
1193                          scsilun_to_int((struct scsi_lun *)&unit->fcp_lun), 0);
1194         atomic_clear_mask(ZFCP_STATUS_UNIT_SCSI_WORK_PENDING, &unit->status);
1195         zfcp_unit_put(unit);
1196         wake_up(&unit->port->adapter->erp_done_wqh);
1197         kfree(p);
1198 }
1199
1200 static void zfcp_erp_schedule_work(struct zfcp_unit *unit)
1201 {
1202         struct zfcp_erp_add_work *p;
1203
1204         p = kzalloc(sizeof(*p), GFP_KERNEL);
1205         if (!p) {
1206                 dev_err(&unit->port->adapter->ccw_device->dev,
1207                         "Registering unit 0x%016Lx on port 0x%016Lx failed\n",
1208                         (unsigned long long)unit->fcp_lun,
1209                         (unsigned long long)unit->port->wwpn);
1210                 return;
1211         }
1212
1213         zfcp_unit_get(unit);
1214         atomic_set_mask(ZFCP_STATUS_UNIT_SCSI_WORK_PENDING, &unit->status);
1215         INIT_WORK(&p->work, zfcp_erp_scsi_scan);
1216         p->unit = unit;
1217         if (!queue_work(zfcp_data.work_queue, &p->work))
1218                 zfcp_unit_put(unit);
1219 }
1220
1221 static void zfcp_erp_action_cleanup(struct zfcp_erp_action *act, int result)
1222 {
1223         struct zfcp_adapter *adapter = act->adapter;
1224         struct zfcp_port *port = act->port;
1225         struct zfcp_unit *unit = act->unit;
1226
1227         switch (act->action) {
1228         case ZFCP_ERP_ACTION_REOPEN_UNIT:
1229                 flush_work(&port->rport_work);
1230                 if ((result == ZFCP_ERP_SUCCEEDED) && !unit->device) {
1231                         if (!(atomic_read(&unit->status) &
1232                               ZFCP_STATUS_UNIT_SCSI_WORK_PENDING))
1233                                 zfcp_erp_schedule_work(unit);
1234                 }
1235                 zfcp_unit_put(unit);
1236                 break;
1237
1238         case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
1239         case ZFCP_ERP_ACTION_REOPEN_PORT:
1240                 if (result == ZFCP_ERP_SUCCEEDED)
1241                         zfcp_scsi_schedule_rport_register(port);
1242                 zfcp_port_put(port);
1243                 break;
1244
1245         case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
1246                 if (result == ZFCP_ERP_SUCCEEDED) {
1247                         register_service_level(&adapter->service_level);
1248                         schedule_work(&adapter->scan_work);
1249                 } else
1250                         unregister_service_level(&adapter->service_level);
1251                 zfcp_adapter_put(adapter);
1252                 break;
1253         }
1254 }
1255
1256 static int zfcp_erp_strategy_do_action(struct zfcp_erp_action *erp_action)
1257 {
1258         switch (erp_action->action) {
1259         case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
1260                 return zfcp_erp_adapter_strategy(erp_action);
1261         case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
1262                 return zfcp_erp_port_forced_strategy(erp_action);
1263         case ZFCP_ERP_ACTION_REOPEN_PORT:
1264                 return zfcp_erp_port_strategy(erp_action);
1265         case ZFCP_ERP_ACTION_REOPEN_UNIT:
1266                 return zfcp_erp_unit_strategy(erp_action);
1267         }
1268         return ZFCP_ERP_FAILED;
1269 }
1270
1271 static int zfcp_erp_strategy(struct zfcp_erp_action *erp_action)
1272 {
1273         int retval;
1274         struct zfcp_adapter *adapter = erp_action->adapter;
1275         unsigned long flags;
1276
1277         read_lock_irqsave(&zfcp_data.config_lock, flags);
1278         write_lock(&adapter->erp_lock);
1279
1280         zfcp_erp_strategy_check_fsfreq(erp_action);
1281
1282         if (erp_action->status & ZFCP_STATUS_ERP_DISMISSED) {
1283                 zfcp_erp_action_dequeue(erp_action);
1284                 retval = ZFCP_ERP_DISMISSED;
1285                 goto unlock;
1286         }
1287
1288         zfcp_erp_action_to_running(erp_action);
1289
1290         /* no lock to allow for blocking operations */
1291         write_unlock(&adapter->erp_lock);
1292         read_unlock_irqrestore(&zfcp_data.config_lock, flags);
1293         retval = zfcp_erp_strategy_do_action(erp_action);
1294         read_lock_irqsave(&zfcp_data.config_lock, flags);
1295         write_lock(&adapter->erp_lock);
1296
1297         if (erp_action->status & ZFCP_STATUS_ERP_DISMISSED)
1298                 retval = ZFCP_ERP_CONTINUES;
1299
1300         switch (retval) {
1301         case ZFCP_ERP_NOMEM:
1302                 if (!(erp_action->status & ZFCP_STATUS_ERP_LOWMEM)) {
1303                         ++adapter->erp_low_mem_count;
1304                         erp_action->status |= ZFCP_STATUS_ERP_LOWMEM;
1305                 }
1306                 if (adapter->erp_total_count == adapter->erp_low_mem_count)
1307                         _zfcp_erp_adapter_reopen(adapter, 0, "erstgy1", NULL);
1308                 else {
1309                         zfcp_erp_strategy_memwait(erp_action);
1310                         retval = ZFCP_ERP_CONTINUES;
1311                 }
1312                 goto unlock;
1313
1314         case ZFCP_ERP_CONTINUES:
1315                 if (erp_action->status & ZFCP_STATUS_ERP_LOWMEM) {
1316                         --adapter->erp_low_mem_count;
1317                         erp_action->status &= ~ZFCP_STATUS_ERP_LOWMEM;
1318                 }
1319                 goto unlock;
1320         }
1321
1322         retval = zfcp_erp_strategy_check_target(erp_action, retval);
1323         zfcp_erp_action_dequeue(erp_action);
1324         retval = zfcp_erp_strategy_statechange(erp_action, retval);
1325         if (retval == ZFCP_ERP_EXIT)
1326                 goto unlock;
1327         zfcp_erp_strategy_followup_actions(erp_action);
1328
1329  unlock:
1330         write_unlock(&adapter->erp_lock);
1331         read_unlock_irqrestore(&zfcp_data.config_lock, flags);
1332
1333         if (retval != ZFCP_ERP_CONTINUES)
1334                 zfcp_erp_action_cleanup(erp_action, retval);
1335
1336         return retval;
1337 }
1338
1339 static int zfcp_erp_thread(void *data)
1340 {
1341         struct zfcp_adapter *adapter = (struct zfcp_adapter *) data;
1342         struct list_head *next;
1343         struct zfcp_erp_action *act;
1344         unsigned long flags;
1345         int ignore;
1346
1347         daemonize("zfcperp%s", dev_name(&adapter->ccw_device->dev));
1348         /* Block all signals */
1349         siginitsetinv(&current->blocked, 0);
1350         atomic_set_mask(ZFCP_STATUS_ADAPTER_ERP_THREAD_UP, &adapter->status);
1351         wake_up(&adapter->erp_thread_wqh);
1352
1353         while (!(atomic_read(&adapter->status) &
1354                  ZFCP_STATUS_ADAPTER_ERP_THREAD_KILL)) {
1355                 write_lock_irqsave(&adapter->erp_lock, flags);
1356                 next = adapter->erp_ready_head.next;
1357                 write_unlock_irqrestore(&adapter->erp_lock, flags);
1358
1359                 if (next != &adapter->erp_ready_head) {
1360                         act = list_entry(next, struct zfcp_erp_action, list);
1361
1362                         /* there is more to come after dismission, no notify */
1363                         if (zfcp_erp_strategy(act) != ZFCP_ERP_DISMISSED)
1364                                 zfcp_erp_wakeup(adapter);
1365                 }
1366
1367                 zfcp_rec_dbf_event_thread_lock("erthrd1", adapter);
1368                 ignore = down_interruptible(&adapter->erp_ready_sem);
1369                 zfcp_rec_dbf_event_thread_lock("erthrd2", adapter);
1370         }
1371
1372         atomic_clear_mask(ZFCP_STATUS_ADAPTER_ERP_THREAD_UP, &adapter->status);
1373         wake_up(&adapter->erp_thread_wqh);
1374
1375         return 0;
1376 }
1377
1378 /**
1379  * zfcp_erp_thread_setup - Start ERP thread for adapter
1380  * @adapter: Adapter to start the ERP thread for
1381  *
1382  * Returns 0 on success or error code from kernel_thread()
1383  */
1384 int zfcp_erp_thread_setup(struct zfcp_adapter *adapter)
1385 {
1386         int retval;
1387
1388         atomic_clear_mask(ZFCP_STATUS_ADAPTER_ERP_THREAD_UP, &adapter->status);
1389         retval = kernel_thread(zfcp_erp_thread, adapter, SIGCHLD);
1390         if (retval < 0) {
1391                 dev_err(&adapter->ccw_device->dev,
1392                         "Creating an ERP thread for the FCP device failed.\n");
1393                 return retval;
1394         }
1395         wait_event(adapter->erp_thread_wqh,
1396                    atomic_read(&adapter->status) &
1397                         ZFCP_STATUS_ADAPTER_ERP_THREAD_UP);
1398         return 0;
1399 }
1400
1401 /**
1402  * zfcp_erp_thread_kill - Stop ERP thread.
1403  * @adapter: Adapter where the ERP thread should be stopped.
1404  *
1405  * The caller of this routine ensures that the specified adapter has
1406  * been shut down and that this operation has been completed. Thus,
1407  * there are no pending erp_actions which would need to be handled
1408  * here.
1409  */
1410 void zfcp_erp_thread_kill(struct zfcp_adapter *adapter)
1411 {
1412         atomic_set_mask(ZFCP_STATUS_ADAPTER_ERP_THREAD_KILL, &adapter->status);
1413         up(&adapter->erp_ready_sem);
1414         zfcp_rec_dbf_event_thread_lock("erthrk1", adapter);
1415
1416         wait_event(adapter->erp_thread_wqh,
1417                    !(atomic_read(&adapter->status) &
1418                                 ZFCP_STATUS_ADAPTER_ERP_THREAD_UP));
1419
1420         atomic_clear_mask(ZFCP_STATUS_ADAPTER_ERP_THREAD_KILL,
1421                           &adapter->status);
1422 }
1423
1424 /**
1425  * zfcp_erp_adapter_failed - Set adapter status to failed.
1426  * @adapter: Failed adapter.
1427  * @id: Event id for debug trace.
1428  * @ref: Reference for debug trace.
1429  */
1430 void zfcp_erp_adapter_failed(struct zfcp_adapter *adapter, char *id, void *ref)
1431 {
1432         zfcp_erp_modify_adapter_status(adapter, id, ref,
1433                                        ZFCP_STATUS_COMMON_ERP_FAILED, ZFCP_SET);
1434 }
1435
1436 /**
1437  * zfcp_erp_port_failed - Set port status to failed.
1438  * @port: Failed port.
1439  * @id: Event id for debug trace.
1440  * @ref: Reference for debug trace.
1441  */
1442 void zfcp_erp_port_failed(struct zfcp_port *port, char *id, void *ref)
1443 {
1444         zfcp_erp_modify_port_status(port, id, ref,
1445                                     ZFCP_STATUS_COMMON_ERP_FAILED, ZFCP_SET);
1446 }
1447
1448 /**
1449  * zfcp_erp_unit_failed - Set unit status to failed.
1450  * @unit: Failed unit.
1451  * @id: Event id for debug trace.
1452  * @ref: Reference for debug trace.
1453  */
1454 void zfcp_erp_unit_failed(struct zfcp_unit *unit, char *id, void *ref)
1455 {
1456         zfcp_erp_modify_unit_status(unit, id, ref,
1457                                     ZFCP_STATUS_COMMON_ERP_FAILED, ZFCP_SET);
1458 }
1459
1460 /**
1461  * zfcp_erp_wait - wait for completion of error recovery on an adapter
1462  * @adapter: adapter for which to wait for completion of its error recovery
1463  */
1464 void zfcp_erp_wait(struct zfcp_adapter *adapter)
1465 {
1466         wait_event(adapter->erp_done_wqh,
1467                    !(atomic_read(&adapter->status) &
1468                         ZFCP_STATUS_ADAPTER_ERP_PENDING));
1469 }
1470
1471 /**
1472  * zfcp_erp_modify_adapter_status - change adapter status bits
1473  * @adapter: adapter to change the status
1474  * @id: id for the debug trace
1475  * @ref: reference for the debug trace
1476  * @mask: status bits to change
1477  * @set_or_clear: ZFCP_SET or ZFCP_CLEAR
1478  *
1479  * Changes in common status bits are propagated to attached ports and units.
1480  */
1481 void zfcp_erp_modify_adapter_status(struct zfcp_adapter *adapter, char *id,
1482                                     void *ref, u32 mask, int set_or_clear)
1483 {
1484         struct zfcp_port *port;
1485         u32 common_mask = mask & ZFCP_COMMON_FLAGS;
1486
1487         if (set_or_clear == ZFCP_SET) {
1488                 if (status_change_set(mask, &adapter->status))
1489                         zfcp_rec_dbf_event_adapter(id, ref, adapter);
1490                 atomic_set_mask(mask, &adapter->status);
1491         } else {
1492                 if (status_change_clear(mask, &adapter->status))
1493                         zfcp_rec_dbf_event_adapter(id, ref, adapter);
1494                 atomic_clear_mask(mask, &adapter->status);
1495                 if (mask & ZFCP_STATUS_COMMON_ERP_FAILED)
1496                         atomic_set(&adapter->erp_counter, 0);
1497         }
1498
1499         if (common_mask)
1500                 list_for_each_entry(port, &adapter->port_list_head, list)
1501                         zfcp_erp_modify_port_status(port, id, ref, common_mask,
1502                                                     set_or_clear);
1503 }
1504
1505 /**
1506  * zfcp_erp_modify_port_status - change port status bits
1507  * @port: port to change the status bits
1508  * @id: id for the debug trace
1509  * @ref: reference for the debug trace
1510  * @mask: status bits to change
1511  * @set_or_clear: ZFCP_SET or ZFCP_CLEAR
1512  *
1513  * Changes in common status bits are propagated to attached units.
1514  */
1515 void zfcp_erp_modify_port_status(struct zfcp_port *port, char *id, void *ref,
1516                                  u32 mask, int set_or_clear)
1517 {
1518         struct zfcp_unit *unit;
1519         u32 common_mask = mask & ZFCP_COMMON_FLAGS;
1520
1521         if (set_or_clear == ZFCP_SET) {
1522                 if (status_change_set(mask, &port->status))
1523                         zfcp_rec_dbf_event_port(id, ref, port);
1524                 atomic_set_mask(mask, &port->status);
1525         } else {
1526                 if (status_change_clear(mask, &port->status))
1527                         zfcp_rec_dbf_event_port(id, ref, port);
1528                 atomic_clear_mask(mask, &port->status);
1529                 if (mask & ZFCP_STATUS_COMMON_ERP_FAILED)
1530                         atomic_set(&port->erp_counter, 0);
1531         }
1532
1533         if (common_mask)
1534                 list_for_each_entry(unit, &port->unit_list_head, list)
1535                         zfcp_erp_modify_unit_status(unit, id, ref, common_mask,
1536                                                     set_or_clear);
1537 }
1538
1539 /**
1540  * zfcp_erp_modify_unit_status - change unit status bits
1541  * @unit: unit to change the status bits
1542  * @id: id for the debug trace
1543  * @ref: reference for the debug trace
1544  * @mask: status bits to change
1545  * @set_or_clear: ZFCP_SET or ZFCP_CLEAR
1546  */
1547 void zfcp_erp_modify_unit_status(struct zfcp_unit *unit, char *id, void *ref,
1548                                  u32 mask, int set_or_clear)
1549 {
1550         if (set_or_clear == ZFCP_SET) {
1551                 if (status_change_set(mask, &unit->status))
1552                         zfcp_rec_dbf_event_unit(id, ref, unit);
1553                 atomic_set_mask(mask, &unit->status);
1554         } else {
1555                 if (status_change_clear(mask, &unit->status))
1556                         zfcp_rec_dbf_event_unit(id, ref, unit);
1557                 atomic_clear_mask(mask, &unit->status);
1558                 if (mask & ZFCP_STATUS_COMMON_ERP_FAILED) {
1559                         atomic_set(&unit->erp_counter, 0);
1560                 }
1561         }
1562 }
1563
1564 /**
1565  * zfcp_erp_port_boxed - Mark port as "boxed" and start ERP
1566  * @port: The "boxed" port.
1567  * @id: The debug trace id.
1568  * @id: Reference for the debug trace.
1569  */
1570 void zfcp_erp_port_boxed(struct zfcp_port *port, char *id, void *ref)
1571 {
1572         unsigned long flags;
1573
1574         read_lock_irqsave(&zfcp_data.config_lock, flags);
1575         zfcp_erp_modify_port_status(port, id, ref,
1576                                     ZFCP_STATUS_COMMON_ACCESS_BOXED, ZFCP_SET);
1577         read_unlock_irqrestore(&zfcp_data.config_lock, flags);
1578         zfcp_erp_port_reopen(port, ZFCP_STATUS_COMMON_ERP_FAILED, id, ref);
1579 }
1580
1581 /**
1582  * zfcp_erp_unit_boxed - Mark unit as "boxed" and start ERP
1583  * @port: The "boxed" unit.
1584  * @id: The debug trace id.
1585  * @id: Reference for the debug trace.
1586  */
1587 void zfcp_erp_unit_boxed(struct zfcp_unit *unit, char *id, void *ref)
1588 {
1589         zfcp_erp_modify_unit_status(unit, id, ref,
1590                                     ZFCP_STATUS_COMMON_ACCESS_BOXED, ZFCP_SET);
1591         zfcp_erp_unit_reopen(unit, ZFCP_STATUS_COMMON_ERP_FAILED, id, ref);
1592 }
1593
1594 /**
1595  * zfcp_erp_port_access_denied - Adapter denied access to port.
1596  * @port: port where access has been denied
1597  * @id: id for debug trace
1598  * @ref: reference for debug trace
1599  *
1600  * Since the adapter has denied access, stop using the port and the
1601  * attached units.
1602  */
1603 void zfcp_erp_port_access_denied(struct zfcp_port *port, char *id, void *ref)
1604 {
1605         unsigned long flags;
1606
1607         read_lock_irqsave(&zfcp_data.config_lock, flags);
1608         zfcp_erp_modify_port_status(port, id, ref,
1609                                     ZFCP_STATUS_COMMON_ERP_FAILED |
1610                                     ZFCP_STATUS_COMMON_ACCESS_DENIED, ZFCP_SET);
1611         read_unlock_irqrestore(&zfcp_data.config_lock, flags);
1612 }
1613
1614 /**
1615  * zfcp_erp_unit_access_denied - Adapter denied access to unit.
1616  * @unit: unit where access has been denied
1617  * @id: id for debug trace
1618  * @ref: reference for debug trace
1619  *
1620  * Since the adapter has denied access, stop using the unit.
1621  */
1622 void zfcp_erp_unit_access_denied(struct zfcp_unit *unit, char *id, void *ref)
1623 {
1624         zfcp_erp_modify_unit_status(unit, id, ref,
1625                                     ZFCP_STATUS_COMMON_ERP_FAILED |
1626                                     ZFCP_STATUS_COMMON_ACCESS_DENIED, ZFCP_SET);
1627 }
1628
1629 static void zfcp_erp_unit_access_changed(struct zfcp_unit *unit, char *id,
1630                                          void *ref)
1631 {
1632         int status = atomic_read(&unit->status);
1633         if (!(status & (ZFCP_STATUS_COMMON_ACCESS_DENIED |
1634                         ZFCP_STATUS_COMMON_ACCESS_BOXED)))
1635                 return;
1636
1637         zfcp_erp_unit_reopen(unit, ZFCP_STATUS_COMMON_ERP_FAILED, id, ref);
1638 }
1639
1640 static void zfcp_erp_port_access_changed(struct zfcp_port *port, char *id,
1641                                          void *ref)
1642 {
1643         struct zfcp_unit *unit;
1644         int status = atomic_read(&port->status);
1645
1646         if (!(status & (ZFCP_STATUS_COMMON_ACCESS_DENIED |
1647                         ZFCP_STATUS_COMMON_ACCESS_BOXED))) {
1648                 list_for_each_entry(unit, &port->unit_list_head, list)
1649                                     zfcp_erp_unit_access_changed(unit, id, ref);
1650                 return;
1651         }
1652
1653         zfcp_erp_port_reopen(port, ZFCP_STATUS_COMMON_ERP_FAILED, id, ref);
1654 }
1655
1656 /**
1657  * zfcp_erp_adapter_access_changed - Process change in adapter ACT
1658  * @adapter: Adapter where the Access Control Table (ACT) changed
1659  * @id: Id for debug trace
1660  * @ref: Reference for debug trace
1661  */
1662 void zfcp_erp_adapter_access_changed(struct zfcp_adapter *adapter, char *id,
1663                                      void *ref)
1664 {
1665         struct zfcp_port *port;
1666         unsigned long flags;
1667
1668         if (adapter->connection_features & FSF_FEATURE_NPIV_MODE)
1669                 return;
1670
1671         read_lock_irqsave(&zfcp_data.config_lock, flags);
1672         list_for_each_entry(port, &adapter->port_list_head, list)
1673                 zfcp_erp_port_access_changed(port, id, ref);
1674         read_unlock_irqrestore(&zfcp_data.config_lock, flags);
1675 }