blob: 822a852d578e9522d6d2ec10146d0cfe7e7e4272 [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
Kees Cook75492a52017-10-16 16:44:34 -070059static void zfcp_erp_memwait_handler(struct timer_list *t);
60
Christof Schmitt287ac012008-07-02 10:56:40 +020061static void zfcp_erp_adapter_block(struct zfcp_adapter *adapter, int mask)
Andreas Herrmann2abbe862006-09-18 22:29:56 +020062{
Swen Schilligedaed852010-09-08 14:40:01 +020063 zfcp_erp_clear_adapter_status(adapter,
64 ZFCP_STATUS_COMMON_UNBLOCKED | mask);
Andreas Herrmann2abbe862006-09-18 22:29:56 +020065}
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
Christof Schmitt287ac012008-07-02 10:56:40 +020067static int zfcp_erp_action_exists(struct zfcp_erp_action *act)
Linus Torvalds1da177e2005-04-16 15:20:36 -070068{
Christof Schmitt287ac012008-07-02 10:56:40 +020069 struct zfcp_erp_action *curr_act;
70
71 list_for_each_entry(curr_act, &act->adapter->erp_running_head, list)
72 if (act == curr_act)
73 return ZFCP_ERP_ACTION_RUNNING;
74 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075}
76
Christof Schmitt287ac012008-07-02 10:56:40 +020077static void zfcp_erp_action_ready(struct zfcp_erp_action *act)
Linus Torvalds1da177e2005-04-16 15:20:36 -070078{
Christof Schmitt287ac012008-07-02 10:56:40 +020079 struct zfcp_adapter *adapter = act->adapter;
80
81 list_move(&act->list, &act->adapter->erp_ready_head);
Swen Schilligae0904f2010-12-02 15:16:12 +010082 zfcp_dbf_rec_run("erardy1", act);
Christof Schmitt347c6a92009-08-18 15:43:25 +020083 wake_up(&adapter->erp_ready_wq);
Swen Schilligae0904f2010-12-02 15:16:12 +010084 zfcp_dbf_rec_run("erardy2", act);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085}
86
Christof Schmitt287ac012008-07-02 10:56:40 +020087static void zfcp_erp_action_dismiss(struct zfcp_erp_action *act)
Linus Torvalds1da177e2005-04-16 15:20:36 -070088{
Christof Schmitt287ac012008-07-02 10:56:40 +020089 act->status |= ZFCP_STATUS_ERP_DISMISSED;
90 if (zfcp_erp_action_exists(act) == ZFCP_ERP_ACTION_RUNNING)
91 zfcp_erp_action_ready(act);
92}
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
Christof Schmittb62a8d92010-09-08 14:39:55 +020094static void zfcp_erp_action_dismiss_lun(struct scsi_device *sdev)
Christof Schmitt287ac012008-07-02 10:56:40 +020095{
Christof Schmittb62a8d92010-09-08 14:39:55 +020096 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
97
98 if (atomic_read(&zfcp_sdev->status) & ZFCP_STATUS_COMMON_ERP_INUSE)
99 zfcp_erp_action_dismiss(&zfcp_sdev->erp_action);
Christof Schmitt287ac012008-07-02 10:56:40 +0200100}
101
102static void zfcp_erp_action_dismiss_port(struct zfcp_port *port)
103{
Christof Schmittb62a8d92010-09-08 14:39:55 +0200104 struct scsi_device *sdev;
Christof Schmitt287ac012008-07-02 10:56:40 +0200105
106 if (atomic_read(&port->status) & ZFCP_STATUS_COMMON_ERP_INUSE)
107 zfcp_erp_action_dismiss(&port->erp_action);
Martin Peschke924dd582013-08-22 17:45:37 +0200108 else {
109 spin_lock(port->adapter->scsi_host->host_lock);
110 __shost_for_each_device(sdev, port->adapter->scsi_host)
Christof Schmittb62a8d92010-09-08 14:39:55 +0200111 if (sdev_to_zfcp(sdev)->port == port)
112 zfcp_erp_action_dismiss_lun(sdev);
Martin Peschke924dd582013-08-22 17:45:37 +0200113 spin_unlock(port->adapter->scsi_host->host_lock);
114 }
Christof Schmitt287ac012008-07-02 10:56:40 +0200115}
116
117static void zfcp_erp_action_dismiss_adapter(struct zfcp_adapter *adapter)
118{
119 struct zfcp_port *port;
120
121 if (atomic_read(&adapter->status) & ZFCP_STATUS_COMMON_ERP_INUSE)
122 zfcp_erp_action_dismiss(&adapter->erp_action);
Swen Schilligecf0c772009-11-24 16:53:58 +0100123 else {
124 read_lock(&adapter->port_list_lock);
125 list_for_each_entry(port, &adapter->port_list, list)
Christof Schmitt287ac012008-07-02 10:56:40 +0200126 zfcp_erp_action_dismiss_port(port);
Swen Schilligecf0c772009-11-24 16:53:58 +0100127 read_unlock(&adapter->port_list_lock);
128 }
Christof Schmitt287ac012008-07-02 10:56:40 +0200129}
130
131static int zfcp_erp_required_act(int want, struct zfcp_adapter *adapter,
132 struct zfcp_port *port,
Christof Schmittb62a8d92010-09-08 14:39:55 +0200133 struct scsi_device *sdev)
Christof Schmitt287ac012008-07-02 10:56:40 +0200134{
135 int need = want;
Christof Schmittb62a8d92010-09-08 14:39:55 +0200136 int l_status, p_status, a_status;
137 struct zfcp_scsi_dev *zfcp_sdev;
Christof Schmitt287ac012008-07-02 10:56:40 +0200138
139 switch (want) {
Christof Schmittb62a8d92010-09-08 14:39:55 +0200140 case ZFCP_ERP_ACTION_REOPEN_LUN:
141 zfcp_sdev = sdev_to_zfcp(sdev);
142 l_status = atomic_read(&zfcp_sdev->status);
143 if (l_status & ZFCP_STATUS_COMMON_ERP_INUSE)
Christof Schmitt287ac012008-07-02 10:56:40 +0200144 return 0;
145 p_status = atomic_read(&port->status);
146 if (!(p_status & ZFCP_STATUS_COMMON_RUNNING) ||
147 p_status & ZFCP_STATUS_COMMON_ERP_FAILED)
148 return 0;
149 if (!(p_status & ZFCP_STATUS_COMMON_UNBLOCKED))
150 need = ZFCP_ERP_ACTION_REOPEN_PORT;
151 /* fall through */
Christof Schmitt287ac012008-07-02 10:56:40 +0200152 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
153 p_status = atomic_read(&port->status);
Christof Schmitt097ef3b2010-07-08 09:53:06 +0200154 if (!(p_status & ZFCP_STATUS_COMMON_OPEN))
155 need = ZFCP_ERP_ACTION_REOPEN_PORT;
156 /* fall through */
157 case ZFCP_ERP_ACTION_REOPEN_PORT:
158 p_status = atomic_read(&port->status);
Christof Schmitt287ac012008-07-02 10:56:40 +0200159 if (p_status & ZFCP_STATUS_COMMON_ERP_INUSE)
160 return 0;
161 a_status = atomic_read(&adapter->status);
162 if (!(a_status & ZFCP_STATUS_COMMON_RUNNING) ||
163 a_status & ZFCP_STATUS_COMMON_ERP_FAILED)
164 return 0;
Swen Schilligd3e10882010-11-17 14:23:42 +0100165 if (p_status & ZFCP_STATUS_COMMON_NOESC)
166 return need;
Christof Schmitt287ac012008-07-02 10:56:40 +0200167 if (!(a_status & ZFCP_STATUS_COMMON_UNBLOCKED))
168 need = ZFCP_ERP_ACTION_REOPEN_ADAPTER;
169 /* fall through */
170 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
171 a_status = atomic_read(&adapter->status);
172 if (a_status & ZFCP_STATUS_COMMON_ERP_INUSE)
173 return 0;
Christof Schmitt143bb6b2009-08-18 15:43:27 +0200174 if (!(a_status & ZFCP_STATUS_COMMON_RUNNING) &&
175 !(a_status & ZFCP_STATUS_COMMON_OPEN))
176 return 0; /* shutdown requested for closed adapter */
Christof Schmitt287ac012008-07-02 10:56:40 +0200177 }
178
179 return need;
180}
181
Christof Schmittfdbd1c52010-09-08 14:39:54 +0200182static struct zfcp_erp_action *zfcp_erp_setup_act(int need, u32 act_status,
Christof Schmitt287ac012008-07-02 10:56:40 +0200183 struct zfcp_adapter *adapter,
184 struct zfcp_port *port,
Christof Schmittb62a8d92010-09-08 14:39:55 +0200185 struct scsi_device *sdev)
Christof Schmitt287ac012008-07-02 10:56:40 +0200186{
187 struct zfcp_erp_action *erp_action;
Christof Schmittb62a8d92010-09-08 14:39:55 +0200188 struct zfcp_scsi_dev *zfcp_sdev;
Christof Schmitt287ac012008-07-02 10:56:40 +0200189
190 switch (need) {
Christof Schmittb62a8d92010-09-08 14:39:55 +0200191 case ZFCP_ERP_ACTION_REOPEN_LUN:
192 zfcp_sdev = sdev_to_zfcp(sdev);
Christof Schmittfdbd1c52010-09-08 14:39:54 +0200193 if (!(act_status & ZFCP_STATUS_ERP_NO_REF))
Christof Schmittb62a8d92010-09-08 14:39:55 +0200194 if (scsi_device_get(sdev))
Christof Schmittfdbd1c52010-09-08 14:39:54 +0200195 return NULL;
Peter Zijlstra805de8f42015-04-24 01:12:32 +0200196 atomic_or(ZFCP_STATUS_COMMON_ERP_INUSE,
Christof Schmittb62a8d92010-09-08 14:39:55 +0200197 &zfcp_sdev->status);
198 erp_action = &zfcp_sdev->erp_action;
Steffen Maierab31fd02017-10-13 15:40:07 +0200199 WARN_ON_ONCE(erp_action->port != port);
200 WARN_ON_ONCE(erp_action->sdev != sdev);
Christof Schmittb62a8d92010-09-08 14:39:55 +0200201 if (!(atomic_read(&zfcp_sdev->status) &
202 ZFCP_STATUS_COMMON_RUNNING))
Christof Schmittfdbd1c52010-09-08 14:39:54 +0200203 act_status |= ZFCP_STATUS_ERP_CLOSE_ONLY;
Christof Schmitt287ac012008-07-02 10:56:40 +0200204 break;
205
206 case ZFCP_ERP_ACTION_REOPEN_PORT:
207 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
Christof Schmitt615f59e2010-02-17 11:18:56 +0100208 if (!get_device(&port->dev))
Swen Schillig6b1833342009-11-24 16:54:05 +0100209 return NULL;
Christof Schmitt287ac012008-07-02 10:56:40 +0200210 zfcp_erp_action_dismiss_port(port);
Peter Zijlstra805de8f42015-04-24 01:12:32 +0200211 atomic_or(ZFCP_STATUS_COMMON_ERP_INUSE, &port->status);
Christof Schmitt287ac012008-07-02 10:56:40 +0200212 erp_action = &port->erp_action;
Steffen Maierab31fd02017-10-13 15:40:07 +0200213 WARN_ON_ONCE(erp_action->port != port);
214 WARN_ON_ONCE(erp_action->sdev != NULL);
Christof Schmitt287ac012008-07-02 10:56:40 +0200215 if (!(atomic_read(&port->status) & ZFCP_STATUS_COMMON_RUNNING))
Christof Schmittfdbd1c52010-09-08 14:39:54 +0200216 act_status |= ZFCP_STATUS_ERP_CLOSE_ONLY;
Christof Schmitt287ac012008-07-02 10:56:40 +0200217 break;
218
219 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
Swen Schilligf3450c72009-11-24 16:53:59 +0100220 kref_get(&adapter->ref);
Christof Schmitt287ac012008-07-02 10:56:40 +0200221 zfcp_erp_action_dismiss_adapter(adapter);
Peter Zijlstra805de8f42015-04-24 01:12:32 +0200222 atomic_or(ZFCP_STATUS_COMMON_ERP_INUSE, &adapter->status);
Christof Schmitt287ac012008-07-02 10:56:40 +0200223 erp_action = &adapter->erp_action;
Steffen Maierab31fd02017-10-13 15:40:07 +0200224 WARN_ON_ONCE(erp_action->port != NULL);
225 WARN_ON_ONCE(erp_action->sdev != NULL);
Christof Schmitt287ac012008-07-02 10:56:40 +0200226 if (!(atomic_read(&adapter->status) &
227 ZFCP_STATUS_COMMON_RUNNING))
Christof Schmittfdbd1c52010-09-08 14:39:54 +0200228 act_status |= ZFCP_STATUS_ERP_CLOSE_ONLY;
Christof Schmitt287ac012008-07-02 10:56:40 +0200229 break;
230
231 default:
232 return NULL;
233 }
234
Steffen Maierab31fd02017-10-13 15:40:07 +0200235 WARN_ON_ONCE(erp_action->adapter != adapter);
236 memset(&erp_action->list, 0, sizeof(erp_action->list));
237 memset(&erp_action->timer, 0, sizeof(erp_action->timer));
238 erp_action->step = ZFCP_ERP_STEP_UNINITIALIZED;
239 erp_action->fsf_req_id = 0;
Christof Schmitt287ac012008-07-02 10:56:40 +0200240 erp_action->action = need;
Christof Schmittfdbd1c52010-09-08 14:39:54 +0200241 erp_action->status = act_status;
Kees Cook75492a52017-10-16 16:44:34 -0700242 timer_setup(&erp_action->timer, zfcp_erp_memwait_handler, 0);
Christof Schmitt287ac012008-07-02 10:56:40 +0200243
244 return erp_action;
245}
246
247static int zfcp_erp_action_enqueue(int want, struct zfcp_adapter *adapter,
248 struct zfcp_port *port,
Christof Schmittb62a8d92010-09-08 14:39:55 +0200249 struct scsi_device *sdev,
Swen Schilligea4a3a62010-12-02 15:16:16 +0100250 char *id, u32 act_status)
Christof Schmitt287ac012008-07-02 10:56:40 +0200251{
252 int retval = 1, need;
Swen Schilligae0904f2010-12-02 15:16:12 +0100253 struct zfcp_erp_action *act;
Christof Schmitt287ac012008-07-02 10:56:40 +0200254
Christof Schmitt347c6a92009-08-18 15:43:25 +0200255 if (!adapter->erp_thread)
Christof Schmitt287ac012008-07-02 10:56:40 +0200256 return -EIO;
257
Christof Schmittb62a8d92010-09-08 14:39:55 +0200258 need = zfcp_erp_required_act(want, adapter, port, sdev);
Christof Schmitt287ac012008-07-02 10:56:40 +0200259 if (!need)
260 goto out;
261
Christof Schmittb62a8d92010-09-08 14:39:55 +0200262 act = zfcp_erp_setup_act(need, act_status, adapter, port, sdev);
Christof Schmitt287ac012008-07-02 10:56:40 +0200263 if (!act)
264 goto out;
Peter Zijlstra805de8f42015-04-24 01:12:32 +0200265 atomic_or(ZFCP_STATUS_ADAPTER_ERP_PENDING, &adapter->status);
Christof Schmitt287ac012008-07-02 10:56:40 +0200266 ++adapter->erp_total_count;
267 list_add_tail(&act->list, &adapter->erp_ready_head);
Christof Schmitt347c6a92009-08-18 15:43:25 +0200268 wake_up(&adapter->erp_ready_wq);
Christof Schmitt287ac012008-07-02 10:56:40 +0200269 retval = 0;
270 out:
Swen Schilligae0904f2010-12-02 15:16:12 +0100271 zfcp_dbf_rec_trig(id, adapter, port, sdev, want, need);
Christof Schmitt287ac012008-07-02 10:56:40 +0200272 return retval;
273}
274
275static int _zfcp_erp_adapter_reopen(struct zfcp_adapter *adapter,
Swen Schilligea4a3a62010-12-02 15:16:16 +0100276 int clear_mask, char *id)
Christof Schmitt287ac012008-07-02 10:56:40 +0200277{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 zfcp_erp_adapter_block(adapter, clear_mask);
Christof Schmitta2fa0ae2009-03-02 13:09:08 +0100279 zfcp_scsi_schedule_rports_block(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
Christof Schmitt287ac012008-07-02 10:56:40 +0200281 /* ensure propagation of failed status to new devices */
282 if (atomic_read(&adapter->status) & ZFCP_STATUS_COMMON_ERP_FAILED) {
Swen Schilligedaed852010-09-08 14:40:01 +0200283 zfcp_erp_set_adapter_status(adapter,
284 ZFCP_STATUS_COMMON_ERP_FAILED);
Christof Schmitt287ac012008-07-02 10:56:40 +0200285 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 }
Christof Schmitt287ac012008-07-02 10:56:40 +0200287 return zfcp_erp_action_enqueue(ZFCP_ERP_ACTION_REOPEN_ADAPTER,
Swen Schilligea4a3a62010-12-02 15:16:16 +0100288 adapter, NULL, NULL, id, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289}
290
291/**
Christof Schmitt287ac012008-07-02 10:56:40 +0200292 * zfcp_erp_adapter_reopen - Reopen adapter.
293 * @adapter: Adapter to reopen.
294 * @clear: Status flags to clear.
295 * @id: Id for debug trace event.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 */
Swen Schilligea4a3a62010-12-02 15:16:16 +0100297void zfcp_erp_adapter_reopen(struct zfcp_adapter *adapter, int clear, char *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298{
Christof Schmitt287ac012008-07-02 10:56:40 +0200299 unsigned long flags;
300
Swen Schilligecf0c772009-11-24 16:53:58 +0100301 zfcp_erp_adapter_block(adapter, clear);
302 zfcp_scsi_schedule_rports_block(adapter);
303
304 write_lock_irqsave(&adapter->erp_lock, flags);
305 if (atomic_read(&adapter->status) & ZFCP_STATUS_COMMON_ERP_FAILED)
Swen Schilligedaed852010-09-08 14:40:01 +0200306 zfcp_erp_set_adapter_status(adapter,
307 ZFCP_STATUS_COMMON_ERP_FAILED);
Swen Schilligecf0c772009-11-24 16:53:58 +0100308 else
309 zfcp_erp_action_enqueue(ZFCP_ERP_ACTION_REOPEN_ADAPTER, adapter,
Swen Schilligea4a3a62010-12-02 15:16:16 +0100310 NULL, NULL, id, 0);
Swen Schilligecf0c772009-11-24 16:53:58 +0100311 write_unlock_irqrestore(&adapter->erp_lock, flags);
Christof Schmitt287ac012008-07-02 10:56:40 +0200312}
313
314/**
315 * zfcp_erp_adapter_shutdown - Shutdown adapter.
316 * @adapter: Adapter to shut down.
317 * @clear: Status flags to clear.
318 * @id: Id for debug trace event.
Christof Schmitt287ac012008-07-02 10:56:40 +0200319 */
320void zfcp_erp_adapter_shutdown(struct zfcp_adapter *adapter, int clear,
Swen Schilligea4a3a62010-12-02 15:16:16 +0100321 char *id)
Christof Schmitt287ac012008-07-02 10:56:40 +0200322{
323 int flags = ZFCP_STATUS_COMMON_RUNNING | ZFCP_STATUS_COMMON_ERP_FAILED;
Swen Schilligea4a3a62010-12-02 15:16:16 +0100324 zfcp_erp_adapter_reopen(adapter, clear | flags, id);
Christof Schmitt287ac012008-07-02 10:56:40 +0200325}
326
327/**
328 * zfcp_erp_port_shutdown - Shutdown port
329 * @port: Port to shut down.
330 * @clear: Status flags to clear.
331 * @id: Id for debug trace event.
Christof Schmitt287ac012008-07-02 10:56:40 +0200332 */
Swen Schilligea4a3a62010-12-02 15:16:16 +0100333void zfcp_erp_port_shutdown(struct zfcp_port *port, int clear, char *id)
Christof Schmitt287ac012008-07-02 10:56:40 +0200334{
335 int flags = ZFCP_STATUS_COMMON_RUNNING | ZFCP_STATUS_COMMON_ERP_FAILED;
Swen Schilligea4a3a62010-12-02 15:16:16 +0100336 zfcp_erp_port_reopen(port, clear | flags, id);
Christof Schmitt287ac012008-07-02 10:56:40 +0200337}
338
Christof Schmitt287ac012008-07-02 10:56:40 +0200339static void zfcp_erp_port_block(struct zfcp_port *port, int clear)
340{
Swen Schilligedaed852010-09-08 14:40:01 +0200341 zfcp_erp_clear_port_status(port,
342 ZFCP_STATUS_COMMON_UNBLOCKED | clear);
Christof Schmitt287ac012008-07-02 10:56:40 +0200343}
344
Swen Schilligea4a3a62010-12-02 15:16:16 +0100345static void _zfcp_erp_port_forced_reopen(struct zfcp_port *port, int clear,
346 char *id)
Christof Schmitt287ac012008-07-02 10:56:40 +0200347{
348 zfcp_erp_port_block(port, clear);
Christof Schmitta2fa0ae2009-03-02 13:09:08 +0100349 zfcp_scsi_schedule_rport_block(port);
Christof Schmitt287ac012008-07-02 10:56:40 +0200350
351 if (atomic_read(&port->status) & ZFCP_STATUS_COMMON_ERP_FAILED)
352 return;
353
354 zfcp_erp_action_enqueue(ZFCP_ERP_ACTION_REOPEN_PORT_FORCED,
Swen Schilligea4a3a62010-12-02 15:16:16 +0100355 port->adapter, port, NULL, id, 0);
Christof Schmitt287ac012008-07-02 10:56:40 +0200356}
357
358/**
359 * zfcp_erp_port_forced_reopen - Forced close of port and open again
360 * @port: Port to force close and to reopen.
Swen Schilligea4a3a62010-12-02 15:16:16 +0100361 * @clear: Status flags to clear.
Christof Schmitt287ac012008-07-02 10:56:40 +0200362 * @id: Id for debug trace event.
Christof Schmitt287ac012008-07-02 10:56:40 +0200363 */
Swen Schilligea4a3a62010-12-02 15:16:16 +0100364void zfcp_erp_port_forced_reopen(struct zfcp_port *port, int clear, char *id)
Christof Schmitt287ac012008-07-02 10:56:40 +0200365{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 unsigned long flags;
367 struct zfcp_adapter *adapter = port->adapter;
368
Swen Schilligecf0c772009-11-24 16:53:58 +0100369 write_lock_irqsave(&adapter->erp_lock, flags);
Swen Schilligea4a3a62010-12-02 15:16:16 +0100370 _zfcp_erp_port_forced_reopen(port, clear, id);
Swen Schilligecf0c772009-11-24 16:53:58 +0100371 write_unlock_irqrestore(&adapter->erp_lock, flags);
Christof Schmitt287ac012008-07-02 10:56:40 +0200372}
373
Swen Schilligea4a3a62010-12-02 15:16:16 +0100374static int _zfcp_erp_port_reopen(struct zfcp_port *port, int clear, char *id)
Christof Schmitt287ac012008-07-02 10:56:40 +0200375{
376 zfcp_erp_port_block(port, clear);
Christof Schmitta2fa0ae2009-03-02 13:09:08 +0100377 zfcp_scsi_schedule_rport_block(port);
Christof Schmitt287ac012008-07-02 10:56:40 +0200378
379 if (atomic_read(&port->status) & ZFCP_STATUS_COMMON_ERP_FAILED) {
380 /* ensure propagation of failed status to new devices */
Swen Schilligedaed852010-09-08 14:40:01 +0200381 zfcp_erp_set_port_status(port, ZFCP_STATUS_COMMON_ERP_FAILED);
Christof Schmitt287ac012008-07-02 10:56:40 +0200382 return -EIO;
383 }
384
385 return zfcp_erp_action_enqueue(ZFCP_ERP_ACTION_REOPEN_PORT,
Swen Schilligea4a3a62010-12-02 15:16:16 +0100386 port->adapter, port, NULL, id, 0);
Christof Schmitt287ac012008-07-02 10:56:40 +0200387}
388
389/**
390 * zfcp_erp_port_reopen - trigger remote port recovery
391 * @port: port to recover
392 * @clear_mask: flags in port status to be cleared
Swen Schilligea4a3a62010-12-02 15:16:16 +0100393 * @id: Id for debug trace event.
Christof Schmitt287ac012008-07-02 10:56:40 +0200394 *
395 * Returns 0 if recovery has been triggered, < 0 if not.
396 */
Swen Schilligea4a3a62010-12-02 15:16:16 +0100397int zfcp_erp_port_reopen(struct zfcp_port *port, int clear, char *id)
Christof Schmitt287ac012008-07-02 10:56:40 +0200398{
Christof Schmitt287ac012008-07-02 10:56:40 +0200399 int retval;
Swen Schilligecf0c772009-11-24 16:53:58 +0100400 unsigned long flags;
Christof Schmitt287ac012008-07-02 10:56:40 +0200401 struct zfcp_adapter *adapter = port->adapter;
402
Swen Schilligecf0c772009-11-24 16:53:58 +0100403 write_lock_irqsave(&adapter->erp_lock, flags);
Swen Schilligea4a3a62010-12-02 15:16:16 +0100404 retval = _zfcp_erp_port_reopen(port, clear, id);
Swen Schilligecf0c772009-11-24 16:53:58 +0100405 write_unlock_irqrestore(&adapter->erp_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
407 return retval;
408}
409
Christof Schmittb62a8d92010-09-08 14:39:55 +0200410static void zfcp_erp_lun_block(struct scsi_device *sdev, int clear_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411{
Swen Schilligedaed852010-09-08 14:40:01 +0200412 zfcp_erp_clear_lun_status(sdev,
413 ZFCP_STATUS_COMMON_UNBLOCKED | clear_mask);
Christof Schmitt287ac012008-07-02 10:56:40 +0200414}
415
Christof Schmittb62a8d92010-09-08 14:39:55 +0200416static void _zfcp_erp_lun_reopen(struct scsi_device *sdev, int clear, char *id,
Swen Schilligea4a3a62010-12-02 15:16:16 +0100417 u32 act_status)
Christof Schmitt287ac012008-07-02 10:56:40 +0200418{
Christof Schmittb62a8d92010-09-08 14:39:55 +0200419 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
420 struct zfcp_adapter *adapter = zfcp_sdev->port->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
Christof Schmittb62a8d92010-09-08 14:39:55 +0200422 zfcp_erp_lun_block(sdev, clear);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
Christof Schmittb62a8d92010-09-08 14:39:55 +0200424 if (atomic_read(&zfcp_sdev->status) & ZFCP_STATUS_COMMON_ERP_FAILED)
Christof Schmitt287ac012008-07-02 10:56:40 +0200425 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
Christof Schmittb62a8d92010-09-08 14:39:55 +0200427 zfcp_erp_action_enqueue(ZFCP_ERP_ACTION_REOPEN_LUN, adapter,
Swen Schilligea4a3a62010-12-02 15:16:16 +0100428 zfcp_sdev->port, sdev, id, act_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429}
430
431/**
Christof Schmittb62a8d92010-09-08 14:39:55 +0200432 * zfcp_erp_lun_reopen - initiate reopen of a LUN
433 * @sdev: SCSI device / LUN to be reopened
434 * @clear_mask: specifies flags in LUN status to be cleared
Swen Schilligea4a3a62010-12-02 15:16:16 +0100435 * @id: Id for debug trace event.
436 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 * Return: 0 on success, < 0 on error
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 */
Swen Schilligea4a3a62010-12-02 15:16:16 +0100439void zfcp_erp_lun_reopen(struct scsi_device *sdev, int clear, char *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 unsigned long flags;
Christof Schmittb62a8d92010-09-08 14:39:55 +0200442 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
443 struct zfcp_port *port = zfcp_sdev->port;
Christof Schmitt287ac012008-07-02 10:56:40 +0200444 struct zfcp_adapter *adapter = port->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
Swen Schilligecf0c772009-11-24 16:53:58 +0100446 write_lock_irqsave(&adapter->erp_lock, flags);
Swen Schilligea4a3a62010-12-02 15:16:16 +0100447 _zfcp_erp_lun_reopen(sdev, clear, id, 0);
Swen Schilligecf0c772009-11-24 16:53:58 +0100448 write_unlock_irqrestore(&adapter->erp_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449}
450
Christof Schmittfdbd1c52010-09-08 14:39:54 +0200451/**
Christof Schmittb62a8d92010-09-08 14:39:55 +0200452 * zfcp_erp_lun_shutdown - Shutdown LUN
453 * @sdev: SCSI device / LUN to shut down.
Christof Schmittfdbd1c52010-09-08 14:39:54 +0200454 * @clear: Status flags to clear.
455 * @id: Id for debug trace event.
Christof Schmittfdbd1c52010-09-08 14:39:54 +0200456 */
Swen Schilligea4a3a62010-12-02 15:16:16 +0100457void zfcp_erp_lun_shutdown(struct scsi_device *sdev, int clear, char *id)
Christof Schmittfdbd1c52010-09-08 14:39:54 +0200458{
459 int flags = ZFCP_STATUS_COMMON_RUNNING | ZFCP_STATUS_COMMON_ERP_FAILED;
Swen Schilligea4a3a62010-12-02 15:16:16 +0100460 zfcp_erp_lun_reopen(sdev, clear | flags, id);
Christof Schmittfdbd1c52010-09-08 14:39:54 +0200461}
462
463/**
Christof Schmittb62a8d92010-09-08 14:39:55 +0200464 * zfcp_erp_lun_shutdown_wait - Shutdown LUN and wait for erp completion
465 * @sdev: SCSI device / LUN to shut down.
Christof Schmittfdbd1c52010-09-08 14:39:54 +0200466 * @id: Id for debug trace event.
467 *
Christof Schmittb62a8d92010-09-08 14:39:55 +0200468 * Do not acquire a reference for the LUN when creating the ERP
Christof Schmittfdbd1c52010-09-08 14:39:54 +0200469 * action. It is safe, because this function waits for the ERP to
Christof Schmittb62a8d92010-09-08 14:39:55 +0200470 * complete first. This allows to shutdown the LUN, even when the SCSI
471 * device is in the state SDEV_DEL when scsi_device_get will fail.
Christof Schmittfdbd1c52010-09-08 14:39:54 +0200472 */
Christof Schmittb62a8d92010-09-08 14:39:55 +0200473void zfcp_erp_lun_shutdown_wait(struct scsi_device *sdev, char *id)
Christof Schmittfdbd1c52010-09-08 14:39:54 +0200474{
475 unsigned long flags;
Christof Schmittb62a8d92010-09-08 14:39:55 +0200476 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
477 struct zfcp_port *port = zfcp_sdev->port;
Christof Schmittfdbd1c52010-09-08 14:39:54 +0200478 struct zfcp_adapter *adapter = port->adapter;
479 int clear = ZFCP_STATUS_COMMON_RUNNING | ZFCP_STATUS_COMMON_ERP_FAILED;
480
481 write_lock_irqsave(&adapter->erp_lock, flags);
Swen Schilligea4a3a62010-12-02 15:16:16 +0100482 _zfcp_erp_lun_reopen(sdev, clear, id, ZFCP_STATUS_ERP_NO_REF);
Christof Schmittfdbd1c52010-09-08 14:39:54 +0200483 write_unlock_irqrestore(&adapter->erp_lock, flags);
484
485 zfcp_erp_wait(adapter);
486}
487
Christof Schmitt287ac012008-07-02 10:56:40 +0200488static int status_change_set(unsigned long mask, atomic_t *status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489{
Christof Schmitt287ac012008-07-02 10:56:40 +0200490 return (atomic_read(status) ^ mask) & mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491}
492
Andreas Herrmannf6c0e7a2006-08-02 11:05:52 +0200493static void zfcp_erp_adapter_unblock(struct zfcp_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494{
Christof Schmitt287ac012008-07-02 10:56:40 +0200495 if (status_change_set(ZFCP_STATUS_COMMON_UNBLOCKED, &adapter->status))
Swen Schilligae0904f2010-12-02 15:16:12 +0100496 zfcp_dbf_rec_run("eraubl1", &adapter->erp_action);
Peter Zijlstra805de8f42015-04-24 01:12:32 +0200497 atomic_or(ZFCP_STATUS_COMMON_UNBLOCKED, &adapter->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498}
499
Christof Schmitt287ac012008-07-02 10:56:40 +0200500static void zfcp_erp_port_unblock(struct zfcp_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501{
Christof Schmitt287ac012008-07-02 10:56:40 +0200502 if (status_change_set(ZFCP_STATUS_COMMON_UNBLOCKED, &port->status))
Swen Schilligae0904f2010-12-02 15:16:12 +0100503 zfcp_dbf_rec_run("erpubl1", &port->erp_action);
Peter Zijlstra805de8f42015-04-24 01:12:32 +0200504 atomic_or(ZFCP_STATUS_COMMON_UNBLOCKED, &port->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505}
506
Christof Schmittb62a8d92010-09-08 14:39:55 +0200507static void zfcp_erp_lun_unblock(struct scsi_device *sdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508{
Christof Schmittb62a8d92010-09-08 14:39:55 +0200509 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
510
511 if (status_change_set(ZFCP_STATUS_COMMON_UNBLOCKED, &zfcp_sdev->status))
Swen Schilligae0904f2010-12-02 15:16:12 +0100512 zfcp_dbf_rec_run("erlubl1", &sdev_to_zfcp(sdev)->erp_action);
Peter Zijlstra805de8f42015-04-24 01:12:32 +0200513 atomic_or(ZFCP_STATUS_COMMON_UNBLOCKED, &zfcp_sdev->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514}
515
Christof Schmitt287ac012008-07-02 10:56:40 +0200516static void zfcp_erp_action_to_running(struct zfcp_erp_action *erp_action)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517{
Christof Schmitt287ac012008-07-02 10:56:40 +0200518 list_move(&erp_action->list, &erp_action->adapter->erp_running_head);
Swen Schilligae0904f2010-12-02 15:16:12 +0100519 zfcp_dbf_rec_run("erator1", erp_action);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520}
521
Christof Schmitt287ac012008-07-02 10:56:40 +0200522static void zfcp_erp_strategy_check_fsfreq(struct zfcp_erp_action *act)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523{
Christof Schmitt287ac012008-07-02 10:56:40 +0200524 struct zfcp_adapter *adapter = act->adapter;
Christof Schmitte60a6d62010-02-17 11:18:49 +0100525 struct zfcp_fsf_req *req;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
Christof Schmitte60a6d62010-02-17 11:18:49 +0100527 if (!act->fsf_req_id)
Christof Schmitt287ac012008-07-02 10:56:40 +0200528 return;
529
Christof Schmittb6bd2fb2010-02-17 11:18:50 +0100530 spin_lock(&adapter->req_list->lock);
531 req = _zfcp_reqlist_find(adapter->req_list, act->fsf_req_id);
Christof Schmitte60a6d62010-02-17 11:18:49 +0100532 if (req && req->erp_action == act) {
Christof Schmitt287ac012008-07-02 10:56:40 +0200533 if (act->status & (ZFCP_STATUS_ERP_DISMISSED |
534 ZFCP_STATUS_ERP_TIMEDOUT)) {
Christof Schmitte60a6d62010-02-17 11:18:49 +0100535 req->status |= ZFCP_STATUS_FSFREQ_DISMISSED;
Swen Schilligae0904f2010-12-02 15:16:12 +0100536 zfcp_dbf_rec_run("erscf_1", act);
Christof Schmitte60a6d62010-02-17 11:18:49 +0100537 req->erp_action = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 }
Christof Schmitt287ac012008-07-02 10:56:40 +0200539 if (act->status & ZFCP_STATUS_ERP_TIMEDOUT)
Swen Schilligae0904f2010-12-02 15:16:12 +0100540 zfcp_dbf_rec_run("erscf_2", act);
Christof Schmitte60a6d62010-02-17 11:18:49 +0100541 if (req->status & ZFCP_STATUS_FSFREQ_DISMISSED)
542 act->fsf_req_id = 0;
Christof Schmitt287ac012008-07-02 10:56:40 +0200543 } else
Christof Schmitte60a6d62010-02-17 11:18:49 +0100544 act->fsf_req_id = 0;
Christof Schmittb6bd2fb2010-02-17 11:18:50 +0100545 spin_unlock(&adapter->req_list->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546}
547
Andreas Herrmannf6c0e7a2006-08-02 11:05:52 +0200548/**
Christof Schmitt287ac012008-07-02 10:56:40 +0200549 * zfcp_erp_notify - Trigger ERP action.
550 * @erp_action: ERP action to continue.
551 * @set_mask: ERP action status flags to set.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 */
Christof Schmitt287ac012008-07-02 10:56:40 +0200553void zfcp_erp_notify(struct zfcp_erp_action *erp_action, unsigned long set_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554{
555 struct zfcp_adapter *adapter = erp_action->adapter;
556 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
558 write_lock_irqsave(&adapter->erp_lock, flags);
Christof Schmitt287ac012008-07-02 10:56:40 +0200559 if (zfcp_erp_action_exists(erp_action) == ZFCP_ERP_ACTION_RUNNING) {
560 erp_action->status |= set_mask;
561 zfcp_erp_action_ready(erp_action);
562 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 write_unlock_irqrestore(&adapter->erp_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564}
565
Andreas Herrmannf6c0e7a2006-08-02 11:05:52 +0200566/**
Christof Schmitt287ac012008-07-02 10:56:40 +0200567 * zfcp_erp_timeout_handler - Trigger ERP action from timed out ERP request
568 * @data: ERP action (from timer data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 */
Kees Cook75492a52017-10-16 16:44:34 -0700570void zfcp_erp_timeout_handler(struct timer_list *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571{
Kees Cook75492a52017-10-16 16:44:34 -0700572 struct zfcp_fsf_req *fsf_req = from_timer(fsf_req, t, timer);
573 struct zfcp_erp_action *act = fsf_req->erp_action;
Christof Schmitt287ac012008-07-02 10:56:40 +0200574 zfcp_erp_notify(act, ZFCP_STATUS_ERP_TIMEDOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575}
576
Kees Cook75492a52017-10-16 16:44:34 -0700577static void zfcp_erp_memwait_handler(struct timer_list *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578{
Kees Cook75492a52017-10-16 16:44:34 -0700579 struct zfcp_erp_action *act = from_timer(act, t, timer);
580
581 zfcp_erp_notify(act, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582}
583
Christof Schmitt287ac012008-07-02 10:56:40 +0200584static void zfcp_erp_strategy_memwait(struct zfcp_erp_action *erp_action)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585{
Christof Schmitt287ac012008-07-02 10:56:40 +0200586 erp_action->timer.expires = jiffies + HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 add_timer(&erp_action->timer);
Christof Schmitt287ac012008-07-02 10:56:40 +0200588}
589
590static void _zfcp_erp_port_reopen_all(struct zfcp_adapter *adapter,
Swen Schilligea4a3a62010-12-02 15:16:16 +0100591 int clear, char *id)
Christof Schmitt287ac012008-07-02 10:56:40 +0200592{
593 struct zfcp_port *port;
594
Swen Schilligecf0c772009-11-24 16:53:58 +0100595 read_lock(&adapter->port_list_lock);
596 list_for_each_entry(port, &adapter->port_list, list)
Swen Schilligea4a3a62010-12-02 15:16:16 +0100597 _zfcp_erp_port_reopen(port, clear, id);
Swen Schilligecf0c772009-11-24 16:53:58 +0100598 read_unlock(&adapter->port_list_lock);
Christof Schmitt287ac012008-07-02 10:56:40 +0200599}
600
Christof Schmittb62a8d92010-09-08 14:39:55 +0200601static void _zfcp_erp_lun_reopen_all(struct zfcp_port *port, int clear,
Swen Schilligea4a3a62010-12-02 15:16:16 +0100602 char *id)
Christof Schmitt287ac012008-07-02 10:56:40 +0200603{
Christof Schmittb62a8d92010-09-08 14:39:55 +0200604 struct scsi_device *sdev;
Christof Schmitt287ac012008-07-02 10:56:40 +0200605
Martin Peschke924dd582013-08-22 17:45:37 +0200606 spin_lock(port->adapter->scsi_host->host_lock);
607 __shost_for_each_device(sdev, port->adapter->scsi_host)
Christof Schmittb62a8d92010-09-08 14:39:55 +0200608 if (sdev_to_zfcp(sdev)->port == port)
Swen Schilligea4a3a62010-12-02 15:16:16 +0100609 _zfcp_erp_lun_reopen(sdev, clear, id, 0);
Martin Peschke924dd582013-08-22 17:45:37 +0200610 spin_unlock(port->adapter->scsi_host->host_lock);
Christof Schmitt287ac012008-07-02 10:56:40 +0200611}
612
Christof Schmitt85600f72009-07-13 15:06:09 +0200613static void zfcp_erp_strategy_followup_failed(struct zfcp_erp_action *act)
Christof Schmitt287ac012008-07-02 10:56:40 +0200614{
Christof Schmitt287ac012008-07-02 10:56:40 +0200615 switch (act->action) {
Christof Schmitt287ac012008-07-02 10:56:40 +0200616 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
Swen Schilligea4a3a62010-12-02 15:16:16 +0100617 _zfcp_erp_adapter_reopen(act->adapter, 0, "ersff_1");
Christof Schmitt287ac012008-07-02 10:56:40 +0200618 break;
Christof Schmitt287ac012008-07-02 10:56:40 +0200619 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
Swen Schilligea4a3a62010-12-02 15:16:16 +0100620 _zfcp_erp_port_forced_reopen(act->port, 0, "ersff_2");
Christof Schmitt287ac012008-07-02 10:56:40 +0200621 break;
Christof Schmitt287ac012008-07-02 10:56:40 +0200622 case ZFCP_ERP_ACTION_REOPEN_PORT:
Swen Schilligea4a3a62010-12-02 15:16:16 +0100623 _zfcp_erp_port_reopen(act->port, 0, "ersff_3");
Christof Schmitt287ac012008-07-02 10:56:40 +0200624 break;
Christof Schmittb62a8d92010-09-08 14:39:55 +0200625 case ZFCP_ERP_ACTION_REOPEN_LUN:
Swen Schilligea4a3a62010-12-02 15:16:16 +0100626 _zfcp_erp_lun_reopen(act->sdev, 0, "ersff_4", 0);
Christof Schmitt85600f72009-07-13 15:06:09 +0200627 break;
628 }
629}
630
631static void zfcp_erp_strategy_followup_success(struct zfcp_erp_action *act)
632{
633 switch (act->action) {
634 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
Swen Schilligea4a3a62010-12-02 15:16:16 +0100635 _zfcp_erp_port_reopen_all(act->adapter, 0, "ersfs_1");
Christof Schmitt85600f72009-07-13 15:06:09 +0200636 break;
637 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
Swen Schilligea4a3a62010-12-02 15:16:16 +0100638 _zfcp_erp_port_reopen(act->port, 0, "ersfs_2");
Christof Schmitt85600f72009-07-13 15:06:09 +0200639 break;
640 case ZFCP_ERP_ACTION_REOPEN_PORT:
Swen Schilligea4a3a62010-12-02 15:16:16 +0100641 _zfcp_erp_lun_reopen_all(act->port, 0, "ersfs_3");
Christof Schmitt287ac012008-07-02 10:56:40 +0200642 break;
643 }
644}
645
646static void zfcp_erp_wakeup(struct zfcp_adapter *adapter)
647{
648 unsigned long flags;
649
Swen Schilligecf0c772009-11-24 16:53:58 +0100650 read_lock_irqsave(&adapter->erp_lock, flags);
Christof Schmitt287ac012008-07-02 10:56:40 +0200651 if (list_empty(&adapter->erp_ready_head) &&
652 list_empty(&adapter->erp_running_head)) {
Peter Zijlstra805de8f42015-04-24 01:12:32 +0200653 atomic_andnot(ZFCP_STATUS_ADAPTER_ERP_PENDING,
Christof Schmitt287ac012008-07-02 10:56:40 +0200654 &adapter->status);
655 wake_up(&adapter->erp_done_wqh);
656 }
Swen Schilligecf0c772009-11-24 16:53:58 +0100657 read_unlock_irqrestore(&adapter->erp_lock, flags);
Christof Schmitt287ac012008-07-02 10:56:40 +0200658}
659
Christof Schmitt287ac012008-07-02 10:56:40 +0200660static void zfcp_erp_enqueue_ptp_port(struct zfcp_adapter *adapter)
661{
662 struct zfcp_port *port;
663 port = zfcp_port_enqueue(adapter, adapter->peer_wwpn, 0,
664 adapter->peer_d_id);
665 if (IS_ERR(port)) /* error or port already attached */
666 return;
Swen Schilligea4a3a62010-12-02 15:16:16 +0100667 _zfcp_erp_port_reopen(port, 0, "ereptp1");
Christof Schmitt287ac012008-07-02 10:56:40 +0200668}
669
670static int zfcp_erp_adapter_strat_fsf_xconf(struct zfcp_erp_action *erp_action)
671{
672 int retries;
673 int sleep = 1;
674 struct zfcp_adapter *adapter = erp_action->adapter;
675
Peter Zijlstra805de8f42015-04-24 01:12:32 +0200676 atomic_andnot(ZFCP_STATUS_ADAPTER_XCONFIG_OK, &adapter->status);
Christof Schmitt287ac012008-07-02 10:56:40 +0200677
678 for (retries = 7; retries; retries--) {
Peter Zijlstra805de8f42015-04-24 01:12:32 +0200679 atomic_andnot(ZFCP_STATUS_ADAPTER_HOST_CON_INIT,
Christof Schmitt287ac012008-07-02 10:56:40 +0200680 &adapter->status);
681 write_lock_irq(&adapter->erp_lock);
682 zfcp_erp_action_to_running(erp_action);
683 write_unlock_irq(&adapter->erp_lock);
684 if (zfcp_fsf_exchange_config_data(erp_action)) {
Peter Zijlstra805de8f42015-04-24 01:12:32 +0200685 atomic_andnot(ZFCP_STATUS_ADAPTER_HOST_CON_INIT,
Christof Schmitt287ac012008-07-02 10:56:40 +0200686 &adapter->status);
687 return ZFCP_ERP_FAILED;
688 }
689
Christof Schmitt347c6a92009-08-18 15:43:25 +0200690 wait_event(adapter->erp_ready_wq,
691 !list_empty(&adapter->erp_ready_head));
Christof Schmitt287ac012008-07-02 10:56:40 +0200692 if (erp_action->status & ZFCP_STATUS_ERP_TIMEDOUT)
693 break;
694
695 if (!(atomic_read(&adapter->status) &
696 ZFCP_STATUS_ADAPTER_HOST_CON_INIT))
697 break;
698
699 ssleep(sleep);
700 sleep *= 2;
701 }
702
Peter Zijlstra805de8f42015-04-24 01:12:32 +0200703 atomic_andnot(ZFCP_STATUS_ADAPTER_HOST_CON_INIT,
Christof Schmitt287ac012008-07-02 10:56:40 +0200704 &adapter->status);
705
706 if (!(atomic_read(&adapter->status) & ZFCP_STATUS_ADAPTER_XCONFIG_OK))
707 return ZFCP_ERP_FAILED;
708
709 if (fc_host_port_type(adapter->scsi_host) == FC_PORTTYPE_PTP)
710 zfcp_erp_enqueue_ptp_port(adapter);
711
712 return ZFCP_ERP_SUCCEEDED;
713}
714
715static int zfcp_erp_adapter_strategy_open_fsf_xport(struct zfcp_erp_action *act)
716{
717 int ret;
718 struct zfcp_adapter *adapter = act->adapter;
719
Christof Schmitt287ac012008-07-02 10:56:40 +0200720 write_lock_irq(&adapter->erp_lock);
721 zfcp_erp_action_to_running(act);
722 write_unlock_irq(&adapter->erp_lock);
723
724 ret = zfcp_fsf_exchange_port_data(act);
725 if (ret == -EOPNOTSUPP)
726 return ZFCP_ERP_SUCCEEDED;
727 if (ret)
728 return ZFCP_ERP_FAILED;
729
Swen Schilligae0904f2010-12-02 15:16:12 +0100730 zfcp_dbf_rec_run("erasox1", act);
Christof Schmitt347c6a92009-08-18 15:43:25 +0200731 wait_event(adapter->erp_ready_wq,
732 !list_empty(&adapter->erp_ready_head));
Swen Schilligae0904f2010-12-02 15:16:12 +0100733 zfcp_dbf_rec_run("erasox2", act);
Christof Schmitt287ac012008-07-02 10:56:40 +0200734 if (act->status & ZFCP_STATUS_ERP_TIMEDOUT)
735 return ZFCP_ERP_FAILED;
736
737 return ZFCP_ERP_SUCCEEDED;
738}
739
740static int zfcp_erp_adapter_strategy_open_fsf(struct zfcp_erp_action *act)
741{
742 if (zfcp_erp_adapter_strat_fsf_xconf(act) == ZFCP_ERP_FAILED)
743 return ZFCP_ERP_FAILED;
744
745 if (zfcp_erp_adapter_strategy_open_fsf_xport(act) == ZFCP_ERP_FAILED)
746 return ZFCP_ERP_FAILED;
747
Christof Schmittc7b279a2011-02-22 19:54:40 +0100748 if (mempool_resize(act->adapter->pool.sr_data,
David Rientjes11d83362015-04-14 15:48:21 -0700749 act->adapter->stat_read_buf_num))
Christof Schmitt8d88cf32010-06-21 10:11:33 +0200750 return ZFCP_ERP_FAILED;
751
752 if (mempool_resize(act->adapter->pool.status_read_req,
David Rientjes11d83362015-04-14 15:48:21 -0700753 act->adapter->stat_read_buf_num))
Christof Schmitt8d88cf32010-06-21 10:11:33 +0200754 return ZFCP_ERP_FAILED;
755
Christof Schmitt64deb6e2010-04-30 18:09:36 +0200756 atomic_set(&act->adapter->stat_miss, act->adapter->stat_read_buf_num);
Christof Schmitt287ac012008-07-02 10:56:40 +0200757 if (zfcp_status_read_refill(act->adapter))
758 return ZFCP_ERP_FAILED;
759
760 return ZFCP_ERP_SUCCEEDED;
761}
762
Swen Schilligcf13c082009-03-02 13:09:03 +0100763static void zfcp_erp_adapter_strategy_close(struct zfcp_erp_action *act)
Christof Schmitt287ac012008-07-02 10:56:40 +0200764{
Christof Schmitt287ac012008-07-02 10:56:40 +0200765 struct zfcp_adapter *adapter = act->adapter;
766
Christof Schmitt287ac012008-07-02 10:56:40 +0200767 /* close queues to ensure that buffers are not accessed by adapter */
Swen Schillig564e1c82009-08-18 15:43:19 +0200768 zfcp_qdio_close(adapter->qdio);
Christof Schmitt287ac012008-07-02 10:56:40 +0200769 zfcp_fsf_req_dismiss_all(adapter);
770 adapter->fsf_req_seq_no = 0;
Christof Schmitt55c770f2009-08-18 15:43:12 +0200771 zfcp_fc_wka_ports_force_offline(adapter->gs);
Christof Schmittb62a8d92010-09-08 14:39:55 +0200772 /* all ports and LUNs are closed */
Swen Schilligedaed852010-09-08 14:40:01 +0200773 zfcp_erp_clear_adapter_status(adapter, ZFCP_STATUS_COMMON_OPEN);
Swen Schilligcf13c082009-03-02 13:09:03 +0100774
Peter Zijlstra805de8f42015-04-24 01:12:32 +0200775 atomic_andnot(ZFCP_STATUS_ADAPTER_XCONFIG_OK |
Swen Schilligcf13c082009-03-02 13:09:03 +0100776 ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED, &adapter->status);
777}
778
779static int zfcp_erp_adapter_strategy_open(struct zfcp_erp_action *act)
780{
781 struct zfcp_adapter *adapter = act->adapter;
782
Christof Schmitt3d63d3b2010-12-02 15:16:17 +0100783 if (zfcp_qdio_open(adapter->qdio)) {
Peter Zijlstra805de8f42015-04-24 01:12:32 +0200784 atomic_andnot(ZFCP_STATUS_ADAPTER_XCONFIG_OK |
Swen Schilligcf13c082009-03-02 13:09:03 +0100785 ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED,
786 &adapter->status);
787 return ZFCP_ERP_FAILED;
788 }
789
790 if (zfcp_erp_adapter_strategy_open_fsf(act)) {
791 zfcp_erp_adapter_strategy_close(act);
792 return ZFCP_ERP_FAILED;
793 }
794
Peter Zijlstra805de8f42015-04-24 01:12:32 +0200795 atomic_or(ZFCP_STATUS_COMMON_OPEN, &adapter->status);
Swen Schilligcf13c082009-03-02 13:09:03 +0100796
797 return ZFCP_ERP_SUCCEEDED;
Christof Schmitt287ac012008-07-02 10:56:40 +0200798}
799
800static int zfcp_erp_adapter_strategy(struct zfcp_erp_action *act)
801{
Swen Schilligcf13c082009-03-02 13:09:03 +0100802 struct zfcp_adapter *adapter = act->adapter;
Christof Schmitt287ac012008-07-02 10:56:40 +0200803
Swen Schilligcf13c082009-03-02 13:09:03 +0100804 if (atomic_read(&adapter->status) & ZFCP_STATUS_COMMON_OPEN) {
805 zfcp_erp_adapter_strategy_close(act);
806 if (act->status & ZFCP_STATUS_ERP_CLOSE_ONLY)
807 return ZFCP_ERP_EXIT;
808 }
Christof Schmitt287ac012008-07-02 10:56:40 +0200809
Swen Schilligcf13c082009-03-02 13:09:03 +0100810 if (zfcp_erp_adapter_strategy_open(act)) {
Christof Schmitt287ac012008-07-02 10:56:40 +0200811 ssleep(8);
Swen Schilligcf13c082009-03-02 13:09:03 +0100812 return ZFCP_ERP_FAILED;
813 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814
Swen Schilligcf13c082009-03-02 13:09:03 +0100815 return ZFCP_ERP_SUCCEEDED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816}
817
Christof Schmitt287ac012008-07-02 10:56:40 +0200818static int zfcp_erp_port_forced_strategy_close(struct zfcp_erp_action *act)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819{
Christof Schmitt287ac012008-07-02 10:56:40 +0200820 int retval;
821
822 retval = zfcp_fsf_close_physical_port(act);
823 if (retval == -ENOMEM)
824 return ZFCP_ERP_NOMEM;
825 act->step = ZFCP_ERP_STEP_PHYS_PORT_CLOSING;
826 if (retval)
827 return ZFCP_ERP_FAILED;
828
829 return ZFCP_ERP_CONTINUES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830}
831
Christof Schmitt287ac012008-07-02 10:56:40 +0200832static int zfcp_erp_port_forced_strategy(struct zfcp_erp_action *erp_action)
833{
834 struct zfcp_port *port = erp_action->port;
835 int status = atomic_read(&port->status);
836
837 switch (erp_action->step) {
838 case ZFCP_ERP_STEP_UNINITIALIZED:
Christof Schmitt287ac012008-07-02 10:56:40 +0200839 if ((status & ZFCP_STATUS_PORT_PHYS_OPEN) &&
840 (status & ZFCP_STATUS_COMMON_OPEN))
841 return zfcp_erp_port_forced_strategy_close(erp_action);
842 else
843 return ZFCP_ERP_FAILED;
844
845 case ZFCP_ERP_STEP_PHYS_PORT_CLOSING:
Christof Schmittddb3e0c2009-07-13 15:06:08 +0200846 if (!(status & ZFCP_STATUS_PORT_PHYS_OPEN))
Christof Schmitt287ac012008-07-02 10:56:40 +0200847 return ZFCP_ERP_SUCCEEDED;
848 }
849 return ZFCP_ERP_FAILED;
850}
851
852static int zfcp_erp_port_strategy_close(struct zfcp_erp_action *erp_action)
853{
854 int retval;
855
856 retval = zfcp_fsf_close_port(erp_action);
857 if (retval == -ENOMEM)
858 return ZFCP_ERP_NOMEM;
859 erp_action->step = ZFCP_ERP_STEP_PORT_CLOSING;
860 if (retval)
861 return ZFCP_ERP_FAILED;
862 return ZFCP_ERP_CONTINUES;
863}
864
865static int zfcp_erp_port_strategy_open_port(struct zfcp_erp_action *erp_action)
866{
867 int retval;
868
869 retval = zfcp_fsf_open_port(erp_action);
870 if (retval == -ENOMEM)
871 return ZFCP_ERP_NOMEM;
872 erp_action->step = ZFCP_ERP_STEP_PORT_OPENING;
873 if (retval)
874 return ZFCP_ERP_FAILED;
875 return ZFCP_ERP_CONTINUES;
876}
877
Christof Schmitt287ac012008-07-02 10:56:40 +0200878static int zfcp_erp_open_ptp_port(struct zfcp_erp_action *act)
879{
880 struct zfcp_adapter *adapter = act->adapter;
881 struct zfcp_port *port = act->port;
882
883 if (port->wwpn != adapter->peer_wwpn) {
Swen Schilligedaed852010-09-08 14:40:01 +0200884 zfcp_erp_set_port_status(port, ZFCP_STATUS_COMMON_ERP_FAILED);
Christof Schmitt287ac012008-07-02 10:56:40 +0200885 return ZFCP_ERP_FAILED;
886 }
887 port->d_id = adapter->peer_d_id;
Christof Schmitt287ac012008-07-02 10:56:40 +0200888 return zfcp_erp_port_strategy_open_port(act);
889}
890
891static int zfcp_erp_port_strategy_open_common(struct zfcp_erp_action *act)
892{
893 struct zfcp_adapter *adapter = act->adapter;
894 struct zfcp_port *port = act->port;
Christof Schmitt287ac012008-07-02 10:56:40 +0200895 int p_status = atomic_read(&port->status);
896
897 switch (act->step) {
898 case ZFCP_ERP_STEP_UNINITIALIZED:
899 case ZFCP_ERP_STEP_PHYS_PORT_CLOSING:
900 case ZFCP_ERP_STEP_PORT_CLOSING:
901 if (fc_host_port_type(adapter->scsi_host) == FC_PORTTYPE_PTP)
902 return zfcp_erp_open_ptp_port(act);
Christof Schmittb98478d2008-12-19 16:56:59 +0100903 if (!port->d_id) {
Christof Schmitt934aeb5872009-10-14 11:00:43 +0200904 zfcp_fc_trigger_did_lookup(port);
Christof Schmitt799b76d2009-08-18 15:43:20 +0200905 return ZFCP_ERP_EXIT;
Christof Schmitt287ac012008-07-02 10:56:40 +0200906 }
Christof Schmitt287ac012008-07-02 10:56:40 +0200907 return zfcp_erp_port_strategy_open_port(act);
908
909 case ZFCP_ERP_STEP_PORT_OPENING:
910 /* D_ID might have changed during open */
Swen Schillig5ab944f2008-10-01 12:42:17 +0200911 if (p_status & ZFCP_STATUS_COMMON_OPEN) {
Christof Schmitt934aeb5872009-10-14 11:00:43 +0200912 if (!port->d_id) {
913 zfcp_fc_trigger_did_lookup(port);
914 return ZFCP_ERP_EXIT;
Swen Schillig5ab944f2008-10-01 12:42:17 +0200915 }
Christof Schmitt934aeb5872009-10-14 11:00:43 +0200916 return ZFCP_ERP_SUCCEEDED;
Swen Schillig5ab944f2008-10-01 12:42:17 +0200917 }
Swen Schilligea460a82009-05-15 13:18:20 +0200918 if (port->d_id && !(p_status & ZFCP_STATUS_COMMON_NOESC)) {
919 port->d_id = 0;
Christof Schmittf7bd7c32010-07-08 09:53:10 +0200920 return ZFCP_ERP_FAILED;
Swen Schilligea460a82009-05-15 13:18:20 +0200921 }
922 /* fall through otherwise */
Christof Schmitt287ac012008-07-02 10:56:40 +0200923 }
924 return ZFCP_ERP_FAILED;
925}
926
Christof Schmitt287ac012008-07-02 10:56:40 +0200927static int zfcp_erp_port_strategy(struct zfcp_erp_action *erp_action)
928{
929 struct zfcp_port *port = erp_action->port;
Christof Schmitt934aeb5872009-10-14 11:00:43 +0200930 int p_status = atomic_read(&port->status);
Christof Schmitt287ac012008-07-02 10:56:40 +0200931
Christof Schmitt934aeb5872009-10-14 11:00:43 +0200932 if ((p_status & ZFCP_STATUS_COMMON_NOESC) &&
933 !(p_status & ZFCP_STATUS_COMMON_OPEN))
Swen Schillig5ab944f2008-10-01 12:42:17 +0200934 goto close_init_done;
935
Christof Schmitt287ac012008-07-02 10:56:40 +0200936 switch (erp_action->step) {
937 case ZFCP_ERP_STEP_UNINITIALIZED:
Christof Schmitt934aeb5872009-10-14 11:00:43 +0200938 if (p_status & ZFCP_STATUS_COMMON_OPEN)
Christof Schmitt287ac012008-07-02 10:56:40 +0200939 return zfcp_erp_port_strategy_close(erp_action);
940 break;
941
942 case ZFCP_ERP_STEP_PORT_CLOSING:
Christof Schmitt934aeb5872009-10-14 11:00:43 +0200943 if (p_status & ZFCP_STATUS_COMMON_OPEN)
Christof Schmitt287ac012008-07-02 10:56:40 +0200944 return ZFCP_ERP_FAILED;
945 break;
946 }
Swen Schillig5ab944f2008-10-01 12:42:17 +0200947
948close_init_done:
Christof Schmitt287ac012008-07-02 10:56:40 +0200949 if (erp_action->status & ZFCP_STATUS_ERP_CLOSE_ONLY)
950 return ZFCP_ERP_EXIT;
Christof Schmitt287ac012008-07-02 10:56:40 +0200951
Swen Schillig5ab944f2008-10-01 12:42:17 +0200952 return zfcp_erp_port_strategy_open_common(erp_action);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953}
954
Christof Schmittb62a8d92010-09-08 14:39:55 +0200955static void zfcp_erp_lun_strategy_clearstati(struct scsi_device *sdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956{
Christof Schmittb62a8d92010-09-08 14:39:55 +0200957 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
958
Peter Zijlstra805de8f42015-04-24 01:12:32 +0200959 atomic_andnot(ZFCP_STATUS_COMMON_ACCESS_DENIED,
Christof Schmittb62a8d92010-09-08 14:39:55 +0200960 &zfcp_sdev->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961}
962
Christof Schmittb62a8d92010-09-08 14:39:55 +0200963static int zfcp_erp_lun_strategy_close(struct zfcp_erp_action *erp_action)
Christof Schmitt287ac012008-07-02 10:56:40 +0200964{
Christof Schmittb62a8d92010-09-08 14:39:55 +0200965 int retval = zfcp_fsf_close_lun(erp_action);
Christof Schmitt287ac012008-07-02 10:56:40 +0200966 if (retval == -ENOMEM)
967 return ZFCP_ERP_NOMEM;
Christof Schmittb62a8d92010-09-08 14:39:55 +0200968 erp_action->step = ZFCP_ERP_STEP_LUN_CLOSING;
Christof Schmitt287ac012008-07-02 10:56:40 +0200969 if (retval)
970 return ZFCP_ERP_FAILED;
971 return ZFCP_ERP_CONTINUES;
972}
973
Christof Schmittb62a8d92010-09-08 14:39:55 +0200974static int zfcp_erp_lun_strategy_open(struct zfcp_erp_action *erp_action)
Christof Schmitt287ac012008-07-02 10:56:40 +0200975{
Christof Schmittb62a8d92010-09-08 14:39:55 +0200976 int retval = zfcp_fsf_open_lun(erp_action);
Christof Schmitt287ac012008-07-02 10:56:40 +0200977 if (retval == -ENOMEM)
978 return ZFCP_ERP_NOMEM;
Christof Schmittb62a8d92010-09-08 14:39:55 +0200979 erp_action->step = ZFCP_ERP_STEP_LUN_OPENING;
Christof Schmitt287ac012008-07-02 10:56:40 +0200980 if (retval)
981 return ZFCP_ERP_FAILED;
982 return ZFCP_ERP_CONTINUES;
983}
984
Christof Schmittb62a8d92010-09-08 14:39:55 +0200985static int zfcp_erp_lun_strategy(struct zfcp_erp_action *erp_action)
Christof Schmitt287ac012008-07-02 10:56:40 +0200986{
Christof Schmittb62a8d92010-09-08 14:39:55 +0200987 struct scsi_device *sdev = erp_action->sdev;
988 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
Christof Schmitt287ac012008-07-02 10:56:40 +0200989
990 switch (erp_action->step) {
991 case ZFCP_ERP_STEP_UNINITIALIZED:
Christof Schmittb62a8d92010-09-08 14:39:55 +0200992 zfcp_erp_lun_strategy_clearstati(sdev);
993 if (atomic_read(&zfcp_sdev->status) & ZFCP_STATUS_COMMON_OPEN)
994 return zfcp_erp_lun_strategy_close(erp_action);
Christof Schmitt287ac012008-07-02 10:56:40 +0200995 /* already closed, fall through */
Christof Schmittb62a8d92010-09-08 14:39:55 +0200996 case ZFCP_ERP_STEP_LUN_CLOSING:
997 if (atomic_read(&zfcp_sdev->status) & ZFCP_STATUS_COMMON_OPEN)
Christof Schmitt287ac012008-07-02 10:56:40 +0200998 return ZFCP_ERP_FAILED;
999 if (erp_action->status & ZFCP_STATUS_ERP_CLOSE_ONLY)
1000 return ZFCP_ERP_EXIT;
Christof Schmittb62a8d92010-09-08 14:39:55 +02001001 return zfcp_erp_lun_strategy_open(erp_action);
Christof Schmitt287ac012008-07-02 10:56:40 +02001002
Christof Schmittb62a8d92010-09-08 14:39:55 +02001003 case ZFCP_ERP_STEP_LUN_OPENING:
1004 if (atomic_read(&zfcp_sdev->status) & ZFCP_STATUS_COMMON_OPEN)
Christof Schmitt287ac012008-07-02 10:56:40 +02001005 return ZFCP_ERP_SUCCEEDED;
1006 }
1007 return ZFCP_ERP_FAILED;
1008}
1009
Christof Schmittb62a8d92010-09-08 14:39:55 +02001010static int zfcp_erp_strategy_check_lun(struct scsi_device *sdev, int result)
Christof Schmitt287ac012008-07-02 10:56:40 +02001011{
Christof Schmittb62a8d92010-09-08 14:39:55 +02001012 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
1013
Christof Schmitt287ac012008-07-02 10:56:40 +02001014 switch (result) {
1015 case ZFCP_ERP_SUCCEEDED :
Christof Schmittb62a8d92010-09-08 14:39:55 +02001016 atomic_set(&zfcp_sdev->erp_counter, 0);
1017 zfcp_erp_lun_unblock(sdev);
Christof Schmitt287ac012008-07-02 10:56:40 +02001018 break;
1019 case ZFCP_ERP_FAILED :
Christof Schmittb62a8d92010-09-08 14:39:55 +02001020 atomic_inc(&zfcp_sdev->erp_counter);
1021 if (atomic_read(&zfcp_sdev->erp_counter) > ZFCP_MAX_ERPS) {
1022 dev_err(&zfcp_sdev->port->adapter->ccw_device->dev,
1023 "ERP failed for LUN 0x%016Lx on "
Christof Schmittff3b24f2008-10-01 12:42:15 +02001024 "port 0x%016Lx\n",
Christof Schmittb62a8d92010-09-08 14:39:55 +02001025 (unsigned long long)zfcp_scsi_dev_lun(sdev),
1026 (unsigned long long)zfcp_sdev->port->wwpn);
Swen Schilligedaed852010-09-08 14:40:01 +02001027 zfcp_erp_set_lun_status(sdev,
1028 ZFCP_STATUS_COMMON_ERP_FAILED);
Christof Schmittff3b24f2008-10-01 12:42:15 +02001029 }
Christof Schmitt287ac012008-07-02 10:56:40 +02001030 break;
1031 }
1032
Christof Schmittb62a8d92010-09-08 14:39:55 +02001033 if (atomic_read(&zfcp_sdev->status) & ZFCP_STATUS_COMMON_ERP_FAILED) {
1034 zfcp_erp_lun_block(sdev, 0);
Christof Schmitt287ac012008-07-02 10:56:40 +02001035 result = ZFCP_ERP_EXIT;
1036 }
1037 return result;
1038}
1039
1040static int zfcp_erp_strategy_check_port(struct zfcp_port *port, int result)
1041{
1042 switch (result) {
1043 case ZFCP_ERP_SUCCEEDED :
1044 atomic_set(&port->erp_counter, 0);
1045 zfcp_erp_port_unblock(port);
1046 break;
1047
1048 case ZFCP_ERP_FAILED :
1049 if (atomic_read(&port->status) & ZFCP_STATUS_COMMON_NOESC) {
1050 zfcp_erp_port_block(port, 0);
1051 result = ZFCP_ERP_EXIT;
1052 }
1053 atomic_inc(&port->erp_counter);
Christof Schmittff3b24f2008-10-01 12:42:15 +02001054 if (atomic_read(&port->erp_counter) > ZFCP_MAX_ERPS) {
1055 dev_err(&port->adapter->ccw_device->dev,
1056 "ERP failed for remote port 0x%016Lx\n",
Swen Schillig7ba58c92008-10-01 12:42:18 +02001057 (unsigned long long)port->wwpn);
Swen Schilligedaed852010-09-08 14:40:01 +02001058 zfcp_erp_set_port_status(port,
1059 ZFCP_STATUS_COMMON_ERP_FAILED);
Christof Schmittff3b24f2008-10-01 12:42:15 +02001060 }
Christof Schmitt287ac012008-07-02 10:56:40 +02001061 break;
1062 }
1063
1064 if (atomic_read(&port->status) & ZFCP_STATUS_COMMON_ERP_FAILED) {
1065 zfcp_erp_port_block(port, 0);
1066 result = ZFCP_ERP_EXIT;
1067 }
1068 return result;
1069}
1070
1071static int zfcp_erp_strategy_check_adapter(struct zfcp_adapter *adapter,
1072 int result)
1073{
1074 switch (result) {
1075 case ZFCP_ERP_SUCCEEDED :
1076 atomic_set(&adapter->erp_counter, 0);
1077 zfcp_erp_adapter_unblock(adapter);
1078 break;
1079
1080 case ZFCP_ERP_FAILED :
1081 atomic_inc(&adapter->erp_counter);
Christof Schmittff3b24f2008-10-01 12:42:15 +02001082 if (atomic_read(&adapter->erp_counter) > ZFCP_MAX_ERPS) {
1083 dev_err(&adapter->ccw_device->dev,
1084 "ERP cannot recover an error "
1085 "on the FCP device\n");
Swen Schilligedaed852010-09-08 14:40:01 +02001086 zfcp_erp_set_adapter_status(adapter,
1087 ZFCP_STATUS_COMMON_ERP_FAILED);
Christof Schmittff3b24f2008-10-01 12:42:15 +02001088 }
Christof Schmitt287ac012008-07-02 10:56:40 +02001089 break;
1090 }
1091
1092 if (atomic_read(&adapter->status) & ZFCP_STATUS_COMMON_ERP_FAILED) {
1093 zfcp_erp_adapter_block(adapter, 0);
1094 result = ZFCP_ERP_EXIT;
1095 }
1096 return result;
1097}
1098
1099static int zfcp_erp_strategy_check_target(struct zfcp_erp_action *erp_action,
1100 int result)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101{
1102 struct zfcp_adapter *adapter = erp_action->adapter;
1103 struct zfcp_port *port = erp_action->port;
Christof Schmittb62a8d92010-09-08 14:39:55 +02001104 struct scsi_device *sdev = erp_action->sdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 switch (erp_action->action) {
1107
Christof Schmittb62a8d92010-09-08 14:39:55 +02001108 case ZFCP_ERP_ACTION_REOPEN_LUN:
1109 result = zfcp_erp_strategy_check_lun(sdev, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 break;
1111
1112 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
1113 case ZFCP_ERP_ACTION_REOPEN_PORT:
1114 result = zfcp_erp_strategy_check_port(port, result);
1115 break;
1116
1117 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
1118 result = zfcp_erp_strategy_check_adapter(adapter, result);
1119 break;
1120 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 return result;
1122}
1123
Christof Schmitt287ac012008-07-02 10:56:40 +02001124static int zfcp_erp_strat_change_det(atomic_t *target_status, u32 erp_status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125{
Christof Schmitt287ac012008-07-02 10:56:40 +02001126 int status = atomic_read(target_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127
Christof Schmitt287ac012008-07-02 10:56:40 +02001128 if ((status & ZFCP_STATUS_COMMON_RUNNING) &&
1129 (erp_status & ZFCP_STATUS_ERP_CLOSE_ONLY))
1130 return 1; /* take it online */
1131
1132 if (!(status & ZFCP_STATUS_COMMON_RUNNING) &&
1133 !(erp_status & ZFCP_STATUS_ERP_CLOSE_ONLY))
1134 return 1; /* take it offline */
1135
1136 return 0;
1137}
1138
1139static int zfcp_erp_strategy_statechange(struct zfcp_erp_action *act, int ret)
1140{
1141 int action = act->action;
1142 struct zfcp_adapter *adapter = act->adapter;
1143 struct zfcp_port *port = act->port;
Christof Schmittb62a8d92010-09-08 14:39:55 +02001144 struct scsi_device *sdev = act->sdev;
1145 struct zfcp_scsi_dev *zfcp_sdev;
Christof Schmitt287ac012008-07-02 10:56:40 +02001146 u32 erp_status = act->status;
1147
1148 switch (action) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
Christof Schmitt287ac012008-07-02 10:56:40 +02001150 if (zfcp_erp_strat_change_det(&adapter->status, erp_status)) {
1151 _zfcp_erp_adapter_reopen(adapter,
1152 ZFCP_STATUS_COMMON_ERP_FAILED,
Swen Schilligea4a3a62010-12-02 15:16:16 +01001153 "ersscg1");
Christof Schmitt287ac012008-07-02 10:56:40 +02001154 return ZFCP_ERP_EXIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 }
1156 break;
1157
1158 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
1159 case ZFCP_ERP_ACTION_REOPEN_PORT:
Christof Schmitt287ac012008-07-02 10:56:40 +02001160 if (zfcp_erp_strat_change_det(&port->status, erp_status)) {
1161 _zfcp_erp_port_reopen(port,
1162 ZFCP_STATUS_COMMON_ERP_FAILED,
Swen Schilligea4a3a62010-12-02 15:16:16 +01001163 "ersscg2");
Christof Schmitt287ac012008-07-02 10:56:40 +02001164 return ZFCP_ERP_EXIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 }
1166 break;
1167
Christof Schmittb62a8d92010-09-08 14:39:55 +02001168 case ZFCP_ERP_ACTION_REOPEN_LUN:
1169 zfcp_sdev = sdev_to_zfcp(sdev);
1170 if (zfcp_erp_strat_change_det(&zfcp_sdev->status, erp_status)) {
1171 _zfcp_erp_lun_reopen(sdev,
1172 ZFCP_STATUS_COMMON_ERP_FAILED,
Swen Schilligea4a3a62010-12-02 15:16:16 +01001173 "ersscg3", 0);
Christof Schmitt287ac012008-07-02 10:56:40 +02001174 return ZFCP_ERP_EXIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 }
1176 break;
1177 }
Christof Schmitt287ac012008-07-02 10:56:40 +02001178 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179}
1180
Christof Schmitt287ac012008-07-02 10:56:40 +02001181static void zfcp_erp_action_dequeue(struct zfcp_erp_action *erp_action)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182{
Christof Schmitt287ac012008-07-02 10:56:40 +02001183 struct zfcp_adapter *adapter = erp_action->adapter;
Christof Schmittb62a8d92010-09-08 14:39:55 +02001184 struct zfcp_scsi_dev *zfcp_sdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185
Christof Schmitt287ac012008-07-02 10:56:40 +02001186 adapter->erp_total_count--;
1187 if (erp_action->status & ZFCP_STATUS_ERP_LOWMEM) {
1188 adapter->erp_low_mem_count--;
1189 erp_action->status &= ~ZFCP_STATUS_ERP_LOWMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 }
1191
Christof Schmitt287ac012008-07-02 10:56:40 +02001192 list_del(&erp_action->list);
Swen Schilligae0904f2010-12-02 15:16:12 +01001193 zfcp_dbf_rec_run("eractd1", erp_action);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194
Christof Schmitt287ac012008-07-02 10:56:40 +02001195 switch (erp_action->action) {
Christof Schmittb62a8d92010-09-08 14:39:55 +02001196 case ZFCP_ERP_ACTION_REOPEN_LUN:
1197 zfcp_sdev = sdev_to_zfcp(erp_action->sdev);
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001198 atomic_andnot(ZFCP_STATUS_COMMON_ERP_INUSE,
Christof Schmittb62a8d92010-09-08 14:39:55 +02001199 &zfcp_sdev->status);
Christof Schmitt287ac012008-07-02 10:56:40 +02001200 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201
Christof Schmitt287ac012008-07-02 10:56:40 +02001202 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
1203 case ZFCP_ERP_ACTION_REOPEN_PORT:
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001204 atomic_andnot(ZFCP_STATUS_COMMON_ERP_INUSE,
Christof Schmitt287ac012008-07-02 10:56:40 +02001205 &erp_action->port->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 break;
Christof Schmitt287ac012008-07-02 10:56:40 +02001207
1208 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001209 atomic_andnot(ZFCP_STATUS_COMMON_ERP_INUSE,
Christof Schmitt287ac012008-07-02 10:56:40 +02001210 &erp_action->adapter->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 break;
1212 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213}
1214
Steffen Maier6f2ce1c2016-12-09 17:16:33 +01001215/**
1216 * zfcp_erp_try_rport_unblock - unblock rport if no more/new recovery
1217 * @port: zfcp_port whose fc_rport we should try to unblock
1218 */
1219static void zfcp_erp_try_rport_unblock(struct zfcp_port *port)
1220{
1221 unsigned long flags;
1222 struct zfcp_adapter *adapter = port->adapter;
1223 int port_status;
1224 struct Scsi_Host *shost = adapter->scsi_host;
1225 struct scsi_device *sdev;
1226
1227 write_lock_irqsave(&adapter->erp_lock, flags);
1228 port_status = atomic_read(&port->status);
1229 if ((port_status & ZFCP_STATUS_COMMON_UNBLOCKED) == 0 ||
1230 (port_status & (ZFCP_STATUS_COMMON_ERP_INUSE |
1231 ZFCP_STATUS_COMMON_ERP_FAILED)) != 0) {
1232 /* new ERP of severity >= port triggered elsewhere meanwhile or
1233 * local link down (adapter erp_failed but not clear unblock)
1234 */
1235 zfcp_dbf_rec_run_lvl(4, "ertru_p", &port->erp_action);
1236 write_unlock_irqrestore(&adapter->erp_lock, flags);
1237 return;
1238 }
1239 spin_lock(shost->host_lock);
1240 __shost_for_each_device(sdev, shost) {
1241 struct zfcp_scsi_dev *zsdev = sdev_to_zfcp(sdev);
1242 int lun_status;
1243
1244 if (zsdev->port != port)
1245 continue;
1246 /* LUN under port of interest */
1247 lun_status = atomic_read(&zsdev->status);
1248 if ((lun_status & ZFCP_STATUS_COMMON_ERP_FAILED) != 0)
1249 continue; /* unblock rport despite failed LUNs */
1250 /* LUN recovery not given up yet [maybe follow-up pending] */
1251 if ((lun_status & ZFCP_STATUS_COMMON_UNBLOCKED) == 0 ||
1252 (lun_status & ZFCP_STATUS_COMMON_ERP_INUSE) != 0) {
1253 /* LUN blocked:
1254 * not yet unblocked [LUN recovery pending]
1255 * or meanwhile blocked [new LUN recovery triggered]
1256 */
1257 zfcp_dbf_rec_run_lvl(4, "ertru_l", &zsdev->erp_action);
1258 spin_unlock(shost->host_lock);
1259 write_unlock_irqrestore(&adapter->erp_lock, flags);
1260 return;
1261 }
1262 }
1263 /* now port has no child or all children have completed recovery,
1264 * and no ERP of severity >= port was meanwhile triggered elsewhere
1265 */
1266 zfcp_scsi_schedule_rport_register(port);
1267 spin_unlock(shost->host_lock);
1268 write_unlock_irqrestore(&adapter->erp_lock, flags);
1269}
1270
Christof Schmitt287ac012008-07-02 10:56:40 +02001271static void zfcp_erp_action_cleanup(struct zfcp_erp_action *act, int result)
Maxim Shchetyninaef4a982005-09-13 21:51:16 +02001272{
Christof Schmitt287ac012008-07-02 10:56:40 +02001273 struct zfcp_adapter *adapter = act->adapter;
1274 struct zfcp_port *port = act->port;
Christof Schmittb62a8d92010-09-08 14:39:55 +02001275 struct scsi_device *sdev = act->sdev;
Maxim Shchetyninaef4a982005-09-13 21:51:16 +02001276
Christof Schmitt287ac012008-07-02 10:56:40 +02001277 switch (act->action) {
Christof Schmittb62a8d92010-09-08 14:39:55 +02001278 case ZFCP_ERP_ACTION_REOPEN_LUN:
Christof Schmittfdbd1c52010-09-08 14:39:54 +02001279 if (!(act->status & ZFCP_STATUS_ERP_NO_REF))
Christof Schmittb62a8d92010-09-08 14:39:55 +02001280 scsi_device_put(sdev);
Steffen Maier6f2ce1c2016-12-09 17:16:33 +01001281 zfcp_erp_try_rport_unblock(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 break;
Christof Schmitt287ac012008-07-02 10:56:40 +02001283
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 case ZFCP_ERP_ACTION_REOPEN_PORT:
Steffen Maier4eeaa4f2016-08-10 18:30:46 +02001285 /* This switch case might also happen after a forced reopen
1286 * was successfully done and thus overwritten with a new
1287 * non-forced reopen at `ersfs_2'. In this case, we must not
1288 * do the clean-up of the non-forced version.
1289 */
1290 if (act->step != ZFCP_ERP_STEP_UNINITIALIZED)
1291 if (result == ZFCP_ERP_SUCCEEDED)
Steffen Maier6f2ce1c2016-12-09 17:16:33 +01001292 zfcp_erp_try_rport_unblock(port);
Christof Schmitt57676202010-07-08 09:53:05 +02001293 /* fall through */
1294 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
Christof Schmitt615f59e2010-02-17 11:18:56 +01001295 put_device(&port->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 break;
Christof Schmitt287ac012008-07-02 10:56:40 +02001297
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
Christof Schmitta2fa0ae2009-03-02 13:09:08 +01001299 if (result == ZFCP_ERP_SUCCEEDED) {
Christof Schmittbd43a42b72008-12-25 13:38:50 +01001300 register_service_level(&adapter->service_level);
Steffen Maier43f60cb2012-09-04 15:23:35 +02001301 zfcp_fc_conditional_port_scan(adapter);
Christof Schmitt038d9442011-02-22 19:54:48 +01001302 queue_work(adapter->work_queue, &adapter->ns_up_work);
Christof Schmitta2fa0ae2009-03-02 13:09:08 +01001303 } else
1304 unregister_service_level(&adapter->service_level);
Christof Schmitt038d9442011-02-22 19:54:48 +01001305
Swen Schilligf3450c72009-11-24 16:53:59 +01001306 kref_put(&adapter->ref, zfcp_adapter_release);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 }
1309}
1310
Christof Schmitt287ac012008-07-02 10:56:40 +02001311static int zfcp_erp_strategy_do_action(struct zfcp_erp_action *erp_action)
1312{
1313 switch (erp_action->action) {
1314 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
1315 return zfcp_erp_adapter_strategy(erp_action);
1316 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
1317 return zfcp_erp_port_forced_strategy(erp_action);
1318 case ZFCP_ERP_ACTION_REOPEN_PORT:
1319 return zfcp_erp_port_strategy(erp_action);
Christof Schmittb62a8d92010-09-08 14:39:55 +02001320 case ZFCP_ERP_ACTION_REOPEN_LUN:
1321 return zfcp_erp_lun_strategy(erp_action);
Christof Schmitt287ac012008-07-02 10:56:40 +02001322 }
1323 return ZFCP_ERP_FAILED;
1324}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325
Christof Schmitt287ac012008-07-02 10:56:40 +02001326static int zfcp_erp_strategy(struct zfcp_erp_action *erp_action)
1327{
1328 int retval;
Christof Schmitt287ac012008-07-02 10:56:40 +02001329 unsigned long flags;
Swen Schilligecf0c772009-11-24 16:53:58 +01001330 struct zfcp_adapter *adapter = erp_action->adapter;
Christof Schmitt287ac012008-07-02 10:56:40 +02001331
Swen Schilligf3450c72009-11-24 16:53:59 +01001332 kref_get(&adapter->ref);
Christof Schmitt287ac012008-07-02 10:56:40 +02001333
Swen Schilligf3450c72009-11-24 16:53:59 +01001334 write_lock_irqsave(&adapter->erp_lock, flags);
Christof Schmitt287ac012008-07-02 10:56:40 +02001335 zfcp_erp_strategy_check_fsfreq(erp_action);
1336
1337 if (erp_action->status & ZFCP_STATUS_ERP_DISMISSED) {
1338 zfcp_erp_action_dequeue(erp_action);
1339 retval = ZFCP_ERP_DISMISSED;
1340 goto unlock;
1341 }
1342
Christof Schmitt9c785d92010-07-08 09:53:09 +02001343 if (erp_action->status & ZFCP_STATUS_ERP_TIMEDOUT) {
1344 retval = ZFCP_ERP_FAILED;
1345 goto check_target;
1346 }
1347
Christof Schmitt287ac012008-07-02 10:56:40 +02001348 zfcp_erp_action_to_running(erp_action);
1349
1350 /* no lock to allow for blocking operations */
Swen Schilligecf0c772009-11-24 16:53:58 +01001351 write_unlock_irqrestore(&adapter->erp_lock, flags);
Christof Schmitt287ac012008-07-02 10:56:40 +02001352 retval = zfcp_erp_strategy_do_action(erp_action);
Swen Schilligecf0c772009-11-24 16:53:58 +01001353 write_lock_irqsave(&adapter->erp_lock, flags);
Christof Schmitt287ac012008-07-02 10:56:40 +02001354
1355 if (erp_action->status & ZFCP_STATUS_ERP_DISMISSED)
1356 retval = ZFCP_ERP_CONTINUES;
1357
1358 switch (retval) {
1359 case ZFCP_ERP_NOMEM:
1360 if (!(erp_action->status & ZFCP_STATUS_ERP_LOWMEM)) {
1361 ++adapter->erp_low_mem_count;
1362 erp_action->status |= ZFCP_STATUS_ERP_LOWMEM;
1363 }
1364 if (adapter->erp_total_count == adapter->erp_low_mem_count)
Swen Schilligea4a3a62010-12-02 15:16:16 +01001365 _zfcp_erp_adapter_reopen(adapter, 0, "erstgy1");
Christof Schmitt287ac012008-07-02 10:56:40 +02001366 else {
1367 zfcp_erp_strategy_memwait(erp_action);
1368 retval = ZFCP_ERP_CONTINUES;
1369 }
1370 goto unlock;
1371
1372 case ZFCP_ERP_CONTINUES:
1373 if (erp_action->status & ZFCP_STATUS_ERP_LOWMEM) {
1374 --adapter->erp_low_mem_count;
1375 erp_action->status &= ~ZFCP_STATUS_ERP_LOWMEM;
1376 }
1377 goto unlock;
1378 }
1379
Christof Schmitt9c785d92010-07-08 09:53:09 +02001380check_target:
Christof Schmitt287ac012008-07-02 10:56:40 +02001381 retval = zfcp_erp_strategy_check_target(erp_action, retval);
1382 zfcp_erp_action_dequeue(erp_action);
1383 retval = zfcp_erp_strategy_statechange(erp_action, retval);
1384 if (retval == ZFCP_ERP_EXIT)
1385 goto unlock;
Christof Schmitt85600f72009-07-13 15:06:09 +02001386 if (retval == ZFCP_ERP_SUCCEEDED)
1387 zfcp_erp_strategy_followup_success(erp_action);
1388 if (retval == ZFCP_ERP_FAILED)
1389 zfcp_erp_strategy_followup_failed(erp_action);
Christof Schmitt287ac012008-07-02 10:56:40 +02001390
1391 unlock:
Swen Schilligecf0c772009-11-24 16:53:58 +01001392 write_unlock_irqrestore(&adapter->erp_lock, flags);
Christof Schmitt287ac012008-07-02 10:56:40 +02001393
1394 if (retval != ZFCP_ERP_CONTINUES)
1395 zfcp_erp_action_cleanup(erp_action, retval);
1396
Swen Schilligf3450c72009-11-24 16:53:59 +01001397 kref_put(&adapter->ref, zfcp_adapter_release);
Christof Schmitt287ac012008-07-02 10:56:40 +02001398 return retval;
1399}
1400
1401static int zfcp_erp_thread(void *data)
1402{
1403 struct zfcp_adapter *adapter = (struct zfcp_adapter *) data;
1404 struct list_head *next;
1405 struct zfcp_erp_action *act;
1406 unsigned long flags;
1407
Christof Schmitt347c6a92009-08-18 15:43:25 +02001408 for (;;) {
Christof Schmitt347c6a92009-08-18 15:43:25 +02001409 wait_event_interruptible(adapter->erp_ready_wq,
1410 !list_empty(&adapter->erp_ready_head) ||
1411 kthread_should_stop());
Swen Schillig94ab4b32009-04-17 15:08:06 +02001412
Christof Schmitt347c6a92009-08-18 15:43:25 +02001413 if (kthread_should_stop())
1414 break;
1415
Christof Schmitt287ac012008-07-02 10:56:40 +02001416 write_lock_irqsave(&adapter->erp_lock, flags);
1417 next = adapter->erp_ready_head.next;
1418 write_unlock_irqrestore(&adapter->erp_lock, flags);
1419
1420 if (next != &adapter->erp_ready_head) {
1421 act = list_entry(next, struct zfcp_erp_action, list);
1422
1423 /* there is more to come after dismission, no notify */
1424 if (zfcp_erp_strategy(act) != ZFCP_ERP_DISMISSED)
1425 zfcp_erp_wakeup(adapter);
1426 }
Christof Schmitt287ac012008-07-02 10:56:40 +02001427 }
1428
Christof Schmitt287ac012008-07-02 10:56:40 +02001429 return 0;
1430}
1431
1432/**
1433 * zfcp_erp_thread_setup - Start ERP thread for adapter
1434 * @adapter: Adapter to start the ERP thread for
1435 *
1436 * Returns 0 on success or error code from kernel_thread()
1437 */
1438int zfcp_erp_thread_setup(struct zfcp_adapter *adapter)
1439{
Christof Schmitt347c6a92009-08-18 15:43:25 +02001440 struct task_struct *thread;
Christof Schmitt287ac012008-07-02 10:56:40 +02001441
Christof Schmitt347c6a92009-08-18 15:43:25 +02001442 thread = kthread_run(zfcp_erp_thread, adapter, "zfcperp%s",
1443 dev_name(&adapter->ccw_device->dev));
1444 if (IS_ERR(thread)) {
Christof Schmitt287ac012008-07-02 10:56:40 +02001445 dev_err(&adapter->ccw_device->dev,
Christof Schmittff3b24f2008-10-01 12:42:15 +02001446 "Creating an ERP thread for the FCP device failed.\n");
Christof Schmitt347c6a92009-08-18 15:43:25 +02001447 return PTR_ERR(thread);
Christof Schmitt287ac012008-07-02 10:56:40 +02001448 }
Christof Schmitt347c6a92009-08-18 15:43:25 +02001449
1450 adapter->erp_thread = thread;
Christof Schmitt287ac012008-07-02 10:56:40 +02001451 return 0;
1452}
1453
1454/**
1455 * zfcp_erp_thread_kill - Stop ERP thread.
1456 * @adapter: Adapter where the ERP thread should be stopped.
1457 *
1458 * The caller of this routine ensures that the specified adapter has
1459 * been shut down and that this operation has been completed. Thus,
1460 * there are no pending erp_actions which would need to be handled
1461 * here.
1462 */
1463void zfcp_erp_thread_kill(struct zfcp_adapter *adapter)
1464{
Christof Schmitt347c6a92009-08-18 15:43:25 +02001465 kthread_stop(adapter->erp_thread);
1466 adapter->erp_thread = NULL;
Christof Schmitt143bb6b2009-08-18 15:43:27 +02001467 WARN_ON(!list_empty(&adapter->erp_ready_head));
1468 WARN_ON(!list_empty(&adapter->erp_running_head));
Christof Schmitt287ac012008-07-02 10:56:40 +02001469}
1470
1471/**
Christof Schmitt287ac012008-07-02 10:56:40 +02001472 * zfcp_erp_wait - wait for completion of error recovery on an adapter
1473 * @adapter: adapter for which to wait for completion of its error recovery
1474 */
1475void zfcp_erp_wait(struct zfcp_adapter *adapter)
1476{
1477 wait_event(adapter->erp_done_wqh,
1478 !(atomic_read(&adapter->status) &
1479 ZFCP_STATUS_ADAPTER_ERP_PENDING));
1480}
1481
1482/**
Swen Schilligedaed852010-09-08 14:40:01 +02001483 * zfcp_erp_set_adapter_status - set adapter status bits
Christof Schmitt287ac012008-07-02 10:56:40 +02001484 * @adapter: adapter to change the status
Christof Schmitt287ac012008-07-02 10:56:40 +02001485 * @mask: status bits to change
Christof Schmitt287ac012008-07-02 10:56:40 +02001486 *
Christof Schmittb62a8d92010-09-08 14:39:55 +02001487 * Changes in common status bits are propagated to attached ports and LUNs.
Christof Schmitt287ac012008-07-02 10:56:40 +02001488 */
Swen Schilligedaed852010-09-08 14:40:01 +02001489void zfcp_erp_set_adapter_status(struct zfcp_adapter *adapter, u32 mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 struct zfcp_port *port;
Swen Schilligedaed852010-09-08 14:40:01 +02001492 struct scsi_device *sdev;
Swen Schilligecf0c772009-11-24 16:53:58 +01001493 unsigned long flags;
Christof Schmitt287ac012008-07-02 10:56:40 +02001494 u32 common_mask = mask & ZFCP_COMMON_FLAGS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001496 atomic_or(mask, &adapter->status);
Christof Schmitt287ac012008-07-02 10:56:40 +02001497
Swen Schilligedaed852010-09-08 14:40:01 +02001498 if (!common_mask)
1499 return;
1500
1501 read_lock_irqsave(&adapter->port_list_lock, flags);
1502 list_for_each_entry(port, &adapter->port_list, list)
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001503 atomic_or(common_mask, &port->status);
Swen Schilligedaed852010-09-08 14:40:01 +02001504 read_unlock_irqrestore(&adapter->port_list_lock, flags);
1505
Martin Peschke924dd582013-08-22 17:45:37 +02001506 spin_lock_irqsave(adapter->scsi_host->host_lock, flags);
1507 __shost_for_each_device(sdev, adapter->scsi_host)
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001508 atomic_or(common_mask, &sdev_to_zfcp(sdev)->status);
Martin Peschke924dd582013-08-22 17:45:37 +02001509 spin_unlock_irqrestore(adapter->scsi_host->host_lock, flags);
Swen Schilligedaed852010-09-08 14:40:01 +02001510}
1511
1512/**
1513 * zfcp_erp_clear_adapter_status - clear adapter status bits
1514 * @adapter: adapter to change the status
1515 * @mask: status bits to change
1516 *
1517 * Changes in common status bits are propagated to attached ports and LUNs.
1518 */
1519void zfcp_erp_clear_adapter_status(struct zfcp_adapter *adapter, u32 mask)
1520{
1521 struct zfcp_port *port;
1522 struct scsi_device *sdev;
1523 unsigned long flags;
1524 u32 common_mask = mask & ZFCP_COMMON_FLAGS;
1525 u32 clear_counter = mask & ZFCP_STATUS_COMMON_ERP_FAILED;
1526
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001527 atomic_andnot(mask, &adapter->status);
Swen Schilligedaed852010-09-08 14:40:01 +02001528
1529 if (!common_mask)
1530 return;
1531
1532 if (clear_counter)
1533 atomic_set(&adapter->erp_counter, 0);
1534
1535 read_lock_irqsave(&adapter->port_list_lock, flags);
1536 list_for_each_entry(port, &adapter->port_list, list) {
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001537 atomic_andnot(common_mask, &port->status);
Swen Schilligedaed852010-09-08 14:40:01 +02001538 if (clear_counter)
1539 atomic_set(&port->erp_counter, 0);
1540 }
1541 read_unlock_irqrestore(&adapter->port_list_lock, flags);
1542
Martin Peschke924dd582013-08-22 17:45:37 +02001543 spin_lock_irqsave(adapter->scsi_host->host_lock, flags);
1544 __shost_for_each_device(sdev, adapter->scsi_host) {
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001545 atomic_andnot(common_mask, &sdev_to_zfcp(sdev)->status);
Swen Schilligedaed852010-09-08 14:40:01 +02001546 if (clear_counter)
1547 atomic_set(&sdev_to_zfcp(sdev)->erp_counter, 0);
Swen Schilligecf0c772009-11-24 16:53:58 +01001548 }
Martin Peschke924dd582013-08-22 17:45:37 +02001549 spin_unlock_irqrestore(adapter->scsi_host->host_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550}
1551
Christof Schmitt287ac012008-07-02 10:56:40 +02001552/**
Swen Schilligedaed852010-09-08 14:40:01 +02001553 * zfcp_erp_set_port_status - set port status bits
1554 * @port: port to change the status
Christof Schmitt287ac012008-07-02 10:56:40 +02001555 * @mask: status bits to change
Christof Schmitt287ac012008-07-02 10:56:40 +02001556 *
Christof Schmittb62a8d92010-09-08 14:39:55 +02001557 * Changes in common status bits are propagated to attached LUNs.
Christof Schmitt287ac012008-07-02 10:56:40 +02001558 */
Swen Schilligedaed852010-09-08 14:40:01 +02001559void zfcp_erp_set_port_status(struct zfcp_port *port, u32 mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560{
Christof Schmittb62a8d92010-09-08 14:39:55 +02001561 struct scsi_device *sdev;
Christof Schmitt287ac012008-07-02 10:56:40 +02001562 u32 common_mask = mask & ZFCP_COMMON_FLAGS;
Martin Peschke924dd582013-08-22 17:45:37 +02001563 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001565 atomic_or(mask, &port->status);
Christof Schmitt287ac012008-07-02 10:56:40 +02001566
Swen Schilligedaed852010-09-08 14:40:01 +02001567 if (!common_mask)
1568 return;
1569
Martin Peschke924dd582013-08-22 17:45:37 +02001570 spin_lock_irqsave(port->adapter->scsi_host->host_lock, flags);
1571 __shost_for_each_device(sdev, port->adapter->scsi_host)
Swen Schilligedaed852010-09-08 14:40:01 +02001572 if (sdev_to_zfcp(sdev)->port == port)
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001573 atomic_or(common_mask,
Swen Schilligedaed852010-09-08 14:40:01 +02001574 &sdev_to_zfcp(sdev)->status);
Martin Peschke924dd582013-08-22 17:45:37 +02001575 spin_unlock_irqrestore(port->adapter->scsi_host->host_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576}
1577
Christof Schmitt287ac012008-07-02 10:56:40 +02001578/**
Swen Schilligedaed852010-09-08 14:40:01 +02001579 * zfcp_erp_clear_port_status - clear port status bits
1580 * @port: adapter to change the status
Christof Schmitt287ac012008-07-02 10:56:40 +02001581 * @mask: status bits to change
Swen Schilligedaed852010-09-08 14:40:01 +02001582 *
1583 * Changes in common status bits are propagated to attached LUNs.
Christof Schmitt287ac012008-07-02 10:56:40 +02001584 */
Swen Schilligedaed852010-09-08 14:40:01 +02001585void zfcp_erp_clear_port_status(struct zfcp_port *port, u32 mask)
1586{
1587 struct scsi_device *sdev;
1588 u32 common_mask = mask & ZFCP_COMMON_FLAGS;
1589 u32 clear_counter = mask & ZFCP_STATUS_COMMON_ERP_FAILED;
Martin Peschke924dd582013-08-22 17:45:37 +02001590 unsigned long flags;
Swen Schilligedaed852010-09-08 14:40:01 +02001591
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001592 atomic_andnot(mask, &port->status);
Swen Schilligedaed852010-09-08 14:40:01 +02001593
1594 if (!common_mask)
1595 return;
1596
1597 if (clear_counter)
1598 atomic_set(&port->erp_counter, 0);
1599
Martin Peschke924dd582013-08-22 17:45:37 +02001600 spin_lock_irqsave(port->adapter->scsi_host->host_lock, flags);
1601 __shost_for_each_device(sdev, port->adapter->scsi_host)
Swen Schilligedaed852010-09-08 14:40:01 +02001602 if (sdev_to_zfcp(sdev)->port == port) {
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001603 atomic_andnot(common_mask,
Swen Schilligedaed852010-09-08 14:40:01 +02001604 &sdev_to_zfcp(sdev)->status);
1605 if (clear_counter)
1606 atomic_set(&sdev_to_zfcp(sdev)->erp_counter, 0);
1607 }
Martin Peschke924dd582013-08-22 17:45:37 +02001608 spin_unlock_irqrestore(port->adapter->scsi_host->host_lock, flags);
Swen Schilligedaed852010-09-08 14:40:01 +02001609}
1610
1611/**
1612 * zfcp_erp_set_lun_status - set lun status bits
1613 * @sdev: SCSI device / lun to set the status bits
1614 * @mask: status bits to change
1615 */
1616void zfcp_erp_set_lun_status(struct scsi_device *sdev, u32 mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617{
Christof Schmittb62a8d92010-09-08 14:39:55 +02001618 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
1619
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001620 atomic_or(mask, &zfcp_sdev->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621}
1622
Christof Schmitt287ac012008-07-02 10:56:40 +02001623/**
Swen Schilligedaed852010-09-08 14:40:01 +02001624 * zfcp_erp_clear_lun_status - clear lun status bits
1625 * @sdev: SCSi device / lun to clear the status bits
1626 * @mask: status bits to change
Christof Schmitt287ac012008-07-02 10:56:40 +02001627 */
Swen Schilligedaed852010-09-08 14:40:01 +02001628void zfcp_erp_clear_lun_status(struct scsi_device *sdev, u32 mask)
Andreas Herrmannd736a27b2005-06-13 13:23:57 +02001629{
Swen Schilligedaed852010-09-08 14:40:01 +02001630 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
1631
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001632 atomic_andnot(mask, &zfcp_sdev->status);
Swen Schilligedaed852010-09-08 14:40:01 +02001633
1634 if (mask & ZFCP_STATUS_COMMON_ERP_FAILED)
1635 atomic_set(&zfcp_sdev->erp_counter, 0);
Andreas Herrmannd736a27b2005-06-13 13:23:57 +02001636}
1637