zfcp_erp.c 44.9 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, 2010
L
Linus Torvalds 已提交
7 8
 */

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

12
#include <linux/kthread.h>
L
Linus Torvalds 已提交
13
#include "zfcp_ext.h"
14
#include "zfcp_reqlist.h"
L
Linus Torvalds 已提交
15

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

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

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

	list_move(&act->list, &act->adapter->erp_ready_head);
S
Swen Schillig 已提交
79
	zfcp_dbf_rec_action("erardy1", act);
80
	wake_up(&adapter->erp_ready_wq);
S
Swen Schillig 已提交
81
	zfcp_dbf_rec_thread("erardy2", adapter->dbf);
82 83
}

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

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

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

101 102
	if (atomic_read(&port->status) & ZFCP_STATUS_COMMON_ERP_INUSE)
		zfcp_erp_action_dismiss(&port->erp_action);
103 104 105 106 107 108
	else {
		read_lock(&port->unit_list_lock);
		list_for_each_entry(unit, &port->unit_list, list)
			zfcp_erp_action_dismiss_unit(unit);
		read_unlock(&port->unit_list_lock);
	}
L
Linus Torvalds 已提交
109 110
}

111
static void zfcp_erp_action_dismiss_adapter(struct zfcp_adapter *adapter)
L
Linus Torvalds 已提交
112
{
113
	struct zfcp_port *port;
L
Linus Torvalds 已提交
114

115 116
	if (atomic_read(&adapter->status) & ZFCP_STATUS_COMMON_ERP_INUSE)
		zfcp_erp_action_dismiss(&adapter->erp_action);
117 118 119
	else {
		read_lock(&adapter->port_list_lock);
		list_for_each_entry(port, &adapter->port_list, list)
120
		    zfcp_erp_action_dismiss_port(port);
121 122
		read_unlock(&adapter->port_list_lock);
	}
L
Linus Torvalds 已提交
123 124
}

125 126 127
static int zfcp_erp_required_act(int want, struct zfcp_adapter *adapter,
				 struct zfcp_port *port,
				 struct zfcp_unit *unit)
L
Linus Torvalds 已提交
128
{
129 130
	int need = want;
	int u_status, p_status, a_status;
L
Linus Torvalds 已提交
131

132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
	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;
160 161 162
		if (!(a_status & ZFCP_STATUS_COMMON_RUNNING) &&
		    !(a_status & ZFCP_STATUS_COMMON_OPEN))
			return 0; /* shutdown requested for closed adapter */
163
	}
L
Linus Torvalds 已提交
164

165
	return need;
L
Linus Torvalds 已提交
166 167
}

168 169 170 171
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 已提交
172
{
173 174
	struct zfcp_erp_action *erp_action;
	u32 status = 0;
L
Linus Torvalds 已提交
175

176 177
	switch (need) {
	case ZFCP_ERP_ACTION_REOPEN_UNIT:
178
		if (!get_device(&unit->dev))
179
			return NULL;
180 181 182 183 184
		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 已提交
185

186 187
	case ZFCP_ERP_ACTION_REOPEN_PORT:
	case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
188
		if (!get_device(&port->dev))
189
			return NULL;
190 191 192 193 194 195
		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 已提交
196

197
	case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
198
		kref_get(&adapter->ref);
199 200 201 202 203 204 205 206 207 208 209
		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 已提交
210

211 212 213 214 215 216
	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 已提交
217

218
	return erp_action;
L
Linus Torvalds 已提交
219 220
}

221 222
static int zfcp_erp_action_enqueue(int want, struct zfcp_adapter *adapter,
				   struct zfcp_port *port,
223
				   struct zfcp_unit *unit, char *id, void *ref)
L
Linus Torvalds 已提交
224
{
225 226
	int retval = 1, need;
	struct zfcp_erp_action *act = NULL;
L
Linus Torvalds 已提交
227

228
	if (!adapter->erp_thread)
229
		return -EIO;
L
Linus Torvalds 已提交
230

231 232
	need = zfcp_erp_required_act(want, adapter, port, unit);
	if (!need)
L
Linus Torvalds 已提交
233 234
		goto out;

235 236 237 238 239 240
	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);
241
	wake_up(&adapter->erp_ready_wq);
S
Swen Schillig 已提交
242
	zfcp_dbf_rec_thread("eracte1", adapter->dbf);
243
	retval = 0;
L
Linus Torvalds 已提交
244
 out:
S
Swen Schillig 已提交
245
	zfcp_dbf_rec_trigger(id, ref, want, need, act, adapter, port, unit);
L
Linus Torvalds 已提交
246 247 248
	return retval;
}

249
static int _zfcp_erp_adapter_reopen(struct zfcp_adapter *adapter,
250
				    int clear_mask, char *id, void *ref)
251 252
{
	zfcp_erp_adapter_block(adapter, clear_mask);
253
	zfcp_scsi_schedule_rports_block(adapter);
254 255 256

	/* ensure propagation of failed status to new devices */
	if (atomic_read(&adapter->status) & ZFCP_STATUS_COMMON_ERP_FAILED) {
257
		zfcp_erp_adapter_failed(adapter, "erareo1", NULL);
258 259 260 261 262 263 264 265 266 267 268 269
		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 已提交
270
 */
271
void zfcp_erp_adapter_reopen(struct zfcp_adapter *adapter, int clear,
272
			     char *id, void *ref)
L
Linus Torvalds 已提交
273 274 275
{
	unsigned long flags;

276 277 278 279 280 281 282 283 284 285
	zfcp_erp_adapter_block(adapter, clear);
	zfcp_scsi_schedule_rports_block(adapter);

	write_lock_irqsave(&adapter->erp_lock, flags);
	if (atomic_read(&adapter->status) & ZFCP_STATUS_COMMON_ERP_FAILED)
		zfcp_erp_adapter_failed(adapter, "erareo1", NULL);
	else
		zfcp_erp_action_enqueue(ZFCP_ERP_ACTION_REOPEN_ADAPTER, adapter,
					NULL, NULL, id, ref);
	write_unlock_irqrestore(&adapter->erp_lock, flags);
286
}
L
Linus Torvalds 已提交
287

288 289 290 291 292 293 294 295
/**
 * 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,
296
			       char *id, void *ref)
297 298 299
{
	int flags = ZFCP_STATUS_COMMON_RUNNING | ZFCP_STATUS_COMMON_ERP_FAILED;
	zfcp_erp_adapter_reopen(adapter, clear | flags, id, ref);
L
Linus Torvalds 已提交
300 301
}

302 303 304 305 306 307
/**
 * 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 已提交
308
 */
309 310
void zfcp_erp_port_shutdown(struct zfcp_port *port, int clear, char *id,
			    void *ref)
L
Linus Torvalds 已提交
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.
 */
323 324
void zfcp_erp_unit_shutdown(struct zfcp_unit *unit, int clear, char *id,
			    void *ref)
325 326 327 328 329 330 331
{
	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)
{
332
	zfcp_erp_modify_port_status(port, "erpblk1", NULL,
333 334 335 336 337
				    ZFCP_STATUS_COMMON_UNBLOCKED | clear,
				    ZFCP_CLEAR);
}

static void _zfcp_erp_port_forced_reopen(struct zfcp_port *port,
338
					 int clear, char *id, void *ref)
339 340
{
	zfcp_erp_port_block(port, clear);
341
	zfcp_scsi_schedule_rport_block(port);
342 343 344

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

346 347 348 349 350 351 352 353 354 355
	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.
 */
356
void zfcp_erp_port_forced_reopen(struct zfcp_port *port, int clear, char *id,
357 358 359 360 361
				 void *ref)
{
	unsigned long flags;
	struct zfcp_adapter *adapter = port->adapter;

362
	write_lock_irqsave(&adapter->erp_lock, flags);
363
	_zfcp_erp_port_forced_reopen(port, clear, id, ref);
364
	write_unlock_irqrestore(&adapter->erp_lock, flags);
365 366
}

367
static int _zfcp_erp_port_reopen(struct zfcp_port *port, int clear, char *id,
368 369 370
				 void *ref)
{
	zfcp_erp_port_block(port, clear);
371
	zfcp_scsi_schedule_rport_block(port);
L
Linus Torvalds 已提交
372

373
	if (atomic_read(&port->status) & ZFCP_STATUS_COMMON_ERP_FAILED) {
L
Linus Torvalds 已提交
374
		/* ensure propagation of failed status to new devices */
375
		zfcp_erp_port_failed(port, "erpreo1", NULL);
376
		return -EIO;
L
Linus Torvalds 已提交
377 378
	}

379 380
	return zfcp_erp_action_enqueue(ZFCP_ERP_ACTION_REOPEN_PORT,
				       port->adapter, port, NULL, id, ref);
L
Linus Torvalds 已提交
381 382 383
}

/**
384 385 386
 * zfcp_erp_port_reopen - trigger remote port recovery
 * @port: port to recover
 * @clear_mask: flags in port status to be cleared
L
Linus Torvalds 已提交
387
 *
388
 * Returns 0 if recovery has been triggered, < 0 if not.
L
Linus Torvalds 已提交
389
 */
390
int zfcp_erp_port_reopen(struct zfcp_port *port, int clear, char *id, void *ref)
L
Linus Torvalds 已提交
391
{
392
	int retval;
393
	unsigned long flags;
L
Linus Torvalds 已提交
394 395
	struct zfcp_adapter *adapter = port->adapter;

396
	write_lock_irqsave(&adapter->erp_lock, flags);
397
	retval = _zfcp_erp_port_reopen(port, clear, id, ref);
398
	write_unlock_irqrestore(&adapter->erp_lock, flags);
L
Linus Torvalds 已提交
399 400 401 402

	return retval;
}

403 404
static void zfcp_erp_unit_block(struct zfcp_unit *unit, int clear_mask)
{
405
	zfcp_erp_modify_unit_status(unit, "erublk1", NULL,
406 407 408 409
				    ZFCP_STATUS_COMMON_UNBLOCKED | clear_mask,
				    ZFCP_CLEAR);
}

410
static void _zfcp_erp_unit_reopen(struct zfcp_unit *unit, int clear, char *id,
411
				  void *ref)
L
Linus Torvalds 已提交
412 413 414
{
	struct zfcp_adapter *adapter = unit->port->adapter;

415
	zfcp_erp_unit_block(unit, clear);
L
Linus Torvalds 已提交
416

417 418
	if (atomic_read(&unit->status) & ZFCP_STATUS_COMMON_ERP_FAILED)
		return;
L
Linus Torvalds 已提交
419

420 421
	zfcp_erp_action_enqueue(ZFCP_ERP_ACTION_REOPEN_UNIT,
				adapter, unit->port, unit, id, ref);
L
Linus Torvalds 已提交
422 423 424 425 426 427 428 429
}

/**
 * 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
 */
430 431
void zfcp_erp_unit_reopen(struct zfcp_unit *unit, int clear, char *id,
			  void *ref)
L
Linus Torvalds 已提交
432 433
{
	unsigned long flags;
434 435
	struct zfcp_port *port = unit->port;
	struct zfcp_adapter *adapter = port->adapter;
L
Linus Torvalds 已提交
436

437
	write_lock_irqsave(&adapter->erp_lock, flags);
438
	_zfcp_erp_unit_reopen(unit, clear, id, ref);
439
	write_unlock_irqrestore(&adapter->erp_lock, flags);
L
Linus Torvalds 已提交
440 441
}

442
static int status_change_set(unsigned long mask, atomic_t *status)
L
Linus Torvalds 已提交
443
{
444
	return (atomic_read(status) ^ mask) & mask;
L
Linus Torvalds 已提交
445 446
}

447
static int status_change_clear(unsigned long mask, atomic_t *status)
448
{
449
	return atomic_read(status) & mask;
450 451
}

452
static void zfcp_erp_adapter_unblock(struct zfcp_adapter *adapter)
453
{
454
	if (status_change_set(ZFCP_STATUS_COMMON_UNBLOCKED, &adapter->status))
S
Swen Schillig 已提交
455
		zfcp_dbf_rec_adapter("eraubl1", NULL, adapter->dbf);
456
	atomic_set_mask(ZFCP_STATUS_COMMON_UNBLOCKED, &adapter->status);
457 458
}

459
static void zfcp_erp_port_unblock(struct zfcp_port *port)
L
Linus Torvalds 已提交
460
{
461
	if (status_change_set(ZFCP_STATUS_COMMON_UNBLOCKED, &port->status))
S
Swen Schillig 已提交
462
		zfcp_dbf_rec_port("erpubl1", NULL, port);
463
	atomic_set_mask(ZFCP_STATUS_COMMON_UNBLOCKED, &port->status);
L
Linus Torvalds 已提交
464 465
}

466
static void zfcp_erp_unit_unblock(struct zfcp_unit *unit)
L
Linus Torvalds 已提交
467
{
468
	if (status_change_set(ZFCP_STATUS_COMMON_UNBLOCKED, &unit->status))
S
Swen Schillig 已提交
469
		zfcp_dbf_rec_unit("eruubl1", NULL, unit);
470
	atomic_set_mask(ZFCP_STATUS_COMMON_UNBLOCKED, &unit->status);
L
Linus Torvalds 已提交
471 472
}

473
static void zfcp_erp_action_to_running(struct zfcp_erp_action *erp_action)
L
Linus Torvalds 已提交
474
{
475
	list_move(&erp_action->list, &erp_action->adapter->erp_running_head);
S
Swen Schillig 已提交
476
	zfcp_dbf_rec_action("erator1", erp_action);
L
Linus Torvalds 已提交
477 478
}

479
static void zfcp_erp_strategy_check_fsfreq(struct zfcp_erp_action *act)
L
Linus Torvalds 已提交
480
{
481
	struct zfcp_adapter *adapter = act->adapter;
482
	struct zfcp_fsf_req *req;
L
Linus Torvalds 已提交
483

484
	if (!act->fsf_req_id)
485
		return;
L
Linus Torvalds 已提交
486

487 488
	spin_lock(&adapter->req_list->lock);
	req = _zfcp_reqlist_find(adapter->req_list, act->fsf_req_id);
489
	if (req && req->erp_action == act) {
490 491
		if (act->status & (ZFCP_STATUS_ERP_DISMISSED |
				   ZFCP_STATUS_ERP_TIMEDOUT)) {
492
			req->status |= ZFCP_STATUS_FSFREQ_DISMISSED;
S
Swen Schillig 已提交
493
			zfcp_dbf_rec_action("erscf_1", act);
494
			req->erp_action = NULL;
L
Linus Torvalds 已提交
495
		}
496
		if (act->status & ZFCP_STATUS_ERP_TIMEDOUT)
S
Swen Schillig 已提交
497
			zfcp_dbf_rec_action("erscf_2", act);
498 499
		if (req->status & ZFCP_STATUS_FSFREQ_DISMISSED)
			act->fsf_req_id = 0;
500
	} else
501
		act->fsf_req_id = 0;
502
	spin_unlock(&adapter->req_list->lock);
L
Linus Torvalds 已提交
503 504
}

505 506 507 508
/**
 * zfcp_erp_notify - Trigger ERP action.
 * @erp_action: ERP action to continue.
 * @set_mask: ERP action status flags to set.
L
Linus Torvalds 已提交
509
 */
510
void zfcp_erp_notify(struct zfcp_erp_action *erp_action, unsigned long set_mask)
L
Linus Torvalds 已提交
511 512
{
	struct zfcp_adapter *adapter = erp_action->adapter;
513
	unsigned long flags;
L
Linus Torvalds 已提交
514

515
	write_lock_irqsave(&adapter->erp_lock, flags);
L
Linus Torvalds 已提交
516 517 518 519 520 521 522
	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);
}

523 524 525
/**
 * zfcp_erp_timeout_handler - Trigger ERP action from timed out ERP request
 * @data: ERP action (from timer data)
L
Linus Torvalds 已提交
526
 */
527
void zfcp_erp_timeout_handler(unsigned long data)
L
Linus Torvalds 已提交
528
{
529 530
	struct zfcp_erp_action *act = (struct zfcp_erp_action *) data;
	zfcp_erp_notify(act, ZFCP_STATUS_ERP_TIMEDOUT);
L
Linus Torvalds 已提交
531 532
}

533
static void zfcp_erp_memwait_handler(unsigned long data)
L
Linus Torvalds 已提交
534
{
535
	zfcp_erp_notify((struct zfcp_erp_action *)data, 0);
L
Linus Torvalds 已提交
536 537
}

538
static void zfcp_erp_strategy_memwait(struct zfcp_erp_action *erp_action)
L
Linus Torvalds 已提交
539
{
540 541 542 543 544
	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 已提交
545 546
}

547
static void _zfcp_erp_port_reopen_all(struct zfcp_adapter *adapter,
548
				      int clear, char *id, void *ref)
L
Linus Torvalds 已提交
549
{
550
	struct zfcp_port *port;
L
Linus Torvalds 已提交
551

552 553
	read_lock(&adapter->port_list_lock);
	list_for_each_entry(port, &adapter->port_list, list)
554
		_zfcp_erp_port_reopen(port, clear, id, ref);
555
	read_unlock(&adapter->port_list_lock);
L
Linus Torvalds 已提交
556 557
}

558 559
static void _zfcp_erp_unit_reopen_all(struct zfcp_port *port, int clear,
				      char *id, void *ref)
L
Linus Torvalds 已提交
560
{
561
	struct zfcp_unit *unit;
L
Linus Torvalds 已提交
562

563 564
	read_lock(&port->unit_list_lock);
	list_for_each_entry(unit, &port->unit_list, list)
565
		_zfcp_erp_unit_reopen(unit, clear, id, ref);
566
	read_unlock(&port->unit_list_lock);
L
Linus Torvalds 已提交
567 568
}

569
static void zfcp_erp_strategy_followup_failed(struct zfcp_erp_action *act)
L
Linus Torvalds 已提交
570
{
571 572
	switch (act->action) {
	case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
573
		_zfcp_erp_adapter_reopen(act->adapter, 0, "ersff_1", NULL);
574 575
		break;
	case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
576
		_zfcp_erp_port_forced_reopen(act->port, 0, "ersff_2", NULL);
577 578
		break;
	case ZFCP_ERP_ACTION_REOPEN_PORT:
579
		_zfcp_erp_port_reopen(act->port, 0, "ersff_3", NULL);
580 581
		break;
	case ZFCP_ERP_ACTION_REOPEN_UNIT:
582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597
		_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);
598
		break;
L
Linus Torvalds 已提交
599 600 601
	}
}

602
static void zfcp_erp_wakeup(struct zfcp_adapter *adapter)
L
Linus Torvalds 已提交
603 604 605
{
	unsigned long flags;

606
	read_lock_irqsave(&adapter->erp_lock, flags);
607 608 609 610 611
	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 已提交
612
	}
613
	read_unlock_irqrestore(&adapter->erp_lock, flags);
614
}
L
Linus Torvalds 已提交
615

616 617
static int zfcp_erp_adapter_strategy_open_qdio(struct zfcp_erp_action *act)
{
618 619 620
	struct zfcp_qdio *qdio = act->adapter->qdio;

	if (zfcp_qdio_open(qdio))
621
		return ZFCP_ERP_FAILED;
622
	init_waitqueue_head(&qdio->req_q_wq);
623 624 625
	atomic_set_mask(ZFCP_STATUS_ADAPTER_QDIOUP, &act->adapter->status);
	return ZFCP_ERP_SUCCEEDED;
}
L
Linus Torvalds 已提交
626

627 628 629 630 631 632 633
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;
634
	_zfcp_erp_port_reopen(port, 0, "ereptp1", NULL);
635
}
L
Linus Torvalds 已提交
636

637 638 639 640 641
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 已提交
642

643 644 645 646 647 648 649 650 651 652 653 654
	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 已提交
655 656
		}

S
Swen Schillig 已提交
657
		zfcp_dbf_rec_thread_lock("erasfx1", adapter->dbf);
658 659
		wait_event(adapter->erp_ready_wq,
			   !list_empty(&adapter->erp_ready_head));
S
Swen Schillig 已提交
660
		zfcp_dbf_rec_thread_lock("erasfx2", adapter->dbf);
661 662
		if (erp_action->status & ZFCP_STATUS_ERP_TIMEDOUT)
			break;
L
Linus Torvalds 已提交
663

664 665 666
		if (!(atomic_read(&adapter->status) &
		      ZFCP_STATUS_ADAPTER_HOST_CON_INIT))
			break;
L
Linus Torvalds 已提交
667

668 669
		ssleep(sleep);
		sleep *= 2;
L
Linus Torvalds 已提交
670 671
	}

672 673
	atomic_clear_mask(ZFCP_STATUS_ADAPTER_HOST_CON_INIT,
			  &adapter->status);
L
Linus Torvalds 已提交
674

675 676
	if (!(atomic_read(&adapter->status) & ZFCP_STATUS_ADAPTER_XCONFIG_OK))
		return ZFCP_ERP_FAILED;
S
Swen Schillig 已提交
677

678 679
	if (fc_host_port_type(adapter->scsi_host) == FC_PORTTYPE_PTP)
		zfcp_erp_enqueue_ptp_port(adapter);
L
Linus Torvalds 已提交
680

681
	return ZFCP_ERP_SUCCEEDED;
L
Linus Torvalds 已提交
682 683
}

684
static int zfcp_erp_adapter_strategy_open_fsf_xport(struct zfcp_erp_action *act)
L
Linus Torvalds 已提交
685
{
686 687
	int ret;
	struct zfcp_adapter *adapter = act->adapter;
L
Linus Torvalds 已提交
688

689 690 691 692 693 694 695 696 697 698
	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;

S
Swen Schillig 已提交
699
	zfcp_dbf_rec_thread_lock("erasox1", adapter->dbf);
700 701
	wait_event(adapter->erp_ready_wq,
		   !list_empty(&adapter->erp_ready_head));
S
Swen Schillig 已提交
702
	zfcp_dbf_rec_thread_lock("erasox2", adapter->dbf);
703 704 705 706
	if (act->status & ZFCP_STATUS_ERP_TIMEDOUT)
		return ZFCP_ERP_FAILED;

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

709
static int zfcp_erp_adapter_strategy_open_fsf(struct zfcp_erp_action *act)
L
Linus Torvalds 已提交
710
{
711 712
	if (zfcp_erp_adapter_strat_fsf_xconf(act) == ZFCP_ERP_FAILED)
		return ZFCP_ERP_FAILED;
L
Linus Torvalds 已提交
713

714 715
	if (zfcp_erp_adapter_strategy_open_fsf_xport(act) == ZFCP_ERP_FAILED)
		return ZFCP_ERP_FAILED;
L
Linus Torvalds 已提交
716

717
	atomic_set(&act->adapter->stat_miss, act->adapter->stat_read_buf_num);
718 719
	if (zfcp_status_read_refill(act->adapter))
		return ZFCP_ERP_FAILED;
L
Linus Torvalds 已提交
720

721 722
	return ZFCP_ERP_SUCCEEDED;
}
L
Linus Torvalds 已提交
723

724
static void zfcp_erp_adapter_strategy_close(struct zfcp_erp_action *act)
725 726
{
	struct zfcp_adapter *adapter = act->adapter;
L
Linus Torvalds 已提交
727

728
	/* close queues to ensure that buffers are not accessed by adapter */
729
	zfcp_qdio_close(adapter->qdio);
730 731
	zfcp_fsf_req_dismiss_all(adapter);
	adapter->fsf_req_seq_no = 0;
732
	zfcp_fc_wka_ports_force_offline(adapter->gs);
733
	/* all ports and units are closed */
734
	zfcp_erp_modify_adapter_status(adapter, "erascl1", NULL,
735
				       ZFCP_STATUS_COMMON_OPEN, ZFCP_CLEAR);
736

737
	atomic_clear_mask(ZFCP_STATUS_ADAPTER_XCONFIG_OK |
738
			  ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED, &adapter->status);
L
Linus Torvalds 已提交
739 740
}

741
static int zfcp_erp_adapter_strategy_open(struct zfcp_erp_action *act)
L
Linus Torvalds 已提交
742
{
743
	struct zfcp_adapter *adapter = act->adapter;
L
Linus Torvalds 已提交
744

745 746 747 748 749 750
	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;
	}
751

752 753 754 755 756 757 758 759 760
	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;
}
761

762 763 764 765 766 767 768 769 770 771 772
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)) {
773
		ssleep(8);
774 775
		return ZFCP_ERP_FAILED;
	}
L
Linus Torvalds 已提交
776

777
	return ZFCP_ERP_SUCCEEDED;
L
Linus Torvalds 已提交
778 779
}

780
static int zfcp_erp_port_forced_strategy_close(struct zfcp_erp_action *act)
L
Linus Torvalds 已提交
781
{
782 783 784 785 786 787 788 789 790 791
	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 已提交
792 793
}

794
static void zfcp_erp_port_strategy_clearstati(struct zfcp_port *port)
L
Linus Torvalds 已提交
795
{
796
	atomic_clear_mask(ZFCP_STATUS_COMMON_ACCESS_DENIED, &port->status);
797
}
L
Linus Torvalds 已提交
798

799 800 801 802 803 804 805 806 807 808 809 810 811 812 813
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:
814
		if (!(status & ZFCP_STATUS_PORT_PHYS_OPEN))
815 816 817
			return ZFCP_ERP_SUCCEEDED;
	}
	return ZFCP_ERP_FAILED;
L
Linus Torvalds 已提交
818 819
}

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

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

833
static int zfcp_erp_port_strategy_open_port(struct zfcp_erp_action *erp_action)
L
Linus Torvalds 已提交
834
{
835
	int retval;
L
Linus Torvalds 已提交
836

837 838 839 840 841 842 843 844
	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 已提交
845

846
static int zfcp_erp_open_ptp_port(struct zfcp_erp_action *act)
L
Linus Torvalds 已提交
847
{
848 849
	struct zfcp_adapter *adapter = act->adapter;
	struct zfcp_port *port = act->port;
L
Linus Torvalds 已提交
850

851
	if (port->wwpn != adapter->peer_wwpn) {
852
		zfcp_erp_port_failed(port, "eroptp1", NULL);
853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870
		return ZFCP_ERP_FAILED;
	}
	port->d_id = adapter->peer_d_id;
	return zfcp_erp_port_strategy_open_port(act);
}

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);
871
		if (!port->d_id) {
872
			zfcp_fc_trigger_did_lookup(port);
873
			return ZFCP_ERP_EXIT;
874 875 876 877 878
		}
		return zfcp_erp_port_strategy_open_port(act);

	case ZFCP_ERP_STEP_PORT_OPENING:
		/* D_ID might have changed during open */
879
		if (p_status & ZFCP_STATUS_COMMON_OPEN) {
880 881 882
			if (!port->d_id) {
				zfcp_fc_trigger_did_lookup(port);
				return ZFCP_ERP_EXIT;
883
			}
884
			return ZFCP_ERP_SUCCEEDED;
885
		}
886 887 888 889 890 891
		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 */
892 893 894 895 896 897 898
	}
	return ZFCP_ERP_FAILED;
}

static int zfcp_erp_port_strategy(struct zfcp_erp_action *erp_action)
{
	struct zfcp_port *port = erp_action->port;
899
	int p_status = atomic_read(&port->status);
900

901 902
	if ((p_status & ZFCP_STATUS_COMMON_NOESC) &&
	    !(p_status & ZFCP_STATUS_COMMON_OPEN))
903 904
		goto close_init_done;

905 906 907
	switch (erp_action->step) {
	case ZFCP_ERP_STEP_UNINITIALIZED:
		zfcp_erp_port_strategy_clearstati(port);
908
		if (p_status & ZFCP_STATUS_COMMON_OPEN)
909
			return zfcp_erp_port_strategy_close(erp_action);
L
Linus Torvalds 已提交
910 911
		break;

912
	case ZFCP_ERP_STEP_PORT_CLOSING:
913
		if (p_status & ZFCP_STATUS_COMMON_OPEN)
914
			return ZFCP_ERP_FAILED;
L
Linus Torvalds 已提交
915 916
		break;
	}
917 918

close_init_done:
919 920
	if (erp_action->status & ZFCP_STATUS_ERP_CLOSE_ONLY)
		return ZFCP_ERP_EXIT;
L
Linus Torvalds 已提交
921

922
	return zfcp_erp_port_strategy_open_common(erp_action);
923 924 925 926
}

static void zfcp_erp_unit_strategy_clearstati(struct zfcp_unit *unit)
{
927
	atomic_clear_mask(ZFCP_STATUS_COMMON_ACCESS_DENIED |
928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952
			  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 已提交
953 954
}

955
static int zfcp_erp_unit_strategy(struct zfcp_erp_action *erp_action)
L
Linus Torvalds 已提交
956
{
957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976
	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 已提交
977 978
}

979
static int zfcp_erp_strategy_check_unit(struct zfcp_unit *unit, int result)
L
Linus Torvalds 已提交
980 981 982 983 984 985 986 987
{
	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);
988 989 990 991
		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",
992 993
				(unsigned long long)unit->fcp_lun,
				(unsigned long long)unit->port->wwpn);
994
			zfcp_erp_unit_failed(unit, "erusck1", NULL);
995
		}
L
Linus Torvalds 已提交
996 997 998
		break;
	}

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

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

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

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

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

L
Linus Torvalds 已提交
1045 1046
	case ZFCP_ERP_FAILED :
		atomic_inc(&adapter->erp_counter);
1047 1048 1049 1050
		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");
1051
			zfcp_erp_adapter_failed(adapter, "erasck1", NULL);
1052
		}
L
Linus Torvalds 已提交
1053 1054 1055
		break;
	}

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

1063 1064
static int zfcp_erp_strategy_check_target(struct zfcp_erp_action *erp_action,
					  int result)
1065
{
1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085
	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;
1086 1087
}

1088
static int zfcp_erp_strat_change_det(atomic_t *target_status, u32 erp_status)
1089
{
1090
	int status = atomic_read(target_status);
1091

1092 1093 1094
	if ((status & ZFCP_STATUS_COMMON_RUNNING) &&
	    (erp_status & ZFCP_STATUS_ERP_CLOSE_ONLY))
		return 1; /* take it online */
1095

1096 1097 1098 1099 1100
	if (!(status & ZFCP_STATUS_COMMON_RUNNING) &&
	    !(erp_status & ZFCP_STATUS_ERP_CLOSE_ONLY))
		return 1; /* take it offline */

	return 0;
1101 1102
}

1103
static int zfcp_erp_strategy_statechange(struct zfcp_erp_action *act, int ret)
L
Linus Torvalds 已提交
1104
{
1105 1106 1107 1108 1109
	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 已提交
1110

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

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

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

1143
static void zfcp_erp_action_dequeue(struct zfcp_erp_action *erp_action)
L
Linus Torvalds 已提交
1144
{
1145
	struct zfcp_adapter *adapter = erp_action->adapter;
L
Linus Torvalds 已提交
1146

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

1153
	list_del(&erp_action->list);
S
Swen Schillig 已提交
1154
	zfcp_dbf_rec_action("eractd1", erp_action);
L
Linus Torvalds 已提交
1155

1156 1157 1158 1159 1160
	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 已提交
1161

1162 1163 1164 1165 1166
	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 已提交
1167

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

1175
static void zfcp_erp_action_cleanup(struct zfcp_erp_action *act, int result)
L
Linus Torvalds 已提交
1176
{
1177 1178 1179
	struct zfcp_adapter *adapter = act->adapter;
	struct zfcp_port *port = act->port;
	struct zfcp_unit *unit = act->unit;
L
Linus Torvalds 已提交
1180

1181 1182
	switch (act->action) {
	case ZFCP_ERP_ACTION_REOPEN_UNIT:
1183
		if ((result == ZFCP_ERP_SUCCEEDED) && !unit->device) {
1184
			get_device(&unit->dev);
1185 1186
			if (scsi_queue_work(unit->port->adapter->scsi_host,
					    &unit->scsi_work) <= 0)
1187
				put_device(&unit->dev);
1188
		}
1189
		put_device(&unit->dev);
1190
		break;
L
Linus Torvalds 已提交
1191

1192 1193
	case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
	case ZFCP_ERP_ACTION_REOPEN_PORT:
1194 1195
		if (result == ZFCP_ERP_SUCCEEDED)
			zfcp_scsi_schedule_rport_register(port);
1196
		put_device(&port->dev);
1197 1198 1199
		break;

	case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
1200
		if (result == ZFCP_ERP_SUCCEEDED) {
1201
			register_service_level(&adapter->service_level);
1202
			queue_work(adapter->work_queue, &adapter->scan_work);
1203 1204
		} else
			unregister_service_level(&adapter->service_level);
1205
		kref_put(&adapter->ref, zfcp_adapter_release);
1206 1207
		break;
	}
L
Linus Torvalds 已提交
1208 1209
}

1210
static int zfcp_erp_strategy_do_action(struct zfcp_erp_action *erp_action)
L
Linus Torvalds 已提交
1211
{
1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222
	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 已提交
1223 1224
}

1225
static int zfcp_erp_strategy(struct zfcp_erp_action *erp_action)
L
Linus Torvalds 已提交
1226 1227
{
	int retval;
1228
	unsigned long flags;
1229
	struct zfcp_adapter *adapter = erp_action->adapter;
L
Linus Torvalds 已提交
1230

1231
	kref_get(&adapter->ref);
L
Linus Torvalds 已提交
1232

1233
	write_lock_irqsave(&adapter->erp_lock, flags);
1234
	zfcp_erp_strategy_check_fsfreq(erp_action);
L
Linus Torvalds 已提交
1235

1236 1237 1238 1239
	if (erp_action->status & ZFCP_STATUS_ERP_DISMISSED) {
		zfcp_erp_action_dequeue(erp_action);
		retval = ZFCP_ERP_DISMISSED;
		goto unlock;
1240
	}
L
Linus Torvalds 已提交
1241

1242
	zfcp_erp_action_to_running(erp_action);
L
Linus Torvalds 已提交
1243

1244
	/* no lock to allow for blocking operations */
1245
	write_unlock_irqrestore(&adapter->erp_lock, flags);
1246
	retval = zfcp_erp_strategy_do_action(erp_action);
1247
	write_lock_irqsave(&adapter->erp_lock, flags);
L
Linus Torvalds 已提交
1248

1249 1250
	if (erp_action->status & ZFCP_STATUS_ERP_DISMISSED)
		retval = ZFCP_ERP_CONTINUES;
1251

1252 1253 1254 1255 1256
	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 已提交
1257
		}
1258
		if (adapter->erp_total_count == adapter->erp_low_mem_count)
1259
			_zfcp_erp_adapter_reopen(adapter, 0, "erstgy1", NULL);
1260 1261 1262
		else {
			zfcp_erp_strategy_memwait(erp_action);
			retval = ZFCP_ERP_CONTINUES;
L
Linus Torvalds 已提交
1263
		}
1264
		goto unlock;
L
Linus Torvalds 已提交
1265

1266 1267 1268 1269
	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 已提交
1270
		}
1271
		goto unlock;
L
Linus Torvalds 已提交
1272 1273
	}

1274 1275 1276 1277 1278
	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;
1279 1280 1281 1282
	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 已提交
1283

1284
 unlock:
1285
	write_unlock_irqrestore(&adapter->erp_lock, flags);
L
Linus Torvalds 已提交
1286

1287 1288
	if (retval != ZFCP_ERP_CONTINUES)
		zfcp_erp_action_cleanup(erp_action, retval);
L
Linus Torvalds 已提交
1289

1290
	kref_put(&adapter->ref, zfcp_adapter_release);
L
Linus Torvalds 已提交
1291 1292 1293
	return retval;
}

1294
static int zfcp_erp_thread(void *data)
L
Linus Torvalds 已提交
1295
{
1296 1297 1298 1299
	struct zfcp_adapter *adapter = (struct zfcp_adapter *) data;
	struct list_head *next;
	struct zfcp_erp_action *act;
	unsigned long flags;
1300

1301
	for (;;) {
S
Swen Schillig 已提交
1302
		zfcp_dbf_rec_thread_lock("erthrd1", adapter->dbf);
1303 1304 1305
		wait_event_interruptible(adapter->erp_ready_wq,
			   !list_empty(&adapter->erp_ready_head) ||
			   kthread_should_stop());
S
Swen Schillig 已提交
1306
		zfcp_dbf_rec_thread_lock("erthrd2", adapter->dbf);
1307

1308 1309 1310
		if (kthread_should_stop())
			break;

1311 1312 1313
		write_lock_irqsave(&adapter->erp_lock, flags);
		next = adapter->erp_ready_head.next;
		write_unlock_irqrestore(&adapter->erp_lock, flags);
L
Linus Torvalds 已提交
1314

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

1318 1319 1320
			/* there is more to come after dismission, no notify */
			if (zfcp_erp_strategy(act) != ZFCP_ERP_DISMISSED)
				zfcp_erp_wakeup(adapter);
L
Linus Torvalds 已提交
1321 1322 1323
		}
	}

1324 1325
	return 0;
}
L
Linus Torvalds 已提交
1326

1327 1328 1329 1330 1331 1332 1333 1334
/**
 * 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)
{
1335
	struct task_struct *thread;
L
Linus Torvalds 已提交
1336

1337 1338 1339
	thread = kthread_run(zfcp_erp_thread, adapter, "zfcperp%s",
			     dev_name(&adapter->ccw_device->dev));
	if (IS_ERR(thread)) {
1340
		dev_err(&adapter->ccw_device->dev,
1341
			"Creating an ERP thread for the FCP device failed.\n");
1342
		return PTR_ERR(thread);
L
Linus Torvalds 已提交
1343
	}
1344 1345

	adapter->erp_thread = thread;
1346 1347
	return 0;
}
L
Linus Torvalds 已提交
1348

1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359
/**
 * 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)
{
1360 1361
	kthread_stop(adapter->erp_thread);
	adapter->erp_thread = NULL;
1362 1363
	WARN_ON(!list_empty(&adapter->erp_ready_head));
	WARN_ON(!list_empty(&adapter->erp_running_head));
L
Linus Torvalds 已提交
1364 1365
}

1366 1367 1368 1369 1370 1371
/**
 * zfcp_erp_adapter_failed - Set adapter status to failed.
 * @adapter: Failed adapter.
 * @id: Event id for debug trace.
 * @ref: Reference for debug trace.
 */
1372
void zfcp_erp_adapter_failed(struct zfcp_adapter *adapter, char *id, void *ref)
L
Linus Torvalds 已提交
1373
{
1374 1375 1376
	zfcp_erp_modify_adapter_status(adapter, id, ref,
				       ZFCP_STATUS_COMMON_ERP_FAILED, ZFCP_SET);
}
L
Linus Torvalds 已提交
1377

1378 1379 1380 1381 1382 1383
/**
 * zfcp_erp_port_failed - Set port status to failed.
 * @port: Failed port.
 * @id: Event id for debug trace.
 * @ref: Reference for debug trace.
 */
1384
void zfcp_erp_port_failed(struct zfcp_port *port, char *id, void *ref)
1385 1386 1387
{
	zfcp_erp_modify_port_status(port, id, ref,
				    ZFCP_STATUS_COMMON_ERP_FAILED, ZFCP_SET);
L
Linus Torvalds 已提交
1388 1389 1390
}

/**
1391 1392 1393 1394
 * 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 已提交
1395
 */
1396
void zfcp_erp_unit_failed(struct zfcp_unit *unit, char *id, void *ref)
L
Linus Torvalds 已提交
1397
{
1398 1399
	zfcp_erp_modify_unit_status(unit, id, ref,
				    ZFCP_STATUS_COMMON_ERP_FAILED, ZFCP_SET);
L
Linus Torvalds 已提交
1400 1401
}

1402 1403 1404 1405 1406 1407 1408 1409 1410 1411
/**
 * 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 已提交
1412

1413 1414 1415 1416 1417 1418 1419 1420 1421 1422
/**
 * 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.
 */
1423
void zfcp_erp_modify_adapter_status(struct zfcp_adapter *adapter, char *id,
1424
				    void *ref, u32 mask, int set_or_clear)
L
Linus Torvalds 已提交
1425 1426
{
	struct zfcp_port *port;
1427
	unsigned long flags;
1428
	u32 common_mask = mask & ZFCP_COMMON_FLAGS;
L
Linus Torvalds 已提交
1429

1430 1431
	if (set_or_clear == ZFCP_SET) {
		if (status_change_set(mask, &adapter->status))
S
Swen Schillig 已提交
1432
			zfcp_dbf_rec_adapter(id, ref, adapter->dbf);
1433 1434 1435
		atomic_set_mask(mask, &adapter->status);
	} else {
		if (status_change_clear(mask, &adapter->status))
S
Swen Schillig 已提交
1436
			zfcp_dbf_rec_adapter(id, ref, adapter->dbf);
1437 1438 1439 1440 1441
		atomic_clear_mask(mask, &adapter->status);
		if (mask & ZFCP_STATUS_COMMON_ERP_FAILED)
			atomic_set(&adapter->erp_counter, 0);
	}

1442 1443 1444
	if (common_mask) {
		read_lock_irqsave(&adapter->port_list_lock, flags);
		list_for_each_entry(port, &adapter->port_list, list)
1445 1446
			zfcp_erp_modify_port_status(port, id, ref, common_mask,
						    set_or_clear);
1447 1448
		read_unlock_irqrestore(&adapter->port_list_lock, flags);
	}
L
Linus Torvalds 已提交
1449 1450
}

1451 1452 1453 1454 1455 1456 1457 1458 1459 1460
/**
 * 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.
 */
1461
void zfcp_erp_modify_port_status(struct zfcp_port *port, char *id, void *ref,
1462
				 u32 mask, int set_or_clear)
L
Linus Torvalds 已提交
1463 1464
{
	struct zfcp_unit *unit;
1465
	unsigned long flags;
1466
	u32 common_mask = mask & ZFCP_COMMON_FLAGS;
L
Linus Torvalds 已提交
1467

1468 1469
	if (set_or_clear == ZFCP_SET) {
		if (status_change_set(mask, &port->status))
S
Swen Schillig 已提交
1470
			zfcp_dbf_rec_port(id, ref, port);
1471 1472 1473
		atomic_set_mask(mask, &port->status);
	} else {
		if (status_change_clear(mask, &port->status))
S
Swen Schillig 已提交
1474
			zfcp_dbf_rec_port(id, ref, port);
1475 1476 1477 1478
		atomic_clear_mask(mask, &port->status);
		if (mask & ZFCP_STATUS_COMMON_ERP_FAILED)
			atomic_set(&port->erp_counter, 0);
	}
L
Linus Torvalds 已提交
1479

1480 1481 1482
	if (common_mask) {
		read_lock_irqsave(&port->unit_list_lock, flags);
		list_for_each_entry(unit, &port->unit_list, list)
1483 1484
			zfcp_erp_modify_unit_status(unit, id, ref, common_mask,
						    set_or_clear);
1485 1486
		read_unlock_irqrestore(&port->unit_list_lock, flags);
	}
L
Linus Torvalds 已提交
1487 1488
}

1489 1490 1491 1492 1493 1494 1495 1496
/**
 * 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
 */
1497
void zfcp_erp_modify_unit_status(struct zfcp_unit *unit, char *id, void *ref,
1498
				 u32 mask, int set_or_clear)
L
Linus Torvalds 已提交
1499
{
1500 1501
	if (set_or_clear == ZFCP_SET) {
		if (status_change_set(mask, &unit->status))
S
Swen Schillig 已提交
1502
			zfcp_dbf_rec_unit(id, ref, unit);
1503 1504 1505
		atomic_set_mask(mask, &unit->status);
	} else {
		if (status_change_clear(mask, &unit->status))
S
Swen Schillig 已提交
1506
			zfcp_dbf_rec_unit(id, ref, unit);
1507 1508 1509 1510 1511
		atomic_clear_mask(mask, &unit->status);
		if (mask & ZFCP_STATUS_COMMON_ERP_FAILED) {
			atomic_set(&unit->erp_counter, 0);
		}
	}
L
Linus Torvalds 已提交
1512 1513
}

1514 1515 1516 1517 1518 1519
/**
 * 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.
 */
1520
void zfcp_erp_port_boxed(struct zfcp_port *port, char *id, void *ref)
1521
{
1522 1523
	zfcp_erp_modify_port_status(port, id, ref,
				    ZFCP_STATUS_COMMON_ACCESS_BOXED, ZFCP_SET);
1524
	zfcp_erp_port_reopen(port, ZFCP_STATUS_COMMON_ERP_FAILED, id, ref);
1525 1526
}

1527 1528 1529 1530 1531 1532
/**
 * 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.
 */
1533
void zfcp_erp_unit_boxed(struct zfcp_unit *unit, char *id, void *ref)
1534
{
1535 1536
	zfcp_erp_modify_unit_status(unit, id, ref,
				    ZFCP_STATUS_COMMON_ACCESS_BOXED, ZFCP_SET);
1537
	zfcp_erp_unit_reopen(unit, ZFCP_STATUS_COMMON_ERP_FAILED, id, ref);
1538 1539
}

1540 1541 1542 1543 1544 1545 1546 1547 1548
/**
 * 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.
 */
1549
void zfcp_erp_port_access_denied(struct zfcp_port *port, char *id, void *ref)
L
Linus Torvalds 已提交
1550
{
1551 1552 1553
	zfcp_erp_modify_port_status(port, id, ref,
				    ZFCP_STATUS_COMMON_ERP_FAILED |
				    ZFCP_STATUS_COMMON_ACCESS_DENIED, ZFCP_SET);
L
Linus Torvalds 已提交
1554 1555
}

1556 1557 1558 1559 1560 1561 1562 1563
/**
 * 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.
 */
1564
void zfcp_erp_unit_access_denied(struct zfcp_unit *unit, char *id, void *ref)
L
Linus Torvalds 已提交
1565
{
1566 1567 1568
	zfcp_erp_modify_unit_status(unit, id, ref,
				    ZFCP_STATUS_COMMON_ERP_FAILED |
				    ZFCP_STATUS_COMMON_ACCESS_DENIED, ZFCP_SET);
L
Linus Torvalds 已提交
1569 1570
}

1571
static void zfcp_erp_unit_access_changed(struct zfcp_unit *unit, char *id,
1572
					 void *ref)
L
Linus Torvalds 已提交
1573
{
1574 1575 1576
	int status = atomic_read(&unit->status);
	if (!(status & (ZFCP_STATUS_COMMON_ACCESS_DENIED |
			ZFCP_STATUS_COMMON_ACCESS_BOXED)))
1577 1578
		return;

1579
	zfcp_erp_unit_reopen(unit, ZFCP_STATUS_COMMON_ERP_FAILED, id, ref);
L
Linus Torvalds 已提交
1580 1581
}

1582
static void zfcp_erp_port_access_changed(struct zfcp_port *port, char *id,
1583
					 void *ref)
L
Linus Torvalds 已提交
1584 1585
{
	struct zfcp_unit *unit;
1586
	unsigned long flags;
1587
	int status = atomic_read(&port->status);
L
Linus Torvalds 已提交
1588

1589 1590
	if (!(status & (ZFCP_STATUS_COMMON_ACCESS_DENIED |
			ZFCP_STATUS_COMMON_ACCESS_BOXED))) {
1591 1592
		read_lock_irqsave(&port->unit_list_lock, flags);
		list_for_each_entry(unit, &port->unit_list, list)
1593
				    zfcp_erp_unit_access_changed(unit, id, ref);
1594
		read_unlock_irqrestore(&port->unit_list_lock, flags);
L
Linus Torvalds 已提交
1595 1596 1597
		return;
	}

C
Christof Schmitt 已提交
1598
	zfcp_erp_port_reopen(port, ZFCP_STATUS_COMMON_ERP_FAILED, id, ref);
L
Linus Torvalds 已提交
1599 1600
}

1601 1602 1603 1604 1605 1606
/**
 * 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
 */
1607
void zfcp_erp_adapter_access_changed(struct zfcp_adapter *adapter, char *id,
1608
				     void *ref)
L
Linus Torvalds 已提交
1609
{
1610
	unsigned long flags;
1611
	struct zfcp_port *port;
1612 1613

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

1616 1617
	read_lock_irqsave(&adapter->port_list_lock, flags);
	list_for_each_entry(port, &adapter->port_list, list)
1618
		zfcp_erp_port_access_changed(port, id, ref);
1619
	read_unlock_irqrestore(&adapter->port_list_lock, flags);
L
Linus Torvalds 已提交
1620
}