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