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