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