blob: e7d3bce51429a596787970d791d9658d47acd61d [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 Schmitt553448f2008-06-10 18:20:58 +02006 * Copyright IBM Corporation 2002, 2008
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 */
8
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include "zfcp_ext.h"
10
Christof Schmitt287ac012008-07-02 10:56:40 +020011#define ZFCP_MAX_ERPS 3
Linus Torvalds1da177e2005-04-16 15:20:36 -070012
Christof Schmitt287ac012008-07-02 10:56:40 +020013enum zfcp_erp_act_flags {
14 ZFCP_STATUS_ERP_TIMEDOUT = 0x10000000,
15 ZFCP_STATUS_ERP_CLOSE_ONLY = 0x01000000,
16 ZFCP_STATUS_ERP_DISMISSING = 0x00100000,
17 ZFCP_STATUS_ERP_DISMISSED = 0x00200000,
18 ZFCP_STATUS_ERP_LOWMEM = 0x00400000,
19};
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
Christof Schmitt287ac012008-07-02 10:56:40 +020021enum zfcp_erp_steps {
22 ZFCP_ERP_STEP_UNINITIALIZED = 0x0000,
23 ZFCP_ERP_STEP_FSF_XCONFIG = 0x0001,
24 ZFCP_ERP_STEP_PHYS_PORT_CLOSING = 0x0010,
25 ZFCP_ERP_STEP_PORT_CLOSING = 0x0100,
Christof Schmitt287ac012008-07-02 10:56:40 +020026 ZFCP_ERP_STEP_NAMESERVER_LOOKUP = 0x0400,
27 ZFCP_ERP_STEP_PORT_OPENING = 0x0800,
28 ZFCP_ERP_STEP_UNIT_CLOSING = 0x1000,
29 ZFCP_ERP_STEP_UNIT_OPENING = 0x2000,
30};
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
Christof Schmitt287ac012008-07-02 10:56:40 +020032enum zfcp_erp_act_type {
33 ZFCP_ERP_ACTION_REOPEN_UNIT = 1,
34 ZFCP_ERP_ACTION_REOPEN_PORT = 2,
35 ZFCP_ERP_ACTION_REOPEN_PORT_FORCED = 3,
36 ZFCP_ERP_ACTION_REOPEN_ADAPTER = 4,
37};
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
Christof Schmitt287ac012008-07-02 10:56:40 +020039enum zfcp_erp_act_state {
40 ZFCP_ERP_ACTION_RUNNING = 1,
41 ZFCP_ERP_ACTION_READY = 2,
42};
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Christof Schmitt287ac012008-07-02 10:56:40 +020044enum zfcp_erp_act_result {
45 ZFCP_ERP_SUCCEEDED = 0,
46 ZFCP_ERP_FAILED = 1,
47 ZFCP_ERP_CONTINUES = 2,
48 ZFCP_ERP_EXIT = 3,
49 ZFCP_ERP_DISMISSED = 4,
50 ZFCP_ERP_NOMEM = 5,
51};
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Christof Schmitt287ac012008-07-02 10:56:40 +020053static void zfcp_erp_adapter_block(struct zfcp_adapter *adapter, int mask)
Andreas Herrmann2abbe862006-09-18 22:29:56 +020054{
Christof Schmitt287ac012008-07-02 10:56:40 +020055 zfcp_erp_modify_adapter_status(adapter, 15, NULL,
56 ZFCP_STATUS_COMMON_UNBLOCKED | mask,
57 ZFCP_CLEAR);
Andreas Herrmann2abbe862006-09-18 22:29:56 +020058}
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Christof Schmitt287ac012008-07-02 10:56:40 +020060static int zfcp_erp_action_exists(struct zfcp_erp_action *act)
Linus Torvalds1da177e2005-04-16 15:20:36 -070061{
Christof Schmitt287ac012008-07-02 10:56:40 +020062 struct zfcp_erp_action *curr_act;
63
64 list_for_each_entry(curr_act, &act->adapter->erp_running_head, list)
65 if (act == curr_act)
66 return ZFCP_ERP_ACTION_RUNNING;
67 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068}
69
Christof Schmitt287ac012008-07-02 10:56:40 +020070static void zfcp_erp_action_ready(struct zfcp_erp_action *act)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071{
Christof Schmitt287ac012008-07-02 10:56:40 +020072 struct zfcp_adapter *adapter = act->adapter;
73
74 list_move(&act->list, &act->adapter->erp_ready_head);
75 zfcp_rec_dbf_event_action(146, act);
76 up(&adapter->erp_ready_sem);
77 zfcp_rec_dbf_event_thread(2, adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078}
79
Christof Schmitt287ac012008-07-02 10:56:40 +020080static void zfcp_erp_action_dismiss(struct zfcp_erp_action *act)
Linus Torvalds1da177e2005-04-16 15:20:36 -070081{
Christof Schmitt287ac012008-07-02 10:56:40 +020082 act->status |= ZFCP_STATUS_ERP_DISMISSED;
83 if (zfcp_erp_action_exists(act) == ZFCP_ERP_ACTION_RUNNING)
84 zfcp_erp_action_ready(act);
85}
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
Christof Schmitt287ac012008-07-02 10:56:40 +020087static void zfcp_erp_action_dismiss_unit(struct zfcp_unit *unit)
88{
89 if (atomic_read(&unit->status) & ZFCP_STATUS_COMMON_ERP_INUSE)
90 zfcp_erp_action_dismiss(&unit->erp_action);
91}
92
93static void zfcp_erp_action_dismiss_port(struct zfcp_port *port)
94{
95 struct zfcp_unit *unit;
96
97 if (atomic_read(&port->status) & ZFCP_STATUS_COMMON_ERP_INUSE)
98 zfcp_erp_action_dismiss(&port->erp_action);
99 else
100 list_for_each_entry(unit, &port->unit_list_head, list)
101 zfcp_erp_action_dismiss_unit(unit);
102}
103
104static void zfcp_erp_action_dismiss_adapter(struct zfcp_adapter *adapter)
105{
106 struct zfcp_port *port;
107
108 if (atomic_read(&adapter->status) & ZFCP_STATUS_COMMON_ERP_INUSE)
109 zfcp_erp_action_dismiss(&adapter->erp_action);
110 else
111 list_for_each_entry(port, &adapter->port_list_head, list)
112 zfcp_erp_action_dismiss_port(port);
113}
114
115static int zfcp_erp_required_act(int want, struct zfcp_adapter *adapter,
116 struct zfcp_port *port,
117 struct zfcp_unit *unit)
118{
119 int need = want;
120 int u_status, p_status, a_status;
121
122 switch (want) {
123 case ZFCP_ERP_ACTION_REOPEN_UNIT:
124 u_status = atomic_read(&unit->status);
125 if (u_status & ZFCP_STATUS_COMMON_ERP_INUSE)
126 return 0;
127 p_status = atomic_read(&port->status);
128 if (!(p_status & ZFCP_STATUS_COMMON_RUNNING) ||
129 p_status & ZFCP_STATUS_COMMON_ERP_FAILED)
130 return 0;
131 if (!(p_status & ZFCP_STATUS_COMMON_UNBLOCKED))
132 need = ZFCP_ERP_ACTION_REOPEN_PORT;
133 /* fall through */
134 case ZFCP_ERP_ACTION_REOPEN_PORT:
135 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
136 p_status = atomic_read(&port->status);
137 if (p_status & ZFCP_STATUS_COMMON_ERP_INUSE)
138 return 0;
139 a_status = atomic_read(&adapter->status);
140 if (!(a_status & ZFCP_STATUS_COMMON_RUNNING) ||
141 a_status & ZFCP_STATUS_COMMON_ERP_FAILED)
142 return 0;
143 if (!(a_status & ZFCP_STATUS_COMMON_UNBLOCKED))
144 need = ZFCP_ERP_ACTION_REOPEN_ADAPTER;
145 /* fall through */
146 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
147 a_status = atomic_read(&adapter->status);
148 if (a_status & ZFCP_STATUS_COMMON_ERP_INUSE)
149 return 0;
150 }
151
152 return need;
153}
154
155static struct zfcp_erp_action *zfcp_erp_setup_act(int need,
156 struct zfcp_adapter *adapter,
157 struct zfcp_port *port,
158 struct zfcp_unit *unit)
159{
160 struct zfcp_erp_action *erp_action;
161 u32 status = 0;
162
163 switch (need) {
164 case ZFCP_ERP_ACTION_REOPEN_UNIT:
165 zfcp_unit_get(unit);
166 atomic_set_mask(ZFCP_STATUS_COMMON_ERP_INUSE, &unit->status);
167 erp_action = &unit->erp_action;
168 if (!(atomic_read(&unit->status) & ZFCP_STATUS_COMMON_RUNNING))
169 status = ZFCP_STATUS_ERP_CLOSE_ONLY;
170 break;
171
172 case ZFCP_ERP_ACTION_REOPEN_PORT:
173 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
174 zfcp_port_get(port);
175 zfcp_erp_action_dismiss_port(port);
176 atomic_set_mask(ZFCP_STATUS_COMMON_ERP_INUSE, &port->status);
177 erp_action = &port->erp_action;
178 if (!(atomic_read(&port->status) & ZFCP_STATUS_COMMON_RUNNING))
179 status = ZFCP_STATUS_ERP_CLOSE_ONLY;
180 break;
181
182 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
183 zfcp_adapter_get(adapter);
184 zfcp_erp_action_dismiss_adapter(adapter);
185 atomic_set_mask(ZFCP_STATUS_COMMON_ERP_INUSE, &adapter->status);
186 erp_action = &adapter->erp_action;
187 if (!(atomic_read(&adapter->status) &
188 ZFCP_STATUS_COMMON_RUNNING))
189 status = ZFCP_STATUS_ERP_CLOSE_ONLY;
190 break;
191
192 default:
193 return NULL;
194 }
195
196 memset(erp_action, 0, sizeof(struct zfcp_erp_action));
197 erp_action->adapter = adapter;
198 erp_action->port = port;
199 erp_action->unit = unit;
200 erp_action->action = need;
201 erp_action->status = status;
202
203 return erp_action;
204}
205
206static int zfcp_erp_action_enqueue(int want, struct zfcp_adapter *adapter,
207 struct zfcp_port *port,
208 struct zfcp_unit *unit, u8 id, void *ref)
209{
210 int retval = 1, need;
211 struct zfcp_erp_action *act = NULL;
212
213 if (!(atomic_read(&adapter->status) &
214 ZFCP_STATUS_ADAPTER_ERP_THREAD_UP))
215 return -EIO;
216
217 need = zfcp_erp_required_act(want, adapter, port, unit);
218 if (!need)
219 goto out;
220
221 atomic_set_mask(ZFCP_STATUS_ADAPTER_ERP_PENDING, &adapter->status);
222 act = zfcp_erp_setup_act(need, adapter, port, unit);
223 if (!act)
224 goto out;
225 ++adapter->erp_total_count;
226 list_add_tail(&act->list, &adapter->erp_ready_head);
227 up(&adapter->erp_ready_sem);
228 zfcp_rec_dbf_event_thread(1, adapter);
229 retval = 0;
230 out:
231 zfcp_rec_dbf_event_trigger(id, ref, want, need, act,
232 adapter, port, unit);
233 return retval;
234}
235
236static int _zfcp_erp_adapter_reopen(struct zfcp_adapter *adapter,
237 int clear_mask, u8 id, void *ref)
238{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 zfcp_erp_adapter_block(adapter, clear_mask);
240
Christof Schmitt287ac012008-07-02 10:56:40 +0200241 /* ensure propagation of failed status to new devices */
242 if (atomic_read(&adapter->status) & ZFCP_STATUS_COMMON_ERP_FAILED) {
Martin Peschke1f6f7122008-04-18 12:51:55 +0200243 zfcp_erp_adapter_failed(adapter, 13, NULL);
Christof Schmitt287ac012008-07-02 10:56:40 +0200244 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 }
Christof Schmitt287ac012008-07-02 10:56:40 +0200246 return zfcp_erp_action_enqueue(ZFCP_ERP_ACTION_REOPEN_ADAPTER,
247 adapter, NULL, NULL, id, ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248}
249
250/**
Christof Schmitt287ac012008-07-02 10:56:40 +0200251 * zfcp_erp_adapter_reopen - Reopen adapter.
252 * @adapter: Adapter to reopen.
253 * @clear: Status flags to clear.
254 * @id: Id for debug trace event.
255 * @ref: Reference for debug trace event.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 */
Christof Schmitt287ac012008-07-02 10:56:40 +0200257void zfcp_erp_adapter_reopen(struct zfcp_adapter *adapter, int clear,
258 u8 id, void *ref)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259{
Christof Schmitt287ac012008-07-02 10:56:40 +0200260 unsigned long flags;
261
262 read_lock_irqsave(&zfcp_data.config_lock, flags);
263 write_lock(&adapter->erp_lock);
264 _zfcp_erp_adapter_reopen(adapter, clear, id, ref);
265 write_unlock(&adapter->erp_lock);
266 read_unlock_irqrestore(&zfcp_data.config_lock, flags);
267}
268
269/**
270 * zfcp_erp_adapter_shutdown - Shutdown adapter.
271 * @adapter: Adapter to shut down.
272 * @clear: Status flags to clear.
273 * @id: Id for debug trace event.
274 * @ref: Reference for debug trace event.
275 */
276void zfcp_erp_adapter_shutdown(struct zfcp_adapter *adapter, int clear,
277 u8 id, void *ref)
278{
279 int flags = ZFCP_STATUS_COMMON_RUNNING | ZFCP_STATUS_COMMON_ERP_FAILED;
280 zfcp_erp_adapter_reopen(adapter, clear | flags, id, ref);
281}
282
283/**
284 * zfcp_erp_port_shutdown - Shutdown port
285 * @port: Port to shut down.
286 * @clear: Status flags to clear.
287 * @id: Id for debug trace event.
288 * @ref: Reference for debug trace event.
289 */
290void zfcp_erp_port_shutdown(struct zfcp_port *port, int clear, u8 id, void *ref)
291{
292 int flags = ZFCP_STATUS_COMMON_RUNNING | ZFCP_STATUS_COMMON_ERP_FAILED;
293 zfcp_erp_port_reopen(port, clear | flags, id, ref);
294}
295
296/**
297 * zfcp_erp_unit_shutdown - Shutdown unit
298 * @unit: Unit to shut down.
299 * @clear: Status flags to clear.
300 * @id: Id for debug trace event.
301 * @ref: Reference for debug trace event.
302 */
303void zfcp_erp_unit_shutdown(struct zfcp_unit *unit, int clear, u8 id, void *ref)
304{
305 int flags = ZFCP_STATUS_COMMON_RUNNING | ZFCP_STATUS_COMMON_ERP_FAILED;
306 zfcp_erp_unit_reopen(unit, clear | flags, id, ref);
307}
308
309static void zfcp_erp_port_block(struct zfcp_port *port, int clear)
310{
311 zfcp_erp_modify_port_status(port, 17, NULL,
312 ZFCP_STATUS_COMMON_UNBLOCKED | clear,
313 ZFCP_CLEAR);
314}
315
316static void _zfcp_erp_port_forced_reopen(struct zfcp_port *port,
317 int clear, u8 id, void *ref)
318{
319 zfcp_erp_port_block(port, clear);
320
321 if (atomic_read(&port->status) & ZFCP_STATUS_COMMON_ERP_FAILED)
322 return;
323
324 zfcp_erp_action_enqueue(ZFCP_ERP_ACTION_REOPEN_PORT_FORCED,
325 port->adapter, port, NULL, id, ref);
326}
327
328/**
329 * zfcp_erp_port_forced_reopen - Forced close of port and open again
330 * @port: Port to force close and to reopen.
331 * @id: Id for debug trace event.
332 * @ref: Reference for debug trace event.
333 */
334void zfcp_erp_port_forced_reopen(struct zfcp_port *port, int clear, u8 id,
335 void *ref)
336{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 unsigned long flags;
338 struct zfcp_adapter *adapter = port->adapter;
339
340 read_lock_irqsave(&zfcp_data.config_lock, flags);
341 write_lock(&adapter->erp_lock);
Christof Schmitt287ac012008-07-02 10:56:40 +0200342 _zfcp_erp_port_forced_reopen(port, clear, id, ref);
343 write_unlock(&adapter->erp_lock);
344 read_unlock_irqrestore(&zfcp_data.config_lock, flags);
345}
346
347static int _zfcp_erp_port_reopen(struct zfcp_port *port, int clear, u8 id,
348 void *ref)
349{
350 zfcp_erp_port_block(port, clear);
351
352 if (atomic_read(&port->status) & ZFCP_STATUS_COMMON_ERP_FAILED) {
353 /* ensure propagation of failed status to new devices */
354 zfcp_erp_port_failed(port, 14, NULL);
355 return -EIO;
356 }
357
358 return zfcp_erp_action_enqueue(ZFCP_ERP_ACTION_REOPEN_PORT,
359 port->adapter, port, NULL, id, ref);
360}
361
362/**
363 * zfcp_erp_port_reopen - trigger remote port recovery
364 * @port: port to recover
365 * @clear_mask: flags in port status to be cleared
366 *
367 * Returns 0 if recovery has been triggered, < 0 if not.
368 */
369int zfcp_erp_port_reopen(struct zfcp_port *port, int clear, u8 id, void *ref)
370{
371 unsigned long flags;
372 int retval;
373 struct zfcp_adapter *adapter = port->adapter;
374
375 read_lock_irqsave(&zfcp_data.config_lock, flags);
376 write_lock(&adapter->erp_lock);
377 retval = _zfcp_erp_port_reopen(port, clear, id, ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 write_unlock(&adapter->erp_lock);
379 read_unlock_irqrestore(&zfcp_data.config_lock, flags);
380
381 return retval;
382}
383
Christof Schmitt287ac012008-07-02 10:56:40 +0200384static void zfcp_erp_unit_block(struct zfcp_unit *unit, int clear_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385{
Christof Schmitt287ac012008-07-02 10:56:40 +0200386 zfcp_erp_modify_unit_status(unit, 19, NULL,
387 ZFCP_STATUS_COMMON_UNBLOCKED | clear_mask,
388 ZFCP_CLEAR);
389}
390
391static void _zfcp_erp_unit_reopen(struct zfcp_unit *unit, int clear, u8 id,
392 void *ref)
393{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 struct zfcp_adapter *adapter = unit->port->adapter;
395
Christof Schmitt287ac012008-07-02 10:56:40 +0200396 zfcp_erp_unit_block(unit, clear);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
Christof Schmitt287ac012008-07-02 10:56:40 +0200398 if (atomic_read(&unit->status) & ZFCP_STATUS_COMMON_ERP_FAILED)
399 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
Christof Schmitt287ac012008-07-02 10:56:40 +0200401 zfcp_erp_action_enqueue(ZFCP_ERP_ACTION_REOPEN_UNIT,
402 adapter, unit->port, unit, id, ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403}
404
405/**
406 * zfcp_erp_unit_reopen - initiate reopen of a unit
407 * @unit: unit to be reopened
408 * @clear_mask: specifies flags in unit status to be cleared
409 * Return: 0 on success, < 0 on error
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 */
Christof Schmitt287ac012008-07-02 10:56:40 +0200411void zfcp_erp_unit_reopen(struct zfcp_unit *unit, int clear, u8 id, void *ref)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 unsigned long flags;
Christof Schmitt287ac012008-07-02 10:56:40 +0200414 struct zfcp_port *port = unit->port;
415 struct zfcp_adapter *adapter = port->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
417 read_lock_irqsave(&zfcp_data.config_lock, flags);
418 write_lock(&adapter->erp_lock);
Christof Schmitt287ac012008-07-02 10:56:40 +0200419 _zfcp_erp_unit_reopen(unit, clear, id, ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 write_unlock(&adapter->erp_lock);
421 read_unlock_irqrestore(&zfcp_data.config_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422}
423
Christof Schmitt287ac012008-07-02 10:56:40 +0200424static int status_change_set(unsigned long mask, atomic_t *status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425{
Christof Schmitt287ac012008-07-02 10:56:40 +0200426 return (atomic_read(status) ^ mask) & mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427}
428
Christof Schmitt287ac012008-07-02 10:56:40 +0200429static int status_change_clear(unsigned long mask, atomic_t *status)
Martin Peschke698ec0162008-03-27 14:22:02 +0100430{
Christof Schmitt287ac012008-07-02 10:56:40 +0200431 return atomic_read(status) & mask;
Martin Peschke698ec0162008-03-27 14:22:02 +0100432}
433
Andreas Herrmannf6c0e7a2006-08-02 11:05:52 +0200434static void zfcp_erp_adapter_unblock(struct zfcp_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435{
Christof Schmitt287ac012008-07-02 10:56:40 +0200436 if (status_change_set(ZFCP_STATUS_COMMON_UNBLOCKED, &adapter->status))
Martin Peschke1f6f7122008-04-18 12:51:55 +0200437 zfcp_rec_dbf_event_adapter(16, NULL, adapter);
Christof Schmitt287ac012008-07-02 10:56:40 +0200438 atomic_set_mask(ZFCP_STATUS_COMMON_UNBLOCKED, &adapter->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439}
440
Christof Schmitt287ac012008-07-02 10:56:40 +0200441static void zfcp_erp_port_unblock(struct zfcp_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442{
Christof Schmitt287ac012008-07-02 10:56:40 +0200443 if (status_change_set(ZFCP_STATUS_COMMON_UNBLOCKED, &port->status))
Martin Peschke1f6f7122008-04-18 12:51:55 +0200444 zfcp_rec_dbf_event_port(18, NULL, port);
Christof Schmitt287ac012008-07-02 10:56:40 +0200445 atomic_set_mask(ZFCP_STATUS_COMMON_UNBLOCKED, &port->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446}
447
Christof Schmitt287ac012008-07-02 10:56:40 +0200448static void zfcp_erp_unit_unblock(struct zfcp_unit *unit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449{
Christof Schmitt287ac012008-07-02 10:56:40 +0200450 if (status_change_set(ZFCP_STATUS_COMMON_UNBLOCKED, &unit->status))
Martin Peschke1f6f7122008-04-18 12:51:55 +0200451 zfcp_rec_dbf_event_unit(20, NULL, unit);
Christof Schmitt287ac012008-07-02 10:56:40 +0200452 atomic_set_mask(ZFCP_STATUS_COMMON_UNBLOCKED, &unit->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453}
454
Christof Schmitt287ac012008-07-02 10:56:40 +0200455static void zfcp_erp_action_to_running(struct zfcp_erp_action *erp_action)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456{
Christof Schmitt287ac012008-07-02 10:56:40 +0200457 list_move(&erp_action->list, &erp_action->adapter->erp_running_head);
458 zfcp_rec_dbf_event_action(145, erp_action);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459}
460
Christof Schmitt287ac012008-07-02 10:56:40 +0200461static void zfcp_erp_strategy_check_fsfreq(struct zfcp_erp_action *act)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462{
Christof Schmitt287ac012008-07-02 10:56:40 +0200463 struct zfcp_adapter *adapter = act->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
Christof Schmitt287ac012008-07-02 10:56:40 +0200465 if (!act->fsf_req)
466 return;
467
468 spin_lock(&adapter->req_list_lock);
469 if (zfcp_reqlist_find_safe(adapter, act->fsf_req) &&
470 act->fsf_req->erp_action == act) {
471 if (act->status & (ZFCP_STATUS_ERP_DISMISSED |
472 ZFCP_STATUS_ERP_TIMEDOUT)) {
473 act->fsf_req->status |= ZFCP_STATUS_FSFREQ_DISMISSED;
474 zfcp_rec_dbf_event_action(142, act);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 }
Christof Schmitt287ac012008-07-02 10:56:40 +0200476 if (act->status & ZFCP_STATUS_ERP_TIMEDOUT)
477 zfcp_rec_dbf_event_action(143, act);
478 if (act->fsf_req->status & (ZFCP_STATUS_FSFREQ_COMPLETED |
479 ZFCP_STATUS_FSFREQ_DISMISSED))
480 act->fsf_req = NULL;
481 } else
482 act->fsf_req = NULL;
483 spin_unlock(&adapter->req_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484}
485
Andreas Herrmannf6c0e7a2006-08-02 11:05:52 +0200486/**
Christof Schmitt287ac012008-07-02 10:56:40 +0200487 * zfcp_erp_notify - Trigger ERP action.
488 * @erp_action: ERP action to continue.
489 * @set_mask: ERP action status flags to set.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 */
Christof Schmitt287ac012008-07-02 10:56:40 +0200491void zfcp_erp_notify(struct zfcp_erp_action *erp_action, unsigned long set_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492{
493 struct zfcp_adapter *adapter = erp_action->adapter;
494 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
496 write_lock_irqsave(&adapter->erp_lock, flags);
Christof Schmitt287ac012008-07-02 10:56:40 +0200497 if (zfcp_erp_action_exists(erp_action) == ZFCP_ERP_ACTION_RUNNING) {
498 erp_action->status |= set_mask;
499 zfcp_erp_action_ready(erp_action);
500 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 write_unlock_irqrestore(&adapter->erp_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502}
503
Andreas Herrmannf6c0e7a2006-08-02 11:05:52 +0200504/**
Christof Schmitt287ac012008-07-02 10:56:40 +0200505 * zfcp_erp_timeout_handler - Trigger ERP action from timed out ERP request
506 * @data: ERP action (from timer data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 */
Christof Schmitt287ac012008-07-02 10:56:40 +0200508void zfcp_erp_timeout_handler(unsigned long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509{
Christof Schmitt287ac012008-07-02 10:56:40 +0200510 struct zfcp_erp_action *act = (struct zfcp_erp_action *) data;
511 zfcp_erp_notify(act, ZFCP_STATUS_ERP_TIMEDOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512}
513
Christof Schmitt287ac012008-07-02 10:56:40 +0200514static void zfcp_erp_memwait_handler(unsigned long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515{
Christof Schmitt287ac012008-07-02 10:56:40 +0200516 zfcp_erp_notify((struct zfcp_erp_action *)data, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517}
518
Christof Schmitt287ac012008-07-02 10:56:40 +0200519static void zfcp_erp_strategy_memwait(struct zfcp_erp_action *erp_action)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 init_timer(&erp_action->timer);
522 erp_action->timer.function = zfcp_erp_memwait_handler;
523 erp_action->timer.data = (unsigned long) erp_action;
Christof Schmitt287ac012008-07-02 10:56:40 +0200524 erp_action->timer.expires = jiffies + HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 add_timer(&erp_action->timer);
Christof Schmitt287ac012008-07-02 10:56:40 +0200526}
527
528static void _zfcp_erp_port_reopen_all(struct zfcp_adapter *adapter,
529 int clear, u8 id, void *ref)
530{
531 struct zfcp_port *port;
532
533 list_for_each_entry(port, &adapter->port_list_head, list)
Swen Schillig5ab944f2008-10-01 12:42:17 +0200534 _zfcp_erp_port_reopen(port, clear, id, ref);
Christof Schmitt287ac012008-07-02 10:56:40 +0200535}
536
537static void _zfcp_erp_unit_reopen_all(struct zfcp_port *port, int clear, u8 id,
538 void *ref)
539{
540 struct zfcp_unit *unit;
541
542 list_for_each_entry(unit, &port->unit_list_head, list)
543 _zfcp_erp_unit_reopen(unit, clear, id, ref);
544}
545
546static void zfcp_erp_strategy_followup_actions(struct zfcp_erp_action *act)
547{
548 struct zfcp_adapter *adapter = act->adapter;
549 struct zfcp_port *port = act->port;
550 struct zfcp_unit *unit = act->unit;
551 u32 status = act->status;
552
553 /* initiate follow-up actions depending on success of finished action */
554 switch (act->action) {
555
556 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
557 if (status == ZFCP_ERP_SUCCEEDED)
558 _zfcp_erp_port_reopen_all(adapter, 0, 70, NULL);
559 else
560 _zfcp_erp_adapter_reopen(adapter, 0, 71, NULL);
561 break;
562
563 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
564 if (status == ZFCP_ERP_SUCCEEDED)
565 _zfcp_erp_port_reopen(port, 0, 72, NULL);
566 else
567 _zfcp_erp_adapter_reopen(adapter, 0, 73, NULL);
568 break;
569
570 case ZFCP_ERP_ACTION_REOPEN_PORT:
571 if (status == ZFCP_ERP_SUCCEEDED)
572 _zfcp_erp_unit_reopen_all(port, 0, 74, NULL);
573 else
574 _zfcp_erp_port_forced_reopen(port, 0, 75, NULL);
575 break;
576
577 case ZFCP_ERP_ACTION_REOPEN_UNIT:
578 if (status != ZFCP_ERP_SUCCEEDED)
579 _zfcp_erp_port_reopen(unit->port, 0, 76, NULL);
580 break;
581 }
582}
583
584static void zfcp_erp_wakeup(struct zfcp_adapter *adapter)
585{
586 unsigned long flags;
587
588 read_lock_irqsave(&zfcp_data.config_lock, flags);
589 read_lock(&adapter->erp_lock);
590 if (list_empty(&adapter->erp_ready_head) &&
591 list_empty(&adapter->erp_running_head)) {
592 atomic_clear_mask(ZFCP_STATUS_ADAPTER_ERP_PENDING,
593 &adapter->status);
594 wake_up(&adapter->erp_done_wqh);
595 }
596 read_unlock(&adapter->erp_lock);
597 read_unlock_irqrestore(&zfcp_data.config_lock, flags);
598}
599
600static int zfcp_erp_adapter_strategy_open_qdio(struct zfcp_erp_action *act)
601{
602 if (zfcp_qdio_open(act->adapter))
603 return ZFCP_ERP_FAILED;
604 init_waitqueue_head(&act->adapter->request_wq);
605 atomic_set_mask(ZFCP_STATUS_ADAPTER_QDIOUP, &act->adapter->status);
606 return ZFCP_ERP_SUCCEEDED;
607}
608
609static void zfcp_erp_enqueue_ptp_port(struct zfcp_adapter *adapter)
610{
611 struct zfcp_port *port;
612 port = zfcp_port_enqueue(adapter, adapter->peer_wwpn, 0,
613 adapter->peer_d_id);
614 if (IS_ERR(port)) /* error or port already attached */
615 return;
616 _zfcp_erp_port_reopen(port, 0, 150, NULL);
617}
618
619static int zfcp_erp_adapter_strat_fsf_xconf(struct zfcp_erp_action *erp_action)
620{
621 int retries;
622 int sleep = 1;
623 struct zfcp_adapter *adapter = erp_action->adapter;
624
625 atomic_clear_mask(ZFCP_STATUS_ADAPTER_XCONFIG_OK, &adapter->status);
626
627 for (retries = 7; retries; retries--) {
628 atomic_clear_mask(ZFCP_STATUS_ADAPTER_HOST_CON_INIT,
629 &adapter->status);
630 write_lock_irq(&adapter->erp_lock);
631 zfcp_erp_action_to_running(erp_action);
632 write_unlock_irq(&adapter->erp_lock);
633 if (zfcp_fsf_exchange_config_data(erp_action)) {
634 atomic_clear_mask(ZFCP_STATUS_ADAPTER_HOST_CON_INIT,
635 &adapter->status);
636 return ZFCP_ERP_FAILED;
637 }
638
639 zfcp_rec_dbf_event_thread_lock(6, adapter);
640 down(&adapter->erp_ready_sem);
641 zfcp_rec_dbf_event_thread_lock(7, adapter);
642 if (erp_action->status & ZFCP_STATUS_ERP_TIMEDOUT)
643 break;
644
645 if (!(atomic_read(&adapter->status) &
646 ZFCP_STATUS_ADAPTER_HOST_CON_INIT))
647 break;
648
649 ssleep(sleep);
650 sleep *= 2;
651 }
652
653 atomic_clear_mask(ZFCP_STATUS_ADAPTER_HOST_CON_INIT,
654 &adapter->status);
655
656 if (!(atomic_read(&adapter->status) & ZFCP_STATUS_ADAPTER_XCONFIG_OK))
657 return ZFCP_ERP_FAILED;
658
659 if (fc_host_port_type(adapter->scsi_host) == FC_PORTTYPE_PTP)
660 zfcp_erp_enqueue_ptp_port(adapter);
661
662 return ZFCP_ERP_SUCCEEDED;
663}
664
665static int zfcp_erp_adapter_strategy_open_fsf_xport(struct zfcp_erp_action *act)
666{
667 int ret;
668 struct zfcp_adapter *adapter = act->adapter;
669
Christof Schmitt287ac012008-07-02 10:56:40 +0200670 write_lock_irq(&adapter->erp_lock);
671 zfcp_erp_action_to_running(act);
672 write_unlock_irq(&adapter->erp_lock);
673
674 ret = zfcp_fsf_exchange_port_data(act);
675 if (ret == -EOPNOTSUPP)
676 return ZFCP_ERP_SUCCEEDED;
677 if (ret)
678 return ZFCP_ERP_FAILED;
679
680 zfcp_rec_dbf_event_thread_lock(8, adapter);
681 down(&adapter->erp_ready_sem);
682 zfcp_rec_dbf_event_thread_lock(9, adapter);
683 if (act->status & ZFCP_STATUS_ERP_TIMEDOUT)
684 return ZFCP_ERP_FAILED;
685
686 return ZFCP_ERP_SUCCEEDED;
687}
688
689static int zfcp_erp_adapter_strategy_open_fsf(struct zfcp_erp_action *act)
690{
691 if (zfcp_erp_adapter_strat_fsf_xconf(act) == ZFCP_ERP_FAILED)
692 return ZFCP_ERP_FAILED;
693
694 if (zfcp_erp_adapter_strategy_open_fsf_xport(act) == ZFCP_ERP_FAILED)
695 return ZFCP_ERP_FAILED;
696
697 atomic_set(&act->adapter->stat_miss, 16);
698 if (zfcp_status_read_refill(act->adapter))
699 return ZFCP_ERP_FAILED;
700
701 return ZFCP_ERP_SUCCEEDED;
702}
703
704static int zfcp_erp_adapter_strategy_generic(struct zfcp_erp_action *act,
705 int close)
706{
707 int retval = ZFCP_ERP_SUCCEEDED;
708 struct zfcp_adapter *adapter = act->adapter;
709
710 if (close)
711 goto close_only;
712
713 retval = zfcp_erp_adapter_strategy_open_qdio(act);
714 if (retval != ZFCP_ERP_SUCCEEDED)
715 goto failed_qdio;
716
717 retval = zfcp_erp_adapter_strategy_open_fsf(act);
718 if (retval != ZFCP_ERP_SUCCEEDED)
719 goto failed_openfcp;
720
721 atomic_set_mask(ZFCP_STATUS_COMMON_OPEN, &act->adapter->status);
722 schedule_work(&act->adapter->scan_work);
723
724 return ZFCP_ERP_SUCCEEDED;
725
726 close_only:
727 atomic_clear_mask(ZFCP_STATUS_COMMON_OPEN,
728 &act->adapter->status);
729
730 failed_openfcp:
731 /* close queues to ensure that buffers are not accessed by adapter */
732 zfcp_qdio_close(adapter);
733 zfcp_fsf_req_dismiss_all(adapter);
734 adapter->fsf_req_seq_no = 0;
735 /* all ports and units are closed */
736 zfcp_erp_modify_adapter_status(adapter, 24, NULL,
737 ZFCP_STATUS_COMMON_OPEN, ZFCP_CLEAR);
738 failed_qdio:
739 atomic_clear_mask(ZFCP_STATUS_ADAPTER_XCONFIG_OK |
Swen Schillig44cc76f2008-10-01 12:42:16 +0200740 ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED,
Christof Schmitt287ac012008-07-02 10:56:40 +0200741 &act->adapter->status);
742 return retval;
743}
744
745static int zfcp_erp_adapter_strategy(struct zfcp_erp_action *act)
746{
747 int retval;
748
Christof Schmitt287ac012008-07-02 10:56:40 +0200749 zfcp_erp_adapter_strategy_generic(act, 1); /* close */
Christof Schmitt287ac012008-07-02 10:56:40 +0200750 if (act->status & ZFCP_STATUS_ERP_CLOSE_ONLY)
751 return ZFCP_ERP_EXIT;
752
Christof Schmitt287ac012008-07-02 10:56:40 +0200753 retval = zfcp_erp_adapter_strategy_generic(act, 0); /* open */
Christof Schmitt287ac012008-07-02 10:56:40 +0200754
755 if (retval == ZFCP_ERP_FAILED)
756 ssleep(8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757
758 return retval;
759}
760
Christof Schmitt287ac012008-07-02 10:56:40 +0200761static int zfcp_erp_port_forced_strategy_close(struct zfcp_erp_action *act)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762{
Christof Schmitt287ac012008-07-02 10:56:40 +0200763 int retval;
764
765 retval = zfcp_fsf_close_physical_port(act);
766 if (retval == -ENOMEM)
767 return ZFCP_ERP_NOMEM;
768 act->step = ZFCP_ERP_STEP_PHYS_PORT_CLOSING;
769 if (retval)
770 return ZFCP_ERP_FAILED;
771
772 return ZFCP_ERP_CONTINUES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773}
774
Christof Schmitt287ac012008-07-02 10:56:40 +0200775static void zfcp_erp_port_strategy_clearstati(struct zfcp_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776{
Swen Schillig44cc76f2008-10-01 12:42:16 +0200777 atomic_clear_mask(ZFCP_STATUS_COMMON_ACCESS_DENIED |
Christof Schmitt287ac012008-07-02 10:56:40 +0200778 ZFCP_STATUS_PORT_PHYS_CLOSING |
779 ZFCP_STATUS_PORT_INVALID_WWPN,
780 &port->status);
781}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
Christof Schmitt287ac012008-07-02 10:56:40 +0200783static int zfcp_erp_port_forced_strategy(struct zfcp_erp_action *erp_action)
784{
785 struct zfcp_port *port = erp_action->port;
786 int status = atomic_read(&port->status);
787
788 switch (erp_action->step) {
789 case ZFCP_ERP_STEP_UNINITIALIZED:
790 zfcp_erp_port_strategy_clearstati(port);
791 if ((status & ZFCP_STATUS_PORT_PHYS_OPEN) &&
792 (status & ZFCP_STATUS_COMMON_OPEN))
793 return zfcp_erp_port_forced_strategy_close(erp_action);
794 else
795 return ZFCP_ERP_FAILED;
796
797 case ZFCP_ERP_STEP_PHYS_PORT_CLOSING:
798 if (status & ZFCP_STATUS_PORT_PHYS_OPEN)
799 return ZFCP_ERP_SUCCEEDED;
800 }
801 return ZFCP_ERP_FAILED;
802}
803
804static int zfcp_erp_port_strategy_close(struct zfcp_erp_action *erp_action)
805{
806 int retval;
807
808 retval = zfcp_fsf_close_port(erp_action);
809 if (retval == -ENOMEM)
810 return ZFCP_ERP_NOMEM;
811 erp_action->step = ZFCP_ERP_STEP_PORT_CLOSING;
812 if (retval)
813 return ZFCP_ERP_FAILED;
814 return ZFCP_ERP_CONTINUES;
815}
816
817static int zfcp_erp_port_strategy_open_port(struct zfcp_erp_action *erp_action)
818{
819 int retval;
820
821 retval = zfcp_fsf_open_port(erp_action);
822 if (retval == -ENOMEM)
823 return ZFCP_ERP_NOMEM;
824 erp_action->step = ZFCP_ERP_STEP_PORT_OPENING;
825 if (retval)
826 return ZFCP_ERP_FAILED;
827 return ZFCP_ERP_CONTINUES;
828}
829
Christof Schmitt287ac012008-07-02 10:56:40 +0200830static int zfcp_erp_open_ptp_port(struct zfcp_erp_action *act)
831{
832 struct zfcp_adapter *adapter = act->adapter;
833 struct zfcp_port *port = act->port;
834
835 if (port->wwpn != adapter->peer_wwpn) {
Christof Schmitt287ac012008-07-02 10:56:40 +0200836 zfcp_erp_port_failed(port, 25, NULL);
837 return ZFCP_ERP_FAILED;
838 }
839 port->d_id = adapter->peer_d_id;
840 atomic_set_mask(ZFCP_STATUS_PORT_DID_DID, &port->status);
841 return zfcp_erp_port_strategy_open_port(act);
842}
843
Swen Schillig5ab944f2008-10-01 12:42:17 +0200844void zfcp_erp_port_strategy_open_lookup(struct work_struct *work)
845{
846 int retval;
847 struct zfcp_port *port = container_of(work, struct zfcp_port,
848 gid_pn_work);
849
850 retval = zfcp_fc_ns_gid_pn(&port->erp_action);
851 if (retval == -ENOMEM)
852 zfcp_erp_notify(&port->erp_action, ZFCP_ERP_NOMEM);
853 port->erp_action.step = ZFCP_ERP_STEP_NAMESERVER_LOOKUP;
854 if (retval)
855 zfcp_erp_notify(&port->erp_action, ZFCP_ERP_FAILED);
856
857}
858
Christof Schmitt287ac012008-07-02 10:56:40 +0200859static int zfcp_erp_port_strategy_open_common(struct zfcp_erp_action *act)
860{
861 struct zfcp_adapter *adapter = act->adapter;
862 struct zfcp_port *port = act->port;
Christof Schmitt287ac012008-07-02 10:56:40 +0200863 int p_status = atomic_read(&port->status);
864
865 switch (act->step) {
866 case ZFCP_ERP_STEP_UNINITIALIZED:
867 case ZFCP_ERP_STEP_PHYS_PORT_CLOSING:
868 case ZFCP_ERP_STEP_PORT_CLOSING:
869 if (fc_host_port_type(adapter->scsi_host) == FC_PORTTYPE_PTP)
870 return zfcp_erp_open_ptp_port(act);
Swen Schillig5ab944f2008-10-01 12:42:17 +0200871 if (!(p_status & ZFCP_STATUS_PORT_DID_DID)) {
872 schedule_work(&port->gid_pn_work);
873 return ZFCP_ERP_CONTINUES;
Christof Schmitt287ac012008-07-02 10:56:40 +0200874 }
Christof Schmitt287ac012008-07-02 10:56:40 +0200875 case ZFCP_ERP_STEP_NAMESERVER_LOOKUP:
876 if (!(p_status & ZFCP_STATUS_PORT_DID_DID)) {
877 if (p_status & (ZFCP_STATUS_PORT_INVALID_WWPN)) {
878 zfcp_erp_port_failed(port, 26, NULL);
879 return ZFCP_ERP_EXIT;
880 }
881 return ZFCP_ERP_FAILED;
882 }
883 return zfcp_erp_port_strategy_open_port(act);
884
885 case ZFCP_ERP_STEP_PORT_OPENING:
886 /* D_ID might have changed during open */
Swen Schillig5ab944f2008-10-01 12:42:17 +0200887 if (p_status & ZFCP_STATUS_COMMON_OPEN) {
888 if (p_status & ZFCP_STATUS_PORT_DID_DID)
889 return ZFCP_ERP_SUCCEEDED;
890 else {
891 act->step = ZFCP_ERP_STEP_PORT_CLOSING;
892 return ZFCP_ERP_CONTINUES;
893 }
Christof Schmitt287ac012008-07-02 10:56:40 +0200894 /* fall through otherwise */
Swen Schillig5ab944f2008-10-01 12:42:17 +0200895 }
Christof Schmitt287ac012008-07-02 10:56:40 +0200896 }
897 return ZFCP_ERP_FAILED;
898}
899
Christof Schmitt287ac012008-07-02 10:56:40 +0200900static int zfcp_erp_port_strategy(struct zfcp_erp_action *erp_action)
901{
902 struct zfcp_port *port = erp_action->port;
903
Swen Schillig5ab944f2008-10-01 12:42:17 +0200904 if (atomic_read(&port->status) & ZFCP_STATUS_COMMON_NOESC)
905 goto close_init_done;
906
Christof Schmitt287ac012008-07-02 10:56:40 +0200907 switch (erp_action->step) {
908 case ZFCP_ERP_STEP_UNINITIALIZED:
909 zfcp_erp_port_strategy_clearstati(port);
910 if (atomic_read(&port->status) & ZFCP_STATUS_COMMON_OPEN)
911 return zfcp_erp_port_strategy_close(erp_action);
912 break;
913
914 case ZFCP_ERP_STEP_PORT_CLOSING:
915 if (atomic_read(&port->status) & ZFCP_STATUS_COMMON_OPEN)
916 return ZFCP_ERP_FAILED;
917 break;
918 }
Swen Schillig5ab944f2008-10-01 12:42:17 +0200919
920close_init_done:
Christof Schmitt287ac012008-07-02 10:56:40 +0200921 if (erp_action->status & ZFCP_STATUS_ERP_CLOSE_ONLY)
922 return ZFCP_ERP_EXIT;
Christof Schmitt287ac012008-07-02 10:56:40 +0200923
Swen Schillig5ab944f2008-10-01 12:42:17 +0200924 return zfcp_erp_port_strategy_open_common(erp_action);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925}
926
Christof Schmitt287ac012008-07-02 10:56:40 +0200927static void zfcp_erp_unit_strategy_clearstati(struct zfcp_unit *unit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928{
Swen Schillig44cc76f2008-10-01 12:42:16 +0200929 atomic_clear_mask(ZFCP_STATUS_COMMON_ACCESS_DENIED |
Christof Schmitt287ac012008-07-02 10:56:40 +0200930 ZFCP_STATUS_UNIT_SHARED |
931 ZFCP_STATUS_UNIT_READONLY,
932 &unit->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933}
934
Christof Schmitt287ac012008-07-02 10:56:40 +0200935static int zfcp_erp_unit_strategy_close(struct zfcp_erp_action *erp_action)
936{
937 int retval = zfcp_fsf_close_unit(erp_action);
938 if (retval == -ENOMEM)
939 return ZFCP_ERP_NOMEM;
940 erp_action->step = ZFCP_ERP_STEP_UNIT_CLOSING;
941 if (retval)
942 return ZFCP_ERP_FAILED;
943 return ZFCP_ERP_CONTINUES;
944}
945
946static int zfcp_erp_unit_strategy_open(struct zfcp_erp_action *erp_action)
947{
948 int retval = zfcp_fsf_open_unit(erp_action);
949 if (retval == -ENOMEM)
950 return ZFCP_ERP_NOMEM;
951 erp_action->step = ZFCP_ERP_STEP_UNIT_OPENING;
952 if (retval)
953 return ZFCP_ERP_FAILED;
954 return ZFCP_ERP_CONTINUES;
955}
956
957static int zfcp_erp_unit_strategy(struct zfcp_erp_action *erp_action)
958{
959 struct zfcp_unit *unit = erp_action->unit;
960
961 switch (erp_action->step) {
962 case ZFCP_ERP_STEP_UNINITIALIZED:
963 zfcp_erp_unit_strategy_clearstati(unit);
964 if (atomic_read(&unit->status) & ZFCP_STATUS_COMMON_OPEN)
965 return zfcp_erp_unit_strategy_close(erp_action);
966 /* already closed, fall through */
967 case ZFCP_ERP_STEP_UNIT_CLOSING:
968 if (atomic_read(&unit->status) & ZFCP_STATUS_COMMON_OPEN)
969 return ZFCP_ERP_FAILED;
970 if (erp_action->status & ZFCP_STATUS_ERP_CLOSE_ONLY)
971 return ZFCP_ERP_EXIT;
972 return zfcp_erp_unit_strategy_open(erp_action);
973
974 case ZFCP_ERP_STEP_UNIT_OPENING:
975 if (atomic_read(&unit->status) & ZFCP_STATUS_COMMON_OPEN)
976 return ZFCP_ERP_SUCCEEDED;
977 }
978 return ZFCP_ERP_FAILED;
979}
980
981static int zfcp_erp_strategy_check_unit(struct zfcp_unit *unit, int result)
982{
983 switch (result) {
984 case ZFCP_ERP_SUCCEEDED :
985 atomic_set(&unit->erp_counter, 0);
986 zfcp_erp_unit_unblock(unit);
987 break;
988 case ZFCP_ERP_FAILED :
989 atomic_inc(&unit->erp_counter);
Christof Schmittff3b24f2008-10-01 12:42:15 +0200990 if (atomic_read(&unit->erp_counter) > ZFCP_MAX_ERPS) {
991 dev_err(&unit->port->adapter->ccw_device->dev,
992 "ERP failed for unit 0x%016Lx on "
993 "port 0x%016Lx\n",
994 unit->fcp_lun, unit->port->wwpn);
Christof Schmitt287ac012008-07-02 10:56:40 +0200995 zfcp_erp_unit_failed(unit, 21, NULL);
Christof Schmittff3b24f2008-10-01 12:42:15 +0200996 }
Christof Schmitt287ac012008-07-02 10:56:40 +0200997 break;
998 }
999
1000 if (atomic_read(&unit->status) & ZFCP_STATUS_COMMON_ERP_FAILED) {
1001 zfcp_erp_unit_block(unit, 0);
1002 result = ZFCP_ERP_EXIT;
1003 }
1004 return result;
1005}
1006
1007static int zfcp_erp_strategy_check_port(struct zfcp_port *port, int result)
1008{
1009 switch (result) {
1010 case ZFCP_ERP_SUCCEEDED :
1011 atomic_set(&port->erp_counter, 0);
1012 zfcp_erp_port_unblock(port);
1013 break;
1014
1015 case ZFCP_ERP_FAILED :
1016 if (atomic_read(&port->status) & ZFCP_STATUS_COMMON_NOESC) {
1017 zfcp_erp_port_block(port, 0);
1018 result = ZFCP_ERP_EXIT;
1019 }
1020 atomic_inc(&port->erp_counter);
Christof Schmittff3b24f2008-10-01 12:42:15 +02001021 if (atomic_read(&port->erp_counter) > ZFCP_MAX_ERPS) {
1022 dev_err(&port->adapter->ccw_device->dev,
1023 "ERP failed for remote port 0x%016Lx\n",
1024 port->wwpn);
Christof Schmitt287ac012008-07-02 10:56:40 +02001025 zfcp_erp_port_failed(port, 22, NULL);
Christof Schmittff3b24f2008-10-01 12:42:15 +02001026 }
Christof Schmitt287ac012008-07-02 10:56:40 +02001027 break;
1028 }
1029
1030 if (atomic_read(&port->status) & ZFCP_STATUS_COMMON_ERP_FAILED) {
1031 zfcp_erp_port_block(port, 0);
1032 result = ZFCP_ERP_EXIT;
1033 }
1034 return result;
1035}
1036
1037static int zfcp_erp_strategy_check_adapter(struct zfcp_adapter *adapter,
1038 int result)
1039{
1040 switch (result) {
1041 case ZFCP_ERP_SUCCEEDED :
1042 atomic_set(&adapter->erp_counter, 0);
1043 zfcp_erp_adapter_unblock(adapter);
1044 break;
1045
1046 case ZFCP_ERP_FAILED :
1047 atomic_inc(&adapter->erp_counter);
Christof Schmittff3b24f2008-10-01 12:42:15 +02001048 if (atomic_read(&adapter->erp_counter) > ZFCP_MAX_ERPS) {
1049 dev_err(&adapter->ccw_device->dev,
1050 "ERP cannot recover an error "
1051 "on the FCP device\n");
Christof Schmitt287ac012008-07-02 10:56:40 +02001052 zfcp_erp_adapter_failed(adapter, 23, NULL);
Christof Schmittff3b24f2008-10-01 12:42:15 +02001053 }
Christof Schmitt287ac012008-07-02 10:56:40 +02001054 break;
1055 }
1056
1057 if (atomic_read(&adapter->status) & ZFCP_STATUS_COMMON_ERP_FAILED) {
1058 zfcp_erp_adapter_block(adapter, 0);
1059 result = ZFCP_ERP_EXIT;
1060 }
1061 return result;
1062}
1063
1064static int zfcp_erp_strategy_check_target(struct zfcp_erp_action *erp_action,
1065 int result)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066{
1067 struct zfcp_adapter *adapter = erp_action->adapter;
1068 struct zfcp_port *port = erp_action->port;
1069 struct zfcp_unit *unit = erp_action->unit;
1070
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 switch (erp_action->action) {
1072
1073 case ZFCP_ERP_ACTION_REOPEN_UNIT:
1074 result = zfcp_erp_strategy_check_unit(unit, result);
1075 break;
1076
1077 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
1078 case ZFCP_ERP_ACTION_REOPEN_PORT:
1079 result = zfcp_erp_strategy_check_port(port, result);
1080 break;
1081
1082 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
1083 result = zfcp_erp_strategy_check_adapter(adapter, result);
1084 break;
1085 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 return result;
1087}
1088
Christof Schmitt287ac012008-07-02 10:56:40 +02001089static int zfcp_erp_strat_change_det(atomic_t *target_status, u32 erp_status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090{
Christof Schmitt287ac012008-07-02 10:56:40 +02001091 int status = atomic_read(target_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092
Christof Schmitt287ac012008-07-02 10:56:40 +02001093 if ((status & ZFCP_STATUS_COMMON_RUNNING) &&
1094 (erp_status & ZFCP_STATUS_ERP_CLOSE_ONLY))
1095 return 1; /* take it online */
1096
1097 if (!(status & ZFCP_STATUS_COMMON_RUNNING) &&
1098 !(erp_status & ZFCP_STATUS_ERP_CLOSE_ONLY))
1099 return 1; /* take it offline */
1100
1101 return 0;
1102}
1103
1104static int zfcp_erp_strategy_statechange(struct zfcp_erp_action *act, int ret)
1105{
1106 int action = act->action;
1107 struct zfcp_adapter *adapter = act->adapter;
1108 struct zfcp_port *port = act->port;
1109 struct zfcp_unit *unit = act->unit;
1110 u32 erp_status = act->status;
1111
1112 switch (action) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
Christof Schmitt287ac012008-07-02 10:56:40 +02001114 if (zfcp_erp_strat_change_det(&adapter->status, erp_status)) {
1115 _zfcp_erp_adapter_reopen(adapter,
1116 ZFCP_STATUS_COMMON_ERP_FAILED,
1117 67, NULL);
1118 return ZFCP_ERP_EXIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 }
1120 break;
1121
1122 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
1123 case ZFCP_ERP_ACTION_REOPEN_PORT:
Christof Schmitt287ac012008-07-02 10:56:40 +02001124 if (zfcp_erp_strat_change_det(&port->status, erp_status)) {
1125 _zfcp_erp_port_reopen(port,
1126 ZFCP_STATUS_COMMON_ERP_FAILED,
1127 68, NULL);
1128 return ZFCP_ERP_EXIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 }
1130 break;
1131
1132 case ZFCP_ERP_ACTION_REOPEN_UNIT:
Christof Schmitt287ac012008-07-02 10:56:40 +02001133 if (zfcp_erp_strat_change_det(&unit->status, erp_status)) {
1134 _zfcp_erp_unit_reopen(unit,
1135 ZFCP_STATUS_COMMON_ERP_FAILED,
1136 69, NULL);
1137 return ZFCP_ERP_EXIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 }
1139 break;
1140 }
Christof Schmitt287ac012008-07-02 10:56:40 +02001141 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142}
1143
Christof Schmitt287ac012008-07-02 10:56:40 +02001144static void zfcp_erp_action_dequeue(struct zfcp_erp_action *erp_action)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145{
Christof Schmitt287ac012008-07-02 10:56:40 +02001146 struct zfcp_adapter *adapter = erp_action->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147
Christof Schmitt287ac012008-07-02 10:56:40 +02001148 adapter->erp_total_count--;
1149 if (erp_action->status & ZFCP_STATUS_ERP_LOWMEM) {
1150 adapter->erp_low_mem_count--;
1151 erp_action->status &= ~ZFCP_STATUS_ERP_LOWMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 }
1153
Christof Schmitt287ac012008-07-02 10:56:40 +02001154 list_del(&erp_action->list);
1155 zfcp_rec_dbf_event_action(144, erp_action);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156
Christof Schmitt287ac012008-07-02 10:56:40 +02001157 switch (erp_action->action) {
1158 case ZFCP_ERP_ACTION_REOPEN_UNIT:
1159 atomic_clear_mask(ZFCP_STATUS_COMMON_ERP_INUSE,
1160 &erp_action->unit->status);
1161 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162
Christof Schmitt287ac012008-07-02 10:56:40 +02001163 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
1164 case ZFCP_ERP_ACTION_REOPEN_PORT:
1165 atomic_clear_mask(ZFCP_STATUS_COMMON_ERP_INUSE,
1166 &erp_action->port->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 break;
Christof Schmitt287ac012008-07-02 10:56:40 +02001168
1169 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
1170 atomic_clear_mask(ZFCP_STATUS_COMMON_ERP_INUSE,
1171 &erp_action->adapter->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 break;
1173 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174}
1175
Christof Schmitt5f852be2007-05-08 11:16:52 +02001176struct zfcp_erp_add_work {
1177 struct zfcp_unit *unit;
1178 struct work_struct work;
1179};
1180
Christof Schmitt5f852be2007-05-08 11:16:52 +02001181static void zfcp_erp_scsi_scan(struct work_struct *work)
1182{
1183 struct zfcp_erp_add_work *p =
1184 container_of(work, struct zfcp_erp_add_work, work);
1185 struct zfcp_unit *unit = p->unit;
1186 struct fc_rport *rport = unit->port->rport;
1187 scsi_scan_target(&rport->dev, 0, rport->scsi_target_id,
1188 unit->scsi_lun, 0);
1189 atomic_clear_mask(ZFCP_STATUS_UNIT_SCSI_WORK_PENDING, &unit->status);
Christof Schmitt5f852be2007-05-08 11:16:52 +02001190 zfcp_unit_put(unit);
1191 kfree(p);
1192}
1193
Christof Schmitt287ac012008-07-02 10:56:40 +02001194static void zfcp_erp_schedule_work(struct zfcp_unit *unit)
Christof Schmitt5f852be2007-05-08 11:16:52 +02001195{
1196 struct zfcp_erp_add_work *p;
1197
Swen Schillig0d661322007-07-18 10:55:08 +02001198 p = kzalloc(sizeof(*p), GFP_KERNEL);
Christof Schmitt5f852be2007-05-08 11:16:52 +02001199 if (!p) {
Christof Schmitt553448f2008-06-10 18:20:58 +02001200 dev_err(&unit->port->adapter->ccw_device->dev,
Christof Schmittff3b24f2008-10-01 12:42:15 +02001201 "Registering unit 0x%016Lx on port 0x%016Lx failed\n",
Christof Schmitt553448f2008-06-10 18:20:58 +02001202 unit->fcp_lun, unit->port->wwpn);
Christof Schmitt5f852be2007-05-08 11:16:52 +02001203 return;
1204 }
1205
1206 zfcp_unit_get(unit);
Christof Schmitt5f852be2007-05-08 11:16:52 +02001207 atomic_set_mask(ZFCP_STATUS_UNIT_SCSI_WORK_PENDING, &unit->status);
1208 INIT_WORK(&p->work, zfcp_erp_scsi_scan);
1209 p->unit = unit;
1210 schedule_work(&p->work);
1211}
1212
Christof Schmitt287ac012008-07-02 10:56:40 +02001213static void zfcp_erp_rport_register(struct zfcp_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214{
Christof Schmitt287ac012008-07-02 10:56:40 +02001215 struct fc_rport_identifiers ids;
1216 ids.node_name = port->wwnn;
1217 ids.port_name = port->wwpn;
1218 ids.port_id = port->d_id;
1219 ids.roles = FC_RPORT_ROLE_FCP_TARGET;
1220 port->rport = fc_remote_port_add(port->adapter->scsi_host, 0, &ids);
1221 if (!port->rport) {
1222 dev_err(&port->adapter->ccw_device->dev,
Christof Schmittff3b24f2008-10-01 12:42:15 +02001223 "Registering port 0x%016Lx failed\n",
1224 port->wwpn);
Swen Schilligcc8c2822008-06-10 18:21:00 +02001225 return;
Christof Schmitt287ac012008-07-02 10:56:40 +02001226 }
1227
1228 scsi_target_unblock(&port->rport->dev);
1229 port->rport->maxframe_size = port->maxframe_size;
1230 port->rport->supported_classes = port->supported_classes;
Swen Schilligcc8c2822008-06-10 18:21:00 +02001231}
1232
Christof Schmitt287ac012008-07-02 10:56:40 +02001233static void zfcp_erp_rports_del(struct zfcp_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234{
Christof Schmitt287ac012008-07-02 10:56:40 +02001235 struct zfcp_port *port;
Swen Schillig5ab944f2008-10-01 12:42:17 +02001236 list_for_each_entry(port, &adapter->port_list_head, list) {
1237 fc_remote_port_delete(port->rport);
1238 port->rport = NULL;
1239 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240}
1241
Christof Schmitt287ac012008-07-02 10:56:40 +02001242static void zfcp_erp_action_cleanup(struct zfcp_erp_action *act, int result)
Maxim Shchetyninaef4a982005-09-13 21:51:16 +02001243{
Christof Schmitt287ac012008-07-02 10:56:40 +02001244 struct zfcp_adapter *adapter = act->adapter;
1245 struct zfcp_port *port = act->port;
1246 struct zfcp_unit *unit = act->unit;
Maxim Shchetyninaef4a982005-09-13 21:51:16 +02001247
Christof Schmitt287ac012008-07-02 10:56:40 +02001248 switch (act->action) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 case ZFCP_ERP_ACTION_REOPEN_UNIT:
Christof Schmitt287ac012008-07-02 10:56:40 +02001250 if ((result == ZFCP_ERP_SUCCEEDED) &&
1251 !unit->device && port->rport) {
Andreas Herrmannad58f7d2006-03-10 00:56:16 +01001252 atomic_set_mask(ZFCP_STATUS_UNIT_REGISTERED,
1253 &unit->status);
Christof Schmitt287ac012008-07-02 10:56:40 +02001254 if (!(atomic_read(&unit->status) &
1255 ZFCP_STATUS_UNIT_SCSI_WORK_PENDING))
Christof Schmitt5f852be2007-05-08 11:16:52 +02001256 zfcp_erp_schedule_work(unit);
Andreas Herrmannad58f7d2006-03-10 00:56:16 +01001257 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 zfcp_unit_put(unit);
1259 break;
Christof Schmitt287ac012008-07-02 10:56:40 +02001260
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
1262 case ZFCP_ERP_ACTION_REOPEN_PORT:
Christof Schmitt287ac012008-07-02 10:56:40 +02001263 if (atomic_read(&port->status) & ZFCP_STATUS_PORT_NO_WWPN) {
Andreas Herrmann338151e2006-05-22 18:25:56 +02001264 zfcp_port_put(port);
Christof Schmitt287ac012008-07-02 10:56:40 +02001265 return;
Andreas Herrmann338151e2006-05-22 18:25:56 +02001266 }
Christof Schmitt287ac012008-07-02 10:56:40 +02001267 if ((result == ZFCP_ERP_SUCCEEDED) && !port->rport)
1268 zfcp_erp_rport_register(port);
Andreas Herrmann338151e2006-05-22 18:25:56 +02001269 if ((result != ZFCP_ERP_SUCCEEDED) && port->rport) {
1270 fc_remote_port_delete(port->rport);
1271 port->rport = NULL;
1272 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 zfcp_port_put(port);
1274 break;
Christof Schmitt287ac012008-07-02 10:56:40 +02001275
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
Christof Schmitt287ac012008-07-02 10:56:40 +02001277 if (result != ZFCP_ERP_SUCCEEDED)
1278 zfcp_erp_rports_del(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 zfcp_adapter_put(adapter);
1280 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 }
1282}
1283
Christof Schmitt287ac012008-07-02 10:56:40 +02001284static int zfcp_erp_strategy_do_action(struct zfcp_erp_action *erp_action)
1285{
1286 switch (erp_action->action) {
1287 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
1288 return zfcp_erp_adapter_strategy(erp_action);
1289 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
1290 return zfcp_erp_port_forced_strategy(erp_action);
1291 case ZFCP_ERP_ACTION_REOPEN_PORT:
1292 return zfcp_erp_port_strategy(erp_action);
1293 case ZFCP_ERP_ACTION_REOPEN_UNIT:
1294 return zfcp_erp_unit_strategy(erp_action);
1295 }
1296 return ZFCP_ERP_FAILED;
1297}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298
Christof Schmitt287ac012008-07-02 10:56:40 +02001299static int zfcp_erp_strategy(struct zfcp_erp_action *erp_action)
1300{
1301 int retval;
1302 struct zfcp_adapter *adapter = erp_action->adapter;
1303 unsigned long flags;
1304
1305 read_lock_irqsave(&zfcp_data.config_lock, flags);
1306 write_lock(&adapter->erp_lock);
1307
1308 zfcp_erp_strategy_check_fsfreq(erp_action);
1309
1310 if (erp_action->status & ZFCP_STATUS_ERP_DISMISSED) {
1311 zfcp_erp_action_dequeue(erp_action);
1312 retval = ZFCP_ERP_DISMISSED;
1313 goto unlock;
1314 }
1315
1316 zfcp_erp_action_to_running(erp_action);
1317
1318 /* no lock to allow for blocking operations */
1319 write_unlock(&adapter->erp_lock);
1320 read_unlock_irqrestore(&zfcp_data.config_lock, flags);
1321 retval = zfcp_erp_strategy_do_action(erp_action);
1322 read_lock_irqsave(&zfcp_data.config_lock, flags);
1323 write_lock(&adapter->erp_lock);
1324
1325 if (erp_action->status & ZFCP_STATUS_ERP_DISMISSED)
1326 retval = ZFCP_ERP_CONTINUES;
1327
1328 switch (retval) {
1329 case ZFCP_ERP_NOMEM:
1330 if (!(erp_action->status & ZFCP_STATUS_ERP_LOWMEM)) {
1331 ++adapter->erp_low_mem_count;
1332 erp_action->status |= ZFCP_STATUS_ERP_LOWMEM;
1333 }
1334 if (adapter->erp_total_count == adapter->erp_low_mem_count)
1335 _zfcp_erp_adapter_reopen(adapter, 0, 66, NULL);
1336 else {
1337 zfcp_erp_strategy_memwait(erp_action);
1338 retval = ZFCP_ERP_CONTINUES;
1339 }
1340 goto unlock;
1341
1342 case ZFCP_ERP_CONTINUES:
1343 if (erp_action->status & ZFCP_STATUS_ERP_LOWMEM) {
1344 --adapter->erp_low_mem_count;
1345 erp_action->status &= ~ZFCP_STATUS_ERP_LOWMEM;
1346 }
1347 goto unlock;
1348 }
1349
1350 retval = zfcp_erp_strategy_check_target(erp_action, retval);
1351 zfcp_erp_action_dequeue(erp_action);
1352 retval = zfcp_erp_strategy_statechange(erp_action, retval);
1353 if (retval == ZFCP_ERP_EXIT)
1354 goto unlock;
1355 zfcp_erp_strategy_followup_actions(erp_action);
1356
1357 unlock:
1358 write_unlock(&adapter->erp_lock);
1359 read_unlock_irqrestore(&zfcp_data.config_lock, flags);
1360
1361 if (retval != ZFCP_ERP_CONTINUES)
1362 zfcp_erp_action_cleanup(erp_action, retval);
1363
1364 return retval;
1365}
1366
1367static int zfcp_erp_thread(void *data)
1368{
1369 struct zfcp_adapter *adapter = (struct zfcp_adapter *) data;
1370 struct list_head *next;
1371 struct zfcp_erp_action *act;
1372 unsigned long flags;
1373
1374 daemonize("zfcperp%s", adapter->ccw_device->dev.bus_id);
1375 /* Block all signals */
1376 siginitsetinv(&current->blocked, 0);
1377 atomic_set_mask(ZFCP_STATUS_ADAPTER_ERP_THREAD_UP, &adapter->status);
1378 wake_up(&adapter->erp_thread_wqh);
1379
1380 while (!(atomic_read(&adapter->status) &
1381 ZFCP_STATUS_ADAPTER_ERP_THREAD_KILL)) {
1382 write_lock_irqsave(&adapter->erp_lock, flags);
1383 next = adapter->erp_ready_head.next;
1384 write_unlock_irqrestore(&adapter->erp_lock, flags);
1385
1386 if (next != &adapter->erp_ready_head) {
1387 act = list_entry(next, struct zfcp_erp_action, list);
1388
1389 /* there is more to come after dismission, no notify */
1390 if (zfcp_erp_strategy(act) != ZFCP_ERP_DISMISSED)
1391 zfcp_erp_wakeup(adapter);
1392 }
1393
1394 zfcp_rec_dbf_event_thread(4, adapter);
1395 down_interruptible(&adapter->erp_ready_sem);
1396 zfcp_rec_dbf_event_thread(5, adapter);
1397 }
1398
1399 atomic_clear_mask(ZFCP_STATUS_ADAPTER_ERP_THREAD_UP, &adapter->status);
1400 wake_up(&adapter->erp_thread_wqh);
1401
1402 return 0;
1403}
1404
1405/**
1406 * zfcp_erp_thread_setup - Start ERP thread for adapter
1407 * @adapter: Adapter to start the ERP thread for
1408 *
1409 * Returns 0 on success or error code from kernel_thread()
1410 */
1411int zfcp_erp_thread_setup(struct zfcp_adapter *adapter)
1412{
1413 int retval;
1414
1415 atomic_clear_mask(ZFCP_STATUS_ADAPTER_ERP_THREAD_UP, &adapter->status);
1416 retval = kernel_thread(zfcp_erp_thread, adapter, SIGCHLD);
1417 if (retval < 0) {
1418 dev_err(&adapter->ccw_device->dev,
Christof Schmittff3b24f2008-10-01 12:42:15 +02001419 "Creating an ERP thread for the FCP device failed.\n");
Christof Schmitt287ac012008-07-02 10:56:40 +02001420 return retval;
1421 }
1422 wait_event(adapter->erp_thread_wqh,
1423 atomic_read(&adapter->status) &
1424 ZFCP_STATUS_ADAPTER_ERP_THREAD_UP);
1425 return 0;
1426}
1427
1428/**
1429 * zfcp_erp_thread_kill - Stop ERP thread.
1430 * @adapter: Adapter where the ERP thread should be stopped.
1431 *
1432 * The caller of this routine ensures that the specified adapter has
1433 * been shut down and that this operation has been completed. Thus,
1434 * there are no pending erp_actions which would need to be handled
1435 * here.
1436 */
1437void zfcp_erp_thread_kill(struct zfcp_adapter *adapter)
1438{
1439 atomic_set_mask(ZFCP_STATUS_ADAPTER_ERP_THREAD_KILL, &adapter->status);
1440 up(&adapter->erp_ready_sem);
1441 zfcp_rec_dbf_event_thread_lock(2, adapter);
1442
1443 wait_event(adapter->erp_thread_wqh,
1444 !(atomic_read(&adapter->status) &
1445 ZFCP_STATUS_ADAPTER_ERP_THREAD_UP));
1446
1447 atomic_clear_mask(ZFCP_STATUS_ADAPTER_ERP_THREAD_KILL,
1448 &adapter->status);
1449}
1450
1451/**
1452 * zfcp_erp_adapter_failed - Set adapter status to failed.
1453 * @adapter: Failed adapter.
1454 * @id: Event id for debug trace.
1455 * @ref: Reference for debug trace.
1456 */
1457void zfcp_erp_adapter_failed(struct zfcp_adapter *adapter, u8 id, void *ref)
1458{
1459 zfcp_erp_modify_adapter_status(adapter, id, ref,
1460 ZFCP_STATUS_COMMON_ERP_FAILED, ZFCP_SET);
Christof Schmitt287ac012008-07-02 10:56:40 +02001461}
1462
1463/**
1464 * zfcp_erp_port_failed - Set port status to failed.
1465 * @port: Failed port.
1466 * @id: Event id for debug trace.
1467 * @ref: Reference for debug trace.
1468 */
1469void zfcp_erp_port_failed(struct zfcp_port *port, u8 id, void *ref)
1470{
1471 zfcp_erp_modify_port_status(port, id, ref,
1472 ZFCP_STATUS_COMMON_ERP_FAILED, ZFCP_SET);
Christof Schmitt287ac012008-07-02 10:56:40 +02001473}
1474
1475/**
1476 * zfcp_erp_unit_failed - Set unit status to failed.
1477 * @unit: Failed unit.
1478 * @id: Event id for debug trace.
1479 * @ref: Reference for debug trace.
1480 */
1481void zfcp_erp_unit_failed(struct zfcp_unit *unit, u8 id, void *ref)
1482{
1483 zfcp_erp_modify_unit_status(unit, id, ref,
1484 ZFCP_STATUS_COMMON_ERP_FAILED, ZFCP_SET);
Christof Schmitt287ac012008-07-02 10:56:40 +02001485}
1486
1487/**
1488 * zfcp_erp_wait - wait for completion of error recovery on an adapter
1489 * @adapter: adapter for which to wait for completion of its error recovery
1490 */
1491void zfcp_erp_wait(struct zfcp_adapter *adapter)
1492{
1493 wait_event(adapter->erp_done_wqh,
1494 !(atomic_read(&adapter->status) &
1495 ZFCP_STATUS_ADAPTER_ERP_PENDING));
1496}
1497
1498/**
1499 * zfcp_erp_modify_adapter_status - change adapter status bits
1500 * @adapter: adapter to change the status
1501 * @id: id for the debug trace
1502 * @ref: reference for the debug trace
1503 * @mask: status bits to change
1504 * @set_or_clear: ZFCP_SET or ZFCP_CLEAR
1505 *
1506 * Changes in common status bits are propagated to attached ports and units.
1507 */
1508void zfcp_erp_modify_adapter_status(struct zfcp_adapter *adapter, u8 id,
1509 void *ref, u32 mask, int set_or_clear)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 struct zfcp_port *port;
Christof Schmitt287ac012008-07-02 10:56:40 +02001512 u32 common_mask = mask & ZFCP_COMMON_FLAGS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513
Christof Schmitt287ac012008-07-02 10:56:40 +02001514 if (set_or_clear == ZFCP_SET) {
1515 if (status_change_set(mask, &adapter->status))
1516 zfcp_rec_dbf_event_adapter(id, ref, adapter);
1517 atomic_set_mask(mask, &adapter->status);
1518 } else {
1519 if (status_change_clear(mask, &adapter->status))
1520 zfcp_rec_dbf_event_adapter(id, ref, adapter);
1521 atomic_clear_mask(mask, &adapter->status);
1522 if (mask & ZFCP_STATUS_COMMON_ERP_FAILED)
1523 atomic_set(&adapter->erp_counter, 0);
1524 }
1525
1526 if (common_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 list_for_each_entry(port, &adapter->port_list_head, list)
Christof Schmitt287ac012008-07-02 10:56:40 +02001528 zfcp_erp_modify_port_status(port, id, ref, common_mask,
1529 set_or_clear);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530}
1531
Christof Schmitt287ac012008-07-02 10:56:40 +02001532/**
1533 * zfcp_erp_modify_port_status - change port status bits
1534 * @port: port to change the status bits
1535 * @id: id for the debug trace
1536 * @ref: reference for the debug trace
1537 * @mask: status bits to change
1538 * @set_or_clear: ZFCP_SET or ZFCP_CLEAR
1539 *
1540 * Changes in common status bits are propagated to attached units.
1541 */
1542void zfcp_erp_modify_port_status(struct zfcp_port *port, u8 id, void *ref,
1543 u32 mask, int set_or_clear)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 struct zfcp_unit *unit;
Christof Schmitt287ac012008-07-02 10:56:40 +02001546 u32 common_mask = mask & ZFCP_COMMON_FLAGS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547
Christof Schmitt287ac012008-07-02 10:56:40 +02001548 if (set_or_clear == ZFCP_SET) {
1549 if (status_change_set(mask, &port->status))
1550 zfcp_rec_dbf_event_port(id, ref, port);
1551 atomic_set_mask(mask, &port->status);
1552 } else {
1553 if (status_change_clear(mask, &port->status))
1554 zfcp_rec_dbf_event_port(id, ref, port);
1555 atomic_clear_mask(mask, &port->status);
1556 if (mask & ZFCP_STATUS_COMMON_ERP_FAILED)
1557 atomic_set(&port->erp_counter, 0);
1558 }
1559
1560 if (common_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 list_for_each_entry(unit, &port->unit_list_head, list)
Christof Schmitt287ac012008-07-02 10:56:40 +02001562 zfcp_erp_modify_unit_status(unit, id, ref, common_mask,
1563 set_or_clear);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564}
1565
Christof Schmitt287ac012008-07-02 10:56:40 +02001566/**
1567 * zfcp_erp_modify_unit_status - change unit status bits
1568 * @unit: unit to change the status bits
1569 * @id: id for the debug trace
1570 * @ref: reference for the debug trace
1571 * @mask: status bits to change
1572 * @set_or_clear: ZFCP_SET or ZFCP_CLEAR
1573 */
1574void zfcp_erp_modify_unit_status(struct zfcp_unit *unit, u8 id, void *ref,
1575 u32 mask, int set_or_clear)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576{
Christof Schmitt287ac012008-07-02 10:56:40 +02001577 if (set_or_clear == ZFCP_SET) {
1578 if (status_change_set(mask, &unit->status))
1579 zfcp_rec_dbf_event_unit(id, ref, unit);
1580 atomic_set_mask(mask, &unit->status);
1581 } else {
1582 if (status_change_clear(mask, &unit->status))
1583 zfcp_rec_dbf_event_unit(id, ref, unit);
1584 atomic_clear_mask(mask, &unit->status);
1585 if (mask & ZFCP_STATUS_COMMON_ERP_FAILED) {
1586 atomic_set(&unit->erp_counter, 0);
1587 }
1588 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589}
1590
Christof Schmitt287ac012008-07-02 10:56:40 +02001591/**
1592 * zfcp_erp_port_boxed - Mark port as "boxed" and start ERP
1593 * @port: The "boxed" port.
1594 * @id: The debug trace id.
1595 * @id: Reference for the debug trace.
1596 */
Martin Peschke1f6f7122008-04-18 12:51:55 +02001597void zfcp_erp_port_boxed(struct zfcp_port *port, u8 id, void *ref)
Andreas Herrmannd736a27b2005-06-13 13:23:57 +02001598{
Andreas Herrmannd736a27b2005-06-13 13:23:57 +02001599 unsigned long flags;
1600
Andreas Herrmannd736a27b2005-06-13 13:23:57 +02001601 read_lock_irqsave(&zfcp_data.config_lock, flags);
Martin Peschke698ec0162008-03-27 14:22:02 +01001602 zfcp_erp_modify_port_status(port, id, ref,
1603 ZFCP_STATUS_COMMON_ACCESS_BOXED, ZFCP_SET);
Andreas Herrmannd736a27b2005-06-13 13:23:57 +02001604 read_unlock_irqrestore(&zfcp_data.config_lock, flags);
Martin Peschke9467a9b2008-03-27 14:22:03 +01001605 zfcp_erp_port_reopen(port, ZFCP_STATUS_COMMON_ERP_FAILED, id, ref);
Andreas Herrmannd736a27b2005-06-13 13:23:57 +02001606}
1607
Christof Schmitt287ac012008-07-02 10:56:40 +02001608/**
1609 * zfcp_erp_unit_boxed - Mark unit as "boxed" and start ERP
1610 * @port: The "boxed" unit.
1611 * @id: The debug trace id.
1612 * @id: Reference for the debug trace.
1613 */
Martin Peschke1f6f7122008-04-18 12:51:55 +02001614void zfcp_erp_unit_boxed(struct zfcp_unit *unit, u8 id, void *ref)
Andreas Herrmannd736a27b2005-06-13 13:23:57 +02001615{
Martin Peschke698ec0162008-03-27 14:22:02 +01001616 zfcp_erp_modify_unit_status(unit, id, ref,
1617 ZFCP_STATUS_COMMON_ACCESS_BOXED, ZFCP_SET);
Martin Peschke9467a9b2008-03-27 14:22:03 +01001618 zfcp_erp_unit_reopen(unit, ZFCP_STATUS_COMMON_ERP_FAILED, id, ref);
Andreas Herrmannd736a27b2005-06-13 13:23:57 +02001619}
1620
Christof Schmitt287ac012008-07-02 10:56:40 +02001621/**
1622 * zfcp_erp_port_access_denied - Adapter denied access to port.
1623 * @port: port where access has been denied
1624 * @id: id for debug trace
1625 * @ref: reference for debug trace
1626 *
1627 * Since the adapter has denied access, stop using the port and the
1628 * attached units.
1629 */
Martin Peschke1f6f7122008-04-18 12:51:55 +02001630void zfcp_erp_port_access_denied(struct zfcp_port *port, u8 id, void *ref)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 unsigned long flags;
1633
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 read_lock_irqsave(&zfcp_data.config_lock, flags);
Martin Peschke698ec0162008-03-27 14:22:02 +01001635 zfcp_erp_modify_port_status(port, id, ref,
1636 ZFCP_STATUS_COMMON_ERP_FAILED |
1637 ZFCP_STATUS_COMMON_ACCESS_DENIED, ZFCP_SET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 read_unlock_irqrestore(&zfcp_data.config_lock, flags);
1639}
1640
Christof Schmitt287ac012008-07-02 10:56:40 +02001641/**
1642 * zfcp_erp_unit_access_denied - Adapter denied access to unit.
1643 * @unit: unit where access has been denied
1644 * @id: id for debug trace
1645 * @ref: reference for debug trace
1646 *
1647 * Since the adapter has denied access, stop using the unit.
1648 */
Martin Peschke1f6f7122008-04-18 12:51:55 +02001649void zfcp_erp_unit_access_denied(struct zfcp_unit *unit, u8 id, void *ref)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650{
Martin Peschke698ec0162008-03-27 14:22:02 +01001651 zfcp_erp_modify_unit_status(unit, id, ref,
1652 ZFCP_STATUS_COMMON_ERP_FAILED |
1653 ZFCP_STATUS_COMMON_ACCESS_DENIED, ZFCP_SET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654}
1655
Christof Schmitt287ac012008-07-02 10:56:40 +02001656static void zfcp_erp_unit_access_changed(struct zfcp_unit *unit, u8 id,
1657 void *ref)
1658{
1659 int status = atomic_read(&unit->status);
1660 if (!(status & (ZFCP_STATUS_COMMON_ACCESS_DENIED |
1661 ZFCP_STATUS_COMMON_ACCESS_BOXED)))
1662 return;
1663
1664 zfcp_erp_unit_reopen(unit, ZFCP_STATUS_COMMON_ERP_FAILED, id, ref);
1665}
1666
1667static void zfcp_erp_port_access_changed(struct zfcp_port *port, u8 id,
1668 void *ref)
1669{
1670 struct zfcp_unit *unit;
1671 int status = atomic_read(&port->status);
1672
1673 if (!(status & (ZFCP_STATUS_COMMON_ACCESS_DENIED |
1674 ZFCP_STATUS_COMMON_ACCESS_BOXED))) {
Swen Schillig5ab944f2008-10-01 12:42:17 +02001675 list_for_each_entry(unit, &port->unit_list_head, list)
1676 zfcp_erp_unit_access_changed(unit, id, ref);
Christof Schmitt287ac012008-07-02 10:56:40 +02001677 return;
1678 }
1679
1680 zfcp_erp_port_reopen(port, ZFCP_STATUS_COMMON_ERP_FAILED, id, ref);
1681}
1682
1683/**
1684 * zfcp_erp_adapter_access_changed - Process change in adapter ACT
1685 * @adapter: Adapter where the Access Control Table (ACT) changed
1686 * @id: Id for debug trace
1687 * @ref: Reference for debug trace
1688 */
Martin Peschke9467a9b2008-03-27 14:22:03 +01001689void zfcp_erp_adapter_access_changed(struct zfcp_adapter *adapter, u8 id,
Martin Peschke1f6f7122008-04-18 12:51:55 +02001690 void *ref)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691{
1692 struct zfcp_port *port;
1693 unsigned long flags;
1694
Maxim Shchetyninaef4a982005-09-13 21:51:16 +02001695 if (adapter->connection_features & FSF_FEATURE_NPIV_MODE)
1696 return;
1697
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 read_lock_irqsave(&zfcp_data.config_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 list_for_each_entry(port, &adapter->port_list_head, list)
Swen Schillig5ab944f2008-10-01 12:42:17 +02001700 zfcp_erp_port_access_changed(port, id, ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 read_unlock_irqrestore(&zfcp_data.config_lock, flags);
1702}