blob: 67297d2744fb8343b6dfe22e5843ce0fae75dfc8 [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 *
Christof Schmitta2fa0ae2009-03-02 13:09:08 +01006 * Copyright IBM Corporation 2002, 2009
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include "zfcp_ext.h"
13
Christof Schmitt287ac012008-07-02 10:56:40 +020014#define ZFCP_MAX_ERPS 3
Linus Torvalds1da177e2005-04-16 15:20:36 -070015
Christof Schmitt287ac012008-07-02 10:56:40 +020016enum zfcp_erp_act_flags {
17 ZFCP_STATUS_ERP_TIMEDOUT = 0x10000000,
18 ZFCP_STATUS_ERP_CLOSE_ONLY = 0x01000000,
19 ZFCP_STATUS_ERP_DISMISSING = 0x00100000,
20 ZFCP_STATUS_ERP_DISMISSED = 0x00200000,
21 ZFCP_STATUS_ERP_LOWMEM = 0x00400000,
22};
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
Christof Schmitt287ac012008-07-02 10:56:40 +020024enum zfcp_erp_steps {
25 ZFCP_ERP_STEP_UNINITIALIZED = 0x0000,
26 ZFCP_ERP_STEP_FSF_XCONFIG = 0x0001,
27 ZFCP_ERP_STEP_PHYS_PORT_CLOSING = 0x0010,
28 ZFCP_ERP_STEP_PORT_CLOSING = 0x0100,
Christof Schmitt287ac012008-07-02 10:56:40 +020029 ZFCP_ERP_STEP_PORT_OPENING = 0x0800,
30 ZFCP_ERP_STEP_UNIT_CLOSING = 0x1000,
31 ZFCP_ERP_STEP_UNIT_OPENING = 0x2000,
32};
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
Christof Schmitt287ac012008-07-02 10:56:40 +020034enum zfcp_erp_act_type {
35 ZFCP_ERP_ACTION_REOPEN_UNIT = 1,
36 ZFCP_ERP_ACTION_REOPEN_PORT = 2,
37 ZFCP_ERP_ACTION_REOPEN_PORT_FORCED = 3,
38 ZFCP_ERP_ACTION_REOPEN_ADAPTER = 4,
39};
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Christof Schmitt287ac012008-07-02 10:56:40 +020041enum zfcp_erp_act_state {
42 ZFCP_ERP_ACTION_RUNNING = 1,
43 ZFCP_ERP_ACTION_READY = 2,
44};
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Christof Schmitt287ac012008-07-02 10:56:40 +020046enum zfcp_erp_act_result {
47 ZFCP_ERP_SUCCEEDED = 0,
48 ZFCP_ERP_FAILED = 1,
49 ZFCP_ERP_CONTINUES = 2,
50 ZFCP_ERP_EXIT = 3,
51 ZFCP_ERP_DISMISSED = 4,
52 ZFCP_ERP_NOMEM = 5,
53};
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Christof Schmitt287ac012008-07-02 10:56:40 +020055static void zfcp_erp_adapter_block(struct zfcp_adapter *adapter, int mask)
Andreas Herrmann2abbe862006-09-18 22:29:56 +020056{
Swen Schillig5ffd51a2009-03-02 13:09:04 +010057 zfcp_erp_modify_adapter_status(adapter, "erablk1", NULL,
Christof Schmitt287ac012008-07-02 10:56:40 +020058 ZFCP_STATUS_COMMON_UNBLOCKED | mask,
59 ZFCP_CLEAR);
Andreas Herrmann2abbe862006-09-18 22:29:56 +020060}
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Christof Schmitt287ac012008-07-02 10:56:40 +020062static int zfcp_erp_action_exists(struct zfcp_erp_action *act)
Linus Torvalds1da177e2005-04-16 15:20:36 -070063{
Christof Schmitt287ac012008-07-02 10:56:40 +020064 struct zfcp_erp_action *curr_act;
65
66 list_for_each_entry(curr_act, &act->adapter->erp_running_head, list)
67 if (act == curr_act)
68 return ZFCP_ERP_ACTION_RUNNING;
69 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070}
71
Christof Schmitt287ac012008-07-02 10:56:40 +020072static void zfcp_erp_action_ready(struct zfcp_erp_action *act)
Linus Torvalds1da177e2005-04-16 15:20:36 -070073{
Christof Schmitt287ac012008-07-02 10:56:40 +020074 struct zfcp_adapter *adapter = act->adapter;
75
76 list_move(&act->list, &act->adapter->erp_ready_head);
Swen Schillig5ffd51a2009-03-02 13:09:04 +010077 zfcp_rec_dbf_event_action("erardy1", act);
Christof Schmitt287ac012008-07-02 10:56:40 +020078 up(&adapter->erp_ready_sem);
Swen Schillig5ffd51a2009-03-02 13:09:04 +010079 zfcp_rec_dbf_event_thread("erardy2", adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080}
81
Christof Schmitt287ac012008-07-02 10:56:40 +020082static void zfcp_erp_action_dismiss(struct zfcp_erp_action *act)
Linus Torvalds1da177e2005-04-16 15:20:36 -070083{
Christof Schmitt287ac012008-07-02 10:56:40 +020084 act->status |= ZFCP_STATUS_ERP_DISMISSED;
85 if (zfcp_erp_action_exists(act) == ZFCP_ERP_ACTION_RUNNING)
86 zfcp_erp_action_ready(act);
87}
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
Christof Schmitt287ac012008-07-02 10:56:40 +020089static void zfcp_erp_action_dismiss_unit(struct zfcp_unit *unit)
90{
91 if (atomic_read(&unit->status) & ZFCP_STATUS_COMMON_ERP_INUSE)
92 zfcp_erp_action_dismiss(&unit->erp_action);
93}
94
95static void zfcp_erp_action_dismiss_port(struct zfcp_port *port)
96{
97 struct zfcp_unit *unit;
98
99 if (atomic_read(&port->status) & ZFCP_STATUS_COMMON_ERP_INUSE)
100 zfcp_erp_action_dismiss(&port->erp_action);
101 else
102 list_for_each_entry(unit, &port->unit_list_head, list)
103 zfcp_erp_action_dismiss_unit(unit);
104}
105
106static void zfcp_erp_action_dismiss_adapter(struct zfcp_adapter *adapter)
107{
108 struct zfcp_port *port;
109
110 if (atomic_read(&adapter->status) & ZFCP_STATUS_COMMON_ERP_INUSE)
111 zfcp_erp_action_dismiss(&adapter->erp_action);
112 else
113 list_for_each_entry(port, &adapter->port_list_head, list)
114 zfcp_erp_action_dismiss_port(port);
115}
116
117static int zfcp_erp_required_act(int want, struct zfcp_adapter *adapter,
118 struct zfcp_port *port,
119 struct zfcp_unit *unit)
120{
121 int need = want;
122 int u_status, p_status, a_status;
123
124 switch (want) {
125 case ZFCP_ERP_ACTION_REOPEN_UNIT:
126 u_status = atomic_read(&unit->status);
127 if (u_status & ZFCP_STATUS_COMMON_ERP_INUSE)
128 return 0;
129 p_status = atomic_read(&port->status);
130 if (!(p_status & ZFCP_STATUS_COMMON_RUNNING) ||
131 p_status & ZFCP_STATUS_COMMON_ERP_FAILED)
132 return 0;
133 if (!(p_status & ZFCP_STATUS_COMMON_UNBLOCKED))
134 need = ZFCP_ERP_ACTION_REOPEN_PORT;
135 /* fall through */
136 case ZFCP_ERP_ACTION_REOPEN_PORT:
137 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
138 p_status = atomic_read(&port->status);
139 if (p_status & ZFCP_STATUS_COMMON_ERP_INUSE)
140 return 0;
141 a_status = atomic_read(&adapter->status);
142 if (!(a_status & ZFCP_STATUS_COMMON_RUNNING) ||
143 a_status & ZFCP_STATUS_COMMON_ERP_FAILED)
144 return 0;
145 if (!(a_status & ZFCP_STATUS_COMMON_UNBLOCKED))
146 need = ZFCP_ERP_ACTION_REOPEN_ADAPTER;
147 /* fall through */
148 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
149 a_status = atomic_read(&adapter->status);
150 if (a_status & ZFCP_STATUS_COMMON_ERP_INUSE)
151 return 0;
152 }
153
154 return need;
155}
156
157static struct zfcp_erp_action *zfcp_erp_setup_act(int need,
158 struct zfcp_adapter *adapter,
159 struct zfcp_port *port,
160 struct zfcp_unit *unit)
161{
162 struct zfcp_erp_action *erp_action;
163 u32 status = 0;
164
165 switch (need) {
166 case ZFCP_ERP_ACTION_REOPEN_UNIT:
167 zfcp_unit_get(unit);
168 atomic_set_mask(ZFCP_STATUS_COMMON_ERP_INUSE, &unit->status);
169 erp_action = &unit->erp_action;
170 if (!(atomic_read(&unit->status) & ZFCP_STATUS_COMMON_RUNNING))
171 status = ZFCP_STATUS_ERP_CLOSE_ONLY;
172 break;
173
174 case ZFCP_ERP_ACTION_REOPEN_PORT:
175 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
176 zfcp_port_get(port);
177 zfcp_erp_action_dismiss_port(port);
178 atomic_set_mask(ZFCP_STATUS_COMMON_ERP_INUSE, &port->status);
179 erp_action = &port->erp_action;
180 if (!(atomic_read(&port->status) & ZFCP_STATUS_COMMON_RUNNING))
181 status = ZFCP_STATUS_ERP_CLOSE_ONLY;
182 break;
183
184 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
185 zfcp_adapter_get(adapter);
186 zfcp_erp_action_dismiss_adapter(adapter);
187 atomic_set_mask(ZFCP_STATUS_COMMON_ERP_INUSE, &adapter->status);
188 erp_action = &adapter->erp_action;
189 if (!(atomic_read(&adapter->status) &
190 ZFCP_STATUS_COMMON_RUNNING))
191 status = ZFCP_STATUS_ERP_CLOSE_ONLY;
192 break;
193
194 default:
195 return NULL;
196 }
197
198 memset(erp_action, 0, sizeof(struct zfcp_erp_action));
199 erp_action->adapter = adapter;
200 erp_action->port = port;
201 erp_action->unit = unit;
202 erp_action->action = need;
203 erp_action->status = status;
204
205 return erp_action;
206}
207
208static int zfcp_erp_action_enqueue(int want, struct zfcp_adapter *adapter,
209 struct zfcp_port *port,
Swen Schillig5ffd51a2009-03-02 13:09:04 +0100210 struct zfcp_unit *unit, char *id, void *ref)
Christof Schmitt287ac012008-07-02 10:56:40 +0200211{
212 int retval = 1, need;
213 struct zfcp_erp_action *act = NULL;
214
215 if (!(atomic_read(&adapter->status) &
216 ZFCP_STATUS_ADAPTER_ERP_THREAD_UP))
217 return -EIO;
218
219 need = zfcp_erp_required_act(want, adapter, port, unit);
220 if (!need)
221 goto out;
222
223 atomic_set_mask(ZFCP_STATUS_ADAPTER_ERP_PENDING, &adapter->status);
224 act = zfcp_erp_setup_act(need, adapter, port, unit);
225 if (!act)
226 goto out;
227 ++adapter->erp_total_count;
228 list_add_tail(&act->list, &adapter->erp_ready_head);
229 up(&adapter->erp_ready_sem);
Swen Schillig5ffd51a2009-03-02 13:09:04 +0100230 zfcp_rec_dbf_event_thread("eracte1", adapter);
Christof Schmitt287ac012008-07-02 10:56:40 +0200231 retval = 0;
232 out:
233 zfcp_rec_dbf_event_trigger(id, ref, want, need, act,
234 adapter, port, unit);
235 return retval;
236}
237
238static int _zfcp_erp_adapter_reopen(struct zfcp_adapter *adapter,
Swen Schillig5ffd51a2009-03-02 13:09:04 +0100239 int clear_mask, char *id, void *ref)
Christof Schmitt287ac012008-07-02 10:56:40 +0200240{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 zfcp_erp_adapter_block(adapter, clear_mask);
Christof Schmitta2fa0ae2009-03-02 13:09:08 +0100242 zfcp_scsi_schedule_rports_block(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
Christof Schmitt287ac012008-07-02 10:56:40 +0200244 /* ensure propagation of failed status to new devices */
245 if (atomic_read(&adapter->status) & ZFCP_STATUS_COMMON_ERP_FAILED) {
Swen Schillig5ffd51a2009-03-02 13:09:04 +0100246 zfcp_erp_adapter_failed(adapter, "erareo1", NULL);
Christof Schmitt287ac012008-07-02 10:56:40 +0200247 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 }
Christof Schmitt287ac012008-07-02 10:56:40 +0200249 return zfcp_erp_action_enqueue(ZFCP_ERP_ACTION_REOPEN_ADAPTER,
250 adapter, NULL, NULL, id, ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251}
252
253/**
Christof Schmitt287ac012008-07-02 10:56:40 +0200254 * zfcp_erp_adapter_reopen - Reopen adapter.
255 * @adapter: Adapter to reopen.
256 * @clear: Status flags to clear.
257 * @id: Id for debug trace event.
258 * @ref: Reference for debug trace event.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 */
Christof Schmitt287ac012008-07-02 10:56:40 +0200260void zfcp_erp_adapter_reopen(struct zfcp_adapter *adapter, int clear,
Swen Schillig5ffd51a2009-03-02 13:09:04 +0100261 char *id, void *ref)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262{
Christof Schmitt287ac012008-07-02 10:56:40 +0200263 unsigned long flags;
264
265 read_lock_irqsave(&zfcp_data.config_lock, flags);
266 write_lock(&adapter->erp_lock);
267 _zfcp_erp_adapter_reopen(adapter, clear, id, ref);
268 write_unlock(&adapter->erp_lock);
269 read_unlock_irqrestore(&zfcp_data.config_lock, flags);
270}
271
272/**
273 * zfcp_erp_adapter_shutdown - Shutdown adapter.
274 * @adapter: Adapter to shut down.
275 * @clear: Status flags to clear.
276 * @id: Id for debug trace event.
277 * @ref: Reference for debug trace event.
278 */
279void zfcp_erp_adapter_shutdown(struct zfcp_adapter *adapter, int clear,
Swen Schillig5ffd51a2009-03-02 13:09:04 +0100280 char *id, void *ref)
Christof Schmitt287ac012008-07-02 10:56:40 +0200281{
282 int flags = ZFCP_STATUS_COMMON_RUNNING | ZFCP_STATUS_COMMON_ERP_FAILED;
283 zfcp_erp_adapter_reopen(adapter, clear | flags, id, ref);
284}
285
286/**
287 * zfcp_erp_port_shutdown - Shutdown port
288 * @port: Port to shut down.
289 * @clear: Status flags to clear.
290 * @id: Id for debug trace event.
291 * @ref: Reference for debug trace event.
292 */
Swen Schillig5ffd51a2009-03-02 13:09:04 +0100293void zfcp_erp_port_shutdown(struct zfcp_port *port, int clear, char *id,
294 void *ref)
Christof Schmitt287ac012008-07-02 10:56:40 +0200295{
296 int flags = ZFCP_STATUS_COMMON_RUNNING | ZFCP_STATUS_COMMON_ERP_FAILED;
297 zfcp_erp_port_reopen(port, clear | flags, id, ref);
298}
299
300/**
301 * zfcp_erp_unit_shutdown - Shutdown unit
302 * @unit: Unit to shut down.
303 * @clear: Status flags to clear.
304 * @id: Id for debug trace event.
305 * @ref: Reference for debug trace event.
306 */
Swen Schillig5ffd51a2009-03-02 13:09:04 +0100307void zfcp_erp_unit_shutdown(struct zfcp_unit *unit, int clear, char *id,
308 void *ref)
Christof Schmitt287ac012008-07-02 10:56:40 +0200309{
310 int flags = ZFCP_STATUS_COMMON_RUNNING | ZFCP_STATUS_COMMON_ERP_FAILED;
311 zfcp_erp_unit_reopen(unit, clear | flags, id, ref);
312}
313
314static void zfcp_erp_port_block(struct zfcp_port *port, int clear)
315{
Swen Schillig5ffd51a2009-03-02 13:09:04 +0100316 zfcp_erp_modify_port_status(port, "erpblk1", NULL,
Christof Schmitt287ac012008-07-02 10:56:40 +0200317 ZFCP_STATUS_COMMON_UNBLOCKED | clear,
318 ZFCP_CLEAR);
319}
320
321static void _zfcp_erp_port_forced_reopen(struct zfcp_port *port,
Swen Schillig5ffd51a2009-03-02 13:09:04 +0100322 int clear, char *id, void *ref)
Christof Schmitt287ac012008-07-02 10:56:40 +0200323{
324 zfcp_erp_port_block(port, clear);
Christof Schmitta2fa0ae2009-03-02 13:09:08 +0100325 zfcp_scsi_schedule_rport_block(port);
Christof Schmitt287ac012008-07-02 10:56:40 +0200326
327 if (atomic_read(&port->status) & ZFCP_STATUS_COMMON_ERP_FAILED)
328 return;
329
330 zfcp_erp_action_enqueue(ZFCP_ERP_ACTION_REOPEN_PORT_FORCED,
331 port->adapter, port, NULL, id, ref);
332}
333
334/**
335 * zfcp_erp_port_forced_reopen - Forced close of port and open again
336 * @port: Port to force close and to reopen.
337 * @id: Id for debug trace event.
338 * @ref: Reference for debug trace event.
339 */
Swen Schillig5ffd51a2009-03-02 13:09:04 +0100340void zfcp_erp_port_forced_reopen(struct zfcp_port *port, int clear, char *id,
Christof Schmitt287ac012008-07-02 10:56:40 +0200341 void *ref)
342{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 unsigned long flags;
344 struct zfcp_adapter *adapter = port->adapter;
345
346 read_lock_irqsave(&zfcp_data.config_lock, flags);
347 write_lock(&adapter->erp_lock);
Christof Schmitt287ac012008-07-02 10:56:40 +0200348 _zfcp_erp_port_forced_reopen(port, clear, id, ref);
349 write_unlock(&adapter->erp_lock);
350 read_unlock_irqrestore(&zfcp_data.config_lock, flags);
351}
352
Swen Schillig5ffd51a2009-03-02 13:09:04 +0100353static int _zfcp_erp_port_reopen(struct zfcp_port *port, int clear, char *id,
Christof Schmitt287ac012008-07-02 10:56:40 +0200354 void *ref)
355{
356 zfcp_erp_port_block(port, clear);
Christof Schmitta2fa0ae2009-03-02 13:09:08 +0100357 zfcp_scsi_schedule_rport_block(port);
Christof Schmitt287ac012008-07-02 10:56:40 +0200358
359 if (atomic_read(&port->status) & ZFCP_STATUS_COMMON_ERP_FAILED) {
360 /* ensure propagation of failed status to new devices */
Swen Schillig5ffd51a2009-03-02 13:09:04 +0100361 zfcp_erp_port_failed(port, "erpreo1", NULL);
Christof Schmitt287ac012008-07-02 10:56:40 +0200362 return -EIO;
363 }
364
365 return zfcp_erp_action_enqueue(ZFCP_ERP_ACTION_REOPEN_PORT,
366 port->adapter, port, NULL, id, ref);
367}
368
369/**
370 * zfcp_erp_port_reopen - trigger remote port recovery
371 * @port: port to recover
372 * @clear_mask: flags in port status to be cleared
373 *
374 * Returns 0 if recovery has been triggered, < 0 if not.
375 */
Swen Schillig5ffd51a2009-03-02 13:09:04 +0100376int zfcp_erp_port_reopen(struct zfcp_port *port, int clear, char *id, void *ref)
Christof Schmitt287ac012008-07-02 10:56:40 +0200377{
378 unsigned long flags;
379 int retval;
380 struct zfcp_adapter *adapter = port->adapter;
381
382 read_lock_irqsave(&zfcp_data.config_lock, flags);
383 write_lock(&adapter->erp_lock);
384 retval = _zfcp_erp_port_reopen(port, clear, id, ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 write_unlock(&adapter->erp_lock);
386 read_unlock_irqrestore(&zfcp_data.config_lock, flags);
387
388 return retval;
389}
390
Christof Schmitt287ac012008-07-02 10:56:40 +0200391static void zfcp_erp_unit_block(struct zfcp_unit *unit, int clear_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392{
Swen Schillig5ffd51a2009-03-02 13:09:04 +0100393 zfcp_erp_modify_unit_status(unit, "erublk1", NULL,
Christof Schmitt287ac012008-07-02 10:56:40 +0200394 ZFCP_STATUS_COMMON_UNBLOCKED | clear_mask,
395 ZFCP_CLEAR);
396}
397
Swen Schillig5ffd51a2009-03-02 13:09:04 +0100398static void _zfcp_erp_unit_reopen(struct zfcp_unit *unit, int clear, char *id,
Christof Schmitt287ac012008-07-02 10:56:40 +0200399 void *ref)
400{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 struct zfcp_adapter *adapter = unit->port->adapter;
402
Christof Schmitt287ac012008-07-02 10:56:40 +0200403 zfcp_erp_unit_block(unit, clear);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
Christof Schmitt287ac012008-07-02 10:56:40 +0200405 if (atomic_read(&unit->status) & ZFCP_STATUS_COMMON_ERP_FAILED)
406 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
Christof Schmitt287ac012008-07-02 10:56:40 +0200408 zfcp_erp_action_enqueue(ZFCP_ERP_ACTION_REOPEN_UNIT,
409 adapter, unit->port, unit, id, ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410}
411
412/**
413 * zfcp_erp_unit_reopen - initiate reopen of a unit
414 * @unit: unit to be reopened
415 * @clear_mask: specifies flags in unit status to be cleared
416 * Return: 0 on success, < 0 on error
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 */
Swen Schillig5ffd51a2009-03-02 13:09:04 +0100418void zfcp_erp_unit_reopen(struct zfcp_unit *unit, int clear, char *id,
419 void *ref)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 unsigned long flags;
Christof Schmitt287ac012008-07-02 10:56:40 +0200422 struct zfcp_port *port = unit->port;
423 struct zfcp_adapter *adapter = port->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
425 read_lock_irqsave(&zfcp_data.config_lock, flags);
426 write_lock(&adapter->erp_lock);
Christof Schmitt287ac012008-07-02 10:56:40 +0200427 _zfcp_erp_unit_reopen(unit, clear, id, ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 write_unlock(&adapter->erp_lock);
429 read_unlock_irqrestore(&zfcp_data.config_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430}
431
Christof Schmitt287ac012008-07-02 10:56:40 +0200432static int status_change_set(unsigned long mask, atomic_t *status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433{
Christof Schmitt287ac012008-07-02 10:56:40 +0200434 return (atomic_read(status) ^ mask) & mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435}
436
Christof Schmitt287ac012008-07-02 10:56:40 +0200437static int status_change_clear(unsigned long mask, atomic_t *status)
Martin Peschke698ec0162008-03-27 14:22:02 +0100438{
Christof Schmitt287ac012008-07-02 10:56:40 +0200439 return atomic_read(status) & mask;
Martin Peschke698ec0162008-03-27 14:22:02 +0100440}
441
Andreas Herrmannf6c0e7a2006-08-02 11:05:52 +0200442static void zfcp_erp_adapter_unblock(struct zfcp_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443{
Christof Schmitt287ac012008-07-02 10:56:40 +0200444 if (status_change_set(ZFCP_STATUS_COMMON_UNBLOCKED, &adapter->status))
Swen Schillig5ffd51a2009-03-02 13:09:04 +0100445 zfcp_rec_dbf_event_adapter("eraubl1", NULL, adapter);
Christof Schmitt287ac012008-07-02 10:56:40 +0200446 atomic_set_mask(ZFCP_STATUS_COMMON_UNBLOCKED, &adapter->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447}
448
Christof Schmitt287ac012008-07-02 10:56:40 +0200449static void zfcp_erp_port_unblock(struct zfcp_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450{
Christof Schmitt287ac012008-07-02 10:56:40 +0200451 if (status_change_set(ZFCP_STATUS_COMMON_UNBLOCKED, &port->status))
Swen Schillig5ffd51a2009-03-02 13:09:04 +0100452 zfcp_rec_dbf_event_port("erpubl1", NULL, port);
Christof Schmitt287ac012008-07-02 10:56:40 +0200453 atomic_set_mask(ZFCP_STATUS_COMMON_UNBLOCKED, &port->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454}
455
Christof Schmitt287ac012008-07-02 10:56:40 +0200456static void zfcp_erp_unit_unblock(struct zfcp_unit *unit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457{
Christof Schmitt287ac012008-07-02 10:56:40 +0200458 if (status_change_set(ZFCP_STATUS_COMMON_UNBLOCKED, &unit->status))
Swen Schillig5ffd51a2009-03-02 13:09:04 +0100459 zfcp_rec_dbf_event_unit("eruubl1", NULL, unit);
Christof Schmitt287ac012008-07-02 10:56:40 +0200460 atomic_set_mask(ZFCP_STATUS_COMMON_UNBLOCKED, &unit->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461}
462
Christof Schmitt287ac012008-07-02 10:56:40 +0200463static void zfcp_erp_action_to_running(struct zfcp_erp_action *erp_action)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464{
Christof Schmitt287ac012008-07-02 10:56:40 +0200465 list_move(&erp_action->list, &erp_action->adapter->erp_running_head);
Swen Schillig5ffd51a2009-03-02 13:09:04 +0100466 zfcp_rec_dbf_event_action("erator1", erp_action);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467}
468
Christof Schmitt287ac012008-07-02 10:56:40 +0200469static void zfcp_erp_strategy_check_fsfreq(struct zfcp_erp_action *act)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470{
Christof Schmitt287ac012008-07-02 10:56:40 +0200471 struct zfcp_adapter *adapter = act->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
Christof Schmitt287ac012008-07-02 10:56:40 +0200473 if (!act->fsf_req)
474 return;
475
476 spin_lock(&adapter->req_list_lock);
477 if (zfcp_reqlist_find_safe(adapter, act->fsf_req) &&
478 act->fsf_req->erp_action == act) {
479 if (act->status & (ZFCP_STATUS_ERP_DISMISSED |
480 ZFCP_STATUS_ERP_TIMEDOUT)) {
481 act->fsf_req->status |= ZFCP_STATUS_FSFREQ_DISMISSED;
Swen Schillig5ffd51a2009-03-02 13:09:04 +0100482 zfcp_rec_dbf_event_action("erscf_1", act);
Martin Petermann7ea633f2008-11-04 16:35:11 +0100483 act->fsf_req->erp_action = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 }
Christof Schmitt287ac012008-07-02 10:56:40 +0200485 if (act->status & ZFCP_STATUS_ERP_TIMEDOUT)
Swen Schillig5ffd51a2009-03-02 13:09:04 +0100486 zfcp_rec_dbf_event_action("erscf_2", act);
Swen Schillig058b8642009-08-18 15:43:14 +0200487 if (act->fsf_req->status & ZFCP_STATUS_FSFREQ_DISMISSED)
Christof Schmitt287ac012008-07-02 10:56:40 +0200488 act->fsf_req = NULL;
489 } else
490 act->fsf_req = NULL;
491 spin_unlock(&adapter->req_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492}
493
Andreas Herrmannf6c0e7a2006-08-02 11:05:52 +0200494/**
Christof Schmitt287ac012008-07-02 10:56:40 +0200495 * zfcp_erp_notify - Trigger ERP action.
496 * @erp_action: ERP action to continue.
497 * @set_mask: ERP action status flags to set.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 */
Christof Schmitt287ac012008-07-02 10:56:40 +0200499void zfcp_erp_notify(struct zfcp_erp_action *erp_action, unsigned long set_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500{
501 struct zfcp_adapter *adapter = erp_action->adapter;
502 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
504 write_lock_irqsave(&adapter->erp_lock, flags);
Christof Schmitt287ac012008-07-02 10:56:40 +0200505 if (zfcp_erp_action_exists(erp_action) == ZFCP_ERP_ACTION_RUNNING) {
506 erp_action->status |= set_mask;
507 zfcp_erp_action_ready(erp_action);
508 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 write_unlock_irqrestore(&adapter->erp_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510}
511
Andreas Herrmannf6c0e7a2006-08-02 11:05:52 +0200512/**
Christof Schmitt287ac012008-07-02 10:56:40 +0200513 * zfcp_erp_timeout_handler - Trigger ERP action from timed out ERP request
514 * @data: ERP action (from timer data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 */
Christof Schmitt287ac012008-07-02 10:56:40 +0200516void zfcp_erp_timeout_handler(unsigned long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517{
Christof Schmitt287ac012008-07-02 10:56:40 +0200518 struct zfcp_erp_action *act = (struct zfcp_erp_action *) data;
519 zfcp_erp_notify(act, ZFCP_STATUS_ERP_TIMEDOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520}
521
Christof Schmitt287ac012008-07-02 10:56:40 +0200522static void zfcp_erp_memwait_handler(unsigned long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523{
Christof Schmitt287ac012008-07-02 10:56:40 +0200524 zfcp_erp_notify((struct zfcp_erp_action *)data, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525}
526
Christof Schmitt287ac012008-07-02 10:56:40 +0200527static void zfcp_erp_strategy_memwait(struct zfcp_erp_action *erp_action)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 init_timer(&erp_action->timer);
530 erp_action->timer.function = zfcp_erp_memwait_handler;
531 erp_action->timer.data = (unsigned long) erp_action;
Christof Schmitt287ac012008-07-02 10:56:40 +0200532 erp_action->timer.expires = jiffies + HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 add_timer(&erp_action->timer);
Christof Schmitt287ac012008-07-02 10:56:40 +0200534}
535
536static void _zfcp_erp_port_reopen_all(struct zfcp_adapter *adapter,
Swen Schillig5ffd51a2009-03-02 13:09:04 +0100537 int clear, char *id, void *ref)
Christof Schmitt287ac012008-07-02 10:56:40 +0200538{
539 struct zfcp_port *port;
540
541 list_for_each_entry(port, &adapter->port_list_head, list)
Swen Schillig5ab944f2008-10-01 12:42:17 +0200542 _zfcp_erp_port_reopen(port, clear, id, ref);
Christof Schmitt287ac012008-07-02 10:56:40 +0200543}
544
Swen Schillig5ffd51a2009-03-02 13:09:04 +0100545static void _zfcp_erp_unit_reopen_all(struct zfcp_port *port, int clear,
546 char *id, void *ref)
Christof Schmitt287ac012008-07-02 10:56:40 +0200547{
548 struct zfcp_unit *unit;
549
550 list_for_each_entry(unit, &port->unit_list_head, list)
551 _zfcp_erp_unit_reopen(unit, clear, id, ref);
552}
553
Christof Schmitt85600f72009-07-13 15:06:09 +0200554static void zfcp_erp_strategy_followup_failed(struct zfcp_erp_action *act)
Christof Schmitt287ac012008-07-02 10:56:40 +0200555{
Christof Schmitt287ac012008-07-02 10:56:40 +0200556 switch (act->action) {
Christof Schmitt287ac012008-07-02 10:56:40 +0200557 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
Christof Schmitt85600f72009-07-13 15:06:09 +0200558 _zfcp_erp_adapter_reopen(act->adapter, 0, "ersff_1", NULL);
Christof Schmitt287ac012008-07-02 10:56:40 +0200559 break;
Christof Schmitt287ac012008-07-02 10:56:40 +0200560 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
Christof Schmitt85600f72009-07-13 15:06:09 +0200561 _zfcp_erp_port_forced_reopen(act->port, 0, "ersff_2", NULL);
Christof Schmitt287ac012008-07-02 10:56:40 +0200562 break;
Christof Schmitt287ac012008-07-02 10:56:40 +0200563 case ZFCP_ERP_ACTION_REOPEN_PORT:
Christof Schmitt85600f72009-07-13 15:06:09 +0200564 _zfcp_erp_port_reopen(act->port, 0, "ersff_3", NULL);
Christof Schmitt287ac012008-07-02 10:56:40 +0200565 break;
Christof Schmitt287ac012008-07-02 10:56:40 +0200566 case ZFCP_ERP_ACTION_REOPEN_UNIT:
Christof Schmitt85600f72009-07-13 15:06:09 +0200567 _zfcp_erp_unit_reopen(act->unit, 0, "ersff_4", NULL);
568 break;
569 }
570}
571
572static void zfcp_erp_strategy_followup_success(struct zfcp_erp_action *act)
573{
574 switch (act->action) {
575 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
576 _zfcp_erp_port_reopen_all(act->adapter, 0, "ersfs_1", NULL);
577 break;
578 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
579 _zfcp_erp_port_reopen(act->port, 0, "ersfs_2", NULL);
580 break;
581 case ZFCP_ERP_ACTION_REOPEN_PORT:
582 _zfcp_erp_unit_reopen_all(act->port, 0, "ersfs_3", NULL);
Christof Schmitt287ac012008-07-02 10:56:40 +0200583 break;
584 }
585}
586
587static void zfcp_erp_wakeup(struct zfcp_adapter *adapter)
588{
589 unsigned long flags;
590
591 read_lock_irqsave(&zfcp_data.config_lock, flags);
592 read_lock(&adapter->erp_lock);
593 if (list_empty(&adapter->erp_ready_head) &&
594 list_empty(&adapter->erp_running_head)) {
595 atomic_clear_mask(ZFCP_STATUS_ADAPTER_ERP_PENDING,
596 &adapter->status);
597 wake_up(&adapter->erp_done_wqh);
598 }
599 read_unlock(&adapter->erp_lock);
600 read_unlock_irqrestore(&zfcp_data.config_lock, flags);
601}
602
603static int zfcp_erp_adapter_strategy_open_qdio(struct zfcp_erp_action *act)
604{
Swen Schillig564e1c82009-08-18 15:43:19 +0200605 struct zfcp_qdio *qdio = act->adapter->qdio;
606
607 if (zfcp_qdio_open(qdio))
Christof Schmitt287ac012008-07-02 10:56:40 +0200608 return ZFCP_ERP_FAILED;
Swen Schillig564e1c82009-08-18 15:43:19 +0200609 init_waitqueue_head(&qdio->req_q_wq);
Christof Schmitt287ac012008-07-02 10:56:40 +0200610 atomic_set_mask(ZFCP_STATUS_ADAPTER_QDIOUP, &act->adapter->status);
611 return ZFCP_ERP_SUCCEEDED;
612}
613
614static void zfcp_erp_enqueue_ptp_port(struct zfcp_adapter *adapter)
615{
616 struct zfcp_port *port;
617 port = zfcp_port_enqueue(adapter, adapter->peer_wwpn, 0,
618 adapter->peer_d_id);
619 if (IS_ERR(port)) /* error or port already attached */
620 return;
Swen Schillig5ffd51a2009-03-02 13:09:04 +0100621 _zfcp_erp_port_reopen(port, 0, "ereptp1", NULL);
Christof Schmitt287ac012008-07-02 10:56:40 +0200622}
623
624static int zfcp_erp_adapter_strat_fsf_xconf(struct zfcp_erp_action *erp_action)
625{
626 int retries;
627 int sleep = 1;
628 struct zfcp_adapter *adapter = erp_action->adapter;
629
630 atomic_clear_mask(ZFCP_STATUS_ADAPTER_XCONFIG_OK, &adapter->status);
631
632 for (retries = 7; retries; retries--) {
633 atomic_clear_mask(ZFCP_STATUS_ADAPTER_HOST_CON_INIT,
634 &adapter->status);
635 write_lock_irq(&adapter->erp_lock);
636 zfcp_erp_action_to_running(erp_action);
637 write_unlock_irq(&adapter->erp_lock);
638 if (zfcp_fsf_exchange_config_data(erp_action)) {
639 atomic_clear_mask(ZFCP_STATUS_ADAPTER_HOST_CON_INIT,
640 &adapter->status);
641 return ZFCP_ERP_FAILED;
642 }
643
Swen Schillig5ffd51a2009-03-02 13:09:04 +0100644 zfcp_rec_dbf_event_thread_lock("erasfx1", adapter);
Christof Schmitt287ac012008-07-02 10:56:40 +0200645 down(&adapter->erp_ready_sem);
Swen Schillig5ffd51a2009-03-02 13:09:04 +0100646 zfcp_rec_dbf_event_thread_lock("erasfx2", adapter);
Christof Schmitt287ac012008-07-02 10:56:40 +0200647 if (erp_action->status & ZFCP_STATUS_ERP_TIMEDOUT)
648 break;
649
650 if (!(atomic_read(&adapter->status) &
651 ZFCP_STATUS_ADAPTER_HOST_CON_INIT))
652 break;
653
654 ssleep(sleep);
655 sleep *= 2;
656 }
657
658 atomic_clear_mask(ZFCP_STATUS_ADAPTER_HOST_CON_INIT,
659 &adapter->status);
660
661 if (!(atomic_read(&adapter->status) & ZFCP_STATUS_ADAPTER_XCONFIG_OK))
662 return ZFCP_ERP_FAILED;
663
664 if (fc_host_port_type(adapter->scsi_host) == FC_PORTTYPE_PTP)
665 zfcp_erp_enqueue_ptp_port(adapter);
666
667 return ZFCP_ERP_SUCCEEDED;
668}
669
670static int zfcp_erp_adapter_strategy_open_fsf_xport(struct zfcp_erp_action *act)
671{
672 int ret;
673 struct zfcp_adapter *adapter = act->adapter;
674
Christof Schmitt287ac012008-07-02 10:56:40 +0200675 write_lock_irq(&adapter->erp_lock);
676 zfcp_erp_action_to_running(act);
677 write_unlock_irq(&adapter->erp_lock);
678
679 ret = zfcp_fsf_exchange_port_data(act);
680 if (ret == -EOPNOTSUPP)
681 return ZFCP_ERP_SUCCEEDED;
682 if (ret)
683 return ZFCP_ERP_FAILED;
684
Swen Schillig5ffd51a2009-03-02 13:09:04 +0100685 zfcp_rec_dbf_event_thread_lock("erasox1", adapter);
Christof Schmitt287ac012008-07-02 10:56:40 +0200686 down(&adapter->erp_ready_sem);
Swen Schillig5ffd51a2009-03-02 13:09:04 +0100687 zfcp_rec_dbf_event_thread_lock("erasox2", adapter);
Christof Schmitt287ac012008-07-02 10:56:40 +0200688 if (act->status & ZFCP_STATUS_ERP_TIMEDOUT)
689 return ZFCP_ERP_FAILED;
690
691 return ZFCP_ERP_SUCCEEDED;
692}
693
694static int zfcp_erp_adapter_strategy_open_fsf(struct zfcp_erp_action *act)
695{
696 if (zfcp_erp_adapter_strat_fsf_xconf(act) == ZFCP_ERP_FAILED)
697 return ZFCP_ERP_FAILED;
698
699 if (zfcp_erp_adapter_strategy_open_fsf_xport(act) == ZFCP_ERP_FAILED)
700 return ZFCP_ERP_FAILED;
701
702 atomic_set(&act->adapter->stat_miss, 16);
703 if (zfcp_status_read_refill(act->adapter))
704 return ZFCP_ERP_FAILED;
705
706 return ZFCP_ERP_SUCCEEDED;
707}
708
Swen Schilligcf13c082009-03-02 13:09:03 +0100709static void zfcp_erp_adapter_strategy_close(struct zfcp_erp_action *act)
Christof Schmitt287ac012008-07-02 10:56:40 +0200710{
Christof Schmitt287ac012008-07-02 10:56:40 +0200711 struct zfcp_adapter *adapter = act->adapter;
712
Christof Schmitt287ac012008-07-02 10:56:40 +0200713 /* close queues to ensure that buffers are not accessed by adapter */
Swen Schillig564e1c82009-08-18 15:43:19 +0200714 zfcp_qdio_close(adapter->qdio);
Christof Schmitt287ac012008-07-02 10:56:40 +0200715 zfcp_fsf_req_dismiss_all(adapter);
716 adapter->fsf_req_seq_no = 0;
Christof Schmitt55c770f2009-08-18 15:43:12 +0200717 zfcp_fc_wka_ports_force_offline(adapter->gs);
Christof Schmitt287ac012008-07-02 10:56:40 +0200718 /* all ports and units are closed */
Swen Schillig5ffd51a2009-03-02 13:09:04 +0100719 zfcp_erp_modify_adapter_status(adapter, "erascl1", NULL,
Christof Schmitt287ac012008-07-02 10:56:40 +0200720 ZFCP_STATUS_COMMON_OPEN, ZFCP_CLEAR);
Swen Schilligcf13c082009-03-02 13:09:03 +0100721
Christof Schmitt287ac012008-07-02 10:56:40 +0200722 atomic_clear_mask(ZFCP_STATUS_ADAPTER_XCONFIG_OK |
Swen Schilligcf13c082009-03-02 13:09:03 +0100723 ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED, &adapter->status);
724}
725
726static int zfcp_erp_adapter_strategy_open(struct zfcp_erp_action *act)
727{
728 struct zfcp_adapter *adapter = act->adapter;
729
730 if (zfcp_erp_adapter_strategy_open_qdio(act)) {
731 atomic_clear_mask(ZFCP_STATUS_ADAPTER_XCONFIG_OK |
732 ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED,
733 &adapter->status);
734 return ZFCP_ERP_FAILED;
735 }
736
737 if (zfcp_erp_adapter_strategy_open_fsf(act)) {
738 zfcp_erp_adapter_strategy_close(act);
739 return ZFCP_ERP_FAILED;
740 }
741
742 atomic_set_mask(ZFCP_STATUS_COMMON_OPEN, &adapter->status);
743
744 return ZFCP_ERP_SUCCEEDED;
Christof Schmitt287ac012008-07-02 10:56:40 +0200745}
746
747static int zfcp_erp_adapter_strategy(struct zfcp_erp_action *act)
748{
Swen Schilligcf13c082009-03-02 13:09:03 +0100749 struct zfcp_adapter *adapter = act->adapter;
Christof Schmitt287ac012008-07-02 10:56:40 +0200750
Swen Schilligcf13c082009-03-02 13:09:03 +0100751 if (atomic_read(&adapter->status) & ZFCP_STATUS_COMMON_OPEN) {
752 zfcp_erp_adapter_strategy_close(act);
753 if (act->status & ZFCP_STATUS_ERP_CLOSE_ONLY)
754 return ZFCP_ERP_EXIT;
755 }
Christof Schmitt287ac012008-07-02 10:56:40 +0200756
Swen Schilligcf13c082009-03-02 13:09:03 +0100757 if (zfcp_erp_adapter_strategy_open(act)) {
Christof Schmitt287ac012008-07-02 10:56:40 +0200758 ssleep(8);
Swen Schilligcf13c082009-03-02 13:09:03 +0100759 return ZFCP_ERP_FAILED;
760 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
Swen Schilligcf13c082009-03-02 13:09:03 +0100762 return ZFCP_ERP_SUCCEEDED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763}
764
Christof Schmitt287ac012008-07-02 10:56:40 +0200765static int zfcp_erp_port_forced_strategy_close(struct zfcp_erp_action *act)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766{
Christof Schmitt287ac012008-07-02 10:56:40 +0200767 int retval;
768
769 retval = zfcp_fsf_close_physical_port(act);
770 if (retval == -ENOMEM)
771 return ZFCP_ERP_NOMEM;
772 act->step = ZFCP_ERP_STEP_PHYS_PORT_CLOSING;
773 if (retval)
774 return ZFCP_ERP_FAILED;
775
776 return ZFCP_ERP_CONTINUES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777}
778
Christof Schmitt287ac012008-07-02 10:56:40 +0200779static void zfcp_erp_port_strategy_clearstati(struct zfcp_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780{
Christof Schmitta5b11dd2009-03-02 13:08:54 +0100781 atomic_clear_mask(ZFCP_STATUS_COMMON_ACCESS_DENIED, &port->status);
Christof Schmitt287ac012008-07-02 10:56:40 +0200782}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783
Christof Schmitt287ac012008-07-02 10:56:40 +0200784static int zfcp_erp_port_forced_strategy(struct zfcp_erp_action *erp_action)
785{
786 struct zfcp_port *port = erp_action->port;
787 int status = atomic_read(&port->status);
788
789 switch (erp_action->step) {
790 case ZFCP_ERP_STEP_UNINITIALIZED:
791 zfcp_erp_port_strategy_clearstati(port);
792 if ((status & ZFCP_STATUS_PORT_PHYS_OPEN) &&
793 (status & ZFCP_STATUS_COMMON_OPEN))
794 return zfcp_erp_port_forced_strategy_close(erp_action);
795 else
796 return ZFCP_ERP_FAILED;
797
798 case ZFCP_ERP_STEP_PHYS_PORT_CLOSING:
Christof Schmittddb3e0c2009-07-13 15:06:08 +0200799 if (!(status & ZFCP_STATUS_PORT_PHYS_OPEN))
Christof Schmitt287ac012008-07-02 10:56:40 +0200800 return ZFCP_ERP_SUCCEEDED;
801 }
802 return ZFCP_ERP_FAILED;
803}
804
805static int zfcp_erp_port_strategy_close(struct zfcp_erp_action *erp_action)
806{
807 int retval;
808
809 retval = zfcp_fsf_close_port(erp_action);
810 if (retval == -ENOMEM)
811 return ZFCP_ERP_NOMEM;
812 erp_action->step = ZFCP_ERP_STEP_PORT_CLOSING;
813 if (retval)
814 return ZFCP_ERP_FAILED;
815 return ZFCP_ERP_CONTINUES;
816}
817
818static int zfcp_erp_port_strategy_open_port(struct zfcp_erp_action *erp_action)
819{
820 int retval;
821
822 retval = zfcp_fsf_open_port(erp_action);
823 if (retval == -ENOMEM)
824 return ZFCP_ERP_NOMEM;
825 erp_action->step = ZFCP_ERP_STEP_PORT_OPENING;
826 if (retval)
827 return ZFCP_ERP_FAILED;
828 return ZFCP_ERP_CONTINUES;
829}
830
Christof Schmitt287ac012008-07-02 10:56:40 +0200831static int zfcp_erp_open_ptp_port(struct zfcp_erp_action *act)
832{
833 struct zfcp_adapter *adapter = act->adapter;
834 struct zfcp_port *port = act->port;
835
836 if (port->wwpn != adapter->peer_wwpn) {
Swen Schillig5ffd51a2009-03-02 13:09:04 +0100837 zfcp_erp_port_failed(port, "eroptp1", NULL);
Christof Schmitt287ac012008-07-02 10:56:40 +0200838 return ZFCP_ERP_FAILED;
839 }
840 port->d_id = adapter->peer_d_id;
Christof Schmitt287ac012008-07-02 10:56:40 +0200841 return zfcp_erp_port_strategy_open_port(act);
842}
843
844static int zfcp_erp_port_strategy_open_common(struct zfcp_erp_action *act)
845{
846 struct zfcp_adapter *adapter = act->adapter;
847 struct zfcp_port *port = act->port;
Christof Schmitt287ac012008-07-02 10:56:40 +0200848 int p_status = atomic_read(&port->status);
849
850 switch (act->step) {
851 case ZFCP_ERP_STEP_UNINITIALIZED:
852 case ZFCP_ERP_STEP_PHYS_PORT_CLOSING:
853 case ZFCP_ERP_STEP_PORT_CLOSING:
854 if (fc_host_port_type(adapter->scsi_host) == FC_PORTTYPE_PTP)
855 return zfcp_erp_open_ptp_port(act);
Christof Schmittb98478d2008-12-19 16:56:59 +0100856 if (!port->d_id) {
Swen Schillig947a9ac2009-03-02 13:09:10 +0100857 zfcp_port_get(port);
Swen Schillig45446832009-08-18 15:43:17 +0200858 if (!queue_work(adapter->work_queue,
Swen Schillig947a9ac2009-03-02 13:09:10 +0100859 &port->gid_pn_work))
860 zfcp_port_put(port);
Christof Schmitt799b76d2009-08-18 15:43:20 +0200861 return ZFCP_ERP_EXIT;
Christof Schmitt287ac012008-07-02 10:56:40 +0200862 }
Christof Schmitt287ac012008-07-02 10:56:40 +0200863 return zfcp_erp_port_strategy_open_port(act);
864
865 case ZFCP_ERP_STEP_PORT_OPENING:
866 /* D_ID might have changed during open */
Swen Schillig5ab944f2008-10-01 12:42:17 +0200867 if (p_status & ZFCP_STATUS_COMMON_OPEN) {
Christof Schmittb98478d2008-12-19 16:56:59 +0100868 if (port->d_id)
Swen Schillig5ab944f2008-10-01 12:42:17 +0200869 return ZFCP_ERP_SUCCEEDED;
870 else {
871 act->step = ZFCP_ERP_STEP_PORT_CLOSING;
872 return ZFCP_ERP_CONTINUES;
873 }
Swen Schillig5ab944f2008-10-01 12:42:17 +0200874 }
Swen Schilligea460a82009-05-15 13:18:20 +0200875 if (port->d_id && !(p_status & ZFCP_STATUS_COMMON_NOESC)) {
876 port->d_id = 0;
877 _zfcp_erp_port_reopen(port, 0, "erpsoc1", NULL);
878 return ZFCP_ERP_EXIT;
879 }
880 /* fall through otherwise */
Christof Schmitt287ac012008-07-02 10:56:40 +0200881 }
882 return ZFCP_ERP_FAILED;
883}
884
Christof Schmitt287ac012008-07-02 10:56:40 +0200885static int zfcp_erp_port_strategy(struct zfcp_erp_action *erp_action)
886{
887 struct zfcp_port *port = erp_action->port;
888
Swen Schillig5ab944f2008-10-01 12:42:17 +0200889 if (atomic_read(&port->status) & ZFCP_STATUS_COMMON_NOESC)
890 goto close_init_done;
891
Christof Schmitt287ac012008-07-02 10:56:40 +0200892 switch (erp_action->step) {
893 case ZFCP_ERP_STEP_UNINITIALIZED:
894 zfcp_erp_port_strategy_clearstati(port);
895 if (atomic_read(&port->status) & ZFCP_STATUS_COMMON_OPEN)
896 return zfcp_erp_port_strategy_close(erp_action);
897 break;
898
899 case ZFCP_ERP_STEP_PORT_CLOSING:
900 if (atomic_read(&port->status) & ZFCP_STATUS_COMMON_OPEN)
901 return ZFCP_ERP_FAILED;
902 break;
903 }
Swen Schillig5ab944f2008-10-01 12:42:17 +0200904
905close_init_done:
Christof Schmitt287ac012008-07-02 10:56:40 +0200906 if (erp_action->status & ZFCP_STATUS_ERP_CLOSE_ONLY)
907 return ZFCP_ERP_EXIT;
Christof Schmitt287ac012008-07-02 10:56:40 +0200908
Swen Schillig5ab944f2008-10-01 12:42:17 +0200909 return zfcp_erp_port_strategy_open_common(erp_action);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910}
911
Christof Schmitt287ac012008-07-02 10:56:40 +0200912static void zfcp_erp_unit_strategy_clearstati(struct zfcp_unit *unit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913{
Swen Schillig44cc76f2008-10-01 12:42:16 +0200914 atomic_clear_mask(ZFCP_STATUS_COMMON_ACCESS_DENIED |
Christof Schmitt287ac012008-07-02 10:56:40 +0200915 ZFCP_STATUS_UNIT_SHARED |
916 ZFCP_STATUS_UNIT_READONLY,
917 &unit->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918}
919
Christof Schmitt287ac012008-07-02 10:56:40 +0200920static int zfcp_erp_unit_strategy_close(struct zfcp_erp_action *erp_action)
921{
922 int retval = zfcp_fsf_close_unit(erp_action);
923 if (retval == -ENOMEM)
924 return ZFCP_ERP_NOMEM;
925 erp_action->step = ZFCP_ERP_STEP_UNIT_CLOSING;
926 if (retval)
927 return ZFCP_ERP_FAILED;
928 return ZFCP_ERP_CONTINUES;
929}
930
931static int zfcp_erp_unit_strategy_open(struct zfcp_erp_action *erp_action)
932{
933 int retval = zfcp_fsf_open_unit(erp_action);
934 if (retval == -ENOMEM)
935 return ZFCP_ERP_NOMEM;
936 erp_action->step = ZFCP_ERP_STEP_UNIT_OPENING;
937 if (retval)
938 return ZFCP_ERP_FAILED;
939 return ZFCP_ERP_CONTINUES;
940}
941
942static int zfcp_erp_unit_strategy(struct zfcp_erp_action *erp_action)
943{
944 struct zfcp_unit *unit = erp_action->unit;
945
946 switch (erp_action->step) {
947 case ZFCP_ERP_STEP_UNINITIALIZED:
948 zfcp_erp_unit_strategy_clearstati(unit);
949 if (atomic_read(&unit->status) & ZFCP_STATUS_COMMON_OPEN)
950 return zfcp_erp_unit_strategy_close(erp_action);
951 /* already closed, fall through */
952 case ZFCP_ERP_STEP_UNIT_CLOSING:
953 if (atomic_read(&unit->status) & ZFCP_STATUS_COMMON_OPEN)
954 return ZFCP_ERP_FAILED;
955 if (erp_action->status & ZFCP_STATUS_ERP_CLOSE_ONLY)
956 return ZFCP_ERP_EXIT;
957 return zfcp_erp_unit_strategy_open(erp_action);
958
959 case ZFCP_ERP_STEP_UNIT_OPENING:
960 if (atomic_read(&unit->status) & ZFCP_STATUS_COMMON_OPEN)
961 return ZFCP_ERP_SUCCEEDED;
962 }
963 return ZFCP_ERP_FAILED;
964}
965
966static int zfcp_erp_strategy_check_unit(struct zfcp_unit *unit, int result)
967{
968 switch (result) {
969 case ZFCP_ERP_SUCCEEDED :
970 atomic_set(&unit->erp_counter, 0);
971 zfcp_erp_unit_unblock(unit);
972 break;
973 case ZFCP_ERP_FAILED :
974 atomic_inc(&unit->erp_counter);
Christof Schmittff3b24f2008-10-01 12:42:15 +0200975 if (atomic_read(&unit->erp_counter) > ZFCP_MAX_ERPS) {
976 dev_err(&unit->port->adapter->ccw_device->dev,
977 "ERP failed for unit 0x%016Lx on "
978 "port 0x%016Lx\n",
Swen Schillig7ba58c92008-10-01 12:42:18 +0200979 (unsigned long long)unit->fcp_lun,
980 (unsigned long long)unit->port->wwpn);
Swen Schillig5ffd51a2009-03-02 13:09:04 +0100981 zfcp_erp_unit_failed(unit, "erusck1", NULL);
Christof Schmittff3b24f2008-10-01 12:42:15 +0200982 }
Christof Schmitt287ac012008-07-02 10:56:40 +0200983 break;
984 }
985
986 if (atomic_read(&unit->status) & ZFCP_STATUS_COMMON_ERP_FAILED) {
987 zfcp_erp_unit_block(unit, 0);
988 result = ZFCP_ERP_EXIT;
989 }
990 return result;
991}
992
993static int zfcp_erp_strategy_check_port(struct zfcp_port *port, int result)
994{
995 switch (result) {
996 case ZFCP_ERP_SUCCEEDED :
997 atomic_set(&port->erp_counter, 0);
998 zfcp_erp_port_unblock(port);
999 break;
1000
1001 case ZFCP_ERP_FAILED :
1002 if (atomic_read(&port->status) & ZFCP_STATUS_COMMON_NOESC) {
1003 zfcp_erp_port_block(port, 0);
1004 result = ZFCP_ERP_EXIT;
1005 }
1006 atomic_inc(&port->erp_counter);
Christof Schmittff3b24f2008-10-01 12:42:15 +02001007 if (atomic_read(&port->erp_counter) > ZFCP_MAX_ERPS) {
1008 dev_err(&port->adapter->ccw_device->dev,
1009 "ERP failed for remote port 0x%016Lx\n",
Swen Schillig7ba58c92008-10-01 12:42:18 +02001010 (unsigned long long)port->wwpn);
Swen Schillig5ffd51a2009-03-02 13:09:04 +01001011 zfcp_erp_port_failed(port, "erpsck1", NULL);
Christof Schmittff3b24f2008-10-01 12:42:15 +02001012 }
Christof Schmitt287ac012008-07-02 10:56:40 +02001013 break;
1014 }
1015
1016 if (atomic_read(&port->status) & ZFCP_STATUS_COMMON_ERP_FAILED) {
1017 zfcp_erp_port_block(port, 0);
1018 result = ZFCP_ERP_EXIT;
1019 }
1020 return result;
1021}
1022
1023static int zfcp_erp_strategy_check_adapter(struct zfcp_adapter *adapter,
1024 int result)
1025{
1026 switch (result) {
1027 case ZFCP_ERP_SUCCEEDED :
1028 atomic_set(&adapter->erp_counter, 0);
1029 zfcp_erp_adapter_unblock(adapter);
1030 break;
1031
1032 case ZFCP_ERP_FAILED :
1033 atomic_inc(&adapter->erp_counter);
Christof Schmittff3b24f2008-10-01 12:42:15 +02001034 if (atomic_read(&adapter->erp_counter) > ZFCP_MAX_ERPS) {
1035 dev_err(&adapter->ccw_device->dev,
1036 "ERP cannot recover an error "
1037 "on the FCP device\n");
Swen Schillig5ffd51a2009-03-02 13:09:04 +01001038 zfcp_erp_adapter_failed(adapter, "erasck1", NULL);
Christof Schmittff3b24f2008-10-01 12:42:15 +02001039 }
Christof Schmitt287ac012008-07-02 10:56:40 +02001040 break;
1041 }
1042
1043 if (atomic_read(&adapter->status) & ZFCP_STATUS_COMMON_ERP_FAILED) {
1044 zfcp_erp_adapter_block(adapter, 0);
1045 result = ZFCP_ERP_EXIT;
1046 }
1047 return result;
1048}
1049
1050static int zfcp_erp_strategy_check_target(struct zfcp_erp_action *erp_action,
1051 int result)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052{
1053 struct zfcp_adapter *adapter = erp_action->adapter;
1054 struct zfcp_port *port = erp_action->port;
1055 struct zfcp_unit *unit = erp_action->unit;
1056
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 switch (erp_action->action) {
1058
1059 case ZFCP_ERP_ACTION_REOPEN_UNIT:
1060 result = zfcp_erp_strategy_check_unit(unit, result);
1061 break;
1062
1063 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
1064 case ZFCP_ERP_ACTION_REOPEN_PORT:
1065 result = zfcp_erp_strategy_check_port(port, result);
1066 break;
1067
1068 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
1069 result = zfcp_erp_strategy_check_adapter(adapter, result);
1070 break;
1071 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 return result;
1073}
1074
Christof Schmitt287ac012008-07-02 10:56:40 +02001075static int zfcp_erp_strat_change_det(atomic_t *target_status, u32 erp_status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076{
Christof Schmitt287ac012008-07-02 10:56:40 +02001077 int status = atomic_read(target_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078
Christof Schmitt287ac012008-07-02 10:56:40 +02001079 if ((status & ZFCP_STATUS_COMMON_RUNNING) &&
1080 (erp_status & ZFCP_STATUS_ERP_CLOSE_ONLY))
1081 return 1; /* take it online */
1082
1083 if (!(status & ZFCP_STATUS_COMMON_RUNNING) &&
1084 !(erp_status & ZFCP_STATUS_ERP_CLOSE_ONLY))
1085 return 1; /* take it offline */
1086
1087 return 0;
1088}
1089
1090static int zfcp_erp_strategy_statechange(struct zfcp_erp_action *act, int ret)
1091{
1092 int action = act->action;
1093 struct zfcp_adapter *adapter = act->adapter;
1094 struct zfcp_port *port = act->port;
1095 struct zfcp_unit *unit = act->unit;
1096 u32 erp_status = act->status;
1097
1098 switch (action) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
Christof Schmitt287ac012008-07-02 10:56:40 +02001100 if (zfcp_erp_strat_change_det(&adapter->status, erp_status)) {
1101 _zfcp_erp_adapter_reopen(adapter,
1102 ZFCP_STATUS_COMMON_ERP_FAILED,
Swen Schillig5ffd51a2009-03-02 13:09:04 +01001103 "ersscg1", NULL);
Christof Schmitt287ac012008-07-02 10:56:40 +02001104 return ZFCP_ERP_EXIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 }
1106 break;
1107
1108 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
1109 case ZFCP_ERP_ACTION_REOPEN_PORT:
Christof Schmitt287ac012008-07-02 10:56:40 +02001110 if (zfcp_erp_strat_change_det(&port->status, erp_status)) {
1111 _zfcp_erp_port_reopen(port,
1112 ZFCP_STATUS_COMMON_ERP_FAILED,
Swen Schillig5ffd51a2009-03-02 13:09:04 +01001113 "ersscg2", NULL);
Christof Schmitt287ac012008-07-02 10:56:40 +02001114 return ZFCP_ERP_EXIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 }
1116 break;
1117
1118 case ZFCP_ERP_ACTION_REOPEN_UNIT:
Christof Schmitt287ac012008-07-02 10:56:40 +02001119 if (zfcp_erp_strat_change_det(&unit->status, erp_status)) {
1120 _zfcp_erp_unit_reopen(unit,
1121 ZFCP_STATUS_COMMON_ERP_FAILED,
Swen Schillig5ffd51a2009-03-02 13:09:04 +01001122 "ersscg3", NULL);
Christof Schmitt287ac012008-07-02 10:56:40 +02001123 return ZFCP_ERP_EXIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 }
1125 break;
1126 }
Christof Schmitt287ac012008-07-02 10:56:40 +02001127 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128}
1129
Christof Schmitt287ac012008-07-02 10:56:40 +02001130static void zfcp_erp_action_dequeue(struct zfcp_erp_action *erp_action)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131{
Christof Schmitt287ac012008-07-02 10:56:40 +02001132 struct zfcp_adapter *adapter = erp_action->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133
Christof Schmitt287ac012008-07-02 10:56:40 +02001134 adapter->erp_total_count--;
1135 if (erp_action->status & ZFCP_STATUS_ERP_LOWMEM) {
1136 adapter->erp_low_mem_count--;
1137 erp_action->status &= ~ZFCP_STATUS_ERP_LOWMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 }
1139
Christof Schmitt287ac012008-07-02 10:56:40 +02001140 list_del(&erp_action->list);
Swen Schillig5ffd51a2009-03-02 13:09:04 +01001141 zfcp_rec_dbf_event_action("eractd1", erp_action);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142
Christof Schmitt287ac012008-07-02 10:56:40 +02001143 switch (erp_action->action) {
1144 case ZFCP_ERP_ACTION_REOPEN_UNIT:
1145 atomic_clear_mask(ZFCP_STATUS_COMMON_ERP_INUSE,
1146 &erp_action->unit->status);
1147 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148
Christof Schmitt287ac012008-07-02 10:56:40 +02001149 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
1150 case ZFCP_ERP_ACTION_REOPEN_PORT:
1151 atomic_clear_mask(ZFCP_STATUS_COMMON_ERP_INUSE,
1152 &erp_action->port->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 break;
Christof Schmitt287ac012008-07-02 10:56:40 +02001154
1155 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
1156 atomic_clear_mask(ZFCP_STATUS_COMMON_ERP_INUSE,
1157 &erp_action->adapter->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 break;
1159 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160}
1161
Christof Schmitt287ac012008-07-02 10:56:40 +02001162static void zfcp_erp_action_cleanup(struct zfcp_erp_action *act, int result)
Maxim Shchetyninaef4a982005-09-13 21:51:16 +02001163{
Christof Schmitt287ac012008-07-02 10:56:40 +02001164 struct zfcp_adapter *adapter = act->adapter;
1165 struct zfcp_port *port = act->port;
1166 struct zfcp_unit *unit = act->unit;
Maxim Shchetyninaef4a982005-09-13 21:51:16 +02001167
Christof Schmitt287ac012008-07-02 10:56:40 +02001168 switch (act->action) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 case ZFCP_ERP_ACTION_REOPEN_UNIT:
Christof Schmitta2fa0ae2009-03-02 13:09:08 +01001170 if ((result == ZFCP_ERP_SUCCEEDED) && !unit->device) {
Swen Schillig92d51932009-04-17 15:08:04 +02001171 zfcp_unit_get(unit);
1172 if (scsi_queue_work(unit->port->adapter->scsi_host,
1173 &unit->scsi_work) <= 0)
1174 zfcp_unit_put(unit);
Andreas Herrmannad58f7d2006-03-10 00:56:16 +01001175 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 zfcp_unit_put(unit);
1177 break;
Christof Schmitt287ac012008-07-02 10:56:40 +02001178
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
1180 case ZFCP_ERP_ACTION_REOPEN_PORT:
Christof Schmitta2fa0ae2009-03-02 13:09:08 +01001181 if (result == ZFCP_ERP_SUCCEEDED)
1182 zfcp_scsi_schedule_rport_register(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 zfcp_port_put(port);
1184 break;
Christof Schmitt287ac012008-07-02 10:56:40 +02001185
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
Christof Schmitta2fa0ae2009-03-02 13:09:08 +01001187 if (result == ZFCP_ERP_SUCCEEDED) {
Christof Schmittbd43a42b72008-12-25 13:38:50 +01001188 register_service_level(&adapter->service_level);
Swen Schilligfca55b62008-11-26 18:07:40 +01001189 schedule_work(&adapter->scan_work);
Christof Schmitta2fa0ae2009-03-02 13:09:08 +01001190 } else
1191 unregister_service_level(&adapter->service_level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 zfcp_adapter_put(adapter);
1193 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 }
1195}
1196
Christof Schmitt287ac012008-07-02 10:56:40 +02001197static int zfcp_erp_strategy_do_action(struct zfcp_erp_action *erp_action)
1198{
1199 switch (erp_action->action) {
1200 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
1201 return zfcp_erp_adapter_strategy(erp_action);
1202 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
1203 return zfcp_erp_port_forced_strategy(erp_action);
1204 case ZFCP_ERP_ACTION_REOPEN_PORT:
1205 return zfcp_erp_port_strategy(erp_action);
1206 case ZFCP_ERP_ACTION_REOPEN_UNIT:
1207 return zfcp_erp_unit_strategy(erp_action);
1208 }
1209 return ZFCP_ERP_FAILED;
1210}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211
Christof Schmitt287ac012008-07-02 10:56:40 +02001212static int zfcp_erp_strategy(struct zfcp_erp_action *erp_action)
1213{
1214 int retval;
1215 struct zfcp_adapter *adapter = erp_action->adapter;
1216 unsigned long flags;
1217
1218 read_lock_irqsave(&zfcp_data.config_lock, flags);
1219 write_lock(&adapter->erp_lock);
1220
1221 zfcp_erp_strategy_check_fsfreq(erp_action);
1222
1223 if (erp_action->status & ZFCP_STATUS_ERP_DISMISSED) {
1224 zfcp_erp_action_dequeue(erp_action);
1225 retval = ZFCP_ERP_DISMISSED;
1226 goto unlock;
1227 }
1228
1229 zfcp_erp_action_to_running(erp_action);
1230
1231 /* no lock to allow for blocking operations */
1232 write_unlock(&adapter->erp_lock);
1233 read_unlock_irqrestore(&zfcp_data.config_lock, flags);
1234 retval = zfcp_erp_strategy_do_action(erp_action);
1235 read_lock_irqsave(&zfcp_data.config_lock, flags);
1236 write_lock(&adapter->erp_lock);
1237
1238 if (erp_action->status & ZFCP_STATUS_ERP_DISMISSED)
1239 retval = ZFCP_ERP_CONTINUES;
1240
1241 switch (retval) {
1242 case ZFCP_ERP_NOMEM:
1243 if (!(erp_action->status & ZFCP_STATUS_ERP_LOWMEM)) {
1244 ++adapter->erp_low_mem_count;
1245 erp_action->status |= ZFCP_STATUS_ERP_LOWMEM;
1246 }
1247 if (adapter->erp_total_count == adapter->erp_low_mem_count)
Swen Schillig5ffd51a2009-03-02 13:09:04 +01001248 _zfcp_erp_adapter_reopen(adapter, 0, "erstgy1", NULL);
Christof Schmitt287ac012008-07-02 10:56:40 +02001249 else {
1250 zfcp_erp_strategy_memwait(erp_action);
1251 retval = ZFCP_ERP_CONTINUES;
1252 }
1253 goto unlock;
1254
1255 case ZFCP_ERP_CONTINUES:
1256 if (erp_action->status & ZFCP_STATUS_ERP_LOWMEM) {
1257 --adapter->erp_low_mem_count;
1258 erp_action->status &= ~ZFCP_STATUS_ERP_LOWMEM;
1259 }
1260 goto unlock;
1261 }
1262
1263 retval = zfcp_erp_strategy_check_target(erp_action, retval);
1264 zfcp_erp_action_dequeue(erp_action);
1265 retval = zfcp_erp_strategy_statechange(erp_action, retval);
1266 if (retval == ZFCP_ERP_EXIT)
1267 goto unlock;
Christof Schmitt85600f72009-07-13 15:06:09 +02001268 if (retval == ZFCP_ERP_SUCCEEDED)
1269 zfcp_erp_strategy_followup_success(erp_action);
1270 if (retval == ZFCP_ERP_FAILED)
1271 zfcp_erp_strategy_followup_failed(erp_action);
Christof Schmitt287ac012008-07-02 10:56:40 +02001272
1273 unlock:
1274 write_unlock(&adapter->erp_lock);
1275 read_unlock_irqrestore(&zfcp_data.config_lock, flags);
1276
1277 if (retval != ZFCP_ERP_CONTINUES)
1278 zfcp_erp_action_cleanup(erp_action, retval);
1279
1280 return retval;
1281}
1282
1283static int zfcp_erp_thread(void *data)
1284{
1285 struct zfcp_adapter *adapter = (struct zfcp_adapter *) data;
1286 struct list_head *next;
1287 struct zfcp_erp_action *act;
1288 unsigned long flags;
Heiko Carstens06499fa2008-12-19 16:56:56 +01001289 int ignore;
Christof Schmitt287ac012008-07-02 10:56:40 +02001290
Cornelia Huckb9d3aed2008-10-10 21:33:11 +02001291 daemonize("zfcperp%s", dev_name(&adapter->ccw_device->dev));
Christof Schmitt287ac012008-07-02 10:56:40 +02001292 /* Block all signals */
1293 siginitsetinv(&current->blocked, 0);
1294 atomic_set_mask(ZFCP_STATUS_ADAPTER_ERP_THREAD_UP, &adapter->status);
1295 wake_up(&adapter->erp_thread_wqh);
1296
1297 while (!(atomic_read(&adapter->status) &
1298 ZFCP_STATUS_ADAPTER_ERP_THREAD_KILL)) {
Swen Schillig94ab4b32009-04-17 15:08:06 +02001299
1300 zfcp_rec_dbf_event_thread_lock("erthrd1", adapter);
1301 ignore = down_interruptible(&adapter->erp_ready_sem);
1302 zfcp_rec_dbf_event_thread_lock("erthrd2", adapter);
1303
Christof Schmitt287ac012008-07-02 10:56:40 +02001304 write_lock_irqsave(&adapter->erp_lock, flags);
1305 next = adapter->erp_ready_head.next;
1306 write_unlock_irqrestore(&adapter->erp_lock, flags);
1307
1308 if (next != &adapter->erp_ready_head) {
1309 act = list_entry(next, struct zfcp_erp_action, list);
1310
1311 /* there is more to come after dismission, no notify */
1312 if (zfcp_erp_strategy(act) != ZFCP_ERP_DISMISSED)
1313 zfcp_erp_wakeup(adapter);
1314 }
Christof Schmitt287ac012008-07-02 10:56:40 +02001315 }
1316
1317 atomic_clear_mask(ZFCP_STATUS_ADAPTER_ERP_THREAD_UP, &adapter->status);
1318 wake_up(&adapter->erp_thread_wqh);
1319
1320 return 0;
1321}
1322
1323/**
1324 * zfcp_erp_thread_setup - Start ERP thread for adapter
1325 * @adapter: Adapter to start the ERP thread for
1326 *
1327 * Returns 0 on success or error code from kernel_thread()
1328 */
1329int zfcp_erp_thread_setup(struct zfcp_adapter *adapter)
1330{
1331 int retval;
1332
1333 atomic_clear_mask(ZFCP_STATUS_ADAPTER_ERP_THREAD_UP, &adapter->status);
1334 retval = kernel_thread(zfcp_erp_thread, adapter, SIGCHLD);
1335 if (retval < 0) {
1336 dev_err(&adapter->ccw_device->dev,
Christof Schmittff3b24f2008-10-01 12:42:15 +02001337 "Creating an ERP thread for the FCP device failed.\n");
Christof Schmitt287ac012008-07-02 10:56:40 +02001338 return retval;
1339 }
1340 wait_event(adapter->erp_thread_wqh,
1341 atomic_read(&adapter->status) &
1342 ZFCP_STATUS_ADAPTER_ERP_THREAD_UP);
1343 return 0;
1344}
1345
1346/**
1347 * zfcp_erp_thread_kill - Stop ERP thread.
1348 * @adapter: Adapter where the ERP thread should be stopped.
1349 *
1350 * The caller of this routine ensures that the specified adapter has
1351 * been shut down and that this operation has been completed. Thus,
1352 * there are no pending erp_actions which would need to be handled
1353 * here.
1354 */
1355void zfcp_erp_thread_kill(struct zfcp_adapter *adapter)
1356{
1357 atomic_set_mask(ZFCP_STATUS_ADAPTER_ERP_THREAD_KILL, &adapter->status);
1358 up(&adapter->erp_ready_sem);
Swen Schillig5ffd51a2009-03-02 13:09:04 +01001359 zfcp_rec_dbf_event_thread_lock("erthrk1", adapter);
Christof Schmitt287ac012008-07-02 10:56:40 +02001360
1361 wait_event(adapter->erp_thread_wqh,
1362 !(atomic_read(&adapter->status) &
1363 ZFCP_STATUS_ADAPTER_ERP_THREAD_UP));
1364
1365 atomic_clear_mask(ZFCP_STATUS_ADAPTER_ERP_THREAD_KILL,
1366 &adapter->status);
1367}
1368
1369/**
1370 * zfcp_erp_adapter_failed - Set adapter status to failed.
1371 * @adapter: Failed adapter.
1372 * @id: Event id for debug trace.
1373 * @ref: Reference for debug trace.
1374 */
Swen Schillig5ffd51a2009-03-02 13:09:04 +01001375void zfcp_erp_adapter_failed(struct zfcp_adapter *adapter, char *id, void *ref)
Christof Schmitt287ac012008-07-02 10:56:40 +02001376{
1377 zfcp_erp_modify_adapter_status(adapter, id, ref,
1378 ZFCP_STATUS_COMMON_ERP_FAILED, ZFCP_SET);
Christof Schmitt287ac012008-07-02 10:56:40 +02001379}
1380
1381/**
1382 * zfcp_erp_port_failed - Set port status to failed.
1383 * @port: Failed port.
1384 * @id: Event id for debug trace.
1385 * @ref: Reference for debug trace.
1386 */
Swen Schillig5ffd51a2009-03-02 13:09:04 +01001387void zfcp_erp_port_failed(struct zfcp_port *port, char *id, void *ref)
Christof Schmitt287ac012008-07-02 10:56:40 +02001388{
1389 zfcp_erp_modify_port_status(port, id, ref,
1390 ZFCP_STATUS_COMMON_ERP_FAILED, ZFCP_SET);
Christof Schmitt287ac012008-07-02 10:56:40 +02001391}
1392
1393/**
1394 * zfcp_erp_unit_failed - Set unit status to failed.
1395 * @unit: Failed unit.
1396 * @id: Event id for debug trace.
1397 * @ref: Reference for debug trace.
1398 */
Swen Schillig5ffd51a2009-03-02 13:09:04 +01001399void zfcp_erp_unit_failed(struct zfcp_unit *unit, char *id, void *ref)
Christof Schmitt287ac012008-07-02 10:56:40 +02001400{
1401 zfcp_erp_modify_unit_status(unit, id, ref,
1402 ZFCP_STATUS_COMMON_ERP_FAILED, ZFCP_SET);
Christof Schmitt287ac012008-07-02 10:56:40 +02001403}
1404
1405/**
1406 * zfcp_erp_wait - wait for completion of error recovery on an adapter
1407 * @adapter: adapter for which to wait for completion of its error recovery
1408 */
1409void zfcp_erp_wait(struct zfcp_adapter *adapter)
1410{
1411 wait_event(adapter->erp_done_wqh,
1412 !(atomic_read(&adapter->status) &
1413 ZFCP_STATUS_ADAPTER_ERP_PENDING));
1414}
1415
1416/**
1417 * zfcp_erp_modify_adapter_status - change adapter status bits
1418 * @adapter: adapter to change the status
1419 * @id: id for the debug trace
1420 * @ref: reference for the debug trace
1421 * @mask: status bits to change
1422 * @set_or_clear: ZFCP_SET or ZFCP_CLEAR
1423 *
1424 * Changes in common status bits are propagated to attached ports and units.
1425 */
Swen Schillig5ffd51a2009-03-02 13:09:04 +01001426void zfcp_erp_modify_adapter_status(struct zfcp_adapter *adapter, char *id,
Christof Schmitt287ac012008-07-02 10:56:40 +02001427 void *ref, u32 mask, int set_or_clear)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 struct zfcp_port *port;
Christof Schmitt287ac012008-07-02 10:56:40 +02001430 u32 common_mask = mask & ZFCP_COMMON_FLAGS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431
Christof Schmitt287ac012008-07-02 10:56:40 +02001432 if (set_or_clear == ZFCP_SET) {
1433 if (status_change_set(mask, &adapter->status))
1434 zfcp_rec_dbf_event_adapter(id, ref, adapter);
1435 atomic_set_mask(mask, &adapter->status);
1436 } else {
1437 if (status_change_clear(mask, &adapter->status))
1438 zfcp_rec_dbf_event_adapter(id, ref, adapter);
1439 atomic_clear_mask(mask, &adapter->status);
1440 if (mask & ZFCP_STATUS_COMMON_ERP_FAILED)
1441 atomic_set(&adapter->erp_counter, 0);
1442 }
1443
1444 if (common_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 list_for_each_entry(port, &adapter->port_list_head, list)
Christof Schmitt287ac012008-07-02 10:56:40 +02001446 zfcp_erp_modify_port_status(port, id, ref, common_mask,
1447 set_or_clear);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448}
1449
Christof Schmitt287ac012008-07-02 10:56:40 +02001450/**
1451 * zfcp_erp_modify_port_status - change port status bits
1452 * @port: port to change the status bits
1453 * @id: id for the debug trace
1454 * @ref: reference for the debug trace
1455 * @mask: status bits to change
1456 * @set_or_clear: ZFCP_SET or ZFCP_CLEAR
1457 *
1458 * Changes in common status bits are propagated to attached units.
1459 */
Swen Schillig5ffd51a2009-03-02 13:09:04 +01001460void zfcp_erp_modify_port_status(struct zfcp_port *port, char *id, void *ref,
Christof Schmitt287ac012008-07-02 10:56:40 +02001461 u32 mask, int set_or_clear)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 struct zfcp_unit *unit;
Christof Schmitt287ac012008-07-02 10:56:40 +02001464 u32 common_mask = mask & ZFCP_COMMON_FLAGS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465
Christof Schmitt287ac012008-07-02 10:56:40 +02001466 if (set_or_clear == ZFCP_SET) {
1467 if (status_change_set(mask, &port->status))
1468 zfcp_rec_dbf_event_port(id, ref, port);
1469 atomic_set_mask(mask, &port->status);
1470 } else {
1471 if (status_change_clear(mask, &port->status))
1472 zfcp_rec_dbf_event_port(id, ref, port);
1473 atomic_clear_mask(mask, &port->status);
1474 if (mask & ZFCP_STATUS_COMMON_ERP_FAILED)
1475 atomic_set(&port->erp_counter, 0);
1476 }
1477
1478 if (common_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 list_for_each_entry(unit, &port->unit_list_head, list)
Christof Schmitt287ac012008-07-02 10:56:40 +02001480 zfcp_erp_modify_unit_status(unit, id, ref, common_mask,
1481 set_or_clear);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482}
1483
Christof Schmitt287ac012008-07-02 10:56:40 +02001484/**
1485 * zfcp_erp_modify_unit_status - change unit status bits
1486 * @unit: unit to change the status bits
1487 * @id: id for the debug trace
1488 * @ref: reference for the debug trace
1489 * @mask: status bits to change
1490 * @set_or_clear: ZFCP_SET or ZFCP_CLEAR
1491 */
Swen Schillig5ffd51a2009-03-02 13:09:04 +01001492void zfcp_erp_modify_unit_status(struct zfcp_unit *unit, char *id, void *ref,
Christof Schmitt287ac012008-07-02 10:56:40 +02001493 u32 mask, int set_or_clear)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494{
Christof Schmitt287ac012008-07-02 10:56:40 +02001495 if (set_or_clear == ZFCP_SET) {
1496 if (status_change_set(mask, &unit->status))
1497 zfcp_rec_dbf_event_unit(id, ref, unit);
1498 atomic_set_mask(mask, &unit->status);
1499 } else {
1500 if (status_change_clear(mask, &unit->status))
1501 zfcp_rec_dbf_event_unit(id, ref, unit);
1502 atomic_clear_mask(mask, &unit->status);
1503 if (mask & ZFCP_STATUS_COMMON_ERP_FAILED) {
1504 atomic_set(&unit->erp_counter, 0);
1505 }
1506 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507}
1508
Christof Schmitt287ac012008-07-02 10:56:40 +02001509/**
1510 * zfcp_erp_port_boxed - Mark port as "boxed" and start ERP
1511 * @port: The "boxed" port.
1512 * @id: The debug trace id.
1513 * @id: Reference for the debug trace.
1514 */
Swen Schillig5ffd51a2009-03-02 13:09:04 +01001515void zfcp_erp_port_boxed(struct zfcp_port *port, char *id, void *ref)
Andreas Herrmannd736a27b2005-06-13 13:23:57 +02001516{
Andreas Herrmannd736a27b2005-06-13 13:23:57 +02001517 unsigned long flags;
1518
Andreas Herrmannd736a27b2005-06-13 13:23:57 +02001519 read_lock_irqsave(&zfcp_data.config_lock, flags);
Martin Peschke698ec0162008-03-27 14:22:02 +01001520 zfcp_erp_modify_port_status(port, id, ref,
1521 ZFCP_STATUS_COMMON_ACCESS_BOXED, ZFCP_SET);
Andreas Herrmannd736a27b2005-06-13 13:23:57 +02001522 read_unlock_irqrestore(&zfcp_data.config_lock, flags);
Martin Peschke9467a9b2008-03-27 14:22:03 +01001523 zfcp_erp_port_reopen(port, ZFCP_STATUS_COMMON_ERP_FAILED, id, ref);
Andreas Herrmannd736a27b2005-06-13 13:23:57 +02001524}
1525
Christof Schmitt287ac012008-07-02 10:56:40 +02001526/**
1527 * zfcp_erp_unit_boxed - Mark unit as "boxed" and start ERP
1528 * @port: The "boxed" unit.
1529 * @id: The debug trace id.
1530 * @id: Reference for the debug trace.
1531 */
Swen Schillig5ffd51a2009-03-02 13:09:04 +01001532void zfcp_erp_unit_boxed(struct zfcp_unit *unit, char *id, void *ref)
Andreas Herrmannd736a27b2005-06-13 13:23:57 +02001533{
Martin Peschke698ec0162008-03-27 14:22:02 +01001534 zfcp_erp_modify_unit_status(unit, id, ref,
1535 ZFCP_STATUS_COMMON_ACCESS_BOXED, ZFCP_SET);
Martin Peschke9467a9b2008-03-27 14:22:03 +01001536 zfcp_erp_unit_reopen(unit, ZFCP_STATUS_COMMON_ERP_FAILED, id, ref);
Andreas Herrmannd736a27b2005-06-13 13:23:57 +02001537}
1538
Christof Schmitt287ac012008-07-02 10:56:40 +02001539/**
1540 * zfcp_erp_port_access_denied - Adapter denied access to port.
1541 * @port: port where access has been denied
1542 * @id: id for debug trace
1543 * @ref: reference for debug trace
1544 *
1545 * Since the adapter has denied access, stop using the port and the
1546 * attached units.
1547 */
Swen Schillig5ffd51a2009-03-02 13:09:04 +01001548void zfcp_erp_port_access_denied(struct zfcp_port *port, char *id, void *ref)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 unsigned long flags;
1551
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 read_lock_irqsave(&zfcp_data.config_lock, flags);
Martin Peschke698ec0162008-03-27 14:22:02 +01001553 zfcp_erp_modify_port_status(port, id, ref,
1554 ZFCP_STATUS_COMMON_ERP_FAILED |
1555 ZFCP_STATUS_COMMON_ACCESS_DENIED, ZFCP_SET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 read_unlock_irqrestore(&zfcp_data.config_lock, flags);
1557}
1558
Christof Schmitt287ac012008-07-02 10:56:40 +02001559/**
1560 * zfcp_erp_unit_access_denied - Adapter denied access to unit.
1561 * @unit: unit where access has been denied
1562 * @id: id for debug trace
1563 * @ref: reference for debug trace
1564 *
1565 * Since the adapter has denied access, stop using the unit.
1566 */
Swen Schillig5ffd51a2009-03-02 13:09:04 +01001567void zfcp_erp_unit_access_denied(struct zfcp_unit *unit, char *id, void *ref)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568{
Martin Peschke698ec0162008-03-27 14:22:02 +01001569 zfcp_erp_modify_unit_status(unit, id, ref,
1570 ZFCP_STATUS_COMMON_ERP_FAILED |
1571 ZFCP_STATUS_COMMON_ACCESS_DENIED, ZFCP_SET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572}
1573
Swen Schillig5ffd51a2009-03-02 13:09:04 +01001574static void zfcp_erp_unit_access_changed(struct zfcp_unit *unit, char *id,
Christof Schmitt287ac012008-07-02 10:56:40 +02001575 void *ref)
1576{
1577 int status = atomic_read(&unit->status);
1578 if (!(status & (ZFCP_STATUS_COMMON_ACCESS_DENIED |
1579 ZFCP_STATUS_COMMON_ACCESS_BOXED)))
1580 return;
1581
1582 zfcp_erp_unit_reopen(unit, ZFCP_STATUS_COMMON_ERP_FAILED, id, ref);
1583}
1584
Swen Schillig5ffd51a2009-03-02 13:09:04 +01001585static void zfcp_erp_port_access_changed(struct zfcp_port *port, char *id,
Christof Schmitt287ac012008-07-02 10:56:40 +02001586 void *ref)
1587{
1588 struct zfcp_unit *unit;
1589 int status = atomic_read(&port->status);
1590
1591 if (!(status & (ZFCP_STATUS_COMMON_ACCESS_DENIED |
1592 ZFCP_STATUS_COMMON_ACCESS_BOXED))) {
Swen Schillig5ab944f2008-10-01 12:42:17 +02001593 list_for_each_entry(unit, &port->unit_list_head, list)
1594 zfcp_erp_unit_access_changed(unit, id, ref);
Christof Schmitt287ac012008-07-02 10:56:40 +02001595 return;
1596 }
1597
1598 zfcp_erp_port_reopen(port, ZFCP_STATUS_COMMON_ERP_FAILED, id, ref);
1599}
1600
1601/**
1602 * zfcp_erp_adapter_access_changed - Process change in adapter ACT
1603 * @adapter: Adapter where the Access Control Table (ACT) changed
1604 * @id: Id for debug trace
1605 * @ref: Reference for debug trace
1606 */
Swen Schillig5ffd51a2009-03-02 13:09:04 +01001607void zfcp_erp_adapter_access_changed(struct zfcp_adapter *adapter, char *id,
Martin Peschke1f6f7122008-04-18 12:51:55 +02001608 void *ref)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609{
1610 struct zfcp_port *port;
1611 unsigned long flags;
1612
Maxim Shchetyninaef4a982005-09-13 21:51:16 +02001613 if (adapter->connection_features & FSF_FEATURE_NPIV_MODE)
1614 return;
1615
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 read_lock_irqsave(&zfcp_data.config_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 list_for_each_entry(port, &adapter->port_list_head, list)
Swen Schillig5ab944f2008-10-01 12:42:17 +02001618 zfcp_erp_port_access_changed(port, id, ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 read_unlock_irqrestore(&zfcp_data.config_lock, flags);
1620}