zfcp_erp.c 45.7 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
 *
6
 * Copyright IBM Corporation 2002, 2009
L
Linus Torvalds 已提交
7 8
 */

9 10 11
#define KMSG_COMPONENT "zfcp"
#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt

L
Linus Torvalds 已提交
12 13
#include "zfcp_ext.h"

14 15 16 17 18 19 20 21 22
#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 已提交
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 54 55 56
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 已提交
57
{
58
	zfcp_erp_modify_adapter_status(adapter, "erablk1", NULL,
59 60
				       ZFCP_STATUS_COMMON_UNBLOCKED | mask,
				       ZFCP_CLEAR);
61
}
L
Linus Torvalds 已提交
62

63
static int zfcp_erp_action_exists(struct zfcp_erp_action *act)
64
{
65 66 67 68 69 70
	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 已提交
71 72
}

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

	list_move(&act->list, &act->adapter->erp_ready_head);
78
	zfcp_rec_dbf_event_action("erardy1", act);
79
	up(&adapter->erp_ready_sem);
80
	zfcp_rec_dbf_event_thread("erardy2", adapter);
81 82
}

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

90 91 92 93 94
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 已提交
95

96 97 98
static void zfcp_erp_action_dismiss_port(struct zfcp_port *port)
{
	struct zfcp_unit *unit;
L
Linus Torvalds 已提交
99

100 101 102 103 104
	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 已提交
105 106
}

107
static void zfcp_erp_action_dismiss_adapter(struct zfcp_adapter *adapter)
L
Linus Torvalds 已提交
108
{
109
	struct zfcp_port *port;
L
Linus Torvalds 已提交
110

111 112 113 114 115
	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 已提交
116 117
}

118 119 120
static int zfcp_erp_required_act(int want, struct zfcp_adapter *adapter,
				 struct zfcp_port *port,
				 struct zfcp_unit *unit)
L
Linus Torvalds 已提交
121
{
122 123
	int need = want;
	int u_status, p_status, a_status;
L
Linus Torvalds 已提交
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 151 152 153
	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 已提交
154

155
	return need;
L
Linus Torvalds 已提交
156 157
}

158 159 160 161
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 已提交
162
{
163 164
	struct zfcp_erp_action *erp_action;
	u32 status = 0;
L
Linus Torvalds 已提交
165

166 167 168 169 170 171 172 173
	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 已提交
174

175 176 177 178 179 180 181 182 183
	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 已提交
184

185 186 187 188 189 190 191 192 193 194 195 196 197
	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 已提交
198

199 200 201 202 203 204
	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 已提交
205

206
	return erp_action;
L
Linus Torvalds 已提交
207 208
}

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

216 217 218
	if (!(atomic_read(&adapter->status) &
	      ZFCP_STATUS_ADAPTER_ERP_THREAD_UP))
		return -EIO;
L
Linus Torvalds 已提交
219

220 221
	need = zfcp_erp_required_act(want, adapter, port, unit);
	if (!need)
L
Linus Torvalds 已提交
222 223
		goto out;

224 225 226 227 228 229 230
	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);
231
	zfcp_rec_dbf_event_thread("eracte1", adapter);
232
	retval = 0;
L
Linus Torvalds 已提交
233
 out:
234 235
	zfcp_rec_dbf_event_trigger(id, ref, want, need, act,
				   adapter, port, unit);
L
Linus Torvalds 已提交
236 237 238
	return retval;
}

239
static int _zfcp_erp_adapter_reopen(struct zfcp_adapter *adapter,
240
				    int clear_mask, char *id, void *ref)
241 242
{
	zfcp_erp_adapter_block(adapter, clear_mask);
243
	zfcp_scsi_schedule_rports_block(adapter);
244 245 246

	/* ensure propagation of failed status to new devices */
	if (atomic_read(&adapter->status) & ZFCP_STATUS_COMMON_ERP_FAILED) {
247
		zfcp_erp_adapter_failed(adapter, "erareo1", NULL);
248 249 250 251 252 253 254 255 256 257 258 259
		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 已提交
260
 */
261
void zfcp_erp_adapter_reopen(struct zfcp_adapter *adapter, int clear,
262
			     char *id, void *ref)
L
Linus Torvalds 已提交
263 264 265 266 267
{
	unsigned long flags;

	read_lock_irqsave(&zfcp_data.config_lock, flags);
	write_lock(&adapter->erp_lock);
268
	_zfcp_erp_adapter_reopen(adapter, clear, id, ref);
L
Linus Torvalds 已提交
269 270
	write_unlock(&adapter->erp_lock);
	read_unlock_irqrestore(&zfcp_data.config_lock, flags);
271
}
L
Linus Torvalds 已提交
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,
281
			       char *id, void *ref)
282 283 284
{
	int flags = ZFCP_STATUS_COMMON_RUNNING | ZFCP_STATUS_COMMON_ERP_FAILED;
	zfcp_erp_adapter_reopen(adapter, clear | flags, id, ref);
L
Linus Torvalds 已提交
285 286
}

287 288 289 290 291 292
/**
 * 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 已提交
293
 */
294 295
void zfcp_erp_port_shutdown(struct zfcp_port *port, int clear, char *id,
			    void *ref)
L
Linus Torvalds 已提交
296
{
297 298 299 300 301 302 303 304 305 306 307
	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.
 */
308 309
void zfcp_erp_unit_shutdown(struct zfcp_unit *unit, int clear, char *id,
			    void *ref)
310 311 312 313 314 315 316
{
	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)
{
317
	zfcp_erp_modify_port_status(port, "erpblk1", NULL,
318 319 320 321 322
				    ZFCP_STATUS_COMMON_UNBLOCKED | clear,
				    ZFCP_CLEAR);
}

static void _zfcp_erp_port_forced_reopen(struct zfcp_port *port,
323
					 int clear, char *id, void *ref)
324 325
{
	zfcp_erp_port_block(port, clear);
326
	zfcp_scsi_schedule_rport_block(port);
327 328 329

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

331 332 333 334 335 336 337 338 339 340
	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.
 */
341
void zfcp_erp_port_forced_reopen(struct zfcp_port *port, int clear, char *id,
342 343 344 345 346 347 348 349 350 351 352 353
				 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);
}

354
static int _zfcp_erp_port_reopen(struct zfcp_port *port, int clear, char *id,
355 356 357
				 void *ref)
{
	zfcp_erp_port_block(port, clear);
358
	zfcp_scsi_schedule_rport_block(port);
L
Linus Torvalds 已提交
359

360
	if (atomic_read(&port->status) & ZFCP_STATUS_COMMON_ERP_FAILED) {
L
Linus Torvalds 已提交
361
		/* ensure propagation of failed status to new devices */
362
		zfcp_erp_port_failed(port, "erpreo1", NULL);
363
		return -EIO;
L
Linus Torvalds 已提交
364 365
	}

366 367
	return zfcp_erp_action_enqueue(ZFCP_ERP_ACTION_REOPEN_PORT,
				       port->adapter, port, NULL, id, ref);
L
Linus Torvalds 已提交
368 369 370
}

/**
371 372 373
 * zfcp_erp_port_reopen - trigger remote port recovery
 * @port: port to recover
 * @clear_mask: flags in port status to be cleared
L
Linus Torvalds 已提交
374
 *
375
 * Returns 0 if recovery has been triggered, < 0 if not.
L
Linus Torvalds 已提交
376
 */
377
int zfcp_erp_port_reopen(struct zfcp_port *port, int clear, char *id, void *ref)
L
Linus Torvalds 已提交
378 379
{
	unsigned long flags;
380
	int retval;
L
Linus Torvalds 已提交
381 382 383 384
	struct zfcp_adapter *adapter = port->adapter;

	read_lock_irqsave(&zfcp_data.config_lock, flags);
	write_lock(&adapter->erp_lock);
385
	retval = _zfcp_erp_port_reopen(port, clear, id, ref);
L
Linus Torvalds 已提交
386 387 388 389 390 391
	write_unlock(&adapter->erp_lock);
	read_unlock_irqrestore(&zfcp_data.config_lock, flags);

	return retval;
}

392 393
static void zfcp_erp_unit_block(struct zfcp_unit *unit, int clear_mask)
{
394
	zfcp_erp_modify_unit_status(unit, "erublk1", NULL,
395 396 397 398
				    ZFCP_STATUS_COMMON_UNBLOCKED | clear_mask,
				    ZFCP_CLEAR);
}

399
static void _zfcp_erp_unit_reopen(struct zfcp_unit *unit, int clear, char *id,
400
				  void *ref)
L
Linus Torvalds 已提交
401 402 403
{
	struct zfcp_adapter *adapter = unit->port->adapter;

404
	zfcp_erp_unit_block(unit, clear);
L
Linus Torvalds 已提交
405

406 407
	if (atomic_read(&unit->status) & ZFCP_STATUS_COMMON_ERP_FAILED)
		return;
L
Linus Torvalds 已提交
408

409 410
	zfcp_erp_action_enqueue(ZFCP_ERP_ACTION_REOPEN_UNIT,
				adapter, unit->port, unit, id, ref);
L
Linus Torvalds 已提交
411 412 413 414 415 416 417 418
}

/**
 * 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
 */
419 420
void zfcp_erp_unit_reopen(struct zfcp_unit *unit, int clear, char *id,
			  void *ref)
L
Linus Torvalds 已提交
421 422
{
	unsigned long flags;
423 424
	struct zfcp_port *port = unit->port;
	struct zfcp_adapter *adapter = port->adapter;
L
Linus Torvalds 已提交
425 426 427

	read_lock_irqsave(&zfcp_data.config_lock, flags);
	write_lock(&adapter->erp_lock);
428
	_zfcp_erp_unit_reopen(unit, clear, id, ref);
L
Linus Torvalds 已提交
429 430 431 432
	write_unlock(&adapter->erp_lock);
	read_unlock_irqrestore(&zfcp_data.config_lock, flags);
}

433
static int status_change_set(unsigned long mask, atomic_t *status)
L
Linus Torvalds 已提交
434
{
435
	return (atomic_read(status) ^ mask) & mask;
L
Linus Torvalds 已提交
436 437
}

438
static int status_change_clear(unsigned long mask, atomic_t *status)
439
{
440
	return atomic_read(status) & mask;
441 442
}

443
static void zfcp_erp_adapter_unblock(struct zfcp_adapter *adapter)
444
{
445
	if (status_change_set(ZFCP_STATUS_COMMON_UNBLOCKED, &adapter->status))
446
		zfcp_rec_dbf_event_adapter("eraubl1", NULL, adapter);
447
	atomic_set_mask(ZFCP_STATUS_COMMON_UNBLOCKED, &adapter->status);
448 449
}

450
static void zfcp_erp_port_unblock(struct zfcp_port *port)
L
Linus Torvalds 已提交
451
{
452
	if (status_change_set(ZFCP_STATUS_COMMON_UNBLOCKED, &port->status))
453
		zfcp_rec_dbf_event_port("erpubl1", NULL, port);
454
	atomic_set_mask(ZFCP_STATUS_COMMON_UNBLOCKED, &port->status);
L
Linus Torvalds 已提交
455 456
}

457
static void zfcp_erp_unit_unblock(struct zfcp_unit *unit)
L
Linus Torvalds 已提交
458
{
459
	if (status_change_set(ZFCP_STATUS_COMMON_UNBLOCKED, &unit->status))
460
		zfcp_rec_dbf_event_unit("eruubl1", NULL, unit);
461
	atomic_set_mask(ZFCP_STATUS_COMMON_UNBLOCKED, &unit->status);
L
Linus Torvalds 已提交
462 463
}

464
static void zfcp_erp_action_to_running(struct zfcp_erp_action *erp_action)
L
Linus Torvalds 已提交
465
{
466
	list_move(&erp_action->list, &erp_action->adapter->erp_running_head);
467
	zfcp_rec_dbf_event_action("erator1", erp_action);
L
Linus Torvalds 已提交
468 469
}

470
static void zfcp_erp_strategy_check_fsfreq(struct zfcp_erp_action *act)
L
Linus Torvalds 已提交
471
{
472
	struct zfcp_adapter *adapter = act->adapter;
L
Linus Torvalds 已提交
473

474 475
	if (!act->fsf_req)
		return;
L
Linus Torvalds 已提交
476

477 478 479 480 481 482
	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;
483
			zfcp_rec_dbf_event_action("erscf_1", act);
484
			act->fsf_req->erp_action = NULL;
L
Linus Torvalds 已提交
485
		}
486
		if (act->status & ZFCP_STATUS_ERP_TIMEDOUT)
487
			zfcp_rec_dbf_event_action("erscf_2", act);
488
		if (act->fsf_req->status & ZFCP_STATUS_FSFREQ_DISMISSED)
489 490 491 492
			act->fsf_req = NULL;
	} else
		act->fsf_req = NULL;
	spin_unlock(&adapter->req_list_lock);
L
Linus Torvalds 已提交
493 494
}

495 496 497 498
/**
 * zfcp_erp_notify - Trigger ERP action.
 * @erp_action: ERP action to continue.
 * @set_mask: ERP action status flags to set.
L
Linus Torvalds 已提交
499
 */
500
void zfcp_erp_notify(struct zfcp_erp_action *erp_action, unsigned long set_mask)
L
Linus Torvalds 已提交
501 502
{
	struct zfcp_adapter *adapter = erp_action->adapter;
503
	unsigned long flags;
L
Linus Torvalds 已提交
504

505
	write_lock_irqsave(&adapter->erp_lock, flags);
L
Linus Torvalds 已提交
506 507 508 509 510 511 512
	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);
}

513 514 515
/**
 * zfcp_erp_timeout_handler - Trigger ERP action from timed out ERP request
 * @data: ERP action (from timer data)
L
Linus Torvalds 已提交
516
 */
517
void zfcp_erp_timeout_handler(unsigned long data)
L
Linus Torvalds 已提交
518
{
519 520
	struct zfcp_erp_action *act = (struct zfcp_erp_action *) data;
	zfcp_erp_notify(act, ZFCP_STATUS_ERP_TIMEDOUT);
L
Linus Torvalds 已提交
521 522
}

523
static void zfcp_erp_memwait_handler(unsigned long data)
L
Linus Torvalds 已提交
524
{
525
	zfcp_erp_notify((struct zfcp_erp_action *)data, 0);
L
Linus Torvalds 已提交
526 527
}

528
static void zfcp_erp_strategy_memwait(struct zfcp_erp_action *erp_action)
L
Linus Torvalds 已提交
529
{
530 531 532 533 534
	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 已提交
535 536
}

537
static void _zfcp_erp_port_reopen_all(struct zfcp_adapter *adapter,
538
				      int clear, char *id, void *ref)
L
Linus Torvalds 已提交
539
{
540
	struct zfcp_port *port;
L
Linus Torvalds 已提交
541

542
	list_for_each_entry(port, &adapter->port_list_head, list)
543
		_zfcp_erp_port_reopen(port, clear, id, ref);
L
Linus Torvalds 已提交
544 545
}

546 547
static void _zfcp_erp_unit_reopen_all(struct zfcp_port *port, int clear,
				      char *id, void *ref)
L
Linus Torvalds 已提交
548
{
549
	struct zfcp_unit *unit;
L
Linus Torvalds 已提交
550

551 552
	list_for_each_entry(unit, &port->unit_list_head, list)
		_zfcp_erp_unit_reopen(unit, clear, id, ref);
L
Linus Torvalds 已提交
553 554
}

555
static void zfcp_erp_strategy_followup_failed(struct zfcp_erp_action *act)
L
Linus Torvalds 已提交
556
{
557 558
	switch (act->action) {
	case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
559
		_zfcp_erp_adapter_reopen(act->adapter, 0, "ersff_1", NULL);
560 561
		break;
	case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
562
		_zfcp_erp_port_forced_reopen(act->port, 0, "ersff_2", NULL);
563 564
		break;
	case ZFCP_ERP_ACTION_REOPEN_PORT:
565
		_zfcp_erp_port_reopen(act->port, 0, "ersff_3", NULL);
566 567
		break;
	case ZFCP_ERP_ACTION_REOPEN_UNIT:
568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583
		_zfcp_erp_unit_reopen(act->unit, 0, "ersff_4", NULL);
		break;
	}
}

static void zfcp_erp_strategy_followup_success(struct zfcp_erp_action *act)
{
	switch (act->action) {
	case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
		_zfcp_erp_port_reopen_all(act->adapter, 0, "ersfs_1", NULL);
		break;
	case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
		_zfcp_erp_port_reopen(act->port, 0, "ersfs_2", NULL);
		break;
	case ZFCP_ERP_ACTION_REOPEN_PORT:
		_zfcp_erp_unit_reopen_all(act->port, 0, "ersfs_3", NULL);
584
		break;
L
Linus Torvalds 已提交
585 586 587
	}
}

588
static void zfcp_erp_wakeup(struct zfcp_adapter *adapter)
L
Linus Torvalds 已提交
589 590 591 592
{
	unsigned long flags;

	read_lock_irqsave(&zfcp_data.config_lock, flags);
593 594 595 596 597 598
	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 已提交
599
	}
600 601 602
	read_unlock(&adapter->erp_lock);
	read_unlock_irqrestore(&zfcp_data.config_lock, flags);
}
L
Linus Torvalds 已提交
603

604 605
static int zfcp_erp_adapter_strategy_open_qdio(struct zfcp_erp_action *act)
{
606 607 608
	struct zfcp_qdio *qdio = act->adapter->qdio;

	if (zfcp_qdio_open(qdio))
609
		return ZFCP_ERP_FAILED;
610
	init_waitqueue_head(&qdio->req_q_wq);
611 612 613
	atomic_set_mask(ZFCP_STATUS_ADAPTER_QDIOUP, &act->adapter->status);
	return ZFCP_ERP_SUCCEEDED;
}
L
Linus Torvalds 已提交
614

615 616 617 618 619 620 621
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;
622
	_zfcp_erp_port_reopen(port, 0, "ereptp1", NULL);
623
}
L
Linus Torvalds 已提交
624

625 626 627 628 629
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 已提交
630

631 632 633 634 635 636 637 638 639 640 641 642
	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 已提交
643 644
		}

645
		zfcp_rec_dbf_event_thread_lock("erasfx1", adapter);
646
		down(&adapter->erp_ready_sem);
647
		zfcp_rec_dbf_event_thread_lock("erasfx2", adapter);
648 649
		if (erp_action->status & ZFCP_STATUS_ERP_TIMEDOUT)
			break;
L
Linus Torvalds 已提交
650

651 652 653
		if (!(atomic_read(&adapter->status) &
		      ZFCP_STATUS_ADAPTER_HOST_CON_INIT))
			break;
L
Linus Torvalds 已提交
654

655 656
		ssleep(sleep);
		sleep *= 2;
L
Linus Torvalds 已提交
657 658
	}

659 660
	atomic_clear_mask(ZFCP_STATUS_ADAPTER_HOST_CON_INIT,
			  &adapter->status);
L
Linus Torvalds 已提交
661

662 663
	if (!(atomic_read(&adapter->status) & ZFCP_STATUS_ADAPTER_XCONFIG_OK))
		return ZFCP_ERP_FAILED;
S
Swen Schillig 已提交
664

665 666
	if (fc_host_port_type(adapter->scsi_host) == FC_PORTTYPE_PTP)
		zfcp_erp_enqueue_ptp_port(adapter);
L
Linus Torvalds 已提交
667

668
	return ZFCP_ERP_SUCCEEDED;
L
Linus Torvalds 已提交
669 670
}

671
static int zfcp_erp_adapter_strategy_open_fsf_xport(struct zfcp_erp_action *act)
L
Linus Torvalds 已提交
672
{
673 674
	int ret;
	struct zfcp_adapter *adapter = act->adapter;
L
Linus Torvalds 已提交
675

676 677 678 679 680 681 682 683 684 685
	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;

686
	zfcp_rec_dbf_event_thread_lock("erasox1", adapter);
687
	down(&adapter->erp_ready_sem);
688
	zfcp_rec_dbf_event_thread_lock("erasox2", adapter);
689 690 691 692
	if (act->status & ZFCP_STATUS_ERP_TIMEDOUT)
		return ZFCP_ERP_FAILED;

	return ZFCP_ERP_SUCCEEDED;
L
Linus Torvalds 已提交
693 694
}

695
static int zfcp_erp_adapter_strategy_open_fsf(struct zfcp_erp_action *act)
L
Linus Torvalds 已提交
696
{
697 698
	if (zfcp_erp_adapter_strat_fsf_xconf(act) == ZFCP_ERP_FAILED)
		return ZFCP_ERP_FAILED;
L
Linus Torvalds 已提交
699

700 701
	if (zfcp_erp_adapter_strategy_open_fsf_xport(act) == ZFCP_ERP_FAILED)
		return ZFCP_ERP_FAILED;
L
Linus Torvalds 已提交
702

703 704 705
	atomic_set(&act->adapter->stat_miss, 16);
	if (zfcp_status_read_refill(act->adapter))
		return ZFCP_ERP_FAILED;
L
Linus Torvalds 已提交
706

707 708
	return ZFCP_ERP_SUCCEEDED;
}
L
Linus Torvalds 已提交
709

710
static void zfcp_erp_adapter_strategy_close(struct zfcp_erp_action *act)
711 712
{
	struct zfcp_adapter *adapter = act->adapter;
L
Linus Torvalds 已提交
713

714
	/* close queues to ensure that buffers are not accessed by adapter */
715
	zfcp_qdio_close(adapter->qdio);
716 717
	zfcp_fsf_req_dismiss_all(adapter);
	adapter->fsf_req_seq_no = 0;
718
	zfcp_fc_wka_ports_force_offline(adapter->gs);
719
	/* all ports and units are closed */
720
	zfcp_erp_modify_adapter_status(adapter, "erascl1", NULL,
721
				       ZFCP_STATUS_COMMON_OPEN, ZFCP_CLEAR);
722

723
	atomic_clear_mask(ZFCP_STATUS_ADAPTER_XCONFIG_OK |
724
			  ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED, &adapter->status);
L
Linus Torvalds 已提交
725 726
}

727
static int zfcp_erp_adapter_strategy_open(struct zfcp_erp_action *act)
L
Linus Torvalds 已提交
728
{
729
	struct zfcp_adapter *adapter = act->adapter;
L
Linus Torvalds 已提交
730

731 732 733 734 735 736
	if (zfcp_erp_adapter_strategy_open_qdio(act)) {
		atomic_clear_mask(ZFCP_STATUS_ADAPTER_XCONFIG_OK |
				  ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED,
				  &adapter->status);
		return ZFCP_ERP_FAILED;
	}
737

738 739 740 741 742 743 744 745 746
	if (zfcp_erp_adapter_strategy_open_fsf(act)) {
		zfcp_erp_adapter_strategy_close(act);
		return ZFCP_ERP_FAILED;
	}

	atomic_set_mask(ZFCP_STATUS_COMMON_OPEN, &adapter->status);

	return ZFCP_ERP_SUCCEEDED;
}
747

748 749 750 751 752 753 754 755 756 757 758
static int zfcp_erp_adapter_strategy(struct zfcp_erp_action *act)
{
	struct zfcp_adapter *adapter = act->adapter;

	if (atomic_read(&adapter->status) & ZFCP_STATUS_COMMON_OPEN) {
		zfcp_erp_adapter_strategy_close(act);
		if (act->status & ZFCP_STATUS_ERP_CLOSE_ONLY)
			return ZFCP_ERP_EXIT;
	}

	if (zfcp_erp_adapter_strategy_open(act)) {
759
		ssleep(8);
760 761
		return ZFCP_ERP_FAILED;
	}
L
Linus Torvalds 已提交
762

763
	return ZFCP_ERP_SUCCEEDED;
L
Linus Torvalds 已提交
764 765
}

766
static int zfcp_erp_port_forced_strategy_close(struct zfcp_erp_action *act)
L
Linus Torvalds 已提交
767
{
768 769 770 771 772 773 774 775 776 777
	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 已提交
778 779
}

780
static void zfcp_erp_port_strategy_clearstati(struct zfcp_port *port)
L
Linus Torvalds 已提交
781
{
782
	atomic_clear_mask(ZFCP_STATUS_COMMON_ACCESS_DENIED, &port->status);
783
}
L
Linus Torvalds 已提交
784

785 786 787 788 789 790 791 792 793 794 795 796 797 798 799
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:
800
		if (!(status & ZFCP_STATUS_PORT_PHYS_OPEN))
801 802 803
			return ZFCP_ERP_SUCCEEDED;
	}
	return ZFCP_ERP_FAILED;
L
Linus Torvalds 已提交
804 805
}

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

810 811 812 813 814 815 816
	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 已提交
817 818
}

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

823 824 825 826 827 828 829 830
	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 已提交
831

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

837
	if (port->wwpn != adapter->peer_wwpn) {
838
		zfcp_erp_port_failed(port, "eroptp1", NULL);
839 840 841 842 843 844
		return ZFCP_ERP_FAILED;
	}
	port->d_id = adapter->peer_d_id;
	return zfcp_erp_port_strategy_open_port(act);
}

845 846 847 848 849 850 851
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);
852 853 854 855 856
	if (!retval) {
		port->erp_action.step = ZFCP_ERP_STEP_NAMESERVER_LOOKUP;
		goto out;
	}
	if (retval == -ENOMEM) {
857
		zfcp_erp_notify(&port->erp_action, ZFCP_STATUS_ERP_LOWMEM);
858 859 860 861 862
		goto out;
	}
	/* all other error condtions */
	zfcp_erp_notify(&port->erp_action, 0);
out:
863
	zfcp_port_put(port);
864 865
}

866 867 868 869 870 871 872 873 874 875 876 877
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);
878
		if (!port->d_id) {
879
			zfcp_port_get(port);
880
			if (!queue_work(adapter->work_queue,
881 882
					&port->gid_pn_work))
				zfcp_port_put(port);
883
			return ZFCP_ERP_CONTINUES;
884
		}
885
		/* fall through */
886
	case ZFCP_ERP_STEP_NAMESERVER_LOOKUP:
887
		if (!port->d_id)
888 889 890 891 892
			return ZFCP_ERP_FAILED;
		return zfcp_erp_port_strategy_open_port(act);

	case ZFCP_ERP_STEP_PORT_OPENING:
		/* D_ID might have changed during open */
893
		if (p_status & ZFCP_STATUS_COMMON_OPEN) {
894
			if (port->d_id)
895 896 897 898 899 900
				return ZFCP_ERP_SUCCEEDED;
			else {
				act->step = ZFCP_ERP_STEP_PORT_CLOSING;
				return ZFCP_ERP_CONTINUES;
			}
		}
901 902 903 904 905 906
		if (port->d_id && !(p_status & ZFCP_STATUS_COMMON_NOESC)) {
			port->d_id = 0;
			_zfcp_erp_port_reopen(port, 0, "erpsoc1", NULL);
			return ZFCP_ERP_EXIT;
		}
		/* fall through otherwise */
907 908 909 910 911 912 913 914
	}
	return ZFCP_ERP_FAILED;
}

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

915 916 917
	if (atomic_read(&port->status) & ZFCP_STATUS_COMMON_NOESC)
		goto close_init_done;

918 919 920 921 922
	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 已提交
923 924
		break;

925 926 927
	case ZFCP_ERP_STEP_PORT_CLOSING:
		if (atomic_read(&port->status) & ZFCP_STATUS_COMMON_OPEN)
			return ZFCP_ERP_FAILED;
L
Linus Torvalds 已提交
928 929
		break;
	}
930 931

close_init_done:
932 933
	if (erp_action->status & ZFCP_STATUS_ERP_CLOSE_ONLY)
		return ZFCP_ERP_EXIT;
L
Linus Torvalds 已提交
934

935
	return zfcp_erp_port_strategy_open_common(erp_action);
936 937 938 939
}

static void zfcp_erp_unit_strategy_clearstati(struct zfcp_unit *unit)
{
940
	atomic_clear_mask(ZFCP_STATUS_COMMON_ACCESS_DENIED |
941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965
			  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 已提交
966 967
}

968
static int zfcp_erp_unit_strategy(struct zfcp_erp_action *erp_action)
L
Linus Torvalds 已提交
969
{
970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989
	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 已提交
990 991
}

992
static int zfcp_erp_strategy_check_unit(struct zfcp_unit *unit, int result)
L
Linus Torvalds 已提交
993 994 995 996 997 998 999 1000
{
	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);
1001 1002 1003 1004
		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",
1005 1006
				(unsigned long long)unit->fcp_lun,
				(unsigned long long)unit->port->wwpn);
1007
			zfcp_erp_unit_failed(unit, "erusck1", NULL);
1008
		}
L
Linus Torvalds 已提交
1009 1010 1011
		break;
	}

1012 1013
	if (atomic_read(&unit->status) & ZFCP_STATUS_COMMON_ERP_FAILED) {
		zfcp_erp_unit_block(unit, 0);
L
Linus Torvalds 已提交
1014 1015 1016 1017 1018
		result = ZFCP_ERP_EXIT;
	}
	return result;
}

1019
static int zfcp_erp_strategy_check_port(struct zfcp_port *port, int result)
L
Linus Torvalds 已提交
1020 1021 1022 1023 1024 1025
{
	switch (result) {
	case ZFCP_ERP_SUCCEEDED :
		atomic_set(&port->erp_counter, 0);
		zfcp_erp_port_unblock(port);
		break;
1026

L
Linus Torvalds 已提交
1027
	case ZFCP_ERP_FAILED :
1028
		if (atomic_read(&port->status) & ZFCP_STATUS_COMMON_NOESC) {
1029 1030 1031
			zfcp_erp_port_block(port, 0);
			result = ZFCP_ERP_EXIT;
		}
L
Linus Torvalds 已提交
1032
		atomic_inc(&port->erp_counter);
1033 1034 1035
		if (atomic_read(&port->erp_counter) > ZFCP_MAX_ERPS) {
			dev_err(&port->adapter->ccw_device->dev,
				"ERP failed for remote port 0x%016Lx\n",
1036
				(unsigned long long)port->wwpn);
1037
			zfcp_erp_port_failed(port, "erpsck1", NULL);
1038
		}
L
Linus Torvalds 已提交
1039 1040 1041
		break;
	}

1042 1043
	if (atomic_read(&port->status) & ZFCP_STATUS_COMMON_ERP_FAILED) {
		zfcp_erp_port_block(port, 0);
L
Linus Torvalds 已提交
1044 1045 1046 1047 1048
		result = ZFCP_ERP_EXIT;
	}
	return result;
}

1049 1050
static int zfcp_erp_strategy_check_adapter(struct zfcp_adapter *adapter,
					   int result)
L
Linus Torvalds 已提交
1051 1052 1053 1054 1055 1056
{
	switch (result) {
	case ZFCP_ERP_SUCCEEDED :
		atomic_set(&adapter->erp_counter, 0);
		zfcp_erp_adapter_unblock(adapter);
		break;
1057

L
Linus Torvalds 已提交
1058 1059
	case ZFCP_ERP_FAILED :
		atomic_inc(&adapter->erp_counter);
1060 1061 1062 1063
		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");
1064
			zfcp_erp_adapter_failed(adapter, "erasck1", NULL);
1065
		}
L
Linus Torvalds 已提交
1066 1067 1068
		break;
	}

1069 1070
	if (atomic_read(&adapter->status) & ZFCP_STATUS_COMMON_ERP_FAILED) {
		zfcp_erp_adapter_block(adapter, 0);
L
Linus Torvalds 已提交
1071 1072 1073 1074 1075
		result = ZFCP_ERP_EXIT;
	}
	return result;
}

1076 1077
static int zfcp_erp_strategy_check_target(struct zfcp_erp_action *erp_action,
					  int result)
1078
{
1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098
	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;
1099 1100
}

1101
static int zfcp_erp_strat_change_det(atomic_t *target_status, u32 erp_status)
1102
{
1103
	int status = atomic_read(target_status);
1104

1105 1106 1107
	if ((status & ZFCP_STATUS_COMMON_RUNNING) &&
	    (erp_status & ZFCP_STATUS_ERP_CLOSE_ONLY))
		return 1; /* take it online */
1108

1109 1110 1111 1112 1113
	if (!(status & ZFCP_STATUS_COMMON_RUNNING) &&
	    !(erp_status & ZFCP_STATUS_ERP_CLOSE_ONLY))
		return 1; /* take it offline */

	return 0;
1114 1115
}

1116
static int zfcp_erp_strategy_statechange(struct zfcp_erp_action *act, int ret)
L
Linus Torvalds 已提交
1117
{
1118 1119 1120 1121 1122
	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 已提交
1123

1124
	switch (action) {
L
Linus Torvalds 已提交
1125
	case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
1126 1127 1128
		if (zfcp_erp_strat_change_det(&adapter->status, erp_status)) {
			_zfcp_erp_adapter_reopen(adapter,
						 ZFCP_STATUS_COMMON_ERP_FAILED,
1129
						 "ersscg1", NULL);
1130 1131
			return ZFCP_ERP_EXIT;
		}
L
Linus Torvalds 已提交
1132 1133 1134 1135
		break;

	case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
	case ZFCP_ERP_ACTION_REOPEN_PORT:
1136 1137 1138
		if (zfcp_erp_strat_change_det(&port->status, erp_status)) {
			_zfcp_erp_port_reopen(port,
					      ZFCP_STATUS_COMMON_ERP_FAILED,
1139
					      "ersscg2", NULL);
1140 1141
			return ZFCP_ERP_EXIT;
		}
L
Linus Torvalds 已提交
1142 1143 1144
		break;

	case ZFCP_ERP_ACTION_REOPEN_UNIT:
1145 1146 1147
		if (zfcp_erp_strat_change_det(&unit->status, erp_status)) {
			_zfcp_erp_unit_reopen(unit,
					      ZFCP_STATUS_COMMON_ERP_FAILED,
1148
					      "ersscg3", NULL);
1149 1150
			return ZFCP_ERP_EXIT;
		}
L
Linus Torvalds 已提交
1151 1152
		break;
	}
1153
	return ret;
L
Linus Torvalds 已提交
1154 1155
}

1156
static void zfcp_erp_action_dequeue(struct zfcp_erp_action *erp_action)
L
Linus Torvalds 已提交
1157
{
1158
	struct zfcp_adapter *adapter = erp_action->adapter;
L
Linus Torvalds 已提交
1159

1160 1161 1162 1163
	adapter->erp_total_count--;
	if (erp_action->status & ZFCP_STATUS_ERP_LOWMEM) {
		adapter->erp_low_mem_count--;
		erp_action->status &= ~ZFCP_STATUS_ERP_LOWMEM;
1164
	}
L
Linus Torvalds 已提交
1165

1166
	list_del(&erp_action->list);
1167
	zfcp_rec_dbf_event_action("eractd1", erp_action);
L
Linus Torvalds 已提交
1168

1169 1170 1171 1172 1173
	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 已提交
1174

1175 1176 1177 1178 1179
	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 已提交
1180

1181 1182 1183 1184 1185
	case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
		atomic_clear_mask(ZFCP_STATUS_COMMON_ERP_INUSE,
				  &erp_action->adapter->status);
		break;
	}
L
Linus Torvalds 已提交
1186 1187
}

1188
static void zfcp_erp_action_cleanup(struct zfcp_erp_action *act, int result)
L
Linus Torvalds 已提交
1189
{
1190 1191 1192
	struct zfcp_adapter *adapter = act->adapter;
	struct zfcp_port *port = act->port;
	struct zfcp_unit *unit = act->unit;
L
Linus Torvalds 已提交
1193

1194 1195
	switch (act->action) {
	case ZFCP_ERP_ACTION_REOPEN_UNIT:
1196
		if ((result == ZFCP_ERP_SUCCEEDED) && !unit->device) {
1197 1198 1199 1200
			zfcp_unit_get(unit);
			if (scsi_queue_work(unit->port->adapter->scsi_host,
					    &unit->scsi_work) <= 0)
				zfcp_unit_put(unit);
1201 1202 1203
		}
		zfcp_unit_put(unit);
		break;
L
Linus Torvalds 已提交
1204

1205 1206
	case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
	case ZFCP_ERP_ACTION_REOPEN_PORT:
1207 1208
		if (result == ZFCP_ERP_SUCCEEDED)
			zfcp_scsi_schedule_rport_register(port);
1209 1210 1211 1212
		zfcp_port_put(port);
		break;

	case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
1213
		if (result == ZFCP_ERP_SUCCEEDED) {
1214
			register_service_level(&adapter->service_level);
1215
			schedule_work(&adapter->scan_work);
1216 1217
		} else
			unregister_service_level(&adapter->service_level);
1218 1219 1220
		zfcp_adapter_put(adapter);
		break;
	}
L
Linus Torvalds 已提交
1221 1222
}

1223
static int zfcp_erp_strategy_do_action(struct zfcp_erp_action *erp_action)
L
Linus Torvalds 已提交
1224
{
1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235
	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 已提交
1236 1237
}

1238
static int zfcp_erp_strategy(struct zfcp_erp_action *erp_action)
L
Linus Torvalds 已提交
1239 1240
{
	int retval;
1241 1242
	struct zfcp_adapter *adapter = erp_action->adapter;
	unsigned long flags;
L
Linus Torvalds 已提交
1243

1244 1245
	read_lock_irqsave(&zfcp_data.config_lock, flags);
	write_lock(&adapter->erp_lock);
L
Linus Torvalds 已提交
1246

1247
	zfcp_erp_strategy_check_fsfreq(erp_action);
L
Linus Torvalds 已提交
1248

1249 1250 1251 1252
	if (erp_action->status & ZFCP_STATUS_ERP_DISMISSED) {
		zfcp_erp_action_dequeue(erp_action);
		retval = ZFCP_ERP_DISMISSED;
		goto unlock;
1253
	}
L
Linus Torvalds 已提交
1254

1255
	zfcp_erp_action_to_running(erp_action);
L
Linus Torvalds 已提交
1256

1257 1258 1259 1260 1261 1262
	/* 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 已提交
1263

1264 1265
	if (erp_action->status & ZFCP_STATUS_ERP_DISMISSED)
		retval = ZFCP_ERP_CONTINUES;
1266

1267 1268 1269 1270 1271
	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 已提交
1272
		}
1273
		if (adapter->erp_total_count == adapter->erp_low_mem_count)
1274
			_zfcp_erp_adapter_reopen(adapter, 0, "erstgy1", NULL);
1275 1276 1277
		else {
			zfcp_erp_strategy_memwait(erp_action);
			retval = ZFCP_ERP_CONTINUES;
L
Linus Torvalds 已提交
1278
		}
1279
		goto unlock;
L
Linus Torvalds 已提交
1280

1281 1282 1283 1284
	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 已提交
1285
		}
1286
		goto unlock;
L
Linus Torvalds 已提交
1287 1288
	}

1289 1290 1291 1292 1293
	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;
1294 1295 1296 1297
	if (retval == ZFCP_ERP_SUCCEEDED)
		zfcp_erp_strategy_followup_success(erp_action);
	if (retval == ZFCP_ERP_FAILED)
		zfcp_erp_strategy_followup_failed(erp_action);
L
Linus Torvalds 已提交
1298

1299 1300 1301
 unlock:
	write_unlock(&adapter->erp_lock);
	read_unlock_irqrestore(&zfcp_data.config_lock, flags);
L
Linus Torvalds 已提交
1302

1303 1304
	if (retval != ZFCP_ERP_CONTINUES)
		zfcp_erp_action_cleanup(erp_action, retval);
L
Linus Torvalds 已提交
1305 1306 1307 1308

	return retval;
}

1309
static int zfcp_erp_thread(void *data)
L
Linus Torvalds 已提交
1310
{
1311 1312 1313 1314
	struct zfcp_adapter *adapter = (struct zfcp_adapter *) data;
	struct list_head *next;
	struct zfcp_erp_action *act;
	unsigned long flags;
H
Heiko Carstens 已提交
1315
	int ignore;
L
Linus Torvalds 已提交
1316

1317
	daemonize("zfcperp%s", dev_name(&adapter->ccw_device->dev));
1318 1319 1320 1321
	/* 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 已提交
1322

1323 1324
	while (!(atomic_read(&adapter->status) &
		 ZFCP_STATUS_ADAPTER_ERP_THREAD_KILL)) {
1325 1326 1327 1328 1329

		zfcp_rec_dbf_event_thread_lock("erthrd1", adapter);
		ignore = down_interruptible(&adapter->erp_ready_sem);
		zfcp_rec_dbf_event_thread_lock("erthrd2", adapter);

1330 1331 1332
		write_lock_irqsave(&adapter->erp_lock, flags);
		next = adapter->erp_ready_head.next;
		write_unlock_irqrestore(&adapter->erp_lock, flags);
L
Linus Torvalds 已提交
1333

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

1337 1338 1339
			/* there is more to come after dismission, no notify */
			if (zfcp_erp_strategy(act) != ZFCP_ERP_DISMISSED)
				zfcp_erp_wakeup(adapter);
L
Linus Torvalds 已提交
1340 1341 1342
		}
	}

1343 1344
	atomic_clear_mask(ZFCP_STATUS_ADAPTER_ERP_THREAD_UP, &adapter->status);
	wake_up(&adapter->erp_thread_wqh);
L
Linus Torvalds 已提交
1345

1346 1347
	return 0;
}
L
Linus Torvalds 已提交
1348

1349 1350 1351 1352 1353 1354 1355 1356 1357
/**
 * 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 已提交
1358

1359 1360 1361 1362
	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,
1363
			"Creating an ERP thread for the FCP device failed.\n");
1364
		return retval;
L
Linus Torvalds 已提交
1365
	}
1366 1367 1368 1369 1370
	wait_event(adapter->erp_thread_wqh,
		   atomic_read(&adapter->status) &
			ZFCP_STATUS_ADAPTER_ERP_THREAD_UP);
	return 0;
}
L
Linus Torvalds 已提交
1371

1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384
/**
 * 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);
1385
	zfcp_rec_dbf_event_thread_lock("erthrk1", adapter);
L
Linus Torvalds 已提交
1386

1387 1388 1389
	wait_event(adapter->erp_thread_wqh,
		   !(atomic_read(&adapter->status) &
				ZFCP_STATUS_ADAPTER_ERP_THREAD_UP));
L
Linus Torvalds 已提交
1390

1391 1392
	atomic_clear_mask(ZFCP_STATUS_ADAPTER_ERP_THREAD_KILL,
			  &adapter->status);
L
Linus Torvalds 已提交
1393 1394
}

1395 1396 1397 1398 1399 1400
/**
 * zfcp_erp_adapter_failed - Set adapter status to failed.
 * @adapter: Failed adapter.
 * @id: Event id for debug trace.
 * @ref: Reference for debug trace.
 */
1401
void zfcp_erp_adapter_failed(struct zfcp_adapter *adapter, char *id, void *ref)
L
Linus Torvalds 已提交
1402
{
1403 1404 1405
	zfcp_erp_modify_adapter_status(adapter, id, ref,
				       ZFCP_STATUS_COMMON_ERP_FAILED, ZFCP_SET);
}
L
Linus Torvalds 已提交
1406

1407 1408 1409 1410 1411 1412
/**
 * zfcp_erp_port_failed - Set port status to failed.
 * @port: Failed port.
 * @id: Event id for debug trace.
 * @ref: Reference for debug trace.
 */
1413
void zfcp_erp_port_failed(struct zfcp_port *port, char *id, void *ref)
1414 1415 1416
{
	zfcp_erp_modify_port_status(port, id, ref,
				    ZFCP_STATUS_COMMON_ERP_FAILED, ZFCP_SET);
L
Linus Torvalds 已提交
1417 1418 1419
}

/**
1420 1421 1422 1423
 * 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 已提交
1424
 */
1425
void zfcp_erp_unit_failed(struct zfcp_unit *unit, char *id, void *ref)
L
Linus Torvalds 已提交
1426
{
1427 1428
	zfcp_erp_modify_unit_status(unit, id, ref,
				    ZFCP_STATUS_COMMON_ERP_FAILED, ZFCP_SET);
L
Linus Torvalds 已提交
1429 1430
}

1431 1432 1433 1434 1435 1436 1437 1438 1439 1440
/**
 * 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 已提交
1441

1442 1443 1444 1445 1446 1447 1448 1449 1450 1451
/**
 * 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.
 */
1452
void zfcp_erp_modify_adapter_status(struct zfcp_adapter *adapter, char *id,
1453
				    void *ref, u32 mask, int set_or_clear)
L
Linus Torvalds 已提交
1454 1455
{
	struct zfcp_port *port;
1456
	u32 common_mask = mask & ZFCP_COMMON_FLAGS;
L
Linus Torvalds 已提交
1457

1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470
	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 已提交
1471
		list_for_each_entry(port, &adapter->port_list_head, list)
1472 1473
			zfcp_erp_modify_port_status(port, id, ref, common_mask,
						    set_or_clear);
L
Linus Torvalds 已提交
1474 1475
}

1476 1477 1478 1479 1480 1481 1482 1483 1484 1485
/**
 * 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.
 */
1486
void zfcp_erp_modify_port_status(struct zfcp_port *port, char *id, void *ref,
1487
				 u32 mask, int set_or_clear)
L
Linus Torvalds 已提交
1488 1489
{
	struct zfcp_unit *unit;
1490
	u32 common_mask = mask & ZFCP_COMMON_FLAGS;
L
Linus Torvalds 已提交
1491

1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502
	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 已提交
1503

1504 1505 1506 1507
	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 已提交
1508 1509
}

1510 1511 1512 1513 1514 1515 1516 1517
/**
 * 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
 */
1518
void zfcp_erp_modify_unit_status(struct zfcp_unit *unit, char *id, void *ref,
1519
				 u32 mask, int set_or_clear)
L
Linus Torvalds 已提交
1520
{
1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532
	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 已提交
1533 1534
}

1535 1536 1537 1538 1539 1540
/**
 * 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.
 */
1541
void zfcp_erp_port_boxed(struct zfcp_port *port, char *id, void *ref)
1542 1543 1544 1545
{
	unsigned long flags;

	read_lock_irqsave(&zfcp_data.config_lock, flags);
1546 1547
	zfcp_erp_modify_port_status(port, id, ref,
				    ZFCP_STATUS_COMMON_ACCESS_BOXED, ZFCP_SET);
1548
	read_unlock_irqrestore(&zfcp_data.config_lock, flags);
1549
	zfcp_erp_port_reopen(port, ZFCP_STATUS_COMMON_ERP_FAILED, id, ref);
1550 1551
}

1552 1553 1554 1555 1556 1557
/**
 * 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.
 */
1558
void zfcp_erp_unit_boxed(struct zfcp_unit *unit, char *id, void *ref)
1559
{
1560 1561
	zfcp_erp_modify_unit_status(unit, id, ref,
				    ZFCP_STATUS_COMMON_ACCESS_BOXED, ZFCP_SET);
1562
	zfcp_erp_unit_reopen(unit, ZFCP_STATUS_COMMON_ERP_FAILED, id, ref);
1563 1564
}

1565 1566 1567 1568 1569 1570 1571 1572 1573
/**
 * 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.
 */
1574
void zfcp_erp_port_access_denied(struct zfcp_port *port, char *id, void *ref)
L
Linus Torvalds 已提交
1575 1576 1577 1578
{
	unsigned long flags;

	read_lock_irqsave(&zfcp_data.config_lock, flags);
1579 1580 1581
	zfcp_erp_modify_port_status(port, id, ref,
				    ZFCP_STATUS_COMMON_ERP_FAILED |
				    ZFCP_STATUS_COMMON_ACCESS_DENIED, ZFCP_SET);
L
Linus Torvalds 已提交
1582 1583 1584
	read_unlock_irqrestore(&zfcp_data.config_lock, flags);
}

1585 1586 1587 1588 1589 1590 1591 1592
/**
 * 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.
 */
1593
void zfcp_erp_unit_access_denied(struct zfcp_unit *unit, char *id, void *ref)
L
Linus Torvalds 已提交
1594
{
1595 1596 1597
	zfcp_erp_modify_unit_status(unit, id, ref,
				    ZFCP_STATUS_COMMON_ERP_FAILED |
				    ZFCP_STATUS_COMMON_ACCESS_DENIED, ZFCP_SET);
L
Linus Torvalds 已提交
1598 1599
}

1600
static void zfcp_erp_unit_access_changed(struct zfcp_unit *unit, char *id,
1601
					 void *ref)
L
Linus Torvalds 已提交
1602
{
1603 1604 1605
	int status = atomic_read(&unit->status);
	if (!(status & (ZFCP_STATUS_COMMON_ACCESS_DENIED |
			ZFCP_STATUS_COMMON_ACCESS_BOXED)))
1606 1607
		return;

1608
	zfcp_erp_unit_reopen(unit, ZFCP_STATUS_COMMON_ERP_FAILED, id, ref);
L
Linus Torvalds 已提交
1609 1610
}

1611
static void zfcp_erp_port_access_changed(struct zfcp_port *port, char *id,
1612
					 void *ref)
L
Linus Torvalds 已提交
1613 1614
{
	struct zfcp_unit *unit;
1615
	int status = atomic_read(&port->status);
L
Linus Torvalds 已提交
1616

1617 1618
	if (!(status & (ZFCP_STATUS_COMMON_ACCESS_DENIED |
			ZFCP_STATUS_COMMON_ACCESS_BOXED))) {
1619 1620
		list_for_each_entry(unit, &port->unit_list_head, list)
				    zfcp_erp_unit_access_changed(unit, id, ref);
L
Linus Torvalds 已提交
1621 1622 1623
		return;
	}

C
Christof Schmitt 已提交
1624
	zfcp_erp_port_reopen(port, ZFCP_STATUS_COMMON_ERP_FAILED, id, ref);
L
Linus Torvalds 已提交
1625 1626
}

1627 1628 1629 1630 1631 1632
/**
 * 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
 */
1633
void zfcp_erp_adapter_access_changed(struct zfcp_adapter *adapter, char *id,
1634
				     void *ref)
L
Linus Torvalds 已提交
1635
{
1636 1637 1638 1639
	struct zfcp_port *port;
	unsigned long flags;

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

1642 1643
	read_lock_irqsave(&zfcp_data.config_lock, flags);
	list_for_each_entry(port, &adapter->port_list_head, list)
1644
		zfcp_erp_port_access_changed(port, id, ref);
1645
	read_unlock_irqrestore(&zfcp_data.config_lock, flags);
L
Linus Torvalds 已提交
1646
}