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