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