blob: 69dfb328dba4485549c4ab18d8b341e2c9649e77 [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
Steffen Maier512857a72018-05-17 19:14:45 +020038/**
39 * enum zfcp_erp_act_type - Type of ERP action object.
40 * @ZFCP_ERP_ACTION_REOPEN_LUN: LUN recovery.
41 * @ZFCP_ERP_ACTION_REOPEN_PORT: Port recovery.
42 * @ZFCP_ERP_ACTION_REOPEN_PORT_FORCED: Forced port recovery.
43 * @ZFCP_ERP_ACTION_REOPEN_ADAPTER: Adapter recovery.
44 * @ZFCP_ERP_ACTION_NONE: Eyecatcher pseudo flag to bitwise or-combine with
Steffen Maierd70aab52018-05-17 19:14:47 +020045 * either of the first four enum values.
Steffen Maier512857a72018-05-17 19:14:45 +020046 * Used to indicate that an ERP action could not be
47 * set up despite a detected need for some recovery.
Steffen Maierd70aab52018-05-17 19:14:47 +020048 * @ZFCP_ERP_ACTION_FAILED: Eyecatcher pseudo flag to bitwise or-combine with
49 * either of the first four enum values.
50 * Used to indicate that ERP not needed because
51 * the object has ZFCP_STATUS_COMMON_ERP_FAILED.
Steffen Maier512857a72018-05-17 19:14:45 +020052 */
Christof Schmitt287ac012008-07-02 10:56:40 +020053enum zfcp_erp_act_type {
Christof Schmittb62a8d92010-09-08 14:39:55 +020054 ZFCP_ERP_ACTION_REOPEN_LUN = 1,
Christof Schmitt287ac012008-07-02 10:56:40 +020055 ZFCP_ERP_ACTION_REOPEN_PORT = 2,
56 ZFCP_ERP_ACTION_REOPEN_PORT_FORCED = 3,
57 ZFCP_ERP_ACTION_REOPEN_ADAPTER = 4,
Steffen Maier512857a72018-05-17 19:14:45 +020058 ZFCP_ERP_ACTION_NONE = 0xc0,
Steffen Maierd70aab52018-05-17 19:14:47 +020059 ZFCP_ERP_ACTION_FAILED = 0xe0,
Christof Schmitt287ac012008-07-02 10:56:40 +020060};
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Christof Schmitt287ac012008-07-02 10:56:40 +020062enum zfcp_erp_act_state {
63 ZFCP_ERP_ACTION_RUNNING = 1,
64 ZFCP_ERP_ACTION_READY = 2,
65};
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
Christof Schmitt287ac012008-07-02 10:56:40 +020067enum zfcp_erp_act_result {
68 ZFCP_ERP_SUCCEEDED = 0,
69 ZFCP_ERP_FAILED = 1,
70 ZFCP_ERP_CONTINUES = 2,
71 ZFCP_ERP_EXIT = 3,
72 ZFCP_ERP_DISMISSED = 4,
73 ZFCP_ERP_NOMEM = 5,
74};
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
Christof Schmitt287ac012008-07-02 10:56:40 +020076static void zfcp_erp_adapter_block(struct zfcp_adapter *adapter, int mask)
Andreas Herrmann2abbe862006-09-18 22:29:56 +020077{
Swen Schilligedaed852010-09-08 14:40:01 +020078 zfcp_erp_clear_adapter_status(adapter,
79 ZFCP_STATUS_COMMON_UNBLOCKED | mask);
Andreas Herrmann2abbe862006-09-18 22:29:56 +020080}
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
Christof Schmitt287ac012008-07-02 10:56:40 +020082static int zfcp_erp_action_exists(struct zfcp_erp_action *act)
Linus Torvalds1da177e2005-04-16 15:20:36 -070083{
Christof Schmitt287ac012008-07-02 10:56:40 +020084 struct zfcp_erp_action *curr_act;
85
86 list_for_each_entry(curr_act, &act->adapter->erp_running_head, list)
87 if (act == curr_act)
88 return ZFCP_ERP_ACTION_RUNNING;
89 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090}
91
Christof Schmitt287ac012008-07-02 10:56:40 +020092static void zfcp_erp_action_ready(struct zfcp_erp_action *act)
Linus Torvalds1da177e2005-04-16 15:20:36 -070093{
Christof Schmitt287ac012008-07-02 10:56:40 +020094 struct zfcp_adapter *adapter = act->adapter;
95
96 list_move(&act->list, &act->adapter->erp_ready_head);
Swen Schilligae0904f2010-12-02 15:16:12 +010097 zfcp_dbf_rec_run("erardy1", act);
Christof Schmitt347c6a92009-08-18 15:43:25 +020098 wake_up(&adapter->erp_ready_wq);
Swen Schilligae0904f2010-12-02 15:16:12 +010099 zfcp_dbf_rec_run("erardy2", act);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100}
101
Christof Schmitt287ac012008-07-02 10:56:40 +0200102static void zfcp_erp_action_dismiss(struct zfcp_erp_action *act)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103{
Christof Schmitt287ac012008-07-02 10:56:40 +0200104 act->status |= ZFCP_STATUS_ERP_DISMISSED;
105 if (zfcp_erp_action_exists(act) == ZFCP_ERP_ACTION_RUNNING)
106 zfcp_erp_action_ready(act);
107}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
Christof Schmittb62a8d92010-09-08 14:39:55 +0200109static void zfcp_erp_action_dismiss_lun(struct scsi_device *sdev)
Christof Schmitt287ac012008-07-02 10:56:40 +0200110{
Christof Schmittb62a8d92010-09-08 14:39:55 +0200111 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
112
113 if (atomic_read(&zfcp_sdev->status) & ZFCP_STATUS_COMMON_ERP_INUSE)
114 zfcp_erp_action_dismiss(&zfcp_sdev->erp_action);
Christof Schmitt287ac012008-07-02 10:56:40 +0200115}
116
117static void zfcp_erp_action_dismiss_port(struct zfcp_port *port)
118{
Christof Schmittb62a8d92010-09-08 14:39:55 +0200119 struct scsi_device *sdev;
Christof Schmitt287ac012008-07-02 10:56:40 +0200120
121 if (atomic_read(&port->status) & ZFCP_STATUS_COMMON_ERP_INUSE)
122 zfcp_erp_action_dismiss(&port->erp_action);
Martin Peschke924dd582013-08-22 17:45:37 +0200123 else {
124 spin_lock(port->adapter->scsi_host->host_lock);
125 __shost_for_each_device(sdev, port->adapter->scsi_host)
Christof Schmittb62a8d92010-09-08 14:39:55 +0200126 if (sdev_to_zfcp(sdev)->port == port)
127 zfcp_erp_action_dismiss_lun(sdev);
Martin Peschke924dd582013-08-22 17:45:37 +0200128 spin_unlock(port->adapter->scsi_host->host_lock);
129 }
Christof Schmitt287ac012008-07-02 10:56:40 +0200130}
131
132static void zfcp_erp_action_dismiss_adapter(struct zfcp_adapter *adapter)
133{
134 struct zfcp_port *port;
135
136 if (atomic_read(&adapter->status) & ZFCP_STATUS_COMMON_ERP_INUSE)
137 zfcp_erp_action_dismiss(&adapter->erp_action);
Swen Schilligecf0c772009-11-24 16:53:58 +0100138 else {
139 read_lock(&adapter->port_list_lock);
140 list_for_each_entry(port, &adapter->port_list, list)
Christof Schmitt287ac012008-07-02 10:56:40 +0200141 zfcp_erp_action_dismiss_port(port);
Swen Schilligecf0c772009-11-24 16:53:58 +0100142 read_unlock(&adapter->port_list_lock);
143 }
Christof Schmitt287ac012008-07-02 10:56:40 +0200144}
145
Steffen Maier8c3d20a2018-05-17 19:14:48 +0200146static int zfcp_erp_handle_failed(int want, struct zfcp_adapter *adapter,
147 struct zfcp_port *port,
148 struct scsi_device *sdev)
149{
150 int need = want;
151 struct zfcp_scsi_dev *zsdev;
152
153 switch (want) {
154 case ZFCP_ERP_ACTION_REOPEN_LUN:
155 zsdev = sdev_to_zfcp(sdev);
156 if (atomic_read(&zsdev->status) & ZFCP_STATUS_COMMON_ERP_FAILED)
157 need = 0;
158 break;
159 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
160 if (atomic_read(&port->status) & ZFCP_STATUS_COMMON_ERP_FAILED)
161 need = 0;
162 break;
163 case ZFCP_ERP_ACTION_REOPEN_PORT:
164 if (atomic_read(&port->status) &
165 ZFCP_STATUS_COMMON_ERP_FAILED) {
166 need = 0;
167 /* ensure propagation of failed status to new devices */
168 zfcp_erp_set_port_status(
169 port, ZFCP_STATUS_COMMON_ERP_FAILED);
170 }
171 break;
172 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
173 if (atomic_read(&adapter->status) &
174 ZFCP_STATUS_COMMON_ERP_FAILED) {
175 need = 0;
176 /* ensure propagation of failed status to new devices */
177 zfcp_erp_set_adapter_status(
178 adapter, ZFCP_STATUS_COMMON_ERP_FAILED);
179 }
180 break;
181 default:
182 need = 0;
183 break;
184 }
185
186 return need;
187}
188
Christof Schmitt287ac012008-07-02 10:56:40 +0200189static int zfcp_erp_required_act(int want, struct zfcp_adapter *adapter,
190 struct zfcp_port *port,
Christof Schmittb62a8d92010-09-08 14:39:55 +0200191 struct scsi_device *sdev)
Christof Schmitt287ac012008-07-02 10:56:40 +0200192{
193 int need = want;
Christof Schmittb62a8d92010-09-08 14:39:55 +0200194 int l_status, p_status, a_status;
195 struct zfcp_scsi_dev *zfcp_sdev;
Christof Schmitt287ac012008-07-02 10:56:40 +0200196
197 switch (want) {
Christof Schmittb62a8d92010-09-08 14:39:55 +0200198 case ZFCP_ERP_ACTION_REOPEN_LUN:
199 zfcp_sdev = sdev_to_zfcp(sdev);
200 l_status = atomic_read(&zfcp_sdev->status);
201 if (l_status & ZFCP_STATUS_COMMON_ERP_INUSE)
Christof Schmitt287ac012008-07-02 10:56:40 +0200202 return 0;
203 p_status = atomic_read(&port->status);
204 if (!(p_status & ZFCP_STATUS_COMMON_RUNNING) ||
205 p_status & ZFCP_STATUS_COMMON_ERP_FAILED)
206 return 0;
207 if (!(p_status & ZFCP_STATUS_COMMON_UNBLOCKED))
208 need = ZFCP_ERP_ACTION_REOPEN_PORT;
209 /* fall through */
Christof Schmitt287ac012008-07-02 10:56:40 +0200210 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
211 p_status = atomic_read(&port->status);
Christof Schmitt097ef3b2010-07-08 09:53:06 +0200212 if (!(p_status & ZFCP_STATUS_COMMON_OPEN))
213 need = ZFCP_ERP_ACTION_REOPEN_PORT;
214 /* fall through */
215 case ZFCP_ERP_ACTION_REOPEN_PORT:
216 p_status = atomic_read(&port->status);
Christof Schmitt287ac012008-07-02 10:56:40 +0200217 if (p_status & ZFCP_STATUS_COMMON_ERP_INUSE)
218 return 0;
219 a_status = atomic_read(&adapter->status);
220 if (!(a_status & ZFCP_STATUS_COMMON_RUNNING) ||
221 a_status & ZFCP_STATUS_COMMON_ERP_FAILED)
222 return 0;
Swen Schilligd3e10882010-11-17 14:23:42 +0100223 if (p_status & ZFCP_STATUS_COMMON_NOESC)
224 return need;
Christof Schmitt287ac012008-07-02 10:56:40 +0200225 if (!(a_status & ZFCP_STATUS_COMMON_UNBLOCKED))
226 need = ZFCP_ERP_ACTION_REOPEN_ADAPTER;
227 /* fall through */
228 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
229 a_status = atomic_read(&adapter->status);
230 if (a_status & ZFCP_STATUS_COMMON_ERP_INUSE)
231 return 0;
Christof Schmitt143bb6b2009-08-18 15:43:27 +0200232 if (!(a_status & ZFCP_STATUS_COMMON_RUNNING) &&
233 !(a_status & ZFCP_STATUS_COMMON_OPEN))
234 return 0; /* shutdown requested for closed adapter */
Christof Schmitt287ac012008-07-02 10:56:40 +0200235 }
236
237 return need;
238}
239
Christof Schmittfdbd1c52010-09-08 14:39:54 +0200240static struct zfcp_erp_action *zfcp_erp_setup_act(int need, u32 act_status,
Christof Schmitt287ac012008-07-02 10:56:40 +0200241 struct zfcp_adapter *adapter,
242 struct zfcp_port *port,
Christof Schmittb62a8d92010-09-08 14:39:55 +0200243 struct scsi_device *sdev)
Christof Schmitt287ac012008-07-02 10:56:40 +0200244{
245 struct zfcp_erp_action *erp_action;
Christof Schmittb62a8d92010-09-08 14:39:55 +0200246 struct zfcp_scsi_dev *zfcp_sdev;
Christof Schmitt287ac012008-07-02 10:56:40 +0200247
248 switch (need) {
Christof Schmittb62a8d92010-09-08 14:39:55 +0200249 case ZFCP_ERP_ACTION_REOPEN_LUN:
250 zfcp_sdev = sdev_to_zfcp(sdev);
Christof Schmittfdbd1c52010-09-08 14:39:54 +0200251 if (!(act_status & ZFCP_STATUS_ERP_NO_REF))
Christof Schmittb62a8d92010-09-08 14:39:55 +0200252 if (scsi_device_get(sdev))
Christof Schmittfdbd1c52010-09-08 14:39:54 +0200253 return NULL;
Peter Zijlstra805de8f42015-04-24 01:12:32 +0200254 atomic_or(ZFCP_STATUS_COMMON_ERP_INUSE,
Christof Schmittb62a8d92010-09-08 14:39:55 +0200255 &zfcp_sdev->status);
256 erp_action = &zfcp_sdev->erp_action;
Steffen Maierab31fd02017-10-13 15:40:07 +0200257 WARN_ON_ONCE(erp_action->port != port);
258 WARN_ON_ONCE(erp_action->sdev != sdev);
Christof Schmittb62a8d92010-09-08 14:39:55 +0200259 if (!(atomic_read(&zfcp_sdev->status) &
260 ZFCP_STATUS_COMMON_RUNNING))
Christof Schmittfdbd1c52010-09-08 14:39:54 +0200261 act_status |= ZFCP_STATUS_ERP_CLOSE_ONLY;
Christof Schmitt287ac012008-07-02 10:56:40 +0200262 break;
263
264 case ZFCP_ERP_ACTION_REOPEN_PORT:
265 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
Christof Schmitt615f59e2010-02-17 11:18:56 +0100266 if (!get_device(&port->dev))
Swen Schillig6b1833342009-11-24 16:54:05 +0100267 return NULL;
Christof Schmitt287ac012008-07-02 10:56:40 +0200268 zfcp_erp_action_dismiss_port(port);
Peter Zijlstra805de8f42015-04-24 01:12:32 +0200269 atomic_or(ZFCP_STATUS_COMMON_ERP_INUSE, &port->status);
Christof Schmitt287ac012008-07-02 10:56:40 +0200270 erp_action = &port->erp_action;
Steffen Maierab31fd02017-10-13 15:40:07 +0200271 WARN_ON_ONCE(erp_action->port != port);
272 WARN_ON_ONCE(erp_action->sdev != NULL);
Christof Schmitt287ac012008-07-02 10:56:40 +0200273 if (!(atomic_read(&port->status) & ZFCP_STATUS_COMMON_RUNNING))
Christof Schmittfdbd1c52010-09-08 14:39:54 +0200274 act_status |= ZFCP_STATUS_ERP_CLOSE_ONLY;
Christof Schmitt287ac012008-07-02 10:56:40 +0200275 break;
276
277 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
Swen Schilligf3450c72009-11-24 16:53:59 +0100278 kref_get(&adapter->ref);
Christof Schmitt287ac012008-07-02 10:56:40 +0200279 zfcp_erp_action_dismiss_adapter(adapter);
Peter Zijlstra805de8f42015-04-24 01:12:32 +0200280 atomic_or(ZFCP_STATUS_COMMON_ERP_INUSE, &adapter->status);
Christof Schmitt287ac012008-07-02 10:56:40 +0200281 erp_action = &adapter->erp_action;
Steffen Maierab31fd02017-10-13 15:40:07 +0200282 WARN_ON_ONCE(erp_action->port != NULL);
283 WARN_ON_ONCE(erp_action->sdev != NULL);
Christof Schmitt287ac012008-07-02 10:56:40 +0200284 if (!(atomic_read(&adapter->status) &
285 ZFCP_STATUS_COMMON_RUNNING))
Christof Schmittfdbd1c52010-09-08 14:39:54 +0200286 act_status |= ZFCP_STATUS_ERP_CLOSE_ONLY;
Christof Schmitt287ac012008-07-02 10:56:40 +0200287 break;
288
289 default:
290 return NULL;
291 }
292
Steffen Maierab31fd02017-10-13 15:40:07 +0200293 WARN_ON_ONCE(erp_action->adapter != adapter);
294 memset(&erp_action->list, 0, sizeof(erp_action->list));
295 memset(&erp_action->timer, 0, sizeof(erp_action->timer));
296 erp_action->step = ZFCP_ERP_STEP_UNINITIALIZED;
297 erp_action->fsf_req_id = 0;
Christof Schmitt287ac012008-07-02 10:56:40 +0200298 erp_action->action = need;
Christof Schmittfdbd1c52010-09-08 14:39:54 +0200299 erp_action->status = act_status;
Christof Schmitt287ac012008-07-02 10:56:40 +0200300
301 return erp_action;
302}
303
304static int zfcp_erp_action_enqueue(int want, struct zfcp_adapter *adapter,
305 struct zfcp_port *port,
Christof Schmittb62a8d92010-09-08 14:39:55 +0200306 struct scsi_device *sdev,
Swen Schilligea4a3a62010-12-02 15:16:16 +0100307 char *id, u32 act_status)
Christof Schmitt287ac012008-07-02 10:56:40 +0200308{
309 int retval = 1, need;
Swen Schilligae0904f2010-12-02 15:16:12 +0100310 struct zfcp_erp_action *act;
Christof Schmitt287ac012008-07-02 10:56:40 +0200311
Steffen Maier8c3d20a2018-05-17 19:14:48 +0200312 need = zfcp_erp_handle_failed(want, adapter, port, sdev);
313 if (!need) {
314 need = ZFCP_ERP_ACTION_FAILED; /* marker for trace */
315 goto out;
316 }
317
Steffen Maier6a765502018-05-17 19:14:49 +0200318 if (!adapter->erp_thread) {
319 need = ZFCP_ERP_ACTION_NONE; /* marker for trace */
320 retval = -EIO;
321 goto out;
322 }
Christof Schmitt287ac012008-07-02 10:56:40 +0200323
Christof Schmittb62a8d92010-09-08 14:39:55 +0200324 need = zfcp_erp_required_act(want, adapter, port, sdev);
Christof Schmitt287ac012008-07-02 10:56:40 +0200325 if (!need)
326 goto out;
327
Christof Schmittb62a8d92010-09-08 14:39:55 +0200328 act = zfcp_erp_setup_act(need, act_status, adapter, port, sdev);
Steffen Maier512857a72018-05-17 19:14:45 +0200329 if (!act) {
330 need |= ZFCP_ERP_ACTION_NONE; /* marker for trace */
Christof Schmitt287ac012008-07-02 10:56:40 +0200331 goto out;
Steffen Maier512857a72018-05-17 19:14:45 +0200332 }
Peter Zijlstra805de8f42015-04-24 01:12:32 +0200333 atomic_or(ZFCP_STATUS_ADAPTER_ERP_PENDING, &adapter->status);
Christof Schmitt287ac012008-07-02 10:56:40 +0200334 ++adapter->erp_total_count;
335 list_add_tail(&act->list, &adapter->erp_ready_head);
Christof Schmitt347c6a92009-08-18 15:43:25 +0200336 wake_up(&adapter->erp_ready_wq);
Christof Schmitt287ac012008-07-02 10:56:40 +0200337 retval = 0;
338 out:
Swen Schilligae0904f2010-12-02 15:16:12 +0100339 zfcp_dbf_rec_trig(id, adapter, port, sdev, want, need);
Christof Schmitt287ac012008-07-02 10:56:40 +0200340 return retval;
341}
342
Steffen Maier96d92702018-05-17 19:14:46 +0200343void zfcp_erp_port_forced_no_port_dbf(char *id, struct zfcp_adapter *adapter,
344 u64 port_name, u32 port_id)
345{
346 unsigned long flags;
347 static /* don't waste stack */ struct zfcp_port tmpport;
348
349 write_lock_irqsave(&adapter->erp_lock, flags);
350 /* Stand-in zfcp port with fields just good enough for
351 * zfcp_dbf_rec_trig() and zfcp_dbf_set_common().
352 * Under lock because tmpport is static.
353 */
354 atomic_set(&tmpport.status, -1); /* unknown */
355 tmpport.wwpn = port_name;
356 tmpport.d_id = port_id;
357 zfcp_dbf_rec_trig(id, adapter, &tmpport, NULL,
358 ZFCP_ERP_ACTION_REOPEN_PORT_FORCED,
359 ZFCP_ERP_ACTION_NONE);
360 write_unlock_irqrestore(&adapter->erp_lock, flags);
361}
362
Christof Schmitt287ac012008-07-02 10:56:40 +0200363static int _zfcp_erp_adapter_reopen(struct zfcp_adapter *adapter,
Swen Schilligea4a3a62010-12-02 15:16:16 +0100364 int clear_mask, char *id)
Christof Schmitt287ac012008-07-02 10:56:40 +0200365{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 zfcp_erp_adapter_block(adapter, clear_mask);
Christof Schmitta2fa0ae2009-03-02 13:09:08 +0100367 zfcp_scsi_schedule_rports_block(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
Christof Schmitt287ac012008-07-02 10:56:40 +0200369 return zfcp_erp_action_enqueue(ZFCP_ERP_ACTION_REOPEN_ADAPTER,
Swen Schilligea4a3a62010-12-02 15:16:16 +0100370 adapter, NULL, NULL, id, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371}
372
373/**
Christof Schmitt287ac012008-07-02 10:56:40 +0200374 * zfcp_erp_adapter_reopen - Reopen adapter.
375 * @adapter: Adapter to reopen.
376 * @clear: Status flags to clear.
377 * @id: Id for debug trace event.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 */
Swen Schilligea4a3a62010-12-02 15:16:16 +0100379void zfcp_erp_adapter_reopen(struct zfcp_adapter *adapter, int clear, char *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380{
Christof Schmitt287ac012008-07-02 10:56:40 +0200381 unsigned long flags;
382
Swen Schilligecf0c772009-11-24 16:53:58 +0100383 zfcp_erp_adapter_block(adapter, clear);
384 zfcp_scsi_schedule_rports_block(adapter);
385
386 write_lock_irqsave(&adapter->erp_lock, flags);
Steffen Maier8c3d20a2018-05-17 19:14:48 +0200387 zfcp_erp_action_enqueue(ZFCP_ERP_ACTION_REOPEN_ADAPTER, adapter,
388 NULL, NULL, id, 0);
Swen Schilligecf0c772009-11-24 16:53:58 +0100389 write_unlock_irqrestore(&adapter->erp_lock, flags);
Christof Schmitt287ac012008-07-02 10:56:40 +0200390}
391
392/**
393 * zfcp_erp_adapter_shutdown - Shutdown adapter.
394 * @adapter: Adapter to shut down.
395 * @clear: Status flags to clear.
396 * @id: Id for debug trace event.
Christof Schmitt287ac012008-07-02 10:56:40 +0200397 */
398void zfcp_erp_adapter_shutdown(struct zfcp_adapter *adapter, int clear,
Swen Schilligea4a3a62010-12-02 15:16:16 +0100399 char *id)
Christof Schmitt287ac012008-07-02 10:56:40 +0200400{
401 int flags = ZFCP_STATUS_COMMON_RUNNING | ZFCP_STATUS_COMMON_ERP_FAILED;
Swen Schilligea4a3a62010-12-02 15:16:16 +0100402 zfcp_erp_adapter_reopen(adapter, clear | flags, id);
Christof Schmitt287ac012008-07-02 10:56:40 +0200403}
404
405/**
406 * zfcp_erp_port_shutdown - Shutdown port
407 * @port: Port to shut down.
408 * @clear: Status flags to clear.
409 * @id: Id for debug trace event.
Christof Schmitt287ac012008-07-02 10:56:40 +0200410 */
Swen Schilligea4a3a62010-12-02 15:16:16 +0100411void zfcp_erp_port_shutdown(struct zfcp_port *port, int clear, char *id)
Christof Schmitt287ac012008-07-02 10:56:40 +0200412{
413 int flags = ZFCP_STATUS_COMMON_RUNNING | ZFCP_STATUS_COMMON_ERP_FAILED;
Swen Schilligea4a3a62010-12-02 15:16:16 +0100414 zfcp_erp_port_reopen(port, clear | flags, id);
Christof Schmitt287ac012008-07-02 10:56:40 +0200415}
416
Christof Schmitt287ac012008-07-02 10:56:40 +0200417static void zfcp_erp_port_block(struct zfcp_port *port, int clear)
418{
Swen Schilligedaed852010-09-08 14:40:01 +0200419 zfcp_erp_clear_port_status(port,
420 ZFCP_STATUS_COMMON_UNBLOCKED | clear);
Christof Schmitt287ac012008-07-02 10:56:40 +0200421}
422
Swen Schilligea4a3a62010-12-02 15:16:16 +0100423static void _zfcp_erp_port_forced_reopen(struct zfcp_port *port, int clear,
424 char *id)
Christof Schmitt287ac012008-07-02 10:56:40 +0200425{
426 zfcp_erp_port_block(port, clear);
Christof Schmitta2fa0ae2009-03-02 13:09:08 +0100427 zfcp_scsi_schedule_rport_block(port);
Christof Schmitt287ac012008-07-02 10:56:40 +0200428
Christof Schmitt287ac012008-07-02 10:56:40 +0200429 zfcp_erp_action_enqueue(ZFCP_ERP_ACTION_REOPEN_PORT_FORCED,
Swen Schilligea4a3a62010-12-02 15:16:16 +0100430 port->adapter, port, NULL, id, 0);
Christof Schmitt287ac012008-07-02 10:56:40 +0200431}
432
433/**
434 * zfcp_erp_port_forced_reopen - Forced close of port and open again
435 * @port: Port to force close and to reopen.
Swen Schilligea4a3a62010-12-02 15:16:16 +0100436 * @clear: Status flags to clear.
Christof Schmitt287ac012008-07-02 10:56:40 +0200437 * @id: Id for debug trace event.
Christof Schmitt287ac012008-07-02 10:56:40 +0200438 */
Swen Schilligea4a3a62010-12-02 15:16:16 +0100439void zfcp_erp_port_forced_reopen(struct zfcp_port *port, int clear, char *id)
Christof Schmitt287ac012008-07-02 10:56:40 +0200440{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 unsigned long flags;
442 struct zfcp_adapter *adapter = port->adapter;
443
Swen Schilligecf0c772009-11-24 16:53:58 +0100444 write_lock_irqsave(&adapter->erp_lock, flags);
Swen Schilligea4a3a62010-12-02 15:16:16 +0100445 _zfcp_erp_port_forced_reopen(port, clear, id);
Swen Schilligecf0c772009-11-24 16:53:58 +0100446 write_unlock_irqrestore(&adapter->erp_lock, flags);
Christof Schmitt287ac012008-07-02 10:56:40 +0200447}
448
Swen Schilligea4a3a62010-12-02 15:16:16 +0100449static int _zfcp_erp_port_reopen(struct zfcp_port *port, int clear, char *id)
Christof Schmitt287ac012008-07-02 10:56:40 +0200450{
451 zfcp_erp_port_block(port, clear);
Christof Schmitta2fa0ae2009-03-02 13:09:08 +0100452 zfcp_scsi_schedule_rport_block(port);
Christof Schmitt287ac012008-07-02 10:56:40 +0200453
Christof Schmitt287ac012008-07-02 10:56:40 +0200454 return zfcp_erp_action_enqueue(ZFCP_ERP_ACTION_REOPEN_PORT,
Swen Schilligea4a3a62010-12-02 15:16:16 +0100455 port->adapter, port, NULL, id, 0);
Christof Schmitt287ac012008-07-02 10:56:40 +0200456}
457
458/**
459 * zfcp_erp_port_reopen - trigger remote port recovery
460 * @port: port to recover
461 * @clear_mask: flags in port status to be cleared
Swen Schilligea4a3a62010-12-02 15:16:16 +0100462 * @id: Id for debug trace event.
Christof Schmitt287ac012008-07-02 10:56:40 +0200463 *
464 * Returns 0 if recovery has been triggered, < 0 if not.
465 */
Swen Schilligea4a3a62010-12-02 15:16:16 +0100466int zfcp_erp_port_reopen(struct zfcp_port *port, int clear, char *id)
Christof Schmitt287ac012008-07-02 10:56:40 +0200467{
Christof Schmitt287ac012008-07-02 10:56:40 +0200468 int retval;
Swen Schilligecf0c772009-11-24 16:53:58 +0100469 unsigned long flags;
Christof Schmitt287ac012008-07-02 10:56:40 +0200470 struct zfcp_adapter *adapter = port->adapter;
471
Swen Schilligecf0c772009-11-24 16:53:58 +0100472 write_lock_irqsave(&adapter->erp_lock, flags);
Swen Schilligea4a3a62010-12-02 15:16:16 +0100473 retval = _zfcp_erp_port_reopen(port, clear, id);
Swen Schilligecf0c772009-11-24 16:53:58 +0100474 write_unlock_irqrestore(&adapter->erp_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
476 return retval;
477}
478
Christof Schmittb62a8d92010-09-08 14:39:55 +0200479static void zfcp_erp_lun_block(struct scsi_device *sdev, int clear_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480{
Swen Schilligedaed852010-09-08 14:40:01 +0200481 zfcp_erp_clear_lun_status(sdev,
482 ZFCP_STATUS_COMMON_UNBLOCKED | clear_mask);
Christof Schmitt287ac012008-07-02 10:56:40 +0200483}
484
Christof Schmittb62a8d92010-09-08 14:39:55 +0200485static void _zfcp_erp_lun_reopen(struct scsi_device *sdev, int clear, char *id,
Swen Schilligea4a3a62010-12-02 15:16:16 +0100486 u32 act_status)
Christof Schmitt287ac012008-07-02 10:56:40 +0200487{
Christof Schmittb62a8d92010-09-08 14:39:55 +0200488 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
489 struct zfcp_adapter *adapter = zfcp_sdev->port->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
Christof Schmittb62a8d92010-09-08 14:39:55 +0200491 zfcp_erp_lun_block(sdev, clear);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
Christof Schmittb62a8d92010-09-08 14:39:55 +0200493 zfcp_erp_action_enqueue(ZFCP_ERP_ACTION_REOPEN_LUN, adapter,
Swen Schilligea4a3a62010-12-02 15:16:16 +0100494 zfcp_sdev->port, sdev, id, act_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495}
496
497/**
Christof Schmittb62a8d92010-09-08 14:39:55 +0200498 * zfcp_erp_lun_reopen - initiate reopen of a LUN
499 * @sdev: SCSI device / LUN to be reopened
500 * @clear_mask: specifies flags in LUN status to be cleared
Swen Schilligea4a3a62010-12-02 15:16:16 +0100501 * @id: Id for debug trace event.
502 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 * Return: 0 on success, < 0 on error
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 */
Swen Schilligea4a3a62010-12-02 15:16:16 +0100505void zfcp_erp_lun_reopen(struct scsi_device *sdev, int clear, char *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 unsigned long flags;
Christof Schmittb62a8d92010-09-08 14:39:55 +0200508 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
509 struct zfcp_port *port = zfcp_sdev->port;
Christof Schmitt287ac012008-07-02 10:56:40 +0200510 struct zfcp_adapter *adapter = port->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
Swen Schilligecf0c772009-11-24 16:53:58 +0100512 write_lock_irqsave(&adapter->erp_lock, flags);
Swen Schilligea4a3a62010-12-02 15:16:16 +0100513 _zfcp_erp_lun_reopen(sdev, clear, id, 0);
Swen Schilligecf0c772009-11-24 16:53:58 +0100514 write_unlock_irqrestore(&adapter->erp_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515}
516
Christof Schmittfdbd1c52010-09-08 14:39:54 +0200517/**
Christof Schmittb62a8d92010-09-08 14:39:55 +0200518 * zfcp_erp_lun_shutdown - Shutdown LUN
519 * @sdev: SCSI device / LUN to shut down.
Christof Schmittfdbd1c52010-09-08 14:39:54 +0200520 * @clear: Status flags to clear.
521 * @id: Id for debug trace event.
Christof Schmittfdbd1c52010-09-08 14:39:54 +0200522 */
Swen Schilligea4a3a62010-12-02 15:16:16 +0100523void zfcp_erp_lun_shutdown(struct scsi_device *sdev, int clear, char *id)
Christof Schmittfdbd1c52010-09-08 14:39:54 +0200524{
525 int flags = ZFCP_STATUS_COMMON_RUNNING | ZFCP_STATUS_COMMON_ERP_FAILED;
Swen Schilligea4a3a62010-12-02 15:16:16 +0100526 zfcp_erp_lun_reopen(sdev, clear | flags, id);
Christof Schmittfdbd1c52010-09-08 14:39:54 +0200527}
528
529/**
Christof Schmittb62a8d92010-09-08 14:39:55 +0200530 * zfcp_erp_lun_shutdown_wait - Shutdown LUN and wait for erp completion
531 * @sdev: SCSI device / LUN to shut down.
Christof Schmittfdbd1c52010-09-08 14:39:54 +0200532 * @id: Id for debug trace event.
533 *
Christof Schmittb62a8d92010-09-08 14:39:55 +0200534 * Do not acquire a reference for the LUN when creating the ERP
Christof Schmittfdbd1c52010-09-08 14:39:54 +0200535 * action. It is safe, because this function waits for the ERP to
Christof Schmittb62a8d92010-09-08 14:39:55 +0200536 * complete first. This allows to shutdown the LUN, even when the SCSI
537 * device is in the state SDEV_DEL when scsi_device_get will fail.
Christof Schmittfdbd1c52010-09-08 14:39:54 +0200538 */
Christof Schmittb62a8d92010-09-08 14:39:55 +0200539void zfcp_erp_lun_shutdown_wait(struct scsi_device *sdev, char *id)
Christof Schmittfdbd1c52010-09-08 14:39:54 +0200540{
541 unsigned long flags;
Christof Schmittb62a8d92010-09-08 14:39:55 +0200542 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
543 struct zfcp_port *port = zfcp_sdev->port;
Christof Schmittfdbd1c52010-09-08 14:39:54 +0200544 struct zfcp_adapter *adapter = port->adapter;
545 int clear = ZFCP_STATUS_COMMON_RUNNING | ZFCP_STATUS_COMMON_ERP_FAILED;
546
547 write_lock_irqsave(&adapter->erp_lock, flags);
Swen Schilligea4a3a62010-12-02 15:16:16 +0100548 _zfcp_erp_lun_reopen(sdev, clear, id, ZFCP_STATUS_ERP_NO_REF);
Christof Schmittfdbd1c52010-09-08 14:39:54 +0200549 write_unlock_irqrestore(&adapter->erp_lock, flags);
550
551 zfcp_erp_wait(adapter);
552}
553
Christof Schmitt287ac012008-07-02 10:56:40 +0200554static int status_change_set(unsigned long mask, atomic_t *status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555{
Christof Schmitt287ac012008-07-02 10:56:40 +0200556 return (atomic_read(status) ^ mask) & mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557}
558
Andreas Herrmannf6c0e7a2006-08-02 11:05:52 +0200559static void zfcp_erp_adapter_unblock(struct zfcp_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560{
Christof Schmitt287ac012008-07-02 10:56:40 +0200561 if (status_change_set(ZFCP_STATUS_COMMON_UNBLOCKED, &adapter->status))
Swen Schilligae0904f2010-12-02 15:16:12 +0100562 zfcp_dbf_rec_run("eraubl1", &adapter->erp_action);
Peter Zijlstra805de8f42015-04-24 01:12:32 +0200563 atomic_or(ZFCP_STATUS_COMMON_UNBLOCKED, &adapter->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564}
565
Christof Schmitt287ac012008-07-02 10:56:40 +0200566static void zfcp_erp_port_unblock(struct zfcp_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567{
Christof Schmitt287ac012008-07-02 10:56:40 +0200568 if (status_change_set(ZFCP_STATUS_COMMON_UNBLOCKED, &port->status))
Swen Schilligae0904f2010-12-02 15:16:12 +0100569 zfcp_dbf_rec_run("erpubl1", &port->erp_action);
Peter Zijlstra805de8f42015-04-24 01:12:32 +0200570 atomic_or(ZFCP_STATUS_COMMON_UNBLOCKED, &port->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571}
572
Christof Schmittb62a8d92010-09-08 14:39:55 +0200573static void zfcp_erp_lun_unblock(struct scsi_device *sdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574{
Christof Schmittb62a8d92010-09-08 14:39:55 +0200575 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
576
577 if (status_change_set(ZFCP_STATUS_COMMON_UNBLOCKED, &zfcp_sdev->status))
Swen Schilligae0904f2010-12-02 15:16:12 +0100578 zfcp_dbf_rec_run("erlubl1", &sdev_to_zfcp(sdev)->erp_action);
Peter Zijlstra805de8f42015-04-24 01:12:32 +0200579 atomic_or(ZFCP_STATUS_COMMON_UNBLOCKED, &zfcp_sdev->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580}
581
Christof Schmitt287ac012008-07-02 10:56:40 +0200582static void zfcp_erp_action_to_running(struct zfcp_erp_action *erp_action)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583{
Christof Schmitt287ac012008-07-02 10:56:40 +0200584 list_move(&erp_action->list, &erp_action->adapter->erp_running_head);
Swen Schilligae0904f2010-12-02 15:16:12 +0100585 zfcp_dbf_rec_run("erator1", erp_action);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586}
587
Christof Schmitt287ac012008-07-02 10:56:40 +0200588static void zfcp_erp_strategy_check_fsfreq(struct zfcp_erp_action *act)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589{
Christof Schmitt287ac012008-07-02 10:56:40 +0200590 struct zfcp_adapter *adapter = act->adapter;
Christof Schmitte60a6d62010-02-17 11:18:49 +0100591 struct zfcp_fsf_req *req;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
Christof Schmitte60a6d62010-02-17 11:18:49 +0100593 if (!act->fsf_req_id)
Christof Schmitt287ac012008-07-02 10:56:40 +0200594 return;
595
Christof Schmittb6bd2fb2010-02-17 11:18:50 +0100596 spin_lock(&adapter->req_list->lock);
597 req = _zfcp_reqlist_find(adapter->req_list, act->fsf_req_id);
Christof Schmitte60a6d62010-02-17 11:18:49 +0100598 if (req && req->erp_action == act) {
Christof Schmitt287ac012008-07-02 10:56:40 +0200599 if (act->status & (ZFCP_STATUS_ERP_DISMISSED |
600 ZFCP_STATUS_ERP_TIMEDOUT)) {
Christof Schmitte60a6d62010-02-17 11:18:49 +0100601 req->status |= ZFCP_STATUS_FSFREQ_DISMISSED;
Swen Schilligae0904f2010-12-02 15:16:12 +0100602 zfcp_dbf_rec_run("erscf_1", act);
Christof Schmitte60a6d62010-02-17 11:18:49 +0100603 req->erp_action = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 }
Christof Schmitt287ac012008-07-02 10:56:40 +0200605 if (act->status & ZFCP_STATUS_ERP_TIMEDOUT)
Swen Schilligae0904f2010-12-02 15:16:12 +0100606 zfcp_dbf_rec_run("erscf_2", act);
Christof Schmitte60a6d62010-02-17 11:18:49 +0100607 if (req->status & ZFCP_STATUS_FSFREQ_DISMISSED)
608 act->fsf_req_id = 0;
Christof Schmitt287ac012008-07-02 10:56:40 +0200609 } else
Christof Schmitte60a6d62010-02-17 11:18:49 +0100610 act->fsf_req_id = 0;
Christof Schmittb6bd2fb2010-02-17 11:18:50 +0100611 spin_unlock(&adapter->req_list->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612}
613
Andreas Herrmannf6c0e7a2006-08-02 11:05:52 +0200614/**
Christof Schmitt287ac012008-07-02 10:56:40 +0200615 * zfcp_erp_notify - Trigger ERP action.
616 * @erp_action: ERP action to continue.
617 * @set_mask: ERP action status flags to set.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 */
Christof Schmitt287ac012008-07-02 10:56:40 +0200619void zfcp_erp_notify(struct zfcp_erp_action *erp_action, unsigned long set_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620{
621 struct zfcp_adapter *adapter = erp_action->adapter;
622 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
624 write_lock_irqsave(&adapter->erp_lock, flags);
Christof Schmitt287ac012008-07-02 10:56:40 +0200625 if (zfcp_erp_action_exists(erp_action) == ZFCP_ERP_ACTION_RUNNING) {
626 erp_action->status |= set_mask;
627 zfcp_erp_action_ready(erp_action);
628 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 write_unlock_irqrestore(&adapter->erp_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630}
631
Andreas Herrmannf6c0e7a2006-08-02 11:05:52 +0200632/**
Christof Schmitt287ac012008-07-02 10:56:40 +0200633 * zfcp_erp_timeout_handler - Trigger ERP action from timed out ERP request
634 * @data: ERP action (from timer data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 */
Kees Cook75492a52017-10-16 16:44:34 -0700636void zfcp_erp_timeout_handler(struct timer_list *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637{
Kees Cook75492a52017-10-16 16:44:34 -0700638 struct zfcp_fsf_req *fsf_req = from_timer(fsf_req, t, timer);
639 struct zfcp_erp_action *act = fsf_req->erp_action;
Steffen Maier5c13db92017-10-17 18:40:51 +0200640
Christof Schmitt287ac012008-07-02 10:56:40 +0200641 zfcp_erp_notify(act, ZFCP_STATUS_ERP_TIMEDOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642}
643
Kees Cook75492a52017-10-16 16:44:34 -0700644static void zfcp_erp_memwait_handler(struct timer_list *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645{
Kees Cook75492a52017-10-16 16:44:34 -0700646 struct zfcp_erp_action *act = from_timer(act, t, timer);
647
648 zfcp_erp_notify(act, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649}
650
Christof Schmitt287ac012008-07-02 10:56:40 +0200651static void zfcp_erp_strategy_memwait(struct zfcp_erp_action *erp_action)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652{
Steffen Maier5c13db92017-10-17 18:40:51 +0200653 timer_setup(&erp_action->timer, zfcp_erp_memwait_handler, 0);
Christof Schmitt287ac012008-07-02 10:56:40 +0200654 erp_action->timer.expires = jiffies + HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 add_timer(&erp_action->timer);
Christof Schmitt287ac012008-07-02 10:56:40 +0200656}
657
658static void _zfcp_erp_port_reopen_all(struct zfcp_adapter *adapter,
Swen Schilligea4a3a62010-12-02 15:16:16 +0100659 int clear, char *id)
Christof Schmitt287ac012008-07-02 10:56:40 +0200660{
661 struct zfcp_port *port;
662
Swen Schilligecf0c772009-11-24 16:53:58 +0100663 read_lock(&adapter->port_list_lock);
664 list_for_each_entry(port, &adapter->port_list, list)
Swen Schilligea4a3a62010-12-02 15:16:16 +0100665 _zfcp_erp_port_reopen(port, clear, id);
Swen Schilligecf0c772009-11-24 16:53:58 +0100666 read_unlock(&adapter->port_list_lock);
Christof Schmitt287ac012008-07-02 10:56:40 +0200667}
668
Christof Schmittb62a8d92010-09-08 14:39:55 +0200669static void _zfcp_erp_lun_reopen_all(struct zfcp_port *port, int clear,
Swen Schilligea4a3a62010-12-02 15:16:16 +0100670 char *id)
Christof Schmitt287ac012008-07-02 10:56:40 +0200671{
Christof Schmittb62a8d92010-09-08 14:39:55 +0200672 struct scsi_device *sdev;
Christof Schmitt287ac012008-07-02 10:56:40 +0200673
Martin Peschke924dd582013-08-22 17:45:37 +0200674 spin_lock(port->adapter->scsi_host->host_lock);
675 __shost_for_each_device(sdev, port->adapter->scsi_host)
Christof Schmittb62a8d92010-09-08 14:39:55 +0200676 if (sdev_to_zfcp(sdev)->port == port)
Swen Schilligea4a3a62010-12-02 15:16:16 +0100677 _zfcp_erp_lun_reopen(sdev, clear, id, 0);
Martin Peschke924dd582013-08-22 17:45:37 +0200678 spin_unlock(port->adapter->scsi_host->host_lock);
Christof Schmitt287ac012008-07-02 10:56:40 +0200679}
680
Christof Schmitt85600f72009-07-13 15:06:09 +0200681static void zfcp_erp_strategy_followup_failed(struct zfcp_erp_action *act)
Christof Schmitt287ac012008-07-02 10:56:40 +0200682{
Christof Schmitt287ac012008-07-02 10:56:40 +0200683 switch (act->action) {
Christof Schmitt287ac012008-07-02 10:56:40 +0200684 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
Swen Schilligea4a3a62010-12-02 15:16:16 +0100685 _zfcp_erp_adapter_reopen(act->adapter, 0, "ersff_1");
Christof Schmitt287ac012008-07-02 10:56:40 +0200686 break;
Christof Schmitt287ac012008-07-02 10:56:40 +0200687 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
Swen Schilligea4a3a62010-12-02 15:16:16 +0100688 _zfcp_erp_port_forced_reopen(act->port, 0, "ersff_2");
Christof Schmitt287ac012008-07-02 10:56:40 +0200689 break;
Christof Schmitt287ac012008-07-02 10:56:40 +0200690 case ZFCP_ERP_ACTION_REOPEN_PORT:
Swen Schilligea4a3a62010-12-02 15:16:16 +0100691 _zfcp_erp_port_reopen(act->port, 0, "ersff_3");
Christof Schmitt287ac012008-07-02 10:56:40 +0200692 break;
Christof Schmittb62a8d92010-09-08 14:39:55 +0200693 case ZFCP_ERP_ACTION_REOPEN_LUN:
Swen Schilligea4a3a62010-12-02 15:16:16 +0100694 _zfcp_erp_lun_reopen(act->sdev, 0, "ersff_4", 0);
Christof Schmitt85600f72009-07-13 15:06:09 +0200695 break;
696 }
697}
698
699static void zfcp_erp_strategy_followup_success(struct zfcp_erp_action *act)
700{
701 switch (act->action) {
702 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
Swen Schilligea4a3a62010-12-02 15:16:16 +0100703 _zfcp_erp_port_reopen_all(act->adapter, 0, "ersfs_1");
Christof Schmitt85600f72009-07-13 15:06:09 +0200704 break;
705 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
Swen Schilligea4a3a62010-12-02 15:16:16 +0100706 _zfcp_erp_port_reopen(act->port, 0, "ersfs_2");
Christof Schmitt85600f72009-07-13 15:06:09 +0200707 break;
708 case ZFCP_ERP_ACTION_REOPEN_PORT:
Swen Schilligea4a3a62010-12-02 15:16:16 +0100709 _zfcp_erp_lun_reopen_all(act->port, 0, "ersfs_3");
Christof Schmitt287ac012008-07-02 10:56:40 +0200710 break;
711 }
712}
713
714static void zfcp_erp_wakeup(struct zfcp_adapter *adapter)
715{
716 unsigned long flags;
717
Swen Schilligecf0c772009-11-24 16:53:58 +0100718 read_lock_irqsave(&adapter->erp_lock, flags);
Christof Schmitt287ac012008-07-02 10:56:40 +0200719 if (list_empty(&adapter->erp_ready_head) &&
720 list_empty(&adapter->erp_running_head)) {
Peter Zijlstra805de8f42015-04-24 01:12:32 +0200721 atomic_andnot(ZFCP_STATUS_ADAPTER_ERP_PENDING,
Christof Schmitt287ac012008-07-02 10:56:40 +0200722 &adapter->status);
723 wake_up(&adapter->erp_done_wqh);
724 }
Swen Schilligecf0c772009-11-24 16:53:58 +0100725 read_unlock_irqrestore(&adapter->erp_lock, flags);
Christof Schmitt287ac012008-07-02 10:56:40 +0200726}
727
Christof Schmitt287ac012008-07-02 10:56:40 +0200728static void zfcp_erp_enqueue_ptp_port(struct zfcp_adapter *adapter)
729{
730 struct zfcp_port *port;
731 port = zfcp_port_enqueue(adapter, adapter->peer_wwpn, 0,
732 adapter->peer_d_id);
733 if (IS_ERR(port)) /* error or port already attached */
734 return;
Swen Schilligea4a3a62010-12-02 15:16:16 +0100735 _zfcp_erp_port_reopen(port, 0, "ereptp1");
Christof Schmitt287ac012008-07-02 10:56:40 +0200736}
737
738static int zfcp_erp_adapter_strat_fsf_xconf(struct zfcp_erp_action *erp_action)
739{
740 int retries;
741 int sleep = 1;
742 struct zfcp_adapter *adapter = erp_action->adapter;
743
Peter Zijlstra805de8f42015-04-24 01:12:32 +0200744 atomic_andnot(ZFCP_STATUS_ADAPTER_XCONFIG_OK, &adapter->status);
Christof Schmitt287ac012008-07-02 10:56:40 +0200745
746 for (retries = 7; retries; retries--) {
Peter Zijlstra805de8f42015-04-24 01:12:32 +0200747 atomic_andnot(ZFCP_STATUS_ADAPTER_HOST_CON_INIT,
Christof Schmitt287ac012008-07-02 10:56:40 +0200748 &adapter->status);
749 write_lock_irq(&adapter->erp_lock);
750 zfcp_erp_action_to_running(erp_action);
751 write_unlock_irq(&adapter->erp_lock);
752 if (zfcp_fsf_exchange_config_data(erp_action)) {
Peter Zijlstra805de8f42015-04-24 01:12:32 +0200753 atomic_andnot(ZFCP_STATUS_ADAPTER_HOST_CON_INIT,
Christof Schmitt287ac012008-07-02 10:56:40 +0200754 &adapter->status);
755 return ZFCP_ERP_FAILED;
756 }
757
Christof Schmitt347c6a92009-08-18 15:43:25 +0200758 wait_event(adapter->erp_ready_wq,
759 !list_empty(&adapter->erp_ready_head));
Christof Schmitt287ac012008-07-02 10:56:40 +0200760 if (erp_action->status & ZFCP_STATUS_ERP_TIMEDOUT)
761 break;
762
763 if (!(atomic_read(&adapter->status) &
764 ZFCP_STATUS_ADAPTER_HOST_CON_INIT))
765 break;
766
767 ssleep(sleep);
768 sleep *= 2;
769 }
770
Peter Zijlstra805de8f42015-04-24 01:12:32 +0200771 atomic_andnot(ZFCP_STATUS_ADAPTER_HOST_CON_INIT,
Christof Schmitt287ac012008-07-02 10:56:40 +0200772 &adapter->status);
773
774 if (!(atomic_read(&adapter->status) & ZFCP_STATUS_ADAPTER_XCONFIG_OK))
775 return ZFCP_ERP_FAILED;
776
777 if (fc_host_port_type(adapter->scsi_host) == FC_PORTTYPE_PTP)
778 zfcp_erp_enqueue_ptp_port(adapter);
779
780 return ZFCP_ERP_SUCCEEDED;
781}
782
783static int zfcp_erp_adapter_strategy_open_fsf_xport(struct zfcp_erp_action *act)
784{
785 int ret;
786 struct zfcp_adapter *adapter = act->adapter;
787
Christof Schmitt287ac012008-07-02 10:56:40 +0200788 write_lock_irq(&adapter->erp_lock);
789 zfcp_erp_action_to_running(act);
790 write_unlock_irq(&adapter->erp_lock);
791
792 ret = zfcp_fsf_exchange_port_data(act);
793 if (ret == -EOPNOTSUPP)
794 return ZFCP_ERP_SUCCEEDED;
795 if (ret)
796 return ZFCP_ERP_FAILED;
797
Swen Schilligae0904f2010-12-02 15:16:12 +0100798 zfcp_dbf_rec_run("erasox1", act);
Christof Schmitt347c6a92009-08-18 15:43:25 +0200799 wait_event(adapter->erp_ready_wq,
800 !list_empty(&adapter->erp_ready_head));
Swen Schilligae0904f2010-12-02 15:16:12 +0100801 zfcp_dbf_rec_run("erasox2", act);
Christof Schmitt287ac012008-07-02 10:56:40 +0200802 if (act->status & ZFCP_STATUS_ERP_TIMEDOUT)
803 return ZFCP_ERP_FAILED;
804
805 return ZFCP_ERP_SUCCEEDED;
806}
807
808static int zfcp_erp_adapter_strategy_open_fsf(struct zfcp_erp_action *act)
809{
810 if (zfcp_erp_adapter_strat_fsf_xconf(act) == ZFCP_ERP_FAILED)
811 return ZFCP_ERP_FAILED;
812
813 if (zfcp_erp_adapter_strategy_open_fsf_xport(act) == ZFCP_ERP_FAILED)
814 return ZFCP_ERP_FAILED;
815
Christof Schmittc7b279a2011-02-22 19:54:40 +0100816 if (mempool_resize(act->adapter->pool.sr_data,
David Rientjes11d83362015-04-14 15:48:21 -0700817 act->adapter->stat_read_buf_num))
Christof Schmitt8d88cf32010-06-21 10:11:33 +0200818 return ZFCP_ERP_FAILED;
819
820 if (mempool_resize(act->adapter->pool.status_read_req,
David Rientjes11d83362015-04-14 15:48:21 -0700821 act->adapter->stat_read_buf_num))
Christof Schmitt8d88cf32010-06-21 10:11:33 +0200822 return ZFCP_ERP_FAILED;
823
Christof Schmitt64deb6e2010-04-30 18:09:36 +0200824 atomic_set(&act->adapter->stat_miss, act->adapter->stat_read_buf_num);
Christof Schmitt287ac012008-07-02 10:56:40 +0200825 if (zfcp_status_read_refill(act->adapter))
826 return ZFCP_ERP_FAILED;
827
828 return ZFCP_ERP_SUCCEEDED;
829}
830
Swen Schilligcf13c082009-03-02 13:09:03 +0100831static void zfcp_erp_adapter_strategy_close(struct zfcp_erp_action *act)
Christof Schmitt287ac012008-07-02 10:56:40 +0200832{
Christof Schmitt287ac012008-07-02 10:56:40 +0200833 struct zfcp_adapter *adapter = act->adapter;
834
Christof Schmitt287ac012008-07-02 10:56:40 +0200835 /* close queues to ensure that buffers are not accessed by adapter */
Swen Schillig564e1c82009-08-18 15:43:19 +0200836 zfcp_qdio_close(adapter->qdio);
Christof Schmitt287ac012008-07-02 10:56:40 +0200837 zfcp_fsf_req_dismiss_all(adapter);
838 adapter->fsf_req_seq_no = 0;
Christof Schmitt55c770f2009-08-18 15:43:12 +0200839 zfcp_fc_wka_ports_force_offline(adapter->gs);
Christof Schmittb62a8d92010-09-08 14:39:55 +0200840 /* all ports and LUNs are closed */
Swen Schilligedaed852010-09-08 14:40:01 +0200841 zfcp_erp_clear_adapter_status(adapter, ZFCP_STATUS_COMMON_OPEN);
Swen Schilligcf13c082009-03-02 13:09:03 +0100842
Peter Zijlstra805de8f42015-04-24 01:12:32 +0200843 atomic_andnot(ZFCP_STATUS_ADAPTER_XCONFIG_OK |
Swen Schilligcf13c082009-03-02 13:09:03 +0100844 ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED, &adapter->status);
845}
846
847static int zfcp_erp_adapter_strategy_open(struct zfcp_erp_action *act)
848{
849 struct zfcp_adapter *adapter = act->adapter;
850
Christof Schmitt3d63d3b2010-12-02 15:16:17 +0100851 if (zfcp_qdio_open(adapter->qdio)) {
Peter Zijlstra805de8f42015-04-24 01:12:32 +0200852 atomic_andnot(ZFCP_STATUS_ADAPTER_XCONFIG_OK |
Swen Schilligcf13c082009-03-02 13:09:03 +0100853 ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED,
854 &adapter->status);
855 return ZFCP_ERP_FAILED;
856 }
857
858 if (zfcp_erp_adapter_strategy_open_fsf(act)) {
859 zfcp_erp_adapter_strategy_close(act);
860 return ZFCP_ERP_FAILED;
861 }
862
Peter Zijlstra805de8f42015-04-24 01:12:32 +0200863 atomic_or(ZFCP_STATUS_COMMON_OPEN, &adapter->status);
Swen Schilligcf13c082009-03-02 13:09:03 +0100864
865 return ZFCP_ERP_SUCCEEDED;
Christof Schmitt287ac012008-07-02 10:56:40 +0200866}
867
868static int zfcp_erp_adapter_strategy(struct zfcp_erp_action *act)
869{
Swen Schilligcf13c082009-03-02 13:09:03 +0100870 struct zfcp_adapter *adapter = act->adapter;
Christof Schmitt287ac012008-07-02 10:56:40 +0200871
Swen Schilligcf13c082009-03-02 13:09:03 +0100872 if (atomic_read(&adapter->status) & ZFCP_STATUS_COMMON_OPEN) {
873 zfcp_erp_adapter_strategy_close(act);
874 if (act->status & ZFCP_STATUS_ERP_CLOSE_ONLY)
875 return ZFCP_ERP_EXIT;
876 }
Christof Schmitt287ac012008-07-02 10:56:40 +0200877
Swen Schilligcf13c082009-03-02 13:09:03 +0100878 if (zfcp_erp_adapter_strategy_open(act)) {
Christof Schmitt287ac012008-07-02 10:56:40 +0200879 ssleep(8);
Swen Schilligcf13c082009-03-02 13:09:03 +0100880 return ZFCP_ERP_FAILED;
881 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882
Swen Schilligcf13c082009-03-02 13:09:03 +0100883 return ZFCP_ERP_SUCCEEDED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884}
885
Christof Schmitt287ac012008-07-02 10:56:40 +0200886static int zfcp_erp_port_forced_strategy_close(struct zfcp_erp_action *act)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887{
Christof Schmitt287ac012008-07-02 10:56:40 +0200888 int retval;
889
890 retval = zfcp_fsf_close_physical_port(act);
891 if (retval == -ENOMEM)
892 return ZFCP_ERP_NOMEM;
893 act->step = ZFCP_ERP_STEP_PHYS_PORT_CLOSING;
894 if (retval)
895 return ZFCP_ERP_FAILED;
896
897 return ZFCP_ERP_CONTINUES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898}
899
Christof Schmitt287ac012008-07-02 10:56:40 +0200900static int zfcp_erp_port_forced_strategy(struct zfcp_erp_action *erp_action)
901{
902 struct zfcp_port *port = erp_action->port;
903 int status = atomic_read(&port->status);
904
905 switch (erp_action->step) {
906 case ZFCP_ERP_STEP_UNINITIALIZED:
Christof Schmitt287ac012008-07-02 10:56:40 +0200907 if ((status & ZFCP_STATUS_PORT_PHYS_OPEN) &&
908 (status & ZFCP_STATUS_COMMON_OPEN))
909 return zfcp_erp_port_forced_strategy_close(erp_action);
910 else
911 return ZFCP_ERP_FAILED;
912
913 case ZFCP_ERP_STEP_PHYS_PORT_CLOSING:
Christof Schmittddb3e0c2009-07-13 15:06:08 +0200914 if (!(status & ZFCP_STATUS_PORT_PHYS_OPEN))
Christof Schmitt287ac012008-07-02 10:56:40 +0200915 return ZFCP_ERP_SUCCEEDED;
916 }
917 return ZFCP_ERP_FAILED;
918}
919
920static int zfcp_erp_port_strategy_close(struct zfcp_erp_action *erp_action)
921{
922 int retval;
923
924 retval = zfcp_fsf_close_port(erp_action);
925 if (retval == -ENOMEM)
926 return ZFCP_ERP_NOMEM;
927 erp_action->step = ZFCP_ERP_STEP_PORT_CLOSING;
928 if (retval)
929 return ZFCP_ERP_FAILED;
930 return ZFCP_ERP_CONTINUES;
931}
932
933static int zfcp_erp_port_strategy_open_port(struct zfcp_erp_action *erp_action)
934{
935 int retval;
936
937 retval = zfcp_fsf_open_port(erp_action);
938 if (retval == -ENOMEM)
939 return ZFCP_ERP_NOMEM;
940 erp_action->step = ZFCP_ERP_STEP_PORT_OPENING;
941 if (retval)
942 return ZFCP_ERP_FAILED;
943 return ZFCP_ERP_CONTINUES;
944}
945
Christof Schmitt287ac012008-07-02 10:56:40 +0200946static int zfcp_erp_open_ptp_port(struct zfcp_erp_action *act)
947{
948 struct zfcp_adapter *adapter = act->adapter;
949 struct zfcp_port *port = act->port;
950
951 if (port->wwpn != adapter->peer_wwpn) {
Swen Schilligedaed852010-09-08 14:40:01 +0200952 zfcp_erp_set_port_status(port, ZFCP_STATUS_COMMON_ERP_FAILED);
Christof Schmitt287ac012008-07-02 10:56:40 +0200953 return ZFCP_ERP_FAILED;
954 }
955 port->d_id = adapter->peer_d_id;
Christof Schmitt287ac012008-07-02 10:56:40 +0200956 return zfcp_erp_port_strategy_open_port(act);
957}
958
959static int zfcp_erp_port_strategy_open_common(struct zfcp_erp_action *act)
960{
961 struct zfcp_adapter *adapter = act->adapter;
962 struct zfcp_port *port = act->port;
Christof Schmitt287ac012008-07-02 10:56:40 +0200963 int p_status = atomic_read(&port->status);
964
965 switch (act->step) {
966 case ZFCP_ERP_STEP_UNINITIALIZED:
967 case ZFCP_ERP_STEP_PHYS_PORT_CLOSING:
968 case ZFCP_ERP_STEP_PORT_CLOSING:
969 if (fc_host_port_type(adapter->scsi_host) == FC_PORTTYPE_PTP)
970 return zfcp_erp_open_ptp_port(act);
Christof Schmittb98478d2008-12-19 16:56:59 +0100971 if (!port->d_id) {
Christof Schmitt934aeb5872009-10-14 11:00:43 +0200972 zfcp_fc_trigger_did_lookup(port);
Christof Schmitt799b76d2009-08-18 15:43:20 +0200973 return ZFCP_ERP_EXIT;
Christof Schmitt287ac012008-07-02 10:56:40 +0200974 }
Christof Schmitt287ac012008-07-02 10:56:40 +0200975 return zfcp_erp_port_strategy_open_port(act);
976
977 case ZFCP_ERP_STEP_PORT_OPENING:
978 /* D_ID might have changed during open */
Swen Schillig5ab944f2008-10-01 12:42:17 +0200979 if (p_status & ZFCP_STATUS_COMMON_OPEN) {
Christof Schmitt934aeb5872009-10-14 11:00:43 +0200980 if (!port->d_id) {
981 zfcp_fc_trigger_did_lookup(port);
982 return ZFCP_ERP_EXIT;
Swen Schillig5ab944f2008-10-01 12:42:17 +0200983 }
Christof Schmitt934aeb5872009-10-14 11:00:43 +0200984 return ZFCP_ERP_SUCCEEDED;
Swen Schillig5ab944f2008-10-01 12:42:17 +0200985 }
Swen Schilligea460a82009-05-15 13:18:20 +0200986 if (port->d_id && !(p_status & ZFCP_STATUS_COMMON_NOESC)) {
987 port->d_id = 0;
Christof Schmittf7bd7c32010-07-08 09:53:10 +0200988 return ZFCP_ERP_FAILED;
Swen Schilligea460a82009-05-15 13:18:20 +0200989 }
990 /* fall through otherwise */
Christof Schmitt287ac012008-07-02 10:56:40 +0200991 }
992 return ZFCP_ERP_FAILED;
993}
994
Christof Schmitt287ac012008-07-02 10:56:40 +0200995static int zfcp_erp_port_strategy(struct zfcp_erp_action *erp_action)
996{
997 struct zfcp_port *port = erp_action->port;
Christof Schmitt934aeb5872009-10-14 11:00:43 +0200998 int p_status = atomic_read(&port->status);
Christof Schmitt287ac012008-07-02 10:56:40 +0200999
Christof Schmitt934aeb5872009-10-14 11:00:43 +02001000 if ((p_status & ZFCP_STATUS_COMMON_NOESC) &&
1001 !(p_status & ZFCP_STATUS_COMMON_OPEN))
Swen Schillig5ab944f2008-10-01 12:42:17 +02001002 goto close_init_done;
1003
Christof Schmitt287ac012008-07-02 10:56:40 +02001004 switch (erp_action->step) {
1005 case ZFCP_ERP_STEP_UNINITIALIZED:
Christof Schmitt934aeb5872009-10-14 11:00:43 +02001006 if (p_status & ZFCP_STATUS_COMMON_OPEN)
Christof Schmitt287ac012008-07-02 10:56:40 +02001007 return zfcp_erp_port_strategy_close(erp_action);
1008 break;
1009
1010 case ZFCP_ERP_STEP_PORT_CLOSING:
Christof Schmitt934aeb5872009-10-14 11:00:43 +02001011 if (p_status & ZFCP_STATUS_COMMON_OPEN)
Christof Schmitt287ac012008-07-02 10:56:40 +02001012 return ZFCP_ERP_FAILED;
1013 break;
1014 }
Swen Schillig5ab944f2008-10-01 12:42:17 +02001015
1016close_init_done:
Christof Schmitt287ac012008-07-02 10:56:40 +02001017 if (erp_action->status & ZFCP_STATUS_ERP_CLOSE_ONLY)
1018 return ZFCP_ERP_EXIT;
Christof Schmitt287ac012008-07-02 10:56:40 +02001019
Swen Schillig5ab944f2008-10-01 12:42:17 +02001020 return zfcp_erp_port_strategy_open_common(erp_action);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021}
1022
Christof Schmittb62a8d92010-09-08 14:39:55 +02001023static void zfcp_erp_lun_strategy_clearstati(struct scsi_device *sdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024{
Christof Schmittb62a8d92010-09-08 14:39:55 +02001025 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
1026
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001027 atomic_andnot(ZFCP_STATUS_COMMON_ACCESS_DENIED,
Christof Schmittb62a8d92010-09-08 14:39:55 +02001028 &zfcp_sdev->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029}
1030
Christof Schmittb62a8d92010-09-08 14:39:55 +02001031static int zfcp_erp_lun_strategy_close(struct zfcp_erp_action *erp_action)
Christof Schmitt287ac012008-07-02 10:56:40 +02001032{
Christof Schmittb62a8d92010-09-08 14:39:55 +02001033 int retval = zfcp_fsf_close_lun(erp_action);
Christof Schmitt287ac012008-07-02 10:56:40 +02001034 if (retval == -ENOMEM)
1035 return ZFCP_ERP_NOMEM;
Christof Schmittb62a8d92010-09-08 14:39:55 +02001036 erp_action->step = ZFCP_ERP_STEP_LUN_CLOSING;
Christof Schmitt287ac012008-07-02 10:56:40 +02001037 if (retval)
1038 return ZFCP_ERP_FAILED;
1039 return ZFCP_ERP_CONTINUES;
1040}
1041
Christof Schmittb62a8d92010-09-08 14:39:55 +02001042static int zfcp_erp_lun_strategy_open(struct zfcp_erp_action *erp_action)
Christof Schmitt287ac012008-07-02 10:56:40 +02001043{
Christof Schmittb62a8d92010-09-08 14:39:55 +02001044 int retval = zfcp_fsf_open_lun(erp_action);
Christof Schmitt287ac012008-07-02 10:56:40 +02001045 if (retval == -ENOMEM)
1046 return ZFCP_ERP_NOMEM;
Christof Schmittb62a8d92010-09-08 14:39:55 +02001047 erp_action->step = ZFCP_ERP_STEP_LUN_OPENING;
Christof Schmitt287ac012008-07-02 10:56:40 +02001048 if (retval)
1049 return ZFCP_ERP_FAILED;
1050 return ZFCP_ERP_CONTINUES;
1051}
1052
Christof Schmittb62a8d92010-09-08 14:39:55 +02001053static int zfcp_erp_lun_strategy(struct zfcp_erp_action *erp_action)
Christof Schmitt287ac012008-07-02 10:56:40 +02001054{
Christof Schmittb62a8d92010-09-08 14:39:55 +02001055 struct scsi_device *sdev = erp_action->sdev;
1056 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
Christof Schmitt287ac012008-07-02 10:56:40 +02001057
1058 switch (erp_action->step) {
1059 case ZFCP_ERP_STEP_UNINITIALIZED:
Christof Schmittb62a8d92010-09-08 14:39:55 +02001060 zfcp_erp_lun_strategy_clearstati(sdev);
1061 if (atomic_read(&zfcp_sdev->status) & ZFCP_STATUS_COMMON_OPEN)
1062 return zfcp_erp_lun_strategy_close(erp_action);
Christof Schmitt287ac012008-07-02 10:56:40 +02001063 /* already closed, fall through */
Christof Schmittb62a8d92010-09-08 14:39:55 +02001064 case ZFCP_ERP_STEP_LUN_CLOSING:
1065 if (atomic_read(&zfcp_sdev->status) & ZFCP_STATUS_COMMON_OPEN)
Christof Schmitt287ac012008-07-02 10:56:40 +02001066 return ZFCP_ERP_FAILED;
1067 if (erp_action->status & ZFCP_STATUS_ERP_CLOSE_ONLY)
1068 return ZFCP_ERP_EXIT;
Christof Schmittb62a8d92010-09-08 14:39:55 +02001069 return zfcp_erp_lun_strategy_open(erp_action);
Christof Schmitt287ac012008-07-02 10:56:40 +02001070
Christof Schmittb62a8d92010-09-08 14:39:55 +02001071 case ZFCP_ERP_STEP_LUN_OPENING:
1072 if (atomic_read(&zfcp_sdev->status) & ZFCP_STATUS_COMMON_OPEN)
Christof Schmitt287ac012008-07-02 10:56:40 +02001073 return ZFCP_ERP_SUCCEEDED;
1074 }
1075 return ZFCP_ERP_FAILED;
1076}
1077
Christof Schmittb62a8d92010-09-08 14:39:55 +02001078static int zfcp_erp_strategy_check_lun(struct scsi_device *sdev, int result)
Christof Schmitt287ac012008-07-02 10:56:40 +02001079{
Christof Schmittb62a8d92010-09-08 14:39:55 +02001080 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
1081
Christof Schmitt287ac012008-07-02 10:56:40 +02001082 switch (result) {
1083 case ZFCP_ERP_SUCCEEDED :
Christof Schmittb62a8d92010-09-08 14:39:55 +02001084 atomic_set(&zfcp_sdev->erp_counter, 0);
1085 zfcp_erp_lun_unblock(sdev);
Christof Schmitt287ac012008-07-02 10:56:40 +02001086 break;
1087 case ZFCP_ERP_FAILED :
Christof Schmittb62a8d92010-09-08 14:39:55 +02001088 atomic_inc(&zfcp_sdev->erp_counter);
1089 if (atomic_read(&zfcp_sdev->erp_counter) > ZFCP_MAX_ERPS) {
1090 dev_err(&zfcp_sdev->port->adapter->ccw_device->dev,
1091 "ERP failed for LUN 0x%016Lx on "
Christof Schmittff3b24f2008-10-01 12:42:15 +02001092 "port 0x%016Lx\n",
Christof Schmittb62a8d92010-09-08 14:39:55 +02001093 (unsigned long long)zfcp_scsi_dev_lun(sdev),
1094 (unsigned long long)zfcp_sdev->port->wwpn);
Swen Schilligedaed852010-09-08 14:40:01 +02001095 zfcp_erp_set_lun_status(sdev,
1096 ZFCP_STATUS_COMMON_ERP_FAILED);
Christof Schmittff3b24f2008-10-01 12:42:15 +02001097 }
Christof Schmitt287ac012008-07-02 10:56:40 +02001098 break;
1099 }
1100
Christof Schmittb62a8d92010-09-08 14:39:55 +02001101 if (atomic_read(&zfcp_sdev->status) & ZFCP_STATUS_COMMON_ERP_FAILED) {
1102 zfcp_erp_lun_block(sdev, 0);
Christof Schmitt287ac012008-07-02 10:56:40 +02001103 result = ZFCP_ERP_EXIT;
1104 }
1105 return result;
1106}
1107
1108static int zfcp_erp_strategy_check_port(struct zfcp_port *port, int result)
1109{
1110 switch (result) {
1111 case ZFCP_ERP_SUCCEEDED :
1112 atomic_set(&port->erp_counter, 0);
1113 zfcp_erp_port_unblock(port);
1114 break;
1115
1116 case ZFCP_ERP_FAILED :
1117 if (atomic_read(&port->status) & ZFCP_STATUS_COMMON_NOESC) {
1118 zfcp_erp_port_block(port, 0);
1119 result = ZFCP_ERP_EXIT;
1120 }
1121 atomic_inc(&port->erp_counter);
Christof Schmittff3b24f2008-10-01 12:42:15 +02001122 if (atomic_read(&port->erp_counter) > ZFCP_MAX_ERPS) {
1123 dev_err(&port->adapter->ccw_device->dev,
1124 "ERP failed for remote port 0x%016Lx\n",
Swen Schillig7ba58c92008-10-01 12:42:18 +02001125 (unsigned long long)port->wwpn);
Swen Schilligedaed852010-09-08 14:40:01 +02001126 zfcp_erp_set_port_status(port,
1127 ZFCP_STATUS_COMMON_ERP_FAILED);
Christof Schmittff3b24f2008-10-01 12:42:15 +02001128 }
Christof Schmitt287ac012008-07-02 10:56:40 +02001129 break;
1130 }
1131
1132 if (atomic_read(&port->status) & ZFCP_STATUS_COMMON_ERP_FAILED) {
1133 zfcp_erp_port_block(port, 0);
1134 result = ZFCP_ERP_EXIT;
1135 }
1136 return result;
1137}
1138
1139static int zfcp_erp_strategy_check_adapter(struct zfcp_adapter *adapter,
1140 int result)
1141{
1142 switch (result) {
1143 case ZFCP_ERP_SUCCEEDED :
1144 atomic_set(&adapter->erp_counter, 0);
1145 zfcp_erp_adapter_unblock(adapter);
1146 break;
1147
1148 case ZFCP_ERP_FAILED :
1149 atomic_inc(&adapter->erp_counter);
Christof Schmittff3b24f2008-10-01 12:42:15 +02001150 if (atomic_read(&adapter->erp_counter) > ZFCP_MAX_ERPS) {
1151 dev_err(&adapter->ccw_device->dev,
1152 "ERP cannot recover an error "
1153 "on the FCP device\n");
Swen Schilligedaed852010-09-08 14:40:01 +02001154 zfcp_erp_set_adapter_status(adapter,
1155 ZFCP_STATUS_COMMON_ERP_FAILED);
Christof Schmittff3b24f2008-10-01 12:42:15 +02001156 }
Christof Schmitt287ac012008-07-02 10:56:40 +02001157 break;
1158 }
1159
1160 if (atomic_read(&adapter->status) & ZFCP_STATUS_COMMON_ERP_FAILED) {
1161 zfcp_erp_adapter_block(adapter, 0);
1162 result = ZFCP_ERP_EXIT;
1163 }
1164 return result;
1165}
1166
1167static int zfcp_erp_strategy_check_target(struct zfcp_erp_action *erp_action,
1168 int result)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169{
1170 struct zfcp_adapter *adapter = erp_action->adapter;
1171 struct zfcp_port *port = erp_action->port;
Christof Schmittb62a8d92010-09-08 14:39:55 +02001172 struct scsi_device *sdev = erp_action->sdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 switch (erp_action->action) {
1175
Christof Schmittb62a8d92010-09-08 14:39:55 +02001176 case ZFCP_ERP_ACTION_REOPEN_LUN:
1177 result = zfcp_erp_strategy_check_lun(sdev, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 break;
1179
1180 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
1181 case ZFCP_ERP_ACTION_REOPEN_PORT:
1182 result = zfcp_erp_strategy_check_port(port, result);
1183 break;
1184
1185 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
1186 result = zfcp_erp_strategy_check_adapter(adapter, result);
1187 break;
1188 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 return result;
1190}
1191
Christof Schmitt287ac012008-07-02 10:56:40 +02001192static int zfcp_erp_strat_change_det(atomic_t *target_status, u32 erp_status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193{
Christof Schmitt287ac012008-07-02 10:56:40 +02001194 int status = atomic_read(target_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195
Christof Schmitt287ac012008-07-02 10:56:40 +02001196 if ((status & ZFCP_STATUS_COMMON_RUNNING) &&
1197 (erp_status & ZFCP_STATUS_ERP_CLOSE_ONLY))
1198 return 1; /* take it online */
1199
1200 if (!(status & ZFCP_STATUS_COMMON_RUNNING) &&
1201 !(erp_status & ZFCP_STATUS_ERP_CLOSE_ONLY))
1202 return 1; /* take it offline */
1203
1204 return 0;
1205}
1206
1207static int zfcp_erp_strategy_statechange(struct zfcp_erp_action *act, int ret)
1208{
1209 int action = act->action;
1210 struct zfcp_adapter *adapter = act->adapter;
1211 struct zfcp_port *port = act->port;
Christof Schmittb62a8d92010-09-08 14:39:55 +02001212 struct scsi_device *sdev = act->sdev;
1213 struct zfcp_scsi_dev *zfcp_sdev;
Christof Schmitt287ac012008-07-02 10:56:40 +02001214 u32 erp_status = act->status;
1215
1216 switch (action) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
Christof Schmitt287ac012008-07-02 10:56:40 +02001218 if (zfcp_erp_strat_change_det(&adapter->status, erp_status)) {
1219 _zfcp_erp_adapter_reopen(adapter,
1220 ZFCP_STATUS_COMMON_ERP_FAILED,
Swen Schilligea4a3a62010-12-02 15:16:16 +01001221 "ersscg1");
Christof Schmitt287ac012008-07-02 10:56:40 +02001222 return ZFCP_ERP_EXIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 }
1224 break;
1225
1226 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
1227 case ZFCP_ERP_ACTION_REOPEN_PORT:
Christof Schmitt287ac012008-07-02 10:56:40 +02001228 if (zfcp_erp_strat_change_det(&port->status, erp_status)) {
1229 _zfcp_erp_port_reopen(port,
1230 ZFCP_STATUS_COMMON_ERP_FAILED,
Swen Schilligea4a3a62010-12-02 15:16:16 +01001231 "ersscg2");
Christof Schmitt287ac012008-07-02 10:56:40 +02001232 return ZFCP_ERP_EXIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 }
1234 break;
1235
Christof Schmittb62a8d92010-09-08 14:39:55 +02001236 case ZFCP_ERP_ACTION_REOPEN_LUN:
1237 zfcp_sdev = sdev_to_zfcp(sdev);
1238 if (zfcp_erp_strat_change_det(&zfcp_sdev->status, erp_status)) {
1239 _zfcp_erp_lun_reopen(sdev,
1240 ZFCP_STATUS_COMMON_ERP_FAILED,
Swen Schilligea4a3a62010-12-02 15:16:16 +01001241 "ersscg3", 0);
Christof Schmitt287ac012008-07-02 10:56:40 +02001242 return ZFCP_ERP_EXIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 }
1244 break;
1245 }
Christof Schmitt287ac012008-07-02 10:56:40 +02001246 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247}
1248
Christof Schmitt287ac012008-07-02 10:56:40 +02001249static void zfcp_erp_action_dequeue(struct zfcp_erp_action *erp_action)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250{
Christof Schmitt287ac012008-07-02 10:56:40 +02001251 struct zfcp_adapter *adapter = erp_action->adapter;
Christof Schmittb62a8d92010-09-08 14:39:55 +02001252 struct zfcp_scsi_dev *zfcp_sdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253
Christof Schmitt287ac012008-07-02 10:56:40 +02001254 adapter->erp_total_count--;
1255 if (erp_action->status & ZFCP_STATUS_ERP_LOWMEM) {
1256 adapter->erp_low_mem_count--;
1257 erp_action->status &= ~ZFCP_STATUS_ERP_LOWMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 }
1259
Christof Schmitt287ac012008-07-02 10:56:40 +02001260 list_del(&erp_action->list);
Swen Schilligae0904f2010-12-02 15:16:12 +01001261 zfcp_dbf_rec_run("eractd1", erp_action);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262
Christof Schmitt287ac012008-07-02 10:56:40 +02001263 switch (erp_action->action) {
Christof Schmittb62a8d92010-09-08 14:39:55 +02001264 case ZFCP_ERP_ACTION_REOPEN_LUN:
1265 zfcp_sdev = sdev_to_zfcp(erp_action->sdev);
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001266 atomic_andnot(ZFCP_STATUS_COMMON_ERP_INUSE,
Christof Schmittb62a8d92010-09-08 14:39:55 +02001267 &zfcp_sdev->status);
Christof Schmitt287ac012008-07-02 10:56:40 +02001268 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269
Christof Schmitt287ac012008-07-02 10:56:40 +02001270 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
1271 case ZFCP_ERP_ACTION_REOPEN_PORT:
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001272 atomic_andnot(ZFCP_STATUS_COMMON_ERP_INUSE,
Christof Schmitt287ac012008-07-02 10:56:40 +02001273 &erp_action->port->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 break;
Christof Schmitt287ac012008-07-02 10:56:40 +02001275
1276 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001277 atomic_andnot(ZFCP_STATUS_COMMON_ERP_INUSE,
Christof Schmitt287ac012008-07-02 10:56:40 +02001278 &erp_action->adapter->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 break;
1280 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281}
1282
Steffen Maier6f2ce1c2016-12-09 17:16:33 +01001283/**
1284 * zfcp_erp_try_rport_unblock - unblock rport if no more/new recovery
1285 * @port: zfcp_port whose fc_rport we should try to unblock
1286 */
1287static void zfcp_erp_try_rport_unblock(struct zfcp_port *port)
1288{
1289 unsigned long flags;
1290 struct zfcp_adapter *adapter = port->adapter;
1291 int port_status;
1292 struct Scsi_Host *shost = adapter->scsi_host;
1293 struct scsi_device *sdev;
1294
1295 write_lock_irqsave(&adapter->erp_lock, flags);
1296 port_status = atomic_read(&port->status);
1297 if ((port_status & ZFCP_STATUS_COMMON_UNBLOCKED) == 0 ||
1298 (port_status & (ZFCP_STATUS_COMMON_ERP_INUSE |
1299 ZFCP_STATUS_COMMON_ERP_FAILED)) != 0) {
1300 /* new ERP of severity >= port triggered elsewhere meanwhile or
1301 * local link down (adapter erp_failed but not clear unblock)
1302 */
1303 zfcp_dbf_rec_run_lvl(4, "ertru_p", &port->erp_action);
1304 write_unlock_irqrestore(&adapter->erp_lock, flags);
1305 return;
1306 }
1307 spin_lock(shost->host_lock);
1308 __shost_for_each_device(sdev, shost) {
1309 struct zfcp_scsi_dev *zsdev = sdev_to_zfcp(sdev);
1310 int lun_status;
1311
1312 if (zsdev->port != port)
1313 continue;
1314 /* LUN under port of interest */
1315 lun_status = atomic_read(&zsdev->status);
1316 if ((lun_status & ZFCP_STATUS_COMMON_ERP_FAILED) != 0)
1317 continue; /* unblock rport despite failed LUNs */
1318 /* LUN recovery not given up yet [maybe follow-up pending] */
1319 if ((lun_status & ZFCP_STATUS_COMMON_UNBLOCKED) == 0 ||
1320 (lun_status & ZFCP_STATUS_COMMON_ERP_INUSE) != 0) {
1321 /* LUN blocked:
1322 * not yet unblocked [LUN recovery pending]
1323 * or meanwhile blocked [new LUN recovery triggered]
1324 */
1325 zfcp_dbf_rec_run_lvl(4, "ertru_l", &zsdev->erp_action);
1326 spin_unlock(shost->host_lock);
1327 write_unlock_irqrestore(&adapter->erp_lock, flags);
1328 return;
1329 }
1330 }
1331 /* now port has no child or all children have completed recovery,
1332 * and no ERP of severity >= port was meanwhile triggered elsewhere
1333 */
1334 zfcp_scsi_schedule_rport_register(port);
1335 spin_unlock(shost->host_lock);
1336 write_unlock_irqrestore(&adapter->erp_lock, flags);
1337}
1338
Christof Schmitt287ac012008-07-02 10:56:40 +02001339static void zfcp_erp_action_cleanup(struct zfcp_erp_action *act, int result)
Maxim Shchetyninaef4a982005-09-13 21:51:16 +02001340{
Christof Schmitt287ac012008-07-02 10:56:40 +02001341 struct zfcp_adapter *adapter = act->adapter;
1342 struct zfcp_port *port = act->port;
Christof Schmittb62a8d92010-09-08 14:39:55 +02001343 struct scsi_device *sdev = act->sdev;
Maxim Shchetyninaef4a982005-09-13 21:51:16 +02001344
Christof Schmitt287ac012008-07-02 10:56:40 +02001345 switch (act->action) {
Christof Schmittb62a8d92010-09-08 14:39:55 +02001346 case ZFCP_ERP_ACTION_REOPEN_LUN:
Christof Schmittfdbd1c52010-09-08 14:39:54 +02001347 if (!(act->status & ZFCP_STATUS_ERP_NO_REF))
Christof Schmittb62a8d92010-09-08 14:39:55 +02001348 scsi_device_put(sdev);
Steffen Maier6f2ce1c2016-12-09 17:16:33 +01001349 zfcp_erp_try_rport_unblock(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 break;
Christof Schmitt287ac012008-07-02 10:56:40 +02001351
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 case ZFCP_ERP_ACTION_REOPEN_PORT:
Steffen Maier4eeaa4f2016-08-10 18:30:46 +02001353 /* This switch case might also happen after a forced reopen
1354 * was successfully done and thus overwritten with a new
1355 * non-forced reopen at `ersfs_2'. In this case, we must not
1356 * do the clean-up of the non-forced version.
1357 */
1358 if (act->step != ZFCP_ERP_STEP_UNINITIALIZED)
1359 if (result == ZFCP_ERP_SUCCEEDED)
Steffen Maier6f2ce1c2016-12-09 17:16:33 +01001360 zfcp_erp_try_rport_unblock(port);
Christof Schmitt57676202010-07-08 09:53:05 +02001361 /* fall through */
1362 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
Christof Schmitt615f59e2010-02-17 11:18:56 +01001363 put_device(&port->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 break;
Christof Schmitt287ac012008-07-02 10:56:40 +02001365
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
Christof Schmitta2fa0ae2009-03-02 13:09:08 +01001367 if (result == ZFCP_ERP_SUCCEEDED) {
Christof Schmittbd43a42b72008-12-25 13:38:50 +01001368 register_service_level(&adapter->service_level);
Steffen Maier43f60cb2012-09-04 15:23:35 +02001369 zfcp_fc_conditional_port_scan(adapter);
Christof Schmitt038d9442011-02-22 19:54:48 +01001370 queue_work(adapter->work_queue, &adapter->ns_up_work);
Christof Schmitta2fa0ae2009-03-02 13:09:08 +01001371 } else
1372 unregister_service_level(&adapter->service_level);
Christof Schmitt038d9442011-02-22 19:54:48 +01001373
Swen Schilligf3450c72009-11-24 16:53:59 +01001374 kref_put(&adapter->ref, zfcp_adapter_release);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 }
1377}
1378
Christof Schmitt287ac012008-07-02 10:56:40 +02001379static int zfcp_erp_strategy_do_action(struct zfcp_erp_action *erp_action)
1380{
1381 switch (erp_action->action) {
1382 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
1383 return zfcp_erp_adapter_strategy(erp_action);
1384 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
1385 return zfcp_erp_port_forced_strategy(erp_action);
1386 case ZFCP_ERP_ACTION_REOPEN_PORT:
1387 return zfcp_erp_port_strategy(erp_action);
Christof Schmittb62a8d92010-09-08 14:39:55 +02001388 case ZFCP_ERP_ACTION_REOPEN_LUN:
1389 return zfcp_erp_lun_strategy(erp_action);
Christof Schmitt287ac012008-07-02 10:56:40 +02001390 }
1391 return ZFCP_ERP_FAILED;
1392}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393
Christof Schmitt287ac012008-07-02 10:56:40 +02001394static int zfcp_erp_strategy(struct zfcp_erp_action *erp_action)
1395{
1396 int retval;
Christof Schmitt287ac012008-07-02 10:56:40 +02001397 unsigned long flags;
Swen Schilligecf0c772009-11-24 16:53:58 +01001398 struct zfcp_adapter *adapter = erp_action->adapter;
Christof Schmitt287ac012008-07-02 10:56:40 +02001399
Swen Schilligf3450c72009-11-24 16:53:59 +01001400 kref_get(&adapter->ref);
Christof Schmitt287ac012008-07-02 10:56:40 +02001401
Swen Schilligf3450c72009-11-24 16:53:59 +01001402 write_lock_irqsave(&adapter->erp_lock, flags);
Christof Schmitt287ac012008-07-02 10:56:40 +02001403 zfcp_erp_strategy_check_fsfreq(erp_action);
1404
1405 if (erp_action->status & ZFCP_STATUS_ERP_DISMISSED) {
1406 zfcp_erp_action_dequeue(erp_action);
1407 retval = ZFCP_ERP_DISMISSED;
1408 goto unlock;
1409 }
1410
Christof Schmitt9c785d92010-07-08 09:53:09 +02001411 if (erp_action->status & ZFCP_STATUS_ERP_TIMEDOUT) {
1412 retval = ZFCP_ERP_FAILED;
1413 goto check_target;
1414 }
1415
Christof Schmitt287ac012008-07-02 10:56:40 +02001416 zfcp_erp_action_to_running(erp_action);
1417
1418 /* no lock to allow for blocking operations */
Swen Schilligecf0c772009-11-24 16:53:58 +01001419 write_unlock_irqrestore(&adapter->erp_lock, flags);
Christof Schmitt287ac012008-07-02 10:56:40 +02001420 retval = zfcp_erp_strategy_do_action(erp_action);
Swen Schilligecf0c772009-11-24 16:53:58 +01001421 write_lock_irqsave(&adapter->erp_lock, flags);
Christof Schmitt287ac012008-07-02 10:56:40 +02001422
1423 if (erp_action->status & ZFCP_STATUS_ERP_DISMISSED)
1424 retval = ZFCP_ERP_CONTINUES;
1425
1426 switch (retval) {
1427 case ZFCP_ERP_NOMEM:
1428 if (!(erp_action->status & ZFCP_STATUS_ERP_LOWMEM)) {
1429 ++adapter->erp_low_mem_count;
1430 erp_action->status |= ZFCP_STATUS_ERP_LOWMEM;
1431 }
1432 if (adapter->erp_total_count == adapter->erp_low_mem_count)
Swen Schilligea4a3a62010-12-02 15:16:16 +01001433 _zfcp_erp_adapter_reopen(adapter, 0, "erstgy1");
Christof Schmitt287ac012008-07-02 10:56:40 +02001434 else {
1435 zfcp_erp_strategy_memwait(erp_action);
1436 retval = ZFCP_ERP_CONTINUES;
1437 }
1438 goto unlock;
1439
1440 case ZFCP_ERP_CONTINUES:
1441 if (erp_action->status & ZFCP_STATUS_ERP_LOWMEM) {
1442 --adapter->erp_low_mem_count;
1443 erp_action->status &= ~ZFCP_STATUS_ERP_LOWMEM;
1444 }
1445 goto unlock;
1446 }
1447
Christof Schmitt9c785d92010-07-08 09:53:09 +02001448check_target:
Christof Schmitt287ac012008-07-02 10:56:40 +02001449 retval = zfcp_erp_strategy_check_target(erp_action, retval);
1450 zfcp_erp_action_dequeue(erp_action);
1451 retval = zfcp_erp_strategy_statechange(erp_action, retval);
1452 if (retval == ZFCP_ERP_EXIT)
1453 goto unlock;
Christof Schmitt85600f72009-07-13 15:06:09 +02001454 if (retval == ZFCP_ERP_SUCCEEDED)
1455 zfcp_erp_strategy_followup_success(erp_action);
1456 if (retval == ZFCP_ERP_FAILED)
1457 zfcp_erp_strategy_followup_failed(erp_action);
Christof Schmitt287ac012008-07-02 10:56:40 +02001458
1459 unlock:
Swen Schilligecf0c772009-11-24 16:53:58 +01001460 write_unlock_irqrestore(&adapter->erp_lock, flags);
Christof Schmitt287ac012008-07-02 10:56:40 +02001461
1462 if (retval != ZFCP_ERP_CONTINUES)
1463 zfcp_erp_action_cleanup(erp_action, retval);
1464
Swen Schilligf3450c72009-11-24 16:53:59 +01001465 kref_put(&adapter->ref, zfcp_adapter_release);
Christof Schmitt287ac012008-07-02 10:56:40 +02001466 return retval;
1467}
1468
1469static int zfcp_erp_thread(void *data)
1470{
1471 struct zfcp_adapter *adapter = (struct zfcp_adapter *) data;
1472 struct list_head *next;
1473 struct zfcp_erp_action *act;
1474 unsigned long flags;
1475
Christof Schmitt347c6a92009-08-18 15:43:25 +02001476 for (;;) {
Christof Schmitt347c6a92009-08-18 15:43:25 +02001477 wait_event_interruptible(adapter->erp_ready_wq,
1478 !list_empty(&adapter->erp_ready_head) ||
1479 kthread_should_stop());
Swen Schillig94ab4b32009-04-17 15:08:06 +02001480
Christof Schmitt347c6a92009-08-18 15:43:25 +02001481 if (kthread_should_stop())
1482 break;
1483
Christof Schmitt287ac012008-07-02 10:56:40 +02001484 write_lock_irqsave(&adapter->erp_lock, flags);
1485 next = adapter->erp_ready_head.next;
1486 write_unlock_irqrestore(&adapter->erp_lock, flags);
1487
1488 if (next != &adapter->erp_ready_head) {
1489 act = list_entry(next, struct zfcp_erp_action, list);
1490
1491 /* there is more to come after dismission, no notify */
1492 if (zfcp_erp_strategy(act) != ZFCP_ERP_DISMISSED)
1493 zfcp_erp_wakeup(adapter);
1494 }
Christof Schmitt287ac012008-07-02 10:56:40 +02001495 }
1496
Christof Schmitt287ac012008-07-02 10:56:40 +02001497 return 0;
1498}
1499
1500/**
1501 * zfcp_erp_thread_setup - Start ERP thread for adapter
1502 * @adapter: Adapter to start the ERP thread for
1503 *
1504 * Returns 0 on success or error code from kernel_thread()
1505 */
1506int zfcp_erp_thread_setup(struct zfcp_adapter *adapter)
1507{
Christof Schmitt347c6a92009-08-18 15:43:25 +02001508 struct task_struct *thread;
Christof Schmitt287ac012008-07-02 10:56:40 +02001509
Christof Schmitt347c6a92009-08-18 15:43:25 +02001510 thread = kthread_run(zfcp_erp_thread, adapter, "zfcperp%s",
1511 dev_name(&adapter->ccw_device->dev));
1512 if (IS_ERR(thread)) {
Christof Schmitt287ac012008-07-02 10:56:40 +02001513 dev_err(&adapter->ccw_device->dev,
Christof Schmittff3b24f2008-10-01 12:42:15 +02001514 "Creating an ERP thread for the FCP device failed.\n");
Christof Schmitt347c6a92009-08-18 15:43:25 +02001515 return PTR_ERR(thread);
Christof Schmitt287ac012008-07-02 10:56:40 +02001516 }
Christof Schmitt347c6a92009-08-18 15:43:25 +02001517
1518 adapter->erp_thread = thread;
Christof Schmitt287ac012008-07-02 10:56:40 +02001519 return 0;
1520}
1521
1522/**
1523 * zfcp_erp_thread_kill - Stop ERP thread.
1524 * @adapter: Adapter where the ERP thread should be stopped.
1525 *
1526 * The caller of this routine ensures that the specified adapter has
1527 * been shut down and that this operation has been completed. Thus,
1528 * there are no pending erp_actions which would need to be handled
1529 * here.
1530 */
1531void zfcp_erp_thread_kill(struct zfcp_adapter *adapter)
1532{
Christof Schmitt347c6a92009-08-18 15:43:25 +02001533 kthread_stop(adapter->erp_thread);
1534 adapter->erp_thread = NULL;
Christof Schmitt143bb6b2009-08-18 15:43:27 +02001535 WARN_ON(!list_empty(&adapter->erp_ready_head));
1536 WARN_ON(!list_empty(&adapter->erp_running_head));
Christof Schmitt287ac012008-07-02 10:56:40 +02001537}
1538
1539/**
Christof Schmitt287ac012008-07-02 10:56:40 +02001540 * zfcp_erp_wait - wait for completion of error recovery on an adapter
1541 * @adapter: adapter for which to wait for completion of its error recovery
1542 */
1543void zfcp_erp_wait(struct zfcp_adapter *adapter)
1544{
1545 wait_event(adapter->erp_done_wqh,
1546 !(atomic_read(&adapter->status) &
1547 ZFCP_STATUS_ADAPTER_ERP_PENDING));
1548}
1549
1550/**
Swen Schilligedaed852010-09-08 14:40:01 +02001551 * zfcp_erp_set_adapter_status - set adapter status bits
Christof Schmitt287ac012008-07-02 10:56:40 +02001552 * @adapter: adapter to change the status
Christof Schmitt287ac012008-07-02 10:56:40 +02001553 * @mask: status bits to change
Christof Schmitt287ac012008-07-02 10:56:40 +02001554 *
Christof Schmittb62a8d92010-09-08 14:39:55 +02001555 * Changes in common status bits are propagated to attached ports and LUNs.
Christof Schmitt287ac012008-07-02 10:56:40 +02001556 */
Swen Schilligedaed852010-09-08 14:40:01 +02001557void zfcp_erp_set_adapter_status(struct zfcp_adapter *adapter, u32 mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 struct zfcp_port *port;
Swen Schilligedaed852010-09-08 14:40:01 +02001560 struct scsi_device *sdev;
Swen Schilligecf0c772009-11-24 16:53:58 +01001561 unsigned long flags;
Christof Schmitt287ac012008-07-02 10:56:40 +02001562 u32 common_mask = mask & ZFCP_COMMON_FLAGS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001564 atomic_or(mask, &adapter->status);
Christof Schmitt287ac012008-07-02 10:56:40 +02001565
Swen Schilligedaed852010-09-08 14:40:01 +02001566 if (!common_mask)
1567 return;
1568
1569 read_lock_irqsave(&adapter->port_list_lock, flags);
1570 list_for_each_entry(port, &adapter->port_list, list)
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001571 atomic_or(common_mask, &port->status);
Swen Schilligedaed852010-09-08 14:40:01 +02001572 read_unlock_irqrestore(&adapter->port_list_lock, flags);
1573
Martin Peschke924dd582013-08-22 17:45:37 +02001574 spin_lock_irqsave(adapter->scsi_host->host_lock, flags);
1575 __shost_for_each_device(sdev, adapter->scsi_host)
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001576 atomic_or(common_mask, &sdev_to_zfcp(sdev)->status);
Martin Peschke924dd582013-08-22 17:45:37 +02001577 spin_unlock_irqrestore(adapter->scsi_host->host_lock, flags);
Swen Schilligedaed852010-09-08 14:40:01 +02001578}
1579
1580/**
1581 * zfcp_erp_clear_adapter_status - clear adapter status bits
1582 * @adapter: adapter to change the status
1583 * @mask: status bits to change
1584 *
1585 * Changes in common status bits are propagated to attached ports and LUNs.
1586 */
1587void zfcp_erp_clear_adapter_status(struct zfcp_adapter *adapter, u32 mask)
1588{
1589 struct zfcp_port *port;
1590 struct scsi_device *sdev;
1591 unsigned long flags;
1592 u32 common_mask = mask & ZFCP_COMMON_FLAGS;
1593 u32 clear_counter = mask & ZFCP_STATUS_COMMON_ERP_FAILED;
1594
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001595 atomic_andnot(mask, &adapter->status);
Swen Schilligedaed852010-09-08 14:40:01 +02001596
1597 if (!common_mask)
1598 return;
1599
1600 if (clear_counter)
1601 atomic_set(&adapter->erp_counter, 0);
1602
1603 read_lock_irqsave(&adapter->port_list_lock, flags);
1604 list_for_each_entry(port, &adapter->port_list, list) {
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001605 atomic_andnot(common_mask, &port->status);
Swen Schilligedaed852010-09-08 14:40:01 +02001606 if (clear_counter)
1607 atomic_set(&port->erp_counter, 0);
1608 }
1609 read_unlock_irqrestore(&adapter->port_list_lock, flags);
1610
Martin Peschke924dd582013-08-22 17:45:37 +02001611 spin_lock_irqsave(adapter->scsi_host->host_lock, flags);
1612 __shost_for_each_device(sdev, adapter->scsi_host) {
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001613 atomic_andnot(common_mask, &sdev_to_zfcp(sdev)->status);
Swen Schilligedaed852010-09-08 14:40:01 +02001614 if (clear_counter)
1615 atomic_set(&sdev_to_zfcp(sdev)->erp_counter, 0);
Swen Schilligecf0c772009-11-24 16:53:58 +01001616 }
Martin Peschke924dd582013-08-22 17:45:37 +02001617 spin_unlock_irqrestore(adapter->scsi_host->host_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618}
1619
Christof Schmitt287ac012008-07-02 10:56:40 +02001620/**
Swen Schilligedaed852010-09-08 14:40:01 +02001621 * zfcp_erp_set_port_status - set port status bits
1622 * @port: port to change the status
Christof Schmitt287ac012008-07-02 10:56:40 +02001623 * @mask: status bits to change
Christof Schmitt287ac012008-07-02 10:56:40 +02001624 *
Christof Schmittb62a8d92010-09-08 14:39:55 +02001625 * Changes in common status bits are propagated to attached LUNs.
Christof Schmitt287ac012008-07-02 10:56:40 +02001626 */
Swen Schilligedaed852010-09-08 14:40:01 +02001627void zfcp_erp_set_port_status(struct zfcp_port *port, u32 mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628{
Christof Schmittb62a8d92010-09-08 14:39:55 +02001629 struct scsi_device *sdev;
Christof Schmitt287ac012008-07-02 10:56:40 +02001630 u32 common_mask = mask & ZFCP_COMMON_FLAGS;
Martin Peschke924dd582013-08-22 17:45:37 +02001631 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001633 atomic_or(mask, &port->status);
Christof Schmitt287ac012008-07-02 10:56:40 +02001634
Swen Schilligedaed852010-09-08 14:40:01 +02001635 if (!common_mask)
1636 return;
1637
Martin Peschke924dd582013-08-22 17:45:37 +02001638 spin_lock_irqsave(port->adapter->scsi_host->host_lock, flags);
1639 __shost_for_each_device(sdev, port->adapter->scsi_host)
Swen Schilligedaed852010-09-08 14:40:01 +02001640 if (sdev_to_zfcp(sdev)->port == port)
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001641 atomic_or(common_mask,
Swen Schilligedaed852010-09-08 14:40:01 +02001642 &sdev_to_zfcp(sdev)->status);
Martin Peschke924dd582013-08-22 17:45:37 +02001643 spin_unlock_irqrestore(port->adapter->scsi_host->host_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644}
1645
Christof Schmitt287ac012008-07-02 10:56:40 +02001646/**
Swen Schilligedaed852010-09-08 14:40:01 +02001647 * zfcp_erp_clear_port_status - clear port status bits
1648 * @port: adapter to change the status
Christof Schmitt287ac012008-07-02 10:56:40 +02001649 * @mask: status bits to change
Swen Schilligedaed852010-09-08 14:40:01 +02001650 *
1651 * Changes in common status bits are propagated to attached LUNs.
Christof Schmitt287ac012008-07-02 10:56:40 +02001652 */
Swen Schilligedaed852010-09-08 14:40:01 +02001653void zfcp_erp_clear_port_status(struct zfcp_port *port, u32 mask)
1654{
1655 struct scsi_device *sdev;
1656 u32 common_mask = mask & ZFCP_COMMON_FLAGS;
1657 u32 clear_counter = mask & ZFCP_STATUS_COMMON_ERP_FAILED;
Martin Peschke924dd582013-08-22 17:45:37 +02001658 unsigned long flags;
Swen Schilligedaed852010-09-08 14:40:01 +02001659
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001660 atomic_andnot(mask, &port->status);
Swen Schilligedaed852010-09-08 14:40:01 +02001661
1662 if (!common_mask)
1663 return;
1664
1665 if (clear_counter)
1666 atomic_set(&port->erp_counter, 0);
1667
Martin Peschke924dd582013-08-22 17:45:37 +02001668 spin_lock_irqsave(port->adapter->scsi_host->host_lock, flags);
1669 __shost_for_each_device(sdev, port->adapter->scsi_host)
Swen Schilligedaed852010-09-08 14:40:01 +02001670 if (sdev_to_zfcp(sdev)->port == port) {
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001671 atomic_andnot(common_mask,
Swen Schilligedaed852010-09-08 14:40:01 +02001672 &sdev_to_zfcp(sdev)->status);
1673 if (clear_counter)
1674 atomic_set(&sdev_to_zfcp(sdev)->erp_counter, 0);
1675 }
Martin Peschke924dd582013-08-22 17:45:37 +02001676 spin_unlock_irqrestore(port->adapter->scsi_host->host_lock, flags);
Swen Schilligedaed852010-09-08 14:40:01 +02001677}
1678
1679/**
1680 * zfcp_erp_set_lun_status - set lun status bits
1681 * @sdev: SCSI device / lun to set the status bits
1682 * @mask: status bits to change
1683 */
1684void zfcp_erp_set_lun_status(struct scsi_device *sdev, u32 mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685{
Christof Schmittb62a8d92010-09-08 14:39:55 +02001686 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
1687
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001688 atomic_or(mask, &zfcp_sdev->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689}
1690
Christof Schmitt287ac012008-07-02 10:56:40 +02001691/**
Swen Schilligedaed852010-09-08 14:40:01 +02001692 * zfcp_erp_clear_lun_status - clear lun status bits
1693 * @sdev: SCSi device / lun to clear the status bits
1694 * @mask: status bits to change
Christof Schmitt287ac012008-07-02 10:56:40 +02001695 */
Swen Schilligedaed852010-09-08 14:40:01 +02001696void zfcp_erp_clear_lun_status(struct scsi_device *sdev, u32 mask)
Andreas Herrmannd736a27b2005-06-13 13:23:57 +02001697{
Swen Schilligedaed852010-09-08 14:40:01 +02001698 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
1699
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001700 atomic_andnot(mask, &zfcp_sdev->status);
Swen Schilligedaed852010-09-08 14:40:01 +02001701
1702 if (mask & ZFCP_STATUS_COMMON_ERP_FAILED)
1703 atomic_set(&zfcp_sdev->erp_counter, 0);
Andreas Herrmannd736a27b2005-06-13 13:23:57 +02001704}
1705