zfcp_erp.c 43.4 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
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,
33 34
	ZFCP_ERP_STEP_LUN_CLOSING	= 0x1000,
	ZFCP_ERP_STEP_LUN_OPENING	= 0x2000,
35 36 37
};

enum zfcp_erp_act_type {
38
	ZFCP_ERP_ACTION_REOPEN_LUN         = 1,
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
	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 61
	zfcp_erp_clear_adapter_status(adapter,
				       ZFCP_STATUS_COMMON_UNBLOCKED | mask);
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);
79
	zfcp_dbf_rec_run("erardy1", act);
80
	wake_up(&adapter->erp_ready_wq);
81
	zfcp_dbf_rec_run("erardy2", act);
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
static void zfcp_erp_action_dismiss_lun(struct scsi_device *sdev)
92
{
93 94 95 96
	struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);

	if (atomic_read(&zfcp_sdev->status) & ZFCP_STATUS_COMMON_ERP_INUSE)
		zfcp_erp_action_dismiss(&zfcp_sdev->erp_action);
97
}
L
Linus Torvalds 已提交
98

99 100
static void zfcp_erp_action_dismiss_port(struct zfcp_port *port)
{
101
	struct scsi_device *sdev;
L
Linus Torvalds 已提交
102

103 104
	if (atomic_read(&port->status) & ZFCP_STATUS_COMMON_ERP_INUSE)
		zfcp_erp_action_dismiss(&port->erp_action);
105 106 107 108
	else
		shost_for_each_device(sdev, port->adapter->scsi_host)
			if (sdev_to_zfcp(sdev)->port == port)
				zfcp_erp_action_dismiss_lun(sdev);
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
static int zfcp_erp_required_act(int want, struct zfcp_adapter *adapter,
				 struct zfcp_port *port,
127
				 struct scsi_device *sdev)
L
Linus Torvalds 已提交
128
{
129
	int need = want;
130 131
	int l_status, p_status, a_status;
	struct zfcp_scsi_dev *zfcp_sdev;
L
Linus Torvalds 已提交
132

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

173
	return need;
L
Linus Torvalds 已提交
174 175
}

176
static struct zfcp_erp_action *zfcp_erp_setup_act(int need, u32 act_status,
177 178
						  struct zfcp_adapter *adapter,
						  struct zfcp_port *port,
179
						  struct scsi_device *sdev)
L
Linus Torvalds 已提交
180
{
181
	struct zfcp_erp_action *erp_action;
182
	struct zfcp_scsi_dev *zfcp_sdev;
L
Linus Torvalds 已提交
183

184
	switch (need) {
185 186
	case ZFCP_ERP_ACTION_REOPEN_LUN:
		zfcp_sdev = sdev_to_zfcp(sdev);
187
		if (!(act_status & ZFCP_STATUS_ERP_NO_REF))
188
			if (scsi_device_get(sdev))
189
				return NULL;
190 191 192
		atomic_set_mask(ZFCP_STATUS_COMMON_ERP_INUSE,
				&zfcp_sdev->status);
		erp_action = &zfcp_sdev->erp_action;
193 194 195
		memset(erp_action, 0, sizeof(struct zfcp_erp_action));
		erp_action->port = port;
		erp_action->sdev = sdev;
196 197
		if (!(atomic_read(&zfcp_sdev->status) &
		      ZFCP_STATUS_COMMON_RUNNING))
198
			act_status |= ZFCP_STATUS_ERP_CLOSE_ONLY;
199
		break;
L
Linus Torvalds 已提交
200

201 202
	case ZFCP_ERP_ACTION_REOPEN_PORT:
	case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
203
		if (!get_device(&port->dev))
204
			return NULL;
205 206 207
		zfcp_erp_action_dismiss_port(port);
		atomic_set_mask(ZFCP_STATUS_COMMON_ERP_INUSE, &port->status);
		erp_action = &port->erp_action;
208 209
		memset(erp_action, 0, sizeof(struct zfcp_erp_action));
		erp_action->port = port;
210
		if (!(atomic_read(&port->status) & ZFCP_STATUS_COMMON_RUNNING))
211
			act_status |= ZFCP_STATUS_ERP_CLOSE_ONLY;
212
		break;
L
Linus Torvalds 已提交
213

214
	case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
215
		kref_get(&adapter->ref);
216 217 218
		zfcp_erp_action_dismiss_adapter(adapter);
		atomic_set_mask(ZFCP_STATUS_COMMON_ERP_INUSE, &adapter->status);
		erp_action = &adapter->erp_action;
219
		memset(erp_action, 0, sizeof(struct zfcp_erp_action));
220 221
		if (!(atomic_read(&adapter->status) &
		      ZFCP_STATUS_COMMON_RUNNING))
222
			act_status |= ZFCP_STATUS_ERP_CLOSE_ONLY;
223 224 225 226 227
		break;

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

229 230
	erp_action->adapter = adapter;
	erp_action->action = need;
231
	erp_action->status = act_status;
L
Linus Torvalds 已提交
232

233
	return erp_action;
L
Linus Torvalds 已提交
234 235
}

236 237
static int zfcp_erp_action_enqueue(int want, struct zfcp_adapter *adapter,
				   struct zfcp_port *port,
238 239
				   struct scsi_device *sdev,
				   char *id, void *ref, u32 act_status)
L
Linus Torvalds 已提交
240
{
241
	int retval = 1, need;
242
	struct zfcp_erp_action *act;
L
Linus Torvalds 已提交
243

244
	if (!adapter->erp_thread)
245
		return -EIO;
L
Linus Torvalds 已提交
246

247
	need = zfcp_erp_required_act(want, adapter, port, sdev);
248
	if (!need)
L
Linus Torvalds 已提交
249 250
		goto out;

251
	act = zfcp_erp_setup_act(need, act_status, adapter, port, sdev);
252 253
	if (!act)
		goto out;
254
	atomic_set_mask(ZFCP_STATUS_ADAPTER_ERP_PENDING, &adapter->status);
255 256
	++adapter->erp_total_count;
	list_add_tail(&act->list, &adapter->erp_ready_head);
257
	wake_up(&adapter->erp_ready_wq);
258
	retval = 0;
L
Linus Torvalds 已提交
259
 out:
260
	zfcp_dbf_rec_trig(id, adapter, port, sdev, want, need);
L
Linus Torvalds 已提交
261 262 263
	return retval;
}

264
static int _zfcp_erp_adapter_reopen(struct zfcp_adapter *adapter,
265
				    int clear_mask, char *id, void *ref)
266 267
{
	zfcp_erp_adapter_block(adapter, clear_mask);
268
	zfcp_scsi_schedule_rports_block(adapter);
269 270 271

	/* ensure propagation of failed status to new devices */
	if (atomic_read(&adapter->status) & ZFCP_STATUS_COMMON_ERP_FAILED) {
272 273
		zfcp_erp_set_adapter_status(adapter,
					    ZFCP_STATUS_COMMON_ERP_FAILED);
274 275 276
		return -EIO;
	}
	return zfcp_erp_action_enqueue(ZFCP_ERP_ACTION_REOPEN_ADAPTER,
277
				       adapter, NULL, NULL, id, ref, 0);
278 279 280 281 282 283 284 285
}

/**
 * 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 已提交
286
 */
287
void zfcp_erp_adapter_reopen(struct zfcp_adapter *adapter, int clear,
288
			     char *id, void *ref)
L
Linus Torvalds 已提交
289 290 291
{
	unsigned long flags;

292 293 294 295 296
	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)
297 298
		zfcp_erp_set_adapter_status(adapter,
					    ZFCP_STATUS_COMMON_ERP_FAILED);
299 300
	else
		zfcp_erp_action_enqueue(ZFCP_ERP_ACTION_REOPEN_ADAPTER, adapter,
301
					NULL, NULL, id, ref, 0);
302
	write_unlock_irqrestore(&adapter->erp_lock, flags);
303
}
L
Linus Torvalds 已提交
304

305 306 307 308 309 310 311 312
/**
 * 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,
313
			       char *id, void *ref)
314 315 316
{
	int flags = ZFCP_STATUS_COMMON_RUNNING | ZFCP_STATUS_COMMON_ERP_FAILED;
	zfcp_erp_adapter_reopen(adapter, clear | flags, id, ref);
L
Linus Torvalds 已提交
317 318
}

319 320 321 322 323 324
/**
 * 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 已提交
325
 */
326 327
void zfcp_erp_port_shutdown(struct zfcp_port *port, int clear, char *id,
			    void *ref)
L
Linus Torvalds 已提交
328
{
329 330 331 332 333 334
	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)
{
335 336
	zfcp_erp_clear_port_status(port,
				    ZFCP_STATUS_COMMON_UNBLOCKED | clear);
337 338 339
}

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

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

348
	zfcp_erp_action_enqueue(ZFCP_ERP_ACTION_REOPEN_PORT_FORCED,
349
				port->adapter, port, NULL, id, ref, 0);
350 351 352 353 354 355 356 357
}

/**
 * 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.
 */
358
void zfcp_erp_port_forced_reopen(struct zfcp_port *port, int clear, char *id,
359 360 361 362 363
				 void *ref)
{
	unsigned long flags;
	struct zfcp_adapter *adapter = port->adapter;

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

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

375
	if (atomic_read(&port->status) & ZFCP_STATUS_COMMON_ERP_FAILED) {
L
Linus Torvalds 已提交
376
		/* ensure propagation of failed status to new devices */
377
		zfcp_erp_set_port_status(port, ZFCP_STATUS_COMMON_ERP_FAILED);
378
		return -EIO;
L
Linus Torvalds 已提交
379 380
	}

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

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

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

	return retval;
}

405
static void zfcp_erp_lun_block(struct scsi_device *sdev, int clear_mask)
406
{
407 408
	zfcp_erp_clear_lun_status(sdev,
				  ZFCP_STATUS_COMMON_UNBLOCKED | clear_mask);
409 410
}

411 412
static void _zfcp_erp_lun_reopen(struct scsi_device *sdev, int clear, char *id,
				 void *ref, u32 act_status)
L
Linus Torvalds 已提交
413
{
414 415
	struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
	struct zfcp_adapter *adapter = zfcp_sdev->port->adapter;
L
Linus Torvalds 已提交
416

417
	zfcp_erp_lun_block(sdev, clear);
L
Linus Torvalds 已提交
418

419
	if (atomic_read(&zfcp_sdev->status) & ZFCP_STATUS_COMMON_ERP_FAILED)
420
		return;
L
Linus Torvalds 已提交
421

422 423
	zfcp_erp_action_enqueue(ZFCP_ERP_ACTION_REOPEN_LUN, adapter,
				zfcp_sdev->port, sdev, id, ref, act_status);
L
Linus Torvalds 已提交
424 425 426
}

/**
427 428 429
 * zfcp_erp_lun_reopen - initiate reopen of a LUN
 * @sdev: SCSI device / LUN to be reopened
 * @clear_mask: specifies flags in LUN status to be cleared
L
Linus Torvalds 已提交
430 431
 * Return: 0 on success, < 0 on error
 */
432 433
void zfcp_erp_lun_reopen(struct scsi_device *sdev, int clear, char *id,
			 void *ref)
L
Linus Torvalds 已提交
434 435
{
	unsigned long flags;
436 437
	struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
	struct zfcp_port *port = zfcp_sdev->port;
438
	struct zfcp_adapter *adapter = port->adapter;
L
Linus Torvalds 已提交
439

440
	write_lock_irqsave(&adapter->erp_lock, flags);
441
	_zfcp_erp_lun_reopen(sdev, clear, id, ref, 0);
442 443 444 445
	write_unlock_irqrestore(&adapter->erp_lock, flags);
}

/**
446 447
 * zfcp_erp_lun_shutdown - Shutdown LUN
 * @sdev: SCSI device / LUN to shut down.
448 449 450 451
 * @clear: Status flags to clear.
 * @id: Id for debug trace event.
 * @ref: Reference for debug trace event.
 */
452 453
void zfcp_erp_lun_shutdown(struct scsi_device *sdev, int clear, char *id,
			   void *ref)
454 455
{
	int flags = ZFCP_STATUS_COMMON_RUNNING | ZFCP_STATUS_COMMON_ERP_FAILED;
456
	zfcp_erp_lun_reopen(sdev, clear | flags, id, ref);
457 458 459
}

/**
460 461
 * zfcp_erp_lun_shutdown_wait - Shutdown LUN and wait for erp completion
 * @sdev: SCSI device / LUN to shut down.
462 463
 * @id: Id for debug trace event.
 *
464
 * Do not acquire a reference for the LUN when creating the ERP
465
 * action. It is safe, because this function waits for the ERP to
466 467
 * complete first. This allows to shutdown the LUN, even when the SCSI
 * device is in the state SDEV_DEL when scsi_device_get will fail.
468
 */
469
void zfcp_erp_lun_shutdown_wait(struct scsi_device *sdev, char *id)
470 471
{
	unsigned long flags;
472 473
	struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
	struct zfcp_port *port = zfcp_sdev->port;
474 475 476 477
	struct zfcp_adapter *adapter = port->adapter;
	int clear = ZFCP_STATUS_COMMON_RUNNING | ZFCP_STATUS_COMMON_ERP_FAILED;

	write_lock_irqsave(&adapter->erp_lock, flags);
478
	_zfcp_erp_lun_reopen(sdev, clear, id, NULL, ZFCP_STATUS_ERP_NO_REF);
479
	write_unlock_irqrestore(&adapter->erp_lock, flags);
480 481

	zfcp_erp_wait(adapter);
L
Linus Torvalds 已提交
482 483
}

484
static int status_change_set(unsigned long mask, atomic_t *status)
L
Linus Torvalds 已提交
485
{
486
	return (atomic_read(status) ^ mask) & mask;
L
Linus Torvalds 已提交
487 488
}

489
static void zfcp_erp_adapter_unblock(struct zfcp_adapter *adapter)
490
{
491
	if (status_change_set(ZFCP_STATUS_COMMON_UNBLOCKED, &adapter->status))
492
		zfcp_dbf_rec_run("eraubl1", &adapter->erp_action);
493
	atomic_set_mask(ZFCP_STATUS_COMMON_UNBLOCKED, &adapter->status);
494 495
}

496
static void zfcp_erp_port_unblock(struct zfcp_port *port)
L
Linus Torvalds 已提交
497
{
498
	if (status_change_set(ZFCP_STATUS_COMMON_UNBLOCKED, &port->status))
499
		zfcp_dbf_rec_run("erpubl1", &port->erp_action);
500
	atomic_set_mask(ZFCP_STATUS_COMMON_UNBLOCKED, &port->status);
L
Linus Torvalds 已提交
501 502
}

503
static void zfcp_erp_lun_unblock(struct scsi_device *sdev)
L
Linus Torvalds 已提交
504
{
505 506 507
	struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);

	if (status_change_set(ZFCP_STATUS_COMMON_UNBLOCKED, &zfcp_sdev->status))
508
		zfcp_dbf_rec_run("erlubl1", &sdev_to_zfcp(sdev)->erp_action);
509
	atomic_set_mask(ZFCP_STATUS_COMMON_UNBLOCKED, &zfcp_sdev->status);
L
Linus Torvalds 已提交
510 511
}

512
static void zfcp_erp_action_to_running(struct zfcp_erp_action *erp_action)
L
Linus Torvalds 已提交
513
{
514
	list_move(&erp_action->list, &erp_action->adapter->erp_running_head);
515
	zfcp_dbf_rec_run("erator1", erp_action);
L
Linus Torvalds 已提交
516 517
}

518
static void zfcp_erp_strategy_check_fsfreq(struct zfcp_erp_action *act)
L
Linus Torvalds 已提交
519
{
520
	struct zfcp_adapter *adapter = act->adapter;
521
	struct zfcp_fsf_req *req;
L
Linus Torvalds 已提交
522

523
	if (!act->fsf_req_id)
524
		return;
L
Linus Torvalds 已提交
525

526 527
	spin_lock(&adapter->req_list->lock);
	req = _zfcp_reqlist_find(adapter->req_list, act->fsf_req_id);
528
	if (req && req->erp_action == act) {
529 530
		if (act->status & (ZFCP_STATUS_ERP_DISMISSED |
				   ZFCP_STATUS_ERP_TIMEDOUT)) {
531
			req->status |= ZFCP_STATUS_FSFREQ_DISMISSED;
532
			zfcp_dbf_rec_run("erscf_1", act);
533
			req->erp_action = NULL;
L
Linus Torvalds 已提交
534
		}
535
		if (act->status & ZFCP_STATUS_ERP_TIMEDOUT)
536
			zfcp_dbf_rec_run("erscf_2", act);
537 538
		if (req->status & ZFCP_STATUS_FSFREQ_DISMISSED)
			act->fsf_req_id = 0;
539
	} else
540
		act->fsf_req_id = 0;
541
	spin_unlock(&adapter->req_list->lock);
L
Linus Torvalds 已提交
542 543
}

544 545 546 547
/**
 * zfcp_erp_notify - Trigger ERP action.
 * @erp_action: ERP action to continue.
 * @set_mask: ERP action status flags to set.
L
Linus Torvalds 已提交
548
 */
549
void zfcp_erp_notify(struct zfcp_erp_action *erp_action, unsigned long set_mask)
L
Linus Torvalds 已提交
550 551
{
	struct zfcp_adapter *adapter = erp_action->adapter;
552
	unsigned long flags;
L
Linus Torvalds 已提交
553

554
	write_lock_irqsave(&adapter->erp_lock, flags);
L
Linus Torvalds 已提交
555 556 557 558 559 560 561
	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);
}

562 563 564
/**
 * zfcp_erp_timeout_handler - Trigger ERP action from timed out ERP request
 * @data: ERP action (from timer data)
L
Linus Torvalds 已提交
565
 */
566
void zfcp_erp_timeout_handler(unsigned long data)
L
Linus Torvalds 已提交
567
{
568 569
	struct zfcp_erp_action *act = (struct zfcp_erp_action *) data;
	zfcp_erp_notify(act, ZFCP_STATUS_ERP_TIMEDOUT);
L
Linus Torvalds 已提交
570 571
}

572
static void zfcp_erp_memwait_handler(unsigned long data)
L
Linus Torvalds 已提交
573
{
574
	zfcp_erp_notify((struct zfcp_erp_action *)data, 0);
L
Linus Torvalds 已提交
575 576
}

577
static void zfcp_erp_strategy_memwait(struct zfcp_erp_action *erp_action)
L
Linus Torvalds 已提交
578
{
579 580 581 582 583
	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 已提交
584 585
}

586
static void _zfcp_erp_port_reopen_all(struct zfcp_adapter *adapter,
587
				      int clear, char *id, void *ref)
L
Linus Torvalds 已提交
588
{
589
	struct zfcp_port *port;
L
Linus Torvalds 已提交
590

591 592
	read_lock(&adapter->port_list_lock);
	list_for_each_entry(port, &adapter->port_list, list)
593
		_zfcp_erp_port_reopen(port, clear, id, ref);
594
	read_unlock(&adapter->port_list_lock);
L
Linus Torvalds 已提交
595 596
}

597 598
static void _zfcp_erp_lun_reopen_all(struct zfcp_port *port, int clear,
				     char *id, void *ref)
L
Linus Torvalds 已提交
599
{
600
	struct scsi_device *sdev;
L
Linus Torvalds 已提交
601

602 603 604
	shost_for_each_device(sdev, port->adapter->scsi_host)
		if (sdev_to_zfcp(sdev)->port == port)
			_zfcp_erp_lun_reopen(sdev, clear, id, ref, 0);
L
Linus Torvalds 已提交
605 606
}

607
static void zfcp_erp_strategy_followup_failed(struct zfcp_erp_action *act)
L
Linus Torvalds 已提交
608
{
609 610
	switch (act->action) {
	case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
611
		_zfcp_erp_adapter_reopen(act->adapter, 0, "ersff_1", NULL);
612 613
		break;
	case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
614
		_zfcp_erp_port_forced_reopen(act->port, 0, "ersff_2", NULL);
615 616
		break;
	case ZFCP_ERP_ACTION_REOPEN_PORT:
617
		_zfcp_erp_port_reopen(act->port, 0, "ersff_3", NULL);
618
		break;
619 620
	case ZFCP_ERP_ACTION_REOPEN_LUN:
		_zfcp_erp_lun_reopen(act->sdev, 0, "ersff_4", NULL, 0);
621 622 623 624 625 626 627 628 629 630 631 632 633 634
		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:
635
		_zfcp_erp_lun_reopen_all(act->port, 0, "ersfs_3", NULL);
636
		break;
L
Linus Torvalds 已提交
637 638 639
	}
}

640
static void zfcp_erp_wakeup(struct zfcp_adapter *adapter)
L
Linus Torvalds 已提交
641 642 643
{
	unsigned long flags;

644
	read_lock_irqsave(&adapter->erp_lock, flags);
645 646 647 648 649
	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 已提交
650
	}
651
	read_unlock_irqrestore(&adapter->erp_lock, flags);
652
}
L
Linus Torvalds 已提交
653

654 655
static int zfcp_erp_adapter_strategy_open_qdio(struct zfcp_erp_action *act)
{
656 657 658
	struct zfcp_qdio *qdio = act->adapter->qdio;

	if (zfcp_qdio_open(qdio))
659
		return ZFCP_ERP_FAILED;
660
	init_waitqueue_head(&qdio->req_q_wq);
661 662 663
	atomic_set_mask(ZFCP_STATUS_ADAPTER_QDIOUP, &act->adapter->status);
	return ZFCP_ERP_SUCCEEDED;
}
L
Linus Torvalds 已提交
664

665 666 667 668 669 670 671
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;
672
	_zfcp_erp_port_reopen(port, 0, "ereptp1", NULL);
673
}
L
Linus Torvalds 已提交
674

675 676 677 678 679
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 已提交
680

681 682 683 684 685 686 687 688 689 690 691 692
	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 已提交
693 694
		}

695 696
		wait_event(adapter->erp_ready_wq,
			   !list_empty(&adapter->erp_ready_head));
697 698
		if (erp_action->status & ZFCP_STATUS_ERP_TIMEDOUT)
			break;
L
Linus Torvalds 已提交
699

700 701 702
		if (!(atomic_read(&adapter->status) &
		      ZFCP_STATUS_ADAPTER_HOST_CON_INIT))
			break;
L
Linus Torvalds 已提交
703

704 705
		ssleep(sleep);
		sleep *= 2;
L
Linus Torvalds 已提交
706 707
	}

708 709
	atomic_clear_mask(ZFCP_STATUS_ADAPTER_HOST_CON_INIT,
			  &adapter->status);
L
Linus Torvalds 已提交
710

711 712
	if (!(atomic_read(&adapter->status) & ZFCP_STATUS_ADAPTER_XCONFIG_OK))
		return ZFCP_ERP_FAILED;
S
Swen Schillig 已提交
713

714 715
	if (fc_host_port_type(adapter->scsi_host) == FC_PORTTYPE_PTP)
		zfcp_erp_enqueue_ptp_port(adapter);
L
Linus Torvalds 已提交
716

717
	return ZFCP_ERP_SUCCEEDED;
L
Linus Torvalds 已提交
718 719
}

720
static int zfcp_erp_adapter_strategy_open_fsf_xport(struct zfcp_erp_action *act)
L
Linus Torvalds 已提交
721
{
722 723
	int ret;
	struct zfcp_adapter *adapter = act->adapter;
L
Linus Torvalds 已提交
724

725 726 727 728 729 730 731 732 733 734
	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;

735
	zfcp_dbf_rec_run("erasox1", act);
736 737
	wait_event(adapter->erp_ready_wq,
		   !list_empty(&adapter->erp_ready_head));
738
	zfcp_dbf_rec_run("erasox2", act);
739 740 741 742
	if (act->status & ZFCP_STATUS_ERP_TIMEDOUT)
		return ZFCP_ERP_FAILED;

	return ZFCP_ERP_SUCCEEDED;
L
Linus Torvalds 已提交
743 744
}

745
static int zfcp_erp_adapter_strategy_open_fsf(struct zfcp_erp_action *act)
L
Linus Torvalds 已提交
746
{
747 748
	if (zfcp_erp_adapter_strat_fsf_xconf(act) == ZFCP_ERP_FAILED)
		return ZFCP_ERP_FAILED;
L
Linus Torvalds 已提交
749

750 751
	if (zfcp_erp_adapter_strategy_open_fsf_xport(act) == ZFCP_ERP_FAILED)
		return ZFCP_ERP_FAILED;
L
Linus Torvalds 已提交
752

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

761
	atomic_set(&act->adapter->stat_miss, act->adapter->stat_read_buf_num);
762 763
	if (zfcp_status_read_refill(act->adapter))
		return ZFCP_ERP_FAILED;
L
Linus Torvalds 已提交
764

765 766
	return ZFCP_ERP_SUCCEEDED;
}
L
Linus Torvalds 已提交
767

768
static void zfcp_erp_adapter_strategy_close(struct zfcp_erp_action *act)
769 770
{
	struct zfcp_adapter *adapter = act->adapter;
L
Linus Torvalds 已提交
771

772
	/* close queues to ensure that buffers are not accessed by adapter */
773
	zfcp_qdio_close(adapter->qdio);
774 775
	zfcp_fsf_req_dismiss_all(adapter);
	adapter->fsf_req_seq_no = 0;
776
	zfcp_fc_wka_ports_force_offline(adapter->gs);
777
	/* all ports and LUNs are closed */
778
	zfcp_erp_clear_adapter_status(adapter, ZFCP_STATUS_COMMON_OPEN);
779

780
	atomic_clear_mask(ZFCP_STATUS_ADAPTER_XCONFIG_OK |
781
			  ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED, &adapter->status);
L
Linus Torvalds 已提交
782 783
}

784
static int zfcp_erp_adapter_strategy_open(struct zfcp_erp_action *act)
L
Linus Torvalds 已提交
785
{
786
	struct zfcp_adapter *adapter = act->adapter;
L
Linus Torvalds 已提交
787

788 789 790 791 792 793
	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;
	}
794

795 796 797 798 799 800 801 802 803
	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;
}
804

805 806 807 808 809 810 811 812 813 814 815
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)) {
816
		ssleep(8);
817 818
		return ZFCP_ERP_FAILED;
	}
L
Linus Torvalds 已提交
819

820
	return ZFCP_ERP_SUCCEEDED;
L
Linus Torvalds 已提交
821 822
}

823
static int zfcp_erp_port_forced_strategy_close(struct zfcp_erp_action *act)
L
Linus Torvalds 已提交
824
{
825 826 827 828 829 830 831 832 833 834
	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 已提交
835 836
}

837
static void zfcp_erp_port_strategy_clearstati(struct zfcp_port *port)
L
Linus Torvalds 已提交
838
{
839
	atomic_clear_mask(ZFCP_STATUS_COMMON_ACCESS_DENIED, &port->status);
840
}
L
Linus Torvalds 已提交
841

842 843 844 845 846 847 848 849 850 851 852 853 854 855 856
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:
857
		if (!(status & ZFCP_STATUS_PORT_PHYS_OPEN))
858 859 860
			return ZFCP_ERP_SUCCEEDED;
	}
	return ZFCP_ERP_FAILED;
L
Linus Torvalds 已提交
861 862
}

863
static int zfcp_erp_port_strategy_close(struct zfcp_erp_action *erp_action)
L
Linus Torvalds 已提交
864
{
865
	int retval;
L
Linus Torvalds 已提交
866

867 868 869 870 871 872 873
	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 已提交
874 875
}

876
static int zfcp_erp_port_strategy_open_port(struct zfcp_erp_action *erp_action)
L
Linus Torvalds 已提交
877
{
878
	int retval;
L
Linus Torvalds 已提交
879

880 881 882 883 884 885 886 887
	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 已提交
888

889
static int zfcp_erp_open_ptp_port(struct zfcp_erp_action *act)
L
Linus Torvalds 已提交
890
{
891 892
	struct zfcp_adapter *adapter = act->adapter;
	struct zfcp_port *port = act->port;
L
Linus Torvalds 已提交
893

894
	if (port->wwpn != adapter->peer_wwpn) {
895
		zfcp_erp_set_port_status(port, ZFCP_STATUS_COMMON_ERP_FAILED);
896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913
		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);
914
		if (!port->d_id) {
915
			zfcp_fc_trigger_did_lookup(port);
916
			return ZFCP_ERP_EXIT;
917 918 919 920 921
		}
		return zfcp_erp_port_strategy_open_port(act);

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

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

943 944
	if ((p_status & ZFCP_STATUS_COMMON_NOESC) &&
	    !(p_status & ZFCP_STATUS_COMMON_OPEN))
945 946
		goto close_init_done;

947 948 949
	switch (erp_action->step) {
	case ZFCP_ERP_STEP_UNINITIALIZED:
		zfcp_erp_port_strategy_clearstati(port);
950
		if (p_status & ZFCP_STATUS_COMMON_OPEN)
951
			return zfcp_erp_port_strategy_close(erp_action);
L
Linus Torvalds 已提交
952 953
		break;

954
	case ZFCP_ERP_STEP_PORT_CLOSING:
955
		if (p_status & ZFCP_STATUS_COMMON_OPEN)
956
			return ZFCP_ERP_FAILED;
L
Linus Torvalds 已提交
957 958
		break;
	}
959 960

close_init_done:
961 962
	if (erp_action->status & ZFCP_STATUS_ERP_CLOSE_ONLY)
		return ZFCP_ERP_EXIT;
L
Linus Torvalds 已提交
963

964
	return zfcp_erp_port_strategy_open_common(erp_action);
965 966
}

967
static void zfcp_erp_lun_strategy_clearstati(struct scsi_device *sdev)
968
{
969 970
	struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);

971
	atomic_clear_mask(ZFCP_STATUS_COMMON_ACCESS_DENIED |
972 973
			  ZFCP_STATUS_LUN_SHARED | ZFCP_STATUS_LUN_READONLY,
			  &zfcp_sdev->status);
974 975
}

976
static int zfcp_erp_lun_strategy_close(struct zfcp_erp_action *erp_action)
977
{
978
	int retval = zfcp_fsf_close_lun(erp_action);
979 980
	if (retval == -ENOMEM)
		return ZFCP_ERP_NOMEM;
981
	erp_action->step = ZFCP_ERP_STEP_LUN_CLOSING;
982 983 984 985 986
	if (retval)
		return ZFCP_ERP_FAILED;
	return ZFCP_ERP_CONTINUES;
}

987
static int zfcp_erp_lun_strategy_open(struct zfcp_erp_action *erp_action)
988
{
989
	int retval = zfcp_fsf_open_lun(erp_action);
990 991
	if (retval == -ENOMEM)
		return ZFCP_ERP_NOMEM;
992
	erp_action->step = ZFCP_ERP_STEP_LUN_OPENING;
993 994 995
	if (retval)
		return  ZFCP_ERP_FAILED;
	return ZFCP_ERP_CONTINUES;
L
Linus Torvalds 已提交
996 997
}

998
static int zfcp_erp_lun_strategy(struct zfcp_erp_action *erp_action)
L
Linus Torvalds 已提交
999
{
1000 1001
	struct scsi_device *sdev = erp_action->sdev;
	struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
1002 1003 1004

	switch (erp_action->step) {
	case ZFCP_ERP_STEP_UNINITIALIZED:
1005 1006 1007
		zfcp_erp_lun_strategy_clearstati(sdev);
		if (atomic_read(&zfcp_sdev->status) & ZFCP_STATUS_COMMON_OPEN)
			return zfcp_erp_lun_strategy_close(erp_action);
1008
		/* already closed, fall through */
1009 1010
	case ZFCP_ERP_STEP_LUN_CLOSING:
		if (atomic_read(&zfcp_sdev->status) & ZFCP_STATUS_COMMON_OPEN)
1011 1012 1013
			return ZFCP_ERP_FAILED;
		if (erp_action->status & ZFCP_STATUS_ERP_CLOSE_ONLY)
			return ZFCP_ERP_EXIT;
1014
		return zfcp_erp_lun_strategy_open(erp_action);
1015

1016 1017
	case ZFCP_ERP_STEP_LUN_OPENING:
		if (atomic_read(&zfcp_sdev->status) & ZFCP_STATUS_COMMON_OPEN)
1018 1019 1020
			return ZFCP_ERP_SUCCEEDED;
	}
	return ZFCP_ERP_FAILED;
L
Linus Torvalds 已提交
1021 1022
}

1023
static int zfcp_erp_strategy_check_lun(struct scsi_device *sdev, int result)
L
Linus Torvalds 已提交
1024
{
1025 1026
	struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);

L
Linus Torvalds 已提交
1027 1028
	switch (result) {
	case ZFCP_ERP_SUCCEEDED :
1029 1030
		atomic_set(&zfcp_sdev->erp_counter, 0);
		zfcp_erp_lun_unblock(sdev);
L
Linus Torvalds 已提交
1031 1032
		break;
	case ZFCP_ERP_FAILED :
1033 1034 1035 1036
		atomic_inc(&zfcp_sdev->erp_counter);
		if (atomic_read(&zfcp_sdev->erp_counter) > ZFCP_MAX_ERPS) {
			dev_err(&zfcp_sdev->port->adapter->ccw_device->dev,
				"ERP failed for LUN 0x%016Lx on "
1037
				"port 0x%016Lx\n",
1038 1039
				(unsigned long long)zfcp_scsi_dev_lun(sdev),
				(unsigned long long)zfcp_sdev->port->wwpn);
1040 1041
			zfcp_erp_set_lun_status(sdev,
						ZFCP_STATUS_COMMON_ERP_FAILED);
1042
		}
L
Linus Torvalds 已提交
1043 1044 1045
		break;
	}

1046 1047
	if (atomic_read(&zfcp_sdev->status) & ZFCP_STATUS_COMMON_ERP_FAILED) {
		zfcp_erp_lun_block(sdev, 0);
L
Linus Torvalds 已提交
1048 1049 1050 1051 1052
		result = ZFCP_ERP_EXIT;
	}
	return result;
}

1053
static int zfcp_erp_strategy_check_port(struct zfcp_port *port, int result)
L
Linus Torvalds 已提交
1054 1055 1056 1057 1058 1059
{
	switch (result) {
	case ZFCP_ERP_SUCCEEDED :
		atomic_set(&port->erp_counter, 0);
		zfcp_erp_port_unblock(port);
		break;
1060

L
Linus Torvalds 已提交
1061
	case ZFCP_ERP_FAILED :
1062
		if (atomic_read(&port->status) & ZFCP_STATUS_COMMON_NOESC) {
1063 1064 1065
			zfcp_erp_port_block(port, 0);
			result = ZFCP_ERP_EXIT;
		}
L
Linus Torvalds 已提交
1066
		atomic_inc(&port->erp_counter);
1067 1068 1069
		if (atomic_read(&port->erp_counter) > ZFCP_MAX_ERPS) {
			dev_err(&port->adapter->ccw_device->dev,
				"ERP failed for remote port 0x%016Lx\n",
1070
				(unsigned long long)port->wwpn);
1071 1072
			zfcp_erp_set_port_status(port,
					 ZFCP_STATUS_COMMON_ERP_FAILED);
1073
		}
L
Linus Torvalds 已提交
1074 1075 1076
		break;
	}

1077 1078
	if (atomic_read(&port->status) & ZFCP_STATUS_COMMON_ERP_FAILED) {
		zfcp_erp_port_block(port, 0);
L
Linus Torvalds 已提交
1079 1080 1081 1082 1083
		result = ZFCP_ERP_EXIT;
	}
	return result;
}

1084 1085
static int zfcp_erp_strategy_check_adapter(struct zfcp_adapter *adapter,
					   int result)
L
Linus Torvalds 已提交
1086 1087 1088 1089 1090 1091
{
	switch (result) {
	case ZFCP_ERP_SUCCEEDED :
		atomic_set(&adapter->erp_counter, 0);
		zfcp_erp_adapter_unblock(adapter);
		break;
1092

L
Linus Torvalds 已提交
1093 1094
	case ZFCP_ERP_FAILED :
		atomic_inc(&adapter->erp_counter);
1095 1096 1097 1098
		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");
1099 1100
			zfcp_erp_set_adapter_status(adapter,
					    ZFCP_STATUS_COMMON_ERP_FAILED);
1101
		}
L
Linus Torvalds 已提交
1102 1103 1104
		break;
	}

1105 1106
	if (atomic_read(&adapter->status) & ZFCP_STATUS_COMMON_ERP_FAILED) {
		zfcp_erp_adapter_block(adapter, 0);
L
Linus Torvalds 已提交
1107 1108 1109 1110 1111
		result = ZFCP_ERP_EXIT;
	}
	return result;
}

1112 1113
static int zfcp_erp_strategy_check_target(struct zfcp_erp_action *erp_action,
					  int result)
1114
{
1115 1116
	struct zfcp_adapter *adapter = erp_action->adapter;
	struct zfcp_port *port = erp_action->port;
1117
	struct scsi_device *sdev = erp_action->sdev;
1118 1119 1120

	switch (erp_action->action) {

1121 1122
	case ZFCP_ERP_ACTION_REOPEN_LUN:
		result = zfcp_erp_strategy_check_lun(sdev, result);
1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134
		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;
1135 1136
}

1137
static int zfcp_erp_strat_change_det(atomic_t *target_status, u32 erp_status)
1138
{
1139
	int status = atomic_read(target_status);
1140

1141 1142 1143
	if ((status & ZFCP_STATUS_COMMON_RUNNING) &&
	    (erp_status & ZFCP_STATUS_ERP_CLOSE_ONLY))
		return 1; /* take it online */
1144

1145 1146 1147 1148 1149
	if (!(status & ZFCP_STATUS_COMMON_RUNNING) &&
	    !(erp_status & ZFCP_STATUS_ERP_CLOSE_ONLY))
		return 1; /* take it offline */

	return 0;
1150 1151
}

1152
static int zfcp_erp_strategy_statechange(struct zfcp_erp_action *act, int ret)
L
Linus Torvalds 已提交
1153
{
1154 1155 1156
	int action = act->action;
	struct zfcp_adapter *adapter = act->adapter;
	struct zfcp_port *port = act->port;
1157 1158
	struct scsi_device *sdev = act->sdev;
	struct zfcp_scsi_dev *zfcp_sdev;
1159
	u32 erp_status = act->status;
L
Linus Torvalds 已提交
1160

1161
	switch (action) {
L
Linus Torvalds 已提交
1162
	case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
1163 1164 1165
		if (zfcp_erp_strat_change_det(&adapter->status, erp_status)) {
			_zfcp_erp_adapter_reopen(adapter,
						 ZFCP_STATUS_COMMON_ERP_FAILED,
1166
						 "ersscg1", NULL);
1167 1168
			return ZFCP_ERP_EXIT;
		}
L
Linus Torvalds 已提交
1169 1170 1171 1172
		break;

	case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
	case ZFCP_ERP_ACTION_REOPEN_PORT:
1173 1174 1175
		if (zfcp_erp_strat_change_det(&port->status, erp_status)) {
			_zfcp_erp_port_reopen(port,
					      ZFCP_STATUS_COMMON_ERP_FAILED,
1176
					      "ersscg2", NULL);
1177 1178
			return ZFCP_ERP_EXIT;
		}
L
Linus Torvalds 已提交
1179 1180
		break;

1181 1182 1183 1184 1185 1186
	case ZFCP_ERP_ACTION_REOPEN_LUN:
		zfcp_sdev = sdev_to_zfcp(sdev);
		if (zfcp_erp_strat_change_det(&zfcp_sdev->status, erp_status)) {
			_zfcp_erp_lun_reopen(sdev,
					     ZFCP_STATUS_COMMON_ERP_FAILED,
					     "ersscg3", NULL, 0);
1187 1188
			return ZFCP_ERP_EXIT;
		}
L
Linus Torvalds 已提交
1189 1190
		break;
	}
1191
	return ret;
L
Linus Torvalds 已提交
1192 1193
}

1194
static void zfcp_erp_action_dequeue(struct zfcp_erp_action *erp_action)
L
Linus Torvalds 已提交
1195
{
1196
	struct zfcp_adapter *adapter = erp_action->adapter;
1197
	struct zfcp_scsi_dev *zfcp_sdev;
L
Linus Torvalds 已提交
1198

1199 1200 1201 1202
	adapter->erp_total_count--;
	if (erp_action->status & ZFCP_STATUS_ERP_LOWMEM) {
		adapter->erp_low_mem_count--;
		erp_action->status &= ~ZFCP_STATUS_ERP_LOWMEM;
1203
	}
L
Linus Torvalds 已提交
1204

1205
	list_del(&erp_action->list);
1206
	zfcp_dbf_rec_run("eractd1", erp_action);
L
Linus Torvalds 已提交
1207

1208
	switch (erp_action->action) {
1209 1210
	case ZFCP_ERP_ACTION_REOPEN_LUN:
		zfcp_sdev = sdev_to_zfcp(erp_action->sdev);
1211
		atomic_clear_mask(ZFCP_STATUS_COMMON_ERP_INUSE,
1212
				  &zfcp_sdev->status);
1213
		break;
L
Linus Torvalds 已提交
1214

1215 1216 1217 1218 1219
	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 已提交
1220

1221 1222 1223 1224 1225
	case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
		atomic_clear_mask(ZFCP_STATUS_COMMON_ERP_INUSE,
				  &erp_action->adapter->status);
		break;
	}
L
Linus Torvalds 已提交
1226 1227
}

1228
static void zfcp_erp_action_cleanup(struct zfcp_erp_action *act, int result)
L
Linus Torvalds 已提交
1229
{
1230 1231
	struct zfcp_adapter *adapter = act->adapter;
	struct zfcp_port *port = act->port;
1232
	struct scsi_device *sdev = act->sdev;
L
Linus Torvalds 已提交
1233

1234
	switch (act->action) {
1235
	case ZFCP_ERP_ACTION_REOPEN_LUN:
1236
		if (!(act->status & ZFCP_STATUS_ERP_NO_REF))
1237
			scsi_device_put(sdev);
1238
		break;
L
Linus Torvalds 已提交
1239

1240
	case ZFCP_ERP_ACTION_REOPEN_PORT:
1241 1242
		if (result == ZFCP_ERP_SUCCEEDED)
			zfcp_scsi_schedule_rport_register(port);
1243 1244
		/* fall through */
	case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
1245
		put_device(&port->dev);
1246 1247 1248
		break;

	case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
1249
		if (result == ZFCP_ERP_SUCCEEDED) {
1250
			register_service_level(&adapter->service_level);
1251
			queue_work(adapter->work_queue, &adapter->scan_work);
1252 1253
		} else
			unregister_service_level(&adapter->service_level);
1254
		kref_put(&adapter->ref, zfcp_adapter_release);
1255 1256
		break;
	}
L
Linus Torvalds 已提交
1257 1258
}

1259
static int zfcp_erp_strategy_do_action(struct zfcp_erp_action *erp_action)
L
Linus Torvalds 已提交
1260
{
1261 1262 1263 1264 1265 1266 1267
	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);
1268 1269
	case ZFCP_ERP_ACTION_REOPEN_LUN:
		return zfcp_erp_lun_strategy(erp_action);
1270 1271
	}
	return ZFCP_ERP_FAILED;
L
Linus Torvalds 已提交
1272 1273
}

1274
static int zfcp_erp_strategy(struct zfcp_erp_action *erp_action)
L
Linus Torvalds 已提交
1275 1276
{
	int retval;
1277
	unsigned long flags;
1278
	struct zfcp_adapter *adapter = erp_action->adapter;
L
Linus Torvalds 已提交
1279

1280
	kref_get(&adapter->ref);
L
Linus Torvalds 已提交
1281

1282
	write_lock_irqsave(&adapter->erp_lock, flags);
1283
	zfcp_erp_strategy_check_fsfreq(erp_action);
L
Linus Torvalds 已提交
1284

1285 1286 1287 1288
	if (erp_action->status & ZFCP_STATUS_ERP_DISMISSED) {
		zfcp_erp_action_dequeue(erp_action);
		retval = ZFCP_ERP_DISMISSED;
		goto unlock;
1289
	}
L
Linus Torvalds 已提交
1290

1291 1292 1293 1294 1295
	if (erp_action->status & ZFCP_STATUS_ERP_TIMEDOUT) {
		retval = ZFCP_ERP_FAILED;
		goto check_target;
	}

1296
	zfcp_erp_action_to_running(erp_action);
L
Linus Torvalds 已提交
1297

1298
	/* no lock to allow for blocking operations */
1299
	write_unlock_irqrestore(&adapter->erp_lock, flags);
1300
	retval = zfcp_erp_strategy_do_action(erp_action);
1301
	write_lock_irqsave(&adapter->erp_lock, flags);
L
Linus Torvalds 已提交
1302

1303 1304
	if (erp_action->status & ZFCP_STATUS_ERP_DISMISSED)
		retval = ZFCP_ERP_CONTINUES;
1305

1306 1307 1308 1309 1310
	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 已提交
1311
		}
1312
		if (adapter->erp_total_count == adapter->erp_low_mem_count)
1313
			_zfcp_erp_adapter_reopen(adapter, 0, "erstgy1", NULL);
1314 1315 1316
		else {
			zfcp_erp_strategy_memwait(erp_action);
			retval = ZFCP_ERP_CONTINUES;
L
Linus Torvalds 已提交
1317
		}
1318
		goto unlock;
L
Linus Torvalds 已提交
1319

1320 1321 1322 1323
	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 已提交
1324
		}
1325
		goto unlock;
L
Linus Torvalds 已提交
1326 1327
	}

1328
check_target:
1329 1330 1331 1332 1333
	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;
1334 1335 1336 1337
	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 已提交
1338

1339
 unlock:
1340
	write_unlock_irqrestore(&adapter->erp_lock, flags);
L
Linus Torvalds 已提交
1341

1342 1343
	if (retval != ZFCP_ERP_CONTINUES)
		zfcp_erp_action_cleanup(erp_action, retval);
L
Linus Torvalds 已提交
1344

1345
	kref_put(&adapter->ref, zfcp_adapter_release);
L
Linus Torvalds 已提交
1346 1347 1348
	return retval;
}

1349
static int zfcp_erp_thread(void *data)
L
Linus Torvalds 已提交
1350
{
1351 1352 1353 1354
	struct zfcp_adapter *adapter = (struct zfcp_adapter *) data;
	struct list_head *next;
	struct zfcp_erp_action *act;
	unsigned long flags;
1355

1356 1357 1358 1359
	for (;;) {
		wait_event_interruptible(adapter->erp_ready_wq,
			   !list_empty(&adapter->erp_ready_head) ||
			   kthread_should_stop());
1360

1361 1362 1363
		if (kthread_should_stop())
			break;

1364 1365 1366
		write_lock_irqsave(&adapter->erp_lock, flags);
		next = adapter->erp_ready_head.next;
		write_unlock_irqrestore(&adapter->erp_lock, flags);
L
Linus Torvalds 已提交
1367

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

1371 1372 1373
			/* there is more to come after dismission, no notify */
			if (zfcp_erp_strategy(act) != ZFCP_ERP_DISMISSED)
				zfcp_erp_wakeup(adapter);
L
Linus Torvalds 已提交
1374 1375 1376
		}
	}

1377 1378
	return 0;
}
L
Linus Torvalds 已提交
1379

1380 1381 1382 1383 1384 1385 1386 1387
/**
 * 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)
{
1388
	struct task_struct *thread;
L
Linus Torvalds 已提交
1389

1390 1391 1392
	thread = kthread_run(zfcp_erp_thread, adapter, "zfcperp%s",
			     dev_name(&adapter->ccw_device->dev));
	if (IS_ERR(thread)) {
1393
		dev_err(&adapter->ccw_device->dev,
1394
			"Creating an ERP thread for the FCP device failed.\n");
1395
		return PTR_ERR(thread);
L
Linus Torvalds 已提交
1396
	}
1397 1398

	adapter->erp_thread = thread;
1399 1400
	return 0;
}
L
Linus Torvalds 已提交
1401

1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412
/**
 * 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)
{
1413 1414
	kthread_stop(adapter->erp_thread);
	adapter->erp_thread = NULL;
1415 1416
	WARN_ON(!list_empty(&adapter->erp_ready_head));
	WARN_ON(!list_empty(&adapter->erp_running_head));
L
Linus Torvalds 已提交
1417 1418
}

1419
/**
1420 1421
 * zfcp_erp_wait - wait for completion of error recovery on an adapter
 * @adapter: adapter for which to wait for completion of its error recovery
1422
 */
1423
void zfcp_erp_wait(struct zfcp_adapter *adapter)
L
Linus Torvalds 已提交
1424
{
1425 1426 1427
	wait_event(adapter->erp_done_wqh,
		   !(atomic_read(&adapter->status) &
			ZFCP_STATUS_ADAPTER_ERP_PENDING));
1428
}
L
Linus Torvalds 已提交
1429

1430
/**
1431 1432 1433 1434 1435
 * zfcp_erp_set_adapter_status - set adapter status bits
 * @adapter: adapter to change the status
 * @mask: status bits to change
 *
 * Changes in common status bits are propagated to attached ports and LUNs.
1436
 */
1437
void zfcp_erp_set_adapter_status(struct zfcp_adapter *adapter, u32 mask)
1438
{
1439 1440 1441 1442
	struct zfcp_port *port;
	struct scsi_device *sdev;
	unsigned long flags;
	u32 common_mask = mask & ZFCP_COMMON_FLAGS;
L
Linus Torvalds 已提交
1443

1444
	atomic_set_mask(mask, &adapter->status);
L
Linus Torvalds 已提交
1445

1446 1447 1448 1449 1450 1451 1452 1453 1454 1455
	if (!common_mask)
		return;

	read_lock_irqsave(&adapter->port_list_lock, flags);
	list_for_each_entry(port, &adapter->port_list, list)
		atomic_set_mask(common_mask, &port->status);
	read_unlock_irqrestore(&adapter->port_list_lock, flags);

	shost_for_each_device(sdev, adapter->scsi_host)
		atomic_set_mask(common_mask, &sdev_to_zfcp(sdev)->status);
1456
}
L
Linus Torvalds 已提交
1457

1458
/**
1459
 * zfcp_erp_clear_adapter_status - clear adapter status bits
1460 1461 1462
 * @adapter: adapter to change the status
 * @mask: status bits to change
 *
1463
 * Changes in common status bits are propagated to attached ports and LUNs.
1464
 */
1465
void zfcp_erp_clear_adapter_status(struct zfcp_adapter *adapter, u32 mask)
L
Linus Torvalds 已提交
1466 1467
{
	struct zfcp_port *port;
1468
	struct scsi_device *sdev;
1469
	unsigned long flags;
1470
	u32 common_mask = mask & ZFCP_COMMON_FLAGS;
1471
	u32 clear_counter = mask & ZFCP_STATUS_COMMON_ERP_FAILED;
L
Linus Torvalds 已提交
1472

1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485
	atomic_clear_mask(mask, &adapter->status);

	if (!common_mask)
		return;

	if (clear_counter)
		atomic_set(&adapter->erp_counter, 0);

	read_lock_irqsave(&adapter->port_list_lock, flags);
	list_for_each_entry(port, &adapter->port_list, list) {
		atomic_clear_mask(common_mask, &port->status);
		if (clear_counter)
			atomic_set(&port->erp_counter, 0);
1486
	}
1487
	read_unlock_irqrestore(&adapter->port_list_lock, flags);
1488

1489 1490 1491 1492
	shost_for_each_device(sdev, adapter->scsi_host) {
		atomic_clear_mask(common_mask, &sdev_to_zfcp(sdev)->status);
		if (clear_counter)
			atomic_set(&sdev_to_zfcp(sdev)->erp_counter, 0);
1493
	}
L
Linus Torvalds 已提交
1494 1495
}

1496
/**
1497 1498
 * zfcp_erp_set_port_status - set port status bits
 * @port: port to change the status
1499 1500
 * @mask: status bits to change
 *
1501
 * Changes in common status bits are propagated to attached LUNs.
1502
 */
1503
void zfcp_erp_set_port_status(struct zfcp_port *port, u32 mask)
L
Linus Torvalds 已提交
1504
{
1505
	struct scsi_device *sdev;
1506
	u32 common_mask = mask & ZFCP_COMMON_FLAGS;
L
Linus Torvalds 已提交
1507

1508
	atomic_set_mask(mask, &port->status);
L
Linus Torvalds 已提交
1509

1510 1511 1512 1513 1514 1515 1516
	if (!common_mask)
		return;

	shost_for_each_device(sdev, port->adapter->scsi_host)
		if (sdev_to_zfcp(sdev)->port == port)
			atomic_set_mask(common_mask,
					&sdev_to_zfcp(sdev)->status);
L
Linus Torvalds 已提交
1517 1518
}

1519
/**
1520 1521
 * zfcp_erp_clear_port_status - clear port status bits
 * @port: adapter to change the status
1522
 * @mask: status bits to change
1523 1524
 *
 * Changes in common status bits are propagated to attached LUNs.
1525
 */
1526
void zfcp_erp_clear_port_status(struct zfcp_port *port, u32 mask)
L
Linus Torvalds 已提交
1527
{
1528 1529 1530 1531 1532 1533 1534 1535
	struct scsi_device *sdev;
	u32 common_mask = mask & ZFCP_COMMON_FLAGS;
	u32 clear_counter = mask & ZFCP_STATUS_COMMON_ERP_FAILED;

	atomic_clear_mask(mask, &port->status);

	if (!common_mask)
		return;
1536

1537 1538 1539 1540 1541 1542 1543 1544 1545
	if (clear_counter)
		atomic_set(&port->erp_counter, 0);

	shost_for_each_device(sdev, port->adapter->scsi_host)
		if (sdev_to_zfcp(sdev)->port == port) {
			atomic_clear_mask(common_mask,
					  &sdev_to_zfcp(sdev)->status);
			if (clear_counter)
				atomic_set(&sdev_to_zfcp(sdev)->erp_counter, 0);
1546
		}
L
Linus Torvalds 已提交
1547 1548
}

1549
/**
1550 1551 1552
 * zfcp_erp_set_lun_status - set lun status bits
 * @sdev: SCSI device / lun to set the status bits
 * @mask: status bits to change
1553
 */
1554
void zfcp_erp_set_lun_status(struct scsi_device *sdev, u32 mask)
1555
{
1556 1557 1558
	struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);

	atomic_set_mask(mask, &zfcp_sdev->status);
1559 1560
}

1561
/**
1562 1563 1564
 * zfcp_erp_clear_lun_status - clear lun status bits
 * @sdev: SCSi device / lun to clear the status bits
 * @mask: status bits to change
1565
 */
1566
void zfcp_erp_clear_lun_status(struct scsi_device *sdev, u32 mask)
1567
{
1568 1569 1570 1571 1572 1573
	struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);

	atomic_clear_mask(mask, &zfcp_sdev->status);

	if (mask & ZFCP_STATUS_COMMON_ERP_FAILED)
		atomic_set(&zfcp_sdev->erp_counter, 0);
1574
}
1575