xref: /openbmc/linux/drivers/s390/scsi/zfcp_erp.c (revision ccb01374)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * zfcp device driver
4  *
5  * Error Recovery Procedures (ERP).
6  *
7  * Copyright IBM Corp. 2002, 2017
8  */
9 
10 #define KMSG_COMPONENT "zfcp"
11 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
12 
13 #include <linux/kthread.h>
14 #include "zfcp_ext.h"
15 #include "zfcp_reqlist.h"
16 
17 #define ZFCP_MAX_ERPS                   3
18 
19 enum zfcp_erp_act_flags {
20 	ZFCP_STATUS_ERP_TIMEDOUT	= 0x10000000,
21 	ZFCP_STATUS_ERP_CLOSE_ONLY	= 0x01000000,
22 	ZFCP_STATUS_ERP_DISMISSED	= 0x00200000,
23 	ZFCP_STATUS_ERP_LOWMEM		= 0x00400000,
24 	ZFCP_STATUS_ERP_NO_REF		= 0x00800000,
25 };
26 
27 /*
28  * Eyecatcher pseudo flag to bitwise or-combine with enum zfcp_erp_act_type.
29  * Used to indicate that an ERP action could not be set up despite a detected
30  * need for some recovery.
31  */
32 #define ZFCP_ERP_ACTION_NONE		0xc0
33 /*
34  * Eyecatcher pseudo flag to bitwise or-combine with enum zfcp_erp_act_type.
35  * Used to indicate that ERP not needed because the object has
36  * ZFCP_STATUS_COMMON_ERP_FAILED.
37  */
38 #define ZFCP_ERP_ACTION_FAILED		0xe0
39 
40 enum zfcp_erp_act_result {
41 	ZFCP_ERP_SUCCEEDED = 0,
42 	ZFCP_ERP_FAILED    = 1,
43 	ZFCP_ERP_CONTINUES = 2,
44 	ZFCP_ERP_EXIT      = 3,
45 	ZFCP_ERP_DISMISSED = 4,
46 	ZFCP_ERP_NOMEM     = 5,
47 };
48 
49 static void zfcp_erp_adapter_block(struct zfcp_adapter *adapter, int mask)
50 {
51 	zfcp_erp_clear_adapter_status(adapter,
52 				       ZFCP_STATUS_COMMON_UNBLOCKED | mask);
53 }
54 
55 static bool zfcp_erp_action_is_running(struct zfcp_erp_action *act)
56 {
57 	struct zfcp_erp_action *curr_act;
58 
59 	list_for_each_entry(curr_act, &act->adapter->erp_running_head, list)
60 		if (act == curr_act)
61 			return true;
62 	return false;
63 }
64 
65 static void zfcp_erp_action_ready(struct zfcp_erp_action *act)
66 {
67 	struct zfcp_adapter *adapter = act->adapter;
68 
69 	list_move(&act->list, &act->adapter->erp_ready_head);
70 	zfcp_dbf_rec_run("erardy1", act);
71 	wake_up(&adapter->erp_ready_wq);
72 	zfcp_dbf_rec_run("erardy2", act);
73 }
74 
75 static void zfcp_erp_action_dismiss(struct zfcp_erp_action *act)
76 {
77 	act->status |= ZFCP_STATUS_ERP_DISMISSED;
78 	if (zfcp_erp_action_is_running(act))
79 		zfcp_erp_action_ready(act);
80 }
81 
82 static void zfcp_erp_action_dismiss_lun(struct scsi_device *sdev)
83 {
84 	struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
85 
86 	if (atomic_read(&zfcp_sdev->status) & ZFCP_STATUS_COMMON_ERP_INUSE)
87 		zfcp_erp_action_dismiss(&zfcp_sdev->erp_action);
88 }
89 
90 static void zfcp_erp_action_dismiss_port(struct zfcp_port *port)
91 {
92 	struct scsi_device *sdev;
93 
94 	if (atomic_read(&port->status) & ZFCP_STATUS_COMMON_ERP_INUSE)
95 		zfcp_erp_action_dismiss(&port->erp_action);
96 	else {
97 		spin_lock(port->adapter->scsi_host->host_lock);
98 		__shost_for_each_device(sdev, port->adapter->scsi_host)
99 			if (sdev_to_zfcp(sdev)->port == port)
100 				zfcp_erp_action_dismiss_lun(sdev);
101 		spin_unlock(port->adapter->scsi_host->host_lock);
102 	}
103 }
104 
105 static void zfcp_erp_action_dismiss_adapter(struct zfcp_adapter *adapter)
106 {
107 	struct zfcp_port *port;
108 
109 	if (atomic_read(&adapter->status) & ZFCP_STATUS_COMMON_ERP_INUSE)
110 		zfcp_erp_action_dismiss(&adapter->erp_action);
111 	else {
112 		read_lock(&adapter->port_list_lock);
113 		list_for_each_entry(port, &adapter->port_list, list)
114 		    zfcp_erp_action_dismiss_port(port);
115 		read_unlock(&adapter->port_list_lock);
116 	}
117 }
118 
119 static enum zfcp_erp_act_type zfcp_erp_handle_failed(
120 	enum zfcp_erp_act_type want, struct zfcp_adapter *adapter,
121 	struct zfcp_port *port,	struct scsi_device *sdev)
122 {
123 	enum zfcp_erp_act_type need = want;
124 	struct zfcp_scsi_dev *zsdev;
125 
126 	switch (want) {
127 	case ZFCP_ERP_ACTION_REOPEN_LUN:
128 		zsdev = sdev_to_zfcp(sdev);
129 		if (atomic_read(&zsdev->status) & ZFCP_STATUS_COMMON_ERP_FAILED)
130 			need = 0;
131 		break;
132 	case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
133 		if (atomic_read(&port->status) & ZFCP_STATUS_COMMON_ERP_FAILED)
134 			need = 0;
135 		break;
136 	case ZFCP_ERP_ACTION_REOPEN_PORT:
137 		if (atomic_read(&port->status) &
138 		    ZFCP_STATUS_COMMON_ERP_FAILED) {
139 			need = 0;
140 			/* ensure propagation of failed status to new devices */
141 			zfcp_erp_set_port_status(
142 				port, ZFCP_STATUS_COMMON_ERP_FAILED);
143 		}
144 		break;
145 	case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
146 		if (atomic_read(&adapter->status) &
147 		    ZFCP_STATUS_COMMON_ERP_FAILED) {
148 			need = 0;
149 			/* ensure propagation of failed status to new devices */
150 			zfcp_erp_set_adapter_status(
151 				adapter, ZFCP_STATUS_COMMON_ERP_FAILED);
152 		}
153 		break;
154 	}
155 
156 	return need;
157 }
158 
159 static enum zfcp_erp_act_type zfcp_erp_required_act(enum zfcp_erp_act_type want,
160 				 struct zfcp_adapter *adapter,
161 				 struct zfcp_port *port,
162 				 struct scsi_device *sdev)
163 {
164 	enum zfcp_erp_act_type need = want;
165 	int l_status, p_status, a_status;
166 	struct zfcp_scsi_dev *zfcp_sdev;
167 
168 	switch (want) {
169 	case ZFCP_ERP_ACTION_REOPEN_LUN:
170 		zfcp_sdev = sdev_to_zfcp(sdev);
171 		l_status = atomic_read(&zfcp_sdev->status);
172 		if (l_status & ZFCP_STATUS_COMMON_ERP_INUSE)
173 			return 0;
174 		p_status = atomic_read(&port->status);
175 		if (!(p_status & ZFCP_STATUS_COMMON_RUNNING) ||
176 		      p_status & ZFCP_STATUS_COMMON_ERP_FAILED)
177 			return 0;
178 		if (!(p_status & ZFCP_STATUS_COMMON_UNBLOCKED))
179 			need = ZFCP_ERP_ACTION_REOPEN_PORT;
180 		/* fall through */
181 	case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
182 		p_status = atomic_read(&port->status);
183 		if (!(p_status & ZFCP_STATUS_COMMON_OPEN))
184 			need = ZFCP_ERP_ACTION_REOPEN_PORT;
185 		/* fall through */
186 	case ZFCP_ERP_ACTION_REOPEN_PORT:
187 		p_status = atomic_read(&port->status);
188 		if (p_status & ZFCP_STATUS_COMMON_ERP_INUSE)
189 			return 0;
190 		a_status = atomic_read(&adapter->status);
191 		if (!(a_status & ZFCP_STATUS_COMMON_RUNNING) ||
192 		      a_status & ZFCP_STATUS_COMMON_ERP_FAILED)
193 			return 0;
194 		if (p_status & ZFCP_STATUS_COMMON_NOESC)
195 			return need;
196 		if (!(a_status & ZFCP_STATUS_COMMON_UNBLOCKED))
197 			need = ZFCP_ERP_ACTION_REOPEN_ADAPTER;
198 		/* fall through */
199 	case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
200 		a_status = atomic_read(&adapter->status);
201 		if (a_status & ZFCP_STATUS_COMMON_ERP_INUSE)
202 			return 0;
203 		if (!(a_status & ZFCP_STATUS_COMMON_RUNNING) &&
204 		    !(a_status & ZFCP_STATUS_COMMON_OPEN))
205 			return 0; /* shutdown requested for closed adapter */
206 	}
207 
208 	return need;
209 }
210 
211 static struct zfcp_erp_action *zfcp_erp_setup_act(enum zfcp_erp_act_type need,
212 						  u32 act_status,
213 						  struct zfcp_adapter *adapter,
214 						  struct zfcp_port *port,
215 						  struct scsi_device *sdev)
216 {
217 	struct zfcp_erp_action *erp_action;
218 	struct zfcp_scsi_dev *zfcp_sdev;
219 
220 	switch (need) {
221 	case ZFCP_ERP_ACTION_REOPEN_LUN:
222 		zfcp_sdev = sdev_to_zfcp(sdev);
223 		if (!(act_status & ZFCP_STATUS_ERP_NO_REF))
224 			if (scsi_device_get(sdev))
225 				return NULL;
226 		atomic_or(ZFCP_STATUS_COMMON_ERP_INUSE,
227 				&zfcp_sdev->status);
228 		erp_action = &zfcp_sdev->erp_action;
229 		WARN_ON_ONCE(erp_action->port != port);
230 		WARN_ON_ONCE(erp_action->sdev != sdev);
231 		if (!(atomic_read(&zfcp_sdev->status) &
232 		      ZFCP_STATUS_COMMON_RUNNING))
233 			act_status |= ZFCP_STATUS_ERP_CLOSE_ONLY;
234 		break;
235 
236 	case ZFCP_ERP_ACTION_REOPEN_PORT:
237 	case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
238 		if (!get_device(&port->dev))
239 			return NULL;
240 		zfcp_erp_action_dismiss_port(port);
241 		atomic_or(ZFCP_STATUS_COMMON_ERP_INUSE, &port->status);
242 		erp_action = &port->erp_action;
243 		WARN_ON_ONCE(erp_action->port != port);
244 		WARN_ON_ONCE(erp_action->sdev != NULL);
245 		if (!(atomic_read(&port->status) & ZFCP_STATUS_COMMON_RUNNING))
246 			act_status |= ZFCP_STATUS_ERP_CLOSE_ONLY;
247 		break;
248 
249 	case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
250 		kref_get(&adapter->ref);
251 		zfcp_erp_action_dismiss_adapter(adapter);
252 		atomic_or(ZFCP_STATUS_COMMON_ERP_INUSE, &adapter->status);
253 		erp_action = &adapter->erp_action;
254 		WARN_ON_ONCE(erp_action->port != NULL);
255 		WARN_ON_ONCE(erp_action->sdev != NULL);
256 		if (!(atomic_read(&adapter->status) &
257 		      ZFCP_STATUS_COMMON_RUNNING))
258 			act_status |= ZFCP_STATUS_ERP_CLOSE_ONLY;
259 		break;
260 	}
261 
262 	WARN_ON_ONCE(erp_action->adapter != adapter);
263 	memset(&erp_action->list, 0, sizeof(erp_action->list));
264 	memset(&erp_action->timer, 0, sizeof(erp_action->timer));
265 	erp_action->step = ZFCP_ERP_STEP_UNINITIALIZED;
266 	erp_action->fsf_req_id = 0;
267 	erp_action->type = need;
268 	erp_action->status = act_status;
269 
270 	return erp_action;
271 }
272 
273 static void zfcp_erp_action_enqueue(enum zfcp_erp_act_type want,
274 				    struct zfcp_adapter *adapter,
275 				    struct zfcp_port *port,
276 				    struct scsi_device *sdev,
277 				    char *dbftag, u32 act_status)
278 {
279 	enum zfcp_erp_act_type need;
280 	struct zfcp_erp_action *act;
281 
282 	need = zfcp_erp_handle_failed(want, adapter, port, sdev);
283 	if (!need) {
284 		need = ZFCP_ERP_ACTION_FAILED; /* marker for trace */
285 		goto out;
286 	}
287 
288 	if (!adapter->erp_thread) {
289 		need = ZFCP_ERP_ACTION_NONE; /* marker for trace */
290 		goto out;
291 	}
292 
293 	need = zfcp_erp_required_act(want, adapter, port, sdev);
294 	if (!need)
295 		goto out;
296 
297 	act = zfcp_erp_setup_act(need, act_status, adapter, port, sdev);
298 	if (!act) {
299 		need |= ZFCP_ERP_ACTION_NONE; /* marker for trace */
300 		goto out;
301 	}
302 	atomic_or(ZFCP_STATUS_ADAPTER_ERP_PENDING, &adapter->status);
303 	++adapter->erp_total_count;
304 	list_add_tail(&act->list, &adapter->erp_ready_head);
305 	wake_up(&adapter->erp_ready_wq);
306  out:
307 	zfcp_dbf_rec_trig(dbftag, adapter, port, sdev, want, need);
308 }
309 
310 void zfcp_erp_port_forced_no_port_dbf(char *dbftag,
311 				      struct zfcp_adapter *adapter,
312 				      u64 port_name, u32 port_id)
313 {
314 	unsigned long flags;
315 	static /* don't waste stack */ struct zfcp_port tmpport;
316 
317 	write_lock_irqsave(&adapter->erp_lock, flags);
318 	/* Stand-in zfcp port with fields just good enough for
319 	 * zfcp_dbf_rec_trig() and zfcp_dbf_set_common().
320 	 * Under lock because tmpport is static.
321 	 */
322 	atomic_set(&tmpport.status, -1); /* unknown */
323 	tmpport.wwpn = port_name;
324 	tmpport.d_id = port_id;
325 	zfcp_dbf_rec_trig(dbftag, adapter, &tmpport, NULL,
326 			  ZFCP_ERP_ACTION_REOPEN_PORT_FORCED,
327 			  ZFCP_ERP_ACTION_NONE);
328 	write_unlock_irqrestore(&adapter->erp_lock, flags);
329 }
330 
331 static void _zfcp_erp_adapter_reopen(struct zfcp_adapter *adapter,
332 				    int clear_mask, char *dbftag)
333 {
334 	zfcp_erp_adapter_block(adapter, clear_mask);
335 	zfcp_scsi_schedule_rports_block(adapter);
336 
337 	zfcp_erp_action_enqueue(ZFCP_ERP_ACTION_REOPEN_ADAPTER,
338 				adapter, NULL, NULL, dbftag, 0);
339 }
340 
341 /**
342  * zfcp_erp_adapter_reopen - Reopen adapter.
343  * @adapter: Adapter to reopen.
344  * @clear: Status flags to clear.
345  * @dbftag: Tag for debug trace event.
346  */
347 void zfcp_erp_adapter_reopen(struct zfcp_adapter *adapter, int clear,
348 			     char *dbftag)
349 {
350 	unsigned long flags;
351 
352 	zfcp_erp_adapter_block(adapter, clear);
353 	zfcp_scsi_schedule_rports_block(adapter);
354 
355 	write_lock_irqsave(&adapter->erp_lock, flags);
356 	zfcp_erp_action_enqueue(ZFCP_ERP_ACTION_REOPEN_ADAPTER, adapter,
357 				NULL, NULL, dbftag, 0);
358 	write_unlock_irqrestore(&adapter->erp_lock, flags);
359 }
360 
361 /**
362  * zfcp_erp_adapter_shutdown - Shutdown adapter.
363  * @adapter: Adapter to shut down.
364  * @clear: Status flags to clear.
365  * @dbftag: Tag for debug trace event.
366  */
367 void zfcp_erp_adapter_shutdown(struct zfcp_adapter *adapter, int clear,
368 			       char *dbftag)
369 {
370 	int flags = ZFCP_STATUS_COMMON_RUNNING | ZFCP_STATUS_COMMON_ERP_FAILED;
371 	zfcp_erp_adapter_reopen(adapter, clear | flags, dbftag);
372 }
373 
374 /**
375  * zfcp_erp_port_shutdown - Shutdown port
376  * @port: Port to shut down.
377  * @clear: Status flags to clear.
378  * @dbftag: Tag for debug trace event.
379  */
380 void zfcp_erp_port_shutdown(struct zfcp_port *port, int clear, char *dbftag)
381 {
382 	int flags = ZFCP_STATUS_COMMON_RUNNING | ZFCP_STATUS_COMMON_ERP_FAILED;
383 	zfcp_erp_port_reopen(port, clear | flags, dbftag);
384 }
385 
386 static void zfcp_erp_port_block(struct zfcp_port *port, int clear)
387 {
388 	zfcp_erp_clear_port_status(port,
389 				    ZFCP_STATUS_COMMON_UNBLOCKED | clear);
390 }
391 
392 static void _zfcp_erp_port_forced_reopen(struct zfcp_port *port, int clear,
393 					 char *dbftag)
394 {
395 	zfcp_erp_port_block(port, clear);
396 	zfcp_scsi_schedule_rport_block(port);
397 
398 	zfcp_erp_action_enqueue(ZFCP_ERP_ACTION_REOPEN_PORT_FORCED,
399 				port->adapter, port, NULL, dbftag, 0);
400 }
401 
402 /**
403  * zfcp_erp_port_forced_reopen - Forced close of port and open again
404  * @port: Port to force close and to reopen.
405  * @clear: Status flags to clear.
406  * @dbftag: Tag for debug trace event.
407  */
408 void zfcp_erp_port_forced_reopen(struct zfcp_port *port, int clear,
409 				 char *dbftag)
410 {
411 	unsigned long flags;
412 	struct zfcp_adapter *adapter = port->adapter;
413 
414 	write_lock_irqsave(&adapter->erp_lock, flags);
415 	_zfcp_erp_port_forced_reopen(port, clear, dbftag);
416 	write_unlock_irqrestore(&adapter->erp_lock, flags);
417 }
418 
419 static void _zfcp_erp_port_reopen(struct zfcp_port *port, int clear,
420 				  char *dbftag)
421 {
422 	zfcp_erp_port_block(port, clear);
423 	zfcp_scsi_schedule_rport_block(port);
424 
425 	zfcp_erp_action_enqueue(ZFCP_ERP_ACTION_REOPEN_PORT,
426 				port->adapter, port, NULL, dbftag, 0);
427 }
428 
429 /**
430  * zfcp_erp_port_reopen - trigger remote port recovery
431  * @port: port to recover
432  * @clear: flags in port status to be cleared
433  * @dbftag: Tag for debug trace event.
434  */
435 void zfcp_erp_port_reopen(struct zfcp_port *port, int clear, char *dbftag)
436 {
437 	unsigned long flags;
438 	struct zfcp_adapter *adapter = port->adapter;
439 
440 	write_lock_irqsave(&adapter->erp_lock, flags);
441 	_zfcp_erp_port_reopen(port, clear, dbftag);
442 	write_unlock_irqrestore(&adapter->erp_lock, flags);
443 }
444 
445 static void zfcp_erp_lun_block(struct scsi_device *sdev, int clear_mask)
446 {
447 	zfcp_erp_clear_lun_status(sdev,
448 				  ZFCP_STATUS_COMMON_UNBLOCKED | clear_mask);
449 }
450 
451 static void _zfcp_erp_lun_reopen(struct scsi_device *sdev, int clear,
452 				 char *dbftag, u32 act_status)
453 {
454 	struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
455 	struct zfcp_adapter *adapter = zfcp_sdev->port->adapter;
456 
457 	zfcp_erp_lun_block(sdev, clear);
458 
459 	zfcp_erp_action_enqueue(ZFCP_ERP_ACTION_REOPEN_LUN, adapter,
460 				zfcp_sdev->port, sdev, dbftag, act_status);
461 }
462 
463 /**
464  * zfcp_erp_lun_reopen - initiate reopen of a LUN
465  * @sdev: SCSI device / LUN to be reopened
466  * @clear: specifies flags in LUN status to be cleared
467  * @dbftag: Tag for debug trace event.
468  *
469  * Return: 0 on success, < 0 on error
470  */
471 void zfcp_erp_lun_reopen(struct scsi_device *sdev, int clear, char *dbftag)
472 {
473 	unsigned long flags;
474 	struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
475 	struct zfcp_port *port = zfcp_sdev->port;
476 	struct zfcp_adapter *adapter = port->adapter;
477 
478 	write_lock_irqsave(&adapter->erp_lock, flags);
479 	_zfcp_erp_lun_reopen(sdev, clear, dbftag, 0);
480 	write_unlock_irqrestore(&adapter->erp_lock, flags);
481 }
482 
483 /**
484  * zfcp_erp_lun_shutdown - Shutdown LUN
485  * @sdev: SCSI device / LUN to shut down.
486  * @clear: Status flags to clear.
487  * @dbftag: Tag for debug trace event.
488  */
489 void zfcp_erp_lun_shutdown(struct scsi_device *sdev, int clear, char *dbftag)
490 {
491 	int flags = ZFCP_STATUS_COMMON_RUNNING | ZFCP_STATUS_COMMON_ERP_FAILED;
492 	zfcp_erp_lun_reopen(sdev, clear | flags, dbftag);
493 }
494 
495 /**
496  * zfcp_erp_lun_shutdown_wait - Shutdown LUN and wait for erp completion
497  * @sdev: SCSI device / LUN to shut down.
498  * @dbftag: Tag for debug trace event.
499  *
500  * Do not acquire a reference for the LUN when creating the ERP
501  * action. It is safe, because this function waits for the ERP to
502  * complete first. This allows to shutdown the LUN, even when the SCSI
503  * device is in the state SDEV_DEL when scsi_device_get will fail.
504  */
505 void zfcp_erp_lun_shutdown_wait(struct scsi_device *sdev, char *dbftag)
506 {
507 	unsigned long flags;
508 	struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
509 	struct zfcp_port *port = zfcp_sdev->port;
510 	struct zfcp_adapter *adapter = port->adapter;
511 	int clear = ZFCP_STATUS_COMMON_RUNNING | ZFCP_STATUS_COMMON_ERP_FAILED;
512 
513 	write_lock_irqsave(&adapter->erp_lock, flags);
514 	_zfcp_erp_lun_reopen(sdev, clear, dbftag, ZFCP_STATUS_ERP_NO_REF);
515 	write_unlock_irqrestore(&adapter->erp_lock, flags);
516 
517 	zfcp_erp_wait(adapter);
518 }
519 
520 static int zfcp_erp_status_change_set(unsigned long mask, atomic_t *status)
521 {
522 	return (atomic_read(status) ^ mask) & mask;
523 }
524 
525 static void zfcp_erp_adapter_unblock(struct zfcp_adapter *adapter)
526 {
527 	if (zfcp_erp_status_change_set(ZFCP_STATUS_COMMON_UNBLOCKED,
528 				       &adapter->status))
529 		zfcp_dbf_rec_run("eraubl1", &adapter->erp_action);
530 	atomic_or(ZFCP_STATUS_COMMON_UNBLOCKED, &adapter->status);
531 }
532 
533 static void zfcp_erp_port_unblock(struct zfcp_port *port)
534 {
535 	if (zfcp_erp_status_change_set(ZFCP_STATUS_COMMON_UNBLOCKED,
536 				       &port->status))
537 		zfcp_dbf_rec_run("erpubl1", &port->erp_action);
538 	atomic_or(ZFCP_STATUS_COMMON_UNBLOCKED, &port->status);
539 }
540 
541 static void zfcp_erp_lun_unblock(struct scsi_device *sdev)
542 {
543 	struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
544 
545 	if (zfcp_erp_status_change_set(ZFCP_STATUS_COMMON_UNBLOCKED,
546 				       &zfcp_sdev->status))
547 		zfcp_dbf_rec_run("erlubl1", &sdev_to_zfcp(sdev)->erp_action);
548 	atomic_or(ZFCP_STATUS_COMMON_UNBLOCKED, &zfcp_sdev->status);
549 }
550 
551 static void zfcp_erp_action_to_running(struct zfcp_erp_action *erp_action)
552 {
553 	list_move(&erp_action->list, &erp_action->adapter->erp_running_head);
554 	zfcp_dbf_rec_run("erator1", erp_action);
555 }
556 
557 static void zfcp_erp_strategy_check_fsfreq(struct zfcp_erp_action *act)
558 {
559 	struct zfcp_adapter *adapter = act->adapter;
560 	struct zfcp_fsf_req *req;
561 
562 	if (!act->fsf_req_id)
563 		return;
564 
565 	spin_lock(&adapter->req_list->lock);
566 	req = _zfcp_reqlist_find(adapter->req_list, act->fsf_req_id);
567 	if (req && req->erp_action == act) {
568 		if (act->status & (ZFCP_STATUS_ERP_DISMISSED |
569 				   ZFCP_STATUS_ERP_TIMEDOUT)) {
570 			req->status |= ZFCP_STATUS_FSFREQ_DISMISSED;
571 			zfcp_dbf_rec_run("erscf_1", act);
572 			req->erp_action = NULL;
573 		}
574 		if (act->status & ZFCP_STATUS_ERP_TIMEDOUT)
575 			zfcp_dbf_rec_run("erscf_2", act);
576 		if (req->status & ZFCP_STATUS_FSFREQ_DISMISSED)
577 			act->fsf_req_id = 0;
578 	} else
579 		act->fsf_req_id = 0;
580 	spin_unlock(&adapter->req_list->lock);
581 }
582 
583 /**
584  * zfcp_erp_notify - Trigger ERP action.
585  * @erp_action: ERP action to continue.
586  * @set_mask: ERP action status flags to set.
587  */
588 void zfcp_erp_notify(struct zfcp_erp_action *erp_action, unsigned long set_mask)
589 {
590 	struct zfcp_adapter *adapter = erp_action->adapter;
591 	unsigned long flags;
592 
593 	write_lock_irqsave(&adapter->erp_lock, flags);
594 	if (zfcp_erp_action_is_running(erp_action)) {
595 		erp_action->status |= set_mask;
596 		zfcp_erp_action_ready(erp_action);
597 	}
598 	write_unlock_irqrestore(&adapter->erp_lock, flags);
599 }
600 
601 /**
602  * zfcp_erp_timeout_handler - Trigger ERP action from timed out ERP request
603  * @t: timer list entry embedded in zfcp FSF request
604  */
605 void zfcp_erp_timeout_handler(struct timer_list *t)
606 {
607 	struct zfcp_fsf_req *fsf_req = from_timer(fsf_req, t, timer);
608 	struct zfcp_erp_action *act = fsf_req->erp_action;
609 
610 	zfcp_erp_notify(act, ZFCP_STATUS_ERP_TIMEDOUT);
611 }
612 
613 static void zfcp_erp_memwait_handler(struct timer_list *t)
614 {
615 	struct zfcp_erp_action *act = from_timer(act, t, timer);
616 
617 	zfcp_erp_notify(act, 0);
618 }
619 
620 static void zfcp_erp_strategy_memwait(struct zfcp_erp_action *erp_action)
621 {
622 	timer_setup(&erp_action->timer, zfcp_erp_memwait_handler, 0);
623 	erp_action->timer.expires = jiffies + HZ;
624 	add_timer(&erp_action->timer);
625 }
626 
627 static void _zfcp_erp_port_reopen_all(struct zfcp_adapter *adapter,
628 				      int clear, char *dbftag)
629 {
630 	struct zfcp_port *port;
631 
632 	read_lock(&adapter->port_list_lock);
633 	list_for_each_entry(port, &adapter->port_list, list)
634 		_zfcp_erp_port_reopen(port, clear, dbftag);
635 	read_unlock(&adapter->port_list_lock);
636 }
637 
638 static void _zfcp_erp_lun_reopen_all(struct zfcp_port *port, int clear,
639 				     char *dbftag)
640 {
641 	struct scsi_device *sdev;
642 
643 	spin_lock(port->adapter->scsi_host->host_lock);
644 	__shost_for_each_device(sdev, port->adapter->scsi_host)
645 		if (sdev_to_zfcp(sdev)->port == port)
646 			_zfcp_erp_lun_reopen(sdev, clear, dbftag, 0);
647 	spin_unlock(port->adapter->scsi_host->host_lock);
648 }
649 
650 static void zfcp_erp_strategy_followup_failed(struct zfcp_erp_action *act)
651 {
652 	switch (act->type) {
653 	case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
654 		_zfcp_erp_adapter_reopen(act->adapter, 0, "ersff_1");
655 		break;
656 	case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
657 		_zfcp_erp_port_forced_reopen(act->port, 0, "ersff_2");
658 		break;
659 	case ZFCP_ERP_ACTION_REOPEN_PORT:
660 		_zfcp_erp_port_reopen(act->port, 0, "ersff_3");
661 		break;
662 	case ZFCP_ERP_ACTION_REOPEN_LUN:
663 		_zfcp_erp_lun_reopen(act->sdev, 0, "ersff_4", 0);
664 		break;
665 	}
666 }
667 
668 static void zfcp_erp_strategy_followup_success(struct zfcp_erp_action *act)
669 {
670 	switch (act->type) {
671 	case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
672 		_zfcp_erp_port_reopen_all(act->adapter, 0, "ersfs_1");
673 		break;
674 	case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
675 		_zfcp_erp_port_reopen(act->port, 0, "ersfs_2");
676 		break;
677 	case ZFCP_ERP_ACTION_REOPEN_PORT:
678 		_zfcp_erp_lun_reopen_all(act->port, 0, "ersfs_3");
679 		break;
680 	case ZFCP_ERP_ACTION_REOPEN_LUN:
681 		/* NOP */
682 		break;
683 	}
684 }
685 
686 static void zfcp_erp_wakeup(struct zfcp_adapter *adapter)
687 {
688 	unsigned long flags;
689 
690 	read_lock_irqsave(&adapter->erp_lock, flags);
691 	if (list_empty(&adapter->erp_ready_head) &&
692 	    list_empty(&adapter->erp_running_head)) {
693 			atomic_andnot(ZFCP_STATUS_ADAPTER_ERP_PENDING,
694 					  &adapter->status);
695 			wake_up(&adapter->erp_done_wqh);
696 	}
697 	read_unlock_irqrestore(&adapter->erp_lock, flags);
698 }
699 
700 static void zfcp_erp_enqueue_ptp_port(struct zfcp_adapter *adapter)
701 {
702 	struct zfcp_port *port;
703 	port = zfcp_port_enqueue(adapter, adapter->peer_wwpn, 0,
704 				 adapter->peer_d_id);
705 	if (IS_ERR(port)) /* error or port already attached */
706 		return;
707 	_zfcp_erp_port_reopen(port, 0, "ereptp1");
708 }
709 
710 static enum zfcp_erp_act_result zfcp_erp_adapter_strat_fsf_xconf(
711 	struct zfcp_erp_action *erp_action)
712 {
713 	int retries;
714 	int sleep = 1;
715 	struct zfcp_adapter *adapter = erp_action->adapter;
716 
717 	atomic_andnot(ZFCP_STATUS_ADAPTER_XCONFIG_OK, &adapter->status);
718 
719 	for (retries = 7; retries; retries--) {
720 		atomic_andnot(ZFCP_STATUS_ADAPTER_HOST_CON_INIT,
721 				  &adapter->status);
722 		write_lock_irq(&adapter->erp_lock);
723 		zfcp_erp_action_to_running(erp_action);
724 		write_unlock_irq(&adapter->erp_lock);
725 		if (zfcp_fsf_exchange_config_data(erp_action)) {
726 			atomic_andnot(ZFCP_STATUS_ADAPTER_HOST_CON_INIT,
727 					  &adapter->status);
728 			return ZFCP_ERP_FAILED;
729 		}
730 
731 		wait_event(adapter->erp_ready_wq,
732 			   !list_empty(&adapter->erp_ready_head));
733 		if (erp_action->status & ZFCP_STATUS_ERP_TIMEDOUT)
734 			break;
735 
736 		if (!(atomic_read(&adapter->status) &
737 		      ZFCP_STATUS_ADAPTER_HOST_CON_INIT))
738 			break;
739 
740 		ssleep(sleep);
741 		sleep *= 2;
742 	}
743 
744 	atomic_andnot(ZFCP_STATUS_ADAPTER_HOST_CON_INIT,
745 			  &adapter->status);
746 
747 	if (!(atomic_read(&adapter->status) & ZFCP_STATUS_ADAPTER_XCONFIG_OK))
748 		return ZFCP_ERP_FAILED;
749 
750 	if (fc_host_port_type(adapter->scsi_host) == FC_PORTTYPE_PTP)
751 		zfcp_erp_enqueue_ptp_port(adapter);
752 
753 	return ZFCP_ERP_SUCCEEDED;
754 }
755 
756 static enum zfcp_erp_act_result zfcp_erp_adapter_strategy_open_fsf_xport(
757 	struct zfcp_erp_action *act)
758 {
759 	int ret;
760 	struct zfcp_adapter *adapter = act->adapter;
761 
762 	write_lock_irq(&adapter->erp_lock);
763 	zfcp_erp_action_to_running(act);
764 	write_unlock_irq(&adapter->erp_lock);
765 
766 	ret = zfcp_fsf_exchange_port_data(act);
767 	if (ret == -EOPNOTSUPP)
768 		return ZFCP_ERP_SUCCEEDED;
769 	if (ret)
770 		return ZFCP_ERP_FAILED;
771 
772 	zfcp_dbf_rec_run("erasox1", act);
773 	wait_event(adapter->erp_ready_wq,
774 		   !list_empty(&adapter->erp_ready_head));
775 	zfcp_dbf_rec_run("erasox2", act);
776 	if (act->status & ZFCP_STATUS_ERP_TIMEDOUT)
777 		return ZFCP_ERP_FAILED;
778 
779 	return ZFCP_ERP_SUCCEEDED;
780 }
781 
782 static enum zfcp_erp_act_result zfcp_erp_adapter_strategy_open_fsf(
783 	struct zfcp_erp_action *act)
784 {
785 	if (zfcp_erp_adapter_strat_fsf_xconf(act) == ZFCP_ERP_FAILED)
786 		return ZFCP_ERP_FAILED;
787 
788 	if (zfcp_erp_adapter_strategy_open_fsf_xport(act) == ZFCP_ERP_FAILED)
789 		return ZFCP_ERP_FAILED;
790 
791 	if (mempool_resize(act->adapter->pool.sr_data,
792 			   act->adapter->stat_read_buf_num))
793 		return ZFCP_ERP_FAILED;
794 
795 	if (mempool_resize(act->adapter->pool.status_read_req,
796 			   act->adapter->stat_read_buf_num))
797 		return ZFCP_ERP_FAILED;
798 
799 	atomic_set(&act->adapter->stat_miss, act->adapter->stat_read_buf_num);
800 	if (zfcp_status_read_refill(act->adapter))
801 		return ZFCP_ERP_FAILED;
802 
803 	return ZFCP_ERP_SUCCEEDED;
804 }
805 
806 static void zfcp_erp_adapter_strategy_close(struct zfcp_erp_action *act)
807 {
808 	struct zfcp_adapter *adapter = act->adapter;
809 
810 	/* close queues to ensure that buffers are not accessed by adapter */
811 	zfcp_qdio_close(adapter->qdio);
812 	zfcp_fsf_req_dismiss_all(adapter);
813 	adapter->fsf_req_seq_no = 0;
814 	zfcp_fc_wka_ports_force_offline(adapter->gs);
815 	/* all ports and LUNs are closed */
816 	zfcp_erp_clear_adapter_status(adapter, ZFCP_STATUS_COMMON_OPEN);
817 
818 	atomic_andnot(ZFCP_STATUS_ADAPTER_XCONFIG_OK |
819 			  ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED, &adapter->status);
820 }
821 
822 static enum zfcp_erp_act_result zfcp_erp_adapter_strategy_open(
823 	struct zfcp_erp_action *act)
824 {
825 	struct zfcp_adapter *adapter = act->adapter;
826 
827 	if (zfcp_qdio_open(adapter->qdio)) {
828 		atomic_andnot(ZFCP_STATUS_ADAPTER_XCONFIG_OK |
829 				  ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED,
830 				  &adapter->status);
831 		return ZFCP_ERP_FAILED;
832 	}
833 
834 	if (zfcp_erp_adapter_strategy_open_fsf(act)) {
835 		zfcp_erp_adapter_strategy_close(act);
836 		return ZFCP_ERP_FAILED;
837 	}
838 
839 	atomic_or(ZFCP_STATUS_COMMON_OPEN, &adapter->status);
840 
841 	return ZFCP_ERP_SUCCEEDED;
842 }
843 
844 static enum zfcp_erp_act_result zfcp_erp_adapter_strategy(
845 	struct zfcp_erp_action *act)
846 {
847 	struct zfcp_adapter *adapter = act->adapter;
848 
849 	if (atomic_read(&adapter->status) & ZFCP_STATUS_COMMON_OPEN) {
850 		zfcp_erp_adapter_strategy_close(act);
851 		if (act->status & ZFCP_STATUS_ERP_CLOSE_ONLY)
852 			return ZFCP_ERP_EXIT;
853 	}
854 
855 	if (zfcp_erp_adapter_strategy_open(act)) {
856 		ssleep(8);
857 		return ZFCP_ERP_FAILED;
858 	}
859 
860 	return ZFCP_ERP_SUCCEEDED;
861 }
862 
863 static enum zfcp_erp_act_result zfcp_erp_port_forced_strategy_close(
864 	struct zfcp_erp_action *act)
865 {
866 	int retval;
867 
868 	retval = zfcp_fsf_close_physical_port(act);
869 	if (retval == -ENOMEM)
870 		return ZFCP_ERP_NOMEM;
871 	act->step = ZFCP_ERP_STEP_PHYS_PORT_CLOSING;
872 	if (retval)
873 		return ZFCP_ERP_FAILED;
874 
875 	return ZFCP_ERP_CONTINUES;
876 }
877 
878 static enum zfcp_erp_act_result zfcp_erp_port_forced_strategy(
879 	struct zfcp_erp_action *erp_action)
880 {
881 	struct zfcp_port *port = erp_action->port;
882 	int status = atomic_read(&port->status);
883 
884 	switch (erp_action->step) {
885 	case ZFCP_ERP_STEP_UNINITIALIZED:
886 		if ((status & ZFCP_STATUS_PORT_PHYS_OPEN) &&
887 		    (status & ZFCP_STATUS_COMMON_OPEN))
888 			return zfcp_erp_port_forced_strategy_close(erp_action);
889 		else
890 			return ZFCP_ERP_FAILED;
891 
892 	case ZFCP_ERP_STEP_PHYS_PORT_CLOSING:
893 		if (!(status & ZFCP_STATUS_PORT_PHYS_OPEN))
894 			return ZFCP_ERP_SUCCEEDED;
895 		break;
896 	case ZFCP_ERP_STEP_PORT_CLOSING:
897 	case ZFCP_ERP_STEP_PORT_OPENING:
898 	case ZFCP_ERP_STEP_LUN_CLOSING:
899 	case ZFCP_ERP_STEP_LUN_OPENING:
900 		/* NOP */
901 		break;
902 	}
903 	return ZFCP_ERP_FAILED;
904 }
905 
906 static enum zfcp_erp_act_result zfcp_erp_port_strategy_close(
907 	struct zfcp_erp_action *erp_action)
908 {
909 	int retval;
910 
911 	retval = zfcp_fsf_close_port(erp_action);
912 	if (retval == -ENOMEM)
913 		return ZFCP_ERP_NOMEM;
914 	erp_action->step = ZFCP_ERP_STEP_PORT_CLOSING;
915 	if (retval)
916 		return ZFCP_ERP_FAILED;
917 	return ZFCP_ERP_CONTINUES;
918 }
919 
920 static enum zfcp_erp_act_result zfcp_erp_port_strategy_open_port(
921 	struct zfcp_erp_action *erp_action)
922 {
923 	int retval;
924 
925 	retval = zfcp_fsf_open_port(erp_action);
926 	if (retval == -ENOMEM)
927 		return ZFCP_ERP_NOMEM;
928 	erp_action->step = ZFCP_ERP_STEP_PORT_OPENING;
929 	if (retval)
930 		return ZFCP_ERP_FAILED;
931 	return ZFCP_ERP_CONTINUES;
932 }
933 
934 static int zfcp_erp_open_ptp_port(struct zfcp_erp_action *act)
935 {
936 	struct zfcp_adapter *adapter = act->adapter;
937 	struct zfcp_port *port = act->port;
938 
939 	if (port->wwpn != adapter->peer_wwpn) {
940 		zfcp_erp_set_port_status(port, ZFCP_STATUS_COMMON_ERP_FAILED);
941 		return ZFCP_ERP_FAILED;
942 	}
943 	port->d_id = adapter->peer_d_id;
944 	return zfcp_erp_port_strategy_open_port(act);
945 }
946 
947 static enum zfcp_erp_act_result zfcp_erp_port_strategy_open_common(
948 	struct zfcp_erp_action *act)
949 {
950 	struct zfcp_adapter *adapter = act->adapter;
951 	struct zfcp_port *port = act->port;
952 	int p_status = atomic_read(&port->status);
953 
954 	switch (act->step) {
955 	case ZFCP_ERP_STEP_UNINITIALIZED:
956 	case ZFCP_ERP_STEP_PHYS_PORT_CLOSING:
957 	case ZFCP_ERP_STEP_PORT_CLOSING:
958 		if (fc_host_port_type(adapter->scsi_host) == FC_PORTTYPE_PTP)
959 			return zfcp_erp_open_ptp_port(act);
960 		if (!port->d_id) {
961 			zfcp_fc_trigger_did_lookup(port);
962 			return ZFCP_ERP_EXIT;
963 		}
964 		return zfcp_erp_port_strategy_open_port(act);
965 
966 	case ZFCP_ERP_STEP_PORT_OPENING:
967 		/* D_ID might have changed during open */
968 		if (p_status & ZFCP_STATUS_COMMON_OPEN) {
969 			if (!port->d_id) {
970 				zfcp_fc_trigger_did_lookup(port);
971 				return ZFCP_ERP_EXIT;
972 			}
973 			return ZFCP_ERP_SUCCEEDED;
974 		}
975 		if (port->d_id && !(p_status & ZFCP_STATUS_COMMON_NOESC)) {
976 			port->d_id = 0;
977 			return ZFCP_ERP_FAILED;
978 		}
979 		/* no early return otherwise, continue after switch case */
980 		break;
981 	case ZFCP_ERP_STEP_LUN_CLOSING:
982 	case ZFCP_ERP_STEP_LUN_OPENING:
983 		/* NOP */
984 		break;
985 	}
986 	return ZFCP_ERP_FAILED;
987 }
988 
989 static enum zfcp_erp_act_result zfcp_erp_port_strategy(
990 	struct zfcp_erp_action *erp_action)
991 {
992 	struct zfcp_port *port = erp_action->port;
993 	int p_status = atomic_read(&port->status);
994 
995 	if ((p_status & ZFCP_STATUS_COMMON_NOESC) &&
996 	    !(p_status & ZFCP_STATUS_COMMON_OPEN))
997 		goto close_init_done;
998 
999 	switch (erp_action->step) {
1000 	case ZFCP_ERP_STEP_UNINITIALIZED:
1001 		if (p_status & ZFCP_STATUS_COMMON_OPEN)
1002 			return zfcp_erp_port_strategy_close(erp_action);
1003 		break;
1004 
1005 	case ZFCP_ERP_STEP_PORT_CLOSING:
1006 		if (p_status & ZFCP_STATUS_COMMON_OPEN)
1007 			return ZFCP_ERP_FAILED;
1008 		break;
1009 	case ZFCP_ERP_STEP_PHYS_PORT_CLOSING:
1010 	case ZFCP_ERP_STEP_PORT_OPENING:
1011 	case ZFCP_ERP_STEP_LUN_CLOSING:
1012 	case ZFCP_ERP_STEP_LUN_OPENING:
1013 		/* NOP */
1014 		break;
1015 	}
1016 
1017 close_init_done:
1018 	if (erp_action->status & ZFCP_STATUS_ERP_CLOSE_ONLY)
1019 		return ZFCP_ERP_EXIT;
1020 
1021 	return zfcp_erp_port_strategy_open_common(erp_action);
1022 }
1023 
1024 static void zfcp_erp_lun_strategy_clearstati(struct scsi_device *sdev)
1025 {
1026 	struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
1027 
1028 	atomic_andnot(ZFCP_STATUS_COMMON_ACCESS_DENIED,
1029 			  &zfcp_sdev->status);
1030 }
1031 
1032 static enum zfcp_erp_act_result zfcp_erp_lun_strategy_close(
1033 	struct zfcp_erp_action *erp_action)
1034 {
1035 	int retval = zfcp_fsf_close_lun(erp_action);
1036 	if (retval == -ENOMEM)
1037 		return ZFCP_ERP_NOMEM;
1038 	erp_action->step = ZFCP_ERP_STEP_LUN_CLOSING;
1039 	if (retval)
1040 		return ZFCP_ERP_FAILED;
1041 	return ZFCP_ERP_CONTINUES;
1042 }
1043 
1044 static enum zfcp_erp_act_result zfcp_erp_lun_strategy_open(
1045 	struct zfcp_erp_action *erp_action)
1046 {
1047 	int retval = zfcp_fsf_open_lun(erp_action);
1048 	if (retval == -ENOMEM)
1049 		return ZFCP_ERP_NOMEM;
1050 	erp_action->step = ZFCP_ERP_STEP_LUN_OPENING;
1051 	if (retval)
1052 		return  ZFCP_ERP_FAILED;
1053 	return ZFCP_ERP_CONTINUES;
1054 }
1055 
1056 static enum zfcp_erp_act_result zfcp_erp_lun_strategy(
1057 	struct zfcp_erp_action *erp_action)
1058 {
1059 	struct scsi_device *sdev = erp_action->sdev;
1060 	struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
1061 
1062 	switch (erp_action->step) {
1063 	case ZFCP_ERP_STEP_UNINITIALIZED:
1064 		zfcp_erp_lun_strategy_clearstati(sdev);
1065 		if (atomic_read(&zfcp_sdev->status) & ZFCP_STATUS_COMMON_OPEN)
1066 			return zfcp_erp_lun_strategy_close(erp_action);
1067 		/* already closed */
1068 		/* fall through */
1069 	case ZFCP_ERP_STEP_LUN_CLOSING:
1070 		if (atomic_read(&zfcp_sdev->status) & ZFCP_STATUS_COMMON_OPEN)
1071 			return ZFCP_ERP_FAILED;
1072 		if (erp_action->status & ZFCP_STATUS_ERP_CLOSE_ONLY)
1073 			return ZFCP_ERP_EXIT;
1074 		return zfcp_erp_lun_strategy_open(erp_action);
1075 
1076 	case ZFCP_ERP_STEP_LUN_OPENING:
1077 		if (atomic_read(&zfcp_sdev->status) & ZFCP_STATUS_COMMON_OPEN)
1078 			return ZFCP_ERP_SUCCEEDED;
1079 		break;
1080 	case ZFCP_ERP_STEP_PHYS_PORT_CLOSING:
1081 	case ZFCP_ERP_STEP_PORT_CLOSING:
1082 	case ZFCP_ERP_STEP_PORT_OPENING:
1083 		/* NOP */
1084 		break;
1085 	}
1086 	return ZFCP_ERP_FAILED;
1087 }
1088 
1089 static enum zfcp_erp_act_result zfcp_erp_strategy_check_lun(
1090 	struct scsi_device *sdev, enum zfcp_erp_act_result result)
1091 {
1092 	struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
1093 
1094 	switch (result) {
1095 	case ZFCP_ERP_SUCCEEDED :
1096 		atomic_set(&zfcp_sdev->erp_counter, 0);
1097 		zfcp_erp_lun_unblock(sdev);
1098 		break;
1099 	case ZFCP_ERP_FAILED :
1100 		atomic_inc(&zfcp_sdev->erp_counter);
1101 		if (atomic_read(&zfcp_sdev->erp_counter) > ZFCP_MAX_ERPS) {
1102 			dev_err(&zfcp_sdev->port->adapter->ccw_device->dev,
1103 				"ERP failed for LUN 0x%016Lx on "
1104 				"port 0x%016Lx\n",
1105 				(unsigned long long)zfcp_scsi_dev_lun(sdev),
1106 				(unsigned long long)zfcp_sdev->port->wwpn);
1107 			zfcp_erp_set_lun_status(sdev,
1108 						ZFCP_STATUS_COMMON_ERP_FAILED);
1109 		}
1110 		break;
1111 	case ZFCP_ERP_CONTINUES:
1112 	case ZFCP_ERP_EXIT:
1113 	case ZFCP_ERP_DISMISSED:
1114 	case ZFCP_ERP_NOMEM:
1115 		/* NOP */
1116 		break;
1117 	}
1118 
1119 	if (atomic_read(&zfcp_sdev->status) & ZFCP_STATUS_COMMON_ERP_FAILED) {
1120 		zfcp_erp_lun_block(sdev, 0);
1121 		result = ZFCP_ERP_EXIT;
1122 	}
1123 	return result;
1124 }
1125 
1126 static enum zfcp_erp_act_result zfcp_erp_strategy_check_port(
1127 	struct zfcp_port *port, enum zfcp_erp_act_result result)
1128 {
1129 	switch (result) {
1130 	case ZFCP_ERP_SUCCEEDED :
1131 		atomic_set(&port->erp_counter, 0);
1132 		zfcp_erp_port_unblock(port);
1133 		break;
1134 
1135 	case ZFCP_ERP_FAILED :
1136 		if (atomic_read(&port->status) & ZFCP_STATUS_COMMON_NOESC) {
1137 			zfcp_erp_port_block(port, 0);
1138 			result = ZFCP_ERP_EXIT;
1139 		}
1140 		atomic_inc(&port->erp_counter);
1141 		if (atomic_read(&port->erp_counter) > ZFCP_MAX_ERPS) {
1142 			dev_err(&port->adapter->ccw_device->dev,
1143 				"ERP failed for remote port 0x%016Lx\n",
1144 				(unsigned long long)port->wwpn);
1145 			zfcp_erp_set_port_status(port,
1146 					 ZFCP_STATUS_COMMON_ERP_FAILED);
1147 		}
1148 		break;
1149 	case ZFCP_ERP_CONTINUES:
1150 	case ZFCP_ERP_EXIT:
1151 	case ZFCP_ERP_DISMISSED:
1152 	case ZFCP_ERP_NOMEM:
1153 		/* NOP */
1154 		break;
1155 	}
1156 
1157 	if (atomic_read(&port->status) & ZFCP_STATUS_COMMON_ERP_FAILED) {
1158 		zfcp_erp_port_block(port, 0);
1159 		result = ZFCP_ERP_EXIT;
1160 	}
1161 	return result;
1162 }
1163 
1164 static enum zfcp_erp_act_result zfcp_erp_strategy_check_adapter(
1165 	struct zfcp_adapter *adapter, enum zfcp_erp_act_result result)
1166 {
1167 	switch (result) {
1168 	case ZFCP_ERP_SUCCEEDED :
1169 		atomic_set(&adapter->erp_counter, 0);
1170 		zfcp_erp_adapter_unblock(adapter);
1171 		break;
1172 
1173 	case ZFCP_ERP_FAILED :
1174 		atomic_inc(&adapter->erp_counter);
1175 		if (atomic_read(&adapter->erp_counter) > ZFCP_MAX_ERPS) {
1176 			dev_err(&adapter->ccw_device->dev,
1177 				"ERP cannot recover an error "
1178 				"on the FCP device\n");
1179 			zfcp_erp_set_adapter_status(adapter,
1180 					    ZFCP_STATUS_COMMON_ERP_FAILED);
1181 		}
1182 		break;
1183 	case ZFCP_ERP_CONTINUES:
1184 	case ZFCP_ERP_EXIT:
1185 	case ZFCP_ERP_DISMISSED:
1186 	case ZFCP_ERP_NOMEM:
1187 		/* NOP */
1188 		break;
1189 	}
1190 
1191 	if (atomic_read(&adapter->status) & ZFCP_STATUS_COMMON_ERP_FAILED) {
1192 		zfcp_erp_adapter_block(adapter, 0);
1193 		result = ZFCP_ERP_EXIT;
1194 	}
1195 	return result;
1196 }
1197 
1198 static enum zfcp_erp_act_result zfcp_erp_strategy_check_target(
1199 	struct zfcp_erp_action *erp_action, enum zfcp_erp_act_result result)
1200 {
1201 	struct zfcp_adapter *adapter = erp_action->adapter;
1202 	struct zfcp_port *port = erp_action->port;
1203 	struct scsi_device *sdev = erp_action->sdev;
1204 
1205 	switch (erp_action->type) {
1206 
1207 	case ZFCP_ERP_ACTION_REOPEN_LUN:
1208 		result = zfcp_erp_strategy_check_lun(sdev, result);
1209 		break;
1210 
1211 	case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
1212 	case ZFCP_ERP_ACTION_REOPEN_PORT:
1213 		result = zfcp_erp_strategy_check_port(port, result);
1214 		break;
1215 
1216 	case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
1217 		result = zfcp_erp_strategy_check_adapter(adapter, result);
1218 		break;
1219 	}
1220 	return result;
1221 }
1222 
1223 static int zfcp_erp_strat_change_det(atomic_t *target_status, u32 erp_status)
1224 {
1225 	int status = atomic_read(target_status);
1226 
1227 	if ((status & ZFCP_STATUS_COMMON_RUNNING) &&
1228 	    (erp_status & ZFCP_STATUS_ERP_CLOSE_ONLY))
1229 		return 1; /* take it online */
1230 
1231 	if (!(status & ZFCP_STATUS_COMMON_RUNNING) &&
1232 	    !(erp_status & ZFCP_STATUS_ERP_CLOSE_ONLY))
1233 		return 1; /* take it offline */
1234 
1235 	return 0;
1236 }
1237 
1238 static enum zfcp_erp_act_result zfcp_erp_strategy_statechange(
1239 	struct zfcp_erp_action *act, enum zfcp_erp_act_result result)
1240 {
1241 	enum zfcp_erp_act_type type = act->type;
1242 	struct zfcp_adapter *adapter = act->adapter;
1243 	struct zfcp_port *port = act->port;
1244 	struct scsi_device *sdev = act->sdev;
1245 	struct zfcp_scsi_dev *zfcp_sdev;
1246 	u32 erp_status = act->status;
1247 
1248 	switch (type) {
1249 	case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
1250 		if (zfcp_erp_strat_change_det(&adapter->status, erp_status)) {
1251 			_zfcp_erp_adapter_reopen(adapter,
1252 						 ZFCP_STATUS_COMMON_ERP_FAILED,
1253 						 "ersscg1");
1254 			return ZFCP_ERP_EXIT;
1255 		}
1256 		break;
1257 
1258 	case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
1259 	case ZFCP_ERP_ACTION_REOPEN_PORT:
1260 		if (zfcp_erp_strat_change_det(&port->status, erp_status)) {
1261 			_zfcp_erp_port_reopen(port,
1262 					      ZFCP_STATUS_COMMON_ERP_FAILED,
1263 					      "ersscg2");
1264 			return ZFCP_ERP_EXIT;
1265 		}
1266 		break;
1267 
1268 	case ZFCP_ERP_ACTION_REOPEN_LUN:
1269 		zfcp_sdev = sdev_to_zfcp(sdev);
1270 		if (zfcp_erp_strat_change_det(&zfcp_sdev->status, erp_status)) {
1271 			_zfcp_erp_lun_reopen(sdev,
1272 					     ZFCP_STATUS_COMMON_ERP_FAILED,
1273 					     "ersscg3", 0);
1274 			return ZFCP_ERP_EXIT;
1275 		}
1276 		break;
1277 	}
1278 	return result;
1279 }
1280 
1281 static void zfcp_erp_action_dequeue(struct zfcp_erp_action *erp_action)
1282 {
1283 	struct zfcp_adapter *adapter = erp_action->adapter;
1284 	struct zfcp_scsi_dev *zfcp_sdev;
1285 
1286 	adapter->erp_total_count--;
1287 	if (erp_action->status & ZFCP_STATUS_ERP_LOWMEM) {
1288 		adapter->erp_low_mem_count--;
1289 		erp_action->status &= ~ZFCP_STATUS_ERP_LOWMEM;
1290 	}
1291 
1292 	list_del(&erp_action->list);
1293 	zfcp_dbf_rec_run("eractd1", erp_action);
1294 
1295 	switch (erp_action->type) {
1296 	case ZFCP_ERP_ACTION_REOPEN_LUN:
1297 		zfcp_sdev = sdev_to_zfcp(erp_action->sdev);
1298 		atomic_andnot(ZFCP_STATUS_COMMON_ERP_INUSE,
1299 				  &zfcp_sdev->status);
1300 		break;
1301 
1302 	case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
1303 	case ZFCP_ERP_ACTION_REOPEN_PORT:
1304 		atomic_andnot(ZFCP_STATUS_COMMON_ERP_INUSE,
1305 				  &erp_action->port->status);
1306 		break;
1307 
1308 	case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
1309 		atomic_andnot(ZFCP_STATUS_COMMON_ERP_INUSE,
1310 				  &erp_action->adapter->status);
1311 		break;
1312 	}
1313 }
1314 
1315 /**
1316  * zfcp_erp_try_rport_unblock - unblock rport if no more/new recovery
1317  * @port: zfcp_port whose fc_rport we should try to unblock
1318  */
1319 static void zfcp_erp_try_rport_unblock(struct zfcp_port *port)
1320 {
1321 	unsigned long flags;
1322 	struct zfcp_adapter *adapter = port->adapter;
1323 	int port_status;
1324 	struct Scsi_Host *shost = adapter->scsi_host;
1325 	struct scsi_device *sdev;
1326 
1327 	write_lock_irqsave(&adapter->erp_lock, flags);
1328 	port_status = atomic_read(&port->status);
1329 	if ((port_status & ZFCP_STATUS_COMMON_UNBLOCKED)    == 0 ||
1330 	    (port_status & (ZFCP_STATUS_COMMON_ERP_INUSE |
1331 			    ZFCP_STATUS_COMMON_ERP_FAILED)) != 0) {
1332 		/* new ERP of severity >= port triggered elsewhere meanwhile or
1333 		 * local link down (adapter erp_failed but not clear unblock)
1334 		 */
1335 		zfcp_dbf_rec_run_lvl(4, "ertru_p", &port->erp_action);
1336 		write_unlock_irqrestore(&adapter->erp_lock, flags);
1337 		return;
1338 	}
1339 	spin_lock(shost->host_lock);
1340 	__shost_for_each_device(sdev, shost) {
1341 		struct zfcp_scsi_dev *zsdev = sdev_to_zfcp(sdev);
1342 		int lun_status;
1343 
1344 		if (zsdev->port != port)
1345 			continue;
1346 		/* LUN under port of interest */
1347 		lun_status = atomic_read(&zsdev->status);
1348 		if ((lun_status & ZFCP_STATUS_COMMON_ERP_FAILED) != 0)
1349 			continue; /* unblock rport despite failed LUNs */
1350 		/* LUN recovery not given up yet [maybe follow-up pending] */
1351 		if ((lun_status & ZFCP_STATUS_COMMON_UNBLOCKED) == 0 ||
1352 		    (lun_status & ZFCP_STATUS_COMMON_ERP_INUSE) != 0) {
1353 			/* LUN blocked:
1354 			 * not yet unblocked [LUN recovery pending]
1355 			 * or meanwhile blocked [new LUN recovery triggered]
1356 			 */
1357 			zfcp_dbf_rec_run_lvl(4, "ertru_l", &zsdev->erp_action);
1358 			spin_unlock(shost->host_lock);
1359 			write_unlock_irqrestore(&adapter->erp_lock, flags);
1360 			return;
1361 		}
1362 	}
1363 	/* now port has no child or all children have completed recovery,
1364 	 * and no ERP of severity >= port was meanwhile triggered elsewhere
1365 	 */
1366 	zfcp_scsi_schedule_rport_register(port);
1367 	spin_unlock(shost->host_lock);
1368 	write_unlock_irqrestore(&adapter->erp_lock, flags);
1369 }
1370 
1371 static void zfcp_erp_action_cleanup(struct zfcp_erp_action *act,
1372 				    enum zfcp_erp_act_result result)
1373 {
1374 	struct zfcp_adapter *adapter = act->adapter;
1375 	struct zfcp_port *port = act->port;
1376 	struct scsi_device *sdev = act->sdev;
1377 
1378 	switch (act->type) {
1379 	case ZFCP_ERP_ACTION_REOPEN_LUN:
1380 		if (!(act->status & ZFCP_STATUS_ERP_NO_REF))
1381 			scsi_device_put(sdev);
1382 		zfcp_erp_try_rport_unblock(port);
1383 		break;
1384 
1385 	case ZFCP_ERP_ACTION_REOPEN_PORT:
1386 		/* This switch case might also happen after a forced reopen
1387 		 * was successfully done and thus overwritten with a new
1388 		 * non-forced reopen at `ersfs_2'. In this case, we must not
1389 		 * do the clean-up of the non-forced version.
1390 		 */
1391 		if (act->step != ZFCP_ERP_STEP_UNINITIALIZED)
1392 			if (result == ZFCP_ERP_SUCCEEDED)
1393 				zfcp_erp_try_rport_unblock(port);
1394 		/* fall through */
1395 	case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
1396 		put_device(&port->dev);
1397 		break;
1398 
1399 	case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
1400 		if (result == ZFCP_ERP_SUCCEEDED) {
1401 			register_service_level(&adapter->service_level);
1402 			zfcp_fc_conditional_port_scan(adapter);
1403 			queue_work(adapter->work_queue, &adapter->ns_up_work);
1404 		} else
1405 			unregister_service_level(&adapter->service_level);
1406 
1407 		kref_put(&adapter->ref, zfcp_adapter_release);
1408 		break;
1409 	}
1410 }
1411 
1412 static enum zfcp_erp_act_result zfcp_erp_strategy_do_action(
1413 	struct zfcp_erp_action *erp_action)
1414 {
1415 	switch (erp_action->type) {
1416 	case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
1417 		return zfcp_erp_adapter_strategy(erp_action);
1418 	case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
1419 		return zfcp_erp_port_forced_strategy(erp_action);
1420 	case ZFCP_ERP_ACTION_REOPEN_PORT:
1421 		return zfcp_erp_port_strategy(erp_action);
1422 	case ZFCP_ERP_ACTION_REOPEN_LUN:
1423 		return zfcp_erp_lun_strategy(erp_action);
1424 	}
1425 	return ZFCP_ERP_FAILED;
1426 }
1427 
1428 static enum zfcp_erp_act_result zfcp_erp_strategy(
1429 	struct zfcp_erp_action *erp_action)
1430 {
1431 	enum zfcp_erp_act_result result;
1432 	unsigned long flags;
1433 	struct zfcp_adapter *adapter = erp_action->adapter;
1434 
1435 	kref_get(&adapter->ref);
1436 
1437 	write_lock_irqsave(&adapter->erp_lock, flags);
1438 	zfcp_erp_strategy_check_fsfreq(erp_action);
1439 
1440 	if (erp_action->status & ZFCP_STATUS_ERP_DISMISSED) {
1441 		zfcp_erp_action_dequeue(erp_action);
1442 		result = ZFCP_ERP_DISMISSED;
1443 		goto unlock;
1444 	}
1445 
1446 	if (erp_action->status & ZFCP_STATUS_ERP_TIMEDOUT) {
1447 		result = ZFCP_ERP_FAILED;
1448 		goto check_target;
1449 	}
1450 
1451 	zfcp_erp_action_to_running(erp_action);
1452 
1453 	/* no lock to allow for blocking operations */
1454 	write_unlock_irqrestore(&adapter->erp_lock, flags);
1455 	result = zfcp_erp_strategy_do_action(erp_action);
1456 	write_lock_irqsave(&adapter->erp_lock, flags);
1457 
1458 	if (erp_action->status & ZFCP_STATUS_ERP_DISMISSED)
1459 		result = ZFCP_ERP_CONTINUES;
1460 
1461 	switch (result) {
1462 	case ZFCP_ERP_NOMEM:
1463 		if (!(erp_action->status & ZFCP_STATUS_ERP_LOWMEM)) {
1464 			++adapter->erp_low_mem_count;
1465 			erp_action->status |= ZFCP_STATUS_ERP_LOWMEM;
1466 		}
1467 		if (adapter->erp_total_count == adapter->erp_low_mem_count)
1468 			_zfcp_erp_adapter_reopen(adapter, 0, "erstgy1");
1469 		else {
1470 			zfcp_erp_strategy_memwait(erp_action);
1471 			result = ZFCP_ERP_CONTINUES;
1472 		}
1473 		goto unlock;
1474 
1475 	case ZFCP_ERP_CONTINUES:
1476 		if (erp_action->status & ZFCP_STATUS_ERP_LOWMEM) {
1477 			--adapter->erp_low_mem_count;
1478 			erp_action->status &= ~ZFCP_STATUS_ERP_LOWMEM;
1479 		}
1480 		goto unlock;
1481 	case ZFCP_ERP_SUCCEEDED:
1482 	case ZFCP_ERP_FAILED:
1483 	case ZFCP_ERP_EXIT:
1484 	case ZFCP_ERP_DISMISSED:
1485 		/* NOP */
1486 		break;
1487 	}
1488 
1489 check_target:
1490 	result = zfcp_erp_strategy_check_target(erp_action, result);
1491 	zfcp_erp_action_dequeue(erp_action);
1492 	result = zfcp_erp_strategy_statechange(erp_action, result);
1493 	if (result == ZFCP_ERP_EXIT)
1494 		goto unlock;
1495 	if (result == ZFCP_ERP_SUCCEEDED)
1496 		zfcp_erp_strategy_followup_success(erp_action);
1497 	if (result == ZFCP_ERP_FAILED)
1498 		zfcp_erp_strategy_followup_failed(erp_action);
1499 
1500  unlock:
1501 	write_unlock_irqrestore(&adapter->erp_lock, flags);
1502 
1503 	if (result != ZFCP_ERP_CONTINUES)
1504 		zfcp_erp_action_cleanup(erp_action, result);
1505 
1506 	kref_put(&adapter->ref, zfcp_adapter_release);
1507 	return result;
1508 }
1509 
1510 static int zfcp_erp_thread(void *data)
1511 {
1512 	struct zfcp_adapter *adapter = (struct zfcp_adapter *) data;
1513 	struct list_head *next;
1514 	struct zfcp_erp_action *act;
1515 	unsigned long flags;
1516 
1517 	for (;;) {
1518 		wait_event_interruptible(adapter->erp_ready_wq,
1519 			   !list_empty(&adapter->erp_ready_head) ||
1520 			   kthread_should_stop());
1521 
1522 		if (kthread_should_stop())
1523 			break;
1524 
1525 		write_lock_irqsave(&adapter->erp_lock, flags);
1526 		next = adapter->erp_ready_head.next;
1527 		write_unlock_irqrestore(&adapter->erp_lock, flags);
1528 
1529 		if (next != &adapter->erp_ready_head) {
1530 			act = list_entry(next, struct zfcp_erp_action, list);
1531 
1532 			/* there is more to come after dismission, no notify */
1533 			if (zfcp_erp_strategy(act) != ZFCP_ERP_DISMISSED)
1534 				zfcp_erp_wakeup(adapter);
1535 		}
1536 	}
1537 
1538 	return 0;
1539 }
1540 
1541 /**
1542  * zfcp_erp_thread_setup - Start ERP thread for adapter
1543  * @adapter: Adapter to start the ERP thread for
1544  *
1545  * Return: 0 on success, or error code from kthread_run().
1546  */
1547 int zfcp_erp_thread_setup(struct zfcp_adapter *adapter)
1548 {
1549 	struct task_struct *thread;
1550 
1551 	thread = kthread_run(zfcp_erp_thread, adapter, "zfcperp%s",
1552 			     dev_name(&adapter->ccw_device->dev));
1553 	if (IS_ERR(thread)) {
1554 		dev_err(&adapter->ccw_device->dev,
1555 			"Creating an ERP thread for the FCP device failed.\n");
1556 		return PTR_ERR(thread);
1557 	}
1558 
1559 	adapter->erp_thread = thread;
1560 	return 0;
1561 }
1562 
1563 /**
1564  * zfcp_erp_thread_kill - Stop ERP thread.
1565  * @adapter: Adapter where the ERP thread should be stopped.
1566  *
1567  * The caller of this routine ensures that the specified adapter has
1568  * been shut down and that this operation has been completed. Thus,
1569  * there are no pending erp_actions which would need to be handled
1570  * here.
1571  */
1572 void zfcp_erp_thread_kill(struct zfcp_adapter *adapter)
1573 {
1574 	kthread_stop(adapter->erp_thread);
1575 	adapter->erp_thread = NULL;
1576 	WARN_ON(!list_empty(&adapter->erp_ready_head));
1577 	WARN_ON(!list_empty(&adapter->erp_running_head));
1578 }
1579 
1580 /**
1581  * zfcp_erp_wait - wait for completion of error recovery on an adapter
1582  * @adapter: adapter for which to wait for completion of its error recovery
1583  */
1584 void zfcp_erp_wait(struct zfcp_adapter *adapter)
1585 {
1586 	wait_event(adapter->erp_done_wqh,
1587 		   !(atomic_read(&adapter->status) &
1588 			ZFCP_STATUS_ADAPTER_ERP_PENDING));
1589 }
1590 
1591 /**
1592  * zfcp_erp_set_adapter_status - set adapter status bits
1593  * @adapter: adapter to change the status
1594  * @mask: status bits to change
1595  *
1596  * Changes in common status bits are propagated to attached ports and LUNs.
1597  */
1598 void zfcp_erp_set_adapter_status(struct zfcp_adapter *adapter, u32 mask)
1599 {
1600 	struct zfcp_port *port;
1601 	struct scsi_device *sdev;
1602 	unsigned long flags;
1603 	u32 common_mask = mask & ZFCP_COMMON_FLAGS;
1604 
1605 	atomic_or(mask, &adapter->status);
1606 
1607 	if (!common_mask)
1608 		return;
1609 
1610 	read_lock_irqsave(&adapter->port_list_lock, flags);
1611 	list_for_each_entry(port, &adapter->port_list, list)
1612 		atomic_or(common_mask, &port->status);
1613 	read_unlock_irqrestore(&adapter->port_list_lock, flags);
1614 
1615 	spin_lock_irqsave(adapter->scsi_host->host_lock, flags);
1616 	__shost_for_each_device(sdev, adapter->scsi_host)
1617 		atomic_or(common_mask, &sdev_to_zfcp(sdev)->status);
1618 	spin_unlock_irqrestore(adapter->scsi_host->host_lock, flags);
1619 }
1620 
1621 /**
1622  * zfcp_erp_clear_adapter_status - clear adapter status bits
1623  * @adapter: adapter to change the status
1624  * @mask: status bits to change
1625  *
1626  * Changes in common status bits are propagated to attached ports and LUNs.
1627  */
1628 void zfcp_erp_clear_adapter_status(struct zfcp_adapter *adapter, u32 mask)
1629 {
1630 	struct zfcp_port *port;
1631 	struct scsi_device *sdev;
1632 	unsigned long flags;
1633 	u32 common_mask = mask & ZFCP_COMMON_FLAGS;
1634 	u32 clear_counter = mask & ZFCP_STATUS_COMMON_ERP_FAILED;
1635 
1636 	atomic_andnot(mask, &adapter->status);
1637 
1638 	if (!common_mask)
1639 		return;
1640 
1641 	if (clear_counter)
1642 		atomic_set(&adapter->erp_counter, 0);
1643 
1644 	read_lock_irqsave(&adapter->port_list_lock, flags);
1645 	list_for_each_entry(port, &adapter->port_list, list) {
1646 		atomic_andnot(common_mask, &port->status);
1647 		if (clear_counter)
1648 			atomic_set(&port->erp_counter, 0);
1649 	}
1650 	read_unlock_irqrestore(&adapter->port_list_lock, flags);
1651 
1652 	spin_lock_irqsave(adapter->scsi_host->host_lock, flags);
1653 	__shost_for_each_device(sdev, adapter->scsi_host) {
1654 		atomic_andnot(common_mask, &sdev_to_zfcp(sdev)->status);
1655 		if (clear_counter)
1656 			atomic_set(&sdev_to_zfcp(sdev)->erp_counter, 0);
1657 	}
1658 	spin_unlock_irqrestore(adapter->scsi_host->host_lock, flags);
1659 }
1660 
1661 /**
1662  * zfcp_erp_set_port_status - set port status bits
1663  * @port: port to change the status
1664  * @mask: status bits to change
1665  *
1666  * Changes in common status bits are propagated to attached LUNs.
1667  */
1668 void zfcp_erp_set_port_status(struct zfcp_port *port, u32 mask)
1669 {
1670 	struct scsi_device *sdev;
1671 	u32 common_mask = mask & ZFCP_COMMON_FLAGS;
1672 	unsigned long flags;
1673 
1674 	atomic_or(mask, &port->status);
1675 
1676 	if (!common_mask)
1677 		return;
1678 
1679 	spin_lock_irqsave(port->adapter->scsi_host->host_lock, flags);
1680 	__shost_for_each_device(sdev, port->adapter->scsi_host)
1681 		if (sdev_to_zfcp(sdev)->port == port)
1682 			atomic_or(common_mask,
1683 					&sdev_to_zfcp(sdev)->status);
1684 	spin_unlock_irqrestore(port->adapter->scsi_host->host_lock, flags);
1685 }
1686 
1687 /**
1688  * zfcp_erp_clear_port_status - clear port status bits
1689  * @port: adapter to change the status
1690  * @mask: status bits to change
1691  *
1692  * Changes in common status bits are propagated to attached LUNs.
1693  */
1694 void zfcp_erp_clear_port_status(struct zfcp_port *port, u32 mask)
1695 {
1696 	struct scsi_device *sdev;
1697 	u32 common_mask = mask & ZFCP_COMMON_FLAGS;
1698 	u32 clear_counter = mask & ZFCP_STATUS_COMMON_ERP_FAILED;
1699 	unsigned long flags;
1700 
1701 	atomic_andnot(mask, &port->status);
1702 
1703 	if (!common_mask)
1704 		return;
1705 
1706 	if (clear_counter)
1707 		atomic_set(&port->erp_counter, 0);
1708 
1709 	spin_lock_irqsave(port->adapter->scsi_host->host_lock, flags);
1710 	__shost_for_each_device(sdev, port->adapter->scsi_host)
1711 		if (sdev_to_zfcp(sdev)->port == port) {
1712 			atomic_andnot(common_mask,
1713 					  &sdev_to_zfcp(sdev)->status);
1714 			if (clear_counter)
1715 				atomic_set(&sdev_to_zfcp(sdev)->erp_counter, 0);
1716 		}
1717 	spin_unlock_irqrestore(port->adapter->scsi_host->host_lock, flags);
1718 }
1719 
1720 /**
1721  * zfcp_erp_set_lun_status - set lun status bits
1722  * @sdev: SCSI device / lun to set the status bits
1723  * @mask: status bits to change
1724  */
1725 void zfcp_erp_set_lun_status(struct scsi_device *sdev, u32 mask)
1726 {
1727 	struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
1728 
1729 	atomic_or(mask, &zfcp_sdev->status);
1730 }
1731 
1732 /**
1733  * zfcp_erp_clear_lun_status - clear lun status bits
1734  * @sdev: SCSi device / lun to clear the status bits
1735  * @mask: status bits to change
1736  */
1737 void zfcp_erp_clear_lun_status(struct scsi_device *sdev, u32 mask)
1738 {
1739 	struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
1740 
1741 	atomic_andnot(mask, &zfcp_sdev->status);
1742 
1743 	if (mask & ZFCP_STATUS_COMMON_ERP_FAILED)
1744 		atomic_set(&zfcp_sdev->erp_counter, 0);
1745 }
1746 
1747 /**
1748  * zfcp_erp_adapter_reset_sync() - Really reopen adapter and wait.
1749  * @adapter: Pointer to zfcp_adapter to reopen.
1750  * @dbftag: Trace tag string of length %ZFCP_DBF_TAG_LEN.
1751  */
1752 void zfcp_erp_adapter_reset_sync(struct zfcp_adapter *adapter, char *dbftag)
1753 {
1754 	zfcp_erp_set_adapter_status(adapter, ZFCP_STATUS_COMMON_RUNNING);
1755 	zfcp_erp_adapter_reopen(adapter, ZFCP_STATUS_COMMON_ERP_FAILED, dbftag);
1756 	zfcp_erp_wait(adapter);
1757 }
1758