[SCSI] zfcp: evaluate plogi payload to set maxframe_size, supported_classes of rports
[linux-2.6/kmemtrace.git] / drivers / s390 / scsi / zfcp_erp.c
blob05c47f6ca924c9804194f4bf86027fcfcd80fe36
1 /*
2 * This file is part of the zfcp device driver for
3 * FCP adapters for IBM System z9 and zSeries.
5 * (C) Copyright IBM Corp. 2002, 2006
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2, or (at your option)
10 * any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #define ZFCP_LOG_AREA ZFCP_LOG_AREA_ERP
24 #include "zfcp_ext.h"
26 static int zfcp_erp_adisc(struct zfcp_port *);
27 static void zfcp_erp_adisc_handler(unsigned long);
29 static int zfcp_erp_adapter_reopen_internal(struct zfcp_adapter *, int);
30 static int zfcp_erp_port_forced_reopen_internal(struct zfcp_port *, int);
31 static int zfcp_erp_port_reopen_internal(struct zfcp_port *, int);
32 static int zfcp_erp_unit_reopen_internal(struct zfcp_unit *, int);
34 static int zfcp_erp_port_reopen_all_internal(struct zfcp_adapter *, int);
35 static int zfcp_erp_unit_reopen_all_internal(struct zfcp_port *, int);
37 static void zfcp_erp_adapter_block(struct zfcp_adapter *, int);
38 static void zfcp_erp_adapter_unblock(struct zfcp_adapter *);
39 static void zfcp_erp_port_block(struct zfcp_port *, int);
40 static void zfcp_erp_port_unblock(struct zfcp_port *);
41 static void zfcp_erp_unit_block(struct zfcp_unit *, int);
42 static void zfcp_erp_unit_unblock(struct zfcp_unit *);
44 static int zfcp_erp_thread(void *);
46 static int zfcp_erp_strategy(struct zfcp_erp_action *);
48 static int zfcp_erp_strategy_do_action(struct zfcp_erp_action *);
49 static int zfcp_erp_strategy_memwait(struct zfcp_erp_action *);
50 static int zfcp_erp_strategy_check_target(struct zfcp_erp_action *, int);
51 static int zfcp_erp_strategy_check_unit(struct zfcp_unit *, int);
52 static int zfcp_erp_strategy_check_port(struct zfcp_port *, int);
53 static int zfcp_erp_strategy_check_adapter(struct zfcp_adapter *, int);
54 static int zfcp_erp_strategy_statechange(int, u32, struct zfcp_adapter *,
55 struct zfcp_port *,
56 struct zfcp_unit *, int);
57 static inline int zfcp_erp_strategy_statechange_detected(atomic_t *, u32);
58 static int zfcp_erp_strategy_followup_actions(int, struct zfcp_adapter *,
59 struct zfcp_port *,
60 struct zfcp_unit *, int);
61 static int zfcp_erp_strategy_check_queues(struct zfcp_adapter *);
62 static int zfcp_erp_strategy_check_action(struct zfcp_erp_action *, int);
64 static int zfcp_erp_adapter_strategy(struct zfcp_erp_action *);
65 static int zfcp_erp_adapter_strategy_generic(struct zfcp_erp_action *, int);
66 static int zfcp_erp_adapter_strategy_close(struct zfcp_erp_action *);
67 static int zfcp_erp_adapter_strategy_close_qdio(struct zfcp_erp_action *);
68 static int zfcp_erp_adapter_strategy_close_fsf(struct zfcp_erp_action *);
69 static int zfcp_erp_adapter_strategy_open(struct zfcp_erp_action *);
70 static int zfcp_erp_adapter_strategy_open_qdio(struct zfcp_erp_action *);
71 static int zfcp_erp_adapter_strategy_open_fsf(struct zfcp_erp_action *);
72 static int zfcp_erp_adapter_strategy_open_fsf_xconfig(struct zfcp_erp_action *);
73 static int zfcp_erp_adapter_strategy_open_fsf_xport(struct zfcp_erp_action *);
74 static int zfcp_erp_adapter_strategy_open_fsf_statusread(
75 struct zfcp_erp_action *);
77 static int zfcp_erp_port_forced_strategy(struct zfcp_erp_action *);
78 static int zfcp_erp_port_forced_strategy_close(struct zfcp_erp_action *);
80 static int zfcp_erp_port_strategy(struct zfcp_erp_action *);
81 static int zfcp_erp_port_strategy_clearstati(struct zfcp_port *);
82 static int zfcp_erp_port_strategy_close(struct zfcp_erp_action *);
83 static int zfcp_erp_port_strategy_open(struct zfcp_erp_action *);
84 static int zfcp_erp_port_strategy_open_nameserver(struct zfcp_erp_action *);
85 static int zfcp_erp_port_strategy_open_nameserver_wakeup(
86 struct zfcp_erp_action *);
87 static int zfcp_erp_port_strategy_open_common(struct zfcp_erp_action *);
88 static int zfcp_erp_port_strategy_open_common_lookup(struct zfcp_erp_action *);
89 static int zfcp_erp_port_strategy_open_port(struct zfcp_erp_action *);
91 static int zfcp_erp_unit_strategy(struct zfcp_erp_action *);
92 static int zfcp_erp_unit_strategy_clearstati(struct zfcp_unit *);
93 static int zfcp_erp_unit_strategy_close(struct zfcp_erp_action *);
94 static int zfcp_erp_unit_strategy_open(struct zfcp_erp_action *);
96 static int zfcp_erp_action_dismiss_adapter(struct zfcp_adapter *);
97 static int zfcp_erp_action_dismiss_port(struct zfcp_port *);
98 static int zfcp_erp_action_dismiss_unit(struct zfcp_unit *);
99 static int zfcp_erp_action_dismiss(struct zfcp_erp_action *);
101 static int zfcp_erp_action_enqueue(int, struct zfcp_adapter *,
102 struct zfcp_port *, struct zfcp_unit *);
103 static int zfcp_erp_action_dequeue(struct zfcp_erp_action *);
104 static void zfcp_erp_action_cleanup(int, struct zfcp_adapter *,
105 struct zfcp_port *, struct zfcp_unit *,
106 int);
108 static void zfcp_erp_action_ready(struct zfcp_erp_action *);
109 static int zfcp_erp_action_exists(struct zfcp_erp_action *);
111 static inline void zfcp_erp_action_to_ready(struct zfcp_erp_action *);
112 static inline void zfcp_erp_action_to_running(struct zfcp_erp_action *);
114 static void zfcp_erp_memwait_handler(unsigned long);
115 static void zfcp_erp_timeout_handler(unsigned long);
116 static inline void zfcp_erp_timeout_init(struct zfcp_erp_action *);
119 * zfcp_fsf_request_timeout_handler - called if a request timed out
120 * @data: pointer to adapter for handler function
122 * This function needs to be called if requests (ELS, Generic Service,
123 * or SCSI commands) exceed a certain time limit. The assumption is
124 * that after the time limit the adapter get stuck. So we trigger a reopen of
125 * the adapter. This should not be used for error recovery, SCSI abort
126 * commands and SCSI requests from SCSI mid-layer.
128 void
129 zfcp_fsf_request_timeout_handler(unsigned long data)
131 struct zfcp_adapter *adapter;
133 adapter = (struct zfcp_adapter *) data;
135 zfcp_erp_adapter_reopen(adapter, 0);
139 * function: zfcp_fsf_scsi_er_timeout_handler
141 * purpose: This function needs to be called whenever a SCSI error recovery
142 * action (abort/reset) does not return.
143 * Re-opening the adapter means that the command can be returned
144 * by zfcp (it is guarranteed that it does not return via the
145 * adapter anymore). The buffer can then be used again.
147 * returns: sod all
149 void
150 zfcp_fsf_scsi_er_timeout_handler(unsigned long data)
152 struct zfcp_adapter *adapter = (struct zfcp_adapter *) data;
154 ZFCP_LOG_NORMAL("warning: SCSI error recovery timed out. "
155 "Restarting all operations on the adapter %s\n",
156 zfcp_get_busid_by_adapter(adapter));
157 debug_text_event(adapter->erp_dbf, 1, "eh_lmem_tout");
158 zfcp_erp_adapter_reopen(adapter, 0);
160 return;
164 * function:
166 * purpose: called if an adapter failed,
167 * initiates adapter recovery which is done
168 * asynchronously
170 * returns: 0 - initiated action succesfully
171 * <0 - failed to initiate action
174 zfcp_erp_adapter_reopen_internal(struct zfcp_adapter *adapter, int clear_mask)
176 int retval;
178 debug_text_event(adapter->erp_dbf, 5, "a_ro");
179 ZFCP_LOG_DEBUG("reopen adapter %s\n",
180 zfcp_get_busid_by_adapter(adapter));
182 zfcp_erp_adapter_block(adapter, clear_mask);
184 if (atomic_test_mask(ZFCP_STATUS_COMMON_ERP_FAILED, &adapter->status)) {
185 ZFCP_LOG_DEBUG("skipped reopen of failed adapter %s\n",
186 zfcp_get_busid_by_adapter(adapter));
187 debug_text_event(adapter->erp_dbf, 5, "a_ro_f");
188 /* ensure propagation of failed status to new devices */
189 zfcp_erp_adapter_failed(adapter);
190 retval = -EIO;
191 goto out;
193 retval = zfcp_erp_action_enqueue(ZFCP_ERP_ACTION_REOPEN_ADAPTER,
194 adapter, NULL, NULL);
196 out:
197 return retval;
201 * function:
203 * purpose: Wrappper for zfcp_erp_adapter_reopen_internal
204 * used to ensure the correct locking
206 * returns: 0 - initiated action succesfully
207 * <0 - failed to initiate action
210 zfcp_erp_adapter_reopen(struct zfcp_adapter *adapter, int clear_mask)
212 int retval;
213 unsigned long flags;
215 read_lock_irqsave(&zfcp_data.config_lock, flags);
216 write_lock(&adapter->erp_lock);
217 retval = zfcp_erp_adapter_reopen_internal(adapter, clear_mask);
218 write_unlock(&adapter->erp_lock);
219 read_unlock_irqrestore(&zfcp_data.config_lock, flags);
221 return retval;
225 zfcp_erp_adapter_shutdown(struct zfcp_adapter *adapter, int clear_mask)
227 int retval;
229 retval = zfcp_erp_adapter_reopen(adapter,
230 ZFCP_STATUS_COMMON_RUNNING |
231 ZFCP_STATUS_COMMON_ERP_FAILED |
232 clear_mask);
234 return retval;
238 zfcp_erp_port_shutdown(struct zfcp_port *port, int clear_mask)
240 int retval;
242 retval = zfcp_erp_port_reopen(port,
243 ZFCP_STATUS_COMMON_RUNNING |
244 ZFCP_STATUS_COMMON_ERP_FAILED |
245 clear_mask);
247 return retval;
251 zfcp_erp_unit_shutdown(struct zfcp_unit *unit, int clear_mask)
253 int retval;
255 retval = zfcp_erp_unit_reopen(unit,
256 ZFCP_STATUS_COMMON_RUNNING |
257 ZFCP_STATUS_COMMON_ERP_FAILED |
258 clear_mask);
260 return retval;
265 * zfcp_erp_adisc - send ADISC ELS command
266 * @port: port structure
269 zfcp_erp_adisc(struct zfcp_port *port)
271 struct zfcp_adapter *adapter = port->adapter;
272 struct zfcp_send_els *send_els;
273 struct zfcp_ls_adisc *adisc;
274 void *address = NULL;
275 int retval = 0;
276 struct timer_list *timer;
278 send_els = kzalloc(sizeof(struct zfcp_send_els), GFP_ATOMIC);
279 if (send_els == NULL)
280 goto nomem;
282 send_els->req = kzalloc(sizeof(struct scatterlist), GFP_ATOMIC);
283 if (send_els->req == NULL)
284 goto nomem;
286 send_els->resp = kzalloc(sizeof(struct scatterlist), GFP_ATOMIC);
287 if (send_els->resp == NULL)
288 goto nomem;
290 address = (void *) get_zeroed_page(GFP_ATOMIC);
291 if (address == NULL)
292 goto nomem;
294 zfcp_address_to_sg(address, send_els->req);
295 address += PAGE_SIZE >> 1;
296 zfcp_address_to_sg(address, send_els->resp);
297 send_els->req_count = send_els->resp_count = 1;
299 send_els->adapter = adapter;
300 send_els->port = port;
301 send_els->d_id = port->d_id;
302 send_els->handler = zfcp_erp_adisc_handler;
303 send_els->handler_data = (unsigned long) send_els;
305 adisc = zfcp_sg_to_address(send_els->req);
306 send_els->ls_code = adisc->code = ZFCP_LS_ADISC;
308 send_els->req->length = sizeof(struct zfcp_ls_adisc);
309 send_els->resp->length = sizeof(struct zfcp_ls_adisc_acc);
311 /* acc. to FC-FS, hard_nport_id in ADISC should not be set for ports
312 without FC-AL-2 capability, so we don't set it */
313 adisc->wwpn = fc_host_port_name(adapter->scsi_host);
314 adisc->wwnn = fc_host_node_name(adapter->scsi_host);
315 adisc->nport_id = fc_host_port_id(adapter->scsi_host);
316 ZFCP_LOG_INFO("ADISC request from s_id 0x%08x to d_id 0x%08x "
317 "(wwpn=0x%016Lx, wwnn=0x%016Lx, "
318 "hard_nport_id=0x%08x, nport_id=0x%08x)\n",
319 adisc->nport_id, send_els->d_id, (wwn_t) adisc->wwpn,
320 (wwn_t) adisc->wwnn, adisc->hard_nport_id,
321 adisc->nport_id);
323 timer = kmalloc(sizeof(struct timer_list), GFP_ATOMIC);
324 if (!timer)
325 goto nomem;
327 init_timer(timer);
328 timer->function = zfcp_fsf_request_timeout_handler;
329 timer->data = (unsigned long) adapter;
330 timer->expires = ZFCP_FSF_REQUEST_TIMEOUT;
331 send_els->timer = timer;
333 retval = zfcp_fsf_send_els(send_els);
334 if (retval != 0) {
335 ZFCP_LOG_NORMAL("error: initiation of Send ELS failed for port "
336 "0x%08x on adapter %s\n", send_els->d_id,
337 zfcp_get_busid_by_adapter(adapter));
338 del_timer(send_els->timer);
339 goto freemem;
342 goto out;
344 nomem:
345 retval = -ENOMEM;
346 freemem:
347 if (address != NULL)
348 __free_pages(send_els->req->page, 0);
349 if (send_els != NULL) {
350 kfree(send_els->timer);
351 kfree(send_els->req);
352 kfree(send_els->resp);
353 kfree(send_els);
355 out:
356 return retval;
361 * zfcp_erp_adisc_handler - handler for ADISC ELS command
362 * @data: pointer to struct zfcp_send_els
364 * If ADISC failed (LS_RJT or timed out) forced reopen of the port is triggered.
366 void
367 zfcp_erp_adisc_handler(unsigned long data)
369 struct zfcp_send_els *send_els;
370 struct zfcp_port *port;
371 struct zfcp_adapter *adapter;
372 u32 d_id;
373 struct zfcp_ls_adisc_acc *adisc;
375 send_els = (struct zfcp_send_els *) data;
377 del_timer(send_els->timer);
379 adapter = send_els->adapter;
380 port = send_els->port;
381 d_id = send_els->d_id;
383 /* request rejected or timed out */
384 if (send_els->status != 0) {
385 ZFCP_LOG_NORMAL("ELS request rejected/timed out, "
386 "force physical port reopen "
387 "(adapter %s, port d_id=0x%08x)\n",
388 zfcp_get_busid_by_adapter(adapter), d_id);
389 debug_text_event(adapter->erp_dbf, 3, "forcreop");
390 if (zfcp_erp_port_forced_reopen(port, 0))
391 ZFCP_LOG_NORMAL("failed reopen of port "
392 "(adapter %s, wwpn=0x%016Lx)\n",
393 zfcp_get_busid_by_port(port),
394 port->wwpn);
395 goto out;
398 adisc = zfcp_sg_to_address(send_els->resp);
400 ZFCP_LOG_INFO("ADISC response from d_id 0x%08x to s_id "
401 "0x%08x (wwpn=0x%016Lx, wwnn=0x%016Lx, "
402 "hard_nport_id=0x%08x, nport_id=0x%08x)\n",
403 d_id, fc_host_port_id(adapter->scsi_host),
404 (wwn_t) adisc->wwpn, (wwn_t) adisc->wwnn,
405 adisc->hard_nport_id, adisc->nport_id);
407 /* set wwnn for port */
408 if (port->wwnn == 0)
409 port->wwnn = adisc->wwnn;
411 if (port->wwpn != adisc->wwpn) {
412 ZFCP_LOG_NORMAL("d_id assignment changed, reopening "
413 "port (adapter %s, wwpn=0x%016Lx, "
414 "adisc_resp_wwpn=0x%016Lx)\n",
415 zfcp_get_busid_by_port(port),
416 port->wwpn, (wwn_t) adisc->wwpn);
417 if (zfcp_erp_port_reopen(port, 0))
418 ZFCP_LOG_NORMAL("failed reopen of port "
419 "(adapter %s, wwpn=0x%016Lx)\n",
420 zfcp_get_busid_by_port(port),
421 port->wwpn);
424 out:
425 zfcp_port_put(port);
426 __free_pages(send_els->req->page, 0);
427 kfree(send_els->timer);
428 kfree(send_els->req);
429 kfree(send_els->resp);
430 kfree(send_els);
435 * zfcp_test_link - lightweight link test procedure
436 * @port: port to be tested
438 * Test status of a link to a remote port using the ELS command ADISC.
441 zfcp_test_link(struct zfcp_port *port)
443 int retval;
445 zfcp_port_get(port);
446 retval = zfcp_erp_adisc(port);
447 if (retval != 0) {
448 zfcp_port_put(port);
449 ZFCP_LOG_NORMAL("reopen needed for port 0x%016Lx "
450 "on adapter %s\n ", port->wwpn,
451 zfcp_get_busid_by_port(port));
452 retval = zfcp_erp_port_forced_reopen(port, 0);
453 if (retval != 0) {
454 ZFCP_LOG_NORMAL("reopen of remote port 0x%016Lx "
455 "on adapter %s failed\n", port->wwpn,
456 zfcp_get_busid_by_port(port));
457 retval = -EPERM;
461 return retval;
466 * function:
468 * purpose: called if a port failed to be opened normally
469 * initiates Forced Reopen recovery which is done
470 * asynchronously
472 * returns: 0 - initiated action succesfully
473 * <0 - failed to initiate action
475 static int
476 zfcp_erp_port_forced_reopen_internal(struct zfcp_port *port, int clear_mask)
478 int retval;
479 struct zfcp_adapter *adapter = port->adapter;
481 debug_text_event(adapter->erp_dbf, 5, "pf_ro");
482 debug_event(adapter->erp_dbf, 5, &port->wwpn, sizeof (wwn_t));
484 ZFCP_LOG_DEBUG("forced reopen of port 0x%016Lx on adapter %s\n",
485 port->wwpn, zfcp_get_busid_by_port(port));
487 zfcp_erp_port_block(port, clear_mask);
489 if (atomic_test_mask(ZFCP_STATUS_COMMON_ERP_FAILED, &port->status)) {
490 ZFCP_LOG_DEBUG("skipped forced reopen of failed port 0x%016Lx "
491 "on adapter %s\n", port->wwpn,
492 zfcp_get_busid_by_port(port));
493 debug_text_event(adapter->erp_dbf, 5, "pf_ro_f");
494 debug_event(adapter->erp_dbf, 5, &port->wwpn, sizeof (wwn_t));
495 retval = -EIO;
496 goto out;
499 retval = zfcp_erp_action_enqueue(ZFCP_ERP_ACTION_REOPEN_PORT_FORCED,
500 port->adapter, port, NULL);
502 out:
503 return retval;
507 * function:
509 * purpose: Wrappper for zfcp_erp_port_forced_reopen_internal
510 * used to ensure the correct locking
512 * returns: 0 - initiated action succesfully
513 * <0 - failed to initiate action
516 zfcp_erp_port_forced_reopen(struct zfcp_port *port, int clear_mask)
518 int retval;
519 unsigned long flags;
520 struct zfcp_adapter *adapter;
522 adapter = port->adapter;
523 read_lock_irqsave(&zfcp_data.config_lock, flags);
524 write_lock(&adapter->erp_lock);
525 retval = zfcp_erp_port_forced_reopen_internal(port, clear_mask);
526 write_unlock(&adapter->erp_lock);
527 read_unlock_irqrestore(&zfcp_data.config_lock, flags);
529 return retval;
533 * function:
535 * purpose: called if a port is to be opened
536 * initiates Reopen recovery which is done
537 * asynchronously
539 * returns: 0 - initiated action succesfully
540 * <0 - failed to initiate action
542 static int
543 zfcp_erp_port_reopen_internal(struct zfcp_port *port, int clear_mask)
545 int retval;
546 struct zfcp_adapter *adapter = port->adapter;
548 debug_text_event(adapter->erp_dbf, 5, "p_ro");
549 debug_event(adapter->erp_dbf, 5, &port->wwpn, sizeof (wwn_t));
551 ZFCP_LOG_DEBUG("reopen of port 0x%016Lx on adapter %s\n",
552 port->wwpn, zfcp_get_busid_by_port(port));
554 zfcp_erp_port_block(port, clear_mask);
556 if (atomic_test_mask(ZFCP_STATUS_COMMON_ERP_FAILED, &port->status)) {
557 ZFCP_LOG_DEBUG("skipped reopen of failed port 0x%016Lx "
558 "on adapter %s\n", port->wwpn,
559 zfcp_get_busid_by_port(port));
560 debug_text_event(adapter->erp_dbf, 5, "p_ro_f");
561 debug_event(adapter->erp_dbf, 5, &port->wwpn, sizeof (wwn_t));
562 /* ensure propagation of failed status to new devices */
563 zfcp_erp_port_failed(port);
564 retval = -EIO;
565 goto out;
568 retval = zfcp_erp_action_enqueue(ZFCP_ERP_ACTION_REOPEN_PORT,
569 port->adapter, port, NULL);
571 out:
572 return retval;
576 * zfcp_erp_port_reopen - initiate reopen of a remote port
577 * @port: port to be reopened
578 * @clear_mask: specifies flags in port status to be cleared
579 * Return: 0 on success, < 0 on error
581 * This is a wrappper function for zfcp_erp_port_reopen_internal. It ensures
582 * correct locking. An error recovery task is initiated to do the reopen.
583 * To wait for the completion of the reopen zfcp_erp_wait should be used.
586 zfcp_erp_port_reopen(struct zfcp_port *port, int clear_mask)
588 int retval;
589 unsigned long flags;
590 struct zfcp_adapter *adapter = port->adapter;
592 read_lock_irqsave(&zfcp_data.config_lock, flags);
593 write_lock(&adapter->erp_lock);
594 retval = zfcp_erp_port_reopen_internal(port, clear_mask);
595 write_unlock(&adapter->erp_lock);
596 read_unlock_irqrestore(&zfcp_data.config_lock, flags);
598 return retval;
602 * function:
604 * purpose: called if a unit is to be opened
605 * initiates Reopen recovery which is done
606 * asynchronously
608 * returns: 0 - initiated action succesfully
609 * <0 - failed to initiate action
611 static int
612 zfcp_erp_unit_reopen_internal(struct zfcp_unit *unit, int clear_mask)
614 int retval;
615 struct zfcp_adapter *adapter = unit->port->adapter;
617 debug_text_event(adapter->erp_dbf, 5, "u_ro");
618 debug_event(adapter->erp_dbf, 5, &unit->fcp_lun, sizeof (fcp_lun_t));
619 ZFCP_LOG_DEBUG("reopen of unit 0x%016Lx on port 0x%016Lx "
620 "on adapter %s\n", unit->fcp_lun,
621 unit->port->wwpn, zfcp_get_busid_by_unit(unit));
623 zfcp_erp_unit_block(unit, clear_mask);
625 if (atomic_test_mask(ZFCP_STATUS_COMMON_ERP_FAILED, &unit->status)) {
626 ZFCP_LOG_DEBUG("skipped reopen of failed unit 0x%016Lx "
627 "on port 0x%016Lx on adapter %s\n",
628 unit->fcp_lun, unit->port->wwpn,
629 zfcp_get_busid_by_unit(unit));
630 debug_text_event(adapter->erp_dbf, 5, "u_ro_f");
631 debug_event(adapter->erp_dbf, 5, &unit->fcp_lun,
632 sizeof (fcp_lun_t));
633 retval = -EIO;
634 goto out;
637 retval = zfcp_erp_action_enqueue(ZFCP_ERP_ACTION_REOPEN_UNIT,
638 unit->port->adapter, unit->port, unit);
639 out:
640 return retval;
644 * zfcp_erp_unit_reopen - initiate reopen of a unit
645 * @unit: unit to be reopened
646 * @clear_mask: specifies flags in unit status to be cleared
647 * Return: 0 on success, < 0 on error
649 * This is a wrappper for zfcp_erp_unit_reopen_internal. It ensures correct
650 * locking. An error recovery task is initiated to do the reopen.
651 * To wait for the completion of the reopen zfcp_erp_wait should be used.
654 zfcp_erp_unit_reopen(struct zfcp_unit *unit, int clear_mask)
656 int retval;
657 unsigned long flags;
658 struct zfcp_adapter *adapter;
659 struct zfcp_port *port;
661 port = unit->port;
662 adapter = port->adapter;
664 read_lock_irqsave(&zfcp_data.config_lock, flags);
665 write_lock(&adapter->erp_lock);
666 retval = zfcp_erp_unit_reopen_internal(unit, clear_mask);
667 write_unlock(&adapter->erp_lock);
668 read_unlock_irqrestore(&zfcp_data.config_lock, flags);
670 return retval;
674 * function:
676 * purpose: disable I/O,
677 * return any open requests and clean them up,
678 * aim: no pending and incoming I/O
680 * returns:
682 static void
683 zfcp_erp_adapter_block(struct zfcp_adapter *adapter, int clear_mask)
685 debug_text_event(adapter->erp_dbf, 6, "a_bl");
686 zfcp_erp_modify_adapter_status(adapter,
687 ZFCP_STATUS_COMMON_UNBLOCKED |
688 clear_mask, ZFCP_CLEAR);
692 * function:
694 * purpose: enable I/O
696 * returns:
698 static void
699 zfcp_erp_adapter_unblock(struct zfcp_adapter *adapter)
701 debug_text_event(adapter->erp_dbf, 6, "a_ubl");
702 atomic_set_mask(ZFCP_STATUS_COMMON_UNBLOCKED, &adapter->status);
706 * function:
708 * purpose: disable I/O,
709 * return any open requests and clean them up,
710 * aim: no pending and incoming I/O
712 * returns:
714 static void
715 zfcp_erp_port_block(struct zfcp_port *port, int clear_mask)
717 struct zfcp_adapter *adapter = port->adapter;
719 debug_text_event(adapter->erp_dbf, 6, "p_bl");
720 debug_event(adapter->erp_dbf, 6, &port->wwpn, sizeof (wwn_t));
721 zfcp_erp_modify_port_status(port,
722 ZFCP_STATUS_COMMON_UNBLOCKED | clear_mask,
723 ZFCP_CLEAR);
727 * function:
729 * purpose: enable I/O
731 * returns:
733 static void
734 zfcp_erp_port_unblock(struct zfcp_port *port)
736 struct zfcp_adapter *adapter = port->adapter;
738 debug_text_event(adapter->erp_dbf, 6, "p_ubl");
739 debug_event(adapter->erp_dbf, 6, &port->wwpn, sizeof (wwn_t));
740 atomic_set_mask(ZFCP_STATUS_COMMON_UNBLOCKED, &port->status);
744 * function:
746 * purpose: disable I/O,
747 * return any open requests and clean them up,
748 * aim: no pending and incoming I/O
750 * returns:
752 static void
753 zfcp_erp_unit_block(struct zfcp_unit *unit, int clear_mask)
755 struct zfcp_adapter *adapter = unit->port->adapter;
757 debug_text_event(adapter->erp_dbf, 6, "u_bl");
758 debug_event(adapter->erp_dbf, 6, &unit->fcp_lun, sizeof (fcp_lun_t));
759 zfcp_erp_modify_unit_status(unit,
760 ZFCP_STATUS_COMMON_UNBLOCKED | clear_mask,
761 ZFCP_CLEAR);
765 * function:
767 * purpose: enable I/O
769 * returns:
771 static void
772 zfcp_erp_unit_unblock(struct zfcp_unit *unit)
774 struct zfcp_adapter *adapter = unit->port->adapter;
776 debug_text_event(adapter->erp_dbf, 6, "u_ubl");
777 debug_event(adapter->erp_dbf, 6, &unit->fcp_lun, sizeof (fcp_lun_t));
778 atomic_set_mask(ZFCP_STATUS_COMMON_UNBLOCKED, &unit->status);
781 static void
782 zfcp_erp_action_ready(struct zfcp_erp_action *erp_action)
784 struct zfcp_adapter *adapter = erp_action->adapter;
786 debug_text_event(adapter->erp_dbf, 4, "a_ar");
787 debug_event(adapter->erp_dbf, 4, &erp_action->action, sizeof (int));
789 zfcp_erp_action_to_ready(erp_action);
790 up(&adapter->erp_ready_sem);
794 * function:
796 * purpose:
798 * returns: <0 erp_action not found in any list
799 * ZFCP_ERP_ACTION_READY erp_action is in ready list
800 * ZFCP_ERP_ACTION_RUNNING erp_action is in running list
802 * locks: erp_lock must be held
804 static int
805 zfcp_erp_action_exists(struct zfcp_erp_action *erp_action)
807 int retval = -EINVAL;
808 struct list_head *entry;
809 struct zfcp_erp_action *entry_erp_action;
810 struct zfcp_adapter *adapter = erp_action->adapter;
812 /* search in running list */
813 list_for_each(entry, &adapter->erp_running_head) {
814 entry_erp_action =
815 list_entry(entry, struct zfcp_erp_action, list);
816 if (entry_erp_action == erp_action) {
817 retval = ZFCP_ERP_ACTION_RUNNING;
818 goto out;
821 /* search in ready list */
822 list_for_each(entry, &adapter->erp_ready_head) {
823 entry_erp_action =
824 list_entry(entry, struct zfcp_erp_action, list);
825 if (entry_erp_action == erp_action) {
826 retval = ZFCP_ERP_ACTION_READY;
827 goto out;
831 out:
832 return retval;
836 * purpose: checks current status of action (timed out, dismissed, ...)
837 * and does appropriate preparations (dismiss fsf request, ...)
839 * locks: called under erp_lock (disabled interrupts)
841 * returns: 0
843 static int
844 zfcp_erp_strategy_check_fsfreq(struct zfcp_erp_action *erp_action)
846 int retval = 0;
847 struct zfcp_fsf_req *fsf_req = NULL;
848 struct zfcp_adapter *adapter = erp_action->adapter;
850 if (erp_action->fsf_req) {
851 /* take lock to ensure that request is not being deleted meanwhile */
852 spin_lock(&adapter->fsf_req_list_lock);
853 /* check whether fsf req does still exist */
854 list_for_each_entry(fsf_req, &adapter->fsf_req_list_head, list)
855 if (fsf_req == erp_action->fsf_req)
856 break;
857 if (fsf_req && (fsf_req->erp_action == erp_action)) {
858 /* fsf_req still exists */
859 debug_text_event(adapter->erp_dbf, 3, "a_ca_req");
860 debug_event(adapter->erp_dbf, 3, &fsf_req,
861 sizeof (unsigned long));
862 /* dismiss fsf_req of timed out or dismissed erp_action */
863 if (erp_action->status & (ZFCP_STATUS_ERP_DISMISSED |
864 ZFCP_STATUS_ERP_TIMEDOUT)) {
865 debug_text_event(adapter->erp_dbf, 3,
866 "a_ca_disreq");
867 fsf_req->status |= ZFCP_STATUS_FSFREQ_DISMISSED;
869 if (erp_action->status & ZFCP_STATUS_ERP_TIMEDOUT) {
870 ZFCP_LOG_NORMAL("error: erp step timed out "
871 "(action=%d, fsf_req=%p)\n ",
872 erp_action->action,
873 erp_action->fsf_req);
876 * If fsf_req is neither dismissed nor completed
877 * then keep it running asynchronously and don't mess
878 * with the association of erp_action and fsf_req.
880 if (fsf_req->status & (ZFCP_STATUS_FSFREQ_COMPLETED |
881 ZFCP_STATUS_FSFREQ_DISMISSED)) {
882 /* forget about association between fsf_req
883 and erp_action */
884 fsf_req->erp_action = NULL;
885 erp_action->fsf_req = NULL;
887 } else {
888 debug_text_event(adapter->erp_dbf, 3, "a_ca_gonereq");
890 * even if this fsf_req has gone, forget about
891 * association between erp_action and fsf_req
893 erp_action->fsf_req = NULL;
895 spin_unlock(&adapter->fsf_req_list_lock);
896 } else
897 debug_text_event(adapter->erp_dbf, 3, "a_ca_noreq");
899 return retval;
903 * purpose: generic handler for asynchronous events related to erp_action events
904 * (normal completion, time-out, dismissing, retry after
905 * low memory condition)
907 * note: deletion of timer is not required (e.g. in case of a time-out),
908 * but a second try does no harm,
909 * we leave it in here to allow for greater simplification
911 * returns: 0 - there was an action to handle
912 * !0 - otherwise
914 static int
915 zfcp_erp_async_handler_nolock(struct zfcp_erp_action *erp_action,
916 unsigned long set_mask)
918 int retval;
919 struct zfcp_adapter *adapter = erp_action->adapter;
921 if (zfcp_erp_action_exists(erp_action) == ZFCP_ERP_ACTION_RUNNING) {
922 debug_text_event(adapter->erp_dbf, 2, "a_asyh_ex");
923 debug_event(adapter->erp_dbf, 2, &erp_action->action,
924 sizeof (int));
925 if (!(set_mask & ZFCP_STATUS_ERP_TIMEDOUT))
926 del_timer(&erp_action->timer);
927 erp_action->status |= set_mask;
928 zfcp_erp_action_ready(erp_action);
929 retval = 0;
930 } else {
931 /* action is ready or gone - nothing to do */
932 debug_text_event(adapter->erp_dbf, 3, "a_asyh_gone");
933 debug_event(adapter->erp_dbf, 3, &erp_action->action,
934 sizeof (int));
935 retval = 1;
938 return retval;
942 * purpose: generic handler for asynchronous events related to erp_action
943 * events (normal completion, time-out, dismissing, retry after
944 * low memory condition)
946 * note: deletion of timer is not required (e.g. in case of a time-out),
947 * but a second try does no harm,
948 * we leave it in here to allow for greater simplification
950 * returns: 0 - there was an action to handle
951 * !0 - otherwise
954 zfcp_erp_async_handler(struct zfcp_erp_action *erp_action,
955 unsigned long set_mask)
957 struct zfcp_adapter *adapter = erp_action->adapter;
958 unsigned long flags;
959 int retval;
961 write_lock_irqsave(&adapter->erp_lock, flags);
962 retval = zfcp_erp_async_handler_nolock(erp_action, set_mask);
963 write_unlock_irqrestore(&adapter->erp_lock, flags);
965 return retval;
969 * purpose: is called for erp_action which was slept waiting for
970 * memory becoming avaliable,
971 * will trigger that this action will be continued
973 static void
974 zfcp_erp_memwait_handler(unsigned long data)
976 struct zfcp_erp_action *erp_action = (struct zfcp_erp_action *) data;
977 struct zfcp_adapter *adapter = erp_action->adapter;
979 debug_text_event(adapter->erp_dbf, 2, "a_mwh");
980 debug_event(adapter->erp_dbf, 2, &erp_action->action, sizeof (int));
982 zfcp_erp_async_handler(erp_action, 0);
986 * purpose: is called if an asynchronous erp step timed out,
987 * action gets an appropriate flag and will be processed
988 * accordingly
990 static void
991 zfcp_erp_timeout_handler(unsigned long data)
993 struct zfcp_erp_action *erp_action = (struct zfcp_erp_action *) data;
994 struct zfcp_adapter *adapter = erp_action->adapter;
996 debug_text_event(adapter->erp_dbf, 2, "a_th");
997 debug_event(adapter->erp_dbf, 2, &erp_action->action, sizeof (int));
999 zfcp_erp_async_handler(erp_action, ZFCP_STATUS_ERP_TIMEDOUT);
1003 * purpose: is called for an erp_action which needs to be ended
1004 * though not being done,
1005 * this is usually required if an higher is generated,
1006 * action gets an appropriate flag and will be processed
1007 * accordingly
1009 * locks: erp_lock held (thus we need to call another handler variant)
1011 static int
1012 zfcp_erp_action_dismiss(struct zfcp_erp_action *erp_action)
1014 struct zfcp_adapter *adapter = erp_action->adapter;
1016 debug_text_event(adapter->erp_dbf, 2, "a_adis");
1017 debug_event(adapter->erp_dbf, 2, &erp_action->action, sizeof (int));
1019 zfcp_erp_async_handler_nolock(erp_action, ZFCP_STATUS_ERP_DISMISSED);
1021 return 0;
1025 zfcp_erp_thread_setup(struct zfcp_adapter *adapter)
1027 int retval = 0;
1029 atomic_clear_mask(ZFCP_STATUS_ADAPTER_ERP_THREAD_UP, &adapter->status);
1031 retval = kernel_thread(zfcp_erp_thread, adapter, SIGCHLD);
1032 if (retval < 0) {
1033 ZFCP_LOG_NORMAL("error: creation of erp thread failed for "
1034 "adapter %s\n",
1035 zfcp_get_busid_by_adapter(adapter));
1036 debug_text_event(adapter->erp_dbf, 5, "a_thset_fail");
1037 } else {
1038 wait_event(adapter->erp_thread_wqh,
1039 atomic_test_mask(ZFCP_STATUS_ADAPTER_ERP_THREAD_UP,
1040 &adapter->status));
1041 debug_text_event(adapter->erp_dbf, 5, "a_thset_ok");
1044 return (retval < 0);
1048 * function:
1050 * purpose:
1052 * returns:
1054 * context: process (i.e. proc-fs or rmmod/insmod)
1056 * note: The caller of this routine ensures that the specified
1057 * adapter has been shut down and that this operation
1058 * has been completed. Thus, there are no pending erp_actions
1059 * which would need to be handled here.
1062 zfcp_erp_thread_kill(struct zfcp_adapter *adapter)
1064 int retval = 0;
1066 atomic_set_mask(ZFCP_STATUS_ADAPTER_ERP_THREAD_KILL, &adapter->status);
1067 up(&adapter->erp_ready_sem);
1069 wait_event(adapter->erp_thread_wqh,
1070 !atomic_test_mask(ZFCP_STATUS_ADAPTER_ERP_THREAD_UP,
1071 &adapter->status));
1073 atomic_clear_mask(ZFCP_STATUS_ADAPTER_ERP_THREAD_KILL,
1074 &adapter->status);
1076 debug_text_event(adapter->erp_dbf, 5, "a_thki_ok");
1078 return retval;
1082 * purpose: is run as a kernel thread,
1083 * goes through list of error recovery actions of associated adapter
1084 * and delegates single action to execution
1086 * returns: 0
1088 static int
1089 zfcp_erp_thread(void *data)
1091 struct zfcp_adapter *adapter = (struct zfcp_adapter *) data;
1092 struct list_head *next;
1093 struct zfcp_erp_action *erp_action;
1094 unsigned long flags;
1096 daemonize("zfcperp%s", zfcp_get_busid_by_adapter(adapter));
1097 /* Block all signals */
1098 siginitsetinv(&current->blocked, 0);
1099 atomic_set_mask(ZFCP_STATUS_ADAPTER_ERP_THREAD_UP, &adapter->status);
1100 debug_text_event(adapter->erp_dbf, 5, "a_th_run");
1101 wake_up(&adapter->erp_thread_wqh);
1103 while (!atomic_test_mask(ZFCP_STATUS_ADAPTER_ERP_THREAD_KILL,
1104 &adapter->status)) {
1106 write_lock_irqsave(&adapter->erp_lock, flags);
1107 next = adapter->erp_ready_head.prev;
1108 write_unlock_irqrestore(&adapter->erp_lock, flags);
1110 if (next != &adapter->erp_ready_head) {
1111 erp_action =
1112 list_entry(next, struct zfcp_erp_action, list);
1114 * process action (incl. [re]moving it
1115 * from 'ready' queue)
1117 zfcp_erp_strategy(erp_action);
1121 * sleep as long as there is nothing to do, i.e.
1122 * no action in 'ready' queue to be processed and
1123 * thread is not to be killed
1125 down_interruptible(&adapter->erp_ready_sem);
1126 debug_text_event(adapter->erp_dbf, 5, "a_th_woken");
1129 atomic_clear_mask(ZFCP_STATUS_ADAPTER_ERP_THREAD_UP, &adapter->status);
1130 debug_text_event(adapter->erp_dbf, 5, "a_th_stop");
1131 wake_up(&adapter->erp_thread_wqh);
1133 return 0;
1137 * function:
1139 * purpose: drives single error recovery action and schedules higher and
1140 * subordinate actions, if necessary
1142 * returns: ZFCP_ERP_CONTINUES - action continues (asynchronously)
1143 * ZFCP_ERP_SUCCEEDED - action finished successfully (deqd)
1144 * ZFCP_ERP_FAILED - action finished unsuccessfully (deqd)
1145 * ZFCP_ERP_EXIT - action finished (dequeued), offline
1146 * ZFCP_ERP_DISMISSED - action canceled (dequeued)
1148 static int
1149 zfcp_erp_strategy(struct zfcp_erp_action *erp_action)
1151 int retval = 0;
1152 struct zfcp_adapter *adapter = erp_action->adapter;
1153 struct zfcp_port *port = erp_action->port;
1154 struct zfcp_unit *unit = erp_action->unit;
1155 int action = erp_action->action;
1156 u32 status = erp_action->status;
1157 unsigned long flags;
1159 /* serialise dismissing, timing out, moving, enqueueing */
1160 read_lock_irqsave(&zfcp_data.config_lock, flags);
1161 write_lock(&adapter->erp_lock);
1163 /* dequeue dismissed action and leave, if required */
1164 retval = zfcp_erp_strategy_check_action(erp_action, retval);
1165 if (retval == ZFCP_ERP_DISMISSED) {
1166 debug_text_event(adapter->erp_dbf, 4, "a_st_dis1");
1167 goto unlock;
1171 * move action to 'running' queue before processing it
1172 * (to avoid a race condition regarding moving the
1173 * action to the 'running' queue and back)
1175 zfcp_erp_action_to_running(erp_action);
1178 * try to process action as far as possible,
1179 * no lock to allow for blocking operations (kmalloc, qdio, ...),
1180 * afterwards the lock is required again for the following reasons:
1181 * - dequeueing of finished action and enqueueing of
1182 * follow-up actions must be atomic so that any other
1183 * reopen-routine does not believe there is nothing to do
1184 * and that it is safe to enqueue something else,
1185 * - we want to force any control thread which is dismissing
1186 * actions to finish this before we decide about
1187 * necessary steps to be taken here further
1189 write_unlock(&adapter->erp_lock);
1190 read_unlock_irqrestore(&zfcp_data.config_lock, flags);
1191 retval = zfcp_erp_strategy_do_action(erp_action);
1192 read_lock_irqsave(&zfcp_data.config_lock, flags);
1193 write_lock(&adapter->erp_lock);
1196 * check for dismissed status again to avoid follow-up actions,
1197 * failing of targets and so on for dismissed actions
1199 retval = zfcp_erp_strategy_check_action(erp_action, retval);
1201 switch (retval) {
1202 case ZFCP_ERP_DISMISSED:
1203 /* leave since this action has ridden to its ancestors */
1204 debug_text_event(adapter->erp_dbf, 6, "a_st_dis2");
1205 goto unlock;
1206 case ZFCP_ERP_NOMEM:
1207 /* no memory to continue immediately, let it sleep */
1208 if (!(erp_action->status & ZFCP_STATUS_ERP_LOWMEM)) {
1209 ++adapter->erp_low_mem_count;
1210 erp_action->status |= ZFCP_STATUS_ERP_LOWMEM;
1212 /* This condition is true if there is no memory available
1213 for any erp_action on this adapter. This implies that there
1214 are no elements in the memory pool(s) left for erp_actions.
1215 This might happen if an erp_action that used a memory pool
1216 element was timed out.
1218 if (adapter->erp_total_count == adapter->erp_low_mem_count) {
1219 debug_text_event(adapter->erp_dbf, 3, "a_st_lowmem");
1220 ZFCP_LOG_NORMAL("error: no mempool elements available, "
1221 "restarting I/O on adapter %s "
1222 "to free mempool\n",
1223 zfcp_get_busid_by_adapter(adapter));
1224 zfcp_erp_adapter_reopen_internal(adapter, 0);
1225 } else {
1226 debug_text_event(adapter->erp_dbf, 2, "a_st_memw");
1227 retval = zfcp_erp_strategy_memwait(erp_action);
1229 goto unlock;
1230 case ZFCP_ERP_CONTINUES:
1231 /* leave since this action runs asynchronously */
1232 debug_text_event(adapter->erp_dbf, 6, "a_st_cont");
1233 if (erp_action->status & ZFCP_STATUS_ERP_LOWMEM) {
1234 --adapter->erp_low_mem_count;
1235 erp_action->status &= ~ZFCP_STATUS_ERP_LOWMEM;
1237 goto unlock;
1239 /* ok, finished action (whatever its result is) */
1241 /* check for unrecoverable targets */
1242 retval = zfcp_erp_strategy_check_target(erp_action, retval);
1244 /* action must be dequeued (here to allow for further ones) */
1245 zfcp_erp_action_dequeue(erp_action);
1248 * put this target through the erp mill again if someone has
1249 * requested to change the status of a target being online
1250 * to offline or the other way around
1251 * (old retval is preserved if nothing has to be done here)
1253 retval = zfcp_erp_strategy_statechange(action, status, adapter,
1254 port, unit, retval);
1257 * leave if target is in permanent error state or if
1258 * action is repeated in order to process state change
1260 if (retval == ZFCP_ERP_EXIT) {
1261 debug_text_event(adapter->erp_dbf, 2, "a_st_exit");
1262 goto unlock;
1265 /* trigger follow up actions */
1266 zfcp_erp_strategy_followup_actions(action, adapter, port, unit, retval);
1268 unlock:
1269 write_unlock(&adapter->erp_lock);
1270 read_unlock_irqrestore(&zfcp_data.config_lock, flags);
1272 if (retval != ZFCP_ERP_CONTINUES)
1273 zfcp_erp_action_cleanup(action, adapter, port, unit, retval);
1276 * a few tasks remain when the erp queues are empty
1277 * (don't do that if the last action evaluated was dismissed
1278 * since this clearly indicates that there is more to come) :
1279 * - close the name server port if it is open yet
1280 * (enqueues another [probably] final action)
1281 * - otherwise, wake up whoever wants to be woken when we are
1282 * done with erp
1284 if (retval != ZFCP_ERP_DISMISSED)
1285 zfcp_erp_strategy_check_queues(adapter);
1287 debug_text_event(adapter->erp_dbf, 6, "a_st_done");
1289 return retval;
1293 * function:
1295 * purpose:
1297 * returns: ZFCP_ERP_DISMISSED - if action has been dismissed
1298 * retval - otherwise
1300 static int
1301 zfcp_erp_strategy_check_action(struct zfcp_erp_action *erp_action, int retval)
1303 struct zfcp_adapter *adapter = erp_action->adapter;
1305 zfcp_erp_strategy_check_fsfreq(erp_action);
1307 debug_event(adapter->erp_dbf, 5, &erp_action->action, sizeof (int));
1308 if (erp_action->status & ZFCP_STATUS_ERP_DISMISSED) {
1309 debug_text_event(adapter->erp_dbf, 3, "a_stcd_dis");
1310 zfcp_erp_action_dequeue(erp_action);
1311 retval = ZFCP_ERP_DISMISSED;
1312 } else
1313 debug_text_event(adapter->erp_dbf, 5, "a_stcd_nodis");
1315 return retval;
1318 static int
1319 zfcp_erp_strategy_do_action(struct zfcp_erp_action *erp_action)
1321 int retval = ZFCP_ERP_FAILED;
1322 struct zfcp_adapter *adapter = erp_action->adapter;
1325 * try to execute/continue action as far as possible,
1326 * note: no lock in subsequent strategy routines
1327 * (this allows these routine to call schedule, e.g.
1328 * kmalloc with such flags or qdio_initialize & friends)
1329 * Note: in case of timeout, the seperate strategies will fail
1330 * anyhow. No need for a special action. Even worse, a nameserver
1331 * failure would not wake up waiting ports without the call.
1333 switch (erp_action->action) {
1335 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
1336 retval = zfcp_erp_adapter_strategy(erp_action);
1337 break;
1339 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
1340 retval = zfcp_erp_port_forced_strategy(erp_action);
1341 break;
1343 case ZFCP_ERP_ACTION_REOPEN_PORT:
1344 retval = zfcp_erp_port_strategy(erp_action);
1345 break;
1347 case ZFCP_ERP_ACTION_REOPEN_UNIT:
1348 retval = zfcp_erp_unit_strategy(erp_action);
1349 break;
1351 default:
1352 debug_text_exception(adapter->erp_dbf, 1, "a_stda_bug");
1353 debug_event(adapter->erp_dbf, 1, &erp_action->action,
1354 sizeof (int));
1355 ZFCP_LOG_NORMAL("bug: unknown erp action requested on "
1356 "adapter %s (action=%d)\n",
1357 zfcp_get_busid_by_adapter(erp_action->adapter),
1358 erp_action->action);
1361 return retval;
1365 * function:
1367 * purpose: triggers retry of this action after a certain amount of time
1368 * by means of timer provided by erp_action
1370 * returns: ZFCP_ERP_CONTINUES - erp_action sleeps in erp running queue
1372 static int
1373 zfcp_erp_strategy_memwait(struct zfcp_erp_action *erp_action)
1375 int retval = ZFCP_ERP_CONTINUES;
1376 struct zfcp_adapter *adapter = erp_action->adapter;
1378 debug_text_event(adapter->erp_dbf, 6, "a_mwinit");
1379 debug_event(adapter->erp_dbf, 6, &erp_action->action, sizeof (int));
1380 init_timer(&erp_action->timer);
1381 erp_action->timer.function = zfcp_erp_memwait_handler;
1382 erp_action->timer.data = (unsigned long) erp_action;
1383 erp_action->timer.expires = jiffies + ZFCP_ERP_MEMWAIT_TIMEOUT;
1384 add_timer(&erp_action->timer);
1386 return retval;
1390 * function: zfcp_erp_adapter_failed
1392 * purpose: sets the adapter and all underlying devices to ERP_FAILED
1395 void
1396 zfcp_erp_adapter_failed(struct zfcp_adapter *adapter)
1398 zfcp_erp_modify_adapter_status(adapter,
1399 ZFCP_STATUS_COMMON_ERP_FAILED, ZFCP_SET);
1400 ZFCP_LOG_NORMAL("adapter erp failed on adapter %s\n",
1401 zfcp_get_busid_by_adapter(adapter));
1402 debug_text_event(adapter->erp_dbf, 2, "a_afail");
1406 * function: zfcp_erp_port_failed
1408 * purpose: sets the port and all underlying devices to ERP_FAILED
1411 void
1412 zfcp_erp_port_failed(struct zfcp_port *port)
1414 zfcp_erp_modify_port_status(port,
1415 ZFCP_STATUS_COMMON_ERP_FAILED, ZFCP_SET);
1417 if (atomic_test_mask(ZFCP_STATUS_PORT_WKA, &port->status))
1418 ZFCP_LOG_NORMAL("port erp failed (adapter %s, "
1419 "port d_id=0x%08x)\n",
1420 zfcp_get_busid_by_port(port), port->d_id);
1421 else
1422 ZFCP_LOG_NORMAL("port erp failed (adapter %s, wwpn=0x%016Lx)\n",
1423 zfcp_get_busid_by_port(port), port->wwpn);
1425 debug_text_event(port->adapter->erp_dbf, 2, "p_pfail");
1426 debug_event(port->adapter->erp_dbf, 2, &port->wwpn, sizeof (wwn_t));
1430 * function: zfcp_erp_unit_failed
1432 * purpose: sets the unit to ERP_FAILED
1435 void
1436 zfcp_erp_unit_failed(struct zfcp_unit *unit)
1438 zfcp_erp_modify_unit_status(unit,
1439 ZFCP_STATUS_COMMON_ERP_FAILED, ZFCP_SET);
1441 ZFCP_LOG_NORMAL("unit erp failed on unit 0x%016Lx on port 0x%016Lx "
1442 " on adapter %s\n", unit->fcp_lun,
1443 unit->port->wwpn, zfcp_get_busid_by_unit(unit));
1444 debug_text_event(unit->port->adapter->erp_dbf, 2, "u_ufail");
1445 debug_event(unit->port->adapter->erp_dbf, 2,
1446 &unit->fcp_lun, sizeof (fcp_lun_t));
1450 * function: zfcp_erp_strategy_check_target
1452 * purpose: increments the erp action count on the device currently in
1453 * recovery if the action failed or resets the count in case of
1454 * success. If a maximum count is exceeded the device is marked
1455 * as ERP_FAILED.
1456 * The 'blocked' state of a target which has been recovered
1457 * successfully is reset.
1459 * returns: ZFCP_ERP_CONTINUES - action continues (not considered)
1460 * ZFCP_ERP_SUCCEEDED - action finished successfully
1461 * ZFCP_ERP_EXIT - action failed and will not continue
1463 static int
1464 zfcp_erp_strategy_check_target(struct zfcp_erp_action *erp_action, int result)
1466 struct zfcp_adapter *adapter = erp_action->adapter;
1467 struct zfcp_port *port = erp_action->port;
1468 struct zfcp_unit *unit = erp_action->unit;
1470 debug_text_event(adapter->erp_dbf, 5, "a_stct_norm");
1471 debug_event(adapter->erp_dbf, 5, &erp_action->action, sizeof (int));
1472 debug_event(adapter->erp_dbf, 5, &result, sizeof (int));
1474 switch (erp_action->action) {
1476 case ZFCP_ERP_ACTION_REOPEN_UNIT:
1477 result = zfcp_erp_strategy_check_unit(unit, result);
1478 break;
1480 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
1481 case ZFCP_ERP_ACTION_REOPEN_PORT:
1482 result = zfcp_erp_strategy_check_port(port, result);
1483 break;
1485 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
1486 result = zfcp_erp_strategy_check_adapter(adapter, result);
1487 break;
1490 return result;
1493 static int
1494 zfcp_erp_strategy_statechange(int action,
1495 u32 status,
1496 struct zfcp_adapter *adapter,
1497 struct zfcp_port *port,
1498 struct zfcp_unit *unit, int retval)
1500 debug_text_event(adapter->erp_dbf, 3, "a_stsc");
1501 debug_event(adapter->erp_dbf, 3, &action, sizeof (int));
1503 switch (action) {
1505 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
1506 if (zfcp_erp_strategy_statechange_detected(&adapter->status,
1507 status)) {
1508 zfcp_erp_adapter_reopen_internal(adapter, ZFCP_STATUS_COMMON_ERP_FAILED);
1509 retval = ZFCP_ERP_EXIT;
1511 break;
1513 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
1514 case ZFCP_ERP_ACTION_REOPEN_PORT:
1515 if (zfcp_erp_strategy_statechange_detected(&port->status,
1516 status)) {
1517 zfcp_erp_port_reopen_internal(port, ZFCP_STATUS_COMMON_ERP_FAILED);
1518 retval = ZFCP_ERP_EXIT;
1520 break;
1522 case ZFCP_ERP_ACTION_REOPEN_UNIT:
1523 if (zfcp_erp_strategy_statechange_detected(&unit->status,
1524 status)) {
1525 zfcp_erp_unit_reopen_internal(unit, ZFCP_STATUS_COMMON_ERP_FAILED);
1526 retval = ZFCP_ERP_EXIT;
1528 break;
1531 return retval;
1534 static inline int
1535 zfcp_erp_strategy_statechange_detected(atomic_t * target_status, u32 erp_status)
1537 return
1538 /* take it online */
1539 (atomic_test_mask(ZFCP_STATUS_COMMON_RUNNING, target_status) &&
1540 (ZFCP_STATUS_ERP_CLOSE_ONLY & erp_status)) ||
1541 /* take it offline */
1542 (!atomic_test_mask(ZFCP_STATUS_COMMON_RUNNING, target_status) &&
1543 !(ZFCP_STATUS_ERP_CLOSE_ONLY & erp_status));
1546 static int
1547 zfcp_erp_strategy_check_unit(struct zfcp_unit *unit, int result)
1549 debug_text_event(unit->port->adapter->erp_dbf, 5, "u_stct");
1550 debug_event(unit->port->adapter->erp_dbf, 5, &unit->fcp_lun,
1551 sizeof (fcp_lun_t));
1553 switch (result) {
1554 case ZFCP_ERP_SUCCEEDED :
1555 atomic_set(&unit->erp_counter, 0);
1556 zfcp_erp_unit_unblock(unit);
1557 break;
1558 case ZFCP_ERP_FAILED :
1559 atomic_inc(&unit->erp_counter);
1560 if (atomic_read(&unit->erp_counter) > ZFCP_MAX_ERPS)
1561 zfcp_erp_unit_failed(unit);
1562 break;
1563 case ZFCP_ERP_EXIT :
1564 /* nothing */
1565 break;
1568 if (atomic_test_mask(ZFCP_STATUS_COMMON_ERP_FAILED, &unit->status)) {
1569 zfcp_erp_unit_block(unit, 0); /* for ZFCP_ERP_SUCCEEDED */
1570 result = ZFCP_ERP_EXIT;
1573 return result;
1576 static int
1577 zfcp_erp_strategy_check_port(struct zfcp_port *port, int result)
1579 debug_text_event(port->adapter->erp_dbf, 5, "p_stct");
1580 debug_event(port->adapter->erp_dbf, 5, &port->wwpn, sizeof (wwn_t));
1582 switch (result) {
1583 case ZFCP_ERP_SUCCEEDED :
1584 atomic_set(&port->erp_counter, 0);
1585 zfcp_erp_port_unblock(port);
1586 break;
1587 case ZFCP_ERP_FAILED :
1588 atomic_inc(&port->erp_counter);
1589 if (atomic_read(&port->erp_counter) > ZFCP_MAX_ERPS)
1590 zfcp_erp_port_failed(port);
1591 break;
1592 case ZFCP_ERP_EXIT :
1593 /* nothing */
1594 break;
1597 if (atomic_test_mask(ZFCP_STATUS_COMMON_ERP_FAILED, &port->status)) {
1598 zfcp_erp_port_block(port, 0); /* for ZFCP_ERP_SUCCEEDED */
1599 result = ZFCP_ERP_EXIT;
1602 return result;
1605 static int
1606 zfcp_erp_strategy_check_adapter(struct zfcp_adapter *adapter, int result)
1608 debug_text_event(adapter->erp_dbf, 5, "a_stct");
1610 switch (result) {
1611 case ZFCP_ERP_SUCCEEDED :
1612 atomic_set(&adapter->erp_counter, 0);
1613 zfcp_erp_adapter_unblock(adapter);
1614 break;
1615 case ZFCP_ERP_FAILED :
1616 atomic_inc(&adapter->erp_counter);
1617 if (atomic_read(&adapter->erp_counter) > ZFCP_MAX_ERPS)
1618 zfcp_erp_adapter_failed(adapter);
1619 break;
1620 case ZFCP_ERP_EXIT :
1621 /* nothing */
1622 break;
1625 if (atomic_test_mask(ZFCP_STATUS_COMMON_ERP_FAILED, &adapter->status)) {
1626 zfcp_erp_adapter_block(adapter, 0); /* for ZFCP_ERP_SUCCEEDED */
1627 result = ZFCP_ERP_EXIT;
1630 return result;
1634 * function:
1636 * purpose: remaining things in good cases,
1637 * escalation in bad cases
1639 * returns:
1641 static int
1642 zfcp_erp_strategy_followup_actions(int action,
1643 struct zfcp_adapter *adapter,
1644 struct zfcp_port *port,
1645 struct zfcp_unit *unit, int status)
1647 debug_text_event(adapter->erp_dbf, 5, "a_stfol");
1648 debug_event(adapter->erp_dbf, 5, &action, sizeof (int));
1650 /* initiate follow-up actions depending on success of finished action */
1651 switch (action) {
1653 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
1654 if (status == ZFCP_ERP_SUCCEEDED)
1655 zfcp_erp_port_reopen_all_internal(adapter, 0);
1656 else
1657 zfcp_erp_adapter_reopen_internal(adapter, 0);
1658 break;
1660 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
1661 if (status == ZFCP_ERP_SUCCEEDED)
1662 zfcp_erp_port_reopen_internal(port, 0);
1663 else
1664 zfcp_erp_adapter_reopen_internal(adapter, 0);
1665 break;
1667 case ZFCP_ERP_ACTION_REOPEN_PORT:
1668 if (status == ZFCP_ERP_SUCCEEDED)
1669 zfcp_erp_unit_reopen_all_internal(port, 0);
1670 else
1671 zfcp_erp_port_forced_reopen_internal(port, 0);
1672 break;
1674 case ZFCP_ERP_ACTION_REOPEN_UNIT:
1675 if (status == ZFCP_ERP_SUCCEEDED) ; /* no further action */
1676 else
1677 zfcp_erp_port_reopen_internal(unit->port, 0);
1678 break;
1681 return 0;
1684 static int
1685 zfcp_erp_strategy_check_queues(struct zfcp_adapter *adapter)
1687 unsigned long flags;
1689 read_lock_irqsave(&zfcp_data.config_lock, flags);
1690 read_lock(&adapter->erp_lock);
1691 if (list_empty(&adapter->erp_ready_head) &&
1692 list_empty(&adapter->erp_running_head)) {
1693 debug_text_event(adapter->erp_dbf, 4, "a_cq_wake");
1694 atomic_clear_mask(ZFCP_STATUS_ADAPTER_ERP_PENDING,
1695 &adapter->status);
1696 wake_up(&adapter->erp_done_wqh);
1697 } else
1698 debug_text_event(adapter->erp_dbf, 5, "a_cq_notempty");
1699 read_unlock(&adapter->erp_lock);
1700 read_unlock_irqrestore(&zfcp_data.config_lock, flags);
1702 return 0;
1706 * zfcp_erp_wait - wait for completion of error recovery on an adapter
1707 * @adapter: adapter for which to wait for completion of its error recovery
1708 * Return: 0
1711 zfcp_erp_wait(struct zfcp_adapter *adapter)
1713 int retval = 0;
1715 wait_event(adapter->erp_done_wqh,
1716 !atomic_test_mask(ZFCP_STATUS_ADAPTER_ERP_PENDING,
1717 &adapter->status));
1719 return retval;
1722 void
1723 zfcp_erp_modify_adapter_status(struct zfcp_adapter *adapter,
1724 u32 mask, int set_or_clear)
1726 struct zfcp_port *port;
1727 u32 common_mask = mask & ZFCP_COMMON_FLAGS;
1729 if (set_or_clear == ZFCP_SET) {
1730 atomic_set_mask(mask, &adapter->status);
1731 debug_text_event(adapter->erp_dbf, 3, "a_mod_as_s");
1732 } else {
1733 atomic_clear_mask(mask, &adapter->status);
1734 if (mask & ZFCP_STATUS_COMMON_ERP_FAILED)
1735 atomic_set(&adapter->erp_counter, 0);
1736 debug_text_event(adapter->erp_dbf, 3, "a_mod_as_c");
1738 debug_event(adapter->erp_dbf, 3, &mask, sizeof (u32));
1740 /* Deal with all underlying devices, only pass common_mask */
1741 if (common_mask)
1742 list_for_each_entry(port, &adapter->port_list_head, list)
1743 zfcp_erp_modify_port_status(port, common_mask,
1744 set_or_clear);
1748 * function: zfcp_erp_modify_port_status
1750 * purpose: sets the port and all underlying devices to ERP_FAILED
1753 void
1754 zfcp_erp_modify_port_status(struct zfcp_port *port, u32 mask, int set_or_clear)
1756 struct zfcp_unit *unit;
1757 u32 common_mask = mask & ZFCP_COMMON_FLAGS;
1759 if (set_or_clear == ZFCP_SET) {
1760 atomic_set_mask(mask, &port->status);
1761 debug_text_event(port->adapter->erp_dbf, 3, "p_mod_ps_s");
1762 } else {
1763 atomic_clear_mask(mask, &port->status);
1764 if (mask & ZFCP_STATUS_COMMON_ERP_FAILED)
1765 atomic_set(&port->erp_counter, 0);
1766 debug_text_event(port->adapter->erp_dbf, 3, "p_mod_ps_c");
1768 debug_event(port->adapter->erp_dbf, 3, &port->wwpn, sizeof (wwn_t));
1769 debug_event(port->adapter->erp_dbf, 3, &mask, sizeof (u32));
1771 /* Modify status of all underlying devices, only pass common mask */
1772 if (common_mask)
1773 list_for_each_entry(unit, &port->unit_list_head, list)
1774 zfcp_erp_modify_unit_status(unit, common_mask,
1775 set_or_clear);
1779 * function: zfcp_erp_modify_unit_status
1781 * purpose: sets the unit to ERP_FAILED
1784 void
1785 zfcp_erp_modify_unit_status(struct zfcp_unit *unit, u32 mask, int set_or_clear)
1787 if (set_or_clear == ZFCP_SET) {
1788 atomic_set_mask(mask, &unit->status);
1789 debug_text_event(unit->port->adapter->erp_dbf, 3, "u_mod_us_s");
1790 } else {
1791 atomic_clear_mask(mask, &unit->status);
1792 if (mask & ZFCP_STATUS_COMMON_ERP_FAILED) {
1793 atomic_set(&unit->erp_counter, 0);
1795 debug_text_event(unit->port->adapter->erp_dbf, 3, "u_mod_us_c");
1797 debug_event(unit->port->adapter->erp_dbf, 3, &unit->fcp_lun,
1798 sizeof (fcp_lun_t));
1799 debug_event(unit->port->adapter->erp_dbf, 3, &mask, sizeof (u32));
1803 * function:
1805 * purpose: Wrappper for zfcp_erp_port_reopen_all_internal
1806 * used to ensure the correct locking
1808 * returns: 0 - initiated action succesfully
1809 * <0 - failed to initiate action
1812 zfcp_erp_port_reopen_all(struct zfcp_adapter *adapter, int clear_mask)
1814 int retval;
1815 unsigned long flags;
1817 read_lock_irqsave(&zfcp_data.config_lock, flags);
1818 write_lock(&adapter->erp_lock);
1819 retval = zfcp_erp_port_reopen_all_internal(adapter, clear_mask);
1820 write_unlock(&adapter->erp_lock);
1821 read_unlock_irqrestore(&zfcp_data.config_lock, flags);
1823 return retval;
1826 static int
1827 zfcp_erp_port_reopen_all_internal(struct zfcp_adapter *adapter, int clear_mask)
1829 int retval = 0;
1830 struct zfcp_port *port;
1832 list_for_each_entry(port, &adapter->port_list_head, list)
1833 if (!atomic_test_mask(ZFCP_STATUS_PORT_WKA, &port->status))
1834 zfcp_erp_port_reopen_internal(port, clear_mask);
1836 return retval;
1840 * function:
1842 * purpose:
1844 * returns: FIXME
1846 static int
1847 zfcp_erp_unit_reopen_all_internal(struct zfcp_port *port, int clear_mask)
1849 int retval = 0;
1850 struct zfcp_unit *unit;
1852 list_for_each_entry(unit, &port->unit_list_head, list)
1853 zfcp_erp_unit_reopen_internal(unit, clear_mask);
1855 return retval;
1859 * function:
1861 * purpose: this routine executes the 'Reopen Adapter' action
1862 * (the entire action is processed synchronously, since
1863 * there are no actions which might be run concurrently
1864 * per definition)
1866 * returns: ZFCP_ERP_SUCCEEDED - action finished successfully
1867 * ZFCP_ERP_FAILED - action finished unsuccessfully
1869 static int
1870 zfcp_erp_adapter_strategy(struct zfcp_erp_action *erp_action)
1872 int retval;
1873 struct zfcp_adapter *adapter = erp_action->adapter;
1875 retval = zfcp_erp_adapter_strategy_close(erp_action);
1876 if (erp_action->status & ZFCP_STATUS_ERP_CLOSE_ONLY)
1877 retval = ZFCP_ERP_EXIT;
1878 else
1879 retval = zfcp_erp_adapter_strategy_open(erp_action);
1881 debug_text_event(adapter->erp_dbf, 3, "a_ast/ret");
1882 debug_event(adapter->erp_dbf, 3, &erp_action->action, sizeof (int));
1883 debug_event(adapter->erp_dbf, 3, &retval, sizeof (int));
1885 if (retval == ZFCP_ERP_FAILED) {
1886 ZFCP_LOG_INFO("Waiting to allow the adapter %s "
1887 "to recover itself\n",
1888 zfcp_get_busid_by_adapter(adapter));
1889 msleep(jiffies_to_msecs(ZFCP_TYPE2_RECOVERY_TIME));
1892 return retval;
1896 * function:
1898 * purpose:
1900 * returns: ZFCP_ERP_SUCCEEDED - action finished successfully
1901 * ZFCP_ERP_FAILED - action finished unsuccessfully
1903 static int
1904 zfcp_erp_adapter_strategy_close(struct zfcp_erp_action *erp_action)
1906 int retval;
1908 atomic_set_mask(ZFCP_STATUS_COMMON_CLOSING,
1909 &erp_action->adapter->status);
1910 retval = zfcp_erp_adapter_strategy_generic(erp_action, 1);
1911 atomic_clear_mask(ZFCP_STATUS_COMMON_CLOSING,
1912 &erp_action->adapter->status);
1914 return retval;
1918 * function:
1920 * purpose:
1922 * returns: ZFCP_ERP_SUCCEEDED - action finished successfully
1923 * ZFCP_ERP_FAILED - action finished unsuccessfully
1925 static int
1926 zfcp_erp_adapter_strategy_open(struct zfcp_erp_action *erp_action)
1928 int retval;
1930 atomic_set_mask(ZFCP_STATUS_COMMON_OPENING,
1931 &erp_action->adapter->status);
1932 retval = zfcp_erp_adapter_strategy_generic(erp_action, 0);
1933 atomic_clear_mask(ZFCP_STATUS_COMMON_OPENING,
1934 &erp_action->adapter->status);
1936 return retval;
1940 * function: zfcp_register_adapter
1942 * purpose: allocate the irq associated with this devno and register
1943 * the FSF adapter with the SCSI stack
1945 * returns:
1947 static int
1948 zfcp_erp_adapter_strategy_generic(struct zfcp_erp_action *erp_action, int close)
1950 int retval = ZFCP_ERP_SUCCEEDED;
1952 if (close)
1953 goto close_only;
1955 retval = zfcp_erp_adapter_strategy_open_qdio(erp_action);
1956 if (retval != ZFCP_ERP_SUCCEEDED)
1957 goto failed_qdio;
1959 retval = zfcp_erp_adapter_strategy_open_fsf(erp_action);
1960 if (retval != ZFCP_ERP_SUCCEEDED)
1961 goto failed_openfcp;
1963 atomic_set_mask(ZFCP_STATUS_COMMON_OPEN, &erp_action->adapter->status);
1964 goto out;
1966 close_only:
1967 atomic_clear_mask(ZFCP_STATUS_COMMON_OPEN,
1968 &erp_action->adapter->status);
1970 failed_openfcp:
1971 zfcp_erp_adapter_strategy_close_qdio(erp_action);
1972 zfcp_erp_adapter_strategy_close_fsf(erp_action);
1973 failed_qdio:
1974 out:
1975 return retval;
1979 * function: zfcp_qdio_init
1981 * purpose: setup QDIO operation for specified adapter
1983 * returns: 0 - successful setup
1984 * !0 - failed setup
1987 zfcp_erp_adapter_strategy_open_qdio(struct zfcp_erp_action *erp_action)
1989 int retval;
1990 int i;
1991 volatile struct qdio_buffer_element *sbale;
1992 struct zfcp_adapter *adapter = erp_action->adapter;
1994 if (atomic_test_mask(ZFCP_STATUS_ADAPTER_QDIOUP, &adapter->status)) {
1995 ZFCP_LOG_NORMAL("bug: second attempt to set up QDIO on "
1996 "adapter %s\n",
1997 zfcp_get_busid_by_adapter(adapter));
1998 goto failed_sanity;
2001 if (qdio_establish(&adapter->qdio_init_data) != 0) {
2002 ZFCP_LOG_INFO("error: establishment of QDIO queues failed "
2003 "on adapter %s\n",
2004 zfcp_get_busid_by_adapter(adapter));
2005 goto failed_qdio_establish;
2007 debug_text_event(adapter->erp_dbf, 3, "qdio_est");
2009 if (qdio_activate(adapter->ccw_device, 0) != 0) {
2010 ZFCP_LOG_INFO("error: activation of QDIO queues failed "
2011 "on adapter %s\n",
2012 zfcp_get_busid_by_adapter(adapter));
2013 goto failed_qdio_activate;
2015 debug_text_event(adapter->erp_dbf, 3, "qdio_act");
2018 * put buffers into response queue,
2020 for (i = 0; i < QDIO_MAX_BUFFERS_PER_Q; i++) {
2021 sbale = &(adapter->response_queue.buffer[i]->element[0]);
2022 sbale->length = 0;
2023 sbale->flags = SBAL_FLAGS_LAST_ENTRY;
2024 sbale->addr = 0;
2027 ZFCP_LOG_TRACE("calling do_QDIO on adapter %s (flags=0x%x, "
2028 "queue_no=%i, index_in_queue=%i, count=%i)\n",
2029 zfcp_get_busid_by_adapter(adapter),
2030 QDIO_FLAG_SYNC_INPUT, 0, 0, QDIO_MAX_BUFFERS_PER_Q);
2032 retval = do_QDIO(adapter->ccw_device,
2033 QDIO_FLAG_SYNC_INPUT,
2034 0, 0, QDIO_MAX_BUFFERS_PER_Q, NULL);
2036 if (retval) {
2037 ZFCP_LOG_NORMAL("bug: setup of QDIO failed (retval=%d)\n",
2038 retval);
2039 goto failed_do_qdio;
2040 } else {
2041 adapter->response_queue.free_index = 0;
2042 atomic_set(&adapter->response_queue.free_count, 0);
2043 ZFCP_LOG_DEBUG("%i buffers successfully enqueued to "
2044 "response queue\n", QDIO_MAX_BUFFERS_PER_Q);
2046 /* set index of first avalable SBALS / number of available SBALS */
2047 adapter->request_queue.free_index = 0;
2048 atomic_set(&adapter->request_queue.free_count, QDIO_MAX_BUFFERS_PER_Q);
2049 adapter->request_queue.distance_from_int = 0;
2051 /* initialize waitqueue used to wait for free SBALs in requests queue */
2052 init_waitqueue_head(&adapter->request_wq);
2054 /* ok, we did it - skip all cleanups for different failures */
2055 atomic_set_mask(ZFCP_STATUS_ADAPTER_QDIOUP, &adapter->status);
2056 retval = ZFCP_ERP_SUCCEEDED;
2057 goto out;
2059 failed_do_qdio:
2060 /* NOP */
2062 failed_qdio_activate:
2063 debug_text_event(adapter->erp_dbf, 3, "qdio_down1a");
2064 while (qdio_shutdown(adapter->ccw_device,
2065 QDIO_FLAG_CLEANUP_USING_CLEAR) == -EINPROGRESS)
2066 msleep(1000);
2067 debug_text_event(adapter->erp_dbf, 3, "qdio_down1b");
2069 failed_qdio_establish:
2070 failed_sanity:
2071 retval = ZFCP_ERP_FAILED;
2073 out:
2074 return retval;
2078 * function: zfcp_qdio_cleanup
2080 * purpose: cleans up QDIO operation for the specified adapter
2082 * returns: 0 - successful cleanup
2083 * !0 - failed cleanup
2086 zfcp_erp_adapter_strategy_close_qdio(struct zfcp_erp_action *erp_action)
2088 int retval = ZFCP_ERP_SUCCEEDED;
2089 int first_used;
2090 int used_count;
2091 struct zfcp_adapter *adapter = erp_action->adapter;
2093 if (!atomic_test_mask(ZFCP_STATUS_ADAPTER_QDIOUP, &adapter->status)) {
2094 ZFCP_LOG_DEBUG("error: attempt to shut down inactive QDIO "
2095 "queues on adapter %s\n",
2096 zfcp_get_busid_by_adapter(adapter));
2097 retval = ZFCP_ERP_FAILED;
2098 goto out;
2102 * Get queue_lock and clear QDIOUP flag. Thus it's guaranteed that
2103 * do_QDIO won't be called while qdio_shutdown is in progress.
2106 write_lock_irq(&adapter->request_queue.queue_lock);
2107 atomic_clear_mask(ZFCP_STATUS_ADAPTER_QDIOUP, &adapter->status);
2108 write_unlock_irq(&adapter->request_queue.queue_lock);
2110 debug_text_event(adapter->erp_dbf, 3, "qdio_down2a");
2111 while (qdio_shutdown(adapter->ccw_device,
2112 QDIO_FLAG_CLEANUP_USING_CLEAR) == -EINPROGRESS)
2113 msleep(1000);
2114 debug_text_event(adapter->erp_dbf, 3, "qdio_down2b");
2117 * First we had to stop QDIO operation.
2118 * Now it is safe to take the following actions.
2121 /* Cleanup only necessary when there are unacknowledged buffers */
2122 if (atomic_read(&adapter->request_queue.free_count)
2123 < QDIO_MAX_BUFFERS_PER_Q) {
2124 first_used = (adapter->request_queue.free_index +
2125 atomic_read(&adapter->request_queue.free_count))
2126 % QDIO_MAX_BUFFERS_PER_Q;
2127 used_count = QDIO_MAX_BUFFERS_PER_Q -
2128 atomic_read(&adapter->request_queue.free_count);
2129 zfcp_qdio_zero_sbals(adapter->request_queue.buffer,
2130 first_used, used_count);
2132 adapter->response_queue.free_index = 0;
2133 atomic_set(&adapter->response_queue.free_count, 0);
2134 adapter->request_queue.free_index = 0;
2135 atomic_set(&adapter->request_queue.free_count, 0);
2136 adapter->request_queue.distance_from_int = 0;
2137 out:
2138 return retval;
2141 static int
2142 zfcp_erp_adapter_strategy_open_fsf(struct zfcp_erp_action *erp_action)
2144 int retval;
2146 retval = zfcp_erp_adapter_strategy_open_fsf_xconfig(erp_action);
2147 if (retval == ZFCP_ERP_FAILED)
2148 return ZFCP_ERP_FAILED;
2150 retval = zfcp_erp_adapter_strategy_open_fsf_xport(erp_action);
2151 if (retval == ZFCP_ERP_FAILED)
2152 return ZFCP_ERP_FAILED;
2154 return zfcp_erp_adapter_strategy_open_fsf_statusread(erp_action);
2157 static int
2158 zfcp_erp_adapter_strategy_open_fsf_xconfig(struct zfcp_erp_action *erp_action)
2160 int retval = ZFCP_ERP_SUCCEEDED;
2161 int retries;
2162 int sleep = ZFCP_EXCHANGE_CONFIG_DATA_FIRST_SLEEP;
2163 struct zfcp_adapter *adapter = erp_action->adapter;
2165 atomic_clear_mask(ZFCP_STATUS_ADAPTER_XCONFIG_OK, &adapter->status);
2167 for (retries = ZFCP_EXCHANGE_CONFIG_DATA_RETRIES; retries; retries--) {
2168 atomic_clear_mask(ZFCP_STATUS_ADAPTER_HOST_CON_INIT,
2169 &adapter->status);
2170 ZFCP_LOG_DEBUG("Doing exchange config data\n");
2171 write_lock(&adapter->erp_lock);
2172 zfcp_erp_action_to_running(erp_action);
2173 write_unlock(&adapter->erp_lock);
2174 zfcp_erp_timeout_init(erp_action);
2175 if (zfcp_fsf_exchange_config_data(erp_action)) {
2176 retval = ZFCP_ERP_FAILED;
2177 debug_text_event(adapter->erp_dbf, 5, "a_fstx_xf");
2178 ZFCP_LOG_INFO("error: initiation of exchange of "
2179 "configuration data failed for "
2180 "adapter %s\n",
2181 zfcp_get_busid_by_adapter(adapter));
2182 break;
2184 debug_text_event(adapter->erp_dbf, 6, "a_fstx_xok");
2185 ZFCP_LOG_DEBUG("Xchange underway\n");
2188 * Why this works:
2189 * Both the normal completion handler as well as the timeout
2190 * handler will do an 'up' when the 'exchange config data'
2191 * request completes or times out. Thus, the signal to go on
2192 * won't be lost utilizing this semaphore.
2193 * Furthermore, this 'adapter_reopen' action is
2194 * guaranteed to be the only action being there (highest action
2195 * which prevents other actions from being created).
2196 * Resulting from that, the wake signal recognized here
2197 * _must_ be the one belonging to the 'exchange config
2198 * data' request.
2200 down(&adapter->erp_ready_sem);
2201 if (erp_action->status & ZFCP_STATUS_ERP_TIMEDOUT) {
2202 ZFCP_LOG_INFO("error: exchange of configuration data "
2203 "for adapter %s timed out\n",
2204 zfcp_get_busid_by_adapter(adapter));
2205 break;
2208 if (!atomic_test_mask(ZFCP_STATUS_ADAPTER_HOST_CON_INIT,
2209 &adapter->status))
2210 break;
2212 ZFCP_LOG_DEBUG("host connection still initialising... "
2213 "waiting and retrying...\n");
2214 /* sleep a little bit before retry */
2215 msleep(jiffies_to_msecs(sleep));
2216 sleep *= 2;
2219 if (!atomic_test_mask(ZFCP_STATUS_ADAPTER_XCONFIG_OK,
2220 &adapter->status)) {
2221 ZFCP_LOG_INFO("error: exchange of configuration data for "
2222 "adapter %s failed\n",
2223 zfcp_get_busid_by_adapter(adapter));
2224 retval = ZFCP_ERP_FAILED;
2227 return retval;
2230 static int
2231 zfcp_erp_adapter_strategy_open_fsf_xport(struct zfcp_erp_action *erp_action)
2233 int ret;
2234 struct zfcp_adapter *adapter;
2236 adapter = erp_action->adapter;
2237 atomic_clear_mask(ZFCP_STATUS_ADAPTER_XPORT_OK, &adapter->status);
2239 write_lock(&adapter->erp_lock);
2240 zfcp_erp_action_to_running(erp_action);
2241 write_unlock(&adapter->erp_lock);
2243 zfcp_erp_timeout_init(erp_action);
2244 ret = zfcp_fsf_exchange_port_data(erp_action, adapter, NULL);
2245 if (ret == -EOPNOTSUPP) {
2246 debug_text_event(adapter->erp_dbf, 3, "a_xport_notsupp");
2247 return ZFCP_ERP_SUCCEEDED;
2248 } else if (ret) {
2249 debug_text_event(adapter->erp_dbf, 3, "a_xport_failed");
2250 return ZFCP_ERP_FAILED;
2252 debug_text_event(adapter->erp_dbf, 6, "a_xport_ok");
2254 ret = ZFCP_ERP_SUCCEEDED;
2255 down(&adapter->erp_ready_sem);
2256 if (erp_action->status & ZFCP_STATUS_ERP_TIMEDOUT) {
2257 ZFCP_LOG_INFO("error: exchange port data timed out (adapter "
2258 "%s)\n", zfcp_get_busid_by_adapter(adapter));
2259 ret = ZFCP_ERP_FAILED;
2261 if (!atomic_test_mask(ZFCP_STATUS_ADAPTER_XPORT_OK, &adapter->status)) {
2262 ZFCP_LOG_INFO("error: exchange port data failed (adapter "
2263 "%s\n", zfcp_get_busid_by_adapter(adapter));
2264 ret = ZFCP_ERP_FAILED;
2267 return ret;
2270 static int
2271 zfcp_erp_adapter_strategy_open_fsf_statusread(struct zfcp_erp_action
2272 *erp_action)
2274 int retval = ZFCP_ERP_SUCCEEDED;
2275 int temp_ret;
2276 struct zfcp_adapter *adapter = erp_action->adapter;
2277 int i;
2279 adapter->status_read_failed = 0;
2280 for (i = 0; i < ZFCP_STATUS_READS_RECOM; i++) {
2281 temp_ret = zfcp_fsf_status_read(adapter, ZFCP_WAIT_FOR_SBAL);
2282 if (temp_ret < 0) {
2283 ZFCP_LOG_INFO("error: set-up of unsolicited status "
2284 "notification failed on adapter %s\n",
2285 zfcp_get_busid_by_adapter(adapter));
2286 retval = ZFCP_ERP_FAILED;
2287 i--;
2288 break;
2292 return retval;
2296 * function: zfcp_fsf_cleanup
2298 * purpose: cleanup FSF operation for specified adapter
2300 * returns: 0 - FSF operation successfully cleaned up
2301 * !0 - failed to cleanup FSF operation for this adapter
2303 static int
2304 zfcp_erp_adapter_strategy_close_fsf(struct zfcp_erp_action *erp_action)
2306 int retval = ZFCP_ERP_SUCCEEDED;
2307 struct zfcp_adapter *adapter = erp_action->adapter;
2310 * wake waiting initiators of requests,
2311 * return SCSI commands (with error status),
2312 * clean up all requests (synchronously)
2314 zfcp_fsf_req_dismiss_all(adapter);
2315 /* reset FSF request sequence number */
2316 adapter->fsf_req_seq_no = 0;
2317 /* all ports and units are closed */
2318 zfcp_erp_modify_adapter_status(adapter,
2319 ZFCP_STATUS_COMMON_OPEN, ZFCP_CLEAR);
2321 return retval;
2325 * function:
2327 * purpose: this routine executes the 'Reopen Physical Port' action
2329 * returns: ZFCP_ERP_CONTINUES - action continues (asynchronously)
2330 * ZFCP_ERP_SUCCEEDED - action finished successfully
2331 * ZFCP_ERP_FAILED - action finished unsuccessfully
2333 static int
2334 zfcp_erp_port_forced_strategy(struct zfcp_erp_action *erp_action)
2336 int retval = ZFCP_ERP_FAILED;
2337 struct zfcp_port *port = erp_action->port;
2338 struct zfcp_adapter *adapter = erp_action->adapter;
2340 switch (erp_action->step) {
2343 * FIXME:
2344 * the ULP spec. begs for waiting for oustanding commands
2346 case ZFCP_ERP_STEP_UNINITIALIZED:
2347 zfcp_erp_port_strategy_clearstati(port);
2349 * it would be sufficient to test only the normal open flag
2350 * since the phys. open flag cannot be set if the normal
2351 * open flag is unset - however, this is for readabilty ...
2353 if (atomic_test_mask((ZFCP_STATUS_PORT_PHYS_OPEN |
2354 ZFCP_STATUS_COMMON_OPEN),
2355 &port->status)) {
2356 ZFCP_LOG_DEBUG("port 0x%016Lx is open -> trying "
2357 "close physical\n", port->wwpn);
2358 retval =
2359 zfcp_erp_port_forced_strategy_close(erp_action);
2360 } else
2361 retval = ZFCP_ERP_FAILED;
2362 break;
2364 case ZFCP_ERP_STEP_PHYS_PORT_CLOSING:
2365 if (atomic_test_mask(ZFCP_STATUS_PORT_PHYS_OPEN,
2366 &port->status)) {
2367 ZFCP_LOG_DEBUG("close physical failed for port "
2368 "0x%016Lx\n", port->wwpn);
2369 retval = ZFCP_ERP_FAILED;
2370 } else
2371 retval = ZFCP_ERP_SUCCEEDED;
2372 break;
2375 debug_text_event(adapter->erp_dbf, 3, "p_pfst/ret");
2376 debug_event(adapter->erp_dbf, 3, &port->wwpn, sizeof (wwn_t));
2377 debug_event(adapter->erp_dbf, 3, &erp_action->action, sizeof (int));
2378 debug_event(adapter->erp_dbf, 3, &retval, sizeof (int));
2380 return retval;
2384 * function:
2386 * purpose: this routine executes the 'Reopen Port' action
2388 * returns: ZFCP_ERP_CONTINUES - action continues (asynchronously)
2389 * ZFCP_ERP_SUCCEEDED - action finished successfully
2390 * ZFCP_ERP_FAILED - action finished unsuccessfully
2392 static int
2393 zfcp_erp_port_strategy(struct zfcp_erp_action *erp_action)
2395 int retval = ZFCP_ERP_FAILED;
2396 struct zfcp_port *port = erp_action->port;
2397 struct zfcp_adapter *adapter = erp_action->adapter;
2399 switch (erp_action->step) {
2402 * FIXME:
2403 * the ULP spec. begs for waiting for oustanding commands
2405 case ZFCP_ERP_STEP_UNINITIALIZED:
2406 zfcp_erp_port_strategy_clearstati(port);
2407 if (atomic_test_mask(ZFCP_STATUS_COMMON_OPEN, &port->status)) {
2408 ZFCP_LOG_DEBUG("port 0x%016Lx is open -> trying "
2409 "close\n", port->wwpn);
2410 retval = zfcp_erp_port_strategy_close(erp_action);
2411 goto out;
2412 } /* else it's already closed, open it */
2413 break;
2415 case ZFCP_ERP_STEP_PORT_CLOSING:
2416 if (atomic_test_mask(ZFCP_STATUS_COMMON_OPEN, &port->status)) {
2417 ZFCP_LOG_DEBUG("close failed for port 0x%016Lx\n",
2418 port->wwpn);
2419 retval = ZFCP_ERP_FAILED;
2420 goto out;
2421 } /* else it's closed now, open it */
2422 break;
2424 if (erp_action->status & ZFCP_STATUS_ERP_CLOSE_ONLY)
2425 retval = ZFCP_ERP_EXIT;
2426 else
2427 retval = zfcp_erp_port_strategy_open(erp_action);
2429 out:
2430 debug_text_event(adapter->erp_dbf, 3, "p_pst/ret");
2431 debug_event(adapter->erp_dbf, 3, &port->wwpn, sizeof (wwn_t));
2432 debug_event(adapter->erp_dbf, 3, &erp_action->action, sizeof (int));
2433 debug_event(adapter->erp_dbf, 3, &retval, sizeof (int));
2435 return retval;
2438 static int
2439 zfcp_erp_port_strategy_open(struct zfcp_erp_action *erp_action)
2441 int retval;
2443 if (atomic_test_mask(ZFCP_STATUS_PORT_WKA,
2444 &erp_action->port->status))
2445 retval = zfcp_erp_port_strategy_open_nameserver(erp_action);
2446 else
2447 retval = zfcp_erp_port_strategy_open_common(erp_action);
2449 return retval;
2452 static int
2453 zfcp_erp_port_strategy_open_common(struct zfcp_erp_action *erp_action)
2455 int retval = 0;
2456 struct zfcp_adapter *adapter = erp_action->adapter;
2457 struct zfcp_port *port = erp_action->port;
2459 switch (erp_action->step) {
2461 case ZFCP_ERP_STEP_UNINITIALIZED:
2462 case ZFCP_ERP_STEP_PHYS_PORT_CLOSING:
2463 case ZFCP_ERP_STEP_PORT_CLOSING:
2464 if (fc_host_port_type(adapter->scsi_host) == FC_PORTTYPE_PTP) {
2465 if (port->wwpn != adapter->peer_wwpn) {
2466 ZFCP_LOG_NORMAL("Failed to open port 0x%016Lx "
2467 "on adapter %s.\nPeer WWPN "
2468 "0x%016Lx does not match\n",
2469 port->wwpn,
2470 zfcp_get_busid_by_adapter(adapter),
2471 adapter->peer_wwpn);
2472 zfcp_erp_port_failed(port);
2473 retval = ZFCP_ERP_FAILED;
2474 break;
2476 port->d_id = adapter->peer_d_id;
2477 atomic_set_mask(ZFCP_STATUS_PORT_DID_DID, &port->status);
2478 retval = zfcp_erp_port_strategy_open_port(erp_action);
2479 break;
2481 if (!(adapter->nameserver_port)) {
2482 retval = zfcp_nameserver_enqueue(adapter);
2483 if (retval != 0) {
2484 ZFCP_LOG_NORMAL("error: nameserver port "
2485 "unavailable for adapter %s\n",
2486 zfcp_get_busid_by_adapter(adapter));
2487 retval = ZFCP_ERP_FAILED;
2488 break;
2491 if (!atomic_test_mask(ZFCP_STATUS_COMMON_UNBLOCKED,
2492 &adapter->nameserver_port->status)) {
2493 ZFCP_LOG_DEBUG("nameserver port is not open -> open "
2494 "nameserver port\n");
2495 /* nameserver port may live again */
2496 atomic_set_mask(ZFCP_STATUS_COMMON_RUNNING,
2497 &adapter->nameserver_port->status);
2498 if (zfcp_erp_port_reopen(adapter->nameserver_port, 0)
2499 >= 0) {
2500 erp_action->step =
2501 ZFCP_ERP_STEP_NAMESERVER_OPEN;
2502 retval = ZFCP_ERP_CONTINUES;
2503 } else
2504 retval = ZFCP_ERP_FAILED;
2505 break;
2507 /* else nameserver port is already open, fall through */
2508 case ZFCP_ERP_STEP_NAMESERVER_OPEN:
2509 if (!atomic_test_mask(ZFCP_STATUS_COMMON_OPEN,
2510 &adapter->nameserver_port->status)) {
2511 ZFCP_LOG_DEBUG("open failed for nameserver port\n");
2512 retval = ZFCP_ERP_FAILED;
2513 } else {
2514 ZFCP_LOG_DEBUG("nameserver port is open -> "
2515 "nameserver look-up for port 0x%016Lx\n",
2516 port->wwpn);
2517 retval = zfcp_erp_port_strategy_open_common_lookup
2518 (erp_action);
2520 break;
2522 case ZFCP_ERP_STEP_NAMESERVER_LOOKUP:
2523 if (!atomic_test_mask(ZFCP_STATUS_PORT_DID_DID, &port->status)) {
2524 if (atomic_test_mask
2525 (ZFCP_STATUS_PORT_INVALID_WWPN, &port->status)) {
2526 ZFCP_LOG_DEBUG("nameserver look-up failed "
2527 "for port 0x%016Lx "
2528 "(misconfigured WWPN?)\n",
2529 port->wwpn);
2530 zfcp_erp_port_failed(port);
2531 retval = ZFCP_ERP_EXIT;
2532 } else {
2533 ZFCP_LOG_DEBUG("nameserver look-up failed for "
2534 "port 0x%016Lx\n", port->wwpn);
2535 retval = ZFCP_ERP_FAILED;
2537 } else {
2538 ZFCP_LOG_DEBUG("port 0x%016Lx has d_id=0x%08x -> "
2539 "trying open\n", port->wwpn, port->d_id);
2540 retval = zfcp_erp_port_strategy_open_port(erp_action);
2542 break;
2544 case ZFCP_ERP_STEP_PORT_OPENING:
2545 /* D_ID might have changed during open */
2546 if (atomic_test_mask((ZFCP_STATUS_COMMON_OPEN |
2547 ZFCP_STATUS_PORT_DID_DID),
2548 &port->status)) {
2549 ZFCP_LOG_DEBUG("port 0x%016Lx is open\n", port->wwpn);
2550 retval = ZFCP_ERP_SUCCEEDED;
2551 } else {
2552 ZFCP_LOG_DEBUG("open failed for port 0x%016Lx\n",
2553 port->wwpn);
2554 retval = ZFCP_ERP_FAILED;
2556 break;
2558 default:
2559 ZFCP_LOG_NORMAL("bug: unknown erp step 0x%08x\n",
2560 erp_action->step);
2561 retval = ZFCP_ERP_FAILED;
2564 return retval;
2567 static int
2568 zfcp_erp_port_strategy_open_nameserver(struct zfcp_erp_action *erp_action)
2570 int retval;
2571 struct zfcp_port *port = erp_action->port;
2573 switch (erp_action->step) {
2575 case ZFCP_ERP_STEP_UNINITIALIZED:
2576 case ZFCP_ERP_STEP_PHYS_PORT_CLOSING:
2577 case ZFCP_ERP_STEP_PORT_CLOSING:
2578 ZFCP_LOG_DEBUG("port 0x%016Lx has d_id=0x%08x -> trying open\n",
2579 port->wwpn, port->d_id);
2580 retval = zfcp_erp_port_strategy_open_port(erp_action);
2581 break;
2583 case ZFCP_ERP_STEP_PORT_OPENING:
2584 if (atomic_test_mask(ZFCP_STATUS_COMMON_OPEN, &port->status)) {
2585 ZFCP_LOG_DEBUG("WKA port is open\n");
2586 retval = ZFCP_ERP_SUCCEEDED;
2587 } else {
2588 ZFCP_LOG_DEBUG("open failed for WKA port\n");
2589 retval = ZFCP_ERP_FAILED;
2591 /* this is needed anyway (dont care for retval of wakeup) */
2592 ZFCP_LOG_DEBUG("continue other open port operations\n");
2593 zfcp_erp_port_strategy_open_nameserver_wakeup(erp_action);
2594 break;
2596 default:
2597 ZFCP_LOG_NORMAL("bug: unknown erp step 0x%08x\n",
2598 erp_action->step);
2599 retval = ZFCP_ERP_FAILED;
2602 return retval;
2606 * function:
2608 * purpose: makes the erp thread continue with reopen (physical) port
2609 * actions which have been paused until the name server port
2610 * is opened (or failed)
2612 * returns: 0 (a kind of void retval, its not used)
2614 static int
2615 zfcp_erp_port_strategy_open_nameserver_wakeup(struct zfcp_erp_action
2616 *ns_erp_action)
2618 int retval = 0;
2619 unsigned long flags;
2620 struct zfcp_adapter *adapter = ns_erp_action->adapter;
2621 struct zfcp_erp_action *erp_action, *tmp;
2623 read_lock_irqsave(&adapter->erp_lock, flags);
2624 list_for_each_entry_safe(erp_action, tmp, &adapter->erp_running_head,
2625 list) {
2626 debug_text_event(adapter->erp_dbf, 4, "p_pstnsw_n");
2627 debug_event(adapter->erp_dbf, 4, &erp_action->port->wwpn,
2628 sizeof (wwn_t));
2629 if (erp_action->step == ZFCP_ERP_STEP_NAMESERVER_OPEN) {
2630 debug_text_event(adapter->erp_dbf, 3, "p_pstnsw_w");
2631 debug_event(adapter->erp_dbf, 3,
2632 &erp_action->port->wwpn, sizeof (wwn_t));
2633 if (atomic_test_mask(
2634 ZFCP_STATUS_COMMON_ERP_FAILED,
2635 &adapter->nameserver_port->status))
2636 zfcp_erp_port_failed(erp_action->port);
2637 zfcp_erp_action_ready(erp_action);
2640 read_unlock_irqrestore(&adapter->erp_lock, flags);
2642 return retval;
2646 * function:
2648 * purpose:
2650 * returns: ZFCP_ERP_CONTINUES - action continues (asynchronously)
2651 * ZFCP_ERP_FAILED - action finished unsuccessfully
2653 static int
2654 zfcp_erp_port_forced_strategy_close(struct zfcp_erp_action *erp_action)
2656 int retval;
2657 struct zfcp_adapter *adapter = erp_action->adapter;
2658 struct zfcp_port *port = erp_action->port;
2660 zfcp_erp_timeout_init(erp_action);
2661 retval = zfcp_fsf_close_physical_port(erp_action);
2662 if (retval == -ENOMEM) {
2663 debug_text_event(adapter->erp_dbf, 5, "o_pfstc_nomem");
2664 debug_event(adapter->erp_dbf, 5, &port->wwpn, sizeof (wwn_t));
2665 retval = ZFCP_ERP_NOMEM;
2666 goto out;
2668 erp_action->step = ZFCP_ERP_STEP_PHYS_PORT_CLOSING;
2669 if (retval != 0) {
2670 debug_text_event(adapter->erp_dbf, 5, "o_pfstc_cpf");
2671 debug_event(adapter->erp_dbf, 5, &port->wwpn, sizeof (wwn_t));
2672 /* could not send 'open', fail */
2673 retval = ZFCP_ERP_FAILED;
2674 goto out;
2676 debug_text_event(adapter->erp_dbf, 6, "o_pfstc_cpok");
2677 debug_event(adapter->erp_dbf, 6, &port->wwpn, sizeof (wwn_t));
2678 retval = ZFCP_ERP_CONTINUES;
2679 out:
2680 return retval;
2683 static int
2684 zfcp_erp_port_strategy_clearstati(struct zfcp_port *port)
2686 int retval = 0;
2687 struct zfcp_adapter *adapter = port->adapter;
2689 debug_text_event(adapter->erp_dbf, 5, "p_pstclst");
2690 debug_event(adapter->erp_dbf, 5, &port->wwpn, sizeof (wwn_t));
2692 atomic_clear_mask(ZFCP_STATUS_COMMON_OPENING |
2693 ZFCP_STATUS_COMMON_CLOSING |
2694 ZFCP_STATUS_COMMON_ACCESS_DENIED |
2695 ZFCP_STATUS_PORT_DID_DID |
2696 ZFCP_STATUS_PORT_PHYS_CLOSING |
2697 ZFCP_STATUS_PORT_INVALID_WWPN,
2698 &port->status);
2699 return retval;
2703 * function:
2705 * purpose:
2707 * returns: ZFCP_ERP_CONTINUES - action continues (asynchronously)
2708 * ZFCP_ERP_FAILED - action finished unsuccessfully
2710 static int
2711 zfcp_erp_port_strategy_close(struct zfcp_erp_action *erp_action)
2713 int retval;
2714 struct zfcp_adapter *adapter = erp_action->adapter;
2715 struct zfcp_port *port = erp_action->port;
2717 zfcp_erp_timeout_init(erp_action);
2718 retval = zfcp_fsf_close_port(erp_action);
2719 if (retval == -ENOMEM) {
2720 debug_text_event(adapter->erp_dbf, 5, "p_pstc_nomem");
2721 debug_event(adapter->erp_dbf, 5, &port->wwpn, sizeof (wwn_t));
2722 retval = ZFCP_ERP_NOMEM;
2723 goto out;
2725 erp_action->step = ZFCP_ERP_STEP_PORT_CLOSING;
2726 if (retval != 0) {
2727 debug_text_event(adapter->erp_dbf, 5, "p_pstc_cpf");
2728 debug_event(adapter->erp_dbf, 5, &port->wwpn, sizeof (wwn_t));
2729 /* could not send 'close', fail */
2730 retval = ZFCP_ERP_FAILED;
2731 goto out;
2733 debug_text_event(adapter->erp_dbf, 6, "p_pstc_cpok");
2734 debug_event(adapter->erp_dbf, 6, &port->wwpn, sizeof (wwn_t));
2735 retval = ZFCP_ERP_CONTINUES;
2736 out:
2737 return retval;
2741 * function:
2743 * purpose:
2745 * returns: ZFCP_ERP_CONTINUES - action continues (asynchronously)
2746 * ZFCP_ERP_FAILED - action finished unsuccessfully
2748 static int
2749 zfcp_erp_port_strategy_open_port(struct zfcp_erp_action *erp_action)
2751 int retval;
2752 struct zfcp_adapter *adapter = erp_action->adapter;
2753 struct zfcp_port *port = erp_action->port;
2755 zfcp_erp_timeout_init(erp_action);
2756 retval = zfcp_fsf_open_port(erp_action);
2757 if (retval == -ENOMEM) {
2758 debug_text_event(adapter->erp_dbf, 5, "p_psto_nomem");
2759 debug_event(adapter->erp_dbf, 5, &port->wwpn, sizeof (wwn_t));
2760 retval = ZFCP_ERP_NOMEM;
2761 goto out;
2763 erp_action->step = ZFCP_ERP_STEP_PORT_OPENING;
2764 if (retval != 0) {
2765 debug_text_event(adapter->erp_dbf, 5, "p_psto_opf");
2766 debug_event(adapter->erp_dbf, 5, &port->wwpn, sizeof (wwn_t));
2767 /* could not send 'open', fail */
2768 retval = ZFCP_ERP_FAILED;
2769 goto out;
2771 debug_text_event(adapter->erp_dbf, 6, "p_psto_opok");
2772 debug_event(adapter->erp_dbf, 6, &port->wwpn, sizeof (wwn_t));
2773 retval = ZFCP_ERP_CONTINUES;
2774 out:
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_strategy_open_common_lookup(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_ns_gid_pn_request(erp_action);
2795 if (retval == -ENOMEM) {
2796 debug_text_event(adapter->erp_dbf, 5, "p_pstn_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_NAMESERVER_LOOKUP;
2802 if (retval != 0) {
2803 debug_text_event(adapter->erp_dbf, 5, "p_pstn_ref");
2804 debug_event(adapter->erp_dbf, 5, &port->wwpn, sizeof (wwn_t));
2805 /* could not send nameserver request, fail */
2806 retval = ZFCP_ERP_FAILED;
2807 goto out;
2809 debug_text_event(adapter->erp_dbf, 6, "p_pstn_reok");
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: this routine executes the 'Reopen Unit' action
2820 * currently no retries
2822 * returns: ZFCP_ERP_CONTINUES - action continues (asynchronously)
2823 * ZFCP_ERP_SUCCEEDED - action finished successfully
2824 * ZFCP_ERP_FAILED - action finished unsuccessfully
2826 static int
2827 zfcp_erp_unit_strategy(struct zfcp_erp_action *erp_action)
2829 int retval = ZFCP_ERP_FAILED;
2830 struct zfcp_unit *unit = erp_action->unit;
2831 struct zfcp_adapter *adapter = erp_action->adapter;
2833 switch (erp_action->step) {
2836 * FIXME:
2837 * the ULP spec. begs for waiting for oustanding commands
2839 case ZFCP_ERP_STEP_UNINITIALIZED:
2840 zfcp_erp_unit_strategy_clearstati(unit);
2841 if (atomic_test_mask(ZFCP_STATUS_COMMON_OPEN, &unit->status)) {
2842 ZFCP_LOG_DEBUG("unit 0x%016Lx is open -> "
2843 "trying close\n", unit->fcp_lun);
2844 retval = zfcp_erp_unit_strategy_close(erp_action);
2845 break;
2847 /* else it's already closed, fall through */
2848 case ZFCP_ERP_STEP_UNIT_CLOSING:
2849 if (atomic_test_mask(ZFCP_STATUS_COMMON_OPEN, &unit->status)) {
2850 ZFCP_LOG_DEBUG("close failed for unit 0x%016Lx\n",
2851 unit->fcp_lun);
2852 retval = ZFCP_ERP_FAILED;
2853 } else {
2854 if (erp_action->status & ZFCP_STATUS_ERP_CLOSE_ONLY)
2855 retval = ZFCP_ERP_EXIT;
2856 else {
2857 ZFCP_LOG_DEBUG("unit 0x%016Lx is not open -> "
2858 "trying open\n", unit->fcp_lun);
2859 retval =
2860 zfcp_erp_unit_strategy_open(erp_action);
2863 break;
2865 case ZFCP_ERP_STEP_UNIT_OPENING:
2866 if (atomic_test_mask(ZFCP_STATUS_COMMON_OPEN, &unit->status)) {
2867 ZFCP_LOG_DEBUG("unit 0x%016Lx is open\n",
2868 unit->fcp_lun);
2869 retval = ZFCP_ERP_SUCCEEDED;
2870 } else {
2871 ZFCP_LOG_DEBUG("open failed for unit 0x%016Lx\n",
2872 unit->fcp_lun);
2873 retval = ZFCP_ERP_FAILED;
2875 break;
2878 debug_text_event(adapter->erp_dbf, 3, "u_ust/ret");
2879 debug_event(adapter->erp_dbf, 3, &unit->fcp_lun, sizeof (fcp_lun_t));
2880 debug_event(adapter->erp_dbf, 3, &erp_action->action, sizeof (int));
2881 debug_event(adapter->erp_dbf, 3, &retval, sizeof (int));
2882 return retval;
2885 static int
2886 zfcp_erp_unit_strategy_clearstati(struct zfcp_unit *unit)
2888 int retval = 0;
2889 struct zfcp_adapter *adapter = unit->port->adapter;
2891 debug_text_event(adapter->erp_dbf, 5, "u_ustclst");
2892 debug_event(adapter->erp_dbf, 5, &unit->fcp_lun, sizeof (fcp_lun_t));
2894 atomic_clear_mask(ZFCP_STATUS_COMMON_OPENING |
2895 ZFCP_STATUS_COMMON_CLOSING |
2896 ZFCP_STATUS_COMMON_ACCESS_DENIED |
2897 ZFCP_STATUS_UNIT_SHARED |
2898 ZFCP_STATUS_UNIT_READONLY,
2899 &unit->status);
2901 return retval;
2905 * function:
2907 * purpose:
2909 * returns: ZFCP_ERP_CONTINUES - action continues (asynchronously)
2910 * ZFCP_ERP_FAILED - action finished unsuccessfully
2912 static int
2913 zfcp_erp_unit_strategy_close(struct zfcp_erp_action *erp_action)
2915 int retval;
2916 struct zfcp_adapter *adapter = erp_action->adapter;
2917 struct zfcp_unit *unit = erp_action->unit;
2919 zfcp_erp_timeout_init(erp_action);
2920 retval = zfcp_fsf_close_unit(erp_action);
2921 if (retval == -ENOMEM) {
2922 debug_text_event(adapter->erp_dbf, 5, "u_ustc_nomem");
2923 debug_event(adapter->erp_dbf, 5, &unit->fcp_lun,
2924 sizeof (fcp_lun_t));
2925 retval = ZFCP_ERP_NOMEM;
2926 goto out;
2928 erp_action->step = ZFCP_ERP_STEP_UNIT_CLOSING;
2929 if (retval != 0) {
2930 debug_text_event(adapter->erp_dbf, 5, "u_ustc_cuf");
2931 debug_event(adapter->erp_dbf, 5, &unit->fcp_lun,
2932 sizeof (fcp_lun_t));
2933 /* could not send 'close', fail */
2934 retval = ZFCP_ERP_FAILED;
2935 goto out;
2937 debug_text_event(adapter->erp_dbf, 6, "u_ustc_cuok");
2938 debug_event(adapter->erp_dbf, 6, &unit->fcp_lun, sizeof (fcp_lun_t));
2939 retval = ZFCP_ERP_CONTINUES;
2941 out:
2942 return retval;
2946 * function:
2948 * purpose:
2950 * returns: ZFCP_ERP_CONTINUES - action continues (asynchronously)
2951 * ZFCP_ERP_FAILED - action finished unsuccessfully
2953 static int
2954 zfcp_erp_unit_strategy_open(struct zfcp_erp_action *erp_action)
2956 int retval;
2957 struct zfcp_adapter *adapter = erp_action->adapter;
2958 struct zfcp_unit *unit = erp_action->unit;
2960 zfcp_erp_timeout_init(erp_action);
2961 retval = zfcp_fsf_open_unit(erp_action);
2962 if (retval == -ENOMEM) {
2963 debug_text_event(adapter->erp_dbf, 5, "u_usto_nomem");
2964 debug_event(adapter->erp_dbf, 5, &unit->fcp_lun,
2965 sizeof (fcp_lun_t));
2966 retval = ZFCP_ERP_NOMEM;
2967 goto out;
2969 erp_action->step = ZFCP_ERP_STEP_UNIT_OPENING;
2970 if (retval != 0) {
2971 debug_text_event(adapter->erp_dbf, 5, "u_usto_ouf");
2972 debug_event(adapter->erp_dbf, 5, &unit->fcp_lun,
2973 sizeof (fcp_lun_t));
2974 /* could not send 'open', fail */
2975 retval = ZFCP_ERP_FAILED;
2976 goto out;
2978 debug_text_event(adapter->erp_dbf, 6, "u_usto_ouok");
2979 debug_event(adapter->erp_dbf, 6, &unit->fcp_lun, sizeof (fcp_lun_t));
2980 retval = ZFCP_ERP_CONTINUES;
2981 out:
2982 return retval;
2985 static inline void
2986 zfcp_erp_timeout_init(struct zfcp_erp_action *erp_action)
2988 init_timer(&erp_action->timer);
2989 erp_action->timer.function = zfcp_erp_timeout_handler;
2990 erp_action->timer.data = (unsigned long) erp_action;
2991 /* jiffies will be added in zfcp_fsf_req_send */
2992 erp_action->timer.expires = ZFCP_ERP_FSFREQ_TIMEOUT;
2996 * function:
2998 * purpose: enqueue the specified error recovery action, if needed
3000 * returns:
3002 static int
3003 zfcp_erp_action_enqueue(int action,
3004 struct zfcp_adapter *adapter,
3005 struct zfcp_port *port, struct zfcp_unit *unit)
3007 int retval = 1;
3008 struct zfcp_erp_action *erp_action = NULL;
3009 int stronger_action = 0;
3010 u32 status = 0;
3013 * We need some rules here which check whether we really need
3014 * this action or whether we should just drop it.
3015 * E.g. if there is a unfinished 'Reopen Port' request then we drop a
3016 * 'Reopen Unit' request for an associated unit since we can't
3017 * satisfy this request now. A 'Reopen Port' action will trigger
3018 * 'Reopen Unit' actions when it completes.
3019 * Thus, there are only actions in the queue which can immediately be
3020 * executed. This makes the processing of the action queue more
3021 * efficient.
3024 if (!atomic_test_mask(ZFCP_STATUS_ADAPTER_ERP_THREAD_UP,
3025 &adapter->status))
3026 return -EIO;
3028 debug_event(adapter->erp_dbf, 4, &action, sizeof (int));
3029 /* check whether we really need this */
3030 switch (action) {
3031 case ZFCP_ERP_ACTION_REOPEN_UNIT:
3032 if (atomic_test_mask
3033 (ZFCP_STATUS_COMMON_ERP_INUSE, &unit->status)) {
3034 debug_text_event(adapter->erp_dbf, 4, "u_actenq_drp");
3035 debug_event(adapter->erp_dbf, 4, &port->wwpn,
3036 sizeof (wwn_t));
3037 debug_event(adapter->erp_dbf, 4, &unit->fcp_lun,
3038 sizeof (fcp_lun_t));
3039 goto out;
3041 if (!atomic_test_mask
3042 (ZFCP_STATUS_COMMON_RUNNING, &port->status) ||
3043 atomic_test_mask
3044 (ZFCP_STATUS_COMMON_ERP_FAILED, &port->status)) {
3045 goto out;
3047 if (!atomic_test_mask
3048 (ZFCP_STATUS_COMMON_UNBLOCKED, &port->status)) {
3049 stronger_action = ZFCP_ERP_ACTION_REOPEN_PORT;
3050 unit = NULL;
3052 /* fall through !!! */
3054 case ZFCP_ERP_ACTION_REOPEN_PORT:
3055 if (atomic_test_mask
3056 (ZFCP_STATUS_COMMON_ERP_INUSE, &port->status)) {
3057 debug_text_event(adapter->erp_dbf, 4, "p_actenq_drp");
3058 debug_event(adapter->erp_dbf, 4, &port->wwpn,
3059 sizeof (wwn_t));
3060 goto out;
3062 /* fall through !!! */
3064 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
3065 if (atomic_test_mask(ZFCP_STATUS_COMMON_ERP_INUSE,
3066 &port->status)) {
3067 if (port->erp_action.action !=
3068 ZFCP_ERP_ACTION_REOPEN_PORT_FORCED) {
3069 ZFCP_LOG_INFO("dropped erp action %i (port "
3070 "0x%016Lx, action in use: %i)\n",
3071 action, port->wwpn,
3072 port->erp_action.action);
3073 debug_text_event(adapter->erp_dbf, 4,
3074 "pf_actenq_drp");
3075 } else
3076 debug_text_event(adapter->erp_dbf, 4,
3077 "pf_actenq_drpcp");
3078 debug_event(adapter->erp_dbf, 4, &port->wwpn,
3079 sizeof (wwn_t));
3080 goto out;
3082 if (!atomic_test_mask
3083 (ZFCP_STATUS_COMMON_RUNNING, &adapter->status) ||
3084 atomic_test_mask
3085 (ZFCP_STATUS_COMMON_ERP_FAILED, &adapter->status)) {
3086 goto out;
3088 if (!atomic_test_mask
3089 (ZFCP_STATUS_COMMON_UNBLOCKED, &adapter->status)) {
3090 stronger_action = ZFCP_ERP_ACTION_REOPEN_ADAPTER;
3091 port = NULL;
3093 /* fall through !!! */
3095 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
3096 if (atomic_test_mask
3097 (ZFCP_STATUS_COMMON_ERP_INUSE, &adapter->status)) {
3098 debug_text_event(adapter->erp_dbf, 4, "a_actenq_drp");
3099 goto out;
3101 break;
3103 default:
3104 debug_text_exception(adapter->erp_dbf, 1, "a_actenq_bug");
3105 debug_event(adapter->erp_dbf, 1, &action, sizeof (int));
3106 ZFCP_LOG_NORMAL("bug: unknown erp action requested "
3107 "on adapter %s (action=%d)\n",
3108 zfcp_get_busid_by_adapter(adapter), action);
3109 goto out;
3112 /* check whether we need something stronger first */
3113 if (stronger_action) {
3114 debug_text_event(adapter->erp_dbf, 4, "a_actenq_str");
3115 debug_event(adapter->erp_dbf, 4, &stronger_action,
3116 sizeof (int));
3117 ZFCP_LOG_DEBUG("stronger erp action %d needed before "
3118 "erp action %d on adapter %s\n",
3119 stronger_action, action,
3120 zfcp_get_busid_by_adapter(adapter));
3121 action = stronger_action;
3124 /* mark adapter to have some error recovery pending */
3125 atomic_set_mask(ZFCP_STATUS_ADAPTER_ERP_PENDING, &adapter->status);
3127 /* setup error recovery action */
3128 switch (action) {
3130 case ZFCP_ERP_ACTION_REOPEN_UNIT:
3131 zfcp_unit_get(unit);
3132 atomic_set_mask(ZFCP_STATUS_COMMON_ERP_INUSE, &unit->status);
3133 erp_action = &unit->erp_action;
3134 if (!atomic_test_mask
3135 (ZFCP_STATUS_COMMON_RUNNING, &unit->status))
3136 status = ZFCP_STATUS_ERP_CLOSE_ONLY;
3137 break;
3139 case ZFCP_ERP_ACTION_REOPEN_PORT:
3140 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
3141 zfcp_port_get(port);
3142 zfcp_erp_action_dismiss_port(port);
3143 atomic_set_mask(ZFCP_STATUS_COMMON_ERP_INUSE, &port->status);
3144 erp_action = &port->erp_action;
3145 if (!atomic_test_mask
3146 (ZFCP_STATUS_COMMON_RUNNING, &port->status))
3147 status = ZFCP_STATUS_ERP_CLOSE_ONLY;
3148 break;
3150 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
3151 zfcp_adapter_get(adapter);
3152 zfcp_erp_action_dismiss_adapter(adapter);
3153 atomic_set_mask(ZFCP_STATUS_COMMON_ERP_INUSE, &adapter->status);
3154 erp_action = &adapter->erp_action;
3155 if (!atomic_test_mask
3156 (ZFCP_STATUS_COMMON_RUNNING, &adapter->status))
3157 status = ZFCP_STATUS_ERP_CLOSE_ONLY;
3158 break;
3161 debug_text_event(adapter->erp_dbf, 4, "a_actenq");
3163 memset(erp_action, 0, sizeof (struct zfcp_erp_action));
3164 erp_action->adapter = adapter;
3165 erp_action->port = port;
3166 erp_action->unit = unit;
3167 erp_action->action = action;
3168 erp_action->status = status;
3170 ++adapter->erp_total_count;
3172 /* finally put it into 'ready' queue and kick erp thread */
3173 list_add(&erp_action->list, &adapter->erp_ready_head);
3174 up(&adapter->erp_ready_sem);
3175 retval = 0;
3176 out:
3177 return retval;
3180 static int
3181 zfcp_erp_action_dequeue(struct zfcp_erp_action *erp_action)
3183 int retval = 0;
3184 struct zfcp_adapter *adapter = erp_action->adapter;
3186 --adapter->erp_total_count;
3187 if (erp_action->status & ZFCP_STATUS_ERP_LOWMEM) {
3188 --adapter->erp_low_mem_count;
3189 erp_action->status &= ~ZFCP_STATUS_ERP_LOWMEM;
3192 debug_text_event(adapter->erp_dbf, 4, "a_actdeq");
3193 debug_event(adapter->erp_dbf, 4, &erp_action->action, sizeof (int));
3194 list_del(&erp_action->list);
3195 switch (erp_action->action) {
3196 case ZFCP_ERP_ACTION_REOPEN_UNIT:
3197 atomic_clear_mask(ZFCP_STATUS_COMMON_ERP_INUSE,
3198 &erp_action->unit->status);
3199 break;
3200 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
3201 case ZFCP_ERP_ACTION_REOPEN_PORT:
3202 atomic_clear_mask(ZFCP_STATUS_COMMON_ERP_INUSE,
3203 &erp_action->port->status);
3204 break;
3205 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
3206 atomic_clear_mask(ZFCP_STATUS_COMMON_ERP_INUSE,
3207 &erp_action->adapter->status);
3208 break;
3209 default:
3210 /* bug */
3211 break;
3213 return retval;
3217 * zfcp_erp_action_cleanup
3219 * Register unit with scsi stack if appropriate and fix reference counts.
3220 * Note: Temporary units are not registered with scsi stack.
3222 static void
3223 zfcp_erp_action_cleanup(int action, struct zfcp_adapter *adapter,
3224 struct zfcp_port *port, struct zfcp_unit *unit,
3225 int result)
3227 switch (action) {
3228 case ZFCP_ERP_ACTION_REOPEN_UNIT:
3229 if ((result == ZFCP_ERP_SUCCEEDED)
3230 && (!atomic_test_mask(ZFCP_STATUS_UNIT_TEMPORARY,
3231 &unit->status))
3232 && !unit->device
3233 && port->rport) {
3234 atomic_set_mask(ZFCP_STATUS_UNIT_REGISTERED,
3235 &unit->status);
3236 scsi_scan_target(&port->rport->dev, 0,
3237 port->rport->scsi_target_id,
3238 unit->scsi_lun, 0);
3240 zfcp_unit_put(unit);
3241 break;
3242 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
3243 case ZFCP_ERP_ACTION_REOPEN_PORT:
3244 if ((result == ZFCP_ERP_SUCCEEDED)
3245 && !atomic_test_mask(ZFCP_STATUS_PORT_NO_WWPN,
3246 &port->status)
3247 && !port->rport) {
3248 struct fc_rport_identifiers ids;
3249 ids.node_name = port->wwnn;
3250 ids.port_name = port->wwpn;
3251 ids.port_id = port->d_id;
3252 ids.roles = FC_RPORT_ROLE_FCP_TARGET;
3253 port->rport =
3254 fc_remote_port_add(adapter->scsi_host, 0, &ids);
3255 if (!port->rport)
3256 ZFCP_LOG_NORMAL("failed registration of rport"
3257 "(adapter %s, wwpn=0x%016Lx)\n",
3258 zfcp_get_busid_by_port(port),
3259 port->wwpn);
3260 else {
3261 scsi_flush_work(adapter->scsi_host);
3262 port->rport->maxframe_size = port->maxframe_size;
3263 port->rport->supported_classes =
3264 port->supported_classes;
3267 zfcp_port_put(port);
3268 break;
3269 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
3270 zfcp_adapter_put(adapter);
3271 break;
3272 default:
3273 break;
3278 static int
3279 zfcp_erp_action_dismiss_adapter(struct zfcp_adapter *adapter)
3281 int retval = 0;
3282 struct zfcp_port *port;
3284 debug_text_event(adapter->erp_dbf, 5, "a_actab");
3285 if (atomic_test_mask(ZFCP_STATUS_COMMON_ERP_INUSE, &adapter->status))
3286 zfcp_erp_action_dismiss(&adapter->erp_action);
3287 else
3288 list_for_each_entry(port, &adapter->port_list_head, list)
3289 zfcp_erp_action_dismiss_port(port);
3291 return retval;
3294 static int
3295 zfcp_erp_action_dismiss_port(struct zfcp_port *port)
3297 int retval = 0;
3298 struct zfcp_unit *unit;
3299 struct zfcp_adapter *adapter = port->adapter;
3301 debug_text_event(adapter->erp_dbf, 5, "p_actab");
3302 debug_event(adapter->erp_dbf, 5, &port->wwpn, sizeof (wwn_t));
3303 if (atomic_test_mask(ZFCP_STATUS_COMMON_ERP_INUSE, &port->status))
3304 zfcp_erp_action_dismiss(&port->erp_action);
3305 else
3306 list_for_each_entry(unit, &port->unit_list_head, list)
3307 zfcp_erp_action_dismiss_unit(unit);
3309 return retval;
3312 static int
3313 zfcp_erp_action_dismiss_unit(struct zfcp_unit *unit)
3315 int retval = 0;
3316 struct zfcp_adapter *adapter = unit->port->adapter;
3318 debug_text_event(adapter->erp_dbf, 5, "u_actab");
3319 debug_event(adapter->erp_dbf, 5, &unit->fcp_lun, sizeof (fcp_lun_t));
3320 if (atomic_test_mask(ZFCP_STATUS_COMMON_ERP_INUSE, &unit->status))
3321 zfcp_erp_action_dismiss(&unit->erp_action);
3323 return retval;
3326 static inline void
3327 zfcp_erp_action_to_running(struct zfcp_erp_action *erp_action)
3329 struct zfcp_adapter *adapter = erp_action->adapter;
3331 debug_text_event(adapter->erp_dbf, 6, "a_toru");
3332 debug_event(adapter->erp_dbf, 6, &erp_action->action, sizeof (int));
3333 list_move(&erp_action->list, &erp_action->adapter->erp_running_head);
3336 static inline void
3337 zfcp_erp_action_to_ready(struct zfcp_erp_action *erp_action)
3339 struct zfcp_adapter *adapter = erp_action->adapter;
3341 debug_text_event(adapter->erp_dbf, 6, "a_tore");
3342 debug_event(adapter->erp_dbf, 6, &erp_action->action, sizeof (int));
3343 list_move(&erp_action->list, &erp_action->adapter->erp_ready_head);
3346 void
3347 zfcp_erp_port_boxed(struct zfcp_port *port)
3349 struct zfcp_adapter *adapter = port->adapter;
3350 unsigned long flags;
3352 debug_text_event(adapter->erp_dbf, 3, "p_access_boxed");
3353 debug_event(adapter->erp_dbf, 3, &port->wwpn, sizeof(wwn_t));
3354 read_lock_irqsave(&zfcp_data.config_lock, flags);
3355 zfcp_erp_modify_port_status(port,
3356 ZFCP_STATUS_COMMON_ACCESS_BOXED,
3357 ZFCP_SET);
3358 read_unlock_irqrestore(&zfcp_data.config_lock, flags);
3359 zfcp_erp_port_reopen(port, ZFCP_STATUS_COMMON_ERP_FAILED);
3362 void
3363 zfcp_erp_unit_boxed(struct zfcp_unit *unit)
3365 struct zfcp_adapter *adapter = unit->port->adapter;
3367 debug_text_event(adapter->erp_dbf, 3, "u_access_boxed");
3368 debug_event(adapter->erp_dbf, 3, &unit->fcp_lun, sizeof(fcp_lun_t));
3369 zfcp_erp_modify_unit_status(unit,
3370 ZFCP_STATUS_COMMON_ACCESS_BOXED,
3371 ZFCP_SET);
3372 zfcp_erp_unit_reopen(unit, ZFCP_STATUS_COMMON_ERP_FAILED);
3375 void
3376 zfcp_erp_port_access_denied(struct zfcp_port *port)
3378 struct zfcp_adapter *adapter = port->adapter;
3379 unsigned long flags;
3381 debug_text_event(adapter->erp_dbf, 3, "p_access_denied");
3382 debug_event(adapter->erp_dbf, 3, &port->wwpn, sizeof(wwn_t));
3383 read_lock_irqsave(&zfcp_data.config_lock, flags);
3384 zfcp_erp_modify_port_status(port,
3385 ZFCP_STATUS_COMMON_ERP_FAILED |
3386 ZFCP_STATUS_COMMON_ACCESS_DENIED,
3387 ZFCP_SET);
3388 read_unlock_irqrestore(&zfcp_data.config_lock, flags);
3391 void
3392 zfcp_erp_unit_access_denied(struct zfcp_unit *unit)
3394 struct zfcp_adapter *adapter = unit->port->adapter;
3396 debug_text_event(adapter->erp_dbf, 3, "u_access_denied");
3397 debug_event(adapter->erp_dbf, 3, &unit->fcp_lun, sizeof(fcp_lun_t));
3398 zfcp_erp_modify_unit_status(unit,
3399 ZFCP_STATUS_COMMON_ERP_FAILED |
3400 ZFCP_STATUS_COMMON_ACCESS_DENIED,
3401 ZFCP_SET);
3404 void
3405 zfcp_erp_adapter_access_changed(struct zfcp_adapter *adapter)
3407 struct zfcp_port *port;
3408 unsigned long flags;
3410 if (adapter->connection_features & FSF_FEATURE_NPIV_MODE)
3411 return;
3413 debug_text_event(adapter->erp_dbf, 3, "a_access_recover");
3414 debug_event(adapter->erp_dbf, 3, zfcp_get_busid_by_adapter(adapter), 8);
3416 read_lock_irqsave(&zfcp_data.config_lock, flags);
3417 if (adapter->nameserver_port)
3418 zfcp_erp_port_access_changed(adapter->nameserver_port);
3419 list_for_each_entry(port, &adapter->port_list_head, list)
3420 if (port != adapter->nameserver_port)
3421 zfcp_erp_port_access_changed(port);
3422 read_unlock_irqrestore(&zfcp_data.config_lock, flags);
3425 void
3426 zfcp_erp_port_access_changed(struct zfcp_port *port)
3428 struct zfcp_adapter *adapter = port->adapter;
3429 struct zfcp_unit *unit;
3431 debug_text_event(adapter->erp_dbf, 3, "p_access_recover");
3432 debug_event(adapter->erp_dbf, 3, &port->wwpn, sizeof(wwn_t));
3434 if (!atomic_test_mask(ZFCP_STATUS_COMMON_ACCESS_DENIED,
3435 &port->status) &&
3436 !atomic_test_mask(ZFCP_STATUS_COMMON_ACCESS_BOXED,
3437 &port->status)) {
3438 if (!atomic_test_mask(ZFCP_STATUS_PORT_WKA, &port->status))
3439 list_for_each_entry(unit, &port->unit_list_head, list)
3440 zfcp_erp_unit_access_changed(unit);
3441 return;
3444 ZFCP_LOG_NORMAL("reopen of port 0x%016Lx on adapter %s "
3445 "(due to ACT update)\n",
3446 port->wwpn, zfcp_get_busid_by_adapter(adapter));
3447 if (zfcp_erp_port_reopen(port, ZFCP_STATUS_COMMON_ERP_FAILED) != 0)
3448 ZFCP_LOG_NORMAL("failed reopen of port"
3449 "(adapter %s, wwpn=0x%016Lx)\n",
3450 zfcp_get_busid_by_adapter(adapter), port->wwpn);
3453 void
3454 zfcp_erp_unit_access_changed(struct zfcp_unit *unit)
3456 struct zfcp_adapter *adapter = unit->port->adapter;
3458 debug_text_event(adapter->erp_dbf, 3, "u_access_recover");
3459 debug_event(adapter->erp_dbf, 3, &unit->fcp_lun, sizeof(fcp_lun_t));
3461 if (!atomic_test_mask(ZFCP_STATUS_COMMON_ACCESS_DENIED,
3462 &unit->status) &&
3463 !atomic_test_mask(ZFCP_STATUS_COMMON_ACCESS_BOXED,
3464 &unit->status))
3465 return;
3467 ZFCP_LOG_NORMAL("reopen of unit 0x%016Lx on port 0x%016Lx "
3468 " on adapter %s (due to ACT update)\n",
3469 unit->fcp_lun, unit->port->wwpn,
3470 zfcp_get_busid_by_adapter(adapter));
3471 if (zfcp_erp_unit_reopen(unit, ZFCP_STATUS_COMMON_ERP_FAILED) != 0)
3472 ZFCP_LOG_NORMAL("failed reopen of unit (adapter %s, "
3473 "wwpn=0x%016Lx, fcp_lun=0x%016Lx)\n",
3474 zfcp_get_busid_by_adapter(adapter),
3475 unit->port->wwpn, unit->fcp_lun);
3478 #undef ZFCP_LOG_AREA