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