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