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