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