Merge master.kernel.org:/pub/scm/linux/kernel/git/acme/net-2.6
[linux-2.6] / drivers / s390 / scsi / zfcp_erp.c
1 /* 
2  * This file is part of the zfcp device driver for
3  * FCP adapters for IBM System z9 and zSeries.
4  *
5  * (C) Copyright IBM Corp. 2002, 2006
6  * 
7  * This program is free software; you can redistribute it and/or modify 
8  * it under the terms of the GNU General Public License as published by 
9  * the Free Software Foundation; either version 2, or (at your option) 
10  * any later version. 
11  * 
12  * This program is distributed in the hope that it will be useful, 
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of 
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
15  * GNU General Public License for more details. 
16  * 
17  * You should have received a copy of the GNU General Public License 
18  * along with this program; if not, write to the Free Software 
19  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 
20  */
21
22 #define ZFCP_LOG_AREA                   ZFCP_LOG_AREA_ERP
23
24 #include "zfcp_ext.h"
25
26 static int zfcp_erp_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 int
845 zfcp_erp_strategy_check_fsfreq(struct zfcp_erp_action *erp_action)
846 {
847         int retval = 0;
848         struct zfcp_fsf_req *fsf_req = NULL;
849         struct zfcp_adapter *adapter = erp_action->adapter;
850
851         if (erp_action->fsf_req) {
852                 /* take lock to ensure that request is not deleted meanwhile */
853                 spin_lock(&adapter->req_list_lock);
854                 if ((!zfcp_reqlist_ismember(adapter,
855                                             erp_action->fsf_req->req_id)) &&
856                     (fsf_req->erp_action == erp_action)) {
857                         /* fsf_req still exists */
858                         debug_text_event(adapter->erp_dbf, 3, "a_ca_req");
859                         debug_event(adapter->erp_dbf, 3, &fsf_req,
860                                     sizeof (unsigned long));
861                         /* dismiss fsf_req of timed out/dismissed erp_action */
862                         if (erp_action->status & (ZFCP_STATUS_ERP_DISMISSED |
863                                                   ZFCP_STATUS_ERP_TIMEDOUT)) {
864                                 debug_text_event(adapter->erp_dbf, 3,
865                                                  "a_ca_disreq");
866                                 fsf_req->status |= ZFCP_STATUS_FSFREQ_DISMISSED;
867                         }
868                         if (erp_action->status & ZFCP_STATUS_ERP_TIMEDOUT) {
869                                 ZFCP_LOG_NORMAL("error: erp step timed out "
870                                                 "(action=%d, fsf_req=%p)\n ",
871                                                 erp_action->action,
872                                                 erp_action->fsf_req);
873                         }
874                         /*
875                          * If fsf_req is neither dismissed nor completed
876                          * then keep it running asynchronously and don't mess
877                          * with the association of erp_action and fsf_req.
878                          */
879                         if (fsf_req->status & (ZFCP_STATUS_FSFREQ_COMPLETED |
880                                                ZFCP_STATUS_FSFREQ_DISMISSED)) {
881                                 /* forget about association between fsf_req
882                                    and erp_action */
883                                 fsf_req->erp_action = NULL;
884                                 erp_action->fsf_req = NULL;
885                         }
886                 } else {
887                         debug_text_event(adapter->erp_dbf, 3, "a_ca_gonereq");
888                         /*
889                          * even if this fsf_req has gone, forget about
890                          * association between erp_action and fsf_req
891                          */
892                         erp_action->fsf_req = NULL;
893                 }
894                 spin_unlock(&adapter->req_list_lock);
895         } else
896                 debug_text_event(adapter->erp_dbf, 3, "a_ca_noreq");
897
898         return retval;
899 }
900
901 /**
902  * zfcp_erp_async_handler_nolock - complete erp_action
903  *
904  * Used for normal completion, time-out, dismissal and failure after
905  * low memory condition.
906  */
907 static void zfcp_erp_async_handler_nolock(struct zfcp_erp_action *erp_action,
908                                           unsigned long set_mask)
909 {
910         struct zfcp_adapter *adapter = erp_action->adapter;
911
912         if (zfcp_erp_action_exists(erp_action) == ZFCP_ERP_ACTION_RUNNING) {
913                 debug_text_event(adapter->erp_dbf, 2, "a_asyh_ex");
914                 debug_event(adapter->erp_dbf, 2, &erp_action->action,
915                             sizeof (int));
916                 erp_action->status |= set_mask;
917                 zfcp_erp_action_ready(erp_action);
918         } else {
919                 /* action is ready or gone - nothing to do */
920                 debug_text_event(adapter->erp_dbf, 3, "a_asyh_gone");
921                 debug_event(adapter->erp_dbf, 3, &erp_action->action,
922                             sizeof (int));
923         }
924 }
925
926 /**
927  * zfcp_erp_async_handler - wrapper for erp_async_handler_nolock w/ locking
928  */
929 void zfcp_erp_async_handler(struct zfcp_erp_action *erp_action,
930                             unsigned long set_mask)
931 {
932         struct zfcp_adapter *adapter = erp_action->adapter;
933         unsigned long flags;
934
935         write_lock_irqsave(&adapter->erp_lock, flags);
936         zfcp_erp_async_handler_nolock(erp_action, set_mask);
937         write_unlock_irqrestore(&adapter->erp_lock, flags);
938 }
939
940 /*
941  * purpose:     is called for erp_action which was slept waiting for
942  *              memory becoming avaliable,
943  *              will trigger that this action will be continued
944  */
945 static void
946 zfcp_erp_memwait_handler(unsigned long data)
947 {
948         struct zfcp_erp_action *erp_action = (struct zfcp_erp_action *) data;
949         struct zfcp_adapter *adapter = erp_action->adapter;
950
951         debug_text_event(adapter->erp_dbf, 2, "a_mwh");
952         debug_event(adapter->erp_dbf, 2, &erp_action->action, sizeof (int));
953
954         zfcp_erp_async_handler(erp_action, 0);
955 }
956
957 /*
958  * purpose:     is called if an asynchronous erp step timed out,
959  *              action gets an appropriate flag and will be processed
960  *              accordingly
961  */
962 void zfcp_erp_timeout_handler(unsigned long data)
963 {
964         struct zfcp_erp_action *erp_action = (struct zfcp_erp_action *) data;
965         struct zfcp_adapter *adapter = erp_action->adapter;
966
967         debug_text_event(adapter->erp_dbf, 2, "a_th");
968         debug_event(adapter->erp_dbf, 2, &erp_action->action, sizeof (int));
969
970         zfcp_erp_async_handler(erp_action, ZFCP_STATUS_ERP_TIMEDOUT);
971 }
972
973 /**
974  * zfcp_erp_action_dismiss - dismiss an erp_action
975  *
976  * adapter->erp_lock must be held
977  * 
978  * Dismissal of an erp_action is usually required if an erp_action of
979  * higher priority is generated.
980  */
981 static void zfcp_erp_action_dismiss(struct zfcp_erp_action *erp_action)
982 {
983         struct zfcp_adapter *adapter = erp_action->adapter;
984
985         debug_text_event(adapter->erp_dbf, 2, "a_adis");
986         debug_event(adapter->erp_dbf, 2, &erp_action->action, sizeof (int));
987
988         zfcp_erp_async_handler_nolock(erp_action, ZFCP_STATUS_ERP_DISMISSED);
989 }
990
991 int
992 zfcp_erp_thread_setup(struct zfcp_adapter *adapter)
993 {
994         int retval = 0;
995
996         atomic_clear_mask(ZFCP_STATUS_ADAPTER_ERP_THREAD_UP, &adapter->status);
997
998         retval = kernel_thread(zfcp_erp_thread, adapter, SIGCHLD);
999         if (retval < 0) {
1000                 ZFCP_LOG_NORMAL("error: creation of erp thread failed for "
1001                                 "adapter %s\n",
1002                                 zfcp_get_busid_by_adapter(adapter));
1003                 debug_text_event(adapter->erp_dbf, 5, "a_thset_fail");
1004         } else {
1005                 wait_event(adapter->erp_thread_wqh,
1006                            atomic_test_mask(ZFCP_STATUS_ADAPTER_ERP_THREAD_UP,
1007                                             &adapter->status));
1008                 debug_text_event(adapter->erp_dbf, 5, "a_thset_ok");
1009         }
1010
1011         return (retval < 0);
1012 }
1013
1014 /*
1015  * function:    
1016  *
1017  * purpose:     
1018  *
1019  * returns:
1020  *
1021  * context:     process (i.e. proc-fs or rmmod/insmod)
1022  *
1023  * note:        The caller of this routine ensures that the specified
1024  *              adapter has been shut down and that this operation
1025  *              has been completed. Thus, there are no pending erp_actions
1026  *              which would need to be handled here.
1027  */
1028 int
1029 zfcp_erp_thread_kill(struct zfcp_adapter *adapter)
1030 {
1031         int retval = 0;
1032
1033         atomic_set_mask(ZFCP_STATUS_ADAPTER_ERP_THREAD_KILL, &adapter->status);
1034         up(&adapter->erp_ready_sem);
1035
1036         wait_event(adapter->erp_thread_wqh,
1037                    !atomic_test_mask(ZFCP_STATUS_ADAPTER_ERP_THREAD_UP,
1038                                      &adapter->status));
1039
1040         atomic_clear_mask(ZFCP_STATUS_ADAPTER_ERP_THREAD_KILL,
1041                           &adapter->status);
1042
1043         debug_text_event(adapter->erp_dbf, 5, "a_thki_ok");
1044
1045         return retval;
1046 }
1047
1048 /*
1049  * purpose:     is run as a kernel thread,
1050  *              goes through list of error recovery actions of associated adapter
1051  *              and delegates single action to execution
1052  *
1053  * returns:     0
1054  */
1055 static int
1056 zfcp_erp_thread(void *data)
1057 {
1058         struct zfcp_adapter *adapter = (struct zfcp_adapter *) data;
1059         struct list_head *next;
1060         struct zfcp_erp_action *erp_action;
1061         unsigned long flags;
1062
1063         daemonize("zfcperp%s", zfcp_get_busid_by_adapter(adapter));
1064         /* Block all signals */
1065         siginitsetinv(&current->blocked, 0);
1066         atomic_set_mask(ZFCP_STATUS_ADAPTER_ERP_THREAD_UP, &adapter->status);
1067         debug_text_event(adapter->erp_dbf, 5, "a_th_run");
1068         wake_up(&adapter->erp_thread_wqh);
1069
1070         while (!atomic_test_mask(ZFCP_STATUS_ADAPTER_ERP_THREAD_KILL,
1071                                  &adapter->status)) {
1072
1073                 write_lock_irqsave(&adapter->erp_lock, flags);
1074                 next = adapter->erp_ready_head.prev;
1075                 write_unlock_irqrestore(&adapter->erp_lock, flags);
1076
1077                 if (next != &adapter->erp_ready_head) {
1078                         erp_action =
1079                             list_entry(next, struct zfcp_erp_action, list);
1080                         /*
1081                          * process action (incl. [re]moving it
1082                          * from 'ready' queue)
1083                          */
1084                         zfcp_erp_strategy(erp_action);
1085                 }
1086
1087                 /*
1088                  * sleep as long as there is nothing to do, i.e.
1089                  * no action in 'ready' queue to be processed and
1090                  * thread is not to be killed
1091                  */
1092                 down_interruptible(&adapter->erp_ready_sem);
1093                 debug_text_event(adapter->erp_dbf, 5, "a_th_woken");
1094         }
1095
1096         atomic_clear_mask(ZFCP_STATUS_ADAPTER_ERP_THREAD_UP, &adapter->status);
1097         debug_text_event(adapter->erp_dbf, 5, "a_th_stop");
1098         wake_up(&adapter->erp_thread_wqh);
1099
1100         return 0;
1101 }
1102
1103 /*
1104  * function:    
1105  *
1106  * purpose:     drives single error recovery action and schedules higher and
1107  *              subordinate actions, if necessary
1108  *
1109  * returns:     ZFCP_ERP_CONTINUES      - action continues (asynchronously)
1110  *              ZFCP_ERP_SUCCEEDED      - action finished successfully (deqd)
1111  *              ZFCP_ERP_FAILED         - action finished unsuccessfully (deqd)
1112  *              ZFCP_ERP_EXIT           - action finished (dequeued), offline
1113  *              ZFCP_ERP_DISMISSED      - action canceled (dequeued)
1114  */
1115 static int
1116 zfcp_erp_strategy(struct zfcp_erp_action *erp_action)
1117 {
1118         int retval = 0;
1119         struct zfcp_adapter *adapter = erp_action->adapter;
1120         struct zfcp_port *port = erp_action->port;
1121         struct zfcp_unit *unit = erp_action->unit;
1122         int action = erp_action->action;
1123         u32 status = erp_action->status;
1124         unsigned long flags;
1125
1126         /* serialise dismissing, timing out, moving, enqueueing */
1127         read_lock_irqsave(&zfcp_data.config_lock, flags);
1128         write_lock(&adapter->erp_lock);
1129
1130         /* dequeue dismissed action and leave, if required */
1131         retval = zfcp_erp_strategy_check_action(erp_action, retval);
1132         if (retval == ZFCP_ERP_DISMISSED) {
1133                 debug_text_event(adapter->erp_dbf, 4, "a_st_dis1");
1134                 goto unlock;
1135         }
1136
1137         /*
1138          * move action to 'running' queue before processing it
1139          * (to avoid a race condition regarding moving the
1140          * action to the 'running' queue and back)
1141          */
1142         zfcp_erp_action_to_running(erp_action);
1143
1144         /*
1145          * try to process action as far as possible,
1146          * no lock to allow for blocking operations (kmalloc, qdio, ...),
1147          * afterwards the lock is required again for the following reasons:
1148          * - dequeueing of finished action and enqueueing of
1149          *   follow-up actions must be atomic so that any other
1150          *   reopen-routine does not believe there is nothing to do
1151          *   and that it is safe to enqueue something else,
1152          * - we want to force any control thread which is dismissing
1153          *   actions to finish this before we decide about
1154          *   necessary steps to be taken here further
1155          */
1156         write_unlock(&adapter->erp_lock);
1157         read_unlock_irqrestore(&zfcp_data.config_lock, flags);
1158         retval = zfcp_erp_strategy_do_action(erp_action);
1159         read_lock_irqsave(&zfcp_data.config_lock, flags);
1160         write_lock(&adapter->erp_lock);
1161
1162         /*
1163          * check for dismissed status again to avoid follow-up actions,
1164          * failing of targets and so on for dismissed actions
1165          */
1166         retval = zfcp_erp_strategy_check_action(erp_action, retval);
1167
1168         switch (retval) {
1169         case ZFCP_ERP_DISMISSED:
1170                 /* leave since this action has ridden to its ancestors */
1171                 debug_text_event(adapter->erp_dbf, 6, "a_st_dis2");
1172                 goto unlock;
1173         case ZFCP_ERP_NOMEM:
1174                 /* no memory to continue immediately, let it sleep */
1175                 if (!(erp_action->status & ZFCP_STATUS_ERP_LOWMEM)) {
1176                         ++adapter->erp_low_mem_count;
1177                         erp_action->status |= ZFCP_STATUS_ERP_LOWMEM;
1178                 }
1179                 /* This condition is true if there is no memory available
1180                    for any erp_action on this adapter. This implies that there
1181                    are no elements in the memory pool(s) left for erp_actions.
1182                    This might happen if an erp_action that used a memory pool
1183                    element was timed out.
1184                  */
1185                 if (adapter->erp_total_count == adapter->erp_low_mem_count) {
1186                         debug_text_event(adapter->erp_dbf, 3, "a_st_lowmem");
1187                         ZFCP_LOG_NORMAL("error: no mempool elements available, "
1188                                         "restarting I/O on adapter %s "
1189                                         "to free mempool\n",
1190                                         zfcp_get_busid_by_adapter(adapter));
1191                         zfcp_erp_adapter_reopen_internal(adapter, 0);
1192                 } else {
1193                 debug_text_event(adapter->erp_dbf, 2, "a_st_memw");
1194                 retval = zfcp_erp_strategy_memwait(erp_action);
1195                 }
1196                 goto unlock;
1197         case ZFCP_ERP_CONTINUES:
1198                 /* leave since this action runs asynchronously */
1199                 debug_text_event(adapter->erp_dbf, 6, "a_st_cont");
1200                 if (erp_action->status & ZFCP_STATUS_ERP_LOWMEM) {
1201                         --adapter->erp_low_mem_count;
1202                         erp_action->status &= ~ZFCP_STATUS_ERP_LOWMEM;
1203                 }
1204                 goto unlock;
1205         }
1206         /* ok, finished action (whatever its result is) */
1207
1208         /* check for unrecoverable targets */
1209         retval = zfcp_erp_strategy_check_target(erp_action, retval);
1210
1211         /* action must be dequeued (here to allow for further ones) */
1212         zfcp_erp_action_dequeue(erp_action);
1213
1214         /*
1215          * put this target through the erp mill again if someone has
1216          * requested to change the status of a target being online 
1217          * to offline or the other way around
1218          * (old retval is preserved if nothing has to be done here)
1219          */
1220         retval = zfcp_erp_strategy_statechange(action, status, adapter,
1221                                                port, unit, retval);
1222
1223         /*
1224          * leave if target is in permanent error state or if
1225          * action is repeated in order to process state change
1226          */
1227         if (retval == ZFCP_ERP_EXIT) {
1228                 debug_text_event(adapter->erp_dbf, 2, "a_st_exit");
1229                 goto unlock;
1230         }
1231
1232         /* trigger follow up actions */
1233         zfcp_erp_strategy_followup_actions(action, adapter, port, unit, retval);
1234
1235  unlock:
1236         write_unlock(&adapter->erp_lock);
1237         read_unlock_irqrestore(&zfcp_data.config_lock, flags);
1238         
1239         if (retval != ZFCP_ERP_CONTINUES)
1240                 zfcp_erp_action_cleanup(action, adapter, port, unit, retval);
1241
1242         /*
1243          * a few tasks remain when the erp queues are empty
1244          * (don't do that if the last action evaluated was dismissed
1245          * since this clearly indicates that there is more to come) :
1246          * - close the name server port if it is open yet
1247          *   (enqueues another [probably] final action)
1248          * - otherwise, wake up whoever wants to be woken when we are
1249          *   done with erp
1250          */
1251         if (retval != ZFCP_ERP_DISMISSED)
1252                 zfcp_erp_strategy_check_queues(adapter);
1253
1254         debug_text_event(adapter->erp_dbf, 6, "a_st_done");
1255
1256         return retval;
1257 }
1258
1259 /*
1260  * function:    
1261  *
1262  * purpose:     
1263  *
1264  * returns:     ZFCP_ERP_DISMISSED      - if action has been dismissed
1265  *              retval                  - otherwise
1266  */
1267 static int
1268 zfcp_erp_strategy_check_action(struct zfcp_erp_action *erp_action, int retval)
1269 {
1270         struct zfcp_adapter *adapter = erp_action->adapter;
1271
1272         zfcp_erp_strategy_check_fsfreq(erp_action);
1273
1274         debug_event(adapter->erp_dbf, 5, &erp_action->action, sizeof (int));
1275         if (erp_action->status & ZFCP_STATUS_ERP_DISMISSED) {
1276                 debug_text_event(adapter->erp_dbf, 3, "a_stcd_dis");
1277                 zfcp_erp_action_dequeue(erp_action);
1278                 retval = ZFCP_ERP_DISMISSED;
1279         } else
1280                 debug_text_event(adapter->erp_dbf, 5, "a_stcd_nodis");
1281
1282         return retval;
1283 }
1284
1285 static int
1286 zfcp_erp_strategy_do_action(struct zfcp_erp_action *erp_action)
1287 {
1288         int retval = ZFCP_ERP_FAILED;
1289         struct zfcp_adapter *adapter = erp_action->adapter;
1290
1291         /*
1292          * try to execute/continue action as far as possible,
1293          * note: no lock in subsequent strategy routines
1294          * (this allows these routine to call schedule, e.g.
1295          * kmalloc with such flags or qdio_initialize & friends)
1296          * Note: in case of timeout, the seperate strategies will fail
1297          * anyhow. No need for a special action. Even worse, a nameserver
1298          * failure would not wake up waiting ports without the call.
1299          */
1300         switch (erp_action->action) {
1301
1302         case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
1303                 retval = zfcp_erp_adapter_strategy(erp_action);
1304                 break;
1305
1306         case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
1307                 retval = zfcp_erp_port_forced_strategy(erp_action);
1308                 break;
1309
1310         case ZFCP_ERP_ACTION_REOPEN_PORT:
1311                 retval = zfcp_erp_port_strategy(erp_action);
1312                 break;
1313
1314         case ZFCP_ERP_ACTION_REOPEN_UNIT:
1315                 retval = zfcp_erp_unit_strategy(erp_action);
1316                 break;
1317
1318         default:
1319                 debug_text_exception(adapter->erp_dbf, 1, "a_stda_bug");
1320                 debug_event(adapter->erp_dbf, 1, &erp_action->action,
1321                             sizeof (int));
1322                 ZFCP_LOG_NORMAL("bug: unknown erp action requested on "
1323                                 "adapter %s (action=%d)\n",
1324                                 zfcp_get_busid_by_adapter(erp_action->adapter),
1325                                 erp_action->action);
1326         }
1327
1328         return retval;
1329 }
1330
1331 /*
1332  * function:    
1333  *
1334  * purpose:     triggers retry of this action after a certain amount of time
1335  *              by means of timer provided by erp_action
1336  *
1337  * returns:     ZFCP_ERP_CONTINUES - erp_action sleeps in erp running queue
1338  */
1339 static int
1340 zfcp_erp_strategy_memwait(struct zfcp_erp_action *erp_action)
1341 {
1342         int retval = ZFCP_ERP_CONTINUES;
1343         struct zfcp_adapter *adapter = erp_action->adapter;
1344
1345         debug_text_event(adapter->erp_dbf, 6, "a_mwinit");
1346         debug_event(adapter->erp_dbf, 6, &erp_action->action, sizeof (int));
1347         init_timer(&erp_action->timer);
1348         erp_action->timer.function = zfcp_erp_memwait_handler;
1349         erp_action->timer.data = (unsigned long) erp_action;
1350         erp_action->timer.expires = jiffies + ZFCP_ERP_MEMWAIT_TIMEOUT;
1351         add_timer(&erp_action->timer);
1352
1353         return retval;
1354 }
1355
1356 /* 
1357  * function:    zfcp_erp_adapter_failed
1358  *
1359  * purpose:     sets the adapter and all underlying devices to ERP_FAILED
1360  *
1361  */
1362 void
1363 zfcp_erp_adapter_failed(struct zfcp_adapter *adapter)
1364 {
1365         zfcp_erp_modify_adapter_status(adapter,
1366                                        ZFCP_STATUS_COMMON_ERP_FAILED, ZFCP_SET);
1367         ZFCP_LOG_NORMAL("adapter erp failed on adapter %s\n",
1368                         zfcp_get_busid_by_adapter(adapter));
1369         debug_text_event(adapter->erp_dbf, 2, "a_afail");
1370 }
1371
1372 /* 
1373  * function:    zfcp_erp_port_failed
1374  *
1375  * purpose:     sets the port and all underlying devices to ERP_FAILED
1376  *
1377  */
1378 void
1379 zfcp_erp_port_failed(struct zfcp_port *port)
1380 {
1381         zfcp_erp_modify_port_status(port,
1382                                     ZFCP_STATUS_COMMON_ERP_FAILED, ZFCP_SET);
1383
1384         if (atomic_test_mask(ZFCP_STATUS_PORT_WKA, &port->status))
1385                 ZFCP_LOG_NORMAL("port erp failed (adapter %s, "
1386                                 "port d_id=0x%08x)\n",
1387                                 zfcp_get_busid_by_port(port), port->d_id);
1388         else
1389                 ZFCP_LOG_NORMAL("port erp failed (adapter %s, wwpn=0x%016Lx)\n",
1390                                 zfcp_get_busid_by_port(port), port->wwpn);
1391
1392         debug_text_event(port->adapter->erp_dbf, 2, "p_pfail");
1393         debug_event(port->adapter->erp_dbf, 2, &port->wwpn, sizeof (wwn_t));
1394 }
1395
1396 /* 
1397  * function:    zfcp_erp_unit_failed
1398  *
1399  * purpose:     sets the unit to ERP_FAILED
1400  *
1401  */
1402 void
1403 zfcp_erp_unit_failed(struct zfcp_unit *unit)
1404 {
1405         zfcp_erp_modify_unit_status(unit,
1406                                     ZFCP_STATUS_COMMON_ERP_FAILED, ZFCP_SET);
1407
1408         ZFCP_LOG_NORMAL("unit erp failed on unit 0x%016Lx on port 0x%016Lx "
1409                         " on adapter %s\n", unit->fcp_lun,
1410                         unit->port->wwpn, zfcp_get_busid_by_unit(unit));
1411         debug_text_event(unit->port->adapter->erp_dbf, 2, "u_ufail");
1412         debug_event(unit->port->adapter->erp_dbf, 2,
1413                     &unit->fcp_lun, sizeof (fcp_lun_t));
1414 }
1415
1416 /*
1417  * function:    zfcp_erp_strategy_check_target
1418  *
1419  * purpose:     increments the erp action count on the device currently in
1420  *              recovery if the action failed or resets the count in case of
1421  *              success. If a maximum count is exceeded the device is marked
1422  *              as ERP_FAILED.
1423  *              The 'blocked' state of a target which has been recovered
1424  *              successfully is reset.
1425  *
1426  * returns:     ZFCP_ERP_CONTINUES      - action continues (not considered)
1427  *              ZFCP_ERP_SUCCEEDED      - action finished successfully 
1428  *              ZFCP_ERP_EXIT           - action failed and will not continue
1429  */
1430 static int
1431 zfcp_erp_strategy_check_target(struct zfcp_erp_action *erp_action, int result)
1432 {
1433         struct zfcp_adapter *adapter = erp_action->adapter;
1434         struct zfcp_port *port = erp_action->port;
1435         struct zfcp_unit *unit = erp_action->unit;
1436
1437         debug_text_event(adapter->erp_dbf, 5, "a_stct_norm");
1438         debug_event(adapter->erp_dbf, 5, &erp_action->action, sizeof (int));
1439         debug_event(adapter->erp_dbf, 5, &result, sizeof (int));
1440
1441         switch (erp_action->action) {
1442
1443         case ZFCP_ERP_ACTION_REOPEN_UNIT:
1444                 result = zfcp_erp_strategy_check_unit(unit, result);
1445                 break;
1446
1447         case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
1448         case ZFCP_ERP_ACTION_REOPEN_PORT:
1449                 result = zfcp_erp_strategy_check_port(port, result);
1450                 break;
1451
1452         case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
1453                 result = zfcp_erp_strategy_check_adapter(adapter, result);
1454                 break;
1455         }
1456
1457         return result;
1458 }
1459
1460 static int
1461 zfcp_erp_strategy_statechange(int action,
1462                               u32 status,
1463                               struct zfcp_adapter *adapter,
1464                               struct zfcp_port *port,
1465                               struct zfcp_unit *unit, int retval)
1466 {
1467         debug_text_event(adapter->erp_dbf, 3, "a_stsc");
1468         debug_event(adapter->erp_dbf, 3, &action, sizeof (int));
1469
1470         switch (action) {
1471
1472         case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
1473                 if (zfcp_erp_strategy_statechange_detected(&adapter->status,
1474                                                            status)) {
1475                         zfcp_erp_adapter_reopen_internal(adapter, ZFCP_STATUS_COMMON_ERP_FAILED);
1476                         retval = ZFCP_ERP_EXIT;
1477                 }
1478                 break;
1479
1480         case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
1481         case ZFCP_ERP_ACTION_REOPEN_PORT:
1482                 if (zfcp_erp_strategy_statechange_detected(&port->status,
1483                                                            status)) {
1484                         zfcp_erp_port_reopen_internal(port, ZFCP_STATUS_COMMON_ERP_FAILED);
1485                         retval = ZFCP_ERP_EXIT;
1486                 }
1487                 break;
1488
1489         case ZFCP_ERP_ACTION_REOPEN_UNIT:
1490                 if (zfcp_erp_strategy_statechange_detected(&unit->status,
1491                                                            status)) {
1492                         zfcp_erp_unit_reopen_internal(unit, ZFCP_STATUS_COMMON_ERP_FAILED);
1493                         retval = ZFCP_ERP_EXIT;
1494                 }
1495                 break;
1496         }
1497
1498         return retval;
1499 }
1500
1501 static inline int
1502 zfcp_erp_strategy_statechange_detected(atomic_t * target_status, u32 erp_status)
1503 {
1504         return
1505             /* take it online */
1506             (atomic_test_mask(ZFCP_STATUS_COMMON_RUNNING, target_status) &&
1507              (ZFCP_STATUS_ERP_CLOSE_ONLY & erp_status)) ||
1508             /* take it offline */
1509             (!atomic_test_mask(ZFCP_STATUS_COMMON_RUNNING, target_status) &&
1510              !(ZFCP_STATUS_ERP_CLOSE_ONLY & erp_status));
1511 }
1512
1513 static int
1514 zfcp_erp_strategy_check_unit(struct zfcp_unit *unit, int result)
1515 {
1516         debug_text_event(unit->port->adapter->erp_dbf, 5, "u_stct");
1517         debug_event(unit->port->adapter->erp_dbf, 5, &unit->fcp_lun,
1518                     sizeof (fcp_lun_t));
1519
1520         switch (result) {
1521         case ZFCP_ERP_SUCCEEDED :
1522                 atomic_set(&unit->erp_counter, 0);
1523                 zfcp_erp_unit_unblock(unit);
1524                 break;
1525         case ZFCP_ERP_FAILED :
1526                 atomic_inc(&unit->erp_counter);
1527                 if (atomic_read(&unit->erp_counter) > ZFCP_MAX_ERPS)
1528                         zfcp_erp_unit_failed(unit);
1529                 break;
1530         case ZFCP_ERP_EXIT :
1531                 /* nothing */
1532                 break;
1533         }
1534
1535         if (atomic_test_mask(ZFCP_STATUS_COMMON_ERP_FAILED, &unit->status)) {
1536                 zfcp_erp_unit_block(unit, 0); /* for ZFCP_ERP_SUCCEEDED */
1537                 result = ZFCP_ERP_EXIT;
1538         }
1539
1540         return result;
1541 }
1542
1543 static int
1544 zfcp_erp_strategy_check_port(struct zfcp_port *port, int result)
1545 {
1546         debug_text_event(port->adapter->erp_dbf, 5, "p_stct");
1547         debug_event(port->adapter->erp_dbf, 5, &port->wwpn, sizeof (wwn_t));
1548
1549         switch (result) {
1550         case ZFCP_ERP_SUCCEEDED :
1551                 atomic_set(&port->erp_counter, 0);
1552                 zfcp_erp_port_unblock(port);
1553                 break;
1554         case ZFCP_ERP_FAILED :
1555                 atomic_inc(&port->erp_counter);
1556                 if (atomic_read(&port->erp_counter) > ZFCP_MAX_ERPS)
1557                         zfcp_erp_port_failed(port);
1558                 break;
1559         case ZFCP_ERP_EXIT :
1560                 /* nothing */
1561                 break;
1562         }
1563
1564         if (atomic_test_mask(ZFCP_STATUS_COMMON_ERP_FAILED, &port->status)) {
1565                 zfcp_erp_port_block(port, 0); /* for ZFCP_ERP_SUCCEEDED */
1566                 result = ZFCP_ERP_EXIT;
1567         }
1568
1569         return result;
1570 }
1571
1572 static int
1573 zfcp_erp_strategy_check_adapter(struct zfcp_adapter *adapter, int result)
1574 {
1575         debug_text_event(adapter->erp_dbf, 5, "a_stct");
1576
1577         switch (result) {
1578         case ZFCP_ERP_SUCCEEDED :
1579                 atomic_set(&adapter->erp_counter, 0);
1580                 zfcp_erp_adapter_unblock(adapter);
1581                 break;
1582         case ZFCP_ERP_FAILED :
1583                 atomic_inc(&adapter->erp_counter);
1584                 if (atomic_read(&adapter->erp_counter) > ZFCP_MAX_ERPS)
1585                         zfcp_erp_adapter_failed(adapter);
1586                 break;
1587         case ZFCP_ERP_EXIT :
1588                 /* nothing */
1589                 break;
1590         }
1591
1592         if (atomic_test_mask(ZFCP_STATUS_COMMON_ERP_FAILED, &adapter->status)) {
1593                 zfcp_erp_adapter_block(adapter, 0); /* for ZFCP_ERP_SUCCEEDED */
1594                 result = ZFCP_ERP_EXIT;
1595         }
1596
1597         return result;
1598 }
1599
1600 /*
1601  * function:    
1602  *
1603  * purpose:     remaining things in good cases,
1604  *              escalation in bad cases
1605  *
1606  * returns:
1607  */
1608 static int
1609 zfcp_erp_strategy_followup_actions(int action,
1610                                    struct zfcp_adapter *adapter,
1611                                    struct zfcp_port *port,
1612                                    struct zfcp_unit *unit, int status)
1613 {
1614         debug_text_event(adapter->erp_dbf, 5, "a_stfol");
1615         debug_event(adapter->erp_dbf, 5, &action, sizeof (int));
1616
1617         /* initiate follow-up actions depending on success of finished action */
1618         switch (action) {
1619
1620         case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
1621                 if (status == ZFCP_ERP_SUCCEEDED)
1622                         zfcp_erp_port_reopen_all_internal(adapter, 0);
1623                 else
1624                         zfcp_erp_adapter_reopen_internal(adapter, 0);
1625                 break;
1626
1627         case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
1628                 if (status == ZFCP_ERP_SUCCEEDED)
1629                         zfcp_erp_port_reopen_internal(port, 0);
1630                 else
1631                         zfcp_erp_adapter_reopen_internal(adapter, 0);
1632                 break;
1633
1634         case ZFCP_ERP_ACTION_REOPEN_PORT:
1635                 if (status == ZFCP_ERP_SUCCEEDED)
1636                         zfcp_erp_unit_reopen_all_internal(port, 0);
1637                 else
1638                         zfcp_erp_port_forced_reopen_internal(port, 0);
1639                 break;
1640
1641         case ZFCP_ERP_ACTION_REOPEN_UNIT:
1642                 if (status == ZFCP_ERP_SUCCEEDED) ;     /* no further action */
1643                 else
1644                         zfcp_erp_port_reopen_internal(unit->port, 0);
1645                 break;
1646         }
1647
1648         return 0;
1649 }
1650
1651 static int
1652 zfcp_erp_strategy_check_queues(struct zfcp_adapter *adapter)
1653 {
1654         unsigned long flags;
1655
1656         read_lock_irqsave(&zfcp_data.config_lock, flags);
1657         read_lock(&adapter->erp_lock);
1658         if (list_empty(&adapter->erp_ready_head) &&
1659             list_empty(&adapter->erp_running_head)) {
1660                         debug_text_event(adapter->erp_dbf, 4, "a_cq_wake");
1661                         atomic_clear_mask(ZFCP_STATUS_ADAPTER_ERP_PENDING,
1662                                           &adapter->status);
1663                         wake_up(&adapter->erp_done_wqh);
1664         } else
1665                 debug_text_event(adapter->erp_dbf, 5, "a_cq_notempty");
1666         read_unlock(&adapter->erp_lock);
1667         read_unlock_irqrestore(&zfcp_data.config_lock, flags);
1668
1669         return 0;
1670 }
1671
1672 /**
1673  * zfcp_erp_wait - wait for completion of error recovery on an adapter
1674  * @adapter: adapter for which to wait for completion of its error recovery
1675  * Return: 0
1676  */
1677 int
1678 zfcp_erp_wait(struct zfcp_adapter *adapter)
1679 {
1680         int retval = 0;
1681
1682         wait_event(adapter->erp_done_wqh,
1683                    !atomic_test_mask(ZFCP_STATUS_ADAPTER_ERP_PENDING,
1684                                      &adapter->status));
1685
1686         return retval;
1687 }
1688
1689 void
1690 zfcp_erp_modify_adapter_status(struct zfcp_adapter *adapter,
1691                                u32 mask, int set_or_clear)
1692 {
1693         struct zfcp_port *port;
1694         u32 common_mask = mask & ZFCP_COMMON_FLAGS;
1695
1696         if (set_or_clear == ZFCP_SET) {
1697                 atomic_set_mask(mask, &adapter->status);
1698                 debug_text_event(adapter->erp_dbf, 3, "a_mod_as_s");
1699         } else {
1700                 atomic_clear_mask(mask, &adapter->status);
1701                 if (mask & ZFCP_STATUS_COMMON_ERP_FAILED)
1702                         atomic_set(&adapter->erp_counter, 0);
1703                 debug_text_event(adapter->erp_dbf, 3, "a_mod_as_c");
1704         }
1705         debug_event(adapter->erp_dbf, 3, &mask, sizeof (u32));
1706
1707         /* Deal with all underlying devices, only pass common_mask */
1708         if (common_mask)
1709                 list_for_each_entry(port, &adapter->port_list_head, list)
1710                     zfcp_erp_modify_port_status(port, common_mask,
1711                                                 set_or_clear);
1712 }
1713
1714 /*
1715  * function:    zfcp_erp_modify_port_status
1716  *
1717  * purpose:     sets the port and all underlying devices to ERP_FAILED
1718  *
1719  */
1720 void
1721 zfcp_erp_modify_port_status(struct zfcp_port *port, u32 mask, int set_or_clear)
1722 {
1723         struct zfcp_unit *unit;
1724         u32 common_mask = mask & ZFCP_COMMON_FLAGS;
1725
1726         if (set_or_clear == ZFCP_SET) {
1727                 atomic_set_mask(mask, &port->status);
1728                 debug_text_event(port->adapter->erp_dbf, 3, "p_mod_ps_s");
1729         } else {
1730                 atomic_clear_mask(mask, &port->status);
1731                 if (mask & ZFCP_STATUS_COMMON_ERP_FAILED)
1732                         atomic_set(&port->erp_counter, 0);
1733                 debug_text_event(port->adapter->erp_dbf, 3, "p_mod_ps_c");
1734         }
1735         debug_event(port->adapter->erp_dbf, 3, &port->wwpn, sizeof (wwn_t));
1736         debug_event(port->adapter->erp_dbf, 3, &mask, sizeof (u32));
1737
1738         /* Modify status of all underlying devices, only pass common mask */
1739         if (common_mask)
1740                 list_for_each_entry(unit, &port->unit_list_head, list)
1741                     zfcp_erp_modify_unit_status(unit, common_mask,
1742                                                 set_or_clear);
1743 }
1744
1745 /*
1746  * function:    zfcp_erp_modify_unit_status
1747  *
1748  * purpose:     sets the unit to ERP_FAILED
1749  *
1750  */
1751 void
1752 zfcp_erp_modify_unit_status(struct zfcp_unit *unit, u32 mask, int set_or_clear)
1753 {
1754         if (set_or_clear == ZFCP_SET) {
1755                 atomic_set_mask(mask, &unit->status);
1756                 debug_text_event(unit->port->adapter->erp_dbf, 3, "u_mod_us_s");
1757         } else {
1758                 atomic_clear_mask(mask, &unit->status);
1759                 if (mask & ZFCP_STATUS_COMMON_ERP_FAILED) {
1760                         atomic_set(&unit->erp_counter, 0);
1761                 }
1762                 debug_text_event(unit->port->adapter->erp_dbf, 3, "u_mod_us_c");
1763         }
1764         debug_event(unit->port->adapter->erp_dbf, 3, &unit->fcp_lun,
1765                     sizeof (fcp_lun_t));
1766         debug_event(unit->port->adapter->erp_dbf, 3, &mask, sizeof (u32));
1767 }
1768
1769 /*
1770  * function:    
1771  *
1772  * purpose:     Wrappper for zfcp_erp_port_reopen_all_internal
1773  *              used to ensure the correct locking
1774  *
1775  * returns:     0       - initiated action successfully
1776  *              <0      - failed to initiate action
1777  */
1778 int
1779 zfcp_erp_port_reopen_all(struct zfcp_adapter *adapter, int clear_mask)
1780 {
1781         int retval;
1782         unsigned long flags;
1783
1784         read_lock_irqsave(&zfcp_data.config_lock, flags);
1785         write_lock(&adapter->erp_lock);
1786         retval = zfcp_erp_port_reopen_all_internal(adapter, clear_mask);
1787         write_unlock(&adapter->erp_lock);
1788         read_unlock_irqrestore(&zfcp_data.config_lock, flags);
1789
1790         return retval;
1791 }
1792
1793 static int
1794 zfcp_erp_port_reopen_all_internal(struct zfcp_adapter *adapter, int clear_mask)
1795 {
1796         int retval = 0;
1797         struct zfcp_port *port;
1798
1799         list_for_each_entry(port, &adapter->port_list_head, list)
1800                 if (!atomic_test_mask(ZFCP_STATUS_PORT_WKA, &port->status))
1801                         zfcp_erp_port_reopen_internal(port, clear_mask);
1802
1803         return retval;
1804 }
1805
1806 /*
1807  * function:    
1808  *
1809  * purpose:     
1810  *
1811  * returns:     FIXME
1812  */
1813 static int
1814 zfcp_erp_unit_reopen_all_internal(struct zfcp_port *port, int clear_mask)
1815 {
1816         int retval = 0;
1817         struct zfcp_unit *unit;
1818
1819         list_for_each_entry(unit, &port->unit_list_head, list)
1820             zfcp_erp_unit_reopen_internal(unit, clear_mask);
1821
1822         return retval;
1823 }
1824
1825 /*
1826  * function:    
1827  *
1828  * purpose:     this routine executes the 'Reopen Adapter' action
1829  *              (the entire action is processed synchronously, since
1830  *              there are no actions which might be run concurrently
1831  *              per definition)
1832  *
1833  * returns:     ZFCP_ERP_SUCCEEDED      - action finished successfully
1834  *              ZFCP_ERP_FAILED         - action finished unsuccessfully
1835  */
1836 static int
1837 zfcp_erp_adapter_strategy(struct zfcp_erp_action *erp_action)
1838 {
1839         int retval;
1840         struct zfcp_adapter *adapter = erp_action->adapter;
1841
1842         retval = zfcp_erp_adapter_strategy_close(erp_action);
1843         if (erp_action->status & ZFCP_STATUS_ERP_CLOSE_ONLY)
1844                 retval = ZFCP_ERP_EXIT;
1845         else
1846                 retval = zfcp_erp_adapter_strategy_open(erp_action);
1847
1848         debug_text_event(adapter->erp_dbf, 3, "a_ast/ret");
1849         debug_event(adapter->erp_dbf, 3, &erp_action->action, sizeof (int));
1850         debug_event(adapter->erp_dbf, 3, &retval, sizeof (int));
1851
1852         if (retval == ZFCP_ERP_FAILED) {
1853                 ZFCP_LOG_INFO("Waiting to allow the adapter %s "
1854                               "to recover itself\n",
1855                               zfcp_get_busid_by_adapter(adapter));
1856                 msleep(jiffies_to_msecs(ZFCP_TYPE2_RECOVERY_TIME));
1857         }
1858
1859         return retval;
1860 }
1861
1862 /*
1863  * function:    
1864  *
1865  * purpose:     
1866  *
1867  * returns:     ZFCP_ERP_SUCCEEDED      - action finished successfully
1868  *              ZFCP_ERP_FAILED         - action finished unsuccessfully
1869  */
1870 static int
1871 zfcp_erp_adapter_strategy_close(struct zfcp_erp_action *erp_action)
1872 {
1873         int retval;
1874
1875         atomic_set_mask(ZFCP_STATUS_COMMON_CLOSING,
1876                         &erp_action->adapter->status);
1877         retval = zfcp_erp_adapter_strategy_generic(erp_action, 1);
1878         atomic_clear_mask(ZFCP_STATUS_COMMON_CLOSING,
1879                           &erp_action->adapter->status);
1880
1881         return retval;
1882 }
1883
1884 /*
1885  * function:    
1886  *
1887  * purpose:     
1888  *
1889  * returns:     ZFCP_ERP_SUCCEEDED      - action finished successfully
1890  *              ZFCP_ERP_FAILED         - action finished unsuccessfully
1891  */
1892 static int
1893 zfcp_erp_adapter_strategy_open(struct zfcp_erp_action *erp_action)
1894 {
1895         int retval;
1896
1897         atomic_set_mask(ZFCP_STATUS_COMMON_OPENING,
1898                         &erp_action->adapter->status);
1899         retval = zfcp_erp_adapter_strategy_generic(erp_action, 0);
1900         atomic_clear_mask(ZFCP_STATUS_COMMON_OPENING,
1901                           &erp_action->adapter->status);
1902
1903         return retval;
1904 }
1905
1906 /*
1907  * function:    zfcp_register_adapter
1908  *
1909  * purpose:     allocate the irq associated with this devno and register
1910  *              the FSF adapter with the SCSI stack
1911  *
1912  * returns:     
1913  */
1914 static int
1915 zfcp_erp_adapter_strategy_generic(struct zfcp_erp_action *erp_action, int close)
1916 {
1917         int retval = ZFCP_ERP_SUCCEEDED;
1918
1919         if (close)
1920                 goto close_only;
1921
1922         retval = zfcp_erp_adapter_strategy_open_qdio(erp_action);
1923         if (retval != ZFCP_ERP_SUCCEEDED)
1924                 goto failed_qdio;
1925
1926         retval = zfcp_erp_adapter_strategy_open_fsf(erp_action);
1927         if (retval != ZFCP_ERP_SUCCEEDED)
1928                 goto failed_openfcp;
1929
1930         atomic_set_mask(ZFCP_STATUS_COMMON_OPEN, &erp_action->adapter->status);
1931         goto out;
1932
1933  close_only:
1934         atomic_clear_mask(ZFCP_STATUS_COMMON_OPEN,
1935                           &erp_action->adapter->status);
1936
1937  failed_openfcp:
1938         zfcp_close_fsf(erp_action->adapter);
1939  failed_qdio:
1940  out:
1941         return retval;
1942 }
1943
1944 /*
1945  * function:    zfcp_qdio_init
1946  *
1947  * purpose:     setup QDIO operation for specified adapter
1948  *
1949  * returns:     0 - successful setup
1950  *              !0 - failed setup
1951  */
1952 int
1953 zfcp_erp_adapter_strategy_open_qdio(struct zfcp_erp_action *erp_action)
1954 {
1955         int retval;
1956         int i;
1957         volatile struct qdio_buffer_element *sbale;
1958         struct zfcp_adapter *adapter = erp_action->adapter;
1959
1960         if (atomic_test_mask(ZFCP_STATUS_ADAPTER_QDIOUP, &adapter->status)) {
1961                 ZFCP_LOG_NORMAL("bug: second attempt to set up QDIO on "
1962                                 "adapter %s\n",
1963                                 zfcp_get_busid_by_adapter(adapter));
1964                 goto failed_sanity;
1965         }
1966
1967         if (qdio_establish(&adapter->qdio_init_data) != 0) {
1968                 ZFCP_LOG_INFO("error: establishment of QDIO queues failed "
1969                               "on adapter %s\n",
1970                               zfcp_get_busid_by_adapter(adapter));
1971                 goto failed_qdio_establish;
1972         }
1973         debug_text_event(adapter->erp_dbf, 3, "qdio_est");
1974
1975         if (qdio_activate(adapter->ccw_device, 0) != 0) {
1976                 ZFCP_LOG_INFO("error: activation of QDIO queues failed "
1977                               "on adapter %s\n",
1978                               zfcp_get_busid_by_adapter(adapter));
1979                 goto failed_qdio_activate;
1980         }
1981         debug_text_event(adapter->erp_dbf, 3, "qdio_act");
1982
1983         /*
1984          * put buffers into response queue,
1985          */
1986         for (i = 0; i < QDIO_MAX_BUFFERS_PER_Q; i++) {
1987                 sbale = &(adapter->response_queue.buffer[i]->element[0]);
1988                 sbale->length = 0;
1989                 sbale->flags = SBAL_FLAGS_LAST_ENTRY;
1990                 sbale->addr = 0;
1991         }
1992
1993         ZFCP_LOG_TRACE("calling do_QDIO on adapter %s (flags=0x%x, "
1994                        "queue_no=%i, index_in_queue=%i, count=%i)\n",
1995                        zfcp_get_busid_by_adapter(adapter),
1996                        QDIO_FLAG_SYNC_INPUT, 0, 0, QDIO_MAX_BUFFERS_PER_Q);
1997
1998         retval = do_QDIO(adapter->ccw_device,
1999                          QDIO_FLAG_SYNC_INPUT,
2000                          0, 0, QDIO_MAX_BUFFERS_PER_Q, NULL);
2001
2002         if (retval) {
2003                 ZFCP_LOG_NORMAL("bug: setup of QDIO failed (retval=%d)\n",
2004                                 retval);
2005                 goto failed_do_qdio;
2006         } else {
2007                 adapter->response_queue.free_index = 0;
2008                 atomic_set(&adapter->response_queue.free_count, 0);
2009                 ZFCP_LOG_DEBUG("%i buffers successfully enqueued to "
2010                                "response queue\n", QDIO_MAX_BUFFERS_PER_Q);
2011         }
2012         /* set index of first avalable SBALS / number of available SBALS */
2013         adapter->request_queue.free_index = 0;
2014         atomic_set(&adapter->request_queue.free_count, QDIO_MAX_BUFFERS_PER_Q);
2015         adapter->request_queue.distance_from_int = 0;
2016
2017         /* initialize waitqueue used to wait for free SBALs in requests queue */
2018         init_waitqueue_head(&adapter->request_wq);
2019
2020         /* ok, we did it - skip all cleanups for different failures */
2021         atomic_set_mask(ZFCP_STATUS_ADAPTER_QDIOUP, &adapter->status);
2022         retval = ZFCP_ERP_SUCCEEDED;
2023         goto out;
2024
2025  failed_do_qdio:
2026         /* NOP */
2027
2028  failed_qdio_activate:
2029         debug_text_event(adapter->erp_dbf, 3, "qdio_down1a");
2030         while (qdio_shutdown(adapter->ccw_device,
2031                              QDIO_FLAG_CLEANUP_USING_CLEAR) == -EINPROGRESS)
2032                 msleep(1000);
2033         debug_text_event(adapter->erp_dbf, 3, "qdio_down1b");
2034
2035  failed_qdio_establish:
2036  failed_sanity:
2037         retval = ZFCP_ERP_FAILED;
2038
2039  out:
2040         return retval;
2041 }
2042
2043
2044 static int
2045 zfcp_erp_adapter_strategy_open_fsf(struct zfcp_erp_action *erp_action)
2046 {
2047         int retval;
2048
2049         retval = zfcp_erp_adapter_strategy_open_fsf_xconfig(erp_action);
2050         if (retval == ZFCP_ERP_FAILED)
2051                 return ZFCP_ERP_FAILED;
2052
2053         retval = zfcp_erp_adapter_strategy_open_fsf_xport(erp_action);
2054         if (retval == ZFCP_ERP_FAILED)
2055                 return ZFCP_ERP_FAILED;
2056
2057         return zfcp_erp_adapter_strategy_open_fsf_statusread(erp_action);
2058 }
2059
2060 static int
2061 zfcp_erp_adapter_strategy_open_fsf_xconfig(struct zfcp_erp_action *erp_action)
2062 {
2063         int retval = ZFCP_ERP_SUCCEEDED;
2064         int retries;
2065         int sleep = ZFCP_EXCHANGE_CONFIG_DATA_FIRST_SLEEP;
2066         struct zfcp_adapter *adapter = erp_action->adapter;
2067
2068         atomic_clear_mask(ZFCP_STATUS_ADAPTER_XCONFIG_OK, &adapter->status);
2069
2070         for (retries = ZFCP_EXCHANGE_CONFIG_DATA_RETRIES; retries; retries--) {
2071                 atomic_clear_mask(ZFCP_STATUS_ADAPTER_HOST_CON_INIT,
2072                                   &adapter->status);
2073                 ZFCP_LOG_DEBUG("Doing exchange config data\n");
2074                 write_lock_irq(&adapter->erp_lock);
2075                 zfcp_erp_action_to_running(erp_action);
2076                 write_unlock_irq(&adapter->erp_lock);
2077                 if (zfcp_fsf_exchange_config_data(erp_action)) {
2078                         retval = ZFCP_ERP_FAILED;
2079                         debug_text_event(adapter->erp_dbf, 5, "a_fstx_xf");
2080                         ZFCP_LOG_INFO("error:  initiation of exchange of "
2081                                       "configuration data failed for "
2082                                       "adapter %s\n",
2083                                       zfcp_get_busid_by_adapter(adapter));
2084                         break;
2085                 }
2086                 debug_text_event(adapter->erp_dbf, 6, "a_fstx_xok");
2087                 ZFCP_LOG_DEBUG("Xchange underway\n");
2088
2089                 /*
2090                  * Why this works:
2091                  * Both the normal completion handler as well as the timeout
2092                  * handler will do an 'up' when the 'exchange config data'
2093                  * request completes or times out. Thus, the signal to go on
2094                  * won't be lost utilizing this semaphore.
2095                  * Furthermore, this 'adapter_reopen' action is
2096                  * guaranteed to be the only action being there (highest action
2097                  * which prevents other actions from being created).
2098                  * Resulting from that, the wake signal recognized here
2099                  * _must_ be the one belonging to the 'exchange config
2100                  * data' request.
2101                  */
2102                 down(&adapter->erp_ready_sem);
2103                 if (erp_action->status & ZFCP_STATUS_ERP_TIMEDOUT) {
2104                         ZFCP_LOG_INFO("error: exchange of configuration data "
2105                                       "for adapter %s timed out\n",
2106                                       zfcp_get_busid_by_adapter(adapter));
2107                         break;
2108                 }
2109
2110                 if (!atomic_test_mask(ZFCP_STATUS_ADAPTER_HOST_CON_INIT,
2111                                      &adapter->status))
2112                         break;
2113
2114                 ZFCP_LOG_DEBUG("host connection still initialising... "
2115                                "waiting and retrying...\n");
2116                 /* sleep a little bit before retry */
2117                 msleep(jiffies_to_msecs(sleep));
2118                 sleep *= 2;
2119         }
2120
2121         if (!atomic_test_mask(ZFCP_STATUS_ADAPTER_XCONFIG_OK,
2122                               &adapter->status)) {
2123                 ZFCP_LOG_INFO("error: exchange of configuration data for "
2124                               "adapter %s failed\n",
2125                               zfcp_get_busid_by_adapter(adapter));
2126                 retval = ZFCP_ERP_FAILED;
2127         }
2128
2129         return retval;
2130 }
2131
2132 static int
2133 zfcp_erp_adapter_strategy_open_fsf_xport(struct zfcp_erp_action *erp_action)
2134 {
2135         int ret;
2136         struct zfcp_adapter *adapter;
2137
2138         adapter = erp_action->adapter;
2139         atomic_clear_mask(ZFCP_STATUS_ADAPTER_XPORT_OK, &adapter->status);
2140
2141         write_lock_irq(&adapter->erp_lock);
2142         zfcp_erp_action_to_running(erp_action);
2143         write_unlock_irq(&adapter->erp_lock);
2144
2145         ret = zfcp_fsf_exchange_port_data(erp_action, adapter, NULL);
2146         if (ret == -EOPNOTSUPP) {
2147                 debug_text_event(adapter->erp_dbf, 3, "a_xport_notsupp");
2148                 return ZFCP_ERP_SUCCEEDED;
2149         } else if (ret) {
2150                 debug_text_event(adapter->erp_dbf, 3, "a_xport_failed");
2151                 return ZFCP_ERP_FAILED;
2152         }
2153         debug_text_event(adapter->erp_dbf, 6, "a_xport_ok");
2154
2155         ret = ZFCP_ERP_SUCCEEDED;
2156         down(&adapter->erp_ready_sem);
2157         if (erp_action->status & ZFCP_STATUS_ERP_TIMEDOUT) {
2158                 ZFCP_LOG_INFO("error: exchange port data timed out (adapter "
2159                               "%s)\n", zfcp_get_busid_by_adapter(adapter));
2160                 ret = ZFCP_ERP_FAILED;
2161         }
2162
2163         /* don't treat as error for the sake of compatibility */
2164         if (!atomic_test_mask(ZFCP_STATUS_ADAPTER_XPORT_OK, &adapter->status))
2165                 ZFCP_LOG_INFO("warning: exchange port data failed (adapter "
2166                               "%s\n", zfcp_get_busid_by_adapter(adapter));
2167
2168         return ret;
2169 }
2170
2171 static int
2172 zfcp_erp_adapter_strategy_open_fsf_statusread(struct zfcp_erp_action
2173                                               *erp_action)
2174 {
2175         int retval = ZFCP_ERP_SUCCEEDED;
2176         int temp_ret;
2177         struct zfcp_adapter *adapter = erp_action->adapter;
2178         int i;
2179
2180         adapter->status_read_failed = 0;
2181         for (i = 0; i < ZFCP_STATUS_READS_RECOM; i++) {
2182                 temp_ret = zfcp_fsf_status_read(adapter, ZFCP_WAIT_FOR_SBAL);
2183                 if (temp_ret < 0) {
2184                         ZFCP_LOG_INFO("error: set-up of unsolicited status "
2185                                       "notification failed on adapter %s\n",
2186                                       zfcp_get_busid_by_adapter(adapter));
2187                         retval = ZFCP_ERP_FAILED;
2188                         i--;
2189                         break;
2190                 }
2191         }
2192
2193         return retval;
2194 }
2195
2196 /*
2197  * function:    
2198  *
2199  * purpose:     this routine executes the 'Reopen Physical Port' action
2200  *
2201  * returns:     ZFCP_ERP_CONTINUES      - action continues (asynchronously)
2202  *              ZFCP_ERP_SUCCEEDED      - action finished successfully
2203  *              ZFCP_ERP_FAILED         - action finished unsuccessfully
2204  */
2205 static int
2206 zfcp_erp_port_forced_strategy(struct zfcp_erp_action *erp_action)
2207 {
2208         int retval = ZFCP_ERP_FAILED;
2209         struct zfcp_port *port = erp_action->port;
2210         struct zfcp_adapter *adapter = erp_action->adapter;
2211
2212         switch (erp_action->step) {
2213
2214                 /*
2215                  * FIXME:
2216                  * the ULP spec. begs for waiting for oustanding commands
2217                  */
2218         case ZFCP_ERP_STEP_UNINITIALIZED:
2219                 zfcp_erp_port_strategy_clearstati(port);
2220                 /*
2221                  * it would be sufficient to test only the normal open flag
2222                  * since the phys. open flag cannot be set if the normal
2223                  * open flag is unset - however, this is for readabilty ...
2224                  */
2225                 if (atomic_test_mask((ZFCP_STATUS_PORT_PHYS_OPEN |
2226                                       ZFCP_STATUS_COMMON_OPEN),
2227                                      &port->status)) {
2228                         ZFCP_LOG_DEBUG("port 0x%016Lx is open -> trying "
2229                                        "close physical\n", port->wwpn);
2230                         retval =
2231                             zfcp_erp_port_forced_strategy_close(erp_action);
2232                 } else
2233                         retval = ZFCP_ERP_FAILED;
2234                 break;
2235
2236         case ZFCP_ERP_STEP_PHYS_PORT_CLOSING:
2237                 if (atomic_test_mask(ZFCP_STATUS_PORT_PHYS_OPEN,
2238                                      &port->status)) {
2239                         ZFCP_LOG_DEBUG("close physical failed for port "
2240                                        "0x%016Lx\n", port->wwpn);
2241                         retval = ZFCP_ERP_FAILED;
2242                 } else
2243                         retval = ZFCP_ERP_SUCCEEDED;
2244                 break;
2245         }
2246
2247         debug_text_event(adapter->erp_dbf, 3, "p_pfst/ret");
2248         debug_event(adapter->erp_dbf, 3, &port->wwpn, sizeof (wwn_t));
2249         debug_event(adapter->erp_dbf, 3, &erp_action->action, sizeof (int));
2250         debug_event(adapter->erp_dbf, 3, &retval, sizeof (int));
2251
2252         return retval;
2253 }
2254
2255 /*
2256  * function:    
2257  *
2258  * purpose:     this routine executes the 'Reopen Port' action
2259  *
2260  * returns:     ZFCP_ERP_CONTINUES      - action continues (asynchronously)
2261  *              ZFCP_ERP_SUCCEEDED      - action finished successfully
2262  *              ZFCP_ERP_FAILED         - action finished unsuccessfully
2263  */
2264 static int
2265 zfcp_erp_port_strategy(struct zfcp_erp_action *erp_action)
2266 {
2267         int retval = ZFCP_ERP_FAILED;
2268         struct zfcp_port *port = erp_action->port;
2269         struct zfcp_adapter *adapter = erp_action->adapter;
2270
2271         switch (erp_action->step) {
2272
2273                 /*
2274                  * FIXME:
2275                  * the ULP spec. begs for waiting for oustanding commands
2276                  */
2277         case ZFCP_ERP_STEP_UNINITIALIZED:
2278                 zfcp_erp_port_strategy_clearstati(port);
2279                 if (atomic_test_mask(ZFCP_STATUS_COMMON_OPEN, &port->status)) {
2280                         ZFCP_LOG_DEBUG("port 0x%016Lx is open -> trying "
2281                                        "close\n", port->wwpn);
2282                         retval = zfcp_erp_port_strategy_close(erp_action);
2283                         goto out;
2284                 }               /* else it's already closed, open it */
2285                 break;
2286
2287         case ZFCP_ERP_STEP_PORT_CLOSING:
2288                 if (atomic_test_mask(ZFCP_STATUS_COMMON_OPEN, &port->status)) {
2289                         ZFCP_LOG_DEBUG("close failed for port 0x%016Lx\n",
2290                                        port->wwpn);
2291                         retval = ZFCP_ERP_FAILED;
2292                         goto out;
2293                 }               /* else it's closed now, open it */
2294                 break;
2295         }
2296         if (erp_action->status & ZFCP_STATUS_ERP_CLOSE_ONLY)
2297                 retval = ZFCP_ERP_EXIT;
2298         else
2299                 retval = zfcp_erp_port_strategy_open(erp_action);
2300
2301  out:
2302         debug_text_event(adapter->erp_dbf, 3, "p_pst/ret");
2303         debug_event(adapter->erp_dbf, 3, &port->wwpn, sizeof (wwn_t));
2304         debug_event(adapter->erp_dbf, 3, &erp_action->action, sizeof (int));
2305         debug_event(adapter->erp_dbf, 3, &retval, sizeof (int));
2306
2307         return retval;
2308 }
2309
2310 static int
2311 zfcp_erp_port_strategy_open(struct zfcp_erp_action *erp_action)
2312 {
2313         int retval;
2314
2315         if (atomic_test_mask(ZFCP_STATUS_PORT_WKA,
2316                              &erp_action->port->status))
2317                 retval = zfcp_erp_port_strategy_open_nameserver(erp_action);
2318         else
2319                 retval = zfcp_erp_port_strategy_open_common(erp_action);
2320
2321         return retval;
2322 }
2323
2324 static int
2325 zfcp_erp_port_strategy_open_common(struct zfcp_erp_action *erp_action)
2326 {
2327         int retval = 0;
2328         struct zfcp_adapter *adapter = erp_action->adapter;
2329         struct zfcp_port *port = erp_action->port;
2330
2331         switch (erp_action->step) {
2332
2333         case ZFCP_ERP_STEP_UNINITIALIZED:
2334         case ZFCP_ERP_STEP_PHYS_PORT_CLOSING:
2335         case ZFCP_ERP_STEP_PORT_CLOSING:
2336                 if (fc_host_port_type(adapter->scsi_host) == FC_PORTTYPE_PTP) {
2337                         if (port->wwpn != adapter->peer_wwpn) {
2338                                 ZFCP_LOG_NORMAL("Failed to open port 0x%016Lx "
2339                                                 "on adapter %s.\nPeer WWPN "
2340                                                 "0x%016Lx does not match\n",
2341                                                 port->wwpn,
2342                                                 zfcp_get_busid_by_adapter(adapter),
2343                                                 adapter->peer_wwpn);
2344                                 zfcp_erp_port_failed(port);
2345                                 retval = ZFCP_ERP_FAILED;
2346                                 break;
2347                         }
2348                         port->d_id = adapter->peer_d_id;
2349                         atomic_set_mask(ZFCP_STATUS_PORT_DID_DID, &port->status);
2350                         retval = zfcp_erp_port_strategy_open_port(erp_action);
2351                         break;
2352                 }
2353                 if (!(adapter->nameserver_port)) {
2354                         retval = zfcp_nameserver_enqueue(adapter);
2355                         if (retval != 0) {
2356                                 ZFCP_LOG_NORMAL("error: nameserver port "
2357                                                 "unavailable for adapter %s\n",
2358                                                 zfcp_get_busid_by_adapter(adapter));
2359                                 retval = ZFCP_ERP_FAILED;
2360                                 break;
2361                         }
2362                 }
2363                 if (!atomic_test_mask(ZFCP_STATUS_COMMON_UNBLOCKED,
2364                                       &adapter->nameserver_port->status)) {
2365                         ZFCP_LOG_DEBUG("nameserver port is not open -> open "
2366                                        "nameserver port\n");
2367                         /* nameserver port may live again */
2368                         atomic_set_mask(ZFCP_STATUS_COMMON_RUNNING,
2369                                         &adapter->nameserver_port->status);
2370                         if (zfcp_erp_port_reopen(adapter->nameserver_port, 0)
2371                             >= 0) {
2372                                 erp_action->step =
2373                                         ZFCP_ERP_STEP_NAMESERVER_OPEN;
2374                                 retval = ZFCP_ERP_CONTINUES;
2375                         } else
2376                                 retval = ZFCP_ERP_FAILED;
2377                         break;
2378                 }
2379                 /* else nameserver port is already open, fall through */
2380         case ZFCP_ERP_STEP_NAMESERVER_OPEN:
2381                 if (!atomic_test_mask(ZFCP_STATUS_COMMON_OPEN,
2382                                       &adapter->nameserver_port->status)) {
2383                         ZFCP_LOG_DEBUG("open failed for nameserver port\n");
2384                         retval = ZFCP_ERP_FAILED;
2385                 } else {
2386                         ZFCP_LOG_DEBUG("nameserver port is open -> "
2387                                        "nameserver look-up for port 0x%016Lx\n",
2388                                        port->wwpn);
2389                         retval = zfcp_erp_port_strategy_open_common_lookup
2390                                 (erp_action);
2391                 }
2392                 break;
2393
2394         case ZFCP_ERP_STEP_NAMESERVER_LOOKUP:
2395                 if (!atomic_test_mask(ZFCP_STATUS_PORT_DID_DID, &port->status)) {
2396                         if (atomic_test_mask
2397                             (ZFCP_STATUS_PORT_INVALID_WWPN, &port->status)) {
2398                                 ZFCP_LOG_DEBUG("nameserver look-up failed "
2399                                                "for port 0x%016Lx "
2400                                                "(misconfigured WWPN?)\n",
2401                                                port->wwpn);
2402                                 zfcp_erp_port_failed(port);
2403                                 retval = ZFCP_ERP_EXIT;
2404                         } else {
2405                                 ZFCP_LOG_DEBUG("nameserver look-up failed for "
2406                                                "port 0x%016Lx\n", port->wwpn);
2407                                 retval = ZFCP_ERP_FAILED;
2408                         }
2409                 } else {
2410                         ZFCP_LOG_DEBUG("port 0x%016Lx has d_id=0x%08x -> "
2411                                        "trying open\n", port->wwpn, port->d_id);
2412                         retval = zfcp_erp_port_strategy_open_port(erp_action);
2413                 }
2414                 break;
2415
2416         case ZFCP_ERP_STEP_PORT_OPENING:
2417                 /* D_ID might have changed during open */
2418                 if (atomic_test_mask((ZFCP_STATUS_COMMON_OPEN |
2419                                       ZFCP_STATUS_PORT_DID_DID),
2420                                      &port->status)) {
2421                         ZFCP_LOG_DEBUG("port 0x%016Lx is open\n", port->wwpn);
2422                         retval = ZFCP_ERP_SUCCEEDED;
2423                 } else {
2424                         ZFCP_LOG_DEBUG("open failed for port 0x%016Lx\n",
2425                                        port->wwpn);
2426                         retval = ZFCP_ERP_FAILED;
2427                 }
2428                 break;
2429
2430         default:
2431                 ZFCP_LOG_NORMAL("bug: unknown erp step 0x%08x\n",
2432                                 erp_action->step);
2433                 retval = ZFCP_ERP_FAILED;
2434         }
2435
2436         return retval;
2437 }
2438
2439 static int
2440 zfcp_erp_port_strategy_open_nameserver(struct zfcp_erp_action *erp_action)
2441 {
2442         int retval;
2443         struct zfcp_port *port = erp_action->port;
2444
2445         switch (erp_action->step) {
2446
2447         case ZFCP_ERP_STEP_UNINITIALIZED:
2448         case ZFCP_ERP_STEP_PHYS_PORT_CLOSING:
2449         case ZFCP_ERP_STEP_PORT_CLOSING:
2450                 ZFCP_LOG_DEBUG("port 0x%016Lx has d_id=0x%08x -> trying open\n",
2451                                port->wwpn, port->d_id);
2452                 retval = zfcp_erp_port_strategy_open_port(erp_action);
2453                 break;
2454
2455         case ZFCP_ERP_STEP_PORT_OPENING:
2456                 if (atomic_test_mask(ZFCP_STATUS_COMMON_OPEN, &port->status)) {
2457                         ZFCP_LOG_DEBUG("WKA port is open\n");
2458                         retval = ZFCP_ERP_SUCCEEDED;
2459                 } else {
2460                         ZFCP_LOG_DEBUG("open failed for WKA port\n");
2461                         retval = ZFCP_ERP_FAILED;
2462                 }
2463                 /* this is needed anyway (dont care for retval of wakeup) */
2464                 ZFCP_LOG_DEBUG("continue other open port operations\n");
2465                 zfcp_erp_port_strategy_open_nameserver_wakeup(erp_action);
2466                 break;
2467
2468         default:
2469                 ZFCP_LOG_NORMAL("bug: unknown erp step 0x%08x\n",
2470                                 erp_action->step);
2471                 retval = ZFCP_ERP_FAILED;
2472         }
2473
2474         return retval;
2475 }
2476
2477 /*
2478  * function:    
2479  *
2480  * purpose:     makes the erp thread continue with reopen (physical) port
2481  *              actions which have been paused until the name server port
2482  *              is opened (or failed)
2483  *
2484  * returns:     0       (a kind of void retval, its not used)
2485  */
2486 static int
2487 zfcp_erp_port_strategy_open_nameserver_wakeup(struct zfcp_erp_action
2488                                               *ns_erp_action)
2489 {
2490         int retval = 0;
2491         unsigned long flags;
2492         struct zfcp_adapter *adapter = ns_erp_action->adapter;
2493         struct zfcp_erp_action *erp_action, *tmp;
2494
2495         read_lock_irqsave(&adapter->erp_lock, flags);
2496         list_for_each_entry_safe(erp_action, tmp, &adapter->erp_running_head,
2497                                  list) {
2498                 debug_text_event(adapter->erp_dbf, 4, "p_pstnsw_n");
2499                 debug_event(adapter->erp_dbf, 4, &erp_action->port->wwpn,
2500                             sizeof (wwn_t));
2501                 if (erp_action->step == ZFCP_ERP_STEP_NAMESERVER_OPEN) {
2502                         debug_text_event(adapter->erp_dbf, 3, "p_pstnsw_w");
2503                         debug_event(adapter->erp_dbf, 3,
2504                                     &erp_action->port->wwpn, sizeof (wwn_t));
2505                         if (atomic_test_mask(
2506                                     ZFCP_STATUS_COMMON_ERP_FAILED,
2507                                     &adapter->nameserver_port->status))
2508                                 zfcp_erp_port_failed(erp_action->port);
2509                         zfcp_erp_action_ready(erp_action);
2510                 }
2511         }
2512         read_unlock_irqrestore(&adapter->erp_lock, flags);
2513
2514         return retval;
2515 }
2516
2517 /*
2518  * function:    
2519  *
2520  * purpose:     
2521  *
2522  * returns:     ZFCP_ERP_CONTINUES      - action continues (asynchronously)
2523  *              ZFCP_ERP_FAILED         - action finished unsuccessfully
2524  */
2525 static int
2526 zfcp_erp_port_forced_strategy_close(struct zfcp_erp_action *erp_action)
2527 {
2528         int retval;
2529         struct zfcp_adapter *adapter = erp_action->adapter;
2530         struct zfcp_port *port = erp_action->port;
2531
2532         retval = zfcp_fsf_close_physical_port(erp_action);
2533         if (retval == -ENOMEM) {
2534                 debug_text_event(adapter->erp_dbf, 5, "o_pfstc_nomem");
2535                 debug_event(adapter->erp_dbf, 5, &port->wwpn, sizeof (wwn_t));
2536                 retval = ZFCP_ERP_NOMEM;
2537                 goto out;
2538         }
2539         erp_action->step = ZFCP_ERP_STEP_PHYS_PORT_CLOSING;
2540         if (retval != 0) {
2541                 debug_text_event(adapter->erp_dbf, 5, "o_pfstc_cpf");
2542                 debug_event(adapter->erp_dbf, 5, &port->wwpn, sizeof (wwn_t));
2543                 /* could not send 'open', fail */
2544                 retval = ZFCP_ERP_FAILED;
2545                 goto out;
2546         }
2547         debug_text_event(adapter->erp_dbf, 6, "o_pfstc_cpok");
2548         debug_event(adapter->erp_dbf, 6, &port->wwpn, sizeof (wwn_t));
2549         retval = ZFCP_ERP_CONTINUES;
2550  out:
2551         return retval;
2552 }
2553
2554 static int
2555 zfcp_erp_port_strategy_clearstati(struct zfcp_port *port)
2556 {
2557         int retval = 0;
2558         struct zfcp_adapter *adapter = port->adapter;
2559
2560         debug_text_event(adapter->erp_dbf, 5, "p_pstclst");
2561         debug_event(adapter->erp_dbf, 5, &port->wwpn, sizeof (wwn_t));
2562
2563         atomic_clear_mask(ZFCP_STATUS_COMMON_OPENING |
2564                           ZFCP_STATUS_COMMON_CLOSING |
2565                           ZFCP_STATUS_COMMON_ACCESS_DENIED |
2566                           ZFCP_STATUS_PORT_DID_DID |
2567                           ZFCP_STATUS_PORT_PHYS_CLOSING |
2568                           ZFCP_STATUS_PORT_INVALID_WWPN,
2569                           &port->status);
2570         return retval;
2571 }
2572
2573 /*
2574  * function:    
2575  *
2576  * purpose:     
2577  *
2578  * returns:     ZFCP_ERP_CONTINUES      - action continues (asynchronously)
2579  *              ZFCP_ERP_FAILED         - action finished unsuccessfully
2580  */
2581 static int
2582 zfcp_erp_port_strategy_close(struct zfcp_erp_action *erp_action)
2583 {
2584         int retval;
2585         struct zfcp_adapter *adapter = erp_action->adapter;
2586         struct zfcp_port *port = erp_action->port;
2587
2588         retval = zfcp_fsf_close_port(erp_action);
2589         if (retval == -ENOMEM) {
2590                 debug_text_event(adapter->erp_dbf, 5, "p_pstc_nomem");
2591                 debug_event(adapter->erp_dbf, 5, &port->wwpn, sizeof (wwn_t));
2592                 retval = ZFCP_ERP_NOMEM;
2593                 goto out;
2594         }
2595         erp_action->step = ZFCP_ERP_STEP_PORT_CLOSING;
2596         if (retval != 0) {
2597                 debug_text_event(adapter->erp_dbf, 5, "p_pstc_cpf");
2598                 debug_event(adapter->erp_dbf, 5, &port->wwpn, sizeof (wwn_t));
2599                 /* could not send 'close', fail */
2600                 retval = ZFCP_ERP_FAILED;
2601                 goto out;
2602         }
2603         debug_text_event(adapter->erp_dbf, 6, "p_pstc_cpok");
2604         debug_event(adapter->erp_dbf, 6, &port->wwpn, sizeof (wwn_t));
2605         retval = ZFCP_ERP_CONTINUES;
2606  out:
2607         return retval;
2608 }
2609
2610 /*
2611  * function:    
2612  *
2613  * purpose:     
2614  *
2615  * returns:     ZFCP_ERP_CONTINUES      - action continues (asynchronously)
2616  *              ZFCP_ERP_FAILED         - action finished unsuccessfully
2617  */
2618 static int
2619 zfcp_erp_port_strategy_open_port(struct zfcp_erp_action *erp_action)
2620 {
2621         int retval;
2622         struct zfcp_adapter *adapter = erp_action->adapter;
2623         struct zfcp_port *port = erp_action->port;
2624
2625         retval = zfcp_fsf_open_port(erp_action);
2626         if (retval == -ENOMEM) {
2627                 debug_text_event(adapter->erp_dbf, 5, "p_psto_nomem");
2628                 debug_event(adapter->erp_dbf, 5, &port->wwpn, sizeof (wwn_t));
2629                 retval = ZFCP_ERP_NOMEM;
2630                 goto out;
2631         }
2632         erp_action->step = ZFCP_ERP_STEP_PORT_OPENING;
2633         if (retval != 0) {
2634                 debug_text_event(adapter->erp_dbf, 5, "p_psto_opf");
2635                 debug_event(adapter->erp_dbf, 5, &port->wwpn, sizeof (wwn_t));
2636                 /* could not send 'open', fail */
2637                 retval = ZFCP_ERP_FAILED;
2638                 goto out;
2639         }
2640         debug_text_event(adapter->erp_dbf, 6, "p_psto_opok");
2641         debug_event(adapter->erp_dbf, 6, &port->wwpn, sizeof (wwn_t));
2642         retval = ZFCP_ERP_CONTINUES;
2643  out:
2644         return retval;
2645 }
2646
2647 /*
2648  * function:    
2649  *
2650  * purpose:     
2651  *
2652  * returns:     ZFCP_ERP_CONTINUES      - action continues (asynchronously)
2653  *              ZFCP_ERP_FAILED         - action finished unsuccessfully
2654  */
2655 static int
2656 zfcp_erp_port_strategy_open_common_lookup(struct zfcp_erp_action *erp_action)
2657 {
2658         int retval;
2659         struct zfcp_adapter *adapter = erp_action->adapter;
2660         struct zfcp_port *port = erp_action->port;
2661
2662         retval = zfcp_ns_gid_pn_request(erp_action);
2663         if (retval == -ENOMEM) {
2664                 debug_text_event(adapter->erp_dbf, 5, "p_pstn_nomem");
2665                 debug_event(adapter->erp_dbf, 5, &port->wwpn, sizeof (wwn_t));
2666                 retval = ZFCP_ERP_NOMEM;
2667                 goto out;
2668         }
2669         erp_action->step = ZFCP_ERP_STEP_NAMESERVER_LOOKUP;
2670         if (retval != 0) {
2671                 debug_text_event(adapter->erp_dbf, 5, "p_pstn_ref");
2672                 debug_event(adapter->erp_dbf, 5, &port->wwpn, sizeof (wwn_t));
2673                 /* could not send nameserver request, fail */
2674                 retval = ZFCP_ERP_FAILED;
2675                 goto out;
2676         }
2677         debug_text_event(adapter->erp_dbf, 6, "p_pstn_reok");
2678         debug_event(adapter->erp_dbf, 6, &port->wwpn, sizeof (wwn_t));
2679         retval = ZFCP_ERP_CONTINUES;
2680  out:
2681         return retval;
2682 }
2683
2684 /*
2685  * function:    
2686  *
2687  * purpose:     this routine executes the 'Reopen Unit' action
2688  *              currently no retries
2689  *
2690  * returns:     ZFCP_ERP_CONTINUES      - action continues (asynchronously)
2691  *              ZFCP_ERP_SUCCEEDED      - action finished successfully
2692  *              ZFCP_ERP_FAILED         - action finished unsuccessfully
2693  */
2694 static int
2695 zfcp_erp_unit_strategy(struct zfcp_erp_action *erp_action)
2696 {
2697         int retval = ZFCP_ERP_FAILED;
2698         struct zfcp_unit *unit = erp_action->unit;
2699         struct zfcp_adapter *adapter = erp_action->adapter;
2700
2701         switch (erp_action->step) {
2702
2703                 /*
2704                  * FIXME:
2705                  * the ULP spec. begs for waiting for oustanding commands
2706                  */
2707         case ZFCP_ERP_STEP_UNINITIALIZED:
2708                 zfcp_erp_unit_strategy_clearstati(unit);
2709                 if (atomic_test_mask(ZFCP_STATUS_COMMON_OPEN, &unit->status)) {
2710                         ZFCP_LOG_DEBUG("unit 0x%016Lx is open -> "
2711                                        "trying close\n", unit->fcp_lun);
2712                         retval = zfcp_erp_unit_strategy_close(erp_action);
2713                         break;
2714                 }
2715                 /* else it's already closed, fall through */
2716         case ZFCP_ERP_STEP_UNIT_CLOSING:
2717                 if (atomic_test_mask(ZFCP_STATUS_COMMON_OPEN, &unit->status)) {
2718                         ZFCP_LOG_DEBUG("close failed for unit 0x%016Lx\n",
2719                                        unit->fcp_lun);
2720                         retval = ZFCP_ERP_FAILED;
2721                 } else {
2722                         if (erp_action->status & ZFCP_STATUS_ERP_CLOSE_ONLY)
2723                                 retval = ZFCP_ERP_EXIT;
2724                         else {
2725                                 ZFCP_LOG_DEBUG("unit 0x%016Lx is not open -> "
2726                                                "trying open\n", unit->fcp_lun);
2727                                 retval =
2728                                     zfcp_erp_unit_strategy_open(erp_action);
2729                         }
2730                 }
2731                 break;
2732
2733         case ZFCP_ERP_STEP_UNIT_OPENING:
2734                 if (atomic_test_mask(ZFCP_STATUS_COMMON_OPEN, &unit->status)) {
2735                         ZFCP_LOG_DEBUG("unit 0x%016Lx is open\n",
2736                                        unit->fcp_lun);
2737                         retval = ZFCP_ERP_SUCCEEDED;
2738                 } else {
2739                         ZFCP_LOG_DEBUG("open failed for unit 0x%016Lx\n",
2740                                        unit->fcp_lun);
2741                         retval = ZFCP_ERP_FAILED;
2742                 }
2743                 break;
2744         }
2745
2746         debug_text_event(adapter->erp_dbf, 3, "u_ust/ret");
2747         debug_event(adapter->erp_dbf, 3, &unit->fcp_lun, sizeof (fcp_lun_t));
2748         debug_event(adapter->erp_dbf, 3, &erp_action->action, sizeof (int));
2749         debug_event(adapter->erp_dbf, 3, &retval, sizeof (int));
2750         return retval;
2751 }
2752
2753 static int
2754 zfcp_erp_unit_strategy_clearstati(struct zfcp_unit *unit)
2755 {
2756         int retval = 0;
2757         struct zfcp_adapter *adapter = unit->port->adapter;
2758
2759         debug_text_event(adapter->erp_dbf, 5, "u_ustclst");
2760         debug_event(adapter->erp_dbf, 5, &unit->fcp_lun, sizeof (fcp_lun_t));
2761
2762         atomic_clear_mask(ZFCP_STATUS_COMMON_OPENING |
2763                           ZFCP_STATUS_COMMON_CLOSING |
2764                           ZFCP_STATUS_COMMON_ACCESS_DENIED |
2765                           ZFCP_STATUS_UNIT_SHARED |
2766                           ZFCP_STATUS_UNIT_READONLY,
2767                           &unit->status);
2768
2769         return retval;
2770 }
2771
2772 /*
2773  * function:    
2774  *
2775  * purpose:     
2776  *
2777  * returns:     ZFCP_ERP_CONTINUES      - action continues (asynchronously)
2778  *              ZFCP_ERP_FAILED         - action finished unsuccessfully
2779  */
2780 static int
2781 zfcp_erp_unit_strategy_close(struct zfcp_erp_action *erp_action)
2782 {
2783         int retval;
2784         struct zfcp_adapter *adapter = erp_action->adapter;
2785         struct zfcp_unit *unit = erp_action->unit;
2786
2787         retval = zfcp_fsf_close_unit(erp_action);
2788         if (retval == -ENOMEM) {
2789                 debug_text_event(adapter->erp_dbf, 5, "u_ustc_nomem");
2790                 debug_event(adapter->erp_dbf, 5, &unit->fcp_lun,
2791                             sizeof (fcp_lun_t));
2792                 retval = ZFCP_ERP_NOMEM;
2793                 goto out;
2794         }
2795         erp_action->step = ZFCP_ERP_STEP_UNIT_CLOSING;
2796         if (retval != 0) {
2797                 debug_text_event(adapter->erp_dbf, 5, "u_ustc_cuf");
2798                 debug_event(adapter->erp_dbf, 5, &unit->fcp_lun,
2799                             sizeof (fcp_lun_t));
2800                 /* could not send 'close', fail */
2801                 retval = ZFCP_ERP_FAILED;
2802                 goto out;
2803         }
2804         debug_text_event(adapter->erp_dbf, 6, "u_ustc_cuok");
2805         debug_event(adapter->erp_dbf, 6, &unit->fcp_lun, sizeof (fcp_lun_t));
2806         retval = ZFCP_ERP_CONTINUES;
2807
2808  out:
2809         return retval;
2810 }
2811
2812 /*
2813  * function:    
2814  *
2815  * purpose:     
2816  *
2817  * returns:     ZFCP_ERP_CONTINUES      - action continues (asynchronously)
2818  *              ZFCP_ERP_FAILED         - action finished unsuccessfully
2819  */
2820 static int
2821 zfcp_erp_unit_strategy_open(struct zfcp_erp_action *erp_action)
2822 {
2823         int retval;
2824         struct zfcp_adapter *adapter = erp_action->adapter;
2825         struct zfcp_unit *unit = erp_action->unit;
2826
2827         retval = zfcp_fsf_open_unit(erp_action);
2828         if (retval == -ENOMEM) {
2829                 debug_text_event(adapter->erp_dbf, 5, "u_usto_nomem");
2830                 debug_event(adapter->erp_dbf, 5, &unit->fcp_lun,
2831                             sizeof (fcp_lun_t));
2832                 retval = ZFCP_ERP_NOMEM;
2833                 goto out;
2834         }
2835         erp_action->step = ZFCP_ERP_STEP_UNIT_OPENING;
2836         if (retval != 0) {
2837                 debug_text_event(adapter->erp_dbf, 5, "u_usto_ouf");
2838                 debug_event(adapter->erp_dbf, 5, &unit->fcp_lun,
2839                             sizeof (fcp_lun_t));
2840                 /* could not send 'open', fail */
2841                 retval = ZFCP_ERP_FAILED;
2842                 goto out;
2843         }
2844         debug_text_event(adapter->erp_dbf, 6, "u_usto_ouok");
2845         debug_event(adapter->erp_dbf, 6, &unit->fcp_lun, sizeof (fcp_lun_t));
2846         retval = ZFCP_ERP_CONTINUES;
2847  out:
2848         return retval;
2849 }
2850
2851 void zfcp_erp_start_timer(struct zfcp_fsf_req *fsf_req)
2852 {
2853         BUG_ON(!fsf_req->erp_action);
2854         fsf_req->timer.function = zfcp_erp_timeout_handler;
2855         fsf_req->timer.data = (unsigned long) fsf_req->erp_action;
2856         fsf_req->timer.expires = jiffies + ZFCP_ERP_FSFREQ_TIMEOUT;
2857         add_timer(&fsf_req->timer);
2858 }
2859
2860 /*
2861  * function:    
2862  *
2863  * purpose:     enqueue the specified error recovery action, if needed
2864  *
2865  * returns:
2866  */
2867 static int
2868 zfcp_erp_action_enqueue(int action,
2869                         struct zfcp_adapter *adapter,
2870                         struct zfcp_port *port, struct zfcp_unit *unit)
2871 {
2872         int retval = 1;
2873         struct zfcp_erp_action *erp_action = NULL;
2874         int stronger_action = 0;
2875         u32 status = 0;
2876
2877         /*
2878          * We need some rules here which check whether we really need
2879          * this action or whether we should just drop it.
2880          * E.g. if there is a unfinished 'Reopen Port' request then we drop a
2881          * 'Reopen Unit' request for an associated unit since we can't
2882          * satisfy this request now. A 'Reopen Port' action will trigger
2883          * 'Reopen Unit' actions when it completes.
2884          * Thus, there are only actions in the queue which can immediately be
2885          * executed. This makes the processing of the action queue more
2886          * efficient.
2887          */
2888
2889         if (!atomic_test_mask(ZFCP_STATUS_ADAPTER_ERP_THREAD_UP,
2890                               &adapter->status))
2891                 return -EIO;
2892
2893         debug_event(adapter->erp_dbf, 4, &action, sizeof (int));
2894         /* check whether we really need this */
2895         switch (action) {
2896         case ZFCP_ERP_ACTION_REOPEN_UNIT:
2897                 if (atomic_test_mask
2898                     (ZFCP_STATUS_COMMON_ERP_INUSE, &unit->status)) {
2899                         debug_text_event(adapter->erp_dbf, 4, "u_actenq_drp");
2900                         debug_event(adapter->erp_dbf, 4, &port->wwpn,
2901                                     sizeof (wwn_t));
2902                         debug_event(adapter->erp_dbf, 4, &unit->fcp_lun,
2903                                     sizeof (fcp_lun_t));
2904                         goto out;
2905                 }
2906                 if (!atomic_test_mask
2907                     (ZFCP_STATUS_COMMON_RUNNING, &port->status) ||
2908                     atomic_test_mask
2909                     (ZFCP_STATUS_COMMON_ERP_FAILED, &port->status)) {
2910                         goto out;
2911                 }
2912                 if (!atomic_test_mask
2913                     (ZFCP_STATUS_COMMON_UNBLOCKED, &port->status)) {
2914                         stronger_action = ZFCP_ERP_ACTION_REOPEN_PORT;
2915                         unit = NULL;
2916                 }
2917                 /* fall through !!! */
2918
2919         case ZFCP_ERP_ACTION_REOPEN_PORT:
2920                 if (atomic_test_mask
2921                     (ZFCP_STATUS_COMMON_ERP_INUSE, &port->status)) {
2922                         debug_text_event(adapter->erp_dbf, 4, "p_actenq_drp");
2923                         debug_event(adapter->erp_dbf, 4, &port->wwpn,
2924                                     sizeof (wwn_t));
2925                         goto out;
2926                 }
2927                 /* fall through !!! */
2928
2929         case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
2930                 if (atomic_test_mask(ZFCP_STATUS_COMMON_ERP_INUSE,
2931                                      &port->status)) {
2932                         if (port->erp_action.action !=
2933                             ZFCP_ERP_ACTION_REOPEN_PORT_FORCED) {
2934                                 ZFCP_LOG_INFO("dropped erp action %i (port "
2935                                               "0x%016Lx, action in use: %i)\n",
2936                                               action, port->wwpn,
2937                                               port->erp_action.action);
2938                                 debug_text_event(adapter->erp_dbf, 4,
2939                                                  "pf_actenq_drp");
2940                         } else 
2941                                 debug_text_event(adapter->erp_dbf, 4,
2942                                                  "pf_actenq_drpcp");
2943                         debug_event(adapter->erp_dbf, 4, &port->wwpn,
2944                                     sizeof (wwn_t));
2945                         goto out;
2946                 }
2947                 if (!atomic_test_mask
2948                     (ZFCP_STATUS_COMMON_RUNNING, &adapter->status) ||
2949                     atomic_test_mask
2950                     (ZFCP_STATUS_COMMON_ERP_FAILED, &adapter->status)) {
2951                         goto out;
2952                 }
2953                 if (!atomic_test_mask
2954                     (ZFCP_STATUS_COMMON_UNBLOCKED, &adapter->status)) {
2955                         stronger_action = ZFCP_ERP_ACTION_REOPEN_ADAPTER;
2956                         port = NULL;
2957                 }
2958                 /* fall through !!! */
2959
2960         case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
2961                 if (atomic_test_mask
2962                     (ZFCP_STATUS_COMMON_ERP_INUSE, &adapter->status)) {
2963                         debug_text_event(adapter->erp_dbf, 4, "a_actenq_drp");
2964                         goto out;
2965                 }
2966                 break;
2967
2968         default:
2969                 debug_text_exception(adapter->erp_dbf, 1, "a_actenq_bug");
2970                 debug_event(adapter->erp_dbf, 1, &action, sizeof (int));
2971                 ZFCP_LOG_NORMAL("bug: unknown erp action requested "
2972                                 "on adapter %s (action=%d)\n",
2973                                 zfcp_get_busid_by_adapter(adapter), action);
2974                 goto out;
2975         }
2976
2977         /* check whether we need something stronger first */
2978         if (stronger_action) {
2979                 debug_text_event(adapter->erp_dbf, 4, "a_actenq_str");
2980                 debug_event(adapter->erp_dbf, 4, &stronger_action,
2981                             sizeof (int));
2982                 ZFCP_LOG_DEBUG("stronger erp action %d needed before "
2983                                "erp action %d on adapter %s\n",
2984                                stronger_action, action,
2985                                zfcp_get_busid_by_adapter(adapter));
2986                 action = stronger_action;
2987         }
2988
2989         /* mark adapter to have some error recovery pending */
2990         atomic_set_mask(ZFCP_STATUS_ADAPTER_ERP_PENDING, &adapter->status);
2991
2992         /* setup error recovery action */
2993         switch (action) {
2994
2995         case ZFCP_ERP_ACTION_REOPEN_UNIT:
2996                 zfcp_unit_get(unit);
2997                 atomic_set_mask(ZFCP_STATUS_COMMON_ERP_INUSE, &unit->status);
2998                 erp_action = &unit->erp_action;
2999                 if (!atomic_test_mask
3000                     (ZFCP_STATUS_COMMON_RUNNING, &unit->status))
3001                         status = ZFCP_STATUS_ERP_CLOSE_ONLY;
3002                 break;
3003
3004         case ZFCP_ERP_ACTION_REOPEN_PORT:
3005         case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
3006                 zfcp_port_get(port);
3007                 zfcp_erp_action_dismiss_port(port);
3008                 atomic_set_mask(ZFCP_STATUS_COMMON_ERP_INUSE, &port->status);
3009                 erp_action = &port->erp_action;
3010                 if (!atomic_test_mask
3011                     (ZFCP_STATUS_COMMON_RUNNING, &port->status))
3012                         status = ZFCP_STATUS_ERP_CLOSE_ONLY;
3013                 break;
3014
3015         case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
3016                 zfcp_adapter_get(adapter);
3017                 zfcp_erp_action_dismiss_adapter(adapter);
3018                 atomic_set_mask(ZFCP_STATUS_COMMON_ERP_INUSE, &adapter->status);
3019                 erp_action = &adapter->erp_action;
3020                 if (!atomic_test_mask
3021                     (ZFCP_STATUS_COMMON_RUNNING, &adapter->status))
3022                         status = ZFCP_STATUS_ERP_CLOSE_ONLY;
3023                 break;
3024         }
3025
3026         debug_text_event(adapter->erp_dbf, 4, "a_actenq");
3027
3028         memset(erp_action, 0, sizeof (struct zfcp_erp_action));
3029         erp_action->adapter = adapter;
3030         erp_action->port = port;
3031         erp_action->unit = unit;
3032         erp_action->action = action;
3033         erp_action->status = status;
3034
3035         ++adapter->erp_total_count;
3036
3037         /* finally put it into 'ready' queue and kick erp thread */
3038         list_add(&erp_action->list, &adapter->erp_ready_head);
3039         up(&adapter->erp_ready_sem);
3040         retval = 0;
3041  out:
3042         return retval;
3043 }
3044
3045 static int
3046 zfcp_erp_action_dequeue(struct zfcp_erp_action *erp_action)
3047 {
3048         int retval = 0;
3049         struct zfcp_adapter *adapter = erp_action->adapter;
3050
3051         --adapter->erp_total_count;
3052         if (erp_action->status & ZFCP_STATUS_ERP_LOWMEM) {
3053                 --adapter->erp_low_mem_count;
3054                 erp_action->status &= ~ZFCP_STATUS_ERP_LOWMEM;
3055         }
3056
3057         debug_text_event(adapter->erp_dbf, 4, "a_actdeq");
3058         debug_event(adapter->erp_dbf, 4, &erp_action->action, sizeof (int));
3059         list_del(&erp_action->list);
3060         switch (erp_action->action) {
3061         case ZFCP_ERP_ACTION_REOPEN_UNIT:
3062                 atomic_clear_mask(ZFCP_STATUS_COMMON_ERP_INUSE,
3063                                   &erp_action->unit->status);
3064                 break;
3065         case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
3066         case ZFCP_ERP_ACTION_REOPEN_PORT:
3067                 atomic_clear_mask(ZFCP_STATUS_COMMON_ERP_INUSE,
3068                                   &erp_action->port->status);
3069                 break;
3070         case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
3071                 atomic_clear_mask(ZFCP_STATUS_COMMON_ERP_INUSE,
3072                                   &erp_action->adapter->status);
3073                 break;
3074         default:
3075                 /* bug */
3076                 break;
3077         }
3078         return retval;
3079 }
3080
3081 /**
3082  * zfcp_erp_action_cleanup
3083  *
3084  * Register unit with scsi stack if appropriate and fix reference counts.
3085  * Note: Temporary units are not registered with scsi stack.
3086  */
3087 static void
3088 zfcp_erp_action_cleanup(int action, struct zfcp_adapter *adapter,
3089                         struct zfcp_port *port, struct zfcp_unit *unit,
3090                         int result)
3091 {
3092         switch (action) {
3093         case ZFCP_ERP_ACTION_REOPEN_UNIT:
3094                 if ((result == ZFCP_ERP_SUCCEEDED)
3095                     && (!atomic_test_mask(ZFCP_STATUS_UNIT_TEMPORARY,
3096                                           &unit->status))
3097                     && !unit->device
3098                     && port->rport) {
3099                         atomic_set_mask(ZFCP_STATUS_UNIT_REGISTERED,
3100                                         &unit->status);
3101                         scsi_scan_target(&port->rport->dev, 0,
3102                                          port->rport->scsi_target_id,
3103                                          unit->scsi_lun, 0);
3104                 }
3105                 zfcp_unit_put(unit);
3106                 break;
3107         case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
3108         case ZFCP_ERP_ACTION_REOPEN_PORT:
3109                 if (atomic_test_mask(ZFCP_STATUS_PORT_NO_WWPN,
3110                                      &port->status)) {
3111                         zfcp_port_put(port);
3112                         break;
3113                 }
3114
3115                 if ((result == ZFCP_ERP_SUCCEEDED)
3116                     && !port->rport) {
3117                         struct fc_rport_identifiers ids;
3118                         ids.node_name = port->wwnn;
3119                         ids.port_name = port->wwpn;
3120                         ids.port_id = port->d_id;
3121                         ids.roles = FC_RPORT_ROLE_FCP_TARGET;
3122                         port->rport =
3123                                 fc_remote_port_add(adapter->scsi_host, 0, &ids);
3124                         if (!port->rport)
3125                                 ZFCP_LOG_NORMAL("failed registration of rport"
3126                                                 "(adapter %s, wwpn=0x%016Lx)\n",
3127                                                 zfcp_get_busid_by_port(port),
3128                                                 port->wwpn);
3129                         else {
3130                                 scsi_flush_work(adapter->scsi_host);
3131                                 port->rport->maxframe_size = port->maxframe_size;
3132                                 port->rport->supported_classes =
3133                                         port->supported_classes;
3134                         }
3135                 }
3136                 if ((result != ZFCP_ERP_SUCCEEDED) && port->rport) {
3137                         fc_remote_port_delete(port->rport);
3138                         port->rport = NULL;
3139                 }
3140                 zfcp_port_put(port);
3141                 break;
3142         case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
3143                 if (result != ZFCP_ERP_SUCCEEDED) {
3144                         struct zfcp_port *port;
3145                         list_for_each_entry(port, &adapter->port_list_head, list)
3146                                 if (port->rport &&
3147                                     !atomic_test_mask(ZFCP_STATUS_PORT_WKA,
3148                                                       &port->status)) {
3149                                         fc_remote_port_delete(port->rport);
3150                                         port->rport = NULL;
3151                                 }
3152                 }
3153                 zfcp_adapter_put(adapter);
3154                 break;
3155         default:
3156                 break;
3157         }
3158 }
3159
3160
3161 static void zfcp_erp_action_dismiss_adapter(struct zfcp_adapter *adapter)
3162 {
3163         struct zfcp_port *port;
3164
3165         debug_text_event(adapter->erp_dbf, 5, "a_actab");
3166         if (atomic_test_mask(ZFCP_STATUS_COMMON_ERP_INUSE, &adapter->status))
3167                 zfcp_erp_action_dismiss(&adapter->erp_action);
3168         else
3169                 list_for_each_entry(port, &adapter->port_list_head, list)
3170                     zfcp_erp_action_dismiss_port(port);
3171 }
3172
3173 static void zfcp_erp_action_dismiss_port(struct zfcp_port *port)
3174 {
3175         struct zfcp_unit *unit;
3176         struct zfcp_adapter *adapter = port->adapter;
3177
3178         debug_text_event(adapter->erp_dbf, 5, "p_actab");
3179         debug_event(adapter->erp_dbf, 5, &port->wwpn, sizeof (wwn_t));
3180         if (atomic_test_mask(ZFCP_STATUS_COMMON_ERP_INUSE, &port->status))
3181                 zfcp_erp_action_dismiss(&port->erp_action);
3182         else
3183                 list_for_each_entry(unit, &port->unit_list_head, list)
3184                     zfcp_erp_action_dismiss_unit(unit);
3185 }
3186
3187 static void zfcp_erp_action_dismiss_unit(struct zfcp_unit *unit)
3188 {
3189         struct zfcp_adapter *adapter = unit->port->adapter;
3190
3191         debug_text_event(adapter->erp_dbf, 5, "u_actab");
3192         debug_event(adapter->erp_dbf, 5, &unit->fcp_lun, sizeof (fcp_lun_t));
3193         if (atomic_test_mask(ZFCP_STATUS_COMMON_ERP_INUSE, &unit->status))
3194                 zfcp_erp_action_dismiss(&unit->erp_action);
3195 }
3196
3197 static inline void
3198 zfcp_erp_action_to_running(struct zfcp_erp_action *erp_action)
3199 {
3200         struct zfcp_adapter *adapter = erp_action->adapter;
3201
3202         debug_text_event(adapter->erp_dbf, 6, "a_toru");
3203         debug_event(adapter->erp_dbf, 6, &erp_action->action, sizeof (int));
3204         list_move(&erp_action->list, &erp_action->adapter->erp_running_head);
3205 }
3206
3207 static inline void
3208 zfcp_erp_action_to_ready(struct zfcp_erp_action *erp_action)
3209 {
3210         struct zfcp_adapter *adapter = erp_action->adapter;
3211
3212         debug_text_event(adapter->erp_dbf, 6, "a_tore");
3213         debug_event(adapter->erp_dbf, 6, &erp_action->action, sizeof (int));
3214         list_move(&erp_action->list, &erp_action->adapter->erp_ready_head);
3215 }
3216
3217 void
3218 zfcp_erp_port_boxed(struct zfcp_port *port)
3219 {
3220         struct zfcp_adapter *adapter = port->adapter;
3221         unsigned long flags;
3222
3223         debug_text_event(adapter->erp_dbf, 3, "p_access_boxed");
3224         debug_event(adapter->erp_dbf, 3, &port->wwpn, sizeof(wwn_t));
3225         read_lock_irqsave(&zfcp_data.config_lock, flags);
3226         zfcp_erp_modify_port_status(port,
3227                         ZFCP_STATUS_COMMON_ACCESS_BOXED,
3228                         ZFCP_SET);
3229         read_unlock_irqrestore(&zfcp_data.config_lock, flags);
3230         zfcp_erp_port_reopen(port, ZFCP_STATUS_COMMON_ERP_FAILED);
3231 }
3232
3233 void
3234 zfcp_erp_unit_boxed(struct zfcp_unit *unit)
3235 {
3236         struct zfcp_adapter *adapter = unit->port->adapter;
3237
3238         debug_text_event(adapter->erp_dbf, 3, "u_access_boxed");
3239         debug_event(adapter->erp_dbf, 3, &unit->fcp_lun, sizeof(fcp_lun_t));
3240         zfcp_erp_modify_unit_status(unit,
3241                         ZFCP_STATUS_COMMON_ACCESS_BOXED,
3242                         ZFCP_SET);
3243         zfcp_erp_unit_reopen(unit, ZFCP_STATUS_COMMON_ERP_FAILED);
3244 }
3245
3246 void
3247 zfcp_erp_port_access_denied(struct zfcp_port *port)
3248 {
3249         struct zfcp_adapter *adapter = port->adapter;
3250         unsigned long flags;
3251
3252         debug_text_event(adapter->erp_dbf, 3, "p_access_denied");
3253         debug_event(adapter->erp_dbf, 3, &port->wwpn, sizeof(wwn_t));
3254         read_lock_irqsave(&zfcp_data.config_lock, flags);
3255         zfcp_erp_modify_port_status(port,
3256                         ZFCP_STATUS_COMMON_ERP_FAILED |
3257                         ZFCP_STATUS_COMMON_ACCESS_DENIED,
3258                         ZFCP_SET);
3259         read_unlock_irqrestore(&zfcp_data.config_lock, flags);
3260 }
3261
3262 void
3263 zfcp_erp_unit_access_denied(struct zfcp_unit *unit)
3264 {
3265         struct zfcp_adapter *adapter = unit->port->adapter;
3266
3267         debug_text_event(adapter->erp_dbf, 3, "u_access_denied");
3268         debug_event(adapter->erp_dbf, 3, &unit->fcp_lun, sizeof(fcp_lun_t));
3269         zfcp_erp_modify_unit_status(unit,
3270                         ZFCP_STATUS_COMMON_ERP_FAILED |
3271                         ZFCP_STATUS_COMMON_ACCESS_DENIED,
3272                         ZFCP_SET);
3273 }
3274
3275 void
3276 zfcp_erp_adapter_access_changed(struct zfcp_adapter *adapter)
3277 {
3278         struct zfcp_port *port;
3279         unsigned long flags;
3280
3281         if (adapter->connection_features & FSF_FEATURE_NPIV_MODE)
3282                 return;
3283
3284         debug_text_event(adapter->erp_dbf, 3, "a_access_recover");
3285         debug_event(adapter->erp_dbf, 3, zfcp_get_busid_by_adapter(adapter), 8);
3286
3287         read_lock_irqsave(&zfcp_data.config_lock, flags);
3288         if (adapter->nameserver_port)
3289                 zfcp_erp_port_access_changed(adapter->nameserver_port);
3290         list_for_each_entry(port, &adapter->port_list_head, list)
3291                 if (port != adapter->nameserver_port)
3292                         zfcp_erp_port_access_changed(port);
3293         read_unlock_irqrestore(&zfcp_data.config_lock, flags);
3294 }
3295
3296 void
3297 zfcp_erp_port_access_changed(struct zfcp_port *port)
3298 {
3299         struct zfcp_adapter *adapter = port->adapter;
3300         struct zfcp_unit *unit;
3301
3302         debug_text_event(adapter->erp_dbf, 3, "p_access_recover");
3303         debug_event(adapter->erp_dbf, 3, &port->wwpn, sizeof(wwn_t));
3304
3305         if (!atomic_test_mask(ZFCP_STATUS_COMMON_ACCESS_DENIED,
3306                               &port->status) &&
3307             !atomic_test_mask(ZFCP_STATUS_COMMON_ACCESS_BOXED,
3308                               &port->status)) {
3309                 if (!atomic_test_mask(ZFCP_STATUS_PORT_WKA, &port->status))
3310                         list_for_each_entry(unit, &port->unit_list_head, list)
3311                                 zfcp_erp_unit_access_changed(unit);
3312                 return;
3313         }
3314
3315         ZFCP_LOG_NORMAL("reopen of port 0x%016Lx on adapter %s "
3316                         "(due to ACT update)\n",
3317                         port->wwpn, zfcp_get_busid_by_adapter(adapter));
3318         if (zfcp_erp_port_reopen(port, ZFCP_STATUS_COMMON_ERP_FAILED) != 0)
3319                 ZFCP_LOG_NORMAL("failed reopen of port"
3320                                 "(adapter %s, wwpn=0x%016Lx)\n",
3321                                 zfcp_get_busid_by_adapter(adapter), port->wwpn);
3322 }
3323
3324 void
3325 zfcp_erp_unit_access_changed(struct zfcp_unit *unit)
3326 {
3327         struct zfcp_adapter *adapter = unit->port->adapter;
3328
3329         debug_text_event(adapter->erp_dbf, 3, "u_access_recover");
3330         debug_event(adapter->erp_dbf, 3, &unit->fcp_lun, sizeof(fcp_lun_t));
3331
3332         if (!atomic_test_mask(ZFCP_STATUS_COMMON_ACCESS_DENIED,
3333                               &unit->status) &&
3334             !atomic_test_mask(ZFCP_STATUS_COMMON_ACCESS_BOXED,
3335                               &unit->status))
3336                 return;
3337
3338         ZFCP_LOG_NORMAL("reopen of unit 0x%016Lx on port 0x%016Lx "
3339                         " on adapter %s (due to ACT update)\n",
3340                         unit->fcp_lun, unit->port->wwpn,
3341                         zfcp_get_busid_by_adapter(adapter));
3342         if (zfcp_erp_unit_reopen(unit, ZFCP_STATUS_COMMON_ERP_FAILED) != 0)
3343                 ZFCP_LOG_NORMAL("failed reopen of unit (adapter %s, "
3344                                 "wwpn=0x%016Lx, fcp_lun=0x%016Lx)\n",
3345                                 zfcp_get_busid_by_adapter(adapter),
3346                                 unit->port->wwpn, unit->fcp_lun);
3347 }
3348
3349 #undef ZFCP_LOG_AREA