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