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