gpio: adp5588-gpio: support interrupt controller
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / s390 / scsi / zfcp_erp.c
blobd37c7331f244f1463c154a05ec9649306fe0c358
1 /*
2 * zfcp device driver
4 * Error Recovery Procedures (ERP).
6 * Copyright IBM Corporation 2002, 2010
7 */
9 #define KMSG_COMPONENT "zfcp"
10 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
12 #include <linux/kthread.h>
13 #include "zfcp_ext.h"
14 #include "zfcp_reqlist.h"
16 #define ZFCP_MAX_ERPS 3
18 enum 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,
24 ZFCP_STATUS_ERP_NO_REF = 0x00800000,
27 enum 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,
32 ZFCP_ERP_STEP_PORT_OPENING = 0x0800,
33 ZFCP_ERP_STEP_LUN_CLOSING = 0x1000,
34 ZFCP_ERP_STEP_LUN_OPENING = 0x2000,
37 enum zfcp_erp_act_type {
38 ZFCP_ERP_ACTION_REOPEN_LUN = 1,
39 ZFCP_ERP_ACTION_REOPEN_PORT = 2,
40 ZFCP_ERP_ACTION_REOPEN_PORT_FORCED = 3,
41 ZFCP_ERP_ACTION_REOPEN_ADAPTER = 4,
44 enum zfcp_erp_act_state {
45 ZFCP_ERP_ACTION_RUNNING = 1,
46 ZFCP_ERP_ACTION_READY = 2,
49 enum 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,
58 static void zfcp_erp_adapter_block(struct zfcp_adapter *adapter, int mask)
60 zfcp_erp_clear_adapter_status(adapter,
61 ZFCP_STATUS_COMMON_UNBLOCKED | mask);
64 static int zfcp_erp_action_exists(struct zfcp_erp_action *act)
66 struct zfcp_erp_action *curr_act;
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;
74 static void zfcp_erp_action_ready(struct zfcp_erp_action *act)
76 struct zfcp_adapter *adapter = act->adapter;
78 list_move(&act->list, &act->adapter->erp_ready_head);
79 zfcp_dbf_rec_action("erardy1", act);
80 wake_up(&adapter->erp_ready_wq);
81 zfcp_dbf_rec_thread("erardy2", adapter->dbf);
84 static void zfcp_erp_action_dismiss(struct zfcp_erp_action *act)
86 act->status |= ZFCP_STATUS_ERP_DISMISSED;
87 if (zfcp_erp_action_exists(act) == ZFCP_ERP_ACTION_RUNNING)
88 zfcp_erp_action_ready(act);
91 static void zfcp_erp_action_dismiss_lun(struct scsi_device *sdev)
93 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
95 if (atomic_read(&zfcp_sdev->status) & ZFCP_STATUS_COMMON_ERP_INUSE)
96 zfcp_erp_action_dismiss(&zfcp_sdev->erp_action);
99 static void zfcp_erp_action_dismiss_port(struct zfcp_port *port)
101 struct scsi_device *sdev;
103 if (atomic_read(&port->status) & ZFCP_STATUS_COMMON_ERP_INUSE)
104 zfcp_erp_action_dismiss(&port->erp_action);
105 else
106 shost_for_each_device(sdev, port->adapter->scsi_host)
107 if (sdev_to_zfcp(sdev)->port == port)
108 zfcp_erp_action_dismiss_lun(sdev);
111 static void zfcp_erp_action_dismiss_adapter(struct zfcp_adapter *adapter)
113 struct zfcp_port *port;
115 if (atomic_read(&adapter->status) & ZFCP_STATUS_COMMON_ERP_INUSE)
116 zfcp_erp_action_dismiss(&adapter->erp_action);
117 else {
118 read_lock(&adapter->port_list_lock);
119 list_for_each_entry(port, &adapter->port_list, list)
120 zfcp_erp_action_dismiss_port(port);
121 read_unlock(&adapter->port_list_lock);
125 static int zfcp_erp_required_act(int want, struct zfcp_adapter *adapter,
126 struct zfcp_port *port,
127 struct scsi_device *sdev)
129 int need = want;
130 int l_status, p_status, a_status;
131 struct zfcp_scsi_dev *zfcp_sdev;
133 switch (want) {
134 case ZFCP_ERP_ACTION_REOPEN_LUN:
135 zfcp_sdev = sdev_to_zfcp(sdev);
136 l_status = atomic_read(&zfcp_sdev->status);
137 if (l_status & ZFCP_STATUS_COMMON_ERP_INUSE)
138 return 0;
139 p_status = atomic_read(&port->status);
140 if (!(p_status & ZFCP_STATUS_COMMON_RUNNING) ||
141 p_status & ZFCP_STATUS_COMMON_ERP_FAILED)
142 return 0;
143 if (!(p_status & ZFCP_STATUS_COMMON_UNBLOCKED))
144 need = ZFCP_ERP_ACTION_REOPEN_PORT;
145 /* fall through */
146 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
147 p_status = atomic_read(&port->status);
148 if (!(p_status & ZFCP_STATUS_COMMON_OPEN))
149 need = ZFCP_ERP_ACTION_REOPEN_PORT;
150 /* fall through */
151 case ZFCP_ERP_ACTION_REOPEN_PORT:
152 p_status = atomic_read(&port->status);
153 if (p_status & ZFCP_STATUS_COMMON_ERP_INUSE)
154 return 0;
155 a_status = atomic_read(&adapter->status);
156 if (!(a_status & ZFCP_STATUS_COMMON_RUNNING) ||
157 a_status & ZFCP_STATUS_COMMON_ERP_FAILED)
158 return 0;
159 if (!(a_status & ZFCP_STATUS_COMMON_UNBLOCKED))
160 need = ZFCP_ERP_ACTION_REOPEN_ADAPTER;
161 /* fall through */
162 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
163 a_status = atomic_read(&adapter->status);
164 if (a_status & ZFCP_STATUS_COMMON_ERP_INUSE)
165 return 0;
166 if (!(a_status & ZFCP_STATUS_COMMON_RUNNING) &&
167 !(a_status & ZFCP_STATUS_COMMON_OPEN))
168 return 0; /* shutdown requested for closed adapter */
171 return need;
174 static struct zfcp_erp_action *zfcp_erp_setup_act(int need, u32 act_status,
175 struct zfcp_adapter *adapter,
176 struct zfcp_port *port,
177 struct scsi_device *sdev)
179 struct zfcp_erp_action *erp_action;
180 struct zfcp_scsi_dev *zfcp_sdev;
182 switch (need) {
183 case ZFCP_ERP_ACTION_REOPEN_LUN:
184 zfcp_sdev = sdev_to_zfcp(sdev);
185 if (!(act_status & ZFCP_STATUS_ERP_NO_REF))
186 if (scsi_device_get(sdev))
187 return NULL;
188 atomic_set_mask(ZFCP_STATUS_COMMON_ERP_INUSE,
189 &zfcp_sdev->status);
190 erp_action = &zfcp_sdev->erp_action;
191 if (!(atomic_read(&zfcp_sdev->status) &
192 ZFCP_STATUS_COMMON_RUNNING))
193 act_status |= ZFCP_STATUS_ERP_CLOSE_ONLY;
194 break;
196 case ZFCP_ERP_ACTION_REOPEN_PORT:
197 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
198 if (!get_device(&port->dev))
199 return NULL;
200 zfcp_erp_action_dismiss_port(port);
201 atomic_set_mask(ZFCP_STATUS_COMMON_ERP_INUSE, &port->status);
202 erp_action = &port->erp_action;
203 if (!(atomic_read(&port->status) & ZFCP_STATUS_COMMON_RUNNING))
204 act_status |= ZFCP_STATUS_ERP_CLOSE_ONLY;
205 break;
207 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
208 kref_get(&adapter->ref);
209 zfcp_erp_action_dismiss_adapter(adapter);
210 atomic_set_mask(ZFCP_STATUS_COMMON_ERP_INUSE, &adapter->status);
211 erp_action = &adapter->erp_action;
212 if (!(atomic_read(&adapter->status) &
213 ZFCP_STATUS_COMMON_RUNNING))
214 act_status |= ZFCP_STATUS_ERP_CLOSE_ONLY;
215 break;
217 default:
218 return NULL;
221 memset(erp_action, 0, sizeof(struct zfcp_erp_action));
222 erp_action->adapter = adapter;
223 erp_action->port = port;
224 erp_action->sdev = sdev;
225 erp_action->action = need;
226 erp_action->status = act_status;
228 return erp_action;
231 static int zfcp_erp_action_enqueue(int want, struct zfcp_adapter *adapter,
232 struct zfcp_port *port,
233 struct scsi_device *sdev,
234 char *id, void *ref, u32 act_status)
236 int retval = 1, need;
237 struct zfcp_erp_action *act = NULL;
239 if (!adapter->erp_thread)
240 return -EIO;
242 need = zfcp_erp_required_act(want, adapter, port, sdev);
243 if (!need)
244 goto out;
246 act = zfcp_erp_setup_act(need, act_status, adapter, port, sdev);
247 if (!act)
248 goto out;
249 atomic_set_mask(ZFCP_STATUS_ADAPTER_ERP_PENDING, &adapter->status);
250 ++adapter->erp_total_count;
251 list_add_tail(&act->list, &adapter->erp_ready_head);
252 wake_up(&adapter->erp_ready_wq);
253 zfcp_dbf_rec_thread("eracte1", adapter->dbf);
254 retval = 0;
255 out:
256 zfcp_dbf_rec_trigger(id, ref, want, need, act, adapter, port, sdev);
257 return retval;
260 static int _zfcp_erp_adapter_reopen(struct zfcp_adapter *adapter,
261 int clear_mask, char *id, void *ref)
263 zfcp_erp_adapter_block(adapter, clear_mask);
264 zfcp_scsi_schedule_rports_block(adapter);
266 /* ensure propagation of failed status to new devices */
267 if (atomic_read(&adapter->status) & ZFCP_STATUS_COMMON_ERP_FAILED) {
268 zfcp_erp_set_adapter_status(adapter,
269 ZFCP_STATUS_COMMON_ERP_FAILED);
270 return -EIO;
272 return zfcp_erp_action_enqueue(ZFCP_ERP_ACTION_REOPEN_ADAPTER,
273 adapter, NULL, NULL, id, ref, 0);
277 * zfcp_erp_adapter_reopen - Reopen adapter.
278 * @adapter: Adapter to reopen.
279 * @clear: Status flags to clear.
280 * @id: Id for debug trace event.
281 * @ref: Reference for debug trace event.
283 void zfcp_erp_adapter_reopen(struct zfcp_adapter *adapter, int clear,
284 char *id, void *ref)
286 unsigned long flags;
288 zfcp_erp_adapter_block(adapter, clear);
289 zfcp_scsi_schedule_rports_block(adapter);
291 write_lock_irqsave(&adapter->erp_lock, flags);
292 if (atomic_read(&adapter->status) & ZFCP_STATUS_COMMON_ERP_FAILED)
293 zfcp_erp_set_adapter_status(adapter,
294 ZFCP_STATUS_COMMON_ERP_FAILED);
295 else
296 zfcp_erp_action_enqueue(ZFCP_ERP_ACTION_REOPEN_ADAPTER, adapter,
297 NULL, NULL, id, ref, 0);
298 write_unlock_irqrestore(&adapter->erp_lock, flags);
302 * zfcp_erp_adapter_shutdown - Shutdown adapter.
303 * @adapter: Adapter to shut down.
304 * @clear: Status flags to clear.
305 * @id: Id for debug trace event.
306 * @ref: Reference for debug trace event.
308 void zfcp_erp_adapter_shutdown(struct zfcp_adapter *adapter, int clear,
309 char *id, void *ref)
311 int flags = ZFCP_STATUS_COMMON_RUNNING | ZFCP_STATUS_COMMON_ERP_FAILED;
312 zfcp_erp_adapter_reopen(adapter, clear | flags, id, ref);
316 * zfcp_erp_port_shutdown - Shutdown port
317 * @port: Port to shut down.
318 * @clear: Status flags to clear.
319 * @id: Id for debug trace event.
320 * @ref: Reference for debug trace event.
322 void zfcp_erp_port_shutdown(struct zfcp_port *port, int clear, char *id,
323 void *ref)
325 int flags = ZFCP_STATUS_COMMON_RUNNING | ZFCP_STATUS_COMMON_ERP_FAILED;
326 zfcp_erp_port_reopen(port, clear | flags, id, ref);
329 static void zfcp_erp_port_block(struct zfcp_port *port, int clear)
331 zfcp_erp_clear_port_status(port,
332 ZFCP_STATUS_COMMON_UNBLOCKED | clear);
335 static void _zfcp_erp_port_forced_reopen(struct zfcp_port *port,
336 int clear, char *id, void *ref)
338 zfcp_erp_port_block(port, clear);
339 zfcp_scsi_schedule_rport_block(port);
341 if (atomic_read(&port->status) & ZFCP_STATUS_COMMON_ERP_FAILED)
342 return;
344 zfcp_erp_action_enqueue(ZFCP_ERP_ACTION_REOPEN_PORT_FORCED,
345 port->adapter, port, NULL, id, ref, 0);
349 * zfcp_erp_port_forced_reopen - Forced close of port and open again
350 * @port: Port to force close and to reopen.
351 * @id: Id for debug trace event.
352 * @ref: Reference for debug trace event.
354 void zfcp_erp_port_forced_reopen(struct zfcp_port *port, int clear, char *id,
355 void *ref)
357 unsigned long flags;
358 struct zfcp_adapter *adapter = port->adapter;
360 write_lock_irqsave(&adapter->erp_lock, flags);
361 _zfcp_erp_port_forced_reopen(port, clear, id, ref);
362 write_unlock_irqrestore(&adapter->erp_lock, flags);
365 static int _zfcp_erp_port_reopen(struct zfcp_port *port, int clear, char *id,
366 void *ref)
368 zfcp_erp_port_block(port, clear);
369 zfcp_scsi_schedule_rport_block(port);
371 if (atomic_read(&port->status) & ZFCP_STATUS_COMMON_ERP_FAILED) {
372 /* ensure propagation of failed status to new devices */
373 zfcp_erp_set_port_status(port, ZFCP_STATUS_COMMON_ERP_FAILED);
374 return -EIO;
377 return zfcp_erp_action_enqueue(ZFCP_ERP_ACTION_REOPEN_PORT,
378 port->adapter, port, NULL, id, ref, 0);
382 * zfcp_erp_port_reopen - trigger remote port recovery
383 * @port: port to recover
384 * @clear_mask: flags in port status to be cleared
386 * Returns 0 if recovery has been triggered, < 0 if not.
388 int zfcp_erp_port_reopen(struct zfcp_port *port, int clear, char *id, void *ref)
390 int retval;
391 unsigned long flags;
392 struct zfcp_adapter *adapter = port->adapter;
394 write_lock_irqsave(&adapter->erp_lock, flags);
395 retval = _zfcp_erp_port_reopen(port, clear, id, ref);
396 write_unlock_irqrestore(&adapter->erp_lock, flags);
398 return retval;
401 static void zfcp_erp_lun_block(struct scsi_device *sdev, int clear_mask)
403 zfcp_erp_clear_lun_status(sdev,
404 ZFCP_STATUS_COMMON_UNBLOCKED | clear_mask);
407 static void _zfcp_erp_lun_reopen(struct scsi_device *sdev, int clear, char *id,
408 void *ref, u32 act_status)
410 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
411 struct zfcp_adapter *adapter = zfcp_sdev->port->adapter;
413 zfcp_erp_lun_block(sdev, clear);
415 if (atomic_read(&zfcp_sdev->status) & ZFCP_STATUS_COMMON_ERP_FAILED)
416 return;
418 zfcp_erp_action_enqueue(ZFCP_ERP_ACTION_REOPEN_LUN, adapter,
419 zfcp_sdev->port, sdev, id, ref, act_status);
423 * zfcp_erp_lun_reopen - initiate reopen of a LUN
424 * @sdev: SCSI device / LUN to be reopened
425 * @clear_mask: specifies flags in LUN status to be cleared
426 * Return: 0 on success, < 0 on error
428 void zfcp_erp_lun_reopen(struct scsi_device *sdev, int clear, char *id,
429 void *ref)
431 unsigned long flags;
432 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
433 struct zfcp_port *port = zfcp_sdev->port;
434 struct zfcp_adapter *adapter = port->adapter;
436 write_lock_irqsave(&adapter->erp_lock, flags);
437 _zfcp_erp_lun_reopen(sdev, clear, id, ref, 0);
438 write_unlock_irqrestore(&adapter->erp_lock, flags);
442 * zfcp_erp_lun_shutdown - Shutdown LUN
443 * @sdev: SCSI device / LUN to shut down.
444 * @clear: Status flags to clear.
445 * @id: Id for debug trace event.
446 * @ref: Reference for debug trace event.
448 void zfcp_erp_lun_shutdown(struct scsi_device *sdev, int clear, char *id,
449 void *ref)
451 int flags = ZFCP_STATUS_COMMON_RUNNING | ZFCP_STATUS_COMMON_ERP_FAILED;
452 zfcp_erp_lun_reopen(sdev, clear | flags, id, ref);
456 * zfcp_erp_lun_shutdown_wait - Shutdown LUN and wait for erp completion
457 * @sdev: SCSI device / LUN to shut down.
458 * @id: Id for debug trace event.
460 * Do not acquire a reference for the LUN when creating the ERP
461 * action. It is safe, because this function waits for the ERP to
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.
465 void zfcp_erp_lun_shutdown_wait(struct scsi_device *sdev, char *id)
467 unsigned long flags;
468 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
469 struct zfcp_port *port = zfcp_sdev->port;
470 struct zfcp_adapter *adapter = port->adapter;
471 int clear = ZFCP_STATUS_COMMON_RUNNING | ZFCP_STATUS_COMMON_ERP_FAILED;
473 write_lock_irqsave(&adapter->erp_lock, flags);
474 _zfcp_erp_lun_reopen(sdev, clear, id, NULL, ZFCP_STATUS_ERP_NO_REF);
475 write_unlock_irqrestore(&adapter->erp_lock, flags);
477 zfcp_erp_wait(adapter);
480 static int status_change_set(unsigned long mask, atomic_t *status)
482 return (atomic_read(status) ^ mask) & mask;
485 static void zfcp_erp_adapter_unblock(struct zfcp_adapter *adapter)
487 if (status_change_set(ZFCP_STATUS_COMMON_UNBLOCKED, &adapter->status))
488 zfcp_dbf_rec_adapter("eraubl1", NULL, adapter->dbf);
489 atomic_set_mask(ZFCP_STATUS_COMMON_UNBLOCKED, &adapter->status);
492 static void zfcp_erp_port_unblock(struct zfcp_port *port)
494 if (status_change_set(ZFCP_STATUS_COMMON_UNBLOCKED, &port->status))
495 zfcp_dbf_rec_port("erpubl1", NULL, port);
496 atomic_set_mask(ZFCP_STATUS_COMMON_UNBLOCKED, &port->status);
499 static void zfcp_erp_lun_unblock(struct scsi_device *sdev)
501 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
503 if (status_change_set(ZFCP_STATUS_COMMON_UNBLOCKED, &zfcp_sdev->status))
504 zfcp_dbf_rec_lun("erlubl1", NULL, sdev);
505 atomic_set_mask(ZFCP_STATUS_COMMON_UNBLOCKED, &zfcp_sdev->status);
508 static void zfcp_erp_action_to_running(struct zfcp_erp_action *erp_action)
510 list_move(&erp_action->list, &erp_action->adapter->erp_running_head);
511 zfcp_dbf_rec_action("erator1", erp_action);
514 static void zfcp_erp_strategy_check_fsfreq(struct zfcp_erp_action *act)
516 struct zfcp_adapter *adapter = act->adapter;
517 struct zfcp_fsf_req *req;
519 if (!act->fsf_req_id)
520 return;
522 spin_lock(&adapter->req_list->lock);
523 req = _zfcp_reqlist_find(adapter->req_list, act->fsf_req_id);
524 if (req && req->erp_action == act) {
525 if (act->status & (ZFCP_STATUS_ERP_DISMISSED |
526 ZFCP_STATUS_ERP_TIMEDOUT)) {
527 req->status |= ZFCP_STATUS_FSFREQ_DISMISSED;
528 zfcp_dbf_rec_action("erscf_1", act);
529 req->erp_action = NULL;
531 if (act->status & ZFCP_STATUS_ERP_TIMEDOUT)
532 zfcp_dbf_rec_action("erscf_2", act);
533 if (req->status & ZFCP_STATUS_FSFREQ_DISMISSED)
534 act->fsf_req_id = 0;
535 } else
536 act->fsf_req_id = 0;
537 spin_unlock(&adapter->req_list->lock);
541 * zfcp_erp_notify - Trigger ERP action.
542 * @erp_action: ERP action to continue.
543 * @set_mask: ERP action status flags to set.
545 void zfcp_erp_notify(struct zfcp_erp_action *erp_action, unsigned long set_mask)
547 struct zfcp_adapter *adapter = erp_action->adapter;
548 unsigned long flags;
550 write_lock_irqsave(&adapter->erp_lock, flags);
551 if (zfcp_erp_action_exists(erp_action) == ZFCP_ERP_ACTION_RUNNING) {
552 erp_action->status |= set_mask;
553 zfcp_erp_action_ready(erp_action);
555 write_unlock_irqrestore(&adapter->erp_lock, flags);
559 * zfcp_erp_timeout_handler - Trigger ERP action from timed out ERP request
560 * @data: ERP action (from timer data)
562 void zfcp_erp_timeout_handler(unsigned long data)
564 struct zfcp_erp_action *act = (struct zfcp_erp_action *) data;
565 zfcp_erp_notify(act, ZFCP_STATUS_ERP_TIMEDOUT);
568 static void zfcp_erp_memwait_handler(unsigned long data)
570 zfcp_erp_notify((struct zfcp_erp_action *)data, 0);
573 static void zfcp_erp_strategy_memwait(struct zfcp_erp_action *erp_action)
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);
582 static void _zfcp_erp_port_reopen_all(struct zfcp_adapter *adapter,
583 int clear, char *id, void *ref)
585 struct zfcp_port *port;
587 read_lock(&adapter->port_list_lock);
588 list_for_each_entry(port, &adapter->port_list, list)
589 _zfcp_erp_port_reopen(port, clear, id, ref);
590 read_unlock(&adapter->port_list_lock);
593 static void _zfcp_erp_lun_reopen_all(struct zfcp_port *port, int clear,
594 char *id, void *ref)
596 struct scsi_device *sdev;
598 shost_for_each_device(sdev, port->adapter->scsi_host)
599 if (sdev_to_zfcp(sdev)->port == port)
600 _zfcp_erp_lun_reopen(sdev, clear, id, ref, 0);
603 static void zfcp_erp_strategy_followup_failed(struct zfcp_erp_action *act)
605 switch (act->action) {
606 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
607 _zfcp_erp_adapter_reopen(act->adapter, 0, "ersff_1", NULL);
608 break;
609 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
610 _zfcp_erp_port_forced_reopen(act->port, 0, "ersff_2", NULL);
611 break;
612 case ZFCP_ERP_ACTION_REOPEN_PORT:
613 _zfcp_erp_port_reopen(act->port, 0, "ersff_3", NULL);
614 break;
615 case ZFCP_ERP_ACTION_REOPEN_LUN:
616 _zfcp_erp_lun_reopen(act->sdev, 0, "ersff_4", NULL, 0);
617 break;
621 static void zfcp_erp_strategy_followup_success(struct zfcp_erp_action *act)
623 switch (act->action) {
624 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
625 _zfcp_erp_port_reopen_all(act->adapter, 0, "ersfs_1", NULL);
626 break;
627 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
628 _zfcp_erp_port_reopen(act->port, 0, "ersfs_2", NULL);
629 break;
630 case ZFCP_ERP_ACTION_REOPEN_PORT:
631 _zfcp_erp_lun_reopen_all(act->port, 0, "ersfs_3", NULL);
632 break;
636 static void zfcp_erp_wakeup(struct zfcp_adapter *adapter)
638 unsigned long flags;
640 read_lock_irqsave(&adapter->erp_lock, flags);
641 if (list_empty(&adapter->erp_ready_head) &&
642 list_empty(&adapter->erp_running_head)) {
643 atomic_clear_mask(ZFCP_STATUS_ADAPTER_ERP_PENDING,
644 &adapter->status);
645 wake_up(&adapter->erp_done_wqh);
647 read_unlock_irqrestore(&adapter->erp_lock, flags);
650 static int zfcp_erp_adapter_strategy_open_qdio(struct zfcp_erp_action *act)
652 struct zfcp_qdio *qdio = act->adapter->qdio;
654 if (zfcp_qdio_open(qdio))
655 return ZFCP_ERP_FAILED;
656 init_waitqueue_head(&qdio->req_q_wq);
657 atomic_set_mask(ZFCP_STATUS_ADAPTER_QDIOUP, &act->adapter->status);
658 return ZFCP_ERP_SUCCEEDED;
661 static void zfcp_erp_enqueue_ptp_port(struct zfcp_adapter *adapter)
663 struct zfcp_port *port;
664 port = zfcp_port_enqueue(adapter, adapter->peer_wwpn, 0,
665 adapter->peer_d_id);
666 if (IS_ERR(port)) /* error or port already attached */
667 return;
668 _zfcp_erp_port_reopen(port, 0, "ereptp1", NULL);
671 static int zfcp_erp_adapter_strat_fsf_xconf(struct zfcp_erp_action *erp_action)
673 int retries;
674 int sleep = 1;
675 struct zfcp_adapter *adapter = erp_action->adapter;
677 atomic_clear_mask(ZFCP_STATUS_ADAPTER_XCONFIG_OK, &adapter->status);
679 for (retries = 7; retries; retries--) {
680 atomic_clear_mask(ZFCP_STATUS_ADAPTER_HOST_CON_INIT,
681 &adapter->status);
682 write_lock_irq(&adapter->erp_lock);
683 zfcp_erp_action_to_running(erp_action);
684 write_unlock_irq(&adapter->erp_lock);
685 if (zfcp_fsf_exchange_config_data(erp_action)) {
686 atomic_clear_mask(ZFCP_STATUS_ADAPTER_HOST_CON_INIT,
687 &adapter->status);
688 return ZFCP_ERP_FAILED;
691 zfcp_dbf_rec_thread_lock("erasfx1", adapter->dbf);
692 wait_event(adapter->erp_ready_wq,
693 !list_empty(&adapter->erp_ready_head));
694 zfcp_dbf_rec_thread_lock("erasfx2", adapter->dbf);
695 if (erp_action->status & ZFCP_STATUS_ERP_TIMEDOUT)
696 break;
698 if (!(atomic_read(&adapter->status) &
699 ZFCP_STATUS_ADAPTER_HOST_CON_INIT))
700 break;
702 ssleep(sleep);
703 sleep *= 2;
706 atomic_clear_mask(ZFCP_STATUS_ADAPTER_HOST_CON_INIT,
707 &adapter->status);
709 if (!(atomic_read(&adapter->status) & ZFCP_STATUS_ADAPTER_XCONFIG_OK))
710 return ZFCP_ERP_FAILED;
712 if (fc_host_port_type(adapter->scsi_host) == FC_PORTTYPE_PTP)
713 zfcp_erp_enqueue_ptp_port(adapter);
715 return ZFCP_ERP_SUCCEEDED;
718 static int zfcp_erp_adapter_strategy_open_fsf_xport(struct zfcp_erp_action *act)
720 int ret;
721 struct zfcp_adapter *adapter = act->adapter;
723 write_lock_irq(&adapter->erp_lock);
724 zfcp_erp_action_to_running(act);
725 write_unlock_irq(&adapter->erp_lock);
727 ret = zfcp_fsf_exchange_port_data(act);
728 if (ret == -EOPNOTSUPP)
729 return ZFCP_ERP_SUCCEEDED;
730 if (ret)
731 return ZFCP_ERP_FAILED;
733 zfcp_dbf_rec_thread_lock("erasox1", adapter->dbf);
734 wait_event(adapter->erp_ready_wq,
735 !list_empty(&adapter->erp_ready_head));
736 zfcp_dbf_rec_thread_lock("erasox2", adapter->dbf);
737 if (act->status & ZFCP_STATUS_ERP_TIMEDOUT)
738 return ZFCP_ERP_FAILED;
740 return ZFCP_ERP_SUCCEEDED;
743 static int zfcp_erp_adapter_strategy_open_fsf(struct zfcp_erp_action *act)
745 if (zfcp_erp_adapter_strat_fsf_xconf(act) == ZFCP_ERP_FAILED)
746 return ZFCP_ERP_FAILED;
748 if (zfcp_erp_adapter_strategy_open_fsf_xport(act) == ZFCP_ERP_FAILED)
749 return ZFCP_ERP_FAILED;
751 if (mempool_resize(act->adapter->pool.status_read_data,
752 act->adapter->stat_read_buf_num, GFP_KERNEL))
753 return ZFCP_ERP_FAILED;
755 if (mempool_resize(act->adapter->pool.status_read_req,
756 act->adapter->stat_read_buf_num, GFP_KERNEL))
757 return ZFCP_ERP_FAILED;
759 atomic_set(&act->adapter->stat_miss, act->adapter->stat_read_buf_num);
760 if (zfcp_status_read_refill(act->adapter))
761 return ZFCP_ERP_FAILED;
763 return ZFCP_ERP_SUCCEEDED;
766 static void zfcp_erp_adapter_strategy_close(struct zfcp_erp_action *act)
768 struct zfcp_adapter *adapter = act->adapter;
770 /* close queues to ensure that buffers are not accessed by adapter */
771 zfcp_qdio_close(adapter->qdio);
772 zfcp_fsf_req_dismiss_all(adapter);
773 adapter->fsf_req_seq_no = 0;
774 zfcp_fc_wka_ports_force_offline(adapter->gs);
775 /* all ports and LUNs are closed */
776 zfcp_erp_clear_adapter_status(adapter, ZFCP_STATUS_COMMON_OPEN);
778 atomic_clear_mask(ZFCP_STATUS_ADAPTER_XCONFIG_OK |
779 ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED, &adapter->status);
782 static int zfcp_erp_adapter_strategy_open(struct zfcp_erp_action *act)
784 struct zfcp_adapter *adapter = act->adapter;
786 if (zfcp_erp_adapter_strategy_open_qdio(act)) {
787 atomic_clear_mask(ZFCP_STATUS_ADAPTER_XCONFIG_OK |
788 ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED,
789 &adapter->status);
790 return ZFCP_ERP_FAILED;
793 if (zfcp_erp_adapter_strategy_open_fsf(act)) {
794 zfcp_erp_adapter_strategy_close(act);
795 return ZFCP_ERP_FAILED;
798 atomic_set_mask(ZFCP_STATUS_COMMON_OPEN, &adapter->status);
800 return ZFCP_ERP_SUCCEEDED;
803 static int zfcp_erp_adapter_strategy(struct zfcp_erp_action *act)
805 struct zfcp_adapter *adapter = act->adapter;
807 if (atomic_read(&adapter->status) & ZFCP_STATUS_COMMON_OPEN) {
808 zfcp_erp_adapter_strategy_close(act);
809 if (act->status & ZFCP_STATUS_ERP_CLOSE_ONLY)
810 return ZFCP_ERP_EXIT;
813 if (zfcp_erp_adapter_strategy_open(act)) {
814 ssleep(8);
815 return ZFCP_ERP_FAILED;
818 return ZFCP_ERP_SUCCEEDED;
821 static int zfcp_erp_port_forced_strategy_close(struct zfcp_erp_action *act)
823 int retval;
825 retval = zfcp_fsf_close_physical_port(act);
826 if (retval == -ENOMEM)
827 return ZFCP_ERP_NOMEM;
828 act->step = ZFCP_ERP_STEP_PHYS_PORT_CLOSING;
829 if (retval)
830 return ZFCP_ERP_FAILED;
832 return ZFCP_ERP_CONTINUES;
835 static void zfcp_erp_port_strategy_clearstati(struct zfcp_port *port)
837 atomic_clear_mask(ZFCP_STATUS_COMMON_ACCESS_DENIED, &port->status);
840 static int zfcp_erp_port_forced_strategy(struct zfcp_erp_action *erp_action)
842 struct zfcp_port *port = erp_action->port;
843 int status = atomic_read(&port->status);
845 switch (erp_action->step) {
846 case ZFCP_ERP_STEP_UNINITIALIZED:
847 zfcp_erp_port_strategy_clearstati(port);
848 if ((status & ZFCP_STATUS_PORT_PHYS_OPEN) &&
849 (status & ZFCP_STATUS_COMMON_OPEN))
850 return zfcp_erp_port_forced_strategy_close(erp_action);
851 else
852 return ZFCP_ERP_FAILED;
854 case ZFCP_ERP_STEP_PHYS_PORT_CLOSING:
855 if (!(status & ZFCP_STATUS_PORT_PHYS_OPEN))
856 return ZFCP_ERP_SUCCEEDED;
858 return ZFCP_ERP_FAILED;
861 static int zfcp_erp_port_strategy_close(struct zfcp_erp_action *erp_action)
863 int retval;
865 retval = zfcp_fsf_close_port(erp_action);
866 if (retval == -ENOMEM)
867 return ZFCP_ERP_NOMEM;
868 erp_action->step = ZFCP_ERP_STEP_PORT_CLOSING;
869 if (retval)
870 return ZFCP_ERP_FAILED;
871 return ZFCP_ERP_CONTINUES;
874 static int zfcp_erp_port_strategy_open_port(struct zfcp_erp_action *erp_action)
876 int retval;
878 retval = zfcp_fsf_open_port(erp_action);
879 if (retval == -ENOMEM)
880 return ZFCP_ERP_NOMEM;
881 erp_action->step = ZFCP_ERP_STEP_PORT_OPENING;
882 if (retval)
883 return ZFCP_ERP_FAILED;
884 return ZFCP_ERP_CONTINUES;
887 static int zfcp_erp_open_ptp_port(struct zfcp_erp_action *act)
889 struct zfcp_adapter *adapter = act->adapter;
890 struct zfcp_port *port = act->port;
892 if (port->wwpn != adapter->peer_wwpn) {
893 zfcp_erp_set_port_status(port, ZFCP_STATUS_COMMON_ERP_FAILED);
894 return ZFCP_ERP_FAILED;
896 port->d_id = adapter->peer_d_id;
897 return zfcp_erp_port_strategy_open_port(act);
900 static int zfcp_erp_port_strategy_open_common(struct zfcp_erp_action *act)
902 struct zfcp_adapter *adapter = act->adapter;
903 struct zfcp_port *port = act->port;
904 int p_status = atomic_read(&port->status);
906 switch (act->step) {
907 case ZFCP_ERP_STEP_UNINITIALIZED:
908 case ZFCP_ERP_STEP_PHYS_PORT_CLOSING:
909 case ZFCP_ERP_STEP_PORT_CLOSING:
910 if (fc_host_port_type(adapter->scsi_host) == FC_PORTTYPE_PTP)
911 return zfcp_erp_open_ptp_port(act);
912 if (!port->d_id) {
913 zfcp_fc_trigger_did_lookup(port);
914 return ZFCP_ERP_EXIT;
916 return zfcp_erp_port_strategy_open_port(act);
918 case ZFCP_ERP_STEP_PORT_OPENING:
919 /* D_ID might have changed during open */
920 if (p_status & ZFCP_STATUS_COMMON_OPEN) {
921 if (!port->d_id) {
922 zfcp_fc_trigger_did_lookup(port);
923 return ZFCP_ERP_EXIT;
925 return ZFCP_ERP_SUCCEEDED;
927 if (port->d_id && !(p_status & ZFCP_STATUS_COMMON_NOESC)) {
928 port->d_id = 0;
929 return ZFCP_ERP_FAILED;
931 /* fall through otherwise */
933 return ZFCP_ERP_FAILED;
936 static int zfcp_erp_port_strategy(struct zfcp_erp_action *erp_action)
938 struct zfcp_port *port = erp_action->port;
939 int p_status = atomic_read(&port->status);
941 if ((p_status & ZFCP_STATUS_COMMON_NOESC) &&
942 !(p_status & ZFCP_STATUS_COMMON_OPEN))
943 goto close_init_done;
945 switch (erp_action->step) {
946 case ZFCP_ERP_STEP_UNINITIALIZED:
947 zfcp_erp_port_strategy_clearstati(port);
948 if (p_status & ZFCP_STATUS_COMMON_OPEN)
949 return zfcp_erp_port_strategy_close(erp_action);
950 break;
952 case ZFCP_ERP_STEP_PORT_CLOSING:
953 if (p_status & ZFCP_STATUS_COMMON_OPEN)
954 return ZFCP_ERP_FAILED;
955 break;
958 close_init_done:
959 if (erp_action->status & ZFCP_STATUS_ERP_CLOSE_ONLY)
960 return ZFCP_ERP_EXIT;
962 return zfcp_erp_port_strategy_open_common(erp_action);
965 static void zfcp_erp_lun_strategy_clearstati(struct scsi_device *sdev)
967 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
969 atomic_clear_mask(ZFCP_STATUS_COMMON_ACCESS_DENIED |
970 ZFCP_STATUS_LUN_SHARED | ZFCP_STATUS_LUN_READONLY,
971 &zfcp_sdev->status);
974 static int zfcp_erp_lun_strategy_close(struct zfcp_erp_action *erp_action)
976 int retval = zfcp_fsf_close_lun(erp_action);
977 if (retval == -ENOMEM)
978 return ZFCP_ERP_NOMEM;
979 erp_action->step = ZFCP_ERP_STEP_LUN_CLOSING;
980 if (retval)
981 return ZFCP_ERP_FAILED;
982 return ZFCP_ERP_CONTINUES;
985 static int zfcp_erp_lun_strategy_open(struct zfcp_erp_action *erp_action)
987 int retval = zfcp_fsf_open_lun(erp_action);
988 if (retval == -ENOMEM)
989 return ZFCP_ERP_NOMEM;
990 erp_action->step = ZFCP_ERP_STEP_LUN_OPENING;
991 if (retval)
992 return ZFCP_ERP_FAILED;
993 return ZFCP_ERP_CONTINUES;
996 static int zfcp_erp_lun_strategy(struct zfcp_erp_action *erp_action)
998 struct scsi_device *sdev = erp_action->sdev;
999 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
1001 switch (erp_action->step) {
1002 case ZFCP_ERP_STEP_UNINITIALIZED:
1003 zfcp_erp_lun_strategy_clearstati(sdev);
1004 if (atomic_read(&zfcp_sdev->status) & ZFCP_STATUS_COMMON_OPEN)
1005 return zfcp_erp_lun_strategy_close(erp_action);
1006 /* already closed, fall through */
1007 case ZFCP_ERP_STEP_LUN_CLOSING:
1008 if (atomic_read(&zfcp_sdev->status) & ZFCP_STATUS_COMMON_OPEN)
1009 return ZFCP_ERP_FAILED;
1010 if (erp_action->status & ZFCP_STATUS_ERP_CLOSE_ONLY)
1011 return ZFCP_ERP_EXIT;
1012 return zfcp_erp_lun_strategy_open(erp_action);
1014 case ZFCP_ERP_STEP_LUN_OPENING:
1015 if (atomic_read(&zfcp_sdev->status) & ZFCP_STATUS_COMMON_OPEN)
1016 return ZFCP_ERP_SUCCEEDED;
1018 return ZFCP_ERP_FAILED;
1021 static int zfcp_erp_strategy_check_lun(struct scsi_device *sdev, int result)
1023 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
1025 switch (result) {
1026 case ZFCP_ERP_SUCCEEDED :
1027 atomic_set(&zfcp_sdev->erp_counter, 0);
1028 zfcp_erp_lun_unblock(sdev);
1029 break;
1030 case ZFCP_ERP_FAILED :
1031 atomic_inc(&zfcp_sdev->erp_counter);
1032 if (atomic_read(&zfcp_sdev->erp_counter) > ZFCP_MAX_ERPS) {
1033 dev_err(&zfcp_sdev->port->adapter->ccw_device->dev,
1034 "ERP failed for LUN 0x%016Lx on "
1035 "port 0x%016Lx\n",
1036 (unsigned long long)zfcp_scsi_dev_lun(sdev),
1037 (unsigned long long)zfcp_sdev->port->wwpn);
1038 zfcp_erp_set_lun_status(sdev,
1039 ZFCP_STATUS_COMMON_ERP_FAILED);
1041 break;
1044 if (atomic_read(&zfcp_sdev->status) & ZFCP_STATUS_COMMON_ERP_FAILED) {
1045 zfcp_erp_lun_block(sdev, 0);
1046 result = ZFCP_ERP_EXIT;
1048 return result;
1051 static int zfcp_erp_strategy_check_port(struct zfcp_port *port, int result)
1053 switch (result) {
1054 case ZFCP_ERP_SUCCEEDED :
1055 atomic_set(&port->erp_counter, 0);
1056 zfcp_erp_port_unblock(port);
1057 break;
1059 case ZFCP_ERP_FAILED :
1060 if (atomic_read(&port->status) & ZFCP_STATUS_COMMON_NOESC) {
1061 zfcp_erp_port_block(port, 0);
1062 result = ZFCP_ERP_EXIT;
1064 atomic_inc(&port->erp_counter);
1065 if (atomic_read(&port->erp_counter) > ZFCP_MAX_ERPS) {
1066 dev_err(&port->adapter->ccw_device->dev,
1067 "ERP failed for remote port 0x%016Lx\n",
1068 (unsigned long long)port->wwpn);
1069 zfcp_erp_set_port_status(port,
1070 ZFCP_STATUS_COMMON_ERP_FAILED);
1072 break;
1075 if (atomic_read(&port->status) & ZFCP_STATUS_COMMON_ERP_FAILED) {
1076 zfcp_erp_port_block(port, 0);
1077 result = ZFCP_ERP_EXIT;
1079 return result;
1082 static int zfcp_erp_strategy_check_adapter(struct zfcp_adapter *adapter,
1083 int result)
1085 switch (result) {
1086 case ZFCP_ERP_SUCCEEDED :
1087 atomic_set(&adapter->erp_counter, 0);
1088 zfcp_erp_adapter_unblock(adapter);
1089 break;
1091 case ZFCP_ERP_FAILED :
1092 atomic_inc(&adapter->erp_counter);
1093 if (atomic_read(&adapter->erp_counter) > ZFCP_MAX_ERPS) {
1094 dev_err(&adapter->ccw_device->dev,
1095 "ERP cannot recover an error "
1096 "on the FCP device\n");
1097 zfcp_erp_set_adapter_status(adapter,
1098 ZFCP_STATUS_COMMON_ERP_FAILED);
1100 break;
1103 if (atomic_read(&adapter->status) & ZFCP_STATUS_COMMON_ERP_FAILED) {
1104 zfcp_erp_adapter_block(adapter, 0);
1105 result = ZFCP_ERP_EXIT;
1107 return result;
1110 static int zfcp_erp_strategy_check_target(struct zfcp_erp_action *erp_action,
1111 int result)
1113 struct zfcp_adapter *adapter = erp_action->adapter;
1114 struct zfcp_port *port = erp_action->port;
1115 struct scsi_device *sdev = erp_action->sdev;
1117 switch (erp_action->action) {
1119 case ZFCP_ERP_ACTION_REOPEN_LUN:
1120 result = zfcp_erp_strategy_check_lun(sdev, result);
1121 break;
1123 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
1124 case ZFCP_ERP_ACTION_REOPEN_PORT:
1125 result = zfcp_erp_strategy_check_port(port, result);
1126 break;
1128 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
1129 result = zfcp_erp_strategy_check_adapter(adapter, result);
1130 break;
1132 return result;
1135 static int zfcp_erp_strat_change_det(atomic_t *target_status, u32 erp_status)
1137 int status = atomic_read(target_status);
1139 if ((status & ZFCP_STATUS_COMMON_RUNNING) &&
1140 (erp_status & ZFCP_STATUS_ERP_CLOSE_ONLY))
1141 return 1; /* take it online */
1143 if (!(status & ZFCP_STATUS_COMMON_RUNNING) &&
1144 !(erp_status & ZFCP_STATUS_ERP_CLOSE_ONLY))
1145 return 1; /* take it offline */
1147 return 0;
1150 static int zfcp_erp_strategy_statechange(struct zfcp_erp_action *act, int ret)
1152 int action = act->action;
1153 struct zfcp_adapter *adapter = act->adapter;
1154 struct zfcp_port *port = act->port;
1155 struct scsi_device *sdev = act->sdev;
1156 struct zfcp_scsi_dev *zfcp_sdev;
1157 u32 erp_status = act->status;
1159 switch (action) {
1160 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
1161 if (zfcp_erp_strat_change_det(&adapter->status, erp_status)) {
1162 _zfcp_erp_adapter_reopen(adapter,
1163 ZFCP_STATUS_COMMON_ERP_FAILED,
1164 "ersscg1", NULL);
1165 return ZFCP_ERP_EXIT;
1167 break;
1169 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
1170 case ZFCP_ERP_ACTION_REOPEN_PORT:
1171 if (zfcp_erp_strat_change_det(&port->status, erp_status)) {
1172 _zfcp_erp_port_reopen(port,
1173 ZFCP_STATUS_COMMON_ERP_FAILED,
1174 "ersscg2", NULL);
1175 return ZFCP_ERP_EXIT;
1177 break;
1179 case ZFCP_ERP_ACTION_REOPEN_LUN:
1180 zfcp_sdev = sdev_to_zfcp(sdev);
1181 if (zfcp_erp_strat_change_det(&zfcp_sdev->status, erp_status)) {
1182 _zfcp_erp_lun_reopen(sdev,
1183 ZFCP_STATUS_COMMON_ERP_FAILED,
1184 "ersscg3", NULL, 0);
1185 return ZFCP_ERP_EXIT;
1187 break;
1189 return ret;
1192 static void zfcp_erp_action_dequeue(struct zfcp_erp_action *erp_action)
1194 struct zfcp_adapter *adapter = erp_action->adapter;
1195 struct zfcp_scsi_dev *zfcp_sdev;
1197 adapter->erp_total_count--;
1198 if (erp_action->status & ZFCP_STATUS_ERP_LOWMEM) {
1199 adapter->erp_low_mem_count--;
1200 erp_action->status &= ~ZFCP_STATUS_ERP_LOWMEM;
1203 list_del(&erp_action->list);
1204 zfcp_dbf_rec_action("eractd1", erp_action);
1206 switch (erp_action->action) {
1207 case ZFCP_ERP_ACTION_REOPEN_LUN:
1208 zfcp_sdev = sdev_to_zfcp(erp_action->sdev);
1209 atomic_clear_mask(ZFCP_STATUS_COMMON_ERP_INUSE,
1210 &zfcp_sdev->status);
1211 break;
1213 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
1214 case ZFCP_ERP_ACTION_REOPEN_PORT:
1215 atomic_clear_mask(ZFCP_STATUS_COMMON_ERP_INUSE,
1216 &erp_action->port->status);
1217 break;
1219 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
1220 atomic_clear_mask(ZFCP_STATUS_COMMON_ERP_INUSE,
1221 &erp_action->adapter->status);
1222 break;
1226 static void zfcp_erp_action_cleanup(struct zfcp_erp_action *act, int result)
1228 struct zfcp_adapter *adapter = act->adapter;
1229 struct zfcp_port *port = act->port;
1230 struct scsi_device *sdev = act->sdev;
1232 switch (act->action) {
1233 case ZFCP_ERP_ACTION_REOPEN_LUN:
1234 if (!(act->status & ZFCP_STATUS_ERP_NO_REF))
1235 scsi_device_put(sdev);
1236 break;
1238 case ZFCP_ERP_ACTION_REOPEN_PORT:
1239 if (result == ZFCP_ERP_SUCCEEDED)
1240 zfcp_scsi_schedule_rport_register(port);
1241 /* fall through */
1242 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
1243 put_device(&port->dev);
1244 break;
1246 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
1247 if (result == ZFCP_ERP_SUCCEEDED) {
1248 register_service_level(&adapter->service_level);
1249 queue_work(adapter->work_queue, &adapter->scan_work);
1250 } else
1251 unregister_service_level(&adapter->service_level);
1252 kref_put(&adapter->ref, zfcp_adapter_release);
1253 break;
1257 static int zfcp_erp_strategy_do_action(struct zfcp_erp_action *erp_action)
1259 switch (erp_action->action) {
1260 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
1261 return zfcp_erp_adapter_strategy(erp_action);
1262 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
1263 return zfcp_erp_port_forced_strategy(erp_action);
1264 case ZFCP_ERP_ACTION_REOPEN_PORT:
1265 return zfcp_erp_port_strategy(erp_action);
1266 case ZFCP_ERP_ACTION_REOPEN_LUN:
1267 return zfcp_erp_lun_strategy(erp_action);
1269 return ZFCP_ERP_FAILED;
1272 static int zfcp_erp_strategy(struct zfcp_erp_action *erp_action)
1274 int retval;
1275 unsigned long flags;
1276 struct zfcp_adapter *adapter = erp_action->adapter;
1278 kref_get(&adapter->ref);
1280 write_lock_irqsave(&adapter->erp_lock, flags);
1281 zfcp_erp_strategy_check_fsfreq(erp_action);
1283 if (erp_action->status & ZFCP_STATUS_ERP_DISMISSED) {
1284 zfcp_erp_action_dequeue(erp_action);
1285 retval = ZFCP_ERP_DISMISSED;
1286 goto unlock;
1289 if (erp_action->status & ZFCP_STATUS_ERP_TIMEDOUT) {
1290 retval = ZFCP_ERP_FAILED;
1291 goto check_target;
1294 zfcp_erp_action_to_running(erp_action);
1296 /* no lock to allow for blocking operations */
1297 write_unlock_irqrestore(&adapter->erp_lock, flags);
1298 retval = zfcp_erp_strategy_do_action(erp_action);
1299 write_lock_irqsave(&adapter->erp_lock, flags);
1301 if (erp_action->status & ZFCP_STATUS_ERP_DISMISSED)
1302 retval = ZFCP_ERP_CONTINUES;
1304 switch (retval) {
1305 case ZFCP_ERP_NOMEM:
1306 if (!(erp_action->status & ZFCP_STATUS_ERP_LOWMEM)) {
1307 ++adapter->erp_low_mem_count;
1308 erp_action->status |= ZFCP_STATUS_ERP_LOWMEM;
1310 if (adapter->erp_total_count == adapter->erp_low_mem_count)
1311 _zfcp_erp_adapter_reopen(adapter, 0, "erstgy1", NULL);
1312 else {
1313 zfcp_erp_strategy_memwait(erp_action);
1314 retval = ZFCP_ERP_CONTINUES;
1316 goto unlock;
1318 case ZFCP_ERP_CONTINUES:
1319 if (erp_action->status & ZFCP_STATUS_ERP_LOWMEM) {
1320 --adapter->erp_low_mem_count;
1321 erp_action->status &= ~ZFCP_STATUS_ERP_LOWMEM;
1323 goto unlock;
1326 check_target:
1327 retval = zfcp_erp_strategy_check_target(erp_action, retval);
1328 zfcp_erp_action_dequeue(erp_action);
1329 retval = zfcp_erp_strategy_statechange(erp_action, retval);
1330 if (retval == ZFCP_ERP_EXIT)
1331 goto unlock;
1332 if (retval == ZFCP_ERP_SUCCEEDED)
1333 zfcp_erp_strategy_followup_success(erp_action);
1334 if (retval == ZFCP_ERP_FAILED)
1335 zfcp_erp_strategy_followup_failed(erp_action);
1337 unlock:
1338 write_unlock_irqrestore(&adapter->erp_lock, flags);
1340 if (retval != ZFCP_ERP_CONTINUES)
1341 zfcp_erp_action_cleanup(erp_action, retval);
1343 kref_put(&adapter->ref, zfcp_adapter_release);
1344 return retval;
1347 static int zfcp_erp_thread(void *data)
1349 struct zfcp_adapter *adapter = (struct zfcp_adapter *) data;
1350 struct list_head *next;
1351 struct zfcp_erp_action *act;
1352 unsigned long flags;
1354 for (;;) {
1355 zfcp_dbf_rec_thread_lock("erthrd1", adapter->dbf);
1356 wait_event_interruptible(adapter->erp_ready_wq,
1357 !list_empty(&adapter->erp_ready_head) ||
1358 kthread_should_stop());
1359 zfcp_dbf_rec_thread_lock("erthrd2", adapter->dbf);
1361 if (kthread_should_stop())
1362 break;
1364 write_lock_irqsave(&adapter->erp_lock, flags);
1365 next = adapter->erp_ready_head.next;
1366 write_unlock_irqrestore(&adapter->erp_lock, flags);
1368 if (next != &adapter->erp_ready_head) {
1369 act = list_entry(next, struct zfcp_erp_action, list);
1371 /* there is more to come after dismission, no notify */
1372 if (zfcp_erp_strategy(act) != ZFCP_ERP_DISMISSED)
1373 zfcp_erp_wakeup(adapter);
1377 return 0;
1381 * zfcp_erp_thread_setup - Start ERP thread for adapter
1382 * @adapter: Adapter to start the ERP thread for
1384 * Returns 0 on success or error code from kernel_thread()
1386 int zfcp_erp_thread_setup(struct zfcp_adapter *adapter)
1388 struct task_struct *thread;
1390 thread = kthread_run(zfcp_erp_thread, adapter, "zfcperp%s",
1391 dev_name(&adapter->ccw_device->dev));
1392 if (IS_ERR(thread)) {
1393 dev_err(&adapter->ccw_device->dev,
1394 "Creating an ERP thread for the FCP device failed.\n");
1395 return PTR_ERR(thread);
1398 adapter->erp_thread = thread;
1399 return 0;
1403 * zfcp_erp_thread_kill - Stop ERP thread.
1404 * @adapter: Adapter where the ERP thread should be stopped.
1406 * The caller of this routine ensures that the specified adapter has
1407 * been shut down and that this operation has been completed. Thus,
1408 * there are no pending erp_actions which would need to be handled
1409 * here.
1411 void zfcp_erp_thread_kill(struct zfcp_adapter *adapter)
1413 kthread_stop(adapter->erp_thread);
1414 adapter->erp_thread = NULL;
1415 WARN_ON(!list_empty(&adapter->erp_ready_head));
1416 WARN_ON(!list_empty(&adapter->erp_running_head));
1420 * zfcp_erp_wait - wait for completion of error recovery on an adapter
1421 * @adapter: adapter for which to wait for completion of its error recovery
1423 void zfcp_erp_wait(struct zfcp_adapter *adapter)
1425 wait_event(adapter->erp_done_wqh,
1426 !(atomic_read(&adapter->status) &
1427 ZFCP_STATUS_ADAPTER_ERP_PENDING));
1431 * zfcp_erp_set_adapter_status - set adapter status bits
1432 * @adapter: adapter to change the status
1433 * @mask: status bits to change
1435 * Changes in common status bits are propagated to attached ports and LUNs.
1437 void zfcp_erp_set_adapter_status(struct zfcp_adapter *adapter, u32 mask)
1439 struct zfcp_port *port;
1440 struct scsi_device *sdev;
1441 unsigned long flags;
1442 u32 common_mask = mask & ZFCP_COMMON_FLAGS;
1444 atomic_set_mask(mask, &adapter->status);
1446 if (!common_mask)
1447 return;
1449 read_lock_irqsave(&adapter->port_list_lock, flags);
1450 list_for_each_entry(port, &adapter->port_list, list)
1451 atomic_set_mask(common_mask, &port->status);
1452 read_unlock_irqrestore(&adapter->port_list_lock, flags);
1454 shost_for_each_device(sdev, adapter->scsi_host)
1455 atomic_set_mask(common_mask, &sdev_to_zfcp(sdev)->status);
1459 * zfcp_erp_clear_adapter_status - clear adapter status bits
1460 * @adapter: adapter to change the status
1461 * @mask: status bits to change
1463 * Changes in common status bits are propagated to attached ports and LUNs.
1465 void zfcp_erp_clear_adapter_status(struct zfcp_adapter *adapter, u32 mask)
1467 struct zfcp_port *port;
1468 struct scsi_device *sdev;
1469 unsigned long flags;
1470 u32 common_mask = mask & ZFCP_COMMON_FLAGS;
1471 u32 clear_counter = mask & ZFCP_STATUS_COMMON_ERP_FAILED;
1473 atomic_clear_mask(mask, &adapter->status);
1475 if (!common_mask)
1476 return;
1478 if (clear_counter)
1479 atomic_set(&adapter->erp_counter, 0);
1481 read_lock_irqsave(&adapter->port_list_lock, flags);
1482 list_for_each_entry(port, &adapter->port_list, list) {
1483 atomic_clear_mask(common_mask, &port->status);
1484 if (clear_counter)
1485 atomic_set(&port->erp_counter, 0);
1487 read_unlock_irqrestore(&adapter->port_list_lock, flags);
1489 shost_for_each_device(sdev, adapter->scsi_host) {
1490 atomic_clear_mask(common_mask, &sdev_to_zfcp(sdev)->status);
1491 if (clear_counter)
1492 atomic_set(&sdev_to_zfcp(sdev)->erp_counter, 0);
1497 * zfcp_erp_set_port_status - set port status bits
1498 * @port: port to change the status
1499 * @mask: status bits to change
1501 * Changes in common status bits are propagated to attached LUNs.
1503 void zfcp_erp_set_port_status(struct zfcp_port *port, u32 mask)
1505 struct scsi_device *sdev;
1506 u32 common_mask = mask & ZFCP_COMMON_FLAGS;
1508 atomic_set_mask(mask, &port->status);
1510 if (!common_mask)
1511 return;
1513 shost_for_each_device(sdev, port->adapter->scsi_host)
1514 if (sdev_to_zfcp(sdev)->port == port)
1515 atomic_set_mask(common_mask,
1516 &sdev_to_zfcp(sdev)->status);
1520 * zfcp_erp_clear_port_status - clear port status bits
1521 * @port: adapter to change the status
1522 * @mask: status bits to change
1524 * Changes in common status bits are propagated to attached LUNs.
1526 void zfcp_erp_clear_port_status(struct zfcp_port *port, u32 mask)
1528 struct scsi_device *sdev;
1529 u32 common_mask = mask & ZFCP_COMMON_FLAGS;
1530 u32 clear_counter = mask & ZFCP_STATUS_COMMON_ERP_FAILED;
1532 atomic_clear_mask(mask, &port->status);
1534 if (!common_mask)
1535 return;
1537 if (clear_counter)
1538 atomic_set(&port->erp_counter, 0);
1540 shost_for_each_device(sdev, port->adapter->scsi_host)
1541 if (sdev_to_zfcp(sdev)->port == port) {
1542 atomic_clear_mask(common_mask,
1543 &sdev_to_zfcp(sdev)->status);
1544 if (clear_counter)
1545 atomic_set(&sdev_to_zfcp(sdev)->erp_counter, 0);
1550 * zfcp_erp_set_lun_status - set lun status bits
1551 * @sdev: SCSI device / lun to set the status bits
1552 * @mask: status bits to change
1554 void zfcp_erp_set_lun_status(struct scsi_device *sdev, u32 mask)
1556 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
1558 atomic_set_mask(mask, &zfcp_sdev->status);
1562 * zfcp_erp_clear_lun_status - clear lun status bits
1563 * @sdev: SCSi device / lun to clear the status bits
1564 * @mask: status bits to change
1566 void zfcp_erp_clear_lun_status(struct scsi_device *sdev, u32 mask)
1568 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
1570 atomic_clear_mask(mask, &zfcp_sdev->status);
1572 if (mask & ZFCP_STATUS_COMMON_ERP_FAILED)
1573 atomic_set(&zfcp_sdev->erp_counter, 0);