[SCSI] zfcp: Move FC code to new file
[linux-2.6] / drivers / s390 / scsi / zfcp_erp.c
1 /*
2  * This file is part of the zfcp device driver for
3  * FCP adapters for IBM System z9 and zSeries.
4  *
5  * (C) Copyright IBM Corp. 2002, 2006
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2, or (at your option)
10  * any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  */
21
22 #define ZFCP_LOG_AREA                   ZFCP_LOG_AREA_ERP
23
24 #include "zfcp_ext.h"
25
26 static int zfcp_erp_adapter_reopen_internal(struct zfcp_adapter *, int, u8,
27                                             void *);
28 static int zfcp_erp_port_forced_reopen_internal(struct zfcp_port *, int, u8,
29                                                 void *);
30 static int zfcp_erp_port_reopen_internal(struct zfcp_port *, int, u8, void *);
31 static int zfcp_erp_unit_reopen_internal(struct zfcp_unit *, int, u8, void *);
32
33 static int zfcp_erp_port_reopen_all_internal(struct zfcp_adapter *, int, u8,
34                                              void *);
35 static int zfcp_erp_unit_reopen_all_internal(struct zfcp_port *, int, u8,
36                                              void *);
37
38 static void zfcp_erp_adapter_block(struct zfcp_adapter *, int);
39 static void zfcp_erp_adapter_unblock(struct zfcp_adapter *);
40 static void zfcp_erp_port_block(struct zfcp_port *, int);
41 static void zfcp_erp_port_unblock(struct zfcp_port *);
42 static void zfcp_erp_unit_block(struct zfcp_unit *, int);
43 static void zfcp_erp_unit_unblock(struct zfcp_unit *);
44
45 static int zfcp_erp_thread(void *);
46
47 static int zfcp_erp_strategy(struct zfcp_erp_action *);
48
49 static int zfcp_erp_strategy_do_action(struct zfcp_erp_action *);
50 static int zfcp_erp_strategy_memwait(struct zfcp_erp_action *);
51 static int zfcp_erp_strategy_check_target(struct zfcp_erp_action *, int);
52 static int zfcp_erp_strategy_check_unit(struct zfcp_unit *, int);
53 static int zfcp_erp_strategy_check_port(struct zfcp_port *, int);
54 static int zfcp_erp_strategy_check_adapter(struct zfcp_adapter *, int);
55 static int zfcp_erp_strategy_statechange(int, u32, struct zfcp_adapter *,
56                                          struct zfcp_port *,
57                                          struct zfcp_unit *, int);
58 static int zfcp_erp_strategy_statechange_detected(atomic_t *, u32);
59 static int zfcp_erp_strategy_followup_actions(int, struct zfcp_adapter *,
60                                               struct zfcp_port *,
61                                               struct zfcp_unit *, int);
62 static int zfcp_erp_strategy_check_queues(struct zfcp_adapter *);
63 static int zfcp_erp_strategy_check_action(struct zfcp_erp_action *, int);
64
65 static int zfcp_erp_adapter_strategy(struct zfcp_erp_action *);
66 static int zfcp_erp_adapter_strategy_generic(struct zfcp_erp_action *, int);
67 static int zfcp_erp_adapter_strategy_close(struct zfcp_erp_action *);
68 static int zfcp_erp_adapter_strategy_open(struct zfcp_erp_action *);
69 static int zfcp_erp_adapter_strategy_open_qdio(struct zfcp_erp_action *);
70 static int zfcp_erp_adapter_strategy_open_fsf(struct zfcp_erp_action *);
71 static int zfcp_erp_adapter_strategy_open_fsf_xconfig(struct zfcp_erp_action *);
72 static int zfcp_erp_adapter_strategy_open_fsf_xport(struct zfcp_erp_action *);
73 static int zfcp_erp_adapter_strategy_open_fsf_statusread(
74         struct zfcp_erp_action *);
75
76 static int zfcp_erp_port_forced_strategy(struct zfcp_erp_action *);
77 static int zfcp_erp_port_forced_strategy_close(struct zfcp_erp_action *);
78
79 static int zfcp_erp_port_strategy(struct zfcp_erp_action *);
80 static int zfcp_erp_port_strategy_clearstati(struct zfcp_port *);
81 static int zfcp_erp_port_strategy_close(struct zfcp_erp_action *);
82 static int zfcp_erp_port_strategy_open(struct zfcp_erp_action *);
83 static int zfcp_erp_port_strategy_open_nameserver(struct zfcp_erp_action *);
84 static int zfcp_erp_port_strategy_open_nameserver_wakeup(
85         struct zfcp_erp_action *);
86 static int zfcp_erp_port_strategy_open_common(struct zfcp_erp_action *);
87 static int zfcp_erp_port_strategy_open_common_lookup(struct zfcp_erp_action *);
88 static int zfcp_erp_port_strategy_open_port(struct zfcp_erp_action *);
89
90 static int zfcp_erp_unit_strategy(struct zfcp_erp_action *);
91 static int zfcp_erp_unit_strategy_clearstati(struct zfcp_unit *);
92 static int zfcp_erp_unit_strategy_close(struct zfcp_erp_action *);
93 static int zfcp_erp_unit_strategy_open(struct zfcp_erp_action *);
94
95 static void zfcp_erp_action_dismiss_adapter(struct zfcp_adapter *);
96 static void zfcp_erp_action_dismiss_port(struct zfcp_port *);
97 static void zfcp_erp_action_dismiss_unit(struct zfcp_unit *);
98 static void zfcp_erp_action_dismiss(struct zfcp_erp_action *);
99
100 static int zfcp_erp_action_enqueue(int, struct zfcp_adapter *,
101                                    struct zfcp_port *, struct zfcp_unit *,
102                                    u8 id, void *ref);
103 static int zfcp_erp_action_dequeue(struct zfcp_erp_action *);
104 static void zfcp_erp_action_cleanup(int, struct zfcp_adapter *,
105                                     struct zfcp_port *, struct zfcp_unit *,
106                                     int);
107
108 static void zfcp_erp_action_ready(struct zfcp_erp_action *);
109 static int  zfcp_erp_action_exists(struct zfcp_erp_action *);
110
111 static void zfcp_erp_action_to_ready(struct zfcp_erp_action *);
112 static void zfcp_erp_action_to_running(struct zfcp_erp_action *);
113
114 static void zfcp_erp_memwait_handler(unsigned long);
115
116 /**
117  * zfcp_close_qdio - close qdio queues for an adapter
118  */
119 static void zfcp_close_qdio(struct zfcp_adapter *adapter)
120 {
121         struct zfcp_qdio_queue *req_queue;
122         int first, count;
123
124         if (!atomic_test_mask(ZFCP_STATUS_ADAPTER_QDIOUP, &adapter->status))
125                 return;
126
127         /* clear QDIOUP flag, thus do_QDIO is not called during qdio_shutdown */
128         req_queue = &adapter->request_queue;
129         write_lock_irq(&req_queue->queue_lock);
130         atomic_clear_mask(ZFCP_STATUS_ADAPTER_QDIOUP, &adapter->status);
131         write_unlock_irq(&req_queue->queue_lock);
132
133         while (qdio_shutdown(adapter->ccw_device,
134                              QDIO_FLAG_CLEANUP_USING_CLEAR) == -EINPROGRESS)
135                 ssleep(1);
136
137         /* cleanup used outbound sbals */
138         count = atomic_read(&req_queue->free_count);
139         if (count < QDIO_MAX_BUFFERS_PER_Q) {
140                 first = (req_queue->free_index+count) % QDIO_MAX_BUFFERS_PER_Q;
141                 count = QDIO_MAX_BUFFERS_PER_Q - count;
142                 zfcp_qdio_zero_sbals(req_queue->buffer, first, count);
143         }
144         req_queue->free_index = 0;
145         atomic_set(&req_queue->free_count, 0);
146         req_queue->distance_from_int = 0;
147         adapter->response_queue.free_index = 0;
148         atomic_set(&adapter->response_queue.free_count, 0);
149 }
150
151 /**
152  * zfcp_close_fsf - stop FSF operations for an adapter
153  *
154  * Dismiss and cleanup all pending fsf_reqs (this wakes up all initiators of
155  * requests waiting for completion; especially this returns SCSI commands
156  * with error state).
157  */
158 static void zfcp_close_fsf(struct zfcp_adapter *adapter)
159 {
160         /* close queues to ensure that buffers are not accessed by adapter */
161         zfcp_close_qdio(adapter);
162         zfcp_fsf_req_dismiss_all(adapter);
163         /* reset FSF request sequence number */
164         adapter->fsf_req_seq_no = 0;
165         /* all ports and units are closed */
166         zfcp_erp_modify_adapter_status(adapter, 24, NULL,
167                                        ZFCP_STATUS_COMMON_OPEN, ZFCP_CLEAR);
168 }
169
170 /**
171  * zfcp_fsf_request_timeout_handler - called if a request timed out
172  * @data: pointer to adapter for handler function
173  *
174  * This function needs to be called if requests (ELS, Generic Service,
175  * or SCSI commands) exceed a certain time limit. The assumption is
176  * that after the time limit the adapter get stuck. So we trigger a reopen of
177  * the adapter.
178  */
179 static void zfcp_fsf_request_timeout_handler(unsigned long data)
180 {
181         struct zfcp_adapter *adapter = (struct zfcp_adapter *) data;
182         zfcp_erp_adapter_reopen(adapter, ZFCP_STATUS_COMMON_ERP_FAILED, 62,
183                                 NULL);
184 }
185
186 void zfcp_fsf_start_timer(struct zfcp_fsf_req *fsf_req, unsigned long timeout)
187 {
188         fsf_req->timer.function = zfcp_fsf_request_timeout_handler;
189         fsf_req->timer.data = (unsigned long) fsf_req->adapter;
190         fsf_req->timer.expires = jiffies + timeout;
191         add_timer(&fsf_req->timer);
192 }
193
194 /*
195  * function:
196  *
197  * purpose:     called if an adapter failed,
198  *              initiates adapter recovery which is done
199  *              asynchronously
200  *
201  * returns:     0       - initiated action successfully
202  *              <0      - failed to initiate action
203  */
204 static int zfcp_erp_adapter_reopen_internal(struct zfcp_adapter *adapter,
205                                             int clear_mask, u8 id, void *ref)
206 {
207         int retval;
208
209         ZFCP_LOG_DEBUG("reopen adapter %s\n",
210                        zfcp_get_busid_by_adapter(adapter));
211
212         zfcp_erp_adapter_block(adapter, clear_mask);
213
214         if (atomic_test_mask(ZFCP_STATUS_COMMON_ERP_FAILED, &adapter->status)) {
215                 ZFCP_LOG_DEBUG("skipped reopen of failed adapter %s\n",
216                                zfcp_get_busid_by_adapter(adapter));
217                 /* ensure propagation of failed status to new devices */
218                 zfcp_erp_adapter_failed(adapter, 13, NULL);
219                 retval = -EIO;
220                 goto out;
221         }
222         retval = zfcp_erp_action_enqueue(ZFCP_ERP_ACTION_REOPEN_ADAPTER,
223                                          adapter, NULL, NULL, id, ref);
224
225  out:
226         return retval;
227 }
228
229 /*
230  * function:
231  *
232  * purpose:     Wrappper for zfcp_erp_adapter_reopen_internal
233  *              used to ensure the correct locking
234  *
235  * returns:     0       - initiated action successfully
236  *              <0      - failed to initiate action
237  */
238 int zfcp_erp_adapter_reopen(struct zfcp_adapter *adapter, int clear_mask,
239                             u8 id, void *ref)
240 {
241         int retval;
242         unsigned long flags;
243
244         read_lock_irqsave(&zfcp_data.config_lock, flags);
245         write_lock(&adapter->erp_lock);
246         retval = zfcp_erp_adapter_reopen_internal(adapter, clear_mask, id, ref);
247         write_unlock(&adapter->erp_lock);
248         read_unlock_irqrestore(&zfcp_data.config_lock, flags);
249
250         return retval;
251 }
252
253 int zfcp_erp_adapter_shutdown(struct zfcp_adapter *adapter, int clear_mask,
254                               u8 id, void *ref)
255 {
256         int retval;
257
258         retval = zfcp_erp_adapter_reopen(adapter,
259                                          ZFCP_STATUS_COMMON_RUNNING |
260                                          ZFCP_STATUS_COMMON_ERP_FAILED |
261                                          clear_mask, id, ref);
262
263         return retval;
264 }
265
266 int zfcp_erp_port_shutdown(struct zfcp_port *port, int clear_mask, u8 id,
267                            void *ref)
268 {
269         int retval;
270
271         retval = zfcp_erp_port_reopen(port,
272                                       ZFCP_STATUS_COMMON_RUNNING |
273                                       ZFCP_STATUS_COMMON_ERP_FAILED |
274                                       clear_mask, id, ref);
275
276         return retval;
277 }
278
279 int zfcp_erp_unit_shutdown(struct zfcp_unit *unit, int clear_mask, u8 id,
280                            void *ref)
281 {
282         int retval;
283
284         retval = zfcp_erp_unit_reopen(unit,
285                                       ZFCP_STATUS_COMMON_RUNNING |
286                                       ZFCP_STATUS_COMMON_ERP_FAILED |
287                                       clear_mask, id, ref);
288
289         return retval;
290 }
291
292 /*
293  * function:
294  *
295  * purpose:     called if a port failed to be opened normally
296  *              initiates Forced Reopen recovery which is done
297  *              asynchronously
298  *
299  * returns:     0       - initiated action successfully
300  *              <0      - failed to initiate action
301  */
302 static int zfcp_erp_port_forced_reopen_internal(struct zfcp_port *port,
303                                                 int clear_mask, u8 id,
304                                                 void *ref)
305 {
306         int retval;
307
308         ZFCP_LOG_DEBUG("forced reopen of port 0x%016Lx on adapter %s\n",
309                        port->wwpn, zfcp_get_busid_by_port(port));
310
311         zfcp_erp_port_block(port, clear_mask);
312
313         if (atomic_test_mask(ZFCP_STATUS_COMMON_ERP_FAILED, &port->status)) {
314                 ZFCP_LOG_DEBUG("skipped forced reopen of failed port 0x%016Lx "
315                                "on adapter %s\n", port->wwpn,
316                                zfcp_get_busid_by_port(port));
317                 retval = -EIO;
318                 goto out;
319         }
320
321         retval = zfcp_erp_action_enqueue(ZFCP_ERP_ACTION_REOPEN_PORT_FORCED,
322                                          port->adapter, port, NULL, id, ref);
323
324  out:
325         return retval;
326 }
327
328 /*
329  * function:
330  *
331  * purpose:     Wrappper for zfcp_erp_port_forced_reopen_internal
332  *              used to ensure the correct locking
333  *
334  * returns:     0       - initiated action successfully
335  *              <0      - failed to initiate action
336  */
337 int zfcp_erp_port_forced_reopen(struct zfcp_port *port, int clear_mask, u8 id,
338                                 void *ref)
339 {
340         int retval;
341         unsigned long flags;
342         struct zfcp_adapter *adapter;
343
344         adapter = port->adapter;
345         read_lock_irqsave(&zfcp_data.config_lock, flags);
346         write_lock(&adapter->erp_lock);
347         retval = zfcp_erp_port_forced_reopen_internal(port, clear_mask, id,
348                                                       ref);
349         write_unlock(&adapter->erp_lock);
350         read_unlock_irqrestore(&zfcp_data.config_lock, flags);
351
352         return retval;
353 }
354
355 /*
356  * function:
357  *
358  * purpose:     called if a port is to be opened
359  *              initiates Reopen recovery which is done
360  *              asynchronously
361  *
362  * returns:     0       - initiated action successfully
363  *              <0      - failed to initiate action
364  */
365 static int zfcp_erp_port_reopen_internal(struct zfcp_port *port, int clear_mask,
366                                          u8 id, void *ref)
367 {
368         int retval;
369
370         ZFCP_LOG_DEBUG("reopen of port 0x%016Lx on adapter %s\n",
371                        port->wwpn, zfcp_get_busid_by_port(port));
372
373         zfcp_erp_port_block(port, clear_mask);
374
375         if (atomic_test_mask(ZFCP_STATUS_COMMON_ERP_FAILED, &port->status)) {
376                 ZFCP_LOG_DEBUG("skipped reopen of failed port 0x%016Lx "
377                                "on adapter %s\n", port->wwpn,
378                                zfcp_get_busid_by_port(port));
379                 /* ensure propagation of failed status to new devices */
380                 zfcp_erp_port_failed(port, 14, NULL);
381                 retval = -EIO;
382                 goto out;
383         }
384
385         retval = zfcp_erp_action_enqueue(ZFCP_ERP_ACTION_REOPEN_PORT,
386                                          port->adapter, port, NULL, id, ref);
387
388  out:
389         return retval;
390 }
391
392 /**
393  * zfcp_erp_port_reopen - initiate reopen of a remote port
394  * @port: port to be reopened
395  * @clear_mask: specifies flags in port status to be cleared
396  * Return: 0 on success, < 0 on error
397  *
398  * This is a wrappper function for zfcp_erp_port_reopen_internal. It ensures
399  * correct locking. An error recovery task is initiated to do the reopen.
400  * To wait for the completion of the reopen zfcp_erp_wait should be used.
401  */
402 int zfcp_erp_port_reopen(struct zfcp_port *port, int clear_mask, u8 id,
403                          void *ref)
404 {
405         int retval;
406         unsigned long flags;
407         struct zfcp_adapter *adapter = port->adapter;
408
409         read_lock_irqsave(&zfcp_data.config_lock, flags);
410         write_lock(&adapter->erp_lock);
411         retval = zfcp_erp_port_reopen_internal(port, clear_mask, id, ref);
412         write_unlock(&adapter->erp_lock);
413         read_unlock_irqrestore(&zfcp_data.config_lock, flags);
414
415         return retval;
416 }
417
418 /*
419  * function:
420  *
421  * purpose:     called if a unit is to be opened
422  *              initiates Reopen recovery which is done
423  *              asynchronously
424  *
425  * returns:     0       - initiated action successfully
426  *              <0      - failed to initiate action
427  */
428 static int zfcp_erp_unit_reopen_internal(struct zfcp_unit *unit, int clear_mask,
429                                          u8 id, void *ref)
430 {
431         int retval;
432         struct zfcp_adapter *adapter = unit->port->adapter;
433
434         ZFCP_LOG_DEBUG("reopen of unit 0x%016Lx on port 0x%016Lx "
435                        "on adapter %s\n", unit->fcp_lun,
436                        unit->port->wwpn, zfcp_get_busid_by_unit(unit));
437
438         zfcp_erp_unit_block(unit, clear_mask);
439
440         if (atomic_test_mask(ZFCP_STATUS_COMMON_ERP_FAILED, &unit->status)) {
441                 ZFCP_LOG_DEBUG("skipped reopen of failed unit 0x%016Lx "
442                                "on port 0x%016Lx on adapter %s\n",
443                                unit->fcp_lun, unit->port->wwpn,
444                                zfcp_get_busid_by_unit(unit));
445                 retval = -EIO;
446                 goto out;
447         }
448
449         retval = zfcp_erp_action_enqueue(ZFCP_ERP_ACTION_REOPEN_UNIT,
450                                          adapter, unit->port, unit, id, ref);
451  out:
452         return retval;
453 }
454
455 /**
456  * zfcp_erp_unit_reopen - initiate reopen of a unit
457  * @unit: unit to be reopened
458  * @clear_mask: specifies flags in unit status to be cleared
459  * Return: 0 on success, < 0 on error
460  *
461  * This is a wrappper for zfcp_erp_unit_reopen_internal. It ensures correct
462  * locking. An error recovery task is initiated to do the reopen.
463  * To wait for the completion of the reopen zfcp_erp_wait should be used.
464  */
465 int zfcp_erp_unit_reopen(struct zfcp_unit *unit, int clear_mask, u8 id,
466                          void *ref)
467 {
468         int retval;
469         unsigned long flags;
470         struct zfcp_adapter *adapter;
471         struct zfcp_port *port;
472
473         port = unit->port;
474         adapter = port->adapter;
475
476         read_lock_irqsave(&zfcp_data.config_lock, flags);
477         write_lock(&adapter->erp_lock);
478         retval = zfcp_erp_unit_reopen_internal(unit, clear_mask, id, ref);
479         write_unlock(&adapter->erp_lock);
480         read_unlock_irqrestore(&zfcp_data.config_lock, flags);
481
482         return retval;
483 }
484
485 /**
486  * zfcp_erp_adapter_block - mark adapter as blocked, block scsi requests
487  */
488 static void zfcp_erp_adapter_block(struct zfcp_adapter *adapter, int clear_mask)
489 {
490         zfcp_erp_modify_adapter_status(adapter, 15, NULL,
491                                        ZFCP_STATUS_COMMON_UNBLOCKED |
492                                        clear_mask, ZFCP_CLEAR);
493 }
494
495 /* FIXME: isn't really atomic */
496 /*
497  * returns the mask which has not been set so far, i.e.
498  * 0 if no bit has been changed, !0 if some bit has been changed
499  */
500 static int atomic_test_and_set_mask(unsigned long mask, atomic_t *v)
501 {
502         int changed_bits = (atomic_read(v) /*XOR*/^ mask) & mask;
503         atomic_set_mask(mask, v);
504         return changed_bits;
505 }
506
507 /* FIXME: isn't really atomic */
508 /*
509  * returns the mask which has not been cleared so far, i.e.
510  * 0 if no bit has been changed, !0 if some bit has been changed
511  */
512 static int atomic_test_and_clear_mask(unsigned long mask, atomic_t *v)
513 {
514         int changed_bits = atomic_read(v) & mask;
515         atomic_clear_mask(mask, v);
516         return changed_bits;
517 }
518
519 /**
520  * zfcp_erp_adapter_unblock - mark adapter as unblocked, allow scsi requests
521  */
522 static void zfcp_erp_adapter_unblock(struct zfcp_adapter *adapter)
523 {
524         if (atomic_test_and_set_mask(ZFCP_STATUS_COMMON_UNBLOCKED,
525                                      &adapter->status))
526                 zfcp_rec_dbf_event_adapter(16, NULL, adapter);
527 }
528
529 /*
530  * function:
531  *
532  * purpose:     disable I/O,
533  *              return any open requests and clean them up,
534  *              aim: no pending and incoming I/O
535  *
536  * returns:
537  */
538 static void
539 zfcp_erp_port_block(struct zfcp_port *port, int clear_mask)
540 {
541         zfcp_erp_modify_port_status(port, 17, NULL,
542                                     ZFCP_STATUS_COMMON_UNBLOCKED | clear_mask,
543                                     ZFCP_CLEAR);
544 }
545
546 /*
547  * function:
548  *
549  * purpose:     enable I/O
550  *
551  * returns:
552  */
553 static void
554 zfcp_erp_port_unblock(struct zfcp_port *port)
555 {
556         if (atomic_test_and_set_mask(ZFCP_STATUS_COMMON_UNBLOCKED,
557                                      &port->status))
558                 zfcp_rec_dbf_event_port(18, NULL, port);
559 }
560
561 /*
562  * function:
563  *
564  * purpose:     disable I/O,
565  *              return any open requests and clean them up,
566  *              aim: no pending and incoming I/O
567  *
568  * returns:
569  */
570 static void
571 zfcp_erp_unit_block(struct zfcp_unit *unit, int clear_mask)
572 {
573         zfcp_erp_modify_unit_status(unit, 19, NULL,
574                                     ZFCP_STATUS_COMMON_UNBLOCKED | clear_mask,
575                                     ZFCP_CLEAR);
576 }
577
578 /*
579  * function:
580  *
581  * purpose:     enable I/O
582  *
583  * returns:
584  */
585 static void
586 zfcp_erp_unit_unblock(struct zfcp_unit *unit)
587 {
588         if (atomic_test_and_set_mask(ZFCP_STATUS_COMMON_UNBLOCKED,
589                                      &unit->status))
590                 zfcp_rec_dbf_event_unit(20, NULL, unit);
591 }
592
593 static void
594 zfcp_erp_action_ready(struct zfcp_erp_action *erp_action)
595 {
596         struct zfcp_adapter *adapter = erp_action->adapter;
597
598         zfcp_erp_action_to_ready(erp_action);
599         up(&adapter->erp_ready_sem);
600         zfcp_rec_dbf_event_thread(2, adapter);
601 }
602
603 /*
604  * function:
605  *
606  * purpose:
607  *
608  * returns:     <0                      erp_action not found in any list
609  *              ZFCP_ERP_ACTION_READY   erp_action is in ready list
610  *              ZFCP_ERP_ACTION_RUNNING erp_action is in running list
611  *
612  * locks:       erp_lock must be held
613  */
614 static int
615 zfcp_erp_action_exists(struct zfcp_erp_action *erp_action)
616 {
617         int retval = -EINVAL;
618         struct list_head *entry;
619         struct zfcp_erp_action *entry_erp_action;
620         struct zfcp_adapter *adapter = erp_action->adapter;
621
622         /* search in running list */
623         list_for_each(entry, &adapter->erp_running_head) {
624                 entry_erp_action =
625                     list_entry(entry, struct zfcp_erp_action, list);
626                 if (entry_erp_action == erp_action) {
627                         retval = ZFCP_ERP_ACTION_RUNNING;
628                         goto out;
629                 }
630         }
631         /* search in ready list */
632         list_for_each(entry, &adapter->erp_ready_head) {
633                 entry_erp_action =
634                     list_entry(entry, struct zfcp_erp_action, list);
635                 if (entry_erp_action == erp_action) {
636                         retval = ZFCP_ERP_ACTION_READY;
637                         goto out;
638                 }
639         }
640
641  out:
642         return retval;
643 }
644
645 /*
646  * purpose:     checks current status of action (timed out, dismissed, ...)
647  *              and does appropriate preparations (dismiss fsf request, ...)
648  *
649  * locks:       called under erp_lock (disabled interrupts)
650  */
651 static void
652 zfcp_erp_strategy_check_fsfreq(struct zfcp_erp_action *erp_action)
653 {
654         struct zfcp_adapter *adapter = erp_action->adapter;
655
656         if (erp_action->fsf_req) {
657                 /* take lock to ensure that request is not deleted meanwhile */
658                 spin_lock(&adapter->req_list_lock);
659                 if (zfcp_reqlist_find_safe(adapter, erp_action->fsf_req) &&
660                     erp_action->fsf_req->erp_action == erp_action) {
661                         /* fsf_req still exists */
662                         /* dismiss fsf_req of timed out/dismissed erp_action */
663                         if (erp_action->status & (ZFCP_STATUS_ERP_DISMISSED |
664                                                   ZFCP_STATUS_ERP_TIMEDOUT)) {
665                                 erp_action->fsf_req->status |=
666                                         ZFCP_STATUS_FSFREQ_DISMISSED;
667                                 zfcp_rec_dbf_event_action(142, erp_action);
668                         }
669                         if (erp_action->status & ZFCP_STATUS_ERP_TIMEDOUT) {
670                                 zfcp_rec_dbf_event_action(143, erp_action);
671                                 ZFCP_LOG_NORMAL("error: erp step timed out "
672                                                 "(action=%d, fsf_req=%p)\n ",
673                                                 erp_action->action,
674                                                 erp_action->fsf_req);
675                         }
676                         /*
677                          * If fsf_req is neither dismissed nor completed
678                          * then keep it running asynchronously and don't mess
679                          * with the association of erp_action and fsf_req.
680                          */
681                         if (erp_action->fsf_req->status &
682                                         (ZFCP_STATUS_FSFREQ_COMPLETED |
683                                                ZFCP_STATUS_FSFREQ_DISMISSED)) {
684                                 /* forget about association between fsf_req
685                                    and erp_action */
686                                 erp_action->fsf_req = NULL;
687                         }
688                 } else {
689                         /*
690                          * even if this fsf_req has gone, forget about
691                          * association between erp_action and fsf_req
692                          */
693                         erp_action->fsf_req = NULL;
694                 }
695                 spin_unlock(&adapter->req_list_lock);
696         }
697 }
698
699 /**
700  * zfcp_erp_async_handler_nolock - complete erp_action
701  *
702  * Used for normal completion, time-out, dismissal and failure after
703  * low memory condition.
704  */
705 static void zfcp_erp_async_handler_nolock(struct zfcp_erp_action *erp_action,
706                                           unsigned long set_mask)
707 {
708         if (zfcp_erp_action_exists(erp_action) == ZFCP_ERP_ACTION_RUNNING) {
709                 erp_action->status |= set_mask;
710                 zfcp_erp_action_ready(erp_action);
711         } else {
712                 /* action is ready or gone - nothing to do */
713         }
714 }
715
716 /**
717  * zfcp_erp_async_handler - wrapper for erp_async_handler_nolock w/ locking
718  */
719 void zfcp_erp_async_handler(struct zfcp_erp_action *erp_action,
720                             unsigned long set_mask)
721 {
722         struct zfcp_adapter *adapter = erp_action->adapter;
723         unsigned long flags;
724
725         write_lock_irqsave(&adapter->erp_lock, flags);
726         zfcp_erp_async_handler_nolock(erp_action, set_mask);
727         write_unlock_irqrestore(&adapter->erp_lock, flags);
728 }
729
730 /*
731  * purpose:     is called for erp_action which was slept waiting for
732  *              memory becoming avaliable,
733  *              will trigger that this action will be continued
734  */
735 static void
736 zfcp_erp_memwait_handler(unsigned long data)
737 {
738         struct zfcp_erp_action *erp_action = (struct zfcp_erp_action *) data;
739
740         zfcp_erp_async_handler(erp_action, 0);
741 }
742
743 /*
744  * purpose:     is called if an asynchronous erp step timed out,
745  *              action gets an appropriate flag and will be processed
746  *              accordingly
747  */
748 static void zfcp_erp_timeout_handler(unsigned long data)
749 {
750         struct zfcp_erp_action *erp_action = (struct zfcp_erp_action *) data;
751
752         zfcp_erp_async_handler(erp_action, ZFCP_STATUS_ERP_TIMEDOUT);
753 }
754
755 /**
756  * zfcp_erp_action_dismiss - dismiss an erp_action
757  *
758  * adapter->erp_lock must be held
759  *
760  * Dismissal of an erp_action is usually required if an erp_action of
761  * higher priority is generated.
762  */
763 static void zfcp_erp_action_dismiss(struct zfcp_erp_action *erp_action)
764 {
765         erp_action->status |= ZFCP_STATUS_ERP_DISMISSED;
766         if (zfcp_erp_action_exists(erp_action) == ZFCP_ERP_ACTION_RUNNING)
767                 zfcp_erp_action_ready(erp_action);
768 }
769
770 int
771 zfcp_erp_thread_setup(struct zfcp_adapter *adapter)
772 {
773         int retval = 0;
774
775         atomic_clear_mask(ZFCP_STATUS_ADAPTER_ERP_THREAD_UP, &adapter->status);
776
777         retval = kernel_thread(zfcp_erp_thread, adapter, SIGCHLD);
778         if (retval < 0) {
779                 ZFCP_LOG_NORMAL("error: creation of erp thread failed for "
780                                 "adapter %s\n",
781                                 zfcp_get_busid_by_adapter(adapter));
782         } else {
783                 wait_event(adapter->erp_thread_wqh,
784                            atomic_test_mask(ZFCP_STATUS_ADAPTER_ERP_THREAD_UP,
785                                             &adapter->status));
786         }
787
788         return (retval < 0);
789 }
790
791 /*
792  * function:
793  *
794  * purpose:
795  *
796  * returns:
797  *
798  * context:     process (i.e. proc-fs or rmmod/insmod)
799  *
800  * note:        The caller of this routine ensures that the specified
801  *              adapter has been shut down and that this operation
802  *              has been completed. Thus, there are no pending erp_actions
803  *              which would need to be handled here.
804  */
805 int
806 zfcp_erp_thread_kill(struct zfcp_adapter *adapter)
807 {
808         int retval = 0;
809
810         atomic_set_mask(ZFCP_STATUS_ADAPTER_ERP_THREAD_KILL, &adapter->status);
811         up(&adapter->erp_ready_sem);
812         zfcp_rec_dbf_event_thread_lock(2, adapter);
813
814         wait_event(adapter->erp_thread_wqh,
815                    !atomic_test_mask(ZFCP_STATUS_ADAPTER_ERP_THREAD_UP,
816                                      &adapter->status));
817
818         atomic_clear_mask(ZFCP_STATUS_ADAPTER_ERP_THREAD_KILL,
819                           &adapter->status);
820
821         return retval;
822 }
823
824 /*
825  * purpose:     is run as a kernel thread,
826  *              goes through list of error recovery actions of associated adapter
827  *              and delegates single action to execution
828  *
829  * returns:     0
830  */
831 static int
832 zfcp_erp_thread(void *data)
833 {
834         struct zfcp_adapter *adapter = (struct zfcp_adapter *) data;
835         struct list_head *next;
836         struct zfcp_erp_action *erp_action;
837         unsigned long flags;
838
839         daemonize("zfcperp%s", zfcp_get_busid_by_adapter(adapter));
840         /* Block all signals */
841         siginitsetinv(&current->blocked, 0);
842         atomic_set_mask(ZFCP_STATUS_ADAPTER_ERP_THREAD_UP, &adapter->status);
843         wake_up(&adapter->erp_thread_wqh);
844
845         while (!atomic_test_mask(ZFCP_STATUS_ADAPTER_ERP_THREAD_KILL,
846                                  &adapter->status)) {
847
848                 write_lock_irqsave(&adapter->erp_lock, flags);
849                 next = adapter->erp_ready_head.next;
850                 write_unlock_irqrestore(&adapter->erp_lock, flags);
851
852                 if (next != &adapter->erp_ready_head) {
853                         erp_action =
854                             list_entry(next, struct zfcp_erp_action, list);
855                         /*
856                          * process action (incl. [re]moving it
857                          * from 'ready' queue)
858                          */
859                         zfcp_erp_strategy(erp_action);
860                 }
861
862                 /*
863                  * sleep as long as there is nothing to do, i.e.
864                  * no action in 'ready' queue to be processed and
865                  * thread is not to be killed
866                  */
867                 zfcp_rec_dbf_event_thread_lock(4, adapter);
868                 down_interruptible(&adapter->erp_ready_sem);
869                 zfcp_rec_dbf_event_thread_lock(5, adapter);
870         }
871
872         atomic_clear_mask(ZFCP_STATUS_ADAPTER_ERP_THREAD_UP, &adapter->status);
873         wake_up(&adapter->erp_thread_wqh);
874
875         return 0;
876 }
877
878 /*
879  * function:
880  *
881  * purpose:     drives single error recovery action and schedules higher and
882  *              subordinate actions, if necessary
883  *
884  * returns:     ZFCP_ERP_CONTINUES      - action continues (asynchronously)
885  *              ZFCP_ERP_SUCCEEDED      - action finished successfully (deqd)
886  *              ZFCP_ERP_FAILED         - action finished unsuccessfully (deqd)
887  *              ZFCP_ERP_EXIT           - action finished (dequeued), offline
888  *              ZFCP_ERP_DISMISSED      - action canceled (dequeued)
889  */
890 static int
891 zfcp_erp_strategy(struct zfcp_erp_action *erp_action)
892 {
893         int retval = 0;
894         struct zfcp_adapter *adapter = erp_action->adapter;
895         struct zfcp_port *port = erp_action->port;
896         struct zfcp_unit *unit = erp_action->unit;
897         int action = erp_action->action;
898         u32 status = erp_action->status;
899         unsigned long flags;
900
901         /* serialise dismissing, timing out, moving, enqueueing */
902         read_lock_irqsave(&zfcp_data.config_lock, flags);
903         write_lock(&adapter->erp_lock);
904
905         /* dequeue dismissed action and leave, if required */
906         retval = zfcp_erp_strategy_check_action(erp_action, retval);
907         if (retval == ZFCP_ERP_DISMISSED) {
908                 goto unlock;
909         }
910
911         /*
912          * move action to 'running' queue before processing it
913          * (to avoid a race condition regarding moving the
914          * action to the 'running' queue and back)
915          */
916         zfcp_erp_action_to_running(erp_action);
917
918         /*
919          * try to process action as far as possible,
920          * no lock to allow for blocking operations (kmalloc, qdio, ...),
921          * afterwards the lock is required again for the following reasons:
922          * - dequeueing of finished action and enqueueing of
923          *   follow-up actions must be atomic so that any other
924          *   reopen-routine does not believe there is nothing to do
925          *   and that it is safe to enqueue something else,
926          * - we want to force any control thread which is dismissing
927          *   actions to finish this before we decide about
928          *   necessary steps to be taken here further
929          */
930         write_unlock(&adapter->erp_lock);
931         read_unlock_irqrestore(&zfcp_data.config_lock, flags);
932         retval = zfcp_erp_strategy_do_action(erp_action);
933         read_lock_irqsave(&zfcp_data.config_lock, flags);
934         write_lock(&adapter->erp_lock);
935
936         /*
937          * check for dismissed status again to avoid follow-up actions,
938          * failing of targets and so on for dismissed actions,
939          * we go through down() here because there has been an up()
940          */
941         if (erp_action->status & ZFCP_STATUS_ERP_DISMISSED)
942                 retval = ZFCP_ERP_CONTINUES;
943
944         switch (retval) {
945         case ZFCP_ERP_NOMEM:
946                 /* no memory to continue immediately, let it sleep */
947                 if (!(erp_action->status & ZFCP_STATUS_ERP_LOWMEM)) {
948                         ++adapter->erp_low_mem_count;
949                         erp_action->status |= ZFCP_STATUS_ERP_LOWMEM;
950                 }
951                 /* This condition is true if there is no memory available
952                    for any erp_action on this adapter. This implies that there
953                    are no elements in the memory pool(s) left for erp_actions.
954                    This might happen if an erp_action that used a memory pool
955                    element was timed out.
956                  */
957                 if (adapter->erp_total_count == adapter->erp_low_mem_count) {
958                         ZFCP_LOG_NORMAL("error: no mempool elements available, "
959                                         "restarting I/O on adapter %s "
960                                         "to free mempool\n",
961                                         zfcp_get_busid_by_adapter(adapter));
962                         zfcp_erp_adapter_reopen_internal(adapter, 0, 66, NULL);
963                 } else {
964                 retval = zfcp_erp_strategy_memwait(erp_action);
965                 }
966                 goto unlock;
967         case ZFCP_ERP_CONTINUES:
968                 /* leave since this action runs asynchronously */
969                 if (erp_action->status & ZFCP_STATUS_ERP_LOWMEM) {
970                         --adapter->erp_low_mem_count;
971                         erp_action->status &= ~ZFCP_STATUS_ERP_LOWMEM;
972                 }
973                 goto unlock;
974         }
975         /* ok, finished action (whatever its result is) */
976
977         /* check for unrecoverable targets */
978         retval = zfcp_erp_strategy_check_target(erp_action, retval);
979
980         /* action must be dequeued (here to allow for further ones) */
981         zfcp_erp_action_dequeue(erp_action);
982
983         /*
984          * put this target through the erp mill again if someone has
985          * requested to change the status of a target being online
986          * to offline or the other way around
987          * (old retval is preserved if nothing has to be done here)
988          */
989         retval = zfcp_erp_strategy_statechange(action, status, adapter,
990                                                port, unit, retval);
991
992         /*
993          * leave if target is in permanent error state or if
994          * action is repeated in order to process state change
995          */
996         if (retval == ZFCP_ERP_EXIT) {
997                 goto unlock;
998         }
999
1000         /* trigger follow up actions */
1001         zfcp_erp_strategy_followup_actions(action, adapter, port, unit, retval);
1002
1003  unlock:
1004         write_unlock(&adapter->erp_lock);
1005         read_unlock_irqrestore(&zfcp_data.config_lock, flags);
1006
1007         if (retval != ZFCP_ERP_CONTINUES)
1008                 zfcp_erp_action_cleanup(action, adapter, port, unit, retval);
1009
1010         /*
1011          * a few tasks remain when the erp queues are empty
1012          * (don't do that if the last action evaluated was dismissed
1013          * since this clearly indicates that there is more to come) :
1014          * - close the name server port if it is open yet
1015          *   (enqueues another [probably] final action)
1016          * - otherwise, wake up whoever wants to be woken when we are
1017          *   done with erp
1018          */
1019         if (retval != ZFCP_ERP_DISMISSED)
1020                 zfcp_erp_strategy_check_queues(adapter);
1021
1022         return retval;
1023 }
1024
1025 /*
1026  * function:
1027  *
1028  * purpose:
1029  *
1030  * returns:     ZFCP_ERP_DISMISSED      - if action has been dismissed
1031  *              retval                  - otherwise
1032  */
1033 static int
1034 zfcp_erp_strategy_check_action(struct zfcp_erp_action *erp_action, int retval)
1035 {
1036         zfcp_erp_strategy_check_fsfreq(erp_action);
1037
1038         if (erp_action->status & ZFCP_STATUS_ERP_DISMISSED) {
1039                 zfcp_erp_action_dequeue(erp_action);
1040                 retval = ZFCP_ERP_DISMISSED;
1041         }
1042
1043         return retval;
1044 }
1045
1046 static int
1047 zfcp_erp_strategy_do_action(struct zfcp_erp_action *erp_action)
1048 {
1049         int retval = ZFCP_ERP_FAILED;
1050
1051         /*
1052          * try to execute/continue action as far as possible,
1053          * note: no lock in subsequent strategy routines
1054          * (this allows these routine to call schedule, e.g.
1055          * kmalloc with such flags or qdio_initialize & friends)
1056          * Note: in case of timeout, the separate strategies will fail
1057          * anyhow. No need for a special action. Even worse, a nameserver
1058          * failure would not wake up waiting ports without the call.
1059          */
1060         switch (erp_action->action) {
1061
1062         case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
1063                 retval = zfcp_erp_adapter_strategy(erp_action);
1064                 break;
1065
1066         case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
1067                 retval = zfcp_erp_port_forced_strategy(erp_action);
1068                 break;
1069
1070         case ZFCP_ERP_ACTION_REOPEN_PORT:
1071                 retval = zfcp_erp_port_strategy(erp_action);
1072                 break;
1073
1074         case ZFCP_ERP_ACTION_REOPEN_UNIT:
1075                 retval = zfcp_erp_unit_strategy(erp_action);
1076                 break;
1077
1078         default:
1079                 ZFCP_LOG_NORMAL("bug: unknown erp action requested on "
1080                                 "adapter %s (action=%d)\n",
1081                                 zfcp_get_busid_by_adapter(erp_action->adapter),
1082                                 erp_action->action);
1083         }
1084
1085         return retval;
1086 }
1087
1088 /*
1089  * function:
1090  *
1091  * purpose:     triggers retry of this action after a certain amount of time
1092  *              by means of timer provided by erp_action
1093  *
1094  * returns:     ZFCP_ERP_CONTINUES - erp_action sleeps in erp running queue
1095  */
1096 static int
1097 zfcp_erp_strategy_memwait(struct zfcp_erp_action *erp_action)
1098 {
1099         int retval = ZFCP_ERP_CONTINUES;
1100
1101         init_timer(&erp_action->timer);
1102         erp_action->timer.function = zfcp_erp_memwait_handler;
1103         erp_action->timer.data = (unsigned long) erp_action;
1104         erp_action->timer.expires = jiffies + ZFCP_ERP_MEMWAIT_TIMEOUT;
1105         add_timer(&erp_action->timer);
1106
1107         return retval;
1108 }
1109
1110 /*
1111  * function:    zfcp_erp_adapter_failed
1112  *
1113  * purpose:     sets the adapter and all underlying devices to ERP_FAILED
1114  *
1115  */
1116 void
1117 zfcp_erp_adapter_failed(struct zfcp_adapter *adapter, u8 id, void *ref)
1118 {
1119         zfcp_erp_modify_adapter_status(adapter, id, ref,
1120                                        ZFCP_STATUS_COMMON_ERP_FAILED, ZFCP_SET);
1121         ZFCP_LOG_NORMAL("adapter erp failed on adapter %s\n",
1122                         zfcp_get_busid_by_adapter(adapter));
1123 }
1124
1125 /*
1126  * function:    zfcp_erp_port_failed
1127  *
1128  * purpose:     sets the port and all underlying devices to ERP_FAILED
1129  *
1130  */
1131 void
1132 zfcp_erp_port_failed(struct zfcp_port *port, u8 id, void *ref)
1133 {
1134         zfcp_erp_modify_port_status(port, id, ref,
1135                                     ZFCP_STATUS_COMMON_ERP_FAILED, ZFCP_SET);
1136
1137         if (atomic_test_mask(ZFCP_STATUS_PORT_WKA, &port->status))
1138                 ZFCP_LOG_NORMAL("port erp failed (adapter %s, "
1139                                 "port d_id=0x%06x)\n",
1140                                 zfcp_get_busid_by_port(port), port->d_id);
1141         else
1142                 ZFCP_LOG_NORMAL("port erp failed (adapter %s, wwpn=0x%016Lx)\n",
1143                                 zfcp_get_busid_by_port(port), port->wwpn);
1144 }
1145
1146 /*
1147  * function:    zfcp_erp_unit_failed
1148  *
1149  * purpose:     sets the unit to ERP_FAILED
1150  *
1151  */
1152 void
1153 zfcp_erp_unit_failed(struct zfcp_unit *unit, u8 id, void *ref)
1154 {
1155         zfcp_erp_modify_unit_status(unit, id, ref,
1156                                     ZFCP_STATUS_COMMON_ERP_FAILED, ZFCP_SET);
1157
1158         ZFCP_LOG_NORMAL("unit erp failed on unit 0x%016Lx on port 0x%016Lx "
1159                         " on adapter %s\n", unit->fcp_lun,
1160                         unit->port->wwpn, zfcp_get_busid_by_unit(unit));
1161 }
1162
1163 /*
1164  * function:    zfcp_erp_strategy_check_target
1165  *
1166  * purpose:     increments the erp action count on the device currently in
1167  *              recovery if the action failed or resets the count in case of
1168  *              success. If a maximum count is exceeded the device is marked
1169  *              as ERP_FAILED.
1170  *              The 'blocked' state of a target which has been recovered
1171  *              successfully is reset.
1172  *
1173  * returns:     ZFCP_ERP_CONTINUES      - action continues (not considered)
1174  *              ZFCP_ERP_SUCCEEDED      - action finished successfully
1175  *              ZFCP_ERP_EXIT           - action failed and will not continue
1176  */
1177 static int
1178 zfcp_erp_strategy_check_target(struct zfcp_erp_action *erp_action, int result)
1179 {
1180         struct zfcp_adapter *adapter = erp_action->adapter;
1181         struct zfcp_port *port = erp_action->port;
1182         struct zfcp_unit *unit = erp_action->unit;
1183
1184         switch (erp_action->action) {
1185
1186         case ZFCP_ERP_ACTION_REOPEN_UNIT:
1187                 result = zfcp_erp_strategy_check_unit(unit, result);
1188                 break;
1189
1190         case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
1191         case ZFCP_ERP_ACTION_REOPEN_PORT:
1192                 result = zfcp_erp_strategy_check_port(port, result);
1193                 break;
1194
1195         case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
1196                 result = zfcp_erp_strategy_check_adapter(adapter, result);
1197                 break;
1198         }
1199
1200         return result;
1201 }
1202
1203 static int
1204 zfcp_erp_strategy_statechange(int action,
1205                               u32 status,
1206                               struct zfcp_adapter *adapter,
1207                               struct zfcp_port *port,
1208                               struct zfcp_unit *unit, int retval)
1209 {
1210         switch (action) {
1211
1212         case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
1213                 if (zfcp_erp_strategy_statechange_detected(&adapter->status,
1214                                                            status)) {
1215                         zfcp_erp_adapter_reopen_internal(adapter,
1216                                                 ZFCP_STATUS_COMMON_ERP_FAILED,
1217                                                 67, NULL);
1218                         retval = ZFCP_ERP_EXIT;
1219                 }
1220                 break;
1221
1222         case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
1223         case ZFCP_ERP_ACTION_REOPEN_PORT:
1224                 if (zfcp_erp_strategy_statechange_detected(&port->status,
1225                                                            status)) {
1226                         zfcp_erp_port_reopen_internal(port,
1227                                                 ZFCP_STATUS_COMMON_ERP_FAILED,
1228                                                 68, NULL);
1229                         retval = ZFCP_ERP_EXIT;
1230                 }
1231                 break;
1232
1233         case ZFCP_ERP_ACTION_REOPEN_UNIT:
1234                 if (zfcp_erp_strategy_statechange_detected(&unit->status,
1235                                                            status)) {
1236                         zfcp_erp_unit_reopen_internal(unit,
1237                                                 ZFCP_STATUS_COMMON_ERP_FAILED,
1238                                                 69, NULL);
1239                         retval = ZFCP_ERP_EXIT;
1240                 }
1241                 break;
1242         }
1243
1244         return retval;
1245 }
1246
1247 static int
1248 zfcp_erp_strategy_statechange_detected(atomic_t * target_status, u32 erp_status)
1249 {
1250         return
1251             /* take it online */
1252             (atomic_test_mask(ZFCP_STATUS_COMMON_RUNNING, target_status) &&
1253              (ZFCP_STATUS_ERP_CLOSE_ONLY & erp_status)) ||
1254             /* take it offline */
1255             (!atomic_test_mask(ZFCP_STATUS_COMMON_RUNNING, target_status) &&
1256              !(ZFCP_STATUS_ERP_CLOSE_ONLY & erp_status));
1257 }
1258
1259 static int
1260 zfcp_erp_strategy_check_unit(struct zfcp_unit *unit, int result)
1261 {
1262         switch (result) {
1263         case ZFCP_ERP_SUCCEEDED :
1264                 atomic_set(&unit->erp_counter, 0);
1265                 zfcp_erp_unit_unblock(unit);
1266                 break;
1267         case ZFCP_ERP_FAILED :
1268                 atomic_inc(&unit->erp_counter);
1269                 if (atomic_read(&unit->erp_counter) > ZFCP_MAX_ERPS)
1270                         zfcp_erp_unit_failed(unit, 21, NULL);
1271                 break;
1272         case ZFCP_ERP_EXIT :
1273                 /* nothing */
1274                 break;
1275         }
1276
1277         if (atomic_test_mask(ZFCP_STATUS_COMMON_ERP_FAILED, &unit->status)) {
1278                 zfcp_erp_unit_block(unit, 0); /* for ZFCP_ERP_SUCCEEDED */
1279                 result = ZFCP_ERP_EXIT;
1280         }
1281
1282         return result;
1283 }
1284
1285 static int
1286 zfcp_erp_strategy_check_port(struct zfcp_port *port, int result)
1287 {
1288         switch (result) {
1289         case ZFCP_ERP_SUCCEEDED :
1290                 atomic_set(&port->erp_counter, 0);
1291                 zfcp_erp_port_unblock(port);
1292                 break;
1293         case ZFCP_ERP_FAILED :
1294                 atomic_inc(&port->erp_counter);
1295                 if (atomic_read(&port->erp_counter) > ZFCP_MAX_ERPS)
1296                         zfcp_erp_port_failed(port, 22, NULL);
1297                 break;
1298         case ZFCP_ERP_EXIT :
1299                 /* nothing */
1300                 break;
1301         }
1302
1303         if (atomic_test_mask(ZFCP_STATUS_COMMON_ERP_FAILED, &port->status)) {
1304                 zfcp_erp_port_block(port, 0); /* for ZFCP_ERP_SUCCEEDED */
1305                 result = ZFCP_ERP_EXIT;
1306         }
1307
1308         return result;
1309 }
1310
1311 static int
1312 zfcp_erp_strategy_check_adapter(struct zfcp_adapter *adapter, int result)
1313 {
1314         switch (result) {
1315         case ZFCP_ERP_SUCCEEDED :
1316                 atomic_set(&adapter->erp_counter, 0);
1317                 zfcp_erp_adapter_unblock(adapter);
1318                 break;
1319         case ZFCP_ERP_FAILED :
1320                 atomic_inc(&adapter->erp_counter);
1321                 if (atomic_read(&adapter->erp_counter) > ZFCP_MAX_ERPS)
1322                         zfcp_erp_adapter_failed(adapter, 23, NULL);
1323                 break;
1324         case ZFCP_ERP_EXIT :
1325                 /* nothing */
1326                 break;
1327         }
1328
1329         if (atomic_test_mask(ZFCP_STATUS_COMMON_ERP_FAILED, &adapter->status)) {
1330                 zfcp_erp_adapter_block(adapter, 0); /* for ZFCP_ERP_SUCCEEDED */
1331                 result = ZFCP_ERP_EXIT;
1332         }
1333
1334         return result;
1335 }
1336
1337 struct zfcp_erp_add_work {
1338         struct zfcp_unit  *unit;
1339         struct work_struct work;
1340 };
1341
1342 /**
1343  * zfcp_erp_scsi_scan
1344  * @data: pointer to a struct zfcp_erp_add_work
1345  *
1346  * Registers a logical unit with the SCSI stack.
1347  */
1348 static void zfcp_erp_scsi_scan(struct work_struct *work)
1349 {
1350         struct zfcp_erp_add_work *p =
1351                 container_of(work, struct zfcp_erp_add_work, work);
1352         struct zfcp_unit *unit = p->unit;
1353         struct fc_rport *rport = unit->port->rport;
1354         scsi_scan_target(&rport->dev, 0, rport->scsi_target_id,
1355                          unit->scsi_lun, 0);
1356         atomic_clear_mask(ZFCP_STATUS_UNIT_SCSI_WORK_PENDING, &unit->status);
1357         zfcp_unit_put(unit);
1358         kfree(p);
1359 }
1360
1361 /**
1362  * zfcp_erp_schedule_work
1363  * @unit: pointer to unit which should be registered with SCSI stack
1364  *
1365  * Schedules work which registers a unit with the SCSI stack
1366  */
1367 static void
1368 zfcp_erp_schedule_work(struct zfcp_unit *unit)
1369 {
1370         struct zfcp_erp_add_work *p;
1371
1372         p = kzalloc(sizeof(*p), GFP_KERNEL);
1373         if (!p) {
1374                 ZFCP_LOG_NORMAL("error: Out of resources. Could not register "
1375                                 "the FCP-LUN 0x%Lx connected to "
1376                                 "the port with WWPN 0x%Lx connected to "
1377                                 "the adapter %s with the SCSI stack.\n",
1378                                 unit->fcp_lun,
1379                                 unit->port->wwpn,
1380                                 zfcp_get_busid_by_unit(unit));
1381                 return;
1382         }
1383
1384         zfcp_unit_get(unit);
1385         atomic_set_mask(ZFCP_STATUS_UNIT_SCSI_WORK_PENDING, &unit->status);
1386         INIT_WORK(&p->work, zfcp_erp_scsi_scan);
1387         p->unit = unit;
1388         schedule_work(&p->work);
1389 }
1390
1391 /*
1392  * function:
1393  *
1394  * purpose:     remaining things in good cases,
1395  *              escalation in bad cases
1396  *
1397  * returns:
1398  */
1399 static int
1400 zfcp_erp_strategy_followup_actions(int action,
1401                                    struct zfcp_adapter *adapter,
1402                                    struct zfcp_port *port,
1403                                    struct zfcp_unit *unit, int status)
1404 {
1405         /* initiate follow-up actions depending on success of finished action */
1406         switch (action) {
1407
1408         case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
1409                 if (status == ZFCP_ERP_SUCCEEDED)
1410                         zfcp_erp_port_reopen_all_internal(adapter, 0, 70, NULL);
1411                 else
1412                         zfcp_erp_adapter_reopen_internal(adapter, 0, 71, NULL);
1413                 break;
1414
1415         case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
1416                 if (status == ZFCP_ERP_SUCCEEDED)
1417                         zfcp_erp_port_reopen_internal(port, 0, 72, NULL);
1418                 else
1419                         zfcp_erp_adapter_reopen_internal(adapter, 0, 73, NULL);
1420                 break;
1421
1422         case ZFCP_ERP_ACTION_REOPEN_PORT:
1423                 if (status == ZFCP_ERP_SUCCEEDED)
1424                         zfcp_erp_unit_reopen_all_internal(port, 0, 74, NULL);
1425                 else
1426                         zfcp_erp_port_forced_reopen_internal(port, 0, 75, NULL);
1427                 break;
1428
1429         case ZFCP_ERP_ACTION_REOPEN_UNIT:
1430                 /* Nothing to do if status == ZFCP_ERP_SUCCEEDED */
1431                 if (status != ZFCP_ERP_SUCCEEDED)
1432                         zfcp_erp_port_reopen_internal(unit->port, 0, 76, NULL);
1433                 break;
1434         }
1435
1436         return 0;
1437 }
1438
1439 static int
1440 zfcp_erp_strategy_check_queues(struct zfcp_adapter *adapter)
1441 {
1442         unsigned long flags;
1443
1444         read_lock_irqsave(&zfcp_data.config_lock, flags);
1445         read_lock(&adapter->erp_lock);
1446         if (list_empty(&adapter->erp_ready_head) &&
1447             list_empty(&adapter->erp_running_head)) {
1448                         atomic_clear_mask(ZFCP_STATUS_ADAPTER_ERP_PENDING,
1449                                           &adapter->status);
1450                         wake_up(&adapter->erp_done_wqh);
1451         }
1452         read_unlock(&adapter->erp_lock);
1453         read_unlock_irqrestore(&zfcp_data.config_lock, flags);
1454
1455         return 0;
1456 }
1457
1458 /**
1459  * zfcp_erp_wait - wait for completion of error recovery on an adapter
1460  * @adapter: adapter for which to wait for completion of its error recovery
1461  * Return: 0
1462  */
1463 int
1464 zfcp_erp_wait(struct zfcp_adapter *adapter)
1465 {
1466         int retval = 0;
1467
1468         wait_event(adapter->erp_done_wqh,
1469                    !atomic_test_mask(ZFCP_STATUS_ADAPTER_ERP_PENDING,
1470                                      &adapter->status));
1471
1472         return retval;
1473 }
1474
1475 void zfcp_erp_modify_adapter_status(struct zfcp_adapter *adapter, u8 id,
1476                                     void *ref, u32 mask, int set_or_clear)
1477 {
1478         struct zfcp_port *port;
1479         u32 changed, common_mask = mask & ZFCP_COMMON_FLAGS;
1480
1481         if (set_or_clear == ZFCP_SET) {
1482                 changed = atomic_test_and_set_mask(mask, &adapter->status);
1483         } else {
1484                 changed = atomic_test_and_clear_mask(mask, &adapter->status);
1485                 if (mask & ZFCP_STATUS_COMMON_ERP_FAILED)
1486                         atomic_set(&adapter->erp_counter, 0);
1487         }
1488         if (changed)
1489                 zfcp_rec_dbf_event_adapter(id, ref, adapter);
1490
1491         /* Deal with all underlying devices, only pass common_mask */
1492         if (common_mask)
1493                 list_for_each_entry(port, &adapter->port_list_head, list)
1494                         zfcp_erp_modify_port_status(port, id, ref, common_mask,
1495                                                     set_or_clear);
1496 }
1497
1498 /*
1499  * function:    zfcp_erp_modify_port_status
1500  *
1501  * purpose:     sets the port and all underlying devices to ERP_FAILED
1502  *
1503  */
1504 void zfcp_erp_modify_port_status(struct zfcp_port *port, u8 id, void *ref,
1505                                  u32 mask, int set_or_clear)
1506 {
1507         struct zfcp_unit *unit;
1508         u32 changed, common_mask = mask & ZFCP_COMMON_FLAGS;
1509
1510         if (set_or_clear == ZFCP_SET) {
1511                 changed = atomic_test_and_set_mask(mask, &port->status);
1512         } else {
1513                 changed = atomic_test_and_clear_mask(mask, &port->status);
1514                 if (mask & ZFCP_STATUS_COMMON_ERP_FAILED)
1515                         atomic_set(&port->erp_counter, 0);
1516         }
1517         if (changed)
1518                 zfcp_rec_dbf_event_port(id, ref, port);
1519
1520         /* Modify status of all underlying devices, only pass common mask */
1521         if (common_mask)
1522                 list_for_each_entry(unit, &port->unit_list_head, list)
1523                         zfcp_erp_modify_unit_status(unit, id, ref, common_mask,
1524                                                     set_or_clear);
1525 }
1526
1527 /*
1528  * function:    zfcp_erp_modify_unit_status
1529  *
1530  * purpose:     sets the unit to ERP_FAILED
1531  *
1532  */
1533 void zfcp_erp_modify_unit_status(struct zfcp_unit *unit, u8 id, void *ref,
1534                                  u32 mask, int set_or_clear)
1535 {
1536         u32 changed;
1537
1538         if (set_or_clear == ZFCP_SET) {
1539                 changed = atomic_test_and_set_mask(mask, &unit->status);
1540         } else {
1541                 changed = atomic_test_and_clear_mask(mask, &unit->status);
1542                 if (mask & ZFCP_STATUS_COMMON_ERP_FAILED) {
1543                         atomic_set(&unit->erp_counter, 0);
1544                 }
1545         }
1546         if (changed)
1547                 zfcp_rec_dbf_event_unit(id, ref, unit);
1548 }
1549
1550 /*
1551  * function:
1552  *
1553  * purpose:     Wrappper for zfcp_erp_port_reopen_all_internal
1554  *              used to ensure the correct locking
1555  *
1556  * returns:     0       - initiated action successfully
1557  *              <0      - failed to initiate action
1558  */
1559 int zfcp_erp_port_reopen_all(struct zfcp_adapter *adapter, int clear_mask,
1560                              u8 id, void *ref)
1561 {
1562         int retval;
1563         unsigned long flags;
1564
1565         read_lock_irqsave(&zfcp_data.config_lock, flags);
1566         write_lock(&adapter->erp_lock);
1567         retval = zfcp_erp_port_reopen_all_internal(adapter, clear_mask, id,
1568                                                    ref);
1569         write_unlock(&adapter->erp_lock);
1570         read_unlock_irqrestore(&zfcp_data.config_lock, flags);
1571
1572         return retval;
1573 }
1574
1575 static int zfcp_erp_port_reopen_all_internal(struct zfcp_adapter *adapter,
1576                                              int clear_mask, u8 id, void *ref)
1577 {
1578         int retval = 0;
1579         struct zfcp_port *port;
1580
1581         list_for_each_entry(port, &adapter->port_list_head, list)
1582                 if (!atomic_test_mask(ZFCP_STATUS_PORT_WKA, &port->status))
1583                         zfcp_erp_port_reopen_internal(port, clear_mask, id,
1584                                                       ref);
1585
1586         return retval;
1587 }
1588
1589 /*
1590  * function:
1591  *
1592  * purpose:
1593  *
1594  * returns:     FIXME
1595  */
1596 static int zfcp_erp_unit_reopen_all_internal(struct zfcp_port *port,
1597                                              int clear_mask, u8 id, void *ref)
1598 {
1599         int retval = 0;
1600         struct zfcp_unit *unit;
1601
1602         list_for_each_entry(unit, &port->unit_list_head, list)
1603                 zfcp_erp_unit_reopen_internal(unit, clear_mask, id, ref);
1604
1605         return retval;
1606 }
1607
1608 /*
1609  * function:
1610  *
1611  * purpose:     this routine executes the 'Reopen Adapter' action
1612  *              (the entire action is processed synchronously, since
1613  *              there are no actions which might be run concurrently
1614  *              per definition)
1615  *
1616  * returns:     ZFCP_ERP_SUCCEEDED      - action finished successfully
1617  *              ZFCP_ERP_FAILED         - action finished unsuccessfully
1618  */
1619 static int
1620 zfcp_erp_adapter_strategy(struct zfcp_erp_action *erp_action)
1621 {
1622         int retval;
1623         struct zfcp_adapter *adapter = erp_action->adapter;
1624
1625         retval = zfcp_erp_adapter_strategy_close(erp_action);
1626         if (erp_action->status & ZFCP_STATUS_ERP_CLOSE_ONLY)
1627                 retval = ZFCP_ERP_EXIT;
1628         else
1629                 retval = zfcp_erp_adapter_strategy_open(erp_action);
1630
1631         if (retval == ZFCP_ERP_FAILED) {
1632                 ZFCP_LOG_INFO("Waiting to allow the adapter %s "
1633                               "to recover itself\n",
1634                               zfcp_get_busid_by_adapter(adapter));
1635                 ssleep(ZFCP_TYPE2_RECOVERY_TIME);
1636         }
1637
1638         return retval;
1639 }
1640
1641 /*
1642  * function:
1643  *
1644  * purpose:
1645  *
1646  * returns:     ZFCP_ERP_SUCCEEDED      - action finished successfully
1647  *              ZFCP_ERP_FAILED         - action finished unsuccessfully
1648  */
1649 static int
1650 zfcp_erp_adapter_strategy_close(struct zfcp_erp_action *erp_action)
1651 {
1652         int retval;
1653
1654         atomic_set_mask(ZFCP_STATUS_COMMON_CLOSING,
1655                         &erp_action->adapter->status);
1656         retval = zfcp_erp_adapter_strategy_generic(erp_action, 1);
1657         atomic_clear_mask(ZFCP_STATUS_COMMON_CLOSING,
1658                           &erp_action->adapter->status);
1659
1660         return retval;
1661 }
1662
1663 /*
1664  * function:
1665  *
1666  * purpose:
1667  *
1668  * returns:     ZFCP_ERP_SUCCEEDED      - action finished successfully
1669  *              ZFCP_ERP_FAILED         - action finished unsuccessfully
1670  */
1671 static int
1672 zfcp_erp_adapter_strategy_open(struct zfcp_erp_action *erp_action)
1673 {
1674         int retval;
1675
1676         atomic_set_mask(ZFCP_STATUS_COMMON_OPENING,
1677                         &erp_action->adapter->status);
1678         retval = zfcp_erp_adapter_strategy_generic(erp_action, 0);
1679         atomic_clear_mask(ZFCP_STATUS_COMMON_OPENING,
1680                           &erp_action->adapter->status);
1681
1682         return retval;
1683 }
1684
1685 /*
1686  * function:    zfcp_register_adapter
1687  *
1688  * purpose:     allocate the irq associated with this devno and register
1689  *              the FSF adapter with the SCSI stack
1690  *
1691  * returns:
1692  */
1693 static int
1694 zfcp_erp_adapter_strategy_generic(struct zfcp_erp_action *erp_action, int close)
1695 {
1696         int retval = ZFCP_ERP_SUCCEEDED;
1697
1698         if (close)
1699                 goto close_only;
1700
1701         retval = zfcp_erp_adapter_strategy_open_qdio(erp_action);
1702         if (retval != ZFCP_ERP_SUCCEEDED)
1703                 goto failed_qdio;
1704
1705         retval = zfcp_erp_adapter_strategy_open_fsf(erp_action);
1706         if (retval != ZFCP_ERP_SUCCEEDED)
1707                 goto failed_openfcp;
1708
1709         atomic_set_mask(ZFCP_STATUS_COMMON_OPEN, &erp_action->adapter->status);
1710         goto out;
1711
1712  close_only:
1713         atomic_clear_mask(ZFCP_STATUS_COMMON_OPEN,
1714                           &erp_action->adapter->status);
1715
1716  failed_openfcp:
1717         zfcp_close_fsf(erp_action->adapter);
1718  failed_qdio:
1719         atomic_clear_mask(ZFCP_STATUS_ADAPTER_XCONFIG_OK |
1720                           ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED |
1721                           ZFCP_STATUS_ADAPTER_XPORT_OK,
1722                           &erp_action->adapter->status);
1723  out:
1724         return retval;
1725 }
1726
1727 /*
1728  * function:    zfcp_qdio_init
1729  *
1730  * purpose:     setup QDIO operation for specified adapter
1731  *
1732  * returns:     0 - successful setup
1733  *              !0 - failed setup
1734  */
1735 static int
1736 zfcp_erp_adapter_strategy_open_qdio(struct zfcp_erp_action *erp_action)
1737 {
1738         int retval;
1739         int i;
1740         volatile struct qdio_buffer_element *sbale;
1741         struct zfcp_adapter *adapter = erp_action->adapter;
1742
1743         if (atomic_test_mask(ZFCP_STATUS_ADAPTER_QDIOUP, &adapter->status)) {
1744                 ZFCP_LOG_NORMAL("bug: second attempt to set up QDIO on "
1745                                 "adapter %s\n",
1746                                 zfcp_get_busid_by_adapter(adapter));
1747                 goto failed_sanity;
1748         }
1749
1750         if (qdio_establish(&adapter->qdio_init_data) != 0) {
1751                 ZFCP_LOG_INFO("error: establishment of QDIO queues failed "
1752                               "on adapter %s\n",
1753                               zfcp_get_busid_by_adapter(adapter));
1754                 goto failed_qdio_establish;
1755         }
1756
1757         if (qdio_activate(adapter->ccw_device, 0) != 0) {
1758                 ZFCP_LOG_INFO("error: activation of QDIO queues failed "
1759                               "on adapter %s\n",
1760                               zfcp_get_busid_by_adapter(adapter));
1761                 goto failed_qdio_activate;
1762         }
1763
1764         /*
1765          * put buffers into response queue,
1766          */
1767         for (i = 0; i < QDIO_MAX_BUFFERS_PER_Q; i++) {
1768                 sbale = &(adapter->response_queue.buffer[i]->element[0]);
1769                 sbale->length = 0;
1770                 sbale->flags = SBAL_FLAGS_LAST_ENTRY;
1771                 sbale->addr = NULL;
1772         }
1773
1774         ZFCP_LOG_TRACE("calling do_QDIO on adapter %s (flags=0x%x, "
1775                        "queue_no=%i, index_in_queue=%i, count=%i)\n",
1776                        zfcp_get_busid_by_adapter(adapter),
1777                        QDIO_FLAG_SYNC_INPUT, 0, 0, QDIO_MAX_BUFFERS_PER_Q);
1778
1779         retval = do_QDIO(adapter->ccw_device,
1780                          QDIO_FLAG_SYNC_INPUT,
1781                          0, 0, QDIO_MAX_BUFFERS_PER_Q, NULL);
1782
1783         if (retval) {
1784                 ZFCP_LOG_NORMAL("bug: setup of QDIO failed (retval=%d)\n",
1785                                 retval);
1786                 goto failed_do_qdio;
1787         } else {
1788                 adapter->response_queue.free_index = 0;
1789                 atomic_set(&adapter->response_queue.free_count, 0);
1790                 ZFCP_LOG_DEBUG("%i buffers successfully enqueued to "
1791                                "response queue\n", QDIO_MAX_BUFFERS_PER_Q);
1792         }
1793         /* set index of first avalable SBALS / number of available SBALS */
1794         adapter->request_queue.free_index = 0;
1795         atomic_set(&adapter->request_queue.free_count, QDIO_MAX_BUFFERS_PER_Q);
1796         adapter->request_queue.distance_from_int = 0;
1797
1798         /* initialize waitqueue used to wait for free SBALs in requests queue */
1799         init_waitqueue_head(&adapter->request_wq);
1800
1801         /* ok, we did it - skip all cleanups for different failures */
1802         atomic_set_mask(ZFCP_STATUS_ADAPTER_QDIOUP, &adapter->status);
1803         retval = ZFCP_ERP_SUCCEEDED;
1804         goto out;
1805
1806  failed_do_qdio:
1807         /* NOP */
1808
1809  failed_qdio_activate:
1810         while (qdio_shutdown(adapter->ccw_device,
1811                              QDIO_FLAG_CLEANUP_USING_CLEAR) == -EINPROGRESS)
1812                 ssleep(1);
1813
1814  failed_qdio_establish:
1815  failed_sanity:
1816         retval = ZFCP_ERP_FAILED;
1817
1818  out:
1819         return retval;
1820 }
1821
1822
1823 static int
1824 zfcp_erp_adapter_strategy_open_fsf(struct zfcp_erp_action *erp_action)
1825 {
1826         int retval;
1827
1828         retval = zfcp_erp_adapter_strategy_open_fsf_xconfig(erp_action);
1829         if (retval == ZFCP_ERP_FAILED)
1830                 return ZFCP_ERP_FAILED;
1831
1832         retval = zfcp_erp_adapter_strategy_open_fsf_xport(erp_action);
1833         if (retval == ZFCP_ERP_FAILED)
1834                 return ZFCP_ERP_FAILED;
1835
1836         return zfcp_erp_adapter_strategy_open_fsf_statusread(erp_action);
1837 }
1838
1839 static int
1840 zfcp_erp_adapter_strategy_open_fsf_xconfig(struct zfcp_erp_action *erp_action)
1841 {
1842         int retval = ZFCP_ERP_SUCCEEDED;
1843         int retries;
1844         int sleep = ZFCP_EXCHANGE_CONFIG_DATA_FIRST_SLEEP;
1845         struct zfcp_adapter *adapter = erp_action->adapter;
1846
1847         atomic_clear_mask(ZFCP_STATUS_ADAPTER_XCONFIG_OK, &adapter->status);
1848
1849         for (retries = ZFCP_EXCHANGE_CONFIG_DATA_RETRIES; retries; retries--) {
1850                 atomic_clear_mask(ZFCP_STATUS_ADAPTER_HOST_CON_INIT,
1851                                   &adapter->status);
1852                 ZFCP_LOG_DEBUG("Doing exchange config data\n");
1853                 write_lock_irq(&adapter->erp_lock);
1854                 zfcp_erp_action_to_running(erp_action);
1855                 write_unlock_irq(&adapter->erp_lock);
1856                 if (zfcp_fsf_exchange_config_data(erp_action)) {
1857                         retval = ZFCP_ERP_FAILED;
1858                         ZFCP_LOG_INFO("error:  initiation of exchange of "
1859                                       "configuration data failed for "
1860                                       "adapter %s\n",
1861                                       zfcp_get_busid_by_adapter(adapter));
1862                         break;
1863                 }
1864                 ZFCP_LOG_DEBUG("Xchange underway\n");
1865
1866                 /*
1867                  * Why this works:
1868                  * Both the normal completion handler as well as the timeout
1869                  * handler will do an 'up' when the 'exchange config data'
1870                  * request completes or times out. Thus, the signal to go on
1871                  * won't be lost utilizing this semaphore.
1872                  * Furthermore, this 'adapter_reopen' action is
1873                  * guaranteed to be the only action being there (highest action
1874                  * which prevents other actions from being created).
1875                  * Resulting from that, the wake signal recognized here
1876                  * _must_ be the one belonging to the 'exchange config
1877                  * data' request.
1878                  */
1879                 zfcp_rec_dbf_event_thread_lock(6, adapter);
1880                 down(&adapter->erp_ready_sem);
1881                 zfcp_rec_dbf_event_thread_lock(7, adapter);
1882                 if (erp_action->status & ZFCP_STATUS_ERP_TIMEDOUT) {
1883                         ZFCP_LOG_INFO("error: exchange of configuration data "
1884                                       "for adapter %s timed out\n",
1885                                       zfcp_get_busid_by_adapter(adapter));
1886                         break;
1887                 }
1888
1889                 if (!atomic_test_mask(ZFCP_STATUS_ADAPTER_HOST_CON_INIT,
1890                                      &adapter->status))
1891                         break;
1892
1893                 ZFCP_LOG_DEBUG("host connection still initialising... "
1894                                "waiting and retrying...\n");
1895                 /* sleep a little bit before retry */
1896                 ssleep(sleep);
1897                 sleep *= 2;
1898         }
1899
1900         atomic_clear_mask(ZFCP_STATUS_ADAPTER_HOST_CON_INIT,
1901                           &adapter->status);
1902
1903         if (!atomic_test_mask(ZFCP_STATUS_ADAPTER_XCONFIG_OK,
1904                               &adapter->status)) {
1905                 ZFCP_LOG_INFO("error: exchange of configuration data for "
1906                               "adapter %s failed\n",
1907                               zfcp_get_busid_by_adapter(adapter));
1908                 retval = ZFCP_ERP_FAILED;
1909         }
1910
1911         return retval;
1912 }
1913
1914 static int
1915 zfcp_erp_adapter_strategy_open_fsf_xport(struct zfcp_erp_action *erp_action)
1916 {
1917         int ret;
1918         struct zfcp_adapter *adapter;
1919
1920         adapter = erp_action->adapter;
1921         atomic_clear_mask(ZFCP_STATUS_ADAPTER_XPORT_OK, &adapter->status);
1922
1923         write_lock_irq(&adapter->erp_lock);
1924         zfcp_erp_action_to_running(erp_action);
1925         write_unlock_irq(&adapter->erp_lock);
1926
1927         ret = zfcp_fsf_exchange_port_data(erp_action);
1928         if (ret == -EOPNOTSUPP) {
1929                 return ZFCP_ERP_SUCCEEDED;
1930         } else if (ret) {
1931                 return ZFCP_ERP_FAILED;
1932         }
1933
1934         ret = ZFCP_ERP_SUCCEEDED;
1935         zfcp_rec_dbf_event_thread_lock(8, adapter);
1936         down(&adapter->erp_ready_sem);
1937         zfcp_rec_dbf_event_thread_lock(9, adapter);
1938         if (erp_action->status & ZFCP_STATUS_ERP_TIMEDOUT) {
1939                 ZFCP_LOG_INFO("error: exchange port data timed out (adapter "
1940                               "%s)\n", zfcp_get_busid_by_adapter(adapter));
1941                 ret = ZFCP_ERP_FAILED;
1942         }
1943
1944         /* don't treat as error for the sake of compatibility */
1945         if (!atomic_test_mask(ZFCP_STATUS_ADAPTER_XPORT_OK, &adapter->status))
1946                 ZFCP_LOG_INFO("warning: exchange port data failed (adapter "
1947                               "%s\n", zfcp_get_busid_by_adapter(adapter));
1948
1949         return ret;
1950 }
1951
1952 static int
1953 zfcp_erp_adapter_strategy_open_fsf_statusread(struct zfcp_erp_action
1954                                               *erp_action)
1955 {
1956         struct zfcp_adapter *adapter = erp_action->adapter;
1957
1958         atomic_set(&adapter->stat_miss, 16);
1959         return zfcp_status_read_refill(adapter);
1960 }
1961
1962 /*
1963  * function:
1964  *
1965  * purpose:     this routine executes the 'Reopen Physical Port' action
1966  *
1967  * returns:     ZFCP_ERP_CONTINUES      - action continues (asynchronously)
1968  *              ZFCP_ERP_SUCCEEDED      - action finished successfully
1969  *              ZFCP_ERP_FAILED         - action finished unsuccessfully
1970  */
1971 static int
1972 zfcp_erp_port_forced_strategy(struct zfcp_erp_action *erp_action)
1973 {
1974         int retval = ZFCP_ERP_FAILED;
1975         struct zfcp_port *port = erp_action->port;
1976
1977         switch (erp_action->step) {
1978
1979                 /*
1980                  * FIXME:
1981                  * the ULP spec. begs for waiting for oustanding commands
1982                  */
1983         case ZFCP_ERP_STEP_UNINITIALIZED:
1984                 zfcp_erp_port_strategy_clearstati(port);
1985                 /*
1986                  * it would be sufficient to test only the normal open flag
1987                  * since the phys. open flag cannot be set if the normal
1988                  * open flag is unset - however, this is for readabilty ...
1989                  */
1990                 if (atomic_test_mask((ZFCP_STATUS_PORT_PHYS_OPEN |
1991                                       ZFCP_STATUS_COMMON_OPEN),
1992                                      &port->status)) {
1993                         ZFCP_LOG_DEBUG("port 0x%016Lx is open -> trying "
1994                                        "close physical\n", port->wwpn);
1995                         retval =
1996                             zfcp_erp_port_forced_strategy_close(erp_action);
1997                 } else
1998                         retval = ZFCP_ERP_FAILED;
1999                 break;
2000
2001         case ZFCP_ERP_STEP_PHYS_PORT_CLOSING:
2002                 if (atomic_test_mask(ZFCP_STATUS_PORT_PHYS_OPEN,
2003                                      &port->status)) {
2004                         ZFCP_LOG_DEBUG("close physical failed for port "
2005                                        "0x%016Lx\n", port->wwpn);
2006                         retval = ZFCP_ERP_FAILED;
2007                 } else
2008                         retval = ZFCP_ERP_SUCCEEDED;
2009                 break;
2010         }
2011
2012         return retval;
2013 }
2014
2015 /*
2016  * function:
2017  *
2018  * purpose:     this routine executes the 'Reopen Port' action
2019  *
2020  * returns:     ZFCP_ERP_CONTINUES      - action continues (asynchronously)
2021  *              ZFCP_ERP_SUCCEEDED      - action finished successfully
2022  *              ZFCP_ERP_FAILED         - action finished unsuccessfully
2023  */
2024 static int
2025 zfcp_erp_port_strategy(struct zfcp_erp_action *erp_action)
2026 {
2027         int retval = ZFCP_ERP_FAILED;
2028         struct zfcp_port *port = erp_action->port;
2029
2030         switch (erp_action->step) {
2031
2032                 /*
2033                  * FIXME:
2034                  * the ULP spec. begs for waiting for oustanding commands
2035                  */
2036         case ZFCP_ERP_STEP_UNINITIALIZED:
2037                 zfcp_erp_port_strategy_clearstati(port);
2038                 if (atomic_test_mask(ZFCP_STATUS_COMMON_OPEN, &port->status)) {
2039                         ZFCP_LOG_DEBUG("port 0x%016Lx is open -> trying "
2040                                        "close\n", port->wwpn);
2041                         retval = zfcp_erp_port_strategy_close(erp_action);
2042                         goto out;
2043                 }               /* else it's already closed, open it */
2044                 break;
2045
2046         case ZFCP_ERP_STEP_PORT_CLOSING:
2047                 if (atomic_test_mask(ZFCP_STATUS_COMMON_OPEN, &port->status)) {
2048                         ZFCP_LOG_DEBUG("close failed for port 0x%016Lx\n",
2049                                        port->wwpn);
2050                         retval = ZFCP_ERP_FAILED;
2051                         goto out;
2052                 }               /* else it's closed now, open it */
2053                 break;
2054         }
2055         if (erp_action->status & ZFCP_STATUS_ERP_CLOSE_ONLY)
2056                 retval = ZFCP_ERP_EXIT;
2057         else
2058                 retval = zfcp_erp_port_strategy_open(erp_action);
2059
2060  out:
2061         return retval;
2062 }
2063
2064 static int
2065 zfcp_erp_port_strategy_open(struct zfcp_erp_action *erp_action)
2066 {
2067         int retval;
2068
2069         if (atomic_test_mask(ZFCP_STATUS_PORT_WKA,
2070                              &erp_action->port->status))
2071                 retval = zfcp_erp_port_strategy_open_nameserver(erp_action);
2072         else
2073                 retval = zfcp_erp_port_strategy_open_common(erp_action);
2074
2075         return retval;
2076 }
2077
2078 static int
2079 zfcp_erp_port_strategy_open_common(struct zfcp_erp_action *erp_action)
2080 {
2081         int retval = 0;
2082         struct zfcp_adapter *adapter = erp_action->adapter;
2083         struct zfcp_port *port = erp_action->port;
2084
2085         switch (erp_action->step) {
2086
2087         case ZFCP_ERP_STEP_UNINITIALIZED:
2088         case ZFCP_ERP_STEP_PHYS_PORT_CLOSING:
2089         case ZFCP_ERP_STEP_PORT_CLOSING:
2090                 if (fc_host_port_type(adapter->scsi_host) == FC_PORTTYPE_PTP) {
2091                         if (port->wwpn != adapter->peer_wwpn) {
2092                                 ZFCP_LOG_NORMAL("Failed to open port 0x%016Lx "
2093                                                 "on adapter %s.\nPeer WWPN "
2094                                                 "0x%016Lx does not match\n",
2095                                                 port->wwpn,
2096                                                 zfcp_get_busid_by_adapter(adapter),
2097                                                 adapter->peer_wwpn);
2098                                 zfcp_erp_port_failed(port, 25, NULL);
2099                                 retval = ZFCP_ERP_FAILED;
2100                                 break;
2101                         }
2102                         port->d_id = adapter->peer_d_id;
2103                         atomic_set_mask(ZFCP_STATUS_PORT_DID_DID, &port->status);
2104                         retval = zfcp_erp_port_strategy_open_port(erp_action);
2105                         break;
2106                 }
2107                 if (!(adapter->nameserver_port)) {
2108                         retval = zfcp_nameserver_enqueue(adapter);
2109                         if (retval != 0) {
2110                                 ZFCP_LOG_NORMAL("error: nameserver port "
2111                                                 "unavailable for adapter %s\n",
2112                                                 zfcp_get_busid_by_adapter(adapter));
2113                                 retval = ZFCP_ERP_FAILED;
2114                                 break;
2115                         }
2116                 }
2117                 if (!atomic_test_mask(ZFCP_STATUS_COMMON_UNBLOCKED,
2118                                       &adapter->nameserver_port->status)) {
2119                         ZFCP_LOG_DEBUG("nameserver port is not open -> open "
2120                                        "nameserver port\n");
2121                         /* nameserver port may live again */
2122                         atomic_set_mask(ZFCP_STATUS_COMMON_RUNNING,
2123                                         &adapter->nameserver_port->status);
2124                         if (zfcp_erp_port_reopen(adapter->nameserver_port, 0,
2125                                                  77, erp_action) >= 0) {
2126                                 erp_action->step =
2127                                         ZFCP_ERP_STEP_NAMESERVER_OPEN;
2128                                 retval = ZFCP_ERP_CONTINUES;
2129                         } else
2130                                 retval = ZFCP_ERP_FAILED;
2131                         break;
2132                 }
2133                 /* else nameserver port is already open, fall through */
2134         case ZFCP_ERP_STEP_NAMESERVER_OPEN:
2135                 if (!atomic_test_mask(ZFCP_STATUS_COMMON_OPEN,
2136                                       &adapter->nameserver_port->status)) {
2137                         ZFCP_LOG_DEBUG("open failed for nameserver port\n");
2138                         retval = ZFCP_ERP_FAILED;
2139                 } else {
2140                         ZFCP_LOG_DEBUG("nameserver port is open -> "
2141                                        "nameserver look-up for port 0x%016Lx\n",
2142                                        port->wwpn);
2143                         retval = zfcp_erp_port_strategy_open_common_lookup
2144                                 (erp_action);
2145                 }
2146                 break;
2147
2148         case ZFCP_ERP_STEP_NAMESERVER_LOOKUP:
2149                 if (!atomic_test_mask(ZFCP_STATUS_PORT_DID_DID, &port->status)) {
2150                         if (atomic_test_mask
2151                             (ZFCP_STATUS_PORT_INVALID_WWPN, &port->status)) {
2152                                 ZFCP_LOG_DEBUG("nameserver look-up failed "
2153                                                "for port 0x%016Lx "
2154                                                "(misconfigured WWPN?)\n",
2155                                                port->wwpn);
2156                                 zfcp_erp_port_failed(port, 26, NULL);
2157                                 retval = ZFCP_ERP_EXIT;
2158                         } else {
2159                                 ZFCP_LOG_DEBUG("nameserver look-up failed for "
2160                                                "port 0x%016Lx\n", port->wwpn);
2161                                 retval = ZFCP_ERP_FAILED;
2162                         }
2163                 } else {
2164                         ZFCP_LOG_DEBUG("port 0x%016Lx has d_id=0x%06x -> "
2165                                        "trying open\n", port->wwpn, port->d_id);
2166                         retval = zfcp_erp_port_strategy_open_port(erp_action);
2167                 }
2168                 break;
2169
2170         case ZFCP_ERP_STEP_PORT_OPENING:
2171                 /* D_ID might have changed during open */
2172                 if (atomic_test_mask((ZFCP_STATUS_COMMON_OPEN |
2173                                       ZFCP_STATUS_PORT_DID_DID),
2174                                      &port->status)) {
2175                         ZFCP_LOG_DEBUG("port 0x%016Lx is open\n", port->wwpn);
2176                         retval = ZFCP_ERP_SUCCEEDED;
2177                 } else {
2178                         ZFCP_LOG_DEBUG("open failed for port 0x%016Lx\n",
2179                                        port->wwpn);
2180                         retval = ZFCP_ERP_FAILED;
2181                 }
2182                 break;
2183
2184         default:
2185                 ZFCP_LOG_NORMAL("bug: unknown erp step 0x%08x\n",
2186                                 erp_action->step);
2187                 retval = ZFCP_ERP_FAILED;
2188         }
2189
2190         return retval;
2191 }
2192
2193 static int
2194 zfcp_erp_port_strategy_open_nameserver(struct zfcp_erp_action *erp_action)
2195 {
2196         int retval;
2197         struct zfcp_port *port = erp_action->port;
2198
2199         switch (erp_action->step) {
2200
2201         case ZFCP_ERP_STEP_UNINITIALIZED:
2202         case ZFCP_ERP_STEP_PHYS_PORT_CLOSING:
2203         case ZFCP_ERP_STEP_PORT_CLOSING:
2204                 ZFCP_LOG_DEBUG("port 0x%016Lx has d_id=0x%06x -> trying open\n",
2205                                port->wwpn, port->d_id);
2206                 retval = zfcp_erp_port_strategy_open_port(erp_action);
2207                 break;
2208
2209         case ZFCP_ERP_STEP_PORT_OPENING:
2210                 if (atomic_test_mask(ZFCP_STATUS_COMMON_OPEN, &port->status)) {
2211                         ZFCP_LOG_DEBUG("WKA port is open\n");
2212                         retval = ZFCP_ERP_SUCCEEDED;
2213                 } else {
2214                         ZFCP_LOG_DEBUG("open failed for WKA port\n");
2215                         retval = ZFCP_ERP_FAILED;
2216                 }
2217                 /* this is needed anyway (dont care for retval of wakeup) */
2218                 ZFCP_LOG_DEBUG("continue other open port operations\n");
2219                 zfcp_erp_port_strategy_open_nameserver_wakeup(erp_action);
2220                 break;
2221
2222         default:
2223                 ZFCP_LOG_NORMAL("bug: unknown erp step 0x%08x\n",
2224                                 erp_action->step);
2225                 retval = ZFCP_ERP_FAILED;
2226         }
2227
2228         return retval;
2229 }
2230
2231 /*
2232  * function:
2233  *
2234  * purpose:     makes the erp thread continue with reopen (physical) port
2235  *              actions which have been paused until the name server port
2236  *              is opened (or failed)
2237  *
2238  * returns:     0       (a kind of void retval, its not used)
2239  */
2240 static int
2241 zfcp_erp_port_strategy_open_nameserver_wakeup(struct zfcp_erp_action
2242                                               *ns_erp_action)
2243 {
2244         int retval = 0;
2245         unsigned long flags;
2246         struct zfcp_adapter *adapter = ns_erp_action->adapter;
2247         struct zfcp_erp_action *erp_action, *tmp;
2248
2249         read_lock_irqsave(&adapter->erp_lock, flags);
2250         list_for_each_entry_safe(erp_action, tmp, &adapter->erp_running_head,
2251                                  list) {
2252                 if (erp_action->step == ZFCP_ERP_STEP_NAMESERVER_OPEN) {
2253                         if (atomic_test_mask(
2254                                     ZFCP_STATUS_COMMON_ERP_FAILED,
2255                                     &adapter->nameserver_port->status))
2256                                 zfcp_erp_port_failed(erp_action->port, 27,
2257                                                      NULL);
2258                         zfcp_erp_action_ready(erp_action);
2259                 }
2260         }
2261         read_unlock_irqrestore(&adapter->erp_lock, flags);
2262
2263         return retval;
2264 }
2265
2266 /*
2267  * function:
2268  *
2269  * purpose:
2270  *
2271  * returns:     ZFCP_ERP_CONTINUES      - action continues (asynchronously)
2272  *              ZFCP_ERP_FAILED         - action finished unsuccessfully
2273  */
2274 static int
2275 zfcp_erp_port_forced_strategy_close(struct zfcp_erp_action *erp_action)
2276 {
2277         int retval;
2278
2279         retval = zfcp_fsf_close_physical_port(erp_action);
2280         if (retval == -ENOMEM) {
2281                 retval = ZFCP_ERP_NOMEM;
2282                 goto out;
2283         }
2284         erp_action->step = ZFCP_ERP_STEP_PHYS_PORT_CLOSING;
2285         if (retval != 0) {
2286                 /* could not send 'open', fail */
2287                 retval = ZFCP_ERP_FAILED;
2288                 goto out;
2289         }
2290         retval = ZFCP_ERP_CONTINUES;
2291  out:
2292         return retval;
2293 }
2294
2295 static int
2296 zfcp_erp_port_strategy_clearstati(struct zfcp_port *port)
2297 {
2298         int retval = 0;
2299
2300         atomic_clear_mask(ZFCP_STATUS_COMMON_OPENING |
2301                           ZFCP_STATUS_COMMON_CLOSING |
2302                           ZFCP_STATUS_COMMON_ACCESS_DENIED |
2303                           ZFCP_STATUS_PORT_DID_DID |
2304                           ZFCP_STATUS_PORT_PHYS_CLOSING |
2305                           ZFCP_STATUS_PORT_INVALID_WWPN,
2306                           &port->status);
2307         return retval;
2308 }
2309
2310 /*
2311  * function:
2312  *
2313  * purpose:
2314  *
2315  * returns:     ZFCP_ERP_CONTINUES      - action continues (asynchronously)
2316  *              ZFCP_ERP_FAILED         - action finished unsuccessfully
2317  */
2318 static int
2319 zfcp_erp_port_strategy_close(struct zfcp_erp_action *erp_action)
2320 {
2321         int retval;
2322
2323         retval = zfcp_fsf_close_port(erp_action);
2324         if (retval == -ENOMEM) {
2325                 retval = ZFCP_ERP_NOMEM;
2326                 goto out;
2327         }
2328         erp_action->step = ZFCP_ERP_STEP_PORT_CLOSING;
2329         if (retval != 0) {
2330                 /* could not send 'close', fail */
2331                 retval = ZFCP_ERP_FAILED;
2332                 goto out;
2333         }
2334         retval = ZFCP_ERP_CONTINUES;
2335  out:
2336         return retval;
2337 }
2338
2339 /*
2340  * function:
2341  *
2342  * purpose:
2343  *
2344  * returns:     ZFCP_ERP_CONTINUES      - action continues (asynchronously)
2345  *              ZFCP_ERP_FAILED         - action finished unsuccessfully
2346  */
2347 static int
2348 zfcp_erp_port_strategy_open_port(struct zfcp_erp_action *erp_action)
2349 {
2350         int retval;
2351
2352         retval = zfcp_fsf_open_port(erp_action);
2353         if (retval == -ENOMEM) {
2354                 retval = ZFCP_ERP_NOMEM;
2355                 goto out;
2356         }
2357         erp_action->step = ZFCP_ERP_STEP_PORT_OPENING;
2358         if (retval != 0) {
2359                 /* could not send 'open', fail */
2360                 retval = ZFCP_ERP_FAILED;
2361                 goto out;
2362         }
2363         retval = ZFCP_ERP_CONTINUES;
2364  out:
2365         return retval;
2366 }
2367
2368 /*
2369  * function:
2370  *
2371  * purpose:
2372  *
2373  * returns:     ZFCP_ERP_CONTINUES      - action continues (asynchronously)
2374  *              ZFCP_ERP_FAILED         - action finished unsuccessfully
2375  */
2376 static int
2377 zfcp_erp_port_strategy_open_common_lookup(struct zfcp_erp_action *erp_action)
2378 {
2379         int retval;
2380
2381         retval = zfcp_fc_ns_gid_pn_request(erp_action);
2382         if (retval == -ENOMEM) {
2383                 retval = ZFCP_ERP_NOMEM;
2384                 goto out;
2385         }
2386         erp_action->step = ZFCP_ERP_STEP_NAMESERVER_LOOKUP;
2387         if (retval != 0) {
2388                 /* could not send nameserver request, fail */
2389                 retval = ZFCP_ERP_FAILED;
2390                 goto out;
2391         }
2392         retval = ZFCP_ERP_CONTINUES;
2393  out:
2394         return retval;
2395 }
2396
2397 /*
2398  * function:
2399  *
2400  * purpose:     this routine executes the 'Reopen Unit' action
2401  *              currently no retries
2402  *
2403  * returns:     ZFCP_ERP_CONTINUES      - action continues (asynchronously)
2404  *              ZFCP_ERP_SUCCEEDED      - action finished successfully
2405  *              ZFCP_ERP_FAILED         - action finished unsuccessfully
2406  */
2407 static int
2408 zfcp_erp_unit_strategy(struct zfcp_erp_action *erp_action)
2409 {
2410         int retval = ZFCP_ERP_FAILED;
2411         struct zfcp_unit *unit = erp_action->unit;
2412
2413         switch (erp_action->step) {
2414
2415                 /*
2416                  * FIXME:
2417                  * the ULP spec. begs for waiting for oustanding commands
2418                  */
2419         case ZFCP_ERP_STEP_UNINITIALIZED:
2420                 zfcp_erp_unit_strategy_clearstati(unit);
2421                 if (atomic_test_mask(ZFCP_STATUS_COMMON_OPEN, &unit->status)) {
2422                         ZFCP_LOG_DEBUG("unit 0x%016Lx is open -> "
2423                                        "trying close\n", unit->fcp_lun);
2424                         retval = zfcp_erp_unit_strategy_close(erp_action);
2425                         break;
2426                 }
2427                 /* else it's already closed, fall through */
2428         case ZFCP_ERP_STEP_UNIT_CLOSING:
2429                 if (atomic_test_mask(ZFCP_STATUS_COMMON_OPEN, &unit->status)) {
2430                         ZFCP_LOG_DEBUG("close failed for unit 0x%016Lx\n",
2431                                        unit->fcp_lun);
2432                         retval = ZFCP_ERP_FAILED;
2433                 } else {
2434                         if (erp_action->status & ZFCP_STATUS_ERP_CLOSE_ONLY)
2435                                 retval = ZFCP_ERP_EXIT;
2436                         else {
2437                                 ZFCP_LOG_DEBUG("unit 0x%016Lx is not open -> "
2438                                                "trying open\n", unit->fcp_lun);
2439                                 retval =
2440                                     zfcp_erp_unit_strategy_open(erp_action);
2441                         }
2442                 }
2443                 break;
2444
2445         case ZFCP_ERP_STEP_UNIT_OPENING:
2446                 if (atomic_test_mask(ZFCP_STATUS_COMMON_OPEN, &unit->status)) {
2447                         ZFCP_LOG_DEBUG("unit 0x%016Lx is open\n",
2448                                        unit->fcp_lun);
2449                         retval = ZFCP_ERP_SUCCEEDED;
2450                 } else {
2451                         ZFCP_LOG_DEBUG("open failed for unit 0x%016Lx\n",
2452                                        unit->fcp_lun);
2453                         retval = ZFCP_ERP_FAILED;
2454                 }
2455                 break;
2456         }
2457
2458         return retval;
2459 }
2460
2461 static int
2462 zfcp_erp_unit_strategy_clearstati(struct zfcp_unit *unit)
2463 {
2464         int retval = 0;
2465
2466         atomic_clear_mask(ZFCP_STATUS_COMMON_OPENING |
2467                           ZFCP_STATUS_COMMON_CLOSING |
2468                           ZFCP_STATUS_COMMON_ACCESS_DENIED |
2469                           ZFCP_STATUS_UNIT_SHARED |
2470                           ZFCP_STATUS_UNIT_READONLY,
2471                           &unit->status);
2472
2473         return retval;
2474 }
2475
2476 /*
2477  * function:
2478  *
2479  * purpose:
2480  *
2481  * returns:     ZFCP_ERP_CONTINUES      - action continues (asynchronously)
2482  *              ZFCP_ERP_FAILED         - action finished unsuccessfully
2483  */
2484 static int
2485 zfcp_erp_unit_strategy_close(struct zfcp_erp_action *erp_action)
2486 {
2487         int retval;
2488
2489         retval = zfcp_fsf_close_unit(erp_action);
2490         if (retval == -ENOMEM) {
2491                 retval = ZFCP_ERP_NOMEM;
2492                 goto out;
2493         }
2494         erp_action->step = ZFCP_ERP_STEP_UNIT_CLOSING;
2495         if (retval != 0) {
2496                 /* could not send 'close', fail */
2497                 retval = ZFCP_ERP_FAILED;
2498                 goto out;
2499         }
2500         retval = ZFCP_ERP_CONTINUES;
2501
2502  out:
2503         return retval;
2504 }
2505
2506 /*
2507  * function:
2508  *
2509  * purpose:
2510  *
2511  * returns:     ZFCP_ERP_CONTINUES      - action continues (asynchronously)
2512  *              ZFCP_ERP_FAILED         - action finished unsuccessfully
2513  */
2514 static int
2515 zfcp_erp_unit_strategy_open(struct zfcp_erp_action *erp_action)
2516 {
2517         int retval;
2518
2519         retval = zfcp_fsf_open_unit(erp_action);
2520         if (retval == -ENOMEM) {
2521                 retval = ZFCP_ERP_NOMEM;
2522                 goto out;
2523         }
2524         erp_action->step = ZFCP_ERP_STEP_UNIT_OPENING;
2525         if (retval != 0) {
2526                 /* could not send 'open', fail */
2527                 retval = ZFCP_ERP_FAILED;
2528                 goto out;
2529         }
2530         retval = ZFCP_ERP_CONTINUES;
2531  out:
2532         return retval;
2533 }
2534
2535 void zfcp_erp_start_timer(struct zfcp_fsf_req *fsf_req)
2536 {
2537         BUG_ON(!fsf_req->erp_action);
2538         fsf_req->timer.function = zfcp_erp_timeout_handler;
2539         fsf_req->timer.data = (unsigned long) fsf_req->erp_action;
2540         fsf_req->timer.expires = jiffies + ZFCP_ERP_FSFREQ_TIMEOUT;
2541         add_timer(&fsf_req->timer);
2542 }
2543
2544 /*
2545  * function:
2546  *
2547  * purpose:     enqueue the specified error recovery action, if needed
2548  *
2549  * returns:
2550  */
2551 static int zfcp_erp_action_enqueue(int want, struct zfcp_adapter *adapter,
2552                                    struct zfcp_port *port,
2553                                    struct zfcp_unit *unit, u8 id, void *ref)
2554 {
2555         int retval = 1, need = want;
2556         struct zfcp_erp_action *erp_action = NULL;
2557         u32 status = 0;
2558
2559         /*
2560          * We need some rules here which check whether we really need
2561          * this action or whether we should just drop it.
2562          * E.g. if there is a unfinished 'Reopen Port' request then we drop a
2563          * 'Reopen Unit' request for an associated unit since we can't
2564          * satisfy this request now. A 'Reopen Port' action will trigger
2565          * 'Reopen Unit' actions when it completes.
2566          * Thus, there are only actions in the queue which can immediately be
2567          * executed. This makes the processing of the action queue more
2568          * efficient.
2569          */
2570
2571         if (!atomic_test_mask(ZFCP_STATUS_ADAPTER_ERP_THREAD_UP,
2572                               &adapter->status))
2573                 return -EIO;
2574
2575         /* check whether we really need this */
2576         switch (want) {
2577         case ZFCP_ERP_ACTION_REOPEN_UNIT:
2578                 if (atomic_test_mask
2579                     (ZFCP_STATUS_COMMON_ERP_INUSE, &unit->status)) {
2580                         goto out;
2581                 }
2582                 if (!atomic_test_mask
2583                     (ZFCP_STATUS_COMMON_RUNNING, &port->status) ||
2584                     atomic_test_mask
2585                     (ZFCP_STATUS_COMMON_ERP_FAILED, &port->status)) {
2586                         goto out;
2587                 }
2588                 if (!atomic_test_mask
2589                     (ZFCP_STATUS_COMMON_UNBLOCKED, &port->status))
2590                         need = ZFCP_ERP_ACTION_REOPEN_PORT;
2591                 /* fall through !!! */
2592
2593         case ZFCP_ERP_ACTION_REOPEN_PORT:
2594                 if (atomic_test_mask
2595                     (ZFCP_STATUS_COMMON_ERP_INUSE, &port->status)) {
2596                         goto out;
2597                 }
2598                 /* fall through !!! */
2599
2600         case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
2601                 if (atomic_test_mask(ZFCP_STATUS_COMMON_ERP_INUSE,
2602                                      &port->status)) {
2603                         if (port->erp_action.action !=
2604                             ZFCP_ERP_ACTION_REOPEN_PORT_FORCED) {
2605                                 ZFCP_LOG_INFO("dropped erp action %i (port "
2606                                               "0x%016Lx, action in use: %i)\n",
2607                                               want, port->wwpn,
2608                                               port->erp_action.action);
2609                         }
2610                         goto out;
2611                 }
2612                 if (!atomic_test_mask
2613                     (ZFCP_STATUS_COMMON_RUNNING, &adapter->status) ||
2614                     atomic_test_mask
2615                     (ZFCP_STATUS_COMMON_ERP_FAILED, &adapter->status)) {
2616                         goto out;
2617                 }
2618                 if (!atomic_test_mask
2619                     (ZFCP_STATUS_COMMON_UNBLOCKED, &adapter->status))
2620                         need = ZFCP_ERP_ACTION_REOPEN_ADAPTER;
2621                 /* fall through !!! */
2622
2623         case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
2624                 if (atomic_test_mask
2625                     (ZFCP_STATUS_COMMON_ERP_INUSE, &adapter->status)) {
2626                         goto out;
2627                 }
2628                 break;
2629
2630         default:
2631                 ZFCP_LOG_NORMAL("bug: unknown erp action requested "
2632                                 "on adapter %s (action=%d)\n",
2633                                 zfcp_get_busid_by_adapter(adapter), want);
2634                 goto out;
2635         }
2636
2637         /* check whether we need something stronger first */
2638         if (need) {
2639                 ZFCP_LOG_DEBUG("stronger erp action %d needed before "
2640                                "erp action %d on adapter %s\n",
2641                                need, want, zfcp_get_busid_by_adapter(adapter));
2642         }
2643
2644         /* mark adapter to have some error recovery pending */
2645         atomic_set_mask(ZFCP_STATUS_ADAPTER_ERP_PENDING, &adapter->status);
2646
2647         /* setup error recovery action */
2648         switch (need) {
2649
2650         case ZFCP_ERP_ACTION_REOPEN_UNIT:
2651                 zfcp_unit_get(unit);
2652                 atomic_set_mask(ZFCP_STATUS_COMMON_ERP_INUSE, &unit->status);
2653                 erp_action = &unit->erp_action;
2654                 if (!atomic_test_mask
2655                     (ZFCP_STATUS_COMMON_RUNNING, &unit->status))
2656                         status = ZFCP_STATUS_ERP_CLOSE_ONLY;
2657                 break;
2658
2659         case ZFCP_ERP_ACTION_REOPEN_PORT:
2660         case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
2661                 zfcp_port_get(port);
2662                 zfcp_erp_action_dismiss_port(port);
2663                 atomic_set_mask(ZFCP_STATUS_COMMON_ERP_INUSE, &port->status);
2664                 erp_action = &port->erp_action;
2665                 if (!atomic_test_mask
2666                     (ZFCP_STATUS_COMMON_RUNNING, &port->status))
2667                         status = ZFCP_STATUS_ERP_CLOSE_ONLY;
2668                 break;
2669
2670         case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
2671                 zfcp_adapter_get(adapter);
2672                 zfcp_erp_action_dismiss_adapter(adapter);
2673                 atomic_set_mask(ZFCP_STATUS_COMMON_ERP_INUSE, &adapter->status);
2674                 erp_action = &adapter->erp_action;
2675                 if (!atomic_test_mask
2676                     (ZFCP_STATUS_COMMON_RUNNING, &adapter->status))
2677                         status = ZFCP_STATUS_ERP_CLOSE_ONLY;
2678                 break;
2679         }
2680
2681         memset(erp_action, 0, sizeof (struct zfcp_erp_action));
2682         erp_action->adapter = adapter;
2683         erp_action->port = port;
2684         erp_action->unit = unit;
2685         erp_action->action = need;
2686         erp_action->status = status;
2687
2688         ++adapter->erp_total_count;
2689
2690         /* finally put it into 'ready' queue and kick erp thread */
2691         list_add_tail(&erp_action->list, &adapter->erp_ready_head);
2692         up(&adapter->erp_ready_sem);
2693         zfcp_rec_dbf_event_thread(1, adapter);
2694         retval = 0;
2695  out:
2696         zfcp_rec_dbf_event_trigger(id, ref, want, need, erp_action,
2697                                    adapter, port, unit);
2698         return retval;
2699 }
2700
2701 static int
2702 zfcp_erp_action_dequeue(struct zfcp_erp_action *erp_action)
2703 {
2704         int retval = 0;
2705         struct zfcp_adapter *adapter = erp_action->adapter;
2706
2707         --adapter->erp_total_count;
2708         if (erp_action->status & ZFCP_STATUS_ERP_LOWMEM) {
2709                 --adapter->erp_low_mem_count;
2710                 erp_action->status &= ~ZFCP_STATUS_ERP_LOWMEM;
2711         }
2712
2713         list_del(&erp_action->list);
2714         zfcp_rec_dbf_event_action(144, erp_action);
2715
2716         switch (erp_action->action) {
2717         case ZFCP_ERP_ACTION_REOPEN_UNIT:
2718                 atomic_clear_mask(ZFCP_STATUS_COMMON_ERP_INUSE,
2719                                   &erp_action->unit->status);
2720                 break;
2721         case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
2722         case ZFCP_ERP_ACTION_REOPEN_PORT:
2723                 atomic_clear_mask(ZFCP_STATUS_COMMON_ERP_INUSE,
2724                                   &erp_action->port->status);
2725                 break;
2726         case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
2727                 atomic_clear_mask(ZFCP_STATUS_COMMON_ERP_INUSE,
2728                                   &erp_action->adapter->status);
2729                 break;
2730         default:
2731                 /* bug */
2732                 break;
2733         }
2734         return retval;
2735 }
2736
2737 /**
2738  * zfcp_erp_action_cleanup
2739  *
2740  * Register unit with scsi stack if appropriate and fix reference counts.
2741  * Note: Temporary units are not registered with scsi stack.
2742  */
2743 static void
2744 zfcp_erp_action_cleanup(int action, struct zfcp_adapter *adapter,
2745                         struct zfcp_port *port, struct zfcp_unit *unit,
2746                         int result)
2747 {
2748         switch (action) {
2749         case ZFCP_ERP_ACTION_REOPEN_UNIT:
2750                 if ((result == ZFCP_ERP_SUCCEEDED)
2751                     && (!atomic_test_mask(ZFCP_STATUS_UNIT_TEMPORARY,
2752                                           &unit->status))
2753                     && !unit->device
2754                     && port->rport) {
2755                         atomic_set_mask(ZFCP_STATUS_UNIT_REGISTERED,
2756                                         &unit->status);
2757                         if (atomic_test_mask(ZFCP_STATUS_UNIT_SCSI_WORK_PENDING,
2758                                              &unit->status) == 0)
2759                                 zfcp_erp_schedule_work(unit);
2760                 }
2761                 zfcp_unit_put(unit);
2762                 break;
2763         case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
2764         case ZFCP_ERP_ACTION_REOPEN_PORT:
2765                 if (atomic_test_mask(ZFCP_STATUS_PORT_NO_WWPN,
2766                                      &port->status)) {
2767                         zfcp_port_put(port);
2768                         break;
2769                 }
2770
2771                 if ((result == ZFCP_ERP_SUCCEEDED)
2772                     && !port->rport) {
2773                         struct fc_rport_identifiers ids;
2774                         ids.node_name = port->wwnn;
2775                         ids.port_name = port->wwpn;
2776                         ids.port_id = port->d_id;
2777                         ids.roles = FC_RPORT_ROLE_FCP_TARGET;
2778                         port->rport =
2779                                 fc_remote_port_add(adapter->scsi_host, 0, &ids);
2780                         if (!port->rport)
2781                                 ZFCP_LOG_NORMAL("failed registration of rport"
2782                                                 "(adapter %s, wwpn=0x%016Lx)\n",
2783                                                 zfcp_get_busid_by_port(port),
2784                                                 port->wwpn);
2785                         else {
2786                                 scsi_target_unblock(&port->rport->dev);
2787                                 port->rport->maxframe_size = port->maxframe_size;
2788                                 port->rport->supported_classes =
2789                                         port->supported_classes;
2790                         }
2791                 }
2792                 if ((result != ZFCP_ERP_SUCCEEDED) && port->rport) {
2793                         fc_remote_port_delete(port->rport);
2794                         port->rport = NULL;
2795                 }
2796                 zfcp_port_put(port);
2797                 break;
2798         case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
2799                 if (result != ZFCP_ERP_SUCCEEDED) {
2800                         list_for_each_entry(port, &adapter->port_list_head, list)
2801                                 if (port->rport &&
2802                                     !atomic_test_mask(ZFCP_STATUS_PORT_WKA,
2803                                                       &port->status)) {
2804                                         fc_remote_port_delete(port->rport);
2805                                         port->rport = NULL;
2806                                 }
2807                 }
2808                 zfcp_adapter_put(adapter);
2809                 break;
2810         default:
2811                 break;
2812         }
2813 }
2814
2815
2816 static void zfcp_erp_action_dismiss_adapter(struct zfcp_adapter *adapter)
2817 {
2818         struct zfcp_port *port;
2819
2820         if (atomic_test_mask(ZFCP_STATUS_COMMON_ERP_INUSE, &adapter->status))
2821                 zfcp_erp_action_dismiss(&adapter->erp_action);
2822         else
2823                 list_for_each_entry(port, &adapter->port_list_head, list)
2824                     zfcp_erp_action_dismiss_port(port);
2825 }
2826
2827 static void zfcp_erp_action_dismiss_port(struct zfcp_port *port)
2828 {
2829         struct zfcp_unit *unit;
2830
2831         if (atomic_test_mask(ZFCP_STATUS_COMMON_ERP_INUSE, &port->status))
2832                 zfcp_erp_action_dismiss(&port->erp_action);
2833         else
2834                 list_for_each_entry(unit, &port->unit_list_head, list)
2835                     zfcp_erp_action_dismiss_unit(unit);
2836 }
2837
2838 static void zfcp_erp_action_dismiss_unit(struct zfcp_unit *unit)
2839 {
2840         if (atomic_test_mask(ZFCP_STATUS_COMMON_ERP_INUSE, &unit->status))
2841                 zfcp_erp_action_dismiss(&unit->erp_action);
2842 }
2843
2844 static void zfcp_erp_action_to_running(struct zfcp_erp_action *erp_action)
2845 {
2846         list_move(&erp_action->list, &erp_action->adapter->erp_running_head);
2847         zfcp_rec_dbf_event_action(145, erp_action);
2848 }
2849
2850 static void zfcp_erp_action_to_ready(struct zfcp_erp_action *erp_action)
2851 {
2852         list_move(&erp_action->list, &erp_action->adapter->erp_ready_head);
2853         zfcp_rec_dbf_event_action(146, erp_action);
2854 }
2855
2856 void zfcp_erp_port_boxed(struct zfcp_port *port, u8 id, void *ref)
2857 {
2858         unsigned long flags;
2859
2860         read_lock_irqsave(&zfcp_data.config_lock, flags);
2861         zfcp_erp_modify_port_status(port, id, ref,
2862                                     ZFCP_STATUS_COMMON_ACCESS_BOXED, ZFCP_SET);
2863         read_unlock_irqrestore(&zfcp_data.config_lock, flags);
2864         zfcp_erp_port_reopen(port, ZFCP_STATUS_COMMON_ERP_FAILED, id, ref);
2865 }
2866
2867 void zfcp_erp_unit_boxed(struct zfcp_unit *unit, u8 id, void *ref)
2868 {
2869         zfcp_erp_modify_unit_status(unit, id, ref,
2870                                     ZFCP_STATUS_COMMON_ACCESS_BOXED, ZFCP_SET);
2871         zfcp_erp_unit_reopen(unit, ZFCP_STATUS_COMMON_ERP_FAILED, id, ref);
2872 }
2873
2874 void zfcp_erp_port_access_denied(struct zfcp_port *port, u8 id, void *ref)
2875 {
2876         unsigned long flags;
2877
2878         read_lock_irqsave(&zfcp_data.config_lock, flags);
2879         zfcp_erp_modify_port_status(port, id, ref,
2880                                     ZFCP_STATUS_COMMON_ERP_FAILED |
2881                                     ZFCP_STATUS_COMMON_ACCESS_DENIED, ZFCP_SET);
2882         read_unlock_irqrestore(&zfcp_data.config_lock, flags);
2883 }
2884
2885 void zfcp_erp_unit_access_denied(struct zfcp_unit *unit, u8 id, void *ref)
2886 {
2887         zfcp_erp_modify_unit_status(unit, id, ref,
2888                                     ZFCP_STATUS_COMMON_ERP_FAILED |
2889                                     ZFCP_STATUS_COMMON_ACCESS_DENIED, ZFCP_SET);
2890 }
2891
2892 void zfcp_erp_adapter_access_changed(struct zfcp_adapter *adapter, u8 id,
2893                                      void *ref)
2894 {
2895         struct zfcp_port *port;
2896         unsigned long flags;
2897
2898         if (adapter->connection_features & FSF_FEATURE_NPIV_MODE)
2899                 return;
2900
2901         read_lock_irqsave(&zfcp_data.config_lock, flags);
2902         if (adapter->nameserver_port)
2903                 zfcp_erp_port_access_changed(adapter->nameserver_port, id, ref);
2904         list_for_each_entry(port, &adapter->port_list_head, list)
2905                 if (port != adapter->nameserver_port)
2906                         zfcp_erp_port_access_changed(port, id, ref);
2907         read_unlock_irqrestore(&zfcp_data.config_lock, flags);
2908 }
2909
2910 void zfcp_erp_port_access_changed(struct zfcp_port *port, u8 id, void *ref)
2911 {
2912         struct zfcp_adapter *adapter = port->adapter;
2913         struct zfcp_unit *unit;
2914
2915         if (!atomic_test_mask(ZFCP_STATUS_COMMON_ACCESS_DENIED,
2916                               &port->status) &&
2917             !atomic_test_mask(ZFCP_STATUS_COMMON_ACCESS_BOXED,
2918                               &port->status)) {
2919                 if (!atomic_test_mask(ZFCP_STATUS_PORT_WKA, &port->status))
2920                         list_for_each_entry(unit, &port->unit_list_head, list)
2921                                 zfcp_erp_unit_access_changed(unit, id, ref);
2922                 return;
2923         }
2924
2925         ZFCP_LOG_NORMAL("reopen of port 0x%016Lx on adapter %s "
2926                         "(due to ACT update)\n",
2927                         port->wwpn, zfcp_get_busid_by_adapter(adapter));
2928         if (zfcp_erp_port_reopen(port, ZFCP_STATUS_COMMON_ERP_FAILED, id, ref))
2929                 ZFCP_LOG_NORMAL("failed reopen of port"
2930                                 "(adapter %s, wwpn=0x%016Lx)\n",
2931                                 zfcp_get_busid_by_adapter(adapter), port->wwpn);
2932 }
2933
2934 void zfcp_erp_unit_access_changed(struct zfcp_unit *unit, u8 id, void *ref)
2935 {
2936         struct zfcp_adapter *adapter = unit->port->adapter;
2937
2938         if (!atomic_test_mask(ZFCP_STATUS_COMMON_ACCESS_DENIED,
2939                               &unit->status) &&
2940             !atomic_test_mask(ZFCP_STATUS_COMMON_ACCESS_BOXED,
2941                               &unit->status))
2942                 return;
2943
2944         ZFCP_LOG_NORMAL("reopen of unit 0x%016Lx on port 0x%016Lx "
2945                         " on adapter %s (due to ACT update)\n",
2946                         unit->fcp_lun, unit->port->wwpn,
2947                         zfcp_get_busid_by_adapter(adapter));
2948         if (zfcp_erp_unit_reopen(unit, ZFCP_STATUS_COMMON_ERP_FAILED, id, ref))
2949                 ZFCP_LOG_NORMAL("failed reopen of unit (adapter %s, "
2950                                 "wwpn=0x%016Lx, fcp_lun=0x%016Lx)\n",
2951                                 zfcp_get_busid_by_adapter(adapter),
2952                                 unit->port->wwpn, unit->fcp_lun);
2953 }
2954
2955 #undef ZFCP_LOG_AREA