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