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