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