1 // SPDX-License-Identifier: GPL-2.0
5 * Error Recovery Procedures (ERP).
7 * Copyright IBM Corp. 2002, 2016
10 #define KMSG_COMPONENT "zfcp"
11 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
13 #include <linux/kthread.h>
14 #include <linux/bug.h>
16 #include "zfcp_reqlist.h"
18 #define ZFCP_MAX_ERPS 3
20 enum zfcp_erp_act_flags {
21 ZFCP_STATUS_ERP_TIMEDOUT = 0x10000000,
22 ZFCP_STATUS_ERP_CLOSE_ONLY = 0x01000000,
23 ZFCP_STATUS_ERP_DISMISSING = 0x00100000,
24 ZFCP_STATUS_ERP_DISMISSED = 0x00200000,
25 ZFCP_STATUS_ERP_LOWMEM = 0x00400000,
26 ZFCP_STATUS_ERP_NO_REF = 0x00800000,
30 ZFCP_ERP_STEP_UNINITIALIZED = 0x0000,
31 ZFCP_ERP_STEP_FSF_XCONFIG = 0x0001,
32 ZFCP_ERP_STEP_PHYS_PORT_CLOSING = 0x0010,
33 ZFCP_ERP_STEP_PORT_CLOSING = 0x0100,
34 ZFCP_ERP_STEP_PORT_OPENING = 0x0800,
35 ZFCP_ERP_STEP_LUN_CLOSING = 0x1000,
36 ZFCP_ERP_STEP_LUN_OPENING = 0x2000,
40 * enum zfcp_erp_act_type - Type of ERP action object.
41 * @ZFCP_ERP_ACTION_REOPEN_LUN: LUN recovery.
42 * @ZFCP_ERP_ACTION_REOPEN_PORT: Port recovery.
43 * @ZFCP_ERP_ACTION_REOPEN_PORT_FORCED: Forced port recovery.
44 * @ZFCP_ERP_ACTION_REOPEN_ADAPTER: Adapter recovery.
45 * @ZFCP_ERP_ACTION_NONE: Eyecatcher pseudo flag to bitwise or-combine with
46 * either of the first four enum values.
47 * Used to indicate that an ERP action could not be
48 * set up despite a detected need for some recovery.
49 * @ZFCP_ERP_ACTION_FAILED: Eyecatcher pseudo flag to bitwise or-combine with
50 * either of the first four enum values.
51 * Used to indicate that ERP not needed because
52 * the object has ZFCP_STATUS_COMMON_ERP_FAILED.
54 enum zfcp_erp_act_type {
55 ZFCP_ERP_ACTION_REOPEN_LUN = 1,
56 ZFCP_ERP_ACTION_REOPEN_PORT = 2,
57 ZFCP_ERP_ACTION_REOPEN_PORT_FORCED = 3,
58 ZFCP_ERP_ACTION_REOPEN_ADAPTER = 4,
59 ZFCP_ERP_ACTION_NONE = 0xc0,
60 ZFCP_ERP_ACTION_FAILED = 0xe0,
63 enum zfcp_erp_act_state {
64 ZFCP_ERP_ACTION_RUNNING = 1,
65 ZFCP_ERP_ACTION_READY = 2,
68 enum zfcp_erp_act_result {
69 ZFCP_ERP_SUCCEEDED = 0,
71 ZFCP_ERP_CONTINUES = 2,
73 ZFCP_ERP_DISMISSED = 4,
77 static void zfcp_erp_adapter_block(struct zfcp_adapter *adapter, int mask)
79 zfcp_erp_clear_adapter_status(adapter,
80 ZFCP_STATUS_COMMON_UNBLOCKED | mask);
83 static int zfcp_erp_action_exists(struct zfcp_erp_action *act)
85 struct zfcp_erp_action *curr_act;
87 list_for_each_entry(curr_act, &act->adapter->erp_running_head, list)
89 return ZFCP_ERP_ACTION_RUNNING;
93 static void zfcp_erp_action_ready(struct zfcp_erp_action *act)
95 struct zfcp_adapter *adapter = act->adapter;
97 list_move(&act->list, &act->adapter->erp_ready_head);
98 zfcp_dbf_rec_run("erardy1", act);
99 wake_up(&adapter->erp_ready_wq);
100 zfcp_dbf_rec_run("erardy2", act);
103 static void zfcp_erp_action_dismiss(struct zfcp_erp_action *act)
105 act->status |= ZFCP_STATUS_ERP_DISMISSED;
106 if (zfcp_erp_action_exists(act) == ZFCP_ERP_ACTION_RUNNING)
107 zfcp_erp_action_ready(act);
110 static void zfcp_erp_action_dismiss_lun(struct scsi_device *sdev)
112 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
114 if (atomic_read(&zfcp_sdev->status) & ZFCP_STATUS_COMMON_ERP_INUSE)
115 zfcp_erp_action_dismiss(&zfcp_sdev->erp_action);
118 static void zfcp_erp_action_dismiss_port(struct zfcp_port *port)
120 struct scsi_device *sdev;
122 if (atomic_read(&port->status) & ZFCP_STATUS_COMMON_ERP_INUSE)
123 zfcp_erp_action_dismiss(&port->erp_action);
125 spin_lock(port->adapter->scsi_host->host_lock);
126 __shost_for_each_device(sdev, port->adapter->scsi_host)
127 if (sdev_to_zfcp(sdev)->port == port)
128 zfcp_erp_action_dismiss_lun(sdev);
129 spin_unlock(port->adapter->scsi_host->host_lock);
133 static void zfcp_erp_action_dismiss_adapter(struct zfcp_adapter *adapter)
135 struct zfcp_port *port;
137 if (atomic_read(&adapter->status) & ZFCP_STATUS_COMMON_ERP_INUSE)
138 zfcp_erp_action_dismiss(&adapter->erp_action);
140 read_lock(&adapter->port_list_lock);
141 list_for_each_entry(port, &adapter->port_list, list)
142 zfcp_erp_action_dismiss_port(port);
143 read_unlock(&adapter->port_list_lock);
147 static int zfcp_erp_handle_failed(int want, struct zfcp_adapter *adapter,
148 struct zfcp_port *port,
149 struct scsi_device *sdev)
152 struct zfcp_scsi_dev *zsdev;
155 case ZFCP_ERP_ACTION_REOPEN_LUN:
156 zsdev = sdev_to_zfcp(sdev);
157 if (atomic_read(&zsdev->status) & ZFCP_STATUS_COMMON_ERP_FAILED)
160 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
161 if (atomic_read(&port->status) & ZFCP_STATUS_COMMON_ERP_FAILED)
164 case ZFCP_ERP_ACTION_REOPEN_PORT:
165 if (atomic_read(&port->status) &
166 ZFCP_STATUS_COMMON_ERP_FAILED) {
168 /* ensure propagation of failed status to new devices */
169 zfcp_erp_set_port_status(
170 port, ZFCP_STATUS_COMMON_ERP_FAILED);
173 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
174 if (atomic_read(&adapter->status) &
175 ZFCP_STATUS_COMMON_ERP_FAILED) {
177 /* ensure propagation of failed status to new devices */
178 zfcp_erp_set_adapter_status(
179 adapter, ZFCP_STATUS_COMMON_ERP_FAILED);
187 static int zfcp_erp_required_act(int want, struct zfcp_adapter *adapter,
188 struct zfcp_port *port,
189 struct scsi_device *sdev)
192 int l_status, p_status, a_status;
193 struct zfcp_scsi_dev *zfcp_sdev;
196 case ZFCP_ERP_ACTION_REOPEN_LUN:
197 zfcp_sdev = sdev_to_zfcp(sdev);
198 l_status = atomic_read(&zfcp_sdev->status);
199 if (l_status & ZFCP_STATUS_COMMON_ERP_INUSE)
201 p_status = atomic_read(&port->status);
202 if (!(p_status & ZFCP_STATUS_COMMON_RUNNING) ||
203 p_status & ZFCP_STATUS_COMMON_ERP_FAILED)
205 if (!(p_status & ZFCP_STATUS_COMMON_UNBLOCKED))
206 need = ZFCP_ERP_ACTION_REOPEN_PORT;
208 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
209 p_status = atomic_read(&port->status);
210 if (!(p_status & ZFCP_STATUS_COMMON_OPEN))
211 need = ZFCP_ERP_ACTION_REOPEN_PORT;
213 case ZFCP_ERP_ACTION_REOPEN_PORT:
214 p_status = atomic_read(&port->status);
215 if (p_status & ZFCP_STATUS_COMMON_ERP_INUSE)
217 a_status = atomic_read(&adapter->status);
218 if (!(a_status & ZFCP_STATUS_COMMON_RUNNING) ||
219 a_status & ZFCP_STATUS_COMMON_ERP_FAILED)
221 if (p_status & ZFCP_STATUS_COMMON_NOESC)
223 if (!(a_status & ZFCP_STATUS_COMMON_UNBLOCKED))
224 need = ZFCP_ERP_ACTION_REOPEN_ADAPTER;
226 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
227 a_status = atomic_read(&adapter->status);
228 if (a_status & ZFCP_STATUS_COMMON_ERP_INUSE)
230 if (!(a_status & ZFCP_STATUS_COMMON_RUNNING) &&
231 !(a_status & ZFCP_STATUS_COMMON_OPEN))
232 return 0; /* shutdown requested for closed adapter */
238 static struct zfcp_erp_action *zfcp_erp_setup_act(int need, u32 act_status,
239 struct zfcp_adapter *adapter,
240 struct zfcp_port *port,
241 struct scsi_device *sdev)
243 struct zfcp_erp_action *erp_action;
244 struct zfcp_scsi_dev *zfcp_sdev;
246 if (WARN_ON_ONCE(need != ZFCP_ERP_ACTION_REOPEN_LUN &&
247 need != ZFCP_ERP_ACTION_REOPEN_PORT &&
248 need != ZFCP_ERP_ACTION_REOPEN_PORT_FORCED &&
249 need != ZFCP_ERP_ACTION_REOPEN_ADAPTER))
253 case ZFCP_ERP_ACTION_REOPEN_LUN:
254 zfcp_sdev = sdev_to_zfcp(sdev);
255 if (!(act_status & ZFCP_STATUS_ERP_NO_REF))
256 if (scsi_device_get(sdev))
258 atomic_or(ZFCP_STATUS_COMMON_ERP_INUSE,
260 erp_action = &zfcp_sdev->erp_action;
261 WARN_ON_ONCE(erp_action->port != port);
262 WARN_ON_ONCE(erp_action->sdev != sdev);
263 if (!(atomic_read(&zfcp_sdev->status) &
264 ZFCP_STATUS_COMMON_RUNNING))
265 act_status |= ZFCP_STATUS_ERP_CLOSE_ONLY;
268 case ZFCP_ERP_ACTION_REOPEN_PORT:
269 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
270 if (!get_device(&port->dev))
272 zfcp_erp_action_dismiss_port(port);
273 atomic_or(ZFCP_STATUS_COMMON_ERP_INUSE, &port->status);
274 erp_action = &port->erp_action;
275 WARN_ON_ONCE(erp_action->port != port);
276 WARN_ON_ONCE(erp_action->sdev != NULL);
277 if (!(atomic_read(&port->status) & ZFCP_STATUS_COMMON_RUNNING))
278 act_status |= ZFCP_STATUS_ERP_CLOSE_ONLY;
281 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
282 kref_get(&adapter->ref);
283 zfcp_erp_action_dismiss_adapter(adapter);
284 atomic_or(ZFCP_STATUS_COMMON_ERP_INUSE, &adapter->status);
285 erp_action = &adapter->erp_action;
286 WARN_ON_ONCE(erp_action->port != NULL);
287 WARN_ON_ONCE(erp_action->sdev != NULL);
288 if (!(atomic_read(&adapter->status) &
289 ZFCP_STATUS_COMMON_RUNNING))
290 act_status |= ZFCP_STATUS_ERP_CLOSE_ONLY;
297 WARN_ON_ONCE(erp_action->adapter != adapter);
298 memset(&erp_action->list, 0, sizeof(erp_action->list));
299 memset(&erp_action->timer, 0, sizeof(erp_action->timer));
300 erp_action->step = ZFCP_ERP_STEP_UNINITIALIZED;
301 erp_action->fsf_req_id = 0;
302 erp_action->action = need;
303 erp_action->status = act_status;
308 static int zfcp_erp_action_enqueue(int want, struct zfcp_adapter *adapter,
309 struct zfcp_port *port,
310 struct scsi_device *sdev,
311 char *id, u32 act_status)
313 int retval = 1, need;
314 struct zfcp_erp_action *act;
316 need = zfcp_erp_handle_failed(want, adapter, port, sdev);
318 need = ZFCP_ERP_ACTION_FAILED; /* marker for trace */
322 if (!adapter->erp_thread) {
323 need = ZFCP_ERP_ACTION_NONE; /* marker for trace */
328 need = zfcp_erp_required_act(want, adapter, port, sdev);
332 act = zfcp_erp_setup_act(need, act_status, adapter, port, sdev);
334 need |= ZFCP_ERP_ACTION_NONE; /* marker for trace */
337 atomic_or(ZFCP_STATUS_ADAPTER_ERP_PENDING, &adapter->status);
338 ++adapter->erp_total_count;
339 list_add_tail(&act->list, &adapter->erp_ready_head);
340 wake_up(&adapter->erp_ready_wq);
343 zfcp_dbf_rec_trig(id, adapter, port, sdev, want, need);
347 void zfcp_erp_port_forced_no_port_dbf(char *id, struct zfcp_adapter *adapter,
348 u64 port_name, u32 port_id)
351 static /* don't waste stack */ struct zfcp_port tmpport;
353 write_lock_irqsave(&adapter->erp_lock, flags);
354 /* Stand-in zfcp port with fields just good enough for
355 * zfcp_dbf_rec_trig() and zfcp_dbf_set_common().
356 * Under lock because tmpport is static.
358 atomic_set(&tmpport.status, -1); /* unknown */
359 tmpport.wwpn = port_name;
360 tmpport.d_id = port_id;
361 zfcp_dbf_rec_trig(id, adapter, &tmpport, NULL,
362 ZFCP_ERP_ACTION_REOPEN_PORT_FORCED,
363 ZFCP_ERP_ACTION_NONE);
364 write_unlock_irqrestore(&adapter->erp_lock, flags);
367 static int _zfcp_erp_adapter_reopen(struct zfcp_adapter *adapter,
368 int clear_mask, char *id)
370 zfcp_erp_adapter_block(adapter, clear_mask);
371 zfcp_scsi_schedule_rports_block(adapter);
373 return zfcp_erp_action_enqueue(ZFCP_ERP_ACTION_REOPEN_ADAPTER,
374 adapter, NULL, NULL, id, 0);
378 * zfcp_erp_adapter_reopen - Reopen adapter.
379 * @adapter: Adapter to reopen.
380 * @clear: Status flags to clear.
381 * @id: Id for debug trace event.
383 void zfcp_erp_adapter_reopen(struct zfcp_adapter *adapter, int clear, char *id)
387 zfcp_erp_adapter_block(adapter, clear);
388 zfcp_scsi_schedule_rports_block(adapter);
390 write_lock_irqsave(&adapter->erp_lock, flags);
391 zfcp_erp_action_enqueue(ZFCP_ERP_ACTION_REOPEN_ADAPTER, adapter,
393 write_unlock_irqrestore(&adapter->erp_lock, flags);
397 * zfcp_erp_adapter_shutdown - Shutdown adapter.
398 * @adapter: Adapter to shut down.
399 * @clear: Status flags to clear.
400 * @id: Id for debug trace event.
402 void zfcp_erp_adapter_shutdown(struct zfcp_adapter *adapter, int clear,
405 int flags = ZFCP_STATUS_COMMON_RUNNING | ZFCP_STATUS_COMMON_ERP_FAILED;
406 zfcp_erp_adapter_reopen(adapter, clear | flags, id);
410 * zfcp_erp_port_shutdown - Shutdown port
411 * @port: Port to shut down.
412 * @clear: Status flags to clear.
413 * @id: Id for debug trace event.
415 void zfcp_erp_port_shutdown(struct zfcp_port *port, int clear, char *id)
417 int flags = ZFCP_STATUS_COMMON_RUNNING | ZFCP_STATUS_COMMON_ERP_FAILED;
418 zfcp_erp_port_reopen(port, clear | flags, id);
421 static void zfcp_erp_port_block(struct zfcp_port *port, int clear)
423 zfcp_erp_clear_port_status(port,
424 ZFCP_STATUS_COMMON_UNBLOCKED | clear);
427 static void _zfcp_erp_port_forced_reopen(struct zfcp_port *port, int clear,
430 zfcp_erp_port_block(port, clear);
431 zfcp_scsi_schedule_rport_block(port);
433 zfcp_erp_action_enqueue(ZFCP_ERP_ACTION_REOPEN_PORT_FORCED,
434 port->adapter, port, NULL, id, 0);
438 * zfcp_erp_port_forced_reopen - Forced close of port and open again
439 * @port: Port to force close and to reopen.
440 * @clear: Status flags to clear.
441 * @id: Id for debug trace event.
443 void zfcp_erp_port_forced_reopen(struct zfcp_port *port, int clear, char *id)
446 struct zfcp_adapter *adapter = port->adapter;
448 write_lock_irqsave(&adapter->erp_lock, flags);
449 _zfcp_erp_port_forced_reopen(port, clear, id);
450 write_unlock_irqrestore(&adapter->erp_lock, flags);
453 static int _zfcp_erp_port_reopen(struct zfcp_port *port, int clear, char *id)
455 zfcp_erp_port_block(port, clear);
456 zfcp_scsi_schedule_rport_block(port);
458 return zfcp_erp_action_enqueue(ZFCP_ERP_ACTION_REOPEN_PORT,
459 port->adapter, port, NULL, id, 0);
463 * zfcp_erp_port_reopen - trigger remote port recovery
464 * @port: port to recover
465 * @clear_mask: flags in port status to be cleared
466 * @id: Id for debug trace event.
468 * Returns 0 if recovery has been triggered, < 0 if not.
470 int zfcp_erp_port_reopen(struct zfcp_port *port, int clear, char *id)
474 struct zfcp_adapter *adapter = port->adapter;
476 write_lock_irqsave(&adapter->erp_lock, flags);
477 retval = _zfcp_erp_port_reopen(port, clear, id);
478 write_unlock_irqrestore(&adapter->erp_lock, flags);
483 static void zfcp_erp_lun_block(struct scsi_device *sdev, int clear_mask)
485 zfcp_erp_clear_lun_status(sdev,
486 ZFCP_STATUS_COMMON_UNBLOCKED | clear_mask);
489 static void _zfcp_erp_lun_reopen(struct scsi_device *sdev, int clear, char *id,
492 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
493 struct zfcp_adapter *adapter = zfcp_sdev->port->adapter;
495 zfcp_erp_lun_block(sdev, clear);
497 zfcp_erp_action_enqueue(ZFCP_ERP_ACTION_REOPEN_LUN, adapter,
498 zfcp_sdev->port, sdev, id, act_status);
502 * zfcp_erp_lun_reopen - initiate reopen of a LUN
503 * @sdev: SCSI device / LUN to be reopened
504 * @clear_mask: specifies flags in LUN status to be cleared
505 * @id: Id for debug trace event.
507 * Return: 0 on success, < 0 on error
509 void zfcp_erp_lun_reopen(struct scsi_device *sdev, int clear, char *id)
512 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
513 struct zfcp_port *port = zfcp_sdev->port;
514 struct zfcp_adapter *adapter = port->adapter;
516 write_lock_irqsave(&adapter->erp_lock, flags);
517 _zfcp_erp_lun_reopen(sdev, clear, id, 0);
518 write_unlock_irqrestore(&adapter->erp_lock, flags);
522 * zfcp_erp_lun_shutdown - Shutdown LUN
523 * @sdev: SCSI device / LUN to shut down.
524 * @clear: Status flags to clear.
525 * @id: Id for debug trace event.
527 void zfcp_erp_lun_shutdown(struct scsi_device *sdev, int clear, char *id)
529 int flags = ZFCP_STATUS_COMMON_RUNNING | ZFCP_STATUS_COMMON_ERP_FAILED;
530 zfcp_erp_lun_reopen(sdev, clear | flags, id);
534 * zfcp_erp_lun_shutdown_wait - Shutdown LUN and wait for erp completion
535 * @sdev: SCSI device / LUN to shut down.
536 * @id: Id for debug trace event.
538 * Do not acquire a reference for the LUN when creating the ERP
539 * action. It is safe, because this function waits for the ERP to
540 * complete first. This allows to shutdown the LUN, even when the SCSI
541 * device is in the state SDEV_DEL when scsi_device_get will fail.
543 void zfcp_erp_lun_shutdown_wait(struct scsi_device *sdev, char *id)
546 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
547 struct zfcp_port *port = zfcp_sdev->port;
548 struct zfcp_adapter *adapter = port->adapter;
549 int clear = ZFCP_STATUS_COMMON_RUNNING | ZFCP_STATUS_COMMON_ERP_FAILED;
551 write_lock_irqsave(&adapter->erp_lock, flags);
552 _zfcp_erp_lun_reopen(sdev, clear, id, ZFCP_STATUS_ERP_NO_REF);
553 write_unlock_irqrestore(&adapter->erp_lock, flags);
555 zfcp_erp_wait(adapter);
558 static int status_change_set(unsigned long mask, atomic_t *status)
560 return (atomic_read(status) ^ mask) & mask;
563 static void zfcp_erp_adapter_unblock(struct zfcp_adapter *adapter)
565 if (status_change_set(ZFCP_STATUS_COMMON_UNBLOCKED, &adapter->status))
566 zfcp_dbf_rec_run("eraubl1", &adapter->erp_action);
567 atomic_or(ZFCP_STATUS_COMMON_UNBLOCKED, &adapter->status);
570 static void zfcp_erp_port_unblock(struct zfcp_port *port)
572 if (status_change_set(ZFCP_STATUS_COMMON_UNBLOCKED, &port->status))
573 zfcp_dbf_rec_run("erpubl1", &port->erp_action);
574 atomic_or(ZFCP_STATUS_COMMON_UNBLOCKED, &port->status);
577 static void zfcp_erp_lun_unblock(struct scsi_device *sdev)
579 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
581 if (status_change_set(ZFCP_STATUS_COMMON_UNBLOCKED, &zfcp_sdev->status))
582 zfcp_dbf_rec_run("erlubl1", &sdev_to_zfcp(sdev)->erp_action);
583 atomic_or(ZFCP_STATUS_COMMON_UNBLOCKED, &zfcp_sdev->status);
586 static void zfcp_erp_action_to_running(struct zfcp_erp_action *erp_action)
588 list_move(&erp_action->list, &erp_action->adapter->erp_running_head);
589 zfcp_dbf_rec_run("erator1", erp_action);
592 static void zfcp_erp_strategy_check_fsfreq(struct zfcp_erp_action *act)
594 struct zfcp_adapter *adapter = act->adapter;
595 struct zfcp_fsf_req *req;
597 if (!act->fsf_req_id)
600 spin_lock(&adapter->req_list->lock);
601 req = _zfcp_reqlist_find(adapter->req_list, act->fsf_req_id);
602 if (req && req->erp_action == act) {
603 if (act->status & (ZFCP_STATUS_ERP_DISMISSED |
604 ZFCP_STATUS_ERP_TIMEDOUT)) {
605 req->status |= ZFCP_STATUS_FSFREQ_DISMISSED;
606 zfcp_dbf_rec_run("erscf_1", act);
607 req->erp_action = NULL;
609 if (act->status & ZFCP_STATUS_ERP_TIMEDOUT)
610 zfcp_dbf_rec_run("erscf_2", act);
611 if (req->status & ZFCP_STATUS_FSFREQ_DISMISSED)
615 spin_unlock(&adapter->req_list->lock);
619 * zfcp_erp_notify - Trigger ERP action.
620 * @erp_action: ERP action to continue.
621 * @set_mask: ERP action status flags to set.
623 void zfcp_erp_notify(struct zfcp_erp_action *erp_action, unsigned long set_mask)
625 struct zfcp_adapter *adapter = erp_action->adapter;
628 write_lock_irqsave(&adapter->erp_lock, flags);
629 if (zfcp_erp_action_exists(erp_action) == ZFCP_ERP_ACTION_RUNNING) {
630 erp_action->status |= set_mask;
631 zfcp_erp_action_ready(erp_action);
633 write_unlock_irqrestore(&adapter->erp_lock, flags);
637 * zfcp_erp_timeout_handler - Trigger ERP action from timed out ERP request
638 * @data: ERP action (from timer data)
640 void zfcp_erp_timeout_handler(unsigned long data)
642 struct zfcp_erp_action *act = (struct zfcp_erp_action *) data;
643 zfcp_erp_notify(act, ZFCP_STATUS_ERP_TIMEDOUT);
646 static void zfcp_erp_memwait_handler(unsigned long data)
648 zfcp_erp_notify((struct zfcp_erp_action *)data, 0);
651 static void zfcp_erp_strategy_memwait(struct zfcp_erp_action *erp_action)
653 setup_timer(&erp_action->timer, zfcp_erp_memwait_handler,
654 (unsigned long) erp_action);
655 erp_action->timer.expires = jiffies + HZ;
656 add_timer(&erp_action->timer);
659 void zfcp_erp_port_forced_reopen_all(struct zfcp_adapter *adapter,
660 int clear, char *dbftag)
663 struct zfcp_port *port;
665 write_lock_irqsave(&adapter->erp_lock, flags);
666 read_lock(&adapter->port_list_lock);
667 list_for_each_entry(port, &adapter->port_list, list)
668 _zfcp_erp_port_forced_reopen(port, clear, dbftag);
669 read_unlock(&adapter->port_list_lock);
670 write_unlock_irqrestore(&adapter->erp_lock, flags);
673 static void _zfcp_erp_port_reopen_all(struct zfcp_adapter *adapter,
676 struct zfcp_port *port;
678 read_lock(&adapter->port_list_lock);
679 list_for_each_entry(port, &adapter->port_list, list)
680 _zfcp_erp_port_reopen(port, clear, id);
681 read_unlock(&adapter->port_list_lock);
684 static void _zfcp_erp_lun_reopen_all(struct zfcp_port *port, int clear,
687 struct scsi_device *sdev;
689 spin_lock(port->adapter->scsi_host->host_lock);
690 __shost_for_each_device(sdev, port->adapter->scsi_host)
691 if (sdev_to_zfcp(sdev)->port == port)
692 _zfcp_erp_lun_reopen(sdev, clear, id, 0);
693 spin_unlock(port->adapter->scsi_host->host_lock);
696 static void zfcp_erp_strategy_followup_failed(struct zfcp_erp_action *act)
698 switch (act->action) {
699 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
700 _zfcp_erp_adapter_reopen(act->adapter, 0, "ersff_1");
702 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
703 _zfcp_erp_port_forced_reopen(act->port, 0, "ersff_2");
705 case ZFCP_ERP_ACTION_REOPEN_PORT:
706 _zfcp_erp_port_reopen(act->port, 0, "ersff_3");
708 case ZFCP_ERP_ACTION_REOPEN_LUN:
709 _zfcp_erp_lun_reopen(act->sdev, 0, "ersff_4", 0);
714 static void zfcp_erp_strategy_followup_success(struct zfcp_erp_action *act)
716 switch (act->action) {
717 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
718 _zfcp_erp_port_reopen_all(act->adapter, 0, "ersfs_1");
720 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
721 _zfcp_erp_port_reopen(act->port, 0, "ersfs_2");
723 case ZFCP_ERP_ACTION_REOPEN_PORT:
724 _zfcp_erp_lun_reopen_all(act->port, 0, "ersfs_3");
729 static void zfcp_erp_wakeup(struct zfcp_adapter *adapter)
733 read_lock_irqsave(&adapter->erp_lock, flags);
734 if (list_empty(&adapter->erp_ready_head) &&
735 list_empty(&adapter->erp_running_head)) {
736 atomic_andnot(ZFCP_STATUS_ADAPTER_ERP_PENDING,
738 wake_up(&adapter->erp_done_wqh);
740 read_unlock_irqrestore(&adapter->erp_lock, flags);
743 static void zfcp_erp_enqueue_ptp_port(struct zfcp_adapter *adapter)
745 struct zfcp_port *port;
746 port = zfcp_port_enqueue(adapter, adapter->peer_wwpn, 0,
748 if (IS_ERR(port)) /* error or port already attached */
750 zfcp_erp_port_reopen(port, 0, "ereptp1");
753 static int zfcp_erp_adapter_strat_fsf_xconf(struct zfcp_erp_action *erp_action)
757 struct zfcp_adapter *adapter = erp_action->adapter;
759 atomic_andnot(ZFCP_STATUS_ADAPTER_XCONFIG_OK, &adapter->status);
761 for (retries = 7; retries; retries--) {
762 atomic_andnot(ZFCP_STATUS_ADAPTER_HOST_CON_INIT,
764 write_lock_irq(&adapter->erp_lock);
765 zfcp_erp_action_to_running(erp_action);
766 write_unlock_irq(&adapter->erp_lock);
767 if (zfcp_fsf_exchange_config_data(erp_action)) {
768 atomic_andnot(ZFCP_STATUS_ADAPTER_HOST_CON_INIT,
770 return ZFCP_ERP_FAILED;
773 wait_event(adapter->erp_ready_wq,
774 !list_empty(&adapter->erp_ready_head));
775 if (erp_action->status & ZFCP_STATUS_ERP_TIMEDOUT)
778 if (!(atomic_read(&adapter->status) &
779 ZFCP_STATUS_ADAPTER_HOST_CON_INIT))
786 atomic_andnot(ZFCP_STATUS_ADAPTER_HOST_CON_INIT,
789 if (!(atomic_read(&adapter->status) & ZFCP_STATUS_ADAPTER_XCONFIG_OK))
790 return ZFCP_ERP_FAILED;
792 if (fc_host_port_type(adapter->scsi_host) == FC_PORTTYPE_PTP)
793 zfcp_erp_enqueue_ptp_port(adapter);
795 return ZFCP_ERP_SUCCEEDED;
798 static int zfcp_erp_adapter_strategy_open_fsf_xport(struct zfcp_erp_action *act)
801 struct zfcp_adapter *adapter = act->adapter;
803 write_lock_irq(&adapter->erp_lock);
804 zfcp_erp_action_to_running(act);
805 write_unlock_irq(&adapter->erp_lock);
807 ret = zfcp_fsf_exchange_port_data(act);
808 if (ret == -EOPNOTSUPP)
809 return ZFCP_ERP_SUCCEEDED;
811 return ZFCP_ERP_FAILED;
813 zfcp_dbf_rec_run("erasox1", act);
814 wait_event(adapter->erp_ready_wq,
815 !list_empty(&adapter->erp_ready_head));
816 zfcp_dbf_rec_run("erasox2", act);
817 if (act->status & ZFCP_STATUS_ERP_TIMEDOUT)
818 return ZFCP_ERP_FAILED;
820 return ZFCP_ERP_SUCCEEDED;
823 static int zfcp_erp_adapter_strategy_open_fsf(struct zfcp_erp_action *act)
825 if (zfcp_erp_adapter_strat_fsf_xconf(act) == ZFCP_ERP_FAILED)
826 return ZFCP_ERP_FAILED;
828 if (zfcp_erp_adapter_strategy_open_fsf_xport(act) == ZFCP_ERP_FAILED)
829 return ZFCP_ERP_FAILED;
831 if (mempool_resize(act->adapter->pool.sr_data,
832 act->adapter->stat_read_buf_num))
833 return ZFCP_ERP_FAILED;
835 if (mempool_resize(act->adapter->pool.status_read_req,
836 act->adapter->stat_read_buf_num))
837 return ZFCP_ERP_FAILED;
839 atomic_set(&act->adapter->stat_miss, act->adapter->stat_read_buf_num);
840 if (zfcp_status_read_refill(act->adapter))
841 return ZFCP_ERP_FAILED;
843 return ZFCP_ERP_SUCCEEDED;
846 static void zfcp_erp_adapter_strategy_close(struct zfcp_erp_action *act)
848 struct zfcp_adapter *adapter = act->adapter;
850 /* close queues to ensure that buffers are not accessed by adapter */
851 zfcp_qdio_close(adapter->qdio);
852 zfcp_fsf_req_dismiss_all(adapter);
853 adapter->fsf_req_seq_no = 0;
854 zfcp_fc_wka_ports_force_offline(adapter->gs);
855 /* all ports and LUNs are closed */
856 zfcp_erp_clear_adapter_status(adapter, ZFCP_STATUS_COMMON_OPEN);
858 atomic_andnot(ZFCP_STATUS_ADAPTER_XCONFIG_OK |
859 ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED, &adapter->status);
862 static int zfcp_erp_adapter_strategy_open(struct zfcp_erp_action *act)
864 struct zfcp_adapter *adapter = act->adapter;
866 if (zfcp_qdio_open(adapter->qdio)) {
867 atomic_andnot(ZFCP_STATUS_ADAPTER_XCONFIG_OK |
868 ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED,
870 return ZFCP_ERP_FAILED;
873 if (zfcp_erp_adapter_strategy_open_fsf(act)) {
874 zfcp_erp_adapter_strategy_close(act);
875 return ZFCP_ERP_FAILED;
878 atomic_or(ZFCP_STATUS_COMMON_OPEN, &adapter->status);
880 return ZFCP_ERP_SUCCEEDED;
883 static int zfcp_erp_adapter_strategy(struct zfcp_erp_action *act)
885 struct zfcp_adapter *adapter = act->adapter;
887 if (atomic_read(&adapter->status) & ZFCP_STATUS_COMMON_OPEN) {
888 zfcp_erp_adapter_strategy_close(act);
889 if (act->status & ZFCP_STATUS_ERP_CLOSE_ONLY)
890 return ZFCP_ERP_EXIT;
893 if (zfcp_erp_adapter_strategy_open(act)) {
895 return ZFCP_ERP_FAILED;
898 return ZFCP_ERP_SUCCEEDED;
901 static int zfcp_erp_port_forced_strategy_close(struct zfcp_erp_action *act)
905 retval = zfcp_fsf_close_physical_port(act);
906 if (retval == -ENOMEM)
907 return ZFCP_ERP_NOMEM;
908 act->step = ZFCP_ERP_STEP_PHYS_PORT_CLOSING;
910 return ZFCP_ERP_FAILED;
912 return ZFCP_ERP_CONTINUES;
915 static int zfcp_erp_port_forced_strategy(struct zfcp_erp_action *erp_action)
917 struct zfcp_port *port = erp_action->port;
918 int status = atomic_read(&port->status);
920 switch (erp_action->step) {
921 case ZFCP_ERP_STEP_UNINITIALIZED:
922 if ((status & ZFCP_STATUS_PORT_PHYS_OPEN) &&
923 (status & ZFCP_STATUS_COMMON_OPEN))
924 return zfcp_erp_port_forced_strategy_close(erp_action);
926 return ZFCP_ERP_FAILED;
928 case ZFCP_ERP_STEP_PHYS_PORT_CLOSING:
929 if (!(status & ZFCP_STATUS_PORT_PHYS_OPEN))
930 return ZFCP_ERP_SUCCEEDED;
932 return ZFCP_ERP_FAILED;
935 static int zfcp_erp_port_strategy_close(struct zfcp_erp_action *erp_action)
939 retval = zfcp_fsf_close_port(erp_action);
940 if (retval == -ENOMEM)
941 return ZFCP_ERP_NOMEM;
942 erp_action->step = ZFCP_ERP_STEP_PORT_CLOSING;
944 return ZFCP_ERP_FAILED;
945 return ZFCP_ERP_CONTINUES;
948 static int zfcp_erp_port_strategy_open_port(struct zfcp_erp_action *erp_action)
952 retval = zfcp_fsf_open_port(erp_action);
953 if (retval == -ENOMEM)
954 return ZFCP_ERP_NOMEM;
955 erp_action->step = ZFCP_ERP_STEP_PORT_OPENING;
957 return ZFCP_ERP_FAILED;
958 return ZFCP_ERP_CONTINUES;
961 static int zfcp_erp_open_ptp_port(struct zfcp_erp_action *act)
963 struct zfcp_adapter *adapter = act->adapter;
964 struct zfcp_port *port = act->port;
966 if (port->wwpn != adapter->peer_wwpn) {
967 zfcp_erp_set_port_status(port, ZFCP_STATUS_COMMON_ERP_FAILED);
968 return ZFCP_ERP_FAILED;
970 port->d_id = adapter->peer_d_id;
971 return zfcp_erp_port_strategy_open_port(act);
974 static int zfcp_erp_port_strategy_open_common(struct zfcp_erp_action *act)
976 struct zfcp_adapter *adapter = act->adapter;
977 struct zfcp_port *port = act->port;
978 int p_status = atomic_read(&port->status);
981 case ZFCP_ERP_STEP_UNINITIALIZED:
982 case ZFCP_ERP_STEP_PHYS_PORT_CLOSING:
983 case ZFCP_ERP_STEP_PORT_CLOSING:
984 if (fc_host_port_type(adapter->scsi_host) == FC_PORTTYPE_PTP)
985 return zfcp_erp_open_ptp_port(act);
987 zfcp_fc_trigger_did_lookup(port);
988 return ZFCP_ERP_EXIT;
990 return zfcp_erp_port_strategy_open_port(act);
992 case ZFCP_ERP_STEP_PORT_OPENING:
993 /* D_ID might have changed during open */
994 if (p_status & ZFCP_STATUS_COMMON_OPEN) {
996 zfcp_fc_trigger_did_lookup(port);
997 return ZFCP_ERP_EXIT;
999 return ZFCP_ERP_SUCCEEDED;
1001 if (port->d_id && !(p_status & ZFCP_STATUS_COMMON_NOESC)) {
1003 return ZFCP_ERP_FAILED;
1005 /* fall through otherwise */
1007 return ZFCP_ERP_FAILED;
1010 static int zfcp_erp_port_strategy(struct zfcp_erp_action *erp_action)
1012 struct zfcp_port *port = erp_action->port;
1013 int p_status = atomic_read(&port->status);
1015 if ((p_status & ZFCP_STATUS_COMMON_NOESC) &&
1016 !(p_status & ZFCP_STATUS_COMMON_OPEN))
1017 goto close_init_done;
1019 switch (erp_action->step) {
1020 case ZFCP_ERP_STEP_UNINITIALIZED:
1021 if (p_status & ZFCP_STATUS_COMMON_OPEN)
1022 return zfcp_erp_port_strategy_close(erp_action);
1025 case ZFCP_ERP_STEP_PORT_CLOSING:
1026 if (p_status & ZFCP_STATUS_COMMON_OPEN)
1027 return ZFCP_ERP_FAILED;
1032 if (erp_action->status & ZFCP_STATUS_ERP_CLOSE_ONLY)
1033 return ZFCP_ERP_EXIT;
1035 return zfcp_erp_port_strategy_open_common(erp_action);
1038 static void zfcp_erp_lun_strategy_clearstati(struct scsi_device *sdev)
1040 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
1042 atomic_andnot(ZFCP_STATUS_COMMON_ACCESS_DENIED,
1043 &zfcp_sdev->status);
1046 static int zfcp_erp_lun_strategy_close(struct zfcp_erp_action *erp_action)
1048 int retval = zfcp_fsf_close_lun(erp_action);
1049 if (retval == -ENOMEM)
1050 return ZFCP_ERP_NOMEM;
1051 erp_action->step = ZFCP_ERP_STEP_LUN_CLOSING;
1053 return ZFCP_ERP_FAILED;
1054 return ZFCP_ERP_CONTINUES;
1057 static int zfcp_erp_lun_strategy_open(struct zfcp_erp_action *erp_action)
1059 int retval = zfcp_fsf_open_lun(erp_action);
1060 if (retval == -ENOMEM)
1061 return ZFCP_ERP_NOMEM;
1062 erp_action->step = ZFCP_ERP_STEP_LUN_OPENING;
1064 return ZFCP_ERP_FAILED;
1065 return ZFCP_ERP_CONTINUES;
1068 static int zfcp_erp_lun_strategy(struct zfcp_erp_action *erp_action)
1070 struct scsi_device *sdev = erp_action->sdev;
1071 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
1073 switch (erp_action->step) {
1074 case ZFCP_ERP_STEP_UNINITIALIZED:
1075 zfcp_erp_lun_strategy_clearstati(sdev);
1076 if (atomic_read(&zfcp_sdev->status) & ZFCP_STATUS_COMMON_OPEN)
1077 return zfcp_erp_lun_strategy_close(erp_action);
1078 /* already closed, fall through */
1079 case ZFCP_ERP_STEP_LUN_CLOSING:
1080 if (atomic_read(&zfcp_sdev->status) & ZFCP_STATUS_COMMON_OPEN)
1081 return ZFCP_ERP_FAILED;
1082 if (erp_action->status & ZFCP_STATUS_ERP_CLOSE_ONLY)
1083 return ZFCP_ERP_EXIT;
1084 return zfcp_erp_lun_strategy_open(erp_action);
1086 case ZFCP_ERP_STEP_LUN_OPENING:
1087 if (atomic_read(&zfcp_sdev->status) & ZFCP_STATUS_COMMON_OPEN)
1088 return ZFCP_ERP_SUCCEEDED;
1090 return ZFCP_ERP_FAILED;
1093 static int zfcp_erp_strategy_check_lun(struct scsi_device *sdev, int result)
1095 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
1098 case ZFCP_ERP_SUCCEEDED :
1099 atomic_set(&zfcp_sdev->erp_counter, 0);
1100 zfcp_erp_lun_unblock(sdev);
1102 case ZFCP_ERP_FAILED :
1103 atomic_inc(&zfcp_sdev->erp_counter);
1104 if (atomic_read(&zfcp_sdev->erp_counter) > ZFCP_MAX_ERPS) {
1105 dev_err(&zfcp_sdev->port->adapter->ccw_device->dev,
1106 "ERP failed for LUN 0x%016Lx on "
1108 (unsigned long long)zfcp_scsi_dev_lun(sdev),
1109 (unsigned long long)zfcp_sdev->port->wwpn);
1110 zfcp_erp_set_lun_status(sdev,
1111 ZFCP_STATUS_COMMON_ERP_FAILED);
1116 if (atomic_read(&zfcp_sdev->status) & ZFCP_STATUS_COMMON_ERP_FAILED) {
1117 zfcp_erp_lun_block(sdev, 0);
1118 result = ZFCP_ERP_EXIT;
1123 static int zfcp_erp_strategy_check_port(struct zfcp_port *port, int result)
1126 case ZFCP_ERP_SUCCEEDED :
1127 atomic_set(&port->erp_counter, 0);
1128 zfcp_erp_port_unblock(port);
1131 case ZFCP_ERP_FAILED :
1132 if (atomic_read(&port->status) & ZFCP_STATUS_COMMON_NOESC) {
1133 zfcp_erp_port_block(port, 0);
1134 result = ZFCP_ERP_EXIT;
1136 atomic_inc(&port->erp_counter);
1137 if (atomic_read(&port->erp_counter) > ZFCP_MAX_ERPS) {
1138 dev_err(&port->adapter->ccw_device->dev,
1139 "ERP failed for remote port 0x%016Lx\n",
1140 (unsigned long long)port->wwpn);
1141 zfcp_erp_set_port_status(port,
1142 ZFCP_STATUS_COMMON_ERP_FAILED);
1147 if (atomic_read(&port->status) & ZFCP_STATUS_COMMON_ERP_FAILED) {
1148 zfcp_erp_port_block(port, 0);
1149 result = ZFCP_ERP_EXIT;
1154 static int zfcp_erp_strategy_check_adapter(struct zfcp_adapter *adapter,
1158 case ZFCP_ERP_SUCCEEDED :
1159 atomic_set(&adapter->erp_counter, 0);
1160 zfcp_erp_adapter_unblock(adapter);
1163 case ZFCP_ERP_FAILED :
1164 atomic_inc(&adapter->erp_counter);
1165 if (atomic_read(&adapter->erp_counter) > ZFCP_MAX_ERPS) {
1166 dev_err(&adapter->ccw_device->dev,
1167 "ERP cannot recover an error "
1168 "on the FCP device\n");
1169 zfcp_erp_set_adapter_status(adapter,
1170 ZFCP_STATUS_COMMON_ERP_FAILED);
1175 if (atomic_read(&adapter->status) & ZFCP_STATUS_COMMON_ERP_FAILED) {
1176 zfcp_erp_adapter_block(adapter, 0);
1177 result = ZFCP_ERP_EXIT;
1182 static int zfcp_erp_strategy_check_target(struct zfcp_erp_action *erp_action,
1185 struct zfcp_adapter *adapter = erp_action->adapter;
1186 struct zfcp_port *port = erp_action->port;
1187 struct scsi_device *sdev = erp_action->sdev;
1189 switch (erp_action->action) {
1191 case ZFCP_ERP_ACTION_REOPEN_LUN:
1192 result = zfcp_erp_strategy_check_lun(sdev, result);
1195 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
1196 case ZFCP_ERP_ACTION_REOPEN_PORT:
1197 result = zfcp_erp_strategy_check_port(port, result);
1200 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
1201 result = zfcp_erp_strategy_check_adapter(adapter, result);
1207 static int zfcp_erp_strat_change_det(atomic_t *target_status, u32 erp_status)
1209 int status = atomic_read(target_status);
1211 if ((status & ZFCP_STATUS_COMMON_RUNNING) &&
1212 (erp_status & ZFCP_STATUS_ERP_CLOSE_ONLY))
1213 return 1; /* take it online */
1215 if (!(status & ZFCP_STATUS_COMMON_RUNNING) &&
1216 !(erp_status & ZFCP_STATUS_ERP_CLOSE_ONLY))
1217 return 1; /* take it offline */
1222 static int zfcp_erp_strategy_statechange(struct zfcp_erp_action *act, int ret)
1224 int action = act->action;
1225 struct zfcp_adapter *adapter = act->adapter;
1226 struct zfcp_port *port = act->port;
1227 struct scsi_device *sdev = act->sdev;
1228 struct zfcp_scsi_dev *zfcp_sdev;
1229 u32 erp_status = act->status;
1232 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
1233 if (zfcp_erp_strat_change_det(&adapter->status, erp_status)) {
1234 _zfcp_erp_adapter_reopen(adapter,
1235 ZFCP_STATUS_COMMON_ERP_FAILED,
1237 return ZFCP_ERP_EXIT;
1241 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
1242 case ZFCP_ERP_ACTION_REOPEN_PORT:
1243 if (zfcp_erp_strat_change_det(&port->status, erp_status)) {
1244 _zfcp_erp_port_reopen(port,
1245 ZFCP_STATUS_COMMON_ERP_FAILED,
1247 return ZFCP_ERP_EXIT;
1251 case ZFCP_ERP_ACTION_REOPEN_LUN:
1252 zfcp_sdev = sdev_to_zfcp(sdev);
1253 if (zfcp_erp_strat_change_det(&zfcp_sdev->status, erp_status)) {
1254 _zfcp_erp_lun_reopen(sdev,
1255 ZFCP_STATUS_COMMON_ERP_FAILED,
1257 return ZFCP_ERP_EXIT;
1264 static void zfcp_erp_action_dequeue(struct zfcp_erp_action *erp_action)
1266 struct zfcp_adapter *adapter = erp_action->adapter;
1267 struct zfcp_scsi_dev *zfcp_sdev;
1269 adapter->erp_total_count--;
1270 if (erp_action->status & ZFCP_STATUS_ERP_LOWMEM) {
1271 adapter->erp_low_mem_count--;
1272 erp_action->status &= ~ZFCP_STATUS_ERP_LOWMEM;
1275 list_del(&erp_action->list);
1276 zfcp_dbf_rec_run("eractd1", erp_action);
1278 switch (erp_action->action) {
1279 case ZFCP_ERP_ACTION_REOPEN_LUN:
1280 zfcp_sdev = sdev_to_zfcp(erp_action->sdev);
1281 atomic_andnot(ZFCP_STATUS_COMMON_ERP_INUSE,
1282 &zfcp_sdev->status);
1285 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
1286 case ZFCP_ERP_ACTION_REOPEN_PORT:
1287 atomic_andnot(ZFCP_STATUS_COMMON_ERP_INUSE,
1288 &erp_action->port->status);
1291 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
1292 atomic_andnot(ZFCP_STATUS_COMMON_ERP_INUSE,
1293 &erp_action->adapter->status);
1299 * zfcp_erp_try_rport_unblock - unblock rport if no more/new recovery
1300 * @port: zfcp_port whose fc_rport we should try to unblock
1302 static void zfcp_erp_try_rport_unblock(struct zfcp_port *port)
1304 unsigned long flags;
1305 struct zfcp_adapter *adapter = port->adapter;
1307 struct Scsi_Host *shost = adapter->scsi_host;
1308 struct scsi_device *sdev;
1310 write_lock_irqsave(&adapter->erp_lock, flags);
1311 port_status = atomic_read(&port->status);
1312 if ((port_status & ZFCP_STATUS_COMMON_UNBLOCKED) == 0 ||
1313 (port_status & (ZFCP_STATUS_COMMON_ERP_INUSE |
1314 ZFCP_STATUS_COMMON_ERP_FAILED)) != 0) {
1315 /* new ERP of severity >= port triggered elsewhere meanwhile or
1316 * local link down (adapter erp_failed but not clear unblock)
1318 zfcp_dbf_rec_run_lvl(4, "ertru_p", &port->erp_action);
1319 write_unlock_irqrestore(&adapter->erp_lock, flags);
1322 spin_lock(shost->host_lock);
1323 __shost_for_each_device(sdev, shost) {
1324 struct zfcp_scsi_dev *zsdev = sdev_to_zfcp(sdev);
1327 if (sdev->sdev_state == SDEV_DEL ||
1328 sdev->sdev_state == SDEV_CANCEL)
1330 if (zsdev->port != port)
1332 /* LUN under port of interest */
1333 lun_status = atomic_read(&zsdev->status);
1334 if ((lun_status & ZFCP_STATUS_COMMON_ERP_FAILED) != 0)
1335 continue; /* unblock rport despite failed LUNs */
1336 /* LUN recovery not given up yet [maybe follow-up pending] */
1337 if ((lun_status & ZFCP_STATUS_COMMON_UNBLOCKED) == 0 ||
1338 (lun_status & ZFCP_STATUS_COMMON_ERP_INUSE) != 0) {
1340 * not yet unblocked [LUN recovery pending]
1341 * or meanwhile blocked [new LUN recovery triggered]
1343 zfcp_dbf_rec_run_lvl(4, "ertru_l", &zsdev->erp_action);
1344 spin_unlock(shost->host_lock);
1345 write_unlock_irqrestore(&adapter->erp_lock, flags);
1349 /* now port has no child or all children have completed recovery,
1350 * and no ERP of severity >= port was meanwhile triggered elsewhere
1352 zfcp_scsi_schedule_rport_register(port);
1353 spin_unlock(shost->host_lock);
1354 write_unlock_irqrestore(&adapter->erp_lock, flags);
1357 static void zfcp_erp_action_cleanup(struct zfcp_erp_action *act, int result)
1359 struct zfcp_adapter *adapter = act->adapter;
1360 struct zfcp_port *port = act->port;
1361 struct scsi_device *sdev = act->sdev;
1363 switch (act->action) {
1364 case ZFCP_ERP_ACTION_REOPEN_LUN:
1365 if (!(act->status & ZFCP_STATUS_ERP_NO_REF))
1366 scsi_device_put(sdev);
1367 zfcp_erp_try_rport_unblock(port);
1370 case ZFCP_ERP_ACTION_REOPEN_PORT:
1371 /* This switch case might also happen after a forced reopen
1372 * was successfully done and thus overwritten with a new
1373 * non-forced reopen at `ersfs_2'. In this case, we must not
1374 * do the clean-up of the non-forced version.
1376 if (act->step != ZFCP_ERP_STEP_UNINITIALIZED)
1377 if (result == ZFCP_ERP_SUCCEEDED)
1378 zfcp_erp_try_rport_unblock(port);
1380 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
1381 put_device(&port->dev);
1384 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
1385 if (result == ZFCP_ERP_SUCCEEDED) {
1386 register_service_level(&adapter->service_level);
1387 zfcp_fc_conditional_port_scan(adapter);
1388 queue_work(adapter->work_queue, &adapter->ns_up_work);
1390 unregister_service_level(&adapter->service_level);
1392 kref_put(&adapter->ref, zfcp_adapter_release);
1397 static int zfcp_erp_strategy_do_action(struct zfcp_erp_action *erp_action)
1399 switch (erp_action->action) {
1400 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
1401 return zfcp_erp_adapter_strategy(erp_action);
1402 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
1403 return zfcp_erp_port_forced_strategy(erp_action);
1404 case ZFCP_ERP_ACTION_REOPEN_PORT:
1405 return zfcp_erp_port_strategy(erp_action);
1406 case ZFCP_ERP_ACTION_REOPEN_LUN:
1407 return zfcp_erp_lun_strategy(erp_action);
1409 return ZFCP_ERP_FAILED;
1412 static int zfcp_erp_strategy(struct zfcp_erp_action *erp_action)
1415 unsigned long flags;
1416 struct zfcp_adapter *adapter = erp_action->adapter;
1418 kref_get(&adapter->ref);
1420 write_lock_irqsave(&adapter->erp_lock, flags);
1421 zfcp_erp_strategy_check_fsfreq(erp_action);
1423 if (erp_action->status & ZFCP_STATUS_ERP_DISMISSED) {
1424 zfcp_erp_action_dequeue(erp_action);
1425 retval = ZFCP_ERP_DISMISSED;
1429 if (erp_action->status & ZFCP_STATUS_ERP_TIMEDOUT) {
1430 retval = ZFCP_ERP_FAILED;
1434 zfcp_erp_action_to_running(erp_action);
1436 /* no lock to allow for blocking operations */
1437 write_unlock_irqrestore(&adapter->erp_lock, flags);
1438 retval = zfcp_erp_strategy_do_action(erp_action);
1439 write_lock_irqsave(&adapter->erp_lock, flags);
1441 if (erp_action->status & ZFCP_STATUS_ERP_DISMISSED)
1442 retval = ZFCP_ERP_CONTINUES;
1445 case ZFCP_ERP_NOMEM:
1446 if (!(erp_action->status & ZFCP_STATUS_ERP_LOWMEM)) {
1447 ++adapter->erp_low_mem_count;
1448 erp_action->status |= ZFCP_STATUS_ERP_LOWMEM;
1450 if (adapter->erp_total_count == adapter->erp_low_mem_count)
1451 _zfcp_erp_adapter_reopen(adapter, 0, "erstgy1");
1453 zfcp_erp_strategy_memwait(erp_action);
1454 retval = ZFCP_ERP_CONTINUES;
1458 case ZFCP_ERP_CONTINUES:
1459 if (erp_action->status & ZFCP_STATUS_ERP_LOWMEM) {
1460 --adapter->erp_low_mem_count;
1461 erp_action->status &= ~ZFCP_STATUS_ERP_LOWMEM;
1467 retval = zfcp_erp_strategy_check_target(erp_action, retval);
1468 zfcp_erp_action_dequeue(erp_action);
1469 retval = zfcp_erp_strategy_statechange(erp_action, retval);
1470 if (retval == ZFCP_ERP_EXIT)
1472 if (retval == ZFCP_ERP_SUCCEEDED)
1473 zfcp_erp_strategy_followup_success(erp_action);
1474 if (retval == ZFCP_ERP_FAILED)
1475 zfcp_erp_strategy_followup_failed(erp_action);
1478 write_unlock_irqrestore(&adapter->erp_lock, flags);
1480 if (retval != ZFCP_ERP_CONTINUES)
1481 zfcp_erp_action_cleanup(erp_action, retval);
1483 kref_put(&adapter->ref, zfcp_adapter_release);
1487 static int zfcp_erp_thread(void *data)
1489 struct zfcp_adapter *adapter = (struct zfcp_adapter *) data;
1490 struct list_head *next;
1491 struct zfcp_erp_action *act;
1492 unsigned long flags;
1495 wait_event_interruptible(adapter->erp_ready_wq,
1496 !list_empty(&adapter->erp_ready_head) ||
1497 kthread_should_stop());
1499 if (kthread_should_stop())
1502 write_lock_irqsave(&adapter->erp_lock, flags);
1503 next = adapter->erp_ready_head.next;
1504 write_unlock_irqrestore(&adapter->erp_lock, flags);
1506 if (next != &adapter->erp_ready_head) {
1507 act = list_entry(next, struct zfcp_erp_action, list);
1509 /* there is more to come after dismission, no notify */
1510 if (zfcp_erp_strategy(act) != ZFCP_ERP_DISMISSED)
1511 zfcp_erp_wakeup(adapter);
1519 * zfcp_erp_thread_setup - Start ERP thread for adapter
1520 * @adapter: Adapter to start the ERP thread for
1522 * Returns 0 on success or error code from kernel_thread()
1524 int zfcp_erp_thread_setup(struct zfcp_adapter *adapter)
1526 struct task_struct *thread;
1528 thread = kthread_run(zfcp_erp_thread, adapter, "zfcperp%s",
1529 dev_name(&adapter->ccw_device->dev));
1530 if (IS_ERR(thread)) {
1531 dev_err(&adapter->ccw_device->dev,
1532 "Creating an ERP thread for the FCP device failed.\n");
1533 return PTR_ERR(thread);
1536 adapter->erp_thread = thread;
1541 * zfcp_erp_thread_kill - Stop ERP thread.
1542 * @adapter: Adapter where the ERP thread should be stopped.
1544 * The caller of this routine ensures that the specified adapter has
1545 * been shut down and that this operation has been completed. Thus,
1546 * there are no pending erp_actions which would need to be handled
1549 void zfcp_erp_thread_kill(struct zfcp_adapter *adapter)
1551 kthread_stop(adapter->erp_thread);
1552 adapter->erp_thread = NULL;
1553 WARN_ON(!list_empty(&adapter->erp_ready_head));
1554 WARN_ON(!list_empty(&adapter->erp_running_head));
1558 * zfcp_erp_wait - wait for completion of error recovery on an adapter
1559 * @adapter: adapter for which to wait for completion of its error recovery
1561 void zfcp_erp_wait(struct zfcp_adapter *adapter)
1563 wait_event(adapter->erp_done_wqh,
1564 !(atomic_read(&adapter->status) &
1565 ZFCP_STATUS_ADAPTER_ERP_PENDING));
1569 * zfcp_erp_set_adapter_status - set adapter status bits
1570 * @adapter: adapter to change the status
1571 * @mask: status bits to change
1573 * Changes in common status bits are propagated to attached ports and LUNs.
1575 void zfcp_erp_set_adapter_status(struct zfcp_adapter *adapter, u32 mask)
1577 struct zfcp_port *port;
1578 struct scsi_device *sdev;
1579 unsigned long flags;
1580 u32 common_mask = mask & ZFCP_COMMON_FLAGS;
1582 atomic_or(mask, &adapter->status);
1587 read_lock_irqsave(&adapter->port_list_lock, flags);
1588 list_for_each_entry(port, &adapter->port_list, list)
1589 atomic_or(common_mask, &port->status);
1590 read_unlock_irqrestore(&adapter->port_list_lock, flags);
1592 spin_lock_irqsave(adapter->scsi_host->host_lock, flags);
1593 __shost_for_each_device(sdev, adapter->scsi_host)
1594 atomic_or(common_mask, &sdev_to_zfcp(sdev)->status);
1595 spin_unlock_irqrestore(adapter->scsi_host->host_lock, flags);
1599 * zfcp_erp_clear_adapter_status - clear adapter status bits
1600 * @adapter: adapter to change the status
1601 * @mask: status bits to change
1603 * Changes in common status bits are propagated to attached ports and LUNs.
1605 void zfcp_erp_clear_adapter_status(struct zfcp_adapter *adapter, u32 mask)
1607 struct zfcp_port *port;
1608 struct scsi_device *sdev;
1609 unsigned long flags;
1610 u32 common_mask = mask & ZFCP_COMMON_FLAGS;
1611 u32 clear_counter = mask & ZFCP_STATUS_COMMON_ERP_FAILED;
1613 atomic_andnot(mask, &adapter->status);
1619 atomic_set(&adapter->erp_counter, 0);
1621 read_lock_irqsave(&adapter->port_list_lock, flags);
1622 list_for_each_entry(port, &adapter->port_list, list) {
1623 atomic_andnot(common_mask, &port->status);
1625 atomic_set(&port->erp_counter, 0);
1627 read_unlock_irqrestore(&adapter->port_list_lock, flags);
1629 spin_lock_irqsave(adapter->scsi_host->host_lock, flags);
1630 __shost_for_each_device(sdev, adapter->scsi_host) {
1631 atomic_andnot(common_mask, &sdev_to_zfcp(sdev)->status);
1633 atomic_set(&sdev_to_zfcp(sdev)->erp_counter, 0);
1635 spin_unlock_irqrestore(adapter->scsi_host->host_lock, flags);
1639 * zfcp_erp_set_port_status - set port status bits
1640 * @port: port to change the status
1641 * @mask: status bits to change
1643 * Changes in common status bits are propagated to attached LUNs.
1645 void zfcp_erp_set_port_status(struct zfcp_port *port, u32 mask)
1647 struct scsi_device *sdev;
1648 u32 common_mask = mask & ZFCP_COMMON_FLAGS;
1649 unsigned long flags;
1651 atomic_or(mask, &port->status);
1656 spin_lock_irqsave(port->adapter->scsi_host->host_lock, flags);
1657 __shost_for_each_device(sdev, port->adapter->scsi_host)
1658 if (sdev_to_zfcp(sdev)->port == port)
1659 atomic_or(common_mask,
1660 &sdev_to_zfcp(sdev)->status);
1661 spin_unlock_irqrestore(port->adapter->scsi_host->host_lock, flags);
1665 * zfcp_erp_clear_port_status - clear port status bits
1666 * @port: adapter to change the status
1667 * @mask: status bits to change
1669 * Changes in common status bits are propagated to attached LUNs.
1671 void zfcp_erp_clear_port_status(struct zfcp_port *port, u32 mask)
1673 struct scsi_device *sdev;
1674 u32 common_mask = mask & ZFCP_COMMON_FLAGS;
1675 u32 clear_counter = mask & ZFCP_STATUS_COMMON_ERP_FAILED;
1676 unsigned long flags;
1678 atomic_andnot(mask, &port->status);
1684 atomic_set(&port->erp_counter, 0);
1686 spin_lock_irqsave(port->adapter->scsi_host->host_lock, flags);
1687 __shost_for_each_device(sdev, port->adapter->scsi_host)
1688 if (sdev_to_zfcp(sdev)->port == port) {
1689 atomic_andnot(common_mask,
1690 &sdev_to_zfcp(sdev)->status);
1692 atomic_set(&sdev_to_zfcp(sdev)->erp_counter, 0);
1694 spin_unlock_irqrestore(port->adapter->scsi_host->host_lock, flags);
1698 * zfcp_erp_set_lun_status - set lun status bits
1699 * @sdev: SCSI device / lun to set the status bits
1700 * @mask: status bits to change
1702 void zfcp_erp_set_lun_status(struct scsi_device *sdev, u32 mask)
1704 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
1706 atomic_or(mask, &zfcp_sdev->status);
1710 * zfcp_erp_clear_lun_status - clear lun status bits
1711 * @sdev: SCSi device / lun to clear the status bits
1712 * @mask: status bits to change
1714 void zfcp_erp_clear_lun_status(struct scsi_device *sdev, u32 mask)
1716 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
1718 atomic_andnot(mask, &zfcp_sdev->status);
1720 if (mask & ZFCP_STATUS_COMMON_ERP_FAILED)
1721 atomic_set(&zfcp_sdev->erp_counter, 0);