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