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