zfcp_erp.c 47.2 KB
Newer Older
S
Swen Schillig 已提交
1
/*
C
Christof Schmitt 已提交
2
 * zfcp device driver
L
Linus Torvalds 已提交
3
 *
C
Christof Schmitt 已提交
4
 * Error Recovery Procedures (ERP).
S
Swen Schillig 已提交
5
 *
C
Christof Schmitt 已提交
6
 * Copyright IBM Corporation 2002, 2008
L
Linus Torvalds 已提交
7 8 9 10
 */

#include "zfcp_ext.h"

11 12 13 14 15 16 17 18 19
#define ZFCP_MAX_ERPS                   3

enum zfcp_erp_act_flags {
	ZFCP_STATUS_ERP_TIMEDOUT	= 0x10000000,
	ZFCP_STATUS_ERP_CLOSE_ONLY	= 0x01000000,
	ZFCP_STATUS_ERP_DISMISSING	= 0x00100000,
	ZFCP_STATUS_ERP_DISMISSED	= 0x00200000,
	ZFCP_STATUS_ERP_LOWMEM		= 0x00400000,
};
L
Linus Torvalds 已提交
20

21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
enum zfcp_erp_steps {
	ZFCP_ERP_STEP_UNINITIALIZED	= 0x0000,
	ZFCP_ERP_STEP_FSF_XCONFIG	= 0x0001,
	ZFCP_ERP_STEP_PHYS_PORT_CLOSING	= 0x0010,
	ZFCP_ERP_STEP_PORT_CLOSING	= 0x0100,
	ZFCP_ERP_STEP_NAMESERVER_LOOKUP	= 0x0400,
	ZFCP_ERP_STEP_PORT_OPENING	= 0x0800,
	ZFCP_ERP_STEP_UNIT_CLOSING	= 0x1000,
	ZFCP_ERP_STEP_UNIT_OPENING	= 0x2000,
};

enum zfcp_erp_act_type {
	ZFCP_ERP_ACTION_REOPEN_UNIT        = 1,
	ZFCP_ERP_ACTION_REOPEN_PORT	   = 2,
	ZFCP_ERP_ACTION_REOPEN_PORT_FORCED = 3,
	ZFCP_ERP_ACTION_REOPEN_ADAPTER     = 4,
};

enum zfcp_erp_act_state {
	ZFCP_ERP_ACTION_RUNNING = 1,
	ZFCP_ERP_ACTION_READY   = 2,
};

enum zfcp_erp_act_result {
	ZFCP_ERP_SUCCEEDED = 0,
	ZFCP_ERP_FAILED    = 1,
	ZFCP_ERP_CONTINUES = 2,
	ZFCP_ERP_EXIT      = 3,
	ZFCP_ERP_DISMISSED = 4,
	ZFCP_ERP_NOMEM     = 5,
};

static void zfcp_erp_adapter_block(struct zfcp_adapter *adapter, int mask)
L
Linus Torvalds 已提交
54
{
55 56 57
	zfcp_erp_modify_adapter_status(adapter, 15, NULL,
				       ZFCP_STATUS_COMMON_UNBLOCKED | mask,
				       ZFCP_CLEAR);
58
}
L
Linus Torvalds 已提交
59

60
static int zfcp_erp_action_exists(struct zfcp_erp_action *act)
61
{
62 63 64 65 66 67
	struct zfcp_erp_action *curr_act;

	list_for_each_entry(curr_act, &act->adapter->erp_running_head, list)
		if (act == curr_act)
			return ZFCP_ERP_ACTION_RUNNING;
	return 0;
L
Linus Torvalds 已提交
68 69
}

70
static void zfcp_erp_action_ready(struct zfcp_erp_action *act)
71
{
72 73 74 75 76 77
	struct zfcp_adapter *adapter = act->adapter;

	list_move(&act->list, &act->adapter->erp_ready_head);
	zfcp_rec_dbf_event_action(146, act);
	up(&adapter->erp_ready_sem);
	zfcp_rec_dbf_event_thread(2, adapter);
78 79
}

80
static void zfcp_erp_action_dismiss(struct zfcp_erp_action *act)
L
Linus Torvalds 已提交
81
{
82 83 84 85
	act->status |= ZFCP_STATUS_ERP_DISMISSED;
	if (zfcp_erp_action_exists(act) == ZFCP_ERP_ACTION_RUNNING)
		zfcp_erp_action_ready(act);
}
L
Linus Torvalds 已提交
86

87 88 89 90 91
static void zfcp_erp_action_dismiss_unit(struct zfcp_unit *unit)
{
	if (atomic_read(&unit->status) & ZFCP_STATUS_COMMON_ERP_INUSE)
		zfcp_erp_action_dismiss(&unit->erp_action);
}
L
Linus Torvalds 已提交
92

93 94 95
static void zfcp_erp_action_dismiss_port(struct zfcp_port *port)
{
	struct zfcp_unit *unit;
L
Linus Torvalds 已提交
96

97 98 99 100 101
	if (atomic_read(&port->status) & ZFCP_STATUS_COMMON_ERP_INUSE)
		zfcp_erp_action_dismiss(&port->erp_action);
	else
		list_for_each_entry(unit, &port->unit_list_head, list)
		    zfcp_erp_action_dismiss_unit(unit);
L
Linus Torvalds 已提交
102 103
}

104
static void zfcp_erp_action_dismiss_adapter(struct zfcp_adapter *adapter)
L
Linus Torvalds 已提交
105
{
106
	struct zfcp_port *port;
L
Linus Torvalds 已提交
107

108 109 110 111 112
	if (atomic_read(&adapter->status) & ZFCP_STATUS_COMMON_ERP_INUSE)
		zfcp_erp_action_dismiss(&adapter->erp_action);
	else
		list_for_each_entry(port, &adapter->port_list_head, list)
		    zfcp_erp_action_dismiss_port(port);
L
Linus Torvalds 已提交
113 114
}

115 116 117
static int zfcp_erp_required_act(int want, struct zfcp_adapter *adapter,
				 struct zfcp_port *port,
				 struct zfcp_unit *unit)
L
Linus Torvalds 已提交
118
{
119 120
	int need = want;
	int u_status, p_status, a_status;
L
Linus Torvalds 已提交
121

122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
	switch (want) {
	case ZFCP_ERP_ACTION_REOPEN_UNIT:
		u_status = atomic_read(&unit->status);
		if (u_status & ZFCP_STATUS_COMMON_ERP_INUSE)
			return 0;
		p_status = atomic_read(&port->status);
		if (!(p_status & ZFCP_STATUS_COMMON_RUNNING) ||
		      p_status & ZFCP_STATUS_COMMON_ERP_FAILED)
			return 0;
		if (!(p_status & ZFCP_STATUS_COMMON_UNBLOCKED))
			need = ZFCP_ERP_ACTION_REOPEN_PORT;
		/* fall through */
	case ZFCP_ERP_ACTION_REOPEN_PORT:
	case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
		p_status = atomic_read(&port->status);
		if (p_status & ZFCP_STATUS_COMMON_ERP_INUSE)
			return 0;
		a_status = atomic_read(&adapter->status);
		if (!(a_status & ZFCP_STATUS_COMMON_RUNNING) ||
		      a_status & ZFCP_STATUS_COMMON_ERP_FAILED)
			return 0;
		if (!(a_status & ZFCP_STATUS_COMMON_UNBLOCKED))
			need = ZFCP_ERP_ACTION_REOPEN_ADAPTER;
		/* fall through */
	case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
		a_status = atomic_read(&adapter->status);
		if (a_status & ZFCP_STATUS_COMMON_ERP_INUSE)
			return 0;
	}
L
Linus Torvalds 已提交
151

152
	return need;
L
Linus Torvalds 已提交
153 154
}

155 156 157 158
static struct zfcp_erp_action *zfcp_erp_setup_act(int need,
						  struct zfcp_adapter *adapter,
						  struct zfcp_port *port,
						  struct zfcp_unit *unit)
L
Linus Torvalds 已提交
159
{
160 161
	struct zfcp_erp_action *erp_action;
	u32 status = 0;
L
Linus Torvalds 已提交
162

163 164 165 166 167 168 169 170
	switch (need) {
	case ZFCP_ERP_ACTION_REOPEN_UNIT:
		zfcp_unit_get(unit);
		atomic_set_mask(ZFCP_STATUS_COMMON_ERP_INUSE, &unit->status);
		erp_action = &unit->erp_action;
		if (!(atomic_read(&unit->status) & ZFCP_STATUS_COMMON_RUNNING))
			status = ZFCP_STATUS_ERP_CLOSE_ONLY;
		break;
L
Linus Torvalds 已提交
171

172 173 174 175 176 177 178 179 180
	case ZFCP_ERP_ACTION_REOPEN_PORT:
	case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
		zfcp_port_get(port);
		zfcp_erp_action_dismiss_port(port);
		atomic_set_mask(ZFCP_STATUS_COMMON_ERP_INUSE, &port->status);
		erp_action = &port->erp_action;
		if (!(atomic_read(&port->status) & ZFCP_STATUS_COMMON_RUNNING))
			status = ZFCP_STATUS_ERP_CLOSE_ONLY;
		break;
L
Linus Torvalds 已提交
181

182 183 184 185 186 187 188 189 190 191 192 193 194
	case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
		zfcp_adapter_get(adapter);
		zfcp_erp_action_dismiss_adapter(adapter);
		atomic_set_mask(ZFCP_STATUS_COMMON_ERP_INUSE, &adapter->status);
		erp_action = &adapter->erp_action;
		if (!(atomic_read(&adapter->status) &
		      ZFCP_STATUS_COMMON_RUNNING))
			status = ZFCP_STATUS_ERP_CLOSE_ONLY;
		break;

	default:
		return NULL;
	}
L
Linus Torvalds 已提交
195

196 197 198 199 200 201
	memset(erp_action, 0, sizeof(struct zfcp_erp_action));
	erp_action->adapter = adapter;
	erp_action->port = port;
	erp_action->unit = unit;
	erp_action->action = need;
	erp_action->status = status;
L
Linus Torvalds 已提交
202

203
	return erp_action;
L
Linus Torvalds 已提交
204 205
}

206 207 208
static int zfcp_erp_action_enqueue(int want, struct zfcp_adapter *adapter,
				   struct zfcp_port *port,
				   struct zfcp_unit *unit, u8 id, void *ref)
L
Linus Torvalds 已提交
209
{
210 211
	int retval = 1, need;
	struct zfcp_erp_action *act = NULL;
L
Linus Torvalds 已提交
212

213 214 215
	if (!(atomic_read(&adapter->status) &
	      ZFCP_STATUS_ADAPTER_ERP_THREAD_UP))
		return -EIO;
L
Linus Torvalds 已提交
216

217 218
	need = zfcp_erp_required_act(want, adapter, port, unit);
	if (!need)
L
Linus Torvalds 已提交
219 220
		goto out;

221 222 223 224 225 226 227 228 229
	atomic_set_mask(ZFCP_STATUS_ADAPTER_ERP_PENDING, &adapter->status);
	act = zfcp_erp_setup_act(need, adapter, port, unit);
	if (!act)
		goto out;
	++adapter->erp_total_count;
	list_add_tail(&act->list, &adapter->erp_ready_head);
	up(&adapter->erp_ready_sem);
	zfcp_rec_dbf_event_thread(1, adapter);
	retval = 0;
L
Linus Torvalds 已提交
230
 out:
231 232
	zfcp_rec_dbf_event_trigger(id, ref, want, need, act,
				   adapter, port, unit);
L
Linus Torvalds 已提交
233 234 235
	return retval;
}

236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
static int _zfcp_erp_adapter_reopen(struct zfcp_adapter *adapter,
				    int clear_mask, u8 id, void *ref)
{
	zfcp_erp_adapter_block(adapter, clear_mask);

	/* ensure propagation of failed status to new devices */
	if (atomic_read(&adapter->status) & ZFCP_STATUS_COMMON_ERP_FAILED) {
		zfcp_erp_adapter_failed(adapter, 13, NULL);
		return -EIO;
	}
	return zfcp_erp_action_enqueue(ZFCP_ERP_ACTION_REOPEN_ADAPTER,
				       adapter, NULL, NULL, id, ref);
}

/**
 * zfcp_erp_adapter_reopen - Reopen adapter.
 * @adapter: Adapter to reopen.
 * @clear: Status flags to clear.
 * @id: Id for debug trace event.
 * @ref: Reference for debug trace event.
L
Linus Torvalds 已提交
256
 */
257 258
void zfcp_erp_adapter_reopen(struct zfcp_adapter *adapter, int clear,
			     u8 id, void *ref)
L
Linus Torvalds 已提交
259 260 261 262 263
{
	unsigned long flags;

	read_lock_irqsave(&zfcp_data.config_lock, flags);
	write_lock(&adapter->erp_lock);
264
	_zfcp_erp_adapter_reopen(adapter, clear, id, ref);
L
Linus Torvalds 已提交
265 266
	write_unlock(&adapter->erp_lock);
	read_unlock_irqrestore(&zfcp_data.config_lock, flags);
267
}
L
Linus Torvalds 已提交
268

269 270 271 272 273 274 275 276 277 278 279 280
/**
 * zfcp_erp_adapter_shutdown - Shutdown adapter.
 * @adapter: Adapter to shut down.
 * @clear: Status flags to clear.
 * @id: Id for debug trace event.
 * @ref: Reference for debug trace event.
 */
void zfcp_erp_adapter_shutdown(struct zfcp_adapter *adapter, int clear,
			       u8 id, void *ref)
{
	int flags = ZFCP_STATUS_COMMON_RUNNING | ZFCP_STATUS_COMMON_ERP_FAILED;
	zfcp_erp_adapter_reopen(adapter, clear | flags, id, ref);
L
Linus Torvalds 已提交
281 282
}

283 284 285 286 287 288
/**
 * zfcp_erp_port_shutdown - Shutdown port
 * @port: Port to shut down.
 * @clear: Status flags to clear.
 * @id: Id for debug trace event.
 * @ref: Reference for debug trace event.
L
Linus Torvalds 已提交
289
 */
290
void zfcp_erp_port_shutdown(struct zfcp_port *port, int clear, u8 id, void *ref)
L
Linus Torvalds 已提交
291
{
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
	int flags = ZFCP_STATUS_COMMON_RUNNING | ZFCP_STATUS_COMMON_ERP_FAILED;
	zfcp_erp_port_reopen(port, clear | flags, id, ref);
}

/**
 * zfcp_erp_unit_shutdown - Shutdown unit
 * @unit: Unit to shut down.
 * @clear: Status flags to clear.
 * @id: Id for debug trace event.
 * @ref: Reference for debug trace event.
 */
void zfcp_erp_unit_shutdown(struct zfcp_unit *unit, int clear, u8 id, void *ref)
{
	int flags = ZFCP_STATUS_COMMON_RUNNING | ZFCP_STATUS_COMMON_ERP_FAILED;
	zfcp_erp_unit_reopen(unit, clear | flags, id, ref);
}

static void zfcp_erp_port_block(struct zfcp_port *port, int clear)
{
	zfcp_erp_modify_port_status(port, 17, NULL,
				    ZFCP_STATUS_COMMON_UNBLOCKED | clear,
				    ZFCP_CLEAR);
}

static void _zfcp_erp_port_forced_reopen(struct zfcp_port *port,
					 int clear, u8 id, void *ref)
{
	zfcp_erp_port_block(port, clear);

	if (atomic_read(&port->status) & ZFCP_STATUS_COMMON_ERP_FAILED)
		return;
L
Linus Torvalds 已提交
323

324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
	zfcp_erp_action_enqueue(ZFCP_ERP_ACTION_REOPEN_PORT_FORCED,
				port->adapter, port, NULL, id, ref);
}

/**
 * zfcp_erp_port_forced_reopen - Forced close of port and open again
 * @port: Port to force close and to reopen.
 * @id: Id for debug trace event.
 * @ref: Reference for debug trace event.
 */
void zfcp_erp_port_forced_reopen(struct zfcp_port *port, int clear, u8 id,
				 void *ref)
{
	unsigned long flags;
	struct zfcp_adapter *adapter = port->adapter;

	read_lock_irqsave(&zfcp_data.config_lock, flags);
	write_lock(&adapter->erp_lock);
	_zfcp_erp_port_forced_reopen(port, clear, id, ref);
	write_unlock(&adapter->erp_lock);
	read_unlock_irqrestore(&zfcp_data.config_lock, flags);
}

static int _zfcp_erp_port_reopen(struct zfcp_port *port, int clear, u8 id,
				 void *ref)
{
	zfcp_erp_port_block(port, clear);
L
Linus Torvalds 已提交
351

352
	if (atomic_read(&port->status) & ZFCP_STATUS_COMMON_ERP_FAILED) {
L
Linus Torvalds 已提交
353
		/* ensure propagation of failed status to new devices */
354
		zfcp_erp_port_failed(port, 14, NULL);
355
		return -EIO;
L
Linus Torvalds 已提交
356 357
	}

358 359
	return zfcp_erp_action_enqueue(ZFCP_ERP_ACTION_REOPEN_PORT,
				       port->adapter, port, NULL, id, ref);
L
Linus Torvalds 已提交
360 361 362
}

/**
363 364 365
 * zfcp_erp_port_reopen - trigger remote port recovery
 * @port: port to recover
 * @clear_mask: flags in port status to be cleared
L
Linus Torvalds 已提交
366
 *
367
 * Returns 0 if recovery has been triggered, < 0 if not.
L
Linus Torvalds 已提交
368
 */
369
int zfcp_erp_port_reopen(struct zfcp_port *port, int clear, u8 id, void *ref)
L
Linus Torvalds 已提交
370 371
{
	unsigned long flags;
372
	int retval;
L
Linus Torvalds 已提交
373 374 375 376
	struct zfcp_adapter *adapter = port->adapter;

	read_lock_irqsave(&zfcp_data.config_lock, flags);
	write_lock(&adapter->erp_lock);
377
	retval = _zfcp_erp_port_reopen(port, clear, id, ref);
L
Linus Torvalds 已提交
378 379 380 381 382 383
	write_unlock(&adapter->erp_lock);
	read_unlock_irqrestore(&zfcp_data.config_lock, flags);

	return retval;
}

384 385 386 387 388 389 390 391 392
static void zfcp_erp_unit_block(struct zfcp_unit *unit, int clear_mask)
{
	zfcp_erp_modify_unit_status(unit, 19, NULL,
				    ZFCP_STATUS_COMMON_UNBLOCKED | clear_mask,
				    ZFCP_CLEAR);
}

static void _zfcp_erp_unit_reopen(struct zfcp_unit *unit, int clear, u8 id,
				  void *ref)
L
Linus Torvalds 已提交
393 394 395
{
	struct zfcp_adapter *adapter = unit->port->adapter;

396
	zfcp_erp_unit_block(unit, clear);
L
Linus Torvalds 已提交
397

398 399
	if (atomic_read(&unit->status) & ZFCP_STATUS_COMMON_ERP_FAILED)
		return;
L
Linus Torvalds 已提交
400

401 402
	zfcp_erp_action_enqueue(ZFCP_ERP_ACTION_REOPEN_UNIT,
				adapter, unit->port, unit, id, ref);
L
Linus Torvalds 已提交
403 404 405 406 407 408 409 410
}

/**
 * zfcp_erp_unit_reopen - initiate reopen of a unit
 * @unit: unit to be reopened
 * @clear_mask: specifies flags in unit status to be cleared
 * Return: 0 on success, < 0 on error
 */
411
void zfcp_erp_unit_reopen(struct zfcp_unit *unit, int clear, u8 id, void *ref)
L
Linus Torvalds 已提交
412 413
{
	unsigned long flags;
414 415
	struct zfcp_port *port = unit->port;
	struct zfcp_adapter *adapter = port->adapter;
L
Linus Torvalds 已提交
416 417 418

	read_lock_irqsave(&zfcp_data.config_lock, flags);
	write_lock(&adapter->erp_lock);
419
	_zfcp_erp_unit_reopen(unit, clear, id, ref);
L
Linus Torvalds 已提交
420 421 422 423
	write_unlock(&adapter->erp_lock);
	read_unlock_irqrestore(&zfcp_data.config_lock, flags);
}

424
static int status_change_set(unsigned long mask, atomic_t *status)
L
Linus Torvalds 已提交
425
{
426
	return (atomic_read(status) ^ mask) & mask;
L
Linus Torvalds 已提交
427 428
}

429
static int status_change_clear(unsigned long mask, atomic_t *status)
430
{
431
	return atomic_read(status) & mask;
432 433
}

434
static void zfcp_erp_adapter_unblock(struct zfcp_adapter *adapter)
435
{
436 437 438
	if (status_change_set(ZFCP_STATUS_COMMON_UNBLOCKED, &adapter->status))
		zfcp_rec_dbf_event_adapter(16, NULL, adapter);
	atomic_set_mask(ZFCP_STATUS_COMMON_UNBLOCKED, &adapter->status);
439 440
}

441
static void zfcp_erp_port_unblock(struct zfcp_port *port)
L
Linus Torvalds 已提交
442
{
443 444 445
	if (status_change_set(ZFCP_STATUS_COMMON_UNBLOCKED, &port->status))
		zfcp_rec_dbf_event_port(18, NULL, port);
	atomic_set_mask(ZFCP_STATUS_COMMON_UNBLOCKED, &port->status);
L
Linus Torvalds 已提交
446 447
}

448
static void zfcp_erp_unit_unblock(struct zfcp_unit *unit)
L
Linus Torvalds 已提交
449
{
450 451 452
	if (status_change_set(ZFCP_STATUS_COMMON_UNBLOCKED, &unit->status))
		zfcp_rec_dbf_event_unit(20, NULL, unit);
	atomic_set_mask(ZFCP_STATUS_COMMON_UNBLOCKED, &unit->status);
L
Linus Torvalds 已提交
453 454
}

455
static void zfcp_erp_action_to_running(struct zfcp_erp_action *erp_action)
L
Linus Torvalds 已提交
456
{
457 458
	list_move(&erp_action->list, &erp_action->adapter->erp_running_head);
	zfcp_rec_dbf_event_action(145, erp_action);
L
Linus Torvalds 已提交
459 460
}

461
static void zfcp_erp_strategy_check_fsfreq(struct zfcp_erp_action *act)
L
Linus Torvalds 已提交
462
{
463
	struct zfcp_adapter *adapter = act->adapter;
L
Linus Torvalds 已提交
464

465 466
	if (!act->fsf_req)
		return;
L
Linus Torvalds 已提交
467

468 469 470 471 472 473 474
	spin_lock(&adapter->req_list_lock);
	if (zfcp_reqlist_find_safe(adapter, act->fsf_req) &&
	    act->fsf_req->erp_action == act) {
		if (act->status & (ZFCP_STATUS_ERP_DISMISSED |
				   ZFCP_STATUS_ERP_TIMEDOUT)) {
			act->fsf_req->status |= ZFCP_STATUS_FSFREQ_DISMISSED;
			zfcp_rec_dbf_event_action(142, act);
475
			act->fsf_req->erp_action = NULL;
L
Linus Torvalds 已提交
476
		}
477 478 479 480 481 482 483 484
		if (act->status & ZFCP_STATUS_ERP_TIMEDOUT)
			zfcp_rec_dbf_event_action(143, act);
		if (act->fsf_req->status & (ZFCP_STATUS_FSFREQ_COMPLETED |
					    ZFCP_STATUS_FSFREQ_DISMISSED))
			act->fsf_req = NULL;
	} else
		act->fsf_req = NULL;
	spin_unlock(&adapter->req_list_lock);
L
Linus Torvalds 已提交
485 486
}

487 488 489 490
/**
 * zfcp_erp_notify - Trigger ERP action.
 * @erp_action: ERP action to continue.
 * @set_mask: ERP action status flags to set.
L
Linus Torvalds 已提交
491
 */
492
void zfcp_erp_notify(struct zfcp_erp_action *erp_action, unsigned long set_mask)
L
Linus Torvalds 已提交
493 494
{
	struct zfcp_adapter *adapter = erp_action->adapter;
495
	unsigned long flags;
L
Linus Torvalds 已提交
496

497
	write_lock_irqsave(&adapter->erp_lock, flags);
L
Linus Torvalds 已提交
498 499 500 501 502 503 504
	if (zfcp_erp_action_exists(erp_action) == ZFCP_ERP_ACTION_RUNNING) {
		erp_action->status |= set_mask;
		zfcp_erp_action_ready(erp_action);
	}
	write_unlock_irqrestore(&adapter->erp_lock, flags);
}

505 506 507
/**
 * zfcp_erp_timeout_handler - Trigger ERP action from timed out ERP request
 * @data: ERP action (from timer data)
L
Linus Torvalds 已提交
508
 */
509
void zfcp_erp_timeout_handler(unsigned long data)
L
Linus Torvalds 已提交
510
{
511 512
	struct zfcp_erp_action *act = (struct zfcp_erp_action *) data;
	zfcp_erp_notify(act, ZFCP_STATUS_ERP_TIMEDOUT);
L
Linus Torvalds 已提交
513 514
}

515
static void zfcp_erp_memwait_handler(unsigned long data)
L
Linus Torvalds 已提交
516
{
517
	zfcp_erp_notify((struct zfcp_erp_action *)data, 0);
L
Linus Torvalds 已提交
518 519
}

520
static void zfcp_erp_strategy_memwait(struct zfcp_erp_action *erp_action)
L
Linus Torvalds 已提交
521
{
522 523 524 525 526
	init_timer(&erp_action->timer);
	erp_action->timer.function = zfcp_erp_memwait_handler;
	erp_action->timer.data = (unsigned long) erp_action;
	erp_action->timer.expires = jiffies + HZ;
	add_timer(&erp_action->timer);
L
Linus Torvalds 已提交
527 528
}

529 530
static void _zfcp_erp_port_reopen_all(struct zfcp_adapter *adapter,
				      int clear, u8 id, void *ref)
L
Linus Torvalds 已提交
531
{
532
	struct zfcp_port *port;
L
Linus Torvalds 已提交
533

534
	list_for_each_entry(port, &adapter->port_list_head, list)
535
		_zfcp_erp_port_reopen(port, clear, id, ref);
L
Linus Torvalds 已提交
536 537
}

538 539
static void _zfcp_erp_unit_reopen_all(struct zfcp_port *port, int clear, u8 id,
				      void *ref)
L
Linus Torvalds 已提交
540
{
541
	struct zfcp_unit *unit;
L
Linus Torvalds 已提交
542

543 544
	list_for_each_entry(unit, &port->unit_list_head, list)
		_zfcp_erp_unit_reopen(unit, clear, id, ref);
L
Linus Torvalds 已提交
545 546
}

547
static void zfcp_erp_strategy_followup_actions(struct zfcp_erp_action *act)
L
Linus Torvalds 已提交
548
{
549 550 551 552
	struct zfcp_adapter *adapter = act->adapter;
	struct zfcp_port *port = act->port;
	struct zfcp_unit *unit = act->unit;
	u32 status = act->status;
L
Linus Torvalds 已提交
553

554 555
	/* initiate follow-up actions depending on success of finished action */
	switch (act->action) {
L
Linus Torvalds 已提交
556

557 558 559 560 561 562
	case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
		if (status == ZFCP_ERP_SUCCEEDED)
			_zfcp_erp_port_reopen_all(adapter, 0, 70, NULL);
		else
			_zfcp_erp_adapter_reopen(adapter, 0, 71, NULL);
		break;
L
Linus Torvalds 已提交
563

564 565 566 567 568 569
	case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
		if (status == ZFCP_ERP_SUCCEEDED)
			_zfcp_erp_port_reopen(port, 0, 72, NULL);
		else
			_zfcp_erp_adapter_reopen(adapter, 0, 73, NULL);
		break;
L
Linus Torvalds 已提交
570

571 572 573 574 575 576
	case ZFCP_ERP_ACTION_REOPEN_PORT:
		if (status == ZFCP_ERP_SUCCEEDED)
			_zfcp_erp_unit_reopen_all(port, 0, 74, NULL);
		else
			_zfcp_erp_port_forced_reopen(port, 0, 75, NULL);
		break;
L
Linus Torvalds 已提交
577

578 579 580 581
	case ZFCP_ERP_ACTION_REOPEN_UNIT:
		if (status != ZFCP_ERP_SUCCEEDED)
			_zfcp_erp_port_reopen(unit->port, 0, 76, NULL);
		break;
L
Linus Torvalds 已提交
582 583 584
	}
}

585
static void zfcp_erp_wakeup(struct zfcp_adapter *adapter)
L
Linus Torvalds 已提交
586 587 588 589
{
	unsigned long flags;

	read_lock_irqsave(&zfcp_data.config_lock, flags);
590 591 592 593 594 595
	read_lock(&adapter->erp_lock);
	if (list_empty(&adapter->erp_ready_head) &&
	    list_empty(&adapter->erp_running_head)) {
			atomic_clear_mask(ZFCP_STATUS_ADAPTER_ERP_PENDING,
					  &adapter->status);
			wake_up(&adapter->erp_done_wqh);
L
Linus Torvalds 已提交
596
	}
597 598 599
	read_unlock(&adapter->erp_lock);
	read_unlock_irqrestore(&zfcp_data.config_lock, flags);
}
L
Linus Torvalds 已提交
600

601 602 603 604 605 606 607 608
static int zfcp_erp_adapter_strategy_open_qdio(struct zfcp_erp_action *act)
{
	if (zfcp_qdio_open(act->adapter))
		return ZFCP_ERP_FAILED;
	init_waitqueue_head(&act->adapter->request_wq);
	atomic_set_mask(ZFCP_STATUS_ADAPTER_QDIOUP, &act->adapter->status);
	return ZFCP_ERP_SUCCEEDED;
}
L
Linus Torvalds 已提交
609

610 611 612 613 614 615 616 617 618
static void zfcp_erp_enqueue_ptp_port(struct zfcp_adapter *adapter)
{
	struct zfcp_port *port;
	port = zfcp_port_enqueue(adapter, adapter->peer_wwpn, 0,
				 adapter->peer_d_id);
	if (IS_ERR(port)) /* error or port already attached */
		return;
	_zfcp_erp_port_reopen(port, 0, 150, NULL);
}
L
Linus Torvalds 已提交
619

620 621 622 623 624
static int zfcp_erp_adapter_strat_fsf_xconf(struct zfcp_erp_action *erp_action)
{
	int retries;
	int sleep = 1;
	struct zfcp_adapter *adapter = erp_action->adapter;
L
Linus Torvalds 已提交
625

626 627 628 629 630 631 632 633 634 635 636 637
	atomic_clear_mask(ZFCP_STATUS_ADAPTER_XCONFIG_OK, &adapter->status);

	for (retries = 7; retries; retries--) {
		atomic_clear_mask(ZFCP_STATUS_ADAPTER_HOST_CON_INIT,
				  &adapter->status);
		write_lock_irq(&adapter->erp_lock);
		zfcp_erp_action_to_running(erp_action);
		write_unlock_irq(&adapter->erp_lock);
		if (zfcp_fsf_exchange_config_data(erp_action)) {
			atomic_clear_mask(ZFCP_STATUS_ADAPTER_HOST_CON_INIT,
					  &adapter->status);
			return ZFCP_ERP_FAILED;
L
Linus Torvalds 已提交
638 639
		}

640 641 642 643 644
		zfcp_rec_dbf_event_thread_lock(6, adapter);
		down(&adapter->erp_ready_sem);
		zfcp_rec_dbf_event_thread_lock(7, adapter);
		if (erp_action->status & ZFCP_STATUS_ERP_TIMEDOUT)
			break;
L
Linus Torvalds 已提交
645

646 647 648
		if (!(atomic_read(&adapter->status) &
		      ZFCP_STATUS_ADAPTER_HOST_CON_INIT))
			break;
L
Linus Torvalds 已提交
649

650 651
		ssleep(sleep);
		sleep *= 2;
L
Linus Torvalds 已提交
652 653
	}

654 655
	atomic_clear_mask(ZFCP_STATUS_ADAPTER_HOST_CON_INIT,
			  &adapter->status);
L
Linus Torvalds 已提交
656

657 658
	if (!(atomic_read(&adapter->status) & ZFCP_STATUS_ADAPTER_XCONFIG_OK))
		return ZFCP_ERP_FAILED;
S
Swen Schillig 已提交
659

660 661
	if (fc_host_port_type(adapter->scsi_host) == FC_PORTTYPE_PTP)
		zfcp_erp_enqueue_ptp_port(adapter);
L
Linus Torvalds 已提交
662

663
	return ZFCP_ERP_SUCCEEDED;
L
Linus Torvalds 已提交
664 665
}

666
static int zfcp_erp_adapter_strategy_open_fsf_xport(struct zfcp_erp_action *act)
L
Linus Torvalds 已提交
667
{
668 669
	int ret;
	struct zfcp_adapter *adapter = act->adapter;
L
Linus Torvalds 已提交
670

671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687
	write_lock_irq(&adapter->erp_lock);
	zfcp_erp_action_to_running(act);
	write_unlock_irq(&adapter->erp_lock);

	ret = zfcp_fsf_exchange_port_data(act);
	if (ret == -EOPNOTSUPP)
		return ZFCP_ERP_SUCCEEDED;
	if (ret)
		return ZFCP_ERP_FAILED;

	zfcp_rec_dbf_event_thread_lock(8, adapter);
	down(&adapter->erp_ready_sem);
	zfcp_rec_dbf_event_thread_lock(9, adapter);
	if (act->status & ZFCP_STATUS_ERP_TIMEDOUT)
		return ZFCP_ERP_FAILED;

	return ZFCP_ERP_SUCCEEDED;
L
Linus Torvalds 已提交
688 689
}

690
static int zfcp_erp_adapter_strategy_open_fsf(struct zfcp_erp_action *act)
L
Linus Torvalds 已提交
691
{
692 693
	if (zfcp_erp_adapter_strat_fsf_xconf(act) == ZFCP_ERP_FAILED)
		return ZFCP_ERP_FAILED;
L
Linus Torvalds 已提交
694

695 696
	if (zfcp_erp_adapter_strategy_open_fsf_xport(act) == ZFCP_ERP_FAILED)
		return ZFCP_ERP_FAILED;
L
Linus Torvalds 已提交
697

698 699 700
	atomic_set(&act->adapter->stat_miss, 16);
	if (zfcp_status_read_refill(act->adapter))
		return ZFCP_ERP_FAILED;
L
Linus Torvalds 已提交
701

702 703
	return ZFCP_ERP_SUCCEEDED;
}
L
Linus Torvalds 已提交
704

705 706 707 708 709
static int zfcp_erp_adapter_strategy_generic(struct zfcp_erp_action *act,
					     int close)
{
	int retval = ZFCP_ERP_SUCCEEDED;
	struct zfcp_adapter *adapter = act->adapter;
L
Linus Torvalds 已提交
710

711 712 713 714 715 716
	if (close)
		goto close_only;

	retval = zfcp_erp_adapter_strategy_open_qdio(act);
	if (retval != ZFCP_ERP_SUCCEEDED)
		goto failed_qdio;
L
Linus Torvalds 已提交
717

718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740
	retval = zfcp_erp_adapter_strategy_open_fsf(act);
	if (retval != ZFCP_ERP_SUCCEEDED)
		goto failed_openfcp;

	atomic_set_mask(ZFCP_STATUS_COMMON_OPEN, &act->adapter->status);
	schedule_work(&act->adapter->scan_work);

	return ZFCP_ERP_SUCCEEDED;

 close_only:
	atomic_clear_mask(ZFCP_STATUS_COMMON_OPEN,
			  &act->adapter->status);

 failed_openfcp:
	/* close queues to ensure that buffers are not accessed by adapter */
	zfcp_qdio_close(adapter);
	zfcp_fsf_req_dismiss_all(adapter);
	adapter->fsf_req_seq_no = 0;
	/* all ports and units are closed */
	zfcp_erp_modify_adapter_status(adapter, 24, NULL,
				       ZFCP_STATUS_COMMON_OPEN, ZFCP_CLEAR);
 failed_qdio:
	atomic_clear_mask(ZFCP_STATUS_ADAPTER_XCONFIG_OK |
741
			  ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED,
742
			  &act->adapter->status);
L
Linus Torvalds 已提交
743 744 745
	return retval;
}

746
static int zfcp_erp_adapter_strategy(struct zfcp_erp_action *act)
L
Linus Torvalds 已提交
747
{
748
	int retval;
L
Linus Torvalds 已提交
749

750 751 752 753 754 755 756 757
	zfcp_erp_adapter_strategy_generic(act, 1); /* close */
	if (act->status & ZFCP_STATUS_ERP_CLOSE_ONLY)
		return ZFCP_ERP_EXIT;

	retval = zfcp_erp_adapter_strategy_generic(act, 0); /* open */

	if (retval == ZFCP_ERP_FAILED)
		ssleep(8);
L
Linus Torvalds 已提交
758 759 760 761

	return retval;
}

762
static int zfcp_erp_port_forced_strategy_close(struct zfcp_erp_action *act)
L
Linus Torvalds 已提交
763
{
764 765 766 767 768 769 770 771 772 773
	int retval;

	retval = zfcp_fsf_close_physical_port(act);
	if (retval == -ENOMEM)
		return ZFCP_ERP_NOMEM;
	act->step = ZFCP_ERP_STEP_PHYS_PORT_CLOSING;
	if (retval)
		return ZFCP_ERP_FAILED;

	return ZFCP_ERP_CONTINUES;
L
Linus Torvalds 已提交
774 775
}

776
static void zfcp_erp_port_strategy_clearstati(struct zfcp_port *port)
L
Linus Torvalds 已提交
777
{
778
	atomic_clear_mask(ZFCP_STATUS_COMMON_ACCESS_DENIED |
779 780 781 782
			  ZFCP_STATUS_PORT_PHYS_CLOSING |
			  ZFCP_STATUS_PORT_INVALID_WWPN,
			  &port->status);
}
L
Linus Torvalds 已提交
783

784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802
static int zfcp_erp_port_forced_strategy(struct zfcp_erp_action *erp_action)
{
	struct zfcp_port *port = erp_action->port;
	int status = atomic_read(&port->status);

	switch (erp_action->step) {
	case ZFCP_ERP_STEP_UNINITIALIZED:
		zfcp_erp_port_strategy_clearstati(port);
		if ((status & ZFCP_STATUS_PORT_PHYS_OPEN) &&
		    (status & ZFCP_STATUS_COMMON_OPEN))
			return zfcp_erp_port_forced_strategy_close(erp_action);
		else
			return ZFCP_ERP_FAILED;

	case ZFCP_ERP_STEP_PHYS_PORT_CLOSING:
		if (status & ZFCP_STATUS_PORT_PHYS_OPEN)
			return ZFCP_ERP_SUCCEEDED;
	}
	return ZFCP_ERP_FAILED;
L
Linus Torvalds 已提交
803 804
}

805
static int zfcp_erp_port_strategy_close(struct zfcp_erp_action *erp_action)
L
Linus Torvalds 已提交
806
{
807
	int retval;
L
Linus Torvalds 已提交
808

809 810 811 812 813 814 815
	retval = zfcp_fsf_close_port(erp_action);
	if (retval == -ENOMEM)
		return ZFCP_ERP_NOMEM;
	erp_action->step = ZFCP_ERP_STEP_PORT_CLOSING;
	if (retval)
		return ZFCP_ERP_FAILED;
	return ZFCP_ERP_CONTINUES;
L
Linus Torvalds 已提交
816 817
}

818
static int zfcp_erp_port_strategy_open_port(struct zfcp_erp_action *erp_action)
L
Linus Torvalds 已提交
819
{
820
	int retval;
L
Linus Torvalds 已提交
821

822 823 824 825 826 827 828 829
	retval = zfcp_fsf_open_port(erp_action);
	if (retval == -ENOMEM)
		return ZFCP_ERP_NOMEM;
	erp_action->step = ZFCP_ERP_STEP_PORT_OPENING;
	if (retval)
		return ZFCP_ERP_FAILED;
	return ZFCP_ERP_CONTINUES;
}
L
Linus Torvalds 已提交
830

831
static int zfcp_erp_open_ptp_port(struct zfcp_erp_action *act)
L
Linus Torvalds 已提交
832
{
833 834
	struct zfcp_adapter *adapter = act->adapter;
	struct zfcp_port *port = act->port;
L
Linus Torvalds 已提交
835

836 837 838 839 840 841 842 843 844
	if (port->wwpn != adapter->peer_wwpn) {
		zfcp_erp_port_failed(port, 25, NULL);
		return ZFCP_ERP_FAILED;
	}
	port->d_id = adapter->peer_d_id;
	atomic_set_mask(ZFCP_STATUS_PORT_DID_DID, &port->status);
	return zfcp_erp_port_strategy_open_port(act);
}

845 846 847 848 849 850 851 852 853 854 855 856 857 858 859
void zfcp_erp_port_strategy_open_lookup(struct work_struct *work)
{
	int retval;
	struct zfcp_port *port = container_of(work, struct zfcp_port,
					      gid_pn_work);

	retval = zfcp_fc_ns_gid_pn(&port->erp_action);
	if (retval == -ENOMEM)
		zfcp_erp_notify(&port->erp_action, ZFCP_ERP_NOMEM);
	port->erp_action.step = ZFCP_ERP_STEP_NAMESERVER_LOOKUP;
	if (retval)
		zfcp_erp_notify(&port->erp_action, ZFCP_ERP_FAILED);

}

860 861 862 863 864 865 866 867 868 869 870 871
static int zfcp_erp_port_strategy_open_common(struct zfcp_erp_action *act)
{
	struct zfcp_adapter *adapter = act->adapter;
	struct zfcp_port *port = act->port;
	int p_status = atomic_read(&port->status);

	switch (act->step) {
	case ZFCP_ERP_STEP_UNINITIALIZED:
	case ZFCP_ERP_STEP_PHYS_PORT_CLOSING:
	case ZFCP_ERP_STEP_PORT_CLOSING:
		if (fc_host_port_type(adapter->scsi_host) == FC_PORTTYPE_PTP)
			return zfcp_erp_open_ptp_port(act);
872
		if (!(p_status & ZFCP_STATUS_PORT_DID_DID)) {
873
			queue_work(zfcp_data.work_queue, &port->gid_pn_work);
874
			return ZFCP_ERP_CONTINUES;
875 876 877 878 879 880 881 882
		}
	case ZFCP_ERP_STEP_NAMESERVER_LOOKUP:
		if (!(p_status & ZFCP_STATUS_PORT_DID_DID)) {
			if (p_status & (ZFCP_STATUS_PORT_INVALID_WWPN)) {
				zfcp_erp_port_failed(port, 26, NULL);
				return ZFCP_ERP_EXIT;
			}
			return ZFCP_ERP_FAILED;
L
Linus Torvalds 已提交
883
		}
884 885 886 887
		return zfcp_erp_port_strategy_open_port(act);

	case ZFCP_ERP_STEP_PORT_OPENING:
		/* D_ID might have changed during open */
888 889 890 891 892 893 894
		if (p_status & ZFCP_STATUS_COMMON_OPEN) {
			if (p_status & ZFCP_STATUS_PORT_DID_DID)
				return ZFCP_ERP_SUCCEEDED;
			else {
				act->step = ZFCP_ERP_STEP_PORT_CLOSING;
				return ZFCP_ERP_CONTINUES;
			}
895
		/* fall through otherwise */
896
		}
897 898 899 900 901 902 903 904
	}
	return ZFCP_ERP_FAILED;
}

static int zfcp_erp_port_strategy(struct zfcp_erp_action *erp_action)
{
	struct zfcp_port *port = erp_action->port;

905 906 907
	if (atomic_read(&port->status) & ZFCP_STATUS_COMMON_NOESC)
		goto close_init_done;

908 909 910 911 912
	switch (erp_action->step) {
	case ZFCP_ERP_STEP_UNINITIALIZED:
		zfcp_erp_port_strategy_clearstati(port);
		if (atomic_read(&port->status) & ZFCP_STATUS_COMMON_OPEN)
			return zfcp_erp_port_strategy_close(erp_action);
L
Linus Torvalds 已提交
913 914
		break;

915 916 917
	case ZFCP_ERP_STEP_PORT_CLOSING:
		if (atomic_read(&port->status) & ZFCP_STATUS_COMMON_OPEN)
			return ZFCP_ERP_FAILED;
L
Linus Torvalds 已提交
918 919
		break;
	}
920 921

close_init_done:
922 923
	if (erp_action->status & ZFCP_STATUS_ERP_CLOSE_ONLY)
		return ZFCP_ERP_EXIT;
L
Linus Torvalds 已提交
924

925
	return zfcp_erp_port_strategy_open_common(erp_action);
926 927 928 929
}

static void zfcp_erp_unit_strategy_clearstati(struct zfcp_unit *unit)
{
930
	atomic_clear_mask(ZFCP_STATUS_COMMON_ACCESS_DENIED |
931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955
			  ZFCP_STATUS_UNIT_SHARED |
			  ZFCP_STATUS_UNIT_READONLY,
			  &unit->status);
}

static int zfcp_erp_unit_strategy_close(struct zfcp_erp_action *erp_action)
{
	int retval = zfcp_fsf_close_unit(erp_action);
	if (retval == -ENOMEM)
		return ZFCP_ERP_NOMEM;
	erp_action->step = ZFCP_ERP_STEP_UNIT_CLOSING;
	if (retval)
		return ZFCP_ERP_FAILED;
	return ZFCP_ERP_CONTINUES;
}

static int zfcp_erp_unit_strategy_open(struct zfcp_erp_action *erp_action)
{
	int retval = zfcp_fsf_open_unit(erp_action);
	if (retval == -ENOMEM)
		return ZFCP_ERP_NOMEM;
	erp_action->step = ZFCP_ERP_STEP_UNIT_OPENING;
	if (retval)
		return  ZFCP_ERP_FAILED;
	return ZFCP_ERP_CONTINUES;
L
Linus Torvalds 已提交
956 957
}

958
static int zfcp_erp_unit_strategy(struct zfcp_erp_action *erp_action)
L
Linus Torvalds 已提交
959
{
960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979
	struct zfcp_unit *unit = erp_action->unit;

	switch (erp_action->step) {
	case ZFCP_ERP_STEP_UNINITIALIZED:
		zfcp_erp_unit_strategy_clearstati(unit);
		if (atomic_read(&unit->status) & ZFCP_STATUS_COMMON_OPEN)
			return zfcp_erp_unit_strategy_close(erp_action);
		/* already closed, fall through */
	case ZFCP_ERP_STEP_UNIT_CLOSING:
		if (atomic_read(&unit->status) & ZFCP_STATUS_COMMON_OPEN)
			return ZFCP_ERP_FAILED;
		if (erp_action->status & ZFCP_STATUS_ERP_CLOSE_ONLY)
			return ZFCP_ERP_EXIT;
		return zfcp_erp_unit_strategy_open(erp_action);

	case ZFCP_ERP_STEP_UNIT_OPENING:
		if (atomic_read(&unit->status) & ZFCP_STATUS_COMMON_OPEN)
			return ZFCP_ERP_SUCCEEDED;
	}
	return ZFCP_ERP_FAILED;
L
Linus Torvalds 已提交
980 981
}

982
static int zfcp_erp_strategy_check_unit(struct zfcp_unit *unit, int result)
L
Linus Torvalds 已提交
983 984 985 986 987 988 989 990
{
	switch (result) {
	case ZFCP_ERP_SUCCEEDED :
		atomic_set(&unit->erp_counter, 0);
		zfcp_erp_unit_unblock(unit);
		break;
	case ZFCP_ERP_FAILED :
		atomic_inc(&unit->erp_counter);
991 992 993 994
		if (atomic_read(&unit->erp_counter) > ZFCP_MAX_ERPS) {
			dev_err(&unit->port->adapter->ccw_device->dev,
				"ERP failed for unit 0x%016Lx on "
				"port 0x%016Lx\n",
995 996
				(unsigned long long)unit->fcp_lun,
				(unsigned long long)unit->port->wwpn);
997
			zfcp_erp_unit_failed(unit, 21, NULL);
998
		}
L
Linus Torvalds 已提交
999 1000 1001
		break;
	}

1002 1003
	if (atomic_read(&unit->status) & ZFCP_STATUS_COMMON_ERP_FAILED) {
		zfcp_erp_unit_block(unit, 0);
L
Linus Torvalds 已提交
1004 1005 1006 1007 1008
		result = ZFCP_ERP_EXIT;
	}
	return result;
}

1009
static int zfcp_erp_strategy_check_port(struct zfcp_port *port, int result)
L
Linus Torvalds 已提交
1010 1011 1012 1013 1014 1015
{
	switch (result) {
	case ZFCP_ERP_SUCCEEDED :
		atomic_set(&port->erp_counter, 0);
		zfcp_erp_port_unblock(port);
		break;
1016

L
Linus Torvalds 已提交
1017
	case ZFCP_ERP_FAILED :
1018
		if (atomic_read(&port->status) & ZFCP_STATUS_COMMON_NOESC) {
1019 1020 1021
			zfcp_erp_port_block(port, 0);
			result = ZFCP_ERP_EXIT;
		}
L
Linus Torvalds 已提交
1022
		atomic_inc(&port->erp_counter);
1023 1024 1025
		if (atomic_read(&port->erp_counter) > ZFCP_MAX_ERPS) {
			dev_err(&port->adapter->ccw_device->dev,
				"ERP failed for remote port 0x%016Lx\n",
1026
				(unsigned long long)port->wwpn);
1027
			zfcp_erp_port_failed(port, 22, NULL);
1028
		}
L
Linus Torvalds 已提交
1029 1030 1031
		break;
	}

1032 1033
	if (atomic_read(&port->status) & ZFCP_STATUS_COMMON_ERP_FAILED) {
		zfcp_erp_port_block(port, 0);
L
Linus Torvalds 已提交
1034 1035 1036 1037 1038
		result = ZFCP_ERP_EXIT;
	}
	return result;
}

1039 1040
static int zfcp_erp_strategy_check_adapter(struct zfcp_adapter *adapter,
					   int result)
L
Linus Torvalds 已提交
1041 1042 1043 1044 1045 1046
{
	switch (result) {
	case ZFCP_ERP_SUCCEEDED :
		atomic_set(&adapter->erp_counter, 0);
		zfcp_erp_adapter_unblock(adapter);
		break;
1047

L
Linus Torvalds 已提交
1048 1049
	case ZFCP_ERP_FAILED :
		atomic_inc(&adapter->erp_counter);
1050 1051 1052 1053
		if (atomic_read(&adapter->erp_counter) > ZFCP_MAX_ERPS) {
			dev_err(&adapter->ccw_device->dev,
				"ERP cannot recover an error "
				"on the FCP device\n");
1054
			zfcp_erp_adapter_failed(adapter, 23, NULL);
1055
		}
L
Linus Torvalds 已提交
1056 1057 1058
		break;
	}

1059 1060
	if (atomic_read(&adapter->status) & ZFCP_STATUS_COMMON_ERP_FAILED) {
		zfcp_erp_adapter_block(adapter, 0);
L
Linus Torvalds 已提交
1061 1062 1063 1064 1065
		result = ZFCP_ERP_EXIT;
	}
	return result;
}

1066 1067
static int zfcp_erp_strategy_check_target(struct zfcp_erp_action *erp_action,
					  int result)
1068
{
1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088
	struct zfcp_adapter *adapter = erp_action->adapter;
	struct zfcp_port *port = erp_action->port;
	struct zfcp_unit *unit = erp_action->unit;

	switch (erp_action->action) {

	case ZFCP_ERP_ACTION_REOPEN_UNIT:
		result = zfcp_erp_strategy_check_unit(unit, result);
		break;

	case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
	case ZFCP_ERP_ACTION_REOPEN_PORT:
		result = zfcp_erp_strategy_check_port(port, result);
		break;

	case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
		result = zfcp_erp_strategy_check_adapter(adapter, result);
		break;
	}
	return result;
1089 1090
}

1091
static int zfcp_erp_strat_change_det(atomic_t *target_status, u32 erp_status)
1092
{
1093
	int status = atomic_read(target_status);
1094

1095 1096 1097
	if ((status & ZFCP_STATUS_COMMON_RUNNING) &&
	    (erp_status & ZFCP_STATUS_ERP_CLOSE_ONLY))
		return 1; /* take it online */
1098

1099 1100 1101 1102 1103
	if (!(status & ZFCP_STATUS_COMMON_RUNNING) &&
	    !(erp_status & ZFCP_STATUS_ERP_CLOSE_ONLY))
		return 1; /* take it offline */

	return 0;
1104 1105
}

1106
static int zfcp_erp_strategy_statechange(struct zfcp_erp_action *act, int ret)
L
Linus Torvalds 已提交
1107
{
1108 1109 1110 1111 1112
	int action = act->action;
	struct zfcp_adapter *adapter = act->adapter;
	struct zfcp_port *port = act->port;
	struct zfcp_unit *unit = act->unit;
	u32 erp_status = act->status;
L
Linus Torvalds 已提交
1113

1114
	switch (action) {
L
Linus Torvalds 已提交
1115
	case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
1116 1117 1118 1119 1120 1121
		if (zfcp_erp_strat_change_det(&adapter->status, erp_status)) {
			_zfcp_erp_adapter_reopen(adapter,
						 ZFCP_STATUS_COMMON_ERP_FAILED,
						 67, NULL);
			return ZFCP_ERP_EXIT;
		}
L
Linus Torvalds 已提交
1122 1123 1124 1125
		break;

	case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
	case ZFCP_ERP_ACTION_REOPEN_PORT:
1126 1127 1128 1129 1130 1131
		if (zfcp_erp_strat_change_det(&port->status, erp_status)) {
			_zfcp_erp_port_reopen(port,
					      ZFCP_STATUS_COMMON_ERP_FAILED,
					      68, NULL);
			return ZFCP_ERP_EXIT;
		}
L
Linus Torvalds 已提交
1132 1133 1134
		break;

	case ZFCP_ERP_ACTION_REOPEN_UNIT:
1135 1136 1137 1138 1139 1140
		if (zfcp_erp_strat_change_det(&unit->status, erp_status)) {
			_zfcp_erp_unit_reopen(unit,
					      ZFCP_STATUS_COMMON_ERP_FAILED,
					      69, NULL);
			return ZFCP_ERP_EXIT;
		}
L
Linus Torvalds 已提交
1141 1142
		break;
	}
1143
	return ret;
L
Linus Torvalds 已提交
1144 1145
}

1146
static void zfcp_erp_action_dequeue(struct zfcp_erp_action *erp_action)
L
Linus Torvalds 已提交
1147
{
1148
	struct zfcp_adapter *adapter = erp_action->adapter;
L
Linus Torvalds 已提交
1149

1150 1151 1152 1153
	adapter->erp_total_count--;
	if (erp_action->status & ZFCP_STATUS_ERP_LOWMEM) {
		adapter->erp_low_mem_count--;
		erp_action->status &= ~ZFCP_STATUS_ERP_LOWMEM;
1154
	}
L
Linus Torvalds 已提交
1155

1156 1157
	list_del(&erp_action->list);
	zfcp_rec_dbf_event_action(144, erp_action);
L
Linus Torvalds 已提交
1158

1159 1160 1161 1162 1163
	switch (erp_action->action) {
	case ZFCP_ERP_ACTION_REOPEN_UNIT:
		atomic_clear_mask(ZFCP_STATUS_COMMON_ERP_INUSE,
				  &erp_action->unit->status);
		break;
L
Linus Torvalds 已提交
1164

1165 1166 1167 1168 1169
	case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
	case ZFCP_ERP_ACTION_REOPEN_PORT:
		atomic_clear_mask(ZFCP_STATUS_COMMON_ERP_INUSE,
				  &erp_action->port->status);
		break;
L
Linus Torvalds 已提交
1170

1171 1172 1173 1174 1175
	case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
		atomic_clear_mask(ZFCP_STATUS_COMMON_ERP_INUSE,
				  &erp_action->adapter->status);
		break;
	}
L
Linus Torvalds 已提交
1176 1177
}

1178 1179 1180 1181
struct zfcp_erp_add_work {
	struct zfcp_unit  *unit;
	struct work_struct work;
};
L
Linus Torvalds 已提交
1182

1183 1184 1185 1186 1187 1188
static void zfcp_erp_scsi_scan(struct work_struct *work)
{
	struct zfcp_erp_add_work *p =
		container_of(work, struct zfcp_erp_add_work, work);
	struct zfcp_unit *unit = p->unit;
	struct fc_rport *rport = unit->port->rport;
1189 1190 1191

	if (rport && rport->port_state == FC_PORTSTATE_ONLINE)
		scsi_scan_target(&rport->dev, 0, rport->scsi_target_id,
1192
			 scsilun_to_int((struct scsi_lun *)&unit->fcp_lun), 0);
1193 1194
	atomic_clear_mask(ZFCP_STATUS_UNIT_SCSI_WORK_PENDING, &unit->status);
	zfcp_unit_put(unit);
1195
	wake_up(&unit->port->adapter->erp_done_wqh);
1196
	kfree(p);
L
Linus Torvalds 已提交
1197 1198
}

1199
static void zfcp_erp_schedule_work(struct zfcp_unit *unit)
L
Linus Torvalds 已提交
1200
{
1201
	struct zfcp_erp_add_work *p;
L
Linus Torvalds 已提交
1202

1203 1204 1205
	p = kzalloc(sizeof(*p), GFP_KERNEL);
	if (!p) {
		dev_err(&unit->port->adapter->ccw_device->dev,
1206
			"Registering unit 0x%016Lx on port 0x%016Lx failed\n",
1207 1208
			(unsigned long long)unit->fcp_lun,
			(unsigned long long)unit->port->wwpn);
1209
		return;
L
Linus Torvalds 已提交
1210 1211
	}

1212 1213 1214 1215
	zfcp_unit_get(unit);
	atomic_set_mask(ZFCP_STATUS_UNIT_SCSI_WORK_PENDING, &unit->status);
	INIT_WORK(&p->work, zfcp_erp_scsi_scan);
	p->unit = unit;
1216
	queue_work(zfcp_data.work_queue, &p->work);
L
Linus Torvalds 已提交
1217 1218
}

1219
static void zfcp_erp_rport_register(struct zfcp_port *port)
L
Linus Torvalds 已提交
1220
{
1221 1222 1223 1224 1225 1226 1227 1228
	struct fc_rport_identifiers ids;
	ids.node_name = port->wwnn;
	ids.port_name = port->wwpn;
	ids.port_id = port->d_id;
	ids.roles = FC_RPORT_ROLE_FCP_TARGET;
	port->rport = fc_remote_port_add(port->adapter->scsi_host, 0, &ids);
	if (!port->rport) {
		dev_err(&port->adapter->ccw_device->dev,
1229
			"Registering port 0x%016Lx failed\n",
1230
			(unsigned long long)port->wwpn);
1231
		return;
L
Linus Torvalds 已提交
1232 1233
	}

1234 1235 1236
	scsi_target_unblock(&port->rport->dev);
	port->rport->maxframe_size = port->maxframe_size;
	port->rport->supported_classes = port->supported_classes;
L
Linus Torvalds 已提交
1237 1238
}

1239
static void zfcp_erp_rports_del(struct zfcp_adapter *adapter)
L
Linus Torvalds 已提交
1240 1241
{
	struct zfcp_port *port;
1242
	list_for_each_entry(port, &adapter->port_list_head, list) {
1243 1244
		if (!port->rport)
			continue;
1245 1246 1247
		fc_remote_port_delete(port->rport);
		port->rport = NULL;
	}
L
Linus Torvalds 已提交
1248 1249
}

1250
static void zfcp_erp_action_cleanup(struct zfcp_erp_action *act, int result)
L
Linus Torvalds 已提交
1251
{
1252 1253 1254
	struct zfcp_adapter *adapter = act->adapter;
	struct zfcp_port *port = act->port;
	struct zfcp_unit *unit = act->unit;
L
Linus Torvalds 已提交
1255

1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267
	switch (act->action) {
	case ZFCP_ERP_ACTION_REOPEN_UNIT:
		if ((result == ZFCP_ERP_SUCCEEDED) &&
		    !unit->device && port->rport) {
			atomic_set_mask(ZFCP_STATUS_UNIT_REGISTERED,
					&unit->status);
			if (!(atomic_read(&unit->status) &
			      ZFCP_STATUS_UNIT_SCSI_WORK_PENDING))
				zfcp_erp_schedule_work(unit);
		}
		zfcp_unit_put(unit);
		break;
L
Linus Torvalds 已提交
1268

1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289
	case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
	case ZFCP_ERP_ACTION_REOPEN_PORT:
		if (atomic_read(&port->status) & ZFCP_STATUS_PORT_NO_WWPN) {
			zfcp_port_put(port);
			return;
		}
		if ((result == ZFCP_ERP_SUCCEEDED) && !port->rport)
			zfcp_erp_rport_register(port);
		if ((result != ZFCP_ERP_SUCCEEDED) && port->rport) {
			fc_remote_port_delete(port->rport);
			port->rport = NULL;
		}
		zfcp_port_put(port);
		break;

	case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
		if (result != ZFCP_ERP_SUCCEEDED)
			zfcp_erp_rports_del(adapter);
		zfcp_adapter_put(adapter);
		break;
	}
L
Linus Torvalds 已提交
1290 1291
}

1292
static int zfcp_erp_strategy_do_action(struct zfcp_erp_action *erp_action)
L
Linus Torvalds 已提交
1293
{
1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304
	switch (erp_action->action) {
	case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
		return zfcp_erp_adapter_strategy(erp_action);
	case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
		return zfcp_erp_port_forced_strategy(erp_action);
	case ZFCP_ERP_ACTION_REOPEN_PORT:
		return zfcp_erp_port_strategy(erp_action);
	case ZFCP_ERP_ACTION_REOPEN_UNIT:
		return zfcp_erp_unit_strategy(erp_action);
	}
	return ZFCP_ERP_FAILED;
L
Linus Torvalds 已提交
1305 1306
}

1307
static int zfcp_erp_strategy(struct zfcp_erp_action *erp_action)
L
Linus Torvalds 已提交
1308 1309
{
	int retval;
1310 1311
	struct zfcp_adapter *adapter = erp_action->adapter;
	unsigned long flags;
L
Linus Torvalds 已提交
1312

1313 1314
	read_lock_irqsave(&zfcp_data.config_lock, flags);
	write_lock(&adapter->erp_lock);
L
Linus Torvalds 已提交
1315

1316
	zfcp_erp_strategy_check_fsfreq(erp_action);
L
Linus Torvalds 已提交
1317

1318 1319 1320 1321
	if (erp_action->status & ZFCP_STATUS_ERP_DISMISSED) {
		zfcp_erp_action_dequeue(erp_action);
		retval = ZFCP_ERP_DISMISSED;
		goto unlock;
1322
	}
L
Linus Torvalds 已提交
1323

1324
	zfcp_erp_action_to_running(erp_action);
L
Linus Torvalds 已提交
1325

1326 1327 1328 1329 1330 1331
	/* no lock to allow for blocking operations */
	write_unlock(&adapter->erp_lock);
	read_unlock_irqrestore(&zfcp_data.config_lock, flags);
	retval = zfcp_erp_strategy_do_action(erp_action);
	read_lock_irqsave(&zfcp_data.config_lock, flags);
	write_lock(&adapter->erp_lock);
L
Linus Torvalds 已提交
1332

1333 1334
	if (erp_action->status & ZFCP_STATUS_ERP_DISMISSED)
		retval = ZFCP_ERP_CONTINUES;
1335

1336 1337 1338 1339 1340
	switch (retval) {
	case ZFCP_ERP_NOMEM:
		if (!(erp_action->status & ZFCP_STATUS_ERP_LOWMEM)) {
			++adapter->erp_low_mem_count;
			erp_action->status |= ZFCP_STATUS_ERP_LOWMEM;
L
Linus Torvalds 已提交
1341
		}
1342 1343 1344 1345 1346
		if (adapter->erp_total_count == adapter->erp_low_mem_count)
			_zfcp_erp_adapter_reopen(adapter, 0, 66, NULL);
		else {
			zfcp_erp_strategy_memwait(erp_action);
			retval = ZFCP_ERP_CONTINUES;
L
Linus Torvalds 已提交
1347
		}
1348
		goto unlock;
L
Linus Torvalds 已提交
1349

1350 1351 1352 1353
	case ZFCP_ERP_CONTINUES:
		if (erp_action->status & ZFCP_STATUS_ERP_LOWMEM) {
			--adapter->erp_low_mem_count;
			erp_action->status &= ~ZFCP_STATUS_ERP_LOWMEM;
L
Linus Torvalds 已提交
1354
		}
1355
		goto unlock;
L
Linus Torvalds 已提交
1356 1357
	}

1358 1359 1360 1361 1362 1363
	retval = zfcp_erp_strategy_check_target(erp_action, retval);
	zfcp_erp_action_dequeue(erp_action);
	retval = zfcp_erp_strategy_statechange(erp_action, retval);
	if (retval == ZFCP_ERP_EXIT)
		goto unlock;
	zfcp_erp_strategy_followup_actions(erp_action);
L
Linus Torvalds 已提交
1364

1365 1366 1367
 unlock:
	write_unlock(&adapter->erp_lock);
	read_unlock_irqrestore(&zfcp_data.config_lock, flags);
L
Linus Torvalds 已提交
1368

1369 1370
	if (retval != ZFCP_ERP_CONTINUES)
		zfcp_erp_action_cleanup(erp_action, retval);
L
Linus Torvalds 已提交
1371 1372 1373 1374

	return retval;
}

1375
static int zfcp_erp_thread(void *data)
L
Linus Torvalds 已提交
1376
{
1377 1378 1379 1380
	struct zfcp_adapter *adapter = (struct zfcp_adapter *) data;
	struct list_head *next;
	struct zfcp_erp_action *act;
	unsigned long flags;
L
Linus Torvalds 已提交
1381

1382
	daemonize("zfcperp%s", dev_name(&adapter->ccw_device->dev));
1383 1384 1385 1386
	/* Block all signals */
	siginitsetinv(&current->blocked, 0);
	atomic_set_mask(ZFCP_STATUS_ADAPTER_ERP_THREAD_UP, &adapter->status);
	wake_up(&adapter->erp_thread_wqh);
L
Linus Torvalds 已提交
1387

1388 1389 1390 1391 1392
	while (!(atomic_read(&adapter->status) &
		 ZFCP_STATUS_ADAPTER_ERP_THREAD_KILL)) {
		write_lock_irqsave(&adapter->erp_lock, flags);
		next = adapter->erp_ready_head.next;
		write_unlock_irqrestore(&adapter->erp_lock, flags);
L
Linus Torvalds 已提交
1393

1394 1395
		if (next != &adapter->erp_ready_head) {
			act = list_entry(next, struct zfcp_erp_action, list);
L
Linus Torvalds 已提交
1396

1397 1398 1399
			/* there is more to come after dismission, no notify */
			if (zfcp_erp_strategy(act) != ZFCP_ERP_DISMISSED)
				zfcp_erp_wakeup(adapter);
L
Linus Torvalds 已提交
1400 1401
		}

1402
		zfcp_rec_dbf_event_thread_lock(4, adapter);
1403
		down_interruptible(&adapter->erp_ready_sem);
1404
		zfcp_rec_dbf_event_thread_lock(5, adapter);
L
Linus Torvalds 已提交
1405 1406
	}

1407 1408
	atomic_clear_mask(ZFCP_STATUS_ADAPTER_ERP_THREAD_UP, &adapter->status);
	wake_up(&adapter->erp_thread_wqh);
L
Linus Torvalds 已提交
1409

1410 1411
	return 0;
}
L
Linus Torvalds 已提交
1412

1413 1414 1415 1416 1417 1418 1419 1420 1421
/**
 * zfcp_erp_thread_setup - Start ERP thread for adapter
 * @adapter: Adapter to start the ERP thread for
 *
 * Returns 0 on success or error code from kernel_thread()
 */
int zfcp_erp_thread_setup(struct zfcp_adapter *adapter)
{
	int retval;
L
Linus Torvalds 已提交
1422

1423 1424 1425 1426
	atomic_clear_mask(ZFCP_STATUS_ADAPTER_ERP_THREAD_UP, &adapter->status);
	retval = kernel_thread(zfcp_erp_thread, adapter, SIGCHLD);
	if (retval < 0) {
		dev_err(&adapter->ccw_device->dev,
1427
			"Creating an ERP thread for the FCP device failed.\n");
1428
		return retval;
L
Linus Torvalds 已提交
1429
	}
1430 1431 1432 1433 1434
	wait_event(adapter->erp_thread_wqh,
		   atomic_read(&adapter->status) &
			ZFCP_STATUS_ADAPTER_ERP_THREAD_UP);
	return 0;
}
L
Linus Torvalds 已提交
1435

1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448
/**
 * zfcp_erp_thread_kill - Stop ERP thread.
 * @adapter: Adapter where the ERP thread should be stopped.
 *
 * The caller of this routine ensures that the specified adapter has
 * been shut down and that this operation has been completed. Thus,
 * there are no pending erp_actions which would need to be handled
 * here.
 */
void zfcp_erp_thread_kill(struct zfcp_adapter *adapter)
{
	atomic_set_mask(ZFCP_STATUS_ADAPTER_ERP_THREAD_KILL, &adapter->status);
	up(&adapter->erp_ready_sem);
1449
	zfcp_rec_dbf_event_thread_lock(3, adapter);
L
Linus Torvalds 已提交
1450

1451 1452 1453
	wait_event(adapter->erp_thread_wqh,
		   !(atomic_read(&adapter->status) &
				ZFCP_STATUS_ADAPTER_ERP_THREAD_UP));
L
Linus Torvalds 已提交
1454

1455 1456
	atomic_clear_mask(ZFCP_STATUS_ADAPTER_ERP_THREAD_KILL,
			  &adapter->status);
L
Linus Torvalds 已提交
1457 1458
}

1459 1460 1461 1462 1463 1464 1465
/**
 * zfcp_erp_adapter_failed - Set adapter status to failed.
 * @adapter: Failed adapter.
 * @id: Event id for debug trace.
 * @ref: Reference for debug trace.
 */
void zfcp_erp_adapter_failed(struct zfcp_adapter *adapter, u8 id, void *ref)
L
Linus Torvalds 已提交
1466
{
1467 1468 1469
	zfcp_erp_modify_adapter_status(adapter, id, ref,
				       ZFCP_STATUS_COMMON_ERP_FAILED, ZFCP_SET);
}
L
Linus Torvalds 已提交
1470

1471 1472 1473 1474 1475 1476 1477 1478 1479 1480
/**
 * zfcp_erp_port_failed - Set port status to failed.
 * @port: Failed port.
 * @id: Event id for debug trace.
 * @ref: Reference for debug trace.
 */
void zfcp_erp_port_failed(struct zfcp_port *port, u8 id, void *ref)
{
	zfcp_erp_modify_port_status(port, id, ref,
				    ZFCP_STATUS_COMMON_ERP_FAILED, ZFCP_SET);
L
Linus Torvalds 已提交
1481 1482 1483
}

/**
1484 1485 1486 1487
 * zfcp_erp_unit_failed - Set unit status to failed.
 * @unit: Failed unit.
 * @id: Event id for debug trace.
 * @ref: Reference for debug trace.
L
Linus Torvalds 已提交
1488
 */
1489
void zfcp_erp_unit_failed(struct zfcp_unit *unit, u8 id, void *ref)
L
Linus Torvalds 已提交
1490
{
1491 1492
	zfcp_erp_modify_unit_status(unit, id, ref,
				    ZFCP_STATUS_COMMON_ERP_FAILED, ZFCP_SET);
L
Linus Torvalds 已提交
1493 1494
}

1495 1496 1497 1498 1499 1500 1501 1502 1503 1504
/**
 * zfcp_erp_wait - wait for completion of error recovery on an adapter
 * @adapter: adapter for which to wait for completion of its error recovery
 */
void zfcp_erp_wait(struct zfcp_adapter *adapter)
{
	wait_event(adapter->erp_done_wqh,
		   !(atomic_read(&adapter->status) &
			ZFCP_STATUS_ADAPTER_ERP_PENDING));
}
L
Linus Torvalds 已提交
1505

1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517
/**
 * zfcp_erp_modify_adapter_status - change adapter status bits
 * @adapter: adapter to change the status
 * @id: id for the debug trace
 * @ref: reference for the debug trace
 * @mask: status bits to change
 * @set_or_clear: ZFCP_SET or ZFCP_CLEAR
 *
 * Changes in common status bits are propagated to attached ports and units.
 */
void zfcp_erp_modify_adapter_status(struct zfcp_adapter *adapter, u8 id,
				    void *ref, u32 mask, int set_or_clear)
L
Linus Torvalds 已提交
1518 1519
{
	struct zfcp_port *port;
1520
	u32 common_mask = mask & ZFCP_COMMON_FLAGS;
L
Linus Torvalds 已提交
1521

1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534
	if (set_or_clear == ZFCP_SET) {
		if (status_change_set(mask, &adapter->status))
			zfcp_rec_dbf_event_adapter(id, ref, adapter);
		atomic_set_mask(mask, &adapter->status);
	} else {
		if (status_change_clear(mask, &adapter->status))
			zfcp_rec_dbf_event_adapter(id, ref, adapter);
		atomic_clear_mask(mask, &adapter->status);
		if (mask & ZFCP_STATUS_COMMON_ERP_FAILED)
			atomic_set(&adapter->erp_counter, 0);
	}

	if (common_mask)
L
Linus Torvalds 已提交
1535
		list_for_each_entry(port, &adapter->port_list_head, list)
1536 1537
			zfcp_erp_modify_port_status(port, id, ref, common_mask,
						    set_or_clear);
L
Linus Torvalds 已提交
1538 1539
}

1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551
/**
 * zfcp_erp_modify_port_status - change port status bits
 * @port: port to change the status bits
 * @id: id for the debug trace
 * @ref: reference for the debug trace
 * @mask: status bits to change
 * @set_or_clear: ZFCP_SET or ZFCP_CLEAR
 *
 * Changes in common status bits are propagated to attached units.
 */
void zfcp_erp_modify_port_status(struct zfcp_port *port, u8 id, void *ref,
				 u32 mask, int set_or_clear)
L
Linus Torvalds 已提交
1552 1553
{
	struct zfcp_unit *unit;
1554
	u32 common_mask = mask & ZFCP_COMMON_FLAGS;
L
Linus Torvalds 已提交
1555

1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566
	if (set_or_clear == ZFCP_SET) {
		if (status_change_set(mask, &port->status))
			zfcp_rec_dbf_event_port(id, ref, port);
		atomic_set_mask(mask, &port->status);
	} else {
		if (status_change_clear(mask, &port->status))
			zfcp_rec_dbf_event_port(id, ref, port);
		atomic_clear_mask(mask, &port->status);
		if (mask & ZFCP_STATUS_COMMON_ERP_FAILED)
			atomic_set(&port->erp_counter, 0);
	}
L
Linus Torvalds 已提交
1567

1568 1569 1570 1571
	if (common_mask)
		list_for_each_entry(unit, &port->unit_list_head, list)
			zfcp_erp_modify_unit_status(unit, id, ref, common_mask,
						    set_or_clear);
L
Linus Torvalds 已提交
1572 1573
}

1574 1575 1576 1577 1578 1579 1580 1581 1582 1583
/**
 * zfcp_erp_modify_unit_status - change unit status bits
 * @unit: unit to change the status bits
 * @id: id for the debug trace
 * @ref: reference for the debug trace
 * @mask: status bits to change
 * @set_or_clear: ZFCP_SET or ZFCP_CLEAR
 */
void zfcp_erp_modify_unit_status(struct zfcp_unit *unit, u8 id, void *ref,
				 u32 mask, int set_or_clear)
L
Linus Torvalds 已提交
1584
{
1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596
	if (set_or_clear == ZFCP_SET) {
		if (status_change_set(mask, &unit->status))
			zfcp_rec_dbf_event_unit(id, ref, unit);
		atomic_set_mask(mask, &unit->status);
	} else {
		if (status_change_clear(mask, &unit->status))
			zfcp_rec_dbf_event_unit(id, ref, unit);
		atomic_clear_mask(mask, &unit->status);
		if (mask & ZFCP_STATUS_COMMON_ERP_FAILED) {
			atomic_set(&unit->erp_counter, 0);
		}
	}
L
Linus Torvalds 已提交
1597 1598
}

1599 1600 1601 1602 1603 1604
/**
 * zfcp_erp_port_boxed - Mark port as "boxed" and start ERP
 * @port: The "boxed" port.
 * @id: The debug trace id.
 * @id: Reference for the debug trace.
 */
1605
void zfcp_erp_port_boxed(struct zfcp_port *port, u8 id, void *ref)
1606 1607 1608 1609
{
	unsigned long flags;

	read_lock_irqsave(&zfcp_data.config_lock, flags);
1610 1611
	zfcp_erp_modify_port_status(port, id, ref,
				    ZFCP_STATUS_COMMON_ACCESS_BOXED, ZFCP_SET);
1612
	read_unlock_irqrestore(&zfcp_data.config_lock, flags);
1613
	zfcp_erp_port_reopen(port, ZFCP_STATUS_COMMON_ERP_FAILED, id, ref);
1614 1615
}

1616 1617 1618 1619 1620 1621
/**
 * zfcp_erp_unit_boxed - Mark unit as "boxed" and start ERP
 * @port: The "boxed" unit.
 * @id: The debug trace id.
 * @id: Reference for the debug trace.
 */
1622
void zfcp_erp_unit_boxed(struct zfcp_unit *unit, u8 id, void *ref)
1623
{
1624 1625
	zfcp_erp_modify_unit_status(unit, id, ref,
				    ZFCP_STATUS_COMMON_ACCESS_BOXED, ZFCP_SET);
1626
	zfcp_erp_unit_reopen(unit, ZFCP_STATUS_COMMON_ERP_FAILED, id, ref);
1627 1628
}

1629 1630 1631 1632 1633 1634 1635 1636 1637
/**
 * zfcp_erp_port_access_denied - Adapter denied access to port.
 * @port: port where access has been denied
 * @id: id for debug trace
 * @ref: reference for debug trace
 *
 * Since the adapter has denied access, stop using the port and the
 * attached units.
 */
1638
void zfcp_erp_port_access_denied(struct zfcp_port *port, u8 id, void *ref)
L
Linus Torvalds 已提交
1639 1640 1641 1642
{
	unsigned long flags;

	read_lock_irqsave(&zfcp_data.config_lock, flags);
1643 1644 1645
	zfcp_erp_modify_port_status(port, id, ref,
				    ZFCP_STATUS_COMMON_ERP_FAILED |
				    ZFCP_STATUS_COMMON_ACCESS_DENIED, ZFCP_SET);
L
Linus Torvalds 已提交
1646 1647 1648
	read_unlock_irqrestore(&zfcp_data.config_lock, flags);
}

1649 1650 1651 1652 1653 1654 1655 1656
/**
 * zfcp_erp_unit_access_denied - Adapter denied access to unit.
 * @unit: unit where access has been denied
 * @id: id for debug trace
 * @ref: reference for debug trace
 *
 * Since the adapter has denied access, stop using the unit.
 */
1657
void zfcp_erp_unit_access_denied(struct zfcp_unit *unit, u8 id, void *ref)
L
Linus Torvalds 已提交
1658
{
1659 1660 1661
	zfcp_erp_modify_unit_status(unit, id, ref,
				    ZFCP_STATUS_COMMON_ERP_FAILED |
				    ZFCP_STATUS_COMMON_ACCESS_DENIED, ZFCP_SET);
L
Linus Torvalds 已提交
1662 1663
}

1664 1665
static void zfcp_erp_unit_access_changed(struct zfcp_unit *unit, u8 id,
					 void *ref)
L
Linus Torvalds 已提交
1666
{
1667 1668 1669
	int status = atomic_read(&unit->status);
	if (!(status & (ZFCP_STATUS_COMMON_ACCESS_DENIED |
			ZFCP_STATUS_COMMON_ACCESS_BOXED)))
1670 1671
		return;

1672
	zfcp_erp_unit_reopen(unit, ZFCP_STATUS_COMMON_ERP_FAILED, id, ref);
L
Linus Torvalds 已提交
1673 1674
}

1675 1676
static void zfcp_erp_port_access_changed(struct zfcp_port *port, u8 id,
					 void *ref)
L
Linus Torvalds 已提交
1677 1678
{
	struct zfcp_unit *unit;
1679
	int status = atomic_read(&port->status);
L
Linus Torvalds 已提交
1680

1681 1682
	if (!(status & (ZFCP_STATUS_COMMON_ACCESS_DENIED |
			ZFCP_STATUS_COMMON_ACCESS_BOXED))) {
1683 1684
		list_for_each_entry(unit, &port->unit_list_head, list)
				    zfcp_erp_unit_access_changed(unit, id, ref);
L
Linus Torvalds 已提交
1685 1686 1687
		return;
	}

C
Christof Schmitt 已提交
1688
	zfcp_erp_port_reopen(port, ZFCP_STATUS_COMMON_ERP_FAILED, id, ref);
L
Linus Torvalds 已提交
1689 1690
}

1691 1692 1693 1694 1695 1696 1697 1698
/**
 * zfcp_erp_adapter_access_changed - Process change in adapter ACT
 * @adapter: Adapter where the Access Control Table (ACT) changed
 * @id: Id for debug trace
 * @ref: Reference for debug trace
 */
void zfcp_erp_adapter_access_changed(struct zfcp_adapter *adapter, u8 id,
				     void *ref)
L
Linus Torvalds 已提交
1699
{
1700 1701 1702 1703
	struct zfcp_port *port;
	unsigned long flags;

	if (adapter->connection_features & FSF_FEATURE_NPIV_MODE)
L
Linus Torvalds 已提交
1704 1705
		return;

1706 1707
	read_lock_irqsave(&zfcp_data.config_lock, flags);
	list_for_each_entry(port, &adapter->port_list_head, list)
1708
		zfcp_erp_port_access_changed(port, id, ref);
1709
	read_unlock_irqrestore(&zfcp_data.config_lock, flags);
L
Linus Torvalds 已提交
1710
}