[SCSI] zfcp: Use kthread API for zfcp erp thread
[deliverable/linux.git] / drivers / s390 / scsi / zfcp_erp.c
1 /*
2 * zfcp device driver
3 *
4 * Error Recovery Procedures (ERP).
5 *
6 * Copyright IBM Corporation 2002, 2009
7 */
8
9 #define KMSG_COMPONENT "zfcp"
10 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
11
12 #include <linux/kthread.h>
13 #include "zfcp_ext.h"
14
15 #define ZFCP_MAX_ERPS 3
16
17 enum zfcp_erp_act_flags {
18 ZFCP_STATUS_ERP_TIMEDOUT = 0x10000000,
19 ZFCP_STATUS_ERP_CLOSE_ONLY = 0x01000000,
20 ZFCP_STATUS_ERP_DISMISSING = 0x00100000,
21 ZFCP_STATUS_ERP_DISMISSED = 0x00200000,
22 ZFCP_STATUS_ERP_LOWMEM = 0x00400000,
23 };
24
25 enum zfcp_erp_steps {
26 ZFCP_ERP_STEP_UNINITIALIZED = 0x0000,
27 ZFCP_ERP_STEP_FSF_XCONFIG = 0x0001,
28 ZFCP_ERP_STEP_PHYS_PORT_CLOSING = 0x0010,
29 ZFCP_ERP_STEP_PORT_CLOSING = 0x0100,
30 ZFCP_ERP_STEP_PORT_OPENING = 0x0800,
31 ZFCP_ERP_STEP_UNIT_CLOSING = 0x1000,
32 ZFCP_ERP_STEP_UNIT_OPENING = 0x2000,
33 };
34
35 enum 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 };
41
42 enum zfcp_erp_act_state {
43 ZFCP_ERP_ACTION_RUNNING = 1,
44 ZFCP_ERP_ACTION_READY = 2,
45 };
46
47 enum 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 };
55
56 static void zfcp_erp_adapter_block(struct zfcp_adapter *adapter, int mask)
57 {
58 zfcp_erp_modify_adapter_status(adapter, "erablk1", NULL,
59 ZFCP_STATUS_COMMON_UNBLOCKED | mask,
60 ZFCP_CLEAR);
61 }
62
63 static int zfcp_erp_action_exists(struct zfcp_erp_action *act)
64 {
65 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;
71 }
72
73 static void zfcp_erp_action_ready(struct zfcp_erp_action *act)
74 {
75 struct zfcp_adapter *adapter = act->adapter;
76
77 list_move(&act->list, &act->adapter->erp_ready_head);
78 zfcp_dbf_rec_action("erardy1", act);
79 wake_up(&adapter->erp_ready_wq);
80 zfcp_dbf_rec_thread("erardy2", adapter->dbf);
81 }
82
83 static void zfcp_erp_action_dismiss(struct zfcp_erp_action *act)
84 {
85 act->status |= ZFCP_STATUS_ERP_DISMISSED;
86 if (zfcp_erp_action_exists(act) == ZFCP_ERP_ACTION_RUNNING)
87 zfcp_erp_action_ready(act);
88 }
89
90 static 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
96 static 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
107 static 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
118 static 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
158 static 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
209 static int zfcp_erp_action_enqueue(int want, struct zfcp_adapter *adapter,
210 struct zfcp_port *port,
211 struct zfcp_unit *unit, char *id, void *ref)
212 {
213 int retval = 1, need;
214 struct zfcp_erp_action *act = NULL;
215
216 if (!adapter->erp_thread)
217 return -EIO;
218
219 need = zfcp_erp_required_act(want, adapter, port, unit);
220 if (!need)
221 goto out;
222
223 atomic_set_mask(ZFCP_STATUS_ADAPTER_ERP_PENDING, &adapter->status);
224 act = zfcp_erp_setup_act(need, adapter, port, unit);
225 if (!act)
226 goto out;
227 ++adapter->erp_total_count;
228 list_add_tail(&act->list, &adapter->erp_ready_head);
229 wake_up(&adapter->erp_ready_wq);
230 zfcp_dbf_rec_thread("eracte1", adapter->dbf);
231 retval = 0;
232 out:
233 zfcp_dbf_rec_trigger(id, ref, want, need, act, adapter, port, unit);
234 return retval;
235 }
236
237 static int _zfcp_erp_adapter_reopen(struct zfcp_adapter *adapter,
238 int clear_mask, char *id, void *ref)
239 {
240 zfcp_erp_adapter_block(adapter, clear_mask);
241 zfcp_scsi_schedule_rports_block(adapter);
242
243 /* ensure propagation of failed status to new devices */
244 if (atomic_read(&adapter->status) & ZFCP_STATUS_COMMON_ERP_FAILED) {
245 zfcp_erp_adapter_failed(adapter, "erareo1", NULL);
246 return -EIO;
247 }
248 return zfcp_erp_action_enqueue(ZFCP_ERP_ACTION_REOPEN_ADAPTER,
249 adapter, NULL, NULL, id, ref);
250 }
251
252 /**
253 * zfcp_erp_adapter_reopen - Reopen adapter.
254 * @adapter: Adapter to reopen.
255 * @clear: Status flags to clear.
256 * @id: Id for debug trace event.
257 * @ref: Reference for debug trace event.
258 */
259 void zfcp_erp_adapter_reopen(struct zfcp_adapter *adapter, int clear,
260 char *id, void *ref)
261 {
262 unsigned long flags;
263
264 read_lock_irqsave(&zfcp_data.config_lock, flags);
265 write_lock(&adapter->erp_lock);
266 _zfcp_erp_adapter_reopen(adapter, clear, id, ref);
267 write_unlock(&adapter->erp_lock);
268 read_unlock_irqrestore(&zfcp_data.config_lock, flags);
269 }
270
271 /**
272 * zfcp_erp_adapter_shutdown - Shutdown adapter.
273 * @adapter: Adapter to shut down.
274 * @clear: Status flags to clear.
275 * @id: Id for debug trace event.
276 * @ref: Reference for debug trace event.
277 */
278 void zfcp_erp_adapter_shutdown(struct zfcp_adapter *adapter, int clear,
279 char *id, void *ref)
280 {
281 int flags = ZFCP_STATUS_COMMON_RUNNING | ZFCP_STATUS_COMMON_ERP_FAILED;
282 zfcp_erp_adapter_reopen(adapter, clear | flags, id, ref);
283 }
284
285 /**
286 * zfcp_erp_port_shutdown - Shutdown port
287 * @port: Port to shut down.
288 * @clear: Status flags to clear.
289 * @id: Id for debug trace event.
290 * @ref: Reference for debug trace event.
291 */
292 void zfcp_erp_port_shutdown(struct zfcp_port *port, int clear, char *id,
293 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 */
306 void zfcp_erp_unit_shutdown(struct zfcp_unit *unit, int clear, char *id,
307 void *ref)
308 {
309 int flags = ZFCP_STATUS_COMMON_RUNNING | ZFCP_STATUS_COMMON_ERP_FAILED;
310 zfcp_erp_unit_reopen(unit, clear | flags, id, ref);
311 }
312
313 static void zfcp_erp_port_block(struct zfcp_port *port, int clear)
314 {
315 zfcp_erp_modify_port_status(port, "erpblk1", NULL,
316 ZFCP_STATUS_COMMON_UNBLOCKED | clear,
317 ZFCP_CLEAR);
318 }
319
320 static void _zfcp_erp_port_forced_reopen(struct zfcp_port *port,
321 int clear, char *id, void *ref)
322 {
323 zfcp_erp_port_block(port, clear);
324 zfcp_scsi_schedule_rport_block(port);
325
326 if (atomic_read(&port->status) & ZFCP_STATUS_COMMON_ERP_FAILED)
327 return;
328
329 zfcp_erp_action_enqueue(ZFCP_ERP_ACTION_REOPEN_PORT_FORCED,
330 port->adapter, port, NULL, id, ref);
331 }
332
333 /**
334 * zfcp_erp_port_forced_reopen - Forced close of port and open again
335 * @port: Port to force close and to reopen.
336 * @id: Id for debug trace event.
337 * @ref: Reference for debug trace event.
338 */
339 void zfcp_erp_port_forced_reopen(struct zfcp_port *port, int clear, char *id,
340 void *ref)
341 {
342 unsigned long flags;
343 struct zfcp_adapter *adapter = port->adapter;
344
345 read_lock_irqsave(&zfcp_data.config_lock, flags);
346 write_lock(&adapter->erp_lock);
347 _zfcp_erp_port_forced_reopen(port, clear, id, ref);
348 write_unlock(&adapter->erp_lock);
349 read_unlock_irqrestore(&zfcp_data.config_lock, flags);
350 }
351
352 static int _zfcp_erp_port_reopen(struct zfcp_port *port, int clear, char *id,
353 void *ref)
354 {
355 zfcp_erp_port_block(port, clear);
356 zfcp_scsi_schedule_rport_block(port);
357
358 if (atomic_read(&port->status) & ZFCP_STATUS_COMMON_ERP_FAILED) {
359 /* ensure propagation of failed status to new devices */
360 zfcp_erp_port_failed(port, "erpreo1", NULL);
361 return -EIO;
362 }
363
364 return zfcp_erp_action_enqueue(ZFCP_ERP_ACTION_REOPEN_PORT,
365 port->adapter, port, NULL, id, ref);
366 }
367
368 /**
369 * zfcp_erp_port_reopen - trigger remote port recovery
370 * @port: port to recover
371 * @clear_mask: flags in port status to be cleared
372 *
373 * Returns 0 if recovery has been triggered, < 0 if not.
374 */
375 int zfcp_erp_port_reopen(struct zfcp_port *port, int clear, char *id, void *ref)
376 {
377 unsigned long flags;
378 int retval;
379 struct zfcp_adapter *adapter = port->adapter;
380
381 read_lock_irqsave(&zfcp_data.config_lock, flags);
382 write_lock(&adapter->erp_lock);
383 retval = _zfcp_erp_port_reopen(port, clear, id, ref);
384 write_unlock(&adapter->erp_lock);
385 read_unlock_irqrestore(&zfcp_data.config_lock, flags);
386
387 return retval;
388 }
389
390 static void zfcp_erp_unit_block(struct zfcp_unit *unit, int clear_mask)
391 {
392 zfcp_erp_modify_unit_status(unit, "erublk1", NULL,
393 ZFCP_STATUS_COMMON_UNBLOCKED | clear_mask,
394 ZFCP_CLEAR);
395 }
396
397 static void _zfcp_erp_unit_reopen(struct zfcp_unit *unit, int clear, char *id,
398 void *ref)
399 {
400 struct zfcp_adapter *adapter = unit->port->adapter;
401
402 zfcp_erp_unit_block(unit, clear);
403
404 if (atomic_read(&unit->status) & ZFCP_STATUS_COMMON_ERP_FAILED)
405 return;
406
407 zfcp_erp_action_enqueue(ZFCP_ERP_ACTION_REOPEN_UNIT,
408 adapter, unit->port, unit, id, ref);
409 }
410
411 /**
412 * zfcp_erp_unit_reopen - initiate reopen of a unit
413 * @unit: unit to be reopened
414 * @clear_mask: specifies flags in unit status to be cleared
415 * Return: 0 on success, < 0 on error
416 */
417 void zfcp_erp_unit_reopen(struct zfcp_unit *unit, int clear, char *id,
418 void *ref)
419 {
420 unsigned long flags;
421 struct zfcp_port *port = unit->port;
422 struct zfcp_adapter *adapter = port->adapter;
423
424 read_lock_irqsave(&zfcp_data.config_lock, flags);
425 write_lock(&adapter->erp_lock);
426 _zfcp_erp_unit_reopen(unit, clear, id, ref);
427 write_unlock(&adapter->erp_lock);
428 read_unlock_irqrestore(&zfcp_data.config_lock, flags);
429 }
430
431 static int status_change_set(unsigned long mask, atomic_t *status)
432 {
433 return (atomic_read(status) ^ mask) & mask;
434 }
435
436 static int status_change_clear(unsigned long mask, atomic_t *status)
437 {
438 return atomic_read(status) & mask;
439 }
440
441 static void zfcp_erp_adapter_unblock(struct zfcp_adapter *adapter)
442 {
443 if (status_change_set(ZFCP_STATUS_COMMON_UNBLOCKED, &adapter->status))
444 zfcp_dbf_rec_adapter("eraubl1", NULL, adapter->dbf);
445 atomic_set_mask(ZFCP_STATUS_COMMON_UNBLOCKED, &adapter->status);
446 }
447
448 static void zfcp_erp_port_unblock(struct zfcp_port *port)
449 {
450 if (status_change_set(ZFCP_STATUS_COMMON_UNBLOCKED, &port->status))
451 zfcp_dbf_rec_port("erpubl1", NULL, port);
452 atomic_set_mask(ZFCP_STATUS_COMMON_UNBLOCKED, &port->status);
453 }
454
455 static void zfcp_erp_unit_unblock(struct zfcp_unit *unit)
456 {
457 if (status_change_set(ZFCP_STATUS_COMMON_UNBLOCKED, &unit->status))
458 zfcp_dbf_rec_unit("eruubl1", NULL, unit);
459 atomic_set_mask(ZFCP_STATUS_COMMON_UNBLOCKED, &unit->status);
460 }
461
462 static void zfcp_erp_action_to_running(struct zfcp_erp_action *erp_action)
463 {
464 list_move(&erp_action->list, &erp_action->adapter->erp_running_head);
465 zfcp_dbf_rec_action("erator1", erp_action);
466 }
467
468 static void zfcp_erp_strategy_check_fsfreq(struct zfcp_erp_action *act)
469 {
470 struct zfcp_adapter *adapter = act->adapter;
471
472 if (!act->fsf_req)
473 return;
474
475 spin_lock(&adapter->req_list_lock);
476 if (zfcp_reqlist_find_safe(adapter, act->fsf_req) &&
477 act->fsf_req->erp_action == act) {
478 if (act->status & (ZFCP_STATUS_ERP_DISMISSED |
479 ZFCP_STATUS_ERP_TIMEDOUT)) {
480 act->fsf_req->status |= ZFCP_STATUS_FSFREQ_DISMISSED;
481 zfcp_dbf_rec_action("erscf_1", act);
482 act->fsf_req->erp_action = NULL;
483 }
484 if (act->status & ZFCP_STATUS_ERP_TIMEDOUT)
485 zfcp_dbf_rec_action("erscf_2", act);
486 if (act->fsf_req->status & ZFCP_STATUS_FSFREQ_DISMISSED)
487 act->fsf_req = NULL;
488 } else
489 act->fsf_req = NULL;
490 spin_unlock(&adapter->req_list_lock);
491 }
492
493 /**
494 * zfcp_erp_notify - Trigger ERP action.
495 * @erp_action: ERP action to continue.
496 * @set_mask: ERP action status flags to set.
497 */
498 void zfcp_erp_notify(struct zfcp_erp_action *erp_action, unsigned long set_mask)
499 {
500 struct zfcp_adapter *adapter = erp_action->adapter;
501 unsigned long flags;
502
503 write_lock_irqsave(&adapter->erp_lock, flags);
504 if (zfcp_erp_action_exists(erp_action) == ZFCP_ERP_ACTION_RUNNING) {
505 erp_action->status |= set_mask;
506 zfcp_erp_action_ready(erp_action);
507 }
508 write_unlock_irqrestore(&adapter->erp_lock, flags);
509 }
510
511 /**
512 * zfcp_erp_timeout_handler - Trigger ERP action from timed out ERP request
513 * @data: ERP action (from timer data)
514 */
515 void zfcp_erp_timeout_handler(unsigned long data)
516 {
517 struct zfcp_erp_action *act = (struct zfcp_erp_action *) data;
518 zfcp_erp_notify(act, ZFCP_STATUS_ERP_TIMEDOUT);
519 }
520
521 static void zfcp_erp_memwait_handler(unsigned long data)
522 {
523 zfcp_erp_notify((struct zfcp_erp_action *)data, 0);
524 }
525
526 static void zfcp_erp_strategy_memwait(struct zfcp_erp_action *erp_action)
527 {
528 init_timer(&erp_action->timer);
529 erp_action->timer.function = zfcp_erp_memwait_handler;
530 erp_action->timer.data = (unsigned long) erp_action;
531 erp_action->timer.expires = jiffies + HZ;
532 add_timer(&erp_action->timer);
533 }
534
535 static void _zfcp_erp_port_reopen_all(struct zfcp_adapter *adapter,
536 int clear, char *id, void *ref)
537 {
538 struct zfcp_port *port;
539
540 list_for_each_entry(port, &adapter->port_list_head, list)
541 _zfcp_erp_port_reopen(port, clear, id, ref);
542 }
543
544 static void _zfcp_erp_unit_reopen_all(struct zfcp_port *port, int clear,
545 char *id, void *ref)
546 {
547 struct zfcp_unit *unit;
548
549 list_for_each_entry(unit, &port->unit_list_head, list)
550 _zfcp_erp_unit_reopen(unit, clear, id, ref);
551 }
552
553 static void zfcp_erp_strategy_followup_failed(struct zfcp_erp_action *act)
554 {
555 switch (act->action) {
556 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
557 _zfcp_erp_adapter_reopen(act->adapter, 0, "ersff_1", NULL);
558 break;
559 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
560 _zfcp_erp_port_forced_reopen(act->port, 0, "ersff_2", NULL);
561 break;
562 case ZFCP_ERP_ACTION_REOPEN_PORT:
563 _zfcp_erp_port_reopen(act->port, 0, "ersff_3", NULL);
564 break;
565 case ZFCP_ERP_ACTION_REOPEN_UNIT:
566 _zfcp_erp_unit_reopen(act->unit, 0, "ersff_4", NULL);
567 break;
568 }
569 }
570
571 static void zfcp_erp_strategy_followup_success(struct zfcp_erp_action *act)
572 {
573 switch (act->action) {
574 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
575 _zfcp_erp_port_reopen_all(act->adapter, 0, "ersfs_1", NULL);
576 break;
577 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
578 _zfcp_erp_port_reopen(act->port, 0, "ersfs_2", NULL);
579 break;
580 case ZFCP_ERP_ACTION_REOPEN_PORT:
581 _zfcp_erp_unit_reopen_all(act->port, 0, "ersfs_3", NULL);
582 break;
583 }
584 }
585
586 static void zfcp_erp_wakeup(struct zfcp_adapter *adapter)
587 {
588 unsigned long flags;
589
590 read_lock_irqsave(&zfcp_data.config_lock, flags);
591 read_lock(&adapter->erp_lock);
592 if (list_empty(&adapter->erp_ready_head) &&
593 list_empty(&adapter->erp_running_head)) {
594 atomic_clear_mask(ZFCP_STATUS_ADAPTER_ERP_PENDING,
595 &adapter->status);
596 wake_up(&adapter->erp_done_wqh);
597 }
598 read_unlock(&adapter->erp_lock);
599 read_unlock_irqrestore(&zfcp_data.config_lock, flags);
600 }
601
602 static int zfcp_erp_adapter_strategy_open_qdio(struct zfcp_erp_action *act)
603 {
604 struct zfcp_qdio *qdio = act->adapter->qdio;
605
606 if (zfcp_qdio_open(qdio))
607 return ZFCP_ERP_FAILED;
608 init_waitqueue_head(&qdio->req_q_wq);
609 atomic_set_mask(ZFCP_STATUS_ADAPTER_QDIOUP, &act->adapter->status);
610 return ZFCP_ERP_SUCCEEDED;
611 }
612
613 static 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, "ereptp1", NULL);
621 }
622
623 static 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_dbf_rec_thread_lock("erasfx1", adapter->dbf);
644 wait_event(adapter->erp_ready_wq,
645 !list_empty(&adapter->erp_ready_head));
646 zfcp_dbf_rec_thread_lock("erasfx2", adapter->dbf);
647 if (erp_action->status & ZFCP_STATUS_ERP_TIMEDOUT)
648 break;
649
650 if (!(atomic_read(&adapter->status) &
651 ZFCP_STATUS_ADAPTER_HOST_CON_INIT))
652 break;
653
654 ssleep(sleep);
655 sleep *= 2;
656 }
657
658 atomic_clear_mask(ZFCP_STATUS_ADAPTER_HOST_CON_INIT,
659 &adapter->status);
660
661 if (!(atomic_read(&adapter->status) & ZFCP_STATUS_ADAPTER_XCONFIG_OK))
662 return ZFCP_ERP_FAILED;
663
664 if (fc_host_port_type(adapter->scsi_host) == FC_PORTTYPE_PTP)
665 zfcp_erp_enqueue_ptp_port(adapter);
666
667 return ZFCP_ERP_SUCCEEDED;
668 }
669
670 static int zfcp_erp_adapter_strategy_open_fsf_xport(struct zfcp_erp_action *act)
671 {
672 int ret;
673 struct zfcp_adapter *adapter = act->adapter;
674
675 write_lock_irq(&adapter->erp_lock);
676 zfcp_erp_action_to_running(act);
677 write_unlock_irq(&adapter->erp_lock);
678
679 ret = zfcp_fsf_exchange_port_data(act);
680 if (ret == -EOPNOTSUPP)
681 return ZFCP_ERP_SUCCEEDED;
682 if (ret)
683 return ZFCP_ERP_FAILED;
684
685 zfcp_dbf_rec_thread_lock("erasox1", adapter->dbf);
686 wait_event(adapter->erp_ready_wq,
687 !list_empty(&adapter->erp_ready_head));
688 zfcp_dbf_rec_thread_lock("erasox2", adapter->dbf);
689 if (act->status & ZFCP_STATUS_ERP_TIMEDOUT)
690 return ZFCP_ERP_FAILED;
691
692 return ZFCP_ERP_SUCCEEDED;
693 }
694
695 static int zfcp_erp_adapter_strategy_open_fsf(struct zfcp_erp_action *act)
696 {
697 if (zfcp_erp_adapter_strat_fsf_xconf(act) == ZFCP_ERP_FAILED)
698 return ZFCP_ERP_FAILED;
699
700 if (zfcp_erp_adapter_strategy_open_fsf_xport(act) == ZFCP_ERP_FAILED)
701 return ZFCP_ERP_FAILED;
702
703 atomic_set(&act->adapter->stat_miss, 16);
704 if (zfcp_status_read_refill(act->adapter))
705 return ZFCP_ERP_FAILED;
706
707 return ZFCP_ERP_SUCCEEDED;
708 }
709
710 static void zfcp_erp_adapter_strategy_close(struct zfcp_erp_action *act)
711 {
712 struct zfcp_adapter *adapter = act->adapter;
713
714 /* close queues to ensure that buffers are not accessed by adapter */
715 zfcp_qdio_close(adapter->qdio);
716 zfcp_fsf_req_dismiss_all(adapter);
717 adapter->fsf_req_seq_no = 0;
718 zfcp_fc_wka_ports_force_offline(adapter->gs);
719 /* all ports and units are closed */
720 zfcp_erp_modify_adapter_status(adapter, "erascl1", NULL,
721 ZFCP_STATUS_COMMON_OPEN, ZFCP_CLEAR);
722
723 atomic_clear_mask(ZFCP_STATUS_ADAPTER_XCONFIG_OK |
724 ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED, &adapter->status);
725 }
726
727 static int zfcp_erp_adapter_strategy_open(struct zfcp_erp_action *act)
728 {
729 struct zfcp_adapter *adapter = act->adapter;
730
731 if (zfcp_erp_adapter_strategy_open_qdio(act)) {
732 atomic_clear_mask(ZFCP_STATUS_ADAPTER_XCONFIG_OK |
733 ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED,
734 &adapter->status);
735 return ZFCP_ERP_FAILED;
736 }
737
738 if (zfcp_erp_adapter_strategy_open_fsf(act)) {
739 zfcp_erp_adapter_strategy_close(act);
740 return ZFCP_ERP_FAILED;
741 }
742
743 atomic_set_mask(ZFCP_STATUS_COMMON_OPEN, &adapter->status);
744
745 return ZFCP_ERP_SUCCEEDED;
746 }
747
748 static int zfcp_erp_adapter_strategy(struct zfcp_erp_action *act)
749 {
750 struct zfcp_adapter *adapter = act->adapter;
751
752 if (atomic_read(&adapter->status) & ZFCP_STATUS_COMMON_OPEN) {
753 zfcp_erp_adapter_strategy_close(act);
754 if (act->status & ZFCP_STATUS_ERP_CLOSE_ONLY)
755 return ZFCP_ERP_EXIT;
756 }
757
758 if (zfcp_erp_adapter_strategy_open(act)) {
759 ssleep(8);
760 return ZFCP_ERP_FAILED;
761 }
762
763 return ZFCP_ERP_SUCCEEDED;
764 }
765
766 static int zfcp_erp_port_forced_strategy_close(struct zfcp_erp_action *act)
767 {
768 int retval;
769
770 retval = zfcp_fsf_close_physical_port(act);
771 if (retval == -ENOMEM)
772 return ZFCP_ERP_NOMEM;
773 act->step = ZFCP_ERP_STEP_PHYS_PORT_CLOSING;
774 if (retval)
775 return ZFCP_ERP_FAILED;
776
777 return ZFCP_ERP_CONTINUES;
778 }
779
780 static void zfcp_erp_port_strategy_clearstati(struct zfcp_port *port)
781 {
782 atomic_clear_mask(ZFCP_STATUS_COMMON_ACCESS_DENIED, &port->status);
783 }
784
785 static int zfcp_erp_port_forced_strategy(struct zfcp_erp_action *erp_action)
786 {
787 struct zfcp_port *port = erp_action->port;
788 int status = atomic_read(&port->status);
789
790 switch (erp_action->step) {
791 case ZFCP_ERP_STEP_UNINITIALIZED:
792 zfcp_erp_port_strategy_clearstati(port);
793 if ((status & ZFCP_STATUS_PORT_PHYS_OPEN) &&
794 (status & ZFCP_STATUS_COMMON_OPEN))
795 return zfcp_erp_port_forced_strategy_close(erp_action);
796 else
797 return ZFCP_ERP_FAILED;
798
799 case ZFCP_ERP_STEP_PHYS_PORT_CLOSING:
800 if (!(status & ZFCP_STATUS_PORT_PHYS_OPEN))
801 return ZFCP_ERP_SUCCEEDED;
802 }
803 return ZFCP_ERP_FAILED;
804 }
805
806 static int zfcp_erp_port_strategy_close(struct zfcp_erp_action *erp_action)
807 {
808 int retval;
809
810 retval = zfcp_fsf_close_port(erp_action);
811 if (retval == -ENOMEM)
812 return ZFCP_ERP_NOMEM;
813 erp_action->step = ZFCP_ERP_STEP_PORT_CLOSING;
814 if (retval)
815 return ZFCP_ERP_FAILED;
816 return ZFCP_ERP_CONTINUES;
817 }
818
819 static int zfcp_erp_port_strategy_open_port(struct zfcp_erp_action *erp_action)
820 {
821 int retval;
822
823 retval = zfcp_fsf_open_port(erp_action);
824 if (retval == -ENOMEM)
825 return ZFCP_ERP_NOMEM;
826 erp_action->step = ZFCP_ERP_STEP_PORT_OPENING;
827 if (retval)
828 return ZFCP_ERP_FAILED;
829 return ZFCP_ERP_CONTINUES;
830 }
831
832 static int zfcp_erp_open_ptp_port(struct zfcp_erp_action *act)
833 {
834 struct zfcp_adapter *adapter = act->adapter;
835 struct zfcp_port *port = act->port;
836
837 if (port->wwpn != adapter->peer_wwpn) {
838 zfcp_erp_port_failed(port, "eroptp1", NULL);
839 return ZFCP_ERP_FAILED;
840 }
841 port->d_id = adapter->peer_d_id;
842 return zfcp_erp_port_strategy_open_port(act);
843 }
844
845 static int zfcp_erp_port_strategy_open_common(struct zfcp_erp_action *act)
846 {
847 struct zfcp_adapter *adapter = act->adapter;
848 struct zfcp_port *port = act->port;
849 int p_status = atomic_read(&port->status);
850
851 switch (act->step) {
852 case ZFCP_ERP_STEP_UNINITIALIZED:
853 case ZFCP_ERP_STEP_PHYS_PORT_CLOSING:
854 case ZFCP_ERP_STEP_PORT_CLOSING:
855 if (fc_host_port_type(adapter->scsi_host) == FC_PORTTYPE_PTP)
856 return zfcp_erp_open_ptp_port(act);
857 if (!port->d_id) {
858 zfcp_port_get(port);
859 if (!queue_work(adapter->work_queue,
860 &port->gid_pn_work))
861 zfcp_port_put(port);
862 return ZFCP_ERP_EXIT;
863 }
864 return zfcp_erp_port_strategy_open_port(act);
865
866 case ZFCP_ERP_STEP_PORT_OPENING:
867 /* D_ID might have changed during open */
868 if (p_status & ZFCP_STATUS_COMMON_OPEN) {
869 if (port->d_id)
870 return ZFCP_ERP_SUCCEEDED;
871 else {
872 act->step = ZFCP_ERP_STEP_PORT_CLOSING;
873 return ZFCP_ERP_CONTINUES;
874 }
875 }
876 if (port->d_id && !(p_status & ZFCP_STATUS_COMMON_NOESC)) {
877 port->d_id = 0;
878 _zfcp_erp_port_reopen(port, 0, "erpsoc1", NULL);
879 return ZFCP_ERP_EXIT;
880 }
881 /* fall through otherwise */
882 }
883 return ZFCP_ERP_FAILED;
884 }
885
886 static int zfcp_erp_port_strategy(struct zfcp_erp_action *erp_action)
887 {
888 struct zfcp_port *port = erp_action->port;
889
890 if (atomic_read(&port->status) & ZFCP_STATUS_COMMON_NOESC)
891 goto close_init_done;
892
893 switch (erp_action->step) {
894 case ZFCP_ERP_STEP_UNINITIALIZED:
895 zfcp_erp_port_strategy_clearstati(port);
896 if (atomic_read(&port->status) & ZFCP_STATUS_COMMON_OPEN)
897 return zfcp_erp_port_strategy_close(erp_action);
898 break;
899
900 case ZFCP_ERP_STEP_PORT_CLOSING:
901 if (atomic_read(&port->status) & ZFCP_STATUS_COMMON_OPEN)
902 return ZFCP_ERP_FAILED;
903 break;
904 }
905
906 close_init_done:
907 if (erp_action->status & ZFCP_STATUS_ERP_CLOSE_ONLY)
908 return ZFCP_ERP_EXIT;
909
910 return zfcp_erp_port_strategy_open_common(erp_action);
911 }
912
913 static void zfcp_erp_unit_strategy_clearstati(struct zfcp_unit *unit)
914 {
915 atomic_clear_mask(ZFCP_STATUS_COMMON_ACCESS_DENIED |
916 ZFCP_STATUS_UNIT_SHARED |
917 ZFCP_STATUS_UNIT_READONLY,
918 &unit->status);
919 }
920
921 static int zfcp_erp_unit_strategy_close(struct zfcp_erp_action *erp_action)
922 {
923 int retval = zfcp_fsf_close_unit(erp_action);
924 if (retval == -ENOMEM)
925 return ZFCP_ERP_NOMEM;
926 erp_action->step = ZFCP_ERP_STEP_UNIT_CLOSING;
927 if (retval)
928 return ZFCP_ERP_FAILED;
929 return ZFCP_ERP_CONTINUES;
930 }
931
932 static int zfcp_erp_unit_strategy_open(struct zfcp_erp_action *erp_action)
933 {
934 int retval = zfcp_fsf_open_unit(erp_action);
935 if (retval == -ENOMEM)
936 return ZFCP_ERP_NOMEM;
937 erp_action->step = ZFCP_ERP_STEP_UNIT_OPENING;
938 if (retval)
939 return ZFCP_ERP_FAILED;
940 return ZFCP_ERP_CONTINUES;
941 }
942
943 static int zfcp_erp_unit_strategy(struct zfcp_erp_action *erp_action)
944 {
945 struct zfcp_unit *unit = erp_action->unit;
946
947 switch (erp_action->step) {
948 case ZFCP_ERP_STEP_UNINITIALIZED:
949 zfcp_erp_unit_strategy_clearstati(unit);
950 if (atomic_read(&unit->status) & ZFCP_STATUS_COMMON_OPEN)
951 return zfcp_erp_unit_strategy_close(erp_action);
952 /* already closed, fall through */
953 case ZFCP_ERP_STEP_UNIT_CLOSING:
954 if (atomic_read(&unit->status) & ZFCP_STATUS_COMMON_OPEN)
955 return ZFCP_ERP_FAILED;
956 if (erp_action->status & ZFCP_STATUS_ERP_CLOSE_ONLY)
957 return ZFCP_ERP_EXIT;
958 return zfcp_erp_unit_strategy_open(erp_action);
959
960 case ZFCP_ERP_STEP_UNIT_OPENING:
961 if (atomic_read(&unit->status) & ZFCP_STATUS_COMMON_OPEN)
962 return ZFCP_ERP_SUCCEEDED;
963 }
964 return ZFCP_ERP_FAILED;
965 }
966
967 static int zfcp_erp_strategy_check_unit(struct zfcp_unit *unit, int result)
968 {
969 switch (result) {
970 case ZFCP_ERP_SUCCEEDED :
971 atomic_set(&unit->erp_counter, 0);
972 zfcp_erp_unit_unblock(unit);
973 break;
974 case ZFCP_ERP_FAILED :
975 atomic_inc(&unit->erp_counter);
976 if (atomic_read(&unit->erp_counter) > ZFCP_MAX_ERPS) {
977 dev_err(&unit->port->adapter->ccw_device->dev,
978 "ERP failed for unit 0x%016Lx on "
979 "port 0x%016Lx\n",
980 (unsigned long long)unit->fcp_lun,
981 (unsigned long long)unit->port->wwpn);
982 zfcp_erp_unit_failed(unit, "erusck1", NULL);
983 }
984 break;
985 }
986
987 if (atomic_read(&unit->status) & ZFCP_STATUS_COMMON_ERP_FAILED) {
988 zfcp_erp_unit_block(unit, 0);
989 result = ZFCP_ERP_EXIT;
990 }
991 return result;
992 }
993
994 static int zfcp_erp_strategy_check_port(struct zfcp_port *port, int result)
995 {
996 switch (result) {
997 case ZFCP_ERP_SUCCEEDED :
998 atomic_set(&port->erp_counter, 0);
999 zfcp_erp_port_unblock(port);
1000 break;
1001
1002 case ZFCP_ERP_FAILED :
1003 if (atomic_read(&port->status) & ZFCP_STATUS_COMMON_NOESC) {
1004 zfcp_erp_port_block(port, 0);
1005 result = ZFCP_ERP_EXIT;
1006 }
1007 atomic_inc(&port->erp_counter);
1008 if (atomic_read(&port->erp_counter) > ZFCP_MAX_ERPS) {
1009 dev_err(&port->adapter->ccw_device->dev,
1010 "ERP failed for remote port 0x%016Lx\n",
1011 (unsigned long long)port->wwpn);
1012 zfcp_erp_port_failed(port, "erpsck1", NULL);
1013 }
1014 break;
1015 }
1016
1017 if (atomic_read(&port->status) & ZFCP_STATUS_COMMON_ERP_FAILED) {
1018 zfcp_erp_port_block(port, 0);
1019 result = ZFCP_ERP_EXIT;
1020 }
1021 return result;
1022 }
1023
1024 static int zfcp_erp_strategy_check_adapter(struct zfcp_adapter *adapter,
1025 int result)
1026 {
1027 switch (result) {
1028 case ZFCP_ERP_SUCCEEDED :
1029 atomic_set(&adapter->erp_counter, 0);
1030 zfcp_erp_adapter_unblock(adapter);
1031 break;
1032
1033 case ZFCP_ERP_FAILED :
1034 atomic_inc(&adapter->erp_counter);
1035 if (atomic_read(&adapter->erp_counter) > ZFCP_MAX_ERPS) {
1036 dev_err(&adapter->ccw_device->dev,
1037 "ERP cannot recover an error "
1038 "on the FCP device\n");
1039 zfcp_erp_adapter_failed(adapter, "erasck1", NULL);
1040 }
1041 break;
1042 }
1043
1044 if (atomic_read(&adapter->status) & ZFCP_STATUS_COMMON_ERP_FAILED) {
1045 zfcp_erp_adapter_block(adapter, 0);
1046 result = ZFCP_ERP_EXIT;
1047 }
1048 return result;
1049 }
1050
1051 static int zfcp_erp_strategy_check_target(struct zfcp_erp_action *erp_action,
1052 int result)
1053 {
1054 struct zfcp_adapter *adapter = erp_action->adapter;
1055 struct zfcp_port *port = erp_action->port;
1056 struct zfcp_unit *unit = erp_action->unit;
1057
1058 switch (erp_action->action) {
1059
1060 case ZFCP_ERP_ACTION_REOPEN_UNIT:
1061 result = zfcp_erp_strategy_check_unit(unit, result);
1062 break;
1063
1064 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
1065 case ZFCP_ERP_ACTION_REOPEN_PORT:
1066 result = zfcp_erp_strategy_check_port(port, result);
1067 break;
1068
1069 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
1070 result = zfcp_erp_strategy_check_adapter(adapter, result);
1071 break;
1072 }
1073 return result;
1074 }
1075
1076 static int zfcp_erp_strat_change_det(atomic_t *target_status, u32 erp_status)
1077 {
1078 int status = atomic_read(target_status);
1079
1080 if ((status & ZFCP_STATUS_COMMON_RUNNING) &&
1081 (erp_status & ZFCP_STATUS_ERP_CLOSE_ONLY))
1082 return 1; /* take it online */
1083
1084 if (!(status & ZFCP_STATUS_COMMON_RUNNING) &&
1085 !(erp_status & ZFCP_STATUS_ERP_CLOSE_ONLY))
1086 return 1; /* take it offline */
1087
1088 return 0;
1089 }
1090
1091 static int zfcp_erp_strategy_statechange(struct zfcp_erp_action *act, int ret)
1092 {
1093 int action = act->action;
1094 struct zfcp_adapter *adapter = act->adapter;
1095 struct zfcp_port *port = act->port;
1096 struct zfcp_unit *unit = act->unit;
1097 u32 erp_status = act->status;
1098
1099 switch (action) {
1100 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
1101 if (zfcp_erp_strat_change_det(&adapter->status, erp_status)) {
1102 _zfcp_erp_adapter_reopen(adapter,
1103 ZFCP_STATUS_COMMON_ERP_FAILED,
1104 "ersscg1", NULL);
1105 return ZFCP_ERP_EXIT;
1106 }
1107 break;
1108
1109 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
1110 case ZFCP_ERP_ACTION_REOPEN_PORT:
1111 if (zfcp_erp_strat_change_det(&port->status, erp_status)) {
1112 _zfcp_erp_port_reopen(port,
1113 ZFCP_STATUS_COMMON_ERP_FAILED,
1114 "ersscg2", NULL);
1115 return ZFCP_ERP_EXIT;
1116 }
1117 break;
1118
1119 case ZFCP_ERP_ACTION_REOPEN_UNIT:
1120 if (zfcp_erp_strat_change_det(&unit->status, erp_status)) {
1121 _zfcp_erp_unit_reopen(unit,
1122 ZFCP_STATUS_COMMON_ERP_FAILED,
1123 "ersscg3", NULL);
1124 return ZFCP_ERP_EXIT;
1125 }
1126 break;
1127 }
1128 return ret;
1129 }
1130
1131 static void zfcp_erp_action_dequeue(struct zfcp_erp_action *erp_action)
1132 {
1133 struct zfcp_adapter *adapter = erp_action->adapter;
1134
1135 adapter->erp_total_count--;
1136 if (erp_action->status & ZFCP_STATUS_ERP_LOWMEM) {
1137 adapter->erp_low_mem_count--;
1138 erp_action->status &= ~ZFCP_STATUS_ERP_LOWMEM;
1139 }
1140
1141 list_del(&erp_action->list);
1142 zfcp_dbf_rec_action("eractd1", erp_action);
1143
1144 switch (erp_action->action) {
1145 case ZFCP_ERP_ACTION_REOPEN_UNIT:
1146 atomic_clear_mask(ZFCP_STATUS_COMMON_ERP_INUSE,
1147 &erp_action->unit->status);
1148 break;
1149
1150 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
1151 case ZFCP_ERP_ACTION_REOPEN_PORT:
1152 atomic_clear_mask(ZFCP_STATUS_COMMON_ERP_INUSE,
1153 &erp_action->port->status);
1154 break;
1155
1156 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
1157 atomic_clear_mask(ZFCP_STATUS_COMMON_ERP_INUSE,
1158 &erp_action->adapter->status);
1159 break;
1160 }
1161 }
1162
1163 static void zfcp_erp_action_cleanup(struct zfcp_erp_action *act, int result)
1164 {
1165 struct zfcp_adapter *adapter = act->adapter;
1166 struct zfcp_port *port = act->port;
1167 struct zfcp_unit *unit = act->unit;
1168
1169 switch (act->action) {
1170 case ZFCP_ERP_ACTION_REOPEN_UNIT:
1171 if ((result == ZFCP_ERP_SUCCEEDED) && !unit->device) {
1172 zfcp_unit_get(unit);
1173 if (scsi_queue_work(unit->port->adapter->scsi_host,
1174 &unit->scsi_work) <= 0)
1175 zfcp_unit_put(unit);
1176 }
1177 zfcp_unit_put(unit);
1178 break;
1179
1180 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
1181 case ZFCP_ERP_ACTION_REOPEN_PORT:
1182 if (result == ZFCP_ERP_SUCCEEDED)
1183 zfcp_scsi_schedule_rport_register(port);
1184 zfcp_port_put(port);
1185 break;
1186
1187 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
1188 if (result == ZFCP_ERP_SUCCEEDED) {
1189 register_service_level(&adapter->service_level);
1190 schedule_work(&adapter->scan_work);
1191 } else
1192 unregister_service_level(&adapter->service_level);
1193 zfcp_adapter_put(adapter);
1194 break;
1195 }
1196 }
1197
1198 static int zfcp_erp_strategy_do_action(struct zfcp_erp_action *erp_action)
1199 {
1200 switch (erp_action->action) {
1201 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
1202 return zfcp_erp_adapter_strategy(erp_action);
1203 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
1204 return zfcp_erp_port_forced_strategy(erp_action);
1205 case ZFCP_ERP_ACTION_REOPEN_PORT:
1206 return zfcp_erp_port_strategy(erp_action);
1207 case ZFCP_ERP_ACTION_REOPEN_UNIT:
1208 return zfcp_erp_unit_strategy(erp_action);
1209 }
1210 return ZFCP_ERP_FAILED;
1211 }
1212
1213 static int zfcp_erp_strategy(struct zfcp_erp_action *erp_action)
1214 {
1215 int retval;
1216 struct zfcp_adapter *adapter = erp_action->adapter;
1217 unsigned long flags;
1218
1219 read_lock_irqsave(&zfcp_data.config_lock, flags);
1220 write_lock(&adapter->erp_lock);
1221
1222 zfcp_erp_strategy_check_fsfreq(erp_action);
1223
1224 if (erp_action->status & ZFCP_STATUS_ERP_DISMISSED) {
1225 zfcp_erp_action_dequeue(erp_action);
1226 retval = ZFCP_ERP_DISMISSED;
1227 goto unlock;
1228 }
1229
1230 zfcp_erp_action_to_running(erp_action);
1231
1232 /* no lock to allow for blocking operations */
1233 write_unlock(&adapter->erp_lock);
1234 read_unlock_irqrestore(&zfcp_data.config_lock, flags);
1235 retval = zfcp_erp_strategy_do_action(erp_action);
1236 read_lock_irqsave(&zfcp_data.config_lock, flags);
1237 write_lock(&adapter->erp_lock);
1238
1239 if (erp_action->status & ZFCP_STATUS_ERP_DISMISSED)
1240 retval = ZFCP_ERP_CONTINUES;
1241
1242 switch (retval) {
1243 case ZFCP_ERP_NOMEM:
1244 if (!(erp_action->status & ZFCP_STATUS_ERP_LOWMEM)) {
1245 ++adapter->erp_low_mem_count;
1246 erp_action->status |= ZFCP_STATUS_ERP_LOWMEM;
1247 }
1248 if (adapter->erp_total_count == adapter->erp_low_mem_count)
1249 _zfcp_erp_adapter_reopen(adapter, 0, "erstgy1", NULL);
1250 else {
1251 zfcp_erp_strategy_memwait(erp_action);
1252 retval = ZFCP_ERP_CONTINUES;
1253 }
1254 goto unlock;
1255
1256 case ZFCP_ERP_CONTINUES:
1257 if (erp_action->status & ZFCP_STATUS_ERP_LOWMEM) {
1258 --adapter->erp_low_mem_count;
1259 erp_action->status &= ~ZFCP_STATUS_ERP_LOWMEM;
1260 }
1261 goto unlock;
1262 }
1263
1264 retval = zfcp_erp_strategy_check_target(erp_action, retval);
1265 zfcp_erp_action_dequeue(erp_action);
1266 retval = zfcp_erp_strategy_statechange(erp_action, retval);
1267 if (retval == ZFCP_ERP_EXIT)
1268 goto unlock;
1269 if (retval == ZFCP_ERP_SUCCEEDED)
1270 zfcp_erp_strategy_followup_success(erp_action);
1271 if (retval == ZFCP_ERP_FAILED)
1272 zfcp_erp_strategy_followup_failed(erp_action);
1273
1274 unlock:
1275 write_unlock(&adapter->erp_lock);
1276 read_unlock_irqrestore(&zfcp_data.config_lock, flags);
1277
1278 if (retval != ZFCP_ERP_CONTINUES)
1279 zfcp_erp_action_cleanup(erp_action, retval);
1280
1281 return retval;
1282 }
1283
1284 static int zfcp_erp_thread(void *data)
1285 {
1286 struct zfcp_adapter *adapter = (struct zfcp_adapter *) data;
1287 struct list_head *next;
1288 struct zfcp_erp_action *act;
1289 unsigned long flags;
1290
1291 for (;;) {
1292 zfcp_dbf_rec_thread_lock("erthrd1", adapter->dbf);
1293 wait_event_interruptible(adapter->erp_ready_wq,
1294 !list_empty(&adapter->erp_ready_head) ||
1295 kthread_should_stop());
1296 zfcp_dbf_rec_thread_lock("erthrd2", adapter->dbf);
1297
1298 if (kthread_should_stop())
1299 break;
1300
1301 write_lock_irqsave(&adapter->erp_lock, flags);
1302 next = adapter->erp_ready_head.next;
1303 write_unlock_irqrestore(&adapter->erp_lock, flags);
1304
1305 if (next != &adapter->erp_ready_head) {
1306 act = list_entry(next, struct zfcp_erp_action, list);
1307
1308 /* there is more to come after dismission, no notify */
1309 if (zfcp_erp_strategy(act) != ZFCP_ERP_DISMISSED)
1310 zfcp_erp_wakeup(adapter);
1311 }
1312 }
1313
1314 return 0;
1315 }
1316
1317 /**
1318 * zfcp_erp_thread_setup - Start ERP thread for adapter
1319 * @adapter: Adapter to start the ERP thread for
1320 *
1321 * Returns 0 on success or error code from kernel_thread()
1322 */
1323 int zfcp_erp_thread_setup(struct zfcp_adapter *adapter)
1324 {
1325 struct task_struct *thread;
1326
1327 thread = kthread_run(zfcp_erp_thread, adapter, "zfcperp%s",
1328 dev_name(&adapter->ccw_device->dev));
1329 if (IS_ERR(thread)) {
1330 dev_err(&adapter->ccw_device->dev,
1331 "Creating an ERP thread for the FCP device failed.\n");
1332 return PTR_ERR(thread);
1333 }
1334
1335 adapter->erp_thread = thread;
1336 return 0;
1337 }
1338
1339 /**
1340 * zfcp_erp_thread_kill - Stop ERP thread.
1341 * @adapter: Adapter where the ERP thread should be stopped.
1342 *
1343 * The caller of this routine ensures that the specified adapter has
1344 * been shut down and that this operation has been completed. Thus,
1345 * there are no pending erp_actions which would need to be handled
1346 * here.
1347 */
1348 void zfcp_erp_thread_kill(struct zfcp_adapter *adapter)
1349 {
1350 kthread_stop(adapter->erp_thread);
1351 adapter->erp_thread = NULL;
1352 }
1353
1354 /**
1355 * zfcp_erp_adapter_failed - Set adapter status to failed.
1356 * @adapter: Failed adapter.
1357 * @id: Event id for debug trace.
1358 * @ref: Reference for debug trace.
1359 */
1360 void zfcp_erp_adapter_failed(struct zfcp_adapter *adapter, char *id, void *ref)
1361 {
1362 zfcp_erp_modify_adapter_status(adapter, id, ref,
1363 ZFCP_STATUS_COMMON_ERP_FAILED, ZFCP_SET);
1364 }
1365
1366 /**
1367 * zfcp_erp_port_failed - Set port status to failed.
1368 * @port: Failed port.
1369 * @id: Event id for debug trace.
1370 * @ref: Reference for debug trace.
1371 */
1372 void zfcp_erp_port_failed(struct zfcp_port *port, char *id, void *ref)
1373 {
1374 zfcp_erp_modify_port_status(port, id, ref,
1375 ZFCP_STATUS_COMMON_ERP_FAILED, ZFCP_SET);
1376 }
1377
1378 /**
1379 * zfcp_erp_unit_failed - Set unit status to failed.
1380 * @unit: Failed unit.
1381 * @id: Event id for debug trace.
1382 * @ref: Reference for debug trace.
1383 */
1384 void zfcp_erp_unit_failed(struct zfcp_unit *unit, char *id, void *ref)
1385 {
1386 zfcp_erp_modify_unit_status(unit, id, ref,
1387 ZFCP_STATUS_COMMON_ERP_FAILED, ZFCP_SET);
1388 }
1389
1390 /**
1391 * zfcp_erp_wait - wait for completion of error recovery on an adapter
1392 * @adapter: adapter for which to wait for completion of its error recovery
1393 */
1394 void zfcp_erp_wait(struct zfcp_adapter *adapter)
1395 {
1396 wait_event(adapter->erp_done_wqh,
1397 !(atomic_read(&adapter->status) &
1398 ZFCP_STATUS_ADAPTER_ERP_PENDING));
1399 }
1400
1401 /**
1402 * zfcp_erp_modify_adapter_status - change adapter status bits
1403 * @adapter: adapter to change the status
1404 * @id: id for the debug trace
1405 * @ref: reference for the debug trace
1406 * @mask: status bits to change
1407 * @set_or_clear: ZFCP_SET or ZFCP_CLEAR
1408 *
1409 * Changes in common status bits are propagated to attached ports and units.
1410 */
1411 void zfcp_erp_modify_adapter_status(struct zfcp_adapter *adapter, char *id,
1412 void *ref, u32 mask, int set_or_clear)
1413 {
1414 struct zfcp_port *port;
1415 u32 common_mask = mask & ZFCP_COMMON_FLAGS;
1416
1417 if (set_or_clear == ZFCP_SET) {
1418 if (status_change_set(mask, &adapter->status))
1419 zfcp_dbf_rec_adapter(id, ref, adapter->dbf);
1420 atomic_set_mask(mask, &adapter->status);
1421 } else {
1422 if (status_change_clear(mask, &adapter->status))
1423 zfcp_dbf_rec_adapter(id, ref, adapter->dbf);
1424 atomic_clear_mask(mask, &adapter->status);
1425 if (mask & ZFCP_STATUS_COMMON_ERP_FAILED)
1426 atomic_set(&adapter->erp_counter, 0);
1427 }
1428
1429 if (common_mask)
1430 list_for_each_entry(port, &adapter->port_list_head, list)
1431 zfcp_erp_modify_port_status(port, id, ref, common_mask,
1432 set_or_clear);
1433 }
1434
1435 /**
1436 * zfcp_erp_modify_port_status - change port status bits
1437 * @port: port to change the status bits
1438 * @id: id for the debug trace
1439 * @ref: reference for the debug trace
1440 * @mask: status bits to change
1441 * @set_or_clear: ZFCP_SET or ZFCP_CLEAR
1442 *
1443 * Changes in common status bits are propagated to attached units.
1444 */
1445 void zfcp_erp_modify_port_status(struct zfcp_port *port, char *id, void *ref,
1446 u32 mask, int set_or_clear)
1447 {
1448 struct zfcp_unit *unit;
1449 u32 common_mask = mask & ZFCP_COMMON_FLAGS;
1450
1451 if (set_or_clear == ZFCP_SET) {
1452 if (status_change_set(mask, &port->status))
1453 zfcp_dbf_rec_port(id, ref, port);
1454 atomic_set_mask(mask, &port->status);
1455 } else {
1456 if (status_change_clear(mask, &port->status))
1457 zfcp_dbf_rec_port(id, ref, port);
1458 atomic_clear_mask(mask, &port->status);
1459 if (mask & ZFCP_STATUS_COMMON_ERP_FAILED)
1460 atomic_set(&port->erp_counter, 0);
1461 }
1462
1463 if (common_mask)
1464 list_for_each_entry(unit, &port->unit_list_head, list)
1465 zfcp_erp_modify_unit_status(unit, id, ref, common_mask,
1466 set_or_clear);
1467 }
1468
1469 /**
1470 * zfcp_erp_modify_unit_status - change unit status bits
1471 * @unit: unit to change the status bits
1472 * @id: id for the debug trace
1473 * @ref: reference for the debug trace
1474 * @mask: status bits to change
1475 * @set_or_clear: ZFCP_SET or ZFCP_CLEAR
1476 */
1477 void zfcp_erp_modify_unit_status(struct zfcp_unit *unit, char *id, void *ref,
1478 u32 mask, int set_or_clear)
1479 {
1480 if (set_or_clear == ZFCP_SET) {
1481 if (status_change_set(mask, &unit->status))
1482 zfcp_dbf_rec_unit(id, ref, unit);
1483 atomic_set_mask(mask, &unit->status);
1484 } else {
1485 if (status_change_clear(mask, &unit->status))
1486 zfcp_dbf_rec_unit(id, ref, unit);
1487 atomic_clear_mask(mask, &unit->status);
1488 if (mask & ZFCP_STATUS_COMMON_ERP_FAILED) {
1489 atomic_set(&unit->erp_counter, 0);
1490 }
1491 }
1492 }
1493
1494 /**
1495 * zfcp_erp_port_boxed - Mark port as "boxed" and start ERP
1496 * @port: The "boxed" port.
1497 * @id: The debug trace id.
1498 * @id: Reference for the debug trace.
1499 */
1500 void zfcp_erp_port_boxed(struct zfcp_port *port, char *id, void *ref)
1501 {
1502 unsigned long flags;
1503
1504 read_lock_irqsave(&zfcp_data.config_lock, flags);
1505 zfcp_erp_modify_port_status(port, id, ref,
1506 ZFCP_STATUS_COMMON_ACCESS_BOXED, ZFCP_SET);
1507 read_unlock_irqrestore(&zfcp_data.config_lock, flags);
1508 zfcp_erp_port_reopen(port, ZFCP_STATUS_COMMON_ERP_FAILED, id, ref);
1509 }
1510
1511 /**
1512 * zfcp_erp_unit_boxed - Mark unit as "boxed" and start ERP
1513 * @port: The "boxed" unit.
1514 * @id: The debug trace id.
1515 * @id: Reference for the debug trace.
1516 */
1517 void zfcp_erp_unit_boxed(struct zfcp_unit *unit, char *id, void *ref)
1518 {
1519 zfcp_erp_modify_unit_status(unit, id, ref,
1520 ZFCP_STATUS_COMMON_ACCESS_BOXED, ZFCP_SET);
1521 zfcp_erp_unit_reopen(unit, ZFCP_STATUS_COMMON_ERP_FAILED, id, ref);
1522 }
1523
1524 /**
1525 * zfcp_erp_port_access_denied - Adapter denied access to port.
1526 * @port: port where access has been denied
1527 * @id: id for debug trace
1528 * @ref: reference for debug trace
1529 *
1530 * Since the adapter has denied access, stop using the port and the
1531 * attached units.
1532 */
1533 void zfcp_erp_port_access_denied(struct zfcp_port *port, char *id, void *ref)
1534 {
1535 unsigned long flags;
1536
1537 read_lock_irqsave(&zfcp_data.config_lock, flags);
1538 zfcp_erp_modify_port_status(port, id, ref,
1539 ZFCP_STATUS_COMMON_ERP_FAILED |
1540 ZFCP_STATUS_COMMON_ACCESS_DENIED, ZFCP_SET);
1541 read_unlock_irqrestore(&zfcp_data.config_lock, flags);
1542 }
1543
1544 /**
1545 * zfcp_erp_unit_access_denied - Adapter denied access to unit.
1546 * @unit: unit where access has been denied
1547 * @id: id for debug trace
1548 * @ref: reference for debug trace
1549 *
1550 * Since the adapter has denied access, stop using the unit.
1551 */
1552 void zfcp_erp_unit_access_denied(struct zfcp_unit *unit, char *id, void *ref)
1553 {
1554 zfcp_erp_modify_unit_status(unit, id, ref,
1555 ZFCP_STATUS_COMMON_ERP_FAILED |
1556 ZFCP_STATUS_COMMON_ACCESS_DENIED, ZFCP_SET);
1557 }
1558
1559 static void zfcp_erp_unit_access_changed(struct zfcp_unit *unit, char *id,
1560 void *ref)
1561 {
1562 int status = atomic_read(&unit->status);
1563 if (!(status & (ZFCP_STATUS_COMMON_ACCESS_DENIED |
1564 ZFCP_STATUS_COMMON_ACCESS_BOXED)))
1565 return;
1566
1567 zfcp_erp_unit_reopen(unit, ZFCP_STATUS_COMMON_ERP_FAILED, id, ref);
1568 }
1569
1570 static void zfcp_erp_port_access_changed(struct zfcp_port *port, char *id,
1571 void *ref)
1572 {
1573 struct zfcp_unit *unit;
1574 int status = atomic_read(&port->status);
1575
1576 if (!(status & (ZFCP_STATUS_COMMON_ACCESS_DENIED |
1577 ZFCP_STATUS_COMMON_ACCESS_BOXED))) {
1578 list_for_each_entry(unit, &port->unit_list_head, list)
1579 zfcp_erp_unit_access_changed(unit, id, ref);
1580 return;
1581 }
1582
1583 zfcp_erp_port_reopen(port, ZFCP_STATUS_COMMON_ERP_FAILED, id, ref);
1584 }
1585
1586 /**
1587 * zfcp_erp_adapter_access_changed - Process change in adapter ACT
1588 * @adapter: Adapter where the Access Control Table (ACT) changed
1589 * @id: Id for debug trace
1590 * @ref: Reference for debug trace
1591 */
1592 void zfcp_erp_adapter_access_changed(struct zfcp_adapter *adapter, char *id,
1593 void *ref)
1594 {
1595 struct zfcp_port *port;
1596 unsigned long flags;
1597
1598 if (adapter->connection_features & FSF_FEATURE_NPIV_MODE)
1599 return;
1600
1601 read_lock_irqsave(&zfcp_data.config_lock, flags);
1602 list_for_each_entry(port, &adapter->port_list_head, list)
1603 zfcp_erp_port_access_changed(port, id, ref);
1604 read_unlock_irqrestore(&zfcp_data.config_lock, flags);
1605 }
This page took 0.074317 seconds and 5 git commands to generate.