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