zfcp_fsf.c 72.3 KB
Newer Older
L
Linus Torvalds 已提交
1
/*
C
Christof Schmitt 已提交
2
 * zfcp device driver
L
Linus Torvalds 已提交
3
 *
C
Christof Schmitt 已提交
4
 * Implementation of FSF commands.
L
Linus Torvalds 已提交
5
 *
6
 * Copyright IBM Corporation 2002, 2009
L
Linus Torvalds 已提交
7 8
 */

9 10 11
#define KMSG_COMPONENT "zfcp"
#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt

S
Stefan Raspl 已提交
12
#include <linux/blktrace_api.h>
L
Linus Torvalds 已提交
13
#include "zfcp_ext.h"
14
#include "zfcp_dbf.h"
L
Linus Torvalds 已提交
15

16 17 18
#define ZFCP_REQ_AUTO_CLEANUP	0x00000002
#define ZFCP_REQ_NO_QTCB	0x00000008

19 20 21
static void zfcp_fsf_request_timeout_handler(unsigned long data)
{
	struct zfcp_adapter *adapter = (struct zfcp_adapter *) data;
22 23
	zfcp_erp_adapter_reopen(adapter, ZFCP_STATUS_COMMON_ERP_FAILED,
				"fsrth_1", NULL);
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
}

static void zfcp_fsf_start_timer(struct zfcp_fsf_req *fsf_req,
				 unsigned long timeout)
{
	fsf_req->timer.function = zfcp_fsf_request_timeout_handler;
	fsf_req->timer.data = (unsigned long) fsf_req->adapter;
	fsf_req->timer.expires = jiffies + timeout;
	add_timer(&fsf_req->timer);
}

static void zfcp_fsf_start_erp_timer(struct zfcp_fsf_req *fsf_req)
{
	BUG_ON(!fsf_req->erp_action);
	fsf_req->timer.function = zfcp_erp_timeout_handler;
	fsf_req->timer.data = (unsigned long) fsf_req->erp_action;
	fsf_req->timer.expires = jiffies + 30 * HZ;
	add_timer(&fsf_req->timer);
}

L
Linus Torvalds 已提交
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
/* association between FSF command and FSF QTCB type */
static u32 fsf_qtcb_type[] = {
	[FSF_QTCB_FCP_CMND] =             FSF_IO_COMMAND,
	[FSF_QTCB_ABORT_FCP_CMND] =       FSF_SUPPORT_COMMAND,
	[FSF_QTCB_OPEN_PORT_WITH_DID] =   FSF_SUPPORT_COMMAND,
	[FSF_QTCB_OPEN_LUN] =             FSF_SUPPORT_COMMAND,
	[FSF_QTCB_CLOSE_LUN] =            FSF_SUPPORT_COMMAND,
	[FSF_QTCB_CLOSE_PORT] =           FSF_SUPPORT_COMMAND,
	[FSF_QTCB_CLOSE_PHYSICAL_PORT] =  FSF_SUPPORT_COMMAND,
	[FSF_QTCB_SEND_ELS] =             FSF_SUPPORT_COMMAND,
	[FSF_QTCB_SEND_GENERIC] =         FSF_SUPPORT_COMMAND,
	[FSF_QTCB_EXCHANGE_CONFIG_DATA] = FSF_CONFIG_COMMAND,
	[FSF_QTCB_EXCHANGE_PORT_DATA] =   FSF_PORT_COMMAND,
	[FSF_QTCB_DOWNLOAD_CONTROL_FILE] = FSF_SUPPORT_COMMAND,
	[FSF_QTCB_UPLOAD_CONTROL_FILE] =  FSF_SUPPORT_COMMAND
};

C
Christof Schmitt 已提交
61 62
static void zfcp_act_eval_err(struct zfcp_adapter *adapter, u32 table)
{
S
Swen Schillig 已提交
63
	u16 subtable = table >> 16;
C
Christof Schmitt 已提交
64
	u16 rule = table & 0xffff;
65
	const char *act_type[] = { "unknown", "OS", "WWPN", "DID", "LUN" };
C
Christof Schmitt 已提交
66

67
	if (subtable && subtable < ARRAY_SIZE(act_type))
C
Christof Schmitt 已提交
68
		dev_warn(&adapter->ccw_device->dev,
69 70
			 "Access denied according to ACT rule type %s, "
			 "rule %d\n", act_type[subtable], rule);
C
Christof Schmitt 已提交
71 72 73 74 75 76 77
}

static void zfcp_fsf_access_denied_port(struct zfcp_fsf_req *req,
					struct zfcp_port *port)
{
	struct fsf_qtcb_header *header = &req->qtcb->header;
	dev_warn(&req->adapter->ccw_device->dev,
78
		 "Access denied to port 0x%016Lx\n",
79
		 (unsigned long long)port->wwpn);
C
Christof Schmitt 已提交
80 81
	zfcp_act_eval_err(req->adapter, header->fsf_status_qual.halfword[0]);
	zfcp_act_eval_err(req->adapter, header->fsf_status_qual.halfword[1]);
82
	zfcp_erp_port_access_denied(port, "fspad_1", req);
C
Christof Schmitt 已提交
83 84 85 86 87 88 89 90
	req->status |= ZFCP_STATUS_FSFREQ_ERROR;
}

static void zfcp_fsf_access_denied_unit(struct zfcp_fsf_req *req,
					struct zfcp_unit *unit)
{
	struct fsf_qtcb_header *header = &req->qtcb->header;
	dev_warn(&req->adapter->ccw_device->dev,
91
		 "Access denied to unit 0x%016Lx on port 0x%016Lx\n",
92 93
		 (unsigned long long)unit->fcp_lun,
		 (unsigned long long)unit->port->wwpn);
C
Christof Schmitt 已提交
94 95
	zfcp_act_eval_err(req->adapter, header->fsf_status_qual.halfword[0]);
	zfcp_act_eval_err(req->adapter, header->fsf_status_qual.halfword[1]);
96
	zfcp_erp_unit_access_denied(unit, "fsuad_1", req);
C
Christof Schmitt 已提交
97 98 99 100 101
	req->status |= ZFCP_STATUS_FSFREQ_ERROR;
}

static void zfcp_fsf_class_not_supp(struct zfcp_fsf_req *req)
{
102 103
	dev_err(&req->adapter->ccw_device->dev, "FCP device not "
		"operational because of an unsupported FC class\n");
104
	zfcp_erp_adapter_shutdown(req->adapter, 0, "fscns_1", req);
C
Christof Schmitt 已提交
105 106 107
	req->status |= ZFCP_STATUS_FSFREQ_ERROR;
}

S
Swen Schillig 已提交
108 109 110
/**
 * zfcp_fsf_req_free - free memory used by fsf request
 * @fsf_req: pointer to struct zfcp_fsf_req
L
Linus Torvalds 已提交
111
 */
S
Swen Schillig 已提交
112
void zfcp_fsf_req_free(struct zfcp_fsf_req *req)
L
Linus Torvalds 已提交
113
{
S
Swen Schillig 已提交
114 115
	if (likely(req->pool)) {
		mempool_free(req, req->pool);
116 117 118
		return;
	}

S
Swen Schillig 已提交
119 120
	if (req->qtcb) {
		kmem_cache_free(zfcp_data.fsf_req_qtcb_cache, req);
121 122
		return;
	}
L
Linus Torvalds 已提交
123 124
}

S
Swen Schillig 已提交
125 126 127 128
/**
 * zfcp_fsf_req_dismiss_all - dismiss all fsf requests
 * @adapter: pointer to struct zfcp_adapter
 *
129 130 131 132
 * Never ever call this without shutting down the adapter first.
 * Otherwise the adapter would continue using and corrupting s390 storage.
 * Included BUG_ON() call to ensure this is done.
 * ERP is supposed to be the only user of this function.
L
Linus Torvalds 已提交
133
 */
134
void zfcp_fsf_req_dismiss_all(struct zfcp_adapter *adapter)
L
Linus Torvalds 已提交
135
{
S
Swen Schillig 已提交
136
	struct zfcp_fsf_req *req, *tmp;
137
	unsigned long flags;
138
	LIST_HEAD(remove_queue);
139
	unsigned int i;
140

S
Swen Schillig 已提交
141
	BUG_ON(atomic_read(&adapter->status) & ZFCP_STATUS_ADAPTER_QDIOUP);
142
	spin_lock_irqsave(&adapter->req_list_lock, flags);
143
	for (i = 0; i < REQUEST_LIST_SIZE; i++)
144
		list_splice_init(&adapter->req_list[i], &remove_queue);
145 146
	spin_unlock_irqrestore(&adapter->req_list_lock, flags);

S
Swen Schillig 已提交
147 148 149 150
	list_for_each_entry_safe(req, tmp, &remove_queue, list) {
		list_del(&req->list);
		req->status |= ZFCP_STATUS_FSFREQ_DISMISSED;
		zfcp_fsf_req_complete(req);
L
Linus Torvalds 已提交
151 152 153
	}
}

S
Swen Schillig 已提交
154
static void zfcp_fsf_status_read_port_closed(struct zfcp_fsf_req *req)
L
Linus Torvalds 已提交
155
{
S
Swen Schillig 已提交
156 157 158 159 160
	struct fsf_status_read_buffer *sr_buf = req->data;
	struct zfcp_adapter *adapter = req->adapter;
	struct zfcp_port *port;
	int d_id = sr_buf->d_id & ZFCP_DID_MASK;
	unsigned long flags;
L
Linus Torvalds 已提交
161

S
Swen Schillig 已提交
162 163 164 165
	read_lock_irqsave(&zfcp_data.config_lock, flags);
	list_for_each_entry(port, &adapter->port_list_head, list)
		if (port->d_id == d_id) {
			read_unlock_irqrestore(&zfcp_data.config_lock, flags);
166
			zfcp_erp_port_reopen(port, 0, "fssrpc1", req);
S
Swen Schillig 已提交
167 168 169
			return;
		}
	read_unlock_irqrestore(&zfcp_data.config_lock, flags);
L
Linus Torvalds 已提交
170 171
}

172
static void zfcp_fsf_link_down_info_eval(struct zfcp_fsf_req *req, char *id,
S
Swen Schillig 已提交
173
					 struct fsf_link_down_info *link_down)
174
{
S
Swen Schillig 已提交
175
	struct zfcp_adapter *adapter = req->adapter;
176
	unsigned long flags;
177

S
Swen Schillig 已提交
178
	if (atomic_read(&adapter->status) & ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED)
179 180 181
		return;

	atomic_set_mask(ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED, &adapter->status);
182 183

	read_lock_irqsave(&zfcp_data.config_lock, flags);
184
	zfcp_scsi_schedule_rports_block(adapter);
185
	read_unlock_irqrestore(&zfcp_data.config_lock, flags);
186

S
Swen Schillig 已提交
187
	if (!link_down)
188
		goto out;
189

190 191
	switch (link_down->error_code) {
	case FSF_PSQ_LINK_NO_LIGHT:
S
Swen Schillig 已提交
192
		dev_warn(&req->adapter->ccw_device->dev,
193 194
			 "There is no light signal from the local "
			 "fibre channel cable\n");
195 196
		break;
	case FSF_PSQ_LINK_WRAP_PLUG:
S
Swen Schillig 已提交
197
		dev_warn(&req->adapter->ccw_device->dev,
198 199
			 "There is a wrap plug instead of a fibre "
			 "channel cable\n");
200 201
		break;
	case FSF_PSQ_LINK_NO_FCP:
S
Swen Schillig 已提交
202
		dev_warn(&req->adapter->ccw_device->dev,
203 204
			 "The adjacent fibre channel node does not "
			 "support FCP\n");
205 206
		break;
	case FSF_PSQ_LINK_FIRMWARE_UPDATE:
S
Swen Schillig 已提交
207
		dev_warn(&req->adapter->ccw_device->dev,
208 209
			 "The FCP device is suspended because of a "
			 "firmware update\n");
C
Christof Schmitt 已提交
210
		break;
211
	case FSF_PSQ_LINK_INVALID_WWPN:
S
Swen Schillig 已提交
212
		dev_warn(&req->adapter->ccw_device->dev,
213 214
			 "The FCP device detected a WWPN that is "
			 "duplicate or not valid\n");
215 216
		break;
	case FSF_PSQ_LINK_NO_NPIV_SUPPORT:
S
Swen Schillig 已提交
217
		dev_warn(&req->adapter->ccw_device->dev,
218
			 "The fibre channel fabric does not support NPIV\n");
219 220
		break;
	case FSF_PSQ_LINK_NO_FCP_RESOURCES:
S
Swen Schillig 已提交
221
		dev_warn(&req->adapter->ccw_device->dev,
222
			 "The FCP adapter cannot support more NPIV ports\n");
223 224
		break;
	case FSF_PSQ_LINK_NO_FABRIC_RESOURCES:
S
Swen Schillig 已提交
225
		dev_warn(&req->adapter->ccw_device->dev,
226 227
			 "The adjacent switch cannot support "
			 "more NPIV ports\n");
228 229
		break;
	case FSF_PSQ_LINK_FABRIC_LOGIN_UNABLE:
S
Swen Schillig 已提交
230
		dev_warn(&req->adapter->ccw_device->dev,
231 232
			 "The FCP adapter could not log in to the "
			 "fibre channel fabric\n");
233 234
		break;
	case FSF_PSQ_LINK_WWPN_ASSIGNMENT_CORRUPTED:
S
Swen Schillig 已提交
235
		dev_warn(&req->adapter->ccw_device->dev,
236 237
			 "The WWPN assignment file on the FCP adapter "
			 "has been damaged\n");
238 239
		break;
	case FSF_PSQ_LINK_MODE_TABLE_CURRUPTED:
S
Swen Schillig 已提交
240
		dev_warn(&req->adapter->ccw_device->dev,
241 242
			 "The mode table on the FCP adapter "
			 "has been damaged\n");
243 244
		break;
	case FSF_PSQ_LINK_NO_WWPN_ASSIGNMENT:
S
Swen Schillig 已提交
245
		dev_warn(&req->adapter->ccw_device->dev,
246 247
			 "All NPIV ports on the FCP adapter have "
			 "been assigned\n");
248 249
		break;
	default:
S
Swen Schillig 已提交
250
		dev_warn(&req->adapter->ccw_device->dev,
251 252
			 "The link between the FCP adapter and "
			 "the FC fabric is down\n");
253
	}
S
Swen Schillig 已提交
254 255
out:
	zfcp_erp_adapter_failed(adapter, id, req);
256 257
}

S
Swen Schillig 已提交
258
static void zfcp_fsf_status_read_link_down(struct zfcp_fsf_req *req)
L
Linus Torvalds 已提交
259
{
S
Swen Schillig 已提交
260 261 262
	struct fsf_status_read_buffer *sr_buf = req->data;
	struct fsf_link_down_info *ldi =
		(struct fsf_link_down_info *) &sr_buf->payload;
L
Linus Torvalds 已提交
263

S
Swen Schillig 已提交
264 265
	switch (sr_buf->status_subtype) {
	case FSF_STATUS_READ_SUB_NO_PHYSICAL_LINK:
266
		zfcp_fsf_link_down_info_eval(req, "fssrld1", ldi);
L
Linus Torvalds 已提交
267
		break;
S
Swen Schillig 已提交
268
	case FSF_STATUS_READ_SUB_FDISC_FAILED:
269
		zfcp_fsf_link_down_info_eval(req, "fssrld2", ldi);
L
Linus Torvalds 已提交
270
		break;
S
Swen Schillig 已提交
271
	case FSF_STATUS_READ_SUB_FIRMWARE_UPDATE:
272
		zfcp_fsf_link_down_info_eval(req, "fssrld3", NULL);
S
Swen Schillig 已提交
273 274
	};
}
L
Linus Torvalds 已提交
275

S
Swen Schillig 已提交
276 277 278 279
static void zfcp_fsf_status_read_handler(struct zfcp_fsf_req *req)
{
	struct zfcp_adapter *adapter = req->adapter;
	struct fsf_status_read_buffer *sr_buf = req->data;
L
Linus Torvalds 已提交
280

S
Swen Schillig 已提交
281 282 283 284 285 286
	if (req->status & ZFCP_STATUS_FSFREQ_DISMISSED) {
		zfcp_hba_dbf_event_fsf_unsol("dism", adapter, sr_buf);
		mempool_free(sr_buf, adapter->pool.data_status_read);
		zfcp_fsf_req_free(req);
		return;
	}
L
Linus Torvalds 已提交
287

S
Swen Schillig 已提交
288
	zfcp_hba_dbf_event_fsf_unsol("read", adapter, sr_buf);
L
Linus Torvalds 已提交
289

S
Swen Schillig 已提交
290 291 292
	switch (sr_buf->status_type) {
	case FSF_STATUS_READ_PORT_CLOSED:
		zfcp_fsf_status_read_port_closed(req);
L
Linus Torvalds 已提交
293
		break;
S
Swen Schillig 已提交
294 295
	case FSF_STATUS_READ_INCOMING_ELS:
		zfcp_fc_incoming_els(req);
L
Linus Torvalds 已提交
296
		break;
S
Swen Schillig 已提交
297
	case FSF_STATUS_READ_SENSE_DATA_AVAIL:
L
Linus Torvalds 已提交
298
		break;
S
Swen Schillig 已提交
299
	case FSF_STATUS_READ_BIT_ERROR_THRESHOLD:
300 301 302
		dev_warn(&adapter->ccw_device->dev,
			 "The error threshold for checksum statistics "
			 "has been exceeded\n");
303
		zfcp_hba_dbf_event_berr(adapter, req);
L
Linus Torvalds 已提交
304
		break;
S
Swen Schillig 已提交
305 306 307 308 309
	case FSF_STATUS_READ_LINK_DOWN:
		zfcp_fsf_status_read_link_down(req);
		break;
	case FSF_STATUS_READ_LINK_UP:
		dev_info(&adapter->ccw_device->dev,
310
			 "The local link has been restored\n");
S
Swen Schillig 已提交
311
		/* All ports should be marked as ready to run again */
312
		zfcp_erp_modify_adapter_status(adapter, "fssrh_1", NULL,
S
Swen Schillig 已提交
313 314 315 316 317
					       ZFCP_STATUS_COMMON_RUNNING,
					       ZFCP_SET);
		zfcp_erp_adapter_reopen(adapter,
					ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED |
					ZFCP_STATUS_COMMON_ERP_FAILED,
318
					"fssrh_2", req);
S
Swen Schillig 已提交
319 320 321
		break;
	case FSF_STATUS_READ_NOTIFICATION_LOST:
		if (sr_buf->status_subtype & FSF_STATUS_READ_SUB_ACT_UPDATED)
322 323
			zfcp_erp_adapter_access_changed(adapter, "fssrh_3",
							req);
S
Swen Schillig 已提交
324 325 326 327
		if (sr_buf->status_subtype & FSF_STATUS_READ_SUB_INCOMING_ELS)
			schedule_work(&adapter->scan_work);
		break;
	case FSF_STATUS_READ_CFDC_UPDATED:
328
		zfcp_erp_adapter_access_changed(adapter, "fssrh_4", req);
S
Swen Schillig 已提交
329 330 331
		break;
	case FSF_STATUS_READ_FEATURE_UPDATE_ALERT:
		adapter->adapter_features = sr_buf->payload.word[0];
L
Linus Torvalds 已提交
332 333 334
		break;
	}

S
Swen Schillig 已提交
335 336
	mempool_free(sr_buf, adapter->pool.data_status_read);
	zfcp_fsf_req_free(req);
L
Linus Torvalds 已提交
337

S
Swen Schillig 已提交
338
	atomic_inc(&adapter->stat_miss);
339
	queue_work(zfcp_data.work_queue, &adapter->stat_work);
S
Swen Schillig 已提交
340
}
L
Linus Torvalds 已提交
341

S
Swen Schillig 已提交
342 343 344 345 346 347 348 349 350 351 352 353 354
static void zfcp_fsf_fsfstatus_qual_eval(struct zfcp_fsf_req *req)
{
	switch (req->qtcb->header.fsf_status_qual.word[0]) {
	case FSF_SQ_FCP_RSP_AVAILABLE:
	case FSF_SQ_INVOKE_LINK_TEST_PROCEDURE:
	case FSF_SQ_NO_RETRY_POSSIBLE:
	case FSF_SQ_ULP_DEPENDENT_ERP_REQUIRED:
		return;
	case FSF_SQ_COMMAND_ABORTED:
		req->status |= ZFCP_STATUS_FSFREQ_ABORTED;
		break;
	case FSF_SQ_NO_RECOM:
		dev_err(&req->adapter->ccw_device->dev,
355 356
			"The FCP adapter reported a problem "
			"that cannot be recovered\n");
357
		zfcp_erp_adapter_shutdown(req->adapter, 0, "fsfsqe1", req);
S
Swen Schillig 已提交
358 359 360 361
		break;
	}
	/* all non-return stats set FSFREQ_ERROR*/
	req->status |= ZFCP_STATUS_FSFREQ_ERROR;
L
Linus Torvalds 已提交
362 363
}

S
Swen Schillig 已提交
364
static void zfcp_fsf_fsfstatus_eval(struct zfcp_fsf_req *req)
L
Linus Torvalds 已提交
365
{
S
Swen Schillig 已提交
366 367
	if (unlikely(req->status & ZFCP_STATUS_FSFREQ_ERROR))
		return;
L
Linus Torvalds 已提交
368

S
Swen Schillig 已提交
369 370 371
	switch (req->qtcb->header.fsf_status) {
	case FSF_UNKNOWN_COMMAND:
		dev_err(&req->adapter->ccw_device->dev,
372
			"The FCP adapter does not recognize the command 0x%x\n",
S
Swen Schillig 已提交
373
			req->qtcb->header.fsf_command);
374
		zfcp_erp_adapter_shutdown(req->adapter, 0, "fsfse_1", req);
S
Swen Schillig 已提交
375 376 377 378 379 380 381
		req->status |= ZFCP_STATUS_FSFREQ_ERROR;
		break;
	case FSF_ADAPTER_STATUS_AVAILABLE:
		zfcp_fsf_fsfstatus_qual_eval(req);
		break;
	}
}
L
Linus Torvalds 已提交
382

S
Swen Schillig 已提交
383 384 385 386 387
static void zfcp_fsf_protstatus_eval(struct zfcp_fsf_req *req)
{
	struct zfcp_adapter *adapter = req->adapter;
	struct fsf_qtcb *qtcb = req->qtcb;
	union fsf_prot_status_qual *psq = &qtcb->prefix.prot_status_qual;
L
Linus Torvalds 已提交
388

S
Swen Schillig 已提交
389
	zfcp_hba_dbf_event_fsf_response(req);
L
Linus Torvalds 已提交
390

S
Swen Schillig 已提交
391 392 393 394 395
	if (req->status & ZFCP_STATUS_FSFREQ_DISMISSED) {
		req->status |= ZFCP_STATUS_FSFREQ_ERROR |
			ZFCP_STATUS_FSFREQ_RETRY; /* only for SCSI cmnds. */
		return;
	}
L
Linus Torvalds 已提交
396

S
Swen Schillig 已提交
397 398 399 400 401 402
	switch (qtcb->prefix.prot_status) {
	case FSF_PROT_GOOD:
	case FSF_PROT_FSF_STATUS_PRESENTED:
		return;
	case FSF_PROT_QTCB_VERSION_ERROR:
		dev_err(&adapter->ccw_device->dev,
403 404 405
			"QTCB version 0x%x not supported by FCP adapter "
			"(0x%x to 0x%x)\n", FSF_QTCB_CURRENT_VERSION,
			psq->word[0], psq->word[1]);
406
		zfcp_erp_adapter_shutdown(adapter, 0, "fspse_1", req);
S
Swen Schillig 已提交
407 408 409
		break;
	case FSF_PROT_ERROR_STATE:
	case FSF_PROT_SEQ_NUMB_ERROR:
410
		zfcp_erp_adapter_reopen(adapter, 0, "fspse_2", req);
S
Swen Schillig 已提交
411 412 413 414
		req->status |= ZFCP_STATUS_FSFREQ_RETRY;
		break;
	case FSF_PROT_UNSUPP_QTCB_TYPE:
		dev_err(&adapter->ccw_device->dev,
415
			"The QTCB type is not supported by the FCP adapter\n");
416
		zfcp_erp_adapter_shutdown(adapter, 0, "fspse_3", req);
S
Swen Schillig 已提交
417 418 419 420 421 422 423
		break;
	case FSF_PROT_HOST_CONNECTION_INITIALIZING:
		atomic_set_mask(ZFCP_STATUS_ADAPTER_HOST_CON_INIT,
				&adapter->status);
		break;
	case FSF_PROT_DUPLICATE_REQUEST_ID:
		dev_err(&adapter->ccw_device->dev,
424
			"0x%Lx is an ambiguous request identifier\n",
S
Swen Schillig 已提交
425
			(unsigned long long)qtcb->bottom.support.req_handle);
426
		zfcp_erp_adapter_shutdown(adapter, 0, "fspse_4", req);
S
Swen Schillig 已提交
427 428
		break;
	case FSF_PROT_LINK_DOWN:
429 430
		zfcp_fsf_link_down_info_eval(req, "fspse_5",
					     &psq->link_down_info);
S
Swen Schillig 已提交
431
		/* FIXME: reopening adapter now? better wait for link up */
432
		zfcp_erp_adapter_reopen(adapter, 0, "fspse_6", req);
S
Swen Schillig 已提交
433 434 435
		break;
	case FSF_PROT_REEST_QUEUE:
		/* All ports should be marked as ready to run again */
436
		zfcp_erp_modify_adapter_status(adapter, "fspse_7", NULL,
S
Swen Schillig 已提交
437 438 439 440
					       ZFCP_STATUS_COMMON_RUNNING,
					       ZFCP_SET);
		zfcp_erp_adapter_reopen(adapter,
					ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED |
441 442
					ZFCP_STATUS_COMMON_ERP_FAILED,
					"fspse_8", req);
S
Swen Schillig 已提交
443 444 445
		break;
	default:
		dev_err(&adapter->ccw_device->dev,
446
			"0x%x is not a valid transfer protocol status\n",
S
Swen Schillig 已提交
447
			qtcb->prefix.prot_status);
448
		zfcp_erp_adapter_shutdown(adapter, 0, "fspse_9", req);
S
Swen Schillig 已提交
449 450
	}
	req->status |= ZFCP_STATUS_FSFREQ_ERROR;
L
Linus Torvalds 已提交
451 452
}

S
Swen Schillig 已提交
453 454 455 456 457 458 459 460 461 462
/**
 * zfcp_fsf_req_complete - process completion of a FSF request
 * @fsf_req: The FSF request that has been completed.
 *
 * When a request has been completed either from the FCP adapter,
 * or it has been dismissed due to a queue shutdown, this function
 * is called to process the completion status and trigger further
 * events related to the FSF request.
 */
void zfcp_fsf_req_complete(struct zfcp_fsf_req *req)
L
Linus Torvalds 已提交
463
{
S
Swen Schillig 已提交
464 465 466 467
	if (unlikely(req->fsf_command == FSF_QTCB_UNSOLICITED_STATUS)) {
		zfcp_fsf_status_read_handler(req);
		return;
	}
L
Linus Torvalds 已提交
468

S
Swen Schillig 已提交
469 470 471 472
	del_timer(&req->timer);
	zfcp_fsf_protstatus_eval(req);
	zfcp_fsf_fsfstatus_eval(req);
	req->handler(req);
L
Linus Torvalds 已提交
473

S
Swen Schillig 已提交
474
	if (req->erp_action)
475
		zfcp_erp_notify(req->erp_action, 0);
S
Swen Schillig 已提交
476
	req->status |= ZFCP_STATUS_FSFREQ_COMPLETED;
L
Linus Torvalds 已提交
477

S
Swen Schillig 已提交
478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493
	if (likely(req->status & ZFCP_STATUS_FSFREQ_CLEANUP))
		zfcp_fsf_req_free(req);
	else
	/* notify initiator waiting for the requests completion */
	/*
	 * FIXME: Race! We must not access fsf_req here as it might have been
	 * cleaned up already due to the set ZFCP_STATUS_FSFREQ_COMPLETED
	 * flag. It's an improbable case. But, we have the same paranoia for
	 * the cleanup flag already.
	 * Might better be handled using complete()?
	 * (setting the flag and doing wakeup ought to be atomic
	 *  with regard to checking the flag as long as waitqueue is
	 *  part of the to be released structure)
	 */
		wake_up(&req->completion_wq);
}
L
Linus Torvalds 已提交
494

S
Swen Schillig 已提交
495 496 497 498 499
static int zfcp_fsf_exchange_config_evaluate(struct zfcp_fsf_req *req)
{
	struct fsf_qtcb_bottom_config *bottom;
	struct zfcp_adapter *adapter = req->adapter;
	struct Scsi_Host *shost = adapter->scsi_host;
L
Linus Torvalds 已提交
500

S
Swen Schillig 已提交
501
	bottom = &req->qtcb->bottom.config;
L
Linus Torvalds 已提交
502

S
Swen Schillig 已提交
503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529
	if (req->data)
		memcpy(req->data, bottom, sizeof(*bottom));

	fc_host_node_name(shost) = bottom->nport_serv_param.wwnn;
	fc_host_port_name(shost) = bottom->nport_serv_param.wwpn;
	fc_host_port_id(shost) = bottom->s_id & ZFCP_DID_MASK;
	fc_host_speed(shost) = bottom->fc_link_speed;
	fc_host_supported_classes(shost) = FC_COS_CLASS2 | FC_COS_CLASS3;

	adapter->hydra_version = bottom->adapter_type;
	adapter->timer_ticks = bottom->timer_interval;

	if (fc_host_permanent_port_name(shost) == -1)
		fc_host_permanent_port_name(shost) = fc_host_port_name(shost);

	switch (bottom->fc_topology) {
	case FSF_TOPO_P2P:
		adapter->peer_d_id = bottom->peer_d_id & ZFCP_DID_MASK;
		adapter->peer_wwpn = bottom->plogi_payload.wwpn;
		adapter->peer_wwnn = bottom->plogi_payload.wwnn;
		fc_host_port_type(shost) = FC_PORTTYPE_PTP;
		break;
	case FSF_TOPO_FABRIC:
		fc_host_port_type(shost) = FC_PORTTYPE_NPORT;
		break;
	case FSF_TOPO_AL:
		fc_host_port_type(shost) = FC_PORTTYPE_NLPORT;
530
		/* fall through */
S
Swen Schillig 已提交
531 532
	default:
		dev_err(&adapter->ccw_device->dev,
533 534
			"Unknown or unsupported arbitrated loop "
			"fibre channel topology detected\n");
535
		zfcp_erp_adapter_shutdown(adapter, 0, "fsece_1", req);
S
Swen Schillig 已提交
536
		return -EIO;
L
Linus Torvalds 已提交
537
	}
S
Swen Schillig 已提交
538

L
Linus Torvalds 已提交
539 540 541
	return 0;
}

S
Swen Schillig 已提交
542
static void zfcp_fsf_exchange_config_data_handler(struct zfcp_fsf_req *req)
C
Christof Schmitt 已提交
543 544
{
	struct zfcp_adapter *adapter = req->adapter;
S
Swen Schillig 已提交
545 546 547
	struct fsf_qtcb *qtcb = req->qtcb;
	struct fsf_qtcb_bottom_config *bottom = &qtcb->bottom.config;
	struct Scsi_Host *shost = adapter->scsi_host;
C
Christof Schmitt 已提交
548

S
Swen Schillig 已提交
549 550
	if (req->status & ZFCP_STATUS_FSFREQ_ERROR)
		return;
L
Linus Torvalds 已提交
551

S
Swen Schillig 已提交
552 553 554 555 556 557
	adapter->fsf_lic_version = bottom->lic_version;
	adapter->adapter_features = bottom->adapter_features;
	adapter->connection_features = bottom->connection_features;
	adapter->peer_wwpn = 0;
	adapter->peer_wwnn = 0;
	adapter->peer_d_id = 0;
558

S
Swen Schillig 已提交
559 560 561 562
	switch (qtcb->header.fsf_status) {
	case FSF_GOOD:
		if (zfcp_fsf_exchange_config_evaluate(req))
			return;
L
Linus Torvalds 已提交
563

S
Swen Schillig 已提交
564 565
		if (bottom->max_qtcb_size < sizeof(struct fsf_qtcb)) {
			dev_err(&adapter->ccw_device->dev,
566 567 568
				"FCP adapter maximum QTCB size (%d bytes) "
				"is too small\n",
				bottom->max_qtcb_size);
569
			zfcp_erp_adapter_shutdown(adapter, 0, "fsecdh1", req);
S
Swen Schillig 已提交
570 571 572 573
			return;
		}
		atomic_set_mask(ZFCP_STATUS_ADAPTER_XCONFIG_OK,
				&adapter->status);
L
Linus Torvalds 已提交
574
		break;
S
Swen Schillig 已提交
575 576 577 578 579 580 581
	case FSF_EXCHANGE_CONFIG_DATA_INCOMPLETE:
		fc_host_node_name(shost) = 0;
		fc_host_port_name(shost) = 0;
		fc_host_port_id(shost) = 0;
		fc_host_speed(shost) = FC_PORTSPEED_UNKNOWN;
		fc_host_port_type(shost) = FC_PORTTYPE_UNKNOWN;
		adapter->hydra_version = 0;
L
Linus Torvalds 已提交
582

S
Swen Schillig 已提交
583 584
		atomic_set_mask(ZFCP_STATUS_ADAPTER_XCONFIG_OK,
				&adapter->status);
L
Linus Torvalds 已提交
585

586
		zfcp_fsf_link_down_info_eval(req, "fsecdh2",
S
Swen Schillig 已提交
587
			&qtcb->header.fsf_status_qual.link_down_info);
L
Linus Torvalds 已提交
588
		break;
S
Swen Schillig 已提交
589
	default:
590
		zfcp_erp_adapter_shutdown(adapter, 0, "fsecdh3", req);
S
Swen Schillig 已提交
591 592
		return;
	}
L
Linus Torvalds 已提交
593

S
Swen Schillig 已提交
594 595 596 597 598 599 600
	if (adapter->adapter_features & FSF_FEATURE_HBAAPI_MANAGEMENT) {
		adapter->hardware_version = bottom->hardware_version;
		memcpy(fc_host_serial_number(shost), bottom->serial_number,
		       min(FC_SERIAL_NUMBER_SIZE, 17));
		EBCASC(fc_host_serial_number(shost),
		       min(FC_SERIAL_NUMBER_SIZE, 17));
	}
L
Linus Torvalds 已提交
601

S
Swen Schillig 已提交
602 603
	if (FSF_QTCB_CURRENT_VERSION < bottom->low_qtcb_version) {
		dev_err(&adapter->ccw_device->dev,
604 605
			"The FCP adapter only supports newer "
			"control block versions\n");
606
		zfcp_erp_adapter_shutdown(adapter, 0, "fsecdh4", req);
S
Swen Schillig 已提交
607 608 609 610
		return;
	}
	if (FSF_QTCB_CURRENT_VERSION > bottom->high_qtcb_version) {
		dev_err(&adapter->ccw_device->dev,
611 612
			"The FCP adapter only supports older "
			"control block versions\n");
613
		zfcp_erp_adapter_shutdown(adapter, 0, "fsecdh5", req);
S
Swen Schillig 已提交
614 615
	}
}
L
Linus Torvalds 已提交
616

S
Swen Schillig 已提交
617 618 619 620 621
static void zfcp_fsf_exchange_port_evaluate(struct zfcp_fsf_req *req)
{
	struct zfcp_adapter *adapter = req->adapter;
	struct fsf_qtcb_bottom_port *bottom = &req->qtcb->bottom.port;
	struct Scsi_Host *shost = adapter->scsi_host;
L
Linus Torvalds 已提交
622

S
Swen Schillig 已提交
623 624
	if (req->data)
		memcpy(req->data, bottom, sizeof(*bottom));
625

626
	if (adapter->connection_features & FSF_FEATURE_NPIV_MODE) {
S
Swen Schillig 已提交
627
		fc_host_permanent_port_name(shost) = bottom->wwpn;
628 629
		fc_host_port_type(shost) = FC_PORTTYPE_NPIV;
	} else
S
Swen Schillig 已提交
630 631 632 633
		fc_host_permanent_port_name(shost) = fc_host_port_name(shost);
	fc_host_maxframe_size(shost) = bottom->maximum_frame_size;
	fc_host_supported_speeds(shost) = bottom->supported_speed;
}
L
Linus Torvalds 已提交
634

S
Swen Schillig 已提交
635 636 637 638 639 640 641 642 643 644 645 646 647
static void zfcp_fsf_exchange_port_data_handler(struct zfcp_fsf_req *req)
{
	struct fsf_qtcb *qtcb = req->qtcb;

	if (req->status & ZFCP_STATUS_FSFREQ_ERROR)
		return;

	switch (qtcb->header.fsf_status) {
	case FSF_GOOD:
		zfcp_fsf_exchange_port_evaluate(req);
		break;
	case FSF_EXCHANGE_CONFIG_DATA_INCOMPLETE:
		zfcp_fsf_exchange_port_evaluate(req);
648
		zfcp_fsf_link_down_info_eval(req, "fsepdh1",
S
Swen Schillig 已提交
649
			&qtcb->header.fsf_status_qual.link_down_info);
650
		break;
L
Linus Torvalds 已提交
651
	}
S
Swen Schillig 已提交
652
}
653

654
static int zfcp_fsf_sbal_check(struct zfcp_adapter *adapter)
S
Swen Schillig 已提交
655
{
656
	struct zfcp_qdio_queue *req_q = &adapter->req_q;
S
Swen Schillig 已提交
657

658 659 660 661 662 663 664 665 666 667
	spin_lock_bh(&adapter->req_q_lock);
	if (atomic_read(&req_q->count))
		return 1;
	spin_unlock_bh(&adapter->req_q_lock);
	return 0;
}

static int zfcp_fsf_req_sbal_get(struct zfcp_adapter *adapter)
{
	long ret;
668

669
	spin_unlock_bh(&adapter->req_q_lock);
S
Swen Schillig 已提交
670
	ret = wait_event_interruptible_timeout(adapter->request_wq,
671
			       zfcp_fsf_sbal_check(adapter), 5 * HZ);
S
Swen Schillig 已提交
672 673
	if (ret > 0)
		return 0;
674
	if (!ret) {
675
		atomic_inc(&adapter->qdio_outb_full);
676 677 678
		/* assume hanging outbound queue, try queue recovery */
		zfcp_erp_adapter_reopen(adapter, 0, "fsrsg_1", NULL);
	}
679 680

	spin_lock_bh(&adapter->req_q_lock);
S
Swen Schillig 已提交
681 682 683 684 685 686 687 688 689 690
	return -EIO;
}

static struct zfcp_fsf_req *zfcp_fsf_alloc_noqtcb(mempool_t *pool)
{
	struct zfcp_fsf_req *req;
	req = mempool_alloc(pool, GFP_ATOMIC);
	if (!req)
		return NULL;
	memset(req, 0, sizeof(*req));
691
	req->pool = pool;
S
Swen Schillig 已提交
692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716
	return req;
}

static struct zfcp_fsf_req *zfcp_fsf_alloc_qtcb(mempool_t *pool)
{
	struct zfcp_fsf_req_qtcb *qtcb;

	if (likely(pool))
		qtcb = mempool_alloc(pool, GFP_ATOMIC);
	else
		qtcb = kmem_cache_alloc(zfcp_data.fsf_req_qtcb_cache,
					GFP_ATOMIC);
	if (unlikely(!qtcb))
		return NULL;

	memset(qtcb, 0, sizeof(*qtcb));
	qtcb->fsf_req.qtcb = &qtcb->qtcb;
	qtcb->fsf_req.pool = pool;

	return &qtcb->fsf_req;
}

static struct zfcp_fsf_req *zfcp_fsf_req_create(struct zfcp_adapter *adapter,
						u32 fsf_cmd, int req_flags,
						mempool_t *pool)
L
Linus Torvalds 已提交
717
{
718
	struct qdio_buffer_element *sbale;
L
Linus Torvalds 已提交
719

S
Swen Schillig 已提交
720 721
	struct zfcp_fsf_req *req;
	struct zfcp_qdio_queue *req_q = &adapter->req_q;
722

S
Swen Schillig 已提交
723 724 725 726
	if (req_flags & ZFCP_REQ_NO_QTCB)
		req = zfcp_fsf_alloc_noqtcb(pool);
	else
		req = zfcp_fsf_alloc_qtcb(pool);
L
Linus Torvalds 已提交
727

S
Swen Schillig 已提交
728
	if (unlikely(!req))
729
		return ERR_PTR(-ENOMEM);
L
Linus Torvalds 已提交
730

S
Swen Schillig 已提交
731 732
	if (adapter->req_no == 0)
		adapter->req_no++;
L
Linus Torvalds 已提交
733

S
Swen Schillig 已提交
734 735 736
	INIT_LIST_HEAD(&req->list);
	init_timer(&req->timer);
	init_waitqueue_head(&req->completion_wq);
L
Linus Torvalds 已提交
737

S
Swen Schillig 已提交
738 739
	req->adapter = adapter;
	req->fsf_command = fsf_cmd;
740
	req->req_id = adapter->req_no;
S
Swen Schillig 已提交
741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767
	req->sbal_number = 1;
	req->sbal_first = req_q->first;
	req->sbal_last = req_q->first;
	req->sbale_curr = 1;

	sbale = zfcp_qdio_sbale_req(req);
	sbale[0].addr = (void *) req->req_id;
	sbale[0].flags |= SBAL_FLAGS0_COMMAND;

	if (likely(req->qtcb)) {
		req->qtcb->prefix.req_seq_no = req->adapter->fsf_req_seq_no;
		req->qtcb->prefix.req_id = req->req_id;
		req->qtcb->prefix.ulp_info = 26;
		req->qtcb->prefix.qtcb_type = fsf_qtcb_type[req->fsf_command];
		req->qtcb->prefix.qtcb_version = FSF_QTCB_CURRENT_VERSION;
		req->qtcb->header.req_handle = req->req_id;
		req->qtcb->header.fsf_command = req->fsf_command;
		req->seq_no = adapter->fsf_req_seq_no;
		req->qtcb->prefix.req_seq_no = adapter->fsf_req_seq_no;
		sbale[1].addr = (void *) req->qtcb;
		sbale[1].length = sizeof(struct fsf_qtcb);
	}

	if (!(atomic_read(&adapter->status) & ZFCP_STATUS_ADAPTER_QDIOUP)) {
		zfcp_fsf_req_free(req);
		return ERR_PTR(-EIO);
	}
768

S
Swen Schillig 已提交
769 770
	if (likely(req_flags & ZFCP_REQ_AUTO_CLEANUP))
		req->status |= ZFCP_STATUS_FSFREQ_CLEANUP;
L
Linus Torvalds 已提交
771

S
Swen Schillig 已提交
772
	return req;
L
Linus Torvalds 已提交
773 774
}

S
Swen Schillig 已提交
775 776 777
static int zfcp_fsf_req_send(struct zfcp_fsf_req *req)
{
	struct zfcp_adapter *adapter = req->adapter;
778 779 780
	unsigned long	     flags;
	int		     idx;
	int		     with_qtcb = (req->qtcb != NULL);
S
Swen Schillig 已提交
781 782

	/* put allocated FSF request into hash table */
783
	spin_lock_irqsave(&adapter->req_list_lock, flags);
S
Swen Schillig 已提交
784 785
	idx = zfcp_reqlist_hash(req->req_id);
	list_add_tail(&req->list, &adapter->req_list[idx]);
786
	spin_unlock_irqrestore(&adapter->req_list_lock, flags);
S
Swen Schillig 已提交
787

788
	req->qdio_outb_usage = atomic_read(&adapter->req_q.count);
S
Swen Schillig 已提交
789 790 791
	req->issued = get_clock();
	if (zfcp_qdio_send(req)) {
		del_timer(&req->timer);
792 793 794 795 796
		spin_lock_irqsave(&adapter->req_list_lock, flags);
		/* lookup request again, list might have changed */
		if (zfcp_reqlist_find_safe(adapter, req))
			zfcp_reqlist_remove(adapter, req);
		spin_unlock_irqrestore(&adapter->req_list_lock, flags);
797
		zfcp_erp_adapter_reopen(adapter, 0, "fsrs__1", req);
S
Swen Schillig 已提交
798 799 800 801
		return -EIO;
	}

	/* Don't increase for unsolicited status */
802
	if (with_qtcb)
S
Swen Schillig 已提交
803
		adapter->fsf_req_seq_no++;
804
	adapter->req_no++;
S
Swen Schillig 已提交
805 806 807 808 809 810 811 812 813

	return 0;
}

/**
 * zfcp_fsf_status_read - send status read request
 * @adapter: pointer to struct zfcp_adapter
 * @req_flags: request flags
 * Returns: 0 on success, ERROR otherwise
L
Linus Torvalds 已提交
814
 */
S
Swen Schillig 已提交
815
int zfcp_fsf_status_read(struct zfcp_adapter *adapter)
L
Linus Torvalds 已提交
816
{
S
Swen Schillig 已提交
817 818
	struct zfcp_fsf_req *req;
	struct fsf_status_read_buffer *sr_buf;
819
	struct qdio_buffer_element *sbale;
S
Swen Schillig 已提交
820
	int retval = -EIO;
L
Linus Torvalds 已提交
821

822
	spin_lock_bh(&adapter->req_q_lock);
S
Swen Schillig 已提交
823 824 825 826 827 828
	if (zfcp_fsf_req_sbal_get(adapter))
		goto out;

	req = zfcp_fsf_req_create(adapter, FSF_QTCB_UNSOLICITED_STATUS,
				  ZFCP_REQ_NO_QTCB,
				  adapter->pool.fsf_req_status_read);
829
	if (IS_ERR(req)) {
S
Swen Schillig 已提交
830 831
		retval = PTR_ERR(req);
		goto out;
L
Linus Torvalds 已提交
832 833
	}

S
Swen Schillig 已提交
834 835 836 837 838 839 840 841 842 843 844 845 846 847
	sbale = zfcp_qdio_sbale_req(req);
	sbale[2].flags |= SBAL_FLAGS_LAST_ENTRY;
	req->sbale_curr = 2;

	sr_buf = mempool_alloc(adapter->pool.data_status_read, GFP_ATOMIC);
	if (!sr_buf) {
		retval = -ENOMEM;
		goto failed_buf;
	}
	memset(sr_buf, 0, sizeof(*sr_buf));
	req->data = sr_buf;
	sbale = zfcp_qdio_sbale_curr(req);
	sbale->addr = (void *) sr_buf;
	sbale->length = sizeof(*sr_buf);
848

S
Swen Schillig 已提交
849 850 851
	retval = zfcp_fsf_req_send(req);
	if (retval)
		goto failed_req_send;
L
Linus Torvalds 已提交
852

S
Swen Schillig 已提交
853 854 855 856 857 858 859 860
	goto out;

failed_req_send:
	mempool_free(sr_buf, adapter->pool.data_status_read);
failed_buf:
	zfcp_fsf_req_free(req);
	zfcp_hba_dbf_event_fsf_unsol("fail", adapter, NULL);
out:
861
	spin_unlock_bh(&adapter->req_q_lock);
S
Swen Schillig 已提交
862 863 864 865 866 867 868 869 870 871 872 873
	return retval;
}

static void zfcp_fsf_abort_fcp_command_handler(struct zfcp_fsf_req *req)
{
	struct zfcp_unit *unit = req->data;
	union fsf_status_qual *fsq = &req->qtcb->header.fsf_status_qual;

	if (req->status & ZFCP_STATUS_FSFREQ_ERROR)
		return;

	switch (req->qtcb->header.fsf_status) {
L
Linus Torvalds 已提交
874
	case FSF_PORT_HANDLE_NOT_VALID:
S
Swen Schillig 已提交
875
		if (fsq->word[0] == fsq->word[1]) {
876 877
			zfcp_erp_adapter_reopen(unit->port->adapter, 0,
						"fsafch1", req);
S
Swen Schillig 已提交
878
			req->status |= ZFCP_STATUS_FSFREQ_ERROR;
L
Linus Torvalds 已提交
879 880 881
		}
		break;
	case FSF_LUN_HANDLE_NOT_VALID:
S
Swen Schillig 已提交
882
		if (fsq->word[0] == fsq->word[1]) {
883
			zfcp_erp_port_reopen(unit->port, 0, "fsafch2", req);
S
Swen Schillig 已提交
884
			req->status |= ZFCP_STATUS_FSFREQ_ERROR;
L
Linus Torvalds 已提交
885 886 887
		}
		break;
	case FSF_FCP_COMMAND_DOES_NOT_EXIST:
S
Swen Schillig 已提交
888
		req->status |= ZFCP_STATUS_FSFREQ_ABORTNOTNEEDED;
L
Linus Torvalds 已提交
889 890
		break;
	case FSF_PORT_BOXED:
891
		zfcp_erp_port_boxed(unit->port, "fsafch3", req);
S
Swen Schillig 已提交
892 893
		req->status |= ZFCP_STATUS_FSFREQ_ERROR |
			       ZFCP_STATUS_FSFREQ_RETRY;
L
Linus Torvalds 已提交
894 895
		break;
	case FSF_LUN_BOXED:
896
		zfcp_erp_unit_boxed(unit, "fsafch4", req);
S
Swen Schillig 已提交
897 898
		req->status |= ZFCP_STATUS_FSFREQ_ERROR |
			       ZFCP_STATUS_FSFREQ_RETRY;
L
Linus Torvalds 已提交
899 900
                break;
	case FSF_ADAPTER_STATUS_AVAILABLE:
S
Swen Schillig 已提交
901
		switch (fsq->word[0]) {
L
Linus Torvalds 已提交
902
		case FSF_SQ_INVOKE_LINK_TEST_PROCEDURE:
903
			zfcp_test_link(unit->port);
904
			/* fall through */
L
Linus Torvalds 已提交
905
		case FSF_SQ_ULP_DEPENDENT_ERP_REQUIRED:
S
Swen Schillig 已提交
906
			req->status |= ZFCP_STATUS_FSFREQ_ERROR;
L
Linus Torvalds 已提交
907 908 909 910
			break;
		}
		break;
	case FSF_GOOD:
S
Swen Schillig 已提交
911
		req->status |= ZFCP_STATUS_FSFREQ_ABORTSUCCEEDED;
L
Linus Torvalds 已提交
912 913 914 915 916
		break;
	}
}

/**
S
Swen Schillig 已提交
917 918 919 920
 * zfcp_fsf_abort_fcp_command - abort running SCSI command
 * @old_req_id: unsigned long
 * @unit: pointer to struct zfcp_unit
 * Returns: pointer to struct zfcp_fsf_req
L
Linus Torvalds 已提交
921 922
 */

S
Swen Schillig 已提交
923
struct zfcp_fsf_req *zfcp_fsf_abort_fcp_command(unsigned long old_req_id,
924
						struct zfcp_unit *unit)
L
Linus Torvalds 已提交
925
{
926
	struct qdio_buffer_element *sbale;
S
Swen Schillig 已提交
927
	struct zfcp_fsf_req *req = NULL;
928
	struct zfcp_adapter *adapter = unit->port->adapter;
929

930 931
	spin_lock_bh(&adapter->req_q_lock);
	if (zfcp_fsf_req_sbal_get(adapter))
S
Swen Schillig 已提交
932 933
		goto out;
	req = zfcp_fsf_req_create(adapter, FSF_QTCB_ABORT_FCP_CMND,
934
				  0, adapter->pool.fsf_req_abort);
935 936
	if (IS_ERR(req)) {
		req = NULL;
S
Swen Schillig 已提交
937
		goto out;
938
	}
939

S
Swen Schillig 已提交
940 941 942
	if (unlikely(!(atomic_read(&unit->status) &
		       ZFCP_STATUS_COMMON_UNBLOCKED)))
		goto out_error_free;
L
Linus Torvalds 已提交
943

S
Swen Schillig 已提交
944 945 946
	sbale = zfcp_qdio_sbale_req(req);
	sbale[0].flags |= SBAL_FLAGS0_TYPE_READ;
	sbale[1].flags |= SBAL_FLAGS_LAST_ENTRY;
L
Linus Torvalds 已提交
947

S
Swen Schillig 已提交
948 949 950 951 952 953 954 955 956 957 958 959 960 961
	req->data = unit;
	req->handler = zfcp_fsf_abort_fcp_command_handler;
	req->qtcb->header.lun_handle = unit->handle;
	req->qtcb->header.port_handle = unit->port->handle;
	req->qtcb->bottom.support.req_handle = (u64) old_req_id;

	zfcp_fsf_start_timer(req, ZFCP_SCSI_ER_TIMEOUT);
	if (!zfcp_fsf_req_send(req))
		goto out;

out_error_free:
	zfcp_fsf_req_free(req);
	req = NULL;
out:
962
	spin_unlock_bh(&adapter->req_q_lock);
S
Swen Schillig 已提交
963
	return req;
L
Linus Torvalds 已提交
964 965
}

S
Swen Schillig 已提交
966
static void zfcp_fsf_send_ct_handler(struct zfcp_fsf_req *req)
L
Linus Torvalds 已提交
967
{
S
Swen Schillig 已提交
968 969 970
	struct zfcp_adapter *adapter = req->adapter;
	struct zfcp_send_ct *send_ct = req->data;
	struct fsf_qtcb_header *header = &req->qtcb->header;
L
Linus Torvalds 已提交
971

S
Swen Schillig 已提交
972
	send_ct->status = -EINVAL;
L
Linus Torvalds 已提交
973

S
Swen Schillig 已提交
974
	if (req->status & ZFCP_STATUS_FSFREQ_ERROR)
L
Linus Torvalds 已提交
975 976 977 978
		goto skip_fsfstatus;

	switch (header->fsf_status) {
        case FSF_GOOD:
S
Swen Schillig 已提交
979 980
		zfcp_san_dbf_event_ct_response(req);
		send_ct->status = 0;
L
Linus Torvalds 已提交
981 982
		break;
        case FSF_SERVICE_CLASS_NOT_SUPPORTED:
S
Swen Schillig 已提交
983
		zfcp_fsf_class_not_supp(req);
L
Linus Torvalds 已提交
984 985 986 987 988
		break;
        case FSF_ADAPTER_STATUS_AVAILABLE:
                switch (header->fsf_status_qual.word[0]){
                case FSF_SQ_INVOKE_LINK_TEST_PROCEDURE:
                case FSF_SQ_ULP_DEPENDENT_ERP_REQUIRED:
S
Swen Schillig 已提交
989
			req->status |= ZFCP_STATUS_FSFREQ_ERROR;
L
Linus Torvalds 已提交
990 991 992 993 994 995
			break;
                }
                break;
	case FSF_ACCESS_DENIED:
		break;
        case FSF_PORT_BOXED:
S
Swen Schillig 已提交
996 997
		req->status |= ZFCP_STATUS_FSFREQ_ERROR |
			       ZFCP_STATUS_FSFREQ_RETRY;
L
Linus Torvalds 已提交
998
		break;
S
Swen Schillig 已提交
999
	case FSF_PORT_HANDLE_NOT_VALID:
1000
		zfcp_erp_adapter_reopen(adapter, 0, "fsscth1", req);
1001
		/* fall through */
S
Swen Schillig 已提交
1002
	case FSF_GENERIC_COMMAND_REJECTED:
L
Linus Torvalds 已提交
1003 1004 1005 1006
	case FSF_PAYLOAD_SIZE_MISMATCH:
	case FSF_REQUEST_SIZE_TOO_LARGE:
	case FSF_RESPONSE_SIZE_TOO_LARGE:
	case FSF_SBAL_MISMATCH:
S
Swen Schillig 已提交
1007
		req->status |= ZFCP_STATUS_FSFREQ_ERROR;
L
Linus Torvalds 已提交
1008 1009 1010 1011
		break;
	}

skip_fsfstatus:
S
Swen Schillig 已提交
1012
	if (send_ct->handler)
L
Linus Torvalds 已提交
1013
		send_ct->handler(send_ct->handler_data);
S
Swen Schillig 已提交
1014
}
L
Linus Torvalds 已提交
1015

1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032
static void zfcp_fsf_setup_ct_els_unchained(struct qdio_buffer_element *sbale,
					    struct scatterlist *sg_req,
					    struct scatterlist *sg_resp)
{
	sbale[0].flags |= SBAL_FLAGS0_TYPE_WRITE_READ;
	sbale[2].addr   = sg_virt(sg_req);
	sbale[2].length = sg_req->length;
	sbale[3].addr   = sg_virt(sg_resp);
	sbale[3].length = sg_resp->length;
	sbale[3].flags |= SBAL_FLAGS_LAST_ENTRY;
}

static int zfcp_fsf_one_sbal(struct scatterlist *sg)
{
	return sg_is_last(sg) && sg->length <= PAGE_SIZE;
}

1033 1034 1035 1036
static int zfcp_fsf_setup_ct_els_sbals(struct zfcp_fsf_req *req,
				       struct scatterlist *sg_req,
				       struct scatterlist *sg_resp,
				       int max_sbals)
S
Swen Schillig 已提交
1037
{
1038 1039
	struct qdio_buffer_element *sbale = zfcp_qdio_sbale_req(req);
	u32 feat = req->adapter->adapter_features;
S
Swen Schillig 已提交
1040 1041
	int bytes;

1042
	if (!(feat & FSF_FEATURE_ELS_CT_CHAINED_SBALS)) {
1043
		if (!zfcp_fsf_one_sbal(sg_req) || !zfcp_fsf_one_sbal(sg_resp))
1044 1045
			return -EOPNOTSUPP;

1046 1047 1048 1049 1050 1051 1052
		zfcp_fsf_setup_ct_els_unchained(sbale, sg_req, sg_resp);
		return 0;
	}

	/* use single, unchained SBAL if it can hold the request */
	if (zfcp_fsf_one_sbal(sg_req) && zfcp_fsf_one_sbal(sg_resp)) {
		zfcp_fsf_setup_ct_els_unchained(sbale, sg_req, sg_resp);
1053 1054 1055
		return 0;
	}

S
Swen Schillig 已提交
1056 1057 1058
	bytes = zfcp_qdio_sbals_from_sg(req, SBAL_FLAGS0_TYPE_WRITE_READ,
					sg_req, max_sbals);
	if (bytes <= 0)
1059
		return -EIO;
S
Swen Schillig 已提交
1060 1061 1062 1063 1064 1065
	req->qtcb->bottom.support.req_buf_length = bytes;
	req->sbale_curr = ZFCP_LAST_SBALE_PER_SBAL;

	bytes = zfcp_qdio_sbals_from_sg(req, SBAL_FLAGS0_TYPE_WRITE_READ,
					sg_resp, max_sbals);
	if (bytes <= 0)
1066
		return -EIO;
S
Swen Schillig 已提交
1067 1068 1069
	req->qtcb->bottom.support.resp_buf_length = bytes;

	return 0;
L
Linus Torvalds 已提交
1070 1071 1072
}

/**
S
Swen Schillig 已提交
1073 1074 1075 1076
 * zfcp_fsf_send_ct - initiate a Generic Service request (FC-GS)
 * @ct: pointer to struct zfcp_send_ct with data for request
 * @pool: if non-null this mempool is used to allocate struct zfcp_fsf_req
 * @erp_action: if non-null the Generic Service request sent within ERP
L
Linus Torvalds 已提交
1077
 */
S
Swen Schillig 已提交
1078 1079
int zfcp_fsf_send_ct(struct zfcp_send_ct *ct, mempool_t *pool,
		     struct zfcp_erp_action *erp_action)
L
Linus Torvalds 已提交
1080
{
1081 1082
	struct zfcp_wka_port *wka_port = ct->wka_port;
	struct zfcp_adapter *adapter = wka_port->adapter;
S
Swen Schillig 已提交
1083 1084
	struct zfcp_fsf_req *req;
	int ret = -EIO;
L
Linus Torvalds 已提交
1085

1086
	spin_lock_bh(&adapter->req_q_lock);
S
Swen Schillig 已提交
1087 1088
	if (zfcp_fsf_req_sbal_get(adapter))
		goto out;
L
Linus Torvalds 已提交
1089

S
Swen Schillig 已提交
1090 1091
	req = zfcp_fsf_req_create(adapter, FSF_QTCB_SEND_GENERIC,
				  ZFCP_REQ_AUTO_CLEANUP, pool);
1092
	if (IS_ERR(req)) {
S
Swen Schillig 已提交
1093 1094
		ret = PTR_ERR(req);
		goto out;
1095 1096
	}

1097 1098
	ret = zfcp_fsf_setup_ct_els_sbals(req, ct->req, ct->resp,
					  FSF_MAX_SBALS_PER_REQ);
C
Christof Schmitt 已提交
1099
	if (ret)
L
Linus Torvalds 已提交
1100 1101
		goto failed_send;

S
Swen Schillig 已提交
1102
	req->handler = zfcp_fsf_send_ct_handler;
1103
	req->qtcb->header.port_handle = wka_port->handle;
S
Swen Schillig 已提交
1104 1105 1106 1107 1108 1109 1110 1111 1112
	req->qtcb->bottom.support.service_class = FSF_CLASS_3;
	req->qtcb->bottom.support.timeout = ct->timeout;
	req->data = ct;

	zfcp_san_dbf_event_ct_request(req);

	if (erp_action) {
		erp_action->fsf_req = req;
		req->erp_action = erp_action;
1113
		zfcp_fsf_start_erp_timer(req);
S
Swen Schillig 已提交
1114 1115
	} else
		zfcp_fsf_start_timer(req, ZFCP_FSF_REQUEST_TIMEOUT);
L
Linus Torvalds 已提交
1116

S
Swen Schillig 已提交
1117 1118 1119
	ret = zfcp_fsf_req_send(req);
	if (ret)
		goto failed_send;
L
Linus Torvalds 已提交
1120

S
Swen Schillig 已提交
1121
	goto out;
L
Linus Torvalds 已提交
1122

S
Swen Schillig 已提交
1123 1124 1125 1126 1127
failed_send:
	zfcp_fsf_req_free(req);
	if (erp_action)
		erp_action->fsf_req = NULL;
out:
1128
	spin_unlock_bh(&adapter->req_q_lock);
S
Swen Schillig 已提交
1129
	return ret;
L
Linus Torvalds 已提交
1130 1131
}

S
Swen Schillig 已提交
1132
static void zfcp_fsf_send_els_handler(struct zfcp_fsf_req *req)
L
Linus Torvalds 已提交
1133
{
S
Swen Schillig 已提交
1134 1135 1136
	struct zfcp_send_els *send_els = req->data;
	struct zfcp_port *port = send_els->port;
	struct fsf_qtcb_header *header = &req->qtcb->header;
L
Linus Torvalds 已提交
1137

S
Swen Schillig 已提交
1138
	send_els->status = -EINVAL;
L
Linus Torvalds 已提交
1139

S
Swen Schillig 已提交
1140
	if (req->status & ZFCP_STATUS_FSFREQ_ERROR)
L
Linus Torvalds 已提交
1141 1142 1143 1144
		goto skip_fsfstatus;

	switch (header->fsf_status) {
	case FSF_GOOD:
S
Swen Schillig 已提交
1145 1146
		zfcp_san_dbf_event_els_response(req);
		send_els->status = 0;
L
Linus Torvalds 已提交
1147 1148
		break;
	case FSF_SERVICE_CLASS_NOT_SUPPORTED:
S
Swen Schillig 已提交
1149
		zfcp_fsf_class_not_supp(req);
L
Linus Torvalds 已提交
1150 1151 1152 1153
		break;
	case FSF_ADAPTER_STATUS_AVAILABLE:
		switch (header->fsf_status_qual.word[0]){
		case FSF_SQ_INVOKE_LINK_TEST_PROCEDURE:
1154 1155
			if (port && (send_els->ls_code != ZFCP_LS_ADISC))
				zfcp_test_link(port);
S
Swen Schillig 已提交
1156
			/*fall through */
L
Linus Torvalds 已提交
1157 1158
		case FSF_SQ_ULP_DEPENDENT_ERP_REQUIRED:
		case FSF_SQ_RETRY_IF_POSSIBLE:
S
Swen Schillig 已提交
1159
			req->status |= ZFCP_STATUS_FSFREQ_ERROR;
L
Linus Torvalds 已提交
1160 1161 1162 1163 1164 1165 1166 1167 1168
			break;
		}
		break;
	case FSF_ELS_COMMAND_REJECTED:
	case FSF_PAYLOAD_SIZE_MISMATCH:
	case FSF_REQUEST_SIZE_TOO_LARGE:
	case FSF_RESPONSE_SIZE_TOO_LARGE:
		break;
	case FSF_ACCESS_DENIED:
1169 1170
		if (port)
			zfcp_fsf_access_denied_port(req, port);
L
Linus Torvalds 已提交
1171
		break;
S
Swen Schillig 已提交
1172 1173 1174
	case FSF_SBAL_MISMATCH:
		/* should never occure, avoided in zfcp_fsf_send_els */
		/* fall through */
L
Linus Torvalds 已提交
1175
	default:
S
Swen Schillig 已提交
1176
		req->status |= ZFCP_STATUS_FSFREQ_ERROR;
L
Linus Torvalds 已提交
1177 1178 1179
		break;
	}
skip_fsfstatus:
H
Heiko Carstens 已提交
1180
	if (send_els->handler)
L
Linus Torvalds 已提交
1181
		send_els->handler(send_els->handler_data);
S
Swen Schillig 已提交
1182
}
L
Linus Torvalds 已提交
1183

S
Swen Schillig 已提交
1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194
/**
 * zfcp_fsf_send_els - initiate an ELS command (FC-FS)
 * @els: pointer to struct zfcp_send_els with data for the command
 */
int zfcp_fsf_send_els(struct zfcp_send_els *els)
{
	struct zfcp_fsf_req *req;
	struct zfcp_adapter *adapter = els->adapter;
	struct fsf_qtcb_bottom_support *bottom;
	int ret = -EIO;

1195 1196
	spin_lock_bh(&adapter->req_q_lock);
	if (zfcp_fsf_req_sbal_get(adapter))
S
Swen Schillig 已提交
1197 1198 1199
		goto out;
	req = zfcp_fsf_req_create(adapter, FSF_QTCB_SEND_ELS,
				  ZFCP_REQ_AUTO_CLEANUP, NULL);
1200
	if (IS_ERR(req)) {
S
Swen Schillig 已提交
1201 1202 1203 1204
		ret = PTR_ERR(req);
		goto out;
	}

1205
	ret = zfcp_fsf_setup_ct_els_sbals(req, els->req, els->resp, 2);
1206

S
Swen Schillig 已提交
1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228
	if (ret)
		goto failed_send;

	bottom = &req->qtcb->bottom.support;
	req->handler = zfcp_fsf_send_els_handler;
	bottom->d_id = els->d_id;
	bottom->service_class = FSF_CLASS_3;
	bottom->timeout = 2 * R_A_TOV;
	req->data = els;

	zfcp_san_dbf_event_els_request(req);

	zfcp_fsf_start_timer(req, ZFCP_FSF_REQUEST_TIMEOUT);
	ret = zfcp_fsf_req_send(req);
	if (ret)
		goto failed_send;

	goto out;

failed_send:
	zfcp_fsf_req_free(req);
out:
1229
	spin_unlock_bh(&adapter->req_q_lock);
S
Swen Schillig 已提交
1230
	return ret;
L
Linus Torvalds 已提交
1231 1232
}

S
Swen Schillig 已提交
1233
int zfcp_fsf_exchange_config_data(struct zfcp_erp_action *erp_action)
L
Linus Torvalds 已提交
1234
{
1235
	struct qdio_buffer_element *sbale;
S
Swen Schillig 已提交
1236
	struct zfcp_fsf_req *req;
1237
	struct zfcp_adapter *adapter = erp_action->adapter;
S
Swen Schillig 已提交
1238 1239
	int retval = -EIO;

1240
	spin_lock_bh(&adapter->req_q_lock);
1241
	if (zfcp_fsf_req_sbal_get(adapter))
S
Swen Schillig 已提交
1242 1243 1244 1245 1246
		goto out;
	req = zfcp_fsf_req_create(adapter,
				  FSF_QTCB_EXCHANGE_CONFIG_DATA,
				  ZFCP_REQ_AUTO_CLEANUP,
				  adapter->pool.fsf_req_erp);
1247
	if (IS_ERR(req)) {
S
Swen Schillig 已提交
1248 1249
		retval = PTR_ERR(req);
		goto out;
L
Linus Torvalds 已提交
1250 1251
	}

S
Swen Schillig 已提交
1252
	sbale = zfcp_qdio_sbale_req(req);
1253 1254
	sbale[0].flags |= SBAL_FLAGS0_TYPE_READ;
	sbale[1].flags |= SBAL_FLAGS_LAST_ENTRY;
L
Linus Torvalds 已提交
1255

S
Swen Schillig 已提交
1256
	req->qtcb->bottom.config.feature_selection =
1257 1258
			FSF_FEATURE_CFDC |
			FSF_FEATURE_LUN_SHARING |
1259
			FSF_FEATURE_NOTIFICATION_LOST |
1260
			FSF_FEATURE_UPDATE_ALERT;
S
Swen Schillig 已提交
1261 1262 1263
	req->erp_action = erp_action;
	req->handler = zfcp_fsf_exchange_config_data_handler;
	erp_action->fsf_req = req;
L
Linus Torvalds 已提交
1264

1265
	zfcp_fsf_start_erp_timer(req);
S
Swen Schillig 已提交
1266
	retval = zfcp_fsf_req_send(req);
L
Linus Torvalds 已提交
1267
	if (retval) {
S
Swen Schillig 已提交
1268
		zfcp_fsf_req_free(req);
L
Linus Torvalds 已提交
1269 1270
		erp_action->fsf_req = NULL;
	}
S
Swen Schillig 已提交
1271
out:
1272
	spin_unlock_bh(&adapter->req_q_lock);
1273 1274
	return retval;
}
L
Linus Torvalds 已提交
1275

S
Swen Schillig 已提交
1276 1277
int zfcp_fsf_exchange_config_data_sync(struct zfcp_adapter *adapter,
				       struct fsf_qtcb_bottom_config *data)
1278
{
1279
	struct qdio_buffer_element *sbale;
S
Swen Schillig 已提交
1280 1281 1282
	struct zfcp_fsf_req *req = NULL;
	int retval = -EIO;

1283
	spin_lock_bh(&adapter->req_q_lock);
S
Swen Schillig 已提交
1284
	if (zfcp_fsf_req_sbal_get(adapter))
1285
		goto out_unlock;
S
Swen Schillig 已提交
1286 1287 1288

	req = zfcp_fsf_req_create(adapter, FSF_QTCB_EXCHANGE_CONFIG_DATA,
				  0, NULL);
1289
	if (IS_ERR(req)) {
S
Swen Schillig 已提交
1290
		retval = PTR_ERR(req);
1291
		goto out_unlock;
1292 1293
	}

S
Swen Schillig 已提交
1294
	sbale = zfcp_qdio_sbale_req(req);
1295 1296
	sbale[0].flags |= SBAL_FLAGS0_TYPE_READ;
	sbale[1].flags |= SBAL_FLAGS_LAST_ENTRY;
S
Swen Schillig 已提交
1297
	req->handler = zfcp_fsf_exchange_config_data_handler;
1298

S
Swen Schillig 已提交
1299
	req->qtcb->bottom.config.feature_selection =
1300 1301 1302 1303 1304 1305
			FSF_FEATURE_CFDC |
			FSF_FEATURE_LUN_SHARING |
			FSF_FEATURE_NOTIFICATION_LOST |
			FSF_FEATURE_UPDATE_ALERT;

	if (data)
S
Swen Schillig 已提交
1306
		req->data = data;
1307

S
Swen Schillig 已提交
1308 1309
	zfcp_fsf_start_timer(req, ZFCP_FSF_REQUEST_TIMEOUT);
	retval = zfcp_fsf_req_send(req);
1310
	spin_unlock_bh(&adapter->req_q_lock);
C
Christof Schmitt 已提交
1311
	if (!retval)
S
Swen Schillig 已提交
1312 1313
		wait_event(req->completion_wq,
			   req->status & ZFCP_STATUS_FSFREQ_COMPLETED);
1314

S
Swen Schillig 已提交
1315
	zfcp_fsf_req_free(req);
1316
	return retval;
1317

1318 1319
out_unlock:
	spin_unlock_bh(&adapter->req_q_lock);
L
Linus Torvalds 已提交
1320 1321 1322 1323 1324
	return retval;
}

/**
 * zfcp_fsf_exchange_port_data - request information about local port
1325
 * @erp_action: ERP action for the adapter for which port data is requested
S
Swen Schillig 已提交
1326
 * Returns: 0 on success, error otherwise
L
Linus Torvalds 已提交
1327
 */
S
Swen Schillig 已提交
1328
int zfcp_fsf_exchange_port_data(struct zfcp_erp_action *erp_action)
L
Linus Torvalds 已提交
1329
{
1330
	struct qdio_buffer_element *sbale;
S
Swen Schillig 已提交
1331
	struct zfcp_fsf_req *req;
1332
	struct zfcp_adapter *adapter = erp_action->adapter;
S
Swen Schillig 已提交
1333
	int retval = -EIO;
L
Linus Torvalds 已提交
1334

C
Christof Schmitt 已提交
1335
	if (!(adapter->adapter_features & FSF_FEATURE_HBAAPI_MANAGEMENT))
1336
		return -EOPNOTSUPP;
L
Linus Torvalds 已提交
1337

1338
	spin_lock_bh(&adapter->req_q_lock);
1339
	if (zfcp_fsf_req_sbal_get(adapter))
S
Swen Schillig 已提交
1340 1341 1342 1343
		goto out;
	req = zfcp_fsf_req_create(adapter, FSF_QTCB_EXCHANGE_PORT_DATA,
				  ZFCP_REQ_AUTO_CLEANUP,
				  adapter->pool.fsf_req_erp);
1344
	if (IS_ERR(req)) {
S
Swen Schillig 已提交
1345 1346
		retval = PTR_ERR(req);
		goto out;
1347 1348
	}

S
Swen Schillig 已提交
1349
	sbale = zfcp_qdio_sbale_req(req);
1350 1351
	sbale[0].flags |= SBAL_FLAGS0_TYPE_READ;
	sbale[1].flags |= SBAL_FLAGS_LAST_ENTRY;
L
Linus Torvalds 已提交
1352

S
Swen Schillig 已提交
1353 1354 1355
	req->handler = zfcp_fsf_exchange_port_data_handler;
	req->erp_action = erp_action;
	erp_action->fsf_req = req;
1356

1357
	zfcp_fsf_start_erp_timer(req);
S
Swen Schillig 已提交
1358
	retval = zfcp_fsf_req_send(req);
L
Linus Torvalds 已提交
1359
	if (retval) {
S
Swen Schillig 已提交
1360
		zfcp_fsf_req_free(req);
1361 1362
		erp_action->fsf_req = NULL;
	}
S
Swen Schillig 已提交
1363
out:
1364
	spin_unlock_bh(&adapter->req_q_lock);
1365 1366 1367 1368 1369
	return retval;
}

/**
 * zfcp_fsf_exchange_port_data_sync - request information about local port
S
Swen Schillig 已提交
1370 1371 1372
 * @adapter: pointer to struct zfcp_adapter
 * @data: pointer to struct fsf_qtcb_bottom_port
 * Returns: 0 on success, error otherwise
1373
 */
S
Swen Schillig 已提交
1374 1375
int zfcp_fsf_exchange_port_data_sync(struct zfcp_adapter *adapter,
				     struct fsf_qtcb_bottom_port *data)
1376
{
1377
	struct qdio_buffer_element *sbale;
S
Swen Schillig 已提交
1378 1379
	struct zfcp_fsf_req *req = NULL;
	int retval = -EIO;
1380

C
Christof Schmitt 已提交
1381
	if (!(adapter->adapter_features & FSF_FEATURE_HBAAPI_MANAGEMENT))
1382 1383
		return -EOPNOTSUPP;

1384
	spin_lock_bh(&adapter->req_q_lock);
1385
	if (zfcp_fsf_req_sbal_get(adapter))
1386
		goto out_unlock;
S
Swen Schillig 已提交
1387 1388 1389

	req = zfcp_fsf_req_create(adapter, FSF_QTCB_EXCHANGE_PORT_DATA, 0,
				  NULL);
1390
	if (IS_ERR(req)) {
S
Swen Schillig 已提交
1391
		retval = PTR_ERR(req);
1392
		goto out_unlock;
L
Linus Torvalds 已提交
1393 1394
	}

1395
	if (data)
S
Swen Schillig 已提交
1396
		req->data = data;
1397

S
Swen Schillig 已提交
1398
	sbale = zfcp_qdio_sbale_req(req);
1399 1400 1401
	sbale[0].flags |= SBAL_FLAGS0_TYPE_READ;
	sbale[1].flags |= SBAL_FLAGS_LAST_ENTRY;

S
Swen Schillig 已提交
1402 1403 1404
	req->handler = zfcp_fsf_exchange_port_data_handler;
	zfcp_fsf_start_timer(req, ZFCP_FSF_REQUEST_TIMEOUT);
	retval = zfcp_fsf_req_send(req);
1405
	spin_unlock_bh(&adapter->req_q_lock);
1406

C
Christof Schmitt 已提交
1407
	if (!retval)
S
Swen Schillig 已提交
1408 1409 1410
		wait_event(req->completion_wq,
			   req->status & ZFCP_STATUS_FSFREQ_COMPLETED);
	zfcp_fsf_req_free(req);
L
Linus Torvalds 已提交
1411 1412

	return retval;
1413 1414 1415 1416

out_unlock:
	spin_unlock_bh(&adapter->req_q_lock);
	return retval;
L
Linus Torvalds 已提交
1417 1418
}

S
Swen Schillig 已提交
1419
static void zfcp_fsf_open_port_handler(struct zfcp_fsf_req *req)
L
Linus Torvalds 已提交
1420
{
S
Swen Schillig 已提交
1421 1422
	struct zfcp_port *port = req->data;
	struct fsf_qtcb_header *header = &req->qtcb->header;
L
Linus Torvalds 已提交
1423 1424
	struct fsf_plogi *plogi;

S
Swen Schillig 已提交
1425
	if (req->status & ZFCP_STATUS_FSFREQ_ERROR)
1426
		goto out;
L
Linus Torvalds 已提交
1427 1428 1429 1430 1431

	switch (header->fsf_status) {
	case FSF_PORT_ALREADY_OPEN:
		break;
	case FSF_ACCESS_DENIED:
S
Swen Schillig 已提交
1432
		zfcp_fsf_access_denied_port(req, port);
L
Linus Torvalds 已提交
1433 1434
		break;
	case FSF_MAXIMUM_NUMBER_OF_PORTS_EXCEEDED:
S
Swen Schillig 已提交
1435
		dev_warn(&req->adapter->ccw_device->dev,
1436
			 "Not enough FCP adapter resources to open "
1437 1438
			 "remote port 0x%016Lx\n",
			 (unsigned long long)port->wwpn);
1439
		zfcp_erp_port_failed(port, "fsoph_1", req);
S
Swen Schillig 已提交
1440
		req->status |= ZFCP_STATUS_FSFREQ_ERROR;
L
Linus Torvalds 已提交
1441 1442 1443 1444 1445 1446
		break;
	case FSF_ADAPTER_STATUS_AVAILABLE:
		switch (header->fsf_status_qual.word[0]) {
		case FSF_SQ_INVOKE_LINK_TEST_PROCEDURE:
		case FSF_SQ_ULP_DEPENDENT_ERP_REQUIRED:
		case FSF_SQ_NO_RETRY_POSSIBLE:
S
Swen Schillig 已提交
1447
			req->status |= ZFCP_STATUS_FSFREQ_ERROR;
L
Linus Torvalds 已提交
1448 1449 1450 1451 1452 1453 1454
			break;
		}
		break;
	case FSF_GOOD:
		port->handle = header->port_handle;
		atomic_set_mask(ZFCP_STATUS_COMMON_OPEN |
				ZFCP_STATUS_PORT_PHYS_OPEN, &port->status);
1455 1456 1457
		atomic_clear_mask(ZFCP_STATUS_COMMON_ACCESS_DENIED |
		                  ZFCP_STATUS_COMMON_ACCESS_BOXED,
		                  &port->status);
L
Linus Torvalds 已提交
1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472
		/* check whether D_ID has changed during open */
		/*
		 * FIXME: This check is not airtight, as the FCP channel does
		 * not monitor closures of target port connections caused on
		 * the remote side. Thus, they might miss out on invalidating
		 * locally cached WWPNs (and other N_Port parameters) of gone
		 * target ports. So, our heroic attempt to make things safe
		 * could be undermined by 'open port' response data tagged with
		 * obsolete WWPNs. Another reason to monitor potential
		 * connection closures ourself at least (by interpreting
		 * incoming ELS' and unsolicited status). It just crosses my
		 * mind that one should be able to cross-check by means of
		 * another GID_PN straight after a port has been opened.
		 * Alternately, an ADISC/PDISC ELS should suffice, as well.
		 */
S
Swen Schillig 已提交
1473
		plogi = (struct fsf_plogi *) req->qtcb->bottom.support.els;
1474 1475
		if (req->qtcb->bottom.support.els1_length >=
		    FSF_PLOGI_MIN_LEN) {
S
Swen Schillig 已提交
1476
			if (plogi->serv_param.wwpn != port->wwpn)
1477
				port->d_id = 0;
S
Swen Schillig 已提交
1478 1479 1480
			else {
				port->wwnn = plogi->serv_param.wwnn;
				zfcp_fc_plogi_evaluate(port, plogi);
L
Linus Torvalds 已提交
1481 1482 1483 1484
			}
		}
		break;
	case FSF_UNKNOWN_OP_SUBTYPE:
S
Swen Schillig 已提交
1485
		req->status |= ZFCP_STATUS_FSFREQ_ERROR;
L
Linus Torvalds 已提交
1486 1487
		break;
	}
1488 1489 1490

out:
	zfcp_port_put(port);
L
Linus Torvalds 已提交
1491 1492
}

S
Swen Schillig 已提交
1493 1494 1495 1496
/**
 * zfcp_fsf_open_port - create and send open port request
 * @erp_action: pointer to struct zfcp_erp_action
 * Returns: 0 on success, error otherwise
L
Linus Torvalds 已提交
1497
 */
S
Swen Schillig 已提交
1498
int zfcp_fsf_open_port(struct zfcp_erp_action *erp_action)
L
Linus Torvalds 已提交
1499
{
1500
	struct qdio_buffer_element *sbale;
S
Swen Schillig 已提交
1501 1502
	struct zfcp_adapter *adapter = erp_action->adapter;
	struct zfcp_fsf_req *req;
1503
	struct zfcp_port *port = erp_action->port;
S
Swen Schillig 已提交
1504 1505
	int retval = -EIO;

1506
	spin_lock_bh(&adapter->req_q_lock);
S
Swen Schillig 已提交
1507 1508 1509 1510 1511 1512 1513
	if (zfcp_fsf_req_sbal_get(adapter))
		goto out;

	req = zfcp_fsf_req_create(adapter,
				  FSF_QTCB_OPEN_PORT_WITH_DID,
				  ZFCP_REQ_AUTO_CLEANUP,
				  adapter->pool.fsf_req_erp);
1514
	if (IS_ERR(req)) {
S
Swen Schillig 已提交
1515
		retval = PTR_ERR(req);
L
Linus Torvalds 已提交
1516
		goto out;
S
Swen Schillig 已提交
1517
	}
L
Linus Torvalds 已提交
1518

S
Swen Schillig 已提交
1519
	sbale = zfcp_qdio_sbale_req(req);
L
Linus Torvalds 已提交
1520 1521 1522
        sbale[0].flags |= SBAL_FLAGS0_TYPE_READ;
        sbale[1].flags |= SBAL_FLAGS_LAST_ENTRY;

S
Swen Schillig 已提交
1523
	req->handler = zfcp_fsf_open_port_handler;
1524 1525
	req->qtcb->bottom.support.d_id = port->d_id;
	req->data = port;
S
Swen Schillig 已提交
1526 1527
	req->erp_action = erp_action;
	erp_action->fsf_req = req;
1528
	zfcp_port_get(port);
S
Swen Schillig 已提交
1529

1530
	zfcp_fsf_start_erp_timer(req);
S
Swen Schillig 已提交
1531
	retval = zfcp_fsf_req_send(req);
L
Linus Torvalds 已提交
1532
	if (retval) {
S
Swen Schillig 已提交
1533
		zfcp_fsf_req_free(req);
L
Linus Torvalds 已提交
1534
		erp_action->fsf_req = NULL;
1535
		zfcp_port_put(port);
L
Linus Torvalds 已提交
1536
	}
S
Swen Schillig 已提交
1537
out:
1538
	spin_unlock_bh(&adapter->req_q_lock);
L
Linus Torvalds 已提交
1539 1540 1541
	return retval;
}

S
Swen Schillig 已提交
1542
static void zfcp_fsf_close_port_handler(struct zfcp_fsf_req *req)
L
Linus Torvalds 已提交
1543
{
S
Swen Schillig 已提交
1544
	struct zfcp_port *port = req->data;
L
Linus Torvalds 已提交
1545

S
Swen Schillig 已提交
1546
	if (req->status & ZFCP_STATUS_FSFREQ_ERROR)
1547
		return;
L
Linus Torvalds 已提交
1548

S
Swen Schillig 已提交
1549
	switch (req->qtcb->header.fsf_status) {
L
Linus Torvalds 已提交
1550
	case FSF_PORT_HANDLE_NOT_VALID:
1551
		zfcp_erp_adapter_reopen(port->adapter, 0, "fscph_1", req);
S
Swen Schillig 已提交
1552
		req->status |= ZFCP_STATUS_FSFREQ_ERROR;
L
Linus Torvalds 已提交
1553 1554 1555 1556
		break;
	case FSF_ADAPTER_STATUS_AVAILABLE:
		break;
	case FSF_GOOD:
1557
		zfcp_erp_modify_port_status(port, "fscph_2", req,
L
Linus Torvalds 已提交
1558 1559 1560 1561 1562 1563
					    ZFCP_STATUS_COMMON_OPEN,
					    ZFCP_CLEAR);
		break;
	}
}

S
Swen Schillig 已提交
1564 1565 1566 1567
/**
 * zfcp_fsf_close_port - create and send close port request
 * @erp_action: pointer to struct zfcp_erp_action
 * Returns: 0 on success, error otherwise
L
Linus Torvalds 已提交
1568
 */
S
Swen Schillig 已提交
1569
int zfcp_fsf_close_port(struct zfcp_erp_action *erp_action)
L
Linus Torvalds 已提交
1570
{
1571
	struct qdio_buffer_element *sbale;
S
Swen Schillig 已提交
1572 1573 1574 1575
	struct zfcp_adapter *adapter = erp_action->adapter;
	struct zfcp_fsf_req *req;
	int retval = -EIO;

1576
	spin_lock_bh(&adapter->req_q_lock);
S
Swen Schillig 已提交
1577 1578 1579 1580 1581 1582
	if (zfcp_fsf_req_sbal_get(adapter))
		goto out;

	req = zfcp_fsf_req_create(adapter, FSF_QTCB_CLOSE_PORT,
				  ZFCP_REQ_AUTO_CLEANUP,
				  adapter->pool.fsf_req_erp);
1583
	if (IS_ERR(req)) {
S
Swen Schillig 已提交
1584
		retval = PTR_ERR(req);
L
Linus Torvalds 已提交
1585
		goto out;
S
Swen Schillig 已提交
1586
	}
L
Linus Torvalds 已提交
1587

S
Swen Schillig 已提交
1588
	sbale = zfcp_qdio_sbale_req(req);
L
Linus Torvalds 已提交
1589 1590 1591
	sbale[0].flags |= SBAL_FLAGS0_TYPE_READ;
	sbale[1].flags |= SBAL_FLAGS_LAST_ENTRY;

S
Swen Schillig 已提交
1592 1593 1594 1595 1596 1597
	req->handler = zfcp_fsf_close_port_handler;
	req->data = erp_action->port;
	req->erp_action = erp_action;
	req->qtcb->header.port_handle = erp_action->port->handle;
	erp_action->fsf_req = req;

1598
	zfcp_fsf_start_erp_timer(req);
S
Swen Schillig 已提交
1599
	retval = zfcp_fsf_req_send(req);
L
Linus Torvalds 已提交
1600
	if (retval) {
S
Swen Schillig 已提交
1601
		zfcp_fsf_req_free(req);
L
Linus Torvalds 已提交
1602 1603
		erp_action->fsf_req = NULL;
	}
S
Swen Schillig 已提交
1604
out:
1605
	spin_unlock_bh(&adapter->req_q_lock);
L
Linus Torvalds 已提交
1606 1607 1608
	return retval;
}

1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622
static void zfcp_fsf_open_wka_port_handler(struct zfcp_fsf_req *req)
{
	struct zfcp_wka_port *wka_port = req->data;
	struct fsf_qtcb_header *header = &req->qtcb->header;

	if (req->status & ZFCP_STATUS_FSFREQ_ERROR) {
		wka_port->status = ZFCP_WKA_PORT_OFFLINE;
		goto out;
	}

	switch (header->fsf_status) {
	case FSF_MAXIMUM_NUMBER_OF_PORTS_EXCEEDED:
		dev_warn(&req->adapter->ccw_device->dev,
			 "Opening WKA port 0x%x failed\n", wka_port->d_id);
1623
		/* fall through */
1624 1625
	case FSF_ADAPTER_STATUS_AVAILABLE:
		req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1626
		/* fall through */
1627 1628 1629 1630 1631
	case FSF_ACCESS_DENIED:
		wka_port->status = ZFCP_WKA_PORT_OFFLINE;
		break;
	case FSF_GOOD:
		wka_port->handle = header->port_handle;
1632 1633
		/* fall through */
	case FSF_PORT_ALREADY_OPEN:
1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651
		wka_port->status = ZFCP_WKA_PORT_ONLINE;
	}
out:
	wake_up(&wka_port->completion_wq);
}

/**
 * zfcp_fsf_open_wka_port - create and send open wka-port request
 * @wka_port: pointer to struct zfcp_wka_port
 * Returns: 0 on success, error otherwise
 */
int zfcp_fsf_open_wka_port(struct zfcp_wka_port *wka_port)
{
	struct qdio_buffer_element *sbale;
	struct zfcp_adapter *adapter = wka_port->adapter;
	struct zfcp_fsf_req *req;
	int retval = -EIO;

1652
	spin_lock_bh(&adapter->req_q_lock);
1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677
	if (zfcp_fsf_req_sbal_get(adapter))
		goto out;

	req = zfcp_fsf_req_create(adapter,
				  FSF_QTCB_OPEN_PORT_WITH_DID,
				  ZFCP_REQ_AUTO_CLEANUP,
				  adapter->pool.fsf_req_erp);
	if (unlikely(IS_ERR(req))) {
		retval = PTR_ERR(req);
		goto out;
	}

	sbale = zfcp_qdio_sbale_req(req);
	sbale[0].flags |= SBAL_FLAGS0_TYPE_READ;
	sbale[1].flags |= SBAL_FLAGS_LAST_ENTRY;

	req->handler = zfcp_fsf_open_wka_port_handler;
	req->qtcb->bottom.support.d_id = wka_port->d_id;
	req->data = wka_port;

	zfcp_fsf_start_timer(req, ZFCP_FSF_REQUEST_TIMEOUT);
	retval = zfcp_fsf_req_send(req);
	if (retval)
		zfcp_fsf_req_free(req);
out:
1678
	spin_unlock_bh(&adapter->req_q_lock);
1679 1680 1681 1682 1683 1684 1685 1686 1687
	return retval;
}

static void zfcp_fsf_close_wka_port_handler(struct zfcp_fsf_req *req)
{
	struct zfcp_wka_port *wka_port = req->data;

	if (req->qtcb->header.fsf_status == FSF_PORT_HANDLE_NOT_VALID) {
		req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1688
		zfcp_erp_adapter_reopen(wka_port->adapter, 0, "fscwph1", req);
1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706
	}

	wka_port->status = ZFCP_WKA_PORT_OFFLINE;
	wake_up(&wka_port->completion_wq);
}

/**
 * zfcp_fsf_close_wka_port - create and send close wka port request
 * @erp_action: pointer to struct zfcp_erp_action
 * Returns: 0 on success, error otherwise
 */
int zfcp_fsf_close_wka_port(struct zfcp_wka_port *wka_port)
{
	struct qdio_buffer_element *sbale;
	struct zfcp_adapter *adapter = wka_port->adapter;
	struct zfcp_fsf_req *req;
	int retval = -EIO;

1707
	spin_lock_bh(&adapter->req_q_lock);
1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731
	if (zfcp_fsf_req_sbal_get(adapter))
		goto out;

	req = zfcp_fsf_req_create(adapter, FSF_QTCB_CLOSE_PORT,
				  ZFCP_REQ_AUTO_CLEANUP,
				  adapter->pool.fsf_req_erp);
	if (unlikely(IS_ERR(req))) {
		retval = PTR_ERR(req);
		goto out;
	}

	sbale = zfcp_qdio_sbale_req(req);
	sbale[0].flags |= SBAL_FLAGS0_TYPE_READ;
	sbale[1].flags |= SBAL_FLAGS_LAST_ENTRY;

	req->handler = zfcp_fsf_close_wka_port_handler;
	req->data = wka_port;
	req->qtcb->header.port_handle = wka_port->handle;

	zfcp_fsf_start_timer(req, ZFCP_FSF_REQUEST_TIMEOUT);
	retval = zfcp_fsf_req_send(req);
	if (retval)
		zfcp_fsf_req_free(req);
out:
1732
	spin_unlock_bh(&adapter->req_q_lock);
1733 1734 1735
	return retval;
}

S
Swen Schillig 已提交
1736
static void zfcp_fsf_close_physical_port_handler(struct zfcp_fsf_req *req)
L
Linus Torvalds 已提交
1737
{
S
Swen Schillig 已提交
1738 1739
	struct zfcp_port *port = req->data;
	struct fsf_qtcb_header *header = &req->qtcb->header;
L
Linus Torvalds 已提交
1740 1741
	struct zfcp_unit *unit;

S
Swen Schillig 已提交
1742
	if (req->status & ZFCP_STATUS_FSFREQ_ERROR)
1743
		return;
L
Linus Torvalds 已提交
1744 1745 1746

	switch (header->fsf_status) {
	case FSF_PORT_HANDLE_NOT_VALID:
1747
		zfcp_erp_adapter_reopen(port->adapter, 0, "fscpph1", req);
S
Swen Schillig 已提交
1748
		req->status |= ZFCP_STATUS_FSFREQ_ERROR;
L
Linus Torvalds 已提交
1749 1750
		break;
	case FSF_ACCESS_DENIED:
S
Swen Schillig 已提交
1751
		zfcp_fsf_access_denied_port(req, port);
L
Linus Torvalds 已提交
1752 1753
		break;
	case FSF_PORT_BOXED:
1754 1755 1756 1757 1758 1759
		/* can't use generic zfcp_erp_modify_port_status because
		 * ZFCP_STATUS_COMMON_OPEN must not be reset for the port */
		atomic_clear_mask(ZFCP_STATUS_PORT_PHYS_OPEN, &port->status);
		list_for_each_entry(unit, &port->unit_list_head, list)
			atomic_clear_mask(ZFCP_STATUS_COMMON_OPEN,
					  &unit->status);
1760 1761 1762 1763
		zfcp_erp_port_boxed(port, "fscpph2", req);
		req->status |= ZFCP_STATUS_FSFREQ_ERROR |
			       ZFCP_STATUS_FSFREQ_RETRY;

L
Linus Torvalds 已提交
1764 1765 1766 1767
		break;
	case FSF_ADAPTER_STATUS_AVAILABLE:
		switch (header->fsf_status_qual.word[0]) {
		case FSF_SQ_INVOKE_LINK_TEST_PROCEDURE:
S
Swen Schillig 已提交
1768
			/* fall through */
L
Linus Torvalds 已提交
1769
		case FSF_SQ_ULP_DEPENDENT_ERP_REQUIRED:
S
Swen Schillig 已提交
1770
			req->status |= ZFCP_STATUS_FSFREQ_ERROR;
L
Linus Torvalds 已提交
1771 1772 1773 1774 1775 1776 1777 1778 1779
			break;
		}
		break;
	case FSF_GOOD:
		/* can't use generic zfcp_erp_modify_port_status because
		 * ZFCP_STATUS_COMMON_OPEN must not be reset for the port
		 */
		atomic_clear_mask(ZFCP_STATUS_PORT_PHYS_OPEN, &port->status);
		list_for_each_entry(unit, &port->unit_list_head, list)
S
Swen Schillig 已提交
1780 1781
			atomic_clear_mask(ZFCP_STATUS_COMMON_OPEN,
					  &unit->status);
L
Linus Torvalds 已提交
1782 1783 1784 1785
		break;
	}
}

S
Swen Schillig 已提交
1786 1787 1788 1789
/**
 * zfcp_fsf_close_physical_port - close physical port
 * @erp_action: pointer to struct zfcp_erp_action
 * Returns: 0 on success
L
Linus Torvalds 已提交
1790
 */
S
Swen Schillig 已提交
1791
int zfcp_fsf_close_physical_port(struct zfcp_erp_action *erp_action)
L
Linus Torvalds 已提交
1792
{
1793
	struct qdio_buffer_element *sbale;
S
Swen Schillig 已提交
1794 1795 1796 1797
	struct zfcp_adapter *adapter = erp_action->adapter;
	struct zfcp_fsf_req *req;
	int retval = -EIO;

1798
	spin_lock_bh(&adapter->req_q_lock);
S
Swen Schillig 已提交
1799
	if (zfcp_fsf_req_sbal_get(adapter))
L
Linus Torvalds 已提交
1800 1801
		goto out;

S
Swen Schillig 已提交
1802 1803 1804
	req = zfcp_fsf_req_create(adapter, FSF_QTCB_CLOSE_PHYSICAL_PORT,
				  ZFCP_REQ_AUTO_CLEANUP,
				  adapter->pool.fsf_req_erp);
1805
	if (IS_ERR(req)) {
S
Swen Schillig 已提交
1806 1807 1808
		retval = PTR_ERR(req);
		goto out;
	}
L
Linus Torvalds 已提交
1809

S
Swen Schillig 已提交
1810 1811 1812
	sbale = zfcp_qdio_sbale_req(req);
	sbale[0].flags |= SBAL_FLAGS0_TYPE_READ;
	sbale[1].flags |= SBAL_FLAGS_LAST_ENTRY;
L
Linus Torvalds 已提交
1813

S
Swen Schillig 已提交
1814 1815 1816 1817 1818 1819
	req->data = erp_action->port;
	req->qtcb->header.port_handle = erp_action->port->handle;
	req->erp_action = erp_action;
	req->handler = zfcp_fsf_close_physical_port_handler;
	erp_action->fsf_req = req;

1820
	zfcp_fsf_start_erp_timer(req);
S
Swen Schillig 已提交
1821
	retval = zfcp_fsf_req_send(req);
L
Linus Torvalds 已提交
1822
	if (retval) {
S
Swen Schillig 已提交
1823
		zfcp_fsf_req_free(req);
L
Linus Torvalds 已提交
1824 1825
		erp_action->fsf_req = NULL;
	}
S
Swen Schillig 已提交
1826
out:
1827
	spin_unlock_bh(&adapter->req_q_lock);
L
Linus Torvalds 已提交
1828 1829 1830
	return retval;
}

S
Swen Schillig 已提交
1831
static void zfcp_fsf_open_unit_handler(struct zfcp_fsf_req *req)
L
Linus Torvalds 已提交
1832
{
S
Swen Schillig 已提交
1833 1834 1835 1836 1837 1838
	struct zfcp_adapter *adapter = req->adapter;
	struct zfcp_unit *unit = req->data;
	struct fsf_qtcb_header *header = &req->qtcb->header;
	struct fsf_qtcb_bottom_support *bottom = &req->qtcb->bottom.support;
	struct fsf_queue_designator *queue_designator =
				&header->fsf_status_qual.fsf_queue_designator;
1839
	int exclusive, readwrite;
L
Linus Torvalds 已提交
1840

S
Swen Schillig 已提交
1841
	if (req->status & ZFCP_STATUS_FSFREQ_ERROR)
1842
		return;
L
Linus Torvalds 已提交
1843 1844

	atomic_clear_mask(ZFCP_STATUS_COMMON_ACCESS_DENIED |
1845
			  ZFCP_STATUS_COMMON_ACCESS_BOXED |
L
Linus Torvalds 已提交
1846 1847 1848 1849 1850 1851 1852
			  ZFCP_STATUS_UNIT_SHARED |
			  ZFCP_STATUS_UNIT_READONLY,
			  &unit->status);

	switch (header->fsf_status) {

	case FSF_PORT_HANDLE_NOT_VALID:
1853
		zfcp_erp_adapter_reopen(unit->port->adapter, 0, "fsouh_1", req);
S
Swen Schillig 已提交
1854
		/* fall through */
L
Linus Torvalds 已提交
1855 1856 1857
	case FSF_LUN_ALREADY_OPEN:
		break;
	case FSF_ACCESS_DENIED:
S
Swen Schillig 已提交
1858
		zfcp_fsf_access_denied_unit(req, unit);
L
Linus Torvalds 已提交
1859
		atomic_clear_mask(ZFCP_STATUS_UNIT_SHARED, &unit->status);
C
Christof Schmitt 已提交
1860
		atomic_clear_mask(ZFCP_STATUS_UNIT_READONLY, &unit->status);
L
Linus Torvalds 已提交
1861 1862
		break;
	case FSF_PORT_BOXED:
1863
		zfcp_erp_port_boxed(unit->port, "fsouh_2", req);
S
Swen Schillig 已提交
1864 1865
		req->status |= ZFCP_STATUS_FSFREQ_ERROR |
			       ZFCP_STATUS_FSFREQ_RETRY;
L
Linus Torvalds 已提交
1866 1867
		break;
	case FSF_LUN_SHARING_VIOLATION:
S
Swen Schillig 已提交
1868
		if (header->fsf_status_qual.word[0])
C
Christof Schmitt 已提交
1869
			dev_warn(&adapter->ccw_device->dev,
1870 1871
				 "LUN 0x%Lx on port 0x%Lx is already in "
				 "use by CSS%d, MIF Image ID %x\n",
1872 1873
				 (unsigned long long)unit->fcp_lun,
				 (unsigned long long)unit->port->wwpn,
1874 1875
				 queue_designator->cssid,
				 queue_designator->hla);
S
Swen Schillig 已提交
1876
		else
C
Christof Schmitt 已提交
1877 1878
			zfcp_act_eval_err(adapter,
					  header->fsf_status_qual.word[2]);
1879
		zfcp_erp_unit_access_denied(unit, "fsouh_3", req);
L
Linus Torvalds 已提交
1880 1881
		atomic_clear_mask(ZFCP_STATUS_UNIT_SHARED, &unit->status);
		atomic_clear_mask(ZFCP_STATUS_UNIT_READONLY, &unit->status);
S
Swen Schillig 已提交
1882
		req->status |= ZFCP_STATUS_FSFREQ_ERROR;
L
Linus Torvalds 已提交
1883 1884
		break;
	case FSF_MAXIMUM_NUMBER_OF_LUNS_EXCEEDED:
S
Swen Schillig 已提交
1885
		dev_warn(&adapter->ccw_device->dev,
1886 1887
			 "No handle is available for LUN "
			 "0x%016Lx on port 0x%016Lx\n",
1888 1889
			 (unsigned long long)unit->fcp_lun,
			 (unsigned long long)unit->port->wwpn);
1890
		zfcp_erp_unit_failed(unit, "fsouh_4", req);
S
Swen Schillig 已提交
1891 1892 1893
		/* fall through */
	case FSF_INVALID_COMMAND_OPTION:
		req->status |= ZFCP_STATUS_FSFREQ_ERROR;
L
Linus Torvalds 已提交
1894 1895 1896 1897
		break;
	case FSF_ADAPTER_STATUS_AVAILABLE:
		switch (header->fsf_status_qual.word[0]) {
		case FSF_SQ_INVOKE_LINK_TEST_PROCEDURE:
1898
			zfcp_test_link(unit->port);
S
Swen Schillig 已提交
1899
			/* fall through */
L
Linus Torvalds 已提交
1900
		case FSF_SQ_ULP_DEPENDENT_ERP_REQUIRED:
S
Swen Schillig 已提交
1901
			req->status |= ZFCP_STATUS_FSFREQ_ERROR;
L
Linus Torvalds 已提交
1902 1903 1904 1905 1906 1907 1908
			break;
		}
		break;

	case FSF_GOOD:
		unit->handle = header->lun_handle;
		atomic_set_mask(ZFCP_STATUS_COMMON_OPEN, &unit->status);
1909 1910 1911

		if (!(adapter->connection_features & FSF_FEATURE_NPIV_MODE) &&
		    (adapter->adapter_features & FSF_FEATURE_LUN_SHARING) &&
1912
		    !zfcp_ccw_priv_sch(adapter)) {
1913 1914 1915 1916 1917
			exclusive = (bottom->lun_access_info &
					FSF_UNIT_ACCESS_EXCLUSIVE);
			readwrite = (bottom->lun_access_info &
					FSF_UNIT_ACCESS_OUTBOUND_TRANSFER);

L
Linus Torvalds 已提交
1918 1919 1920 1921 1922 1923 1924
			if (!exclusive)
		                atomic_set_mask(ZFCP_STATUS_UNIT_SHARED,
						&unit->status);

			if (!readwrite) {
                		atomic_set_mask(ZFCP_STATUS_UNIT_READONLY,
						&unit->status);
S
Swen Schillig 已提交
1925
				dev_info(&adapter->ccw_device->dev,
1926 1927
					 "SCSI device at LUN 0x%016Lx on port "
					 "0x%016Lx opened read-only\n",
1928 1929
					 (unsigned long long)unit->fcp_lun,
					 (unsigned long long)unit->port->wwpn);
L
Linus Torvalds 已提交
1930 1931 1932
        		}

        		if (exclusive && !readwrite) {
S
Swen Schillig 已提交
1933
				dev_err(&adapter->ccw_device->dev,
1934 1935 1936
					"Exclusive read-only access not "
					"supported (unit 0x%016Lx, "
					"port 0x%016Lx)\n",
1937 1938
					(unsigned long long)unit->fcp_lun,
					(unsigned long long)unit->port->wwpn);
1939
				zfcp_erp_unit_failed(unit, "fsouh_5", req);
S
Swen Schillig 已提交
1940
				req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1941
				zfcp_erp_unit_shutdown(unit, 0, "fsouh_6", req);
L
Linus Torvalds 已提交
1942
        		} else if (!exclusive && readwrite) {
S
Swen Schillig 已提交
1943
				dev_err(&adapter->ccw_device->dev,
1944 1945
					"Shared read-write access not "
					"supported (unit 0x%016Lx, port "
1946
					"0x%016Lx)\n",
1947 1948
					(unsigned long long)unit->fcp_lun,
					(unsigned long long)unit->port->wwpn);
1949
				zfcp_erp_unit_failed(unit, "fsouh_7", req);
S
Swen Schillig 已提交
1950
				req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1951
				zfcp_erp_unit_shutdown(unit, 0, "fsouh_8", req);
L
Linus Torvalds 已提交
1952 1953 1954 1955 1956 1957
        		}
		}
		break;
	}
}

S
Swen Schillig 已提交
1958 1959 1960 1961
/**
 * zfcp_fsf_open_unit - open unit
 * @erp_action: pointer to struct zfcp_erp_action
 * Returns: 0 on success, error otherwise
L
Linus Torvalds 已提交
1962
 */
S
Swen Schillig 已提交
1963
int zfcp_fsf_open_unit(struct zfcp_erp_action *erp_action)
L
Linus Torvalds 已提交
1964
{
1965
	struct qdio_buffer_element *sbale;
S
Swen Schillig 已提交
1966 1967 1968 1969
	struct zfcp_adapter *adapter = erp_action->adapter;
	struct zfcp_fsf_req *req;
	int retval = -EIO;

1970
	spin_lock_bh(&adapter->req_q_lock);
S
Swen Schillig 已提交
1971
	if (zfcp_fsf_req_sbal_get(adapter))
L
Linus Torvalds 已提交
1972 1973
		goto out;

S
Swen Schillig 已提交
1974 1975 1976
	req = zfcp_fsf_req_create(adapter, FSF_QTCB_OPEN_LUN,
				  ZFCP_REQ_AUTO_CLEANUP,
				  adapter->pool.fsf_req_erp);
1977
	if (IS_ERR(req)) {
S
Swen Schillig 已提交
1978 1979 1980 1981 1982
		retval = PTR_ERR(req);
		goto out;
	}

	sbale = zfcp_qdio_sbale_req(req);
L
Linus Torvalds 已提交
1983 1984 1985
        sbale[0].flags |= SBAL_FLAGS0_TYPE_READ;
        sbale[1].flags |= SBAL_FLAGS_LAST_ENTRY;

S
Swen Schillig 已提交
1986 1987 1988 1989 1990 1991 1992 1993 1994 1995
	req->qtcb->header.port_handle = erp_action->port->handle;
	req->qtcb->bottom.support.fcp_lun = erp_action->unit->fcp_lun;
	req->handler = zfcp_fsf_open_unit_handler;
	req->data = erp_action->unit;
	req->erp_action = erp_action;
	erp_action->fsf_req = req;

	if (!(adapter->connection_features & FSF_FEATURE_NPIV_MODE))
		req->qtcb->bottom.support.option = FSF_OPEN_LUN_SUPPRESS_BOXING;

1996
	zfcp_fsf_start_erp_timer(req);
S
Swen Schillig 已提交
1997
	retval = zfcp_fsf_req_send(req);
L
Linus Torvalds 已提交
1998
	if (retval) {
S
Swen Schillig 已提交
1999
		zfcp_fsf_req_free(req);
L
Linus Torvalds 已提交
2000 2001
		erp_action->fsf_req = NULL;
	}
S
Swen Schillig 已提交
2002
out:
2003
	spin_unlock_bh(&adapter->req_q_lock);
L
Linus Torvalds 已提交
2004 2005 2006
	return retval;
}

S
Swen Schillig 已提交
2007
static void zfcp_fsf_close_unit_handler(struct zfcp_fsf_req *req)
L
Linus Torvalds 已提交
2008
{
S
Swen Schillig 已提交
2009
	struct zfcp_unit *unit = req->data;
L
Linus Torvalds 已提交
2010

S
Swen Schillig 已提交
2011
	if (req->status & ZFCP_STATUS_FSFREQ_ERROR)
2012
		return;
L
Linus Torvalds 已提交
2013

S
Swen Schillig 已提交
2014
	switch (req->qtcb->header.fsf_status) {
L
Linus Torvalds 已提交
2015
	case FSF_PORT_HANDLE_NOT_VALID:
2016
		zfcp_erp_adapter_reopen(unit->port->adapter, 0, "fscuh_1", req);
S
Swen Schillig 已提交
2017
		req->status |= ZFCP_STATUS_FSFREQ_ERROR;
L
Linus Torvalds 已提交
2018 2019
		break;
	case FSF_LUN_HANDLE_NOT_VALID:
2020
		zfcp_erp_port_reopen(unit->port, 0, "fscuh_2", req);
S
Swen Schillig 已提交
2021
		req->status |= ZFCP_STATUS_FSFREQ_ERROR;
L
Linus Torvalds 已提交
2022 2023
		break;
	case FSF_PORT_BOXED:
2024
		zfcp_erp_port_boxed(unit->port, "fscuh_3", req);
S
Swen Schillig 已提交
2025 2026
		req->status |= ZFCP_STATUS_FSFREQ_ERROR |
			       ZFCP_STATUS_FSFREQ_RETRY;
L
Linus Torvalds 已提交
2027 2028
		break;
	case FSF_ADAPTER_STATUS_AVAILABLE:
S
Swen Schillig 已提交
2029
		switch (req->qtcb->header.fsf_status_qual.word[0]) {
L
Linus Torvalds 已提交
2030
		case FSF_SQ_INVOKE_LINK_TEST_PROCEDURE:
2031
			zfcp_test_link(unit->port);
S
Swen Schillig 已提交
2032
			/* fall through */
L
Linus Torvalds 已提交
2033
		case FSF_SQ_ULP_DEPENDENT_ERP_REQUIRED:
S
Swen Schillig 已提交
2034
			req->status |= ZFCP_STATUS_FSFREQ_ERROR;
L
Linus Torvalds 已提交
2035 2036 2037 2038 2039 2040 2041 2042 2043 2044
			break;
		}
		break;
	case FSF_GOOD:
		atomic_clear_mask(ZFCP_STATUS_COMMON_OPEN, &unit->status);
		break;
	}
}

/**
S
Swen Schillig 已提交
2045 2046 2047
 * zfcp_fsf_close_unit - close zfcp unit
 * @erp_action: pointer to struct zfcp_unit
 * Returns: 0 on success, error otherwise
L
Linus Torvalds 已提交
2048
 */
S
Swen Schillig 已提交
2049
int zfcp_fsf_close_unit(struct zfcp_erp_action *erp_action)
L
Linus Torvalds 已提交
2050
{
2051
	struct qdio_buffer_element *sbale;
S
Swen Schillig 已提交
2052 2053 2054
	struct zfcp_adapter *adapter = erp_action->adapter;
	struct zfcp_fsf_req *req;
	int retval = -EIO;
L
Linus Torvalds 已提交
2055

2056
	spin_lock_bh(&adapter->req_q_lock);
S
Swen Schillig 已提交
2057
	if (zfcp_fsf_req_sbal_get(adapter))
L
Linus Torvalds 已提交
2058
		goto out;
S
Swen Schillig 已提交
2059 2060 2061
	req = zfcp_fsf_req_create(adapter, FSF_QTCB_CLOSE_LUN,
				  ZFCP_REQ_AUTO_CLEANUP,
				  adapter->pool.fsf_req_erp);
2062
	if (IS_ERR(req)) {
S
Swen Schillig 已提交
2063 2064 2065
		retval = PTR_ERR(req);
		goto out;
	}
L
Linus Torvalds 已提交
2066

S
Swen Schillig 已提交
2067 2068
	sbale = zfcp_qdio_sbale_req(req);
	sbale[0].flags |= SBAL_FLAGS0_TYPE_READ;
L
Linus Torvalds 已提交
2069 2070
	sbale[1].flags |= SBAL_FLAGS_LAST_ENTRY;

S
Swen Schillig 已提交
2071 2072 2073 2074 2075 2076
	req->qtcb->header.port_handle = erp_action->port->handle;
	req->qtcb->header.lun_handle = erp_action->unit->handle;
	req->handler = zfcp_fsf_close_unit_handler;
	req->data = erp_action->unit;
	req->erp_action = erp_action;
	erp_action->fsf_req = req;
2077

2078
	zfcp_fsf_start_erp_timer(req);
S
Swen Schillig 已提交
2079 2080 2081 2082 2083 2084
	retval = zfcp_fsf_req_send(req);
	if (retval) {
		zfcp_fsf_req_free(req);
		erp_action->fsf_req = NULL;
	}
out:
2085
	spin_unlock_bh(&adapter->req_q_lock);
S
Swen Schillig 已提交
2086
	return retval;
L
Linus Torvalds 已提交
2087 2088
}

2089 2090 2091
static void zfcp_fsf_update_lat(struct fsf_latency_record *lat_rec, u32 lat)
{
	lat_rec->sum += lat;
S
Swen Schillig 已提交
2092 2093
	lat_rec->min = min(lat_rec->min, lat);
	lat_rec->max = max(lat_rec->max, lat);
2094 2095
}

S
Swen Schillig 已提交
2096
static void zfcp_fsf_req_latency(struct zfcp_fsf_req *req)
2097 2098 2099
{
	struct fsf_qual_latency_info *lat_inf;
	struct latency_cont *lat;
S
Swen Schillig 已提交
2100
	struct zfcp_unit *unit = req->unit;
2101

S
Swen Schillig 已提交
2102
	lat_inf = &req->qtcb->prefix.prot_status_qual.latency_info;
2103

S
Swen Schillig 已提交
2104
	switch (req->qtcb->bottom.io.data_direction) {
2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117
	case FSF_DATADIR_READ:
		lat = &unit->latencies.read;
		break;
	case FSF_DATADIR_WRITE:
		lat = &unit->latencies.write;
		break;
	case FSF_DATADIR_CMND:
		lat = &unit->latencies.cmd;
		break;
	default:
		return;
	}

2118
	spin_lock(&unit->latencies.lock);
2119 2120 2121
	zfcp_fsf_update_lat(&lat->channel, lat_inf->channel_lat);
	zfcp_fsf_update_lat(&lat->fabric, lat_inf->fabric_lat);
	lat->counter++;
2122
	spin_unlock(&unit->latencies.lock);
L
Linus Torvalds 已提交
2123 2124
}

S
Stefan Raspl 已提交
2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154
#ifdef CONFIG_BLK_DEV_IO_TRACE
static void zfcp_fsf_trace_latency(struct zfcp_fsf_req *fsf_req)
{
	struct fsf_qual_latency_info *lat_inf;
	struct scsi_cmnd *scsi_cmnd = (struct scsi_cmnd *)fsf_req->data;
	struct request *req = scsi_cmnd->request;
	struct zfcp_blk_drv_data trace;
	int ticks = fsf_req->adapter->timer_ticks;

	trace.flags = 0;
	trace.magic = ZFCP_BLK_DRV_DATA_MAGIC;
	if (fsf_req->adapter->adapter_features & FSF_FEATURE_MEASUREMENT_DATA) {
		trace.flags |= ZFCP_BLK_LAT_VALID;
		lat_inf = &fsf_req->qtcb->prefix.prot_status_qual.latency_info;
		trace.channel_lat = lat_inf->channel_lat * ticks;
		trace.fabric_lat = lat_inf->fabric_lat * ticks;
	}
	if (fsf_req->status & ZFCP_STATUS_FSFREQ_ERROR)
		trace.flags |= ZFCP_BLK_REQ_ERROR;
	trace.inb_usage = fsf_req->qdio_inb_usage;
	trace.outb_usage = fsf_req->qdio_outb_usage;

	blk_add_driver_data(req->q, req, &trace, sizeof(trace));
}
#else
static inline void zfcp_fsf_trace_latency(struct zfcp_fsf_req *fsf_req)
{
}
#endif

S
Swen Schillig 已提交
2155
static void zfcp_fsf_send_fcp_command_task_handler(struct zfcp_fsf_req *req)
L
Linus Torvalds 已提交
2156
{
2157
	struct scsi_cmnd *scpnt;
L
Linus Torvalds 已提交
2158
	struct fcp_rsp_iu *fcp_rsp_iu = (struct fcp_rsp_iu *)
S
Swen Schillig 已提交
2159
	    &(req->qtcb->bottom.io.fcp_rsp);
L
Linus Torvalds 已提交
2160
	u32 sns_len;
2161
	char *fcp_rsp_info = (unsigned char *) &fcp_rsp_iu[1];
L
Linus Torvalds 已提交
2162 2163
	unsigned long flags;

S
Swen Schillig 已提交
2164 2165
	read_lock_irqsave(&req->adapter->abort_lock, flags);

2166 2167 2168 2169 2170 2171
	scpnt = req->data;
	if (unlikely(!scpnt)) {
		read_unlock_irqrestore(&req->adapter->abort_lock, flags);
		return;
	}

S
Swen Schillig 已提交
2172
	if (unlikely(req->status & ZFCP_STATUS_FSFREQ_ABORTED)) {
2173
		set_host_byte(scpnt, DID_SOFT_ERROR);
L
Linus Torvalds 已提交
2174 2175 2176
		goto skip_fsfstatus;
	}

S
Swen Schillig 已提交
2177
	if (unlikely(req->status & ZFCP_STATUS_FSFREQ_ERROR)) {
2178
		set_host_byte(scpnt, DID_ERROR);
L
Linus Torvalds 已提交
2179 2180 2181
		goto skip_fsfstatus;
	}

2182
	set_msg_byte(scpnt, COMMAND_COMPLETE);
L
Linus Torvalds 已提交
2183 2184 2185

	scpnt->result |= fcp_rsp_iu->scsi_status;

S
Swen Schillig 已提交
2186 2187
	if (req->adapter->adapter_features & FSF_FEATURE_MEASUREMENT_DATA)
		zfcp_fsf_req_latency(req);
2188

S
Stefan Raspl 已提交
2189 2190
	zfcp_fsf_trace_latency(req);

L
Linus Torvalds 已提交
2191
	if (unlikely(fcp_rsp_iu->validity.bits.fcp_rsp_len_valid)) {
S
Swen Schillig 已提交
2192
		if (fcp_rsp_info[3] == RSP_CODE_GOOD)
2193
			set_host_byte(scpnt, DID_OK);
S
Swen Schillig 已提交
2194
		else {
2195
			set_host_byte(scpnt, DID_ERROR);
已提交
2196
			goto skip_fsfstatus;
L
Linus Torvalds 已提交
2197 2198 2199 2200
		}
	}

	if (unlikely(fcp_rsp_iu->validity.bits.fcp_sns_len_valid)) {
S
Swen Schillig 已提交
2201 2202
		sns_len = FSF_FCP_RSP_SIZE - sizeof(struct fcp_rsp_iu) +
			  fcp_rsp_iu->fcp_rsp_len;
L
Linus Torvalds 已提交
2203 2204 2205
		sns_len = min(sns_len, (u32) SCSI_SENSE_BUFFERSIZE);
		sns_len = min(sns_len, fcp_rsp_iu->fcp_sns_len);

2206
		memcpy(scpnt->sense_buffer,
L
Linus Torvalds 已提交
2207 2208 2209 2210
		       zfcp_get_fcp_sns_info_ptr(fcp_rsp_iu), sns_len);
	}

	if (unlikely(fcp_rsp_iu->validity.bits.fcp_resid_under)) {
2211 2212 2213
		scsi_set_resid(scpnt, fcp_rsp_iu->fcp_resid);
		if (scsi_bufflen(scpnt) - scsi_get_resid(scpnt) <
		    scpnt->underflow)
2214
			set_host_byte(scpnt, DID_ERROR);
L
Linus Torvalds 已提交
2215
	}
S
Swen Schillig 已提交
2216
skip_fsfstatus:
2217
	if (scpnt->result != 0)
S
Swen Schillig 已提交
2218
		zfcp_scsi_dbf_event_result("erro", 3, req->adapter, scpnt, req);
2219
	else if (scpnt->retries > 0)
S
Swen Schillig 已提交
2220
		zfcp_scsi_dbf_event_result("retr", 4, req->adapter, scpnt, req);
2221
	else
S
Swen Schillig 已提交
2222
		zfcp_scsi_dbf_event_result("norm", 6, req->adapter, scpnt, req);
L
Linus Torvalds 已提交
2223 2224 2225 2226 2227 2228 2229 2230 2231

	scpnt->host_scribble = NULL;
	(scpnt->scsi_done) (scpnt);
	/*
	 * We must hold this lock until scsi_done has been called.
	 * Otherwise we may call scsi_done after abort regarding this
	 * command has completed.
	 * Note: scsi_done must not block!
	 */
S
Swen Schillig 已提交
2232
	read_unlock_irqrestore(&req->adapter->abort_lock, flags);
L
Linus Torvalds 已提交
2233 2234
}

S
Swen Schillig 已提交
2235
static void zfcp_fsf_send_fcp_ctm_handler(struct zfcp_fsf_req *req)
L
Linus Torvalds 已提交
2236 2237
{
	struct fcp_rsp_iu *fcp_rsp_iu = (struct fcp_rsp_iu *)
S
Swen Schillig 已提交
2238
	    &(req->qtcb->bottom.io.fcp_rsp);
2239
	char *fcp_rsp_info = (unsigned char *) &fcp_rsp_iu[1];
L
Linus Torvalds 已提交
2240

S
Swen Schillig 已提交
2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257
	if ((fcp_rsp_info[3] != RSP_CODE_GOOD) ||
	     (req->status & ZFCP_STATUS_FSFREQ_ERROR))
		req->status |= ZFCP_STATUS_FSFREQ_TMFUNCFAILED;
}


static void zfcp_fsf_send_fcp_command_handler(struct zfcp_fsf_req *req)
{
	struct zfcp_unit *unit;
	struct fsf_qtcb_header *header = &req->qtcb->header;

	if (unlikely(req->status & ZFCP_STATUS_FSFREQ_TASK_MANAGEMENT))
		unit = req->data;
	else
		unit = req->unit;

	if (unlikely(req->status & ZFCP_STATUS_FSFREQ_ERROR))
L
Linus Torvalds 已提交
2258 2259
		goto skip_fsfstatus;

S
Swen Schillig 已提交
2260 2261 2262
	switch (header->fsf_status) {
	case FSF_HANDLE_MISMATCH:
	case FSF_PORT_HANDLE_NOT_VALID:
2263
		zfcp_erp_adapter_reopen(unit->port->adapter, 0, "fssfch1", req);
S
Swen Schillig 已提交
2264 2265 2266 2267
		req->status |= ZFCP_STATUS_FSFREQ_ERROR;
		break;
	case FSF_FCPLUN_NOT_VALID:
	case FSF_LUN_HANDLE_NOT_VALID:
2268
		zfcp_erp_port_reopen(unit->port, 0, "fssfch2", req);
S
Swen Schillig 已提交
2269
		req->status |= ZFCP_STATUS_FSFREQ_ERROR;
L
Linus Torvalds 已提交
2270
		break;
S
Swen Schillig 已提交
2271 2272
	case FSF_SERVICE_CLASS_NOT_SUPPORTED:
		zfcp_fsf_class_not_supp(req);
L
Linus Torvalds 已提交
2273
		break;
S
Swen Schillig 已提交
2274 2275 2276 2277 2278
	case FSF_ACCESS_DENIED:
		zfcp_fsf_access_denied_unit(req, unit);
		break;
	case FSF_DIRECTION_INDICATOR_NOT_VALID:
		dev_err(&req->adapter->ccw_device->dev,
2279 2280
			"Incorrect direction %d, unit 0x%016Lx on port "
			"0x%016Lx closed\n",
S
Swen Schillig 已提交
2281
			req->qtcb->bottom.io.data_direction,
2282 2283
			(unsigned long long)unit->fcp_lun,
			(unsigned long long)unit->port->wwpn);
2284 2285
		zfcp_erp_adapter_shutdown(unit->port->adapter, 0, "fssfch3",
					  req);
S
Swen Schillig 已提交
2286 2287 2288 2289
		req->status |= ZFCP_STATUS_FSFREQ_ERROR;
		break;
	case FSF_CMND_LENGTH_NOT_VALID:
		dev_err(&req->adapter->ccw_device->dev,
2290 2291
			"Incorrect CDB length %d, unit 0x%016Lx on "
			"port 0x%016Lx closed\n",
S
Swen Schillig 已提交
2292
			req->qtcb->bottom.io.fcp_cmnd_length,
2293 2294
			(unsigned long long)unit->fcp_lun,
			(unsigned long long)unit->port->wwpn);
2295 2296
		zfcp_erp_adapter_shutdown(unit->port->adapter, 0, "fssfch4",
					  req);
S
Swen Schillig 已提交
2297 2298 2299
		req->status |= ZFCP_STATUS_FSFREQ_ERROR;
		break;
	case FSF_PORT_BOXED:
2300
		zfcp_erp_port_boxed(unit->port, "fssfch5", req);
S
Swen Schillig 已提交
2301 2302 2303 2304
		req->status |= ZFCP_STATUS_FSFREQ_ERROR |
			       ZFCP_STATUS_FSFREQ_RETRY;
		break;
	case FSF_LUN_BOXED:
2305
		zfcp_erp_unit_boxed(unit, "fssfch6", req);
S
Swen Schillig 已提交
2306 2307 2308 2309 2310 2311 2312 2313
		req->status |= ZFCP_STATUS_FSFREQ_ERROR |
			       ZFCP_STATUS_FSFREQ_RETRY;
		break;
	case FSF_ADAPTER_STATUS_AVAILABLE:
		if (header->fsf_status_qual.word[0] ==
		    FSF_SQ_INVOKE_LINK_TEST_PROCEDURE)
			zfcp_test_link(unit->port);
		req->status |= ZFCP_STATUS_FSFREQ_ERROR;
L
Linus Torvalds 已提交
2314 2315
		break;
	}
S
Swen Schillig 已提交
2316 2317 2318 2319 2320 2321 2322 2323
skip_fsfstatus:
	if (req->status & ZFCP_STATUS_FSFREQ_TASK_MANAGEMENT)
		zfcp_fsf_send_fcp_ctm_handler(req);
	else {
		zfcp_fsf_send_fcp_command_task_handler(req);
		req->unit = NULL;
		zfcp_unit_put(unit);
	}
L
Linus Torvalds 已提交
2324 2325
}

2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339
static void zfcp_set_fcp_dl(struct fcp_cmnd_iu *fcp_cmd, u32 fcp_dl)
{
	u32 *fcp_dl_ptr;

	/*
	 * fcp_dl_addr = start address of fcp_cmnd structure +
	 * size of fixed part + size of dynamically sized add_dcp_cdb field
	 * SEE FCP-2 documentation
	 */
	fcp_dl_ptr = (u32 *) ((unsigned char *) &fcp_cmd[1] +
			(fcp_cmd->add_fcp_cdb_length << 2));
	*fcp_dl_ptr = fcp_dl;
}

S
Swen Schillig 已提交
2340 2341 2342 2343
/**
 * zfcp_fsf_send_fcp_command_task - initiate an FCP command (for a SCSI command)
 * @unit: unit where command is sent to
 * @scsi_cmnd: scsi command to be sent
L
Linus Torvalds 已提交
2344
 */
2345 2346
int zfcp_fsf_send_fcp_command_task(struct zfcp_unit *unit,
				   struct scsi_cmnd *scsi_cmnd)
L
Linus Torvalds 已提交
2347
{
S
Swen Schillig 已提交
2348 2349
	struct zfcp_fsf_req *req;
	struct fcp_cmnd_iu *fcp_cmnd_iu;
2350
	unsigned int sbtype = SBAL_FLAGS0_TYPE_READ;
S
Swen Schillig 已提交
2351
	int real_bytes, retval = -EIO;
2352
	struct zfcp_adapter *adapter = unit->port->adapter;
L
Linus Torvalds 已提交
2353

S
Swen Schillig 已提交
2354 2355 2356
	if (unlikely(!(atomic_read(&unit->status) &
		       ZFCP_STATUS_COMMON_UNBLOCKED)))
		return -EBUSY;
L
Linus Torvalds 已提交
2357

2358
	spin_lock(&adapter->req_q_lock);
2359 2360
	if (atomic_read(&adapter->req_q.count) <= 0) {
		atomic_inc(&adapter->qdio_outb_full);
S
Swen Schillig 已提交
2361
		goto out;
2362
	}
2363 2364
	req = zfcp_fsf_req_create(adapter, FSF_QTCB_FCP_CMND,
				  ZFCP_REQ_AUTO_CLEANUP,
S
Swen Schillig 已提交
2365
				  adapter->pool.fsf_req_scsi);
2366
	if (IS_ERR(req)) {
S
Swen Schillig 已提交
2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391
		retval = PTR_ERR(req);
		goto out;
	}

	zfcp_unit_get(unit);
	req->unit = unit;
	req->data = scsi_cmnd;
	req->handler = zfcp_fsf_send_fcp_command_handler;
	req->qtcb->header.lun_handle = unit->handle;
	req->qtcb->header.port_handle = unit->port->handle;
	req->qtcb->bottom.io.service_class = FSF_CLASS_3;

	scsi_cmnd->host_scribble = (unsigned char *) req->req_id;

	fcp_cmnd_iu = (struct fcp_cmnd_iu *) &(req->qtcb->bottom.io.fcp_cmnd);
	fcp_cmnd_iu->fcp_lun = unit->fcp_lun;
	/*
	 * set depending on data direction:
	 *      data direction bits in SBALE (SB Type)
	 *      data direction bits in QTCB
	 *      data direction bits in FCP_CMND IU
	 */
	switch (scsi_cmnd->sc_data_direction) {
	case DMA_NONE:
		req->qtcb->bottom.io.data_direction = FSF_DATADIR_CMND;
L
Linus Torvalds 已提交
2392
		break;
S
Swen Schillig 已提交
2393 2394 2395 2396 2397 2398 2399 2400
	case DMA_FROM_DEVICE:
		req->qtcb->bottom.io.data_direction = FSF_DATADIR_READ;
		fcp_cmnd_iu->rddata = 1;
		break;
	case DMA_TO_DEVICE:
		req->qtcb->bottom.io.data_direction = FSF_DATADIR_WRITE;
		sbtype = SBAL_FLAGS0_TYPE_WRITE;
		fcp_cmnd_iu->wddata = 1;
L
Linus Torvalds 已提交
2401
		break;
S
Swen Schillig 已提交
2402 2403
	case DMA_BIDIRECTIONAL:
		goto failed_scsi_cmnd;
L
Linus Torvalds 已提交
2404 2405
	}

S
Swen Schillig 已提交
2406 2407 2408 2409 2410 2411
	if (likely((scsi_cmnd->device->simple_tags) ||
		   ((atomic_read(&unit->status) & ZFCP_STATUS_UNIT_READONLY) &&
		    (atomic_read(&unit->status) & ZFCP_STATUS_UNIT_SHARED))))
		fcp_cmnd_iu->task_attribute = SIMPLE_Q;
	else
		fcp_cmnd_iu->task_attribute = UNTAGGED;
L
Linus Torvalds 已提交
2412

S
Swen Schillig 已提交
2413 2414 2415
	if (unlikely(scsi_cmnd->cmd_len > FCP_CDB_LENGTH))
		fcp_cmnd_iu->add_fcp_cdb_length =
			(scsi_cmnd->cmd_len - FCP_CDB_LENGTH) >> 2;
L
Linus Torvalds 已提交
2416

S
Swen Schillig 已提交
2417
	memcpy(fcp_cmnd_iu->fcp_cdb, scsi_cmnd->cmnd, scsi_cmnd->cmd_len);
2418

S
Swen Schillig 已提交
2419
	req->qtcb->bottom.io.fcp_cmnd_length = sizeof(struct fcp_cmnd_iu) +
2420
		fcp_cmnd_iu->add_fcp_cdb_length + sizeof(u32);
L
Linus Torvalds 已提交
2421

S
Swen Schillig 已提交
2422 2423 2424 2425
	real_bytes = zfcp_qdio_sbals_from_sg(req, sbtype,
					     scsi_sglist(scsi_cmnd),
					     FSF_MAX_SBALS_PER_REQ);
	if (unlikely(real_bytes < 0)) {
2426
		if (req->sbal_number >= FSF_MAX_SBALS_PER_REQ) {
S
Swen Schillig 已提交
2427
			dev_err(&adapter->ccw_device->dev,
2428 2429
				"Oversize data package, unit 0x%016Lx "
				"on port 0x%016Lx closed\n",
2430 2431
				(unsigned long long)unit->fcp_lun,
				(unsigned long long)unit->port->wwpn);
2432
			zfcp_erp_unit_shutdown(unit, 0, "fssfct1", req);
S
Swen Schillig 已提交
2433 2434 2435
			retval = -EINVAL;
		}
		goto failed_scsi_cmnd;
L
Linus Torvalds 已提交
2436 2437
	}

S
Swen Schillig 已提交
2438
	zfcp_set_fcp_dl(fcp_cmnd_iu, real_bytes);
L
Linus Torvalds 已提交
2439

S
Swen Schillig 已提交
2440 2441 2442
	retval = zfcp_fsf_req_send(req);
	if (unlikely(retval))
		goto failed_scsi_cmnd;
L
Linus Torvalds 已提交
2443

S
Swen Schillig 已提交
2444
	goto out;
L
Linus Torvalds 已提交
2445

S
Swen Schillig 已提交
2446 2447 2448 2449 2450
failed_scsi_cmnd:
	zfcp_unit_put(unit);
	zfcp_fsf_req_free(req);
	scsi_cmnd->host_scribble = NULL;
out:
2451
	spin_unlock(&adapter->req_q_lock);
S
Swen Schillig 已提交
2452
	return retval;
L
Linus Torvalds 已提交
2453 2454 2455
}

/**
S
Swen Schillig 已提交
2456 2457 2458 2459
 * zfcp_fsf_send_fcp_ctm - send SCSI task management command
 * @unit: pointer to struct zfcp_unit
 * @tm_flags: unsigned byte for task management flags
 * Returns: on success pointer to struct fsf_req, NULL otherwise
L
Linus Torvalds 已提交
2460
 */
2461
struct zfcp_fsf_req *zfcp_fsf_send_fcp_ctm(struct zfcp_unit *unit, u8 tm_flags)
L
Linus Torvalds 已提交
2462
{
2463
	struct qdio_buffer_element *sbale;
S
Swen Schillig 已提交
2464 2465
	struct zfcp_fsf_req *req = NULL;
	struct fcp_cmnd_iu *fcp_cmnd_iu;
2466
	struct zfcp_adapter *adapter = unit->port->adapter;
L
Linus Torvalds 已提交
2467

S
Swen Schillig 已提交
2468 2469 2470
	if (unlikely(!(atomic_read(&unit->status) &
		       ZFCP_STATUS_COMMON_UNBLOCKED)))
		return NULL;
L
Linus Torvalds 已提交
2471

2472 2473
	spin_lock_bh(&adapter->req_q_lock);
	if (zfcp_fsf_req_sbal_get(adapter))
S
Swen Schillig 已提交
2474
		goto out;
2475
	req = zfcp_fsf_req_create(adapter, FSF_QTCB_FCP_CMND, 0,
S
Swen Schillig 已提交
2476
				  adapter->pool.fsf_req_scsi);
2477 2478
	if (IS_ERR(req)) {
		req = NULL;
S
Swen Schillig 已提交
2479
		goto out;
2480
	}
L
Linus Torvalds 已提交
2481

S
Swen Schillig 已提交
2482 2483 2484 2485 2486 2487 2488 2489
	req->status |= ZFCP_STATUS_FSFREQ_TASK_MANAGEMENT;
	req->data = unit;
	req->handler = zfcp_fsf_send_fcp_command_handler;
	req->qtcb->header.lun_handle = unit->handle;
	req->qtcb->header.port_handle = unit->port->handle;
	req->qtcb->bottom.io.data_direction = FSF_DATADIR_CMND;
	req->qtcb->bottom.io.service_class = FSF_CLASS_3;
	req->qtcb->bottom.io.fcp_cmnd_length = 	sizeof(struct fcp_cmnd_iu) +
2490
						sizeof(u32);
S
Swen Schillig 已提交
2491 2492 2493 2494

	sbale = zfcp_qdio_sbale_req(req);
	sbale[0].flags |= SBAL_FLAGS0_TYPE_WRITE;
	sbale[1].flags |= SBAL_FLAGS_LAST_ENTRY;
L
Linus Torvalds 已提交
2495

S
Swen Schillig 已提交
2496 2497 2498
	fcp_cmnd_iu = (struct fcp_cmnd_iu *) &req->qtcb->bottom.io.fcp_cmnd;
	fcp_cmnd_iu->fcp_lun = unit->fcp_lun;
	fcp_cmnd_iu->task_management_flags = tm_flags;
L
Linus Torvalds 已提交
2499

S
Swen Schillig 已提交
2500 2501 2502
	zfcp_fsf_start_timer(req, ZFCP_SCSI_ER_TIMEOUT);
	if (!zfcp_fsf_req_send(req))
		goto out;
L
Linus Torvalds 已提交
2503

S
Swen Schillig 已提交
2504 2505 2506
	zfcp_fsf_req_free(req);
	req = NULL;
out:
2507
	spin_unlock_bh(&adapter->req_q_lock);
S
Swen Schillig 已提交
2508 2509
	return req;
}
L
Linus Torvalds 已提交
2510

S
Swen Schillig 已提交
2511 2512
static void zfcp_fsf_control_file_handler(struct zfcp_fsf_req *req)
{
L
Linus Torvalds 已提交
2513 2514
}

S
Swen Schillig 已提交
2515 2516 2517 2518 2519
/**
 * zfcp_fsf_control_file - control file upload/download
 * @adapter: pointer to struct zfcp_adapter
 * @fsf_cfdc: pointer to struct zfcp_fsf_cfdc
 * Returns: on success pointer to struct zfcp_fsf_req, NULL otherwise
L
Linus Torvalds 已提交
2520
 */
S
Swen Schillig 已提交
2521 2522
struct zfcp_fsf_req *zfcp_fsf_control_file(struct zfcp_adapter *adapter,
					   struct zfcp_fsf_cfdc *fsf_cfdc)
L
Linus Torvalds 已提交
2523
{
2524
	struct qdio_buffer_element *sbale;
S
Swen Schillig 已提交
2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541
	struct zfcp_fsf_req *req = NULL;
	struct fsf_qtcb_bottom_support *bottom;
	int direction, retval = -EIO, bytes;

	if (!(adapter->adapter_features & FSF_FEATURE_CFDC))
		return ERR_PTR(-EOPNOTSUPP);

	switch (fsf_cfdc->command) {
	case FSF_QTCB_DOWNLOAD_CONTROL_FILE:
		direction = SBAL_FLAGS0_TYPE_WRITE;
		break;
	case FSF_QTCB_UPLOAD_CONTROL_FILE:
		direction = SBAL_FLAGS0_TYPE_READ;
		break;
	default:
		return ERR_PTR(-EINVAL);
	}
L
Linus Torvalds 已提交
2542

2543
	spin_lock_bh(&adapter->req_q_lock);
S
Swen Schillig 已提交
2544 2545
	if (zfcp_fsf_req_sbal_get(adapter))
		goto out;
L
Linus Torvalds 已提交
2546

S
Swen Schillig 已提交
2547
	req = zfcp_fsf_req_create(adapter, fsf_cfdc->command, 0, NULL);
2548
	if (IS_ERR(req)) {
S
Swen Schillig 已提交
2549 2550 2551
		retval = -EPERM;
		goto out;
	}
L
Linus Torvalds 已提交
2552

S
Swen Schillig 已提交
2553
	req->handler = zfcp_fsf_control_file_handler;
L
Linus Torvalds 已提交
2554

S
Swen Schillig 已提交
2555 2556
	sbale = zfcp_qdio_sbale_req(req);
	sbale[0].flags |= direction;
2557

S
Swen Schillig 已提交
2558 2559 2560
	bottom = &req->qtcb->bottom.support;
	bottom->operation_subtype = FSF_CFDC_OPERATION_SUBTYPE;
	bottom->option = fsf_cfdc->option;
2561

S
Swen Schillig 已提交
2562 2563 2564 2565 2566 2567
	bytes = zfcp_qdio_sbals_from_sg(req, direction, fsf_cfdc->sg,
					FSF_MAX_SBALS_PER_REQ);
	if (bytes != ZFCP_CFDC_MAX_SIZE) {
		zfcp_fsf_req_free(req);
		goto out;
	}
L
Linus Torvalds 已提交
2568

S
Swen Schillig 已提交
2569 2570 2571
	zfcp_fsf_start_timer(req, ZFCP_FSF_REQUEST_TIMEOUT);
	retval = zfcp_fsf_req_send(req);
out:
2572
	spin_unlock_bh(&adapter->req_q_lock);
2573

S
Swen Schillig 已提交
2574 2575 2576 2577
	if (!retval) {
		wait_event(req->completion_wq,
			   req->status & ZFCP_STATUS_FSFREQ_COMPLETED);
		return req;
L
Linus Torvalds 已提交
2578
	}
S
Swen Schillig 已提交
2579
	return ERR_PTR(retval);
L
Linus Torvalds 已提交
2580
}