xref: /openbmc/linux/drivers/s390/scsi/zfcp_erp.c (revision 1fa0a7dc)
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 void zfcp_erp_port_forced_reopen_all(struct zfcp_adapter *adapter,
628 				     int clear, char *dbftag)
629 {
630 	unsigned long flags;
631 	struct zfcp_port *port;
632 
633 	write_lock_irqsave(&adapter->erp_lock, flags);
634 	read_lock(&adapter->port_list_lock);
635 	list_for_each_entry(port, &adapter->port_list, list)
636 		_zfcp_erp_port_forced_reopen(port, clear, dbftag);
637 	read_unlock(&adapter->port_list_lock);
638 	write_unlock_irqrestore(&adapter->erp_lock, flags);
639 }
640 
641 static void _zfcp_erp_port_reopen_all(struct zfcp_adapter *adapter,
642 				      int clear, char *dbftag)
643 {
644 	struct zfcp_port *port;
645 
646 	read_lock(&adapter->port_list_lock);
647 	list_for_each_entry(port, &adapter->port_list, list)
648 		_zfcp_erp_port_reopen(port, clear, dbftag);
649 	read_unlock(&adapter->port_list_lock);
650 }
651 
652 static void _zfcp_erp_lun_reopen_all(struct zfcp_port *port, int clear,
653 				     char *dbftag)
654 {
655 	struct scsi_device *sdev;
656 
657 	spin_lock(port->adapter->scsi_host->host_lock);
658 	__shost_for_each_device(sdev, port->adapter->scsi_host)
659 		if (sdev_to_zfcp(sdev)->port == port)
660 			_zfcp_erp_lun_reopen(sdev, clear, dbftag, 0);
661 	spin_unlock(port->adapter->scsi_host->host_lock);
662 }
663 
664 static void zfcp_erp_strategy_followup_failed(struct zfcp_erp_action *act)
665 {
666 	switch (act->type) {
667 	case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
668 		_zfcp_erp_adapter_reopen(act->adapter, 0, "ersff_1");
669 		break;
670 	case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
671 		_zfcp_erp_port_forced_reopen(act->port, 0, "ersff_2");
672 		break;
673 	case ZFCP_ERP_ACTION_REOPEN_PORT:
674 		_zfcp_erp_port_reopen(act->port, 0, "ersff_3");
675 		break;
676 	case ZFCP_ERP_ACTION_REOPEN_LUN:
677 		_zfcp_erp_lun_reopen(act->sdev, 0, "ersff_4", 0);
678 		break;
679 	}
680 }
681 
682 static void zfcp_erp_strategy_followup_success(struct zfcp_erp_action *act)
683 {
684 	switch (act->type) {
685 	case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
686 		_zfcp_erp_port_reopen_all(act->adapter, 0, "ersfs_1");
687 		break;
688 	case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
689 		_zfcp_erp_port_reopen(act->port, 0, "ersfs_2");
690 		break;
691 	case ZFCP_ERP_ACTION_REOPEN_PORT:
692 		_zfcp_erp_lun_reopen_all(act->port, 0, "ersfs_3");
693 		break;
694 	case ZFCP_ERP_ACTION_REOPEN_LUN:
695 		/* NOP */
696 		break;
697 	}
698 }
699 
700 static void zfcp_erp_wakeup(struct zfcp_adapter *adapter)
701 {
702 	unsigned long flags;
703 
704 	read_lock_irqsave(&adapter->erp_lock, flags);
705 	if (list_empty(&adapter->erp_ready_head) &&
706 	    list_empty(&adapter->erp_running_head)) {
707 			atomic_andnot(ZFCP_STATUS_ADAPTER_ERP_PENDING,
708 					  &adapter->status);
709 			wake_up(&adapter->erp_done_wqh);
710 	}
711 	read_unlock_irqrestore(&adapter->erp_lock, flags);
712 }
713 
714 static void zfcp_erp_enqueue_ptp_port(struct zfcp_adapter *adapter)
715 {
716 	struct zfcp_port *port;
717 	port = zfcp_port_enqueue(adapter, adapter->peer_wwpn, 0,
718 				 adapter->peer_d_id);
719 	if (IS_ERR(port)) /* error or port already attached */
720 		return;
721 	_zfcp_erp_port_reopen(port, 0, "ereptp1");
722 }
723 
724 static enum zfcp_erp_act_result zfcp_erp_adapter_strat_fsf_xconf(
725 	struct zfcp_erp_action *erp_action)
726 {
727 	int retries;
728 	int sleep = 1;
729 	struct zfcp_adapter *adapter = erp_action->adapter;
730 
731 	atomic_andnot(ZFCP_STATUS_ADAPTER_XCONFIG_OK, &adapter->status);
732 
733 	for (retries = 7; retries; retries--) {
734 		atomic_andnot(ZFCP_STATUS_ADAPTER_HOST_CON_INIT,
735 				  &adapter->status);
736 		write_lock_irq(&adapter->erp_lock);
737 		zfcp_erp_action_to_running(erp_action);
738 		write_unlock_irq(&adapter->erp_lock);
739 		if (zfcp_fsf_exchange_config_data(erp_action)) {
740 			atomic_andnot(ZFCP_STATUS_ADAPTER_HOST_CON_INIT,
741 					  &adapter->status);
742 			return ZFCP_ERP_FAILED;
743 		}
744 
745 		wait_event(adapter->erp_ready_wq,
746 			   !list_empty(&adapter->erp_ready_head));
747 		if (erp_action->status & ZFCP_STATUS_ERP_TIMEDOUT)
748 			break;
749 
750 		if (!(atomic_read(&adapter->status) &
751 		      ZFCP_STATUS_ADAPTER_HOST_CON_INIT))
752 			break;
753 
754 		ssleep(sleep);
755 		sleep *= 2;
756 	}
757 
758 	atomic_andnot(ZFCP_STATUS_ADAPTER_HOST_CON_INIT,
759 			  &adapter->status);
760 
761 	if (!(atomic_read(&adapter->status) & ZFCP_STATUS_ADAPTER_XCONFIG_OK))
762 		return ZFCP_ERP_FAILED;
763 
764 	if (fc_host_port_type(adapter->scsi_host) == FC_PORTTYPE_PTP)
765 		zfcp_erp_enqueue_ptp_port(adapter);
766 
767 	return ZFCP_ERP_SUCCEEDED;
768 }
769 
770 static enum zfcp_erp_act_result zfcp_erp_adapter_strategy_open_fsf_xport(
771 	struct zfcp_erp_action *act)
772 {
773 	int ret;
774 	struct zfcp_adapter *adapter = act->adapter;
775 
776 	write_lock_irq(&adapter->erp_lock);
777 	zfcp_erp_action_to_running(act);
778 	write_unlock_irq(&adapter->erp_lock);
779 
780 	ret = zfcp_fsf_exchange_port_data(act);
781 	if (ret == -EOPNOTSUPP)
782 		return ZFCP_ERP_SUCCEEDED;
783 	if (ret)
784 		return ZFCP_ERP_FAILED;
785 
786 	zfcp_dbf_rec_run("erasox1", act);
787 	wait_event(adapter->erp_ready_wq,
788 		   !list_empty(&adapter->erp_ready_head));
789 	zfcp_dbf_rec_run("erasox2", act);
790 	if (act->status & ZFCP_STATUS_ERP_TIMEDOUT)
791 		return ZFCP_ERP_FAILED;
792 
793 	return ZFCP_ERP_SUCCEEDED;
794 }
795 
796 static enum zfcp_erp_act_result zfcp_erp_adapter_strategy_open_fsf(
797 	struct zfcp_erp_action *act)
798 {
799 	if (zfcp_erp_adapter_strat_fsf_xconf(act) == ZFCP_ERP_FAILED)
800 		return ZFCP_ERP_FAILED;
801 
802 	if (zfcp_erp_adapter_strategy_open_fsf_xport(act) == ZFCP_ERP_FAILED)
803 		return ZFCP_ERP_FAILED;
804 
805 	if (mempool_resize(act->adapter->pool.sr_data,
806 			   act->adapter->stat_read_buf_num))
807 		return ZFCP_ERP_FAILED;
808 
809 	if (mempool_resize(act->adapter->pool.status_read_req,
810 			   act->adapter->stat_read_buf_num))
811 		return ZFCP_ERP_FAILED;
812 
813 	atomic_set(&act->adapter->stat_miss, act->adapter->stat_read_buf_num);
814 	if (zfcp_status_read_refill(act->adapter))
815 		return ZFCP_ERP_FAILED;
816 
817 	return ZFCP_ERP_SUCCEEDED;
818 }
819 
820 static void zfcp_erp_adapter_strategy_close(struct zfcp_erp_action *act)
821 {
822 	struct zfcp_adapter *adapter = act->adapter;
823 
824 	/* close queues to ensure that buffers are not accessed by adapter */
825 	zfcp_qdio_close(adapter->qdio);
826 	zfcp_fsf_req_dismiss_all(adapter);
827 	adapter->fsf_req_seq_no = 0;
828 	zfcp_fc_wka_ports_force_offline(adapter->gs);
829 	/* all ports and LUNs are closed */
830 	zfcp_erp_clear_adapter_status(adapter, ZFCP_STATUS_COMMON_OPEN);
831 
832 	atomic_andnot(ZFCP_STATUS_ADAPTER_XCONFIG_OK |
833 			  ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED, &adapter->status);
834 }
835 
836 static enum zfcp_erp_act_result zfcp_erp_adapter_strategy_open(
837 	struct zfcp_erp_action *act)
838 {
839 	struct zfcp_adapter *adapter = act->adapter;
840 
841 	if (zfcp_qdio_open(adapter->qdio)) {
842 		atomic_andnot(ZFCP_STATUS_ADAPTER_XCONFIG_OK |
843 				  ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED,
844 				  &adapter->status);
845 		return ZFCP_ERP_FAILED;
846 	}
847 
848 	if (zfcp_erp_adapter_strategy_open_fsf(act)) {
849 		zfcp_erp_adapter_strategy_close(act);
850 		return ZFCP_ERP_FAILED;
851 	}
852 
853 	atomic_or(ZFCP_STATUS_COMMON_OPEN, &adapter->status);
854 
855 	return ZFCP_ERP_SUCCEEDED;
856 }
857 
858 static enum zfcp_erp_act_result zfcp_erp_adapter_strategy(
859 	struct zfcp_erp_action *act)
860 {
861 	struct zfcp_adapter *adapter = act->adapter;
862 
863 	if (atomic_read(&adapter->status) & ZFCP_STATUS_COMMON_OPEN) {
864 		zfcp_erp_adapter_strategy_close(act);
865 		if (act->status & ZFCP_STATUS_ERP_CLOSE_ONLY)
866 			return ZFCP_ERP_EXIT;
867 	}
868 
869 	if (zfcp_erp_adapter_strategy_open(act)) {
870 		ssleep(8);
871 		return ZFCP_ERP_FAILED;
872 	}
873 
874 	return ZFCP_ERP_SUCCEEDED;
875 }
876 
877 static enum zfcp_erp_act_result zfcp_erp_port_forced_strategy_close(
878 	struct zfcp_erp_action *act)
879 {
880 	int retval;
881 
882 	retval = zfcp_fsf_close_physical_port(act);
883 	if (retval == -ENOMEM)
884 		return ZFCP_ERP_NOMEM;
885 	act->step = ZFCP_ERP_STEP_PHYS_PORT_CLOSING;
886 	if (retval)
887 		return ZFCP_ERP_FAILED;
888 
889 	return ZFCP_ERP_CONTINUES;
890 }
891 
892 static enum zfcp_erp_act_result zfcp_erp_port_forced_strategy(
893 	struct zfcp_erp_action *erp_action)
894 {
895 	struct zfcp_port *port = erp_action->port;
896 	int status = atomic_read(&port->status);
897 
898 	switch (erp_action->step) {
899 	case ZFCP_ERP_STEP_UNINITIALIZED:
900 		if ((status & ZFCP_STATUS_PORT_PHYS_OPEN) &&
901 		    (status & ZFCP_STATUS_COMMON_OPEN))
902 			return zfcp_erp_port_forced_strategy_close(erp_action);
903 		else
904 			return ZFCP_ERP_FAILED;
905 
906 	case ZFCP_ERP_STEP_PHYS_PORT_CLOSING:
907 		if (!(status & ZFCP_STATUS_PORT_PHYS_OPEN))
908 			return ZFCP_ERP_SUCCEEDED;
909 		break;
910 	case ZFCP_ERP_STEP_PORT_CLOSING:
911 	case ZFCP_ERP_STEP_PORT_OPENING:
912 	case ZFCP_ERP_STEP_LUN_CLOSING:
913 	case ZFCP_ERP_STEP_LUN_OPENING:
914 		/* NOP */
915 		break;
916 	}
917 	return ZFCP_ERP_FAILED;
918 }
919 
920 static enum zfcp_erp_act_result zfcp_erp_port_strategy_close(
921 	struct zfcp_erp_action *erp_action)
922 {
923 	int retval;
924 
925 	retval = zfcp_fsf_close_port(erp_action);
926 	if (retval == -ENOMEM)
927 		return ZFCP_ERP_NOMEM;
928 	erp_action->step = ZFCP_ERP_STEP_PORT_CLOSING;
929 	if (retval)
930 		return ZFCP_ERP_FAILED;
931 	return ZFCP_ERP_CONTINUES;
932 }
933 
934 static enum zfcp_erp_act_result zfcp_erp_port_strategy_open_port(
935 	struct zfcp_erp_action *erp_action)
936 {
937 	int retval;
938 
939 	retval = zfcp_fsf_open_port(erp_action);
940 	if (retval == -ENOMEM)
941 		return ZFCP_ERP_NOMEM;
942 	erp_action->step = ZFCP_ERP_STEP_PORT_OPENING;
943 	if (retval)
944 		return ZFCP_ERP_FAILED;
945 	return ZFCP_ERP_CONTINUES;
946 }
947 
948 static int zfcp_erp_open_ptp_port(struct zfcp_erp_action *act)
949 {
950 	struct zfcp_adapter *adapter = act->adapter;
951 	struct zfcp_port *port = act->port;
952 
953 	if (port->wwpn != adapter->peer_wwpn) {
954 		zfcp_erp_set_port_status(port, ZFCP_STATUS_COMMON_ERP_FAILED);
955 		return ZFCP_ERP_FAILED;
956 	}
957 	port->d_id = adapter->peer_d_id;
958 	return zfcp_erp_port_strategy_open_port(act);
959 }
960 
961 static enum zfcp_erp_act_result zfcp_erp_port_strategy_open_common(
962 	struct zfcp_erp_action *act)
963 {
964 	struct zfcp_adapter *adapter = act->adapter;
965 	struct zfcp_port *port = act->port;
966 	int p_status = atomic_read(&port->status);
967 
968 	switch (act->step) {
969 	case ZFCP_ERP_STEP_UNINITIALIZED:
970 	case ZFCP_ERP_STEP_PHYS_PORT_CLOSING:
971 	case ZFCP_ERP_STEP_PORT_CLOSING:
972 		if (fc_host_port_type(adapter->scsi_host) == FC_PORTTYPE_PTP)
973 			return zfcp_erp_open_ptp_port(act);
974 		if (!port->d_id) {
975 			zfcp_fc_trigger_did_lookup(port);
976 			return ZFCP_ERP_EXIT;
977 		}
978 		return zfcp_erp_port_strategy_open_port(act);
979 
980 	case ZFCP_ERP_STEP_PORT_OPENING:
981 		/* D_ID might have changed during open */
982 		if (p_status & ZFCP_STATUS_COMMON_OPEN) {
983 			if (!port->d_id) {
984 				zfcp_fc_trigger_did_lookup(port);
985 				return ZFCP_ERP_EXIT;
986 			}
987 			return ZFCP_ERP_SUCCEEDED;
988 		}
989 		if (port->d_id && !(p_status & ZFCP_STATUS_COMMON_NOESC)) {
990 			port->d_id = 0;
991 			return ZFCP_ERP_FAILED;
992 		}
993 		/* no early return otherwise, continue after switch case */
994 		break;
995 	case ZFCP_ERP_STEP_LUN_CLOSING:
996 	case ZFCP_ERP_STEP_LUN_OPENING:
997 		/* NOP */
998 		break;
999 	}
1000 	return ZFCP_ERP_FAILED;
1001 }
1002 
1003 static enum zfcp_erp_act_result zfcp_erp_port_strategy(
1004 	struct zfcp_erp_action *erp_action)
1005 {
1006 	struct zfcp_port *port = erp_action->port;
1007 	int p_status = atomic_read(&port->status);
1008 
1009 	if ((p_status & ZFCP_STATUS_COMMON_NOESC) &&
1010 	    !(p_status & ZFCP_STATUS_COMMON_OPEN))
1011 		goto close_init_done;
1012 
1013 	switch (erp_action->step) {
1014 	case ZFCP_ERP_STEP_UNINITIALIZED:
1015 		if (p_status & ZFCP_STATUS_COMMON_OPEN)
1016 			return zfcp_erp_port_strategy_close(erp_action);
1017 		break;
1018 
1019 	case ZFCP_ERP_STEP_PORT_CLOSING:
1020 		if (p_status & ZFCP_STATUS_COMMON_OPEN)
1021 			return ZFCP_ERP_FAILED;
1022 		break;
1023 	case ZFCP_ERP_STEP_PHYS_PORT_CLOSING:
1024 	case ZFCP_ERP_STEP_PORT_OPENING:
1025 	case ZFCP_ERP_STEP_LUN_CLOSING:
1026 	case ZFCP_ERP_STEP_LUN_OPENING:
1027 		/* NOP */
1028 		break;
1029 	}
1030 
1031 close_init_done:
1032 	if (erp_action->status & ZFCP_STATUS_ERP_CLOSE_ONLY)
1033 		return ZFCP_ERP_EXIT;
1034 
1035 	return zfcp_erp_port_strategy_open_common(erp_action);
1036 }
1037 
1038 static void zfcp_erp_lun_strategy_clearstati(struct scsi_device *sdev)
1039 {
1040 	struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
1041 
1042 	atomic_andnot(ZFCP_STATUS_COMMON_ACCESS_DENIED,
1043 			  &zfcp_sdev->status);
1044 }
1045 
1046 static enum zfcp_erp_act_result zfcp_erp_lun_strategy_close(
1047 	struct zfcp_erp_action *erp_action)
1048 {
1049 	int retval = zfcp_fsf_close_lun(erp_action);
1050 	if (retval == -ENOMEM)
1051 		return ZFCP_ERP_NOMEM;
1052 	erp_action->step = ZFCP_ERP_STEP_LUN_CLOSING;
1053 	if (retval)
1054 		return ZFCP_ERP_FAILED;
1055 	return ZFCP_ERP_CONTINUES;
1056 }
1057 
1058 static enum zfcp_erp_act_result zfcp_erp_lun_strategy_open(
1059 	struct zfcp_erp_action *erp_action)
1060 {
1061 	int retval = zfcp_fsf_open_lun(erp_action);
1062 	if (retval == -ENOMEM)
1063 		return ZFCP_ERP_NOMEM;
1064 	erp_action->step = ZFCP_ERP_STEP_LUN_OPENING;
1065 	if (retval)
1066 		return  ZFCP_ERP_FAILED;
1067 	return ZFCP_ERP_CONTINUES;
1068 }
1069 
1070 static enum zfcp_erp_act_result zfcp_erp_lun_strategy(
1071 	struct zfcp_erp_action *erp_action)
1072 {
1073 	struct scsi_device *sdev = erp_action->sdev;
1074 	struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
1075 
1076 	switch (erp_action->step) {
1077 	case ZFCP_ERP_STEP_UNINITIALIZED:
1078 		zfcp_erp_lun_strategy_clearstati(sdev);
1079 		if (atomic_read(&zfcp_sdev->status) & ZFCP_STATUS_COMMON_OPEN)
1080 			return zfcp_erp_lun_strategy_close(erp_action);
1081 		/* already closed */
1082 		/* fall through */
1083 	case ZFCP_ERP_STEP_LUN_CLOSING:
1084 		if (atomic_read(&zfcp_sdev->status) & ZFCP_STATUS_COMMON_OPEN)
1085 			return ZFCP_ERP_FAILED;
1086 		if (erp_action->status & ZFCP_STATUS_ERP_CLOSE_ONLY)
1087 			return ZFCP_ERP_EXIT;
1088 		return zfcp_erp_lun_strategy_open(erp_action);
1089 
1090 	case ZFCP_ERP_STEP_LUN_OPENING:
1091 		if (atomic_read(&zfcp_sdev->status) & ZFCP_STATUS_COMMON_OPEN)
1092 			return ZFCP_ERP_SUCCEEDED;
1093 		break;
1094 	case ZFCP_ERP_STEP_PHYS_PORT_CLOSING:
1095 	case ZFCP_ERP_STEP_PORT_CLOSING:
1096 	case ZFCP_ERP_STEP_PORT_OPENING:
1097 		/* NOP */
1098 		break;
1099 	}
1100 	return ZFCP_ERP_FAILED;
1101 }
1102 
1103 static enum zfcp_erp_act_result zfcp_erp_strategy_check_lun(
1104 	struct scsi_device *sdev, enum zfcp_erp_act_result result)
1105 {
1106 	struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
1107 
1108 	switch (result) {
1109 	case ZFCP_ERP_SUCCEEDED :
1110 		atomic_set(&zfcp_sdev->erp_counter, 0);
1111 		zfcp_erp_lun_unblock(sdev);
1112 		break;
1113 	case ZFCP_ERP_FAILED :
1114 		atomic_inc(&zfcp_sdev->erp_counter);
1115 		if (atomic_read(&zfcp_sdev->erp_counter) > ZFCP_MAX_ERPS) {
1116 			dev_err(&zfcp_sdev->port->adapter->ccw_device->dev,
1117 				"ERP failed for LUN 0x%016Lx on "
1118 				"port 0x%016Lx\n",
1119 				(unsigned long long)zfcp_scsi_dev_lun(sdev),
1120 				(unsigned long long)zfcp_sdev->port->wwpn);
1121 			zfcp_erp_set_lun_status(sdev,
1122 						ZFCP_STATUS_COMMON_ERP_FAILED);
1123 		}
1124 		break;
1125 	case ZFCP_ERP_CONTINUES:
1126 	case ZFCP_ERP_EXIT:
1127 	case ZFCP_ERP_DISMISSED:
1128 	case ZFCP_ERP_NOMEM:
1129 		/* NOP */
1130 		break;
1131 	}
1132 
1133 	if (atomic_read(&zfcp_sdev->status) & ZFCP_STATUS_COMMON_ERP_FAILED) {
1134 		zfcp_erp_lun_block(sdev, 0);
1135 		result = ZFCP_ERP_EXIT;
1136 	}
1137 	return result;
1138 }
1139 
1140 static enum zfcp_erp_act_result zfcp_erp_strategy_check_port(
1141 	struct zfcp_port *port, enum zfcp_erp_act_result result)
1142 {
1143 	switch (result) {
1144 	case ZFCP_ERP_SUCCEEDED :
1145 		atomic_set(&port->erp_counter, 0);
1146 		zfcp_erp_port_unblock(port);
1147 		break;
1148 
1149 	case ZFCP_ERP_FAILED :
1150 		if (atomic_read(&port->status) & ZFCP_STATUS_COMMON_NOESC) {
1151 			zfcp_erp_port_block(port, 0);
1152 			result = ZFCP_ERP_EXIT;
1153 		}
1154 		atomic_inc(&port->erp_counter);
1155 		if (atomic_read(&port->erp_counter) > ZFCP_MAX_ERPS) {
1156 			dev_err(&port->adapter->ccw_device->dev,
1157 				"ERP failed for remote port 0x%016Lx\n",
1158 				(unsigned long long)port->wwpn);
1159 			zfcp_erp_set_port_status(port,
1160 					 ZFCP_STATUS_COMMON_ERP_FAILED);
1161 		}
1162 		break;
1163 	case ZFCP_ERP_CONTINUES:
1164 	case ZFCP_ERP_EXIT:
1165 	case ZFCP_ERP_DISMISSED:
1166 	case ZFCP_ERP_NOMEM:
1167 		/* NOP */
1168 		break;
1169 	}
1170 
1171 	if (atomic_read(&port->status) & ZFCP_STATUS_COMMON_ERP_FAILED) {
1172 		zfcp_erp_port_block(port, 0);
1173 		result = ZFCP_ERP_EXIT;
1174 	}
1175 	return result;
1176 }
1177 
1178 static enum zfcp_erp_act_result zfcp_erp_strategy_check_adapter(
1179 	struct zfcp_adapter *adapter, enum zfcp_erp_act_result result)
1180 {
1181 	switch (result) {
1182 	case ZFCP_ERP_SUCCEEDED :
1183 		atomic_set(&adapter->erp_counter, 0);
1184 		zfcp_erp_adapter_unblock(adapter);
1185 		break;
1186 
1187 	case ZFCP_ERP_FAILED :
1188 		atomic_inc(&adapter->erp_counter);
1189 		if (atomic_read(&adapter->erp_counter) > ZFCP_MAX_ERPS) {
1190 			dev_err(&adapter->ccw_device->dev,
1191 				"ERP cannot recover an error "
1192 				"on the FCP device\n");
1193 			zfcp_erp_set_adapter_status(adapter,
1194 					    ZFCP_STATUS_COMMON_ERP_FAILED);
1195 		}
1196 		break;
1197 	case ZFCP_ERP_CONTINUES:
1198 	case ZFCP_ERP_EXIT:
1199 	case ZFCP_ERP_DISMISSED:
1200 	case ZFCP_ERP_NOMEM:
1201 		/* NOP */
1202 		break;
1203 	}
1204 
1205 	if (atomic_read(&adapter->status) & ZFCP_STATUS_COMMON_ERP_FAILED) {
1206 		zfcp_erp_adapter_block(adapter, 0);
1207 		result = ZFCP_ERP_EXIT;
1208 	}
1209 	return result;
1210 }
1211 
1212 static enum zfcp_erp_act_result zfcp_erp_strategy_check_target(
1213 	struct zfcp_erp_action *erp_action, enum zfcp_erp_act_result result)
1214 {
1215 	struct zfcp_adapter *adapter = erp_action->adapter;
1216 	struct zfcp_port *port = erp_action->port;
1217 	struct scsi_device *sdev = erp_action->sdev;
1218 
1219 	switch (erp_action->type) {
1220 
1221 	case ZFCP_ERP_ACTION_REOPEN_LUN:
1222 		result = zfcp_erp_strategy_check_lun(sdev, result);
1223 		break;
1224 
1225 	case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
1226 	case ZFCP_ERP_ACTION_REOPEN_PORT:
1227 		result = zfcp_erp_strategy_check_port(port, result);
1228 		break;
1229 
1230 	case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
1231 		result = zfcp_erp_strategy_check_adapter(adapter, result);
1232 		break;
1233 	}
1234 	return result;
1235 }
1236 
1237 static int zfcp_erp_strat_change_det(atomic_t *target_status, u32 erp_status)
1238 {
1239 	int status = atomic_read(target_status);
1240 
1241 	if ((status & ZFCP_STATUS_COMMON_RUNNING) &&
1242 	    (erp_status & ZFCP_STATUS_ERP_CLOSE_ONLY))
1243 		return 1; /* take it online */
1244 
1245 	if (!(status & ZFCP_STATUS_COMMON_RUNNING) &&
1246 	    !(erp_status & ZFCP_STATUS_ERP_CLOSE_ONLY))
1247 		return 1; /* take it offline */
1248 
1249 	return 0;
1250 }
1251 
1252 static enum zfcp_erp_act_result zfcp_erp_strategy_statechange(
1253 	struct zfcp_erp_action *act, enum zfcp_erp_act_result result)
1254 {
1255 	enum zfcp_erp_act_type type = act->type;
1256 	struct zfcp_adapter *adapter = act->adapter;
1257 	struct zfcp_port *port = act->port;
1258 	struct scsi_device *sdev = act->sdev;
1259 	struct zfcp_scsi_dev *zfcp_sdev;
1260 	u32 erp_status = act->status;
1261 
1262 	switch (type) {
1263 	case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
1264 		if (zfcp_erp_strat_change_det(&adapter->status, erp_status)) {
1265 			_zfcp_erp_adapter_reopen(adapter,
1266 						 ZFCP_STATUS_COMMON_ERP_FAILED,
1267 						 "ersscg1");
1268 			return ZFCP_ERP_EXIT;
1269 		}
1270 		break;
1271 
1272 	case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
1273 	case ZFCP_ERP_ACTION_REOPEN_PORT:
1274 		if (zfcp_erp_strat_change_det(&port->status, erp_status)) {
1275 			_zfcp_erp_port_reopen(port,
1276 					      ZFCP_STATUS_COMMON_ERP_FAILED,
1277 					      "ersscg2");
1278 			return ZFCP_ERP_EXIT;
1279 		}
1280 		break;
1281 
1282 	case ZFCP_ERP_ACTION_REOPEN_LUN:
1283 		zfcp_sdev = sdev_to_zfcp(sdev);
1284 		if (zfcp_erp_strat_change_det(&zfcp_sdev->status, erp_status)) {
1285 			_zfcp_erp_lun_reopen(sdev,
1286 					     ZFCP_STATUS_COMMON_ERP_FAILED,
1287 					     "ersscg3", 0);
1288 			return ZFCP_ERP_EXIT;
1289 		}
1290 		break;
1291 	}
1292 	return result;
1293 }
1294 
1295 static void zfcp_erp_action_dequeue(struct zfcp_erp_action *erp_action)
1296 {
1297 	struct zfcp_adapter *adapter = erp_action->adapter;
1298 	struct zfcp_scsi_dev *zfcp_sdev;
1299 
1300 	adapter->erp_total_count--;
1301 	if (erp_action->status & ZFCP_STATUS_ERP_LOWMEM) {
1302 		adapter->erp_low_mem_count--;
1303 		erp_action->status &= ~ZFCP_STATUS_ERP_LOWMEM;
1304 	}
1305 
1306 	list_del(&erp_action->list);
1307 	zfcp_dbf_rec_run("eractd1", erp_action);
1308 
1309 	switch (erp_action->type) {
1310 	case ZFCP_ERP_ACTION_REOPEN_LUN:
1311 		zfcp_sdev = sdev_to_zfcp(erp_action->sdev);
1312 		atomic_andnot(ZFCP_STATUS_COMMON_ERP_INUSE,
1313 				  &zfcp_sdev->status);
1314 		break;
1315 
1316 	case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
1317 	case ZFCP_ERP_ACTION_REOPEN_PORT:
1318 		atomic_andnot(ZFCP_STATUS_COMMON_ERP_INUSE,
1319 				  &erp_action->port->status);
1320 		break;
1321 
1322 	case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
1323 		atomic_andnot(ZFCP_STATUS_COMMON_ERP_INUSE,
1324 				  &erp_action->adapter->status);
1325 		break;
1326 	}
1327 }
1328 
1329 /**
1330  * zfcp_erp_try_rport_unblock - unblock rport if no more/new recovery
1331  * @port: zfcp_port whose fc_rport we should try to unblock
1332  */
1333 static void zfcp_erp_try_rport_unblock(struct zfcp_port *port)
1334 {
1335 	unsigned long flags;
1336 	struct zfcp_adapter *adapter = port->adapter;
1337 	int port_status;
1338 	struct Scsi_Host *shost = adapter->scsi_host;
1339 	struct scsi_device *sdev;
1340 
1341 	write_lock_irqsave(&adapter->erp_lock, flags);
1342 	port_status = atomic_read(&port->status);
1343 	if ((port_status & ZFCP_STATUS_COMMON_UNBLOCKED)    == 0 ||
1344 	    (port_status & (ZFCP_STATUS_COMMON_ERP_INUSE |
1345 			    ZFCP_STATUS_COMMON_ERP_FAILED)) != 0) {
1346 		/* new ERP of severity >= port triggered elsewhere meanwhile or
1347 		 * local link down (adapter erp_failed but not clear unblock)
1348 		 */
1349 		zfcp_dbf_rec_run_lvl(4, "ertru_p", &port->erp_action);
1350 		write_unlock_irqrestore(&adapter->erp_lock, flags);
1351 		return;
1352 	}
1353 	spin_lock(shost->host_lock);
1354 	__shost_for_each_device(sdev, shost) {
1355 		struct zfcp_scsi_dev *zsdev = sdev_to_zfcp(sdev);
1356 		int lun_status;
1357 
1358 		if (sdev->sdev_state == SDEV_DEL ||
1359 		    sdev->sdev_state == SDEV_CANCEL)
1360 			continue;
1361 		if (zsdev->port != port)
1362 			continue;
1363 		/* LUN under port of interest */
1364 		lun_status = atomic_read(&zsdev->status);
1365 		if ((lun_status & ZFCP_STATUS_COMMON_ERP_FAILED) != 0)
1366 			continue; /* unblock rport despite failed LUNs */
1367 		/* LUN recovery not given up yet [maybe follow-up pending] */
1368 		if ((lun_status & ZFCP_STATUS_COMMON_UNBLOCKED) == 0 ||
1369 		    (lun_status & ZFCP_STATUS_COMMON_ERP_INUSE) != 0) {
1370 			/* LUN blocked:
1371 			 * not yet unblocked [LUN recovery pending]
1372 			 * or meanwhile blocked [new LUN recovery triggered]
1373 			 */
1374 			zfcp_dbf_rec_run_lvl(4, "ertru_l", &zsdev->erp_action);
1375 			spin_unlock(shost->host_lock);
1376 			write_unlock_irqrestore(&adapter->erp_lock, flags);
1377 			return;
1378 		}
1379 	}
1380 	/* now port has no child or all children have completed recovery,
1381 	 * and no ERP of severity >= port was meanwhile triggered elsewhere
1382 	 */
1383 	zfcp_scsi_schedule_rport_register(port);
1384 	spin_unlock(shost->host_lock);
1385 	write_unlock_irqrestore(&adapter->erp_lock, flags);
1386 }
1387 
1388 static void zfcp_erp_action_cleanup(struct zfcp_erp_action *act,
1389 				    enum zfcp_erp_act_result result)
1390 {
1391 	struct zfcp_adapter *adapter = act->adapter;
1392 	struct zfcp_port *port = act->port;
1393 	struct scsi_device *sdev = act->sdev;
1394 
1395 	switch (act->type) {
1396 	case ZFCP_ERP_ACTION_REOPEN_LUN:
1397 		if (!(act->status & ZFCP_STATUS_ERP_NO_REF))
1398 			scsi_device_put(sdev);
1399 		zfcp_erp_try_rport_unblock(port);
1400 		break;
1401 
1402 	case ZFCP_ERP_ACTION_REOPEN_PORT:
1403 		/* This switch case might also happen after a forced reopen
1404 		 * was successfully done and thus overwritten with a new
1405 		 * non-forced reopen at `ersfs_2'. In this case, we must not
1406 		 * do the clean-up of the non-forced version.
1407 		 */
1408 		if (act->step != ZFCP_ERP_STEP_UNINITIALIZED)
1409 			if (result == ZFCP_ERP_SUCCEEDED)
1410 				zfcp_erp_try_rport_unblock(port);
1411 		/* fall through */
1412 	case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
1413 		put_device(&port->dev);
1414 		break;
1415 
1416 	case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
1417 		if (result == ZFCP_ERP_SUCCEEDED) {
1418 			register_service_level(&adapter->service_level);
1419 			zfcp_fc_conditional_port_scan(adapter);
1420 			queue_work(adapter->work_queue, &adapter->ns_up_work);
1421 		} else
1422 			unregister_service_level(&adapter->service_level);
1423 
1424 		kref_put(&adapter->ref, zfcp_adapter_release);
1425 		break;
1426 	}
1427 }
1428 
1429 static enum zfcp_erp_act_result zfcp_erp_strategy_do_action(
1430 	struct zfcp_erp_action *erp_action)
1431 {
1432 	switch (erp_action->type) {
1433 	case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
1434 		return zfcp_erp_adapter_strategy(erp_action);
1435 	case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
1436 		return zfcp_erp_port_forced_strategy(erp_action);
1437 	case ZFCP_ERP_ACTION_REOPEN_PORT:
1438 		return zfcp_erp_port_strategy(erp_action);
1439 	case ZFCP_ERP_ACTION_REOPEN_LUN:
1440 		return zfcp_erp_lun_strategy(erp_action);
1441 	}
1442 	return ZFCP_ERP_FAILED;
1443 }
1444 
1445 static enum zfcp_erp_act_result zfcp_erp_strategy(
1446 	struct zfcp_erp_action *erp_action)
1447 {
1448 	enum zfcp_erp_act_result result;
1449 	unsigned long flags;
1450 	struct zfcp_adapter *adapter = erp_action->adapter;
1451 
1452 	kref_get(&adapter->ref);
1453 
1454 	write_lock_irqsave(&adapter->erp_lock, flags);
1455 	zfcp_erp_strategy_check_fsfreq(erp_action);
1456 
1457 	if (erp_action->status & ZFCP_STATUS_ERP_DISMISSED) {
1458 		zfcp_erp_action_dequeue(erp_action);
1459 		result = ZFCP_ERP_DISMISSED;
1460 		goto unlock;
1461 	}
1462 
1463 	if (erp_action->status & ZFCP_STATUS_ERP_TIMEDOUT) {
1464 		result = ZFCP_ERP_FAILED;
1465 		goto check_target;
1466 	}
1467 
1468 	zfcp_erp_action_to_running(erp_action);
1469 
1470 	/* no lock to allow for blocking operations */
1471 	write_unlock_irqrestore(&adapter->erp_lock, flags);
1472 	result = zfcp_erp_strategy_do_action(erp_action);
1473 	write_lock_irqsave(&adapter->erp_lock, flags);
1474 
1475 	if (erp_action->status & ZFCP_STATUS_ERP_DISMISSED)
1476 		result = ZFCP_ERP_CONTINUES;
1477 
1478 	switch (result) {
1479 	case ZFCP_ERP_NOMEM:
1480 		if (!(erp_action->status & ZFCP_STATUS_ERP_LOWMEM)) {
1481 			++adapter->erp_low_mem_count;
1482 			erp_action->status |= ZFCP_STATUS_ERP_LOWMEM;
1483 		}
1484 		if (adapter->erp_total_count == adapter->erp_low_mem_count)
1485 			_zfcp_erp_adapter_reopen(adapter, 0, "erstgy1");
1486 		else {
1487 			zfcp_erp_strategy_memwait(erp_action);
1488 			result = ZFCP_ERP_CONTINUES;
1489 		}
1490 		goto unlock;
1491 
1492 	case ZFCP_ERP_CONTINUES:
1493 		if (erp_action->status & ZFCP_STATUS_ERP_LOWMEM) {
1494 			--adapter->erp_low_mem_count;
1495 			erp_action->status &= ~ZFCP_STATUS_ERP_LOWMEM;
1496 		}
1497 		goto unlock;
1498 	case ZFCP_ERP_SUCCEEDED:
1499 	case ZFCP_ERP_FAILED:
1500 	case ZFCP_ERP_EXIT:
1501 	case ZFCP_ERP_DISMISSED:
1502 		/* NOP */
1503 		break;
1504 	}
1505 
1506 check_target:
1507 	result = zfcp_erp_strategy_check_target(erp_action, result);
1508 	zfcp_erp_action_dequeue(erp_action);
1509 	result = zfcp_erp_strategy_statechange(erp_action, result);
1510 	if (result == ZFCP_ERP_EXIT)
1511 		goto unlock;
1512 	if (result == ZFCP_ERP_SUCCEEDED)
1513 		zfcp_erp_strategy_followup_success(erp_action);
1514 	if (result == ZFCP_ERP_FAILED)
1515 		zfcp_erp_strategy_followup_failed(erp_action);
1516 
1517  unlock:
1518 	write_unlock_irqrestore(&adapter->erp_lock, flags);
1519 
1520 	if (result != ZFCP_ERP_CONTINUES)
1521 		zfcp_erp_action_cleanup(erp_action, result);
1522 
1523 	kref_put(&adapter->ref, zfcp_adapter_release);
1524 	return result;
1525 }
1526 
1527 static int zfcp_erp_thread(void *data)
1528 {
1529 	struct zfcp_adapter *adapter = (struct zfcp_adapter *) data;
1530 	struct list_head *next;
1531 	struct zfcp_erp_action *act;
1532 	unsigned long flags;
1533 
1534 	for (;;) {
1535 		wait_event_interruptible(adapter->erp_ready_wq,
1536 			   !list_empty(&adapter->erp_ready_head) ||
1537 			   kthread_should_stop());
1538 
1539 		if (kthread_should_stop())
1540 			break;
1541 
1542 		write_lock_irqsave(&adapter->erp_lock, flags);
1543 		next = adapter->erp_ready_head.next;
1544 		write_unlock_irqrestore(&adapter->erp_lock, flags);
1545 
1546 		if (next != &adapter->erp_ready_head) {
1547 			act = list_entry(next, struct zfcp_erp_action, list);
1548 
1549 			/* there is more to come after dismission, no notify */
1550 			if (zfcp_erp_strategy(act) != ZFCP_ERP_DISMISSED)
1551 				zfcp_erp_wakeup(adapter);
1552 		}
1553 	}
1554 
1555 	return 0;
1556 }
1557 
1558 /**
1559  * zfcp_erp_thread_setup - Start ERP thread for adapter
1560  * @adapter: Adapter to start the ERP thread for
1561  *
1562  * Return: 0 on success, or error code from kthread_run().
1563  */
1564 int zfcp_erp_thread_setup(struct zfcp_adapter *adapter)
1565 {
1566 	struct task_struct *thread;
1567 
1568 	thread = kthread_run(zfcp_erp_thread, adapter, "zfcperp%s",
1569 			     dev_name(&adapter->ccw_device->dev));
1570 	if (IS_ERR(thread)) {
1571 		dev_err(&adapter->ccw_device->dev,
1572 			"Creating an ERP thread for the FCP device failed.\n");
1573 		return PTR_ERR(thread);
1574 	}
1575 
1576 	adapter->erp_thread = thread;
1577 	return 0;
1578 }
1579 
1580 /**
1581  * zfcp_erp_thread_kill - Stop ERP thread.
1582  * @adapter: Adapter where the ERP thread should be stopped.
1583  *
1584  * The caller of this routine ensures that the specified adapter has
1585  * been shut down and that this operation has been completed. Thus,
1586  * there are no pending erp_actions which would need to be handled
1587  * here.
1588  */
1589 void zfcp_erp_thread_kill(struct zfcp_adapter *adapter)
1590 {
1591 	kthread_stop(adapter->erp_thread);
1592 	adapter->erp_thread = NULL;
1593 	WARN_ON(!list_empty(&adapter->erp_ready_head));
1594 	WARN_ON(!list_empty(&adapter->erp_running_head));
1595 }
1596 
1597 /**
1598  * zfcp_erp_wait - wait for completion of error recovery on an adapter
1599  * @adapter: adapter for which to wait for completion of its error recovery
1600  */
1601 void zfcp_erp_wait(struct zfcp_adapter *adapter)
1602 {
1603 	wait_event(adapter->erp_done_wqh,
1604 		   !(atomic_read(&adapter->status) &
1605 			ZFCP_STATUS_ADAPTER_ERP_PENDING));
1606 }
1607 
1608 /**
1609  * zfcp_erp_set_adapter_status - set adapter status bits
1610  * @adapter: adapter to change the status
1611  * @mask: status bits to change
1612  *
1613  * Changes in common status bits are propagated to attached ports and LUNs.
1614  */
1615 void zfcp_erp_set_adapter_status(struct zfcp_adapter *adapter, u32 mask)
1616 {
1617 	struct zfcp_port *port;
1618 	struct scsi_device *sdev;
1619 	unsigned long flags;
1620 	u32 common_mask = mask & ZFCP_COMMON_FLAGS;
1621 
1622 	atomic_or(mask, &adapter->status);
1623 
1624 	if (!common_mask)
1625 		return;
1626 
1627 	read_lock_irqsave(&adapter->port_list_lock, flags);
1628 	list_for_each_entry(port, &adapter->port_list, list)
1629 		atomic_or(common_mask, &port->status);
1630 	read_unlock_irqrestore(&adapter->port_list_lock, flags);
1631 
1632 	spin_lock_irqsave(adapter->scsi_host->host_lock, flags);
1633 	__shost_for_each_device(sdev, adapter->scsi_host)
1634 		atomic_or(common_mask, &sdev_to_zfcp(sdev)->status);
1635 	spin_unlock_irqrestore(adapter->scsi_host->host_lock, flags);
1636 }
1637 
1638 /**
1639  * zfcp_erp_clear_adapter_status - clear adapter status bits
1640  * @adapter: adapter to change the status
1641  * @mask: status bits to change
1642  *
1643  * Changes in common status bits are propagated to attached ports and LUNs.
1644  */
1645 void zfcp_erp_clear_adapter_status(struct zfcp_adapter *adapter, u32 mask)
1646 {
1647 	struct zfcp_port *port;
1648 	struct scsi_device *sdev;
1649 	unsigned long flags;
1650 	u32 common_mask = mask & ZFCP_COMMON_FLAGS;
1651 	u32 clear_counter = mask & ZFCP_STATUS_COMMON_ERP_FAILED;
1652 
1653 	atomic_andnot(mask, &adapter->status);
1654 
1655 	if (!common_mask)
1656 		return;
1657 
1658 	if (clear_counter)
1659 		atomic_set(&adapter->erp_counter, 0);
1660 
1661 	read_lock_irqsave(&adapter->port_list_lock, flags);
1662 	list_for_each_entry(port, &adapter->port_list, list) {
1663 		atomic_andnot(common_mask, &port->status);
1664 		if (clear_counter)
1665 			atomic_set(&port->erp_counter, 0);
1666 	}
1667 	read_unlock_irqrestore(&adapter->port_list_lock, flags);
1668 
1669 	spin_lock_irqsave(adapter->scsi_host->host_lock, flags);
1670 	__shost_for_each_device(sdev, adapter->scsi_host) {
1671 		atomic_andnot(common_mask, &sdev_to_zfcp(sdev)->status);
1672 		if (clear_counter)
1673 			atomic_set(&sdev_to_zfcp(sdev)->erp_counter, 0);
1674 	}
1675 	spin_unlock_irqrestore(adapter->scsi_host->host_lock, flags);
1676 }
1677 
1678 /**
1679  * zfcp_erp_set_port_status - set port status bits
1680  * @port: port to change the status
1681  * @mask: status bits to change
1682  *
1683  * Changes in common status bits are propagated to attached LUNs.
1684  */
1685 void zfcp_erp_set_port_status(struct zfcp_port *port, u32 mask)
1686 {
1687 	struct scsi_device *sdev;
1688 	u32 common_mask = mask & ZFCP_COMMON_FLAGS;
1689 	unsigned long flags;
1690 
1691 	atomic_or(mask, &port->status);
1692 
1693 	if (!common_mask)
1694 		return;
1695 
1696 	spin_lock_irqsave(port->adapter->scsi_host->host_lock, flags);
1697 	__shost_for_each_device(sdev, port->adapter->scsi_host)
1698 		if (sdev_to_zfcp(sdev)->port == port)
1699 			atomic_or(common_mask,
1700 					&sdev_to_zfcp(sdev)->status);
1701 	spin_unlock_irqrestore(port->adapter->scsi_host->host_lock, flags);
1702 }
1703 
1704 /**
1705  * zfcp_erp_clear_port_status - clear port status bits
1706  * @port: adapter to change the status
1707  * @mask: status bits to change
1708  *
1709  * Changes in common status bits are propagated to attached LUNs.
1710  */
1711 void zfcp_erp_clear_port_status(struct zfcp_port *port, u32 mask)
1712 {
1713 	struct scsi_device *sdev;
1714 	u32 common_mask = mask & ZFCP_COMMON_FLAGS;
1715 	u32 clear_counter = mask & ZFCP_STATUS_COMMON_ERP_FAILED;
1716 	unsigned long flags;
1717 
1718 	atomic_andnot(mask, &port->status);
1719 
1720 	if (!common_mask)
1721 		return;
1722 
1723 	if (clear_counter)
1724 		atomic_set(&port->erp_counter, 0);
1725 
1726 	spin_lock_irqsave(port->adapter->scsi_host->host_lock, flags);
1727 	__shost_for_each_device(sdev, port->adapter->scsi_host)
1728 		if (sdev_to_zfcp(sdev)->port == port) {
1729 			atomic_andnot(common_mask,
1730 					  &sdev_to_zfcp(sdev)->status);
1731 			if (clear_counter)
1732 				atomic_set(&sdev_to_zfcp(sdev)->erp_counter, 0);
1733 		}
1734 	spin_unlock_irqrestore(port->adapter->scsi_host->host_lock, flags);
1735 }
1736 
1737 /**
1738  * zfcp_erp_set_lun_status - set lun status bits
1739  * @sdev: SCSI device / lun to set the status bits
1740  * @mask: status bits to change
1741  */
1742 void zfcp_erp_set_lun_status(struct scsi_device *sdev, u32 mask)
1743 {
1744 	struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
1745 
1746 	atomic_or(mask, &zfcp_sdev->status);
1747 }
1748 
1749 /**
1750  * zfcp_erp_clear_lun_status - clear lun status bits
1751  * @sdev: SCSi device / lun to clear the status bits
1752  * @mask: status bits to change
1753  */
1754 void zfcp_erp_clear_lun_status(struct scsi_device *sdev, u32 mask)
1755 {
1756 	struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
1757 
1758 	atomic_andnot(mask, &zfcp_sdev->status);
1759 
1760 	if (mask & ZFCP_STATUS_COMMON_ERP_FAILED)
1761 		atomic_set(&zfcp_sdev->erp_counter, 0);
1762 }
1763 
1764 /**
1765  * zfcp_erp_adapter_reset_sync() - Really reopen adapter and wait.
1766  * @adapter: Pointer to zfcp_adapter to reopen.
1767  * @dbftag: Trace tag string of length %ZFCP_DBF_TAG_LEN.
1768  */
1769 void zfcp_erp_adapter_reset_sync(struct zfcp_adapter *adapter, char *dbftag)
1770 {
1771 	zfcp_erp_set_adapter_status(adapter, ZFCP_STATUS_COMMON_RUNNING);
1772 	zfcp_erp_adapter_reopen(adapter, ZFCP_STATUS_COMMON_ERP_FAILED, dbftag);
1773 	zfcp_erp_wait(adapter);
1774 }
1775