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