zfcp_fsf.c 67.5 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
	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;
498
	adapter->timer_ticks = bottom->timer_interval & ZFCP_FSF_TIMER_INT_MASK;
499 500
	adapter->stat_read_buf_num = max(bottom->status_read_buf_num,
					 (u16)FSF_STATUS_READS_RECOM);
S
Swen Schillig 已提交
501 502 503 504 505 506

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

699 700 701 702 703 704 705 706 707 708 709
	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);
		}

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

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

S
Swen Schillig 已提交
723
	return req;
L
Linus Torvalds 已提交
724 725
}

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

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

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

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

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

766
	spin_lock_bh(&qdio->req_q_lock);
767
	if (zfcp_qdio_sbal_get(qdio))
S
Swen Schillig 已提交
768 769
		goto out;

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

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

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

S
Swen Schillig 已提交
788 789 790
	retval = zfcp_fsf_req_send(req);
	if (retval)
		goto failed_req_send;
L
Linus Torvalds 已提交
791

S
Swen Schillig 已提交
792 793 794
	goto out;

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

/**
S
Swen Schillig 已提交
854 855 856 857
 * 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 已提交
858 859
 */

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

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

S
Swen Schillig 已提交
877 878 879
	if (unlikely(!(atomic_read(&unit->status) &
		       ZFCP_STATUS_COMMON_UNBLOCKED)))
		goto out_error_free;
L
Linus Torvalds 已提交
880

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

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

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

907
	ct->status = -EINVAL;
L
Linus Torvalds 已提交
908

S
Swen Schillig 已提交
909
	if (req->status & ZFCP_STATUS_FSFREQ_ERROR)
L
Linus Torvalds 已提交
910 911 912 913
		goto skip_fsfstatus;

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

skip_fsfstatus:
946 947
	if (ct->handler)
		ct->handler(ct->handler_data);
S
Swen Schillig 已提交
948
}
L
Linus Torvalds 已提交
949

950 951
static void zfcp_fsf_setup_ct_els_unchained(struct zfcp_qdio *qdio,
					    struct zfcp_qdio_req *q_req,
952 953 954
					    struct scatterlist *sg_req,
					    struct scatterlist *sg_resp)
{
955 956 957
	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);
958 959
}

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

969
	if (!(feat & FSF_FEATURE_ELS_CT_CHAINED_SBALS)) {
970 971
		if (!zfcp_qdio_sg_one_sbale(sg_req) ||
		    !zfcp_qdio_sg_one_sbale(sg_resp))
972 973
			return -EOPNOTSUPP;

974 975
		zfcp_fsf_setup_ct_els_unchained(adapter->qdio, &req->qdio_req,
						sg_req, sg_resp);
976 977 978 979
		return 0;
	}

	/* use single, unchained SBAL if it can hold the request */
980
	if (zfcp_qdio_sg_one_sbale(sg_req) && zfcp_qdio_sg_one_sbale(sg_resp)) {
981 982
		zfcp_fsf_setup_ct_els_unchained(adapter->qdio, &req->qdio_req,
						sg_req, sg_resp);
983 984 985
		return 0;
	}

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

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

999 1000 1001 1002 1003 1004
	return 0;
}

static int zfcp_fsf_setup_ct_els(struct zfcp_fsf_req *req,
				 struct scatterlist *sg_req,
				 struct scatterlist *sg_resp,
1005
				 int max_sbals, unsigned int timeout)
1006 1007 1008 1009 1010 1011 1012
{
	int ret;

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

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

	return 0;
L
Linus Torvalds 已提交
1021 1022 1023
}

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

1036
	spin_lock_bh(&qdio->req_q_lock);
1037
	if (zfcp_qdio_sbal_get(qdio))
S
Swen Schillig 已提交
1038
		goto out;
L
Linus Torvalds 已提交
1039

1040 1041
	req = zfcp_fsf_req_create(qdio, FSF_QTCB_SEND_GENERIC,
				  SBAL_FLAGS0_TYPE_WRITE_READ, pool);
1042

1043
	if (IS_ERR(req)) {
S
Swen Schillig 已提交
1044 1045
		ret = PTR_ERR(req);
		goto out;
1046 1047
	}

1048
	req->status |= ZFCP_STATUS_FSFREQ_CLEANUP;
1049
	ret = zfcp_fsf_setup_ct_els(req, ct->req, ct->resp,
1050
				    ZFCP_FSF_MAX_SBALS_PER_REQ, timeout);
C
Christof Schmitt 已提交
1051
	if (ret)
L
Linus Torvalds 已提交
1052 1053
		goto failed_send;

S
Swen Schillig 已提交
1054
	req->handler = zfcp_fsf_send_ct_handler;
1055
	req->qtcb->header.port_handle = wka_port->handle;
S
Swen Schillig 已提交
1056 1057
	req->data = ct;

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

S
Swen Schillig 已提交
1060 1061 1062
	ret = zfcp_fsf_req_send(req);
	if (ret)
		goto failed_send;
L
Linus Torvalds 已提交
1063

S
Swen Schillig 已提交
1064
	goto out;
L
Linus Torvalds 已提交
1065

S
Swen Schillig 已提交
1066 1067 1068
failed_send:
	zfcp_fsf_req_free(req);
out:
1069
	spin_unlock_bh(&qdio->req_q_lock);
S
Swen Schillig 已提交
1070
	return ret;
L
Linus Torvalds 已提交
1071 1072
}

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

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

S
Swen Schillig 已提交
1081
	if (req->status & ZFCP_STATUS_FSFREQ_ERROR)
L
Linus Torvalds 已提交
1082 1083 1084 1085
		goto skip_fsfstatus;

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

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

1133
	spin_lock_bh(&qdio->req_q_lock);
1134
	if (zfcp_qdio_sbal_get(qdio))
S
Swen Schillig 已提交
1135
		goto out;
1136

1137 1138
	req = zfcp_fsf_req_create(qdio, FSF_QTCB_SEND_ELS,
				  SBAL_FLAGS0_TYPE_WRITE_READ, NULL);
1139

1140
	if (IS_ERR(req)) {
S
Swen Schillig 已提交
1141 1142 1143 1144
		ret = PTR_ERR(req);
		goto out;
	}

1145
	req->status |= ZFCP_STATUS_FSFREQ_CLEANUP;
1146
	ret = zfcp_fsf_setup_ct_els(req, els->req, els->resp, 2, timeout);
1147

S
Swen Schillig 已提交
1148 1149 1150
	if (ret)
		goto failed_send;

1151
	hton24(req->qtcb->bottom.support.d_id, d_id);
S
Swen Schillig 已提交
1152 1153 1154
	req->handler = zfcp_fsf_send_els_handler;
	req->data = els;

S
Swen Schillig 已提交
1155
	zfcp_dbf_san_els_request(req);
S
Swen Schillig 已提交
1156 1157 1158 1159 1160 1161 1162 1163 1164 1165

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

	goto out;

failed_send:
	zfcp_fsf_req_free(req);
out:
1166
	spin_unlock_bh(&qdio->req_q_lock);
S
Swen Schillig 已提交
1167
	return ret;
L
Linus Torvalds 已提交
1168 1169
}

S
Swen Schillig 已提交
1170
int zfcp_fsf_exchange_config_data(struct zfcp_erp_action *erp_action)
L
Linus Torvalds 已提交
1171
{
S
Swen Schillig 已提交
1172
	struct zfcp_fsf_req *req;
1173
	struct zfcp_qdio *qdio = erp_action->adapter->qdio;
S
Swen Schillig 已提交
1174 1175
	int retval = -EIO;

1176
	spin_lock_bh(&qdio->req_q_lock);
1177
	if (zfcp_qdio_sbal_get(qdio))
S
Swen Schillig 已提交
1178
		goto out;
1179

1180
	req = zfcp_fsf_req_create(qdio, FSF_QTCB_EXCHANGE_CONFIG_DATA,
1181
				  SBAL_FLAGS0_TYPE_READ,
1182
				  qdio->adapter->pool.erp_req);
1183

1184
	if (IS_ERR(req)) {
S
Swen Schillig 已提交
1185 1186
		retval = PTR_ERR(req);
		goto out;
L
Linus Torvalds 已提交
1187 1188
	}

1189
	req->status |= ZFCP_STATUS_FSFREQ_CLEANUP;
1190
	zfcp_qdio_set_sbale_last(qdio, &req->qdio_req);
L
Linus Torvalds 已提交
1191

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

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

1212
int zfcp_fsf_exchange_config_data_sync(struct zfcp_qdio *qdio,
S
Swen Schillig 已提交
1213
				       struct fsf_qtcb_bottom_config *data)
1214
{
S
Swen Schillig 已提交
1215 1216 1217
	struct zfcp_fsf_req *req = NULL;
	int retval = -EIO;

1218
	spin_lock_bh(&qdio->req_q_lock);
1219
	if (zfcp_qdio_sbal_get(qdio))
1220
		goto out_unlock;
S
Swen Schillig 已提交
1221

1222 1223
	req = zfcp_fsf_req_create(qdio, FSF_QTCB_EXCHANGE_CONFIG_DATA,
				  SBAL_FLAGS0_TYPE_READ, NULL);
1224

1225
	if (IS_ERR(req)) {
S
Swen Schillig 已提交
1226
		retval = PTR_ERR(req);
1227
		goto out_unlock;
1228 1229
	}

1230
	zfcp_qdio_set_sbale_last(qdio, &req->qdio_req);
S
Swen Schillig 已提交
1231
	req->handler = zfcp_fsf_exchange_config_data_handler;
1232

S
Swen Schillig 已提交
1233
	req->qtcb->bottom.config.feature_selection =
1234 1235 1236 1237 1238 1239
			FSF_FEATURE_CFDC |
			FSF_FEATURE_LUN_SHARING |
			FSF_FEATURE_NOTIFICATION_LOST |
			FSF_FEATURE_UPDATE_ALERT;

	if (data)
S
Swen Schillig 已提交
1240
		req->data = data;
1241

S
Swen Schillig 已提交
1242 1243
	zfcp_fsf_start_timer(req, ZFCP_FSF_REQUEST_TIMEOUT);
	retval = zfcp_fsf_req_send(req);
1244
	spin_unlock_bh(&qdio->req_q_lock);
C
Christof Schmitt 已提交
1245
	if (!retval)
1246
		wait_for_completion(&req->completion);
1247

S
Swen Schillig 已提交
1248
	zfcp_fsf_req_free(req);
1249
	return retval;
1250

1251
out_unlock:
1252
	spin_unlock_bh(&qdio->req_q_lock);
L
Linus Torvalds 已提交
1253 1254 1255 1256 1257
	return retval;
}

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

1267
	if (!(qdio->adapter->adapter_features & FSF_FEATURE_HBAAPI_MANAGEMENT))
1268
		return -EOPNOTSUPP;
L
Linus Torvalds 已提交
1269

1270
	spin_lock_bh(&qdio->req_q_lock);
1271
	if (zfcp_qdio_sbal_get(qdio))
S
Swen Schillig 已提交
1272
		goto out;
1273

1274
	req = zfcp_fsf_req_create(qdio, FSF_QTCB_EXCHANGE_PORT_DATA,
1275
				  SBAL_FLAGS0_TYPE_READ,
1276
				  qdio->adapter->pool.erp_req);
1277

1278
	if (IS_ERR(req)) {
S
Swen Schillig 已提交
1279 1280
		retval = PTR_ERR(req);
		goto out;
1281 1282
	}

1283
	req->status |= ZFCP_STATUS_FSFREQ_CLEANUP;
1284
	zfcp_qdio_set_sbale_last(qdio, &req->qdio_req);
L
Linus Torvalds 已提交
1285

S
Swen Schillig 已提交
1286 1287
	req->handler = zfcp_fsf_exchange_port_data_handler;
	req->erp_action = erp_action;
1288
	erp_action->fsf_req_id = req->req_id;
1289

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

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

1313
	if (!(qdio->adapter->adapter_features & FSF_FEATURE_HBAAPI_MANAGEMENT))
1314 1315
		return -EOPNOTSUPP;

1316
	spin_lock_bh(&qdio->req_q_lock);
1317
	if (zfcp_qdio_sbal_get(qdio))
1318
		goto out_unlock;
S
Swen Schillig 已提交
1319

1320 1321
	req = zfcp_fsf_req_create(qdio, FSF_QTCB_EXCHANGE_PORT_DATA,
				  SBAL_FLAGS0_TYPE_READ, NULL);
1322

1323
	if (IS_ERR(req)) {
S
Swen Schillig 已提交
1324
		retval = PTR_ERR(req);
1325
		goto out_unlock;
L
Linus Torvalds 已提交
1326 1327
	}

1328
	if (data)
S
Swen Schillig 已提交
1329
		req->data = data;
1330

1331
	zfcp_qdio_set_sbale_last(qdio, &req->qdio_req);
1332

S
Swen Schillig 已提交
1333 1334 1335
	req->handler = zfcp_fsf_exchange_port_data_handler;
	zfcp_fsf_start_timer(req, ZFCP_FSF_REQUEST_TIMEOUT);
	retval = zfcp_fsf_req_send(req);
1336
	spin_unlock_bh(&qdio->req_q_lock);
1337

C
Christof Schmitt 已提交
1338
	if (!retval)
1339 1340
		wait_for_completion(&req->completion);

S
Swen Schillig 已提交
1341
	zfcp_fsf_req_free(req);
L
Linus Torvalds 已提交
1342 1343

	return retval;
1344 1345

out_unlock:
1346
	spin_unlock_bh(&qdio->req_q_lock);
1347
	return retval;
L
Linus Torvalds 已提交
1348 1349
}

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

S
Swen Schillig 已提交
1356
	if (req->status & ZFCP_STATUS_FSFREQ_ERROR)
1357
		goto out;
L
Linus Torvalds 已提交
1358 1359 1360 1361 1362

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

out:
1415
	put_device(&port->dev);
L
Linus Torvalds 已提交
1416 1417
}

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

1430
	spin_lock_bh(&qdio->req_q_lock);
1431
	if (zfcp_qdio_sbal_get(qdio))
S
Swen Schillig 已提交
1432 1433
		goto out;

1434
	req = zfcp_fsf_req_create(qdio, FSF_QTCB_OPEN_PORT_WITH_DID,
1435
				  SBAL_FLAGS0_TYPE_READ,
1436
				  qdio->adapter->pool.erp_req);
1437

1438
	if (IS_ERR(req)) {
S
Swen Schillig 已提交
1439
		retval = PTR_ERR(req);
L
Linus Torvalds 已提交
1440
		goto out;
S
Swen Schillig 已提交
1441
	}
L
Linus Torvalds 已提交
1442

1443
	req->status |= ZFCP_STATUS_FSFREQ_CLEANUP;
1444
	zfcp_qdio_set_sbale_last(qdio, &req->qdio_req);
L
Linus Torvalds 已提交
1445

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

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

S
Swen Schillig 已提交
1465
static void zfcp_fsf_close_port_handler(struct zfcp_fsf_req *req)
L
Linus Torvalds 已提交
1466
{
S
Swen Schillig 已提交
1467
	struct zfcp_port *port = req->data;
L
Linus Torvalds 已提交
1468

S
Swen Schillig 已提交
1469
	if (req->status & ZFCP_STATUS_FSFREQ_ERROR)
1470
		return;
L
Linus Torvalds 已提交
1471

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

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

1498
	spin_lock_bh(&qdio->req_q_lock);
1499
	if (zfcp_qdio_sbal_get(qdio))
S
Swen Schillig 已提交
1500 1501
		goto out;

1502
	req = zfcp_fsf_req_create(qdio, FSF_QTCB_CLOSE_PORT,
1503
				  SBAL_FLAGS0_TYPE_READ,
1504
				  qdio->adapter->pool.erp_req);
1505

1506
	if (IS_ERR(req)) {
S
Swen Schillig 已提交
1507
		retval = PTR_ERR(req);
L
Linus Torvalds 已提交
1508
		goto out;
S
Swen Schillig 已提交
1509
	}
L
Linus Torvalds 已提交
1510

1511
	req->status |= ZFCP_STATUS_FSFREQ_CLEANUP;
1512
	zfcp_qdio_set_sbale_last(qdio, &req->qdio_req);
L
Linus Torvalds 已提交
1513

S
Swen Schillig 已提交
1514 1515 1516 1517
	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;
1518
	erp_action->fsf_req_id = req->req_id;
S
Swen Schillig 已提交
1519

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

1531 1532
static void zfcp_fsf_open_wka_port_handler(struct zfcp_fsf_req *req)
{
1533
	struct zfcp_fc_wka_port *wka_port = req->data;
1534 1535 1536
	struct fsf_qtcb_header *header = &req->qtcb->header;

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

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

1573
	spin_lock_bh(&qdio->req_q_lock);
1574
	if (zfcp_qdio_sbal_get(qdio))
1575 1576
		goto out;

1577
	req = zfcp_fsf_req_create(qdio, FSF_QTCB_OPEN_PORT_WITH_DID,
1578
				  SBAL_FLAGS0_TYPE_READ,
1579
				  qdio->adapter->pool.erp_req);
1580

1581 1582 1583 1584 1585
	if (unlikely(IS_ERR(req))) {
		retval = PTR_ERR(req);
		goto out;
	}

1586
	req->status |= ZFCP_STATUS_FSFREQ_CLEANUP;
1587
	zfcp_qdio_set_sbale_last(qdio, &req->qdio_req);
1588 1589

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

static void zfcp_fsf_close_wka_port_handler(struct zfcp_fsf_req *req)
{
1604
	struct zfcp_fc_wka_port *wka_port = req->data;
1605 1606 1607

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

1611
	wka_port->status = ZFCP_FC_WKA_PORT_OFFLINE;
1612 1613 1614 1615 1616
	wake_up(&wka_port->completion_wq);
}

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

1626
	spin_lock_bh(&qdio->req_q_lock);
1627
	if (zfcp_qdio_sbal_get(qdio))
1628 1629
		goto out;

1630
	req = zfcp_fsf_req_create(qdio, FSF_QTCB_CLOSE_PORT,
1631
				  SBAL_FLAGS0_TYPE_READ,
1632
				  qdio->adapter->pool.erp_req);
1633

1634 1635 1636 1637 1638
	if (unlikely(IS_ERR(req))) {
		retval = PTR_ERR(req);
		goto out;
	}

1639
	req->status |= ZFCP_STATUS_FSFREQ_CLEANUP;
1640
	zfcp_qdio_set_sbale_last(qdio, &req->qdio_req);
1641 1642 1643 1644 1645 1646 1647 1648 1649 1650

	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:
1651
	spin_unlock_bh(&qdio->req_q_lock);
1652 1653 1654
	return retval;
}

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

S
Swen Schillig 已提交
1661
	if (req->status & ZFCP_STATUS_FSFREQ_ERROR)
1662
		return;
L
Linus Torvalds 已提交
1663 1664 1665

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

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

1718
	spin_lock_bh(&qdio->req_q_lock);
1719
	if (zfcp_qdio_sbal_get(qdio))
L
Linus Torvalds 已提交
1720 1721
		goto out;

1722
	req = zfcp_fsf_req_create(qdio, FSF_QTCB_CLOSE_PHYSICAL_PORT,
1723
				  SBAL_FLAGS0_TYPE_READ,
1724
				  qdio->adapter->pool.erp_req);
1725

1726
	if (IS_ERR(req)) {
S
Swen Schillig 已提交
1727 1728 1729
		retval = PTR_ERR(req);
		goto out;
	}
L
Linus Torvalds 已提交
1730

1731
	req->status |= ZFCP_STATUS_FSFREQ_CLEANUP;
1732
	zfcp_qdio_set_sbale_last(qdio, &req->qdio_req);
L
Linus Torvalds 已提交
1733

S
Swen Schillig 已提交
1734 1735 1736 1737
	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;
1738
	erp_action->fsf_req_id = req->req_id;
S
Swen Schillig 已提交
1739

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

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

S
Swen Schillig 已提交
1761
	if (req->status & ZFCP_STATUS_FSFREQ_ERROR)
1762
		return;
L
Linus Torvalds 已提交
1763 1764

	atomic_clear_mask(ZFCP_STATUS_COMMON_ACCESS_DENIED |
1765
			  ZFCP_STATUS_COMMON_ACCESS_BOXED |
L
Linus Torvalds 已提交
1766 1767 1768 1769 1770 1771 1772
			  ZFCP_STATUS_UNIT_SHARED |
			  ZFCP_STATUS_UNIT_READONLY,
			  &unit->status);

	switch (header->fsf_status) {

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

	case FSF_GOOD:
		unit->handle = header->lun_handle;
		atomic_set_mask(ZFCP_STATUS_COMMON_OPEN, &unit->status);
1828 1829 1830

		if (!(adapter->connection_features & FSF_FEATURE_NPIV_MODE) &&
		    (adapter->adapter_features & FSF_FEATURE_LUN_SHARING) &&
1831
		    !zfcp_ccw_priv_sch(adapter)) {
1832 1833 1834 1835 1836
			exclusive = (bottom->lun_access_info &
					FSF_UNIT_ACCESS_EXCLUSIVE);
			readwrite = (bottom->lun_access_info &
					FSF_UNIT_ACCESS_OUTBOUND_TRANSFER);

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

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

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

1889
	spin_lock_bh(&qdio->req_q_lock);
1890
	if (zfcp_qdio_sbal_get(qdio))
L
Linus Torvalds 已提交
1891 1892
		goto out;

1893
	req = zfcp_fsf_req_create(qdio, FSF_QTCB_OPEN_LUN,
1894
				  SBAL_FLAGS0_TYPE_READ,
1895
				  adapter->pool.erp_req);
1896

1897
	if (IS_ERR(req)) {
S
Swen Schillig 已提交
1898 1899 1900 1901
		retval = PTR_ERR(req);
		goto out;
	}

1902
	req->status |= ZFCP_STATUS_FSFREQ_CLEANUP;
1903
	zfcp_qdio_set_sbale_last(qdio, &req->qdio_req);
L
Linus Torvalds 已提交
1904

S
Swen Schillig 已提交
1905 1906 1907 1908 1909
	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;
1910
	erp_action->fsf_req_id = req->req_id;
S
Swen Schillig 已提交
1911 1912 1913 1914

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

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

S
Swen Schillig 已提交
1926
static void zfcp_fsf_close_unit_handler(struct zfcp_fsf_req *req)
L
Linus Torvalds 已提交
1927
{
S
Swen Schillig 已提交
1928
	struct zfcp_unit *unit = req->data;
L
Linus Torvalds 已提交
1929

S
Swen Schillig 已提交
1930
	if (req->status & ZFCP_STATUS_FSFREQ_ERROR)
1931
		return;
L
Linus Torvalds 已提交
1932

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

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

1973
	spin_lock_bh(&qdio->req_q_lock);
1974
	if (zfcp_qdio_sbal_get(qdio))
L
Linus Torvalds 已提交
1975
		goto out;
1976

1977
	req = zfcp_fsf_req_create(qdio, FSF_QTCB_CLOSE_LUN,
1978
				  SBAL_FLAGS0_TYPE_READ,
1979
				  qdio->adapter->pool.erp_req);
1980

1981
	if (IS_ERR(req)) {
S
Swen Schillig 已提交
1982 1983 1984
		retval = PTR_ERR(req);
		goto out;
	}
L
Linus Torvalds 已提交
1985

1986
	req->status |= ZFCP_STATUS_FSFREQ_CLEANUP;
1987
	zfcp_qdio_set_sbale_last(qdio, &req->qdio_req);
L
Linus Torvalds 已提交
1988

S
Swen Schillig 已提交
1989 1990 1991 1992 1993
	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;
1994
	erp_action->fsf_req_id = req->req_id;
1995

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

2007 2008 2009
static void zfcp_fsf_update_lat(struct fsf_latency_record *lat_rec, u32 lat)
{
	lat_rec->sum += lat;
S
Swen Schillig 已提交
2010 2011
	lat_rec->min = min(lat_rec->min, lat);
	lat_rec->max = max(lat_rec->max, lat);
2012 2013
}

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

2022
	lat_in = &req->qtcb->prefix.prot_status_qual.latency_info;
2023

2024 2025 2026 2027
	blktrc.flags = 0;
	blktrc.magic = ZFCP_BLK_DRV_DATA_MAGIC;
	if (req->status & ZFCP_STATUS_FSFREQ_ERROR)
		blktrc.flags |= ZFCP_BLK_REQ_ERROR;
2028 2029
	blktrc.inb_usage = req->qdio_req.qdio_inb_usage;
	blktrc.outb_usage = req->qdio_req.qdio_outb_usage;
2030

2031 2032
	if (req->adapter->adapter_features & FSF_FEATURE_MEASUREMENT_DATA &&
	    !(req->status & ZFCP_STATUS_FSFREQ_ERROR)) {
2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047
		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 已提交
2048

2049 2050 2051 2052 2053 2054 2055
		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 已提交
2056 2057
	}

2058 2059
	blk_add_driver_data(scsi->request->q, scsi->request, &blktrc,
			    sizeof(blktrc));
S
Stefan Raspl 已提交
2060 2061
}

S
Swen Schillig 已提交
2062
static void zfcp_fsf_send_fcp_command_task_handler(struct zfcp_fsf_req *req)
L
Linus Torvalds 已提交
2063
{
2064
	struct scsi_cmnd *scpnt;
2065
	struct fcp_resp_with_ext *fcp_rsp;
L
Linus Torvalds 已提交
2066 2067
	unsigned long flags;

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

2070 2071 2072 2073 2074 2075
	scpnt = req->data;
	if (unlikely(!scpnt)) {
		read_unlock_irqrestore(&req->adapter->abort_lock, flags);
		return;
	}

S
Swen Schillig 已提交
2076
	if (unlikely(req->status & ZFCP_STATUS_FSFREQ_ERROR)) {
2077
		set_host_byte(scpnt, DID_TRANSPORT_DISRUPTED);
L
Linus Torvalds 已提交
2078 2079 2080
		goto skip_fsfstatus;
	}

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

S
Swen Schillig 已提交
2084
skip_fsfstatus:
2085
	zfcp_fsf_req_trace(req, scpnt);
2086
	zfcp_dbf_scsi_result(req->adapter->dbf, scpnt, req);
L
Linus Torvalds 已提交
2087 2088 2089 2090 2091 2092 2093 2094 2095

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

S
Swen Schillig 已提交
2099
static void zfcp_fsf_send_fcp_ctm_handler(struct zfcp_fsf_req *req)
L
Linus Torvalds 已提交
2100
{
2101 2102
	struct fcp_resp_with_ext *fcp_rsp;
	struct fcp_resp_rsp_info *rsp_info;
L
Linus Torvalds 已提交
2103

2104 2105 2106 2107
	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 已提交
2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123
	     (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 已提交
2124 2125
		goto skip_fsfstatus;

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

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

S
Swen Schillig 已提交
2205 2206 2207
	if (unlikely(!(atomic_read(&unit->status) &
		       ZFCP_STATUS_COMMON_UNBLOCKED)))
		return -EBUSY;
L
Linus Torvalds 已提交
2208

2209 2210 2211
	spin_lock(&qdio->req_q_lock);
	if (atomic_read(&qdio->req_q.count) <= 0) {
		atomic_inc(&qdio->req_q_full);
S
Swen Schillig 已提交
2212
		goto out;
2213
	}
2214

2215 2216 2217
	if (scsi_cmnd->sc_data_direction == DMA_TO_DEVICE)
		sbtype = SBAL_FLAGS0_TYPE_WRITE;

2218
	req = zfcp_fsf_req_create(qdio, FSF_QTCB_FCP_CMND,
2219
				  sbtype, adapter->pool.scsi_req);
2220

2221
	if (IS_ERR(req)) {
S
Swen Schillig 已提交
2222 2223 2224 2225
		retval = PTR_ERR(req);
		goto out;
	}

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

	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 已提交
2245
		break;
S
Swen Schillig 已提交
2246 2247 2248 2249 2250
	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 已提交
2251
		break;
S
Swen Schillig 已提交
2252 2253
	case DMA_BIDIRECTIONAL:
		goto failed_scsi_cmnd;
L
Linus Torvalds 已提交
2254 2255
	}

2256 2257
	get_device(&unit->dev);

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

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

S
Swen Schillig 已提交
2277 2278 2279
	retval = zfcp_fsf_req_send(req);
	if (unlikely(retval))
		goto failed_scsi_cmnd;
L
Linus Torvalds 已提交
2280

S
Swen Schillig 已提交
2281
	goto out;
L
Linus Torvalds 已提交
2282

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

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

S
Swen Schillig 已提交
2304 2305 2306
	if (unlikely(!(atomic_read(&unit->status) &
		       ZFCP_STATUS_COMMON_UNBLOCKED)))
		return NULL;
L
Linus Torvalds 已提交
2307

2308
	spin_lock_bh(&qdio->req_q_lock);
2309
	if (zfcp_qdio_sbal_get(qdio))
S
Swen Schillig 已提交
2310
		goto out;
2311

2312
	req = zfcp_fsf_req_create(qdio, FSF_QTCB_FCP_CMND,
2313
				  SBAL_FLAGS0_TYPE_WRITE,
2314
				  qdio->adapter->pool.scsi_req);
2315

2316 2317
	if (IS_ERR(req)) {
		req = NULL;
S
Swen Schillig 已提交
2318
		goto out;
2319
	}
L
Linus Torvalds 已提交
2320

S
Swen Schillig 已提交
2321 2322 2323 2324 2325 2326 2327
	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;
2328
	req->qtcb->bottom.io.fcp_cmnd_length = FCP_CMND_LEN;
S
Swen Schillig 已提交
2329

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

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

S
Swen Schillig 已提交
2335 2336 2337
	zfcp_fsf_start_timer(req, ZFCP_SCSI_ER_TIMEOUT);
	if (!zfcp_fsf_req_send(req))
		goto out;
L
Linus Torvalds 已提交
2338

S
Swen Schillig 已提交
2339 2340 2341
	zfcp_fsf_req_free(req);
	req = NULL;
out:
2342
	spin_unlock_bh(&qdio->req_q_lock);
S
Swen Schillig 已提交
2343 2344
	return req;
}
L
Linus Torvalds 已提交
2345

S
Swen Schillig 已提交
2346 2347
static void zfcp_fsf_control_file_handler(struct zfcp_fsf_req *req)
{
L
Linus Torvalds 已提交
2348 2349
}

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

2378
	spin_lock_bh(&qdio->req_q_lock);
2379
	if (zfcp_qdio_sbal_get(qdio))
S
Swen Schillig 已提交
2380
		goto out;
L
Linus Torvalds 已提交
2381

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

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

S
Swen Schillig 已提交
2390 2391 2392
	bottom = &req->qtcb->bottom.support;
	bottom->operation_subtype = FSF_CFDC_OPERATION_SUBTYPE;
	bottom->option = fsf_cfdc->option;
2393

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

S
Swen Schillig 已提交
2402 2403 2404
	zfcp_fsf_start_timer(req, ZFCP_FSF_REQUEST_TIMEOUT);
	retval = zfcp_fsf_req_send(req);
out:
2405
	spin_unlock_bh(&qdio->req_q_lock);
2406

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

/**
 * 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
 */
2419
void zfcp_fsf_reqid_check(struct zfcp_qdio *qdio, int sbal_idx)
2420
{
2421 2422
	struct zfcp_adapter *adapter = qdio->adapter;
	struct qdio_buffer *sbal = qdio->resp_q.sbal[sbal_idx];
2423 2424
	struct qdio_buffer_element *sbale;
	struct zfcp_fsf_req *fsf_req;
2425
	unsigned long req_id;
2426 2427 2428 2429 2430 2431
	int idx;

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

		sbale = &sbal->element[idx];
		req_id = (unsigned long) sbale->addr;
2432
		fsf_req = zfcp_reqlist_find_rm(adapter->req_list, req_id);
2433 2434 2435 2436 2437 2438 2439 2440 2441

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

2442 2443
		fsf_req->qdio_req.sbal_response = sbal_idx;
		fsf_req->qdio_req.qdio_inb_usage =
2444
			atomic_read(&qdio->resp_q.count);
2445 2446 2447 2448 2449 2450
		zfcp_fsf_req_complete(fsf_req);

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