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