zfcp_erp.c 46.2 KB
Newer Older
S
Swen Schillig 已提交
1
/*
C
Christof Schmitt 已提交
2
 * zfcp device driver
L
Linus Torvalds 已提交
3
 *
C
Christof Schmitt 已提交
4
 * Error Recovery Procedures (ERP).
S
Swen Schillig 已提交
5
 *
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
#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,
24
	ZFCP_STATUS_ERP_NO_REF		= 0x00800000,
25
};
L
Linus Torvalds 已提交
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 58
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 已提交
59
{
60
	zfcp_erp_modify_adapter_status(adapter, "erablk1", NULL,
61 62
				       ZFCP_STATUS_COMMON_UNBLOCKED | mask,
				       ZFCP_CLEAR);
63
}
L
Linus Torvalds 已提交
64

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

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

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

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

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

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

102 103
	if (atomic_read(&port->status) & ZFCP_STATUS_COMMON_ERP_INUSE)
		zfcp_erp_action_dismiss(&port->erp_action);
104 105 106 107 108 109
	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 已提交
110 111
}

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

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

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

133 134 135 136 137 138 139 140 141 142 143 144 145
	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_FORCED:
146 147 148 149 150
		p_status = atomic_read(&port->status);
		if (!(p_status & ZFCP_STATUS_COMMON_OPEN))
			need = ZFCP_ERP_ACTION_REOPEN_PORT;
		/* fall through */
	case ZFCP_ERP_ACTION_REOPEN_PORT:
151 152 153 154 155 156 157 158 159 160 161 162 163 164
		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;
165 166 167
		if (!(a_status & ZFCP_STATUS_COMMON_RUNNING) &&
		    !(a_status & ZFCP_STATUS_COMMON_OPEN))
			return 0; /* shutdown requested for closed adapter */
168
	}
L
Linus Torvalds 已提交
169

170
	return need;
L
Linus Torvalds 已提交
171 172
}

173
static struct zfcp_erp_action *zfcp_erp_setup_act(int need, u32 act_status,
174 175 176
						  struct zfcp_adapter *adapter,
						  struct zfcp_port *port,
						  struct zfcp_unit *unit)
L
Linus Torvalds 已提交
177
{
178
	struct zfcp_erp_action *erp_action;
L
Linus Torvalds 已提交
179

180 181
	switch (need) {
	case ZFCP_ERP_ACTION_REOPEN_UNIT:
182 183 184
		if (!(act_status & ZFCP_STATUS_ERP_NO_REF))
			if (!get_device(&unit->dev))
				return NULL;
185 186 187
		atomic_set_mask(ZFCP_STATUS_COMMON_ERP_INUSE, &unit->status);
		erp_action = &unit->erp_action;
		if (!(atomic_read(&unit->status) & ZFCP_STATUS_COMMON_RUNNING))
188
			act_status |= ZFCP_STATUS_ERP_CLOSE_ONLY;
189
		break;
L
Linus Torvalds 已提交
190

191 192
	case ZFCP_ERP_ACTION_REOPEN_PORT:
	case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
193
		if (!get_device(&port->dev))
194
			return NULL;
195 196 197 198
		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))
199
			act_status |= ZFCP_STATUS_ERP_CLOSE_ONLY;
200
		break;
L
Linus Torvalds 已提交
201

202
	case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
203
		kref_get(&adapter->ref);
204 205 206 207 208
		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))
209
			act_status |= ZFCP_STATUS_ERP_CLOSE_ONLY;
210 211 212 213 214
		break;

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

216 217 218 219 220
	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;
221
	erp_action->status = act_status;
L
Linus Torvalds 已提交
222

223
	return erp_action;
L
Linus Torvalds 已提交
224 225
}

226 227
static int zfcp_erp_action_enqueue(int want, struct zfcp_adapter *adapter,
				   struct zfcp_port *port,
228 229
				   struct zfcp_unit *unit, char *id, void *ref,
				   u32 act_status)
L
Linus Torvalds 已提交
230
{
231 232
	int retval = 1, need;
	struct zfcp_erp_action *act = NULL;
L
Linus Torvalds 已提交
233

234
	if (!adapter->erp_thread)
235
		return -EIO;
L
Linus Torvalds 已提交
236

237 238
	need = zfcp_erp_required_act(want, adapter, port, unit);
	if (!need)
L
Linus Torvalds 已提交
239 240
		goto out;

241
	act = zfcp_erp_setup_act(need, act_status, adapter, port, unit);
242 243
	if (!act)
		goto out;
244
	atomic_set_mask(ZFCP_STATUS_ADAPTER_ERP_PENDING, &adapter->status);
245 246
	++adapter->erp_total_count;
	list_add_tail(&act->list, &adapter->erp_ready_head);
247
	wake_up(&adapter->erp_ready_wq);
S
Swen Schillig 已提交
248
	zfcp_dbf_rec_thread("eracte1", adapter->dbf);
249
	retval = 0;
L
Linus Torvalds 已提交
250
 out:
S
Swen Schillig 已提交
251
	zfcp_dbf_rec_trigger(id, ref, want, need, act, adapter, port, unit);
L
Linus Torvalds 已提交
252 253 254
	return retval;
}

255
static int _zfcp_erp_adapter_reopen(struct zfcp_adapter *adapter,
256
				    int clear_mask, char *id, void *ref)
257 258
{
	zfcp_erp_adapter_block(adapter, clear_mask);
259
	zfcp_scsi_schedule_rports_block(adapter);
260 261 262

	/* ensure propagation of failed status to new devices */
	if (atomic_read(&adapter->status) & ZFCP_STATUS_COMMON_ERP_FAILED) {
263
		zfcp_erp_adapter_failed(adapter, "erareo1", NULL);
264 265 266
		return -EIO;
	}
	return zfcp_erp_action_enqueue(ZFCP_ERP_ACTION_REOPEN_ADAPTER,
267
				       adapter, NULL, NULL, id, ref, 0);
268 269 270 271 272 273 274 275
}

/**
 * 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 已提交
276
 */
277
void zfcp_erp_adapter_reopen(struct zfcp_adapter *adapter, int clear,
278
			     char *id, void *ref)
L
Linus Torvalds 已提交
279 280 281
{
	unsigned long flags;

282 283 284 285 286 287 288 289
	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,
290
					NULL, NULL, id, ref, 0);
291
	write_unlock_irqrestore(&adapter->erp_lock, flags);
292
}
L
Linus Torvalds 已提交
293

294 295 296 297 298 299 300 301
/**
 * 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,
302
			       char *id, void *ref)
303 304 305
{
	int flags = ZFCP_STATUS_COMMON_RUNNING | ZFCP_STATUS_COMMON_ERP_FAILED;
	zfcp_erp_adapter_reopen(adapter, clear | flags, id, ref);
L
Linus Torvalds 已提交
306 307
}

308 309 310 311 312 313
/**
 * 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 已提交
314
 */
315 316
void zfcp_erp_port_shutdown(struct zfcp_port *port, int clear, char *id,
			    void *ref)
L
Linus Torvalds 已提交
317
{
318 319 320 321 322 323
	int flags = ZFCP_STATUS_COMMON_RUNNING | ZFCP_STATUS_COMMON_ERP_FAILED;
	zfcp_erp_port_reopen(port, clear | flags, id, ref);
}

static void zfcp_erp_port_block(struct zfcp_port *port, int clear)
{
324
	zfcp_erp_modify_port_status(port, "erpblk1", NULL,
325 326 327 328 329
				    ZFCP_STATUS_COMMON_UNBLOCKED | clear,
				    ZFCP_CLEAR);
}

static void _zfcp_erp_port_forced_reopen(struct zfcp_port *port,
330
					 int clear, char *id, void *ref)
331 332
{
	zfcp_erp_port_block(port, clear);
333
	zfcp_scsi_schedule_rport_block(port);
334 335 336

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

338
	zfcp_erp_action_enqueue(ZFCP_ERP_ACTION_REOPEN_PORT_FORCED,
339
				port->adapter, port, NULL, id, ref, 0);
340 341 342 343 344 345 346 347
}

/**
 * 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.
 */
348
void zfcp_erp_port_forced_reopen(struct zfcp_port *port, int clear, char *id,
349 350 351 352 353
				 void *ref)
{
	unsigned long flags;
	struct zfcp_adapter *adapter = port->adapter;

354
	write_lock_irqsave(&adapter->erp_lock, flags);
355
	_zfcp_erp_port_forced_reopen(port, clear, id, ref);
356
	write_unlock_irqrestore(&adapter->erp_lock, flags);
357 358
}

359
static int _zfcp_erp_port_reopen(struct zfcp_port *port, int clear, char *id,
360 361 362
				 void *ref)
{
	zfcp_erp_port_block(port, clear);
363
	zfcp_scsi_schedule_rport_block(port);
L
Linus Torvalds 已提交
364

365
	if (atomic_read(&port->status) & ZFCP_STATUS_COMMON_ERP_FAILED) {
L
Linus Torvalds 已提交
366
		/* ensure propagation of failed status to new devices */
367
		zfcp_erp_port_failed(port, "erpreo1", NULL);
368
		return -EIO;
L
Linus Torvalds 已提交
369 370
	}

371
	return zfcp_erp_action_enqueue(ZFCP_ERP_ACTION_REOPEN_PORT,
372
				       port->adapter, port, NULL, id, ref, 0);
L
Linus Torvalds 已提交
373 374 375
}

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

388
	write_lock_irqsave(&adapter->erp_lock, flags);
389
	retval = _zfcp_erp_port_reopen(port, clear, id, ref);
390
	write_unlock_irqrestore(&adapter->erp_lock, flags);
L
Linus Torvalds 已提交
391 392 393 394

	return retval;
}

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

402
static void _zfcp_erp_unit_reopen(struct zfcp_unit *unit, int clear, char *id,
403
				  void *ref, u32 act_status)
L
Linus Torvalds 已提交
404 405 406
{
	struct zfcp_adapter *adapter = unit->port->adapter;

407
	zfcp_erp_unit_block(unit, clear);
L
Linus Torvalds 已提交
408

409 410
	if (atomic_read(&unit->status) & ZFCP_STATUS_COMMON_ERP_FAILED)
		return;
L
Linus Torvalds 已提交
411

412
	zfcp_erp_action_enqueue(ZFCP_ERP_ACTION_REOPEN_UNIT,
413
				adapter, unit->port, unit, id, ref, act_status);
L
Linus Torvalds 已提交
414 415 416 417 418 419 420 421
}

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

429
	write_lock_irqsave(&adapter->erp_lock, flags);
430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465
	_zfcp_erp_unit_reopen(unit, clear, id, ref, 0);
	write_unlock_irqrestore(&adapter->erp_lock, flags);
}

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

/**
 * zfcp_erp_unit_shutdown_wait - Shutdown unit and wait for erp completion
 * @unit: Unit to shut down.
 * @id: Id for debug trace event.
 *
 * Do not acquire a reference for the unit when creating the ERP
 * action. It is safe, because this function waits for the ERP to
 * complete first.
 */
void zfcp_erp_unit_shutdown_wait(struct zfcp_unit *unit, char *id)
{
	unsigned long flags;
	struct zfcp_port *port = unit->port;
	struct zfcp_adapter *adapter = port->adapter;
	int clear = ZFCP_STATUS_COMMON_RUNNING | ZFCP_STATUS_COMMON_ERP_FAILED;

	write_lock_irqsave(&adapter->erp_lock, flags);
	_zfcp_erp_unit_reopen(unit, clear, id, NULL, ZFCP_STATUS_ERP_NO_REF);
466
	write_unlock_irqrestore(&adapter->erp_lock, flags);
467 468

	zfcp_erp_wait(adapter);
L
Linus Torvalds 已提交
469 470
}

471
static int status_change_set(unsigned long mask, atomic_t *status)
L
Linus Torvalds 已提交
472
{
473
	return (atomic_read(status) ^ mask) & mask;
L
Linus Torvalds 已提交
474 475
}

476
static int status_change_clear(unsigned long mask, atomic_t *status)
477
{
478
	return atomic_read(status) & mask;
479 480
}

481
static void zfcp_erp_adapter_unblock(struct zfcp_adapter *adapter)
482
{
483
	if (status_change_set(ZFCP_STATUS_COMMON_UNBLOCKED, &adapter->status))
S
Swen Schillig 已提交
484
		zfcp_dbf_rec_adapter("eraubl1", NULL, adapter->dbf);
485
	atomic_set_mask(ZFCP_STATUS_COMMON_UNBLOCKED, &adapter->status);
486 487
}

488
static void zfcp_erp_port_unblock(struct zfcp_port *port)
L
Linus Torvalds 已提交
489
{
490
	if (status_change_set(ZFCP_STATUS_COMMON_UNBLOCKED, &port->status))
S
Swen Schillig 已提交
491
		zfcp_dbf_rec_port("erpubl1", NULL, port);
492
	atomic_set_mask(ZFCP_STATUS_COMMON_UNBLOCKED, &port->status);
L
Linus Torvalds 已提交
493 494
}

495
static void zfcp_erp_unit_unblock(struct zfcp_unit *unit)
L
Linus Torvalds 已提交
496
{
497
	if (status_change_set(ZFCP_STATUS_COMMON_UNBLOCKED, &unit->status))
S
Swen Schillig 已提交
498
		zfcp_dbf_rec_unit("eruubl1", NULL, unit);
499
	atomic_set_mask(ZFCP_STATUS_COMMON_UNBLOCKED, &unit->status);
L
Linus Torvalds 已提交
500 501
}

502
static void zfcp_erp_action_to_running(struct zfcp_erp_action *erp_action)
L
Linus Torvalds 已提交
503
{
504
	list_move(&erp_action->list, &erp_action->adapter->erp_running_head);
S
Swen Schillig 已提交
505
	zfcp_dbf_rec_action("erator1", erp_action);
L
Linus Torvalds 已提交
506 507
}

508
static void zfcp_erp_strategy_check_fsfreq(struct zfcp_erp_action *act)
L
Linus Torvalds 已提交
509
{
510
	struct zfcp_adapter *adapter = act->adapter;
511
	struct zfcp_fsf_req *req;
L
Linus Torvalds 已提交
512

513
	if (!act->fsf_req_id)
514
		return;
L
Linus Torvalds 已提交
515

516 517
	spin_lock(&adapter->req_list->lock);
	req = _zfcp_reqlist_find(adapter->req_list, act->fsf_req_id);
518
	if (req && req->erp_action == act) {
519 520
		if (act->status & (ZFCP_STATUS_ERP_DISMISSED |
				   ZFCP_STATUS_ERP_TIMEDOUT)) {
521
			req->status |= ZFCP_STATUS_FSFREQ_DISMISSED;
S
Swen Schillig 已提交
522
			zfcp_dbf_rec_action("erscf_1", act);
523
			req->erp_action = NULL;
L
Linus Torvalds 已提交
524
		}
525
		if (act->status & ZFCP_STATUS_ERP_TIMEDOUT)
S
Swen Schillig 已提交
526
			zfcp_dbf_rec_action("erscf_2", act);
527 528
		if (req->status & ZFCP_STATUS_FSFREQ_DISMISSED)
			act->fsf_req_id = 0;
529
	} else
530
		act->fsf_req_id = 0;
531
	spin_unlock(&adapter->req_list->lock);
L
Linus Torvalds 已提交
532 533
}

534 535 536 537
/**
 * zfcp_erp_notify - Trigger ERP action.
 * @erp_action: ERP action to continue.
 * @set_mask: ERP action status flags to set.
L
Linus Torvalds 已提交
538
 */
539
void zfcp_erp_notify(struct zfcp_erp_action *erp_action, unsigned long set_mask)
L
Linus Torvalds 已提交
540 541
{
	struct zfcp_adapter *adapter = erp_action->adapter;
542
	unsigned long flags;
L
Linus Torvalds 已提交
543

544
	write_lock_irqsave(&adapter->erp_lock, flags);
L
Linus Torvalds 已提交
545 546 547 548 549 550 551
	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);
}

552 553 554
/**
 * zfcp_erp_timeout_handler - Trigger ERP action from timed out ERP request
 * @data: ERP action (from timer data)
L
Linus Torvalds 已提交
555
 */
556
void zfcp_erp_timeout_handler(unsigned long data)
L
Linus Torvalds 已提交
557
{
558 559
	struct zfcp_erp_action *act = (struct zfcp_erp_action *) data;
	zfcp_erp_notify(act, ZFCP_STATUS_ERP_TIMEDOUT);
L
Linus Torvalds 已提交
560 561
}

562
static void zfcp_erp_memwait_handler(unsigned long data)
L
Linus Torvalds 已提交
563
{
564
	zfcp_erp_notify((struct zfcp_erp_action *)data, 0);
L
Linus Torvalds 已提交
565 566
}

567
static void zfcp_erp_strategy_memwait(struct zfcp_erp_action *erp_action)
L
Linus Torvalds 已提交
568
{
569 570 571 572 573
	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 已提交
574 575
}

576
static void _zfcp_erp_port_reopen_all(struct zfcp_adapter *adapter,
577
				      int clear, char *id, void *ref)
L
Linus Torvalds 已提交
578
{
579
	struct zfcp_port *port;
L
Linus Torvalds 已提交
580

581 582
	read_lock(&adapter->port_list_lock);
	list_for_each_entry(port, &adapter->port_list, list)
583
		_zfcp_erp_port_reopen(port, clear, id, ref);
584
	read_unlock(&adapter->port_list_lock);
L
Linus Torvalds 已提交
585 586
}

587 588
static void _zfcp_erp_unit_reopen_all(struct zfcp_port *port, int clear,
				      char *id, void *ref)
L
Linus Torvalds 已提交
589
{
590
	struct zfcp_unit *unit;
L
Linus Torvalds 已提交
591

592 593
	read_lock(&port->unit_list_lock);
	list_for_each_entry(unit, &port->unit_list, list)
594
		_zfcp_erp_unit_reopen(unit, clear, id, ref, 0);
595
	read_unlock(&port->unit_list_lock);
L
Linus Torvalds 已提交
596 597
}

598
static void zfcp_erp_strategy_followup_failed(struct zfcp_erp_action *act)
L
Linus Torvalds 已提交
599
{
600 601
	switch (act->action) {
	case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
602
		_zfcp_erp_adapter_reopen(act->adapter, 0, "ersff_1", NULL);
603 604
		break;
	case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
605
		_zfcp_erp_port_forced_reopen(act->port, 0, "ersff_2", NULL);
606 607
		break;
	case ZFCP_ERP_ACTION_REOPEN_PORT:
608
		_zfcp_erp_port_reopen(act->port, 0, "ersff_3", NULL);
609 610
		break;
	case ZFCP_ERP_ACTION_REOPEN_UNIT:
611
		_zfcp_erp_unit_reopen(act->unit, 0, "ersff_4", NULL, 0);
612 613 614 615 616 617 618 619 620 621 622 623 624 625 626
		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);
627
		break;
L
Linus Torvalds 已提交
628 629 630
	}
}

631
static void zfcp_erp_wakeup(struct zfcp_adapter *adapter)
L
Linus Torvalds 已提交
632 633 634
{
	unsigned long flags;

635
	read_lock_irqsave(&adapter->erp_lock, flags);
636 637 638 639 640
	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 已提交
641
	}
642
	read_unlock_irqrestore(&adapter->erp_lock, flags);
643
}
L
Linus Torvalds 已提交
644

645 646
static int zfcp_erp_adapter_strategy_open_qdio(struct zfcp_erp_action *act)
{
647 648 649
	struct zfcp_qdio *qdio = act->adapter->qdio;

	if (zfcp_qdio_open(qdio))
650
		return ZFCP_ERP_FAILED;
651
	init_waitqueue_head(&qdio->req_q_wq);
652 653 654
	atomic_set_mask(ZFCP_STATUS_ADAPTER_QDIOUP, &act->adapter->status);
	return ZFCP_ERP_SUCCEEDED;
}
L
Linus Torvalds 已提交
655

656 657 658 659 660 661 662
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;
663
	_zfcp_erp_port_reopen(port, 0, "ereptp1", NULL);
664
}
L
Linus Torvalds 已提交
665

666 667 668 669 670
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 已提交
671

672 673 674 675 676 677 678 679 680 681 682 683
	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 已提交
684 685
		}

S
Swen Schillig 已提交
686
		zfcp_dbf_rec_thread_lock("erasfx1", adapter->dbf);
687 688
		wait_event(adapter->erp_ready_wq,
			   !list_empty(&adapter->erp_ready_head));
S
Swen Schillig 已提交
689
		zfcp_dbf_rec_thread_lock("erasfx2", adapter->dbf);
690 691
		if (erp_action->status & ZFCP_STATUS_ERP_TIMEDOUT)
			break;
L
Linus Torvalds 已提交
692

693 694 695
		if (!(atomic_read(&adapter->status) &
		      ZFCP_STATUS_ADAPTER_HOST_CON_INIT))
			break;
L
Linus Torvalds 已提交
696

697 698
		ssleep(sleep);
		sleep *= 2;
L
Linus Torvalds 已提交
699 700
	}

701 702
	atomic_clear_mask(ZFCP_STATUS_ADAPTER_HOST_CON_INIT,
			  &adapter->status);
L
Linus Torvalds 已提交
703

704 705
	if (!(atomic_read(&adapter->status) & ZFCP_STATUS_ADAPTER_XCONFIG_OK))
		return ZFCP_ERP_FAILED;
S
Swen Schillig 已提交
706

707 708
	if (fc_host_port_type(adapter->scsi_host) == FC_PORTTYPE_PTP)
		zfcp_erp_enqueue_ptp_port(adapter);
L
Linus Torvalds 已提交
709

710
	return ZFCP_ERP_SUCCEEDED;
L
Linus Torvalds 已提交
711 712
}

713
static int zfcp_erp_adapter_strategy_open_fsf_xport(struct zfcp_erp_action *act)
L
Linus Torvalds 已提交
714
{
715 716
	int ret;
	struct zfcp_adapter *adapter = act->adapter;
L
Linus Torvalds 已提交
717

718 719 720 721 722 723 724 725 726 727
	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 已提交
728
	zfcp_dbf_rec_thread_lock("erasox1", adapter->dbf);
729 730
	wait_event(adapter->erp_ready_wq,
		   !list_empty(&adapter->erp_ready_head));
S
Swen Schillig 已提交
731
	zfcp_dbf_rec_thread_lock("erasox2", adapter->dbf);
732 733 734 735
	if (act->status & ZFCP_STATUS_ERP_TIMEDOUT)
		return ZFCP_ERP_FAILED;

	return ZFCP_ERP_SUCCEEDED;
L
Linus Torvalds 已提交
736 737
}

738
static int zfcp_erp_adapter_strategy_open_fsf(struct zfcp_erp_action *act)
L
Linus Torvalds 已提交
739
{
740 741
	if (zfcp_erp_adapter_strat_fsf_xconf(act) == ZFCP_ERP_FAILED)
		return ZFCP_ERP_FAILED;
L
Linus Torvalds 已提交
742

743 744
	if (zfcp_erp_adapter_strategy_open_fsf_xport(act) == ZFCP_ERP_FAILED)
		return ZFCP_ERP_FAILED;
L
Linus Torvalds 已提交
745

746 747 748 749 750 751 752 753
	if (mempool_resize(act->adapter->pool.status_read_data,
			   act->adapter->stat_read_buf_num, GFP_KERNEL))
		return ZFCP_ERP_FAILED;

	if (mempool_resize(act->adapter->pool.status_read_req,
			   act->adapter->stat_read_buf_num, GFP_KERNEL))
		return ZFCP_ERP_FAILED;

754
	atomic_set(&act->adapter->stat_miss, act->adapter->stat_read_buf_num);
755 756
	if (zfcp_status_read_refill(act->adapter))
		return ZFCP_ERP_FAILED;
L
Linus Torvalds 已提交
757

758 759
	return ZFCP_ERP_SUCCEEDED;
}
L
Linus Torvalds 已提交
760

761
static void zfcp_erp_adapter_strategy_close(struct zfcp_erp_action *act)
762 763
{
	struct zfcp_adapter *adapter = act->adapter;
L
Linus Torvalds 已提交
764

765
	/* close queues to ensure that buffers are not accessed by adapter */
766
	zfcp_qdio_close(adapter->qdio);
767 768
	zfcp_fsf_req_dismiss_all(adapter);
	adapter->fsf_req_seq_no = 0;
769
	zfcp_fc_wka_ports_force_offline(adapter->gs);
770
	/* all ports and units are closed */
771
	zfcp_erp_modify_adapter_status(adapter, "erascl1", NULL,
772
				       ZFCP_STATUS_COMMON_OPEN, ZFCP_CLEAR);
773

774
	atomic_clear_mask(ZFCP_STATUS_ADAPTER_XCONFIG_OK |
775
			  ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED, &adapter->status);
L
Linus Torvalds 已提交
776 777
}

778
static int zfcp_erp_adapter_strategy_open(struct zfcp_erp_action *act)
L
Linus Torvalds 已提交
779
{
780
	struct zfcp_adapter *adapter = act->adapter;
L
Linus Torvalds 已提交
781

782 783 784 785 786 787
	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;
	}
788

789 790 791 792 793 794 795 796 797
	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;
}
798

799 800 801 802 803 804 805 806 807 808 809
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)) {
810
		ssleep(8);
811 812
		return ZFCP_ERP_FAILED;
	}
L
Linus Torvalds 已提交
813

814
	return ZFCP_ERP_SUCCEEDED;
L
Linus Torvalds 已提交
815 816
}

817
static int zfcp_erp_port_forced_strategy_close(struct zfcp_erp_action *act)
L
Linus Torvalds 已提交
818
{
819 820 821 822 823 824 825 826 827 828
	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 已提交
829 830
}

831
static void zfcp_erp_port_strategy_clearstati(struct zfcp_port *port)
L
Linus Torvalds 已提交
832
{
833
	atomic_clear_mask(ZFCP_STATUS_COMMON_ACCESS_DENIED, &port->status);
834
}
L
Linus Torvalds 已提交
835

836 837 838 839 840 841 842 843 844 845 846 847 848 849 850
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:
851
		if (!(status & ZFCP_STATUS_PORT_PHYS_OPEN))
852 853 854
			return ZFCP_ERP_SUCCEEDED;
	}
	return ZFCP_ERP_FAILED;
L
Linus Torvalds 已提交
855 856
}

857
static int zfcp_erp_port_strategy_close(struct zfcp_erp_action *erp_action)
L
Linus Torvalds 已提交
858
{
859
	int retval;
L
Linus Torvalds 已提交
860

861 862 863 864 865 866 867
	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 已提交
868 869
}

870
static int zfcp_erp_port_strategy_open_port(struct zfcp_erp_action *erp_action)
L
Linus Torvalds 已提交
871
{
872
	int retval;
L
Linus Torvalds 已提交
873

874 875 876 877 878 879 880 881
	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 已提交
882

883
static int zfcp_erp_open_ptp_port(struct zfcp_erp_action *act)
L
Linus Torvalds 已提交
884
{
885 886
	struct zfcp_adapter *adapter = act->adapter;
	struct zfcp_port *port = act->port;
L
Linus Torvalds 已提交
887

888
	if (port->wwpn != adapter->peer_wwpn) {
889
		zfcp_erp_port_failed(port, "eroptp1", NULL);
890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907
		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);
908
		if (!port->d_id) {
909
			zfcp_fc_trigger_did_lookup(port);
910
			return ZFCP_ERP_EXIT;
911 912 913 914 915
		}
		return zfcp_erp_port_strategy_open_port(act);

	case ZFCP_ERP_STEP_PORT_OPENING:
		/* D_ID might have changed during open */
916
		if (p_status & ZFCP_STATUS_COMMON_OPEN) {
917 918 919
			if (!port->d_id) {
				zfcp_fc_trigger_did_lookup(port);
				return ZFCP_ERP_EXIT;
920
			}
921
			return ZFCP_ERP_SUCCEEDED;
922
		}
923 924
		if (port->d_id && !(p_status & ZFCP_STATUS_COMMON_NOESC)) {
			port->d_id = 0;
925
			return ZFCP_ERP_FAILED;
926 927
		}
		/* fall through otherwise */
928 929 930 931 932 933 934
	}
	return ZFCP_ERP_FAILED;
}

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

937 938
	if ((p_status & ZFCP_STATUS_COMMON_NOESC) &&
	    !(p_status & ZFCP_STATUS_COMMON_OPEN))
939 940
		goto close_init_done;

941 942 943
	switch (erp_action->step) {
	case ZFCP_ERP_STEP_UNINITIALIZED:
		zfcp_erp_port_strategy_clearstati(port);
944
		if (p_status & ZFCP_STATUS_COMMON_OPEN)
945
			return zfcp_erp_port_strategy_close(erp_action);
L
Linus Torvalds 已提交
946 947
		break;

948
	case ZFCP_ERP_STEP_PORT_CLOSING:
949
		if (p_status & ZFCP_STATUS_COMMON_OPEN)
950
			return ZFCP_ERP_FAILED;
L
Linus Torvalds 已提交
951 952
		break;
	}
953 954

close_init_done:
955 956
	if (erp_action->status & ZFCP_STATUS_ERP_CLOSE_ONLY)
		return ZFCP_ERP_EXIT;
L
Linus Torvalds 已提交
957

958
	return zfcp_erp_port_strategy_open_common(erp_action);
959 960 961 962
}

static void zfcp_erp_unit_strategy_clearstati(struct zfcp_unit *unit)
{
963
	atomic_clear_mask(ZFCP_STATUS_COMMON_ACCESS_DENIED |
964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988
			  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 已提交
989 990
}

991
static int zfcp_erp_unit_strategy(struct zfcp_erp_action *erp_action)
L
Linus Torvalds 已提交
992
{
993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012
	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 已提交
1013 1014
}

1015
static int zfcp_erp_strategy_check_unit(struct zfcp_unit *unit, int result)
L
Linus Torvalds 已提交
1016 1017 1018 1019 1020 1021 1022 1023
{
	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);
1024 1025 1026 1027
		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",
1028 1029
				(unsigned long long)unit->fcp_lun,
				(unsigned long long)unit->port->wwpn);
1030
			zfcp_erp_unit_failed(unit, "erusck1", NULL);
1031
		}
L
Linus Torvalds 已提交
1032 1033 1034
		break;
	}

1035 1036
	if (atomic_read(&unit->status) & ZFCP_STATUS_COMMON_ERP_FAILED) {
		zfcp_erp_unit_block(unit, 0);
L
Linus Torvalds 已提交
1037 1038 1039 1040 1041
		result = ZFCP_ERP_EXIT;
	}
	return result;
}

1042
static int zfcp_erp_strategy_check_port(struct zfcp_port *port, int result)
L
Linus Torvalds 已提交
1043 1044 1045 1046 1047 1048
{
	switch (result) {
	case ZFCP_ERP_SUCCEEDED :
		atomic_set(&port->erp_counter, 0);
		zfcp_erp_port_unblock(port);
		break;
1049

L
Linus Torvalds 已提交
1050
	case ZFCP_ERP_FAILED :
1051
		if (atomic_read(&port->status) & ZFCP_STATUS_COMMON_NOESC) {
1052 1053 1054
			zfcp_erp_port_block(port, 0);
			result = ZFCP_ERP_EXIT;
		}
L
Linus Torvalds 已提交
1055
		atomic_inc(&port->erp_counter);
1056 1057 1058
		if (atomic_read(&port->erp_counter) > ZFCP_MAX_ERPS) {
			dev_err(&port->adapter->ccw_device->dev,
				"ERP failed for remote port 0x%016Lx\n",
1059
				(unsigned long long)port->wwpn);
1060
			zfcp_erp_port_failed(port, "erpsck1", NULL);
1061
		}
L
Linus Torvalds 已提交
1062 1063 1064
		break;
	}

1065 1066
	if (atomic_read(&port->status) & ZFCP_STATUS_COMMON_ERP_FAILED) {
		zfcp_erp_port_block(port, 0);
L
Linus Torvalds 已提交
1067 1068 1069 1070 1071
		result = ZFCP_ERP_EXIT;
	}
	return result;
}

1072 1073
static int zfcp_erp_strategy_check_adapter(struct zfcp_adapter *adapter,
					   int result)
L
Linus Torvalds 已提交
1074 1075 1076 1077 1078 1079
{
	switch (result) {
	case ZFCP_ERP_SUCCEEDED :
		atomic_set(&adapter->erp_counter, 0);
		zfcp_erp_adapter_unblock(adapter);
		break;
1080

L
Linus Torvalds 已提交
1081 1082
	case ZFCP_ERP_FAILED :
		atomic_inc(&adapter->erp_counter);
1083 1084 1085 1086
		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");
1087
			zfcp_erp_adapter_failed(adapter, "erasck1", NULL);
1088
		}
L
Linus Torvalds 已提交
1089 1090 1091
		break;
	}

1092 1093
	if (atomic_read(&adapter->status) & ZFCP_STATUS_COMMON_ERP_FAILED) {
		zfcp_erp_adapter_block(adapter, 0);
L
Linus Torvalds 已提交
1094 1095 1096 1097 1098
		result = ZFCP_ERP_EXIT;
	}
	return result;
}

1099 1100
static int zfcp_erp_strategy_check_target(struct zfcp_erp_action *erp_action,
					  int result)
1101
{
1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121
	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;
1122 1123
}

1124
static int zfcp_erp_strat_change_det(atomic_t *target_status, u32 erp_status)
1125
{
1126
	int status = atomic_read(target_status);
1127

1128 1129 1130
	if ((status & ZFCP_STATUS_COMMON_RUNNING) &&
	    (erp_status & ZFCP_STATUS_ERP_CLOSE_ONLY))
		return 1; /* take it online */
1131

1132 1133 1134 1135 1136
	if (!(status & ZFCP_STATUS_COMMON_RUNNING) &&
	    !(erp_status & ZFCP_STATUS_ERP_CLOSE_ONLY))
		return 1; /* take it offline */

	return 0;
1137 1138
}

1139
static int zfcp_erp_strategy_statechange(struct zfcp_erp_action *act, int ret)
L
Linus Torvalds 已提交
1140
{
1141 1142 1143 1144 1145
	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 已提交
1146

1147
	switch (action) {
L
Linus Torvalds 已提交
1148
	case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
1149 1150 1151
		if (zfcp_erp_strat_change_det(&adapter->status, erp_status)) {
			_zfcp_erp_adapter_reopen(adapter,
						 ZFCP_STATUS_COMMON_ERP_FAILED,
1152
						 "ersscg1", NULL);
1153 1154
			return ZFCP_ERP_EXIT;
		}
L
Linus Torvalds 已提交
1155 1156 1157 1158
		break;

	case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
	case ZFCP_ERP_ACTION_REOPEN_PORT:
1159 1160 1161
		if (zfcp_erp_strat_change_det(&port->status, erp_status)) {
			_zfcp_erp_port_reopen(port,
					      ZFCP_STATUS_COMMON_ERP_FAILED,
1162
					      "ersscg2", NULL);
1163 1164
			return ZFCP_ERP_EXIT;
		}
L
Linus Torvalds 已提交
1165 1166 1167
		break;

	case ZFCP_ERP_ACTION_REOPEN_UNIT:
1168 1169 1170
		if (zfcp_erp_strat_change_det(&unit->status, erp_status)) {
			_zfcp_erp_unit_reopen(unit,
					      ZFCP_STATUS_COMMON_ERP_FAILED,
1171
					      "ersscg3", NULL, 0);
1172 1173
			return ZFCP_ERP_EXIT;
		}
L
Linus Torvalds 已提交
1174 1175
		break;
	}
1176
	return ret;
L
Linus Torvalds 已提交
1177 1178
}

1179
static void zfcp_erp_action_dequeue(struct zfcp_erp_action *erp_action)
L
Linus Torvalds 已提交
1180
{
1181
	struct zfcp_adapter *adapter = erp_action->adapter;
L
Linus Torvalds 已提交
1182

1183 1184 1185 1186
	adapter->erp_total_count--;
	if (erp_action->status & ZFCP_STATUS_ERP_LOWMEM) {
		adapter->erp_low_mem_count--;
		erp_action->status &= ~ZFCP_STATUS_ERP_LOWMEM;
1187
	}
L
Linus Torvalds 已提交
1188

1189
	list_del(&erp_action->list);
S
Swen Schillig 已提交
1190
	zfcp_dbf_rec_action("eractd1", erp_action);
L
Linus Torvalds 已提交
1191

1192 1193 1194 1195 1196
	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 已提交
1197

1198 1199 1200 1201 1202
	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 已提交
1203

1204 1205 1206 1207 1208
	case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
		atomic_clear_mask(ZFCP_STATUS_COMMON_ERP_INUSE,
				  &erp_action->adapter->status);
		break;
	}
L
Linus Torvalds 已提交
1209 1210
}

1211
static void zfcp_erp_action_cleanup(struct zfcp_erp_action *act, int result)
L
Linus Torvalds 已提交
1212
{
1213 1214 1215
	struct zfcp_adapter *adapter = act->adapter;
	struct zfcp_port *port = act->port;
	struct zfcp_unit *unit = act->unit;
L
Linus Torvalds 已提交
1216

1217 1218
	switch (act->action) {
	case ZFCP_ERP_ACTION_REOPEN_UNIT:
1219 1220
		if (!(act->status & ZFCP_STATUS_ERP_NO_REF))
			put_device(&unit->dev);
1221
		break;
L
Linus Torvalds 已提交
1222

1223
	case ZFCP_ERP_ACTION_REOPEN_PORT:
1224 1225
		if (result == ZFCP_ERP_SUCCEEDED)
			zfcp_scsi_schedule_rport_register(port);
1226 1227
		/* fall through */
	case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
1228
		put_device(&port->dev);
1229 1230 1231
		break;

	case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
1232
		if (result == ZFCP_ERP_SUCCEEDED) {
1233
			register_service_level(&adapter->service_level);
1234
			queue_work(adapter->work_queue, &adapter->scan_work);
1235 1236
		} else
			unregister_service_level(&adapter->service_level);
1237
		kref_put(&adapter->ref, zfcp_adapter_release);
1238 1239
		break;
	}
L
Linus Torvalds 已提交
1240 1241
}

1242
static int zfcp_erp_strategy_do_action(struct zfcp_erp_action *erp_action)
L
Linus Torvalds 已提交
1243
{
1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254
	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 已提交
1255 1256
}

1257
static int zfcp_erp_strategy(struct zfcp_erp_action *erp_action)
L
Linus Torvalds 已提交
1258 1259
{
	int retval;
1260
	unsigned long flags;
1261
	struct zfcp_adapter *adapter = erp_action->adapter;
L
Linus Torvalds 已提交
1262

1263
	kref_get(&adapter->ref);
L
Linus Torvalds 已提交
1264

1265
	write_lock_irqsave(&adapter->erp_lock, flags);
1266
	zfcp_erp_strategy_check_fsfreq(erp_action);
L
Linus Torvalds 已提交
1267

1268 1269 1270 1271
	if (erp_action->status & ZFCP_STATUS_ERP_DISMISSED) {
		zfcp_erp_action_dequeue(erp_action);
		retval = ZFCP_ERP_DISMISSED;
		goto unlock;
1272
	}
L
Linus Torvalds 已提交
1273

1274 1275 1276 1277 1278
	if (erp_action->status & ZFCP_STATUS_ERP_TIMEDOUT) {
		retval = ZFCP_ERP_FAILED;
		goto check_target;
	}

1279
	zfcp_erp_action_to_running(erp_action);
L
Linus Torvalds 已提交
1280

1281
	/* no lock to allow for blocking operations */
1282
	write_unlock_irqrestore(&adapter->erp_lock, flags);
1283
	retval = zfcp_erp_strategy_do_action(erp_action);
1284
	write_lock_irqsave(&adapter->erp_lock, flags);
L
Linus Torvalds 已提交
1285

1286 1287
	if (erp_action->status & ZFCP_STATUS_ERP_DISMISSED)
		retval = ZFCP_ERP_CONTINUES;
1288

1289 1290 1291 1292 1293
	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 已提交
1294
		}
1295
		if (adapter->erp_total_count == adapter->erp_low_mem_count)
1296
			_zfcp_erp_adapter_reopen(adapter, 0, "erstgy1", NULL);
1297 1298 1299
		else {
			zfcp_erp_strategy_memwait(erp_action);
			retval = ZFCP_ERP_CONTINUES;
L
Linus Torvalds 已提交
1300
		}
1301
		goto unlock;
L
Linus Torvalds 已提交
1302

1303 1304 1305 1306
	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 已提交
1307
		}
1308
		goto unlock;
L
Linus Torvalds 已提交
1309 1310
	}

1311
check_target:
1312 1313 1314 1315 1316
	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;
1317 1318 1319 1320
	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 已提交
1321

1322
 unlock:
1323
	write_unlock_irqrestore(&adapter->erp_lock, flags);
L
Linus Torvalds 已提交
1324

1325 1326
	if (retval != ZFCP_ERP_CONTINUES)
		zfcp_erp_action_cleanup(erp_action, retval);
L
Linus Torvalds 已提交
1327

1328
	kref_put(&adapter->ref, zfcp_adapter_release);
L
Linus Torvalds 已提交
1329 1330 1331
	return retval;
}

1332
static int zfcp_erp_thread(void *data)
L
Linus Torvalds 已提交
1333
{
1334 1335 1336 1337
	struct zfcp_adapter *adapter = (struct zfcp_adapter *) data;
	struct list_head *next;
	struct zfcp_erp_action *act;
	unsigned long flags;
1338

1339
	for (;;) {
S
Swen Schillig 已提交
1340
		zfcp_dbf_rec_thread_lock("erthrd1", adapter->dbf);
1341 1342 1343
		wait_event_interruptible(adapter->erp_ready_wq,
			   !list_empty(&adapter->erp_ready_head) ||
			   kthread_should_stop());
S
Swen Schillig 已提交
1344
		zfcp_dbf_rec_thread_lock("erthrd2", adapter->dbf);
1345

1346 1347 1348
		if (kthread_should_stop())
			break;

1349 1350 1351
		write_lock_irqsave(&adapter->erp_lock, flags);
		next = adapter->erp_ready_head.next;
		write_unlock_irqrestore(&adapter->erp_lock, flags);
L
Linus Torvalds 已提交
1352

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

1356 1357 1358
			/* there is more to come after dismission, no notify */
			if (zfcp_erp_strategy(act) != ZFCP_ERP_DISMISSED)
				zfcp_erp_wakeup(adapter);
L
Linus Torvalds 已提交
1359 1360 1361
		}
	}

1362 1363
	return 0;
}
L
Linus Torvalds 已提交
1364

1365 1366 1367 1368 1369 1370 1371 1372
/**
 * 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)
{
1373
	struct task_struct *thread;
L
Linus Torvalds 已提交
1374

1375 1376 1377
	thread = kthread_run(zfcp_erp_thread, adapter, "zfcperp%s",
			     dev_name(&adapter->ccw_device->dev));
	if (IS_ERR(thread)) {
1378
		dev_err(&adapter->ccw_device->dev,
1379
			"Creating an ERP thread for the FCP device failed.\n");
1380
		return PTR_ERR(thread);
L
Linus Torvalds 已提交
1381
	}
1382 1383

	adapter->erp_thread = thread;
1384 1385
	return 0;
}
L
Linus Torvalds 已提交
1386

1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397
/**
 * 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)
{
1398 1399
	kthread_stop(adapter->erp_thread);
	adapter->erp_thread = NULL;
1400 1401
	WARN_ON(!list_empty(&adapter->erp_ready_head));
	WARN_ON(!list_empty(&adapter->erp_running_head));
L
Linus Torvalds 已提交
1402 1403
}

1404 1405 1406 1407 1408 1409
/**
 * zfcp_erp_adapter_failed - Set adapter status to failed.
 * @adapter: Failed adapter.
 * @id: Event id for debug trace.
 * @ref: Reference for debug trace.
 */
1410
void zfcp_erp_adapter_failed(struct zfcp_adapter *adapter, char *id, void *ref)
L
Linus Torvalds 已提交
1411
{
1412 1413 1414
	zfcp_erp_modify_adapter_status(adapter, id, ref,
				       ZFCP_STATUS_COMMON_ERP_FAILED, ZFCP_SET);
}
L
Linus Torvalds 已提交
1415

1416 1417 1418 1419 1420 1421
/**
 * zfcp_erp_port_failed - Set port status to failed.
 * @port: Failed port.
 * @id: Event id for debug trace.
 * @ref: Reference for debug trace.
 */
1422
void zfcp_erp_port_failed(struct zfcp_port *port, char *id, void *ref)
1423 1424 1425
{
	zfcp_erp_modify_port_status(port, id, ref,
				    ZFCP_STATUS_COMMON_ERP_FAILED, ZFCP_SET);
L
Linus Torvalds 已提交
1426 1427 1428
}

/**
1429 1430 1431 1432
 * 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 已提交
1433
 */
1434
void zfcp_erp_unit_failed(struct zfcp_unit *unit, char *id, void *ref)
L
Linus Torvalds 已提交
1435
{
1436 1437
	zfcp_erp_modify_unit_status(unit, id, ref,
				    ZFCP_STATUS_COMMON_ERP_FAILED, ZFCP_SET);
L
Linus Torvalds 已提交
1438 1439
}

1440 1441 1442 1443 1444 1445 1446 1447 1448 1449
/**
 * 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 已提交
1450

1451 1452 1453 1454 1455 1456 1457 1458 1459 1460
/**
 * 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.
 */
1461
void zfcp_erp_modify_adapter_status(struct zfcp_adapter *adapter, char *id,
1462
				    void *ref, u32 mask, int set_or_clear)
L
Linus Torvalds 已提交
1463 1464
{
	struct zfcp_port *port;
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, &adapter->status))
S
Swen Schillig 已提交
1470
			zfcp_dbf_rec_adapter(id, ref, adapter->dbf);
1471 1472 1473
		atomic_set_mask(mask, &adapter->status);
	} else {
		if (status_change_clear(mask, &adapter->status))
S
Swen Schillig 已提交
1474
			zfcp_dbf_rec_adapter(id, ref, adapter->dbf);
1475 1476 1477 1478 1479
		atomic_clear_mask(mask, &adapter->status);
		if (mask & ZFCP_STATUS_COMMON_ERP_FAILED)
			atomic_set(&adapter->erp_counter, 0);
	}

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

1489 1490 1491 1492 1493 1494 1495 1496 1497 1498
/**
 * 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.
 */
1499
void zfcp_erp_modify_port_status(struct zfcp_port *port, char *id, void *ref,
1500
				 u32 mask, int set_or_clear)
L
Linus Torvalds 已提交
1501 1502
{
	struct zfcp_unit *unit;
1503
	unsigned long flags;
1504
	u32 common_mask = mask & ZFCP_COMMON_FLAGS;
L
Linus Torvalds 已提交
1505

1506 1507
	if (set_or_clear == ZFCP_SET) {
		if (status_change_set(mask, &port->status))
S
Swen Schillig 已提交
1508
			zfcp_dbf_rec_port(id, ref, port);
1509 1510 1511
		atomic_set_mask(mask, &port->status);
	} else {
		if (status_change_clear(mask, &port->status))
S
Swen Schillig 已提交
1512
			zfcp_dbf_rec_port(id, ref, port);
1513 1514 1515 1516
		atomic_clear_mask(mask, &port->status);
		if (mask & ZFCP_STATUS_COMMON_ERP_FAILED)
			atomic_set(&port->erp_counter, 0);
	}
L
Linus Torvalds 已提交
1517

1518 1519 1520
	if (common_mask) {
		read_lock_irqsave(&port->unit_list_lock, flags);
		list_for_each_entry(unit, &port->unit_list, list)
1521 1522
			zfcp_erp_modify_unit_status(unit, id, ref, common_mask,
						    set_or_clear);
1523 1524
		read_unlock_irqrestore(&port->unit_list_lock, flags);
	}
L
Linus Torvalds 已提交
1525 1526
}

1527 1528 1529 1530 1531 1532 1533 1534
/**
 * 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
 */
1535
void zfcp_erp_modify_unit_status(struct zfcp_unit *unit, char *id, void *ref,
1536
				 u32 mask, int set_or_clear)
L
Linus Torvalds 已提交
1537
{
1538 1539
	if (set_or_clear == ZFCP_SET) {
		if (status_change_set(mask, &unit->status))
S
Swen Schillig 已提交
1540
			zfcp_dbf_rec_unit(id, ref, unit);
1541 1542 1543
		atomic_set_mask(mask, &unit->status);
	} else {
		if (status_change_clear(mask, &unit->status))
S
Swen Schillig 已提交
1544
			zfcp_dbf_rec_unit(id, ref, unit);
1545 1546 1547 1548 1549
		atomic_clear_mask(mask, &unit->status);
		if (mask & ZFCP_STATUS_COMMON_ERP_FAILED) {
			atomic_set(&unit->erp_counter, 0);
		}
	}
L
Linus Torvalds 已提交
1550 1551
}

1552 1553 1554 1555 1556 1557
/**
 * 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.
 */
1558
void zfcp_erp_port_boxed(struct zfcp_port *port, char *id, void *ref)
1559
{
1560 1561
	zfcp_erp_modify_port_status(port, id, ref,
				    ZFCP_STATUS_COMMON_ACCESS_BOXED, ZFCP_SET);
1562
	zfcp_erp_port_reopen(port, ZFCP_STATUS_COMMON_ERP_FAILED, id, ref);
1563 1564
}

1565 1566 1567 1568 1569 1570
/**
 * 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.
 */
1571
void zfcp_erp_unit_boxed(struct zfcp_unit *unit, char *id, void *ref)
1572
{
1573 1574
	zfcp_erp_modify_unit_status(unit, id, ref,
				    ZFCP_STATUS_COMMON_ACCESS_BOXED, ZFCP_SET);
1575
	zfcp_erp_unit_reopen(unit, ZFCP_STATUS_COMMON_ERP_FAILED, id, ref);
1576 1577
}

1578 1579 1580 1581 1582 1583 1584 1585 1586
/**
 * 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.
 */
1587
void zfcp_erp_port_access_denied(struct zfcp_port *port, char *id, void *ref)
L
Linus Torvalds 已提交
1588
{
1589 1590 1591
	zfcp_erp_modify_port_status(port, id, ref,
				    ZFCP_STATUS_COMMON_ERP_FAILED |
				    ZFCP_STATUS_COMMON_ACCESS_DENIED, ZFCP_SET);
L
Linus Torvalds 已提交
1592 1593
}

1594 1595 1596 1597 1598 1599 1600 1601
/**
 * 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.
 */
1602
void zfcp_erp_unit_access_denied(struct zfcp_unit *unit, char *id, void *ref)
L
Linus Torvalds 已提交
1603
{
1604 1605 1606
	zfcp_erp_modify_unit_status(unit, id, ref,
				    ZFCP_STATUS_COMMON_ERP_FAILED |
				    ZFCP_STATUS_COMMON_ACCESS_DENIED, ZFCP_SET);
L
Linus Torvalds 已提交
1607 1608
}

1609
static void zfcp_erp_unit_access_changed(struct zfcp_unit *unit, char *id,
1610
					 void *ref)
L
Linus Torvalds 已提交
1611
{
1612 1613 1614
	int status = atomic_read(&unit->status);
	if (!(status & (ZFCP_STATUS_COMMON_ACCESS_DENIED |
			ZFCP_STATUS_COMMON_ACCESS_BOXED)))
1615 1616
		return;

1617
	zfcp_erp_unit_reopen(unit, ZFCP_STATUS_COMMON_ERP_FAILED, id, ref);
L
Linus Torvalds 已提交
1618 1619
}

1620
static void zfcp_erp_port_access_changed(struct zfcp_port *port, char *id,
1621
					 void *ref)
L
Linus Torvalds 已提交
1622 1623
{
	struct zfcp_unit *unit;
1624
	unsigned long flags;
1625
	int status = atomic_read(&port->status);
L
Linus Torvalds 已提交
1626

1627 1628
	if (!(status & (ZFCP_STATUS_COMMON_ACCESS_DENIED |
			ZFCP_STATUS_COMMON_ACCESS_BOXED))) {
1629 1630
		read_lock_irqsave(&port->unit_list_lock, flags);
		list_for_each_entry(unit, &port->unit_list, list)
1631
				    zfcp_erp_unit_access_changed(unit, id, ref);
1632
		read_unlock_irqrestore(&port->unit_list_lock, flags);
L
Linus Torvalds 已提交
1633 1634 1635
		return;
	}

C
Christof Schmitt 已提交
1636
	zfcp_erp_port_reopen(port, ZFCP_STATUS_COMMON_ERP_FAILED, id, ref);
L
Linus Torvalds 已提交
1637 1638
}

1639 1640 1641 1642 1643 1644
/**
 * 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
 */
1645
void zfcp_erp_adapter_access_changed(struct zfcp_adapter *adapter, char *id,
1646
				     void *ref)
L
Linus Torvalds 已提交
1647
{
1648
	unsigned long flags;
1649
	struct zfcp_port *port;
1650 1651

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

1654 1655
	read_lock_irqsave(&adapter->port_list_lock, flags);
	list_for_each_entry(port, &adapter->port_list, list)
1656
		zfcp_erp_port_access_changed(port, id, ref);
1657
	read_unlock_irqrestore(&adapter->port_list_lock, flags);
L
Linus Torvalds 已提交
1658
}