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