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