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