Linux 4.19.133
[linux/fpc-iii.git] / drivers / s390 / scsi / zfcp_erp.c
blobb7afdb55a459fad9e60a71630a83b2ed0e43c9cb
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * zfcp device driver
5 * Error Recovery Procedures (ERP).
7 * Copyright IBM Corp. 2002, 2016
8 */
10 #define KMSG_COMPONENT "zfcp"
11 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
13 #include <linux/kthread.h>
14 #include <linux/bug.h>
15 #include "zfcp_ext.h"
16 #include "zfcp_reqlist.h"
18 #define ZFCP_MAX_ERPS 3
20 enum zfcp_erp_act_flags {
21 ZFCP_STATUS_ERP_TIMEDOUT = 0x10000000,
22 ZFCP_STATUS_ERP_CLOSE_ONLY = 0x01000000,
23 ZFCP_STATUS_ERP_DISMISSED = 0x00200000,
24 ZFCP_STATUS_ERP_LOWMEM = 0x00400000,
25 ZFCP_STATUS_ERP_NO_REF = 0x00800000,
28 enum zfcp_erp_steps {
29 ZFCP_ERP_STEP_UNINITIALIZED = 0x0000,
30 ZFCP_ERP_STEP_PHYS_PORT_CLOSING = 0x0010,
31 ZFCP_ERP_STEP_PORT_CLOSING = 0x0100,
32 ZFCP_ERP_STEP_PORT_OPENING = 0x0800,
33 ZFCP_ERP_STEP_LUN_CLOSING = 0x1000,
34 ZFCP_ERP_STEP_LUN_OPENING = 0x2000,
37 /**
38 * enum zfcp_erp_act_type - Type of ERP action object.
39 * @ZFCP_ERP_ACTION_REOPEN_LUN: LUN recovery.
40 * @ZFCP_ERP_ACTION_REOPEN_PORT: Port recovery.
41 * @ZFCP_ERP_ACTION_REOPEN_PORT_FORCED: Forced port recovery.
42 * @ZFCP_ERP_ACTION_REOPEN_ADAPTER: Adapter recovery.
43 * @ZFCP_ERP_ACTION_NONE: Eyecatcher pseudo flag to bitwise or-combine with
44 * either of the first four enum values.
45 * Used to indicate that an ERP action could not be
46 * set up despite a detected need for some recovery.
47 * @ZFCP_ERP_ACTION_FAILED: Eyecatcher pseudo flag to bitwise or-combine with
48 * either of the first four enum values.
49 * Used to indicate that ERP not needed because
50 * the object has ZFCP_STATUS_COMMON_ERP_FAILED.
52 enum zfcp_erp_act_type {
53 ZFCP_ERP_ACTION_REOPEN_LUN = 1,
54 ZFCP_ERP_ACTION_REOPEN_PORT = 2,
55 ZFCP_ERP_ACTION_REOPEN_PORT_FORCED = 3,
56 ZFCP_ERP_ACTION_REOPEN_ADAPTER = 4,
57 ZFCP_ERP_ACTION_NONE = 0xc0,
58 ZFCP_ERP_ACTION_FAILED = 0xe0,
61 enum zfcp_erp_act_result {
62 ZFCP_ERP_SUCCEEDED = 0,
63 ZFCP_ERP_FAILED = 1,
64 ZFCP_ERP_CONTINUES = 2,
65 ZFCP_ERP_EXIT = 3,
66 ZFCP_ERP_DISMISSED = 4,
67 ZFCP_ERP_NOMEM = 5,
70 static void zfcp_erp_adapter_block(struct zfcp_adapter *adapter, int mask)
72 zfcp_erp_clear_adapter_status(adapter,
73 ZFCP_STATUS_COMMON_UNBLOCKED | mask);
76 static bool zfcp_erp_action_is_running(struct zfcp_erp_action *act)
78 struct zfcp_erp_action *curr_act;
80 list_for_each_entry(curr_act, &act->adapter->erp_running_head, list)
81 if (act == curr_act)
82 return true;
83 return false;
86 static void zfcp_erp_action_ready(struct zfcp_erp_action *act)
88 struct zfcp_adapter *adapter = act->adapter;
90 list_move(&act->list, &act->adapter->erp_ready_head);
91 zfcp_dbf_rec_run("erardy1", act);
92 wake_up(&adapter->erp_ready_wq);
93 zfcp_dbf_rec_run("erardy2", act);
96 static void zfcp_erp_action_dismiss(struct zfcp_erp_action *act)
98 act->status |= ZFCP_STATUS_ERP_DISMISSED;
99 if (zfcp_erp_action_is_running(act))
100 zfcp_erp_action_ready(act);
103 static void zfcp_erp_action_dismiss_lun(struct scsi_device *sdev)
105 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
107 if (atomic_read(&zfcp_sdev->status) & ZFCP_STATUS_COMMON_ERP_INUSE)
108 zfcp_erp_action_dismiss(&zfcp_sdev->erp_action);
111 static void zfcp_erp_action_dismiss_port(struct zfcp_port *port)
113 struct scsi_device *sdev;
115 if (atomic_read(&port->status) & ZFCP_STATUS_COMMON_ERP_INUSE)
116 zfcp_erp_action_dismiss(&port->erp_action);
117 else {
118 spin_lock(port->adapter->scsi_host->host_lock);
119 __shost_for_each_device(sdev, port->adapter->scsi_host)
120 if (sdev_to_zfcp(sdev)->port == port)
121 zfcp_erp_action_dismiss_lun(sdev);
122 spin_unlock(port->adapter->scsi_host->host_lock);
126 static void zfcp_erp_action_dismiss_adapter(struct zfcp_adapter *adapter)
128 struct zfcp_port *port;
130 if (atomic_read(&adapter->status) & ZFCP_STATUS_COMMON_ERP_INUSE)
131 zfcp_erp_action_dismiss(&adapter->erp_action);
132 else {
133 read_lock(&adapter->port_list_lock);
134 list_for_each_entry(port, &adapter->port_list, list)
135 zfcp_erp_action_dismiss_port(port);
136 read_unlock(&adapter->port_list_lock);
140 static int zfcp_erp_handle_failed(int want, struct zfcp_adapter *adapter,
141 struct zfcp_port *port,
142 struct scsi_device *sdev)
144 int need = want;
145 struct zfcp_scsi_dev *zsdev;
147 switch (want) {
148 case ZFCP_ERP_ACTION_REOPEN_LUN:
149 zsdev = sdev_to_zfcp(sdev);
150 if (atomic_read(&zsdev->status) & ZFCP_STATUS_COMMON_ERP_FAILED)
151 need = 0;
152 break;
153 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
154 if (atomic_read(&port->status) & ZFCP_STATUS_COMMON_ERP_FAILED)
155 need = 0;
156 break;
157 case ZFCP_ERP_ACTION_REOPEN_PORT:
158 if (atomic_read(&port->status) &
159 ZFCP_STATUS_COMMON_ERP_FAILED) {
160 need = 0;
161 /* ensure propagation of failed status to new devices */
162 zfcp_erp_set_port_status(
163 port, ZFCP_STATUS_COMMON_ERP_FAILED);
165 break;
166 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
167 if (atomic_read(&adapter->status) &
168 ZFCP_STATUS_COMMON_ERP_FAILED) {
169 need = 0;
170 /* ensure propagation of failed status to new devices */
171 zfcp_erp_set_adapter_status(
172 adapter, ZFCP_STATUS_COMMON_ERP_FAILED);
174 break;
177 return need;
180 static int zfcp_erp_required_act(int want, struct zfcp_adapter *adapter,
181 struct zfcp_port *port,
182 struct scsi_device *sdev)
184 int need = want;
185 int l_status, p_status, a_status;
186 struct zfcp_scsi_dev *zfcp_sdev;
188 switch (want) {
189 case ZFCP_ERP_ACTION_REOPEN_LUN:
190 zfcp_sdev = sdev_to_zfcp(sdev);
191 l_status = atomic_read(&zfcp_sdev->status);
192 if (l_status & ZFCP_STATUS_COMMON_ERP_INUSE)
193 return 0;
194 p_status = atomic_read(&port->status);
195 if (!(p_status & ZFCP_STATUS_COMMON_RUNNING) ||
196 p_status & ZFCP_STATUS_COMMON_ERP_FAILED)
197 return 0;
198 if (!(p_status & ZFCP_STATUS_COMMON_UNBLOCKED))
199 need = ZFCP_ERP_ACTION_REOPEN_PORT;
200 /* fall through */
201 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
202 p_status = atomic_read(&port->status);
203 if (!(p_status & ZFCP_STATUS_COMMON_OPEN))
204 need = ZFCP_ERP_ACTION_REOPEN_PORT;
205 /* fall through */
206 case ZFCP_ERP_ACTION_REOPEN_PORT:
207 p_status = atomic_read(&port->status);
208 if (p_status & ZFCP_STATUS_COMMON_ERP_INUSE)
209 return 0;
210 a_status = atomic_read(&adapter->status);
211 if (!(a_status & ZFCP_STATUS_COMMON_RUNNING) ||
212 a_status & ZFCP_STATUS_COMMON_ERP_FAILED)
213 return 0;
214 if (p_status & ZFCP_STATUS_COMMON_NOESC)
215 return need;
216 if (!(a_status & ZFCP_STATUS_COMMON_UNBLOCKED))
217 need = ZFCP_ERP_ACTION_REOPEN_ADAPTER;
218 /* fall through */
219 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
220 a_status = atomic_read(&adapter->status);
221 if (a_status & ZFCP_STATUS_COMMON_ERP_INUSE)
222 return 0;
223 if (!(a_status & ZFCP_STATUS_COMMON_RUNNING) &&
224 !(a_status & ZFCP_STATUS_COMMON_OPEN))
225 return 0; /* shutdown requested for closed adapter */
228 return need;
231 static struct zfcp_erp_action *zfcp_erp_setup_act(int need, u32 act_status,
232 struct zfcp_adapter *adapter,
233 struct zfcp_port *port,
234 struct scsi_device *sdev)
236 struct zfcp_erp_action *erp_action;
237 struct zfcp_scsi_dev *zfcp_sdev;
239 if (WARN_ON_ONCE(need != ZFCP_ERP_ACTION_REOPEN_LUN &&
240 need != ZFCP_ERP_ACTION_REOPEN_PORT &&
241 need != ZFCP_ERP_ACTION_REOPEN_PORT_FORCED &&
242 need != ZFCP_ERP_ACTION_REOPEN_ADAPTER))
243 return NULL;
245 switch (need) {
246 case ZFCP_ERP_ACTION_REOPEN_LUN:
247 zfcp_sdev = sdev_to_zfcp(sdev);
248 if (!(act_status & ZFCP_STATUS_ERP_NO_REF))
249 if (scsi_device_get(sdev))
250 return NULL;
251 atomic_or(ZFCP_STATUS_COMMON_ERP_INUSE,
252 &zfcp_sdev->status);
253 erp_action = &zfcp_sdev->erp_action;
254 WARN_ON_ONCE(erp_action->port != port);
255 WARN_ON_ONCE(erp_action->sdev != sdev);
256 if (!(atomic_read(&zfcp_sdev->status) &
257 ZFCP_STATUS_COMMON_RUNNING))
258 act_status |= ZFCP_STATUS_ERP_CLOSE_ONLY;
259 break;
261 case ZFCP_ERP_ACTION_REOPEN_PORT:
262 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
263 if (!get_device(&port->dev))
264 return NULL;
265 zfcp_erp_action_dismiss_port(port);
266 atomic_or(ZFCP_STATUS_COMMON_ERP_INUSE, &port->status);
267 erp_action = &port->erp_action;
268 WARN_ON_ONCE(erp_action->port != port);
269 WARN_ON_ONCE(erp_action->sdev != NULL);
270 if (!(atomic_read(&port->status) & ZFCP_STATUS_COMMON_RUNNING))
271 act_status |= ZFCP_STATUS_ERP_CLOSE_ONLY;
272 break;
274 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
275 kref_get(&adapter->ref);
276 zfcp_erp_action_dismiss_adapter(adapter);
277 atomic_or(ZFCP_STATUS_COMMON_ERP_INUSE, &adapter->status);
278 erp_action = &adapter->erp_action;
279 WARN_ON_ONCE(erp_action->port != NULL);
280 WARN_ON_ONCE(erp_action->sdev != NULL);
281 if (!(atomic_read(&adapter->status) &
282 ZFCP_STATUS_COMMON_RUNNING))
283 act_status |= ZFCP_STATUS_ERP_CLOSE_ONLY;
284 break;
286 default:
287 return NULL;
290 WARN_ON_ONCE(erp_action->adapter != adapter);
291 memset(&erp_action->list, 0, sizeof(erp_action->list));
292 memset(&erp_action->timer, 0, sizeof(erp_action->timer));
293 erp_action->step = ZFCP_ERP_STEP_UNINITIALIZED;
294 erp_action->fsf_req_id = 0;
295 erp_action->action = need;
296 erp_action->status = act_status;
298 return erp_action;
301 static void zfcp_erp_action_enqueue(int want, struct zfcp_adapter *adapter,
302 struct zfcp_port *port,
303 struct scsi_device *sdev,
304 char *id, u32 act_status)
306 int need;
307 struct zfcp_erp_action *act;
309 need = zfcp_erp_handle_failed(want, adapter, port, sdev);
310 if (!need) {
311 need = ZFCP_ERP_ACTION_FAILED; /* marker for trace */
312 goto out;
315 if (!adapter->erp_thread) {
316 need = ZFCP_ERP_ACTION_NONE; /* marker for trace */
317 goto out;
320 need = zfcp_erp_required_act(want, adapter, port, sdev);
321 if (!need)
322 goto out;
324 act = zfcp_erp_setup_act(need, act_status, adapter, port, sdev);
325 if (!act) {
326 need |= ZFCP_ERP_ACTION_NONE; /* marker for trace */
327 goto out;
329 atomic_or(ZFCP_STATUS_ADAPTER_ERP_PENDING, &adapter->status);
330 ++adapter->erp_total_count;
331 list_add_tail(&act->list, &adapter->erp_ready_head);
332 wake_up(&adapter->erp_ready_wq);
333 out:
334 zfcp_dbf_rec_trig(id, adapter, port, sdev, want, need);
337 void zfcp_erp_port_forced_no_port_dbf(char *id, struct zfcp_adapter *adapter,
338 u64 port_name, u32 port_id)
340 unsigned long flags;
341 static /* don't waste stack */ struct zfcp_port tmpport;
343 write_lock_irqsave(&adapter->erp_lock, flags);
344 /* Stand-in zfcp port with fields just good enough for
345 * zfcp_dbf_rec_trig() and zfcp_dbf_set_common().
346 * Under lock because tmpport is static.
348 atomic_set(&tmpport.status, -1); /* unknown */
349 tmpport.wwpn = port_name;
350 tmpport.d_id = port_id;
351 zfcp_dbf_rec_trig(id, adapter, &tmpport, NULL,
352 ZFCP_ERP_ACTION_REOPEN_PORT_FORCED,
353 ZFCP_ERP_ACTION_NONE);
354 write_unlock_irqrestore(&adapter->erp_lock, flags);
357 static void _zfcp_erp_adapter_reopen(struct zfcp_adapter *adapter,
358 int clear_mask, char *id)
360 zfcp_erp_adapter_block(adapter, clear_mask);
361 zfcp_scsi_schedule_rports_block(adapter);
363 zfcp_erp_action_enqueue(ZFCP_ERP_ACTION_REOPEN_ADAPTER,
364 adapter, NULL, NULL, id, 0);
368 * zfcp_erp_adapter_reopen - Reopen adapter.
369 * @adapter: Adapter to reopen.
370 * @clear: Status flags to clear.
371 * @id: Id for debug trace event.
373 void zfcp_erp_adapter_reopen(struct zfcp_adapter *adapter, int clear, char *id)
375 unsigned long flags;
377 zfcp_erp_adapter_block(adapter, clear);
378 zfcp_scsi_schedule_rports_block(adapter);
380 write_lock_irqsave(&adapter->erp_lock, flags);
381 zfcp_erp_action_enqueue(ZFCP_ERP_ACTION_REOPEN_ADAPTER, adapter,
382 NULL, NULL, id, 0);
383 write_unlock_irqrestore(&adapter->erp_lock, flags);
387 * zfcp_erp_adapter_shutdown - Shutdown adapter.
388 * @adapter: Adapter to shut down.
389 * @clear: Status flags to clear.
390 * @id: Id for debug trace event.
392 void zfcp_erp_adapter_shutdown(struct zfcp_adapter *adapter, int clear,
393 char *id)
395 int flags = ZFCP_STATUS_COMMON_RUNNING | ZFCP_STATUS_COMMON_ERP_FAILED;
396 zfcp_erp_adapter_reopen(adapter, clear | flags, id);
400 * zfcp_erp_port_shutdown - Shutdown port
401 * @port: Port to shut down.
402 * @clear: Status flags to clear.
403 * @id: Id for debug trace event.
405 void zfcp_erp_port_shutdown(struct zfcp_port *port, int clear, char *id)
407 int flags = ZFCP_STATUS_COMMON_RUNNING | ZFCP_STATUS_COMMON_ERP_FAILED;
408 zfcp_erp_port_reopen(port, clear | flags, id);
411 static void zfcp_erp_port_block(struct zfcp_port *port, int clear)
413 zfcp_erp_clear_port_status(port,
414 ZFCP_STATUS_COMMON_UNBLOCKED | clear);
417 static void _zfcp_erp_port_forced_reopen(struct zfcp_port *port, int clear,
418 char *id)
420 zfcp_erp_port_block(port, clear);
421 zfcp_scsi_schedule_rport_block(port);
423 zfcp_erp_action_enqueue(ZFCP_ERP_ACTION_REOPEN_PORT_FORCED,
424 port->adapter, port, NULL, id, 0);
428 * zfcp_erp_port_forced_reopen - Forced close of port and open again
429 * @port: Port to force close and to reopen.
430 * @clear: Status flags to clear.
431 * @id: Id for debug trace event.
433 void zfcp_erp_port_forced_reopen(struct zfcp_port *port, int clear, char *id)
435 unsigned long flags;
436 struct zfcp_adapter *adapter = port->adapter;
438 write_lock_irqsave(&adapter->erp_lock, flags);
439 _zfcp_erp_port_forced_reopen(port, clear, id);
440 write_unlock_irqrestore(&adapter->erp_lock, flags);
443 static void _zfcp_erp_port_reopen(struct zfcp_port *port, int clear, char *id)
445 zfcp_erp_port_block(port, clear);
446 zfcp_scsi_schedule_rport_block(port);
448 zfcp_erp_action_enqueue(ZFCP_ERP_ACTION_REOPEN_PORT,
449 port->adapter, port, NULL, id, 0);
453 * zfcp_erp_port_reopen - trigger remote port recovery
454 * @port: port to recover
455 * @clear_mask: flags in port status to be cleared
456 * @id: Id for debug trace event.
458 void zfcp_erp_port_reopen(struct zfcp_port *port, int clear, char *id)
460 unsigned long flags;
461 struct zfcp_adapter *adapter = port->adapter;
463 write_lock_irqsave(&adapter->erp_lock, flags);
464 _zfcp_erp_port_reopen(port, clear, id);
465 write_unlock_irqrestore(&adapter->erp_lock, flags);
468 static void zfcp_erp_lun_block(struct scsi_device *sdev, int clear_mask)
470 zfcp_erp_clear_lun_status(sdev,
471 ZFCP_STATUS_COMMON_UNBLOCKED | clear_mask);
474 static void _zfcp_erp_lun_reopen(struct scsi_device *sdev, int clear, char *id,
475 u32 act_status)
477 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
478 struct zfcp_adapter *adapter = zfcp_sdev->port->adapter;
480 zfcp_erp_lun_block(sdev, clear);
482 zfcp_erp_action_enqueue(ZFCP_ERP_ACTION_REOPEN_LUN, adapter,
483 zfcp_sdev->port, sdev, id, act_status);
487 * zfcp_erp_lun_reopen - initiate reopen of a LUN
488 * @sdev: SCSI device / LUN to be reopened
489 * @clear_mask: specifies flags in LUN status to be cleared
490 * @id: Id for debug trace event.
492 * Return: 0 on success, < 0 on error
494 void zfcp_erp_lun_reopen(struct scsi_device *sdev, int clear, char *id)
496 unsigned long flags;
497 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
498 struct zfcp_port *port = zfcp_sdev->port;
499 struct zfcp_adapter *adapter = port->adapter;
501 write_lock_irqsave(&adapter->erp_lock, flags);
502 _zfcp_erp_lun_reopen(sdev, clear, id, 0);
503 write_unlock_irqrestore(&adapter->erp_lock, flags);
507 * zfcp_erp_lun_shutdown - Shutdown LUN
508 * @sdev: SCSI device / LUN to shut down.
509 * @clear: Status flags to clear.
510 * @id: Id for debug trace event.
512 void zfcp_erp_lun_shutdown(struct scsi_device *sdev, int clear, char *id)
514 int flags = ZFCP_STATUS_COMMON_RUNNING | ZFCP_STATUS_COMMON_ERP_FAILED;
515 zfcp_erp_lun_reopen(sdev, clear | flags, id);
519 * zfcp_erp_lun_shutdown_wait - Shutdown LUN and wait for erp completion
520 * @sdev: SCSI device / LUN to shut down.
521 * @id: Id for debug trace event.
523 * Do not acquire a reference for the LUN when creating the ERP
524 * action. It is safe, because this function waits for the ERP to
525 * complete first. This allows to shutdown the LUN, even when the SCSI
526 * device is in the state SDEV_DEL when scsi_device_get will fail.
528 void zfcp_erp_lun_shutdown_wait(struct scsi_device *sdev, char *id)
530 unsigned long flags;
531 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
532 struct zfcp_port *port = zfcp_sdev->port;
533 struct zfcp_adapter *adapter = port->adapter;
534 int clear = ZFCP_STATUS_COMMON_RUNNING | ZFCP_STATUS_COMMON_ERP_FAILED;
536 write_lock_irqsave(&adapter->erp_lock, flags);
537 _zfcp_erp_lun_reopen(sdev, clear, id, ZFCP_STATUS_ERP_NO_REF);
538 write_unlock_irqrestore(&adapter->erp_lock, flags);
540 zfcp_erp_wait(adapter);
543 static int zfcp_erp_status_change_set(unsigned long mask, atomic_t *status)
545 return (atomic_read(status) ^ mask) & mask;
548 static void zfcp_erp_adapter_unblock(struct zfcp_adapter *adapter)
550 if (zfcp_erp_status_change_set(ZFCP_STATUS_COMMON_UNBLOCKED,
551 &adapter->status))
552 zfcp_dbf_rec_run("eraubl1", &adapter->erp_action);
553 atomic_or(ZFCP_STATUS_COMMON_UNBLOCKED, &adapter->status);
556 static void zfcp_erp_port_unblock(struct zfcp_port *port)
558 if (zfcp_erp_status_change_set(ZFCP_STATUS_COMMON_UNBLOCKED,
559 &port->status))
560 zfcp_dbf_rec_run("erpubl1", &port->erp_action);
561 atomic_or(ZFCP_STATUS_COMMON_UNBLOCKED, &port->status);
564 static void zfcp_erp_lun_unblock(struct scsi_device *sdev)
566 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
568 if (zfcp_erp_status_change_set(ZFCP_STATUS_COMMON_UNBLOCKED,
569 &zfcp_sdev->status))
570 zfcp_dbf_rec_run("erlubl1", &sdev_to_zfcp(sdev)->erp_action);
571 atomic_or(ZFCP_STATUS_COMMON_UNBLOCKED, &zfcp_sdev->status);
574 static void zfcp_erp_action_to_running(struct zfcp_erp_action *erp_action)
576 list_move(&erp_action->list, &erp_action->adapter->erp_running_head);
577 zfcp_dbf_rec_run("erator1", erp_action);
580 static void zfcp_erp_strategy_check_fsfreq(struct zfcp_erp_action *act)
582 struct zfcp_adapter *adapter = act->adapter;
583 struct zfcp_fsf_req *req;
585 if (!act->fsf_req_id)
586 return;
588 spin_lock(&adapter->req_list->lock);
589 req = _zfcp_reqlist_find(adapter->req_list, act->fsf_req_id);
590 if (req && req->erp_action == act) {
591 if (act->status & (ZFCP_STATUS_ERP_DISMISSED |
592 ZFCP_STATUS_ERP_TIMEDOUT)) {
593 req->status |= ZFCP_STATUS_FSFREQ_DISMISSED;
594 zfcp_dbf_rec_run("erscf_1", act);
595 /* lock-free concurrent access with
596 * zfcp_erp_timeout_handler()
598 WRITE_ONCE(req->erp_action, NULL);
600 if (act->status & ZFCP_STATUS_ERP_TIMEDOUT)
601 zfcp_dbf_rec_run("erscf_2", act);
602 if (req->status & ZFCP_STATUS_FSFREQ_DISMISSED)
603 act->fsf_req_id = 0;
604 } else
605 act->fsf_req_id = 0;
606 spin_unlock(&adapter->req_list->lock);
610 * zfcp_erp_notify - Trigger ERP action.
611 * @erp_action: ERP action to continue.
612 * @set_mask: ERP action status flags to set.
614 void zfcp_erp_notify(struct zfcp_erp_action *erp_action, unsigned long set_mask)
616 struct zfcp_adapter *adapter = erp_action->adapter;
617 unsigned long flags;
619 write_lock_irqsave(&adapter->erp_lock, flags);
620 if (zfcp_erp_action_is_running(erp_action)) {
621 erp_action->status |= set_mask;
622 zfcp_erp_action_ready(erp_action);
624 write_unlock_irqrestore(&adapter->erp_lock, flags);
628 * zfcp_erp_timeout_handler - Trigger ERP action from timed out ERP request
629 * @data: ERP action (from timer data)
631 void zfcp_erp_timeout_handler(struct timer_list *t)
633 struct zfcp_fsf_req *fsf_req = from_timer(fsf_req, t, timer);
634 struct zfcp_erp_action *act;
636 if (fsf_req->status & ZFCP_STATUS_FSFREQ_DISMISSED)
637 return;
638 /* lock-free concurrent access with zfcp_erp_strategy_check_fsfreq() */
639 act = READ_ONCE(fsf_req->erp_action);
640 if (!act)
641 return;
642 zfcp_erp_notify(act, ZFCP_STATUS_ERP_TIMEDOUT);
645 static void zfcp_erp_memwait_handler(struct timer_list *t)
647 struct zfcp_erp_action *act = from_timer(act, t, timer);
649 zfcp_erp_notify(act, 0);
652 static void zfcp_erp_strategy_memwait(struct zfcp_erp_action *erp_action)
654 timer_setup(&erp_action->timer, zfcp_erp_memwait_handler, 0);
655 erp_action->timer.expires = jiffies + HZ;
656 add_timer(&erp_action->timer);
659 void zfcp_erp_port_forced_reopen_all(struct zfcp_adapter *adapter,
660 int clear, char *dbftag)
662 unsigned long flags;
663 struct zfcp_port *port;
665 write_lock_irqsave(&adapter->erp_lock, flags);
666 read_lock(&adapter->port_list_lock);
667 list_for_each_entry(port, &adapter->port_list, list)
668 _zfcp_erp_port_forced_reopen(port, clear, dbftag);
669 read_unlock(&adapter->port_list_lock);
670 write_unlock_irqrestore(&adapter->erp_lock, flags);
673 static void _zfcp_erp_port_reopen_all(struct zfcp_adapter *adapter,
674 int clear, char *id)
676 struct zfcp_port *port;
678 read_lock(&adapter->port_list_lock);
679 list_for_each_entry(port, &adapter->port_list, list)
680 _zfcp_erp_port_reopen(port, clear, id);
681 read_unlock(&adapter->port_list_lock);
684 static void _zfcp_erp_lun_reopen_all(struct zfcp_port *port, int clear,
685 char *id)
687 struct scsi_device *sdev;
689 spin_lock(port->adapter->scsi_host->host_lock);
690 __shost_for_each_device(sdev, port->adapter->scsi_host)
691 if (sdev_to_zfcp(sdev)->port == port)
692 _zfcp_erp_lun_reopen(sdev, clear, id, 0);
693 spin_unlock(port->adapter->scsi_host->host_lock);
696 static void zfcp_erp_strategy_followup_failed(struct zfcp_erp_action *act)
698 switch (act->action) {
699 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
700 _zfcp_erp_adapter_reopen(act->adapter, 0, "ersff_1");
701 break;
702 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
703 _zfcp_erp_port_forced_reopen(act->port, 0, "ersff_2");
704 break;
705 case ZFCP_ERP_ACTION_REOPEN_PORT:
706 _zfcp_erp_port_reopen(act->port, 0, "ersff_3");
707 break;
708 case ZFCP_ERP_ACTION_REOPEN_LUN:
709 _zfcp_erp_lun_reopen(act->sdev, 0, "ersff_4", 0);
710 break;
714 static void zfcp_erp_strategy_followup_success(struct zfcp_erp_action *act)
716 switch (act->action) {
717 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
718 _zfcp_erp_port_reopen_all(act->adapter, 0, "ersfs_1");
719 break;
720 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
721 _zfcp_erp_port_reopen(act->port, 0, "ersfs_2");
722 break;
723 case ZFCP_ERP_ACTION_REOPEN_PORT:
724 _zfcp_erp_lun_reopen_all(act->port, 0, "ersfs_3");
725 break;
729 static void zfcp_erp_wakeup(struct zfcp_adapter *adapter)
731 unsigned long flags;
733 read_lock_irqsave(&adapter->erp_lock, flags);
734 if (list_empty(&adapter->erp_ready_head) &&
735 list_empty(&adapter->erp_running_head)) {
736 atomic_andnot(ZFCP_STATUS_ADAPTER_ERP_PENDING,
737 &adapter->status);
738 wake_up(&adapter->erp_done_wqh);
740 read_unlock_irqrestore(&adapter->erp_lock, flags);
743 static void zfcp_erp_enqueue_ptp_port(struct zfcp_adapter *adapter)
745 struct zfcp_port *port;
746 port = zfcp_port_enqueue(adapter, adapter->peer_wwpn, 0,
747 adapter->peer_d_id);
748 if (IS_ERR(port)) /* error or port already attached */
749 return;
750 zfcp_erp_port_reopen(port, 0, "ereptp1");
753 static int zfcp_erp_adapter_strat_fsf_xconf(struct zfcp_erp_action *erp_action)
755 int retries;
756 int sleep = 1;
757 struct zfcp_adapter *adapter = erp_action->adapter;
759 atomic_andnot(ZFCP_STATUS_ADAPTER_XCONFIG_OK, &adapter->status);
761 for (retries = 7; retries; retries--) {
762 atomic_andnot(ZFCP_STATUS_ADAPTER_HOST_CON_INIT,
763 &adapter->status);
764 write_lock_irq(&adapter->erp_lock);
765 zfcp_erp_action_to_running(erp_action);
766 write_unlock_irq(&adapter->erp_lock);
767 if (zfcp_fsf_exchange_config_data(erp_action)) {
768 atomic_andnot(ZFCP_STATUS_ADAPTER_HOST_CON_INIT,
769 &adapter->status);
770 return ZFCP_ERP_FAILED;
773 wait_event(adapter->erp_ready_wq,
774 !list_empty(&adapter->erp_ready_head));
775 if (erp_action->status & ZFCP_STATUS_ERP_TIMEDOUT)
776 break;
778 if (!(atomic_read(&adapter->status) &
779 ZFCP_STATUS_ADAPTER_HOST_CON_INIT))
780 break;
782 ssleep(sleep);
783 sleep *= 2;
786 atomic_andnot(ZFCP_STATUS_ADAPTER_HOST_CON_INIT,
787 &adapter->status);
789 if (!(atomic_read(&adapter->status) & ZFCP_STATUS_ADAPTER_XCONFIG_OK))
790 return ZFCP_ERP_FAILED;
792 if (fc_host_port_type(adapter->scsi_host) == FC_PORTTYPE_PTP)
793 zfcp_erp_enqueue_ptp_port(adapter);
795 return ZFCP_ERP_SUCCEEDED;
798 static int zfcp_erp_adapter_strategy_open_fsf_xport(struct zfcp_erp_action *act)
800 int ret;
801 struct zfcp_adapter *adapter = act->adapter;
803 write_lock_irq(&adapter->erp_lock);
804 zfcp_erp_action_to_running(act);
805 write_unlock_irq(&adapter->erp_lock);
807 ret = zfcp_fsf_exchange_port_data(act);
808 if (ret == -EOPNOTSUPP)
809 return ZFCP_ERP_SUCCEEDED;
810 if (ret)
811 return ZFCP_ERP_FAILED;
813 zfcp_dbf_rec_run("erasox1", act);
814 wait_event(adapter->erp_ready_wq,
815 !list_empty(&adapter->erp_ready_head));
816 zfcp_dbf_rec_run("erasox2", act);
817 if (act->status & ZFCP_STATUS_ERP_TIMEDOUT)
818 return ZFCP_ERP_FAILED;
820 return ZFCP_ERP_SUCCEEDED;
823 static int zfcp_erp_adapter_strategy_open_fsf(struct zfcp_erp_action *act)
825 if (zfcp_erp_adapter_strat_fsf_xconf(act) == ZFCP_ERP_FAILED)
826 return ZFCP_ERP_FAILED;
828 if (zfcp_erp_adapter_strategy_open_fsf_xport(act) == ZFCP_ERP_FAILED)
829 return ZFCP_ERP_FAILED;
831 if (mempool_resize(act->adapter->pool.sr_data,
832 act->adapter->stat_read_buf_num))
833 return ZFCP_ERP_FAILED;
835 if (mempool_resize(act->adapter->pool.status_read_req,
836 act->adapter->stat_read_buf_num))
837 return ZFCP_ERP_FAILED;
839 atomic_set(&act->adapter->stat_miss, act->adapter->stat_read_buf_num);
840 if (zfcp_status_read_refill(act->adapter))
841 return ZFCP_ERP_FAILED;
843 return ZFCP_ERP_SUCCEEDED;
846 static void zfcp_erp_adapter_strategy_close(struct zfcp_erp_action *act)
848 struct zfcp_adapter *adapter = act->adapter;
850 /* close queues to ensure that buffers are not accessed by adapter */
851 zfcp_qdio_close(adapter->qdio);
852 zfcp_fsf_req_dismiss_all(adapter);
853 adapter->fsf_req_seq_no = 0;
854 zfcp_fc_wka_ports_force_offline(adapter->gs);
855 /* all ports and LUNs are closed */
856 zfcp_erp_clear_adapter_status(adapter, ZFCP_STATUS_COMMON_OPEN);
858 atomic_andnot(ZFCP_STATUS_ADAPTER_XCONFIG_OK |
859 ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED, &adapter->status);
862 static int zfcp_erp_adapter_strategy_open(struct zfcp_erp_action *act)
864 struct zfcp_adapter *adapter = act->adapter;
866 if (zfcp_qdio_open(adapter->qdio)) {
867 atomic_andnot(ZFCP_STATUS_ADAPTER_XCONFIG_OK |
868 ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED,
869 &adapter->status);
870 return ZFCP_ERP_FAILED;
873 if (zfcp_erp_adapter_strategy_open_fsf(act)) {
874 zfcp_erp_adapter_strategy_close(act);
875 return ZFCP_ERP_FAILED;
878 atomic_or(ZFCP_STATUS_COMMON_OPEN, &adapter->status);
880 return ZFCP_ERP_SUCCEEDED;
883 static int zfcp_erp_adapter_strategy(struct zfcp_erp_action *act)
885 struct zfcp_adapter *adapter = act->adapter;
887 if (atomic_read(&adapter->status) & ZFCP_STATUS_COMMON_OPEN) {
888 zfcp_erp_adapter_strategy_close(act);
889 if (act->status & ZFCP_STATUS_ERP_CLOSE_ONLY)
890 return ZFCP_ERP_EXIT;
893 if (zfcp_erp_adapter_strategy_open(act)) {
894 ssleep(8);
895 return ZFCP_ERP_FAILED;
898 return ZFCP_ERP_SUCCEEDED;
901 static int zfcp_erp_port_forced_strategy_close(struct zfcp_erp_action *act)
903 int retval;
905 retval = zfcp_fsf_close_physical_port(act);
906 if (retval == -ENOMEM)
907 return ZFCP_ERP_NOMEM;
908 act->step = ZFCP_ERP_STEP_PHYS_PORT_CLOSING;
909 if (retval)
910 return ZFCP_ERP_FAILED;
912 return ZFCP_ERP_CONTINUES;
915 static int zfcp_erp_port_forced_strategy(struct zfcp_erp_action *erp_action)
917 struct zfcp_port *port = erp_action->port;
918 int status = atomic_read(&port->status);
920 switch (erp_action->step) {
921 case ZFCP_ERP_STEP_UNINITIALIZED:
922 if ((status & ZFCP_STATUS_PORT_PHYS_OPEN) &&
923 (status & ZFCP_STATUS_COMMON_OPEN))
924 return zfcp_erp_port_forced_strategy_close(erp_action);
925 else
926 return ZFCP_ERP_FAILED;
928 case ZFCP_ERP_STEP_PHYS_PORT_CLOSING:
929 if (!(status & ZFCP_STATUS_PORT_PHYS_OPEN))
930 return ZFCP_ERP_SUCCEEDED;
932 return ZFCP_ERP_FAILED;
935 static int zfcp_erp_port_strategy_close(struct zfcp_erp_action *erp_action)
937 int retval;
939 retval = zfcp_fsf_close_port(erp_action);
940 if (retval == -ENOMEM)
941 return ZFCP_ERP_NOMEM;
942 erp_action->step = ZFCP_ERP_STEP_PORT_CLOSING;
943 if (retval)
944 return ZFCP_ERP_FAILED;
945 return ZFCP_ERP_CONTINUES;
948 static int zfcp_erp_port_strategy_open_port(struct zfcp_erp_action *erp_action)
950 int retval;
952 retval = zfcp_fsf_open_port(erp_action);
953 if (retval == -ENOMEM)
954 return ZFCP_ERP_NOMEM;
955 erp_action->step = ZFCP_ERP_STEP_PORT_OPENING;
956 if (retval)
957 return ZFCP_ERP_FAILED;
958 return ZFCP_ERP_CONTINUES;
961 static int zfcp_erp_open_ptp_port(struct zfcp_erp_action *act)
963 struct zfcp_adapter *adapter = act->adapter;
964 struct zfcp_port *port = act->port;
966 if (port->wwpn != adapter->peer_wwpn) {
967 zfcp_erp_set_port_status(port, ZFCP_STATUS_COMMON_ERP_FAILED);
968 return ZFCP_ERP_FAILED;
970 port->d_id = adapter->peer_d_id;
971 return zfcp_erp_port_strategy_open_port(act);
974 static int zfcp_erp_port_strategy_open_common(struct zfcp_erp_action *act)
976 struct zfcp_adapter *adapter = act->adapter;
977 struct zfcp_port *port = act->port;
978 int p_status = atomic_read(&port->status);
980 switch (act->step) {
981 case ZFCP_ERP_STEP_UNINITIALIZED:
982 case ZFCP_ERP_STEP_PHYS_PORT_CLOSING:
983 case ZFCP_ERP_STEP_PORT_CLOSING:
984 if (fc_host_port_type(adapter->scsi_host) == FC_PORTTYPE_PTP)
985 return zfcp_erp_open_ptp_port(act);
986 if (!port->d_id) {
987 zfcp_fc_trigger_did_lookup(port);
988 return ZFCP_ERP_EXIT;
990 return zfcp_erp_port_strategy_open_port(act);
992 case ZFCP_ERP_STEP_PORT_OPENING:
993 /* D_ID might have changed during open */
994 if (p_status & ZFCP_STATUS_COMMON_OPEN) {
995 if (!port->d_id) {
996 zfcp_fc_trigger_did_lookup(port);
997 return ZFCP_ERP_EXIT;
999 return ZFCP_ERP_SUCCEEDED;
1001 if (port->d_id && !(p_status & ZFCP_STATUS_COMMON_NOESC)) {
1002 port->d_id = 0;
1003 return ZFCP_ERP_FAILED;
1005 /* fall through otherwise */
1007 return ZFCP_ERP_FAILED;
1010 static int zfcp_erp_port_strategy(struct zfcp_erp_action *erp_action)
1012 struct zfcp_port *port = erp_action->port;
1013 int p_status = atomic_read(&port->status);
1015 if ((p_status & ZFCP_STATUS_COMMON_NOESC) &&
1016 !(p_status & ZFCP_STATUS_COMMON_OPEN))
1017 goto close_init_done;
1019 switch (erp_action->step) {
1020 case ZFCP_ERP_STEP_UNINITIALIZED:
1021 if (p_status & ZFCP_STATUS_COMMON_OPEN)
1022 return zfcp_erp_port_strategy_close(erp_action);
1023 break;
1025 case ZFCP_ERP_STEP_PORT_CLOSING:
1026 if (p_status & ZFCP_STATUS_COMMON_OPEN)
1027 return ZFCP_ERP_FAILED;
1028 break;
1031 close_init_done:
1032 if (erp_action->status & ZFCP_STATUS_ERP_CLOSE_ONLY)
1033 return ZFCP_ERP_EXIT;
1035 return zfcp_erp_port_strategy_open_common(erp_action);
1038 static void zfcp_erp_lun_strategy_clearstati(struct scsi_device *sdev)
1040 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
1042 atomic_andnot(ZFCP_STATUS_COMMON_ACCESS_DENIED,
1043 &zfcp_sdev->status);
1046 static int zfcp_erp_lun_strategy_close(struct zfcp_erp_action *erp_action)
1048 int retval = zfcp_fsf_close_lun(erp_action);
1049 if (retval == -ENOMEM)
1050 return ZFCP_ERP_NOMEM;
1051 erp_action->step = ZFCP_ERP_STEP_LUN_CLOSING;
1052 if (retval)
1053 return ZFCP_ERP_FAILED;
1054 return ZFCP_ERP_CONTINUES;
1057 static int zfcp_erp_lun_strategy_open(struct zfcp_erp_action *erp_action)
1059 int retval = zfcp_fsf_open_lun(erp_action);
1060 if (retval == -ENOMEM)
1061 return ZFCP_ERP_NOMEM;
1062 erp_action->step = ZFCP_ERP_STEP_LUN_OPENING;
1063 if (retval)
1064 return ZFCP_ERP_FAILED;
1065 return ZFCP_ERP_CONTINUES;
1068 static int zfcp_erp_lun_strategy(struct zfcp_erp_action *erp_action)
1070 struct scsi_device *sdev = erp_action->sdev;
1071 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
1073 switch (erp_action->step) {
1074 case ZFCP_ERP_STEP_UNINITIALIZED:
1075 zfcp_erp_lun_strategy_clearstati(sdev);
1076 if (atomic_read(&zfcp_sdev->status) & ZFCP_STATUS_COMMON_OPEN)
1077 return zfcp_erp_lun_strategy_close(erp_action);
1078 /* already closed, fall through */
1079 case ZFCP_ERP_STEP_LUN_CLOSING:
1080 if (atomic_read(&zfcp_sdev->status) & ZFCP_STATUS_COMMON_OPEN)
1081 return ZFCP_ERP_FAILED;
1082 if (erp_action->status & ZFCP_STATUS_ERP_CLOSE_ONLY)
1083 return ZFCP_ERP_EXIT;
1084 return zfcp_erp_lun_strategy_open(erp_action);
1086 case ZFCP_ERP_STEP_LUN_OPENING:
1087 if (atomic_read(&zfcp_sdev->status) & ZFCP_STATUS_COMMON_OPEN)
1088 return ZFCP_ERP_SUCCEEDED;
1090 return ZFCP_ERP_FAILED;
1093 static int zfcp_erp_strategy_check_lun(struct scsi_device *sdev, int result)
1095 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
1097 switch (result) {
1098 case ZFCP_ERP_SUCCEEDED :
1099 atomic_set(&zfcp_sdev->erp_counter, 0);
1100 zfcp_erp_lun_unblock(sdev);
1101 break;
1102 case ZFCP_ERP_FAILED :
1103 atomic_inc(&zfcp_sdev->erp_counter);
1104 if (atomic_read(&zfcp_sdev->erp_counter) > ZFCP_MAX_ERPS) {
1105 dev_err(&zfcp_sdev->port->adapter->ccw_device->dev,
1106 "ERP failed for LUN 0x%016Lx on "
1107 "port 0x%016Lx\n",
1108 (unsigned long long)zfcp_scsi_dev_lun(sdev),
1109 (unsigned long long)zfcp_sdev->port->wwpn);
1110 zfcp_erp_set_lun_status(sdev,
1111 ZFCP_STATUS_COMMON_ERP_FAILED);
1113 break;
1116 if (atomic_read(&zfcp_sdev->status) & ZFCP_STATUS_COMMON_ERP_FAILED) {
1117 zfcp_erp_lun_block(sdev, 0);
1118 result = ZFCP_ERP_EXIT;
1120 return result;
1123 static int zfcp_erp_strategy_check_port(struct zfcp_port *port, int result)
1125 switch (result) {
1126 case ZFCP_ERP_SUCCEEDED :
1127 atomic_set(&port->erp_counter, 0);
1128 zfcp_erp_port_unblock(port);
1129 break;
1131 case ZFCP_ERP_FAILED :
1132 if (atomic_read(&port->status) & ZFCP_STATUS_COMMON_NOESC) {
1133 zfcp_erp_port_block(port, 0);
1134 result = ZFCP_ERP_EXIT;
1136 atomic_inc(&port->erp_counter);
1137 if (atomic_read(&port->erp_counter) > ZFCP_MAX_ERPS) {
1138 dev_err(&port->adapter->ccw_device->dev,
1139 "ERP failed for remote port 0x%016Lx\n",
1140 (unsigned long long)port->wwpn);
1141 zfcp_erp_set_port_status(port,
1142 ZFCP_STATUS_COMMON_ERP_FAILED);
1144 break;
1147 if (atomic_read(&port->status) & ZFCP_STATUS_COMMON_ERP_FAILED) {
1148 zfcp_erp_port_block(port, 0);
1149 result = ZFCP_ERP_EXIT;
1151 return result;
1154 static int zfcp_erp_strategy_check_adapter(struct zfcp_adapter *adapter,
1155 int result)
1157 switch (result) {
1158 case ZFCP_ERP_SUCCEEDED :
1159 atomic_set(&adapter->erp_counter, 0);
1160 zfcp_erp_adapter_unblock(adapter);
1161 break;
1163 case ZFCP_ERP_FAILED :
1164 atomic_inc(&adapter->erp_counter);
1165 if (atomic_read(&adapter->erp_counter) > ZFCP_MAX_ERPS) {
1166 dev_err(&adapter->ccw_device->dev,
1167 "ERP cannot recover an error "
1168 "on the FCP device\n");
1169 zfcp_erp_set_adapter_status(adapter,
1170 ZFCP_STATUS_COMMON_ERP_FAILED);
1172 break;
1175 if (atomic_read(&adapter->status) & ZFCP_STATUS_COMMON_ERP_FAILED) {
1176 zfcp_erp_adapter_block(adapter, 0);
1177 result = ZFCP_ERP_EXIT;
1179 return result;
1182 static int zfcp_erp_strategy_check_target(struct zfcp_erp_action *erp_action,
1183 int result)
1185 struct zfcp_adapter *adapter = erp_action->adapter;
1186 struct zfcp_port *port = erp_action->port;
1187 struct scsi_device *sdev = erp_action->sdev;
1189 switch (erp_action->action) {
1191 case ZFCP_ERP_ACTION_REOPEN_LUN:
1192 result = zfcp_erp_strategy_check_lun(sdev, result);
1193 break;
1195 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
1196 case ZFCP_ERP_ACTION_REOPEN_PORT:
1197 result = zfcp_erp_strategy_check_port(port, result);
1198 break;
1200 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
1201 result = zfcp_erp_strategy_check_adapter(adapter, result);
1202 break;
1204 return result;
1207 static int zfcp_erp_strat_change_det(atomic_t *target_status, u32 erp_status)
1209 int status = atomic_read(target_status);
1211 if ((status & ZFCP_STATUS_COMMON_RUNNING) &&
1212 (erp_status & ZFCP_STATUS_ERP_CLOSE_ONLY))
1213 return 1; /* take it online */
1215 if (!(status & ZFCP_STATUS_COMMON_RUNNING) &&
1216 !(erp_status & ZFCP_STATUS_ERP_CLOSE_ONLY))
1217 return 1; /* take it offline */
1219 return 0;
1222 static int zfcp_erp_strategy_statechange(struct zfcp_erp_action *act, int ret)
1224 int action = act->action;
1225 struct zfcp_adapter *adapter = act->adapter;
1226 struct zfcp_port *port = act->port;
1227 struct scsi_device *sdev = act->sdev;
1228 struct zfcp_scsi_dev *zfcp_sdev;
1229 u32 erp_status = act->status;
1231 switch (action) {
1232 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
1233 if (zfcp_erp_strat_change_det(&adapter->status, erp_status)) {
1234 _zfcp_erp_adapter_reopen(adapter,
1235 ZFCP_STATUS_COMMON_ERP_FAILED,
1236 "ersscg1");
1237 return ZFCP_ERP_EXIT;
1239 break;
1241 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
1242 case ZFCP_ERP_ACTION_REOPEN_PORT:
1243 if (zfcp_erp_strat_change_det(&port->status, erp_status)) {
1244 _zfcp_erp_port_reopen(port,
1245 ZFCP_STATUS_COMMON_ERP_FAILED,
1246 "ersscg2");
1247 return ZFCP_ERP_EXIT;
1249 break;
1251 case ZFCP_ERP_ACTION_REOPEN_LUN:
1252 zfcp_sdev = sdev_to_zfcp(sdev);
1253 if (zfcp_erp_strat_change_det(&zfcp_sdev->status, erp_status)) {
1254 _zfcp_erp_lun_reopen(sdev,
1255 ZFCP_STATUS_COMMON_ERP_FAILED,
1256 "ersscg3", 0);
1257 return ZFCP_ERP_EXIT;
1259 break;
1261 return ret;
1264 static void zfcp_erp_action_dequeue(struct zfcp_erp_action *erp_action)
1266 struct zfcp_adapter *adapter = erp_action->adapter;
1267 struct zfcp_scsi_dev *zfcp_sdev;
1269 adapter->erp_total_count--;
1270 if (erp_action->status & ZFCP_STATUS_ERP_LOWMEM) {
1271 adapter->erp_low_mem_count--;
1272 erp_action->status &= ~ZFCP_STATUS_ERP_LOWMEM;
1275 list_del(&erp_action->list);
1276 zfcp_dbf_rec_run("eractd1", erp_action);
1278 switch (erp_action->action) {
1279 case ZFCP_ERP_ACTION_REOPEN_LUN:
1280 zfcp_sdev = sdev_to_zfcp(erp_action->sdev);
1281 atomic_andnot(ZFCP_STATUS_COMMON_ERP_INUSE,
1282 &zfcp_sdev->status);
1283 break;
1285 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
1286 case ZFCP_ERP_ACTION_REOPEN_PORT:
1287 atomic_andnot(ZFCP_STATUS_COMMON_ERP_INUSE,
1288 &erp_action->port->status);
1289 break;
1291 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
1292 atomic_andnot(ZFCP_STATUS_COMMON_ERP_INUSE,
1293 &erp_action->adapter->status);
1294 break;
1299 * zfcp_erp_try_rport_unblock - unblock rport if no more/new recovery
1300 * @port: zfcp_port whose fc_rport we should try to unblock
1302 static void zfcp_erp_try_rport_unblock(struct zfcp_port *port)
1304 unsigned long flags;
1305 struct zfcp_adapter *adapter = port->adapter;
1306 int port_status;
1307 struct Scsi_Host *shost = adapter->scsi_host;
1308 struct scsi_device *sdev;
1310 write_lock_irqsave(&adapter->erp_lock, flags);
1311 port_status = atomic_read(&port->status);
1312 if ((port_status & ZFCP_STATUS_COMMON_UNBLOCKED) == 0 ||
1313 (port_status & (ZFCP_STATUS_COMMON_ERP_INUSE |
1314 ZFCP_STATUS_COMMON_ERP_FAILED)) != 0) {
1315 /* new ERP of severity >= port triggered elsewhere meanwhile or
1316 * local link down (adapter erp_failed but not clear unblock)
1318 zfcp_dbf_rec_run_lvl(4, "ertru_p", &port->erp_action);
1319 write_unlock_irqrestore(&adapter->erp_lock, flags);
1320 return;
1322 spin_lock(shost->host_lock);
1323 __shost_for_each_device(sdev, shost) {
1324 struct zfcp_scsi_dev *zsdev = sdev_to_zfcp(sdev);
1325 int lun_status;
1327 if (sdev->sdev_state == SDEV_DEL ||
1328 sdev->sdev_state == SDEV_CANCEL)
1329 continue;
1330 if (zsdev->port != port)
1331 continue;
1332 /* LUN under port of interest */
1333 lun_status = atomic_read(&zsdev->status);
1334 if ((lun_status & ZFCP_STATUS_COMMON_ERP_FAILED) != 0)
1335 continue; /* unblock rport despite failed LUNs */
1336 /* LUN recovery not given up yet [maybe follow-up pending] */
1337 if ((lun_status & ZFCP_STATUS_COMMON_UNBLOCKED) == 0 ||
1338 (lun_status & ZFCP_STATUS_COMMON_ERP_INUSE) != 0) {
1339 /* LUN blocked:
1340 * not yet unblocked [LUN recovery pending]
1341 * or meanwhile blocked [new LUN recovery triggered]
1343 zfcp_dbf_rec_run_lvl(4, "ertru_l", &zsdev->erp_action);
1344 spin_unlock(shost->host_lock);
1345 write_unlock_irqrestore(&adapter->erp_lock, flags);
1346 return;
1349 /* now port has no child or all children have completed recovery,
1350 * and no ERP of severity >= port was meanwhile triggered elsewhere
1352 zfcp_scsi_schedule_rport_register(port);
1353 spin_unlock(shost->host_lock);
1354 write_unlock_irqrestore(&adapter->erp_lock, flags);
1357 static void zfcp_erp_action_cleanup(struct zfcp_erp_action *act, int result)
1359 struct zfcp_adapter *adapter = act->adapter;
1360 struct zfcp_port *port = act->port;
1361 struct scsi_device *sdev = act->sdev;
1363 switch (act->action) {
1364 case ZFCP_ERP_ACTION_REOPEN_LUN:
1365 if (!(act->status & ZFCP_STATUS_ERP_NO_REF))
1366 scsi_device_put(sdev);
1367 zfcp_erp_try_rport_unblock(port);
1368 break;
1370 case ZFCP_ERP_ACTION_REOPEN_PORT:
1371 /* This switch case might also happen after a forced reopen
1372 * was successfully done and thus overwritten with a new
1373 * non-forced reopen at `ersfs_2'. In this case, we must not
1374 * do the clean-up of the non-forced version.
1376 if (act->step != ZFCP_ERP_STEP_UNINITIALIZED)
1377 if (result == ZFCP_ERP_SUCCEEDED)
1378 zfcp_erp_try_rport_unblock(port);
1379 /* fall through */
1380 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
1381 put_device(&port->dev);
1382 break;
1384 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
1385 if (result == ZFCP_ERP_SUCCEEDED) {
1386 register_service_level(&adapter->service_level);
1387 zfcp_fc_conditional_port_scan(adapter);
1388 queue_work(adapter->work_queue, &adapter->ns_up_work);
1389 } else
1390 unregister_service_level(&adapter->service_level);
1392 kref_put(&adapter->ref, zfcp_adapter_release);
1393 break;
1397 static int zfcp_erp_strategy_do_action(struct zfcp_erp_action *erp_action)
1399 switch (erp_action->action) {
1400 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
1401 return zfcp_erp_adapter_strategy(erp_action);
1402 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
1403 return zfcp_erp_port_forced_strategy(erp_action);
1404 case ZFCP_ERP_ACTION_REOPEN_PORT:
1405 return zfcp_erp_port_strategy(erp_action);
1406 case ZFCP_ERP_ACTION_REOPEN_LUN:
1407 return zfcp_erp_lun_strategy(erp_action);
1409 return ZFCP_ERP_FAILED;
1412 static int zfcp_erp_strategy(struct zfcp_erp_action *erp_action)
1414 int retval;
1415 unsigned long flags;
1416 struct zfcp_adapter *adapter = erp_action->adapter;
1418 kref_get(&adapter->ref);
1420 write_lock_irqsave(&adapter->erp_lock, flags);
1421 zfcp_erp_strategy_check_fsfreq(erp_action);
1423 if (erp_action->status & ZFCP_STATUS_ERP_DISMISSED) {
1424 zfcp_erp_action_dequeue(erp_action);
1425 retval = ZFCP_ERP_DISMISSED;
1426 goto unlock;
1429 if (erp_action->status & ZFCP_STATUS_ERP_TIMEDOUT) {
1430 retval = ZFCP_ERP_FAILED;
1431 goto check_target;
1434 zfcp_erp_action_to_running(erp_action);
1436 /* no lock to allow for blocking operations */
1437 write_unlock_irqrestore(&adapter->erp_lock, flags);
1438 retval = zfcp_erp_strategy_do_action(erp_action);
1439 write_lock_irqsave(&adapter->erp_lock, flags);
1441 if (erp_action->status & ZFCP_STATUS_ERP_DISMISSED)
1442 retval = ZFCP_ERP_CONTINUES;
1444 switch (retval) {
1445 case ZFCP_ERP_NOMEM:
1446 if (!(erp_action->status & ZFCP_STATUS_ERP_LOWMEM)) {
1447 ++adapter->erp_low_mem_count;
1448 erp_action->status |= ZFCP_STATUS_ERP_LOWMEM;
1450 if (adapter->erp_total_count == adapter->erp_low_mem_count)
1451 _zfcp_erp_adapter_reopen(adapter, 0, "erstgy1");
1452 else {
1453 zfcp_erp_strategy_memwait(erp_action);
1454 retval = ZFCP_ERP_CONTINUES;
1456 goto unlock;
1458 case ZFCP_ERP_CONTINUES:
1459 if (erp_action->status & ZFCP_STATUS_ERP_LOWMEM) {
1460 --adapter->erp_low_mem_count;
1461 erp_action->status &= ~ZFCP_STATUS_ERP_LOWMEM;
1463 goto unlock;
1466 check_target:
1467 retval = zfcp_erp_strategy_check_target(erp_action, retval);
1468 zfcp_erp_action_dequeue(erp_action);
1469 retval = zfcp_erp_strategy_statechange(erp_action, retval);
1470 if (retval == ZFCP_ERP_EXIT)
1471 goto unlock;
1472 if (retval == ZFCP_ERP_SUCCEEDED)
1473 zfcp_erp_strategy_followup_success(erp_action);
1474 if (retval == ZFCP_ERP_FAILED)
1475 zfcp_erp_strategy_followup_failed(erp_action);
1477 unlock:
1478 write_unlock_irqrestore(&adapter->erp_lock, flags);
1480 if (retval != ZFCP_ERP_CONTINUES)
1481 zfcp_erp_action_cleanup(erp_action, retval);
1483 kref_put(&adapter->ref, zfcp_adapter_release);
1484 return retval;
1487 static int zfcp_erp_thread(void *data)
1489 struct zfcp_adapter *adapter = (struct zfcp_adapter *) data;
1490 struct list_head *next;
1491 struct zfcp_erp_action *act;
1492 unsigned long flags;
1494 for (;;) {
1495 wait_event_interruptible(adapter->erp_ready_wq,
1496 !list_empty(&adapter->erp_ready_head) ||
1497 kthread_should_stop());
1499 if (kthread_should_stop())
1500 break;
1502 write_lock_irqsave(&adapter->erp_lock, flags);
1503 next = adapter->erp_ready_head.next;
1504 write_unlock_irqrestore(&adapter->erp_lock, flags);
1506 if (next != &adapter->erp_ready_head) {
1507 act = list_entry(next, struct zfcp_erp_action, list);
1509 /* there is more to come after dismission, no notify */
1510 if (zfcp_erp_strategy(act) != ZFCP_ERP_DISMISSED)
1511 zfcp_erp_wakeup(adapter);
1515 return 0;
1519 * zfcp_erp_thread_setup - Start ERP thread for adapter
1520 * @adapter: Adapter to start the ERP thread for
1522 * Returns 0 on success or error code from kernel_thread()
1524 int zfcp_erp_thread_setup(struct zfcp_adapter *adapter)
1526 struct task_struct *thread;
1528 thread = kthread_run(zfcp_erp_thread, adapter, "zfcperp%s",
1529 dev_name(&adapter->ccw_device->dev));
1530 if (IS_ERR(thread)) {
1531 dev_err(&adapter->ccw_device->dev,
1532 "Creating an ERP thread for the FCP device failed.\n");
1533 return PTR_ERR(thread);
1536 adapter->erp_thread = thread;
1537 return 0;
1541 * zfcp_erp_thread_kill - Stop ERP thread.
1542 * @adapter: Adapter where the ERP thread should be stopped.
1544 * The caller of this routine ensures that the specified adapter has
1545 * been shut down and that this operation has been completed. Thus,
1546 * there are no pending erp_actions which would need to be handled
1547 * here.
1549 void zfcp_erp_thread_kill(struct zfcp_adapter *adapter)
1551 kthread_stop(adapter->erp_thread);
1552 adapter->erp_thread = NULL;
1553 WARN_ON(!list_empty(&adapter->erp_ready_head));
1554 WARN_ON(!list_empty(&adapter->erp_running_head));
1558 * zfcp_erp_wait - wait for completion of error recovery on an adapter
1559 * @adapter: adapter for which to wait for completion of its error recovery
1561 void zfcp_erp_wait(struct zfcp_adapter *adapter)
1563 wait_event(adapter->erp_done_wqh,
1564 !(atomic_read(&adapter->status) &
1565 ZFCP_STATUS_ADAPTER_ERP_PENDING));
1569 * zfcp_erp_set_adapter_status - set adapter status bits
1570 * @adapter: adapter to change the status
1571 * @mask: status bits to change
1573 * Changes in common status bits are propagated to attached ports and LUNs.
1575 void zfcp_erp_set_adapter_status(struct zfcp_adapter *adapter, u32 mask)
1577 struct zfcp_port *port;
1578 struct scsi_device *sdev;
1579 unsigned long flags;
1580 u32 common_mask = mask & ZFCP_COMMON_FLAGS;
1582 atomic_or(mask, &adapter->status);
1584 if (!common_mask)
1585 return;
1587 read_lock_irqsave(&adapter->port_list_lock, flags);
1588 list_for_each_entry(port, &adapter->port_list, list)
1589 atomic_or(common_mask, &port->status);
1590 read_unlock_irqrestore(&adapter->port_list_lock, flags);
1592 spin_lock_irqsave(adapter->scsi_host->host_lock, flags);
1593 __shost_for_each_device(sdev, adapter->scsi_host)
1594 atomic_or(common_mask, &sdev_to_zfcp(sdev)->status);
1595 spin_unlock_irqrestore(adapter->scsi_host->host_lock, flags);
1599 * zfcp_erp_clear_adapter_status - clear adapter status bits
1600 * @adapter: adapter to change the status
1601 * @mask: status bits to change
1603 * Changes in common status bits are propagated to attached ports and LUNs.
1605 void zfcp_erp_clear_adapter_status(struct zfcp_adapter *adapter, u32 mask)
1607 struct zfcp_port *port;
1608 struct scsi_device *sdev;
1609 unsigned long flags;
1610 u32 common_mask = mask & ZFCP_COMMON_FLAGS;
1611 u32 clear_counter = mask & ZFCP_STATUS_COMMON_ERP_FAILED;
1613 atomic_andnot(mask, &adapter->status);
1615 if (!common_mask)
1616 return;
1618 if (clear_counter)
1619 atomic_set(&adapter->erp_counter, 0);
1621 read_lock_irqsave(&adapter->port_list_lock, flags);
1622 list_for_each_entry(port, &adapter->port_list, list) {
1623 atomic_andnot(common_mask, &port->status);
1624 if (clear_counter)
1625 atomic_set(&port->erp_counter, 0);
1627 read_unlock_irqrestore(&adapter->port_list_lock, flags);
1629 spin_lock_irqsave(adapter->scsi_host->host_lock, flags);
1630 __shost_for_each_device(sdev, adapter->scsi_host) {
1631 atomic_andnot(common_mask, &sdev_to_zfcp(sdev)->status);
1632 if (clear_counter)
1633 atomic_set(&sdev_to_zfcp(sdev)->erp_counter, 0);
1635 spin_unlock_irqrestore(adapter->scsi_host->host_lock, flags);
1639 * zfcp_erp_set_port_status - set port status bits
1640 * @port: port to change the status
1641 * @mask: status bits to change
1643 * Changes in common status bits are propagated to attached LUNs.
1645 void zfcp_erp_set_port_status(struct zfcp_port *port, u32 mask)
1647 struct scsi_device *sdev;
1648 u32 common_mask = mask & ZFCP_COMMON_FLAGS;
1649 unsigned long flags;
1651 atomic_or(mask, &port->status);
1653 if (!common_mask)
1654 return;
1656 spin_lock_irqsave(port->adapter->scsi_host->host_lock, flags);
1657 __shost_for_each_device(sdev, port->adapter->scsi_host)
1658 if (sdev_to_zfcp(sdev)->port == port)
1659 atomic_or(common_mask,
1660 &sdev_to_zfcp(sdev)->status);
1661 spin_unlock_irqrestore(port->adapter->scsi_host->host_lock, flags);
1665 * zfcp_erp_clear_port_status - clear port status bits
1666 * @port: adapter to change the status
1667 * @mask: status bits to change
1669 * Changes in common status bits are propagated to attached LUNs.
1671 void zfcp_erp_clear_port_status(struct zfcp_port *port, u32 mask)
1673 struct scsi_device *sdev;
1674 u32 common_mask = mask & ZFCP_COMMON_FLAGS;
1675 u32 clear_counter = mask & ZFCP_STATUS_COMMON_ERP_FAILED;
1676 unsigned long flags;
1678 atomic_andnot(mask, &port->status);
1680 if (!common_mask)
1681 return;
1683 if (clear_counter)
1684 atomic_set(&port->erp_counter, 0);
1686 spin_lock_irqsave(port->adapter->scsi_host->host_lock, flags);
1687 __shost_for_each_device(sdev, port->adapter->scsi_host)
1688 if (sdev_to_zfcp(sdev)->port == port) {
1689 atomic_andnot(common_mask,
1690 &sdev_to_zfcp(sdev)->status);
1691 if (clear_counter)
1692 atomic_set(&sdev_to_zfcp(sdev)->erp_counter, 0);
1694 spin_unlock_irqrestore(port->adapter->scsi_host->host_lock, flags);
1698 * zfcp_erp_set_lun_status - set lun status bits
1699 * @sdev: SCSI device / lun to set the status bits
1700 * @mask: status bits to change
1702 void zfcp_erp_set_lun_status(struct scsi_device *sdev, u32 mask)
1704 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
1706 atomic_or(mask, &zfcp_sdev->status);
1710 * zfcp_erp_clear_lun_status - clear lun status bits
1711 * @sdev: SCSi device / lun to clear the status bits
1712 * @mask: status bits to change
1714 void zfcp_erp_clear_lun_status(struct scsi_device *sdev, u32 mask)
1716 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
1718 atomic_andnot(mask, &zfcp_sdev->status);
1720 if (mask & ZFCP_STATUS_COMMON_ERP_FAILED)
1721 atomic_set(&zfcp_sdev->erp_counter, 0);
1725 * zfcp_erp_adapter_reset_sync() - Really reopen adapter and wait.
1726 * @adapter: Pointer to zfcp_adapter to reopen.
1727 * @id: Trace tag string of length %ZFCP_DBF_TAG_LEN.
1729 void zfcp_erp_adapter_reset_sync(struct zfcp_adapter *adapter, char *id)
1731 zfcp_erp_set_adapter_status(adapter, ZFCP_STATUS_COMMON_RUNNING);
1732 zfcp_erp_adapter_reopen(adapter, ZFCP_STATUS_COMMON_ERP_FAILED, id);
1733 zfcp_erp_wait(adapter);