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