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