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