blob: e7e6b63905e20f04e0628883100da8972056e6b4 [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,
Christof Schmitt287ac012008-07-02 10:56:40 +020022 ZFCP_STATUS_ERP_DISMISSED = 0x00200000,
23 ZFCP_STATUS_ERP_LOWMEM = 0x00400000,
Christof Schmittfdbd1c52010-09-08 14:39:54 +020024 ZFCP_STATUS_ERP_NO_REF = 0x00800000,
Christof Schmitt287ac012008-07-02 10:56:40 +020025};
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
Christof Schmitt287ac012008-07-02 10:56:40 +020027enum zfcp_erp_steps {
28 ZFCP_ERP_STEP_UNINITIALIZED = 0x0000,
Christof Schmitt287ac012008-07-02 10:56:40 +020029 ZFCP_ERP_STEP_PHYS_PORT_CLOSING = 0x0010,
30 ZFCP_ERP_STEP_PORT_CLOSING = 0x0100,
Christof Schmitt287ac012008-07-02 10:56:40 +020031 ZFCP_ERP_STEP_PORT_OPENING = 0x0800,
Christof Schmittb62a8d92010-09-08 14:39:55 +020032 ZFCP_ERP_STEP_LUN_CLOSING = 0x1000,
33 ZFCP_ERP_STEP_LUN_OPENING = 0x2000,
Christof Schmitt287ac012008-07-02 10:56:40 +020034};
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Steffen Maier512857a72018-05-17 19:14:45 +020036/**
37 * enum zfcp_erp_act_type - Type of ERP action object.
38 * @ZFCP_ERP_ACTION_REOPEN_LUN: LUN recovery.
39 * @ZFCP_ERP_ACTION_REOPEN_PORT: Port recovery.
40 * @ZFCP_ERP_ACTION_REOPEN_PORT_FORCED: Forced port recovery.
41 * @ZFCP_ERP_ACTION_REOPEN_ADAPTER: Adapter recovery.
42 * @ZFCP_ERP_ACTION_NONE: Eyecatcher pseudo flag to bitwise or-combine with
Steffen Maierd70aab52018-05-17 19:14:47 +020043 * either of the first four enum values.
Steffen Maier512857a72018-05-17 19:14:45 +020044 * Used to indicate that an ERP action could not be
45 * set up despite a detected need for some recovery.
Steffen Maierd70aab52018-05-17 19:14:47 +020046 * @ZFCP_ERP_ACTION_FAILED: Eyecatcher pseudo flag to bitwise or-combine with
47 * either of the first four enum values.
48 * Used to indicate that ERP not needed because
49 * the object has ZFCP_STATUS_COMMON_ERP_FAILED.
Steffen Maier512857a72018-05-17 19:14:45 +020050 */
Christof Schmitt287ac012008-07-02 10:56:40 +020051enum zfcp_erp_act_type {
Christof Schmittb62a8d92010-09-08 14:39:55 +020052 ZFCP_ERP_ACTION_REOPEN_LUN = 1,
Christof Schmitt287ac012008-07-02 10:56:40 +020053 ZFCP_ERP_ACTION_REOPEN_PORT = 2,
54 ZFCP_ERP_ACTION_REOPEN_PORT_FORCED = 3,
55 ZFCP_ERP_ACTION_REOPEN_ADAPTER = 4,
Steffen Maier512857a72018-05-17 19:14:45 +020056 ZFCP_ERP_ACTION_NONE = 0xc0,
Steffen Maierd70aab52018-05-17 19:14:47 +020057 ZFCP_ERP_ACTION_FAILED = 0xe0,
Christof Schmitt287ac012008-07-02 10:56:40 +020058};
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Christof Schmitt287ac012008-07-02 10:56:40 +020060enum zfcp_erp_act_result {
61 ZFCP_ERP_SUCCEEDED = 0,
62 ZFCP_ERP_FAILED = 1,
63 ZFCP_ERP_CONTINUES = 2,
64 ZFCP_ERP_EXIT = 3,
65 ZFCP_ERP_DISMISSED = 4,
66 ZFCP_ERP_NOMEM = 5,
67};
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Christof Schmitt287ac012008-07-02 10:56:40 +020069static void zfcp_erp_adapter_block(struct zfcp_adapter *adapter, int mask)
Andreas Herrmann2abbe862006-09-18 22:29:56 +020070{
Swen Schilligedaed852010-09-08 14:40:01 +020071 zfcp_erp_clear_adapter_status(adapter,
72 ZFCP_STATUS_COMMON_UNBLOCKED | mask);
Andreas Herrmann2abbe862006-09-18 22:29:56 +020073}
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
Steffen Maier013af852018-05-17 19:15:00 +020075static bool zfcp_erp_action_is_running(struct zfcp_erp_action *act)
Linus Torvalds1da177e2005-04-16 15:20:36 -070076{
Christof Schmitt287ac012008-07-02 10:56:40 +020077 struct zfcp_erp_action *curr_act;
78
79 list_for_each_entry(curr_act, &act->adapter->erp_running_head, list)
80 if (act == curr_act)
Steffen Maier013af852018-05-17 19:15:00 +020081 return true;
82 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083}
84
Christof Schmitt287ac012008-07-02 10:56:40 +020085static void zfcp_erp_action_ready(struct zfcp_erp_action *act)
Linus Torvalds1da177e2005-04-16 15:20:36 -070086{
Christof Schmitt287ac012008-07-02 10:56:40 +020087 struct zfcp_adapter *adapter = act->adapter;
88
89 list_move(&act->list, &act->adapter->erp_ready_head);
Swen Schilligae0904f2010-12-02 15:16:12 +010090 zfcp_dbf_rec_run("erardy1", act);
Christof Schmitt347c6a92009-08-18 15:43:25 +020091 wake_up(&adapter->erp_ready_wq);
Swen Schilligae0904f2010-12-02 15:16:12 +010092 zfcp_dbf_rec_run("erardy2", act);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093}
94
Christof Schmitt287ac012008-07-02 10:56:40 +020095static void zfcp_erp_action_dismiss(struct zfcp_erp_action *act)
Linus Torvalds1da177e2005-04-16 15:20:36 -070096{
Christof Schmitt287ac012008-07-02 10:56:40 +020097 act->status |= ZFCP_STATUS_ERP_DISMISSED;
Steffen Maier013af852018-05-17 19:15:00 +020098 if (zfcp_erp_action_is_running(act))
Christof Schmitt287ac012008-07-02 10:56:40 +020099 zfcp_erp_action_ready(act);
100}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
Christof Schmittb62a8d92010-09-08 14:39:55 +0200102static void zfcp_erp_action_dismiss_lun(struct scsi_device *sdev)
Christof Schmitt287ac012008-07-02 10:56:40 +0200103{
Christof Schmittb62a8d92010-09-08 14:39:55 +0200104 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
105
106 if (atomic_read(&zfcp_sdev->status) & ZFCP_STATUS_COMMON_ERP_INUSE)
107 zfcp_erp_action_dismiss(&zfcp_sdev->erp_action);
Christof Schmitt287ac012008-07-02 10:56:40 +0200108}
109
110static void zfcp_erp_action_dismiss_port(struct zfcp_port *port)
111{
Christof Schmittb62a8d92010-09-08 14:39:55 +0200112 struct scsi_device *sdev;
Christof Schmitt287ac012008-07-02 10:56:40 +0200113
114 if (atomic_read(&port->status) & ZFCP_STATUS_COMMON_ERP_INUSE)
115 zfcp_erp_action_dismiss(&port->erp_action);
Martin Peschke924dd582013-08-22 17:45:37 +0200116 else {
117 spin_lock(port->adapter->scsi_host->host_lock);
118 __shost_for_each_device(sdev, port->adapter->scsi_host)
Christof Schmittb62a8d92010-09-08 14:39:55 +0200119 if (sdev_to_zfcp(sdev)->port == port)
120 zfcp_erp_action_dismiss_lun(sdev);
Martin Peschke924dd582013-08-22 17:45:37 +0200121 spin_unlock(port->adapter->scsi_host->host_lock);
122 }
Christof Schmitt287ac012008-07-02 10:56:40 +0200123}
124
125static void zfcp_erp_action_dismiss_adapter(struct zfcp_adapter *adapter)
126{
127 struct zfcp_port *port;
128
129 if (atomic_read(&adapter->status) & ZFCP_STATUS_COMMON_ERP_INUSE)
130 zfcp_erp_action_dismiss(&adapter->erp_action);
Swen Schilligecf0c772009-11-24 16:53:58 +0100131 else {
132 read_lock(&adapter->port_list_lock);
133 list_for_each_entry(port, &adapter->port_list, list)
Christof Schmitt287ac012008-07-02 10:56:40 +0200134 zfcp_erp_action_dismiss_port(port);
Swen Schilligecf0c772009-11-24 16:53:58 +0100135 read_unlock(&adapter->port_list_lock);
136 }
Christof Schmitt287ac012008-07-02 10:56:40 +0200137}
138
Steffen Maier8c3d20a2018-05-17 19:14:48 +0200139static int zfcp_erp_handle_failed(int want, struct zfcp_adapter *adapter,
140 struct zfcp_port *port,
141 struct scsi_device *sdev)
142{
143 int need = want;
144 struct zfcp_scsi_dev *zsdev;
145
146 switch (want) {
147 case ZFCP_ERP_ACTION_REOPEN_LUN:
148 zsdev = sdev_to_zfcp(sdev);
149 if (atomic_read(&zsdev->status) & ZFCP_STATUS_COMMON_ERP_FAILED)
150 need = 0;
151 break;
152 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
153 if (atomic_read(&port->status) & ZFCP_STATUS_COMMON_ERP_FAILED)
154 need = 0;
155 break;
156 case ZFCP_ERP_ACTION_REOPEN_PORT:
157 if (atomic_read(&port->status) &
158 ZFCP_STATUS_COMMON_ERP_FAILED) {
159 need = 0;
160 /* ensure propagation of failed status to new devices */
161 zfcp_erp_set_port_status(
162 port, ZFCP_STATUS_COMMON_ERP_FAILED);
163 }
164 break;
165 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
166 if (atomic_read(&adapter->status) &
167 ZFCP_STATUS_COMMON_ERP_FAILED) {
168 need = 0;
169 /* ensure propagation of failed status to new devices */
170 zfcp_erp_set_adapter_status(
171 adapter, ZFCP_STATUS_COMMON_ERP_FAILED);
172 }
173 break;
174 default:
175 need = 0;
176 break;
177 }
178
179 return need;
180}
181
Christof Schmitt287ac012008-07-02 10:56:40 +0200182static int zfcp_erp_required_act(int want, struct zfcp_adapter *adapter,
183 struct zfcp_port *port,
Christof Schmittb62a8d92010-09-08 14:39:55 +0200184 struct scsi_device *sdev)
Christof Schmitt287ac012008-07-02 10:56:40 +0200185{
186 int need = want;
Christof Schmittb62a8d92010-09-08 14:39:55 +0200187 int l_status, p_status, a_status;
188 struct zfcp_scsi_dev *zfcp_sdev;
Christof Schmitt287ac012008-07-02 10:56:40 +0200189
190 switch (want) {
Christof Schmittb62a8d92010-09-08 14:39:55 +0200191 case ZFCP_ERP_ACTION_REOPEN_LUN:
192 zfcp_sdev = sdev_to_zfcp(sdev);
193 l_status = atomic_read(&zfcp_sdev->status);
194 if (l_status & ZFCP_STATUS_COMMON_ERP_INUSE)
Christof Schmitt287ac012008-07-02 10:56:40 +0200195 return 0;
196 p_status = atomic_read(&port->status);
197 if (!(p_status & ZFCP_STATUS_COMMON_RUNNING) ||
198 p_status & ZFCP_STATUS_COMMON_ERP_FAILED)
199 return 0;
200 if (!(p_status & ZFCP_STATUS_COMMON_UNBLOCKED))
201 need = ZFCP_ERP_ACTION_REOPEN_PORT;
202 /* fall through */
Christof Schmitt287ac012008-07-02 10:56:40 +0200203 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
204 p_status = atomic_read(&port->status);
Christof Schmitt097ef3b2010-07-08 09:53:06 +0200205 if (!(p_status & ZFCP_STATUS_COMMON_OPEN))
206 need = ZFCP_ERP_ACTION_REOPEN_PORT;
207 /* fall through */
208 case ZFCP_ERP_ACTION_REOPEN_PORT:
209 p_status = atomic_read(&port->status);
Christof Schmitt287ac012008-07-02 10:56:40 +0200210 if (p_status & ZFCP_STATUS_COMMON_ERP_INUSE)
211 return 0;
212 a_status = atomic_read(&adapter->status);
213 if (!(a_status & ZFCP_STATUS_COMMON_RUNNING) ||
214 a_status & ZFCP_STATUS_COMMON_ERP_FAILED)
215 return 0;
Swen Schilligd3e10882010-11-17 14:23:42 +0100216 if (p_status & ZFCP_STATUS_COMMON_NOESC)
217 return need;
Christof Schmitt287ac012008-07-02 10:56:40 +0200218 if (!(a_status & ZFCP_STATUS_COMMON_UNBLOCKED))
219 need = ZFCP_ERP_ACTION_REOPEN_ADAPTER;
220 /* fall through */
221 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
222 a_status = atomic_read(&adapter->status);
223 if (a_status & ZFCP_STATUS_COMMON_ERP_INUSE)
224 return 0;
Christof Schmitt143bb6b2009-08-18 15:43:27 +0200225 if (!(a_status & ZFCP_STATUS_COMMON_RUNNING) &&
226 !(a_status & ZFCP_STATUS_COMMON_OPEN))
227 return 0; /* shutdown requested for closed adapter */
Christof Schmitt287ac012008-07-02 10:56:40 +0200228 }
229
230 return need;
231}
232
Christof Schmittfdbd1c52010-09-08 14:39:54 +0200233static struct zfcp_erp_action *zfcp_erp_setup_act(int need, u32 act_status,
Christof Schmitt287ac012008-07-02 10:56:40 +0200234 struct zfcp_adapter *adapter,
235 struct zfcp_port *port,
Christof Schmittb62a8d92010-09-08 14:39:55 +0200236 struct scsi_device *sdev)
Christof Schmitt287ac012008-07-02 10:56:40 +0200237{
238 struct zfcp_erp_action *erp_action;
Christof Schmittb62a8d92010-09-08 14:39:55 +0200239 struct zfcp_scsi_dev *zfcp_sdev;
Christof Schmitt287ac012008-07-02 10:56:40 +0200240
241 switch (need) {
Christof Schmittb62a8d92010-09-08 14:39:55 +0200242 case ZFCP_ERP_ACTION_REOPEN_LUN:
243 zfcp_sdev = sdev_to_zfcp(sdev);
Christof Schmittfdbd1c52010-09-08 14:39:54 +0200244 if (!(act_status & ZFCP_STATUS_ERP_NO_REF))
Christof Schmittb62a8d92010-09-08 14:39:55 +0200245 if (scsi_device_get(sdev))
Christof Schmittfdbd1c52010-09-08 14:39:54 +0200246 return NULL;
Peter Zijlstra805de8f42015-04-24 01:12:32 +0200247 atomic_or(ZFCP_STATUS_COMMON_ERP_INUSE,
Christof Schmittb62a8d92010-09-08 14:39:55 +0200248 &zfcp_sdev->status);
249 erp_action = &zfcp_sdev->erp_action;
Steffen Maierab31fd02017-10-13 15:40:07 +0200250 WARN_ON_ONCE(erp_action->port != port);
251 WARN_ON_ONCE(erp_action->sdev != sdev);
Christof Schmittb62a8d92010-09-08 14:39:55 +0200252 if (!(atomic_read(&zfcp_sdev->status) &
253 ZFCP_STATUS_COMMON_RUNNING))
Christof Schmittfdbd1c52010-09-08 14:39:54 +0200254 act_status |= ZFCP_STATUS_ERP_CLOSE_ONLY;
Christof Schmitt287ac012008-07-02 10:56:40 +0200255 break;
256
257 case ZFCP_ERP_ACTION_REOPEN_PORT:
258 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
Christof Schmitt615f59e2010-02-17 11:18:56 +0100259 if (!get_device(&port->dev))
Swen Schillig6b1833342009-11-24 16:54:05 +0100260 return NULL;
Christof Schmitt287ac012008-07-02 10:56:40 +0200261 zfcp_erp_action_dismiss_port(port);
Peter Zijlstra805de8f42015-04-24 01:12:32 +0200262 atomic_or(ZFCP_STATUS_COMMON_ERP_INUSE, &port->status);
Christof Schmitt287ac012008-07-02 10:56:40 +0200263 erp_action = &port->erp_action;
Steffen Maierab31fd02017-10-13 15:40:07 +0200264 WARN_ON_ONCE(erp_action->port != port);
265 WARN_ON_ONCE(erp_action->sdev != NULL);
Christof Schmitt287ac012008-07-02 10:56:40 +0200266 if (!(atomic_read(&port->status) & ZFCP_STATUS_COMMON_RUNNING))
Christof Schmittfdbd1c52010-09-08 14:39:54 +0200267 act_status |= ZFCP_STATUS_ERP_CLOSE_ONLY;
Christof Schmitt287ac012008-07-02 10:56:40 +0200268 break;
269
270 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
Swen Schilligf3450c72009-11-24 16:53:59 +0100271 kref_get(&adapter->ref);
Christof Schmitt287ac012008-07-02 10:56:40 +0200272 zfcp_erp_action_dismiss_adapter(adapter);
Peter Zijlstra805de8f42015-04-24 01:12:32 +0200273 atomic_or(ZFCP_STATUS_COMMON_ERP_INUSE, &adapter->status);
Christof Schmitt287ac012008-07-02 10:56:40 +0200274 erp_action = &adapter->erp_action;
Steffen Maierab31fd02017-10-13 15:40:07 +0200275 WARN_ON_ONCE(erp_action->port != NULL);
276 WARN_ON_ONCE(erp_action->sdev != NULL);
Christof Schmitt287ac012008-07-02 10:56:40 +0200277 if (!(atomic_read(&adapter->status) &
278 ZFCP_STATUS_COMMON_RUNNING))
Christof Schmittfdbd1c52010-09-08 14:39:54 +0200279 act_status |= ZFCP_STATUS_ERP_CLOSE_ONLY;
Christof Schmitt287ac012008-07-02 10:56:40 +0200280 break;
281
282 default:
283 return NULL;
284 }
285
Steffen Maierab31fd02017-10-13 15:40:07 +0200286 WARN_ON_ONCE(erp_action->adapter != adapter);
287 memset(&erp_action->list, 0, sizeof(erp_action->list));
288 memset(&erp_action->timer, 0, sizeof(erp_action->timer));
289 erp_action->step = ZFCP_ERP_STEP_UNINITIALIZED;
290 erp_action->fsf_req_id = 0;
Christof Schmitt287ac012008-07-02 10:56:40 +0200291 erp_action->action = need;
Christof Schmittfdbd1c52010-09-08 14:39:54 +0200292 erp_action->status = act_status;
Christof Schmitt287ac012008-07-02 10:56:40 +0200293
294 return erp_action;
295}
296
Steffen Maier2fdd45f2018-05-17 19:15:01 +0200297static void zfcp_erp_action_enqueue(int want, struct zfcp_adapter *adapter,
298 struct zfcp_port *port,
299 struct scsi_device *sdev,
300 char *id, u32 act_status)
Christof Schmitt287ac012008-07-02 10:56:40 +0200301{
Steffen Maier2fdd45f2018-05-17 19:15:01 +0200302 int need;
Swen Schilligae0904f2010-12-02 15:16:12 +0100303 struct zfcp_erp_action *act;
Christof Schmitt287ac012008-07-02 10:56:40 +0200304
Steffen Maier8c3d20a2018-05-17 19:14:48 +0200305 need = zfcp_erp_handle_failed(want, adapter, port, sdev);
306 if (!need) {
307 need = ZFCP_ERP_ACTION_FAILED; /* marker for trace */
308 goto out;
309 }
310
Steffen Maier6a765502018-05-17 19:14:49 +0200311 if (!adapter->erp_thread) {
312 need = ZFCP_ERP_ACTION_NONE; /* marker for trace */
Steffen Maier6a765502018-05-17 19:14:49 +0200313 goto out;
314 }
Christof Schmitt287ac012008-07-02 10:56:40 +0200315
Christof Schmittb62a8d92010-09-08 14:39:55 +0200316 need = zfcp_erp_required_act(want, adapter, port, sdev);
Christof Schmitt287ac012008-07-02 10:56:40 +0200317 if (!need)
318 goto out;
319
Christof Schmittb62a8d92010-09-08 14:39:55 +0200320 act = zfcp_erp_setup_act(need, act_status, adapter, port, sdev);
Steffen Maier512857a72018-05-17 19:14:45 +0200321 if (!act) {
322 need |= ZFCP_ERP_ACTION_NONE; /* marker for trace */
Christof Schmitt287ac012008-07-02 10:56:40 +0200323 goto out;
Steffen Maier512857a72018-05-17 19:14:45 +0200324 }
Peter Zijlstra805de8f42015-04-24 01:12:32 +0200325 atomic_or(ZFCP_STATUS_ADAPTER_ERP_PENDING, &adapter->status);
Christof Schmitt287ac012008-07-02 10:56:40 +0200326 ++adapter->erp_total_count;
327 list_add_tail(&act->list, &adapter->erp_ready_head);
Christof Schmitt347c6a92009-08-18 15:43:25 +0200328 wake_up(&adapter->erp_ready_wq);
Christof Schmitt287ac012008-07-02 10:56:40 +0200329 out:
Swen Schilligae0904f2010-12-02 15:16:12 +0100330 zfcp_dbf_rec_trig(id, adapter, port, sdev, want, need);
Christof Schmitt287ac012008-07-02 10:56:40 +0200331}
332
Steffen Maier96d92702018-05-17 19:14:46 +0200333void zfcp_erp_port_forced_no_port_dbf(char *id, struct zfcp_adapter *adapter,
334 u64 port_name, u32 port_id)
335{
336 unsigned long flags;
337 static /* don't waste stack */ struct zfcp_port tmpport;
338
339 write_lock_irqsave(&adapter->erp_lock, flags);
340 /* Stand-in zfcp port with fields just good enough for
341 * zfcp_dbf_rec_trig() and zfcp_dbf_set_common().
342 * Under lock because tmpport is static.
343 */
344 atomic_set(&tmpport.status, -1); /* unknown */
345 tmpport.wwpn = port_name;
346 tmpport.d_id = port_id;
347 zfcp_dbf_rec_trig(id, adapter, &tmpport, NULL,
348 ZFCP_ERP_ACTION_REOPEN_PORT_FORCED,
349 ZFCP_ERP_ACTION_NONE);
350 write_unlock_irqrestore(&adapter->erp_lock, flags);
351}
352
Steffen Maier2fdd45f2018-05-17 19:15:01 +0200353static void _zfcp_erp_adapter_reopen(struct zfcp_adapter *adapter,
Swen Schilligea4a3a62010-12-02 15:16:16 +0100354 int clear_mask, char *id)
Christof Schmitt287ac012008-07-02 10:56:40 +0200355{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 zfcp_erp_adapter_block(adapter, clear_mask);
Christof Schmitta2fa0ae2009-03-02 13:09:08 +0100357 zfcp_scsi_schedule_rports_block(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
Steffen Maier2fdd45f2018-05-17 19:15:01 +0200359 zfcp_erp_action_enqueue(ZFCP_ERP_ACTION_REOPEN_ADAPTER,
360 adapter, NULL, NULL, id, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361}
362
363/**
Christof Schmitt287ac012008-07-02 10:56:40 +0200364 * zfcp_erp_adapter_reopen - Reopen adapter.
365 * @adapter: Adapter to reopen.
366 * @clear: Status flags to clear.
367 * @id: Id for debug trace event.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 */
Swen Schilligea4a3a62010-12-02 15:16:16 +0100369void zfcp_erp_adapter_reopen(struct zfcp_adapter *adapter, int clear, char *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370{
Christof Schmitt287ac012008-07-02 10:56:40 +0200371 unsigned long flags;
372
Swen Schilligecf0c772009-11-24 16:53:58 +0100373 zfcp_erp_adapter_block(adapter, clear);
374 zfcp_scsi_schedule_rports_block(adapter);
375
376 write_lock_irqsave(&adapter->erp_lock, flags);
Steffen Maier8c3d20a2018-05-17 19:14:48 +0200377 zfcp_erp_action_enqueue(ZFCP_ERP_ACTION_REOPEN_ADAPTER, adapter,
378 NULL, NULL, id, 0);
Swen Schilligecf0c772009-11-24 16:53:58 +0100379 write_unlock_irqrestore(&adapter->erp_lock, flags);
Christof Schmitt287ac012008-07-02 10:56:40 +0200380}
381
382/**
383 * zfcp_erp_adapter_shutdown - Shutdown adapter.
384 * @adapter: Adapter to shut down.
385 * @clear: Status flags to clear.
386 * @id: Id for debug trace event.
Christof Schmitt287ac012008-07-02 10:56:40 +0200387 */
388void zfcp_erp_adapter_shutdown(struct zfcp_adapter *adapter, int clear,
Swen Schilligea4a3a62010-12-02 15:16:16 +0100389 char *id)
Christof Schmitt287ac012008-07-02 10:56:40 +0200390{
391 int flags = ZFCP_STATUS_COMMON_RUNNING | ZFCP_STATUS_COMMON_ERP_FAILED;
Swen Schilligea4a3a62010-12-02 15:16:16 +0100392 zfcp_erp_adapter_reopen(adapter, clear | flags, id);
Christof Schmitt287ac012008-07-02 10:56:40 +0200393}
394
395/**
396 * zfcp_erp_port_shutdown - Shutdown port
397 * @port: Port to shut down.
398 * @clear: Status flags to clear.
399 * @id: Id for debug trace event.
Christof Schmitt287ac012008-07-02 10:56:40 +0200400 */
Swen Schilligea4a3a62010-12-02 15:16:16 +0100401void zfcp_erp_port_shutdown(struct zfcp_port *port, int clear, char *id)
Christof Schmitt287ac012008-07-02 10:56:40 +0200402{
403 int flags = ZFCP_STATUS_COMMON_RUNNING | ZFCP_STATUS_COMMON_ERP_FAILED;
Swen Schilligea4a3a62010-12-02 15:16:16 +0100404 zfcp_erp_port_reopen(port, clear | flags, id);
Christof Schmitt287ac012008-07-02 10:56:40 +0200405}
406
Christof Schmitt287ac012008-07-02 10:56:40 +0200407static void zfcp_erp_port_block(struct zfcp_port *port, int clear)
408{
Swen Schilligedaed852010-09-08 14:40:01 +0200409 zfcp_erp_clear_port_status(port,
410 ZFCP_STATUS_COMMON_UNBLOCKED | clear);
Christof Schmitt287ac012008-07-02 10:56:40 +0200411}
412
Swen Schilligea4a3a62010-12-02 15:16:16 +0100413static void _zfcp_erp_port_forced_reopen(struct zfcp_port *port, int clear,
414 char *id)
Christof Schmitt287ac012008-07-02 10:56:40 +0200415{
416 zfcp_erp_port_block(port, clear);
Christof Schmitta2fa0ae2009-03-02 13:09:08 +0100417 zfcp_scsi_schedule_rport_block(port);
Christof Schmitt287ac012008-07-02 10:56:40 +0200418
Christof Schmitt287ac012008-07-02 10:56:40 +0200419 zfcp_erp_action_enqueue(ZFCP_ERP_ACTION_REOPEN_PORT_FORCED,
Swen Schilligea4a3a62010-12-02 15:16:16 +0100420 port->adapter, port, NULL, id, 0);
Christof Schmitt287ac012008-07-02 10:56:40 +0200421}
422
423/**
424 * zfcp_erp_port_forced_reopen - Forced close of port and open again
425 * @port: Port to force close and to reopen.
Swen Schilligea4a3a62010-12-02 15:16:16 +0100426 * @clear: Status flags to clear.
Christof Schmitt287ac012008-07-02 10:56:40 +0200427 * @id: Id for debug trace event.
Christof Schmitt287ac012008-07-02 10:56:40 +0200428 */
Swen Schilligea4a3a62010-12-02 15:16:16 +0100429void zfcp_erp_port_forced_reopen(struct zfcp_port *port, int clear, char *id)
Christof Schmitt287ac012008-07-02 10:56:40 +0200430{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 unsigned long flags;
432 struct zfcp_adapter *adapter = port->adapter;
433
Swen Schilligecf0c772009-11-24 16:53:58 +0100434 write_lock_irqsave(&adapter->erp_lock, flags);
Swen Schilligea4a3a62010-12-02 15:16:16 +0100435 _zfcp_erp_port_forced_reopen(port, clear, id);
Swen Schilligecf0c772009-11-24 16:53:58 +0100436 write_unlock_irqrestore(&adapter->erp_lock, flags);
Christof Schmitt287ac012008-07-02 10:56:40 +0200437}
438
Steffen Maier2fdd45f2018-05-17 19:15:01 +0200439static void _zfcp_erp_port_reopen(struct zfcp_port *port, int clear, char *id)
Christof Schmitt287ac012008-07-02 10:56:40 +0200440{
441 zfcp_erp_port_block(port, clear);
Christof Schmitta2fa0ae2009-03-02 13:09:08 +0100442 zfcp_scsi_schedule_rport_block(port);
Christof Schmitt287ac012008-07-02 10:56:40 +0200443
Steffen Maier2fdd45f2018-05-17 19:15:01 +0200444 zfcp_erp_action_enqueue(ZFCP_ERP_ACTION_REOPEN_PORT,
445 port->adapter, port, NULL, id, 0);
Christof Schmitt287ac012008-07-02 10:56:40 +0200446}
447
448/**
449 * zfcp_erp_port_reopen - trigger remote port recovery
450 * @port: port to recover
451 * @clear_mask: flags in port status to be cleared
Swen Schilligea4a3a62010-12-02 15:16:16 +0100452 * @id: Id for debug trace event.
Christof Schmitt287ac012008-07-02 10:56:40 +0200453 */
Steffen Maier2fdd45f2018-05-17 19:15:01 +0200454void zfcp_erp_port_reopen(struct zfcp_port *port, int clear, char *id)
Christof Schmitt287ac012008-07-02 10:56:40 +0200455{
Swen Schilligecf0c772009-11-24 16:53:58 +0100456 unsigned long flags;
Christof Schmitt287ac012008-07-02 10:56:40 +0200457 struct zfcp_adapter *adapter = port->adapter;
458
Swen Schilligecf0c772009-11-24 16:53:58 +0100459 write_lock_irqsave(&adapter->erp_lock, flags);
Steffen Maier2fdd45f2018-05-17 19:15:01 +0200460 _zfcp_erp_port_reopen(port, clear, id);
Swen Schilligecf0c772009-11-24 16:53:58 +0100461 write_unlock_irqrestore(&adapter->erp_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462}
463
Christof Schmittb62a8d92010-09-08 14:39:55 +0200464static void zfcp_erp_lun_block(struct scsi_device *sdev, int clear_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465{
Swen Schilligedaed852010-09-08 14:40:01 +0200466 zfcp_erp_clear_lun_status(sdev,
467 ZFCP_STATUS_COMMON_UNBLOCKED | clear_mask);
Christof Schmitt287ac012008-07-02 10:56:40 +0200468}
469
Christof Schmittb62a8d92010-09-08 14:39:55 +0200470static void _zfcp_erp_lun_reopen(struct scsi_device *sdev, int clear, char *id,
Swen Schilligea4a3a62010-12-02 15:16:16 +0100471 u32 act_status)
Christof Schmitt287ac012008-07-02 10:56:40 +0200472{
Christof Schmittb62a8d92010-09-08 14:39:55 +0200473 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
474 struct zfcp_adapter *adapter = zfcp_sdev->port->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
Christof Schmittb62a8d92010-09-08 14:39:55 +0200476 zfcp_erp_lun_block(sdev, clear);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477
Christof Schmittb62a8d92010-09-08 14:39:55 +0200478 zfcp_erp_action_enqueue(ZFCP_ERP_ACTION_REOPEN_LUN, adapter,
Swen Schilligea4a3a62010-12-02 15:16:16 +0100479 zfcp_sdev->port, sdev, id, act_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480}
481
482/**
Christof Schmittb62a8d92010-09-08 14:39:55 +0200483 * zfcp_erp_lun_reopen - initiate reopen of a LUN
484 * @sdev: SCSI device / LUN to be reopened
485 * @clear_mask: specifies flags in LUN status to be cleared
Swen Schilligea4a3a62010-12-02 15:16:16 +0100486 * @id: Id for debug trace event.
487 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 * Return: 0 on success, < 0 on error
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 */
Swen Schilligea4a3a62010-12-02 15:16:16 +0100490void zfcp_erp_lun_reopen(struct scsi_device *sdev, int clear, char *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 unsigned long flags;
Christof Schmittb62a8d92010-09-08 14:39:55 +0200493 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
494 struct zfcp_port *port = zfcp_sdev->port;
Christof Schmitt287ac012008-07-02 10:56:40 +0200495 struct zfcp_adapter *adapter = port->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
Swen Schilligecf0c772009-11-24 16:53:58 +0100497 write_lock_irqsave(&adapter->erp_lock, flags);
Swen Schilligea4a3a62010-12-02 15:16:16 +0100498 _zfcp_erp_lun_reopen(sdev, clear, id, 0);
Swen Schilligecf0c772009-11-24 16:53:58 +0100499 write_unlock_irqrestore(&adapter->erp_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500}
501
Christof Schmittfdbd1c52010-09-08 14:39:54 +0200502/**
Christof Schmittb62a8d92010-09-08 14:39:55 +0200503 * zfcp_erp_lun_shutdown - Shutdown LUN
504 * @sdev: SCSI device / LUN to shut down.
Christof Schmittfdbd1c52010-09-08 14:39:54 +0200505 * @clear: Status flags to clear.
506 * @id: Id for debug trace event.
Christof Schmittfdbd1c52010-09-08 14:39:54 +0200507 */
Swen Schilligea4a3a62010-12-02 15:16:16 +0100508void zfcp_erp_lun_shutdown(struct scsi_device *sdev, int clear, char *id)
Christof Schmittfdbd1c52010-09-08 14:39:54 +0200509{
510 int flags = ZFCP_STATUS_COMMON_RUNNING | ZFCP_STATUS_COMMON_ERP_FAILED;
Swen Schilligea4a3a62010-12-02 15:16:16 +0100511 zfcp_erp_lun_reopen(sdev, clear | flags, id);
Christof Schmittfdbd1c52010-09-08 14:39:54 +0200512}
513
514/**
Christof Schmittb62a8d92010-09-08 14:39:55 +0200515 * zfcp_erp_lun_shutdown_wait - Shutdown LUN and wait for erp completion
516 * @sdev: SCSI device / LUN to shut down.
Christof Schmittfdbd1c52010-09-08 14:39:54 +0200517 * @id: Id for debug trace event.
518 *
Christof Schmittb62a8d92010-09-08 14:39:55 +0200519 * Do not acquire a reference for the LUN when creating the ERP
Christof Schmittfdbd1c52010-09-08 14:39:54 +0200520 * action. It is safe, because this function waits for the ERP to
Christof Schmittb62a8d92010-09-08 14:39:55 +0200521 * complete first. This allows to shutdown the LUN, even when the SCSI
522 * device is in the state SDEV_DEL when scsi_device_get will fail.
Christof Schmittfdbd1c52010-09-08 14:39:54 +0200523 */
Christof Schmittb62a8d92010-09-08 14:39:55 +0200524void zfcp_erp_lun_shutdown_wait(struct scsi_device *sdev, char *id)
Christof Schmittfdbd1c52010-09-08 14:39:54 +0200525{
526 unsigned long flags;
Christof Schmittb62a8d92010-09-08 14:39:55 +0200527 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
528 struct zfcp_port *port = zfcp_sdev->port;
Christof Schmittfdbd1c52010-09-08 14:39:54 +0200529 struct zfcp_adapter *adapter = port->adapter;
530 int clear = ZFCP_STATUS_COMMON_RUNNING | ZFCP_STATUS_COMMON_ERP_FAILED;
531
532 write_lock_irqsave(&adapter->erp_lock, flags);
Swen Schilligea4a3a62010-12-02 15:16:16 +0100533 _zfcp_erp_lun_reopen(sdev, clear, id, ZFCP_STATUS_ERP_NO_REF);
Christof Schmittfdbd1c52010-09-08 14:39:54 +0200534 write_unlock_irqrestore(&adapter->erp_lock, flags);
535
536 zfcp_erp_wait(adapter);
537}
538
Steffen Maierd39eda52018-05-17 19:14:58 +0200539static int zfcp_erp_status_change_set(unsigned long mask, atomic_t *status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540{
Christof Schmitt287ac012008-07-02 10:56:40 +0200541 return (atomic_read(status) ^ mask) & mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542}
543
Andreas Herrmannf6c0e7a2006-08-02 11:05:52 +0200544static void zfcp_erp_adapter_unblock(struct zfcp_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545{
Steffen Maierd39eda52018-05-17 19:14:58 +0200546 if (zfcp_erp_status_change_set(ZFCP_STATUS_COMMON_UNBLOCKED,
547 &adapter->status))
Swen Schilligae0904f2010-12-02 15:16:12 +0100548 zfcp_dbf_rec_run("eraubl1", &adapter->erp_action);
Peter Zijlstra805de8f42015-04-24 01:12:32 +0200549 atomic_or(ZFCP_STATUS_COMMON_UNBLOCKED, &adapter->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550}
551
Christof Schmitt287ac012008-07-02 10:56:40 +0200552static void zfcp_erp_port_unblock(struct zfcp_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553{
Steffen Maierd39eda52018-05-17 19:14:58 +0200554 if (zfcp_erp_status_change_set(ZFCP_STATUS_COMMON_UNBLOCKED,
555 &port->status))
Swen Schilligae0904f2010-12-02 15:16:12 +0100556 zfcp_dbf_rec_run("erpubl1", &port->erp_action);
Peter Zijlstra805de8f42015-04-24 01:12:32 +0200557 atomic_or(ZFCP_STATUS_COMMON_UNBLOCKED, &port->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558}
559
Christof Schmittb62a8d92010-09-08 14:39:55 +0200560static void zfcp_erp_lun_unblock(struct scsi_device *sdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561{
Christof Schmittb62a8d92010-09-08 14:39:55 +0200562 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
563
Steffen Maierd39eda52018-05-17 19:14:58 +0200564 if (zfcp_erp_status_change_set(ZFCP_STATUS_COMMON_UNBLOCKED,
565 &zfcp_sdev->status))
Swen Schilligae0904f2010-12-02 15:16:12 +0100566 zfcp_dbf_rec_run("erlubl1", &sdev_to_zfcp(sdev)->erp_action);
Peter Zijlstra805de8f42015-04-24 01:12:32 +0200567 atomic_or(ZFCP_STATUS_COMMON_UNBLOCKED, &zfcp_sdev->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568}
569
Christof Schmitt287ac012008-07-02 10:56:40 +0200570static void zfcp_erp_action_to_running(struct zfcp_erp_action *erp_action)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571{
Christof Schmitt287ac012008-07-02 10:56:40 +0200572 list_move(&erp_action->list, &erp_action->adapter->erp_running_head);
Swen Schilligae0904f2010-12-02 15:16:12 +0100573 zfcp_dbf_rec_run("erator1", erp_action);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574}
575
Christof Schmitt287ac012008-07-02 10:56:40 +0200576static void zfcp_erp_strategy_check_fsfreq(struct zfcp_erp_action *act)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577{
Christof Schmitt287ac012008-07-02 10:56:40 +0200578 struct zfcp_adapter *adapter = act->adapter;
Christof Schmitte60a6d62010-02-17 11:18:49 +0100579 struct zfcp_fsf_req *req;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
Christof Schmitte60a6d62010-02-17 11:18:49 +0100581 if (!act->fsf_req_id)
Christof Schmitt287ac012008-07-02 10:56:40 +0200582 return;
583
Christof Schmittb6bd2fb2010-02-17 11:18:50 +0100584 spin_lock(&adapter->req_list->lock);
585 req = _zfcp_reqlist_find(adapter->req_list, act->fsf_req_id);
Christof Schmitte60a6d62010-02-17 11:18:49 +0100586 if (req && req->erp_action == act) {
Christof Schmitt287ac012008-07-02 10:56:40 +0200587 if (act->status & (ZFCP_STATUS_ERP_DISMISSED |
588 ZFCP_STATUS_ERP_TIMEDOUT)) {
Christof Schmitte60a6d62010-02-17 11:18:49 +0100589 req->status |= ZFCP_STATUS_FSFREQ_DISMISSED;
Swen Schilligae0904f2010-12-02 15:16:12 +0100590 zfcp_dbf_rec_run("erscf_1", act);
Christof Schmitte60a6d62010-02-17 11:18:49 +0100591 req->erp_action = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 }
Christof Schmitt287ac012008-07-02 10:56:40 +0200593 if (act->status & ZFCP_STATUS_ERP_TIMEDOUT)
Swen Schilligae0904f2010-12-02 15:16:12 +0100594 zfcp_dbf_rec_run("erscf_2", act);
Christof Schmitte60a6d62010-02-17 11:18:49 +0100595 if (req->status & ZFCP_STATUS_FSFREQ_DISMISSED)
596 act->fsf_req_id = 0;
Christof Schmitt287ac012008-07-02 10:56:40 +0200597 } else
Christof Schmitte60a6d62010-02-17 11:18:49 +0100598 act->fsf_req_id = 0;
Christof Schmittb6bd2fb2010-02-17 11:18:50 +0100599 spin_unlock(&adapter->req_list->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600}
601
Andreas Herrmannf6c0e7a2006-08-02 11:05:52 +0200602/**
Christof Schmitt287ac012008-07-02 10:56:40 +0200603 * zfcp_erp_notify - Trigger ERP action.
604 * @erp_action: ERP action to continue.
605 * @set_mask: ERP action status flags to set.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 */
Christof Schmitt287ac012008-07-02 10:56:40 +0200607void zfcp_erp_notify(struct zfcp_erp_action *erp_action, unsigned long set_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608{
609 struct zfcp_adapter *adapter = erp_action->adapter;
610 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
612 write_lock_irqsave(&adapter->erp_lock, flags);
Steffen Maier013af852018-05-17 19:15:00 +0200613 if (zfcp_erp_action_is_running(erp_action)) {
Christof Schmitt287ac012008-07-02 10:56:40 +0200614 erp_action->status |= set_mask;
615 zfcp_erp_action_ready(erp_action);
616 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 write_unlock_irqrestore(&adapter->erp_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618}
619
Andreas Herrmannf6c0e7a2006-08-02 11:05:52 +0200620/**
Christof Schmitt287ac012008-07-02 10:56:40 +0200621 * zfcp_erp_timeout_handler - Trigger ERP action from timed out ERP request
622 * @data: ERP action (from timer data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 */
Kees Cook75492a52017-10-16 16:44:34 -0700624void zfcp_erp_timeout_handler(struct timer_list *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625{
Kees Cook75492a52017-10-16 16:44:34 -0700626 struct zfcp_fsf_req *fsf_req = from_timer(fsf_req, t, timer);
627 struct zfcp_erp_action *act = fsf_req->erp_action;
Steffen Maier5c13db92017-10-17 18:40:51 +0200628
Christof Schmitt287ac012008-07-02 10:56:40 +0200629 zfcp_erp_notify(act, ZFCP_STATUS_ERP_TIMEDOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630}
631
Kees Cook75492a52017-10-16 16:44:34 -0700632static void zfcp_erp_memwait_handler(struct timer_list *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633{
Kees Cook75492a52017-10-16 16:44:34 -0700634 struct zfcp_erp_action *act = from_timer(act, t, timer);
635
636 zfcp_erp_notify(act, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637}
638
Christof Schmitt287ac012008-07-02 10:56:40 +0200639static void zfcp_erp_strategy_memwait(struct zfcp_erp_action *erp_action)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640{
Steffen Maier5c13db92017-10-17 18:40:51 +0200641 timer_setup(&erp_action->timer, zfcp_erp_memwait_handler, 0);
Christof Schmitt287ac012008-07-02 10:56:40 +0200642 erp_action->timer.expires = jiffies + HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 add_timer(&erp_action->timer);
Christof Schmitt287ac012008-07-02 10:56:40 +0200644}
645
646static void _zfcp_erp_port_reopen_all(struct zfcp_adapter *adapter,
Swen Schilligea4a3a62010-12-02 15:16:16 +0100647 int clear, char *id)
Christof Schmitt287ac012008-07-02 10:56:40 +0200648{
649 struct zfcp_port *port;
650
Swen Schilligecf0c772009-11-24 16:53:58 +0100651 read_lock(&adapter->port_list_lock);
652 list_for_each_entry(port, &adapter->port_list, list)
Swen Schilligea4a3a62010-12-02 15:16:16 +0100653 _zfcp_erp_port_reopen(port, clear, id);
Swen Schilligecf0c772009-11-24 16:53:58 +0100654 read_unlock(&adapter->port_list_lock);
Christof Schmitt287ac012008-07-02 10:56:40 +0200655}
656
Christof Schmittb62a8d92010-09-08 14:39:55 +0200657static void _zfcp_erp_lun_reopen_all(struct zfcp_port *port, int clear,
Swen Schilligea4a3a62010-12-02 15:16:16 +0100658 char *id)
Christof Schmitt287ac012008-07-02 10:56:40 +0200659{
Christof Schmittb62a8d92010-09-08 14:39:55 +0200660 struct scsi_device *sdev;
Christof Schmitt287ac012008-07-02 10:56:40 +0200661
Martin Peschke924dd582013-08-22 17:45:37 +0200662 spin_lock(port->adapter->scsi_host->host_lock);
663 __shost_for_each_device(sdev, port->adapter->scsi_host)
Christof Schmittb62a8d92010-09-08 14:39:55 +0200664 if (sdev_to_zfcp(sdev)->port == port)
Swen Schilligea4a3a62010-12-02 15:16:16 +0100665 _zfcp_erp_lun_reopen(sdev, clear, id, 0);
Martin Peschke924dd582013-08-22 17:45:37 +0200666 spin_unlock(port->adapter->scsi_host->host_lock);
Christof Schmitt287ac012008-07-02 10:56:40 +0200667}
668
Christof Schmitt85600f72009-07-13 15:06:09 +0200669static void zfcp_erp_strategy_followup_failed(struct zfcp_erp_action *act)
Christof Schmitt287ac012008-07-02 10:56:40 +0200670{
Christof Schmitt287ac012008-07-02 10:56:40 +0200671 switch (act->action) {
Christof Schmitt287ac012008-07-02 10:56:40 +0200672 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
Swen Schilligea4a3a62010-12-02 15:16:16 +0100673 _zfcp_erp_adapter_reopen(act->adapter, 0, "ersff_1");
Christof Schmitt287ac012008-07-02 10:56:40 +0200674 break;
Christof Schmitt287ac012008-07-02 10:56:40 +0200675 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
Swen Schilligea4a3a62010-12-02 15:16:16 +0100676 _zfcp_erp_port_forced_reopen(act->port, 0, "ersff_2");
Christof Schmitt287ac012008-07-02 10:56:40 +0200677 break;
Christof Schmitt287ac012008-07-02 10:56:40 +0200678 case ZFCP_ERP_ACTION_REOPEN_PORT:
Swen Schilligea4a3a62010-12-02 15:16:16 +0100679 _zfcp_erp_port_reopen(act->port, 0, "ersff_3");
Christof Schmitt287ac012008-07-02 10:56:40 +0200680 break;
Christof Schmittb62a8d92010-09-08 14:39:55 +0200681 case ZFCP_ERP_ACTION_REOPEN_LUN:
Swen Schilligea4a3a62010-12-02 15:16:16 +0100682 _zfcp_erp_lun_reopen(act->sdev, 0, "ersff_4", 0);
Christof Schmitt85600f72009-07-13 15:06:09 +0200683 break;
684 }
685}
686
687static void zfcp_erp_strategy_followup_success(struct zfcp_erp_action *act)
688{
689 switch (act->action) {
690 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
Swen Schilligea4a3a62010-12-02 15:16:16 +0100691 _zfcp_erp_port_reopen_all(act->adapter, 0, "ersfs_1");
Christof Schmitt85600f72009-07-13 15:06:09 +0200692 break;
693 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
Swen Schilligea4a3a62010-12-02 15:16:16 +0100694 _zfcp_erp_port_reopen(act->port, 0, "ersfs_2");
Christof Schmitt85600f72009-07-13 15:06:09 +0200695 break;
696 case ZFCP_ERP_ACTION_REOPEN_PORT:
Swen Schilligea4a3a62010-12-02 15:16:16 +0100697 _zfcp_erp_lun_reopen_all(act->port, 0, "ersfs_3");
Christof Schmitt287ac012008-07-02 10:56:40 +0200698 break;
699 }
700}
701
702static void zfcp_erp_wakeup(struct zfcp_adapter *adapter)
703{
704 unsigned long flags;
705
Swen Schilligecf0c772009-11-24 16:53:58 +0100706 read_lock_irqsave(&adapter->erp_lock, flags);
Christof Schmitt287ac012008-07-02 10:56:40 +0200707 if (list_empty(&adapter->erp_ready_head) &&
708 list_empty(&adapter->erp_running_head)) {
Peter Zijlstra805de8f42015-04-24 01:12:32 +0200709 atomic_andnot(ZFCP_STATUS_ADAPTER_ERP_PENDING,
Christof Schmitt287ac012008-07-02 10:56:40 +0200710 &adapter->status);
711 wake_up(&adapter->erp_done_wqh);
712 }
Swen Schilligecf0c772009-11-24 16:53:58 +0100713 read_unlock_irqrestore(&adapter->erp_lock, flags);
Christof Schmitt287ac012008-07-02 10:56:40 +0200714}
715
Christof Schmitt287ac012008-07-02 10:56:40 +0200716static void zfcp_erp_enqueue_ptp_port(struct zfcp_adapter *adapter)
717{
718 struct zfcp_port *port;
719 port = zfcp_port_enqueue(adapter, adapter->peer_wwpn, 0,
720 adapter->peer_d_id);
721 if (IS_ERR(port)) /* error or port already attached */
722 return;
Swen Schilligea4a3a62010-12-02 15:16:16 +0100723 _zfcp_erp_port_reopen(port, 0, "ereptp1");
Christof Schmitt287ac012008-07-02 10:56:40 +0200724}
725
726static int zfcp_erp_adapter_strat_fsf_xconf(struct zfcp_erp_action *erp_action)
727{
728 int retries;
729 int sleep = 1;
730 struct zfcp_adapter *adapter = erp_action->adapter;
731
Peter Zijlstra805de8f42015-04-24 01:12:32 +0200732 atomic_andnot(ZFCP_STATUS_ADAPTER_XCONFIG_OK, &adapter->status);
Christof Schmitt287ac012008-07-02 10:56:40 +0200733
734 for (retries = 7; retries; retries--) {
Peter Zijlstra805de8f42015-04-24 01:12:32 +0200735 atomic_andnot(ZFCP_STATUS_ADAPTER_HOST_CON_INIT,
Christof Schmitt287ac012008-07-02 10:56:40 +0200736 &adapter->status);
737 write_lock_irq(&adapter->erp_lock);
738 zfcp_erp_action_to_running(erp_action);
739 write_unlock_irq(&adapter->erp_lock);
740 if (zfcp_fsf_exchange_config_data(erp_action)) {
Peter Zijlstra805de8f42015-04-24 01:12:32 +0200741 atomic_andnot(ZFCP_STATUS_ADAPTER_HOST_CON_INIT,
Christof Schmitt287ac012008-07-02 10:56:40 +0200742 &adapter->status);
743 return ZFCP_ERP_FAILED;
744 }
745
Christof Schmitt347c6a92009-08-18 15:43:25 +0200746 wait_event(adapter->erp_ready_wq,
747 !list_empty(&adapter->erp_ready_head));
Christof Schmitt287ac012008-07-02 10:56:40 +0200748 if (erp_action->status & ZFCP_STATUS_ERP_TIMEDOUT)
749 break;
750
751 if (!(atomic_read(&adapter->status) &
752 ZFCP_STATUS_ADAPTER_HOST_CON_INIT))
753 break;
754
755 ssleep(sleep);
756 sleep *= 2;
757 }
758
Peter Zijlstra805de8f42015-04-24 01:12:32 +0200759 atomic_andnot(ZFCP_STATUS_ADAPTER_HOST_CON_INIT,
Christof Schmitt287ac012008-07-02 10:56:40 +0200760 &adapter->status);
761
762 if (!(atomic_read(&adapter->status) & ZFCP_STATUS_ADAPTER_XCONFIG_OK))
763 return ZFCP_ERP_FAILED;
764
765 if (fc_host_port_type(adapter->scsi_host) == FC_PORTTYPE_PTP)
766 zfcp_erp_enqueue_ptp_port(adapter);
767
768 return ZFCP_ERP_SUCCEEDED;
769}
770
771static int zfcp_erp_adapter_strategy_open_fsf_xport(struct zfcp_erp_action *act)
772{
773 int ret;
774 struct zfcp_adapter *adapter = act->adapter;
775
Christof Schmitt287ac012008-07-02 10:56:40 +0200776 write_lock_irq(&adapter->erp_lock);
777 zfcp_erp_action_to_running(act);
778 write_unlock_irq(&adapter->erp_lock);
779
780 ret = zfcp_fsf_exchange_port_data(act);
781 if (ret == -EOPNOTSUPP)
782 return ZFCP_ERP_SUCCEEDED;
783 if (ret)
784 return ZFCP_ERP_FAILED;
785
Swen Schilligae0904f2010-12-02 15:16:12 +0100786 zfcp_dbf_rec_run("erasox1", act);
Christof Schmitt347c6a92009-08-18 15:43:25 +0200787 wait_event(adapter->erp_ready_wq,
788 !list_empty(&adapter->erp_ready_head));
Swen Schilligae0904f2010-12-02 15:16:12 +0100789 zfcp_dbf_rec_run("erasox2", act);
Christof Schmitt287ac012008-07-02 10:56:40 +0200790 if (act->status & ZFCP_STATUS_ERP_TIMEDOUT)
791 return ZFCP_ERP_FAILED;
792
793 return ZFCP_ERP_SUCCEEDED;
794}
795
796static int zfcp_erp_adapter_strategy_open_fsf(struct zfcp_erp_action *act)
797{
798 if (zfcp_erp_adapter_strat_fsf_xconf(act) == ZFCP_ERP_FAILED)
799 return ZFCP_ERP_FAILED;
800
801 if (zfcp_erp_adapter_strategy_open_fsf_xport(act) == ZFCP_ERP_FAILED)
802 return ZFCP_ERP_FAILED;
803
Christof Schmittc7b279a2011-02-22 19:54:40 +0100804 if (mempool_resize(act->adapter->pool.sr_data,
David Rientjes11d83362015-04-14 15:48:21 -0700805 act->adapter->stat_read_buf_num))
Christof Schmitt8d88cf32010-06-21 10:11:33 +0200806 return ZFCP_ERP_FAILED;
807
808 if (mempool_resize(act->adapter->pool.status_read_req,
David Rientjes11d83362015-04-14 15:48:21 -0700809 act->adapter->stat_read_buf_num))
Christof Schmitt8d88cf32010-06-21 10:11:33 +0200810 return ZFCP_ERP_FAILED;
811
Christof Schmitt64deb6e2010-04-30 18:09:36 +0200812 atomic_set(&act->adapter->stat_miss, act->adapter->stat_read_buf_num);
Christof Schmitt287ac012008-07-02 10:56:40 +0200813 if (zfcp_status_read_refill(act->adapter))
814 return ZFCP_ERP_FAILED;
815
816 return ZFCP_ERP_SUCCEEDED;
817}
818
Swen Schilligcf13c082009-03-02 13:09:03 +0100819static void zfcp_erp_adapter_strategy_close(struct zfcp_erp_action *act)
Christof Schmitt287ac012008-07-02 10:56:40 +0200820{
Christof Schmitt287ac012008-07-02 10:56:40 +0200821 struct zfcp_adapter *adapter = act->adapter;
822
Christof Schmitt287ac012008-07-02 10:56:40 +0200823 /* close queues to ensure that buffers are not accessed by adapter */
Swen Schillig564e1c82009-08-18 15:43:19 +0200824 zfcp_qdio_close(adapter->qdio);
Christof Schmitt287ac012008-07-02 10:56:40 +0200825 zfcp_fsf_req_dismiss_all(adapter);
826 adapter->fsf_req_seq_no = 0;
Christof Schmitt55c770f2009-08-18 15:43:12 +0200827 zfcp_fc_wka_ports_force_offline(adapter->gs);
Christof Schmittb62a8d92010-09-08 14:39:55 +0200828 /* all ports and LUNs are closed */
Swen Schilligedaed852010-09-08 14:40:01 +0200829 zfcp_erp_clear_adapter_status(adapter, ZFCP_STATUS_COMMON_OPEN);
Swen Schilligcf13c082009-03-02 13:09:03 +0100830
Peter Zijlstra805de8f42015-04-24 01:12:32 +0200831 atomic_andnot(ZFCP_STATUS_ADAPTER_XCONFIG_OK |
Swen Schilligcf13c082009-03-02 13:09:03 +0100832 ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED, &adapter->status);
833}
834
835static int zfcp_erp_adapter_strategy_open(struct zfcp_erp_action *act)
836{
837 struct zfcp_adapter *adapter = act->adapter;
838
Christof Schmitt3d63d3b2010-12-02 15:16:17 +0100839 if (zfcp_qdio_open(adapter->qdio)) {
Peter Zijlstra805de8f42015-04-24 01:12:32 +0200840 atomic_andnot(ZFCP_STATUS_ADAPTER_XCONFIG_OK |
Swen Schilligcf13c082009-03-02 13:09:03 +0100841 ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED,
842 &adapter->status);
843 return ZFCP_ERP_FAILED;
844 }
845
846 if (zfcp_erp_adapter_strategy_open_fsf(act)) {
847 zfcp_erp_adapter_strategy_close(act);
848 return ZFCP_ERP_FAILED;
849 }
850
Peter Zijlstra805de8f42015-04-24 01:12:32 +0200851 atomic_or(ZFCP_STATUS_COMMON_OPEN, &adapter->status);
Swen Schilligcf13c082009-03-02 13:09:03 +0100852
853 return ZFCP_ERP_SUCCEEDED;
Christof Schmitt287ac012008-07-02 10:56:40 +0200854}
855
856static int zfcp_erp_adapter_strategy(struct zfcp_erp_action *act)
857{
Swen Schilligcf13c082009-03-02 13:09:03 +0100858 struct zfcp_adapter *adapter = act->adapter;
Christof Schmitt287ac012008-07-02 10:56:40 +0200859
Swen Schilligcf13c082009-03-02 13:09:03 +0100860 if (atomic_read(&adapter->status) & ZFCP_STATUS_COMMON_OPEN) {
861 zfcp_erp_adapter_strategy_close(act);
862 if (act->status & ZFCP_STATUS_ERP_CLOSE_ONLY)
863 return ZFCP_ERP_EXIT;
864 }
Christof Schmitt287ac012008-07-02 10:56:40 +0200865
Swen Schilligcf13c082009-03-02 13:09:03 +0100866 if (zfcp_erp_adapter_strategy_open(act)) {
Christof Schmitt287ac012008-07-02 10:56:40 +0200867 ssleep(8);
Swen Schilligcf13c082009-03-02 13:09:03 +0100868 return ZFCP_ERP_FAILED;
869 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870
Swen Schilligcf13c082009-03-02 13:09:03 +0100871 return ZFCP_ERP_SUCCEEDED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872}
873
Christof Schmitt287ac012008-07-02 10:56:40 +0200874static int zfcp_erp_port_forced_strategy_close(struct zfcp_erp_action *act)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875{
Christof Schmitt287ac012008-07-02 10:56:40 +0200876 int retval;
877
878 retval = zfcp_fsf_close_physical_port(act);
879 if (retval == -ENOMEM)
880 return ZFCP_ERP_NOMEM;
881 act->step = ZFCP_ERP_STEP_PHYS_PORT_CLOSING;
882 if (retval)
883 return ZFCP_ERP_FAILED;
884
885 return ZFCP_ERP_CONTINUES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886}
887
Christof Schmitt287ac012008-07-02 10:56:40 +0200888static int zfcp_erp_port_forced_strategy(struct zfcp_erp_action *erp_action)
889{
890 struct zfcp_port *port = erp_action->port;
891 int status = atomic_read(&port->status);
892
893 switch (erp_action->step) {
894 case ZFCP_ERP_STEP_UNINITIALIZED:
Christof Schmitt287ac012008-07-02 10:56:40 +0200895 if ((status & ZFCP_STATUS_PORT_PHYS_OPEN) &&
896 (status & ZFCP_STATUS_COMMON_OPEN))
897 return zfcp_erp_port_forced_strategy_close(erp_action);
898 else
899 return ZFCP_ERP_FAILED;
900
901 case ZFCP_ERP_STEP_PHYS_PORT_CLOSING:
Christof Schmittddb3e0c2009-07-13 15:06:08 +0200902 if (!(status & ZFCP_STATUS_PORT_PHYS_OPEN))
Christof Schmitt287ac012008-07-02 10:56:40 +0200903 return ZFCP_ERP_SUCCEEDED;
904 }
905 return ZFCP_ERP_FAILED;
906}
907
908static int zfcp_erp_port_strategy_close(struct zfcp_erp_action *erp_action)
909{
910 int retval;
911
912 retval = zfcp_fsf_close_port(erp_action);
913 if (retval == -ENOMEM)
914 return ZFCP_ERP_NOMEM;
915 erp_action->step = ZFCP_ERP_STEP_PORT_CLOSING;
916 if (retval)
917 return ZFCP_ERP_FAILED;
918 return ZFCP_ERP_CONTINUES;
919}
920
921static int zfcp_erp_port_strategy_open_port(struct zfcp_erp_action *erp_action)
922{
923 int retval;
924
925 retval = zfcp_fsf_open_port(erp_action);
926 if (retval == -ENOMEM)
927 return ZFCP_ERP_NOMEM;
928 erp_action->step = ZFCP_ERP_STEP_PORT_OPENING;
929 if (retval)
930 return ZFCP_ERP_FAILED;
931 return ZFCP_ERP_CONTINUES;
932}
933
Christof Schmitt287ac012008-07-02 10:56:40 +0200934static int zfcp_erp_open_ptp_port(struct zfcp_erp_action *act)
935{
936 struct zfcp_adapter *adapter = act->adapter;
937 struct zfcp_port *port = act->port;
938
939 if (port->wwpn != adapter->peer_wwpn) {
Swen Schilligedaed852010-09-08 14:40:01 +0200940 zfcp_erp_set_port_status(port, ZFCP_STATUS_COMMON_ERP_FAILED);
Christof Schmitt287ac012008-07-02 10:56:40 +0200941 return ZFCP_ERP_FAILED;
942 }
943 port->d_id = adapter->peer_d_id;
Christof Schmitt287ac012008-07-02 10:56:40 +0200944 return zfcp_erp_port_strategy_open_port(act);
945}
946
947static int zfcp_erp_port_strategy_open_common(struct zfcp_erp_action *act)
948{
949 struct zfcp_adapter *adapter = act->adapter;
950 struct zfcp_port *port = act->port;
Christof Schmitt287ac012008-07-02 10:56:40 +0200951 int p_status = atomic_read(&port->status);
952
953 switch (act->step) {
954 case ZFCP_ERP_STEP_UNINITIALIZED:
955 case ZFCP_ERP_STEP_PHYS_PORT_CLOSING:
956 case ZFCP_ERP_STEP_PORT_CLOSING:
957 if (fc_host_port_type(adapter->scsi_host) == FC_PORTTYPE_PTP)
958 return zfcp_erp_open_ptp_port(act);
Christof Schmittb98478d2008-12-19 16:56:59 +0100959 if (!port->d_id) {
Christof Schmitt934aeb5872009-10-14 11:00:43 +0200960 zfcp_fc_trigger_did_lookup(port);
Christof Schmitt799b76d2009-08-18 15:43:20 +0200961 return ZFCP_ERP_EXIT;
Christof Schmitt287ac012008-07-02 10:56:40 +0200962 }
Christof Schmitt287ac012008-07-02 10:56:40 +0200963 return zfcp_erp_port_strategy_open_port(act);
964
965 case ZFCP_ERP_STEP_PORT_OPENING:
966 /* D_ID might have changed during open */
Swen Schillig5ab944f2008-10-01 12:42:17 +0200967 if (p_status & ZFCP_STATUS_COMMON_OPEN) {
Christof Schmitt934aeb5872009-10-14 11:00:43 +0200968 if (!port->d_id) {
969 zfcp_fc_trigger_did_lookup(port);
970 return ZFCP_ERP_EXIT;
Swen Schillig5ab944f2008-10-01 12:42:17 +0200971 }
Christof Schmitt934aeb5872009-10-14 11:00:43 +0200972 return ZFCP_ERP_SUCCEEDED;
Swen Schillig5ab944f2008-10-01 12:42:17 +0200973 }
Swen Schilligea460a82009-05-15 13:18:20 +0200974 if (port->d_id && !(p_status & ZFCP_STATUS_COMMON_NOESC)) {
975 port->d_id = 0;
Christof Schmittf7bd7c32010-07-08 09:53:10 +0200976 return ZFCP_ERP_FAILED;
Swen Schilligea460a82009-05-15 13:18:20 +0200977 }
978 /* fall through otherwise */
Christof Schmitt287ac012008-07-02 10:56:40 +0200979 }
980 return ZFCP_ERP_FAILED;
981}
982
Christof Schmitt287ac012008-07-02 10:56:40 +0200983static int zfcp_erp_port_strategy(struct zfcp_erp_action *erp_action)
984{
985 struct zfcp_port *port = erp_action->port;
Christof Schmitt934aeb5872009-10-14 11:00:43 +0200986 int p_status = atomic_read(&port->status);
Christof Schmitt287ac012008-07-02 10:56:40 +0200987
Christof Schmitt934aeb5872009-10-14 11:00:43 +0200988 if ((p_status & ZFCP_STATUS_COMMON_NOESC) &&
989 !(p_status & ZFCP_STATUS_COMMON_OPEN))
Swen Schillig5ab944f2008-10-01 12:42:17 +0200990 goto close_init_done;
991
Christof Schmitt287ac012008-07-02 10:56:40 +0200992 switch (erp_action->step) {
993 case ZFCP_ERP_STEP_UNINITIALIZED:
Christof Schmitt934aeb5872009-10-14 11:00:43 +0200994 if (p_status & ZFCP_STATUS_COMMON_OPEN)
Christof Schmitt287ac012008-07-02 10:56:40 +0200995 return zfcp_erp_port_strategy_close(erp_action);
996 break;
997
998 case ZFCP_ERP_STEP_PORT_CLOSING:
Christof Schmitt934aeb5872009-10-14 11:00:43 +0200999 if (p_status & ZFCP_STATUS_COMMON_OPEN)
Christof Schmitt287ac012008-07-02 10:56:40 +02001000 return ZFCP_ERP_FAILED;
1001 break;
1002 }
Swen Schillig5ab944f2008-10-01 12:42:17 +02001003
1004close_init_done:
Christof Schmitt287ac012008-07-02 10:56:40 +02001005 if (erp_action->status & ZFCP_STATUS_ERP_CLOSE_ONLY)
1006 return ZFCP_ERP_EXIT;
Christof Schmitt287ac012008-07-02 10:56:40 +02001007
Swen Schillig5ab944f2008-10-01 12:42:17 +02001008 return zfcp_erp_port_strategy_open_common(erp_action);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009}
1010
Christof Schmittb62a8d92010-09-08 14:39:55 +02001011static void zfcp_erp_lun_strategy_clearstati(struct scsi_device *sdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012{
Christof Schmittb62a8d92010-09-08 14:39:55 +02001013 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
1014
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001015 atomic_andnot(ZFCP_STATUS_COMMON_ACCESS_DENIED,
Christof Schmittb62a8d92010-09-08 14:39:55 +02001016 &zfcp_sdev->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017}
1018
Christof Schmittb62a8d92010-09-08 14:39:55 +02001019static int zfcp_erp_lun_strategy_close(struct zfcp_erp_action *erp_action)
Christof Schmitt287ac012008-07-02 10:56:40 +02001020{
Christof Schmittb62a8d92010-09-08 14:39:55 +02001021 int retval = zfcp_fsf_close_lun(erp_action);
Christof Schmitt287ac012008-07-02 10:56:40 +02001022 if (retval == -ENOMEM)
1023 return ZFCP_ERP_NOMEM;
Christof Schmittb62a8d92010-09-08 14:39:55 +02001024 erp_action->step = ZFCP_ERP_STEP_LUN_CLOSING;
Christof Schmitt287ac012008-07-02 10:56:40 +02001025 if (retval)
1026 return ZFCP_ERP_FAILED;
1027 return ZFCP_ERP_CONTINUES;
1028}
1029
Christof Schmittb62a8d92010-09-08 14:39:55 +02001030static int zfcp_erp_lun_strategy_open(struct zfcp_erp_action *erp_action)
Christof Schmitt287ac012008-07-02 10:56:40 +02001031{
Christof Schmittb62a8d92010-09-08 14:39:55 +02001032 int retval = zfcp_fsf_open_lun(erp_action);
Christof Schmitt287ac012008-07-02 10:56:40 +02001033 if (retval == -ENOMEM)
1034 return ZFCP_ERP_NOMEM;
Christof Schmittb62a8d92010-09-08 14:39:55 +02001035 erp_action->step = ZFCP_ERP_STEP_LUN_OPENING;
Christof Schmitt287ac012008-07-02 10:56:40 +02001036 if (retval)
1037 return ZFCP_ERP_FAILED;
1038 return ZFCP_ERP_CONTINUES;
1039}
1040
Christof Schmittb62a8d92010-09-08 14:39:55 +02001041static int zfcp_erp_lun_strategy(struct zfcp_erp_action *erp_action)
Christof Schmitt287ac012008-07-02 10:56:40 +02001042{
Christof Schmittb62a8d92010-09-08 14:39:55 +02001043 struct scsi_device *sdev = erp_action->sdev;
1044 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
Christof Schmitt287ac012008-07-02 10:56:40 +02001045
1046 switch (erp_action->step) {
1047 case ZFCP_ERP_STEP_UNINITIALIZED:
Christof Schmittb62a8d92010-09-08 14:39:55 +02001048 zfcp_erp_lun_strategy_clearstati(sdev);
1049 if (atomic_read(&zfcp_sdev->status) & ZFCP_STATUS_COMMON_OPEN)
1050 return zfcp_erp_lun_strategy_close(erp_action);
Christof Schmitt287ac012008-07-02 10:56:40 +02001051 /* already closed, fall through */
Christof Schmittb62a8d92010-09-08 14:39:55 +02001052 case ZFCP_ERP_STEP_LUN_CLOSING:
1053 if (atomic_read(&zfcp_sdev->status) & ZFCP_STATUS_COMMON_OPEN)
Christof Schmitt287ac012008-07-02 10:56:40 +02001054 return ZFCP_ERP_FAILED;
1055 if (erp_action->status & ZFCP_STATUS_ERP_CLOSE_ONLY)
1056 return ZFCP_ERP_EXIT;
Christof Schmittb62a8d92010-09-08 14:39:55 +02001057 return zfcp_erp_lun_strategy_open(erp_action);
Christof Schmitt287ac012008-07-02 10:56:40 +02001058
Christof Schmittb62a8d92010-09-08 14:39:55 +02001059 case ZFCP_ERP_STEP_LUN_OPENING:
1060 if (atomic_read(&zfcp_sdev->status) & ZFCP_STATUS_COMMON_OPEN)
Christof Schmitt287ac012008-07-02 10:56:40 +02001061 return ZFCP_ERP_SUCCEEDED;
1062 }
1063 return ZFCP_ERP_FAILED;
1064}
1065
Christof Schmittb62a8d92010-09-08 14:39:55 +02001066static int zfcp_erp_strategy_check_lun(struct scsi_device *sdev, int result)
Christof Schmitt287ac012008-07-02 10:56:40 +02001067{
Christof Schmittb62a8d92010-09-08 14:39:55 +02001068 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
1069
Christof Schmitt287ac012008-07-02 10:56:40 +02001070 switch (result) {
1071 case ZFCP_ERP_SUCCEEDED :
Christof Schmittb62a8d92010-09-08 14:39:55 +02001072 atomic_set(&zfcp_sdev->erp_counter, 0);
1073 zfcp_erp_lun_unblock(sdev);
Christof Schmitt287ac012008-07-02 10:56:40 +02001074 break;
1075 case ZFCP_ERP_FAILED :
Christof Schmittb62a8d92010-09-08 14:39:55 +02001076 atomic_inc(&zfcp_sdev->erp_counter);
1077 if (atomic_read(&zfcp_sdev->erp_counter) > ZFCP_MAX_ERPS) {
1078 dev_err(&zfcp_sdev->port->adapter->ccw_device->dev,
1079 "ERP failed for LUN 0x%016Lx on "
Christof Schmittff3b24f2008-10-01 12:42:15 +02001080 "port 0x%016Lx\n",
Christof Schmittb62a8d92010-09-08 14:39:55 +02001081 (unsigned long long)zfcp_scsi_dev_lun(sdev),
1082 (unsigned long long)zfcp_sdev->port->wwpn);
Swen Schilligedaed852010-09-08 14:40:01 +02001083 zfcp_erp_set_lun_status(sdev,
1084 ZFCP_STATUS_COMMON_ERP_FAILED);
Christof Schmittff3b24f2008-10-01 12:42:15 +02001085 }
Christof Schmitt287ac012008-07-02 10:56:40 +02001086 break;
1087 }
1088
Christof Schmittb62a8d92010-09-08 14:39:55 +02001089 if (atomic_read(&zfcp_sdev->status) & ZFCP_STATUS_COMMON_ERP_FAILED) {
1090 zfcp_erp_lun_block(sdev, 0);
Christof Schmitt287ac012008-07-02 10:56:40 +02001091 result = ZFCP_ERP_EXIT;
1092 }
1093 return result;
1094}
1095
1096static int zfcp_erp_strategy_check_port(struct zfcp_port *port, int result)
1097{
1098 switch (result) {
1099 case ZFCP_ERP_SUCCEEDED :
1100 atomic_set(&port->erp_counter, 0);
1101 zfcp_erp_port_unblock(port);
1102 break;
1103
1104 case ZFCP_ERP_FAILED :
1105 if (atomic_read(&port->status) & ZFCP_STATUS_COMMON_NOESC) {
1106 zfcp_erp_port_block(port, 0);
1107 result = ZFCP_ERP_EXIT;
1108 }
1109 atomic_inc(&port->erp_counter);
Christof Schmittff3b24f2008-10-01 12:42:15 +02001110 if (atomic_read(&port->erp_counter) > ZFCP_MAX_ERPS) {
1111 dev_err(&port->adapter->ccw_device->dev,
1112 "ERP failed for remote port 0x%016Lx\n",
Swen Schillig7ba58c92008-10-01 12:42:18 +02001113 (unsigned long long)port->wwpn);
Swen Schilligedaed852010-09-08 14:40:01 +02001114 zfcp_erp_set_port_status(port,
1115 ZFCP_STATUS_COMMON_ERP_FAILED);
Christof Schmittff3b24f2008-10-01 12:42:15 +02001116 }
Christof Schmitt287ac012008-07-02 10:56:40 +02001117 break;
1118 }
1119
1120 if (atomic_read(&port->status) & ZFCP_STATUS_COMMON_ERP_FAILED) {
1121 zfcp_erp_port_block(port, 0);
1122 result = ZFCP_ERP_EXIT;
1123 }
1124 return result;
1125}
1126
1127static int zfcp_erp_strategy_check_adapter(struct zfcp_adapter *adapter,
1128 int result)
1129{
1130 switch (result) {
1131 case ZFCP_ERP_SUCCEEDED :
1132 atomic_set(&adapter->erp_counter, 0);
1133 zfcp_erp_adapter_unblock(adapter);
1134 break;
1135
1136 case ZFCP_ERP_FAILED :
1137 atomic_inc(&adapter->erp_counter);
Christof Schmittff3b24f2008-10-01 12:42:15 +02001138 if (atomic_read(&adapter->erp_counter) > ZFCP_MAX_ERPS) {
1139 dev_err(&adapter->ccw_device->dev,
1140 "ERP cannot recover an error "
1141 "on the FCP device\n");
Swen Schilligedaed852010-09-08 14:40:01 +02001142 zfcp_erp_set_adapter_status(adapter,
1143 ZFCP_STATUS_COMMON_ERP_FAILED);
Christof Schmittff3b24f2008-10-01 12:42:15 +02001144 }
Christof Schmitt287ac012008-07-02 10:56:40 +02001145 break;
1146 }
1147
1148 if (atomic_read(&adapter->status) & ZFCP_STATUS_COMMON_ERP_FAILED) {
1149 zfcp_erp_adapter_block(adapter, 0);
1150 result = ZFCP_ERP_EXIT;
1151 }
1152 return result;
1153}
1154
1155static int zfcp_erp_strategy_check_target(struct zfcp_erp_action *erp_action,
1156 int result)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157{
1158 struct zfcp_adapter *adapter = erp_action->adapter;
1159 struct zfcp_port *port = erp_action->port;
Christof Schmittb62a8d92010-09-08 14:39:55 +02001160 struct scsi_device *sdev = erp_action->sdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 switch (erp_action->action) {
1163
Christof Schmittb62a8d92010-09-08 14:39:55 +02001164 case ZFCP_ERP_ACTION_REOPEN_LUN:
1165 result = zfcp_erp_strategy_check_lun(sdev, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 break;
1167
1168 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
1169 case ZFCP_ERP_ACTION_REOPEN_PORT:
1170 result = zfcp_erp_strategy_check_port(port, result);
1171 break;
1172
1173 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
1174 result = zfcp_erp_strategy_check_adapter(adapter, result);
1175 break;
1176 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 return result;
1178}
1179
Christof Schmitt287ac012008-07-02 10:56:40 +02001180static int zfcp_erp_strat_change_det(atomic_t *target_status, u32 erp_status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181{
Christof Schmitt287ac012008-07-02 10:56:40 +02001182 int status = atomic_read(target_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183
Christof Schmitt287ac012008-07-02 10:56:40 +02001184 if ((status & ZFCP_STATUS_COMMON_RUNNING) &&
1185 (erp_status & ZFCP_STATUS_ERP_CLOSE_ONLY))
1186 return 1; /* take it online */
1187
1188 if (!(status & ZFCP_STATUS_COMMON_RUNNING) &&
1189 !(erp_status & ZFCP_STATUS_ERP_CLOSE_ONLY))
1190 return 1; /* take it offline */
1191
1192 return 0;
1193}
1194
1195static int zfcp_erp_strategy_statechange(struct zfcp_erp_action *act, int ret)
1196{
1197 int action = act->action;
1198 struct zfcp_adapter *adapter = act->adapter;
1199 struct zfcp_port *port = act->port;
Christof Schmittb62a8d92010-09-08 14:39:55 +02001200 struct scsi_device *sdev = act->sdev;
1201 struct zfcp_scsi_dev *zfcp_sdev;
Christof Schmitt287ac012008-07-02 10:56:40 +02001202 u32 erp_status = act->status;
1203
1204 switch (action) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
Christof Schmitt287ac012008-07-02 10:56:40 +02001206 if (zfcp_erp_strat_change_det(&adapter->status, erp_status)) {
1207 _zfcp_erp_adapter_reopen(adapter,
1208 ZFCP_STATUS_COMMON_ERP_FAILED,
Swen Schilligea4a3a62010-12-02 15:16:16 +01001209 "ersscg1");
Christof Schmitt287ac012008-07-02 10:56:40 +02001210 return ZFCP_ERP_EXIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 }
1212 break;
1213
1214 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
1215 case ZFCP_ERP_ACTION_REOPEN_PORT:
Christof Schmitt287ac012008-07-02 10:56:40 +02001216 if (zfcp_erp_strat_change_det(&port->status, erp_status)) {
1217 _zfcp_erp_port_reopen(port,
1218 ZFCP_STATUS_COMMON_ERP_FAILED,
Swen Schilligea4a3a62010-12-02 15:16:16 +01001219 "ersscg2");
Christof Schmitt287ac012008-07-02 10:56:40 +02001220 return ZFCP_ERP_EXIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 }
1222 break;
1223
Christof Schmittb62a8d92010-09-08 14:39:55 +02001224 case ZFCP_ERP_ACTION_REOPEN_LUN:
1225 zfcp_sdev = sdev_to_zfcp(sdev);
1226 if (zfcp_erp_strat_change_det(&zfcp_sdev->status, erp_status)) {
1227 _zfcp_erp_lun_reopen(sdev,
1228 ZFCP_STATUS_COMMON_ERP_FAILED,
Swen Schilligea4a3a62010-12-02 15:16:16 +01001229 "ersscg3", 0);
Christof Schmitt287ac012008-07-02 10:56:40 +02001230 return ZFCP_ERP_EXIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 }
1232 break;
1233 }
Christof Schmitt287ac012008-07-02 10:56:40 +02001234 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235}
1236
Christof Schmitt287ac012008-07-02 10:56:40 +02001237static void zfcp_erp_action_dequeue(struct zfcp_erp_action *erp_action)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238{
Christof Schmitt287ac012008-07-02 10:56:40 +02001239 struct zfcp_adapter *adapter = erp_action->adapter;
Christof Schmittb62a8d92010-09-08 14:39:55 +02001240 struct zfcp_scsi_dev *zfcp_sdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241
Christof Schmitt287ac012008-07-02 10:56:40 +02001242 adapter->erp_total_count--;
1243 if (erp_action->status & ZFCP_STATUS_ERP_LOWMEM) {
1244 adapter->erp_low_mem_count--;
1245 erp_action->status &= ~ZFCP_STATUS_ERP_LOWMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 }
1247
Christof Schmitt287ac012008-07-02 10:56:40 +02001248 list_del(&erp_action->list);
Swen Schilligae0904f2010-12-02 15:16:12 +01001249 zfcp_dbf_rec_run("eractd1", erp_action);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250
Christof Schmitt287ac012008-07-02 10:56:40 +02001251 switch (erp_action->action) {
Christof Schmittb62a8d92010-09-08 14:39:55 +02001252 case ZFCP_ERP_ACTION_REOPEN_LUN:
1253 zfcp_sdev = sdev_to_zfcp(erp_action->sdev);
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001254 atomic_andnot(ZFCP_STATUS_COMMON_ERP_INUSE,
Christof Schmittb62a8d92010-09-08 14:39:55 +02001255 &zfcp_sdev->status);
Christof Schmitt287ac012008-07-02 10:56:40 +02001256 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257
Christof Schmitt287ac012008-07-02 10:56:40 +02001258 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
1259 case ZFCP_ERP_ACTION_REOPEN_PORT:
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001260 atomic_andnot(ZFCP_STATUS_COMMON_ERP_INUSE,
Christof Schmitt287ac012008-07-02 10:56:40 +02001261 &erp_action->port->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 break;
Christof Schmitt287ac012008-07-02 10:56:40 +02001263
1264 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001265 atomic_andnot(ZFCP_STATUS_COMMON_ERP_INUSE,
Christof Schmitt287ac012008-07-02 10:56:40 +02001266 &erp_action->adapter->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 break;
1268 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269}
1270
Steffen Maier6f2ce1c2016-12-09 17:16:33 +01001271/**
1272 * zfcp_erp_try_rport_unblock - unblock rport if no more/new recovery
1273 * @port: zfcp_port whose fc_rport we should try to unblock
1274 */
1275static void zfcp_erp_try_rport_unblock(struct zfcp_port *port)
1276{
1277 unsigned long flags;
1278 struct zfcp_adapter *adapter = port->adapter;
1279 int port_status;
1280 struct Scsi_Host *shost = adapter->scsi_host;
1281 struct scsi_device *sdev;
1282
1283 write_lock_irqsave(&adapter->erp_lock, flags);
1284 port_status = atomic_read(&port->status);
1285 if ((port_status & ZFCP_STATUS_COMMON_UNBLOCKED) == 0 ||
1286 (port_status & (ZFCP_STATUS_COMMON_ERP_INUSE |
1287 ZFCP_STATUS_COMMON_ERP_FAILED)) != 0) {
1288 /* new ERP of severity >= port triggered elsewhere meanwhile or
1289 * local link down (adapter erp_failed but not clear unblock)
1290 */
1291 zfcp_dbf_rec_run_lvl(4, "ertru_p", &port->erp_action);
1292 write_unlock_irqrestore(&adapter->erp_lock, flags);
1293 return;
1294 }
1295 spin_lock(shost->host_lock);
1296 __shost_for_each_device(sdev, shost) {
1297 struct zfcp_scsi_dev *zsdev = sdev_to_zfcp(sdev);
1298 int lun_status;
1299
1300 if (zsdev->port != port)
1301 continue;
1302 /* LUN under port of interest */
1303 lun_status = atomic_read(&zsdev->status);
1304 if ((lun_status & ZFCP_STATUS_COMMON_ERP_FAILED) != 0)
1305 continue; /* unblock rport despite failed LUNs */
1306 /* LUN recovery not given up yet [maybe follow-up pending] */
1307 if ((lun_status & ZFCP_STATUS_COMMON_UNBLOCKED) == 0 ||
1308 (lun_status & ZFCP_STATUS_COMMON_ERP_INUSE) != 0) {
1309 /* LUN blocked:
1310 * not yet unblocked [LUN recovery pending]
1311 * or meanwhile blocked [new LUN recovery triggered]
1312 */
1313 zfcp_dbf_rec_run_lvl(4, "ertru_l", &zsdev->erp_action);
1314 spin_unlock(shost->host_lock);
1315 write_unlock_irqrestore(&adapter->erp_lock, flags);
1316 return;
1317 }
1318 }
1319 /* now port has no child or all children have completed recovery,
1320 * and no ERP of severity >= port was meanwhile triggered elsewhere
1321 */
1322 zfcp_scsi_schedule_rport_register(port);
1323 spin_unlock(shost->host_lock);
1324 write_unlock_irqrestore(&adapter->erp_lock, flags);
1325}
1326
Christof Schmitt287ac012008-07-02 10:56:40 +02001327static void zfcp_erp_action_cleanup(struct zfcp_erp_action *act, int result)
Maxim Shchetyninaef4a982005-09-13 21:51:16 +02001328{
Christof Schmitt287ac012008-07-02 10:56:40 +02001329 struct zfcp_adapter *adapter = act->adapter;
1330 struct zfcp_port *port = act->port;
Christof Schmittb62a8d92010-09-08 14:39:55 +02001331 struct scsi_device *sdev = act->sdev;
Maxim Shchetyninaef4a982005-09-13 21:51:16 +02001332
Christof Schmitt287ac012008-07-02 10:56:40 +02001333 switch (act->action) {
Christof Schmittb62a8d92010-09-08 14:39:55 +02001334 case ZFCP_ERP_ACTION_REOPEN_LUN:
Christof Schmittfdbd1c52010-09-08 14:39:54 +02001335 if (!(act->status & ZFCP_STATUS_ERP_NO_REF))
Christof Schmittb62a8d92010-09-08 14:39:55 +02001336 scsi_device_put(sdev);
Steffen Maier6f2ce1c2016-12-09 17:16:33 +01001337 zfcp_erp_try_rport_unblock(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 break;
Christof Schmitt287ac012008-07-02 10:56:40 +02001339
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 case ZFCP_ERP_ACTION_REOPEN_PORT:
Steffen Maier4eeaa4f2016-08-10 18:30:46 +02001341 /* This switch case might also happen after a forced reopen
1342 * was successfully done and thus overwritten with a new
1343 * non-forced reopen at `ersfs_2'. In this case, we must not
1344 * do the clean-up of the non-forced version.
1345 */
1346 if (act->step != ZFCP_ERP_STEP_UNINITIALIZED)
1347 if (result == ZFCP_ERP_SUCCEEDED)
Steffen Maier6f2ce1c2016-12-09 17:16:33 +01001348 zfcp_erp_try_rport_unblock(port);
Christof Schmitt57676202010-07-08 09:53:05 +02001349 /* fall through */
1350 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
Christof Schmitt615f59e2010-02-17 11:18:56 +01001351 put_device(&port->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 break;
Christof Schmitt287ac012008-07-02 10:56:40 +02001353
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
Christof Schmitta2fa0ae2009-03-02 13:09:08 +01001355 if (result == ZFCP_ERP_SUCCEEDED) {
Christof Schmittbd43a42b72008-12-25 13:38:50 +01001356 register_service_level(&adapter->service_level);
Steffen Maier43f60cb2012-09-04 15:23:35 +02001357 zfcp_fc_conditional_port_scan(adapter);
Christof Schmitt038d9442011-02-22 19:54:48 +01001358 queue_work(adapter->work_queue, &adapter->ns_up_work);
Christof Schmitta2fa0ae2009-03-02 13:09:08 +01001359 } else
1360 unregister_service_level(&adapter->service_level);
Christof Schmitt038d9442011-02-22 19:54:48 +01001361
Swen Schilligf3450c72009-11-24 16:53:59 +01001362 kref_put(&adapter->ref, zfcp_adapter_release);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 }
1365}
1366
Christof Schmitt287ac012008-07-02 10:56:40 +02001367static int zfcp_erp_strategy_do_action(struct zfcp_erp_action *erp_action)
1368{
1369 switch (erp_action->action) {
1370 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
1371 return zfcp_erp_adapter_strategy(erp_action);
1372 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
1373 return zfcp_erp_port_forced_strategy(erp_action);
1374 case ZFCP_ERP_ACTION_REOPEN_PORT:
1375 return zfcp_erp_port_strategy(erp_action);
Christof Schmittb62a8d92010-09-08 14:39:55 +02001376 case ZFCP_ERP_ACTION_REOPEN_LUN:
1377 return zfcp_erp_lun_strategy(erp_action);
Christof Schmitt287ac012008-07-02 10:56:40 +02001378 }
1379 return ZFCP_ERP_FAILED;
1380}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381
Christof Schmitt287ac012008-07-02 10:56:40 +02001382static int zfcp_erp_strategy(struct zfcp_erp_action *erp_action)
1383{
1384 int retval;
Christof Schmitt287ac012008-07-02 10:56:40 +02001385 unsigned long flags;
Swen Schilligecf0c772009-11-24 16:53:58 +01001386 struct zfcp_adapter *adapter = erp_action->adapter;
Christof Schmitt287ac012008-07-02 10:56:40 +02001387
Swen Schilligf3450c72009-11-24 16:53:59 +01001388 kref_get(&adapter->ref);
Christof Schmitt287ac012008-07-02 10:56:40 +02001389
Swen Schilligf3450c72009-11-24 16:53:59 +01001390 write_lock_irqsave(&adapter->erp_lock, flags);
Christof Schmitt287ac012008-07-02 10:56:40 +02001391 zfcp_erp_strategy_check_fsfreq(erp_action);
1392
1393 if (erp_action->status & ZFCP_STATUS_ERP_DISMISSED) {
1394 zfcp_erp_action_dequeue(erp_action);
1395 retval = ZFCP_ERP_DISMISSED;
1396 goto unlock;
1397 }
1398
Christof Schmitt9c785d92010-07-08 09:53:09 +02001399 if (erp_action->status & ZFCP_STATUS_ERP_TIMEDOUT) {
1400 retval = ZFCP_ERP_FAILED;
1401 goto check_target;
1402 }
1403
Christof Schmitt287ac012008-07-02 10:56:40 +02001404 zfcp_erp_action_to_running(erp_action);
1405
1406 /* no lock to allow for blocking operations */
Swen Schilligecf0c772009-11-24 16:53:58 +01001407 write_unlock_irqrestore(&adapter->erp_lock, flags);
Christof Schmitt287ac012008-07-02 10:56:40 +02001408 retval = zfcp_erp_strategy_do_action(erp_action);
Swen Schilligecf0c772009-11-24 16:53:58 +01001409 write_lock_irqsave(&adapter->erp_lock, flags);
Christof Schmitt287ac012008-07-02 10:56:40 +02001410
1411 if (erp_action->status & ZFCP_STATUS_ERP_DISMISSED)
1412 retval = ZFCP_ERP_CONTINUES;
1413
1414 switch (retval) {
1415 case ZFCP_ERP_NOMEM:
1416 if (!(erp_action->status & ZFCP_STATUS_ERP_LOWMEM)) {
1417 ++adapter->erp_low_mem_count;
1418 erp_action->status |= ZFCP_STATUS_ERP_LOWMEM;
1419 }
1420 if (adapter->erp_total_count == adapter->erp_low_mem_count)
Swen Schilligea4a3a62010-12-02 15:16:16 +01001421 _zfcp_erp_adapter_reopen(adapter, 0, "erstgy1");
Christof Schmitt287ac012008-07-02 10:56:40 +02001422 else {
1423 zfcp_erp_strategy_memwait(erp_action);
1424 retval = ZFCP_ERP_CONTINUES;
1425 }
1426 goto unlock;
1427
1428 case ZFCP_ERP_CONTINUES:
1429 if (erp_action->status & ZFCP_STATUS_ERP_LOWMEM) {
1430 --adapter->erp_low_mem_count;
1431 erp_action->status &= ~ZFCP_STATUS_ERP_LOWMEM;
1432 }
1433 goto unlock;
1434 }
1435
Christof Schmitt9c785d92010-07-08 09:53:09 +02001436check_target:
Christof Schmitt287ac012008-07-02 10:56:40 +02001437 retval = zfcp_erp_strategy_check_target(erp_action, retval);
1438 zfcp_erp_action_dequeue(erp_action);
1439 retval = zfcp_erp_strategy_statechange(erp_action, retval);
1440 if (retval == ZFCP_ERP_EXIT)
1441 goto unlock;
Christof Schmitt85600f72009-07-13 15:06:09 +02001442 if (retval == ZFCP_ERP_SUCCEEDED)
1443 zfcp_erp_strategy_followup_success(erp_action);
1444 if (retval == ZFCP_ERP_FAILED)
1445 zfcp_erp_strategy_followup_failed(erp_action);
Christof Schmitt287ac012008-07-02 10:56:40 +02001446
1447 unlock:
Swen Schilligecf0c772009-11-24 16:53:58 +01001448 write_unlock_irqrestore(&adapter->erp_lock, flags);
Christof Schmitt287ac012008-07-02 10:56:40 +02001449
1450 if (retval != ZFCP_ERP_CONTINUES)
1451 zfcp_erp_action_cleanup(erp_action, retval);
1452
Swen Schilligf3450c72009-11-24 16:53:59 +01001453 kref_put(&adapter->ref, zfcp_adapter_release);
Christof Schmitt287ac012008-07-02 10:56:40 +02001454 return retval;
1455}
1456
1457static int zfcp_erp_thread(void *data)
1458{
1459 struct zfcp_adapter *adapter = (struct zfcp_adapter *) data;
1460 struct list_head *next;
1461 struct zfcp_erp_action *act;
1462 unsigned long flags;
1463
Christof Schmitt347c6a92009-08-18 15:43:25 +02001464 for (;;) {
Christof Schmitt347c6a92009-08-18 15:43:25 +02001465 wait_event_interruptible(adapter->erp_ready_wq,
1466 !list_empty(&adapter->erp_ready_head) ||
1467 kthread_should_stop());
Swen Schillig94ab4b32009-04-17 15:08:06 +02001468
Christof Schmitt347c6a92009-08-18 15:43:25 +02001469 if (kthread_should_stop())
1470 break;
1471
Christof Schmitt287ac012008-07-02 10:56:40 +02001472 write_lock_irqsave(&adapter->erp_lock, flags);
1473 next = adapter->erp_ready_head.next;
1474 write_unlock_irqrestore(&adapter->erp_lock, flags);
1475
1476 if (next != &adapter->erp_ready_head) {
1477 act = list_entry(next, struct zfcp_erp_action, list);
1478
1479 /* there is more to come after dismission, no notify */
1480 if (zfcp_erp_strategy(act) != ZFCP_ERP_DISMISSED)
1481 zfcp_erp_wakeup(adapter);
1482 }
Christof Schmitt287ac012008-07-02 10:56:40 +02001483 }
1484
Christof Schmitt287ac012008-07-02 10:56:40 +02001485 return 0;
1486}
1487
1488/**
1489 * zfcp_erp_thread_setup - Start ERP thread for adapter
1490 * @adapter: Adapter to start the ERP thread for
1491 *
1492 * Returns 0 on success or error code from kernel_thread()
1493 */
1494int zfcp_erp_thread_setup(struct zfcp_adapter *adapter)
1495{
Christof Schmitt347c6a92009-08-18 15:43:25 +02001496 struct task_struct *thread;
Christof Schmitt287ac012008-07-02 10:56:40 +02001497
Christof Schmitt347c6a92009-08-18 15:43:25 +02001498 thread = kthread_run(zfcp_erp_thread, adapter, "zfcperp%s",
1499 dev_name(&adapter->ccw_device->dev));
1500 if (IS_ERR(thread)) {
Christof Schmitt287ac012008-07-02 10:56:40 +02001501 dev_err(&adapter->ccw_device->dev,
Christof Schmittff3b24f2008-10-01 12:42:15 +02001502 "Creating an ERP thread for the FCP device failed.\n");
Christof Schmitt347c6a92009-08-18 15:43:25 +02001503 return PTR_ERR(thread);
Christof Schmitt287ac012008-07-02 10:56:40 +02001504 }
Christof Schmitt347c6a92009-08-18 15:43:25 +02001505
1506 adapter->erp_thread = thread;
Christof Schmitt287ac012008-07-02 10:56:40 +02001507 return 0;
1508}
1509
1510/**
1511 * zfcp_erp_thread_kill - Stop ERP thread.
1512 * @adapter: Adapter where the ERP thread should be stopped.
1513 *
1514 * The caller of this routine ensures that the specified adapter has
1515 * been shut down and that this operation has been completed. Thus,
1516 * there are no pending erp_actions which would need to be handled
1517 * here.
1518 */
1519void zfcp_erp_thread_kill(struct zfcp_adapter *adapter)
1520{
Christof Schmitt347c6a92009-08-18 15:43:25 +02001521 kthread_stop(adapter->erp_thread);
1522 adapter->erp_thread = NULL;
Christof Schmitt143bb6b2009-08-18 15:43:27 +02001523 WARN_ON(!list_empty(&adapter->erp_ready_head));
1524 WARN_ON(!list_empty(&adapter->erp_running_head));
Christof Schmitt287ac012008-07-02 10:56:40 +02001525}
1526
1527/**
Christof Schmitt287ac012008-07-02 10:56:40 +02001528 * zfcp_erp_wait - wait for completion of error recovery on an adapter
1529 * @adapter: adapter for which to wait for completion of its error recovery
1530 */
1531void zfcp_erp_wait(struct zfcp_adapter *adapter)
1532{
1533 wait_event(adapter->erp_done_wqh,
1534 !(atomic_read(&adapter->status) &
1535 ZFCP_STATUS_ADAPTER_ERP_PENDING));
1536}
1537
1538/**
Swen Schilligedaed852010-09-08 14:40:01 +02001539 * zfcp_erp_set_adapter_status - set adapter status bits
Christof Schmitt287ac012008-07-02 10:56:40 +02001540 * @adapter: adapter to change the status
Christof Schmitt287ac012008-07-02 10:56:40 +02001541 * @mask: status bits to change
Christof Schmitt287ac012008-07-02 10:56:40 +02001542 *
Christof Schmittb62a8d92010-09-08 14:39:55 +02001543 * Changes in common status bits are propagated to attached ports and LUNs.
Christof Schmitt287ac012008-07-02 10:56:40 +02001544 */
Swen Schilligedaed852010-09-08 14:40:01 +02001545void zfcp_erp_set_adapter_status(struct zfcp_adapter *adapter, u32 mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 struct zfcp_port *port;
Swen Schilligedaed852010-09-08 14:40:01 +02001548 struct scsi_device *sdev;
Swen Schilligecf0c772009-11-24 16:53:58 +01001549 unsigned long flags;
Christof Schmitt287ac012008-07-02 10:56:40 +02001550 u32 common_mask = mask & ZFCP_COMMON_FLAGS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001552 atomic_or(mask, &adapter->status);
Christof Schmitt287ac012008-07-02 10:56:40 +02001553
Swen Schilligedaed852010-09-08 14:40:01 +02001554 if (!common_mask)
1555 return;
1556
1557 read_lock_irqsave(&adapter->port_list_lock, flags);
1558 list_for_each_entry(port, &adapter->port_list, list)
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001559 atomic_or(common_mask, &port->status);
Swen Schilligedaed852010-09-08 14:40:01 +02001560 read_unlock_irqrestore(&adapter->port_list_lock, flags);
1561
Martin Peschke924dd582013-08-22 17:45:37 +02001562 spin_lock_irqsave(adapter->scsi_host->host_lock, flags);
1563 __shost_for_each_device(sdev, adapter->scsi_host)
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001564 atomic_or(common_mask, &sdev_to_zfcp(sdev)->status);
Martin Peschke924dd582013-08-22 17:45:37 +02001565 spin_unlock_irqrestore(adapter->scsi_host->host_lock, flags);
Swen Schilligedaed852010-09-08 14:40:01 +02001566}
1567
1568/**
1569 * zfcp_erp_clear_adapter_status - clear adapter status bits
1570 * @adapter: adapter to change the status
1571 * @mask: status bits to change
1572 *
1573 * Changes in common status bits are propagated to attached ports and LUNs.
1574 */
1575void zfcp_erp_clear_adapter_status(struct zfcp_adapter *adapter, u32 mask)
1576{
1577 struct zfcp_port *port;
1578 struct scsi_device *sdev;
1579 unsigned long flags;
1580 u32 common_mask = mask & ZFCP_COMMON_FLAGS;
1581 u32 clear_counter = mask & ZFCP_STATUS_COMMON_ERP_FAILED;
1582
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001583 atomic_andnot(mask, &adapter->status);
Swen Schilligedaed852010-09-08 14:40:01 +02001584
1585 if (!common_mask)
1586 return;
1587
1588 if (clear_counter)
1589 atomic_set(&adapter->erp_counter, 0);
1590
1591 read_lock_irqsave(&adapter->port_list_lock, flags);
1592 list_for_each_entry(port, &adapter->port_list, list) {
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001593 atomic_andnot(common_mask, &port->status);
Swen Schilligedaed852010-09-08 14:40:01 +02001594 if (clear_counter)
1595 atomic_set(&port->erp_counter, 0);
1596 }
1597 read_unlock_irqrestore(&adapter->port_list_lock, flags);
1598
Martin Peschke924dd582013-08-22 17:45:37 +02001599 spin_lock_irqsave(adapter->scsi_host->host_lock, flags);
1600 __shost_for_each_device(sdev, adapter->scsi_host) {
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001601 atomic_andnot(common_mask, &sdev_to_zfcp(sdev)->status);
Swen Schilligedaed852010-09-08 14:40:01 +02001602 if (clear_counter)
1603 atomic_set(&sdev_to_zfcp(sdev)->erp_counter, 0);
Swen Schilligecf0c772009-11-24 16:53:58 +01001604 }
Martin Peschke924dd582013-08-22 17:45:37 +02001605 spin_unlock_irqrestore(adapter->scsi_host->host_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606}
1607
Christof Schmitt287ac012008-07-02 10:56:40 +02001608/**
Swen Schilligedaed852010-09-08 14:40:01 +02001609 * zfcp_erp_set_port_status - set port status bits
1610 * @port: port to change the status
Christof Schmitt287ac012008-07-02 10:56:40 +02001611 * @mask: status bits to change
Christof Schmitt287ac012008-07-02 10:56:40 +02001612 *
Christof Schmittb62a8d92010-09-08 14:39:55 +02001613 * Changes in common status bits are propagated to attached LUNs.
Christof Schmitt287ac012008-07-02 10:56:40 +02001614 */
Swen Schilligedaed852010-09-08 14:40:01 +02001615void zfcp_erp_set_port_status(struct zfcp_port *port, u32 mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616{
Christof Schmittb62a8d92010-09-08 14:39:55 +02001617 struct scsi_device *sdev;
Christof Schmitt287ac012008-07-02 10:56:40 +02001618 u32 common_mask = mask & ZFCP_COMMON_FLAGS;
Martin Peschke924dd582013-08-22 17:45:37 +02001619 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001621 atomic_or(mask, &port->status);
Christof Schmitt287ac012008-07-02 10:56:40 +02001622
Swen Schilligedaed852010-09-08 14:40:01 +02001623 if (!common_mask)
1624 return;
1625
Martin Peschke924dd582013-08-22 17:45:37 +02001626 spin_lock_irqsave(port->adapter->scsi_host->host_lock, flags);
1627 __shost_for_each_device(sdev, port->adapter->scsi_host)
Swen Schilligedaed852010-09-08 14:40:01 +02001628 if (sdev_to_zfcp(sdev)->port == port)
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001629 atomic_or(common_mask,
Swen Schilligedaed852010-09-08 14:40:01 +02001630 &sdev_to_zfcp(sdev)->status);
Martin Peschke924dd582013-08-22 17:45:37 +02001631 spin_unlock_irqrestore(port->adapter->scsi_host->host_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632}
1633
Christof Schmitt287ac012008-07-02 10:56:40 +02001634/**
Swen Schilligedaed852010-09-08 14:40:01 +02001635 * zfcp_erp_clear_port_status - clear port status bits
1636 * @port: adapter to change the status
Christof Schmitt287ac012008-07-02 10:56:40 +02001637 * @mask: status bits to change
Swen Schilligedaed852010-09-08 14:40:01 +02001638 *
1639 * Changes in common status bits are propagated to attached LUNs.
Christof Schmitt287ac012008-07-02 10:56:40 +02001640 */
Swen Schilligedaed852010-09-08 14:40:01 +02001641void zfcp_erp_clear_port_status(struct zfcp_port *port, u32 mask)
1642{
1643 struct scsi_device *sdev;
1644 u32 common_mask = mask & ZFCP_COMMON_FLAGS;
1645 u32 clear_counter = mask & ZFCP_STATUS_COMMON_ERP_FAILED;
Martin Peschke924dd582013-08-22 17:45:37 +02001646 unsigned long flags;
Swen Schilligedaed852010-09-08 14:40:01 +02001647
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001648 atomic_andnot(mask, &port->status);
Swen Schilligedaed852010-09-08 14:40:01 +02001649
1650 if (!common_mask)
1651 return;
1652
1653 if (clear_counter)
1654 atomic_set(&port->erp_counter, 0);
1655
Martin Peschke924dd582013-08-22 17:45:37 +02001656 spin_lock_irqsave(port->adapter->scsi_host->host_lock, flags);
1657 __shost_for_each_device(sdev, port->adapter->scsi_host)
Swen Schilligedaed852010-09-08 14:40:01 +02001658 if (sdev_to_zfcp(sdev)->port == port) {
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001659 atomic_andnot(common_mask,
Swen Schilligedaed852010-09-08 14:40:01 +02001660 &sdev_to_zfcp(sdev)->status);
1661 if (clear_counter)
1662 atomic_set(&sdev_to_zfcp(sdev)->erp_counter, 0);
1663 }
Martin Peschke924dd582013-08-22 17:45:37 +02001664 spin_unlock_irqrestore(port->adapter->scsi_host->host_lock, flags);
Swen Schilligedaed852010-09-08 14:40:01 +02001665}
1666
1667/**
1668 * zfcp_erp_set_lun_status - set lun status bits
1669 * @sdev: SCSI device / lun to set the status bits
1670 * @mask: status bits to change
1671 */
1672void zfcp_erp_set_lun_status(struct scsi_device *sdev, u32 mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673{
Christof Schmittb62a8d92010-09-08 14:39:55 +02001674 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
1675
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001676 atomic_or(mask, &zfcp_sdev->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677}
1678
Christof Schmitt287ac012008-07-02 10:56:40 +02001679/**
Swen Schilligedaed852010-09-08 14:40:01 +02001680 * zfcp_erp_clear_lun_status - clear lun status bits
1681 * @sdev: SCSi device / lun to clear the status bits
1682 * @mask: status bits to change
Christof Schmitt287ac012008-07-02 10:56:40 +02001683 */
Swen Schilligedaed852010-09-08 14:40:01 +02001684void zfcp_erp_clear_lun_status(struct scsi_device *sdev, u32 mask)
Andreas Herrmannd736a27b2005-06-13 13:23:57 +02001685{
Swen Schilligedaed852010-09-08 14:40:01 +02001686 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
1687
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001688 atomic_andnot(mask, &zfcp_sdev->status);
Swen Schilligedaed852010-09-08 14:40:01 +02001689
1690 if (mask & ZFCP_STATUS_COMMON_ERP_FAILED)
1691 atomic_set(&zfcp_sdev->erp_counter, 0);
Andreas Herrmannd736a27b2005-06-13 13:23:57 +02001692}
1693
Steffen Maier35e91112018-05-17 19:15:03 +02001694/**
1695 * zfcp_erp_adapter_reset_sync() - Really reopen adapter and wait.
1696 * @adapter: Pointer to zfcp_adapter to reopen.
1697 * @id: Trace tag string of length %ZFCP_DBF_TAG_LEN.
1698 */
1699void zfcp_erp_adapter_reset_sync(struct zfcp_adapter *adapter, char *id)
1700{
1701 zfcp_erp_set_adapter_status(adapter, ZFCP_STATUS_COMMON_RUNNING);
1702 zfcp_erp_adapter_reopen(adapter, ZFCP_STATUS_COMMON_ERP_FAILED, id);
1703 zfcp_erp_wait(adapter);
1704}