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