blob: 3b23d675459829a2d3d2be60c3e1d7b24ecc341e [file] [log] [blame]
Swen Schillig41fa2ad2007-09-07 09:15:31 +02001/*
Christof Schmitt553448f2008-06-10 18:20:58 +02002 * zfcp device driver
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
Christof Schmitt553448f2008-06-10 18:20:58 +02004 * Error Recovery Procedures (ERP).
Swen Schillig41fa2ad2007-09-07 09:15:31 +02005 *
Steffen Maier2a940b82016-12-09 17:16:33 +01006 * Copyright IBM Corp. 2002, 2016
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 */
8
Christof Schmittecf39d42008-12-25 13:39:53 +01009#define KMSG_COMPONENT "zfcp"
10#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
11
Christof Schmitt347c6a92009-08-18 15:43:25 +020012#include <linux/kthread.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include "zfcp_ext.h"
Christof Schmittb6bd2fb2010-02-17 11:18:50 +010014#include "zfcp_reqlist.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070015
Christof Schmitt287ac012008-07-02 10:56:40 +020016#define ZFCP_MAX_ERPS 3
Linus Torvalds1da177e2005-04-16 15:20:36 -070017
Christof Schmitt287ac012008-07-02 10:56:40 +020018enum 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,
Christof Schmittfdbd1c52010-09-08 14:39:54 +020024 ZFCP_STATUS_ERP_NO_REF = 0x00800000,
Christof Schmitt287ac012008-07-02 10:56:40 +020025};
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
Christof Schmitt287ac012008-07-02 10:56:40 +020027enum 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,
Christof Schmitt287ac012008-07-02 10:56:40 +020032 ZFCP_ERP_STEP_PORT_OPENING = 0x0800,
Christof Schmittb62a8d92010-09-08 14:39:55 +020033 ZFCP_ERP_STEP_LUN_CLOSING = 0x1000,
34 ZFCP_ERP_STEP_LUN_OPENING = 0x2000,
Christof Schmitt287ac012008-07-02 10:56:40 +020035};
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Christof Schmitt287ac012008-07-02 10:56:40 +020037enum zfcp_erp_act_type {
Christof Schmittb62a8d92010-09-08 14:39:55 +020038 ZFCP_ERP_ACTION_REOPEN_LUN = 1,
Christof Schmitt287ac012008-07-02 10:56:40 +020039 ZFCP_ERP_ACTION_REOPEN_PORT = 2,
40 ZFCP_ERP_ACTION_REOPEN_PORT_FORCED = 3,
41 ZFCP_ERP_ACTION_REOPEN_ADAPTER = 4,
42};
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Christof Schmitt287ac012008-07-02 10:56:40 +020044enum zfcp_erp_act_state {
45 ZFCP_ERP_ACTION_RUNNING = 1,
46 ZFCP_ERP_ACTION_READY = 2,
47};
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Christof Schmitt287ac012008-07-02 10:56:40 +020049enum zfcp_erp_act_result {
50 ZFCP_ERP_SUCCEEDED = 0,
51 ZFCP_ERP_FAILED = 1,
52 ZFCP_ERP_CONTINUES = 2,
53 ZFCP_ERP_EXIT = 3,
54 ZFCP_ERP_DISMISSED = 4,
55 ZFCP_ERP_NOMEM = 5,
56};
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Christof Schmitt287ac012008-07-02 10:56:40 +020058static void zfcp_erp_adapter_block(struct zfcp_adapter *adapter, int mask)
Andreas Herrmann2abbe862006-09-18 22:29:56 +020059{
Swen Schilligedaed852010-09-08 14:40:01 +020060 zfcp_erp_clear_adapter_status(adapter,
61 ZFCP_STATUS_COMMON_UNBLOCKED | mask);
Andreas Herrmann2abbe862006-09-18 22:29:56 +020062}
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Christof Schmitt287ac012008-07-02 10:56:40 +020064static int zfcp_erp_action_exists(struct zfcp_erp_action *act)
Linus Torvalds1da177e2005-04-16 15:20:36 -070065{
Christof Schmitt287ac012008-07-02 10:56:40 +020066 struct zfcp_erp_action *curr_act;
67
68 list_for_each_entry(curr_act, &act->adapter->erp_running_head, list)
69 if (act == curr_act)
70 return ZFCP_ERP_ACTION_RUNNING;
71 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072}
73
Christof Schmitt287ac012008-07-02 10:56:40 +020074static void zfcp_erp_action_ready(struct zfcp_erp_action *act)
Linus Torvalds1da177e2005-04-16 15:20:36 -070075{
Christof Schmitt287ac012008-07-02 10:56:40 +020076 struct zfcp_adapter *adapter = act->adapter;
77
78 list_move(&act->list, &act->adapter->erp_ready_head);
Swen Schilligae0904f2010-12-02 15:16:12 +010079 zfcp_dbf_rec_run("erardy1", act);
Christof Schmitt347c6a92009-08-18 15:43:25 +020080 wake_up(&adapter->erp_ready_wq);
Swen Schilligae0904f2010-12-02 15:16:12 +010081 zfcp_dbf_rec_run("erardy2", act);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082}
83
Christof Schmitt287ac012008-07-02 10:56:40 +020084static void zfcp_erp_action_dismiss(struct zfcp_erp_action *act)
Linus Torvalds1da177e2005-04-16 15:20:36 -070085{
Christof Schmitt287ac012008-07-02 10:56:40 +020086 act->status |= ZFCP_STATUS_ERP_DISMISSED;
87 if (zfcp_erp_action_exists(act) == ZFCP_ERP_ACTION_RUNNING)
88 zfcp_erp_action_ready(act);
89}
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
Christof Schmittb62a8d92010-09-08 14:39:55 +020091static void zfcp_erp_action_dismiss_lun(struct scsi_device *sdev)
Christof Schmitt287ac012008-07-02 10:56:40 +020092{
Christof Schmittb62a8d92010-09-08 14:39:55 +020093 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
94
95 if (atomic_read(&zfcp_sdev->status) & ZFCP_STATUS_COMMON_ERP_INUSE)
96 zfcp_erp_action_dismiss(&zfcp_sdev->erp_action);
Christof Schmitt287ac012008-07-02 10:56:40 +020097}
98
99static void zfcp_erp_action_dismiss_port(struct zfcp_port *port)
100{
Christof Schmittb62a8d92010-09-08 14:39:55 +0200101 struct scsi_device *sdev;
Christof Schmitt287ac012008-07-02 10:56:40 +0200102
103 if (atomic_read(&port->status) & ZFCP_STATUS_COMMON_ERP_INUSE)
104 zfcp_erp_action_dismiss(&port->erp_action);
Martin Peschke924dd582013-08-22 17:45:37 +0200105 else {
106 spin_lock(port->adapter->scsi_host->host_lock);
107 __shost_for_each_device(sdev, port->adapter->scsi_host)
Christof Schmittb62a8d92010-09-08 14:39:55 +0200108 if (sdev_to_zfcp(sdev)->port == port)
109 zfcp_erp_action_dismiss_lun(sdev);
Martin Peschke924dd582013-08-22 17:45:37 +0200110 spin_unlock(port->adapter->scsi_host->host_lock);
111 }
Christof Schmitt287ac012008-07-02 10:56:40 +0200112}
113
114static void zfcp_erp_action_dismiss_adapter(struct zfcp_adapter *adapter)
115{
116 struct zfcp_port *port;
117
118 if (atomic_read(&adapter->status) & ZFCP_STATUS_COMMON_ERP_INUSE)
119 zfcp_erp_action_dismiss(&adapter->erp_action);
Swen Schilligecf0c772009-11-24 16:53:58 +0100120 else {
121 read_lock(&adapter->port_list_lock);
122 list_for_each_entry(port, &adapter->port_list, list)
Christof Schmitt287ac012008-07-02 10:56:40 +0200123 zfcp_erp_action_dismiss_port(port);
Swen Schilligecf0c772009-11-24 16:53:58 +0100124 read_unlock(&adapter->port_list_lock);
125 }
Christof Schmitt287ac012008-07-02 10:56:40 +0200126}
127
128static int zfcp_erp_required_act(int want, struct zfcp_adapter *adapter,
129 struct zfcp_port *port,
Christof Schmittb62a8d92010-09-08 14:39:55 +0200130 struct scsi_device *sdev)
Christof Schmitt287ac012008-07-02 10:56:40 +0200131{
132 int need = want;
Christof Schmittb62a8d92010-09-08 14:39:55 +0200133 int l_status, p_status, a_status;
134 struct zfcp_scsi_dev *zfcp_sdev;
Christof Schmitt287ac012008-07-02 10:56:40 +0200135
136 switch (want) {
Christof Schmittb62a8d92010-09-08 14:39:55 +0200137 case ZFCP_ERP_ACTION_REOPEN_LUN:
138 zfcp_sdev = sdev_to_zfcp(sdev);
139 l_status = atomic_read(&zfcp_sdev->status);
140 if (l_status & ZFCP_STATUS_COMMON_ERP_INUSE)
Christof Schmitt287ac012008-07-02 10:56:40 +0200141 return 0;
142 p_status = atomic_read(&port->status);
143 if (!(p_status & ZFCP_STATUS_COMMON_RUNNING) ||
144 p_status & ZFCP_STATUS_COMMON_ERP_FAILED)
145 return 0;
146 if (!(p_status & ZFCP_STATUS_COMMON_UNBLOCKED))
147 need = ZFCP_ERP_ACTION_REOPEN_PORT;
148 /* fall through */
Christof Schmitt287ac012008-07-02 10:56:40 +0200149 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
150 p_status = atomic_read(&port->status);
Christof Schmitt097ef3b2010-07-08 09:53:06 +0200151 if (!(p_status & ZFCP_STATUS_COMMON_OPEN))
152 need = ZFCP_ERP_ACTION_REOPEN_PORT;
153 /* fall through */
154 case ZFCP_ERP_ACTION_REOPEN_PORT:
155 p_status = atomic_read(&port->status);
Christof Schmitt287ac012008-07-02 10:56:40 +0200156 if (p_status & ZFCP_STATUS_COMMON_ERP_INUSE)
157 return 0;
158 a_status = atomic_read(&adapter->status);
159 if (!(a_status & ZFCP_STATUS_COMMON_RUNNING) ||
160 a_status & ZFCP_STATUS_COMMON_ERP_FAILED)
161 return 0;
Swen Schilligd3e10882010-11-17 14:23:42 +0100162 if (p_status & ZFCP_STATUS_COMMON_NOESC)
163 return need;
Christof Schmitt287ac012008-07-02 10:56:40 +0200164 if (!(a_status & ZFCP_STATUS_COMMON_UNBLOCKED))
165 need = ZFCP_ERP_ACTION_REOPEN_ADAPTER;
166 /* fall through */
167 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
168 a_status = atomic_read(&adapter->status);
169 if (a_status & ZFCP_STATUS_COMMON_ERP_INUSE)
170 return 0;
Christof Schmitt143bb6b2009-08-18 15:43:27 +0200171 if (!(a_status & ZFCP_STATUS_COMMON_RUNNING) &&
172 !(a_status & ZFCP_STATUS_COMMON_OPEN))
173 return 0; /* shutdown requested for closed adapter */
Christof Schmitt287ac012008-07-02 10:56:40 +0200174 }
175
176 return need;
177}
178
Christof Schmittfdbd1c52010-09-08 14:39:54 +0200179static struct zfcp_erp_action *zfcp_erp_setup_act(int need, u32 act_status,
Christof Schmitt287ac012008-07-02 10:56:40 +0200180 struct zfcp_adapter *adapter,
181 struct zfcp_port *port,
Christof Schmittb62a8d92010-09-08 14:39:55 +0200182 struct scsi_device *sdev)
Christof Schmitt287ac012008-07-02 10:56:40 +0200183{
184 struct zfcp_erp_action *erp_action;
Christof Schmittb62a8d92010-09-08 14:39:55 +0200185 struct zfcp_scsi_dev *zfcp_sdev;
Christof Schmitt287ac012008-07-02 10:56:40 +0200186
187 switch (need) {
Christof Schmittb62a8d92010-09-08 14:39:55 +0200188 case ZFCP_ERP_ACTION_REOPEN_LUN:
189 zfcp_sdev = sdev_to_zfcp(sdev);
Christof Schmittfdbd1c52010-09-08 14:39:54 +0200190 if (!(act_status & ZFCP_STATUS_ERP_NO_REF))
Christof Schmittb62a8d92010-09-08 14:39:55 +0200191 if (scsi_device_get(sdev))
Christof Schmittfdbd1c52010-09-08 14:39:54 +0200192 return NULL;
Peter Zijlstra805de8f42015-04-24 01:12:32 +0200193 atomic_or(ZFCP_STATUS_COMMON_ERP_INUSE,
Christof Schmittb62a8d92010-09-08 14:39:55 +0200194 &zfcp_sdev->status);
195 erp_action = &zfcp_sdev->erp_action;
Steffen Maier88acde82017-10-13 15:40:07 +0200196 WARN_ON_ONCE(erp_action->port != port);
197 WARN_ON_ONCE(erp_action->sdev != sdev);
Christof Schmittb62a8d92010-09-08 14:39:55 +0200198 if (!(atomic_read(&zfcp_sdev->status) &
199 ZFCP_STATUS_COMMON_RUNNING))
Christof Schmittfdbd1c52010-09-08 14:39:54 +0200200 act_status |= ZFCP_STATUS_ERP_CLOSE_ONLY;
Christof Schmitt287ac012008-07-02 10:56:40 +0200201 break;
202
203 case ZFCP_ERP_ACTION_REOPEN_PORT:
204 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
Christof Schmitt615f59e2010-02-17 11:18:56 +0100205 if (!get_device(&port->dev))
Swen Schillig6b1833342009-11-24 16:54:05 +0100206 return NULL;
Christof Schmitt287ac012008-07-02 10:56:40 +0200207 zfcp_erp_action_dismiss_port(port);
Peter Zijlstra805de8f42015-04-24 01:12:32 +0200208 atomic_or(ZFCP_STATUS_COMMON_ERP_INUSE, &port->status);
Christof Schmitt287ac012008-07-02 10:56:40 +0200209 erp_action = &port->erp_action;
Steffen Maier88acde82017-10-13 15:40:07 +0200210 WARN_ON_ONCE(erp_action->port != port);
211 WARN_ON_ONCE(erp_action->sdev != NULL);
Christof Schmitt287ac012008-07-02 10:56:40 +0200212 if (!(atomic_read(&port->status) & ZFCP_STATUS_COMMON_RUNNING))
Christof Schmittfdbd1c52010-09-08 14:39:54 +0200213 act_status |= ZFCP_STATUS_ERP_CLOSE_ONLY;
Christof Schmitt287ac012008-07-02 10:56:40 +0200214 break;
215
216 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
Swen Schilligf3450c72009-11-24 16:53:59 +0100217 kref_get(&adapter->ref);
Christof Schmitt287ac012008-07-02 10:56:40 +0200218 zfcp_erp_action_dismiss_adapter(adapter);
Peter Zijlstra805de8f42015-04-24 01:12:32 +0200219 atomic_or(ZFCP_STATUS_COMMON_ERP_INUSE, &adapter->status);
Christof Schmitt287ac012008-07-02 10:56:40 +0200220 erp_action = &adapter->erp_action;
Steffen Maier88acde82017-10-13 15:40:07 +0200221 WARN_ON_ONCE(erp_action->port != NULL);
222 WARN_ON_ONCE(erp_action->sdev != NULL);
Christof Schmitt287ac012008-07-02 10:56:40 +0200223 if (!(atomic_read(&adapter->status) &
224 ZFCP_STATUS_COMMON_RUNNING))
Christof Schmittfdbd1c52010-09-08 14:39:54 +0200225 act_status |= ZFCP_STATUS_ERP_CLOSE_ONLY;
Christof Schmitt287ac012008-07-02 10:56:40 +0200226 break;
227
228 default:
229 return NULL;
230 }
231
Steffen Maier88acde82017-10-13 15:40:07 +0200232 WARN_ON_ONCE(erp_action->adapter != adapter);
233 memset(&erp_action->list, 0, sizeof(erp_action->list));
234 memset(&erp_action->timer, 0, sizeof(erp_action->timer));
235 erp_action->step = ZFCP_ERP_STEP_UNINITIALIZED;
236 erp_action->fsf_req_id = 0;
Christof Schmitt287ac012008-07-02 10:56:40 +0200237 erp_action->action = need;
Christof Schmittfdbd1c52010-09-08 14:39:54 +0200238 erp_action->status = act_status;
Christof Schmitt287ac012008-07-02 10:56:40 +0200239
240 return erp_action;
241}
242
243static int zfcp_erp_action_enqueue(int want, struct zfcp_adapter *adapter,
244 struct zfcp_port *port,
Christof Schmittb62a8d92010-09-08 14:39:55 +0200245 struct scsi_device *sdev,
Swen Schilligea4a3a62010-12-02 15:16:16 +0100246 char *id, u32 act_status)
Christof Schmitt287ac012008-07-02 10:56:40 +0200247{
248 int retval = 1, need;
Swen Schilligae0904f2010-12-02 15:16:12 +0100249 struct zfcp_erp_action *act;
Christof Schmitt287ac012008-07-02 10:56:40 +0200250
Christof Schmitt347c6a92009-08-18 15:43:25 +0200251 if (!adapter->erp_thread)
Christof Schmitt287ac012008-07-02 10:56:40 +0200252 return -EIO;
253
Christof Schmittb62a8d92010-09-08 14:39:55 +0200254 need = zfcp_erp_required_act(want, adapter, port, sdev);
Christof Schmitt287ac012008-07-02 10:56:40 +0200255 if (!need)
256 goto out;
257
Christof Schmittb62a8d92010-09-08 14:39:55 +0200258 act = zfcp_erp_setup_act(need, act_status, adapter, port, sdev);
Christof Schmitt287ac012008-07-02 10:56:40 +0200259 if (!act)
260 goto out;
Peter Zijlstra805de8f42015-04-24 01:12:32 +0200261 atomic_or(ZFCP_STATUS_ADAPTER_ERP_PENDING, &adapter->status);
Christof Schmitt287ac012008-07-02 10:56:40 +0200262 ++adapter->erp_total_count;
263 list_add_tail(&act->list, &adapter->erp_ready_head);
Christof Schmitt347c6a92009-08-18 15:43:25 +0200264 wake_up(&adapter->erp_ready_wq);
Christof Schmitt287ac012008-07-02 10:56:40 +0200265 retval = 0;
266 out:
Swen Schilligae0904f2010-12-02 15:16:12 +0100267 zfcp_dbf_rec_trig(id, adapter, port, sdev, want, need);
Christof Schmitt287ac012008-07-02 10:56:40 +0200268 return retval;
269}
270
271static int _zfcp_erp_adapter_reopen(struct zfcp_adapter *adapter,
Swen Schilligea4a3a62010-12-02 15:16:16 +0100272 int clear_mask, char *id)
Christof Schmitt287ac012008-07-02 10:56:40 +0200273{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 zfcp_erp_adapter_block(adapter, clear_mask);
Christof Schmitta2fa0ae2009-03-02 13:09:08 +0100275 zfcp_scsi_schedule_rports_block(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
Christof Schmitt287ac012008-07-02 10:56:40 +0200277 /* ensure propagation of failed status to new devices */
278 if (atomic_read(&adapter->status) & ZFCP_STATUS_COMMON_ERP_FAILED) {
Swen Schilligedaed852010-09-08 14:40:01 +0200279 zfcp_erp_set_adapter_status(adapter,
280 ZFCP_STATUS_COMMON_ERP_FAILED);
Christof Schmitt287ac012008-07-02 10:56:40 +0200281 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 }
Christof Schmitt287ac012008-07-02 10:56:40 +0200283 return zfcp_erp_action_enqueue(ZFCP_ERP_ACTION_REOPEN_ADAPTER,
Swen Schilligea4a3a62010-12-02 15:16:16 +0100284 adapter, NULL, NULL, id, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285}
286
287/**
Christof Schmitt287ac012008-07-02 10:56:40 +0200288 * zfcp_erp_adapter_reopen - Reopen adapter.
289 * @adapter: Adapter to reopen.
290 * @clear: Status flags to clear.
291 * @id: Id for debug trace event.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 */
Swen Schilligea4a3a62010-12-02 15:16:16 +0100293void zfcp_erp_adapter_reopen(struct zfcp_adapter *adapter, int clear, char *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294{
Christof Schmitt287ac012008-07-02 10:56:40 +0200295 unsigned long flags;
296
Swen Schilligecf0c772009-11-24 16:53:58 +0100297 zfcp_erp_adapter_block(adapter, clear);
298 zfcp_scsi_schedule_rports_block(adapter);
299
300 write_lock_irqsave(&adapter->erp_lock, flags);
301 if (atomic_read(&adapter->status) & ZFCP_STATUS_COMMON_ERP_FAILED)
Swen Schilligedaed852010-09-08 14:40:01 +0200302 zfcp_erp_set_adapter_status(adapter,
303 ZFCP_STATUS_COMMON_ERP_FAILED);
Swen Schilligecf0c772009-11-24 16:53:58 +0100304 else
305 zfcp_erp_action_enqueue(ZFCP_ERP_ACTION_REOPEN_ADAPTER, adapter,
Swen Schilligea4a3a62010-12-02 15:16:16 +0100306 NULL, NULL, id, 0);
Swen Schilligecf0c772009-11-24 16:53:58 +0100307 write_unlock_irqrestore(&adapter->erp_lock, flags);
Christof Schmitt287ac012008-07-02 10:56:40 +0200308}
309
310/**
311 * zfcp_erp_adapter_shutdown - Shutdown adapter.
312 * @adapter: Adapter to shut down.
313 * @clear: Status flags to clear.
314 * @id: Id for debug trace event.
Christof Schmitt287ac012008-07-02 10:56:40 +0200315 */
316void zfcp_erp_adapter_shutdown(struct zfcp_adapter *adapter, int clear,
Swen Schilligea4a3a62010-12-02 15:16:16 +0100317 char *id)
Christof Schmitt287ac012008-07-02 10:56:40 +0200318{
319 int flags = ZFCP_STATUS_COMMON_RUNNING | ZFCP_STATUS_COMMON_ERP_FAILED;
Swen Schilligea4a3a62010-12-02 15:16:16 +0100320 zfcp_erp_adapter_reopen(adapter, clear | flags, id);
Christof Schmitt287ac012008-07-02 10:56:40 +0200321}
322
323/**
324 * zfcp_erp_port_shutdown - Shutdown port
325 * @port: Port to shut down.
326 * @clear: Status flags to clear.
327 * @id: Id for debug trace event.
Christof Schmitt287ac012008-07-02 10:56:40 +0200328 */
Swen Schilligea4a3a62010-12-02 15:16:16 +0100329void zfcp_erp_port_shutdown(struct zfcp_port *port, int clear, char *id)
Christof Schmitt287ac012008-07-02 10:56:40 +0200330{
331 int flags = ZFCP_STATUS_COMMON_RUNNING | ZFCP_STATUS_COMMON_ERP_FAILED;
Swen Schilligea4a3a62010-12-02 15:16:16 +0100332 zfcp_erp_port_reopen(port, clear | flags, id);
Christof Schmitt287ac012008-07-02 10:56:40 +0200333}
334
Christof Schmitt287ac012008-07-02 10:56:40 +0200335static void zfcp_erp_port_block(struct zfcp_port *port, int clear)
336{
Swen Schilligedaed852010-09-08 14:40:01 +0200337 zfcp_erp_clear_port_status(port,
338 ZFCP_STATUS_COMMON_UNBLOCKED | clear);
Christof Schmitt287ac012008-07-02 10:56:40 +0200339}
340
Swen Schilligea4a3a62010-12-02 15:16:16 +0100341static void _zfcp_erp_port_forced_reopen(struct zfcp_port *port, int clear,
342 char *id)
Christof Schmitt287ac012008-07-02 10:56:40 +0200343{
344 zfcp_erp_port_block(port, clear);
Christof Schmitta2fa0ae2009-03-02 13:09:08 +0100345 zfcp_scsi_schedule_rport_block(port);
Christof Schmitt287ac012008-07-02 10:56:40 +0200346
347 if (atomic_read(&port->status) & ZFCP_STATUS_COMMON_ERP_FAILED)
348 return;
349
350 zfcp_erp_action_enqueue(ZFCP_ERP_ACTION_REOPEN_PORT_FORCED,
Swen Schilligea4a3a62010-12-02 15:16:16 +0100351 port->adapter, port, NULL, id, 0);
Christof Schmitt287ac012008-07-02 10:56:40 +0200352}
353
354/**
355 * zfcp_erp_port_forced_reopen - Forced close of port and open again
356 * @port: Port to force close and to reopen.
Swen Schilligea4a3a62010-12-02 15:16:16 +0100357 * @clear: Status flags to clear.
Christof Schmitt287ac012008-07-02 10:56:40 +0200358 * @id: Id for debug trace event.
Christof Schmitt287ac012008-07-02 10:56:40 +0200359 */
Swen Schilligea4a3a62010-12-02 15:16:16 +0100360void zfcp_erp_port_forced_reopen(struct zfcp_port *port, int clear, char *id)
Christof Schmitt287ac012008-07-02 10:56:40 +0200361{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 unsigned long flags;
363 struct zfcp_adapter *adapter = port->adapter;
364
Swen Schilligecf0c772009-11-24 16:53:58 +0100365 write_lock_irqsave(&adapter->erp_lock, flags);
Swen Schilligea4a3a62010-12-02 15:16:16 +0100366 _zfcp_erp_port_forced_reopen(port, clear, id);
Swen Schilligecf0c772009-11-24 16:53:58 +0100367 write_unlock_irqrestore(&adapter->erp_lock, flags);
Christof Schmitt287ac012008-07-02 10:56:40 +0200368}
369
Swen Schilligea4a3a62010-12-02 15:16:16 +0100370static int _zfcp_erp_port_reopen(struct zfcp_port *port, int clear, char *id)
Christof Schmitt287ac012008-07-02 10:56:40 +0200371{
372 zfcp_erp_port_block(port, clear);
Christof Schmitta2fa0ae2009-03-02 13:09:08 +0100373 zfcp_scsi_schedule_rport_block(port);
Christof Schmitt287ac012008-07-02 10:56:40 +0200374
375 if (atomic_read(&port->status) & ZFCP_STATUS_COMMON_ERP_FAILED) {
376 /* ensure propagation of failed status to new devices */
Swen Schilligedaed852010-09-08 14:40:01 +0200377 zfcp_erp_set_port_status(port, ZFCP_STATUS_COMMON_ERP_FAILED);
Christof Schmitt287ac012008-07-02 10:56:40 +0200378 return -EIO;
379 }
380
381 return zfcp_erp_action_enqueue(ZFCP_ERP_ACTION_REOPEN_PORT,
Swen Schilligea4a3a62010-12-02 15:16:16 +0100382 port->adapter, port, NULL, id, 0);
Christof Schmitt287ac012008-07-02 10:56:40 +0200383}
384
385/**
386 * zfcp_erp_port_reopen - trigger remote port recovery
387 * @port: port to recover
388 * @clear_mask: flags in port status to be cleared
Swen Schilligea4a3a62010-12-02 15:16:16 +0100389 * @id: Id for debug trace event.
Christof Schmitt287ac012008-07-02 10:56:40 +0200390 *
391 * Returns 0 if recovery has been triggered, < 0 if not.
392 */
Swen Schilligea4a3a62010-12-02 15:16:16 +0100393int zfcp_erp_port_reopen(struct zfcp_port *port, int clear, char *id)
Christof Schmitt287ac012008-07-02 10:56:40 +0200394{
Christof Schmitt287ac012008-07-02 10:56:40 +0200395 int retval;
Swen Schilligecf0c772009-11-24 16:53:58 +0100396 unsigned long flags;
Christof Schmitt287ac012008-07-02 10:56:40 +0200397 struct zfcp_adapter *adapter = port->adapter;
398
Swen Schilligecf0c772009-11-24 16:53:58 +0100399 write_lock_irqsave(&adapter->erp_lock, flags);
Swen Schilligea4a3a62010-12-02 15:16:16 +0100400 retval = _zfcp_erp_port_reopen(port, clear, id);
Swen Schilligecf0c772009-11-24 16:53:58 +0100401 write_unlock_irqrestore(&adapter->erp_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
403 return retval;
404}
405
Christof Schmittb62a8d92010-09-08 14:39:55 +0200406static void zfcp_erp_lun_block(struct scsi_device *sdev, int clear_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407{
Swen Schilligedaed852010-09-08 14:40:01 +0200408 zfcp_erp_clear_lun_status(sdev,
409 ZFCP_STATUS_COMMON_UNBLOCKED | clear_mask);
Christof Schmitt287ac012008-07-02 10:56:40 +0200410}
411
Christof Schmittb62a8d92010-09-08 14:39:55 +0200412static void _zfcp_erp_lun_reopen(struct scsi_device *sdev, int clear, char *id,
Swen Schilligea4a3a62010-12-02 15:16:16 +0100413 u32 act_status)
Christof Schmitt287ac012008-07-02 10:56:40 +0200414{
Christof Schmittb62a8d92010-09-08 14:39:55 +0200415 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
416 struct zfcp_adapter *adapter = zfcp_sdev->port->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
Christof Schmittb62a8d92010-09-08 14:39:55 +0200418 zfcp_erp_lun_block(sdev, clear);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
Christof Schmittb62a8d92010-09-08 14:39:55 +0200420 if (atomic_read(&zfcp_sdev->status) & ZFCP_STATUS_COMMON_ERP_FAILED)
Christof Schmitt287ac012008-07-02 10:56:40 +0200421 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
Christof Schmittb62a8d92010-09-08 14:39:55 +0200423 zfcp_erp_action_enqueue(ZFCP_ERP_ACTION_REOPEN_LUN, adapter,
Swen Schilligea4a3a62010-12-02 15:16:16 +0100424 zfcp_sdev->port, sdev, id, act_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425}
426
427/**
Christof Schmittb62a8d92010-09-08 14:39:55 +0200428 * zfcp_erp_lun_reopen - initiate reopen of a LUN
429 * @sdev: SCSI device / LUN to be reopened
430 * @clear_mask: specifies flags in LUN status to be cleared
Swen Schilligea4a3a62010-12-02 15:16:16 +0100431 * @id: Id for debug trace event.
432 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 * Return: 0 on success, < 0 on error
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 */
Swen Schilligea4a3a62010-12-02 15:16:16 +0100435void zfcp_erp_lun_reopen(struct scsi_device *sdev, int clear, char *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 unsigned long flags;
Christof Schmittb62a8d92010-09-08 14:39:55 +0200438 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
439 struct zfcp_port *port = zfcp_sdev->port;
Christof Schmitt287ac012008-07-02 10:56:40 +0200440 struct zfcp_adapter *adapter = port->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
Swen Schilligecf0c772009-11-24 16:53:58 +0100442 write_lock_irqsave(&adapter->erp_lock, flags);
Swen Schilligea4a3a62010-12-02 15:16:16 +0100443 _zfcp_erp_lun_reopen(sdev, clear, id, 0);
Swen Schilligecf0c772009-11-24 16:53:58 +0100444 write_unlock_irqrestore(&adapter->erp_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445}
446
Christof Schmittfdbd1c52010-09-08 14:39:54 +0200447/**
Christof Schmittb62a8d92010-09-08 14:39:55 +0200448 * zfcp_erp_lun_shutdown - Shutdown LUN
449 * @sdev: SCSI device / LUN to shut down.
Christof Schmittfdbd1c52010-09-08 14:39:54 +0200450 * @clear: Status flags to clear.
451 * @id: Id for debug trace event.
Christof Schmittfdbd1c52010-09-08 14:39:54 +0200452 */
Swen Schilligea4a3a62010-12-02 15:16:16 +0100453void zfcp_erp_lun_shutdown(struct scsi_device *sdev, int clear, char *id)
Christof Schmittfdbd1c52010-09-08 14:39:54 +0200454{
455 int flags = ZFCP_STATUS_COMMON_RUNNING | ZFCP_STATUS_COMMON_ERP_FAILED;
Swen Schilligea4a3a62010-12-02 15:16:16 +0100456 zfcp_erp_lun_reopen(sdev, clear | flags, id);
Christof Schmittfdbd1c52010-09-08 14:39:54 +0200457}
458
459/**
Christof Schmittb62a8d92010-09-08 14:39:55 +0200460 * zfcp_erp_lun_shutdown_wait - Shutdown LUN and wait for erp completion
461 * @sdev: SCSI device / LUN to shut down.
Christof Schmittfdbd1c52010-09-08 14:39:54 +0200462 * @id: Id for debug trace event.
463 *
Christof Schmittb62a8d92010-09-08 14:39:55 +0200464 * Do not acquire a reference for the LUN when creating the ERP
Christof Schmittfdbd1c52010-09-08 14:39:54 +0200465 * action. It is safe, because this function waits for the ERP to
Christof Schmittb62a8d92010-09-08 14:39:55 +0200466 * complete first. This allows to shutdown the LUN, even when the SCSI
467 * device is in the state SDEV_DEL when scsi_device_get will fail.
Christof Schmittfdbd1c52010-09-08 14:39:54 +0200468 */
Christof Schmittb62a8d92010-09-08 14:39:55 +0200469void zfcp_erp_lun_shutdown_wait(struct scsi_device *sdev, char *id)
Christof Schmittfdbd1c52010-09-08 14:39:54 +0200470{
471 unsigned long flags;
Christof Schmittb62a8d92010-09-08 14:39:55 +0200472 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
473 struct zfcp_port *port = zfcp_sdev->port;
Christof Schmittfdbd1c52010-09-08 14:39:54 +0200474 struct zfcp_adapter *adapter = port->adapter;
475 int clear = ZFCP_STATUS_COMMON_RUNNING | ZFCP_STATUS_COMMON_ERP_FAILED;
476
477 write_lock_irqsave(&adapter->erp_lock, flags);
Swen Schilligea4a3a62010-12-02 15:16:16 +0100478 _zfcp_erp_lun_reopen(sdev, clear, id, ZFCP_STATUS_ERP_NO_REF);
Christof Schmittfdbd1c52010-09-08 14:39:54 +0200479 write_unlock_irqrestore(&adapter->erp_lock, flags);
480
481 zfcp_erp_wait(adapter);
482}
483
Christof Schmitt287ac012008-07-02 10:56:40 +0200484static int status_change_set(unsigned long mask, atomic_t *status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485{
Christof Schmitt287ac012008-07-02 10:56:40 +0200486 return (atomic_read(status) ^ mask) & mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487}
488
Andreas Herrmannf6c0e7a2006-08-02 11:05:52 +0200489static void zfcp_erp_adapter_unblock(struct zfcp_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490{
Christof Schmitt287ac012008-07-02 10:56:40 +0200491 if (status_change_set(ZFCP_STATUS_COMMON_UNBLOCKED, &adapter->status))
Swen Schilligae0904f2010-12-02 15:16:12 +0100492 zfcp_dbf_rec_run("eraubl1", &adapter->erp_action);
Peter Zijlstra805de8f42015-04-24 01:12:32 +0200493 atomic_or(ZFCP_STATUS_COMMON_UNBLOCKED, &adapter->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494}
495
Christof Schmitt287ac012008-07-02 10:56:40 +0200496static void zfcp_erp_port_unblock(struct zfcp_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497{
Christof Schmitt287ac012008-07-02 10:56:40 +0200498 if (status_change_set(ZFCP_STATUS_COMMON_UNBLOCKED, &port->status))
Swen Schilligae0904f2010-12-02 15:16:12 +0100499 zfcp_dbf_rec_run("erpubl1", &port->erp_action);
Peter Zijlstra805de8f42015-04-24 01:12:32 +0200500 atomic_or(ZFCP_STATUS_COMMON_UNBLOCKED, &port->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501}
502
Christof Schmittb62a8d92010-09-08 14:39:55 +0200503static void zfcp_erp_lun_unblock(struct scsi_device *sdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504{
Christof Schmittb62a8d92010-09-08 14:39:55 +0200505 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
506
507 if (status_change_set(ZFCP_STATUS_COMMON_UNBLOCKED, &zfcp_sdev->status))
Swen Schilligae0904f2010-12-02 15:16:12 +0100508 zfcp_dbf_rec_run("erlubl1", &sdev_to_zfcp(sdev)->erp_action);
Peter Zijlstra805de8f42015-04-24 01:12:32 +0200509 atomic_or(ZFCP_STATUS_COMMON_UNBLOCKED, &zfcp_sdev->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510}
511
Christof Schmitt287ac012008-07-02 10:56:40 +0200512static void zfcp_erp_action_to_running(struct zfcp_erp_action *erp_action)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513{
Christof Schmitt287ac012008-07-02 10:56:40 +0200514 list_move(&erp_action->list, &erp_action->adapter->erp_running_head);
Swen Schilligae0904f2010-12-02 15:16:12 +0100515 zfcp_dbf_rec_run("erator1", erp_action);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516}
517
Christof Schmitt287ac012008-07-02 10:56:40 +0200518static void zfcp_erp_strategy_check_fsfreq(struct zfcp_erp_action *act)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519{
Christof Schmitt287ac012008-07-02 10:56:40 +0200520 struct zfcp_adapter *adapter = act->adapter;
Christof Schmitte60a6d62010-02-17 11:18:49 +0100521 struct zfcp_fsf_req *req;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
Christof Schmitte60a6d62010-02-17 11:18:49 +0100523 if (!act->fsf_req_id)
Christof Schmitt287ac012008-07-02 10:56:40 +0200524 return;
525
Christof Schmittb6bd2fb2010-02-17 11:18:50 +0100526 spin_lock(&adapter->req_list->lock);
527 req = _zfcp_reqlist_find(adapter->req_list, act->fsf_req_id);
Christof Schmitte60a6d62010-02-17 11:18:49 +0100528 if (req && req->erp_action == act) {
Christof Schmitt287ac012008-07-02 10:56:40 +0200529 if (act->status & (ZFCP_STATUS_ERP_DISMISSED |
530 ZFCP_STATUS_ERP_TIMEDOUT)) {
Christof Schmitte60a6d62010-02-17 11:18:49 +0100531 req->status |= ZFCP_STATUS_FSFREQ_DISMISSED;
Swen Schilligae0904f2010-12-02 15:16:12 +0100532 zfcp_dbf_rec_run("erscf_1", act);
Christof Schmitte60a6d62010-02-17 11:18:49 +0100533 req->erp_action = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 }
Christof Schmitt287ac012008-07-02 10:56:40 +0200535 if (act->status & ZFCP_STATUS_ERP_TIMEDOUT)
Swen Schilligae0904f2010-12-02 15:16:12 +0100536 zfcp_dbf_rec_run("erscf_2", act);
Christof Schmitte60a6d62010-02-17 11:18:49 +0100537 if (req->status & ZFCP_STATUS_FSFREQ_DISMISSED)
538 act->fsf_req_id = 0;
Christof Schmitt287ac012008-07-02 10:56:40 +0200539 } else
Christof Schmitte60a6d62010-02-17 11:18:49 +0100540 act->fsf_req_id = 0;
Christof Schmittb6bd2fb2010-02-17 11:18:50 +0100541 spin_unlock(&adapter->req_list->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542}
543
Andreas Herrmannf6c0e7a2006-08-02 11:05:52 +0200544/**
Christof Schmitt287ac012008-07-02 10:56:40 +0200545 * zfcp_erp_notify - Trigger ERP action.
546 * @erp_action: ERP action to continue.
547 * @set_mask: ERP action status flags to set.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 */
Christof Schmitt287ac012008-07-02 10:56:40 +0200549void zfcp_erp_notify(struct zfcp_erp_action *erp_action, unsigned long set_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550{
551 struct zfcp_adapter *adapter = erp_action->adapter;
552 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
554 write_lock_irqsave(&adapter->erp_lock, flags);
Christof Schmitt287ac012008-07-02 10:56:40 +0200555 if (zfcp_erp_action_exists(erp_action) == ZFCP_ERP_ACTION_RUNNING) {
556 erp_action->status |= set_mask;
557 zfcp_erp_action_ready(erp_action);
558 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 write_unlock_irqrestore(&adapter->erp_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560}
561
Andreas Herrmannf6c0e7a2006-08-02 11:05:52 +0200562/**
Christof Schmitt287ac012008-07-02 10:56:40 +0200563 * zfcp_erp_timeout_handler - Trigger ERP action from timed out ERP request
564 * @data: ERP action (from timer data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 */
Christof Schmitt287ac012008-07-02 10:56:40 +0200566void zfcp_erp_timeout_handler(unsigned long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567{
Christof Schmitt287ac012008-07-02 10:56:40 +0200568 struct zfcp_erp_action *act = (struct zfcp_erp_action *) data;
569 zfcp_erp_notify(act, ZFCP_STATUS_ERP_TIMEDOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570}
571
Christof Schmitt287ac012008-07-02 10:56:40 +0200572static void zfcp_erp_memwait_handler(unsigned long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573{
Christof Schmitt287ac012008-07-02 10:56:40 +0200574 zfcp_erp_notify((struct zfcp_erp_action *)data, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575}
576
Christof Schmitt287ac012008-07-02 10:56:40 +0200577static void zfcp_erp_strategy_memwait(struct zfcp_erp_action *erp_action)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 init_timer(&erp_action->timer);
580 erp_action->timer.function = zfcp_erp_memwait_handler;
581 erp_action->timer.data = (unsigned long) erp_action;
Christof Schmitt287ac012008-07-02 10:56:40 +0200582 erp_action->timer.expires = jiffies + HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 add_timer(&erp_action->timer);
Christof Schmitt287ac012008-07-02 10:56:40 +0200584}
585
586static void _zfcp_erp_port_reopen_all(struct zfcp_adapter *adapter,
Swen Schilligea4a3a62010-12-02 15:16:16 +0100587 int clear, char *id)
Christof Schmitt287ac012008-07-02 10:56:40 +0200588{
589 struct zfcp_port *port;
590
Swen Schilligecf0c772009-11-24 16:53:58 +0100591 read_lock(&adapter->port_list_lock);
592 list_for_each_entry(port, &adapter->port_list, list)
Swen Schilligea4a3a62010-12-02 15:16:16 +0100593 _zfcp_erp_port_reopen(port, clear, id);
Swen Schilligecf0c772009-11-24 16:53:58 +0100594 read_unlock(&adapter->port_list_lock);
Christof Schmitt287ac012008-07-02 10:56:40 +0200595}
596
Christof Schmittb62a8d92010-09-08 14:39:55 +0200597static void _zfcp_erp_lun_reopen_all(struct zfcp_port *port, int clear,
Swen Schilligea4a3a62010-12-02 15:16:16 +0100598 char *id)
Christof Schmitt287ac012008-07-02 10:56:40 +0200599{
Christof Schmittb62a8d92010-09-08 14:39:55 +0200600 struct scsi_device *sdev;
Christof Schmitt287ac012008-07-02 10:56:40 +0200601
Martin Peschke924dd582013-08-22 17:45:37 +0200602 spin_lock(port->adapter->scsi_host->host_lock);
603 __shost_for_each_device(sdev, port->adapter->scsi_host)
Christof Schmittb62a8d92010-09-08 14:39:55 +0200604 if (sdev_to_zfcp(sdev)->port == port)
Swen Schilligea4a3a62010-12-02 15:16:16 +0100605 _zfcp_erp_lun_reopen(sdev, clear, id, 0);
Martin Peschke924dd582013-08-22 17:45:37 +0200606 spin_unlock(port->adapter->scsi_host->host_lock);
Christof Schmitt287ac012008-07-02 10:56:40 +0200607}
608
Christof Schmitt85600f72009-07-13 15:06:09 +0200609static void zfcp_erp_strategy_followup_failed(struct zfcp_erp_action *act)
Christof Schmitt287ac012008-07-02 10:56:40 +0200610{
Christof Schmitt287ac012008-07-02 10:56:40 +0200611 switch (act->action) {
Christof Schmitt287ac012008-07-02 10:56:40 +0200612 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
Swen Schilligea4a3a62010-12-02 15:16:16 +0100613 _zfcp_erp_adapter_reopen(act->adapter, 0, "ersff_1");
Christof Schmitt287ac012008-07-02 10:56:40 +0200614 break;
Christof Schmitt287ac012008-07-02 10:56:40 +0200615 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
Swen Schilligea4a3a62010-12-02 15:16:16 +0100616 _zfcp_erp_port_forced_reopen(act->port, 0, "ersff_2");
Christof Schmitt287ac012008-07-02 10:56:40 +0200617 break;
Christof Schmitt287ac012008-07-02 10:56:40 +0200618 case ZFCP_ERP_ACTION_REOPEN_PORT:
Swen Schilligea4a3a62010-12-02 15:16:16 +0100619 _zfcp_erp_port_reopen(act->port, 0, "ersff_3");
Christof Schmitt287ac012008-07-02 10:56:40 +0200620 break;
Christof Schmittb62a8d92010-09-08 14:39:55 +0200621 case ZFCP_ERP_ACTION_REOPEN_LUN:
Swen Schilligea4a3a62010-12-02 15:16:16 +0100622 _zfcp_erp_lun_reopen(act->sdev, 0, "ersff_4", 0);
Christof Schmitt85600f72009-07-13 15:06:09 +0200623 break;
624 }
625}
626
627static void zfcp_erp_strategy_followup_success(struct zfcp_erp_action *act)
628{
629 switch (act->action) {
630 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
Swen Schilligea4a3a62010-12-02 15:16:16 +0100631 _zfcp_erp_port_reopen_all(act->adapter, 0, "ersfs_1");
Christof Schmitt85600f72009-07-13 15:06:09 +0200632 break;
633 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
Swen Schilligea4a3a62010-12-02 15:16:16 +0100634 _zfcp_erp_port_reopen(act->port, 0, "ersfs_2");
Christof Schmitt85600f72009-07-13 15:06:09 +0200635 break;
636 case ZFCP_ERP_ACTION_REOPEN_PORT:
Swen Schilligea4a3a62010-12-02 15:16:16 +0100637 _zfcp_erp_lun_reopen_all(act->port, 0, "ersfs_3");
Christof Schmitt287ac012008-07-02 10:56:40 +0200638 break;
639 }
640}
641
642static void zfcp_erp_wakeup(struct zfcp_adapter *adapter)
643{
644 unsigned long flags;
645
Swen Schilligecf0c772009-11-24 16:53:58 +0100646 read_lock_irqsave(&adapter->erp_lock, flags);
Christof Schmitt287ac012008-07-02 10:56:40 +0200647 if (list_empty(&adapter->erp_ready_head) &&
648 list_empty(&adapter->erp_running_head)) {
Peter Zijlstra805de8f42015-04-24 01:12:32 +0200649 atomic_andnot(ZFCP_STATUS_ADAPTER_ERP_PENDING,
Christof Schmitt287ac012008-07-02 10:56:40 +0200650 &adapter->status);
651 wake_up(&adapter->erp_done_wqh);
652 }
Swen Schilligecf0c772009-11-24 16:53:58 +0100653 read_unlock_irqrestore(&adapter->erp_lock, flags);
Christof Schmitt287ac012008-07-02 10:56:40 +0200654}
655
Christof Schmitt287ac012008-07-02 10:56:40 +0200656static void zfcp_erp_enqueue_ptp_port(struct zfcp_adapter *adapter)
657{
658 struct zfcp_port *port;
659 port = zfcp_port_enqueue(adapter, adapter->peer_wwpn, 0,
660 adapter->peer_d_id);
661 if (IS_ERR(port)) /* error or port already attached */
662 return;
Swen Schilligea4a3a62010-12-02 15:16:16 +0100663 _zfcp_erp_port_reopen(port, 0, "ereptp1");
Christof Schmitt287ac012008-07-02 10:56:40 +0200664}
665
666static int zfcp_erp_adapter_strat_fsf_xconf(struct zfcp_erp_action *erp_action)
667{
668 int retries;
669 int sleep = 1;
670 struct zfcp_adapter *adapter = erp_action->adapter;
671
Peter Zijlstra805de8f42015-04-24 01:12:32 +0200672 atomic_andnot(ZFCP_STATUS_ADAPTER_XCONFIG_OK, &adapter->status);
Christof Schmitt287ac012008-07-02 10:56:40 +0200673
674 for (retries = 7; retries; retries--) {
Peter Zijlstra805de8f42015-04-24 01:12:32 +0200675 atomic_andnot(ZFCP_STATUS_ADAPTER_HOST_CON_INIT,
Christof Schmitt287ac012008-07-02 10:56:40 +0200676 &adapter->status);
677 write_lock_irq(&adapter->erp_lock);
678 zfcp_erp_action_to_running(erp_action);
679 write_unlock_irq(&adapter->erp_lock);
680 if (zfcp_fsf_exchange_config_data(erp_action)) {
Peter Zijlstra805de8f42015-04-24 01:12:32 +0200681 atomic_andnot(ZFCP_STATUS_ADAPTER_HOST_CON_INIT,
Christof Schmitt287ac012008-07-02 10:56:40 +0200682 &adapter->status);
683 return ZFCP_ERP_FAILED;
684 }
685
Christof Schmitt347c6a92009-08-18 15:43:25 +0200686 wait_event(adapter->erp_ready_wq,
687 !list_empty(&adapter->erp_ready_head));
Christof Schmitt287ac012008-07-02 10:56:40 +0200688 if (erp_action->status & ZFCP_STATUS_ERP_TIMEDOUT)
689 break;
690
691 if (!(atomic_read(&adapter->status) &
692 ZFCP_STATUS_ADAPTER_HOST_CON_INIT))
693 break;
694
695 ssleep(sleep);
696 sleep *= 2;
697 }
698
Peter Zijlstra805de8f42015-04-24 01:12:32 +0200699 atomic_andnot(ZFCP_STATUS_ADAPTER_HOST_CON_INIT,
Christof Schmitt287ac012008-07-02 10:56:40 +0200700 &adapter->status);
701
702 if (!(atomic_read(&adapter->status) & ZFCP_STATUS_ADAPTER_XCONFIG_OK))
703 return ZFCP_ERP_FAILED;
704
705 if (fc_host_port_type(adapter->scsi_host) == FC_PORTTYPE_PTP)
706 zfcp_erp_enqueue_ptp_port(adapter);
707
708 return ZFCP_ERP_SUCCEEDED;
709}
710
711static int zfcp_erp_adapter_strategy_open_fsf_xport(struct zfcp_erp_action *act)
712{
713 int ret;
714 struct zfcp_adapter *adapter = act->adapter;
715
Christof Schmitt287ac012008-07-02 10:56:40 +0200716 write_lock_irq(&adapter->erp_lock);
717 zfcp_erp_action_to_running(act);
718 write_unlock_irq(&adapter->erp_lock);
719
720 ret = zfcp_fsf_exchange_port_data(act);
721 if (ret == -EOPNOTSUPP)
722 return ZFCP_ERP_SUCCEEDED;
723 if (ret)
724 return ZFCP_ERP_FAILED;
725
Swen Schilligae0904f2010-12-02 15:16:12 +0100726 zfcp_dbf_rec_run("erasox1", act);
Christof Schmitt347c6a92009-08-18 15:43:25 +0200727 wait_event(adapter->erp_ready_wq,
728 !list_empty(&adapter->erp_ready_head));
Swen Schilligae0904f2010-12-02 15:16:12 +0100729 zfcp_dbf_rec_run("erasox2", act);
Christof Schmitt287ac012008-07-02 10:56:40 +0200730 if (act->status & ZFCP_STATUS_ERP_TIMEDOUT)
731 return ZFCP_ERP_FAILED;
732
733 return ZFCP_ERP_SUCCEEDED;
734}
735
736static int zfcp_erp_adapter_strategy_open_fsf(struct zfcp_erp_action *act)
737{
738 if (zfcp_erp_adapter_strat_fsf_xconf(act) == ZFCP_ERP_FAILED)
739 return ZFCP_ERP_FAILED;
740
741 if (zfcp_erp_adapter_strategy_open_fsf_xport(act) == ZFCP_ERP_FAILED)
742 return ZFCP_ERP_FAILED;
743
Christof Schmittc7b279a2011-02-22 19:54:40 +0100744 if (mempool_resize(act->adapter->pool.sr_data,
David Rientjes11d83362015-04-14 15:48:21 -0700745 act->adapter->stat_read_buf_num))
Christof Schmitt8d88cf32010-06-21 10:11:33 +0200746 return ZFCP_ERP_FAILED;
747
748 if (mempool_resize(act->adapter->pool.status_read_req,
David Rientjes11d83362015-04-14 15:48:21 -0700749 act->adapter->stat_read_buf_num))
Christof Schmitt8d88cf32010-06-21 10:11:33 +0200750 return ZFCP_ERP_FAILED;
751
Christof Schmitt64deb6e2010-04-30 18:09:36 +0200752 atomic_set(&act->adapter->stat_miss, act->adapter->stat_read_buf_num);
Christof Schmitt287ac012008-07-02 10:56:40 +0200753 if (zfcp_status_read_refill(act->adapter))
754 return ZFCP_ERP_FAILED;
755
756 return ZFCP_ERP_SUCCEEDED;
757}
758
Swen Schilligcf13c082009-03-02 13:09:03 +0100759static void zfcp_erp_adapter_strategy_close(struct zfcp_erp_action *act)
Christof Schmitt287ac012008-07-02 10:56:40 +0200760{
Christof Schmitt287ac012008-07-02 10:56:40 +0200761 struct zfcp_adapter *adapter = act->adapter;
762
Christof Schmitt287ac012008-07-02 10:56:40 +0200763 /* close queues to ensure that buffers are not accessed by adapter */
Swen Schillig564e1c82009-08-18 15:43:19 +0200764 zfcp_qdio_close(adapter->qdio);
Christof Schmitt287ac012008-07-02 10:56:40 +0200765 zfcp_fsf_req_dismiss_all(adapter);
766 adapter->fsf_req_seq_no = 0;
Christof Schmitt55c770f2009-08-18 15:43:12 +0200767 zfcp_fc_wka_ports_force_offline(adapter->gs);
Christof Schmittb62a8d92010-09-08 14:39:55 +0200768 /* all ports and LUNs are closed */
Swen Schilligedaed852010-09-08 14:40:01 +0200769 zfcp_erp_clear_adapter_status(adapter, ZFCP_STATUS_COMMON_OPEN);
Swen Schilligcf13c082009-03-02 13:09:03 +0100770
Peter Zijlstra805de8f42015-04-24 01:12:32 +0200771 atomic_andnot(ZFCP_STATUS_ADAPTER_XCONFIG_OK |
Swen Schilligcf13c082009-03-02 13:09:03 +0100772 ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED, &adapter->status);
773}
774
775static int zfcp_erp_adapter_strategy_open(struct zfcp_erp_action *act)
776{
777 struct zfcp_adapter *adapter = act->adapter;
778
Christof Schmitt3d63d3b2010-12-02 15:16:17 +0100779 if (zfcp_qdio_open(adapter->qdio)) {
Peter Zijlstra805de8f42015-04-24 01:12:32 +0200780 atomic_andnot(ZFCP_STATUS_ADAPTER_XCONFIG_OK |
Swen Schilligcf13c082009-03-02 13:09:03 +0100781 ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED,
782 &adapter->status);
783 return ZFCP_ERP_FAILED;
784 }
785
786 if (zfcp_erp_adapter_strategy_open_fsf(act)) {
787 zfcp_erp_adapter_strategy_close(act);
788 return ZFCP_ERP_FAILED;
789 }
790
Peter Zijlstra805de8f42015-04-24 01:12:32 +0200791 atomic_or(ZFCP_STATUS_COMMON_OPEN, &adapter->status);
Swen Schilligcf13c082009-03-02 13:09:03 +0100792
793 return ZFCP_ERP_SUCCEEDED;
Christof Schmitt287ac012008-07-02 10:56:40 +0200794}
795
796static int zfcp_erp_adapter_strategy(struct zfcp_erp_action *act)
797{
Swen Schilligcf13c082009-03-02 13:09:03 +0100798 struct zfcp_adapter *adapter = act->adapter;
Christof Schmitt287ac012008-07-02 10:56:40 +0200799
Swen Schilligcf13c082009-03-02 13:09:03 +0100800 if (atomic_read(&adapter->status) & ZFCP_STATUS_COMMON_OPEN) {
801 zfcp_erp_adapter_strategy_close(act);
802 if (act->status & ZFCP_STATUS_ERP_CLOSE_ONLY)
803 return ZFCP_ERP_EXIT;
804 }
Christof Schmitt287ac012008-07-02 10:56:40 +0200805
Swen Schilligcf13c082009-03-02 13:09:03 +0100806 if (zfcp_erp_adapter_strategy_open(act)) {
Christof Schmitt287ac012008-07-02 10:56:40 +0200807 ssleep(8);
Swen Schilligcf13c082009-03-02 13:09:03 +0100808 return ZFCP_ERP_FAILED;
809 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810
Swen Schilligcf13c082009-03-02 13:09:03 +0100811 return ZFCP_ERP_SUCCEEDED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812}
813
Christof Schmitt287ac012008-07-02 10:56:40 +0200814static int zfcp_erp_port_forced_strategy_close(struct zfcp_erp_action *act)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815{
Christof Schmitt287ac012008-07-02 10:56:40 +0200816 int retval;
817
818 retval = zfcp_fsf_close_physical_port(act);
819 if (retval == -ENOMEM)
820 return ZFCP_ERP_NOMEM;
821 act->step = ZFCP_ERP_STEP_PHYS_PORT_CLOSING;
822 if (retval)
823 return ZFCP_ERP_FAILED;
824
825 return ZFCP_ERP_CONTINUES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826}
827
Christof Schmitt287ac012008-07-02 10:56:40 +0200828static int zfcp_erp_port_forced_strategy(struct zfcp_erp_action *erp_action)
829{
830 struct zfcp_port *port = erp_action->port;
831 int status = atomic_read(&port->status);
832
833 switch (erp_action->step) {
834 case ZFCP_ERP_STEP_UNINITIALIZED:
Christof Schmitt287ac012008-07-02 10:56:40 +0200835 if ((status & ZFCP_STATUS_PORT_PHYS_OPEN) &&
836 (status & ZFCP_STATUS_COMMON_OPEN))
837 return zfcp_erp_port_forced_strategy_close(erp_action);
838 else
839 return ZFCP_ERP_FAILED;
840
841 case ZFCP_ERP_STEP_PHYS_PORT_CLOSING:
Christof Schmittddb3e0c2009-07-13 15:06:08 +0200842 if (!(status & ZFCP_STATUS_PORT_PHYS_OPEN))
Christof Schmitt287ac012008-07-02 10:56:40 +0200843 return ZFCP_ERP_SUCCEEDED;
844 }
845 return ZFCP_ERP_FAILED;
846}
847
848static int zfcp_erp_port_strategy_close(struct zfcp_erp_action *erp_action)
849{
850 int retval;
851
852 retval = zfcp_fsf_close_port(erp_action);
853 if (retval == -ENOMEM)
854 return ZFCP_ERP_NOMEM;
855 erp_action->step = ZFCP_ERP_STEP_PORT_CLOSING;
856 if (retval)
857 return ZFCP_ERP_FAILED;
858 return ZFCP_ERP_CONTINUES;
859}
860
861static int zfcp_erp_port_strategy_open_port(struct zfcp_erp_action *erp_action)
862{
863 int retval;
864
865 retval = zfcp_fsf_open_port(erp_action);
866 if (retval == -ENOMEM)
867 return ZFCP_ERP_NOMEM;
868 erp_action->step = ZFCP_ERP_STEP_PORT_OPENING;
869 if (retval)
870 return ZFCP_ERP_FAILED;
871 return ZFCP_ERP_CONTINUES;
872}
873
Christof Schmitt287ac012008-07-02 10:56:40 +0200874static int zfcp_erp_open_ptp_port(struct zfcp_erp_action *act)
875{
876 struct zfcp_adapter *adapter = act->adapter;
877 struct zfcp_port *port = act->port;
878
879 if (port->wwpn != adapter->peer_wwpn) {
Swen Schilligedaed852010-09-08 14:40:01 +0200880 zfcp_erp_set_port_status(port, ZFCP_STATUS_COMMON_ERP_FAILED);
Christof Schmitt287ac012008-07-02 10:56:40 +0200881 return ZFCP_ERP_FAILED;
882 }
883 port->d_id = adapter->peer_d_id;
Christof Schmitt287ac012008-07-02 10:56:40 +0200884 return zfcp_erp_port_strategy_open_port(act);
885}
886
887static int zfcp_erp_port_strategy_open_common(struct zfcp_erp_action *act)
888{
889 struct zfcp_adapter *adapter = act->adapter;
890 struct zfcp_port *port = act->port;
Christof Schmitt287ac012008-07-02 10:56:40 +0200891 int p_status = atomic_read(&port->status);
892
893 switch (act->step) {
894 case ZFCP_ERP_STEP_UNINITIALIZED:
895 case ZFCP_ERP_STEP_PHYS_PORT_CLOSING:
896 case ZFCP_ERP_STEP_PORT_CLOSING:
897 if (fc_host_port_type(adapter->scsi_host) == FC_PORTTYPE_PTP)
898 return zfcp_erp_open_ptp_port(act);
Christof Schmittb98478d2008-12-19 16:56:59 +0100899 if (!port->d_id) {
Christof Schmitt934aeb5872009-10-14 11:00:43 +0200900 zfcp_fc_trigger_did_lookup(port);
Christof Schmitt799b76d2009-08-18 15:43:20 +0200901 return ZFCP_ERP_EXIT;
Christof Schmitt287ac012008-07-02 10:56:40 +0200902 }
Christof Schmitt287ac012008-07-02 10:56:40 +0200903 return zfcp_erp_port_strategy_open_port(act);
904
905 case ZFCP_ERP_STEP_PORT_OPENING:
906 /* D_ID might have changed during open */
Swen Schillig5ab944f2008-10-01 12:42:17 +0200907 if (p_status & ZFCP_STATUS_COMMON_OPEN) {
Christof Schmitt934aeb5872009-10-14 11:00:43 +0200908 if (!port->d_id) {
909 zfcp_fc_trigger_did_lookup(port);
910 return ZFCP_ERP_EXIT;
Swen Schillig5ab944f2008-10-01 12:42:17 +0200911 }
Christof Schmitt934aeb5872009-10-14 11:00:43 +0200912 return ZFCP_ERP_SUCCEEDED;
Swen Schillig5ab944f2008-10-01 12:42:17 +0200913 }
Swen Schilligea460a82009-05-15 13:18:20 +0200914 if (port->d_id && !(p_status & ZFCP_STATUS_COMMON_NOESC)) {
915 port->d_id = 0;
Christof Schmittf7bd7c32010-07-08 09:53:10 +0200916 return ZFCP_ERP_FAILED;
Swen Schilligea460a82009-05-15 13:18:20 +0200917 }
918 /* fall through otherwise */
Christof Schmitt287ac012008-07-02 10:56:40 +0200919 }
920 return ZFCP_ERP_FAILED;
921}
922
Christof Schmitt287ac012008-07-02 10:56:40 +0200923static int zfcp_erp_port_strategy(struct zfcp_erp_action *erp_action)
924{
925 struct zfcp_port *port = erp_action->port;
Christof Schmitt934aeb5872009-10-14 11:00:43 +0200926 int p_status = atomic_read(&port->status);
Christof Schmitt287ac012008-07-02 10:56:40 +0200927
Christof Schmitt934aeb5872009-10-14 11:00:43 +0200928 if ((p_status & ZFCP_STATUS_COMMON_NOESC) &&
929 !(p_status & ZFCP_STATUS_COMMON_OPEN))
Swen Schillig5ab944f2008-10-01 12:42:17 +0200930 goto close_init_done;
931
Christof Schmitt287ac012008-07-02 10:56:40 +0200932 switch (erp_action->step) {
933 case ZFCP_ERP_STEP_UNINITIALIZED:
Christof Schmitt934aeb5872009-10-14 11:00:43 +0200934 if (p_status & ZFCP_STATUS_COMMON_OPEN)
Christof Schmitt287ac012008-07-02 10:56:40 +0200935 return zfcp_erp_port_strategy_close(erp_action);
936 break;
937
938 case ZFCP_ERP_STEP_PORT_CLOSING:
Christof Schmitt934aeb5872009-10-14 11:00:43 +0200939 if (p_status & ZFCP_STATUS_COMMON_OPEN)
Christof Schmitt287ac012008-07-02 10:56:40 +0200940 return ZFCP_ERP_FAILED;
941 break;
942 }
Swen Schillig5ab944f2008-10-01 12:42:17 +0200943
944close_init_done:
Christof Schmitt287ac012008-07-02 10:56:40 +0200945 if (erp_action->status & ZFCP_STATUS_ERP_CLOSE_ONLY)
946 return ZFCP_ERP_EXIT;
Christof Schmitt287ac012008-07-02 10:56:40 +0200947
Swen Schillig5ab944f2008-10-01 12:42:17 +0200948 return zfcp_erp_port_strategy_open_common(erp_action);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949}
950
Christof Schmittb62a8d92010-09-08 14:39:55 +0200951static void zfcp_erp_lun_strategy_clearstati(struct scsi_device *sdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952{
Christof Schmittb62a8d92010-09-08 14:39:55 +0200953 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
954
Peter Zijlstra805de8f42015-04-24 01:12:32 +0200955 atomic_andnot(ZFCP_STATUS_COMMON_ACCESS_DENIED,
Christof Schmittb62a8d92010-09-08 14:39:55 +0200956 &zfcp_sdev->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957}
958
Christof Schmittb62a8d92010-09-08 14:39:55 +0200959static int zfcp_erp_lun_strategy_close(struct zfcp_erp_action *erp_action)
Christof Schmitt287ac012008-07-02 10:56:40 +0200960{
Christof Schmittb62a8d92010-09-08 14:39:55 +0200961 int retval = zfcp_fsf_close_lun(erp_action);
Christof Schmitt287ac012008-07-02 10:56:40 +0200962 if (retval == -ENOMEM)
963 return ZFCP_ERP_NOMEM;
Christof Schmittb62a8d92010-09-08 14:39:55 +0200964 erp_action->step = ZFCP_ERP_STEP_LUN_CLOSING;
Christof Schmitt287ac012008-07-02 10:56:40 +0200965 if (retval)
966 return ZFCP_ERP_FAILED;
967 return ZFCP_ERP_CONTINUES;
968}
969
Christof Schmittb62a8d92010-09-08 14:39:55 +0200970static int zfcp_erp_lun_strategy_open(struct zfcp_erp_action *erp_action)
Christof Schmitt287ac012008-07-02 10:56:40 +0200971{
Christof Schmittb62a8d92010-09-08 14:39:55 +0200972 int retval = zfcp_fsf_open_lun(erp_action);
Christof Schmitt287ac012008-07-02 10:56:40 +0200973 if (retval == -ENOMEM)
974 return ZFCP_ERP_NOMEM;
Christof Schmittb62a8d92010-09-08 14:39:55 +0200975 erp_action->step = ZFCP_ERP_STEP_LUN_OPENING;
Christof Schmitt287ac012008-07-02 10:56:40 +0200976 if (retval)
977 return ZFCP_ERP_FAILED;
978 return ZFCP_ERP_CONTINUES;
979}
980
Christof Schmittb62a8d92010-09-08 14:39:55 +0200981static int zfcp_erp_lun_strategy(struct zfcp_erp_action *erp_action)
Christof Schmitt287ac012008-07-02 10:56:40 +0200982{
Christof Schmittb62a8d92010-09-08 14:39:55 +0200983 struct scsi_device *sdev = erp_action->sdev;
984 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
Christof Schmitt287ac012008-07-02 10:56:40 +0200985
986 switch (erp_action->step) {
987 case ZFCP_ERP_STEP_UNINITIALIZED:
Christof Schmittb62a8d92010-09-08 14:39:55 +0200988 zfcp_erp_lun_strategy_clearstati(sdev);
989 if (atomic_read(&zfcp_sdev->status) & ZFCP_STATUS_COMMON_OPEN)
990 return zfcp_erp_lun_strategy_close(erp_action);
Christof Schmitt287ac012008-07-02 10:56:40 +0200991 /* already closed, fall through */
Christof Schmittb62a8d92010-09-08 14:39:55 +0200992 case ZFCP_ERP_STEP_LUN_CLOSING:
993 if (atomic_read(&zfcp_sdev->status) & ZFCP_STATUS_COMMON_OPEN)
Christof Schmitt287ac012008-07-02 10:56:40 +0200994 return ZFCP_ERP_FAILED;
995 if (erp_action->status & ZFCP_STATUS_ERP_CLOSE_ONLY)
996 return ZFCP_ERP_EXIT;
Christof Schmittb62a8d92010-09-08 14:39:55 +0200997 return zfcp_erp_lun_strategy_open(erp_action);
Christof Schmitt287ac012008-07-02 10:56:40 +0200998
Christof Schmittb62a8d92010-09-08 14:39:55 +0200999 case ZFCP_ERP_STEP_LUN_OPENING:
1000 if (atomic_read(&zfcp_sdev->status) & ZFCP_STATUS_COMMON_OPEN)
Christof Schmitt287ac012008-07-02 10:56:40 +02001001 return ZFCP_ERP_SUCCEEDED;
1002 }
1003 return ZFCP_ERP_FAILED;
1004}
1005
Christof Schmittb62a8d92010-09-08 14:39:55 +02001006static int zfcp_erp_strategy_check_lun(struct scsi_device *sdev, int result)
Christof Schmitt287ac012008-07-02 10:56:40 +02001007{
Christof Schmittb62a8d92010-09-08 14:39:55 +02001008 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
1009
Christof Schmitt287ac012008-07-02 10:56:40 +02001010 switch (result) {
1011 case ZFCP_ERP_SUCCEEDED :
Christof Schmittb62a8d92010-09-08 14:39:55 +02001012 atomic_set(&zfcp_sdev->erp_counter, 0);
1013 zfcp_erp_lun_unblock(sdev);
Christof Schmitt287ac012008-07-02 10:56:40 +02001014 break;
1015 case ZFCP_ERP_FAILED :
Christof Schmittb62a8d92010-09-08 14:39:55 +02001016 atomic_inc(&zfcp_sdev->erp_counter);
1017 if (atomic_read(&zfcp_sdev->erp_counter) > ZFCP_MAX_ERPS) {
1018 dev_err(&zfcp_sdev->port->adapter->ccw_device->dev,
1019 "ERP failed for LUN 0x%016Lx on "
Christof Schmittff3b24f2008-10-01 12:42:15 +02001020 "port 0x%016Lx\n",
Christof Schmittb62a8d92010-09-08 14:39:55 +02001021 (unsigned long long)zfcp_scsi_dev_lun(sdev),
1022 (unsigned long long)zfcp_sdev->port->wwpn);
Swen Schilligedaed852010-09-08 14:40:01 +02001023 zfcp_erp_set_lun_status(sdev,
1024 ZFCP_STATUS_COMMON_ERP_FAILED);
Christof Schmittff3b24f2008-10-01 12:42:15 +02001025 }
Christof Schmitt287ac012008-07-02 10:56:40 +02001026 break;
1027 }
1028
Christof Schmittb62a8d92010-09-08 14:39:55 +02001029 if (atomic_read(&zfcp_sdev->status) & ZFCP_STATUS_COMMON_ERP_FAILED) {
1030 zfcp_erp_lun_block(sdev, 0);
Christof Schmitt287ac012008-07-02 10:56:40 +02001031 result = ZFCP_ERP_EXIT;
1032 }
1033 return result;
1034}
1035
1036static int zfcp_erp_strategy_check_port(struct zfcp_port *port, int result)
1037{
1038 switch (result) {
1039 case ZFCP_ERP_SUCCEEDED :
1040 atomic_set(&port->erp_counter, 0);
1041 zfcp_erp_port_unblock(port);
1042 break;
1043
1044 case ZFCP_ERP_FAILED :
1045 if (atomic_read(&port->status) & ZFCP_STATUS_COMMON_NOESC) {
1046 zfcp_erp_port_block(port, 0);
1047 result = ZFCP_ERP_EXIT;
1048 }
1049 atomic_inc(&port->erp_counter);
Christof Schmittff3b24f2008-10-01 12:42:15 +02001050 if (atomic_read(&port->erp_counter) > ZFCP_MAX_ERPS) {
1051 dev_err(&port->adapter->ccw_device->dev,
1052 "ERP failed for remote port 0x%016Lx\n",
Swen Schillig7ba58c92008-10-01 12:42:18 +02001053 (unsigned long long)port->wwpn);
Swen Schilligedaed852010-09-08 14:40:01 +02001054 zfcp_erp_set_port_status(port,
1055 ZFCP_STATUS_COMMON_ERP_FAILED);
Christof Schmittff3b24f2008-10-01 12:42:15 +02001056 }
Christof Schmitt287ac012008-07-02 10:56:40 +02001057 break;
1058 }
1059
1060 if (atomic_read(&port->status) & ZFCP_STATUS_COMMON_ERP_FAILED) {
1061 zfcp_erp_port_block(port, 0);
1062 result = ZFCP_ERP_EXIT;
1063 }
1064 return result;
1065}
1066
1067static int zfcp_erp_strategy_check_adapter(struct zfcp_adapter *adapter,
1068 int result)
1069{
1070 switch (result) {
1071 case ZFCP_ERP_SUCCEEDED :
1072 atomic_set(&adapter->erp_counter, 0);
1073 zfcp_erp_adapter_unblock(adapter);
1074 break;
1075
1076 case ZFCP_ERP_FAILED :
1077 atomic_inc(&adapter->erp_counter);
Christof Schmittff3b24f2008-10-01 12:42:15 +02001078 if (atomic_read(&adapter->erp_counter) > ZFCP_MAX_ERPS) {
1079 dev_err(&adapter->ccw_device->dev,
1080 "ERP cannot recover an error "
1081 "on the FCP device\n");
Swen Schilligedaed852010-09-08 14:40:01 +02001082 zfcp_erp_set_adapter_status(adapter,
1083 ZFCP_STATUS_COMMON_ERP_FAILED);
Christof Schmittff3b24f2008-10-01 12:42:15 +02001084 }
Christof Schmitt287ac012008-07-02 10:56:40 +02001085 break;
1086 }
1087
1088 if (atomic_read(&adapter->status) & ZFCP_STATUS_COMMON_ERP_FAILED) {
1089 zfcp_erp_adapter_block(adapter, 0);
1090 result = ZFCP_ERP_EXIT;
1091 }
1092 return result;
1093}
1094
1095static int zfcp_erp_strategy_check_target(struct zfcp_erp_action *erp_action,
1096 int result)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097{
1098 struct zfcp_adapter *adapter = erp_action->adapter;
1099 struct zfcp_port *port = erp_action->port;
Christof Schmittb62a8d92010-09-08 14:39:55 +02001100 struct scsi_device *sdev = erp_action->sdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 switch (erp_action->action) {
1103
Christof Schmittb62a8d92010-09-08 14:39:55 +02001104 case ZFCP_ERP_ACTION_REOPEN_LUN:
1105 result = zfcp_erp_strategy_check_lun(sdev, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 break;
1107
1108 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
1109 case ZFCP_ERP_ACTION_REOPEN_PORT:
1110 result = zfcp_erp_strategy_check_port(port, result);
1111 break;
1112
1113 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
1114 result = zfcp_erp_strategy_check_adapter(adapter, result);
1115 break;
1116 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 return result;
1118}
1119
Christof Schmitt287ac012008-07-02 10:56:40 +02001120static int zfcp_erp_strat_change_det(atomic_t *target_status, u32 erp_status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121{
Christof Schmitt287ac012008-07-02 10:56:40 +02001122 int status = atomic_read(target_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123
Christof Schmitt287ac012008-07-02 10:56:40 +02001124 if ((status & ZFCP_STATUS_COMMON_RUNNING) &&
1125 (erp_status & ZFCP_STATUS_ERP_CLOSE_ONLY))
1126 return 1; /* take it online */
1127
1128 if (!(status & ZFCP_STATUS_COMMON_RUNNING) &&
1129 !(erp_status & ZFCP_STATUS_ERP_CLOSE_ONLY))
1130 return 1; /* take it offline */
1131
1132 return 0;
1133}
1134
1135static int zfcp_erp_strategy_statechange(struct zfcp_erp_action *act, int ret)
1136{
1137 int action = act->action;
1138 struct zfcp_adapter *adapter = act->adapter;
1139 struct zfcp_port *port = act->port;
Christof Schmittb62a8d92010-09-08 14:39:55 +02001140 struct scsi_device *sdev = act->sdev;
1141 struct zfcp_scsi_dev *zfcp_sdev;
Christof Schmitt287ac012008-07-02 10:56:40 +02001142 u32 erp_status = act->status;
1143
1144 switch (action) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
Christof Schmitt287ac012008-07-02 10:56:40 +02001146 if (zfcp_erp_strat_change_det(&adapter->status, erp_status)) {
1147 _zfcp_erp_adapter_reopen(adapter,
1148 ZFCP_STATUS_COMMON_ERP_FAILED,
Swen Schilligea4a3a62010-12-02 15:16:16 +01001149 "ersscg1");
Christof Schmitt287ac012008-07-02 10:56:40 +02001150 return ZFCP_ERP_EXIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 }
1152 break;
1153
1154 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
1155 case ZFCP_ERP_ACTION_REOPEN_PORT:
Christof Schmitt287ac012008-07-02 10:56:40 +02001156 if (zfcp_erp_strat_change_det(&port->status, erp_status)) {
1157 _zfcp_erp_port_reopen(port,
1158 ZFCP_STATUS_COMMON_ERP_FAILED,
Swen Schilligea4a3a62010-12-02 15:16:16 +01001159 "ersscg2");
Christof Schmitt287ac012008-07-02 10:56:40 +02001160 return ZFCP_ERP_EXIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 }
1162 break;
1163
Christof Schmittb62a8d92010-09-08 14:39:55 +02001164 case ZFCP_ERP_ACTION_REOPEN_LUN:
1165 zfcp_sdev = sdev_to_zfcp(sdev);
1166 if (zfcp_erp_strat_change_det(&zfcp_sdev->status, erp_status)) {
1167 _zfcp_erp_lun_reopen(sdev,
1168 ZFCP_STATUS_COMMON_ERP_FAILED,
Swen Schilligea4a3a62010-12-02 15:16:16 +01001169 "ersscg3", 0);
Christof Schmitt287ac012008-07-02 10:56:40 +02001170 return ZFCP_ERP_EXIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 }
1172 break;
1173 }
Christof Schmitt287ac012008-07-02 10:56:40 +02001174 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175}
1176
Christof Schmitt287ac012008-07-02 10:56:40 +02001177static void zfcp_erp_action_dequeue(struct zfcp_erp_action *erp_action)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178{
Christof Schmitt287ac012008-07-02 10:56:40 +02001179 struct zfcp_adapter *adapter = erp_action->adapter;
Christof Schmittb62a8d92010-09-08 14:39:55 +02001180 struct zfcp_scsi_dev *zfcp_sdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181
Christof Schmitt287ac012008-07-02 10:56:40 +02001182 adapter->erp_total_count--;
1183 if (erp_action->status & ZFCP_STATUS_ERP_LOWMEM) {
1184 adapter->erp_low_mem_count--;
1185 erp_action->status &= ~ZFCP_STATUS_ERP_LOWMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 }
1187
Christof Schmitt287ac012008-07-02 10:56:40 +02001188 list_del(&erp_action->list);
Swen Schilligae0904f2010-12-02 15:16:12 +01001189 zfcp_dbf_rec_run("eractd1", erp_action);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190
Christof Schmitt287ac012008-07-02 10:56:40 +02001191 switch (erp_action->action) {
Christof Schmittb62a8d92010-09-08 14:39:55 +02001192 case ZFCP_ERP_ACTION_REOPEN_LUN:
1193 zfcp_sdev = sdev_to_zfcp(erp_action->sdev);
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001194 atomic_andnot(ZFCP_STATUS_COMMON_ERP_INUSE,
Christof Schmittb62a8d92010-09-08 14:39:55 +02001195 &zfcp_sdev->status);
Christof Schmitt287ac012008-07-02 10:56:40 +02001196 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197
Christof Schmitt287ac012008-07-02 10:56:40 +02001198 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
1199 case ZFCP_ERP_ACTION_REOPEN_PORT:
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001200 atomic_andnot(ZFCP_STATUS_COMMON_ERP_INUSE,
Christof Schmitt287ac012008-07-02 10:56:40 +02001201 &erp_action->port->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 break;
Christof Schmitt287ac012008-07-02 10:56:40 +02001203
1204 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001205 atomic_andnot(ZFCP_STATUS_COMMON_ERP_INUSE,
Christof Schmitt287ac012008-07-02 10:56:40 +02001206 &erp_action->adapter->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 break;
1208 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209}
1210
Steffen Maier2a940b82016-12-09 17:16:33 +01001211/**
1212 * zfcp_erp_try_rport_unblock - unblock rport if no more/new recovery
1213 * @port: zfcp_port whose fc_rport we should try to unblock
1214 */
1215static void zfcp_erp_try_rport_unblock(struct zfcp_port *port)
1216{
1217 unsigned long flags;
1218 struct zfcp_adapter *adapter = port->adapter;
1219 int port_status;
1220 struct Scsi_Host *shost = adapter->scsi_host;
1221 struct scsi_device *sdev;
1222
1223 write_lock_irqsave(&adapter->erp_lock, flags);
1224 port_status = atomic_read(&port->status);
1225 if ((port_status & ZFCP_STATUS_COMMON_UNBLOCKED) == 0 ||
1226 (port_status & (ZFCP_STATUS_COMMON_ERP_INUSE |
1227 ZFCP_STATUS_COMMON_ERP_FAILED)) != 0) {
1228 /* new ERP of severity >= port triggered elsewhere meanwhile or
1229 * local link down (adapter erp_failed but not clear unblock)
1230 */
1231 zfcp_dbf_rec_run_lvl(4, "ertru_p", &port->erp_action);
1232 write_unlock_irqrestore(&adapter->erp_lock, flags);
1233 return;
1234 }
1235 spin_lock(shost->host_lock);
1236 __shost_for_each_device(sdev, shost) {
1237 struct zfcp_scsi_dev *zsdev = sdev_to_zfcp(sdev);
1238 int lun_status;
1239
1240 if (zsdev->port != port)
1241 continue;
1242 /* LUN under port of interest */
1243 lun_status = atomic_read(&zsdev->status);
1244 if ((lun_status & ZFCP_STATUS_COMMON_ERP_FAILED) != 0)
1245 continue; /* unblock rport despite failed LUNs */
1246 /* LUN recovery not given up yet [maybe follow-up pending] */
1247 if ((lun_status & ZFCP_STATUS_COMMON_UNBLOCKED) == 0 ||
1248 (lun_status & ZFCP_STATUS_COMMON_ERP_INUSE) != 0) {
1249 /* LUN blocked:
1250 * not yet unblocked [LUN recovery pending]
1251 * or meanwhile blocked [new LUN recovery triggered]
1252 */
1253 zfcp_dbf_rec_run_lvl(4, "ertru_l", &zsdev->erp_action);
1254 spin_unlock(shost->host_lock);
1255 write_unlock_irqrestore(&adapter->erp_lock, flags);
1256 return;
1257 }
1258 }
1259 /* now port has no child or all children have completed recovery,
1260 * and no ERP of severity >= port was meanwhile triggered elsewhere
1261 */
1262 zfcp_scsi_schedule_rport_register(port);
1263 spin_unlock(shost->host_lock);
1264 write_unlock_irqrestore(&adapter->erp_lock, flags);
1265}
1266
Christof Schmitt287ac012008-07-02 10:56:40 +02001267static void zfcp_erp_action_cleanup(struct zfcp_erp_action *act, int result)
Maxim Shchetyninaef4a982005-09-13 21:51:16 +02001268{
Christof Schmitt287ac012008-07-02 10:56:40 +02001269 struct zfcp_adapter *adapter = act->adapter;
1270 struct zfcp_port *port = act->port;
Christof Schmittb62a8d92010-09-08 14:39:55 +02001271 struct scsi_device *sdev = act->sdev;
Maxim Shchetyninaef4a982005-09-13 21:51:16 +02001272
Christof Schmitt287ac012008-07-02 10:56:40 +02001273 switch (act->action) {
Christof Schmittb62a8d92010-09-08 14:39:55 +02001274 case ZFCP_ERP_ACTION_REOPEN_LUN:
Christof Schmittfdbd1c52010-09-08 14:39:54 +02001275 if (!(act->status & ZFCP_STATUS_ERP_NO_REF))
Christof Schmittb62a8d92010-09-08 14:39:55 +02001276 scsi_device_put(sdev);
Steffen Maier2a940b82016-12-09 17:16:33 +01001277 zfcp_erp_try_rport_unblock(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 break;
Christof Schmitt287ac012008-07-02 10:56:40 +02001279
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 case ZFCP_ERP_ACTION_REOPEN_PORT:
Steffen Maier4eeaa4f2016-08-10 18:30:46 +02001281 /* This switch case might also happen after a forced reopen
1282 * was successfully done and thus overwritten with a new
1283 * non-forced reopen at `ersfs_2'. In this case, we must not
1284 * do the clean-up of the non-forced version.
1285 */
1286 if (act->step != ZFCP_ERP_STEP_UNINITIALIZED)
1287 if (result == ZFCP_ERP_SUCCEEDED)
Steffen Maier2a940b82016-12-09 17:16:33 +01001288 zfcp_erp_try_rport_unblock(port);
Christof Schmitt57676202010-07-08 09:53:05 +02001289 /* fall through */
1290 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
Christof Schmitt615f59e2010-02-17 11:18:56 +01001291 put_device(&port->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 break;
Christof Schmitt287ac012008-07-02 10:56:40 +02001293
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
Christof Schmitta2fa0ae2009-03-02 13:09:08 +01001295 if (result == ZFCP_ERP_SUCCEEDED) {
Christof Schmittbd43a42b72008-12-25 13:38:50 +01001296 register_service_level(&adapter->service_level);
Steffen Maier43f60cb2012-09-04 15:23:35 +02001297 zfcp_fc_conditional_port_scan(adapter);
Christof Schmitt038d9442011-02-22 19:54:48 +01001298 queue_work(adapter->work_queue, &adapter->ns_up_work);
Christof Schmitta2fa0ae2009-03-02 13:09:08 +01001299 } else
1300 unregister_service_level(&adapter->service_level);
Christof Schmitt038d9442011-02-22 19:54:48 +01001301
Swen Schilligf3450c72009-11-24 16:53:59 +01001302 kref_put(&adapter->ref, zfcp_adapter_release);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 }
1305}
1306
Christof Schmitt287ac012008-07-02 10:56:40 +02001307static int zfcp_erp_strategy_do_action(struct zfcp_erp_action *erp_action)
1308{
1309 switch (erp_action->action) {
1310 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
1311 return zfcp_erp_adapter_strategy(erp_action);
1312 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
1313 return zfcp_erp_port_forced_strategy(erp_action);
1314 case ZFCP_ERP_ACTION_REOPEN_PORT:
1315 return zfcp_erp_port_strategy(erp_action);
Christof Schmittb62a8d92010-09-08 14:39:55 +02001316 case ZFCP_ERP_ACTION_REOPEN_LUN:
1317 return zfcp_erp_lun_strategy(erp_action);
Christof Schmitt287ac012008-07-02 10:56:40 +02001318 }
1319 return ZFCP_ERP_FAILED;
1320}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321
Christof Schmitt287ac012008-07-02 10:56:40 +02001322static int zfcp_erp_strategy(struct zfcp_erp_action *erp_action)
1323{
1324 int retval;
Christof Schmitt287ac012008-07-02 10:56:40 +02001325 unsigned long flags;
Swen Schilligecf0c772009-11-24 16:53:58 +01001326 struct zfcp_adapter *adapter = erp_action->adapter;
Christof Schmitt287ac012008-07-02 10:56:40 +02001327
Swen Schilligf3450c72009-11-24 16:53:59 +01001328 kref_get(&adapter->ref);
Christof Schmitt287ac012008-07-02 10:56:40 +02001329
Swen Schilligf3450c72009-11-24 16:53:59 +01001330 write_lock_irqsave(&adapter->erp_lock, flags);
Christof Schmitt287ac012008-07-02 10:56:40 +02001331 zfcp_erp_strategy_check_fsfreq(erp_action);
1332
1333 if (erp_action->status & ZFCP_STATUS_ERP_DISMISSED) {
1334 zfcp_erp_action_dequeue(erp_action);
1335 retval = ZFCP_ERP_DISMISSED;
1336 goto unlock;
1337 }
1338
Christof Schmitt9c785d92010-07-08 09:53:09 +02001339 if (erp_action->status & ZFCP_STATUS_ERP_TIMEDOUT) {
1340 retval = ZFCP_ERP_FAILED;
1341 goto check_target;
1342 }
1343
Christof Schmitt287ac012008-07-02 10:56:40 +02001344 zfcp_erp_action_to_running(erp_action);
1345
1346 /* no lock to allow for blocking operations */
Swen Schilligecf0c772009-11-24 16:53:58 +01001347 write_unlock_irqrestore(&adapter->erp_lock, flags);
Christof Schmitt287ac012008-07-02 10:56:40 +02001348 retval = zfcp_erp_strategy_do_action(erp_action);
Swen Schilligecf0c772009-11-24 16:53:58 +01001349 write_lock_irqsave(&adapter->erp_lock, flags);
Christof Schmitt287ac012008-07-02 10:56:40 +02001350
1351 if (erp_action->status & ZFCP_STATUS_ERP_DISMISSED)
1352 retval = ZFCP_ERP_CONTINUES;
1353
1354 switch (retval) {
1355 case ZFCP_ERP_NOMEM:
1356 if (!(erp_action->status & ZFCP_STATUS_ERP_LOWMEM)) {
1357 ++adapter->erp_low_mem_count;
1358 erp_action->status |= ZFCP_STATUS_ERP_LOWMEM;
1359 }
1360 if (adapter->erp_total_count == adapter->erp_low_mem_count)
Swen Schilligea4a3a62010-12-02 15:16:16 +01001361 _zfcp_erp_adapter_reopen(adapter, 0, "erstgy1");
Christof Schmitt287ac012008-07-02 10:56:40 +02001362 else {
1363 zfcp_erp_strategy_memwait(erp_action);
1364 retval = ZFCP_ERP_CONTINUES;
1365 }
1366 goto unlock;
1367
1368 case ZFCP_ERP_CONTINUES:
1369 if (erp_action->status & ZFCP_STATUS_ERP_LOWMEM) {
1370 --adapter->erp_low_mem_count;
1371 erp_action->status &= ~ZFCP_STATUS_ERP_LOWMEM;
1372 }
1373 goto unlock;
1374 }
1375
Christof Schmitt9c785d92010-07-08 09:53:09 +02001376check_target:
Christof Schmitt287ac012008-07-02 10:56:40 +02001377 retval = zfcp_erp_strategy_check_target(erp_action, retval);
1378 zfcp_erp_action_dequeue(erp_action);
1379 retval = zfcp_erp_strategy_statechange(erp_action, retval);
1380 if (retval == ZFCP_ERP_EXIT)
1381 goto unlock;
Christof Schmitt85600f72009-07-13 15:06:09 +02001382 if (retval == ZFCP_ERP_SUCCEEDED)
1383 zfcp_erp_strategy_followup_success(erp_action);
1384 if (retval == ZFCP_ERP_FAILED)
1385 zfcp_erp_strategy_followup_failed(erp_action);
Christof Schmitt287ac012008-07-02 10:56:40 +02001386
1387 unlock:
Swen Schilligecf0c772009-11-24 16:53:58 +01001388 write_unlock_irqrestore(&adapter->erp_lock, flags);
Christof Schmitt287ac012008-07-02 10:56:40 +02001389
1390 if (retval != ZFCP_ERP_CONTINUES)
1391 zfcp_erp_action_cleanup(erp_action, retval);
1392
Swen Schilligf3450c72009-11-24 16:53:59 +01001393 kref_put(&adapter->ref, zfcp_adapter_release);
Christof Schmitt287ac012008-07-02 10:56:40 +02001394 return retval;
1395}
1396
1397static int zfcp_erp_thread(void *data)
1398{
1399 struct zfcp_adapter *adapter = (struct zfcp_adapter *) data;
1400 struct list_head *next;
1401 struct zfcp_erp_action *act;
1402 unsigned long flags;
1403
Christof Schmitt347c6a92009-08-18 15:43:25 +02001404 for (;;) {
Christof Schmitt347c6a92009-08-18 15:43:25 +02001405 wait_event_interruptible(adapter->erp_ready_wq,
1406 !list_empty(&adapter->erp_ready_head) ||
1407 kthread_should_stop());
Swen Schillig94ab4b32009-04-17 15:08:06 +02001408
Christof Schmitt347c6a92009-08-18 15:43:25 +02001409 if (kthread_should_stop())
1410 break;
1411
Christof Schmitt287ac012008-07-02 10:56:40 +02001412 write_lock_irqsave(&adapter->erp_lock, flags);
1413 next = adapter->erp_ready_head.next;
1414 write_unlock_irqrestore(&adapter->erp_lock, flags);
1415
1416 if (next != &adapter->erp_ready_head) {
1417 act = list_entry(next, struct zfcp_erp_action, list);
1418
1419 /* there is more to come after dismission, no notify */
1420 if (zfcp_erp_strategy(act) != ZFCP_ERP_DISMISSED)
1421 zfcp_erp_wakeup(adapter);
1422 }
Christof Schmitt287ac012008-07-02 10:56:40 +02001423 }
1424
Christof Schmitt287ac012008-07-02 10:56:40 +02001425 return 0;
1426}
1427
1428/**
1429 * zfcp_erp_thread_setup - Start ERP thread for adapter
1430 * @adapter: Adapter to start the ERP thread for
1431 *
1432 * Returns 0 on success or error code from kernel_thread()
1433 */
1434int zfcp_erp_thread_setup(struct zfcp_adapter *adapter)
1435{
Christof Schmitt347c6a92009-08-18 15:43:25 +02001436 struct task_struct *thread;
Christof Schmitt287ac012008-07-02 10:56:40 +02001437
Christof Schmitt347c6a92009-08-18 15:43:25 +02001438 thread = kthread_run(zfcp_erp_thread, adapter, "zfcperp%s",
1439 dev_name(&adapter->ccw_device->dev));
1440 if (IS_ERR(thread)) {
Christof Schmitt287ac012008-07-02 10:56:40 +02001441 dev_err(&adapter->ccw_device->dev,
Christof Schmittff3b24f2008-10-01 12:42:15 +02001442 "Creating an ERP thread for the FCP device failed.\n");
Christof Schmitt347c6a92009-08-18 15:43:25 +02001443 return PTR_ERR(thread);
Christof Schmitt287ac012008-07-02 10:56:40 +02001444 }
Christof Schmitt347c6a92009-08-18 15:43:25 +02001445
1446 adapter->erp_thread = thread;
Christof Schmitt287ac012008-07-02 10:56:40 +02001447 return 0;
1448}
1449
1450/**
1451 * zfcp_erp_thread_kill - Stop ERP thread.
1452 * @adapter: Adapter where the ERP thread should be stopped.
1453 *
1454 * The caller of this routine ensures that the specified adapter has
1455 * been shut down and that this operation has been completed. Thus,
1456 * there are no pending erp_actions which would need to be handled
1457 * here.
1458 */
1459void zfcp_erp_thread_kill(struct zfcp_adapter *adapter)
1460{
Christof Schmitt347c6a92009-08-18 15:43:25 +02001461 kthread_stop(adapter->erp_thread);
1462 adapter->erp_thread = NULL;
Christof Schmitt143bb6b2009-08-18 15:43:27 +02001463 WARN_ON(!list_empty(&adapter->erp_ready_head));
1464 WARN_ON(!list_empty(&adapter->erp_running_head));
Christof Schmitt287ac012008-07-02 10:56:40 +02001465}
1466
1467/**
Christof Schmitt287ac012008-07-02 10:56:40 +02001468 * zfcp_erp_wait - wait for completion of error recovery on an adapter
1469 * @adapter: adapter for which to wait for completion of its error recovery
1470 */
1471void zfcp_erp_wait(struct zfcp_adapter *adapter)
1472{
1473 wait_event(adapter->erp_done_wqh,
1474 !(atomic_read(&adapter->status) &
1475 ZFCP_STATUS_ADAPTER_ERP_PENDING));
1476}
1477
1478/**
Swen Schilligedaed852010-09-08 14:40:01 +02001479 * zfcp_erp_set_adapter_status - set adapter status bits
Christof Schmitt287ac012008-07-02 10:56:40 +02001480 * @adapter: adapter to change the status
Christof Schmitt287ac012008-07-02 10:56:40 +02001481 * @mask: status bits to change
Christof Schmitt287ac012008-07-02 10:56:40 +02001482 *
Christof Schmittb62a8d92010-09-08 14:39:55 +02001483 * Changes in common status bits are propagated to attached ports and LUNs.
Christof Schmitt287ac012008-07-02 10:56:40 +02001484 */
Swen Schilligedaed852010-09-08 14:40:01 +02001485void zfcp_erp_set_adapter_status(struct zfcp_adapter *adapter, u32 mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 struct zfcp_port *port;
Swen Schilligedaed852010-09-08 14:40:01 +02001488 struct scsi_device *sdev;
Swen Schilligecf0c772009-11-24 16:53:58 +01001489 unsigned long flags;
Christof Schmitt287ac012008-07-02 10:56:40 +02001490 u32 common_mask = mask & ZFCP_COMMON_FLAGS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001492 atomic_or(mask, &adapter->status);
Christof Schmitt287ac012008-07-02 10:56:40 +02001493
Swen Schilligedaed852010-09-08 14:40:01 +02001494 if (!common_mask)
1495 return;
1496
1497 read_lock_irqsave(&adapter->port_list_lock, flags);
1498 list_for_each_entry(port, &adapter->port_list, list)
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001499 atomic_or(common_mask, &port->status);
Swen Schilligedaed852010-09-08 14:40:01 +02001500 read_unlock_irqrestore(&adapter->port_list_lock, flags);
1501
Martin Peschke924dd582013-08-22 17:45:37 +02001502 spin_lock_irqsave(adapter->scsi_host->host_lock, flags);
1503 __shost_for_each_device(sdev, adapter->scsi_host)
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001504 atomic_or(common_mask, &sdev_to_zfcp(sdev)->status);
Martin Peschke924dd582013-08-22 17:45:37 +02001505 spin_unlock_irqrestore(adapter->scsi_host->host_lock, flags);
Swen Schilligedaed852010-09-08 14:40:01 +02001506}
1507
1508/**
1509 * zfcp_erp_clear_adapter_status - clear adapter status bits
1510 * @adapter: adapter to change the status
1511 * @mask: status bits to change
1512 *
1513 * Changes in common status bits are propagated to attached ports and LUNs.
1514 */
1515void zfcp_erp_clear_adapter_status(struct zfcp_adapter *adapter, u32 mask)
1516{
1517 struct zfcp_port *port;
1518 struct scsi_device *sdev;
1519 unsigned long flags;
1520 u32 common_mask = mask & ZFCP_COMMON_FLAGS;
1521 u32 clear_counter = mask & ZFCP_STATUS_COMMON_ERP_FAILED;
1522
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001523 atomic_andnot(mask, &adapter->status);
Swen Schilligedaed852010-09-08 14:40:01 +02001524
1525 if (!common_mask)
1526 return;
1527
1528 if (clear_counter)
1529 atomic_set(&adapter->erp_counter, 0);
1530
1531 read_lock_irqsave(&adapter->port_list_lock, flags);
1532 list_for_each_entry(port, &adapter->port_list, list) {
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001533 atomic_andnot(common_mask, &port->status);
Swen Schilligedaed852010-09-08 14:40:01 +02001534 if (clear_counter)
1535 atomic_set(&port->erp_counter, 0);
1536 }
1537 read_unlock_irqrestore(&adapter->port_list_lock, flags);
1538
Martin Peschke924dd582013-08-22 17:45:37 +02001539 spin_lock_irqsave(adapter->scsi_host->host_lock, flags);
1540 __shost_for_each_device(sdev, adapter->scsi_host) {
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001541 atomic_andnot(common_mask, &sdev_to_zfcp(sdev)->status);
Swen Schilligedaed852010-09-08 14:40:01 +02001542 if (clear_counter)
1543 atomic_set(&sdev_to_zfcp(sdev)->erp_counter, 0);
Swen Schilligecf0c772009-11-24 16:53:58 +01001544 }
Martin Peschke924dd582013-08-22 17:45:37 +02001545 spin_unlock_irqrestore(adapter->scsi_host->host_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546}
1547
Christof Schmitt287ac012008-07-02 10:56:40 +02001548/**
Swen Schilligedaed852010-09-08 14:40:01 +02001549 * zfcp_erp_set_port_status - set port status bits
1550 * @port: port to change the status
Christof Schmitt287ac012008-07-02 10:56:40 +02001551 * @mask: status bits to change
Christof Schmitt287ac012008-07-02 10:56:40 +02001552 *
Christof Schmittb62a8d92010-09-08 14:39:55 +02001553 * Changes in common status bits are propagated to attached LUNs.
Christof Schmitt287ac012008-07-02 10:56:40 +02001554 */
Swen Schilligedaed852010-09-08 14:40:01 +02001555void zfcp_erp_set_port_status(struct zfcp_port *port, u32 mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556{
Christof Schmittb62a8d92010-09-08 14:39:55 +02001557 struct scsi_device *sdev;
Christof Schmitt287ac012008-07-02 10:56:40 +02001558 u32 common_mask = mask & ZFCP_COMMON_FLAGS;
Martin Peschke924dd582013-08-22 17:45:37 +02001559 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001561 atomic_or(mask, &port->status);
Christof Schmitt287ac012008-07-02 10:56:40 +02001562
Swen Schilligedaed852010-09-08 14:40:01 +02001563 if (!common_mask)
1564 return;
1565
Martin Peschke924dd582013-08-22 17:45:37 +02001566 spin_lock_irqsave(port->adapter->scsi_host->host_lock, flags);
1567 __shost_for_each_device(sdev, port->adapter->scsi_host)
Swen Schilligedaed852010-09-08 14:40:01 +02001568 if (sdev_to_zfcp(sdev)->port == port)
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001569 atomic_or(common_mask,
Swen Schilligedaed852010-09-08 14:40:01 +02001570 &sdev_to_zfcp(sdev)->status);
Martin Peschke924dd582013-08-22 17:45:37 +02001571 spin_unlock_irqrestore(port->adapter->scsi_host->host_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572}
1573
Christof Schmitt287ac012008-07-02 10:56:40 +02001574/**
Swen Schilligedaed852010-09-08 14:40:01 +02001575 * zfcp_erp_clear_port_status - clear port status bits
1576 * @port: adapter to change the status
Christof Schmitt287ac012008-07-02 10:56:40 +02001577 * @mask: status bits to change
Swen Schilligedaed852010-09-08 14:40:01 +02001578 *
1579 * Changes in common status bits are propagated to attached LUNs.
Christof Schmitt287ac012008-07-02 10:56:40 +02001580 */
Swen Schilligedaed852010-09-08 14:40:01 +02001581void zfcp_erp_clear_port_status(struct zfcp_port *port, u32 mask)
1582{
1583 struct scsi_device *sdev;
1584 u32 common_mask = mask & ZFCP_COMMON_FLAGS;
1585 u32 clear_counter = mask & ZFCP_STATUS_COMMON_ERP_FAILED;
Martin Peschke924dd582013-08-22 17:45:37 +02001586 unsigned long flags;
Swen Schilligedaed852010-09-08 14:40:01 +02001587
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001588 atomic_andnot(mask, &port->status);
Swen Schilligedaed852010-09-08 14:40:01 +02001589
1590 if (!common_mask)
1591 return;
1592
1593 if (clear_counter)
1594 atomic_set(&port->erp_counter, 0);
1595
Martin Peschke924dd582013-08-22 17:45:37 +02001596 spin_lock_irqsave(port->adapter->scsi_host->host_lock, flags);
1597 __shost_for_each_device(sdev, port->adapter->scsi_host)
Swen Schilligedaed852010-09-08 14:40:01 +02001598 if (sdev_to_zfcp(sdev)->port == port) {
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001599 atomic_andnot(common_mask,
Swen Schilligedaed852010-09-08 14:40:01 +02001600 &sdev_to_zfcp(sdev)->status);
1601 if (clear_counter)
1602 atomic_set(&sdev_to_zfcp(sdev)->erp_counter, 0);
1603 }
Martin Peschke924dd582013-08-22 17:45:37 +02001604 spin_unlock_irqrestore(port->adapter->scsi_host->host_lock, flags);
Swen Schilligedaed852010-09-08 14:40:01 +02001605}
1606
1607/**
1608 * zfcp_erp_set_lun_status - set lun status bits
1609 * @sdev: SCSI device / lun to set the status bits
1610 * @mask: status bits to change
1611 */
1612void zfcp_erp_set_lun_status(struct scsi_device *sdev, u32 mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613{
Christof Schmittb62a8d92010-09-08 14:39:55 +02001614 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
1615
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001616 atomic_or(mask, &zfcp_sdev->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617}
1618
Christof Schmitt287ac012008-07-02 10:56:40 +02001619/**
Swen Schilligedaed852010-09-08 14:40:01 +02001620 * zfcp_erp_clear_lun_status - clear lun status bits
1621 * @sdev: SCSi device / lun to clear the status bits
1622 * @mask: status bits to change
Christof Schmitt287ac012008-07-02 10:56:40 +02001623 */
Swen Schilligedaed852010-09-08 14:40:01 +02001624void zfcp_erp_clear_lun_status(struct scsi_device *sdev, u32 mask)
Andreas Herrmannd736a27b2005-06-13 13:23:57 +02001625{
Swen Schilligedaed852010-09-08 14:40:01 +02001626 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
1627
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001628 atomic_andnot(mask, &zfcp_sdev->status);
Swen Schilligedaed852010-09-08 14:40:01 +02001629
1630 if (mask & ZFCP_STATUS_COMMON_ERP_FAILED)
1631 atomic_set(&zfcp_sdev->erp_counter, 0);
Andreas Herrmannd736a27b2005-06-13 13:23:57 +02001632}
1633