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