xref: /openbmc/linux/drivers/s390/scsi/zfcp_erp.c (revision 348447e8)
11da177e4SLinus Torvalds /*
24a9d2d8bSAndreas Herrmann  * This file is part of the zfcp device driver for
34a9d2d8bSAndreas Herrmann  * FCP adapters for IBM System z9 and zSeries.
41da177e4SLinus Torvalds  *
54a9d2d8bSAndreas Herrmann  * (C) Copyright IBM Corp. 2002, 2006
61da177e4SLinus Torvalds  *
71da177e4SLinus Torvalds  * This program is free software; you can redistribute it and/or modify
81da177e4SLinus Torvalds  * it under the terms of the GNU General Public License as published by
91da177e4SLinus Torvalds  * the Free Software Foundation; either version 2, or (at your option)
101da177e4SLinus Torvalds  * any later version.
111da177e4SLinus Torvalds  *
121da177e4SLinus Torvalds  * This program is distributed in the hope that it will be useful,
131da177e4SLinus Torvalds  * but WITHOUT ANY WARRANTY; without even the implied warranty of
141da177e4SLinus Torvalds  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
151da177e4SLinus Torvalds  * GNU General Public License for more details.
161da177e4SLinus Torvalds  *
171da177e4SLinus Torvalds  * You should have received a copy of the GNU General Public License
181da177e4SLinus Torvalds  * along with this program; if not, write to the Free Software
191da177e4SLinus Torvalds  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
201da177e4SLinus Torvalds  */
211da177e4SLinus Torvalds 
221da177e4SLinus Torvalds #define ZFCP_LOG_AREA			ZFCP_LOG_AREA_ERP
231da177e4SLinus Torvalds 
241da177e4SLinus Torvalds #include "zfcp_ext.h"
251da177e4SLinus Torvalds 
2664b29a13SAndreas Herrmann static int zfcp_erp_adisc(struct zfcp_port *);
271da177e4SLinus Torvalds static void zfcp_erp_adisc_handler(unsigned long);
281da177e4SLinus Torvalds 
291da177e4SLinus Torvalds static int zfcp_erp_adapter_reopen_internal(struct zfcp_adapter *, int);
301da177e4SLinus Torvalds static int zfcp_erp_port_forced_reopen_internal(struct zfcp_port *, int);
311da177e4SLinus Torvalds static int zfcp_erp_port_reopen_internal(struct zfcp_port *, int);
321da177e4SLinus Torvalds static int zfcp_erp_unit_reopen_internal(struct zfcp_unit *, int);
331da177e4SLinus Torvalds 
341da177e4SLinus Torvalds static int zfcp_erp_port_reopen_all_internal(struct zfcp_adapter *, int);
351da177e4SLinus Torvalds static int zfcp_erp_unit_reopen_all_internal(struct zfcp_port *, int);
361da177e4SLinus Torvalds 
371da177e4SLinus Torvalds static void zfcp_erp_adapter_block(struct zfcp_adapter *, int);
381da177e4SLinus Torvalds static void zfcp_erp_adapter_unblock(struct zfcp_adapter *);
391da177e4SLinus Torvalds static void zfcp_erp_port_block(struct zfcp_port *, int);
401da177e4SLinus Torvalds static void zfcp_erp_port_unblock(struct zfcp_port *);
411da177e4SLinus Torvalds static void zfcp_erp_unit_block(struct zfcp_unit *, int);
421da177e4SLinus Torvalds static void zfcp_erp_unit_unblock(struct zfcp_unit *);
431da177e4SLinus Torvalds 
441da177e4SLinus Torvalds static int zfcp_erp_thread(void *);
451da177e4SLinus Torvalds 
461da177e4SLinus Torvalds static int zfcp_erp_strategy(struct zfcp_erp_action *);
471da177e4SLinus Torvalds 
481da177e4SLinus Torvalds static int zfcp_erp_strategy_do_action(struct zfcp_erp_action *);
491da177e4SLinus Torvalds static int zfcp_erp_strategy_memwait(struct zfcp_erp_action *);
501da177e4SLinus Torvalds static int zfcp_erp_strategy_check_target(struct zfcp_erp_action *, int);
511da177e4SLinus Torvalds static int zfcp_erp_strategy_check_unit(struct zfcp_unit *, int);
521da177e4SLinus Torvalds static int zfcp_erp_strategy_check_port(struct zfcp_port *, int);
531da177e4SLinus Torvalds static int zfcp_erp_strategy_check_adapter(struct zfcp_adapter *, int);
541da177e4SLinus Torvalds static int zfcp_erp_strategy_statechange(int, u32, struct zfcp_adapter *,
551da177e4SLinus Torvalds 					 struct zfcp_port *,
561da177e4SLinus Torvalds 					 struct zfcp_unit *, int);
57364c8558SHeiko Carstens static int zfcp_erp_strategy_statechange_detected(atomic_t *, u32);
581da177e4SLinus Torvalds static int zfcp_erp_strategy_followup_actions(int, struct zfcp_adapter *,
591da177e4SLinus Torvalds 					      struct zfcp_port *,
601da177e4SLinus Torvalds 					      struct zfcp_unit *, int);
611da177e4SLinus Torvalds static int zfcp_erp_strategy_check_queues(struct zfcp_adapter *);
621da177e4SLinus Torvalds static int zfcp_erp_strategy_check_action(struct zfcp_erp_action *, int);
631da177e4SLinus Torvalds 
641da177e4SLinus Torvalds static int zfcp_erp_adapter_strategy(struct zfcp_erp_action *);
651da177e4SLinus Torvalds static int zfcp_erp_adapter_strategy_generic(struct zfcp_erp_action *, int);
661da177e4SLinus Torvalds static int zfcp_erp_adapter_strategy_close(struct zfcp_erp_action *);
671da177e4SLinus Torvalds static int zfcp_erp_adapter_strategy_open(struct zfcp_erp_action *);
681da177e4SLinus Torvalds static int zfcp_erp_adapter_strategy_open_qdio(struct zfcp_erp_action *);
691da177e4SLinus Torvalds static int zfcp_erp_adapter_strategy_open_fsf(struct zfcp_erp_action *);
701da177e4SLinus Torvalds static int zfcp_erp_adapter_strategy_open_fsf_xconfig(struct zfcp_erp_action *);
71aef4a983SMaxim Shchetynin static int zfcp_erp_adapter_strategy_open_fsf_xport(struct zfcp_erp_action *);
721da177e4SLinus Torvalds static int zfcp_erp_adapter_strategy_open_fsf_statusread(
731da177e4SLinus Torvalds 	struct zfcp_erp_action *);
741da177e4SLinus Torvalds 
751da177e4SLinus Torvalds static int zfcp_erp_port_forced_strategy(struct zfcp_erp_action *);
761da177e4SLinus Torvalds static int zfcp_erp_port_forced_strategy_close(struct zfcp_erp_action *);
771da177e4SLinus Torvalds 
781da177e4SLinus Torvalds static int zfcp_erp_port_strategy(struct zfcp_erp_action *);
791da177e4SLinus Torvalds static int zfcp_erp_port_strategy_clearstati(struct zfcp_port *);
801da177e4SLinus Torvalds static int zfcp_erp_port_strategy_close(struct zfcp_erp_action *);
811da177e4SLinus Torvalds static int zfcp_erp_port_strategy_open(struct zfcp_erp_action *);
821da177e4SLinus Torvalds static int zfcp_erp_port_strategy_open_nameserver(struct zfcp_erp_action *);
831da177e4SLinus Torvalds static int zfcp_erp_port_strategy_open_nameserver_wakeup(
841da177e4SLinus Torvalds 	struct zfcp_erp_action *);
851da177e4SLinus Torvalds static int zfcp_erp_port_strategy_open_common(struct zfcp_erp_action *);
861da177e4SLinus Torvalds static int zfcp_erp_port_strategy_open_common_lookup(struct zfcp_erp_action *);
871da177e4SLinus Torvalds static int zfcp_erp_port_strategy_open_port(struct zfcp_erp_action *);
881da177e4SLinus Torvalds 
891da177e4SLinus Torvalds static int zfcp_erp_unit_strategy(struct zfcp_erp_action *);
901da177e4SLinus Torvalds static int zfcp_erp_unit_strategy_clearstati(struct zfcp_unit *);
911da177e4SLinus Torvalds static int zfcp_erp_unit_strategy_close(struct zfcp_erp_action *);
921da177e4SLinus Torvalds static int zfcp_erp_unit_strategy_open(struct zfcp_erp_action *);
931da177e4SLinus Torvalds 
9481654286SAndreas Herrmann static void zfcp_erp_action_dismiss_adapter(struct zfcp_adapter *);
95f6c0e7a7SAndreas Herrmann static void zfcp_erp_action_dismiss_port(struct zfcp_port *);
96f6c0e7a7SAndreas Herrmann static void zfcp_erp_action_dismiss_unit(struct zfcp_unit *);
97f6c0e7a7SAndreas Herrmann static void zfcp_erp_action_dismiss(struct zfcp_erp_action *);
981da177e4SLinus Torvalds 
991da177e4SLinus Torvalds static int zfcp_erp_action_enqueue(int, struct zfcp_adapter *,
1001da177e4SLinus Torvalds 				   struct zfcp_port *, struct zfcp_unit *);
1011da177e4SLinus Torvalds static int zfcp_erp_action_dequeue(struct zfcp_erp_action *);
1021da177e4SLinus Torvalds static void zfcp_erp_action_cleanup(int, struct zfcp_adapter *,
1031da177e4SLinus Torvalds 				    struct zfcp_port *, struct zfcp_unit *,
1041da177e4SLinus Torvalds 				    int);
1051da177e4SLinus Torvalds 
1061da177e4SLinus Torvalds static void zfcp_erp_action_ready(struct zfcp_erp_action *);
1071da177e4SLinus Torvalds static int  zfcp_erp_action_exists(struct zfcp_erp_action *);
1081da177e4SLinus Torvalds 
109364c8558SHeiko Carstens static void zfcp_erp_action_to_ready(struct zfcp_erp_action *);
110364c8558SHeiko Carstens static void zfcp_erp_action_to_running(struct zfcp_erp_action *);
1111da177e4SLinus Torvalds 
1121da177e4SLinus Torvalds static void zfcp_erp_memwait_handler(unsigned long);
1132abbe866SAndreas Herrmann 
1142abbe866SAndreas Herrmann /**
1152abbe866SAndreas Herrmann  * zfcp_close_qdio - close qdio queues for an adapter
1162abbe866SAndreas Herrmann  */
1172abbe866SAndreas Herrmann static void zfcp_close_qdio(struct zfcp_adapter *adapter)
1182abbe866SAndreas Herrmann {
1192abbe866SAndreas Herrmann 	struct zfcp_qdio_queue *req_queue;
1202abbe866SAndreas Herrmann 	int first, count;
1212abbe866SAndreas Herrmann 
1222abbe866SAndreas Herrmann 	if (!atomic_test_mask(ZFCP_STATUS_ADAPTER_QDIOUP, &adapter->status))
1232abbe866SAndreas Herrmann 		return;
1242abbe866SAndreas Herrmann 
1252abbe866SAndreas Herrmann 	/* clear QDIOUP flag, thus do_QDIO is not called during qdio_shutdown */
1262abbe866SAndreas Herrmann 	req_queue = &adapter->request_queue;
1272abbe866SAndreas Herrmann 	write_lock_irq(&req_queue->queue_lock);
1282abbe866SAndreas Herrmann 	atomic_clear_mask(ZFCP_STATUS_ADAPTER_QDIOUP, &adapter->status);
1292abbe866SAndreas Herrmann 	write_unlock_irq(&req_queue->queue_lock);
1302abbe866SAndreas Herrmann 
1312abbe866SAndreas Herrmann 	debug_text_event(adapter->erp_dbf, 3, "qdio_down2a");
1322abbe866SAndreas Herrmann 	while (qdio_shutdown(adapter->ccw_device,
1332abbe866SAndreas Herrmann 			     QDIO_FLAG_CLEANUP_USING_CLEAR) == -EINPROGRESS)
13418edcdbdSChristof Schmitt 		ssleep(1);
1352abbe866SAndreas Herrmann 	debug_text_event(adapter->erp_dbf, 3, "qdio_down2b");
1362abbe866SAndreas Herrmann 
1372abbe866SAndreas Herrmann 	/* cleanup used outbound sbals */
1382abbe866SAndreas Herrmann 	count = atomic_read(&req_queue->free_count);
1392abbe866SAndreas Herrmann 	if (count < QDIO_MAX_BUFFERS_PER_Q) {
1402abbe866SAndreas Herrmann 		first = (req_queue->free_index+count) % QDIO_MAX_BUFFERS_PER_Q;
1412abbe866SAndreas Herrmann 		count = QDIO_MAX_BUFFERS_PER_Q - count;
1422abbe866SAndreas Herrmann 		zfcp_qdio_zero_sbals(req_queue->buffer, first, count);
1432abbe866SAndreas Herrmann 	}
1442abbe866SAndreas Herrmann 	req_queue->free_index = 0;
1452abbe866SAndreas Herrmann 	atomic_set(&req_queue->free_count, 0);
1462abbe866SAndreas Herrmann 	req_queue->distance_from_int = 0;
1472abbe866SAndreas Herrmann 	adapter->response_queue.free_index = 0;
1482abbe866SAndreas Herrmann 	atomic_set(&adapter->response_queue.free_count, 0);
1492abbe866SAndreas Herrmann }
1502abbe866SAndreas Herrmann 
1512abbe866SAndreas Herrmann /**
1522abbe866SAndreas Herrmann  * zfcp_close_fsf - stop FSF operations for an adapter
1532abbe866SAndreas Herrmann  *
1542abbe866SAndreas Herrmann  * Dismiss and cleanup all pending fsf_reqs (this wakes up all initiators of
1552abbe866SAndreas Herrmann  * requests waiting for completion; especially this returns SCSI commands
1562abbe866SAndreas Herrmann  * with error state).
1572abbe866SAndreas Herrmann  */
1582abbe866SAndreas Herrmann static void zfcp_close_fsf(struct zfcp_adapter *adapter)
1592abbe866SAndreas Herrmann {
1602abbe866SAndreas Herrmann 	/* close queues to ensure that buffers are not accessed by adapter */
1612abbe866SAndreas Herrmann 	zfcp_close_qdio(adapter);
1622abbe866SAndreas Herrmann 	zfcp_fsf_req_dismiss_all(adapter);
1632abbe866SAndreas Herrmann 	/* reset FSF request sequence number */
1642abbe866SAndreas Herrmann 	adapter->fsf_req_seq_no = 0;
1652abbe866SAndreas Herrmann 	/* all ports and units are closed */
1662abbe866SAndreas Herrmann 	zfcp_erp_modify_adapter_status(adapter,
1672abbe866SAndreas Herrmann 				       ZFCP_STATUS_COMMON_OPEN, ZFCP_CLEAR);
1682abbe866SAndreas Herrmann }
1691da177e4SLinus Torvalds 
1701da177e4SLinus Torvalds /**
1711da177e4SLinus Torvalds  * zfcp_fsf_request_timeout_handler - called if a request timed out
1721da177e4SLinus Torvalds  * @data: pointer to adapter for handler function
1731da177e4SLinus Torvalds  *
1741da177e4SLinus Torvalds  * This function needs to be called if requests (ELS, Generic Service,
1751da177e4SLinus Torvalds  * or SCSI commands) exceed a certain time limit. The assumption is
1761da177e4SLinus Torvalds  * that after the time limit the adapter get stuck. So we trigger a reopen of
1772abbe866SAndreas Herrmann  * the adapter.
1781da177e4SLinus Torvalds  */
1792abbe866SAndreas Herrmann static void zfcp_fsf_request_timeout_handler(unsigned long data)
1801da177e4SLinus Torvalds {
1812abbe866SAndreas Herrmann 	struct zfcp_adapter *adapter = (struct zfcp_adapter *) data;
1823b02191aSHeiko Carstens 	zfcp_erp_adapter_reopen(adapter, ZFCP_STATUS_COMMON_ERP_FAILED);
1831da177e4SLinus Torvalds }
1841da177e4SLinus Torvalds 
1852abbe866SAndreas Herrmann void zfcp_fsf_start_timer(struct zfcp_fsf_req *fsf_req, unsigned long timeout)
1861da177e4SLinus Torvalds {
1872abbe866SAndreas Herrmann 	fsf_req->timer.function = zfcp_fsf_request_timeout_handler;
1882abbe866SAndreas Herrmann 	fsf_req->timer.data = (unsigned long) fsf_req->adapter;
1899e3738f3SChristof Schmitt 	fsf_req->timer.expires = jiffies + timeout;
1902abbe866SAndreas Herrmann 	add_timer(&fsf_req->timer);
1911da177e4SLinus Torvalds }
1921da177e4SLinus Torvalds 
1931da177e4SLinus Torvalds /*
1941da177e4SLinus Torvalds  * function:
1951da177e4SLinus Torvalds  *
1961da177e4SLinus Torvalds  * purpose:	called if an adapter failed,
1971da177e4SLinus Torvalds  *		initiates adapter recovery which is done
1981da177e4SLinus Torvalds  *		asynchronously
1991da177e4SLinus Torvalds  *
200d6e05edcSAndreas Mohr  * returns:	0	- initiated action successfully
2011da177e4SLinus Torvalds  *		<0	- failed to initiate action
2021da177e4SLinus Torvalds  */
2032b67fc46SHeiko Carstens static int
2041da177e4SLinus Torvalds zfcp_erp_adapter_reopen_internal(struct zfcp_adapter *adapter, int clear_mask)
2051da177e4SLinus Torvalds {
2061da177e4SLinus Torvalds 	int retval;
2071da177e4SLinus Torvalds 
2081da177e4SLinus Torvalds 	debug_text_event(adapter->erp_dbf, 5, "a_ro");
2091da177e4SLinus Torvalds 	ZFCP_LOG_DEBUG("reopen adapter %s\n",
2101da177e4SLinus Torvalds 		       zfcp_get_busid_by_adapter(adapter));
2111da177e4SLinus Torvalds 
2121da177e4SLinus Torvalds 	zfcp_erp_adapter_block(adapter, clear_mask);
2131da177e4SLinus Torvalds 
2141da177e4SLinus Torvalds 	if (atomic_test_mask(ZFCP_STATUS_COMMON_ERP_FAILED, &adapter->status)) {
2151da177e4SLinus Torvalds 		ZFCP_LOG_DEBUG("skipped reopen of failed adapter %s\n",
2161da177e4SLinus Torvalds 			       zfcp_get_busid_by_adapter(adapter));
2171da177e4SLinus Torvalds 		debug_text_event(adapter->erp_dbf, 5, "a_ro_f");
2181da177e4SLinus Torvalds 		/* ensure propagation of failed status to new devices */
2191da177e4SLinus Torvalds 		zfcp_erp_adapter_failed(adapter);
2201da177e4SLinus Torvalds 		retval = -EIO;
2211da177e4SLinus Torvalds 		goto out;
2221da177e4SLinus Torvalds 	}
2231da177e4SLinus Torvalds 	retval = zfcp_erp_action_enqueue(ZFCP_ERP_ACTION_REOPEN_ADAPTER,
2241da177e4SLinus Torvalds 					 adapter, NULL, NULL);
2251da177e4SLinus Torvalds 
2261da177e4SLinus Torvalds  out:
2271da177e4SLinus Torvalds 	return retval;
2281da177e4SLinus Torvalds }
2291da177e4SLinus Torvalds 
2301da177e4SLinus Torvalds /*
2311da177e4SLinus Torvalds  * function:
2321da177e4SLinus Torvalds  *
2331da177e4SLinus Torvalds  * purpose:	Wrappper for zfcp_erp_adapter_reopen_internal
2341da177e4SLinus Torvalds  *              used to ensure the correct locking
2351da177e4SLinus Torvalds  *
236d6e05edcSAndreas Mohr  * returns:	0	- initiated action successfully
2371da177e4SLinus Torvalds  *		<0	- failed to initiate action
2381da177e4SLinus Torvalds  */
2391da177e4SLinus Torvalds int
2401da177e4SLinus Torvalds zfcp_erp_adapter_reopen(struct zfcp_adapter *adapter, int clear_mask)
2411da177e4SLinus Torvalds {
2421da177e4SLinus Torvalds 	int retval;
2431da177e4SLinus Torvalds 	unsigned long flags;
2441da177e4SLinus Torvalds 
2451da177e4SLinus Torvalds 	read_lock_irqsave(&zfcp_data.config_lock, flags);
2461da177e4SLinus Torvalds 	write_lock(&adapter->erp_lock);
2471da177e4SLinus Torvalds 	retval = zfcp_erp_adapter_reopen_internal(adapter, clear_mask);
2481da177e4SLinus Torvalds 	write_unlock(&adapter->erp_lock);
2491da177e4SLinus Torvalds 	read_unlock_irqrestore(&zfcp_data.config_lock, flags);
2501da177e4SLinus Torvalds 
2511da177e4SLinus Torvalds 	return retval;
2521da177e4SLinus Torvalds }
2531da177e4SLinus Torvalds 
2541da177e4SLinus Torvalds int
2551da177e4SLinus Torvalds zfcp_erp_adapter_shutdown(struct zfcp_adapter *adapter, int clear_mask)
2561da177e4SLinus Torvalds {
2571da177e4SLinus Torvalds 	int retval;
2581da177e4SLinus Torvalds 
2591da177e4SLinus Torvalds 	retval = zfcp_erp_adapter_reopen(adapter,
2601da177e4SLinus Torvalds 					 ZFCP_STATUS_COMMON_RUNNING |
2611da177e4SLinus Torvalds 					 ZFCP_STATUS_COMMON_ERP_FAILED |
2621da177e4SLinus Torvalds 					 clear_mask);
2631da177e4SLinus Torvalds 
2641da177e4SLinus Torvalds 	return retval;
2651da177e4SLinus Torvalds }
2661da177e4SLinus Torvalds 
2671da177e4SLinus Torvalds int
2681da177e4SLinus Torvalds zfcp_erp_port_shutdown(struct zfcp_port *port, int clear_mask)
2691da177e4SLinus Torvalds {
2701da177e4SLinus Torvalds 	int retval;
2711da177e4SLinus Torvalds 
2721da177e4SLinus Torvalds 	retval = zfcp_erp_port_reopen(port,
2731da177e4SLinus Torvalds 				      ZFCP_STATUS_COMMON_RUNNING |
2741da177e4SLinus Torvalds 				      ZFCP_STATUS_COMMON_ERP_FAILED |
2751da177e4SLinus Torvalds 				      clear_mask);
2761da177e4SLinus Torvalds 
2771da177e4SLinus Torvalds 	return retval;
2781da177e4SLinus Torvalds }
2791da177e4SLinus Torvalds 
2801da177e4SLinus Torvalds int
2811da177e4SLinus Torvalds zfcp_erp_unit_shutdown(struct zfcp_unit *unit, int clear_mask)
2821da177e4SLinus Torvalds {
2831da177e4SLinus Torvalds 	int retval;
2841da177e4SLinus Torvalds 
2851da177e4SLinus Torvalds 	retval = zfcp_erp_unit_reopen(unit,
2861da177e4SLinus Torvalds 				      ZFCP_STATUS_COMMON_RUNNING |
2871da177e4SLinus Torvalds 				      ZFCP_STATUS_COMMON_ERP_FAILED |
2881da177e4SLinus Torvalds 				      clear_mask);
2891da177e4SLinus Torvalds 
2901da177e4SLinus Torvalds 	return retval;
2911da177e4SLinus Torvalds }
2921da177e4SLinus Torvalds 
2931da177e4SLinus Torvalds 
2941da177e4SLinus Torvalds /**
2951da177e4SLinus Torvalds  * zfcp_erp_adisc - send ADISC ELS command
29664b29a13SAndreas Herrmann  * @port: port structure
2971da177e4SLinus Torvalds  */
2982b67fc46SHeiko Carstens static int
29964b29a13SAndreas Herrmann zfcp_erp_adisc(struct zfcp_port *port)
3001da177e4SLinus Torvalds {
30164b29a13SAndreas Herrmann 	struct zfcp_adapter *adapter = port->adapter;
3021da177e4SLinus Torvalds 	struct zfcp_send_els *send_els;
3031da177e4SLinus Torvalds 	struct zfcp_ls_adisc *adisc;
3041da177e4SLinus Torvalds 	void *address = NULL;
3051da177e4SLinus Torvalds 	int retval = 0;
3061da177e4SLinus Torvalds 
307ec4081c6SAndreas Herrmann 	send_els = kzalloc(sizeof(struct zfcp_send_els), GFP_ATOMIC);
3081da177e4SLinus Torvalds 	if (send_els == NULL)
3091da177e4SLinus Torvalds 		goto nomem;
3101da177e4SLinus Torvalds 
31173fc4f0dSJens Axboe 	send_els->req = kmalloc(sizeof(struct scatterlist), GFP_ATOMIC);
3121da177e4SLinus Torvalds 	if (send_els->req == NULL)
3131da177e4SLinus Torvalds 		goto nomem;
31473fc4f0dSJens Axboe 	sg_init_table(send_els->req, 1);
3151da177e4SLinus Torvalds 
31673fc4f0dSJens Axboe 	send_els->resp = kmalloc(sizeof(struct scatterlist), GFP_ATOMIC);
3171da177e4SLinus Torvalds 	if (send_els->resp == NULL)
3181da177e4SLinus Torvalds 		goto nomem;
31973fc4f0dSJens Axboe 	sg_init_table(send_els->resp, 1);
3201da177e4SLinus Torvalds 
3211da177e4SLinus Torvalds 	address = (void *) get_zeroed_page(GFP_ATOMIC);
3221da177e4SLinus Torvalds 	if (address == NULL)
3231da177e4SLinus Torvalds 		goto nomem;
3241da177e4SLinus Torvalds 
325642f1490SJens Axboe 	zfcp_address_to_sg(address, send_els->req, sizeof(struct zfcp_ls_adisc));
3261da177e4SLinus Torvalds 	address += PAGE_SIZE >> 1;
327642f1490SJens Axboe 	zfcp_address_to_sg(address, send_els->resp, sizeof(struct zfcp_ls_adisc_acc));
3281da177e4SLinus Torvalds 	send_els->req_count = send_els->resp_count = 1;
3291da177e4SLinus Torvalds 
3301da177e4SLinus Torvalds 	send_els->adapter = adapter;
33164b29a13SAndreas Herrmann 	send_els->port = port;
33264b29a13SAndreas Herrmann 	send_els->d_id = port->d_id;
3331da177e4SLinus Torvalds 	send_els->handler = zfcp_erp_adisc_handler;
3341da177e4SLinus Torvalds 	send_els->handler_data = (unsigned long) send_els;
3351da177e4SLinus Torvalds 
3361da177e4SLinus Torvalds 	adisc = zfcp_sg_to_address(send_els->req);
3371da177e4SLinus Torvalds 	send_els->ls_code = adisc->code = ZFCP_LS_ADISC;
3381da177e4SLinus Torvalds 
3391da177e4SLinus Torvalds 	/* acc. to FC-FS, hard_nport_id in ADISC should not be set for ports
3401da177e4SLinus Torvalds 	   without FC-AL-2 capability, so we don't set it */
34113e1e1f0SAndreas Herrmann 	adisc->wwpn = fc_host_port_name(adapter->scsi_host);
34213e1e1f0SAndreas Herrmann 	adisc->wwnn = fc_host_node_name(adapter->scsi_host);
34313e1e1f0SAndreas Herrmann 	adisc->nport_id = fc_host_port_id(adapter->scsi_host);
3441d589edfSChristof Schmitt 	ZFCP_LOG_INFO("ADISC request from s_id 0x%06x to d_id 0x%06x "
3451da177e4SLinus Torvalds 		      "(wwpn=0x%016Lx, wwnn=0x%016Lx, "
3461d589edfSChristof Schmitt 		      "hard_nport_id=0x%06x, nport_id=0x%06x)\n",
34713e1e1f0SAndreas Herrmann 		      adisc->nport_id, send_els->d_id, (wwn_t) adisc->wwpn,
3481da177e4SLinus Torvalds 		      (wwn_t) adisc->wwnn, adisc->hard_nport_id,
3491da177e4SLinus Torvalds 		      adisc->nport_id);
3501da177e4SLinus Torvalds 
3511da177e4SLinus Torvalds 	retval = zfcp_fsf_send_els(send_els);
3521da177e4SLinus Torvalds 	if (retval != 0) {
3531da177e4SLinus Torvalds 		ZFCP_LOG_NORMAL("error: initiation of Send ELS failed for port "
3541d589edfSChristof Schmitt 				"0x%06x on adapter %s\n", send_els->d_id,
3551da177e4SLinus Torvalds 				zfcp_get_busid_by_adapter(adapter));
3561da177e4SLinus Torvalds 		goto freemem;
3571da177e4SLinus Torvalds 	}
3581da177e4SLinus Torvalds 
3591da177e4SLinus Torvalds 	goto out;
3601da177e4SLinus Torvalds 
3611da177e4SLinus Torvalds  nomem:
3621da177e4SLinus Torvalds 	retval = -ENOMEM;
3631da177e4SLinus Torvalds  freemem:
3641da177e4SLinus Torvalds 	if (address != NULL)
36573fc4f0dSJens Axboe 		__free_pages(sg_page(send_els->req), 0);
3661da177e4SLinus Torvalds 	if (send_els != NULL) {
3671da177e4SLinus Torvalds 		kfree(send_els->req);
3681da177e4SLinus Torvalds 		kfree(send_els->resp);
3691da177e4SLinus Torvalds 		kfree(send_els);
3701da177e4SLinus Torvalds 	}
3711da177e4SLinus Torvalds  out:
3721da177e4SLinus Torvalds 	return retval;
3731da177e4SLinus Torvalds }
3741da177e4SLinus Torvalds 
3751da177e4SLinus Torvalds 
3761da177e4SLinus Torvalds /**
3771da177e4SLinus Torvalds  * zfcp_erp_adisc_handler - handler for ADISC ELS command
3781da177e4SLinus Torvalds  * @data: pointer to struct zfcp_send_els
3791da177e4SLinus Torvalds  *
3801da177e4SLinus Torvalds  * If ADISC failed (LS_RJT or timed out) forced reopen of the port is triggered.
3811da177e4SLinus Torvalds  */
3822b67fc46SHeiko Carstens static void
3831da177e4SLinus Torvalds zfcp_erp_adisc_handler(unsigned long data)
3841da177e4SLinus Torvalds {
3851da177e4SLinus Torvalds 	struct zfcp_send_els *send_els;
3861da177e4SLinus Torvalds 	struct zfcp_port *port;
3871da177e4SLinus Torvalds 	struct zfcp_adapter *adapter;
38813e1e1f0SAndreas Herrmann 	u32 d_id;
3891da177e4SLinus Torvalds 	struct zfcp_ls_adisc_acc *adisc;
3901da177e4SLinus Torvalds 
3911da177e4SLinus Torvalds 	send_els = (struct zfcp_send_els *) data;
3921da177e4SLinus Torvalds 	adapter = send_els->adapter;
39364b29a13SAndreas Herrmann 	port = send_els->port;
3941da177e4SLinus Torvalds 	d_id = send_els->d_id;
3951da177e4SLinus Torvalds 
3961da177e4SLinus Torvalds 	/* request rejected or timed out */
3971da177e4SLinus Torvalds 	if (send_els->status != 0) {
3981da177e4SLinus Torvalds 		ZFCP_LOG_NORMAL("ELS request rejected/timed out, "
3991da177e4SLinus Torvalds 				"force physical port reopen "
4001d589edfSChristof Schmitt 				"(adapter %s, port d_id=0x%06x)\n",
4011da177e4SLinus Torvalds 				zfcp_get_busid_by_adapter(adapter), d_id);
4021da177e4SLinus Torvalds 		debug_text_event(adapter->erp_dbf, 3, "forcreop");
4031da177e4SLinus Torvalds 		if (zfcp_erp_port_forced_reopen(port, 0))
4041da177e4SLinus Torvalds 			ZFCP_LOG_NORMAL("failed reopen of port "
4051da177e4SLinus Torvalds 					"(adapter %s, wwpn=0x%016Lx)\n",
4061da177e4SLinus Torvalds 					zfcp_get_busid_by_port(port),
4071da177e4SLinus Torvalds 					port->wwpn);
4081da177e4SLinus Torvalds 		goto out;
4091da177e4SLinus Torvalds 	}
4101da177e4SLinus Torvalds 
4111da177e4SLinus Torvalds 	adisc = zfcp_sg_to_address(send_els->resp);
4121da177e4SLinus Torvalds 
4131d589edfSChristof Schmitt 	ZFCP_LOG_INFO("ADISC response from d_id 0x%06x to s_id "
4141d589edfSChristof Schmitt 		      "0x%06x (wwpn=0x%016Lx, wwnn=0x%016Lx, "
4151d589edfSChristof Schmitt 		      "hard_nport_id=0x%06x, nport_id=0x%06x)\n",
41613e1e1f0SAndreas Herrmann 		      d_id, fc_host_port_id(adapter->scsi_host),
41713e1e1f0SAndreas Herrmann 		      (wwn_t) adisc->wwpn, (wwn_t) adisc->wwnn,
41813e1e1f0SAndreas Herrmann 		      adisc->hard_nport_id, adisc->nport_id);
4191da177e4SLinus Torvalds 
4201da177e4SLinus Torvalds 	/* set wwnn for port */
4211da177e4SLinus Torvalds 	if (port->wwnn == 0)
4221da177e4SLinus Torvalds 		port->wwnn = adisc->wwnn;
4231da177e4SLinus Torvalds 
4241da177e4SLinus Torvalds 	if (port->wwpn != adisc->wwpn) {
4251da177e4SLinus Torvalds 		ZFCP_LOG_NORMAL("d_id assignment changed, reopening "
4261da177e4SLinus Torvalds 				"port (adapter %s, wwpn=0x%016Lx, "
4271da177e4SLinus Torvalds 				"adisc_resp_wwpn=0x%016Lx)\n",
4281da177e4SLinus Torvalds 				zfcp_get_busid_by_port(port),
4291da177e4SLinus Torvalds 				port->wwpn, (wwn_t) adisc->wwpn);
4301da177e4SLinus Torvalds 		if (zfcp_erp_port_reopen(port, 0))
4311da177e4SLinus Torvalds 			ZFCP_LOG_NORMAL("failed reopen of port "
4321da177e4SLinus Torvalds 					"(adapter %s, wwpn=0x%016Lx)\n",
4331da177e4SLinus Torvalds 					zfcp_get_busid_by_port(port),
4341da177e4SLinus Torvalds 					port->wwpn);
4351da177e4SLinus Torvalds 	}
4361da177e4SLinus Torvalds 
4371da177e4SLinus Torvalds  out:
4381da177e4SLinus Torvalds 	zfcp_port_put(port);
43973fc4f0dSJens Axboe 	__free_pages(sg_page(send_els->req), 0);
4401da177e4SLinus Torvalds 	kfree(send_els->req);
4411da177e4SLinus Torvalds 	kfree(send_els->resp);
4421da177e4SLinus Torvalds 	kfree(send_els);
4431da177e4SLinus Torvalds }
4441da177e4SLinus Torvalds 
4451da177e4SLinus Torvalds 
4461da177e4SLinus Torvalds /**
4471da177e4SLinus Torvalds  * zfcp_test_link - lightweight link test procedure
4481da177e4SLinus Torvalds  * @port: port to be tested
4491da177e4SLinus Torvalds  *
4501da177e4SLinus Torvalds  * Test status of a link to a remote port using the ELS command ADISC.
4511da177e4SLinus Torvalds  */
4521da177e4SLinus Torvalds int
4531da177e4SLinus Torvalds zfcp_test_link(struct zfcp_port *port)
4541da177e4SLinus Torvalds {
4551da177e4SLinus Torvalds 	int retval;
4561da177e4SLinus Torvalds 
4571da177e4SLinus Torvalds 	zfcp_port_get(port);
45864b29a13SAndreas Herrmann 	retval = zfcp_erp_adisc(port);
4593f0ca62aSChristof Schmitt 	if (retval != 0 && retval != -EBUSY) {
4601da177e4SLinus Torvalds 		zfcp_port_put(port);
4611da177e4SLinus Torvalds 		ZFCP_LOG_NORMAL("reopen needed for port 0x%016Lx "
4621da177e4SLinus Torvalds 				"on adapter %s\n ", port->wwpn,
4631da177e4SLinus Torvalds 				zfcp_get_busid_by_port(port));
4641da177e4SLinus Torvalds 		retval = zfcp_erp_port_forced_reopen(port, 0);
4651da177e4SLinus Torvalds 		if (retval != 0) {
4661da177e4SLinus Torvalds 			ZFCP_LOG_NORMAL("reopen of remote port 0x%016Lx "
4671da177e4SLinus Torvalds 					"on adapter %s failed\n", port->wwpn,
4681da177e4SLinus Torvalds 					zfcp_get_busid_by_port(port));
4691da177e4SLinus Torvalds 			retval = -EPERM;
4701da177e4SLinus Torvalds 		}
4711da177e4SLinus Torvalds 	}
4721da177e4SLinus Torvalds 
4731da177e4SLinus Torvalds 	return retval;
4741da177e4SLinus Torvalds }
4751da177e4SLinus Torvalds 
4761da177e4SLinus Torvalds 
4771da177e4SLinus Torvalds /*
4781da177e4SLinus Torvalds  * function:
4791da177e4SLinus Torvalds  *
4801da177e4SLinus Torvalds  * purpose:	called if a port failed to be opened normally
4811da177e4SLinus Torvalds  *		initiates Forced Reopen recovery which is done
4821da177e4SLinus Torvalds  *		asynchronously
4831da177e4SLinus Torvalds  *
484d6e05edcSAndreas Mohr  * returns:	0	- initiated action successfully
4851da177e4SLinus Torvalds  *		<0	- failed to initiate action
4861da177e4SLinus Torvalds  */
4871da177e4SLinus Torvalds static int
4881da177e4SLinus Torvalds zfcp_erp_port_forced_reopen_internal(struct zfcp_port *port, int clear_mask)
4891da177e4SLinus Torvalds {
4901da177e4SLinus Torvalds 	int retval;
4911da177e4SLinus Torvalds 	struct zfcp_adapter *adapter = port->adapter;
4921da177e4SLinus Torvalds 
4931da177e4SLinus Torvalds 	debug_text_event(adapter->erp_dbf, 5, "pf_ro");
4941da177e4SLinus Torvalds 	debug_event(adapter->erp_dbf, 5, &port->wwpn, sizeof (wwn_t));
4951da177e4SLinus Torvalds 
4961da177e4SLinus Torvalds 	ZFCP_LOG_DEBUG("forced reopen of port 0x%016Lx on adapter %s\n",
4971da177e4SLinus Torvalds 		       port->wwpn, zfcp_get_busid_by_port(port));
4981da177e4SLinus Torvalds 
4991da177e4SLinus Torvalds 	zfcp_erp_port_block(port, clear_mask);
5001da177e4SLinus Torvalds 
5011da177e4SLinus Torvalds 	if (atomic_test_mask(ZFCP_STATUS_COMMON_ERP_FAILED, &port->status)) {
5021da177e4SLinus Torvalds 		ZFCP_LOG_DEBUG("skipped forced reopen of failed port 0x%016Lx "
5031da177e4SLinus Torvalds 			       "on adapter %s\n", port->wwpn,
5041da177e4SLinus Torvalds 			       zfcp_get_busid_by_port(port));
5051da177e4SLinus Torvalds 		debug_text_event(adapter->erp_dbf, 5, "pf_ro_f");
5061da177e4SLinus Torvalds 		debug_event(adapter->erp_dbf, 5, &port->wwpn, sizeof (wwn_t));
5071da177e4SLinus Torvalds 		retval = -EIO;
5081da177e4SLinus Torvalds 		goto out;
5091da177e4SLinus Torvalds 	}
5101da177e4SLinus Torvalds 
5111da177e4SLinus Torvalds 	retval = zfcp_erp_action_enqueue(ZFCP_ERP_ACTION_REOPEN_PORT_FORCED,
5121da177e4SLinus Torvalds 					 port->adapter, port, NULL);
5131da177e4SLinus Torvalds 
5141da177e4SLinus Torvalds  out:
5151da177e4SLinus Torvalds 	return retval;
5161da177e4SLinus Torvalds }
5171da177e4SLinus Torvalds 
5181da177e4SLinus Torvalds /*
5191da177e4SLinus Torvalds  * function:
5201da177e4SLinus Torvalds  *
5211da177e4SLinus Torvalds  * purpose:	Wrappper for zfcp_erp_port_forced_reopen_internal
5221da177e4SLinus Torvalds  *              used to ensure the correct locking
5231da177e4SLinus Torvalds  *
524d6e05edcSAndreas Mohr  * returns:	0	- initiated action successfully
5251da177e4SLinus Torvalds  *		<0	- failed to initiate action
5261da177e4SLinus Torvalds  */
5271da177e4SLinus Torvalds int
5281da177e4SLinus Torvalds zfcp_erp_port_forced_reopen(struct zfcp_port *port, int clear_mask)
5291da177e4SLinus Torvalds {
5301da177e4SLinus Torvalds 	int retval;
5311da177e4SLinus Torvalds 	unsigned long flags;
5321da177e4SLinus Torvalds 	struct zfcp_adapter *adapter;
5331da177e4SLinus Torvalds 
5341da177e4SLinus Torvalds 	adapter = port->adapter;
5351da177e4SLinus Torvalds 	read_lock_irqsave(&zfcp_data.config_lock, flags);
5361da177e4SLinus Torvalds 	write_lock(&adapter->erp_lock);
5371da177e4SLinus Torvalds 	retval = zfcp_erp_port_forced_reopen_internal(port, clear_mask);
5381da177e4SLinus Torvalds 	write_unlock(&adapter->erp_lock);
5391da177e4SLinus Torvalds 	read_unlock_irqrestore(&zfcp_data.config_lock, flags);
5401da177e4SLinus Torvalds 
5411da177e4SLinus Torvalds 	return retval;
5421da177e4SLinus Torvalds }
5431da177e4SLinus Torvalds 
5441da177e4SLinus Torvalds /*
5451da177e4SLinus Torvalds  * function:
5461da177e4SLinus Torvalds  *
5471da177e4SLinus Torvalds  * purpose:	called if a port is to be opened
5481da177e4SLinus Torvalds  *		initiates Reopen recovery which is done
5491da177e4SLinus Torvalds  *		asynchronously
5501da177e4SLinus Torvalds  *
551d6e05edcSAndreas Mohr  * returns:	0	- initiated action successfully
5521da177e4SLinus Torvalds  *		<0	- failed to initiate action
5531da177e4SLinus Torvalds  */
5541da177e4SLinus Torvalds static int
5551da177e4SLinus Torvalds zfcp_erp_port_reopen_internal(struct zfcp_port *port, int clear_mask)
5561da177e4SLinus Torvalds {
5571da177e4SLinus Torvalds 	int retval;
5581da177e4SLinus Torvalds 	struct zfcp_adapter *adapter = port->adapter;
5591da177e4SLinus Torvalds 
5601da177e4SLinus Torvalds 	debug_text_event(adapter->erp_dbf, 5, "p_ro");
5611da177e4SLinus Torvalds 	debug_event(adapter->erp_dbf, 5, &port->wwpn, sizeof (wwn_t));
5621da177e4SLinus Torvalds 
5631da177e4SLinus Torvalds 	ZFCP_LOG_DEBUG("reopen of port 0x%016Lx on adapter %s\n",
5641da177e4SLinus Torvalds 		       port->wwpn, zfcp_get_busid_by_port(port));
5651da177e4SLinus Torvalds 
5661da177e4SLinus Torvalds 	zfcp_erp_port_block(port, clear_mask);
5671da177e4SLinus Torvalds 
5681da177e4SLinus Torvalds 	if (atomic_test_mask(ZFCP_STATUS_COMMON_ERP_FAILED, &port->status)) {
5691da177e4SLinus Torvalds 		ZFCP_LOG_DEBUG("skipped reopen of failed port 0x%016Lx "
5701da177e4SLinus Torvalds 			       "on adapter %s\n", port->wwpn,
5711da177e4SLinus Torvalds 			       zfcp_get_busid_by_port(port));
5721da177e4SLinus Torvalds 		debug_text_event(adapter->erp_dbf, 5, "p_ro_f");
5731da177e4SLinus Torvalds 		debug_event(adapter->erp_dbf, 5, &port->wwpn, sizeof (wwn_t));
5741da177e4SLinus Torvalds 		/* ensure propagation of failed status to new devices */
5751da177e4SLinus Torvalds 		zfcp_erp_port_failed(port);
5761da177e4SLinus Torvalds 		retval = -EIO;
5771da177e4SLinus Torvalds 		goto out;
5781da177e4SLinus Torvalds 	}
5791da177e4SLinus Torvalds 
5801da177e4SLinus Torvalds 	retval = zfcp_erp_action_enqueue(ZFCP_ERP_ACTION_REOPEN_PORT,
5811da177e4SLinus Torvalds 					 port->adapter, port, NULL);
5821da177e4SLinus Torvalds 
5831da177e4SLinus Torvalds  out:
5841da177e4SLinus Torvalds 	return retval;
5851da177e4SLinus Torvalds }
5861da177e4SLinus Torvalds 
5871da177e4SLinus Torvalds /**
5881da177e4SLinus Torvalds  * zfcp_erp_port_reopen - initiate reopen of a remote port
5891da177e4SLinus Torvalds  * @port: port to be reopened
5901da177e4SLinus Torvalds  * @clear_mask: specifies flags in port status to be cleared
5911da177e4SLinus Torvalds  * Return: 0 on success, < 0 on error
5921da177e4SLinus Torvalds  *
5931da177e4SLinus Torvalds  * This is a wrappper function for zfcp_erp_port_reopen_internal. It ensures
5941da177e4SLinus Torvalds  * correct locking. An error recovery task is initiated to do the reopen.
5951da177e4SLinus Torvalds  * To wait for the completion of the reopen zfcp_erp_wait should be used.
5961da177e4SLinus Torvalds  */
5971da177e4SLinus Torvalds int
5981da177e4SLinus Torvalds zfcp_erp_port_reopen(struct zfcp_port *port, int clear_mask)
5991da177e4SLinus Torvalds {
6001da177e4SLinus Torvalds 	int retval;
6011da177e4SLinus Torvalds 	unsigned long flags;
6021da177e4SLinus Torvalds 	struct zfcp_adapter *adapter = port->adapter;
6031da177e4SLinus Torvalds 
6041da177e4SLinus Torvalds 	read_lock_irqsave(&zfcp_data.config_lock, flags);
6051da177e4SLinus Torvalds 	write_lock(&adapter->erp_lock);
6061da177e4SLinus Torvalds 	retval = zfcp_erp_port_reopen_internal(port, clear_mask);
6071da177e4SLinus Torvalds 	write_unlock(&adapter->erp_lock);
6081da177e4SLinus Torvalds 	read_unlock_irqrestore(&zfcp_data.config_lock, flags);
6091da177e4SLinus Torvalds 
6101da177e4SLinus Torvalds 	return retval;
6111da177e4SLinus Torvalds }
6121da177e4SLinus Torvalds 
6131da177e4SLinus Torvalds /*
6141da177e4SLinus Torvalds  * function:
6151da177e4SLinus Torvalds  *
6161da177e4SLinus Torvalds  * purpose:	called if a unit is to be opened
6171da177e4SLinus Torvalds  *		initiates Reopen recovery which is done
6181da177e4SLinus Torvalds  *		asynchronously
6191da177e4SLinus Torvalds  *
620d6e05edcSAndreas Mohr  * returns:	0	- initiated action successfully
6211da177e4SLinus Torvalds  *		<0	- failed to initiate action
6221da177e4SLinus Torvalds  */
6231da177e4SLinus Torvalds static int
6241da177e4SLinus Torvalds zfcp_erp_unit_reopen_internal(struct zfcp_unit *unit, int clear_mask)
6251da177e4SLinus Torvalds {
6261da177e4SLinus Torvalds 	int retval;
6271da177e4SLinus Torvalds 	struct zfcp_adapter *adapter = unit->port->adapter;
6281da177e4SLinus Torvalds 
6291da177e4SLinus Torvalds 	debug_text_event(adapter->erp_dbf, 5, "u_ro");
6301da177e4SLinus Torvalds 	debug_event(adapter->erp_dbf, 5, &unit->fcp_lun, sizeof (fcp_lun_t));
6311da177e4SLinus Torvalds 	ZFCP_LOG_DEBUG("reopen of unit 0x%016Lx on port 0x%016Lx "
6321da177e4SLinus Torvalds 		       "on adapter %s\n", unit->fcp_lun,
6331da177e4SLinus Torvalds 		       unit->port->wwpn, zfcp_get_busid_by_unit(unit));
6341da177e4SLinus Torvalds 
6351da177e4SLinus Torvalds 	zfcp_erp_unit_block(unit, clear_mask);
6361da177e4SLinus Torvalds 
6371da177e4SLinus Torvalds 	if (atomic_test_mask(ZFCP_STATUS_COMMON_ERP_FAILED, &unit->status)) {
6381da177e4SLinus Torvalds 		ZFCP_LOG_DEBUG("skipped reopen of failed unit 0x%016Lx "
6391da177e4SLinus Torvalds 			       "on port 0x%016Lx on adapter %s\n",
6401da177e4SLinus Torvalds 			       unit->fcp_lun, unit->port->wwpn,
6411da177e4SLinus Torvalds 			       zfcp_get_busid_by_unit(unit));
6421da177e4SLinus Torvalds 		debug_text_event(adapter->erp_dbf, 5, "u_ro_f");
6431da177e4SLinus Torvalds 		debug_event(adapter->erp_dbf, 5, &unit->fcp_lun,
6441da177e4SLinus Torvalds 			    sizeof (fcp_lun_t));
6451da177e4SLinus Torvalds 		retval = -EIO;
6461da177e4SLinus Torvalds 		goto out;
6471da177e4SLinus Torvalds 	}
6481da177e4SLinus Torvalds 
6491da177e4SLinus Torvalds 	retval = zfcp_erp_action_enqueue(ZFCP_ERP_ACTION_REOPEN_UNIT,
6501da177e4SLinus Torvalds 					 unit->port->adapter, unit->port, unit);
6511da177e4SLinus Torvalds  out:
6521da177e4SLinus Torvalds 	return retval;
6531da177e4SLinus Torvalds }
6541da177e4SLinus Torvalds 
6551da177e4SLinus Torvalds /**
6561da177e4SLinus Torvalds  * zfcp_erp_unit_reopen - initiate reopen of a unit
6571da177e4SLinus Torvalds  * @unit: unit to be reopened
6581da177e4SLinus Torvalds  * @clear_mask: specifies flags in unit status to be cleared
6591da177e4SLinus Torvalds  * Return: 0 on success, < 0 on error
6601da177e4SLinus Torvalds  *
6611da177e4SLinus Torvalds  * This is a wrappper for zfcp_erp_unit_reopen_internal. It ensures correct
6621da177e4SLinus Torvalds  * locking. An error recovery task is initiated to do the reopen.
6631da177e4SLinus Torvalds  * To wait for the completion of the reopen zfcp_erp_wait should be used.
6641da177e4SLinus Torvalds  */
6651da177e4SLinus Torvalds int
6661da177e4SLinus Torvalds zfcp_erp_unit_reopen(struct zfcp_unit *unit, int clear_mask)
6671da177e4SLinus Torvalds {
6681da177e4SLinus Torvalds 	int retval;
6691da177e4SLinus Torvalds 	unsigned long flags;
6701da177e4SLinus Torvalds 	struct zfcp_adapter *adapter;
6711da177e4SLinus Torvalds 	struct zfcp_port *port;
6721da177e4SLinus Torvalds 
6731da177e4SLinus Torvalds 	port = unit->port;
6741da177e4SLinus Torvalds 	adapter = port->adapter;
6751da177e4SLinus Torvalds 
6761da177e4SLinus Torvalds 	read_lock_irqsave(&zfcp_data.config_lock, flags);
6771da177e4SLinus Torvalds 	write_lock(&adapter->erp_lock);
6781da177e4SLinus Torvalds 	retval = zfcp_erp_unit_reopen_internal(unit, clear_mask);
6791da177e4SLinus Torvalds 	write_unlock(&adapter->erp_lock);
6801da177e4SLinus Torvalds 	read_unlock_irqrestore(&zfcp_data.config_lock, flags);
6811da177e4SLinus Torvalds 
6821da177e4SLinus Torvalds 	return retval;
6831da177e4SLinus Torvalds }
6841da177e4SLinus Torvalds 
685f6c0e7a7SAndreas Herrmann /**
686f6c0e7a7SAndreas Herrmann  * zfcp_erp_adapter_block - mark adapter as blocked, block scsi requests
6871da177e4SLinus Torvalds  */
688f6c0e7a7SAndreas Herrmann static void zfcp_erp_adapter_block(struct zfcp_adapter *adapter, int clear_mask)
6891da177e4SLinus Torvalds {
6901da177e4SLinus Torvalds 	debug_text_event(adapter->erp_dbf, 6, "a_bl");
6911da177e4SLinus Torvalds 	zfcp_erp_modify_adapter_status(adapter,
6921da177e4SLinus Torvalds 				       ZFCP_STATUS_COMMON_UNBLOCKED |
6931da177e4SLinus Torvalds 				       clear_mask, ZFCP_CLEAR);
6941da177e4SLinus Torvalds }
6951da177e4SLinus Torvalds 
696f6c0e7a7SAndreas Herrmann /**
697f6c0e7a7SAndreas Herrmann  * zfcp_erp_adapter_unblock - mark adapter as unblocked, allow scsi requests
6981da177e4SLinus Torvalds  */
699f6c0e7a7SAndreas Herrmann static void zfcp_erp_adapter_unblock(struct zfcp_adapter *adapter)
7001da177e4SLinus Torvalds {
7011da177e4SLinus Torvalds 	debug_text_event(adapter->erp_dbf, 6, "a_ubl");
7021da177e4SLinus Torvalds 	atomic_set_mask(ZFCP_STATUS_COMMON_UNBLOCKED, &adapter->status);
7031da177e4SLinus Torvalds }
7041da177e4SLinus Torvalds 
7051da177e4SLinus Torvalds /*
7061da177e4SLinus Torvalds  * function:
7071da177e4SLinus Torvalds  *
7081da177e4SLinus Torvalds  * purpose:	disable I/O,
7091da177e4SLinus Torvalds  *		return any open requests and clean them up,
7101da177e4SLinus Torvalds  *		aim: no pending and incoming I/O
7111da177e4SLinus Torvalds  *
7121da177e4SLinus Torvalds  * returns:
7131da177e4SLinus Torvalds  */
7141da177e4SLinus Torvalds static void
7151da177e4SLinus Torvalds zfcp_erp_port_block(struct zfcp_port *port, int clear_mask)
7161da177e4SLinus Torvalds {
7171da177e4SLinus Torvalds 	struct zfcp_adapter *adapter = port->adapter;
7181da177e4SLinus Torvalds 
7191da177e4SLinus Torvalds 	debug_text_event(adapter->erp_dbf, 6, "p_bl");
7201da177e4SLinus Torvalds 	debug_event(adapter->erp_dbf, 6, &port->wwpn, sizeof (wwn_t));
7211da177e4SLinus Torvalds 	zfcp_erp_modify_port_status(port,
7221da177e4SLinus Torvalds 				    ZFCP_STATUS_COMMON_UNBLOCKED | clear_mask,
7231da177e4SLinus Torvalds 				    ZFCP_CLEAR);
7241da177e4SLinus Torvalds }
7251da177e4SLinus Torvalds 
7261da177e4SLinus Torvalds /*
7271da177e4SLinus Torvalds  * function:
7281da177e4SLinus Torvalds  *
7291da177e4SLinus Torvalds  * purpose:	enable I/O
7301da177e4SLinus Torvalds  *
7311da177e4SLinus Torvalds  * returns:
7321da177e4SLinus Torvalds  */
7331da177e4SLinus Torvalds static void
7341da177e4SLinus Torvalds zfcp_erp_port_unblock(struct zfcp_port *port)
7351da177e4SLinus Torvalds {
7361da177e4SLinus Torvalds 	struct zfcp_adapter *adapter = port->adapter;
7371da177e4SLinus Torvalds 
7381da177e4SLinus Torvalds 	debug_text_event(adapter->erp_dbf, 6, "p_ubl");
7391da177e4SLinus Torvalds 	debug_event(adapter->erp_dbf, 6, &port->wwpn, sizeof (wwn_t));
7401da177e4SLinus Torvalds 	atomic_set_mask(ZFCP_STATUS_COMMON_UNBLOCKED, &port->status);
7411da177e4SLinus Torvalds }
7421da177e4SLinus Torvalds 
7431da177e4SLinus Torvalds /*
7441da177e4SLinus Torvalds  * function:
7451da177e4SLinus Torvalds  *
7461da177e4SLinus Torvalds  * purpose:	disable I/O,
7471da177e4SLinus Torvalds  *		return any open requests and clean them up,
7481da177e4SLinus Torvalds  *		aim: no pending and incoming I/O
7491da177e4SLinus Torvalds  *
7501da177e4SLinus Torvalds  * returns:
7511da177e4SLinus Torvalds  */
7521da177e4SLinus Torvalds static void
7531da177e4SLinus Torvalds zfcp_erp_unit_block(struct zfcp_unit *unit, int clear_mask)
7541da177e4SLinus Torvalds {
7551da177e4SLinus Torvalds 	struct zfcp_adapter *adapter = unit->port->adapter;
7561da177e4SLinus Torvalds 
7571da177e4SLinus Torvalds 	debug_text_event(adapter->erp_dbf, 6, "u_bl");
7581da177e4SLinus Torvalds 	debug_event(adapter->erp_dbf, 6, &unit->fcp_lun, sizeof (fcp_lun_t));
7591da177e4SLinus Torvalds 	zfcp_erp_modify_unit_status(unit,
7601da177e4SLinus Torvalds 				    ZFCP_STATUS_COMMON_UNBLOCKED | clear_mask,
7611da177e4SLinus Torvalds 				    ZFCP_CLEAR);
7621da177e4SLinus Torvalds }
7631da177e4SLinus Torvalds 
7641da177e4SLinus Torvalds /*
7651da177e4SLinus Torvalds  * function:
7661da177e4SLinus Torvalds  *
7671da177e4SLinus Torvalds  * purpose:	enable I/O
7681da177e4SLinus Torvalds  *
7691da177e4SLinus Torvalds  * returns:
7701da177e4SLinus Torvalds  */
7711da177e4SLinus Torvalds static void
7721da177e4SLinus Torvalds zfcp_erp_unit_unblock(struct zfcp_unit *unit)
7731da177e4SLinus Torvalds {
7741da177e4SLinus Torvalds 	struct zfcp_adapter *adapter = unit->port->adapter;
7751da177e4SLinus Torvalds 
7761da177e4SLinus Torvalds 	debug_text_event(adapter->erp_dbf, 6, "u_ubl");
7771da177e4SLinus Torvalds 	debug_event(adapter->erp_dbf, 6, &unit->fcp_lun, sizeof (fcp_lun_t));
7781da177e4SLinus Torvalds 	atomic_set_mask(ZFCP_STATUS_COMMON_UNBLOCKED, &unit->status);
7791da177e4SLinus Torvalds }
7801da177e4SLinus Torvalds 
7811da177e4SLinus Torvalds static void
7821da177e4SLinus Torvalds zfcp_erp_action_ready(struct zfcp_erp_action *erp_action)
7831da177e4SLinus Torvalds {
7841da177e4SLinus Torvalds 	struct zfcp_adapter *adapter = erp_action->adapter;
7851da177e4SLinus Torvalds 
7861da177e4SLinus Torvalds 	debug_text_event(adapter->erp_dbf, 4, "a_ar");
7871da177e4SLinus Torvalds 	debug_event(adapter->erp_dbf, 4, &erp_action->action, sizeof (int));
7881da177e4SLinus Torvalds 
7891da177e4SLinus Torvalds 	zfcp_erp_action_to_ready(erp_action);
7901da177e4SLinus Torvalds 	up(&adapter->erp_ready_sem);
791348447e8SMartin Peschke 	zfcp_rec_dbf_event_thread(2, adapter, 0);
7921da177e4SLinus Torvalds }
7931da177e4SLinus Torvalds 
7941da177e4SLinus Torvalds /*
7951da177e4SLinus Torvalds  * function:
7961da177e4SLinus Torvalds  *
7971da177e4SLinus Torvalds  * purpose:
7981da177e4SLinus Torvalds  *
7991da177e4SLinus Torvalds  * returns:	<0			erp_action not found in any list
8001da177e4SLinus Torvalds  *		ZFCP_ERP_ACTION_READY	erp_action is in ready list
8011da177e4SLinus Torvalds  *		ZFCP_ERP_ACTION_RUNNING	erp_action is in running list
8021da177e4SLinus Torvalds  *
8031da177e4SLinus Torvalds  * locks:	erp_lock must be held
8041da177e4SLinus Torvalds  */
8051da177e4SLinus Torvalds static int
8061da177e4SLinus Torvalds zfcp_erp_action_exists(struct zfcp_erp_action *erp_action)
8071da177e4SLinus Torvalds {
8081da177e4SLinus Torvalds 	int retval = -EINVAL;
8091da177e4SLinus Torvalds 	struct list_head *entry;
8101da177e4SLinus Torvalds 	struct zfcp_erp_action *entry_erp_action;
8111da177e4SLinus Torvalds 	struct zfcp_adapter *adapter = erp_action->adapter;
8121da177e4SLinus Torvalds 
8131da177e4SLinus Torvalds 	/* search in running list */
8141da177e4SLinus Torvalds 	list_for_each(entry, &adapter->erp_running_head) {
8151da177e4SLinus Torvalds 		entry_erp_action =
8161da177e4SLinus Torvalds 		    list_entry(entry, struct zfcp_erp_action, list);
8171da177e4SLinus Torvalds 		if (entry_erp_action == erp_action) {
8181da177e4SLinus Torvalds 			retval = ZFCP_ERP_ACTION_RUNNING;
8191da177e4SLinus Torvalds 			goto out;
8201da177e4SLinus Torvalds 		}
8211da177e4SLinus Torvalds 	}
8221da177e4SLinus Torvalds 	/* search in ready list */
8231da177e4SLinus Torvalds 	list_for_each(entry, &adapter->erp_ready_head) {
8241da177e4SLinus Torvalds 		entry_erp_action =
8251da177e4SLinus Torvalds 		    list_entry(entry, struct zfcp_erp_action, list);
8261da177e4SLinus Torvalds 		if (entry_erp_action == erp_action) {
8271da177e4SLinus Torvalds 			retval = ZFCP_ERP_ACTION_READY;
8281da177e4SLinus Torvalds 			goto out;
8291da177e4SLinus Torvalds 		}
8301da177e4SLinus Torvalds 	}
8311da177e4SLinus Torvalds 
8321da177e4SLinus Torvalds  out:
8331da177e4SLinus Torvalds 	return retval;
8341da177e4SLinus Torvalds }
8351da177e4SLinus Torvalds 
8361da177e4SLinus Torvalds /*
8371da177e4SLinus Torvalds  * purpose:	checks current status of action (timed out, dismissed, ...)
8381da177e4SLinus Torvalds  *		and does appropriate preparations (dismiss fsf request, ...)
8391da177e4SLinus Torvalds  *
8401da177e4SLinus Torvalds  * locks:	called under erp_lock (disabled interrupts)
8411da177e4SLinus Torvalds  */
842ca880cf9SSwen Schillig static void
8431da177e4SLinus Torvalds zfcp_erp_strategy_check_fsfreq(struct zfcp_erp_action *erp_action)
8441da177e4SLinus Torvalds {
8451da177e4SLinus Torvalds 	struct zfcp_adapter *adapter = erp_action->adapter;
8461da177e4SLinus Torvalds 
8471da177e4SLinus Torvalds 	if (erp_action->fsf_req) {
848fea9d6c7SVolker Sameske 		/* take lock to ensure that request is not deleted meanwhile */
849fea9d6c7SVolker Sameske 		spin_lock(&adapter->req_list_lock);
850d1ad09dbSHeiko Carstens 		if (zfcp_reqlist_find_safe(adapter, erp_action->fsf_req) &&
851d1ad09dbSHeiko Carstens 		    erp_action->fsf_req->erp_action == erp_action) {
8521da177e4SLinus Torvalds 			/* fsf_req still exists */
8531da177e4SLinus Torvalds 			debug_text_event(adapter->erp_dbf, 3, "a_ca_req");
854ca880cf9SSwen Schillig 			debug_event(adapter->erp_dbf, 3, &erp_action->fsf_req,
8551da177e4SLinus Torvalds 				    sizeof (unsigned long));
856fea9d6c7SVolker Sameske 			/* dismiss fsf_req of timed out/dismissed erp_action */
8571da177e4SLinus Torvalds 			if (erp_action->status & (ZFCP_STATUS_ERP_DISMISSED |
8581da177e4SLinus Torvalds 						  ZFCP_STATUS_ERP_TIMEDOUT)) {
8591da177e4SLinus Torvalds 				debug_text_event(adapter->erp_dbf, 3,
8601da177e4SLinus Torvalds 						 "a_ca_disreq");
861ca880cf9SSwen Schillig 				erp_action->fsf_req->status |=
862ca880cf9SSwen Schillig 					ZFCP_STATUS_FSFREQ_DISMISSED;
8631da177e4SLinus Torvalds 			}
8641da177e4SLinus Torvalds 			if (erp_action->status & ZFCP_STATUS_ERP_TIMEDOUT) {
8651da177e4SLinus Torvalds 				ZFCP_LOG_NORMAL("error: erp step timed out "
8661da177e4SLinus Torvalds 						"(action=%d, fsf_req=%p)\n ",
8671da177e4SLinus Torvalds 						erp_action->action,
8681da177e4SLinus Torvalds 						erp_action->fsf_req);
8691da177e4SLinus Torvalds 			}
8701da177e4SLinus Torvalds 			/*
8711da177e4SLinus Torvalds 			 * If fsf_req is neither dismissed nor completed
8721da177e4SLinus Torvalds 			 * then keep it running asynchronously and don't mess
8731da177e4SLinus Torvalds 			 * with the association of erp_action and fsf_req.
8741da177e4SLinus Torvalds 			 */
875ca880cf9SSwen Schillig 			if (erp_action->fsf_req->status &
876ca880cf9SSwen Schillig 					(ZFCP_STATUS_FSFREQ_COMPLETED |
8771da177e4SLinus Torvalds 					       ZFCP_STATUS_FSFREQ_DISMISSED)) {
8781da177e4SLinus Torvalds 				/* forget about association between fsf_req
8791da177e4SLinus Torvalds 				   and erp_action */
8801da177e4SLinus Torvalds 				erp_action->fsf_req = NULL;
8811da177e4SLinus Torvalds 			}
8821da177e4SLinus Torvalds 		} else {
8831da177e4SLinus Torvalds 			debug_text_event(adapter->erp_dbf, 3, "a_ca_gonereq");
8841da177e4SLinus Torvalds 			/*
8851da177e4SLinus Torvalds 			 * even if this fsf_req has gone, forget about
8861da177e4SLinus Torvalds 			 * association between erp_action and fsf_req
8871da177e4SLinus Torvalds 			 */
8881da177e4SLinus Torvalds 			erp_action->fsf_req = NULL;
8891da177e4SLinus Torvalds 		}
890fea9d6c7SVolker Sameske 		spin_unlock(&adapter->req_list_lock);
8911da177e4SLinus Torvalds 	} else
8921da177e4SLinus Torvalds 		debug_text_event(adapter->erp_dbf, 3, "a_ca_noreq");
8931da177e4SLinus Torvalds }
8941da177e4SLinus Torvalds 
895f6c0e7a7SAndreas Herrmann /**
896f6c0e7a7SAndreas Herrmann  * zfcp_erp_async_handler_nolock - complete erp_action
8971da177e4SLinus Torvalds  *
898f6c0e7a7SAndreas Herrmann  * Used for normal completion, time-out, dismissal and failure after
899f6c0e7a7SAndreas Herrmann  * low memory condition.
9001da177e4SLinus Torvalds  */
901f6c0e7a7SAndreas Herrmann static void zfcp_erp_async_handler_nolock(struct zfcp_erp_action *erp_action,
9021da177e4SLinus Torvalds 					  unsigned long set_mask)
9031da177e4SLinus Torvalds {
9041da177e4SLinus Torvalds 	struct zfcp_adapter *adapter = erp_action->adapter;
9051da177e4SLinus Torvalds 
9061da177e4SLinus Torvalds 	if (zfcp_erp_action_exists(erp_action) == ZFCP_ERP_ACTION_RUNNING) {
9071da177e4SLinus Torvalds 		debug_text_event(adapter->erp_dbf, 2, "a_asyh_ex");
9081da177e4SLinus Torvalds 		debug_event(adapter->erp_dbf, 2, &erp_action->action,
9091da177e4SLinus Torvalds 			    sizeof (int));
9101da177e4SLinus Torvalds 		erp_action->status |= set_mask;
9111da177e4SLinus Torvalds 		zfcp_erp_action_ready(erp_action);
9121da177e4SLinus Torvalds 	} else {
9131da177e4SLinus Torvalds 		/* action is ready or gone - nothing to do */
9141da177e4SLinus Torvalds 		debug_text_event(adapter->erp_dbf, 3, "a_asyh_gone");
9151da177e4SLinus Torvalds 		debug_event(adapter->erp_dbf, 3, &erp_action->action,
9161da177e4SLinus Torvalds 			    sizeof (int));
917f6c0e7a7SAndreas Herrmann 	}
9181da177e4SLinus Torvalds }
9191da177e4SLinus Torvalds 
920f6c0e7a7SAndreas Herrmann /**
921f6c0e7a7SAndreas Herrmann  * zfcp_erp_async_handler - wrapper for erp_async_handler_nolock w/ locking
9221da177e4SLinus Torvalds  */
923f6c0e7a7SAndreas Herrmann void zfcp_erp_async_handler(struct zfcp_erp_action *erp_action,
9241da177e4SLinus Torvalds 			    unsigned long set_mask)
9251da177e4SLinus Torvalds {
9261da177e4SLinus Torvalds 	struct zfcp_adapter *adapter = erp_action->adapter;
9271da177e4SLinus Torvalds 	unsigned long flags;
9281da177e4SLinus Torvalds 
9291da177e4SLinus Torvalds 	write_lock_irqsave(&adapter->erp_lock, flags);
930f6c0e7a7SAndreas Herrmann 	zfcp_erp_async_handler_nolock(erp_action, set_mask);
9311da177e4SLinus Torvalds 	write_unlock_irqrestore(&adapter->erp_lock, flags);
9321da177e4SLinus Torvalds }
9331da177e4SLinus Torvalds 
9341da177e4SLinus Torvalds /*
9351da177e4SLinus Torvalds  * purpose:	is called for erp_action which was slept waiting for
9361da177e4SLinus Torvalds  *		memory becoming avaliable,
9371da177e4SLinus Torvalds  *		will trigger that this action will be continued
9381da177e4SLinus Torvalds  */
9391da177e4SLinus Torvalds static void
9401da177e4SLinus Torvalds zfcp_erp_memwait_handler(unsigned long data)
9411da177e4SLinus Torvalds {
9421da177e4SLinus Torvalds 	struct zfcp_erp_action *erp_action = (struct zfcp_erp_action *) data;
9431da177e4SLinus Torvalds 	struct zfcp_adapter *adapter = erp_action->adapter;
9441da177e4SLinus Torvalds 
9451da177e4SLinus Torvalds 	debug_text_event(adapter->erp_dbf, 2, "a_mwh");
9461da177e4SLinus Torvalds 	debug_event(adapter->erp_dbf, 2, &erp_action->action, sizeof (int));
9471da177e4SLinus Torvalds 
9481da177e4SLinus Torvalds 	zfcp_erp_async_handler(erp_action, 0);
9491da177e4SLinus Torvalds }
9501da177e4SLinus Torvalds 
9511da177e4SLinus Torvalds /*
9521da177e4SLinus Torvalds  * purpose:	is called if an asynchronous erp step timed out,
9531da177e4SLinus Torvalds  *		action gets an appropriate flag and will be processed
9541da177e4SLinus Torvalds  *		accordingly
9551da177e4SLinus Torvalds  */
956364c8558SHeiko Carstens static void zfcp_erp_timeout_handler(unsigned long data)
9571da177e4SLinus Torvalds {
9581da177e4SLinus Torvalds 	struct zfcp_erp_action *erp_action = (struct zfcp_erp_action *) data;
9591da177e4SLinus Torvalds 	struct zfcp_adapter *adapter = erp_action->adapter;
9601da177e4SLinus Torvalds 
9611da177e4SLinus Torvalds 	debug_text_event(adapter->erp_dbf, 2, "a_th");
9621da177e4SLinus Torvalds 	debug_event(adapter->erp_dbf, 2, &erp_action->action, sizeof (int));
9631da177e4SLinus Torvalds 
9641da177e4SLinus Torvalds 	zfcp_erp_async_handler(erp_action, ZFCP_STATUS_ERP_TIMEDOUT);
9651da177e4SLinus Torvalds }
9661da177e4SLinus Torvalds 
967f6c0e7a7SAndreas Herrmann /**
968f6c0e7a7SAndreas Herrmann  * zfcp_erp_action_dismiss - dismiss an erp_action
9691da177e4SLinus Torvalds  *
970f6c0e7a7SAndreas Herrmann  * adapter->erp_lock must be held
971f6c0e7a7SAndreas Herrmann  *
972f6c0e7a7SAndreas Herrmann  * Dismissal of an erp_action is usually required if an erp_action of
973f6c0e7a7SAndreas Herrmann  * higher priority is generated.
9741da177e4SLinus Torvalds  */
975f6c0e7a7SAndreas Herrmann static void zfcp_erp_action_dismiss(struct zfcp_erp_action *erp_action)
9761da177e4SLinus Torvalds {
9771da177e4SLinus Torvalds 	struct zfcp_adapter *adapter = erp_action->adapter;
9781da177e4SLinus Torvalds 
9791da177e4SLinus Torvalds 	debug_text_event(adapter->erp_dbf, 2, "a_adis");
9801da177e4SLinus Torvalds 	debug_event(adapter->erp_dbf, 2, &erp_action->action, sizeof (int));
9811da177e4SLinus Torvalds 
982d0076f77SMartin Peschke 	erp_action->status |= ZFCP_STATUS_ERP_DISMISSED;
983d0076f77SMartin Peschke 	if (zfcp_erp_action_exists(erp_action) == ZFCP_ERP_ACTION_RUNNING)
984d0076f77SMartin Peschke 		zfcp_erp_action_ready(erp_action);
9851da177e4SLinus Torvalds }
9861da177e4SLinus Torvalds 
9871da177e4SLinus Torvalds int
9881da177e4SLinus Torvalds zfcp_erp_thread_setup(struct zfcp_adapter *adapter)
9891da177e4SLinus Torvalds {
9901da177e4SLinus Torvalds 	int retval = 0;
9911da177e4SLinus Torvalds 
9921da177e4SLinus Torvalds 	atomic_clear_mask(ZFCP_STATUS_ADAPTER_ERP_THREAD_UP, &adapter->status);
9931da177e4SLinus Torvalds 
9941da177e4SLinus Torvalds 	retval = kernel_thread(zfcp_erp_thread, adapter, SIGCHLD);
9951da177e4SLinus Torvalds 	if (retval < 0) {
9961da177e4SLinus Torvalds 		ZFCP_LOG_NORMAL("error: creation of erp thread failed for "
9971da177e4SLinus Torvalds 				"adapter %s\n",
9981da177e4SLinus Torvalds 				zfcp_get_busid_by_adapter(adapter));
9991da177e4SLinus Torvalds 		debug_text_event(adapter->erp_dbf, 5, "a_thset_fail");
10001da177e4SLinus Torvalds 	} else {
10011da177e4SLinus Torvalds 		wait_event(adapter->erp_thread_wqh,
10021da177e4SLinus Torvalds 			   atomic_test_mask(ZFCP_STATUS_ADAPTER_ERP_THREAD_UP,
10031da177e4SLinus Torvalds 					    &adapter->status));
10041da177e4SLinus Torvalds 		debug_text_event(adapter->erp_dbf, 5, "a_thset_ok");
10051da177e4SLinus Torvalds 	}
10061da177e4SLinus Torvalds 
10071da177e4SLinus Torvalds 	return (retval < 0);
10081da177e4SLinus Torvalds }
10091da177e4SLinus Torvalds 
10101da177e4SLinus Torvalds /*
10111da177e4SLinus Torvalds  * function:
10121da177e4SLinus Torvalds  *
10131da177e4SLinus Torvalds  * purpose:
10141da177e4SLinus Torvalds  *
10151da177e4SLinus Torvalds  * returns:
10161da177e4SLinus Torvalds  *
10171da177e4SLinus Torvalds  * context:	process (i.e. proc-fs or rmmod/insmod)
10181da177e4SLinus Torvalds  *
10191da177e4SLinus Torvalds  * note:	The caller of this routine ensures that the specified
10201da177e4SLinus Torvalds  *		adapter has been shut down and that this operation
10211da177e4SLinus Torvalds  *		has been completed. Thus, there are no pending erp_actions
10221da177e4SLinus Torvalds  *		which would need to be handled here.
10231da177e4SLinus Torvalds  */
10241da177e4SLinus Torvalds int
10251da177e4SLinus Torvalds zfcp_erp_thread_kill(struct zfcp_adapter *adapter)
10261da177e4SLinus Torvalds {
10271da177e4SLinus Torvalds 	int retval = 0;
10281da177e4SLinus Torvalds 
10291da177e4SLinus Torvalds 	atomic_set_mask(ZFCP_STATUS_ADAPTER_ERP_THREAD_KILL, &adapter->status);
10301da177e4SLinus Torvalds 	up(&adapter->erp_ready_sem);
1031348447e8SMartin Peschke 	zfcp_rec_dbf_event_thread(2, adapter, 1);
10321da177e4SLinus Torvalds 
10331da177e4SLinus Torvalds 	wait_event(adapter->erp_thread_wqh,
10341da177e4SLinus Torvalds 		   !atomic_test_mask(ZFCP_STATUS_ADAPTER_ERP_THREAD_UP,
10351da177e4SLinus Torvalds 				     &adapter->status));
10361da177e4SLinus Torvalds 
10371da177e4SLinus Torvalds 	atomic_clear_mask(ZFCP_STATUS_ADAPTER_ERP_THREAD_KILL,
10381da177e4SLinus Torvalds 			  &adapter->status);
10391da177e4SLinus Torvalds 
10401da177e4SLinus Torvalds 	debug_text_event(adapter->erp_dbf, 5, "a_thki_ok");
10411da177e4SLinus Torvalds 
10421da177e4SLinus Torvalds 	return retval;
10431da177e4SLinus Torvalds }
10441da177e4SLinus Torvalds 
10451da177e4SLinus Torvalds /*
10461da177e4SLinus Torvalds  * purpose:	is run as a kernel thread,
10471da177e4SLinus Torvalds  *		goes through list of error recovery actions of associated adapter
10481da177e4SLinus Torvalds  *		and delegates single action to execution
10491da177e4SLinus Torvalds  *
10501da177e4SLinus Torvalds  * returns:	0
10511da177e4SLinus Torvalds  */
10521da177e4SLinus Torvalds static int
10531da177e4SLinus Torvalds zfcp_erp_thread(void *data)
10541da177e4SLinus Torvalds {
10551da177e4SLinus Torvalds 	struct zfcp_adapter *adapter = (struct zfcp_adapter *) data;
10561da177e4SLinus Torvalds 	struct list_head *next;
10571da177e4SLinus Torvalds 	struct zfcp_erp_action *erp_action;
10581da177e4SLinus Torvalds 	unsigned long flags;
10591da177e4SLinus Torvalds 
10601da177e4SLinus Torvalds 	daemonize("zfcperp%s", zfcp_get_busid_by_adapter(adapter));
10611da177e4SLinus Torvalds 	/* Block all signals */
10621da177e4SLinus Torvalds 	siginitsetinv(&current->blocked, 0);
10631da177e4SLinus Torvalds 	atomic_set_mask(ZFCP_STATUS_ADAPTER_ERP_THREAD_UP, &adapter->status);
10641da177e4SLinus Torvalds 	debug_text_event(adapter->erp_dbf, 5, "a_th_run");
10651da177e4SLinus Torvalds 	wake_up(&adapter->erp_thread_wqh);
10661da177e4SLinus Torvalds 
10671da177e4SLinus Torvalds 	while (!atomic_test_mask(ZFCP_STATUS_ADAPTER_ERP_THREAD_KILL,
10681da177e4SLinus Torvalds 				 &adapter->status)) {
10691da177e4SLinus Torvalds 
10701da177e4SLinus Torvalds 		write_lock_irqsave(&adapter->erp_lock, flags);
107186e8dfc5SMartin Peschke 		next = adapter->erp_ready_head.next;
10721da177e4SLinus Torvalds 		write_unlock_irqrestore(&adapter->erp_lock, flags);
10731da177e4SLinus Torvalds 
10741da177e4SLinus Torvalds 		if (next != &adapter->erp_ready_head) {
10751da177e4SLinus Torvalds 			erp_action =
10761da177e4SLinus Torvalds 			    list_entry(next, struct zfcp_erp_action, list);
10771da177e4SLinus Torvalds 			/*
10781da177e4SLinus Torvalds 			 * process action (incl. [re]moving it
10791da177e4SLinus Torvalds 			 * from 'ready' queue)
10801da177e4SLinus Torvalds 			 */
10811da177e4SLinus Torvalds 			zfcp_erp_strategy(erp_action);
10821da177e4SLinus Torvalds 		}
10831da177e4SLinus Torvalds 
10841da177e4SLinus Torvalds 		/*
10851da177e4SLinus Torvalds 		 * sleep as long as there is nothing to do, i.e.
10861da177e4SLinus Torvalds 		 * no action in 'ready' queue to be processed and
10871da177e4SLinus Torvalds 		 * thread is not to be killed
10881da177e4SLinus Torvalds 		 */
1089348447e8SMartin Peschke 		zfcp_rec_dbf_event_thread(4, adapter, 1);
10901da177e4SLinus Torvalds 		down_interruptible(&adapter->erp_ready_sem);
1091348447e8SMartin Peschke 		zfcp_rec_dbf_event_thread(5, adapter, 1);
10921da177e4SLinus Torvalds 		debug_text_event(adapter->erp_dbf, 5, "a_th_woken");
10931da177e4SLinus Torvalds 	}
10941da177e4SLinus Torvalds 
10951da177e4SLinus Torvalds 	atomic_clear_mask(ZFCP_STATUS_ADAPTER_ERP_THREAD_UP, &adapter->status);
10961da177e4SLinus Torvalds 	debug_text_event(adapter->erp_dbf, 5, "a_th_stop");
10971da177e4SLinus Torvalds 	wake_up(&adapter->erp_thread_wqh);
10981da177e4SLinus Torvalds 
10991da177e4SLinus Torvalds 	return 0;
11001da177e4SLinus Torvalds }
11011da177e4SLinus Torvalds 
11021da177e4SLinus Torvalds /*
11031da177e4SLinus Torvalds  * function:
11041da177e4SLinus Torvalds  *
11051da177e4SLinus Torvalds  * purpose:	drives single error recovery action and schedules higher and
11061da177e4SLinus Torvalds  *		subordinate actions, if necessary
11071da177e4SLinus Torvalds  *
11081da177e4SLinus Torvalds  * returns:	ZFCP_ERP_CONTINUES	- action continues (asynchronously)
11091da177e4SLinus Torvalds  *		ZFCP_ERP_SUCCEEDED	- action finished successfully (deqd)
11101da177e4SLinus Torvalds  *		ZFCP_ERP_FAILED		- action finished unsuccessfully (deqd)
11111da177e4SLinus Torvalds  *		ZFCP_ERP_EXIT		- action finished (dequeued), offline
11121da177e4SLinus Torvalds  *		ZFCP_ERP_DISMISSED	- action canceled (dequeued)
11131da177e4SLinus Torvalds  */
11141da177e4SLinus Torvalds static int
11151da177e4SLinus Torvalds zfcp_erp_strategy(struct zfcp_erp_action *erp_action)
11161da177e4SLinus Torvalds {
11171da177e4SLinus Torvalds 	int retval = 0;
11181da177e4SLinus Torvalds 	struct zfcp_adapter *adapter = erp_action->adapter;
11191da177e4SLinus Torvalds 	struct zfcp_port *port = erp_action->port;
11201da177e4SLinus Torvalds 	struct zfcp_unit *unit = erp_action->unit;
11211da177e4SLinus Torvalds 	int action = erp_action->action;
11221da177e4SLinus Torvalds 	u32 status = erp_action->status;
11231da177e4SLinus Torvalds 	unsigned long flags;
11241da177e4SLinus Torvalds 
11251da177e4SLinus Torvalds 	/* serialise dismissing, timing out, moving, enqueueing */
11261da177e4SLinus Torvalds 	read_lock_irqsave(&zfcp_data.config_lock, flags);
11271da177e4SLinus Torvalds 	write_lock(&adapter->erp_lock);
11281da177e4SLinus Torvalds 
11291da177e4SLinus Torvalds 	/* dequeue dismissed action and leave, if required */
11301da177e4SLinus Torvalds 	retval = zfcp_erp_strategy_check_action(erp_action, retval);
11311da177e4SLinus Torvalds 	if (retval == ZFCP_ERP_DISMISSED) {
11321da177e4SLinus Torvalds 		debug_text_event(adapter->erp_dbf, 4, "a_st_dis1");
11331da177e4SLinus Torvalds 		goto unlock;
11341da177e4SLinus Torvalds 	}
11351da177e4SLinus Torvalds 
11361da177e4SLinus Torvalds 	/*
11371da177e4SLinus Torvalds 	 * move action to 'running' queue before processing it
11381da177e4SLinus Torvalds 	 * (to avoid a race condition regarding moving the
11391da177e4SLinus Torvalds 	 * action to the 'running' queue and back)
11401da177e4SLinus Torvalds 	 */
11411da177e4SLinus Torvalds 	zfcp_erp_action_to_running(erp_action);
11421da177e4SLinus Torvalds 
11431da177e4SLinus Torvalds 	/*
11441da177e4SLinus Torvalds 	 * try to process action as far as possible,
11451da177e4SLinus Torvalds 	 * no lock to allow for blocking operations (kmalloc, qdio, ...),
11461da177e4SLinus Torvalds 	 * afterwards the lock is required again for the following reasons:
11471da177e4SLinus Torvalds 	 * - dequeueing of finished action and enqueueing of
11481da177e4SLinus Torvalds 	 *   follow-up actions must be atomic so that any other
11491da177e4SLinus Torvalds 	 *   reopen-routine does not believe there is nothing to do
11501da177e4SLinus Torvalds 	 *   and that it is safe to enqueue something else,
11511da177e4SLinus Torvalds 	 * - we want to force any control thread which is dismissing
11521da177e4SLinus Torvalds 	 *   actions to finish this before we decide about
11531da177e4SLinus Torvalds 	 *   necessary steps to be taken here further
11541da177e4SLinus Torvalds 	 */
11551da177e4SLinus Torvalds 	write_unlock(&adapter->erp_lock);
11561da177e4SLinus Torvalds 	read_unlock_irqrestore(&zfcp_data.config_lock, flags);
11571da177e4SLinus Torvalds 	retval = zfcp_erp_strategy_do_action(erp_action);
11581da177e4SLinus Torvalds 	read_lock_irqsave(&zfcp_data.config_lock, flags);
11591da177e4SLinus Torvalds 	write_lock(&adapter->erp_lock);
11601da177e4SLinus Torvalds 
11611da177e4SLinus Torvalds 	/*
11621da177e4SLinus Torvalds 	 * check for dismissed status again to avoid follow-up actions,
116386e8dfc5SMartin Peschke 	 * failing of targets and so on for dismissed actions,
116486e8dfc5SMartin Peschke 	 * we go through down() here because there has been an up()
11651da177e4SLinus Torvalds 	 */
116686e8dfc5SMartin Peschke 	if (erp_action->status & ZFCP_STATUS_ERP_DISMISSED)
116786e8dfc5SMartin Peschke 		retval = ZFCP_ERP_CONTINUES;
11681da177e4SLinus Torvalds 
11691da177e4SLinus Torvalds 	switch (retval) {
11701da177e4SLinus Torvalds 	case ZFCP_ERP_NOMEM:
11711da177e4SLinus Torvalds 		/* no memory to continue immediately, let it sleep */
11721da177e4SLinus Torvalds 		if (!(erp_action->status & ZFCP_STATUS_ERP_LOWMEM)) {
11731da177e4SLinus Torvalds 			++adapter->erp_low_mem_count;
11741da177e4SLinus Torvalds 			erp_action->status |= ZFCP_STATUS_ERP_LOWMEM;
11751da177e4SLinus Torvalds 		}
11761da177e4SLinus Torvalds 		/* This condition is true if there is no memory available
11771da177e4SLinus Torvalds 		   for any erp_action on this adapter. This implies that there
11781da177e4SLinus Torvalds 		   are no elements in the memory pool(s) left for erp_actions.
11791da177e4SLinus Torvalds 		   This might happen if an erp_action that used a memory pool
11801da177e4SLinus Torvalds 		   element was timed out.
11811da177e4SLinus Torvalds 		 */
11821da177e4SLinus Torvalds 		if (adapter->erp_total_count == adapter->erp_low_mem_count) {
11831da177e4SLinus Torvalds 			debug_text_event(adapter->erp_dbf, 3, "a_st_lowmem");
11841da177e4SLinus Torvalds 			ZFCP_LOG_NORMAL("error: no mempool elements available, "
11851da177e4SLinus Torvalds 					"restarting I/O on adapter %s "
11861da177e4SLinus Torvalds 					"to free mempool\n",
11871da177e4SLinus Torvalds 					zfcp_get_busid_by_adapter(adapter));
11881da177e4SLinus Torvalds 			zfcp_erp_adapter_reopen_internal(adapter, 0);
11891da177e4SLinus Torvalds 		} else {
11901da177e4SLinus Torvalds 		debug_text_event(adapter->erp_dbf, 2, "a_st_memw");
11911da177e4SLinus Torvalds 		retval = zfcp_erp_strategy_memwait(erp_action);
11921da177e4SLinus Torvalds 		}
11931da177e4SLinus Torvalds 		goto unlock;
11941da177e4SLinus Torvalds 	case ZFCP_ERP_CONTINUES:
11951da177e4SLinus Torvalds 		/* leave since this action runs asynchronously */
11961da177e4SLinus Torvalds 		debug_text_event(adapter->erp_dbf, 6, "a_st_cont");
11971da177e4SLinus Torvalds 		if (erp_action->status & ZFCP_STATUS_ERP_LOWMEM) {
11981da177e4SLinus Torvalds 			--adapter->erp_low_mem_count;
11991da177e4SLinus Torvalds 			erp_action->status &= ~ZFCP_STATUS_ERP_LOWMEM;
12001da177e4SLinus Torvalds 		}
12011da177e4SLinus Torvalds 		goto unlock;
12021da177e4SLinus Torvalds 	}
12031da177e4SLinus Torvalds 	/* ok, finished action (whatever its result is) */
12041da177e4SLinus Torvalds 
12051da177e4SLinus Torvalds 	/* check for unrecoverable targets */
12061da177e4SLinus Torvalds 	retval = zfcp_erp_strategy_check_target(erp_action, retval);
12071da177e4SLinus Torvalds 
12081da177e4SLinus Torvalds 	/* action must be dequeued (here to allow for further ones) */
12091da177e4SLinus Torvalds 	zfcp_erp_action_dequeue(erp_action);
12101da177e4SLinus Torvalds 
12111da177e4SLinus Torvalds 	/*
12121da177e4SLinus Torvalds 	 * put this target through the erp mill again if someone has
12131da177e4SLinus Torvalds 	 * requested to change the status of a target being online
12141da177e4SLinus Torvalds 	 * to offline or the other way around
12151da177e4SLinus Torvalds 	 * (old retval is preserved if nothing has to be done here)
12161da177e4SLinus Torvalds 	 */
12171da177e4SLinus Torvalds 	retval = zfcp_erp_strategy_statechange(action, status, adapter,
12181da177e4SLinus Torvalds 					       port, unit, retval);
12191da177e4SLinus Torvalds 
12201da177e4SLinus Torvalds 	/*
12211da177e4SLinus Torvalds 	 * leave if target is in permanent error state or if
12221da177e4SLinus Torvalds 	 * action is repeated in order to process state change
12231da177e4SLinus Torvalds 	 */
12241da177e4SLinus Torvalds 	if (retval == ZFCP_ERP_EXIT) {
12251da177e4SLinus Torvalds 		debug_text_event(adapter->erp_dbf, 2, "a_st_exit");
12261da177e4SLinus Torvalds 		goto unlock;
12271da177e4SLinus Torvalds 	}
12281da177e4SLinus Torvalds 
12291da177e4SLinus Torvalds 	/* trigger follow up actions */
12301da177e4SLinus Torvalds 	zfcp_erp_strategy_followup_actions(action, adapter, port, unit, retval);
12311da177e4SLinus Torvalds 
12321da177e4SLinus Torvalds  unlock:
12331da177e4SLinus Torvalds 	write_unlock(&adapter->erp_lock);
12341da177e4SLinus Torvalds 	read_unlock_irqrestore(&zfcp_data.config_lock, flags);
12351da177e4SLinus Torvalds 
12361da177e4SLinus Torvalds 	if (retval != ZFCP_ERP_CONTINUES)
12371da177e4SLinus Torvalds 		zfcp_erp_action_cleanup(action, adapter, port, unit, retval);
12381da177e4SLinus Torvalds 
12391da177e4SLinus Torvalds 	/*
12401da177e4SLinus Torvalds 	 * a few tasks remain when the erp queues are empty
12411da177e4SLinus Torvalds 	 * (don't do that if the last action evaluated was dismissed
12421da177e4SLinus Torvalds 	 * since this clearly indicates that there is more to come) :
12431da177e4SLinus Torvalds 	 * - close the name server port if it is open yet
12441da177e4SLinus Torvalds 	 *   (enqueues another [probably] final action)
12451da177e4SLinus Torvalds 	 * - otherwise, wake up whoever wants to be woken when we are
12461da177e4SLinus Torvalds 	 *   done with erp
12471da177e4SLinus Torvalds 	 */
12481da177e4SLinus Torvalds 	if (retval != ZFCP_ERP_DISMISSED)
12491da177e4SLinus Torvalds 		zfcp_erp_strategy_check_queues(adapter);
12501da177e4SLinus Torvalds 
12511da177e4SLinus Torvalds 	debug_text_event(adapter->erp_dbf, 6, "a_st_done");
12521da177e4SLinus Torvalds 
12531da177e4SLinus Torvalds 	return retval;
12541da177e4SLinus Torvalds }
12551da177e4SLinus Torvalds 
12561da177e4SLinus Torvalds /*
12571da177e4SLinus Torvalds  * function:
12581da177e4SLinus Torvalds  *
12591da177e4SLinus Torvalds  * purpose:
12601da177e4SLinus Torvalds  *
12611da177e4SLinus Torvalds  * returns:	ZFCP_ERP_DISMISSED	- if action has been dismissed
12621da177e4SLinus Torvalds  *		retval			- otherwise
12631da177e4SLinus Torvalds  */
12641da177e4SLinus Torvalds static int
12651da177e4SLinus Torvalds zfcp_erp_strategy_check_action(struct zfcp_erp_action *erp_action, int retval)
12661da177e4SLinus Torvalds {
12671da177e4SLinus Torvalds 	struct zfcp_adapter *adapter = erp_action->adapter;
12681da177e4SLinus Torvalds 
12691da177e4SLinus Torvalds 	zfcp_erp_strategy_check_fsfreq(erp_action);
12701da177e4SLinus Torvalds 
12711da177e4SLinus Torvalds 	debug_event(adapter->erp_dbf, 5, &erp_action->action, sizeof (int));
12721da177e4SLinus Torvalds 	if (erp_action->status & ZFCP_STATUS_ERP_DISMISSED) {
12731da177e4SLinus Torvalds 		debug_text_event(adapter->erp_dbf, 3, "a_stcd_dis");
12741da177e4SLinus Torvalds 		zfcp_erp_action_dequeue(erp_action);
12751da177e4SLinus Torvalds 		retval = ZFCP_ERP_DISMISSED;
12761da177e4SLinus Torvalds 	} else
12771da177e4SLinus Torvalds 		debug_text_event(adapter->erp_dbf, 5, "a_stcd_nodis");
12781da177e4SLinus Torvalds 
12791da177e4SLinus Torvalds 	return retval;
12801da177e4SLinus Torvalds }
12811da177e4SLinus Torvalds 
12821da177e4SLinus Torvalds static int
12831da177e4SLinus Torvalds zfcp_erp_strategy_do_action(struct zfcp_erp_action *erp_action)
12841da177e4SLinus Torvalds {
12851da177e4SLinus Torvalds 	int retval = ZFCP_ERP_FAILED;
12861da177e4SLinus Torvalds 	struct zfcp_adapter *adapter = erp_action->adapter;
12871da177e4SLinus Torvalds 
12881da177e4SLinus Torvalds 	/*
12891da177e4SLinus Torvalds 	 * try to execute/continue action as far as possible,
12901da177e4SLinus Torvalds 	 * note: no lock in subsequent strategy routines
12911da177e4SLinus Torvalds 	 * (this allows these routine to call schedule, e.g.
12921da177e4SLinus Torvalds 	 * kmalloc with such flags or qdio_initialize & friends)
12935d67d164SJoe Perches 	 * Note: in case of timeout, the separate strategies will fail
12941da177e4SLinus Torvalds 	 * anyhow. No need for a special action. Even worse, a nameserver
12951da177e4SLinus Torvalds 	 * failure would not wake up waiting ports without the call.
12961da177e4SLinus Torvalds 	 */
12971da177e4SLinus Torvalds 	switch (erp_action->action) {
12981da177e4SLinus Torvalds 
12991da177e4SLinus Torvalds 	case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
13001da177e4SLinus Torvalds 		retval = zfcp_erp_adapter_strategy(erp_action);
13011da177e4SLinus Torvalds 		break;
13021da177e4SLinus Torvalds 
13031da177e4SLinus Torvalds 	case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
13041da177e4SLinus Torvalds 		retval = zfcp_erp_port_forced_strategy(erp_action);
13051da177e4SLinus Torvalds 		break;
13061da177e4SLinus Torvalds 
13071da177e4SLinus Torvalds 	case ZFCP_ERP_ACTION_REOPEN_PORT:
13081da177e4SLinus Torvalds 		retval = zfcp_erp_port_strategy(erp_action);
13091da177e4SLinus Torvalds 		break;
13101da177e4SLinus Torvalds 
13111da177e4SLinus Torvalds 	case ZFCP_ERP_ACTION_REOPEN_UNIT:
13121da177e4SLinus Torvalds 		retval = zfcp_erp_unit_strategy(erp_action);
13131da177e4SLinus Torvalds 		break;
13141da177e4SLinus Torvalds 
13151da177e4SLinus Torvalds 	default:
13161da177e4SLinus Torvalds 		debug_text_exception(adapter->erp_dbf, 1, "a_stda_bug");
13171da177e4SLinus Torvalds 		debug_event(adapter->erp_dbf, 1, &erp_action->action,
13181da177e4SLinus Torvalds 			    sizeof (int));
13191da177e4SLinus Torvalds 		ZFCP_LOG_NORMAL("bug: unknown erp action requested on "
13201da177e4SLinus Torvalds 				"adapter %s (action=%d)\n",
13211da177e4SLinus Torvalds 				zfcp_get_busid_by_adapter(erp_action->adapter),
13221da177e4SLinus Torvalds 				erp_action->action);
13231da177e4SLinus Torvalds 	}
13241da177e4SLinus Torvalds 
13251da177e4SLinus Torvalds 	return retval;
13261da177e4SLinus Torvalds }
13271da177e4SLinus Torvalds 
13281da177e4SLinus Torvalds /*
13291da177e4SLinus Torvalds  * function:
13301da177e4SLinus Torvalds  *
13311da177e4SLinus Torvalds  * purpose:	triggers retry of this action after a certain amount of time
13321da177e4SLinus Torvalds  *		by means of timer provided by erp_action
13331da177e4SLinus Torvalds  *
13341da177e4SLinus Torvalds  * returns:	ZFCP_ERP_CONTINUES - erp_action sleeps in erp running queue
13351da177e4SLinus Torvalds  */
13361da177e4SLinus Torvalds static int
13371da177e4SLinus Torvalds zfcp_erp_strategy_memwait(struct zfcp_erp_action *erp_action)
13381da177e4SLinus Torvalds {
13391da177e4SLinus Torvalds 	int retval = ZFCP_ERP_CONTINUES;
13401da177e4SLinus Torvalds 	struct zfcp_adapter *adapter = erp_action->adapter;
13411da177e4SLinus Torvalds 
13421da177e4SLinus Torvalds 	debug_text_event(adapter->erp_dbf, 6, "a_mwinit");
13431da177e4SLinus Torvalds 	debug_event(adapter->erp_dbf, 6, &erp_action->action, sizeof (int));
13441da177e4SLinus Torvalds 	init_timer(&erp_action->timer);
13451da177e4SLinus Torvalds 	erp_action->timer.function = zfcp_erp_memwait_handler;
13461da177e4SLinus Torvalds 	erp_action->timer.data = (unsigned long) erp_action;
13471da177e4SLinus Torvalds 	erp_action->timer.expires = jiffies + ZFCP_ERP_MEMWAIT_TIMEOUT;
13481da177e4SLinus Torvalds 	add_timer(&erp_action->timer);
13491da177e4SLinus Torvalds 
13501da177e4SLinus Torvalds 	return retval;
13511da177e4SLinus Torvalds }
13521da177e4SLinus Torvalds 
13531da177e4SLinus Torvalds /*
13541da177e4SLinus Torvalds  * function:    zfcp_erp_adapter_failed
13551da177e4SLinus Torvalds  *
13561da177e4SLinus Torvalds  * purpose:     sets the adapter and all underlying devices to ERP_FAILED
13571da177e4SLinus Torvalds  *
13581da177e4SLinus Torvalds  */
13591da177e4SLinus Torvalds void
13601da177e4SLinus Torvalds zfcp_erp_adapter_failed(struct zfcp_adapter *adapter)
13611da177e4SLinus Torvalds {
13621da177e4SLinus Torvalds 	zfcp_erp_modify_adapter_status(adapter,
13631da177e4SLinus Torvalds 				       ZFCP_STATUS_COMMON_ERP_FAILED, ZFCP_SET);
13641da177e4SLinus Torvalds 	ZFCP_LOG_NORMAL("adapter erp failed on adapter %s\n",
13651da177e4SLinus Torvalds 			zfcp_get_busid_by_adapter(adapter));
13661da177e4SLinus Torvalds 	debug_text_event(adapter->erp_dbf, 2, "a_afail");
13671da177e4SLinus Torvalds }
13681da177e4SLinus Torvalds 
13691da177e4SLinus Torvalds /*
13701da177e4SLinus Torvalds  * function:    zfcp_erp_port_failed
13711da177e4SLinus Torvalds  *
13721da177e4SLinus Torvalds  * purpose:     sets the port and all underlying devices to ERP_FAILED
13731da177e4SLinus Torvalds  *
13741da177e4SLinus Torvalds  */
13751da177e4SLinus Torvalds void
13761da177e4SLinus Torvalds zfcp_erp_port_failed(struct zfcp_port *port)
13771da177e4SLinus Torvalds {
13781da177e4SLinus Torvalds 	zfcp_erp_modify_port_status(port,
13791da177e4SLinus Torvalds 				    ZFCP_STATUS_COMMON_ERP_FAILED, ZFCP_SET);
13801da177e4SLinus Torvalds 
13811da177e4SLinus Torvalds 	if (atomic_test_mask(ZFCP_STATUS_PORT_WKA, &port->status))
13821da177e4SLinus Torvalds 		ZFCP_LOG_NORMAL("port erp failed (adapter %s, "
13831d589edfSChristof Schmitt 				"port d_id=0x%06x)\n",
13841da177e4SLinus Torvalds 				zfcp_get_busid_by_port(port), port->d_id);
13851da177e4SLinus Torvalds 	else
13861da177e4SLinus Torvalds 		ZFCP_LOG_NORMAL("port erp failed (adapter %s, wwpn=0x%016Lx)\n",
13871da177e4SLinus Torvalds 				zfcp_get_busid_by_port(port), port->wwpn);
13881da177e4SLinus Torvalds 
13891da177e4SLinus Torvalds 	debug_text_event(port->adapter->erp_dbf, 2, "p_pfail");
13901da177e4SLinus Torvalds 	debug_event(port->adapter->erp_dbf, 2, &port->wwpn, sizeof (wwn_t));
13911da177e4SLinus Torvalds }
13921da177e4SLinus Torvalds 
13931da177e4SLinus Torvalds /*
13941da177e4SLinus Torvalds  * function:    zfcp_erp_unit_failed
13951da177e4SLinus Torvalds  *
13961da177e4SLinus Torvalds  * purpose:     sets the unit to ERP_FAILED
13971da177e4SLinus Torvalds  *
13981da177e4SLinus Torvalds  */
13991da177e4SLinus Torvalds void
14001da177e4SLinus Torvalds zfcp_erp_unit_failed(struct zfcp_unit *unit)
14011da177e4SLinus Torvalds {
14021da177e4SLinus Torvalds 	zfcp_erp_modify_unit_status(unit,
14031da177e4SLinus Torvalds 				    ZFCP_STATUS_COMMON_ERP_FAILED, ZFCP_SET);
14041da177e4SLinus Torvalds 
14051da177e4SLinus Torvalds 	ZFCP_LOG_NORMAL("unit erp failed on unit 0x%016Lx on port 0x%016Lx "
14061da177e4SLinus Torvalds 			" on adapter %s\n", unit->fcp_lun,
14071da177e4SLinus Torvalds 			unit->port->wwpn, zfcp_get_busid_by_unit(unit));
14081da177e4SLinus Torvalds 	debug_text_event(unit->port->adapter->erp_dbf, 2, "u_ufail");
14091da177e4SLinus Torvalds 	debug_event(unit->port->adapter->erp_dbf, 2,
14101da177e4SLinus Torvalds 		    &unit->fcp_lun, sizeof (fcp_lun_t));
14111da177e4SLinus Torvalds }
14121da177e4SLinus Torvalds 
14131da177e4SLinus Torvalds /*
14141da177e4SLinus Torvalds  * function:	zfcp_erp_strategy_check_target
14151da177e4SLinus Torvalds  *
14161da177e4SLinus Torvalds  * purpose:	increments the erp action count on the device currently in
14171da177e4SLinus Torvalds  *              recovery if the action failed or resets the count in case of
14181da177e4SLinus Torvalds  *              success. If a maximum count is exceeded the device is marked
14191da177e4SLinus Torvalds  *              as ERP_FAILED.
14201da177e4SLinus Torvalds  *		The 'blocked' state of a target which has been recovered
14211da177e4SLinus Torvalds  *              successfully is reset.
14221da177e4SLinus Torvalds  *
14231da177e4SLinus Torvalds  * returns:	ZFCP_ERP_CONTINUES	- action continues (not considered)
14241da177e4SLinus Torvalds  *		ZFCP_ERP_SUCCEEDED	- action finished successfully
14251da177e4SLinus Torvalds  *		ZFCP_ERP_EXIT		- action failed and will not continue
14261da177e4SLinus Torvalds  */
14271da177e4SLinus Torvalds static int
14281da177e4SLinus Torvalds zfcp_erp_strategy_check_target(struct zfcp_erp_action *erp_action, int result)
14291da177e4SLinus Torvalds {
14301da177e4SLinus Torvalds 	struct zfcp_adapter *adapter = erp_action->adapter;
14311da177e4SLinus Torvalds 	struct zfcp_port *port = erp_action->port;
14321da177e4SLinus Torvalds 	struct zfcp_unit *unit = erp_action->unit;
14331da177e4SLinus Torvalds 
14341da177e4SLinus Torvalds 	debug_text_event(adapter->erp_dbf, 5, "a_stct_norm");
14351da177e4SLinus Torvalds 	debug_event(adapter->erp_dbf, 5, &erp_action->action, sizeof (int));
14361da177e4SLinus Torvalds 	debug_event(adapter->erp_dbf, 5, &result, sizeof (int));
14371da177e4SLinus Torvalds 
14381da177e4SLinus Torvalds 	switch (erp_action->action) {
14391da177e4SLinus Torvalds 
14401da177e4SLinus Torvalds 	case ZFCP_ERP_ACTION_REOPEN_UNIT:
14411da177e4SLinus Torvalds 		result = zfcp_erp_strategy_check_unit(unit, result);
14421da177e4SLinus Torvalds 		break;
14431da177e4SLinus Torvalds 
14441da177e4SLinus Torvalds 	case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
14451da177e4SLinus Torvalds 	case ZFCP_ERP_ACTION_REOPEN_PORT:
14461da177e4SLinus Torvalds 		result = zfcp_erp_strategy_check_port(port, result);
14471da177e4SLinus Torvalds 		break;
14481da177e4SLinus Torvalds 
14491da177e4SLinus Torvalds 	case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
14501da177e4SLinus Torvalds 		result = zfcp_erp_strategy_check_adapter(adapter, result);
14511da177e4SLinus Torvalds 		break;
14521da177e4SLinus Torvalds 	}
14531da177e4SLinus Torvalds 
14541da177e4SLinus Torvalds 	return result;
14551da177e4SLinus Torvalds }
14561da177e4SLinus Torvalds 
14571da177e4SLinus Torvalds static int
14581da177e4SLinus Torvalds zfcp_erp_strategy_statechange(int action,
14591da177e4SLinus Torvalds 			      u32 status,
14601da177e4SLinus Torvalds 			      struct zfcp_adapter *adapter,
14611da177e4SLinus Torvalds 			      struct zfcp_port *port,
14621da177e4SLinus Torvalds 			      struct zfcp_unit *unit, int retval)
14631da177e4SLinus Torvalds {
14641da177e4SLinus Torvalds 	debug_text_event(adapter->erp_dbf, 3, "a_stsc");
14651da177e4SLinus Torvalds 	debug_event(adapter->erp_dbf, 3, &action, sizeof (int));
14661da177e4SLinus Torvalds 
14671da177e4SLinus Torvalds 	switch (action) {
14681da177e4SLinus Torvalds 
14691da177e4SLinus Torvalds 	case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
14701da177e4SLinus Torvalds 		if (zfcp_erp_strategy_statechange_detected(&adapter->status,
14711da177e4SLinus Torvalds 							   status)) {
14721da177e4SLinus Torvalds 			zfcp_erp_adapter_reopen_internal(adapter, ZFCP_STATUS_COMMON_ERP_FAILED);
14731da177e4SLinus Torvalds 			retval = ZFCP_ERP_EXIT;
14741da177e4SLinus Torvalds 		}
14751da177e4SLinus Torvalds 		break;
14761da177e4SLinus Torvalds 
14771da177e4SLinus Torvalds 	case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
14781da177e4SLinus Torvalds 	case ZFCP_ERP_ACTION_REOPEN_PORT:
14791da177e4SLinus Torvalds 		if (zfcp_erp_strategy_statechange_detected(&port->status,
14801da177e4SLinus Torvalds 							   status)) {
14811da177e4SLinus Torvalds 			zfcp_erp_port_reopen_internal(port, ZFCP_STATUS_COMMON_ERP_FAILED);
14821da177e4SLinus Torvalds 			retval = ZFCP_ERP_EXIT;
14831da177e4SLinus Torvalds 		}
14841da177e4SLinus Torvalds 		break;
14851da177e4SLinus Torvalds 
14861da177e4SLinus Torvalds 	case ZFCP_ERP_ACTION_REOPEN_UNIT:
14871da177e4SLinus Torvalds 		if (zfcp_erp_strategy_statechange_detected(&unit->status,
14881da177e4SLinus Torvalds 							   status)) {
14891da177e4SLinus Torvalds 			zfcp_erp_unit_reopen_internal(unit, ZFCP_STATUS_COMMON_ERP_FAILED);
14901da177e4SLinus Torvalds 			retval = ZFCP_ERP_EXIT;
14911da177e4SLinus Torvalds 		}
14921da177e4SLinus Torvalds 		break;
14931da177e4SLinus Torvalds 	}
14941da177e4SLinus Torvalds 
14951da177e4SLinus Torvalds 	return retval;
14961da177e4SLinus Torvalds }
14971da177e4SLinus Torvalds 
1498364c8558SHeiko Carstens static int
14991da177e4SLinus Torvalds zfcp_erp_strategy_statechange_detected(atomic_t * target_status, u32 erp_status)
15001da177e4SLinus Torvalds {
15011da177e4SLinus Torvalds 	return
15021da177e4SLinus Torvalds 	    /* take it online */
15031da177e4SLinus Torvalds 	    (atomic_test_mask(ZFCP_STATUS_COMMON_RUNNING, target_status) &&
15041da177e4SLinus Torvalds 	     (ZFCP_STATUS_ERP_CLOSE_ONLY & erp_status)) ||
15051da177e4SLinus Torvalds 	    /* take it offline */
15061da177e4SLinus Torvalds 	    (!atomic_test_mask(ZFCP_STATUS_COMMON_RUNNING, target_status) &&
15071da177e4SLinus Torvalds 	     !(ZFCP_STATUS_ERP_CLOSE_ONLY & erp_status));
15081da177e4SLinus Torvalds }
15091da177e4SLinus Torvalds 
15101da177e4SLinus Torvalds static int
15111da177e4SLinus Torvalds zfcp_erp_strategy_check_unit(struct zfcp_unit *unit, int result)
15121da177e4SLinus Torvalds {
15131da177e4SLinus Torvalds 	debug_text_event(unit->port->adapter->erp_dbf, 5, "u_stct");
15141da177e4SLinus Torvalds 	debug_event(unit->port->adapter->erp_dbf, 5, &unit->fcp_lun,
15151da177e4SLinus Torvalds 		    sizeof (fcp_lun_t));
15161da177e4SLinus Torvalds 
15171da177e4SLinus Torvalds 	switch (result) {
15181da177e4SLinus Torvalds 	case ZFCP_ERP_SUCCEEDED :
15191da177e4SLinus Torvalds 		atomic_set(&unit->erp_counter, 0);
15201da177e4SLinus Torvalds 		zfcp_erp_unit_unblock(unit);
15211da177e4SLinus Torvalds 		break;
15221da177e4SLinus Torvalds 	case ZFCP_ERP_FAILED :
15231da177e4SLinus Torvalds 		atomic_inc(&unit->erp_counter);
15241da177e4SLinus Torvalds 		if (atomic_read(&unit->erp_counter) > ZFCP_MAX_ERPS)
15251da177e4SLinus Torvalds 			zfcp_erp_unit_failed(unit);
15261da177e4SLinus Torvalds 		break;
15271da177e4SLinus Torvalds 	case ZFCP_ERP_EXIT :
15281da177e4SLinus Torvalds 		/* nothing */
15291da177e4SLinus Torvalds 		break;
15301da177e4SLinus Torvalds 	}
15311da177e4SLinus Torvalds 
15321da177e4SLinus Torvalds 	if (atomic_test_mask(ZFCP_STATUS_COMMON_ERP_FAILED, &unit->status)) {
15331da177e4SLinus Torvalds 		zfcp_erp_unit_block(unit, 0); /* for ZFCP_ERP_SUCCEEDED */
15341da177e4SLinus Torvalds 		result = ZFCP_ERP_EXIT;
15351da177e4SLinus Torvalds 	}
15361da177e4SLinus Torvalds 
15371da177e4SLinus Torvalds 	return result;
15381da177e4SLinus Torvalds }
15391da177e4SLinus Torvalds 
15401da177e4SLinus Torvalds static int
15411da177e4SLinus Torvalds zfcp_erp_strategy_check_port(struct zfcp_port *port, int result)
15421da177e4SLinus Torvalds {
15431da177e4SLinus Torvalds 	debug_text_event(port->adapter->erp_dbf, 5, "p_stct");
15441da177e4SLinus Torvalds 	debug_event(port->adapter->erp_dbf, 5, &port->wwpn, sizeof (wwn_t));
15451da177e4SLinus Torvalds 
15461da177e4SLinus Torvalds 	switch (result) {
15471da177e4SLinus Torvalds 	case ZFCP_ERP_SUCCEEDED :
15481da177e4SLinus Torvalds 		atomic_set(&port->erp_counter, 0);
15491da177e4SLinus Torvalds 		zfcp_erp_port_unblock(port);
15501da177e4SLinus Torvalds 		break;
15511da177e4SLinus Torvalds 	case ZFCP_ERP_FAILED :
15521da177e4SLinus Torvalds 		atomic_inc(&port->erp_counter);
15531da177e4SLinus Torvalds 		if (atomic_read(&port->erp_counter) > ZFCP_MAX_ERPS)
15541da177e4SLinus Torvalds 			zfcp_erp_port_failed(port);
15551da177e4SLinus Torvalds 		break;
15561da177e4SLinus Torvalds 	case ZFCP_ERP_EXIT :
15571da177e4SLinus Torvalds 		/* nothing */
15581da177e4SLinus Torvalds 		break;
15591da177e4SLinus Torvalds 	}
15601da177e4SLinus Torvalds 
15611da177e4SLinus Torvalds 	if (atomic_test_mask(ZFCP_STATUS_COMMON_ERP_FAILED, &port->status)) {
15621da177e4SLinus Torvalds 		zfcp_erp_port_block(port, 0); /* for ZFCP_ERP_SUCCEEDED */
15631da177e4SLinus Torvalds 		result = ZFCP_ERP_EXIT;
15641da177e4SLinus Torvalds 	}
15651da177e4SLinus Torvalds 
15661da177e4SLinus Torvalds 	return result;
15671da177e4SLinus Torvalds }
15681da177e4SLinus Torvalds 
15691da177e4SLinus Torvalds static int
15701da177e4SLinus Torvalds zfcp_erp_strategy_check_adapter(struct zfcp_adapter *adapter, int result)
15711da177e4SLinus Torvalds {
15721da177e4SLinus Torvalds 	debug_text_event(adapter->erp_dbf, 5, "a_stct");
15731da177e4SLinus Torvalds 
15741da177e4SLinus Torvalds 	switch (result) {
15751da177e4SLinus Torvalds 	case ZFCP_ERP_SUCCEEDED :
15761da177e4SLinus Torvalds 		atomic_set(&adapter->erp_counter, 0);
15771da177e4SLinus Torvalds 		zfcp_erp_adapter_unblock(adapter);
15781da177e4SLinus Torvalds 		break;
15791da177e4SLinus Torvalds 	case ZFCP_ERP_FAILED :
15801da177e4SLinus Torvalds 		atomic_inc(&adapter->erp_counter);
15811da177e4SLinus Torvalds 		if (atomic_read(&adapter->erp_counter) > ZFCP_MAX_ERPS)
15821da177e4SLinus Torvalds 			zfcp_erp_adapter_failed(adapter);
15831da177e4SLinus Torvalds 		break;
15841da177e4SLinus Torvalds 	case ZFCP_ERP_EXIT :
15851da177e4SLinus Torvalds 		/* nothing */
15861da177e4SLinus Torvalds 		break;
15871da177e4SLinus Torvalds 	}
15881da177e4SLinus Torvalds 
15891da177e4SLinus Torvalds 	if (atomic_test_mask(ZFCP_STATUS_COMMON_ERP_FAILED, &adapter->status)) {
15901da177e4SLinus Torvalds 		zfcp_erp_adapter_block(adapter, 0); /* for ZFCP_ERP_SUCCEEDED */
15911da177e4SLinus Torvalds 		result = ZFCP_ERP_EXIT;
15921da177e4SLinus Torvalds 	}
15931da177e4SLinus Torvalds 
15941da177e4SLinus Torvalds 	return result;
15951da177e4SLinus Torvalds }
15961da177e4SLinus Torvalds 
15975f852be9SChristof Schmitt struct zfcp_erp_add_work {
15985f852be9SChristof Schmitt 	struct zfcp_unit  *unit;
15995f852be9SChristof Schmitt 	struct work_struct work;
16005f852be9SChristof Schmitt };
16015f852be9SChristof Schmitt 
16025f852be9SChristof Schmitt /**
16035f852be9SChristof Schmitt  * zfcp_erp_scsi_scan
16045f852be9SChristof Schmitt  * @data: pointer to a struct zfcp_erp_add_work
16055f852be9SChristof Schmitt  *
16065f852be9SChristof Schmitt  * Registers a logical unit with the SCSI stack.
16075f852be9SChristof Schmitt  */
16085f852be9SChristof Schmitt static void zfcp_erp_scsi_scan(struct work_struct *work)
16095f852be9SChristof Schmitt {
16105f852be9SChristof Schmitt 	struct zfcp_erp_add_work *p =
16115f852be9SChristof Schmitt 		container_of(work, struct zfcp_erp_add_work, work);
16125f852be9SChristof Schmitt 	struct zfcp_unit *unit = p->unit;
16135f852be9SChristof Schmitt 	struct fc_rport *rport = unit->port->rport;
16145f852be9SChristof Schmitt 	scsi_scan_target(&rport->dev, 0, rport->scsi_target_id,
16155f852be9SChristof Schmitt 			 unit->scsi_lun, 0);
16165f852be9SChristof Schmitt 	atomic_clear_mask(ZFCP_STATUS_UNIT_SCSI_WORK_PENDING, &unit->status);
16175f852be9SChristof Schmitt 	zfcp_unit_put(unit);
16185f852be9SChristof Schmitt 	kfree(p);
16195f852be9SChristof Schmitt }
16205f852be9SChristof Schmitt 
16215f852be9SChristof Schmitt /**
16225f852be9SChristof Schmitt  * zfcp_erp_schedule_work
16235f852be9SChristof Schmitt  * @unit: pointer to unit which should be registered with SCSI stack
16245f852be9SChristof Schmitt  *
16255f852be9SChristof Schmitt  * Schedules work which registers a unit with the SCSI stack
16265f852be9SChristof Schmitt  */
16275f852be9SChristof Schmitt static void
16285f852be9SChristof Schmitt zfcp_erp_schedule_work(struct zfcp_unit *unit)
16295f852be9SChristof Schmitt {
16305f852be9SChristof Schmitt 	struct zfcp_erp_add_work *p;
16315f852be9SChristof Schmitt 
16320d661327SSwen Schillig 	p = kzalloc(sizeof(*p), GFP_KERNEL);
16335f852be9SChristof Schmitt 	if (!p) {
16345f852be9SChristof Schmitt 		ZFCP_LOG_NORMAL("error: Out of resources. Could not register "
16355f852be9SChristof Schmitt 				"the FCP-LUN 0x%Lx connected to "
16365f852be9SChristof Schmitt 				"the port with WWPN 0x%Lx connected to "
16375f852be9SChristof Schmitt 				"the adapter %s with the SCSI stack.\n",
16385f852be9SChristof Schmitt 				unit->fcp_lun,
16395f852be9SChristof Schmitt 				unit->port->wwpn,
16405f852be9SChristof Schmitt 				zfcp_get_busid_by_unit(unit));
16415f852be9SChristof Schmitt 		return;
16425f852be9SChristof Schmitt 	}
16435f852be9SChristof Schmitt 
16445f852be9SChristof Schmitt 	zfcp_unit_get(unit);
16455f852be9SChristof Schmitt 	atomic_set_mask(ZFCP_STATUS_UNIT_SCSI_WORK_PENDING, &unit->status);
16465f852be9SChristof Schmitt 	INIT_WORK(&p->work, zfcp_erp_scsi_scan);
16475f852be9SChristof Schmitt 	p->unit = unit;
16485f852be9SChristof Schmitt 	schedule_work(&p->work);
16495f852be9SChristof Schmitt }
16505f852be9SChristof Schmitt 
16511da177e4SLinus Torvalds /*
16521da177e4SLinus Torvalds  * function:
16531da177e4SLinus Torvalds  *
16541da177e4SLinus Torvalds  * purpose:	remaining things in good cases,
16551da177e4SLinus Torvalds  *		escalation in bad cases
16561da177e4SLinus Torvalds  *
16571da177e4SLinus Torvalds  * returns:
16581da177e4SLinus Torvalds  */
16591da177e4SLinus Torvalds static int
16601da177e4SLinus Torvalds zfcp_erp_strategy_followup_actions(int action,
16611da177e4SLinus Torvalds 				   struct zfcp_adapter *adapter,
16621da177e4SLinus Torvalds 				   struct zfcp_port *port,
16631da177e4SLinus Torvalds 				   struct zfcp_unit *unit, int status)
16641da177e4SLinus Torvalds {
16651da177e4SLinus Torvalds 	debug_text_event(adapter->erp_dbf, 5, "a_stfol");
16661da177e4SLinus Torvalds 	debug_event(adapter->erp_dbf, 5, &action, sizeof (int));
16671da177e4SLinus Torvalds 
16681da177e4SLinus Torvalds 	/* initiate follow-up actions depending on success of finished action */
16691da177e4SLinus Torvalds 	switch (action) {
16701da177e4SLinus Torvalds 
16711da177e4SLinus Torvalds 	case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
16721da177e4SLinus Torvalds 		if (status == ZFCP_ERP_SUCCEEDED)
16731da177e4SLinus Torvalds 			zfcp_erp_port_reopen_all_internal(adapter, 0);
16741da177e4SLinus Torvalds 		else
16751da177e4SLinus Torvalds 			zfcp_erp_adapter_reopen_internal(adapter, 0);
16761da177e4SLinus Torvalds 		break;
16771da177e4SLinus Torvalds 
16781da177e4SLinus Torvalds 	case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
16791da177e4SLinus Torvalds 		if (status == ZFCP_ERP_SUCCEEDED)
16801da177e4SLinus Torvalds 			zfcp_erp_port_reopen_internal(port, 0);
16811da177e4SLinus Torvalds 		else
16821da177e4SLinus Torvalds 			zfcp_erp_adapter_reopen_internal(adapter, 0);
16831da177e4SLinus Torvalds 		break;
16841da177e4SLinus Torvalds 
16851da177e4SLinus Torvalds 	case ZFCP_ERP_ACTION_REOPEN_PORT:
16861da177e4SLinus Torvalds 		if (status == ZFCP_ERP_SUCCEEDED)
16871da177e4SLinus Torvalds 			zfcp_erp_unit_reopen_all_internal(port, 0);
16881da177e4SLinus Torvalds 		else
16891da177e4SLinus Torvalds 			zfcp_erp_port_forced_reopen_internal(port, 0);
16901da177e4SLinus Torvalds 		break;
16911da177e4SLinus Torvalds 
16921da177e4SLinus Torvalds 	case ZFCP_ERP_ACTION_REOPEN_UNIT:
1693cc16cebaSHeiko Carstens 		/* Nothing to do if status == ZFCP_ERP_SUCCEEDED */
1694cc16cebaSHeiko Carstens 		if (status != ZFCP_ERP_SUCCEEDED)
16951da177e4SLinus Torvalds 			zfcp_erp_port_reopen_internal(unit->port, 0);
16961da177e4SLinus Torvalds 		break;
16971da177e4SLinus Torvalds 	}
16981da177e4SLinus Torvalds 
16991da177e4SLinus Torvalds 	return 0;
17001da177e4SLinus Torvalds }
17011da177e4SLinus Torvalds 
17021da177e4SLinus Torvalds static int
17031da177e4SLinus Torvalds zfcp_erp_strategy_check_queues(struct zfcp_adapter *adapter)
17041da177e4SLinus Torvalds {
17051da177e4SLinus Torvalds 	unsigned long flags;
17061da177e4SLinus Torvalds 
17071da177e4SLinus Torvalds 	read_lock_irqsave(&zfcp_data.config_lock, flags);
17081da177e4SLinus Torvalds 	read_lock(&adapter->erp_lock);
17091da177e4SLinus Torvalds 	if (list_empty(&adapter->erp_ready_head) &&
17101da177e4SLinus Torvalds 	    list_empty(&adapter->erp_running_head)) {
17111da177e4SLinus Torvalds 			debug_text_event(adapter->erp_dbf, 4, "a_cq_wake");
17121da177e4SLinus Torvalds 			atomic_clear_mask(ZFCP_STATUS_ADAPTER_ERP_PENDING,
17131da177e4SLinus Torvalds 					  &adapter->status);
17141da177e4SLinus Torvalds 			wake_up(&adapter->erp_done_wqh);
17151da177e4SLinus Torvalds 	} else
17161da177e4SLinus Torvalds 		debug_text_event(adapter->erp_dbf, 5, "a_cq_notempty");
17171da177e4SLinus Torvalds 	read_unlock(&adapter->erp_lock);
17181da177e4SLinus Torvalds 	read_unlock_irqrestore(&zfcp_data.config_lock, flags);
17191da177e4SLinus Torvalds 
17201da177e4SLinus Torvalds 	return 0;
17211da177e4SLinus Torvalds }
17221da177e4SLinus Torvalds 
17231da177e4SLinus Torvalds /**
17241da177e4SLinus Torvalds  * zfcp_erp_wait - wait for completion of error recovery on an adapter
17251da177e4SLinus Torvalds  * @adapter: adapter for which to wait for completion of its error recovery
17261da177e4SLinus Torvalds  * Return: 0
17271da177e4SLinus Torvalds  */
17281da177e4SLinus Torvalds int
17291da177e4SLinus Torvalds zfcp_erp_wait(struct zfcp_adapter *adapter)
17301da177e4SLinus Torvalds {
17311da177e4SLinus Torvalds 	int retval = 0;
17321da177e4SLinus Torvalds 
17331da177e4SLinus Torvalds 	wait_event(adapter->erp_done_wqh,
17341da177e4SLinus Torvalds 		   !atomic_test_mask(ZFCP_STATUS_ADAPTER_ERP_PENDING,
17351da177e4SLinus Torvalds 				     &adapter->status));
17361da177e4SLinus Torvalds 
17371da177e4SLinus Torvalds 	return retval;
17381da177e4SLinus Torvalds }
17391da177e4SLinus Torvalds 
17401da177e4SLinus Torvalds void
17411da177e4SLinus Torvalds zfcp_erp_modify_adapter_status(struct zfcp_adapter *adapter,
17421da177e4SLinus Torvalds 			       u32 mask, int set_or_clear)
17431da177e4SLinus Torvalds {
17441da177e4SLinus Torvalds 	struct zfcp_port *port;
17451da177e4SLinus Torvalds 	u32 common_mask = mask & ZFCP_COMMON_FLAGS;
17461da177e4SLinus Torvalds 
17471da177e4SLinus Torvalds 	if (set_or_clear == ZFCP_SET) {
17481da177e4SLinus Torvalds 		atomic_set_mask(mask, &adapter->status);
17491da177e4SLinus Torvalds 		debug_text_event(adapter->erp_dbf, 3, "a_mod_as_s");
17501da177e4SLinus Torvalds 	} else {
17511da177e4SLinus Torvalds 		atomic_clear_mask(mask, &adapter->status);
17521da177e4SLinus Torvalds 		if (mask & ZFCP_STATUS_COMMON_ERP_FAILED)
17531da177e4SLinus Torvalds 			atomic_set(&adapter->erp_counter, 0);
17541da177e4SLinus Torvalds 		debug_text_event(adapter->erp_dbf, 3, "a_mod_as_c");
17551da177e4SLinus Torvalds 	}
17561da177e4SLinus Torvalds 	debug_event(adapter->erp_dbf, 3, &mask, sizeof (u32));
17571da177e4SLinus Torvalds 
17581da177e4SLinus Torvalds 	/* Deal with all underlying devices, only pass common_mask */
17591da177e4SLinus Torvalds 	if (common_mask)
17601da177e4SLinus Torvalds 		list_for_each_entry(port, &adapter->port_list_head, list)
17611da177e4SLinus Torvalds 		    zfcp_erp_modify_port_status(port, common_mask,
17621da177e4SLinus Torvalds 						set_or_clear);
17631da177e4SLinus Torvalds }
17641da177e4SLinus Torvalds 
17651da177e4SLinus Torvalds /*
17661da177e4SLinus Torvalds  * function:	zfcp_erp_modify_port_status
17671da177e4SLinus Torvalds  *
17681da177e4SLinus Torvalds  * purpose:	sets the port and all underlying devices to ERP_FAILED
17691da177e4SLinus Torvalds  *
17701da177e4SLinus Torvalds  */
17711da177e4SLinus Torvalds void
17721da177e4SLinus Torvalds zfcp_erp_modify_port_status(struct zfcp_port *port, u32 mask, int set_or_clear)
17731da177e4SLinus Torvalds {
17741da177e4SLinus Torvalds 	struct zfcp_unit *unit;
17751da177e4SLinus Torvalds 	u32 common_mask = mask & ZFCP_COMMON_FLAGS;
17761da177e4SLinus Torvalds 
17771da177e4SLinus Torvalds 	if (set_or_clear == ZFCP_SET) {
17781da177e4SLinus Torvalds 		atomic_set_mask(mask, &port->status);
17791da177e4SLinus Torvalds 		debug_text_event(port->adapter->erp_dbf, 3, "p_mod_ps_s");
17801da177e4SLinus Torvalds 	} else {
17811da177e4SLinus Torvalds 		atomic_clear_mask(mask, &port->status);
17821da177e4SLinus Torvalds 		if (mask & ZFCP_STATUS_COMMON_ERP_FAILED)
17831da177e4SLinus Torvalds 			atomic_set(&port->erp_counter, 0);
17841da177e4SLinus Torvalds 		debug_text_event(port->adapter->erp_dbf, 3, "p_mod_ps_c");
17851da177e4SLinus Torvalds 	}
17861da177e4SLinus Torvalds 	debug_event(port->adapter->erp_dbf, 3, &port->wwpn, sizeof (wwn_t));
17871da177e4SLinus Torvalds 	debug_event(port->adapter->erp_dbf, 3, &mask, sizeof (u32));
17881da177e4SLinus Torvalds 
17891da177e4SLinus Torvalds 	/* Modify status of all underlying devices, only pass common mask */
17901da177e4SLinus Torvalds 	if (common_mask)
17911da177e4SLinus Torvalds 		list_for_each_entry(unit, &port->unit_list_head, list)
17921da177e4SLinus Torvalds 		    zfcp_erp_modify_unit_status(unit, common_mask,
17931da177e4SLinus Torvalds 						set_or_clear);
17941da177e4SLinus Torvalds }
17951da177e4SLinus Torvalds 
17961da177e4SLinus Torvalds /*
17971da177e4SLinus Torvalds  * function:	zfcp_erp_modify_unit_status
17981da177e4SLinus Torvalds  *
17991da177e4SLinus Torvalds  * purpose:	sets the unit to ERP_FAILED
18001da177e4SLinus Torvalds  *
18011da177e4SLinus Torvalds  */
18021da177e4SLinus Torvalds void
18031da177e4SLinus Torvalds zfcp_erp_modify_unit_status(struct zfcp_unit *unit, u32 mask, int set_or_clear)
18041da177e4SLinus Torvalds {
18051da177e4SLinus Torvalds 	if (set_or_clear == ZFCP_SET) {
18061da177e4SLinus Torvalds 		atomic_set_mask(mask, &unit->status);
18071da177e4SLinus Torvalds 		debug_text_event(unit->port->adapter->erp_dbf, 3, "u_mod_us_s");
18081da177e4SLinus Torvalds 	} else {
18091da177e4SLinus Torvalds 		atomic_clear_mask(mask, &unit->status);
18101da177e4SLinus Torvalds 		if (mask & ZFCP_STATUS_COMMON_ERP_FAILED) {
18111da177e4SLinus Torvalds 			atomic_set(&unit->erp_counter, 0);
18121da177e4SLinus Torvalds 		}
18131da177e4SLinus Torvalds 		debug_text_event(unit->port->adapter->erp_dbf, 3, "u_mod_us_c");
18141da177e4SLinus Torvalds 	}
18151da177e4SLinus Torvalds 	debug_event(unit->port->adapter->erp_dbf, 3, &unit->fcp_lun,
18161da177e4SLinus Torvalds 		    sizeof (fcp_lun_t));
18171da177e4SLinus Torvalds 	debug_event(unit->port->adapter->erp_dbf, 3, &mask, sizeof (u32));
18181da177e4SLinus Torvalds }
18191da177e4SLinus Torvalds 
18201da177e4SLinus Torvalds /*
18211da177e4SLinus Torvalds  * function:
18221da177e4SLinus Torvalds  *
18231da177e4SLinus Torvalds  * purpose:	Wrappper for zfcp_erp_port_reopen_all_internal
18241da177e4SLinus Torvalds  *              used to ensure the correct locking
18251da177e4SLinus Torvalds  *
1826d6e05edcSAndreas Mohr  * returns:	0	- initiated action successfully
18271da177e4SLinus Torvalds  *		<0	- failed to initiate action
18281da177e4SLinus Torvalds  */
18291da177e4SLinus Torvalds int
18301da177e4SLinus Torvalds zfcp_erp_port_reopen_all(struct zfcp_adapter *adapter, int clear_mask)
18311da177e4SLinus Torvalds {
18321da177e4SLinus Torvalds 	int retval;
18331da177e4SLinus Torvalds 	unsigned long flags;
18341da177e4SLinus Torvalds 
18351da177e4SLinus Torvalds 	read_lock_irqsave(&zfcp_data.config_lock, flags);
18361da177e4SLinus Torvalds 	write_lock(&adapter->erp_lock);
18371da177e4SLinus Torvalds 	retval = zfcp_erp_port_reopen_all_internal(adapter, clear_mask);
18381da177e4SLinus Torvalds 	write_unlock(&adapter->erp_lock);
18391da177e4SLinus Torvalds 	read_unlock_irqrestore(&zfcp_data.config_lock, flags);
18401da177e4SLinus Torvalds 
18411da177e4SLinus Torvalds 	return retval;
18421da177e4SLinus Torvalds }
18431da177e4SLinus Torvalds 
18441da177e4SLinus Torvalds static int
18451da177e4SLinus Torvalds zfcp_erp_port_reopen_all_internal(struct zfcp_adapter *adapter, int clear_mask)
18461da177e4SLinus Torvalds {
18471da177e4SLinus Torvalds 	int retval = 0;
18481da177e4SLinus Torvalds 	struct zfcp_port *port;
18491da177e4SLinus Torvalds 
18501da177e4SLinus Torvalds 	list_for_each_entry(port, &adapter->port_list_head, list)
18511da177e4SLinus Torvalds 		if (!atomic_test_mask(ZFCP_STATUS_PORT_WKA, &port->status))
18521da177e4SLinus Torvalds 			zfcp_erp_port_reopen_internal(port, clear_mask);
18531da177e4SLinus Torvalds 
18541da177e4SLinus Torvalds 	return retval;
18551da177e4SLinus Torvalds }
18561da177e4SLinus Torvalds 
18571da177e4SLinus Torvalds /*
18581da177e4SLinus Torvalds  * function:
18591da177e4SLinus Torvalds  *
18601da177e4SLinus Torvalds  * purpose:
18611da177e4SLinus Torvalds  *
18621da177e4SLinus Torvalds  * returns:	FIXME
18631da177e4SLinus Torvalds  */
18641da177e4SLinus Torvalds static int
18651da177e4SLinus Torvalds zfcp_erp_unit_reopen_all_internal(struct zfcp_port *port, int clear_mask)
18661da177e4SLinus Torvalds {
18671da177e4SLinus Torvalds 	int retval = 0;
18681da177e4SLinus Torvalds 	struct zfcp_unit *unit;
18691da177e4SLinus Torvalds 
18701da177e4SLinus Torvalds 	list_for_each_entry(unit, &port->unit_list_head, list)
18711da177e4SLinus Torvalds 	    zfcp_erp_unit_reopen_internal(unit, clear_mask);
18721da177e4SLinus Torvalds 
18731da177e4SLinus Torvalds 	return retval;
18741da177e4SLinus Torvalds }
18751da177e4SLinus Torvalds 
18761da177e4SLinus Torvalds /*
18771da177e4SLinus Torvalds  * function:
18781da177e4SLinus Torvalds  *
18791da177e4SLinus Torvalds  * purpose:	this routine executes the 'Reopen Adapter' action
18801da177e4SLinus Torvalds  *		(the entire action is processed synchronously, since
18811da177e4SLinus Torvalds  *		there are no actions which might be run concurrently
18821da177e4SLinus Torvalds  *		per definition)
18831da177e4SLinus Torvalds  *
18841da177e4SLinus Torvalds  * returns:	ZFCP_ERP_SUCCEEDED	- action finished successfully
18851da177e4SLinus Torvalds  *		ZFCP_ERP_FAILED		- action finished unsuccessfully
18861da177e4SLinus Torvalds  */
18871da177e4SLinus Torvalds static int
18881da177e4SLinus Torvalds zfcp_erp_adapter_strategy(struct zfcp_erp_action *erp_action)
18891da177e4SLinus Torvalds {
18901da177e4SLinus Torvalds 	int retval;
18911da177e4SLinus Torvalds 	struct zfcp_adapter *adapter = erp_action->adapter;
18921da177e4SLinus Torvalds 
18931da177e4SLinus Torvalds 	retval = zfcp_erp_adapter_strategy_close(erp_action);
18941da177e4SLinus Torvalds 	if (erp_action->status & ZFCP_STATUS_ERP_CLOSE_ONLY)
18951da177e4SLinus Torvalds 		retval = ZFCP_ERP_EXIT;
18961da177e4SLinus Torvalds 	else
18971da177e4SLinus Torvalds 		retval = zfcp_erp_adapter_strategy_open(erp_action);
18981da177e4SLinus Torvalds 
18991da177e4SLinus Torvalds 	debug_text_event(adapter->erp_dbf, 3, "a_ast/ret");
19001da177e4SLinus Torvalds 	debug_event(adapter->erp_dbf, 3, &erp_action->action, sizeof (int));
19011da177e4SLinus Torvalds 	debug_event(adapter->erp_dbf, 3, &retval, sizeof (int));
19021da177e4SLinus Torvalds 
19031da177e4SLinus Torvalds 	if (retval == ZFCP_ERP_FAILED) {
19041da177e4SLinus Torvalds 		ZFCP_LOG_INFO("Waiting to allow the adapter %s "
19051da177e4SLinus Torvalds 			      "to recover itself\n",
19061da177e4SLinus Torvalds 			      zfcp_get_busid_by_adapter(adapter));
190718edcdbdSChristof Schmitt 		ssleep(ZFCP_TYPE2_RECOVERY_TIME);
19081da177e4SLinus Torvalds 	}
19091da177e4SLinus Torvalds 
19101da177e4SLinus Torvalds 	return retval;
19111da177e4SLinus Torvalds }
19121da177e4SLinus Torvalds 
19131da177e4SLinus Torvalds /*
19141da177e4SLinus Torvalds  * function:
19151da177e4SLinus Torvalds  *
19161da177e4SLinus Torvalds  * purpose:
19171da177e4SLinus Torvalds  *
19181da177e4SLinus Torvalds  * returns:	ZFCP_ERP_SUCCEEDED      - action finished successfully
19191da177e4SLinus Torvalds  *              ZFCP_ERP_FAILED         - action finished unsuccessfully
19201da177e4SLinus Torvalds  */
19211da177e4SLinus Torvalds static int
19221da177e4SLinus Torvalds zfcp_erp_adapter_strategy_close(struct zfcp_erp_action *erp_action)
19231da177e4SLinus Torvalds {
19241da177e4SLinus Torvalds 	int retval;
19251da177e4SLinus Torvalds 
19261da177e4SLinus Torvalds 	atomic_set_mask(ZFCP_STATUS_COMMON_CLOSING,
19271da177e4SLinus Torvalds 			&erp_action->adapter->status);
19281da177e4SLinus Torvalds 	retval = zfcp_erp_adapter_strategy_generic(erp_action, 1);
19291da177e4SLinus Torvalds 	atomic_clear_mask(ZFCP_STATUS_COMMON_CLOSING,
19301da177e4SLinus Torvalds 			  &erp_action->adapter->status);
19311da177e4SLinus Torvalds 
19321da177e4SLinus Torvalds 	return retval;
19331da177e4SLinus Torvalds }
19341da177e4SLinus Torvalds 
19351da177e4SLinus Torvalds /*
19361da177e4SLinus Torvalds  * function:
19371da177e4SLinus Torvalds  *
19381da177e4SLinus Torvalds  * purpose:
19391da177e4SLinus Torvalds  *
19401da177e4SLinus Torvalds  * returns:	ZFCP_ERP_SUCCEEDED      - action finished successfully
19411da177e4SLinus Torvalds  *              ZFCP_ERP_FAILED         - action finished unsuccessfully
19421da177e4SLinus Torvalds  */
19431da177e4SLinus Torvalds static int
19441da177e4SLinus Torvalds zfcp_erp_adapter_strategy_open(struct zfcp_erp_action *erp_action)
19451da177e4SLinus Torvalds {
19461da177e4SLinus Torvalds 	int retval;
19471da177e4SLinus Torvalds 
19481da177e4SLinus Torvalds 	atomic_set_mask(ZFCP_STATUS_COMMON_OPENING,
19491da177e4SLinus Torvalds 			&erp_action->adapter->status);
19501da177e4SLinus Torvalds 	retval = zfcp_erp_adapter_strategy_generic(erp_action, 0);
19511da177e4SLinus Torvalds 	atomic_clear_mask(ZFCP_STATUS_COMMON_OPENING,
19521da177e4SLinus Torvalds 			  &erp_action->adapter->status);
19531da177e4SLinus Torvalds 
19541da177e4SLinus Torvalds 	return retval;
19551da177e4SLinus Torvalds }
19561da177e4SLinus Torvalds 
19571da177e4SLinus Torvalds /*
19581da177e4SLinus Torvalds  * function:    zfcp_register_adapter
19591da177e4SLinus Torvalds  *
19601da177e4SLinus Torvalds  * purpose:	allocate the irq associated with this devno and register
19611da177e4SLinus Torvalds  *		the FSF adapter with the SCSI stack
19621da177e4SLinus Torvalds  *
19631da177e4SLinus Torvalds  * returns:
19641da177e4SLinus Torvalds  */
19651da177e4SLinus Torvalds static int
19661da177e4SLinus Torvalds zfcp_erp_adapter_strategy_generic(struct zfcp_erp_action *erp_action, int close)
19671da177e4SLinus Torvalds {
19681da177e4SLinus Torvalds 	int retval = ZFCP_ERP_SUCCEEDED;
19691da177e4SLinus Torvalds 
19701da177e4SLinus Torvalds 	if (close)
19711da177e4SLinus Torvalds 		goto close_only;
19721da177e4SLinus Torvalds 
19731da177e4SLinus Torvalds 	retval = zfcp_erp_adapter_strategy_open_qdio(erp_action);
19741da177e4SLinus Torvalds 	if (retval != ZFCP_ERP_SUCCEEDED)
19751da177e4SLinus Torvalds 		goto failed_qdio;
19761da177e4SLinus Torvalds 
19771da177e4SLinus Torvalds 	retval = zfcp_erp_adapter_strategy_open_fsf(erp_action);
19781da177e4SLinus Torvalds 	if (retval != ZFCP_ERP_SUCCEEDED)
19791da177e4SLinus Torvalds 		goto failed_openfcp;
19801da177e4SLinus Torvalds 
19811da177e4SLinus Torvalds 	atomic_set_mask(ZFCP_STATUS_COMMON_OPEN, &erp_action->adapter->status);
19821da177e4SLinus Torvalds 	goto out;
19831da177e4SLinus Torvalds 
19841da177e4SLinus Torvalds  close_only:
19851da177e4SLinus Torvalds 	atomic_clear_mask(ZFCP_STATUS_COMMON_OPEN,
19861da177e4SLinus Torvalds 			  &erp_action->adapter->status);
19871da177e4SLinus Torvalds 
19881da177e4SLinus Torvalds  failed_openfcp:
19892abbe866SAndreas Herrmann 	zfcp_close_fsf(erp_action->adapter);
19901da177e4SLinus Torvalds  failed_qdio:
1991c7f6b3a3SVolker Sameske 	atomic_clear_mask(ZFCP_STATUS_ADAPTER_XCONFIG_OK |
1992c7f6b3a3SVolker Sameske 			  ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED |
1993c7f6b3a3SVolker Sameske 			  ZFCP_STATUS_ADAPTER_XPORT_OK,
1994c7f6b3a3SVolker Sameske 			  &erp_action->adapter->status);
19951da177e4SLinus Torvalds  out:
19961da177e4SLinus Torvalds 	return retval;
19971da177e4SLinus Torvalds }
19981da177e4SLinus Torvalds 
19991da177e4SLinus Torvalds /*
20001da177e4SLinus Torvalds  * function:    zfcp_qdio_init
20011da177e4SLinus Torvalds  *
20021da177e4SLinus Torvalds  * purpose:	setup QDIO operation for specified adapter
20031da177e4SLinus Torvalds  *
20041da177e4SLinus Torvalds  * returns:	0 - successful setup
20051da177e4SLinus Torvalds  *		!0 - failed setup
20061da177e4SLinus Torvalds  */
2007364c8558SHeiko Carstens static int
20081da177e4SLinus Torvalds zfcp_erp_adapter_strategy_open_qdio(struct zfcp_erp_action *erp_action)
20091da177e4SLinus Torvalds {
20101da177e4SLinus Torvalds 	int retval;
20111da177e4SLinus Torvalds 	int i;
20121da177e4SLinus Torvalds 	volatile struct qdio_buffer_element *sbale;
20131da177e4SLinus Torvalds 	struct zfcp_adapter *adapter = erp_action->adapter;
20141da177e4SLinus Torvalds 
20151da177e4SLinus Torvalds 	if (atomic_test_mask(ZFCP_STATUS_ADAPTER_QDIOUP, &adapter->status)) {
20161da177e4SLinus Torvalds 		ZFCP_LOG_NORMAL("bug: second attempt to set up QDIO on "
20171da177e4SLinus Torvalds 				"adapter %s\n",
20181da177e4SLinus Torvalds 				zfcp_get_busid_by_adapter(adapter));
20191da177e4SLinus Torvalds 		goto failed_sanity;
20201da177e4SLinus Torvalds 	}
20211da177e4SLinus Torvalds 
20221da177e4SLinus Torvalds 	if (qdio_establish(&adapter->qdio_init_data) != 0) {
20231da177e4SLinus Torvalds 		ZFCP_LOG_INFO("error: establishment of QDIO queues failed "
20241da177e4SLinus Torvalds 			      "on adapter %s\n",
20251da177e4SLinus Torvalds 			      zfcp_get_busid_by_adapter(adapter));
20261da177e4SLinus Torvalds 		goto failed_qdio_establish;
20271da177e4SLinus Torvalds 	}
20281da177e4SLinus Torvalds 	debug_text_event(adapter->erp_dbf, 3, "qdio_est");
20291da177e4SLinus Torvalds 
20301da177e4SLinus Torvalds 	if (qdio_activate(adapter->ccw_device, 0) != 0) {
20311da177e4SLinus Torvalds 		ZFCP_LOG_INFO("error: activation of QDIO queues failed "
20321da177e4SLinus Torvalds 			      "on adapter %s\n",
20331da177e4SLinus Torvalds 			      zfcp_get_busid_by_adapter(adapter));
20341da177e4SLinus Torvalds 		goto failed_qdio_activate;
20351da177e4SLinus Torvalds 	}
20361da177e4SLinus Torvalds 	debug_text_event(adapter->erp_dbf, 3, "qdio_act");
20371da177e4SLinus Torvalds 
20381da177e4SLinus Torvalds 	/*
20391da177e4SLinus Torvalds 	 * put buffers into response queue,
20401da177e4SLinus Torvalds 	 */
20411da177e4SLinus Torvalds 	for (i = 0; i < QDIO_MAX_BUFFERS_PER_Q; i++) {
20421da177e4SLinus Torvalds 		sbale = &(adapter->response_queue.buffer[i]->element[0]);
20431da177e4SLinus Torvalds 		sbale->length = 0;
20441da177e4SLinus Torvalds 		sbale->flags = SBAL_FLAGS_LAST_ENTRY;
20456aae8738SAl Viro 		sbale->addr = NULL;
20461da177e4SLinus Torvalds 	}
20471da177e4SLinus Torvalds 
20481da177e4SLinus Torvalds 	ZFCP_LOG_TRACE("calling do_QDIO on adapter %s (flags=0x%x, "
20491da177e4SLinus Torvalds 		       "queue_no=%i, index_in_queue=%i, count=%i)\n",
20501da177e4SLinus Torvalds 		       zfcp_get_busid_by_adapter(adapter),
20511da177e4SLinus Torvalds 		       QDIO_FLAG_SYNC_INPUT, 0, 0, QDIO_MAX_BUFFERS_PER_Q);
20521da177e4SLinus Torvalds 
20531da177e4SLinus Torvalds 	retval = do_QDIO(adapter->ccw_device,
20541da177e4SLinus Torvalds 			 QDIO_FLAG_SYNC_INPUT,
20551da177e4SLinus Torvalds 			 0, 0, QDIO_MAX_BUFFERS_PER_Q, NULL);
20561da177e4SLinus Torvalds 
20571da177e4SLinus Torvalds 	if (retval) {
20581da177e4SLinus Torvalds 		ZFCP_LOG_NORMAL("bug: setup of QDIO failed (retval=%d)\n",
20591da177e4SLinus Torvalds 				retval);
20601da177e4SLinus Torvalds 		goto failed_do_qdio;
20611da177e4SLinus Torvalds 	} else {
20621da177e4SLinus Torvalds 		adapter->response_queue.free_index = 0;
20631da177e4SLinus Torvalds 		atomic_set(&adapter->response_queue.free_count, 0);
20641da177e4SLinus Torvalds 		ZFCP_LOG_DEBUG("%i buffers successfully enqueued to "
20651da177e4SLinus Torvalds 			       "response queue\n", QDIO_MAX_BUFFERS_PER_Q);
20661da177e4SLinus Torvalds 	}
20671da177e4SLinus Torvalds 	/* set index of first avalable SBALS / number of available SBALS */
20681da177e4SLinus Torvalds 	adapter->request_queue.free_index = 0;
20691da177e4SLinus Torvalds 	atomic_set(&adapter->request_queue.free_count, QDIO_MAX_BUFFERS_PER_Q);
20701da177e4SLinus Torvalds 	adapter->request_queue.distance_from_int = 0;
20711da177e4SLinus Torvalds 
20721da177e4SLinus Torvalds 	/* initialize waitqueue used to wait for free SBALs in requests queue */
20731da177e4SLinus Torvalds 	init_waitqueue_head(&adapter->request_wq);
20741da177e4SLinus Torvalds 
20751da177e4SLinus Torvalds 	/* ok, we did it - skip all cleanups for different failures */
20761da177e4SLinus Torvalds 	atomic_set_mask(ZFCP_STATUS_ADAPTER_QDIOUP, &adapter->status);
20771da177e4SLinus Torvalds 	retval = ZFCP_ERP_SUCCEEDED;
20781da177e4SLinus Torvalds 	goto out;
20791da177e4SLinus Torvalds 
20801da177e4SLinus Torvalds  failed_do_qdio:
20811da177e4SLinus Torvalds 	/* NOP */
20821da177e4SLinus Torvalds 
20831da177e4SLinus Torvalds  failed_qdio_activate:
20841da177e4SLinus Torvalds 	debug_text_event(adapter->erp_dbf, 3, "qdio_down1a");
20851da177e4SLinus Torvalds 	while (qdio_shutdown(adapter->ccw_device,
20861da177e4SLinus Torvalds 			     QDIO_FLAG_CLEANUP_USING_CLEAR) == -EINPROGRESS)
208718edcdbdSChristof Schmitt 		ssleep(1);
20881da177e4SLinus Torvalds 	debug_text_event(adapter->erp_dbf, 3, "qdio_down1b");
20891da177e4SLinus Torvalds 
20901da177e4SLinus Torvalds  failed_qdio_establish:
20911da177e4SLinus Torvalds  failed_sanity:
20921da177e4SLinus Torvalds 	retval = ZFCP_ERP_FAILED;
20931da177e4SLinus Torvalds 
20941da177e4SLinus Torvalds  out:
20951da177e4SLinus Torvalds 	return retval;
20961da177e4SLinus Torvalds }
20971da177e4SLinus Torvalds 
20981da177e4SLinus Torvalds 
20991da177e4SLinus Torvalds static int
21001da177e4SLinus Torvalds zfcp_erp_adapter_strategy_open_fsf(struct zfcp_erp_action *erp_action)
21011da177e4SLinus Torvalds {
21022448c459SAndreas Herrmann 	int retval;
21031da177e4SLinus Torvalds 
21042448c459SAndreas Herrmann 	retval = zfcp_erp_adapter_strategy_open_fsf_xconfig(erp_action);
21052448c459SAndreas Herrmann 	if (retval == ZFCP_ERP_FAILED)
21062448c459SAndreas Herrmann 		return ZFCP_ERP_FAILED;
21072448c459SAndreas Herrmann 
21082448c459SAndreas Herrmann 	retval = zfcp_erp_adapter_strategy_open_fsf_xport(erp_action);
21092448c459SAndreas Herrmann 	if (retval == ZFCP_ERP_FAILED)
2110aef4a983SMaxim Shchetynin 		return ZFCP_ERP_FAILED;
2111aef4a983SMaxim Shchetynin 
2112aef4a983SMaxim Shchetynin 	return zfcp_erp_adapter_strategy_open_fsf_statusread(erp_action);
21131da177e4SLinus Torvalds }
21141da177e4SLinus Torvalds 
21151da177e4SLinus Torvalds static int
21161da177e4SLinus Torvalds zfcp_erp_adapter_strategy_open_fsf_xconfig(struct zfcp_erp_action *erp_action)
21171da177e4SLinus Torvalds {
21181da177e4SLinus Torvalds 	int retval = ZFCP_ERP_SUCCEEDED;
21191da177e4SLinus Torvalds 	int retries;
212022753fa5SAndreas Herrmann 	int sleep = ZFCP_EXCHANGE_CONFIG_DATA_FIRST_SLEEP;
21211da177e4SLinus Torvalds 	struct zfcp_adapter *adapter = erp_action->adapter;
21221da177e4SLinus Torvalds 
21231da177e4SLinus Torvalds 	atomic_clear_mask(ZFCP_STATUS_ADAPTER_XCONFIG_OK, &adapter->status);
21241da177e4SLinus Torvalds 
212522753fa5SAndreas Herrmann 	for (retries = ZFCP_EXCHANGE_CONFIG_DATA_RETRIES; retries; retries--) {
21261da177e4SLinus Torvalds 		atomic_clear_mask(ZFCP_STATUS_ADAPTER_HOST_CON_INIT,
21271da177e4SLinus Torvalds 				  &adapter->status);
21281da177e4SLinus Torvalds 		ZFCP_LOG_DEBUG("Doing exchange config data\n");
21299f09c548SHeiko Carstens 		write_lock_irq(&adapter->erp_lock);
21301da177e4SLinus Torvalds 		zfcp_erp_action_to_running(erp_action);
21319f09c548SHeiko Carstens 		write_unlock_irq(&adapter->erp_lock);
21321da177e4SLinus Torvalds 		if (zfcp_fsf_exchange_config_data(erp_action)) {
21331da177e4SLinus Torvalds 			retval = ZFCP_ERP_FAILED;
21341da177e4SLinus Torvalds 			debug_text_event(adapter->erp_dbf, 5, "a_fstx_xf");
21351da177e4SLinus Torvalds 			ZFCP_LOG_INFO("error:  initiation of exchange of "
21361da177e4SLinus Torvalds 				      "configuration data failed for "
21371da177e4SLinus Torvalds 				      "adapter %s\n",
21381da177e4SLinus Torvalds 				      zfcp_get_busid_by_adapter(adapter));
21391da177e4SLinus Torvalds 			break;
21401da177e4SLinus Torvalds 		}
21411da177e4SLinus Torvalds 		debug_text_event(adapter->erp_dbf, 6, "a_fstx_xok");
21421da177e4SLinus Torvalds 		ZFCP_LOG_DEBUG("Xchange underway\n");
21431da177e4SLinus Torvalds 
21441da177e4SLinus Torvalds 		/*
21451da177e4SLinus Torvalds 		 * Why this works:
21461da177e4SLinus Torvalds 		 * Both the normal completion handler as well as the timeout
21471da177e4SLinus Torvalds 		 * handler will do an 'up' when the 'exchange config data'
21481da177e4SLinus Torvalds 		 * request completes or times out. Thus, the signal to go on
21491da177e4SLinus Torvalds 		 * won't be lost utilizing this semaphore.
21501da177e4SLinus Torvalds 		 * Furthermore, this 'adapter_reopen' action is
21511da177e4SLinus Torvalds 		 * guaranteed to be the only action being there (highest action
21521da177e4SLinus Torvalds 		 * which prevents other actions from being created).
21531da177e4SLinus Torvalds 		 * Resulting from that, the wake signal recognized here
21541da177e4SLinus Torvalds 		 * _must_ be the one belonging to the 'exchange config
21551da177e4SLinus Torvalds 		 * data' request.
21561da177e4SLinus Torvalds 		 */
2157348447e8SMartin Peschke 		zfcp_rec_dbf_event_thread(6, adapter, 1);
21581da177e4SLinus Torvalds 		down(&adapter->erp_ready_sem);
2159348447e8SMartin Peschke 		zfcp_rec_dbf_event_thread(7, adapter, 1);
21601da177e4SLinus Torvalds 		if (erp_action->status & ZFCP_STATUS_ERP_TIMEDOUT) {
21611da177e4SLinus Torvalds 			ZFCP_LOG_INFO("error: exchange of configuration data "
21621da177e4SLinus Torvalds 				      "for adapter %s timed out\n",
21631da177e4SLinus Torvalds 				      zfcp_get_busid_by_adapter(adapter));
21641da177e4SLinus Torvalds 			break;
21651da177e4SLinus Torvalds 		}
216622753fa5SAndreas Herrmann 
216722753fa5SAndreas Herrmann 		if (!atomic_test_mask(ZFCP_STATUS_ADAPTER_HOST_CON_INIT,
216822753fa5SAndreas Herrmann 				     &adapter->status))
216922753fa5SAndreas Herrmann 			break;
217022753fa5SAndreas Herrmann 
21711da177e4SLinus Torvalds 		ZFCP_LOG_DEBUG("host connection still initialising... "
21721da177e4SLinus Torvalds 			       "waiting and retrying...\n");
21731da177e4SLinus Torvalds 		/* sleep a little bit before retry */
217418edcdbdSChristof Schmitt 		ssleep(sleep);
217522753fa5SAndreas Herrmann 		sleep *= 2;
21761da177e4SLinus Torvalds 	}
21771da177e4SLinus Torvalds 
2178c7f6b3a3SVolker Sameske 	atomic_clear_mask(ZFCP_STATUS_ADAPTER_HOST_CON_INIT,
2179c7f6b3a3SVolker Sameske 			  &adapter->status);
2180c7f6b3a3SVolker Sameske 
21811da177e4SLinus Torvalds 	if (!atomic_test_mask(ZFCP_STATUS_ADAPTER_XCONFIG_OK,
21821da177e4SLinus Torvalds 			      &adapter->status)) {
21831da177e4SLinus Torvalds 		ZFCP_LOG_INFO("error: exchange of configuration data for "
21841da177e4SLinus Torvalds 			      "adapter %s failed\n",
21851da177e4SLinus Torvalds 			      zfcp_get_busid_by_adapter(adapter));
21861da177e4SLinus Torvalds 		retval = ZFCP_ERP_FAILED;
21871da177e4SLinus Torvalds 	}
21881da177e4SLinus Torvalds 
21891da177e4SLinus Torvalds 	return retval;
21901da177e4SLinus Torvalds }
21911da177e4SLinus Torvalds 
2192aef4a983SMaxim Shchetynin static int
2193aef4a983SMaxim Shchetynin zfcp_erp_adapter_strategy_open_fsf_xport(struct zfcp_erp_action *erp_action)
2194aef4a983SMaxim Shchetynin {
21952448c459SAndreas Herrmann 	int ret;
21962f8f3ed5SAndreas Herrmann 	struct zfcp_adapter *adapter;
2197aef4a983SMaxim Shchetynin 
21982f8f3ed5SAndreas Herrmann 	adapter = erp_action->adapter;
2199aef4a983SMaxim Shchetynin 	atomic_clear_mask(ZFCP_STATUS_ADAPTER_XPORT_OK, &adapter->status);
2200aef4a983SMaxim Shchetynin 
22019f09c548SHeiko Carstens 	write_lock_irq(&adapter->erp_lock);
2202aef4a983SMaxim Shchetynin 	zfcp_erp_action_to_running(erp_action);
22039f09c548SHeiko Carstens 	write_unlock_irq(&adapter->erp_lock);
22042f8f3ed5SAndreas Herrmann 
220552ef11a7SSwen Schillig 	ret = zfcp_fsf_exchange_port_data(erp_action);
22062448c459SAndreas Herrmann 	if (ret == -EOPNOTSUPP) {
22072448c459SAndreas Herrmann 		debug_text_event(adapter->erp_dbf, 3, "a_xport_notsupp");
22082448c459SAndreas Herrmann 		return ZFCP_ERP_SUCCEEDED;
22092448c459SAndreas Herrmann 	} else if (ret) {
22102448c459SAndreas Herrmann 		debug_text_event(adapter->erp_dbf, 3, "a_xport_failed");
22112448c459SAndreas Herrmann 		return ZFCP_ERP_FAILED;
2212aef4a983SMaxim Shchetynin 	}
22132448c459SAndreas Herrmann 	debug_text_event(adapter->erp_dbf, 6, "a_xport_ok");
2214aef4a983SMaxim Shchetynin 
22152f8f3ed5SAndreas Herrmann 	ret = ZFCP_ERP_SUCCEEDED;
2216348447e8SMartin Peschke 	zfcp_rec_dbf_event_thread(8, adapter, 1);
2217aef4a983SMaxim Shchetynin 	down(&adapter->erp_ready_sem);
2218348447e8SMartin Peschke 	zfcp_rec_dbf_event_thread(9, adapter, 1);
2219aef4a983SMaxim Shchetynin 	if (erp_action->status & ZFCP_STATUS_ERP_TIMEDOUT) {
22202f8f3ed5SAndreas Herrmann 		ZFCP_LOG_INFO("error: exchange port data timed out (adapter "
22212f8f3ed5SAndreas Herrmann 			      "%s)\n", zfcp_get_busid_by_adapter(adapter));
22222f8f3ed5SAndreas Herrmann 		ret = ZFCP_ERP_FAILED;
2223aef4a983SMaxim Shchetynin 	}
2224f6c0e7a7SAndreas Herrmann 
2225f6c0e7a7SAndreas Herrmann 	/* don't treat as error for the sake of compatibility */
2226f6c0e7a7SAndreas Herrmann 	if (!atomic_test_mask(ZFCP_STATUS_ADAPTER_XPORT_OK, &adapter->status))
2227f6c0e7a7SAndreas Herrmann 		ZFCP_LOG_INFO("warning: exchange port data failed (adapter "
22282f8f3ed5SAndreas Herrmann 			      "%s\n", zfcp_get_busid_by_adapter(adapter));
2229aef4a983SMaxim Shchetynin 
22302f8f3ed5SAndreas Herrmann 	return ret;
2231aef4a983SMaxim Shchetynin }
2232aef4a983SMaxim Shchetynin 
22331da177e4SLinus Torvalds static int
22341da177e4SLinus Torvalds zfcp_erp_adapter_strategy_open_fsf_statusread(struct zfcp_erp_action
22351da177e4SLinus Torvalds 					      *erp_action)
22361da177e4SLinus Torvalds {
22371da177e4SLinus Torvalds 	int retval = ZFCP_ERP_SUCCEEDED;
22381da177e4SLinus Torvalds 	int temp_ret;
22391da177e4SLinus Torvalds 	struct zfcp_adapter *adapter = erp_action->adapter;
22401da177e4SLinus Torvalds 	int i;
22411da177e4SLinus Torvalds 
22421da177e4SLinus Torvalds 	adapter->status_read_failed = 0;
22431da177e4SLinus Torvalds 	for (i = 0; i < ZFCP_STATUS_READS_RECOM; i++) {
22441da177e4SLinus Torvalds 		temp_ret = zfcp_fsf_status_read(adapter, ZFCP_WAIT_FOR_SBAL);
22451da177e4SLinus Torvalds 		if (temp_ret < 0) {
22461da177e4SLinus Torvalds 			ZFCP_LOG_INFO("error: set-up of unsolicited status "
22471da177e4SLinus Torvalds 				      "notification failed on adapter %s\n",
22481da177e4SLinus Torvalds 				      zfcp_get_busid_by_adapter(adapter));
22491da177e4SLinus Torvalds 			retval = ZFCP_ERP_FAILED;
22501da177e4SLinus Torvalds 			i--;
22511da177e4SLinus Torvalds 			break;
22521da177e4SLinus Torvalds 		}
22531da177e4SLinus Torvalds 	}
22541da177e4SLinus Torvalds 
22551da177e4SLinus Torvalds 	return retval;
22561da177e4SLinus Torvalds }
22571da177e4SLinus Torvalds 
22581da177e4SLinus Torvalds /*
22591da177e4SLinus Torvalds  * function:
22601da177e4SLinus Torvalds  *
22611da177e4SLinus Torvalds  * purpose:	this routine executes the 'Reopen Physical Port' action
22621da177e4SLinus Torvalds  *
22631da177e4SLinus Torvalds  * returns:	ZFCP_ERP_CONTINUES	- action continues (asynchronously)
22641da177e4SLinus Torvalds  *		ZFCP_ERP_SUCCEEDED	- action finished successfully
22651da177e4SLinus Torvalds  *		ZFCP_ERP_FAILED		- action finished unsuccessfully
22661da177e4SLinus Torvalds  */
22671da177e4SLinus Torvalds static int
22681da177e4SLinus Torvalds zfcp_erp_port_forced_strategy(struct zfcp_erp_action *erp_action)
22691da177e4SLinus Torvalds {
22701da177e4SLinus Torvalds 	int retval = ZFCP_ERP_FAILED;
22711da177e4SLinus Torvalds 	struct zfcp_port *port = erp_action->port;
22721da177e4SLinus Torvalds 	struct zfcp_adapter *adapter = erp_action->adapter;
22731da177e4SLinus Torvalds 
22741da177e4SLinus Torvalds 	switch (erp_action->step) {
22751da177e4SLinus Torvalds 
22761da177e4SLinus Torvalds 		/*
22771da177e4SLinus Torvalds 		 * FIXME:
22781da177e4SLinus Torvalds 		 * the ULP spec. begs for waiting for oustanding commands
22791da177e4SLinus Torvalds 		 */
22801da177e4SLinus Torvalds 	case ZFCP_ERP_STEP_UNINITIALIZED:
22811da177e4SLinus Torvalds 		zfcp_erp_port_strategy_clearstati(port);
22821da177e4SLinus Torvalds 		/*
22831da177e4SLinus Torvalds 		 * it would be sufficient to test only the normal open flag
22841da177e4SLinus Torvalds 		 * since the phys. open flag cannot be set if the normal
22851da177e4SLinus Torvalds 		 * open flag is unset - however, this is for readabilty ...
22861da177e4SLinus Torvalds 		 */
22871da177e4SLinus Torvalds 		if (atomic_test_mask((ZFCP_STATUS_PORT_PHYS_OPEN |
22881da177e4SLinus Torvalds 				      ZFCP_STATUS_COMMON_OPEN),
22891da177e4SLinus Torvalds 			             &port->status)) {
22901da177e4SLinus Torvalds 			ZFCP_LOG_DEBUG("port 0x%016Lx is open -> trying "
22911da177e4SLinus Torvalds 				       "close physical\n", port->wwpn);
22921da177e4SLinus Torvalds 			retval =
22931da177e4SLinus Torvalds 			    zfcp_erp_port_forced_strategy_close(erp_action);
22941da177e4SLinus Torvalds 		} else
22951da177e4SLinus Torvalds 			retval = ZFCP_ERP_FAILED;
22961da177e4SLinus Torvalds 		break;
22971da177e4SLinus Torvalds 
22981da177e4SLinus Torvalds 	case ZFCP_ERP_STEP_PHYS_PORT_CLOSING:
22991da177e4SLinus Torvalds 		if (atomic_test_mask(ZFCP_STATUS_PORT_PHYS_OPEN,
23001da177e4SLinus Torvalds 				     &port->status)) {
23011da177e4SLinus Torvalds 			ZFCP_LOG_DEBUG("close physical failed for port "
23021da177e4SLinus Torvalds 				       "0x%016Lx\n", port->wwpn);
23031da177e4SLinus Torvalds 			retval = ZFCP_ERP_FAILED;
23041da177e4SLinus Torvalds 		} else
23051da177e4SLinus Torvalds 			retval = ZFCP_ERP_SUCCEEDED;
23061da177e4SLinus Torvalds 		break;
23071da177e4SLinus Torvalds 	}
23081da177e4SLinus Torvalds 
23091da177e4SLinus Torvalds 	debug_text_event(adapter->erp_dbf, 3, "p_pfst/ret");
23101da177e4SLinus Torvalds 	debug_event(adapter->erp_dbf, 3, &port->wwpn, sizeof (wwn_t));
23111da177e4SLinus Torvalds 	debug_event(adapter->erp_dbf, 3, &erp_action->action, sizeof (int));
23121da177e4SLinus Torvalds 	debug_event(adapter->erp_dbf, 3, &retval, sizeof (int));
23131da177e4SLinus Torvalds 
23141da177e4SLinus Torvalds 	return retval;
23151da177e4SLinus Torvalds }
23161da177e4SLinus Torvalds 
23171da177e4SLinus Torvalds /*
23181da177e4SLinus Torvalds  * function:
23191da177e4SLinus Torvalds  *
23201da177e4SLinus Torvalds  * purpose:	this routine executes the 'Reopen Port' action
23211da177e4SLinus Torvalds  *
23221da177e4SLinus Torvalds  * returns:	ZFCP_ERP_CONTINUES	- action continues (asynchronously)
23231da177e4SLinus Torvalds  *		ZFCP_ERP_SUCCEEDED	- action finished successfully
23241da177e4SLinus Torvalds  *		ZFCP_ERP_FAILED		- action finished unsuccessfully
23251da177e4SLinus Torvalds  */
23261da177e4SLinus Torvalds static int
23271da177e4SLinus Torvalds zfcp_erp_port_strategy(struct zfcp_erp_action *erp_action)
23281da177e4SLinus Torvalds {
23291da177e4SLinus Torvalds 	int retval = ZFCP_ERP_FAILED;
23301da177e4SLinus Torvalds 	struct zfcp_port *port = erp_action->port;
23311da177e4SLinus Torvalds 	struct zfcp_adapter *adapter = erp_action->adapter;
23321da177e4SLinus Torvalds 
23331da177e4SLinus Torvalds 	switch (erp_action->step) {
23341da177e4SLinus Torvalds 
23351da177e4SLinus Torvalds 		/*
23361da177e4SLinus Torvalds 		 * FIXME:
23371da177e4SLinus Torvalds 		 * the ULP spec. begs for waiting for oustanding commands
23381da177e4SLinus Torvalds 		 */
23391da177e4SLinus Torvalds 	case ZFCP_ERP_STEP_UNINITIALIZED:
23401da177e4SLinus Torvalds 		zfcp_erp_port_strategy_clearstati(port);
23411da177e4SLinus Torvalds 		if (atomic_test_mask(ZFCP_STATUS_COMMON_OPEN, &port->status)) {
23421da177e4SLinus Torvalds 			ZFCP_LOG_DEBUG("port 0x%016Lx is open -> trying "
23431da177e4SLinus Torvalds 				       "close\n", port->wwpn);
23441da177e4SLinus Torvalds 			retval = zfcp_erp_port_strategy_close(erp_action);
23451da177e4SLinus Torvalds 			goto out;
23461da177e4SLinus Torvalds 		}		/* else it's already closed, open it */
23471da177e4SLinus Torvalds 		break;
23481da177e4SLinus Torvalds 
23491da177e4SLinus Torvalds 	case ZFCP_ERP_STEP_PORT_CLOSING:
23501da177e4SLinus Torvalds 		if (atomic_test_mask(ZFCP_STATUS_COMMON_OPEN, &port->status)) {
23511da177e4SLinus Torvalds 			ZFCP_LOG_DEBUG("close failed for port 0x%016Lx\n",
23521da177e4SLinus Torvalds 				       port->wwpn);
23531da177e4SLinus Torvalds 			retval = ZFCP_ERP_FAILED;
23541da177e4SLinus Torvalds 			goto out;
23551da177e4SLinus Torvalds 		}		/* else it's closed now, open it */
23561da177e4SLinus Torvalds 		break;
23571da177e4SLinus Torvalds 	}
23581da177e4SLinus Torvalds 	if (erp_action->status & ZFCP_STATUS_ERP_CLOSE_ONLY)
23591da177e4SLinus Torvalds 		retval = ZFCP_ERP_EXIT;
23601da177e4SLinus Torvalds 	else
23611da177e4SLinus Torvalds 		retval = zfcp_erp_port_strategy_open(erp_action);
23621da177e4SLinus Torvalds 
23631da177e4SLinus Torvalds  out:
23641da177e4SLinus Torvalds 	debug_text_event(adapter->erp_dbf, 3, "p_pst/ret");
23651da177e4SLinus Torvalds 	debug_event(adapter->erp_dbf, 3, &port->wwpn, sizeof (wwn_t));
23661da177e4SLinus Torvalds 	debug_event(adapter->erp_dbf, 3, &erp_action->action, sizeof (int));
23671da177e4SLinus Torvalds 	debug_event(adapter->erp_dbf, 3, &retval, sizeof (int));
23681da177e4SLinus Torvalds 
23691da177e4SLinus Torvalds 	return retval;
23701da177e4SLinus Torvalds }
23711da177e4SLinus Torvalds 
23721da177e4SLinus Torvalds static int
23731da177e4SLinus Torvalds zfcp_erp_port_strategy_open(struct zfcp_erp_action *erp_action)
23741da177e4SLinus Torvalds {
23751da177e4SLinus Torvalds 	int retval;
23761da177e4SLinus Torvalds 
23771da177e4SLinus Torvalds 	if (atomic_test_mask(ZFCP_STATUS_PORT_WKA,
23781da177e4SLinus Torvalds 			     &erp_action->port->status))
23791da177e4SLinus Torvalds 		retval = zfcp_erp_port_strategy_open_nameserver(erp_action);
23801da177e4SLinus Torvalds 	else
23811da177e4SLinus Torvalds 		retval = zfcp_erp_port_strategy_open_common(erp_action);
23821da177e4SLinus Torvalds 
23831da177e4SLinus Torvalds 	return retval;
23841da177e4SLinus Torvalds }
23851da177e4SLinus Torvalds 
23861da177e4SLinus Torvalds static int
23871da177e4SLinus Torvalds zfcp_erp_port_strategy_open_common(struct zfcp_erp_action *erp_action)
23881da177e4SLinus Torvalds {
23891da177e4SLinus Torvalds 	int retval = 0;
23901da177e4SLinus Torvalds 	struct zfcp_adapter *adapter = erp_action->adapter;
23911da177e4SLinus Torvalds 	struct zfcp_port *port = erp_action->port;
23921da177e4SLinus Torvalds 
23931da177e4SLinus Torvalds 	switch (erp_action->step) {
23941da177e4SLinus Torvalds 
23951da177e4SLinus Torvalds 	case ZFCP_ERP_STEP_UNINITIALIZED:
23961da177e4SLinus Torvalds 	case ZFCP_ERP_STEP_PHYS_PORT_CLOSING:
23971da177e4SLinus Torvalds 	case ZFCP_ERP_STEP_PORT_CLOSING:
2398ad757cdfSAndreas Herrmann 		if (fc_host_port_type(adapter->scsi_host) == FC_PORTTYPE_PTP) {
23996f71d9bcS 			if (port->wwpn != adapter->peer_wwpn) {
24006f71d9bcS 				ZFCP_LOG_NORMAL("Failed to open port 0x%016Lx "
24016f71d9bcS 						"on adapter %s.\nPeer WWPN "
24026f71d9bcS 						"0x%016Lx does not match\n",
24036f71d9bcS 						port->wwpn,
24046f71d9bcS 						zfcp_get_busid_by_adapter(adapter),
24056f71d9bcS 						adapter->peer_wwpn);
24066f71d9bcS 				zfcp_erp_port_failed(port);
24076f71d9bcS 				retval = ZFCP_ERP_FAILED;
24086f71d9bcS 				break;
24096f71d9bcS 			}
24106f71d9bcS 			port->d_id = adapter->peer_d_id;
24116f71d9bcS 			atomic_set_mask(ZFCP_STATUS_PORT_DID_DID, &port->status);
24126f71d9bcS 			retval = zfcp_erp_port_strategy_open_port(erp_action);
24136f71d9bcS 			break;
24146f71d9bcS 		}
24151da177e4SLinus Torvalds 		if (!(adapter->nameserver_port)) {
24161da177e4SLinus Torvalds 			retval = zfcp_nameserver_enqueue(adapter);
24171da177e4SLinus Torvalds 			if (retval != 0) {
24181da177e4SLinus Torvalds 				ZFCP_LOG_NORMAL("error: nameserver port "
24191da177e4SLinus Torvalds 						"unavailable for adapter %s\n",
24201da177e4SLinus Torvalds 						zfcp_get_busid_by_adapter(adapter));
24211da177e4SLinus Torvalds 				retval = ZFCP_ERP_FAILED;
24221da177e4SLinus Torvalds 				break;
24231da177e4SLinus Torvalds 			}
24241da177e4SLinus Torvalds 		}
24251da177e4SLinus Torvalds 		if (!atomic_test_mask(ZFCP_STATUS_COMMON_UNBLOCKED,
24261da177e4SLinus Torvalds 				      &adapter->nameserver_port->status)) {
24271da177e4SLinus Torvalds 			ZFCP_LOG_DEBUG("nameserver port is not open -> open "
24281da177e4SLinus Torvalds 				       "nameserver port\n");
24291da177e4SLinus Torvalds 			/* nameserver port may live again */
24301da177e4SLinus Torvalds 			atomic_set_mask(ZFCP_STATUS_COMMON_RUNNING,
24311da177e4SLinus Torvalds 					&adapter->nameserver_port->status);
24321da177e4SLinus Torvalds 			if (zfcp_erp_port_reopen(adapter->nameserver_port, 0)
24331da177e4SLinus Torvalds 			    >= 0) {
24341da177e4SLinus Torvalds 				erp_action->step =
24351da177e4SLinus Torvalds 					ZFCP_ERP_STEP_NAMESERVER_OPEN;
24361da177e4SLinus Torvalds 				retval = ZFCP_ERP_CONTINUES;
24371da177e4SLinus Torvalds 			} else
24381da177e4SLinus Torvalds 				retval = ZFCP_ERP_FAILED;
24391da177e4SLinus Torvalds 			break;
24401da177e4SLinus Torvalds 		}
24411da177e4SLinus Torvalds 		/* else nameserver port is already open, fall through */
24421da177e4SLinus Torvalds 	case ZFCP_ERP_STEP_NAMESERVER_OPEN:
24431da177e4SLinus Torvalds 		if (!atomic_test_mask(ZFCP_STATUS_COMMON_OPEN,
24441da177e4SLinus Torvalds 				      &adapter->nameserver_port->status)) {
24451da177e4SLinus Torvalds 			ZFCP_LOG_DEBUG("open failed for nameserver port\n");
24461da177e4SLinus Torvalds 			retval = ZFCP_ERP_FAILED;
24471da177e4SLinus Torvalds 		} else {
24481da177e4SLinus Torvalds 			ZFCP_LOG_DEBUG("nameserver port is open -> "
24491da177e4SLinus Torvalds 				       "nameserver look-up for port 0x%016Lx\n",
24501da177e4SLinus Torvalds 				       port->wwpn);
24511da177e4SLinus Torvalds 			retval = zfcp_erp_port_strategy_open_common_lookup
24521da177e4SLinus Torvalds 				(erp_action);
24531da177e4SLinus Torvalds 		}
24541da177e4SLinus Torvalds 		break;
24551da177e4SLinus Torvalds 
24561da177e4SLinus Torvalds 	case ZFCP_ERP_STEP_NAMESERVER_LOOKUP:
24571da177e4SLinus Torvalds 		if (!atomic_test_mask(ZFCP_STATUS_PORT_DID_DID, &port->status)) {
24581da177e4SLinus Torvalds 			if (atomic_test_mask
24591da177e4SLinus Torvalds 			    (ZFCP_STATUS_PORT_INVALID_WWPN, &port->status)) {
24601da177e4SLinus Torvalds 				ZFCP_LOG_DEBUG("nameserver look-up failed "
24611da177e4SLinus Torvalds 					       "for port 0x%016Lx "
24621da177e4SLinus Torvalds 					       "(misconfigured WWPN?)\n",
24631da177e4SLinus Torvalds 					       port->wwpn);
24641da177e4SLinus Torvalds 				zfcp_erp_port_failed(port);
24651da177e4SLinus Torvalds 				retval = ZFCP_ERP_EXIT;
24661da177e4SLinus Torvalds 			} else {
24671da177e4SLinus Torvalds 				ZFCP_LOG_DEBUG("nameserver look-up failed for "
24681da177e4SLinus Torvalds 					       "port 0x%016Lx\n", port->wwpn);
24691da177e4SLinus Torvalds 				retval = ZFCP_ERP_FAILED;
24701da177e4SLinus Torvalds 			}
24711da177e4SLinus Torvalds 		} else {
24721d589edfSChristof Schmitt 			ZFCP_LOG_DEBUG("port 0x%016Lx has d_id=0x%06x -> "
24731da177e4SLinus Torvalds 				       "trying open\n", port->wwpn, port->d_id);
24741da177e4SLinus Torvalds 			retval = zfcp_erp_port_strategy_open_port(erp_action);
24751da177e4SLinus Torvalds 		}
24761da177e4SLinus Torvalds 		break;
24771da177e4SLinus Torvalds 
24781da177e4SLinus Torvalds 	case ZFCP_ERP_STEP_PORT_OPENING:
24791da177e4SLinus Torvalds 		/* D_ID might have changed during open */
24801da177e4SLinus Torvalds 		if (atomic_test_mask((ZFCP_STATUS_COMMON_OPEN |
24811da177e4SLinus Torvalds 				      ZFCP_STATUS_PORT_DID_DID),
24821da177e4SLinus Torvalds 				     &port->status)) {
24831da177e4SLinus Torvalds 			ZFCP_LOG_DEBUG("port 0x%016Lx is open\n", port->wwpn);
24841da177e4SLinus Torvalds 			retval = ZFCP_ERP_SUCCEEDED;
24851da177e4SLinus Torvalds 		} else {
24861da177e4SLinus Torvalds 			ZFCP_LOG_DEBUG("open failed for port 0x%016Lx\n",
24871da177e4SLinus Torvalds 				       port->wwpn);
24881da177e4SLinus Torvalds 			retval = ZFCP_ERP_FAILED;
24891da177e4SLinus Torvalds 		}
24901da177e4SLinus Torvalds 		break;
24911da177e4SLinus Torvalds 
24921da177e4SLinus Torvalds 	default:
24931da177e4SLinus Torvalds 		ZFCP_LOG_NORMAL("bug: unknown erp step 0x%08x\n",
24941da177e4SLinus Torvalds 				erp_action->step);
24951da177e4SLinus Torvalds 		retval = ZFCP_ERP_FAILED;
24961da177e4SLinus Torvalds 	}
24971da177e4SLinus Torvalds 
24981da177e4SLinus Torvalds 	return retval;
24991da177e4SLinus Torvalds }
25001da177e4SLinus Torvalds 
25011da177e4SLinus Torvalds static int
25021da177e4SLinus Torvalds zfcp_erp_port_strategy_open_nameserver(struct zfcp_erp_action *erp_action)
25031da177e4SLinus Torvalds {
25041da177e4SLinus Torvalds 	int retval;
25051da177e4SLinus Torvalds 	struct zfcp_port *port = erp_action->port;
25061da177e4SLinus Torvalds 
25071da177e4SLinus Torvalds 	switch (erp_action->step) {
25081da177e4SLinus Torvalds 
25091da177e4SLinus Torvalds 	case ZFCP_ERP_STEP_UNINITIALIZED:
25101da177e4SLinus Torvalds 	case ZFCP_ERP_STEP_PHYS_PORT_CLOSING:
25111da177e4SLinus Torvalds 	case ZFCP_ERP_STEP_PORT_CLOSING:
25121d589edfSChristof Schmitt 		ZFCP_LOG_DEBUG("port 0x%016Lx has d_id=0x%06x -> trying open\n",
25131da177e4SLinus Torvalds 			       port->wwpn, port->d_id);
25141da177e4SLinus Torvalds 		retval = zfcp_erp_port_strategy_open_port(erp_action);
25151da177e4SLinus Torvalds 		break;
25161da177e4SLinus Torvalds 
25171da177e4SLinus Torvalds 	case ZFCP_ERP_STEP_PORT_OPENING:
25181da177e4SLinus Torvalds 		if (atomic_test_mask(ZFCP_STATUS_COMMON_OPEN, &port->status)) {
25191da177e4SLinus Torvalds 			ZFCP_LOG_DEBUG("WKA port is open\n");
25201da177e4SLinus Torvalds 			retval = ZFCP_ERP_SUCCEEDED;
25211da177e4SLinus Torvalds 		} else {
25221da177e4SLinus Torvalds 			ZFCP_LOG_DEBUG("open failed for WKA port\n");
25231da177e4SLinus Torvalds 			retval = ZFCP_ERP_FAILED;
25241da177e4SLinus Torvalds 		}
25251da177e4SLinus Torvalds 		/* this is needed anyway (dont care for retval of wakeup) */
25261da177e4SLinus Torvalds 		ZFCP_LOG_DEBUG("continue other open port operations\n");
25271da177e4SLinus Torvalds 		zfcp_erp_port_strategy_open_nameserver_wakeup(erp_action);
25281da177e4SLinus Torvalds 		break;
25291da177e4SLinus Torvalds 
25301da177e4SLinus Torvalds 	default:
25311da177e4SLinus Torvalds 		ZFCP_LOG_NORMAL("bug: unknown erp step 0x%08x\n",
25321da177e4SLinus Torvalds 				erp_action->step);
25331da177e4SLinus Torvalds 		retval = ZFCP_ERP_FAILED;
25341da177e4SLinus Torvalds 	}
25351da177e4SLinus Torvalds 
25361da177e4SLinus Torvalds 	return retval;
25371da177e4SLinus Torvalds }
25381da177e4SLinus Torvalds 
25391da177e4SLinus Torvalds /*
25401da177e4SLinus Torvalds  * function:
25411da177e4SLinus Torvalds  *
25421da177e4SLinus Torvalds  * purpose:	makes the erp thread continue with reopen (physical) port
25431da177e4SLinus Torvalds  *		actions which have been paused until the name server port
25441da177e4SLinus Torvalds  *		is opened (or failed)
25451da177e4SLinus Torvalds  *
25461da177e4SLinus Torvalds  * returns:	0	(a kind of void retval, its not used)
25471da177e4SLinus Torvalds  */
25481da177e4SLinus Torvalds static int
25491da177e4SLinus Torvalds zfcp_erp_port_strategy_open_nameserver_wakeup(struct zfcp_erp_action
25501da177e4SLinus Torvalds 					      *ns_erp_action)
25511da177e4SLinus Torvalds {
25521da177e4SLinus Torvalds 	int retval = 0;
25531da177e4SLinus Torvalds 	unsigned long flags;
25541da177e4SLinus Torvalds 	struct zfcp_adapter *adapter = ns_erp_action->adapter;
25551da177e4SLinus Torvalds 	struct zfcp_erp_action *erp_action, *tmp;
25561da177e4SLinus Torvalds 
25571da177e4SLinus Torvalds 	read_lock_irqsave(&adapter->erp_lock, flags);
25581da177e4SLinus Torvalds 	list_for_each_entry_safe(erp_action, tmp, &adapter->erp_running_head,
25591da177e4SLinus Torvalds 				 list) {
25601da177e4SLinus Torvalds 		debug_text_event(adapter->erp_dbf, 4, "p_pstnsw_n");
25611da177e4SLinus Torvalds 		debug_event(adapter->erp_dbf, 4, &erp_action->port->wwpn,
25621da177e4SLinus Torvalds 			    sizeof (wwn_t));
25631da177e4SLinus Torvalds 		if (erp_action->step == ZFCP_ERP_STEP_NAMESERVER_OPEN) {
25641da177e4SLinus Torvalds 			debug_text_event(adapter->erp_dbf, 3, "p_pstnsw_w");
25651da177e4SLinus Torvalds 			debug_event(adapter->erp_dbf, 3,
25661da177e4SLinus Torvalds 				    &erp_action->port->wwpn, sizeof (wwn_t));
25671da177e4SLinus Torvalds 			if (atomic_test_mask(
25681da177e4SLinus Torvalds 				    ZFCP_STATUS_COMMON_ERP_FAILED,
25691da177e4SLinus Torvalds 				    &adapter->nameserver_port->status))
25701da177e4SLinus Torvalds 				zfcp_erp_port_failed(erp_action->port);
25711da177e4SLinus Torvalds 			zfcp_erp_action_ready(erp_action);
25721da177e4SLinus Torvalds 		}
25731da177e4SLinus Torvalds 	}
25741da177e4SLinus Torvalds 	read_unlock_irqrestore(&adapter->erp_lock, flags);
25751da177e4SLinus Torvalds 
25761da177e4SLinus Torvalds 	return retval;
25771da177e4SLinus Torvalds }
25781da177e4SLinus Torvalds 
25791da177e4SLinus Torvalds /*
25801da177e4SLinus Torvalds  * function:
25811da177e4SLinus Torvalds  *
25821da177e4SLinus Torvalds  * purpose:
25831da177e4SLinus Torvalds  *
25841da177e4SLinus Torvalds  * returns:	ZFCP_ERP_CONTINUES	- action continues (asynchronously)
25851da177e4SLinus Torvalds  *		ZFCP_ERP_FAILED		- action finished unsuccessfully
25861da177e4SLinus Torvalds  */
25871da177e4SLinus Torvalds static int
25881da177e4SLinus Torvalds zfcp_erp_port_forced_strategy_close(struct zfcp_erp_action *erp_action)
25891da177e4SLinus Torvalds {
25901da177e4SLinus Torvalds 	int retval;
25911da177e4SLinus Torvalds 	struct zfcp_adapter *adapter = erp_action->adapter;
25921da177e4SLinus Torvalds 	struct zfcp_port *port = erp_action->port;
25931da177e4SLinus Torvalds 
25941da177e4SLinus Torvalds 	retval = zfcp_fsf_close_physical_port(erp_action);
25951da177e4SLinus Torvalds 	if (retval == -ENOMEM) {
25961da177e4SLinus Torvalds 		debug_text_event(adapter->erp_dbf, 5, "o_pfstc_nomem");
25971da177e4SLinus Torvalds 		debug_event(adapter->erp_dbf, 5, &port->wwpn, sizeof (wwn_t));
25981da177e4SLinus Torvalds 		retval = ZFCP_ERP_NOMEM;
25991da177e4SLinus Torvalds 		goto out;
26001da177e4SLinus Torvalds 	}
26011da177e4SLinus Torvalds 	erp_action->step = ZFCP_ERP_STEP_PHYS_PORT_CLOSING;
26021da177e4SLinus Torvalds 	if (retval != 0) {
26031da177e4SLinus Torvalds 		debug_text_event(adapter->erp_dbf, 5, "o_pfstc_cpf");
26041da177e4SLinus Torvalds 		debug_event(adapter->erp_dbf, 5, &port->wwpn, sizeof (wwn_t));
26051da177e4SLinus Torvalds 		/* could not send 'open', fail */
26061da177e4SLinus Torvalds 		retval = ZFCP_ERP_FAILED;
26071da177e4SLinus Torvalds 		goto out;
26081da177e4SLinus Torvalds 	}
26091da177e4SLinus Torvalds 	debug_text_event(adapter->erp_dbf, 6, "o_pfstc_cpok");
26101da177e4SLinus Torvalds 	debug_event(adapter->erp_dbf, 6, &port->wwpn, sizeof (wwn_t));
26111da177e4SLinus Torvalds 	retval = ZFCP_ERP_CONTINUES;
26121da177e4SLinus Torvalds  out:
26131da177e4SLinus Torvalds 	return retval;
26141da177e4SLinus Torvalds }
26151da177e4SLinus Torvalds 
26161da177e4SLinus Torvalds static int
26171da177e4SLinus Torvalds zfcp_erp_port_strategy_clearstati(struct zfcp_port *port)
26181da177e4SLinus Torvalds {
26191da177e4SLinus Torvalds 	int retval = 0;
26201da177e4SLinus Torvalds 	struct zfcp_adapter *adapter = port->adapter;
26211da177e4SLinus Torvalds 
26221da177e4SLinus Torvalds 	debug_text_event(adapter->erp_dbf, 5, "p_pstclst");
26231da177e4SLinus Torvalds 	debug_event(adapter->erp_dbf, 5, &port->wwpn, sizeof (wwn_t));
26241da177e4SLinus Torvalds 
26251da177e4SLinus Torvalds 	atomic_clear_mask(ZFCP_STATUS_COMMON_OPENING |
26261da177e4SLinus Torvalds 			  ZFCP_STATUS_COMMON_CLOSING |
26271da177e4SLinus Torvalds 			  ZFCP_STATUS_COMMON_ACCESS_DENIED |
26281da177e4SLinus Torvalds 			  ZFCP_STATUS_PORT_DID_DID |
26291da177e4SLinus Torvalds 			  ZFCP_STATUS_PORT_PHYS_CLOSING |
26301da177e4SLinus Torvalds 			  ZFCP_STATUS_PORT_INVALID_WWPN,
26311da177e4SLinus Torvalds 			  &port->status);
26321da177e4SLinus Torvalds 	return retval;
26331da177e4SLinus Torvalds }
26341da177e4SLinus Torvalds 
26351da177e4SLinus Torvalds /*
26361da177e4SLinus Torvalds  * function:
26371da177e4SLinus Torvalds  *
26381da177e4SLinus Torvalds  * purpose:
26391da177e4SLinus Torvalds  *
26401da177e4SLinus Torvalds  * returns:	ZFCP_ERP_CONTINUES	- action continues (asynchronously)
26411da177e4SLinus Torvalds  *		ZFCP_ERP_FAILED		- action finished unsuccessfully
26421da177e4SLinus Torvalds  */
26431da177e4SLinus Torvalds static int
26441da177e4SLinus Torvalds zfcp_erp_port_strategy_close(struct zfcp_erp_action *erp_action)
26451da177e4SLinus Torvalds {
26461da177e4SLinus Torvalds 	int retval;
26471da177e4SLinus Torvalds 	struct zfcp_adapter *adapter = erp_action->adapter;
26481da177e4SLinus Torvalds 	struct zfcp_port *port = erp_action->port;
26491da177e4SLinus Torvalds 
26501da177e4SLinus Torvalds 	retval = zfcp_fsf_close_port(erp_action);
26511da177e4SLinus Torvalds 	if (retval == -ENOMEM) {
26521da177e4SLinus Torvalds 		debug_text_event(adapter->erp_dbf, 5, "p_pstc_nomem");
26531da177e4SLinus Torvalds 		debug_event(adapter->erp_dbf, 5, &port->wwpn, sizeof (wwn_t));
26541da177e4SLinus Torvalds 		retval = ZFCP_ERP_NOMEM;
26551da177e4SLinus Torvalds 		goto out;
26561da177e4SLinus Torvalds 	}
26571da177e4SLinus Torvalds 	erp_action->step = ZFCP_ERP_STEP_PORT_CLOSING;
26581da177e4SLinus Torvalds 	if (retval != 0) {
26591da177e4SLinus Torvalds 		debug_text_event(adapter->erp_dbf, 5, "p_pstc_cpf");
26601da177e4SLinus Torvalds 		debug_event(adapter->erp_dbf, 5, &port->wwpn, sizeof (wwn_t));
26611da177e4SLinus Torvalds 		/* could not send 'close', fail */
26621da177e4SLinus Torvalds 		retval = ZFCP_ERP_FAILED;
26631da177e4SLinus Torvalds 		goto out;
26641da177e4SLinus Torvalds 	}
26651da177e4SLinus Torvalds 	debug_text_event(adapter->erp_dbf, 6, "p_pstc_cpok");
26661da177e4SLinus Torvalds 	debug_event(adapter->erp_dbf, 6, &port->wwpn, sizeof (wwn_t));
26671da177e4SLinus Torvalds 	retval = ZFCP_ERP_CONTINUES;
26681da177e4SLinus Torvalds  out:
26691da177e4SLinus Torvalds 	return retval;
26701da177e4SLinus Torvalds }
26711da177e4SLinus Torvalds 
26721da177e4SLinus Torvalds /*
26731da177e4SLinus Torvalds  * function:
26741da177e4SLinus Torvalds  *
26751da177e4SLinus Torvalds  * purpose:
26761da177e4SLinus Torvalds  *
26771da177e4SLinus Torvalds  * returns:	ZFCP_ERP_CONTINUES	- action continues (asynchronously)
26781da177e4SLinus Torvalds  *		ZFCP_ERP_FAILED		- action finished unsuccessfully
26791da177e4SLinus Torvalds  */
26801da177e4SLinus Torvalds static int
26811da177e4SLinus Torvalds zfcp_erp_port_strategy_open_port(struct zfcp_erp_action *erp_action)
26821da177e4SLinus Torvalds {
26831da177e4SLinus Torvalds 	int retval;
26841da177e4SLinus Torvalds 	struct zfcp_adapter *adapter = erp_action->adapter;
26851da177e4SLinus Torvalds 	struct zfcp_port *port = erp_action->port;
26861da177e4SLinus Torvalds 
26871da177e4SLinus Torvalds 	retval = zfcp_fsf_open_port(erp_action);
26881da177e4SLinus Torvalds 	if (retval == -ENOMEM) {
26891da177e4SLinus Torvalds 		debug_text_event(adapter->erp_dbf, 5, "p_psto_nomem");
26901da177e4SLinus Torvalds 		debug_event(adapter->erp_dbf, 5, &port->wwpn, sizeof (wwn_t));
26911da177e4SLinus Torvalds 		retval = ZFCP_ERP_NOMEM;
26921da177e4SLinus Torvalds 		goto out;
26931da177e4SLinus Torvalds 	}
26941da177e4SLinus Torvalds 	erp_action->step = ZFCP_ERP_STEP_PORT_OPENING;
26951da177e4SLinus Torvalds 	if (retval != 0) {
26961da177e4SLinus Torvalds 		debug_text_event(adapter->erp_dbf, 5, "p_psto_opf");
26971da177e4SLinus Torvalds 		debug_event(adapter->erp_dbf, 5, &port->wwpn, sizeof (wwn_t));
26981da177e4SLinus Torvalds 		/* could not send 'open', fail */
26991da177e4SLinus Torvalds 		retval = ZFCP_ERP_FAILED;
27001da177e4SLinus Torvalds 		goto out;
27011da177e4SLinus Torvalds 	}
27021da177e4SLinus Torvalds 	debug_text_event(adapter->erp_dbf, 6, "p_psto_opok");
27031da177e4SLinus Torvalds 	debug_event(adapter->erp_dbf, 6, &port->wwpn, sizeof (wwn_t));
27041da177e4SLinus Torvalds 	retval = ZFCP_ERP_CONTINUES;
27051da177e4SLinus Torvalds  out:
27061da177e4SLinus Torvalds 	return retval;
27071da177e4SLinus Torvalds }
27081da177e4SLinus Torvalds 
27091da177e4SLinus Torvalds /*
27101da177e4SLinus Torvalds  * function:
27111da177e4SLinus Torvalds  *
27121da177e4SLinus Torvalds  * purpose:
27131da177e4SLinus Torvalds  *
27141da177e4SLinus Torvalds  * returns:	ZFCP_ERP_CONTINUES	- action continues (asynchronously)
27151da177e4SLinus Torvalds  *		ZFCP_ERP_FAILED		- action finished unsuccessfully
27161da177e4SLinus Torvalds  */
27171da177e4SLinus Torvalds static int
27181da177e4SLinus Torvalds zfcp_erp_port_strategy_open_common_lookup(struct zfcp_erp_action *erp_action)
27191da177e4SLinus Torvalds {
27201da177e4SLinus Torvalds 	int retval;
27211da177e4SLinus Torvalds 	struct zfcp_adapter *adapter = erp_action->adapter;
27221da177e4SLinus Torvalds 	struct zfcp_port *port = erp_action->port;
27231da177e4SLinus Torvalds 
27241da177e4SLinus Torvalds 	retval = zfcp_ns_gid_pn_request(erp_action);
27251da177e4SLinus Torvalds 	if (retval == -ENOMEM) {
27261da177e4SLinus Torvalds 		debug_text_event(adapter->erp_dbf, 5, "p_pstn_nomem");
27271da177e4SLinus Torvalds 		debug_event(adapter->erp_dbf, 5, &port->wwpn, sizeof (wwn_t));
27281da177e4SLinus Torvalds 		retval = ZFCP_ERP_NOMEM;
27291da177e4SLinus Torvalds 		goto out;
27301da177e4SLinus Torvalds 	}
27311da177e4SLinus Torvalds 	erp_action->step = ZFCP_ERP_STEP_NAMESERVER_LOOKUP;
27321da177e4SLinus Torvalds 	if (retval != 0) {
27331da177e4SLinus Torvalds 		debug_text_event(adapter->erp_dbf, 5, "p_pstn_ref");
27341da177e4SLinus Torvalds 		debug_event(adapter->erp_dbf, 5, &port->wwpn, sizeof (wwn_t));
27351da177e4SLinus Torvalds 		/* could not send nameserver request, fail */
27361da177e4SLinus Torvalds 		retval = ZFCP_ERP_FAILED;
27371da177e4SLinus Torvalds 		goto out;
27381da177e4SLinus Torvalds 	}
27391da177e4SLinus Torvalds 	debug_text_event(adapter->erp_dbf, 6, "p_pstn_reok");
27401da177e4SLinus Torvalds 	debug_event(adapter->erp_dbf, 6, &port->wwpn, sizeof (wwn_t));
27411da177e4SLinus Torvalds 	retval = ZFCP_ERP_CONTINUES;
27421da177e4SLinus Torvalds  out:
27431da177e4SLinus Torvalds 	return retval;
27441da177e4SLinus Torvalds }
27451da177e4SLinus Torvalds 
27461da177e4SLinus Torvalds /*
27471da177e4SLinus Torvalds  * function:
27481da177e4SLinus Torvalds  *
27491da177e4SLinus Torvalds  * purpose:	this routine executes the 'Reopen Unit' action
27501da177e4SLinus Torvalds  *		currently no retries
27511da177e4SLinus Torvalds  *
27521da177e4SLinus Torvalds  * returns:	ZFCP_ERP_CONTINUES	- action continues (asynchronously)
27531da177e4SLinus Torvalds  *		ZFCP_ERP_SUCCEEDED	- action finished successfully
27541da177e4SLinus Torvalds  *		ZFCP_ERP_FAILED		- action finished unsuccessfully
27551da177e4SLinus Torvalds  */
27561da177e4SLinus Torvalds static int
27571da177e4SLinus Torvalds zfcp_erp_unit_strategy(struct zfcp_erp_action *erp_action)
27581da177e4SLinus Torvalds {
27591da177e4SLinus Torvalds 	int retval = ZFCP_ERP_FAILED;
27601da177e4SLinus Torvalds 	struct zfcp_unit *unit = erp_action->unit;
27611da177e4SLinus Torvalds 	struct zfcp_adapter *adapter = erp_action->adapter;
27621da177e4SLinus Torvalds 
27631da177e4SLinus Torvalds 	switch (erp_action->step) {
27641da177e4SLinus Torvalds 
27651da177e4SLinus Torvalds 		/*
27661da177e4SLinus Torvalds 		 * FIXME:
27671da177e4SLinus Torvalds 		 * the ULP spec. begs for waiting for oustanding commands
27681da177e4SLinus Torvalds 		 */
27691da177e4SLinus Torvalds 	case ZFCP_ERP_STEP_UNINITIALIZED:
27701da177e4SLinus Torvalds 		zfcp_erp_unit_strategy_clearstati(unit);
27711da177e4SLinus Torvalds 		if (atomic_test_mask(ZFCP_STATUS_COMMON_OPEN, &unit->status)) {
27721da177e4SLinus Torvalds 			ZFCP_LOG_DEBUG("unit 0x%016Lx is open -> "
27731da177e4SLinus Torvalds 				       "trying close\n", unit->fcp_lun);
27741da177e4SLinus Torvalds 			retval = zfcp_erp_unit_strategy_close(erp_action);
27751da177e4SLinus Torvalds 			break;
27761da177e4SLinus Torvalds 		}
27771da177e4SLinus Torvalds 		/* else it's already closed, fall through */
27781da177e4SLinus Torvalds 	case ZFCP_ERP_STEP_UNIT_CLOSING:
27791da177e4SLinus Torvalds 		if (atomic_test_mask(ZFCP_STATUS_COMMON_OPEN, &unit->status)) {
27801da177e4SLinus Torvalds 			ZFCP_LOG_DEBUG("close failed for unit 0x%016Lx\n",
27811da177e4SLinus Torvalds 				       unit->fcp_lun);
27821da177e4SLinus Torvalds 			retval = ZFCP_ERP_FAILED;
27831da177e4SLinus Torvalds 		} else {
27841da177e4SLinus Torvalds 			if (erp_action->status & ZFCP_STATUS_ERP_CLOSE_ONLY)
27851da177e4SLinus Torvalds 				retval = ZFCP_ERP_EXIT;
27861da177e4SLinus Torvalds 			else {
27871da177e4SLinus Torvalds 				ZFCP_LOG_DEBUG("unit 0x%016Lx is not open -> "
27881da177e4SLinus Torvalds 					       "trying open\n", unit->fcp_lun);
27891da177e4SLinus Torvalds 				retval =
27901da177e4SLinus Torvalds 				    zfcp_erp_unit_strategy_open(erp_action);
27911da177e4SLinus Torvalds 			}
27921da177e4SLinus Torvalds 		}
27931da177e4SLinus Torvalds 		break;
27941da177e4SLinus Torvalds 
27951da177e4SLinus Torvalds 	case ZFCP_ERP_STEP_UNIT_OPENING:
27961da177e4SLinus Torvalds 		if (atomic_test_mask(ZFCP_STATUS_COMMON_OPEN, &unit->status)) {
27971da177e4SLinus Torvalds 			ZFCP_LOG_DEBUG("unit 0x%016Lx is open\n",
27981da177e4SLinus Torvalds 				       unit->fcp_lun);
27991da177e4SLinus Torvalds 			retval = ZFCP_ERP_SUCCEEDED;
28001da177e4SLinus Torvalds 		} else {
28011da177e4SLinus Torvalds 			ZFCP_LOG_DEBUG("open failed for unit 0x%016Lx\n",
28021da177e4SLinus Torvalds 				       unit->fcp_lun);
28031da177e4SLinus Torvalds 			retval = ZFCP_ERP_FAILED;
28041da177e4SLinus Torvalds 		}
28051da177e4SLinus Torvalds 		break;
28061da177e4SLinus Torvalds 	}
28071da177e4SLinus Torvalds 
28081da177e4SLinus Torvalds 	debug_text_event(adapter->erp_dbf, 3, "u_ust/ret");
28091da177e4SLinus Torvalds 	debug_event(adapter->erp_dbf, 3, &unit->fcp_lun, sizeof (fcp_lun_t));
28101da177e4SLinus Torvalds 	debug_event(adapter->erp_dbf, 3, &erp_action->action, sizeof (int));
28111da177e4SLinus Torvalds 	debug_event(adapter->erp_dbf, 3, &retval, sizeof (int));
28121da177e4SLinus Torvalds 	return retval;
28131da177e4SLinus Torvalds }
28141da177e4SLinus Torvalds 
28151da177e4SLinus Torvalds static int
28161da177e4SLinus Torvalds zfcp_erp_unit_strategy_clearstati(struct zfcp_unit *unit)
28171da177e4SLinus Torvalds {
28181da177e4SLinus Torvalds 	int retval = 0;
28191da177e4SLinus Torvalds 	struct zfcp_adapter *adapter = unit->port->adapter;
28201da177e4SLinus Torvalds 
28211da177e4SLinus Torvalds 	debug_text_event(adapter->erp_dbf, 5, "u_ustclst");
28221da177e4SLinus Torvalds 	debug_event(adapter->erp_dbf, 5, &unit->fcp_lun, sizeof (fcp_lun_t));
28231da177e4SLinus Torvalds 
28241da177e4SLinus Torvalds 	atomic_clear_mask(ZFCP_STATUS_COMMON_OPENING |
28251da177e4SLinus Torvalds 			  ZFCP_STATUS_COMMON_CLOSING |
28261da177e4SLinus Torvalds 			  ZFCP_STATUS_COMMON_ACCESS_DENIED |
28271da177e4SLinus Torvalds 			  ZFCP_STATUS_UNIT_SHARED |
28281da177e4SLinus Torvalds 			  ZFCP_STATUS_UNIT_READONLY,
28291da177e4SLinus Torvalds 			  &unit->status);
28301da177e4SLinus Torvalds 
28311da177e4SLinus Torvalds 	return retval;
28321da177e4SLinus Torvalds }
28331da177e4SLinus Torvalds 
28341da177e4SLinus Torvalds /*
28351da177e4SLinus Torvalds  * function:
28361da177e4SLinus Torvalds  *
28371da177e4SLinus Torvalds  * purpose:
28381da177e4SLinus Torvalds  *
28391da177e4SLinus Torvalds  * returns:	ZFCP_ERP_CONTINUES	- action continues (asynchronously)
28401da177e4SLinus Torvalds  *		ZFCP_ERP_FAILED		- action finished unsuccessfully
28411da177e4SLinus Torvalds  */
28421da177e4SLinus Torvalds static int
28431da177e4SLinus Torvalds zfcp_erp_unit_strategy_close(struct zfcp_erp_action *erp_action)
28441da177e4SLinus Torvalds {
28451da177e4SLinus Torvalds 	int retval;
28461da177e4SLinus Torvalds 	struct zfcp_adapter *adapter = erp_action->adapter;
28471da177e4SLinus Torvalds 	struct zfcp_unit *unit = erp_action->unit;
28481da177e4SLinus Torvalds 
28491da177e4SLinus Torvalds 	retval = zfcp_fsf_close_unit(erp_action);
28501da177e4SLinus Torvalds 	if (retval == -ENOMEM) {
28511da177e4SLinus Torvalds 		debug_text_event(adapter->erp_dbf, 5, "u_ustc_nomem");
28521da177e4SLinus Torvalds 		debug_event(adapter->erp_dbf, 5, &unit->fcp_lun,
28531da177e4SLinus Torvalds 			    sizeof (fcp_lun_t));
28541da177e4SLinus Torvalds 		retval = ZFCP_ERP_NOMEM;
28551da177e4SLinus Torvalds 		goto out;
28561da177e4SLinus Torvalds 	}
28571da177e4SLinus Torvalds 	erp_action->step = ZFCP_ERP_STEP_UNIT_CLOSING;
28581da177e4SLinus Torvalds 	if (retval != 0) {
28591da177e4SLinus Torvalds 		debug_text_event(adapter->erp_dbf, 5, "u_ustc_cuf");
28601da177e4SLinus Torvalds 		debug_event(adapter->erp_dbf, 5, &unit->fcp_lun,
28611da177e4SLinus Torvalds 			    sizeof (fcp_lun_t));
28621da177e4SLinus Torvalds 		/* could not send 'close', fail */
28631da177e4SLinus Torvalds 		retval = ZFCP_ERP_FAILED;
28641da177e4SLinus Torvalds 		goto out;
28651da177e4SLinus Torvalds 	}
28661da177e4SLinus Torvalds 	debug_text_event(adapter->erp_dbf, 6, "u_ustc_cuok");
28671da177e4SLinus Torvalds 	debug_event(adapter->erp_dbf, 6, &unit->fcp_lun, sizeof (fcp_lun_t));
28681da177e4SLinus Torvalds 	retval = ZFCP_ERP_CONTINUES;
28691da177e4SLinus Torvalds 
28701da177e4SLinus Torvalds  out:
28711da177e4SLinus Torvalds 	return retval;
28721da177e4SLinus Torvalds }
28731da177e4SLinus Torvalds 
28741da177e4SLinus Torvalds /*
28751da177e4SLinus Torvalds  * function:
28761da177e4SLinus Torvalds  *
28771da177e4SLinus Torvalds  * purpose:
28781da177e4SLinus Torvalds  *
28791da177e4SLinus Torvalds  * returns:	ZFCP_ERP_CONTINUES	- action continues (asynchronously)
28801da177e4SLinus Torvalds  *		ZFCP_ERP_FAILED		- action finished unsuccessfully
28811da177e4SLinus Torvalds  */
28821da177e4SLinus Torvalds static int
28831da177e4SLinus Torvalds zfcp_erp_unit_strategy_open(struct zfcp_erp_action *erp_action)
28841da177e4SLinus Torvalds {
28851da177e4SLinus Torvalds 	int retval;
28861da177e4SLinus Torvalds 	struct zfcp_adapter *adapter = erp_action->adapter;
28871da177e4SLinus Torvalds 	struct zfcp_unit *unit = erp_action->unit;
28881da177e4SLinus Torvalds 
28891da177e4SLinus Torvalds 	retval = zfcp_fsf_open_unit(erp_action);
28901da177e4SLinus Torvalds 	if (retval == -ENOMEM) {
28911da177e4SLinus Torvalds 		debug_text_event(adapter->erp_dbf, 5, "u_usto_nomem");
28921da177e4SLinus Torvalds 		debug_event(adapter->erp_dbf, 5, &unit->fcp_lun,
28931da177e4SLinus Torvalds 			    sizeof (fcp_lun_t));
28941da177e4SLinus Torvalds 		retval = ZFCP_ERP_NOMEM;
28951da177e4SLinus Torvalds 		goto out;
28961da177e4SLinus Torvalds 	}
28971da177e4SLinus Torvalds 	erp_action->step = ZFCP_ERP_STEP_UNIT_OPENING;
28981da177e4SLinus Torvalds 	if (retval != 0) {
28991da177e4SLinus Torvalds 		debug_text_event(adapter->erp_dbf, 5, "u_usto_ouf");
29001da177e4SLinus Torvalds 		debug_event(adapter->erp_dbf, 5, &unit->fcp_lun,
29011da177e4SLinus Torvalds 			    sizeof (fcp_lun_t));
29021da177e4SLinus Torvalds 		/* could not send 'open', fail */
29031da177e4SLinus Torvalds 		retval = ZFCP_ERP_FAILED;
29041da177e4SLinus Torvalds 		goto out;
29051da177e4SLinus Torvalds 	}
29061da177e4SLinus Torvalds 	debug_text_event(adapter->erp_dbf, 6, "u_usto_ouok");
29071da177e4SLinus Torvalds 	debug_event(adapter->erp_dbf, 6, &unit->fcp_lun, sizeof (fcp_lun_t));
29081da177e4SLinus Torvalds 	retval = ZFCP_ERP_CONTINUES;
29091da177e4SLinus Torvalds  out:
29101da177e4SLinus Torvalds 	return retval;
29111da177e4SLinus Torvalds }
29121da177e4SLinus Torvalds 
29132abbe866SAndreas Herrmann void zfcp_erp_start_timer(struct zfcp_fsf_req *fsf_req)
29141da177e4SLinus Torvalds {
29152abbe866SAndreas Herrmann 	BUG_ON(!fsf_req->erp_action);
29162abbe866SAndreas Herrmann 	fsf_req->timer.function = zfcp_erp_timeout_handler;
29172abbe866SAndreas Herrmann 	fsf_req->timer.data = (unsigned long) fsf_req->erp_action;
29182abbe866SAndreas Herrmann 	fsf_req->timer.expires = jiffies + ZFCP_ERP_FSFREQ_TIMEOUT;
29192abbe866SAndreas Herrmann 	add_timer(&fsf_req->timer);
29201da177e4SLinus Torvalds }
29211da177e4SLinus Torvalds 
29221da177e4SLinus Torvalds /*
29231da177e4SLinus Torvalds  * function:
29241da177e4SLinus Torvalds  *
29251da177e4SLinus Torvalds  * purpose:	enqueue the specified error recovery action, if needed
29261da177e4SLinus Torvalds  *
29271da177e4SLinus Torvalds  * returns:
29281da177e4SLinus Torvalds  */
29291da177e4SLinus Torvalds static int
29301da177e4SLinus Torvalds zfcp_erp_action_enqueue(int action,
29311da177e4SLinus Torvalds 			struct zfcp_adapter *adapter,
29321da177e4SLinus Torvalds 			struct zfcp_port *port, struct zfcp_unit *unit)
29331da177e4SLinus Torvalds {
29341da177e4SLinus Torvalds 	int retval = 1;
29351da177e4SLinus Torvalds 	struct zfcp_erp_action *erp_action = NULL;
29361da177e4SLinus Torvalds 	int stronger_action = 0;
29371da177e4SLinus Torvalds 	u32 status = 0;
29381da177e4SLinus Torvalds 
29391da177e4SLinus Torvalds 	/*
29401da177e4SLinus Torvalds 	 * We need some rules here which check whether we really need
29411da177e4SLinus Torvalds 	 * this action or whether we should just drop it.
29421da177e4SLinus Torvalds 	 * E.g. if there is a unfinished 'Reopen Port' request then we drop a
29431da177e4SLinus Torvalds 	 * 'Reopen Unit' request for an associated unit since we can't
29441da177e4SLinus Torvalds 	 * satisfy this request now. A 'Reopen Port' action will trigger
29451da177e4SLinus Torvalds 	 * 'Reopen Unit' actions when it completes.
29461da177e4SLinus Torvalds 	 * Thus, there are only actions in the queue which can immediately be
29471da177e4SLinus Torvalds 	 * executed. This makes the processing of the action queue more
29481da177e4SLinus Torvalds 	 * efficient.
29491da177e4SLinus Torvalds 	 */
29501da177e4SLinus Torvalds 
29511da177e4SLinus Torvalds 	if (!atomic_test_mask(ZFCP_STATUS_ADAPTER_ERP_THREAD_UP,
29521da177e4SLinus Torvalds 			      &adapter->status))
29531da177e4SLinus Torvalds 		return -EIO;
29541da177e4SLinus Torvalds 
29551da177e4SLinus Torvalds 	debug_event(adapter->erp_dbf, 4, &action, sizeof (int));
29561da177e4SLinus Torvalds 	/* check whether we really need this */
29571da177e4SLinus Torvalds 	switch (action) {
29581da177e4SLinus Torvalds 	case ZFCP_ERP_ACTION_REOPEN_UNIT:
29591da177e4SLinus Torvalds 		if (atomic_test_mask
29601da177e4SLinus Torvalds 		    (ZFCP_STATUS_COMMON_ERP_INUSE, &unit->status)) {
29611da177e4SLinus Torvalds 			debug_text_event(adapter->erp_dbf, 4, "u_actenq_drp");
29621da177e4SLinus Torvalds 			debug_event(adapter->erp_dbf, 4, &port->wwpn,
29631da177e4SLinus Torvalds 				    sizeof (wwn_t));
29641da177e4SLinus Torvalds 			debug_event(adapter->erp_dbf, 4, &unit->fcp_lun,
29651da177e4SLinus Torvalds 				    sizeof (fcp_lun_t));
29661da177e4SLinus Torvalds 			goto out;
29671da177e4SLinus Torvalds 		}
29681da177e4SLinus Torvalds 		if (!atomic_test_mask
29691da177e4SLinus Torvalds 		    (ZFCP_STATUS_COMMON_RUNNING, &port->status) ||
29701da177e4SLinus Torvalds 		    atomic_test_mask
29711da177e4SLinus Torvalds 		    (ZFCP_STATUS_COMMON_ERP_FAILED, &port->status)) {
29721da177e4SLinus Torvalds 			goto out;
29731da177e4SLinus Torvalds 		}
29741da177e4SLinus Torvalds 		if (!atomic_test_mask
29751da177e4SLinus Torvalds 		    (ZFCP_STATUS_COMMON_UNBLOCKED, &port->status)) {
29761da177e4SLinus Torvalds 			stronger_action = ZFCP_ERP_ACTION_REOPEN_PORT;
29771da177e4SLinus Torvalds 			unit = NULL;
29781da177e4SLinus Torvalds 		}
29791da177e4SLinus Torvalds 		/* fall through !!! */
29801da177e4SLinus Torvalds 
29811da177e4SLinus Torvalds 	case ZFCP_ERP_ACTION_REOPEN_PORT:
29821da177e4SLinus Torvalds 		if (atomic_test_mask
29831da177e4SLinus Torvalds 		    (ZFCP_STATUS_COMMON_ERP_INUSE, &port->status)) {
29841da177e4SLinus Torvalds 			debug_text_event(adapter->erp_dbf, 4, "p_actenq_drp");
29851da177e4SLinus Torvalds 			debug_event(adapter->erp_dbf, 4, &port->wwpn,
29861da177e4SLinus Torvalds 				    sizeof (wwn_t));
29871da177e4SLinus Torvalds 			goto out;
29881da177e4SLinus Torvalds 		}
29891da177e4SLinus Torvalds 		/* fall through !!! */
29901da177e4SLinus Torvalds 
29911da177e4SLinus Torvalds 	case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
29923734d24bSAndreas Herrmann 		if (atomic_test_mask(ZFCP_STATUS_COMMON_ERP_INUSE,
29933734d24bSAndreas Herrmann 				     &port->status)) {
29943734d24bSAndreas Herrmann 			if (port->erp_action.action !=
29951da177e4SLinus Torvalds 			    ZFCP_ERP_ACTION_REOPEN_PORT_FORCED) {
29963734d24bSAndreas Herrmann 				ZFCP_LOG_INFO("dropped erp action %i (port "
29973734d24bSAndreas Herrmann 					      "0x%016Lx, action in use: %i)\n",
29983734d24bSAndreas Herrmann 					      action, port->wwpn,
29993734d24bSAndreas Herrmann 					      port->erp_action.action);
30003734d24bSAndreas Herrmann 				debug_text_event(adapter->erp_dbf, 4,
30013734d24bSAndreas Herrmann 						 "pf_actenq_drp");
30023734d24bSAndreas Herrmann 			} else
30033734d24bSAndreas Herrmann 				debug_text_event(adapter->erp_dbf, 4,
30043734d24bSAndreas Herrmann 						 "pf_actenq_drpcp");
30051da177e4SLinus Torvalds 			debug_event(adapter->erp_dbf, 4, &port->wwpn,
30061da177e4SLinus Torvalds 				    sizeof (wwn_t));
30071da177e4SLinus Torvalds 			goto out;
30081da177e4SLinus Torvalds 		}
30091da177e4SLinus Torvalds 		if (!atomic_test_mask
30101da177e4SLinus Torvalds 		    (ZFCP_STATUS_COMMON_RUNNING, &adapter->status) ||
30111da177e4SLinus Torvalds 		    atomic_test_mask
30121da177e4SLinus Torvalds 		    (ZFCP_STATUS_COMMON_ERP_FAILED, &adapter->status)) {
30131da177e4SLinus Torvalds 			goto out;
30141da177e4SLinus Torvalds 		}
30151da177e4SLinus Torvalds 		if (!atomic_test_mask
30161da177e4SLinus Torvalds 		    (ZFCP_STATUS_COMMON_UNBLOCKED, &adapter->status)) {
30171da177e4SLinus Torvalds 			stronger_action = ZFCP_ERP_ACTION_REOPEN_ADAPTER;
30181da177e4SLinus Torvalds 			port = NULL;
30191da177e4SLinus Torvalds 		}
30201da177e4SLinus Torvalds 		/* fall through !!! */
30211da177e4SLinus Torvalds 
30221da177e4SLinus Torvalds 	case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
30231da177e4SLinus Torvalds 		if (atomic_test_mask
30241da177e4SLinus Torvalds 		    (ZFCP_STATUS_COMMON_ERP_INUSE, &adapter->status)) {
30251da177e4SLinus Torvalds 			debug_text_event(adapter->erp_dbf, 4, "a_actenq_drp");
30261da177e4SLinus Torvalds 			goto out;
30271da177e4SLinus Torvalds 		}
30281da177e4SLinus Torvalds 		break;
30291da177e4SLinus Torvalds 
30301da177e4SLinus Torvalds 	default:
30311da177e4SLinus Torvalds 		debug_text_exception(adapter->erp_dbf, 1, "a_actenq_bug");
30321da177e4SLinus Torvalds 		debug_event(adapter->erp_dbf, 1, &action, sizeof (int));
30331da177e4SLinus Torvalds 		ZFCP_LOG_NORMAL("bug: unknown erp action requested "
30341da177e4SLinus Torvalds 				"on adapter %s (action=%d)\n",
30351da177e4SLinus Torvalds 				zfcp_get_busid_by_adapter(adapter), action);
30361da177e4SLinus Torvalds 		goto out;
30371da177e4SLinus Torvalds 	}
30381da177e4SLinus Torvalds 
30391da177e4SLinus Torvalds 	/* check whether we need something stronger first */
30401da177e4SLinus Torvalds 	if (stronger_action) {
30411da177e4SLinus Torvalds 		debug_text_event(adapter->erp_dbf, 4, "a_actenq_str");
30421da177e4SLinus Torvalds 		debug_event(adapter->erp_dbf, 4, &stronger_action,
30431da177e4SLinus Torvalds 			    sizeof (int));
30441da177e4SLinus Torvalds 		ZFCP_LOG_DEBUG("stronger erp action %d needed before "
30451da177e4SLinus Torvalds 			       "erp action %d on adapter %s\n",
30461da177e4SLinus Torvalds 			       stronger_action, action,
30471da177e4SLinus Torvalds 			       zfcp_get_busid_by_adapter(adapter));
30481da177e4SLinus Torvalds 		action = stronger_action;
30491da177e4SLinus Torvalds 	}
30501da177e4SLinus Torvalds 
30511da177e4SLinus Torvalds 	/* mark adapter to have some error recovery pending */
30521da177e4SLinus Torvalds 	atomic_set_mask(ZFCP_STATUS_ADAPTER_ERP_PENDING, &adapter->status);
30531da177e4SLinus Torvalds 
30541da177e4SLinus Torvalds 	/* setup error recovery action */
30551da177e4SLinus Torvalds 	switch (action) {
30561da177e4SLinus Torvalds 
30571da177e4SLinus Torvalds 	case ZFCP_ERP_ACTION_REOPEN_UNIT:
30581da177e4SLinus Torvalds 		zfcp_unit_get(unit);
30591da177e4SLinus Torvalds 		atomic_set_mask(ZFCP_STATUS_COMMON_ERP_INUSE, &unit->status);
30601da177e4SLinus Torvalds 		erp_action = &unit->erp_action;
30611da177e4SLinus Torvalds 		if (!atomic_test_mask
30621da177e4SLinus Torvalds 		    (ZFCP_STATUS_COMMON_RUNNING, &unit->status))
30631da177e4SLinus Torvalds 			status = ZFCP_STATUS_ERP_CLOSE_ONLY;
30641da177e4SLinus Torvalds 		break;
30651da177e4SLinus Torvalds 
30661da177e4SLinus Torvalds 	case ZFCP_ERP_ACTION_REOPEN_PORT:
30671da177e4SLinus Torvalds 	case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
30681da177e4SLinus Torvalds 		zfcp_port_get(port);
30691da177e4SLinus Torvalds 		zfcp_erp_action_dismiss_port(port);
30701da177e4SLinus Torvalds 		atomic_set_mask(ZFCP_STATUS_COMMON_ERP_INUSE, &port->status);
30711da177e4SLinus Torvalds 		erp_action = &port->erp_action;
30721da177e4SLinus Torvalds 		if (!atomic_test_mask
30731da177e4SLinus Torvalds 		    (ZFCP_STATUS_COMMON_RUNNING, &port->status))
30741da177e4SLinus Torvalds 			status = ZFCP_STATUS_ERP_CLOSE_ONLY;
30751da177e4SLinus Torvalds 		break;
30761da177e4SLinus Torvalds 
30771da177e4SLinus Torvalds 	case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
30781da177e4SLinus Torvalds 		zfcp_adapter_get(adapter);
30791da177e4SLinus Torvalds 		zfcp_erp_action_dismiss_adapter(adapter);
30801da177e4SLinus Torvalds 		atomic_set_mask(ZFCP_STATUS_COMMON_ERP_INUSE, &adapter->status);
30811da177e4SLinus Torvalds 		erp_action = &adapter->erp_action;
30821da177e4SLinus Torvalds 		if (!atomic_test_mask
30831da177e4SLinus Torvalds 		    (ZFCP_STATUS_COMMON_RUNNING, &adapter->status))
30841da177e4SLinus Torvalds 			status = ZFCP_STATUS_ERP_CLOSE_ONLY;
30851da177e4SLinus Torvalds 		break;
30861da177e4SLinus Torvalds 	}
30871da177e4SLinus Torvalds 
30881da177e4SLinus Torvalds 	debug_text_event(adapter->erp_dbf, 4, "a_actenq");
30891da177e4SLinus Torvalds 
30901da177e4SLinus Torvalds 	memset(erp_action, 0, sizeof (struct zfcp_erp_action));
30911da177e4SLinus Torvalds 	erp_action->adapter = adapter;
30921da177e4SLinus Torvalds 	erp_action->port = port;
30931da177e4SLinus Torvalds 	erp_action->unit = unit;
30941da177e4SLinus Torvalds 	erp_action->action = action;
30951da177e4SLinus Torvalds 	erp_action->status = status;
30961da177e4SLinus Torvalds 
30971da177e4SLinus Torvalds 	++adapter->erp_total_count;
30981da177e4SLinus Torvalds 
30991da177e4SLinus Torvalds 	/* finally put it into 'ready' queue and kick erp thread */
310086e8dfc5SMartin Peschke 	list_add_tail(&erp_action->list, &adapter->erp_ready_head);
31011da177e4SLinus Torvalds 	up(&adapter->erp_ready_sem);
3102348447e8SMartin Peschke 	zfcp_rec_dbf_event_thread(1, adapter, 0);
31031da177e4SLinus Torvalds 	retval = 0;
31041da177e4SLinus Torvalds  out:
31051da177e4SLinus Torvalds 	return retval;
31061da177e4SLinus Torvalds }
31071da177e4SLinus Torvalds 
31081da177e4SLinus Torvalds static int
31091da177e4SLinus Torvalds zfcp_erp_action_dequeue(struct zfcp_erp_action *erp_action)
31101da177e4SLinus Torvalds {
31111da177e4SLinus Torvalds 	int retval = 0;
31121da177e4SLinus Torvalds 	struct zfcp_adapter *adapter = erp_action->adapter;
31131da177e4SLinus Torvalds 
31141da177e4SLinus Torvalds 	--adapter->erp_total_count;
31151da177e4SLinus Torvalds 	if (erp_action->status & ZFCP_STATUS_ERP_LOWMEM) {
31161da177e4SLinus Torvalds 		--adapter->erp_low_mem_count;
31171da177e4SLinus Torvalds 		erp_action->status &= ~ZFCP_STATUS_ERP_LOWMEM;
31181da177e4SLinus Torvalds 	}
31191da177e4SLinus Torvalds 
31201da177e4SLinus Torvalds 	debug_text_event(adapter->erp_dbf, 4, "a_actdeq");
31211da177e4SLinus Torvalds 	debug_event(adapter->erp_dbf, 4, &erp_action->action, sizeof (int));
31221da177e4SLinus Torvalds 	list_del(&erp_action->list);
31231da177e4SLinus Torvalds 	switch (erp_action->action) {
31241da177e4SLinus Torvalds 	case ZFCP_ERP_ACTION_REOPEN_UNIT:
31251da177e4SLinus Torvalds 		atomic_clear_mask(ZFCP_STATUS_COMMON_ERP_INUSE,
31261da177e4SLinus Torvalds 				  &erp_action->unit->status);
31271da177e4SLinus Torvalds 		break;
31281da177e4SLinus Torvalds 	case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
31291da177e4SLinus Torvalds 	case ZFCP_ERP_ACTION_REOPEN_PORT:
31301da177e4SLinus Torvalds 		atomic_clear_mask(ZFCP_STATUS_COMMON_ERP_INUSE,
31311da177e4SLinus Torvalds 				  &erp_action->port->status);
31321da177e4SLinus Torvalds 		break;
31331da177e4SLinus Torvalds 	case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
31341da177e4SLinus Torvalds 		atomic_clear_mask(ZFCP_STATUS_COMMON_ERP_INUSE,
31351da177e4SLinus Torvalds 				  &erp_action->adapter->status);
31361da177e4SLinus Torvalds 		break;
31371da177e4SLinus Torvalds 	default:
31381da177e4SLinus Torvalds 		/* bug */
31391da177e4SLinus Torvalds 		break;
31401da177e4SLinus Torvalds 	}
31411da177e4SLinus Torvalds 	return retval;
31421da177e4SLinus Torvalds }
31431da177e4SLinus Torvalds 
31441da177e4SLinus Torvalds /**
31451da177e4SLinus Torvalds  * zfcp_erp_action_cleanup
31461da177e4SLinus Torvalds  *
3147575c9687SAdrian Bunk  * Register unit with scsi stack if appropriate and fix reference counts.
31481da177e4SLinus Torvalds  * Note: Temporary units are not registered with scsi stack.
31491da177e4SLinus Torvalds  */
31501da177e4SLinus Torvalds static void
31511da177e4SLinus Torvalds zfcp_erp_action_cleanup(int action, struct zfcp_adapter *adapter,
31521da177e4SLinus Torvalds 			struct zfcp_port *port, struct zfcp_unit *unit,
31531da177e4SLinus Torvalds 			int result)
31541da177e4SLinus Torvalds {
31551da177e4SLinus Torvalds 	switch (action) {
31561da177e4SLinus Torvalds 	case ZFCP_ERP_ACTION_REOPEN_UNIT:
31571da177e4SLinus Torvalds 		if ((result == ZFCP_ERP_SUCCEEDED)
31581da177e4SLinus Torvalds 		    && (!atomic_test_mask(ZFCP_STATUS_UNIT_TEMPORARY,
31591da177e4SLinus Torvalds 					  &unit->status))
31603859f6a2SAndreas Herrmann 		    && !unit->device
3161ad58f7dbSAndreas Herrmann 		    && port->rport) {
3162ad58f7dbSAndreas Herrmann 			atomic_set_mask(ZFCP_STATUS_UNIT_REGISTERED,
3163ad58f7dbSAndreas Herrmann 					&unit->status);
31645f852be9SChristof Schmitt 			if (atomic_test_mask(ZFCP_STATUS_UNIT_SCSI_WORK_PENDING,
31655f852be9SChristof Schmitt 					     &unit->status) == 0)
31665f852be9SChristof Schmitt 				zfcp_erp_schedule_work(unit);
3167ad58f7dbSAndreas Herrmann 		}
31681da177e4SLinus Torvalds 		zfcp_unit_put(unit);
31691da177e4SLinus Torvalds 		break;
31701da177e4SLinus Torvalds 	case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
31711da177e4SLinus Torvalds 	case ZFCP_ERP_ACTION_REOPEN_PORT:
3172338151e0SAndreas Herrmann 		if (atomic_test_mask(ZFCP_STATUS_PORT_NO_WWPN,
3173338151e0SAndreas Herrmann 				     &port->status)) {
3174338151e0SAndreas Herrmann 			zfcp_port_put(port);
3175338151e0SAndreas Herrmann 			break;
3176338151e0SAndreas Herrmann 		}
3177338151e0SAndreas Herrmann 
31783859f6a2SAndreas Herrmann 		if ((result == ZFCP_ERP_SUCCEEDED)
31793859f6a2SAndreas Herrmann 		    && !port->rport) {
31803859f6a2SAndreas Herrmann 			struct fc_rport_identifiers ids;
31813859f6a2SAndreas Herrmann 			ids.node_name = port->wwnn;
31823859f6a2SAndreas Herrmann 			ids.port_name = port->wwpn;
31833859f6a2SAndreas Herrmann 			ids.port_id = port->d_id;
31843859f6a2SAndreas Herrmann 			ids.roles = FC_RPORT_ROLE_FCP_TARGET;
31853859f6a2SAndreas Herrmann 			port->rport =
31863859f6a2SAndreas Herrmann 				fc_remote_port_add(adapter->scsi_host, 0, &ids);
31873859f6a2SAndreas Herrmann 			if (!port->rport)
31883859f6a2SAndreas Herrmann 				ZFCP_LOG_NORMAL("failed registration of rport"
31893859f6a2SAndreas Herrmann 						"(adapter %s, wwpn=0x%016Lx)\n",
31903859f6a2SAndreas Herrmann 						zfcp_get_busid_by_port(port),
31913859f6a2SAndreas Herrmann 						port->wwpn);
319275bfc283SRalph Wuerthner 			else {
31935f852be9SChristof Schmitt 				scsi_target_unblock(&port->rport->dev);
319475bfc283SRalph Wuerthner 				port->rport->maxframe_size = port->maxframe_size;
319575bfc283SRalph Wuerthner 				port->rport->supported_classes =
319675bfc283SRalph Wuerthner 					port->supported_classes;
319775bfc283SRalph Wuerthner 			}
31983859f6a2SAndreas Herrmann 		}
3199338151e0SAndreas Herrmann 		if ((result != ZFCP_ERP_SUCCEEDED) && port->rport) {
3200338151e0SAndreas Herrmann 			fc_remote_port_delete(port->rport);
3201338151e0SAndreas Herrmann 			port->rport = NULL;
3202338151e0SAndreas Herrmann 		}
32031da177e4SLinus Torvalds 		zfcp_port_put(port);
32041da177e4SLinus Torvalds 		break;
32051da177e4SLinus Torvalds 	case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
3206338151e0SAndreas Herrmann 		if (result != ZFCP_ERP_SUCCEEDED) {
3207338151e0SAndreas Herrmann 			list_for_each_entry(port, &adapter->port_list_head, list)
3208338151e0SAndreas Herrmann 				if (port->rport &&
3209338151e0SAndreas Herrmann 				    !atomic_test_mask(ZFCP_STATUS_PORT_WKA,
3210338151e0SAndreas Herrmann 						      &port->status)) {
3211338151e0SAndreas Herrmann 					fc_remote_port_delete(port->rport);
3212338151e0SAndreas Herrmann 					port->rport = NULL;
3213338151e0SAndreas Herrmann 				}
3214338151e0SAndreas Herrmann 		}
32151da177e4SLinus Torvalds 		zfcp_adapter_put(adapter);
32161da177e4SLinus Torvalds 		break;
32171da177e4SLinus Torvalds 	default:
32181da177e4SLinus Torvalds 		break;
32191da177e4SLinus Torvalds 	}
32201da177e4SLinus Torvalds }
32211da177e4SLinus Torvalds 
32221da177e4SLinus Torvalds 
322381654286SAndreas Herrmann static void zfcp_erp_action_dismiss_adapter(struct zfcp_adapter *adapter)
32241da177e4SLinus Torvalds {
32251da177e4SLinus Torvalds 	struct zfcp_port *port;
32261da177e4SLinus Torvalds 
32271da177e4SLinus Torvalds 	debug_text_event(adapter->erp_dbf, 5, "a_actab");
32281da177e4SLinus Torvalds 	if (atomic_test_mask(ZFCP_STATUS_COMMON_ERP_INUSE, &adapter->status))
32291da177e4SLinus Torvalds 		zfcp_erp_action_dismiss(&adapter->erp_action);
32301da177e4SLinus Torvalds 	else
32311da177e4SLinus Torvalds 		list_for_each_entry(port, &adapter->port_list_head, list)
32321da177e4SLinus Torvalds 		    zfcp_erp_action_dismiss_port(port);
32331da177e4SLinus Torvalds }
32341da177e4SLinus Torvalds 
3235f6c0e7a7SAndreas Herrmann static void zfcp_erp_action_dismiss_port(struct zfcp_port *port)
32361da177e4SLinus Torvalds {
32371da177e4SLinus Torvalds 	struct zfcp_unit *unit;
32381da177e4SLinus Torvalds 	struct zfcp_adapter *adapter = port->adapter;
32391da177e4SLinus Torvalds 
32401da177e4SLinus Torvalds 	debug_text_event(adapter->erp_dbf, 5, "p_actab");
32411da177e4SLinus Torvalds 	debug_event(adapter->erp_dbf, 5, &port->wwpn, sizeof (wwn_t));
32421da177e4SLinus Torvalds 	if (atomic_test_mask(ZFCP_STATUS_COMMON_ERP_INUSE, &port->status))
32431da177e4SLinus Torvalds 		zfcp_erp_action_dismiss(&port->erp_action);
32441da177e4SLinus Torvalds 	else
32451da177e4SLinus Torvalds 		list_for_each_entry(unit, &port->unit_list_head, list)
32461da177e4SLinus Torvalds 		    zfcp_erp_action_dismiss_unit(unit);
32471da177e4SLinus Torvalds }
32481da177e4SLinus Torvalds 
3249f6c0e7a7SAndreas Herrmann static void zfcp_erp_action_dismiss_unit(struct zfcp_unit *unit)
32501da177e4SLinus Torvalds {
32511da177e4SLinus Torvalds 	struct zfcp_adapter *adapter = unit->port->adapter;
32521da177e4SLinus Torvalds 
32531da177e4SLinus Torvalds 	debug_text_event(adapter->erp_dbf, 5, "u_actab");
32541da177e4SLinus Torvalds 	debug_event(adapter->erp_dbf, 5, &unit->fcp_lun, sizeof (fcp_lun_t));
32551da177e4SLinus Torvalds 	if (atomic_test_mask(ZFCP_STATUS_COMMON_ERP_INUSE, &unit->status))
32561da177e4SLinus Torvalds 		zfcp_erp_action_dismiss(&unit->erp_action);
32571da177e4SLinus Torvalds }
32581da177e4SLinus Torvalds 
3259364c8558SHeiko Carstens static void zfcp_erp_action_to_running(struct zfcp_erp_action *erp_action)
32601da177e4SLinus Torvalds {
32611da177e4SLinus Torvalds 	struct zfcp_adapter *adapter = erp_action->adapter;
32621da177e4SLinus Torvalds 
32631da177e4SLinus Torvalds 	debug_text_event(adapter->erp_dbf, 6, "a_toru");
32641da177e4SLinus Torvalds 	debug_event(adapter->erp_dbf, 6, &erp_action->action, sizeof (int));
32651da177e4SLinus Torvalds 	list_move(&erp_action->list, &erp_action->adapter->erp_running_head);
32661da177e4SLinus Torvalds }
32671da177e4SLinus Torvalds 
3268364c8558SHeiko Carstens static void zfcp_erp_action_to_ready(struct zfcp_erp_action *erp_action)
32691da177e4SLinus Torvalds {
32701da177e4SLinus Torvalds 	struct zfcp_adapter *adapter = erp_action->adapter;
32711da177e4SLinus Torvalds 
32721da177e4SLinus Torvalds 	debug_text_event(adapter->erp_dbf, 6, "a_tore");
32731da177e4SLinus Torvalds 	debug_event(adapter->erp_dbf, 6, &erp_action->action, sizeof (int));
32741da177e4SLinus Torvalds 	list_move(&erp_action->list, &erp_action->adapter->erp_ready_head);
32751da177e4SLinus Torvalds }
32761da177e4SLinus Torvalds 
3277d736a27bSAndreas Herrmann void
3278d736a27bSAndreas Herrmann zfcp_erp_port_boxed(struct zfcp_port *port)
3279d736a27bSAndreas Herrmann {
3280d736a27bSAndreas Herrmann 	struct zfcp_adapter *adapter = port->adapter;
3281d736a27bSAndreas Herrmann 	unsigned long flags;
3282d736a27bSAndreas Herrmann 
3283d736a27bSAndreas Herrmann 	debug_text_event(adapter->erp_dbf, 3, "p_access_boxed");
3284d736a27bSAndreas Herrmann 	debug_event(adapter->erp_dbf, 3, &port->wwpn, sizeof(wwn_t));
3285d736a27bSAndreas Herrmann 	read_lock_irqsave(&zfcp_data.config_lock, flags);
3286d736a27bSAndreas Herrmann 	zfcp_erp_modify_port_status(port,
3287d736a27bSAndreas Herrmann 			ZFCP_STATUS_COMMON_ACCESS_BOXED,
3288d736a27bSAndreas Herrmann 			ZFCP_SET);
3289d736a27bSAndreas Herrmann 	read_unlock_irqrestore(&zfcp_data.config_lock, flags);
3290d736a27bSAndreas Herrmann 	zfcp_erp_port_reopen(port, ZFCP_STATUS_COMMON_ERP_FAILED);
3291d736a27bSAndreas Herrmann }
3292d736a27bSAndreas Herrmann 
3293d736a27bSAndreas Herrmann void
3294d736a27bSAndreas Herrmann zfcp_erp_unit_boxed(struct zfcp_unit *unit)
3295d736a27bSAndreas Herrmann {
3296d736a27bSAndreas Herrmann 	struct zfcp_adapter *adapter = unit->port->adapter;
3297d736a27bSAndreas Herrmann 
3298d736a27bSAndreas Herrmann 	debug_text_event(adapter->erp_dbf, 3, "u_access_boxed");
3299d736a27bSAndreas Herrmann 	debug_event(adapter->erp_dbf, 3, &unit->fcp_lun, sizeof(fcp_lun_t));
3300d736a27bSAndreas Herrmann 	zfcp_erp_modify_unit_status(unit,
3301d736a27bSAndreas Herrmann 			ZFCP_STATUS_COMMON_ACCESS_BOXED,
3302d736a27bSAndreas Herrmann 			ZFCP_SET);
3303d736a27bSAndreas Herrmann 	zfcp_erp_unit_reopen(unit, ZFCP_STATUS_COMMON_ERP_FAILED);
3304d736a27bSAndreas Herrmann }
3305d736a27bSAndreas Herrmann 
33061da177e4SLinus Torvalds void
33071da177e4SLinus Torvalds zfcp_erp_port_access_denied(struct zfcp_port *port)
33081da177e4SLinus Torvalds {
33091da177e4SLinus Torvalds 	struct zfcp_adapter *adapter = port->adapter;
33101da177e4SLinus Torvalds 	unsigned long flags;
33111da177e4SLinus Torvalds 
3312d736a27bSAndreas Herrmann 	debug_text_event(adapter->erp_dbf, 3, "p_access_denied");
33131da177e4SLinus Torvalds 	debug_event(adapter->erp_dbf, 3, &port->wwpn, sizeof(wwn_t));
33141da177e4SLinus Torvalds 	read_lock_irqsave(&zfcp_data.config_lock, flags);
3315d736a27bSAndreas Herrmann 	zfcp_erp_modify_port_status(port,
3316d736a27bSAndreas Herrmann 			ZFCP_STATUS_COMMON_ERP_FAILED |
3317d736a27bSAndreas Herrmann 			ZFCP_STATUS_COMMON_ACCESS_DENIED,
3318d736a27bSAndreas Herrmann 			ZFCP_SET);
33191da177e4SLinus Torvalds 	read_unlock_irqrestore(&zfcp_data.config_lock, flags);
33201da177e4SLinus Torvalds }
33211da177e4SLinus Torvalds 
33221da177e4SLinus Torvalds void
33231da177e4SLinus Torvalds zfcp_erp_unit_access_denied(struct zfcp_unit *unit)
33241da177e4SLinus Torvalds {
33251da177e4SLinus Torvalds 	struct zfcp_adapter *adapter = unit->port->adapter;
33261da177e4SLinus Torvalds 
3327d736a27bSAndreas Herrmann 	debug_text_event(adapter->erp_dbf, 3, "u_access_denied");
33281da177e4SLinus Torvalds 	debug_event(adapter->erp_dbf, 3, &unit->fcp_lun, sizeof(fcp_lun_t));
3329d736a27bSAndreas Herrmann 	zfcp_erp_modify_unit_status(unit,
3330d736a27bSAndreas Herrmann 			ZFCP_STATUS_COMMON_ERP_FAILED |
3331d736a27bSAndreas Herrmann 			ZFCP_STATUS_COMMON_ACCESS_DENIED,
3332d736a27bSAndreas Herrmann 			ZFCP_SET);
33331da177e4SLinus Torvalds }
33341da177e4SLinus Torvalds 
33351da177e4SLinus Torvalds void
33361da177e4SLinus Torvalds zfcp_erp_adapter_access_changed(struct zfcp_adapter *adapter)
33371da177e4SLinus Torvalds {
33381da177e4SLinus Torvalds 	struct zfcp_port *port;
33391da177e4SLinus Torvalds 	unsigned long flags;
33401da177e4SLinus Torvalds 
3341aef4a983SMaxim Shchetynin 	if (adapter->connection_features & FSF_FEATURE_NPIV_MODE)
3342aef4a983SMaxim Shchetynin 		return;
3343aef4a983SMaxim Shchetynin 
3344d736a27bSAndreas Herrmann 	debug_text_event(adapter->erp_dbf, 3, "a_access_recover");
334506506d00SAndreas Herrmann 	debug_event(adapter->erp_dbf, 3, zfcp_get_busid_by_adapter(adapter), 8);
33461da177e4SLinus Torvalds 
33471da177e4SLinus Torvalds 	read_lock_irqsave(&zfcp_data.config_lock, flags);
33486f71d9bcS 	if (adapter->nameserver_port)
33496f71d9bcS 		zfcp_erp_port_access_changed(adapter->nameserver_port);
33501da177e4SLinus Torvalds 	list_for_each_entry(port, &adapter->port_list_head, list)
33511da177e4SLinus Torvalds 		if (port != adapter->nameserver_port)
33521da177e4SLinus Torvalds 			zfcp_erp_port_access_changed(port);
33531da177e4SLinus Torvalds 	read_unlock_irqrestore(&zfcp_data.config_lock, flags);
33541da177e4SLinus Torvalds }
33551da177e4SLinus Torvalds 
33561da177e4SLinus Torvalds void
33571da177e4SLinus Torvalds zfcp_erp_port_access_changed(struct zfcp_port *port)
33581da177e4SLinus Torvalds {
33591da177e4SLinus Torvalds 	struct zfcp_adapter *adapter = port->adapter;
33601da177e4SLinus Torvalds 	struct zfcp_unit *unit;
33611da177e4SLinus Torvalds 
3362d736a27bSAndreas Herrmann 	debug_text_event(adapter->erp_dbf, 3, "p_access_recover");
33631da177e4SLinus Torvalds 	debug_event(adapter->erp_dbf, 3, &port->wwpn, sizeof(wwn_t));
33641da177e4SLinus Torvalds 
33651da177e4SLinus Torvalds 	if (!atomic_test_mask(ZFCP_STATUS_COMMON_ACCESS_DENIED,
3366d736a27bSAndreas Herrmann 			      &port->status) &&
3367d736a27bSAndreas Herrmann 	    !atomic_test_mask(ZFCP_STATUS_COMMON_ACCESS_BOXED,
33681da177e4SLinus Torvalds 			      &port->status)) {
33691da177e4SLinus Torvalds 		if (!atomic_test_mask(ZFCP_STATUS_PORT_WKA, &port->status))
33701da177e4SLinus Torvalds 			list_for_each_entry(unit, &port->unit_list_head, list)
33711da177e4SLinus Torvalds 				zfcp_erp_unit_access_changed(unit);
33721da177e4SLinus Torvalds 		return;
33731da177e4SLinus Torvalds 	}
33741da177e4SLinus Torvalds 
33751da177e4SLinus Torvalds 	ZFCP_LOG_NORMAL("reopen of port 0x%016Lx on adapter %s "
33761da177e4SLinus Torvalds 			"(due to ACT update)\n",
33771da177e4SLinus Torvalds 			port->wwpn, zfcp_get_busid_by_adapter(adapter));
33781da177e4SLinus Torvalds 	if (zfcp_erp_port_reopen(port, ZFCP_STATUS_COMMON_ERP_FAILED) != 0)
33791da177e4SLinus Torvalds 		ZFCP_LOG_NORMAL("failed reopen of port"
33801da177e4SLinus Torvalds 				"(adapter %s, wwpn=0x%016Lx)\n",
33811da177e4SLinus Torvalds 				zfcp_get_busid_by_adapter(adapter), port->wwpn);
33821da177e4SLinus Torvalds }
33831da177e4SLinus Torvalds 
33841da177e4SLinus Torvalds void
33851da177e4SLinus Torvalds zfcp_erp_unit_access_changed(struct zfcp_unit *unit)
33861da177e4SLinus Torvalds {
33871da177e4SLinus Torvalds 	struct zfcp_adapter *adapter = unit->port->adapter;
33881da177e4SLinus Torvalds 
3389d736a27bSAndreas Herrmann 	debug_text_event(adapter->erp_dbf, 3, "u_access_recover");
33901da177e4SLinus Torvalds 	debug_event(adapter->erp_dbf, 3, &unit->fcp_lun, sizeof(fcp_lun_t));
33911da177e4SLinus Torvalds 
3392d736a27bSAndreas Herrmann 	if (!atomic_test_mask(ZFCP_STATUS_COMMON_ACCESS_DENIED,
3393d736a27bSAndreas Herrmann 			      &unit->status) &&
3394d736a27bSAndreas Herrmann 	    !atomic_test_mask(ZFCP_STATUS_COMMON_ACCESS_BOXED,
3395d736a27bSAndreas Herrmann 			      &unit->status))
33961da177e4SLinus Torvalds 		return;
33971da177e4SLinus Torvalds 
33981da177e4SLinus Torvalds 	ZFCP_LOG_NORMAL("reopen of unit 0x%016Lx on port 0x%016Lx "
33991da177e4SLinus Torvalds 			" on adapter %s (due to ACT update)\n",
34001da177e4SLinus Torvalds 			unit->fcp_lun, unit->port->wwpn,
34011da177e4SLinus Torvalds 			zfcp_get_busid_by_adapter(adapter));
34021da177e4SLinus Torvalds 	if (zfcp_erp_unit_reopen(unit, ZFCP_STATUS_COMMON_ERP_FAILED) != 0)
34031da177e4SLinus Torvalds 		ZFCP_LOG_NORMAL("failed reopen of unit (adapter %s, "
34041da177e4SLinus Torvalds 				"wwpn=0x%016Lx, fcp_lun=0x%016Lx)\n",
34051da177e4SLinus Torvalds 				zfcp_get_busid_by_adapter(adapter),
34061da177e4SLinus Torvalds 				unit->port->wwpn, unit->fcp_lun);
34071da177e4SLinus Torvalds }
34081da177e4SLinus Torvalds 
34091da177e4SLinus Torvalds #undef ZFCP_LOG_AREA
3410