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