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 shost_for_each_device(sdev, port->adapter->scsi_host)
107 if (sdev_to_zfcp(sdev)->port == port)
108 zfcp_erp_action_dismiss_lun(sdev);
111 static void zfcp_erp_action_dismiss_adapter(struct zfcp_adapter *adapter)
113 struct zfcp_port *port;
115 if (atomic_read(&adapter->status) & ZFCP_STATUS_COMMON_ERP_INUSE)
116 zfcp_erp_action_dismiss(&adapter->erp_action);
118 read_lock(&adapter->port_list_lock);
119 list_for_each_entry(port, &adapter->port_list, list)
120 zfcp_erp_action_dismiss_port(port);
121 read_unlock(&adapter->port_list_lock);
125 static int zfcp_erp_required_act(int want, struct zfcp_adapter *adapter,
126 struct zfcp_port *port,
127 struct scsi_device *sdev)
130 int l_status, p_status, a_status;
131 struct zfcp_scsi_dev *zfcp_sdev;
134 case ZFCP_ERP_ACTION_REOPEN_LUN:
135 zfcp_sdev = sdev_to_zfcp(sdev);
136 l_status = atomic_read(&zfcp_sdev->status);
137 if (l_status & ZFCP_STATUS_COMMON_ERP_INUSE)
139 p_status = atomic_read(&port->status);
140 if (!(p_status & ZFCP_STATUS_COMMON_RUNNING) ||
141 p_status & ZFCP_STATUS_COMMON_ERP_FAILED)
143 if (!(p_status & ZFCP_STATUS_COMMON_UNBLOCKED))
144 need = ZFCP_ERP_ACTION_REOPEN_PORT;
146 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
147 p_status = atomic_read(&port->status);
148 if (!(p_status & ZFCP_STATUS_COMMON_OPEN))
149 need = ZFCP_ERP_ACTION_REOPEN_PORT;
151 case ZFCP_ERP_ACTION_REOPEN_PORT:
152 p_status = atomic_read(&port->status);
153 if (p_status & ZFCP_STATUS_COMMON_ERP_INUSE)
155 a_status = atomic_read(&adapter->status);
156 if (!(a_status & ZFCP_STATUS_COMMON_RUNNING) ||
157 a_status & ZFCP_STATUS_COMMON_ERP_FAILED)
159 if (p_status & ZFCP_STATUS_COMMON_NOESC)
161 if (!(a_status & ZFCP_STATUS_COMMON_UNBLOCKED))
162 need = ZFCP_ERP_ACTION_REOPEN_ADAPTER;
164 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
165 a_status = atomic_read(&adapter->status);
166 if (a_status & ZFCP_STATUS_COMMON_ERP_INUSE)
168 if (!(a_status & ZFCP_STATUS_COMMON_RUNNING) &&
169 !(a_status & ZFCP_STATUS_COMMON_OPEN))
170 return 0; /* shutdown requested for closed adapter */
176 static struct zfcp_erp_action *zfcp_erp_setup_act(int need, u32 act_status,
177 struct zfcp_adapter *adapter,
178 struct zfcp_port *port,
179 struct scsi_device *sdev)
181 struct zfcp_erp_action *erp_action;
182 struct zfcp_scsi_dev *zfcp_sdev;
185 case ZFCP_ERP_ACTION_REOPEN_LUN:
186 zfcp_sdev = sdev_to_zfcp(sdev);
187 if (!(act_status & ZFCP_STATUS_ERP_NO_REF))
188 if (scsi_device_get(sdev))
190 atomic_set_mask(ZFCP_STATUS_COMMON_ERP_INUSE,
192 erp_action = &zfcp_sdev->erp_action;
193 memset(erp_action, 0, sizeof(struct zfcp_erp_action));
194 erp_action->port = port;
195 erp_action->sdev = sdev;
196 if (!(atomic_read(&zfcp_sdev->status) &
197 ZFCP_STATUS_COMMON_RUNNING))
198 act_status |= ZFCP_STATUS_ERP_CLOSE_ONLY;
201 case ZFCP_ERP_ACTION_REOPEN_PORT:
202 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
203 if (!get_device(&port->dev))
205 zfcp_erp_action_dismiss_port(port);
206 atomic_set_mask(ZFCP_STATUS_COMMON_ERP_INUSE, &port->status);
207 erp_action = &port->erp_action;
208 memset(erp_action, 0, sizeof(struct zfcp_erp_action));
209 erp_action->port = port;
210 if (!(atomic_read(&port->status) & ZFCP_STATUS_COMMON_RUNNING))
211 act_status |= ZFCP_STATUS_ERP_CLOSE_ONLY;
214 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
215 kref_get(&adapter->ref);
216 zfcp_erp_action_dismiss_adapter(adapter);
217 atomic_set_mask(ZFCP_STATUS_COMMON_ERP_INUSE, &adapter->status);
218 erp_action = &adapter->erp_action;
219 memset(erp_action, 0, sizeof(struct zfcp_erp_action));
220 if (!(atomic_read(&adapter->status) &
221 ZFCP_STATUS_COMMON_RUNNING))
222 act_status |= ZFCP_STATUS_ERP_CLOSE_ONLY;
229 erp_action->adapter = adapter;
230 erp_action->action = need;
231 erp_action->status = act_status;
236 static int zfcp_erp_action_enqueue(int want, struct zfcp_adapter *adapter,
237 struct zfcp_port *port,
238 struct scsi_device *sdev,
239 char *id, u32 act_status)
241 int retval = 1, need;
242 struct zfcp_erp_action *act;
244 if (!adapter->erp_thread)
247 need = zfcp_erp_required_act(want, adapter, port, sdev);
251 act = zfcp_erp_setup_act(need, act_status, adapter, port, sdev);
254 atomic_set_mask(ZFCP_STATUS_ADAPTER_ERP_PENDING, &adapter->status);
255 ++adapter->erp_total_count;
256 list_add_tail(&act->list, &adapter->erp_ready_head);
257 wake_up(&adapter->erp_ready_wq);
260 zfcp_dbf_rec_trig(id, adapter, port, sdev, want, need);
264 static int _zfcp_erp_adapter_reopen(struct zfcp_adapter *adapter,
265 int clear_mask, char *id)
267 zfcp_erp_adapter_block(adapter, clear_mask);
268 zfcp_scsi_schedule_rports_block(adapter);
270 /* ensure propagation of failed status to new devices */
271 if (atomic_read(&adapter->status) & ZFCP_STATUS_COMMON_ERP_FAILED) {
272 zfcp_erp_set_adapter_status(adapter,
273 ZFCP_STATUS_COMMON_ERP_FAILED);
276 return zfcp_erp_action_enqueue(ZFCP_ERP_ACTION_REOPEN_ADAPTER,
277 adapter, NULL, NULL, id, 0);
281 * zfcp_erp_adapter_reopen - Reopen adapter.
282 * @adapter: Adapter to reopen.
283 * @clear: Status flags to clear.
284 * @id: Id for debug trace event.
286 void zfcp_erp_adapter_reopen(struct zfcp_adapter *adapter, int clear, char *id)
290 zfcp_erp_adapter_block(adapter, clear);
291 zfcp_scsi_schedule_rports_block(adapter);
293 write_lock_irqsave(&adapter->erp_lock, flags);
294 if (atomic_read(&adapter->status) & ZFCP_STATUS_COMMON_ERP_FAILED)
295 zfcp_erp_set_adapter_status(adapter,
296 ZFCP_STATUS_COMMON_ERP_FAILED);
298 zfcp_erp_action_enqueue(ZFCP_ERP_ACTION_REOPEN_ADAPTER, adapter,
300 write_unlock_irqrestore(&adapter->erp_lock, flags);
304 * zfcp_erp_adapter_shutdown - Shutdown adapter.
305 * @adapter: Adapter to shut down.
306 * @clear: Status flags to clear.
307 * @id: Id for debug trace event.
309 void zfcp_erp_adapter_shutdown(struct zfcp_adapter *adapter, int clear,
312 int flags = ZFCP_STATUS_COMMON_RUNNING | ZFCP_STATUS_COMMON_ERP_FAILED;
313 zfcp_erp_adapter_reopen(adapter, clear | flags, id);
317 * zfcp_erp_port_shutdown - Shutdown port
318 * @port: Port to shut down.
319 * @clear: Status flags to clear.
320 * @id: Id for debug trace event.
322 void zfcp_erp_port_shutdown(struct zfcp_port *port, int clear, char *id)
324 int flags = ZFCP_STATUS_COMMON_RUNNING | ZFCP_STATUS_COMMON_ERP_FAILED;
325 zfcp_erp_port_reopen(port, clear | flags, id);
328 static void zfcp_erp_port_block(struct zfcp_port *port, int clear)
330 zfcp_erp_clear_port_status(port,
331 ZFCP_STATUS_COMMON_UNBLOCKED | clear);
334 static void _zfcp_erp_port_forced_reopen(struct zfcp_port *port, int clear,
337 zfcp_erp_port_block(port, clear);
338 zfcp_scsi_schedule_rport_block(port);
340 if (atomic_read(&port->status) & ZFCP_STATUS_COMMON_ERP_FAILED)
343 zfcp_erp_action_enqueue(ZFCP_ERP_ACTION_REOPEN_PORT_FORCED,
344 port->adapter, port, NULL, id, 0);
348 * zfcp_erp_port_forced_reopen - Forced close of port and open again
349 * @port: Port to force close and to reopen.
350 * @clear: Status flags to clear.
351 * @id: Id for debug trace event.
353 void zfcp_erp_port_forced_reopen(struct zfcp_port *port, int clear, char *id)
356 struct zfcp_adapter *adapter = port->adapter;
358 write_lock_irqsave(&adapter->erp_lock, flags);
359 _zfcp_erp_port_forced_reopen(port, clear, id);
360 write_unlock_irqrestore(&adapter->erp_lock, flags);
363 static int _zfcp_erp_port_reopen(struct zfcp_port *port, int clear, char *id)
365 zfcp_erp_port_block(port, clear);
366 zfcp_scsi_schedule_rport_block(port);
368 if (atomic_read(&port->status) & ZFCP_STATUS_COMMON_ERP_FAILED) {
369 /* ensure propagation of failed status to new devices */
370 zfcp_erp_set_port_status(port, ZFCP_STATUS_COMMON_ERP_FAILED);
374 return zfcp_erp_action_enqueue(ZFCP_ERP_ACTION_REOPEN_PORT,
375 port->adapter, port, NULL, id, 0);
379 * zfcp_erp_port_reopen - trigger remote port recovery
380 * @port: port to recover
381 * @clear_mask: flags in port status to be cleared
382 * @id: Id for debug trace event.
384 * Returns 0 if recovery has been triggered, < 0 if not.
386 int zfcp_erp_port_reopen(struct zfcp_port *port, int clear, char *id)
390 struct zfcp_adapter *adapter = port->adapter;
392 write_lock_irqsave(&adapter->erp_lock, flags);
393 retval = _zfcp_erp_port_reopen(port, clear, id);
394 write_unlock_irqrestore(&adapter->erp_lock, flags);
399 static void zfcp_erp_lun_block(struct scsi_device *sdev, int clear_mask)
401 zfcp_erp_clear_lun_status(sdev,
402 ZFCP_STATUS_COMMON_UNBLOCKED | clear_mask);
405 static void _zfcp_erp_lun_reopen(struct scsi_device *sdev, int clear, char *id,
408 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
409 struct zfcp_adapter *adapter = zfcp_sdev->port->adapter;
411 zfcp_erp_lun_block(sdev, clear);
413 if (atomic_read(&zfcp_sdev->status) & ZFCP_STATUS_COMMON_ERP_FAILED)
416 zfcp_erp_action_enqueue(ZFCP_ERP_ACTION_REOPEN_LUN, adapter,
417 zfcp_sdev->port, sdev, id, act_status);
421 * zfcp_erp_lun_reopen - initiate reopen of a LUN
422 * @sdev: SCSI device / LUN to be reopened
423 * @clear_mask: specifies flags in LUN status to be cleared
424 * @id: Id for debug trace event.
426 * Return: 0 on success, < 0 on error
428 void zfcp_erp_lun_reopen(struct scsi_device *sdev, int clear, char *id)
431 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
432 struct zfcp_port *port = zfcp_sdev->port;
433 struct zfcp_adapter *adapter = port->adapter;
435 write_lock_irqsave(&adapter->erp_lock, flags);
436 _zfcp_erp_lun_reopen(sdev, clear, id, 0);
437 write_unlock_irqrestore(&adapter->erp_lock, flags);
441 * zfcp_erp_lun_shutdown - Shutdown LUN
442 * @sdev: SCSI device / LUN to shut down.
443 * @clear: Status flags to clear.
444 * @id: Id for debug trace event.
446 void zfcp_erp_lun_shutdown(struct scsi_device *sdev, int clear, char *id)
448 int flags = ZFCP_STATUS_COMMON_RUNNING | ZFCP_STATUS_COMMON_ERP_FAILED;
449 zfcp_erp_lun_reopen(sdev, clear | flags, id);
453 * zfcp_erp_lun_shutdown_wait - Shutdown LUN and wait for erp completion
454 * @sdev: SCSI device / LUN to shut down.
455 * @id: Id for debug trace event.
457 * Do not acquire a reference for the LUN when creating the ERP
458 * action. It is safe, because this function waits for the ERP to
459 * complete first. This allows to shutdown the LUN, even when the SCSI
460 * device is in the state SDEV_DEL when scsi_device_get will fail.
462 void zfcp_erp_lun_shutdown_wait(struct scsi_device *sdev, char *id)
465 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
466 struct zfcp_port *port = zfcp_sdev->port;
467 struct zfcp_adapter *adapter = port->adapter;
468 int clear = ZFCP_STATUS_COMMON_RUNNING | ZFCP_STATUS_COMMON_ERP_FAILED;
470 write_lock_irqsave(&adapter->erp_lock, flags);
471 _zfcp_erp_lun_reopen(sdev, clear, id, ZFCP_STATUS_ERP_NO_REF);
472 write_unlock_irqrestore(&adapter->erp_lock, flags);
474 zfcp_erp_wait(adapter);
477 static int status_change_set(unsigned long mask, atomic_t *status)
479 return (atomic_read(status) ^ mask) & mask;
482 static void zfcp_erp_adapter_unblock(struct zfcp_adapter *adapter)
484 if (status_change_set(ZFCP_STATUS_COMMON_UNBLOCKED, &adapter->status))
485 zfcp_dbf_rec_run("eraubl1", &adapter->erp_action);
486 atomic_set_mask(ZFCP_STATUS_COMMON_UNBLOCKED, &adapter->status);
489 static void zfcp_erp_port_unblock(struct zfcp_port *port)
491 if (status_change_set(ZFCP_STATUS_COMMON_UNBLOCKED, &port->status))
492 zfcp_dbf_rec_run("erpubl1", &port->erp_action);
493 atomic_set_mask(ZFCP_STATUS_COMMON_UNBLOCKED, &port->status);
496 static void zfcp_erp_lun_unblock(struct scsi_device *sdev)
498 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
500 if (status_change_set(ZFCP_STATUS_COMMON_UNBLOCKED, &zfcp_sdev->status))
501 zfcp_dbf_rec_run("erlubl1", &sdev_to_zfcp(sdev)->erp_action);
502 atomic_set_mask(ZFCP_STATUS_COMMON_UNBLOCKED, &zfcp_sdev->status);
505 static void zfcp_erp_action_to_running(struct zfcp_erp_action *erp_action)
507 list_move(&erp_action->list, &erp_action->adapter->erp_running_head);
508 zfcp_dbf_rec_run("erator1", erp_action);
511 static void zfcp_erp_strategy_check_fsfreq(struct zfcp_erp_action *act)
513 struct zfcp_adapter *adapter = act->adapter;
514 struct zfcp_fsf_req *req;
516 if (!act->fsf_req_id)
519 spin_lock(&adapter->req_list->lock);
520 req = _zfcp_reqlist_find(adapter->req_list, act->fsf_req_id);
521 if (req && req->erp_action == act) {
522 if (act->status & (ZFCP_STATUS_ERP_DISMISSED |
523 ZFCP_STATUS_ERP_TIMEDOUT)) {
524 req->status |= ZFCP_STATUS_FSFREQ_DISMISSED;
525 zfcp_dbf_rec_run("erscf_1", act);
526 req->erp_action = NULL;
528 if (act->status & ZFCP_STATUS_ERP_TIMEDOUT)
529 zfcp_dbf_rec_run("erscf_2", act);
530 if (req->status & ZFCP_STATUS_FSFREQ_DISMISSED)
534 spin_unlock(&adapter->req_list->lock);
538 * zfcp_erp_notify - Trigger ERP action.
539 * @erp_action: ERP action to continue.
540 * @set_mask: ERP action status flags to set.
542 void zfcp_erp_notify(struct zfcp_erp_action *erp_action, unsigned long set_mask)
544 struct zfcp_adapter *adapter = erp_action->adapter;
547 write_lock_irqsave(&adapter->erp_lock, flags);
548 if (zfcp_erp_action_exists(erp_action) == ZFCP_ERP_ACTION_RUNNING) {
549 erp_action->status |= set_mask;
550 zfcp_erp_action_ready(erp_action);
552 write_unlock_irqrestore(&adapter->erp_lock, flags);
556 * zfcp_erp_timeout_handler - Trigger ERP action from timed out ERP request
557 * @data: ERP action (from timer data)
559 void zfcp_erp_timeout_handler(unsigned long data)
561 struct zfcp_erp_action *act = (struct zfcp_erp_action *) data;
562 zfcp_erp_notify(act, ZFCP_STATUS_ERP_TIMEDOUT);
565 static void zfcp_erp_memwait_handler(unsigned long data)
567 zfcp_erp_notify((struct zfcp_erp_action *)data, 0);
570 static void zfcp_erp_strategy_memwait(struct zfcp_erp_action *erp_action)
572 init_timer(&erp_action->timer);
573 erp_action->timer.function = zfcp_erp_memwait_handler;
574 erp_action->timer.data = (unsigned long) erp_action;
575 erp_action->timer.expires = jiffies + HZ;
576 add_timer(&erp_action->timer);
579 static void _zfcp_erp_port_reopen_all(struct zfcp_adapter *adapter,
582 struct zfcp_port *port;
584 read_lock(&adapter->port_list_lock);
585 list_for_each_entry(port, &adapter->port_list, list)
586 _zfcp_erp_port_reopen(port, clear, id);
587 read_unlock(&adapter->port_list_lock);
590 static void _zfcp_erp_lun_reopen_all(struct zfcp_port *port, int clear,
593 struct scsi_device *sdev;
595 shost_for_each_device(sdev, port->adapter->scsi_host)
596 if (sdev_to_zfcp(sdev)->port == port)
597 _zfcp_erp_lun_reopen(sdev, clear, id, 0);
600 static void zfcp_erp_strategy_followup_failed(struct zfcp_erp_action *act)
602 switch (act->action) {
603 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
604 _zfcp_erp_adapter_reopen(act->adapter, 0, "ersff_1");
606 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
607 _zfcp_erp_port_forced_reopen(act->port, 0, "ersff_2");
609 case ZFCP_ERP_ACTION_REOPEN_PORT:
610 _zfcp_erp_port_reopen(act->port, 0, "ersff_3");
612 case ZFCP_ERP_ACTION_REOPEN_LUN:
613 _zfcp_erp_lun_reopen(act->sdev, 0, "ersff_4", 0);
618 static void zfcp_erp_strategy_followup_success(struct zfcp_erp_action *act)
620 switch (act->action) {
621 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
622 _zfcp_erp_port_reopen_all(act->adapter, 0, "ersfs_1");
624 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
625 _zfcp_erp_port_reopen(act->port, 0, "ersfs_2");
627 case ZFCP_ERP_ACTION_REOPEN_PORT:
628 _zfcp_erp_lun_reopen_all(act->port, 0, "ersfs_3");
633 static void zfcp_erp_wakeup(struct zfcp_adapter *adapter)
637 read_lock_irqsave(&adapter->erp_lock, flags);
638 if (list_empty(&adapter->erp_ready_head) &&
639 list_empty(&adapter->erp_running_head)) {
640 atomic_clear_mask(ZFCP_STATUS_ADAPTER_ERP_PENDING,
642 wake_up(&adapter->erp_done_wqh);
644 read_unlock_irqrestore(&adapter->erp_lock, flags);
647 static void zfcp_erp_enqueue_ptp_port(struct zfcp_adapter *adapter)
649 struct zfcp_port *port;
650 port = zfcp_port_enqueue(adapter, adapter->peer_wwpn, 0,
652 if (IS_ERR(port)) /* error or port already attached */
654 _zfcp_erp_port_reopen(port, 0, "ereptp1");
657 static int zfcp_erp_adapter_strat_fsf_xconf(struct zfcp_erp_action *erp_action)
661 struct zfcp_adapter *adapter = erp_action->adapter;
663 atomic_clear_mask(ZFCP_STATUS_ADAPTER_XCONFIG_OK, &adapter->status);
665 for (retries = 7; retries; retries--) {
666 atomic_clear_mask(ZFCP_STATUS_ADAPTER_HOST_CON_INIT,
668 write_lock_irq(&adapter->erp_lock);
669 zfcp_erp_action_to_running(erp_action);
670 write_unlock_irq(&adapter->erp_lock);
671 if (zfcp_fsf_exchange_config_data(erp_action)) {
672 atomic_clear_mask(ZFCP_STATUS_ADAPTER_HOST_CON_INIT,
674 return ZFCP_ERP_FAILED;
677 wait_event(adapter->erp_ready_wq,
678 !list_empty(&adapter->erp_ready_head));
679 if (erp_action->status & ZFCP_STATUS_ERP_TIMEDOUT)
682 if (!(atomic_read(&adapter->status) &
683 ZFCP_STATUS_ADAPTER_HOST_CON_INIT))
690 atomic_clear_mask(ZFCP_STATUS_ADAPTER_HOST_CON_INIT,
693 if (!(atomic_read(&adapter->status) & ZFCP_STATUS_ADAPTER_XCONFIG_OK))
694 return ZFCP_ERP_FAILED;
696 if (fc_host_port_type(adapter->scsi_host) == FC_PORTTYPE_PTP)
697 zfcp_erp_enqueue_ptp_port(adapter);
699 return ZFCP_ERP_SUCCEEDED;
702 static int zfcp_erp_adapter_strategy_open_fsf_xport(struct zfcp_erp_action *act)
705 struct zfcp_adapter *adapter = act->adapter;
707 write_lock_irq(&adapter->erp_lock);
708 zfcp_erp_action_to_running(act);
709 write_unlock_irq(&adapter->erp_lock);
711 ret = zfcp_fsf_exchange_port_data(act);
712 if (ret == -EOPNOTSUPP)
713 return ZFCP_ERP_SUCCEEDED;
715 return ZFCP_ERP_FAILED;
717 zfcp_dbf_rec_run("erasox1", act);
718 wait_event(adapter->erp_ready_wq,
719 !list_empty(&adapter->erp_ready_head));
720 zfcp_dbf_rec_run("erasox2", act);
721 if (act->status & ZFCP_STATUS_ERP_TIMEDOUT)
722 return ZFCP_ERP_FAILED;
724 return ZFCP_ERP_SUCCEEDED;
727 static int zfcp_erp_adapter_strategy_open_fsf(struct zfcp_erp_action *act)
729 if (zfcp_erp_adapter_strat_fsf_xconf(act) == ZFCP_ERP_FAILED)
730 return ZFCP_ERP_FAILED;
732 if (zfcp_erp_adapter_strategy_open_fsf_xport(act) == ZFCP_ERP_FAILED)
733 return ZFCP_ERP_FAILED;
735 if (mempool_resize(act->adapter->pool.sr_data,
736 act->adapter->stat_read_buf_num, GFP_KERNEL))
737 return ZFCP_ERP_FAILED;
739 if (mempool_resize(act->adapter->pool.status_read_req,
740 act->adapter->stat_read_buf_num, GFP_KERNEL))
741 return ZFCP_ERP_FAILED;
743 atomic_set(&act->adapter->stat_miss, act->adapter->stat_read_buf_num);
744 if (zfcp_status_read_refill(act->adapter))
745 return ZFCP_ERP_FAILED;
747 return ZFCP_ERP_SUCCEEDED;
750 static void zfcp_erp_adapter_strategy_close(struct zfcp_erp_action *act)
752 struct zfcp_adapter *adapter = act->adapter;
754 /* close queues to ensure that buffers are not accessed by adapter */
755 zfcp_qdio_close(adapter->qdio);
756 zfcp_fsf_req_dismiss_all(adapter);
757 adapter->fsf_req_seq_no = 0;
758 zfcp_fc_wka_ports_force_offline(adapter->gs);
759 /* all ports and LUNs are closed */
760 zfcp_erp_clear_adapter_status(adapter, ZFCP_STATUS_COMMON_OPEN);
762 atomic_clear_mask(ZFCP_STATUS_ADAPTER_XCONFIG_OK |
763 ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED, &adapter->status);
766 static int zfcp_erp_adapter_strategy_open(struct zfcp_erp_action *act)
768 struct zfcp_adapter *adapter = act->adapter;
770 if (zfcp_qdio_open(adapter->qdio)) {
771 atomic_clear_mask(ZFCP_STATUS_ADAPTER_XCONFIG_OK |
772 ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED,
774 return ZFCP_ERP_FAILED;
777 if (zfcp_erp_adapter_strategy_open_fsf(act)) {
778 zfcp_erp_adapter_strategy_close(act);
779 return ZFCP_ERP_FAILED;
782 atomic_set_mask(ZFCP_STATUS_COMMON_OPEN, &adapter->status);
784 return ZFCP_ERP_SUCCEEDED;
787 static int zfcp_erp_adapter_strategy(struct zfcp_erp_action *act)
789 struct zfcp_adapter *adapter = act->adapter;
791 if (atomic_read(&adapter->status) & ZFCP_STATUS_COMMON_OPEN) {
792 zfcp_erp_adapter_strategy_close(act);
793 if (act->status & ZFCP_STATUS_ERP_CLOSE_ONLY)
794 return ZFCP_ERP_EXIT;
797 if (zfcp_erp_adapter_strategy_open(act)) {
799 return ZFCP_ERP_FAILED;
802 return ZFCP_ERP_SUCCEEDED;
805 static int zfcp_erp_port_forced_strategy_close(struct zfcp_erp_action *act)
809 retval = zfcp_fsf_close_physical_port(act);
810 if (retval == -ENOMEM)
811 return ZFCP_ERP_NOMEM;
812 act->step = ZFCP_ERP_STEP_PHYS_PORT_CLOSING;
814 return ZFCP_ERP_FAILED;
816 return ZFCP_ERP_CONTINUES;
819 static void zfcp_erp_port_strategy_clearstati(struct zfcp_port *port)
821 atomic_clear_mask(ZFCP_STATUS_COMMON_ACCESS_DENIED, &port->status);
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 zfcp_erp_port_strategy_clearstati(port);
832 if ((status & ZFCP_STATUS_PORT_PHYS_OPEN) &&
833 (status & ZFCP_STATUS_COMMON_OPEN))
834 return zfcp_erp_port_forced_strategy_close(erp_action);
836 return ZFCP_ERP_FAILED;
838 case ZFCP_ERP_STEP_PHYS_PORT_CLOSING:
839 if (!(status & ZFCP_STATUS_PORT_PHYS_OPEN))
840 return ZFCP_ERP_SUCCEEDED;
842 return ZFCP_ERP_FAILED;
845 static int zfcp_erp_port_strategy_close(struct zfcp_erp_action *erp_action)
849 retval = zfcp_fsf_close_port(erp_action);
850 if (retval == -ENOMEM)
851 return ZFCP_ERP_NOMEM;
852 erp_action->step = ZFCP_ERP_STEP_PORT_CLOSING;
854 return ZFCP_ERP_FAILED;
855 return ZFCP_ERP_CONTINUES;
858 static int zfcp_erp_port_strategy_open_port(struct zfcp_erp_action *erp_action)
862 retval = zfcp_fsf_open_port(erp_action);
863 if (retval == -ENOMEM)
864 return ZFCP_ERP_NOMEM;
865 erp_action->step = ZFCP_ERP_STEP_PORT_OPENING;
867 return ZFCP_ERP_FAILED;
868 return ZFCP_ERP_CONTINUES;
871 static int zfcp_erp_open_ptp_port(struct zfcp_erp_action *act)
873 struct zfcp_adapter *adapter = act->adapter;
874 struct zfcp_port *port = act->port;
876 if (port->wwpn != adapter->peer_wwpn) {
877 zfcp_erp_set_port_status(port, ZFCP_STATUS_COMMON_ERP_FAILED);
878 return ZFCP_ERP_FAILED;
880 port->d_id = adapter->peer_d_id;
881 return zfcp_erp_port_strategy_open_port(act);
884 static int zfcp_erp_port_strategy_open_common(struct zfcp_erp_action *act)
886 struct zfcp_adapter *adapter = act->adapter;
887 struct zfcp_port *port = act->port;
888 int p_status = atomic_read(&port->status);
891 case ZFCP_ERP_STEP_UNINITIALIZED:
892 case ZFCP_ERP_STEP_PHYS_PORT_CLOSING:
893 case ZFCP_ERP_STEP_PORT_CLOSING:
894 if (fc_host_port_type(adapter->scsi_host) == FC_PORTTYPE_PTP)
895 return zfcp_erp_open_ptp_port(act);
897 zfcp_fc_trigger_did_lookup(port);
898 return ZFCP_ERP_EXIT;
900 return zfcp_erp_port_strategy_open_port(act);
902 case ZFCP_ERP_STEP_PORT_OPENING:
903 /* D_ID might have changed during open */
904 if (p_status & ZFCP_STATUS_COMMON_OPEN) {
906 zfcp_fc_trigger_did_lookup(port);
907 return ZFCP_ERP_EXIT;
909 return ZFCP_ERP_SUCCEEDED;
911 if (port->d_id && !(p_status & ZFCP_STATUS_COMMON_NOESC)) {
913 return ZFCP_ERP_FAILED;
915 /* fall through otherwise */
917 return ZFCP_ERP_FAILED;
920 static int zfcp_erp_port_strategy(struct zfcp_erp_action *erp_action)
922 struct zfcp_port *port = erp_action->port;
923 int p_status = atomic_read(&port->status);
925 if ((p_status & ZFCP_STATUS_COMMON_NOESC) &&
926 !(p_status & ZFCP_STATUS_COMMON_OPEN))
927 goto close_init_done;
929 switch (erp_action->step) {
930 case ZFCP_ERP_STEP_UNINITIALIZED:
931 zfcp_erp_port_strategy_clearstati(port);
932 if (p_status & ZFCP_STATUS_COMMON_OPEN)
933 return zfcp_erp_port_strategy_close(erp_action);
936 case ZFCP_ERP_STEP_PORT_CLOSING:
937 if (p_status & ZFCP_STATUS_COMMON_OPEN)
938 return ZFCP_ERP_FAILED;
943 if (erp_action->status & ZFCP_STATUS_ERP_CLOSE_ONLY)
944 return ZFCP_ERP_EXIT;
946 return zfcp_erp_port_strategy_open_common(erp_action);
949 static void zfcp_erp_lun_strategy_clearstati(struct scsi_device *sdev)
951 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
953 atomic_clear_mask(ZFCP_STATUS_COMMON_ACCESS_DENIED,
957 static int zfcp_erp_lun_strategy_close(struct zfcp_erp_action *erp_action)
959 int retval = zfcp_fsf_close_lun(erp_action);
960 if (retval == -ENOMEM)
961 return ZFCP_ERP_NOMEM;
962 erp_action->step = ZFCP_ERP_STEP_LUN_CLOSING;
964 return ZFCP_ERP_FAILED;
965 return ZFCP_ERP_CONTINUES;
968 static int zfcp_erp_lun_strategy_open(struct zfcp_erp_action *erp_action)
970 int retval = zfcp_fsf_open_lun(erp_action);
971 if (retval == -ENOMEM)
972 return ZFCP_ERP_NOMEM;
973 erp_action->step = ZFCP_ERP_STEP_LUN_OPENING;
975 return ZFCP_ERP_FAILED;
976 return ZFCP_ERP_CONTINUES;
979 static int zfcp_erp_lun_strategy(struct zfcp_erp_action *erp_action)
981 struct scsi_device *sdev = erp_action->sdev;
982 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
984 switch (erp_action->step) {
985 case ZFCP_ERP_STEP_UNINITIALIZED:
986 zfcp_erp_lun_strategy_clearstati(sdev);
987 if (atomic_read(&zfcp_sdev->status) & ZFCP_STATUS_COMMON_OPEN)
988 return zfcp_erp_lun_strategy_close(erp_action);
989 /* already closed, fall through */
990 case ZFCP_ERP_STEP_LUN_CLOSING:
991 if (atomic_read(&zfcp_sdev->status) & ZFCP_STATUS_COMMON_OPEN)
992 return ZFCP_ERP_FAILED;
993 if (erp_action->status & ZFCP_STATUS_ERP_CLOSE_ONLY)
994 return ZFCP_ERP_EXIT;
995 return zfcp_erp_lun_strategy_open(erp_action);
997 case ZFCP_ERP_STEP_LUN_OPENING:
998 if (atomic_read(&zfcp_sdev->status) & ZFCP_STATUS_COMMON_OPEN)
999 return ZFCP_ERP_SUCCEEDED;
1001 return ZFCP_ERP_FAILED;
1004 static int zfcp_erp_strategy_check_lun(struct scsi_device *sdev, int result)
1006 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
1009 case ZFCP_ERP_SUCCEEDED :
1010 atomic_set(&zfcp_sdev->erp_counter, 0);
1011 zfcp_erp_lun_unblock(sdev);
1013 case ZFCP_ERP_FAILED :
1014 atomic_inc(&zfcp_sdev->erp_counter);
1015 if (atomic_read(&zfcp_sdev->erp_counter) > ZFCP_MAX_ERPS) {
1016 dev_err(&zfcp_sdev->port->adapter->ccw_device->dev,
1017 "ERP failed for LUN 0x%016Lx on "
1019 (unsigned long long)zfcp_scsi_dev_lun(sdev),
1020 (unsigned long long)zfcp_sdev->port->wwpn);
1021 zfcp_erp_set_lun_status(sdev,
1022 ZFCP_STATUS_COMMON_ERP_FAILED);
1027 if (atomic_read(&zfcp_sdev->status) & ZFCP_STATUS_COMMON_ERP_FAILED) {
1028 zfcp_erp_lun_block(sdev, 0);
1029 result = ZFCP_ERP_EXIT;
1034 static int zfcp_erp_strategy_check_port(struct zfcp_port *port, int result)
1037 case ZFCP_ERP_SUCCEEDED :
1038 atomic_set(&port->erp_counter, 0);
1039 zfcp_erp_port_unblock(port);
1042 case ZFCP_ERP_FAILED :
1043 if (atomic_read(&port->status) & ZFCP_STATUS_COMMON_NOESC) {
1044 zfcp_erp_port_block(port, 0);
1045 result = ZFCP_ERP_EXIT;
1047 atomic_inc(&port->erp_counter);
1048 if (atomic_read(&port->erp_counter) > ZFCP_MAX_ERPS) {
1049 dev_err(&port->adapter->ccw_device->dev,
1050 "ERP failed for remote port 0x%016Lx\n",
1051 (unsigned long long)port->wwpn);
1052 zfcp_erp_set_port_status(port,
1053 ZFCP_STATUS_COMMON_ERP_FAILED);
1058 if (atomic_read(&port->status) & ZFCP_STATUS_COMMON_ERP_FAILED) {
1059 zfcp_erp_port_block(port, 0);
1060 result = ZFCP_ERP_EXIT;
1065 static int zfcp_erp_strategy_check_adapter(struct zfcp_adapter *adapter,
1069 case ZFCP_ERP_SUCCEEDED :
1070 atomic_set(&adapter->erp_counter, 0);
1071 zfcp_erp_adapter_unblock(adapter);
1074 case ZFCP_ERP_FAILED :
1075 atomic_inc(&adapter->erp_counter);
1076 if (atomic_read(&adapter->erp_counter) > ZFCP_MAX_ERPS) {
1077 dev_err(&adapter->ccw_device->dev,
1078 "ERP cannot recover an error "
1079 "on the FCP device\n");
1080 zfcp_erp_set_adapter_status(adapter,
1081 ZFCP_STATUS_COMMON_ERP_FAILED);
1086 if (atomic_read(&adapter->status) & ZFCP_STATUS_COMMON_ERP_FAILED) {
1087 zfcp_erp_adapter_block(adapter, 0);
1088 result = ZFCP_ERP_EXIT;
1093 static int zfcp_erp_strategy_check_target(struct zfcp_erp_action *erp_action,
1096 struct zfcp_adapter *adapter = erp_action->adapter;
1097 struct zfcp_port *port = erp_action->port;
1098 struct scsi_device *sdev = erp_action->sdev;
1100 switch (erp_action->action) {
1102 case ZFCP_ERP_ACTION_REOPEN_LUN:
1103 result = zfcp_erp_strategy_check_lun(sdev, result);
1106 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
1107 case ZFCP_ERP_ACTION_REOPEN_PORT:
1108 result = zfcp_erp_strategy_check_port(port, result);
1111 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
1112 result = zfcp_erp_strategy_check_adapter(adapter, result);
1118 static int zfcp_erp_strat_change_det(atomic_t *target_status, u32 erp_status)
1120 int status = atomic_read(target_status);
1122 if ((status & ZFCP_STATUS_COMMON_RUNNING) &&
1123 (erp_status & ZFCP_STATUS_ERP_CLOSE_ONLY))
1124 return 1; /* take it online */
1126 if (!(status & ZFCP_STATUS_COMMON_RUNNING) &&
1127 !(erp_status & ZFCP_STATUS_ERP_CLOSE_ONLY))
1128 return 1; /* take it offline */
1133 static int zfcp_erp_strategy_statechange(struct zfcp_erp_action *act, int ret)
1135 int action = act->action;
1136 struct zfcp_adapter *adapter = act->adapter;
1137 struct zfcp_port *port = act->port;
1138 struct scsi_device *sdev = act->sdev;
1139 struct zfcp_scsi_dev *zfcp_sdev;
1140 u32 erp_status = act->status;
1143 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
1144 if (zfcp_erp_strat_change_det(&adapter->status, erp_status)) {
1145 _zfcp_erp_adapter_reopen(adapter,
1146 ZFCP_STATUS_COMMON_ERP_FAILED,
1148 return ZFCP_ERP_EXIT;
1152 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
1153 case ZFCP_ERP_ACTION_REOPEN_PORT:
1154 if (zfcp_erp_strat_change_det(&port->status, erp_status)) {
1155 _zfcp_erp_port_reopen(port,
1156 ZFCP_STATUS_COMMON_ERP_FAILED,
1158 return ZFCP_ERP_EXIT;
1162 case ZFCP_ERP_ACTION_REOPEN_LUN:
1163 zfcp_sdev = sdev_to_zfcp(sdev);
1164 if (zfcp_erp_strat_change_det(&zfcp_sdev->status, erp_status)) {
1165 _zfcp_erp_lun_reopen(sdev,
1166 ZFCP_STATUS_COMMON_ERP_FAILED,
1168 return ZFCP_ERP_EXIT;
1175 static void zfcp_erp_action_dequeue(struct zfcp_erp_action *erp_action)
1177 struct zfcp_adapter *adapter = erp_action->adapter;
1178 struct zfcp_scsi_dev *zfcp_sdev;
1180 adapter->erp_total_count--;
1181 if (erp_action->status & ZFCP_STATUS_ERP_LOWMEM) {
1182 adapter->erp_low_mem_count--;
1183 erp_action->status &= ~ZFCP_STATUS_ERP_LOWMEM;
1186 list_del(&erp_action->list);
1187 zfcp_dbf_rec_run("eractd1", erp_action);
1189 switch (erp_action->action) {
1190 case ZFCP_ERP_ACTION_REOPEN_LUN:
1191 zfcp_sdev = sdev_to_zfcp(erp_action->sdev);
1192 atomic_clear_mask(ZFCP_STATUS_COMMON_ERP_INUSE,
1193 &zfcp_sdev->status);
1196 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
1197 case ZFCP_ERP_ACTION_REOPEN_PORT:
1198 atomic_clear_mask(ZFCP_STATUS_COMMON_ERP_INUSE,
1199 &erp_action->port->status);
1202 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
1203 atomic_clear_mask(ZFCP_STATUS_COMMON_ERP_INUSE,
1204 &erp_action->adapter->status);
1209 static void zfcp_erp_action_cleanup(struct zfcp_erp_action *act, int result)
1211 struct zfcp_adapter *adapter = act->adapter;
1212 struct zfcp_port *port = act->port;
1213 struct scsi_device *sdev = act->sdev;
1215 switch (act->action) {
1216 case ZFCP_ERP_ACTION_REOPEN_LUN:
1217 if (!(act->status & ZFCP_STATUS_ERP_NO_REF))
1218 scsi_device_put(sdev);
1221 case ZFCP_ERP_ACTION_REOPEN_PORT:
1222 if (result == ZFCP_ERP_SUCCEEDED)
1223 zfcp_scsi_schedule_rport_register(port);
1225 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
1226 put_device(&port->dev);
1229 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
1230 if (result == ZFCP_ERP_SUCCEEDED) {
1231 register_service_level(&adapter->service_level);
1232 zfcp_fc_conditional_port_scan(adapter);
1233 queue_work(adapter->work_queue, &adapter->ns_up_work);
1235 unregister_service_level(&adapter->service_level);
1237 kref_put(&adapter->ref, zfcp_adapter_release);
1242 static int zfcp_erp_strategy_do_action(struct zfcp_erp_action *erp_action)
1244 switch (erp_action->action) {
1245 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
1246 return zfcp_erp_adapter_strategy(erp_action);
1247 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
1248 return zfcp_erp_port_forced_strategy(erp_action);
1249 case ZFCP_ERP_ACTION_REOPEN_PORT:
1250 return zfcp_erp_port_strategy(erp_action);
1251 case ZFCP_ERP_ACTION_REOPEN_LUN:
1252 return zfcp_erp_lun_strategy(erp_action);
1254 return ZFCP_ERP_FAILED;
1257 static int zfcp_erp_strategy(struct zfcp_erp_action *erp_action)
1260 unsigned long flags;
1261 struct zfcp_adapter *adapter = erp_action->adapter;
1263 kref_get(&adapter->ref);
1265 write_lock_irqsave(&adapter->erp_lock, flags);
1266 zfcp_erp_strategy_check_fsfreq(erp_action);
1268 if (erp_action->status & ZFCP_STATUS_ERP_DISMISSED) {
1269 zfcp_erp_action_dequeue(erp_action);
1270 retval = ZFCP_ERP_DISMISSED;
1274 if (erp_action->status & ZFCP_STATUS_ERP_TIMEDOUT) {
1275 retval = ZFCP_ERP_FAILED;
1279 zfcp_erp_action_to_running(erp_action);
1281 /* no lock to allow for blocking operations */
1282 write_unlock_irqrestore(&adapter->erp_lock, flags);
1283 retval = zfcp_erp_strategy_do_action(erp_action);
1284 write_lock_irqsave(&adapter->erp_lock, flags);
1286 if (erp_action->status & ZFCP_STATUS_ERP_DISMISSED)
1287 retval = ZFCP_ERP_CONTINUES;
1290 case ZFCP_ERP_NOMEM:
1291 if (!(erp_action->status & ZFCP_STATUS_ERP_LOWMEM)) {
1292 ++adapter->erp_low_mem_count;
1293 erp_action->status |= ZFCP_STATUS_ERP_LOWMEM;
1295 if (adapter->erp_total_count == adapter->erp_low_mem_count)
1296 _zfcp_erp_adapter_reopen(adapter, 0, "erstgy1");
1298 zfcp_erp_strategy_memwait(erp_action);
1299 retval = ZFCP_ERP_CONTINUES;
1303 case ZFCP_ERP_CONTINUES:
1304 if (erp_action->status & ZFCP_STATUS_ERP_LOWMEM) {
1305 --adapter->erp_low_mem_count;
1306 erp_action->status &= ~ZFCP_STATUS_ERP_LOWMEM;
1312 retval = zfcp_erp_strategy_check_target(erp_action, retval);
1313 zfcp_erp_action_dequeue(erp_action);
1314 retval = zfcp_erp_strategy_statechange(erp_action, retval);
1315 if (retval == ZFCP_ERP_EXIT)
1317 if (retval == ZFCP_ERP_SUCCEEDED)
1318 zfcp_erp_strategy_followup_success(erp_action);
1319 if (retval == ZFCP_ERP_FAILED)
1320 zfcp_erp_strategy_followup_failed(erp_action);
1323 write_unlock_irqrestore(&adapter->erp_lock, flags);
1325 if (retval != ZFCP_ERP_CONTINUES)
1326 zfcp_erp_action_cleanup(erp_action, retval);
1328 kref_put(&adapter->ref, zfcp_adapter_release);
1332 static int zfcp_erp_thread(void *data)
1334 struct zfcp_adapter *adapter = (struct zfcp_adapter *) data;
1335 struct list_head *next;
1336 struct zfcp_erp_action *act;
1337 unsigned long flags;
1340 wait_event_interruptible(adapter->erp_ready_wq,
1341 !list_empty(&adapter->erp_ready_head) ||
1342 kthread_should_stop());
1344 if (kthread_should_stop())
1347 write_lock_irqsave(&adapter->erp_lock, flags);
1348 next = adapter->erp_ready_head.next;
1349 write_unlock_irqrestore(&adapter->erp_lock, flags);
1351 if (next != &adapter->erp_ready_head) {
1352 act = list_entry(next, struct zfcp_erp_action, list);
1354 /* there is more to come after dismission, no notify */
1355 if (zfcp_erp_strategy(act) != ZFCP_ERP_DISMISSED)
1356 zfcp_erp_wakeup(adapter);
1364 * zfcp_erp_thread_setup - Start ERP thread for adapter
1365 * @adapter: Adapter to start the ERP thread for
1367 * Returns 0 on success or error code from kernel_thread()
1369 int zfcp_erp_thread_setup(struct zfcp_adapter *adapter)
1371 struct task_struct *thread;
1373 thread = kthread_run(zfcp_erp_thread, adapter, "zfcperp%s",
1374 dev_name(&adapter->ccw_device->dev));
1375 if (IS_ERR(thread)) {
1376 dev_err(&adapter->ccw_device->dev,
1377 "Creating an ERP thread for the FCP device failed.\n");
1378 return PTR_ERR(thread);
1381 adapter->erp_thread = thread;
1386 * zfcp_erp_thread_kill - Stop ERP thread.
1387 * @adapter: Adapter where the ERP thread should be stopped.
1389 * The caller of this routine ensures that the specified adapter has
1390 * been shut down and that this operation has been completed. Thus,
1391 * there are no pending erp_actions which would need to be handled
1394 void zfcp_erp_thread_kill(struct zfcp_adapter *adapter)
1396 kthread_stop(adapter->erp_thread);
1397 adapter->erp_thread = NULL;
1398 WARN_ON(!list_empty(&adapter->erp_ready_head));
1399 WARN_ON(!list_empty(&adapter->erp_running_head));
1403 * zfcp_erp_wait - wait for completion of error recovery on an adapter
1404 * @adapter: adapter for which to wait for completion of its error recovery
1406 void zfcp_erp_wait(struct zfcp_adapter *adapter)
1408 wait_event(adapter->erp_done_wqh,
1409 !(atomic_read(&adapter->status) &
1410 ZFCP_STATUS_ADAPTER_ERP_PENDING));
1414 * zfcp_erp_set_adapter_status - set adapter status bits
1415 * @adapter: adapter to change the status
1416 * @mask: status bits to change
1418 * Changes in common status bits are propagated to attached ports and LUNs.
1420 void zfcp_erp_set_adapter_status(struct zfcp_adapter *adapter, u32 mask)
1422 struct zfcp_port *port;
1423 struct scsi_device *sdev;
1424 unsigned long flags;
1425 u32 common_mask = mask & ZFCP_COMMON_FLAGS;
1427 atomic_set_mask(mask, &adapter->status);
1432 read_lock_irqsave(&adapter->port_list_lock, flags);
1433 list_for_each_entry(port, &adapter->port_list, list)
1434 atomic_set_mask(common_mask, &port->status);
1435 read_unlock_irqrestore(&adapter->port_list_lock, flags);
1437 shost_for_each_device(sdev, adapter->scsi_host)
1438 atomic_set_mask(common_mask, &sdev_to_zfcp(sdev)->status);
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 shost_for_each_device(sdev, adapter->scsi_host) {
1473 atomic_clear_mask(common_mask, &sdev_to_zfcp(sdev)->status);
1475 atomic_set(&sdev_to_zfcp(sdev)->erp_counter, 0);
1480 * zfcp_erp_set_port_status - set port status bits
1481 * @port: port to change the status
1482 * @mask: status bits to change
1484 * Changes in common status bits are propagated to attached LUNs.
1486 void zfcp_erp_set_port_status(struct zfcp_port *port, u32 mask)
1488 struct scsi_device *sdev;
1489 u32 common_mask = mask & ZFCP_COMMON_FLAGS;
1491 atomic_set_mask(mask, &port->status);
1496 shost_for_each_device(sdev, port->adapter->scsi_host)
1497 if (sdev_to_zfcp(sdev)->port == port)
1498 atomic_set_mask(common_mask,
1499 &sdev_to_zfcp(sdev)->status);
1503 * zfcp_erp_clear_port_status - clear port status bits
1504 * @port: adapter to change the status
1505 * @mask: status bits to change
1507 * Changes in common status bits are propagated to attached LUNs.
1509 void zfcp_erp_clear_port_status(struct zfcp_port *port, u32 mask)
1511 struct scsi_device *sdev;
1512 u32 common_mask = mask & ZFCP_COMMON_FLAGS;
1513 u32 clear_counter = mask & ZFCP_STATUS_COMMON_ERP_FAILED;
1515 atomic_clear_mask(mask, &port->status);
1521 atomic_set(&port->erp_counter, 0);
1523 shost_for_each_device(sdev, port->adapter->scsi_host)
1524 if (sdev_to_zfcp(sdev)->port == port) {
1525 atomic_clear_mask(common_mask,
1526 &sdev_to_zfcp(sdev)->status);
1528 atomic_set(&sdev_to_zfcp(sdev)->erp_counter, 0);
1533 * zfcp_erp_set_lun_status - set lun status bits
1534 * @sdev: SCSI device / lun to set the status bits
1535 * @mask: status bits to change
1537 void zfcp_erp_set_lun_status(struct scsi_device *sdev, u32 mask)
1539 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
1541 atomic_set_mask(mask, &zfcp_sdev->status);
1545 * zfcp_erp_clear_lun_status - clear lun status bits
1546 * @sdev: SCSi device / lun to clear the status bits
1547 * @mask: status bits to change
1549 void zfcp_erp_clear_lun_status(struct scsi_device *sdev, u32 mask)
1551 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
1553 atomic_clear_mask(mask, &zfcp_sdev->status);
1555 if (mask & ZFCP_STATUS_COMMON_ERP_FAILED)
1556 atomic_set(&zfcp_sdev->erp_counter, 0);