blob: 1d91a32db08ef33bfe8e571375f243fa4871ef2c [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Swen Schillig41fa2ada2007-09-07 09:15:31 +02002/*
Christof Schmitt553448f2008-06-10 18:20:58 +02003 * zfcp device driver
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Christof Schmitt553448f2008-06-10 18:20:58 +02005 * Error Recovery Procedures (ERP).
Swen Schillig41fa2ada2007-09-07 09:15:31 +02006 *
Steffen Maier6f2ce1c2016-12-09 17:16:33 +01007 * Copyright IBM Corp. 2002, 2016
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 */
9
Christof Schmittecf39d42008-12-25 13:39:53 +010010#define KMSG_COMPONENT "zfcp"
11#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
12
Christof Schmitt347c6a92009-08-18 15:43:25 +020013#include <linux/kthread.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include "zfcp_ext.h"
Christof Schmittb6bd2fb2010-02-17 11:18:50 +010015#include "zfcp_reqlist.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070016
Christof Schmitt287ac012008-07-02 10:56:40 +020017#define ZFCP_MAX_ERPS 3
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
Christof Schmitt287ac012008-07-02 10:56:40 +020019enum zfcp_erp_act_flags {
20 ZFCP_STATUS_ERP_TIMEDOUT = 0x10000000,
21 ZFCP_STATUS_ERP_CLOSE_ONLY = 0x01000000,
22 ZFCP_STATUS_ERP_DISMISSING = 0x00100000,
23 ZFCP_STATUS_ERP_DISMISSED = 0x00200000,
24 ZFCP_STATUS_ERP_LOWMEM = 0x00400000,
Christof Schmittfdbd1c52010-09-08 14:39:54 +020025 ZFCP_STATUS_ERP_NO_REF = 0x00800000,
Christof Schmitt287ac012008-07-02 10:56:40 +020026};
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
Christof Schmitt287ac012008-07-02 10:56:40 +020028enum zfcp_erp_steps {
29 ZFCP_ERP_STEP_UNINITIALIZED = 0x0000,
30 ZFCP_ERP_STEP_FSF_XCONFIG = 0x0001,
31 ZFCP_ERP_STEP_PHYS_PORT_CLOSING = 0x0010,
32 ZFCP_ERP_STEP_PORT_CLOSING = 0x0100,
Christof Schmitt287ac012008-07-02 10:56:40 +020033 ZFCP_ERP_STEP_PORT_OPENING = 0x0800,
Christof Schmittb62a8d92010-09-08 14:39:55 +020034 ZFCP_ERP_STEP_LUN_CLOSING = 0x1000,
35 ZFCP_ERP_STEP_LUN_OPENING = 0x2000,
Christof Schmitt287ac012008-07-02 10:56:40 +020036};
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Christof Schmitt287ac012008-07-02 10:56:40 +020038enum zfcp_erp_act_type {
Christof Schmittb62a8d92010-09-08 14:39:55 +020039 ZFCP_ERP_ACTION_REOPEN_LUN = 1,
Christof Schmitt287ac012008-07-02 10:56:40 +020040 ZFCP_ERP_ACTION_REOPEN_PORT = 2,
41 ZFCP_ERP_ACTION_REOPEN_PORT_FORCED = 3,
42 ZFCP_ERP_ACTION_REOPEN_ADAPTER = 4,
43};
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
Christof Schmitt287ac012008-07-02 10:56:40 +020045enum zfcp_erp_act_state {
46 ZFCP_ERP_ACTION_RUNNING = 1,
47 ZFCP_ERP_ACTION_READY = 2,
48};
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Christof Schmitt287ac012008-07-02 10:56:40 +020050enum zfcp_erp_act_result {
51 ZFCP_ERP_SUCCEEDED = 0,
52 ZFCP_ERP_FAILED = 1,
53 ZFCP_ERP_CONTINUES = 2,
54 ZFCP_ERP_EXIT = 3,
55 ZFCP_ERP_DISMISSED = 4,
56 ZFCP_ERP_NOMEM = 5,
57};
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Christof Schmitt287ac012008-07-02 10:56:40 +020059static void zfcp_erp_adapter_block(struct zfcp_adapter *adapter, int mask)
Andreas Herrmann2abbe862006-09-18 22:29:56 +020060{
Swen Schilligedaed852010-09-08 14:40:01 +020061 zfcp_erp_clear_adapter_status(adapter,
62 ZFCP_STATUS_COMMON_UNBLOCKED | mask);
Andreas Herrmann2abbe862006-09-18 22:29:56 +020063}
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Christof Schmitt287ac012008-07-02 10:56:40 +020065static int zfcp_erp_action_exists(struct zfcp_erp_action *act)
Linus Torvalds1da177e2005-04-16 15:20:36 -070066{
Christof Schmitt287ac012008-07-02 10:56:40 +020067 struct zfcp_erp_action *curr_act;
68
69 list_for_each_entry(curr_act, &act->adapter->erp_running_head, list)
70 if (act == curr_act)
71 return ZFCP_ERP_ACTION_RUNNING;
72 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073}
74
Christof Schmitt287ac012008-07-02 10:56:40 +020075static void zfcp_erp_action_ready(struct zfcp_erp_action *act)
Linus Torvalds1da177e2005-04-16 15:20:36 -070076{
Christof Schmitt287ac012008-07-02 10:56:40 +020077 struct zfcp_adapter *adapter = act->adapter;
78
79 list_move(&act->list, &act->adapter->erp_ready_head);
Swen Schilligae0904f2010-12-02 15:16:12 +010080 zfcp_dbf_rec_run("erardy1", act);
Christof Schmitt347c6a92009-08-18 15:43:25 +020081 wake_up(&adapter->erp_ready_wq);
Swen Schilligae0904f2010-12-02 15:16:12 +010082 zfcp_dbf_rec_run("erardy2", act);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083}
84
Christof Schmitt287ac012008-07-02 10:56:40 +020085static void zfcp_erp_action_dismiss(struct zfcp_erp_action *act)
Linus Torvalds1da177e2005-04-16 15:20:36 -070086{
Christof Schmitt287ac012008-07-02 10:56:40 +020087 act->status |= ZFCP_STATUS_ERP_DISMISSED;
88 if (zfcp_erp_action_exists(act) == ZFCP_ERP_ACTION_RUNNING)
89 zfcp_erp_action_ready(act);
90}
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Christof Schmittb62a8d92010-09-08 14:39:55 +020092static void zfcp_erp_action_dismiss_lun(struct scsi_device *sdev)
Christof Schmitt287ac012008-07-02 10:56:40 +020093{
Christof Schmittb62a8d92010-09-08 14:39:55 +020094 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
95
96 if (atomic_read(&zfcp_sdev->status) & ZFCP_STATUS_COMMON_ERP_INUSE)
97 zfcp_erp_action_dismiss(&zfcp_sdev->erp_action);
Christof Schmitt287ac012008-07-02 10:56:40 +020098}
99
100static void zfcp_erp_action_dismiss_port(struct zfcp_port *port)
101{
Christof Schmittb62a8d92010-09-08 14:39:55 +0200102 struct scsi_device *sdev;
Christof Schmitt287ac012008-07-02 10:56:40 +0200103
104 if (atomic_read(&port->status) & ZFCP_STATUS_COMMON_ERP_INUSE)
105 zfcp_erp_action_dismiss(&port->erp_action);
Martin Peschke924dd582013-08-22 17:45:37 +0200106 else {
107 spin_lock(port->adapter->scsi_host->host_lock);
108 __shost_for_each_device(sdev, port->adapter->scsi_host)
Christof Schmittb62a8d92010-09-08 14:39:55 +0200109 if (sdev_to_zfcp(sdev)->port == port)
110 zfcp_erp_action_dismiss_lun(sdev);
Martin Peschke924dd582013-08-22 17:45:37 +0200111 spin_unlock(port->adapter->scsi_host->host_lock);
112 }
Christof Schmitt287ac012008-07-02 10:56:40 +0200113}
114
115static void zfcp_erp_action_dismiss_adapter(struct zfcp_adapter *adapter)
116{
117 struct zfcp_port *port;
118
119 if (atomic_read(&adapter->status) & ZFCP_STATUS_COMMON_ERP_INUSE)
120 zfcp_erp_action_dismiss(&adapter->erp_action);
Swen Schilligecf0c772009-11-24 16:53:58 +0100121 else {
122 read_lock(&adapter->port_list_lock);
123 list_for_each_entry(port, &adapter->port_list, list)
Christof Schmitt287ac012008-07-02 10:56:40 +0200124 zfcp_erp_action_dismiss_port(port);
Swen Schilligecf0c772009-11-24 16:53:58 +0100125 read_unlock(&adapter->port_list_lock);
126 }
Christof Schmitt287ac012008-07-02 10:56:40 +0200127}
128
129static int zfcp_erp_required_act(int want, struct zfcp_adapter *adapter,
130 struct zfcp_port *port,
Christof Schmittb62a8d92010-09-08 14:39:55 +0200131 struct scsi_device *sdev)
Christof Schmitt287ac012008-07-02 10:56:40 +0200132{
133 int need = want;
Christof Schmittb62a8d92010-09-08 14:39:55 +0200134 int l_status, p_status, a_status;
135 struct zfcp_scsi_dev *zfcp_sdev;
Christof Schmitt287ac012008-07-02 10:56:40 +0200136
137 switch (want) {
Christof Schmittb62a8d92010-09-08 14:39:55 +0200138 case ZFCP_ERP_ACTION_REOPEN_LUN:
139 zfcp_sdev = sdev_to_zfcp(sdev);
140 l_status = atomic_read(&zfcp_sdev->status);
141 if (l_status & ZFCP_STATUS_COMMON_ERP_INUSE)
Christof Schmitt287ac012008-07-02 10:56:40 +0200142 return 0;
143 p_status = atomic_read(&port->status);
144 if (!(p_status & ZFCP_STATUS_COMMON_RUNNING) ||
145 p_status & ZFCP_STATUS_COMMON_ERP_FAILED)
146 return 0;
147 if (!(p_status & ZFCP_STATUS_COMMON_UNBLOCKED))
148 need = ZFCP_ERP_ACTION_REOPEN_PORT;
149 /* fall through */
Christof Schmitt287ac012008-07-02 10:56:40 +0200150 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
151 p_status = atomic_read(&port->status);
Christof Schmitt097ef3b2010-07-08 09:53:06 +0200152 if (!(p_status & ZFCP_STATUS_COMMON_OPEN))
153 need = ZFCP_ERP_ACTION_REOPEN_PORT;
154 /* fall through */
155 case ZFCP_ERP_ACTION_REOPEN_PORT:
156 p_status = atomic_read(&port->status);
Christof Schmitt287ac012008-07-02 10:56:40 +0200157 if (p_status & ZFCP_STATUS_COMMON_ERP_INUSE)
158 return 0;
159 a_status = atomic_read(&adapter->status);
160 if (!(a_status & ZFCP_STATUS_COMMON_RUNNING) ||
161 a_status & ZFCP_STATUS_COMMON_ERP_FAILED)
162 return 0;
Swen Schilligd3e10882010-11-17 14:23:42 +0100163 if (p_status & ZFCP_STATUS_COMMON_NOESC)
164 return need;
Christof Schmitt287ac012008-07-02 10:56:40 +0200165 if (!(a_status & ZFCP_STATUS_COMMON_UNBLOCKED))
166 need = ZFCP_ERP_ACTION_REOPEN_ADAPTER;
167 /* fall through */
168 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
169 a_status = atomic_read(&adapter->status);
170 if (a_status & ZFCP_STATUS_COMMON_ERP_INUSE)
171 return 0;
Christof Schmitt143bb6b2009-08-18 15:43:27 +0200172 if (!(a_status & ZFCP_STATUS_COMMON_RUNNING) &&
173 !(a_status & ZFCP_STATUS_COMMON_OPEN))
174 return 0; /* shutdown requested for closed adapter */
Christof Schmitt287ac012008-07-02 10:56:40 +0200175 }
176
177 return need;
178}
179
Christof Schmittfdbd1c52010-09-08 14:39:54 +0200180static struct zfcp_erp_action *zfcp_erp_setup_act(int need, u32 act_status,
Christof Schmitt287ac012008-07-02 10:56:40 +0200181 struct zfcp_adapter *adapter,
182 struct zfcp_port *port,
Christof Schmittb62a8d92010-09-08 14:39:55 +0200183 struct scsi_device *sdev)
Christof Schmitt287ac012008-07-02 10:56:40 +0200184{
185 struct zfcp_erp_action *erp_action;
Christof Schmittb62a8d92010-09-08 14:39:55 +0200186 struct zfcp_scsi_dev *zfcp_sdev;
Christof Schmitt287ac012008-07-02 10:56:40 +0200187
188 switch (need) {
Christof Schmittb62a8d92010-09-08 14:39:55 +0200189 case ZFCP_ERP_ACTION_REOPEN_LUN:
190 zfcp_sdev = sdev_to_zfcp(sdev);
Christof Schmittfdbd1c52010-09-08 14:39:54 +0200191 if (!(act_status & ZFCP_STATUS_ERP_NO_REF))
Christof Schmittb62a8d92010-09-08 14:39:55 +0200192 if (scsi_device_get(sdev))
Christof Schmittfdbd1c52010-09-08 14:39:54 +0200193 return NULL;
Peter Zijlstra805de8f42015-04-24 01:12:32 +0200194 atomic_or(ZFCP_STATUS_COMMON_ERP_INUSE,
Christof Schmittb62a8d92010-09-08 14:39:55 +0200195 &zfcp_sdev->status);
196 erp_action = &zfcp_sdev->erp_action;
Steffen Maierab31fd02017-10-13 15:40:07 +0200197 WARN_ON_ONCE(erp_action->port != port);
198 WARN_ON_ONCE(erp_action->sdev != sdev);
Christof Schmittb62a8d92010-09-08 14:39:55 +0200199 if (!(atomic_read(&zfcp_sdev->status) &
200 ZFCP_STATUS_COMMON_RUNNING))
Christof Schmittfdbd1c52010-09-08 14:39:54 +0200201 act_status |= ZFCP_STATUS_ERP_CLOSE_ONLY;
Christof Schmitt287ac012008-07-02 10:56:40 +0200202 break;
203
204 case ZFCP_ERP_ACTION_REOPEN_PORT:
205 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
Christof Schmitt615f59e2010-02-17 11:18:56 +0100206 if (!get_device(&port->dev))
Swen Schillig6b1833342009-11-24 16:54:05 +0100207 return NULL;
Christof Schmitt287ac012008-07-02 10:56:40 +0200208 zfcp_erp_action_dismiss_port(port);
Peter Zijlstra805de8f42015-04-24 01:12:32 +0200209 atomic_or(ZFCP_STATUS_COMMON_ERP_INUSE, &port->status);
Christof Schmitt287ac012008-07-02 10:56:40 +0200210 erp_action = &port->erp_action;
Steffen Maierab31fd02017-10-13 15:40:07 +0200211 WARN_ON_ONCE(erp_action->port != port);
212 WARN_ON_ONCE(erp_action->sdev != NULL);
Christof Schmitt287ac012008-07-02 10:56:40 +0200213 if (!(atomic_read(&port->status) & ZFCP_STATUS_COMMON_RUNNING))
Christof Schmittfdbd1c52010-09-08 14:39:54 +0200214 act_status |= ZFCP_STATUS_ERP_CLOSE_ONLY;
Christof Schmitt287ac012008-07-02 10:56:40 +0200215 break;
216
217 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
Swen Schilligf3450c72009-11-24 16:53:59 +0100218 kref_get(&adapter->ref);
Christof Schmitt287ac012008-07-02 10:56:40 +0200219 zfcp_erp_action_dismiss_adapter(adapter);
Peter Zijlstra805de8f42015-04-24 01:12:32 +0200220 atomic_or(ZFCP_STATUS_COMMON_ERP_INUSE, &adapter->status);
Christof Schmitt287ac012008-07-02 10:56:40 +0200221 erp_action = &adapter->erp_action;
Steffen Maierab31fd02017-10-13 15:40:07 +0200222 WARN_ON_ONCE(erp_action->port != NULL);
223 WARN_ON_ONCE(erp_action->sdev != NULL);
Christof Schmitt287ac012008-07-02 10:56:40 +0200224 if (!(atomic_read(&adapter->status) &
225 ZFCP_STATUS_COMMON_RUNNING))
Christof Schmittfdbd1c52010-09-08 14:39:54 +0200226 act_status |= ZFCP_STATUS_ERP_CLOSE_ONLY;
Christof Schmitt287ac012008-07-02 10:56:40 +0200227 break;
228
229 default:
230 return NULL;
231 }
232
Steffen Maierab31fd02017-10-13 15:40:07 +0200233 WARN_ON_ONCE(erp_action->adapter != adapter);
234 memset(&erp_action->list, 0, sizeof(erp_action->list));
235 memset(&erp_action->timer, 0, sizeof(erp_action->timer));
236 erp_action->step = ZFCP_ERP_STEP_UNINITIALIZED;
237 erp_action->fsf_req_id = 0;
Christof Schmitt287ac012008-07-02 10:56:40 +0200238 erp_action->action = need;
Christof Schmittfdbd1c52010-09-08 14:39:54 +0200239 erp_action->status = act_status;
Christof Schmitt287ac012008-07-02 10:56:40 +0200240
241 return erp_action;
242}
243
244static int zfcp_erp_action_enqueue(int want, struct zfcp_adapter *adapter,
245 struct zfcp_port *port,
Christof Schmittb62a8d92010-09-08 14:39:55 +0200246 struct scsi_device *sdev,
Swen Schilligea4a3a62010-12-02 15:16:16 +0100247 char *id, u32 act_status)
Christof Schmitt287ac012008-07-02 10:56:40 +0200248{
249 int retval = 1, need;
Swen Schilligae0904f2010-12-02 15:16:12 +0100250 struct zfcp_erp_action *act;
Christof Schmitt287ac012008-07-02 10:56:40 +0200251
Christof Schmitt347c6a92009-08-18 15:43:25 +0200252 if (!adapter->erp_thread)
Christof Schmitt287ac012008-07-02 10:56:40 +0200253 return -EIO;
254
Christof Schmittb62a8d92010-09-08 14:39:55 +0200255 need = zfcp_erp_required_act(want, adapter, port, sdev);
Christof Schmitt287ac012008-07-02 10:56:40 +0200256 if (!need)
257 goto out;
258
Christof Schmittb62a8d92010-09-08 14:39:55 +0200259 act = zfcp_erp_setup_act(need, act_status, adapter, port, sdev);
Christof Schmitt287ac012008-07-02 10:56:40 +0200260 if (!act)
261 goto out;
Peter Zijlstra805de8f42015-04-24 01:12:32 +0200262 atomic_or(ZFCP_STATUS_ADAPTER_ERP_PENDING, &adapter->status);
Christof Schmitt287ac012008-07-02 10:56:40 +0200263 ++adapter->erp_total_count;
264 list_add_tail(&act->list, &adapter->erp_ready_head);
Christof Schmitt347c6a92009-08-18 15:43:25 +0200265 wake_up(&adapter->erp_ready_wq);
Christof Schmitt287ac012008-07-02 10:56:40 +0200266 retval = 0;
267 out:
Swen Schilligae0904f2010-12-02 15:16:12 +0100268 zfcp_dbf_rec_trig(id, adapter, port, sdev, want, need);
Christof Schmitt287ac012008-07-02 10:56:40 +0200269 return retval;
270}
271
272static int _zfcp_erp_adapter_reopen(struct zfcp_adapter *adapter,
Swen Schilligea4a3a62010-12-02 15:16:16 +0100273 int clear_mask, char *id)
Christof Schmitt287ac012008-07-02 10:56:40 +0200274{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 zfcp_erp_adapter_block(adapter, clear_mask);
Christof Schmitta2fa0ae2009-03-02 13:09:08 +0100276 zfcp_scsi_schedule_rports_block(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
Christof Schmitt287ac012008-07-02 10:56:40 +0200278 /* ensure propagation of failed status to new devices */
279 if (atomic_read(&adapter->status) & ZFCP_STATUS_COMMON_ERP_FAILED) {
Swen Schilligedaed852010-09-08 14:40:01 +0200280 zfcp_erp_set_adapter_status(adapter,
281 ZFCP_STATUS_COMMON_ERP_FAILED);
Christof Schmitt287ac012008-07-02 10:56:40 +0200282 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 }
Christof Schmitt287ac012008-07-02 10:56:40 +0200284 return zfcp_erp_action_enqueue(ZFCP_ERP_ACTION_REOPEN_ADAPTER,
Swen Schilligea4a3a62010-12-02 15:16:16 +0100285 adapter, NULL, NULL, id, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286}
287
288/**
Christof Schmitt287ac012008-07-02 10:56:40 +0200289 * zfcp_erp_adapter_reopen - Reopen adapter.
290 * @adapter: Adapter to reopen.
291 * @clear: Status flags to clear.
292 * @id: Id for debug trace event.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 */
Swen Schilligea4a3a62010-12-02 15:16:16 +0100294void zfcp_erp_adapter_reopen(struct zfcp_adapter *adapter, int clear, char *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295{
Christof Schmitt287ac012008-07-02 10:56:40 +0200296 unsigned long flags;
297
Swen Schilligecf0c772009-11-24 16:53:58 +0100298 zfcp_erp_adapter_block(adapter, clear);
299 zfcp_scsi_schedule_rports_block(adapter);
300
301 write_lock_irqsave(&adapter->erp_lock, flags);
302 if (atomic_read(&adapter->status) & ZFCP_STATUS_COMMON_ERP_FAILED)
Swen Schilligedaed852010-09-08 14:40:01 +0200303 zfcp_erp_set_adapter_status(adapter,
304 ZFCP_STATUS_COMMON_ERP_FAILED);
Swen Schilligecf0c772009-11-24 16:53:58 +0100305 else
306 zfcp_erp_action_enqueue(ZFCP_ERP_ACTION_REOPEN_ADAPTER, adapter,
Swen Schilligea4a3a62010-12-02 15:16:16 +0100307 NULL, NULL, id, 0);
Swen Schilligecf0c772009-11-24 16:53:58 +0100308 write_unlock_irqrestore(&adapter->erp_lock, flags);
Christof Schmitt287ac012008-07-02 10:56:40 +0200309}
310
311/**
312 * zfcp_erp_adapter_shutdown - Shutdown adapter.
313 * @adapter: Adapter to shut down.
314 * @clear: Status flags to clear.
315 * @id: Id for debug trace event.
Christof Schmitt287ac012008-07-02 10:56:40 +0200316 */
317void zfcp_erp_adapter_shutdown(struct zfcp_adapter *adapter, int clear,
Swen Schilligea4a3a62010-12-02 15:16:16 +0100318 char *id)
Christof Schmitt287ac012008-07-02 10:56:40 +0200319{
320 int flags = ZFCP_STATUS_COMMON_RUNNING | ZFCP_STATUS_COMMON_ERP_FAILED;
Swen Schilligea4a3a62010-12-02 15:16:16 +0100321 zfcp_erp_adapter_reopen(adapter, clear | flags, id);
Christof Schmitt287ac012008-07-02 10:56:40 +0200322}
323
324/**
325 * zfcp_erp_port_shutdown - Shutdown port
326 * @port: Port to shut down.
327 * @clear: Status flags to clear.
328 * @id: Id for debug trace event.
Christof Schmitt287ac012008-07-02 10:56:40 +0200329 */
Swen Schilligea4a3a62010-12-02 15:16:16 +0100330void zfcp_erp_port_shutdown(struct zfcp_port *port, int clear, char *id)
Christof Schmitt287ac012008-07-02 10:56:40 +0200331{
332 int flags = ZFCP_STATUS_COMMON_RUNNING | ZFCP_STATUS_COMMON_ERP_FAILED;
Swen Schilligea4a3a62010-12-02 15:16:16 +0100333 zfcp_erp_port_reopen(port, clear | flags, id);
Christof Schmitt287ac012008-07-02 10:56:40 +0200334}
335
Christof Schmitt287ac012008-07-02 10:56:40 +0200336static void zfcp_erp_port_block(struct zfcp_port *port, int clear)
337{
Swen Schilligedaed852010-09-08 14:40:01 +0200338 zfcp_erp_clear_port_status(port,
339 ZFCP_STATUS_COMMON_UNBLOCKED | clear);
Christof Schmitt287ac012008-07-02 10:56:40 +0200340}
341
Swen Schilligea4a3a62010-12-02 15:16:16 +0100342static void _zfcp_erp_port_forced_reopen(struct zfcp_port *port, int clear,
343 char *id)
Christof Schmitt287ac012008-07-02 10:56:40 +0200344{
345 zfcp_erp_port_block(port, clear);
Christof Schmitta2fa0ae2009-03-02 13:09:08 +0100346 zfcp_scsi_schedule_rport_block(port);
Christof Schmitt287ac012008-07-02 10:56:40 +0200347
348 if (atomic_read(&port->status) & ZFCP_STATUS_COMMON_ERP_FAILED)
349 return;
350
351 zfcp_erp_action_enqueue(ZFCP_ERP_ACTION_REOPEN_PORT_FORCED,
Swen Schilligea4a3a62010-12-02 15:16:16 +0100352 port->adapter, port, NULL, id, 0);
Christof Schmitt287ac012008-07-02 10:56:40 +0200353}
354
355/**
356 * zfcp_erp_port_forced_reopen - Forced close of port and open again
357 * @port: Port to force close and to reopen.
Swen Schilligea4a3a62010-12-02 15:16:16 +0100358 * @clear: Status flags to clear.
Christof Schmitt287ac012008-07-02 10:56:40 +0200359 * @id: Id for debug trace event.
Christof Schmitt287ac012008-07-02 10:56:40 +0200360 */
Swen Schilligea4a3a62010-12-02 15:16:16 +0100361void zfcp_erp_port_forced_reopen(struct zfcp_port *port, int clear, char *id)
Christof Schmitt287ac012008-07-02 10:56:40 +0200362{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 unsigned long flags;
364 struct zfcp_adapter *adapter = port->adapter;
365
Swen Schilligecf0c772009-11-24 16:53:58 +0100366 write_lock_irqsave(&adapter->erp_lock, flags);
Swen Schilligea4a3a62010-12-02 15:16:16 +0100367 _zfcp_erp_port_forced_reopen(port, clear, id);
Swen Schilligecf0c772009-11-24 16:53:58 +0100368 write_unlock_irqrestore(&adapter->erp_lock, flags);
Christof Schmitt287ac012008-07-02 10:56:40 +0200369}
370
Swen Schilligea4a3a62010-12-02 15:16:16 +0100371static int _zfcp_erp_port_reopen(struct zfcp_port *port, int clear, char *id)
Christof Schmitt287ac012008-07-02 10:56:40 +0200372{
373 zfcp_erp_port_block(port, clear);
Christof Schmitta2fa0ae2009-03-02 13:09:08 +0100374 zfcp_scsi_schedule_rport_block(port);
Christof Schmitt287ac012008-07-02 10:56:40 +0200375
376 if (atomic_read(&port->status) & ZFCP_STATUS_COMMON_ERP_FAILED) {
377 /* ensure propagation of failed status to new devices */
Swen Schilligedaed852010-09-08 14:40:01 +0200378 zfcp_erp_set_port_status(port, ZFCP_STATUS_COMMON_ERP_FAILED);
Christof Schmitt287ac012008-07-02 10:56:40 +0200379 return -EIO;
380 }
381
382 return zfcp_erp_action_enqueue(ZFCP_ERP_ACTION_REOPEN_PORT,
Swen Schilligea4a3a62010-12-02 15:16:16 +0100383 port->adapter, port, NULL, id, 0);
Christof Schmitt287ac012008-07-02 10:56:40 +0200384}
385
386/**
387 * zfcp_erp_port_reopen - trigger remote port recovery
388 * @port: port to recover
389 * @clear_mask: flags in port status to be cleared
Swen Schilligea4a3a62010-12-02 15:16:16 +0100390 * @id: Id for debug trace event.
Christof Schmitt287ac012008-07-02 10:56:40 +0200391 *
392 * Returns 0 if recovery has been triggered, < 0 if not.
393 */
Swen Schilligea4a3a62010-12-02 15:16:16 +0100394int zfcp_erp_port_reopen(struct zfcp_port *port, int clear, char *id)
Christof Schmitt287ac012008-07-02 10:56:40 +0200395{
Christof Schmitt287ac012008-07-02 10:56:40 +0200396 int retval;
Swen Schilligecf0c772009-11-24 16:53:58 +0100397 unsigned long flags;
Christof Schmitt287ac012008-07-02 10:56:40 +0200398 struct zfcp_adapter *adapter = port->adapter;
399
Swen Schilligecf0c772009-11-24 16:53:58 +0100400 write_lock_irqsave(&adapter->erp_lock, flags);
Swen Schilligea4a3a62010-12-02 15:16:16 +0100401 retval = _zfcp_erp_port_reopen(port, clear, id);
Swen Schilligecf0c772009-11-24 16:53:58 +0100402 write_unlock_irqrestore(&adapter->erp_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
404 return retval;
405}
406
Christof Schmittb62a8d92010-09-08 14:39:55 +0200407static void zfcp_erp_lun_block(struct scsi_device *sdev, int clear_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408{
Swen Schilligedaed852010-09-08 14:40:01 +0200409 zfcp_erp_clear_lun_status(sdev,
410 ZFCP_STATUS_COMMON_UNBLOCKED | clear_mask);
Christof Schmitt287ac012008-07-02 10:56:40 +0200411}
412
Christof Schmittb62a8d92010-09-08 14:39:55 +0200413static void _zfcp_erp_lun_reopen(struct scsi_device *sdev, int clear, char *id,
Swen Schilligea4a3a62010-12-02 15:16:16 +0100414 u32 act_status)
Christof Schmitt287ac012008-07-02 10:56:40 +0200415{
Christof Schmittb62a8d92010-09-08 14:39:55 +0200416 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
417 struct zfcp_adapter *adapter = zfcp_sdev->port->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
Christof Schmittb62a8d92010-09-08 14:39:55 +0200419 zfcp_erp_lun_block(sdev, clear);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
Christof Schmittb62a8d92010-09-08 14:39:55 +0200421 if (atomic_read(&zfcp_sdev->status) & ZFCP_STATUS_COMMON_ERP_FAILED)
Christof Schmitt287ac012008-07-02 10:56:40 +0200422 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
Christof Schmittb62a8d92010-09-08 14:39:55 +0200424 zfcp_erp_action_enqueue(ZFCP_ERP_ACTION_REOPEN_LUN, adapter,
Swen Schilligea4a3a62010-12-02 15:16:16 +0100425 zfcp_sdev->port, sdev, id, act_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426}
427
428/**
Christof Schmittb62a8d92010-09-08 14:39:55 +0200429 * zfcp_erp_lun_reopen - initiate reopen of a LUN
430 * @sdev: SCSI device / LUN to be reopened
431 * @clear_mask: specifies flags in LUN status to be cleared
Swen Schilligea4a3a62010-12-02 15:16:16 +0100432 * @id: Id for debug trace event.
433 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 * Return: 0 on success, < 0 on error
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 */
Swen Schilligea4a3a62010-12-02 15:16:16 +0100436void zfcp_erp_lun_reopen(struct scsi_device *sdev, int clear, char *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 unsigned long flags;
Christof Schmittb62a8d92010-09-08 14:39:55 +0200439 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
440 struct zfcp_port *port = zfcp_sdev->port;
Christof Schmitt287ac012008-07-02 10:56:40 +0200441 struct zfcp_adapter *adapter = port->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
Swen Schilligecf0c772009-11-24 16:53:58 +0100443 write_lock_irqsave(&adapter->erp_lock, flags);
Swen Schilligea4a3a62010-12-02 15:16:16 +0100444 _zfcp_erp_lun_reopen(sdev, clear, id, 0);
Swen Schilligecf0c772009-11-24 16:53:58 +0100445 write_unlock_irqrestore(&adapter->erp_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446}
447
Christof Schmittfdbd1c52010-09-08 14:39:54 +0200448/**
Christof Schmittb62a8d92010-09-08 14:39:55 +0200449 * zfcp_erp_lun_shutdown - Shutdown LUN
450 * @sdev: SCSI device / LUN to shut down.
Christof Schmittfdbd1c52010-09-08 14:39:54 +0200451 * @clear: Status flags to clear.
452 * @id: Id for debug trace event.
Christof Schmittfdbd1c52010-09-08 14:39:54 +0200453 */
Swen Schilligea4a3a62010-12-02 15:16:16 +0100454void zfcp_erp_lun_shutdown(struct scsi_device *sdev, int clear, char *id)
Christof Schmittfdbd1c52010-09-08 14:39:54 +0200455{
456 int flags = ZFCP_STATUS_COMMON_RUNNING | ZFCP_STATUS_COMMON_ERP_FAILED;
Swen Schilligea4a3a62010-12-02 15:16:16 +0100457 zfcp_erp_lun_reopen(sdev, clear | flags, id);
Christof Schmittfdbd1c52010-09-08 14:39:54 +0200458}
459
460/**
Christof Schmittb62a8d92010-09-08 14:39:55 +0200461 * zfcp_erp_lun_shutdown_wait - Shutdown LUN and wait for erp completion
462 * @sdev: SCSI device / LUN to shut down.
Christof Schmittfdbd1c52010-09-08 14:39:54 +0200463 * @id: Id for debug trace event.
464 *
Christof Schmittb62a8d92010-09-08 14:39:55 +0200465 * Do not acquire a reference for the LUN when creating the ERP
Christof Schmittfdbd1c52010-09-08 14:39:54 +0200466 * action. It is safe, because this function waits for the ERP to
Christof Schmittb62a8d92010-09-08 14:39:55 +0200467 * complete first. This allows to shutdown the LUN, even when the SCSI
468 * device is in the state SDEV_DEL when scsi_device_get will fail.
Christof Schmittfdbd1c52010-09-08 14:39:54 +0200469 */
Christof Schmittb62a8d92010-09-08 14:39:55 +0200470void zfcp_erp_lun_shutdown_wait(struct scsi_device *sdev, char *id)
Christof Schmittfdbd1c52010-09-08 14:39:54 +0200471{
472 unsigned long flags;
Christof Schmittb62a8d92010-09-08 14:39:55 +0200473 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
474 struct zfcp_port *port = zfcp_sdev->port;
Christof Schmittfdbd1c52010-09-08 14:39:54 +0200475 struct zfcp_adapter *adapter = port->adapter;
476 int clear = ZFCP_STATUS_COMMON_RUNNING | ZFCP_STATUS_COMMON_ERP_FAILED;
477
478 write_lock_irqsave(&adapter->erp_lock, flags);
Swen Schilligea4a3a62010-12-02 15:16:16 +0100479 _zfcp_erp_lun_reopen(sdev, clear, id, ZFCP_STATUS_ERP_NO_REF);
Christof Schmittfdbd1c52010-09-08 14:39:54 +0200480 write_unlock_irqrestore(&adapter->erp_lock, flags);
481
482 zfcp_erp_wait(adapter);
483}
484
Christof Schmitt287ac012008-07-02 10:56:40 +0200485static int status_change_set(unsigned long mask, atomic_t *status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486{
Christof Schmitt287ac012008-07-02 10:56:40 +0200487 return (atomic_read(status) ^ mask) & mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488}
489
Andreas Herrmannf6c0e7a2006-08-02 11:05:52 +0200490static void zfcp_erp_adapter_unblock(struct zfcp_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491{
Christof Schmitt287ac012008-07-02 10:56:40 +0200492 if (status_change_set(ZFCP_STATUS_COMMON_UNBLOCKED, &adapter->status))
Swen Schilligae0904f2010-12-02 15:16:12 +0100493 zfcp_dbf_rec_run("eraubl1", &adapter->erp_action);
Peter Zijlstra805de8f42015-04-24 01:12:32 +0200494 atomic_or(ZFCP_STATUS_COMMON_UNBLOCKED, &adapter->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495}
496
Christof Schmitt287ac012008-07-02 10:56:40 +0200497static void zfcp_erp_port_unblock(struct zfcp_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498{
Christof Schmitt287ac012008-07-02 10:56:40 +0200499 if (status_change_set(ZFCP_STATUS_COMMON_UNBLOCKED, &port->status))
Swen Schilligae0904f2010-12-02 15:16:12 +0100500 zfcp_dbf_rec_run("erpubl1", &port->erp_action);
Peter Zijlstra805de8f42015-04-24 01:12:32 +0200501 atomic_or(ZFCP_STATUS_COMMON_UNBLOCKED, &port->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502}
503
Christof Schmittb62a8d92010-09-08 14:39:55 +0200504static void zfcp_erp_lun_unblock(struct scsi_device *sdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505{
Christof Schmittb62a8d92010-09-08 14:39:55 +0200506 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
507
508 if (status_change_set(ZFCP_STATUS_COMMON_UNBLOCKED, &zfcp_sdev->status))
Swen Schilligae0904f2010-12-02 15:16:12 +0100509 zfcp_dbf_rec_run("erlubl1", &sdev_to_zfcp(sdev)->erp_action);
Peter Zijlstra805de8f42015-04-24 01:12:32 +0200510 atomic_or(ZFCP_STATUS_COMMON_UNBLOCKED, &zfcp_sdev->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511}
512
Christof Schmitt287ac012008-07-02 10:56:40 +0200513static void zfcp_erp_action_to_running(struct zfcp_erp_action *erp_action)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514{
Christof Schmitt287ac012008-07-02 10:56:40 +0200515 list_move(&erp_action->list, &erp_action->adapter->erp_running_head);
Swen Schilligae0904f2010-12-02 15:16:12 +0100516 zfcp_dbf_rec_run("erator1", erp_action);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517}
518
Christof Schmitt287ac012008-07-02 10:56:40 +0200519static void zfcp_erp_strategy_check_fsfreq(struct zfcp_erp_action *act)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520{
Christof Schmitt287ac012008-07-02 10:56:40 +0200521 struct zfcp_adapter *adapter = act->adapter;
Christof Schmitte60a6d62010-02-17 11:18:49 +0100522 struct zfcp_fsf_req *req;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
Christof Schmitte60a6d62010-02-17 11:18:49 +0100524 if (!act->fsf_req_id)
Christof Schmitt287ac012008-07-02 10:56:40 +0200525 return;
526
Christof Schmittb6bd2fb2010-02-17 11:18:50 +0100527 spin_lock(&adapter->req_list->lock);
528 req = _zfcp_reqlist_find(adapter->req_list, act->fsf_req_id);
Christof Schmitte60a6d62010-02-17 11:18:49 +0100529 if (req && req->erp_action == act) {
Christof Schmitt287ac012008-07-02 10:56:40 +0200530 if (act->status & (ZFCP_STATUS_ERP_DISMISSED |
531 ZFCP_STATUS_ERP_TIMEDOUT)) {
Christof Schmitte60a6d62010-02-17 11:18:49 +0100532 req->status |= ZFCP_STATUS_FSFREQ_DISMISSED;
Swen Schilligae0904f2010-12-02 15:16:12 +0100533 zfcp_dbf_rec_run("erscf_1", act);
Christof Schmitte60a6d62010-02-17 11:18:49 +0100534 req->erp_action = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 }
Christof Schmitt287ac012008-07-02 10:56:40 +0200536 if (act->status & ZFCP_STATUS_ERP_TIMEDOUT)
Swen Schilligae0904f2010-12-02 15:16:12 +0100537 zfcp_dbf_rec_run("erscf_2", act);
Christof Schmitte60a6d62010-02-17 11:18:49 +0100538 if (req->status & ZFCP_STATUS_FSFREQ_DISMISSED)
539 act->fsf_req_id = 0;
Christof Schmitt287ac012008-07-02 10:56:40 +0200540 } else
Christof Schmitte60a6d62010-02-17 11:18:49 +0100541 act->fsf_req_id = 0;
Christof Schmittb6bd2fb2010-02-17 11:18:50 +0100542 spin_unlock(&adapter->req_list->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543}
544
Andreas Herrmannf6c0e7a2006-08-02 11:05:52 +0200545/**
Christof Schmitt287ac012008-07-02 10:56:40 +0200546 * zfcp_erp_notify - Trigger ERP action.
547 * @erp_action: ERP action to continue.
548 * @set_mask: ERP action status flags to set.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 */
Christof Schmitt287ac012008-07-02 10:56:40 +0200550void zfcp_erp_notify(struct zfcp_erp_action *erp_action, unsigned long set_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551{
552 struct zfcp_adapter *adapter = erp_action->adapter;
553 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
555 write_lock_irqsave(&adapter->erp_lock, flags);
Christof Schmitt287ac012008-07-02 10:56:40 +0200556 if (zfcp_erp_action_exists(erp_action) == ZFCP_ERP_ACTION_RUNNING) {
557 erp_action->status |= set_mask;
558 zfcp_erp_action_ready(erp_action);
559 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 write_unlock_irqrestore(&adapter->erp_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561}
562
Andreas Herrmannf6c0e7a2006-08-02 11:05:52 +0200563/**
Christof Schmitt287ac012008-07-02 10:56:40 +0200564 * zfcp_erp_timeout_handler - Trigger ERP action from timed out ERP request
565 * @data: ERP action (from timer data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 */
Kees Cook75492a52017-10-16 16:44:34 -0700567void zfcp_erp_timeout_handler(struct timer_list *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568{
Kees Cook75492a52017-10-16 16:44:34 -0700569 struct zfcp_fsf_req *fsf_req = from_timer(fsf_req, t, timer);
570 struct zfcp_erp_action *act = fsf_req->erp_action;
Steffen Maier5c13db92017-10-17 18:40:51 +0200571
Christof Schmitt287ac012008-07-02 10:56:40 +0200572 zfcp_erp_notify(act, ZFCP_STATUS_ERP_TIMEDOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573}
574
Kees Cook75492a52017-10-16 16:44:34 -0700575static void zfcp_erp_memwait_handler(struct timer_list *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576{
Kees Cook75492a52017-10-16 16:44:34 -0700577 struct zfcp_erp_action *act = from_timer(act, t, timer);
578
579 zfcp_erp_notify(act, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580}
581
Christof Schmitt287ac012008-07-02 10:56:40 +0200582static void zfcp_erp_strategy_memwait(struct zfcp_erp_action *erp_action)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583{
Steffen Maier5c13db92017-10-17 18:40:51 +0200584 timer_setup(&erp_action->timer, zfcp_erp_memwait_handler, 0);
Christof Schmitt287ac012008-07-02 10:56:40 +0200585 erp_action->timer.expires = jiffies + HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 add_timer(&erp_action->timer);
Christof Schmitt287ac012008-07-02 10:56:40 +0200587}
588
589static void _zfcp_erp_port_reopen_all(struct zfcp_adapter *adapter,
Swen Schilligea4a3a62010-12-02 15:16:16 +0100590 int clear, char *id)
Christof Schmitt287ac012008-07-02 10:56:40 +0200591{
592 struct zfcp_port *port;
593
Swen Schilligecf0c772009-11-24 16:53:58 +0100594 read_lock(&adapter->port_list_lock);
595 list_for_each_entry(port, &adapter->port_list, list)
Swen Schilligea4a3a62010-12-02 15:16:16 +0100596 _zfcp_erp_port_reopen(port, clear, id);
Swen Schilligecf0c772009-11-24 16:53:58 +0100597 read_unlock(&adapter->port_list_lock);
Christof Schmitt287ac012008-07-02 10:56:40 +0200598}
599
Christof Schmittb62a8d92010-09-08 14:39:55 +0200600static void _zfcp_erp_lun_reopen_all(struct zfcp_port *port, int clear,
Swen Schilligea4a3a62010-12-02 15:16:16 +0100601 char *id)
Christof Schmitt287ac012008-07-02 10:56:40 +0200602{
Christof Schmittb62a8d92010-09-08 14:39:55 +0200603 struct scsi_device *sdev;
Christof Schmitt287ac012008-07-02 10:56:40 +0200604
Martin Peschke924dd582013-08-22 17:45:37 +0200605 spin_lock(port->adapter->scsi_host->host_lock);
606 __shost_for_each_device(sdev, port->adapter->scsi_host)
Christof Schmittb62a8d92010-09-08 14:39:55 +0200607 if (sdev_to_zfcp(sdev)->port == port)
Swen Schilligea4a3a62010-12-02 15:16:16 +0100608 _zfcp_erp_lun_reopen(sdev, clear, id, 0);
Martin Peschke924dd582013-08-22 17:45:37 +0200609 spin_unlock(port->adapter->scsi_host->host_lock);
Christof Schmitt287ac012008-07-02 10:56:40 +0200610}
611
Christof Schmitt85600f72009-07-13 15:06:09 +0200612static void zfcp_erp_strategy_followup_failed(struct zfcp_erp_action *act)
Christof Schmitt287ac012008-07-02 10:56:40 +0200613{
Christof Schmitt287ac012008-07-02 10:56:40 +0200614 switch (act->action) {
Christof Schmitt287ac012008-07-02 10:56:40 +0200615 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
Swen Schilligea4a3a62010-12-02 15:16:16 +0100616 _zfcp_erp_adapter_reopen(act->adapter, 0, "ersff_1");
Christof Schmitt287ac012008-07-02 10:56:40 +0200617 break;
Christof Schmitt287ac012008-07-02 10:56:40 +0200618 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
Swen Schilligea4a3a62010-12-02 15:16:16 +0100619 _zfcp_erp_port_forced_reopen(act->port, 0, "ersff_2");
Christof Schmitt287ac012008-07-02 10:56:40 +0200620 break;
Christof Schmitt287ac012008-07-02 10:56:40 +0200621 case ZFCP_ERP_ACTION_REOPEN_PORT:
Swen Schilligea4a3a62010-12-02 15:16:16 +0100622 _zfcp_erp_port_reopen(act->port, 0, "ersff_3");
Christof Schmitt287ac012008-07-02 10:56:40 +0200623 break;
Christof Schmittb62a8d92010-09-08 14:39:55 +0200624 case ZFCP_ERP_ACTION_REOPEN_LUN:
Swen Schilligea4a3a62010-12-02 15:16:16 +0100625 _zfcp_erp_lun_reopen(act->sdev, 0, "ersff_4", 0);
Christof Schmitt85600f72009-07-13 15:06:09 +0200626 break;
627 }
628}
629
630static void zfcp_erp_strategy_followup_success(struct zfcp_erp_action *act)
631{
632 switch (act->action) {
633 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
Swen Schilligea4a3a62010-12-02 15:16:16 +0100634 _zfcp_erp_port_reopen_all(act->adapter, 0, "ersfs_1");
Christof Schmitt85600f72009-07-13 15:06:09 +0200635 break;
636 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
Swen Schilligea4a3a62010-12-02 15:16:16 +0100637 _zfcp_erp_port_reopen(act->port, 0, "ersfs_2");
Christof Schmitt85600f72009-07-13 15:06:09 +0200638 break;
639 case ZFCP_ERP_ACTION_REOPEN_PORT:
Swen Schilligea4a3a62010-12-02 15:16:16 +0100640 _zfcp_erp_lun_reopen_all(act->port, 0, "ersfs_3");
Christof Schmitt287ac012008-07-02 10:56:40 +0200641 break;
642 }
643}
644
645static void zfcp_erp_wakeup(struct zfcp_adapter *adapter)
646{
647 unsigned long flags;
648
Swen Schilligecf0c772009-11-24 16:53:58 +0100649 read_lock_irqsave(&adapter->erp_lock, flags);
Christof Schmitt287ac012008-07-02 10:56:40 +0200650 if (list_empty(&adapter->erp_ready_head) &&
651 list_empty(&adapter->erp_running_head)) {
Peter Zijlstra805de8f42015-04-24 01:12:32 +0200652 atomic_andnot(ZFCP_STATUS_ADAPTER_ERP_PENDING,
Christof Schmitt287ac012008-07-02 10:56:40 +0200653 &adapter->status);
654 wake_up(&adapter->erp_done_wqh);
655 }
Swen Schilligecf0c772009-11-24 16:53:58 +0100656 read_unlock_irqrestore(&adapter->erp_lock, flags);
Christof Schmitt287ac012008-07-02 10:56:40 +0200657}
658
Christof Schmitt287ac012008-07-02 10:56:40 +0200659static void zfcp_erp_enqueue_ptp_port(struct zfcp_adapter *adapter)
660{
661 struct zfcp_port *port;
662 port = zfcp_port_enqueue(adapter, adapter->peer_wwpn, 0,
663 adapter->peer_d_id);
664 if (IS_ERR(port)) /* error or port already attached */
665 return;
Swen Schilligea4a3a62010-12-02 15:16:16 +0100666 _zfcp_erp_port_reopen(port, 0, "ereptp1");
Christof Schmitt287ac012008-07-02 10:56:40 +0200667}
668
669static int zfcp_erp_adapter_strat_fsf_xconf(struct zfcp_erp_action *erp_action)
670{
671 int retries;
672 int sleep = 1;
673 struct zfcp_adapter *adapter = erp_action->adapter;
674
Peter Zijlstra805de8f42015-04-24 01:12:32 +0200675 atomic_andnot(ZFCP_STATUS_ADAPTER_XCONFIG_OK, &adapter->status);
Christof Schmitt287ac012008-07-02 10:56:40 +0200676
677 for (retries = 7; retries; retries--) {
Peter Zijlstra805de8f42015-04-24 01:12:32 +0200678 atomic_andnot(ZFCP_STATUS_ADAPTER_HOST_CON_INIT,
Christof Schmitt287ac012008-07-02 10:56:40 +0200679 &adapter->status);
680 write_lock_irq(&adapter->erp_lock);
681 zfcp_erp_action_to_running(erp_action);
682 write_unlock_irq(&adapter->erp_lock);
683 if (zfcp_fsf_exchange_config_data(erp_action)) {
Peter Zijlstra805de8f42015-04-24 01:12:32 +0200684 atomic_andnot(ZFCP_STATUS_ADAPTER_HOST_CON_INIT,
Christof Schmitt287ac012008-07-02 10:56:40 +0200685 &adapter->status);
686 return ZFCP_ERP_FAILED;
687 }
688
Christof Schmitt347c6a92009-08-18 15:43:25 +0200689 wait_event(adapter->erp_ready_wq,
690 !list_empty(&adapter->erp_ready_head));
Christof Schmitt287ac012008-07-02 10:56:40 +0200691 if (erp_action->status & ZFCP_STATUS_ERP_TIMEDOUT)
692 break;
693
694 if (!(atomic_read(&adapter->status) &
695 ZFCP_STATUS_ADAPTER_HOST_CON_INIT))
696 break;
697
698 ssleep(sleep);
699 sleep *= 2;
700 }
701
Peter Zijlstra805de8f42015-04-24 01:12:32 +0200702 atomic_andnot(ZFCP_STATUS_ADAPTER_HOST_CON_INIT,
Christof Schmitt287ac012008-07-02 10:56:40 +0200703 &adapter->status);
704
705 if (!(atomic_read(&adapter->status) & ZFCP_STATUS_ADAPTER_XCONFIG_OK))
706 return ZFCP_ERP_FAILED;
707
708 if (fc_host_port_type(adapter->scsi_host) == FC_PORTTYPE_PTP)
709 zfcp_erp_enqueue_ptp_port(adapter);
710
711 return ZFCP_ERP_SUCCEEDED;
712}
713
714static int zfcp_erp_adapter_strategy_open_fsf_xport(struct zfcp_erp_action *act)
715{
716 int ret;
717 struct zfcp_adapter *adapter = act->adapter;
718
Christof Schmitt287ac012008-07-02 10:56:40 +0200719 write_lock_irq(&adapter->erp_lock);
720 zfcp_erp_action_to_running(act);
721 write_unlock_irq(&adapter->erp_lock);
722
723 ret = zfcp_fsf_exchange_port_data(act);
724 if (ret == -EOPNOTSUPP)
725 return ZFCP_ERP_SUCCEEDED;
726 if (ret)
727 return ZFCP_ERP_FAILED;
728
Swen Schilligae0904f2010-12-02 15:16:12 +0100729 zfcp_dbf_rec_run("erasox1", act);
Christof Schmitt347c6a92009-08-18 15:43:25 +0200730 wait_event(adapter->erp_ready_wq,
731 !list_empty(&adapter->erp_ready_head));
Swen Schilligae0904f2010-12-02 15:16:12 +0100732 zfcp_dbf_rec_run("erasox2", act);
Christof Schmitt287ac012008-07-02 10:56:40 +0200733 if (act->status & ZFCP_STATUS_ERP_TIMEDOUT)
734 return ZFCP_ERP_FAILED;
735
736 return ZFCP_ERP_SUCCEEDED;
737}
738
739static int zfcp_erp_adapter_strategy_open_fsf(struct zfcp_erp_action *act)
740{
741 if (zfcp_erp_adapter_strat_fsf_xconf(act) == ZFCP_ERP_FAILED)
742 return ZFCP_ERP_FAILED;
743
744 if (zfcp_erp_adapter_strategy_open_fsf_xport(act) == ZFCP_ERP_FAILED)
745 return ZFCP_ERP_FAILED;
746
Christof Schmittc7b279a2011-02-22 19:54:40 +0100747 if (mempool_resize(act->adapter->pool.sr_data,
David Rientjes11d83362015-04-14 15:48:21 -0700748 act->adapter->stat_read_buf_num))
Christof Schmitt8d88cf32010-06-21 10:11:33 +0200749 return ZFCP_ERP_FAILED;
750
751 if (mempool_resize(act->adapter->pool.status_read_req,
David Rientjes11d83362015-04-14 15:48:21 -0700752 act->adapter->stat_read_buf_num))
Christof Schmitt8d88cf32010-06-21 10:11:33 +0200753 return ZFCP_ERP_FAILED;
754
Christof Schmitt64deb6e2010-04-30 18:09:36 +0200755 atomic_set(&act->adapter->stat_miss, act->adapter->stat_read_buf_num);
Christof Schmitt287ac012008-07-02 10:56:40 +0200756 if (zfcp_status_read_refill(act->adapter))
757 return ZFCP_ERP_FAILED;
758
759 return ZFCP_ERP_SUCCEEDED;
760}
761
Swen Schilligcf13c082009-03-02 13:09:03 +0100762static void zfcp_erp_adapter_strategy_close(struct zfcp_erp_action *act)
Christof Schmitt287ac012008-07-02 10:56:40 +0200763{
Christof Schmitt287ac012008-07-02 10:56:40 +0200764 struct zfcp_adapter *adapter = act->adapter;
765
Christof Schmitt287ac012008-07-02 10:56:40 +0200766 /* close queues to ensure that buffers are not accessed by adapter */
Swen Schillig564e1c82009-08-18 15:43:19 +0200767 zfcp_qdio_close(adapter->qdio);
Christof Schmitt287ac012008-07-02 10:56:40 +0200768 zfcp_fsf_req_dismiss_all(adapter);
769 adapter->fsf_req_seq_no = 0;
Christof Schmitt55c770f2009-08-18 15:43:12 +0200770 zfcp_fc_wka_ports_force_offline(adapter->gs);
Christof Schmittb62a8d92010-09-08 14:39:55 +0200771 /* all ports and LUNs are closed */
Swen Schilligedaed852010-09-08 14:40:01 +0200772 zfcp_erp_clear_adapter_status(adapter, ZFCP_STATUS_COMMON_OPEN);
Swen Schilligcf13c082009-03-02 13:09:03 +0100773
Peter Zijlstra805de8f42015-04-24 01:12:32 +0200774 atomic_andnot(ZFCP_STATUS_ADAPTER_XCONFIG_OK |
Swen Schilligcf13c082009-03-02 13:09:03 +0100775 ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED, &adapter->status);
776}
777
778static int zfcp_erp_adapter_strategy_open(struct zfcp_erp_action *act)
779{
780 struct zfcp_adapter *adapter = act->adapter;
781
Christof Schmitt3d63d3b2010-12-02 15:16:17 +0100782 if (zfcp_qdio_open(adapter->qdio)) {
Peter Zijlstra805de8f42015-04-24 01:12:32 +0200783 atomic_andnot(ZFCP_STATUS_ADAPTER_XCONFIG_OK |
Swen Schilligcf13c082009-03-02 13:09:03 +0100784 ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED,
785 &adapter->status);
786 return ZFCP_ERP_FAILED;
787 }
788
789 if (zfcp_erp_adapter_strategy_open_fsf(act)) {
790 zfcp_erp_adapter_strategy_close(act);
791 return ZFCP_ERP_FAILED;
792 }
793
Peter Zijlstra805de8f42015-04-24 01:12:32 +0200794 atomic_or(ZFCP_STATUS_COMMON_OPEN, &adapter->status);
Swen Schilligcf13c082009-03-02 13:09:03 +0100795
796 return ZFCP_ERP_SUCCEEDED;
Christof Schmitt287ac012008-07-02 10:56:40 +0200797}
798
799static int zfcp_erp_adapter_strategy(struct zfcp_erp_action *act)
800{
Swen Schilligcf13c082009-03-02 13:09:03 +0100801 struct zfcp_adapter *adapter = act->adapter;
Christof Schmitt287ac012008-07-02 10:56:40 +0200802
Swen Schilligcf13c082009-03-02 13:09:03 +0100803 if (atomic_read(&adapter->status) & ZFCP_STATUS_COMMON_OPEN) {
804 zfcp_erp_adapter_strategy_close(act);
805 if (act->status & ZFCP_STATUS_ERP_CLOSE_ONLY)
806 return ZFCP_ERP_EXIT;
807 }
Christof Schmitt287ac012008-07-02 10:56:40 +0200808
Swen Schilligcf13c082009-03-02 13:09:03 +0100809 if (zfcp_erp_adapter_strategy_open(act)) {
Christof Schmitt287ac012008-07-02 10:56:40 +0200810 ssleep(8);
Swen Schilligcf13c082009-03-02 13:09:03 +0100811 return ZFCP_ERP_FAILED;
812 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813
Swen Schilligcf13c082009-03-02 13:09:03 +0100814 return ZFCP_ERP_SUCCEEDED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815}
816
Christof Schmitt287ac012008-07-02 10:56:40 +0200817static int zfcp_erp_port_forced_strategy_close(struct zfcp_erp_action *act)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818{
Christof Schmitt287ac012008-07-02 10:56:40 +0200819 int retval;
820
821 retval = zfcp_fsf_close_physical_port(act);
822 if (retval == -ENOMEM)
823 return ZFCP_ERP_NOMEM;
824 act->step = ZFCP_ERP_STEP_PHYS_PORT_CLOSING;
825 if (retval)
826 return ZFCP_ERP_FAILED;
827
828 return ZFCP_ERP_CONTINUES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829}
830
Christof Schmitt287ac012008-07-02 10:56:40 +0200831static int zfcp_erp_port_forced_strategy(struct zfcp_erp_action *erp_action)
832{
833 struct zfcp_port *port = erp_action->port;
834 int status = atomic_read(&port->status);
835
836 switch (erp_action->step) {
837 case ZFCP_ERP_STEP_UNINITIALIZED:
Christof Schmitt287ac012008-07-02 10:56:40 +0200838 if ((status & ZFCP_STATUS_PORT_PHYS_OPEN) &&
839 (status & ZFCP_STATUS_COMMON_OPEN))
840 return zfcp_erp_port_forced_strategy_close(erp_action);
841 else
842 return ZFCP_ERP_FAILED;
843
844 case ZFCP_ERP_STEP_PHYS_PORT_CLOSING:
Christof Schmittddb3e0c2009-07-13 15:06:08 +0200845 if (!(status & ZFCP_STATUS_PORT_PHYS_OPEN))
Christof Schmitt287ac012008-07-02 10:56:40 +0200846 return ZFCP_ERP_SUCCEEDED;
847 }
848 return ZFCP_ERP_FAILED;
849}
850
851static int zfcp_erp_port_strategy_close(struct zfcp_erp_action *erp_action)
852{
853 int retval;
854
855 retval = zfcp_fsf_close_port(erp_action);
856 if (retval == -ENOMEM)
857 return ZFCP_ERP_NOMEM;
858 erp_action->step = ZFCP_ERP_STEP_PORT_CLOSING;
859 if (retval)
860 return ZFCP_ERP_FAILED;
861 return ZFCP_ERP_CONTINUES;
862}
863
864static int zfcp_erp_port_strategy_open_port(struct zfcp_erp_action *erp_action)
865{
866 int retval;
867
868 retval = zfcp_fsf_open_port(erp_action);
869 if (retval == -ENOMEM)
870 return ZFCP_ERP_NOMEM;
871 erp_action->step = ZFCP_ERP_STEP_PORT_OPENING;
872 if (retval)
873 return ZFCP_ERP_FAILED;
874 return ZFCP_ERP_CONTINUES;
875}
876
Christof Schmitt287ac012008-07-02 10:56:40 +0200877static int zfcp_erp_open_ptp_port(struct zfcp_erp_action *act)
878{
879 struct zfcp_adapter *adapter = act->adapter;
880 struct zfcp_port *port = act->port;
881
882 if (port->wwpn != adapter->peer_wwpn) {
Swen Schilligedaed852010-09-08 14:40:01 +0200883 zfcp_erp_set_port_status(port, ZFCP_STATUS_COMMON_ERP_FAILED);
Christof Schmitt287ac012008-07-02 10:56:40 +0200884 return ZFCP_ERP_FAILED;
885 }
886 port->d_id = adapter->peer_d_id;
Christof Schmitt287ac012008-07-02 10:56:40 +0200887 return zfcp_erp_port_strategy_open_port(act);
888}
889
890static int zfcp_erp_port_strategy_open_common(struct zfcp_erp_action *act)
891{
892 struct zfcp_adapter *adapter = act->adapter;
893 struct zfcp_port *port = act->port;
Christof Schmitt287ac012008-07-02 10:56:40 +0200894 int p_status = atomic_read(&port->status);
895
896 switch (act->step) {
897 case ZFCP_ERP_STEP_UNINITIALIZED:
898 case ZFCP_ERP_STEP_PHYS_PORT_CLOSING:
899 case ZFCP_ERP_STEP_PORT_CLOSING:
900 if (fc_host_port_type(adapter->scsi_host) == FC_PORTTYPE_PTP)
901 return zfcp_erp_open_ptp_port(act);
Christof Schmittb98478d2008-12-19 16:56:59 +0100902 if (!port->d_id) {
Christof Schmitt934aeb5872009-10-14 11:00:43 +0200903 zfcp_fc_trigger_did_lookup(port);
Christof Schmitt799b76d2009-08-18 15:43:20 +0200904 return ZFCP_ERP_EXIT;
Christof Schmitt287ac012008-07-02 10:56:40 +0200905 }
Christof Schmitt287ac012008-07-02 10:56:40 +0200906 return zfcp_erp_port_strategy_open_port(act);
907
908 case ZFCP_ERP_STEP_PORT_OPENING:
909 /* D_ID might have changed during open */
Swen Schillig5ab944f2008-10-01 12:42:17 +0200910 if (p_status & ZFCP_STATUS_COMMON_OPEN) {
Christof Schmitt934aeb5872009-10-14 11:00:43 +0200911 if (!port->d_id) {
912 zfcp_fc_trigger_did_lookup(port);
913 return ZFCP_ERP_EXIT;
Swen Schillig5ab944f2008-10-01 12:42:17 +0200914 }
Christof Schmitt934aeb5872009-10-14 11:00:43 +0200915 return ZFCP_ERP_SUCCEEDED;
Swen Schillig5ab944f2008-10-01 12:42:17 +0200916 }
Swen Schilligea460a82009-05-15 13:18:20 +0200917 if (port->d_id && !(p_status & ZFCP_STATUS_COMMON_NOESC)) {
918 port->d_id = 0;
Christof Schmittf7bd7c32010-07-08 09:53:10 +0200919 return ZFCP_ERP_FAILED;
Swen Schilligea460a82009-05-15 13:18:20 +0200920 }
921 /* fall through otherwise */
Christof Schmitt287ac012008-07-02 10:56:40 +0200922 }
923 return ZFCP_ERP_FAILED;
924}
925
Christof Schmitt287ac012008-07-02 10:56:40 +0200926static int zfcp_erp_port_strategy(struct zfcp_erp_action *erp_action)
927{
928 struct zfcp_port *port = erp_action->port;
Christof Schmitt934aeb5872009-10-14 11:00:43 +0200929 int p_status = atomic_read(&port->status);
Christof Schmitt287ac012008-07-02 10:56:40 +0200930
Christof Schmitt934aeb5872009-10-14 11:00:43 +0200931 if ((p_status & ZFCP_STATUS_COMMON_NOESC) &&
932 !(p_status & ZFCP_STATUS_COMMON_OPEN))
Swen Schillig5ab944f2008-10-01 12:42:17 +0200933 goto close_init_done;
934
Christof Schmitt287ac012008-07-02 10:56:40 +0200935 switch (erp_action->step) {
936 case ZFCP_ERP_STEP_UNINITIALIZED:
Christof Schmitt934aeb5872009-10-14 11:00:43 +0200937 if (p_status & ZFCP_STATUS_COMMON_OPEN)
Christof Schmitt287ac012008-07-02 10:56:40 +0200938 return zfcp_erp_port_strategy_close(erp_action);
939 break;
940
941 case ZFCP_ERP_STEP_PORT_CLOSING:
Christof Schmitt934aeb5872009-10-14 11:00:43 +0200942 if (p_status & ZFCP_STATUS_COMMON_OPEN)
Christof Schmitt287ac012008-07-02 10:56:40 +0200943 return ZFCP_ERP_FAILED;
944 break;
945 }
Swen Schillig5ab944f2008-10-01 12:42:17 +0200946
947close_init_done:
Christof Schmitt287ac012008-07-02 10:56:40 +0200948 if (erp_action->status & ZFCP_STATUS_ERP_CLOSE_ONLY)
949 return ZFCP_ERP_EXIT;
Christof Schmitt287ac012008-07-02 10:56:40 +0200950
Swen Schillig5ab944f2008-10-01 12:42:17 +0200951 return zfcp_erp_port_strategy_open_common(erp_action);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952}
953
Christof Schmittb62a8d92010-09-08 14:39:55 +0200954static void zfcp_erp_lun_strategy_clearstati(struct scsi_device *sdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955{
Christof Schmittb62a8d92010-09-08 14:39:55 +0200956 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
957
Peter Zijlstra805de8f42015-04-24 01:12:32 +0200958 atomic_andnot(ZFCP_STATUS_COMMON_ACCESS_DENIED,
Christof Schmittb62a8d92010-09-08 14:39:55 +0200959 &zfcp_sdev->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960}
961
Christof Schmittb62a8d92010-09-08 14:39:55 +0200962static int zfcp_erp_lun_strategy_close(struct zfcp_erp_action *erp_action)
Christof Schmitt287ac012008-07-02 10:56:40 +0200963{
Christof Schmittb62a8d92010-09-08 14:39:55 +0200964 int retval = zfcp_fsf_close_lun(erp_action);
Christof Schmitt287ac012008-07-02 10:56:40 +0200965 if (retval == -ENOMEM)
966 return ZFCP_ERP_NOMEM;
Christof Schmittb62a8d92010-09-08 14:39:55 +0200967 erp_action->step = ZFCP_ERP_STEP_LUN_CLOSING;
Christof Schmitt287ac012008-07-02 10:56:40 +0200968 if (retval)
969 return ZFCP_ERP_FAILED;
970 return ZFCP_ERP_CONTINUES;
971}
972
Christof Schmittb62a8d92010-09-08 14:39:55 +0200973static int zfcp_erp_lun_strategy_open(struct zfcp_erp_action *erp_action)
Christof Schmitt287ac012008-07-02 10:56:40 +0200974{
Christof Schmittb62a8d92010-09-08 14:39:55 +0200975 int retval = zfcp_fsf_open_lun(erp_action);
Christof Schmitt287ac012008-07-02 10:56:40 +0200976 if (retval == -ENOMEM)
977 return ZFCP_ERP_NOMEM;
Christof Schmittb62a8d92010-09-08 14:39:55 +0200978 erp_action->step = ZFCP_ERP_STEP_LUN_OPENING;
Christof Schmitt287ac012008-07-02 10:56:40 +0200979 if (retval)
980 return ZFCP_ERP_FAILED;
981 return ZFCP_ERP_CONTINUES;
982}
983
Christof Schmittb62a8d92010-09-08 14:39:55 +0200984static int zfcp_erp_lun_strategy(struct zfcp_erp_action *erp_action)
Christof Schmitt287ac012008-07-02 10:56:40 +0200985{
Christof Schmittb62a8d92010-09-08 14:39:55 +0200986 struct scsi_device *sdev = erp_action->sdev;
987 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
Christof Schmitt287ac012008-07-02 10:56:40 +0200988
989 switch (erp_action->step) {
990 case ZFCP_ERP_STEP_UNINITIALIZED:
Christof Schmittb62a8d92010-09-08 14:39:55 +0200991 zfcp_erp_lun_strategy_clearstati(sdev);
992 if (atomic_read(&zfcp_sdev->status) & ZFCP_STATUS_COMMON_OPEN)
993 return zfcp_erp_lun_strategy_close(erp_action);
Christof Schmitt287ac012008-07-02 10:56:40 +0200994 /* already closed, fall through */
Christof Schmittb62a8d92010-09-08 14:39:55 +0200995 case ZFCP_ERP_STEP_LUN_CLOSING:
996 if (atomic_read(&zfcp_sdev->status) & ZFCP_STATUS_COMMON_OPEN)
Christof Schmitt287ac012008-07-02 10:56:40 +0200997 return ZFCP_ERP_FAILED;
998 if (erp_action->status & ZFCP_STATUS_ERP_CLOSE_ONLY)
999 return ZFCP_ERP_EXIT;
Christof Schmittb62a8d92010-09-08 14:39:55 +02001000 return zfcp_erp_lun_strategy_open(erp_action);
Christof Schmitt287ac012008-07-02 10:56:40 +02001001
Christof Schmittb62a8d92010-09-08 14:39:55 +02001002 case ZFCP_ERP_STEP_LUN_OPENING:
1003 if (atomic_read(&zfcp_sdev->status) & ZFCP_STATUS_COMMON_OPEN)
Christof Schmitt287ac012008-07-02 10:56:40 +02001004 return ZFCP_ERP_SUCCEEDED;
1005 }
1006 return ZFCP_ERP_FAILED;
1007}
1008
Christof Schmittb62a8d92010-09-08 14:39:55 +02001009static int zfcp_erp_strategy_check_lun(struct scsi_device *sdev, int result)
Christof Schmitt287ac012008-07-02 10:56:40 +02001010{
Christof Schmittb62a8d92010-09-08 14:39:55 +02001011 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
1012
Christof Schmitt287ac012008-07-02 10:56:40 +02001013 switch (result) {
1014 case ZFCP_ERP_SUCCEEDED :
Christof Schmittb62a8d92010-09-08 14:39:55 +02001015 atomic_set(&zfcp_sdev->erp_counter, 0);
1016 zfcp_erp_lun_unblock(sdev);
Christof Schmitt287ac012008-07-02 10:56:40 +02001017 break;
1018 case ZFCP_ERP_FAILED :
Christof Schmittb62a8d92010-09-08 14:39:55 +02001019 atomic_inc(&zfcp_sdev->erp_counter);
1020 if (atomic_read(&zfcp_sdev->erp_counter) > ZFCP_MAX_ERPS) {
1021 dev_err(&zfcp_sdev->port->adapter->ccw_device->dev,
1022 "ERP failed for LUN 0x%016Lx on "
Christof Schmittff3b24f2008-10-01 12:42:15 +02001023 "port 0x%016Lx\n",
Christof Schmittb62a8d92010-09-08 14:39:55 +02001024 (unsigned long long)zfcp_scsi_dev_lun(sdev),
1025 (unsigned long long)zfcp_sdev->port->wwpn);
Swen Schilligedaed852010-09-08 14:40:01 +02001026 zfcp_erp_set_lun_status(sdev,
1027 ZFCP_STATUS_COMMON_ERP_FAILED);
Christof Schmittff3b24f2008-10-01 12:42:15 +02001028 }
Christof Schmitt287ac012008-07-02 10:56:40 +02001029 break;
1030 }
1031
Christof Schmittb62a8d92010-09-08 14:39:55 +02001032 if (atomic_read(&zfcp_sdev->status) & ZFCP_STATUS_COMMON_ERP_FAILED) {
1033 zfcp_erp_lun_block(sdev, 0);
Christof Schmitt287ac012008-07-02 10:56:40 +02001034 result = ZFCP_ERP_EXIT;
1035 }
1036 return result;
1037}
1038
1039static int zfcp_erp_strategy_check_port(struct zfcp_port *port, int result)
1040{
1041 switch (result) {
1042 case ZFCP_ERP_SUCCEEDED :
1043 atomic_set(&port->erp_counter, 0);
1044 zfcp_erp_port_unblock(port);
1045 break;
1046
1047 case ZFCP_ERP_FAILED :
1048 if (atomic_read(&port->status) & ZFCP_STATUS_COMMON_NOESC) {
1049 zfcp_erp_port_block(port, 0);
1050 result = ZFCP_ERP_EXIT;
1051 }
1052 atomic_inc(&port->erp_counter);
Christof Schmittff3b24f2008-10-01 12:42:15 +02001053 if (atomic_read(&port->erp_counter) > ZFCP_MAX_ERPS) {
1054 dev_err(&port->adapter->ccw_device->dev,
1055 "ERP failed for remote port 0x%016Lx\n",
Swen Schillig7ba58c92008-10-01 12:42:18 +02001056 (unsigned long long)port->wwpn);
Swen Schilligedaed852010-09-08 14:40:01 +02001057 zfcp_erp_set_port_status(port,
1058 ZFCP_STATUS_COMMON_ERP_FAILED);
Christof Schmittff3b24f2008-10-01 12:42:15 +02001059 }
Christof Schmitt287ac012008-07-02 10:56:40 +02001060 break;
1061 }
1062
1063 if (atomic_read(&port->status) & ZFCP_STATUS_COMMON_ERP_FAILED) {
1064 zfcp_erp_port_block(port, 0);
1065 result = ZFCP_ERP_EXIT;
1066 }
1067 return result;
1068}
1069
1070static int zfcp_erp_strategy_check_adapter(struct zfcp_adapter *adapter,
1071 int result)
1072{
1073 switch (result) {
1074 case ZFCP_ERP_SUCCEEDED :
1075 atomic_set(&adapter->erp_counter, 0);
1076 zfcp_erp_adapter_unblock(adapter);
1077 break;
1078
1079 case ZFCP_ERP_FAILED :
1080 atomic_inc(&adapter->erp_counter);
Christof Schmittff3b24f2008-10-01 12:42:15 +02001081 if (atomic_read(&adapter->erp_counter) > ZFCP_MAX_ERPS) {
1082 dev_err(&adapter->ccw_device->dev,
1083 "ERP cannot recover an error "
1084 "on the FCP device\n");
Swen Schilligedaed852010-09-08 14:40:01 +02001085 zfcp_erp_set_adapter_status(adapter,
1086 ZFCP_STATUS_COMMON_ERP_FAILED);
Christof Schmittff3b24f2008-10-01 12:42:15 +02001087 }
Christof Schmitt287ac012008-07-02 10:56:40 +02001088 break;
1089 }
1090
1091 if (atomic_read(&adapter->status) & ZFCP_STATUS_COMMON_ERP_FAILED) {
1092 zfcp_erp_adapter_block(adapter, 0);
1093 result = ZFCP_ERP_EXIT;
1094 }
1095 return result;
1096}
1097
1098static int zfcp_erp_strategy_check_target(struct zfcp_erp_action *erp_action,
1099 int result)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100{
1101 struct zfcp_adapter *adapter = erp_action->adapter;
1102 struct zfcp_port *port = erp_action->port;
Christof Schmittb62a8d92010-09-08 14:39:55 +02001103 struct scsi_device *sdev = erp_action->sdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 switch (erp_action->action) {
1106
Christof Schmittb62a8d92010-09-08 14:39:55 +02001107 case ZFCP_ERP_ACTION_REOPEN_LUN:
1108 result = zfcp_erp_strategy_check_lun(sdev, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 break;
1110
1111 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
1112 case ZFCP_ERP_ACTION_REOPEN_PORT:
1113 result = zfcp_erp_strategy_check_port(port, result);
1114 break;
1115
1116 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
1117 result = zfcp_erp_strategy_check_adapter(adapter, result);
1118 break;
1119 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 return result;
1121}
1122
Christof Schmitt287ac012008-07-02 10:56:40 +02001123static int zfcp_erp_strat_change_det(atomic_t *target_status, u32 erp_status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124{
Christof Schmitt287ac012008-07-02 10:56:40 +02001125 int status = atomic_read(target_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126
Christof Schmitt287ac012008-07-02 10:56:40 +02001127 if ((status & ZFCP_STATUS_COMMON_RUNNING) &&
1128 (erp_status & ZFCP_STATUS_ERP_CLOSE_ONLY))
1129 return 1; /* take it online */
1130
1131 if (!(status & ZFCP_STATUS_COMMON_RUNNING) &&
1132 !(erp_status & ZFCP_STATUS_ERP_CLOSE_ONLY))
1133 return 1; /* take it offline */
1134
1135 return 0;
1136}
1137
1138static int zfcp_erp_strategy_statechange(struct zfcp_erp_action *act, int ret)
1139{
1140 int action = act->action;
1141 struct zfcp_adapter *adapter = act->adapter;
1142 struct zfcp_port *port = act->port;
Christof Schmittb62a8d92010-09-08 14:39:55 +02001143 struct scsi_device *sdev = act->sdev;
1144 struct zfcp_scsi_dev *zfcp_sdev;
Christof Schmitt287ac012008-07-02 10:56:40 +02001145 u32 erp_status = act->status;
1146
1147 switch (action) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
Christof Schmitt287ac012008-07-02 10:56:40 +02001149 if (zfcp_erp_strat_change_det(&adapter->status, erp_status)) {
1150 _zfcp_erp_adapter_reopen(adapter,
1151 ZFCP_STATUS_COMMON_ERP_FAILED,
Swen Schilligea4a3a62010-12-02 15:16:16 +01001152 "ersscg1");
Christof Schmitt287ac012008-07-02 10:56:40 +02001153 return ZFCP_ERP_EXIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 }
1155 break;
1156
1157 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
1158 case ZFCP_ERP_ACTION_REOPEN_PORT:
Christof Schmitt287ac012008-07-02 10:56:40 +02001159 if (zfcp_erp_strat_change_det(&port->status, erp_status)) {
1160 _zfcp_erp_port_reopen(port,
1161 ZFCP_STATUS_COMMON_ERP_FAILED,
Swen Schilligea4a3a62010-12-02 15:16:16 +01001162 "ersscg2");
Christof Schmitt287ac012008-07-02 10:56:40 +02001163 return ZFCP_ERP_EXIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 }
1165 break;
1166
Christof Schmittb62a8d92010-09-08 14:39:55 +02001167 case ZFCP_ERP_ACTION_REOPEN_LUN:
1168 zfcp_sdev = sdev_to_zfcp(sdev);
1169 if (zfcp_erp_strat_change_det(&zfcp_sdev->status, erp_status)) {
1170 _zfcp_erp_lun_reopen(sdev,
1171 ZFCP_STATUS_COMMON_ERP_FAILED,
Swen Schilligea4a3a62010-12-02 15:16:16 +01001172 "ersscg3", 0);
Christof Schmitt287ac012008-07-02 10:56:40 +02001173 return ZFCP_ERP_EXIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 }
1175 break;
1176 }
Christof Schmitt287ac012008-07-02 10:56:40 +02001177 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178}
1179
Christof Schmitt287ac012008-07-02 10:56:40 +02001180static void zfcp_erp_action_dequeue(struct zfcp_erp_action *erp_action)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181{
Christof Schmitt287ac012008-07-02 10:56:40 +02001182 struct zfcp_adapter *adapter = erp_action->adapter;
Christof Schmittb62a8d92010-09-08 14:39:55 +02001183 struct zfcp_scsi_dev *zfcp_sdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184
Christof Schmitt287ac012008-07-02 10:56:40 +02001185 adapter->erp_total_count--;
1186 if (erp_action->status & ZFCP_STATUS_ERP_LOWMEM) {
1187 adapter->erp_low_mem_count--;
1188 erp_action->status &= ~ZFCP_STATUS_ERP_LOWMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 }
1190
Christof Schmitt287ac012008-07-02 10:56:40 +02001191 list_del(&erp_action->list);
Swen Schilligae0904f2010-12-02 15:16:12 +01001192 zfcp_dbf_rec_run("eractd1", erp_action);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193
Christof Schmitt287ac012008-07-02 10:56:40 +02001194 switch (erp_action->action) {
Christof Schmittb62a8d92010-09-08 14:39:55 +02001195 case ZFCP_ERP_ACTION_REOPEN_LUN:
1196 zfcp_sdev = sdev_to_zfcp(erp_action->sdev);
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001197 atomic_andnot(ZFCP_STATUS_COMMON_ERP_INUSE,
Christof Schmittb62a8d92010-09-08 14:39:55 +02001198 &zfcp_sdev->status);
Christof Schmitt287ac012008-07-02 10:56:40 +02001199 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200
Christof Schmitt287ac012008-07-02 10:56:40 +02001201 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
1202 case ZFCP_ERP_ACTION_REOPEN_PORT:
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001203 atomic_andnot(ZFCP_STATUS_COMMON_ERP_INUSE,
Christof Schmitt287ac012008-07-02 10:56:40 +02001204 &erp_action->port->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 break;
Christof Schmitt287ac012008-07-02 10:56:40 +02001206
1207 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001208 atomic_andnot(ZFCP_STATUS_COMMON_ERP_INUSE,
Christof Schmitt287ac012008-07-02 10:56:40 +02001209 &erp_action->adapter->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 break;
1211 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212}
1213
Steffen Maier6f2ce1c2016-12-09 17:16:33 +01001214/**
1215 * zfcp_erp_try_rport_unblock - unblock rport if no more/new recovery
1216 * @port: zfcp_port whose fc_rport we should try to unblock
1217 */
1218static void zfcp_erp_try_rport_unblock(struct zfcp_port *port)
1219{
1220 unsigned long flags;
1221 struct zfcp_adapter *adapter = port->adapter;
1222 int port_status;
1223 struct Scsi_Host *shost = adapter->scsi_host;
1224 struct scsi_device *sdev;
1225
1226 write_lock_irqsave(&adapter->erp_lock, flags);
1227 port_status = atomic_read(&port->status);
1228 if ((port_status & ZFCP_STATUS_COMMON_UNBLOCKED) == 0 ||
1229 (port_status & (ZFCP_STATUS_COMMON_ERP_INUSE |
1230 ZFCP_STATUS_COMMON_ERP_FAILED)) != 0) {
1231 /* new ERP of severity >= port triggered elsewhere meanwhile or
1232 * local link down (adapter erp_failed but not clear unblock)
1233 */
1234 zfcp_dbf_rec_run_lvl(4, "ertru_p", &port->erp_action);
1235 write_unlock_irqrestore(&adapter->erp_lock, flags);
1236 return;
1237 }
1238 spin_lock(shost->host_lock);
1239 __shost_for_each_device(sdev, shost) {
1240 struct zfcp_scsi_dev *zsdev = sdev_to_zfcp(sdev);
1241 int lun_status;
1242
1243 if (zsdev->port != port)
1244 continue;
1245 /* LUN under port of interest */
1246 lun_status = atomic_read(&zsdev->status);
1247 if ((lun_status & ZFCP_STATUS_COMMON_ERP_FAILED) != 0)
1248 continue; /* unblock rport despite failed LUNs */
1249 /* LUN recovery not given up yet [maybe follow-up pending] */
1250 if ((lun_status & ZFCP_STATUS_COMMON_UNBLOCKED) == 0 ||
1251 (lun_status & ZFCP_STATUS_COMMON_ERP_INUSE) != 0) {
1252 /* LUN blocked:
1253 * not yet unblocked [LUN recovery pending]
1254 * or meanwhile blocked [new LUN recovery triggered]
1255 */
1256 zfcp_dbf_rec_run_lvl(4, "ertru_l", &zsdev->erp_action);
1257 spin_unlock(shost->host_lock);
1258 write_unlock_irqrestore(&adapter->erp_lock, flags);
1259 return;
1260 }
1261 }
1262 /* now port has no child or all children have completed recovery,
1263 * and no ERP of severity >= port was meanwhile triggered elsewhere
1264 */
1265 zfcp_scsi_schedule_rport_register(port);
1266 spin_unlock(shost->host_lock);
1267 write_unlock_irqrestore(&adapter->erp_lock, flags);
1268}
1269
Christof Schmitt287ac012008-07-02 10:56:40 +02001270static void zfcp_erp_action_cleanup(struct zfcp_erp_action *act, int result)
Maxim Shchetyninaef4a982005-09-13 21:51:16 +02001271{
Christof Schmitt287ac012008-07-02 10:56:40 +02001272 struct zfcp_adapter *adapter = act->adapter;
1273 struct zfcp_port *port = act->port;
Christof Schmittb62a8d92010-09-08 14:39:55 +02001274 struct scsi_device *sdev = act->sdev;
Maxim Shchetyninaef4a982005-09-13 21:51:16 +02001275
Christof Schmitt287ac012008-07-02 10:56:40 +02001276 switch (act->action) {
Christof Schmittb62a8d92010-09-08 14:39:55 +02001277 case ZFCP_ERP_ACTION_REOPEN_LUN:
Christof Schmittfdbd1c52010-09-08 14:39:54 +02001278 if (!(act->status & ZFCP_STATUS_ERP_NO_REF))
Christof Schmittb62a8d92010-09-08 14:39:55 +02001279 scsi_device_put(sdev);
Steffen Maier6f2ce1c2016-12-09 17:16:33 +01001280 zfcp_erp_try_rport_unblock(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 break;
Christof Schmitt287ac012008-07-02 10:56:40 +02001282
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 case ZFCP_ERP_ACTION_REOPEN_PORT:
Steffen Maier4eeaa4f2016-08-10 18:30:46 +02001284 /* This switch case might also happen after a forced reopen
1285 * was successfully done and thus overwritten with a new
1286 * non-forced reopen at `ersfs_2'. In this case, we must not
1287 * do the clean-up of the non-forced version.
1288 */
1289 if (act->step != ZFCP_ERP_STEP_UNINITIALIZED)
1290 if (result == ZFCP_ERP_SUCCEEDED)
Steffen Maier6f2ce1c2016-12-09 17:16:33 +01001291 zfcp_erp_try_rport_unblock(port);
Christof Schmitt57676202010-07-08 09:53:05 +02001292 /* fall through */
1293 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
Christof Schmitt615f59e2010-02-17 11:18:56 +01001294 put_device(&port->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 break;
Christof Schmitt287ac012008-07-02 10:56:40 +02001296
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
Christof Schmitta2fa0ae2009-03-02 13:09:08 +01001298 if (result == ZFCP_ERP_SUCCEEDED) {
Christof Schmittbd43a42b72008-12-25 13:38:50 +01001299 register_service_level(&adapter->service_level);
Steffen Maier43f60cb2012-09-04 15:23:35 +02001300 zfcp_fc_conditional_port_scan(adapter);
Christof Schmitt038d9442011-02-22 19:54:48 +01001301 queue_work(adapter->work_queue, &adapter->ns_up_work);
Christof Schmitta2fa0ae2009-03-02 13:09:08 +01001302 } else
1303 unregister_service_level(&adapter->service_level);
Christof Schmitt038d9442011-02-22 19:54:48 +01001304
Swen Schilligf3450c72009-11-24 16:53:59 +01001305 kref_put(&adapter->ref, zfcp_adapter_release);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 }
1308}
1309
Christof Schmitt287ac012008-07-02 10:56:40 +02001310static int zfcp_erp_strategy_do_action(struct zfcp_erp_action *erp_action)
1311{
1312 switch (erp_action->action) {
1313 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
1314 return zfcp_erp_adapter_strategy(erp_action);
1315 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
1316 return zfcp_erp_port_forced_strategy(erp_action);
1317 case ZFCP_ERP_ACTION_REOPEN_PORT:
1318 return zfcp_erp_port_strategy(erp_action);
Christof Schmittb62a8d92010-09-08 14:39:55 +02001319 case ZFCP_ERP_ACTION_REOPEN_LUN:
1320 return zfcp_erp_lun_strategy(erp_action);
Christof Schmitt287ac012008-07-02 10:56:40 +02001321 }
1322 return ZFCP_ERP_FAILED;
1323}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324
Christof Schmitt287ac012008-07-02 10:56:40 +02001325static int zfcp_erp_strategy(struct zfcp_erp_action *erp_action)
1326{
1327 int retval;
Christof Schmitt287ac012008-07-02 10:56:40 +02001328 unsigned long flags;
Swen Schilligecf0c772009-11-24 16:53:58 +01001329 struct zfcp_adapter *adapter = erp_action->adapter;
Christof Schmitt287ac012008-07-02 10:56:40 +02001330
Swen Schilligf3450c72009-11-24 16:53:59 +01001331 kref_get(&adapter->ref);
Christof Schmitt287ac012008-07-02 10:56:40 +02001332
Swen Schilligf3450c72009-11-24 16:53:59 +01001333 write_lock_irqsave(&adapter->erp_lock, flags);
Christof Schmitt287ac012008-07-02 10:56:40 +02001334 zfcp_erp_strategy_check_fsfreq(erp_action);
1335
1336 if (erp_action->status & ZFCP_STATUS_ERP_DISMISSED) {
1337 zfcp_erp_action_dequeue(erp_action);
1338 retval = ZFCP_ERP_DISMISSED;
1339 goto unlock;
1340 }
1341
Christof Schmitt9c785d92010-07-08 09:53:09 +02001342 if (erp_action->status & ZFCP_STATUS_ERP_TIMEDOUT) {
1343 retval = ZFCP_ERP_FAILED;
1344 goto check_target;
1345 }
1346
Christof Schmitt287ac012008-07-02 10:56:40 +02001347 zfcp_erp_action_to_running(erp_action);
1348
1349 /* no lock to allow for blocking operations */
Swen Schilligecf0c772009-11-24 16:53:58 +01001350 write_unlock_irqrestore(&adapter->erp_lock, flags);
Christof Schmitt287ac012008-07-02 10:56:40 +02001351 retval = zfcp_erp_strategy_do_action(erp_action);
Swen Schilligecf0c772009-11-24 16:53:58 +01001352 write_lock_irqsave(&adapter->erp_lock, flags);
Christof Schmitt287ac012008-07-02 10:56:40 +02001353
1354 if (erp_action->status & ZFCP_STATUS_ERP_DISMISSED)
1355 retval = ZFCP_ERP_CONTINUES;
1356
1357 switch (retval) {
1358 case ZFCP_ERP_NOMEM:
1359 if (!(erp_action->status & ZFCP_STATUS_ERP_LOWMEM)) {
1360 ++adapter->erp_low_mem_count;
1361 erp_action->status |= ZFCP_STATUS_ERP_LOWMEM;
1362 }
1363 if (adapter->erp_total_count == adapter->erp_low_mem_count)
Swen Schilligea4a3a62010-12-02 15:16:16 +01001364 _zfcp_erp_adapter_reopen(adapter, 0, "erstgy1");
Christof Schmitt287ac012008-07-02 10:56:40 +02001365 else {
1366 zfcp_erp_strategy_memwait(erp_action);
1367 retval = ZFCP_ERP_CONTINUES;
1368 }
1369 goto unlock;
1370
1371 case ZFCP_ERP_CONTINUES:
1372 if (erp_action->status & ZFCP_STATUS_ERP_LOWMEM) {
1373 --adapter->erp_low_mem_count;
1374 erp_action->status &= ~ZFCP_STATUS_ERP_LOWMEM;
1375 }
1376 goto unlock;
1377 }
1378
Christof Schmitt9c785d92010-07-08 09:53:09 +02001379check_target:
Christof Schmitt287ac012008-07-02 10:56:40 +02001380 retval = zfcp_erp_strategy_check_target(erp_action, retval);
1381 zfcp_erp_action_dequeue(erp_action);
1382 retval = zfcp_erp_strategy_statechange(erp_action, retval);
1383 if (retval == ZFCP_ERP_EXIT)
1384 goto unlock;
Christof Schmitt85600f72009-07-13 15:06:09 +02001385 if (retval == ZFCP_ERP_SUCCEEDED)
1386 zfcp_erp_strategy_followup_success(erp_action);
1387 if (retval == ZFCP_ERP_FAILED)
1388 zfcp_erp_strategy_followup_failed(erp_action);
Christof Schmitt287ac012008-07-02 10:56:40 +02001389
1390 unlock:
Swen Schilligecf0c772009-11-24 16:53:58 +01001391 write_unlock_irqrestore(&adapter->erp_lock, flags);
Christof Schmitt287ac012008-07-02 10:56:40 +02001392
1393 if (retval != ZFCP_ERP_CONTINUES)
1394 zfcp_erp_action_cleanup(erp_action, retval);
1395
Swen Schilligf3450c72009-11-24 16:53:59 +01001396 kref_put(&adapter->ref, zfcp_adapter_release);
Christof Schmitt287ac012008-07-02 10:56:40 +02001397 return retval;
1398}
1399
1400static int zfcp_erp_thread(void *data)
1401{
1402 struct zfcp_adapter *adapter = (struct zfcp_adapter *) data;
1403 struct list_head *next;
1404 struct zfcp_erp_action *act;
1405 unsigned long flags;
1406
Christof Schmitt347c6a92009-08-18 15:43:25 +02001407 for (;;) {
Christof Schmitt347c6a92009-08-18 15:43:25 +02001408 wait_event_interruptible(adapter->erp_ready_wq,
1409 !list_empty(&adapter->erp_ready_head) ||
1410 kthread_should_stop());
Swen Schillig94ab4b32009-04-17 15:08:06 +02001411
Christof Schmitt347c6a92009-08-18 15:43:25 +02001412 if (kthread_should_stop())
1413 break;
1414
Christof Schmitt287ac012008-07-02 10:56:40 +02001415 write_lock_irqsave(&adapter->erp_lock, flags);
1416 next = adapter->erp_ready_head.next;
1417 write_unlock_irqrestore(&adapter->erp_lock, flags);
1418
1419 if (next != &adapter->erp_ready_head) {
1420 act = list_entry(next, struct zfcp_erp_action, list);
1421
1422 /* there is more to come after dismission, no notify */
1423 if (zfcp_erp_strategy(act) != ZFCP_ERP_DISMISSED)
1424 zfcp_erp_wakeup(adapter);
1425 }
Christof Schmitt287ac012008-07-02 10:56:40 +02001426 }
1427
Christof Schmitt287ac012008-07-02 10:56:40 +02001428 return 0;
1429}
1430
1431/**
1432 * zfcp_erp_thread_setup - Start ERP thread for adapter
1433 * @adapter: Adapter to start the ERP thread for
1434 *
1435 * Returns 0 on success or error code from kernel_thread()
1436 */
1437int zfcp_erp_thread_setup(struct zfcp_adapter *adapter)
1438{
Christof Schmitt347c6a92009-08-18 15:43:25 +02001439 struct task_struct *thread;
Christof Schmitt287ac012008-07-02 10:56:40 +02001440
Christof Schmitt347c6a92009-08-18 15:43:25 +02001441 thread = kthread_run(zfcp_erp_thread, adapter, "zfcperp%s",
1442 dev_name(&adapter->ccw_device->dev));
1443 if (IS_ERR(thread)) {
Christof Schmitt287ac012008-07-02 10:56:40 +02001444 dev_err(&adapter->ccw_device->dev,
Christof Schmittff3b24f2008-10-01 12:42:15 +02001445 "Creating an ERP thread for the FCP device failed.\n");
Christof Schmitt347c6a92009-08-18 15:43:25 +02001446 return PTR_ERR(thread);
Christof Schmitt287ac012008-07-02 10:56:40 +02001447 }
Christof Schmitt347c6a92009-08-18 15:43:25 +02001448
1449 adapter->erp_thread = thread;
Christof Schmitt287ac012008-07-02 10:56:40 +02001450 return 0;
1451}
1452
1453/**
1454 * zfcp_erp_thread_kill - Stop ERP thread.
1455 * @adapter: Adapter where the ERP thread should be stopped.
1456 *
1457 * The caller of this routine ensures that the specified adapter has
1458 * been shut down and that this operation has been completed. Thus,
1459 * there are no pending erp_actions which would need to be handled
1460 * here.
1461 */
1462void zfcp_erp_thread_kill(struct zfcp_adapter *adapter)
1463{
Christof Schmitt347c6a92009-08-18 15:43:25 +02001464 kthread_stop(adapter->erp_thread);
1465 adapter->erp_thread = NULL;
Christof Schmitt143bb6b2009-08-18 15:43:27 +02001466 WARN_ON(!list_empty(&adapter->erp_ready_head));
1467 WARN_ON(!list_empty(&adapter->erp_running_head));
Christof Schmitt287ac012008-07-02 10:56:40 +02001468}
1469
1470/**
Christof Schmitt287ac012008-07-02 10:56:40 +02001471 * zfcp_erp_wait - wait for completion of error recovery on an adapter
1472 * @adapter: adapter for which to wait for completion of its error recovery
1473 */
1474void zfcp_erp_wait(struct zfcp_adapter *adapter)
1475{
1476 wait_event(adapter->erp_done_wqh,
1477 !(atomic_read(&adapter->status) &
1478 ZFCP_STATUS_ADAPTER_ERP_PENDING));
1479}
1480
1481/**
Swen Schilligedaed852010-09-08 14:40:01 +02001482 * zfcp_erp_set_adapter_status - set adapter status bits
Christof Schmitt287ac012008-07-02 10:56:40 +02001483 * @adapter: adapter to change the status
Christof Schmitt287ac012008-07-02 10:56:40 +02001484 * @mask: status bits to change
Christof Schmitt287ac012008-07-02 10:56:40 +02001485 *
Christof Schmittb62a8d92010-09-08 14:39:55 +02001486 * Changes in common status bits are propagated to attached ports and LUNs.
Christof Schmitt287ac012008-07-02 10:56:40 +02001487 */
Swen Schilligedaed852010-09-08 14:40:01 +02001488void zfcp_erp_set_adapter_status(struct zfcp_adapter *adapter, u32 mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 struct zfcp_port *port;
Swen Schilligedaed852010-09-08 14:40:01 +02001491 struct scsi_device *sdev;
Swen Schilligecf0c772009-11-24 16:53:58 +01001492 unsigned long flags;
Christof Schmitt287ac012008-07-02 10:56:40 +02001493 u32 common_mask = mask & ZFCP_COMMON_FLAGS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001495 atomic_or(mask, &adapter->status);
Christof Schmitt287ac012008-07-02 10:56:40 +02001496
Swen Schilligedaed852010-09-08 14:40:01 +02001497 if (!common_mask)
1498 return;
1499
1500 read_lock_irqsave(&adapter->port_list_lock, flags);
1501 list_for_each_entry(port, &adapter->port_list, list)
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001502 atomic_or(common_mask, &port->status);
Swen Schilligedaed852010-09-08 14:40:01 +02001503 read_unlock_irqrestore(&adapter->port_list_lock, flags);
1504
Martin Peschke924dd582013-08-22 17:45:37 +02001505 spin_lock_irqsave(adapter->scsi_host->host_lock, flags);
1506 __shost_for_each_device(sdev, adapter->scsi_host)
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001507 atomic_or(common_mask, &sdev_to_zfcp(sdev)->status);
Martin Peschke924dd582013-08-22 17:45:37 +02001508 spin_unlock_irqrestore(adapter->scsi_host->host_lock, flags);
Swen Schilligedaed852010-09-08 14:40:01 +02001509}
1510
1511/**
1512 * zfcp_erp_clear_adapter_status - clear adapter status bits
1513 * @adapter: adapter to change the status
1514 * @mask: status bits to change
1515 *
1516 * Changes in common status bits are propagated to attached ports and LUNs.
1517 */
1518void zfcp_erp_clear_adapter_status(struct zfcp_adapter *adapter, u32 mask)
1519{
1520 struct zfcp_port *port;
1521 struct scsi_device *sdev;
1522 unsigned long flags;
1523 u32 common_mask = mask & ZFCP_COMMON_FLAGS;
1524 u32 clear_counter = mask & ZFCP_STATUS_COMMON_ERP_FAILED;
1525
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001526 atomic_andnot(mask, &adapter->status);
Swen Schilligedaed852010-09-08 14:40:01 +02001527
1528 if (!common_mask)
1529 return;
1530
1531 if (clear_counter)
1532 atomic_set(&adapter->erp_counter, 0);
1533
1534 read_lock_irqsave(&adapter->port_list_lock, flags);
1535 list_for_each_entry(port, &adapter->port_list, list) {
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001536 atomic_andnot(common_mask, &port->status);
Swen Schilligedaed852010-09-08 14:40:01 +02001537 if (clear_counter)
1538 atomic_set(&port->erp_counter, 0);
1539 }
1540 read_unlock_irqrestore(&adapter->port_list_lock, flags);
1541
Martin Peschke924dd582013-08-22 17:45:37 +02001542 spin_lock_irqsave(adapter->scsi_host->host_lock, flags);
1543 __shost_for_each_device(sdev, adapter->scsi_host) {
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001544 atomic_andnot(common_mask, &sdev_to_zfcp(sdev)->status);
Swen Schilligedaed852010-09-08 14:40:01 +02001545 if (clear_counter)
1546 atomic_set(&sdev_to_zfcp(sdev)->erp_counter, 0);
Swen Schilligecf0c772009-11-24 16:53:58 +01001547 }
Martin Peschke924dd582013-08-22 17:45:37 +02001548 spin_unlock_irqrestore(adapter->scsi_host->host_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549}
1550
Christof Schmitt287ac012008-07-02 10:56:40 +02001551/**
Swen Schilligedaed852010-09-08 14:40:01 +02001552 * zfcp_erp_set_port_status - set port status bits
1553 * @port: port to change the status
Christof Schmitt287ac012008-07-02 10:56:40 +02001554 * @mask: status bits to change
Christof Schmitt287ac012008-07-02 10:56:40 +02001555 *
Christof Schmittb62a8d92010-09-08 14:39:55 +02001556 * Changes in common status bits are propagated to attached LUNs.
Christof Schmitt287ac012008-07-02 10:56:40 +02001557 */
Swen Schilligedaed852010-09-08 14:40:01 +02001558void zfcp_erp_set_port_status(struct zfcp_port *port, u32 mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559{
Christof Schmittb62a8d92010-09-08 14:39:55 +02001560 struct scsi_device *sdev;
Christof Schmitt287ac012008-07-02 10:56:40 +02001561 u32 common_mask = mask & ZFCP_COMMON_FLAGS;
Martin Peschke924dd582013-08-22 17:45:37 +02001562 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001564 atomic_or(mask, &port->status);
Christof Schmitt287ac012008-07-02 10:56:40 +02001565
Swen Schilligedaed852010-09-08 14:40:01 +02001566 if (!common_mask)
1567 return;
1568
Martin Peschke924dd582013-08-22 17:45:37 +02001569 spin_lock_irqsave(port->adapter->scsi_host->host_lock, flags);
1570 __shost_for_each_device(sdev, port->adapter->scsi_host)
Swen Schilligedaed852010-09-08 14:40:01 +02001571 if (sdev_to_zfcp(sdev)->port == port)
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001572 atomic_or(common_mask,
Swen Schilligedaed852010-09-08 14:40:01 +02001573 &sdev_to_zfcp(sdev)->status);
Martin Peschke924dd582013-08-22 17:45:37 +02001574 spin_unlock_irqrestore(port->adapter->scsi_host->host_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575}
1576
Christof Schmitt287ac012008-07-02 10:56:40 +02001577/**
Swen Schilligedaed852010-09-08 14:40:01 +02001578 * zfcp_erp_clear_port_status - clear port status bits
1579 * @port: adapter to change the status
Christof Schmitt287ac012008-07-02 10:56:40 +02001580 * @mask: status bits to change
Swen Schilligedaed852010-09-08 14:40:01 +02001581 *
1582 * Changes in common status bits are propagated to attached LUNs.
Christof Schmitt287ac012008-07-02 10:56:40 +02001583 */
Swen Schilligedaed852010-09-08 14:40:01 +02001584void zfcp_erp_clear_port_status(struct zfcp_port *port, u32 mask)
1585{
1586 struct scsi_device *sdev;
1587 u32 common_mask = mask & ZFCP_COMMON_FLAGS;
1588 u32 clear_counter = mask & ZFCP_STATUS_COMMON_ERP_FAILED;
Martin Peschke924dd582013-08-22 17:45:37 +02001589 unsigned long flags;
Swen Schilligedaed852010-09-08 14:40:01 +02001590
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001591 atomic_andnot(mask, &port->status);
Swen Schilligedaed852010-09-08 14:40:01 +02001592
1593 if (!common_mask)
1594 return;
1595
1596 if (clear_counter)
1597 atomic_set(&port->erp_counter, 0);
1598
Martin Peschke924dd582013-08-22 17:45:37 +02001599 spin_lock_irqsave(port->adapter->scsi_host->host_lock, flags);
1600 __shost_for_each_device(sdev, port->adapter->scsi_host)
Swen Schilligedaed852010-09-08 14:40:01 +02001601 if (sdev_to_zfcp(sdev)->port == port) {
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001602 atomic_andnot(common_mask,
Swen Schilligedaed852010-09-08 14:40:01 +02001603 &sdev_to_zfcp(sdev)->status);
1604 if (clear_counter)
1605 atomic_set(&sdev_to_zfcp(sdev)->erp_counter, 0);
1606 }
Martin Peschke924dd582013-08-22 17:45:37 +02001607 spin_unlock_irqrestore(port->adapter->scsi_host->host_lock, flags);
Swen Schilligedaed852010-09-08 14:40:01 +02001608}
1609
1610/**
1611 * zfcp_erp_set_lun_status - set lun status bits
1612 * @sdev: SCSI device / lun to set the status bits
1613 * @mask: status bits to change
1614 */
1615void zfcp_erp_set_lun_status(struct scsi_device *sdev, u32 mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616{
Christof Schmittb62a8d92010-09-08 14:39:55 +02001617 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
1618
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001619 atomic_or(mask, &zfcp_sdev->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620}
1621
Christof Schmitt287ac012008-07-02 10:56:40 +02001622/**
Swen Schilligedaed852010-09-08 14:40:01 +02001623 * zfcp_erp_clear_lun_status - clear lun status bits
1624 * @sdev: SCSi device / lun to clear the status bits
1625 * @mask: status bits to change
Christof Schmitt287ac012008-07-02 10:56:40 +02001626 */
Swen Schilligedaed852010-09-08 14:40:01 +02001627void zfcp_erp_clear_lun_status(struct scsi_device *sdev, u32 mask)
Andreas Herrmannd736a27b2005-06-13 13:23:57 +02001628{
Swen Schilligedaed852010-09-08 14:40:01 +02001629 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
1630
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001631 atomic_andnot(mask, &zfcp_sdev->status);
Swen Schilligedaed852010-09-08 14:40:01 +02001632
1633 if (mask & ZFCP_STATUS_COMMON_ERP_FAILED)
1634 atomic_set(&zfcp_sdev->erp_counter, 0);
Andreas Herrmannd736a27b2005-06-13 13:23:57 +02001635}
1636