blob: 28c7185f24bc95761113dc161dcbf02004a9dd48 [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
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_NAMESERVER_LOOKUP = 0x0400,
30 ZFCP_ERP_STEP_PORT_OPENING = 0x0800,
31 ZFCP_ERP_STEP_UNIT_CLOSING = 0x1000,
32 ZFCP_ERP_STEP_UNIT_OPENING = 0x2000,
33};
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Christof Schmitt287ac012008-07-02 10:56:40 +020035enum zfcp_erp_act_type {
36 ZFCP_ERP_ACTION_REOPEN_UNIT = 1,
37 ZFCP_ERP_ACTION_REOPEN_PORT = 2,
38 ZFCP_ERP_ACTION_REOPEN_PORT_FORCED = 3,
39 ZFCP_ERP_ACTION_REOPEN_ADAPTER = 4,
40};
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Christof Schmitt287ac012008-07-02 10:56:40 +020042enum zfcp_erp_act_state {
43 ZFCP_ERP_ACTION_RUNNING = 1,
44 ZFCP_ERP_ACTION_READY = 2,
45};
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Christof Schmitt287ac012008-07-02 10:56:40 +020047enum zfcp_erp_act_result {
48 ZFCP_ERP_SUCCEEDED = 0,
49 ZFCP_ERP_FAILED = 1,
50 ZFCP_ERP_CONTINUES = 2,
51 ZFCP_ERP_EXIT = 3,
52 ZFCP_ERP_DISMISSED = 4,
53 ZFCP_ERP_NOMEM = 5,
54};
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
Christof Schmitt287ac012008-07-02 10:56:40 +020056static void zfcp_erp_adapter_block(struct zfcp_adapter *adapter, int mask)
Andreas Herrmann2abbe862006-09-18 22:29:56 +020057{
Christof Schmitt287ac012008-07-02 10:56:40 +020058 zfcp_erp_modify_adapter_status(adapter, 15, NULL,
59 ZFCP_STATUS_COMMON_UNBLOCKED | mask,
60 ZFCP_CLEAR);
Andreas Herrmann2abbe862006-09-18 22:29:56 +020061}
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Christof Schmitt287ac012008-07-02 10:56:40 +020063static int zfcp_erp_action_exists(struct zfcp_erp_action *act)
Linus Torvalds1da177e2005-04-16 15:20:36 -070064{
Christof Schmitt287ac012008-07-02 10:56:40 +020065 struct zfcp_erp_action *curr_act;
66
67 list_for_each_entry(curr_act, &act->adapter->erp_running_head, list)
68 if (act == curr_act)
69 return ZFCP_ERP_ACTION_RUNNING;
70 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071}
72
Christof Schmitt287ac012008-07-02 10:56:40 +020073static void zfcp_erp_action_ready(struct zfcp_erp_action *act)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074{
Christof Schmitt287ac012008-07-02 10:56:40 +020075 struct zfcp_adapter *adapter = act->adapter;
76
77 list_move(&act->list, &act->adapter->erp_ready_head);
78 zfcp_rec_dbf_event_action(146, act);
79 up(&adapter->erp_ready_sem);
80 zfcp_rec_dbf_event_thread(2, adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081}
82
Christof Schmitt287ac012008-07-02 10:56:40 +020083static void zfcp_erp_action_dismiss(struct zfcp_erp_action *act)
Linus Torvalds1da177e2005-04-16 15:20:36 -070084{
Christof Schmitt287ac012008-07-02 10:56:40 +020085 act->status |= ZFCP_STATUS_ERP_DISMISSED;
86 if (zfcp_erp_action_exists(act) == ZFCP_ERP_ACTION_RUNNING)
87 zfcp_erp_action_ready(act);
88}
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
Christof Schmitt287ac012008-07-02 10:56:40 +020090static void zfcp_erp_action_dismiss_unit(struct zfcp_unit *unit)
91{
92 if (atomic_read(&unit->status) & ZFCP_STATUS_COMMON_ERP_INUSE)
93 zfcp_erp_action_dismiss(&unit->erp_action);
94}
95
96static void zfcp_erp_action_dismiss_port(struct zfcp_port *port)
97{
98 struct zfcp_unit *unit;
99
100 if (atomic_read(&port->status) & ZFCP_STATUS_COMMON_ERP_INUSE)
101 zfcp_erp_action_dismiss(&port->erp_action);
102 else
103 list_for_each_entry(unit, &port->unit_list_head, list)
104 zfcp_erp_action_dismiss_unit(unit);
105}
106
107static void zfcp_erp_action_dismiss_adapter(struct zfcp_adapter *adapter)
108{
109 struct zfcp_port *port;
110
111 if (atomic_read(&adapter->status) & ZFCP_STATUS_COMMON_ERP_INUSE)
112 zfcp_erp_action_dismiss(&adapter->erp_action);
113 else
114 list_for_each_entry(port, &adapter->port_list_head, list)
115 zfcp_erp_action_dismiss_port(port);
116}
117
118static int zfcp_erp_required_act(int want, struct zfcp_adapter *adapter,
119 struct zfcp_port *port,
120 struct zfcp_unit *unit)
121{
122 int need = want;
123 int u_status, p_status, a_status;
124
125 switch (want) {
126 case ZFCP_ERP_ACTION_REOPEN_UNIT:
127 u_status = atomic_read(&unit->status);
128 if (u_status & ZFCP_STATUS_COMMON_ERP_INUSE)
129 return 0;
130 p_status = atomic_read(&port->status);
131 if (!(p_status & ZFCP_STATUS_COMMON_RUNNING) ||
132 p_status & ZFCP_STATUS_COMMON_ERP_FAILED)
133 return 0;
134 if (!(p_status & ZFCP_STATUS_COMMON_UNBLOCKED))
135 need = ZFCP_ERP_ACTION_REOPEN_PORT;
136 /* fall through */
137 case ZFCP_ERP_ACTION_REOPEN_PORT:
138 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
139 p_status = atomic_read(&port->status);
140 if (p_status & ZFCP_STATUS_COMMON_ERP_INUSE)
141 return 0;
142 a_status = atomic_read(&adapter->status);
143 if (!(a_status & ZFCP_STATUS_COMMON_RUNNING) ||
144 a_status & ZFCP_STATUS_COMMON_ERP_FAILED)
145 return 0;
146 if (!(a_status & ZFCP_STATUS_COMMON_UNBLOCKED))
147 need = ZFCP_ERP_ACTION_REOPEN_ADAPTER;
148 /* fall through */
149 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
150 a_status = atomic_read(&adapter->status);
151 if (a_status & ZFCP_STATUS_COMMON_ERP_INUSE)
152 return 0;
153 }
154
155 return need;
156}
157
158static struct zfcp_erp_action *zfcp_erp_setup_act(int need,
159 struct zfcp_adapter *adapter,
160 struct zfcp_port *port,
161 struct zfcp_unit *unit)
162{
163 struct zfcp_erp_action *erp_action;
164 u32 status = 0;
165
166 switch (need) {
167 case ZFCP_ERP_ACTION_REOPEN_UNIT:
168 zfcp_unit_get(unit);
169 atomic_set_mask(ZFCP_STATUS_COMMON_ERP_INUSE, &unit->status);
170 erp_action = &unit->erp_action;
171 if (!(atomic_read(&unit->status) & ZFCP_STATUS_COMMON_RUNNING))
172 status = ZFCP_STATUS_ERP_CLOSE_ONLY;
173 break;
174
175 case ZFCP_ERP_ACTION_REOPEN_PORT:
176 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
177 zfcp_port_get(port);
178 zfcp_erp_action_dismiss_port(port);
179 atomic_set_mask(ZFCP_STATUS_COMMON_ERP_INUSE, &port->status);
180 erp_action = &port->erp_action;
181 if (!(atomic_read(&port->status) & ZFCP_STATUS_COMMON_RUNNING))
182 status = ZFCP_STATUS_ERP_CLOSE_ONLY;
183 break;
184
185 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
186 zfcp_adapter_get(adapter);
187 zfcp_erp_action_dismiss_adapter(adapter);
188 atomic_set_mask(ZFCP_STATUS_COMMON_ERP_INUSE, &adapter->status);
189 erp_action = &adapter->erp_action;
190 if (!(atomic_read(&adapter->status) &
191 ZFCP_STATUS_COMMON_RUNNING))
192 status = ZFCP_STATUS_ERP_CLOSE_ONLY;
193 break;
194
195 default:
196 return NULL;
197 }
198
199 memset(erp_action, 0, sizeof(struct zfcp_erp_action));
200 erp_action->adapter = adapter;
201 erp_action->port = port;
202 erp_action->unit = unit;
203 erp_action->action = need;
204 erp_action->status = status;
205
206 return erp_action;
207}
208
209static int zfcp_erp_action_enqueue(int want, struct zfcp_adapter *adapter,
210 struct zfcp_port *port,
211 struct zfcp_unit *unit, u8 id, void *ref)
212{
213 int retval = 1, need;
214 struct zfcp_erp_action *act = NULL;
215
216 if (!(atomic_read(&adapter->status) &
217 ZFCP_STATUS_ADAPTER_ERP_THREAD_UP))
218 return -EIO;
219
220 need = zfcp_erp_required_act(want, adapter, port, unit);
221 if (!need)
222 goto out;
223
224 atomic_set_mask(ZFCP_STATUS_ADAPTER_ERP_PENDING, &adapter->status);
225 act = zfcp_erp_setup_act(need, adapter, port, unit);
226 if (!act)
227 goto out;
228 ++adapter->erp_total_count;
229 list_add_tail(&act->list, &adapter->erp_ready_head);
230 up(&adapter->erp_ready_sem);
231 zfcp_rec_dbf_event_thread(1, adapter);
232 retval = 0;
233 out:
234 zfcp_rec_dbf_event_trigger(id, ref, want, need, act,
235 adapter, port, unit);
236 return retval;
237}
238
239static int _zfcp_erp_adapter_reopen(struct zfcp_adapter *adapter,
240 int clear_mask, u8 id, void *ref)
241{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 zfcp_erp_adapter_block(adapter, clear_mask);
243
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) {
Martin Peschke1f6f7122008-04-18 12:51:55 +0200246 zfcp_erp_adapter_failed(adapter, 13, 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,
261 u8 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,
280 u8 id, void *ref)
281{
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 */
293void zfcp_erp_port_shutdown(struct zfcp_port *port, int clear, u8 id, void *ref)
294{
295 int flags = ZFCP_STATUS_COMMON_RUNNING | ZFCP_STATUS_COMMON_ERP_FAILED;
296 zfcp_erp_port_reopen(port, clear | flags, id, ref);
297}
298
299/**
300 * zfcp_erp_unit_shutdown - Shutdown unit
301 * @unit: Unit to shut down.
302 * @clear: Status flags to clear.
303 * @id: Id for debug trace event.
304 * @ref: Reference for debug trace event.
305 */
306void zfcp_erp_unit_shutdown(struct zfcp_unit *unit, int clear, u8 id, void *ref)
307{
308 int flags = ZFCP_STATUS_COMMON_RUNNING | ZFCP_STATUS_COMMON_ERP_FAILED;
309 zfcp_erp_unit_reopen(unit, clear | flags, id, ref);
310}
311
312static void zfcp_erp_port_block(struct zfcp_port *port, int clear)
313{
314 zfcp_erp_modify_port_status(port, 17, NULL,
315 ZFCP_STATUS_COMMON_UNBLOCKED | clear,
316 ZFCP_CLEAR);
317}
318
319static void _zfcp_erp_port_forced_reopen(struct zfcp_port *port,
320 int clear, u8 id, void *ref)
321{
322 zfcp_erp_port_block(port, clear);
323
324 if (atomic_read(&port->status) & ZFCP_STATUS_COMMON_ERP_FAILED)
325 return;
326
327 zfcp_erp_action_enqueue(ZFCP_ERP_ACTION_REOPEN_PORT_FORCED,
328 port->adapter, port, NULL, id, ref);
329}
330
331/**
332 * zfcp_erp_port_forced_reopen - Forced close of port and open again
333 * @port: Port to force close and to reopen.
334 * @id: Id for debug trace event.
335 * @ref: Reference for debug trace event.
336 */
337void zfcp_erp_port_forced_reopen(struct zfcp_port *port, int clear, u8 id,
338 void *ref)
339{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 unsigned long flags;
341 struct zfcp_adapter *adapter = port->adapter;
342
343 read_lock_irqsave(&zfcp_data.config_lock, flags);
344 write_lock(&adapter->erp_lock);
Christof Schmitt287ac012008-07-02 10:56:40 +0200345 _zfcp_erp_port_forced_reopen(port, clear, id, ref);
346 write_unlock(&adapter->erp_lock);
347 read_unlock_irqrestore(&zfcp_data.config_lock, flags);
348}
349
350static int _zfcp_erp_port_reopen(struct zfcp_port *port, int clear, u8 id,
351 void *ref)
352{
353 zfcp_erp_port_block(port, clear);
354
355 if (atomic_read(&port->status) & ZFCP_STATUS_COMMON_ERP_FAILED) {
356 /* ensure propagation of failed status to new devices */
357 zfcp_erp_port_failed(port, 14, NULL);
358 return -EIO;
359 }
360
361 return zfcp_erp_action_enqueue(ZFCP_ERP_ACTION_REOPEN_PORT,
362 port->adapter, port, NULL, id, ref);
363}
364
365/**
366 * zfcp_erp_port_reopen - trigger remote port recovery
367 * @port: port to recover
368 * @clear_mask: flags in port status to be cleared
369 *
370 * Returns 0 if recovery has been triggered, < 0 if not.
371 */
372int zfcp_erp_port_reopen(struct zfcp_port *port, int clear, u8 id, void *ref)
373{
374 unsigned long flags;
375 int retval;
376 struct zfcp_adapter *adapter = port->adapter;
377
378 read_lock_irqsave(&zfcp_data.config_lock, flags);
379 write_lock(&adapter->erp_lock);
380 retval = _zfcp_erp_port_reopen(port, clear, id, ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 write_unlock(&adapter->erp_lock);
382 read_unlock_irqrestore(&zfcp_data.config_lock, flags);
383
384 return retval;
385}
386
Christof Schmitt287ac012008-07-02 10:56:40 +0200387static void zfcp_erp_unit_block(struct zfcp_unit *unit, int clear_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388{
Christof Schmitt287ac012008-07-02 10:56:40 +0200389 zfcp_erp_modify_unit_status(unit, 19, NULL,
390 ZFCP_STATUS_COMMON_UNBLOCKED | clear_mask,
391 ZFCP_CLEAR);
392}
393
394static void _zfcp_erp_unit_reopen(struct zfcp_unit *unit, int clear, u8 id,
395 void *ref)
396{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 struct zfcp_adapter *adapter = unit->port->adapter;
398
Christof Schmitt287ac012008-07-02 10:56:40 +0200399 zfcp_erp_unit_block(unit, clear);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
Christof Schmitt287ac012008-07-02 10:56:40 +0200401 if (atomic_read(&unit->status) & ZFCP_STATUS_COMMON_ERP_FAILED)
402 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
Christof Schmitt287ac012008-07-02 10:56:40 +0200404 zfcp_erp_action_enqueue(ZFCP_ERP_ACTION_REOPEN_UNIT,
405 adapter, unit->port, unit, id, ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406}
407
408/**
409 * zfcp_erp_unit_reopen - initiate reopen of a unit
410 * @unit: unit to be reopened
411 * @clear_mask: specifies flags in unit status to be cleared
412 * Return: 0 on success, < 0 on error
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 */
Christof Schmitt287ac012008-07-02 10:56:40 +0200414void zfcp_erp_unit_reopen(struct zfcp_unit *unit, int clear, u8 id, void *ref)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 unsigned long flags;
Christof Schmitt287ac012008-07-02 10:56:40 +0200417 struct zfcp_port *port = unit->port;
418 struct zfcp_adapter *adapter = port->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
420 read_lock_irqsave(&zfcp_data.config_lock, flags);
421 write_lock(&adapter->erp_lock);
Christof Schmitt287ac012008-07-02 10:56:40 +0200422 _zfcp_erp_unit_reopen(unit, clear, id, ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 write_unlock(&adapter->erp_lock);
424 read_unlock_irqrestore(&zfcp_data.config_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425}
426
Christof Schmitt287ac012008-07-02 10:56:40 +0200427static int status_change_set(unsigned long mask, atomic_t *status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428{
Christof Schmitt287ac012008-07-02 10:56:40 +0200429 return (atomic_read(status) ^ mask) & mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430}
431
Christof Schmitt287ac012008-07-02 10:56:40 +0200432static int status_change_clear(unsigned long mask, atomic_t *status)
Martin Peschke698ec0162008-03-27 14:22:02 +0100433{
Christof Schmitt287ac012008-07-02 10:56:40 +0200434 return atomic_read(status) & mask;
Martin Peschke698ec0162008-03-27 14:22:02 +0100435}
436
Andreas Herrmannf6c0e7a2006-08-02 11:05:52 +0200437static void zfcp_erp_adapter_unblock(struct zfcp_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438{
Christof Schmitt287ac012008-07-02 10:56:40 +0200439 if (status_change_set(ZFCP_STATUS_COMMON_UNBLOCKED, &adapter->status))
Martin Peschke1f6f7122008-04-18 12:51:55 +0200440 zfcp_rec_dbf_event_adapter(16, NULL, adapter);
Christof Schmitt287ac012008-07-02 10:56:40 +0200441 atomic_set_mask(ZFCP_STATUS_COMMON_UNBLOCKED, &adapter->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442}
443
Christof Schmitt287ac012008-07-02 10:56:40 +0200444static void zfcp_erp_port_unblock(struct zfcp_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445{
Christof Schmitt287ac012008-07-02 10:56:40 +0200446 if (status_change_set(ZFCP_STATUS_COMMON_UNBLOCKED, &port->status))
Martin Peschke1f6f7122008-04-18 12:51:55 +0200447 zfcp_rec_dbf_event_port(18, NULL, port);
Christof Schmitt287ac012008-07-02 10:56:40 +0200448 atomic_set_mask(ZFCP_STATUS_COMMON_UNBLOCKED, &port->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449}
450
Christof Schmitt287ac012008-07-02 10:56:40 +0200451static void zfcp_erp_unit_unblock(struct zfcp_unit *unit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452{
Christof Schmitt287ac012008-07-02 10:56:40 +0200453 if (status_change_set(ZFCP_STATUS_COMMON_UNBLOCKED, &unit->status))
Martin Peschke1f6f7122008-04-18 12:51:55 +0200454 zfcp_rec_dbf_event_unit(20, NULL, unit);
Christof Schmitt287ac012008-07-02 10:56:40 +0200455 atomic_set_mask(ZFCP_STATUS_COMMON_UNBLOCKED, &unit->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456}
457
Christof Schmitt287ac012008-07-02 10:56:40 +0200458static void zfcp_erp_action_to_running(struct zfcp_erp_action *erp_action)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459{
Christof Schmitt287ac012008-07-02 10:56:40 +0200460 list_move(&erp_action->list, &erp_action->adapter->erp_running_head);
461 zfcp_rec_dbf_event_action(145, erp_action);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462}
463
Christof Schmitt287ac012008-07-02 10:56:40 +0200464static void zfcp_erp_strategy_check_fsfreq(struct zfcp_erp_action *act)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465{
Christof Schmitt287ac012008-07-02 10:56:40 +0200466 struct zfcp_adapter *adapter = act->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
Christof Schmitt287ac012008-07-02 10:56:40 +0200468 if (!act->fsf_req)
469 return;
470
471 spin_lock(&adapter->req_list_lock);
472 if (zfcp_reqlist_find_safe(adapter, act->fsf_req) &&
473 act->fsf_req->erp_action == act) {
474 if (act->status & (ZFCP_STATUS_ERP_DISMISSED |
475 ZFCP_STATUS_ERP_TIMEDOUT)) {
476 act->fsf_req->status |= ZFCP_STATUS_FSFREQ_DISMISSED;
477 zfcp_rec_dbf_event_action(142, act);
Martin Petermann7ea633f2008-11-04 16:35:11 +0100478 act->fsf_req->erp_action = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 }
Christof Schmitt287ac012008-07-02 10:56:40 +0200480 if (act->status & ZFCP_STATUS_ERP_TIMEDOUT)
481 zfcp_rec_dbf_event_action(143, act);
482 if (act->fsf_req->status & (ZFCP_STATUS_FSFREQ_COMPLETED |
483 ZFCP_STATUS_FSFREQ_DISMISSED))
484 act->fsf_req = NULL;
485 } else
486 act->fsf_req = NULL;
487 spin_unlock(&adapter->req_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488}
489
Andreas Herrmannf6c0e7a2006-08-02 11:05:52 +0200490/**
Christof Schmitt287ac012008-07-02 10:56:40 +0200491 * zfcp_erp_notify - Trigger ERP action.
492 * @erp_action: ERP action to continue.
493 * @set_mask: ERP action status flags to set.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 */
Christof Schmitt287ac012008-07-02 10:56:40 +0200495void zfcp_erp_notify(struct zfcp_erp_action *erp_action, unsigned long set_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496{
497 struct zfcp_adapter *adapter = erp_action->adapter;
498 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
500 write_lock_irqsave(&adapter->erp_lock, flags);
Christof Schmitt287ac012008-07-02 10:56:40 +0200501 if (zfcp_erp_action_exists(erp_action) == ZFCP_ERP_ACTION_RUNNING) {
502 erp_action->status |= set_mask;
503 zfcp_erp_action_ready(erp_action);
504 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 write_unlock_irqrestore(&adapter->erp_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506}
507
Andreas Herrmannf6c0e7a2006-08-02 11:05:52 +0200508/**
Christof Schmitt287ac012008-07-02 10:56:40 +0200509 * zfcp_erp_timeout_handler - Trigger ERP action from timed out ERP request
510 * @data: ERP action (from timer data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 */
Christof Schmitt287ac012008-07-02 10:56:40 +0200512void zfcp_erp_timeout_handler(unsigned long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513{
Christof Schmitt287ac012008-07-02 10:56:40 +0200514 struct zfcp_erp_action *act = (struct zfcp_erp_action *) data;
515 zfcp_erp_notify(act, ZFCP_STATUS_ERP_TIMEDOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516}
517
Christof Schmitt287ac012008-07-02 10:56:40 +0200518static void zfcp_erp_memwait_handler(unsigned long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519{
Christof Schmitt287ac012008-07-02 10:56:40 +0200520 zfcp_erp_notify((struct zfcp_erp_action *)data, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521}
522
Christof Schmitt287ac012008-07-02 10:56:40 +0200523static void zfcp_erp_strategy_memwait(struct zfcp_erp_action *erp_action)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 init_timer(&erp_action->timer);
526 erp_action->timer.function = zfcp_erp_memwait_handler;
527 erp_action->timer.data = (unsigned long) erp_action;
Christof Schmitt287ac012008-07-02 10:56:40 +0200528 erp_action->timer.expires = jiffies + HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 add_timer(&erp_action->timer);
Christof Schmitt287ac012008-07-02 10:56:40 +0200530}
531
532static void _zfcp_erp_port_reopen_all(struct zfcp_adapter *adapter,
533 int clear, u8 id, void *ref)
534{
535 struct zfcp_port *port;
536
537 list_for_each_entry(port, &adapter->port_list_head, list)
Swen Schillig5ab944f2008-10-01 12:42:17 +0200538 _zfcp_erp_port_reopen(port, clear, id, ref);
Christof Schmitt287ac012008-07-02 10:56:40 +0200539}
540
541static void _zfcp_erp_unit_reopen_all(struct zfcp_port *port, int clear, u8 id,
542 void *ref)
543{
544 struct zfcp_unit *unit;
545
546 list_for_each_entry(unit, &port->unit_list_head, list)
547 _zfcp_erp_unit_reopen(unit, clear, id, ref);
548}
549
550static void zfcp_erp_strategy_followup_actions(struct zfcp_erp_action *act)
551{
552 struct zfcp_adapter *adapter = act->adapter;
553 struct zfcp_port *port = act->port;
554 struct zfcp_unit *unit = act->unit;
555 u32 status = act->status;
556
557 /* initiate follow-up actions depending on success of finished action */
558 switch (act->action) {
559
560 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
561 if (status == ZFCP_ERP_SUCCEEDED)
562 _zfcp_erp_port_reopen_all(adapter, 0, 70, NULL);
563 else
564 _zfcp_erp_adapter_reopen(adapter, 0, 71, NULL);
565 break;
566
567 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
568 if (status == ZFCP_ERP_SUCCEEDED)
569 _zfcp_erp_port_reopen(port, 0, 72, NULL);
570 else
571 _zfcp_erp_adapter_reopen(adapter, 0, 73, NULL);
572 break;
573
574 case ZFCP_ERP_ACTION_REOPEN_PORT:
575 if (status == ZFCP_ERP_SUCCEEDED)
576 _zfcp_erp_unit_reopen_all(port, 0, 74, NULL);
577 else
578 _zfcp_erp_port_forced_reopen(port, 0, 75, NULL);
579 break;
580
581 case ZFCP_ERP_ACTION_REOPEN_UNIT:
582 if (status != ZFCP_ERP_SUCCEEDED)
583 _zfcp_erp_port_reopen(unit->port, 0, 76, NULL);
584 break;
585 }
586}
587
588static void zfcp_erp_wakeup(struct zfcp_adapter *adapter)
589{
590 unsigned long flags;
591
592 read_lock_irqsave(&zfcp_data.config_lock, flags);
593 read_lock(&adapter->erp_lock);
594 if (list_empty(&adapter->erp_ready_head) &&
595 list_empty(&adapter->erp_running_head)) {
596 atomic_clear_mask(ZFCP_STATUS_ADAPTER_ERP_PENDING,
597 &adapter->status);
598 wake_up(&adapter->erp_done_wqh);
599 }
600 read_unlock(&adapter->erp_lock);
601 read_unlock_irqrestore(&zfcp_data.config_lock, flags);
602}
603
604static int zfcp_erp_adapter_strategy_open_qdio(struct zfcp_erp_action *act)
605{
606 if (zfcp_qdio_open(act->adapter))
607 return ZFCP_ERP_FAILED;
608 init_waitqueue_head(&act->adapter->request_wq);
609 atomic_set_mask(ZFCP_STATUS_ADAPTER_QDIOUP, &act->adapter->status);
610 return ZFCP_ERP_SUCCEEDED;
611}
612
613static void zfcp_erp_enqueue_ptp_port(struct zfcp_adapter *adapter)
614{
615 struct zfcp_port *port;
616 port = zfcp_port_enqueue(adapter, adapter->peer_wwpn, 0,
617 adapter->peer_d_id);
618 if (IS_ERR(port)) /* error or port already attached */
619 return;
620 _zfcp_erp_port_reopen(port, 0, 150, NULL);
621}
622
623static int zfcp_erp_adapter_strat_fsf_xconf(struct zfcp_erp_action *erp_action)
624{
625 int retries;
626 int sleep = 1;
627 struct zfcp_adapter *adapter = erp_action->adapter;
628
629 atomic_clear_mask(ZFCP_STATUS_ADAPTER_XCONFIG_OK, &adapter->status);
630
631 for (retries = 7; retries; retries--) {
632 atomic_clear_mask(ZFCP_STATUS_ADAPTER_HOST_CON_INIT,
633 &adapter->status);
634 write_lock_irq(&adapter->erp_lock);
635 zfcp_erp_action_to_running(erp_action);
636 write_unlock_irq(&adapter->erp_lock);
637 if (zfcp_fsf_exchange_config_data(erp_action)) {
638 atomic_clear_mask(ZFCP_STATUS_ADAPTER_HOST_CON_INIT,
639 &adapter->status);
640 return ZFCP_ERP_FAILED;
641 }
642
643 zfcp_rec_dbf_event_thread_lock(6, adapter);
644 down(&adapter->erp_ready_sem);
645 zfcp_rec_dbf_event_thread_lock(7, adapter);
646 if (erp_action->status & ZFCP_STATUS_ERP_TIMEDOUT)
647 break;
648
649 if (!(atomic_read(&adapter->status) &
650 ZFCP_STATUS_ADAPTER_HOST_CON_INIT))
651 break;
652
653 ssleep(sleep);
654 sleep *= 2;
655 }
656
657 atomic_clear_mask(ZFCP_STATUS_ADAPTER_HOST_CON_INIT,
658 &adapter->status);
659
660 if (!(atomic_read(&adapter->status) & ZFCP_STATUS_ADAPTER_XCONFIG_OK))
661 return ZFCP_ERP_FAILED;
662
663 if (fc_host_port_type(adapter->scsi_host) == FC_PORTTYPE_PTP)
664 zfcp_erp_enqueue_ptp_port(adapter);
665
666 return ZFCP_ERP_SUCCEEDED;
667}
668
669static int zfcp_erp_adapter_strategy_open_fsf_xport(struct zfcp_erp_action *act)
670{
671 int ret;
672 struct zfcp_adapter *adapter = act->adapter;
673
Christof Schmitt287ac012008-07-02 10:56:40 +0200674 write_lock_irq(&adapter->erp_lock);
675 zfcp_erp_action_to_running(act);
676 write_unlock_irq(&adapter->erp_lock);
677
678 ret = zfcp_fsf_exchange_port_data(act);
679 if (ret == -EOPNOTSUPP)
680 return ZFCP_ERP_SUCCEEDED;
681 if (ret)
682 return ZFCP_ERP_FAILED;
683
684 zfcp_rec_dbf_event_thread_lock(8, adapter);
685 down(&adapter->erp_ready_sem);
686 zfcp_rec_dbf_event_thread_lock(9, adapter);
687 if (act->status & ZFCP_STATUS_ERP_TIMEDOUT)
688 return ZFCP_ERP_FAILED;
689
690 return ZFCP_ERP_SUCCEEDED;
691}
692
693static int zfcp_erp_adapter_strategy_open_fsf(struct zfcp_erp_action *act)
694{
695 if (zfcp_erp_adapter_strat_fsf_xconf(act) == ZFCP_ERP_FAILED)
696 return ZFCP_ERP_FAILED;
697
698 if (zfcp_erp_adapter_strategy_open_fsf_xport(act) == ZFCP_ERP_FAILED)
699 return ZFCP_ERP_FAILED;
700
701 atomic_set(&act->adapter->stat_miss, 16);
702 if (zfcp_status_read_refill(act->adapter))
703 return ZFCP_ERP_FAILED;
704
705 return ZFCP_ERP_SUCCEEDED;
706}
707
Swen Schilligcf13c082009-03-02 13:09:03 +0100708static void zfcp_erp_adapter_strategy_close(struct zfcp_erp_action *act)
Christof Schmitt287ac012008-07-02 10:56:40 +0200709{
Christof Schmitt287ac012008-07-02 10:56:40 +0200710 struct zfcp_adapter *adapter = act->adapter;
711
Christof Schmitt287ac012008-07-02 10:56:40 +0200712 /* close queues to ensure that buffers are not accessed by adapter */
713 zfcp_qdio_close(adapter);
714 zfcp_fsf_req_dismiss_all(adapter);
715 adapter->fsf_req_seq_no = 0;
716 /* all ports and units are closed */
717 zfcp_erp_modify_adapter_status(adapter, 24, NULL,
718 ZFCP_STATUS_COMMON_OPEN, ZFCP_CLEAR);
Swen Schilligcf13c082009-03-02 13:09:03 +0100719
Christof Schmitt287ac012008-07-02 10:56:40 +0200720 atomic_clear_mask(ZFCP_STATUS_ADAPTER_XCONFIG_OK |
Swen Schilligcf13c082009-03-02 13:09:03 +0100721 ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED, &adapter->status);
722}
723
724static int zfcp_erp_adapter_strategy_open(struct zfcp_erp_action *act)
725{
726 struct zfcp_adapter *adapter = act->adapter;
727
728 if (zfcp_erp_adapter_strategy_open_qdio(act)) {
729 atomic_clear_mask(ZFCP_STATUS_ADAPTER_XCONFIG_OK |
730 ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED,
731 &adapter->status);
732 return ZFCP_ERP_FAILED;
733 }
734
735 if (zfcp_erp_adapter_strategy_open_fsf(act)) {
736 zfcp_erp_adapter_strategy_close(act);
737 return ZFCP_ERP_FAILED;
738 }
739
740 atomic_set_mask(ZFCP_STATUS_COMMON_OPEN, &adapter->status);
741
742 return ZFCP_ERP_SUCCEEDED;
Christof Schmitt287ac012008-07-02 10:56:40 +0200743}
744
745static int zfcp_erp_adapter_strategy(struct zfcp_erp_action *act)
746{
Swen Schilligcf13c082009-03-02 13:09:03 +0100747 struct zfcp_adapter *adapter = act->adapter;
Christof Schmitt287ac012008-07-02 10:56:40 +0200748
Swen Schilligcf13c082009-03-02 13:09:03 +0100749 if (atomic_read(&adapter->status) & ZFCP_STATUS_COMMON_OPEN) {
750 zfcp_erp_adapter_strategy_close(act);
751 if (act->status & ZFCP_STATUS_ERP_CLOSE_ONLY)
752 return ZFCP_ERP_EXIT;
753 }
Christof Schmitt287ac012008-07-02 10:56:40 +0200754
Swen Schilligcf13c082009-03-02 13:09:03 +0100755 if (zfcp_erp_adapter_strategy_open(act)) {
Christof Schmitt287ac012008-07-02 10:56:40 +0200756 ssleep(8);
Swen Schilligcf13c082009-03-02 13:09:03 +0100757 return ZFCP_ERP_FAILED;
758 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
Swen Schilligcf13c082009-03-02 13:09:03 +0100760 return ZFCP_ERP_SUCCEEDED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761}
762
Christof Schmitt287ac012008-07-02 10:56:40 +0200763static int zfcp_erp_port_forced_strategy_close(struct zfcp_erp_action *act)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764{
Christof Schmitt287ac012008-07-02 10:56:40 +0200765 int retval;
766
767 retval = zfcp_fsf_close_physical_port(act);
768 if (retval == -ENOMEM)
769 return ZFCP_ERP_NOMEM;
770 act->step = ZFCP_ERP_STEP_PHYS_PORT_CLOSING;
771 if (retval)
772 return ZFCP_ERP_FAILED;
773
774 return ZFCP_ERP_CONTINUES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775}
776
Christof Schmitt287ac012008-07-02 10:56:40 +0200777static void zfcp_erp_port_strategy_clearstati(struct zfcp_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778{
Christof Schmitta5b11dd2009-03-02 13:08:54 +0100779 atomic_clear_mask(ZFCP_STATUS_COMMON_ACCESS_DENIED, &port->status);
Christof Schmitt287ac012008-07-02 10:56:40 +0200780}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781
Christof Schmitt287ac012008-07-02 10:56:40 +0200782static int zfcp_erp_port_forced_strategy(struct zfcp_erp_action *erp_action)
783{
784 struct zfcp_port *port = erp_action->port;
785 int status = atomic_read(&port->status);
786
787 switch (erp_action->step) {
788 case ZFCP_ERP_STEP_UNINITIALIZED:
789 zfcp_erp_port_strategy_clearstati(port);
790 if ((status & ZFCP_STATUS_PORT_PHYS_OPEN) &&
791 (status & ZFCP_STATUS_COMMON_OPEN))
792 return zfcp_erp_port_forced_strategy_close(erp_action);
793 else
794 return ZFCP_ERP_FAILED;
795
796 case ZFCP_ERP_STEP_PHYS_PORT_CLOSING:
797 if (status & ZFCP_STATUS_PORT_PHYS_OPEN)
798 return ZFCP_ERP_SUCCEEDED;
799 }
800 return ZFCP_ERP_FAILED;
801}
802
803static int zfcp_erp_port_strategy_close(struct zfcp_erp_action *erp_action)
804{
805 int retval;
806
807 retval = zfcp_fsf_close_port(erp_action);
808 if (retval == -ENOMEM)
809 return ZFCP_ERP_NOMEM;
810 erp_action->step = ZFCP_ERP_STEP_PORT_CLOSING;
811 if (retval)
812 return ZFCP_ERP_FAILED;
813 return ZFCP_ERP_CONTINUES;
814}
815
816static int zfcp_erp_port_strategy_open_port(struct zfcp_erp_action *erp_action)
817{
818 int retval;
819
820 retval = zfcp_fsf_open_port(erp_action);
821 if (retval == -ENOMEM)
822 return ZFCP_ERP_NOMEM;
823 erp_action->step = ZFCP_ERP_STEP_PORT_OPENING;
824 if (retval)
825 return ZFCP_ERP_FAILED;
826 return ZFCP_ERP_CONTINUES;
827}
828
Christof Schmitt287ac012008-07-02 10:56:40 +0200829static int zfcp_erp_open_ptp_port(struct zfcp_erp_action *act)
830{
831 struct zfcp_adapter *adapter = act->adapter;
832 struct zfcp_port *port = act->port;
833
834 if (port->wwpn != adapter->peer_wwpn) {
Christof Schmitt287ac012008-07-02 10:56:40 +0200835 zfcp_erp_port_failed(port, 25, NULL);
836 return ZFCP_ERP_FAILED;
837 }
838 port->d_id = adapter->peer_d_id;
Christof Schmitt287ac012008-07-02 10:56:40 +0200839 return zfcp_erp_port_strategy_open_port(act);
840}
841
Swen Schillig5ab944f2008-10-01 12:42:17 +0200842void zfcp_erp_port_strategy_open_lookup(struct work_struct *work)
843{
844 int retval;
845 struct zfcp_port *port = container_of(work, struct zfcp_port,
846 gid_pn_work);
847
848 retval = zfcp_fc_ns_gid_pn(&port->erp_action);
849 if (retval == -ENOMEM)
850 zfcp_erp_notify(&port->erp_action, ZFCP_ERP_NOMEM);
851 port->erp_action.step = ZFCP_ERP_STEP_NAMESERVER_LOOKUP;
852 if (retval)
853 zfcp_erp_notify(&port->erp_action, ZFCP_ERP_FAILED);
854
855}
856
Christof Schmitt287ac012008-07-02 10:56:40 +0200857static int zfcp_erp_port_strategy_open_common(struct zfcp_erp_action *act)
858{
859 struct zfcp_adapter *adapter = act->adapter;
860 struct zfcp_port *port = act->port;
Christof Schmitt287ac012008-07-02 10:56:40 +0200861 int p_status = atomic_read(&port->status);
862
863 switch (act->step) {
864 case ZFCP_ERP_STEP_UNINITIALIZED:
865 case ZFCP_ERP_STEP_PHYS_PORT_CLOSING:
866 case ZFCP_ERP_STEP_PORT_CLOSING:
867 if (fc_host_port_type(adapter->scsi_host) == FC_PORTTYPE_PTP)
868 return zfcp_erp_open_ptp_port(act);
Christof Schmittb98478d2008-12-19 16:56:59 +0100869 if (!port->d_id) {
Swen Schilligb7f15f32008-10-01 12:42:22 +0200870 queue_work(zfcp_data.work_queue, &port->gid_pn_work);
Swen Schillig5ab944f2008-10-01 12:42:17 +0200871 return ZFCP_ERP_CONTINUES;
Christof Schmitt287ac012008-07-02 10:56:40 +0200872 }
Christof Schmitt287ac012008-07-02 10:56:40 +0200873 case ZFCP_ERP_STEP_NAMESERVER_LOOKUP:
Christof Schmitta5b11dd2009-03-02 13:08:54 +0100874 if (!port->d_id)
Christof Schmitt287ac012008-07-02 10:56:40 +0200875 return ZFCP_ERP_FAILED;
Christof Schmitt287ac012008-07-02 10:56:40 +0200876 return zfcp_erp_port_strategy_open_port(act);
877
878 case ZFCP_ERP_STEP_PORT_OPENING:
879 /* D_ID might have changed during open */
Swen Schillig5ab944f2008-10-01 12:42:17 +0200880 if (p_status & ZFCP_STATUS_COMMON_OPEN) {
Christof Schmittb98478d2008-12-19 16:56:59 +0100881 if (port->d_id)
Swen Schillig5ab944f2008-10-01 12:42:17 +0200882 return ZFCP_ERP_SUCCEEDED;
883 else {
884 act->step = ZFCP_ERP_STEP_PORT_CLOSING;
885 return ZFCP_ERP_CONTINUES;
886 }
Christof Schmitt287ac012008-07-02 10:56:40 +0200887 /* fall through otherwise */
Swen Schillig5ab944f2008-10-01 12:42:17 +0200888 }
Christof Schmitt287ac012008-07-02 10:56:40 +0200889 }
890 return ZFCP_ERP_FAILED;
891}
892
Christof Schmitt287ac012008-07-02 10:56:40 +0200893static int zfcp_erp_port_strategy(struct zfcp_erp_action *erp_action)
894{
895 struct zfcp_port *port = erp_action->port;
896
Swen Schillig5ab944f2008-10-01 12:42:17 +0200897 if (atomic_read(&port->status) & ZFCP_STATUS_COMMON_NOESC)
898 goto close_init_done;
899
Christof Schmitt287ac012008-07-02 10:56:40 +0200900 switch (erp_action->step) {
901 case ZFCP_ERP_STEP_UNINITIALIZED:
902 zfcp_erp_port_strategy_clearstati(port);
903 if (atomic_read(&port->status) & ZFCP_STATUS_COMMON_OPEN)
904 return zfcp_erp_port_strategy_close(erp_action);
905 break;
906
907 case ZFCP_ERP_STEP_PORT_CLOSING:
908 if (atomic_read(&port->status) & ZFCP_STATUS_COMMON_OPEN)
909 return ZFCP_ERP_FAILED;
910 break;
911 }
Swen Schillig5ab944f2008-10-01 12:42:17 +0200912
913close_init_done:
Christof Schmitt287ac012008-07-02 10:56:40 +0200914 if (erp_action->status & ZFCP_STATUS_ERP_CLOSE_ONLY)
915 return ZFCP_ERP_EXIT;
Christof Schmitt287ac012008-07-02 10:56:40 +0200916
Swen Schillig5ab944f2008-10-01 12:42:17 +0200917 return zfcp_erp_port_strategy_open_common(erp_action);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918}
919
Christof Schmitt287ac012008-07-02 10:56:40 +0200920static void zfcp_erp_unit_strategy_clearstati(struct zfcp_unit *unit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921{
Swen Schillig44cc76f2008-10-01 12:42:16 +0200922 atomic_clear_mask(ZFCP_STATUS_COMMON_ACCESS_DENIED |
Christof Schmitt287ac012008-07-02 10:56:40 +0200923 ZFCP_STATUS_UNIT_SHARED |
924 ZFCP_STATUS_UNIT_READONLY,
925 &unit->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926}
927
Christof Schmitt287ac012008-07-02 10:56:40 +0200928static int zfcp_erp_unit_strategy_close(struct zfcp_erp_action *erp_action)
929{
930 int retval = zfcp_fsf_close_unit(erp_action);
931 if (retval == -ENOMEM)
932 return ZFCP_ERP_NOMEM;
933 erp_action->step = ZFCP_ERP_STEP_UNIT_CLOSING;
934 if (retval)
935 return ZFCP_ERP_FAILED;
936 return ZFCP_ERP_CONTINUES;
937}
938
939static int zfcp_erp_unit_strategy_open(struct zfcp_erp_action *erp_action)
940{
941 int retval = zfcp_fsf_open_unit(erp_action);
942 if (retval == -ENOMEM)
943 return ZFCP_ERP_NOMEM;
944 erp_action->step = ZFCP_ERP_STEP_UNIT_OPENING;
945 if (retval)
946 return ZFCP_ERP_FAILED;
947 return ZFCP_ERP_CONTINUES;
948}
949
950static int zfcp_erp_unit_strategy(struct zfcp_erp_action *erp_action)
951{
952 struct zfcp_unit *unit = erp_action->unit;
953
954 switch (erp_action->step) {
955 case ZFCP_ERP_STEP_UNINITIALIZED:
956 zfcp_erp_unit_strategy_clearstati(unit);
957 if (atomic_read(&unit->status) & ZFCP_STATUS_COMMON_OPEN)
958 return zfcp_erp_unit_strategy_close(erp_action);
959 /* already closed, fall through */
960 case ZFCP_ERP_STEP_UNIT_CLOSING:
961 if (atomic_read(&unit->status) & ZFCP_STATUS_COMMON_OPEN)
962 return ZFCP_ERP_FAILED;
963 if (erp_action->status & ZFCP_STATUS_ERP_CLOSE_ONLY)
964 return ZFCP_ERP_EXIT;
965 return zfcp_erp_unit_strategy_open(erp_action);
966
967 case ZFCP_ERP_STEP_UNIT_OPENING:
968 if (atomic_read(&unit->status) & ZFCP_STATUS_COMMON_OPEN)
969 return ZFCP_ERP_SUCCEEDED;
970 }
971 return ZFCP_ERP_FAILED;
972}
973
974static int zfcp_erp_strategy_check_unit(struct zfcp_unit *unit, int result)
975{
976 switch (result) {
977 case ZFCP_ERP_SUCCEEDED :
978 atomic_set(&unit->erp_counter, 0);
979 zfcp_erp_unit_unblock(unit);
980 break;
981 case ZFCP_ERP_FAILED :
982 atomic_inc(&unit->erp_counter);
Christof Schmittff3b24f2008-10-01 12:42:15 +0200983 if (atomic_read(&unit->erp_counter) > ZFCP_MAX_ERPS) {
984 dev_err(&unit->port->adapter->ccw_device->dev,
985 "ERP failed for unit 0x%016Lx on "
986 "port 0x%016Lx\n",
Swen Schillig7ba58c92008-10-01 12:42:18 +0200987 (unsigned long long)unit->fcp_lun,
988 (unsigned long long)unit->port->wwpn);
Christof Schmitt287ac012008-07-02 10:56:40 +0200989 zfcp_erp_unit_failed(unit, 21, NULL);
Christof Schmittff3b24f2008-10-01 12:42:15 +0200990 }
Christof Schmitt287ac012008-07-02 10:56:40 +0200991 break;
992 }
993
994 if (atomic_read(&unit->status) & ZFCP_STATUS_COMMON_ERP_FAILED) {
995 zfcp_erp_unit_block(unit, 0);
996 result = ZFCP_ERP_EXIT;
997 }
998 return result;
999}
1000
1001static int zfcp_erp_strategy_check_port(struct zfcp_port *port, int result)
1002{
1003 switch (result) {
1004 case ZFCP_ERP_SUCCEEDED :
1005 atomic_set(&port->erp_counter, 0);
1006 zfcp_erp_port_unblock(port);
1007 break;
1008
1009 case ZFCP_ERP_FAILED :
1010 if (atomic_read(&port->status) & ZFCP_STATUS_COMMON_NOESC) {
1011 zfcp_erp_port_block(port, 0);
1012 result = ZFCP_ERP_EXIT;
1013 }
1014 atomic_inc(&port->erp_counter);
Christof Schmittff3b24f2008-10-01 12:42:15 +02001015 if (atomic_read(&port->erp_counter) > ZFCP_MAX_ERPS) {
1016 dev_err(&port->adapter->ccw_device->dev,
1017 "ERP failed for remote port 0x%016Lx\n",
Swen Schillig7ba58c92008-10-01 12:42:18 +02001018 (unsigned long long)port->wwpn);
Christof Schmitt287ac012008-07-02 10:56:40 +02001019 zfcp_erp_port_failed(port, 22, NULL);
Christof Schmittff3b24f2008-10-01 12:42:15 +02001020 }
Christof Schmitt287ac012008-07-02 10:56:40 +02001021 break;
1022 }
1023
1024 if (atomic_read(&port->status) & ZFCP_STATUS_COMMON_ERP_FAILED) {
1025 zfcp_erp_port_block(port, 0);
1026 result = ZFCP_ERP_EXIT;
1027 }
1028 return result;
1029}
1030
1031static int zfcp_erp_strategy_check_adapter(struct zfcp_adapter *adapter,
1032 int result)
1033{
1034 switch (result) {
1035 case ZFCP_ERP_SUCCEEDED :
1036 atomic_set(&adapter->erp_counter, 0);
1037 zfcp_erp_adapter_unblock(adapter);
1038 break;
1039
1040 case ZFCP_ERP_FAILED :
1041 atomic_inc(&adapter->erp_counter);
Christof Schmittff3b24f2008-10-01 12:42:15 +02001042 if (atomic_read(&adapter->erp_counter) > ZFCP_MAX_ERPS) {
1043 dev_err(&adapter->ccw_device->dev,
1044 "ERP cannot recover an error "
1045 "on the FCP device\n");
Christof Schmitt287ac012008-07-02 10:56:40 +02001046 zfcp_erp_adapter_failed(adapter, 23, NULL);
Christof Schmittff3b24f2008-10-01 12:42:15 +02001047 }
Christof Schmitt287ac012008-07-02 10:56:40 +02001048 break;
1049 }
1050
1051 if (atomic_read(&adapter->status) & ZFCP_STATUS_COMMON_ERP_FAILED) {
1052 zfcp_erp_adapter_block(adapter, 0);
1053 result = ZFCP_ERP_EXIT;
1054 }
1055 return result;
1056}
1057
1058static int zfcp_erp_strategy_check_target(struct zfcp_erp_action *erp_action,
1059 int result)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060{
1061 struct zfcp_adapter *adapter = erp_action->adapter;
1062 struct zfcp_port *port = erp_action->port;
1063 struct zfcp_unit *unit = erp_action->unit;
1064
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 switch (erp_action->action) {
1066
1067 case ZFCP_ERP_ACTION_REOPEN_UNIT:
1068 result = zfcp_erp_strategy_check_unit(unit, result);
1069 break;
1070
1071 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
1072 case ZFCP_ERP_ACTION_REOPEN_PORT:
1073 result = zfcp_erp_strategy_check_port(port, result);
1074 break;
1075
1076 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
1077 result = zfcp_erp_strategy_check_adapter(adapter, result);
1078 break;
1079 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 return result;
1081}
1082
Christof Schmitt287ac012008-07-02 10:56:40 +02001083static int zfcp_erp_strat_change_det(atomic_t *target_status, u32 erp_status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084{
Christof Schmitt287ac012008-07-02 10:56:40 +02001085 int status = atomic_read(target_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086
Christof Schmitt287ac012008-07-02 10:56:40 +02001087 if ((status & ZFCP_STATUS_COMMON_RUNNING) &&
1088 (erp_status & ZFCP_STATUS_ERP_CLOSE_ONLY))
1089 return 1; /* take it online */
1090
1091 if (!(status & ZFCP_STATUS_COMMON_RUNNING) &&
1092 !(erp_status & ZFCP_STATUS_ERP_CLOSE_ONLY))
1093 return 1; /* take it offline */
1094
1095 return 0;
1096}
1097
1098static int zfcp_erp_strategy_statechange(struct zfcp_erp_action *act, int ret)
1099{
1100 int action = act->action;
1101 struct zfcp_adapter *adapter = act->adapter;
1102 struct zfcp_port *port = act->port;
1103 struct zfcp_unit *unit = act->unit;
1104 u32 erp_status = act->status;
1105
1106 switch (action) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
Christof Schmitt287ac012008-07-02 10:56:40 +02001108 if (zfcp_erp_strat_change_det(&adapter->status, erp_status)) {
1109 _zfcp_erp_adapter_reopen(adapter,
1110 ZFCP_STATUS_COMMON_ERP_FAILED,
1111 67, NULL);
1112 return ZFCP_ERP_EXIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 }
1114 break;
1115
1116 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
1117 case ZFCP_ERP_ACTION_REOPEN_PORT:
Christof Schmitt287ac012008-07-02 10:56:40 +02001118 if (zfcp_erp_strat_change_det(&port->status, erp_status)) {
1119 _zfcp_erp_port_reopen(port,
1120 ZFCP_STATUS_COMMON_ERP_FAILED,
1121 68, NULL);
1122 return ZFCP_ERP_EXIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 }
1124 break;
1125
1126 case ZFCP_ERP_ACTION_REOPEN_UNIT:
Christof Schmitt287ac012008-07-02 10:56:40 +02001127 if (zfcp_erp_strat_change_det(&unit->status, erp_status)) {
1128 _zfcp_erp_unit_reopen(unit,
1129 ZFCP_STATUS_COMMON_ERP_FAILED,
1130 69, NULL);
1131 return ZFCP_ERP_EXIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 }
1133 break;
1134 }
Christof Schmitt287ac012008-07-02 10:56:40 +02001135 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136}
1137
Christof Schmitt287ac012008-07-02 10:56:40 +02001138static void zfcp_erp_action_dequeue(struct zfcp_erp_action *erp_action)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139{
Christof Schmitt287ac012008-07-02 10:56:40 +02001140 struct zfcp_adapter *adapter = erp_action->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141
Christof Schmitt287ac012008-07-02 10:56:40 +02001142 adapter->erp_total_count--;
1143 if (erp_action->status & ZFCP_STATUS_ERP_LOWMEM) {
1144 adapter->erp_low_mem_count--;
1145 erp_action->status &= ~ZFCP_STATUS_ERP_LOWMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 }
1147
Christof Schmitt287ac012008-07-02 10:56:40 +02001148 list_del(&erp_action->list);
1149 zfcp_rec_dbf_event_action(144, erp_action);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150
Christof Schmitt287ac012008-07-02 10:56:40 +02001151 switch (erp_action->action) {
1152 case ZFCP_ERP_ACTION_REOPEN_UNIT:
1153 atomic_clear_mask(ZFCP_STATUS_COMMON_ERP_INUSE,
1154 &erp_action->unit->status);
1155 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156
Christof Schmitt287ac012008-07-02 10:56:40 +02001157 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
1158 case ZFCP_ERP_ACTION_REOPEN_PORT:
1159 atomic_clear_mask(ZFCP_STATUS_COMMON_ERP_INUSE,
1160 &erp_action->port->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 break;
Christof Schmitt287ac012008-07-02 10:56:40 +02001162
1163 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
1164 atomic_clear_mask(ZFCP_STATUS_COMMON_ERP_INUSE,
1165 &erp_action->adapter->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 break;
1167 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168}
1169
Christof Schmitt5f852be2007-05-08 11:16:52 +02001170struct zfcp_erp_add_work {
1171 struct zfcp_unit *unit;
1172 struct work_struct work;
1173};
1174
Christof Schmitt5f852be2007-05-08 11:16:52 +02001175static void zfcp_erp_scsi_scan(struct work_struct *work)
1176{
1177 struct zfcp_erp_add_work *p =
1178 container_of(work, struct zfcp_erp_add_work, work);
1179 struct zfcp_unit *unit = p->unit;
1180 struct fc_rport *rport = unit->port->rport;
Swen Schillig26871c92008-11-26 18:07:38 +01001181
1182 if (rport && rport->port_state == FC_PORTSTATE_ONLINE)
1183 scsi_scan_target(&rport->dev, 0, rport->scsi_target_id,
Christof Schmitt04062892008-10-01 12:42:20 +02001184 scsilun_to_int((struct scsi_lun *)&unit->fcp_lun), 0);
Christof Schmitt5f852be2007-05-08 11:16:52 +02001185 atomic_clear_mask(ZFCP_STATUS_UNIT_SCSI_WORK_PENDING, &unit->status);
Christof Schmitt5f852be2007-05-08 11:16:52 +02001186 zfcp_unit_put(unit);
Swen Schillig091694a2008-10-01 12:42:25 +02001187 wake_up(&unit->port->adapter->erp_done_wqh);
Christof Schmitt5f852be2007-05-08 11:16:52 +02001188 kfree(p);
1189}
1190
Christof Schmitt287ac012008-07-02 10:56:40 +02001191static void zfcp_erp_schedule_work(struct zfcp_unit *unit)
Christof Schmitt5f852be2007-05-08 11:16:52 +02001192{
1193 struct zfcp_erp_add_work *p;
1194
Swen Schillig0d661322007-07-18 10:55:08 +02001195 p = kzalloc(sizeof(*p), GFP_KERNEL);
Christof Schmitt5f852be2007-05-08 11:16:52 +02001196 if (!p) {
Christof Schmitt553448f2008-06-10 18:20:58 +02001197 dev_err(&unit->port->adapter->ccw_device->dev,
Christof Schmittff3b24f2008-10-01 12:42:15 +02001198 "Registering unit 0x%016Lx on port 0x%016Lx failed\n",
Swen Schillig7ba58c92008-10-01 12:42:18 +02001199 (unsigned long long)unit->fcp_lun,
1200 (unsigned long long)unit->port->wwpn);
Christof Schmitt5f852be2007-05-08 11:16:52 +02001201 return;
1202 }
1203
1204 zfcp_unit_get(unit);
Christof Schmitt5f852be2007-05-08 11:16:52 +02001205 atomic_set_mask(ZFCP_STATUS_UNIT_SCSI_WORK_PENDING, &unit->status);
1206 INIT_WORK(&p->work, zfcp_erp_scsi_scan);
1207 p->unit = unit;
Swen Schilligb7f15f32008-10-01 12:42:22 +02001208 queue_work(zfcp_data.work_queue, &p->work);
Christof Schmitt5f852be2007-05-08 11:16:52 +02001209}
1210
Christof Schmitt287ac012008-07-02 10:56:40 +02001211static void zfcp_erp_rport_register(struct zfcp_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212{
Christof Schmitt287ac012008-07-02 10:56:40 +02001213 struct fc_rport_identifiers ids;
1214 ids.node_name = port->wwnn;
1215 ids.port_name = port->wwpn;
1216 ids.port_id = port->d_id;
1217 ids.roles = FC_RPORT_ROLE_FCP_TARGET;
1218 port->rport = fc_remote_port_add(port->adapter->scsi_host, 0, &ids);
1219 if (!port->rport) {
1220 dev_err(&port->adapter->ccw_device->dev,
Christof Schmittff3b24f2008-10-01 12:42:15 +02001221 "Registering port 0x%016Lx failed\n",
Swen Schillig7ba58c92008-10-01 12:42:18 +02001222 (unsigned long long)port->wwpn);
Swen Schilligcc8c2822008-06-10 18:21:00 +02001223 return;
Christof Schmitt287ac012008-07-02 10:56:40 +02001224 }
1225
1226 scsi_target_unblock(&port->rport->dev);
1227 port->rport->maxframe_size = port->maxframe_size;
1228 port->rport->supported_classes = port->supported_classes;
Swen Schilligcc8c2822008-06-10 18:21:00 +02001229}
1230
Christof Schmitt287ac012008-07-02 10:56:40 +02001231static void zfcp_erp_rports_del(struct zfcp_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232{
Christof Schmitt287ac012008-07-02 10:56:40 +02001233 struct zfcp_port *port;
Swen Schillig5ab944f2008-10-01 12:42:17 +02001234 list_for_each_entry(port, &adapter->port_list_head, list) {
Swen Schillige4e9ba52008-10-01 12:42:23 +02001235 if (!port->rport)
1236 continue;
Swen Schillig5ab944f2008-10-01 12:42:17 +02001237 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) {
Christof Schmitt287ac012008-07-02 10:56:40 +02001252 if (!(atomic_read(&unit->status) &
1253 ZFCP_STATUS_UNIT_SCSI_WORK_PENDING))
Christof Schmitt5f852be2007-05-08 11:16:52 +02001254 zfcp_erp_schedule_work(unit);
Andreas Herrmannad58f7d2006-03-10 00:56:16 +01001255 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 zfcp_unit_put(unit);
1257 break;
Christof Schmitt287ac012008-07-02 10:56:40 +02001258
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
1260 case ZFCP_ERP_ACTION_REOPEN_PORT:
Christof Schmitt287ac012008-07-02 10:56:40 +02001261 if ((result == ZFCP_ERP_SUCCEEDED) && !port->rport)
1262 zfcp_erp_rport_register(port);
Andreas Herrmann338151e2006-05-22 18:25:56 +02001263 if ((result != ZFCP_ERP_SUCCEEDED) && port->rport) {
1264 fc_remote_port_delete(port->rport);
1265 port->rport = NULL;
1266 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 zfcp_port_put(port);
1268 break;
Christof Schmitt287ac012008-07-02 10:56:40 +02001269
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
Christof Schmittbd43a42b72008-12-25 13:38:50 +01001271 if (result != ZFCP_ERP_SUCCEEDED) {
1272 unregister_service_level(&adapter->service_level);
Christof Schmitt287ac012008-07-02 10:56:40 +02001273 zfcp_erp_rports_del(adapter);
Christof Schmittbd43a42b72008-12-25 13:38:50 +01001274 } else {
1275 register_service_level(&adapter->service_level);
Swen Schilligfca55b62008-11-26 18:07:40 +01001276 schedule_work(&adapter->scan_work);
Christof Schmittbd43a42b72008-12-25 13:38:50 +01001277 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 zfcp_adapter_put(adapter);
1279 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 }
1281}
1282
Christof Schmitt287ac012008-07-02 10:56:40 +02001283static int zfcp_erp_strategy_do_action(struct zfcp_erp_action *erp_action)
1284{
1285 switch (erp_action->action) {
1286 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
1287 return zfcp_erp_adapter_strategy(erp_action);
1288 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
1289 return zfcp_erp_port_forced_strategy(erp_action);
1290 case ZFCP_ERP_ACTION_REOPEN_PORT:
1291 return zfcp_erp_port_strategy(erp_action);
1292 case ZFCP_ERP_ACTION_REOPEN_UNIT:
1293 return zfcp_erp_unit_strategy(erp_action);
1294 }
1295 return ZFCP_ERP_FAILED;
1296}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297
Christof Schmitt287ac012008-07-02 10:56:40 +02001298static int zfcp_erp_strategy(struct zfcp_erp_action *erp_action)
1299{
1300 int retval;
1301 struct zfcp_adapter *adapter = erp_action->adapter;
1302 unsigned long flags;
1303
1304 read_lock_irqsave(&zfcp_data.config_lock, flags);
1305 write_lock(&adapter->erp_lock);
1306
1307 zfcp_erp_strategy_check_fsfreq(erp_action);
1308
1309 if (erp_action->status & ZFCP_STATUS_ERP_DISMISSED) {
1310 zfcp_erp_action_dequeue(erp_action);
1311 retval = ZFCP_ERP_DISMISSED;
1312 goto unlock;
1313 }
1314
1315 zfcp_erp_action_to_running(erp_action);
1316
1317 /* no lock to allow for blocking operations */
1318 write_unlock(&adapter->erp_lock);
1319 read_unlock_irqrestore(&zfcp_data.config_lock, flags);
1320 retval = zfcp_erp_strategy_do_action(erp_action);
1321 read_lock_irqsave(&zfcp_data.config_lock, flags);
1322 write_lock(&adapter->erp_lock);
1323
1324 if (erp_action->status & ZFCP_STATUS_ERP_DISMISSED)
1325 retval = ZFCP_ERP_CONTINUES;
1326
1327 switch (retval) {
1328 case ZFCP_ERP_NOMEM:
1329 if (!(erp_action->status & ZFCP_STATUS_ERP_LOWMEM)) {
1330 ++adapter->erp_low_mem_count;
1331 erp_action->status |= ZFCP_STATUS_ERP_LOWMEM;
1332 }
1333 if (adapter->erp_total_count == adapter->erp_low_mem_count)
1334 _zfcp_erp_adapter_reopen(adapter, 0, 66, NULL);
1335 else {
1336 zfcp_erp_strategy_memwait(erp_action);
1337 retval = ZFCP_ERP_CONTINUES;
1338 }
1339 goto unlock;
1340
1341 case ZFCP_ERP_CONTINUES:
1342 if (erp_action->status & ZFCP_STATUS_ERP_LOWMEM) {
1343 --adapter->erp_low_mem_count;
1344 erp_action->status &= ~ZFCP_STATUS_ERP_LOWMEM;
1345 }
1346 goto unlock;
1347 }
1348
1349 retval = zfcp_erp_strategy_check_target(erp_action, retval);
1350 zfcp_erp_action_dequeue(erp_action);
1351 retval = zfcp_erp_strategy_statechange(erp_action, retval);
1352 if (retval == ZFCP_ERP_EXIT)
1353 goto unlock;
1354 zfcp_erp_strategy_followup_actions(erp_action);
1355
1356 unlock:
1357 write_unlock(&adapter->erp_lock);
1358 read_unlock_irqrestore(&zfcp_data.config_lock, flags);
1359
1360 if (retval != ZFCP_ERP_CONTINUES)
1361 zfcp_erp_action_cleanup(erp_action, retval);
1362
1363 return retval;
1364}
1365
1366static int zfcp_erp_thread(void *data)
1367{
1368 struct zfcp_adapter *adapter = (struct zfcp_adapter *) data;
1369 struct list_head *next;
1370 struct zfcp_erp_action *act;
1371 unsigned long flags;
Heiko Carstens06499fa2008-12-19 16:56:56 +01001372 int ignore;
Christof Schmitt287ac012008-07-02 10:56:40 +02001373
Cornelia Huckb9d3aed2008-10-10 21:33:11 +02001374 daemonize("zfcperp%s", dev_name(&adapter->ccw_device->dev));
Christof Schmitt287ac012008-07-02 10:56:40 +02001375 /* 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
Swen Schillig9fb3cd82008-10-01 12:42:24 +02001394 zfcp_rec_dbf_event_thread_lock(4, adapter);
Heiko Carstens06499fa2008-12-19 16:56:56 +01001395 ignore = down_interruptible(&adapter->erp_ready_sem);
Swen Schillig9fb3cd82008-10-01 12:42:24 +02001396 zfcp_rec_dbf_event_thread_lock(5, adapter);
Christof Schmitt287ac012008-07-02 10:56:40 +02001397 }
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);
Swen Schillig41bfcf92008-10-01 12:42:26 +02001441 zfcp_rec_dbf_event_thread_lock(3, adapter);
Christof Schmitt287ac012008-07-02 10:56:40 +02001442
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}