blob: 7ccfce55903423f5e998fe3d93f6942a5879376f [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 Maier2a940b82016-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 Schmittb6bd2fb92010-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 Schmittb6bd2fb92010-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 Schmittb6bd2fb92010-02-17 11:18:50 +0100537 spin_unlock(&adapter->req_list->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538}
539
Andreas Herrmannf6c0e7a2006-08-02 11:05:52 +0200540/**
Christof Schmitt287ac012008-07-02 10:56:40 +0200541 * zfcp_erp_notify - Trigger ERP action.
542 * @erp_action: ERP action to continue.
543 * @set_mask: ERP action status flags to set.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 */
Christof Schmitt287ac012008-07-02 10:56:40 +0200545void zfcp_erp_notify(struct zfcp_erp_action *erp_action, unsigned long set_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546{
547 struct zfcp_adapter *adapter = erp_action->adapter;
548 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
550 write_lock_irqsave(&adapter->erp_lock, flags);
Christof Schmitt287ac012008-07-02 10:56:40 +0200551 if (zfcp_erp_action_exists(erp_action) == ZFCP_ERP_ACTION_RUNNING) {
552 erp_action->status |= set_mask;
553 zfcp_erp_action_ready(erp_action);
554 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 write_unlock_irqrestore(&adapter->erp_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556}
557
Andreas Herrmannf6c0e7a2006-08-02 11:05:52 +0200558/**
Christof Schmitt287ac012008-07-02 10:56:40 +0200559 * zfcp_erp_timeout_handler - Trigger ERP action from timed out ERP request
560 * @data: ERP action (from timer data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 */
Christof Schmitt287ac012008-07-02 10:56:40 +0200562void zfcp_erp_timeout_handler(unsigned long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563{
Christof Schmitt287ac012008-07-02 10:56:40 +0200564 struct zfcp_erp_action *act = (struct zfcp_erp_action *) data;
565 zfcp_erp_notify(act, ZFCP_STATUS_ERP_TIMEDOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566}
567
Christof Schmitt287ac012008-07-02 10:56:40 +0200568static void zfcp_erp_memwait_handler(unsigned long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569{
Christof Schmitt287ac012008-07-02 10:56:40 +0200570 zfcp_erp_notify((struct zfcp_erp_action *)data, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571}
572
Christof Schmitt287ac012008-07-02 10:56:40 +0200573static void zfcp_erp_strategy_memwait(struct zfcp_erp_action *erp_action)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 init_timer(&erp_action->timer);
576 erp_action->timer.function = zfcp_erp_memwait_handler;
577 erp_action->timer.data = (unsigned long) erp_action;
Christof Schmitt287ac012008-07-02 10:56:40 +0200578 erp_action->timer.expires = jiffies + HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 add_timer(&erp_action->timer);
Christof Schmitt287ac012008-07-02 10:56:40 +0200580}
581
582static void _zfcp_erp_port_reopen_all(struct zfcp_adapter *adapter,
Swen Schilligea4a3a62010-12-02 15:16:16 +0100583 int clear, char *id)
Christof Schmitt287ac012008-07-02 10:56:40 +0200584{
585 struct zfcp_port *port;
586
Swen Schilligecf0c772009-11-24 16:53:58 +0100587 read_lock(&adapter->port_list_lock);
588 list_for_each_entry(port, &adapter->port_list, list)
Swen Schilligea4a3a62010-12-02 15:16:16 +0100589 _zfcp_erp_port_reopen(port, clear, id);
Swen Schilligecf0c772009-11-24 16:53:58 +0100590 read_unlock(&adapter->port_list_lock);
Christof Schmitt287ac012008-07-02 10:56:40 +0200591}
592
Christof Schmittb62a8d92010-09-08 14:39:55 +0200593static void _zfcp_erp_lun_reopen_all(struct zfcp_port *port, int clear,
Swen Schilligea4a3a62010-12-02 15:16:16 +0100594 char *id)
Christof Schmitt287ac012008-07-02 10:56:40 +0200595{
Christof Schmittb62a8d92010-09-08 14:39:55 +0200596 struct scsi_device *sdev;
Christof Schmitt287ac012008-07-02 10:56:40 +0200597
Martin Peschke924dd582013-08-22 17:45:37 +0200598 spin_lock(port->adapter->scsi_host->host_lock);
599 __shost_for_each_device(sdev, port->adapter->scsi_host)
Christof Schmittb62a8d92010-09-08 14:39:55 +0200600 if (sdev_to_zfcp(sdev)->port == port)
Swen Schilligea4a3a62010-12-02 15:16:16 +0100601 _zfcp_erp_lun_reopen(sdev, clear, id, 0);
Martin Peschke924dd582013-08-22 17:45:37 +0200602 spin_unlock(port->adapter->scsi_host->host_lock);
Christof Schmitt287ac012008-07-02 10:56:40 +0200603}
604
Christof Schmitt85600f72009-07-13 15:06:09 +0200605static void zfcp_erp_strategy_followup_failed(struct zfcp_erp_action *act)
Christof Schmitt287ac012008-07-02 10:56:40 +0200606{
Christof Schmitt287ac012008-07-02 10:56:40 +0200607 switch (act->action) {
Christof Schmitt287ac012008-07-02 10:56:40 +0200608 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
Swen Schilligea4a3a62010-12-02 15:16:16 +0100609 _zfcp_erp_adapter_reopen(act->adapter, 0, "ersff_1");
Christof Schmitt287ac012008-07-02 10:56:40 +0200610 break;
Christof Schmitt287ac012008-07-02 10:56:40 +0200611 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
Swen Schilligea4a3a62010-12-02 15:16:16 +0100612 _zfcp_erp_port_forced_reopen(act->port, 0, "ersff_2");
Christof Schmitt287ac012008-07-02 10:56:40 +0200613 break;
Christof Schmitt287ac012008-07-02 10:56:40 +0200614 case ZFCP_ERP_ACTION_REOPEN_PORT:
Swen Schilligea4a3a62010-12-02 15:16:16 +0100615 _zfcp_erp_port_reopen(act->port, 0, "ersff_3");
Christof Schmitt287ac012008-07-02 10:56:40 +0200616 break;
Christof Schmittb62a8d92010-09-08 14:39:55 +0200617 case ZFCP_ERP_ACTION_REOPEN_LUN:
Swen Schilligea4a3a62010-12-02 15:16:16 +0100618 _zfcp_erp_lun_reopen(act->sdev, 0, "ersff_4", 0);
Christof Schmitt85600f72009-07-13 15:06:09 +0200619 break;
620 }
621}
622
623static void zfcp_erp_strategy_followup_success(struct zfcp_erp_action *act)
624{
625 switch (act->action) {
626 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
Swen Schilligea4a3a62010-12-02 15:16:16 +0100627 _zfcp_erp_port_reopen_all(act->adapter, 0, "ersfs_1");
Christof Schmitt85600f72009-07-13 15:06:09 +0200628 break;
629 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
Swen Schilligea4a3a62010-12-02 15:16:16 +0100630 _zfcp_erp_port_reopen(act->port, 0, "ersfs_2");
Christof Schmitt85600f72009-07-13 15:06:09 +0200631 break;
632 case ZFCP_ERP_ACTION_REOPEN_PORT:
Swen Schilligea4a3a62010-12-02 15:16:16 +0100633 _zfcp_erp_lun_reopen_all(act->port, 0, "ersfs_3");
Christof Schmitt287ac012008-07-02 10:56:40 +0200634 break;
635 }
636}
637
638static void zfcp_erp_wakeup(struct zfcp_adapter *adapter)
639{
640 unsigned long flags;
641
Swen Schilligecf0c772009-11-24 16:53:58 +0100642 read_lock_irqsave(&adapter->erp_lock, flags);
Christof Schmitt287ac012008-07-02 10:56:40 +0200643 if (list_empty(&adapter->erp_ready_head) &&
644 list_empty(&adapter->erp_running_head)) {
Peter Zijlstra805de8f42015-04-24 01:12:32 +0200645 atomic_andnot(ZFCP_STATUS_ADAPTER_ERP_PENDING,
Christof Schmitt287ac012008-07-02 10:56:40 +0200646 &adapter->status);
647 wake_up(&adapter->erp_done_wqh);
648 }
Swen Schilligecf0c772009-11-24 16:53:58 +0100649 read_unlock_irqrestore(&adapter->erp_lock, flags);
Christof Schmitt287ac012008-07-02 10:56:40 +0200650}
651
Christof Schmitt287ac012008-07-02 10:56:40 +0200652static void zfcp_erp_enqueue_ptp_port(struct zfcp_adapter *adapter)
653{
654 struct zfcp_port *port;
655 port = zfcp_port_enqueue(adapter, adapter->peer_wwpn, 0,
656 adapter->peer_d_id);
657 if (IS_ERR(port)) /* error or port already attached */
658 return;
Swen Schilligea4a3a62010-12-02 15:16:16 +0100659 _zfcp_erp_port_reopen(port, 0, "ereptp1");
Christof Schmitt287ac012008-07-02 10:56:40 +0200660}
661
662static int zfcp_erp_adapter_strat_fsf_xconf(struct zfcp_erp_action *erp_action)
663{
664 int retries;
665 int sleep = 1;
666 struct zfcp_adapter *adapter = erp_action->adapter;
667
Peter Zijlstra805de8f42015-04-24 01:12:32 +0200668 atomic_andnot(ZFCP_STATUS_ADAPTER_XCONFIG_OK, &adapter->status);
Christof Schmitt287ac012008-07-02 10:56:40 +0200669
670 for (retries = 7; retries; retries--) {
Peter Zijlstra805de8f42015-04-24 01:12:32 +0200671 atomic_andnot(ZFCP_STATUS_ADAPTER_HOST_CON_INIT,
Christof Schmitt287ac012008-07-02 10:56:40 +0200672 &adapter->status);
673 write_lock_irq(&adapter->erp_lock);
674 zfcp_erp_action_to_running(erp_action);
675 write_unlock_irq(&adapter->erp_lock);
676 if (zfcp_fsf_exchange_config_data(erp_action)) {
Peter Zijlstra805de8f42015-04-24 01:12:32 +0200677 atomic_andnot(ZFCP_STATUS_ADAPTER_HOST_CON_INIT,
Christof Schmitt287ac012008-07-02 10:56:40 +0200678 &adapter->status);
679 return ZFCP_ERP_FAILED;
680 }
681
Christof Schmitt347c6a92009-08-18 15:43:25 +0200682 wait_event(adapter->erp_ready_wq,
683 !list_empty(&adapter->erp_ready_head));
Christof Schmitt287ac012008-07-02 10:56:40 +0200684 if (erp_action->status & ZFCP_STATUS_ERP_TIMEDOUT)
685 break;
686
687 if (!(atomic_read(&adapter->status) &
688 ZFCP_STATUS_ADAPTER_HOST_CON_INIT))
689 break;
690
691 ssleep(sleep);
692 sleep *= 2;
693 }
694
Peter Zijlstra805de8f42015-04-24 01:12:32 +0200695 atomic_andnot(ZFCP_STATUS_ADAPTER_HOST_CON_INIT,
Christof Schmitt287ac012008-07-02 10:56:40 +0200696 &adapter->status);
697
698 if (!(atomic_read(&adapter->status) & ZFCP_STATUS_ADAPTER_XCONFIG_OK))
699 return ZFCP_ERP_FAILED;
700
701 if (fc_host_port_type(adapter->scsi_host) == FC_PORTTYPE_PTP)
702 zfcp_erp_enqueue_ptp_port(adapter);
703
704 return ZFCP_ERP_SUCCEEDED;
705}
706
707static int zfcp_erp_adapter_strategy_open_fsf_xport(struct zfcp_erp_action *act)
708{
709 int ret;
710 struct zfcp_adapter *adapter = act->adapter;
711
Christof Schmitt287ac012008-07-02 10:56:40 +0200712 write_lock_irq(&adapter->erp_lock);
713 zfcp_erp_action_to_running(act);
714 write_unlock_irq(&adapter->erp_lock);
715
716 ret = zfcp_fsf_exchange_port_data(act);
717 if (ret == -EOPNOTSUPP)
718 return ZFCP_ERP_SUCCEEDED;
719 if (ret)
720 return ZFCP_ERP_FAILED;
721
Swen Schilligae0904f2010-12-02 15:16:12 +0100722 zfcp_dbf_rec_run("erasox1", act);
Christof Schmitt347c6a92009-08-18 15:43:25 +0200723 wait_event(adapter->erp_ready_wq,
724 !list_empty(&adapter->erp_ready_head));
Swen Schilligae0904f2010-12-02 15:16:12 +0100725 zfcp_dbf_rec_run("erasox2", act);
Christof Schmitt287ac012008-07-02 10:56:40 +0200726 if (act->status & ZFCP_STATUS_ERP_TIMEDOUT)
727 return ZFCP_ERP_FAILED;
728
729 return ZFCP_ERP_SUCCEEDED;
730}
731
732static int zfcp_erp_adapter_strategy_open_fsf(struct zfcp_erp_action *act)
733{
734 if (zfcp_erp_adapter_strat_fsf_xconf(act) == ZFCP_ERP_FAILED)
735 return ZFCP_ERP_FAILED;
736
737 if (zfcp_erp_adapter_strategy_open_fsf_xport(act) == ZFCP_ERP_FAILED)
738 return ZFCP_ERP_FAILED;
739
Christof Schmittc7b279a2011-02-22 19:54:40 +0100740 if (mempool_resize(act->adapter->pool.sr_data,
David Rientjes11d83362015-04-14 15:48:21 -0700741 act->adapter->stat_read_buf_num))
Christof Schmitt8d88cf32010-06-21 10:11:33 +0200742 return ZFCP_ERP_FAILED;
743
744 if (mempool_resize(act->adapter->pool.status_read_req,
David Rientjes11d83362015-04-14 15:48:21 -0700745 act->adapter->stat_read_buf_num))
Christof Schmitt8d88cf32010-06-21 10:11:33 +0200746 return ZFCP_ERP_FAILED;
747
Christof Schmitt64deb6e2010-04-30 18:09:36 +0200748 atomic_set(&act->adapter->stat_miss, act->adapter->stat_read_buf_num);
Christof Schmitt287ac012008-07-02 10:56:40 +0200749 if (zfcp_status_read_refill(act->adapter))
750 return ZFCP_ERP_FAILED;
751
752 return ZFCP_ERP_SUCCEEDED;
753}
754
Swen Schilligcf13c082009-03-02 13:09:03 +0100755static void zfcp_erp_adapter_strategy_close(struct zfcp_erp_action *act)
Christof Schmitt287ac012008-07-02 10:56:40 +0200756{
Christof Schmitt287ac012008-07-02 10:56:40 +0200757 struct zfcp_adapter *adapter = act->adapter;
758
Christof Schmitt287ac012008-07-02 10:56:40 +0200759 /* close queues to ensure that buffers are not accessed by adapter */
Swen Schillig564e1c82009-08-18 15:43:19 +0200760 zfcp_qdio_close(adapter->qdio);
Christof Schmitt287ac012008-07-02 10:56:40 +0200761 zfcp_fsf_req_dismiss_all(adapter);
762 adapter->fsf_req_seq_no = 0;
Christof Schmitt55c770f2009-08-18 15:43:12 +0200763 zfcp_fc_wka_ports_force_offline(adapter->gs);
Christof Schmittb62a8d92010-09-08 14:39:55 +0200764 /* all ports and LUNs are closed */
Swen Schilligedaed852010-09-08 14:40:01 +0200765 zfcp_erp_clear_adapter_status(adapter, ZFCP_STATUS_COMMON_OPEN);
Swen Schilligcf13c082009-03-02 13:09:03 +0100766
Peter Zijlstra805de8f42015-04-24 01:12:32 +0200767 atomic_andnot(ZFCP_STATUS_ADAPTER_XCONFIG_OK |
Swen Schilligcf13c082009-03-02 13:09:03 +0100768 ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED, &adapter->status);
769}
770
771static int zfcp_erp_adapter_strategy_open(struct zfcp_erp_action *act)
772{
773 struct zfcp_adapter *adapter = act->adapter;
774
Christof Schmitt3d63d3b2010-12-02 15:16:17 +0100775 if (zfcp_qdio_open(adapter->qdio)) {
Peter Zijlstra805de8f42015-04-24 01:12:32 +0200776 atomic_andnot(ZFCP_STATUS_ADAPTER_XCONFIG_OK |
Swen Schilligcf13c082009-03-02 13:09:03 +0100777 ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED,
778 &adapter->status);
779 return ZFCP_ERP_FAILED;
780 }
781
782 if (zfcp_erp_adapter_strategy_open_fsf(act)) {
783 zfcp_erp_adapter_strategy_close(act);
784 return ZFCP_ERP_FAILED;
785 }
786
Peter Zijlstra805de8f42015-04-24 01:12:32 +0200787 atomic_or(ZFCP_STATUS_COMMON_OPEN, &adapter->status);
Swen Schilligcf13c082009-03-02 13:09:03 +0100788
789 return ZFCP_ERP_SUCCEEDED;
Christof Schmitt287ac012008-07-02 10:56:40 +0200790}
791
792static int zfcp_erp_adapter_strategy(struct zfcp_erp_action *act)
793{
Swen Schilligcf13c082009-03-02 13:09:03 +0100794 struct zfcp_adapter *adapter = act->adapter;
Christof Schmitt287ac012008-07-02 10:56:40 +0200795
Swen Schilligcf13c082009-03-02 13:09:03 +0100796 if (atomic_read(&adapter->status) & ZFCP_STATUS_COMMON_OPEN) {
797 zfcp_erp_adapter_strategy_close(act);
798 if (act->status & ZFCP_STATUS_ERP_CLOSE_ONLY)
799 return ZFCP_ERP_EXIT;
800 }
Christof Schmitt287ac012008-07-02 10:56:40 +0200801
Swen Schilligcf13c082009-03-02 13:09:03 +0100802 if (zfcp_erp_adapter_strategy_open(act)) {
Christof Schmitt287ac012008-07-02 10:56:40 +0200803 ssleep(8);
Swen Schilligcf13c082009-03-02 13:09:03 +0100804 return ZFCP_ERP_FAILED;
805 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806
Swen Schilligcf13c082009-03-02 13:09:03 +0100807 return ZFCP_ERP_SUCCEEDED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808}
809
Christof Schmitt287ac012008-07-02 10:56:40 +0200810static int zfcp_erp_port_forced_strategy_close(struct zfcp_erp_action *act)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811{
Christof Schmitt287ac012008-07-02 10:56:40 +0200812 int retval;
813
814 retval = zfcp_fsf_close_physical_port(act);
815 if (retval == -ENOMEM)
816 return ZFCP_ERP_NOMEM;
817 act->step = ZFCP_ERP_STEP_PHYS_PORT_CLOSING;
818 if (retval)
819 return ZFCP_ERP_FAILED;
820
821 return ZFCP_ERP_CONTINUES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822}
823
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:
Christof Schmitt287ac012008-07-02 10:56:40 +0200831 if ((status & ZFCP_STATUS_PORT_PHYS_OPEN) &&
832 (status & ZFCP_STATUS_COMMON_OPEN))
833 return zfcp_erp_port_forced_strategy_close(erp_action);
834 else
835 return ZFCP_ERP_FAILED;
836
837 case ZFCP_ERP_STEP_PHYS_PORT_CLOSING:
Christof Schmittddb3e0c2009-07-13 15:06:08 +0200838 if (!(status & ZFCP_STATUS_PORT_PHYS_OPEN))
Christof Schmitt287ac012008-07-02 10:56:40 +0200839 return ZFCP_ERP_SUCCEEDED;
840 }
841 return ZFCP_ERP_FAILED;
842}
843
844static int zfcp_erp_port_strategy_close(struct zfcp_erp_action *erp_action)
845{
846 int retval;
847
848 retval = zfcp_fsf_close_port(erp_action);
849 if (retval == -ENOMEM)
850 return ZFCP_ERP_NOMEM;
851 erp_action->step = ZFCP_ERP_STEP_PORT_CLOSING;
852 if (retval)
853 return ZFCP_ERP_FAILED;
854 return ZFCP_ERP_CONTINUES;
855}
856
857static int zfcp_erp_port_strategy_open_port(struct zfcp_erp_action *erp_action)
858{
859 int retval;
860
861 retval = zfcp_fsf_open_port(erp_action);
862 if (retval == -ENOMEM)
863 return ZFCP_ERP_NOMEM;
864 erp_action->step = ZFCP_ERP_STEP_PORT_OPENING;
865 if (retval)
866 return ZFCP_ERP_FAILED;
867 return ZFCP_ERP_CONTINUES;
868}
869
Christof Schmitt287ac012008-07-02 10:56:40 +0200870static int zfcp_erp_open_ptp_port(struct zfcp_erp_action *act)
871{
872 struct zfcp_adapter *adapter = act->adapter;
873 struct zfcp_port *port = act->port;
874
875 if (port->wwpn != adapter->peer_wwpn) {
Swen Schilligedaed852010-09-08 14:40:01 +0200876 zfcp_erp_set_port_status(port, ZFCP_STATUS_COMMON_ERP_FAILED);
Christof Schmitt287ac012008-07-02 10:56:40 +0200877 return ZFCP_ERP_FAILED;
878 }
879 port->d_id = adapter->peer_d_id;
Christof Schmitt287ac012008-07-02 10:56:40 +0200880 return zfcp_erp_port_strategy_open_port(act);
881}
882
883static int zfcp_erp_port_strategy_open_common(struct zfcp_erp_action *act)
884{
885 struct zfcp_adapter *adapter = act->adapter;
886 struct zfcp_port *port = act->port;
Christof Schmitt287ac012008-07-02 10:56:40 +0200887 int p_status = atomic_read(&port->status);
888
889 switch (act->step) {
890 case ZFCP_ERP_STEP_UNINITIALIZED:
891 case ZFCP_ERP_STEP_PHYS_PORT_CLOSING:
892 case ZFCP_ERP_STEP_PORT_CLOSING:
893 if (fc_host_port_type(adapter->scsi_host) == FC_PORTTYPE_PTP)
894 return zfcp_erp_open_ptp_port(act);
Christof Schmittb98478d2008-12-19 16:56:59 +0100895 if (!port->d_id) {
Christof Schmitt934aeb5872009-10-14 11:00:43 +0200896 zfcp_fc_trigger_did_lookup(port);
Christof Schmitt799b76d2009-08-18 15:43:20 +0200897 return ZFCP_ERP_EXIT;
Christof Schmitt287ac012008-07-02 10:56:40 +0200898 }
Christof Schmitt287ac012008-07-02 10:56:40 +0200899 return zfcp_erp_port_strategy_open_port(act);
900
901 case ZFCP_ERP_STEP_PORT_OPENING:
902 /* D_ID might have changed during open */
Swen Schillig5ab944f2008-10-01 12:42:17 +0200903 if (p_status & ZFCP_STATUS_COMMON_OPEN) {
Christof Schmitt934aeb5872009-10-14 11:00:43 +0200904 if (!port->d_id) {
905 zfcp_fc_trigger_did_lookup(port);
906 return ZFCP_ERP_EXIT;
Swen Schillig5ab944f2008-10-01 12:42:17 +0200907 }
Christof Schmitt934aeb5872009-10-14 11:00:43 +0200908 return ZFCP_ERP_SUCCEEDED;
Swen Schillig5ab944f2008-10-01 12:42:17 +0200909 }
Swen Schilligea460a82009-05-15 13:18:20 +0200910 if (port->d_id && !(p_status & ZFCP_STATUS_COMMON_NOESC)) {
911 port->d_id = 0;
Christof Schmittf7bd7c32010-07-08 09:53:10 +0200912 return ZFCP_ERP_FAILED;
Swen Schilligea460a82009-05-15 13:18:20 +0200913 }
914 /* fall through otherwise */
Christof Schmitt287ac012008-07-02 10:56:40 +0200915 }
916 return ZFCP_ERP_FAILED;
917}
918
Christof Schmitt287ac012008-07-02 10:56:40 +0200919static int zfcp_erp_port_strategy(struct zfcp_erp_action *erp_action)
920{
921 struct zfcp_port *port = erp_action->port;
Christof Schmitt934aeb5872009-10-14 11:00:43 +0200922 int p_status = atomic_read(&port->status);
Christof Schmitt287ac012008-07-02 10:56:40 +0200923
Christof Schmitt934aeb5872009-10-14 11:00:43 +0200924 if ((p_status & ZFCP_STATUS_COMMON_NOESC) &&
925 !(p_status & ZFCP_STATUS_COMMON_OPEN))
Swen Schillig5ab944f2008-10-01 12:42:17 +0200926 goto close_init_done;
927
Christof Schmitt287ac012008-07-02 10:56:40 +0200928 switch (erp_action->step) {
929 case ZFCP_ERP_STEP_UNINITIALIZED:
Christof Schmitt934aeb5872009-10-14 11:00:43 +0200930 if (p_status & ZFCP_STATUS_COMMON_OPEN)
Christof Schmitt287ac012008-07-02 10:56:40 +0200931 return zfcp_erp_port_strategy_close(erp_action);
932 break;
933
934 case ZFCP_ERP_STEP_PORT_CLOSING:
Christof Schmitt934aeb5872009-10-14 11:00:43 +0200935 if (p_status & ZFCP_STATUS_COMMON_OPEN)
Christof Schmitt287ac012008-07-02 10:56:40 +0200936 return ZFCP_ERP_FAILED;
937 break;
938 }
Swen Schillig5ab944f2008-10-01 12:42:17 +0200939
940close_init_done:
Christof Schmitt287ac012008-07-02 10:56:40 +0200941 if (erp_action->status & ZFCP_STATUS_ERP_CLOSE_ONLY)
942 return ZFCP_ERP_EXIT;
Christof Schmitt287ac012008-07-02 10:56:40 +0200943
Swen Schillig5ab944f2008-10-01 12:42:17 +0200944 return zfcp_erp_port_strategy_open_common(erp_action);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945}
946
Christof Schmittb62a8d92010-09-08 14:39:55 +0200947static void zfcp_erp_lun_strategy_clearstati(struct scsi_device *sdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948{
Christof Schmittb62a8d92010-09-08 14:39:55 +0200949 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
950
Peter Zijlstra805de8f42015-04-24 01:12:32 +0200951 atomic_andnot(ZFCP_STATUS_COMMON_ACCESS_DENIED,
Christof Schmittb62a8d92010-09-08 14:39:55 +0200952 &zfcp_sdev->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953}
954
Christof Schmittb62a8d92010-09-08 14:39:55 +0200955static int zfcp_erp_lun_strategy_close(struct zfcp_erp_action *erp_action)
Christof Schmitt287ac012008-07-02 10:56:40 +0200956{
Christof Schmittb62a8d92010-09-08 14:39:55 +0200957 int retval = zfcp_fsf_close_lun(erp_action);
Christof Schmitt287ac012008-07-02 10:56:40 +0200958 if (retval == -ENOMEM)
959 return ZFCP_ERP_NOMEM;
Christof Schmittb62a8d92010-09-08 14:39:55 +0200960 erp_action->step = ZFCP_ERP_STEP_LUN_CLOSING;
Christof Schmitt287ac012008-07-02 10:56:40 +0200961 if (retval)
962 return ZFCP_ERP_FAILED;
963 return ZFCP_ERP_CONTINUES;
964}
965
Christof Schmittb62a8d92010-09-08 14:39:55 +0200966static int zfcp_erp_lun_strategy_open(struct zfcp_erp_action *erp_action)
Christof Schmitt287ac012008-07-02 10:56:40 +0200967{
Christof Schmittb62a8d92010-09-08 14:39:55 +0200968 int retval = zfcp_fsf_open_lun(erp_action);
Christof Schmitt287ac012008-07-02 10:56:40 +0200969 if (retval == -ENOMEM)
970 return ZFCP_ERP_NOMEM;
Christof Schmittb62a8d92010-09-08 14:39:55 +0200971 erp_action->step = ZFCP_ERP_STEP_LUN_OPENING;
Christof Schmitt287ac012008-07-02 10:56:40 +0200972 if (retval)
973 return ZFCP_ERP_FAILED;
974 return ZFCP_ERP_CONTINUES;
975}
976
Christof Schmittb62a8d92010-09-08 14:39:55 +0200977static int zfcp_erp_lun_strategy(struct zfcp_erp_action *erp_action)
Christof Schmitt287ac012008-07-02 10:56:40 +0200978{
Christof Schmittb62a8d92010-09-08 14:39:55 +0200979 struct scsi_device *sdev = erp_action->sdev;
980 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
Christof Schmitt287ac012008-07-02 10:56:40 +0200981
982 switch (erp_action->step) {
983 case ZFCP_ERP_STEP_UNINITIALIZED:
Christof Schmittb62a8d92010-09-08 14:39:55 +0200984 zfcp_erp_lun_strategy_clearstati(sdev);
985 if (atomic_read(&zfcp_sdev->status) & ZFCP_STATUS_COMMON_OPEN)
986 return zfcp_erp_lun_strategy_close(erp_action);
Christof Schmitt287ac012008-07-02 10:56:40 +0200987 /* already closed, fall through */
Christof Schmittb62a8d92010-09-08 14:39:55 +0200988 case ZFCP_ERP_STEP_LUN_CLOSING:
989 if (atomic_read(&zfcp_sdev->status) & ZFCP_STATUS_COMMON_OPEN)
Christof Schmitt287ac012008-07-02 10:56:40 +0200990 return ZFCP_ERP_FAILED;
991 if (erp_action->status & ZFCP_STATUS_ERP_CLOSE_ONLY)
992 return ZFCP_ERP_EXIT;
Christof Schmittb62a8d92010-09-08 14:39:55 +0200993 return zfcp_erp_lun_strategy_open(erp_action);
Christof Schmitt287ac012008-07-02 10:56:40 +0200994
Christof Schmittb62a8d92010-09-08 14:39:55 +0200995 case ZFCP_ERP_STEP_LUN_OPENING:
996 if (atomic_read(&zfcp_sdev->status) & ZFCP_STATUS_COMMON_OPEN)
Christof Schmitt287ac012008-07-02 10:56:40 +0200997 return ZFCP_ERP_SUCCEEDED;
998 }
999 return ZFCP_ERP_FAILED;
1000}
1001
Christof Schmittb62a8d92010-09-08 14:39:55 +02001002static int zfcp_erp_strategy_check_lun(struct scsi_device *sdev, int result)
Christof Schmitt287ac012008-07-02 10:56:40 +02001003{
Christof Schmittb62a8d92010-09-08 14:39:55 +02001004 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
1005
Christof Schmitt287ac012008-07-02 10:56:40 +02001006 switch (result) {
1007 case ZFCP_ERP_SUCCEEDED :
Christof Schmittb62a8d92010-09-08 14:39:55 +02001008 atomic_set(&zfcp_sdev->erp_counter, 0);
1009 zfcp_erp_lun_unblock(sdev);
Christof Schmitt287ac012008-07-02 10:56:40 +02001010 break;
1011 case ZFCP_ERP_FAILED :
Christof Schmittb62a8d92010-09-08 14:39:55 +02001012 atomic_inc(&zfcp_sdev->erp_counter);
1013 if (atomic_read(&zfcp_sdev->erp_counter) > ZFCP_MAX_ERPS) {
1014 dev_err(&zfcp_sdev->port->adapter->ccw_device->dev,
1015 "ERP failed for LUN 0x%016Lx on "
Christof Schmittff3b24f2008-10-01 12:42:15 +02001016 "port 0x%016Lx\n",
Christof Schmittb62a8d92010-09-08 14:39:55 +02001017 (unsigned long long)zfcp_scsi_dev_lun(sdev),
1018 (unsigned long long)zfcp_sdev->port->wwpn);
Swen Schilligedaed852010-09-08 14:40:01 +02001019 zfcp_erp_set_lun_status(sdev,
1020 ZFCP_STATUS_COMMON_ERP_FAILED);
Christof Schmittff3b24f2008-10-01 12:42:15 +02001021 }
Christof Schmitt287ac012008-07-02 10:56:40 +02001022 break;
1023 }
1024
Christof Schmittb62a8d92010-09-08 14:39:55 +02001025 if (atomic_read(&zfcp_sdev->status) & ZFCP_STATUS_COMMON_ERP_FAILED) {
1026 zfcp_erp_lun_block(sdev, 0);
Christof Schmitt287ac012008-07-02 10:56:40 +02001027 result = ZFCP_ERP_EXIT;
1028 }
1029 return result;
1030}
1031
1032static int zfcp_erp_strategy_check_port(struct zfcp_port *port, int result)
1033{
1034 switch (result) {
1035 case ZFCP_ERP_SUCCEEDED :
1036 atomic_set(&port->erp_counter, 0);
1037 zfcp_erp_port_unblock(port);
1038 break;
1039
1040 case ZFCP_ERP_FAILED :
1041 if (atomic_read(&port->status) & ZFCP_STATUS_COMMON_NOESC) {
1042 zfcp_erp_port_block(port, 0);
1043 result = ZFCP_ERP_EXIT;
1044 }
1045 atomic_inc(&port->erp_counter);
Christof Schmittff3b24f2008-10-01 12:42:15 +02001046 if (atomic_read(&port->erp_counter) > ZFCP_MAX_ERPS) {
1047 dev_err(&port->adapter->ccw_device->dev,
1048 "ERP failed for remote port 0x%016Lx\n",
Swen Schillig7ba58c92008-10-01 12:42:18 +02001049 (unsigned long long)port->wwpn);
Swen Schilligedaed852010-09-08 14:40:01 +02001050 zfcp_erp_set_port_status(port,
1051 ZFCP_STATUS_COMMON_ERP_FAILED);
Christof Schmittff3b24f2008-10-01 12:42:15 +02001052 }
Christof Schmitt287ac012008-07-02 10:56:40 +02001053 break;
1054 }
1055
1056 if (atomic_read(&port->status) & ZFCP_STATUS_COMMON_ERP_FAILED) {
1057 zfcp_erp_port_block(port, 0);
1058 result = ZFCP_ERP_EXIT;
1059 }
1060 return result;
1061}
1062
1063static int zfcp_erp_strategy_check_adapter(struct zfcp_adapter *adapter,
1064 int result)
1065{
1066 switch (result) {
1067 case ZFCP_ERP_SUCCEEDED :
1068 atomic_set(&adapter->erp_counter, 0);
1069 zfcp_erp_adapter_unblock(adapter);
1070 break;
1071
1072 case ZFCP_ERP_FAILED :
1073 atomic_inc(&adapter->erp_counter);
Christof Schmittff3b24f2008-10-01 12:42:15 +02001074 if (atomic_read(&adapter->erp_counter) > ZFCP_MAX_ERPS) {
1075 dev_err(&adapter->ccw_device->dev,
1076 "ERP cannot recover an error "
1077 "on the FCP device\n");
Swen Schilligedaed852010-09-08 14:40:01 +02001078 zfcp_erp_set_adapter_status(adapter,
1079 ZFCP_STATUS_COMMON_ERP_FAILED);
Christof Schmittff3b24f2008-10-01 12:42:15 +02001080 }
Christof Schmitt287ac012008-07-02 10:56:40 +02001081 break;
1082 }
1083
1084 if (atomic_read(&adapter->status) & ZFCP_STATUS_COMMON_ERP_FAILED) {
1085 zfcp_erp_adapter_block(adapter, 0);
1086 result = ZFCP_ERP_EXIT;
1087 }
1088 return result;
1089}
1090
1091static int zfcp_erp_strategy_check_target(struct zfcp_erp_action *erp_action,
1092 int result)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093{
1094 struct zfcp_adapter *adapter = erp_action->adapter;
1095 struct zfcp_port *port = erp_action->port;
Christof Schmittb62a8d92010-09-08 14:39:55 +02001096 struct scsi_device *sdev = erp_action->sdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 switch (erp_action->action) {
1099
Christof Schmittb62a8d92010-09-08 14:39:55 +02001100 case ZFCP_ERP_ACTION_REOPEN_LUN:
1101 result = zfcp_erp_strategy_check_lun(sdev, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 break;
1103
1104 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
1105 case ZFCP_ERP_ACTION_REOPEN_PORT:
1106 result = zfcp_erp_strategy_check_port(port, result);
1107 break;
1108
1109 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
1110 result = zfcp_erp_strategy_check_adapter(adapter, result);
1111 break;
1112 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 return result;
1114}
1115
Christof Schmitt287ac012008-07-02 10:56:40 +02001116static int zfcp_erp_strat_change_det(atomic_t *target_status, u32 erp_status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117{
Christof Schmitt287ac012008-07-02 10:56:40 +02001118 int status = atomic_read(target_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119
Christof Schmitt287ac012008-07-02 10:56:40 +02001120 if ((status & ZFCP_STATUS_COMMON_RUNNING) &&
1121 (erp_status & ZFCP_STATUS_ERP_CLOSE_ONLY))
1122 return 1; /* take it online */
1123
1124 if (!(status & ZFCP_STATUS_COMMON_RUNNING) &&
1125 !(erp_status & ZFCP_STATUS_ERP_CLOSE_ONLY))
1126 return 1; /* take it offline */
1127
1128 return 0;
1129}
1130
1131static int zfcp_erp_strategy_statechange(struct zfcp_erp_action *act, int ret)
1132{
1133 int action = act->action;
1134 struct zfcp_adapter *adapter = act->adapter;
1135 struct zfcp_port *port = act->port;
Christof Schmittb62a8d92010-09-08 14:39:55 +02001136 struct scsi_device *sdev = act->sdev;
1137 struct zfcp_scsi_dev *zfcp_sdev;
Christof Schmitt287ac012008-07-02 10:56:40 +02001138 u32 erp_status = act->status;
1139
1140 switch (action) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
Christof Schmitt287ac012008-07-02 10:56:40 +02001142 if (zfcp_erp_strat_change_det(&adapter->status, erp_status)) {
1143 _zfcp_erp_adapter_reopen(adapter,
1144 ZFCP_STATUS_COMMON_ERP_FAILED,
Swen Schilligea4a3a62010-12-02 15:16:16 +01001145 "ersscg1");
Christof Schmitt287ac012008-07-02 10:56:40 +02001146 return ZFCP_ERP_EXIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 }
1148 break;
1149
1150 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
1151 case ZFCP_ERP_ACTION_REOPEN_PORT:
Christof Schmitt287ac012008-07-02 10:56:40 +02001152 if (zfcp_erp_strat_change_det(&port->status, erp_status)) {
1153 _zfcp_erp_port_reopen(port,
1154 ZFCP_STATUS_COMMON_ERP_FAILED,
Swen Schilligea4a3a62010-12-02 15:16:16 +01001155 "ersscg2");
Christof Schmitt287ac012008-07-02 10:56:40 +02001156 return ZFCP_ERP_EXIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 }
1158 break;
1159
Christof Schmittb62a8d92010-09-08 14:39:55 +02001160 case ZFCP_ERP_ACTION_REOPEN_LUN:
1161 zfcp_sdev = sdev_to_zfcp(sdev);
1162 if (zfcp_erp_strat_change_det(&zfcp_sdev->status, erp_status)) {
1163 _zfcp_erp_lun_reopen(sdev,
1164 ZFCP_STATUS_COMMON_ERP_FAILED,
Swen Schilligea4a3a62010-12-02 15:16:16 +01001165 "ersscg3", 0);
Christof Schmitt287ac012008-07-02 10:56:40 +02001166 return ZFCP_ERP_EXIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 }
1168 break;
1169 }
Christof Schmitt287ac012008-07-02 10:56:40 +02001170 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171}
1172
Christof Schmitt287ac012008-07-02 10:56:40 +02001173static void zfcp_erp_action_dequeue(struct zfcp_erp_action *erp_action)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174{
Christof Schmitt287ac012008-07-02 10:56:40 +02001175 struct zfcp_adapter *adapter = erp_action->adapter;
Christof Schmittb62a8d92010-09-08 14:39:55 +02001176 struct zfcp_scsi_dev *zfcp_sdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177
Christof Schmitt287ac012008-07-02 10:56:40 +02001178 adapter->erp_total_count--;
1179 if (erp_action->status & ZFCP_STATUS_ERP_LOWMEM) {
1180 adapter->erp_low_mem_count--;
1181 erp_action->status &= ~ZFCP_STATUS_ERP_LOWMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 }
1183
Christof Schmitt287ac012008-07-02 10:56:40 +02001184 list_del(&erp_action->list);
Swen Schilligae0904f2010-12-02 15:16:12 +01001185 zfcp_dbf_rec_run("eractd1", erp_action);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186
Christof Schmitt287ac012008-07-02 10:56:40 +02001187 switch (erp_action->action) {
Christof Schmittb62a8d92010-09-08 14:39:55 +02001188 case ZFCP_ERP_ACTION_REOPEN_LUN:
1189 zfcp_sdev = sdev_to_zfcp(erp_action->sdev);
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001190 atomic_andnot(ZFCP_STATUS_COMMON_ERP_INUSE,
Christof Schmittb62a8d92010-09-08 14:39:55 +02001191 &zfcp_sdev->status);
Christof Schmitt287ac012008-07-02 10:56:40 +02001192 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193
Christof Schmitt287ac012008-07-02 10:56:40 +02001194 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
1195 case ZFCP_ERP_ACTION_REOPEN_PORT:
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001196 atomic_andnot(ZFCP_STATUS_COMMON_ERP_INUSE,
Christof Schmitt287ac012008-07-02 10:56:40 +02001197 &erp_action->port->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 break;
Christof Schmitt287ac012008-07-02 10:56:40 +02001199
1200 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001201 atomic_andnot(ZFCP_STATUS_COMMON_ERP_INUSE,
Christof Schmitt287ac012008-07-02 10:56:40 +02001202 &erp_action->adapter->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 break;
1204 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205}
1206
Steffen Maier2a940b82016-12-09 17:16:33 +01001207/**
1208 * zfcp_erp_try_rport_unblock - unblock rport if no more/new recovery
1209 * @port: zfcp_port whose fc_rport we should try to unblock
1210 */
1211static void zfcp_erp_try_rport_unblock(struct zfcp_port *port)
1212{
1213 unsigned long flags;
1214 struct zfcp_adapter *adapter = port->adapter;
1215 int port_status;
1216 struct Scsi_Host *shost = adapter->scsi_host;
1217 struct scsi_device *sdev;
1218
1219 write_lock_irqsave(&adapter->erp_lock, flags);
1220 port_status = atomic_read(&port->status);
1221 if ((port_status & ZFCP_STATUS_COMMON_UNBLOCKED) == 0 ||
1222 (port_status & (ZFCP_STATUS_COMMON_ERP_INUSE |
1223 ZFCP_STATUS_COMMON_ERP_FAILED)) != 0) {
1224 /* new ERP of severity >= port triggered elsewhere meanwhile or
1225 * local link down (adapter erp_failed but not clear unblock)
1226 */
1227 zfcp_dbf_rec_run_lvl(4, "ertru_p", &port->erp_action);
1228 write_unlock_irqrestore(&adapter->erp_lock, flags);
1229 return;
1230 }
1231 spin_lock(shost->host_lock);
1232 __shost_for_each_device(sdev, shost) {
1233 struct zfcp_scsi_dev *zsdev = sdev_to_zfcp(sdev);
1234 int lun_status;
1235
1236 if (zsdev->port != port)
1237 continue;
1238 /* LUN under port of interest */
1239 lun_status = atomic_read(&zsdev->status);
1240 if ((lun_status & ZFCP_STATUS_COMMON_ERP_FAILED) != 0)
1241 continue; /* unblock rport despite failed LUNs */
1242 /* LUN recovery not given up yet [maybe follow-up pending] */
1243 if ((lun_status & ZFCP_STATUS_COMMON_UNBLOCKED) == 0 ||
1244 (lun_status & ZFCP_STATUS_COMMON_ERP_INUSE) != 0) {
1245 /* LUN blocked:
1246 * not yet unblocked [LUN recovery pending]
1247 * or meanwhile blocked [new LUN recovery triggered]
1248 */
1249 zfcp_dbf_rec_run_lvl(4, "ertru_l", &zsdev->erp_action);
1250 spin_unlock(shost->host_lock);
1251 write_unlock_irqrestore(&adapter->erp_lock, flags);
1252 return;
1253 }
1254 }
1255 /* now port has no child or all children have completed recovery,
1256 * and no ERP of severity >= port was meanwhile triggered elsewhere
1257 */
1258 zfcp_scsi_schedule_rport_register(port);
1259 spin_unlock(shost->host_lock);
1260 write_unlock_irqrestore(&adapter->erp_lock, flags);
1261}
1262
Christof Schmitt287ac012008-07-02 10:56:40 +02001263static void zfcp_erp_action_cleanup(struct zfcp_erp_action *act, int result)
Maxim Shchetyninaef4a982005-09-13 21:51:16 +02001264{
Christof Schmitt287ac012008-07-02 10:56:40 +02001265 struct zfcp_adapter *adapter = act->adapter;
1266 struct zfcp_port *port = act->port;
Christof Schmittb62a8d92010-09-08 14:39:55 +02001267 struct scsi_device *sdev = act->sdev;
Maxim Shchetyninaef4a982005-09-13 21:51:16 +02001268
Christof Schmitt287ac012008-07-02 10:56:40 +02001269 switch (act->action) {
Christof Schmittb62a8d92010-09-08 14:39:55 +02001270 case ZFCP_ERP_ACTION_REOPEN_LUN:
Christof Schmittfdbd1c52010-09-08 14:39:54 +02001271 if (!(act->status & ZFCP_STATUS_ERP_NO_REF))
Christof Schmittb62a8d92010-09-08 14:39:55 +02001272 scsi_device_put(sdev);
Steffen Maier2a940b82016-12-09 17:16:33 +01001273 zfcp_erp_try_rport_unblock(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 break;
Christof Schmitt287ac012008-07-02 10:56:40 +02001275
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 case ZFCP_ERP_ACTION_REOPEN_PORT:
Steffen Maier4eeaa4f2016-08-10 18:30:46 +02001277 /* This switch case might also happen after a forced reopen
1278 * was successfully done and thus overwritten with a new
1279 * non-forced reopen at `ersfs_2'. In this case, we must not
1280 * do the clean-up of the non-forced version.
1281 */
1282 if (act->step != ZFCP_ERP_STEP_UNINITIALIZED)
1283 if (result == ZFCP_ERP_SUCCEEDED)
Steffen Maier2a940b82016-12-09 17:16:33 +01001284 zfcp_erp_try_rport_unblock(port);
Christof Schmitt57676202010-07-08 09:53:05 +02001285 /* fall through */
1286 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
Christof Schmitt615f59e2010-02-17 11:18:56 +01001287 put_device(&port->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 break;
Christof Schmitt287ac012008-07-02 10:56:40 +02001289
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
Christof Schmitta2fa0ae2009-03-02 13:09:08 +01001291 if (result == ZFCP_ERP_SUCCEEDED) {
Christof Schmittbd43a42b72008-12-25 13:38:50 +01001292 register_service_level(&adapter->service_level);
Steffen Maier43f60cb2012-09-04 15:23:35 +02001293 zfcp_fc_conditional_port_scan(adapter);
Christof Schmitt038d9442011-02-22 19:54:48 +01001294 queue_work(adapter->work_queue, &adapter->ns_up_work);
Christof Schmitta2fa0ae2009-03-02 13:09:08 +01001295 } else
1296 unregister_service_level(&adapter->service_level);
Christof Schmitt038d9442011-02-22 19:54:48 +01001297
Swen Schilligf3450c72009-11-24 16:53:59 +01001298 kref_put(&adapter->ref, zfcp_adapter_release);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 }
1301}
1302
Christof Schmitt287ac012008-07-02 10:56:40 +02001303static int zfcp_erp_strategy_do_action(struct zfcp_erp_action *erp_action)
1304{
1305 switch (erp_action->action) {
1306 case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
1307 return zfcp_erp_adapter_strategy(erp_action);
1308 case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
1309 return zfcp_erp_port_forced_strategy(erp_action);
1310 case ZFCP_ERP_ACTION_REOPEN_PORT:
1311 return zfcp_erp_port_strategy(erp_action);
Christof Schmittb62a8d92010-09-08 14:39:55 +02001312 case ZFCP_ERP_ACTION_REOPEN_LUN:
1313 return zfcp_erp_lun_strategy(erp_action);
Christof Schmitt287ac012008-07-02 10:56:40 +02001314 }
1315 return ZFCP_ERP_FAILED;
1316}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317
Christof Schmitt287ac012008-07-02 10:56:40 +02001318static int zfcp_erp_strategy(struct zfcp_erp_action *erp_action)
1319{
1320 int retval;
Christof Schmitt287ac012008-07-02 10:56:40 +02001321 unsigned long flags;
Swen Schilligecf0c772009-11-24 16:53:58 +01001322 struct zfcp_adapter *adapter = erp_action->adapter;
Christof Schmitt287ac012008-07-02 10:56:40 +02001323
Swen Schilligf3450c72009-11-24 16:53:59 +01001324 kref_get(&adapter->ref);
Christof Schmitt287ac012008-07-02 10:56:40 +02001325
Swen Schilligf3450c72009-11-24 16:53:59 +01001326 write_lock_irqsave(&adapter->erp_lock, flags);
Christof Schmitt287ac012008-07-02 10:56:40 +02001327 zfcp_erp_strategy_check_fsfreq(erp_action);
1328
1329 if (erp_action->status & ZFCP_STATUS_ERP_DISMISSED) {
1330 zfcp_erp_action_dequeue(erp_action);
1331 retval = ZFCP_ERP_DISMISSED;
1332 goto unlock;
1333 }
1334
Christof Schmitt9c785d92010-07-08 09:53:09 +02001335 if (erp_action->status & ZFCP_STATUS_ERP_TIMEDOUT) {
1336 retval = ZFCP_ERP_FAILED;
1337 goto check_target;
1338 }
1339
Christof Schmitt287ac012008-07-02 10:56:40 +02001340 zfcp_erp_action_to_running(erp_action);
1341
1342 /* no lock to allow for blocking operations */
Swen Schilligecf0c772009-11-24 16:53:58 +01001343 write_unlock_irqrestore(&adapter->erp_lock, flags);
Christof Schmitt287ac012008-07-02 10:56:40 +02001344 retval = zfcp_erp_strategy_do_action(erp_action);
Swen Schilligecf0c772009-11-24 16:53:58 +01001345 write_lock_irqsave(&adapter->erp_lock, flags);
Christof Schmitt287ac012008-07-02 10:56:40 +02001346
1347 if (erp_action->status & ZFCP_STATUS_ERP_DISMISSED)
1348 retval = ZFCP_ERP_CONTINUES;
1349
1350 switch (retval) {
1351 case ZFCP_ERP_NOMEM:
1352 if (!(erp_action->status & ZFCP_STATUS_ERP_LOWMEM)) {
1353 ++adapter->erp_low_mem_count;
1354 erp_action->status |= ZFCP_STATUS_ERP_LOWMEM;
1355 }
1356 if (adapter->erp_total_count == adapter->erp_low_mem_count)
Swen Schilligea4a3a62010-12-02 15:16:16 +01001357 _zfcp_erp_adapter_reopen(adapter, 0, "erstgy1");
Christof Schmitt287ac012008-07-02 10:56:40 +02001358 else {
1359 zfcp_erp_strategy_memwait(erp_action);
1360 retval = ZFCP_ERP_CONTINUES;
1361 }
1362 goto unlock;
1363
1364 case ZFCP_ERP_CONTINUES:
1365 if (erp_action->status & ZFCP_STATUS_ERP_LOWMEM) {
1366 --adapter->erp_low_mem_count;
1367 erp_action->status &= ~ZFCP_STATUS_ERP_LOWMEM;
1368 }
1369 goto unlock;
1370 }
1371
Christof Schmitt9c785d92010-07-08 09:53:09 +02001372check_target:
Christof Schmitt287ac012008-07-02 10:56:40 +02001373 retval = zfcp_erp_strategy_check_target(erp_action, retval);
1374 zfcp_erp_action_dequeue(erp_action);
1375 retval = zfcp_erp_strategy_statechange(erp_action, retval);
1376 if (retval == ZFCP_ERP_EXIT)
1377 goto unlock;
Christof Schmitt85600f72009-07-13 15:06:09 +02001378 if (retval == ZFCP_ERP_SUCCEEDED)
1379 zfcp_erp_strategy_followup_success(erp_action);
1380 if (retval == ZFCP_ERP_FAILED)
1381 zfcp_erp_strategy_followup_failed(erp_action);
Christof Schmitt287ac012008-07-02 10:56:40 +02001382
1383 unlock:
Swen Schilligecf0c772009-11-24 16:53:58 +01001384 write_unlock_irqrestore(&adapter->erp_lock, flags);
Christof Schmitt287ac012008-07-02 10:56:40 +02001385
1386 if (retval != ZFCP_ERP_CONTINUES)
1387 zfcp_erp_action_cleanup(erp_action, retval);
1388
Swen Schilligf3450c72009-11-24 16:53:59 +01001389 kref_put(&adapter->ref, zfcp_adapter_release);
Christof Schmitt287ac012008-07-02 10:56:40 +02001390 return retval;
1391}
1392
1393static int zfcp_erp_thread(void *data)
1394{
1395 struct zfcp_adapter *adapter = (struct zfcp_adapter *) data;
1396 struct list_head *next;
1397 struct zfcp_erp_action *act;
1398 unsigned long flags;
1399
Christof Schmitt347c6a92009-08-18 15:43:25 +02001400 for (;;) {
Christof Schmitt347c6a92009-08-18 15:43:25 +02001401 wait_event_interruptible(adapter->erp_ready_wq,
1402 !list_empty(&adapter->erp_ready_head) ||
1403 kthread_should_stop());
Swen Schillig94ab4b32009-04-17 15:08:06 +02001404
Christof Schmitt347c6a92009-08-18 15:43:25 +02001405 if (kthread_should_stop())
1406 break;
1407
Christof Schmitt287ac012008-07-02 10:56:40 +02001408 write_lock_irqsave(&adapter->erp_lock, flags);
1409 next = adapter->erp_ready_head.next;
1410 write_unlock_irqrestore(&adapter->erp_lock, flags);
1411
1412 if (next != &adapter->erp_ready_head) {
1413 act = list_entry(next, struct zfcp_erp_action, list);
1414
1415 /* there is more to come after dismission, no notify */
1416 if (zfcp_erp_strategy(act) != ZFCP_ERP_DISMISSED)
1417 zfcp_erp_wakeup(adapter);
1418 }
Christof Schmitt287ac012008-07-02 10:56:40 +02001419 }
1420
Christof Schmitt287ac012008-07-02 10:56:40 +02001421 return 0;
1422}
1423
1424/**
1425 * zfcp_erp_thread_setup - Start ERP thread for adapter
1426 * @adapter: Adapter to start the ERP thread for
1427 *
1428 * Returns 0 on success or error code from kernel_thread()
1429 */
1430int zfcp_erp_thread_setup(struct zfcp_adapter *adapter)
1431{
Christof Schmitt347c6a92009-08-18 15:43:25 +02001432 struct task_struct *thread;
Christof Schmitt287ac012008-07-02 10:56:40 +02001433
Christof Schmitt347c6a92009-08-18 15:43:25 +02001434 thread = kthread_run(zfcp_erp_thread, adapter, "zfcperp%s",
1435 dev_name(&adapter->ccw_device->dev));
1436 if (IS_ERR(thread)) {
Christof Schmitt287ac012008-07-02 10:56:40 +02001437 dev_err(&adapter->ccw_device->dev,
Christof Schmittff3b24f2008-10-01 12:42:15 +02001438 "Creating an ERP thread for the FCP device failed.\n");
Christof Schmitt347c6a92009-08-18 15:43:25 +02001439 return PTR_ERR(thread);
Christof Schmitt287ac012008-07-02 10:56:40 +02001440 }
Christof Schmitt347c6a92009-08-18 15:43:25 +02001441
1442 adapter->erp_thread = thread;
Christof Schmitt287ac012008-07-02 10:56:40 +02001443 return 0;
1444}
1445
1446/**
1447 * zfcp_erp_thread_kill - Stop ERP thread.
1448 * @adapter: Adapter where the ERP thread should be stopped.
1449 *
1450 * The caller of this routine ensures that the specified adapter has
1451 * been shut down and that this operation has been completed. Thus,
1452 * there are no pending erp_actions which would need to be handled
1453 * here.
1454 */
1455void zfcp_erp_thread_kill(struct zfcp_adapter *adapter)
1456{
Christof Schmitt347c6a92009-08-18 15:43:25 +02001457 kthread_stop(adapter->erp_thread);
1458 adapter->erp_thread = NULL;
Christof Schmitt143bb6b2009-08-18 15:43:27 +02001459 WARN_ON(!list_empty(&adapter->erp_ready_head));
1460 WARN_ON(!list_empty(&adapter->erp_running_head));
Christof Schmitt287ac012008-07-02 10:56:40 +02001461}
1462
1463/**
Christof Schmitt287ac012008-07-02 10:56:40 +02001464 * zfcp_erp_wait - wait for completion of error recovery on an adapter
1465 * @adapter: adapter for which to wait for completion of its error recovery
1466 */
1467void zfcp_erp_wait(struct zfcp_adapter *adapter)
1468{
1469 wait_event(adapter->erp_done_wqh,
1470 !(atomic_read(&adapter->status) &
1471 ZFCP_STATUS_ADAPTER_ERP_PENDING));
1472}
1473
1474/**
Swen Schilligedaed852010-09-08 14:40:01 +02001475 * zfcp_erp_set_adapter_status - set adapter status bits
Christof Schmitt287ac012008-07-02 10:56:40 +02001476 * @adapter: adapter to change the status
Christof Schmitt287ac012008-07-02 10:56:40 +02001477 * @mask: status bits to change
Christof Schmitt287ac012008-07-02 10:56:40 +02001478 *
Christof Schmittb62a8d92010-09-08 14:39:55 +02001479 * Changes in common status bits are propagated to attached ports and LUNs.
Christof Schmitt287ac012008-07-02 10:56:40 +02001480 */
Swen Schilligedaed852010-09-08 14:40:01 +02001481void zfcp_erp_set_adapter_status(struct zfcp_adapter *adapter, u32 mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 struct zfcp_port *port;
Swen Schilligedaed852010-09-08 14:40:01 +02001484 struct scsi_device *sdev;
Swen Schilligecf0c772009-11-24 16:53:58 +01001485 unsigned long flags;
Christof Schmitt287ac012008-07-02 10:56:40 +02001486 u32 common_mask = mask & ZFCP_COMMON_FLAGS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001488 atomic_or(mask, &adapter->status);
Christof Schmitt287ac012008-07-02 10:56:40 +02001489
Swen Schilligedaed852010-09-08 14:40:01 +02001490 if (!common_mask)
1491 return;
1492
1493 read_lock_irqsave(&adapter->port_list_lock, flags);
1494 list_for_each_entry(port, &adapter->port_list, list)
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001495 atomic_or(common_mask, &port->status);
Swen Schilligedaed852010-09-08 14:40:01 +02001496 read_unlock_irqrestore(&adapter->port_list_lock, flags);
1497
Martin Peschke924dd582013-08-22 17:45:37 +02001498 spin_lock_irqsave(adapter->scsi_host->host_lock, flags);
1499 __shost_for_each_device(sdev, adapter->scsi_host)
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001500 atomic_or(common_mask, &sdev_to_zfcp(sdev)->status);
Martin Peschke924dd582013-08-22 17:45:37 +02001501 spin_unlock_irqrestore(adapter->scsi_host->host_lock, flags);
Swen Schilligedaed852010-09-08 14:40:01 +02001502}
1503
1504/**
1505 * zfcp_erp_clear_adapter_status - clear adapter status bits
1506 * @adapter: adapter to change the status
1507 * @mask: status bits to change
1508 *
1509 * Changes in common status bits are propagated to attached ports and LUNs.
1510 */
1511void zfcp_erp_clear_adapter_status(struct zfcp_adapter *adapter, u32 mask)
1512{
1513 struct zfcp_port *port;
1514 struct scsi_device *sdev;
1515 unsigned long flags;
1516 u32 common_mask = mask & ZFCP_COMMON_FLAGS;
1517 u32 clear_counter = mask & ZFCP_STATUS_COMMON_ERP_FAILED;
1518
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001519 atomic_andnot(mask, &adapter->status);
Swen Schilligedaed852010-09-08 14:40:01 +02001520
1521 if (!common_mask)
1522 return;
1523
1524 if (clear_counter)
1525 atomic_set(&adapter->erp_counter, 0);
1526
1527 read_lock_irqsave(&adapter->port_list_lock, flags);
1528 list_for_each_entry(port, &adapter->port_list, list) {
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001529 atomic_andnot(common_mask, &port->status);
Swen Schilligedaed852010-09-08 14:40:01 +02001530 if (clear_counter)
1531 atomic_set(&port->erp_counter, 0);
1532 }
1533 read_unlock_irqrestore(&adapter->port_list_lock, flags);
1534
Martin Peschke924dd582013-08-22 17:45:37 +02001535 spin_lock_irqsave(adapter->scsi_host->host_lock, flags);
1536 __shost_for_each_device(sdev, adapter->scsi_host) {
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001537 atomic_andnot(common_mask, &sdev_to_zfcp(sdev)->status);
Swen Schilligedaed852010-09-08 14:40:01 +02001538 if (clear_counter)
1539 atomic_set(&sdev_to_zfcp(sdev)->erp_counter, 0);
Swen Schilligecf0c772009-11-24 16:53:58 +01001540 }
Martin Peschke924dd582013-08-22 17:45:37 +02001541 spin_unlock_irqrestore(adapter->scsi_host->host_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542}
1543
Christof Schmitt287ac012008-07-02 10:56:40 +02001544/**
Swen Schilligedaed852010-09-08 14:40:01 +02001545 * zfcp_erp_set_port_status - set port status bits
1546 * @port: port to change the status
Christof Schmitt287ac012008-07-02 10:56:40 +02001547 * @mask: status bits to change
Christof Schmitt287ac012008-07-02 10:56:40 +02001548 *
Christof Schmittb62a8d92010-09-08 14:39:55 +02001549 * Changes in common status bits are propagated to attached LUNs.
Christof Schmitt287ac012008-07-02 10:56:40 +02001550 */
Swen Schilligedaed852010-09-08 14:40:01 +02001551void zfcp_erp_set_port_status(struct zfcp_port *port, u32 mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552{
Christof Schmittb62a8d92010-09-08 14:39:55 +02001553 struct scsi_device *sdev;
Christof Schmitt287ac012008-07-02 10:56:40 +02001554 u32 common_mask = mask & ZFCP_COMMON_FLAGS;
Martin Peschke924dd582013-08-22 17:45:37 +02001555 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001557 atomic_or(mask, &port->status);
Christof Schmitt287ac012008-07-02 10:56:40 +02001558
Swen Schilligedaed852010-09-08 14:40:01 +02001559 if (!common_mask)
1560 return;
1561
Martin Peschke924dd582013-08-22 17:45:37 +02001562 spin_lock_irqsave(port->adapter->scsi_host->host_lock, flags);
1563 __shost_for_each_device(sdev, port->adapter->scsi_host)
Swen Schilligedaed852010-09-08 14:40:01 +02001564 if (sdev_to_zfcp(sdev)->port == port)
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001565 atomic_or(common_mask,
Swen Schilligedaed852010-09-08 14:40:01 +02001566 &sdev_to_zfcp(sdev)->status);
Martin Peschke924dd582013-08-22 17:45:37 +02001567 spin_unlock_irqrestore(port->adapter->scsi_host->host_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568}
1569
Christof Schmitt287ac012008-07-02 10:56:40 +02001570/**
Swen Schilligedaed852010-09-08 14:40:01 +02001571 * zfcp_erp_clear_port_status - clear port status bits
1572 * @port: adapter to change the status
Christof Schmitt287ac012008-07-02 10:56:40 +02001573 * @mask: status bits to change
Swen Schilligedaed852010-09-08 14:40:01 +02001574 *
1575 * Changes in common status bits are propagated to attached LUNs.
Christof Schmitt287ac012008-07-02 10:56:40 +02001576 */
Swen Schilligedaed852010-09-08 14:40:01 +02001577void zfcp_erp_clear_port_status(struct zfcp_port *port, u32 mask)
1578{
1579 struct scsi_device *sdev;
1580 u32 common_mask = mask & ZFCP_COMMON_FLAGS;
1581 u32 clear_counter = mask & ZFCP_STATUS_COMMON_ERP_FAILED;
Martin Peschke924dd582013-08-22 17:45:37 +02001582 unsigned long flags;
Swen Schilligedaed852010-09-08 14:40:01 +02001583
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001584 atomic_andnot(mask, &port->status);
Swen Schilligedaed852010-09-08 14:40:01 +02001585
1586 if (!common_mask)
1587 return;
1588
1589 if (clear_counter)
1590 atomic_set(&port->erp_counter, 0);
1591
Martin Peschke924dd582013-08-22 17:45:37 +02001592 spin_lock_irqsave(port->adapter->scsi_host->host_lock, flags);
1593 __shost_for_each_device(sdev, port->adapter->scsi_host)
Swen Schilligedaed852010-09-08 14:40:01 +02001594 if (sdev_to_zfcp(sdev)->port == port) {
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001595 atomic_andnot(common_mask,
Swen Schilligedaed852010-09-08 14:40:01 +02001596 &sdev_to_zfcp(sdev)->status);
1597 if (clear_counter)
1598 atomic_set(&sdev_to_zfcp(sdev)->erp_counter, 0);
1599 }
Martin Peschke924dd582013-08-22 17:45:37 +02001600 spin_unlock_irqrestore(port->adapter->scsi_host->host_lock, flags);
Swen Schilligedaed852010-09-08 14:40:01 +02001601}
1602
1603/**
1604 * zfcp_erp_set_lun_status - set lun status bits
1605 * @sdev: SCSI device / lun to set the status bits
1606 * @mask: status bits to change
1607 */
1608void zfcp_erp_set_lun_status(struct scsi_device *sdev, u32 mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609{
Christof Schmittb62a8d92010-09-08 14:39:55 +02001610 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
1611
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001612 atomic_or(mask, &zfcp_sdev->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613}
1614
Christof Schmitt287ac012008-07-02 10:56:40 +02001615/**
Swen Schilligedaed852010-09-08 14:40:01 +02001616 * zfcp_erp_clear_lun_status - clear lun status bits
1617 * @sdev: SCSi device / lun to clear the status bits
1618 * @mask: status bits to change
Christof Schmitt287ac012008-07-02 10:56:40 +02001619 */
Swen Schilligedaed852010-09-08 14:40:01 +02001620void zfcp_erp_clear_lun_status(struct scsi_device *sdev, u32 mask)
Andreas Herrmannd736a27b2005-06-13 13:23:57 +02001621{
Swen Schilligedaed852010-09-08 14:40:01 +02001622 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
1623
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001624 atomic_andnot(mask, &zfcp_sdev->status);
Swen Schilligedaed852010-09-08 14:40:01 +02001625
1626 if (mask & ZFCP_STATUS_COMMON_ERP_FAILED)
1627 atomic_set(&zfcp_sdev->erp_counter, 0);
Andreas Herrmannd736a27b2005-06-13 13:23:57 +02001628}
1629