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