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