Merge tag 'scsi-fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/jejb/scsi
[muen/linux.git] / drivers / s390 / scsi / zfcp_erp.c
1 /*
2  * zfcp device driver
3  *
4  * Error Recovery Procedures (ERP).
5  *
6  * Copyright IBM Corp. 2002, 2016
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                 spin_lock(port->adapter->scsi_host->host_lock);
107                 __shost_for_each_device(sdev, port->adapter->scsi_host)
108                         if (sdev_to_zfcp(sdev)->port == port)
109                                 zfcp_erp_action_dismiss_lun(sdev);
110                 spin_unlock(port->adapter->scsi_host->host_lock);
111         }
112 }
113
114 static void zfcp_erp_action_dismiss_adapter(struct zfcp_adapter *adapter)
115 {
116         struct zfcp_port *port;
117
118         if (atomic_read(&adapter->status) & ZFCP_STATUS_COMMON_ERP_INUSE)
119                 zfcp_erp_action_dismiss(&adapter->erp_action);
120         else {
121                 read_lock(&adapter->port_list_lock);
122                 list_for_each_entry(port, &adapter->port_list, list)
123                     zfcp_erp_action_dismiss_port(port);
124                 read_unlock(&adapter->port_list_lock);
125         }
126 }
127
128 static int zfcp_erp_required_act(int want, struct zfcp_adapter *adapter,
129                                  struct zfcp_port *port,
130                                  struct scsi_device *sdev)
131 {
132         int need = want;
133         int l_status, p_status, a_status;
134         struct zfcp_scsi_dev *zfcp_sdev;
135
136         switch (want) {
137         case ZFCP_ERP_ACTION_REOPEN_LUN:
138                 zfcp_sdev = sdev_to_zfcp(sdev);
139                 l_status = atomic_read(&zfcp_sdev->status);
140                 if (l_status & ZFCP_STATUS_COMMON_ERP_INUSE)
141                         return 0;
142                 p_status = atomic_read(&port->status);
143                 if (!(p_status & ZFCP_STATUS_COMMON_RUNNING) ||
144                       p_status & ZFCP_STATUS_COMMON_ERP_FAILED)
145                         return 0;
146                 if (!(p_status & ZFCP_STATUS_COMMON_UNBLOCKED))
147                         need = ZFCP_ERP_ACTION_REOPEN_PORT;
148                 /* fall through */
149         case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
150                 p_status = atomic_read(&port->status);
151                 if (!(p_status & ZFCP_STATUS_COMMON_OPEN))
152                         need = ZFCP_ERP_ACTION_REOPEN_PORT;
153                 /* fall through */
154         case ZFCP_ERP_ACTION_REOPEN_PORT:
155                 p_status = atomic_read(&port->status);
156                 if (p_status & ZFCP_STATUS_COMMON_ERP_INUSE)
157                         return 0;
158                 a_status = atomic_read(&adapter->status);
159                 if (!(a_status & ZFCP_STATUS_COMMON_RUNNING) ||
160                       a_status & ZFCP_STATUS_COMMON_ERP_FAILED)
161                         return 0;
162                 if (p_status & ZFCP_STATUS_COMMON_NOESC)
163                         return need;
164                 if (!(a_status & ZFCP_STATUS_COMMON_UNBLOCKED))
165                         need = ZFCP_ERP_ACTION_REOPEN_ADAPTER;
166                 /* fall through */
167         case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
168                 a_status = atomic_read(&adapter->status);
169                 if (a_status & ZFCP_STATUS_COMMON_ERP_INUSE)
170                         return 0;
171                 if (!(a_status & ZFCP_STATUS_COMMON_RUNNING) &&
172                     !(a_status & ZFCP_STATUS_COMMON_OPEN))
173                         return 0; /* shutdown requested for closed adapter */
174         }
175
176         return need;
177 }
178
179 static struct zfcp_erp_action *zfcp_erp_setup_act(int need, u32 act_status,
180                                                   struct zfcp_adapter *adapter,
181                                                   struct zfcp_port *port,
182                                                   struct scsi_device *sdev)
183 {
184         struct zfcp_erp_action *erp_action;
185         struct zfcp_scsi_dev *zfcp_sdev;
186
187         switch (need) {
188         case ZFCP_ERP_ACTION_REOPEN_LUN:
189                 zfcp_sdev = sdev_to_zfcp(sdev);
190                 if (!(act_status & ZFCP_STATUS_ERP_NO_REF))
191                         if (scsi_device_get(sdev))
192                                 return NULL;
193                 atomic_or(ZFCP_STATUS_COMMON_ERP_INUSE,
194                                 &zfcp_sdev->status);
195                 erp_action = &zfcp_sdev->erp_action;
196                 WARN_ON_ONCE(erp_action->port != port);
197                 WARN_ON_ONCE(erp_action->sdev != sdev);
198                 if (!(atomic_read(&zfcp_sdev->status) &
199                       ZFCP_STATUS_COMMON_RUNNING))
200                         act_status |= ZFCP_STATUS_ERP_CLOSE_ONLY;
201                 break;
202
203         case ZFCP_ERP_ACTION_REOPEN_PORT:
204         case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
205                 if (!get_device(&port->dev))
206                         return NULL;
207                 zfcp_erp_action_dismiss_port(port);
208                 atomic_or(ZFCP_STATUS_COMMON_ERP_INUSE, &port->status);
209                 erp_action = &port->erp_action;
210                 WARN_ON_ONCE(erp_action->port != port);
211                 WARN_ON_ONCE(erp_action->sdev != NULL);
212                 if (!(atomic_read(&port->status) & ZFCP_STATUS_COMMON_RUNNING))
213                         act_status |= ZFCP_STATUS_ERP_CLOSE_ONLY;
214                 break;
215
216         case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
217                 kref_get(&adapter->ref);
218                 zfcp_erp_action_dismiss_adapter(adapter);
219                 atomic_or(ZFCP_STATUS_COMMON_ERP_INUSE, &adapter->status);
220                 erp_action = &adapter->erp_action;
221                 WARN_ON_ONCE(erp_action->port != NULL);
222                 WARN_ON_ONCE(erp_action->sdev != NULL);
223                 if (!(atomic_read(&adapter->status) &
224                       ZFCP_STATUS_COMMON_RUNNING))
225                         act_status |= ZFCP_STATUS_ERP_CLOSE_ONLY;
226                 break;
227
228         default:
229                 return NULL;
230         }
231
232         WARN_ON_ONCE(erp_action->adapter != adapter);
233         memset(&erp_action->list, 0, sizeof(erp_action->list));
234         memset(&erp_action->timer, 0, sizeof(erp_action->timer));
235         erp_action->step = ZFCP_ERP_STEP_UNINITIALIZED;
236         erp_action->fsf_req_id = 0;
237         erp_action->action = need;
238         erp_action->status = act_status;
239
240         return erp_action;
241 }
242
243 static int zfcp_erp_action_enqueue(int want, struct zfcp_adapter *adapter,
244                                    struct zfcp_port *port,
245                                    struct scsi_device *sdev,
246                                    char *id, u32 act_status)
247 {
248         int retval = 1, need;
249         struct zfcp_erp_action *act;
250
251         if (!adapter->erp_thread)
252                 return -EIO;
253
254         need = zfcp_erp_required_act(want, adapter, port, sdev);
255         if (!need)
256                 goto out;
257
258         act = zfcp_erp_setup_act(need, act_status, adapter, port, sdev);
259         if (!act)
260                 goto out;
261         atomic_or(ZFCP_STATUS_ADAPTER_ERP_PENDING, &adapter->status);
262         ++adapter->erp_total_count;
263         list_add_tail(&act->list, &adapter->erp_ready_head);
264         wake_up(&adapter->erp_ready_wq);
265         retval = 0;
266  out:
267         zfcp_dbf_rec_trig(id, adapter, port, sdev, want, need);
268         return retval;
269 }
270
271 static int _zfcp_erp_adapter_reopen(struct zfcp_adapter *adapter,
272                                     int clear_mask, char *id)
273 {
274         zfcp_erp_adapter_block(adapter, clear_mask);
275         zfcp_scsi_schedule_rports_block(adapter);
276
277         /* ensure propagation of failed status to new devices */
278         if (atomic_read(&adapter->status) & ZFCP_STATUS_COMMON_ERP_FAILED) {
279                 zfcp_erp_set_adapter_status(adapter,
280                                             ZFCP_STATUS_COMMON_ERP_FAILED);
281                 return -EIO;
282         }
283         return zfcp_erp_action_enqueue(ZFCP_ERP_ACTION_REOPEN_ADAPTER,
284                                        adapter, NULL, NULL, id, 0);
285 }
286
287 /**
288  * zfcp_erp_adapter_reopen - Reopen adapter.
289  * @adapter: Adapter to reopen.
290  * @clear: Status flags to clear.
291  * @id: Id for debug trace event.
292  */
293 void zfcp_erp_adapter_reopen(struct zfcp_adapter *adapter, int clear, char *id)
294 {
295         unsigned long flags;
296
297         zfcp_erp_adapter_block(adapter, clear);
298         zfcp_scsi_schedule_rports_block(adapter);
299
300         write_lock_irqsave(&adapter->erp_lock, flags);
301         if (atomic_read(&adapter->status) & ZFCP_STATUS_COMMON_ERP_FAILED)
302                 zfcp_erp_set_adapter_status(adapter,
303                                             ZFCP_STATUS_COMMON_ERP_FAILED);
304         else
305                 zfcp_erp_action_enqueue(ZFCP_ERP_ACTION_REOPEN_ADAPTER, adapter,
306                                         NULL, NULL, id, 0);
307         write_unlock_irqrestore(&adapter->erp_lock, flags);
308 }
309
310 /**
311  * zfcp_erp_adapter_shutdown - Shutdown adapter.
312  * @adapter: Adapter to shut down.
313  * @clear: Status flags to clear.
314  * @id: Id for debug trace event.
315  */
316 void zfcp_erp_adapter_shutdown(struct zfcp_adapter *adapter, int clear,
317                                char *id)
318 {
319         int flags = ZFCP_STATUS_COMMON_RUNNING | ZFCP_STATUS_COMMON_ERP_FAILED;
320         zfcp_erp_adapter_reopen(adapter, clear | flags, id);
321 }
322
323 /**
324  * zfcp_erp_port_shutdown - Shutdown port
325  * @port: Port to shut down.
326  * @clear: Status flags to clear.
327  * @id: Id for debug trace event.
328  */
329 void zfcp_erp_port_shutdown(struct zfcp_port *port, int clear, char *id)
330 {
331         int flags = ZFCP_STATUS_COMMON_RUNNING | ZFCP_STATUS_COMMON_ERP_FAILED;
332         zfcp_erp_port_reopen(port, clear | flags, id);
333 }
334
335 static void zfcp_erp_port_block(struct zfcp_port *port, int clear)
336 {
337         zfcp_erp_clear_port_status(port,
338                                     ZFCP_STATUS_COMMON_UNBLOCKED | clear);
339 }
340
341 static void _zfcp_erp_port_forced_reopen(struct zfcp_port *port, int clear,
342                                          char *id)
343 {
344         zfcp_erp_port_block(port, clear);
345         zfcp_scsi_schedule_rport_block(port);
346
347         if (atomic_read(&port->status) & ZFCP_STATUS_COMMON_ERP_FAILED)
348                 return;
349
350         zfcp_erp_action_enqueue(ZFCP_ERP_ACTION_REOPEN_PORT_FORCED,
351                                 port->adapter, port, NULL, id, 0);
352 }
353
354 /**
355  * zfcp_erp_port_forced_reopen - Forced close of port and open again
356  * @port: Port to force close and to reopen.
357  * @clear: Status flags to clear.
358  * @id: Id for debug trace event.
359  */
360 void zfcp_erp_port_forced_reopen(struct zfcp_port *port, int clear, char *id)
361 {
362         unsigned long flags;
363         struct zfcp_adapter *adapter = port->adapter;
364
365         write_lock_irqsave(&adapter->erp_lock, flags);
366         _zfcp_erp_port_forced_reopen(port, clear, id);
367         write_unlock_irqrestore(&adapter->erp_lock, flags);
368 }
369
370 static int _zfcp_erp_port_reopen(struct zfcp_port *port, int clear, char *id)
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, 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  * @id: Id for debug trace event.
390  *
391  * Returns 0 if recovery has been triggered, < 0 if not.
392  */
393 int zfcp_erp_port_reopen(struct zfcp_port *port, int clear, char *id)
394 {
395         int retval;
396         unsigned long flags;
397         struct zfcp_adapter *adapter = port->adapter;
398
399         write_lock_irqsave(&adapter->erp_lock, flags);
400         retval = _zfcp_erp_port_reopen(port, clear, id);
401         write_unlock_irqrestore(&adapter->erp_lock, flags);
402
403         return retval;
404 }
405
406 static void zfcp_erp_lun_block(struct scsi_device *sdev, int clear_mask)
407 {
408         zfcp_erp_clear_lun_status(sdev,
409                                   ZFCP_STATUS_COMMON_UNBLOCKED | clear_mask);
410 }
411
412 static void _zfcp_erp_lun_reopen(struct scsi_device *sdev, int clear, char *id,
413                                  u32 act_status)
414 {
415         struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
416         struct zfcp_adapter *adapter = zfcp_sdev->port->adapter;
417
418         zfcp_erp_lun_block(sdev, clear);
419
420         if (atomic_read(&zfcp_sdev->status) & ZFCP_STATUS_COMMON_ERP_FAILED)
421                 return;
422
423         zfcp_erp_action_enqueue(ZFCP_ERP_ACTION_REOPEN_LUN, adapter,
424                                 zfcp_sdev->port, sdev, id, act_status);
425 }
426
427 /**
428  * zfcp_erp_lun_reopen - initiate reopen of a LUN
429  * @sdev: SCSI device / LUN to be reopened
430  * @clear_mask: specifies flags in LUN status to be cleared
431  * @id: Id for debug trace event.
432  *
433  * Return: 0 on success, < 0 on error
434  */
435 void zfcp_erp_lun_reopen(struct scsi_device *sdev, int clear, char *id)
436 {
437         unsigned long flags;
438         struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
439         struct zfcp_port *port = zfcp_sdev->port;
440         struct zfcp_adapter *adapter = port->adapter;
441
442         write_lock_irqsave(&adapter->erp_lock, flags);
443         _zfcp_erp_lun_reopen(sdev, clear, id, 0);
444         write_unlock_irqrestore(&adapter->erp_lock, flags);
445 }
446
447 /**
448  * zfcp_erp_lun_shutdown - Shutdown LUN
449  * @sdev: SCSI device / LUN to shut down.
450  * @clear: Status flags to clear.
451  * @id: Id for debug trace event.
452  */
453 void zfcp_erp_lun_shutdown(struct scsi_device *sdev, int clear, char *id)
454 {
455         int flags = ZFCP_STATUS_COMMON_RUNNING | ZFCP_STATUS_COMMON_ERP_FAILED;
456         zfcp_erp_lun_reopen(sdev, clear | flags, id);
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, 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_or(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_or(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_or(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         setup_timer(&erp_action->timer, zfcp_erp_memwait_handler,
580                     (unsigned long) erp_action);
581         erp_action->timer.expires = jiffies + HZ;
582         add_timer(&erp_action->timer);
583 }
584
585 static void _zfcp_erp_port_reopen_all(struct zfcp_adapter *adapter,
586                                       int clear, char *id)
587 {
588         struct zfcp_port *port;
589
590         read_lock(&adapter->port_list_lock);
591         list_for_each_entry(port, &adapter->port_list, list)
592                 _zfcp_erp_port_reopen(port, clear, id);
593         read_unlock(&adapter->port_list_lock);
594 }
595
596 static void _zfcp_erp_lun_reopen_all(struct zfcp_port *port, int clear,
597                                      char *id)
598 {
599         struct scsi_device *sdev;
600
601         spin_lock(port->adapter->scsi_host->host_lock);
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, 0);
605         spin_unlock(port->adapter->scsi_host->host_lock);
606 }
607
608 static void zfcp_erp_strategy_followup_failed(struct zfcp_erp_action *act)
609 {
610         switch (act->action) {
611         case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
612                 _zfcp_erp_adapter_reopen(act->adapter, 0, "ersff_1");
613                 break;
614         case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
615                 _zfcp_erp_port_forced_reopen(act->port, 0, "ersff_2");
616                 break;
617         case ZFCP_ERP_ACTION_REOPEN_PORT:
618                 _zfcp_erp_port_reopen(act->port, 0, "ersff_3");
619                 break;
620         case ZFCP_ERP_ACTION_REOPEN_LUN:
621                 _zfcp_erp_lun_reopen(act->sdev, 0, "ersff_4", 0);
622                 break;
623         }
624 }
625
626 static void zfcp_erp_strategy_followup_success(struct zfcp_erp_action *act)
627 {
628         switch (act->action) {
629         case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
630                 _zfcp_erp_port_reopen_all(act->adapter, 0, "ersfs_1");
631                 break;
632         case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
633                 _zfcp_erp_port_reopen(act->port, 0, "ersfs_2");
634                 break;
635         case ZFCP_ERP_ACTION_REOPEN_PORT:
636                 _zfcp_erp_lun_reopen_all(act->port, 0, "ersfs_3");
637                 break;
638         }
639 }
640
641 static void zfcp_erp_wakeup(struct zfcp_adapter *adapter)
642 {
643         unsigned long flags;
644
645         read_lock_irqsave(&adapter->erp_lock, flags);
646         if (list_empty(&adapter->erp_ready_head) &&
647             list_empty(&adapter->erp_running_head)) {
648                         atomic_andnot(ZFCP_STATUS_ADAPTER_ERP_PENDING,
649                                           &adapter->status);
650                         wake_up(&adapter->erp_done_wqh);
651         }
652         read_unlock_irqrestore(&adapter->erp_lock, flags);
653 }
654
655 static void zfcp_erp_enqueue_ptp_port(struct zfcp_adapter *adapter)
656 {
657         struct zfcp_port *port;
658         port = zfcp_port_enqueue(adapter, adapter->peer_wwpn, 0,
659                                  adapter->peer_d_id);
660         if (IS_ERR(port)) /* error or port already attached */
661                 return;
662         _zfcp_erp_port_reopen(port, 0, "ereptp1");
663 }
664
665 static int zfcp_erp_adapter_strat_fsf_xconf(struct zfcp_erp_action *erp_action)
666 {
667         int retries;
668         int sleep = 1;
669         struct zfcp_adapter *adapter = erp_action->adapter;
670
671         atomic_andnot(ZFCP_STATUS_ADAPTER_XCONFIG_OK, &adapter->status);
672
673         for (retries = 7; retries; retries--) {
674                 atomic_andnot(ZFCP_STATUS_ADAPTER_HOST_CON_INIT,
675                                   &adapter->status);
676                 write_lock_irq(&adapter->erp_lock);
677                 zfcp_erp_action_to_running(erp_action);
678                 write_unlock_irq(&adapter->erp_lock);
679                 if (zfcp_fsf_exchange_config_data(erp_action)) {
680                         atomic_andnot(ZFCP_STATUS_ADAPTER_HOST_CON_INIT,
681                                           &adapter->status);
682                         return ZFCP_ERP_FAILED;
683                 }
684
685                 wait_event(adapter->erp_ready_wq,
686                            !list_empty(&adapter->erp_ready_head));
687                 if (erp_action->status & ZFCP_STATUS_ERP_TIMEDOUT)
688                         break;
689
690                 if (!(atomic_read(&adapter->status) &
691                       ZFCP_STATUS_ADAPTER_HOST_CON_INIT))
692                         break;
693
694                 ssleep(sleep);
695                 sleep *= 2;
696         }
697
698         atomic_andnot(ZFCP_STATUS_ADAPTER_HOST_CON_INIT,
699                           &adapter->status);
700
701         if (!(atomic_read(&adapter->status) & ZFCP_STATUS_ADAPTER_XCONFIG_OK))
702                 return ZFCP_ERP_FAILED;
703
704         if (fc_host_port_type(adapter->scsi_host) == FC_PORTTYPE_PTP)
705                 zfcp_erp_enqueue_ptp_port(adapter);
706
707         return ZFCP_ERP_SUCCEEDED;
708 }
709
710 static int zfcp_erp_adapter_strategy_open_fsf_xport(struct zfcp_erp_action *act)
711 {
712         int ret;
713         struct zfcp_adapter *adapter = act->adapter;
714
715         write_lock_irq(&adapter->erp_lock);
716         zfcp_erp_action_to_running(act);
717         write_unlock_irq(&adapter->erp_lock);
718
719         ret = zfcp_fsf_exchange_port_data(act);
720         if (ret == -EOPNOTSUPP)
721                 return ZFCP_ERP_SUCCEEDED;
722         if (ret)
723                 return ZFCP_ERP_FAILED;
724
725         zfcp_dbf_rec_run("erasox1", act);
726         wait_event(adapter->erp_ready_wq,
727                    !list_empty(&adapter->erp_ready_head));
728         zfcp_dbf_rec_run("erasox2", act);
729         if (act->status & ZFCP_STATUS_ERP_TIMEDOUT)
730                 return ZFCP_ERP_FAILED;
731
732         return ZFCP_ERP_SUCCEEDED;
733 }
734
735 static int zfcp_erp_adapter_strategy_open_fsf(struct zfcp_erp_action *act)
736 {
737         if (zfcp_erp_adapter_strat_fsf_xconf(act) == ZFCP_ERP_FAILED)
738                 return ZFCP_ERP_FAILED;
739
740         if (zfcp_erp_adapter_strategy_open_fsf_xport(act) == ZFCP_ERP_FAILED)
741                 return ZFCP_ERP_FAILED;
742
743         if (mempool_resize(act->adapter->pool.sr_data,
744                            act->adapter->stat_read_buf_num))
745                 return ZFCP_ERP_FAILED;
746
747         if (mempool_resize(act->adapter->pool.status_read_req,
748                            act->adapter->stat_read_buf_num))
749                 return ZFCP_ERP_FAILED;
750
751         atomic_set(&act->adapter->stat_miss, act->adapter->stat_read_buf_num);
752         if (zfcp_status_read_refill(act->adapter))
753                 return ZFCP_ERP_FAILED;
754
755         return ZFCP_ERP_SUCCEEDED;
756 }
757
758 static void zfcp_erp_adapter_strategy_close(struct zfcp_erp_action *act)
759 {
760         struct zfcp_adapter *adapter = act->adapter;
761
762         /* close queues to ensure that buffers are not accessed by adapter */
763         zfcp_qdio_close(adapter->qdio);
764         zfcp_fsf_req_dismiss_all(adapter);
765         adapter->fsf_req_seq_no = 0;
766         zfcp_fc_wka_ports_force_offline(adapter->gs);
767         /* all ports and LUNs are closed */
768         zfcp_erp_clear_adapter_status(adapter, ZFCP_STATUS_COMMON_OPEN);
769
770         atomic_andnot(ZFCP_STATUS_ADAPTER_XCONFIG_OK |
771                           ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED, &adapter->status);
772 }
773
774 static int zfcp_erp_adapter_strategy_open(struct zfcp_erp_action *act)
775 {
776         struct zfcp_adapter *adapter = act->adapter;
777
778         if (zfcp_qdio_open(adapter->qdio)) {
779                 atomic_andnot(ZFCP_STATUS_ADAPTER_XCONFIG_OK |
780                                   ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED,
781                                   &adapter->status);
782                 return ZFCP_ERP_FAILED;
783         }
784
785         if (zfcp_erp_adapter_strategy_open_fsf(act)) {
786                 zfcp_erp_adapter_strategy_close(act);
787                 return ZFCP_ERP_FAILED;
788         }
789
790         atomic_or(ZFCP_STATUS_COMMON_OPEN, &adapter->status);
791
792         return ZFCP_ERP_SUCCEEDED;
793 }
794
795 static int zfcp_erp_adapter_strategy(struct zfcp_erp_action *act)
796 {
797         struct zfcp_adapter *adapter = act->adapter;
798
799         if (atomic_read(&adapter->status) & ZFCP_STATUS_COMMON_OPEN) {
800                 zfcp_erp_adapter_strategy_close(act);
801                 if (act->status & ZFCP_STATUS_ERP_CLOSE_ONLY)
802                         return ZFCP_ERP_EXIT;
803         }
804
805         if (zfcp_erp_adapter_strategy_open(act)) {
806                 ssleep(8);
807                 return ZFCP_ERP_FAILED;
808         }
809
810         return ZFCP_ERP_SUCCEEDED;
811 }
812
813 static int zfcp_erp_port_forced_strategy_close(struct zfcp_erp_action *act)
814 {
815         int retval;
816
817         retval = zfcp_fsf_close_physical_port(act);
818         if (retval == -ENOMEM)
819                 return ZFCP_ERP_NOMEM;
820         act->step = ZFCP_ERP_STEP_PHYS_PORT_CLOSING;
821         if (retval)
822                 return ZFCP_ERP_FAILED;
823
824         return ZFCP_ERP_CONTINUES;
825 }
826
827 static int zfcp_erp_port_forced_strategy(struct zfcp_erp_action *erp_action)
828 {
829         struct zfcp_port *port = erp_action->port;
830         int status = atomic_read(&port->status);
831
832         switch (erp_action->step) {
833         case ZFCP_ERP_STEP_UNINITIALIZED:
834                 if ((status & ZFCP_STATUS_PORT_PHYS_OPEN) &&
835                     (status & ZFCP_STATUS_COMMON_OPEN))
836                         return zfcp_erp_port_forced_strategy_close(erp_action);
837                 else
838                         return ZFCP_ERP_FAILED;
839
840         case ZFCP_ERP_STEP_PHYS_PORT_CLOSING:
841                 if (!(status & ZFCP_STATUS_PORT_PHYS_OPEN))
842                         return ZFCP_ERP_SUCCEEDED;
843         }
844         return ZFCP_ERP_FAILED;
845 }
846
847 static int zfcp_erp_port_strategy_close(struct zfcp_erp_action *erp_action)
848 {
849         int retval;
850
851         retval = zfcp_fsf_close_port(erp_action);
852         if (retval == -ENOMEM)
853                 return ZFCP_ERP_NOMEM;
854         erp_action->step = ZFCP_ERP_STEP_PORT_CLOSING;
855         if (retval)
856                 return ZFCP_ERP_FAILED;
857         return ZFCP_ERP_CONTINUES;
858 }
859
860 static int zfcp_erp_port_strategy_open_port(struct zfcp_erp_action *erp_action)
861 {
862         int retval;
863
864         retval = zfcp_fsf_open_port(erp_action);
865         if (retval == -ENOMEM)
866                 return ZFCP_ERP_NOMEM;
867         erp_action->step = ZFCP_ERP_STEP_PORT_OPENING;
868         if (retval)
869                 return ZFCP_ERP_FAILED;
870         return ZFCP_ERP_CONTINUES;
871 }
872
873 static int zfcp_erp_open_ptp_port(struct zfcp_erp_action *act)
874 {
875         struct zfcp_adapter *adapter = act->adapter;
876         struct zfcp_port *port = act->port;
877
878         if (port->wwpn != adapter->peer_wwpn) {
879                 zfcp_erp_set_port_status(port, ZFCP_STATUS_COMMON_ERP_FAILED);
880                 return ZFCP_ERP_FAILED;
881         }
882         port->d_id = adapter->peer_d_id;
883         return zfcp_erp_port_strategy_open_port(act);
884 }
885
886 static int zfcp_erp_port_strategy_open_common(struct zfcp_erp_action *act)
887 {
888         struct zfcp_adapter *adapter = act->adapter;
889         struct zfcp_port *port = act->port;
890         int p_status = atomic_read(&port->status);
891
892         switch (act->step) {
893         case ZFCP_ERP_STEP_UNINITIALIZED:
894         case ZFCP_ERP_STEP_PHYS_PORT_CLOSING:
895         case ZFCP_ERP_STEP_PORT_CLOSING:
896                 if (fc_host_port_type(adapter->scsi_host) == FC_PORTTYPE_PTP)
897                         return zfcp_erp_open_ptp_port(act);
898                 if (!port->d_id) {
899                         zfcp_fc_trigger_did_lookup(port);
900                         return ZFCP_ERP_EXIT;
901                 }
902                 return zfcp_erp_port_strategy_open_port(act);
903
904         case ZFCP_ERP_STEP_PORT_OPENING:
905                 /* D_ID might have changed during open */
906                 if (p_status & ZFCP_STATUS_COMMON_OPEN) {
907                         if (!port->d_id) {
908                                 zfcp_fc_trigger_did_lookup(port);
909                                 return ZFCP_ERP_EXIT;
910                         }
911                         return ZFCP_ERP_SUCCEEDED;
912                 }
913                 if (port->d_id && !(p_status & ZFCP_STATUS_COMMON_NOESC)) {
914                         port->d_id = 0;
915                         return ZFCP_ERP_FAILED;
916                 }
917                 /* fall through otherwise */
918         }
919         return ZFCP_ERP_FAILED;
920 }
921
922 static int zfcp_erp_port_strategy(struct zfcp_erp_action *erp_action)
923 {
924         struct zfcp_port *port = erp_action->port;
925         int p_status = atomic_read(&port->status);
926
927         if ((p_status & ZFCP_STATUS_COMMON_NOESC) &&
928             !(p_status & ZFCP_STATUS_COMMON_OPEN))
929                 goto close_init_done;
930
931         switch (erp_action->step) {
932         case ZFCP_ERP_STEP_UNINITIALIZED:
933                 if (p_status & ZFCP_STATUS_COMMON_OPEN)
934                         return zfcp_erp_port_strategy_close(erp_action);
935                 break;
936
937         case ZFCP_ERP_STEP_PORT_CLOSING:
938                 if (p_status & ZFCP_STATUS_COMMON_OPEN)
939                         return ZFCP_ERP_FAILED;
940                 break;
941         }
942
943 close_init_done:
944         if (erp_action->status & ZFCP_STATUS_ERP_CLOSE_ONLY)
945                 return ZFCP_ERP_EXIT;
946
947         return zfcp_erp_port_strategy_open_common(erp_action);
948 }
949
950 static void zfcp_erp_lun_strategy_clearstati(struct scsi_device *sdev)
951 {
952         struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
953
954         atomic_andnot(ZFCP_STATUS_COMMON_ACCESS_DENIED,
955                           &zfcp_sdev->status);
956 }
957
958 static int zfcp_erp_lun_strategy_close(struct zfcp_erp_action *erp_action)
959 {
960         int retval = zfcp_fsf_close_lun(erp_action);
961         if (retval == -ENOMEM)
962                 return ZFCP_ERP_NOMEM;
963         erp_action->step = ZFCP_ERP_STEP_LUN_CLOSING;
964         if (retval)
965                 return ZFCP_ERP_FAILED;
966         return ZFCP_ERP_CONTINUES;
967 }
968
969 static int zfcp_erp_lun_strategy_open(struct zfcp_erp_action *erp_action)
970 {
971         int retval = zfcp_fsf_open_lun(erp_action);
972         if (retval == -ENOMEM)
973                 return ZFCP_ERP_NOMEM;
974         erp_action->step = ZFCP_ERP_STEP_LUN_OPENING;
975         if (retval)
976                 return  ZFCP_ERP_FAILED;
977         return ZFCP_ERP_CONTINUES;
978 }
979
980 static int zfcp_erp_lun_strategy(struct zfcp_erp_action *erp_action)
981 {
982         struct scsi_device *sdev = erp_action->sdev;
983         struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
984
985         switch (erp_action->step) {
986         case ZFCP_ERP_STEP_UNINITIALIZED:
987                 zfcp_erp_lun_strategy_clearstati(sdev);
988                 if (atomic_read(&zfcp_sdev->status) & ZFCP_STATUS_COMMON_OPEN)
989                         return zfcp_erp_lun_strategy_close(erp_action);
990                 /* already closed, fall through */
991         case ZFCP_ERP_STEP_LUN_CLOSING:
992                 if (atomic_read(&zfcp_sdev->status) & ZFCP_STATUS_COMMON_OPEN)
993                         return ZFCP_ERP_FAILED;
994                 if (erp_action->status & ZFCP_STATUS_ERP_CLOSE_ONLY)
995                         return ZFCP_ERP_EXIT;
996                 return zfcp_erp_lun_strategy_open(erp_action);
997
998         case ZFCP_ERP_STEP_LUN_OPENING:
999                 if (atomic_read(&zfcp_sdev->status) & ZFCP_STATUS_COMMON_OPEN)
1000                         return ZFCP_ERP_SUCCEEDED;
1001         }
1002         return ZFCP_ERP_FAILED;
1003 }
1004
1005 static int zfcp_erp_strategy_check_lun(struct scsi_device *sdev, int result)
1006 {
1007         struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
1008
1009         switch (result) {
1010         case ZFCP_ERP_SUCCEEDED :
1011                 atomic_set(&zfcp_sdev->erp_counter, 0);
1012                 zfcp_erp_lun_unblock(sdev);
1013                 break;
1014         case ZFCP_ERP_FAILED :
1015                 atomic_inc(&zfcp_sdev->erp_counter);
1016                 if (atomic_read(&zfcp_sdev->erp_counter) > ZFCP_MAX_ERPS) {
1017                         dev_err(&zfcp_sdev->port->adapter->ccw_device->dev,
1018                                 "ERP failed for LUN 0x%016Lx on "
1019                                 "port 0x%016Lx\n",
1020                                 (unsigned long long)zfcp_scsi_dev_lun(sdev),
1021                                 (unsigned long long)zfcp_sdev->port->wwpn);
1022                         zfcp_erp_set_lun_status(sdev,
1023                                                 ZFCP_STATUS_COMMON_ERP_FAILED);
1024                 }
1025                 break;
1026         }
1027
1028         if (atomic_read(&zfcp_sdev->status) & ZFCP_STATUS_COMMON_ERP_FAILED) {
1029                 zfcp_erp_lun_block(sdev, 0);
1030                 result = ZFCP_ERP_EXIT;
1031         }
1032         return result;
1033 }
1034
1035 static int zfcp_erp_strategy_check_port(struct zfcp_port *port, int result)
1036 {
1037         switch (result) {
1038         case ZFCP_ERP_SUCCEEDED :
1039                 atomic_set(&port->erp_counter, 0);
1040                 zfcp_erp_port_unblock(port);
1041                 break;
1042
1043         case ZFCP_ERP_FAILED :
1044                 if (atomic_read(&port->status) & ZFCP_STATUS_COMMON_NOESC) {
1045                         zfcp_erp_port_block(port, 0);
1046                         result = ZFCP_ERP_EXIT;
1047                 }
1048                 atomic_inc(&port->erp_counter);
1049                 if (atomic_read(&port->erp_counter) > ZFCP_MAX_ERPS) {
1050                         dev_err(&port->adapter->ccw_device->dev,
1051                                 "ERP failed for remote port 0x%016Lx\n",
1052                                 (unsigned long long)port->wwpn);
1053                         zfcp_erp_set_port_status(port,
1054                                          ZFCP_STATUS_COMMON_ERP_FAILED);
1055                 }
1056                 break;
1057         }
1058
1059         if (atomic_read(&port->status) & ZFCP_STATUS_COMMON_ERP_FAILED) {
1060                 zfcp_erp_port_block(port, 0);
1061                 result = ZFCP_ERP_EXIT;
1062         }
1063         return result;
1064 }
1065
1066 static int zfcp_erp_strategy_check_adapter(struct zfcp_adapter *adapter,
1067                                            int result)
1068 {
1069         switch (result) {
1070         case ZFCP_ERP_SUCCEEDED :
1071                 atomic_set(&adapter->erp_counter, 0);
1072                 zfcp_erp_adapter_unblock(adapter);
1073                 break;
1074
1075         case ZFCP_ERP_FAILED :
1076                 atomic_inc(&adapter->erp_counter);
1077                 if (atomic_read(&adapter->erp_counter) > ZFCP_MAX_ERPS) {
1078                         dev_err(&adapter->ccw_device->dev,
1079                                 "ERP cannot recover an error "
1080                                 "on the FCP device\n");
1081                         zfcp_erp_set_adapter_status(adapter,
1082                                             ZFCP_STATUS_COMMON_ERP_FAILED);
1083                 }
1084                 break;
1085         }
1086
1087         if (atomic_read(&adapter->status) & ZFCP_STATUS_COMMON_ERP_FAILED) {
1088                 zfcp_erp_adapter_block(adapter, 0);
1089                 result = ZFCP_ERP_EXIT;
1090         }
1091         return result;
1092 }
1093
1094 static int zfcp_erp_strategy_check_target(struct zfcp_erp_action *erp_action,
1095                                           int result)
1096 {
1097         struct zfcp_adapter *adapter = erp_action->adapter;
1098         struct zfcp_port *port = erp_action->port;
1099         struct scsi_device *sdev = erp_action->sdev;
1100
1101         switch (erp_action->action) {
1102
1103         case ZFCP_ERP_ACTION_REOPEN_LUN:
1104                 result = zfcp_erp_strategy_check_lun(sdev, result);
1105                 break;
1106
1107         case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
1108         case ZFCP_ERP_ACTION_REOPEN_PORT:
1109                 result = zfcp_erp_strategy_check_port(port, result);
1110                 break;
1111
1112         case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
1113                 result = zfcp_erp_strategy_check_adapter(adapter, result);
1114                 break;
1115         }
1116         return result;
1117 }
1118
1119 static int zfcp_erp_strat_change_det(atomic_t *target_status, u32 erp_status)
1120 {
1121         int status = atomic_read(target_status);
1122
1123         if ((status & ZFCP_STATUS_COMMON_RUNNING) &&
1124             (erp_status & ZFCP_STATUS_ERP_CLOSE_ONLY))
1125                 return 1; /* take it online */
1126
1127         if (!(status & ZFCP_STATUS_COMMON_RUNNING) &&
1128             !(erp_status & ZFCP_STATUS_ERP_CLOSE_ONLY))
1129                 return 1; /* take it offline */
1130
1131         return 0;
1132 }
1133
1134 static int zfcp_erp_strategy_statechange(struct zfcp_erp_action *act, int ret)
1135 {
1136         int action = act->action;
1137         struct zfcp_adapter *adapter = act->adapter;
1138         struct zfcp_port *port = act->port;
1139         struct scsi_device *sdev = act->sdev;
1140         struct zfcp_scsi_dev *zfcp_sdev;
1141         u32 erp_status = act->status;
1142
1143         switch (action) {
1144         case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
1145                 if (zfcp_erp_strat_change_det(&adapter->status, erp_status)) {
1146                         _zfcp_erp_adapter_reopen(adapter,
1147                                                  ZFCP_STATUS_COMMON_ERP_FAILED,
1148                                                  "ersscg1");
1149                         return ZFCP_ERP_EXIT;
1150                 }
1151                 break;
1152
1153         case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
1154         case ZFCP_ERP_ACTION_REOPEN_PORT:
1155                 if (zfcp_erp_strat_change_det(&port->status, erp_status)) {
1156                         _zfcp_erp_port_reopen(port,
1157                                               ZFCP_STATUS_COMMON_ERP_FAILED,
1158                                               "ersscg2");
1159                         return ZFCP_ERP_EXIT;
1160                 }
1161                 break;
1162
1163         case ZFCP_ERP_ACTION_REOPEN_LUN:
1164                 zfcp_sdev = sdev_to_zfcp(sdev);
1165                 if (zfcp_erp_strat_change_det(&zfcp_sdev->status, erp_status)) {
1166                         _zfcp_erp_lun_reopen(sdev,
1167                                              ZFCP_STATUS_COMMON_ERP_FAILED,
1168                                              "ersscg3", 0);
1169                         return ZFCP_ERP_EXIT;
1170                 }
1171                 break;
1172         }
1173         return ret;
1174 }
1175
1176 static void zfcp_erp_action_dequeue(struct zfcp_erp_action *erp_action)
1177 {
1178         struct zfcp_adapter *adapter = erp_action->adapter;
1179         struct zfcp_scsi_dev *zfcp_sdev;
1180
1181         adapter->erp_total_count--;
1182         if (erp_action->status & ZFCP_STATUS_ERP_LOWMEM) {
1183                 adapter->erp_low_mem_count--;
1184                 erp_action->status &= ~ZFCP_STATUS_ERP_LOWMEM;
1185         }
1186
1187         list_del(&erp_action->list);
1188         zfcp_dbf_rec_run("eractd1", erp_action);
1189
1190         switch (erp_action->action) {
1191         case ZFCP_ERP_ACTION_REOPEN_LUN:
1192                 zfcp_sdev = sdev_to_zfcp(erp_action->sdev);
1193                 atomic_andnot(ZFCP_STATUS_COMMON_ERP_INUSE,
1194                                   &zfcp_sdev->status);
1195                 break;
1196
1197         case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
1198         case ZFCP_ERP_ACTION_REOPEN_PORT:
1199                 atomic_andnot(ZFCP_STATUS_COMMON_ERP_INUSE,
1200                                   &erp_action->port->status);
1201                 break;
1202
1203         case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
1204                 atomic_andnot(ZFCP_STATUS_COMMON_ERP_INUSE,
1205                                   &erp_action->adapter->status);
1206                 break;
1207         }
1208 }
1209
1210 /**
1211  * zfcp_erp_try_rport_unblock - unblock rport if no more/new recovery
1212  * @port: zfcp_port whose fc_rport we should try to unblock
1213  */
1214 static void zfcp_erp_try_rport_unblock(struct zfcp_port *port)
1215 {
1216         unsigned long flags;
1217         struct zfcp_adapter *adapter = port->adapter;
1218         int port_status;
1219         struct Scsi_Host *shost = adapter->scsi_host;
1220         struct scsi_device *sdev;
1221
1222         write_lock_irqsave(&adapter->erp_lock, flags);
1223         port_status = atomic_read(&port->status);
1224         if ((port_status & ZFCP_STATUS_COMMON_UNBLOCKED)    == 0 ||
1225             (port_status & (ZFCP_STATUS_COMMON_ERP_INUSE |
1226                             ZFCP_STATUS_COMMON_ERP_FAILED)) != 0) {
1227                 /* new ERP of severity >= port triggered elsewhere meanwhile or
1228                  * local link down (adapter erp_failed but not clear unblock)
1229                  */
1230                 zfcp_dbf_rec_run_lvl(4, "ertru_p", &port->erp_action);
1231                 write_unlock_irqrestore(&adapter->erp_lock, flags);
1232                 return;
1233         }
1234         spin_lock(shost->host_lock);
1235         __shost_for_each_device(sdev, shost) {
1236                 struct zfcp_scsi_dev *zsdev = sdev_to_zfcp(sdev);
1237                 int lun_status;
1238
1239                 if (zsdev->port != port)
1240                         continue;
1241                 /* LUN under port of interest */
1242                 lun_status = atomic_read(&zsdev->status);
1243                 if ((lun_status & ZFCP_STATUS_COMMON_ERP_FAILED) != 0)
1244                         continue; /* unblock rport despite failed LUNs */
1245                 /* LUN recovery not given up yet [maybe follow-up pending] */
1246                 if ((lun_status & ZFCP_STATUS_COMMON_UNBLOCKED) == 0 ||
1247                     (lun_status & ZFCP_STATUS_COMMON_ERP_INUSE) != 0) {
1248                         /* LUN blocked:
1249                          * not yet unblocked [LUN recovery pending]
1250                          * or meanwhile blocked [new LUN recovery triggered]
1251                          */
1252                         zfcp_dbf_rec_run_lvl(4, "ertru_l", &zsdev->erp_action);
1253                         spin_unlock(shost->host_lock);
1254                         write_unlock_irqrestore(&adapter->erp_lock, flags);
1255                         return;
1256                 }
1257         }
1258         /* now port has no child or all children have completed recovery,
1259          * and no ERP of severity >= port was meanwhile triggered elsewhere
1260          */
1261         zfcp_scsi_schedule_rport_register(port);
1262         spin_unlock(shost->host_lock);
1263         write_unlock_irqrestore(&adapter->erp_lock, flags);
1264 }
1265
1266 static void zfcp_erp_action_cleanup(struct zfcp_erp_action *act, int result)
1267 {
1268         struct zfcp_adapter *adapter = act->adapter;
1269         struct zfcp_port *port = act->port;
1270         struct scsi_device *sdev = act->sdev;
1271
1272         switch (act->action) {
1273         case ZFCP_ERP_ACTION_REOPEN_LUN:
1274                 if (!(act->status & ZFCP_STATUS_ERP_NO_REF))
1275                         scsi_device_put(sdev);
1276                 zfcp_erp_try_rport_unblock(port);
1277                 break;
1278
1279         case ZFCP_ERP_ACTION_REOPEN_PORT:
1280                 /* This switch case might also happen after a forced reopen
1281                  * was successfully done and thus overwritten with a new
1282                  * non-forced reopen at `ersfs_2'. In this case, we must not
1283                  * do the clean-up of the non-forced version.
1284                  */
1285                 if (act->step != ZFCP_ERP_STEP_UNINITIALIZED)
1286                         if (result == ZFCP_ERP_SUCCEEDED)
1287                                 zfcp_erp_try_rport_unblock(port);
1288                 /* fall through */
1289         case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
1290                 put_device(&port->dev);
1291                 break;
1292
1293         case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
1294                 if (result == ZFCP_ERP_SUCCEEDED) {
1295                         register_service_level(&adapter->service_level);
1296                         zfcp_fc_conditional_port_scan(adapter);
1297                         queue_work(adapter->work_queue, &adapter->ns_up_work);
1298                 } else
1299                         unregister_service_level(&adapter->service_level);
1300
1301                 kref_put(&adapter->ref, zfcp_adapter_release);
1302                 break;
1303         }
1304 }
1305
1306 static int zfcp_erp_strategy_do_action(struct zfcp_erp_action *erp_action)
1307 {
1308         switch (erp_action->action) {
1309         case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
1310                 return zfcp_erp_adapter_strategy(erp_action);
1311         case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
1312                 return zfcp_erp_port_forced_strategy(erp_action);
1313         case ZFCP_ERP_ACTION_REOPEN_PORT:
1314                 return zfcp_erp_port_strategy(erp_action);
1315         case ZFCP_ERP_ACTION_REOPEN_LUN:
1316                 return zfcp_erp_lun_strategy(erp_action);
1317         }
1318         return ZFCP_ERP_FAILED;
1319 }
1320
1321 static int zfcp_erp_strategy(struct zfcp_erp_action *erp_action)
1322 {
1323         int retval;
1324         unsigned long flags;
1325         struct zfcp_adapter *adapter = erp_action->adapter;
1326
1327         kref_get(&adapter->ref);
1328
1329         write_lock_irqsave(&adapter->erp_lock, flags);
1330         zfcp_erp_strategy_check_fsfreq(erp_action);
1331
1332         if (erp_action->status & ZFCP_STATUS_ERP_DISMISSED) {
1333                 zfcp_erp_action_dequeue(erp_action);
1334                 retval = ZFCP_ERP_DISMISSED;
1335                 goto unlock;
1336         }
1337
1338         if (erp_action->status & ZFCP_STATUS_ERP_TIMEDOUT) {
1339                 retval = ZFCP_ERP_FAILED;
1340                 goto check_target;
1341         }
1342
1343         zfcp_erp_action_to_running(erp_action);
1344
1345         /* no lock to allow for blocking operations */
1346         write_unlock_irqrestore(&adapter->erp_lock, flags);
1347         retval = zfcp_erp_strategy_do_action(erp_action);
1348         write_lock_irqsave(&adapter->erp_lock, flags);
1349
1350         if (erp_action->status & ZFCP_STATUS_ERP_DISMISSED)
1351                 retval = ZFCP_ERP_CONTINUES;
1352
1353         switch (retval) {
1354         case ZFCP_ERP_NOMEM:
1355                 if (!(erp_action->status & ZFCP_STATUS_ERP_LOWMEM)) {
1356                         ++adapter->erp_low_mem_count;
1357                         erp_action->status |= ZFCP_STATUS_ERP_LOWMEM;
1358                 }
1359                 if (adapter->erp_total_count == adapter->erp_low_mem_count)
1360                         _zfcp_erp_adapter_reopen(adapter, 0, "erstgy1");
1361                 else {
1362                         zfcp_erp_strategy_memwait(erp_action);
1363                         retval = ZFCP_ERP_CONTINUES;
1364                 }
1365                 goto unlock;
1366
1367         case ZFCP_ERP_CONTINUES:
1368                 if (erp_action->status & ZFCP_STATUS_ERP_LOWMEM) {
1369                         --adapter->erp_low_mem_count;
1370                         erp_action->status &= ~ZFCP_STATUS_ERP_LOWMEM;
1371                 }
1372                 goto unlock;
1373         }
1374
1375 check_target:
1376         retval = zfcp_erp_strategy_check_target(erp_action, retval);
1377         zfcp_erp_action_dequeue(erp_action);
1378         retval = zfcp_erp_strategy_statechange(erp_action, retval);
1379         if (retval == ZFCP_ERP_EXIT)
1380                 goto unlock;
1381         if (retval == ZFCP_ERP_SUCCEEDED)
1382                 zfcp_erp_strategy_followup_success(erp_action);
1383         if (retval == ZFCP_ERP_FAILED)
1384                 zfcp_erp_strategy_followup_failed(erp_action);
1385
1386  unlock:
1387         write_unlock_irqrestore(&adapter->erp_lock, flags);
1388
1389         if (retval != ZFCP_ERP_CONTINUES)
1390                 zfcp_erp_action_cleanup(erp_action, retval);
1391
1392         kref_put(&adapter->ref, zfcp_adapter_release);
1393         return retval;
1394 }
1395
1396 static int zfcp_erp_thread(void *data)
1397 {
1398         struct zfcp_adapter *adapter = (struct zfcp_adapter *) data;
1399         struct list_head *next;
1400         struct zfcp_erp_action *act;
1401         unsigned long flags;
1402
1403         for (;;) {
1404                 wait_event_interruptible(adapter->erp_ready_wq,
1405                            !list_empty(&adapter->erp_ready_head) ||
1406                            kthread_should_stop());
1407
1408                 if (kthread_should_stop())
1409                         break;
1410
1411                 write_lock_irqsave(&adapter->erp_lock, flags);
1412                 next = adapter->erp_ready_head.next;
1413                 write_unlock_irqrestore(&adapter->erp_lock, flags);
1414
1415                 if (next != &adapter->erp_ready_head) {
1416                         act = list_entry(next, struct zfcp_erp_action, list);
1417
1418                         /* there is more to come after dismission, no notify */
1419                         if (zfcp_erp_strategy(act) != ZFCP_ERP_DISMISSED)
1420                                 zfcp_erp_wakeup(adapter);
1421                 }
1422         }
1423
1424         return 0;
1425 }
1426
1427 /**
1428  * zfcp_erp_thread_setup - Start ERP thread for adapter
1429  * @adapter: Adapter to start the ERP thread for
1430  *
1431  * Returns 0 on success or error code from kernel_thread()
1432  */
1433 int zfcp_erp_thread_setup(struct zfcp_adapter *adapter)
1434 {
1435         struct task_struct *thread;
1436
1437         thread = kthread_run(zfcp_erp_thread, adapter, "zfcperp%s",
1438                              dev_name(&adapter->ccw_device->dev));
1439         if (IS_ERR(thread)) {
1440                 dev_err(&adapter->ccw_device->dev,
1441                         "Creating an ERP thread for the FCP device failed.\n");
1442                 return PTR_ERR(thread);
1443         }
1444
1445         adapter->erp_thread = thread;
1446         return 0;
1447 }
1448
1449 /**
1450  * zfcp_erp_thread_kill - Stop ERP thread.
1451  * @adapter: Adapter where the ERP thread should be stopped.
1452  *
1453  * The caller of this routine ensures that the specified adapter has
1454  * been shut down and that this operation has been completed. Thus,
1455  * there are no pending erp_actions which would need to be handled
1456  * here.
1457  */
1458 void zfcp_erp_thread_kill(struct zfcp_adapter *adapter)
1459 {
1460         kthread_stop(adapter->erp_thread);
1461         adapter->erp_thread = NULL;
1462         WARN_ON(!list_empty(&adapter->erp_ready_head));
1463         WARN_ON(!list_empty(&adapter->erp_running_head));
1464 }
1465
1466 /**
1467  * zfcp_erp_wait - wait for completion of error recovery on an adapter
1468  * @adapter: adapter for which to wait for completion of its error recovery
1469  */
1470 void zfcp_erp_wait(struct zfcp_adapter *adapter)
1471 {
1472         wait_event(adapter->erp_done_wqh,
1473                    !(atomic_read(&adapter->status) &
1474                         ZFCP_STATUS_ADAPTER_ERP_PENDING));
1475 }
1476
1477 /**
1478  * zfcp_erp_set_adapter_status - set adapter status bits
1479  * @adapter: adapter to change the status
1480  * @mask: status bits to change
1481  *
1482  * Changes in common status bits are propagated to attached ports and LUNs.
1483  */
1484 void zfcp_erp_set_adapter_status(struct zfcp_adapter *adapter, u32 mask)
1485 {
1486         struct zfcp_port *port;
1487         struct scsi_device *sdev;
1488         unsigned long flags;
1489         u32 common_mask = mask & ZFCP_COMMON_FLAGS;
1490
1491         atomic_or(mask, &adapter->status);
1492
1493         if (!common_mask)
1494                 return;
1495
1496         read_lock_irqsave(&adapter->port_list_lock, flags);
1497         list_for_each_entry(port, &adapter->port_list, list)
1498                 atomic_or(common_mask, &port->status);
1499         read_unlock_irqrestore(&adapter->port_list_lock, flags);
1500
1501         spin_lock_irqsave(adapter->scsi_host->host_lock, flags);
1502         __shost_for_each_device(sdev, adapter->scsi_host)
1503                 atomic_or(common_mask, &sdev_to_zfcp(sdev)->status);
1504         spin_unlock_irqrestore(adapter->scsi_host->host_lock, flags);
1505 }
1506
1507 /**
1508  * zfcp_erp_clear_adapter_status - clear adapter status bits
1509  * @adapter: adapter to change the status
1510  * @mask: status bits to change
1511  *
1512  * Changes in common status bits are propagated to attached ports and LUNs.
1513  */
1514 void zfcp_erp_clear_adapter_status(struct zfcp_adapter *adapter, u32 mask)
1515 {
1516         struct zfcp_port *port;
1517         struct scsi_device *sdev;
1518         unsigned long flags;
1519         u32 common_mask = mask & ZFCP_COMMON_FLAGS;
1520         u32 clear_counter = mask & ZFCP_STATUS_COMMON_ERP_FAILED;
1521
1522         atomic_andnot(mask, &adapter->status);
1523
1524         if (!common_mask)
1525                 return;
1526
1527         if (clear_counter)
1528                 atomic_set(&adapter->erp_counter, 0);
1529
1530         read_lock_irqsave(&adapter->port_list_lock, flags);
1531         list_for_each_entry(port, &adapter->port_list, list) {
1532                 atomic_andnot(common_mask, &port->status);
1533                 if (clear_counter)
1534                         atomic_set(&port->erp_counter, 0);
1535         }
1536         read_unlock_irqrestore(&adapter->port_list_lock, flags);
1537
1538         spin_lock_irqsave(adapter->scsi_host->host_lock, flags);
1539         __shost_for_each_device(sdev, adapter->scsi_host) {
1540                 atomic_andnot(common_mask, &sdev_to_zfcp(sdev)->status);
1541                 if (clear_counter)
1542                         atomic_set(&sdev_to_zfcp(sdev)->erp_counter, 0);
1543         }
1544         spin_unlock_irqrestore(adapter->scsi_host->host_lock, flags);
1545 }
1546
1547 /**
1548  * zfcp_erp_set_port_status - set port status bits
1549  * @port: port to change the status
1550  * @mask: status bits to change
1551  *
1552  * Changes in common status bits are propagated to attached LUNs.
1553  */
1554 void zfcp_erp_set_port_status(struct zfcp_port *port, u32 mask)
1555 {
1556         struct scsi_device *sdev;
1557         u32 common_mask = mask & ZFCP_COMMON_FLAGS;
1558         unsigned long flags;
1559
1560         atomic_or(mask, &port->status);
1561
1562         if (!common_mask)
1563                 return;
1564
1565         spin_lock_irqsave(port->adapter->scsi_host->host_lock, flags);
1566         __shost_for_each_device(sdev, port->adapter->scsi_host)
1567                 if (sdev_to_zfcp(sdev)->port == port)
1568                         atomic_or(common_mask,
1569                                         &sdev_to_zfcp(sdev)->status);
1570         spin_unlock_irqrestore(port->adapter->scsi_host->host_lock, flags);
1571 }
1572
1573 /**
1574  * zfcp_erp_clear_port_status - clear port status bits
1575  * @port: adapter to change the status
1576  * @mask: status bits to change
1577  *
1578  * Changes in common status bits are propagated to attached LUNs.
1579  */
1580 void zfcp_erp_clear_port_status(struct zfcp_port *port, u32 mask)
1581 {
1582         struct scsi_device *sdev;
1583         u32 common_mask = mask & ZFCP_COMMON_FLAGS;
1584         u32 clear_counter = mask & ZFCP_STATUS_COMMON_ERP_FAILED;
1585         unsigned long flags;
1586
1587         atomic_andnot(mask, &port->status);
1588
1589         if (!common_mask)
1590                 return;
1591
1592         if (clear_counter)
1593                 atomic_set(&port->erp_counter, 0);
1594
1595         spin_lock_irqsave(port->adapter->scsi_host->host_lock, flags);
1596         __shost_for_each_device(sdev, port->adapter->scsi_host)
1597                 if (sdev_to_zfcp(sdev)->port == port) {
1598                         atomic_andnot(common_mask,
1599                                           &sdev_to_zfcp(sdev)->status);
1600                         if (clear_counter)
1601                                 atomic_set(&sdev_to_zfcp(sdev)->erp_counter, 0);
1602                 }
1603         spin_unlock_irqrestore(port->adapter->scsi_host->host_lock, flags);
1604 }
1605
1606 /**
1607  * zfcp_erp_set_lun_status - set lun status bits
1608  * @sdev: SCSI device / lun to set the status bits
1609  * @mask: status bits to change
1610  */
1611 void zfcp_erp_set_lun_status(struct scsi_device *sdev, u32 mask)
1612 {
1613         struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
1614
1615         atomic_or(mask, &zfcp_sdev->status);
1616 }
1617
1618 /**
1619  * zfcp_erp_clear_lun_status - clear lun status bits
1620  * @sdev: SCSi device / lun to clear the status bits
1621  * @mask: status bits to change
1622  */
1623 void zfcp_erp_clear_lun_status(struct scsi_device *sdev, u32 mask)
1624 {
1625         struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
1626
1627         atomic_andnot(mask, &zfcp_sdev->status);
1628
1629         if (mask & ZFCP_STATUS_COMMON_ERP_FAILED)
1630                 atomic_set(&zfcp_sdev->erp_counter, 0);
1631 }
1632