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