[SCSI] zfcp: fix adapter erp when link is unplugged
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / s390 / scsi / zfcp_erp.c
blob8ed6fcb41653a83dd12976408bbf06602ef666c8
1 /*
2 *
3 * linux/drivers/s390/scsi/zfcp_erp.c
4 *
5 * FCP adapter driver for IBM eServer zSeries
6 *
7 * (C) Copyright IBM Corp. 2002, 2004
9 * Author(s): Martin Peschke <mpeschke@de.ibm.com>
10 * Raimund Schroeder <raimund.schroeder@de.ibm.com>
11 * Aron Zeh
12 * Wolfgang Taphorn
13 * Stefan Bader <stefan.bader@de.ibm.com>
14 * Heiko Carstens <heiko.carstens@de.ibm.com>
15 * Andreas Herrmann <aherrman@de.ibm.com>
17 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation; either version 2, or (at your option)
20 * any later version.
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
27 * You should have received a copy of the GNU General Public License
28 * along with this program; if not, write to the Free Software
29 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
32 #define ZFCP_LOG_AREA ZFCP_LOG_AREA_ERP
34 #include "zfcp_ext.h"
36 static int zfcp_erp_adisc(struct zfcp_port *);
37 static void zfcp_erp_adisc_handler(unsigned long);
39 static int zfcp_erp_adapter_reopen_internal(struct zfcp_adapter *, int);
40 static int zfcp_erp_port_forced_reopen_internal(struct zfcp_port *, int);
41 static int zfcp_erp_port_reopen_internal(struct zfcp_port *, int);
42 static int zfcp_erp_unit_reopen_internal(struct zfcp_unit *, int);
44 static int zfcp_erp_port_reopen_all_internal(struct zfcp_adapter *, int);
45 static int zfcp_erp_unit_reopen_all_internal(struct zfcp_port *, int);
47 static void zfcp_erp_adapter_block(struct zfcp_adapter *, int);
48 static void zfcp_erp_adapter_unblock(struct zfcp_adapter *);
49 static void zfcp_erp_port_block(struct zfcp_port *, int);
50 static void zfcp_erp_port_unblock(struct zfcp_port *);
51 static void zfcp_erp_unit_block(struct zfcp_unit *, int);
52 static void zfcp_erp_unit_unblock(struct zfcp_unit *);
54 static int zfcp_erp_thread(void *);
56 static int zfcp_erp_strategy(struct zfcp_erp_action *);
58 static int zfcp_erp_strategy_do_action(struct zfcp_erp_action *);
59 static int zfcp_erp_strategy_memwait(struct zfcp_erp_action *);
60 static int zfcp_erp_strategy_check_target(struct zfcp_erp_action *, int);
61 static int zfcp_erp_strategy_check_unit(struct zfcp_unit *, int);
62 static int zfcp_erp_strategy_check_port(struct zfcp_port *, int);
63 static int zfcp_erp_strategy_check_adapter(struct zfcp_adapter *, int);
64 static int zfcp_erp_strategy_statechange(int, u32, struct zfcp_adapter *,
65 struct zfcp_port *,
66 struct zfcp_unit *, int);
67 static inline int zfcp_erp_strategy_statechange_detected(atomic_t *, u32);
68 static int zfcp_erp_strategy_followup_actions(int, struct zfcp_adapter *,
69 struct zfcp_port *,
70 struct zfcp_unit *, int);
71 static int zfcp_erp_strategy_check_queues(struct zfcp_adapter *);
72 static int zfcp_erp_strategy_check_action(struct zfcp_erp_action *, int);
74 static int zfcp_erp_adapter_strategy(struct zfcp_erp_action *);
75 static int zfcp_erp_adapter_strategy_generic(struct zfcp_erp_action *, int);
76 static int zfcp_erp_adapter_strategy_close(struct zfcp_erp_action *);
77 static int zfcp_erp_adapter_strategy_close_qdio(struct zfcp_erp_action *);
78 static int zfcp_erp_adapter_strategy_close_fsf(struct zfcp_erp_action *);
79 static int zfcp_erp_adapter_strategy_open(struct zfcp_erp_action *);
80 static int zfcp_erp_adapter_strategy_open_qdio(struct zfcp_erp_action *);
81 static int zfcp_erp_adapter_strategy_open_fsf(struct zfcp_erp_action *);
82 static int zfcp_erp_adapter_strategy_open_fsf_xconfig(struct zfcp_erp_action *);
83 static int zfcp_erp_adapter_strategy_open_fsf_xport(struct zfcp_erp_action *);
84 static int zfcp_erp_adapter_strategy_open_fsf_statusread(
85 struct zfcp_erp_action *);
87 static int zfcp_erp_port_forced_strategy(struct zfcp_erp_action *);
88 static int zfcp_erp_port_forced_strategy_close(struct zfcp_erp_action *);
90 static int zfcp_erp_port_strategy(struct zfcp_erp_action *);
91 static int zfcp_erp_port_strategy_clearstati(struct zfcp_port *);
92 static int zfcp_erp_port_strategy_close(struct zfcp_erp_action *);
93 static int zfcp_erp_port_strategy_open(struct zfcp_erp_action *);
94 static int zfcp_erp_port_strategy_open_nameserver(struct zfcp_erp_action *);
95 static int zfcp_erp_port_strategy_open_nameserver_wakeup(
96 struct zfcp_erp_action *);
97 static int zfcp_erp_port_strategy_open_common(struct zfcp_erp_action *);
98 static int zfcp_erp_port_strategy_open_common_lookup(struct zfcp_erp_action *);
99 static int zfcp_erp_port_strategy_open_port(struct zfcp_erp_action *);
101 static int zfcp_erp_unit_strategy(struct zfcp_erp_action *);
102 static int zfcp_erp_unit_strategy_clearstati(struct zfcp_unit *);
103 static int zfcp_erp_unit_strategy_close(struct zfcp_erp_action *);
104 static int zfcp_erp_unit_strategy_open(struct zfcp_erp_action *);
106 static int zfcp_erp_action_dismiss_adapter(struct zfcp_adapter *);
107 static int zfcp_erp_action_dismiss_port(struct zfcp_port *);
108 static int zfcp_erp_action_dismiss_unit(struct zfcp_unit *);
109 static int zfcp_erp_action_dismiss(struct zfcp_erp_action *);
111 static int zfcp_erp_action_enqueue(int, struct zfcp_adapter *,
112 struct zfcp_port *, struct zfcp_unit *);
113 static int zfcp_erp_action_dequeue(struct zfcp_erp_action *);
114 static void zfcp_erp_action_cleanup(int, struct zfcp_adapter *,
115 struct zfcp_port *, struct zfcp_unit *,
116 int);
118 static void zfcp_erp_action_ready(struct zfcp_erp_action *);
119 static int zfcp_erp_action_exists(struct zfcp_erp_action *);
121 static inline void zfcp_erp_action_to_ready(struct zfcp_erp_action *);
122 static inline void zfcp_erp_action_to_running(struct zfcp_erp_action *);
124 static void zfcp_erp_memwait_handler(unsigned long);
125 static void zfcp_erp_timeout_handler(unsigned long);
126 static inline void zfcp_erp_timeout_init(struct zfcp_erp_action *);
129 * zfcp_fsf_request_timeout_handler - called if a request timed out
130 * @data: pointer to adapter for handler function
132 * This function needs to be called if requests (ELS, Generic Service,
133 * or SCSI commands) exceed a certain time limit. The assumption is
134 * that after the time limit the adapter get stuck. So we trigger a reopen of
135 * the adapter. This should not be used for error recovery, SCSI abort
136 * commands and SCSI requests from SCSI mid-layer.
138 void
139 zfcp_fsf_request_timeout_handler(unsigned long data)
141 struct zfcp_adapter *adapter;
143 adapter = (struct zfcp_adapter *) data;
145 zfcp_erp_adapter_reopen(adapter, 0);
149 * function: zfcp_fsf_scsi_er_timeout_handler
151 * purpose: This function needs to be called whenever a SCSI error recovery
152 * action (abort/reset) does not return.
153 * Re-opening the adapter means that the command can be returned
154 * by zfcp (it is guarranteed that it does not return via the
155 * adapter anymore). The buffer can then be used again.
157 * returns: sod all
159 void
160 zfcp_fsf_scsi_er_timeout_handler(unsigned long data)
162 struct zfcp_adapter *adapter = (struct zfcp_adapter *) data;
164 ZFCP_LOG_NORMAL("warning: SCSI error recovery timed out. "
165 "Restarting all operations on the adapter %s\n",
166 zfcp_get_busid_by_adapter(adapter));
167 debug_text_event(adapter->erp_dbf, 1, "eh_lmem_tout");
168 zfcp_erp_adapter_reopen(adapter, 0);
170 return;
174 * function:
176 * purpose: called if an adapter failed,
177 * initiates adapter recovery which is done
178 * asynchronously
180 * returns: 0 - initiated action succesfully
181 * <0 - failed to initiate action
184 zfcp_erp_adapter_reopen_internal(struct zfcp_adapter *adapter, int clear_mask)
186 int retval;
188 debug_text_event(adapter->erp_dbf, 5, "a_ro");
189 ZFCP_LOG_DEBUG("reopen adapter %s\n",
190 zfcp_get_busid_by_adapter(adapter));
192 zfcp_erp_adapter_block(adapter, clear_mask);
194 if (atomic_test_mask(ZFCP_STATUS_COMMON_ERP_FAILED, &adapter->status)) {
195 ZFCP_LOG_DEBUG("skipped reopen of failed adapter %s\n",
196 zfcp_get_busid_by_adapter(adapter));
197 debug_text_event(adapter->erp_dbf, 5, "a_ro_f");
198 /* ensure propagation of failed status to new devices */
199 zfcp_erp_adapter_failed(adapter);
200 retval = -EIO;
201 goto out;
203 retval = zfcp_erp_action_enqueue(ZFCP_ERP_ACTION_REOPEN_ADAPTER,
204 adapter, NULL, NULL);
206 out:
207 return retval;
211 * function:
213 * purpose: Wrappper for zfcp_erp_adapter_reopen_internal
214 * used to ensure the correct locking
216 * returns: 0 - initiated action succesfully
217 * <0 - failed to initiate action
220 zfcp_erp_adapter_reopen(struct zfcp_adapter *adapter, int clear_mask)
222 int retval;
223 unsigned long flags;
225 read_lock_irqsave(&zfcp_data.config_lock, flags);
226 write_lock(&adapter->erp_lock);
227 retval = zfcp_erp_adapter_reopen_internal(adapter, clear_mask);
228 write_unlock(&adapter->erp_lock);
229 read_unlock_irqrestore(&zfcp_data.config_lock, flags);
231 return retval;
235 * function:
237 * purpose:
239 * returns:
242 zfcp_erp_adapter_shutdown(struct zfcp_adapter *adapter, int clear_mask)
244 int retval;
246 retval = zfcp_erp_adapter_reopen(adapter,
247 ZFCP_STATUS_COMMON_RUNNING |
248 ZFCP_STATUS_COMMON_ERP_FAILED |
249 clear_mask);
251 return retval;
255 * function:
257 * purpose:
259 * returns:
262 zfcp_erp_port_shutdown(struct zfcp_port *port, int clear_mask)
264 int retval;
266 retval = zfcp_erp_port_reopen(port,
267 ZFCP_STATUS_COMMON_RUNNING |
268 ZFCP_STATUS_COMMON_ERP_FAILED |
269 clear_mask);
271 return retval;
275 * function:
277 * purpose:
279 * returns:
282 zfcp_erp_unit_shutdown(struct zfcp_unit *unit, int clear_mask)
284 int retval;
286 retval = zfcp_erp_unit_reopen(unit,
287 ZFCP_STATUS_COMMON_RUNNING |
288 ZFCP_STATUS_COMMON_ERP_FAILED |
289 clear_mask);
291 return retval;
296 * zfcp_erp_adisc - send ADISC ELS command
297 * @port: port structure
300 zfcp_erp_adisc(struct zfcp_port *port)
302 struct zfcp_adapter *adapter = port->adapter;
303 struct zfcp_send_els *send_els;
304 struct zfcp_ls_adisc *adisc;
305 void *address = NULL;
306 int retval = 0;
307 struct timer_list *timer;
309 send_els = kmalloc(sizeof(struct zfcp_send_els), GFP_ATOMIC);
310 if (send_els == NULL)
311 goto nomem;
312 memset(send_els, 0, sizeof(*send_els));
314 send_els->req = kmalloc(sizeof(struct scatterlist), GFP_ATOMIC);
315 if (send_els->req == NULL)
316 goto nomem;
317 memset(send_els->req, 0, sizeof(*send_els->req));
319 send_els->resp = kmalloc(sizeof(struct scatterlist), GFP_ATOMIC);
320 if (send_els->resp == NULL)
321 goto nomem;
322 memset(send_els->resp, 0, sizeof(*send_els->resp));
324 address = (void *) get_zeroed_page(GFP_ATOMIC);
325 if (address == NULL)
326 goto nomem;
328 zfcp_address_to_sg(address, send_els->req);
329 address += PAGE_SIZE >> 1;
330 zfcp_address_to_sg(address, send_els->resp);
331 send_els->req_count = send_els->resp_count = 1;
333 send_els->adapter = adapter;
334 send_els->port = port;
335 send_els->d_id = port->d_id;
336 send_els->handler = zfcp_erp_adisc_handler;
337 send_els->handler_data = (unsigned long) send_els;
339 adisc = zfcp_sg_to_address(send_els->req);
340 send_els->ls_code = adisc->code = ZFCP_LS_ADISC;
342 send_els->req->length = sizeof(struct zfcp_ls_adisc);
343 send_els->resp->length = sizeof(struct zfcp_ls_adisc_acc);
345 /* acc. to FC-FS, hard_nport_id in ADISC should not be set for ports
346 without FC-AL-2 capability, so we don't set it */
347 adisc->wwpn = fc_host_port_name(adapter->scsi_host);
348 adisc->wwnn = fc_host_node_name(adapter->scsi_host);
349 adisc->nport_id = fc_host_port_id(adapter->scsi_host);
350 ZFCP_LOG_INFO("ADISC request from s_id 0x%08x to d_id 0x%08x "
351 "(wwpn=0x%016Lx, wwnn=0x%016Lx, "
352 "hard_nport_id=0x%08x, nport_id=0x%08x)\n",
353 adisc->nport_id, send_els->d_id, (wwn_t) adisc->wwpn,
354 (wwn_t) adisc->wwnn, adisc->hard_nport_id,
355 adisc->nport_id);
357 timer = kmalloc(sizeof(struct timer_list), GFP_ATOMIC);
358 if (!timer)
359 goto nomem;
361 init_timer(timer);
362 timer->function = zfcp_fsf_request_timeout_handler;
363 timer->data = (unsigned long) adapter;
364 timer->expires = ZFCP_FSF_REQUEST_TIMEOUT;
365 send_els->timer = timer;
367 retval = zfcp_fsf_send_els(send_els);
368 if (retval != 0) {
369 ZFCP_LOG_NORMAL("error: initiation of Send ELS failed for port "
370 "0x%08x on adapter %s\n", send_els->d_id,
371 zfcp_get_busid_by_adapter(adapter));
372 del_timer(send_els->timer);
373 goto freemem;
376 goto out;
378 nomem:
379 retval = -ENOMEM;
380 freemem:
381 if (address != NULL)
382 __free_pages(send_els->req->page, 0);
383 if (send_els != NULL) {
384 kfree(send_els->timer);
385 kfree(send_els->req);
386 kfree(send_els->resp);
387 kfree(send_els);
389 out:
390 return retval;
395 * zfcp_erp_adisc_handler - handler for ADISC ELS command
396 * @data: pointer to struct zfcp_send_els
398 * If ADISC failed (LS_RJT or timed out) forced reopen of the port is triggered.
400 void
401 zfcp_erp_adisc_handler(unsigned long data)
403 struct zfcp_send_els *send_els;
404 struct zfcp_port *port;
405 struct zfcp_adapter *adapter;
406 u32 d_id;
407 struct zfcp_ls_adisc_acc *adisc;
409 send_els = (struct zfcp_send_els *) data;
411 del_timer(send_els->timer);
413 adapter = send_els->adapter;
414 port = send_els->port;
415 d_id = send_els->d_id;
417 /* request rejected or timed out */
418 if (send_els->status != 0) {
419 ZFCP_LOG_NORMAL("ELS request rejected/timed out, "
420 "force physical port reopen "
421 "(adapter %s, port d_id=0x%08x)\n",
422 zfcp_get_busid_by_adapter(adapter), d_id);
423 debug_text_event(adapter->erp_dbf, 3, "forcreop");
424 if (zfcp_erp_port_forced_reopen(port, 0))
425 ZFCP_LOG_NORMAL("failed reopen of port "
426 "(adapter %s, wwpn=0x%016Lx)\n",
427 zfcp_get_busid_by_port(port),
428 port->wwpn);
429 goto out;
432 adisc = zfcp_sg_to_address(send_els->resp);
434 ZFCP_LOG_INFO("ADISC response from d_id 0x%08x to s_id "
435 "0x%08x (wwpn=0x%016Lx, wwnn=0x%016Lx, "
436 "hard_nport_id=0x%08x, nport_id=0x%08x)\n",
437 d_id, fc_host_port_id(adapter->scsi_host),
438 (wwn_t) adisc->wwpn, (wwn_t) adisc->wwnn,
439 adisc->hard_nport_id, adisc->nport_id);
441 /* set wwnn for port */
442 if (port->wwnn == 0)
443 port->wwnn = adisc->wwnn;
445 if (port->wwpn != adisc->wwpn) {
446 ZFCP_LOG_NORMAL("d_id assignment changed, reopening "
447 "port (adapter %s, wwpn=0x%016Lx, "
448 "adisc_resp_wwpn=0x%016Lx)\n",
449 zfcp_get_busid_by_port(port),
450 port->wwpn, (wwn_t) adisc->wwpn);
451 if (zfcp_erp_port_reopen(port, 0))
452 ZFCP_LOG_NORMAL("failed reopen of port "
453 "(adapter %s, wwpn=0x%016Lx)\n",
454 zfcp_get_busid_by_port(port),
455 port->wwpn);
458 out:
459 zfcp_port_put(port);
460 __free_pages(send_els->req->page, 0);
461 kfree(send_els->timer);
462 kfree(send_els->req);
463 kfree(send_els->resp);
464 kfree(send_els);
469 * zfcp_test_link - lightweight link test procedure
470 * @port: port to be tested
472 * Test status of a link to a remote port using the ELS command ADISC.
475 zfcp_test_link(struct zfcp_port *port)
477 int retval;
479 zfcp_port_get(port);
480 retval = zfcp_erp_adisc(port);
481 if (retval != 0) {
482 zfcp_port_put(port);
483 ZFCP_LOG_NORMAL("reopen needed for port 0x%016Lx "
484 "on adapter %s\n ", port->wwpn,
485 zfcp_get_busid_by_port(port));
486 retval = zfcp_erp_port_forced_reopen(port, 0);
487 if (retval != 0) {
488 ZFCP_LOG_NORMAL("reopen of remote port 0x%016Lx "
489 "on adapter %s failed\n", port->wwpn,
490 zfcp_get_busid_by_port(port));
491 retval = -EPERM;
495 return retval;
500 * function:
502 * purpose: called if a port failed to be opened normally
503 * initiates Forced Reopen recovery which is done
504 * asynchronously
506 * returns: 0 - initiated action succesfully
507 * <0 - failed to initiate action
509 static int
510 zfcp_erp_port_forced_reopen_internal(struct zfcp_port *port, int clear_mask)
512 int retval;
513 struct zfcp_adapter *adapter = port->adapter;
515 debug_text_event(adapter->erp_dbf, 5, "pf_ro");
516 debug_event(adapter->erp_dbf, 5, &port->wwpn, sizeof (wwn_t));
518 ZFCP_LOG_DEBUG("forced reopen of port 0x%016Lx on adapter %s\n",
519 port->wwpn, zfcp_get_busid_by_port(port));
521 zfcp_erp_port_block(port, clear_mask);
523 if (atomic_test_mask(ZFCP_STATUS_COMMON_ERP_FAILED, &port->status)) {
524 ZFCP_LOG_DEBUG("skipped forced reopen of failed port 0x%016Lx "
525 "on adapter %s\n", port->wwpn,
526 zfcp_get_busid_by_port(port));
527 debug_text_event(adapter->erp_dbf, 5, "pf_ro_f");
528 debug_event(adapter->erp_dbf, 5, &port->wwpn, sizeof (wwn_t));
529 retval = -EIO;
530 goto out;
533 retval = zfcp_erp_action_enqueue(ZFCP_ERP_ACTION_REOPEN_PORT_FORCED,
534 port->adapter, port, NULL);
536 out:
537 return retval;
541 * function:
543 * purpose: Wrappper for zfcp_erp_port_forced_reopen_internal
544 * used to ensure the correct locking
546 * returns: 0 - initiated action succesfully
547 * <0 - failed to initiate action
550 zfcp_erp_port_forced_reopen(struct zfcp_port *port, int clear_mask)
552 int retval;
553 unsigned long flags;
554 struct zfcp_adapter *adapter;
556 adapter = port->adapter;
557 read_lock_irqsave(&zfcp_data.config_lock, flags);
558 write_lock(&adapter->erp_lock);
559 retval = zfcp_erp_port_forced_reopen_internal(port, clear_mask);
560 write_unlock(&adapter->erp_lock);
561 read_unlock_irqrestore(&zfcp_data.config_lock, flags);
563 return retval;
567 * function:
569 * purpose: called if a port is to be opened
570 * initiates Reopen recovery which is done
571 * asynchronously
573 * returns: 0 - initiated action succesfully
574 * <0 - failed to initiate action
576 static int
577 zfcp_erp_port_reopen_internal(struct zfcp_port *port, int clear_mask)
579 int retval;
580 struct zfcp_adapter *adapter = port->adapter;
582 debug_text_event(adapter->erp_dbf, 5, "p_ro");
583 debug_event(adapter->erp_dbf, 5, &port->wwpn, sizeof (wwn_t));
585 ZFCP_LOG_DEBUG("reopen of port 0x%016Lx on adapter %s\n",
586 port->wwpn, zfcp_get_busid_by_port(port));
588 zfcp_erp_port_block(port, clear_mask);
590 if (atomic_test_mask(ZFCP_STATUS_COMMON_ERP_FAILED, &port->status)) {
591 ZFCP_LOG_DEBUG("skipped reopen of failed port 0x%016Lx "
592 "on adapter %s\n", port->wwpn,
593 zfcp_get_busid_by_port(port));
594 debug_text_event(adapter->erp_dbf, 5, "p_ro_f");
595 debug_event(adapter->erp_dbf, 5, &port->wwpn, sizeof (wwn_t));
596 /* ensure propagation of failed status to new devices */
597 zfcp_erp_port_failed(port);
598 retval = -EIO;
599 goto out;
602 retval = zfcp_erp_action_enqueue(ZFCP_ERP_ACTION_REOPEN_PORT,
603 port->adapter, port, NULL);
605 out:
606 return retval;
610 * zfcp_erp_port_reopen - initiate reopen of a remote port
611 * @port: port to be reopened
612 * @clear_mask: specifies flags in port status to be cleared
613 * Return: 0 on success, < 0 on error
615 * This is a wrappper function for zfcp_erp_port_reopen_internal. It ensures
616 * correct locking. An error recovery task is initiated to do the reopen.
617 * To wait for the completion of the reopen zfcp_erp_wait should be used.
620 zfcp_erp_port_reopen(struct zfcp_port *port, int clear_mask)
622 int retval;
623 unsigned long flags;
624 struct zfcp_adapter *adapter = port->adapter;
626 read_lock_irqsave(&zfcp_data.config_lock, flags);
627 write_lock(&adapter->erp_lock);
628 retval = zfcp_erp_port_reopen_internal(port, clear_mask);
629 write_unlock(&adapter->erp_lock);
630 read_unlock_irqrestore(&zfcp_data.config_lock, flags);
632 return retval;
636 * function:
638 * purpose: called if a unit is to be opened
639 * initiates Reopen recovery which is done
640 * asynchronously
642 * returns: 0 - initiated action succesfully
643 * <0 - failed to initiate action
645 static int
646 zfcp_erp_unit_reopen_internal(struct zfcp_unit *unit, int clear_mask)
648 int retval;
649 struct zfcp_adapter *adapter = unit->port->adapter;
651 debug_text_event(adapter->erp_dbf, 5, "u_ro");
652 debug_event(adapter->erp_dbf, 5, &unit->fcp_lun, sizeof (fcp_lun_t));
653 ZFCP_LOG_DEBUG("reopen of unit 0x%016Lx on port 0x%016Lx "
654 "on adapter %s\n", unit->fcp_lun,
655 unit->port->wwpn, zfcp_get_busid_by_unit(unit));
657 zfcp_erp_unit_block(unit, clear_mask);
659 if (atomic_test_mask(ZFCP_STATUS_COMMON_ERP_FAILED, &unit->status)) {
660 ZFCP_LOG_DEBUG("skipped reopen of failed unit 0x%016Lx "
661 "on port 0x%016Lx on adapter %s\n",
662 unit->fcp_lun, unit->port->wwpn,
663 zfcp_get_busid_by_unit(unit));
664 debug_text_event(adapter->erp_dbf, 5, "u_ro_f");
665 debug_event(adapter->erp_dbf, 5, &unit->fcp_lun,
666 sizeof (fcp_lun_t));
667 retval = -EIO;
668 goto out;
671 retval = zfcp_erp_action_enqueue(ZFCP_ERP_ACTION_REOPEN_UNIT,
672 unit->port->adapter, unit->port, unit);
673 out:
674 return retval;
678 * zfcp_erp_unit_reopen - initiate reopen of a unit
679 * @unit: unit to be reopened
680 * @clear_mask: specifies flags in unit status to be cleared
681 * Return: 0 on success, < 0 on error
683 * This is a wrappper for zfcp_erp_unit_reopen_internal. It ensures correct
684 * locking. An error recovery task is initiated to do the reopen.
685 * To wait for the completion of the reopen zfcp_erp_wait should be used.
688 zfcp_erp_unit_reopen(struct zfcp_unit *unit, int clear_mask)
690 int retval;
691 unsigned long flags;
692 struct zfcp_adapter *adapter;
693 struct zfcp_port *port;
695 port = unit->port;
696 adapter = port->adapter;
698 read_lock_irqsave(&zfcp_data.config_lock, flags);
699 write_lock(&adapter->erp_lock);
700 retval = zfcp_erp_unit_reopen_internal(unit, clear_mask);
701 write_unlock(&adapter->erp_lock);
702 read_unlock_irqrestore(&zfcp_data.config_lock, flags);
704 return retval;
708 * function:
710 * purpose: disable I/O,
711 * return any open requests and clean them up,
712 * aim: no pending and incoming I/O
714 * returns:
716 static void
717 zfcp_erp_adapter_block(struct zfcp_adapter *adapter, int clear_mask)
719 debug_text_event(adapter->erp_dbf, 6, "a_bl");
720 zfcp_erp_modify_adapter_status(adapter,
721 ZFCP_STATUS_COMMON_UNBLOCKED |
722 clear_mask, ZFCP_CLEAR);
726 * function:
728 * purpose: enable I/O
730 * returns:
732 static void
733 zfcp_erp_adapter_unblock(struct zfcp_adapter *adapter)
735 debug_text_event(adapter->erp_dbf, 6, "a_ubl");
736 atomic_set_mask(ZFCP_STATUS_COMMON_UNBLOCKED, &adapter->status);
740 * function:
742 * purpose: disable I/O,
743 * return any open requests and clean them up,
744 * aim: no pending and incoming I/O
746 * returns:
748 static void
749 zfcp_erp_port_block(struct zfcp_port *port, int clear_mask)
751 struct zfcp_adapter *adapter = port->adapter;
753 debug_text_event(adapter->erp_dbf, 6, "p_bl");
754 debug_event(adapter->erp_dbf, 6, &port->wwpn, sizeof (wwn_t));
755 zfcp_erp_modify_port_status(port,
756 ZFCP_STATUS_COMMON_UNBLOCKED | clear_mask,
757 ZFCP_CLEAR);
761 * function:
763 * purpose: enable I/O
765 * returns:
767 static void
768 zfcp_erp_port_unblock(struct zfcp_port *port)
770 struct zfcp_adapter *adapter = port->adapter;
772 debug_text_event(adapter->erp_dbf, 6, "p_ubl");
773 debug_event(adapter->erp_dbf, 6, &port->wwpn, sizeof (wwn_t));
774 atomic_set_mask(ZFCP_STATUS_COMMON_UNBLOCKED, &port->status);
778 * function:
780 * purpose: disable I/O,
781 * return any open requests and clean them up,
782 * aim: no pending and incoming I/O
784 * returns:
786 static void
787 zfcp_erp_unit_block(struct zfcp_unit *unit, int clear_mask)
789 struct zfcp_adapter *adapter = unit->port->adapter;
791 debug_text_event(adapter->erp_dbf, 6, "u_bl");
792 debug_event(adapter->erp_dbf, 6, &unit->fcp_lun, sizeof (fcp_lun_t));
793 zfcp_erp_modify_unit_status(unit,
794 ZFCP_STATUS_COMMON_UNBLOCKED | clear_mask,
795 ZFCP_CLEAR);
799 * function:
801 * purpose: enable I/O
803 * returns:
805 static void
806 zfcp_erp_unit_unblock(struct zfcp_unit *unit)
808 struct zfcp_adapter *adapter = unit->port->adapter;
810 debug_text_event(adapter->erp_dbf, 6, "u_ubl");
811 debug_event(adapter->erp_dbf, 6, &unit->fcp_lun, sizeof (fcp_lun_t));
812 atomic_set_mask(ZFCP_STATUS_COMMON_UNBLOCKED, &unit->status);
816 * function:
818 * purpose:
820 * returns:
822 static void
823 zfcp_erp_action_ready(struct zfcp_erp_action *erp_action)
825 struct zfcp_adapter *adapter = erp_action->adapter;
827 debug_text_event(adapter->erp_dbf, 4, "a_ar");
828 debug_event(adapter->erp_dbf, 4, &erp_action->action, sizeof (int));
830 zfcp_erp_action_to_ready(erp_action);
831 up(&adapter->erp_ready_sem);
835 * function:
837 * purpose:
839 * returns: <0 erp_action not found in any list
840 * ZFCP_ERP_ACTION_READY erp_action is in ready list
841 * ZFCP_ERP_ACTION_RUNNING erp_action is in running list
843 * locks: erp_lock must be held
845 static int
846 zfcp_erp_action_exists(struct zfcp_erp_action *erp_action)
848 int retval = -EINVAL;
849 struct list_head *entry;
850 struct zfcp_erp_action *entry_erp_action;
851 struct zfcp_adapter *adapter = erp_action->adapter;
853 /* search in running list */
854 list_for_each(entry, &adapter->erp_running_head) {
855 entry_erp_action =
856 list_entry(entry, struct zfcp_erp_action, list);
857 if (entry_erp_action == erp_action) {
858 retval = ZFCP_ERP_ACTION_RUNNING;
859 goto out;
862 /* search in ready list */
863 list_for_each(entry, &adapter->erp_ready_head) {
864 entry_erp_action =
865 list_entry(entry, struct zfcp_erp_action, list);
866 if (entry_erp_action == erp_action) {
867 retval = ZFCP_ERP_ACTION_READY;
868 goto out;
872 out:
873 return retval;
877 * purpose: checks current status of action (timed out, dismissed, ...)
878 * and does appropriate preparations (dismiss fsf request, ...)
880 * locks: called under erp_lock (disabled interrupts)
882 * returns: 0
884 static int
885 zfcp_erp_strategy_check_fsfreq(struct zfcp_erp_action *erp_action)
887 int retval = 0;
888 struct zfcp_fsf_req *fsf_req = NULL;
889 struct zfcp_adapter *adapter = erp_action->adapter;
891 if (erp_action->fsf_req) {
892 /* take lock to ensure that request is not being deleted meanwhile */
893 spin_lock(&adapter->fsf_req_list_lock);
894 /* check whether fsf req does still exist */
895 list_for_each_entry(fsf_req, &adapter->fsf_req_list_head, list)
896 if (fsf_req == erp_action->fsf_req)
897 break;
898 if (fsf_req && (fsf_req->erp_action == erp_action)) {
899 /* fsf_req still exists */
900 debug_text_event(adapter->erp_dbf, 3, "a_ca_req");
901 debug_event(adapter->erp_dbf, 3, &fsf_req,
902 sizeof (unsigned long));
903 /* dismiss fsf_req of timed out or dismissed erp_action */
904 if (erp_action->status & (ZFCP_STATUS_ERP_DISMISSED |
905 ZFCP_STATUS_ERP_TIMEDOUT)) {
906 debug_text_event(adapter->erp_dbf, 3,
907 "a_ca_disreq");
908 fsf_req->status |= ZFCP_STATUS_FSFREQ_DISMISSED;
910 if (erp_action->status & ZFCP_STATUS_ERP_TIMEDOUT) {
911 ZFCP_LOG_NORMAL("error: erp step timed out "
912 "(action=%d, fsf_req=%p)\n ",
913 erp_action->action,
914 erp_action->fsf_req);
917 * If fsf_req is neither dismissed nor completed
918 * then keep it running asynchronously and don't mess
919 * with the association of erp_action and fsf_req.
921 if (fsf_req->status & (ZFCP_STATUS_FSFREQ_COMPLETED |
922 ZFCP_STATUS_FSFREQ_DISMISSED)) {
923 /* forget about association between fsf_req
924 and erp_action */
925 fsf_req->erp_action = NULL;
926 erp_action->fsf_req = NULL;
928 } else {
929 debug_text_event(adapter->erp_dbf, 3, "a_ca_gonereq");
931 * even if this fsf_req has gone, forget about
932 * association between erp_action and fsf_req
934 erp_action->fsf_req = NULL;
936 spin_unlock(&adapter->fsf_req_list_lock);
937 } else
938 debug_text_event(adapter->erp_dbf, 3, "a_ca_noreq");
940 return retval;
944 * purpose: generic handler for asynchronous events related to erp_action events
945 * (normal completion, time-out, dismissing, retry after
946 * low memory condition)
948 * note: deletion of timer is not required (e.g. in case of a time-out),
949 * but a second try does no harm,
950 * we leave it in here to allow for greater simplification
952 * returns: 0 - there was an action to handle
953 * !0 - otherwise
955 static int
956 zfcp_erp_async_handler_nolock(struct zfcp_erp_action *erp_action,
957 unsigned long set_mask)
959 int retval;
960 struct zfcp_adapter *adapter = erp_action->adapter;
962 if (zfcp_erp_action_exists(erp_action) == ZFCP_ERP_ACTION_RUNNING) {
963 debug_text_event(adapter->erp_dbf, 2, "a_asyh_ex");
964 debug_event(adapter->erp_dbf, 2, &erp_action->action,
965 sizeof (int));
966 if (!(set_mask & ZFCP_STATUS_ERP_TIMEDOUT))
967 del_timer(&erp_action->timer);
968 erp_action->status |= set_mask;
969 zfcp_erp_action_ready(erp_action);
970 retval = 0;
971 } else {
972 /* action is ready or gone - nothing to do */
973 debug_text_event(adapter->erp_dbf, 3, "a_asyh_gone");
974 debug_event(adapter->erp_dbf, 3, &erp_action->action,
975 sizeof (int));
976 retval = 1;
979 return retval;
983 * purpose: generic handler for asynchronous events related to erp_action
984 * events (normal completion, time-out, dismissing, retry after
985 * low memory condition)
987 * note: deletion of timer is not required (e.g. in case of a time-out),
988 * but a second try does no harm,
989 * we leave it in here to allow for greater simplification
991 * returns: 0 - there was an action to handle
992 * !0 - otherwise
995 zfcp_erp_async_handler(struct zfcp_erp_action *erp_action,
996 unsigned long set_mask)
998 struct zfcp_adapter *adapter = erp_action->adapter;
999 unsigned long flags;
1000 int retval;
1002 write_lock_irqsave(&adapter->erp_lock, flags);
1003 retval = zfcp_erp_async_handler_nolock(erp_action, set_mask);
1004 write_unlock_irqrestore(&adapter->erp_lock, flags);
1006 return retval;
1010 * purpose: is called for erp_action which was slept waiting for
1011 * memory becoming avaliable,
1012 * will trigger that this action will be continued
1014 static void
1015 zfcp_erp_memwait_handler(unsigned long data)
1017 struct zfcp_erp_action *erp_action = (struct zfcp_erp_action *) data;
1018 struct zfcp_adapter *adapter = erp_action->adapter;
1020 debug_text_event(adapter->erp_dbf, 2, "a_mwh");
1021 debug_event(adapter->erp_dbf, 2, &erp_action->action, sizeof (int));
1023 zfcp_erp_async_handler(erp_action, 0);
1027 * purpose: is called if an asynchronous erp step timed out,
1028 * action gets an appropriate flag and will be processed
1029 * accordingly
1031 static void
1032 zfcp_erp_timeout_handler(unsigned long data)
1034 struct zfcp_erp_action *erp_action = (struct zfcp_erp_action *) data;
1035 struct zfcp_adapter *adapter = erp_action->adapter;
1037 debug_text_event(adapter->erp_dbf, 2, "a_th");
1038 debug_event(adapter->erp_dbf, 2, &erp_action->action, sizeof (int));
1040 zfcp_erp_async_handler(erp_action, ZFCP_STATUS_ERP_TIMEDOUT);
1044 * purpose: is called for an erp_action which needs to be ended
1045 * though not being done,
1046 * this is usually required if an higher is generated,
1047 * action gets an appropriate flag and will be processed
1048 * accordingly
1050 * locks: erp_lock held (thus we need to call another handler variant)
1052 static int
1053 zfcp_erp_action_dismiss(struct zfcp_erp_action *erp_action)
1055 struct zfcp_adapter *adapter = erp_action->adapter;
1057 debug_text_event(adapter->erp_dbf, 2, "a_adis");
1058 debug_event(adapter->erp_dbf, 2, &erp_action->action, sizeof (int));
1060 zfcp_erp_async_handler_nolock(erp_action, ZFCP_STATUS_ERP_DISMISSED);
1062 return 0;
1066 zfcp_erp_thread_setup(struct zfcp_adapter *adapter)
1068 int retval = 0;
1070 atomic_clear_mask(ZFCP_STATUS_ADAPTER_ERP_THREAD_UP, &adapter->status);
1072 retval = kernel_thread(zfcp_erp_thread, adapter, SIGCHLD);
1073 if (retval < 0) {
1074 ZFCP_LOG_NORMAL("error: creation of erp thread failed for "
1075 "adapter %s\n",
1076 zfcp_get_busid_by_adapter(adapter));
1077 debug_text_event(adapter->erp_dbf, 5, "a_thset_fail");
1078 } else {
1079 wait_event(adapter->erp_thread_wqh,
1080 atomic_test_mask(ZFCP_STATUS_ADAPTER_ERP_THREAD_UP,
1081 &adapter->status));
1082 debug_text_event(adapter->erp_dbf, 5, "a_thset_ok");
1085 return (retval < 0);
1089 * function:
1091 * purpose:
1093 * returns:
1095 * context: process (i.e. proc-fs or rmmod/insmod)
1097 * note: The caller of this routine ensures that the specified
1098 * adapter has been shut down and that this operation
1099 * has been completed. Thus, there are no pending erp_actions
1100 * which would need to be handled here.
1103 zfcp_erp_thread_kill(struct zfcp_adapter *adapter)
1105 int retval = 0;
1107 atomic_set_mask(ZFCP_STATUS_ADAPTER_ERP_THREAD_KILL, &adapter->status);
1108 up(&adapter->erp_ready_sem);
1110 wait_event(adapter->erp_thread_wqh,
1111 !atomic_test_mask(ZFCP_STATUS_ADAPTER_ERP_THREAD_UP,
1112 &adapter->status));
1114 atomic_clear_mask(ZFCP_STATUS_ADAPTER_ERP_THREAD_KILL,
1115 &adapter->status);
1117 debug_text_event(adapter->erp_dbf, 5, "a_thki_ok");
1119 return retval;
1123 * purpose: is run as a kernel thread,
1124 * goes through list of error recovery actions of associated adapter
1125 * and delegates single action to execution
1127 * returns: 0
1129 static int
1130 zfcp_erp_thread(void *data)
1132 struct zfcp_adapter *adapter = (struct zfcp_adapter *) data;
1133 struct list_head *next;
1134 struct zfcp_erp_action *erp_action;
1135 unsigned long flags;
1137 daemonize("zfcperp%s", zfcp_get_busid_by_adapter(adapter));
1138 /* Block all signals */
1139 siginitsetinv(&current->blocked, 0);
1140 atomic_set_mask(ZFCP_STATUS_ADAPTER_ERP_THREAD_UP, &adapter->status);
1141 debug_text_event(adapter->erp_dbf, 5, "a_th_run");
1142 wake_up(&adapter->erp_thread_wqh);
1144 while (!atomic_test_mask(ZFCP_STATUS_ADAPTER_ERP_THREAD_KILL,
1145 &adapter->status)) {
1147 write_lock_irqsave(&adapter->erp_lock, flags);
1148 next = adapter->erp_ready_head.prev;
1149 write_unlock_irqrestore(&adapter->erp_lock, flags);
1151 if (next != &adapter->erp_ready_head) {
1152 erp_action =
1153 list_entry(next, struct zfcp_erp_action, list);
1155 * process action (incl. [re]moving it
1156 * from 'ready' queue)
1158 zfcp_erp_strategy(erp_action);
1162 * sleep as long as there is nothing to do, i.e.
1163 * no action in 'ready' queue to be processed and
1164 * thread is not to be killed
1166 down_interruptible(&adapter->erp_ready_sem);
1167 debug_text_event(adapter->erp_dbf, 5, "a_th_woken");
1170 atomic_clear_mask(ZFCP_STATUS_ADAPTER_ERP_THREAD_UP, &adapter->status);
1171 debug_text_event(adapter->erp_dbf, 5, "a_th_stop");
1172 wake_up(&adapter->erp_thread_wqh);
1174 return 0;
1178 * function:
1180 * purpose: drives single error recovery action and schedules higher and
1181 * subordinate actions, if necessary
1183 * returns: ZFCP_ERP_CONTINUES - action continues (asynchronously)
1184 * ZFCP_ERP_SUCCEEDED - action finished successfully (deqd)
1185 * ZFCP_ERP_FAILED - action finished unsuccessfully (deqd)
1186 * ZFCP_ERP_EXIT - action finished (dequeued), offline
1187 * ZFCP_ERP_DISMISSED - action canceled (dequeued)
1189 static int
1190 zfcp_erp_strategy(struct zfcp_erp_action *erp_action)
1192 int retval = 0;
1193 struct zfcp_adapter *adapter = erp_action->adapter;
1194 struct zfcp_port *port = erp_action->port;
1195 struct zfcp_unit *unit = erp_action->unit;
1196 int action = erp_action->action;
1197 u32 status = erp_action->status;
1198 unsigned long flags;
1200 /* serialise dismissing, timing out, moving, enqueueing */
1201 read_lock_irqsave(&zfcp_data.config_lock, flags);
1202 write_lock(&adapter->erp_lock);
1204 /* dequeue dismissed action and leave, if required */
1205 retval = zfcp_erp_strategy_check_action(erp_action, retval);
1206 if (retval == ZFCP_ERP_DISMISSED) {
1207 debug_text_event(adapter->erp_dbf, 4, "a_st_dis1");
1208 goto unlock;
1212 * move action to 'running' queue before processing it
1213 * (to avoid a race condition regarding moving the
1214 * action to the 'running' queue and back)
1216 zfcp_erp_action_to_running(erp_action);
1219 * try to process action as far as possible,
1220 * no lock to allow for blocking operations (kmalloc, qdio, ...),
1221 * afterwards the lock is required again for the following reasons:
1222 * - dequeueing of finished action and enqueueing of
1223 * follow-up actions must be atomic so that any other
1224 * reopen-routine does not believe there is nothing to do
1225 * and that it is safe to enqueue something else,
1226 * - we want to force any control thread which is dismissing
1227 * actions to finish this before we decide about
1228 * necessary steps to be taken here further
1230 write_unlock(&adapter->erp_lock);
1231 read_unlock_irqrestore(&zfcp_data.config_lock, flags);
1232 retval = zfcp_erp_strategy_do_action(erp_action);
1233 read_lock_irqsave(&zfcp_data.config_lock, flags);
1234 write_lock(&adapter->erp_lock);
1237 * check for dismissed status again to avoid follow-up actions,
1238 * failing of targets and so on for dismissed actions
1240 retval = zfcp_erp_strategy_check_action(erp_action, retval);
1242 switch (retval) {
1243 case ZFCP_ERP_DISMISSED:
1244 /* leave since this action has ridden to its ancestors */
1245 debug_text_event(adapter->erp_dbf, 6, "a_st_dis2");
1246 goto unlock;
1247 case ZFCP_ERP_NOMEM:
1248 /* no memory to continue immediately, let it sleep */
1249 if (!(erp_action->status & ZFCP_STATUS_ERP_LOWMEM)) {
1250 ++adapter->erp_low_mem_count;
1251 erp_action->status |= ZFCP_STATUS_ERP_LOWMEM;
1253 /* This condition is true if there is no memory available
1254 for any erp_action on this adapter. This implies that there
1255 are no elements in the memory pool(s) left for erp_actions.
1256 This might happen if an erp_action that used a memory pool
1257 element was timed out.
1259 if (adapter->erp_total_count == adapter->erp_low_mem_count) {
1260 debug_text_event(adapter->erp_dbf, 3, "a_st_lowmem");
1261 ZFCP_LOG_NORMAL("error: no mempool elements available, "
1262 "restarting I/O on adapter %s "
1263 "to free mempool\n",
1264 zfcp_get_busid_by_adapter(adapter));
1265 zfcp_erp_adapter_reopen_internal(adapter, 0);
1266 } else {
1267 debug_text_event(adapter->erp_dbf, 2, "a_st_memw");
1268 retval = zfcp_erp_strategy_memwait(erp_action);
1270 goto unlock;
1271 case ZFCP_ERP_CONTINUES:
1272 /* leave since this action runs asynchronously */
1273 debug_text_event(adapter->erp_dbf, 6, "a_st_cont");
1274 if (erp_action->status & ZFCP_STATUS_ERP_LOWMEM) {
1275 --adapter->erp_low_mem_count;
1276 erp_action->status &= ~ZFCP_STATUS_ERP_LOWMEM;
1278 goto unlock;
1280 /* ok, finished action (whatever its result is) */
1282 /* check for unrecoverable targets */
1283 retval = zfcp_erp_strategy_check_target(erp_action, retval);
1285 /* action must be dequeued (here to allow for further ones) */
1286 zfcp_erp_action_dequeue(erp_action);
1289 * put this target through the erp mill again if someone has
1290 * requested to change the status of a target being online
1291 * to offline or the other way around
1292 * (old retval is preserved if nothing has to be done here)
1294 retval = zfcp_erp_strategy_statechange(action, status, adapter,
1295 port, unit, retval);
1298 * leave if target is in permanent error state or if
1299 * action is repeated in order to process state change
1301 if (retval == ZFCP_ERP_EXIT) {
1302 debug_text_event(adapter->erp_dbf, 2, "a_st_exit");
1303 goto unlock;
1306 /* trigger follow up actions */
1307 zfcp_erp_strategy_followup_actions(action, adapter, port, unit, retval);
1309 unlock:
1310 write_unlock(&adapter->erp_lock);
1311 read_unlock_irqrestore(&zfcp_data.config_lock, flags);
1313 if (retval != ZFCP_ERP_CONTINUES)
1314 zfcp_erp_action_cleanup(action, adapter, port, unit, retval);
1317 * a few tasks remain when the erp queues are empty
1318 * (don't do that if the last action evaluated was dismissed
1319 * since this clearly indicates that there is more to come) :
1320 * - close the name server port if it is open yet
1321 * (enqueues another [probably] final action)
1322 * - otherwise, wake up whoever wants to be woken when we are
1323 * done with erp
1325 if (retval != ZFCP_ERP_DISMISSED)
1326 zfcp_erp_strategy_check_queues(adapter);
1328 debug_text_event(adapter->erp_dbf, 6, "a_st_done");
1330 return retval;
1334 * function:
1336 * purpose:
1338 * returns: ZFCP_ERP_DISMISSED - if action has been dismissed
1339 * retval - otherwise
1341 static int
1342 zfcp_erp_strategy_check_action(struct zfcp_erp_action *erp_action, int retval)
1344 struct zfcp_adapter *adapter = erp_action->adapter;
1346 zfcp_erp_strategy_check_fsfreq(erp_action);
1348 debug_event(adapter->erp_dbf, 5, &erp_action->action, sizeof (int));
1349 if (erp_action->status & ZFCP_STATUS_ERP_DISMISSED) {
1350 debug_text_event(adapter->erp_dbf, 3, "a_stcd_dis");
1351 zfcp_erp_action_dequeue(erp_action);
1352 retval = ZFCP_ERP_DISMISSED;
1353 } else
1354 debug_text_event(adapter->erp_dbf, 5, "a_stcd_nodis");
1356 return retval;
1360 * function:
1362 * purpose:
1364 * returns:
1366 static int
1367 zfcp_erp_strategy_do_action(struct zfcp_erp_action *erp_action)
1369 int retval = ZFCP_ERP_FAILED;
1370 struct zfcp_adapter *adapter = erp_action->adapter;
1373 * try to execute/continue action as far as possible,
1374 * note: no lock in subsequent strategy routines
1375 * (this allows these routine to call schedule, e.g.
1376 * kmalloc with such flags or qdio_initialize & friends)
1377 * Note: in case of timeout, the seperate strategies will fail
1378 * anyhow. No need for a special action. Even worse, a nameserver
1379 * failure would not wake up waiting ports without the call.
1381 switch (erp_action->action) {
1383 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
1384 retval = zfcp_erp_adapter_strategy(erp_action);
1385 break;
1387 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
1388 retval = zfcp_erp_port_forced_strategy(erp_action);
1389 break;
1391 case ZFCP_ERP_ACTION_REOPEN_PORT:
1392 retval = zfcp_erp_port_strategy(erp_action);
1393 break;
1395 case ZFCP_ERP_ACTION_REOPEN_UNIT:
1396 retval = zfcp_erp_unit_strategy(erp_action);
1397 break;
1399 default:
1400 debug_text_exception(adapter->erp_dbf, 1, "a_stda_bug");
1401 debug_event(adapter->erp_dbf, 1, &erp_action->action,
1402 sizeof (int));
1403 ZFCP_LOG_NORMAL("bug: unknown erp action requested on "
1404 "adapter %s (action=%d)\n",
1405 zfcp_get_busid_by_adapter(erp_action->adapter),
1406 erp_action->action);
1409 return retval;
1413 * function:
1415 * purpose: triggers retry of this action after a certain amount of time
1416 * by means of timer provided by erp_action
1418 * returns: ZFCP_ERP_CONTINUES - erp_action sleeps in erp running queue
1420 static int
1421 zfcp_erp_strategy_memwait(struct zfcp_erp_action *erp_action)
1423 int retval = ZFCP_ERP_CONTINUES;
1424 struct zfcp_adapter *adapter = erp_action->adapter;
1426 debug_text_event(adapter->erp_dbf, 6, "a_mwinit");
1427 debug_event(adapter->erp_dbf, 6, &erp_action->action, sizeof (int));
1428 init_timer(&erp_action->timer);
1429 erp_action->timer.function = zfcp_erp_memwait_handler;
1430 erp_action->timer.data = (unsigned long) erp_action;
1431 erp_action->timer.expires = jiffies + ZFCP_ERP_MEMWAIT_TIMEOUT;
1432 add_timer(&erp_action->timer);
1434 return retval;
1438 * function: zfcp_erp_adapter_failed
1440 * purpose: sets the adapter and all underlying devices to ERP_FAILED
1443 void
1444 zfcp_erp_adapter_failed(struct zfcp_adapter *adapter)
1446 zfcp_erp_modify_adapter_status(adapter,
1447 ZFCP_STATUS_COMMON_ERP_FAILED, ZFCP_SET);
1448 ZFCP_LOG_NORMAL("adapter erp failed on adapter %s\n",
1449 zfcp_get_busid_by_adapter(adapter));
1450 debug_text_event(adapter->erp_dbf, 2, "a_afail");
1454 * function: zfcp_erp_port_failed
1456 * purpose: sets the port and all underlying devices to ERP_FAILED
1459 void
1460 zfcp_erp_port_failed(struct zfcp_port *port)
1462 zfcp_erp_modify_port_status(port,
1463 ZFCP_STATUS_COMMON_ERP_FAILED, ZFCP_SET);
1465 if (atomic_test_mask(ZFCP_STATUS_PORT_WKA, &port->status))
1466 ZFCP_LOG_NORMAL("port erp failed (adapter %s, "
1467 "port d_id=0x%08x)\n",
1468 zfcp_get_busid_by_port(port), port->d_id);
1469 else
1470 ZFCP_LOG_NORMAL("port erp failed (adapter %s, wwpn=0x%016Lx)\n",
1471 zfcp_get_busid_by_port(port), port->wwpn);
1473 debug_text_event(port->adapter->erp_dbf, 2, "p_pfail");
1474 debug_event(port->adapter->erp_dbf, 2, &port->wwpn, sizeof (wwn_t));
1478 * function: zfcp_erp_unit_failed
1480 * purpose: sets the unit to ERP_FAILED
1483 void
1484 zfcp_erp_unit_failed(struct zfcp_unit *unit)
1486 zfcp_erp_modify_unit_status(unit,
1487 ZFCP_STATUS_COMMON_ERP_FAILED, ZFCP_SET);
1489 ZFCP_LOG_NORMAL("unit erp failed on unit 0x%016Lx on port 0x%016Lx "
1490 " on adapter %s\n", unit->fcp_lun,
1491 unit->port->wwpn, zfcp_get_busid_by_unit(unit));
1492 debug_text_event(unit->port->adapter->erp_dbf, 2, "u_ufail");
1493 debug_event(unit->port->adapter->erp_dbf, 2,
1494 &unit->fcp_lun, sizeof (fcp_lun_t));
1498 * function: zfcp_erp_strategy_check_target
1500 * purpose: increments the erp action count on the device currently in
1501 * recovery if the action failed or resets the count in case of
1502 * success. If a maximum count is exceeded the device is marked
1503 * as ERP_FAILED.
1504 * The 'blocked' state of a target which has been recovered
1505 * successfully is reset.
1507 * returns: ZFCP_ERP_CONTINUES - action continues (not considered)
1508 * ZFCP_ERP_SUCCEEDED - action finished successfully
1509 * ZFCP_ERP_EXIT - action failed and will not continue
1511 static int
1512 zfcp_erp_strategy_check_target(struct zfcp_erp_action *erp_action, int result)
1514 struct zfcp_adapter *adapter = erp_action->adapter;
1515 struct zfcp_port *port = erp_action->port;
1516 struct zfcp_unit *unit = erp_action->unit;
1518 debug_text_event(adapter->erp_dbf, 5, "a_stct_norm");
1519 debug_event(adapter->erp_dbf, 5, &erp_action->action, sizeof (int));
1520 debug_event(adapter->erp_dbf, 5, &result, sizeof (int));
1522 switch (erp_action->action) {
1524 case ZFCP_ERP_ACTION_REOPEN_UNIT:
1525 result = zfcp_erp_strategy_check_unit(unit, result);
1526 break;
1528 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
1529 case ZFCP_ERP_ACTION_REOPEN_PORT:
1530 result = zfcp_erp_strategy_check_port(port, result);
1531 break;
1533 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
1534 result = zfcp_erp_strategy_check_adapter(adapter, result);
1535 break;
1538 return result;
1542 * function:
1544 * purpose:
1546 * returns:
1548 static int
1549 zfcp_erp_strategy_statechange(int action,
1550 u32 status,
1551 struct zfcp_adapter *adapter,
1552 struct zfcp_port *port,
1553 struct zfcp_unit *unit, int retval)
1555 debug_text_event(adapter->erp_dbf, 3, "a_stsc");
1556 debug_event(adapter->erp_dbf, 3, &action, sizeof (int));
1558 switch (action) {
1560 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
1561 if (zfcp_erp_strategy_statechange_detected(&adapter->status,
1562 status)) {
1563 zfcp_erp_adapter_reopen_internal(adapter, ZFCP_STATUS_COMMON_ERP_FAILED);
1564 retval = ZFCP_ERP_EXIT;
1566 break;
1568 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
1569 case ZFCP_ERP_ACTION_REOPEN_PORT:
1570 if (zfcp_erp_strategy_statechange_detected(&port->status,
1571 status)) {
1572 zfcp_erp_port_reopen_internal(port, ZFCP_STATUS_COMMON_ERP_FAILED);
1573 retval = ZFCP_ERP_EXIT;
1575 break;
1577 case ZFCP_ERP_ACTION_REOPEN_UNIT:
1578 if (zfcp_erp_strategy_statechange_detected(&unit->status,
1579 status)) {
1580 zfcp_erp_unit_reopen_internal(unit, ZFCP_STATUS_COMMON_ERP_FAILED);
1581 retval = ZFCP_ERP_EXIT;
1583 break;
1586 return retval;
1590 * function:
1592 * purpose:
1594 * returns:
1596 static inline int
1597 zfcp_erp_strategy_statechange_detected(atomic_t * target_status, u32 erp_status)
1599 return
1600 /* take it online */
1601 (atomic_test_mask(ZFCP_STATUS_COMMON_RUNNING, target_status) &&
1602 (ZFCP_STATUS_ERP_CLOSE_ONLY & erp_status)) ||
1603 /* take it offline */
1604 (!atomic_test_mask(ZFCP_STATUS_COMMON_RUNNING, target_status) &&
1605 !(ZFCP_STATUS_ERP_CLOSE_ONLY & erp_status));
1609 * function:
1611 * purpose:
1613 * returns:
1615 static int
1616 zfcp_erp_strategy_check_unit(struct zfcp_unit *unit, int result)
1618 debug_text_event(unit->port->adapter->erp_dbf, 5, "u_stct");
1619 debug_event(unit->port->adapter->erp_dbf, 5, &unit->fcp_lun,
1620 sizeof (fcp_lun_t));
1622 switch (result) {
1623 case ZFCP_ERP_SUCCEEDED :
1624 atomic_set(&unit->erp_counter, 0);
1625 zfcp_erp_unit_unblock(unit);
1626 break;
1627 case ZFCP_ERP_FAILED :
1628 atomic_inc(&unit->erp_counter);
1629 if (atomic_read(&unit->erp_counter) > ZFCP_MAX_ERPS)
1630 zfcp_erp_unit_failed(unit);
1631 break;
1632 case ZFCP_ERP_EXIT :
1633 /* nothing */
1634 break;
1637 if (atomic_test_mask(ZFCP_STATUS_COMMON_ERP_FAILED, &unit->status)) {
1638 zfcp_erp_unit_block(unit, 0); /* for ZFCP_ERP_SUCCEEDED */
1639 result = ZFCP_ERP_EXIT;
1642 return result;
1646 * function:
1648 * purpose:
1650 * returns:
1652 static int
1653 zfcp_erp_strategy_check_port(struct zfcp_port *port, int result)
1655 debug_text_event(port->adapter->erp_dbf, 5, "p_stct");
1656 debug_event(port->adapter->erp_dbf, 5, &port->wwpn, sizeof (wwn_t));
1658 switch (result) {
1659 case ZFCP_ERP_SUCCEEDED :
1660 atomic_set(&port->erp_counter, 0);
1661 zfcp_erp_port_unblock(port);
1662 break;
1663 case ZFCP_ERP_FAILED :
1664 atomic_inc(&port->erp_counter);
1665 if (atomic_read(&port->erp_counter) > ZFCP_MAX_ERPS)
1666 zfcp_erp_port_failed(port);
1667 break;
1668 case ZFCP_ERP_EXIT :
1669 /* nothing */
1670 break;
1673 if (atomic_test_mask(ZFCP_STATUS_COMMON_ERP_FAILED, &port->status)) {
1674 zfcp_erp_port_block(port, 0); /* for ZFCP_ERP_SUCCEEDED */
1675 result = ZFCP_ERP_EXIT;
1678 return result;
1682 * function:
1684 * purpose:
1686 * returns:
1688 static int
1689 zfcp_erp_strategy_check_adapter(struct zfcp_adapter *adapter, int result)
1691 debug_text_event(adapter->erp_dbf, 5, "a_stct");
1693 switch (result) {
1694 case ZFCP_ERP_SUCCEEDED :
1695 atomic_set(&adapter->erp_counter, 0);
1696 zfcp_erp_adapter_unblock(adapter);
1697 break;
1698 case ZFCP_ERP_FAILED :
1699 atomic_inc(&adapter->erp_counter);
1700 if (atomic_read(&adapter->erp_counter) > ZFCP_MAX_ERPS)
1701 zfcp_erp_adapter_failed(adapter);
1702 break;
1703 case ZFCP_ERP_EXIT :
1704 /* nothing */
1705 break;
1708 if (atomic_test_mask(ZFCP_STATUS_COMMON_ERP_FAILED, &adapter->status)) {
1709 zfcp_erp_adapter_block(adapter, 0); /* for ZFCP_ERP_SUCCEEDED */
1710 result = ZFCP_ERP_EXIT;
1713 return result;
1717 * function:
1719 * purpose: remaining things in good cases,
1720 * escalation in bad cases
1722 * returns:
1724 static int
1725 zfcp_erp_strategy_followup_actions(int action,
1726 struct zfcp_adapter *adapter,
1727 struct zfcp_port *port,
1728 struct zfcp_unit *unit, int status)
1730 debug_text_event(adapter->erp_dbf, 5, "a_stfol");
1731 debug_event(adapter->erp_dbf, 5, &action, sizeof (int));
1733 /* initiate follow-up actions depending on success of finished action */
1734 switch (action) {
1736 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
1737 if (status == ZFCP_ERP_SUCCEEDED)
1738 zfcp_erp_port_reopen_all_internal(adapter, 0);
1739 else
1740 zfcp_erp_adapter_reopen_internal(adapter, 0);
1741 break;
1743 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
1744 if (status == ZFCP_ERP_SUCCEEDED)
1745 zfcp_erp_port_reopen_internal(port, 0);
1746 else
1747 zfcp_erp_adapter_reopen_internal(adapter, 0);
1748 break;
1750 case ZFCP_ERP_ACTION_REOPEN_PORT:
1751 if (status == ZFCP_ERP_SUCCEEDED)
1752 zfcp_erp_unit_reopen_all_internal(port, 0);
1753 else
1754 zfcp_erp_port_forced_reopen_internal(port, 0);
1755 break;
1757 case ZFCP_ERP_ACTION_REOPEN_UNIT:
1758 if (status == ZFCP_ERP_SUCCEEDED) ; /* no further action */
1759 else
1760 zfcp_erp_port_reopen_internal(unit->port, 0);
1761 break;
1764 return 0;
1768 * function:
1770 * purpose:
1772 * returns:
1774 static int
1775 zfcp_erp_strategy_check_queues(struct zfcp_adapter *adapter)
1777 unsigned long flags;
1779 read_lock_irqsave(&zfcp_data.config_lock, flags);
1780 read_lock(&adapter->erp_lock);
1781 if (list_empty(&adapter->erp_ready_head) &&
1782 list_empty(&adapter->erp_running_head)) {
1783 debug_text_event(adapter->erp_dbf, 4, "a_cq_wake");
1784 atomic_clear_mask(ZFCP_STATUS_ADAPTER_ERP_PENDING,
1785 &adapter->status);
1786 wake_up(&adapter->erp_done_wqh);
1787 } else
1788 debug_text_event(adapter->erp_dbf, 5, "a_cq_notempty");
1789 read_unlock(&adapter->erp_lock);
1790 read_unlock_irqrestore(&zfcp_data.config_lock, flags);
1792 return 0;
1796 * zfcp_erp_wait - wait for completion of error recovery on an adapter
1797 * @adapter: adapter for which to wait for completion of its error recovery
1798 * Return: 0
1801 zfcp_erp_wait(struct zfcp_adapter *adapter)
1803 int retval = 0;
1805 wait_event(adapter->erp_done_wqh,
1806 !atomic_test_mask(ZFCP_STATUS_ADAPTER_ERP_PENDING,
1807 &adapter->status));
1809 return retval;
1813 * function: zfcp_erp_modify_adapter_status
1815 * purpose:
1818 void
1819 zfcp_erp_modify_adapter_status(struct zfcp_adapter *adapter,
1820 u32 mask, int set_or_clear)
1822 struct zfcp_port *port;
1823 u32 common_mask = mask & ZFCP_COMMON_FLAGS;
1825 if (set_or_clear == ZFCP_SET) {
1826 atomic_set_mask(mask, &adapter->status);
1827 debug_text_event(adapter->erp_dbf, 3, "a_mod_as_s");
1828 } else {
1829 atomic_clear_mask(mask, &adapter->status);
1830 if (mask & ZFCP_STATUS_COMMON_ERP_FAILED)
1831 atomic_set(&adapter->erp_counter, 0);
1832 debug_text_event(adapter->erp_dbf, 3, "a_mod_as_c");
1834 debug_event(adapter->erp_dbf, 3, &mask, sizeof (u32));
1836 /* Deal with all underlying devices, only pass common_mask */
1837 if (common_mask)
1838 list_for_each_entry(port, &adapter->port_list_head, list)
1839 zfcp_erp_modify_port_status(port, common_mask,
1840 set_or_clear);
1844 * function: zfcp_erp_modify_port_status
1846 * purpose: sets the port and all underlying devices to ERP_FAILED
1849 void
1850 zfcp_erp_modify_port_status(struct zfcp_port *port, u32 mask, int set_or_clear)
1852 struct zfcp_unit *unit;
1853 u32 common_mask = mask & ZFCP_COMMON_FLAGS;
1855 if (set_or_clear == ZFCP_SET) {
1856 atomic_set_mask(mask, &port->status);
1857 debug_text_event(port->adapter->erp_dbf, 3, "p_mod_ps_s");
1858 } else {
1859 atomic_clear_mask(mask, &port->status);
1860 if (mask & ZFCP_STATUS_COMMON_ERP_FAILED)
1861 atomic_set(&port->erp_counter, 0);
1862 debug_text_event(port->adapter->erp_dbf, 3, "p_mod_ps_c");
1864 debug_event(port->adapter->erp_dbf, 3, &port->wwpn, sizeof (wwn_t));
1865 debug_event(port->adapter->erp_dbf, 3, &mask, sizeof (u32));
1867 /* Modify status of all underlying devices, only pass common mask */
1868 if (common_mask)
1869 list_for_each_entry(unit, &port->unit_list_head, list)
1870 zfcp_erp_modify_unit_status(unit, common_mask,
1871 set_or_clear);
1875 * function: zfcp_erp_modify_unit_status
1877 * purpose: sets the unit to ERP_FAILED
1880 void
1881 zfcp_erp_modify_unit_status(struct zfcp_unit *unit, u32 mask, int set_or_clear)
1883 if (set_or_clear == ZFCP_SET) {
1884 atomic_set_mask(mask, &unit->status);
1885 debug_text_event(unit->port->adapter->erp_dbf, 3, "u_mod_us_s");
1886 } else {
1887 atomic_clear_mask(mask, &unit->status);
1888 if (mask & ZFCP_STATUS_COMMON_ERP_FAILED) {
1889 atomic_set(&unit->erp_counter, 0);
1891 debug_text_event(unit->port->adapter->erp_dbf, 3, "u_mod_us_c");
1893 debug_event(unit->port->adapter->erp_dbf, 3, &unit->fcp_lun,
1894 sizeof (fcp_lun_t));
1895 debug_event(unit->port->adapter->erp_dbf, 3, &mask, sizeof (u32));
1899 * function:
1901 * purpose: Wrappper for zfcp_erp_port_reopen_all_internal
1902 * used to ensure the correct locking
1904 * returns: 0 - initiated action succesfully
1905 * <0 - failed to initiate action
1908 zfcp_erp_port_reopen_all(struct zfcp_adapter *adapter, int clear_mask)
1910 int retval;
1911 unsigned long flags;
1913 read_lock_irqsave(&zfcp_data.config_lock, flags);
1914 write_lock(&adapter->erp_lock);
1915 retval = zfcp_erp_port_reopen_all_internal(adapter, clear_mask);
1916 write_unlock(&adapter->erp_lock);
1917 read_unlock_irqrestore(&zfcp_data.config_lock, flags);
1919 return retval;
1923 * function:
1925 * purpose:
1927 * returns: FIXME
1929 static int
1930 zfcp_erp_port_reopen_all_internal(struct zfcp_adapter *adapter, int clear_mask)
1932 int retval = 0;
1933 struct zfcp_port *port;
1935 list_for_each_entry(port, &adapter->port_list_head, list)
1936 if (!atomic_test_mask(ZFCP_STATUS_PORT_WKA, &port->status))
1937 zfcp_erp_port_reopen_internal(port, clear_mask);
1939 return retval;
1943 * function:
1945 * purpose:
1947 * returns: FIXME
1949 static int
1950 zfcp_erp_unit_reopen_all_internal(struct zfcp_port *port, int clear_mask)
1952 int retval = 0;
1953 struct zfcp_unit *unit;
1955 list_for_each_entry(unit, &port->unit_list_head, list)
1956 zfcp_erp_unit_reopen_internal(unit, clear_mask);
1958 return retval;
1962 * function:
1964 * purpose: this routine executes the 'Reopen Adapter' action
1965 * (the entire action is processed synchronously, since
1966 * there are no actions which might be run concurrently
1967 * per definition)
1969 * returns: ZFCP_ERP_SUCCEEDED - action finished successfully
1970 * ZFCP_ERP_FAILED - action finished unsuccessfully
1972 static int
1973 zfcp_erp_adapter_strategy(struct zfcp_erp_action *erp_action)
1975 int retval;
1976 struct zfcp_adapter *adapter = erp_action->adapter;
1978 retval = zfcp_erp_adapter_strategy_close(erp_action);
1979 if (erp_action->status & ZFCP_STATUS_ERP_CLOSE_ONLY)
1980 retval = ZFCP_ERP_EXIT;
1981 else
1982 retval = zfcp_erp_adapter_strategy_open(erp_action);
1984 debug_text_event(adapter->erp_dbf, 3, "a_ast/ret");
1985 debug_event(adapter->erp_dbf, 3, &erp_action->action, sizeof (int));
1986 debug_event(adapter->erp_dbf, 3, &retval, sizeof (int));
1988 if (retval == ZFCP_ERP_FAILED) {
1989 ZFCP_LOG_INFO("Waiting to allow the adapter %s "
1990 "to recover itself\n",
1991 zfcp_get_busid_by_adapter(adapter));
1992 msleep(jiffies_to_msecs(ZFCP_TYPE2_RECOVERY_TIME));
1995 return retval;
1999 * function:
2001 * purpose:
2003 * returns: ZFCP_ERP_SUCCEEDED - action finished successfully
2004 * ZFCP_ERP_FAILED - action finished unsuccessfully
2006 static int
2007 zfcp_erp_adapter_strategy_close(struct zfcp_erp_action *erp_action)
2009 int retval;
2011 atomic_set_mask(ZFCP_STATUS_COMMON_CLOSING,
2012 &erp_action->adapter->status);
2013 retval = zfcp_erp_adapter_strategy_generic(erp_action, 1);
2014 atomic_clear_mask(ZFCP_STATUS_COMMON_CLOSING,
2015 &erp_action->adapter->status);
2017 return retval;
2021 * function:
2023 * purpose:
2025 * returns: ZFCP_ERP_SUCCEEDED - action finished successfully
2026 * ZFCP_ERP_FAILED - action finished unsuccessfully
2028 static int
2029 zfcp_erp_adapter_strategy_open(struct zfcp_erp_action *erp_action)
2031 int retval;
2033 atomic_set_mask(ZFCP_STATUS_COMMON_OPENING,
2034 &erp_action->adapter->status);
2035 retval = zfcp_erp_adapter_strategy_generic(erp_action, 0);
2036 atomic_clear_mask(ZFCP_STATUS_COMMON_OPENING,
2037 &erp_action->adapter->status);
2039 return retval;
2043 * function: zfcp_register_adapter
2045 * purpose: allocate the irq associated with this devno and register
2046 * the FSF adapter with the SCSI stack
2048 * returns:
2050 static int
2051 zfcp_erp_adapter_strategy_generic(struct zfcp_erp_action *erp_action, int close)
2053 int retval = ZFCP_ERP_SUCCEEDED;
2055 if (close)
2056 goto close_only;
2058 retval = zfcp_erp_adapter_strategy_open_qdio(erp_action);
2059 if (retval != ZFCP_ERP_SUCCEEDED)
2060 goto failed_qdio;
2062 retval = zfcp_erp_adapter_strategy_open_fsf(erp_action);
2063 if (retval != ZFCP_ERP_SUCCEEDED)
2064 goto failed_openfcp;
2066 atomic_set_mask(ZFCP_STATUS_COMMON_OPEN, &erp_action->adapter->status);
2067 goto out;
2069 close_only:
2070 atomic_clear_mask(ZFCP_STATUS_COMMON_OPEN,
2071 &erp_action->adapter->status);
2073 failed_openfcp:
2074 zfcp_erp_adapter_strategy_close_qdio(erp_action);
2075 zfcp_erp_adapter_strategy_close_fsf(erp_action);
2076 failed_qdio:
2077 out:
2078 return retval;
2082 * function: zfcp_qdio_init
2084 * purpose: setup QDIO operation for specified adapter
2086 * returns: 0 - successful setup
2087 * !0 - failed setup
2090 zfcp_erp_adapter_strategy_open_qdio(struct zfcp_erp_action *erp_action)
2092 int retval;
2093 int i;
2094 volatile struct qdio_buffer_element *sbale;
2095 struct zfcp_adapter *adapter = erp_action->adapter;
2097 if (atomic_test_mask(ZFCP_STATUS_ADAPTER_QDIOUP, &adapter->status)) {
2098 ZFCP_LOG_NORMAL("bug: second attempt to set up QDIO on "
2099 "adapter %s\n",
2100 zfcp_get_busid_by_adapter(adapter));
2101 goto failed_sanity;
2104 if (qdio_establish(&adapter->qdio_init_data) != 0) {
2105 ZFCP_LOG_INFO("error: establishment of QDIO queues failed "
2106 "on adapter %s\n",
2107 zfcp_get_busid_by_adapter(adapter));
2108 goto failed_qdio_establish;
2110 debug_text_event(adapter->erp_dbf, 3, "qdio_est");
2112 if (qdio_activate(adapter->ccw_device, 0) != 0) {
2113 ZFCP_LOG_INFO("error: activation of QDIO queues failed "
2114 "on adapter %s\n",
2115 zfcp_get_busid_by_adapter(adapter));
2116 goto failed_qdio_activate;
2118 debug_text_event(adapter->erp_dbf, 3, "qdio_act");
2121 * put buffers into response queue,
2123 for (i = 0; i < QDIO_MAX_BUFFERS_PER_Q; i++) {
2124 sbale = &(adapter->response_queue.buffer[i]->element[0]);
2125 sbale->length = 0;
2126 sbale->flags = SBAL_FLAGS_LAST_ENTRY;
2127 sbale->addr = 0;
2130 ZFCP_LOG_TRACE("calling do_QDIO on adapter %s (flags=0x%x, "
2131 "queue_no=%i, index_in_queue=%i, count=%i)\n",
2132 zfcp_get_busid_by_adapter(adapter),
2133 QDIO_FLAG_SYNC_INPUT, 0, 0, QDIO_MAX_BUFFERS_PER_Q);
2135 retval = do_QDIO(adapter->ccw_device,
2136 QDIO_FLAG_SYNC_INPUT,
2137 0, 0, QDIO_MAX_BUFFERS_PER_Q, NULL);
2139 if (retval) {
2140 ZFCP_LOG_NORMAL("bug: setup of QDIO failed (retval=%d)\n",
2141 retval);
2142 goto failed_do_qdio;
2143 } else {
2144 adapter->response_queue.free_index = 0;
2145 atomic_set(&adapter->response_queue.free_count, 0);
2146 ZFCP_LOG_DEBUG("%i buffers successfully enqueued to "
2147 "response queue\n", QDIO_MAX_BUFFERS_PER_Q);
2149 /* set index of first avalable SBALS / number of available SBALS */
2150 adapter->request_queue.free_index = 0;
2151 atomic_set(&adapter->request_queue.free_count, QDIO_MAX_BUFFERS_PER_Q);
2152 adapter->request_queue.distance_from_int = 0;
2154 /* initialize waitqueue used to wait for free SBALs in requests queue */
2155 init_waitqueue_head(&adapter->request_wq);
2157 /* ok, we did it - skip all cleanups for different failures */
2158 atomic_set_mask(ZFCP_STATUS_ADAPTER_QDIOUP, &adapter->status);
2159 retval = ZFCP_ERP_SUCCEEDED;
2160 goto out;
2162 failed_do_qdio:
2163 /* NOP */
2165 failed_qdio_activate:
2166 debug_text_event(adapter->erp_dbf, 3, "qdio_down1a");
2167 while (qdio_shutdown(adapter->ccw_device,
2168 QDIO_FLAG_CLEANUP_USING_CLEAR) == -EINPROGRESS)
2169 msleep(1000);
2170 debug_text_event(adapter->erp_dbf, 3, "qdio_down1b");
2172 failed_qdio_establish:
2173 failed_sanity:
2174 retval = ZFCP_ERP_FAILED;
2176 out:
2177 return retval;
2181 * function: zfcp_qdio_cleanup
2183 * purpose: cleans up QDIO operation for the specified adapter
2185 * returns: 0 - successful cleanup
2186 * !0 - failed cleanup
2189 zfcp_erp_adapter_strategy_close_qdio(struct zfcp_erp_action *erp_action)
2191 int retval = ZFCP_ERP_SUCCEEDED;
2192 int first_used;
2193 int used_count;
2194 struct zfcp_adapter *adapter = erp_action->adapter;
2196 if (!atomic_test_mask(ZFCP_STATUS_ADAPTER_QDIOUP, &adapter->status)) {
2197 ZFCP_LOG_DEBUG("error: attempt to shut down inactive QDIO "
2198 "queues on adapter %s\n",
2199 zfcp_get_busid_by_adapter(adapter));
2200 retval = ZFCP_ERP_FAILED;
2201 goto out;
2205 * Get queue_lock and clear QDIOUP flag. Thus it's guaranteed that
2206 * do_QDIO won't be called while qdio_shutdown is in progress.
2209 write_lock_irq(&adapter->request_queue.queue_lock);
2210 atomic_clear_mask(ZFCP_STATUS_ADAPTER_QDIOUP, &adapter->status);
2211 write_unlock_irq(&adapter->request_queue.queue_lock);
2213 debug_text_event(adapter->erp_dbf, 3, "qdio_down2a");
2214 while (qdio_shutdown(adapter->ccw_device,
2215 QDIO_FLAG_CLEANUP_USING_CLEAR) == -EINPROGRESS)
2216 msleep(1000);
2217 debug_text_event(adapter->erp_dbf, 3, "qdio_down2b");
2220 * First we had to stop QDIO operation.
2221 * Now it is safe to take the following actions.
2224 /* Cleanup only necessary when there are unacknowledged buffers */
2225 if (atomic_read(&adapter->request_queue.free_count)
2226 < QDIO_MAX_BUFFERS_PER_Q) {
2227 first_used = (adapter->request_queue.free_index +
2228 atomic_read(&adapter->request_queue.free_count))
2229 % QDIO_MAX_BUFFERS_PER_Q;
2230 used_count = QDIO_MAX_BUFFERS_PER_Q -
2231 atomic_read(&adapter->request_queue.free_count);
2232 zfcp_qdio_zero_sbals(adapter->request_queue.buffer,
2233 first_used, used_count);
2235 adapter->response_queue.free_index = 0;
2236 atomic_set(&adapter->response_queue.free_count, 0);
2237 adapter->request_queue.free_index = 0;
2238 atomic_set(&adapter->request_queue.free_count, 0);
2239 adapter->request_queue.distance_from_int = 0;
2240 out:
2241 return retval;
2244 static int
2245 zfcp_erp_adapter_strategy_open_fsf(struct zfcp_erp_action *erp_action)
2247 int retval;
2249 retval = zfcp_erp_adapter_strategy_open_fsf_xconfig(erp_action);
2250 if (retval == ZFCP_ERP_FAILED)
2251 return ZFCP_ERP_FAILED;
2253 retval = zfcp_erp_adapter_strategy_open_fsf_xport(erp_action);
2254 if (retval == ZFCP_ERP_FAILED)
2255 return ZFCP_ERP_FAILED;
2257 return zfcp_erp_adapter_strategy_open_fsf_statusread(erp_action);
2260 static int
2261 zfcp_erp_adapter_strategy_open_fsf_xconfig(struct zfcp_erp_action *erp_action)
2263 int retval = ZFCP_ERP_SUCCEEDED;
2264 int retries;
2265 int sleep = ZFCP_EXCHANGE_CONFIG_DATA_FIRST_SLEEP;
2266 struct zfcp_adapter *adapter = erp_action->adapter;
2268 atomic_clear_mask(ZFCP_STATUS_ADAPTER_XCONFIG_OK, &adapter->status);
2270 for (retries = ZFCP_EXCHANGE_CONFIG_DATA_RETRIES; retries; retries--) {
2271 atomic_clear_mask(ZFCP_STATUS_ADAPTER_HOST_CON_INIT,
2272 &adapter->status);
2273 ZFCP_LOG_DEBUG("Doing exchange config data\n");
2274 write_lock(&adapter->erp_lock);
2275 zfcp_erp_action_to_running(erp_action);
2276 write_unlock(&adapter->erp_lock);
2277 zfcp_erp_timeout_init(erp_action);
2278 if (zfcp_fsf_exchange_config_data(erp_action)) {
2279 retval = ZFCP_ERP_FAILED;
2280 debug_text_event(adapter->erp_dbf, 5, "a_fstx_xf");
2281 ZFCP_LOG_INFO("error: initiation of exchange of "
2282 "configuration data failed for "
2283 "adapter %s\n",
2284 zfcp_get_busid_by_adapter(adapter));
2285 break;
2287 debug_text_event(adapter->erp_dbf, 6, "a_fstx_xok");
2288 ZFCP_LOG_DEBUG("Xchange underway\n");
2291 * Why this works:
2292 * Both the normal completion handler as well as the timeout
2293 * handler will do an 'up' when the 'exchange config data'
2294 * request completes or times out. Thus, the signal to go on
2295 * won't be lost utilizing this semaphore.
2296 * Furthermore, this 'adapter_reopen' action is
2297 * guaranteed to be the only action being there (highest action
2298 * which prevents other actions from being created).
2299 * Resulting from that, the wake signal recognized here
2300 * _must_ be the one belonging to the 'exchange config
2301 * data' request.
2303 down(&adapter->erp_ready_sem);
2304 if (erp_action->status & ZFCP_STATUS_ERP_TIMEDOUT) {
2305 ZFCP_LOG_INFO("error: exchange of configuration data "
2306 "for adapter %s timed out\n",
2307 zfcp_get_busid_by_adapter(adapter));
2308 break;
2311 if (!atomic_test_mask(ZFCP_STATUS_ADAPTER_HOST_CON_INIT,
2312 &adapter->status))
2313 break;
2315 ZFCP_LOG_DEBUG("host connection still initialising... "
2316 "waiting and retrying...\n");
2317 /* sleep a little bit before retry */
2318 msleep(jiffies_to_msecs(sleep));
2319 sleep *= 2;
2322 if (!atomic_test_mask(ZFCP_STATUS_ADAPTER_XCONFIG_OK,
2323 &adapter->status)) {
2324 ZFCP_LOG_INFO("error: exchange of configuration data for "
2325 "adapter %s failed\n",
2326 zfcp_get_busid_by_adapter(adapter));
2327 retval = ZFCP_ERP_FAILED;
2330 return retval;
2333 static int
2334 zfcp_erp_adapter_strategy_open_fsf_xport(struct zfcp_erp_action *erp_action)
2336 int ret;
2337 struct zfcp_adapter *adapter;
2339 adapter = erp_action->adapter;
2340 atomic_clear_mask(ZFCP_STATUS_ADAPTER_XPORT_OK, &adapter->status);
2342 write_lock(&adapter->erp_lock);
2343 zfcp_erp_action_to_running(erp_action);
2344 write_unlock(&adapter->erp_lock);
2346 zfcp_erp_timeout_init(erp_action);
2347 ret = zfcp_fsf_exchange_port_data(erp_action, adapter, NULL);
2348 if (ret == -EOPNOTSUPP) {
2349 debug_text_event(adapter->erp_dbf, 3, "a_xport_notsupp");
2350 return ZFCP_ERP_SUCCEEDED;
2351 } else if (ret) {
2352 debug_text_event(adapter->erp_dbf, 3, "a_xport_failed");
2353 return ZFCP_ERP_FAILED;
2355 debug_text_event(adapter->erp_dbf, 6, "a_xport_ok");
2357 ret = ZFCP_ERP_SUCCEEDED;
2358 down(&adapter->erp_ready_sem);
2359 if (erp_action->status & ZFCP_STATUS_ERP_TIMEDOUT) {
2360 ZFCP_LOG_INFO("error: exchange port data timed out (adapter "
2361 "%s)\n", zfcp_get_busid_by_adapter(adapter));
2362 ret = ZFCP_ERP_FAILED;
2364 if (!atomic_test_mask(ZFCP_STATUS_ADAPTER_XPORT_OK, &adapter->status)) {
2365 ZFCP_LOG_INFO("error: exchange port data failed (adapter "
2366 "%s\n", zfcp_get_busid_by_adapter(adapter));
2367 ret = ZFCP_ERP_FAILED;
2370 return ret;
2374 * function:
2376 * purpose:
2378 * returns:
2380 static int
2381 zfcp_erp_adapter_strategy_open_fsf_statusread(struct zfcp_erp_action
2382 *erp_action)
2384 int retval = ZFCP_ERP_SUCCEEDED;
2385 int temp_ret;
2386 struct zfcp_adapter *adapter = erp_action->adapter;
2387 int i;
2389 adapter->status_read_failed = 0;
2390 for (i = 0; i < ZFCP_STATUS_READS_RECOM; i++) {
2391 temp_ret = zfcp_fsf_status_read(adapter, ZFCP_WAIT_FOR_SBAL);
2392 if (temp_ret < 0) {
2393 ZFCP_LOG_INFO("error: set-up of unsolicited status "
2394 "notification failed on adapter %s\n",
2395 zfcp_get_busid_by_adapter(adapter));
2396 retval = ZFCP_ERP_FAILED;
2397 i--;
2398 break;
2402 return retval;
2406 * function: zfcp_fsf_cleanup
2408 * purpose: cleanup FSF operation for specified adapter
2410 * returns: 0 - FSF operation successfully cleaned up
2411 * !0 - failed to cleanup FSF operation for this adapter
2413 static int
2414 zfcp_erp_adapter_strategy_close_fsf(struct zfcp_erp_action *erp_action)
2416 int retval = ZFCP_ERP_SUCCEEDED;
2417 struct zfcp_adapter *adapter = erp_action->adapter;
2420 * wake waiting initiators of requests,
2421 * return SCSI commands (with error status),
2422 * clean up all requests (synchronously)
2424 zfcp_fsf_req_dismiss_all(adapter);
2425 /* reset FSF request sequence number */
2426 adapter->fsf_req_seq_no = 0;
2427 /* all ports and units are closed */
2428 zfcp_erp_modify_adapter_status(adapter,
2429 ZFCP_STATUS_COMMON_OPEN, ZFCP_CLEAR);
2431 return retval;
2435 * function:
2437 * purpose: this routine executes the 'Reopen Physical Port' action
2439 * returns: ZFCP_ERP_CONTINUES - action continues (asynchronously)
2440 * ZFCP_ERP_SUCCEEDED - action finished successfully
2441 * ZFCP_ERP_FAILED - action finished unsuccessfully
2443 static int
2444 zfcp_erp_port_forced_strategy(struct zfcp_erp_action *erp_action)
2446 int retval = ZFCP_ERP_FAILED;
2447 struct zfcp_port *port = erp_action->port;
2448 struct zfcp_adapter *adapter = erp_action->adapter;
2450 switch (erp_action->step) {
2453 * FIXME:
2454 * the ULP spec. begs for waiting for oustanding commands
2456 case ZFCP_ERP_STEP_UNINITIALIZED:
2457 zfcp_erp_port_strategy_clearstati(port);
2459 * it would be sufficient to test only the normal open flag
2460 * since the phys. open flag cannot be set if the normal
2461 * open flag is unset - however, this is for readabilty ...
2463 if (atomic_test_mask((ZFCP_STATUS_PORT_PHYS_OPEN |
2464 ZFCP_STATUS_COMMON_OPEN),
2465 &port->status)) {
2466 ZFCP_LOG_DEBUG("port 0x%016Lx is open -> trying "
2467 "close physical\n", port->wwpn);
2468 retval =
2469 zfcp_erp_port_forced_strategy_close(erp_action);
2470 } else
2471 retval = ZFCP_ERP_FAILED;
2472 break;
2474 case ZFCP_ERP_STEP_PHYS_PORT_CLOSING:
2475 if (atomic_test_mask(ZFCP_STATUS_PORT_PHYS_OPEN,
2476 &port->status)) {
2477 ZFCP_LOG_DEBUG("close physical failed for port "
2478 "0x%016Lx\n", port->wwpn);
2479 retval = ZFCP_ERP_FAILED;
2480 } else
2481 retval = ZFCP_ERP_SUCCEEDED;
2482 break;
2485 debug_text_event(adapter->erp_dbf, 3, "p_pfst/ret");
2486 debug_event(adapter->erp_dbf, 3, &port->wwpn, sizeof (wwn_t));
2487 debug_event(adapter->erp_dbf, 3, &erp_action->action, sizeof (int));
2488 debug_event(adapter->erp_dbf, 3, &retval, sizeof (int));
2490 return retval;
2494 * function:
2496 * purpose: this routine executes the 'Reopen Port' action
2498 * returns: ZFCP_ERP_CONTINUES - action continues (asynchronously)
2499 * ZFCP_ERP_SUCCEEDED - action finished successfully
2500 * ZFCP_ERP_FAILED - action finished unsuccessfully
2502 static int
2503 zfcp_erp_port_strategy(struct zfcp_erp_action *erp_action)
2505 int retval = ZFCP_ERP_FAILED;
2506 struct zfcp_port *port = erp_action->port;
2507 struct zfcp_adapter *adapter = erp_action->adapter;
2509 switch (erp_action->step) {
2512 * FIXME:
2513 * the ULP spec. begs for waiting for oustanding commands
2515 case ZFCP_ERP_STEP_UNINITIALIZED:
2516 zfcp_erp_port_strategy_clearstati(port);
2517 if (atomic_test_mask(ZFCP_STATUS_COMMON_OPEN, &port->status)) {
2518 ZFCP_LOG_DEBUG("port 0x%016Lx is open -> trying "
2519 "close\n", port->wwpn);
2520 retval = zfcp_erp_port_strategy_close(erp_action);
2521 goto out;
2522 } /* else it's already closed, open it */
2523 break;
2525 case ZFCP_ERP_STEP_PORT_CLOSING:
2526 if (atomic_test_mask(ZFCP_STATUS_COMMON_OPEN, &port->status)) {
2527 ZFCP_LOG_DEBUG("close failed for port 0x%016Lx\n",
2528 port->wwpn);
2529 retval = ZFCP_ERP_FAILED;
2530 goto out;
2531 } /* else it's closed now, open it */
2532 break;
2534 if (erp_action->status & ZFCP_STATUS_ERP_CLOSE_ONLY)
2535 retval = ZFCP_ERP_EXIT;
2536 else
2537 retval = zfcp_erp_port_strategy_open(erp_action);
2539 out:
2540 debug_text_event(adapter->erp_dbf, 3, "p_pst/ret");
2541 debug_event(adapter->erp_dbf, 3, &port->wwpn, sizeof (wwn_t));
2542 debug_event(adapter->erp_dbf, 3, &erp_action->action, sizeof (int));
2543 debug_event(adapter->erp_dbf, 3, &retval, sizeof (int));
2545 return retval;
2549 * function:
2551 * purpose:
2553 * returns:
2555 static int
2556 zfcp_erp_port_strategy_open(struct zfcp_erp_action *erp_action)
2558 int retval;
2560 if (atomic_test_mask(ZFCP_STATUS_PORT_WKA,
2561 &erp_action->port->status))
2562 retval = zfcp_erp_port_strategy_open_nameserver(erp_action);
2563 else
2564 retval = zfcp_erp_port_strategy_open_common(erp_action);
2566 return retval;
2570 * function:
2572 * purpose:
2574 * returns:
2576 * FIXME(design): currently only prepared for fabric (nameserver!)
2578 static int
2579 zfcp_erp_port_strategy_open_common(struct zfcp_erp_action *erp_action)
2581 int retval = 0;
2582 struct zfcp_adapter *adapter = erp_action->adapter;
2583 struct zfcp_port *port = erp_action->port;
2585 switch (erp_action->step) {
2587 case ZFCP_ERP_STEP_UNINITIALIZED:
2588 case ZFCP_ERP_STEP_PHYS_PORT_CLOSING:
2589 case ZFCP_ERP_STEP_PORT_CLOSING:
2590 if (fc_host_port_type(adapter->scsi_host) == FC_PORTTYPE_PTP) {
2591 if (port->wwpn != adapter->peer_wwpn) {
2592 ZFCP_LOG_NORMAL("Failed to open port 0x%016Lx "
2593 "on adapter %s.\nPeer WWPN "
2594 "0x%016Lx does not match\n",
2595 port->wwpn,
2596 zfcp_get_busid_by_adapter(adapter),
2597 adapter->peer_wwpn);
2598 zfcp_erp_port_failed(port);
2599 retval = ZFCP_ERP_FAILED;
2600 break;
2602 port->d_id = adapter->peer_d_id;
2603 atomic_set_mask(ZFCP_STATUS_PORT_DID_DID, &port->status);
2604 retval = zfcp_erp_port_strategy_open_port(erp_action);
2605 break;
2607 if (!(adapter->nameserver_port)) {
2608 retval = zfcp_nameserver_enqueue(adapter);
2609 if (retval != 0) {
2610 ZFCP_LOG_NORMAL("error: nameserver port "
2611 "unavailable for adapter %s\n",
2612 zfcp_get_busid_by_adapter(adapter));
2613 retval = ZFCP_ERP_FAILED;
2614 break;
2617 if (!atomic_test_mask(ZFCP_STATUS_COMMON_UNBLOCKED,
2618 &adapter->nameserver_port->status)) {
2619 ZFCP_LOG_DEBUG("nameserver port is not open -> open "
2620 "nameserver port\n");
2621 /* nameserver port may live again */
2622 atomic_set_mask(ZFCP_STATUS_COMMON_RUNNING,
2623 &adapter->nameserver_port->status);
2624 if (zfcp_erp_port_reopen(adapter->nameserver_port, 0)
2625 >= 0) {
2626 erp_action->step =
2627 ZFCP_ERP_STEP_NAMESERVER_OPEN;
2628 retval = ZFCP_ERP_CONTINUES;
2629 } else
2630 retval = ZFCP_ERP_FAILED;
2631 break;
2633 /* else nameserver port is already open, fall through */
2634 case ZFCP_ERP_STEP_NAMESERVER_OPEN:
2635 if (!atomic_test_mask(ZFCP_STATUS_COMMON_OPEN,
2636 &adapter->nameserver_port->status)) {
2637 ZFCP_LOG_DEBUG("open failed for nameserver port\n");
2638 retval = ZFCP_ERP_FAILED;
2639 } else {
2640 ZFCP_LOG_DEBUG("nameserver port is open -> "
2641 "nameserver look-up for port 0x%016Lx\n",
2642 port->wwpn);
2643 retval = zfcp_erp_port_strategy_open_common_lookup
2644 (erp_action);
2646 break;
2648 case ZFCP_ERP_STEP_NAMESERVER_LOOKUP:
2649 if (!atomic_test_mask(ZFCP_STATUS_PORT_DID_DID, &port->status)) {
2650 if (atomic_test_mask
2651 (ZFCP_STATUS_PORT_INVALID_WWPN, &port->status)) {
2652 ZFCP_LOG_DEBUG("nameserver look-up failed "
2653 "for port 0x%016Lx "
2654 "(misconfigured WWPN?)\n",
2655 port->wwpn);
2656 zfcp_erp_port_failed(port);
2657 retval = ZFCP_ERP_EXIT;
2658 } else {
2659 ZFCP_LOG_DEBUG("nameserver look-up failed for "
2660 "port 0x%016Lx\n", port->wwpn);
2661 retval = ZFCP_ERP_FAILED;
2663 } else {
2664 ZFCP_LOG_DEBUG("port 0x%016Lx has d_id=0x%08x -> "
2665 "trying open\n", port->wwpn, port->d_id);
2666 retval = zfcp_erp_port_strategy_open_port(erp_action);
2668 break;
2670 case ZFCP_ERP_STEP_PORT_OPENING:
2671 /* D_ID might have changed during open */
2672 if (atomic_test_mask((ZFCP_STATUS_COMMON_OPEN |
2673 ZFCP_STATUS_PORT_DID_DID),
2674 &port->status)) {
2675 ZFCP_LOG_DEBUG("port 0x%016Lx is open\n", port->wwpn);
2676 retval = ZFCP_ERP_SUCCEEDED;
2677 } else {
2678 ZFCP_LOG_DEBUG("open failed for port 0x%016Lx\n",
2679 port->wwpn);
2680 retval = ZFCP_ERP_FAILED;
2682 break;
2684 default:
2685 ZFCP_LOG_NORMAL("bug: unknown erp step 0x%08x\n",
2686 erp_action->step);
2687 retval = ZFCP_ERP_FAILED;
2690 return retval;
2694 * function:
2696 * purpose:
2698 * returns:
2700 static int
2701 zfcp_erp_port_strategy_open_nameserver(struct zfcp_erp_action *erp_action)
2703 int retval;
2704 struct zfcp_port *port = erp_action->port;
2706 switch (erp_action->step) {
2708 case ZFCP_ERP_STEP_UNINITIALIZED:
2709 case ZFCP_ERP_STEP_PHYS_PORT_CLOSING:
2710 case ZFCP_ERP_STEP_PORT_CLOSING:
2711 ZFCP_LOG_DEBUG("port 0x%016Lx has d_id=0x%08x -> trying open\n",
2712 port->wwpn, port->d_id);
2713 retval = zfcp_erp_port_strategy_open_port(erp_action);
2714 break;
2716 case ZFCP_ERP_STEP_PORT_OPENING:
2717 if (atomic_test_mask(ZFCP_STATUS_COMMON_OPEN, &port->status)) {
2718 ZFCP_LOG_DEBUG("WKA port is open\n");
2719 retval = ZFCP_ERP_SUCCEEDED;
2720 } else {
2721 ZFCP_LOG_DEBUG("open failed for WKA port\n");
2722 retval = ZFCP_ERP_FAILED;
2724 /* this is needed anyway (dont care for retval of wakeup) */
2725 ZFCP_LOG_DEBUG("continue other open port operations\n");
2726 zfcp_erp_port_strategy_open_nameserver_wakeup(erp_action);
2727 break;
2729 default:
2730 ZFCP_LOG_NORMAL("bug: unknown erp step 0x%08x\n",
2731 erp_action->step);
2732 retval = ZFCP_ERP_FAILED;
2735 return retval;
2739 * function:
2741 * purpose: makes the erp thread continue with reopen (physical) port
2742 * actions which have been paused until the name server port
2743 * is opened (or failed)
2745 * returns: 0 (a kind of void retval, its not used)
2747 static int
2748 zfcp_erp_port_strategy_open_nameserver_wakeup(struct zfcp_erp_action
2749 *ns_erp_action)
2751 int retval = 0;
2752 unsigned long flags;
2753 struct zfcp_adapter *adapter = ns_erp_action->adapter;
2754 struct zfcp_erp_action *erp_action, *tmp;
2756 read_lock_irqsave(&adapter->erp_lock, flags);
2757 list_for_each_entry_safe(erp_action, tmp, &adapter->erp_running_head,
2758 list) {
2759 debug_text_event(adapter->erp_dbf, 4, "p_pstnsw_n");
2760 debug_event(adapter->erp_dbf, 4, &erp_action->port->wwpn,
2761 sizeof (wwn_t));
2762 if (erp_action->step == ZFCP_ERP_STEP_NAMESERVER_OPEN) {
2763 debug_text_event(adapter->erp_dbf, 3, "p_pstnsw_w");
2764 debug_event(adapter->erp_dbf, 3,
2765 &erp_action->port->wwpn, sizeof (wwn_t));
2766 if (atomic_test_mask(
2767 ZFCP_STATUS_COMMON_ERP_FAILED,
2768 &adapter->nameserver_port->status))
2769 zfcp_erp_port_failed(erp_action->port);
2770 zfcp_erp_action_ready(erp_action);
2773 read_unlock_irqrestore(&adapter->erp_lock, flags);
2775 return retval;
2779 * function:
2781 * purpose:
2783 * returns: ZFCP_ERP_CONTINUES - action continues (asynchronously)
2784 * ZFCP_ERP_FAILED - action finished unsuccessfully
2786 static int
2787 zfcp_erp_port_forced_strategy_close(struct zfcp_erp_action *erp_action)
2789 int retval;
2790 struct zfcp_adapter *adapter = erp_action->adapter;
2791 struct zfcp_port *port = erp_action->port;
2793 zfcp_erp_timeout_init(erp_action);
2794 retval = zfcp_fsf_close_physical_port(erp_action);
2795 if (retval == -ENOMEM) {
2796 debug_text_event(adapter->erp_dbf, 5, "o_pfstc_nomem");
2797 debug_event(adapter->erp_dbf, 5, &port->wwpn, sizeof (wwn_t));
2798 retval = ZFCP_ERP_NOMEM;
2799 goto out;
2801 erp_action->step = ZFCP_ERP_STEP_PHYS_PORT_CLOSING;
2802 if (retval != 0) {
2803 debug_text_event(adapter->erp_dbf, 5, "o_pfstc_cpf");
2804 debug_event(adapter->erp_dbf, 5, &port->wwpn, sizeof (wwn_t));
2805 /* could not send 'open', fail */
2806 retval = ZFCP_ERP_FAILED;
2807 goto out;
2809 debug_text_event(adapter->erp_dbf, 6, "o_pfstc_cpok");
2810 debug_event(adapter->erp_dbf, 6, &port->wwpn, sizeof (wwn_t));
2811 retval = ZFCP_ERP_CONTINUES;
2812 out:
2813 return retval;
2817 * function:
2819 * purpose:
2821 * returns:
2823 static int
2824 zfcp_erp_port_strategy_clearstati(struct zfcp_port *port)
2826 int retval = 0;
2827 struct zfcp_adapter *adapter = port->adapter;
2829 debug_text_event(adapter->erp_dbf, 5, "p_pstclst");
2830 debug_event(adapter->erp_dbf, 5, &port->wwpn, sizeof (wwn_t));
2832 atomic_clear_mask(ZFCP_STATUS_COMMON_OPENING |
2833 ZFCP_STATUS_COMMON_CLOSING |
2834 ZFCP_STATUS_COMMON_ACCESS_DENIED |
2835 ZFCP_STATUS_PORT_DID_DID |
2836 ZFCP_STATUS_PORT_PHYS_CLOSING |
2837 ZFCP_STATUS_PORT_INVALID_WWPN,
2838 &port->status);
2839 return retval;
2843 * function:
2845 * purpose:
2847 * returns: ZFCP_ERP_CONTINUES - action continues (asynchronously)
2848 * ZFCP_ERP_FAILED - action finished unsuccessfully
2850 static int
2851 zfcp_erp_port_strategy_close(struct zfcp_erp_action *erp_action)
2853 int retval;
2854 struct zfcp_adapter *adapter = erp_action->adapter;
2855 struct zfcp_port *port = erp_action->port;
2857 zfcp_erp_timeout_init(erp_action);
2858 retval = zfcp_fsf_close_port(erp_action);
2859 if (retval == -ENOMEM) {
2860 debug_text_event(adapter->erp_dbf, 5, "p_pstc_nomem");
2861 debug_event(adapter->erp_dbf, 5, &port->wwpn, sizeof (wwn_t));
2862 retval = ZFCP_ERP_NOMEM;
2863 goto out;
2865 erp_action->step = ZFCP_ERP_STEP_PORT_CLOSING;
2866 if (retval != 0) {
2867 debug_text_event(adapter->erp_dbf, 5, "p_pstc_cpf");
2868 debug_event(adapter->erp_dbf, 5, &port->wwpn, sizeof (wwn_t));
2869 /* could not send 'close', fail */
2870 retval = ZFCP_ERP_FAILED;
2871 goto out;
2873 debug_text_event(adapter->erp_dbf, 6, "p_pstc_cpok");
2874 debug_event(adapter->erp_dbf, 6, &port->wwpn, sizeof (wwn_t));
2875 retval = ZFCP_ERP_CONTINUES;
2876 out:
2877 return retval;
2881 * function:
2883 * purpose:
2885 * returns: ZFCP_ERP_CONTINUES - action continues (asynchronously)
2886 * ZFCP_ERP_FAILED - action finished unsuccessfully
2888 static int
2889 zfcp_erp_port_strategy_open_port(struct zfcp_erp_action *erp_action)
2891 int retval;
2892 struct zfcp_adapter *adapter = erp_action->adapter;
2893 struct zfcp_port *port = erp_action->port;
2895 zfcp_erp_timeout_init(erp_action);
2896 retval = zfcp_fsf_open_port(erp_action);
2897 if (retval == -ENOMEM) {
2898 debug_text_event(adapter->erp_dbf, 5, "p_psto_nomem");
2899 debug_event(adapter->erp_dbf, 5, &port->wwpn, sizeof (wwn_t));
2900 retval = ZFCP_ERP_NOMEM;
2901 goto out;
2903 erp_action->step = ZFCP_ERP_STEP_PORT_OPENING;
2904 if (retval != 0) {
2905 debug_text_event(adapter->erp_dbf, 5, "p_psto_opf");
2906 debug_event(adapter->erp_dbf, 5, &port->wwpn, sizeof (wwn_t));
2907 /* could not send 'open', fail */
2908 retval = ZFCP_ERP_FAILED;
2909 goto out;
2911 debug_text_event(adapter->erp_dbf, 6, "p_psto_opok");
2912 debug_event(adapter->erp_dbf, 6, &port->wwpn, sizeof (wwn_t));
2913 retval = ZFCP_ERP_CONTINUES;
2914 out:
2915 return retval;
2919 * function:
2921 * purpose:
2923 * returns: ZFCP_ERP_CONTINUES - action continues (asynchronously)
2924 * ZFCP_ERP_FAILED - action finished unsuccessfully
2926 static int
2927 zfcp_erp_port_strategy_open_common_lookup(struct zfcp_erp_action *erp_action)
2929 int retval;
2930 struct zfcp_adapter *adapter = erp_action->adapter;
2931 struct zfcp_port *port = erp_action->port;
2933 zfcp_erp_timeout_init(erp_action);
2934 retval = zfcp_ns_gid_pn_request(erp_action);
2935 if (retval == -ENOMEM) {
2936 debug_text_event(adapter->erp_dbf, 5, "p_pstn_nomem");
2937 debug_event(adapter->erp_dbf, 5, &port->wwpn, sizeof (wwn_t));
2938 retval = ZFCP_ERP_NOMEM;
2939 goto out;
2941 erp_action->step = ZFCP_ERP_STEP_NAMESERVER_LOOKUP;
2942 if (retval != 0) {
2943 debug_text_event(adapter->erp_dbf, 5, "p_pstn_ref");
2944 debug_event(adapter->erp_dbf, 5, &port->wwpn, sizeof (wwn_t));
2945 /* could not send nameserver request, fail */
2946 retval = ZFCP_ERP_FAILED;
2947 goto out;
2949 debug_text_event(adapter->erp_dbf, 6, "p_pstn_reok");
2950 debug_event(adapter->erp_dbf, 6, &port->wwpn, sizeof (wwn_t));
2951 retval = ZFCP_ERP_CONTINUES;
2952 out:
2953 return retval;
2957 * function:
2959 * purpose: this routine executes the 'Reopen Unit' action
2960 * currently no retries
2962 * returns: ZFCP_ERP_CONTINUES - action continues (asynchronously)
2963 * ZFCP_ERP_SUCCEEDED - action finished successfully
2964 * ZFCP_ERP_FAILED - action finished unsuccessfully
2966 static int
2967 zfcp_erp_unit_strategy(struct zfcp_erp_action *erp_action)
2969 int retval = ZFCP_ERP_FAILED;
2970 struct zfcp_unit *unit = erp_action->unit;
2971 struct zfcp_adapter *adapter = erp_action->adapter;
2973 switch (erp_action->step) {
2976 * FIXME:
2977 * the ULP spec. begs for waiting for oustanding commands
2979 case ZFCP_ERP_STEP_UNINITIALIZED:
2980 zfcp_erp_unit_strategy_clearstati(unit);
2981 if (atomic_test_mask(ZFCP_STATUS_COMMON_OPEN, &unit->status)) {
2982 ZFCP_LOG_DEBUG("unit 0x%016Lx is open -> "
2983 "trying close\n", unit->fcp_lun);
2984 retval = zfcp_erp_unit_strategy_close(erp_action);
2985 break;
2987 /* else it's already closed, fall through */
2988 case ZFCP_ERP_STEP_UNIT_CLOSING:
2989 if (atomic_test_mask(ZFCP_STATUS_COMMON_OPEN, &unit->status)) {
2990 ZFCP_LOG_DEBUG("close failed for unit 0x%016Lx\n",
2991 unit->fcp_lun);
2992 retval = ZFCP_ERP_FAILED;
2993 } else {
2994 if (erp_action->status & ZFCP_STATUS_ERP_CLOSE_ONLY)
2995 retval = ZFCP_ERP_EXIT;
2996 else {
2997 ZFCP_LOG_DEBUG("unit 0x%016Lx is not open -> "
2998 "trying open\n", unit->fcp_lun);
2999 retval =
3000 zfcp_erp_unit_strategy_open(erp_action);
3003 break;
3005 case ZFCP_ERP_STEP_UNIT_OPENING:
3006 if (atomic_test_mask(ZFCP_STATUS_COMMON_OPEN, &unit->status)) {
3007 ZFCP_LOG_DEBUG("unit 0x%016Lx is open\n",
3008 unit->fcp_lun);
3009 retval = ZFCP_ERP_SUCCEEDED;
3010 } else {
3011 ZFCP_LOG_DEBUG("open failed for unit 0x%016Lx\n",
3012 unit->fcp_lun);
3013 retval = ZFCP_ERP_FAILED;
3015 break;
3018 debug_text_event(adapter->erp_dbf, 3, "u_ust/ret");
3019 debug_event(adapter->erp_dbf, 3, &unit->fcp_lun, sizeof (fcp_lun_t));
3020 debug_event(adapter->erp_dbf, 3, &erp_action->action, sizeof (int));
3021 debug_event(adapter->erp_dbf, 3, &retval, sizeof (int));
3022 return retval;
3026 * function:
3028 * purpose:
3030 * returns:
3032 static int
3033 zfcp_erp_unit_strategy_clearstati(struct zfcp_unit *unit)
3035 int retval = 0;
3036 struct zfcp_adapter *adapter = unit->port->adapter;
3038 debug_text_event(adapter->erp_dbf, 5, "u_ustclst");
3039 debug_event(adapter->erp_dbf, 5, &unit->fcp_lun, sizeof (fcp_lun_t));
3041 atomic_clear_mask(ZFCP_STATUS_COMMON_OPENING |
3042 ZFCP_STATUS_COMMON_CLOSING |
3043 ZFCP_STATUS_COMMON_ACCESS_DENIED |
3044 ZFCP_STATUS_UNIT_SHARED |
3045 ZFCP_STATUS_UNIT_READONLY,
3046 &unit->status);
3048 return retval;
3052 * function:
3054 * purpose:
3056 * returns: ZFCP_ERP_CONTINUES - action continues (asynchronously)
3057 * ZFCP_ERP_FAILED - action finished unsuccessfully
3059 static int
3060 zfcp_erp_unit_strategy_close(struct zfcp_erp_action *erp_action)
3062 int retval;
3063 struct zfcp_adapter *adapter = erp_action->adapter;
3064 struct zfcp_unit *unit = erp_action->unit;
3066 zfcp_erp_timeout_init(erp_action);
3067 retval = zfcp_fsf_close_unit(erp_action);
3068 if (retval == -ENOMEM) {
3069 debug_text_event(adapter->erp_dbf, 5, "u_ustc_nomem");
3070 debug_event(adapter->erp_dbf, 5, &unit->fcp_lun,
3071 sizeof (fcp_lun_t));
3072 retval = ZFCP_ERP_NOMEM;
3073 goto out;
3075 erp_action->step = ZFCP_ERP_STEP_UNIT_CLOSING;
3076 if (retval != 0) {
3077 debug_text_event(adapter->erp_dbf, 5, "u_ustc_cuf");
3078 debug_event(adapter->erp_dbf, 5, &unit->fcp_lun,
3079 sizeof (fcp_lun_t));
3080 /* could not send 'close', fail */
3081 retval = ZFCP_ERP_FAILED;
3082 goto out;
3084 debug_text_event(adapter->erp_dbf, 6, "u_ustc_cuok");
3085 debug_event(adapter->erp_dbf, 6, &unit->fcp_lun, sizeof (fcp_lun_t));
3086 retval = ZFCP_ERP_CONTINUES;
3088 out:
3089 return retval;
3093 * function:
3095 * purpose:
3097 * returns: ZFCP_ERP_CONTINUES - action continues (asynchronously)
3098 * ZFCP_ERP_FAILED - action finished unsuccessfully
3100 static int
3101 zfcp_erp_unit_strategy_open(struct zfcp_erp_action *erp_action)
3103 int retval;
3104 struct zfcp_adapter *adapter = erp_action->adapter;
3105 struct zfcp_unit *unit = erp_action->unit;
3107 zfcp_erp_timeout_init(erp_action);
3108 retval = zfcp_fsf_open_unit(erp_action);
3109 if (retval == -ENOMEM) {
3110 debug_text_event(adapter->erp_dbf, 5, "u_usto_nomem");
3111 debug_event(adapter->erp_dbf, 5, &unit->fcp_lun,
3112 sizeof (fcp_lun_t));
3113 retval = ZFCP_ERP_NOMEM;
3114 goto out;
3116 erp_action->step = ZFCP_ERP_STEP_UNIT_OPENING;
3117 if (retval != 0) {
3118 debug_text_event(adapter->erp_dbf, 5, "u_usto_ouf");
3119 debug_event(adapter->erp_dbf, 5, &unit->fcp_lun,
3120 sizeof (fcp_lun_t));
3121 /* could not send 'open', fail */
3122 retval = ZFCP_ERP_FAILED;
3123 goto out;
3125 debug_text_event(adapter->erp_dbf, 6, "u_usto_ouok");
3126 debug_event(adapter->erp_dbf, 6, &unit->fcp_lun, sizeof (fcp_lun_t));
3127 retval = ZFCP_ERP_CONTINUES;
3128 out:
3129 return retval;
3133 * function:
3135 * purpose:
3137 * returns:
3139 static inline void
3140 zfcp_erp_timeout_init(struct zfcp_erp_action *erp_action)
3142 init_timer(&erp_action->timer);
3143 erp_action->timer.function = zfcp_erp_timeout_handler;
3144 erp_action->timer.data = (unsigned long) erp_action;
3145 /* jiffies will be added in zfcp_fsf_req_send */
3146 erp_action->timer.expires = ZFCP_ERP_FSFREQ_TIMEOUT;
3150 * function:
3152 * purpose: enqueue the specified error recovery action, if needed
3154 * returns:
3156 static int
3157 zfcp_erp_action_enqueue(int action,
3158 struct zfcp_adapter *adapter,
3159 struct zfcp_port *port, struct zfcp_unit *unit)
3161 int retval = 1;
3162 struct zfcp_erp_action *erp_action = NULL;
3163 int stronger_action = 0;
3164 u32 status = 0;
3167 * We need some rules here which check whether we really need
3168 * this action or whether we should just drop it.
3169 * E.g. if there is a unfinished 'Reopen Port' request then we drop a
3170 * 'Reopen Unit' request for an associated unit since we can't
3171 * satisfy this request now. A 'Reopen Port' action will trigger
3172 * 'Reopen Unit' actions when it completes.
3173 * Thus, there are only actions in the queue which can immediately be
3174 * executed. This makes the processing of the action queue more
3175 * efficient.
3178 if (!atomic_test_mask(ZFCP_STATUS_ADAPTER_ERP_THREAD_UP,
3179 &adapter->status))
3180 return -EIO;
3182 debug_event(adapter->erp_dbf, 4, &action, sizeof (int));
3183 /* check whether we really need this */
3184 switch (action) {
3185 case ZFCP_ERP_ACTION_REOPEN_UNIT:
3186 if (atomic_test_mask
3187 (ZFCP_STATUS_COMMON_ERP_INUSE, &unit->status)) {
3188 debug_text_event(adapter->erp_dbf, 4, "u_actenq_drp");
3189 debug_event(adapter->erp_dbf, 4, &port->wwpn,
3190 sizeof (wwn_t));
3191 debug_event(adapter->erp_dbf, 4, &unit->fcp_lun,
3192 sizeof (fcp_lun_t));
3193 goto out;
3195 if (!atomic_test_mask
3196 (ZFCP_STATUS_COMMON_RUNNING, &port->status) ||
3197 atomic_test_mask
3198 (ZFCP_STATUS_COMMON_ERP_FAILED, &port->status)) {
3199 goto out;
3201 if (!atomic_test_mask
3202 (ZFCP_STATUS_COMMON_UNBLOCKED, &port->status)) {
3203 stronger_action = ZFCP_ERP_ACTION_REOPEN_PORT;
3204 unit = NULL;
3206 /* fall through !!! */
3208 case ZFCP_ERP_ACTION_REOPEN_PORT:
3209 if (atomic_test_mask
3210 (ZFCP_STATUS_COMMON_ERP_INUSE, &port->status)) {
3211 debug_text_event(adapter->erp_dbf, 4, "p_actenq_drp");
3212 debug_event(adapter->erp_dbf, 4, &port->wwpn,
3213 sizeof (wwn_t));
3214 goto out;
3216 /* fall through !!! */
3218 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
3219 if (atomic_test_mask(ZFCP_STATUS_COMMON_ERP_INUSE,
3220 &port->status)) {
3221 if (port->erp_action.action !=
3222 ZFCP_ERP_ACTION_REOPEN_PORT_FORCED) {
3223 ZFCP_LOG_INFO("dropped erp action %i (port "
3224 "0x%016Lx, action in use: %i)\n",
3225 action, port->wwpn,
3226 port->erp_action.action);
3227 debug_text_event(adapter->erp_dbf, 4,
3228 "pf_actenq_drp");
3229 } else
3230 debug_text_event(adapter->erp_dbf, 4,
3231 "pf_actenq_drpcp");
3232 debug_event(adapter->erp_dbf, 4, &port->wwpn,
3233 sizeof (wwn_t));
3234 goto out;
3236 if (!atomic_test_mask
3237 (ZFCP_STATUS_COMMON_RUNNING, &adapter->status) ||
3238 atomic_test_mask
3239 (ZFCP_STATUS_COMMON_ERP_FAILED, &adapter->status)) {
3240 goto out;
3242 if (!atomic_test_mask
3243 (ZFCP_STATUS_COMMON_UNBLOCKED, &adapter->status)) {
3244 stronger_action = ZFCP_ERP_ACTION_REOPEN_ADAPTER;
3245 port = NULL;
3247 /* fall through !!! */
3249 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
3250 if (atomic_test_mask
3251 (ZFCP_STATUS_COMMON_ERP_INUSE, &adapter->status)) {
3252 debug_text_event(adapter->erp_dbf, 4, "a_actenq_drp");
3253 goto out;
3255 break;
3257 default:
3258 debug_text_exception(adapter->erp_dbf, 1, "a_actenq_bug");
3259 debug_event(adapter->erp_dbf, 1, &action, sizeof (int));
3260 ZFCP_LOG_NORMAL("bug: unknown erp action requested "
3261 "on adapter %s (action=%d)\n",
3262 zfcp_get_busid_by_adapter(adapter), action);
3263 goto out;
3266 /* check whether we need something stronger first */
3267 if (stronger_action) {
3268 debug_text_event(adapter->erp_dbf, 4, "a_actenq_str");
3269 debug_event(adapter->erp_dbf, 4, &stronger_action,
3270 sizeof (int));
3271 ZFCP_LOG_DEBUG("stronger erp action %d needed before "
3272 "erp action %d on adapter %s\n",
3273 stronger_action, action,
3274 zfcp_get_busid_by_adapter(adapter));
3275 action = stronger_action;
3278 /* mark adapter to have some error recovery pending */
3279 atomic_set_mask(ZFCP_STATUS_ADAPTER_ERP_PENDING, &adapter->status);
3281 /* setup error recovery action */
3282 switch (action) {
3284 case ZFCP_ERP_ACTION_REOPEN_UNIT:
3285 zfcp_unit_get(unit);
3286 atomic_set_mask(ZFCP_STATUS_COMMON_ERP_INUSE, &unit->status);
3287 erp_action = &unit->erp_action;
3288 if (!atomic_test_mask
3289 (ZFCP_STATUS_COMMON_RUNNING, &unit->status))
3290 status = ZFCP_STATUS_ERP_CLOSE_ONLY;
3291 break;
3293 case ZFCP_ERP_ACTION_REOPEN_PORT:
3294 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
3295 zfcp_port_get(port);
3296 zfcp_erp_action_dismiss_port(port);
3297 atomic_set_mask(ZFCP_STATUS_COMMON_ERP_INUSE, &port->status);
3298 erp_action = &port->erp_action;
3299 if (!atomic_test_mask
3300 (ZFCP_STATUS_COMMON_RUNNING, &port->status))
3301 status = ZFCP_STATUS_ERP_CLOSE_ONLY;
3302 break;
3304 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
3305 zfcp_adapter_get(adapter);
3306 zfcp_erp_action_dismiss_adapter(adapter);
3307 atomic_set_mask(ZFCP_STATUS_COMMON_ERP_INUSE, &adapter->status);
3308 erp_action = &adapter->erp_action;
3309 if (!atomic_test_mask
3310 (ZFCP_STATUS_COMMON_RUNNING, &adapter->status))
3311 status = ZFCP_STATUS_ERP_CLOSE_ONLY;
3312 break;
3315 debug_text_event(adapter->erp_dbf, 4, "a_actenq");
3317 memset(erp_action, 0, sizeof (struct zfcp_erp_action));
3318 erp_action->adapter = adapter;
3319 erp_action->port = port;
3320 erp_action->unit = unit;
3321 erp_action->action = action;
3322 erp_action->status = status;
3324 ++adapter->erp_total_count;
3326 /* finally put it into 'ready' queue and kick erp thread */
3327 list_add(&erp_action->list, &adapter->erp_ready_head);
3328 up(&adapter->erp_ready_sem);
3329 retval = 0;
3330 out:
3331 return retval;
3335 * function:
3337 * purpose:
3339 * returns:
3341 static int
3342 zfcp_erp_action_dequeue(struct zfcp_erp_action *erp_action)
3344 int retval = 0;
3345 struct zfcp_adapter *adapter = erp_action->adapter;
3347 --adapter->erp_total_count;
3348 if (erp_action->status & ZFCP_STATUS_ERP_LOWMEM) {
3349 --adapter->erp_low_mem_count;
3350 erp_action->status &= ~ZFCP_STATUS_ERP_LOWMEM;
3353 debug_text_event(adapter->erp_dbf, 4, "a_actdeq");
3354 debug_event(adapter->erp_dbf, 4, &erp_action->action, sizeof (int));
3355 list_del(&erp_action->list);
3356 switch (erp_action->action) {
3357 case ZFCP_ERP_ACTION_REOPEN_UNIT:
3358 atomic_clear_mask(ZFCP_STATUS_COMMON_ERP_INUSE,
3359 &erp_action->unit->status);
3360 break;
3361 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
3362 case ZFCP_ERP_ACTION_REOPEN_PORT:
3363 atomic_clear_mask(ZFCP_STATUS_COMMON_ERP_INUSE,
3364 &erp_action->port->status);
3365 break;
3366 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
3367 atomic_clear_mask(ZFCP_STATUS_COMMON_ERP_INUSE,
3368 &erp_action->adapter->status);
3369 break;
3370 default:
3371 /* bug */
3372 break;
3374 return retval;
3378 * zfcp_erp_action_cleanup
3380 * Register unit with scsi stack if appropriate and fix reference counts.
3381 * Note: Temporary units are not registered with scsi stack.
3383 static void
3384 zfcp_erp_action_cleanup(int action, struct zfcp_adapter *adapter,
3385 struct zfcp_port *port, struct zfcp_unit *unit,
3386 int result)
3388 switch (action) {
3389 case ZFCP_ERP_ACTION_REOPEN_UNIT:
3390 if ((result == ZFCP_ERP_SUCCEEDED)
3391 && (!atomic_test_mask(ZFCP_STATUS_UNIT_TEMPORARY,
3392 &unit->status))
3393 && !unit->device
3394 && port->rport)
3395 scsi_add_device(port->adapter->scsi_host, 0,
3396 port->rport->scsi_target_id,
3397 unit->scsi_lun);
3398 zfcp_unit_put(unit);
3399 break;
3400 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
3401 case ZFCP_ERP_ACTION_REOPEN_PORT:
3402 if ((result == ZFCP_ERP_SUCCEEDED)
3403 && !atomic_test_mask(ZFCP_STATUS_PORT_NO_WWPN,
3404 &port->status)
3405 && !port->rport) {
3406 struct fc_rport_identifiers ids;
3407 ids.node_name = port->wwnn;
3408 ids.port_name = port->wwpn;
3409 ids.port_id = port->d_id;
3410 ids.roles = FC_RPORT_ROLE_FCP_TARGET;
3411 port->rport =
3412 fc_remote_port_add(adapter->scsi_host, 0, &ids);
3413 if (!port->rport)
3414 ZFCP_LOG_NORMAL("failed registration of rport"
3415 "(adapter %s, wwpn=0x%016Lx)\n",
3416 zfcp_get_busid_by_port(port),
3417 port->wwpn);
3419 zfcp_port_put(port);
3420 break;
3421 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
3422 zfcp_adapter_put(adapter);
3423 break;
3424 default:
3425 break;
3431 * function:
3433 * purpose:
3435 * returns: FIXME
3437 static int
3438 zfcp_erp_action_dismiss_adapter(struct zfcp_adapter *adapter)
3440 int retval = 0;
3441 struct zfcp_port *port;
3443 debug_text_event(adapter->erp_dbf, 5, "a_actab");
3444 if (atomic_test_mask(ZFCP_STATUS_COMMON_ERP_INUSE, &adapter->status))
3445 zfcp_erp_action_dismiss(&adapter->erp_action);
3446 else
3447 list_for_each_entry(port, &adapter->port_list_head, list)
3448 zfcp_erp_action_dismiss_port(port);
3450 return retval;
3454 * function:
3456 * purpose:
3458 * returns: FIXME
3460 static int
3461 zfcp_erp_action_dismiss_port(struct zfcp_port *port)
3463 int retval = 0;
3464 struct zfcp_unit *unit;
3465 struct zfcp_adapter *adapter = port->adapter;
3467 debug_text_event(adapter->erp_dbf, 5, "p_actab");
3468 debug_event(adapter->erp_dbf, 5, &port->wwpn, sizeof (wwn_t));
3469 if (atomic_test_mask(ZFCP_STATUS_COMMON_ERP_INUSE, &port->status))
3470 zfcp_erp_action_dismiss(&port->erp_action);
3471 else
3472 list_for_each_entry(unit, &port->unit_list_head, list)
3473 zfcp_erp_action_dismiss_unit(unit);
3475 return retval;
3479 * function:
3481 * purpose:
3483 * returns: FIXME
3485 static int
3486 zfcp_erp_action_dismiss_unit(struct zfcp_unit *unit)
3488 int retval = 0;
3489 struct zfcp_adapter *adapter = unit->port->adapter;
3491 debug_text_event(adapter->erp_dbf, 5, "u_actab");
3492 debug_event(adapter->erp_dbf, 5, &unit->fcp_lun, sizeof (fcp_lun_t));
3493 if (atomic_test_mask(ZFCP_STATUS_COMMON_ERP_INUSE, &unit->status))
3494 zfcp_erp_action_dismiss(&unit->erp_action);
3496 return retval;
3500 * function:
3502 * purpose: moves erp_action to 'erp running list'
3504 * returns:
3506 static inline void
3507 zfcp_erp_action_to_running(struct zfcp_erp_action *erp_action)
3509 struct zfcp_adapter *adapter = erp_action->adapter;
3511 debug_text_event(adapter->erp_dbf, 6, "a_toru");
3512 debug_event(adapter->erp_dbf, 6, &erp_action->action, sizeof (int));
3513 list_move(&erp_action->list, &erp_action->adapter->erp_running_head);
3517 * function:
3519 * purpose: moves erp_action to 'erp ready list'
3521 * returns:
3523 static inline void
3524 zfcp_erp_action_to_ready(struct zfcp_erp_action *erp_action)
3526 struct zfcp_adapter *adapter = erp_action->adapter;
3528 debug_text_event(adapter->erp_dbf, 6, "a_tore");
3529 debug_event(adapter->erp_dbf, 6, &erp_action->action, sizeof (int));
3530 list_move(&erp_action->list, &erp_action->adapter->erp_ready_head);
3534 * function: zfcp_erp_port_boxed
3536 * purpose:
3538 void
3539 zfcp_erp_port_boxed(struct zfcp_port *port)
3541 struct zfcp_adapter *adapter = port->adapter;
3542 unsigned long flags;
3544 debug_text_event(adapter->erp_dbf, 3, "p_access_boxed");
3545 debug_event(adapter->erp_dbf, 3, &port->wwpn, sizeof(wwn_t));
3546 read_lock_irqsave(&zfcp_data.config_lock, flags);
3547 zfcp_erp_modify_port_status(port,
3548 ZFCP_STATUS_COMMON_ACCESS_BOXED,
3549 ZFCP_SET);
3550 read_unlock_irqrestore(&zfcp_data.config_lock, flags);
3551 zfcp_erp_port_reopen(port, ZFCP_STATUS_COMMON_ERP_FAILED);
3555 * function: zfcp_erp_unit_boxed
3557 * purpose:
3559 void
3560 zfcp_erp_unit_boxed(struct zfcp_unit *unit)
3562 struct zfcp_adapter *adapter = unit->port->adapter;
3564 debug_text_event(adapter->erp_dbf, 3, "u_access_boxed");
3565 debug_event(adapter->erp_dbf, 3, &unit->fcp_lun, sizeof(fcp_lun_t));
3566 zfcp_erp_modify_unit_status(unit,
3567 ZFCP_STATUS_COMMON_ACCESS_BOXED,
3568 ZFCP_SET);
3569 zfcp_erp_unit_reopen(unit, ZFCP_STATUS_COMMON_ERP_FAILED);
3573 * function: zfcp_erp_port_access_denied
3575 * purpose:
3577 void
3578 zfcp_erp_port_access_denied(struct zfcp_port *port)
3580 struct zfcp_adapter *adapter = port->adapter;
3581 unsigned long flags;
3583 debug_text_event(adapter->erp_dbf, 3, "p_access_denied");
3584 debug_event(adapter->erp_dbf, 3, &port->wwpn, sizeof(wwn_t));
3585 read_lock_irqsave(&zfcp_data.config_lock, flags);
3586 zfcp_erp_modify_port_status(port,
3587 ZFCP_STATUS_COMMON_ERP_FAILED |
3588 ZFCP_STATUS_COMMON_ACCESS_DENIED,
3589 ZFCP_SET);
3590 read_unlock_irqrestore(&zfcp_data.config_lock, flags);
3594 * function: zfcp_erp_unit_access_denied
3596 * purpose:
3598 void
3599 zfcp_erp_unit_access_denied(struct zfcp_unit *unit)
3601 struct zfcp_adapter *adapter = unit->port->adapter;
3603 debug_text_event(adapter->erp_dbf, 3, "u_access_denied");
3604 debug_event(adapter->erp_dbf, 3, &unit->fcp_lun, sizeof(fcp_lun_t));
3605 zfcp_erp_modify_unit_status(unit,
3606 ZFCP_STATUS_COMMON_ERP_FAILED |
3607 ZFCP_STATUS_COMMON_ACCESS_DENIED,
3608 ZFCP_SET);
3612 * function: zfcp_erp_adapter_access_changed
3614 * purpose:
3616 void
3617 zfcp_erp_adapter_access_changed(struct zfcp_adapter *adapter)
3619 struct zfcp_port *port;
3620 unsigned long flags;
3622 if (adapter->connection_features & FSF_FEATURE_NPIV_MODE)
3623 return;
3625 debug_text_event(adapter->erp_dbf, 3, "a_access_recover");
3626 debug_event(adapter->erp_dbf, 3, &adapter->name, 8);
3628 read_lock_irqsave(&zfcp_data.config_lock, flags);
3629 if (adapter->nameserver_port)
3630 zfcp_erp_port_access_changed(adapter->nameserver_port);
3631 list_for_each_entry(port, &adapter->port_list_head, list)
3632 if (port != adapter->nameserver_port)
3633 zfcp_erp_port_access_changed(port);
3634 read_unlock_irqrestore(&zfcp_data.config_lock, flags);
3638 * function: zfcp_erp_port_access_changed
3640 * purpose:
3642 void
3643 zfcp_erp_port_access_changed(struct zfcp_port *port)
3645 struct zfcp_adapter *adapter = port->adapter;
3646 struct zfcp_unit *unit;
3648 debug_text_event(adapter->erp_dbf, 3, "p_access_recover");
3649 debug_event(adapter->erp_dbf, 3, &port->wwpn, sizeof(wwn_t));
3651 if (!atomic_test_mask(ZFCP_STATUS_COMMON_ACCESS_DENIED,
3652 &port->status) &&
3653 !atomic_test_mask(ZFCP_STATUS_COMMON_ACCESS_BOXED,
3654 &port->status)) {
3655 if (!atomic_test_mask(ZFCP_STATUS_PORT_WKA, &port->status))
3656 list_for_each_entry(unit, &port->unit_list_head, list)
3657 zfcp_erp_unit_access_changed(unit);
3658 return;
3661 ZFCP_LOG_NORMAL("reopen of port 0x%016Lx on adapter %s "
3662 "(due to ACT update)\n",
3663 port->wwpn, zfcp_get_busid_by_adapter(adapter));
3664 if (zfcp_erp_port_reopen(port, ZFCP_STATUS_COMMON_ERP_FAILED) != 0)
3665 ZFCP_LOG_NORMAL("failed reopen of port"
3666 "(adapter %s, wwpn=0x%016Lx)\n",
3667 zfcp_get_busid_by_adapter(adapter), port->wwpn);
3671 * function: zfcp_erp_unit_access_changed
3673 * purpose:
3675 void
3676 zfcp_erp_unit_access_changed(struct zfcp_unit *unit)
3678 struct zfcp_adapter *adapter = unit->port->adapter;
3680 debug_text_event(adapter->erp_dbf, 3, "u_access_recover");
3681 debug_event(adapter->erp_dbf, 3, &unit->fcp_lun, sizeof(fcp_lun_t));
3683 if (!atomic_test_mask(ZFCP_STATUS_COMMON_ACCESS_DENIED,
3684 &unit->status) &&
3685 !atomic_test_mask(ZFCP_STATUS_COMMON_ACCESS_BOXED,
3686 &unit->status))
3687 return;
3689 ZFCP_LOG_NORMAL("reopen of unit 0x%016Lx on port 0x%016Lx "
3690 " on adapter %s (due to ACT update)\n",
3691 unit->fcp_lun, unit->port->wwpn,
3692 zfcp_get_busid_by_adapter(adapter));
3693 if (zfcp_erp_unit_reopen(unit, ZFCP_STATUS_COMMON_ERP_FAILED) != 0)
3694 ZFCP_LOG_NORMAL("failed reopen of unit (adapter %s, "
3695 "wwpn=0x%016Lx, fcp_lun=0x%016Lx)\n",
3696 zfcp_get_busid_by_adapter(adapter),
3697 unit->port->wwpn, unit->fcp_lun);
3700 #undef ZFCP_LOG_AREA