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