blob: 37408f5f81ce84e5f4f598069bbc417edff71469 [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 *
Steffen Maier6f2ce1c2016-12-09 17:16:33 +01006 * Copyright IBM Corp. 2002, 2016
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 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);
Martin Peschke924dd582013-08-22 17:45:37 +0200105 else {
106 spin_lock(port->adapter->scsi_host->host_lock);
107 __shost_for_each_device(sdev, port->adapter->scsi_host)
Christof Schmittb62a8d92010-09-08 14:39:55 +0200108 if (sdev_to_zfcp(sdev)->port == port)
109 zfcp_erp_action_dismiss_lun(sdev);
Martin Peschke924dd582013-08-22 17:45:37 +0200110 spin_unlock(port->adapter->scsi_host->host_lock);
111 }
Christof Schmitt287ac012008-07-02 10:56:40 +0200112}
113
114static void zfcp_erp_action_dismiss_adapter(struct zfcp_adapter *adapter)
115{
116 struct zfcp_port *port;
117
118 if (atomic_read(&adapter->status) & ZFCP_STATUS_COMMON_ERP_INUSE)
119 zfcp_erp_action_dismiss(&adapter->erp_action);
Swen Schilligecf0c772009-11-24 16:53:58 +0100120 else {
121 read_lock(&adapter->port_list_lock);
122 list_for_each_entry(port, &adapter->port_list, list)
Christof Schmitt287ac012008-07-02 10:56:40 +0200123 zfcp_erp_action_dismiss_port(port);
Swen Schilligecf0c772009-11-24 16:53:58 +0100124 read_unlock(&adapter->port_list_lock);
125 }
Christof Schmitt287ac012008-07-02 10:56:40 +0200126}
127
128static int zfcp_erp_required_act(int want, struct zfcp_adapter *adapter,
129 struct zfcp_port *port,
Christof Schmittb62a8d92010-09-08 14:39:55 +0200130 struct scsi_device *sdev)
Christof Schmitt287ac012008-07-02 10:56:40 +0200131{
132 int need = want;
Christof Schmittb62a8d92010-09-08 14:39:55 +0200133 int l_status, p_status, a_status;
134 struct zfcp_scsi_dev *zfcp_sdev;
Christof Schmitt287ac012008-07-02 10:56:40 +0200135
136 switch (want) {
Christof Schmittb62a8d92010-09-08 14:39:55 +0200137 case ZFCP_ERP_ACTION_REOPEN_LUN:
138 zfcp_sdev = sdev_to_zfcp(sdev);
139 l_status = atomic_read(&zfcp_sdev->status);
140 if (l_status & ZFCP_STATUS_COMMON_ERP_INUSE)
Christof Schmitt287ac012008-07-02 10:56:40 +0200141 return 0;
142 p_status = atomic_read(&port->status);
143 if (!(p_status & ZFCP_STATUS_COMMON_RUNNING) ||
144 p_status & ZFCP_STATUS_COMMON_ERP_FAILED)
145 return 0;
146 if (!(p_status & ZFCP_STATUS_COMMON_UNBLOCKED))
147 need = ZFCP_ERP_ACTION_REOPEN_PORT;
148 /* fall through */
Christof Schmitt287ac012008-07-02 10:56:40 +0200149 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
150 p_status = atomic_read(&port->status);
Christof Schmitt097ef3b2010-07-08 09:53:06 +0200151 if (!(p_status & ZFCP_STATUS_COMMON_OPEN))
152 need = ZFCP_ERP_ACTION_REOPEN_PORT;
153 /* fall through */
154 case ZFCP_ERP_ACTION_REOPEN_PORT:
155 p_status = atomic_read(&port->status);
Christof Schmitt287ac012008-07-02 10:56:40 +0200156 if (p_status & ZFCP_STATUS_COMMON_ERP_INUSE)
157 return 0;
158 a_status = atomic_read(&adapter->status);
159 if (!(a_status & ZFCP_STATUS_COMMON_RUNNING) ||
160 a_status & ZFCP_STATUS_COMMON_ERP_FAILED)
161 return 0;
Swen Schilligd3e10882010-11-17 14:23:42 +0100162 if (p_status & ZFCP_STATUS_COMMON_NOESC)
163 return need;
Christof Schmitt287ac012008-07-02 10:56:40 +0200164 if (!(a_status & ZFCP_STATUS_COMMON_UNBLOCKED))
165 need = ZFCP_ERP_ACTION_REOPEN_ADAPTER;
166 /* fall through */
167 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
168 a_status = atomic_read(&adapter->status);
169 if (a_status & ZFCP_STATUS_COMMON_ERP_INUSE)
170 return 0;
Christof Schmitt143bb6b2009-08-18 15:43:27 +0200171 if (!(a_status & ZFCP_STATUS_COMMON_RUNNING) &&
172 !(a_status & ZFCP_STATUS_COMMON_OPEN))
173 return 0; /* shutdown requested for closed adapter */
Christof Schmitt287ac012008-07-02 10:56:40 +0200174 }
175
176 return need;
177}
178
Christof Schmittfdbd1c52010-09-08 14:39:54 +0200179static struct zfcp_erp_action *zfcp_erp_setup_act(int need, u32 act_status,
Christof Schmitt287ac012008-07-02 10:56:40 +0200180 struct zfcp_adapter *adapter,
181 struct zfcp_port *port,
Christof Schmittb62a8d92010-09-08 14:39:55 +0200182 struct scsi_device *sdev)
Christof Schmitt287ac012008-07-02 10:56:40 +0200183{
184 struct zfcp_erp_action *erp_action;
Christof Schmittb62a8d92010-09-08 14:39:55 +0200185 struct zfcp_scsi_dev *zfcp_sdev;
Christof Schmitt287ac012008-07-02 10:56:40 +0200186
187 switch (need) {
Christof Schmittb62a8d92010-09-08 14:39:55 +0200188 case ZFCP_ERP_ACTION_REOPEN_LUN:
189 zfcp_sdev = sdev_to_zfcp(sdev);
Christof Schmittfdbd1c52010-09-08 14:39:54 +0200190 if (!(act_status & ZFCP_STATUS_ERP_NO_REF))
Christof Schmittb62a8d92010-09-08 14:39:55 +0200191 if (scsi_device_get(sdev))
Christof Schmittfdbd1c52010-09-08 14:39:54 +0200192 return NULL;
Peter Zijlstra805de8f42015-04-24 01:12:32 +0200193 atomic_or(ZFCP_STATUS_COMMON_ERP_INUSE,
Christof Schmittb62a8d92010-09-08 14:39:55 +0200194 &zfcp_sdev->status);
195 erp_action = &zfcp_sdev->erp_action;
Swen Schillig14718e32010-11-17 14:23:43 +0100196 memset(erp_action, 0, sizeof(struct zfcp_erp_action));
197 erp_action->port = port;
198 erp_action->sdev = sdev;
Christof Schmittb62a8d92010-09-08 14:39:55 +0200199 if (!(atomic_read(&zfcp_sdev->status) &
200 ZFCP_STATUS_COMMON_RUNNING))
Christof Schmittfdbd1c52010-09-08 14:39:54 +0200201 act_status |= ZFCP_STATUS_ERP_CLOSE_ONLY;
Christof Schmitt287ac012008-07-02 10:56:40 +0200202 break;
203
204 case ZFCP_ERP_ACTION_REOPEN_PORT:
205 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
Christof Schmitt615f59e2010-02-17 11:18:56 +0100206 if (!get_device(&port->dev))
Swen Schillig6b1833342009-11-24 16:54:05 +0100207 return NULL;
Christof Schmitt287ac012008-07-02 10:56:40 +0200208 zfcp_erp_action_dismiss_port(port);
Peter Zijlstra805de8f42015-04-24 01:12:32 +0200209 atomic_or(ZFCP_STATUS_COMMON_ERP_INUSE, &port->status);
Christof Schmitt287ac012008-07-02 10:56:40 +0200210 erp_action = &port->erp_action;
Swen Schillig14718e32010-11-17 14:23:43 +0100211 memset(erp_action, 0, sizeof(struct zfcp_erp_action));
212 erp_action->port = port;
Christof Schmitt287ac012008-07-02 10:56:40 +0200213 if (!(atomic_read(&port->status) & 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 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
Swen Schilligf3450c72009-11-24 16:53:59 +0100218 kref_get(&adapter->ref);
Christof Schmitt287ac012008-07-02 10:56:40 +0200219 zfcp_erp_action_dismiss_adapter(adapter);
Peter Zijlstra805de8f42015-04-24 01:12:32 +0200220 atomic_or(ZFCP_STATUS_COMMON_ERP_INUSE, &adapter->status);
Christof Schmitt287ac012008-07-02 10:56:40 +0200221 erp_action = &adapter->erp_action;
Swen Schillig14718e32010-11-17 14:23:43 +0100222 memset(erp_action, 0, sizeof(struct zfcp_erp_action));
Christof Schmitt287ac012008-07-02 10:56:40 +0200223 if (!(atomic_read(&adapter->status) &
224 ZFCP_STATUS_COMMON_RUNNING))
Christof Schmittfdbd1c52010-09-08 14:39:54 +0200225 act_status |= ZFCP_STATUS_ERP_CLOSE_ONLY;
Christof Schmitt287ac012008-07-02 10:56:40 +0200226 break;
227
228 default:
229 return NULL;
230 }
231
Christof Schmitt287ac012008-07-02 10:56:40 +0200232 erp_action->adapter = adapter;
Christof Schmitt287ac012008-07-02 10:56:40 +0200233 erp_action->action = need;
Christof Schmittfdbd1c52010-09-08 14:39:54 +0200234 erp_action->status = act_status;
Christof Schmitt287ac012008-07-02 10:56:40 +0200235
236 return erp_action;
237}
238
239static int zfcp_erp_action_enqueue(int want, struct zfcp_adapter *adapter,
240 struct zfcp_port *port,
Christof Schmittb62a8d92010-09-08 14:39:55 +0200241 struct scsi_device *sdev,
Swen Schilligea4a3a62010-12-02 15:16:16 +0100242 char *id, u32 act_status)
Christof Schmitt287ac012008-07-02 10:56:40 +0200243{
244 int retval = 1, need;
Swen Schilligae0904f2010-12-02 15:16:12 +0100245 struct zfcp_erp_action *act;
Christof Schmitt287ac012008-07-02 10:56:40 +0200246
Christof Schmitt347c6a92009-08-18 15:43:25 +0200247 if (!adapter->erp_thread)
Christof Schmitt287ac012008-07-02 10:56:40 +0200248 return -EIO;
249
Christof Schmittb62a8d92010-09-08 14:39:55 +0200250 need = zfcp_erp_required_act(want, adapter, port, sdev);
Christof Schmitt287ac012008-07-02 10:56:40 +0200251 if (!need)
252 goto out;
253
Christof Schmittb62a8d92010-09-08 14:39:55 +0200254 act = zfcp_erp_setup_act(need, act_status, adapter, port, sdev);
Christof Schmitt287ac012008-07-02 10:56:40 +0200255 if (!act)
256 goto out;
Peter Zijlstra805de8f42015-04-24 01:12:32 +0200257 atomic_or(ZFCP_STATUS_ADAPTER_ERP_PENDING, &adapter->status);
Christof Schmitt287ac012008-07-02 10:56:40 +0200258 ++adapter->erp_total_count;
259 list_add_tail(&act->list, &adapter->erp_ready_head);
Christof Schmitt347c6a92009-08-18 15:43:25 +0200260 wake_up(&adapter->erp_ready_wq);
Christof Schmitt287ac012008-07-02 10:56:40 +0200261 retval = 0;
262 out:
Swen Schilligae0904f2010-12-02 15:16:12 +0100263 zfcp_dbf_rec_trig(id, adapter, port, sdev, want, need);
Christof Schmitt287ac012008-07-02 10:56:40 +0200264 return retval;
265}
266
267static int _zfcp_erp_adapter_reopen(struct zfcp_adapter *adapter,
Swen Schilligea4a3a62010-12-02 15:16:16 +0100268 int clear_mask, char *id)
Christof Schmitt287ac012008-07-02 10:56:40 +0200269{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 zfcp_erp_adapter_block(adapter, clear_mask);
Christof Schmitta2fa0ae2009-03-02 13:09:08 +0100271 zfcp_scsi_schedule_rports_block(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
Christof Schmitt287ac012008-07-02 10:56:40 +0200273 /* ensure propagation of failed status to new devices */
274 if (atomic_read(&adapter->status) & ZFCP_STATUS_COMMON_ERP_FAILED) {
Swen Schilligedaed852010-09-08 14:40:01 +0200275 zfcp_erp_set_adapter_status(adapter,
276 ZFCP_STATUS_COMMON_ERP_FAILED);
Christof Schmitt287ac012008-07-02 10:56:40 +0200277 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 }
Christof Schmitt287ac012008-07-02 10:56:40 +0200279 return zfcp_erp_action_enqueue(ZFCP_ERP_ACTION_REOPEN_ADAPTER,
Swen Schilligea4a3a62010-12-02 15:16:16 +0100280 adapter, NULL, NULL, id, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281}
282
283/**
Christof Schmitt287ac012008-07-02 10:56:40 +0200284 * zfcp_erp_adapter_reopen - Reopen adapter.
285 * @adapter: Adapter to reopen.
286 * @clear: Status flags to clear.
287 * @id: Id for debug trace event.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 */
Swen Schilligea4a3a62010-12-02 15:16:16 +0100289void zfcp_erp_adapter_reopen(struct zfcp_adapter *adapter, int clear, char *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290{
Christof Schmitt287ac012008-07-02 10:56:40 +0200291 unsigned long flags;
292
Swen Schilligecf0c772009-11-24 16:53:58 +0100293 zfcp_erp_adapter_block(adapter, clear);
294 zfcp_scsi_schedule_rports_block(adapter);
295
296 write_lock_irqsave(&adapter->erp_lock, flags);
297 if (atomic_read(&adapter->status) & ZFCP_STATUS_COMMON_ERP_FAILED)
Swen Schilligedaed852010-09-08 14:40:01 +0200298 zfcp_erp_set_adapter_status(adapter,
299 ZFCP_STATUS_COMMON_ERP_FAILED);
Swen Schilligecf0c772009-11-24 16:53:58 +0100300 else
301 zfcp_erp_action_enqueue(ZFCP_ERP_ACTION_REOPEN_ADAPTER, adapter,
Swen Schilligea4a3a62010-12-02 15:16:16 +0100302 NULL, NULL, id, 0);
Swen Schilligecf0c772009-11-24 16:53:58 +0100303 write_unlock_irqrestore(&adapter->erp_lock, flags);
Christof Schmitt287ac012008-07-02 10:56:40 +0200304}
305
306/**
307 * zfcp_erp_adapter_shutdown - Shutdown adapter.
308 * @adapter: Adapter to shut down.
309 * @clear: Status flags to clear.
310 * @id: Id for debug trace event.
Christof Schmitt287ac012008-07-02 10:56:40 +0200311 */
312void zfcp_erp_adapter_shutdown(struct zfcp_adapter *adapter, int clear,
Swen Schilligea4a3a62010-12-02 15:16:16 +0100313 char *id)
Christof Schmitt287ac012008-07-02 10:56:40 +0200314{
315 int flags = ZFCP_STATUS_COMMON_RUNNING | ZFCP_STATUS_COMMON_ERP_FAILED;
Swen Schilligea4a3a62010-12-02 15:16:16 +0100316 zfcp_erp_adapter_reopen(adapter, clear | flags, id);
Christof Schmitt287ac012008-07-02 10:56:40 +0200317}
318
319/**
320 * zfcp_erp_port_shutdown - Shutdown port
321 * @port: Port to shut down.
322 * @clear: Status flags to clear.
323 * @id: Id for debug trace event.
Christof Schmitt287ac012008-07-02 10:56:40 +0200324 */
Swen Schilligea4a3a62010-12-02 15:16:16 +0100325void zfcp_erp_port_shutdown(struct zfcp_port *port, int clear, char *id)
Christof Schmitt287ac012008-07-02 10:56:40 +0200326{
327 int flags = ZFCP_STATUS_COMMON_RUNNING | ZFCP_STATUS_COMMON_ERP_FAILED;
Swen Schilligea4a3a62010-12-02 15:16:16 +0100328 zfcp_erp_port_reopen(port, clear | flags, id);
Christof Schmitt287ac012008-07-02 10:56:40 +0200329}
330
Christof Schmitt287ac012008-07-02 10:56:40 +0200331static void zfcp_erp_port_block(struct zfcp_port *port, int clear)
332{
Swen Schilligedaed852010-09-08 14:40:01 +0200333 zfcp_erp_clear_port_status(port,
334 ZFCP_STATUS_COMMON_UNBLOCKED | clear);
Christof Schmitt287ac012008-07-02 10:56:40 +0200335}
336
Swen Schilligea4a3a62010-12-02 15:16:16 +0100337static void _zfcp_erp_port_forced_reopen(struct zfcp_port *port, int clear,
338 char *id)
Christof Schmitt287ac012008-07-02 10:56:40 +0200339{
340 zfcp_erp_port_block(port, clear);
Christof Schmitta2fa0ae2009-03-02 13:09:08 +0100341 zfcp_scsi_schedule_rport_block(port);
Christof Schmitt287ac012008-07-02 10:56:40 +0200342
343 if (atomic_read(&port->status) & ZFCP_STATUS_COMMON_ERP_FAILED)
344 return;
345
346 zfcp_erp_action_enqueue(ZFCP_ERP_ACTION_REOPEN_PORT_FORCED,
Swen Schilligea4a3a62010-12-02 15:16:16 +0100347 port->adapter, port, NULL, id, 0);
Christof Schmitt287ac012008-07-02 10:56:40 +0200348}
349
350/**
351 * zfcp_erp_port_forced_reopen - Forced close of port and open again
352 * @port: Port to force close and to reopen.
Swen Schilligea4a3a62010-12-02 15:16:16 +0100353 * @clear: Status flags to clear.
Christof Schmitt287ac012008-07-02 10:56:40 +0200354 * @id: Id for debug trace event.
Christof Schmitt287ac012008-07-02 10:56:40 +0200355 */
Swen Schilligea4a3a62010-12-02 15:16:16 +0100356void zfcp_erp_port_forced_reopen(struct zfcp_port *port, int clear, char *id)
Christof Schmitt287ac012008-07-02 10:56:40 +0200357{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 unsigned long flags;
359 struct zfcp_adapter *adapter = port->adapter;
360
Swen Schilligecf0c772009-11-24 16:53:58 +0100361 write_lock_irqsave(&adapter->erp_lock, flags);
Swen Schilligea4a3a62010-12-02 15:16:16 +0100362 _zfcp_erp_port_forced_reopen(port, clear, id);
Swen Schilligecf0c772009-11-24 16:53:58 +0100363 write_unlock_irqrestore(&adapter->erp_lock, flags);
Christof Schmitt287ac012008-07-02 10:56:40 +0200364}
365
Swen Schilligea4a3a62010-12-02 15:16:16 +0100366static int _zfcp_erp_port_reopen(struct zfcp_port *port, int clear, char *id)
Christof Schmitt287ac012008-07-02 10:56:40 +0200367{
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,
Swen Schilligea4a3a62010-12-02 15:16:16 +0100378 port->adapter, port, NULL, id, 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
Swen Schilligea4a3a62010-12-02 15:16:16 +0100385 * @id: Id for debug trace event.
Christof Schmitt287ac012008-07-02 10:56:40 +0200386 *
387 * Returns 0 if recovery has been triggered, < 0 if not.
388 */
Swen Schilligea4a3a62010-12-02 15:16:16 +0100389int zfcp_erp_port_reopen(struct zfcp_port *port, int clear, char *id)
Christof Schmitt287ac012008-07-02 10:56:40 +0200390{
Christof Schmitt287ac012008-07-02 10:56:40 +0200391 int retval;
Swen Schilligecf0c772009-11-24 16:53:58 +0100392 unsigned long flags;
Christof Schmitt287ac012008-07-02 10:56:40 +0200393 struct zfcp_adapter *adapter = port->adapter;
394
Swen Schilligecf0c772009-11-24 16:53:58 +0100395 write_lock_irqsave(&adapter->erp_lock, flags);
Swen Schilligea4a3a62010-12-02 15:16:16 +0100396 retval = _zfcp_erp_port_reopen(port, clear, id);
Swen Schilligecf0c772009-11-24 16:53:58 +0100397 write_unlock_irqrestore(&adapter->erp_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
399 return retval;
400}
401
Christof Schmittb62a8d92010-09-08 14:39:55 +0200402static void zfcp_erp_lun_block(struct scsi_device *sdev, int clear_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403{
Swen Schilligedaed852010-09-08 14:40:01 +0200404 zfcp_erp_clear_lun_status(sdev,
405 ZFCP_STATUS_COMMON_UNBLOCKED | clear_mask);
Christof Schmitt287ac012008-07-02 10:56:40 +0200406}
407
Christof Schmittb62a8d92010-09-08 14:39:55 +0200408static void _zfcp_erp_lun_reopen(struct scsi_device *sdev, int clear, char *id,
Swen Schilligea4a3a62010-12-02 15:16:16 +0100409 u32 act_status)
Christof Schmitt287ac012008-07-02 10:56:40 +0200410{
Christof Schmittb62a8d92010-09-08 14:39:55 +0200411 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
412 struct zfcp_adapter *adapter = zfcp_sdev->port->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
Christof Schmittb62a8d92010-09-08 14:39:55 +0200414 zfcp_erp_lun_block(sdev, clear);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
Christof Schmittb62a8d92010-09-08 14:39:55 +0200416 if (atomic_read(&zfcp_sdev->status) & ZFCP_STATUS_COMMON_ERP_FAILED)
Christof Schmitt287ac012008-07-02 10:56:40 +0200417 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
Christof Schmittb62a8d92010-09-08 14:39:55 +0200419 zfcp_erp_action_enqueue(ZFCP_ERP_ACTION_REOPEN_LUN, adapter,
Swen Schilligea4a3a62010-12-02 15:16:16 +0100420 zfcp_sdev->port, sdev, id, act_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421}
422
423/**
Christof Schmittb62a8d92010-09-08 14:39:55 +0200424 * zfcp_erp_lun_reopen - initiate reopen of a LUN
425 * @sdev: SCSI device / LUN to be reopened
426 * @clear_mask: specifies flags in LUN status to be cleared
Swen Schilligea4a3a62010-12-02 15:16:16 +0100427 * @id: Id for debug trace event.
428 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 * Return: 0 on success, < 0 on error
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 */
Swen Schilligea4a3a62010-12-02 15:16:16 +0100431void zfcp_erp_lun_reopen(struct scsi_device *sdev, int clear, char *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 unsigned long flags;
Christof Schmittb62a8d92010-09-08 14:39:55 +0200434 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
435 struct zfcp_port *port = zfcp_sdev->port;
Christof Schmitt287ac012008-07-02 10:56:40 +0200436 struct zfcp_adapter *adapter = port->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
Swen Schilligecf0c772009-11-24 16:53:58 +0100438 write_lock_irqsave(&adapter->erp_lock, flags);
Swen Schilligea4a3a62010-12-02 15:16:16 +0100439 _zfcp_erp_lun_reopen(sdev, clear, id, 0);
Swen Schilligecf0c772009-11-24 16:53:58 +0100440 write_unlock_irqrestore(&adapter->erp_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441}
442
Christof Schmittfdbd1c52010-09-08 14:39:54 +0200443/**
Christof Schmittb62a8d92010-09-08 14:39:55 +0200444 * zfcp_erp_lun_shutdown - Shutdown LUN
445 * @sdev: SCSI device / LUN to shut down.
Christof Schmittfdbd1c52010-09-08 14:39:54 +0200446 * @clear: Status flags to clear.
447 * @id: Id for debug trace event.
Christof Schmittfdbd1c52010-09-08 14:39:54 +0200448 */
Swen Schilligea4a3a62010-12-02 15:16:16 +0100449void zfcp_erp_lun_shutdown(struct scsi_device *sdev, int clear, char *id)
Christof Schmittfdbd1c52010-09-08 14:39:54 +0200450{
451 int flags = ZFCP_STATUS_COMMON_RUNNING | ZFCP_STATUS_COMMON_ERP_FAILED;
Swen Schilligea4a3a62010-12-02 15:16:16 +0100452 zfcp_erp_lun_reopen(sdev, clear | flags, id);
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);
Swen Schilligea4a3a62010-12-02 15:16:16 +0100474 _zfcp_erp_lun_reopen(sdev, clear, id, 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 Schilligae0904f2010-12-02 15:16:12 +0100488 zfcp_dbf_rec_run("eraubl1", &adapter->erp_action);
Peter Zijlstra805de8f42015-04-24 01:12:32 +0200489 atomic_or(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 Schilligae0904f2010-12-02 15:16:12 +0100495 zfcp_dbf_rec_run("erpubl1", &port->erp_action);
Peter Zijlstra805de8f42015-04-24 01:12:32 +0200496 atomic_or(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))
Swen Schilligae0904f2010-12-02 15:16:12 +0100504 zfcp_dbf_rec_run("erlubl1", &sdev_to_zfcp(sdev)->erp_action);
Peter Zijlstra805de8f42015-04-24 01:12:32 +0200505 atomic_or(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 Schilligae0904f2010-12-02 15:16:12 +0100511 zfcp_dbf_rec_run("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 Schilligae0904f2010-12-02 15:16:12 +0100528 zfcp_dbf_rec_run("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 Schilligae0904f2010-12-02 15:16:12 +0100532 zfcp_dbf_rec_run("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{
Lukáš Korenčikbc464272017-07-28 12:30:48 +0200575 setup_timer(&erp_action->timer, zfcp_erp_memwait_handler,
576 (unsigned long) erp_action);
Christof Schmitt287ac012008-07-02 10:56:40 +0200577 erp_action->timer.expires = jiffies + HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 add_timer(&erp_action->timer);
Christof Schmitt287ac012008-07-02 10:56:40 +0200579}
580
581static void _zfcp_erp_port_reopen_all(struct zfcp_adapter *adapter,
Swen Schilligea4a3a62010-12-02 15:16:16 +0100582 int clear, char *id)
Christof Schmitt287ac012008-07-02 10:56:40 +0200583{
584 struct zfcp_port *port;
585
Swen Schilligecf0c772009-11-24 16:53:58 +0100586 read_lock(&adapter->port_list_lock);
587 list_for_each_entry(port, &adapter->port_list, list)
Swen Schilligea4a3a62010-12-02 15:16:16 +0100588 _zfcp_erp_port_reopen(port, clear, id);
Swen Schilligecf0c772009-11-24 16:53:58 +0100589 read_unlock(&adapter->port_list_lock);
Christof Schmitt287ac012008-07-02 10:56:40 +0200590}
591
Christof Schmittb62a8d92010-09-08 14:39:55 +0200592static void _zfcp_erp_lun_reopen_all(struct zfcp_port *port, int clear,
Swen Schilligea4a3a62010-12-02 15:16:16 +0100593 char *id)
Christof Schmitt287ac012008-07-02 10:56:40 +0200594{
Christof Schmittb62a8d92010-09-08 14:39:55 +0200595 struct scsi_device *sdev;
Christof Schmitt287ac012008-07-02 10:56:40 +0200596
Martin Peschke924dd582013-08-22 17:45:37 +0200597 spin_lock(port->adapter->scsi_host->host_lock);
598 __shost_for_each_device(sdev, port->adapter->scsi_host)
Christof Schmittb62a8d92010-09-08 14:39:55 +0200599 if (sdev_to_zfcp(sdev)->port == port)
Swen Schilligea4a3a62010-12-02 15:16:16 +0100600 _zfcp_erp_lun_reopen(sdev, clear, id, 0);
Martin Peschke924dd582013-08-22 17:45:37 +0200601 spin_unlock(port->adapter->scsi_host->host_lock);
Christof Schmitt287ac012008-07-02 10:56:40 +0200602}
603
Christof Schmitt85600f72009-07-13 15:06:09 +0200604static void zfcp_erp_strategy_followup_failed(struct zfcp_erp_action *act)
Christof Schmitt287ac012008-07-02 10:56:40 +0200605{
Christof Schmitt287ac012008-07-02 10:56:40 +0200606 switch (act->action) {
Christof Schmitt287ac012008-07-02 10:56:40 +0200607 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
Swen Schilligea4a3a62010-12-02 15:16:16 +0100608 _zfcp_erp_adapter_reopen(act->adapter, 0, "ersff_1");
Christof Schmitt287ac012008-07-02 10:56:40 +0200609 break;
Christof Schmitt287ac012008-07-02 10:56:40 +0200610 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
Swen Schilligea4a3a62010-12-02 15:16:16 +0100611 _zfcp_erp_port_forced_reopen(act->port, 0, "ersff_2");
Christof Schmitt287ac012008-07-02 10:56:40 +0200612 break;
Christof Schmitt287ac012008-07-02 10:56:40 +0200613 case ZFCP_ERP_ACTION_REOPEN_PORT:
Swen Schilligea4a3a62010-12-02 15:16:16 +0100614 _zfcp_erp_port_reopen(act->port, 0, "ersff_3");
Christof Schmitt287ac012008-07-02 10:56:40 +0200615 break;
Christof Schmittb62a8d92010-09-08 14:39:55 +0200616 case ZFCP_ERP_ACTION_REOPEN_LUN:
Swen Schilligea4a3a62010-12-02 15:16:16 +0100617 _zfcp_erp_lun_reopen(act->sdev, 0, "ersff_4", 0);
Christof Schmitt85600f72009-07-13 15:06:09 +0200618 break;
619 }
620}
621
622static void zfcp_erp_strategy_followup_success(struct zfcp_erp_action *act)
623{
624 switch (act->action) {
625 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
Swen Schilligea4a3a62010-12-02 15:16:16 +0100626 _zfcp_erp_port_reopen_all(act->adapter, 0, "ersfs_1");
Christof Schmitt85600f72009-07-13 15:06:09 +0200627 break;
628 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
Swen Schilligea4a3a62010-12-02 15:16:16 +0100629 _zfcp_erp_port_reopen(act->port, 0, "ersfs_2");
Christof Schmitt85600f72009-07-13 15:06:09 +0200630 break;
631 case ZFCP_ERP_ACTION_REOPEN_PORT:
Swen Schilligea4a3a62010-12-02 15:16:16 +0100632 _zfcp_erp_lun_reopen_all(act->port, 0, "ersfs_3");
Christof Schmitt287ac012008-07-02 10:56:40 +0200633 break;
634 }
635}
636
637static void zfcp_erp_wakeup(struct zfcp_adapter *adapter)
638{
639 unsigned long flags;
640
Swen Schilligecf0c772009-11-24 16:53:58 +0100641 read_lock_irqsave(&adapter->erp_lock, flags);
Christof Schmitt287ac012008-07-02 10:56:40 +0200642 if (list_empty(&adapter->erp_ready_head) &&
643 list_empty(&adapter->erp_running_head)) {
Peter Zijlstra805de8f42015-04-24 01:12:32 +0200644 atomic_andnot(ZFCP_STATUS_ADAPTER_ERP_PENDING,
Christof Schmitt287ac012008-07-02 10:56:40 +0200645 &adapter->status);
646 wake_up(&adapter->erp_done_wqh);
647 }
Swen Schilligecf0c772009-11-24 16:53:58 +0100648 read_unlock_irqrestore(&adapter->erp_lock, flags);
Christof Schmitt287ac012008-07-02 10:56:40 +0200649}
650
Christof Schmitt287ac012008-07-02 10:56:40 +0200651static void zfcp_erp_enqueue_ptp_port(struct zfcp_adapter *adapter)
652{
653 struct zfcp_port *port;
654 port = zfcp_port_enqueue(adapter, adapter->peer_wwpn, 0,
655 adapter->peer_d_id);
656 if (IS_ERR(port)) /* error or port already attached */
657 return;
Swen Schilligea4a3a62010-12-02 15:16:16 +0100658 _zfcp_erp_port_reopen(port, 0, "ereptp1");
Christof Schmitt287ac012008-07-02 10:56:40 +0200659}
660
661static int zfcp_erp_adapter_strat_fsf_xconf(struct zfcp_erp_action *erp_action)
662{
663 int retries;
664 int sleep = 1;
665 struct zfcp_adapter *adapter = erp_action->adapter;
666
Peter Zijlstra805de8f42015-04-24 01:12:32 +0200667 atomic_andnot(ZFCP_STATUS_ADAPTER_XCONFIG_OK, &adapter->status);
Christof Schmitt287ac012008-07-02 10:56:40 +0200668
669 for (retries = 7; retries; retries--) {
Peter Zijlstra805de8f42015-04-24 01:12:32 +0200670 atomic_andnot(ZFCP_STATUS_ADAPTER_HOST_CON_INIT,
Christof Schmitt287ac012008-07-02 10:56:40 +0200671 &adapter->status);
672 write_lock_irq(&adapter->erp_lock);
673 zfcp_erp_action_to_running(erp_action);
674 write_unlock_irq(&adapter->erp_lock);
675 if (zfcp_fsf_exchange_config_data(erp_action)) {
Peter Zijlstra805de8f42015-04-24 01:12:32 +0200676 atomic_andnot(ZFCP_STATUS_ADAPTER_HOST_CON_INIT,
Christof Schmitt287ac012008-07-02 10:56:40 +0200677 &adapter->status);
678 return ZFCP_ERP_FAILED;
679 }
680
Christof Schmitt347c6a92009-08-18 15:43:25 +0200681 wait_event(adapter->erp_ready_wq,
682 !list_empty(&adapter->erp_ready_head));
Christof Schmitt287ac012008-07-02 10:56:40 +0200683 if (erp_action->status & ZFCP_STATUS_ERP_TIMEDOUT)
684 break;
685
686 if (!(atomic_read(&adapter->status) &
687 ZFCP_STATUS_ADAPTER_HOST_CON_INIT))
688 break;
689
690 ssleep(sleep);
691 sleep *= 2;
692 }
693
Peter Zijlstra805de8f42015-04-24 01:12:32 +0200694 atomic_andnot(ZFCP_STATUS_ADAPTER_HOST_CON_INIT,
Christof Schmitt287ac012008-07-02 10:56:40 +0200695 &adapter->status);
696
697 if (!(atomic_read(&adapter->status) & ZFCP_STATUS_ADAPTER_XCONFIG_OK))
698 return ZFCP_ERP_FAILED;
699
700 if (fc_host_port_type(adapter->scsi_host) == FC_PORTTYPE_PTP)
701 zfcp_erp_enqueue_ptp_port(adapter);
702
703 return ZFCP_ERP_SUCCEEDED;
704}
705
706static int zfcp_erp_adapter_strategy_open_fsf_xport(struct zfcp_erp_action *act)
707{
708 int ret;
709 struct zfcp_adapter *adapter = act->adapter;
710
Christof Schmitt287ac012008-07-02 10:56:40 +0200711 write_lock_irq(&adapter->erp_lock);
712 zfcp_erp_action_to_running(act);
713 write_unlock_irq(&adapter->erp_lock);
714
715 ret = zfcp_fsf_exchange_port_data(act);
716 if (ret == -EOPNOTSUPP)
717 return ZFCP_ERP_SUCCEEDED;
718 if (ret)
719 return ZFCP_ERP_FAILED;
720
Swen Schilligae0904f2010-12-02 15:16:12 +0100721 zfcp_dbf_rec_run("erasox1", act);
Christof Schmitt347c6a92009-08-18 15:43:25 +0200722 wait_event(adapter->erp_ready_wq,
723 !list_empty(&adapter->erp_ready_head));
Swen Schilligae0904f2010-12-02 15:16:12 +0100724 zfcp_dbf_rec_run("erasox2", act);
Christof Schmitt287ac012008-07-02 10:56:40 +0200725 if (act->status & ZFCP_STATUS_ERP_TIMEDOUT)
726 return ZFCP_ERP_FAILED;
727
728 return ZFCP_ERP_SUCCEEDED;
729}
730
731static int zfcp_erp_adapter_strategy_open_fsf(struct zfcp_erp_action *act)
732{
733 if (zfcp_erp_adapter_strat_fsf_xconf(act) == ZFCP_ERP_FAILED)
734 return ZFCP_ERP_FAILED;
735
736 if (zfcp_erp_adapter_strategy_open_fsf_xport(act) == ZFCP_ERP_FAILED)
737 return ZFCP_ERP_FAILED;
738
Christof Schmittc7b279a2011-02-22 19:54:40 +0100739 if (mempool_resize(act->adapter->pool.sr_data,
David Rientjes11d83362015-04-14 15:48:21 -0700740 act->adapter->stat_read_buf_num))
Christof Schmitt8d88cf32010-06-21 10:11:33 +0200741 return ZFCP_ERP_FAILED;
742
743 if (mempool_resize(act->adapter->pool.status_read_req,
David Rientjes11d83362015-04-14 15:48:21 -0700744 act->adapter->stat_read_buf_num))
Christof Schmitt8d88cf32010-06-21 10:11:33 +0200745 return ZFCP_ERP_FAILED;
746
Christof Schmitt64deb6e2010-04-30 18:09:36 +0200747 atomic_set(&act->adapter->stat_miss, act->adapter->stat_read_buf_num);
Christof Schmitt287ac012008-07-02 10:56:40 +0200748 if (zfcp_status_read_refill(act->adapter))
749 return ZFCP_ERP_FAILED;
750
751 return ZFCP_ERP_SUCCEEDED;
752}
753
Swen Schilligcf13c082009-03-02 13:09:03 +0100754static void zfcp_erp_adapter_strategy_close(struct zfcp_erp_action *act)
Christof Schmitt287ac012008-07-02 10:56:40 +0200755{
Christof Schmitt287ac012008-07-02 10:56:40 +0200756 struct zfcp_adapter *adapter = act->adapter;
757
Christof Schmitt287ac012008-07-02 10:56:40 +0200758 /* close queues to ensure that buffers are not accessed by adapter */
Swen Schillig564e1c82009-08-18 15:43:19 +0200759 zfcp_qdio_close(adapter->qdio);
Christof Schmitt287ac012008-07-02 10:56:40 +0200760 zfcp_fsf_req_dismiss_all(adapter);
761 adapter->fsf_req_seq_no = 0;
Christof Schmitt55c770f2009-08-18 15:43:12 +0200762 zfcp_fc_wka_ports_force_offline(adapter->gs);
Christof Schmittb62a8d92010-09-08 14:39:55 +0200763 /* all ports and LUNs are closed */
Swen Schilligedaed852010-09-08 14:40:01 +0200764 zfcp_erp_clear_adapter_status(adapter, ZFCP_STATUS_COMMON_OPEN);
Swen Schilligcf13c082009-03-02 13:09:03 +0100765
Peter Zijlstra805de8f42015-04-24 01:12:32 +0200766 atomic_andnot(ZFCP_STATUS_ADAPTER_XCONFIG_OK |
Swen Schilligcf13c082009-03-02 13:09:03 +0100767 ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED, &adapter->status);
768}
769
770static int zfcp_erp_adapter_strategy_open(struct zfcp_erp_action *act)
771{
772 struct zfcp_adapter *adapter = act->adapter;
773
Christof Schmitt3d63d3b2010-12-02 15:16:17 +0100774 if (zfcp_qdio_open(adapter->qdio)) {
Peter Zijlstra805de8f42015-04-24 01:12:32 +0200775 atomic_andnot(ZFCP_STATUS_ADAPTER_XCONFIG_OK |
Swen Schilligcf13c082009-03-02 13:09:03 +0100776 ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED,
777 &adapter->status);
778 return ZFCP_ERP_FAILED;
779 }
780
781 if (zfcp_erp_adapter_strategy_open_fsf(act)) {
782 zfcp_erp_adapter_strategy_close(act);
783 return ZFCP_ERP_FAILED;
784 }
785
Peter Zijlstra805de8f42015-04-24 01:12:32 +0200786 atomic_or(ZFCP_STATUS_COMMON_OPEN, &adapter->status);
Swen Schilligcf13c082009-03-02 13:09:03 +0100787
788 return ZFCP_ERP_SUCCEEDED;
Christof Schmitt287ac012008-07-02 10:56:40 +0200789}
790
791static int zfcp_erp_adapter_strategy(struct zfcp_erp_action *act)
792{
Swen Schilligcf13c082009-03-02 13:09:03 +0100793 struct zfcp_adapter *adapter = act->adapter;
Christof Schmitt287ac012008-07-02 10:56:40 +0200794
Swen Schilligcf13c082009-03-02 13:09:03 +0100795 if (atomic_read(&adapter->status) & ZFCP_STATUS_COMMON_OPEN) {
796 zfcp_erp_adapter_strategy_close(act);
797 if (act->status & ZFCP_STATUS_ERP_CLOSE_ONLY)
798 return ZFCP_ERP_EXIT;
799 }
Christof Schmitt287ac012008-07-02 10:56:40 +0200800
Swen Schilligcf13c082009-03-02 13:09:03 +0100801 if (zfcp_erp_adapter_strategy_open(act)) {
Christof Schmitt287ac012008-07-02 10:56:40 +0200802 ssleep(8);
Swen Schilligcf13c082009-03-02 13:09:03 +0100803 return ZFCP_ERP_FAILED;
804 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805
Swen Schilligcf13c082009-03-02 13:09:03 +0100806 return ZFCP_ERP_SUCCEEDED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807}
808
Christof Schmitt287ac012008-07-02 10:56:40 +0200809static int zfcp_erp_port_forced_strategy_close(struct zfcp_erp_action *act)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810{
Christof Schmitt287ac012008-07-02 10:56:40 +0200811 int retval;
812
813 retval = zfcp_fsf_close_physical_port(act);
814 if (retval == -ENOMEM)
815 return ZFCP_ERP_NOMEM;
816 act->step = ZFCP_ERP_STEP_PHYS_PORT_CLOSING;
817 if (retval)
818 return ZFCP_ERP_FAILED;
819
820 return ZFCP_ERP_CONTINUES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821}
822
Christof Schmitt287ac012008-07-02 10:56:40 +0200823static int zfcp_erp_port_forced_strategy(struct zfcp_erp_action *erp_action)
824{
825 struct zfcp_port *port = erp_action->port;
826 int status = atomic_read(&port->status);
827
828 switch (erp_action->step) {
829 case ZFCP_ERP_STEP_UNINITIALIZED:
Christof Schmitt287ac012008-07-02 10:56:40 +0200830 if ((status & ZFCP_STATUS_PORT_PHYS_OPEN) &&
831 (status & ZFCP_STATUS_COMMON_OPEN))
832 return zfcp_erp_port_forced_strategy_close(erp_action);
833 else
834 return ZFCP_ERP_FAILED;
835
836 case ZFCP_ERP_STEP_PHYS_PORT_CLOSING:
Christof Schmittddb3e0c2009-07-13 15:06:08 +0200837 if (!(status & ZFCP_STATUS_PORT_PHYS_OPEN))
Christof Schmitt287ac012008-07-02 10:56:40 +0200838 return ZFCP_ERP_SUCCEEDED;
839 }
840 return ZFCP_ERP_FAILED;
841}
842
843static int zfcp_erp_port_strategy_close(struct zfcp_erp_action *erp_action)
844{
845 int retval;
846
847 retval = zfcp_fsf_close_port(erp_action);
848 if (retval == -ENOMEM)
849 return ZFCP_ERP_NOMEM;
850 erp_action->step = ZFCP_ERP_STEP_PORT_CLOSING;
851 if (retval)
852 return ZFCP_ERP_FAILED;
853 return ZFCP_ERP_CONTINUES;
854}
855
856static int zfcp_erp_port_strategy_open_port(struct zfcp_erp_action *erp_action)
857{
858 int retval;
859
860 retval = zfcp_fsf_open_port(erp_action);
861 if (retval == -ENOMEM)
862 return ZFCP_ERP_NOMEM;
863 erp_action->step = ZFCP_ERP_STEP_PORT_OPENING;
864 if (retval)
865 return ZFCP_ERP_FAILED;
866 return ZFCP_ERP_CONTINUES;
867}
868
Christof Schmitt287ac012008-07-02 10:56:40 +0200869static int zfcp_erp_open_ptp_port(struct zfcp_erp_action *act)
870{
871 struct zfcp_adapter *adapter = act->adapter;
872 struct zfcp_port *port = act->port;
873
874 if (port->wwpn != adapter->peer_wwpn) {
Swen Schilligedaed852010-09-08 14:40:01 +0200875 zfcp_erp_set_port_status(port, ZFCP_STATUS_COMMON_ERP_FAILED);
Christof Schmitt287ac012008-07-02 10:56:40 +0200876 return ZFCP_ERP_FAILED;
877 }
878 port->d_id = adapter->peer_d_id;
Christof Schmitt287ac012008-07-02 10:56:40 +0200879 return zfcp_erp_port_strategy_open_port(act);
880}
881
882static int zfcp_erp_port_strategy_open_common(struct zfcp_erp_action *act)
883{
884 struct zfcp_adapter *adapter = act->adapter;
885 struct zfcp_port *port = act->port;
Christof Schmitt287ac012008-07-02 10:56:40 +0200886 int p_status = atomic_read(&port->status);
887
888 switch (act->step) {
889 case ZFCP_ERP_STEP_UNINITIALIZED:
890 case ZFCP_ERP_STEP_PHYS_PORT_CLOSING:
891 case ZFCP_ERP_STEP_PORT_CLOSING:
892 if (fc_host_port_type(adapter->scsi_host) == FC_PORTTYPE_PTP)
893 return zfcp_erp_open_ptp_port(act);
Christof Schmittb98478d2008-12-19 16:56:59 +0100894 if (!port->d_id) {
Christof Schmitt934aeb5872009-10-14 11:00:43 +0200895 zfcp_fc_trigger_did_lookup(port);
Christof Schmitt799b76d2009-08-18 15:43:20 +0200896 return ZFCP_ERP_EXIT;
Christof Schmitt287ac012008-07-02 10:56:40 +0200897 }
Christof Schmitt287ac012008-07-02 10:56:40 +0200898 return zfcp_erp_port_strategy_open_port(act);
899
900 case ZFCP_ERP_STEP_PORT_OPENING:
901 /* D_ID might have changed during open */
Swen Schillig5ab944f2008-10-01 12:42:17 +0200902 if (p_status & ZFCP_STATUS_COMMON_OPEN) {
Christof Schmitt934aeb5872009-10-14 11:00:43 +0200903 if (!port->d_id) {
904 zfcp_fc_trigger_did_lookup(port);
905 return ZFCP_ERP_EXIT;
Swen Schillig5ab944f2008-10-01 12:42:17 +0200906 }
Christof Schmitt934aeb5872009-10-14 11:00:43 +0200907 return ZFCP_ERP_SUCCEEDED;
Swen Schillig5ab944f2008-10-01 12:42:17 +0200908 }
Swen Schilligea460a82009-05-15 13:18:20 +0200909 if (port->d_id && !(p_status & ZFCP_STATUS_COMMON_NOESC)) {
910 port->d_id = 0;
Christof Schmittf7bd7c32010-07-08 09:53:10 +0200911 return ZFCP_ERP_FAILED;
Swen Schilligea460a82009-05-15 13:18:20 +0200912 }
913 /* fall through otherwise */
Christof Schmitt287ac012008-07-02 10:56:40 +0200914 }
915 return ZFCP_ERP_FAILED;
916}
917
Christof Schmitt287ac012008-07-02 10:56:40 +0200918static int zfcp_erp_port_strategy(struct zfcp_erp_action *erp_action)
919{
920 struct zfcp_port *port = erp_action->port;
Christof Schmitt934aeb5872009-10-14 11:00:43 +0200921 int p_status = atomic_read(&port->status);
Christof Schmitt287ac012008-07-02 10:56:40 +0200922
Christof Schmitt934aeb5872009-10-14 11:00:43 +0200923 if ((p_status & ZFCP_STATUS_COMMON_NOESC) &&
924 !(p_status & ZFCP_STATUS_COMMON_OPEN))
Swen Schillig5ab944f2008-10-01 12:42:17 +0200925 goto close_init_done;
926
Christof Schmitt287ac012008-07-02 10:56:40 +0200927 switch (erp_action->step) {
928 case ZFCP_ERP_STEP_UNINITIALIZED:
Christof Schmitt934aeb5872009-10-14 11:00:43 +0200929 if (p_status & ZFCP_STATUS_COMMON_OPEN)
Christof Schmitt287ac012008-07-02 10:56:40 +0200930 return zfcp_erp_port_strategy_close(erp_action);
931 break;
932
933 case ZFCP_ERP_STEP_PORT_CLOSING:
Christof Schmitt934aeb5872009-10-14 11:00:43 +0200934 if (p_status & ZFCP_STATUS_COMMON_OPEN)
Christof Schmitt287ac012008-07-02 10:56:40 +0200935 return ZFCP_ERP_FAILED;
936 break;
937 }
Swen Schillig5ab944f2008-10-01 12:42:17 +0200938
939close_init_done:
Christof Schmitt287ac012008-07-02 10:56:40 +0200940 if (erp_action->status & ZFCP_STATUS_ERP_CLOSE_ONLY)
941 return ZFCP_ERP_EXIT;
Christof Schmitt287ac012008-07-02 10:56:40 +0200942
Swen Schillig5ab944f2008-10-01 12:42:17 +0200943 return zfcp_erp_port_strategy_open_common(erp_action);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944}
945
Christof Schmittb62a8d92010-09-08 14:39:55 +0200946static void zfcp_erp_lun_strategy_clearstati(struct scsi_device *sdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947{
Christof Schmittb62a8d92010-09-08 14:39:55 +0200948 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
949
Peter Zijlstra805de8f42015-04-24 01:12:32 +0200950 atomic_andnot(ZFCP_STATUS_COMMON_ACCESS_DENIED,
Christof Schmittb62a8d92010-09-08 14:39:55 +0200951 &zfcp_sdev->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952}
953
Christof Schmittb62a8d92010-09-08 14:39:55 +0200954static int zfcp_erp_lun_strategy_close(struct zfcp_erp_action *erp_action)
Christof Schmitt287ac012008-07-02 10:56:40 +0200955{
Christof Schmittb62a8d92010-09-08 14:39:55 +0200956 int retval = zfcp_fsf_close_lun(erp_action);
Christof Schmitt287ac012008-07-02 10:56:40 +0200957 if (retval == -ENOMEM)
958 return ZFCP_ERP_NOMEM;
Christof Schmittb62a8d92010-09-08 14:39:55 +0200959 erp_action->step = ZFCP_ERP_STEP_LUN_CLOSING;
Christof Schmitt287ac012008-07-02 10:56:40 +0200960 if (retval)
961 return ZFCP_ERP_FAILED;
962 return ZFCP_ERP_CONTINUES;
963}
964
Christof Schmittb62a8d92010-09-08 14:39:55 +0200965static int zfcp_erp_lun_strategy_open(struct zfcp_erp_action *erp_action)
Christof Schmitt287ac012008-07-02 10:56:40 +0200966{
Christof Schmittb62a8d92010-09-08 14:39:55 +0200967 int retval = zfcp_fsf_open_lun(erp_action);
Christof Schmitt287ac012008-07-02 10:56:40 +0200968 if (retval == -ENOMEM)
969 return ZFCP_ERP_NOMEM;
Christof Schmittb62a8d92010-09-08 14:39:55 +0200970 erp_action->step = ZFCP_ERP_STEP_LUN_OPENING;
Christof Schmitt287ac012008-07-02 10:56:40 +0200971 if (retval)
972 return ZFCP_ERP_FAILED;
973 return ZFCP_ERP_CONTINUES;
974}
975
Christof Schmittb62a8d92010-09-08 14:39:55 +0200976static int zfcp_erp_lun_strategy(struct zfcp_erp_action *erp_action)
Christof Schmitt287ac012008-07-02 10:56:40 +0200977{
Christof Schmittb62a8d92010-09-08 14:39:55 +0200978 struct scsi_device *sdev = erp_action->sdev;
979 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
Christof Schmitt287ac012008-07-02 10:56:40 +0200980
981 switch (erp_action->step) {
982 case ZFCP_ERP_STEP_UNINITIALIZED:
Christof Schmittb62a8d92010-09-08 14:39:55 +0200983 zfcp_erp_lun_strategy_clearstati(sdev);
984 if (atomic_read(&zfcp_sdev->status) & ZFCP_STATUS_COMMON_OPEN)
985 return zfcp_erp_lun_strategy_close(erp_action);
Christof Schmitt287ac012008-07-02 10:56:40 +0200986 /* already closed, fall through */
Christof Schmittb62a8d92010-09-08 14:39:55 +0200987 case ZFCP_ERP_STEP_LUN_CLOSING:
988 if (atomic_read(&zfcp_sdev->status) & ZFCP_STATUS_COMMON_OPEN)
Christof Schmitt287ac012008-07-02 10:56:40 +0200989 return ZFCP_ERP_FAILED;
990 if (erp_action->status & ZFCP_STATUS_ERP_CLOSE_ONLY)
991 return ZFCP_ERP_EXIT;
Christof Schmittb62a8d92010-09-08 14:39:55 +0200992 return zfcp_erp_lun_strategy_open(erp_action);
Christof Schmitt287ac012008-07-02 10:56:40 +0200993
Christof Schmittb62a8d92010-09-08 14:39:55 +0200994 case ZFCP_ERP_STEP_LUN_OPENING:
995 if (atomic_read(&zfcp_sdev->status) & ZFCP_STATUS_COMMON_OPEN)
Christof Schmitt287ac012008-07-02 10:56:40 +0200996 return ZFCP_ERP_SUCCEEDED;
997 }
998 return ZFCP_ERP_FAILED;
999}
1000
Christof Schmittb62a8d92010-09-08 14:39:55 +02001001static int zfcp_erp_strategy_check_lun(struct scsi_device *sdev, int result)
Christof Schmitt287ac012008-07-02 10:56:40 +02001002{
Christof Schmittb62a8d92010-09-08 14:39:55 +02001003 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
1004
Christof Schmitt287ac012008-07-02 10:56:40 +02001005 switch (result) {
1006 case ZFCP_ERP_SUCCEEDED :
Christof Schmittb62a8d92010-09-08 14:39:55 +02001007 atomic_set(&zfcp_sdev->erp_counter, 0);
1008 zfcp_erp_lun_unblock(sdev);
Christof Schmitt287ac012008-07-02 10:56:40 +02001009 break;
1010 case ZFCP_ERP_FAILED :
Christof Schmittb62a8d92010-09-08 14:39:55 +02001011 atomic_inc(&zfcp_sdev->erp_counter);
1012 if (atomic_read(&zfcp_sdev->erp_counter) > ZFCP_MAX_ERPS) {
1013 dev_err(&zfcp_sdev->port->adapter->ccw_device->dev,
1014 "ERP failed for LUN 0x%016Lx on "
Christof Schmittff3b24f2008-10-01 12:42:15 +02001015 "port 0x%016Lx\n",
Christof Schmittb62a8d92010-09-08 14:39:55 +02001016 (unsigned long long)zfcp_scsi_dev_lun(sdev),
1017 (unsigned long long)zfcp_sdev->port->wwpn);
Swen Schilligedaed852010-09-08 14:40:01 +02001018 zfcp_erp_set_lun_status(sdev,
1019 ZFCP_STATUS_COMMON_ERP_FAILED);
Christof Schmittff3b24f2008-10-01 12:42:15 +02001020 }
Christof Schmitt287ac012008-07-02 10:56:40 +02001021 break;
1022 }
1023
Christof Schmittb62a8d92010-09-08 14:39:55 +02001024 if (atomic_read(&zfcp_sdev->status) & ZFCP_STATUS_COMMON_ERP_FAILED) {
1025 zfcp_erp_lun_block(sdev, 0);
Christof Schmitt287ac012008-07-02 10:56:40 +02001026 result = ZFCP_ERP_EXIT;
1027 }
1028 return result;
1029}
1030
1031static int zfcp_erp_strategy_check_port(struct zfcp_port *port, int result)
1032{
1033 switch (result) {
1034 case ZFCP_ERP_SUCCEEDED :
1035 atomic_set(&port->erp_counter, 0);
1036 zfcp_erp_port_unblock(port);
1037 break;
1038
1039 case ZFCP_ERP_FAILED :
1040 if (atomic_read(&port->status) & ZFCP_STATUS_COMMON_NOESC) {
1041 zfcp_erp_port_block(port, 0);
1042 result = ZFCP_ERP_EXIT;
1043 }
1044 atomic_inc(&port->erp_counter);
Christof Schmittff3b24f2008-10-01 12:42:15 +02001045 if (atomic_read(&port->erp_counter) > ZFCP_MAX_ERPS) {
1046 dev_err(&port->adapter->ccw_device->dev,
1047 "ERP failed for remote port 0x%016Lx\n",
Swen Schillig7ba58c92008-10-01 12:42:18 +02001048 (unsigned long long)port->wwpn);
Swen Schilligedaed852010-09-08 14:40:01 +02001049 zfcp_erp_set_port_status(port,
1050 ZFCP_STATUS_COMMON_ERP_FAILED);
Christof Schmittff3b24f2008-10-01 12:42:15 +02001051 }
Christof Schmitt287ac012008-07-02 10:56:40 +02001052 break;
1053 }
1054
1055 if (atomic_read(&port->status) & ZFCP_STATUS_COMMON_ERP_FAILED) {
1056 zfcp_erp_port_block(port, 0);
1057 result = ZFCP_ERP_EXIT;
1058 }
1059 return result;
1060}
1061
1062static int zfcp_erp_strategy_check_adapter(struct zfcp_adapter *adapter,
1063 int result)
1064{
1065 switch (result) {
1066 case ZFCP_ERP_SUCCEEDED :
1067 atomic_set(&adapter->erp_counter, 0);
1068 zfcp_erp_adapter_unblock(adapter);
1069 break;
1070
1071 case ZFCP_ERP_FAILED :
1072 atomic_inc(&adapter->erp_counter);
Christof Schmittff3b24f2008-10-01 12:42:15 +02001073 if (atomic_read(&adapter->erp_counter) > ZFCP_MAX_ERPS) {
1074 dev_err(&adapter->ccw_device->dev,
1075 "ERP cannot recover an error "
1076 "on the FCP device\n");
Swen Schilligedaed852010-09-08 14:40:01 +02001077 zfcp_erp_set_adapter_status(adapter,
1078 ZFCP_STATUS_COMMON_ERP_FAILED);
Christof Schmittff3b24f2008-10-01 12:42:15 +02001079 }
Christof Schmitt287ac012008-07-02 10:56:40 +02001080 break;
1081 }
1082
1083 if (atomic_read(&adapter->status) & ZFCP_STATUS_COMMON_ERP_FAILED) {
1084 zfcp_erp_adapter_block(adapter, 0);
1085 result = ZFCP_ERP_EXIT;
1086 }
1087 return result;
1088}
1089
1090static int zfcp_erp_strategy_check_target(struct zfcp_erp_action *erp_action,
1091 int result)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092{
1093 struct zfcp_adapter *adapter = erp_action->adapter;
1094 struct zfcp_port *port = erp_action->port;
Christof Schmittb62a8d92010-09-08 14:39:55 +02001095 struct scsi_device *sdev = erp_action->sdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 switch (erp_action->action) {
1098
Christof Schmittb62a8d92010-09-08 14:39:55 +02001099 case ZFCP_ERP_ACTION_REOPEN_LUN:
1100 result = zfcp_erp_strategy_check_lun(sdev, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 break;
1102
1103 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
1104 case ZFCP_ERP_ACTION_REOPEN_PORT:
1105 result = zfcp_erp_strategy_check_port(port, result);
1106 break;
1107
1108 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
1109 result = zfcp_erp_strategy_check_adapter(adapter, result);
1110 break;
1111 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 return result;
1113}
1114
Christof Schmitt287ac012008-07-02 10:56:40 +02001115static int zfcp_erp_strat_change_det(atomic_t *target_status, u32 erp_status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116{
Christof Schmitt287ac012008-07-02 10:56:40 +02001117 int status = atomic_read(target_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118
Christof Schmitt287ac012008-07-02 10:56:40 +02001119 if ((status & ZFCP_STATUS_COMMON_RUNNING) &&
1120 (erp_status & ZFCP_STATUS_ERP_CLOSE_ONLY))
1121 return 1; /* take it online */
1122
1123 if (!(status & ZFCP_STATUS_COMMON_RUNNING) &&
1124 !(erp_status & ZFCP_STATUS_ERP_CLOSE_ONLY))
1125 return 1; /* take it offline */
1126
1127 return 0;
1128}
1129
1130static int zfcp_erp_strategy_statechange(struct zfcp_erp_action *act, int ret)
1131{
1132 int action = act->action;
1133 struct zfcp_adapter *adapter = act->adapter;
1134 struct zfcp_port *port = act->port;
Christof Schmittb62a8d92010-09-08 14:39:55 +02001135 struct scsi_device *sdev = act->sdev;
1136 struct zfcp_scsi_dev *zfcp_sdev;
Christof Schmitt287ac012008-07-02 10:56:40 +02001137 u32 erp_status = act->status;
1138
1139 switch (action) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
Christof Schmitt287ac012008-07-02 10:56:40 +02001141 if (zfcp_erp_strat_change_det(&adapter->status, erp_status)) {
1142 _zfcp_erp_adapter_reopen(adapter,
1143 ZFCP_STATUS_COMMON_ERP_FAILED,
Swen Schilligea4a3a62010-12-02 15:16:16 +01001144 "ersscg1");
Christof Schmitt287ac012008-07-02 10:56:40 +02001145 return ZFCP_ERP_EXIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 }
1147 break;
1148
1149 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
1150 case ZFCP_ERP_ACTION_REOPEN_PORT:
Christof Schmitt287ac012008-07-02 10:56:40 +02001151 if (zfcp_erp_strat_change_det(&port->status, erp_status)) {
1152 _zfcp_erp_port_reopen(port,
1153 ZFCP_STATUS_COMMON_ERP_FAILED,
Swen Schilligea4a3a62010-12-02 15:16:16 +01001154 "ersscg2");
Christof Schmitt287ac012008-07-02 10:56:40 +02001155 return ZFCP_ERP_EXIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 }
1157 break;
1158
Christof Schmittb62a8d92010-09-08 14:39:55 +02001159 case ZFCP_ERP_ACTION_REOPEN_LUN:
1160 zfcp_sdev = sdev_to_zfcp(sdev);
1161 if (zfcp_erp_strat_change_det(&zfcp_sdev->status, erp_status)) {
1162 _zfcp_erp_lun_reopen(sdev,
1163 ZFCP_STATUS_COMMON_ERP_FAILED,
Swen Schilligea4a3a62010-12-02 15:16:16 +01001164 "ersscg3", 0);
Christof Schmitt287ac012008-07-02 10:56:40 +02001165 return ZFCP_ERP_EXIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 }
1167 break;
1168 }
Christof Schmitt287ac012008-07-02 10:56:40 +02001169 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170}
1171
Christof Schmitt287ac012008-07-02 10:56:40 +02001172static void zfcp_erp_action_dequeue(struct zfcp_erp_action *erp_action)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173{
Christof Schmitt287ac012008-07-02 10:56:40 +02001174 struct zfcp_adapter *adapter = erp_action->adapter;
Christof Schmittb62a8d92010-09-08 14:39:55 +02001175 struct zfcp_scsi_dev *zfcp_sdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176
Christof Schmitt287ac012008-07-02 10:56:40 +02001177 adapter->erp_total_count--;
1178 if (erp_action->status & ZFCP_STATUS_ERP_LOWMEM) {
1179 adapter->erp_low_mem_count--;
1180 erp_action->status &= ~ZFCP_STATUS_ERP_LOWMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 }
1182
Christof Schmitt287ac012008-07-02 10:56:40 +02001183 list_del(&erp_action->list);
Swen Schilligae0904f2010-12-02 15:16:12 +01001184 zfcp_dbf_rec_run("eractd1", erp_action);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185
Christof Schmitt287ac012008-07-02 10:56:40 +02001186 switch (erp_action->action) {
Christof Schmittb62a8d92010-09-08 14:39:55 +02001187 case ZFCP_ERP_ACTION_REOPEN_LUN:
1188 zfcp_sdev = sdev_to_zfcp(erp_action->sdev);
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001189 atomic_andnot(ZFCP_STATUS_COMMON_ERP_INUSE,
Christof Schmittb62a8d92010-09-08 14:39:55 +02001190 &zfcp_sdev->status);
Christof Schmitt287ac012008-07-02 10:56:40 +02001191 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192
Christof Schmitt287ac012008-07-02 10:56:40 +02001193 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
1194 case ZFCP_ERP_ACTION_REOPEN_PORT:
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001195 atomic_andnot(ZFCP_STATUS_COMMON_ERP_INUSE,
Christof Schmitt287ac012008-07-02 10:56:40 +02001196 &erp_action->port->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 break;
Christof Schmitt287ac012008-07-02 10:56:40 +02001198
1199 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001200 atomic_andnot(ZFCP_STATUS_COMMON_ERP_INUSE,
Christof Schmitt287ac012008-07-02 10:56:40 +02001201 &erp_action->adapter->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 break;
1203 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204}
1205
Steffen Maier6f2ce1c2016-12-09 17:16:33 +01001206/**
1207 * zfcp_erp_try_rport_unblock - unblock rport if no more/new recovery
1208 * @port: zfcp_port whose fc_rport we should try to unblock
1209 */
1210static void zfcp_erp_try_rport_unblock(struct zfcp_port *port)
1211{
1212 unsigned long flags;
1213 struct zfcp_adapter *adapter = port->adapter;
1214 int port_status;
1215 struct Scsi_Host *shost = adapter->scsi_host;
1216 struct scsi_device *sdev;
1217
1218 write_lock_irqsave(&adapter->erp_lock, flags);
1219 port_status = atomic_read(&port->status);
1220 if ((port_status & ZFCP_STATUS_COMMON_UNBLOCKED) == 0 ||
1221 (port_status & (ZFCP_STATUS_COMMON_ERP_INUSE |
1222 ZFCP_STATUS_COMMON_ERP_FAILED)) != 0) {
1223 /* new ERP of severity >= port triggered elsewhere meanwhile or
1224 * local link down (adapter erp_failed but not clear unblock)
1225 */
1226 zfcp_dbf_rec_run_lvl(4, "ertru_p", &port->erp_action);
1227 write_unlock_irqrestore(&adapter->erp_lock, flags);
1228 return;
1229 }
1230 spin_lock(shost->host_lock);
1231 __shost_for_each_device(sdev, shost) {
1232 struct zfcp_scsi_dev *zsdev = sdev_to_zfcp(sdev);
1233 int lun_status;
1234
1235 if (zsdev->port != port)
1236 continue;
1237 /* LUN under port of interest */
1238 lun_status = atomic_read(&zsdev->status);
1239 if ((lun_status & ZFCP_STATUS_COMMON_ERP_FAILED) != 0)
1240 continue; /* unblock rport despite failed LUNs */
1241 /* LUN recovery not given up yet [maybe follow-up pending] */
1242 if ((lun_status & ZFCP_STATUS_COMMON_UNBLOCKED) == 0 ||
1243 (lun_status & ZFCP_STATUS_COMMON_ERP_INUSE) != 0) {
1244 /* LUN blocked:
1245 * not yet unblocked [LUN recovery pending]
1246 * or meanwhile blocked [new LUN recovery triggered]
1247 */
1248 zfcp_dbf_rec_run_lvl(4, "ertru_l", &zsdev->erp_action);
1249 spin_unlock(shost->host_lock);
1250 write_unlock_irqrestore(&adapter->erp_lock, flags);
1251 return;
1252 }
1253 }
1254 /* now port has no child or all children have completed recovery,
1255 * and no ERP of severity >= port was meanwhile triggered elsewhere
1256 */
1257 zfcp_scsi_schedule_rport_register(port);
1258 spin_unlock(shost->host_lock);
1259 write_unlock_irqrestore(&adapter->erp_lock, flags);
1260}
1261
Christof Schmitt287ac012008-07-02 10:56:40 +02001262static void zfcp_erp_action_cleanup(struct zfcp_erp_action *act, int result)
Maxim Shchetyninaef4a982005-09-13 21:51:16 +02001263{
Christof Schmitt287ac012008-07-02 10:56:40 +02001264 struct zfcp_adapter *adapter = act->adapter;
1265 struct zfcp_port *port = act->port;
Christof Schmittb62a8d92010-09-08 14:39:55 +02001266 struct scsi_device *sdev = act->sdev;
Maxim Shchetyninaef4a982005-09-13 21:51:16 +02001267
Christof Schmitt287ac012008-07-02 10:56:40 +02001268 switch (act->action) {
Christof Schmittb62a8d92010-09-08 14:39:55 +02001269 case ZFCP_ERP_ACTION_REOPEN_LUN:
Christof Schmittfdbd1c52010-09-08 14:39:54 +02001270 if (!(act->status & ZFCP_STATUS_ERP_NO_REF))
Christof Schmittb62a8d92010-09-08 14:39:55 +02001271 scsi_device_put(sdev);
Steffen Maier6f2ce1c2016-12-09 17:16:33 +01001272 zfcp_erp_try_rport_unblock(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 break;
Christof Schmitt287ac012008-07-02 10:56:40 +02001274
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 case ZFCP_ERP_ACTION_REOPEN_PORT:
Steffen Maier4eeaa4f2016-08-10 18:30:46 +02001276 /* This switch case might also happen after a forced reopen
1277 * was successfully done and thus overwritten with a new
1278 * non-forced reopen at `ersfs_2'. In this case, we must not
1279 * do the clean-up of the non-forced version.
1280 */
1281 if (act->step != ZFCP_ERP_STEP_UNINITIALIZED)
1282 if (result == ZFCP_ERP_SUCCEEDED)
Steffen Maier6f2ce1c2016-12-09 17:16:33 +01001283 zfcp_erp_try_rport_unblock(port);
Christof Schmitt57676202010-07-08 09:53:05 +02001284 /* fall through */
1285 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
Christof Schmitt615f59e2010-02-17 11:18:56 +01001286 put_device(&port->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 break;
Christof Schmitt287ac012008-07-02 10:56:40 +02001288
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
Christof Schmitta2fa0ae2009-03-02 13:09:08 +01001290 if (result == ZFCP_ERP_SUCCEEDED) {
Christof Schmittbd43a42b72008-12-25 13:38:50 +01001291 register_service_level(&adapter->service_level);
Steffen Maier43f60cb2012-09-04 15:23:35 +02001292 zfcp_fc_conditional_port_scan(adapter);
Christof Schmitt038d9442011-02-22 19:54:48 +01001293 queue_work(adapter->work_queue, &adapter->ns_up_work);
Christof Schmitta2fa0ae2009-03-02 13:09:08 +01001294 } else
1295 unregister_service_level(&adapter->service_level);
Christof Schmitt038d9442011-02-22 19:54:48 +01001296
Swen Schilligf3450c72009-11-24 16:53:59 +01001297 kref_put(&adapter->ref, zfcp_adapter_release);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 }
1300}
1301
Christof Schmitt287ac012008-07-02 10:56:40 +02001302static int zfcp_erp_strategy_do_action(struct zfcp_erp_action *erp_action)
1303{
1304 switch (erp_action->action) {
1305 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
1306 return zfcp_erp_adapter_strategy(erp_action);
1307 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
1308 return zfcp_erp_port_forced_strategy(erp_action);
1309 case ZFCP_ERP_ACTION_REOPEN_PORT:
1310 return zfcp_erp_port_strategy(erp_action);
Christof Schmittb62a8d92010-09-08 14:39:55 +02001311 case ZFCP_ERP_ACTION_REOPEN_LUN:
1312 return zfcp_erp_lun_strategy(erp_action);
Christof Schmitt287ac012008-07-02 10:56:40 +02001313 }
1314 return ZFCP_ERP_FAILED;
1315}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316
Christof Schmitt287ac012008-07-02 10:56:40 +02001317static int zfcp_erp_strategy(struct zfcp_erp_action *erp_action)
1318{
1319 int retval;
Christof Schmitt287ac012008-07-02 10:56:40 +02001320 unsigned long flags;
Swen Schilligecf0c772009-11-24 16:53:58 +01001321 struct zfcp_adapter *adapter = erp_action->adapter;
Christof Schmitt287ac012008-07-02 10:56:40 +02001322
Swen Schilligf3450c72009-11-24 16:53:59 +01001323 kref_get(&adapter->ref);
Christof Schmitt287ac012008-07-02 10:56:40 +02001324
Swen Schilligf3450c72009-11-24 16:53:59 +01001325 write_lock_irqsave(&adapter->erp_lock, flags);
Christof Schmitt287ac012008-07-02 10:56:40 +02001326 zfcp_erp_strategy_check_fsfreq(erp_action);
1327
1328 if (erp_action->status & ZFCP_STATUS_ERP_DISMISSED) {
1329 zfcp_erp_action_dequeue(erp_action);
1330 retval = ZFCP_ERP_DISMISSED;
1331 goto unlock;
1332 }
1333
Christof Schmitt9c785d92010-07-08 09:53:09 +02001334 if (erp_action->status & ZFCP_STATUS_ERP_TIMEDOUT) {
1335 retval = ZFCP_ERP_FAILED;
1336 goto check_target;
1337 }
1338
Christof Schmitt287ac012008-07-02 10:56:40 +02001339 zfcp_erp_action_to_running(erp_action);
1340
1341 /* no lock to allow for blocking operations */
Swen Schilligecf0c772009-11-24 16:53:58 +01001342 write_unlock_irqrestore(&adapter->erp_lock, flags);
Christof Schmitt287ac012008-07-02 10:56:40 +02001343 retval = zfcp_erp_strategy_do_action(erp_action);
Swen Schilligecf0c772009-11-24 16:53:58 +01001344 write_lock_irqsave(&adapter->erp_lock, flags);
Christof Schmitt287ac012008-07-02 10:56:40 +02001345
1346 if (erp_action->status & ZFCP_STATUS_ERP_DISMISSED)
1347 retval = ZFCP_ERP_CONTINUES;
1348
1349 switch (retval) {
1350 case ZFCP_ERP_NOMEM:
1351 if (!(erp_action->status & ZFCP_STATUS_ERP_LOWMEM)) {
1352 ++adapter->erp_low_mem_count;
1353 erp_action->status |= ZFCP_STATUS_ERP_LOWMEM;
1354 }
1355 if (adapter->erp_total_count == adapter->erp_low_mem_count)
Swen Schilligea4a3a62010-12-02 15:16:16 +01001356 _zfcp_erp_adapter_reopen(adapter, 0, "erstgy1");
Christof Schmitt287ac012008-07-02 10:56:40 +02001357 else {
1358 zfcp_erp_strategy_memwait(erp_action);
1359 retval = ZFCP_ERP_CONTINUES;
1360 }
1361 goto unlock;
1362
1363 case ZFCP_ERP_CONTINUES:
1364 if (erp_action->status & ZFCP_STATUS_ERP_LOWMEM) {
1365 --adapter->erp_low_mem_count;
1366 erp_action->status &= ~ZFCP_STATUS_ERP_LOWMEM;
1367 }
1368 goto unlock;
1369 }
1370
Christof Schmitt9c785d92010-07-08 09:53:09 +02001371check_target:
Christof Schmitt287ac012008-07-02 10:56:40 +02001372 retval = zfcp_erp_strategy_check_target(erp_action, retval);
1373 zfcp_erp_action_dequeue(erp_action);
1374 retval = zfcp_erp_strategy_statechange(erp_action, retval);
1375 if (retval == ZFCP_ERP_EXIT)
1376 goto unlock;
Christof Schmitt85600f72009-07-13 15:06:09 +02001377 if (retval == ZFCP_ERP_SUCCEEDED)
1378 zfcp_erp_strategy_followup_success(erp_action);
1379 if (retval == ZFCP_ERP_FAILED)
1380 zfcp_erp_strategy_followup_failed(erp_action);
Christof Schmitt287ac012008-07-02 10:56:40 +02001381
1382 unlock:
Swen Schilligecf0c772009-11-24 16:53:58 +01001383 write_unlock_irqrestore(&adapter->erp_lock, flags);
Christof Schmitt287ac012008-07-02 10:56:40 +02001384
1385 if (retval != ZFCP_ERP_CONTINUES)
1386 zfcp_erp_action_cleanup(erp_action, retval);
1387
Swen Schilligf3450c72009-11-24 16:53:59 +01001388 kref_put(&adapter->ref, zfcp_adapter_release);
Christof Schmitt287ac012008-07-02 10:56:40 +02001389 return retval;
1390}
1391
1392static int zfcp_erp_thread(void *data)
1393{
1394 struct zfcp_adapter *adapter = (struct zfcp_adapter *) data;
1395 struct list_head *next;
1396 struct zfcp_erp_action *act;
1397 unsigned long flags;
1398
Christof Schmitt347c6a92009-08-18 15:43:25 +02001399 for (;;) {
Christof Schmitt347c6a92009-08-18 15:43:25 +02001400 wait_event_interruptible(adapter->erp_ready_wq,
1401 !list_empty(&adapter->erp_ready_head) ||
1402 kthread_should_stop());
Swen Schillig94ab4b32009-04-17 15:08:06 +02001403
Christof Schmitt347c6a92009-08-18 15:43:25 +02001404 if (kthread_should_stop())
1405 break;
1406
Christof Schmitt287ac012008-07-02 10:56:40 +02001407 write_lock_irqsave(&adapter->erp_lock, flags);
1408 next = adapter->erp_ready_head.next;
1409 write_unlock_irqrestore(&adapter->erp_lock, flags);
1410
1411 if (next != &adapter->erp_ready_head) {
1412 act = list_entry(next, struct zfcp_erp_action, list);
1413
1414 /* there is more to come after dismission, no notify */
1415 if (zfcp_erp_strategy(act) != ZFCP_ERP_DISMISSED)
1416 zfcp_erp_wakeup(adapter);
1417 }
Christof Schmitt287ac012008-07-02 10:56:40 +02001418 }
1419
Christof Schmitt287ac012008-07-02 10:56:40 +02001420 return 0;
1421}
1422
1423/**
1424 * zfcp_erp_thread_setup - Start ERP thread for adapter
1425 * @adapter: Adapter to start the ERP thread for
1426 *
1427 * Returns 0 on success or error code from kernel_thread()
1428 */
1429int zfcp_erp_thread_setup(struct zfcp_adapter *adapter)
1430{
Christof Schmitt347c6a92009-08-18 15:43:25 +02001431 struct task_struct *thread;
Christof Schmitt287ac012008-07-02 10:56:40 +02001432
Christof Schmitt347c6a92009-08-18 15:43:25 +02001433 thread = kthread_run(zfcp_erp_thread, adapter, "zfcperp%s",
1434 dev_name(&adapter->ccw_device->dev));
1435 if (IS_ERR(thread)) {
Christof Schmitt287ac012008-07-02 10:56:40 +02001436 dev_err(&adapter->ccw_device->dev,
Christof Schmittff3b24f2008-10-01 12:42:15 +02001437 "Creating an ERP thread for the FCP device failed.\n");
Christof Schmitt347c6a92009-08-18 15:43:25 +02001438 return PTR_ERR(thread);
Christof Schmitt287ac012008-07-02 10:56:40 +02001439 }
Christof Schmitt347c6a92009-08-18 15:43:25 +02001440
1441 adapter->erp_thread = thread;
Christof Schmitt287ac012008-07-02 10:56:40 +02001442 return 0;
1443}
1444
1445/**
1446 * zfcp_erp_thread_kill - Stop ERP thread.
1447 * @adapter: Adapter where the ERP thread should be stopped.
1448 *
1449 * The caller of this routine ensures that the specified adapter has
1450 * been shut down and that this operation has been completed. Thus,
1451 * there are no pending erp_actions which would need to be handled
1452 * here.
1453 */
1454void zfcp_erp_thread_kill(struct zfcp_adapter *adapter)
1455{
Christof Schmitt347c6a92009-08-18 15:43:25 +02001456 kthread_stop(adapter->erp_thread);
1457 adapter->erp_thread = NULL;
Christof Schmitt143bb6b2009-08-18 15:43:27 +02001458 WARN_ON(!list_empty(&adapter->erp_ready_head));
1459 WARN_ON(!list_empty(&adapter->erp_running_head));
Christof Schmitt287ac012008-07-02 10:56:40 +02001460}
1461
1462/**
Christof Schmitt287ac012008-07-02 10:56:40 +02001463 * zfcp_erp_wait - wait for completion of error recovery on an adapter
1464 * @adapter: adapter for which to wait for completion of its error recovery
1465 */
1466void zfcp_erp_wait(struct zfcp_adapter *adapter)
1467{
1468 wait_event(adapter->erp_done_wqh,
1469 !(atomic_read(&adapter->status) &
1470 ZFCP_STATUS_ADAPTER_ERP_PENDING));
1471}
1472
1473/**
Swen Schilligedaed852010-09-08 14:40:01 +02001474 * zfcp_erp_set_adapter_status - set adapter status bits
Christof Schmitt287ac012008-07-02 10:56:40 +02001475 * @adapter: adapter to change the status
Christof Schmitt287ac012008-07-02 10:56:40 +02001476 * @mask: status bits to change
Christof Schmitt287ac012008-07-02 10:56:40 +02001477 *
Christof Schmittb62a8d92010-09-08 14:39:55 +02001478 * Changes in common status bits are propagated to attached ports and LUNs.
Christof Schmitt287ac012008-07-02 10:56:40 +02001479 */
Swen Schilligedaed852010-09-08 14:40:01 +02001480void zfcp_erp_set_adapter_status(struct zfcp_adapter *adapter, u32 mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 struct zfcp_port *port;
Swen Schilligedaed852010-09-08 14:40:01 +02001483 struct scsi_device *sdev;
Swen Schilligecf0c772009-11-24 16:53:58 +01001484 unsigned long flags;
Christof Schmitt287ac012008-07-02 10:56:40 +02001485 u32 common_mask = mask & ZFCP_COMMON_FLAGS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001487 atomic_or(mask, &adapter->status);
Christof Schmitt287ac012008-07-02 10:56:40 +02001488
Swen Schilligedaed852010-09-08 14:40:01 +02001489 if (!common_mask)
1490 return;
1491
1492 read_lock_irqsave(&adapter->port_list_lock, flags);
1493 list_for_each_entry(port, &adapter->port_list, list)
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001494 atomic_or(common_mask, &port->status);
Swen Schilligedaed852010-09-08 14:40:01 +02001495 read_unlock_irqrestore(&adapter->port_list_lock, flags);
1496
Martin Peschke924dd582013-08-22 17:45:37 +02001497 spin_lock_irqsave(adapter->scsi_host->host_lock, flags);
1498 __shost_for_each_device(sdev, adapter->scsi_host)
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001499 atomic_or(common_mask, &sdev_to_zfcp(sdev)->status);
Martin Peschke924dd582013-08-22 17:45:37 +02001500 spin_unlock_irqrestore(adapter->scsi_host->host_lock, flags);
Swen Schilligedaed852010-09-08 14:40:01 +02001501}
1502
1503/**
1504 * zfcp_erp_clear_adapter_status - clear adapter status bits
1505 * @adapter: adapter to change the status
1506 * @mask: status bits to change
1507 *
1508 * Changes in common status bits are propagated to attached ports and LUNs.
1509 */
1510void zfcp_erp_clear_adapter_status(struct zfcp_adapter *adapter, u32 mask)
1511{
1512 struct zfcp_port *port;
1513 struct scsi_device *sdev;
1514 unsigned long flags;
1515 u32 common_mask = mask & ZFCP_COMMON_FLAGS;
1516 u32 clear_counter = mask & ZFCP_STATUS_COMMON_ERP_FAILED;
1517
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001518 atomic_andnot(mask, &adapter->status);
Swen Schilligedaed852010-09-08 14:40:01 +02001519
1520 if (!common_mask)
1521 return;
1522
1523 if (clear_counter)
1524 atomic_set(&adapter->erp_counter, 0);
1525
1526 read_lock_irqsave(&adapter->port_list_lock, flags);
1527 list_for_each_entry(port, &adapter->port_list, list) {
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001528 atomic_andnot(common_mask, &port->status);
Swen Schilligedaed852010-09-08 14:40:01 +02001529 if (clear_counter)
1530 atomic_set(&port->erp_counter, 0);
1531 }
1532 read_unlock_irqrestore(&adapter->port_list_lock, flags);
1533
Martin Peschke924dd582013-08-22 17:45:37 +02001534 spin_lock_irqsave(adapter->scsi_host->host_lock, flags);
1535 __shost_for_each_device(sdev, adapter->scsi_host) {
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001536 atomic_andnot(common_mask, &sdev_to_zfcp(sdev)->status);
Swen Schilligedaed852010-09-08 14:40:01 +02001537 if (clear_counter)
1538 atomic_set(&sdev_to_zfcp(sdev)->erp_counter, 0);
Swen Schilligecf0c772009-11-24 16:53:58 +01001539 }
Martin Peschke924dd582013-08-22 17:45:37 +02001540 spin_unlock_irqrestore(adapter->scsi_host->host_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541}
1542
Christof Schmitt287ac012008-07-02 10:56:40 +02001543/**
Swen Schilligedaed852010-09-08 14:40:01 +02001544 * zfcp_erp_set_port_status - set port status bits
1545 * @port: port to change the status
Christof Schmitt287ac012008-07-02 10:56:40 +02001546 * @mask: status bits to change
Christof Schmitt287ac012008-07-02 10:56:40 +02001547 *
Christof Schmittb62a8d92010-09-08 14:39:55 +02001548 * Changes in common status bits are propagated to attached LUNs.
Christof Schmitt287ac012008-07-02 10:56:40 +02001549 */
Swen Schilligedaed852010-09-08 14:40:01 +02001550void zfcp_erp_set_port_status(struct zfcp_port *port, u32 mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551{
Christof Schmittb62a8d92010-09-08 14:39:55 +02001552 struct scsi_device *sdev;
Christof Schmitt287ac012008-07-02 10:56:40 +02001553 u32 common_mask = mask & ZFCP_COMMON_FLAGS;
Martin Peschke924dd582013-08-22 17:45:37 +02001554 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001556 atomic_or(mask, &port->status);
Christof Schmitt287ac012008-07-02 10:56:40 +02001557
Swen Schilligedaed852010-09-08 14:40:01 +02001558 if (!common_mask)
1559 return;
1560
Martin Peschke924dd582013-08-22 17:45:37 +02001561 spin_lock_irqsave(port->adapter->scsi_host->host_lock, flags);
1562 __shost_for_each_device(sdev, port->adapter->scsi_host)
Swen Schilligedaed852010-09-08 14:40:01 +02001563 if (sdev_to_zfcp(sdev)->port == port)
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001564 atomic_or(common_mask,
Swen Schilligedaed852010-09-08 14:40:01 +02001565 &sdev_to_zfcp(sdev)->status);
Martin Peschke924dd582013-08-22 17:45:37 +02001566 spin_unlock_irqrestore(port->adapter->scsi_host->host_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567}
1568
Christof Schmitt287ac012008-07-02 10:56:40 +02001569/**
Swen Schilligedaed852010-09-08 14:40:01 +02001570 * zfcp_erp_clear_port_status - clear port status bits
1571 * @port: adapter to change the status
Christof Schmitt287ac012008-07-02 10:56:40 +02001572 * @mask: status bits to change
Swen Schilligedaed852010-09-08 14:40:01 +02001573 *
1574 * Changes in common status bits are propagated to attached LUNs.
Christof Schmitt287ac012008-07-02 10:56:40 +02001575 */
Swen Schilligedaed852010-09-08 14:40:01 +02001576void zfcp_erp_clear_port_status(struct zfcp_port *port, u32 mask)
1577{
1578 struct scsi_device *sdev;
1579 u32 common_mask = mask & ZFCP_COMMON_FLAGS;
1580 u32 clear_counter = mask & ZFCP_STATUS_COMMON_ERP_FAILED;
Martin Peschke924dd582013-08-22 17:45:37 +02001581 unsigned long flags;
Swen Schilligedaed852010-09-08 14:40:01 +02001582
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001583 atomic_andnot(mask, &port->status);
Swen Schilligedaed852010-09-08 14:40:01 +02001584
1585 if (!common_mask)
1586 return;
1587
1588 if (clear_counter)
1589 atomic_set(&port->erp_counter, 0);
1590
Martin Peschke924dd582013-08-22 17:45:37 +02001591 spin_lock_irqsave(port->adapter->scsi_host->host_lock, flags);
1592 __shost_for_each_device(sdev, port->adapter->scsi_host)
Swen Schilligedaed852010-09-08 14:40:01 +02001593 if (sdev_to_zfcp(sdev)->port == port) {
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001594 atomic_andnot(common_mask,
Swen Schilligedaed852010-09-08 14:40:01 +02001595 &sdev_to_zfcp(sdev)->status);
1596 if (clear_counter)
1597 atomic_set(&sdev_to_zfcp(sdev)->erp_counter, 0);
1598 }
Martin Peschke924dd582013-08-22 17:45:37 +02001599 spin_unlock_irqrestore(port->adapter->scsi_host->host_lock, flags);
Swen Schilligedaed852010-09-08 14:40:01 +02001600}
1601
1602/**
1603 * zfcp_erp_set_lun_status - set lun status bits
1604 * @sdev: SCSI device / lun to set the status bits
1605 * @mask: status bits to change
1606 */
1607void zfcp_erp_set_lun_status(struct scsi_device *sdev, u32 mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608{
Christof Schmittb62a8d92010-09-08 14:39:55 +02001609 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
1610
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001611 atomic_or(mask, &zfcp_sdev->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612}
1613
Christof Schmitt287ac012008-07-02 10:56:40 +02001614/**
Swen Schilligedaed852010-09-08 14:40:01 +02001615 * zfcp_erp_clear_lun_status - clear lun status bits
1616 * @sdev: SCSi device / lun to clear the status bits
1617 * @mask: status bits to change
Christof Schmitt287ac012008-07-02 10:56:40 +02001618 */
Swen Schilligedaed852010-09-08 14:40:01 +02001619void zfcp_erp_clear_lun_status(struct scsi_device *sdev, u32 mask)
Andreas Herrmannd736a27b2005-06-13 13:23:57 +02001620{
Swen Schilligedaed852010-09-08 14:40:01 +02001621 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
1622
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001623 atomic_andnot(mask, &zfcp_sdev->status);
Swen Schilligedaed852010-09-08 14:40:01 +02001624
1625 if (mask & ZFCP_STATUS_COMMON_ERP_FAILED)
1626 atomic_set(&zfcp_sdev->erp_counter, 0);
Andreas Herrmannd736a27b2005-06-13 13:23:57 +02001627}
1628