blob: d37c7331f244f1463c154a05ec9649306fe0c358 [file] [log] [blame]
Swen Schillig41fa2ada2007-09-07 09:15:31 +02001/*
Christof Schmitt553448f2008-06-10 18:20:58 +02002 * zfcp device driver
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
Christof Schmitt553448f2008-06-10 18:20:58 +02004 * Error Recovery Procedures (ERP).
Swen Schillig41fa2ada2007-09-07 09:15:31 +02005 *
Christof Schmitt615f59e2010-02-17 11:18:56 +01006 * Copyright IBM Corporation 2002, 2010
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 */
8
Christof Schmittecf39d42008-12-25 13:39:53 +01009#define KMSG_COMPONENT "zfcp"
10#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
11
Christof Schmitt347c6a92009-08-18 15:43:25 +020012#include <linux/kthread.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include "zfcp_ext.h"
Christof Schmittb6bd2fb2010-02-17 11:18:50 +010014#include "zfcp_reqlist.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070015
Christof Schmitt287ac012008-07-02 10:56:40 +020016#define ZFCP_MAX_ERPS 3
Linus Torvalds1da177e2005-04-16 15:20:36 -070017
Christof Schmitt287ac012008-07-02 10:56:40 +020018enum zfcp_erp_act_flags {
19 ZFCP_STATUS_ERP_TIMEDOUT = 0x10000000,
20 ZFCP_STATUS_ERP_CLOSE_ONLY = 0x01000000,
21 ZFCP_STATUS_ERP_DISMISSING = 0x00100000,
22 ZFCP_STATUS_ERP_DISMISSED = 0x00200000,
23 ZFCP_STATUS_ERP_LOWMEM = 0x00400000,
Christof Schmittfdbd1c52010-09-08 14:39:54 +020024 ZFCP_STATUS_ERP_NO_REF = 0x00800000,
Christof Schmitt287ac012008-07-02 10:56:40 +020025};
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
Christof Schmitt287ac012008-07-02 10:56:40 +020027enum zfcp_erp_steps {
28 ZFCP_ERP_STEP_UNINITIALIZED = 0x0000,
29 ZFCP_ERP_STEP_FSF_XCONFIG = 0x0001,
30 ZFCP_ERP_STEP_PHYS_PORT_CLOSING = 0x0010,
31 ZFCP_ERP_STEP_PORT_CLOSING = 0x0100,
Christof Schmitt287ac012008-07-02 10:56:40 +020032 ZFCP_ERP_STEP_PORT_OPENING = 0x0800,
Christof Schmittb62a8d92010-09-08 14:39:55 +020033 ZFCP_ERP_STEP_LUN_CLOSING = 0x1000,
34 ZFCP_ERP_STEP_LUN_OPENING = 0x2000,
Christof Schmitt287ac012008-07-02 10:56:40 +020035};
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Christof Schmitt287ac012008-07-02 10:56:40 +020037enum zfcp_erp_act_type {
Christof Schmittb62a8d92010-09-08 14:39:55 +020038 ZFCP_ERP_ACTION_REOPEN_LUN = 1,
Christof Schmitt287ac012008-07-02 10:56:40 +020039 ZFCP_ERP_ACTION_REOPEN_PORT = 2,
40 ZFCP_ERP_ACTION_REOPEN_PORT_FORCED = 3,
41 ZFCP_ERP_ACTION_REOPEN_ADAPTER = 4,
42};
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Christof Schmitt287ac012008-07-02 10:56:40 +020044enum zfcp_erp_act_state {
45 ZFCP_ERP_ACTION_RUNNING = 1,
46 ZFCP_ERP_ACTION_READY = 2,
47};
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Christof Schmitt287ac012008-07-02 10:56:40 +020049enum zfcp_erp_act_result {
50 ZFCP_ERP_SUCCEEDED = 0,
51 ZFCP_ERP_FAILED = 1,
52 ZFCP_ERP_CONTINUES = 2,
53 ZFCP_ERP_EXIT = 3,
54 ZFCP_ERP_DISMISSED = 4,
55 ZFCP_ERP_NOMEM = 5,
56};
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Christof Schmitt287ac012008-07-02 10:56:40 +020058static void zfcp_erp_adapter_block(struct zfcp_adapter *adapter, int mask)
Andreas Herrmann2abbe862006-09-18 22:29:56 +020059{
Swen Schilligedaed852010-09-08 14:40:01 +020060 zfcp_erp_clear_adapter_status(adapter,
61 ZFCP_STATUS_COMMON_UNBLOCKED | mask);
Andreas Herrmann2abbe862006-09-18 22:29:56 +020062}
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Christof Schmitt287ac012008-07-02 10:56:40 +020064static int zfcp_erp_action_exists(struct zfcp_erp_action *act)
Linus Torvalds1da177e2005-04-16 15:20:36 -070065{
Christof Schmitt287ac012008-07-02 10:56:40 +020066 struct zfcp_erp_action *curr_act;
67
68 list_for_each_entry(curr_act, &act->adapter->erp_running_head, list)
69 if (act == curr_act)
70 return ZFCP_ERP_ACTION_RUNNING;
71 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072}
73
Christof Schmitt287ac012008-07-02 10:56:40 +020074static void zfcp_erp_action_ready(struct zfcp_erp_action *act)
Linus Torvalds1da177e2005-04-16 15:20:36 -070075{
Christof Schmitt287ac012008-07-02 10:56:40 +020076 struct zfcp_adapter *adapter = act->adapter;
77
78 list_move(&act->list, &act->adapter->erp_ready_head);
Swen Schillig57717102009-08-18 15:43:21 +020079 zfcp_dbf_rec_action("erardy1", act);
Christof Schmitt347c6a92009-08-18 15:43:25 +020080 wake_up(&adapter->erp_ready_wq);
Swen Schillig57717102009-08-18 15:43:21 +020081 zfcp_dbf_rec_thread("erardy2", adapter->dbf);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082}
83
Christof Schmitt287ac012008-07-02 10:56:40 +020084static void zfcp_erp_action_dismiss(struct zfcp_erp_action *act)
Linus Torvalds1da177e2005-04-16 15:20:36 -070085{
Christof Schmitt287ac012008-07-02 10:56:40 +020086 act->status |= ZFCP_STATUS_ERP_DISMISSED;
87 if (zfcp_erp_action_exists(act) == ZFCP_ERP_ACTION_RUNNING)
88 zfcp_erp_action_ready(act);
89}
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
Christof Schmittb62a8d92010-09-08 14:39:55 +020091static void zfcp_erp_action_dismiss_lun(struct scsi_device *sdev)
Christof Schmitt287ac012008-07-02 10:56:40 +020092{
Christof Schmittb62a8d92010-09-08 14:39:55 +020093 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
94
95 if (atomic_read(&zfcp_sdev->status) & ZFCP_STATUS_COMMON_ERP_INUSE)
96 zfcp_erp_action_dismiss(&zfcp_sdev->erp_action);
Christof Schmitt287ac012008-07-02 10:56:40 +020097}
98
99static void zfcp_erp_action_dismiss_port(struct zfcp_port *port)
100{
Christof Schmittb62a8d92010-09-08 14:39:55 +0200101 struct scsi_device *sdev;
Christof Schmitt287ac012008-07-02 10:56:40 +0200102
103 if (atomic_read(&port->status) & ZFCP_STATUS_COMMON_ERP_INUSE)
104 zfcp_erp_action_dismiss(&port->erp_action);
Christof Schmittb62a8d92010-09-08 14:39:55 +0200105 else
106 shost_for_each_device(sdev, port->adapter->scsi_host)
107 if (sdev_to_zfcp(sdev)->port == port)
108 zfcp_erp_action_dismiss_lun(sdev);
Christof Schmitt287ac012008-07-02 10:56:40 +0200109}
110
111static void zfcp_erp_action_dismiss_adapter(struct zfcp_adapter *adapter)
112{
113 struct zfcp_port *port;
114
115 if (atomic_read(&adapter->status) & ZFCP_STATUS_COMMON_ERP_INUSE)
116 zfcp_erp_action_dismiss(&adapter->erp_action);
Swen Schilligecf0c772009-11-24 16:53:58 +0100117 else {
118 read_lock(&adapter->port_list_lock);
119 list_for_each_entry(port, &adapter->port_list, list)
Christof Schmitt287ac012008-07-02 10:56:40 +0200120 zfcp_erp_action_dismiss_port(port);
Swen Schilligecf0c772009-11-24 16:53:58 +0100121 read_unlock(&adapter->port_list_lock);
122 }
Christof Schmitt287ac012008-07-02 10:56:40 +0200123}
124
125static int zfcp_erp_required_act(int want, struct zfcp_adapter *adapter,
126 struct zfcp_port *port,
Christof Schmittb62a8d92010-09-08 14:39:55 +0200127 struct scsi_device *sdev)
Christof Schmitt287ac012008-07-02 10:56:40 +0200128{
129 int need = want;
Christof Schmittb62a8d92010-09-08 14:39:55 +0200130 int l_status, p_status, a_status;
131 struct zfcp_scsi_dev *zfcp_sdev;
Christof Schmitt287ac012008-07-02 10:56:40 +0200132
133 switch (want) {
Christof Schmittb62a8d92010-09-08 14:39:55 +0200134 case ZFCP_ERP_ACTION_REOPEN_LUN:
135 zfcp_sdev = sdev_to_zfcp(sdev);
136 l_status = atomic_read(&zfcp_sdev->status);
137 if (l_status & ZFCP_STATUS_COMMON_ERP_INUSE)
Christof Schmitt287ac012008-07-02 10:56:40 +0200138 return 0;
139 p_status = atomic_read(&port->status);
140 if (!(p_status & ZFCP_STATUS_COMMON_RUNNING) ||
141 p_status & ZFCP_STATUS_COMMON_ERP_FAILED)
142 return 0;
143 if (!(p_status & ZFCP_STATUS_COMMON_UNBLOCKED))
144 need = ZFCP_ERP_ACTION_REOPEN_PORT;
145 /* fall through */
Christof Schmitt287ac012008-07-02 10:56:40 +0200146 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
147 p_status = atomic_read(&port->status);
Christof Schmitt097ef3b2010-07-08 09:53:06 +0200148 if (!(p_status & ZFCP_STATUS_COMMON_OPEN))
149 need = ZFCP_ERP_ACTION_REOPEN_PORT;
150 /* fall through */
151 case ZFCP_ERP_ACTION_REOPEN_PORT:
152 p_status = atomic_read(&port->status);
Christof Schmitt287ac012008-07-02 10:56:40 +0200153 if (p_status & ZFCP_STATUS_COMMON_ERP_INUSE)
154 return 0;
155 a_status = atomic_read(&adapter->status);
156 if (!(a_status & ZFCP_STATUS_COMMON_RUNNING) ||
157 a_status & ZFCP_STATUS_COMMON_ERP_FAILED)
158 return 0;
159 if (!(a_status & ZFCP_STATUS_COMMON_UNBLOCKED))
160 need = ZFCP_ERP_ACTION_REOPEN_ADAPTER;
161 /* fall through */
162 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
163 a_status = atomic_read(&adapter->status);
164 if (a_status & ZFCP_STATUS_COMMON_ERP_INUSE)
165 return 0;
Christof Schmitt143bb6b2009-08-18 15:43:27 +0200166 if (!(a_status & ZFCP_STATUS_COMMON_RUNNING) &&
167 !(a_status & ZFCP_STATUS_COMMON_OPEN))
168 return 0; /* shutdown requested for closed adapter */
Christof Schmitt287ac012008-07-02 10:56:40 +0200169 }
170
171 return need;
172}
173
Christof Schmittfdbd1c52010-09-08 14:39:54 +0200174static struct zfcp_erp_action *zfcp_erp_setup_act(int need, u32 act_status,
Christof Schmitt287ac012008-07-02 10:56:40 +0200175 struct zfcp_adapter *adapter,
176 struct zfcp_port *port,
Christof Schmittb62a8d92010-09-08 14:39:55 +0200177 struct scsi_device *sdev)
Christof Schmitt287ac012008-07-02 10:56:40 +0200178{
179 struct zfcp_erp_action *erp_action;
Christof Schmittb62a8d92010-09-08 14:39:55 +0200180 struct zfcp_scsi_dev *zfcp_sdev;
Christof Schmitt287ac012008-07-02 10:56:40 +0200181
182 switch (need) {
Christof Schmittb62a8d92010-09-08 14:39:55 +0200183 case ZFCP_ERP_ACTION_REOPEN_LUN:
184 zfcp_sdev = sdev_to_zfcp(sdev);
Christof Schmittfdbd1c52010-09-08 14:39:54 +0200185 if (!(act_status & ZFCP_STATUS_ERP_NO_REF))
Christof Schmittb62a8d92010-09-08 14:39:55 +0200186 if (scsi_device_get(sdev))
Christof Schmittfdbd1c52010-09-08 14:39:54 +0200187 return NULL;
Christof Schmittb62a8d92010-09-08 14:39:55 +0200188 atomic_set_mask(ZFCP_STATUS_COMMON_ERP_INUSE,
189 &zfcp_sdev->status);
190 erp_action = &zfcp_sdev->erp_action;
191 if (!(atomic_read(&zfcp_sdev->status) &
192 ZFCP_STATUS_COMMON_RUNNING))
Christof Schmittfdbd1c52010-09-08 14:39:54 +0200193 act_status |= ZFCP_STATUS_ERP_CLOSE_ONLY;
Christof Schmitt287ac012008-07-02 10:56:40 +0200194 break;
195
196 case ZFCP_ERP_ACTION_REOPEN_PORT:
197 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
Christof Schmitt615f59e2010-02-17 11:18:56 +0100198 if (!get_device(&port->dev))
Swen Schillig6b1833342009-11-24 16:54:05 +0100199 return NULL;
Christof Schmitt287ac012008-07-02 10:56:40 +0200200 zfcp_erp_action_dismiss_port(port);
201 atomic_set_mask(ZFCP_STATUS_COMMON_ERP_INUSE, &port->status);
202 erp_action = &port->erp_action;
203 if (!(atomic_read(&port->status) & ZFCP_STATUS_COMMON_RUNNING))
Christof Schmittfdbd1c52010-09-08 14:39:54 +0200204 act_status |= ZFCP_STATUS_ERP_CLOSE_ONLY;
Christof Schmitt287ac012008-07-02 10:56:40 +0200205 break;
206
207 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
Swen Schilligf3450c72009-11-24 16:53:59 +0100208 kref_get(&adapter->ref);
Christof Schmitt287ac012008-07-02 10:56:40 +0200209 zfcp_erp_action_dismiss_adapter(adapter);
210 atomic_set_mask(ZFCP_STATUS_COMMON_ERP_INUSE, &adapter->status);
211 erp_action = &adapter->erp_action;
212 if (!(atomic_read(&adapter->status) &
213 ZFCP_STATUS_COMMON_RUNNING))
Christof Schmittfdbd1c52010-09-08 14:39:54 +0200214 act_status |= ZFCP_STATUS_ERP_CLOSE_ONLY;
Christof Schmitt287ac012008-07-02 10:56:40 +0200215 break;
216
217 default:
218 return NULL;
219 }
220
221 memset(erp_action, 0, sizeof(struct zfcp_erp_action));
222 erp_action->adapter = adapter;
223 erp_action->port = port;
Christof Schmittb62a8d92010-09-08 14:39:55 +0200224 erp_action->sdev = sdev;
Christof Schmitt287ac012008-07-02 10:56:40 +0200225 erp_action->action = need;
Christof Schmittfdbd1c52010-09-08 14:39:54 +0200226 erp_action->status = act_status;
Christof Schmitt287ac012008-07-02 10:56:40 +0200227
228 return erp_action;
229}
230
231static int zfcp_erp_action_enqueue(int want, struct zfcp_adapter *adapter,
232 struct zfcp_port *port,
Christof Schmittb62a8d92010-09-08 14:39:55 +0200233 struct scsi_device *sdev,
234 char *id, void *ref, u32 act_status)
Christof Schmitt287ac012008-07-02 10:56:40 +0200235{
236 int retval = 1, need;
237 struct zfcp_erp_action *act = NULL;
238
Christof Schmitt347c6a92009-08-18 15:43:25 +0200239 if (!adapter->erp_thread)
Christof Schmitt287ac012008-07-02 10:56:40 +0200240 return -EIO;
241
Christof Schmittb62a8d92010-09-08 14:39:55 +0200242 need = zfcp_erp_required_act(want, adapter, port, sdev);
Christof Schmitt287ac012008-07-02 10:56:40 +0200243 if (!need)
244 goto out;
245
Christof Schmittb62a8d92010-09-08 14:39:55 +0200246 act = zfcp_erp_setup_act(need, act_status, adapter, port, sdev);
Christof Schmitt287ac012008-07-02 10:56:40 +0200247 if (!act)
248 goto out;
Christof Schmittfdbd1c52010-09-08 14:39:54 +0200249 atomic_set_mask(ZFCP_STATUS_ADAPTER_ERP_PENDING, &adapter->status);
Christof Schmitt287ac012008-07-02 10:56:40 +0200250 ++adapter->erp_total_count;
251 list_add_tail(&act->list, &adapter->erp_ready_head);
Christof Schmitt347c6a92009-08-18 15:43:25 +0200252 wake_up(&adapter->erp_ready_wq);
Swen Schillig57717102009-08-18 15:43:21 +0200253 zfcp_dbf_rec_thread("eracte1", adapter->dbf);
Christof Schmitt287ac012008-07-02 10:56:40 +0200254 retval = 0;
255 out:
Christof Schmittb62a8d92010-09-08 14:39:55 +0200256 zfcp_dbf_rec_trigger(id, ref, want, need, act, adapter, port, sdev);
Christof Schmitt287ac012008-07-02 10:56:40 +0200257 return retval;
258}
259
260static int _zfcp_erp_adapter_reopen(struct zfcp_adapter *adapter,
Swen Schillig5ffd51a2009-03-02 13:09:04 +0100261 int clear_mask, char *id, void *ref)
Christof Schmitt287ac012008-07-02 10:56:40 +0200262{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 zfcp_erp_adapter_block(adapter, clear_mask);
Christof Schmitta2fa0ae2009-03-02 13:09:08 +0100264 zfcp_scsi_schedule_rports_block(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
Christof Schmitt287ac012008-07-02 10:56:40 +0200266 /* ensure propagation of failed status to new devices */
267 if (atomic_read(&adapter->status) & ZFCP_STATUS_COMMON_ERP_FAILED) {
Swen Schilligedaed852010-09-08 14:40:01 +0200268 zfcp_erp_set_adapter_status(adapter,
269 ZFCP_STATUS_COMMON_ERP_FAILED);
Christof Schmitt287ac012008-07-02 10:56:40 +0200270 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 }
Christof Schmitt287ac012008-07-02 10:56:40 +0200272 return zfcp_erp_action_enqueue(ZFCP_ERP_ACTION_REOPEN_ADAPTER,
Christof Schmittfdbd1c52010-09-08 14:39:54 +0200273 adapter, NULL, NULL, id, ref, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274}
275
276/**
Christof Schmitt287ac012008-07-02 10:56:40 +0200277 * zfcp_erp_adapter_reopen - Reopen adapter.
278 * @adapter: Adapter to reopen.
279 * @clear: Status flags to clear.
280 * @id: Id for debug trace event.
281 * @ref: Reference for debug trace event.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 */
Christof Schmitt287ac012008-07-02 10:56:40 +0200283void zfcp_erp_adapter_reopen(struct zfcp_adapter *adapter, int clear,
Swen Schillig5ffd51a2009-03-02 13:09:04 +0100284 char *id, void *ref)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285{
Christof Schmitt287ac012008-07-02 10:56:40 +0200286 unsigned long flags;
287
Swen Schilligecf0c772009-11-24 16:53:58 +0100288 zfcp_erp_adapter_block(adapter, clear);
289 zfcp_scsi_schedule_rports_block(adapter);
290
291 write_lock_irqsave(&adapter->erp_lock, flags);
292 if (atomic_read(&adapter->status) & ZFCP_STATUS_COMMON_ERP_FAILED)
Swen Schilligedaed852010-09-08 14:40:01 +0200293 zfcp_erp_set_adapter_status(adapter,
294 ZFCP_STATUS_COMMON_ERP_FAILED);
Swen Schilligecf0c772009-11-24 16:53:58 +0100295 else
296 zfcp_erp_action_enqueue(ZFCP_ERP_ACTION_REOPEN_ADAPTER, adapter,
Christof Schmittfdbd1c52010-09-08 14:39:54 +0200297 NULL, NULL, id, ref, 0);
Swen Schilligecf0c772009-11-24 16:53:58 +0100298 write_unlock_irqrestore(&adapter->erp_lock, flags);
Christof Schmitt287ac012008-07-02 10:56:40 +0200299}
300
301/**
302 * zfcp_erp_adapter_shutdown - Shutdown adapter.
303 * @adapter: Adapter to shut down.
304 * @clear: Status flags to clear.
305 * @id: Id for debug trace event.
306 * @ref: Reference for debug trace event.
307 */
308void zfcp_erp_adapter_shutdown(struct zfcp_adapter *adapter, int clear,
Swen Schillig5ffd51a2009-03-02 13:09:04 +0100309 char *id, void *ref)
Christof Schmitt287ac012008-07-02 10:56:40 +0200310{
311 int flags = ZFCP_STATUS_COMMON_RUNNING | ZFCP_STATUS_COMMON_ERP_FAILED;
312 zfcp_erp_adapter_reopen(adapter, clear | flags, id, ref);
313}
314
315/**
316 * zfcp_erp_port_shutdown - Shutdown port
317 * @port: Port to shut down.
318 * @clear: Status flags to clear.
319 * @id: Id for debug trace event.
320 * @ref: Reference for debug trace event.
321 */
Swen Schillig5ffd51a2009-03-02 13:09:04 +0100322void zfcp_erp_port_shutdown(struct zfcp_port *port, int clear, char *id,
323 void *ref)
Christof Schmitt287ac012008-07-02 10:56:40 +0200324{
325 int flags = ZFCP_STATUS_COMMON_RUNNING | ZFCP_STATUS_COMMON_ERP_FAILED;
326 zfcp_erp_port_reopen(port, clear | flags, id, ref);
327}
328
Christof Schmitt287ac012008-07-02 10:56:40 +0200329static void zfcp_erp_port_block(struct zfcp_port *port, int clear)
330{
Swen Schilligedaed852010-09-08 14:40:01 +0200331 zfcp_erp_clear_port_status(port,
332 ZFCP_STATUS_COMMON_UNBLOCKED | clear);
Christof Schmitt287ac012008-07-02 10:56:40 +0200333}
334
335static void _zfcp_erp_port_forced_reopen(struct zfcp_port *port,
Swen Schillig5ffd51a2009-03-02 13:09:04 +0100336 int clear, char *id, void *ref)
Christof Schmitt287ac012008-07-02 10:56:40 +0200337{
338 zfcp_erp_port_block(port, clear);
Christof Schmitta2fa0ae2009-03-02 13:09:08 +0100339 zfcp_scsi_schedule_rport_block(port);
Christof Schmitt287ac012008-07-02 10:56:40 +0200340
341 if (atomic_read(&port->status) & ZFCP_STATUS_COMMON_ERP_FAILED)
342 return;
343
344 zfcp_erp_action_enqueue(ZFCP_ERP_ACTION_REOPEN_PORT_FORCED,
Christof Schmittfdbd1c52010-09-08 14:39:54 +0200345 port->adapter, port, NULL, id, ref, 0);
Christof Schmitt287ac012008-07-02 10:56:40 +0200346}
347
348/**
349 * zfcp_erp_port_forced_reopen - Forced close of port and open again
350 * @port: Port to force close and to reopen.
351 * @id: Id for debug trace event.
352 * @ref: Reference for debug trace event.
353 */
Swen Schillig5ffd51a2009-03-02 13:09:04 +0100354void zfcp_erp_port_forced_reopen(struct zfcp_port *port, int clear, char *id,
Christof Schmitt287ac012008-07-02 10:56:40 +0200355 void *ref)
356{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 unsigned long flags;
358 struct zfcp_adapter *adapter = port->adapter;
359
Swen Schilligecf0c772009-11-24 16:53:58 +0100360 write_lock_irqsave(&adapter->erp_lock, flags);
Christof Schmitt287ac012008-07-02 10:56:40 +0200361 _zfcp_erp_port_forced_reopen(port, clear, id, ref);
Swen Schilligecf0c772009-11-24 16:53:58 +0100362 write_unlock_irqrestore(&adapter->erp_lock, flags);
Christof Schmitt287ac012008-07-02 10:56:40 +0200363}
364
Swen Schillig5ffd51a2009-03-02 13:09:04 +0100365static int _zfcp_erp_port_reopen(struct zfcp_port *port, int clear, char *id,
Christof Schmitt287ac012008-07-02 10:56:40 +0200366 void *ref)
367{
368 zfcp_erp_port_block(port, clear);
Christof Schmitta2fa0ae2009-03-02 13:09:08 +0100369 zfcp_scsi_schedule_rport_block(port);
Christof Schmitt287ac012008-07-02 10:56:40 +0200370
371 if (atomic_read(&port->status) & ZFCP_STATUS_COMMON_ERP_FAILED) {
372 /* ensure propagation of failed status to new devices */
Swen Schilligedaed852010-09-08 14:40:01 +0200373 zfcp_erp_set_port_status(port, ZFCP_STATUS_COMMON_ERP_FAILED);
Christof Schmitt287ac012008-07-02 10:56:40 +0200374 return -EIO;
375 }
376
377 return zfcp_erp_action_enqueue(ZFCP_ERP_ACTION_REOPEN_PORT,
Christof Schmittfdbd1c52010-09-08 14:39:54 +0200378 port->adapter, port, NULL, id, ref, 0);
Christof Schmitt287ac012008-07-02 10:56:40 +0200379}
380
381/**
382 * zfcp_erp_port_reopen - trigger remote port recovery
383 * @port: port to recover
384 * @clear_mask: flags in port status to be cleared
385 *
386 * Returns 0 if recovery has been triggered, < 0 if not.
387 */
Swen Schillig5ffd51a2009-03-02 13:09:04 +0100388int zfcp_erp_port_reopen(struct zfcp_port *port, int clear, char *id, void *ref)
Christof Schmitt287ac012008-07-02 10:56:40 +0200389{
Christof Schmitt287ac012008-07-02 10:56:40 +0200390 int retval;
Swen Schilligecf0c772009-11-24 16:53:58 +0100391 unsigned long flags;
Christof Schmitt287ac012008-07-02 10:56:40 +0200392 struct zfcp_adapter *adapter = port->adapter;
393
Swen Schilligecf0c772009-11-24 16:53:58 +0100394 write_lock_irqsave(&adapter->erp_lock, flags);
Christof Schmitt287ac012008-07-02 10:56:40 +0200395 retval = _zfcp_erp_port_reopen(port, clear, id, ref);
Swen Schilligecf0c772009-11-24 16:53:58 +0100396 write_unlock_irqrestore(&adapter->erp_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
398 return retval;
399}
400
Christof Schmittb62a8d92010-09-08 14:39:55 +0200401static void zfcp_erp_lun_block(struct scsi_device *sdev, int clear_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402{
Swen Schilligedaed852010-09-08 14:40:01 +0200403 zfcp_erp_clear_lun_status(sdev,
404 ZFCP_STATUS_COMMON_UNBLOCKED | clear_mask);
Christof Schmitt287ac012008-07-02 10:56:40 +0200405}
406
Christof Schmittb62a8d92010-09-08 14:39:55 +0200407static void _zfcp_erp_lun_reopen(struct scsi_device *sdev, int clear, char *id,
408 void *ref, u32 act_status)
Christof Schmitt287ac012008-07-02 10:56:40 +0200409{
Christof Schmittb62a8d92010-09-08 14:39:55 +0200410 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
411 struct zfcp_adapter *adapter = zfcp_sdev->port->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
Christof Schmittb62a8d92010-09-08 14:39:55 +0200413 zfcp_erp_lun_block(sdev, clear);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
Christof Schmittb62a8d92010-09-08 14:39:55 +0200415 if (atomic_read(&zfcp_sdev->status) & ZFCP_STATUS_COMMON_ERP_FAILED)
Christof Schmitt287ac012008-07-02 10:56:40 +0200416 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
Christof Schmittb62a8d92010-09-08 14:39:55 +0200418 zfcp_erp_action_enqueue(ZFCP_ERP_ACTION_REOPEN_LUN, adapter,
419 zfcp_sdev->port, sdev, id, ref, act_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420}
421
422/**
Christof Schmittb62a8d92010-09-08 14:39:55 +0200423 * zfcp_erp_lun_reopen - initiate reopen of a LUN
424 * @sdev: SCSI device / LUN to be reopened
425 * @clear_mask: specifies flags in LUN status to be cleared
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 * Return: 0 on success, < 0 on error
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 */
Christof Schmittb62a8d92010-09-08 14:39:55 +0200428void zfcp_erp_lun_reopen(struct scsi_device *sdev, int clear, char *id,
429 void *ref)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 unsigned long flags;
Christof Schmittb62a8d92010-09-08 14:39:55 +0200432 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
433 struct zfcp_port *port = zfcp_sdev->port;
Christof Schmitt287ac012008-07-02 10:56:40 +0200434 struct zfcp_adapter *adapter = port->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
Swen Schilligecf0c772009-11-24 16:53:58 +0100436 write_lock_irqsave(&adapter->erp_lock, flags);
Christof Schmittb62a8d92010-09-08 14:39:55 +0200437 _zfcp_erp_lun_reopen(sdev, clear, id, ref, 0);
Swen Schilligecf0c772009-11-24 16:53:58 +0100438 write_unlock_irqrestore(&adapter->erp_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439}
440
Christof Schmittfdbd1c52010-09-08 14:39:54 +0200441/**
Christof Schmittb62a8d92010-09-08 14:39:55 +0200442 * zfcp_erp_lun_shutdown - Shutdown LUN
443 * @sdev: SCSI device / LUN to shut down.
Christof Schmittfdbd1c52010-09-08 14:39:54 +0200444 * @clear: Status flags to clear.
445 * @id: Id for debug trace event.
446 * @ref: Reference for debug trace event.
447 */
Christof Schmittb62a8d92010-09-08 14:39:55 +0200448void zfcp_erp_lun_shutdown(struct scsi_device *sdev, int clear, char *id,
449 void *ref)
Christof Schmittfdbd1c52010-09-08 14:39:54 +0200450{
451 int flags = ZFCP_STATUS_COMMON_RUNNING | ZFCP_STATUS_COMMON_ERP_FAILED;
Christof Schmittb62a8d92010-09-08 14:39:55 +0200452 zfcp_erp_lun_reopen(sdev, clear | flags, id, ref);
Christof Schmittfdbd1c52010-09-08 14:39:54 +0200453}
454
455/**
Christof Schmittb62a8d92010-09-08 14:39:55 +0200456 * zfcp_erp_lun_shutdown_wait - Shutdown LUN and wait for erp completion
457 * @sdev: SCSI device / LUN to shut down.
Christof Schmittfdbd1c52010-09-08 14:39:54 +0200458 * @id: Id for debug trace event.
459 *
Christof Schmittb62a8d92010-09-08 14:39:55 +0200460 * Do not acquire a reference for the LUN when creating the ERP
Christof Schmittfdbd1c52010-09-08 14:39:54 +0200461 * action. It is safe, because this function waits for the ERP to
Christof Schmittb62a8d92010-09-08 14:39:55 +0200462 * complete first. This allows to shutdown the LUN, even when the SCSI
463 * device is in the state SDEV_DEL when scsi_device_get will fail.
Christof Schmittfdbd1c52010-09-08 14:39:54 +0200464 */
Christof Schmittb62a8d92010-09-08 14:39:55 +0200465void zfcp_erp_lun_shutdown_wait(struct scsi_device *sdev, char *id)
Christof Schmittfdbd1c52010-09-08 14:39:54 +0200466{
467 unsigned long flags;
Christof Schmittb62a8d92010-09-08 14:39:55 +0200468 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
469 struct zfcp_port *port = zfcp_sdev->port;
Christof Schmittfdbd1c52010-09-08 14:39:54 +0200470 struct zfcp_adapter *adapter = port->adapter;
471 int clear = ZFCP_STATUS_COMMON_RUNNING | ZFCP_STATUS_COMMON_ERP_FAILED;
472
473 write_lock_irqsave(&adapter->erp_lock, flags);
Christof Schmittb62a8d92010-09-08 14:39:55 +0200474 _zfcp_erp_lun_reopen(sdev, clear, id, NULL, ZFCP_STATUS_ERP_NO_REF);
Christof Schmittfdbd1c52010-09-08 14:39:54 +0200475 write_unlock_irqrestore(&adapter->erp_lock, flags);
476
477 zfcp_erp_wait(adapter);
478}
479
Christof Schmitt287ac012008-07-02 10:56:40 +0200480static int status_change_set(unsigned long mask, atomic_t *status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481{
Christof Schmitt287ac012008-07-02 10:56:40 +0200482 return (atomic_read(status) ^ mask) & mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483}
484
Andreas Herrmannf6c0e7a2006-08-02 11:05:52 +0200485static void zfcp_erp_adapter_unblock(struct zfcp_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486{
Christof Schmitt287ac012008-07-02 10:56:40 +0200487 if (status_change_set(ZFCP_STATUS_COMMON_UNBLOCKED, &adapter->status))
Swen Schillig57717102009-08-18 15:43:21 +0200488 zfcp_dbf_rec_adapter("eraubl1", NULL, adapter->dbf);
Christof Schmitt287ac012008-07-02 10:56:40 +0200489 atomic_set_mask(ZFCP_STATUS_COMMON_UNBLOCKED, &adapter->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490}
491
Christof Schmitt287ac012008-07-02 10:56:40 +0200492static void zfcp_erp_port_unblock(struct zfcp_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493{
Christof Schmitt287ac012008-07-02 10:56:40 +0200494 if (status_change_set(ZFCP_STATUS_COMMON_UNBLOCKED, &port->status))
Swen Schillig57717102009-08-18 15:43:21 +0200495 zfcp_dbf_rec_port("erpubl1", NULL, port);
Christof Schmitt287ac012008-07-02 10:56:40 +0200496 atomic_set_mask(ZFCP_STATUS_COMMON_UNBLOCKED, &port->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497}
498
Christof Schmittb62a8d92010-09-08 14:39:55 +0200499static void zfcp_erp_lun_unblock(struct scsi_device *sdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500{
Christof Schmittb62a8d92010-09-08 14:39:55 +0200501 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
502
503 if (status_change_set(ZFCP_STATUS_COMMON_UNBLOCKED, &zfcp_sdev->status))
504 zfcp_dbf_rec_lun("erlubl1", NULL, sdev);
505 atomic_set_mask(ZFCP_STATUS_COMMON_UNBLOCKED, &zfcp_sdev->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506}
507
Christof Schmitt287ac012008-07-02 10:56:40 +0200508static void zfcp_erp_action_to_running(struct zfcp_erp_action *erp_action)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509{
Christof Schmitt287ac012008-07-02 10:56:40 +0200510 list_move(&erp_action->list, &erp_action->adapter->erp_running_head);
Swen Schillig57717102009-08-18 15:43:21 +0200511 zfcp_dbf_rec_action("erator1", erp_action);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512}
513
Christof Schmitt287ac012008-07-02 10:56:40 +0200514static void zfcp_erp_strategy_check_fsfreq(struct zfcp_erp_action *act)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515{
Christof Schmitt287ac012008-07-02 10:56:40 +0200516 struct zfcp_adapter *adapter = act->adapter;
Christof Schmitte60a6d62010-02-17 11:18:49 +0100517 struct zfcp_fsf_req *req;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
Christof Schmitte60a6d62010-02-17 11:18:49 +0100519 if (!act->fsf_req_id)
Christof Schmitt287ac012008-07-02 10:56:40 +0200520 return;
521
Christof Schmittb6bd2fb2010-02-17 11:18:50 +0100522 spin_lock(&adapter->req_list->lock);
523 req = _zfcp_reqlist_find(adapter->req_list, act->fsf_req_id);
Christof Schmitte60a6d62010-02-17 11:18:49 +0100524 if (req && req->erp_action == act) {
Christof Schmitt287ac012008-07-02 10:56:40 +0200525 if (act->status & (ZFCP_STATUS_ERP_DISMISSED |
526 ZFCP_STATUS_ERP_TIMEDOUT)) {
Christof Schmitte60a6d62010-02-17 11:18:49 +0100527 req->status |= ZFCP_STATUS_FSFREQ_DISMISSED;
Swen Schillig57717102009-08-18 15:43:21 +0200528 zfcp_dbf_rec_action("erscf_1", act);
Christof Schmitte60a6d62010-02-17 11:18:49 +0100529 req->erp_action = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 }
Christof Schmitt287ac012008-07-02 10:56:40 +0200531 if (act->status & ZFCP_STATUS_ERP_TIMEDOUT)
Swen Schillig57717102009-08-18 15:43:21 +0200532 zfcp_dbf_rec_action("erscf_2", act);
Christof Schmitte60a6d62010-02-17 11:18:49 +0100533 if (req->status & ZFCP_STATUS_FSFREQ_DISMISSED)
534 act->fsf_req_id = 0;
Christof Schmitt287ac012008-07-02 10:56:40 +0200535 } else
Christof Schmitte60a6d62010-02-17 11:18:49 +0100536 act->fsf_req_id = 0;
Christof Schmittb6bd2fb2010-02-17 11:18:50 +0100537 spin_unlock(&adapter->req_list->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538}
539
Andreas Herrmannf6c0e7a2006-08-02 11:05:52 +0200540/**
Christof Schmitt287ac012008-07-02 10:56:40 +0200541 * zfcp_erp_notify - Trigger ERP action.
542 * @erp_action: ERP action to continue.
543 * @set_mask: ERP action status flags to set.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 */
Christof Schmitt287ac012008-07-02 10:56:40 +0200545void zfcp_erp_notify(struct zfcp_erp_action *erp_action, unsigned long set_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546{
547 struct zfcp_adapter *adapter = erp_action->adapter;
548 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
550 write_lock_irqsave(&adapter->erp_lock, flags);
Christof Schmitt287ac012008-07-02 10:56:40 +0200551 if (zfcp_erp_action_exists(erp_action) == ZFCP_ERP_ACTION_RUNNING) {
552 erp_action->status |= set_mask;
553 zfcp_erp_action_ready(erp_action);
554 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 write_unlock_irqrestore(&adapter->erp_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556}
557
Andreas Herrmannf6c0e7a2006-08-02 11:05:52 +0200558/**
Christof Schmitt287ac012008-07-02 10:56:40 +0200559 * zfcp_erp_timeout_handler - Trigger ERP action from timed out ERP request
560 * @data: ERP action (from timer data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 */
Christof Schmitt287ac012008-07-02 10:56:40 +0200562void zfcp_erp_timeout_handler(unsigned long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563{
Christof Schmitt287ac012008-07-02 10:56:40 +0200564 struct zfcp_erp_action *act = (struct zfcp_erp_action *) data;
565 zfcp_erp_notify(act, ZFCP_STATUS_ERP_TIMEDOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566}
567
Christof Schmitt287ac012008-07-02 10:56:40 +0200568static void zfcp_erp_memwait_handler(unsigned long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569{
Christof Schmitt287ac012008-07-02 10:56:40 +0200570 zfcp_erp_notify((struct zfcp_erp_action *)data, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571}
572
Christof Schmitt287ac012008-07-02 10:56:40 +0200573static void zfcp_erp_strategy_memwait(struct zfcp_erp_action *erp_action)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 init_timer(&erp_action->timer);
576 erp_action->timer.function = zfcp_erp_memwait_handler;
577 erp_action->timer.data = (unsigned long) erp_action;
Christof Schmitt287ac012008-07-02 10:56:40 +0200578 erp_action->timer.expires = jiffies + HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 add_timer(&erp_action->timer);
Christof Schmitt287ac012008-07-02 10:56:40 +0200580}
581
582static void _zfcp_erp_port_reopen_all(struct zfcp_adapter *adapter,
Swen Schillig5ffd51a2009-03-02 13:09:04 +0100583 int clear, char *id, void *ref)
Christof Schmitt287ac012008-07-02 10:56:40 +0200584{
585 struct zfcp_port *port;
586
Swen Schilligecf0c772009-11-24 16:53:58 +0100587 read_lock(&adapter->port_list_lock);
588 list_for_each_entry(port, &adapter->port_list, list)
Swen Schillig5ab944f2008-10-01 12:42:17 +0200589 _zfcp_erp_port_reopen(port, clear, id, ref);
Swen Schilligecf0c772009-11-24 16:53:58 +0100590 read_unlock(&adapter->port_list_lock);
Christof Schmitt287ac012008-07-02 10:56:40 +0200591}
592
Christof Schmittb62a8d92010-09-08 14:39:55 +0200593static void _zfcp_erp_lun_reopen_all(struct zfcp_port *port, int clear,
594 char *id, void *ref)
Christof Schmitt287ac012008-07-02 10:56:40 +0200595{
Christof Schmittb62a8d92010-09-08 14:39:55 +0200596 struct scsi_device *sdev;
Christof Schmitt287ac012008-07-02 10:56:40 +0200597
Christof Schmittb62a8d92010-09-08 14:39:55 +0200598 shost_for_each_device(sdev, port->adapter->scsi_host)
599 if (sdev_to_zfcp(sdev)->port == port)
600 _zfcp_erp_lun_reopen(sdev, clear, id, ref, 0);
Christof Schmitt287ac012008-07-02 10:56:40 +0200601}
602
Christof Schmitt85600f72009-07-13 15:06:09 +0200603static void zfcp_erp_strategy_followup_failed(struct zfcp_erp_action *act)
Christof Schmitt287ac012008-07-02 10:56:40 +0200604{
Christof Schmitt287ac012008-07-02 10:56:40 +0200605 switch (act->action) {
Christof Schmitt287ac012008-07-02 10:56:40 +0200606 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
Christof Schmitt85600f72009-07-13 15:06:09 +0200607 _zfcp_erp_adapter_reopen(act->adapter, 0, "ersff_1", NULL);
Christof Schmitt287ac012008-07-02 10:56:40 +0200608 break;
Christof Schmitt287ac012008-07-02 10:56:40 +0200609 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
Christof Schmitt85600f72009-07-13 15:06:09 +0200610 _zfcp_erp_port_forced_reopen(act->port, 0, "ersff_2", NULL);
Christof Schmitt287ac012008-07-02 10:56:40 +0200611 break;
Christof Schmitt287ac012008-07-02 10:56:40 +0200612 case ZFCP_ERP_ACTION_REOPEN_PORT:
Christof Schmitt85600f72009-07-13 15:06:09 +0200613 _zfcp_erp_port_reopen(act->port, 0, "ersff_3", NULL);
Christof Schmitt287ac012008-07-02 10:56:40 +0200614 break;
Christof Schmittb62a8d92010-09-08 14:39:55 +0200615 case ZFCP_ERP_ACTION_REOPEN_LUN:
616 _zfcp_erp_lun_reopen(act->sdev, 0, "ersff_4", NULL, 0);
Christof Schmitt85600f72009-07-13 15:06:09 +0200617 break;
618 }
619}
620
621static void zfcp_erp_strategy_followup_success(struct zfcp_erp_action *act)
622{
623 switch (act->action) {
624 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
625 _zfcp_erp_port_reopen_all(act->adapter, 0, "ersfs_1", NULL);
626 break;
627 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
628 _zfcp_erp_port_reopen(act->port, 0, "ersfs_2", NULL);
629 break;
630 case ZFCP_ERP_ACTION_REOPEN_PORT:
Christof Schmittb62a8d92010-09-08 14:39:55 +0200631 _zfcp_erp_lun_reopen_all(act->port, 0, "ersfs_3", NULL);
Christof Schmitt287ac012008-07-02 10:56:40 +0200632 break;
633 }
634}
635
636static void zfcp_erp_wakeup(struct zfcp_adapter *adapter)
637{
638 unsigned long flags;
639
Swen Schilligecf0c772009-11-24 16:53:58 +0100640 read_lock_irqsave(&adapter->erp_lock, flags);
Christof Schmitt287ac012008-07-02 10:56:40 +0200641 if (list_empty(&adapter->erp_ready_head) &&
642 list_empty(&adapter->erp_running_head)) {
643 atomic_clear_mask(ZFCP_STATUS_ADAPTER_ERP_PENDING,
644 &adapter->status);
645 wake_up(&adapter->erp_done_wqh);
646 }
Swen Schilligecf0c772009-11-24 16:53:58 +0100647 read_unlock_irqrestore(&adapter->erp_lock, flags);
Christof Schmitt287ac012008-07-02 10:56:40 +0200648}
649
650static int zfcp_erp_adapter_strategy_open_qdio(struct zfcp_erp_action *act)
651{
Swen Schillig564e1c82009-08-18 15:43:19 +0200652 struct zfcp_qdio *qdio = act->adapter->qdio;
653
654 if (zfcp_qdio_open(qdio))
Christof Schmitt287ac012008-07-02 10:56:40 +0200655 return ZFCP_ERP_FAILED;
Swen Schillig564e1c82009-08-18 15:43:19 +0200656 init_waitqueue_head(&qdio->req_q_wq);
Christof Schmitt287ac012008-07-02 10:56:40 +0200657 atomic_set_mask(ZFCP_STATUS_ADAPTER_QDIOUP, &act->adapter->status);
658 return ZFCP_ERP_SUCCEEDED;
659}
660
661static void zfcp_erp_enqueue_ptp_port(struct zfcp_adapter *adapter)
662{
663 struct zfcp_port *port;
664 port = zfcp_port_enqueue(adapter, adapter->peer_wwpn, 0,
665 adapter->peer_d_id);
666 if (IS_ERR(port)) /* error or port already attached */
667 return;
Swen Schillig5ffd51a2009-03-02 13:09:04 +0100668 _zfcp_erp_port_reopen(port, 0, "ereptp1", NULL);
Christof Schmitt287ac012008-07-02 10:56:40 +0200669}
670
671static int zfcp_erp_adapter_strat_fsf_xconf(struct zfcp_erp_action *erp_action)
672{
673 int retries;
674 int sleep = 1;
675 struct zfcp_adapter *adapter = erp_action->adapter;
676
677 atomic_clear_mask(ZFCP_STATUS_ADAPTER_XCONFIG_OK, &adapter->status);
678
679 for (retries = 7; retries; retries--) {
680 atomic_clear_mask(ZFCP_STATUS_ADAPTER_HOST_CON_INIT,
681 &adapter->status);
682 write_lock_irq(&adapter->erp_lock);
683 zfcp_erp_action_to_running(erp_action);
684 write_unlock_irq(&adapter->erp_lock);
685 if (zfcp_fsf_exchange_config_data(erp_action)) {
686 atomic_clear_mask(ZFCP_STATUS_ADAPTER_HOST_CON_INIT,
687 &adapter->status);
688 return ZFCP_ERP_FAILED;
689 }
690
Swen Schillig57717102009-08-18 15:43:21 +0200691 zfcp_dbf_rec_thread_lock("erasfx1", adapter->dbf);
Christof Schmitt347c6a92009-08-18 15:43:25 +0200692 wait_event(adapter->erp_ready_wq,
693 !list_empty(&adapter->erp_ready_head));
Swen Schillig57717102009-08-18 15:43:21 +0200694 zfcp_dbf_rec_thread_lock("erasfx2", adapter->dbf);
Christof Schmitt287ac012008-07-02 10:56:40 +0200695 if (erp_action->status & ZFCP_STATUS_ERP_TIMEDOUT)
696 break;
697
698 if (!(atomic_read(&adapter->status) &
699 ZFCP_STATUS_ADAPTER_HOST_CON_INIT))
700 break;
701
702 ssleep(sleep);
703 sleep *= 2;
704 }
705
706 atomic_clear_mask(ZFCP_STATUS_ADAPTER_HOST_CON_INIT,
707 &adapter->status);
708
709 if (!(atomic_read(&adapter->status) & ZFCP_STATUS_ADAPTER_XCONFIG_OK))
710 return ZFCP_ERP_FAILED;
711
712 if (fc_host_port_type(adapter->scsi_host) == FC_PORTTYPE_PTP)
713 zfcp_erp_enqueue_ptp_port(adapter);
714
715 return ZFCP_ERP_SUCCEEDED;
716}
717
718static int zfcp_erp_adapter_strategy_open_fsf_xport(struct zfcp_erp_action *act)
719{
720 int ret;
721 struct zfcp_adapter *adapter = act->adapter;
722
Christof Schmitt287ac012008-07-02 10:56:40 +0200723 write_lock_irq(&adapter->erp_lock);
724 zfcp_erp_action_to_running(act);
725 write_unlock_irq(&adapter->erp_lock);
726
727 ret = zfcp_fsf_exchange_port_data(act);
728 if (ret == -EOPNOTSUPP)
729 return ZFCP_ERP_SUCCEEDED;
730 if (ret)
731 return ZFCP_ERP_FAILED;
732
Swen Schillig57717102009-08-18 15:43:21 +0200733 zfcp_dbf_rec_thread_lock("erasox1", adapter->dbf);
Christof Schmitt347c6a92009-08-18 15:43:25 +0200734 wait_event(adapter->erp_ready_wq,
735 !list_empty(&adapter->erp_ready_head));
Swen Schillig57717102009-08-18 15:43:21 +0200736 zfcp_dbf_rec_thread_lock("erasox2", adapter->dbf);
Christof Schmitt287ac012008-07-02 10:56:40 +0200737 if (act->status & ZFCP_STATUS_ERP_TIMEDOUT)
738 return ZFCP_ERP_FAILED;
739
740 return ZFCP_ERP_SUCCEEDED;
741}
742
743static int zfcp_erp_adapter_strategy_open_fsf(struct zfcp_erp_action *act)
744{
745 if (zfcp_erp_adapter_strat_fsf_xconf(act) == ZFCP_ERP_FAILED)
746 return ZFCP_ERP_FAILED;
747
748 if (zfcp_erp_adapter_strategy_open_fsf_xport(act) == ZFCP_ERP_FAILED)
749 return ZFCP_ERP_FAILED;
750
Christof Schmitt8d88cf32010-06-21 10:11:33 +0200751 if (mempool_resize(act->adapter->pool.status_read_data,
752 act->adapter->stat_read_buf_num, GFP_KERNEL))
753 return ZFCP_ERP_FAILED;
754
755 if (mempool_resize(act->adapter->pool.status_read_req,
756 act->adapter->stat_read_buf_num, GFP_KERNEL))
757 return ZFCP_ERP_FAILED;
758
Christof Schmitt64deb6e2010-04-30 18:09:36 +0200759 atomic_set(&act->adapter->stat_miss, act->adapter->stat_read_buf_num);
Christof Schmitt287ac012008-07-02 10:56:40 +0200760 if (zfcp_status_read_refill(act->adapter))
761 return ZFCP_ERP_FAILED;
762
763 return ZFCP_ERP_SUCCEEDED;
764}
765
Swen Schilligcf13c082009-03-02 13:09:03 +0100766static void zfcp_erp_adapter_strategy_close(struct zfcp_erp_action *act)
Christof Schmitt287ac012008-07-02 10:56:40 +0200767{
Christof Schmitt287ac012008-07-02 10:56:40 +0200768 struct zfcp_adapter *adapter = act->adapter;
769
Christof Schmitt287ac012008-07-02 10:56:40 +0200770 /* close queues to ensure that buffers are not accessed by adapter */
Swen Schillig564e1c82009-08-18 15:43:19 +0200771 zfcp_qdio_close(adapter->qdio);
Christof Schmitt287ac012008-07-02 10:56:40 +0200772 zfcp_fsf_req_dismiss_all(adapter);
773 adapter->fsf_req_seq_no = 0;
Christof Schmitt55c770f2009-08-18 15:43:12 +0200774 zfcp_fc_wka_ports_force_offline(adapter->gs);
Christof Schmittb62a8d92010-09-08 14:39:55 +0200775 /* all ports and LUNs are closed */
Swen Schilligedaed852010-09-08 14:40:01 +0200776 zfcp_erp_clear_adapter_status(adapter, ZFCP_STATUS_COMMON_OPEN);
Swen Schilligcf13c082009-03-02 13:09:03 +0100777
Christof Schmitt287ac012008-07-02 10:56:40 +0200778 atomic_clear_mask(ZFCP_STATUS_ADAPTER_XCONFIG_OK |
Swen Schilligcf13c082009-03-02 13:09:03 +0100779 ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED, &adapter->status);
780}
781
782static int zfcp_erp_adapter_strategy_open(struct zfcp_erp_action *act)
783{
784 struct zfcp_adapter *adapter = act->adapter;
785
786 if (zfcp_erp_adapter_strategy_open_qdio(act)) {
787 atomic_clear_mask(ZFCP_STATUS_ADAPTER_XCONFIG_OK |
788 ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED,
789 &adapter->status);
790 return ZFCP_ERP_FAILED;
791 }
792
793 if (zfcp_erp_adapter_strategy_open_fsf(act)) {
794 zfcp_erp_adapter_strategy_close(act);
795 return ZFCP_ERP_FAILED;
796 }
797
798 atomic_set_mask(ZFCP_STATUS_COMMON_OPEN, &adapter->status);
799
800 return ZFCP_ERP_SUCCEEDED;
Christof Schmitt287ac012008-07-02 10:56:40 +0200801}
802
803static int zfcp_erp_adapter_strategy(struct zfcp_erp_action *act)
804{
Swen Schilligcf13c082009-03-02 13:09:03 +0100805 struct zfcp_adapter *adapter = act->adapter;
Christof Schmitt287ac012008-07-02 10:56:40 +0200806
Swen Schilligcf13c082009-03-02 13:09:03 +0100807 if (atomic_read(&adapter->status) & ZFCP_STATUS_COMMON_OPEN) {
808 zfcp_erp_adapter_strategy_close(act);
809 if (act->status & ZFCP_STATUS_ERP_CLOSE_ONLY)
810 return ZFCP_ERP_EXIT;
811 }
Christof Schmitt287ac012008-07-02 10:56:40 +0200812
Swen Schilligcf13c082009-03-02 13:09:03 +0100813 if (zfcp_erp_adapter_strategy_open(act)) {
Christof Schmitt287ac012008-07-02 10:56:40 +0200814 ssleep(8);
Swen Schilligcf13c082009-03-02 13:09:03 +0100815 return ZFCP_ERP_FAILED;
816 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817
Swen Schilligcf13c082009-03-02 13:09:03 +0100818 return ZFCP_ERP_SUCCEEDED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819}
820
Christof Schmitt287ac012008-07-02 10:56:40 +0200821static int zfcp_erp_port_forced_strategy_close(struct zfcp_erp_action *act)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822{
Christof Schmitt287ac012008-07-02 10:56:40 +0200823 int retval;
824
825 retval = zfcp_fsf_close_physical_port(act);
826 if (retval == -ENOMEM)
827 return ZFCP_ERP_NOMEM;
828 act->step = ZFCP_ERP_STEP_PHYS_PORT_CLOSING;
829 if (retval)
830 return ZFCP_ERP_FAILED;
831
832 return ZFCP_ERP_CONTINUES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833}
834
Christof Schmitt287ac012008-07-02 10:56:40 +0200835static void zfcp_erp_port_strategy_clearstati(struct zfcp_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836{
Christof Schmitta5b11dd2009-03-02 13:08:54 +0100837 atomic_clear_mask(ZFCP_STATUS_COMMON_ACCESS_DENIED, &port->status);
Christof Schmitt287ac012008-07-02 10:56:40 +0200838}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839
Christof Schmitt287ac012008-07-02 10:56:40 +0200840static int zfcp_erp_port_forced_strategy(struct zfcp_erp_action *erp_action)
841{
842 struct zfcp_port *port = erp_action->port;
843 int status = atomic_read(&port->status);
844
845 switch (erp_action->step) {
846 case ZFCP_ERP_STEP_UNINITIALIZED:
847 zfcp_erp_port_strategy_clearstati(port);
848 if ((status & ZFCP_STATUS_PORT_PHYS_OPEN) &&
849 (status & ZFCP_STATUS_COMMON_OPEN))
850 return zfcp_erp_port_forced_strategy_close(erp_action);
851 else
852 return ZFCP_ERP_FAILED;
853
854 case ZFCP_ERP_STEP_PHYS_PORT_CLOSING:
Christof Schmittddb3e0c2009-07-13 15:06:08 +0200855 if (!(status & ZFCP_STATUS_PORT_PHYS_OPEN))
Christof Schmitt287ac012008-07-02 10:56:40 +0200856 return ZFCP_ERP_SUCCEEDED;
857 }
858 return ZFCP_ERP_FAILED;
859}
860
861static int zfcp_erp_port_strategy_close(struct zfcp_erp_action *erp_action)
862{
863 int retval;
864
865 retval = zfcp_fsf_close_port(erp_action);
866 if (retval == -ENOMEM)
867 return ZFCP_ERP_NOMEM;
868 erp_action->step = ZFCP_ERP_STEP_PORT_CLOSING;
869 if (retval)
870 return ZFCP_ERP_FAILED;
871 return ZFCP_ERP_CONTINUES;
872}
873
874static int zfcp_erp_port_strategy_open_port(struct zfcp_erp_action *erp_action)
875{
876 int retval;
877
878 retval = zfcp_fsf_open_port(erp_action);
879 if (retval == -ENOMEM)
880 return ZFCP_ERP_NOMEM;
881 erp_action->step = ZFCP_ERP_STEP_PORT_OPENING;
882 if (retval)
883 return ZFCP_ERP_FAILED;
884 return ZFCP_ERP_CONTINUES;
885}
886
Christof Schmitt287ac012008-07-02 10:56:40 +0200887static int zfcp_erp_open_ptp_port(struct zfcp_erp_action *act)
888{
889 struct zfcp_adapter *adapter = act->adapter;
890 struct zfcp_port *port = act->port;
891
892 if (port->wwpn != adapter->peer_wwpn) {
Swen Schilligedaed852010-09-08 14:40:01 +0200893 zfcp_erp_set_port_status(port, ZFCP_STATUS_COMMON_ERP_FAILED);
Christof Schmitt287ac012008-07-02 10:56:40 +0200894 return ZFCP_ERP_FAILED;
895 }
896 port->d_id = adapter->peer_d_id;
Christof Schmitt287ac012008-07-02 10:56:40 +0200897 return zfcp_erp_port_strategy_open_port(act);
898}
899
900static int zfcp_erp_port_strategy_open_common(struct zfcp_erp_action *act)
901{
902 struct zfcp_adapter *adapter = act->adapter;
903 struct zfcp_port *port = act->port;
Christof Schmitt287ac012008-07-02 10:56:40 +0200904 int p_status = atomic_read(&port->status);
905
906 switch (act->step) {
907 case ZFCP_ERP_STEP_UNINITIALIZED:
908 case ZFCP_ERP_STEP_PHYS_PORT_CLOSING:
909 case ZFCP_ERP_STEP_PORT_CLOSING:
910 if (fc_host_port_type(adapter->scsi_host) == FC_PORTTYPE_PTP)
911 return zfcp_erp_open_ptp_port(act);
Christof Schmittb98478d2008-12-19 16:56:59 +0100912 if (!port->d_id) {
Christof Schmitt934aeb5872009-10-14 11:00:43 +0200913 zfcp_fc_trigger_did_lookup(port);
Christof Schmitt799b76d2009-08-18 15:43:20 +0200914 return ZFCP_ERP_EXIT;
Christof Schmitt287ac012008-07-02 10:56:40 +0200915 }
Christof Schmitt287ac012008-07-02 10:56:40 +0200916 return zfcp_erp_port_strategy_open_port(act);
917
918 case ZFCP_ERP_STEP_PORT_OPENING:
919 /* D_ID might have changed during open */
Swen Schillig5ab944f2008-10-01 12:42:17 +0200920 if (p_status & ZFCP_STATUS_COMMON_OPEN) {
Christof Schmitt934aeb5872009-10-14 11:00:43 +0200921 if (!port->d_id) {
922 zfcp_fc_trigger_did_lookup(port);
923 return ZFCP_ERP_EXIT;
Swen Schillig5ab944f2008-10-01 12:42:17 +0200924 }
Christof Schmitt934aeb5872009-10-14 11:00:43 +0200925 return ZFCP_ERP_SUCCEEDED;
Swen Schillig5ab944f2008-10-01 12:42:17 +0200926 }
Swen Schilligea460a82009-05-15 13:18:20 +0200927 if (port->d_id && !(p_status & ZFCP_STATUS_COMMON_NOESC)) {
928 port->d_id = 0;
Christof Schmittf7bd7c32010-07-08 09:53:10 +0200929 return ZFCP_ERP_FAILED;
Swen Schilligea460a82009-05-15 13:18:20 +0200930 }
931 /* fall through otherwise */
Christof Schmitt287ac012008-07-02 10:56:40 +0200932 }
933 return ZFCP_ERP_FAILED;
934}
935
Christof Schmitt287ac012008-07-02 10:56:40 +0200936static int zfcp_erp_port_strategy(struct zfcp_erp_action *erp_action)
937{
938 struct zfcp_port *port = erp_action->port;
Christof Schmitt934aeb5872009-10-14 11:00:43 +0200939 int p_status = atomic_read(&port->status);
Christof Schmitt287ac012008-07-02 10:56:40 +0200940
Christof Schmitt934aeb5872009-10-14 11:00:43 +0200941 if ((p_status & ZFCP_STATUS_COMMON_NOESC) &&
942 !(p_status & ZFCP_STATUS_COMMON_OPEN))
Swen Schillig5ab944f2008-10-01 12:42:17 +0200943 goto close_init_done;
944
Christof Schmitt287ac012008-07-02 10:56:40 +0200945 switch (erp_action->step) {
946 case ZFCP_ERP_STEP_UNINITIALIZED:
947 zfcp_erp_port_strategy_clearstati(port);
Christof Schmitt934aeb5872009-10-14 11:00:43 +0200948 if (p_status & ZFCP_STATUS_COMMON_OPEN)
Christof Schmitt287ac012008-07-02 10:56:40 +0200949 return zfcp_erp_port_strategy_close(erp_action);
950 break;
951
952 case ZFCP_ERP_STEP_PORT_CLOSING:
Christof Schmitt934aeb5872009-10-14 11:00:43 +0200953 if (p_status & ZFCP_STATUS_COMMON_OPEN)
Christof Schmitt287ac012008-07-02 10:56:40 +0200954 return ZFCP_ERP_FAILED;
955 break;
956 }
Swen Schillig5ab944f2008-10-01 12:42:17 +0200957
958close_init_done:
Christof Schmitt287ac012008-07-02 10:56:40 +0200959 if (erp_action->status & ZFCP_STATUS_ERP_CLOSE_ONLY)
960 return ZFCP_ERP_EXIT;
Christof Schmitt287ac012008-07-02 10:56:40 +0200961
Swen Schillig5ab944f2008-10-01 12:42:17 +0200962 return zfcp_erp_port_strategy_open_common(erp_action);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963}
964
Christof Schmittb62a8d92010-09-08 14:39:55 +0200965static void zfcp_erp_lun_strategy_clearstati(struct scsi_device *sdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966{
Christof Schmittb62a8d92010-09-08 14:39:55 +0200967 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
968
Swen Schillig44cc76f2008-10-01 12:42:16 +0200969 atomic_clear_mask(ZFCP_STATUS_COMMON_ACCESS_DENIED |
Christof Schmittb62a8d92010-09-08 14:39:55 +0200970 ZFCP_STATUS_LUN_SHARED | ZFCP_STATUS_LUN_READONLY,
971 &zfcp_sdev->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972}
973
Christof Schmittb62a8d92010-09-08 14:39:55 +0200974static int zfcp_erp_lun_strategy_close(struct zfcp_erp_action *erp_action)
Christof Schmitt287ac012008-07-02 10:56:40 +0200975{
Christof Schmittb62a8d92010-09-08 14:39:55 +0200976 int retval = zfcp_fsf_close_lun(erp_action);
Christof Schmitt287ac012008-07-02 10:56:40 +0200977 if (retval == -ENOMEM)
978 return ZFCP_ERP_NOMEM;
Christof Schmittb62a8d92010-09-08 14:39:55 +0200979 erp_action->step = ZFCP_ERP_STEP_LUN_CLOSING;
Christof Schmitt287ac012008-07-02 10:56:40 +0200980 if (retval)
981 return ZFCP_ERP_FAILED;
982 return ZFCP_ERP_CONTINUES;
983}
984
Christof Schmittb62a8d92010-09-08 14:39:55 +0200985static int zfcp_erp_lun_strategy_open(struct zfcp_erp_action *erp_action)
Christof Schmitt287ac012008-07-02 10:56:40 +0200986{
Christof Schmittb62a8d92010-09-08 14:39:55 +0200987 int retval = zfcp_fsf_open_lun(erp_action);
Christof Schmitt287ac012008-07-02 10:56:40 +0200988 if (retval == -ENOMEM)
989 return ZFCP_ERP_NOMEM;
Christof Schmittb62a8d92010-09-08 14:39:55 +0200990 erp_action->step = ZFCP_ERP_STEP_LUN_OPENING;
Christof Schmitt287ac012008-07-02 10:56:40 +0200991 if (retval)
992 return ZFCP_ERP_FAILED;
993 return ZFCP_ERP_CONTINUES;
994}
995
Christof Schmittb62a8d92010-09-08 14:39:55 +0200996static int zfcp_erp_lun_strategy(struct zfcp_erp_action *erp_action)
Christof Schmitt287ac012008-07-02 10:56:40 +0200997{
Christof Schmittb62a8d92010-09-08 14:39:55 +0200998 struct scsi_device *sdev = erp_action->sdev;
999 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
Christof Schmitt287ac012008-07-02 10:56:40 +02001000
1001 switch (erp_action->step) {
1002 case ZFCP_ERP_STEP_UNINITIALIZED:
Christof Schmittb62a8d92010-09-08 14:39:55 +02001003 zfcp_erp_lun_strategy_clearstati(sdev);
1004 if (atomic_read(&zfcp_sdev->status) & ZFCP_STATUS_COMMON_OPEN)
1005 return zfcp_erp_lun_strategy_close(erp_action);
Christof Schmitt287ac012008-07-02 10:56:40 +02001006 /* already closed, fall through */
Christof Schmittb62a8d92010-09-08 14:39:55 +02001007 case ZFCP_ERP_STEP_LUN_CLOSING:
1008 if (atomic_read(&zfcp_sdev->status) & ZFCP_STATUS_COMMON_OPEN)
Christof Schmitt287ac012008-07-02 10:56:40 +02001009 return ZFCP_ERP_FAILED;
1010 if (erp_action->status & ZFCP_STATUS_ERP_CLOSE_ONLY)
1011 return ZFCP_ERP_EXIT;
Christof Schmittb62a8d92010-09-08 14:39:55 +02001012 return zfcp_erp_lun_strategy_open(erp_action);
Christof Schmitt287ac012008-07-02 10:56:40 +02001013
Christof Schmittb62a8d92010-09-08 14:39:55 +02001014 case ZFCP_ERP_STEP_LUN_OPENING:
1015 if (atomic_read(&zfcp_sdev->status) & ZFCP_STATUS_COMMON_OPEN)
Christof Schmitt287ac012008-07-02 10:56:40 +02001016 return ZFCP_ERP_SUCCEEDED;
1017 }
1018 return ZFCP_ERP_FAILED;
1019}
1020
Christof Schmittb62a8d92010-09-08 14:39:55 +02001021static int zfcp_erp_strategy_check_lun(struct scsi_device *sdev, int result)
Christof Schmitt287ac012008-07-02 10:56:40 +02001022{
Christof Schmittb62a8d92010-09-08 14:39:55 +02001023 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
1024
Christof Schmitt287ac012008-07-02 10:56:40 +02001025 switch (result) {
1026 case ZFCP_ERP_SUCCEEDED :
Christof Schmittb62a8d92010-09-08 14:39:55 +02001027 atomic_set(&zfcp_sdev->erp_counter, 0);
1028 zfcp_erp_lun_unblock(sdev);
Christof Schmitt287ac012008-07-02 10:56:40 +02001029 break;
1030 case ZFCP_ERP_FAILED :
Christof Schmittb62a8d92010-09-08 14:39:55 +02001031 atomic_inc(&zfcp_sdev->erp_counter);
1032 if (atomic_read(&zfcp_sdev->erp_counter) > ZFCP_MAX_ERPS) {
1033 dev_err(&zfcp_sdev->port->adapter->ccw_device->dev,
1034 "ERP failed for LUN 0x%016Lx on "
Christof Schmittff3b24f2008-10-01 12:42:15 +02001035 "port 0x%016Lx\n",
Christof Schmittb62a8d92010-09-08 14:39:55 +02001036 (unsigned long long)zfcp_scsi_dev_lun(sdev),
1037 (unsigned long long)zfcp_sdev->port->wwpn);
Swen Schilligedaed852010-09-08 14:40:01 +02001038 zfcp_erp_set_lun_status(sdev,
1039 ZFCP_STATUS_COMMON_ERP_FAILED);
Christof Schmittff3b24f2008-10-01 12:42:15 +02001040 }
Christof Schmitt287ac012008-07-02 10:56:40 +02001041 break;
1042 }
1043
Christof Schmittb62a8d92010-09-08 14:39:55 +02001044 if (atomic_read(&zfcp_sdev->status) & ZFCP_STATUS_COMMON_ERP_FAILED) {
1045 zfcp_erp_lun_block(sdev, 0);
Christof Schmitt287ac012008-07-02 10:56:40 +02001046 result = ZFCP_ERP_EXIT;
1047 }
1048 return result;
1049}
1050
1051static int zfcp_erp_strategy_check_port(struct zfcp_port *port, int result)
1052{
1053 switch (result) {
1054 case ZFCP_ERP_SUCCEEDED :
1055 atomic_set(&port->erp_counter, 0);
1056 zfcp_erp_port_unblock(port);
1057 break;
1058
1059 case ZFCP_ERP_FAILED :
1060 if (atomic_read(&port->status) & ZFCP_STATUS_COMMON_NOESC) {
1061 zfcp_erp_port_block(port, 0);
1062 result = ZFCP_ERP_EXIT;
1063 }
1064 atomic_inc(&port->erp_counter);
Christof Schmittff3b24f2008-10-01 12:42:15 +02001065 if (atomic_read(&port->erp_counter) > ZFCP_MAX_ERPS) {
1066 dev_err(&port->adapter->ccw_device->dev,
1067 "ERP failed for remote port 0x%016Lx\n",
Swen Schillig7ba58c92008-10-01 12:42:18 +02001068 (unsigned long long)port->wwpn);
Swen Schilligedaed852010-09-08 14:40:01 +02001069 zfcp_erp_set_port_status(port,
1070 ZFCP_STATUS_COMMON_ERP_FAILED);
Christof Schmittff3b24f2008-10-01 12:42:15 +02001071 }
Christof Schmitt287ac012008-07-02 10:56:40 +02001072 break;
1073 }
1074
1075 if (atomic_read(&port->status) & ZFCP_STATUS_COMMON_ERP_FAILED) {
1076 zfcp_erp_port_block(port, 0);
1077 result = ZFCP_ERP_EXIT;
1078 }
1079 return result;
1080}
1081
1082static int zfcp_erp_strategy_check_adapter(struct zfcp_adapter *adapter,
1083 int result)
1084{
1085 switch (result) {
1086 case ZFCP_ERP_SUCCEEDED :
1087 atomic_set(&adapter->erp_counter, 0);
1088 zfcp_erp_adapter_unblock(adapter);
1089 break;
1090
1091 case ZFCP_ERP_FAILED :
1092 atomic_inc(&adapter->erp_counter);
Christof Schmittff3b24f2008-10-01 12:42:15 +02001093 if (atomic_read(&adapter->erp_counter) > ZFCP_MAX_ERPS) {
1094 dev_err(&adapter->ccw_device->dev,
1095 "ERP cannot recover an error "
1096 "on the FCP device\n");
Swen Schilligedaed852010-09-08 14:40:01 +02001097 zfcp_erp_set_adapter_status(adapter,
1098 ZFCP_STATUS_COMMON_ERP_FAILED);
Christof Schmittff3b24f2008-10-01 12:42:15 +02001099 }
Christof Schmitt287ac012008-07-02 10:56:40 +02001100 break;
1101 }
1102
1103 if (atomic_read(&adapter->status) & ZFCP_STATUS_COMMON_ERP_FAILED) {
1104 zfcp_erp_adapter_block(adapter, 0);
1105 result = ZFCP_ERP_EXIT;
1106 }
1107 return result;
1108}
1109
1110static int zfcp_erp_strategy_check_target(struct zfcp_erp_action *erp_action,
1111 int result)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112{
1113 struct zfcp_adapter *adapter = erp_action->adapter;
1114 struct zfcp_port *port = erp_action->port;
Christof Schmittb62a8d92010-09-08 14:39:55 +02001115 struct scsi_device *sdev = erp_action->sdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 switch (erp_action->action) {
1118
Christof Schmittb62a8d92010-09-08 14:39:55 +02001119 case ZFCP_ERP_ACTION_REOPEN_LUN:
1120 result = zfcp_erp_strategy_check_lun(sdev, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 break;
1122
1123 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
1124 case ZFCP_ERP_ACTION_REOPEN_PORT:
1125 result = zfcp_erp_strategy_check_port(port, result);
1126 break;
1127
1128 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
1129 result = zfcp_erp_strategy_check_adapter(adapter, result);
1130 break;
1131 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 return result;
1133}
1134
Christof Schmitt287ac012008-07-02 10:56:40 +02001135static int zfcp_erp_strat_change_det(atomic_t *target_status, u32 erp_status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136{
Christof Schmitt287ac012008-07-02 10:56:40 +02001137 int status = atomic_read(target_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138
Christof Schmitt287ac012008-07-02 10:56:40 +02001139 if ((status & ZFCP_STATUS_COMMON_RUNNING) &&
1140 (erp_status & ZFCP_STATUS_ERP_CLOSE_ONLY))
1141 return 1; /* take it online */
1142
1143 if (!(status & ZFCP_STATUS_COMMON_RUNNING) &&
1144 !(erp_status & ZFCP_STATUS_ERP_CLOSE_ONLY))
1145 return 1; /* take it offline */
1146
1147 return 0;
1148}
1149
1150static int zfcp_erp_strategy_statechange(struct zfcp_erp_action *act, int ret)
1151{
1152 int action = act->action;
1153 struct zfcp_adapter *adapter = act->adapter;
1154 struct zfcp_port *port = act->port;
Christof Schmittb62a8d92010-09-08 14:39:55 +02001155 struct scsi_device *sdev = act->sdev;
1156 struct zfcp_scsi_dev *zfcp_sdev;
Christof Schmitt287ac012008-07-02 10:56:40 +02001157 u32 erp_status = act->status;
1158
1159 switch (action) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
Christof Schmitt287ac012008-07-02 10:56:40 +02001161 if (zfcp_erp_strat_change_det(&adapter->status, erp_status)) {
1162 _zfcp_erp_adapter_reopen(adapter,
1163 ZFCP_STATUS_COMMON_ERP_FAILED,
Swen Schillig5ffd51a2009-03-02 13:09:04 +01001164 "ersscg1", NULL);
Christof Schmitt287ac012008-07-02 10:56:40 +02001165 return ZFCP_ERP_EXIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 }
1167 break;
1168
1169 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
1170 case ZFCP_ERP_ACTION_REOPEN_PORT:
Christof Schmitt287ac012008-07-02 10:56:40 +02001171 if (zfcp_erp_strat_change_det(&port->status, erp_status)) {
1172 _zfcp_erp_port_reopen(port,
1173 ZFCP_STATUS_COMMON_ERP_FAILED,
Swen Schillig5ffd51a2009-03-02 13:09:04 +01001174 "ersscg2", NULL);
Christof Schmitt287ac012008-07-02 10:56:40 +02001175 return ZFCP_ERP_EXIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 }
1177 break;
1178
Christof Schmittb62a8d92010-09-08 14:39:55 +02001179 case ZFCP_ERP_ACTION_REOPEN_LUN:
1180 zfcp_sdev = sdev_to_zfcp(sdev);
1181 if (zfcp_erp_strat_change_det(&zfcp_sdev->status, erp_status)) {
1182 _zfcp_erp_lun_reopen(sdev,
1183 ZFCP_STATUS_COMMON_ERP_FAILED,
1184 "ersscg3", NULL, 0);
Christof Schmitt287ac012008-07-02 10:56:40 +02001185 return ZFCP_ERP_EXIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 }
1187 break;
1188 }
Christof Schmitt287ac012008-07-02 10:56:40 +02001189 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190}
1191
Christof Schmitt287ac012008-07-02 10:56:40 +02001192static void zfcp_erp_action_dequeue(struct zfcp_erp_action *erp_action)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193{
Christof Schmitt287ac012008-07-02 10:56:40 +02001194 struct zfcp_adapter *adapter = erp_action->adapter;
Christof Schmittb62a8d92010-09-08 14:39:55 +02001195 struct zfcp_scsi_dev *zfcp_sdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196
Christof Schmitt287ac012008-07-02 10:56:40 +02001197 adapter->erp_total_count--;
1198 if (erp_action->status & ZFCP_STATUS_ERP_LOWMEM) {
1199 adapter->erp_low_mem_count--;
1200 erp_action->status &= ~ZFCP_STATUS_ERP_LOWMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 }
1202
Christof Schmitt287ac012008-07-02 10:56:40 +02001203 list_del(&erp_action->list);
Swen Schillig57717102009-08-18 15:43:21 +02001204 zfcp_dbf_rec_action("eractd1", erp_action);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205
Christof Schmitt287ac012008-07-02 10:56:40 +02001206 switch (erp_action->action) {
Christof Schmittb62a8d92010-09-08 14:39:55 +02001207 case ZFCP_ERP_ACTION_REOPEN_LUN:
1208 zfcp_sdev = sdev_to_zfcp(erp_action->sdev);
Christof Schmitt287ac012008-07-02 10:56:40 +02001209 atomic_clear_mask(ZFCP_STATUS_COMMON_ERP_INUSE,
Christof Schmittb62a8d92010-09-08 14:39:55 +02001210 &zfcp_sdev->status);
Christof Schmitt287ac012008-07-02 10:56:40 +02001211 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212
Christof Schmitt287ac012008-07-02 10:56:40 +02001213 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
1214 case ZFCP_ERP_ACTION_REOPEN_PORT:
1215 atomic_clear_mask(ZFCP_STATUS_COMMON_ERP_INUSE,
1216 &erp_action->port->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 break;
Christof Schmitt287ac012008-07-02 10:56:40 +02001218
1219 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
1220 atomic_clear_mask(ZFCP_STATUS_COMMON_ERP_INUSE,
1221 &erp_action->adapter->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 break;
1223 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224}
1225
Christof Schmitt287ac012008-07-02 10:56:40 +02001226static void zfcp_erp_action_cleanup(struct zfcp_erp_action *act, int result)
Maxim Shchetyninaef4a982005-09-13 21:51:16 +02001227{
Christof Schmitt287ac012008-07-02 10:56:40 +02001228 struct zfcp_adapter *adapter = act->adapter;
1229 struct zfcp_port *port = act->port;
Christof Schmittb62a8d92010-09-08 14:39:55 +02001230 struct scsi_device *sdev = act->sdev;
Maxim Shchetyninaef4a982005-09-13 21:51:16 +02001231
Christof Schmitt287ac012008-07-02 10:56:40 +02001232 switch (act->action) {
Christof Schmittb62a8d92010-09-08 14:39:55 +02001233 case ZFCP_ERP_ACTION_REOPEN_LUN:
Christof Schmittfdbd1c52010-09-08 14:39:54 +02001234 if (!(act->status & ZFCP_STATUS_ERP_NO_REF))
Christof Schmittb62a8d92010-09-08 14:39:55 +02001235 scsi_device_put(sdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 break;
Christof Schmitt287ac012008-07-02 10:56:40 +02001237
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 case ZFCP_ERP_ACTION_REOPEN_PORT:
Christof Schmitta2fa0ae2009-03-02 13:09:08 +01001239 if (result == ZFCP_ERP_SUCCEEDED)
1240 zfcp_scsi_schedule_rport_register(port);
Christof Schmitt57676202010-07-08 09:53:05 +02001241 /* fall through */
1242 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
Christof Schmitt615f59e2010-02-17 11:18:56 +01001243 put_device(&port->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 break;
Christof Schmitt287ac012008-07-02 10:56:40 +02001245
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
Christof Schmitta2fa0ae2009-03-02 13:09:08 +01001247 if (result == ZFCP_ERP_SUCCEEDED) {
Christof Schmittbd43a42b72008-12-25 13:38:50 +01001248 register_service_level(&adapter->service_level);
Swen Schillig9eae07e2009-11-24 16:54:06 +01001249 queue_work(adapter->work_queue, &adapter->scan_work);
Christof Schmitta2fa0ae2009-03-02 13:09:08 +01001250 } else
1251 unregister_service_level(&adapter->service_level);
Swen Schilligf3450c72009-11-24 16:53:59 +01001252 kref_put(&adapter->ref, zfcp_adapter_release);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 }
1255}
1256
Christof Schmitt287ac012008-07-02 10:56:40 +02001257static int zfcp_erp_strategy_do_action(struct zfcp_erp_action *erp_action)
1258{
1259 switch (erp_action->action) {
1260 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
1261 return zfcp_erp_adapter_strategy(erp_action);
1262 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
1263 return zfcp_erp_port_forced_strategy(erp_action);
1264 case ZFCP_ERP_ACTION_REOPEN_PORT:
1265 return zfcp_erp_port_strategy(erp_action);
Christof Schmittb62a8d92010-09-08 14:39:55 +02001266 case ZFCP_ERP_ACTION_REOPEN_LUN:
1267 return zfcp_erp_lun_strategy(erp_action);
Christof Schmitt287ac012008-07-02 10:56:40 +02001268 }
1269 return ZFCP_ERP_FAILED;
1270}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271
Christof Schmitt287ac012008-07-02 10:56:40 +02001272static int zfcp_erp_strategy(struct zfcp_erp_action *erp_action)
1273{
1274 int retval;
Christof Schmitt287ac012008-07-02 10:56:40 +02001275 unsigned long flags;
Swen Schilligecf0c772009-11-24 16:53:58 +01001276 struct zfcp_adapter *adapter = erp_action->adapter;
Christof Schmitt287ac012008-07-02 10:56:40 +02001277
Swen Schilligf3450c72009-11-24 16:53:59 +01001278 kref_get(&adapter->ref);
Christof Schmitt287ac012008-07-02 10:56:40 +02001279
Swen Schilligf3450c72009-11-24 16:53:59 +01001280 write_lock_irqsave(&adapter->erp_lock, flags);
Christof Schmitt287ac012008-07-02 10:56:40 +02001281 zfcp_erp_strategy_check_fsfreq(erp_action);
1282
1283 if (erp_action->status & ZFCP_STATUS_ERP_DISMISSED) {
1284 zfcp_erp_action_dequeue(erp_action);
1285 retval = ZFCP_ERP_DISMISSED;
1286 goto unlock;
1287 }
1288
Christof Schmitt9c785d92010-07-08 09:53:09 +02001289 if (erp_action->status & ZFCP_STATUS_ERP_TIMEDOUT) {
1290 retval = ZFCP_ERP_FAILED;
1291 goto check_target;
1292 }
1293
Christof Schmitt287ac012008-07-02 10:56:40 +02001294 zfcp_erp_action_to_running(erp_action);
1295
1296 /* no lock to allow for blocking operations */
Swen Schilligecf0c772009-11-24 16:53:58 +01001297 write_unlock_irqrestore(&adapter->erp_lock, flags);
Christof Schmitt287ac012008-07-02 10:56:40 +02001298 retval = zfcp_erp_strategy_do_action(erp_action);
Swen Schilligecf0c772009-11-24 16:53:58 +01001299 write_lock_irqsave(&adapter->erp_lock, flags);
Christof Schmitt287ac012008-07-02 10:56:40 +02001300
1301 if (erp_action->status & ZFCP_STATUS_ERP_DISMISSED)
1302 retval = ZFCP_ERP_CONTINUES;
1303
1304 switch (retval) {
1305 case ZFCP_ERP_NOMEM:
1306 if (!(erp_action->status & ZFCP_STATUS_ERP_LOWMEM)) {
1307 ++adapter->erp_low_mem_count;
1308 erp_action->status |= ZFCP_STATUS_ERP_LOWMEM;
1309 }
1310 if (adapter->erp_total_count == adapter->erp_low_mem_count)
Swen Schillig5ffd51a2009-03-02 13:09:04 +01001311 _zfcp_erp_adapter_reopen(adapter, 0, "erstgy1", NULL);
Christof Schmitt287ac012008-07-02 10:56:40 +02001312 else {
1313 zfcp_erp_strategy_memwait(erp_action);
1314 retval = ZFCP_ERP_CONTINUES;
1315 }
1316 goto unlock;
1317
1318 case ZFCP_ERP_CONTINUES:
1319 if (erp_action->status & ZFCP_STATUS_ERP_LOWMEM) {
1320 --adapter->erp_low_mem_count;
1321 erp_action->status &= ~ZFCP_STATUS_ERP_LOWMEM;
1322 }
1323 goto unlock;
1324 }
1325
Christof Schmitt9c785d92010-07-08 09:53:09 +02001326check_target:
Christof Schmitt287ac012008-07-02 10:56:40 +02001327 retval = zfcp_erp_strategy_check_target(erp_action, retval);
1328 zfcp_erp_action_dequeue(erp_action);
1329 retval = zfcp_erp_strategy_statechange(erp_action, retval);
1330 if (retval == ZFCP_ERP_EXIT)
1331 goto unlock;
Christof Schmitt85600f72009-07-13 15:06:09 +02001332 if (retval == ZFCP_ERP_SUCCEEDED)
1333 zfcp_erp_strategy_followup_success(erp_action);
1334 if (retval == ZFCP_ERP_FAILED)
1335 zfcp_erp_strategy_followup_failed(erp_action);
Christof Schmitt287ac012008-07-02 10:56:40 +02001336
1337 unlock:
Swen Schilligecf0c772009-11-24 16:53:58 +01001338 write_unlock_irqrestore(&adapter->erp_lock, flags);
Christof Schmitt287ac012008-07-02 10:56:40 +02001339
1340 if (retval != ZFCP_ERP_CONTINUES)
1341 zfcp_erp_action_cleanup(erp_action, retval);
1342
Swen Schilligf3450c72009-11-24 16:53:59 +01001343 kref_put(&adapter->ref, zfcp_adapter_release);
Christof Schmitt287ac012008-07-02 10:56:40 +02001344 return retval;
1345}
1346
1347static int zfcp_erp_thread(void *data)
1348{
1349 struct zfcp_adapter *adapter = (struct zfcp_adapter *) data;
1350 struct list_head *next;
1351 struct zfcp_erp_action *act;
1352 unsigned long flags;
1353
Christof Schmitt347c6a92009-08-18 15:43:25 +02001354 for (;;) {
Swen Schillig57717102009-08-18 15:43:21 +02001355 zfcp_dbf_rec_thread_lock("erthrd1", adapter->dbf);
Christof Schmitt347c6a92009-08-18 15:43:25 +02001356 wait_event_interruptible(adapter->erp_ready_wq,
1357 !list_empty(&adapter->erp_ready_head) ||
1358 kthread_should_stop());
Swen Schillig57717102009-08-18 15:43:21 +02001359 zfcp_dbf_rec_thread_lock("erthrd2", adapter->dbf);
Swen Schillig94ab4b32009-04-17 15:08:06 +02001360
Christof Schmitt347c6a92009-08-18 15:43:25 +02001361 if (kthread_should_stop())
1362 break;
1363
Christof Schmitt287ac012008-07-02 10:56:40 +02001364 write_lock_irqsave(&adapter->erp_lock, flags);
1365 next = adapter->erp_ready_head.next;
1366 write_unlock_irqrestore(&adapter->erp_lock, flags);
1367
1368 if (next != &adapter->erp_ready_head) {
1369 act = list_entry(next, struct zfcp_erp_action, list);
1370
1371 /* there is more to come after dismission, no notify */
1372 if (zfcp_erp_strategy(act) != ZFCP_ERP_DISMISSED)
1373 zfcp_erp_wakeup(adapter);
1374 }
Christof Schmitt287ac012008-07-02 10:56:40 +02001375 }
1376
Christof Schmitt287ac012008-07-02 10:56:40 +02001377 return 0;
1378}
1379
1380/**
1381 * zfcp_erp_thread_setup - Start ERP thread for adapter
1382 * @adapter: Adapter to start the ERP thread for
1383 *
1384 * Returns 0 on success or error code from kernel_thread()
1385 */
1386int zfcp_erp_thread_setup(struct zfcp_adapter *adapter)
1387{
Christof Schmitt347c6a92009-08-18 15:43:25 +02001388 struct task_struct *thread;
Christof Schmitt287ac012008-07-02 10:56:40 +02001389
Christof Schmitt347c6a92009-08-18 15:43:25 +02001390 thread = kthread_run(zfcp_erp_thread, adapter, "zfcperp%s",
1391 dev_name(&adapter->ccw_device->dev));
1392 if (IS_ERR(thread)) {
Christof Schmitt287ac012008-07-02 10:56:40 +02001393 dev_err(&adapter->ccw_device->dev,
Christof Schmittff3b24f2008-10-01 12:42:15 +02001394 "Creating an ERP thread for the FCP device failed.\n");
Christof Schmitt347c6a92009-08-18 15:43:25 +02001395 return PTR_ERR(thread);
Christof Schmitt287ac012008-07-02 10:56:40 +02001396 }
Christof Schmitt347c6a92009-08-18 15:43:25 +02001397
1398 adapter->erp_thread = thread;
Christof Schmitt287ac012008-07-02 10:56:40 +02001399 return 0;
1400}
1401
1402/**
1403 * zfcp_erp_thread_kill - Stop ERP thread.
1404 * @adapter: Adapter where the ERP thread should be stopped.
1405 *
1406 * The caller of this routine ensures that the specified adapter has
1407 * been shut down and that this operation has been completed. Thus,
1408 * there are no pending erp_actions which would need to be handled
1409 * here.
1410 */
1411void zfcp_erp_thread_kill(struct zfcp_adapter *adapter)
1412{
Christof Schmitt347c6a92009-08-18 15:43:25 +02001413 kthread_stop(adapter->erp_thread);
1414 adapter->erp_thread = NULL;
Christof Schmitt143bb6b2009-08-18 15:43:27 +02001415 WARN_ON(!list_empty(&adapter->erp_ready_head));
1416 WARN_ON(!list_empty(&adapter->erp_running_head));
Christof Schmitt287ac012008-07-02 10:56:40 +02001417}
1418
1419/**
Christof Schmitt287ac012008-07-02 10:56:40 +02001420 * zfcp_erp_wait - wait for completion of error recovery on an adapter
1421 * @adapter: adapter for which to wait for completion of its error recovery
1422 */
1423void zfcp_erp_wait(struct zfcp_adapter *adapter)
1424{
1425 wait_event(adapter->erp_done_wqh,
1426 !(atomic_read(&adapter->status) &
1427 ZFCP_STATUS_ADAPTER_ERP_PENDING));
1428}
1429
1430/**
Swen Schilligedaed852010-09-08 14:40:01 +02001431 * zfcp_erp_set_adapter_status - set adapter status bits
Christof Schmitt287ac012008-07-02 10:56:40 +02001432 * @adapter: adapter to change the status
Christof Schmitt287ac012008-07-02 10:56:40 +02001433 * @mask: status bits to change
Christof Schmitt287ac012008-07-02 10:56:40 +02001434 *
Christof Schmittb62a8d92010-09-08 14:39:55 +02001435 * Changes in common status bits are propagated to attached ports and LUNs.
Christof Schmitt287ac012008-07-02 10:56:40 +02001436 */
Swen Schilligedaed852010-09-08 14:40:01 +02001437void zfcp_erp_set_adapter_status(struct zfcp_adapter *adapter, u32 mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 struct zfcp_port *port;
Swen Schilligedaed852010-09-08 14:40:01 +02001440 struct scsi_device *sdev;
Swen Schilligecf0c772009-11-24 16:53:58 +01001441 unsigned long flags;
Christof Schmitt287ac012008-07-02 10:56:40 +02001442 u32 common_mask = mask & ZFCP_COMMON_FLAGS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443
Swen Schilligedaed852010-09-08 14:40:01 +02001444 atomic_set_mask(mask, &adapter->status);
Christof Schmitt287ac012008-07-02 10:56:40 +02001445
Swen Schilligedaed852010-09-08 14:40:01 +02001446 if (!common_mask)
1447 return;
1448
1449 read_lock_irqsave(&adapter->port_list_lock, flags);
1450 list_for_each_entry(port, &adapter->port_list, list)
1451 atomic_set_mask(common_mask, &port->status);
1452 read_unlock_irqrestore(&adapter->port_list_lock, flags);
1453
1454 shost_for_each_device(sdev, adapter->scsi_host)
1455 atomic_set_mask(common_mask, &sdev_to_zfcp(sdev)->status);
1456}
1457
1458/**
1459 * zfcp_erp_clear_adapter_status - clear adapter status bits
1460 * @adapter: adapter to change the status
1461 * @mask: status bits to change
1462 *
1463 * Changes in common status bits are propagated to attached ports and LUNs.
1464 */
1465void zfcp_erp_clear_adapter_status(struct zfcp_adapter *adapter, u32 mask)
1466{
1467 struct zfcp_port *port;
1468 struct scsi_device *sdev;
1469 unsigned long flags;
1470 u32 common_mask = mask & ZFCP_COMMON_FLAGS;
1471 u32 clear_counter = mask & ZFCP_STATUS_COMMON_ERP_FAILED;
1472
1473 atomic_clear_mask(mask, &adapter->status);
1474
1475 if (!common_mask)
1476 return;
1477
1478 if (clear_counter)
1479 atomic_set(&adapter->erp_counter, 0);
1480
1481 read_lock_irqsave(&adapter->port_list_lock, flags);
1482 list_for_each_entry(port, &adapter->port_list, list) {
1483 atomic_clear_mask(common_mask, &port->status);
1484 if (clear_counter)
1485 atomic_set(&port->erp_counter, 0);
1486 }
1487 read_unlock_irqrestore(&adapter->port_list_lock, flags);
1488
1489 shost_for_each_device(sdev, adapter->scsi_host) {
1490 atomic_clear_mask(common_mask, &sdev_to_zfcp(sdev)->status);
1491 if (clear_counter)
1492 atomic_set(&sdev_to_zfcp(sdev)->erp_counter, 0);
Swen Schilligecf0c772009-11-24 16:53:58 +01001493 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494}
1495
Christof Schmitt287ac012008-07-02 10:56:40 +02001496/**
Swen Schilligedaed852010-09-08 14:40:01 +02001497 * zfcp_erp_set_port_status - set port status bits
1498 * @port: port to change the status
Christof Schmitt287ac012008-07-02 10:56:40 +02001499 * @mask: status bits to change
Christof Schmitt287ac012008-07-02 10:56:40 +02001500 *
Christof Schmittb62a8d92010-09-08 14:39:55 +02001501 * Changes in common status bits are propagated to attached LUNs.
Christof Schmitt287ac012008-07-02 10:56:40 +02001502 */
Swen Schilligedaed852010-09-08 14:40:01 +02001503void zfcp_erp_set_port_status(struct zfcp_port *port, u32 mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504{
Christof Schmittb62a8d92010-09-08 14:39:55 +02001505 struct scsi_device *sdev;
Christof Schmitt287ac012008-07-02 10:56:40 +02001506 u32 common_mask = mask & ZFCP_COMMON_FLAGS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507
Swen Schilligedaed852010-09-08 14:40:01 +02001508 atomic_set_mask(mask, &port->status);
Christof Schmitt287ac012008-07-02 10:56:40 +02001509
Swen Schilligedaed852010-09-08 14:40:01 +02001510 if (!common_mask)
1511 return;
1512
1513 shost_for_each_device(sdev, port->adapter->scsi_host)
1514 if (sdev_to_zfcp(sdev)->port == port)
1515 atomic_set_mask(common_mask,
1516 &sdev_to_zfcp(sdev)->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517}
1518
Christof Schmitt287ac012008-07-02 10:56:40 +02001519/**
Swen Schilligedaed852010-09-08 14:40:01 +02001520 * zfcp_erp_clear_port_status - clear port status bits
1521 * @port: adapter to change the status
Christof Schmitt287ac012008-07-02 10:56:40 +02001522 * @mask: status bits to change
Swen Schilligedaed852010-09-08 14:40:01 +02001523 *
1524 * Changes in common status bits are propagated to attached LUNs.
Christof Schmitt287ac012008-07-02 10:56:40 +02001525 */
Swen Schilligedaed852010-09-08 14:40:01 +02001526void zfcp_erp_clear_port_status(struct zfcp_port *port, u32 mask)
1527{
1528 struct scsi_device *sdev;
1529 u32 common_mask = mask & ZFCP_COMMON_FLAGS;
1530 u32 clear_counter = mask & ZFCP_STATUS_COMMON_ERP_FAILED;
1531
1532 atomic_clear_mask(mask, &port->status);
1533
1534 if (!common_mask)
1535 return;
1536
1537 if (clear_counter)
1538 atomic_set(&port->erp_counter, 0);
1539
1540 shost_for_each_device(sdev, port->adapter->scsi_host)
1541 if (sdev_to_zfcp(sdev)->port == port) {
1542 atomic_clear_mask(common_mask,
1543 &sdev_to_zfcp(sdev)->status);
1544 if (clear_counter)
1545 atomic_set(&sdev_to_zfcp(sdev)->erp_counter, 0);
1546 }
1547}
1548
1549/**
1550 * zfcp_erp_set_lun_status - set lun status bits
1551 * @sdev: SCSI device / lun to set the status bits
1552 * @mask: status bits to change
1553 */
1554void zfcp_erp_set_lun_status(struct scsi_device *sdev, u32 mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555{
Christof Schmittb62a8d92010-09-08 14:39:55 +02001556 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
1557
Swen Schilligedaed852010-09-08 14:40:01 +02001558 atomic_set_mask(mask, &zfcp_sdev->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559}
1560
Christof Schmitt287ac012008-07-02 10:56:40 +02001561/**
Swen Schilligedaed852010-09-08 14:40:01 +02001562 * zfcp_erp_clear_lun_status - clear lun status bits
1563 * @sdev: SCSi device / lun to clear the status bits
1564 * @mask: status bits to change
Christof Schmitt287ac012008-07-02 10:56:40 +02001565 */
Swen Schilligedaed852010-09-08 14:40:01 +02001566void zfcp_erp_clear_lun_status(struct scsi_device *sdev, u32 mask)
Andreas Herrmannd736a27b2005-06-13 13:23:57 +02001567{
Swen Schilligedaed852010-09-08 14:40:01 +02001568 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
1569
1570 atomic_clear_mask(mask, &zfcp_sdev->status);
1571
1572 if (mask & ZFCP_STATUS_COMMON_ERP_FAILED)
1573 atomic_set(&zfcp_sdev->erp_counter, 0);
Andreas Herrmannd736a27b2005-06-13 13:23:57 +02001574}
1575