4 * Error Recovery Procedures (ERP).
6 * Copyright IBM Corporation 2002, 2009
9 #define KMSG_COMPONENT "zfcp"
10 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
12 #include <linux/kthread.h>
15 #define ZFCP_MAX_ERPS 3
17 enum zfcp_erp_act_flags {
18 ZFCP_STATUS_ERP_TIMEDOUT = 0x10000000,
19 ZFCP_STATUS_ERP_CLOSE_ONLY = 0x01000000,
20 ZFCP_STATUS_ERP_DISMISSING = 0x00100000,
21 ZFCP_STATUS_ERP_DISMISSED = 0x00200000,
22 ZFCP_STATUS_ERP_LOWMEM = 0x00400000,
26 ZFCP_ERP_STEP_UNINITIALIZED = 0x0000,
27 ZFCP_ERP_STEP_FSF_XCONFIG = 0x0001,
28 ZFCP_ERP_STEP_PHYS_PORT_CLOSING = 0x0010,
29 ZFCP_ERP_STEP_PORT_CLOSING = 0x0100,
30 ZFCP_ERP_STEP_PORT_OPENING = 0x0800,
31 ZFCP_ERP_STEP_UNIT_CLOSING = 0x1000,
32 ZFCP_ERP_STEP_UNIT_OPENING = 0x2000,
35 enum zfcp_erp_act_type {
36 ZFCP_ERP_ACTION_REOPEN_UNIT = 1,
37 ZFCP_ERP_ACTION_REOPEN_PORT = 2,
38 ZFCP_ERP_ACTION_REOPEN_PORT_FORCED = 3,
39 ZFCP_ERP_ACTION_REOPEN_ADAPTER = 4,
42 enum zfcp_erp_act_state {
43 ZFCP_ERP_ACTION_RUNNING = 1,
44 ZFCP_ERP_ACTION_READY = 2,
47 enum zfcp_erp_act_result {
48 ZFCP_ERP_SUCCEEDED = 0,
50 ZFCP_ERP_CONTINUES = 2,
52 ZFCP_ERP_DISMISSED = 4,
56 static void zfcp_erp_adapter_block(struct zfcp_adapter *adapter, int mask)
58 zfcp_erp_modify_adapter_status(adapter, "erablk1", NULL,
59 ZFCP_STATUS_COMMON_UNBLOCKED | mask,
63 static int zfcp_erp_action_exists(struct zfcp_erp_action *act)
65 struct zfcp_erp_action *curr_act;
67 list_for_each_entry(curr_act, &act->adapter->erp_running_head, list)
69 return ZFCP_ERP_ACTION_RUNNING;
73 static void zfcp_erp_action_ready(struct zfcp_erp_action *act)
75 struct zfcp_adapter *adapter = act->adapter;
77 list_move(&act->list, &act->adapter->erp_ready_head);
78 zfcp_dbf_rec_action("erardy1", act);
79 wake_up(&adapter->erp_ready_wq);
80 zfcp_dbf_rec_thread("erardy2", adapter->dbf);
83 static void zfcp_erp_action_dismiss(struct zfcp_erp_action *act)
85 act->status |= ZFCP_STATUS_ERP_DISMISSED;
86 if (zfcp_erp_action_exists(act) == ZFCP_ERP_ACTION_RUNNING)
87 zfcp_erp_action_ready(act);
90 static void zfcp_erp_action_dismiss_unit(struct zfcp_unit *unit)
92 if (atomic_read(&unit->status) & ZFCP_STATUS_COMMON_ERP_INUSE)
93 zfcp_erp_action_dismiss(&unit->erp_action);
96 static void zfcp_erp_action_dismiss_port(struct zfcp_port *port)
98 struct zfcp_unit *unit;
100 if (atomic_read(&port->status) & ZFCP_STATUS_COMMON_ERP_INUSE)
101 zfcp_erp_action_dismiss(&port->erp_action);
103 read_lock(&port->unit_list_lock);
104 list_for_each_entry(unit, &port->unit_list, list)
105 zfcp_erp_action_dismiss_unit(unit);
106 read_unlock(&port->unit_list_lock);
110 static void zfcp_erp_action_dismiss_adapter(struct zfcp_adapter *adapter)
112 struct zfcp_port *port;
114 if (atomic_read(&adapter->status) & ZFCP_STATUS_COMMON_ERP_INUSE)
115 zfcp_erp_action_dismiss(&adapter->erp_action);
117 read_lock(&adapter->port_list_lock);
118 list_for_each_entry(port, &adapter->port_list, list)
119 zfcp_erp_action_dismiss_port(port);
120 read_unlock(&adapter->port_list_lock);
124 static int zfcp_erp_required_act(int want, struct zfcp_adapter *adapter,
125 struct zfcp_port *port,
126 struct zfcp_unit *unit)
129 int u_status, p_status, a_status;
132 case ZFCP_ERP_ACTION_REOPEN_UNIT:
133 u_status = atomic_read(&unit->status);
134 if (u_status & ZFCP_STATUS_COMMON_ERP_INUSE)
136 p_status = atomic_read(&port->status);
137 if (!(p_status & ZFCP_STATUS_COMMON_RUNNING) ||
138 p_status & ZFCP_STATUS_COMMON_ERP_FAILED)
140 if (!(p_status & ZFCP_STATUS_COMMON_UNBLOCKED))
141 need = ZFCP_ERP_ACTION_REOPEN_PORT;
143 case ZFCP_ERP_ACTION_REOPEN_PORT:
144 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
145 p_status = atomic_read(&port->status);
146 if (p_status & ZFCP_STATUS_COMMON_ERP_INUSE)
148 a_status = atomic_read(&adapter->status);
149 if (!(a_status & ZFCP_STATUS_COMMON_RUNNING) ||
150 a_status & ZFCP_STATUS_COMMON_ERP_FAILED)
152 if (!(a_status & ZFCP_STATUS_COMMON_UNBLOCKED))
153 need = ZFCP_ERP_ACTION_REOPEN_ADAPTER;
155 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
156 a_status = atomic_read(&adapter->status);
157 if (a_status & ZFCP_STATUS_COMMON_ERP_INUSE)
159 if (!(a_status & ZFCP_STATUS_COMMON_RUNNING) &&
160 !(a_status & ZFCP_STATUS_COMMON_OPEN))
161 return 0; /* shutdown requested for closed adapter */
167 static struct zfcp_erp_action *zfcp_erp_setup_act(int need,
168 struct zfcp_adapter *adapter,
169 struct zfcp_port *port,
170 struct zfcp_unit *unit)
172 struct zfcp_erp_action *erp_action;
176 case ZFCP_ERP_ACTION_REOPEN_UNIT:
177 if (!get_device(&unit->sysfs_device))
179 atomic_set_mask(ZFCP_STATUS_COMMON_ERP_INUSE, &unit->status);
180 erp_action = &unit->erp_action;
181 if (!(atomic_read(&unit->status) & ZFCP_STATUS_COMMON_RUNNING))
182 status = ZFCP_STATUS_ERP_CLOSE_ONLY;
185 case ZFCP_ERP_ACTION_REOPEN_PORT:
186 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
187 if (!get_device(&port->sysfs_device))
189 zfcp_erp_action_dismiss_port(port);
190 atomic_set_mask(ZFCP_STATUS_COMMON_ERP_INUSE, &port->status);
191 erp_action = &port->erp_action;
192 if (!(atomic_read(&port->status) & ZFCP_STATUS_COMMON_RUNNING))
193 status = ZFCP_STATUS_ERP_CLOSE_ONLY;
196 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
197 kref_get(&adapter->ref);
198 zfcp_erp_action_dismiss_adapter(adapter);
199 atomic_set_mask(ZFCP_STATUS_COMMON_ERP_INUSE, &adapter->status);
200 erp_action = &adapter->erp_action;
201 if (!(atomic_read(&adapter->status) &
202 ZFCP_STATUS_COMMON_RUNNING))
203 status = ZFCP_STATUS_ERP_CLOSE_ONLY;
210 memset(erp_action, 0, sizeof(struct zfcp_erp_action));
211 erp_action->adapter = adapter;
212 erp_action->port = port;
213 erp_action->unit = unit;
214 erp_action->action = need;
215 erp_action->status = status;
220 static int zfcp_erp_action_enqueue(int want, struct zfcp_adapter *adapter,
221 struct zfcp_port *port,
222 struct zfcp_unit *unit, char *id, void *ref)
224 int retval = 1, need;
225 struct zfcp_erp_action *act = NULL;
227 if (!adapter->erp_thread)
230 need = zfcp_erp_required_act(want, adapter, port, unit);
234 atomic_set_mask(ZFCP_STATUS_ADAPTER_ERP_PENDING, &adapter->status);
235 act = zfcp_erp_setup_act(need, adapter, port, unit);
238 ++adapter->erp_total_count;
239 list_add_tail(&act->list, &adapter->erp_ready_head);
240 wake_up(&adapter->erp_ready_wq);
241 zfcp_dbf_rec_thread("eracte1", adapter->dbf);
244 zfcp_dbf_rec_trigger(id, ref, want, need, act, adapter, port, unit);
248 static int _zfcp_erp_adapter_reopen(struct zfcp_adapter *adapter,
249 int clear_mask, char *id, void *ref)
251 zfcp_erp_adapter_block(adapter, clear_mask);
252 zfcp_scsi_schedule_rports_block(adapter);
254 /* ensure propagation of failed status to new devices */
255 if (atomic_read(&adapter->status) & ZFCP_STATUS_COMMON_ERP_FAILED) {
256 zfcp_erp_adapter_failed(adapter, "erareo1", NULL);
259 return zfcp_erp_action_enqueue(ZFCP_ERP_ACTION_REOPEN_ADAPTER,
260 adapter, NULL, NULL, id, ref);
264 * zfcp_erp_adapter_reopen - Reopen adapter.
265 * @adapter: Adapter to reopen.
266 * @clear: Status flags to clear.
267 * @id: Id for debug trace event.
268 * @ref: Reference for debug trace event.
270 void zfcp_erp_adapter_reopen(struct zfcp_adapter *adapter, int clear,
275 zfcp_erp_adapter_block(adapter, clear);
276 zfcp_scsi_schedule_rports_block(adapter);
278 write_lock_irqsave(&adapter->erp_lock, flags);
279 if (atomic_read(&adapter->status) & ZFCP_STATUS_COMMON_ERP_FAILED)
280 zfcp_erp_adapter_failed(adapter, "erareo1", NULL);
282 zfcp_erp_action_enqueue(ZFCP_ERP_ACTION_REOPEN_ADAPTER, adapter,
283 NULL, NULL, id, ref);
284 write_unlock_irqrestore(&adapter->erp_lock, flags);
288 * zfcp_erp_adapter_shutdown - Shutdown adapter.
289 * @adapter: Adapter to shut down.
290 * @clear: Status flags to clear.
291 * @id: Id for debug trace event.
292 * @ref: Reference for debug trace event.
294 void zfcp_erp_adapter_shutdown(struct zfcp_adapter *adapter, int clear,
297 int flags = ZFCP_STATUS_COMMON_RUNNING | ZFCP_STATUS_COMMON_ERP_FAILED;
298 zfcp_erp_adapter_reopen(adapter, clear | flags, id, ref);
302 * zfcp_erp_port_shutdown - Shutdown port
303 * @port: Port to shut down.
304 * @clear: Status flags to clear.
305 * @id: Id for debug trace event.
306 * @ref: Reference for debug trace event.
308 void zfcp_erp_port_shutdown(struct zfcp_port *port, int clear, char *id,
311 int flags = ZFCP_STATUS_COMMON_RUNNING | ZFCP_STATUS_COMMON_ERP_FAILED;
312 zfcp_erp_port_reopen(port, clear | flags, id, ref);
316 * zfcp_erp_unit_shutdown - Shutdown unit
317 * @unit: Unit to shut down.
318 * @clear: Status flags to clear.
319 * @id: Id for debug trace event.
320 * @ref: Reference for debug trace event.
322 void zfcp_erp_unit_shutdown(struct zfcp_unit *unit, int clear, char *id,
325 int flags = ZFCP_STATUS_COMMON_RUNNING | ZFCP_STATUS_COMMON_ERP_FAILED;
326 zfcp_erp_unit_reopen(unit, clear | flags, id, ref);
329 static void zfcp_erp_port_block(struct zfcp_port *port, int clear)
331 zfcp_erp_modify_port_status(port, "erpblk1", NULL,
332 ZFCP_STATUS_COMMON_UNBLOCKED | clear,
336 static void _zfcp_erp_port_forced_reopen(struct zfcp_port *port,
337 int clear, char *id, void *ref)
339 zfcp_erp_port_block(port, clear);
340 zfcp_scsi_schedule_rport_block(port);
342 if (atomic_read(&port->status) & ZFCP_STATUS_COMMON_ERP_FAILED)
345 zfcp_erp_action_enqueue(ZFCP_ERP_ACTION_REOPEN_PORT_FORCED,
346 port->adapter, port, NULL, id, ref);
350 * zfcp_erp_port_forced_reopen - Forced close of port and open again
351 * @port: Port to force close and to reopen.
352 * @id: Id for debug trace event.
353 * @ref: Reference for debug trace event.
355 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, ref);
363 write_unlock_irqrestore(&adapter->erp_lock, flags);
366 static int _zfcp_erp_port_reopen(struct zfcp_port *port, int clear, char *id,
369 zfcp_erp_port_block(port, clear);
370 zfcp_scsi_schedule_rport_block(port);
372 if (atomic_read(&port->status) & ZFCP_STATUS_COMMON_ERP_FAILED) {
373 /* ensure propagation of failed status to new devices */
374 zfcp_erp_port_failed(port, "erpreo1", NULL);
378 return zfcp_erp_action_enqueue(ZFCP_ERP_ACTION_REOPEN_PORT,
379 port->adapter, port, NULL, id, ref);
383 * zfcp_erp_port_reopen - trigger remote port recovery
384 * @port: port to recover
385 * @clear_mask: flags in port status to be cleared
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, void *ref)
393 struct zfcp_adapter *adapter = port->adapter;
395 write_lock_irqsave(&adapter->erp_lock, flags);
396 retval = _zfcp_erp_port_reopen(port, clear, id, ref);
397 write_unlock_irqrestore(&adapter->erp_lock, flags);
402 static void zfcp_erp_unit_block(struct zfcp_unit *unit, int clear_mask)
404 zfcp_erp_modify_unit_status(unit, "erublk1", NULL,
405 ZFCP_STATUS_COMMON_UNBLOCKED | clear_mask,
409 static void _zfcp_erp_unit_reopen(struct zfcp_unit *unit, int clear, char *id,
412 struct zfcp_adapter *adapter = unit->port->adapter;
414 zfcp_erp_unit_block(unit, clear);
416 if (atomic_read(&unit->status) & ZFCP_STATUS_COMMON_ERP_FAILED)
419 zfcp_erp_action_enqueue(ZFCP_ERP_ACTION_REOPEN_UNIT,
420 adapter, unit->port, unit, id, ref);
424 * zfcp_erp_unit_reopen - initiate reopen of a unit
425 * @unit: unit to be reopened
426 * @clear_mask: specifies flags in unit status to be cleared
427 * Return: 0 on success, < 0 on error
429 void zfcp_erp_unit_reopen(struct zfcp_unit *unit, int clear, char *id,
433 struct zfcp_port *port = unit->port;
434 struct zfcp_adapter *adapter = port->adapter;
436 write_lock_irqsave(&adapter->erp_lock, flags);
437 _zfcp_erp_unit_reopen(unit, clear, id, ref);
438 write_unlock_irqrestore(&adapter->erp_lock, flags);
441 static int status_change_set(unsigned long mask, atomic_t *status)
443 return (atomic_read(status) ^ mask) & mask;
446 static int status_change_clear(unsigned long mask, atomic_t *status)
448 return atomic_read(status) & mask;
451 static void zfcp_erp_adapter_unblock(struct zfcp_adapter *adapter)
453 if (status_change_set(ZFCP_STATUS_COMMON_UNBLOCKED, &adapter->status))
454 zfcp_dbf_rec_adapter("eraubl1", NULL, adapter->dbf);
455 atomic_set_mask(ZFCP_STATUS_COMMON_UNBLOCKED, &adapter->status);
458 static void zfcp_erp_port_unblock(struct zfcp_port *port)
460 if (status_change_set(ZFCP_STATUS_COMMON_UNBLOCKED, &port->status))
461 zfcp_dbf_rec_port("erpubl1", NULL, port);
462 atomic_set_mask(ZFCP_STATUS_COMMON_UNBLOCKED, &port->status);
465 static void zfcp_erp_unit_unblock(struct zfcp_unit *unit)
467 if (status_change_set(ZFCP_STATUS_COMMON_UNBLOCKED, &unit->status))
468 zfcp_dbf_rec_unit("eruubl1", NULL, unit);
469 atomic_set_mask(ZFCP_STATUS_COMMON_UNBLOCKED, &unit->status);
472 static void zfcp_erp_action_to_running(struct zfcp_erp_action *erp_action)
474 list_move(&erp_action->list, &erp_action->adapter->erp_running_head);
475 zfcp_dbf_rec_action("erator1", erp_action);
478 static void zfcp_erp_strategy_check_fsfreq(struct zfcp_erp_action *act)
480 struct zfcp_adapter *adapter = act->adapter;
481 struct zfcp_fsf_req *req;
483 if (!act->fsf_req_id)
486 spin_lock(&adapter->req_list_lock);
487 req = zfcp_reqlist_find(adapter, act->fsf_req_id);
488 if (req && req->erp_action == act) {
489 if (act->status & (ZFCP_STATUS_ERP_DISMISSED |
490 ZFCP_STATUS_ERP_TIMEDOUT)) {
491 req->status |= ZFCP_STATUS_FSFREQ_DISMISSED;
492 zfcp_dbf_rec_action("erscf_1", act);
493 req->erp_action = NULL;
495 if (act->status & ZFCP_STATUS_ERP_TIMEDOUT)
496 zfcp_dbf_rec_action("erscf_2", act);
497 if (req->status & ZFCP_STATUS_FSFREQ_DISMISSED)
501 spin_unlock(&adapter->req_list_lock);
505 * zfcp_erp_notify - Trigger ERP action.
506 * @erp_action: ERP action to continue.
507 * @set_mask: ERP action status flags to set.
509 void zfcp_erp_notify(struct zfcp_erp_action *erp_action, unsigned long set_mask)
511 struct zfcp_adapter *adapter = erp_action->adapter;
514 write_lock_irqsave(&adapter->erp_lock, flags);
515 if (zfcp_erp_action_exists(erp_action) == ZFCP_ERP_ACTION_RUNNING) {
516 erp_action->status |= set_mask;
517 zfcp_erp_action_ready(erp_action);
519 write_unlock_irqrestore(&adapter->erp_lock, flags);
523 * zfcp_erp_timeout_handler - Trigger ERP action from timed out ERP request
524 * @data: ERP action (from timer data)
526 void zfcp_erp_timeout_handler(unsigned long data)
528 struct zfcp_erp_action *act = (struct zfcp_erp_action *) data;
529 zfcp_erp_notify(act, ZFCP_STATUS_ERP_TIMEDOUT);
532 static void zfcp_erp_memwait_handler(unsigned long data)
534 zfcp_erp_notify((struct zfcp_erp_action *)data, 0);
537 static void zfcp_erp_strategy_memwait(struct zfcp_erp_action *erp_action)
539 init_timer(&erp_action->timer);
540 erp_action->timer.function = zfcp_erp_memwait_handler;
541 erp_action->timer.data = (unsigned long) erp_action;
542 erp_action->timer.expires = jiffies + HZ;
543 add_timer(&erp_action->timer);
546 static void _zfcp_erp_port_reopen_all(struct zfcp_adapter *adapter,
547 int clear, char *id, void *ref)
549 struct zfcp_port *port;
551 read_lock(&adapter->port_list_lock);
552 list_for_each_entry(port, &adapter->port_list, list)
553 _zfcp_erp_port_reopen(port, clear, id, ref);
554 read_unlock(&adapter->port_list_lock);
557 static void _zfcp_erp_unit_reopen_all(struct zfcp_port *port, int clear,
560 struct zfcp_unit *unit;
562 read_lock(&port->unit_list_lock);
563 list_for_each_entry(unit, &port->unit_list, list)
564 _zfcp_erp_unit_reopen(unit, clear, id, ref);
565 read_unlock(&port->unit_list_lock);
568 static void zfcp_erp_strategy_followup_failed(struct zfcp_erp_action *act)
570 switch (act->action) {
571 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
572 _zfcp_erp_adapter_reopen(act->adapter, 0, "ersff_1", NULL);
574 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
575 _zfcp_erp_port_forced_reopen(act->port, 0, "ersff_2", NULL);
577 case ZFCP_ERP_ACTION_REOPEN_PORT:
578 _zfcp_erp_port_reopen(act->port, 0, "ersff_3", NULL);
580 case ZFCP_ERP_ACTION_REOPEN_UNIT:
581 _zfcp_erp_unit_reopen(act->unit, 0, "ersff_4", NULL);
586 static void zfcp_erp_strategy_followup_success(struct zfcp_erp_action *act)
588 switch (act->action) {
589 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
590 _zfcp_erp_port_reopen_all(act->adapter, 0, "ersfs_1", NULL);
592 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
593 _zfcp_erp_port_reopen(act->port, 0, "ersfs_2", NULL);
595 case ZFCP_ERP_ACTION_REOPEN_PORT:
596 _zfcp_erp_unit_reopen_all(act->port, 0, "ersfs_3", NULL);
601 static void zfcp_erp_wakeup(struct zfcp_adapter *adapter)
605 read_lock_irqsave(&adapter->erp_lock, flags);
606 if (list_empty(&adapter->erp_ready_head) &&
607 list_empty(&adapter->erp_running_head)) {
608 atomic_clear_mask(ZFCP_STATUS_ADAPTER_ERP_PENDING,
610 wake_up(&adapter->erp_done_wqh);
612 read_unlock_irqrestore(&adapter->erp_lock, flags);
615 static int zfcp_erp_adapter_strategy_open_qdio(struct zfcp_erp_action *act)
617 struct zfcp_qdio *qdio = act->adapter->qdio;
619 if (zfcp_qdio_open(qdio))
620 return ZFCP_ERP_FAILED;
621 init_waitqueue_head(&qdio->req_q_wq);
622 atomic_set_mask(ZFCP_STATUS_ADAPTER_QDIOUP, &act->adapter->status);
623 return ZFCP_ERP_SUCCEEDED;
626 static void zfcp_erp_enqueue_ptp_port(struct zfcp_adapter *adapter)
628 struct zfcp_port *port;
629 port = zfcp_port_enqueue(adapter, adapter->peer_wwpn, 0,
631 if (IS_ERR(port)) /* error or port already attached */
633 _zfcp_erp_port_reopen(port, 0, "ereptp1", NULL);
636 static int zfcp_erp_adapter_strat_fsf_xconf(struct zfcp_erp_action *erp_action)
640 struct zfcp_adapter *adapter = erp_action->adapter;
642 atomic_clear_mask(ZFCP_STATUS_ADAPTER_XCONFIG_OK, &adapter->status);
644 for (retries = 7; retries; retries--) {
645 atomic_clear_mask(ZFCP_STATUS_ADAPTER_HOST_CON_INIT,
647 write_lock_irq(&adapter->erp_lock);
648 zfcp_erp_action_to_running(erp_action);
649 write_unlock_irq(&adapter->erp_lock);
650 if (zfcp_fsf_exchange_config_data(erp_action)) {
651 atomic_clear_mask(ZFCP_STATUS_ADAPTER_HOST_CON_INIT,
653 return ZFCP_ERP_FAILED;
656 zfcp_dbf_rec_thread_lock("erasfx1", adapter->dbf);
657 wait_event(adapter->erp_ready_wq,
658 !list_empty(&adapter->erp_ready_head));
659 zfcp_dbf_rec_thread_lock("erasfx2", adapter->dbf);
660 if (erp_action->status & ZFCP_STATUS_ERP_TIMEDOUT)
663 if (!(atomic_read(&adapter->status) &
664 ZFCP_STATUS_ADAPTER_HOST_CON_INIT))
671 atomic_clear_mask(ZFCP_STATUS_ADAPTER_HOST_CON_INIT,
674 if (!(atomic_read(&adapter->status) & ZFCP_STATUS_ADAPTER_XCONFIG_OK))
675 return ZFCP_ERP_FAILED;
677 if (fc_host_port_type(adapter->scsi_host) == FC_PORTTYPE_PTP)
678 zfcp_erp_enqueue_ptp_port(adapter);
680 return ZFCP_ERP_SUCCEEDED;
683 static int zfcp_erp_adapter_strategy_open_fsf_xport(struct zfcp_erp_action *act)
686 struct zfcp_adapter *adapter = act->adapter;
688 write_lock_irq(&adapter->erp_lock);
689 zfcp_erp_action_to_running(act);
690 write_unlock_irq(&adapter->erp_lock);
692 ret = zfcp_fsf_exchange_port_data(act);
693 if (ret == -EOPNOTSUPP)
694 return ZFCP_ERP_SUCCEEDED;
696 return ZFCP_ERP_FAILED;
698 zfcp_dbf_rec_thread_lock("erasox1", adapter->dbf);
699 wait_event(adapter->erp_ready_wq,
700 !list_empty(&adapter->erp_ready_head));
701 zfcp_dbf_rec_thread_lock("erasox2", adapter->dbf);
702 if (act->status & ZFCP_STATUS_ERP_TIMEDOUT)
703 return ZFCP_ERP_FAILED;
705 return ZFCP_ERP_SUCCEEDED;
708 static int zfcp_erp_adapter_strategy_open_fsf(struct zfcp_erp_action *act)
710 if (zfcp_erp_adapter_strat_fsf_xconf(act) == ZFCP_ERP_FAILED)
711 return ZFCP_ERP_FAILED;
713 if (zfcp_erp_adapter_strategy_open_fsf_xport(act) == ZFCP_ERP_FAILED)
714 return ZFCP_ERP_FAILED;
716 atomic_set(&act->adapter->stat_miss, 16);
717 if (zfcp_status_read_refill(act->adapter))
718 return ZFCP_ERP_FAILED;
720 return ZFCP_ERP_SUCCEEDED;
723 static void zfcp_erp_adapter_strategy_close(struct zfcp_erp_action *act)
725 struct zfcp_adapter *adapter = act->adapter;
727 /* close queues to ensure that buffers are not accessed by adapter */
728 zfcp_qdio_close(adapter->qdio);
729 zfcp_fsf_req_dismiss_all(adapter);
730 adapter->fsf_req_seq_no = 0;
731 zfcp_fc_wka_ports_force_offline(adapter->gs);
732 /* all ports and units are closed */
733 zfcp_erp_modify_adapter_status(adapter, "erascl1", NULL,
734 ZFCP_STATUS_COMMON_OPEN, ZFCP_CLEAR);
736 atomic_clear_mask(ZFCP_STATUS_ADAPTER_XCONFIG_OK |
737 ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED, &adapter->status);
740 static int zfcp_erp_adapter_strategy_open(struct zfcp_erp_action *act)
742 struct zfcp_adapter *adapter = act->adapter;
744 if (zfcp_erp_adapter_strategy_open_qdio(act)) {
745 atomic_clear_mask(ZFCP_STATUS_ADAPTER_XCONFIG_OK |
746 ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED,
748 return ZFCP_ERP_FAILED;
751 if (zfcp_erp_adapter_strategy_open_fsf(act)) {
752 zfcp_erp_adapter_strategy_close(act);
753 return ZFCP_ERP_FAILED;
756 atomic_set_mask(ZFCP_STATUS_COMMON_OPEN, &adapter->status);
758 return ZFCP_ERP_SUCCEEDED;
761 static int zfcp_erp_adapter_strategy(struct zfcp_erp_action *act)
763 struct zfcp_adapter *adapter = act->adapter;
765 if (atomic_read(&adapter->status) & ZFCP_STATUS_COMMON_OPEN) {
766 zfcp_erp_adapter_strategy_close(act);
767 if (act->status & ZFCP_STATUS_ERP_CLOSE_ONLY)
768 return ZFCP_ERP_EXIT;
771 if (zfcp_erp_adapter_strategy_open(act)) {
773 return ZFCP_ERP_FAILED;
776 return ZFCP_ERP_SUCCEEDED;
779 static int zfcp_erp_port_forced_strategy_close(struct zfcp_erp_action *act)
783 retval = zfcp_fsf_close_physical_port(act);
784 if (retval == -ENOMEM)
785 return ZFCP_ERP_NOMEM;
786 act->step = ZFCP_ERP_STEP_PHYS_PORT_CLOSING;
788 return ZFCP_ERP_FAILED;
790 return ZFCP_ERP_CONTINUES;
793 static void zfcp_erp_port_strategy_clearstati(struct zfcp_port *port)
795 atomic_clear_mask(ZFCP_STATUS_COMMON_ACCESS_DENIED, &port->status);
798 static int zfcp_erp_port_forced_strategy(struct zfcp_erp_action *erp_action)
800 struct zfcp_port *port = erp_action->port;
801 int status = atomic_read(&port->status);
803 switch (erp_action->step) {
804 case ZFCP_ERP_STEP_UNINITIALIZED:
805 zfcp_erp_port_strategy_clearstati(port);
806 if ((status & ZFCP_STATUS_PORT_PHYS_OPEN) &&
807 (status & ZFCP_STATUS_COMMON_OPEN))
808 return zfcp_erp_port_forced_strategy_close(erp_action);
810 return ZFCP_ERP_FAILED;
812 case ZFCP_ERP_STEP_PHYS_PORT_CLOSING:
813 if (!(status & ZFCP_STATUS_PORT_PHYS_OPEN))
814 return ZFCP_ERP_SUCCEEDED;
816 return ZFCP_ERP_FAILED;
819 static int zfcp_erp_port_strategy_close(struct zfcp_erp_action *erp_action)
823 retval = zfcp_fsf_close_port(erp_action);
824 if (retval == -ENOMEM)
825 return ZFCP_ERP_NOMEM;
826 erp_action->step = ZFCP_ERP_STEP_PORT_CLOSING;
828 return ZFCP_ERP_FAILED;
829 return ZFCP_ERP_CONTINUES;
832 static int zfcp_erp_port_strategy_open_port(struct zfcp_erp_action *erp_action)
836 retval = zfcp_fsf_open_port(erp_action);
837 if (retval == -ENOMEM)
838 return ZFCP_ERP_NOMEM;
839 erp_action->step = ZFCP_ERP_STEP_PORT_OPENING;
841 return ZFCP_ERP_FAILED;
842 return ZFCP_ERP_CONTINUES;
845 static int zfcp_erp_open_ptp_port(struct zfcp_erp_action *act)
847 struct zfcp_adapter *adapter = act->adapter;
848 struct zfcp_port *port = act->port;
850 if (port->wwpn != adapter->peer_wwpn) {
851 zfcp_erp_port_failed(port, "eroptp1", NULL);
852 return ZFCP_ERP_FAILED;
854 port->d_id = adapter->peer_d_id;
855 return zfcp_erp_port_strategy_open_port(act);
858 static int zfcp_erp_port_strategy_open_common(struct zfcp_erp_action *act)
860 struct zfcp_adapter *adapter = act->adapter;
861 struct zfcp_port *port = act->port;
862 int p_status = atomic_read(&port->status);
865 case ZFCP_ERP_STEP_UNINITIALIZED:
866 case ZFCP_ERP_STEP_PHYS_PORT_CLOSING:
867 case ZFCP_ERP_STEP_PORT_CLOSING:
868 if (fc_host_port_type(adapter->scsi_host) == FC_PORTTYPE_PTP)
869 return zfcp_erp_open_ptp_port(act);
871 zfcp_fc_trigger_did_lookup(port);
872 return ZFCP_ERP_EXIT;
874 return zfcp_erp_port_strategy_open_port(act);
876 case ZFCP_ERP_STEP_PORT_OPENING:
877 /* D_ID might have changed during open */
878 if (p_status & ZFCP_STATUS_COMMON_OPEN) {
880 zfcp_fc_trigger_did_lookup(port);
881 return ZFCP_ERP_EXIT;
883 return ZFCP_ERP_SUCCEEDED;
885 if (port->d_id && !(p_status & ZFCP_STATUS_COMMON_NOESC)) {
887 _zfcp_erp_port_reopen(port, 0, "erpsoc1", NULL);
888 return ZFCP_ERP_EXIT;
890 /* fall through otherwise */
892 return ZFCP_ERP_FAILED;
895 static int zfcp_erp_port_strategy(struct zfcp_erp_action *erp_action)
897 struct zfcp_port *port = erp_action->port;
898 int p_status = atomic_read(&port->status);
900 if ((p_status & ZFCP_STATUS_COMMON_NOESC) &&
901 !(p_status & ZFCP_STATUS_COMMON_OPEN))
902 goto close_init_done;
904 switch (erp_action->step) {
905 case ZFCP_ERP_STEP_UNINITIALIZED:
906 zfcp_erp_port_strategy_clearstati(port);
907 if (p_status & ZFCP_STATUS_COMMON_OPEN)
908 return zfcp_erp_port_strategy_close(erp_action);
911 case ZFCP_ERP_STEP_PORT_CLOSING:
912 if (p_status & ZFCP_STATUS_COMMON_OPEN)
913 return ZFCP_ERP_FAILED;
918 if (erp_action->status & ZFCP_STATUS_ERP_CLOSE_ONLY)
919 return ZFCP_ERP_EXIT;
921 return zfcp_erp_port_strategy_open_common(erp_action);
924 static void zfcp_erp_unit_strategy_clearstati(struct zfcp_unit *unit)
926 atomic_clear_mask(ZFCP_STATUS_COMMON_ACCESS_DENIED |
927 ZFCP_STATUS_UNIT_SHARED |
928 ZFCP_STATUS_UNIT_READONLY,
932 static int zfcp_erp_unit_strategy_close(struct zfcp_erp_action *erp_action)
934 int retval = zfcp_fsf_close_unit(erp_action);
935 if (retval == -ENOMEM)
936 return ZFCP_ERP_NOMEM;
937 erp_action->step = ZFCP_ERP_STEP_UNIT_CLOSING;
939 return ZFCP_ERP_FAILED;
940 return ZFCP_ERP_CONTINUES;
943 static int zfcp_erp_unit_strategy_open(struct zfcp_erp_action *erp_action)
945 int retval = zfcp_fsf_open_unit(erp_action);
946 if (retval == -ENOMEM)
947 return ZFCP_ERP_NOMEM;
948 erp_action->step = ZFCP_ERP_STEP_UNIT_OPENING;
950 return ZFCP_ERP_FAILED;
951 return ZFCP_ERP_CONTINUES;
954 static int zfcp_erp_unit_strategy(struct zfcp_erp_action *erp_action)
956 struct zfcp_unit *unit = erp_action->unit;
958 switch (erp_action->step) {
959 case ZFCP_ERP_STEP_UNINITIALIZED:
960 zfcp_erp_unit_strategy_clearstati(unit);
961 if (atomic_read(&unit->status) & ZFCP_STATUS_COMMON_OPEN)
962 return zfcp_erp_unit_strategy_close(erp_action);
963 /* already closed, fall through */
964 case ZFCP_ERP_STEP_UNIT_CLOSING:
965 if (atomic_read(&unit->status) & ZFCP_STATUS_COMMON_OPEN)
966 return ZFCP_ERP_FAILED;
967 if (erp_action->status & ZFCP_STATUS_ERP_CLOSE_ONLY)
968 return ZFCP_ERP_EXIT;
969 return zfcp_erp_unit_strategy_open(erp_action);
971 case ZFCP_ERP_STEP_UNIT_OPENING:
972 if (atomic_read(&unit->status) & ZFCP_STATUS_COMMON_OPEN)
973 return ZFCP_ERP_SUCCEEDED;
975 return ZFCP_ERP_FAILED;
978 static int zfcp_erp_strategy_check_unit(struct zfcp_unit *unit, int result)
981 case ZFCP_ERP_SUCCEEDED :
982 atomic_set(&unit->erp_counter, 0);
983 zfcp_erp_unit_unblock(unit);
985 case ZFCP_ERP_FAILED :
986 atomic_inc(&unit->erp_counter);
987 if (atomic_read(&unit->erp_counter) > ZFCP_MAX_ERPS) {
988 dev_err(&unit->port->adapter->ccw_device->dev,
989 "ERP failed for unit 0x%016Lx on "
991 (unsigned long long)unit->fcp_lun,
992 (unsigned long long)unit->port->wwpn);
993 zfcp_erp_unit_failed(unit, "erusck1", NULL);
998 if (atomic_read(&unit->status) & ZFCP_STATUS_COMMON_ERP_FAILED) {
999 zfcp_erp_unit_block(unit, 0);
1000 result = ZFCP_ERP_EXIT;
1005 static int zfcp_erp_strategy_check_port(struct zfcp_port *port, int result)
1008 case ZFCP_ERP_SUCCEEDED :
1009 atomic_set(&port->erp_counter, 0);
1010 zfcp_erp_port_unblock(port);
1013 case ZFCP_ERP_FAILED :
1014 if (atomic_read(&port->status) & ZFCP_STATUS_COMMON_NOESC) {
1015 zfcp_erp_port_block(port, 0);
1016 result = ZFCP_ERP_EXIT;
1018 atomic_inc(&port->erp_counter);
1019 if (atomic_read(&port->erp_counter) > ZFCP_MAX_ERPS) {
1020 dev_err(&port->adapter->ccw_device->dev,
1021 "ERP failed for remote port 0x%016Lx\n",
1022 (unsigned long long)port->wwpn);
1023 zfcp_erp_port_failed(port, "erpsck1", NULL);
1028 if (atomic_read(&port->status) & ZFCP_STATUS_COMMON_ERP_FAILED) {
1029 zfcp_erp_port_block(port, 0);
1030 result = ZFCP_ERP_EXIT;
1035 static int zfcp_erp_strategy_check_adapter(struct zfcp_adapter *adapter,
1039 case ZFCP_ERP_SUCCEEDED :
1040 atomic_set(&adapter->erp_counter, 0);
1041 zfcp_erp_adapter_unblock(adapter);
1044 case ZFCP_ERP_FAILED :
1045 atomic_inc(&adapter->erp_counter);
1046 if (atomic_read(&adapter->erp_counter) > ZFCP_MAX_ERPS) {
1047 dev_err(&adapter->ccw_device->dev,
1048 "ERP cannot recover an error "
1049 "on the FCP device\n");
1050 zfcp_erp_adapter_failed(adapter, "erasck1", NULL);
1055 if (atomic_read(&adapter->status) & ZFCP_STATUS_COMMON_ERP_FAILED) {
1056 zfcp_erp_adapter_block(adapter, 0);
1057 result = ZFCP_ERP_EXIT;
1062 static int zfcp_erp_strategy_check_target(struct zfcp_erp_action *erp_action,
1065 struct zfcp_adapter *adapter = erp_action->adapter;
1066 struct zfcp_port *port = erp_action->port;
1067 struct zfcp_unit *unit = erp_action->unit;
1069 switch (erp_action->action) {
1071 case ZFCP_ERP_ACTION_REOPEN_UNIT:
1072 result = zfcp_erp_strategy_check_unit(unit, result);
1075 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
1076 case ZFCP_ERP_ACTION_REOPEN_PORT:
1077 result = zfcp_erp_strategy_check_port(port, result);
1080 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
1081 result = zfcp_erp_strategy_check_adapter(adapter, result);
1087 static int zfcp_erp_strat_change_det(atomic_t *target_status, u32 erp_status)
1089 int status = atomic_read(target_status);
1091 if ((status & ZFCP_STATUS_COMMON_RUNNING) &&
1092 (erp_status & ZFCP_STATUS_ERP_CLOSE_ONLY))
1093 return 1; /* take it online */
1095 if (!(status & ZFCP_STATUS_COMMON_RUNNING) &&
1096 !(erp_status & ZFCP_STATUS_ERP_CLOSE_ONLY))
1097 return 1; /* take it offline */
1102 static int zfcp_erp_strategy_statechange(struct zfcp_erp_action *act, int ret)
1104 int action = act->action;
1105 struct zfcp_adapter *adapter = act->adapter;
1106 struct zfcp_port *port = act->port;
1107 struct zfcp_unit *unit = act->unit;
1108 u32 erp_status = act->status;
1111 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
1112 if (zfcp_erp_strat_change_det(&adapter->status, erp_status)) {
1113 _zfcp_erp_adapter_reopen(adapter,
1114 ZFCP_STATUS_COMMON_ERP_FAILED,
1116 return ZFCP_ERP_EXIT;
1120 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
1121 case ZFCP_ERP_ACTION_REOPEN_PORT:
1122 if (zfcp_erp_strat_change_det(&port->status, erp_status)) {
1123 _zfcp_erp_port_reopen(port,
1124 ZFCP_STATUS_COMMON_ERP_FAILED,
1126 return ZFCP_ERP_EXIT;
1130 case ZFCP_ERP_ACTION_REOPEN_UNIT:
1131 if (zfcp_erp_strat_change_det(&unit->status, erp_status)) {
1132 _zfcp_erp_unit_reopen(unit,
1133 ZFCP_STATUS_COMMON_ERP_FAILED,
1135 return ZFCP_ERP_EXIT;
1142 static void zfcp_erp_action_dequeue(struct zfcp_erp_action *erp_action)
1144 struct zfcp_adapter *adapter = erp_action->adapter;
1146 adapter->erp_total_count--;
1147 if (erp_action->status & ZFCP_STATUS_ERP_LOWMEM) {
1148 adapter->erp_low_mem_count--;
1149 erp_action->status &= ~ZFCP_STATUS_ERP_LOWMEM;
1152 list_del(&erp_action->list);
1153 zfcp_dbf_rec_action("eractd1", erp_action);
1155 switch (erp_action->action) {
1156 case ZFCP_ERP_ACTION_REOPEN_UNIT:
1157 atomic_clear_mask(ZFCP_STATUS_COMMON_ERP_INUSE,
1158 &erp_action->unit->status);
1161 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
1162 case ZFCP_ERP_ACTION_REOPEN_PORT:
1163 atomic_clear_mask(ZFCP_STATUS_COMMON_ERP_INUSE,
1164 &erp_action->port->status);
1167 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
1168 atomic_clear_mask(ZFCP_STATUS_COMMON_ERP_INUSE,
1169 &erp_action->adapter->status);
1174 static void zfcp_erp_action_cleanup(struct zfcp_erp_action *act, int result)
1176 struct zfcp_adapter *adapter = act->adapter;
1177 struct zfcp_port *port = act->port;
1178 struct zfcp_unit *unit = act->unit;
1180 switch (act->action) {
1181 case ZFCP_ERP_ACTION_REOPEN_UNIT:
1182 if ((result == ZFCP_ERP_SUCCEEDED) && !unit->device) {
1183 get_device(&unit->sysfs_device);
1184 if (scsi_queue_work(unit->port->adapter->scsi_host,
1185 &unit->scsi_work) <= 0)
1186 put_device(&unit->sysfs_device);
1188 put_device(&unit->sysfs_device);
1191 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
1192 case ZFCP_ERP_ACTION_REOPEN_PORT:
1193 if (result == ZFCP_ERP_SUCCEEDED)
1194 zfcp_scsi_schedule_rport_register(port);
1195 put_device(&port->sysfs_device);
1198 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
1199 if (result == ZFCP_ERP_SUCCEEDED) {
1200 register_service_level(&adapter->service_level);
1201 queue_work(adapter->work_queue, &adapter->scan_work);
1203 unregister_service_level(&adapter->service_level);
1204 kref_put(&adapter->ref, zfcp_adapter_release);
1209 static int zfcp_erp_strategy_do_action(struct zfcp_erp_action *erp_action)
1211 switch (erp_action->action) {
1212 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
1213 return zfcp_erp_adapter_strategy(erp_action);
1214 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
1215 return zfcp_erp_port_forced_strategy(erp_action);
1216 case ZFCP_ERP_ACTION_REOPEN_PORT:
1217 return zfcp_erp_port_strategy(erp_action);
1218 case ZFCP_ERP_ACTION_REOPEN_UNIT:
1219 return zfcp_erp_unit_strategy(erp_action);
1221 return ZFCP_ERP_FAILED;
1224 static int zfcp_erp_strategy(struct zfcp_erp_action *erp_action)
1227 unsigned long flags;
1228 struct zfcp_adapter *adapter = erp_action->adapter;
1230 kref_get(&adapter->ref);
1232 write_lock_irqsave(&adapter->erp_lock, flags);
1233 zfcp_erp_strategy_check_fsfreq(erp_action);
1235 if (erp_action->status & ZFCP_STATUS_ERP_DISMISSED) {
1236 zfcp_erp_action_dequeue(erp_action);
1237 retval = ZFCP_ERP_DISMISSED;
1241 zfcp_erp_action_to_running(erp_action);
1243 /* no lock to allow for blocking operations */
1244 write_unlock_irqrestore(&adapter->erp_lock, flags);
1245 retval = zfcp_erp_strategy_do_action(erp_action);
1246 write_lock_irqsave(&adapter->erp_lock, flags);
1248 if (erp_action->status & ZFCP_STATUS_ERP_DISMISSED)
1249 retval = ZFCP_ERP_CONTINUES;
1252 case ZFCP_ERP_NOMEM:
1253 if (!(erp_action->status & ZFCP_STATUS_ERP_LOWMEM)) {
1254 ++adapter->erp_low_mem_count;
1255 erp_action->status |= ZFCP_STATUS_ERP_LOWMEM;
1257 if (adapter->erp_total_count == adapter->erp_low_mem_count)
1258 _zfcp_erp_adapter_reopen(adapter, 0, "erstgy1", NULL);
1260 zfcp_erp_strategy_memwait(erp_action);
1261 retval = ZFCP_ERP_CONTINUES;
1265 case ZFCP_ERP_CONTINUES:
1266 if (erp_action->status & ZFCP_STATUS_ERP_LOWMEM) {
1267 --adapter->erp_low_mem_count;
1268 erp_action->status &= ~ZFCP_STATUS_ERP_LOWMEM;
1273 retval = zfcp_erp_strategy_check_target(erp_action, retval);
1274 zfcp_erp_action_dequeue(erp_action);
1275 retval = zfcp_erp_strategy_statechange(erp_action, retval);
1276 if (retval == ZFCP_ERP_EXIT)
1278 if (retval == ZFCP_ERP_SUCCEEDED)
1279 zfcp_erp_strategy_followup_success(erp_action);
1280 if (retval == ZFCP_ERP_FAILED)
1281 zfcp_erp_strategy_followup_failed(erp_action);
1284 write_unlock_irqrestore(&adapter->erp_lock, flags);
1286 if (retval != ZFCP_ERP_CONTINUES)
1287 zfcp_erp_action_cleanup(erp_action, retval);
1289 kref_put(&adapter->ref, zfcp_adapter_release);
1293 static int zfcp_erp_thread(void *data)
1295 struct zfcp_adapter *adapter = (struct zfcp_adapter *) data;
1296 struct list_head *next;
1297 struct zfcp_erp_action *act;
1298 unsigned long flags;
1301 zfcp_dbf_rec_thread_lock("erthrd1", adapter->dbf);
1302 wait_event_interruptible(adapter->erp_ready_wq,
1303 !list_empty(&adapter->erp_ready_head) ||
1304 kthread_should_stop());
1305 zfcp_dbf_rec_thread_lock("erthrd2", adapter->dbf);
1307 if (kthread_should_stop())
1310 write_lock_irqsave(&adapter->erp_lock, flags);
1311 next = adapter->erp_ready_head.next;
1312 write_unlock_irqrestore(&adapter->erp_lock, flags);
1314 if (next != &adapter->erp_ready_head) {
1315 act = list_entry(next, struct zfcp_erp_action, list);
1317 /* there is more to come after dismission, no notify */
1318 if (zfcp_erp_strategy(act) != ZFCP_ERP_DISMISSED)
1319 zfcp_erp_wakeup(adapter);
1327 * zfcp_erp_thread_setup - Start ERP thread for adapter
1328 * @adapter: Adapter to start the ERP thread for
1330 * Returns 0 on success or error code from kernel_thread()
1332 int zfcp_erp_thread_setup(struct zfcp_adapter *adapter)
1334 struct task_struct *thread;
1336 thread = kthread_run(zfcp_erp_thread, adapter, "zfcperp%s",
1337 dev_name(&adapter->ccw_device->dev));
1338 if (IS_ERR(thread)) {
1339 dev_err(&adapter->ccw_device->dev,
1340 "Creating an ERP thread for the FCP device failed.\n");
1341 return PTR_ERR(thread);
1344 adapter->erp_thread = thread;
1349 * zfcp_erp_thread_kill - Stop ERP thread.
1350 * @adapter: Adapter where the ERP thread should be stopped.
1352 * The caller of this routine ensures that the specified adapter has
1353 * been shut down and that this operation has been completed. Thus,
1354 * there are no pending erp_actions which would need to be handled
1357 void zfcp_erp_thread_kill(struct zfcp_adapter *adapter)
1359 kthread_stop(adapter->erp_thread);
1360 adapter->erp_thread = NULL;
1361 WARN_ON(!list_empty(&adapter->erp_ready_head));
1362 WARN_ON(!list_empty(&adapter->erp_running_head));
1366 * zfcp_erp_adapter_failed - Set adapter status to failed.
1367 * @adapter: Failed adapter.
1368 * @id: Event id for debug trace.
1369 * @ref: Reference for debug trace.
1371 void zfcp_erp_adapter_failed(struct zfcp_adapter *adapter, char *id, void *ref)
1373 zfcp_erp_modify_adapter_status(adapter, id, ref,
1374 ZFCP_STATUS_COMMON_ERP_FAILED, ZFCP_SET);
1378 * zfcp_erp_port_failed - Set port status to failed.
1379 * @port: Failed port.
1380 * @id: Event id for debug trace.
1381 * @ref: Reference for debug trace.
1383 void zfcp_erp_port_failed(struct zfcp_port *port, char *id, void *ref)
1385 zfcp_erp_modify_port_status(port, id, ref,
1386 ZFCP_STATUS_COMMON_ERP_FAILED, ZFCP_SET);
1390 * zfcp_erp_unit_failed - Set unit status to failed.
1391 * @unit: Failed unit.
1392 * @id: Event id for debug trace.
1393 * @ref: Reference for debug trace.
1395 void zfcp_erp_unit_failed(struct zfcp_unit *unit, char *id, void *ref)
1397 zfcp_erp_modify_unit_status(unit, id, ref,
1398 ZFCP_STATUS_COMMON_ERP_FAILED, ZFCP_SET);
1402 * zfcp_erp_wait - wait for completion of error recovery on an adapter
1403 * @adapter: adapter for which to wait for completion of its error recovery
1405 void zfcp_erp_wait(struct zfcp_adapter *adapter)
1407 wait_event(adapter->erp_done_wqh,
1408 !(atomic_read(&adapter->status) &
1409 ZFCP_STATUS_ADAPTER_ERP_PENDING));
1413 * zfcp_erp_modify_adapter_status - change adapter status bits
1414 * @adapter: adapter to change the status
1415 * @id: id for the debug trace
1416 * @ref: reference for the debug trace
1417 * @mask: status bits to change
1418 * @set_or_clear: ZFCP_SET or ZFCP_CLEAR
1420 * Changes in common status bits are propagated to attached ports and units.
1422 void zfcp_erp_modify_adapter_status(struct zfcp_adapter *adapter, char *id,
1423 void *ref, u32 mask, int set_or_clear)
1425 struct zfcp_port *port;
1426 unsigned long flags;
1427 u32 common_mask = mask & ZFCP_COMMON_FLAGS;
1429 if (set_or_clear == ZFCP_SET) {
1430 if (status_change_set(mask, &adapter->status))
1431 zfcp_dbf_rec_adapter(id, ref, adapter->dbf);
1432 atomic_set_mask(mask, &adapter->status);
1434 if (status_change_clear(mask, &adapter->status))
1435 zfcp_dbf_rec_adapter(id, ref, adapter->dbf);
1436 atomic_clear_mask(mask, &adapter->status);
1437 if (mask & ZFCP_STATUS_COMMON_ERP_FAILED)
1438 atomic_set(&adapter->erp_counter, 0);
1442 read_lock_irqsave(&adapter->port_list_lock, flags);
1443 list_for_each_entry(port, &adapter->port_list, list)
1444 zfcp_erp_modify_port_status(port, id, ref, common_mask,
1446 read_unlock_irqrestore(&adapter->port_list_lock, flags);
1451 * zfcp_erp_modify_port_status - change port status bits
1452 * @port: port to change the status bits
1453 * @id: id for the debug trace
1454 * @ref: reference for the debug trace
1455 * @mask: status bits to change
1456 * @set_or_clear: ZFCP_SET or ZFCP_CLEAR
1458 * Changes in common status bits are propagated to attached units.
1460 void zfcp_erp_modify_port_status(struct zfcp_port *port, char *id, void *ref,
1461 u32 mask, int set_or_clear)
1463 struct zfcp_unit *unit;
1464 unsigned long flags;
1465 u32 common_mask = mask & ZFCP_COMMON_FLAGS;
1467 if (set_or_clear == ZFCP_SET) {
1468 if (status_change_set(mask, &port->status))
1469 zfcp_dbf_rec_port(id, ref, port);
1470 atomic_set_mask(mask, &port->status);
1472 if (status_change_clear(mask, &port->status))
1473 zfcp_dbf_rec_port(id, ref, port);
1474 atomic_clear_mask(mask, &port->status);
1475 if (mask & ZFCP_STATUS_COMMON_ERP_FAILED)
1476 atomic_set(&port->erp_counter, 0);
1480 read_lock_irqsave(&port->unit_list_lock, flags);
1481 list_for_each_entry(unit, &port->unit_list, list)
1482 zfcp_erp_modify_unit_status(unit, id, ref, common_mask,
1484 read_unlock_irqrestore(&port->unit_list_lock, flags);
1489 * zfcp_erp_modify_unit_status - change unit status bits
1490 * @unit: unit to change the status bits
1491 * @id: id for the debug trace
1492 * @ref: reference for the debug trace
1493 * @mask: status bits to change
1494 * @set_or_clear: ZFCP_SET or ZFCP_CLEAR
1496 void zfcp_erp_modify_unit_status(struct zfcp_unit *unit, char *id, void *ref,
1497 u32 mask, int set_or_clear)
1499 if (set_or_clear == ZFCP_SET) {
1500 if (status_change_set(mask, &unit->status))
1501 zfcp_dbf_rec_unit(id, ref, unit);
1502 atomic_set_mask(mask, &unit->status);
1504 if (status_change_clear(mask, &unit->status))
1505 zfcp_dbf_rec_unit(id, ref, unit);
1506 atomic_clear_mask(mask, &unit->status);
1507 if (mask & ZFCP_STATUS_COMMON_ERP_FAILED) {
1508 atomic_set(&unit->erp_counter, 0);
1514 * zfcp_erp_port_boxed - Mark port as "boxed" and start ERP
1515 * @port: The "boxed" port.
1516 * @id: The debug trace id.
1517 * @id: Reference for the debug trace.
1519 void zfcp_erp_port_boxed(struct zfcp_port *port, char *id, void *ref)
1521 zfcp_erp_modify_port_status(port, id, ref,
1522 ZFCP_STATUS_COMMON_ACCESS_BOXED, ZFCP_SET);
1523 zfcp_erp_port_reopen(port, ZFCP_STATUS_COMMON_ERP_FAILED, id, ref);
1527 * zfcp_erp_unit_boxed - Mark unit as "boxed" and start ERP
1528 * @port: The "boxed" unit.
1529 * @id: The debug trace id.
1530 * @id: Reference for the debug trace.
1532 void zfcp_erp_unit_boxed(struct zfcp_unit *unit, char *id, void *ref)
1534 zfcp_erp_modify_unit_status(unit, id, ref,
1535 ZFCP_STATUS_COMMON_ACCESS_BOXED, ZFCP_SET);
1536 zfcp_erp_unit_reopen(unit, ZFCP_STATUS_COMMON_ERP_FAILED, id, ref);
1540 * zfcp_erp_port_access_denied - Adapter denied access to port.
1541 * @port: port where access has been denied
1542 * @id: id for debug trace
1543 * @ref: reference for debug trace
1545 * Since the adapter has denied access, stop using the port and the
1548 void zfcp_erp_port_access_denied(struct zfcp_port *port, char *id, void *ref)
1550 zfcp_erp_modify_port_status(port, id, ref,
1551 ZFCP_STATUS_COMMON_ERP_FAILED |
1552 ZFCP_STATUS_COMMON_ACCESS_DENIED, ZFCP_SET);
1556 * zfcp_erp_unit_access_denied - Adapter denied access to unit.
1557 * @unit: unit where access has been denied
1558 * @id: id for debug trace
1559 * @ref: reference for debug trace
1561 * Since the adapter has denied access, stop using the unit.
1563 void zfcp_erp_unit_access_denied(struct zfcp_unit *unit, char *id, void *ref)
1565 zfcp_erp_modify_unit_status(unit, id, ref,
1566 ZFCP_STATUS_COMMON_ERP_FAILED |
1567 ZFCP_STATUS_COMMON_ACCESS_DENIED, ZFCP_SET);
1570 static void zfcp_erp_unit_access_changed(struct zfcp_unit *unit, char *id,
1573 int status = atomic_read(&unit->status);
1574 if (!(status & (ZFCP_STATUS_COMMON_ACCESS_DENIED |
1575 ZFCP_STATUS_COMMON_ACCESS_BOXED)))
1578 zfcp_erp_unit_reopen(unit, ZFCP_STATUS_COMMON_ERP_FAILED, id, ref);
1581 static void zfcp_erp_port_access_changed(struct zfcp_port *port, char *id,
1584 struct zfcp_unit *unit;
1585 unsigned long flags;
1586 int status = atomic_read(&port->status);
1588 if (!(status & (ZFCP_STATUS_COMMON_ACCESS_DENIED |
1589 ZFCP_STATUS_COMMON_ACCESS_BOXED))) {
1590 read_lock_irqsave(&port->unit_list_lock, flags);
1591 list_for_each_entry(unit, &port->unit_list, list)
1592 zfcp_erp_unit_access_changed(unit, id, ref);
1593 read_unlock_irqrestore(&port->unit_list_lock, flags);
1597 zfcp_erp_port_reopen(port, ZFCP_STATUS_COMMON_ERP_FAILED, id, ref);
1601 * zfcp_erp_adapter_access_changed - Process change in adapter ACT
1602 * @adapter: Adapter where the Access Control Table (ACT) changed
1603 * @id: Id for debug trace
1604 * @ref: Reference for debug trace
1606 void zfcp_erp_adapter_access_changed(struct zfcp_adapter *adapter, char *id,
1609 unsigned long flags;
1610 struct zfcp_port *port;
1612 if (adapter->connection_features & FSF_FEATURE_NPIV_MODE)
1615 read_lock_irqsave(&adapter->port_list_lock, flags);
1616 list_for_each_entry(port, &adapter->port_list, list)
1617 zfcp_erp_port_access_changed(port, id, ref);
1618 read_unlock_irqrestore(&adapter->port_list_lock, flags);