Merge branch 'for-jeff' of git://electric-eye.fr.zoreil.com/home/romieu/linux-2.6
[sfrench/cifs-2.6.git] / drivers / s390 / scsi / zfcp_erp.c
1 /* 
2  * 
3  * linux/drivers/s390/scsi/zfcp_erp.c 
4  * 
5  * FCP adapter driver for IBM eServer zSeries 
6  * 
7  * (C) Copyright IBM Corp. 2002, 2004
8  *
9  * Author(s): Martin Peschke <mpeschke@de.ibm.com> 
10  *            Raimund Schroeder <raimund.schroeder@de.ibm.com> 
11  *            Aron Zeh
12  *            Wolfgang Taphorn
13  *            Stefan Bader <stefan.bader@de.ibm.com> 
14  *            Heiko Carstens <heiko.carstens@de.ibm.com> 
15  *            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         if ((atomic_test_mask(ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED,
2250                               &erp_action->adapter->status)) &&
2251             (erp_action->adapter->adapter_features &
2252              FSF_FEATURE_HBAAPI_MANAGEMENT)) {
2253                 zfcp_erp_adapter_strategy_open_fsf_xport(erp_action);
2254                 atomic_set(&erp_action->adapter->erp_counter, 0);
2255                 return ZFCP_ERP_FAILED;
2256         }
2257
2258         retval = zfcp_erp_adapter_strategy_open_fsf_xconfig(erp_action);
2259         if (retval == ZFCP_ERP_FAILED)
2260                 return ZFCP_ERP_FAILED;
2261
2262         retval = zfcp_erp_adapter_strategy_open_fsf_xport(erp_action);
2263         if (retval == ZFCP_ERP_FAILED)
2264                 return ZFCP_ERP_FAILED;
2265
2266         return zfcp_erp_adapter_strategy_open_fsf_statusread(erp_action);
2267 }
2268
2269 /*
2270  * function:    
2271  *
2272  * purpose:     
2273  *
2274  * returns:
2275  */
2276 static int
2277 zfcp_erp_adapter_strategy_open_fsf_xconfig(struct zfcp_erp_action *erp_action)
2278 {
2279         int retval = ZFCP_ERP_SUCCEEDED;
2280         int retries;
2281         int sleep = ZFCP_EXCHANGE_CONFIG_DATA_FIRST_SLEEP;
2282         struct zfcp_adapter *adapter = erp_action->adapter;
2283
2284         atomic_clear_mask(ZFCP_STATUS_ADAPTER_XCONFIG_OK, &adapter->status);
2285
2286         for (retries = ZFCP_EXCHANGE_CONFIG_DATA_RETRIES; retries; retries--) {
2287                 atomic_clear_mask(ZFCP_STATUS_ADAPTER_HOST_CON_INIT,
2288                                   &adapter->status);
2289                 ZFCP_LOG_DEBUG("Doing exchange config data\n");
2290                 write_lock(&adapter->erp_lock);
2291                 zfcp_erp_action_to_running(erp_action);
2292                 write_unlock(&adapter->erp_lock);
2293                 zfcp_erp_timeout_init(erp_action);
2294                 if (zfcp_fsf_exchange_config_data(erp_action)) {
2295                         retval = ZFCP_ERP_FAILED;
2296                         debug_text_event(adapter->erp_dbf, 5, "a_fstx_xf");
2297                         ZFCP_LOG_INFO("error:  initiation of exchange of "
2298                                       "configuration data failed for "
2299                                       "adapter %s\n",
2300                                       zfcp_get_busid_by_adapter(adapter));
2301                         break;
2302                 }
2303                 debug_text_event(adapter->erp_dbf, 6, "a_fstx_xok");
2304                 ZFCP_LOG_DEBUG("Xchange underway\n");
2305
2306                 /*
2307                  * Why this works:
2308                  * Both the normal completion handler as well as the timeout
2309                  * handler will do an 'up' when the 'exchange config data'
2310                  * request completes or times out. Thus, the signal to go on
2311                  * won't be lost utilizing this semaphore.
2312                  * Furthermore, this 'adapter_reopen' action is
2313                  * guaranteed to be the only action being there (highest action
2314                  * which prevents other actions from being created).
2315                  * Resulting from that, the wake signal recognized here
2316                  * _must_ be the one belonging to the 'exchange config
2317                  * data' request.
2318                  */
2319                 down(&adapter->erp_ready_sem);
2320                 if (erp_action->status & ZFCP_STATUS_ERP_TIMEDOUT) {
2321                         ZFCP_LOG_INFO("error: exchange of configuration data "
2322                                       "for adapter %s timed out\n",
2323                                       zfcp_get_busid_by_adapter(adapter));
2324                         break;
2325                 }
2326
2327                 if (!atomic_test_mask(ZFCP_STATUS_ADAPTER_HOST_CON_INIT,
2328                                      &adapter->status))
2329                         break;
2330
2331                 ZFCP_LOG_DEBUG("host connection still initialising... "
2332                                "waiting and retrying...\n");
2333                 /* sleep a little bit before retry */
2334                 msleep(jiffies_to_msecs(sleep));
2335                 sleep *= 2;
2336         }
2337
2338         if (!atomic_test_mask(ZFCP_STATUS_ADAPTER_XCONFIG_OK,
2339                               &adapter->status)) {
2340                 ZFCP_LOG_INFO("error: exchange of configuration data for "
2341                               "adapter %s failed\n",
2342                               zfcp_get_busid_by_adapter(adapter));
2343                 retval = ZFCP_ERP_FAILED;
2344         }
2345
2346         return retval;
2347 }
2348
2349 static int
2350 zfcp_erp_adapter_strategy_open_fsf_xport(struct zfcp_erp_action *erp_action)
2351 {
2352         int ret;
2353         int retries;
2354         int sleep;
2355         struct zfcp_adapter *adapter = erp_action->adapter;
2356
2357         atomic_clear_mask(ZFCP_STATUS_ADAPTER_XPORT_OK, &adapter->status);
2358
2359         retries = 0;
2360         do {
2361                 write_lock(&adapter->erp_lock);
2362                 zfcp_erp_action_to_running(erp_action);
2363                 write_unlock(&adapter->erp_lock);
2364                 zfcp_erp_timeout_init(erp_action);
2365                 ret = zfcp_fsf_exchange_port_data(erp_action, adapter, NULL);
2366                 if (ret == -EOPNOTSUPP) {
2367                         debug_text_event(adapter->erp_dbf, 3, "a_xport_notsupp");
2368                         return ZFCP_ERP_SUCCEEDED;
2369                 } else if (ret) {
2370                         debug_text_event(adapter->erp_dbf, 3, "a_xport_failed");
2371                         return ZFCP_ERP_FAILED;
2372                 }
2373                 debug_text_event(adapter->erp_dbf, 6, "a_xport_ok");
2374
2375                 down(&adapter->erp_ready_sem);
2376                 if (erp_action->status & ZFCP_STATUS_ERP_TIMEDOUT) {
2377                         ZFCP_LOG_INFO("error: exchange of port data "
2378                                       "for adapter %s timed out\n",
2379                                       zfcp_get_busid_by_adapter(adapter));
2380                         break;
2381                 }
2382                 if (!atomic_test_mask(ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED,
2383                                       &adapter->status))
2384                         break;
2385
2386                 if (retries < ZFCP_EXCHANGE_PORT_DATA_SHORT_RETRIES) {
2387                         sleep = ZFCP_EXCHANGE_PORT_DATA_SHORT_SLEEP;
2388                         retries++;
2389                 } else
2390                         sleep = ZFCP_EXCHANGE_PORT_DATA_LONG_SLEEP;
2391                 schedule_timeout(sleep);
2392         } while (1);
2393
2394         return ZFCP_ERP_SUCCEEDED;
2395 }
2396
2397 /*
2398  * function:    
2399  *
2400  * purpose:     
2401  *
2402  * returns:
2403  */
2404 static int
2405 zfcp_erp_adapter_strategy_open_fsf_statusread(struct zfcp_erp_action
2406                                               *erp_action)
2407 {
2408         int retval = ZFCP_ERP_SUCCEEDED;
2409         int temp_ret;
2410         struct zfcp_adapter *adapter = erp_action->adapter;
2411         int i;
2412
2413         adapter->status_read_failed = 0;
2414         for (i = 0; i < ZFCP_STATUS_READS_RECOM; i++) {
2415                 temp_ret = zfcp_fsf_status_read(adapter, ZFCP_WAIT_FOR_SBAL);
2416                 if (temp_ret < 0) {
2417                         ZFCP_LOG_INFO("error: set-up of unsolicited status "
2418                                       "notification failed on adapter %s\n",
2419                                       zfcp_get_busid_by_adapter(adapter));
2420                         retval = ZFCP_ERP_FAILED;
2421                         i--;
2422                         break;
2423                 }
2424         }
2425
2426         return retval;
2427 }
2428
2429 /*
2430  * function:    zfcp_fsf_cleanup
2431  *
2432  * purpose:     cleanup FSF operation for specified adapter
2433  *
2434  * returns:     0 - FSF operation successfully cleaned up
2435  *              !0 - failed to cleanup FSF operation for this adapter
2436  */
2437 static int
2438 zfcp_erp_adapter_strategy_close_fsf(struct zfcp_erp_action *erp_action)
2439 {
2440         int retval = ZFCP_ERP_SUCCEEDED;
2441         struct zfcp_adapter *adapter = erp_action->adapter;
2442
2443         /*
2444          * wake waiting initiators of requests,
2445          * return SCSI commands (with error status),
2446          * clean up all requests (synchronously)
2447          */
2448         zfcp_fsf_req_dismiss_all(adapter);
2449         /* reset FSF request sequence number */
2450         adapter->fsf_req_seq_no = 0;
2451         /* all ports and units are closed */
2452         zfcp_erp_modify_adapter_status(adapter,
2453                                        ZFCP_STATUS_COMMON_OPEN, ZFCP_CLEAR);
2454
2455         return retval;
2456 }
2457
2458 /*
2459  * function:    
2460  *
2461  * purpose:     this routine executes the 'Reopen Physical Port' action
2462  *
2463  * returns:     ZFCP_ERP_CONTINUES      - action continues (asynchronously)
2464  *              ZFCP_ERP_SUCCEEDED      - action finished successfully
2465  *              ZFCP_ERP_FAILED         - action finished unsuccessfully
2466  */
2467 static int
2468 zfcp_erp_port_forced_strategy(struct zfcp_erp_action *erp_action)
2469 {
2470         int retval = ZFCP_ERP_FAILED;
2471         struct zfcp_port *port = erp_action->port;
2472         struct zfcp_adapter *adapter = erp_action->adapter;
2473
2474         switch (erp_action->step) {
2475
2476                 /*
2477                  * FIXME:
2478                  * the ULP spec. begs for waiting for oustanding commands
2479                  */
2480         case ZFCP_ERP_STEP_UNINITIALIZED:
2481                 zfcp_erp_port_strategy_clearstati(port);
2482                 /*
2483                  * it would be sufficient to test only the normal open flag
2484                  * since the phys. open flag cannot be set if the normal
2485                  * open flag is unset - however, this is for readabilty ...
2486                  */
2487                 if (atomic_test_mask((ZFCP_STATUS_PORT_PHYS_OPEN |
2488                                       ZFCP_STATUS_COMMON_OPEN),
2489                                      &port->status)) {
2490                         ZFCP_LOG_DEBUG("port 0x%016Lx is open -> trying "
2491                                        "close physical\n", port->wwpn);
2492                         retval =
2493                             zfcp_erp_port_forced_strategy_close(erp_action);
2494                 } else
2495                         retval = ZFCP_ERP_FAILED;
2496                 break;
2497
2498         case ZFCP_ERP_STEP_PHYS_PORT_CLOSING:
2499                 if (atomic_test_mask(ZFCP_STATUS_PORT_PHYS_OPEN,
2500                                      &port->status)) {
2501                         ZFCP_LOG_DEBUG("close physical failed for port "
2502                                        "0x%016Lx\n", port->wwpn);
2503                         retval = ZFCP_ERP_FAILED;
2504                 } else
2505                         retval = ZFCP_ERP_SUCCEEDED;
2506                 break;
2507         }
2508
2509         debug_text_event(adapter->erp_dbf, 3, "p_pfst/ret");
2510         debug_event(adapter->erp_dbf, 3, &port->wwpn, sizeof (wwn_t));
2511         debug_event(adapter->erp_dbf, 3, &erp_action->action, sizeof (int));
2512         debug_event(adapter->erp_dbf, 3, &retval, sizeof (int));
2513
2514         return retval;
2515 }
2516
2517 /*
2518  * function:    
2519  *
2520  * purpose:     this routine executes the 'Reopen Port' action
2521  *
2522  * returns:     ZFCP_ERP_CONTINUES      - action continues (asynchronously)
2523  *              ZFCP_ERP_SUCCEEDED      - action finished successfully
2524  *              ZFCP_ERP_FAILED         - action finished unsuccessfully
2525  */
2526 static int
2527 zfcp_erp_port_strategy(struct zfcp_erp_action *erp_action)
2528 {
2529         int retval = ZFCP_ERP_FAILED;
2530         struct zfcp_port *port = erp_action->port;
2531         struct zfcp_adapter *adapter = erp_action->adapter;
2532
2533         switch (erp_action->step) {
2534
2535                 /*
2536                  * FIXME:
2537                  * the ULP spec. begs for waiting for oustanding commands
2538                  */
2539         case ZFCP_ERP_STEP_UNINITIALIZED:
2540                 zfcp_erp_port_strategy_clearstati(port);
2541                 if (atomic_test_mask(ZFCP_STATUS_COMMON_OPEN, &port->status)) {
2542                         ZFCP_LOG_DEBUG("port 0x%016Lx is open -> trying "
2543                                        "close\n", port->wwpn);
2544                         retval = zfcp_erp_port_strategy_close(erp_action);
2545                         goto out;
2546                 }               /* else it's already closed, open it */
2547                 break;
2548
2549         case ZFCP_ERP_STEP_PORT_CLOSING:
2550                 if (atomic_test_mask(ZFCP_STATUS_COMMON_OPEN, &port->status)) {
2551                         ZFCP_LOG_DEBUG("close failed for port 0x%016Lx\n",
2552                                        port->wwpn);
2553                         retval = ZFCP_ERP_FAILED;
2554                         goto out;
2555                 }               /* else it's closed now, open it */
2556                 break;
2557         }
2558         if (erp_action->status & ZFCP_STATUS_ERP_CLOSE_ONLY)
2559                 retval = ZFCP_ERP_EXIT;
2560         else
2561                 retval = zfcp_erp_port_strategy_open(erp_action);
2562
2563  out:
2564         debug_text_event(adapter->erp_dbf, 3, "p_pst/ret");
2565         debug_event(adapter->erp_dbf, 3, &port->wwpn, sizeof (wwn_t));
2566         debug_event(adapter->erp_dbf, 3, &erp_action->action, sizeof (int));
2567         debug_event(adapter->erp_dbf, 3, &retval, sizeof (int));
2568
2569         return retval;
2570 }
2571
2572 /*
2573  * function:    
2574  *
2575  * purpose:     
2576  *
2577  * returns:
2578  */
2579 static int
2580 zfcp_erp_port_strategy_open(struct zfcp_erp_action *erp_action)
2581 {
2582         int retval;
2583
2584         if (atomic_test_mask(ZFCP_STATUS_PORT_WKA,
2585                              &erp_action->port->status))
2586                 retval = zfcp_erp_port_strategy_open_nameserver(erp_action);
2587         else
2588                 retval = zfcp_erp_port_strategy_open_common(erp_action);
2589
2590         return retval;
2591 }
2592
2593 /*
2594  * function:    
2595  *
2596  * purpose:     
2597  *
2598  * returns:
2599  *
2600  * FIXME(design):       currently only prepared for fabric (nameserver!)
2601  */
2602 static int
2603 zfcp_erp_port_strategy_open_common(struct zfcp_erp_action *erp_action)
2604 {
2605         int retval = 0;
2606         struct zfcp_adapter *adapter = erp_action->adapter;
2607         struct zfcp_port *port = erp_action->port;
2608
2609         switch (erp_action->step) {
2610
2611         case ZFCP_ERP_STEP_UNINITIALIZED:
2612         case ZFCP_ERP_STEP_PHYS_PORT_CLOSING:
2613         case ZFCP_ERP_STEP_PORT_CLOSING:
2614                 if (fc_host_port_type(adapter->scsi_host) == FC_PORTTYPE_PTP) {
2615                         if (port->wwpn != adapter->peer_wwpn) {
2616                                 ZFCP_LOG_NORMAL("Failed to open port 0x%016Lx "
2617                                                 "on adapter %s.\nPeer WWPN "
2618                                                 "0x%016Lx does not match\n",
2619                                                 port->wwpn,
2620                                                 zfcp_get_busid_by_adapter(adapter),
2621                                                 adapter->peer_wwpn);
2622                                 zfcp_erp_port_failed(port);
2623                                 retval = ZFCP_ERP_FAILED;
2624                                 break;
2625                         }
2626                         port->d_id = adapter->peer_d_id;
2627                         atomic_set_mask(ZFCP_STATUS_PORT_DID_DID, &port->status);
2628                         retval = zfcp_erp_port_strategy_open_port(erp_action);
2629                         break;
2630                 }
2631                 if (!(adapter->nameserver_port)) {
2632                         retval = zfcp_nameserver_enqueue(adapter);
2633                         if (retval != 0) {
2634                                 ZFCP_LOG_NORMAL("error: nameserver port "
2635                                                 "unavailable for adapter %s\n",
2636                                                 zfcp_get_busid_by_adapter(adapter));
2637                                 retval = ZFCP_ERP_FAILED;
2638                                 break;
2639                         }
2640                 }
2641                 if (!atomic_test_mask(ZFCP_STATUS_COMMON_UNBLOCKED,
2642                                       &adapter->nameserver_port->status)) {
2643                         ZFCP_LOG_DEBUG("nameserver port is not open -> open "
2644                                        "nameserver port\n");
2645                         /* nameserver port may live again */
2646                         atomic_set_mask(ZFCP_STATUS_COMMON_RUNNING,
2647                                         &adapter->nameserver_port->status);
2648                         if (zfcp_erp_port_reopen(adapter->nameserver_port, 0)
2649                             >= 0) {
2650                                 erp_action->step =
2651                                         ZFCP_ERP_STEP_NAMESERVER_OPEN;
2652                                 retval = ZFCP_ERP_CONTINUES;
2653                         } else
2654                                 retval = ZFCP_ERP_FAILED;
2655                         break;
2656                 }
2657                 /* else nameserver port is already open, fall through */
2658         case ZFCP_ERP_STEP_NAMESERVER_OPEN:
2659                 if (!atomic_test_mask(ZFCP_STATUS_COMMON_OPEN,
2660                                       &adapter->nameserver_port->status)) {
2661                         ZFCP_LOG_DEBUG("open failed for nameserver port\n");
2662                         retval = ZFCP_ERP_FAILED;
2663                 } else {
2664                         ZFCP_LOG_DEBUG("nameserver port is open -> "
2665                                        "nameserver look-up for port 0x%016Lx\n",
2666                                        port->wwpn);
2667                         retval = zfcp_erp_port_strategy_open_common_lookup
2668                                 (erp_action);
2669                 }
2670                 break;
2671
2672         case ZFCP_ERP_STEP_NAMESERVER_LOOKUP:
2673                 if (!atomic_test_mask(ZFCP_STATUS_PORT_DID_DID, &port->status)) {
2674                         if (atomic_test_mask
2675                             (ZFCP_STATUS_PORT_INVALID_WWPN, &port->status)) {
2676                                 ZFCP_LOG_DEBUG("nameserver look-up failed "
2677                                                "for port 0x%016Lx "
2678                                                "(misconfigured WWPN?)\n",
2679                                                port->wwpn);
2680                                 zfcp_erp_port_failed(port);
2681                                 retval = ZFCP_ERP_EXIT;
2682                         } else {
2683                                 ZFCP_LOG_DEBUG("nameserver look-up failed for "
2684                                                "port 0x%016Lx\n", port->wwpn);
2685                                 retval = ZFCP_ERP_FAILED;
2686                         }
2687                 } else {
2688                         ZFCP_LOG_DEBUG("port 0x%016Lx has d_id=0x%08x -> "
2689                                        "trying open\n", port->wwpn, port->d_id);
2690                         retval = zfcp_erp_port_strategy_open_port(erp_action);
2691                 }
2692                 break;
2693
2694         case ZFCP_ERP_STEP_PORT_OPENING:
2695                 /* D_ID might have changed during open */
2696                 if (atomic_test_mask((ZFCP_STATUS_COMMON_OPEN |
2697                                       ZFCP_STATUS_PORT_DID_DID),
2698                                      &port->status)) {
2699                         ZFCP_LOG_DEBUG("port 0x%016Lx is open\n", port->wwpn);
2700                         retval = ZFCP_ERP_SUCCEEDED;
2701                 } else {
2702                         ZFCP_LOG_DEBUG("open failed for port 0x%016Lx\n",
2703                                        port->wwpn);
2704                         retval = ZFCP_ERP_FAILED;
2705                 }
2706                 break;
2707
2708         default:
2709                 ZFCP_LOG_NORMAL("bug: unknown erp step 0x%08x\n",
2710                                 erp_action->step);
2711                 retval = ZFCP_ERP_FAILED;
2712         }
2713
2714         return retval;
2715 }
2716
2717 /*
2718  * function:    
2719  *
2720  * purpose:     
2721  *
2722  * returns:
2723  */
2724 static int
2725 zfcp_erp_port_strategy_open_nameserver(struct zfcp_erp_action *erp_action)
2726 {
2727         int retval;
2728         struct zfcp_port *port = erp_action->port;
2729
2730         switch (erp_action->step) {
2731
2732         case ZFCP_ERP_STEP_UNINITIALIZED:
2733         case ZFCP_ERP_STEP_PHYS_PORT_CLOSING:
2734         case ZFCP_ERP_STEP_PORT_CLOSING:
2735                 ZFCP_LOG_DEBUG("port 0x%016Lx has d_id=0x%08x -> trying open\n",
2736                                port->wwpn, port->d_id);
2737                 retval = zfcp_erp_port_strategy_open_port(erp_action);
2738                 break;
2739
2740         case ZFCP_ERP_STEP_PORT_OPENING:
2741                 if (atomic_test_mask(ZFCP_STATUS_COMMON_OPEN, &port->status)) {
2742                         ZFCP_LOG_DEBUG("WKA port is open\n");
2743                         retval = ZFCP_ERP_SUCCEEDED;
2744                 } else {
2745                         ZFCP_LOG_DEBUG("open failed for WKA port\n");
2746                         retval = ZFCP_ERP_FAILED;
2747                 }
2748                 /* this is needed anyway (dont care for retval of wakeup) */
2749                 ZFCP_LOG_DEBUG("continue other open port operations\n");
2750                 zfcp_erp_port_strategy_open_nameserver_wakeup(erp_action);
2751                 break;
2752
2753         default:
2754                 ZFCP_LOG_NORMAL("bug: unknown erp step 0x%08x\n",
2755                                 erp_action->step);
2756                 retval = ZFCP_ERP_FAILED;
2757         }
2758
2759         return retval;
2760 }
2761
2762 /*
2763  * function:    
2764  *
2765  * purpose:     makes the erp thread continue with reopen (physical) port
2766  *              actions which have been paused until the name server port
2767  *              is opened (or failed)
2768  *
2769  * returns:     0       (a kind of void retval, its not used)
2770  */
2771 static int
2772 zfcp_erp_port_strategy_open_nameserver_wakeup(struct zfcp_erp_action
2773                                               *ns_erp_action)
2774 {
2775         int retval = 0;
2776         unsigned long flags;
2777         struct zfcp_adapter *adapter = ns_erp_action->adapter;
2778         struct zfcp_erp_action *erp_action, *tmp;
2779
2780         read_lock_irqsave(&adapter->erp_lock, flags);
2781         list_for_each_entry_safe(erp_action, tmp, &adapter->erp_running_head,
2782                                  list) {
2783                 debug_text_event(adapter->erp_dbf, 4, "p_pstnsw_n");
2784                 debug_event(adapter->erp_dbf, 4, &erp_action->port->wwpn,
2785                             sizeof (wwn_t));
2786                 if (erp_action->step == ZFCP_ERP_STEP_NAMESERVER_OPEN) {
2787                         debug_text_event(adapter->erp_dbf, 3, "p_pstnsw_w");
2788                         debug_event(adapter->erp_dbf, 3,
2789                                     &erp_action->port->wwpn, sizeof (wwn_t));
2790                         if (atomic_test_mask(
2791                                     ZFCP_STATUS_COMMON_ERP_FAILED,
2792                                     &adapter->nameserver_port->status))
2793                                 zfcp_erp_port_failed(erp_action->port);
2794                         zfcp_erp_action_ready(erp_action);
2795                 }
2796         }
2797         read_unlock_irqrestore(&adapter->erp_lock, flags);
2798
2799         return retval;
2800 }
2801
2802 /*
2803  * function:    
2804  *
2805  * purpose:     
2806  *
2807  * returns:     ZFCP_ERP_CONTINUES      - action continues (asynchronously)
2808  *              ZFCP_ERP_FAILED         - action finished unsuccessfully
2809  */
2810 static int
2811 zfcp_erp_port_forced_strategy_close(struct zfcp_erp_action *erp_action)
2812 {
2813         int retval;
2814         struct zfcp_adapter *adapter = erp_action->adapter;
2815         struct zfcp_port *port = erp_action->port;
2816
2817         zfcp_erp_timeout_init(erp_action);
2818         retval = zfcp_fsf_close_physical_port(erp_action);
2819         if (retval == -ENOMEM) {
2820                 debug_text_event(adapter->erp_dbf, 5, "o_pfstc_nomem");
2821                 debug_event(adapter->erp_dbf, 5, &port->wwpn, sizeof (wwn_t));
2822                 retval = ZFCP_ERP_NOMEM;
2823                 goto out;
2824         }
2825         erp_action->step = ZFCP_ERP_STEP_PHYS_PORT_CLOSING;
2826         if (retval != 0) {
2827                 debug_text_event(adapter->erp_dbf, 5, "o_pfstc_cpf");
2828                 debug_event(adapter->erp_dbf, 5, &port->wwpn, sizeof (wwn_t));
2829                 /* could not send 'open', fail */
2830                 retval = ZFCP_ERP_FAILED;
2831                 goto out;
2832         }
2833         debug_text_event(adapter->erp_dbf, 6, "o_pfstc_cpok");
2834         debug_event(adapter->erp_dbf, 6, &port->wwpn, sizeof (wwn_t));
2835         retval = ZFCP_ERP_CONTINUES;
2836  out:
2837         return retval;
2838 }
2839
2840 /*
2841  * function:    
2842  *
2843  * purpose:     
2844  *
2845  * returns:
2846  */
2847 static int
2848 zfcp_erp_port_strategy_clearstati(struct zfcp_port *port)
2849 {
2850         int retval = 0;
2851         struct zfcp_adapter *adapter = port->adapter;
2852
2853         debug_text_event(adapter->erp_dbf, 5, "p_pstclst");
2854         debug_event(adapter->erp_dbf, 5, &port->wwpn, sizeof (wwn_t));
2855
2856         atomic_clear_mask(ZFCP_STATUS_COMMON_OPENING |
2857                           ZFCP_STATUS_COMMON_CLOSING |
2858                           ZFCP_STATUS_COMMON_ACCESS_DENIED |
2859                           ZFCP_STATUS_PORT_DID_DID |
2860                           ZFCP_STATUS_PORT_PHYS_CLOSING |
2861                           ZFCP_STATUS_PORT_INVALID_WWPN,
2862                           &port->status);
2863         return retval;
2864 }
2865
2866 /*
2867  * function:    
2868  *
2869  * purpose:     
2870  *
2871  * returns:     ZFCP_ERP_CONTINUES      - action continues (asynchronously)
2872  *              ZFCP_ERP_FAILED         - action finished unsuccessfully
2873  */
2874 static int
2875 zfcp_erp_port_strategy_close(struct zfcp_erp_action *erp_action)
2876 {
2877         int retval;
2878         struct zfcp_adapter *adapter = erp_action->adapter;
2879         struct zfcp_port *port = erp_action->port;
2880
2881         zfcp_erp_timeout_init(erp_action);
2882         retval = zfcp_fsf_close_port(erp_action);
2883         if (retval == -ENOMEM) {
2884                 debug_text_event(adapter->erp_dbf, 5, "p_pstc_nomem");
2885                 debug_event(adapter->erp_dbf, 5, &port->wwpn, sizeof (wwn_t));
2886                 retval = ZFCP_ERP_NOMEM;
2887                 goto out;
2888         }
2889         erp_action->step = ZFCP_ERP_STEP_PORT_CLOSING;
2890         if (retval != 0) {
2891                 debug_text_event(adapter->erp_dbf, 5, "p_pstc_cpf");
2892                 debug_event(adapter->erp_dbf, 5, &port->wwpn, sizeof (wwn_t));
2893                 /* could not send 'close', fail */
2894                 retval = ZFCP_ERP_FAILED;
2895                 goto out;
2896         }
2897         debug_text_event(adapter->erp_dbf, 6, "p_pstc_cpok");
2898         debug_event(adapter->erp_dbf, 6, &port->wwpn, sizeof (wwn_t));
2899         retval = ZFCP_ERP_CONTINUES;
2900  out:
2901         return retval;
2902 }
2903
2904 /*
2905  * function:    
2906  *
2907  * purpose:     
2908  *
2909  * returns:     ZFCP_ERP_CONTINUES      - action continues (asynchronously)
2910  *              ZFCP_ERP_FAILED         - action finished unsuccessfully
2911  */
2912 static int
2913 zfcp_erp_port_strategy_open_port(struct zfcp_erp_action *erp_action)
2914 {
2915         int retval;
2916         struct zfcp_adapter *adapter = erp_action->adapter;
2917         struct zfcp_port *port = erp_action->port;
2918
2919         zfcp_erp_timeout_init(erp_action);
2920         retval = zfcp_fsf_open_port(erp_action);
2921         if (retval == -ENOMEM) {
2922                 debug_text_event(adapter->erp_dbf, 5, "p_psto_nomem");
2923                 debug_event(adapter->erp_dbf, 5, &port->wwpn, sizeof (wwn_t));
2924                 retval = ZFCP_ERP_NOMEM;
2925                 goto out;
2926         }
2927         erp_action->step = ZFCP_ERP_STEP_PORT_OPENING;
2928         if (retval != 0) {
2929                 debug_text_event(adapter->erp_dbf, 5, "p_psto_opf");
2930                 debug_event(adapter->erp_dbf, 5, &port->wwpn, sizeof (wwn_t));
2931                 /* could not send 'open', fail */
2932                 retval = ZFCP_ERP_FAILED;
2933                 goto out;
2934         }
2935         debug_text_event(adapter->erp_dbf, 6, "p_psto_opok");
2936         debug_event(adapter->erp_dbf, 6, &port->wwpn, sizeof (wwn_t));
2937         retval = ZFCP_ERP_CONTINUES;
2938  out:
2939         return retval;
2940 }
2941
2942 /*
2943  * function:    
2944  *
2945  * purpose:     
2946  *
2947  * returns:     ZFCP_ERP_CONTINUES      - action continues (asynchronously)
2948  *              ZFCP_ERP_FAILED         - action finished unsuccessfully
2949  */
2950 static int
2951 zfcp_erp_port_strategy_open_common_lookup(struct zfcp_erp_action *erp_action)
2952 {
2953         int retval;
2954         struct zfcp_adapter *adapter = erp_action->adapter;
2955         struct zfcp_port *port = erp_action->port;
2956
2957         zfcp_erp_timeout_init(erp_action);
2958         retval = zfcp_ns_gid_pn_request(erp_action);
2959         if (retval == -ENOMEM) {
2960                 debug_text_event(adapter->erp_dbf, 5, "p_pstn_nomem");
2961                 debug_event(adapter->erp_dbf, 5, &port->wwpn, sizeof (wwn_t));
2962                 retval = ZFCP_ERP_NOMEM;
2963                 goto out;
2964         }
2965         erp_action->step = ZFCP_ERP_STEP_NAMESERVER_LOOKUP;
2966         if (retval != 0) {
2967                 debug_text_event(adapter->erp_dbf, 5, "p_pstn_ref");
2968                 debug_event(adapter->erp_dbf, 5, &port->wwpn, sizeof (wwn_t));
2969                 /* could not send nameserver request, fail */
2970                 retval = ZFCP_ERP_FAILED;
2971                 goto out;
2972         }
2973         debug_text_event(adapter->erp_dbf, 6, "p_pstn_reok");
2974         debug_event(adapter->erp_dbf, 6, &port->wwpn, sizeof (wwn_t));
2975         retval = ZFCP_ERP_CONTINUES;
2976  out:
2977         return retval;
2978 }
2979
2980 /*
2981  * function:    
2982  *
2983  * purpose:     this routine executes the 'Reopen Unit' action
2984  *              currently no retries
2985  *
2986  * returns:     ZFCP_ERP_CONTINUES      - action continues (asynchronously)
2987  *              ZFCP_ERP_SUCCEEDED      - action finished successfully
2988  *              ZFCP_ERP_FAILED         - action finished unsuccessfully
2989  */
2990 static int
2991 zfcp_erp_unit_strategy(struct zfcp_erp_action *erp_action)
2992 {
2993         int retval = ZFCP_ERP_FAILED;
2994         struct zfcp_unit *unit = erp_action->unit;
2995         struct zfcp_adapter *adapter = erp_action->adapter;
2996
2997         switch (erp_action->step) {
2998
2999                 /*
3000                  * FIXME:
3001                  * the ULP spec. begs for waiting for oustanding commands
3002                  */
3003         case ZFCP_ERP_STEP_UNINITIALIZED:
3004                 zfcp_erp_unit_strategy_clearstati(unit);
3005                 if (atomic_test_mask(ZFCP_STATUS_COMMON_OPEN, &unit->status)) {
3006                         ZFCP_LOG_DEBUG("unit 0x%016Lx is open -> "
3007                                        "trying close\n", unit->fcp_lun);
3008                         retval = zfcp_erp_unit_strategy_close(erp_action);
3009                         break;
3010                 }
3011                 /* else it's already closed, fall through */
3012         case ZFCP_ERP_STEP_UNIT_CLOSING:
3013                 if (atomic_test_mask(ZFCP_STATUS_COMMON_OPEN, &unit->status)) {
3014                         ZFCP_LOG_DEBUG("close failed for unit 0x%016Lx\n",
3015                                        unit->fcp_lun);
3016                         retval = ZFCP_ERP_FAILED;
3017                 } else {
3018                         if (erp_action->status & ZFCP_STATUS_ERP_CLOSE_ONLY)
3019                                 retval = ZFCP_ERP_EXIT;
3020                         else {
3021                                 ZFCP_LOG_DEBUG("unit 0x%016Lx is not open -> "
3022                                                "trying open\n", unit->fcp_lun);
3023                                 retval =
3024                                     zfcp_erp_unit_strategy_open(erp_action);
3025                         }
3026                 }
3027                 break;
3028
3029         case ZFCP_ERP_STEP_UNIT_OPENING:
3030                 if (atomic_test_mask(ZFCP_STATUS_COMMON_OPEN, &unit->status)) {
3031                         ZFCP_LOG_DEBUG("unit 0x%016Lx is open\n",
3032                                        unit->fcp_lun);
3033                         retval = ZFCP_ERP_SUCCEEDED;
3034                 } else {
3035                         ZFCP_LOG_DEBUG("open failed for unit 0x%016Lx\n",
3036                                        unit->fcp_lun);
3037                         retval = ZFCP_ERP_FAILED;
3038                 }
3039                 break;
3040         }
3041
3042         debug_text_event(adapter->erp_dbf, 3, "u_ust/ret");
3043         debug_event(adapter->erp_dbf, 3, &unit->fcp_lun, sizeof (fcp_lun_t));
3044         debug_event(adapter->erp_dbf, 3, &erp_action->action, sizeof (int));
3045         debug_event(adapter->erp_dbf, 3, &retval, sizeof (int));
3046         return retval;
3047 }
3048
3049 /*
3050  * function:
3051  *
3052  * purpose:
3053  *
3054  * returns:
3055  */
3056 static int
3057 zfcp_erp_unit_strategy_clearstati(struct zfcp_unit *unit)
3058 {
3059         int retval = 0;
3060         struct zfcp_adapter *adapter = unit->port->adapter;
3061
3062         debug_text_event(adapter->erp_dbf, 5, "u_ustclst");
3063         debug_event(adapter->erp_dbf, 5, &unit->fcp_lun, sizeof (fcp_lun_t));
3064
3065         atomic_clear_mask(ZFCP_STATUS_COMMON_OPENING |
3066                           ZFCP_STATUS_COMMON_CLOSING |
3067                           ZFCP_STATUS_COMMON_ACCESS_DENIED |
3068                           ZFCP_STATUS_UNIT_SHARED |
3069                           ZFCP_STATUS_UNIT_READONLY,
3070                           &unit->status);
3071
3072         return retval;
3073 }
3074
3075 /*
3076  * function:    
3077  *
3078  * purpose:     
3079  *
3080  * returns:     ZFCP_ERP_CONTINUES      - action continues (asynchronously)
3081  *              ZFCP_ERP_FAILED         - action finished unsuccessfully
3082  */
3083 static int
3084 zfcp_erp_unit_strategy_close(struct zfcp_erp_action *erp_action)
3085 {
3086         int retval;
3087         struct zfcp_adapter *adapter = erp_action->adapter;
3088         struct zfcp_unit *unit = erp_action->unit;
3089
3090         zfcp_erp_timeout_init(erp_action);
3091         retval = zfcp_fsf_close_unit(erp_action);
3092         if (retval == -ENOMEM) {
3093                 debug_text_event(adapter->erp_dbf, 5, "u_ustc_nomem");
3094                 debug_event(adapter->erp_dbf, 5, &unit->fcp_lun,
3095                             sizeof (fcp_lun_t));
3096                 retval = ZFCP_ERP_NOMEM;
3097                 goto out;
3098         }
3099         erp_action->step = ZFCP_ERP_STEP_UNIT_CLOSING;
3100         if (retval != 0) {
3101                 debug_text_event(adapter->erp_dbf, 5, "u_ustc_cuf");
3102                 debug_event(adapter->erp_dbf, 5, &unit->fcp_lun,
3103                             sizeof (fcp_lun_t));
3104                 /* could not send 'close', fail */
3105                 retval = ZFCP_ERP_FAILED;
3106                 goto out;
3107         }
3108         debug_text_event(adapter->erp_dbf, 6, "u_ustc_cuok");
3109         debug_event(adapter->erp_dbf, 6, &unit->fcp_lun, sizeof (fcp_lun_t));
3110         retval = ZFCP_ERP_CONTINUES;
3111
3112  out:
3113         return retval;
3114 }
3115
3116 /*
3117  * function:    
3118  *
3119  * purpose:     
3120  *
3121  * returns:     ZFCP_ERP_CONTINUES      - action continues (asynchronously)
3122  *              ZFCP_ERP_FAILED         - action finished unsuccessfully
3123  */
3124 static int
3125 zfcp_erp_unit_strategy_open(struct zfcp_erp_action *erp_action)
3126 {
3127         int retval;
3128         struct zfcp_adapter *adapter = erp_action->adapter;
3129         struct zfcp_unit *unit = erp_action->unit;
3130
3131         zfcp_erp_timeout_init(erp_action);
3132         retval = zfcp_fsf_open_unit(erp_action);
3133         if (retval == -ENOMEM) {
3134                 debug_text_event(adapter->erp_dbf, 5, "u_usto_nomem");
3135                 debug_event(adapter->erp_dbf, 5, &unit->fcp_lun,
3136                             sizeof (fcp_lun_t));
3137                 retval = ZFCP_ERP_NOMEM;
3138                 goto out;
3139         }
3140         erp_action->step = ZFCP_ERP_STEP_UNIT_OPENING;
3141         if (retval != 0) {
3142                 debug_text_event(adapter->erp_dbf, 5, "u_usto_ouf");
3143                 debug_event(adapter->erp_dbf, 5, &unit->fcp_lun,
3144                             sizeof (fcp_lun_t));
3145                 /* could not send 'open', fail */
3146                 retval = ZFCP_ERP_FAILED;
3147                 goto out;
3148         }
3149         debug_text_event(adapter->erp_dbf, 6, "u_usto_ouok");
3150         debug_event(adapter->erp_dbf, 6, &unit->fcp_lun, sizeof (fcp_lun_t));
3151         retval = ZFCP_ERP_CONTINUES;
3152  out:
3153         return retval;
3154 }
3155
3156 /*
3157  * function:    
3158  *
3159  * purpose:     
3160  *
3161  * returns:
3162  */
3163 static inline void
3164 zfcp_erp_timeout_init(struct zfcp_erp_action *erp_action)
3165 {
3166         init_timer(&erp_action->timer);
3167         erp_action->timer.function = zfcp_erp_timeout_handler;
3168         erp_action->timer.data = (unsigned long) erp_action;
3169         /* jiffies will be added in zfcp_fsf_req_send */
3170         erp_action->timer.expires = ZFCP_ERP_FSFREQ_TIMEOUT;
3171 }
3172
3173 /*
3174  * function:    
3175  *
3176  * purpose:     enqueue the specified error recovery action, if needed
3177  *
3178  * returns:
3179  */
3180 static int
3181 zfcp_erp_action_enqueue(int action,
3182                         struct zfcp_adapter *adapter,
3183                         struct zfcp_port *port, struct zfcp_unit *unit)
3184 {
3185         int retval = 1;
3186         struct zfcp_erp_action *erp_action = NULL;
3187         int stronger_action = 0;
3188         u32 status = 0;
3189
3190         /*
3191          * We need some rules here which check whether we really need
3192          * this action or whether we should just drop it.
3193          * E.g. if there is a unfinished 'Reopen Port' request then we drop a
3194          * 'Reopen Unit' request for an associated unit since we can't
3195          * satisfy this request now. A 'Reopen Port' action will trigger
3196          * 'Reopen Unit' actions when it completes.
3197          * Thus, there are only actions in the queue which can immediately be
3198          * executed. This makes the processing of the action queue more
3199          * efficient.
3200          */
3201
3202         if (!atomic_test_mask(ZFCP_STATUS_ADAPTER_ERP_THREAD_UP,
3203                               &adapter->status))
3204                 return -EIO;
3205
3206         debug_event(adapter->erp_dbf, 4, &action, sizeof (int));
3207         /* check whether we really need this */
3208         switch (action) {
3209         case ZFCP_ERP_ACTION_REOPEN_UNIT:
3210                 if (atomic_test_mask
3211                     (ZFCP_STATUS_COMMON_ERP_INUSE, &unit->status)) {
3212                         debug_text_event(adapter->erp_dbf, 4, "u_actenq_drp");
3213                         debug_event(adapter->erp_dbf, 4, &port->wwpn,
3214                                     sizeof (wwn_t));
3215                         debug_event(adapter->erp_dbf, 4, &unit->fcp_lun,
3216                                     sizeof (fcp_lun_t));
3217                         goto out;
3218                 }
3219                 if (!atomic_test_mask
3220                     (ZFCP_STATUS_COMMON_RUNNING, &port->status) ||
3221                     atomic_test_mask
3222                     (ZFCP_STATUS_COMMON_ERP_FAILED, &port->status)) {
3223                         goto out;
3224                 }
3225                 if (!atomic_test_mask
3226                     (ZFCP_STATUS_COMMON_UNBLOCKED, &port->status)) {
3227                         stronger_action = ZFCP_ERP_ACTION_REOPEN_PORT;
3228                         unit = NULL;
3229                 }
3230                 /* fall through !!! */
3231
3232         case ZFCP_ERP_ACTION_REOPEN_PORT:
3233                 if (atomic_test_mask
3234                     (ZFCP_STATUS_COMMON_ERP_INUSE, &port->status)) {
3235                         debug_text_event(adapter->erp_dbf, 4, "p_actenq_drp");
3236                         debug_event(adapter->erp_dbf, 4, &port->wwpn,
3237                                     sizeof (wwn_t));
3238                         goto out;
3239                 }
3240                 /* fall through !!! */
3241
3242         case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
3243                 if (atomic_test_mask(ZFCP_STATUS_COMMON_ERP_INUSE,
3244                                      &port->status)) {
3245                         if (port->erp_action.action !=
3246                             ZFCP_ERP_ACTION_REOPEN_PORT_FORCED) {
3247                                 ZFCP_LOG_INFO("dropped erp action %i (port "
3248                                               "0x%016Lx, action in use: %i)\n",
3249                                               action, port->wwpn,
3250                                               port->erp_action.action);
3251                                 debug_text_event(adapter->erp_dbf, 4,
3252                                                  "pf_actenq_drp");
3253                         } else 
3254                                 debug_text_event(adapter->erp_dbf, 4,
3255                                                  "pf_actenq_drpcp");
3256                         debug_event(adapter->erp_dbf, 4, &port->wwpn,
3257                                     sizeof (wwn_t));
3258                         goto out;
3259                 }
3260                 if (!atomic_test_mask
3261                     (ZFCP_STATUS_COMMON_RUNNING, &adapter->status) ||
3262                     atomic_test_mask
3263                     (ZFCP_STATUS_COMMON_ERP_FAILED, &adapter->status)) {
3264                         goto out;
3265                 }
3266                 if (!atomic_test_mask
3267                     (ZFCP_STATUS_COMMON_UNBLOCKED, &adapter->status)) {
3268                         stronger_action = ZFCP_ERP_ACTION_REOPEN_ADAPTER;
3269                         port = NULL;
3270                 }
3271                 /* fall through !!! */
3272
3273         case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
3274                 if (atomic_test_mask
3275                     (ZFCP_STATUS_COMMON_ERP_INUSE, &adapter->status)) {
3276                         debug_text_event(adapter->erp_dbf, 4, "a_actenq_drp");
3277                         goto out;
3278                 }
3279                 break;
3280
3281         default:
3282                 debug_text_exception(adapter->erp_dbf, 1, "a_actenq_bug");
3283                 debug_event(adapter->erp_dbf, 1, &action, sizeof (int));
3284                 ZFCP_LOG_NORMAL("bug: unknown erp action requested "
3285                                 "on adapter %s (action=%d)\n",
3286                                 zfcp_get_busid_by_adapter(adapter), action);
3287                 goto out;
3288         }
3289
3290         /* check whether we need something stronger first */
3291         if (stronger_action) {
3292                 debug_text_event(adapter->erp_dbf, 4, "a_actenq_str");
3293                 debug_event(adapter->erp_dbf, 4, &stronger_action,
3294                             sizeof (int));
3295                 ZFCP_LOG_DEBUG("stronger erp action %d needed before "
3296                                "erp action %d on adapter %s\n",
3297                                stronger_action, action,
3298                                zfcp_get_busid_by_adapter(adapter));
3299                 action = stronger_action;
3300         }
3301
3302         /* mark adapter to have some error recovery pending */
3303         atomic_set_mask(ZFCP_STATUS_ADAPTER_ERP_PENDING, &adapter->status);
3304
3305         /* setup error recovery action */
3306         switch (action) {
3307
3308         case ZFCP_ERP_ACTION_REOPEN_UNIT:
3309                 zfcp_unit_get(unit);
3310                 atomic_set_mask(ZFCP_STATUS_COMMON_ERP_INUSE, &unit->status);
3311                 erp_action = &unit->erp_action;
3312                 if (!atomic_test_mask
3313                     (ZFCP_STATUS_COMMON_RUNNING, &unit->status))
3314                         status = ZFCP_STATUS_ERP_CLOSE_ONLY;
3315                 break;
3316
3317         case ZFCP_ERP_ACTION_REOPEN_PORT:
3318         case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
3319                 zfcp_port_get(port);
3320                 zfcp_erp_action_dismiss_port(port);
3321                 atomic_set_mask(ZFCP_STATUS_COMMON_ERP_INUSE, &port->status);
3322                 erp_action = &port->erp_action;
3323                 if (!atomic_test_mask
3324                     (ZFCP_STATUS_COMMON_RUNNING, &port->status))
3325                         status = ZFCP_STATUS_ERP_CLOSE_ONLY;
3326                 break;
3327
3328         case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
3329                 zfcp_adapter_get(adapter);
3330                 zfcp_erp_action_dismiss_adapter(adapter);
3331                 atomic_set_mask(ZFCP_STATUS_COMMON_ERP_INUSE, &adapter->status);
3332                 erp_action = &adapter->erp_action;
3333                 if (!atomic_test_mask
3334                     (ZFCP_STATUS_COMMON_RUNNING, &adapter->status))
3335                         status = ZFCP_STATUS_ERP_CLOSE_ONLY;
3336                 break;
3337         }
3338
3339         debug_text_event(adapter->erp_dbf, 4, "a_actenq");
3340
3341         memset(erp_action, 0, sizeof (struct zfcp_erp_action));
3342         erp_action->adapter = adapter;
3343         erp_action->port = port;
3344         erp_action->unit = unit;
3345         erp_action->action = action;
3346         erp_action->status = status;
3347
3348         ++adapter->erp_total_count;
3349
3350         /* finally put it into 'ready' queue and kick erp thread */
3351         list_add(&erp_action->list, &adapter->erp_ready_head);
3352         up(&adapter->erp_ready_sem);
3353         retval = 0;
3354  out:
3355         return retval;
3356 }
3357
3358 /*
3359  * function:    
3360  *
3361  * purpose:     
3362  *
3363  * returns:
3364  */
3365 static int
3366 zfcp_erp_action_dequeue(struct zfcp_erp_action *erp_action)
3367 {
3368         int retval = 0;
3369         struct zfcp_adapter *adapter = erp_action->adapter;
3370
3371         --adapter->erp_total_count;
3372         if (erp_action->status & ZFCP_STATUS_ERP_LOWMEM) {
3373                 --adapter->erp_low_mem_count;
3374                 erp_action->status &= ~ZFCP_STATUS_ERP_LOWMEM;
3375         }
3376
3377         debug_text_event(adapter->erp_dbf, 4, "a_actdeq");
3378         debug_event(adapter->erp_dbf, 4, &erp_action->action, sizeof (int));
3379         list_del(&erp_action->list);
3380         switch (erp_action->action) {
3381         case ZFCP_ERP_ACTION_REOPEN_UNIT:
3382                 atomic_clear_mask(ZFCP_STATUS_COMMON_ERP_INUSE,
3383                                   &erp_action->unit->status);
3384                 break;
3385         case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
3386         case ZFCP_ERP_ACTION_REOPEN_PORT:
3387                 atomic_clear_mask(ZFCP_STATUS_COMMON_ERP_INUSE,
3388                                   &erp_action->port->status);
3389                 break;
3390         case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
3391                 atomic_clear_mask(ZFCP_STATUS_COMMON_ERP_INUSE,
3392                                   &erp_action->adapter->status);
3393                 break;
3394         default:
3395                 /* bug */
3396                 break;
3397         }
3398         return retval;
3399 }
3400
3401 /**
3402  * zfcp_erp_action_cleanup
3403  *
3404  * Register unit with scsi stack if appropriate and fix reference counts.
3405  * Note: Temporary units are not registered with scsi stack.
3406  */
3407 static void
3408 zfcp_erp_action_cleanup(int action, struct zfcp_adapter *adapter,
3409                         struct zfcp_port *port, struct zfcp_unit *unit,
3410                         int result)
3411 {
3412         switch (action) {
3413         case ZFCP_ERP_ACTION_REOPEN_UNIT:
3414                 if ((result == ZFCP_ERP_SUCCEEDED)
3415                     && (!atomic_test_mask(ZFCP_STATUS_UNIT_TEMPORARY,
3416                                           &unit->status))
3417                     && !unit->device
3418                     && port->rport)
3419                         scsi_add_device(port->adapter->scsi_host, 0,
3420                                         port->rport->scsi_target_id,
3421                                         unit->scsi_lun);
3422                 zfcp_unit_put(unit);
3423                 break;
3424         case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
3425         case ZFCP_ERP_ACTION_REOPEN_PORT:
3426                 if ((result == ZFCP_ERP_SUCCEEDED)
3427                     && !atomic_test_mask(ZFCP_STATUS_PORT_NO_WWPN,
3428                                          &port->status)
3429                     && !port->rport) {
3430                         struct fc_rport_identifiers ids;
3431                         ids.node_name = port->wwnn;
3432                         ids.port_name = port->wwpn;
3433                         ids.port_id = port->d_id;
3434                         ids.roles = FC_RPORT_ROLE_FCP_TARGET;
3435                         port->rport =
3436                                 fc_remote_port_add(adapter->scsi_host, 0, &ids);
3437                         if (!port->rport)
3438                                 ZFCP_LOG_NORMAL("failed registration of rport"
3439                                                 "(adapter %s, wwpn=0x%016Lx)\n",
3440                                                 zfcp_get_busid_by_port(port),
3441                                                 port->wwpn);
3442                 }
3443                 zfcp_port_put(port);
3444                 break;
3445         case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
3446                 zfcp_adapter_put(adapter);
3447                 break;
3448         default:
3449                 break;
3450         }
3451 }
3452
3453
3454 /*
3455  * function:    
3456  *
3457  * purpose:     
3458  *
3459  * returns:     FIXME
3460  */
3461 static int
3462 zfcp_erp_action_dismiss_adapter(struct zfcp_adapter *adapter)
3463 {
3464         int retval = 0;
3465         struct zfcp_port *port;
3466
3467         debug_text_event(adapter->erp_dbf, 5, "a_actab");
3468         if (atomic_test_mask(ZFCP_STATUS_COMMON_ERP_INUSE, &adapter->status))
3469                 zfcp_erp_action_dismiss(&adapter->erp_action);
3470         else
3471                 list_for_each_entry(port, &adapter->port_list_head, list)
3472                     zfcp_erp_action_dismiss_port(port);
3473
3474         return retval;
3475 }
3476
3477 /*
3478  * function:    
3479  *
3480  * purpose:     
3481  *
3482  * returns:     FIXME
3483  */
3484 static int
3485 zfcp_erp_action_dismiss_port(struct zfcp_port *port)
3486 {
3487         int retval = 0;
3488         struct zfcp_unit *unit;
3489         struct zfcp_adapter *adapter = port->adapter;
3490
3491         debug_text_event(adapter->erp_dbf, 5, "p_actab");
3492         debug_event(adapter->erp_dbf, 5, &port->wwpn, sizeof (wwn_t));
3493         if (atomic_test_mask(ZFCP_STATUS_COMMON_ERP_INUSE, &port->status))
3494                 zfcp_erp_action_dismiss(&port->erp_action);
3495         else
3496                 list_for_each_entry(unit, &port->unit_list_head, list)
3497                     zfcp_erp_action_dismiss_unit(unit);
3498
3499         return retval;
3500 }
3501
3502 /*
3503  * function:    
3504  *
3505  * purpose:     
3506  *
3507  * returns:     FIXME
3508  */
3509 static int
3510 zfcp_erp_action_dismiss_unit(struct zfcp_unit *unit)
3511 {
3512         int retval = 0;
3513         struct zfcp_adapter *adapter = unit->port->adapter;
3514
3515         debug_text_event(adapter->erp_dbf, 5, "u_actab");
3516         debug_event(adapter->erp_dbf, 5, &unit->fcp_lun, sizeof (fcp_lun_t));
3517         if (atomic_test_mask(ZFCP_STATUS_COMMON_ERP_INUSE, &unit->status))
3518                 zfcp_erp_action_dismiss(&unit->erp_action);
3519
3520         return retval;
3521 }
3522
3523 /*
3524  * function:    
3525  *
3526  * purpose:     moves erp_action to 'erp running list'
3527  *
3528  * returns:
3529  */
3530 static inline void
3531 zfcp_erp_action_to_running(struct zfcp_erp_action *erp_action)
3532 {
3533         struct zfcp_adapter *adapter = erp_action->adapter;
3534
3535         debug_text_event(adapter->erp_dbf, 6, "a_toru");
3536         debug_event(adapter->erp_dbf, 6, &erp_action->action, sizeof (int));
3537         list_move(&erp_action->list, &erp_action->adapter->erp_running_head);
3538 }
3539
3540 /*
3541  * function:    
3542  *
3543  * purpose:     moves erp_action to 'erp ready list'
3544  *
3545  * returns:
3546  */
3547 static inline void
3548 zfcp_erp_action_to_ready(struct zfcp_erp_action *erp_action)
3549 {
3550         struct zfcp_adapter *adapter = erp_action->adapter;
3551
3552         debug_text_event(adapter->erp_dbf, 6, "a_tore");
3553         debug_event(adapter->erp_dbf, 6, &erp_action->action, sizeof (int));
3554         list_move(&erp_action->list, &erp_action->adapter->erp_ready_head);
3555 }
3556
3557 /*
3558  * function:    zfcp_erp_port_boxed
3559  *
3560  * purpose:
3561  */
3562 void
3563 zfcp_erp_port_boxed(struct zfcp_port *port)
3564 {
3565         struct zfcp_adapter *adapter = port->adapter;
3566         unsigned long flags;
3567
3568         debug_text_event(adapter->erp_dbf, 3, "p_access_boxed");
3569         debug_event(adapter->erp_dbf, 3, &port->wwpn, sizeof(wwn_t));
3570         read_lock_irqsave(&zfcp_data.config_lock, flags);
3571         zfcp_erp_modify_port_status(port,
3572                         ZFCP_STATUS_COMMON_ACCESS_BOXED,
3573                         ZFCP_SET);
3574         read_unlock_irqrestore(&zfcp_data.config_lock, flags);
3575         zfcp_erp_port_reopen(port, ZFCP_STATUS_COMMON_ERP_FAILED);
3576 }
3577
3578 /*
3579  * function:    zfcp_erp_unit_boxed
3580  *
3581  * purpose:
3582  */
3583 void
3584 zfcp_erp_unit_boxed(struct zfcp_unit *unit)
3585 {
3586         struct zfcp_adapter *adapter = unit->port->adapter;
3587
3588         debug_text_event(adapter->erp_dbf, 3, "u_access_boxed");
3589         debug_event(adapter->erp_dbf, 3, &unit->fcp_lun, sizeof(fcp_lun_t));
3590         zfcp_erp_modify_unit_status(unit,
3591                         ZFCP_STATUS_COMMON_ACCESS_BOXED,
3592                         ZFCP_SET);
3593         zfcp_erp_unit_reopen(unit, ZFCP_STATUS_COMMON_ERP_FAILED);
3594 }
3595
3596 /*
3597  * function:    zfcp_erp_port_access_denied
3598  *
3599  * purpose:
3600  */
3601 void
3602 zfcp_erp_port_access_denied(struct zfcp_port *port)
3603 {
3604         struct zfcp_adapter *adapter = port->adapter;
3605         unsigned long flags;
3606
3607         debug_text_event(adapter->erp_dbf, 3, "p_access_denied");
3608         debug_event(adapter->erp_dbf, 3, &port->wwpn, sizeof(wwn_t));
3609         read_lock_irqsave(&zfcp_data.config_lock, flags);
3610         zfcp_erp_modify_port_status(port,
3611                         ZFCP_STATUS_COMMON_ERP_FAILED |
3612                         ZFCP_STATUS_COMMON_ACCESS_DENIED,
3613                         ZFCP_SET);
3614         read_unlock_irqrestore(&zfcp_data.config_lock, flags);
3615 }
3616
3617 /*
3618  * function:    zfcp_erp_unit_access_denied
3619  *
3620  * purpose:
3621  */
3622 void
3623 zfcp_erp_unit_access_denied(struct zfcp_unit *unit)
3624 {
3625         struct zfcp_adapter *adapter = unit->port->adapter;
3626
3627         debug_text_event(adapter->erp_dbf, 3, "u_access_denied");
3628         debug_event(adapter->erp_dbf, 3, &unit->fcp_lun, sizeof(fcp_lun_t));
3629         zfcp_erp_modify_unit_status(unit,
3630                         ZFCP_STATUS_COMMON_ERP_FAILED |
3631                         ZFCP_STATUS_COMMON_ACCESS_DENIED,
3632                         ZFCP_SET);
3633 }
3634
3635 /*
3636  * function:    zfcp_erp_adapter_access_changed
3637  *
3638  * purpose:
3639  */
3640 void
3641 zfcp_erp_adapter_access_changed(struct zfcp_adapter *adapter)
3642 {
3643         struct zfcp_port *port;
3644         unsigned long flags;
3645
3646         if (adapter->connection_features & FSF_FEATURE_NPIV_MODE)
3647                 return;
3648
3649         debug_text_event(adapter->erp_dbf, 3, "a_access_recover");
3650         debug_event(adapter->erp_dbf, 3, &adapter->name, 8);
3651
3652         read_lock_irqsave(&zfcp_data.config_lock, flags);
3653         if (adapter->nameserver_port)
3654                 zfcp_erp_port_access_changed(adapter->nameserver_port);
3655         list_for_each_entry(port, &adapter->port_list_head, list)
3656                 if (port != adapter->nameserver_port)
3657                         zfcp_erp_port_access_changed(port);
3658         read_unlock_irqrestore(&zfcp_data.config_lock, flags);
3659 }
3660
3661 /*
3662  * function:    zfcp_erp_port_access_changed
3663  *
3664  * purpose:
3665  */
3666 void
3667 zfcp_erp_port_access_changed(struct zfcp_port *port)
3668 {
3669         struct zfcp_adapter *adapter = port->adapter;
3670         struct zfcp_unit *unit;
3671
3672         debug_text_event(adapter->erp_dbf, 3, "p_access_recover");
3673         debug_event(adapter->erp_dbf, 3, &port->wwpn, sizeof(wwn_t));
3674
3675         if (!atomic_test_mask(ZFCP_STATUS_COMMON_ACCESS_DENIED,
3676                               &port->status) &&
3677             !atomic_test_mask(ZFCP_STATUS_COMMON_ACCESS_BOXED,
3678                               &port->status)) {
3679                 if (!atomic_test_mask(ZFCP_STATUS_PORT_WKA, &port->status))
3680                         list_for_each_entry(unit, &port->unit_list_head, list)
3681                                 zfcp_erp_unit_access_changed(unit);
3682                 return;
3683         }
3684
3685         ZFCP_LOG_NORMAL("reopen of port 0x%016Lx on adapter %s "
3686                         "(due to ACT update)\n",
3687                         port->wwpn, zfcp_get_busid_by_adapter(adapter));
3688         if (zfcp_erp_port_reopen(port, ZFCP_STATUS_COMMON_ERP_FAILED) != 0)
3689                 ZFCP_LOG_NORMAL("failed reopen of port"
3690                                 "(adapter %s, wwpn=0x%016Lx)\n",
3691                                 zfcp_get_busid_by_adapter(adapter), port->wwpn);
3692 }
3693
3694 /*
3695  * function:    zfcp_erp_unit_access_changed
3696  *
3697  * purpose:
3698  */
3699 void
3700 zfcp_erp_unit_access_changed(struct zfcp_unit *unit)
3701 {
3702         struct zfcp_adapter *adapter = unit->port->adapter;
3703
3704         debug_text_event(adapter->erp_dbf, 3, "u_access_recover");
3705         debug_event(adapter->erp_dbf, 3, &unit->fcp_lun, sizeof(fcp_lun_t));
3706
3707         if (!atomic_test_mask(ZFCP_STATUS_COMMON_ACCESS_DENIED,
3708                               &unit->status) &&
3709             !atomic_test_mask(ZFCP_STATUS_COMMON_ACCESS_BOXED,
3710                               &unit->status))
3711                 return;
3712
3713         ZFCP_LOG_NORMAL("reopen of unit 0x%016Lx on port 0x%016Lx "
3714                         " on adapter %s (due to ACT update)\n",
3715                         unit->fcp_lun, unit->port->wwpn,
3716                         zfcp_get_busid_by_adapter(adapter));
3717         if (zfcp_erp_unit_reopen(unit, ZFCP_STATUS_COMMON_ERP_FAILED) != 0)
3718                 ZFCP_LOG_NORMAL("failed reopen of unit (adapter %s, "
3719                                 "wwpn=0x%016Lx, fcp_lun=0x%016Lx)\n",
3720                                 zfcp_get_busid_by_adapter(adapter),
3721                                 unit->port->wwpn, unit->fcp_lun);
3722 }
3723
3724 #undef ZFCP_LOG_AREA