blob: ee19be13e708f1926d56e1c1c68a6863bbc7abff [file] [log] [blame]
Swen Schillig41fa2ad2007-09-07 09:15:31 +02001/*
Andreas Herrmann4a9d2d82006-05-22 18:14:08 +02002 * This file is part of the zfcp device driver for
3 * FCP adapters for IBM System z9 and zSeries.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Andreas Herrmann4a9d2d82006-05-22 18:14:08 +02005 * (C) Copyright IBM Corp. 2002, 2006
Swen Schillig41fa2ad2007-09-07 09:15:31 +02006 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2, or (at your option)
10 * any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
Linus Torvalds1da177e2005-04-16 15:20:36 -070020 */
21
22#define ZFCP_LOG_AREA ZFCP_LOG_AREA_ERP
23
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include "zfcp_ext.h"
25
Martin Peschke9467a9b2008-03-27 14:22:03 +010026static int zfcp_erp_adapter_reopen_internal(struct zfcp_adapter *, int, u8,
Martin Peschke1f6f7122008-04-18 12:51:55 +020027 void *);
Martin Peschke9467a9b2008-03-27 14:22:03 +010028static int zfcp_erp_port_forced_reopen_internal(struct zfcp_port *, int, u8,
Martin Peschke1f6f7122008-04-18 12:51:55 +020029 void *);
30static int zfcp_erp_port_reopen_internal(struct zfcp_port *, int, u8, void *);
31static int zfcp_erp_unit_reopen_internal(struct zfcp_unit *, int, u8, void *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
Martin Peschke9467a9b2008-03-27 14:22:03 +010033static int zfcp_erp_port_reopen_all_internal(struct zfcp_adapter *, int, u8,
Martin Peschke1f6f7122008-04-18 12:51:55 +020034 void *);
35static int zfcp_erp_unit_reopen_all_internal(struct zfcp_port *, int, u8,
36 void *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38static void zfcp_erp_adapter_block(struct zfcp_adapter *, int);
39static void zfcp_erp_adapter_unblock(struct zfcp_adapter *);
40static void zfcp_erp_port_block(struct zfcp_port *, int);
41static void zfcp_erp_port_unblock(struct zfcp_port *);
42static void zfcp_erp_unit_block(struct zfcp_unit *, int);
43static void zfcp_erp_unit_unblock(struct zfcp_unit *);
44
45static int zfcp_erp_thread(void *);
46
47static int zfcp_erp_strategy(struct zfcp_erp_action *);
48
49static int zfcp_erp_strategy_do_action(struct zfcp_erp_action *);
50static int zfcp_erp_strategy_memwait(struct zfcp_erp_action *);
51static int zfcp_erp_strategy_check_target(struct zfcp_erp_action *, int);
52static int zfcp_erp_strategy_check_unit(struct zfcp_unit *, int);
53static int zfcp_erp_strategy_check_port(struct zfcp_port *, int);
54static int zfcp_erp_strategy_check_adapter(struct zfcp_adapter *, int);
55static int zfcp_erp_strategy_statechange(int, u32, struct zfcp_adapter *,
56 struct zfcp_port *,
57 struct zfcp_unit *, int);
Heiko Carstens364c8552007-10-12 16:11:35 +020058static int zfcp_erp_strategy_statechange_detected(atomic_t *, u32);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059static int zfcp_erp_strategy_followup_actions(int, struct zfcp_adapter *,
60 struct zfcp_port *,
61 struct zfcp_unit *, int);
62static int zfcp_erp_strategy_check_queues(struct zfcp_adapter *);
63static int zfcp_erp_strategy_check_action(struct zfcp_erp_action *, int);
64
65static int zfcp_erp_adapter_strategy(struct zfcp_erp_action *);
66static int zfcp_erp_adapter_strategy_generic(struct zfcp_erp_action *, int);
67static int zfcp_erp_adapter_strategy_close(struct zfcp_erp_action *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068static int zfcp_erp_adapter_strategy_open(struct zfcp_erp_action *);
69static int zfcp_erp_adapter_strategy_open_qdio(struct zfcp_erp_action *);
70static int zfcp_erp_adapter_strategy_open_fsf(struct zfcp_erp_action *);
71static int zfcp_erp_adapter_strategy_open_fsf_xconfig(struct zfcp_erp_action *);
Maxim Shchetyninaef4a982005-09-13 21:51:16 +020072static int zfcp_erp_adapter_strategy_open_fsf_xport(struct zfcp_erp_action *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073static int zfcp_erp_adapter_strategy_open_fsf_statusread(
74 struct zfcp_erp_action *);
75
76static int zfcp_erp_port_forced_strategy(struct zfcp_erp_action *);
77static int zfcp_erp_port_forced_strategy_close(struct zfcp_erp_action *);
78
79static int zfcp_erp_port_strategy(struct zfcp_erp_action *);
80static int zfcp_erp_port_strategy_clearstati(struct zfcp_port *);
81static int zfcp_erp_port_strategy_close(struct zfcp_erp_action *);
82static int zfcp_erp_port_strategy_open(struct zfcp_erp_action *);
83static int zfcp_erp_port_strategy_open_nameserver(struct zfcp_erp_action *);
84static int zfcp_erp_port_strategy_open_nameserver_wakeup(
85 struct zfcp_erp_action *);
86static int zfcp_erp_port_strategy_open_common(struct zfcp_erp_action *);
87static int zfcp_erp_port_strategy_open_common_lookup(struct zfcp_erp_action *);
88static int zfcp_erp_port_strategy_open_port(struct zfcp_erp_action *);
89
90static int zfcp_erp_unit_strategy(struct zfcp_erp_action *);
91static int zfcp_erp_unit_strategy_clearstati(struct zfcp_unit *);
92static int zfcp_erp_unit_strategy_close(struct zfcp_erp_action *);
93static int zfcp_erp_unit_strategy_open(struct zfcp_erp_action *);
94
Andreas Herrmann81654282006-09-18 22:30:36 +020095static void zfcp_erp_action_dismiss_adapter(struct zfcp_adapter *);
Andreas Herrmannf6c0e7a2006-08-02 11:05:52 +020096static void zfcp_erp_action_dismiss_port(struct zfcp_port *);
97static void zfcp_erp_action_dismiss_unit(struct zfcp_unit *);
98static void zfcp_erp_action_dismiss(struct zfcp_erp_action *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
100static int zfcp_erp_action_enqueue(int, struct zfcp_adapter *,
Martin Peschke9467a9b2008-03-27 14:22:03 +0100101 struct zfcp_port *, struct zfcp_unit *,
Martin Peschke1f6f7122008-04-18 12:51:55 +0200102 u8 id, void *ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103static int zfcp_erp_action_dequeue(struct zfcp_erp_action *);
104static void zfcp_erp_action_cleanup(int, struct zfcp_adapter *,
105 struct zfcp_port *, struct zfcp_unit *,
106 int);
107
108static void zfcp_erp_action_ready(struct zfcp_erp_action *);
109static int zfcp_erp_action_exists(struct zfcp_erp_action *);
110
Heiko Carstens364c8552007-10-12 16:11:35 +0200111static void zfcp_erp_action_to_ready(struct zfcp_erp_action *);
112static void zfcp_erp_action_to_running(struct zfcp_erp_action *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
114static void zfcp_erp_memwait_handler(unsigned long);
Andreas Herrmann2abbe862006-09-18 22:29:56 +0200115
116/**
117 * zfcp_close_qdio - close qdio queues for an adapter
118 */
119static void zfcp_close_qdio(struct zfcp_adapter *adapter)
120{
121 struct zfcp_qdio_queue *req_queue;
122 int first, count;
123
124 if (!atomic_test_mask(ZFCP_STATUS_ADAPTER_QDIOUP, &adapter->status))
125 return;
126
127 /* clear QDIOUP flag, thus do_QDIO is not called during qdio_shutdown */
128 req_queue = &adapter->request_queue;
129 write_lock_irq(&req_queue->queue_lock);
130 atomic_clear_mask(ZFCP_STATUS_ADAPTER_QDIOUP, &adapter->status);
131 write_unlock_irq(&req_queue->queue_lock);
132
Andreas Herrmann2abbe862006-09-18 22:29:56 +0200133 while (qdio_shutdown(adapter->ccw_device,
134 QDIO_FLAG_CLEANUP_USING_CLEAR) == -EINPROGRESS)
Christof Schmitt18edcdb2007-11-05 12:37:45 +0100135 ssleep(1);
Andreas Herrmann2abbe862006-09-18 22:29:56 +0200136
137 /* cleanup used outbound sbals */
138 count = atomic_read(&req_queue->free_count);
139 if (count < QDIO_MAX_BUFFERS_PER_Q) {
140 first = (req_queue->free_index+count) % QDIO_MAX_BUFFERS_PER_Q;
141 count = QDIO_MAX_BUFFERS_PER_Q - count;
142 zfcp_qdio_zero_sbals(req_queue->buffer, first, count);
143 }
144 req_queue->free_index = 0;
145 atomic_set(&req_queue->free_count, 0);
146 req_queue->distance_from_int = 0;
147 adapter->response_queue.free_index = 0;
148 atomic_set(&adapter->response_queue.free_count, 0);
149}
150
151/**
152 * zfcp_close_fsf - stop FSF operations for an adapter
153 *
154 * Dismiss and cleanup all pending fsf_reqs (this wakes up all initiators of
155 * requests waiting for completion; especially this returns SCSI commands
156 * with error state).
157 */
158static void zfcp_close_fsf(struct zfcp_adapter *adapter)
159{
160 /* close queues to ensure that buffers are not accessed by adapter */
161 zfcp_close_qdio(adapter);
162 zfcp_fsf_req_dismiss_all(adapter);
163 /* reset FSF request sequence number */
164 adapter->fsf_req_seq_no = 0;
165 /* all ports and units are closed */
Martin Peschke1f6f7122008-04-18 12:51:55 +0200166 zfcp_erp_modify_adapter_status(adapter, 24, NULL,
Andreas Herrmann2abbe862006-09-18 22:29:56 +0200167 ZFCP_STATUS_COMMON_OPEN, ZFCP_CLEAR);
168}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
170/**
171 * zfcp_fsf_request_timeout_handler - called if a request timed out
172 * @data: pointer to adapter for handler function
173 *
174 * This function needs to be called if requests (ELS, Generic Service,
175 * or SCSI commands) exceed a certain time limit. The assumption is
176 * that after the time limit the adapter get stuck. So we trigger a reopen of
Andreas Herrmann2abbe862006-09-18 22:29:56 +0200177 * the adapter.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 */
Andreas Herrmann2abbe862006-09-18 22:29:56 +0200179static void zfcp_fsf_request_timeout_handler(unsigned long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180{
Andreas Herrmann2abbe862006-09-18 22:29:56 +0200181 struct zfcp_adapter *adapter = (struct zfcp_adapter *) data;
Martin Peschke1f6f7122008-04-18 12:51:55 +0200182 zfcp_erp_adapter_reopen(adapter, ZFCP_STATUS_COMMON_ERP_FAILED, 62,
183 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184}
185
Andreas Herrmann2abbe862006-09-18 22:29:56 +0200186void zfcp_fsf_start_timer(struct zfcp_fsf_req *fsf_req, unsigned long timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187{
Andreas Herrmann2abbe862006-09-18 22:29:56 +0200188 fsf_req->timer.function = zfcp_fsf_request_timeout_handler;
189 fsf_req->timer.data = (unsigned long) fsf_req->adapter;
Christof Schmitt9e3738f2007-03-28 14:20:40 +0200190 fsf_req->timer.expires = jiffies + timeout;
Andreas Herrmann2abbe862006-09-18 22:29:56 +0200191 add_timer(&fsf_req->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192}
193
194/*
Swen Schillig41fa2ad2007-09-07 09:15:31 +0200195 * function:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 *
197 * purpose: called if an adapter failed,
198 * initiates adapter recovery which is done
199 * asynchronously
200 *
Andreas Mohrd6e05ed2006-06-26 18:35:02 +0200201 * returns: 0 - initiated action successfully
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 * <0 - failed to initiate action
203 */
Martin Peschke9467a9b2008-03-27 14:22:03 +0100204static int zfcp_erp_adapter_reopen_internal(struct zfcp_adapter *adapter,
Martin Peschke1f6f7122008-04-18 12:51:55 +0200205 int clear_mask, u8 id, void *ref)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206{
207 int retval;
208
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 ZFCP_LOG_DEBUG("reopen adapter %s\n",
210 zfcp_get_busid_by_adapter(adapter));
211
212 zfcp_erp_adapter_block(adapter, clear_mask);
213
214 if (atomic_test_mask(ZFCP_STATUS_COMMON_ERP_FAILED, &adapter->status)) {
215 ZFCP_LOG_DEBUG("skipped reopen of failed adapter %s\n",
216 zfcp_get_busid_by_adapter(adapter));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 /* ensure propagation of failed status to new devices */
Martin Peschke1f6f7122008-04-18 12:51:55 +0200218 zfcp_erp_adapter_failed(adapter, 13, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 retval = -EIO;
220 goto out;
221 }
222 retval = zfcp_erp_action_enqueue(ZFCP_ERP_ACTION_REOPEN_ADAPTER,
Martin Peschke9467a9b2008-03-27 14:22:03 +0100223 adapter, NULL, NULL, id, ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
225 out:
226 return retval;
227}
228
229/*
Swen Schillig41fa2ad2007-09-07 09:15:31 +0200230 * function:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 *
232 * purpose: Wrappper for zfcp_erp_adapter_reopen_internal
233 * used to ensure the correct locking
234 *
Andreas Mohrd6e05ed2006-06-26 18:35:02 +0200235 * returns: 0 - initiated action successfully
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 * <0 - failed to initiate action
237 */
Martin Peschke9467a9b2008-03-27 14:22:03 +0100238int zfcp_erp_adapter_reopen(struct zfcp_adapter *adapter, int clear_mask,
Martin Peschke1f6f7122008-04-18 12:51:55 +0200239 u8 id, void *ref)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240{
241 int retval;
242 unsigned long flags;
243
244 read_lock_irqsave(&zfcp_data.config_lock, flags);
245 write_lock(&adapter->erp_lock);
Martin Peschke9467a9b2008-03-27 14:22:03 +0100246 retval = zfcp_erp_adapter_reopen_internal(adapter, clear_mask, id, ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 write_unlock(&adapter->erp_lock);
248 read_unlock_irqrestore(&zfcp_data.config_lock, flags);
249
250 return retval;
251}
252
Martin Peschke9467a9b2008-03-27 14:22:03 +0100253int zfcp_erp_adapter_shutdown(struct zfcp_adapter *adapter, int clear_mask,
Martin Peschke1f6f7122008-04-18 12:51:55 +0200254 u8 id, void *ref)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255{
256 int retval;
257
258 retval = zfcp_erp_adapter_reopen(adapter,
259 ZFCP_STATUS_COMMON_RUNNING |
260 ZFCP_STATUS_COMMON_ERP_FAILED |
Martin Peschke9467a9b2008-03-27 14:22:03 +0100261 clear_mask, id, ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
263 return retval;
264}
265
Martin Peschke9467a9b2008-03-27 14:22:03 +0100266int zfcp_erp_port_shutdown(struct zfcp_port *port, int clear_mask, u8 id,
Martin Peschke1f6f7122008-04-18 12:51:55 +0200267 void *ref)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268{
269 int retval;
270
271 retval = zfcp_erp_port_reopen(port,
272 ZFCP_STATUS_COMMON_RUNNING |
273 ZFCP_STATUS_COMMON_ERP_FAILED |
Martin Peschke9467a9b2008-03-27 14:22:03 +0100274 clear_mask, id, ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
276 return retval;
277}
278
Martin Peschke9467a9b2008-03-27 14:22:03 +0100279int zfcp_erp_unit_shutdown(struct zfcp_unit *unit, int clear_mask, u8 id,
Martin Peschke1f6f7122008-04-18 12:51:55 +0200280 void *ref)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281{
282 int retval;
283
284 retval = zfcp_erp_unit_reopen(unit,
285 ZFCP_STATUS_COMMON_RUNNING |
286 ZFCP_STATUS_COMMON_ERP_FAILED |
Martin Peschke9467a9b2008-03-27 14:22:03 +0100287 clear_mask, id, ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
289 return retval;
290}
291
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292/*
Swen Schillig41fa2ad2007-09-07 09:15:31 +0200293 * function:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 *
295 * purpose: called if a port failed to be opened normally
296 * initiates Forced Reopen recovery which is done
297 * asynchronously
298 *
Andreas Mohrd6e05ed2006-06-26 18:35:02 +0200299 * returns: 0 - initiated action successfully
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 * <0 - failed to initiate action
301 */
Martin Peschke9467a9b2008-03-27 14:22:03 +0100302static int zfcp_erp_port_forced_reopen_internal(struct zfcp_port *port,
Martin Peschke1f6f7122008-04-18 12:51:55 +0200303 int clear_mask, u8 id,
304 void *ref)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305{
306 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
308 ZFCP_LOG_DEBUG("forced reopen of port 0x%016Lx on adapter %s\n",
309 port->wwpn, zfcp_get_busid_by_port(port));
310
311 zfcp_erp_port_block(port, clear_mask);
312
313 if (atomic_test_mask(ZFCP_STATUS_COMMON_ERP_FAILED, &port->status)) {
314 ZFCP_LOG_DEBUG("skipped forced reopen of failed port 0x%016Lx "
315 "on adapter %s\n", port->wwpn,
316 zfcp_get_busid_by_port(port));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 retval = -EIO;
318 goto out;
319 }
320
321 retval = zfcp_erp_action_enqueue(ZFCP_ERP_ACTION_REOPEN_PORT_FORCED,
Martin Peschke9467a9b2008-03-27 14:22:03 +0100322 port->adapter, port, NULL, id, ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
324 out:
325 return retval;
326}
327
328/*
Swen Schillig41fa2ad2007-09-07 09:15:31 +0200329 * function:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 *
331 * purpose: Wrappper for zfcp_erp_port_forced_reopen_internal
332 * used to ensure the correct locking
333 *
Andreas Mohrd6e05ed2006-06-26 18:35:02 +0200334 * returns: 0 - initiated action successfully
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 * <0 - failed to initiate action
336 */
Martin Peschke9467a9b2008-03-27 14:22:03 +0100337int zfcp_erp_port_forced_reopen(struct zfcp_port *port, int clear_mask, u8 id,
Martin Peschke1f6f7122008-04-18 12:51:55 +0200338 void *ref)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339{
340 int retval;
341 unsigned long flags;
342 struct zfcp_adapter *adapter;
343
344 adapter = port->adapter;
345 read_lock_irqsave(&zfcp_data.config_lock, flags);
346 write_lock(&adapter->erp_lock);
Martin Peschke9467a9b2008-03-27 14:22:03 +0100347 retval = zfcp_erp_port_forced_reopen_internal(port, clear_mask, id,
348 ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 write_unlock(&adapter->erp_lock);
350 read_unlock_irqrestore(&zfcp_data.config_lock, flags);
351
352 return retval;
353}
354
355/*
Swen Schillig41fa2ad2007-09-07 09:15:31 +0200356 * function:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 *
358 * purpose: called if a port is to be opened
359 * initiates Reopen recovery which is done
360 * asynchronously
361 *
Andreas Mohrd6e05ed2006-06-26 18:35:02 +0200362 * returns: 0 - initiated action successfully
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 * <0 - failed to initiate action
364 */
Martin Peschke9467a9b2008-03-27 14:22:03 +0100365static int zfcp_erp_port_reopen_internal(struct zfcp_port *port, int clear_mask,
Martin Peschke1f6f7122008-04-18 12:51:55 +0200366 u8 id, void *ref)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367{
368 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
370 ZFCP_LOG_DEBUG("reopen of port 0x%016Lx on adapter %s\n",
371 port->wwpn, zfcp_get_busid_by_port(port));
372
373 zfcp_erp_port_block(port, clear_mask);
374
375 if (atomic_test_mask(ZFCP_STATUS_COMMON_ERP_FAILED, &port->status)) {
376 ZFCP_LOG_DEBUG("skipped reopen of failed port 0x%016Lx "
377 "on adapter %s\n", port->wwpn,
378 zfcp_get_busid_by_port(port));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 /* ensure propagation of failed status to new devices */
Martin Peschke1f6f7122008-04-18 12:51:55 +0200380 zfcp_erp_port_failed(port, 14, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 retval = -EIO;
382 goto out;
383 }
384
385 retval = zfcp_erp_action_enqueue(ZFCP_ERP_ACTION_REOPEN_PORT,
Martin Peschke9467a9b2008-03-27 14:22:03 +0100386 port->adapter, port, NULL, id, ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
388 out:
389 return retval;
390}
391
392/**
393 * zfcp_erp_port_reopen - initiate reopen of a remote port
394 * @port: port to be reopened
395 * @clear_mask: specifies flags in port status to be cleared
396 * Return: 0 on success, < 0 on error
397 *
398 * This is a wrappper function for zfcp_erp_port_reopen_internal. It ensures
399 * correct locking. An error recovery task is initiated to do the reopen.
400 * To wait for the completion of the reopen zfcp_erp_wait should be used.
401 */
Martin Peschke1f6f7122008-04-18 12:51:55 +0200402int zfcp_erp_port_reopen(struct zfcp_port *port, int clear_mask, u8 id,
403 void *ref)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404{
405 int retval;
406 unsigned long flags;
407 struct zfcp_adapter *adapter = port->adapter;
408
409 read_lock_irqsave(&zfcp_data.config_lock, flags);
410 write_lock(&adapter->erp_lock);
Martin Peschke9467a9b2008-03-27 14:22:03 +0100411 retval = zfcp_erp_port_reopen_internal(port, clear_mask, id, ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 write_unlock(&adapter->erp_lock);
413 read_unlock_irqrestore(&zfcp_data.config_lock, flags);
414
415 return retval;
416}
417
418/*
Swen Schillig41fa2ad2007-09-07 09:15:31 +0200419 * function:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 *
421 * purpose: called if a unit is to be opened
422 * initiates Reopen recovery which is done
423 * asynchronously
424 *
Andreas Mohrd6e05ed2006-06-26 18:35:02 +0200425 * returns: 0 - initiated action successfully
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 * <0 - failed to initiate action
427 */
Martin Peschke9467a9b2008-03-27 14:22:03 +0100428static int zfcp_erp_unit_reopen_internal(struct zfcp_unit *unit, int clear_mask,
Martin Peschke1f6f7122008-04-18 12:51:55 +0200429 u8 id, void *ref)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430{
431 int retval;
432 struct zfcp_adapter *adapter = unit->port->adapter;
433
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 ZFCP_LOG_DEBUG("reopen of unit 0x%016Lx on port 0x%016Lx "
435 "on adapter %s\n", unit->fcp_lun,
436 unit->port->wwpn, zfcp_get_busid_by_unit(unit));
437
438 zfcp_erp_unit_block(unit, clear_mask);
439
440 if (atomic_test_mask(ZFCP_STATUS_COMMON_ERP_FAILED, &unit->status)) {
441 ZFCP_LOG_DEBUG("skipped reopen of failed unit 0x%016Lx "
442 "on port 0x%016Lx on adapter %s\n",
443 unit->fcp_lun, unit->port->wwpn,
444 zfcp_get_busid_by_unit(unit));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 retval = -EIO;
446 goto out;
447 }
448
449 retval = zfcp_erp_action_enqueue(ZFCP_ERP_ACTION_REOPEN_UNIT,
Martin Peschke9467a9b2008-03-27 14:22:03 +0100450 adapter, unit->port, unit, id, ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 out:
452 return retval;
453}
454
455/**
456 * zfcp_erp_unit_reopen - initiate reopen of a unit
457 * @unit: unit to be reopened
458 * @clear_mask: specifies flags in unit status to be cleared
459 * Return: 0 on success, < 0 on error
460 *
461 * This is a wrappper for zfcp_erp_unit_reopen_internal. It ensures correct
462 * locking. An error recovery task is initiated to do the reopen.
463 * To wait for the completion of the reopen zfcp_erp_wait should be used.
464 */
Martin Peschke1f6f7122008-04-18 12:51:55 +0200465int zfcp_erp_unit_reopen(struct zfcp_unit *unit, int clear_mask, u8 id,
466 void *ref)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467{
468 int retval;
469 unsigned long flags;
470 struct zfcp_adapter *adapter;
471 struct zfcp_port *port;
472
473 port = unit->port;
474 adapter = port->adapter;
475
476 read_lock_irqsave(&zfcp_data.config_lock, flags);
477 write_lock(&adapter->erp_lock);
Martin Peschke9467a9b2008-03-27 14:22:03 +0100478 retval = zfcp_erp_unit_reopen_internal(unit, clear_mask, id, ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 write_unlock(&adapter->erp_lock);
480 read_unlock_irqrestore(&zfcp_data.config_lock, flags);
481
482 return retval;
483}
484
Andreas Herrmannf6c0e7a2006-08-02 11:05:52 +0200485/**
486 * zfcp_erp_adapter_block - mark adapter as blocked, block scsi requests
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 */
Andreas Herrmannf6c0e7a2006-08-02 11:05:52 +0200488static void zfcp_erp_adapter_block(struct zfcp_adapter *adapter, int clear_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489{
Martin Peschke1f6f7122008-04-18 12:51:55 +0200490 zfcp_erp_modify_adapter_status(adapter, 15, NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 ZFCP_STATUS_COMMON_UNBLOCKED |
492 clear_mask, ZFCP_CLEAR);
493}
494
Martin Peschke698ec0162008-03-27 14:22:02 +0100495/* FIXME: isn't really atomic */
496/*
497 * returns the mask which has not been set so far, i.e.
498 * 0 if no bit has been changed, !0 if some bit has been changed
499 */
500static int atomic_test_and_set_mask(unsigned long mask, atomic_t *v)
501{
502 int changed_bits = (atomic_read(v) /*XOR*/^ mask) & mask;
503 atomic_set_mask(mask, v);
504 return changed_bits;
505}
506
507/* FIXME: isn't really atomic */
508/*
509 * returns the mask which has not been cleared so far, i.e.
510 * 0 if no bit has been changed, !0 if some bit has been changed
511 */
512static int atomic_test_and_clear_mask(unsigned long mask, atomic_t *v)
513{
514 int changed_bits = atomic_read(v) & mask;
515 atomic_clear_mask(mask, v);
516 return changed_bits;
517}
518
Andreas Herrmannf6c0e7a2006-08-02 11:05:52 +0200519/**
520 * zfcp_erp_adapter_unblock - mark adapter as unblocked, allow scsi requests
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 */
Andreas Herrmannf6c0e7a2006-08-02 11:05:52 +0200522static void zfcp_erp_adapter_unblock(struct zfcp_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523{
Martin Peschke698ec0162008-03-27 14:22:02 +0100524 if (atomic_test_and_set_mask(ZFCP_STATUS_COMMON_UNBLOCKED,
525 &adapter->status))
Martin Peschke1f6f7122008-04-18 12:51:55 +0200526 zfcp_rec_dbf_event_adapter(16, NULL, adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527}
528
529/*
Swen Schillig41fa2ad2007-09-07 09:15:31 +0200530 * function:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 *
532 * purpose: disable I/O,
533 * return any open requests and clean them up,
534 * aim: no pending and incoming I/O
535 *
536 * returns:
537 */
538static void
539zfcp_erp_port_block(struct zfcp_port *port, int clear_mask)
540{
Martin Peschke1f6f7122008-04-18 12:51:55 +0200541 zfcp_erp_modify_port_status(port, 17, NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 ZFCP_STATUS_COMMON_UNBLOCKED | clear_mask,
543 ZFCP_CLEAR);
544}
545
546/*
Swen Schillig41fa2ad2007-09-07 09:15:31 +0200547 * function:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 *
549 * purpose: enable I/O
550 *
551 * returns:
552 */
553static void
554zfcp_erp_port_unblock(struct zfcp_port *port)
555{
Martin Peschke698ec0162008-03-27 14:22:02 +0100556 if (atomic_test_and_set_mask(ZFCP_STATUS_COMMON_UNBLOCKED,
557 &port->status))
Martin Peschke1f6f7122008-04-18 12:51:55 +0200558 zfcp_rec_dbf_event_port(18, NULL, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559}
560
561/*
Swen Schillig41fa2ad2007-09-07 09:15:31 +0200562 * function:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 *
564 * purpose: disable I/O,
565 * return any open requests and clean them up,
566 * aim: no pending and incoming I/O
567 *
568 * returns:
569 */
570static void
571zfcp_erp_unit_block(struct zfcp_unit *unit, int clear_mask)
572{
Martin Peschke1f6f7122008-04-18 12:51:55 +0200573 zfcp_erp_modify_unit_status(unit, 19, NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 ZFCP_STATUS_COMMON_UNBLOCKED | clear_mask,
575 ZFCP_CLEAR);
576}
577
578/*
Swen Schillig41fa2ad2007-09-07 09:15:31 +0200579 * function:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 *
581 * purpose: enable I/O
582 *
583 * returns:
584 */
585static void
586zfcp_erp_unit_unblock(struct zfcp_unit *unit)
587{
Martin Peschke698ec0162008-03-27 14:22:02 +0100588 if (atomic_test_and_set_mask(ZFCP_STATUS_COMMON_UNBLOCKED,
589 &unit->status))
Martin Peschke1f6f7122008-04-18 12:51:55 +0200590 zfcp_rec_dbf_event_unit(20, NULL, unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591}
592
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593static void
594zfcp_erp_action_ready(struct zfcp_erp_action *erp_action)
595{
596 struct zfcp_adapter *adapter = erp_action->adapter;
597
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 zfcp_erp_action_to_ready(erp_action);
599 up(&adapter->erp_ready_sem);
Christof Schmittaa0fec62008-05-19 12:17:47 +0200600 zfcp_rec_dbf_event_thread(2, adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601}
602
603/*
Swen Schillig41fa2ad2007-09-07 09:15:31 +0200604 * function:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 *
606 * purpose:
607 *
608 * returns: <0 erp_action not found in any list
609 * ZFCP_ERP_ACTION_READY erp_action is in ready list
610 * ZFCP_ERP_ACTION_RUNNING erp_action is in running list
611 *
612 * locks: erp_lock must be held
613 */
614static int
615zfcp_erp_action_exists(struct zfcp_erp_action *erp_action)
616{
617 int retval = -EINVAL;
618 struct list_head *entry;
619 struct zfcp_erp_action *entry_erp_action;
620 struct zfcp_adapter *adapter = erp_action->adapter;
621
622 /* search in running list */
623 list_for_each(entry, &adapter->erp_running_head) {
624 entry_erp_action =
625 list_entry(entry, struct zfcp_erp_action, list);
626 if (entry_erp_action == erp_action) {
627 retval = ZFCP_ERP_ACTION_RUNNING;
628 goto out;
629 }
630 }
631 /* search in ready list */
632 list_for_each(entry, &adapter->erp_ready_head) {
633 entry_erp_action =
634 list_entry(entry, struct zfcp_erp_action, list);
635 if (entry_erp_action == erp_action) {
636 retval = ZFCP_ERP_ACTION_READY;
637 goto out;
638 }
639 }
640
641 out:
642 return retval;
643}
644
645/*
646 * purpose: checks current status of action (timed out, dismissed, ...)
647 * and does appropriate preparations (dismiss fsf request, ...)
648 *
649 * locks: called under erp_lock (disabled interrupts)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 */
Swen Schilligca880cf2007-02-09 10:00:14 +0100651static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652zfcp_erp_strategy_check_fsfreq(struct zfcp_erp_action *erp_action)
653{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 struct zfcp_adapter *adapter = erp_action->adapter;
655
656 if (erp_action->fsf_req) {
Volker Sameskefea9d6c2006-08-02 11:05:16 +0200657 /* take lock to ensure that request is not deleted meanwhile */
658 spin_lock(&adapter->req_list_lock);
Heiko Carstensd1ad09d2007-12-20 12:30:22 +0100659 if (zfcp_reqlist_find_safe(adapter, erp_action->fsf_req) &&
660 erp_action->fsf_req->erp_action == erp_action) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 /* fsf_req still exists */
Volker Sameskefea9d6c2006-08-02 11:05:16 +0200662 /* dismiss fsf_req of timed out/dismissed erp_action */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 if (erp_action->status & (ZFCP_STATUS_ERP_DISMISSED |
664 ZFCP_STATUS_ERP_TIMEDOUT)) {
Swen Schilligca880cf2007-02-09 10:00:14 +0100665 erp_action->fsf_req->status |=
666 ZFCP_STATUS_FSFREQ_DISMISSED;
Martin Peschke6f4f3652008-03-27 14:22:04 +0100667 zfcp_rec_dbf_event_action(142, erp_action);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 }
669 if (erp_action->status & ZFCP_STATUS_ERP_TIMEDOUT) {
Martin Peschke6f4f3652008-03-27 14:22:04 +0100670 zfcp_rec_dbf_event_action(143, erp_action);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 ZFCP_LOG_NORMAL("error: erp step timed out "
672 "(action=%d, fsf_req=%p)\n ",
673 erp_action->action,
674 erp_action->fsf_req);
675 }
676 /*
677 * If fsf_req is neither dismissed nor completed
678 * then keep it running asynchronously and don't mess
679 * with the association of erp_action and fsf_req.
680 */
Swen Schilligca880cf2007-02-09 10:00:14 +0100681 if (erp_action->fsf_req->status &
682 (ZFCP_STATUS_FSFREQ_COMPLETED |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 ZFCP_STATUS_FSFREQ_DISMISSED)) {
684 /* forget about association between fsf_req
685 and erp_action */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 erp_action->fsf_req = NULL;
687 }
688 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 /*
690 * even if this fsf_req has gone, forget about
691 * association between erp_action and fsf_req
692 */
693 erp_action->fsf_req = NULL;
694 }
Volker Sameskefea9d6c2006-08-02 11:05:16 +0200695 spin_unlock(&adapter->req_list_lock);
Martin Peschke507e4962008-03-27 14:22:05 +0100696 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697}
698
Andreas Herrmannf6c0e7a2006-08-02 11:05:52 +0200699/**
700 * zfcp_erp_async_handler_nolock - complete erp_action
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 *
Andreas Herrmannf6c0e7a2006-08-02 11:05:52 +0200702 * Used for normal completion, time-out, dismissal and failure after
703 * low memory condition.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 */
Andreas Herrmannf6c0e7a2006-08-02 11:05:52 +0200705static void zfcp_erp_async_handler_nolock(struct zfcp_erp_action *erp_action,
706 unsigned long set_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 if (zfcp_erp_action_exists(erp_action) == ZFCP_ERP_ACTION_RUNNING) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 erp_action->status |= set_mask;
710 zfcp_erp_action_ready(erp_action);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 } else {
712 /* action is ready or gone - nothing to do */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714}
715
Andreas Herrmannf6c0e7a2006-08-02 11:05:52 +0200716/**
717 * zfcp_erp_async_handler - wrapper for erp_async_handler_nolock w/ locking
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 */
Andreas Herrmannf6c0e7a2006-08-02 11:05:52 +0200719void zfcp_erp_async_handler(struct zfcp_erp_action *erp_action,
720 unsigned long set_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721{
722 struct zfcp_adapter *adapter = erp_action->adapter;
723 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
725 write_lock_irqsave(&adapter->erp_lock, flags);
Andreas Herrmannf6c0e7a2006-08-02 11:05:52 +0200726 zfcp_erp_async_handler_nolock(erp_action, set_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 write_unlock_irqrestore(&adapter->erp_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728}
729
730/*
731 * purpose: is called for erp_action which was slept waiting for
732 * memory becoming avaliable,
733 * will trigger that this action will be continued
734 */
735static void
736zfcp_erp_memwait_handler(unsigned long data)
737{
738 struct zfcp_erp_action *erp_action = (struct zfcp_erp_action *) data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739
740 zfcp_erp_async_handler(erp_action, 0);
741}
742
743/*
744 * purpose: is called if an asynchronous erp step timed out,
745 * action gets an appropriate flag and will be processed
746 * accordingly
747 */
Heiko Carstens364c8552007-10-12 16:11:35 +0200748static void zfcp_erp_timeout_handler(unsigned long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749{
750 struct zfcp_erp_action *erp_action = (struct zfcp_erp_action *) data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751
752 zfcp_erp_async_handler(erp_action, ZFCP_STATUS_ERP_TIMEDOUT);
753}
754
Andreas Herrmannf6c0e7a2006-08-02 11:05:52 +0200755/**
756 * zfcp_erp_action_dismiss - dismiss an erp_action
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 *
Andreas Herrmannf6c0e7a2006-08-02 11:05:52 +0200758 * adapter->erp_lock must be held
Swen Schillig41fa2ad2007-09-07 09:15:31 +0200759 *
Andreas Herrmannf6c0e7a2006-08-02 11:05:52 +0200760 * Dismissal of an erp_action is usually required if an erp_action of
761 * higher priority is generated.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 */
Andreas Herrmannf6c0e7a2006-08-02 11:05:52 +0200763static void zfcp_erp_action_dismiss(struct zfcp_erp_action *erp_action)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764{
Martin Peschked0076f72007-11-15 13:57:08 +0100765 erp_action->status |= ZFCP_STATUS_ERP_DISMISSED;
766 if (zfcp_erp_action_exists(erp_action) == ZFCP_ERP_ACTION_RUNNING)
767 zfcp_erp_action_ready(erp_action);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768}
769
770int
771zfcp_erp_thread_setup(struct zfcp_adapter *adapter)
772{
773 int retval = 0;
774
775 atomic_clear_mask(ZFCP_STATUS_ADAPTER_ERP_THREAD_UP, &adapter->status);
776
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 retval = kernel_thread(zfcp_erp_thread, adapter, SIGCHLD);
778 if (retval < 0) {
779 ZFCP_LOG_NORMAL("error: creation of erp thread failed for "
780 "adapter %s\n",
781 zfcp_get_busid_by_adapter(adapter));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 } else {
783 wait_event(adapter->erp_thread_wqh,
784 atomic_test_mask(ZFCP_STATUS_ADAPTER_ERP_THREAD_UP,
785 &adapter->status));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 }
787
788 return (retval < 0);
789}
790
791/*
Swen Schillig41fa2ad2007-09-07 09:15:31 +0200792 * function:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 *
Swen Schillig41fa2ad2007-09-07 09:15:31 +0200794 * purpose:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 *
796 * returns:
797 *
798 * context: process (i.e. proc-fs or rmmod/insmod)
799 *
800 * note: The caller of this routine ensures that the specified
801 * adapter has been shut down and that this operation
802 * has been completed. Thus, there are no pending erp_actions
803 * which would need to be handled here.
804 */
805int
806zfcp_erp_thread_kill(struct zfcp_adapter *adapter)
807{
808 int retval = 0;
809
810 atomic_set_mask(ZFCP_STATUS_ADAPTER_ERP_THREAD_KILL, &adapter->status);
811 up(&adapter->erp_ready_sem);
Christof Schmittaa0fec62008-05-19 12:17:47 +0200812 zfcp_rec_dbf_event_thread_lock(2, adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813
814 wait_event(adapter->erp_thread_wqh,
815 !atomic_test_mask(ZFCP_STATUS_ADAPTER_ERP_THREAD_UP,
816 &adapter->status));
817
818 atomic_clear_mask(ZFCP_STATUS_ADAPTER_ERP_THREAD_KILL,
819 &adapter->status);
820
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 return retval;
822}
823
824/*
825 * purpose: is run as a kernel thread,
826 * goes through list of error recovery actions of associated adapter
827 * and delegates single action to execution
828 *
829 * returns: 0
830 */
831static int
832zfcp_erp_thread(void *data)
833{
834 struct zfcp_adapter *adapter = (struct zfcp_adapter *) data;
835 struct list_head *next;
836 struct zfcp_erp_action *erp_action;
837 unsigned long flags;
838
839 daemonize("zfcperp%s", zfcp_get_busid_by_adapter(adapter));
840 /* Block all signals */
841 siginitsetinv(&current->blocked, 0);
842 atomic_set_mask(ZFCP_STATUS_ADAPTER_ERP_THREAD_UP, &adapter->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 wake_up(&adapter->erp_thread_wqh);
844
845 while (!atomic_test_mask(ZFCP_STATUS_ADAPTER_ERP_THREAD_KILL,
846 &adapter->status)) {
847
848 write_lock_irqsave(&adapter->erp_lock, flags);
Martin Peschke86e8dfc2007-11-15 13:57:17 +0100849 next = adapter->erp_ready_head.next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 write_unlock_irqrestore(&adapter->erp_lock, flags);
851
852 if (next != &adapter->erp_ready_head) {
853 erp_action =
854 list_entry(next, struct zfcp_erp_action, list);
855 /*
856 * process action (incl. [re]moving it
857 * from 'ready' queue)
858 */
859 zfcp_erp_strategy(erp_action);
860 }
861
862 /*
863 * sleep as long as there is nothing to do, i.e.
864 * no action in 'ready' queue to be processed and
865 * thread is not to be killed
866 */
Christof Schmittaa0fec62008-05-19 12:17:47 +0200867 zfcp_rec_dbf_event_thread_lock(4, adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 down_interruptible(&adapter->erp_ready_sem);
Christof Schmittaa0fec62008-05-19 12:17:47 +0200869 zfcp_rec_dbf_event_thread_lock(5, adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 }
871
872 atomic_clear_mask(ZFCP_STATUS_ADAPTER_ERP_THREAD_UP, &adapter->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 wake_up(&adapter->erp_thread_wqh);
874
875 return 0;
876}
877
878/*
Swen Schillig41fa2ad2007-09-07 09:15:31 +0200879 * function:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 *
881 * purpose: drives single error recovery action and schedules higher and
882 * subordinate actions, if necessary
883 *
884 * returns: ZFCP_ERP_CONTINUES - action continues (asynchronously)
885 * ZFCP_ERP_SUCCEEDED - action finished successfully (deqd)
886 * ZFCP_ERP_FAILED - action finished unsuccessfully (deqd)
887 * ZFCP_ERP_EXIT - action finished (dequeued), offline
888 * ZFCP_ERP_DISMISSED - action canceled (dequeued)
889 */
890static int
891zfcp_erp_strategy(struct zfcp_erp_action *erp_action)
892{
893 int retval = 0;
894 struct zfcp_adapter *adapter = erp_action->adapter;
895 struct zfcp_port *port = erp_action->port;
896 struct zfcp_unit *unit = erp_action->unit;
897 int action = erp_action->action;
898 u32 status = erp_action->status;
899 unsigned long flags;
900
901 /* serialise dismissing, timing out, moving, enqueueing */
902 read_lock_irqsave(&zfcp_data.config_lock, flags);
903 write_lock(&adapter->erp_lock);
904
905 /* dequeue dismissed action and leave, if required */
906 retval = zfcp_erp_strategy_check_action(erp_action, retval);
907 if (retval == ZFCP_ERP_DISMISSED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 goto unlock;
909 }
910
911 /*
912 * move action to 'running' queue before processing it
913 * (to avoid a race condition regarding moving the
914 * action to the 'running' queue and back)
915 */
916 zfcp_erp_action_to_running(erp_action);
917
918 /*
919 * try to process action as far as possible,
920 * no lock to allow for blocking operations (kmalloc, qdio, ...),
921 * afterwards the lock is required again for the following reasons:
922 * - dequeueing of finished action and enqueueing of
923 * follow-up actions must be atomic so that any other
924 * reopen-routine does not believe there is nothing to do
925 * and that it is safe to enqueue something else,
926 * - we want to force any control thread which is dismissing
927 * actions to finish this before we decide about
928 * necessary steps to be taken here further
929 */
930 write_unlock(&adapter->erp_lock);
931 read_unlock_irqrestore(&zfcp_data.config_lock, flags);
932 retval = zfcp_erp_strategy_do_action(erp_action);
933 read_lock_irqsave(&zfcp_data.config_lock, flags);
934 write_lock(&adapter->erp_lock);
935
936 /*
937 * check for dismissed status again to avoid follow-up actions,
Martin Peschke86e8dfc2007-11-15 13:57:17 +0100938 * failing of targets and so on for dismissed actions,
939 * we go through down() here because there has been an up()
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 */
Martin Peschke86e8dfc2007-11-15 13:57:17 +0100941 if (erp_action->status & ZFCP_STATUS_ERP_DISMISSED)
942 retval = ZFCP_ERP_CONTINUES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943
944 switch (retval) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 case ZFCP_ERP_NOMEM:
946 /* no memory to continue immediately, let it sleep */
947 if (!(erp_action->status & ZFCP_STATUS_ERP_LOWMEM)) {
948 ++adapter->erp_low_mem_count;
949 erp_action->status |= ZFCP_STATUS_ERP_LOWMEM;
950 }
951 /* This condition is true if there is no memory available
952 for any erp_action on this adapter. This implies that there
953 are no elements in the memory pool(s) left for erp_actions.
954 This might happen if an erp_action that used a memory pool
955 element was timed out.
956 */
957 if (adapter->erp_total_count == adapter->erp_low_mem_count) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 ZFCP_LOG_NORMAL("error: no mempool elements available, "
959 "restarting I/O on adapter %s "
960 "to free mempool\n",
961 zfcp_get_busid_by_adapter(adapter));
Martin Peschke1f6f7122008-04-18 12:51:55 +0200962 zfcp_erp_adapter_reopen_internal(adapter, 0, 66, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 retval = zfcp_erp_strategy_memwait(erp_action);
965 }
966 goto unlock;
967 case ZFCP_ERP_CONTINUES:
968 /* leave since this action runs asynchronously */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 if (erp_action->status & ZFCP_STATUS_ERP_LOWMEM) {
970 --adapter->erp_low_mem_count;
971 erp_action->status &= ~ZFCP_STATUS_ERP_LOWMEM;
972 }
973 goto unlock;
974 }
975 /* ok, finished action (whatever its result is) */
976
977 /* check for unrecoverable targets */
978 retval = zfcp_erp_strategy_check_target(erp_action, retval);
979
980 /* action must be dequeued (here to allow for further ones) */
981 zfcp_erp_action_dequeue(erp_action);
982
983 /*
984 * put this target through the erp mill again if someone has
Swen Schillig41fa2ad2007-09-07 09:15:31 +0200985 * requested to change the status of a target being online
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 * to offline or the other way around
987 * (old retval is preserved if nothing has to be done here)
988 */
989 retval = zfcp_erp_strategy_statechange(action, status, adapter,
990 port, unit, retval);
991
992 /*
993 * leave if target is in permanent error state or if
994 * action is repeated in order to process state change
995 */
996 if (retval == ZFCP_ERP_EXIT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 goto unlock;
998 }
999
1000 /* trigger follow up actions */
1001 zfcp_erp_strategy_followup_actions(action, adapter, port, unit, retval);
1002
1003 unlock:
1004 write_unlock(&adapter->erp_lock);
1005 read_unlock_irqrestore(&zfcp_data.config_lock, flags);
Swen Schillig41fa2ad2007-09-07 09:15:31 +02001006
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 if (retval != ZFCP_ERP_CONTINUES)
1008 zfcp_erp_action_cleanup(action, adapter, port, unit, retval);
1009
1010 /*
1011 * a few tasks remain when the erp queues are empty
1012 * (don't do that if the last action evaluated was dismissed
1013 * since this clearly indicates that there is more to come) :
1014 * - close the name server port if it is open yet
1015 * (enqueues another [probably] final action)
1016 * - otherwise, wake up whoever wants to be woken when we are
1017 * done with erp
1018 */
1019 if (retval != ZFCP_ERP_DISMISSED)
1020 zfcp_erp_strategy_check_queues(adapter);
1021
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 return retval;
1023}
1024
1025/*
Swen Schillig41fa2ad2007-09-07 09:15:31 +02001026 * function:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 *
Swen Schillig41fa2ad2007-09-07 09:15:31 +02001028 * purpose:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 *
1030 * returns: ZFCP_ERP_DISMISSED - if action has been dismissed
1031 * retval - otherwise
1032 */
1033static int
1034zfcp_erp_strategy_check_action(struct zfcp_erp_action *erp_action, int retval)
1035{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 zfcp_erp_strategy_check_fsfreq(erp_action);
1037
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 if (erp_action->status & ZFCP_STATUS_ERP_DISMISSED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 zfcp_erp_action_dequeue(erp_action);
1040 retval = ZFCP_ERP_DISMISSED;
Martin Peschke507e4962008-03-27 14:22:05 +01001041 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042
1043 return retval;
1044}
1045
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046static int
1047zfcp_erp_strategy_do_action(struct zfcp_erp_action *erp_action)
1048{
1049 int retval = ZFCP_ERP_FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050
1051 /*
1052 * try to execute/continue action as far as possible,
1053 * note: no lock in subsequent strategy routines
1054 * (this allows these routine to call schedule, e.g.
1055 * kmalloc with such flags or qdio_initialize & friends)
Joe Perches5d67d162008-01-26 14:11:20 +01001056 * Note: in case of timeout, the separate strategies will fail
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 * anyhow. No need for a special action. Even worse, a nameserver
1058 * failure would not wake up waiting ports without the call.
1059 */
1060 switch (erp_action->action) {
1061
1062 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
1063 retval = zfcp_erp_adapter_strategy(erp_action);
1064 break;
1065
1066 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
1067 retval = zfcp_erp_port_forced_strategy(erp_action);
1068 break;
1069
1070 case ZFCP_ERP_ACTION_REOPEN_PORT:
1071 retval = zfcp_erp_port_strategy(erp_action);
1072 break;
1073
1074 case ZFCP_ERP_ACTION_REOPEN_UNIT:
1075 retval = zfcp_erp_unit_strategy(erp_action);
1076 break;
1077
1078 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 ZFCP_LOG_NORMAL("bug: unknown erp action requested on "
1080 "adapter %s (action=%d)\n",
1081 zfcp_get_busid_by_adapter(erp_action->adapter),
1082 erp_action->action);
1083 }
1084
1085 return retval;
1086}
1087
1088/*
Swen Schillig41fa2ad2007-09-07 09:15:31 +02001089 * function:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 *
1091 * purpose: triggers retry of this action after a certain amount of time
1092 * by means of timer provided by erp_action
1093 *
1094 * returns: ZFCP_ERP_CONTINUES - erp_action sleeps in erp running queue
1095 */
1096static int
1097zfcp_erp_strategy_memwait(struct zfcp_erp_action *erp_action)
1098{
1099 int retval = ZFCP_ERP_CONTINUES;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 init_timer(&erp_action->timer);
1102 erp_action->timer.function = zfcp_erp_memwait_handler;
1103 erp_action->timer.data = (unsigned long) erp_action;
1104 erp_action->timer.expires = jiffies + ZFCP_ERP_MEMWAIT_TIMEOUT;
1105 add_timer(&erp_action->timer);
1106
1107 return retval;
1108}
1109
Swen Schillig41fa2ad2007-09-07 09:15:31 +02001110/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 * function: zfcp_erp_adapter_failed
1112 *
1113 * purpose: sets the adapter and all underlying devices to ERP_FAILED
1114 *
1115 */
1116void
Martin Peschke1f6f7122008-04-18 12:51:55 +02001117zfcp_erp_adapter_failed(struct zfcp_adapter *adapter, u8 id, void *ref)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118{
Martin Peschke698ec0162008-03-27 14:22:02 +01001119 zfcp_erp_modify_adapter_status(adapter, id, ref,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 ZFCP_STATUS_COMMON_ERP_FAILED, ZFCP_SET);
1121 ZFCP_LOG_NORMAL("adapter erp failed on adapter %s\n",
1122 zfcp_get_busid_by_adapter(adapter));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123}
1124
Swen Schillig41fa2ad2007-09-07 09:15:31 +02001125/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 * function: zfcp_erp_port_failed
1127 *
1128 * purpose: sets the port and all underlying devices to ERP_FAILED
1129 *
1130 */
1131void
Martin Peschke1f6f7122008-04-18 12:51:55 +02001132zfcp_erp_port_failed(struct zfcp_port *port, u8 id, void *ref)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133{
Martin Peschke698ec0162008-03-27 14:22:02 +01001134 zfcp_erp_modify_port_status(port, id, ref,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 ZFCP_STATUS_COMMON_ERP_FAILED, ZFCP_SET);
1136
1137 if (atomic_test_mask(ZFCP_STATUS_PORT_WKA, &port->status))
1138 ZFCP_LOG_NORMAL("port erp failed (adapter %s, "
Christof Schmitt1d589ed2007-05-08 11:14:41 +02001139 "port d_id=0x%06x)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 zfcp_get_busid_by_port(port), port->d_id);
1141 else
1142 ZFCP_LOG_NORMAL("port erp failed (adapter %s, wwpn=0x%016Lx)\n",
1143 zfcp_get_busid_by_port(port), port->wwpn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144}
1145
Swen Schillig41fa2ad2007-09-07 09:15:31 +02001146/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 * function: zfcp_erp_unit_failed
1148 *
1149 * purpose: sets the unit to ERP_FAILED
1150 *
1151 */
1152void
Martin Peschke1f6f7122008-04-18 12:51:55 +02001153zfcp_erp_unit_failed(struct zfcp_unit *unit, u8 id, void *ref)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154{
Martin Peschke698ec0162008-03-27 14:22:02 +01001155 zfcp_erp_modify_unit_status(unit, id, ref,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 ZFCP_STATUS_COMMON_ERP_FAILED, ZFCP_SET);
1157
1158 ZFCP_LOG_NORMAL("unit erp failed on unit 0x%016Lx on port 0x%016Lx "
1159 " on adapter %s\n", unit->fcp_lun,
1160 unit->port->wwpn, zfcp_get_busid_by_unit(unit));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161}
1162
1163/*
1164 * function: zfcp_erp_strategy_check_target
1165 *
1166 * purpose: increments the erp action count on the device currently in
1167 * recovery if the action failed or resets the count in case of
1168 * success. If a maximum count is exceeded the device is marked
1169 * as ERP_FAILED.
1170 * The 'blocked' state of a target which has been recovered
1171 * successfully is reset.
1172 *
1173 * returns: ZFCP_ERP_CONTINUES - action continues (not considered)
Swen Schillig41fa2ad2007-09-07 09:15:31 +02001174 * ZFCP_ERP_SUCCEEDED - action finished successfully
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 * ZFCP_ERP_EXIT - action failed and will not continue
1176 */
1177static int
1178zfcp_erp_strategy_check_target(struct zfcp_erp_action *erp_action, int result)
1179{
1180 struct zfcp_adapter *adapter = erp_action->adapter;
1181 struct zfcp_port *port = erp_action->port;
1182 struct zfcp_unit *unit = erp_action->unit;
1183
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 switch (erp_action->action) {
1185
1186 case ZFCP_ERP_ACTION_REOPEN_UNIT:
1187 result = zfcp_erp_strategy_check_unit(unit, result);
1188 break;
1189
1190 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
1191 case ZFCP_ERP_ACTION_REOPEN_PORT:
1192 result = zfcp_erp_strategy_check_port(port, result);
1193 break;
1194
1195 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
1196 result = zfcp_erp_strategy_check_adapter(adapter, result);
1197 break;
1198 }
1199
1200 return result;
1201}
1202
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203static int
1204zfcp_erp_strategy_statechange(int action,
1205 u32 status,
1206 struct zfcp_adapter *adapter,
1207 struct zfcp_port *port,
1208 struct zfcp_unit *unit, int retval)
1209{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 switch (action) {
1211
1212 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
1213 if (zfcp_erp_strategy_statechange_detected(&adapter->status,
1214 status)) {
Martin Peschke9467a9b2008-03-27 14:22:03 +01001215 zfcp_erp_adapter_reopen_internal(adapter,
1216 ZFCP_STATUS_COMMON_ERP_FAILED,
Martin Peschke1f6f7122008-04-18 12:51:55 +02001217 67, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 retval = ZFCP_ERP_EXIT;
1219 }
1220 break;
1221
1222 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
1223 case ZFCP_ERP_ACTION_REOPEN_PORT:
1224 if (zfcp_erp_strategy_statechange_detected(&port->status,
1225 status)) {
Martin Peschke9467a9b2008-03-27 14:22:03 +01001226 zfcp_erp_port_reopen_internal(port,
1227 ZFCP_STATUS_COMMON_ERP_FAILED,
Martin Peschke1f6f7122008-04-18 12:51:55 +02001228 68, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 retval = ZFCP_ERP_EXIT;
1230 }
1231 break;
1232
1233 case ZFCP_ERP_ACTION_REOPEN_UNIT:
1234 if (zfcp_erp_strategy_statechange_detected(&unit->status,
1235 status)) {
Martin Peschke9467a9b2008-03-27 14:22:03 +01001236 zfcp_erp_unit_reopen_internal(unit,
1237 ZFCP_STATUS_COMMON_ERP_FAILED,
Martin Peschke1f6f7122008-04-18 12:51:55 +02001238 69, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 retval = ZFCP_ERP_EXIT;
1240 }
1241 break;
1242 }
1243
1244 return retval;
1245}
1246
Heiko Carstens364c8552007-10-12 16:11:35 +02001247static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248zfcp_erp_strategy_statechange_detected(atomic_t * target_status, u32 erp_status)
1249{
1250 return
1251 /* take it online */
1252 (atomic_test_mask(ZFCP_STATUS_COMMON_RUNNING, target_status) &&
1253 (ZFCP_STATUS_ERP_CLOSE_ONLY & erp_status)) ||
1254 /* take it offline */
1255 (!atomic_test_mask(ZFCP_STATUS_COMMON_RUNNING, target_status) &&
1256 !(ZFCP_STATUS_ERP_CLOSE_ONLY & erp_status));
1257}
1258
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259static int
1260zfcp_erp_strategy_check_unit(struct zfcp_unit *unit, int result)
1261{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 switch (result) {
1263 case ZFCP_ERP_SUCCEEDED :
1264 atomic_set(&unit->erp_counter, 0);
1265 zfcp_erp_unit_unblock(unit);
1266 break;
1267 case ZFCP_ERP_FAILED :
1268 atomic_inc(&unit->erp_counter);
1269 if (atomic_read(&unit->erp_counter) > ZFCP_MAX_ERPS)
Martin Peschke1f6f7122008-04-18 12:51:55 +02001270 zfcp_erp_unit_failed(unit, 21, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 break;
1272 case ZFCP_ERP_EXIT :
1273 /* nothing */
1274 break;
1275 }
1276
1277 if (atomic_test_mask(ZFCP_STATUS_COMMON_ERP_FAILED, &unit->status)) {
1278 zfcp_erp_unit_block(unit, 0); /* for ZFCP_ERP_SUCCEEDED */
1279 result = ZFCP_ERP_EXIT;
1280 }
1281
1282 return result;
1283}
1284
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285static int
1286zfcp_erp_strategy_check_port(struct zfcp_port *port, int result)
1287{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 switch (result) {
1289 case ZFCP_ERP_SUCCEEDED :
1290 atomic_set(&port->erp_counter, 0);
1291 zfcp_erp_port_unblock(port);
1292 break;
1293 case ZFCP_ERP_FAILED :
1294 atomic_inc(&port->erp_counter);
1295 if (atomic_read(&port->erp_counter) > ZFCP_MAX_ERPS)
Martin Peschke1f6f7122008-04-18 12:51:55 +02001296 zfcp_erp_port_failed(port, 22, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 break;
1298 case ZFCP_ERP_EXIT :
1299 /* nothing */
1300 break;
1301 }
1302
1303 if (atomic_test_mask(ZFCP_STATUS_COMMON_ERP_FAILED, &port->status)) {
1304 zfcp_erp_port_block(port, 0); /* for ZFCP_ERP_SUCCEEDED */
1305 result = ZFCP_ERP_EXIT;
1306 }
1307
1308 return result;
1309}
1310
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311static int
1312zfcp_erp_strategy_check_adapter(struct zfcp_adapter *adapter, int result)
1313{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 switch (result) {
1315 case ZFCP_ERP_SUCCEEDED :
1316 atomic_set(&adapter->erp_counter, 0);
1317 zfcp_erp_adapter_unblock(adapter);
1318 break;
1319 case ZFCP_ERP_FAILED :
1320 atomic_inc(&adapter->erp_counter);
1321 if (atomic_read(&adapter->erp_counter) > ZFCP_MAX_ERPS)
Martin Peschke1f6f7122008-04-18 12:51:55 +02001322 zfcp_erp_adapter_failed(adapter, 23, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 break;
1324 case ZFCP_ERP_EXIT :
1325 /* nothing */
1326 break;
1327 }
1328
1329 if (atomic_test_mask(ZFCP_STATUS_COMMON_ERP_FAILED, &adapter->status)) {
1330 zfcp_erp_adapter_block(adapter, 0); /* for ZFCP_ERP_SUCCEEDED */
1331 result = ZFCP_ERP_EXIT;
1332 }
1333
1334 return result;
1335}
1336
Christof Schmitt5f852be2007-05-08 11:16:52 +02001337struct zfcp_erp_add_work {
1338 struct zfcp_unit *unit;
1339 struct work_struct work;
1340};
1341
1342/**
1343 * zfcp_erp_scsi_scan
1344 * @data: pointer to a struct zfcp_erp_add_work
1345 *
1346 * Registers a logical unit with the SCSI stack.
1347 */
1348static void zfcp_erp_scsi_scan(struct work_struct *work)
1349{
1350 struct zfcp_erp_add_work *p =
1351 container_of(work, struct zfcp_erp_add_work, work);
1352 struct zfcp_unit *unit = p->unit;
1353 struct fc_rport *rport = unit->port->rport;
1354 scsi_scan_target(&rport->dev, 0, rport->scsi_target_id,
1355 unit->scsi_lun, 0);
1356 atomic_clear_mask(ZFCP_STATUS_UNIT_SCSI_WORK_PENDING, &unit->status);
Christof Schmitt5f852be2007-05-08 11:16:52 +02001357 zfcp_unit_put(unit);
1358 kfree(p);
1359}
1360
1361/**
1362 * zfcp_erp_schedule_work
1363 * @unit: pointer to unit which should be registered with SCSI stack
1364 *
1365 * Schedules work which registers a unit with the SCSI stack
1366 */
1367static void
1368zfcp_erp_schedule_work(struct zfcp_unit *unit)
1369{
1370 struct zfcp_erp_add_work *p;
1371
Swen Schillig0d661322007-07-18 10:55:08 +02001372 p = kzalloc(sizeof(*p), GFP_KERNEL);
Christof Schmitt5f852be2007-05-08 11:16:52 +02001373 if (!p) {
1374 ZFCP_LOG_NORMAL("error: Out of resources. Could not register "
1375 "the FCP-LUN 0x%Lx connected to "
1376 "the port with WWPN 0x%Lx connected to "
1377 "the adapter %s with the SCSI stack.\n",
1378 unit->fcp_lun,
1379 unit->port->wwpn,
1380 zfcp_get_busid_by_unit(unit));
1381 return;
1382 }
1383
1384 zfcp_unit_get(unit);
Christof Schmitt5f852be2007-05-08 11:16:52 +02001385 atomic_set_mask(ZFCP_STATUS_UNIT_SCSI_WORK_PENDING, &unit->status);
1386 INIT_WORK(&p->work, zfcp_erp_scsi_scan);
1387 p->unit = unit;
1388 schedule_work(&p->work);
1389}
1390
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391/*
Swen Schillig41fa2ad2007-09-07 09:15:31 +02001392 * function:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 *
1394 * purpose: remaining things in good cases,
1395 * escalation in bad cases
1396 *
1397 * returns:
1398 */
1399static int
1400zfcp_erp_strategy_followup_actions(int action,
1401 struct zfcp_adapter *adapter,
1402 struct zfcp_port *port,
1403 struct zfcp_unit *unit, int status)
1404{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 /* initiate follow-up actions depending on success of finished action */
1406 switch (action) {
1407
1408 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
1409 if (status == ZFCP_ERP_SUCCEEDED)
Martin Peschke1f6f7122008-04-18 12:51:55 +02001410 zfcp_erp_port_reopen_all_internal(adapter, 0, 70, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 else
Martin Peschke1f6f7122008-04-18 12:51:55 +02001412 zfcp_erp_adapter_reopen_internal(adapter, 0, 71, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 break;
1414
1415 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
1416 if (status == ZFCP_ERP_SUCCEEDED)
Martin Peschke1f6f7122008-04-18 12:51:55 +02001417 zfcp_erp_port_reopen_internal(port, 0, 72, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 else
Martin Peschke1f6f7122008-04-18 12:51:55 +02001419 zfcp_erp_adapter_reopen_internal(adapter, 0, 73, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 break;
1421
1422 case ZFCP_ERP_ACTION_REOPEN_PORT:
1423 if (status == ZFCP_ERP_SUCCEEDED)
Martin Peschke1f6f7122008-04-18 12:51:55 +02001424 zfcp_erp_unit_reopen_all_internal(port, 0, 74, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 else
Martin Peschke1f6f7122008-04-18 12:51:55 +02001426 zfcp_erp_port_forced_reopen_internal(port, 0, 75, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 break;
1428
1429 case ZFCP_ERP_ACTION_REOPEN_UNIT:
Heiko Carstenscc16ceb2007-08-28 09:30:42 +02001430 /* Nothing to do if status == ZFCP_ERP_SUCCEEDED */
1431 if (status != ZFCP_ERP_SUCCEEDED)
Martin Peschke1f6f7122008-04-18 12:51:55 +02001432 zfcp_erp_port_reopen_internal(unit->port, 0, 76, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 break;
1434 }
1435
1436 return 0;
1437}
1438
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439static int
1440zfcp_erp_strategy_check_queues(struct zfcp_adapter *adapter)
1441{
1442 unsigned long flags;
1443
1444 read_lock_irqsave(&zfcp_data.config_lock, flags);
1445 read_lock(&adapter->erp_lock);
1446 if (list_empty(&adapter->erp_ready_head) &&
1447 list_empty(&adapter->erp_running_head)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 atomic_clear_mask(ZFCP_STATUS_ADAPTER_ERP_PENDING,
1449 &adapter->status);
1450 wake_up(&adapter->erp_done_wqh);
Martin Peschke507e4962008-03-27 14:22:05 +01001451 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 read_unlock(&adapter->erp_lock);
1453 read_unlock_irqrestore(&zfcp_data.config_lock, flags);
1454
1455 return 0;
1456}
1457
1458/**
1459 * zfcp_erp_wait - wait for completion of error recovery on an adapter
1460 * @adapter: adapter for which to wait for completion of its error recovery
1461 * Return: 0
1462 */
1463int
1464zfcp_erp_wait(struct zfcp_adapter *adapter)
1465{
1466 int retval = 0;
1467
1468 wait_event(adapter->erp_done_wqh,
1469 !atomic_test_mask(ZFCP_STATUS_ADAPTER_ERP_PENDING,
1470 &adapter->status));
1471
1472 return retval;
1473}
1474
Martin Peschke698ec0162008-03-27 14:22:02 +01001475void zfcp_erp_modify_adapter_status(struct zfcp_adapter *adapter, u8 id,
Martin Peschke1f6f7122008-04-18 12:51:55 +02001476 void *ref, u32 mask, int set_or_clear)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477{
1478 struct zfcp_port *port;
Martin Peschke698ec0162008-03-27 14:22:02 +01001479 u32 changed, common_mask = mask & ZFCP_COMMON_FLAGS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480
1481 if (set_or_clear == ZFCP_SET) {
Martin Peschke698ec0162008-03-27 14:22:02 +01001482 changed = atomic_test_and_set_mask(mask, &adapter->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 } else {
Martin Peschke698ec0162008-03-27 14:22:02 +01001484 changed = atomic_test_and_clear_mask(mask, &adapter->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 if (mask & ZFCP_STATUS_COMMON_ERP_FAILED)
1486 atomic_set(&adapter->erp_counter, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 }
Martin Peschke698ec0162008-03-27 14:22:02 +01001488 if (changed)
1489 zfcp_rec_dbf_event_adapter(id, ref, adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490
1491 /* Deal with all underlying devices, only pass common_mask */
1492 if (common_mask)
1493 list_for_each_entry(port, &adapter->port_list_head, list)
Martin Peschke698ec0162008-03-27 14:22:02 +01001494 zfcp_erp_modify_port_status(port, id, ref, common_mask,
1495 set_or_clear);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496}
1497
1498/*
1499 * function: zfcp_erp_modify_port_status
1500 *
1501 * purpose: sets the port and all underlying devices to ERP_FAILED
1502 *
1503 */
Martin Peschke1f6f7122008-04-18 12:51:55 +02001504void zfcp_erp_modify_port_status(struct zfcp_port *port, u8 id, void *ref,
Martin Peschke698ec0162008-03-27 14:22:02 +01001505 u32 mask, int set_or_clear)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506{
1507 struct zfcp_unit *unit;
Martin Peschke698ec0162008-03-27 14:22:02 +01001508 u32 changed, common_mask = mask & ZFCP_COMMON_FLAGS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509
1510 if (set_or_clear == ZFCP_SET) {
Martin Peschke698ec0162008-03-27 14:22:02 +01001511 changed = atomic_test_and_set_mask(mask, &port->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 } else {
Martin Peschke698ec0162008-03-27 14:22:02 +01001513 changed = atomic_test_and_clear_mask(mask, &port->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 if (mask & ZFCP_STATUS_COMMON_ERP_FAILED)
1515 atomic_set(&port->erp_counter, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 }
Martin Peschke698ec0162008-03-27 14:22:02 +01001517 if (changed)
1518 zfcp_rec_dbf_event_port(id, ref, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519
1520 /* Modify status of all underlying devices, only pass common mask */
1521 if (common_mask)
1522 list_for_each_entry(unit, &port->unit_list_head, list)
Martin Peschke698ec0162008-03-27 14:22:02 +01001523 zfcp_erp_modify_unit_status(unit, id, ref, common_mask,
1524 set_or_clear);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525}
1526
1527/*
1528 * function: zfcp_erp_modify_unit_status
1529 *
1530 * purpose: sets the unit to ERP_FAILED
1531 *
1532 */
Martin Peschke1f6f7122008-04-18 12:51:55 +02001533void zfcp_erp_modify_unit_status(struct zfcp_unit *unit, u8 id, void *ref,
Martin Peschke698ec0162008-03-27 14:22:02 +01001534 u32 mask, int set_or_clear)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535{
Martin Peschke698ec0162008-03-27 14:22:02 +01001536 u32 changed;
1537
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 if (set_or_clear == ZFCP_SET) {
Martin Peschke698ec0162008-03-27 14:22:02 +01001539 changed = atomic_test_and_set_mask(mask, &unit->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 } else {
Martin Peschke698ec0162008-03-27 14:22:02 +01001541 changed = atomic_test_and_clear_mask(mask, &unit->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 if (mask & ZFCP_STATUS_COMMON_ERP_FAILED) {
1543 atomic_set(&unit->erp_counter, 0);
1544 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 }
Martin Peschke698ec0162008-03-27 14:22:02 +01001546 if (changed)
1547 zfcp_rec_dbf_event_unit(id, ref, unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548}
1549
1550/*
Swen Schillig41fa2ad2007-09-07 09:15:31 +02001551 * function:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 *
1553 * purpose: Wrappper for zfcp_erp_port_reopen_all_internal
1554 * used to ensure the correct locking
1555 *
Andreas Mohrd6e05ed2006-06-26 18:35:02 +02001556 * returns: 0 - initiated action successfully
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 * <0 - failed to initiate action
1558 */
Martin Peschke9467a9b2008-03-27 14:22:03 +01001559int zfcp_erp_port_reopen_all(struct zfcp_adapter *adapter, int clear_mask,
Martin Peschke1f6f7122008-04-18 12:51:55 +02001560 u8 id, void *ref)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561{
1562 int retval;
1563 unsigned long flags;
1564
1565 read_lock_irqsave(&zfcp_data.config_lock, flags);
1566 write_lock(&adapter->erp_lock);
Martin Peschke9467a9b2008-03-27 14:22:03 +01001567 retval = zfcp_erp_port_reopen_all_internal(adapter, clear_mask, id,
1568 ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 write_unlock(&adapter->erp_lock);
1570 read_unlock_irqrestore(&zfcp_data.config_lock, flags);
1571
1572 return retval;
1573}
1574
Martin Peschke9467a9b2008-03-27 14:22:03 +01001575static int zfcp_erp_port_reopen_all_internal(struct zfcp_adapter *adapter,
Martin Peschke1f6f7122008-04-18 12:51:55 +02001576 int clear_mask, u8 id, void *ref)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577{
1578 int retval = 0;
1579 struct zfcp_port *port;
1580
1581 list_for_each_entry(port, &adapter->port_list_head, list)
1582 if (!atomic_test_mask(ZFCP_STATUS_PORT_WKA, &port->status))
Martin Peschke9467a9b2008-03-27 14:22:03 +01001583 zfcp_erp_port_reopen_internal(port, clear_mask, id,
1584 ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585
1586 return retval;
1587}
1588
1589/*
Swen Schillig41fa2ad2007-09-07 09:15:31 +02001590 * function:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 *
Swen Schillig41fa2ad2007-09-07 09:15:31 +02001592 * purpose:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 *
1594 * returns: FIXME
1595 */
Martin Peschke9467a9b2008-03-27 14:22:03 +01001596static int zfcp_erp_unit_reopen_all_internal(struct zfcp_port *port,
Martin Peschke1f6f7122008-04-18 12:51:55 +02001597 int clear_mask, u8 id, void *ref)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598{
1599 int retval = 0;
1600 struct zfcp_unit *unit;
1601
1602 list_for_each_entry(unit, &port->unit_list_head, list)
Martin Peschke9467a9b2008-03-27 14:22:03 +01001603 zfcp_erp_unit_reopen_internal(unit, clear_mask, id, ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604
1605 return retval;
1606}
1607
1608/*
Swen Schillig41fa2ad2007-09-07 09:15:31 +02001609 * function:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 *
1611 * purpose: this routine executes the 'Reopen Adapter' action
1612 * (the entire action is processed synchronously, since
1613 * there are no actions which might be run concurrently
1614 * per definition)
1615 *
1616 * returns: ZFCP_ERP_SUCCEEDED - action finished successfully
1617 * ZFCP_ERP_FAILED - action finished unsuccessfully
1618 */
1619static int
1620zfcp_erp_adapter_strategy(struct zfcp_erp_action *erp_action)
1621{
1622 int retval;
1623 struct zfcp_adapter *adapter = erp_action->adapter;
1624
1625 retval = zfcp_erp_adapter_strategy_close(erp_action);
1626 if (erp_action->status & ZFCP_STATUS_ERP_CLOSE_ONLY)
1627 retval = ZFCP_ERP_EXIT;
1628 else
1629 retval = zfcp_erp_adapter_strategy_open(erp_action);
1630
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 if (retval == ZFCP_ERP_FAILED) {
1632 ZFCP_LOG_INFO("Waiting to allow the adapter %s "
1633 "to recover itself\n",
1634 zfcp_get_busid_by_adapter(adapter));
Christof Schmitt18edcdb2007-11-05 12:37:45 +01001635 ssleep(ZFCP_TYPE2_RECOVERY_TIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 }
1637
1638 return retval;
1639}
1640
1641/*
Swen Schillig41fa2ad2007-09-07 09:15:31 +02001642 * function:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 *
Swen Schillig41fa2ad2007-09-07 09:15:31 +02001644 * purpose:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 *
1646 * returns: ZFCP_ERP_SUCCEEDED - action finished successfully
1647 * ZFCP_ERP_FAILED - action finished unsuccessfully
1648 */
1649static int
1650zfcp_erp_adapter_strategy_close(struct zfcp_erp_action *erp_action)
1651{
1652 int retval;
1653
1654 atomic_set_mask(ZFCP_STATUS_COMMON_CLOSING,
1655 &erp_action->adapter->status);
1656 retval = zfcp_erp_adapter_strategy_generic(erp_action, 1);
1657 atomic_clear_mask(ZFCP_STATUS_COMMON_CLOSING,
1658 &erp_action->adapter->status);
1659
1660 return retval;
1661}
1662
1663/*
Swen Schillig41fa2ad2007-09-07 09:15:31 +02001664 * function:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 *
Swen Schillig41fa2ad2007-09-07 09:15:31 +02001666 * purpose:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 *
1668 * returns: ZFCP_ERP_SUCCEEDED - action finished successfully
1669 * ZFCP_ERP_FAILED - action finished unsuccessfully
1670 */
1671static int
1672zfcp_erp_adapter_strategy_open(struct zfcp_erp_action *erp_action)
1673{
1674 int retval;
1675
1676 atomic_set_mask(ZFCP_STATUS_COMMON_OPENING,
1677 &erp_action->adapter->status);
1678 retval = zfcp_erp_adapter_strategy_generic(erp_action, 0);
1679 atomic_clear_mask(ZFCP_STATUS_COMMON_OPENING,
1680 &erp_action->adapter->status);
1681
1682 return retval;
1683}
1684
1685/*
1686 * function: zfcp_register_adapter
1687 *
1688 * purpose: allocate the irq associated with this devno and register
1689 * the FSF adapter with the SCSI stack
1690 *
Swen Schillig41fa2ad2007-09-07 09:15:31 +02001691 * returns:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 */
1693static int
1694zfcp_erp_adapter_strategy_generic(struct zfcp_erp_action *erp_action, int close)
1695{
1696 int retval = ZFCP_ERP_SUCCEEDED;
1697
1698 if (close)
1699 goto close_only;
1700
1701 retval = zfcp_erp_adapter_strategy_open_qdio(erp_action);
1702 if (retval != ZFCP_ERP_SUCCEEDED)
1703 goto failed_qdio;
1704
1705 retval = zfcp_erp_adapter_strategy_open_fsf(erp_action);
1706 if (retval != ZFCP_ERP_SUCCEEDED)
1707 goto failed_openfcp;
1708
1709 atomic_set_mask(ZFCP_STATUS_COMMON_OPEN, &erp_action->adapter->status);
1710 goto out;
1711
1712 close_only:
1713 atomic_clear_mask(ZFCP_STATUS_COMMON_OPEN,
1714 &erp_action->adapter->status);
1715
1716 failed_openfcp:
Andreas Herrmann2abbe862006-09-18 22:29:56 +02001717 zfcp_close_fsf(erp_action->adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 failed_qdio:
Volker Sameskec7f6b3a2007-05-29 15:29:49 +02001719 atomic_clear_mask(ZFCP_STATUS_ADAPTER_XCONFIG_OK |
1720 ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED |
1721 ZFCP_STATUS_ADAPTER_XPORT_OK,
1722 &erp_action->adapter->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 out:
1724 return retval;
1725}
1726
1727/*
1728 * function: zfcp_qdio_init
1729 *
1730 * purpose: setup QDIO operation for specified adapter
1731 *
1732 * returns: 0 - successful setup
1733 * !0 - failed setup
1734 */
Heiko Carstens364c8552007-10-12 16:11:35 +02001735static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736zfcp_erp_adapter_strategy_open_qdio(struct zfcp_erp_action *erp_action)
1737{
1738 int retval;
1739 int i;
1740 volatile struct qdio_buffer_element *sbale;
1741 struct zfcp_adapter *adapter = erp_action->adapter;
1742
1743 if (atomic_test_mask(ZFCP_STATUS_ADAPTER_QDIOUP, &adapter->status)) {
1744 ZFCP_LOG_NORMAL("bug: second attempt to set up QDIO on "
1745 "adapter %s\n",
1746 zfcp_get_busid_by_adapter(adapter));
1747 goto failed_sanity;
1748 }
1749
1750 if (qdio_establish(&adapter->qdio_init_data) != 0) {
1751 ZFCP_LOG_INFO("error: establishment of QDIO queues failed "
1752 "on adapter %s\n",
1753 zfcp_get_busid_by_adapter(adapter));
1754 goto failed_qdio_establish;
1755 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756
1757 if (qdio_activate(adapter->ccw_device, 0) != 0) {
1758 ZFCP_LOG_INFO("error: activation of QDIO queues failed "
1759 "on adapter %s\n",
1760 zfcp_get_busid_by_adapter(adapter));
1761 goto failed_qdio_activate;
1762 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763
1764 /*
1765 * put buffers into response queue,
1766 */
1767 for (i = 0; i < QDIO_MAX_BUFFERS_PER_Q; i++) {
1768 sbale = &(adapter->response_queue.buffer[i]->element[0]);
1769 sbale->length = 0;
1770 sbale->flags = SBAL_FLAGS_LAST_ENTRY;
Al Viro6aae8732006-10-10 22:44:27 +01001771 sbale->addr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 }
1773
1774 ZFCP_LOG_TRACE("calling do_QDIO on adapter %s (flags=0x%x, "
1775 "queue_no=%i, index_in_queue=%i, count=%i)\n",
1776 zfcp_get_busid_by_adapter(adapter),
1777 QDIO_FLAG_SYNC_INPUT, 0, 0, QDIO_MAX_BUFFERS_PER_Q);
1778
1779 retval = do_QDIO(adapter->ccw_device,
1780 QDIO_FLAG_SYNC_INPUT,
1781 0, 0, QDIO_MAX_BUFFERS_PER_Q, NULL);
1782
1783 if (retval) {
1784 ZFCP_LOG_NORMAL("bug: setup of QDIO failed (retval=%d)\n",
1785 retval);
1786 goto failed_do_qdio;
1787 } else {
1788 adapter->response_queue.free_index = 0;
1789 atomic_set(&adapter->response_queue.free_count, 0);
1790 ZFCP_LOG_DEBUG("%i buffers successfully enqueued to "
1791 "response queue\n", QDIO_MAX_BUFFERS_PER_Q);
1792 }
1793 /* set index of first avalable SBALS / number of available SBALS */
1794 adapter->request_queue.free_index = 0;
1795 atomic_set(&adapter->request_queue.free_count, QDIO_MAX_BUFFERS_PER_Q);
1796 adapter->request_queue.distance_from_int = 0;
1797
1798 /* initialize waitqueue used to wait for free SBALs in requests queue */
1799 init_waitqueue_head(&adapter->request_wq);
1800
1801 /* ok, we did it - skip all cleanups for different failures */
1802 atomic_set_mask(ZFCP_STATUS_ADAPTER_QDIOUP, &adapter->status);
1803 retval = ZFCP_ERP_SUCCEEDED;
1804 goto out;
1805
1806 failed_do_qdio:
1807 /* NOP */
1808
1809 failed_qdio_activate:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 while (qdio_shutdown(adapter->ccw_device,
1811 QDIO_FLAG_CLEANUP_USING_CLEAR) == -EINPROGRESS)
Christof Schmitt18edcdb2007-11-05 12:37:45 +01001812 ssleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813
1814 failed_qdio_establish:
1815 failed_sanity:
1816 retval = ZFCP_ERP_FAILED;
1817
1818 out:
1819 return retval;
1820}
1821
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823static int
1824zfcp_erp_adapter_strategy_open_fsf(struct zfcp_erp_action *erp_action)
1825{
Andreas Herrmann2448c452005-12-01 02:50:36 +01001826 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827
Andreas Herrmann2448c452005-12-01 02:50:36 +01001828 retval = zfcp_erp_adapter_strategy_open_fsf_xconfig(erp_action);
1829 if (retval == ZFCP_ERP_FAILED)
1830 return ZFCP_ERP_FAILED;
1831
1832 retval = zfcp_erp_adapter_strategy_open_fsf_xport(erp_action);
1833 if (retval == ZFCP_ERP_FAILED)
Maxim Shchetyninaef4a982005-09-13 21:51:16 +02001834 return ZFCP_ERP_FAILED;
1835
1836 return zfcp_erp_adapter_strategy_open_fsf_statusread(erp_action);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837}
1838
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839static int
1840zfcp_erp_adapter_strategy_open_fsf_xconfig(struct zfcp_erp_action *erp_action)
1841{
1842 int retval = ZFCP_ERP_SUCCEEDED;
1843 int retries;
Andreas Herrmann22753fa2005-06-13 13:15:15 +02001844 int sleep = ZFCP_EXCHANGE_CONFIG_DATA_FIRST_SLEEP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 struct zfcp_adapter *adapter = erp_action->adapter;
1846
1847 atomic_clear_mask(ZFCP_STATUS_ADAPTER_XCONFIG_OK, &adapter->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848
Andreas Herrmann22753fa2005-06-13 13:15:15 +02001849 for (retries = ZFCP_EXCHANGE_CONFIG_DATA_RETRIES; retries; retries--) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 atomic_clear_mask(ZFCP_STATUS_ADAPTER_HOST_CON_INIT,
1851 &adapter->status);
1852 ZFCP_LOG_DEBUG("Doing exchange config data\n");
Heiko Carstens9f09c542006-07-03 17:32:20 -07001853 write_lock_irq(&adapter->erp_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 zfcp_erp_action_to_running(erp_action);
Heiko Carstens9f09c542006-07-03 17:32:20 -07001855 write_unlock_irq(&adapter->erp_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 if (zfcp_fsf_exchange_config_data(erp_action)) {
1857 retval = ZFCP_ERP_FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 ZFCP_LOG_INFO("error: initiation of exchange of "
1859 "configuration data failed for "
1860 "adapter %s\n",
1861 zfcp_get_busid_by_adapter(adapter));
1862 break;
1863 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 ZFCP_LOG_DEBUG("Xchange underway\n");
1865
1866 /*
1867 * Why this works:
1868 * Both the normal completion handler as well as the timeout
1869 * handler will do an 'up' when the 'exchange config data'
1870 * request completes or times out. Thus, the signal to go on
1871 * won't be lost utilizing this semaphore.
1872 * Furthermore, this 'adapter_reopen' action is
1873 * guaranteed to be the only action being there (highest action
1874 * which prevents other actions from being created).
1875 * Resulting from that, the wake signal recognized here
1876 * _must_ be the one belonging to the 'exchange config
1877 * data' request.
1878 */
Christof Schmittaa0fec62008-05-19 12:17:47 +02001879 zfcp_rec_dbf_event_thread_lock(6, adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 down(&adapter->erp_ready_sem);
Christof Schmittaa0fec62008-05-19 12:17:47 +02001881 zfcp_rec_dbf_event_thread_lock(7, adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 if (erp_action->status & ZFCP_STATUS_ERP_TIMEDOUT) {
1883 ZFCP_LOG_INFO("error: exchange of configuration data "
1884 "for adapter %s timed out\n",
1885 zfcp_get_busid_by_adapter(adapter));
1886 break;
1887 }
Andreas Herrmann22753fa2005-06-13 13:15:15 +02001888
1889 if (!atomic_test_mask(ZFCP_STATUS_ADAPTER_HOST_CON_INIT,
1890 &adapter->status))
1891 break;
1892
1893 ZFCP_LOG_DEBUG("host connection still initialising... "
1894 "waiting and retrying...\n");
1895 /* sleep a little bit before retry */
Christof Schmitt18edcdb2007-11-05 12:37:45 +01001896 ssleep(sleep);
Andreas Herrmann22753fa2005-06-13 13:15:15 +02001897 sleep *= 2;
1898 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899
Volker Sameskec7f6b3a2007-05-29 15:29:49 +02001900 atomic_clear_mask(ZFCP_STATUS_ADAPTER_HOST_CON_INIT,
1901 &adapter->status);
1902
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 if (!atomic_test_mask(ZFCP_STATUS_ADAPTER_XCONFIG_OK,
1904 &adapter->status)) {
1905 ZFCP_LOG_INFO("error: exchange of configuration data for "
1906 "adapter %s failed\n",
1907 zfcp_get_busid_by_adapter(adapter));
1908 retval = ZFCP_ERP_FAILED;
1909 }
1910
1911 return retval;
1912}
1913
Maxim Shchetyninaef4a982005-09-13 21:51:16 +02001914static int
1915zfcp_erp_adapter_strategy_open_fsf_xport(struct zfcp_erp_action *erp_action)
1916{
Andreas Herrmann2448c452005-12-01 02:50:36 +01001917 int ret;
Andreas Herrmann2f8f3ed2006-02-11 01:41:50 +01001918 struct zfcp_adapter *adapter;
Maxim Shchetyninaef4a982005-09-13 21:51:16 +02001919
Andreas Herrmann2f8f3ed2006-02-11 01:41:50 +01001920 adapter = erp_action->adapter;
Maxim Shchetyninaef4a982005-09-13 21:51:16 +02001921 atomic_clear_mask(ZFCP_STATUS_ADAPTER_XPORT_OK, &adapter->status);
1922
Heiko Carstens9f09c542006-07-03 17:32:20 -07001923 write_lock_irq(&adapter->erp_lock);
Andreas Herrmann2f8f3ed2006-02-11 01:41:50 +01001924 zfcp_erp_action_to_running(erp_action);
Heiko Carstens9f09c542006-07-03 17:32:20 -07001925 write_unlock_irq(&adapter->erp_lock);
Maxim Shchetyninaef4a982005-09-13 21:51:16 +02001926
Swen Schillig52ef11a2007-08-28 09:31:09 +02001927 ret = zfcp_fsf_exchange_port_data(erp_action);
Andreas Herrmann2f8f3ed2006-02-11 01:41:50 +01001928 if (ret == -EOPNOTSUPP) {
Andreas Herrmann2f8f3ed2006-02-11 01:41:50 +01001929 return ZFCP_ERP_SUCCEEDED;
1930 } else if (ret) {
Andreas Herrmann2f8f3ed2006-02-11 01:41:50 +01001931 return ZFCP_ERP_FAILED;
1932 }
Maxim Shchetyninaef4a982005-09-13 21:51:16 +02001933
Andreas Herrmann2f8f3ed2006-02-11 01:41:50 +01001934 ret = ZFCP_ERP_SUCCEEDED;
Christof Schmittaa0fec62008-05-19 12:17:47 +02001935 zfcp_rec_dbf_event_thread_lock(8, adapter);
Andreas Herrmann2f8f3ed2006-02-11 01:41:50 +01001936 down(&adapter->erp_ready_sem);
Christof Schmittaa0fec62008-05-19 12:17:47 +02001937 zfcp_rec_dbf_event_thread_lock(9, adapter);
Andreas Herrmann2f8f3ed2006-02-11 01:41:50 +01001938 if (erp_action->status & ZFCP_STATUS_ERP_TIMEDOUT) {
1939 ZFCP_LOG_INFO("error: exchange port data timed out (adapter "
1940 "%s)\n", zfcp_get_busid_by_adapter(adapter));
1941 ret = ZFCP_ERP_FAILED;
1942 }
Andreas Herrmannf6c0e7a2006-08-02 11:05:52 +02001943
1944 /* don't treat as error for the sake of compatibility */
1945 if (!atomic_test_mask(ZFCP_STATUS_ADAPTER_XPORT_OK, &adapter->status))
1946 ZFCP_LOG_INFO("warning: exchange port data failed (adapter "
Andreas Herrmann2f8f3ed2006-02-11 01:41:50 +01001947 "%s\n", zfcp_get_busid_by_adapter(adapter));
Maxim Shchetyninaef4a982005-09-13 21:51:16 +02001948
Andreas Herrmann2f8f3ed2006-02-11 01:41:50 +01001949 return ret;
Maxim Shchetyninaef4a982005-09-13 21:51:16 +02001950}
1951
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952static int
1953zfcp_erp_adapter_strategy_open_fsf_statusread(struct zfcp_erp_action
1954 *erp_action)
1955{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 struct zfcp_adapter *adapter = erp_action->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957
Swen Schilligd26ab062008-05-19 12:17:37 +02001958 atomic_set(&adapter->stat_miss, 16);
1959 return zfcp_status_read_refill(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960}
1961
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962/*
Swen Schillig41fa2ad2007-09-07 09:15:31 +02001963 * function:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 *
1965 * purpose: this routine executes the 'Reopen Physical Port' action
1966 *
1967 * returns: ZFCP_ERP_CONTINUES - action continues (asynchronously)
1968 * ZFCP_ERP_SUCCEEDED - action finished successfully
1969 * ZFCP_ERP_FAILED - action finished unsuccessfully
1970 */
1971static int
1972zfcp_erp_port_forced_strategy(struct zfcp_erp_action *erp_action)
1973{
1974 int retval = ZFCP_ERP_FAILED;
1975 struct zfcp_port *port = erp_action->port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976
1977 switch (erp_action->step) {
1978
1979 /*
1980 * FIXME:
1981 * the ULP spec. begs for waiting for oustanding commands
1982 */
1983 case ZFCP_ERP_STEP_UNINITIALIZED:
1984 zfcp_erp_port_strategy_clearstati(port);
1985 /*
1986 * it would be sufficient to test only the normal open flag
1987 * since the phys. open flag cannot be set if the normal
1988 * open flag is unset - however, this is for readabilty ...
1989 */
1990 if (atomic_test_mask((ZFCP_STATUS_PORT_PHYS_OPEN |
1991 ZFCP_STATUS_COMMON_OPEN),
1992 &port->status)) {
1993 ZFCP_LOG_DEBUG("port 0x%016Lx is open -> trying "
1994 "close physical\n", port->wwpn);
1995 retval =
1996 zfcp_erp_port_forced_strategy_close(erp_action);
1997 } else
1998 retval = ZFCP_ERP_FAILED;
1999 break;
2000
2001 case ZFCP_ERP_STEP_PHYS_PORT_CLOSING:
2002 if (atomic_test_mask(ZFCP_STATUS_PORT_PHYS_OPEN,
2003 &port->status)) {
2004 ZFCP_LOG_DEBUG("close physical failed for port "
2005 "0x%016Lx\n", port->wwpn);
2006 retval = ZFCP_ERP_FAILED;
2007 } else
2008 retval = ZFCP_ERP_SUCCEEDED;
2009 break;
2010 }
2011
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 return retval;
2013}
2014
2015/*
Swen Schillig41fa2ad2007-09-07 09:15:31 +02002016 * function:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 *
2018 * purpose: this routine executes the 'Reopen Port' action
2019 *
2020 * returns: ZFCP_ERP_CONTINUES - action continues (asynchronously)
2021 * ZFCP_ERP_SUCCEEDED - action finished successfully
2022 * ZFCP_ERP_FAILED - action finished unsuccessfully
2023 */
2024static int
2025zfcp_erp_port_strategy(struct zfcp_erp_action *erp_action)
2026{
2027 int retval = ZFCP_ERP_FAILED;
2028 struct zfcp_port *port = erp_action->port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029
2030 switch (erp_action->step) {
2031
2032 /*
2033 * FIXME:
2034 * the ULP spec. begs for waiting for oustanding commands
2035 */
2036 case ZFCP_ERP_STEP_UNINITIALIZED:
2037 zfcp_erp_port_strategy_clearstati(port);
2038 if (atomic_test_mask(ZFCP_STATUS_COMMON_OPEN, &port->status)) {
2039 ZFCP_LOG_DEBUG("port 0x%016Lx is open -> trying "
2040 "close\n", port->wwpn);
2041 retval = zfcp_erp_port_strategy_close(erp_action);
2042 goto out;
2043 } /* else it's already closed, open it */
2044 break;
2045
2046 case ZFCP_ERP_STEP_PORT_CLOSING:
2047 if (atomic_test_mask(ZFCP_STATUS_COMMON_OPEN, &port->status)) {
2048 ZFCP_LOG_DEBUG("close failed for port 0x%016Lx\n",
2049 port->wwpn);
2050 retval = ZFCP_ERP_FAILED;
2051 goto out;
2052 } /* else it's closed now, open it */
2053 break;
2054 }
2055 if (erp_action->status & ZFCP_STATUS_ERP_CLOSE_ONLY)
2056 retval = ZFCP_ERP_EXIT;
2057 else
2058 retval = zfcp_erp_port_strategy_open(erp_action);
2059
2060 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 return retval;
2062}
2063
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064static int
2065zfcp_erp_port_strategy_open(struct zfcp_erp_action *erp_action)
2066{
2067 int retval;
2068
2069 if (atomic_test_mask(ZFCP_STATUS_PORT_WKA,
2070 &erp_action->port->status))
2071 retval = zfcp_erp_port_strategy_open_nameserver(erp_action);
2072 else
2073 retval = zfcp_erp_port_strategy_open_common(erp_action);
2074
2075 return retval;
2076}
2077
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078static int
2079zfcp_erp_port_strategy_open_common(struct zfcp_erp_action *erp_action)
2080{
2081 int retval = 0;
2082 struct zfcp_adapter *adapter = erp_action->adapter;
2083 struct zfcp_port *port = erp_action->port;
2084
2085 switch (erp_action->step) {
2086
2087 case ZFCP_ERP_STEP_UNINITIALIZED:
2088 case ZFCP_ERP_STEP_PHYS_PORT_CLOSING:
2089 case ZFCP_ERP_STEP_PORT_CLOSING:
Andreas Herrmannad757cd2006-01-13 02:26:11 +01002090 if (fc_host_port_type(adapter->scsi_host) == FC_PORTTYPE_PTP) {
6f71d9b2005-04-10 23:04:28 -05002091 if (port->wwpn != adapter->peer_wwpn) {
2092 ZFCP_LOG_NORMAL("Failed to open port 0x%016Lx "
2093 "on adapter %s.\nPeer WWPN "
2094 "0x%016Lx does not match\n",
2095 port->wwpn,
2096 zfcp_get_busid_by_adapter(adapter),
2097 adapter->peer_wwpn);
Martin Peschke1f6f7122008-04-18 12:51:55 +02002098 zfcp_erp_port_failed(port, 25, NULL);
6f71d9b2005-04-10 23:04:28 -05002099 retval = ZFCP_ERP_FAILED;
2100 break;
2101 }
2102 port->d_id = adapter->peer_d_id;
2103 atomic_set_mask(ZFCP_STATUS_PORT_DID_DID, &port->status);
2104 retval = zfcp_erp_port_strategy_open_port(erp_action);
2105 break;
2106 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 if (!(adapter->nameserver_port)) {
2108 retval = zfcp_nameserver_enqueue(adapter);
2109 if (retval != 0) {
2110 ZFCP_LOG_NORMAL("error: nameserver port "
2111 "unavailable for adapter %s\n",
2112 zfcp_get_busid_by_adapter(adapter));
2113 retval = ZFCP_ERP_FAILED;
2114 break;
2115 }
2116 }
2117 if (!atomic_test_mask(ZFCP_STATUS_COMMON_UNBLOCKED,
2118 &adapter->nameserver_port->status)) {
2119 ZFCP_LOG_DEBUG("nameserver port is not open -> open "
2120 "nameserver port\n");
2121 /* nameserver port may live again */
2122 atomic_set_mask(ZFCP_STATUS_COMMON_RUNNING,
2123 &adapter->nameserver_port->status);
Martin Peschke9467a9b2008-03-27 14:22:03 +01002124 if (zfcp_erp_port_reopen(adapter->nameserver_port, 0,
Martin Peschke1f6f7122008-04-18 12:51:55 +02002125 77, erp_action) >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126 erp_action->step =
2127 ZFCP_ERP_STEP_NAMESERVER_OPEN;
2128 retval = ZFCP_ERP_CONTINUES;
2129 } else
2130 retval = ZFCP_ERP_FAILED;
2131 break;
2132 }
2133 /* else nameserver port is already open, fall through */
2134 case ZFCP_ERP_STEP_NAMESERVER_OPEN:
2135 if (!atomic_test_mask(ZFCP_STATUS_COMMON_OPEN,
2136 &adapter->nameserver_port->status)) {
2137 ZFCP_LOG_DEBUG("open failed for nameserver port\n");
2138 retval = ZFCP_ERP_FAILED;
2139 } else {
2140 ZFCP_LOG_DEBUG("nameserver port is open -> "
2141 "nameserver look-up for port 0x%016Lx\n",
2142 port->wwpn);
2143 retval = zfcp_erp_port_strategy_open_common_lookup
2144 (erp_action);
2145 }
2146 break;
2147
2148 case ZFCP_ERP_STEP_NAMESERVER_LOOKUP:
2149 if (!atomic_test_mask(ZFCP_STATUS_PORT_DID_DID, &port->status)) {
2150 if (atomic_test_mask
2151 (ZFCP_STATUS_PORT_INVALID_WWPN, &port->status)) {
2152 ZFCP_LOG_DEBUG("nameserver look-up failed "
2153 "for port 0x%016Lx "
2154 "(misconfigured WWPN?)\n",
2155 port->wwpn);
Martin Peschke1f6f7122008-04-18 12:51:55 +02002156 zfcp_erp_port_failed(port, 26, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 retval = ZFCP_ERP_EXIT;
2158 } else {
2159 ZFCP_LOG_DEBUG("nameserver look-up failed for "
2160 "port 0x%016Lx\n", port->wwpn);
2161 retval = ZFCP_ERP_FAILED;
2162 }
2163 } else {
Christof Schmitt1d589ed2007-05-08 11:14:41 +02002164 ZFCP_LOG_DEBUG("port 0x%016Lx has d_id=0x%06x -> "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 "trying open\n", port->wwpn, port->d_id);
2166 retval = zfcp_erp_port_strategy_open_port(erp_action);
2167 }
2168 break;
2169
2170 case ZFCP_ERP_STEP_PORT_OPENING:
2171 /* D_ID might have changed during open */
2172 if (atomic_test_mask((ZFCP_STATUS_COMMON_OPEN |
2173 ZFCP_STATUS_PORT_DID_DID),
2174 &port->status)) {
2175 ZFCP_LOG_DEBUG("port 0x%016Lx is open\n", port->wwpn);
2176 retval = ZFCP_ERP_SUCCEEDED;
2177 } else {
2178 ZFCP_LOG_DEBUG("open failed for port 0x%016Lx\n",
2179 port->wwpn);
2180 retval = ZFCP_ERP_FAILED;
2181 }
2182 break;
2183
2184 default:
2185 ZFCP_LOG_NORMAL("bug: unknown erp step 0x%08x\n",
2186 erp_action->step);
2187 retval = ZFCP_ERP_FAILED;
2188 }
2189
2190 return retval;
2191}
2192
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193static int
2194zfcp_erp_port_strategy_open_nameserver(struct zfcp_erp_action *erp_action)
2195{
2196 int retval;
2197 struct zfcp_port *port = erp_action->port;
2198
2199 switch (erp_action->step) {
2200
2201 case ZFCP_ERP_STEP_UNINITIALIZED:
2202 case ZFCP_ERP_STEP_PHYS_PORT_CLOSING:
2203 case ZFCP_ERP_STEP_PORT_CLOSING:
Christof Schmitt1d589ed2007-05-08 11:14:41 +02002204 ZFCP_LOG_DEBUG("port 0x%016Lx has d_id=0x%06x -> trying open\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 port->wwpn, port->d_id);
2206 retval = zfcp_erp_port_strategy_open_port(erp_action);
2207 break;
2208
2209 case ZFCP_ERP_STEP_PORT_OPENING:
2210 if (atomic_test_mask(ZFCP_STATUS_COMMON_OPEN, &port->status)) {
2211 ZFCP_LOG_DEBUG("WKA port is open\n");
2212 retval = ZFCP_ERP_SUCCEEDED;
2213 } else {
2214 ZFCP_LOG_DEBUG("open failed for WKA port\n");
2215 retval = ZFCP_ERP_FAILED;
2216 }
2217 /* this is needed anyway (dont care for retval of wakeup) */
2218 ZFCP_LOG_DEBUG("continue other open port operations\n");
2219 zfcp_erp_port_strategy_open_nameserver_wakeup(erp_action);
2220 break;
2221
2222 default:
2223 ZFCP_LOG_NORMAL("bug: unknown erp step 0x%08x\n",
2224 erp_action->step);
2225 retval = ZFCP_ERP_FAILED;
2226 }
2227
2228 return retval;
2229}
2230
2231/*
Swen Schillig41fa2ad2007-09-07 09:15:31 +02002232 * function:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 *
2234 * purpose: makes the erp thread continue with reopen (physical) port
2235 * actions which have been paused until the name server port
2236 * is opened (or failed)
2237 *
2238 * returns: 0 (a kind of void retval, its not used)
2239 */
2240static int
2241zfcp_erp_port_strategy_open_nameserver_wakeup(struct zfcp_erp_action
2242 *ns_erp_action)
2243{
2244 int retval = 0;
2245 unsigned long flags;
2246 struct zfcp_adapter *adapter = ns_erp_action->adapter;
2247 struct zfcp_erp_action *erp_action, *tmp;
2248
2249 read_lock_irqsave(&adapter->erp_lock, flags);
2250 list_for_each_entry_safe(erp_action, tmp, &adapter->erp_running_head,
2251 list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 if (erp_action->step == ZFCP_ERP_STEP_NAMESERVER_OPEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253 if (atomic_test_mask(
2254 ZFCP_STATUS_COMMON_ERP_FAILED,
2255 &adapter->nameserver_port->status))
Martin Peschke1f6f7122008-04-18 12:51:55 +02002256 zfcp_erp_port_failed(erp_action->port, 27,
2257 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 zfcp_erp_action_ready(erp_action);
2259 }
2260 }
2261 read_unlock_irqrestore(&adapter->erp_lock, flags);
2262
2263 return retval;
2264}
2265
2266/*
Swen Schillig41fa2ad2007-09-07 09:15:31 +02002267 * function:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 *
Swen Schillig41fa2ad2007-09-07 09:15:31 +02002269 * purpose:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 *
2271 * returns: ZFCP_ERP_CONTINUES - action continues (asynchronously)
2272 * ZFCP_ERP_FAILED - action finished unsuccessfully
2273 */
2274static int
2275zfcp_erp_port_forced_strategy_close(struct zfcp_erp_action *erp_action)
2276{
2277 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 retval = zfcp_fsf_close_physical_port(erp_action);
2280 if (retval == -ENOMEM) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 retval = ZFCP_ERP_NOMEM;
2282 goto out;
2283 }
2284 erp_action->step = ZFCP_ERP_STEP_PHYS_PORT_CLOSING;
2285 if (retval != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 /* could not send 'open', fail */
2287 retval = ZFCP_ERP_FAILED;
2288 goto out;
2289 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 retval = ZFCP_ERP_CONTINUES;
2291 out:
2292 return retval;
2293}
2294
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295static int
2296zfcp_erp_port_strategy_clearstati(struct zfcp_port *port)
2297{
2298 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299
2300 atomic_clear_mask(ZFCP_STATUS_COMMON_OPENING |
2301 ZFCP_STATUS_COMMON_CLOSING |
2302 ZFCP_STATUS_COMMON_ACCESS_DENIED |
2303 ZFCP_STATUS_PORT_DID_DID |
2304 ZFCP_STATUS_PORT_PHYS_CLOSING |
2305 ZFCP_STATUS_PORT_INVALID_WWPN,
2306 &port->status);
2307 return retval;
2308}
2309
2310/*
Swen Schillig41fa2ad2007-09-07 09:15:31 +02002311 * function:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 *
Swen Schillig41fa2ad2007-09-07 09:15:31 +02002313 * purpose:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 *
2315 * returns: ZFCP_ERP_CONTINUES - action continues (asynchronously)
2316 * ZFCP_ERP_FAILED - action finished unsuccessfully
2317 */
2318static int
2319zfcp_erp_port_strategy_close(struct zfcp_erp_action *erp_action)
2320{
2321 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323 retval = zfcp_fsf_close_port(erp_action);
2324 if (retval == -ENOMEM) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 retval = ZFCP_ERP_NOMEM;
2326 goto out;
2327 }
2328 erp_action->step = ZFCP_ERP_STEP_PORT_CLOSING;
2329 if (retval != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 /* could not send 'close', fail */
2331 retval = ZFCP_ERP_FAILED;
2332 goto out;
2333 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 retval = ZFCP_ERP_CONTINUES;
2335 out:
2336 return retval;
2337}
2338
2339/*
Swen Schillig41fa2ad2007-09-07 09:15:31 +02002340 * function:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 *
Swen Schillig41fa2ad2007-09-07 09:15:31 +02002342 * purpose:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 *
2344 * returns: ZFCP_ERP_CONTINUES - action continues (asynchronously)
2345 * ZFCP_ERP_FAILED - action finished unsuccessfully
2346 */
2347static int
2348zfcp_erp_port_strategy_open_port(struct zfcp_erp_action *erp_action)
2349{
2350 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352 retval = zfcp_fsf_open_port(erp_action);
2353 if (retval == -ENOMEM) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 retval = ZFCP_ERP_NOMEM;
2355 goto out;
2356 }
2357 erp_action->step = ZFCP_ERP_STEP_PORT_OPENING;
2358 if (retval != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 /* could not send 'open', fail */
2360 retval = ZFCP_ERP_FAILED;
2361 goto out;
2362 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363 retval = ZFCP_ERP_CONTINUES;
2364 out:
2365 return retval;
2366}
2367
2368/*
Swen Schillig41fa2ad2007-09-07 09:15:31 +02002369 * function:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 *
Swen Schillig41fa2ad2007-09-07 09:15:31 +02002371 * purpose:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 *
2373 * returns: ZFCP_ERP_CONTINUES - action continues (asynchronously)
2374 * ZFCP_ERP_FAILED - action finished unsuccessfully
2375 */
2376static int
2377zfcp_erp_port_strategy_open_common_lookup(struct zfcp_erp_action *erp_action)
2378{
2379 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380
Christof Schmitt24073b42008-06-10 18:20:54 +02002381 retval = zfcp_fc_ns_gid_pn_request(erp_action);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 if (retval == -ENOMEM) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383 retval = ZFCP_ERP_NOMEM;
2384 goto out;
2385 }
2386 erp_action->step = ZFCP_ERP_STEP_NAMESERVER_LOOKUP;
2387 if (retval != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388 /* could not send nameserver request, fail */
2389 retval = ZFCP_ERP_FAILED;
2390 goto out;
2391 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 retval = ZFCP_ERP_CONTINUES;
2393 out:
2394 return retval;
2395}
2396
2397/*
Swen Schillig41fa2ad2007-09-07 09:15:31 +02002398 * function:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399 *
2400 * purpose: this routine executes the 'Reopen Unit' action
2401 * currently no retries
2402 *
2403 * returns: ZFCP_ERP_CONTINUES - action continues (asynchronously)
2404 * ZFCP_ERP_SUCCEEDED - action finished successfully
2405 * ZFCP_ERP_FAILED - action finished unsuccessfully
2406 */
2407static int
2408zfcp_erp_unit_strategy(struct zfcp_erp_action *erp_action)
2409{
2410 int retval = ZFCP_ERP_FAILED;
2411 struct zfcp_unit *unit = erp_action->unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412
2413 switch (erp_action->step) {
2414
2415 /*
2416 * FIXME:
2417 * the ULP spec. begs for waiting for oustanding commands
2418 */
2419 case ZFCP_ERP_STEP_UNINITIALIZED:
2420 zfcp_erp_unit_strategy_clearstati(unit);
2421 if (atomic_test_mask(ZFCP_STATUS_COMMON_OPEN, &unit->status)) {
2422 ZFCP_LOG_DEBUG("unit 0x%016Lx is open -> "
2423 "trying close\n", unit->fcp_lun);
2424 retval = zfcp_erp_unit_strategy_close(erp_action);
2425 break;
2426 }
2427 /* else it's already closed, fall through */
2428 case ZFCP_ERP_STEP_UNIT_CLOSING:
2429 if (atomic_test_mask(ZFCP_STATUS_COMMON_OPEN, &unit->status)) {
2430 ZFCP_LOG_DEBUG("close failed for unit 0x%016Lx\n",
2431 unit->fcp_lun);
2432 retval = ZFCP_ERP_FAILED;
2433 } else {
2434 if (erp_action->status & ZFCP_STATUS_ERP_CLOSE_ONLY)
2435 retval = ZFCP_ERP_EXIT;
2436 else {
2437 ZFCP_LOG_DEBUG("unit 0x%016Lx is not open -> "
2438 "trying open\n", unit->fcp_lun);
2439 retval =
2440 zfcp_erp_unit_strategy_open(erp_action);
2441 }
2442 }
2443 break;
2444
2445 case ZFCP_ERP_STEP_UNIT_OPENING:
2446 if (atomic_test_mask(ZFCP_STATUS_COMMON_OPEN, &unit->status)) {
2447 ZFCP_LOG_DEBUG("unit 0x%016Lx is open\n",
2448 unit->fcp_lun);
2449 retval = ZFCP_ERP_SUCCEEDED;
2450 } else {
2451 ZFCP_LOG_DEBUG("open failed for unit 0x%016Lx\n",
2452 unit->fcp_lun);
2453 retval = ZFCP_ERP_FAILED;
2454 }
2455 break;
2456 }
2457
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 return retval;
2459}
2460
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461static int
2462zfcp_erp_unit_strategy_clearstati(struct zfcp_unit *unit)
2463{
2464 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465
2466 atomic_clear_mask(ZFCP_STATUS_COMMON_OPENING |
2467 ZFCP_STATUS_COMMON_CLOSING |
2468 ZFCP_STATUS_COMMON_ACCESS_DENIED |
2469 ZFCP_STATUS_UNIT_SHARED |
2470 ZFCP_STATUS_UNIT_READONLY,
2471 &unit->status);
2472
2473 return retval;
2474}
2475
2476/*
Swen Schillig41fa2ad2007-09-07 09:15:31 +02002477 * function:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478 *
Swen Schillig41fa2ad2007-09-07 09:15:31 +02002479 * purpose:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480 *
2481 * returns: ZFCP_ERP_CONTINUES - action continues (asynchronously)
2482 * ZFCP_ERP_FAILED - action finished unsuccessfully
2483 */
2484static int
2485zfcp_erp_unit_strategy_close(struct zfcp_erp_action *erp_action)
2486{
2487 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489 retval = zfcp_fsf_close_unit(erp_action);
2490 if (retval == -ENOMEM) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491 retval = ZFCP_ERP_NOMEM;
2492 goto out;
2493 }
2494 erp_action->step = ZFCP_ERP_STEP_UNIT_CLOSING;
2495 if (retval != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496 /* could not send 'close', fail */
2497 retval = ZFCP_ERP_FAILED;
2498 goto out;
2499 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500 retval = ZFCP_ERP_CONTINUES;
2501
2502 out:
2503 return retval;
2504}
2505
2506/*
Swen Schillig41fa2ad2007-09-07 09:15:31 +02002507 * function:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508 *
Swen Schillig41fa2ad2007-09-07 09:15:31 +02002509 * purpose:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510 *
2511 * returns: ZFCP_ERP_CONTINUES - action continues (asynchronously)
2512 * ZFCP_ERP_FAILED - action finished unsuccessfully
2513 */
2514static int
2515zfcp_erp_unit_strategy_open(struct zfcp_erp_action *erp_action)
2516{
2517 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519 retval = zfcp_fsf_open_unit(erp_action);
2520 if (retval == -ENOMEM) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521 retval = ZFCP_ERP_NOMEM;
2522 goto out;
2523 }
2524 erp_action->step = ZFCP_ERP_STEP_UNIT_OPENING;
2525 if (retval != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526 /* could not send 'open', fail */
2527 retval = ZFCP_ERP_FAILED;
2528 goto out;
2529 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 retval = ZFCP_ERP_CONTINUES;
2531 out:
2532 return retval;
2533}
2534
Andreas Herrmann2abbe862006-09-18 22:29:56 +02002535void zfcp_erp_start_timer(struct zfcp_fsf_req *fsf_req)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536{
Andreas Herrmann2abbe862006-09-18 22:29:56 +02002537 BUG_ON(!fsf_req->erp_action);
2538 fsf_req->timer.function = zfcp_erp_timeout_handler;
2539 fsf_req->timer.data = (unsigned long) fsf_req->erp_action;
2540 fsf_req->timer.expires = jiffies + ZFCP_ERP_FSFREQ_TIMEOUT;
2541 add_timer(&fsf_req->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542}
2543
2544/*
Swen Schillig41fa2ad2007-09-07 09:15:31 +02002545 * function:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546 *
2547 * purpose: enqueue the specified error recovery action, if needed
2548 *
2549 * returns:
2550 */
Martin Peschke9467a9b2008-03-27 14:22:03 +01002551static int zfcp_erp_action_enqueue(int want, struct zfcp_adapter *adapter,
2552 struct zfcp_port *port,
Martin Peschke1f6f7122008-04-18 12:51:55 +02002553 struct zfcp_unit *unit, u8 id, void *ref)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554{
Martin Peschke9467a9b2008-03-27 14:22:03 +01002555 int retval = 1, need = want;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556 struct zfcp_erp_action *erp_action = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557 u32 status = 0;
2558
2559 /*
2560 * We need some rules here which check whether we really need
2561 * this action or whether we should just drop it.
2562 * E.g. if there is a unfinished 'Reopen Port' request then we drop a
2563 * 'Reopen Unit' request for an associated unit since we can't
2564 * satisfy this request now. A 'Reopen Port' action will trigger
2565 * 'Reopen Unit' actions when it completes.
2566 * Thus, there are only actions in the queue which can immediately be
2567 * executed. This makes the processing of the action queue more
2568 * efficient.
2569 */
2570
2571 if (!atomic_test_mask(ZFCP_STATUS_ADAPTER_ERP_THREAD_UP,
2572 &adapter->status))
2573 return -EIO;
2574
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575 /* check whether we really need this */
Martin Peschke9467a9b2008-03-27 14:22:03 +01002576 switch (want) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577 case ZFCP_ERP_ACTION_REOPEN_UNIT:
2578 if (atomic_test_mask
2579 (ZFCP_STATUS_COMMON_ERP_INUSE, &unit->status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580 goto out;
2581 }
2582 if (!atomic_test_mask
2583 (ZFCP_STATUS_COMMON_RUNNING, &port->status) ||
2584 atomic_test_mask
2585 (ZFCP_STATUS_COMMON_ERP_FAILED, &port->status)) {
2586 goto out;
2587 }
2588 if (!atomic_test_mask
Martin Peschke9467a9b2008-03-27 14:22:03 +01002589 (ZFCP_STATUS_COMMON_UNBLOCKED, &port->status))
2590 need = ZFCP_ERP_ACTION_REOPEN_PORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591 /* fall through !!! */
2592
2593 case ZFCP_ERP_ACTION_REOPEN_PORT:
2594 if (atomic_test_mask
2595 (ZFCP_STATUS_COMMON_ERP_INUSE, &port->status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596 goto out;
2597 }
2598 /* fall through !!! */
2599
2600 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
Andreas Herrmann3734d242005-09-13 21:47:11 +02002601 if (atomic_test_mask(ZFCP_STATUS_COMMON_ERP_INUSE,
2602 &port->status)) {
2603 if (port->erp_action.action !=
2604 ZFCP_ERP_ACTION_REOPEN_PORT_FORCED) {
2605 ZFCP_LOG_INFO("dropped erp action %i (port "
2606 "0x%016Lx, action in use: %i)\n",
Martin Peschke9467a9b2008-03-27 14:22:03 +01002607 want, port->wwpn,
Andreas Herrmann3734d242005-09-13 21:47:11 +02002608 port->erp_action.action);
Martin Peschke507e4962008-03-27 14:22:05 +01002609 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610 goto out;
2611 }
2612 if (!atomic_test_mask
2613 (ZFCP_STATUS_COMMON_RUNNING, &adapter->status) ||
2614 atomic_test_mask
2615 (ZFCP_STATUS_COMMON_ERP_FAILED, &adapter->status)) {
2616 goto out;
2617 }
2618 if (!atomic_test_mask
Martin Peschke9467a9b2008-03-27 14:22:03 +01002619 (ZFCP_STATUS_COMMON_UNBLOCKED, &adapter->status))
2620 need = ZFCP_ERP_ACTION_REOPEN_ADAPTER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621 /* fall through !!! */
2622
2623 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
2624 if (atomic_test_mask
2625 (ZFCP_STATUS_COMMON_ERP_INUSE, &adapter->status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626 goto out;
2627 }
2628 break;
2629
2630 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631 ZFCP_LOG_NORMAL("bug: unknown erp action requested "
2632 "on adapter %s (action=%d)\n",
Martin Peschke9467a9b2008-03-27 14:22:03 +01002633 zfcp_get_busid_by_adapter(adapter), want);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634 goto out;
2635 }
2636
2637 /* check whether we need something stronger first */
Martin Peschke9467a9b2008-03-27 14:22:03 +01002638 if (need) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639 ZFCP_LOG_DEBUG("stronger erp action %d needed before "
2640 "erp action %d on adapter %s\n",
Martin Peschke9467a9b2008-03-27 14:22:03 +01002641 need, want, zfcp_get_busid_by_adapter(adapter));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642 }
2643
2644 /* mark adapter to have some error recovery pending */
2645 atomic_set_mask(ZFCP_STATUS_ADAPTER_ERP_PENDING, &adapter->status);
2646
2647 /* setup error recovery action */
Martin Peschke9467a9b2008-03-27 14:22:03 +01002648 switch (need) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649
2650 case ZFCP_ERP_ACTION_REOPEN_UNIT:
2651 zfcp_unit_get(unit);
2652 atomic_set_mask(ZFCP_STATUS_COMMON_ERP_INUSE, &unit->status);
2653 erp_action = &unit->erp_action;
2654 if (!atomic_test_mask
2655 (ZFCP_STATUS_COMMON_RUNNING, &unit->status))
2656 status = ZFCP_STATUS_ERP_CLOSE_ONLY;
2657 break;
2658
2659 case ZFCP_ERP_ACTION_REOPEN_PORT:
2660 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
2661 zfcp_port_get(port);
2662 zfcp_erp_action_dismiss_port(port);
2663 atomic_set_mask(ZFCP_STATUS_COMMON_ERP_INUSE, &port->status);
2664 erp_action = &port->erp_action;
2665 if (!atomic_test_mask
2666 (ZFCP_STATUS_COMMON_RUNNING, &port->status))
2667 status = ZFCP_STATUS_ERP_CLOSE_ONLY;
2668 break;
2669
2670 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
2671 zfcp_adapter_get(adapter);
2672 zfcp_erp_action_dismiss_adapter(adapter);
2673 atomic_set_mask(ZFCP_STATUS_COMMON_ERP_INUSE, &adapter->status);
2674 erp_action = &adapter->erp_action;
2675 if (!atomic_test_mask
2676 (ZFCP_STATUS_COMMON_RUNNING, &adapter->status))
2677 status = ZFCP_STATUS_ERP_CLOSE_ONLY;
2678 break;
2679 }
2680
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681 memset(erp_action, 0, sizeof (struct zfcp_erp_action));
2682 erp_action->adapter = adapter;
2683 erp_action->port = port;
2684 erp_action->unit = unit;
Martin Peschke9467a9b2008-03-27 14:22:03 +01002685 erp_action->action = need;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686 erp_action->status = status;
2687
2688 ++adapter->erp_total_count;
2689
2690 /* finally put it into 'ready' queue and kick erp thread */
Martin Peschke86e8dfc2007-11-15 13:57:17 +01002691 list_add_tail(&erp_action->list, &adapter->erp_ready_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 up(&adapter->erp_ready_sem);
Christof Schmittaa0fec62008-05-19 12:17:47 +02002693 zfcp_rec_dbf_event_thread(1, adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694 retval = 0;
2695 out:
Martin Peschke1f6f7122008-04-18 12:51:55 +02002696 zfcp_rec_dbf_event_trigger(id, ref, want, need, erp_action,
Martin Peschke9467a9b2008-03-27 14:22:03 +01002697 adapter, port, unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698 return retval;
2699}
2700
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701static int
2702zfcp_erp_action_dequeue(struct zfcp_erp_action *erp_action)
2703{
2704 int retval = 0;
2705 struct zfcp_adapter *adapter = erp_action->adapter;
2706
2707 --adapter->erp_total_count;
2708 if (erp_action->status & ZFCP_STATUS_ERP_LOWMEM) {
2709 --adapter->erp_low_mem_count;
2710 erp_action->status &= ~ZFCP_STATUS_ERP_LOWMEM;
2711 }
2712
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713 list_del(&erp_action->list);
Martin Peschke6f4f3652008-03-27 14:22:04 +01002714 zfcp_rec_dbf_event_action(144, erp_action);
2715
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716 switch (erp_action->action) {
2717 case ZFCP_ERP_ACTION_REOPEN_UNIT:
2718 atomic_clear_mask(ZFCP_STATUS_COMMON_ERP_INUSE,
2719 &erp_action->unit->status);
2720 break;
2721 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
2722 case ZFCP_ERP_ACTION_REOPEN_PORT:
2723 atomic_clear_mask(ZFCP_STATUS_COMMON_ERP_INUSE,
2724 &erp_action->port->status);
2725 break;
2726 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
2727 atomic_clear_mask(ZFCP_STATUS_COMMON_ERP_INUSE,
2728 &erp_action->adapter->status);
2729 break;
2730 default:
2731 /* bug */
2732 break;
2733 }
2734 return retval;
2735}
2736
2737/**
2738 * zfcp_erp_action_cleanup
2739 *
Adrian Bunk575c9682006-01-15 02:00:17 +01002740 * Register unit with scsi stack if appropriate and fix reference counts.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741 * Note: Temporary units are not registered with scsi stack.
2742 */
2743static void
2744zfcp_erp_action_cleanup(int action, struct zfcp_adapter *adapter,
2745 struct zfcp_port *port, struct zfcp_unit *unit,
2746 int result)
2747{
2748 switch (action) {
2749 case ZFCP_ERP_ACTION_REOPEN_UNIT:
2750 if ((result == ZFCP_ERP_SUCCEEDED)
2751 && (!atomic_test_mask(ZFCP_STATUS_UNIT_TEMPORARY,
2752 &unit->status))
Andreas Herrmann3859f6a2005-08-27 11:07:54 -07002753 && !unit->device
Andreas Herrmannad58f7d2006-03-10 00:56:16 +01002754 && port->rport) {
2755 atomic_set_mask(ZFCP_STATUS_UNIT_REGISTERED,
2756 &unit->status);
Christof Schmitt5f852be2007-05-08 11:16:52 +02002757 if (atomic_test_mask(ZFCP_STATUS_UNIT_SCSI_WORK_PENDING,
2758 &unit->status) == 0)
2759 zfcp_erp_schedule_work(unit);
Andreas Herrmannad58f7d2006-03-10 00:56:16 +01002760 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761 zfcp_unit_put(unit);
2762 break;
2763 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
2764 case ZFCP_ERP_ACTION_REOPEN_PORT:
Andreas Herrmann338151e2006-05-22 18:25:56 +02002765 if (atomic_test_mask(ZFCP_STATUS_PORT_NO_WWPN,
2766 &port->status)) {
2767 zfcp_port_put(port);
2768 break;
2769 }
2770
Andreas Herrmann3859f6a2005-08-27 11:07:54 -07002771 if ((result == ZFCP_ERP_SUCCEEDED)
Andreas Herrmann3859f6a2005-08-27 11:07:54 -07002772 && !port->rport) {
2773 struct fc_rport_identifiers ids;
2774 ids.node_name = port->wwnn;
2775 ids.port_name = port->wwpn;
2776 ids.port_id = port->d_id;
2777 ids.roles = FC_RPORT_ROLE_FCP_TARGET;
2778 port->rport =
2779 fc_remote_port_add(adapter->scsi_host, 0, &ids);
2780 if (!port->rport)
2781 ZFCP_LOG_NORMAL("failed registration of rport"
2782 "(adapter %s, wwpn=0x%016Lx)\n",
2783 zfcp_get_busid_by_port(port),
2784 port->wwpn);
Ralph Wuerthner75bfc282006-05-22 18:24:33 +02002785 else {
Christof Schmitt5f852be2007-05-08 11:16:52 +02002786 scsi_target_unblock(&port->rport->dev);
Ralph Wuerthner75bfc282006-05-22 18:24:33 +02002787 port->rport->maxframe_size = port->maxframe_size;
2788 port->rport->supported_classes =
2789 port->supported_classes;
2790 }
Andreas Herrmann3859f6a2005-08-27 11:07:54 -07002791 }
Andreas Herrmann338151e2006-05-22 18:25:56 +02002792 if ((result != ZFCP_ERP_SUCCEEDED) && port->rport) {
2793 fc_remote_port_delete(port->rport);
2794 port->rport = NULL;
2795 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796 zfcp_port_put(port);
2797 break;
2798 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
Andreas Herrmann338151e2006-05-22 18:25:56 +02002799 if (result != ZFCP_ERP_SUCCEEDED) {
Andreas Herrmann338151e2006-05-22 18:25:56 +02002800 list_for_each_entry(port, &adapter->port_list_head, list)
2801 if (port->rport &&
2802 !atomic_test_mask(ZFCP_STATUS_PORT_WKA,
2803 &port->status)) {
2804 fc_remote_port_delete(port->rport);
2805 port->rport = NULL;
2806 }
2807 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808 zfcp_adapter_put(adapter);
2809 break;
2810 default:
2811 break;
2812 }
2813}
2814
2815
Andreas Herrmann81654282006-09-18 22:30:36 +02002816static void zfcp_erp_action_dismiss_adapter(struct zfcp_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818 struct zfcp_port *port;
2819
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820 if (atomic_test_mask(ZFCP_STATUS_COMMON_ERP_INUSE, &adapter->status))
2821 zfcp_erp_action_dismiss(&adapter->erp_action);
2822 else
2823 list_for_each_entry(port, &adapter->port_list_head, list)
2824 zfcp_erp_action_dismiss_port(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825}
2826
Andreas Herrmannf6c0e7a2006-08-02 11:05:52 +02002827static void zfcp_erp_action_dismiss_port(struct zfcp_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829 struct zfcp_unit *unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831 if (atomic_test_mask(ZFCP_STATUS_COMMON_ERP_INUSE, &port->status))
2832 zfcp_erp_action_dismiss(&port->erp_action);
2833 else
2834 list_for_each_entry(unit, &port->unit_list_head, list)
2835 zfcp_erp_action_dismiss_unit(unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836}
2837
Andreas Herrmannf6c0e7a2006-08-02 11:05:52 +02002838static void zfcp_erp_action_dismiss_unit(struct zfcp_unit *unit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840 if (atomic_test_mask(ZFCP_STATUS_COMMON_ERP_INUSE, &unit->status))
2841 zfcp_erp_action_dismiss(&unit->erp_action);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842}
2843
Heiko Carstens364c8552007-10-12 16:11:35 +02002844static void zfcp_erp_action_to_running(struct zfcp_erp_action *erp_action)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846 list_move(&erp_action->list, &erp_action->adapter->erp_running_head);
Martin Peschke6f4f3652008-03-27 14:22:04 +01002847 zfcp_rec_dbf_event_action(145, erp_action);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848}
2849
Heiko Carstens364c8552007-10-12 16:11:35 +02002850static void zfcp_erp_action_to_ready(struct zfcp_erp_action *erp_action)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852 list_move(&erp_action->list, &erp_action->adapter->erp_ready_head);
Martin Peschke6f4f3652008-03-27 14:22:04 +01002853 zfcp_rec_dbf_event_action(146, erp_action);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854}
2855
Martin Peschke1f6f7122008-04-18 12:51:55 +02002856void zfcp_erp_port_boxed(struct zfcp_port *port, u8 id, void *ref)
Andreas Herrmannd736a27b2005-06-13 13:23:57 +02002857{
Andreas Herrmannd736a27b2005-06-13 13:23:57 +02002858 unsigned long flags;
2859
Andreas Herrmannd736a27b2005-06-13 13:23:57 +02002860 read_lock_irqsave(&zfcp_data.config_lock, flags);
Martin Peschke698ec0162008-03-27 14:22:02 +01002861 zfcp_erp_modify_port_status(port, id, ref,
2862 ZFCP_STATUS_COMMON_ACCESS_BOXED, ZFCP_SET);
Andreas Herrmannd736a27b2005-06-13 13:23:57 +02002863 read_unlock_irqrestore(&zfcp_data.config_lock, flags);
Martin Peschke9467a9b2008-03-27 14:22:03 +01002864 zfcp_erp_port_reopen(port, ZFCP_STATUS_COMMON_ERP_FAILED, id, ref);
Andreas Herrmannd736a27b2005-06-13 13:23:57 +02002865}
2866
Martin Peschke1f6f7122008-04-18 12:51:55 +02002867void zfcp_erp_unit_boxed(struct zfcp_unit *unit, u8 id, void *ref)
Andreas Herrmannd736a27b2005-06-13 13:23:57 +02002868{
Martin Peschke698ec0162008-03-27 14:22:02 +01002869 zfcp_erp_modify_unit_status(unit, id, ref,
2870 ZFCP_STATUS_COMMON_ACCESS_BOXED, ZFCP_SET);
Martin Peschke9467a9b2008-03-27 14:22:03 +01002871 zfcp_erp_unit_reopen(unit, ZFCP_STATUS_COMMON_ERP_FAILED, id, ref);
Andreas Herrmannd736a27b2005-06-13 13:23:57 +02002872}
2873
Martin Peschke1f6f7122008-04-18 12:51:55 +02002874void zfcp_erp_port_access_denied(struct zfcp_port *port, u8 id, void *ref)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876 unsigned long flags;
2877
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878 read_lock_irqsave(&zfcp_data.config_lock, flags);
Martin Peschke698ec0162008-03-27 14:22:02 +01002879 zfcp_erp_modify_port_status(port, id, ref,
2880 ZFCP_STATUS_COMMON_ERP_FAILED |
2881 ZFCP_STATUS_COMMON_ACCESS_DENIED, ZFCP_SET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882 read_unlock_irqrestore(&zfcp_data.config_lock, flags);
2883}
2884
Martin Peschke1f6f7122008-04-18 12:51:55 +02002885void zfcp_erp_unit_access_denied(struct zfcp_unit *unit, u8 id, void *ref)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886{
Martin Peschke698ec0162008-03-27 14:22:02 +01002887 zfcp_erp_modify_unit_status(unit, id, ref,
2888 ZFCP_STATUS_COMMON_ERP_FAILED |
2889 ZFCP_STATUS_COMMON_ACCESS_DENIED, ZFCP_SET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890}
2891
Martin Peschke9467a9b2008-03-27 14:22:03 +01002892void zfcp_erp_adapter_access_changed(struct zfcp_adapter *adapter, u8 id,
Martin Peschke1f6f7122008-04-18 12:51:55 +02002893 void *ref)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894{
2895 struct zfcp_port *port;
2896 unsigned long flags;
2897
Maxim Shchetyninaef4a982005-09-13 21:51:16 +02002898 if (adapter->connection_features & FSF_FEATURE_NPIV_MODE)
2899 return;
2900
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901 read_lock_irqsave(&zfcp_data.config_lock, flags);
6f71d9b2005-04-10 23:04:28 -05002902 if (adapter->nameserver_port)
Martin Peschke9467a9b2008-03-27 14:22:03 +01002903 zfcp_erp_port_access_changed(adapter->nameserver_port, id, ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904 list_for_each_entry(port, &adapter->port_list_head, list)
2905 if (port != adapter->nameserver_port)
Martin Peschke9467a9b2008-03-27 14:22:03 +01002906 zfcp_erp_port_access_changed(port, id, ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907 read_unlock_irqrestore(&zfcp_data.config_lock, flags);
2908}
2909
Martin Peschke1f6f7122008-04-18 12:51:55 +02002910void zfcp_erp_port_access_changed(struct zfcp_port *port, u8 id, void *ref)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911{
2912 struct zfcp_adapter *adapter = port->adapter;
2913 struct zfcp_unit *unit;
2914
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915 if (!atomic_test_mask(ZFCP_STATUS_COMMON_ACCESS_DENIED,
Andreas Herrmannd736a27b2005-06-13 13:23:57 +02002916 &port->status) &&
2917 !atomic_test_mask(ZFCP_STATUS_COMMON_ACCESS_BOXED,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918 &port->status)) {
2919 if (!atomic_test_mask(ZFCP_STATUS_PORT_WKA, &port->status))
2920 list_for_each_entry(unit, &port->unit_list_head, list)
Martin Peschke9467a9b2008-03-27 14:22:03 +01002921 zfcp_erp_unit_access_changed(unit, id, ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922 return;
2923 }
2924
2925 ZFCP_LOG_NORMAL("reopen of port 0x%016Lx on adapter %s "
2926 "(due to ACT update)\n",
2927 port->wwpn, zfcp_get_busid_by_adapter(adapter));
Martin Peschke9467a9b2008-03-27 14:22:03 +01002928 if (zfcp_erp_port_reopen(port, ZFCP_STATUS_COMMON_ERP_FAILED, id, ref))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929 ZFCP_LOG_NORMAL("failed reopen of port"
2930 "(adapter %s, wwpn=0x%016Lx)\n",
2931 zfcp_get_busid_by_adapter(adapter), port->wwpn);
2932}
2933
Martin Peschke1f6f7122008-04-18 12:51:55 +02002934void zfcp_erp_unit_access_changed(struct zfcp_unit *unit, u8 id, void *ref)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935{
2936 struct zfcp_adapter *adapter = unit->port->adapter;
2937
Andreas Herrmannd736a27b2005-06-13 13:23:57 +02002938 if (!atomic_test_mask(ZFCP_STATUS_COMMON_ACCESS_DENIED,
2939 &unit->status) &&
2940 !atomic_test_mask(ZFCP_STATUS_COMMON_ACCESS_BOXED,
2941 &unit->status))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942 return;
2943
2944 ZFCP_LOG_NORMAL("reopen of unit 0x%016Lx on port 0x%016Lx "
2945 " on adapter %s (due to ACT update)\n",
2946 unit->fcp_lun, unit->port->wwpn,
2947 zfcp_get_busid_by_adapter(adapter));
Martin Peschke9467a9b2008-03-27 14:22:03 +01002948 if (zfcp_erp_unit_reopen(unit, ZFCP_STATUS_COMMON_ERP_FAILED, id, ref))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949 ZFCP_LOG_NORMAL("failed reopen of unit (adapter %s, "
2950 "wwpn=0x%016Lx, fcp_lun=0x%016Lx)\n",
2951 zfcp_get_busid_by_adapter(adapter),
2952 unit->port->wwpn, unit->fcp_lun);
2953}
2954
2955#undef ZFCP_LOG_AREA