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