blob: e003e306f870aebd0ea3a7b41265509dc35a74ee [file] [log] [blame]
Swen Schillig41fa2ad2007-09-07 09:15:31 +02001/*
Christof Schmitt553448f2008-06-10 18:20:58 +02002 * zfcp device driver
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
Christof Schmitt553448f2008-06-10 18:20:58 +02004 * Error Recovery Procedures (ERP).
Swen Schillig41fa2ad2007-09-07 09:15:31 +02005 *
Christof 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 Schilligedaed8592010-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 Schilligae0904f2010-12-02 15:16:12 +010079 zfcp_dbf_rec_run("erardy1", act);
Christof Schmitt347c6a92009-08-18 15:43:25 +020080 wake_up(&adapter->erp_ready_wq);
Swen Schilligae0904f2010-12-02 15:16:12 +010081 zfcp_dbf_rec_run("erardy2", act);
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;
Swen Schilligd3e10882010-11-17 14:23:42 +0100159 if (p_status & ZFCP_STATUS_COMMON_NOESC)
160 return need;
Christof Schmitt287ac012008-07-02 10:56:40 +0200161 if (!(a_status & ZFCP_STATUS_COMMON_UNBLOCKED))
162 need = ZFCP_ERP_ACTION_REOPEN_ADAPTER;
163 /* fall through */
164 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
165 a_status = atomic_read(&adapter->status);
166 if (a_status & ZFCP_STATUS_COMMON_ERP_INUSE)
167 return 0;
Christof Schmitt143bb6b2009-08-18 15:43:27 +0200168 if (!(a_status & ZFCP_STATUS_COMMON_RUNNING) &&
169 !(a_status & ZFCP_STATUS_COMMON_OPEN))
170 return 0; /* shutdown requested for closed adapter */
Christof Schmitt287ac012008-07-02 10:56:40 +0200171 }
172
173 return need;
174}
175
Christof Schmittfdbd1c52010-09-08 14:39:54 +0200176static struct zfcp_erp_action *zfcp_erp_setup_act(int need, u32 act_status,
Christof Schmitt287ac012008-07-02 10:56:40 +0200177 struct zfcp_adapter *adapter,
178 struct zfcp_port *port,
Christof Schmittb62a8d92010-09-08 14:39:55 +0200179 struct scsi_device *sdev)
Christof Schmitt287ac012008-07-02 10:56:40 +0200180{
181 struct zfcp_erp_action *erp_action;
Christof Schmittb62a8d92010-09-08 14:39:55 +0200182 struct zfcp_scsi_dev *zfcp_sdev;
Christof Schmitt287ac012008-07-02 10:56:40 +0200183
184 switch (need) {
Christof Schmittb62a8d92010-09-08 14:39:55 +0200185 case ZFCP_ERP_ACTION_REOPEN_LUN:
186 zfcp_sdev = sdev_to_zfcp(sdev);
Christof Schmittfdbd1c52010-09-08 14:39:54 +0200187 if (!(act_status & ZFCP_STATUS_ERP_NO_REF))
Christof Schmittb62a8d92010-09-08 14:39:55 +0200188 if (scsi_device_get(sdev))
Christof Schmittfdbd1c52010-09-08 14:39:54 +0200189 return NULL;
Christof Schmittb62a8d92010-09-08 14:39:55 +0200190 atomic_set_mask(ZFCP_STATUS_COMMON_ERP_INUSE,
191 &zfcp_sdev->status);
192 erp_action = &zfcp_sdev->erp_action;
Swen Schillig14718e32010-11-17 14:23:43 +0100193 memset(erp_action, 0, sizeof(struct zfcp_erp_action));
194 erp_action->port = port;
195 erp_action->sdev = sdev;
Christof Schmittb62a8d92010-09-08 14:39:55 +0200196 if (!(atomic_read(&zfcp_sdev->status) &
197 ZFCP_STATUS_COMMON_RUNNING))
Christof Schmittfdbd1c52010-09-08 14:39:54 +0200198 act_status |= ZFCP_STATUS_ERP_CLOSE_ONLY;
Christof Schmitt287ac012008-07-02 10:56:40 +0200199 break;
200
201 case ZFCP_ERP_ACTION_REOPEN_PORT:
202 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
Christof Schmitt615f59e2010-02-17 11:18:56 +0100203 if (!get_device(&port->dev))
Swen Schillig6b183332009-11-24 16:54:05 +0100204 return NULL;
Christof Schmitt287ac012008-07-02 10:56:40 +0200205 zfcp_erp_action_dismiss_port(port);
206 atomic_set_mask(ZFCP_STATUS_COMMON_ERP_INUSE, &port->status);
207 erp_action = &port->erp_action;
Swen Schillig14718e32010-11-17 14:23:43 +0100208 memset(erp_action, 0, sizeof(struct zfcp_erp_action));
209 erp_action->port = port;
Christof Schmitt287ac012008-07-02 10:56:40 +0200210 if (!(atomic_read(&port->status) & ZFCP_STATUS_COMMON_RUNNING))
Christof Schmittfdbd1c52010-09-08 14:39:54 +0200211 act_status |= ZFCP_STATUS_ERP_CLOSE_ONLY;
Christof Schmitt287ac012008-07-02 10:56:40 +0200212 break;
213
214 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
Swen Schilligf3450c72009-11-24 16:53:59 +0100215 kref_get(&adapter->ref);
Christof Schmitt287ac012008-07-02 10:56:40 +0200216 zfcp_erp_action_dismiss_adapter(adapter);
217 atomic_set_mask(ZFCP_STATUS_COMMON_ERP_INUSE, &adapter->status);
218 erp_action = &adapter->erp_action;
Swen Schillig14718e32010-11-17 14:23:43 +0100219 memset(erp_action, 0, sizeof(struct zfcp_erp_action));
Christof Schmitt287ac012008-07-02 10:56:40 +0200220 if (!(atomic_read(&adapter->status) &
221 ZFCP_STATUS_COMMON_RUNNING))
Christof Schmittfdbd1c52010-09-08 14:39:54 +0200222 act_status |= ZFCP_STATUS_ERP_CLOSE_ONLY;
Christof Schmitt287ac012008-07-02 10:56:40 +0200223 break;
224
225 default:
226 return NULL;
227 }
228
Christof Schmitt287ac012008-07-02 10:56:40 +0200229 erp_action->adapter = adapter;
Christof Schmitt287ac012008-07-02 10:56:40 +0200230 erp_action->action = need;
Christof Schmittfdbd1c52010-09-08 14:39:54 +0200231 erp_action->status = act_status;
Christof Schmitt287ac012008-07-02 10:56:40 +0200232
233 return erp_action;
234}
235
236static int zfcp_erp_action_enqueue(int want, struct zfcp_adapter *adapter,
237 struct zfcp_port *port,
Christof Schmittb62a8d92010-09-08 14:39:55 +0200238 struct scsi_device *sdev,
Swen Schilligea4a3a62010-12-02 15:16:16 +0100239 char *id, u32 act_status)
Christof Schmitt287ac012008-07-02 10:56:40 +0200240{
241 int retval = 1, need;
Swen Schilligae0904f2010-12-02 15:16:12 +0100242 struct zfcp_erp_action *act;
Christof Schmitt287ac012008-07-02 10:56:40 +0200243
Christof Schmitt347c6a92009-08-18 15:43:25 +0200244 if (!adapter->erp_thread)
Christof Schmitt287ac012008-07-02 10:56:40 +0200245 return -EIO;
246
Christof Schmittb62a8d92010-09-08 14:39:55 +0200247 need = zfcp_erp_required_act(want, adapter, port, sdev);
Christof Schmitt287ac012008-07-02 10:56:40 +0200248 if (!need)
249 goto out;
250
Christof Schmittb62a8d92010-09-08 14:39:55 +0200251 act = zfcp_erp_setup_act(need, act_status, adapter, port, sdev);
Christof Schmitt287ac012008-07-02 10:56:40 +0200252 if (!act)
253 goto out;
Christof Schmittfdbd1c52010-09-08 14:39:54 +0200254 atomic_set_mask(ZFCP_STATUS_ADAPTER_ERP_PENDING, &adapter->status);
Christof Schmitt287ac012008-07-02 10:56:40 +0200255 ++adapter->erp_total_count;
256 list_add_tail(&act->list, &adapter->erp_ready_head);
Christof Schmitt347c6a92009-08-18 15:43:25 +0200257 wake_up(&adapter->erp_ready_wq);
Christof Schmitt287ac012008-07-02 10:56:40 +0200258 retval = 0;
259 out:
Swen Schilligae0904f2010-12-02 15:16:12 +0100260 zfcp_dbf_rec_trig(id, adapter, port, sdev, want, need);
Christof Schmitt287ac012008-07-02 10:56:40 +0200261 return retval;
262}
263
264static int _zfcp_erp_adapter_reopen(struct zfcp_adapter *adapter,
Swen Schilligea4a3a62010-12-02 15:16:16 +0100265 int clear_mask, char *id)
Christof Schmitt287ac012008-07-02 10:56:40 +0200266{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 zfcp_erp_adapter_block(adapter, clear_mask);
Christof Schmitta2fa0ae2009-03-02 13:09:08 +0100268 zfcp_scsi_schedule_rports_block(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
Christof Schmitt287ac012008-07-02 10:56:40 +0200270 /* ensure propagation of failed status to new devices */
271 if (atomic_read(&adapter->status) & ZFCP_STATUS_COMMON_ERP_FAILED) {
Swen Schilligedaed8592010-09-08 14:40:01 +0200272 zfcp_erp_set_adapter_status(adapter,
273 ZFCP_STATUS_COMMON_ERP_FAILED);
Christof Schmitt287ac012008-07-02 10:56:40 +0200274 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 }
Christof Schmitt287ac012008-07-02 10:56:40 +0200276 return zfcp_erp_action_enqueue(ZFCP_ERP_ACTION_REOPEN_ADAPTER,
Swen Schilligea4a3a62010-12-02 15:16:16 +0100277 adapter, NULL, NULL, id, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278}
279
280/**
Christof Schmitt287ac012008-07-02 10:56:40 +0200281 * zfcp_erp_adapter_reopen - Reopen adapter.
282 * @adapter: Adapter to reopen.
283 * @clear: Status flags to clear.
284 * @id: Id for debug trace event.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 */
Swen Schilligea4a3a62010-12-02 15:16:16 +0100286void zfcp_erp_adapter_reopen(struct zfcp_adapter *adapter, int clear, char *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287{
Christof Schmitt287ac012008-07-02 10:56:40 +0200288 unsigned long flags;
289
Swen Schilligecf0c772009-11-24 16:53:58 +0100290 zfcp_erp_adapter_block(adapter, clear);
291 zfcp_scsi_schedule_rports_block(adapter);
292
293 write_lock_irqsave(&adapter->erp_lock, flags);
294 if (atomic_read(&adapter->status) & ZFCP_STATUS_COMMON_ERP_FAILED)
Swen Schilligedaed8592010-09-08 14:40:01 +0200295 zfcp_erp_set_adapter_status(adapter,
296 ZFCP_STATUS_COMMON_ERP_FAILED);
Swen Schilligecf0c772009-11-24 16:53:58 +0100297 else
298 zfcp_erp_action_enqueue(ZFCP_ERP_ACTION_REOPEN_ADAPTER, adapter,
Swen Schilligea4a3a62010-12-02 15:16:16 +0100299 NULL, NULL, id, 0);
Swen Schilligecf0c772009-11-24 16:53:58 +0100300 write_unlock_irqrestore(&adapter->erp_lock, flags);
Christof Schmitt287ac012008-07-02 10:56:40 +0200301}
302
303/**
304 * zfcp_erp_adapter_shutdown - Shutdown adapter.
305 * @adapter: Adapter to shut down.
306 * @clear: Status flags to clear.
307 * @id: Id for debug trace event.
Christof Schmitt287ac012008-07-02 10:56:40 +0200308 */
309void zfcp_erp_adapter_shutdown(struct zfcp_adapter *adapter, int clear,
Swen Schilligea4a3a62010-12-02 15:16:16 +0100310 char *id)
Christof Schmitt287ac012008-07-02 10:56:40 +0200311{
312 int flags = ZFCP_STATUS_COMMON_RUNNING | ZFCP_STATUS_COMMON_ERP_FAILED;
Swen Schilligea4a3a62010-12-02 15:16:16 +0100313 zfcp_erp_adapter_reopen(adapter, clear | flags, id);
Christof Schmitt287ac012008-07-02 10:56:40 +0200314}
315
316/**
317 * zfcp_erp_port_shutdown - Shutdown port
318 * @port: Port to shut down.
319 * @clear: Status flags to clear.
320 * @id: Id for debug trace event.
Christof Schmitt287ac012008-07-02 10:56:40 +0200321 */
Swen Schilligea4a3a62010-12-02 15:16:16 +0100322void zfcp_erp_port_shutdown(struct zfcp_port *port, int clear, char *id)
Christof Schmitt287ac012008-07-02 10:56:40 +0200323{
324 int flags = ZFCP_STATUS_COMMON_RUNNING | ZFCP_STATUS_COMMON_ERP_FAILED;
Swen Schilligea4a3a62010-12-02 15:16:16 +0100325 zfcp_erp_port_reopen(port, clear | flags, id);
Christof Schmitt287ac012008-07-02 10:56:40 +0200326}
327
Christof Schmitt287ac012008-07-02 10:56:40 +0200328static void zfcp_erp_port_block(struct zfcp_port *port, int clear)
329{
Swen Schilligedaed8592010-09-08 14:40:01 +0200330 zfcp_erp_clear_port_status(port,
331 ZFCP_STATUS_COMMON_UNBLOCKED | clear);
Christof Schmitt287ac012008-07-02 10:56:40 +0200332}
333
Swen Schilligea4a3a62010-12-02 15:16:16 +0100334static void _zfcp_erp_port_forced_reopen(struct zfcp_port *port, int clear,
335 char *id)
Christof Schmitt287ac012008-07-02 10:56:40 +0200336{
337 zfcp_erp_port_block(port, clear);
Christof Schmitta2fa0ae2009-03-02 13:09:08 +0100338 zfcp_scsi_schedule_rport_block(port);
Christof Schmitt287ac012008-07-02 10:56:40 +0200339
340 if (atomic_read(&port->status) & ZFCP_STATUS_COMMON_ERP_FAILED)
341 return;
342
343 zfcp_erp_action_enqueue(ZFCP_ERP_ACTION_REOPEN_PORT_FORCED,
Swen Schilligea4a3a62010-12-02 15:16:16 +0100344 port->adapter, port, NULL, id, 0);
Christof Schmitt287ac012008-07-02 10:56:40 +0200345}
346
347/**
348 * zfcp_erp_port_forced_reopen - Forced close of port and open again
349 * @port: Port to force close and to reopen.
Swen Schilligea4a3a62010-12-02 15:16:16 +0100350 * @clear: Status flags to clear.
Christof Schmitt287ac012008-07-02 10:56:40 +0200351 * @id: Id for debug trace event.
Christof Schmitt287ac012008-07-02 10:56:40 +0200352 */
Swen Schilligea4a3a62010-12-02 15:16:16 +0100353void zfcp_erp_port_forced_reopen(struct zfcp_port *port, int clear, char *id)
Christof Schmitt287ac012008-07-02 10:56:40 +0200354{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 unsigned long flags;
356 struct zfcp_adapter *adapter = port->adapter;
357
Swen Schilligecf0c772009-11-24 16:53:58 +0100358 write_lock_irqsave(&adapter->erp_lock, flags);
Swen Schilligea4a3a62010-12-02 15:16:16 +0100359 _zfcp_erp_port_forced_reopen(port, clear, id);
Swen Schilligecf0c772009-11-24 16:53:58 +0100360 write_unlock_irqrestore(&adapter->erp_lock, flags);
Christof Schmitt287ac012008-07-02 10:56:40 +0200361}
362
Swen Schilligea4a3a62010-12-02 15:16:16 +0100363static int _zfcp_erp_port_reopen(struct zfcp_port *port, int clear, char *id)
Christof Schmitt287ac012008-07-02 10:56:40 +0200364{
365 zfcp_erp_port_block(port, clear);
Christof Schmitta2fa0ae2009-03-02 13:09:08 +0100366 zfcp_scsi_schedule_rport_block(port);
Christof Schmitt287ac012008-07-02 10:56:40 +0200367
368 if (atomic_read(&port->status) & ZFCP_STATUS_COMMON_ERP_FAILED) {
369 /* ensure propagation of failed status to new devices */
Swen Schilligedaed8592010-09-08 14:40:01 +0200370 zfcp_erp_set_port_status(port, ZFCP_STATUS_COMMON_ERP_FAILED);
Christof Schmitt287ac012008-07-02 10:56:40 +0200371 return -EIO;
372 }
373
374 return zfcp_erp_action_enqueue(ZFCP_ERP_ACTION_REOPEN_PORT,
Swen Schilligea4a3a62010-12-02 15:16:16 +0100375 port->adapter, port, NULL, id, 0);
Christof Schmitt287ac012008-07-02 10:56:40 +0200376}
377
378/**
379 * zfcp_erp_port_reopen - trigger remote port recovery
380 * @port: port to recover
381 * @clear_mask: flags in port status to be cleared
Swen Schilligea4a3a62010-12-02 15:16:16 +0100382 * @id: Id for debug trace event.
Christof Schmitt287ac012008-07-02 10:56:40 +0200383 *
384 * Returns 0 if recovery has been triggered, < 0 if not.
385 */
Swen Schilligea4a3a62010-12-02 15:16:16 +0100386int zfcp_erp_port_reopen(struct zfcp_port *port, int clear, char *id)
Christof Schmitt287ac012008-07-02 10:56:40 +0200387{
Christof Schmitt287ac012008-07-02 10:56:40 +0200388 int retval;
Swen Schilligecf0c772009-11-24 16:53:58 +0100389 unsigned long flags;
Christof Schmitt287ac012008-07-02 10:56:40 +0200390 struct zfcp_adapter *adapter = port->adapter;
391
Swen Schilligecf0c772009-11-24 16:53:58 +0100392 write_lock_irqsave(&adapter->erp_lock, flags);
Swen Schilligea4a3a62010-12-02 15:16:16 +0100393 retval = _zfcp_erp_port_reopen(port, clear, id);
Swen Schilligecf0c772009-11-24 16:53:58 +0100394 write_unlock_irqrestore(&adapter->erp_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
396 return retval;
397}
398
Christof Schmittb62a8d92010-09-08 14:39:55 +0200399static void zfcp_erp_lun_block(struct scsi_device *sdev, int clear_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400{
Swen Schilligedaed8592010-09-08 14:40:01 +0200401 zfcp_erp_clear_lun_status(sdev,
402 ZFCP_STATUS_COMMON_UNBLOCKED | clear_mask);
Christof Schmitt287ac012008-07-02 10:56:40 +0200403}
404
Christof Schmittb62a8d92010-09-08 14:39:55 +0200405static void _zfcp_erp_lun_reopen(struct scsi_device *sdev, int clear, char *id,
Swen Schilligea4a3a62010-12-02 15:16:16 +0100406 u32 act_status)
Christof Schmitt287ac012008-07-02 10:56:40 +0200407{
Christof Schmittb62a8d92010-09-08 14:39:55 +0200408 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
409 struct zfcp_adapter *adapter = zfcp_sdev->port->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
Christof Schmittb62a8d92010-09-08 14:39:55 +0200411 zfcp_erp_lun_block(sdev, clear);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
Christof Schmittb62a8d92010-09-08 14:39:55 +0200413 if (atomic_read(&zfcp_sdev->status) & ZFCP_STATUS_COMMON_ERP_FAILED)
Christof Schmitt287ac012008-07-02 10:56:40 +0200414 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
Christof Schmittb62a8d92010-09-08 14:39:55 +0200416 zfcp_erp_action_enqueue(ZFCP_ERP_ACTION_REOPEN_LUN, adapter,
Swen Schilligea4a3a62010-12-02 15:16:16 +0100417 zfcp_sdev->port, sdev, id, act_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418}
419
420/**
Christof Schmittb62a8d92010-09-08 14:39:55 +0200421 * zfcp_erp_lun_reopen - initiate reopen of a LUN
422 * @sdev: SCSI device / LUN to be reopened
423 * @clear_mask: specifies flags in LUN status to be cleared
Swen Schilligea4a3a62010-12-02 15:16:16 +0100424 * @id: Id for debug trace event.
425 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 * Return: 0 on success, < 0 on error
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 */
Swen Schilligea4a3a62010-12-02 15:16:16 +0100428void zfcp_erp_lun_reopen(struct scsi_device *sdev, int clear, char *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 unsigned long flags;
Christof Schmittb62a8d92010-09-08 14:39:55 +0200431 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
432 struct zfcp_port *port = zfcp_sdev->port;
Christof Schmitt287ac012008-07-02 10:56:40 +0200433 struct zfcp_adapter *adapter = port->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
Swen Schilligecf0c772009-11-24 16:53:58 +0100435 write_lock_irqsave(&adapter->erp_lock, flags);
Swen Schilligea4a3a62010-12-02 15:16:16 +0100436 _zfcp_erp_lun_reopen(sdev, clear, id, 0);
Swen Schilligecf0c772009-11-24 16:53:58 +0100437 write_unlock_irqrestore(&adapter->erp_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438}
439
Christof Schmittfdbd1c52010-09-08 14:39:54 +0200440/**
Christof Schmittb62a8d92010-09-08 14:39:55 +0200441 * zfcp_erp_lun_shutdown - Shutdown LUN
442 * @sdev: SCSI device / LUN to shut down.
Christof Schmittfdbd1c52010-09-08 14:39:54 +0200443 * @clear: Status flags to clear.
444 * @id: Id for debug trace event.
Christof Schmittfdbd1c52010-09-08 14:39:54 +0200445 */
Swen Schilligea4a3a62010-12-02 15:16:16 +0100446void zfcp_erp_lun_shutdown(struct scsi_device *sdev, int clear, char *id)
Christof Schmittfdbd1c52010-09-08 14:39:54 +0200447{
448 int flags = ZFCP_STATUS_COMMON_RUNNING | ZFCP_STATUS_COMMON_ERP_FAILED;
Swen Schilligea4a3a62010-12-02 15:16:16 +0100449 zfcp_erp_lun_reopen(sdev, clear | flags, id);
Christof Schmittfdbd1c52010-09-08 14:39:54 +0200450}
451
452/**
Christof Schmittb62a8d92010-09-08 14:39:55 +0200453 * zfcp_erp_lun_shutdown_wait - Shutdown LUN and wait for erp completion
454 * @sdev: SCSI device / LUN to shut down.
Christof Schmittfdbd1c52010-09-08 14:39:54 +0200455 * @id: Id for debug trace event.
456 *
Christof Schmittb62a8d92010-09-08 14:39:55 +0200457 * Do not acquire a reference for the LUN when creating the ERP
Christof Schmittfdbd1c52010-09-08 14:39:54 +0200458 * action. It is safe, because this function waits for the ERP to
Christof Schmittb62a8d92010-09-08 14:39:55 +0200459 * complete first. This allows to shutdown the LUN, even when the SCSI
460 * device is in the state SDEV_DEL when scsi_device_get will fail.
Christof Schmittfdbd1c52010-09-08 14:39:54 +0200461 */
Christof Schmittb62a8d92010-09-08 14:39:55 +0200462void zfcp_erp_lun_shutdown_wait(struct scsi_device *sdev, char *id)
Christof Schmittfdbd1c52010-09-08 14:39:54 +0200463{
464 unsigned long flags;
Christof Schmittb62a8d92010-09-08 14:39:55 +0200465 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
466 struct zfcp_port *port = zfcp_sdev->port;
Christof Schmittfdbd1c52010-09-08 14:39:54 +0200467 struct zfcp_adapter *adapter = port->adapter;
468 int clear = ZFCP_STATUS_COMMON_RUNNING | ZFCP_STATUS_COMMON_ERP_FAILED;
469
470 write_lock_irqsave(&adapter->erp_lock, flags);
Swen Schilligea4a3a62010-12-02 15:16:16 +0100471 _zfcp_erp_lun_reopen(sdev, clear, id, ZFCP_STATUS_ERP_NO_REF);
Christof Schmittfdbd1c52010-09-08 14:39:54 +0200472 write_unlock_irqrestore(&adapter->erp_lock, flags);
473
474 zfcp_erp_wait(adapter);
475}
476
Christof Schmitt287ac012008-07-02 10:56:40 +0200477static int status_change_set(unsigned long mask, atomic_t *status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478{
Christof Schmitt287ac012008-07-02 10:56:40 +0200479 return (atomic_read(status) ^ mask) & mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480}
481
Andreas Herrmannf6c0e7a2006-08-02 11:05:52 +0200482static void zfcp_erp_adapter_unblock(struct zfcp_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483{
Christof Schmitt287ac012008-07-02 10:56:40 +0200484 if (status_change_set(ZFCP_STATUS_COMMON_UNBLOCKED, &adapter->status))
Swen Schilligae0904f2010-12-02 15:16:12 +0100485 zfcp_dbf_rec_run("eraubl1", &adapter->erp_action);
Christof Schmitt287ac012008-07-02 10:56:40 +0200486 atomic_set_mask(ZFCP_STATUS_COMMON_UNBLOCKED, &adapter->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487}
488
Christof Schmitt287ac012008-07-02 10:56:40 +0200489static void zfcp_erp_port_unblock(struct zfcp_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490{
Christof Schmitt287ac012008-07-02 10:56:40 +0200491 if (status_change_set(ZFCP_STATUS_COMMON_UNBLOCKED, &port->status))
Swen Schilligae0904f2010-12-02 15:16:12 +0100492 zfcp_dbf_rec_run("erpubl1", &port->erp_action);
Christof Schmitt287ac012008-07-02 10:56:40 +0200493 atomic_set_mask(ZFCP_STATUS_COMMON_UNBLOCKED, &port->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494}
495
Christof Schmittb62a8d92010-09-08 14:39:55 +0200496static void zfcp_erp_lun_unblock(struct scsi_device *sdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497{
Christof Schmittb62a8d92010-09-08 14:39:55 +0200498 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
499
500 if (status_change_set(ZFCP_STATUS_COMMON_UNBLOCKED, &zfcp_sdev->status))
Swen Schilligae0904f2010-12-02 15:16:12 +0100501 zfcp_dbf_rec_run("erlubl1", &sdev_to_zfcp(sdev)->erp_action);
Christof Schmittb62a8d92010-09-08 14:39:55 +0200502 atomic_set_mask(ZFCP_STATUS_COMMON_UNBLOCKED, &zfcp_sdev->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503}
504
Christof Schmitt287ac012008-07-02 10:56:40 +0200505static void zfcp_erp_action_to_running(struct zfcp_erp_action *erp_action)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506{
Christof Schmitt287ac012008-07-02 10:56:40 +0200507 list_move(&erp_action->list, &erp_action->adapter->erp_running_head);
Swen Schilligae0904f2010-12-02 15:16:12 +0100508 zfcp_dbf_rec_run("erator1", erp_action);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509}
510
Christof Schmitt287ac012008-07-02 10:56:40 +0200511static void zfcp_erp_strategy_check_fsfreq(struct zfcp_erp_action *act)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512{
Christof Schmitt287ac012008-07-02 10:56:40 +0200513 struct zfcp_adapter *adapter = act->adapter;
Christof Schmitte60a6d62010-02-17 11:18:49 +0100514 struct zfcp_fsf_req *req;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
Christof Schmitte60a6d62010-02-17 11:18:49 +0100516 if (!act->fsf_req_id)
Christof Schmitt287ac012008-07-02 10:56:40 +0200517 return;
518
Christof Schmittb6bd2fb2010-02-17 11:18:50 +0100519 spin_lock(&adapter->req_list->lock);
520 req = _zfcp_reqlist_find(adapter->req_list, act->fsf_req_id);
Christof Schmitte60a6d62010-02-17 11:18:49 +0100521 if (req && req->erp_action == act) {
Christof Schmitt287ac012008-07-02 10:56:40 +0200522 if (act->status & (ZFCP_STATUS_ERP_DISMISSED |
523 ZFCP_STATUS_ERP_TIMEDOUT)) {
Christof Schmitte60a6d62010-02-17 11:18:49 +0100524 req->status |= ZFCP_STATUS_FSFREQ_DISMISSED;
Swen Schilligae0904f2010-12-02 15:16:12 +0100525 zfcp_dbf_rec_run("erscf_1", act);
Christof Schmitte60a6d62010-02-17 11:18:49 +0100526 req->erp_action = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 }
Christof Schmitt287ac012008-07-02 10:56:40 +0200528 if (act->status & ZFCP_STATUS_ERP_TIMEDOUT)
Swen Schilligae0904f2010-12-02 15:16:12 +0100529 zfcp_dbf_rec_run("erscf_2", act);
Christof Schmitte60a6d62010-02-17 11:18:49 +0100530 if (req->status & ZFCP_STATUS_FSFREQ_DISMISSED)
531 act->fsf_req_id = 0;
Christof Schmitt287ac012008-07-02 10:56:40 +0200532 } else
Christof Schmitte60a6d62010-02-17 11:18:49 +0100533 act->fsf_req_id = 0;
Christof Schmittb6bd2fb2010-02-17 11:18:50 +0100534 spin_unlock(&adapter->req_list->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535}
536
Andreas Herrmannf6c0e7a2006-08-02 11:05:52 +0200537/**
Christof Schmitt287ac012008-07-02 10:56:40 +0200538 * zfcp_erp_notify - Trigger ERP action.
539 * @erp_action: ERP action to continue.
540 * @set_mask: ERP action status flags to set.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 */
Christof Schmitt287ac012008-07-02 10:56:40 +0200542void zfcp_erp_notify(struct zfcp_erp_action *erp_action, unsigned long set_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543{
544 struct zfcp_adapter *adapter = erp_action->adapter;
545 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
547 write_lock_irqsave(&adapter->erp_lock, flags);
Christof Schmitt287ac012008-07-02 10:56:40 +0200548 if (zfcp_erp_action_exists(erp_action) == ZFCP_ERP_ACTION_RUNNING) {
549 erp_action->status |= set_mask;
550 zfcp_erp_action_ready(erp_action);
551 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 write_unlock_irqrestore(&adapter->erp_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553}
554
Andreas Herrmannf6c0e7a2006-08-02 11:05:52 +0200555/**
Christof Schmitt287ac012008-07-02 10:56:40 +0200556 * zfcp_erp_timeout_handler - Trigger ERP action from timed out ERP request
557 * @data: ERP action (from timer data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 */
Christof Schmitt287ac012008-07-02 10:56:40 +0200559void zfcp_erp_timeout_handler(unsigned long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560{
Christof Schmitt287ac012008-07-02 10:56:40 +0200561 struct zfcp_erp_action *act = (struct zfcp_erp_action *) data;
562 zfcp_erp_notify(act, ZFCP_STATUS_ERP_TIMEDOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563}
564
Christof Schmitt287ac012008-07-02 10:56:40 +0200565static void zfcp_erp_memwait_handler(unsigned long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566{
Christof Schmitt287ac012008-07-02 10:56:40 +0200567 zfcp_erp_notify((struct zfcp_erp_action *)data, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568}
569
Christof Schmitt287ac012008-07-02 10:56:40 +0200570static void zfcp_erp_strategy_memwait(struct zfcp_erp_action *erp_action)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 init_timer(&erp_action->timer);
573 erp_action->timer.function = zfcp_erp_memwait_handler;
574 erp_action->timer.data = (unsigned long) erp_action;
Christof Schmitt287ac012008-07-02 10:56:40 +0200575 erp_action->timer.expires = jiffies + HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 add_timer(&erp_action->timer);
Christof Schmitt287ac012008-07-02 10:56:40 +0200577}
578
579static void _zfcp_erp_port_reopen_all(struct zfcp_adapter *adapter,
Swen Schilligea4a3a62010-12-02 15:16:16 +0100580 int clear, char *id)
Christof Schmitt287ac012008-07-02 10:56:40 +0200581{
582 struct zfcp_port *port;
583
Swen Schilligecf0c772009-11-24 16:53:58 +0100584 read_lock(&adapter->port_list_lock);
585 list_for_each_entry(port, &adapter->port_list, list)
Swen Schilligea4a3a62010-12-02 15:16:16 +0100586 _zfcp_erp_port_reopen(port, clear, id);
Swen Schilligecf0c772009-11-24 16:53:58 +0100587 read_unlock(&adapter->port_list_lock);
Christof Schmitt287ac012008-07-02 10:56:40 +0200588}
589
Christof Schmittb62a8d92010-09-08 14:39:55 +0200590static void _zfcp_erp_lun_reopen_all(struct zfcp_port *port, int clear,
Swen Schilligea4a3a62010-12-02 15:16:16 +0100591 char *id)
Christof Schmitt287ac012008-07-02 10:56:40 +0200592{
Christof Schmittb62a8d92010-09-08 14:39:55 +0200593 struct scsi_device *sdev;
Christof Schmitt287ac012008-07-02 10:56:40 +0200594
Christof Schmittb62a8d92010-09-08 14:39:55 +0200595 shost_for_each_device(sdev, port->adapter->scsi_host)
596 if (sdev_to_zfcp(sdev)->port == port)
Swen Schilligea4a3a62010-12-02 15:16:16 +0100597 _zfcp_erp_lun_reopen(sdev, clear, id, 0);
Christof Schmitt287ac012008-07-02 10:56:40 +0200598}
599
Christof Schmitt85600f72009-07-13 15:06:09 +0200600static void zfcp_erp_strategy_followup_failed(struct zfcp_erp_action *act)
Christof Schmitt287ac012008-07-02 10:56:40 +0200601{
Christof Schmitt287ac012008-07-02 10:56:40 +0200602 switch (act->action) {
Christof Schmitt287ac012008-07-02 10:56:40 +0200603 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
Swen Schilligea4a3a62010-12-02 15:16:16 +0100604 _zfcp_erp_adapter_reopen(act->adapter, 0, "ersff_1");
Christof Schmitt287ac012008-07-02 10:56:40 +0200605 break;
Christof Schmitt287ac012008-07-02 10:56:40 +0200606 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
Swen Schilligea4a3a62010-12-02 15:16:16 +0100607 _zfcp_erp_port_forced_reopen(act->port, 0, "ersff_2");
Christof Schmitt287ac012008-07-02 10:56:40 +0200608 break;
Christof Schmitt287ac012008-07-02 10:56:40 +0200609 case ZFCP_ERP_ACTION_REOPEN_PORT:
Swen Schilligea4a3a62010-12-02 15:16:16 +0100610 _zfcp_erp_port_reopen(act->port, 0, "ersff_3");
Christof Schmitt287ac012008-07-02 10:56:40 +0200611 break;
Christof Schmittb62a8d92010-09-08 14:39:55 +0200612 case ZFCP_ERP_ACTION_REOPEN_LUN:
Swen Schilligea4a3a62010-12-02 15:16:16 +0100613 _zfcp_erp_lun_reopen(act->sdev, 0, "ersff_4", 0);
Christof Schmitt85600f72009-07-13 15:06:09 +0200614 break;
615 }
616}
617
618static void zfcp_erp_strategy_followup_success(struct zfcp_erp_action *act)
619{
620 switch (act->action) {
621 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
Swen Schilligea4a3a62010-12-02 15:16:16 +0100622 _zfcp_erp_port_reopen_all(act->adapter, 0, "ersfs_1");
Christof Schmitt85600f72009-07-13 15:06:09 +0200623 break;
624 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
Swen Schilligea4a3a62010-12-02 15:16:16 +0100625 _zfcp_erp_port_reopen(act->port, 0, "ersfs_2");
Christof Schmitt85600f72009-07-13 15:06:09 +0200626 break;
627 case ZFCP_ERP_ACTION_REOPEN_PORT:
Swen Schilligea4a3a62010-12-02 15:16:16 +0100628 _zfcp_erp_lun_reopen_all(act->port, 0, "ersfs_3");
Christof Schmitt287ac012008-07-02 10:56:40 +0200629 break;
630 }
631}
632
633static void zfcp_erp_wakeup(struct zfcp_adapter *adapter)
634{
635 unsigned long flags;
636
Swen Schilligecf0c772009-11-24 16:53:58 +0100637 read_lock_irqsave(&adapter->erp_lock, flags);
Christof Schmitt287ac012008-07-02 10:56:40 +0200638 if (list_empty(&adapter->erp_ready_head) &&
639 list_empty(&adapter->erp_running_head)) {
640 atomic_clear_mask(ZFCP_STATUS_ADAPTER_ERP_PENDING,
641 &adapter->status);
642 wake_up(&adapter->erp_done_wqh);
643 }
Swen Schilligecf0c772009-11-24 16:53:58 +0100644 read_unlock_irqrestore(&adapter->erp_lock, flags);
Christof Schmitt287ac012008-07-02 10:56:40 +0200645}
646
Christof Schmitt287ac012008-07-02 10:56:40 +0200647static void zfcp_erp_enqueue_ptp_port(struct zfcp_adapter *adapter)
648{
649 struct zfcp_port *port;
650 port = zfcp_port_enqueue(adapter, adapter->peer_wwpn, 0,
651 adapter->peer_d_id);
652 if (IS_ERR(port)) /* error or port already attached */
653 return;
Swen Schilligea4a3a62010-12-02 15:16:16 +0100654 _zfcp_erp_port_reopen(port, 0, "ereptp1");
Christof Schmitt287ac012008-07-02 10:56:40 +0200655}
656
657static int zfcp_erp_adapter_strat_fsf_xconf(struct zfcp_erp_action *erp_action)
658{
659 int retries;
660 int sleep = 1;
661 struct zfcp_adapter *adapter = erp_action->adapter;
662
663 atomic_clear_mask(ZFCP_STATUS_ADAPTER_XCONFIG_OK, &adapter->status);
664
665 for (retries = 7; retries; retries--) {
666 atomic_clear_mask(ZFCP_STATUS_ADAPTER_HOST_CON_INIT,
667 &adapter->status);
668 write_lock_irq(&adapter->erp_lock);
669 zfcp_erp_action_to_running(erp_action);
670 write_unlock_irq(&adapter->erp_lock);
671 if (zfcp_fsf_exchange_config_data(erp_action)) {
672 atomic_clear_mask(ZFCP_STATUS_ADAPTER_HOST_CON_INIT,
673 &adapter->status);
674 return ZFCP_ERP_FAILED;
675 }
676
Christof Schmitt347c6a92009-08-18 15:43:25 +0200677 wait_event(adapter->erp_ready_wq,
678 !list_empty(&adapter->erp_ready_head));
Christof Schmitt287ac012008-07-02 10:56:40 +0200679 if (erp_action->status & ZFCP_STATUS_ERP_TIMEDOUT)
680 break;
681
682 if (!(atomic_read(&adapter->status) &
683 ZFCP_STATUS_ADAPTER_HOST_CON_INIT))
684 break;
685
686 ssleep(sleep);
687 sleep *= 2;
688 }
689
690 atomic_clear_mask(ZFCP_STATUS_ADAPTER_HOST_CON_INIT,
691 &adapter->status);
692
693 if (!(atomic_read(&adapter->status) & ZFCP_STATUS_ADAPTER_XCONFIG_OK))
694 return ZFCP_ERP_FAILED;
695
696 if (fc_host_port_type(adapter->scsi_host) == FC_PORTTYPE_PTP)
697 zfcp_erp_enqueue_ptp_port(adapter);
698
699 return ZFCP_ERP_SUCCEEDED;
700}
701
702static int zfcp_erp_adapter_strategy_open_fsf_xport(struct zfcp_erp_action *act)
703{
704 int ret;
705 struct zfcp_adapter *adapter = act->adapter;
706
Christof Schmitt287ac012008-07-02 10:56:40 +0200707 write_lock_irq(&adapter->erp_lock);
708 zfcp_erp_action_to_running(act);
709 write_unlock_irq(&adapter->erp_lock);
710
711 ret = zfcp_fsf_exchange_port_data(act);
712 if (ret == -EOPNOTSUPP)
713 return ZFCP_ERP_SUCCEEDED;
714 if (ret)
715 return ZFCP_ERP_FAILED;
716
Swen Schilligae0904f2010-12-02 15:16:12 +0100717 zfcp_dbf_rec_run("erasox1", act);
Christof Schmitt347c6a92009-08-18 15:43:25 +0200718 wait_event(adapter->erp_ready_wq,
719 !list_empty(&adapter->erp_ready_head));
Swen Schilligae0904f2010-12-02 15:16:12 +0100720 zfcp_dbf_rec_run("erasox2", act);
Christof Schmitt287ac012008-07-02 10:56:40 +0200721 if (act->status & ZFCP_STATUS_ERP_TIMEDOUT)
722 return ZFCP_ERP_FAILED;
723
724 return ZFCP_ERP_SUCCEEDED;
725}
726
727static int zfcp_erp_adapter_strategy_open_fsf(struct zfcp_erp_action *act)
728{
729 if (zfcp_erp_adapter_strat_fsf_xconf(act) == ZFCP_ERP_FAILED)
730 return ZFCP_ERP_FAILED;
731
732 if (zfcp_erp_adapter_strategy_open_fsf_xport(act) == ZFCP_ERP_FAILED)
733 return ZFCP_ERP_FAILED;
734
Christof Schmitt8d88cf32010-06-21 10:11:33 +0200735 if (mempool_resize(act->adapter->pool.status_read_data,
736 act->adapter->stat_read_buf_num, GFP_KERNEL))
737 return ZFCP_ERP_FAILED;
738
739 if (mempool_resize(act->adapter->pool.status_read_req,
740 act->adapter->stat_read_buf_num, GFP_KERNEL))
741 return ZFCP_ERP_FAILED;
742
Christof Schmitt64deb6e2010-04-30 18:09:36 +0200743 atomic_set(&act->adapter->stat_miss, act->adapter->stat_read_buf_num);
Christof Schmitt287ac012008-07-02 10:56:40 +0200744 if (zfcp_status_read_refill(act->adapter))
745 return ZFCP_ERP_FAILED;
746
747 return ZFCP_ERP_SUCCEEDED;
748}
749
Swen Schilligcf13c082009-03-02 13:09:03 +0100750static void zfcp_erp_adapter_strategy_close(struct zfcp_erp_action *act)
Christof Schmitt287ac012008-07-02 10:56:40 +0200751{
Christof Schmitt287ac012008-07-02 10:56:40 +0200752 struct zfcp_adapter *adapter = act->adapter;
753
Christof Schmitt287ac012008-07-02 10:56:40 +0200754 /* close queues to ensure that buffers are not accessed by adapter */
Swen Schillig564e1c82009-08-18 15:43:19 +0200755 zfcp_qdio_close(adapter->qdio);
Christof Schmitt287ac012008-07-02 10:56:40 +0200756 zfcp_fsf_req_dismiss_all(adapter);
757 adapter->fsf_req_seq_no = 0;
Christof Schmitt55c770f2009-08-18 15:43:12 +0200758 zfcp_fc_wka_ports_force_offline(adapter->gs);
Christof Schmittb62a8d92010-09-08 14:39:55 +0200759 /* all ports and LUNs are closed */
Swen Schilligedaed8592010-09-08 14:40:01 +0200760 zfcp_erp_clear_adapter_status(adapter, ZFCP_STATUS_COMMON_OPEN);
Swen Schilligcf13c082009-03-02 13:09:03 +0100761
Christof Schmitt287ac012008-07-02 10:56:40 +0200762 atomic_clear_mask(ZFCP_STATUS_ADAPTER_XCONFIG_OK |
Swen Schilligcf13c082009-03-02 13:09:03 +0100763 ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED, &adapter->status);
764}
765
766static int zfcp_erp_adapter_strategy_open(struct zfcp_erp_action *act)
767{
768 struct zfcp_adapter *adapter = act->adapter;
769
Christof Schmitt3d63d3b2010-12-02 15:16:17 +0100770 if (zfcp_qdio_open(adapter->qdio)) {
Swen Schilligcf13c082009-03-02 13:09:03 +0100771 atomic_clear_mask(ZFCP_STATUS_ADAPTER_XCONFIG_OK |
772 ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED,
773 &adapter->status);
774 return ZFCP_ERP_FAILED;
775 }
776
777 if (zfcp_erp_adapter_strategy_open_fsf(act)) {
778 zfcp_erp_adapter_strategy_close(act);
779 return ZFCP_ERP_FAILED;
780 }
781
782 atomic_set_mask(ZFCP_STATUS_COMMON_OPEN, &adapter->status);
783
784 return ZFCP_ERP_SUCCEEDED;
Christof Schmitt287ac012008-07-02 10:56:40 +0200785}
786
787static int zfcp_erp_adapter_strategy(struct zfcp_erp_action *act)
788{
Swen Schilligcf13c082009-03-02 13:09:03 +0100789 struct zfcp_adapter *adapter = act->adapter;
Christof Schmitt287ac012008-07-02 10:56:40 +0200790
Swen Schilligcf13c082009-03-02 13:09:03 +0100791 if (atomic_read(&adapter->status) & ZFCP_STATUS_COMMON_OPEN) {
792 zfcp_erp_adapter_strategy_close(act);
793 if (act->status & ZFCP_STATUS_ERP_CLOSE_ONLY)
794 return ZFCP_ERP_EXIT;
795 }
Christof Schmitt287ac012008-07-02 10:56:40 +0200796
Swen Schilligcf13c082009-03-02 13:09:03 +0100797 if (zfcp_erp_adapter_strategy_open(act)) {
Christof Schmitt287ac012008-07-02 10:56:40 +0200798 ssleep(8);
Swen Schilligcf13c082009-03-02 13:09:03 +0100799 return ZFCP_ERP_FAILED;
800 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801
Swen Schilligcf13c082009-03-02 13:09:03 +0100802 return ZFCP_ERP_SUCCEEDED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803}
804
Christof Schmitt287ac012008-07-02 10:56:40 +0200805static int zfcp_erp_port_forced_strategy_close(struct zfcp_erp_action *act)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806{
Christof Schmitt287ac012008-07-02 10:56:40 +0200807 int retval;
808
809 retval = zfcp_fsf_close_physical_port(act);
810 if (retval == -ENOMEM)
811 return ZFCP_ERP_NOMEM;
812 act->step = ZFCP_ERP_STEP_PHYS_PORT_CLOSING;
813 if (retval)
814 return ZFCP_ERP_FAILED;
815
816 return ZFCP_ERP_CONTINUES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817}
818
Christof Schmitt287ac012008-07-02 10:56:40 +0200819static void zfcp_erp_port_strategy_clearstati(struct zfcp_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820{
Christof Schmitta5b11dd2009-03-02 13:08:54 +0100821 atomic_clear_mask(ZFCP_STATUS_COMMON_ACCESS_DENIED, &port->status);
Christof Schmitt287ac012008-07-02 10:56:40 +0200822}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823
Christof Schmitt287ac012008-07-02 10:56:40 +0200824static int zfcp_erp_port_forced_strategy(struct zfcp_erp_action *erp_action)
825{
826 struct zfcp_port *port = erp_action->port;
827 int status = atomic_read(&port->status);
828
829 switch (erp_action->step) {
830 case ZFCP_ERP_STEP_UNINITIALIZED:
831 zfcp_erp_port_strategy_clearstati(port);
832 if ((status & ZFCP_STATUS_PORT_PHYS_OPEN) &&
833 (status & ZFCP_STATUS_COMMON_OPEN))
834 return zfcp_erp_port_forced_strategy_close(erp_action);
835 else
836 return ZFCP_ERP_FAILED;
837
838 case ZFCP_ERP_STEP_PHYS_PORT_CLOSING:
Christof Schmittddb3e0c2009-07-13 15:06:08 +0200839 if (!(status & ZFCP_STATUS_PORT_PHYS_OPEN))
Christof Schmitt287ac012008-07-02 10:56:40 +0200840 return ZFCP_ERP_SUCCEEDED;
841 }
842 return ZFCP_ERP_FAILED;
843}
844
845static int zfcp_erp_port_strategy_close(struct zfcp_erp_action *erp_action)
846{
847 int retval;
848
849 retval = zfcp_fsf_close_port(erp_action);
850 if (retval == -ENOMEM)
851 return ZFCP_ERP_NOMEM;
852 erp_action->step = ZFCP_ERP_STEP_PORT_CLOSING;
853 if (retval)
854 return ZFCP_ERP_FAILED;
855 return ZFCP_ERP_CONTINUES;
856}
857
858static int zfcp_erp_port_strategy_open_port(struct zfcp_erp_action *erp_action)
859{
860 int retval;
861
862 retval = zfcp_fsf_open_port(erp_action);
863 if (retval == -ENOMEM)
864 return ZFCP_ERP_NOMEM;
865 erp_action->step = ZFCP_ERP_STEP_PORT_OPENING;
866 if (retval)
867 return ZFCP_ERP_FAILED;
868 return ZFCP_ERP_CONTINUES;
869}
870
Christof Schmitt287ac012008-07-02 10:56:40 +0200871static int zfcp_erp_open_ptp_port(struct zfcp_erp_action *act)
872{
873 struct zfcp_adapter *adapter = act->adapter;
874 struct zfcp_port *port = act->port;
875
876 if (port->wwpn != adapter->peer_wwpn) {
Swen Schilligedaed8592010-09-08 14:40:01 +0200877 zfcp_erp_set_port_status(port, ZFCP_STATUS_COMMON_ERP_FAILED);
Christof Schmitt287ac012008-07-02 10:56:40 +0200878 return ZFCP_ERP_FAILED;
879 }
880 port->d_id = adapter->peer_d_id;
Christof Schmitt287ac012008-07-02 10:56:40 +0200881 return zfcp_erp_port_strategy_open_port(act);
882}
883
884static int zfcp_erp_port_strategy_open_common(struct zfcp_erp_action *act)
885{
886 struct zfcp_adapter *adapter = act->adapter;
887 struct zfcp_port *port = act->port;
Christof Schmitt287ac012008-07-02 10:56:40 +0200888 int p_status = atomic_read(&port->status);
889
890 switch (act->step) {
891 case ZFCP_ERP_STEP_UNINITIALIZED:
892 case ZFCP_ERP_STEP_PHYS_PORT_CLOSING:
893 case ZFCP_ERP_STEP_PORT_CLOSING:
894 if (fc_host_port_type(adapter->scsi_host) == FC_PORTTYPE_PTP)
895 return zfcp_erp_open_ptp_port(act);
Christof Schmittb98478d2008-12-19 16:56:59 +0100896 if (!port->d_id) {
Christof Schmitt934aeb5872009-10-14 11:00:43 +0200897 zfcp_fc_trigger_did_lookup(port);
Christof Schmitt799b76d2009-08-18 15:43:20 +0200898 return ZFCP_ERP_EXIT;
Christof Schmitt287ac012008-07-02 10:56:40 +0200899 }
Christof Schmitt287ac012008-07-02 10:56:40 +0200900 return zfcp_erp_port_strategy_open_port(act);
901
902 case ZFCP_ERP_STEP_PORT_OPENING:
903 /* D_ID might have changed during open */
Swen Schillig5ab944f2008-10-01 12:42:17 +0200904 if (p_status & ZFCP_STATUS_COMMON_OPEN) {
Christof Schmitt934aeb5872009-10-14 11:00:43 +0200905 if (!port->d_id) {
906 zfcp_fc_trigger_did_lookup(port);
907 return ZFCP_ERP_EXIT;
Swen Schillig5ab944f2008-10-01 12:42:17 +0200908 }
Christof Schmitt934aeb5872009-10-14 11:00:43 +0200909 return ZFCP_ERP_SUCCEEDED;
Swen Schillig5ab944f2008-10-01 12:42:17 +0200910 }
Swen Schilligea460a82009-05-15 13:18:20 +0200911 if (port->d_id && !(p_status & ZFCP_STATUS_COMMON_NOESC)) {
912 port->d_id = 0;
Christof Schmittf7bd7c32010-07-08 09:53:10 +0200913 return ZFCP_ERP_FAILED;
Swen Schilligea460a82009-05-15 13:18:20 +0200914 }
915 /* fall through otherwise */
Christof Schmitt287ac012008-07-02 10:56:40 +0200916 }
917 return ZFCP_ERP_FAILED;
918}
919
Christof Schmitt287ac012008-07-02 10:56:40 +0200920static int zfcp_erp_port_strategy(struct zfcp_erp_action *erp_action)
921{
922 struct zfcp_port *port = erp_action->port;
Christof Schmitt934aeb5872009-10-14 11:00:43 +0200923 int p_status = atomic_read(&port->status);
Christof Schmitt287ac012008-07-02 10:56:40 +0200924
Christof Schmitt934aeb5872009-10-14 11:00:43 +0200925 if ((p_status & ZFCP_STATUS_COMMON_NOESC) &&
926 !(p_status & ZFCP_STATUS_COMMON_OPEN))
Swen Schillig5ab944f2008-10-01 12:42:17 +0200927 goto close_init_done;
928
Christof Schmitt287ac012008-07-02 10:56:40 +0200929 switch (erp_action->step) {
930 case ZFCP_ERP_STEP_UNINITIALIZED:
931 zfcp_erp_port_strategy_clearstati(port);
Christof Schmitt934aeb5872009-10-14 11:00:43 +0200932 if (p_status & ZFCP_STATUS_COMMON_OPEN)
Christof Schmitt287ac012008-07-02 10:56:40 +0200933 return zfcp_erp_port_strategy_close(erp_action);
934 break;
935
936 case ZFCP_ERP_STEP_PORT_CLOSING:
Christof Schmitt934aeb5872009-10-14 11:00:43 +0200937 if (p_status & ZFCP_STATUS_COMMON_OPEN)
Christof Schmitt287ac012008-07-02 10:56:40 +0200938 return ZFCP_ERP_FAILED;
939 break;
940 }
Swen Schillig5ab944f2008-10-01 12:42:17 +0200941
942close_init_done:
Christof Schmitt287ac012008-07-02 10:56:40 +0200943 if (erp_action->status & ZFCP_STATUS_ERP_CLOSE_ONLY)
944 return ZFCP_ERP_EXIT;
Christof Schmitt287ac012008-07-02 10:56:40 +0200945
Swen Schillig5ab944f2008-10-01 12:42:17 +0200946 return zfcp_erp_port_strategy_open_common(erp_action);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947}
948
Christof Schmittb62a8d92010-09-08 14:39:55 +0200949static void zfcp_erp_lun_strategy_clearstati(struct scsi_device *sdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950{
Christof Schmittb62a8d92010-09-08 14:39:55 +0200951 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
952
Swen Schillig44cc76f2008-10-01 12:42:16 +0200953 atomic_clear_mask(ZFCP_STATUS_COMMON_ACCESS_DENIED |
Christof Schmittb62a8d92010-09-08 14:39:55 +0200954 ZFCP_STATUS_LUN_SHARED | ZFCP_STATUS_LUN_READONLY,
955 &zfcp_sdev->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956}
957
Christof Schmittb62a8d92010-09-08 14:39:55 +0200958static int zfcp_erp_lun_strategy_close(struct zfcp_erp_action *erp_action)
Christof Schmitt287ac012008-07-02 10:56:40 +0200959{
Christof Schmittb62a8d92010-09-08 14:39:55 +0200960 int retval = zfcp_fsf_close_lun(erp_action);
Christof Schmitt287ac012008-07-02 10:56:40 +0200961 if (retval == -ENOMEM)
962 return ZFCP_ERP_NOMEM;
Christof Schmittb62a8d92010-09-08 14:39:55 +0200963 erp_action->step = ZFCP_ERP_STEP_LUN_CLOSING;
Christof Schmitt287ac012008-07-02 10:56:40 +0200964 if (retval)
965 return ZFCP_ERP_FAILED;
966 return ZFCP_ERP_CONTINUES;
967}
968
Christof Schmittb62a8d92010-09-08 14:39:55 +0200969static int zfcp_erp_lun_strategy_open(struct zfcp_erp_action *erp_action)
Christof Schmitt287ac012008-07-02 10:56:40 +0200970{
Christof Schmittb62a8d92010-09-08 14:39:55 +0200971 int retval = zfcp_fsf_open_lun(erp_action);
Christof Schmitt287ac012008-07-02 10:56:40 +0200972 if (retval == -ENOMEM)
973 return ZFCP_ERP_NOMEM;
Christof Schmittb62a8d92010-09-08 14:39:55 +0200974 erp_action->step = ZFCP_ERP_STEP_LUN_OPENING;
Christof Schmitt287ac012008-07-02 10:56:40 +0200975 if (retval)
976 return ZFCP_ERP_FAILED;
977 return ZFCP_ERP_CONTINUES;
978}
979
Christof Schmittb62a8d92010-09-08 14:39:55 +0200980static int zfcp_erp_lun_strategy(struct zfcp_erp_action *erp_action)
Christof Schmitt287ac012008-07-02 10:56:40 +0200981{
Christof Schmittb62a8d92010-09-08 14:39:55 +0200982 struct scsi_device *sdev = erp_action->sdev;
983 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
Christof Schmitt287ac012008-07-02 10:56:40 +0200984
985 switch (erp_action->step) {
986 case ZFCP_ERP_STEP_UNINITIALIZED:
Christof Schmittb62a8d92010-09-08 14:39:55 +0200987 zfcp_erp_lun_strategy_clearstati(sdev);
988 if (atomic_read(&zfcp_sdev->status) & ZFCP_STATUS_COMMON_OPEN)
989 return zfcp_erp_lun_strategy_close(erp_action);
Christof Schmitt287ac012008-07-02 10:56:40 +0200990 /* already closed, fall through */
Christof Schmittb62a8d92010-09-08 14:39:55 +0200991 case ZFCP_ERP_STEP_LUN_CLOSING:
992 if (atomic_read(&zfcp_sdev->status) & ZFCP_STATUS_COMMON_OPEN)
Christof Schmitt287ac012008-07-02 10:56:40 +0200993 return ZFCP_ERP_FAILED;
994 if (erp_action->status & ZFCP_STATUS_ERP_CLOSE_ONLY)
995 return ZFCP_ERP_EXIT;
Christof Schmittb62a8d92010-09-08 14:39:55 +0200996 return zfcp_erp_lun_strategy_open(erp_action);
Christof Schmitt287ac012008-07-02 10:56:40 +0200997
Christof Schmittb62a8d92010-09-08 14:39:55 +0200998 case ZFCP_ERP_STEP_LUN_OPENING:
999 if (atomic_read(&zfcp_sdev->status) & ZFCP_STATUS_COMMON_OPEN)
Christof Schmitt287ac012008-07-02 10:56:40 +02001000 return ZFCP_ERP_SUCCEEDED;
1001 }
1002 return ZFCP_ERP_FAILED;
1003}
1004
Christof Schmittb62a8d92010-09-08 14:39:55 +02001005static int zfcp_erp_strategy_check_lun(struct scsi_device *sdev, int result)
Christof Schmitt287ac012008-07-02 10:56:40 +02001006{
Christof Schmittb62a8d92010-09-08 14:39:55 +02001007 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
1008
Christof Schmitt287ac012008-07-02 10:56:40 +02001009 switch (result) {
1010 case ZFCP_ERP_SUCCEEDED :
Christof Schmittb62a8d92010-09-08 14:39:55 +02001011 atomic_set(&zfcp_sdev->erp_counter, 0);
1012 zfcp_erp_lun_unblock(sdev);
Christof Schmitt287ac012008-07-02 10:56:40 +02001013 break;
1014 case ZFCP_ERP_FAILED :
Christof Schmittb62a8d92010-09-08 14:39:55 +02001015 atomic_inc(&zfcp_sdev->erp_counter);
1016 if (atomic_read(&zfcp_sdev->erp_counter) > ZFCP_MAX_ERPS) {
1017 dev_err(&zfcp_sdev->port->adapter->ccw_device->dev,
1018 "ERP failed for LUN 0x%016Lx on "
Christof Schmittff3b24f2008-10-01 12:42:15 +02001019 "port 0x%016Lx\n",
Christof Schmittb62a8d92010-09-08 14:39:55 +02001020 (unsigned long long)zfcp_scsi_dev_lun(sdev),
1021 (unsigned long long)zfcp_sdev->port->wwpn);
Swen Schilligedaed8592010-09-08 14:40:01 +02001022 zfcp_erp_set_lun_status(sdev,
1023 ZFCP_STATUS_COMMON_ERP_FAILED);
Christof Schmittff3b24f2008-10-01 12:42:15 +02001024 }
Christof Schmitt287ac012008-07-02 10:56:40 +02001025 break;
1026 }
1027
Christof Schmittb62a8d92010-09-08 14:39:55 +02001028 if (atomic_read(&zfcp_sdev->status) & ZFCP_STATUS_COMMON_ERP_FAILED) {
1029 zfcp_erp_lun_block(sdev, 0);
Christof Schmitt287ac012008-07-02 10:56:40 +02001030 result = ZFCP_ERP_EXIT;
1031 }
1032 return result;
1033}
1034
1035static int zfcp_erp_strategy_check_port(struct zfcp_port *port, int result)
1036{
1037 switch (result) {
1038 case ZFCP_ERP_SUCCEEDED :
1039 atomic_set(&port->erp_counter, 0);
1040 zfcp_erp_port_unblock(port);
1041 break;
1042
1043 case ZFCP_ERP_FAILED :
1044 if (atomic_read(&port->status) & ZFCP_STATUS_COMMON_NOESC) {
1045 zfcp_erp_port_block(port, 0);
1046 result = ZFCP_ERP_EXIT;
1047 }
1048 atomic_inc(&port->erp_counter);
Christof Schmittff3b24f2008-10-01 12:42:15 +02001049 if (atomic_read(&port->erp_counter) > ZFCP_MAX_ERPS) {
1050 dev_err(&port->adapter->ccw_device->dev,
1051 "ERP failed for remote port 0x%016Lx\n",
Swen Schillig7ba58c92008-10-01 12:42:18 +02001052 (unsigned long long)port->wwpn);
Swen Schilligedaed8592010-09-08 14:40:01 +02001053 zfcp_erp_set_port_status(port,
1054 ZFCP_STATUS_COMMON_ERP_FAILED);
Christof Schmittff3b24f2008-10-01 12:42:15 +02001055 }
Christof Schmitt287ac012008-07-02 10:56:40 +02001056 break;
1057 }
1058
1059 if (atomic_read(&port->status) & ZFCP_STATUS_COMMON_ERP_FAILED) {
1060 zfcp_erp_port_block(port, 0);
1061 result = ZFCP_ERP_EXIT;
1062 }
1063 return result;
1064}
1065
1066static int zfcp_erp_strategy_check_adapter(struct zfcp_adapter *adapter,
1067 int result)
1068{
1069 switch (result) {
1070 case ZFCP_ERP_SUCCEEDED :
1071 atomic_set(&adapter->erp_counter, 0);
1072 zfcp_erp_adapter_unblock(adapter);
1073 break;
1074
1075 case ZFCP_ERP_FAILED :
1076 atomic_inc(&adapter->erp_counter);
Christof Schmittff3b24f2008-10-01 12:42:15 +02001077 if (atomic_read(&adapter->erp_counter) > ZFCP_MAX_ERPS) {
1078 dev_err(&adapter->ccw_device->dev,
1079 "ERP cannot recover an error "
1080 "on the FCP device\n");
Swen Schilligedaed8592010-09-08 14:40:01 +02001081 zfcp_erp_set_adapter_status(adapter,
1082 ZFCP_STATUS_COMMON_ERP_FAILED);
Christof Schmittff3b24f2008-10-01 12:42:15 +02001083 }
Christof Schmitt287ac012008-07-02 10:56:40 +02001084 break;
1085 }
1086
1087 if (atomic_read(&adapter->status) & ZFCP_STATUS_COMMON_ERP_FAILED) {
1088 zfcp_erp_adapter_block(adapter, 0);
1089 result = ZFCP_ERP_EXIT;
1090 }
1091 return result;
1092}
1093
1094static int zfcp_erp_strategy_check_target(struct zfcp_erp_action *erp_action,
1095 int result)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096{
1097 struct zfcp_adapter *adapter = erp_action->adapter;
1098 struct zfcp_port *port = erp_action->port;
Christof Schmittb62a8d92010-09-08 14:39:55 +02001099 struct scsi_device *sdev = erp_action->sdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 switch (erp_action->action) {
1102
Christof Schmittb62a8d92010-09-08 14:39:55 +02001103 case ZFCP_ERP_ACTION_REOPEN_LUN:
1104 result = zfcp_erp_strategy_check_lun(sdev, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 break;
1106
1107 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
1108 case ZFCP_ERP_ACTION_REOPEN_PORT:
1109 result = zfcp_erp_strategy_check_port(port, result);
1110 break;
1111
1112 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
1113 result = zfcp_erp_strategy_check_adapter(adapter, result);
1114 break;
1115 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 return result;
1117}
1118
Christof Schmitt287ac012008-07-02 10:56:40 +02001119static int zfcp_erp_strat_change_det(atomic_t *target_status, u32 erp_status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120{
Christof Schmitt287ac012008-07-02 10:56:40 +02001121 int status = atomic_read(target_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122
Christof Schmitt287ac012008-07-02 10:56:40 +02001123 if ((status & ZFCP_STATUS_COMMON_RUNNING) &&
1124 (erp_status & ZFCP_STATUS_ERP_CLOSE_ONLY))
1125 return 1; /* take it online */
1126
1127 if (!(status & ZFCP_STATUS_COMMON_RUNNING) &&
1128 !(erp_status & ZFCP_STATUS_ERP_CLOSE_ONLY))
1129 return 1; /* take it offline */
1130
1131 return 0;
1132}
1133
1134static int zfcp_erp_strategy_statechange(struct zfcp_erp_action *act, int ret)
1135{
1136 int action = act->action;
1137 struct zfcp_adapter *adapter = act->adapter;
1138 struct zfcp_port *port = act->port;
Christof Schmittb62a8d92010-09-08 14:39:55 +02001139 struct scsi_device *sdev = act->sdev;
1140 struct zfcp_scsi_dev *zfcp_sdev;
Christof Schmitt287ac012008-07-02 10:56:40 +02001141 u32 erp_status = act->status;
1142
1143 switch (action) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
Christof Schmitt287ac012008-07-02 10:56:40 +02001145 if (zfcp_erp_strat_change_det(&adapter->status, erp_status)) {
1146 _zfcp_erp_adapter_reopen(adapter,
1147 ZFCP_STATUS_COMMON_ERP_FAILED,
Swen Schilligea4a3a62010-12-02 15:16:16 +01001148 "ersscg1");
Christof Schmitt287ac012008-07-02 10:56:40 +02001149 return ZFCP_ERP_EXIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 }
1151 break;
1152
1153 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
1154 case ZFCP_ERP_ACTION_REOPEN_PORT:
Christof Schmitt287ac012008-07-02 10:56:40 +02001155 if (zfcp_erp_strat_change_det(&port->status, erp_status)) {
1156 _zfcp_erp_port_reopen(port,
1157 ZFCP_STATUS_COMMON_ERP_FAILED,
Swen Schilligea4a3a62010-12-02 15:16:16 +01001158 "ersscg2");
Christof Schmitt287ac012008-07-02 10:56:40 +02001159 return ZFCP_ERP_EXIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 }
1161 break;
1162
Christof Schmittb62a8d92010-09-08 14:39:55 +02001163 case ZFCP_ERP_ACTION_REOPEN_LUN:
1164 zfcp_sdev = sdev_to_zfcp(sdev);
1165 if (zfcp_erp_strat_change_det(&zfcp_sdev->status, erp_status)) {
1166 _zfcp_erp_lun_reopen(sdev,
1167 ZFCP_STATUS_COMMON_ERP_FAILED,
Swen Schilligea4a3a62010-12-02 15:16:16 +01001168 "ersscg3", 0);
Christof Schmitt287ac012008-07-02 10:56:40 +02001169 return ZFCP_ERP_EXIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 }
1171 break;
1172 }
Christof Schmitt287ac012008-07-02 10:56:40 +02001173 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174}
1175
Christof Schmitt287ac012008-07-02 10:56:40 +02001176static void zfcp_erp_action_dequeue(struct zfcp_erp_action *erp_action)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177{
Christof Schmitt287ac012008-07-02 10:56:40 +02001178 struct zfcp_adapter *adapter = erp_action->adapter;
Christof Schmittb62a8d92010-09-08 14:39:55 +02001179 struct zfcp_scsi_dev *zfcp_sdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180
Christof Schmitt287ac012008-07-02 10:56:40 +02001181 adapter->erp_total_count--;
1182 if (erp_action->status & ZFCP_STATUS_ERP_LOWMEM) {
1183 adapter->erp_low_mem_count--;
1184 erp_action->status &= ~ZFCP_STATUS_ERP_LOWMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 }
1186
Christof Schmitt287ac012008-07-02 10:56:40 +02001187 list_del(&erp_action->list);
Swen Schilligae0904f2010-12-02 15:16:12 +01001188 zfcp_dbf_rec_run("eractd1", erp_action);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189
Christof Schmitt287ac012008-07-02 10:56:40 +02001190 switch (erp_action->action) {
Christof Schmittb62a8d92010-09-08 14:39:55 +02001191 case ZFCP_ERP_ACTION_REOPEN_LUN:
1192 zfcp_sdev = sdev_to_zfcp(erp_action->sdev);
Christof Schmitt287ac012008-07-02 10:56:40 +02001193 atomic_clear_mask(ZFCP_STATUS_COMMON_ERP_INUSE,
Christof Schmittb62a8d92010-09-08 14:39:55 +02001194 &zfcp_sdev->status);
Christof Schmitt287ac012008-07-02 10:56:40 +02001195 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196
Christof Schmitt287ac012008-07-02 10:56:40 +02001197 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
1198 case ZFCP_ERP_ACTION_REOPEN_PORT:
1199 atomic_clear_mask(ZFCP_STATUS_COMMON_ERP_INUSE,
1200 &erp_action->port->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 break;
Christof Schmitt287ac012008-07-02 10:56:40 +02001202
1203 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
1204 atomic_clear_mask(ZFCP_STATUS_COMMON_ERP_INUSE,
1205 &erp_action->adapter->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 break;
1207 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208}
1209
Christof Schmitt287ac012008-07-02 10:56:40 +02001210static void zfcp_erp_action_cleanup(struct zfcp_erp_action *act, int result)
Maxim Shchetyninaef4a982005-09-13 21:51:16 +02001211{
Christof Schmitt287ac012008-07-02 10:56:40 +02001212 struct zfcp_adapter *adapter = act->adapter;
1213 struct zfcp_port *port = act->port;
Christof Schmittb62a8d92010-09-08 14:39:55 +02001214 struct scsi_device *sdev = act->sdev;
Maxim Shchetyninaef4a982005-09-13 21:51:16 +02001215
Christof Schmitt287ac012008-07-02 10:56:40 +02001216 switch (act->action) {
Christof Schmittb62a8d92010-09-08 14:39:55 +02001217 case ZFCP_ERP_ACTION_REOPEN_LUN:
Christof Schmittfdbd1c52010-09-08 14:39:54 +02001218 if (!(act->status & ZFCP_STATUS_ERP_NO_REF))
Christof Schmittb62a8d92010-09-08 14:39:55 +02001219 scsi_device_put(sdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 break;
Christof Schmitt287ac012008-07-02 10:56:40 +02001221
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 case ZFCP_ERP_ACTION_REOPEN_PORT:
Christof Schmitta2fa0ae2009-03-02 13:09:08 +01001223 if (result == ZFCP_ERP_SUCCEEDED)
1224 zfcp_scsi_schedule_rport_register(port);
Christof Schmitt57676202010-07-08 09:53:05 +02001225 /* fall through */
1226 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
Christof Schmitt615f59e2010-02-17 11:18:56 +01001227 put_device(&port->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 break;
Christof Schmitt287ac012008-07-02 10:56:40 +02001229
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
Christof Schmitta2fa0ae2009-03-02 13:09:08 +01001231 if (result == ZFCP_ERP_SUCCEEDED) {
Christof Schmittbd43a422008-12-25 13:38:50 +01001232 register_service_level(&adapter->service_level);
Swen Schillig9eae07e2009-11-24 16:54:06 +01001233 queue_work(adapter->work_queue, &adapter->scan_work);
Christof Schmitta2fa0ae2009-03-02 13:09:08 +01001234 } else
1235 unregister_service_level(&adapter->service_level);
Swen Schilligf3450c72009-11-24 16:53:59 +01001236 kref_put(&adapter->ref, zfcp_adapter_release);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 }
1239}
1240
Christof Schmitt287ac012008-07-02 10:56:40 +02001241static int zfcp_erp_strategy_do_action(struct zfcp_erp_action *erp_action)
1242{
1243 switch (erp_action->action) {
1244 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
1245 return zfcp_erp_adapter_strategy(erp_action);
1246 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
1247 return zfcp_erp_port_forced_strategy(erp_action);
1248 case ZFCP_ERP_ACTION_REOPEN_PORT:
1249 return zfcp_erp_port_strategy(erp_action);
Christof Schmittb62a8d92010-09-08 14:39:55 +02001250 case ZFCP_ERP_ACTION_REOPEN_LUN:
1251 return zfcp_erp_lun_strategy(erp_action);
Christof Schmitt287ac012008-07-02 10:56:40 +02001252 }
1253 return ZFCP_ERP_FAILED;
1254}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255
Christof Schmitt287ac012008-07-02 10:56:40 +02001256static int zfcp_erp_strategy(struct zfcp_erp_action *erp_action)
1257{
1258 int retval;
Christof Schmitt287ac012008-07-02 10:56:40 +02001259 unsigned long flags;
Swen Schilligecf0c772009-11-24 16:53:58 +01001260 struct zfcp_adapter *adapter = erp_action->adapter;
Christof Schmitt287ac012008-07-02 10:56:40 +02001261
Swen Schilligf3450c72009-11-24 16:53:59 +01001262 kref_get(&adapter->ref);
Christof Schmitt287ac012008-07-02 10:56:40 +02001263
Swen Schilligf3450c72009-11-24 16:53:59 +01001264 write_lock_irqsave(&adapter->erp_lock, flags);
Christof Schmitt287ac012008-07-02 10:56:40 +02001265 zfcp_erp_strategy_check_fsfreq(erp_action);
1266
1267 if (erp_action->status & ZFCP_STATUS_ERP_DISMISSED) {
1268 zfcp_erp_action_dequeue(erp_action);
1269 retval = ZFCP_ERP_DISMISSED;
1270 goto unlock;
1271 }
1272
Christof Schmitt9c785d92010-07-08 09:53:09 +02001273 if (erp_action->status & ZFCP_STATUS_ERP_TIMEDOUT) {
1274 retval = ZFCP_ERP_FAILED;
1275 goto check_target;
1276 }
1277
Christof Schmitt287ac012008-07-02 10:56:40 +02001278 zfcp_erp_action_to_running(erp_action);
1279
1280 /* no lock to allow for blocking operations */
Swen Schilligecf0c772009-11-24 16:53:58 +01001281 write_unlock_irqrestore(&adapter->erp_lock, flags);
Christof Schmitt287ac012008-07-02 10:56:40 +02001282 retval = zfcp_erp_strategy_do_action(erp_action);
Swen Schilligecf0c772009-11-24 16:53:58 +01001283 write_lock_irqsave(&adapter->erp_lock, flags);
Christof Schmitt287ac012008-07-02 10:56:40 +02001284
1285 if (erp_action->status & ZFCP_STATUS_ERP_DISMISSED)
1286 retval = ZFCP_ERP_CONTINUES;
1287
1288 switch (retval) {
1289 case ZFCP_ERP_NOMEM:
1290 if (!(erp_action->status & ZFCP_STATUS_ERP_LOWMEM)) {
1291 ++adapter->erp_low_mem_count;
1292 erp_action->status |= ZFCP_STATUS_ERP_LOWMEM;
1293 }
1294 if (adapter->erp_total_count == adapter->erp_low_mem_count)
Swen Schilligea4a3a62010-12-02 15:16:16 +01001295 _zfcp_erp_adapter_reopen(adapter, 0, "erstgy1");
Christof Schmitt287ac012008-07-02 10:56:40 +02001296 else {
1297 zfcp_erp_strategy_memwait(erp_action);
1298 retval = ZFCP_ERP_CONTINUES;
1299 }
1300 goto unlock;
1301
1302 case ZFCP_ERP_CONTINUES:
1303 if (erp_action->status & ZFCP_STATUS_ERP_LOWMEM) {
1304 --adapter->erp_low_mem_count;
1305 erp_action->status &= ~ZFCP_STATUS_ERP_LOWMEM;
1306 }
1307 goto unlock;
1308 }
1309
Christof Schmitt9c785d92010-07-08 09:53:09 +02001310check_target:
Christof Schmitt287ac012008-07-02 10:56:40 +02001311 retval = zfcp_erp_strategy_check_target(erp_action, retval);
1312 zfcp_erp_action_dequeue(erp_action);
1313 retval = zfcp_erp_strategy_statechange(erp_action, retval);
1314 if (retval == ZFCP_ERP_EXIT)
1315 goto unlock;
Christof Schmitt85600f72009-07-13 15:06:09 +02001316 if (retval == ZFCP_ERP_SUCCEEDED)
1317 zfcp_erp_strategy_followup_success(erp_action);
1318 if (retval == ZFCP_ERP_FAILED)
1319 zfcp_erp_strategy_followup_failed(erp_action);
Christof Schmitt287ac012008-07-02 10:56:40 +02001320
1321 unlock:
Swen Schilligecf0c772009-11-24 16:53:58 +01001322 write_unlock_irqrestore(&adapter->erp_lock, flags);
Christof Schmitt287ac012008-07-02 10:56:40 +02001323
1324 if (retval != ZFCP_ERP_CONTINUES)
1325 zfcp_erp_action_cleanup(erp_action, retval);
1326
Swen Schilligf3450c72009-11-24 16:53:59 +01001327 kref_put(&adapter->ref, zfcp_adapter_release);
Christof Schmitt287ac012008-07-02 10:56:40 +02001328 return retval;
1329}
1330
1331static int zfcp_erp_thread(void *data)
1332{
1333 struct zfcp_adapter *adapter = (struct zfcp_adapter *) data;
1334 struct list_head *next;
1335 struct zfcp_erp_action *act;
1336 unsigned long flags;
1337
Christof Schmitt347c6a92009-08-18 15:43:25 +02001338 for (;;) {
Christof Schmitt347c6a92009-08-18 15:43:25 +02001339 wait_event_interruptible(adapter->erp_ready_wq,
1340 !list_empty(&adapter->erp_ready_head) ||
1341 kthread_should_stop());
Swen Schillig94ab4b32009-04-17 15:08:06 +02001342
Christof Schmitt347c6a92009-08-18 15:43:25 +02001343 if (kthread_should_stop())
1344 break;
1345
Christof Schmitt287ac012008-07-02 10:56:40 +02001346 write_lock_irqsave(&adapter->erp_lock, flags);
1347 next = adapter->erp_ready_head.next;
1348 write_unlock_irqrestore(&adapter->erp_lock, flags);
1349
1350 if (next != &adapter->erp_ready_head) {
1351 act = list_entry(next, struct zfcp_erp_action, list);
1352
1353 /* there is more to come after dismission, no notify */
1354 if (zfcp_erp_strategy(act) != ZFCP_ERP_DISMISSED)
1355 zfcp_erp_wakeup(adapter);
1356 }
Christof Schmitt287ac012008-07-02 10:56:40 +02001357 }
1358
Christof Schmitt287ac012008-07-02 10:56:40 +02001359 return 0;
1360}
1361
1362/**
1363 * zfcp_erp_thread_setup - Start ERP thread for adapter
1364 * @adapter: Adapter to start the ERP thread for
1365 *
1366 * Returns 0 on success or error code from kernel_thread()
1367 */
1368int zfcp_erp_thread_setup(struct zfcp_adapter *adapter)
1369{
Christof Schmitt347c6a92009-08-18 15:43:25 +02001370 struct task_struct *thread;
Christof Schmitt287ac012008-07-02 10:56:40 +02001371
Christof Schmitt347c6a92009-08-18 15:43:25 +02001372 thread = kthread_run(zfcp_erp_thread, adapter, "zfcperp%s",
1373 dev_name(&adapter->ccw_device->dev));
1374 if (IS_ERR(thread)) {
Christof Schmitt287ac012008-07-02 10:56:40 +02001375 dev_err(&adapter->ccw_device->dev,
Christof Schmittff3b24f2008-10-01 12:42:15 +02001376 "Creating an ERP thread for the FCP device failed.\n");
Christof Schmitt347c6a92009-08-18 15:43:25 +02001377 return PTR_ERR(thread);
Christof Schmitt287ac012008-07-02 10:56:40 +02001378 }
Christof Schmitt347c6a92009-08-18 15:43:25 +02001379
1380 adapter->erp_thread = thread;
Christof Schmitt287ac012008-07-02 10:56:40 +02001381 return 0;
1382}
1383
1384/**
1385 * zfcp_erp_thread_kill - Stop ERP thread.
1386 * @adapter: Adapter where the ERP thread should be stopped.
1387 *
1388 * The caller of this routine ensures that the specified adapter has
1389 * been shut down and that this operation has been completed. Thus,
1390 * there are no pending erp_actions which would need to be handled
1391 * here.
1392 */
1393void zfcp_erp_thread_kill(struct zfcp_adapter *adapter)
1394{
Christof Schmitt347c6a92009-08-18 15:43:25 +02001395 kthread_stop(adapter->erp_thread);
1396 adapter->erp_thread = NULL;
Christof Schmitt143bb6b2009-08-18 15:43:27 +02001397 WARN_ON(!list_empty(&adapter->erp_ready_head));
1398 WARN_ON(!list_empty(&adapter->erp_running_head));
Christof Schmitt287ac012008-07-02 10:56:40 +02001399}
1400
1401/**
Christof Schmitt287ac012008-07-02 10:56:40 +02001402 * zfcp_erp_wait - wait for completion of error recovery on an adapter
1403 * @adapter: adapter for which to wait for completion of its error recovery
1404 */
1405void zfcp_erp_wait(struct zfcp_adapter *adapter)
1406{
1407 wait_event(adapter->erp_done_wqh,
1408 !(atomic_read(&adapter->status) &
1409 ZFCP_STATUS_ADAPTER_ERP_PENDING));
1410}
1411
1412/**
Swen Schilligedaed8592010-09-08 14:40:01 +02001413 * zfcp_erp_set_adapter_status - set adapter status bits
Christof Schmitt287ac012008-07-02 10:56:40 +02001414 * @adapter: adapter to change the status
Christof Schmitt287ac012008-07-02 10:56:40 +02001415 * @mask: status bits to change
Christof Schmitt287ac012008-07-02 10:56:40 +02001416 *
Christof Schmittb62a8d92010-09-08 14:39:55 +02001417 * Changes in common status bits are propagated to attached ports and LUNs.
Christof Schmitt287ac012008-07-02 10:56:40 +02001418 */
Swen Schilligedaed8592010-09-08 14:40:01 +02001419void zfcp_erp_set_adapter_status(struct zfcp_adapter *adapter, u32 mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 struct zfcp_port *port;
Swen Schilligedaed8592010-09-08 14:40:01 +02001422 struct scsi_device *sdev;
Swen Schilligecf0c772009-11-24 16:53:58 +01001423 unsigned long flags;
Christof Schmitt287ac012008-07-02 10:56:40 +02001424 u32 common_mask = mask & ZFCP_COMMON_FLAGS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425
Swen Schilligedaed8592010-09-08 14:40:01 +02001426 atomic_set_mask(mask, &adapter->status);
Christof Schmitt287ac012008-07-02 10:56:40 +02001427
Swen Schilligedaed8592010-09-08 14:40:01 +02001428 if (!common_mask)
1429 return;
1430
1431 read_lock_irqsave(&adapter->port_list_lock, flags);
1432 list_for_each_entry(port, &adapter->port_list, list)
1433 atomic_set_mask(common_mask, &port->status);
1434 read_unlock_irqrestore(&adapter->port_list_lock, flags);
1435
1436 shost_for_each_device(sdev, adapter->scsi_host)
1437 atomic_set_mask(common_mask, &sdev_to_zfcp(sdev)->status);
1438}
1439
1440/**
1441 * zfcp_erp_clear_adapter_status - clear adapter status bits
1442 * @adapter: adapter to change the status
1443 * @mask: status bits to change
1444 *
1445 * Changes in common status bits are propagated to attached ports and LUNs.
1446 */
1447void zfcp_erp_clear_adapter_status(struct zfcp_adapter *adapter, u32 mask)
1448{
1449 struct zfcp_port *port;
1450 struct scsi_device *sdev;
1451 unsigned long flags;
1452 u32 common_mask = mask & ZFCP_COMMON_FLAGS;
1453 u32 clear_counter = mask & ZFCP_STATUS_COMMON_ERP_FAILED;
1454
1455 atomic_clear_mask(mask, &adapter->status);
1456
1457 if (!common_mask)
1458 return;
1459
1460 if (clear_counter)
1461 atomic_set(&adapter->erp_counter, 0);
1462
1463 read_lock_irqsave(&adapter->port_list_lock, flags);
1464 list_for_each_entry(port, &adapter->port_list, list) {
1465 atomic_clear_mask(common_mask, &port->status);
1466 if (clear_counter)
1467 atomic_set(&port->erp_counter, 0);
1468 }
1469 read_unlock_irqrestore(&adapter->port_list_lock, flags);
1470
1471 shost_for_each_device(sdev, adapter->scsi_host) {
1472 atomic_clear_mask(common_mask, &sdev_to_zfcp(sdev)->status);
1473 if (clear_counter)
1474 atomic_set(&sdev_to_zfcp(sdev)->erp_counter, 0);
Swen Schilligecf0c772009-11-24 16:53:58 +01001475 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476}
1477
Christof Schmitt287ac012008-07-02 10:56:40 +02001478/**
Swen Schilligedaed8592010-09-08 14:40:01 +02001479 * zfcp_erp_set_port_status - set port status bits
1480 * @port: port to change the status
Christof Schmitt287ac012008-07-02 10:56:40 +02001481 * @mask: status bits to change
Christof Schmitt287ac012008-07-02 10:56:40 +02001482 *
Christof Schmittb62a8d92010-09-08 14:39:55 +02001483 * Changes in common status bits are propagated to attached LUNs.
Christof Schmitt287ac012008-07-02 10:56:40 +02001484 */
Swen Schilligedaed8592010-09-08 14:40:01 +02001485void zfcp_erp_set_port_status(struct zfcp_port *port, u32 mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486{
Christof Schmittb62a8d92010-09-08 14:39:55 +02001487 struct scsi_device *sdev;
Christof Schmitt287ac012008-07-02 10:56:40 +02001488 u32 common_mask = mask & ZFCP_COMMON_FLAGS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489
Swen Schilligedaed8592010-09-08 14:40:01 +02001490 atomic_set_mask(mask, &port->status);
Christof Schmitt287ac012008-07-02 10:56:40 +02001491
Swen Schilligedaed8592010-09-08 14:40:01 +02001492 if (!common_mask)
1493 return;
1494
1495 shost_for_each_device(sdev, port->adapter->scsi_host)
1496 if (sdev_to_zfcp(sdev)->port == port)
1497 atomic_set_mask(common_mask,
1498 &sdev_to_zfcp(sdev)->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499}
1500
Christof Schmitt287ac012008-07-02 10:56:40 +02001501/**
Swen Schilligedaed8592010-09-08 14:40:01 +02001502 * zfcp_erp_clear_port_status - clear port status bits
1503 * @port: adapter to change the status
Christof Schmitt287ac012008-07-02 10:56:40 +02001504 * @mask: status bits to change
Swen Schilligedaed8592010-09-08 14:40:01 +02001505 *
1506 * Changes in common status bits are propagated to attached LUNs.
Christof Schmitt287ac012008-07-02 10:56:40 +02001507 */
Swen Schilligedaed8592010-09-08 14:40:01 +02001508void zfcp_erp_clear_port_status(struct zfcp_port *port, u32 mask)
1509{
1510 struct scsi_device *sdev;
1511 u32 common_mask = mask & ZFCP_COMMON_FLAGS;
1512 u32 clear_counter = mask & ZFCP_STATUS_COMMON_ERP_FAILED;
1513
1514 atomic_clear_mask(mask, &port->status);
1515
1516 if (!common_mask)
1517 return;
1518
1519 if (clear_counter)
1520 atomic_set(&port->erp_counter, 0);
1521
1522 shost_for_each_device(sdev, port->adapter->scsi_host)
1523 if (sdev_to_zfcp(sdev)->port == port) {
1524 atomic_clear_mask(common_mask,
1525 &sdev_to_zfcp(sdev)->status);
1526 if (clear_counter)
1527 atomic_set(&sdev_to_zfcp(sdev)->erp_counter, 0);
1528 }
1529}
1530
1531/**
1532 * zfcp_erp_set_lun_status - set lun status bits
1533 * @sdev: SCSI device / lun to set the status bits
1534 * @mask: status bits to change
1535 */
1536void zfcp_erp_set_lun_status(struct scsi_device *sdev, u32 mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537{
Christof Schmittb62a8d92010-09-08 14:39:55 +02001538 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
1539
Swen Schilligedaed8592010-09-08 14:40:01 +02001540 atomic_set_mask(mask, &zfcp_sdev->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541}
1542
Christof Schmitt287ac012008-07-02 10:56:40 +02001543/**
Swen Schilligedaed8592010-09-08 14:40:01 +02001544 * zfcp_erp_clear_lun_status - clear lun status bits
1545 * @sdev: SCSi device / lun to clear the status bits
1546 * @mask: status bits to change
Christof Schmitt287ac012008-07-02 10:56:40 +02001547 */
Swen Schilligedaed8592010-09-08 14:40:01 +02001548void zfcp_erp_clear_lun_status(struct scsi_device *sdev, u32 mask)
Andreas Herrmannd736a272005-06-13 13:23:57 +02001549{
Swen Schilligedaed8592010-09-08 14:40:01 +02001550 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
1551
1552 atomic_clear_mask(mask, &zfcp_sdev->status);
1553
1554 if (mask & ZFCP_STATUS_COMMON_ERP_FAILED)
1555 atomic_set(&zfcp_sdev->erp_counter, 0);
Andreas Herrmannd736a272005-06-13 13:23:57 +02001556}
1557