4 * Error Recovery Procedures (ERP).
6 * Copyright IBM Corp. 2002, 2010
9 #define KMSG_COMPONENT "zfcp"
10 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
12 #include <linux/kthread.h>
14 #include "zfcp_reqlist.h"
16 #define ZFCP_MAX_ERPS 3
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,
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,
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,
44 enum zfcp_erp_act_state
{
45 ZFCP_ERP_ACTION_RUNNING
= 1,
46 ZFCP_ERP_ACTION_READY
= 2,
49 enum zfcp_erp_act_result
{
50 ZFCP_ERP_SUCCEEDED
= 0,
52 ZFCP_ERP_CONTINUES
= 2,
54 ZFCP_ERP_DISMISSED
= 4,
58 static void zfcp_erp_adapter_block(struct zfcp_adapter
*adapter
, int mask
)
60 zfcp_erp_clear_adapter_status(adapter
,
61 ZFCP_STATUS_COMMON_UNBLOCKED
| mask
);
64 static int zfcp_erp_action_exists(struct zfcp_erp_action
*act
)
66 struct zfcp_erp_action
*curr_act
;
68 list_for_each_entry(curr_act
, &act
->adapter
->erp_running_head
, list
)
70 return ZFCP_ERP_ACTION_RUNNING
;
74 static void zfcp_erp_action_ready(struct zfcp_erp_action
*act
)
76 struct zfcp_adapter
*adapter
= act
->adapter
;
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
);
84 static void zfcp_erp_action_dismiss(struct zfcp_erp_action
*act
)
86 act
->status
|= ZFCP_STATUS_ERP_DISMISSED
;
87 if (zfcp_erp_action_exists(act
) == ZFCP_ERP_ACTION_RUNNING
)
88 zfcp_erp_action_ready(act
);
91 static void zfcp_erp_action_dismiss_lun(struct scsi_device
*sdev
)
93 struct zfcp_scsi_dev
*zfcp_sdev
= sdev_to_zfcp(sdev
);
95 if (atomic_read(&zfcp_sdev
->status
) & ZFCP_STATUS_COMMON_ERP_INUSE
)
96 zfcp_erp_action_dismiss(&zfcp_sdev
->erp_action
);
99 static void zfcp_erp_action_dismiss_port(struct zfcp_port
*port
)
101 struct scsi_device
*sdev
;
103 if (atomic_read(&port
->status
) & ZFCP_STATUS_COMMON_ERP_INUSE
)
104 zfcp_erp_action_dismiss(&port
->erp_action
);
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
);
114 static void zfcp_erp_action_dismiss_adapter(struct zfcp_adapter
*adapter
)
116 struct zfcp_port
*port
;
118 if (atomic_read(&adapter
->status
) & ZFCP_STATUS_COMMON_ERP_INUSE
)
119 zfcp_erp_action_dismiss(&adapter
->erp_action
);
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
);
128 static int zfcp_erp_required_act(int want
, struct zfcp_adapter
*adapter
,
129 struct zfcp_port
*port
,
130 struct scsi_device
*sdev
)
133 int l_status
, p_status
, a_status
;
134 struct zfcp_scsi_dev
*zfcp_sdev
;
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
)
142 p_status
= atomic_read(&port
->status
);
143 if (!(p_status
& ZFCP_STATUS_COMMON_RUNNING
) ||
144 p_status
& ZFCP_STATUS_COMMON_ERP_FAILED
)
146 if (!(p_status
& ZFCP_STATUS_COMMON_UNBLOCKED
))
147 need
= ZFCP_ERP_ACTION_REOPEN_PORT
;
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
;
154 case ZFCP_ERP_ACTION_REOPEN_PORT
:
155 p_status
= atomic_read(&port
->status
);
156 if (p_status
& ZFCP_STATUS_COMMON_ERP_INUSE
)
158 a_status
= atomic_read(&adapter
->status
);
159 if (!(a_status
& ZFCP_STATUS_COMMON_RUNNING
) ||
160 a_status
& ZFCP_STATUS_COMMON_ERP_FAILED
)
162 if (p_status
& ZFCP_STATUS_COMMON_NOESC
)
164 if (!(a_status
& ZFCP_STATUS_COMMON_UNBLOCKED
))
165 need
= ZFCP_ERP_ACTION_REOPEN_ADAPTER
;
167 case ZFCP_ERP_ACTION_REOPEN_ADAPTER
:
168 a_status
= atomic_read(&adapter
->status
);
169 if (a_status
& ZFCP_STATUS_COMMON_ERP_INUSE
)
171 if (!(a_status
& ZFCP_STATUS_COMMON_RUNNING
) &&
172 !(a_status
& ZFCP_STATUS_COMMON_OPEN
))
173 return 0; /* shutdown requested for closed adapter */
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
)
184 struct zfcp_erp_action
*erp_action
;
185 struct zfcp_scsi_dev
*zfcp_sdev
;
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
))
193 atomic_set_mask(ZFCP_STATUS_COMMON_ERP_INUSE
,
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
;
204 case ZFCP_ERP_ACTION_REOPEN_PORT
:
205 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED
:
206 if (!get_device(&port
->dev
))
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
;
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
;
232 erp_action
->adapter
= adapter
;
233 erp_action
->action
= need
;
234 erp_action
->status
= 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
)
244 int retval
= 1, need
;
245 struct zfcp_erp_action
*act
;
247 if (!adapter
->erp_thread
)
250 need
= zfcp_erp_required_act(want
, adapter
, port
, sdev
);
254 act
= zfcp_erp_setup_act(need
, act_status
, adapter
, port
, sdev
);
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
);
263 zfcp_dbf_rec_trig(id
, adapter
, port
, sdev
, want
, need
);
267 static int _zfcp_erp_adapter_reopen(struct zfcp_adapter
*adapter
,
268 int clear_mask
, char *id
)
270 zfcp_erp_adapter_block(adapter
, clear_mask
);
271 zfcp_scsi_schedule_rports_block(adapter
);
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
);
279 return zfcp_erp_action_enqueue(ZFCP_ERP_ACTION_REOPEN_ADAPTER
,
280 adapter
, NULL
, NULL
, id
, 0);
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.
289 void zfcp_erp_adapter_reopen(struct zfcp_adapter
*adapter
, int clear
, char *id
)
293 zfcp_erp_adapter_block(adapter
, clear
);
294 zfcp_scsi_schedule_rports_block(adapter
);
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
);
301 zfcp_erp_action_enqueue(ZFCP_ERP_ACTION_REOPEN_ADAPTER
, adapter
,
303 write_unlock_irqrestore(&adapter
->erp_lock
, flags
);
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.
312 void zfcp_erp_adapter_shutdown(struct zfcp_adapter
*adapter
, int clear
,
315 int flags
= ZFCP_STATUS_COMMON_RUNNING
| ZFCP_STATUS_COMMON_ERP_FAILED
;
316 zfcp_erp_adapter_reopen(adapter
, clear
| flags
, id
);
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.
325 void zfcp_erp_port_shutdown(struct zfcp_port
*port
, int clear
, char *id
)
327 int flags
= ZFCP_STATUS_COMMON_RUNNING
| ZFCP_STATUS_COMMON_ERP_FAILED
;
328 zfcp_erp_port_reopen(port
, clear
| flags
, id
);
331 static void zfcp_erp_port_block(struct zfcp_port
*port
, int clear
)
333 zfcp_erp_clear_port_status(port
,
334 ZFCP_STATUS_COMMON_UNBLOCKED
| clear
);
337 static void _zfcp_erp_port_forced_reopen(struct zfcp_port
*port
, int clear
,
340 zfcp_erp_port_block(port
, clear
);
341 zfcp_scsi_schedule_rport_block(port
);
343 if (atomic_read(&port
->status
) & ZFCP_STATUS_COMMON_ERP_FAILED
)
346 zfcp_erp_action_enqueue(ZFCP_ERP_ACTION_REOPEN_PORT_FORCED
,
347 port
->adapter
, port
, NULL
, id
, 0);
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.
356 void zfcp_erp_port_forced_reopen(struct zfcp_port
*port
, int clear
, char *id
)
359 struct zfcp_adapter
*adapter
= port
->adapter
;
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
);
366 static int _zfcp_erp_port_reopen(struct zfcp_port
*port
, int clear
, char *id
)
368 zfcp_erp_port_block(port
, clear
);
369 zfcp_scsi_schedule_rport_block(port
);
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
);
377 return zfcp_erp_action_enqueue(ZFCP_ERP_ACTION_REOPEN_PORT
,
378 port
->adapter
, port
, NULL
, id
, 0);
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.
387 * Returns 0 if recovery has been triggered, < 0 if not.
389 int zfcp_erp_port_reopen(struct zfcp_port
*port
, int clear
, char *id
)
393 struct zfcp_adapter
*adapter
= port
->adapter
;
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
);
402 static void zfcp_erp_lun_block(struct scsi_device
*sdev
, int clear_mask
)
404 zfcp_erp_clear_lun_status(sdev
,
405 ZFCP_STATUS_COMMON_UNBLOCKED
| clear_mask
);
408 static void _zfcp_erp_lun_reopen(struct scsi_device
*sdev
, int clear
, char *id
,
411 struct zfcp_scsi_dev
*zfcp_sdev
= sdev_to_zfcp(sdev
);
412 struct zfcp_adapter
*adapter
= zfcp_sdev
->port
->adapter
;
414 zfcp_erp_lun_block(sdev
, clear
);
416 if (atomic_read(&zfcp_sdev
->status
) & ZFCP_STATUS_COMMON_ERP_FAILED
)
419 zfcp_erp_action_enqueue(ZFCP_ERP_ACTION_REOPEN_LUN
, adapter
,
420 zfcp_sdev
->port
, sdev
, id
, act_status
);
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.
429 * Return: 0 on success, < 0 on error
431 void zfcp_erp_lun_reopen(struct scsi_device
*sdev
, int clear
, char *id
)
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
;
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
);
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.
449 void zfcp_erp_lun_shutdown(struct scsi_device
*sdev
, int clear
, char *id
)
451 int flags
= ZFCP_STATUS_COMMON_RUNNING
| ZFCP_STATUS_COMMON_ERP_FAILED
;
452 zfcp_erp_lun_reopen(sdev
, clear
| flags
, id
);
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.
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.
465 void zfcp_erp_lun_shutdown_wait(struct scsi_device
*sdev
, char *id
)
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
;
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
);
477 zfcp_erp_wait(adapter
);
480 static int status_change_set(unsigned long mask
, atomic_t
*status
)
482 return (atomic_read(status
) ^ mask
) & mask
;
485 static void zfcp_erp_adapter_unblock(struct zfcp_adapter
*adapter
)
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
);
492 static void zfcp_erp_port_unblock(struct zfcp_port
*port
)
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
);
499 static void zfcp_erp_lun_unblock(struct scsi_device
*sdev
)
501 struct zfcp_scsi_dev
*zfcp_sdev
= sdev_to_zfcp(sdev
);
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
);
508 static void zfcp_erp_action_to_running(struct zfcp_erp_action
*erp_action
)
510 list_move(&erp_action
->list
, &erp_action
->adapter
->erp_running_head
);
511 zfcp_dbf_rec_run("erator1", erp_action
);
514 static void zfcp_erp_strategy_check_fsfreq(struct zfcp_erp_action
*act
)
516 struct zfcp_adapter
*adapter
= act
->adapter
;
517 struct zfcp_fsf_req
*req
;
519 if (!act
->fsf_req_id
)
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
;
531 if (act
->status
& ZFCP_STATUS_ERP_TIMEDOUT
)
532 zfcp_dbf_rec_run("erscf_2", act
);
533 if (req
->status
& ZFCP_STATUS_FSFREQ_DISMISSED
)
537 spin_unlock(&adapter
->req_list
->lock
);
541 * zfcp_erp_notify - Trigger ERP action.
542 * @erp_action: ERP action to continue.
543 * @set_mask: ERP action status flags to set.
545 void zfcp_erp_notify(struct zfcp_erp_action
*erp_action
, unsigned long set_mask
)
547 struct zfcp_adapter
*adapter
= erp_action
->adapter
;
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
);
555 write_unlock_irqrestore(&adapter
->erp_lock
, flags
);
559 * zfcp_erp_timeout_handler - Trigger ERP action from timed out ERP request
560 * @data: ERP action (from timer data)
562 void zfcp_erp_timeout_handler(unsigned long data
)
564 struct zfcp_erp_action
*act
= (struct zfcp_erp_action
*) data
;
565 zfcp_erp_notify(act
, ZFCP_STATUS_ERP_TIMEDOUT
);
568 static void zfcp_erp_memwait_handler(unsigned long data
)
570 zfcp_erp_notify((struct zfcp_erp_action
*)data
, 0);
573 static void zfcp_erp_strategy_memwait(struct zfcp_erp_action
*erp_action
)
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
);
582 static void _zfcp_erp_port_reopen_all(struct zfcp_adapter
*adapter
,
585 struct zfcp_port
*port
;
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
);
593 static void _zfcp_erp_lun_reopen_all(struct zfcp_port
*port
, int clear
,
596 struct scsi_device
*sdev
;
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
);
605 static void zfcp_erp_strategy_followup_failed(struct zfcp_erp_action
*act
)
607 switch (act
->action
) {
608 case ZFCP_ERP_ACTION_REOPEN_ADAPTER
:
609 _zfcp_erp_adapter_reopen(act
->adapter
, 0, "ersff_1");
611 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED
:
612 _zfcp_erp_port_forced_reopen(act
->port
, 0, "ersff_2");
614 case ZFCP_ERP_ACTION_REOPEN_PORT
:
615 _zfcp_erp_port_reopen(act
->port
, 0, "ersff_3");
617 case ZFCP_ERP_ACTION_REOPEN_LUN
:
618 _zfcp_erp_lun_reopen(act
->sdev
, 0, "ersff_4", 0);
623 static void zfcp_erp_strategy_followup_success(struct zfcp_erp_action
*act
)
625 switch (act
->action
) {
626 case ZFCP_ERP_ACTION_REOPEN_ADAPTER
:
627 _zfcp_erp_port_reopen_all(act
->adapter
, 0, "ersfs_1");
629 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED
:
630 _zfcp_erp_port_reopen(act
->port
, 0, "ersfs_2");
632 case ZFCP_ERP_ACTION_REOPEN_PORT
:
633 _zfcp_erp_lun_reopen_all(act
->port
, 0, "ersfs_3");
638 static void zfcp_erp_wakeup(struct zfcp_adapter
*adapter
)
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
,
647 wake_up(&adapter
->erp_done_wqh
);
649 read_unlock_irqrestore(&adapter
->erp_lock
, flags
);
652 static void zfcp_erp_enqueue_ptp_port(struct zfcp_adapter
*adapter
)
654 struct zfcp_port
*port
;
655 port
= zfcp_port_enqueue(adapter
, adapter
->peer_wwpn
, 0,
657 if (IS_ERR(port
)) /* error or port already attached */
659 _zfcp_erp_port_reopen(port
, 0, "ereptp1");
662 static int zfcp_erp_adapter_strat_fsf_xconf(struct zfcp_erp_action
*erp_action
)
666 struct zfcp_adapter
*adapter
= erp_action
->adapter
;
668 atomic_clear_mask(ZFCP_STATUS_ADAPTER_XCONFIG_OK
, &adapter
->status
);
670 for (retries
= 7; retries
; retries
--) {
671 atomic_clear_mask(ZFCP_STATUS_ADAPTER_HOST_CON_INIT
,
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
,
679 return ZFCP_ERP_FAILED
;
682 wait_event(adapter
->erp_ready_wq
,
683 !list_empty(&adapter
->erp_ready_head
));
684 if (erp_action
->status
& ZFCP_STATUS_ERP_TIMEDOUT
)
687 if (!(atomic_read(&adapter
->status
) &
688 ZFCP_STATUS_ADAPTER_HOST_CON_INIT
))
695 atomic_clear_mask(ZFCP_STATUS_ADAPTER_HOST_CON_INIT
,
698 if (!(atomic_read(&adapter
->status
) & ZFCP_STATUS_ADAPTER_XCONFIG_OK
))
699 return ZFCP_ERP_FAILED
;
701 if (fc_host_port_type(adapter
->scsi_host
) == FC_PORTTYPE_PTP
)
702 zfcp_erp_enqueue_ptp_port(adapter
);
704 return ZFCP_ERP_SUCCEEDED
;
707 static int zfcp_erp_adapter_strategy_open_fsf_xport(struct zfcp_erp_action
*act
)
710 struct zfcp_adapter
*adapter
= act
->adapter
;
712 write_lock_irq(&adapter
->erp_lock
);
713 zfcp_erp_action_to_running(act
);
714 write_unlock_irq(&adapter
->erp_lock
);
716 ret
= zfcp_fsf_exchange_port_data(act
);
717 if (ret
== -EOPNOTSUPP
)
718 return ZFCP_ERP_SUCCEEDED
;
720 return ZFCP_ERP_FAILED
;
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
;
729 return ZFCP_ERP_SUCCEEDED
;
732 static int zfcp_erp_adapter_strategy_open_fsf(struct zfcp_erp_action
*act
)
734 if (zfcp_erp_adapter_strat_fsf_xconf(act
) == ZFCP_ERP_FAILED
)
735 return ZFCP_ERP_FAILED
;
737 if (zfcp_erp_adapter_strategy_open_fsf_xport(act
) == ZFCP_ERP_FAILED
)
738 return ZFCP_ERP_FAILED
;
740 if (mempool_resize(act
->adapter
->pool
.sr_data
,
741 act
->adapter
->stat_read_buf_num
))
742 return ZFCP_ERP_FAILED
;
744 if (mempool_resize(act
->adapter
->pool
.status_read_req
,
745 act
->adapter
->stat_read_buf_num
))
746 return ZFCP_ERP_FAILED
;
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
;
752 return ZFCP_ERP_SUCCEEDED
;
755 static void zfcp_erp_adapter_strategy_close(struct zfcp_erp_action
*act
)
757 struct zfcp_adapter
*adapter
= act
->adapter
;
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
);
767 atomic_clear_mask(ZFCP_STATUS_ADAPTER_XCONFIG_OK
|
768 ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED
, &adapter
->status
);
771 static int zfcp_erp_adapter_strategy_open(struct zfcp_erp_action
*act
)
773 struct zfcp_adapter
*adapter
= act
->adapter
;
775 if (zfcp_qdio_open(adapter
->qdio
)) {
776 atomic_clear_mask(ZFCP_STATUS_ADAPTER_XCONFIG_OK
|
777 ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED
,
779 return ZFCP_ERP_FAILED
;
782 if (zfcp_erp_adapter_strategy_open_fsf(act
)) {
783 zfcp_erp_adapter_strategy_close(act
);
784 return ZFCP_ERP_FAILED
;
787 atomic_set_mask(ZFCP_STATUS_COMMON_OPEN
, &adapter
->status
);
789 return ZFCP_ERP_SUCCEEDED
;
792 static int zfcp_erp_adapter_strategy(struct zfcp_erp_action
*act
)
794 struct zfcp_adapter
*adapter
= act
->adapter
;
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
;
802 if (zfcp_erp_adapter_strategy_open(act
)) {
804 return ZFCP_ERP_FAILED
;
807 return ZFCP_ERP_SUCCEEDED
;
810 static int zfcp_erp_port_forced_strategy_close(struct zfcp_erp_action
*act
)
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
;
819 return ZFCP_ERP_FAILED
;
821 return ZFCP_ERP_CONTINUES
;
824 static int zfcp_erp_port_forced_strategy(struct zfcp_erp_action
*erp_action
)
826 struct zfcp_port
*port
= erp_action
->port
;
827 int status
= atomic_read(&port
->status
);
829 switch (erp_action
->step
) {
830 case ZFCP_ERP_STEP_UNINITIALIZED
:
831 if ((status
& ZFCP_STATUS_PORT_PHYS_OPEN
) &&
832 (status
& ZFCP_STATUS_COMMON_OPEN
))
833 return zfcp_erp_port_forced_strategy_close(erp_action
);
835 return ZFCP_ERP_FAILED
;
837 case ZFCP_ERP_STEP_PHYS_PORT_CLOSING
:
838 if (!(status
& ZFCP_STATUS_PORT_PHYS_OPEN
))
839 return ZFCP_ERP_SUCCEEDED
;
841 return ZFCP_ERP_FAILED
;
844 static int zfcp_erp_port_strategy_close(struct zfcp_erp_action
*erp_action
)
848 retval
= zfcp_fsf_close_port(erp_action
);
849 if (retval
== -ENOMEM
)
850 return ZFCP_ERP_NOMEM
;
851 erp_action
->step
= ZFCP_ERP_STEP_PORT_CLOSING
;
853 return ZFCP_ERP_FAILED
;
854 return ZFCP_ERP_CONTINUES
;
857 static int zfcp_erp_port_strategy_open_port(struct zfcp_erp_action
*erp_action
)
861 retval
= zfcp_fsf_open_port(erp_action
);
862 if (retval
== -ENOMEM
)
863 return ZFCP_ERP_NOMEM
;
864 erp_action
->step
= ZFCP_ERP_STEP_PORT_OPENING
;
866 return ZFCP_ERP_FAILED
;
867 return ZFCP_ERP_CONTINUES
;
870 static int zfcp_erp_open_ptp_port(struct zfcp_erp_action
*act
)
872 struct zfcp_adapter
*adapter
= act
->adapter
;
873 struct zfcp_port
*port
= act
->port
;
875 if (port
->wwpn
!= adapter
->peer_wwpn
) {
876 zfcp_erp_set_port_status(port
, ZFCP_STATUS_COMMON_ERP_FAILED
);
877 return ZFCP_ERP_FAILED
;
879 port
->d_id
= adapter
->peer_d_id
;
880 return zfcp_erp_port_strategy_open_port(act
);
883 static int zfcp_erp_port_strategy_open_common(struct zfcp_erp_action
*act
)
885 struct zfcp_adapter
*adapter
= act
->adapter
;
886 struct zfcp_port
*port
= act
->port
;
887 int p_status
= atomic_read(&port
->status
);
890 case ZFCP_ERP_STEP_UNINITIALIZED
:
891 case ZFCP_ERP_STEP_PHYS_PORT_CLOSING
:
892 case ZFCP_ERP_STEP_PORT_CLOSING
:
893 if (fc_host_port_type(adapter
->scsi_host
) == FC_PORTTYPE_PTP
)
894 return zfcp_erp_open_ptp_port(act
);
896 zfcp_fc_trigger_did_lookup(port
);
897 return ZFCP_ERP_EXIT
;
899 return zfcp_erp_port_strategy_open_port(act
);
901 case ZFCP_ERP_STEP_PORT_OPENING
:
902 /* D_ID might have changed during open */
903 if (p_status
& ZFCP_STATUS_COMMON_OPEN
) {
905 zfcp_fc_trigger_did_lookup(port
);
906 return ZFCP_ERP_EXIT
;
908 return ZFCP_ERP_SUCCEEDED
;
910 if (port
->d_id
&& !(p_status
& ZFCP_STATUS_COMMON_NOESC
)) {
912 return ZFCP_ERP_FAILED
;
914 /* fall through otherwise */
916 return ZFCP_ERP_FAILED
;
919 static int zfcp_erp_port_strategy(struct zfcp_erp_action
*erp_action
)
921 struct zfcp_port
*port
= erp_action
->port
;
922 int p_status
= atomic_read(&port
->status
);
924 if ((p_status
& ZFCP_STATUS_COMMON_NOESC
) &&
925 !(p_status
& ZFCP_STATUS_COMMON_OPEN
))
926 goto close_init_done
;
928 switch (erp_action
->step
) {
929 case ZFCP_ERP_STEP_UNINITIALIZED
:
930 if (p_status
& ZFCP_STATUS_COMMON_OPEN
)
931 return zfcp_erp_port_strategy_close(erp_action
);
934 case ZFCP_ERP_STEP_PORT_CLOSING
:
935 if (p_status
& ZFCP_STATUS_COMMON_OPEN
)
936 return ZFCP_ERP_FAILED
;
941 if (erp_action
->status
& ZFCP_STATUS_ERP_CLOSE_ONLY
)
942 return ZFCP_ERP_EXIT
;
944 return zfcp_erp_port_strategy_open_common(erp_action
);
947 static void zfcp_erp_lun_strategy_clearstati(struct scsi_device
*sdev
)
949 struct zfcp_scsi_dev
*zfcp_sdev
= sdev_to_zfcp(sdev
);
951 atomic_clear_mask(ZFCP_STATUS_COMMON_ACCESS_DENIED
,
955 static int zfcp_erp_lun_strategy_close(struct zfcp_erp_action
*erp_action
)
957 int retval
= zfcp_fsf_close_lun(erp_action
);
958 if (retval
== -ENOMEM
)
959 return ZFCP_ERP_NOMEM
;
960 erp_action
->step
= ZFCP_ERP_STEP_LUN_CLOSING
;
962 return ZFCP_ERP_FAILED
;
963 return ZFCP_ERP_CONTINUES
;
966 static int zfcp_erp_lun_strategy_open(struct zfcp_erp_action
*erp_action
)
968 int retval
= zfcp_fsf_open_lun(erp_action
);
969 if (retval
== -ENOMEM
)
970 return ZFCP_ERP_NOMEM
;
971 erp_action
->step
= ZFCP_ERP_STEP_LUN_OPENING
;
973 return ZFCP_ERP_FAILED
;
974 return ZFCP_ERP_CONTINUES
;
977 static int zfcp_erp_lun_strategy(struct zfcp_erp_action
*erp_action
)
979 struct scsi_device
*sdev
= erp_action
->sdev
;
980 struct zfcp_scsi_dev
*zfcp_sdev
= sdev_to_zfcp(sdev
);
982 switch (erp_action
->step
) {
983 case ZFCP_ERP_STEP_UNINITIALIZED
:
984 zfcp_erp_lun_strategy_clearstati(sdev
);
985 if (atomic_read(&zfcp_sdev
->status
) & ZFCP_STATUS_COMMON_OPEN
)
986 return zfcp_erp_lun_strategy_close(erp_action
);
987 /* already closed, fall through */
988 case ZFCP_ERP_STEP_LUN_CLOSING
:
989 if (atomic_read(&zfcp_sdev
->status
) & ZFCP_STATUS_COMMON_OPEN
)
990 return ZFCP_ERP_FAILED
;
991 if (erp_action
->status
& ZFCP_STATUS_ERP_CLOSE_ONLY
)
992 return ZFCP_ERP_EXIT
;
993 return zfcp_erp_lun_strategy_open(erp_action
);
995 case ZFCP_ERP_STEP_LUN_OPENING
:
996 if (atomic_read(&zfcp_sdev
->status
) & ZFCP_STATUS_COMMON_OPEN
)
997 return ZFCP_ERP_SUCCEEDED
;
999 return ZFCP_ERP_FAILED
;
1002 static int zfcp_erp_strategy_check_lun(struct scsi_device
*sdev
, int result
)
1004 struct zfcp_scsi_dev
*zfcp_sdev
= sdev_to_zfcp(sdev
);
1007 case ZFCP_ERP_SUCCEEDED
:
1008 atomic_set(&zfcp_sdev
->erp_counter
, 0);
1009 zfcp_erp_lun_unblock(sdev
);
1011 case ZFCP_ERP_FAILED
:
1012 atomic_inc(&zfcp_sdev
->erp_counter
);
1013 if (atomic_read(&zfcp_sdev
->erp_counter
) > ZFCP_MAX_ERPS
) {
1014 dev_err(&zfcp_sdev
->port
->adapter
->ccw_device
->dev
,
1015 "ERP failed for LUN 0x%016Lx on "
1017 (unsigned long long)zfcp_scsi_dev_lun(sdev
),
1018 (unsigned long long)zfcp_sdev
->port
->wwpn
);
1019 zfcp_erp_set_lun_status(sdev
,
1020 ZFCP_STATUS_COMMON_ERP_FAILED
);
1025 if (atomic_read(&zfcp_sdev
->status
) & ZFCP_STATUS_COMMON_ERP_FAILED
) {
1026 zfcp_erp_lun_block(sdev
, 0);
1027 result
= ZFCP_ERP_EXIT
;
1032 static int zfcp_erp_strategy_check_port(struct zfcp_port
*port
, int result
)
1035 case ZFCP_ERP_SUCCEEDED
:
1036 atomic_set(&port
->erp_counter
, 0);
1037 zfcp_erp_port_unblock(port
);
1040 case ZFCP_ERP_FAILED
:
1041 if (atomic_read(&port
->status
) & ZFCP_STATUS_COMMON_NOESC
) {
1042 zfcp_erp_port_block(port
, 0);
1043 result
= ZFCP_ERP_EXIT
;
1045 atomic_inc(&port
->erp_counter
);
1046 if (atomic_read(&port
->erp_counter
) > ZFCP_MAX_ERPS
) {
1047 dev_err(&port
->adapter
->ccw_device
->dev
,
1048 "ERP failed for remote port 0x%016Lx\n",
1049 (unsigned long long)port
->wwpn
);
1050 zfcp_erp_set_port_status(port
,
1051 ZFCP_STATUS_COMMON_ERP_FAILED
);
1056 if (atomic_read(&port
->status
) & ZFCP_STATUS_COMMON_ERP_FAILED
) {
1057 zfcp_erp_port_block(port
, 0);
1058 result
= ZFCP_ERP_EXIT
;
1063 static int zfcp_erp_strategy_check_adapter(struct zfcp_adapter
*adapter
,
1067 case ZFCP_ERP_SUCCEEDED
:
1068 atomic_set(&adapter
->erp_counter
, 0);
1069 zfcp_erp_adapter_unblock(adapter
);
1072 case ZFCP_ERP_FAILED
:
1073 atomic_inc(&adapter
->erp_counter
);
1074 if (atomic_read(&adapter
->erp_counter
) > ZFCP_MAX_ERPS
) {
1075 dev_err(&adapter
->ccw_device
->dev
,
1076 "ERP cannot recover an error "
1077 "on the FCP device\n");
1078 zfcp_erp_set_adapter_status(adapter
,
1079 ZFCP_STATUS_COMMON_ERP_FAILED
);
1084 if (atomic_read(&adapter
->status
) & ZFCP_STATUS_COMMON_ERP_FAILED
) {
1085 zfcp_erp_adapter_block(adapter
, 0);
1086 result
= ZFCP_ERP_EXIT
;
1091 static int zfcp_erp_strategy_check_target(struct zfcp_erp_action
*erp_action
,
1094 struct zfcp_adapter
*adapter
= erp_action
->adapter
;
1095 struct zfcp_port
*port
= erp_action
->port
;
1096 struct scsi_device
*sdev
= erp_action
->sdev
;
1098 switch (erp_action
->action
) {
1100 case ZFCP_ERP_ACTION_REOPEN_LUN
:
1101 result
= zfcp_erp_strategy_check_lun(sdev
, result
);
1104 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED
:
1105 case ZFCP_ERP_ACTION_REOPEN_PORT
:
1106 result
= zfcp_erp_strategy_check_port(port
, result
);
1109 case ZFCP_ERP_ACTION_REOPEN_ADAPTER
:
1110 result
= zfcp_erp_strategy_check_adapter(adapter
, result
);
1116 static int zfcp_erp_strat_change_det(atomic_t
*target_status
, u32 erp_status
)
1118 int status
= atomic_read(target_status
);
1120 if ((status
& ZFCP_STATUS_COMMON_RUNNING
) &&
1121 (erp_status
& ZFCP_STATUS_ERP_CLOSE_ONLY
))
1122 return 1; /* take it online */
1124 if (!(status
& ZFCP_STATUS_COMMON_RUNNING
) &&
1125 !(erp_status
& ZFCP_STATUS_ERP_CLOSE_ONLY
))
1126 return 1; /* take it offline */
1131 static int zfcp_erp_strategy_statechange(struct zfcp_erp_action
*act
, int ret
)
1133 int action
= act
->action
;
1134 struct zfcp_adapter
*adapter
= act
->adapter
;
1135 struct zfcp_port
*port
= act
->port
;
1136 struct scsi_device
*sdev
= act
->sdev
;
1137 struct zfcp_scsi_dev
*zfcp_sdev
;
1138 u32 erp_status
= act
->status
;
1141 case ZFCP_ERP_ACTION_REOPEN_ADAPTER
:
1142 if (zfcp_erp_strat_change_det(&adapter
->status
, erp_status
)) {
1143 _zfcp_erp_adapter_reopen(adapter
,
1144 ZFCP_STATUS_COMMON_ERP_FAILED
,
1146 return ZFCP_ERP_EXIT
;
1150 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED
:
1151 case ZFCP_ERP_ACTION_REOPEN_PORT
:
1152 if (zfcp_erp_strat_change_det(&port
->status
, erp_status
)) {
1153 _zfcp_erp_port_reopen(port
,
1154 ZFCP_STATUS_COMMON_ERP_FAILED
,
1156 return ZFCP_ERP_EXIT
;
1160 case ZFCP_ERP_ACTION_REOPEN_LUN
:
1161 zfcp_sdev
= sdev_to_zfcp(sdev
);
1162 if (zfcp_erp_strat_change_det(&zfcp_sdev
->status
, erp_status
)) {
1163 _zfcp_erp_lun_reopen(sdev
,
1164 ZFCP_STATUS_COMMON_ERP_FAILED
,
1166 return ZFCP_ERP_EXIT
;
1173 static void zfcp_erp_action_dequeue(struct zfcp_erp_action
*erp_action
)
1175 struct zfcp_adapter
*adapter
= erp_action
->adapter
;
1176 struct zfcp_scsi_dev
*zfcp_sdev
;
1178 adapter
->erp_total_count
--;
1179 if (erp_action
->status
& ZFCP_STATUS_ERP_LOWMEM
) {
1180 adapter
->erp_low_mem_count
--;
1181 erp_action
->status
&= ~ZFCP_STATUS_ERP_LOWMEM
;
1184 list_del(&erp_action
->list
);
1185 zfcp_dbf_rec_run("eractd1", erp_action
);
1187 switch (erp_action
->action
) {
1188 case ZFCP_ERP_ACTION_REOPEN_LUN
:
1189 zfcp_sdev
= sdev_to_zfcp(erp_action
->sdev
);
1190 atomic_clear_mask(ZFCP_STATUS_COMMON_ERP_INUSE
,
1191 &zfcp_sdev
->status
);
1194 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED
:
1195 case ZFCP_ERP_ACTION_REOPEN_PORT
:
1196 atomic_clear_mask(ZFCP_STATUS_COMMON_ERP_INUSE
,
1197 &erp_action
->port
->status
);
1200 case ZFCP_ERP_ACTION_REOPEN_ADAPTER
:
1201 atomic_clear_mask(ZFCP_STATUS_COMMON_ERP_INUSE
,
1202 &erp_action
->adapter
->status
);
1207 static void zfcp_erp_action_cleanup(struct zfcp_erp_action
*act
, int result
)
1209 struct zfcp_adapter
*adapter
= act
->adapter
;
1210 struct zfcp_port
*port
= act
->port
;
1211 struct scsi_device
*sdev
= act
->sdev
;
1213 switch (act
->action
) {
1214 case ZFCP_ERP_ACTION_REOPEN_LUN
:
1215 if (!(act
->status
& ZFCP_STATUS_ERP_NO_REF
))
1216 scsi_device_put(sdev
);
1219 case ZFCP_ERP_ACTION_REOPEN_PORT
:
1220 if (result
== ZFCP_ERP_SUCCEEDED
)
1221 zfcp_scsi_schedule_rport_register(port
);
1223 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED
:
1224 put_device(&port
->dev
);
1227 case ZFCP_ERP_ACTION_REOPEN_ADAPTER
:
1228 if (result
== ZFCP_ERP_SUCCEEDED
) {
1229 register_service_level(&adapter
->service_level
);
1230 zfcp_fc_conditional_port_scan(adapter
);
1231 queue_work(adapter
->work_queue
, &adapter
->ns_up_work
);
1233 unregister_service_level(&adapter
->service_level
);
1235 kref_put(&adapter
->ref
, zfcp_adapter_release
);
1240 static int zfcp_erp_strategy_do_action(struct zfcp_erp_action
*erp_action
)
1242 switch (erp_action
->action
) {
1243 case ZFCP_ERP_ACTION_REOPEN_ADAPTER
:
1244 return zfcp_erp_adapter_strategy(erp_action
);
1245 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED
:
1246 return zfcp_erp_port_forced_strategy(erp_action
);
1247 case ZFCP_ERP_ACTION_REOPEN_PORT
:
1248 return zfcp_erp_port_strategy(erp_action
);
1249 case ZFCP_ERP_ACTION_REOPEN_LUN
:
1250 return zfcp_erp_lun_strategy(erp_action
);
1252 return ZFCP_ERP_FAILED
;
1255 static int zfcp_erp_strategy(struct zfcp_erp_action
*erp_action
)
1258 unsigned long flags
;
1259 struct zfcp_adapter
*adapter
= erp_action
->adapter
;
1261 kref_get(&adapter
->ref
);
1263 write_lock_irqsave(&adapter
->erp_lock
, flags
);
1264 zfcp_erp_strategy_check_fsfreq(erp_action
);
1266 if (erp_action
->status
& ZFCP_STATUS_ERP_DISMISSED
) {
1267 zfcp_erp_action_dequeue(erp_action
);
1268 retval
= ZFCP_ERP_DISMISSED
;
1272 if (erp_action
->status
& ZFCP_STATUS_ERP_TIMEDOUT
) {
1273 retval
= ZFCP_ERP_FAILED
;
1277 zfcp_erp_action_to_running(erp_action
);
1279 /* no lock to allow for blocking operations */
1280 write_unlock_irqrestore(&adapter
->erp_lock
, flags
);
1281 retval
= zfcp_erp_strategy_do_action(erp_action
);
1282 write_lock_irqsave(&adapter
->erp_lock
, flags
);
1284 if (erp_action
->status
& ZFCP_STATUS_ERP_DISMISSED
)
1285 retval
= ZFCP_ERP_CONTINUES
;
1288 case ZFCP_ERP_NOMEM
:
1289 if (!(erp_action
->status
& ZFCP_STATUS_ERP_LOWMEM
)) {
1290 ++adapter
->erp_low_mem_count
;
1291 erp_action
->status
|= ZFCP_STATUS_ERP_LOWMEM
;
1293 if (adapter
->erp_total_count
== adapter
->erp_low_mem_count
)
1294 _zfcp_erp_adapter_reopen(adapter
, 0, "erstgy1");
1296 zfcp_erp_strategy_memwait(erp_action
);
1297 retval
= ZFCP_ERP_CONTINUES
;
1301 case ZFCP_ERP_CONTINUES
:
1302 if (erp_action
->status
& ZFCP_STATUS_ERP_LOWMEM
) {
1303 --adapter
->erp_low_mem_count
;
1304 erp_action
->status
&= ~ZFCP_STATUS_ERP_LOWMEM
;
1310 retval
= zfcp_erp_strategy_check_target(erp_action
, retval
);
1311 zfcp_erp_action_dequeue(erp_action
);
1312 retval
= zfcp_erp_strategy_statechange(erp_action
, retval
);
1313 if (retval
== ZFCP_ERP_EXIT
)
1315 if (retval
== ZFCP_ERP_SUCCEEDED
)
1316 zfcp_erp_strategy_followup_success(erp_action
);
1317 if (retval
== ZFCP_ERP_FAILED
)
1318 zfcp_erp_strategy_followup_failed(erp_action
);
1321 write_unlock_irqrestore(&adapter
->erp_lock
, flags
);
1323 if (retval
!= ZFCP_ERP_CONTINUES
)
1324 zfcp_erp_action_cleanup(erp_action
, retval
);
1326 kref_put(&adapter
->ref
, zfcp_adapter_release
);
1330 static int zfcp_erp_thread(void *data
)
1332 struct zfcp_adapter
*adapter
= (struct zfcp_adapter
*) data
;
1333 struct list_head
*next
;
1334 struct zfcp_erp_action
*act
;
1335 unsigned long flags
;
1338 wait_event_interruptible(adapter
->erp_ready_wq
,
1339 !list_empty(&adapter
->erp_ready_head
) ||
1340 kthread_should_stop());
1342 if (kthread_should_stop())
1345 write_lock_irqsave(&adapter
->erp_lock
, flags
);
1346 next
= adapter
->erp_ready_head
.next
;
1347 write_unlock_irqrestore(&adapter
->erp_lock
, flags
);
1349 if (next
!= &adapter
->erp_ready_head
) {
1350 act
= list_entry(next
, struct zfcp_erp_action
, list
);
1352 /* there is more to come after dismission, no notify */
1353 if (zfcp_erp_strategy(act
) != ZFCP_ERP_DISMISSED
)
1354 zfcp_erp_wakeup(adapter
);
1362 * zfcp_erp_thread_setup - Start ERP thread for adapter
1363 * @adapter: Adapter to start the ERP thread for
1365 * Returns 0 on success or error code from kernel_thread()
1367 int zfcp_erp_thread_setup(struct zfcp_adapter
*adapter
)
1369 struct task_struct
*thread
;
1371 thread
= kthread_run(zfcp_erp_thread
, adapter
, "zfcperp%s",
1372 dev_name(&adapter
->ccw_device
->dev
));
1373 if (IS_ERR(thread
)) {
1374 dev_err(&adapter
->ccw_device
->dev
,
1375 "Creating an ERP thread for the FCP device failed.\n");
1376 return PTR_ERR(thread
);
1379 adapter
->erp_thread
= thread
;
1384 * zfcp_erp_thread_kill - Stop ERP thread.
1385 * @adapter: Adapter where the ERP thread should be stopped.
1387 * The caller of this routine ensures that the specified adapter has
1388 * been shut down and that this operation has been completed. Thus,
1389 * there are no pending erp_actions which would need to be handled
1392 void zfcp_erp_thread_kill(struct zfcp_adapter
*adapter
)
1394 kthread_stop(adapter
->erp_thread
);
1395 adapter
->erp_thread
= NULL
;
1396 WARN_ON(!list_empty(&adapter
->erp_ready_head
));
1397 WARN_ON(!list_empty(&adapter
->erp_running_head
));
1401 * zfcp_erp_wait - wait for completion of error recovery on an adapter
1402 * @adapter: adapter for which to wait for completion of its error recovery
1404 void zfcp_erp_wait(struct zfcp_adapter
*adapter
)
1406 wait_event(adapter
->erp_done_wqh
,
1407 !(atomic_read(&adapter
->status
) &
1408 ZFCP_STATUS_ADAPTER_ERP_PENDING
));
1412 * zfcp_erp_set_adapter_status - set adapter status bits
1413 * @adapter: adapter to change the status
1414 * @mask: status bits to change
1416 * Changes in common status bits are propagated to attached ports and LUNs.
1418 void zfcp_erp_set_adapter_status(struct zfcp_adapter
*adapter
, u32 mask
)
1420 struct zfcp_port
*port
;
1421 struct scsi_device
*sdev
;
1422 unsigned long flags
;
1423 u32 common_mask
= mask
& ZFCP_COMMON_FLAGS
;
1425 atomic_set_mask(mask
, &adapter
->status
);
1430 read_lock_irqsave(&adapter
->port_list_lock
, flags
);
1431 list_for_each_entry(port
, &adapter
->port_list
, list
)
1432 atomic_set_mask(common_mask
, &port
->status
);
1433 read_unlock_irqrestore(&adapter
->port_list_lock
, flags
);
1435 spin_lock_irqsave(adapter
->scsi_host
->host_lock
, flags
);
1436 __shost_for_each_device(sdev
, adapter
->scsi_host
)
1437 atomic_set_mask(common_mask
, &sdev_to_zfcp(sdev
)->status
);
1438 spin_unlock_irqrestore(adapter
->scsi_host
->host_lock
, flags
);
1442 * zfcp_erp_clear_adapter_status - clear adapter status bits
1443 * @adapter: adapter to change the status
1444 * @mask: status bits to change
1446 * Changes in common status bits are propagated to attached ports and LUNs.
1448 void zfcp_erp_clear_adapter_status(struct zfcp_adapter
*adapter
, u32 mask
)
1450 struct zfcp_port
*port
;
1451 struct scsi_device
*sdev
;
1452 unsigned long flags
;
1453 u32 common_mask
= mask
& ZFCP_COMMON_FLAGS
;
1454 u32 clear_counter
= mask
& ZFCP_STATUS_COMMON_ERP_FAILED
;
1456 atomic_clear_mask(mask
, &adapter
->status
);
1462 atomic_set(&adapter
->erp_counter
, 0);
1464 read_lock_irqsave(&adapter
->port_list_lock
, flags
);
1465 list_for_each_entry(port
, &adapter
->port_list
, list
) {
1466 atomic_clear_mask(common_mask
, &port
->status
);
1468 atomic_set(&port
->erp_counter
, 0);
1470 read_unlock_irqrestore(&adapter
->port_list_lock
, flags
);
1472 spin_lock_irqsave(adapter
->scsi_host
->host_lock
, flags
);
1473 __shost_for_each_device(sdev
, adapter
->scsi_host
) {
1474 atomic_clear_mask(common_mask
, &sdev_to_zfcp(sdev
)->status
);
1476 atomic_set(&sdev_to_zfcp(sdev
)->erp_counter
, 0);
1478 spin_unlock_irqrestore(adapter
->scsi_host
->host_lock
, flags
);
1482 * zfcp_erp_set_port_status - set port status bits
1483 * @port: port to change the status
1484 * @mask: status bits to change
1486 * Changes in common status bits are propagated to attached LUNs.
1488 void zfcp_erp_set_port_status(struct zfcp_port
*port
, u32 mask
)
1490 struct scsi_device
*sdev
;
1491 u32 common_mask
= mask
& ZFCP_COMMON_FLAGS
;
1492 unsigned long flags
;
1494 atomic_set_mask(mask
, &port
->status
);
1499 spin_lock_irqsave(port
->adapter
->scsi_host
->host_lock
, flags
);
1500 __shost_for_each_device(sdev
, port
->adapter
->scsi_host
)
1501 if (sdev_to_zfcp(sdev
)->port
== port
)
1502 atomic_set_mask(common_mask
,
1503 &sdev_to_zfcp(sdev
)->status
);
1504 spin_unlock_irqrestore(port
->adapter
->scsi_host
->host_lock
, flags
);
1508 * zfcp_erp_clear_port_status - clear port status bits
1509 * @port: adapter to change the status
1510 * @mask: status bits to change
1512 * Changes in common status bits are propagated to attached LUNs.
1514 void zfcp_erp_clear_port_status(struct zfcp_port
*port
, u32 mask
)
1516 struct scsi_device
*sdev
;
1517 u32 common_mask
= mask
& ZFCP_COMMON_FLAGS
;
1518 u32 clear_counter
= mask
& ZFCP_STATUS_COMMON_ERP_FAILED
;
1519 unsigned long flags
;
1521 atomic_clear_mask(mask
, &port
->status
);
1527 atomic_set(&port
->erp_counter
, 0);
1529 spin_lock_irqsave(port
->adapter
->scsi_host
->host_lock
, flags
);
1530 __shost_for_each_device(sdev
, port
->adapter
->scsi_host
)
1531 if (sdev_to_zfcp(sdev
)->port
== port
) {
1532 atomic_clear_mask(common_mask
,
1533 &sdev_to_zfcp(sdev
)->status
);
1535 atomic_set(&sdev_to_zfcp(sdev
)->erp_counter
, 0);
1537 spin_unlock_irqrestore(port
->adapter
->scsi_host
->host_lock
, flags
);
1541 * zfcp_erp_set_lun_status - set lun status bits
1542 * @sdev: SCSI device / lun to set the status bits
1543 * @mask: status bits to change
1545 void zfcp_erp_set_lun_status(struct scsi_device
*sdev
, u32 mask
)
1547 struct zfcp_scsi_dev
*zfcp_sdev
= sdev_to_zfcp(sdev
);
1549 atomic_set_mask(mask
, &zfcp_sdev
->status
);
1553 * zfcp_erp_clear_lun_status - clear lun status bits
1554 * @sdev: SCSi device / lun to clear the status bits
1555 * @mask: status bits to change
1557 void zfcp_erp_clear_lun_status(struct scsi_device
*sdev
, u32 mask
)
1559 struct zfcp_scsi_dev
*zfcp_sdev
= sdev_to_zfcp(sdev
);
1561 atomic_clear_mask(mask
, &zfcp_sdev
->status
);
1563 if (mask
& ZFCP_STATUS_COMMON_ERP_FAILED
)
1564 atomic_set(&zfcp_sdev
->erp_counter
, 0);