]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/s390/scsi/zfcp_erp.c
License cleanup: add SPDX GPL-2.0 license identifier to files with no license
[mirror_ubuntu-bionic-kernel.git] / drivers / s390 / scsi / zfcp_erp.c
CommitLineData
b2441318 1// SPDX-License-Identifier: GPL-2.0
41fa2ada 2/*
553448f6 3 * zfcp device driver
1da177e4 4 *
553448f6 5 * Error Recovery Procedures (ERP).
41fa2ada 6 *
6f2ce1c6 7 * Copyright IBM Corp. 2002, 2016
1da177e4
LT
8 */
9
ecf39d42
CS
10#define KMSG_COMPONENT "zfcp"
11#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
12
347c6a96 13#include <linux/kthread.h>
1da177e4 14#include "zfcp_ext.h"
b6bd2fb9 15#include "zfcp_reqlist.h"
1da177e4 16
287ac01a
CS
17#define ZFCP_MAX_ERPS 3
18
19enum 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,
fdbd1c5e 25 ZFCP_STATUS_ERP_NO_REF = 0x00800000,
287ac01a 26};
1da177e4 27
287ac01a
CS
28enum 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,
287ac01a 33 ZFCP_ERP_STEP_PORT_OPENING = 0x0800,
b62a8d9b
CS
34 ZFCP_ERP_STEP_LUN_CLOSING = 0x1000,
35 ZFCP_ERP_STEP_LUN_OPENING = 0x2000,
287ac01a
CS
36};
37
38enum zfcp_erp_act_type {
b62a8d9b 39 ZFCP_ERP_ACTION_REOPEN_LUN = 1,
287ac01a
CS
40 ZFCP_ERP_ACTION_REOPEN_PORT = 2,
41 ZFCP_ERP_ACTION_REOPEN_PORT_FORCED = 3,
42 ZFCP_ERP_ACTION_REOPEN_ADAPTER = 4,
43};
44
45enum zfcp_erp_act_state {
46 ZFCP_ERP_ACTION_RUNNING = 1,
47 ZFCP_ERP_ACTION_READY = 2,
48};
49
50enum zfcp_erp_act_result {
51 ZFCP_ERP_SUCCEEDED = 0,
52 ZFCP_ERP_FAILED = 1,
53 ZFCP_ERP_CONTINUES = 2,
54 ZFCP_ERP_EXIT = 3,
55 ZFCP_ERP_DISMISSED = 4,
56 ZFCP_ERP_NOMEM = 5,
57};
58
59static void zfcp_erp_adapter_block(struct zfcp_adapter *adapter, int mask)
1da177e4 60{
edaed859
SS
61 zfcp_erp_clear_adapter_status(adapter,
62 ZFCP_STATUS_COMMON_UNBLOCKED | mask);
2abbe866 63}
1da177e4 64
287ac01a 65static int zfcp_erp_action_exists(struct zfcp_erp_action *act)
2abbe866 66{
287ac01a
CS
67 struct zfcp_erp_action *curr_act;
68
69 list_for_each_entry(curr_act, &act->adapter->erp_running_head, list)
70 if (act == curr_act)
71 return ZFCP_ERP_ACTION_RUNNING;
72 return 0;
1da177e4
LT
73}
74
287ac01a 75static void zfcp_erp_action_ready(struct zfcp_erp_action *act)
2abbe866 76{
287ac01a
CS
77 struct zfcp_adapter *adapter = act->adapter;
78
79 list_move(&act->list, &act->adapter->erp_ready_head);
ae0904f6 80 zfcp_dbf_rec_run("erardy1", act);
347c6a96 81 wake_up(&adapter->erp_ready_wq);
ae0904f6 82 zfcp_dbf_rec_run("erardy2", act);
2abbe866
AH
83}
84
287ac01a 85static void zfcp_erp_action_dismiss(struct zfcp_erp_action *act)
1da177e4 86{
287ac01a
CS
87 act->status |= ZFCP_STATUS_ERP_DISMISSED;
88 if (zfcp_erp_action_exists(act) == ZFCP_ERP_ACTION_RUNNING)
89 zfcp_erp_action_ready(act);
90}
1da177e4 91
b62a8d9b 92static void zfcp_erp_action_dismiss_lun(struct scsi_device *sdev)
287ac01a 93{
b62a8d9b
CS
94 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
95
96 if (atomic_read(&zfcp_sdev->status) & ZFCP_STATUS_COMMON_ERP_INUSE)
97 zfcp_erp_action_dismiss(&zfcp_sdev->erp_action);
287ac01a 98}
1da177e4 99
287ac01a
CS
100static void zfcp_erp_action_dismiss_port(struct zfcp_port *port)
101{
b62a8d9b 102 struct scsi_device *sdev;
1da177e4 103
287ac01a
CS
104 if (atomic_read(&port->status) & ZFCP_STATUS_COMMON_ERP_INUSE)
105 zfcp_erp_action_dismiss(&port->erp_action);
924dd584
MP
106 else {
107 spin_lock(port->adapter->scsi_host->host_lock);
108 __shost_for_each_device(sdev, port->adapter->scsi_host)
b62a8d9b
CS
109 if (sdev_to_zfcp(sdev)->port == port)
110 zfcp_erp_action_dismiss_lun(sdev);
924dd584
MP
111 spin_unlock(port->adapter->scsi_host->host_lock);
112 }
1da177e4
LT
113}
114
287ac01a 115static void zfcp_erp_action_dismiss_adapter(struct zfcp_adapter *adapter)
1da177e4 116{
287ac01a 117 struct zfcp_port *port;
1da177e4 118
287ac01a
CS
119 if (atomic_read(&adapter->status) & ZFCP_STATUS_COMMON_ERP_INUSE)
120 zfcp_erp_action_dismiss(&adapter->erp_action);
ecf0c772
SS
121 else {
122 read_lock(&adapter->port_list_lock);
123 list_for_each_entry(port, &adapter->port_list, list)
287ac01a 124 zfcp_erp_action_dismiss_port(port);
ecf0c772
SS
125 read_unlock(&adapter->port_list_lock);
126 }
1da177e4
LT
127}
128
287ac01a
CS
129static int zfcp_erp_required_act(int want, struct zfcp_adapter *adapter,
130 struct zfcp_port *port,
b62a8d9b 131 struct scsi_device *sdev)
1da177e4 132{
287ac01a 133 int need = want;
b62a8d9b
CS
134 int l_status, p_status, a_status;
135 struct zfcp_scsi_dev *zfcp_sdev;
1da177e4 136
287ac01a 137 switch (want) {
b62a8d9b
CS
138 case ZFCP_ERP_ACTION_REOPEN_LUN:
139 zfcp_sdev = sdev_to_zfcp(sdev);
140 l_status = atomic_read(&zfcp_sdev->status);
141 if (l_status & ZFCP_STATUS_COMMON_ERP_INUSE)
287ac01a
CS
142 return 0;
143 p_status = atomic_read(&port->status);
144 if (!(p_status & ZFCP_STATUS_COMMON_RUNNING) ||
145 p_status & ZFCP_STATUS_COMMON_ERP_FAILED)
146 return 0;
147 if (!(p_status & ZFCP_STATUS_COMMON_UNBLOCKED))
148 need = ZFCP_ERP_ACTION_REOPEN_PORT;
149 /* fall through */
287ac01a 150 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
097ef3bd
CS
151 p_status = atomic_read(&port->status);
152 if (!(p_status & ZFCP_STATUS_COMMON_OPEN))
153 need = ZFCP_ERP_ACTION_REOPEN_PORT;
154 /* fall through */
155 case ZFCP_ERP_ACTION_REOPEN_PORT:
287ac01a
CS
156 p_status = atomic_read(&port->status);
157 if (p_status & ZFCP_STATUS_COMMON_ERP_INUSE)
158 return 0;
159 a_status = atomic_read(&adapter->status);
160 if (!(a_status & ZFCP_STATUS_COMMON_RUNNING) ||
161 a_status & ZFCP_STATUS_COMMON_ERP_FAILED)
162 return 0;
d3e1088d
SS
163 if (p_status & ZFCP_STATUS_COMMON_NOESC)
164 return need;
287ac01a
CS
165 if (!(a_status & ZFCP_STATUS_COMMON_UNBLOCKED))
166 need = ZFCP_ERP_ACTION_REOPEN_ADAPTER;
167 /* fall through */
168 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
169 a_status = atomic_read(&adapter->status);
170 if (a_status & ZFCP_STATUS_COMMON_ERP_INUSE)
171 return 0;
143bb6bf
CS
172 if (!(a_status & ZFCP_STATUS_COMMON_RUNNING) &&
173 !(a_status & ZFCP_STATUS_COMMON_OPEN))
174 return 0; /* shutdown requested for closed adapter */
287ac01a 175 }
1da177e4 176
287ac01a 177 return need;
1da177e4
LT
178}
179
fdbd1c5e 180static struct zfcp_erp_action *zfcp_erp_setup_act(int need, u32 act_status,
287ac01a
CS
181 struct zfcp_adapter *adapter,
182 struct zfcp_port *port,
b62a8d9b 183 struct scsi_device *sdev)
1da177e4 184{
287ac01a 185 struct zfcp_erp_action *erp_action;
b62a8d9b 186 struct zfcp_scsi_dev *zfcp_sdev;
1da177e4 187
287ac01a 188 switch (need) {
b62a8d9b
CS
189 case ZFCP_ERP_ACTION_REOPEN_LUN:
190 zfcp_sdev = sdev_to_zfcp(sdev);
fdbd1c5e 191 if (!(act_status & ZFCP_STATUS_ERP_NO_REF))
b62a8d9b 192 if (scsi_device_get(sdev))
fdbd1c5e 193 return NULL;
805de8f4 194 atomic_or(ZFCP_STATUS_COMMON_ERP_INUSE,
b62a8d9b
CS
195 &zfcp_sdev->status);
196 erp_action = &zfcp_sdev->erp_action;
14718e3c
SS
197 memset(erp_action, 0, sizeof(struct zfcp_erp_action));
198 erp_action->port = port;
199 erp_action->sdev = sdev;
b62a8d9b
CS
200 if (!(atomic_read(&zfcp_sdev->status) &
201 ZFCP_STATUS_COMMON_RUNNING))
fdbd1c5e 202 act_status |= ZFCP_STATUS_ERP_CLOSE_ONLY;
287ac01a 203 break;
1da177e4 204
287ac01a
CS
205 case ZFCP_ERP_ACTION_REOPEN_PORT:
206 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
615f59e0 207 if (!get_device(&port->dev))
6b183334 208 return NULL;
287ac01a 209 zfcp_erp_action_dismiss_port(port);
805de8f4 210 atomic_or(ZFCP_STATUS_COMMON_ERP_INUSE, &port->status);
287ac01a 211 erp_action = &port->erp_action;
14718e3c
SS
212 memset(erp_action, 0, sizeof(struct zfcp_erp_action));
213 erp_action->port = port;
287ac01a 214 if (!(atomic_read(&port->status) & ZFCP_STATUS_COMMON_RUNNING))
fdbd1c5e 215 act_status |= ZFCP_STATUS_ERP_CLOSE_ONLY;
287ac01a 216 break;
1da177e4 217
287ac01a 218 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
f3450c7b 219 kref_get(&adapter->ref);
287ac01a 220 zfcp_erp_action_dismiss_adapter(adapter);
805de8f4 221 atomic_or(ZFCP_STATUS_COMMON_ERP_INUSE, &adapter->status);
287ac01a 222 erp_action = &adapter->erp_action;
14718e3c 223 memset(erp_action, 0, sizeof(struct zfcp_erp_action));
287ac01a
CS
224 if (!(atomic_read(&adapter->status) &
225 ZFCP_STATUS_COMMON_RUNNING))
fdbd1c5e 226 act_status |= ZFCP_STATUS_ERP_CLOSE_ONLY;
287ac01a
CS
227 break;
228
229 default:
230 return NULL;
231 }
1da177e4 232
287ac01a 233 erp_action->adapter = adapter;
287ac01a 234 erp_action->action = need;
fdbd1c5e 235 erp_action->status = act_status;
1da177e4 236
287ac01a 237 return erp_action;
1da177e4
LT
238}
239
287ac01a
CS
240static int zfcp_erp_action_enqueue(int want, struct zfcp_adapter *adapter,
241 struct zfcp_port *port,
b62a8d9b 242 struct scsi_device *sdev,
ea4a3a6a 243 char *id, u32 act_status)
1da177e4 244{
287ac01a 245 int retval = 1, need;
ae0904f6 246 struct zfcp_erp_action *act;
1da177e4 247
347c6a96 248 if (!adapter->erp_thread)
287ac01a 249 return -EIO;
1da177e4 250
b62a8d9b 251 need = zfcp_erp_required_act(want, adapter, port, sdev);
287ac01a 252 if (!need)
1da177e4 253 goto out;
1da177e4 254
b62a8d9b 255 act = zfcp_erp_setup_act(need, act_status, adapter, port, sdev);
287ac01a
CS
256 if (!act)
257 goto out;
805de8f4 258 atomic_or(ZFCP_STATUS_ADAPTER_ERP_PENDING, &adapter->status);
287ac01a
CS
259 ++adapter->erp_total_count;
260 list_add_tail(&act->list, &adapter->erp_ready_head);
347c6a96 261 wake_up(&adapter->erp_ready_wq);
287ac01a 262 retval = 0;
1da177e4 263 out:
ae0904f6 264 zfcp_dbf_rec_trig(id, adapter, port, sdev, want, need);
1da177e4
LT
265 return retval;
266}
267
287ac01a 268static int _zfcp_erp_adapter_reopen(struct zfcp_adapter *adapter,
ea4a3a6a 269 int clear_mask, char *id)
287ac01a
CS
270{
271 zfcp_erp_adapter_block(adapter, clear_mask);
a2fa0aed 272 zfcp_scsi_schedule_rports_block(adapter);
287ac01a
CS
273
274 /* ensure propagation of failed status to new devices */
275 if (atomic_read(&adapter->status) & ZFCP_STATUS_COMMON_ERP_FAILED) {
edaed859
SS
276 zfcp_erp_set_adapter_status(adapter,
277 ZFCP_STATUS_COMMON_ERP_FAILED);
287ac01a
CS
278 return -EIO;
279 }
280 return zfcp_erp_action_enqueue(ZFCP_ERP_ACTION_REOPEN_ADAPTER,
ea4a3a6a 281 adapter, NULL, NULL, id, 0);
287ac01a
CS
282}
283
284/**
285 * zfcp_erp_adapter_reopen - Reopen adapter.
286 * @adapter: Adapter to reopen.
287 * @clear: Status flags to clear.
288 * @id: Id for debug trace event.
1da177e4 289 */
ea4a3a6a 290void zfcp_erp_adapter_reopen(struct zfcp_adapter *adapter, int clear, char *id)
1da177e4 291{
1da177e4 292 unsigned long flags;
1da177e4 293
ecf0c772
SS
294 zfcp_erp_adapter_block(adapter, clear);
295 zfcp_scsi_schedule_rports_block(adapter);
296
297 write_lock_irqsave(&adapter->erp_lock, flags);
298 if (atomic_read(&adapter->status) & ZFCP_STATUS_COMMON_ERP_FAILED)
edaed859
SS
299 zfcp_erp_set_adapter_status(adapter,
300 ZFCP_STATUS_COMMON_ERP_FAILED);
ecf0c772
SS
301 else
302 zfcp_erp_action_enqueue(ZFCP_ERP_ACTION_REOPEN_ADAPTER, adapter,
ea4a3a6a 303 NULL, NULL, id, 0);
ecf0c772 304 write_unlock_irqrestore(&adapter->erp_lock, flags);
287ac01a 305}
1da177e4 306
287ac01a
CS
307/**
308 * zfcp_erp_adapter_shutdown - Shutdown adapter.
309 * @adapter: Adapter to shut down.
310 * @clear: Status flags to clear.
311 * @id: Id for debug trace event.
287ac01a
CS
312 */
313void zfcp_erp_adapter_shutdown(struct zfcp_adapter *adapter, int clear,
ea4a3a6a 314 char *id)
287ac01a
CS
315{
316 int flags = ZFCP_STATUS_COMMON_RUNNING | ZFCP_STATUS_COMMON_ERP_FAILED;
ea4a3a6a 317 zfcp_erp_adapter_reopen(adapter, clear | flags, id);
1da177e4
LT
318}
319
287ac01a
CS
320/**
321 * zfcp_erp_port_shutdown - Shutdown port
322 * @port: Port to shut down.
323 * @clear: Status flags to clear.
324 * @id: Id for debug trace event.
1da177e4 325 */
ea4a3a6a 326void zfcp_erp_port_shutdown(struct zfcp_port *port, int clear, char *id)
1da177e4 327{
287ac01a 328 int flags = ZFCP_STATUS_COMMON_RUNNING | ZFCP_STATUS_COMMON_ERP_FAILED;
ea4a3a6a 329 zfcp_erp_port_reopen(port, clear | flags, id);
287ac01a
CS
330}
331
287ac01a
CS
332static void zfcp_erp_port_block(struct zfcp_port *port, int clear)
333{
edaed859
SS
334 zfcp_erp_clear_port_status(port,
335 ZFCP_STATUS_COMMON_UNBLOCKED | clear);
287ac01a
CS
336}
337
ea4a3a6a
SS
338static void _zfcp_erp_port_forced_reopen(struct zfcp_port *port, int clear,
339 char *id)
287ac01a
CS
340{
341 zfcp_erp_port_block(port, clear);
a2fa0aed 342 zfcp_scsi_schedule_rport_block(port);
287ac01a
CS
343
344 if (atomic_read(&port->status) & ZFCP_STATUS_COMMON_ERP_FAILED)
345 return;
1da177e4 346
287ac01a 347 zfcp_erp_action_enqueue(ZFCP_ERP_ACTION_REOPEN_PORT_FORCED,
ea4a3a6a 348 port->adapter, port, NULL, id, 0);
287ac01a
CS
349}
350
351/**
352 * zfcp_erp_port_forced_reopen - Forced close of port and open again
353 * @port: Port to force close and to reopen.
ea4a3a6a 354 * @clear: Status flags to clear.
287ac01a 355 * @id: Id for debug trace event.
287ac01a 356 */
ea4a3a6a 357void zfcp_erp_port_forced_reopen(struct zfcp_port *port, int clear, char *id)
287ac01a
CS
358{
359 unsigned long flags;
360 struct zfcp_adapter *adapter = port->adapter;
361
ecf0c772 362 write_lock_irqsave(&adapter->erp_lock, flags);
ea4a3a6a 363 _zfcp_erp_port_forced_reopen(port, clear, id);
ecf0c772 364 write_unlock_irqrestore(&adapter->erp_lock, flags);
287ac01a
CS
365}
366
ea4a3a6a 367static int _zfcp_erp_port_reopen(struct zfcp_port *port, int clear, char *id)
287ac01a
CS
368{
369 zfcp_erp_port_block(port, clear);
a2fa0aed 370 zfcp_scsi_schedule_rport_block(port);
1da177e4 371
287ac01a 372 if (atomic_read(&port->status) & ZFCP_STATUS_COMMON_ERP_FAILED) {
1da177e4 373 /* ensure propagation of failed status to new devices */
edaed859 374 zfcp_erp_set_port_status(port, ZFCP_STATUS_COMMON_ERP_FAILED);
287ac01a 375 return -EIO;
1da177e4
LT
376 }
377
287ac01a 378 return zfcp_erp_action_enqueue(ZFCP_ERP_ACTION_REOPEN_PORT,
ea4a3a6a 379 port->adapter, port, NULL, id, 0);
1da177e4
LT
380}
381
382/**
287ac01a
CS
383 * zfcp_erp_port_reopen - trigger remote port recovery
384 * @port: port to recover
385 * @clear_mask: flags in port status to be cleared
ea4a3a6a 386 * @id: Id for debug trace event.
1da177e4 387 *
287ac01a 388 * Returns 0 if recovery has been triggered, < 0 if not.
1da177e4 389 */
ea4a3a6a 390int zfcp_erp_port_reopen(struct zfcp_port *port, int clear, char *id)
1da177e4 391{
287ac01a 392 int retval;
ecf0c772 393 unsigned long flags;
1da177e4
LT
394 struct zfcp_adapter *adapter = port->adapter;
395
ecf0c772 396 write_lock_irqsave(&adapter->erp_lock, flags);
ea4a3a6a 397 retval = _zfcp_erp_port_reopen(port, clear, id);
ecf0c772 398 write_unlock_irqrestore(&adapter->erp_lock, flags);
1da177e4
LT
399
400 return retval;
401}
402
b62a8d9b 403static void zfcp_erp_lun_block(struct scsi_device *sdev, int clear_mask)
287ac01a 404{
edaed859
SS
405 zfcp_erp_clear_lun_status(sdev,
406 ZFCP_STATUS_COMMON_UNBLOCKED | clear_mask);
287ac01a
CS
407}
408
b62a8d9b 409static void _zfcp_erp_lun_reopen(struct scsi_device *sdev, int clear, char *id,
ea4a3a6a 410 u32 act_status)
1da177e4 411{
b62a8d9b
CS
412 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
413 struct zfcp_adapter *adapter = zfcp_sdev->port->adapter;
1da177e4 414
b62a8d9b 415 zfcp_erp_lun_block(sdev, clear);
1da177e4 416
b62a8d9b 417 if (atomic_read(&zfcp_sdev->status) & ZFCP_STATUS_COMMON_ERP_FAILED)
287ac01a 418 return;
1da177e4 419
b62a8d9b 420 zfcp_erp_action_enqueue(ZFCP_ERP_ACTION_REOPEN_LUN, adapter,
ea4a3a6a 421 zfcp_sdev->port, sdev, id, act_status);
1da177e4
LT
422}
423
424/**
b62a8d9b
CS
425 * zfcp_erp_lun_reopen - initiate reopen of a LUN
426 * @sdev: SCSI device / LUN to be reopened
427 * @clear_mask: specifies flags in LUN status to be cleared
ea4a3a6a
SS
428 * @id: Id for debug trace event.
429 *
1da177e4 430 * Return: 0 on success, < 0 on error
1da177e4 431 */
ea4a3a6a 432void zfcp_erp_lun_reopen(struct scsi_device *sdev, int clear, char *id)
1da177e4 433{
1da177e4 434 unsigned long flags;
b62a8d9b
CS
435 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
436 struct zfcp_port *port = zfcp_sdev->port;
287ac01a 437 struct zfcp_adapter *adapter = port->adapter;
1da177e4 438
ecf0c772 439 write_lock_irqsave(&adapter->erp_lock, flags);
ea4a3a6a 440 _zfcp_erp_lun_reopen(sdev, clear, id, 0);
fdbd1c5e
CS
441 write_unlock_irqrestore(&adapter->erp_lock, flags);
442}
443
444/**
b62a8d9b
CS
445 * zfcp_erp_lun_shutdown - Shutdown LUN
446 * @sdev: SCSI device / LUN to shut down.
fdbd1c5e
CS
447 * @clear: Status flags to clear.
448 * @id: Id for debug trace event.
fdbd1c5e 449 */
ea4a3a6a 450void zfcp_erp_lun_shutdown(struct scsi_device *sdev, int clear, char *id)
fdbd1c5e
CS
451{
452 int flags = ZFCP_STATUS_COMMON_RUNNING | ZFCP_STATUS_COMMON_ERP_FAILED;
ea4a3a6a 453 zfcp_erp_lun_reopen(sdev, clear | flags, id);
fdbd1c5e
CS
454}
455
456/**
b62a8d9b
CS
457 * zfcp_erp_lun_shutdown_wait - Shutdown LUN and wait for erp completion
458 * @sdev: SCSI device / LUN to shut down.
fdbd1c5e
CS
459 * @id: Id for debug trace event.
460 *
b62a8d9b 461 * Do not acquire a reference for the LUN when creating the ERP
fdbd1c5e 462 * action. It is safe, because this function waits for the ERP to
b62a8d9b
CS
463 * complete first. This allows to shutdown the LUN, even when the SCSI
464 * device is in the state SDEV_DEL when scsi_device_get will fail.
fdbd1c5e 465 */
b62a8d9b 466void zfcp_erp_lun_shutdown_wait(struct scsi_device *sdev, char *id)
fdbd1c5e
CS
467{
468 unsigned long flags;
b62a8d9b
CS
469 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
470 struct zfcp_port *port = zfcp_sdev->port;
fdbd1c5e
CS
471 struct zfcp_adapter *adapter = port->adapter;
472 int clear = ZFCP_STATUS_COMMON_RUNNING | ZFCP_STATUS_COMMON_ERP_FAILED;
473
474 write_lock_irqsave(&adapter->erp_lock, flags);
ea4a3a6a 475 _zfcp_erp_lun_reopen(sdev, clear, id, ZFCP_STATUS_ERP_NO_REF);
ecf0c772 476 write_unlock_irqrestore(&adapter->erp_lock, flags);
fdbd1c5e
CS
477
478 zfcp_erp_wait(adapter);
1da177e4
LT
479}
480
287ac01a 481static int status_change_set(unsigned long mask, atomic_t *status)
1da177e4 482{
287ac01a 483 return (atomic_read(status) ^ mask) & mask;
1da177e4
LT
484}
485
287ac01a 486static void zfcp_erp_adapter_unblock(struct zfcp_adapter *adapter)
698ec016 487{
287ac01a 488 if (status_change_set(ZFCP_STATUS_COMMON_UNBLOCKED, &adapter->status))
ae0904f6 489 zfcp_dbf_rec_run("eraubl1", &adapter->erp_action);
805de8f4 490 atomic_or(ZFCP_STATUS_COMMON_UNBLOCKED, &adapter->status);
698ec016
MP
491}
492
287ac01a 493static void zfcp_erp_port_unblock(struct zfcp_port *port)
1da177e4 494{
287ac01a 495 if (status_change_set(ZFCP_STATUS_COMMON_UNBLOCKED, &port->status))
ae0904f6 496 zfcp_dbf_rec_run("erpubl1", &port->erp_action);
805de8f4 497 atomic_or(ZFCP_STATUS_COMMON_UNBLOCKED, &port->status);
1da177e4
LT
498}
499
b62a8d9b 500static void zfcp_erp_lun_unblock(struct scsi_device *sdev)
1da177e4 501{
b62a8d9b
CS
502 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
503
504 if (status_change_set(ZFCP_STATUS_COMMON_UNBLOCKED, &zfcp_sdev->status))
ae0904f6 505 zfcp_dbf_rec_run("erlubl1", &sdev_to_zfcp(sdev)->erp_action);
805de8f4 506 atomic_or(ZFCP_STATUS_COMMON_UNBLOCKED, &zfcp_sdev->status);
1da177e4
LT
507}
508
287ac01a 509static void zfcp_erp_action_to_running(struct zfcp_erp_action *erp_action)
1da177e4 510{
287ac01a 511 list_move(&erp_action->list, &erp_action->adapter->erp_running_head);
ae0904f6 512 zfcp_dbf_rec_run("erator1", erp_action);
1da177e4
LT
513}
514
287ac01a 515static void zfcp_erp_strategy_check_fsfreq(struct zfcp_erp_action *act)
1da177e4 516{
287ac01a 517 struct zfcp_adapter *adapter = act->adapter;
e60a6d69 518 struct zfcp_fsf_req *req;
1da177e4 519
e60a6d69 520 if (!act->fsf_req_id)
287ac01a 521 return;
1da177e4 522
b6bd2fb9
CS
523 spin_lock(&adapter->req_list->lock);
524 req = _zfcp_reqlist_find(adapter->req_list, act->fsf_req_id);
e60a6d69 525 if (req && req->erp_action == act) {
287ac01a
CS
526 if (act->status & (ZFCP_STATUS_ERP_DISMISSED |
527 ZFCP_STATUS_ERP_TIMEDOUT)) {
e60a6d69 528 req->status |= ZFCP_STATUS_FSFREQ_DISMISSED;
ae0904f6 529 zfcp_dbf_rec_run("erscf_1", act);
e60a6d69 530 req->erp_action = NULL;
1da177e4 531 }
287ac01a 532 if (act->status & ZFCP_STATUS_ERP_TIMEDOUT)
ae0904f6 533 zfcp_dbf_rec_run("erscf_2", act);
e60a6d69
CS
534 if (req->status & ZFCP_STATUS_FSFREQ_DISMISSED)
535 act->fsf_req_id = 0;
287ac01a 536 } else
e60a6d69 537 act->fsf_req_id = 0;
b6bd2fb9 538 spin_unlock(&adapter->req_list->lock);
1da177e4
LT
539}
540
287ac01a
CS
541/**
542 * zfcp_erp_notify - Trigger ERP action.
543 * @erp_action: ERP action to continue.
544 * @set_mask: ERP action status flags to set.
1da177e4 545 */
287ac01a 546void zfcp_erp_notify(struct zfcp_erp_action *erp_action, unsigned long set_mask)
1da177e4 547{
1da177e4 548 struct zfcp_adapter *adapter = erp_action->adapter;
287ac01a 549 unsigned long flags;
1da177e4 550
287ac01a 551 write_lock_irqsave(&adapter->erp_lock, flags);
1da177e4 552 if (zfcp_erp_action_exists(erp_action) == ZFCP_ERP_ACTION_RUNNING) {
1da177e4
LT
553 erp_action->status |= set_mask;
554 zfcp_erp_action_ready(erp_action);
1da177e4 555 }
1da177e4 556 write_unlock_irqrestore(&adapter->erp_lock, flags);
1da177e4
LT
557}
558
287ac01a
CS
559/**
560 * zfcp_erp_timeout_handler - Trigger ERP action from timed out ERP request
561 * @data: ERP action (from timer data)
1da177e4 562 */
287ac01a 563void zfcp_erp_timeout_handler(unsigned long data)
1da177e4 564{
287ac01a
CS
565 struct zfcp_erp_action *act = (struct zfcp_erp_action *) data;
566 zfcp_erp_notify(act, ZFCP_STATUS_ERP_TIMEDOUT);
1da177e4
LT
567}
568
287ac01a 569static void zfcp_erp_memwait_handler(unsigned long data)
1da177e4 570{
287ac01a 571 zfcp_erp_notify((struct zfcp_erp_action *)data, 0);
1da177e4
LT
572}
573
287ac01a 574static void zfcp_erp_strategy_memwait(struct zfcp_erp_action *erp_action)
1da177e4 575{
bc46427e
LK
576 setup_timer(&erp_action->timer, zfcp_erp_memwait_handler,
577 (unsigned long) erp_action);
287ac01a
CS
578 erp_action->timer.expires = jiffies + HZ;
579 add_timer(&erp_action->timer);
1da177e4
LT
580}
581
287ac01a 582static void _zfcp_erp_port_reopen_all(struct zfcp_adapter *adapter,
ea4a3a6a 583 int clear, char *id)
1da177e4 584{
287ac01a 585 struct zfcp_port *port;
1da177e4 586
ecf0c772
SS
587 read_lock(&adapter->port_list_lock);
588 list_for_each_entry(port, &adapter->port_list, list)
ea4a3a6a 589 _zfcp_erp_port_reopen(port, clear, id);
ecf0c772 590 read_unlock(&adapter->port_list_lock);
1da177e4
LT
591}
592
b62a8d9b 593static void _zfcp_erp_lun_reopen_all(struct zfcp_port *port, int clear,
ea4a3a6a 594 char *id)
1da177e4 595{
b62a8d9b 596 struct scsi_device *sdev;
1da177e4 597
924dd584
MP
598 spin_lock(port->adapter->scsi_host->host_lock);
599 __shost_for_each_device(sdev, port->adapter->scsi_host)
b62a8d9b 600 if (sdev_to_zfcp(sdev)->port == port)
ea4a3a6a 601 _zfcp_erp_lun_reopen(sdev, clear, id, 0);
924dd584 602 spin_unlock(port->adapter->scsi_host->host_lock);
1da177e4
LT
603}
604
85600f7f 605static void zfcp_erp_strategy_followup_failed(struct zfcp_erp_action *act)
1da177e4 606{
287ac01a 607 switch (act->action) {
287ac01a 608 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
ea4a3a6a 609 _zfcp_erp_adapter_reopen(act->adapter, 0, "ersff_1");
287ac01a 610 break;
287ac01a 611 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
ea4a3a6a 612 _zfcp_erp_port_forced_reopen(act->port, 0, "ersff_2");
287ac01a 613 break;
287ac01a 614 case ZFCP_ERP_ACTION_REOPEN_PORT:
ea4a3a6a 615 _zfcp_erp_port_reopen(act->port, 0, "ersff_3");
287ac01a 616 break;
b62a8d9b 617 case ZFCP_ERP_ACTION_REOPEN_LUN:
ea4a3a6a 618 _zfcp_erp_lun_reopen(act->sdev, 0, "ersff_4", 0);
85600f7f
CS
619 break;
620 }
621}
622
623static void zfcp_erp_strategy_followup_success(struct zfcp_erp_action *act)
624{
625 switch (act->action) {
626 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
ea4a3a6a 627 _zfcp_erp_port_reopen_all(act->adapter, 0, "ersfs_1");
85600f7f
CS
628 break;
629 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
ea4a3a6a 630 _zfcp_erp_port_reopen(act->port, 0, "ersfs_2");
85600f7f
CS
631 break;
632 case ZFCP_ERP_ACTION_REOPEN_PORT:
ea4a3a6a 633 _zfcp_erp_lun_reopen_all(act->port, 0, "ersfs_3");
287ac01a 634 break;
1da177e4 635 }
1da177e4
LT
636}
637
287ac01a 638static void zfcp_erp_wakeup(struct zfcp_adapter *adapter)
1da177e4 639{
1da177e4
LT
640 unsigned long flags;
641
ecf0c772 642 read_lock_irqsave(&adapter->erp_lock, flags);
287ac01a
CS
643 if (list_empty(&adapter->erp_ready_head) &&
644 list_empty(&adapter->erp_running_head)) {
805de8f4 645 atomic_andnot(ZFCP_STATUS_ADAPTER_ERP_PENDING,
287ac01a
CS
646 &adapter->status);
647 wake_up(&adapter->erp_done_wqh);
1da177e4 648 }
ecf0c772 649 read_unlock_irqrestore(&adapter->erp_lock, flags);
287ac01a 650}
1da177e4 651
287ac01a
CS
652static void zfcp_erp_enqueue_ptp_port(struct zfcp_adapter *adapter)
653{
654 struct zfcp_port *port;
655 port = zfcp_port_enqueue(adapter, adapter->peer_wwpn, 0,
656 adapter->peer_d_id);
657 if (IS_ERR(port)) /* error or port already attached */
658 return;
ea4a3a6a 659 _zfcp_erp_port_reopen(port, 0, "ereptp1");
287ac01a 660}
1da177e4 661
287ac01a
CS
662static int zfcp_erp_adapter_strat_fsf_xconf(struct zfcp_erp_action *erp_action)
663{
664 int retries;
665 int sleep = 1;
666 struct zfcp_adapter *adapter = erp_action->adapter;
1da177e4 667
805de8f4 668 atomic_andnot(ZFCP_STATUS_ADAPTER_XCONFIG_OK, &adapter->status);
287ac01a
CS
669
670 for (retries = 7; retries; retries--) {
805de8f4 671 atomic_andnot(ZFCP_STATUS_ADAPTER_HOST_CON_INIT,
287ac01a
CS
672 &adapter->status);
673 write_lock_irq(&adapter->erp_lock);
674 zfcp_erp_action_to_running(erp_action);
675 write_unlock_irq(&adapter->erp_lock);
676 if (zfcp_fsf_exchange_config_data(erp_action)) {
805de8f4 677 atomic_andnot(ZFCP_STATUS_ADAPTER_HOST_CON_INIT,
287ac01a
CS
678 &adapter->status);
679 return ZFCP_ERP_FAILED;
1da177e4 680 }
1da177e4 681
347c6a96
CS
682 wait_event(adapter->erp_ready_wq,
683 !list_empty(&adapter->erp_ready_head));
287ac01a
CS
684 if (erp_action->status & ZFCP_STATUS_ERP_TIMEDOUT)
685 break;
1da177e4 686
287ac01a
CS
687 if (!(atomic_read(&adapter->status) &
688 ZFCP_STATUS_ADAPTER_HOST_CON_INIT))
689 break;
1da177e4 690
287ac01a
CS
691 ssleep(sleep);
692 sleep *= 2;
1da177e4
LT
693 }
694
805de8f4 695 atomic_andnot(ZFCP_STATUS_ADAPTER_HOST_CON_INIT,
287ac01a 696 &adapter->status);
1da177e4 697
287ac01a
CS
698 if (!(atomic_read(&adapter->status) & ZFCP_STATUS_ADAPTER_XCONFIG_OK))
699 return ZFCP_ERP_FAILED;
41fa2ada 700
287ac01a
CS
701 if (fc_host_port_type(adapter->scsi_host) == FC_PORTTYPE_PTP)
702 zfcp_erp_enqueue_ptp_port(adapter);
1da177e4 703
287ac01a 704 return ZFCP_ERP_SUCCEEDED;
1da177e4
LT
705}
706
287ac01a 707static int zfcp_erp_adapter_strategy_open_fsf_xport(struct zfcp_erp_action *act)
1da177e4 708{
287ac01a
CS
709 int ret;
710 struct zfcp_adapter *adapter = act->adapter;
1da177e4 711
287ac01a
CS
712 write_lock_irq(&adapter->erp_lock);
713 zfcp_erp_action_to_running(act);
714 write_unlock_irq(&adapter->erp_lock);
715
716 ret = zfcp_fsf_exchange_port_data(act);
717 if (ret == -EOPNOTSUPP)
718 return ZFCP_ERP_SUCCEEDED;
719 if (ret)
720 return ZFCP_ERP_FAILED;
721
ae0904f6 722 zfcp_dbf_rec_run("erasox1", act);
347c6a96
CS
723 wait_event(adapter->erp_ready_wq,
724 !list_empty(&adapter->erp_ready_head));
ae0904f6 725 zfcp_dbf_rec_run("erasox2", act);
287ac01a
CS
726 if (act->status & ZFCP_STATUS_ERP_TIMEDOUT)
727 return ZFCP_ERP_FAILED;
728
729 return ZFCP_ERP_SUCCEEDED;
1da177e4
LT
730}
731
287ac01a 732static int zfcp_erp_adapter_strategy_open_fsf(struct zfcp_erp_action *act)
1da177e4 733{
287ac01a
CS
734 if (zfcp_erp_adapter_strat_fsf_xconf(act) == ZFCP_ERP_FAILED)
735 return ZFCP_ERP_FAILED;
1da177e4 736
287ac01a
CS
737 if (zfcp_erp_adapter_strategy_open_fsf_xport(act) == ZFCP_ERP_FAILED)
738 return ZFCP_ERP_FAILED;
1da177e4 739
c7b279ae 740 if (mempool_resize(act->adapter->pool.sr_data,
11d83360 741 act->adapter->stat_read_buf_num))
8d88cf3f
CS
742 return ZFCP_ERP_FAILED;
743
744 if (mempool_resize(act->adapter->pool.status_read_req,
11d83360 745 act->adapter->stat_read_buf_num))
8d88cf3f
CS
746 return ZFCP_ERP_FAILED;
747
64deb6ef 748 atomic_set(&act->adapter->stat_miss, act->adapter->stat_read_buf_num);
287ac01a
CS
749 if (zfcp_status_read_refill(act->adapter))
750 return ZFCP_ERP_FAILED;
1da177e4 751
287ac01a
CS
752 return ZFCP_ERP_SUCCEEDED;
753}
1da177e4 754
cf13c082 755static void zfcp_erp_adapter_strategy_close(struct zfcp_erp_action *act)
287ac01a 756{
287ac01a 757 struct zfcp_adapter *adapter = act->adapter;
1da177e4 758
287ac01a 759 /* close queues to ensure that buffers are not accessed by adapter */
564e1c86 760 zfcp_qdio_close(adapter->qdio);
287ac01a
CS
761 zfcp_fsf_req_dismiss_all(adapter);
762 adapter->fsf_req_seq_no = 0;
55c770fa 763 zfcp_fc_wka_ports_force_offline(adapter->gs);
b62a8d9b 764 /* all ports and LUNs are closed */
edaed859 765 zfcp_erp_clear_adapter_status(adapter, ZFCP_STATUS_COMMON_OPEN);
cf13c082 766
805de8f4 767 atomic_andnot(ZFCP_STATUS_ADAPTER_XCONFIG_OK |
cf13c082 768 ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED, &adapter->status);
1da177e4
LT
769}
770
cf13c082 771static int zfcp_erp_adapter_strategy_open(struct zfcp_erp_action *act)
1da177e4 772{
cf13c082 773 struct zfcp_adapter *adapter = act->adapter;
1da177e4 774
3d63d3b4 775 if (zfcp_qdio_open(adapter->qdio)) {
805de8f4 776 atomic_andnot(ZFCP_STATUS_ADAPTER_XCONFIG_OK |
cf13c082
SS
777 ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED,
778 &adapter->status);
779 return ZFCP_ERP_FAILED;
780 }
287ac01a 781
cf13c082
SS
782 if (zfcp_erp_adapter_strategy_open_fsf(act)) {
783 zfcp_erp_adapter_strategy_close(act);
784 return ZFCP_ERP_FAILED;
785 }
786
805de8f4 787 atomic_or(ZFCP_STATUS_COMMON_OPEN, &adapter->status);
cf13c082
SS
788
789 return ZFCP_ERP_SUCCEEDED;
790}
287ac01a 791
cf13c082
SS
792static int zfcp_erp_adapter_strategy(struct zfcp_erp_action *act)
793{
794 struct zfcp_adapter *adapter = act->adapter;
795
796 if (atomic_read(&adapter->status) & ZFCP_STATUS_COMMON_OPEN) {
797 zfcp_erp_adapter_strategy_close(act);
798 if (act->status & ZFCP_STATUS_ERP_CLOSE_ONLY)
799 return ZFCP_ERP_EXIT;
800 }
801
802 if (zfcp_erp_adapter_strategy_open(act)) {
287ac01a 803 ssleep(8);
cf13c082
SS
804 return ZFCP_ERP_FAILED;
805 }
1da177e4 806
cf13c082 807 return ZFCP_ERP_SUCCEEDED;
1da177e4
LT
808}
809
287ac01a 810static int zfcp_erp_port_forced_strategy_close(struct zfcp_erp_action *act)
1da177e4 811{
287ac01a
CS
812 int retval;
813
814 retval = zfcp_fsf_close_physical_port(act);
815 if (retval == -ENOMEM)
816 return ZFCP_ERP_NOMEM;
817 act->step = ZFCP_ERP_STEP_PHYS_PORT_CLOSING;
818 if (retval)
819 return ZFCP_ERP_FAILED;
820
821 return ZFCP_ERP_CONTINUES;
1da177e4
LT
822}
823
287ac01a
CS
824static int zfcp_erp_port_forced_strategy(struct zfcp_erp_action *erp_action)
825{
826 struct zfcp_port *port = erp_action->port;
827 int status = atomic_read(&port->status);
828
829 switch (erp_action->step) {
830 case ZFCP_ERP_STEP_UNINITIALIZED:
287ac01a
CS
831 if ((status & ZFCP_STATUS_PORT_PHYS_OPEN) &&
832 (status & ZFCP_STATUS_COMMON_OPEN))
833 return zfcp_erp_port_forced_strategy_close(erp_action);
834 else
835 return ZFCP_ERP_FAILED;
836
837 case ZFCP_ERP_STEP_PHYS_PORT_CLOSING:
ddb3e0c1 838 if (!(status & ZFCP_STATUS_PORT_PHYS_OPEN))
287ac01a
CS
839 return ZFCP_ERP_SUCCEEDED;
840 }
841 return ZFCP_ERP_FAILED;
1da177e4
LT
842}
843
287ac01a 844static int zfcp_erp_port_strategy_close(struct zfcp_erp_action *erp_action)
1da177e4 845{
287ac01a 846 int retval;
1da177e4 847
287ac01a
CS
848 retval = zfcp_fsf_close_port(erp_action);
849 if (retval == -ENOMEM)
850 return ZFCP_ERP_NOMEM;
851 erp_action->step = ZFCP_ERP_STEP_PORT_CLOSING;
852 if (retval)
853 return ZFCP_ERP_FAILED;
854 return ZFCP_ERP_CONTINUES;
1da177e4
LT
855}
856
287ac01a 857static int zfcp_erp_port_strategy_open_port(struct zfcp_erp_action *erp_action)
1da177e4 858{
287ac01a 859 int retval;
1da177e4 860
287ac01a
CS
861 retval = zfcp_fsf_open_port(erp_action);
862 if (retval == -ENOMEM)
863 return ZFCP_ERP_NOMEM;
864 erp_action->step = ZFCP_ERP_STEP_PORT_OPENING;
865 if (retval)
866 return ZFCP_ERP_FAILED;
867 return ZFCP_ERP_CONTINUES;
868}
1da177e4 869
287ac01a 870static int zfcp_erp_open_ptp_port(struct zfcp_erp_action *act)
1da177e4 871{
287ac01a
CS
872 struct zfcp_adapter *adapter = act->adapter;
873 struct zfcp_port *port = act->port;
1da177e4 874
287ac01a 875 if (port->wwpn != adapter->peer_wwpn) {
edaed859 876 zfcp_erp_set_port_status(port, ZFCP_STATUS_COMMON_ERP_FAILED);
287ac01a
CS
877 return ZFCP_ERP_FAILED;
878 }
879 port->d_id = adapter->peer_d_id;
287ac01a
CS
880 return zfcp_erp_port_strategy_open_port(act);
881}
882
883static int zfcp_erp_port_strategy_open_common(struct zfcp_erp_action *act)
884{
885 struct zfcp_adapter *adapter = act->adapter;
886 struct zfcp_port *port = act->port;
287ac01a
CS
887 int p_status = atomic_read(&port->status);
888
889 switch (act->step) {
890 case ZFCP_ERP_STEP_UNINITIALIZED:
891 case ZFCP_ERP_STEP_PHYS_PORT_CLOSING:
892 case ZFCP_ERP_STEP_PORT_CLOSING:
893 if (fc_host_port_type(adapter->scsi_host) == FC_PORTTYPE_PTP)
894 return zfcp_erp_open_ptp_port(act);
b98478d7 895 if (!port->d_id) {
934aeb58 896 zfcp_fc_trigger_did_lookup(port);
799b76d0 897 return ZFCP_ERP_EXIT;
287ac01a 898 }
287ac01a
CS
899 return zfcp_erp_port_strategy_open_port(act);
900
901 case ZFCP_ERP_STEP_PORT_OPENING:
902 /* D_ID might have changed during open */
5ab944f9 903 if (p_status & ZFCP_STATUS_COMMON_OPEN) {
934aeb58
CS
904 if (!port->d_id) {
905 zfcp_fc_trigger_did_lookup(port);
906 return ZFCP_ERP_EXIT;
5ab944f9 907 }
934aeb58 908 return ZFCP_ERP_SUCCEEDED;
5ab944f9 909 }
ea460a81
SS
910 if (port->d_id && !(p_status & ZFCP_STATUS_COMMON_NOESC)) {
911 port->d_id = 0;
f7bd7c36 912 return ZFCP_ERP_FAILED;
ea460a81
SS
913 }
914 /* fall through otherwise */
287ac01a
CS
915 }
916 return ZFCP_ERP_FAILED;
917}
918
287ac01a
CS
919static int zfcp_erp_port_strategy(struct zfcp_erp_action *erp_action)
920{
921 struct zfcp_port *port = erp_action->port;
934aeb58 922 int p_status = atomic_read(&port->status);
287ac01a 923
934aeb58
CS
924 if ((p_status & ZFCP_STATUS_COMMON_NOESC) &&
925 !(p_status & ZFCP_STATUS_COMMON_OPEN))
5ab944f9
SS
926 goto close_init_done;
927
287ac01a
CS
928 switch (erp_action->step) {
929 case ZFCP_ERP_STEP_UNINITIALIZED:
934aeb58 930 if (p_status & ZFCP_STATUS_COMMON_OPEN)
287ac01a 931 return zfcp_erp_port_strategy_close(erp_action);
1da177e4
LT
932 break;
933
287ac01a 934 case ZFCP_ERP_STEP_PORT_CLOSING:
934aeb58 935 if (p_status & ZFCP_STATUS_COMMON_OPEN)
287ac01a 936 return ZFCP_ERP_FAILED;
1da177e4
LT
937 break;
938 }
5ab944f9
SS
939
940close_init_done:
287ac01a
CS
941 if (erp_action->status & ZFCP_STATUS_ERP_CLOSE_ONLY)
942 return ZFCP_ERP_EXIT;
1da177e4 943
5ab944f9 944 return zfcp_erp_port_strategy_open_common(erp_action);
287ac01a
CS
945}
946
b62a8d9b 947static void zfcp_erp_lun_strategy_clearstati(struct scsi_device *sdev)
287ac01a 948{
b62a8d9b
CS
949 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
950
805de8f4 951 atomic_andnot(ZFCP_STATUS_COMMON_ACCESS_DENIED,
b62a8d9b 952 &zfcp_sdev->status);
287ac01a
CS
953}
954
b62a8d9b 955static int zfcp_erp_lun_strategy_close(struct zfcp_erp_action *erp_action)
287ac01a 956{
b62a8d9b 957 int retval = zfcp_fsf_close_lun(erp_action);
287ac01a
CS
958 if (retval == -ENOMEM)
959 return ZFCP_ERP_NOMEM;
b62a8d9b 960 erp_action->step = ZFCP_ERP_STEP_LUN_CLOSING;
287ac01a
CS
961 if (retval)
962 return ZFCP_ERP_FAILED;
963 return ZFCP_ERP_CONTINUES;
964}
965
b62a8d9b 966static int zfcp_erp_lun_strategy_open(struct zfcp_erp_action *erp_action)
287ac01a 967{
b62a8d9b 968 int retval = zfcp_fsf_open_lun(erp_action);
287ac01a
CS
969 if (retval == -ENOMEM)
970 return ZFCP_ERP_NOMEM;
b62a8d9b 971 erp_action->step = ZFCP_ERP_STEP_LUN_OPENING;
287ac01a
CS
972 if (retval)
973 return ZFCP_ERP_FAILED;
974 return ZFCP_ERP_CONTINUES;
1da177e4
LT
975}
976
b62a8d9b 977static int zfcp_erp_lun_strategy(struct zfcp_erp_action *erp_action)
1da177e4 978{
b62a8d9b
CS
979 struct scsi_device *sdev = erp_action->sdev;
980 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
287ac01a
CS
981
982 switch (erp_action->step) {
983 case ZFCP_ERP_STEP_UNINITIALIZED:
b62a8d9b
CS
984 zfcp_erp_lun_strategy_clearstati(sdev);
985 if (atomic_read(&zfcp_sdev->status) & ZFCP_STATUS_COMMON_OPEN)
986 return zfcp_erp_lun_strategy_close(erp_action);
287ac01a 987 /* already closed, fall through */
b62a8d9b
CS
988 case ZFCP_ERP_STEP_LUN_CLOSING:
989 if (atomic_read(&zfcp_sdev->status) & ZFCP_STATUS_COMMON_OPEN)
287ac01a
CS
990 return ZFCP_ERP_FAILED;
991 if (erp_action->status & ZFCP_STATUS_ERP_CLOSE_ONLY)
992 return ZFCP_ERP_EXIT;
b62a8d9b 993 return zfcp_erp_lun_strategy_open(erp_action);
287ac01a 994
b62a8d9b
CS
995 case ZFCP_ERP_STEP_LUN_OPENING:
996 if (atomic_read(&zfcp_sdev->status) & ZFCP_STATUS_COMMON_OPEN)
287ac01a
CS
997 return ZFCP_ERP_SUCCEEDED;
998 }
999 return ZFCP_ERP_FAILED;
1da177e4
LT
1000}
1001
b62a8d9b 1002static int zfcp_erp_strategy_check_lun(struct scsi_device *sdev, int result)
1da177e4 1003{
b62a8d9b
CS
1004 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
1005
1da177e4
LT
1006 switch (result) {
1007 case ZFCP_ERP_SUCCEEDED :
b62a8d9b
CS
1008 atomic_set(&zfcp_sdev->erp_counter, 0);
1009 zfcp_erp_lun_unblock(sdev);
1da177e4
LT
1010 break;
1011 case ZFCP_ERP_FAILED :
b62a8d9b
CS
1012 atomic_inc(&zfcp_sdev->erp_counter);
1013 if (atomic_read(&zfcp_sdev->erp_counter) > ZFCP_MAX_ERPS) {
1014 dev_err(&zfcp_sdev->port->adapter->ccw_device->dev,
1015 "ERP failed for LUN 0x%016Lx on "
ff3b24fa 1016 "port 0x%016Lx\n",
b62a8d9b
CS
1017 (unsigned long long)zfcp_scsi_dev_lun(sdev),
1018 (unsigned long long)zfcp_sdev->port->wwpn);
edaed859
SS
1019 zfcp_erp_set_lun_status(sdev,
1020 ZFCP_STATUS_COMMON_ERP_FAILED);
ff3b24fa 1021 }
1da177e4 1022 break;
1da177e4
LT
1023 }
1024
b62a8d9b
CS
1025 if (atomic_read(&zfcp_sdev->status) & ZFCP_STATUS_COMMON_ERP_FAILED) {
1026 zfcp_erp_lun_block(sdev, 0);
1da177e4
LT
1027 result = ZFCP_ERP_EXIT;
1028 }
1da177e4
LT
1029 return result;
1030}
1031
287ac01a 1032static int zfcp_erp_strategy_check_port(struct zfcp_port *port, int result)
1da177e4 1033{
1da177e4
LT
1034 switch (result) {
1035 case ZFCP_ERP_SUCCEEDED :
1036 atomic_set(&port->erp_counter, 0);
1037 zfcp_erp_port_unblock(port);
1038 break;
287ac01a 1039
1da177e4 1040 case ZFCP_ERP_FAILED :
287ac01a 1041 if (atomic_read(&port->status) & ZFCP_STATUS_COMMON_NOESC) {
cc8c2829
SS
1042 zfcp_erp_port_block(port, 0);
1043 result = ZFCP_ERP_EXIT;
1044 }
1da177e4 1045 atomic_inc(&port->erp_counter);
ff3b24fa
CS
1046 if (atomic_read(&port->erp_counter) > ZFCP_MAX_ERPS) {
1047 dev_err(&port->adapter->ccw_device->dev,
1048 "ERP failed for remote port 0x%016Lx\n",
7ba58c9c 1049 (unsigned long long)port->wwpn);
edaed859
SS
1050 zfcp_erp_set_port_status(port,
1051 ZFCP_STATUS_COMMON_ERP_FAILED);
ff3b24fa 1052 }
1da177e4 1053 break;
1da177e4
LT
1054 }
1055
287ac01a
CS
1056 if (atomic_read(&port->status) & ZFCP_STATUS_COMMON_ERP_FAILED) {
1057 zfcp_erp_port_block(port, 0);
1da177e4
LT
1058 result = ZFCP_ERP_EXIT;
1059 }
1da177e4
LT
1060 return result;
1061}
1062
287ac01a
CS
1063static int zfcp_erp_strategy_check_adapter(struct zfcp_adapter *adapter,
1064 int result)
1da177e4 1065{
1da177e4
LT
1066 switch (result) {
1067 case ZFCP_ERP_SUCCEEDED :
1068 atomic_set(&adapter->erp_counter, 0);
1069 zfcp_erp_adapter_unblock(adapter);
1070 break;
287ac01a 1071
1da177e4
LT
1072 case ZFCP_ERP_FAILED :
1073 atomic_inc(&adapter->erp_counter);
ff3b24fa
CS
1074 if (atomic_read(&adapter->erp_counter) > ZFCP_MAX_ERPS) {
1075 dev_err(&adapter->ccw_device->dev,
1076 "ERP cannot recover an error "
1077 "on the FCP device\n");
edaed859
SS
1078 zfcp_erp_set_adapter_status(adapter,
1079 ZFCP_STATUS_COMMON_ERP_FAILED);
ff3b24fa 1080 }
1da177e4 1081 break;
1da177e4
LT
1082 }
1083
287ac01a
CS
1084 if (atomic_read(&adapter->status) & ZFCP_STATUS_COMMON_ERP_FAILED) {
1085 zfcp_erp_adapter_block(adapter, 0);
1da177e4
LT
1086 result = ZFCP_ERP_EXIT;
1087 }
1da177e4
LT
1088 return result;
1089}
1090
287ac01a
CS
1091static int zfcp_erp_strategy_check_target(struct zfcp_erp_action *erp_action,
1092 int result)
5f852be9 1093{
287ac01a
CS
1094 struct zfcp_adapter *adapter = erp_action->adapter;
1095 struct zfcp_port *port = erp_action->port;
b62a8d9b 1096 struct scsi_device *sdev = erp_action->sdev;
287ac01a
CS
1097
1098 switch (erp_action->action) {
1099
b62a8d9b
CS
1100 case ZFCP_ERP_ACTION_REOPEN_LUN:
1101 result = zfcp_erp_strategy_check_lun(sdev, result);
287ac01a
CS
1102 break;
1103
1104 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
1105 case ZFCP_ERP_ACTION_REOPEN_PORT:
1106 result = zfcp_erp_strategy_check_port(port, result);
1107 break;
1108
1109 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
1110 result = zfcp_erp_strategy_check_adapter(adapter, result);
1111 break;
1112 }
1113 return result;
5f852be9
CS
1114}
1115
287ac01a 1116static int zfcp_erp_strat_change_det(atomic_t *target_status, u32 erp_status)
5f852be9 1117{
287ac01a 1118 int status = atomic_read(target_status);
5f852be9 1119
287ac01a
CS
1120 if ((status & ZFCP_STATUS_COMMON_RUNNING) &&
1121 (erp_status & ZFCP_STATUS_ERP_CLOSE_ONLY))
1122 return 1; /* take it online */
5f852be9 1123
287ac01a
CS
1124 if (!(status & ZFCP_STATUS_COMMON_RUNNING) &&
1125 !(erp_status & ZFCP_STATUS_ERP_CLOSE_ONLY))
1126 return 1; /* take it offline */
1127
1128 return 0;
5f852be9
CS
1129}
1130
287ac01a 1131static int zfcp_erp_strategy_statechange(struct zfcp_erp_action *act, int ret)
1da177e4 1132{
287ac01a
CS
1133 int action = act->action;
1134 struct zfcp_adapter *adapter = act->adapter;
1135 struct zfcp_port *port = act->port;
b62a8d9b
CS
1136 struct scsi_device *sdev = act->sdev;
1137 struct zfcp_scsi_dev *zfcp_sdev;
287ac01a 1138 u32 erp_status = act->status;
1da177e4 1139
287ac01a 1140 switch (action) {
1da177e4 1141 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
287ac01a
CS
1142 if (zfcp_erp_strat_change_det(&adapter->status, erp_status)) {
1143 _zfcp_erp_adapter_reopen(adapter,
1144 ZFCP_STATUS_COMMON_ERP_FAILED,
ea4a3a6a 1145 "ersscg1");
287ac01a
CS
1146 return ZFCP_ERP_EXIT;
1147 }
1da177e4
LT
1148 break;
1149
1150 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
1da177e4 1151 case ZFCP_ERP_ACTION_REOPEN_PORT:
287ac01a
CS
1152 if (zfcp_erp_strat_change_det(&port->status, erp_status)) {
1153 _zfcp_erp_port_reopen(port,
1154 ZFCP_STATUS_COMMON_ERP_FAILED,
ea4a3a6a 1155 "ersscg2");
287ac01a
CS
1156 return ZFCP_ERP_EXIT;
1157 }
1da177e4
LT
1158 break;
1159
b62a8d9b
CS
1160 case ZFCP_ERP_ACTION_REOPEN_LUN:
1161 zfcp_sdev = sdev_to_zfcp(sdev);
1162 if (zfcp_erp_strat_change_det(&zfcp_sdev->status, erp_status)) {
1163 _zfcp_erp_lun_reopen(sdev,
1164 ZFCP_STATUS_COMMON_ERP_FAILED,
ea4a3a6a 1165 "ersscg3", 0);
287ac01a
CS
1166 return ZFCP_ERP_EXIT;
1167 }
1da177e4
LT
1168 break;
1169 }
287ac01a 1170 return ret;
1da177e4
LT
1171}
1172
287ac01a 1173static void zfcp_erp_action_dequeue(struct zfcp_erp_action *erp_action)
1da177e4 1174{
287ac01a 1175 struct zfcp_adapter *adapter = erp_action->adapter;
b62a8d9b 1176 struct zfcp_scsi_dev *zfcp_sdev;
1da177e4 1177
287ac01a
CS
1178 adapter->erp_total_count--;
1179 if (erp_action->status & ZFCP_STATUS_ERP_LOWMEM) {
1180 adapter->erp_low_mem_count--;
1181 erp_action->status &= ~ZFCP_STATUS_ERP_LOWMEM;
507e4969 1182 }
1da177e4 1183
287ac01a 1184 list_del(&erp_action->list);
ae0904f6 1185 zfcp_dbf_rec_run("eractd1", erp_action);
1da177e4 1186
287ac01a 1187 switch (erp_action->action) {
b62a8d9b
CS
1188 case ZFCP_ERP_ACTION_REOPEN_LUN:
1189 zfcp_sdev = sdev_to_zfcp(erp_action->sdev);
805de8f4 1190 atomic_andnot(ZFCP_STATUS_COMMON_ERP_INUSE,
b62a8d9b 1191 &zfcp_sdev->status);
287ac01a 1192 break;
1da177e4 1193
287ac01a
CS
1194 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
1195 case ZFCP_ERP_ACTION_REOPEN_PORT:
805de8f4 1196 atomic_andnot(ZFCP_STATUS_COMMON_ERP_INUSE,
287ac01a
CS
1197 &erp_action->port->status);
1198 break;
1da177e4 1199
287ac01a 1200 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
805de8f4 1201 atomic_andnot(ZFCP_STATUS_COMMON_ERP_INUSE,
287ac01a
CS
1202 &erp_action->adapter->status);
1203 break;
1204 }
1da177e4
LT
1205}
1206
6f2ce1c6
SM
1207/**
1208 * zfcp_erp_try_rport_unblock - unblock rport if no more/new recovery
1209 * @port: zfcp_port whose fc_rport we should try to unblock
1210 */
1211static void zfcp_erp_try_rport_unblock(struct zfcp_port *port)
1212{
1213 unsigned long flags;
1214 struct zfcp_adapter *adapter = port->adapter;
1215 int port_status;
1216 struct Scsi_Host *shost = adapter->scsi_host;
1217 struct scsi_device *sdev;
1218
1219 write_lock_irqsave(&adapter->erp_lock, flags);
1220 port_status = atomic_read(&port->status);
1221 if ((port_status & ZFCP_STATUS_COMMON_UNBLOCKED) == 0 ||
1222 (port_status & (ZFCP_STATUS_COMMON_ERP_INUSE |
1223 ZFCP_STATUS_COMMON_ERP_FAILED)) != 0) {
1224 /* new ERP of severity >= port triggered elsewhere meanwhile or
1225 * local link down (adapter erp_failed but not clear unblock)
1226 */
1227 zfcp_dbf_rec_run_lvl(4, "ertru_p", &port->erp_action);
1228 write_unlock_irqrestore(&adapter->erp_lock, flags);
1229 return;
1230 }
1231 spin_lock(shost->host_lock);
1232 __shost_for_each_device(sdev, shost) {
1233 struct zfcp_scsi_dev *zsdev = sdev_to_zfcp(sdev);
1234 int lun_status;
1235
1236 if (zsdev->port != port)
1237 continue;
1238 /* LUN under port of interest */
1239 lun_status = atomic_read(&zsdev->status);
1240 if ((lun_status & ZFCP_STATUS_COMMON_ERP_FAILED) != 0)
1241 continue; /* unblock rport despite failed LUNs */
1242 /* LUN recovery not given up yet [maybe follow-up pending] */
1243 if ((lun_status & ZFCP_STATUS_COMMON_UNBLOCKED) == 0 ||
1244 (lun_status & ZFCP_STATUS_COMMON_ERP_INUSE) != 0) {
1245 /* LUN blocked:
1246 * not yet unblocked [LUN recovery pending]
1247 * or meanwhile blocked [new LUN recovery triggered]
1248 */
1249 zfcp_dbf_rec_run_lvl(4, "ertru_l", &zsdev->erp_action);
1250 spin_unlock(shost->host_lock);
1251 write_unlock_irqrestore(&adapter->erp_lock, flags);
1252 return;
1253 }
1254 }
1255 /* now port has no child or all children have completed recovery,
1256 * and no ERP of severity >= port was meanwhile triggered elsewhere
1257 */
1258 zfcp_scsi_schedule_rport_register(port);
1259 spin_unlock(shost->host_lock);
1260 write_unlock_irqrestore(&adapter->erp_lock, flags);
1261}
1262
287ac01a 1263static void zfcp_erp_action_cleanup(struct zfcp_erp_action *act, int result)
1da177e4 1264{
287ac01a
CS
1265 struct zfcp_adapter *adapter = act->adapter;
1266 struct zfcp_port *port = act->port;
b62a8d9b 1267 struct scsi_device *sdev = act->sdev;
1da177e4 1268
287ac01a 1269 switch (act->action) {
b62a8d9b 1270 case ZFCP_ERP_ACTION_REOPEN_LUN:
fdbd1c5e 1271 if (!(act->status & ZFCP_STATUS_ERP_NO_REF))
b62a8d9b 1272 scsi_device_put(sdev);
6f2ce1c6 1273 zfcp_erp_try_rport_unblock(port);
287ac01a 1274 break;
1da177e4 1275
287ac01a 1276 case ZFCP_ERP_ACTION_REOPEN_PORT:
4eeaa4f3
SM
1277 /* This switch case might also happen after a forced reopen
1278 * was successfully done and thus overwritten with a new
1279 * non-forced reopen at `ersfs_2'. In this case, we must not
1280 * do the clean-up of the non-forced version.
1281 */
1282 if (act->step != ZFCP_ERP_STEP_UNINITIALIZED)
1283 if (result == ZFCP_ERP_SUCCEEDED)
6f2ce1c6 1284 zfcp_erp_try_rport_unblock(port);
5767620c
CS
1285 /* fall through */
1286 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
615f59e0 1287 put_device(&port->dev);
287ac01a
CS
1288 break;
1289
1290 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
a2fa0aed 1291 if (result == ZFCP_ERP_SUCCEEDED) {
bd43a42b 1292 register_service_level(&adapter->service_level);
43f60cbd 1293 zfcp_fc_conditional_port_scan(adapter);
038d9446 1294 queue_work(adapter->work_queue, &adapter->ns_up_work);
a2fa0aed
CS
1295 } else
1296 unregister_service_level(&adapter->service_level);
038d9446 1297
f3450c7b 1298 kref_put(&adapter->ref, zfcp_adapter_release);
287ac01a
CS
1299 break;
1300 }
1da177e4
LT
1301}
1302
287ac01a 1303static int zfcp_erp_strategy_do_action(struct zfcp_erp_action *erp_action)
1da177e4 1304{
287ac01a
CS
1305 switch (erp_action->action) {
1306 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
1307 return zfcp_erp_adapter_strategy(erp_action);
1308 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
1309 return zfcp_erp_port_forced_strategy(erp_action);
1310 case ZFCP_ERP_ACTION_REOPEN_PORT:
1311 return zfcp_erp_port_strategy(erp_action);
b62a8d9b
CS
1312 case ZFCP_ERP_ACTION_REOPEN_LUN:
1313 return zfcp_erp_lun_strategy(erp_action);
287ac01a
CS
1314 }
1315 return ZFCP_ERP_FAILED;
1da177e4
LT
1316}
1317
287ac01a 1318static int zfcp_erp_strategy(struct zfcp_erp_action *erp_action)
1da177e4
LT
1319{
1320 int retval;
287ac01a 1321 unsigned long flags;
ecf0c772 1322 struct zfcp_adapter *adapter = erp_action->adapter;
1da177e4 1323
f3450c7b 1324 kref_get(&adapter->ref);
1da177e4 1325
f3450c7b 1326 write_lock_irqsave(&adapter->erp_lock, flags);
287ac01a 1327 zfcp_erp_strategy_check_fsfreq(erp_action);
1da177e4 1328
287ac01a
CS
1329 if (erp_action->status & ZFCP_STATUS_ERP_DISMISSED) {
1330 zfcp_erp_action_dequeue(erp_action);
1331 retval = ZFCP_ERP_DISMISSED;
1332 goto unlock;
22753fa5 1333 }
1da177e4 1334
9c785d94
CS
1335 if (erp_action->status & ZFCP_STATUS_ERP_TIMEDOUT) {
1336 retval = ZFCP_ERP_FAILED;
1337 goto check_target;
1338 }
1339
2f8f3ed5 1340 zfcp_erp_action_to_running(erp_action);
1da177e4 1341
287ac01a 1342 /* no lock to allow for blocking operations */
ecf0c772 1343 write_unlock_irqrestore(&adapter->erp_lock, flags);
287ac01a 1344 retval = zfcp_erp_strategy_do_action(erp_action);
ecf0c772 1345 write_lock_irqsave(&adapter->erp_lock, flags);
1da177e4 1346
287ac01a
CS
1347 if (erp_action->status & ZFCP_STATUS_ERP_DISMISSED)
1348 retval = ZFCP_ERP_CONTINUES;
cc8c2829 1349
287ac01a
CS
1350 switch (retval) {
1351 case ZFCP_ERP_NOMEM:
1352 if (!(erp_action->status & ZFCP_STATUS_ERP_LOWMEM)) {
1353 ++adapter->erp_low_mem_count;
1354 erp_action->status |= ZFCP_STATUS_ERP_LOWMEM;
1da177e4 1355 }
287ac01a 1356 if (adapter->erp_total_count == adapter->erp_low_mem_count)
ea4a3a6a 1357 _zfcp_erp_adapter_reopen(adapter, 0, "erstgy1");
287ac01a
CS
1358 else {
1359 zfcp_erp_strategy_memwait(erp_action);
1360 retval = ZFCP_ERP_CONTINUES;
1da177e4 1361 }
287ac01a 1362 goto unlock;
1da177e4 1363
287ac01a
CS
1364 case ZFCP_ERP_CONTINUES:
1365 if (erp_action->status & ZFCP_STATUS_ERP_LOWMEM) {
1366 --adapter->erp_low_mem_count;
1367 erp_action->status &= ~ZFCP_STATUS_ERP_LOWMEM;
1da177e4 1368 }
287ac01a 1369 goto unlock;
1da177e4
LT
1370 }
1371
9c785d94 1372check_target:
287ac01a
CS
1373 retval = zfcp_erp_strategy_check_target(erp_action, retval);
1374 zfcp_erp_action_dequeue(erp_action);
1375 retval = zfcp_erp_strategy_statechange(erp_action, retval);
1376 if (retval == ZFCP_ERP_EXIT)
1377 goto unlock;
85600f7f
CS
1378 if (retval == ZFCP_ERP_SUCCEEDED)
1379 zfcp_erp_strategy_followup_success(erp_action);
1380 if (retval == ZFCP_ERP_FAILED)
1381 zfcp_erp_strategy_followup_failed(erp_action);
1da177e4 1382
287ac01a 1383 unlock:
ecf0c772 1384 write_unlock_irqrestore(&adapter->erp_lock, flags);
1da177e4 1385
287ac01a
CS
1386 if (retval != ZFCP_ERP_CONTINUES)
1387 zfcp_erp_action_cleanup(erp_action, retval);
1da177e4 1388
f3450c7b 1389 kref_put(&adapter->ref, zfcp_adapter_release);
1da177e4
LT
1390 return retval;
1391}
1392
287ac01a 1393static int zfcp_erp_thread(void *data)
1da177e4 1394{
287ac01a
CS
1395 struct zfcp_adapter *adapter = (struct zfcp_adapter *) data;
1396 struct list_head *next;
1397 struct zfcp_erp_action *act;
1398 unsigned long flags;
94ab4b38 1399
347c6a96 1400 for (;;) {
347c6a96
CS
1401 wait_event_interruptible(adapter->erp_ready_wq,
1402 !list_empty(&adapter->erp_ready_head) ||
1403 kthread_should_stop());
94ab4b38 1404
347c6a96
CS
1405 if (kthread_should_stop())
1406 break;
1407
287ac01a
CS
1408 write_lock_irqsave(&adapter->erp_lock, flags);
1409 next = adapter->erp_ready_head.next;
1410 write_unlock_irqrestore(&adapter->erp_lock, flags);
1da177e4 1411
287ac01a
CS
1412 if (next != &adapter->erp_ready_head) {
1413 act = list_entry(next, struct zfcp_erp_action, list);
1da177e4 1414
287ac01a
CS
1415 /* there is more to come after dismission, no notify */
1416 if (zfcp_erp_strategy(act) != ZFCP_ERP_DISMISSED)
1417 zfcp_erp_wakeup(adapter);
1da177e4 1418 }
1da177e4
LT
1419 }
1420
287ac01a
CS
1421 return 0;
1422}
1da177e4 1423
287ac01a
CS
1424/**
1425 * zfcp_erp_thread_setup - Start ERP thread for adapter
1426 * @adapter: Adapter to start the ERP thread for
1427 *
1428 * Returns 0 on success or error code from kernel_thread()
1429 */
1430int zfcp_erp_thread_setup(struct zfcp_adapter *adapter)
1431{
347c6a96 1432 struct task_struct *thread;
1da177e4 1433
347c6a96
CS
1434 thread = kthread_run(zfcp_erp_thread, adapter, "zfcperp%s",
1435 dev_name(&adapter->ccw_device->dev));
1436 if (IS_ERR(thread)) {
287ac01a 1437 dev_err(&adapter->ccw_device->dev,
ff3b24fa 1438 "Creating an ERP thread for the FCP device failed.\n");
347c6a96 1439 return PTR_ERR(thread);
1da177e4 1440 }
347c6a96
CS
1441
1442 adapter->erp_thread = thread;
287ac01a
CS
1443 return 0;
1444}
1da177e4 1445
287ac01a
CS
1446/**
1447 * zfcp_erp_thread_kill - Stop ERP thread.
1448 * @adapter: Adapter where the ERP thread should be stopped.
1449 *
1450 * The caller of this routine ensures that the specified adapter has
1451 * been shut down and that this operation has been completed. Thus,
1452 * there are no pending erp_actions which would need to be handled
1453 * here.
1454 */
1455void zfcp_erp_thread_kill(struct zfcp_adapter *adapter)
1456{
347c6a96
CS
1457 kthread_stop(adapter->erp_thread);
1458 adapter->erp_thread = NULL;
143bb6bf
CS
1459 WARN_ON(!list_empty(&adapter->erp_ready_head));
1460 WARN_ON(!list_empty(&adapter->erp_running_head));
1da177e4
LT
1461}
1462
287ac01a 1463/**
edaed859
SS
1464 * zfcp_erp_wait - wait for completion of error recovery on an adapter
1465 * @adapter: adapter for which to wait for completion of its error recovery
287ac01a 1466 */
edaed859 1467void zfcp_erp_wait(struct zfcp_adapter *adapter)
1da177e4 1468{
edaed859
SS
1469 wait_event(adapter->erp_done_wqh,
1470 !(atomic_read(&adapter->status) &
1471 ZFCP_STATUS_ADAPTER_ERP_PENDING));
287ac01a 1472}
1da177e4 1473
287ac01a 1474/**
edaed859
SS
1475 * zfcp_erp_set_adapter_status - set adapter status bits
1476 * @adapter: adapter to change the status
1477 * @mask: status bits to change
1478 *
1479 * Changes in common status bits are propagated to attached ports and LUNs.
287ac01a 1480 */
edaed859 1481void zfcp_erp_set_adapter_status(struct zfcp_adapter *adapter, u32 mask)
287ac01a 1482{
edaed859
SS
1483 struct zfcp_port *port;
1484 struct scsi_device *sdev;
1485 unsigned long flags;
1486 u32 common_mask = mask & ZFCP_COMMON_FLAGS;
1da177e4 1487
805de8f4 1488 atomic_or(mask, &adapter->status);
1da177e4 1489
edaed859
SS
1490 if (!common_mask)
1491 return;
1492
1493 read_lock_irqsave(&adapter->port_list_lock, flags);
1494 list_for_each_entry(port, &adapter->port_list, list)
805de8f4 1495 atomic_or(common_mask, &port->status);
edaed859
SS
1496 read_unlock_irqrestore(&adapter->port_list_lock, flags);
1497
924dd584
MP
1498 spin_lock_irqsave(adapter->scsi_host->host_lock, flags);
1499 __shost_for_each_device(sdev, adapter->scsi_host)
805de8f4 1500 atomic_or(common_mask, &sdev_to_zfcp(sdev)->status);
924dd584 1501 spin_unlock_irqrestore(adapter->scsi_host->host_lock, flags);
287ac01a 1502}
1da177e4 1503
287ac01a 1504/**
edaed859 1505 * zfcp_erp_clear_adapter_status - clear adapter status bits
287ac01a 1506 * @adapter: adapter to change the status
287ac01a 1507 * @mask: status bits to change
287ac01a 1508 *
b62a8d9b 1509 * Changes in common status bits are propagated to attached ports and LUNs.
287ac01a 1510 */
edaed859 1511void zfcp_erp_clear_adapter_status(struct zfcp_adapter *adapter, u32 mask)
1da177e4 1512{
1da177e4 1513 struct zfcp_port *port;
edaed859 1514 struct scsi_device *sdev;
ecf0c772 1515 unsigned long flags;
287ac01a 1516 u32 common_mask = mask & ZFCP_COMMON_FLAGS;
edaed859 1517 u32 clear_counter = mask & ZFCP_STATUS_COMMON_ERP_FAILED;
1da177e4 1518
805de8f4 1519 atomic_andnot(mask, &adapter->status);
edaed859
SS
1520
1521 if (!common_mask)
1522 return;
1523
1524 if (clear_counter)
1525 atomic_set(&adapter->erp_counter, 0);
1526
1527 read_lock_irqsave(&adapter->port_list_lock, flags);
1528 list_for_each_entry(port, &adapter->port_list, list) {
805de8f4 1529 atomic_andnot(common_mask, &port->status);
edaed859
SS
1530 if (clear_counter)
1531 atomic_set(&port->erp_counter, 0);
287ac01a 1532 }
edaed859 1533 read_unlock_irqrestore(&adapter->port_list_lock, flags);
287ac01a 1534
924dd584
MP
1535 spin_lock_irqsave(adapter->scsi_host->host_lock, flags);
1536 __shost_for_each_device(sdev, adapter->scsi_host) {
805de8f4 1537 atomic_andnot(common_mask, &sdev_to_zfcp(sdev)->status);
edaed859
SS
1538 if (clear_counter)
1539 atomic_set(&sdev_to_zfcp(sdev)->erp_counter, 0);
ecf0c772 1540 }
924dd584 1541 spin_unlock_irqrestore(adapter->scsi_host->host_lock, flags);
1da177e4
LT
1542}
1543
287ac01a 1544/**
edaed859
SS
1545 * zfcp_erp_set_port_status - set port status bits
1546 * @port: port to change the status
287ac01a 1547 * @mask: status bits to change
287ac01a 1548 *
b62a8d9b 1549 * Changes in common status bits are propagated to attached LUNs.
287ac01a 1550 */
edaed859 1551void zfcp_erp_set_port_status(struct zfcp_port *port, u32 mask)
1da177e4 1552{
b62a8d9b 1553 struct scsi_device *sdev;
287ac01a 1554 u32 common_mask = mask & ZFCP_COMMON_FLAGS;
924dd584 1555 unsigned long flags;
1da177e4 1556
805de8f4 1557 atomic_or(mask, &port->status);
1da177e4 1558
edaed859
SS
1559 if (!common_mask)
1560 return;
1561
924dd584
MP
1562 spin_lock_irqsave(port->adapter->scsi_host->host_lock, flags);
1563 __shost_for_each_device(sdev, port->adapter->scsi_host)
edaed859 1564 if (sdev_to_zfcp(sdev)->port == port)
805de8f4 1565 atomic_or(common_mask,
edaed859 1566 &sdev_to_zfcp(sdev)->status);
924dd584 1567 spin_unlock_irqrestore(port->adapter->scsi_host->host_lock, flags);
1da177e4
LT
1568}
1569
287ac01a 1570/**
edaed859
SS
1571 * zfcp_erp_clear_port_status - clear port status bits
1572 * @port: adapter to change the status
287ac01a 1573 * @mask: status bits to change
edaed859
SS
1574 *
1575 * Changes in common status bits are propagated to attached LUNs.
287ac01a 1576 */
edaed859 1577void zfcp_erp_clear_port_status(struct zfcp_port *port, u32 mask)
1da177e4 1578{
edaed859
SS
1579 struct scsi_device *sdev;
1580 u32 common_mask = mask & ZFCP_COMMON_FLAGS;
1581 u32 clear_counter = mask & ZFCP_STATUS_COMMON_ERP_FAILED;
924dd584 1582 unsigned long flags;
edaed859 1583
805de8f4 1584 atomic_andnot(mask, &port->status);
edaed859
SS
1585
1586 if (!common_mask)
1587 return;
b62a8d9b 1588
edaed859
SS
1589 if (clear_counter)
1590 atomic_set(&port->erp_counter, 0);
1591
924dd584
MP
1592 spin_lock_irqsave(port->adapter->scsi_host->host_lock, flags);
1593 __shost_for_each_device(sdev, port->adapter->scsi_host)
edaed859 1594 if (sdev_to_zfcp(sdev)->port == port) {
805de8f4 1595 atomic_andnot(common_mask,
edaed859
SS
1596 &sdev_to_zfcp(sdev)->status);
1597 if (clear_counter)
1598 atomic_set(&sdev_to_zfcp(sdev)->erp_counter, 0);
287ac01a 1599 }
924dd584 1600 spin_unlock_irqrestore(port->adapter->scsi_host->host_lock, flags);
1da177e4
LT
1601}
1602
287ac01a 1603/**
edaed859
SS
1604 * zfcp_erp_set_lun_status - set lun status bits
1605 * @sdev: SCSI device / lun to set the status bits
1606 * @mask: status bits to change
287ac01a 1607 */
edaed859 1608void zfcp_erp_set_lun_status(struct scsi_device *sdev, u32 mask)
d736a27b 1609{
edaed859
SS
1610 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
1611
805de8f4 1612 atomic_or(mask, &zfcp_sdev->status);
d736a27b
AH
1613}
1614
287ac01a 1615/**
edaed859
SS
1616 * zfcp_erp_clear_lun_status - clear lun status bits
1617 * @sdev: SCSi device / lun to clear the status bits
1618 * @mask: status bits to change
287ac01a 1619 */
edaed859 1620void zfcp_erp_clear_lun_status(struct scsi_device *sdev, u32 mask)
d736a27b 1621{
edaed859
SS
1622 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
1623
805de8f4 1624 atomic_andnot(mask, &zfcp_sdev->status);
edaed859
SS
1625
1626 if (mask & ZFCP_STATUS_COMMON_ERP_FAILED)
1627 atomic_set(&zfcp_sdev->erp_counter, 0);
d736a27b 1628}
edaed859 1629