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