zfcp_fsf.c 67.6 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, 2010
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>
13
#include <linux/slab.h>
14
#include <scsi/fc/fc_els.h>
L
Linus Torvalds 已提交
15
#include "zfcp_ext.h"
16
#include "zfcp_fc.h"
17
#include "zfcp_dbf.h"
18
#include "zfcp_qdio.h"
19
#include "zfcp_reqlist.h"
L
Linus Torvalds 已提交
20

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

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 已提交
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
/* 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 已提交
63 64
static void zfcp_act_eval_err(struct zfcp_adapter *adapter, u32 table)
{
S
Swen Schillig 已提交
65
	u16 subtable = table >> 16;
C
Christof Schmitt 已提交
66
	u16 rule = table & 0xffff;
67
	const char *act_type[] = { "unknown", "OS", "WWPN", "DID", "LUN" };
C
Christof Schmitt 已提交
68

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

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,
80
		 "Access denied to port 0x%016Lx\n",
81
		 (unsigned long long)port->wwpn);
C
Christof Schmitt 已提交
82 83
	zfcp_act_eval_err(req->adapter, header->fsf_status_qual.halfword[0]);
	zfcp_act_eval_err(req->adapter, header->fsf_status_qual.halfword[1]);
84
	zfcp_erp_port_access_denied(port, "fspad_1", req);
C
Christof Schmitt 已提交
85 86 87 88 89 90 91 92
	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,
93
		 "Access denied to unit 0x%016Lx on port 0x%016Lx\n",
94 95
		 (unsigned long long)unit->fcp_lun,
		 (unsigned long long)unit->port->wwpn);
C
Christof Schmitt 已提交
96 97
	zfcp_act_eval_err(req->adapter, header->fsf_status_qual.halfword[0]);
	zfcp_act_eval_err(req->adapter, header->fsf_status_qual.halfword[1]);
98
	zfcp_erp_unit_access_denied(unit, "fsuad_1", req);
C
Christof Schmitt 已提交
99 100 101 102 103
	req->status |= ZFCP_STATUS_FSFREQ_ERROR;
}

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

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

123 124 125
	if (likely(req->qtcb))
		kmem_cache_free(zfcp_data.qtcb_cache, req->qtcb);
	kfree(req);
L
Linus Torvalds 已提交
126 127
}

S
Swen Schillig 已提交
128
static void zfcp_fsf_status_read_port_closed(struct zfcp_fsf_req *req)
L
Linus Torvalds 已提交
129
{
130
	unsigned long flags;
S
Swen Schillig 已提交
131 132 133
	struct fsf_status_read_buffer *sr_buf = req->data;
	struct zfcp_adapter *adapter = req->adapter;
	struct zfcp_port *port;
134
	int d_id = ntoh24(sr_buf->d_id);
L
Linus Torvalds 已提交
135

136 137
	read_lock_irqsave(&adapter->port_list_lock, flags);
	list_for_each_entry(port, &adapter->port_list, list)
S
Swen Schillig 已提交
138
		if (port->d_id == d_id) {
139
			zfcp_erp_port_reopen(port, 0, "fssrpc1", req);
140
			break;
S
Swen Schillig 已提交
141
		}
142
	read_unlock_irqrestore(&adapter->port_list_lock, flags);
L
Linus Torvalds 已提交
143 144
}

145
static void zfcp_fsf_link_down_info_eval(struct zfcp_fsf_req *req, char *id,
S
Swen Schillig 已提交
146
					 struct fsf_link_down_info *link_down)
147
{
S
Swen Schillig 已提交
148
	struct zfcp_adapter *adapter = req->adapter;
149

S
Swen Schillig 已提交
150
	if (atomic_read(&adapter->status) & ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED)
151 152 153
		return;

	atomic_set_mask(ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED, &adapter->status);
154

155
	zfcp_scsi_schedule_rports_block(adapter);
156

S
Swen Schillig 已提交
157
	if (!link_down)
158
		goto out;
159

160 161
	switch (link_down->error_code) {
	case FSF_PSQ_LINK_NO_LIGHT:
S
Swen Schillig 已提交
162
		dev_warn(&req->adapter->ccw_device->dev,
163 164
			 "There is no light signal from the local "
			 "fibre channel cable\n");
165 166
		break;
	case FSF_PSQ_LINK_WRAP_PLUG:
S
Swen Schillig 已提交
167
		dev_warn(&req->adapter->ccw_device->dev,
168 169
			 "There is a wrap plug instead of a fibre "
			 "channel cable\n");
170 171
		break;
	case FSF_PSQ_LINK_NO_FCP:
S
Swen Schillig 已提交
172
		dev_warn(&req->adapter->ccw_device->dev,
173 174
			 "The adjacent fibre channel node does not "
			 "support FCP\n");
175 176
		break;
	case FSF_PSQ_LINK_FIRMWARE_UPDATE:
S
Swen Schillig 已提交
177
		dev_warn(&req->adapter->ccw_device->dev,
178 179
			 "The FCP device is suspended because of a "
			 "firmware update\n");
C
Christof Schmitt 已提交
180
		break;
181
	case FSF_PSQ_LINK_INVALID_WWPN:
S
Swen Schillig 已提交
182
		dev_warn(&req->adapter->ccw_device->dev,
183 184
			 "The FCP device detected a WWPN that is "
			 "duplicate or not valid\n");
185 186
		break;
	case FSF_PSQ_LINK_NO_NPIV_SUPPORT:
S
Swen Schillig 已提交
187
		dev_warn(&req->adapter->ccw_device->dev,
188
			 "The fibre channel fabric does not support NPIV\n");
189 190
		break;
	case FSF_PSQ_LINK_NO_FCP_RESOURCES:
S
Swen Schillig 已提交
191
		dev_warn(&req->adapter->ccw_device->dev,
192
			 "The FCP adapter cannot support more NPIV ports\n");
193 194
		break;
	case FSF_PSQ_LINK_NO_FABRIC_RESOURCES:
S
Swen Schillig 已提交
195
		dev_warn(&req->adapter->ccw_device->dev,
196 197
			 "The adjacent switch cannot support "
			 "more NPIV ports\n");
198 199
		break;
	case FSF_PSQ_LINK_FABRIC_LOGIN_UNABLE:
S
Swen Schillig 已提交
200
		dev_warn(&req->adapter->ccw_device->dev,
201 202
			 "The FCP adapter could not log in to the "
			 "fibre channel fabric\n");
203 204
		break;
	case FSF_PSQ_LINK_WWPN_ASSIGNMENT_CORRUPTED:
S
Swen Schillig 已提交
205
		dev_warn(&req->adapter->ccw_device->dev,
206 207
			 "The WWPN assignment file on the FCP adapter "
			 "has been damaged\n");
208 209
		break;
	case FSF_PSQ_LINK_MODE_TABLE_CURRUPTED:
S
Swen Schillig 已提交
210
		dev_warn(&req->adapter->ccw_device->dev,
211 212
			 "The mode table on the FCP adapter "
			 "has been damaged\n");
213 214
		break;
	case FSF_PSQ_LINK_NO_WWPN_ASSIGNMENT:
S
Swen Schillig 已提交
215
		dev_warn(&req->adapter->ccw_device->dev,
216 217
			 "All NPIV ports on the FCP adapter have "
			 "been assigned\n");
218 219
		break;
	default:
S
Swen Schillig 已提交
220
		dev_warn(&req->adapter->ccw_device->dev,
221 222
			 "The link between the FCP adapter and "
			 "the FC fabric is down\n");
223
	}
S
Swen Schillig 已提交
224 225
out:
	zfcp_erp_adapter_failed(adapter, id, req);
226 227
}

S
Swen Schillig 已提交
228
static void zfcp_fsf_status_read_link_down(struct zfcp_fsf_req *req)
L
Linus Torvalds 已提交
229
{
S
Swen Schillig 已提交
230 231 232
	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 已提交
233

S
Swen Schillig 已提交
234 235
	switch (sr_buf->status_subtype) {
	case FSF_STATUS_READ_SUB_NO_PHYSICAL_LINK:
236
		zfcp_fsf_link_down_info_eval(req, "fssrld1", ldi);
L
Linus Torvalds 已提交
237
		break;
S
Swen Schillig 已提交
238
	case FSF_STATUS_READ_SUB_FDISC_FAILED:
239
		zfcp_fsf_link_down_info_eval(req, "fssrld2", ldi);
L
Linus Torvalds 已提交
240
		break;
S
Swen Schillig 已提交
241
	case FSF_STATUS_READ_SUB_FIRMWARE_UPDATE:
242
		zfcp_fsf_link_down_info_eval(req, "fssrld3", NULL);
S
Swen Schillig 已提交
243 244
	};
}
L
Linus Torvalds 已提交
245

S
Swen Schillig 已提交
246 247 248 249
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 已提交
250

S
Swen Schillig 已提交
251
	if (req->status & ZFCP_STATUS_FSFREQ_DISMISSED) {
S
Swen Schillig 已提交
252
		zfcp_dbf_hba_fsf_unsol("dism", adapter->dbf, sr_buf);
253
		mempool_free(sr_buf, adapter->pool.status_read_data);
S
Swen Schillig 已提交
254 255 256
		zfcp_fsf_req_free(req);
		return;
	}
L
Linus Torvalds 已提交
257

S
Swen Schillig 已提交
258
	zfcp_dbf_hba_fsf_unsol("read", adapter->dbf, sr_buf);
L
Linus Torvalds 已提交
259

S
Swen Schillig 已提交
260 261 262
	switch (sr_buf->status_type) {
	case FSF_STATUS_READ_PORT_CLOSED:
		zfcp_fsf_status_read_port_closed(req);
L
Linus Torvalds 已提交
263
		break;
S
Swen Schillig 已提交
264 265
	case FSF_STATUS_READ_INCOMING_ELS:
		zfcp_fc_incoming_els(req);
L
Linus Torvalds 已提交
266
		break;
S
Swen Schillig 已提交
267
	case FSF_STATUS_READ_SENSE_DATA_AVAIL:
L
Linus Torvalds 已提交
268
		break;
S
Swen Schillig 已提交
269
	case FSF_STATUS_READ_BIT_ERROR_THRESHOLD:
270 271 272
		dev_warn(&adapter->ccw_device->dev,
			 "The error threshold for checksum statistics "
			 "has been exceeded\n");
S
Swen Schillig 已提交
273
		zfcp_dbf_hba_berr(adapter->dbf, req);
L
Linus Torvalds 已提交
274
		break;
S
Swen Schillig 已提交
275 276 277 278 279
	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,
280
			 "The local link has been restored\n");
S
Swen Schillig 已提交
281
		/* All ports should be marked as ready to run again */
282
		zfcp_erp_modify_adapter_status(adapter, "fssrh_1", NULL,
S
Swen Schillig 已提交
283 284 285 286 287
					       ZFCP_STATUS_COMMON_RUNNING,
					       ZFCP_SET);
		zfcp_erp_adapter_reopen(adapter,
					ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED |
					ZFCP_STATUS_COMMON_ERP_FAILED,
288
					"fssrh_2", req);
S
Swen Schillig 已提交
289 290 291
		break;
	case FSF_STATUS_READ_NOTIFICATION_LOST:
		if (sr_buf->status_subtype & FSF_STATUS_READ_SUB_ACT_UPDATED)
292 293
			zfcp_erp_adapter_access_changed(adapter, "fssrh_3",
							req);
S
Swen Schillig 已提交
294
		if (sr_buf->status_subtype & FSF_STATUS_READ_SUB_INCOMING_ELS)
295
			queue_work(adapter->work_queue, &adapter->scan_work);
S
Swen Schillig 已提交
296 297
		break;
	case FSF_STATUS_READ_CFDC_UPDATED:
298
		zfcp_erp_adapter_access_changed(adapter, "fssrh_4", req);
S
Swen Schillig 已提交
299 300 301
		break;
	case FSF_STATUS_READ_FEATURE_UPDATE_ALERT:
		adapter->adapter_features = sr_buf->payload.word[0];
L
Linus Torvalds 已提交
302 303 304
		break;
	}

305
	mempool_free(sr_buf, adapter->pool.status_read_data);
S
Swen Schillig 已提交
306
	zfcp_fsf_req_free(req);
L
Linus Torvalds 已提交
307

S
Swen Schillig 已提交
308
	atomic_inc(&adapter->stat_miss);
309
	queue_work(adapter->work_queue, &adapter->stat_work);
S
Swen Schillig 已提交
310
}
L
Linus Torvalds 已提交
311

S
Swen Schillig 已提交
312 313 314 315 316 317 318 319 320 321 322 323
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:
		break;
	case FSF_SQ_NO_RECOM:
		dev_err(&req->adapter->ccw_device->dev,
324 325
			"The FCP adapter reported a problem "
			"that cannot be recovered\n");
326
		zfcp_erp_adapter_shutdown(req->adapter, 0, "fsfsqe1", req);
S
Swen Schillig 已提交
327 328 329 330
		break;
	}
	/* all non-return stats set FSFREQ_ERROR*/
	req->status |= ZFCP_STATUS_FSFREQ_ERROR;
L
Linus Torvalds 已提交
331 332
}

S
Swen Schillig 已提交
333
static void zfcp_fsf_fsfstatus_eval(struct zfcp_fsf_req *req)
L
Linus Torvalds 已提交
334
{
S
Swen Schillig 已提交
335 336
	if (unlikely(req->status & ZFCP_STATUS_FSFREQ_ERROR))
		return;
L
Linus Torvalds 已提交
337

S
Swen Schillig 已提交
338 339 340
	switch (req->qtcb->header.fsf_status) {
	case FSF_UNKNOWN_COMMAND:
		dev_err(&req->adapter->ccw_device->dev,
341
			"The FCP adapter does not recognize the command 0x%x\n",
S
Swen Schillig 已提交
342
			req->qtcb->header.fsf_command);
343
		zfcp_erp_adapter_shutdown(req->adapter, 0, "fsfse_1", req);
S
Swen Schillig 已提交
344 345 346 347 348 349 350
		req->status |= ZFCP_STATUS_FSFREQ_ERROR;
		break;
	case FSF_ADAPTER_STATUS_AVAILABLE:
		zfcp_fsf_fsfstatus_qual_eval(req);
		break;
	}
}
L
Linus Torvalds 已提交
351

S
Swen Schillig 已提交
352 353 354 355 356
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 已提交
357

S
Swen Schillig 已提交
358
	zfcp_dbf_hba_fsf_response(req);
L
Linus Torvalds 已提交
359

S
Swen Schillig 已提交
360
	if (req->status & ZFCP_STATUS_FSFREQ_DISMISSED) {
361
		req->status |= ZFCP_STATUS_FSFREQ_ERROR;
S
Swen Schillig 已提交
362 363
		return;
	}
L
Linus Torvalds 已提交
364

S
Swen Schillig 已提交
365 366 367 368 369 370
	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,
371 372 373
			"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]);
374
		zfcp_erp_adapter_shutdown(adapter, 0, "fspse_1", req);
S
Swen Schillig 已提交
375 376 377
		break;
	case FSF_PROT_ERROR_STATE:
	case FSF_PROT_SEQ_NUMB_ERROR:
378
		zfcp_erp_adapter_reopen(adapter, 0, "fspse_2", req);
379
		req->status |= ZFCP_STATUS_FSFREQ_ERROR;
S
Swen Schillig 已提交
380 381 382
		break;
	case FSF_PROT_UNSUPP_QTCB_TYPE:
		dev_err(&adapter->ccw_device->dev,
383
			"The QTCB type is not supported by the FCP adapter\n");
384
		zfcp_erp_adapter_shutdown(adapter, 0, "fspse_3", req);
S
Swen Schillig 已提交
385 386 387 388 389 390 391
		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,
392
			"0x%Lx is an ambiguous request identifier\n",
S
Swen Schillig 已提交
393
			(unsigned long long)qtcb->bottom.support.req_handle);
394
		zfcp_erp_adapter_shutdown(adapter, 0, "fspse_4", req);
S
Swen Schillig 已提交
395 396
		break;
	case FSF_PROT_LINK_DOWN:
397 398
		zfcp_fsf_link_down_info_eval(req, "fspse_5",
					     &psq->link_down_info);
399
		/* go through reopen to flush pending requests */
400
		zfcp_erp_adapter_reopen(adapter, 0, "fspse_6", req);
S
Swen Schillig 已提交
401 402 403
		break;
	case FSF_PROT_REEST_QUEUE:
		/* All ports should be marked as ready to run again */
404
		zfcp_erp_modify_adapter_status(adapter, "fspse_7", NULL,
S
Swen Schillig 已提交
405 406 407 408
					       ZFCP_STATUS_COMMON_RUNNING,
					       ZFCP_SET);
		zfcp_erp_adapter_reopen(adapter,
					ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED |
409 410
					ZFCP_STATUS_COMMON_ERP_FAILED,
					"fspse_8", req);
S
Swen Schillig 已提交
411 412 413
		break;
	default:
		dev_err(&adapter->ccw_device->dev,
414
			"0x%x is not a valid transfer protocol status\n",
S
Swen Schillig 已提交
415
			qtcb->prefix.prot_status);
416
		zfcp_erp_adapter_shutdown(adapter, 0, "fspse_9", req);
S
Swen Schillig 已提交
417 418
	}
	req->status |= ZFCP_STATUS_FSFREQ_ERROR;
L
Linus Torvalds 已提交
419 420
}

S
Swen Schillig 已提交
421 422 423 424 425 426 427 428 429
/**
 * 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.
 */
430
static void zfcp_fsf_req_complete(struct zfcp_fsf_req *req)
L
Linus Torvalds 已提交
431
{
S
Swen Schillig 已提交
432 433 434 435
	if (unlikely(req->fsf_command == FSF_QTCB_UNSOLICITED_STATUS)) {
		zfcp_fsf_status_read_handler(req);
		return;
	}
L
Linus Torvalds 已提交
436

S
Swen Schillig 已提交
437 438 439 440
	del_timer(&req->timer);
	zfcp_fsf_protstatus_eval(req);
	zfcp_fsf_fsfstatus_eval(req);
	req->handler(req);
L
Linus Torvalds 已提交
441

S
Swen Schillig 已提交
442
	if (req->erp_action)
443
		zfcp_erp_notify(req->erp_action, 0);
L
Linus Torvalds 已提交
444

S
Swen Schillig 已提交
445 446 447
	if (likely(req->status & ZFCP_STATUS_FSFREQ_CLEANUP))
		zfcp_fsf_req_free(req);
	else
448
		complete(&req->completion);
S
Swen Schillig 已提交
449
}
L
Linus Torvalds 已提交
450

451 452 453 454 455 456 457 458 459 460 461 462 463 464 465
/**
 * zfcp_fsf_req_dismiss_all - dismiss all fsf requests
 * @adapter: pointer to struct zfcp_adapter
 *
 * 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.
 */
void zfcp_fsf_req_dismiss_all(struct zfcp_adapter *adapter)
{
	struct zfcp_fsf_req *req, *tmp;
	LIST_HEAD(remove_queue);

	BUG_ON(atomic_read(&adapter->status) & ZFCP_STATUS_ADAPTER_QDIOUP);
466
	zfcp_reqlist_move(adapter->req_list, &remove_queue);
467 468 469 470 471 472 473 474

	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);
	}
}

S
Swen Schillig 已提交
475 476
static int zfcp_fsf_exchange_config_evaluate(struct zfcp_fsf_req *req)
{
477
	struct fsf_qtcb_bottom_config *bottom = &req->qtcb->bottom.config;
S
Swen Schillig 已提交
478 479
	struct zfcp_adapter *adapter = req->adapter;
	struct Scsi_Host *shost = adapter->scsi_host;
480
	struct fc_els_flogi *nsp, *plogi;
L
Linus Torvalds 已提交
481

482 483 484 485 486
	/* adjust pointers for missing command code */
	nsp = (struct fc_els_flogi *) ((u8 *)&bottom->nport_serv_param
					- sizeof(u32));
	plogi = (struct fc_els_flogi *) ((u8 *)&bottom->plogi_payload
					- sizeof(u32));
L
Linus Torvalds 已提交
487

S
Swen Schillig 已提交
488 489 490
	if (req->data)
		memcpy(req->data, bottom, sizeof(*bottom));

491 492
	fc_host_port_name(shost) = nsp->fl_wwpn;
	fc_host_node_name(shost) = nsp->fl_wwnn;
493
	fc_host_port_id(shost) = ntoh24(bottom->s_id);
S
Swen Schillig 已提交
494 495 496 497 498
	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;
499
	adapter->stat_read_buf_num = max(bottom->status_read_buf_num, (u16)16);
S
Swen Schillig 已提交
500 501 502 503 504 505

	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:
506
		adapter->peer_d_id = ntoh24(bottom->peer_d_id);
507 508
		adapter->peer_wwpn = plogi->fl_wwpn;
		adapter->peer_wwnn = plogi->fl_wwnn;
S
Swen Schillig 已提交
509 510 511 512 513 514 515
		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;
516
		/* fall through */
S
Swen Schillig 已提交
517 518
	default:
		dev_err(&adapter->ccw_device->dev,
519 520
			"Unknown or unsupported arbitrated loop "
			"fibre channel topology detected\n");
521
		zfcp_erp_adapter_shutdown(adapter, 0, "fsece_1", req);
S
Swen Schillig 已提交
522
		return -EIO;
L
Linus Torvalds 已提交
523
	}
S
Swen Schillig 已提交
524

L
Linus Torvalds 已提交
525 526 527
	return 0;
}

S
Swen Schillig 已提交
528
static void zfcp_fsf_exchange_config_data_handler(struct zfcp_fsf_req *req)
C
Christof Schmitt 已提交
529 530
{
	struct zfcp_adapter *adapter = req->adapter;
S
Swen Schillig 已提交
531 532 533
	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 已提交
534

S
Swen Schillig 已提交
535 536
	if (req->status & ZFCP_STATUS_FSFREQ_ERROR)
		return;
L
Linus Torvalds 已提交
537

S
Swen Schillig 已提交
538 539 540 541 542 543
	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;
544

S
Swen Schillig 已提交
545 546 547 548
	switch (qtcb->header.fsf_status) {
	case FSF_GOOD:
		if (zfcp_fsf_exchange_config_evaluate(req))
			return;
L
Linus Torvalds 已提交
549

S
Swen Schillig 已提交
550 551
		if (bottom->max_qtcb_size < sizeof(struct fsf_qtcb)) {
			dev_err(&adapter->ccw_device->dev,
552 553 554
				"FCP adapter maximum QTCB size (%d bytes) "
				"is too small\n",
				bottom->max_qtcb_size);
555
			zfcp_erp_adapter_shutdown(adapter, 0, "fsecdh1", req);
S
Swen Schillig 已提交
556 557 558 559
			return;
		}
		atomic_set_mask(ZFCP_STATUS_ADAPTER_XCONFIG_OK,
				&adapter->status);
L
Linus Torvalds 已提交
560
		break;
S
Swen Schillig 已提交
561 562 563 564 565 566 567
	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 已提交
568

S
Swen Schillig 已提交
569 570
		atomic_set_mask(ZFCP_STATUS_ADAPTER_XCONFIG_OK,
				&adapter->status);
L
Linus Torvalds 已提交
571

572
		zfcp_fsf_link_down_info_eval(req, "fsecdh2",
S
Swen Schillig 已提交
573
			&qtcb->header.fsf_status_qual.link_down_info);
L
Linus Torvalds 已提交
574
		break;
S
Swen Schillig 已提交
575
	default:
576
		zfcp_erp_adapter_shutdown(adapter, 0, "fsecdh3", req);
S
Swen Schillig 已提交
577 578
		return;
	}
L
Linus Torvalds 已提交
579

S
Swen Schillig 已提交
580 581 582 583 584 585 586
	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 已提交
587

S
Swen Schillig 已提交
588 589
	if (FSF_QTCB_CURRENT_VERSION < bottom->low_qtcb_version) {
		dev_err(&adapter->ccw_device->dev,
590 591
			"The FCP adapter only supports newer "
			"control block versions\n");
592
		zfcp_erp_adapter_shutdown(adapter, 0, "fsecdh4", req);
S
Swen Schillig 已提交
593 594 595 596
		return;
	}
	if (FSF_QTCB_CURRENT_VERSION > bottom->high_qtcb_version) {
		dev_err(&adapter->ccw_device->dev,
597 598
			"The FCP adapter only supports older "
			"control block versions\n");
599
		zfcp_erp_adapter_shutdown(adapter, 0, "fsecdh5", req);
S
Swen Schillig 已提交
600 601
	}
}
L
Linus Torvalds 已提交
602

S
Swen Schillig 已提交
603 604 605 606 607
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 已提交
608

S
Swen Schillig 已提交
609 610
	if (req->data)
		memcpy(req->data, bottom, sizeof(*bottom));
611

612
	if (adapter->connection_features & FSF_FEATURE_NPIV_MODE) {
S
Swen Schillig 已提交
613
		fc_host_permanent_port_name(shost) = bottom->wwpn;
614 615
		fc_host_port_type(shost) = FC_PORTTYPE_NPIV;
	} else
S
Swen Schillig 已提交
616 617 618
		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;
619 620 621 622
	memcpy(fc_host_supported_fc4s(shost), bottom->supported_fc4_types,
	       FC_FC4_LIST_SIZE);
	memcpy(fc_host_active_fc4s(shost), bottom->active_fc4_types,
	       FC_FC4_LIST_SIZE);
S
Swen Schillig 已提交
623
}
L
Linus Torvalds 已提交
624

S
Swen Schillig 已提交
625 626 627 628 629 630 631 632 633 634 635 636 637
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);
638
		zfcp_fsf_link_down_info_eval(req, "fsepdh1",
S
Swen Schillig 已提交
639
			&qtcb->header.fsf_status_qual.link_down_info);
640
		break;
L
Linus Torvalds 已提交
641
	}
S
Swen Schillig 已提交
642
}
643

644
static struct zfcp_fsf_req *zfcp_fsf_alloc(mempool_t *pool)
S
Swen Schillig 已提交
645 646
{
	struct zfcp_fsf_req *req;
647 648 649 650 651 652 653

	if (likely(pool))
		req = mempool_alloc(pool, GFP_ATOMIC);
	else
		req = kmalloc(sizeof(*req), GFP_ATOMIC);

	if (unlikely(!req))
S
Swen Schillig 已提交
654
		return NULL;
655

S
Swen Schillig 已提交
656
	memset(req, 0, sizeof(*req));
657
	req->pool = pool;
S
Swen Schillig 已提交
658 659 660
	return req;
}

661
static struct fsf_qtcb *zfcp_qtcb_alloc(mempool_t *pool)
S
Swen Schillig 已提交
662
{
663
	struct fsf_qtcb *qtcb;
S
Swen Schillig 已提交
664 665 666 667

	if (likely(pool))
		qtcb = mempool_alloc(pool, GFP_ATOMIC);
	else
668 669
		qtcb = kmem_cache_alloc(zfcp_data.qtcb_cache, GFP_ATOMIC);

S
Swen Schillig 已提交
670 671 672 673
	if (unlikely(!qtcb))
		return NULL;

	memset(qtcb, 0, sizeof(*qtcb));
674
	return qtcb;
S
Swen Schillig 已提交
675 676
}

677
static struct zfcp_fsf_req *zfcp_fsf_req_create(struct zfcp_qdio *qdio,
678 679
						u32 fsf_cmd, u32 sbtype,
						mempool_t *pool)
L
Linus Torvalds 已提交
680
{
681
	struct zfcp_adapter *adapter = qdio->adapter;
682
	struct zfcp_fsf_req *req = zfcp_fsf_alloc(pool);
L
Linus Torvalds 已提交
683

S
Swen Schillig 已提交
684
	if (unlikely(!req))
685
		return ERR_PTR(-ENOMEM);
L
Linus Torvalds 已提交
686

S
Swen Schillig 已提交
687 688
	if (adapter->req_no == 0)
		adapter->req_no++;
L
Linus Torvalds 已提交
689

S
Swen Schillig 已提交
690 691
	INIT_LIST_HEAD(&req->list);
	init_timer(&req->timer);
692
	init_completion(&req->completion);
L
Linus Torvalds 已提交
693

S
Swen Schillig 已提交
694 695
	req->adapter = adapter;
	req->fsf_command = fsf_cmd;
696
	req->req_id = adapter->req_no;
S
Swen Schillig 已提交
697

698 699 700 701 702 703 704 705 706 707 708
	if (likely(fsf_cmd != FSF_QTCB_UNSOLICITED_STATUS)) {
		if (likely(pool))
			req->qtcb = zfcp_qtcb_alloc(adapter->pool.qtcb_pool);
		else
			req->qtcb = zfcp_qtcb_alloc(NULL);

		if (unlikely(!req->qtcb)) {
			zfcp_fsf_req_free(req);
			return ERR_PTR(-ENOMEM);
		}

709
		req->seq_no = adapter->fsf_req_seq_no;
710
		req->qtcb->prefix.req_seq_no = adapter->fsf_req_seq_no;
S
Swen Schillig 已提交
711 712 713 714 715 716 717 718
		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;
	}

719 720 721
	zfcp_qdio_req_init(adapter->qdio, &req->qdio_req, req->req_id, sbtype,
			   req->qtcb, sizeof(struct fsf_qtcb));

S
Swen Schillig 已提交
722 723 724 725
	if (!(atomic_read(&adapter->status) & ZFCP_STATUS_ADAPTER_QDIOUP)) {
		zfcp_fsf_req_free(req);
		return ERR_PTR(-EIO);
	}
726

S
Swen Schillig 已提交
727
	return req;
L
Linus Torvalds 已提交
728 729
}

S
Swen Schillig 已提交
730 731 732
static int zfcp_fsf_req_send(struct zfcp_fsf_req *req)
{
	struct zfcp_adapter *adapter = req->adapter;
733
	struct zfcp_qdio *qdio = adapter->qdio;
734
	int with_qtcb = (req->qtcb != NULL);
735
	int req_id = req->req_id;
S
Swen Schillig 已提交
736

737
	zfcp_reqlist_add(adapter->req_list, req);
S
Swen Schillig 已提交
738

739
	req->qdio_req.qdio_outb_usage = atomic_read(&qdio->req_q.count);
S
Swen Schillig 已提交
740
	req->issued = get_clock();
741
	if (zfcp_qdio_send(qdio, &req->qdio_req)) {
S
Swen Schillig 已提交
742
		del_timer(&req->timer);
743
		/* lookup request again, list might have changed */
744
		zfcp_reqlist_find_rm(adapter->req_list, req_id);
745
		zfcp_erp_adapter_reopen(adapter, 0, "fsrs__1", req);
S
Swen Schillig 已提交
746 747 748 749
		return -EIO;
	}

	/* Don't increase for unsolicited status */
750
	if (with_qtcb)
S
Swen Schillig 已提交
751
		adapter->fsf_req_seq_no++;
752
	adapter->req_no++;
S
Swen Schillig 已提交
753 754 755 756 757 758 759 760 761

	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 已提交
762
 */
763
int zfcp_fsf_status_read(struct zfcp_qdio *qdio)
L
Linus Torvalds 已提交
764
{
765
	struct zfcp_adapter *adapter = qdio->adapter;
S
Swen Schillig 已提交
766 767 768
	struct zfcp_fsf_req *req;
	struct fsf_status_read_buffer *sr_buf;
	int retval = -EIO;
L
Linus Torvalds 已提交
769

770
	spin_lock_bh(&qdio->req_q_lock);
771
	if (zfcp_qdio_sbal_get(qdio))
S
Swen Schillig 已提交
772 773
		goto out;

774
	req = zfcp_fsf_req_create(qdio, FSF_QTCB_UNSOLICITED_STATUS, 0,
775
				  adapter->pool.status_read_req);
776
	if (IS_ERR(req)) {
S
Swen Schillig 已提交
777 778
		retval = PTR_ERR(req);
		goto out;
L
Linus Torvalds 已提交
779 780
	}

781
	sr_buf = mempool_alloc(adapter->pool.status_read_data, GFP_ATOMIC);
S
Swen Schillig 已提交
782 783 784 785 786 787
	if (!sr_buf) {
		retval = -ENOMEM;
		goto failed_buf;
	}
	memset(sr_buf, 0, sizeof(*sr_buf));
	req->data = sr_buf;
788 789 790

	zfcp_qdio_fill_next(qdio, &req->qdio_req, sr_buf, sizeof(*sr_buf));
	zfcp_qdio_set_sbale_last(qdio, &req->qdio_req);
791

S
Swen Schillig 已提交
792 793 794
	retval = zfcp_fsf_req_send(req);
	if (retval)
		goto failed_req_send;
L
Linus Torvalds 已提交
795

S
Swen Schillig 已提交
796 797 798
	goto out;

failed_req_send:
799
	mempool_free(sr_buf, adapter->pool.status_read_data);
S
Swen Schillig 已提交
800 801
failed_buf:
	zfcp_fsf_req_free(req);
S
Swen Schillig 已提交
802
	zfcp_dbf_hba_fsf_unsol("fail", adapter->dbf, NULL);
S
Swen Schillig 已提交
803
out:
804
	spin_unlock_bh(&qdio->req_q_lock);
S
Swen Schillig 已提交
805 806 807 808 809 810 811 812 813 814 815 816
	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 已提交
817
	case FSF_PORT_HANDLE_NOT_VALID:
S
Swen Schillig 已提交
818
		if (fsq->word[0] == fsq->word[1]) {
819 820
			zfcp_erp_adapter_reopen(unit->port->adapter, 0,
						"fsafch1", req);
S
Swen Schillig 已提交
821
			req->status |= ZFCP_STATUS_FSFREQ_ERROR;
L
Linus Torvalds 已提交
822 823 824
		}
		break;
	case FSF_LUN_HANDLE_NOT_VALID:
S
Swen Schillig 已提交
825
		if (fsq->word[0] == fsq->word[1]) {
826
			zfcp_erp_port_reopen(unit->port, 0, "fsafch2", req);
S
Swen Schillig 已提交
827
			req->status |= ZFCP_STATUS_FSFREQ_ERROR;
L
Linus Torvalds 已提交
828 829 830
		}
		break;
	case FSF_FCP_COMMAND_DOES_NOT_EXIST:
S
Swen Schillig 已提交
831
		req->status |= ZFCP_STATUS_FSFREQ_ABORTNOTNEEDED;
L
Linus Torvalds 已提交
832 833
		break;
	case FSF_PORT_BOXED:
834
		zfcp_erp_port_boxed(unit->port, "fsafch3", req);
835
		req->status |= ZFCP_STATUS_FSFREQ_ERROR;
L
Linus Torvalds 已提交
836 837
		break;
	case FSF_LUN_BOXED:
838
		zfcp_erp_unit_boxed(unit, "fsafch4", req);
839
		req->status |= ZFCP_STATUS_FSFREQ_ERROR;
L
Linus Torvalds 已提交
840 841
                break;
	case FSF_ADAPTER_STATUS_AVAILABLE:
S
Swen Schillig 已提交
842
		switch (fsq->word[0]) {
L
Linus Torvalds 已提交
843
		case FSF_SQ_INVOKE_LINK_TEST_PROCEDURE:
844
			zfcp_fc_test_link(unit->port);
845
			/* fall through */
L
Linus Torvalds 已提交
846
		case FSF_SQ_ULP_DEPENDENT_ERP_REQUIRED:
S
Swen Schillig 已提交
847
			req->status |= ZFCP_STATUS_FSFREQ_ERROR;
L
Linus Torvalds 已提交
848 849 850 851
			break;
		}
		break;
	case FSF_GOOD:
S
Swen Schillig 已提交
852
		req->status |= ZFCP_STATUS_FSFREQ_ABORTSUCCEEDED;
L
Linus Torvalds 已提交
853 854 855 856 857
		break;
	}
}

/**
S
Swen Schillig 已提交
858 859 860 861
 * 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 已提交
862 863
 */

S
Swen Schillig 已提交
864
struct zfcp_fsf_req *zfcp_fsf_abort_fcp_command(unsigned long old_req_id,
865
						struct zfcp_unit *unit)
L
Linus Torvalds 已提交
866
{
S
Swen Schillig 已提交
867
	struct zfcp_fsf_req *req = NULL;
868
	struct zfcp_qdio *qdio = unit->port->adapter->qdio;
869

870
	spin_lock_bh(&qdio->req_q_lock);
871
	if (zfcp_qdio_sbal_get(qdio))
S
Swen Schillig 已提交
872
		goto out;
873
	req = zfcp_fsf_req_create(qdio, FSF_QTCB_ABORT_FCP_CMND,
874
				  SBAL_FLAGS0_TYPE_READ,
875
				  qdio->adapter->pool.scsi_abort);
876 877
	if (IS_ERR(req)) {
		req = NULL;
S
Swen Schillig 已提交
878
		goto out;
879
	}
880

S
Swen Schillig 已提交
881 882 883
	if (unlikely(!(atomic_read(&unit->status) &
		       ZFCP_STATUS_COMMON_UNBLOCKED)))
		goto out_error_free;
L
Linus Torvalds 已提交
884

885
	zfcp_qdio_set_sbale_last(qdio, &req->qdio_req);
L
Linus Torvalds 已提交
886

S
Swen Schillig 已提交
887 888 889 890 891 892 893 894 895 896 897 898 899 900
	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:
901
	spin_unlock_bh(&qdio->req_q_lock);
S
Swen Schillig 已提交
902
	return req;
L
Linus Torvalds 已提交
903 904
}

S
Swen Schillig 已提交
905
static void zfcp_fsf_send_ct_handler(struct zfcp_fsf_req *req)
L
Linus Torvalds 已提交
906
{
S
Swen Schillig 已提交
907
	struct zfcp_adapter *adapter = req->adapter;
908
	struct zfcp_fsf_ct_els *ct = req->data;
S
Swen Schillig 已提交
909
	struct fsf_qtcb_header *header = &req->qtcb->header;
L
Linus Torvalds 已提交
910

911
	ct->status = -EINVAL;
L
Linus Torvalds 已提交
912

S
Swen Schillig 已提交
913
	if (req->status & ZFCP_STATUS_FSFREQ_ERROR)
L
Linus Torvalds 已提交
914 915 916 917
		goto skip_fsfstatus;

	switch (header->fsf_status) {
        case FSF_GOOD:
S
Swen Schillig 已提交
918
		zfcp_dbf_san_ct_response(req);
919
		ct->status = 0;
L
Linus Torvalds 已提交
920 921
		break;
        case FSF_SERVICE_CLASS_NOT_SUPPORTED:
S
Swen Schillig 已提交
922
		zfcp_fsf_class_not_supp(req);
L
Linus Torvalds 已提交
923 924 925 926 927
		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 已提交
928
			req->status |= ZFCP_STATUS_FSFREQ_ERROR;
L
Linus Torvalds 已提交
929 930 931 932 933 934
			break;
                }
                break;
	case FSF_ACCESS_DENIED:
		break;
        case FSF_PORT_BOXED:
935
		req->status |= ZFCP_STATUS_FSFREQ_ERROR;
L
Linus Torvalds 已提交
936
		break;
S
Swen Schillig 已提交
937
	case FSF_PORT_HANDLE_NOT_VALID:
938
		zfcp_erp_adapter_reopen(adapter, 0, "fsscth1", req);
939
		/* fall through */
S
Swen Schillig 已提交
940
	case FSF_GENERIC_COMMAND_REJECTED:
L
Linus Torvalds 已提交
941 942 943 944
	case FSF_PAYLOAD_SIZE_MISMATCH:
	case FSF_REQUEST_SIZE_TOO_LARGE:
	case FSF_RESPONSE_SIZE_TOO_LARGE:
	case FSF_SBAL_MISMATCH:
S
Swen Schillig 已提交
945
		req->status |= ZFCP_STATUS_FSFREQ_ERROR;
L
Linus Torvalds 已提交
946 947 948 949
		break;
	}

skip_fsfstatus:
950 951
	if (ct->handler)
		ct->handler(ct->handler_data);
S
Swen Schillig 已提交
952
}
L
Linus Torvalds 已提交
953

954 955
static void zfcp_fsf_setup_ct_els_unchained(struct zfcp_qdio *qdio,
					    struct zfcp_qdio_req *q_req,
956 957 958
					    struct scatterlist *sg_req,
					    struct scatterlist *sg_resp)
{
959 960 961
	zfcp_qdio_fill_next(qdio, q_req, sg_virt(sg_req), sg_req->length);
	zfcp_qdio_fill_next(qdio, q_req, sg_virt(sg_resp), sg_resp->length);
	zfcp_qdio_set_sbale_last(qdio, q_req);
962 963
}

964 965 966 967
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 已提交
968
{
969 970
	struct zfcp_adapter *adapter = req->adapter;
	u32 feat = adapter->adapter_features;
S
Swen Schillig 已提交
971 972
	int bytes;

973
	if (!(feat & FSF_FEATURE_ELS_CT_CHAINED_SBALS)) {
974 975
		if (!zfcp_qdio_sg_one_sbale(sg_req) ||
		    !zfcp_qdio_sg_one_sbale(sg_resp))
976 977
			return -EOPNOTSUPP;

978 979
		zfcp_fsf_setup_ct_els_unchained(adapter->qdio, &req->qdio_req,
						sg_req, sg_resp);
980 981 982 983
		return 0;
	}

	/* use single, unchained SBAL if it can hold the request */
984 985 986
	if (zfcp_qdio_sg_one_sbale(sg_req) || zfcp_qdio_sg_one_sbale(sg_resp)) {
		zfcp_fsf_setup_ct_els_unchained(adapter->qdio, &req->qdio_req,
						sg_req, sg_resp);
987 988 989
		return 0;
	}

990
	bytes = zfcp_qdio_sbals_from_sg(adapter->qdio, &req->qdio_req,
S
Swen Schillig 已提交
991 992
					sg_req, max_sbals);
	if (bytes <= 0)
993
		return -EIO;
S
Swen Schillig 已提交
994
	req->qtcb->bottom.support.req_buf_length = bytes;
995
	zfcp_qdio_skip_to_last_sbale(&req->qdio_req);
S
Swen Schillig 已提交
996

997
	bytes = zfcp_qdio_sbals_from_sg(adapter->qdio, &req->qdio_req,
S
Swen Schillig 已提交
998
					sg_resp, max_sbals);
999
	req->qtcb->bottom.support.resp_buf_length = bytes;
S
Swen Schillig 已提交
1000
	if (bytes <= 0)
1001
		return -EIO;
1002

1003 1004 1005 1006 1007 1008
	return 0;
}

static int zfcp_fsf_setup_ct_els(struct zfcp_fsf_req *req,
				 struct scatterlist *sg_req,
				 struct scatterlist *sg_resp,
1009
				 int max_sbals, unsigned int timeout)
1010 1011 1012 1013 1014 1015 1016
{
	int ret;

	ret = zfcp_fsf_setup_ct_els_sbals(req, sg_req, sg_resp, max_sbals);
	if (ret)
		return ret;

1017
	/* common settings for ct/gs and els requests */
1018 1019
	if (timeout > 255)
		timeout = 255; /* max value accepted by hardware */
1020
	req->qtcb->bottom.support.service_class = FSF_CLASS_3;
1021 1022
	req->qtcb->bottom.support.timeout = timeout;
	zfcp_fsf_start_timer(req, (timeout + 10) * HZ);
S
Swen Schillig 已提交
1023 1024

	return 0;
L
Linus Torvalds 已提交
1025 1026 1027
}

/**
S
Swen Schillig 已提交
1028 1029 1030
 * 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
L
Linus Torvalds 已提交
1031
 */
1032
int zfcp_fsf_send_ct(struct zfcp_fc_wka_port *wka_port,
1033 1034
		     struct zfcp_fsf_ct_els *ct, mempool_t *pool,
		     unsigned int timeout)
L
Linus Torvalds 已提交
1035
{
1036
	struct zfcp_qdio *qdio = wka_port->adapter->qdio;
S
Swen Schillig 已提交
1037 1038
	struct zfcp_fsf_req *req;
	int ret = -EIO;
L
Linus Torvalds 已提交
1039

1040
	spin_lock_bh(&qdio->req_q_lock);
1041
	if (zfcp_qdio_sbal_get(qdio))
S
Swen Schillig 已提交
1042
		goto out;
L
Linus Torvalds 已提交
1043

1044 1045
	req = zfcp_fsf_req_create(qdio, FSF_QTCB_SEND_GENERIC,
				  SBAL_FLAGS0_TYPE_WRITE_READ, pool);
1046

1047
	if (IS_ERR(req)) {
S
Swen Schillig 已提交
1048 1049
		ret = PTR_ERR(req);
		goto out;
1050 1051
	}

1052
	req->status |= ZFCP_STATUS_FSFREQ_CLEANUP;
1053
	ret = zfcp_fsf_setup_ct_els(req, ct->req, ct->resp,
1054
				    ZFCP_FSF_MAX_SBALS_PER_REQ, timeout);
C
Christof Schmitt 已提交
1055
	if (ret)
L
Linus Torvalds 已提交
1056 1057
		goto failed_send;

S
Swen Schillig 已提交
1058
	req->handler = zfcp_fsf_send_ct_handler;
1059
	req->qtcb->header.port_handle = wka_port->handle;
S
Swen Schillig 已提交
1060 1061
	req->data = ct;

1062
	zfcp_dbf_san_ct_request(req, wka_port->d_id);
L
Linus Torvalds 已提交
1063

S
Swen Schillig 已提交
1064 1065 1066
	ret = zfcp_fsf_req_send(req);
	if (ret)
		goto failed_send;
L
Linus Torvalds 已提交
1067

S
Swen Schillig 已提交
1068
	goto out;
L
Linus Torvalds 已提交
1069

S
Swen Schillig 已提交
1070 1071 1072
failed_send:
	zfcp_fsf_req_free(req);
out:
1073
	spin_unlock_bh(&qdio->req_q_lock);
S
Swen Schillig 已提交
1074
	return ret;
L
Linus Torvalds 已提交
1075 1076
}

S
Swen Schillig 已提交
1077
static void zfcp_fsf_send_els_handler(struct zfcp_fsf_req *req)
L
Linus Torvalds 已提交
1078
{
1079
	struct zfcp_fsf_ct_els *send_els = req->data;
S
Swen Schillig 已提交
1080 1081
	struct zfcp_port *port = send_els->port;
	struct fsf_qtcb_header *header = &req->qtcb->header;
L
Linus Torvalds 已提交
1082

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

S
Swen Schillig 已提交
1085
	if (req->status & ZFCP_STATUS_FSFREQ_ERROR)
L
Linus Torvalds 已提交
1086 1087 1088 1089
		goto skip_fsfstatus;

	switch (header->fsf_status) {
	case FSF_GOOD:
S
Swen Schillig 已提交
1090
		zfcp_dbf_san_els_response(req);
S
Swen Schillig 已提交
1091
		send_els->status = 0;
L
Linus Torvalds 已提交
1092 1093
		break;
	case FSF_SERVICE_CLASS_NOT_SUPPORTED:
S
Swen Schillig 已提交
1094
		zfcp_fsf_class_not_supp(req);
L
Linus Torvalds 已提交
1095 1096 1097 1098 1099 1100
		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_RETRY_IF_POSSIBLE:
S
Swen Schillig 已提交
1101
			req->status |= ZFCP_STATUS_FSFREQ_ERROR;
L
Linus Torvalds 已提交
1102 1103 1104 1105 1106 1107 1108 1109 1110
			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:
1111 1112
		if (port)
			zfcp_fsf_access_denied_port(req, port);
L
Linus Torvalds 已提交
1113
		break;
S
Swen Schillig 已提交
1114 1115 1116
	case FSF_SBAL_MISMATCH:
		/* should never occure, avoided in zfcp_fsf_send_els */
		/* fall through */
L
Linus Torvalds 已提交
1117
	default:
S
Swen Schillig 已提交
1118
		req->status |= ZFCP_STATUS_FSFREQ_ERROR;
L
Linus Torvalds 已提交
1119 1120 1121
		break;
	}
skip_fsfstatus:
H
Heiko Carstens 已提交
1122
	if (send_els->handler)
L
Linus Torvalds 已提交
1123
		send_els->handler(send_els->handler_data);
S
Swen Schillig 已提交
1124
}
L
Linus Torvalds 已提交
1125

S
Swen Schillig 已提交
1126 1127 1128 1129
/**
 * zfcp_fsf_send_els - initiate an ELS command (FC-FS)
 * @els: pointer to struct zfcp_send_els with data for the command
 */
1130
int zfcp_fsf_send_els(struct zfcp_adapter *adapter, u32 d_id,
1131
		      struct zfcp_fsf_ct_els *els, unsigned int timeout)
S
Swen Schillig 已提交
1132 1133
{
	struct zfcp_fsf_req *req;
1134
	struct zfcp_qdio *qdio = adapter->qdio;
S
Swen Schillig 已提交
1135 1136
	int ret = -EIO;

1137
	spin_lock_bh(&qdio->req_q_lock);
1138
	if (zfcp_qdio_sbal_get(qdio))
S
Swen Schillig 已提交
1139
		goto out;
1140

1141 1142
	req = zfcp_fsf_req_create(qdio, FSF_QTCB_SEND_ELS,
				  SBAL_FLAGS0_TYPE_WRITE_READ, NULL);
1143

1144
	if (IS_ERR(req)) {
S
Swen Schillig 已提交
1145 1146 1147 1148
		ret = PTR_ERR(req);
		goto out;
	}

1149
	req->status |= ZFCP_STATUS_FSFREQ_CLEANUP;
1150
	ret = zfcp_fsf_setup_ct_els(req, els->req, els->resp, 2, timeout);
1151

S
Swen Schillig 已提交
1152 1153 1154
	if (ret)
		goto failed_send;

1155
	hton24(req->qtcb->bottom.support.d_id, d_id);
S
Swen Schillig 已提交
1156 1157 1158
	req->handler = zfcp_fsf_send_els_handler;
	req->data = els;

S
Swen Schillig 已提交
1159
	zfcp_dbf_san_els_request(req);
S
Swen Schillig 已提交
1160 1161 1162 1163 1164 1165 1166 1167 1168 1169

	ret = zfcp_fsf_req_send(req);
	if (ret)
		goto failed_send;

	goto out;

failed_send:
	zfcp_fsf_req_free(req);
out:
1170
	spin_unlock_bh(&qdio->req_q_lock);
S
Swen Schillig 已提交
1171
	return ret;
L
Linus Torvalds 已提交
1172 1173
}

S
Swen Schillig 已提交
1174
int zfcp_fsf_exchange_config_data(struct zfcp_erp_action *erp_action)
L
Linus Torvalds 已提交
1175
{
S
Swen Schillig 已提交
1176
	struct zfcp_fsf_req *req;
1177
	struct zfcp_qdio *qdio = erp_action->adapter->qdio;
S
Swen Schillig 已提交
1178 1179
	int retval = -EIO;

1180
	spin_lock_bh(&qdio->req_q_lock);
1181
	if (zfcp_qdio_sbal_get(qdio))
S
Swen Schillig 已提交
1182
		goto out;
1183

1184
	req = zfcp_fsf_req_create(qdio, FSF_QTCB_EXCHANGE_CONFIG_DATA,
1185
				  SBAL_FLAGS0_TYPE_READ,
1186
				  qdio->adapter->pool.erp_req);
1187

1188
	if (IS_ERR(req)) {
S
Swen Schillig 已提交
1189 1190
		retval = PTR_ERR(req);
		goto out;
L
Linus Torvalds 已提交
1191 1192
	}

1193
	req->status |= ZFCP_STATUS_FSFREQ_CLEANUP;
1194
	zfcp_qdio_set_sbale_last(qdio, &req->qdio_req);
L
Linus Torvalds 已提交
1195

S
Swen Schillig 已提交
1196
	req->qtcb->bottom.config.feature_selection =
1197 1198
			FSF_FEATURE_CFDC |
			FSF_FEATURE_LUN_SHARING |
1199
			FSF_FEATURE_NOTIFICATION_LOST |
1200
			FSF_FEATURE_UPDATE_ALERT;
S
Swen Schillig 已提交
1201 1202
	req->erp_action = erp_action;
	req->handler = zfcp_fsf_exchange_config_data_handler;
1203
	erp_action->fsf_req_id = req->req_id;
L
Linus Torvalds 已提交
1204

1205
	zfcp_fsf_start_erp_timer(req);
S
Swen Schillig 已提交
1206
	retval = zfcp_fsf_req_send(req);
L
Linus Torvalds 已提交
1207
	if (retval) {
S
Swen Schillig 已提交
1208
		zfcp_fsf_req_free(req);
1209
		erp_action->fsf_req_id = 0;
L
Linus Torvalds 已提交
1210
	}
S
Swen Schillig 已提交
1211
out:
1212
	spin_unlock_bh(&qdio->req_q_lock);
1213 1214
	return retval;
}
L
Linus Torvalds 已提交
1215

1216
int zfcp_fsf_exchange_config_data_sync(struct zfcp_qdio *qdio,
S
Swen Schillig 已提交
1217
				       struct fsf_qtcb_bottom_config *data)
1218
{
S
Swen Schillig 已提交
1219 1220 1221
	struct zfcp_fsf_req *req = NULL;
	int retval = -EIO;

1222
	spin_lock_bh(&qdio->req_q_lock);
1223
	if (zfcp_qdio_sbal_get(qdio))
1224
		goto out_unlock;
S
Swen Schillig 已提交
1225

1226 1227
	req = zfcp_fsf_req_create(qdio, FSF_QTCB_EXCHANGE_CONFIG_DATA,
				  SBAL_FLAGS0_TYPE_READ, NULL);
1228

1229
	if (IS_ERR(req)) {
S
Swen Schillig 已提交
1230
		retval = PTR_ERR(req);
1231
		goto out_unlock;
1232 1233
	}

1234
	zfcp_qdio_set_sbale_last(qdio, &req->qdio_req);
S
Swen Schillig 已提交
1235
	req->handler = zfcp_fsf_exchange_config_data_handler;
1236

S
Swen Schillig 已提交
1237
	req->qtcb->bottom.config.feature_selection =
1238 1239 1240 1241 1242 1243
			FSF_FEATURE_CFDC |
			FSF_FEATURE_LUN_SHARING |
			FSF_FEATURE_NOTIFICATION_LOST |
			FSF_FEATURE_UPDATE_ALERT;

	if (data)
S
Swen Schillig 已提交
1244
		req->data = data;
1245

S
Swen Schillig 已提交
1246 1247
	zfcp_fsf_start_timer(req, ZFCP_FSF_REQUEST_TIMEOUT);
	retval = zfcp_fsf_req_send(req);
1248
	spin_unlock_bh(&qdio->req_q_lock);
C
Christof Schmitt 已提交
1249
	if (!retval)
1250
		wait_for_completion(&req->completion);
1251

S
Swen Schillig 已提交
1252
	zfcp_fsf_req_free(req);
1253
	return retval;
1254

1255
out_unlock:
1256
	spin_unlock_bh(&qdio->req_q_lock);
L
Linus Torvalds 已提交
1257 1258 1259 1260 1261
	return retval;
}

/**
 * zfcp_fsf_exchange_port_data - request information about local port
1262
 * @erp_action: ERP action for the adapter for which port data is requested
S
Swen Schillig 已提交
1263
 * Returns: 0 on success, error otherwise
L
Linus Torvalds 已提交
1264
 */
S
Swen Schillig 已提交
1265
int zfcp_fsf_exchange_port_data(struct zfcp_erp_action *erp_action)
L
Linus Torvalds 已提交
1266
{
1267
	struct zfcp_qdio *qdio = erp_action->adapter->qdio;
S
Swen Schillig 已提交
1268 1269
	struct zfcp_fsf_req *req;
	int retval = -EIO;
L
Linus Torvalds 已提交
1270

1271
	if (!(qdio->adapter->adapter_features & FSF_FEATURE_HBAAPI_MANAGEMENT))
1272
		return -EOPNOTSUPP;
L
Linus Torvalds 已提交
1273

1274
	spin_lock_bh(&qdio->req_q_lock);
1275
	if (zfcp_qdio_sbal_get(qdio))
S
Swen Schillig 已提交
1276
		goto out;
1277

1278
	req = zfcp_fsf_req_create(qdio, FSF_QTCB_EXCHANGE_PORT_DATA,
1279
				  SBAL_FLAGS0_TYPE_READ,
1280
				  qdio->adapter->pool.erp_req);
1281

1282
	if (IS_ERR(req)) {
S
Swen Schillig 已提交
1283 1284
		retval = PTR_ERR(req);
		goto out;
1285 1286
	}

1287
	req->status |= ZFCP_STATUS_FSFREQ_CLEANUP;
1288
	zfcp_qdio_set_sbale_last(qdio, &req->qdio_req);
L
Linus Torvalds 已提交
1289

S
Swen Schillig 已提交
1290 1291
	req->handler = zfcp_fsf_exchange_port_data_handler;
	req->erp_action = erp_action;
1292
	erp_action->fsf_req_id = req->req_id;
1293

1294
	zfcp_fsf_start_erp_timer(req);
S
Swen Schillig 已提交
1295
	retval = zfcp_fsf_req_send(req);
L
Linus Torvalds 已提交
1296
	if (retval) {
S
Swen Schillig 已提交
1297
		zfcp_fsf_req_free(req);
1298
		erp_action->fsf_req_id = 0;
1299
	}
S
Swen Schillig 已提交
1300
out:
1301
	spin_unlock_bh(&qdio->req_q_lock);
1302 1303 1304 1305 1306
	return retval;
}

/**
 * zfcp_fsf_exchange_port_data_sync - request information about local port
1307
 * @qdio: pointer to struct zfcp_qdio
S
Swen Schillig 已提交
1308 1309
 * @data: pointer to struct fsf_qtcb_bottom_port
 * Returns: 0 on success, error otherwise
1310
 */
1311
int zfcp_fsf_exchange_port_data_sync(struct zfcp_qdio *qdio,
S
Swen Schillig 已提交
1312
				     struct fsf_qtcb_bottom_port *data)
1313
{
S
Swen Schillig 已提交
1314 1315
	struct zfcp_fsf_req *req = NULL;
	int retval = -EIO;
1316

1317
	if (!(qdio->adapter->adapter_features & FSF_FEATURE_HBAAPI_MANAGEMENT))
1318 1319
		return -EOPNOTSUPP;

1320
	spin_lock_bh(&qdio->req_q_lock);
1321
	if (zfcp_qdio_sbal_get(qdio))
1322
		goto out_unlock;
S
Swen Schillig 已提交
1323

1324 1325
	req = zfcp_fsf_req_create(qdio, FSF_QTCB_EXCHANGE_PORT_DATA,
				  SBAL_FLAGS0_TYPE_READ, NULL);
1326

1327
	if (IS_ERR(req)) {
S
Swen Schillig 已提交
1328
		retval = PTR_ERR(req);
1329
		goto out_unlock;
L
Linus Torvalds 已提交
1330 1331
	}

1332
	if (data)
S
Swen Schillig 已提交
1333
		req->data = data;
1334

1335
	zfcp_qdio_set_sbale_last(qdio, &req->qdio_req);
1336

S
Swen Schillig 已提交
1337 1338 1339
	req->handler = zfcp_fsf_exchange_port_data_handler;
	zfcp_fsf_start_timer(req, ZFCP_FSF_REQUEST_TIMEOUT);
	retval = zfcp_fsf_req_send(req);
1340
	spin_unlock_bh(&qdio->req_q_lock);
1341

C
Christof Schmitt 已提交
1342
	if (!retval)
1343 1344
		wait_for_completion(&req->completion);

S
Swen Schillig 已提交
1345
	zfcp_fsf_req_free(req);
L
Linus Torvalds 已提交
1346 1347

	return retval;
1348 1349

out_unlock:
1350
	spin_unlock_bh(&qdio->req_q_lock);
1351
	return retval;
L
Linus Torvalds 已提交
1352 1353
}

S
Swen Schillig 已提交
1354
static void zfcp_fsf_open_port_handler(struct zfcp_fsf_req *req)
L
Linus Torvalds 已提交
1355
{
S
Swen Schillig 已提交
1356 1357
	struct zfcp_port *port = req->data;
	struct fsf_qtcb_header *header = &req->qtcb->header;
1358
	struct fc_els_flogi *plogi;
L
Linus Torvalds 已提交
1359

S
Swen Schillig 已提交
1360
	if (req->status & ZFCP_STATUS_FSFREQ_ERROR)
1361
		goto out;
L
Linus Torvalds 已提交
1362 1363 1364 1365 1366

	switch (header->fsf_status) {
	case FSF_PORT_ALREADY_OPEN:
		break;
	case FSF_ACCESS_DENIED:
S
Swen Schillig 已提交
1367
		zfcp_fsf_access_denied_port(req, port);
L
Linus Torvalds 已提交
1368 1369
		break;
	case FSF_MAXIMUM_NUMBER_OF_PORTS_EXCEEDED:
S
Swen Schillig 已提交
1370
		dev_warn(&req->adapter->ccw_device->dev,
1371
			 "Not enough FCP adapter resources to open "
1372 1373
			 "remote port 0x%016Lx\n",
			 (unsigned long long)port->wwpn);
1374
		zfcp_erp_port_failed(port, "fsoph_1", req);
S
Swen Schillig 已提交
1375
		req->status |= ZFCP_STATUS_FSFREQ_ERROR;
L
Linus Torvalds 已提交
1376 1377 1378 1379 1380 1381
		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 已提交
1382
			req->status |= ZFCP_STATUS_FSFREQ_ERROR;
L
Linus Torvalds 已提交
1383 1384 1385 1386 1387 1388 1389
			break;
		}
		break;
	case FSF_GOOD:
		port->handle = header->port_handle;
		atomic_set_mask(ZFCP_STATUS_COMMON_OPEN |
				ZFCP_STATUS_PORT_PHYS_OPEN, &port->status);
1390 1391 1392
		atomic_clear_mask(ZFCP_STATUS_COMMON_ACCESS_DENIED |
		                  ZFCP_STATUS_COMMON_ACCESS_BOXED,
		                  &port->status);
L
Linus Torvalds 已提交
1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407
		/* 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.
		 */
1408
		plogi = (struct fc_els_flogi *) req->qtcb->bottom.support.els;
1409
		if (req->qtcb->bottom.support.els1_length >=
1410
		    FSF_PLOGI_MIN_LEN)
S
Swen Schillig 已提交
1411
				zfcp_fc_plogi_evaluate(port, plogi);
L
Linus Torvalds 已提交
1412 1413
		break;
	case FSF_UNKNOWN_OP_SUBTYPE:
S
Swen Schillig 已提交
1414
		req->status |= ZFCP_STATUS_FSFREQ_ERROR;
L
Linus Torvalds 已提交
1415 1416
		break;
	}
1417 1418

out:
1419
	put_device(&port->dev);
L
Linus Torvalds 已提交
1420 1421
}

S
Swen Schillig 已提交
1422 1423 1424 1425
/**
 * 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 已提交
1426
 */
S
Swen Schillig 已提交
1427
int zfcp_fsf_open_port(struct zfcp_erp_action *erp_action)
L
Linus Torvalds 已提交
1428
{
1429
	struct zfcp_qdio *qdio = erp_action->adapter->qdio;
1430
	struct zfcp_port *port = erp_action->port;
1431
	struct zfcp_fsf_req *req;
S
Swen Schillig 已提交
1432 1433
	int retval = -EIO;

1434
	spin_lock_bh(&qdio->req_q_lock);
1435
	if (zfcp_qdio_sbal_get(qdio))
S
Swen Schillig 已提交
1436 1437
		goto out;

1438
	req = zfcp_fsf_req_create(qdio, FSF_QTCB_OPEN_PORT_WITH_DID,
1439
				  SBAL_FLAGS0_TYPE_READ,
1440
				  qdio->adapter->pool.erp_req);
1441

1442
	if (IS_ERR(req)) {
S
Swen Schillig 已提交
1443
		retval = PTR_ERR(req);
L
Linus Torvalds 已提交
1444
		goto out;
S
Swen Schillig 已提交
1445
	}
L
Linus Torvalds 已提交
1446

1447
	req->status |= ZFCP_STATUS_FSFREQ_CLEANUP;
1448
	zfcp_qdio_set_sbale_last(qdio, &req->qdio_req);
L
Linus Torvalds 已提交
1449

S
Swen Schillig 已提交
1450
	req->handler = zfcp_fsf_open_port_handler;
1451
	hton24(req->qtcb->bottom.support.d_id, port->d_id);
1452
	req->data = port;
S
Swen Schillig 已提交
1453
	req->erp_action = erp_action;
1454
	erp_action->fsf_req_id = req->req_id;
1455
	get_device(&port->dev);
S
Swen Schillig 已提交
1456

1457
	zfcp_fsf_start_erp_timer(req);
S
Swen Schillig 已提交
1458
	retval = zfcp_fsf_req_send(req);
L
Linus Torvalds 已提交
1459
	if (retval) {
S
Swen Schillig 已提交
1460
		zfcp_fsf_req_free(req);
1461
		erp_action->fsf_req_id = 0;
1462
		put_device(&port->dev);
L
Linus Torvalds 已提交
1463
	}
S
Swen Schillig 已提交
1464
out:
1465
	spin_unlock_bh(&qdio->req_q_lock);
L
Linus Torvalds 已提交
1466 1467 1468
	return retval;
}

S
Swen Schillig 已提交
1469
static void zfcp_fsf_close_port_handler(struct zfcp_fsf_req *req)
L
Linus Torvalds 已提交
1470
{
S
Swen Schillig 已提交
1471
	struct zfcp_port *port = req->data;
L
Linus Torvalds 已提交
1472

S
Swen Schillig 已提交
1473
	if (req->status & ZFCP_STATUS_FSFREQ_ERROR)
1474
		return;
L
Linus Torvalds 已提交
1475

S
Swen Schillig 已提交
1476
	switch (req->qtcb->header.fsf_status) {
L
Linus Torvalds 已提交
1477
	case FSF_PORT_HANDLE_NOT_VALID:
1478
		zfcp_erp_adapter_reopen(port->adapter, 0, "fscph_1", req);
S
Swen Schillig 已提交
1479
		req->status |= ZFCP_STATUS_FSFREQ_ERROR;
L
Linus Torvalds 已提交
1480 1481 1482 1483
		break;
	case FSF_ADAPTER_STATUS_AVAILABLE:
		break;
	case FSF_GOOD:
1484
		zfcp_erp_modify_port_status(port, "fscph_2", req,
L
Linus Torvalds 已提交
1485 1486 1487 1488 1489 1490
					    ZFCP_STATUS_COMMON_OPEN,
					    ZFCP_CLEAR);
		break;
	}
}

S
Swen Schillig 已提交
1491 1492 1493 1494
/**
 * 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 已提交
1495
 */
S
Swen Schillig 已提交
1496
int zfcp_fsf_close_port(struct zfcp_erp_action *erp_action)
L
Linus Torvalds 已提交
1497
{
1498
	struct zfcp_qdio *qdio = erp_action->adapter->qdio;
S
Swen Schillig 已提交
1499 1500 1501
	struct zfcp_fsf_req *req;
	int retval = -EIO;

1502
	spin_lock_bh(&qdio->req_q_lock);
1503
	if (zfcp_qdio_sbal_get(qdio))
S
Swen Schillig 已提交
1504 1505
		goto out;

1506
	req = zfcp_fsf_req_create(qdio, FSF_QTCB_CLOSE_PORT,
1507
				  SBAL_FLAGS0_TYPE_READ,
1508
				  qdio->adapter->pool.erp_req);
1509

1510
	if (IS_ERR(req)) {
S
Swen Schillig 已提交
1511
		retval = PTR_ERR(req);
L
Linus Torvalds 已提交
1512
		goto out;
S
Swen Schillig 已提交
1513
	}
L
Linus Torvalds 已提交
1514

1515
	req->status |= ZFCP_STATUS_FSFREQ_CLEANUP;
1516
	zfcp_qdio_set_sbale_last(qdio, &req->qdio_req);
L
Linus Torvalds 已提交
1517

S
Swen Schillig 已提交
1518 1519 1520 1521
	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;
1522
	erp_action->fsf_req_id = req->req_id;
S
Swen Schillig 已提交
1523

1524
	zfcp_fsf_start_erp_timer(req);
S
Swen Schillig 已提交
1525
	retval = zfcp_fsf_req_send(req);
L
Linus Torvalds 已提交
1526
	if (retval) {
S
Swen Schillig 已提交
1527
		zfcp_fsf_req_free(req);
1528
		erp_action->fsf_req_id = 0;
L
Linus Torvalds 已提交
1529
	}
S
Swen Schillig 已提交
1530
out:
1531
	spin_unlock_bh(&qdio->req_q_lock);
L
Linus Torvalds 已提交
1532 1533 1534
	return retval;
}

1535 1536
static void zfcp_fsf_open_wka_port_handler(struct zfcp_fsf_req *req)
{
1537
	struct zfcp_fc_wka_port *wka_port = req->data;
1538 1539 1540
	struct fsf_qtcb_header *header = &req->qtcb->header;

	if (req->status & ZFCP_STATUS_FSFREQ_ERROR) {
1541
		wka_port->status = ZFCP_FC_WKA_PORT_OFFLINE;
1542 1543 1544 1545 1546 1547 1548
		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);
1549
		/* fall through */
1550 1551
	case FSF_ADAPTER_STATUS_AVAILABLE:
		req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1552
		/* fall through */
1553
	case FSF_ACCESS_DENIED:
1554
		wka_port->status = ZFCP_FC_WKA_PORT_OFFLINE;
1555 1556 1557
		break;
	case FSF_GOOD:
		wka_port->handle = header->port_handle;
1558 1559
		/* fall through */
	case FSF_PORT_ALREADY_OPEN:
1560
		wka_port->status = ZFCP_FC_WKA_PORT_ONLINE;
1561 1562 1563 1564 1565 1566 1567
	}
out:
	wake_up(&wka_port->completion_wq);
}

/**
 * zfcp_fsf_open_wka_port - create and send open wka-port request
1568
 * @wka_port: pointer to struct zfcp_fc_wka_port
1569 1570
 * Returns: 0 on success, error otherwise
 */
1571
int zfcp_fsf_open_wka_port(struct zfcp_fc_wka_port *wka_port)
1572
{
1573
	struct zfcp_qdio *qdio = wka_port->adapter->qdio;
1574 1575 1576
	struct zfcp_fsf_req *req;
	int retval = -EIO;

1577
	spin_lock_bh(&qdio->req_q_lock);
1578
	if (zfcp_qdio_sbal_get(qdio))
1579 1580
		goto out;

1581
	req = zfcp_fsf_req_create(qdio, FSF_QTCB_OPEN_PORT_WITH_DID,
1582
				  SBAL_FLAGS0_TYPE_READ,
1583
				  qdio->adapter->pool.erp_req);
1584

1585 1586 1587 1588 1589
	if (unlikely(IS_ERR(req))) {
		retval = PTR_ERR(req);
		goto out;
	}

1590
	req->status |= ZFCP_STATUS_FSFREQ_CLEANUP;
1591
	zfcp_qdio_set_sbale_last(qdio, &req->qdio_req);
1592 1593

	req->handler = zfcp_fsf_open_wka_port_handler;
1594
	hton24(req->qtcb->bottom.support.d_id, wka_port->d_id);
1595 1596 1597 1598 1599 1600 1601
	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:
1602
	spin_unlock_bh(&qdio->req_q_lock);
1603 1604 1605 1606 1607
	return retval;
}

static void zfcp_fsf_close_wka_port_handler(struct zfcp_fsf_req *req)
{
1608
	struct zfcp_fc_wka_port *wka_port = req->data;
1609 1610 1611

	if (req->qtcb->header.fsf_status == FSF_PORT_HANDLE_NOT_VALID) {
		req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1612
		zfcp_erp_adapter_reopen(wka_port->adapter, 0, "fscwph1", req);
1613 1614
	}

1615
	wka_port->status = ZFCP_FC_WKA_PORT_OFFLINE;
1616 1617 1618 1619 1620
	wake_up(&wka_port->completion_wq);
}

/**
 * zfcp_fsf_close_wka_port - create and send close wka port request
1621
 * @wka_port: WKA port to open
1622 1623
 * Returns: 0 on success, error otherwise
 */
1624
int zfcp_fsf_close_wka_port(struct zfcp_fc_wka_port *wka_port)
1625
{
1626
	struct zfcp_qdio *qdio = wka_port->adapter->qdio;
1627 1628 1629
	struct zfcp_fsf_req *req;
	int retval = -EIO;

1630
	spin_lock_bh(&qdio->req_q_lock);
1631
	if (zfcp_qdio_sbal_get(qdio))
1632 1633
		goto out;

1634
	req = zfcp_fsf_req_create(qdio, FSF_QTCB_CLOSE_PORT,
1635
				  SBAL_FLAGS0_TYPE_READ,
1636
				  qdio->adapter->pool.erp_req);
1637

1638 1639 1640 1641 1642
	if (unlikely(IS_ERR(req))) {
		retval = PTR_ERR(req);
		goto out;
	}

1643
	req->status |= ZFCP_STATUS_FSFREQ_CLEANUP;
1644
	zfcp_qdio_set_sbale_last(qdio, &req->qdio_req);
1645 1646 1647 1648 1649 1650 1651 1652 1653 1654

	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:
1655
	spin_unlock_bh(&qdio->req_q_lock);
1656 1657 1658
	return retval;
}

S
Swen Schillig 已提交
1659
static void zfcp_fsf_close_physical_port_handler(struct zfcp_fsf_req *req)
L
Linus Torvalds 已提交
1660
{
S
Swen Schillig 已提交
1661 1662
	struct zfcp_port *port = req->data;
	struct fsf_qtcb_header *header = &req->qtcb->header;
L
Linus Torvalds 已提交
1663 1664
	struct zfcp_unit *unit;

S
Swen Schillig 已提交
1665
	if (req->status & ZFCP_STATUS_FSFREQ_ERROR)
1666
		return;
L
Linus Torvalds 已提交
1667 1668 1669

	switch (header->fsf_status) {
	case FSF_PORT_HANDLE_NOT_VALID:
1670
		zfcp_erp_adapter_reopen(port->adapter, 0, "fscpph1", req);
S
Swen Schillig 已提交
1671
		req->status |= ZFCP_STATUS_FSFREQ_ERROR;
L
Linus Torvalds 已提交
1672 1673
		break;
	case FSF_ACCESS_DENIED:
S
Swen Schillig 已提交
1674
		zfcp_fsf_access_denied_port(req, port);
L
Linus Torvalds 已提交
1675 1676
		break;
	case FSF_PORT_BOXED:
1677 1678 1679
		/* 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);
1680 1681
		read_lock(&port->unit_list_lock);
		list_for_each_entry(unit, &port->unit_list, list)
1682 1683
			atomic_clear_mask(ZFCP_STATUS_COMMON_OPEN,
					  &unit->status);
1684
		read_unlock(&port->unit_list_lock);
1685
		zfcp_erp_port_boxed(port, "fscpph2", req);
1686
		req->status |= ZFCP_STATUS_FSFREQ_ERROR;
L
Linus Torvalds 已提交
1687 1688 1689 1690
		break;
	case FSF_ADAPTER_STATUS_AVAILABLE:
		switch (header->fsf_status_qual.word[0]) {
		case FSF_SQ_INVOKE_LINK_TEST_PROCEDURE:
S
Swen Schillig 已提交
1691
			/* fall through */
L
Linus Torvalds 已提交
1692
		case FSF_SQ_ULP_DEPENDENT_ERP_REQUIRED:
S
Swen Schillig 已提交
1693
			req->status |= ZFCP_STATUS_FSFREQ_ERROR;
L
Linus Torvalds 已提交
1694 1695 1696 1697 1698 1699 1700 1701
			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);
1702 1703
		read_lock(&port->unit_list_lock);
		list_for_each_entry(unit, &port->unit_list, list)
S
Swen Schillig 已提交
1704 1705
			atomic_clear_mask(ZFCP_STATUS_COMMON_OPEN,
					  &unit->status);
1706
		read_unlock(&port->unit_list_lock);
L
Linus Torvalds 已提交
1707 1708 1709 1710
		break;
	}
}

S
Swen Schillig 已提交
1711 1712 1713 1714
/**
 * zfcp_fsf_close_physical_port - close physical port
 * @erp_action: pointer to struct zfcp_erp_action
 * Returns: 0 on success
L
Linus Torvalds 已提交
1715
 */
S
Swen Schillig 已提交
1716
int zfcp_fsf_close_physical_port(struct zfcp_erp_action *erp_action)
L
Linus Torvalds 已提交
1717
{
1718
	struct zfcp_qdio *qdio = erp_action->adapter->qdio;
S
Swen Schillig 已提交
1719 1720 1721
	struct zfcp_fsf_req *req;
	int retval = -EIO;

1722
	spin_lock_bh(&qdio->req_q_lock);
1723
	if (zfcp_qdio_sbal_get(qdio))
L
Linus Torvalds 已提交
1724 1725
		goto out;

1726
	req = zfcp_fsf_req_create(qdio, FSF_QTCB_CLOSE_PHYSICAL_PORT,
1727
				  SBAL_FLAGS0_TYPE_READ,
1728
				  qdio->adapter->pool.erp_req);
1729

1730
	if (IS_ERR(req)) {
S
Swen Schillig 已提交
1731 1732 1733
		retval = PTR_ERR(req);
		goto out;
	}
L
Linus Torvalds 已提交
1734

1735
	req->status |= ZFCP_STATUS_FSFREQ_CLEANUP;
1736
	zfcp_qdio_set_sbale_last(qdio, &req->qdio_req);
L
Linus Torvalds 已提交
1737

S
Swen Schillig 已提交
1738 1739 1740 1741
	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;
1742
	erp_action->fsf_req_id = req->req_id;
S
Swen Schillig 已提交
1743

1744
	zfcp_fsf_start_erp_timer(req);
S
Swen Schillig 已提交
1745
	retval = zfcp_fsf_req_send(req);
L
Linus Torvalds 已提交
1746
	if (retval) {
S
Swen Schillig 已提交
1747
		zfcp_fsf_req_free(req);
1748
		erp_action->fsf_req_id = 0;
L
Linus Torvalds 已提交
1749
	}
S
Swen Schillig 已提交
1750
out:
1751
	spin_unlock_bh(&qdio->req_q_lock);
L
Linus Torvalds 已提交
1752 1753 1754
	return retval;
}

S
Swen Schillig 已提交
1755
static void zfcp_fsf_open_unit_handler(struct zfcp_fsf_req *req)
L
Linus Torvalds 已提交
1756
{
S
Swen Schillig 已提交
1757 1758 1759 1760 1761 1762
	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;
1763
	int exclusive, readwrite;
L
Linus Torvalds 已提交
1764

S
Swen Schillig 已提交
1765
	if (req->status & ZFCP_STATUS_FSFREQ_ERROR)
1766
		return;
L
Linus Torvalds 已提交
1767 1768

	atomic_clear_mask(ZFCP_STATUS_COMMON_ACCESS_DENIED |
1769
			  ZFCP_STATUS_COMMON_ACCESS_BOXED |
L
Linus Torvalds 已提交
1770 1771 1772 1773 1774 1775 1776
			  ZFCP_STATUS_UNIT_SHARED |
			  ZFCP_STATUS_UNIT_READONLY,
			  &unit->status);

	switch (header->fsf_status) {

	case FSF_PORT_HANDLE_NOT_VALID:
1777
		zfcp_erp_adapter_reopen(unit->port->adapter, 0, "fsouh_1", req);
S
Swen Schillig 已提交
1778
		/* fall through */
L
Linus Torvalds 已提交
1779 1780 1781
	case FSF_LUN_ALREADY_OPEN:
		break;
	case FSF_ACCESS_DENIED:
S
Swen Schillig 已提交
1782
		zfcp_fsf_access_denied_unit(req, unit);
L
Linus Torvalds 已提交
1783
		atomic_clear_mask(ZFCP_STATUS_UNIT_SHARED, &unit->status);
C
Christof Schmitt 已提交
1784
		atomic_clear_mask(ZFCP_STATUS_UNIT_READONLY, &unit->status);
L
Linus Torvalds 已提交
1785 1786
		break;
	case FSF_PORT_BOXED:
1787
		zfcp_erp_port_boxed(unit->port, "fsouh_2", req);
1788
		req->status |= ZFCP_STATUS_FSFREQ_ERROR;
L
Linus Torvalds 已提交
1789 1790
		break;
	case FSF_LUN_SHARING_VIOLATION:
S
Swen Schillig 已提交
1791
		if (header->fsf_status_qual.word[0])
C
Christof Schmitt 已提交
1792
			dev_warn(&adapter->ccw_device->dev,
1793 1794
				 "LUN 0x%Lx on port 0x%Lx is already in "
				 "use by CSS%d, MIF Image ID %x\n",
1795 1796
				 (unsigned long long)unit->fcp_lun,
				 (unsigned long long)unit->port->wwpn,
1797 1798
				 queue_designator->cssid,
				 queue_designator->hla);
S
Swen Schillig 已提交
1799
		else
C
Christof Schmitt 已提交
1800 1801
			zfcp_act_eval_err(adapter,
					  header->fsf_status_qual.word[2]);
1802
		zfcp_erp_unit_access_denied(unit, "fsouh_3", req);
L
Linus Torvalds 已提交
1803 1804
		atomic_clear_mask(ZFCP_STATUS_UNIT_SHARED, &unit->status);
		atomic_clear_mask(ZFCP_STATUS_UNIT_READONLY, &unit->status);
S
Swen Schillig 已提交
1805
		req->status |= ZFCP_STATUS_FSFREQ_ERROR;
L
Linus Torvalds 已提交
1806 1807
		break;
	case FSF_MAXIMUM_NUMBER_OF_LUNS_EXCEEDED:
S
Swen Schillig 已提交
1808
		dev_warn(&adapter->ccw_device->dev,
1809 1810
			 "No handle is available for LUN "
			 "0x%016Lx on port 0x%016Lx\n",
1811 1812
			 (unsigned long long)unit->fcp_lun,
			 (unsigned long long)unit->port->wwpn);
1813
		zfcp_erp_unit_failed(unit, "fsouh_4", req);
S
Swen Schillig 已提交
1814 1815 1816
		/* fall through */
	case FSF_INVALID_COMMAND_OPTION:
		req->status |= ZFCP_STATUS_FSFREQ_ERROR;
L
Linus Torvalds 已提交
1817 1818 1819 1820
		break;
	case FSF_ADAPTER_STATUS_AVAILABLE:
		switch (header->fsf_status_qual.word[0]) {
		case FSF_SQ_INVOKE_LINK_TEST_PROCEDURE:
1821
			zfcp_fc_test_link(unit->port);
S
Swen Schillig 已提交
1822
			/* fall through */
L
Linus Torvalds 已提交
1823
		case FSF_SQ_ULP_DEPENDENT_ERP_REQUIRED:
S
Swen Schillig 已提交
1824
			req->status |= ZFCP_STATUS_FSFREQ_ERROR;
L
Linus Torvalds 已提交
1825 1826 1827 1828 1829 1830 1831
			break;
		}
		break;

	case FSF_GOOD:
		unit->handle = header->lun_handle;
		atomic_set_mask(ZFCP_STATUS_COMMON_OPEN, &unit->status);
1832 1833 1834

		if (!(adapter->connection_features & FSF_FEATURE_NPIV_MODE) &&
		    (adapter->adapter_features & FSF_FEATURE_LUN_SHARING) &&
1835
		    !zfcp_ccw_priv_sch(adapter)) {
1836 1837 1838 1839 1840
			exclusive = (bottom->lun_access_info &
					FSF_UNIT_ACCESS_EXCLUSIVE);
			readwrite = (bottom->lun_access_info &
					FSF_UNIT_ACCESS_OUTBOUND_TRANSFER);

L
Linus Torvalds 已提交
1841 1842 1843 1844 1845 1846 1847
			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 已提交
1848
				dev_info(&adapter->ccw_device->dev,
1849 1850
					 "SCSI device at LUN 0x%016Lx on port "
					 "0x%016Lx opened read-only\n",
1851 1852
					 (unsigned long long)unit->fcp_lun,
					 (unsigned long long)unit->port->wwpn);
L
Linus Torvalds 已提交
1853 1854 1855
        		}

        		if (exclusive && !readwrite) {
S
Swen Schillig 已提交
1856
				dev_err(&adapter->ccw_device->dev,
1857 1858 1859
					"Exclusive read-only access not "
					"supported (unit 0x%016Lx, "
					"port 0x%016Lx)\n",
1860 1861
					(unsigned long long)unit->fcp_lun,
					(unsigned long long)unit->port->wwpn);
1862
				zfcp_erp_unit_failed(unit, "fsouh_5", req);
S
Swen Schillig 已提交
1863
				req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1864
				zfcp_erp_unit_shutdown(unit, 0, "fsouh_6", req);
L
Linus Torvalds 已提交
1865
        		} else if (!exclusive && readwrite) {
S
Swen Schillig 已提交
1866
				dev_err(&adapter->ccw_device->dev,
1867 1868
					"Shared read-write access not "
					"supported (unit 0x%016Lx, port "
1869
					"0x%016Lx)\n",
1870 1871
					(unsigned long long)unit->fcp_lun,
					(unsigned long long)unit->port->wwpn);
1872
				zfcp_erp_unit_failed(unit, "fsouh_7", req);
S
Swen Schillig 已提交
1873
				req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1874
				zfcp_erp_unit_shutdown(unit, 0, "fsouh_8", req);
L
Linus Torvalds 已提交
1875 1876 1877 1878 1879 1880
        		}
		}
		break;
	}
}

S
Swen Schillig 已提交
1881 1882 1883 1884
/**
 * zfcp_fsf_open_unit - open unit
 * @erp_action: pointer to struct zfcp_erp_action
 * Returns: 0 on success, error otherwise
L
Linus Torvalds 已提交
1885
 */
S
Swen Schillig 已提交
1886
int zfcp_fsf_open_unit(struct zfcp_erp_action *erp_action)
L
Linus Torvalds 已提交
1887
{
S
Swen Schillig 已提交
1888
	struct zfcp_adapter *adapter = erp_action->adapter;
1889
	struct zfcp_qdio *qdio = adapter->qdio;
S
Swen Schillig 已提交
1890 1891 1892
	struct zfcp_fsf_req *req;
	int retval = -EIO;

1893
	spin_lock_bh(&qdio->req_q_lock);
1894
	if (zfcp_qdio_sbal_get(qdio))
L
Linus Torvalds 已提交
1895 1896
		goto out;

1897
	req = zfcp_fsf_req_create(qdio, FSF_QTCB_OPEN_LUN,
1898
				  SBAL_FLAGS0_TYPE_READ,
1899
				  adapter->pool.erp_req);
1900

1901
	if (IS_ERR(req)) {
S
Swen Schillig 已提交
1902 1903 1904 1905
		retval = PTR_ERR(req);
		goto out;
	}

1906
	req->status |= ZFCP_STATUS_FSFREQ_CLEANUP;
1907
	zfcp_qdio_set_sbale_last(qdio, &req->qdio_req);
L
Linus Torvalds 已提交
1908

S
Swen Schillig 已提交
1909 1910 1911 1912 1913
	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;
1914
	erp_action->fsf_req_id = req->req_id;
S
Swen Schillig 已提交
1915 1916 1917 1918

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

1919
	zfcp_fsf_start_erp_timer(req);
S
Swen Schillig 已提交
1920
	retval = zfcp_fsf_req_send(req);
L
Linus Torvalds 已提交
1921
	if (retval) {
S
Swen Schillig 已提交
1922
		zfcp_fsf_req_free(req);
1923
		erp_action->fsf_req_id = 0;
L
Linus Torvalds 已提交
1924
	}
S
Swen Schillig 已提交
1925
out:
1926
	spin_unlock_bh(&qdio->req_q_lock);
L
Linus Torvalds 已提交
1927 1928 1929
	return retval;
}

S
Swen Schillig 已提交
1930
static void zfcp_fsf_close_unit_handler(struct zfcp_fsf_req *req)
L
Linus Torvalds 已提交
1931
{
S
Swen Schillig 已提交
1932
	struct zfcp_unit *unit = req->data;
L
Linus Torvalds 已提交
1933

S
Swen Schillig 已提交
1934
	if (req->status & ZFCP_STATUS_FSFREQ_ERROR)
1935
		return;
L
Linus Torvalds 已提交
1936

S
Swen Schillig 已提交
1937
	switch (req->qtcb->header.fsf_status) {
L
Linus Torvalds 已提交
1938
	case FSF_PORT_HANDLE_NOT_VALID:
1939
		zfcp_erp_adapter_reopen(unit->port->adapter, 0, "fscuh_1", req);
S
Swen Schillig 已提交
1940
		req->status |= ZFCP_STATUS_FSFREQ_ERROR;
L
Linus Torvalds 已提交
1941 1942
		break;
	case FSF_LUN_HANDLE_NOT_VALID:
1943
		zfcp_erp_port_reopen(unit->port, 0, "fscuh_2", req);
S
Swen Schillig 已提交
1944
		req->status |= ZFCP_STATUS_FSFREQ_ERROR;
L
Linus Torvalds 已提交
1945 1946
		break;
	case FSF_PORT_BOXED:
1947
		zfcp_erp_port_boxed(unit->port, "fscuh_3", req);
1948
		req->status |= ZFCP_STATUS_FSFREQ_ERROR;
L
Linus Torvalds 已提交
1949 1950
		break;
	case FSF_ADAPTER_STATUS_AVAILABLE:
S
Swen Schillig 已提交
1951
		switch (req->qtcb->header.fsf_status_qual.word[0]) {
L
Linus Torvalds 已提交
1952
		case FSF_SQ_INVOKE_LINK_TEST_PROCEDURE:
1953
			zfcp_fc_test_link(unit->port);
S
Swen Schillig 已提交
1954
			/* fall through */
L
Linus Torvalds 已提交
1955
		case FSF_SQ_ULP_DEPENDENT_ERP_REQUIRED:
S
Swen Schillig 已提交
1956
			req->status |= ZFCP_STATUS_FSFREQ_ERROR;
L
Linus Torvalds 已提交
1957 1958 1959 1960 1961 1962 1963 1964 1965 1966
			break;
		}
		break;
	case FSF_GOOD:
		atomic_clear_mask(ZFCP_STATUS_COMMON_OPEN, &unit->status);
		break;
	}
}

/**
S
Swen Schillig 已提交
1967 1968 1969
 * zfcp_fsf_close_unit - close zfcp unit
 * @erp_action: pointer to struct zfcp_unit
 * Returns: 0 on success, error otherwise
L
Linus Torvalds 已提交
1970
 */
S
Swen Schillig 已提交
1971
int zfcp_fsf_close_unit(struct zfcp_erp_action *erp_action)
L
Linus Torvalds 已提交
1972
{
1973
	struct zfcp_qdio *qdio = erp_action->adapter->qdio;
S
Swen Schillig 已提交
1974 1975
	struct zfcp_fsf_req *req;
	int retval = -EIO;
L
Linus Torvalds 已提交
1976

1977
	spin_lock_bh(&qdio->req_q_lock);
1978
	if (zfcp_qdio_sbal_get(qdio))
L
Linus Torvalds 已提交
1979
		goto out;
1980

1981
	req = zfcp_fsf_req_create(qdio, FSF_QTCB_CLOSE_LUN,
1982
				  SBAL_FLAGS0_TYPE_READ,
1983
				  qdio->adapter->pool.erp_req);
1984

1985
	if (IS_ERR(req)) {
S
Swen Schillig 已提交
1986 1987 1988
		retval = PTR_ERR(req);
		goto out;
	}
L
Linus Torvalds 已提交
1989

1990
	req->status |= ZFCP_STATUS_FSFREQ_CLEANUP;
1991
	zfcp_qdio_set_sbale_last(qdio, &req->qdio_req);
L
Linus Torvalds 已提交
1992

S
Swen Schillig 已提交
1993 1994 1995 1996 1997
	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;
1998
	erp_action->fsf_req_id = req->req_id;
1999

2000
	zfcp_fsf_start_erp_timer(req);
S
Swen Schillig 已提交
2001 2002 2003
	retval = zfcp_fsf_req_send(req);
	if (retval) {
		zfcp_fsf_req_free(req);
2004
		erp_action->fsf_req_id = 0;
S
Swen Schillig 已提交
2005 2006
	}
out:
2007
	spin_unlock_bh(&qdio->req_q_lock);
S
Swen Schillig 已提交
2008
	return retval;
L
Linus Torvalds 已提交
2009 2010
}

2011 2012 2013
static void zfcp_fsf_update_lat(struct fsf_latency_record *lat_rec, u32 lat)
{
	lat_rec->sum += lat;
S
Swen Schillig 已提交
2014 2015
	lat_rec->min = min(lat_rec->min, lat);
	lat_rec->max = max(lat_rec->max, lat);
2016 2017
}

2018
static void zfcp_fsf_req_trace(struct zfcp_fsf_req *req, struct scsi_cmnd *scsi)
2019
{
2020 2021
	struct fsf_qual_latency_info *lat_in;
	struct latency_cont *lat = NULL;
S
Swen Schillig 已提交
2022
	struct zfcp_unit *unit = req->unit;
2023 2024
	struct zfcp_blk_drv_data blktrc;
	int ticks = req->adapter->timer_ticks;
2025

2026
	lat_in = &req->qtcb->prefix.prot_status_qual.latency_info;
2027

2028 2029 2030 2031
	blktrc.flags = 0;
	blktrc.magic = ZFCP_BLK_DRV_DATA_MAGIC;
	if (req->status & ZFCP_STATUS_FSFREQ_ERROR)
		blktrc.flags |= ZFCP_BLK_REQ_ERROR;
2032 2033
	blktrc.inb_usage = req->qdio_req.qdio_inb_usage;
	blktrc.outb_usage = req->qdio_req.qdio_outb_usage;
2034

2035 2036
	if (req->adapter->adapter_features & FSF_FEATURE_MEASUREMENT_DATA &&
	    !(req->status & ZFCP_STATUS_FSFREQ_ERROR)) {
2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051
		blktrc.flags |= ZFCP_BLK_LAT_VALID;
		blktrc.channel_lat = lat_in->channel_lat * ticks;
		blktrc.fabric_lat = lat_in->fabric_lat * ticks;

		switch (req->qtcb->bottom.io.data_direction) {
		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;
		}
L
Linus Torvalds 已提交
2052

2053 2054 2055 2056 2057 2058 2059
		if (lat) {
			spin_lock(&unit->latencies.lock);
			zfcp_fsf_update_lat(&lat->channel, lat_in->channel_lat);
			zfcp_fsf_update_lat(&lat->fabric, lat_in->fabric_lat);
			lat->counter++;
			spin_unlock(&unit->latencies.lock);
		}
S
Stefan Raspl 已提交
2060 2061
	}

2062 2063
	blk_add_driver_data(scsi->request->q, scsi->request, &blktrc,
			    sizeof(blktrc));
S
Stefan Raspl 已提交
2064 2065
}

S
Swen Schillig 已提交
2066
static void zfcp_fsf_send_fcp_command_task_handler(struct zfcp_fsf_req *req)
L
Linus Torvalds 已提交
2067
{
2068
	struct scsi_cmnd *scpnt;
2069
	struct fcp_resp_with_ext *fcp_rsp;
L
Linus Torvalds 已提交
2070 2071
	unsigned long flags;

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

2074 2075 2076 2077 2078 2079
	scpnt = req->data;
	if (unlikely(!scpnt)) {
		read_unlock_irqrestore(&req->adapter->abort_lock, flags);
		return;
	}

S
Swen Schillig 已提交
2080
	if (unlikely(req->status & ZFCP_STATUS_FSFREQ_ERROR)) {
2081
		set_host_byte(scpnt, DID_TRANSPORT_DISRUPTED);
L
Linus Torvalds 已提交
2082 2083 2084
		goto skip_fsfstatus;
	}

2085 2086
	fcp_rsp = (struct fcp_resp_with_ext *) &req->qtcb->bottom.io.fcp_rsp;
	zfcp_fc_eval_fcp_rsp(fcp_rsp, scpnt);
L
Linus Torvalds 已提交
2087

S
Swen Schillig 已提交
2088
skip_fsfstatus:
2089
	zfcp_fsf_req_trace(req, scpnt);
2090
	zfcp_dbf_scsi_result(req->adapter->dbf, scpnt, req);
L
Linus Torvalds 已提交
2091 2092 2093 2094 2095 2096 2097 2098 2099

	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 已提交
2100
	read_unlock_irqrestore(&req->adapter->abort_lock, flags);
L
Linus Torvalds 已提交
2101 2102
}

S
Swen Schillig 已提交
2103
static void zfcp_fsf_send_fcp_ctm_handler(struct zfcp_fsf_req *req)
L
Linus Torvalds 已提交
2104
{
2105 2106
	struct fcp_resp_with_ext *fcp_rsp;
	struct fcp_resp_rsp_info *rsp_info;
L
Linus Torvalds 已提交
2107

2108 2109 2110 2111
	fcp_rsp = (struct fcp_resp_with_ext *) &req->qtcb->bottom.io.fcp_rsp;
	rsp_info = (struct fcp_resp_rsp_info *) &fcp_rsp[1];

	if ((rsp_info->rsp_code != FCP_TMF_CMPL) ||
S
Swen Schillig 已提交
2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127
	     (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 已提交
2128 2129
		goto skip_fsfstatus;

S
Swen Schillig 已提交
2130 2131 2132
	switch (header->fsf_status) {
	case FSF_HANDLE_MISMATCH:
	case FSF_PORT_HANDLE_NOT_VALID:
2133
		zfcp_erp_adapter_reopen(unit->port->adapter, 0, "fssfch1", req);
S
Swen Schillig 已提交
2134 2135 2136 2137
		req->status |= ZFCP_STATUS_FSFREQ_ERROR;
		break;
	case FSF_FCPLUN_NOT_VALID:
	case FSF_LUN_HANDLE_NOT_VALID:
2138
		zfcp_erp_port_reopen(unit->port, 0, "fssfch2", req);
S
Swen Schillig 已提交
2139
		req->status |= ZFCP_STATUS_FSFREQ_ERROR;
L
Linus Torvalds 已提交
2140
		break;
S
Swen Schillig 已提交
2141 2142
	case FSF_SERVICE_CLASS_NOT_SUPPORTED:
		zfcp_fsf_class_not_supp(req);
L
Linus Torvalds 已提交
2143
		break;
S
Swen Schillig 已提交
2144 2145 2146 2147 2148
	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,
2149 2150
			"Incorrect direction %d, unit 0x%016Lx on port "
			"0x%016Lx closed\n",
S
Swen Schillig 已提交
2151
			req->qtcb->bottom.io.data_direction,
2152 2153
			(unsigned long long)unit->fcp_lun,
			(unsigned long long)unit->port->wwpn);
2154 2155
		zfcp_erp_adapter_shutdown(unit->port->adapter, 0, "fssfch3",
					  req);
S
Swen Schillig 已提交
2156 2157 2158 2159
		req->status |= ZFCP_STATUS_FSFREQ_ERROR;
		break;
	case FSF_CMND_LENGTH_NOT_VALID:
		dev_err(&req->adapter->ccw_device->dev,
2160 2161
			"Incorrect CDB length %d, unit 0x%016Lx on "
			"port 0x%016Lx closed\n",
S
Swen Schillig 已提交
2162
			req->qtcb->bottom.io.fcp_cmnd_length,
2163 2164
			(unsigned long long)unit->fcp_lun,
			(unsigned long long)unit->port->wwpn);
2165 2166
		zfcp_erp_adapter_shutdown(unit->port->adapter, 0, "fssfch4",
					  req);
S
Swen Schillig 已提交
2167 2168 2169
		req->status |= ZFCP_STATUS_FSFREQ_ERROR;
		break;
	case FSF_PORT_BOXED:
2170
		zfcp_erp_port_boxed(unit->port, "fssfch5", req);
2171
		req->status |= ZFCP_STATUS_FSFREQ_ERROR;
S
Swen Schillig 已提交
2172 2173
		break;
	case FSF_LUN_BOXED:
2174
		zfcp_erp_unit_boxed(unit, "fssfch6", req);
2175
		req->status |= ZFCP_STATUS_FSFREQ_ERROR;
S
Swen Schillig 已提交
2176 2177 2178 2179
		break;
	case FSF_ADAPTER_STATUS_AVAILABLE:
		if (header->fsf_status_qual.word[0] ==
		    FSF_SQ_INVOKE_LINK_TEST_PROCEDURE)
2180
			zfcp_fc_test_link(unit->port);
S
Swen Schillig 已提交
2181
		req->status |= ZFCP_STATUS_FSFREQ_ERROR;
L
Linus Torvalds 已提交
2182 2183
		break;
	}
S
Swen Schillig 已提交
2184 2185 2186 2187 2188 2189
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;
2190
		put_device(&unit->dev);
S
Swen Schillig 已提交
2191
	}
L
Linus Torvalds 已提交
2192 2193
}

S
Swen Schillig 已提交
2194 2195 2196 2197
/**
 * 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 已提交
2198
 */
2199 2200
int zfcp_fsf_send_fcp_command_task(struct zfcp_unit *unit,
				   struct scsi_cmnd *scsi_cmnd)
L
Linus Torvalds 已提交
2201
{
S
Swen Schillig 已提交
2202
	struct zfcp_fsf_req *req;
2203
	struct fcp_cmnd *fcp_cmnd;
2204
	unsigned int sbtype = SBAL_FLAGS0_TYPE_READ;
S
Swen Schillig 已提交
2205
	int real_bytes, retval = -EIO;
2206
	struct zfcp_adapter *adapter = unit->port->adapter;
2207
	struct zfcp_qdio *qdio = adapter->qdio;
L
Linus Torvalds 已提交
2208

S
Swen Schillig 已提交
2209 2210 2211
	if (unlikely(!(atomic_read(&unit->status) &
		       ZFCP_STATUS_COMMON_UNBLOCKED)))
		return -EBUSY;
L
Linus Torvalds 已提交
2212

2213 2214 2215
	spin_lock(&qdio->req_q_lock);
	if (atomic_read(&qdio->req_q.count) <= 0) {
		atomic_inc(&qdio->req_q_full);
S
Swen Schillig 已提交
2216
		goto out;
2217
	}
2218

2219 2220 2221
	if (scsi_cmnd->sc_data_direction == DMA_TO_DEVICE)
		sbtype = SBAL_FLAGS0_TYPE_WRITE;

2222
	req = zfcp_fsf_req_create(qdio, FSF_QTCB_FCP_CMND,
2223
				  sbtype, adapter->pool.scsi_req);
2224

2225
	if (IS_ERR(req)) {
S
Swen Schillig 已提交
2226 2227 2228 2229
		retval = PTR_ERR(req);
		goto out;
	}

2230
	req->status |= ZFCP_STATUS_FSFREQ_CLEANUP;
S
Swen Schillig 已提交
2231 2232 2233 2234 2235 2236
	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;
2237
	req->qtcb->bottom.io.fcp_cmnd_length = FCP_CMND_LEN;
S
Swen Schillig 已提交
2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248

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

	/*
	 * set depending on data direction:
	 *      data direction bits in SBALE (SB Type)
	 *      data direction bits in QTCB
	 */
	switch (scsi_cmnd->sc_data_direction) {
	case DMA_NONE:
		req->qtcb->bottom.io.data_direction = FSF_DATADIR_CMND;
L
Linus Torvalds 已提交
2249
		break;
S
Swen Schillig 已提交
2250 2251 2252 2253 2254
	case DMA_FROM_DEVICE:
		req->qtcb->bottom.io.data_direction = FSF_DATADIR_READ;
		break;
	case DMA_TO_DEVICE:
		req->qtcb->bottom.io.data_direction = FSF_DATADIR_WRITE;
L
Linus Torvalds 已提交
2255
		break;
S
Swen Schillig 已提交
2256 2257
	case DMA_BIDIRECTIONAL:
		goto failed_scsi_cmnd;
L
Linus Torvalds 已提交
2258 2259
	}

2260 2261
	get_device(&unit->dev);

2262 2263
	fcp_cmnd = (struct fcp_cmnd *) &req->qtcb->bottom.io.fcp_cmnd;
	zfcp_fc_scsi_to_fcp(fcp_cmnd, scsi_cmnd);
L
Linus Torvalds 已提交
2264

2265
	real_bytes = zfcp_qdio_sbals_from_sg(qdio, &req->qdio_req,
S
Swen Schillig 已提交
2266
					     scsi_sglist(scsi_cmnd),
2267
					     ZFCP_FSF_MAX_SBALS_PER_REQ);
S
Swen Schillig 已提交
2268
	if (unlikely(real_bytes < 0)) {
2269
		if (req->qdio_req.sbal_number >= ZFCP_FSF_MAX_SBALS_PER_REQ) {
S
Swen Schillig 已提交
2270
			dev_err(&adapter->ccw_device->dev,
2271 2272
				"Oversize data package, unit 0x%016Lx "
				"on port 0x%016Lx closed\n",
2273 2274
				(unsigned long long)unit->fcp_lun,
				(unsigned long long)unit->port->wwpn);
2275
			zfcp_erp_unit_shutdown(unit, 0, "fssfct1", req);
S
Swen Schillig 已提交
2276 2277 2278
			retval = -EINVAL;
		}
		goto failed_scsi_cmnd;
L
Linus Torvalds 已提交
2279 2280
	}

S
Swen Schillig 已提交
2281 2282 2283
	retval = zfcp_fsf_req_send(req);
	if (unlikely(retval))
		goto failed_scsi_cmnd;
L
Linus Torvalds 已提交
2284

S
Swen Schillig 已提交
2285
	goto out;
L
Linus Torvalds 已提交
2286

S
Swen Schillig 已提交
2287
failed_scsi_cmnd:
2288
	put_device(&unit->dev);
S
Swen Schillig 已提交
2289 2290 2291
	zfcp_fsf_req_free(req);
	scsi_cmnd->host_scribble = NULL;
out:
2292
	spin_unlock(&qdio->req_q_lock);
S
Swen Schillig 已提交
2293
	return retval;
L
Linus Torvalds 已提交
2294 2295 2296
}

/**
S
Swen Schillig 已提交
2297 2298 2299 2300
 * 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 已提交
2301
 */
2302
struct zfcp_fsf_req *zfcp_fsf_send_fcp_ctm(struct zfcp_unit *unit, u8 tm_flags)
L
Linus Torvalds 已提交
2303
{
S
Swen Schillig 已提交
2304
	struct zfcp_fsf_req *req = NULL;
2305
	struct fcp_cmnd *fcp_cmnd;
2306
	struct zfcp_qdio *qdio = unit->port->adapter->qdio;
L
Linus Torvalds 已提交
2307

S
Swen Schillig 已提交
2308 2309 2310
	if (unlikely(!(atomic_read(&unit->status) &
		       ZFCP_STATUS_COMMON_UNBLOCKED)))
		return NULL;
L
Linus Torvalds 已提交
2311

2312
	spin_lock_bh(&qdio->req_q_lock);
2313
	if (zfcp_qdio_sbal_get(qdio))
S
Swen Schillig 已提交
2314
		goto out;
2315

2316
	req = zfcp_fsf_req_create(qdio, FSF_QTCB_FCP_CMND,
2317
				  SBAL_FLAGS0_TYPE_WRITE,
2318
				  qdio->adapter->pool.scsi_req);
2319

2320 2321
	if (IS_ERR(req)) {
		req = NULL;
S
Swen Schillig 已提交
2322
		goto out;
2323
	}
L
Linus Torvalds 已提交
2324

S
Swen Schillig 已提交
2325 2326 2327 2328 2329 2330 2331
	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;
2332
	req->qtcb->bottom.io.fcp_cmnd_length = FCP_CMND_LEN;
S
Swen Schillig 已提交
2333

2334
	zfcp_qdio_set_sbale_last(qdio, &req->qdio_req);
L
Linus Torvalds 已提交
2335

2336 2337
	fcp_cmnd = (struct fcp_cmnd *) &req->qtcb->bottom.io.fcp_cmnd;
	zfcp_fc_fcp_tm(fcp_cmnd, unit->device, tm_flags);
L
Linus Torvalds 已提交
2338

S
Swen Schillig 已提交
2339 2340 2341
	zfcp_fsf_start_timer(req, ZFCP_SCSI_ER_TIMEOUT);
	if (!zfcp_fsf_req_send(req))
		goto out;
L
Linus Torvalds 已提交
2342

S
Swen Schillig 已提交
2343 2344 2345
	zfcp_fsf_req_free(req);
	req = NULL;
out:
2346
	spin_unlock_bh(&qdio->req_q_lock);
S
Swen Schillig 已提交
2347 2348
	return req;
}
L
Linus Torvalds 已提交
2349

S
Swen Schillig 已提交
2350 2351
static void zfcp_fsf_control_file_handler(struct zfcp_fsf_req *req)
{
L
Linus Torvalds 已提交
2352 2353
}

S
Swen Schillig 已提交
2354 2355 2356 2357 2358
/**
 * 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 已提交
2359
 */
S
Swen Schillig 已提交
2360 2361
struct zfcp_fsf_req *zfcp_fsf_control_file(struct zfcp_adapter *adapter,
					   struct zfcp_fsf_cfdc *fsf_cfdc)
L
Linus Torvalds 已提交
2362
{
2363
	struct zfcp_qdio *qdio = adapter->qdio;
S
Swen Schillig 已提交
2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380
	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 已提交
2381

2382
	spin_lock_bh(&qdio->req_q_lock);
2383
	if (zfcp_qdio_sbal_get(qdio))
S
Swen Schillig 已提交
2384
		goto out;
L
Linus Torvalds 已提交
2385

2386
	req = zfcp_fsf_req_create(qdio, fsf_cfdc->command, direction, NULL);
2387
	if (IS_ERR(req)) {
S
Swen Schillig 已提交
2388 2389 2390
		retval = -EPERM;
		goto out;
	}
L
Linus Torvalds 已提交
2391

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

S
Swen Schillig 已提交
2394 2395 2396
	bottom = &req->qtcb->bottom.support;
	bottom->operation_subtype = FSF_CFDC_OPERATION_SUBTYPE;
	bottom->option = fsf_cfdc->option;
2397

2398
	bytes = zfcp_qdio_sbals_from_sg(qdio, &req->qdio_req,
2399 2400
					fsf_cfdc->sg,
					ZFCP_FSF_MAX_SBALS_PER_REQ);
S
Swen Schillig 已提交
2401 2402 2403 2404
	if (bytes != ZFCP_CFDC_MAX_SIZE) {
		zfcp_fsf_req_free(req);
		goto out;
	}
L
Linus Torvalds 已提交
2405

S
Swen Schillig 已提交
2406 2407 2408
	zfcp_fsf_start_timer(req, ZFCP_FSF_REQUEST_TIMEOUT);
	retval = zfcp_fsf_req_send(req);
out:
2409
	spin_unlock_bh(&qdio->req_q_lock);
2410

S
Swen Schillig 已提交
2411
	if (!retval) {
2412
		wait_for_completion(&req->completion);
S
Swen Schillig 已提交
2413
		return req;
L
Linus Torvalds 已提交
2414
	}
S
Swen Schillig 已提交
2415
	return ERR_PTR(retval);
L
Linus Torvalds 已提交
2416
}
2417 2418 2419 2420 2421 2422

/**
 * zfcp_fsf_reqid_check - validate req_id contained in SBAL returned by QDIO
 * @adapter: pointer to struct zfcp_adapter
 * @sbal_idx: response queue index of SBAL to be processed
 */
2423
void zfcp_fsf_reqid_check(struct zfcp_qdio *qdio, int sbal_idx)
2424
{
2425 2426
	struct zfcp_adapter *adapter = qdio->adapter;
	struct qdio_buffer *sbal = qdio->resp_q.sbal[sbal_idx];
2427 2428
	struct qdio_buffer_element *sbale;
	struct zfcp_fsf_req *fsf_req;
2429
	unsigned long req_id;
2430 2431 2432 2433 2434 2435
	int idx;

	for (idx = 0; idx < QDIO_MAX_ELEMENTS_PER_BUFFER; idx++) {

		sbale = &sbal->element[idx];
		req_id = (unsigned long) sbale->addr;
2436
		fsf_req = zfcp_reqlist_find_rm(adapter->req_list, req_id);
2437 2438 2439 2440 2441 2442 2443 2444 2445

		if (!fsf_req)
			/*
			 * Unknown request means that we have potentially memory
			 * corruption and must stop the machine immediately.
			 */
			panic("error: unknown req_id (%lx) on adapter %s.\n",
			      req_id, dev_name(&adapter->ccw_device->dev));

2446 2447
		fsf_req->qdio_req.sbal_response = sbal_idx;
		fsf_req->qdio_req.qdio_inb_usage =
2448
			atomic_read(&qdio->resp_q.count);
2449 2450 2451 2452 2453 2454
		zfcp_fsf_req_complete(fsf_req);

		if (likely(sbale->flags & SBAL_FLAGS_LAST_ENTRY))
			break;
	}
}