]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/s390/scsi/zfcp_erp.c
atomic: Replace atomic_{set,clear}_mask() usage
[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 *
a53c8fab 6 * Copyright IBM Corp. 2002, 2010
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{
287ac01a
CS
575 init_timer(&erp_action->timer);
576 erp_action->timer.function = zfcp_erp_memwait_handler;
577 erp_action->timer.data = (unsigned long) erp_action;
578 erp_action->timer.expires = jiffies + HZ;
579 add_timer(&erp_action->timer);
1da177e4
LT
580}
581
287ac01a 582static void _zfcp_erp_port_reopen_all(struct zfcp_adapter *adapter,
ea4a3a6a 583 int clear, char *id)
1da177e4 584{
287ac01a 585 struct zfcp_port *port;
1da177e4 586
ecf0c772
SS
587 read_lock(&adapter->port_list_lock);
588 list_for_each_entry(port, &adapter->port_list, list)
ea4a3a6a 589 _zfcp_erp_port_reopen(port, clear, id);
ecf0c772 590 read_unlock(&adapter->port_list_lock);
1da177e4
LT
591}
592
b62a8d9b 593static void _zfcp_erp_lun_reopen_all(struct zfcp_port *port, int clear,
ea4a3a6a 594 char *id)
1da177e4 595{
b62a8d9b 596 struct scsi_device *sdev;
1da177e4 597
924dd584
MP
598 spin_lock(port->adapter->scsi_host->host_lock);
599 __shost_for_each_device(sdev, port->adapter->scsi_host)
b62a8d9b 600 if (sdev_to_zfcp(sdev)->port == port)
ea4a3a6a 601 _zfcp_erp_lun_reopen(sdev, clear, id, 0);
924dd584 602 spin_unlock(port->adapter->scsi_host->host_lock);
1da177e4
LT
603}
604
85600f7f 605static void zfcp_erp_strategy_followup_failed(struct zfcp_erp_action *act)
1da177e4 606{
287ac01a 607 switch (act->action) {
287ac01a 608 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
ea4a3a6a 609 _zfcp_erp_adapter_reopen(act->adapter, 0, "ersff_1");
287ac01a 610 break;
287ac01a 611 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
ea4a3a6a 612 _zfcp_erp_port_forced_reopen(act->port, 0, "ersff_2");
287ac01a 613 break;
287ac01a 614 case ZFCP_ERP_ACTION_REOPEN_PORT:
ea4a3a6a 615 _zfcp_erp_port_reopen(act->port, 0, "ersff_3");
287ac01a 616 break;
b62a8d9b 617 case ZFCP_ERP_ACTION_REOPEN_LUN:
ea4a3a6a 618 _zfcp_erp_lun_reopen(act->sdev, 0, "ersff_4", 0);
85600f7f
CS
619 break;
620 }
621}
622
623static void zfcp_erp_strategy_followup_success(struct zfcp_erp_action *act)
624{
625 switch (act->action) {
626 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
ea4a3a6a 627 _zfcp_erp_port_reopen_all(act->adapter, 0, "ersfs_1");
85600f7f
CS
628 break;
629 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
ea4a3a6a 630 _zfcp_erp_port_reopen(act->port, 0, "ersfs_2");
85600f7f
CS
631 break;
632 case ZFCP_ERP_ACTION_REOPEN_PORT:
ea4a3a6a 633 _zfcp_erp_lun_reopen_all(act->port, 0, "ersfs_3");
287ac01a 634 break;
1da177e4 635 }
1da177e4
LT
636}
637
287ac01a 638static void zfcp_erp_wakeup(struct zfcp_adapter *adapter)
1da177e4 639{
1da177e4
LT
640 unsigned long flags;
641
ecf0c772 642 read_lock_irqsave(&adapter->erp_lock, flags);
287ac01a
CS
643 if (list_empty(&adapter->erp_ready_head) &&
644 list_empty(&adapter->erp_running_head)) {
805de8f4 645 atomic_andnot(ZFCP_STATUS_ADAPTER_ERP_PENDING,
287ac01a
CS
646 &adapter->status);
647 wake_up(&adapter->erp_done_wqh);
1da177e4 648 }
ecf0c772 649 read_unlock_irqrestore(&adapter->erp_lock, flags);
287ac01a 650}
1da177e4 651
287ac01a
CS
652static void zfcp_erp_enqueue_ptp_port(struct zfcp_adapter *adapter)
653{
654 struct zfcp_port *port;
655 port = zfcp_port_enqueue(adapter, adapter->peer_wwpn, 0,
656 adapter->peer_d_id);
657 if (IS_ERR(port)) /* error or port already attached */
658 return;
ea4a3a6a 659 _zfcp_erp_port_reopen(port, 0, "ereptp1");
287ac01a 660}
1da177e4 661
287ac01a
CS
662static int zfcp_erp_adapter_strat_fsf_xconf(struct zfcp_erp_action *erp_action)
663{
664 int retries;
665 int sleep = 1;
666 struct zfcp_adapter *adapter = erp_action->adapter;
1da177e4 667
805de8f4 668 atomic_andnot(ZFCP_STATUS_ADAPTER_XCONFIG_OK, &adapter->status);
287ac01a
CS
669
670 for (retries = 7; retries; retries--) {
805de8f4 671 atomic_andnot(ZFCP_STATUS_ADAPTER_HOST_CON_INIT,
287ac01a
CS
672 &adapter->status);
673 write_lock_irq(&adapter->erp_lock);
674 zfcp_erp_action_to_running(erp_action);
675 write_unlock_irq(&adapter->erp_lock);
676 if (zfcp_fsf_exchange_config_data(erp_action)) {
805de8f4 677 atomic_andnot(ZFCP_STATUS_ADAPTER_HOST_CON_INIT,
287ac01a
CS
678 &adapter->status);
679 return ZFCP_ERP_FAILED;
1da177e4 680 }
1da177e4 681
347c6a96
CS
682 wait_event(adapter->erp_ready_wq,
683 !list_empty(&adapter->erp_ready_head));
287ac01a
CS
684 if (erp_action->status & ZFCP_STATUS_ERP_TIMEDOUT)
685 break;
1da177e4 686
287ac01a
CS
687 if (!(atomic_read(&adapter->status) &
688 ZFCP_STATUS_ADAPTER_HOST_CON_INIT))
689 break;
1da177e4 690
287ac01a
CS
691 ssleep(sleep);
692 sleep *= 2;
1da177e4
LT
693 }
694
805de8f4 695 atomic_andnot(ZFCP_STATUS_ADAPTER_HOST_CON_INIT,
287ac01a 696 &adapter->status);
1da177e4 697
287ac01a
CS
698 if (!(atomic_read(&adapter->status) & ZFCP_STATUS_ADAPTER_XCONFIG_OK))
699 return ZFCP_ERP_FAILED;
41fa2ada 700
287ac01a
CS
701 if (fc_host_port_type(adapter->scsi_host) == FC_PORTTYPE_PTP)
702 zfcp_erp_enqueue_ptp_port(adapter);
1da177e4 703
287ac01a 704 return ZFCP_ERP_SUCCEEDED;
1da177e4
LT
705}
706
287ac01a 707static int zfcp_erp_adapter_strategy_open_fsf_xport(struct zfcp_erp_action *act)
1da177e4 708{
287ac01a
CS
709 int ret;
710 struct zfcp_adapter *adapter = act->adapter;
1da177e4 711
287ac01a
CS
712 write_lock_irq(&adapter->erp_lock);
713 zfcp_erp_action_to_running(act);
714 write_unlock_irq(&adapter->erp_lock);
715
716 ret = zfcp_fsf_exchange_port_data(act);
717 if (ret == -EOPNOTSUPP)
718 return ZFCP_ERP_SUCCEEDED;
719 if (ret)
720 return ZFCP_ERP_FAILED;
721
ae0904f6 722 zfcp_dbf_rec_run("erasox1", act);
347c6a96
CS
723 wait_event(adapter->erp_ready_wq,
724 !list_empty(&adapter->erp_ready_head));
ae0904f6 725 zfcp_dbf_rec_run("erasox2", act);
287ac01a
CS
726 if (act->status & ZFCP_STATUS_ERP_TIMEDOUT)
727 return ZFCP_ERP_FAILED;
728
729 return ZFCP_ERP_SUCCEEDED;
1da177e4
LT
730}
731
287ac01a 732static int zfcp_erp_adapter_strategy_open_fsf(struct zfcp_erp_action *act)
1da177e4 733{
287ac01a
CS
734 if (zfcp_erp_adapter_strat_fsf_xconf(act) == ZFCP_ERP_FAILED)
735 return ZFCP_ERP_FAILED;
1da177e4 736
287ac01a
CS
737 if (zfcp_erp_adapter_strategy_open_fsf_xport(act) == ZFCP_ERP_FAILED)
738 return ZFCP_ERP_FAILED;
1da177e4 739
c7b279ae 740 if (mempool_resize(act->adapter->pool.sr_data,
11d83360 741 act->adapter->stat_read_buf_num))
8d88cf3f
CS
742 return ZFCP_ERP_FAILED;
743
744 if (mempool_resize(act->adapter->pool.status_read_req,
11d83360 745 act->adapter->stat_read_buf_num))
8d88cf3f
CS
746 return ZFCP_ERP_FAILED;
747
64deb6ef 748 atomic_set(&act->adapter->stat_miss, act->adapter->stat_read_buf_num);
287ac01a
CS
749 if (zfcp_status_read_refill(act->adapter))
750 return ZFCP_ERP_FAILED;
1da177e4 751
287ac01a
CS
752 return ZFCP_ERP_SUCCEEDED;
753}
1da177e4 754
cf13c082 755static void zfcp_erp_adapter_strategy_close(struct zfcp_erp_action *act)
287ac01a 756{
287ac01a 757 struct zfcp_adapter *adapter = act->adapter;
1da177e4 758
287ac01a 759 /* close queues to ensure that buffers are not accessed by adapter */
564e1c86 760 zfcp_qdio_close(adapter->qdio);
287ac01a
CS
761 zfcp_fsf_req_dismiss_all(adapter);
762 adapter->fsf_req_seq_no = 0;
55c770fa 763 zfcp_fc_wka_ports_force_offline(adapter->gs);
b62a8d9b 764 /* all ports and LUNs are closed */
edaed859 765 zfcp_erp_clear_adapter_status(adapter, ZFCP_STATUS_COMMON_OPEN);
cf13c082 766
805de8f4 767 atomic_andnot(ZFCP_STATUS_ADAPTER_XCONFIG_OK |
cf13c082 768 ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED, &adapter->status);
1da177e4
LT
769}
770
cf13c082 771static int zfcp_erp_adapter_strategy_open(struct zfcp_erp_action *act)
1da177e4 772{
cf13c082 773 struct zfcp_adapter *adapter = act->adapter;
1da177e4 774
3d63d3b4 775 if (zfcp_qdio_open(adapter->qdio)) {
805de8f4 776 atomic_andnot(ZFCP_STATUS_ADAPTER_XCONFIG_OK |
cf13c082
SS
777 ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED,
778 &adapter->status);
779 return ZFCP_ERP_FAILED;
780 }
287ac01a 781
cf13c082
SS
782 if (zfcp_erp_adapter_strategy_open_fsf(act)) {
783 zfcp_erp_adapter_strategy_close(act);
784 return ZFCP_ERP_FAILED;
785 }
786
805de8f4 787 atomic_or(ZFCP_STATUS_COMMON_OPEN, &adapter->status);
cf13c082
SS
788
789 return ZFCP_ERP_SUCCEEDED;
790}
287ac01a 791
cf13c082
SS
792static int zfcp_erp_adapter_strategy(struct zfcp_erp_action *act)
793{
794 struct zfcp_adapter *adapter = act->adapter;
795
796 if (atomic_read(&adapter->status) & ZFCP_STATUS_COMMON_OPEN) {
797 zfcp_erp_adapter_strategy_close(act);
798 if (act->status & ZFCP_STATUS_ERP_CLOSE_ONLY)
799 return ZFCP_ERP_EXIT;
800 }
801
802 if (zfcp_erp_adapter_strategy_open(act)) {
287ac01a 803 ssleep(8);
cf13c082
SS
804 return ZFCP_ERP_FAILED;
805 }
1da177e4 806
cf13c082 807 return ZFCP_ERP_SUCCEEDED;
1da177e4
LT
808}
809
287ac01a 810static int zfcp_erp_port_forced_strategy_close(struct zfcp_erp_action *act)
1da177e4 811{
287ac01a
CS
812 int retval;
813
814 retval = zfcp_fsf_close_physical_port(act);
815 if (retval == -ENOMEM)
816 return ZFCP_ERP_NOMEM;
817 act->step = ZFCP_ERP_STEP_PHYS_PORT_CLOSING;
818 if (retval)
819 return ZFCP_ERP_FAILED;
820
821 return ZFCP_ERP_CONTINUES;
1da177e4
LT
822}
823
287ac01a
CS
824static int zfcp_erp_port_forced_strategy(struct zfcp_erp_action *erp_action)
825{
826 struct zfcp_port *port = erp_action->port;
827 int status = atomic_read(&port->status);
828
829 switch (erp_action->step) {
830 case ZFCP_ERP_STEP_UNINITIALIZED:
287ac01a
CS
831 if ((status & ZFCP_STATUS_PORT_PHYS_OPEN) &&
832 (status & ZFCP_STATUS_COMMON_OPEN))
833 return zfcp_erp_port_forced_strategy_close(erp_action);
834 else
835 return ZFCP_ERP_FAILED;
836
837 case ZFCP_ERP_STEP_PHYS_PORT_CLOSING:
ddb3e0c1 838 if (!(status & ZFCP_STATUS_PORT_PHYS_OPEN))
287ac01a
CS
839 return ZFCP_ERP_SUCCEEDED;
840 }
841 return ZFCP_ERP_FAILED;
1da177e4
LT
842}
843
287ac01a 844static int zfcp_erp_port_strategy_close(struct zfcp_erp_action *erp_action)
1da177e4 845{
287ac01a 846 int retval;
1da177e4 847
287ac01a
CS
848 retval = zfcp_fsf_close_port(erp_action);
849 if (retval == -ENOMEM)
850 return ZFCP_ERP_NOMEM;
851 erp_action->step = ZFCP_ERP_STEP_PORT_CLOSING;
852 if (retval)
853 return ZFCP_ERP_FAILED;
854 return ZFCP_ERP_CONTINUES;
1da177e4
LT
855}
856
287ac01a 857static int zfcp_erp_port_strategy_open_port(struct zfcp_erp_action *erp_action)
1da177e4 858{
287ac01a 859 int retval;
1da177e4 860
287ac01a
CS
861 retval = zfcp_fsf_open_port(erp_action);
862 if (retval == -ENOMEM)
863 return ZFCP_ERP_NOMEM;
864 erp_action->step = ZFCP_ERP_STEP_PORT_OPENING;
865 if (retval)
866 return ZFCP_ERP_FAILED;
867 return ZFCP_ERP_CONTINUES;
868}
1da177e4 869
287ac01a 870static int zfcp_erp_open_ptp_port(struct zfcp_erp_action *act)
1da177e4 871{
287ac01a
CS
872 struct zfcp_adapter *adapter = act->adapter;
873 struct zfcp_port *port = act->port;
1da177e4 874
287ac01a 875 if (port->wwpn != adapter->peer_wwpn) {
edaed859 876 zfcp_erp_set_port_status(port, ZFCP_STATUS_COMMON_ERP_FAILED);
287ac01a
CS
877 return ZFCP_ERP_FAILED;
878 }
879 port->d_id = adapter->peer_d_id;
287ac01a
CS
880 return zfcp_erp_port_strategy_open_port(act);
881}
882
883static int zfcp_erp_port_strategy_open_common(struct zfcp_erp_action *act)
884{
885 struct zfcp_adapter *adapter = act->adapter;
886 struct zfcp_port *port = act->port;
287ac01a
CS
887 int p_status = atomic_read(&port->status);
888
889 switch (act->step) {
890 case ZFCP_ERP_STEP_UNINITIALIZED:
891 case ZFCP_ERP_STEP_PHYS_PORT_CLOSING:
892 case ZFCP_ERP_STEP_PORT_CLOSING:
893 if (fc_host_port_type(adapter->scsi_host) == FC_PORTTYPE_PTP)
894 return zfcp_erp_open_ptp_port(act);
b98478d7 895 if (!port->d_id) {
934aeb58 896 zfcp_fc_trigger_did_lookup(port);
799b76d0 897 return ZFCP_ERP_EXIT;
287ac01a 898 }
287ac01a
CS
899 return zfcp_erp_port_strategy_open_port(act);
900
901 case ZFCP_ERP_STEP_PORT_OPENING:
902 /* D_ID might have changed during open */
5ab944f9 903 if (p_status & ZFCP_STATUS_COMMON_OPEN) {
934aeb58
CS
904 if (!port->d_id) {
905 zfcp_fc_trigger_did_lookup(port);
906 return ZFCP_ERP_EXIT;
5ab944f9 907 }
934aeb58 908 return ZFCP_ERP_SUCCEEDED;
5ab944f9 909 }
ea460a81
SS
910 if (port->d_id && !(p_status & ZFCP_STATUS_COMMON_NOESC)) {
911 port->d_id = 0;
f7bd7c36 912 return ZFCP_ERP_FAILED;
ea460a81
SS
913 }
914 /* fall through otherwise */
287ac01a
CS
915 }
916 return ZFCP_ERP_FAILED;
917}
918
287ac01a
CS
919static int zfcp_erp_port_strategy(struct zfcp_erp_action *erp_action)
920{
921 struct zfcp_port *port = erp_action->port;
934aeb58 922 int p_status = atomic_read(&port->status);
287ac01a 923
934aeb58
CS
924 if ((p_status & ZFCP_STATUS_COMMON_NOESC) &&
925 !(p_status & ZFCP_STATUS_COMMON_OPEN))
5ab944f9
SS
926 goto close_init_done;
927
287ac01a
CS
928 switch (erp_action->step) {
929 case ZFCP_ERP_STEP_UNINITIALIZED:
934aeb58 930 if (p_status & ZFCP_STATUS_COMMON_OPEN)
287ac01a 931 return zfcp_erp_port_strategy_close(erp_action);
1da177e4
LT
932 break;
933
287ac01a 934 case ZFCP_ERP_STEP_PORT_CLOSING:
934aeb58 935 if (p_status & ZFCP_STATUS_COMMON_OPEN)
287ac01a 936 return ZFCP_ERP_FAILED;
1da177e4
LT
937 break;
938 }
5ab944f9
SS
939
940close_init_done:
287ac01a
CS
941 if (erp_action->status & ZFCP_STATUS_ERP_CLOSE_ONLY)
942 return ZFCP_ERP_EXIT;
1da177e4 943
5ab944f9 944 return zfcp_erp_port_strategy_open_common(erp_action);
287ac01a
CS
945}
946
b62a8d9b 947static void zfcp_erp_lun_strategy_clearstati(struct scsi_device *sdev)
287ac01a 948{
b62a8d9b
CS
949 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
950
805de8f4 951 atomic_andnot(ZFCP_STATUS_COMMON_ACCESS_DENIED,
b62a8d9b 952 &zfcp_sdev->status);
287ac01a
CS
953}
954
b62a8d9b 955static int zfcp_erp_lun_strategy_close(struct zfcp_erp_action *erp_action)
287ac01a 956{
b62a8d9b 957 int retval = zfcp_fsf_close_lun(erp_action);
287ac01a
CS
958 if (retval == -ENOMEM)
959 return ZFCP_ERP_NOMEM;
b62a8d9b 960 erp_action->step = ZFCP_ERP_STEP_LUN_CLOSING;
287ac01a
CS
961 if (retval)
962 return ZFCP_ERP_FAILED;
963 return ZFCP_ERP_CONTINUES;
964}
965
b62a8d9b 966static int zfcp_erp_lun_strategy_open(struct zfcp_erp_action *erp_action)
287ac01a 967{
b62a8d9b 968 int retval = zfcp_fsf_open_lun(erp_action);
287ac01a
CS
969 if (retval == -ENOMEM)
970 return ZFCP_ERP_NOMEM;
b62a8d9b 971 erp_action->step = ZFCP_ERP_STEP_LUN_OPENING;
287ac01a
CS
972 if (retval)
973 return ZFCP_ERP_FAILED;
974 return ZFCP_ERP_CONTINUES;
1da177e4
LT
975}
976
b62a8d9b 977static int zfcp_erp_lun_strategy(struct zfcp_erp_action *erp_action)
1da177e4 978{
b62a8d9b
CS
979 struct scsi_device *sdev = erp_action->sdev;
980 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
287ac01a
CS
981
982 switch (erp_action->step) {
983 case ZFCP_ERP_STEP_UNINITIALIZED:
b62a8d9b
CS
984 zfcp_erp_lun_strategy_clearstati(sdev);
985 if (atomic_read(&zfcp_sdev->status) & ZFCP_STATUS_COMMON_OPEN)
986 return zfcp_erp_lun_strategy_close(erp_action);
287ac01a 987 /* already closed, fall through */
b62a8d9b
CS
988 case ZFCP_ERP_STEP_LUN_CLOSING:
989 if (atomic_read(&zfcp_sdev->status) & ZFCP_STATUS_COMMON_OPEN)
287ac01a
CS
990 return ZFCP_ERP_FAILED;
991 if (erp_action->status & ZFCP_STATUS_ERP_CLOSE_ONLY)
992 return ZFCP_ERP_EXIT;
b62a8d9b 993 return zfcp_erp_lun_strategy_open(erp_action);
287ac01a 994
b62a8d9b
CS
995 case ZFCP_ERP_STEP_LUN_OPENING:
996 if (atomic_read(&zfcp_sdev->status) & ZFCP_STATUS_COMMON_OPEN)
287ac01a
CS
997 return ZFCP_ERP_SUCCEEDED;
998 }
999 return ZFCP_ERP_FAILED;
1da177e4
LT
1000}
1001
b62a8d9b 1002static int zfcp_erp_strategy_check_lun(struct scsi_device *sdev, int result)
1da177e4 1003{
b62a8d9b
CS
1004 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
1005
1da177e4
LT
1006 switch (result) {
1007 case ZFCP_ERP_SUCCEEDED :
b62a8d9b
CS
1008 atomic_set(&zfcp_sdev->erp_counter, 0);
1009 zfcp_erp_lun_unblock(sdev);
1da177e4
LT
1010 break;
1011 case ZFCP_ERP_FAILED :
b62a8d9b
CS
1012 atomic_inc(&zfcp_sdev->erp_counter);
1013 if (atomic_read(&zfcp_sdev->erp_counter) > ZFCP_MAX_ERPS) {
1014 dev_err(&zfcp_sdev->port->adapter->ccw_device->dev,
1015 "ERP failed for LUN 0x%016Lx on "
ff3b24fa 1016 "port 0x%016Lx\n",
b62a8d9b
CS
1017 (unsigned long long)zfcp_scsi_dev_lun(sdev),
1018 (unsigned long long)zfcp_sdev->port->wwpn);
edaed859
SS
1019 zfcp_erp_set_lun_status(sdev,
1020 ZFCP_STATUS_COMMON_ERP_FAILED);
ff3b24fa 1021 }
1da177e4 1022 break;
1da177e4
LT
1023 }
1024
b62a8d9b
CS
1025 if (atomic_read(&zfcp_sdev->status) & ZFCP_STATUS_COMMON_ERP_FAILED) {
1026 zfcp_erp_lun_block(sdev, 0);
1da177e4
LT
1027 result = ZFCP_ERP_EXIT;
1028 }
1da177e4
LT
1029 return result;
1030}
1031
287ac01a 1032static int zfcp_erp_strategy_check_port(struct zfcp_port *port, int result)
1da177e4 1033{
1da177e4
LT
1034 switch (result) {
1035 case ZFCP_ERP_SUCCEEDED :
1036 atomic_set(&port->erp_counter, 0);
1037 zfcp_erp_port_unblock(port);
1038 break;
287ac01a 1039
1da177e4 1040 case ZFCP_ERP_FAILED :
287ac01a 1041 if (atomic_read(&port->status) & ZFCP_STATUS_COMMON_NOESC) {
cc8c2829
SS
1042 zfcp_erp_port_block(port, 0);
1043 result = ZFCP_ERP_EXIT;
1044 }
1da177e4 1045 atomic_inc(&port->erp_counter);
ff3b24fa
CS
1046 if (atomic_read(&port->erp_counter) > ZFCP_MAX_ERPS) {
1047 dev_err(&port->adapter->ccw_device->dev,
1048 "ERP failed for remote port 0x%016Lx\n",
7ba58c9c 1049 (unsigned long long)port->wwpn);
edaed859
SS
1050 zfcp_erp_set_port_status(port,
1051 ZFCP_STATUS_COMMON_ERP_FAILED);
ff3b24fa 1052 }
1da177e4 1053 break;
1da177e4
LT
1054 }
1055
287ac01a
CS
1056 if (atomic_read(&port->status) & ZFCP_STATUS_COMMON_ERP_FAILED) {
1057 zfcp_erp_port_block(port, 0);
1da177e4
LT
1058 result = ZFCP_ERP_EXIT;
1059 }
1da177e4
LT
1060 return result;
1061}
1062
287ac01a
CS
1063static int zfcp_erp_strategy_check_adapter(struct zfcp_adapter *adapter,
1064 int result)
1da177e4 1065{
1da177e4
LT
1066 switch (result) {
1067 case ZFCP_ERP_SUCCEEDED :
1068 atomic_set(&adapter->erp_counter, 0);
1069 zfcp_erp_adapter_unblock(adapter);
1070 break;
287ac01a 1071
1da177e4
LT
1072 case ZFCP_ERP_FAILED :
1073 atomic_inc(&adapter->erp_counter);
ff3b24fa
CS
1074 if (atomic_read(&adapter->erp_counter) > ZFCP_MAX_ERPS) {
1075 dev_err(&adapter->ccw_device->dev,
1076 "ERP cannot recover an error "
1077 "on the FCP device\n");
edaed859
SS
1078 zfcp_erp_set_adapter_status(adapter,
1079 ZFCP_STATUS_COMMON_ERP_FAILED);
ff3b24fa 1080 }
1da177e4 1081 break;
1da177e4
LT
1082 }
1083
287ac01a
CS
1084 if (atomic_read(&adapter->status) & ZFCP_STATUS_COMMON_ERP_FAILED) {
1085 zfcp_erp_adapter_block(adapter, 0);
1da177e4
LT
1086 result = ZFCP_ERP_EXIT;
1087 }
1da177e4
LT
1088 return result;
1089}
1090
287ac01a
CS
1091static int zfcp_erp_strategy_check_target(struct zfcp_erp_action *erp_action,
1092 int result)
5f852be9 1093{
287ac01a
CS
1094 struct zfcp_adapter *adapter = erp_action->adapter;
1095 struct zfcp_port *port = erp_action->port;
b62a8d9b 1096 struct scsi_device *sdev = erp_action->sdev;
287ac01a
CS
1097
1098 switch (erp_action->action) {
1099
b62a8d9b
CS
1100 case ZFCP_ERP_ACTION_REOPEN_LUN:
1101 result = zfcp_erp_strategy_check_lun(sdev, result);
287ac01a
CS
1102 break;
1103
1104 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
1105 case ZFCP_ERP_ACTION_REOPEN_PORT:
1106 result = zfcp_erp_strategy_check_port(port, result);
1107 break;
1108
1109 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
1110 result = zfcp_erp_strategy_check_adapter(adapter, result);
1111 break;
1112 }
1113 return result;
5f852be9
CS
1114}
1115
287ac01a 1116static int zfcp_erp_strat_change_det(atomic_t *target_status, u32 erp_status)
5f852be9 1117{
287ac01a 1118 int status = atomic_read(target_status);
5f852be9 1119
287ac01a
CS
1120 if ((status & ZFCP_STATUS_COMMON_RUNNING) &&
1121 (erp_status & ZFCP_STATUS_ERP_CLOSE_ONLY))
1122 return 1; /* take it online */
5f852be9 1123
287ac01a
CS
1124 if (!(status & ZFCP_STATUS_COMMON_RUNNING) &&
1125 !(erp_status & ZFCP_STATUS_ERP_CLOSE_ONLY))
1126 return 1; /* take it offline */
1127
1128 return 0;
5f852be9
CS
1129}
1130
287ac01a 1131static int zfcp_erp_strategy_statechange(struct zfcp_erp_action *act, int ret)
1da177e4 1132{
287ac01a
CS
1133 int action = act->action;
1134 struct zfcp_adapter *adapter = act->adapter;
1135 struct zfcp_port *port = act->port;
b62a8d9b
CS
1136 struct scsi_device *sdev = act->sdev;
1137 struct zfcp_scsi_dev *zfcp_sdev;
287ac01a 1138 u32 erp_status = act->status;
1da177e4 1139
287ac01a 1140 switch (action) {
1da177e4 1141 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
287ac01a
CS
1142 if (zfcp_erp_strat_change_det(&adapter->status, erp_status)) {
1143 _zfcp_erp_adapter_reopen(adapter,
1144 ZFCP_STATUS_COMMON_ERP_FAILED,
ea4a3a6a 1145 "ersscg1");
287ac01a
CS
1146 return ZFCP_ERP_EXIT;
1147 }
1da177e4
LT
1148 break;
1149
1150 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
1da177e4 1151 case ZFCP_ERP_ACTION_REOPEN_PORT:
287ac01a
CS
1152 if (zfcp_erp_strat_change_det(&port->status, erp_status)) {
1153 _zfcp_erp_port_reopen(port,
1154 ZFCP_STATUS_COMMON_ERP_FAILED,
ea4a3a6a 1155 "ersscg2");
287ac01a
CS
1156 return ZFCP_ERP_EXIT;
1157 }
1da177e4
LT
1158 break;
1159
b62a8d9b
CS
1160 case ZFCP_ERP_ACTION_REOPEN_LUN:
1161 zfcp_sdev = sdev_to_zfcp(sdev);
1162 if (zfcp_erp_strat_change_det(&zfcp_sdev->status, erp_status)) {
1163 _zfcp_erp_lun_reopen(sdev,
1164 ZFCP_STATUS_COMMON_ERP_FAILED,
ea4a3a6a 1165 "ersscg3", 0);
287ac01a
CS
1166 return ZFCP_ERP_EXIT;
1167 }
1da177e4
LT
1168 break;
1169 }
287ac01a 1170 return ret;
1da177e4
LT
1171}
1172
287ac01a 1173static void zfcp_erp_action_dequeue(struct zfcp_erp_action *erp_action)
1da177e4 1174{
287ac01a 1175 struct zfcp_adapter *adapter = erp_action->adapter;
b62a8d9b 1176 struct zfcp_scsi_dev *zfcp_sdev;
1da177e4 1177
287ac01a
CS
1178 adapter->erp_total_count--;
1179 if (erp_action->status & ZFCP_STATUS_ERP_LOWMEM) {
1180 adapter->erp_low_mem_count--;
1181 erp_action->status &= ~ZFCP_STATUS_ERP_LOWMEM;
507e4969 1182 }
1da177e4 1183
287ac01a 1184 list_del(&erp_action->list);
ae0904f6 1185 zfcp_dbf_rec_run("eractd1", erp_action);
1da177e4 1186
287ac01a 1187 switch (erp_action->action) {
b62a8d9b
CS
1188 case ZFCP_ERP_ACTION_REOPEN_LUN:
1189 zfcp_sdev = sdev_to_zfcp(erp_action->sdev);
805de8f4 1190 atomic_andnot(ZFCP_STATUS_COMMON_ERP_INUSE,
b62a8d9b 1191 &zfcp_sdev->status);
287ac01a 1192 break;
1da177e4 1193
287ac01a
CS
1194 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
1195 case ZFCP_ERP_ACTION_REOPEN_PORT:
805de8f4 1196 atomic_andnot(ZFCP_STATUS_COMMON_ERP_INUSE,
287ac01a
CS
1197 &erp_action->port->status);
1198 break;
1da177e4 1199
287ac01a 1200 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
805de8f4 1201 atomic_andnot(ZFCP_STATUS_COMMON_ERP_INUSE,
287ac01a
CS
1202 &erp_action->adapter->status);
1203 break;
1204 }
1da177e4
LT
1205}
1206
287ac01a 1207static void zfcp_erp_action_cleanup(struct zfcp_erp_action *act, int result)
1da177e4 1208{
287ac01a
CS
1209 struct zfcp_adapter *adapter = act->adapter;
1210 struct zfcp_port *port = act->port;
b62a8d9b 1211 struct scsi_device *sdev = act->sdev;
1da177e4 1212
287ac01a 1213 switch (act->action) {
b62a8d9b 1214 case ZFCP_ERP_ACTION_REOPEN_LUN:
fdbd1c5e 1215 if (!(act->status & ZFCP_STATUS_ERP_NO_REF))
b62a8d9b 1216 scsi_device_put(sdev);
287ac01a 1217 break;
1da177e4 1218
287ac01a 1219 case ZFCP_ERP_ACTION_REOPEN_PORT:
a2fa0aed
CS
1220 if (result == ZFCP_ERP_SUCCEEDED)
1221 zfcp_scsi_schedule_rport_register(port);
5767620c
CS
1222 /* fall through */
1223 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
615f59e0 1224 put_device(&port->dev);
287ac01a
CS
1225 break;
1226
1227 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
a2fa0aed 1228 if (result == ZFCP_ERP_SUCCEEDED) {
bd43a42b 1229 register_service_level(&adapter->service_level);
43f60cbd 1230 zfcp_fc_conditional_port_scan(adapter);
038d9446 1231 queue_work(adapter->work_queue, &adapter->ns_up_work);
a2fa0aed
CS
1232 } else
1233 unregister_service_level(&adapter->service_level);
038d9446 1234
f3450c7b 1235 kref_put(&adapter->ref, zfcp_adapter_release);
287ac01a
CS
1236 break;
1237 }
1da177e4
LT
1238}
1239
287ac01a 1240static int zfcp_erp_strategy_do_action(struct zfcp_erp_action *erp_action)
1da177e4 1241{
287ac01a
CS
1242 switch (erp_action->action) {
1243 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
1244 return zfcp_erp_adapter_strategy(erp_action);
1245 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
1246 return zfcp_erp_port_forced_strategy(erp_action);
1247 case ZFCP_ERP_ACTION_REOPEN_PORT:
1248 return zfcp_erp_port_strategy(erp_action);
b62a8d9b
CS
1249 case ZFCP_ERP_ACTION_REOPEN_LUN:
1250 return zfcp_erp_lun_strategy(erp_action);
287ac01a
CS
1251 }
1252 return ZFCP_ERP_FAILED;
1da177e4
LT
1253}
1254
287ac01a 1255static int zfcp_erp_strategy(struct zfcp_erp_action *erp_action)
1da177e4
LT
1256{
1257 int retval;
287ac01a 1258 unsigned long flags;
ecf0c772 1259 struct zfcp_adapter *adapter = erp_action->adapter;
1da177e4 1260
f3450c7b 1261 kref_get(&adapter->ref);
1da177e4 1262
f3450c7b 1263 write_lock_irqsave(&adapter->erp_lock, flags);
287ac01a 1264 zfcp_erp_strategy_check_fsfreq(erp_action);
1da177e4 1265
287ac01a
CS
1266 if (erp_action->status & ZFCP_STATUS_ERP_DISMISSED) {
1267 zfcp_erp_action_dequeue(erp_action);
1268 retval = ZFCP_ERP_DISMISSED;
1269 goto unlock;
22753fa5 1270 }
1da177e4 1271
9c785d94
CS
1272 if (erp_action->status & ZFCP_STATUS_ERP_TIMEDOUT) {
1273 retval = ZFCP_ERP_FAILED;
1274 goto check_target;
1275 }
1276
2f8f3ed5 1277 zfcp_erp_action_to_running(erp_action);
1da177e4 1278
287ac01a 1279 /* no lock to allow for blocking operations */
ecf0c772 1280 write_unlock_irqrestore(&adapter->erp_lock, flags);
287ac01a 1281 retval = zfcp_erp_strategy_do_action(erp_action);
ecf0c772 1282 write_lock_irqsave(&adapter->erp_lock, flags);
1da177e4 1283
287ac01a
CS
1284 if (erp_action->status & ZFCP_STATUS_ERP_DISMISSED)
1285 retval = ZFCP_ERP_CONTINUES;
cc8c2829 1286
287ac01a
CS
1287 switch (retval) {
1288 case ZFCP_ERP_NOMEM:
1289 if (!(erp_action->status & ZFCP_STATUS_ERP_LOWMEM)) {
1290 ++adapter->erp_low_mem_count;
1291 erp_action->status |= ZFCP_STATUS_ERP_LOWMEM;
1da177e4 1292 }
287ac01a 1293 if (adapter->erp_total_count == adapter->erp_low_mem_count)
ea4a3a6a 1294 _zfcp_erp_adapter_reopen(adapter, 0, "erstgy1");
287ac01a
CS
1295 else {
1296 zfcp_erp_strategy_memwait(erp_action);
1297 retval = ZFCP_ERP_CONTINUES;
1da177e4 1298 }
287ac01a 1299 goto unlock;
1da177e4 1300
287ac01a
CS
1301 case ZFCP_ERP_CONTINUES:
1302 if (erp_action->status & ZFCP_STATUS_ERP_LOWMEM) {
1303 --adapter->erp_low_mem_count;
1304 erp_action->status &= ~ZFCP_STATUS_ERP_LOWMEM;
1da177e4 1305 }
287ac01a 1306 goto unlock;
1da177e4
LT
1307 }
1308
9c785d94 1309check_target:
287ac01a
CS
1310 retval = zfcp_erp_strategy_check_target(erp_action, retval);
1311 zfcp_erp_action_dequeue(erp_action);
1312 retval = zfcp_erp_strategy_statechange(erp_action, retval);
1313 if (retval == ZFCP_ERP_EXIT)
1314 goto unlock;
85600f7f
CS
1315 if (retval == ZFCP_ERP_SUCCEEDED)
1316 zfcp_erp_strategy_followup_success(erp_action);
1317 if (retval == ZFCP_ERP_FAILED)
1318 zfcp_erp_strategy_followup_failed(erp_action);
1da177e4 1319
287ac01a 1320 unlock:
ecf0c772 1321 write_unlock_irqrestore(&adapter->erp_lock, flags);
1da177e4 1322
287ac01a
CS
1323 if (retval != ZFCP_ERP_CONTINUES)
1324 zfcp_erp_action_cleanup(erp_action, retval);
1da177e4 1325
f3450c7b 1326 kref_put(&adapter->ref, zfcp_adapter_release);
1da177e4
LT
1327 return retval;
1328}
1329
287ac01a 1330static int zfcp_erp_thread(void *data)
1da177e4 1331{
287ac01a
CS
1332 struct zfcp_adapter *adapter = (struct zfcp_adapter *) data;
1333 struct list_head *next;
1334 struct zfcp_erp_action *act;
1335 unsigned long flags;
94ab4b38 1336
347c6a96 1337 for (;;) {
347c6a96
CS
1338 wait_event_interruptible(adapter->erp_ready_wq,
1339 !list_empty(&adapter->erp_ready_head) ||
1340 kthread_should_stop());
94ab4b38 1341
347c6a96
CS
1342 if (kthread_should_stop())
1343 break;
1344
287ac01a
CS
1345 write_lock_irqsave(&adapter->erp_lock, flags);
1346 next = adapter->erp_ready_head.next;
1347 write_unlock_irqrestore(&adapter->erp_lock, flags);
1da177e4 1348
287ac01a
CS
1349 if (next != &adapter->erp_ready_head) {
1350 act = list_entry(next, struct zfcp_erp_action, list);
1da177e4 1351
287ac01a
CS
1352 /* there is more to come after dismission, no notify */
1353 if (zfcp_erp_strategy(act) != ZFCP_ERP_DISMISSED)
1354 zfcp_erp_wakeup(adapter);
1da177e4 1355 }
1da177e4
LT
1356 }
1357
287ac01a
CS
1358 return 0;
1359}
1da177e4 1360
287ac01a
CS
1361/**
1362 * zfcp_erp_thread_setup - Start ERP thread for adapter
1363 * @adapter: Adapter to start the ERP thread for
1364 *
1365 * Returns 0 on success or error code from kernel_thread()
1366 */
1367int zfcp_erp_thread_setup(struct zfcp_adapter *adapter)
1368{
347c6a96 1369 struct task_struct *thread;
1da177e4 1370
347c6a96
CS
1371 thread = kthread_run(zfcp_erp_thread, adapter, "zfcperp%s",
1372 dev_name(&adapter->ccw_device->dev));
1373 if (IS_ERR(thread)) {
287ac01a 1374 dev_err(&adapter->ccw_device->dev,
ff3b24fa 1375 "Creating an ERP thread for the FCP device failed.\n");
347c6a96 1376 return PTR_ERR(thread);
1da177e4 1377 }
347c6a96
CS
1378
1379 adapter->erp_thread = thread;
287ac01a
CS
1380 return 0;
1381}
1da177e4 1382
287ac01a
CS
1383/**
1384 * zfcp_erp_thread_kill - Stop ERP thread.
1385 * @adapter: Adapter where the ERP thread should be stopped.
1386 *
1387 * The caller of this routine ensures that the specified adapter has
1388 * been shut down and that this operation has been completed. Thus,
1389 * there are no pending erp_actions which would need to be handled
1390 * here.
1391 */
1392void zfcp_erp_thread_kill(struct zfcp_adapter *adapter)
1393{
347c6a96
CS
1394 kthread_stop(adapter->erp_thread);
1395 adapter->erp_thread = NULL;
143bb6bf
CS
1396 WARN_ON(!list_empty(&adapter->erp_ready_head));
1397 WARN_ON(!list_empty(&adapter->erp_running_head));
1da177e4
LT
1398}
1399
287ac01a 1400/**
edaed859
SS
1401 * zfcp_erp_wait - wait for completion of error recovery on an adapter
1402 * @adapter: adapter for which to wait for completion of its error recovery
287ac01a 1403 */
edaed859 1404void zfcp_erp_wait(struct zfcp_adapter *adapter)
1da177e4 1405{
edaed859
SS
1406 wait_event(adapter->erp_done_wqh,
1407 !(atomic_read(&adapter->status) &
1408 ZFCP_STATUS_ADAPTER_ERP_PENDING));
287ac01a 1409}
1da177e4 1410
287ac01a 1411/**
edaed859
SS
1412 * zfcp_erp_set_adapter_status - set adapter status bits
1413 * @adapter: adapter to change the status
1414 * @mask: status bits to change
1415 *
1416 * Changes in common status bits are propagated to attached ports and LUNs.
287ac01a 1417 */
edaed859 1418void zfcp_erp_set_adapter_status(struct zfcp_adapter *adapter, u32 mask)
287ac01a 1419{
edaed859
SS
1420 struct zfcp_port *port;
1421 struct scsi_device *sdev;
1422 unsigned long flags;
1423 u32 common_mask = mask & ZFCP_COMMON_FLAGS;
1da177e4 1424
805de8f4 1425 atomic_or(mask, &adapter->status);
1da177e4 1426
edaed859
SS
1427 if (!common_mask)
1428 return;
1429
1430 read_lock_irqsave(&adapter->port_list_lock, flags);
1431 list_for_each_entry(port, &adapter->port_list, list)
805de8f4 1432 atomic_or(common_mask, &port->status);
edaed859
SS
1433 read_unlock_irqrestore(&adapter->port_list_lock, flags);
1434
924dd584
MP
1435 spin_lock_irqsave(adapter->scsi_host->host_lock, flags);
1436 __shost_for_each_device(sdev, adapter->scsi_host)
805de8f4 1437 atomic_or(common_mask, &sdev_to_zfcp(sdev)->status);
924dd584 1438 spin_unlock_irqrestore(adapter->scsi_host->host_lock, flags);
287ac01a 1439}
1da177e4 1440
287ac01a 1441/**
edaed859 1442 * zfcp_erp_clear_adapter_status - clear adapter status bits
287ac01a 1443 * @adapter: adapter to change the status
287ac01a 1444 * @mask: status bits to change
287ac01a 1445 *
b62a8d9b 1446 * Changes in common status bits are propagated to attached ports and LUNs.
287ac01a 1447 */
edaed859 1448void zfcp_erp_clear_adapter_status(struct zfcp_adapter *adapter, u32 mask)
1da177e4 1449{
1da177e4 1450 struct zfcp_port *port;
edaed859 1451 struct scsi_device *sdev;
ecf0c772 1452 unsigned long flags;
287ac01a 1453 u32 common_mask = mask & ZFCP_COMMON_FLAGS;
edaed859 1454 u32 clear_counter = mask & ZFCP_STATUS_COMMON_ERP_FAILED;
1da177e4 1455
805de8f4 1456 atomic_andnot(mask, &adapter->status);
edaed859
SS
1457
1458 if (!common_mask)
1459 return;
1460
1461 if (clear_counter)
1462 atomic_set(&adapter->erp_counter, 0);
1463
1464 read_lock_irqsave(&adapter->port_list_lock, flags);
1465 list_for_each_entry(port, &adapter->port_list, list) {
805de8f4 1466 atomic_andnot(common_mask, &port->status);
edaed859
SS
1467 if (clear_counter)
1468 atomic_set(&port->erp_counter, 0);
287ac01a 1469 }
edaed859 1470 read_unlock_irqrestore(&adapter->port_list_lock, flags);
287ac01a 1471
924dd584
MP
1472 spin_lock_irqsave(adapter->scsi_host->host_lock, flags);
1473 __shost_for_each_device(sdev, adapter->scsi_host) {
805de8f4 1474 atomic_andnot(common_mask, &sdev_to_zfcp(sdev)->status);
edaed859
SS
1475 if (clear_counter)
1476 atomic_set(&sdev_to_zfcp(sdev)->erp_counter, 0);
ecf0c772 1477 }
924dd584 1478 spin_unlock_irqrestore(adapter->scsi_host->host_lock, flags);
1da177e4
LT
1479}
1480
287ac01a 1481/**
edaed859
SS
1482 * zfcp_erp_set_port_status - set port status bits
1483 * @port: port to change the status
287ac01a 1484 * @mask: status bits to change
287ac01a 1485 *
b62a8d9b 1486 * Changes in common status bits are propagated to attached LUNs.
287ac01a 1487 */
edaed859 1488void zfcp_erp_set_port_status(struct zfcp_port *port, u32 mask)
1da177e4 1489{
b62a8d9b 1490 struct scsi_device *sdev;
287ac01a 1491 u32 common_mask = mask & ZFCP_COMMON_FLAGS;
924dd584 1492 unsigned long flags;
1da177e4 1493
805de8f4 1494 atomic_or(mask, &port->status);
1da177e4 1495
edaed859
SS
1496 if (!common_mask)
1497 return;
1498
924dd584
MP
1499 spin_lock_irqsave(port->adapter->scsi_host->host_lock, flags);
1500 __shost_for_each_device(sdev, port->adapter->scsi_host)
edaed859 1501 if (sdev_to_zfcp(sdev)->port == port)
805de8f4 1502 atomic_or(common_mask,
edaed859 1503 &sdev_to_zfcp(sdev)->status);
924dd584 1504 spin_unlock_irqrestore(port->adapter->scsi_host->host_lock, flags);
1da177e4
LT
1505}
1506
287ac01a 1507/**
edaed859
SS
1508 * zfcp_erp_clear_port_status - clear port status bits
1509 * @port: adapter to change the status
287ac01a 1510 * @mask: status bits to change
edaed859
SS
1511 *
1512 * Changes in common status bits are propagated to attached LUNs.
287ac01a 1513 */
edaed859 1514void zfcp_erp_clear_port_status(struct zfcp_port *port, u32 mask)
1da177e4 1515{
edaed859
SS
1516 struct scsi_device *sdev;
1517 u32 common_mask = mask & ZFCP_COMMON_FLAGS;
1518 u32 clear_counter = mask & ZFCP_STATUS_COMMON_ERP_FAILED;
924dd584 1519 unsigned long flags;
edaed859 1520
805de8f4 1521 atomic_andnot(mask, &port->status);
edaed859
SS
1522
1523 if (!common_mask)
1524 return;
b62a8d9b 1525
edaed859
SS
1526 if (clear_counter)
1527 atomic_set(&port->erp_counter, 0);
1528
924dd584
MP
1529 spin_lock_irqsave(port->adapter->scsi_host->host_lock, flags);
1530 __shost_for_each_device(sdev, port->adapter->scsi_host)
edaed859 1531 if (sdev_to_zfcp(sdev)->port == port) {
805de8f4 1532 atomic_andnot(common_mask,
edaed859
SS
1533 &sdev_to_zfcp(sdev)->status);
1534 if (clear_counter)
1535 atomic_set(&sdev_to_zfcp(sdev)->erp_counter, 0);
287ac01a 1536 }
924dd584 1537 spin_unlock_irqrestore(port->adapter->scsi_host->host_lock, flags);
1da177e4
LT
1538}
1539
287ac01a 1540/**
edaed859
SS
1541 * zfcp_erp_set_lun_status - set lun status bits
1542 * @sdev: SCSI device / lun to set the status bits
1543 * @mask: status bits to change
287ac01a 1544 */
edaed859 1545void zfcp_erp_set_lun_status(struct scsi_device *sdev, u32 mask)
d736a27b 1546{
edaed859
SS
1547 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
1548
805de8f4 1549 atomic_or(mask, &zfcp_sdev->status);
d736a27b
AH
1550}
1551
287ac01a 1552/**
edaed859
SS
1553 * zfcp_erp_clear_lun_status - clear lun status bits
1554 * @sdev: SCSi device / lun to clear the status bits
1555 * @mask: status bits to change
287ac01a 1556 */
edaed859 1557void zfcp_erp_clear_lun_status(struct scsi_device *sdev, u32 mask)
d736a27b 1558{
edaed859
SS
1559 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
1560
805de8f4 1561 atomic_andnot(mask, &zfcp_sdev->status);
edaed859
SS
1562
1563 if (mask & ZFCP_STATUS_COMMON_ERP_FAILED)
1564 atomic_set(&zfcp_sdev->erp_counter, 0);
d736a27b 1565}
edaed859 1566