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