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