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