You need to sign in or sign up before continuing.
zfcp_fsf.c 70.2 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 <scsi/fc/fc_els.h>
L
Linus Torvalds 已提交
14
#include "zfcp_ext.h"
15
#include "zfcp_fc.h"
16
#include "zfcp_dbf.h"
17
#include "zfcp_reqlist.h"
L
Linus Torvalds 已提交
18

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

153
	zfcp_scsi_schedule_rports_block(adapter);
154

S
Swen Schillig 已提交
155
	if (!link_down)
156
		goto out;
157

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

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

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

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

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

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

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

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

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

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

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

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

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

S
Swen Schillig 已提交
356
	zfcp_dbf_hba_fsf_response(req);
L
Linus Torvalds 已提交
357

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

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

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

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

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

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

449 450 451 452 453 454 455 456 457 458 459 460 461 462 463
/**
 * 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);
464
	zfcp_reqlist_move(adapter->req_list, &remove_queue);
465 466 467 468 469 470 471 472

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

480 481 482 483 484
	/* 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 已提交
485

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

489 490
	fc_host_port_name(shost) = nsp->fl_wwpn;
	fc_host_node_name(shost) = nsp->fl_wwnn;
491
	fc_host_port_id(shost) = ntoh24(bottom->s_id);
S
Swen Schillig 已提交
492 493
	fc_host_speed(shost) = bottom->fc_link_speed;
	fc_host_supported_classes(shost) = FC_COS_CLASS2 | FC_COS_CLASS3;
494 495
	fc_host_supported_fc4s(shost)[2] = 1; /* FCP */
	fc_host_active_fc4s(shost)[2] = 1; /* FCP */
S
Swen Schillig 已提交
496 497 498 499 500 501 502 503 504

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

611
	if (adapter->connection_features & FSF_FEATURE_NPIV_MODE) {
S
Swen Schillig 已提交
612
		fc_host_permanent_port_name(shost) = bottom->wwpn;
613 614
		fc_host_port_type(shost) = FC_PORTTYPE_NPIV;
	} else
S
Swen Schillig 已提交
615 616 617 618
		fc_host_permanent_port_name(shost) = fc_host_port_name(shost);
	fc_host_maxframe_size(shost) = bottom->maximum_frame_size;
	fc_host_supported_speeds(shost) = bottom->supported_speed;
}
L
Linus Torvalds 已提交
619

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

639
static int zfcp_fsf_sbal_check(struct zfcp_qdio *qdio)
S
Swen Schillig 已提交
640
{
641
	struct zfcp_qdio_queue *req_q = &qdio->req_q;
S
Swen Schillig 已提交
642

643
	spin_lock_bh(&qdio->req_q_lock);
644 645
	if (atomic_read(&req_q->count))
		return 1;
646
	spin_unlock_bh(&qdio->req_q_lock);
647 648 649
	return 0;
}

650
static int zfcp_fsf_req_sbal_get(struct zfcp_qdio *qdio)
651
{
652
	struct zfcp_adapter *adapter = qdio->adapter;
653
	long ret;
654

655 656 657
	spin_unlock_bh(&qdio->req_q_lock);
	ret = wait_event_interruptible_timeout(qdio->req_q_wq,
			       zfcp_fsf_sbal_check(qdio), 5 * HZ);
S
Swen Schillig 已提交
658 659
	if (ret > 0)
		return 0;
660
	if (!ret) {
661
		atomic_inc(&qdio->req_q_full);
662 663 664
		/* assume hanging outbound queue, try queue recovery */
		zfcp_erp_adapter_reopen(adapter, 0, "fsrsg_1", NULL);
	}
665

666
	spin_lock_bh(&qdio->req_q_lock);
S
Swen Schillig 已提交
667 668 669
	return -EIO;
}

670
static struct zfcp_fsf_req *zfcp_fsf_alloc(mempool_t *pool)
S
Swen Schillig 已提交
671 672
{
	struct zfcp_fsf_req *req;
673 674 675 676 677 678 679

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

	if (unlikely(!req))
S
Swen Schillig 已提交
680
		return NULL;
681

S
Swen Schillig 已提交
682
	memset(req, 0, sizeof(*req));
683
	req->pool = pool;
S
Swen Schillig 已提交
684 685 686
	return req;
}

687
static struct fsf_qtcb *zfcp_qtcb_alloc(mempool_t *pool)
S
Swen Schillig 已提交
688
{
689
	struct fsf_qtcb *qtcb;
S
Swen Schillig 已提交
690 691 692 693

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

S
Swen Schillig 已提交
696 697 698 699
	if (unlikely(!qtcb))
		return NULL;

	memset(qtcb, 0, sizeof(*qtcb));
700
	return qtcb;
S
Swen Schillig 已提交
701 702
}

703
static struct zfcp_fsf_req *zfcp_fsf_req_create(struct zfcp_qdio *qdio,
704
						u32 fsf_cmd, mempool_t *pool)
L
Linus Torvalds 已提交
705
{
706
	struct qdio_buffer_element *sbale;
707 708
	struct zfcp_qdio_queue *req_q = &qdio->req_q;
	struct zfcp_adapter *adapter = qdio->adapter;
709
	struct zfcp_fsf_req *req = zfcp_fsf_alloc(pool);
L
Linus Torvalds 已提交
710

S
Swen Schillig 已提交
711
	if (unlikely(!req))
712
		return ERR_PTR(-ENOMEM);
L
Linus Torvalds 已提交
713

S
Swen Schillig 已提交
714 715
	if (adapter->req_no == 0)
		adapter->req_no++;
L
Linus Torvalds 已提交
716

S
Swen Schillig 已提交
717 718
	INIT_LIST_HEAD(&req->list);
	init_timer(&req->timer);
719
	init_completion(&req->completion);
L
Linus Torvalds 已提交
720

S
Swen Schillig 已提交
721 722
	req->adapter = adapter;
	req->fsf_command = fsf_cmd;
723
	req->req_id = adapter->req_no;
724 725 726 727
	req->queue_req.sbal_number = 1;
	req->queue_req.sbal_first = req_q->first;
	req->queue_req.sbal_last = req_q->first;
	req->queue_req.sbale_curr = 1;
S
Swen Schillig 已提交
728

729
	sbale = zfcp_qdio_sbale_req(qdio, &req->queue_req);
S
Swen Schillig 已提交
730 731 732
	sbale[0].addr = (void *) req->req_id;
	sbale[0].flags |= SBAL_FLAGS0_COMMAND;

733 734 735 736 737 738 739 740 741 742 743
	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);
		}

744
		req->seq_no = adapter->fsf_req_seq_no;
745
		req->qtcb->prefix.req_seq_no = adapter->fsf_req_seq_no;
S
Swen Schillig 已提交
746 747 748 749 750 751 752 753 754 755 756 757 758 759
		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;
		sbale[1].addr = (void *) req->qtcb;
		sbale[1].length = sizeof(struct fsf_qtcb);
	}

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

S
Swen Schillig 已提交
761
	return req;
L
Linus Torvalds 已提交
762 763
}

S
Swen Schillig 已提交
764 765 766
static int zfcp_fsf_req_send(struct zfcp_fsf_req *req)
{
	struct zfcp_adapter *adapter = req->adapter;
767
	struct zfcp_qdio *qdio = adapter->qdio;
768
	int with_qtcb = (req->qtcb != NULL);
769
	int req_id = req->req_id;
S
Swen Schillig 已提交
770

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

773
	req->queue_req.qdio_outb_usage = atomic_read(&qdio->req_q.count);
S
Swen Schillig 已提交
774
	req->issued = get_clock();
775
	if (zfcp_qdio_send(qdio, &req->queue_req)) {
S
Swen Schillig 已提交
776
		del_timer(&req->timer);
777
		/* lookup request again, list might have changed */
778
		zfcp_reqlist_find_rm(adapter->req_list, req_id);
779
		zfcp_erp_adapter_reopen(adapter, 0, "fsrs__1", req);
S
Swen Schillig 已提交
780 781 782 783
		return -EIO;
	}

	/* Don't increase for unsolicited status */
784
	if (with_qtcb)
S
Swen Schillig 已提交
785
		adapter->fsf_req_seq_no++;
786
	adapter->req_no++;
S
Swen Schillig 已提交
787 788 789 790 791 792 793 794 795

	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 已提交
796
 */
797
int zfcp_fsf_status_read(struct zfcp_qdio *qdio)
L
Linus Torvalds 已提交
798
{
799
	struct zfcp_adapter *adapter = qdio->adapter;
S
Swen Schillig 已提交
800 801
	struct zfcp_fsf_req *req;
	struct fsf_status_read_buffer *sr_buf;
802
	struct qdio_buffer_element *sbale;
S
Swen Schillig 已提交
803
	int retval = -EIO;
L
Linus Torvalds 已提交
804

805 806
	spin_lock_bh(&qdio->req_q_lock);
	if (zfcp_fsf_req_sbal_get(qdio))
S
Swen Schillig 已提交
807 808
		goto out;

809
	req = zfcp_fsf_req_create(qdio, FSF_QTCB_UNSOLICITED_STATUS,
810
				  adapter->pool.status_read_req);
811
	if (IS_ERR(req)) {
S
Swen Schillig 已提交
812 813
		retval = PTR_ERR(req);
		goto out;
L
Linus Torvalds 已提交
814 815
	}

816
	sbale = zfcp_qdio_sbale_req(qdio, &req->queue_req);
S
Swen Schillig 已提交
817
	sbale[2].flags |= SBAL_FLAGS_LAST_ENTRY;
818
	req->queue_req.sbale_curr = 2;
S
Swen Schillig 已提交
819

820
	sr_buf = mempool_alloc(adapter->pool.status_read_data, GFP_ATOMIC);
S
Swen Schillig 已提交
821 822 823 824 825 826
	if (!sr_buf) {
		retval = -ENOMEM;
		goto failed_buf;
	}
	memset(sr_buf, 0, sizeof(*sr_buf));
	req->data = sr_buf;
827
	sbale = zfcp_qdio_sbale_curr(qdio, &req->queue_req);
S
Swen Schillig 已提交
828 829
	sbale->addr = (void *) sr_buf;
	sbale->length = sizeof(*sr_buf);
830

S
Swen Schillig 已提交
831 832 833
	retval = zfcp_fsf_req_send(req);
	if (retval)
		goto failed_req_send;
L
Linus Torvalds 已提交
834

S
Swen Schillig 已提交
835 836 837
	goto out;

failed_req_send:
838
	mempool_free(sr_buf, adapter->pool.status_read_data);
S
Swen Schillig 已提交
839 840
failed_buf:
	zfcp_fsf_req_free(req);
S
Swen Schillig 已提交
841
	zfcp_dbf_hba_fsf_unsol("fail", adapter->dbf, NULL);
S
Swen Schillig 已提交
842
out:
843
	spin_unlock_bh(&qdio->req_q_lock);
S
Swen Schillig 已提交
844 845 846 847 848 849 850 851 852 853 854 855
	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 已提交
856
	case FSF_PORT_HANDLE_NOT_VALID:
S
Swen Schillig 已提交
857
		if (fsq->word[0] == fsq->word[1]) {
858 859
			zfcp_erp_adapter_reopen(unit->port->adapter, 0,
						"fsafch1", req);
S
Swen Schillig 已提交
860
			req->status |= ZFCP_STATUS_FSFREQ_ERROR;
L
Linus Torvalds 已提交
861 862 863
		}
		break;
	case FSF_LUN_HANDLE_NOT_VALID:
S
Swen Schillig 已提交
864
		if (fsq->word[0] == fsq->word[1]) {
865
			zfcp_erp_port_reopen(unit->port, 0, "fsafch2", req);
S
Swen Schillig 已提交
866
			req->status |= ZFCP_STATUS_FSFREQ_ERROR;
L
Linus Torvalds 已提交
867 868 869
		}
		break;
	case FSF_FCP_COMMAND_DOES_NOT_EXIST:
S
Swen Schillig 已提交
870
		req->status |= ZFCP_STATUS_FSFREQ_ABORTNOTNEEDED;
L
Linus Torvalds 已提交
871 872
		break;
	case FSF_PORT_BOXED:
873
		zfcp_erp_port_boxed(unit->port, "fsafch3", req);
874
		req->status |= ZFCP_STATUS_FSFREQ_ERROR;
L
Linus Torvalds 已提交
875 876
		break;
	case FSF_LUN_BOXED:
877
		zfcp_erp_unit_boxed(unit, "fsafch4", req);
878
		req->status |= ZFCP_STATUS_FSFREQ_ERROR;
L
Linus Torvalds 已提交
879 880
                break;
	case FSF_ADAPTER_STATUS_AVAILABLE:
S
Swen Schillig 已提交
881
		switch (fsq->word[0]) {
L
Linus Torvalds 已提交
882
		case FSF_SQ_INVOKE_LINK_TEST_PROCEDURE:
883
			zfcp_fc_test_link(unit->port);
884
			/* fall through */
L
Linus Torvalds 已提交
885
		case FSF_SQ_ULP_DEPENDENT_ERP_REQUIRED:
S
Swen Schillig 已提交
886
			req->status |= ZFCP_STATUS_FSFREQ_ERROR;
L
Linus Torvalds 已提交
887 888 889 890
			break;
		}
		break;
	case FSF_GOOD:
S
Swen Schillig 已提交
891
		req->status |= ZFCP_STATUS_FSFREQ_ABORTSUCCEEDED;
L
Linus Torvalds 已提交
892 893 894 895 896
		break;
	}
}

/**
S
Swen Schillig 已提交
897 898 899 900
 * 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 已提交
901 902
 */

S
Swen Schillig 已提交
903
struct zfcp_fsf_req *zfcp_fsf_abort_fcp_command(unsigned long old_req_id,
904
						struct zfcp_unit *unit)
L
Linus Torvalds 已提交
905
{
906
	struct qdio_buffer_element *sbale;
S
Swen Schillig 已提交
907
	struct zfcp_fsf_req *req = NULL;
908
	struct zfcp_qdio *qdio = unit->port->adapter->qdio;
909

910 911
	spin_lock_bh(&qdio->req_q_lock);
	if (zfcp_fsf_req_sbal_get(qdio))
S
Swen Schillig 已提交
912
		goto out;
913 914
	req = zfcp_fsf_req_create(qdio, FSF_QTCB_ABORT_FCP_CMND,
				  qdio->adapter->pool.scsi_abort);
915 916
	if (IS_ERR(req)) {
		req = NULL;
S
Swen Schillig 已提交
917
		goto out;
918
	}
919

S
Swen Schillig 已提交
920 921 922
	if (unlikely(!(atomic_read(&unit->status) &
		       ZFCP_STATUS_COMMON_UNBLOCKED)))
		goto out_error_free;
L
Linus Torvalds 已提交
923

924
	sbale = zfcp_qdio_sbale_req(qdio, &req->queue_req);
S
Swen Schillig 已提交
925 926
	sbale[0].flags |= SBAL_FLAGS0_TYPE_READ;
	sbale[1].flags |= SBAL_FLAGS_LAST_ENTRY;
L
Linus Torvalds 已提交
927

S
Swen Schillig 已提交
928 929 930 931 932 933 934 935 936 937 938 939 940 941
	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:
942
	spin_unlock_bh(&qdio->req_q_lock);
S
Swen Schillig 已提交
943
	return req;
L
Linus Torvalds 已提交
944 945
}

S
Swen Schillig 已提交
946
static void zfcp_fsf_send_ct_handler(struct zfcp_fsf_req *req)
L
Linus Torvalds 已提交
947
{
S
Swen Schillig 已提交
948
	struct zfcp_adapter *adapter = req->adapter;
949
	struct zfcp_fsf_ct_els *ct = req->data;
S
Swen Schillig 已提交
950
	struct fsf_qtcb_header *header = &req->qtcb->header;
L
Linus Torvalds 已提交
951

952
	ct->status = -EINVAL;
L
Linus Torvalds 已提交
953

S
Swen Schillig 已提交
954
	if (req->status & ZFCP_STATUS_FSFREQ_ERROR)
L
Linus Torvalds 已提交
955 956 957 958
		goto skip_fsfstatus;

	switch (header->fsf_status) {
        case FSF_GOOD:
S
Swen Schillig 已提交
959
		zfcp_dbf_san_ct_response(req);
960
		ct->status = 0;
L
Linus Torvalds 已提交
961 962
		break;
        case FSF_SERVICE_CLASS_NOT_SUPPORTED:
S
Swen Schillig 已提交
963
		zfcp_fsf_class_not_supp(req);
L
Linus Torvalds 已提交
964 965 966 967 968
		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 已提交
969
			req->status |= ZFCP_STATUS_FSFREQ_ERROR;
L
Linus Torvalds 已提交
970 971 972 973 974 975
			break;
                }
                break;
	case FSF_ACCESS_DENIED:
		break;
        case FSF_PORT_BOXED:
976
		req->status |= ZFCP_STATUS_FSFREQ_ERROR;
L
Linus Torvalds 已提交
977
		break;
S
Swen Schillig 已提交
978
	case FSF_PORT_HANDLE_NOT_VALID:
979
		zfcp_erp_adapter_reopen(adapter, 0, "fsscth1", req);
980
		/* fall through */
S
Swen Schillig 已提交
981
	case FSF_GENERIC_COMMAND_REJECTED:
L
Linus Torvalds 已提交
982 983 984 985
	case FSF_PAYLOAD_SIZE_MISMATCH:
	case FSF_REQUEST_SIZE_TOO_LARGE:
	case FSF_RESPONSE_SIZE_TOO_LARGE:
	case FSF_SBAL_MISMATCH:
S
Swen Schillig 已提交
986
		req->status |= ZFCP_STATUS_FSFREQ_ERROR;
L
Linus Torvalds 已提交
987 988 989 990
		break;
	}

skip_fsfstatus:
991 992
	if (ct->handler)
		ct->handler(ct->handler_data);
S
Swen Schillig 已提交
993
}
L
Linus Torvalds 已提交
994

995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011
static void zfcp_fsf_setup_ct_els_unchained(struct qdio_buffer_element *sbale,
					    struct scatterlist *sg_req,
					    struct scatterlist *sg_resp)
{
	sbale[0].flags |= SBAL_FLAGS0_TYPE_WRITE_READ;
	sbale[2].addr   = sg_virt(sg_req);
	sbale[2].length = sg_req->length;
	sbale[3].addr   = sg_virt(sg_resp);
	sbale[3].length = sg_resp->length;
	sbale[3].flags |= SBAL_FLAGS_LAST_ENTRY;
}

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

1012 1013 1014 1015
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 已提交
1016
{
1017
	struct zfcp_adapter *adapter = req->adapter;
1018
	struct qdio_buffer_element *sbale = zfcp_qdio_sbale_req(adapter->qdio,
1019 1020
							       &req->queue_req);
	u32 feat = adapter->adapter_features;
S
Swen Schillig 已提交
1021 1022
	int bytes;

1023
	if (!(feat & FSF_FEATURE_ELS_CT_CHAINED_SBALS)) {
1024
		if (!zfcp_fsf_one_sbal(sg_req) || !zfcp_fsf_one_sbal(sg_resp))
1025 1026
			return -EOPNOTSUPP;

1027 1028 1029 1030 1031 1032 1033
		zfcp_fsf_setup_ct_els_unchained(sbale, sg_req, sg_resp);
		return 0;
	}

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

1037
	bytes = zfcp_qdio_sbals_from_sg(adapter->qdio, &req->queue_req,
1038
					SBAL_FLAGS0_TYPE_WRITE_READ,
S
Swen Schillig 已提交
1039 1040
					sg_req, max_sbals);
	if (bytes <= 0)
1041
		return -EIO;
S
Swen Schillig 已提交
1042
	req->qtcb->bottom.support.req_buf_length = bytes;
1043
	req->queue_req.sbale_curr = ZFCP_LAST_SBALE_PER_SBAL;
S
Swen Schillig 已提交
1044

1045
	bytes = zfcp_qdio_sbals_from_sg(adapter->qdio, &req->queue_req,
1046
					SBAL_FLAGS0_TYPE_WRITE_READ,
S
Swen Schillig 已提交
1047
					sg_resp, max_sbals);
1048
	req->qtcb->bottom.support.resp_buf_length = bytes;
S
Swen Schillig 已提交
1049
	if (bytes <= 0)
1050
		return -EIO;
1051

1052 1053 1054 1055 1056 1057
	return 0;
}

static int zfcp_fsf_setup_ct_els(struct zfcp_fsf_req *req,
				 struct scatterlist *sg_req,
				 struct scatterlist *sg_resp,
1058
				 int max_sbals, unsigned int timeout)
1059 1060 1061 1062 1063 1064 1065
{
	int ret;

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

1066
	/* common settings for ct/gs and els requests */
1067 1068
	if (timeout > 255)
		timeout = 255; /* max value accepted by hardware */
1069
	req->qtcb->bottom.support.service_class = FSF_CLASS_3;
1070 1071
	req->qtcb->bottom.support.timeout = timeout;
	zfcp_fsf_start_timer(req, (timeout + 10) * HZ);
S
Swen Schillig 已提交
1072 1073

	return 0;
L
Linus Torvalds 已提交
1074 1075 1076
}

/**
S
Swen Schillig 已提交
1077 1078 1079
 * 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 已提交
1080
 */
1081
int zfcp_fsf_send_ct(struct zfcp_fc_wka_port *wka_port,
1082 1083
		     struct zfcp_fsf_ct_els *ct, mempool_t *pool,
		     unsigned int timeout)
L
Linus Torvalds 已提交
1084
{
1085
	struct zfcp_qdio *qdio = wka_port->adapter->qdio;
S
Swen Schillig 已提交
1086 1087
	struct zfcp_fsf_req *req;
	int ret = -EIO;
L
Linus Torvalds 已提交
1088

1089 1090
	spin_lock_bh(&qdio->req_q_lock);
	if (zfcp_fsf_req_sbal_get(qdio))
S
Swen Schillig 已提交
1091
		goto out;
L
Linus Torvalds 已提交
1092

1093
	req = zfcp_fsf_req_create(qdio, FSF_QTCB_SEND_GENERIC, pool);
1094

1095
	if (IS_ERR(req)) {
S
Swen Schillig 已提交
1096 1097
		ret = PTR_ERR(req);
		goto out;
1098 1099
	}

1100
	req->status |= ZFCP_STATUS_FSFREQ_CLEANUP;
1101
	ret = zfcp_fsf_setup_ct_els(req, ct->req, ct->resp,
1102
				    FSF_MAX_SBALS_PER_REQ, timeout);
C
Christof Schmitt 已提交
1103
	if (ret)
L
Linus Torvalds 已提交
1104 1105
		goto failed_send;

S
Swen Schillig 已提交
1106
	req->handler = zfcp_fsf_send_ct_handler;
1107
	req->qtcb->header.port_handle = wka_port->handle;
S
Swen Schillig 已提交
1108 1109
	req->data = ct;

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

S
Swen Schillig 已提交
1112 1113 1114
	ret = zfcp_fsf_req_send(req);
	if (ret)
		goto failed_send;
L
Linus Torvalds 已提交
1115

S
Swen Schillig 已提交
1116
	goto out;
L
Linus Torvalds 已提交
1117

S
Swen Schillig 已提交
1118 1119 1120
failed_send:
	zfcp_fsf_req_free(req);
out:
1121
	spin_unlock_bh(&qdio->req_q_lock);
S
Swen Schillig 已提交
1122
	return ret;
L
Linus Torvalds 已提交
1123 1124
}

S
Swen Schillig 已提交
1125
static void zfcp_fsf_send_els_handler(struct zfcp_fsf_req *req)
L
Linus Torvalds 已提交
1126
{
1127
	struct zfcp_fsf_ct_els *send_els = req->data;
S
Swen Schillig 已提交
1128 1129
	struct zfcp_port *port = send_els->port;
	struct fsf_qtcb_header *header = &req->qtcb->header;
L
Linus Torvalds 已提交
1130

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

S
Swen Schillig 已提交
1133
	if (req->status & ZFCP_STATUS_FSFREQ_ERROR)
L
Linus Torvalds 已提交
1134 1135 1136 1137
		goto skip_fsfstatus;

	switch (header->fsf_status) {
	case FSF_GOOD:
S
Swen Schillig 已提交
1138
		zfcp_dbf_san_els_response(req);
S
Swen Schillig 已提交
1139
		send_els->status = 0;
L
Linus Torvalds 已提交
1140 1141
		break;
	case FSF_SERVICE_CLASS_NOT_SUPPORTED:
S
Swen Schillig 已提交
1142
		zfcp_fsf_class_not_supp(req);
L
Linus Torvalds 已提交
1143 1144 1145 1146 1147 1148
		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 已提交
1149
			req->status |= ZFCP_STATUS_FSFREQ_ERROR;
L
Linus Torvalds 已提交
1150 1151 1152 1153 1154 1155 1156 1157 1158
			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:
1159 1160
		if (port)
			zfcp_fsf_access_denied_port(req, port);
L
Linus Torvalds 已提交
1161
		break;
S
Swen Schillig 已提交
1162 1163 1164
	case FSF_SBAL_MISMATCH:
		/* should never occure, avoided in zfcp_fsf_send_els */
		/* fall through */
L
Linus Torvalds 已提交
1165
	default:
S
Swen Schillig 已提交
1166
		req->status |= ZFCP_STATUS_FSFREQ_ERROR;
L
Linus Torvalds 已提交
1167 1168 1169
		break;
	}
skip_fsfstatus:
H
Heiko Carstens 已提交
1170
	if (send_els->handler)
L
Linus Torvalds 已提交
1171
		send_els->handler(send_els->handler_data);
S
Swen Schillig 已提交
1172
}
L
Linus Torvalds 已提交
1173

S
Swen Schillig 已提交
1174 1175 1176 1177
/**
 * zfcp_fsf_send_els - initiate an ELS command (FC-FS)
 * @els: pointer to struct zfcp_send_els with data for the command
 */
1178
int zfcp_fsf_send_els(struct zfcp_adapter *adapter, u32 d_id,
1179
		      struct zfcp_fsf_ct_els *els, unsigned int timeout)
S
Swen Schillig 已提交
1180 1181
{
	struct zfcp_fsf_req *req;
1182
	struct zfcp_qdio *qdio = adapter->qdio;
S
Swen Schillig 已提交
1183 1184
	int ret = -EIO;

1185 1186
	spin_lock_bh(&qdio->req_q_lock);
	if (zfcp_fsf_req_sbal_get(qdio))
S
Swen Schillig 已提交
1187
		goto out;
1188

1189
	req = zfcp_fsf_req_create(qdio, FSF_QTCB_SEND_ELS, NULL);
1190

1191
	if (IS_ERR(req)) {
S
Swen Schillig 已提交
1192 1193 1194 1195
		ret = PTR_ERR(req);
		goto out;
	}

1196
	req->status |= ZFCP_STATUS_FSFREQ_CLEANUP;
1197
	ret = zfcp_fsf_setup_ct_els(req, els->req, els->resp, 2, timeout);
1198

S
Swen Schillig 已提交
1199 1200 1201
	if (ret)
		goto failed_send;

1202
	hton24(req->qtcb->bottom.support.d_id, d_id);
S
Swen Schillig 已提交
1203 1204 1205
	req->handler = zfcp_fsf_send_els_handler;
	req->data = els;

S
Swen Schillig 已提交
1206
	zfcp_dbf_san_els_request(req);
S
Swen Schillig 已提交
1207 1208 1209 1210 1211 1212 1213 1214 1215 1216

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

	goto out;

failed_send:
	zfcp_fsf_req_free(req);
out:
1217
	spin_unlock_bh(&qdio->req_q_lock);
S
Swen Schillig 已提交
1218
	return ret;
L
Linus Torvalds 已提交
1219 1220
}

S
Swen Schillig 已提交
1221
int zfcp_fsf_exchange_config_data(struct zfcp_erp_action *erp_action)
L
Linus Torvalds 已提交
1222
{
1223
	struct qdio_buffer_element *sbale;
S
Swen Schillig 已提交
1224
	struct zfcp_fsf_req *req;
1225
	struct zfcp_qdio *qdio = erp_action->adapter->qdio;
S
Swen Schillig 已提交
1226 1227
	int retval = -EIO;

1228 1229
	spin_lock_bh(&qdio->req_q_lock);
	if (zfcp_fsf_req_sbal_get(qdio))
S
Swen Schillig 已提交
1230
		goto out;
1231

1232 1233
	req = zfcp_fsf_req_create(qdio, FSF_QTCB_EXCHANGE_CONFIG_DATA,
				  qdio->adapter->pool.erp_req);
1234

1235
	if (IS_ERR(req)) {
S
Swen Schillig 已提交
1236 1237
		retval = PTR_ERR(req);
		goto out;
L
Linus Torvalds 已提交
1238 1239
	}

1240
	req->status |= ZFCP_STATUS_FSFREQ_CLEANUP;
1241
	sbale = zfcp_qdio_sbale_req(qdio, &req->queue_req);
1242 1243
	sbale[0].flags |= SBAL_FLAGS0_TYPE_READ;
	sbale[1].flags |= SBAL_FLAGS_LAST_ENTRY;
L
Linus Torvalds 已提交
1244

S
Swen Schillig 已提交
1245
	req->qtcb->bottom.config.feature_selection =
1246 1247
			FSF_FEATURE_CFDC |
			FSF_FEATURE_LUN_SHARING |
1248
			FSF_FEATURE_NOTIFICATION_LOST |
1249
			FSF_FEATURE_UPDATE_ALERT;
S
Swen Schillig 已提交
1250 1251
	req->erp_action = erp_action;
	req->handler = zfcp_fsf_exchange_config_data_handler;
1252
	erp_action->fsf_req_id = req->req_id;
L
Linus Torvalds 已提交
1253

1254
	zfcp_fsf_start_erp_timer(req);
S
Swen Schillig 已提交
1255
	retval = zfcp_fsf_req_send(req);
L
Linus Torvalds 已提交
1256
	if (retval) {
S
Swen Schillig 已提交
1257
		zfcp_fsf_req_free(req);
1258
		erp_action->fsf_req_id = 0;
L
Linus Torvalds 已提交
1259
	}
S
Swen Schillig 已提交
1260
out:
1261
	spin_unlock_bh(&qdio->req_q_lock);
1262 1263
	return retval;
}
L
Linus Torvalds 已提交
1264

1265
int zfcp_fsf_exchange_config_data_sync(struct zfcp_qdio *qdio,
S
Swen Schillig 已提交
1266
				       struct fsf_qtcb_bottom_config *data)
1267
{
1268
	struct qdio_buffer_element *sbale;
S
Swen Schillig 已提交
1269 1270 1271
	struct zfcp_fsf_req *req = NULL;
	int retval = -EIO;

1272 1273
	spin_lock_bh(&qdio->req_q_lock);
	if (zfcp_fsf_req_sbal_get(qdio))
1274
		goto out_unlock;
S
Swen Schillig 已提交
1275

1276
	req = zfcp_fsf_req_create(qdio, FSF_QTCB_EXCHANGE_CONFIG_DATA, NULL);
1277

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

1283
	sbale = zfcp_qdio_sbale_req(qdio, &req->queue_req);
1284 1285
	sbale[0].flags |= SBAL_FLAGS0_TYPE_READ;
	sbale[1].flags |= SBAL_FLAGS_LAST_ENTRY;
S
Swen Schillig 已提交
1286
	req->handler = zfcp_fsf_exchange_config_data_handler;
1287

S
Swen Schillig 已提交
1288
	req->qtcb->bottom.config.feature_selection =
1289 1290 1291 1292 1293 1294
			FSF_FEATURE_CFDC |
			FSF_FEATURE_LUN_SHARING |
			FSF_FEATURE_NOTIFICATION_LOST |
			FSF_FEATURE_UPDATE_ALERT;

	if (data)
S
Swen Schillig 已提交
1295
		req->data = data;
1296

S
Swen Schillig 已提交
1297 1298
	zfcp_fsf_start_timer(req, ZFCP_FSF_REQUEST_TIMEOUT);
	retval = zfcp_fsf_req_send(req);
1299
	spin_unlock_bh(&qdio->req_q_lock);
C
Christof Schmitt 已提交
1300
	if (!retval)
1301
		wait_for_completion(&req->completion);
1302

S
Swen Schillig 已提交
1303
	zfcp_fsf_req_free(req);
1304
	return retval;
1305

1306
out_unlock:
1307
	spin_unlock_bh(&qdio->req_q_lock);
L
Linus Torvalds 已提交
1308 1309 1310 1311 1312
	return retval;
}

/**
 * zfcp_fsf_exchange_port_data - request information about local port
1313
 * @erp_action: ERP action for the adapter for which port data is requested
S
Swen Schillig 已提交
1314
 * Returns: 0 on success, error otherwise
L
Linus Torvalds 已提交
1315
 */
S
Swen Schillig 已提交
1316
int zfcp_fsf_exchange_port_data(struct zfcp_erp_action *erp_action)
L
Linus Torvalds 已提交
1317
{
1318
	struct zfcp_qdio *qdio = erp_action->adapter->qdio;
1319
	struct qdio_buffer_element *sbale;
S
Swen Schillig 已提交
1320 1321
	struct zfcp_fsf_req *req;
	int retval = -EIO;
L
Linus Torvalds 已提交
1322

1323
	if (!(qdio->adapter->adapter_features & FSF_FEATURE_HBAAPI_MANAGEMENT))
1324
		return -EOPNOTSUPP;
L
Linus Torvalds 已提交
1325

1326 1327
	spin_lock_bh(&qdio->req_q_lock);
	if (zfcp_fsf_req_sbal_get(qdio))
S
Swen Schillig 已提交
1328
		goto out;
1329

1330 1331
	req = zfcp_fsf_req_create(qdio, FSF_QTCB_EXCHANGE_PORT_DATA,
				  qdio->adapter->pool.erp_req);
1332

1333
	if (IS_ERR(req)) {
S
Swen Schillig 已提交
1334 1335
		retval = PTR_ERR(req);
		goto out;
1336 1337
	}

1338
	req->status |= ZFCP_STATUS_FSFREQ_CLEANUP;
1339
	sbale = zfcp_qdio_sbale_req(qdio, &req->queue_req);
1340 1341
	sbale[0].flags |= SBAL_FLAGS0_TYPE_READ;
	sbale[1].flags |= SBAL_FLAGS_LAST_ENTRY;
L
Linus Torvalds 已提交
1342

S
Swen Schillig 已提交
1343 1344
	req->handler = zfcp_fsf_exchange_port_data_handler;
	req->erp_action = erp_action;
1345
	erp_action->fsf_req_id = req->req_id;
1346

1347
	zfcp_fsf_start_erp_timer(req);
S
Swen Schillig 已提交
1348
	retval = zfcp_fsf_req_send(req);
L
Linus Torvalds 已提交
1349
	if (retval) {
S
Swen Schillig 已提交
1350
		zfcp_fsf_req_free(req);
1351
		erp_action->fsf_req_id = 0;
1352
	}
S
Swen Schillig 已提交
1353
out:
1354
	spin_unlock_bh(&qdio->req_q_lock);
1355 1356 1357 1358 1359
	return retval;
}

/**
 * zfcp_fsf_exchange_port_data_sync - request information about local port
1360
 * @qdio: pointer to struct zfcp_qdio
S
Swen Schillig 已提交
1361 1362
 * @data: pointer to struct fsf_qtcb_bottom_port
 * Returns: 0 on success, error otherwise
1363
 */
1364
int zfcp_fsf_exchange_port_data_sync(struct zfcp_qdio *qdio,
S
Swen Schillig 已提交
1365
				     struct fsf_qtcb_bottom_port *data)
1366
{
1367
	struct qdio_buffer_element *sbale;
S
Swen Schillig 已提交
1368 1369
	struct zfcp_fsf_req *req = NULL;
	int retval = -EIO;
1370

1371
	if (!(qdio->adapter->adapter_features & FSF_FEATURE_HBAAPI_MANAGEMENT))
1372 1373
		return -EOPNOTSUPP;

1374 1375
	spin_lock_bh(&qdio->req_q_lock);
	if (zfcp_fsf_req_sbal_get(qdio))
1376
		goto out_unlock;
S
Swen Schillig 已提交
1377

1378
	req = zfcp_fsf_req_create(qdio, FSF_QTCB_EXCHANGE_PORT_DATA, NULL);
1379

1380
	if (IS_ERR(req)) {
S
Swen Schillig 已提交
1381
		retval = PTR_ERR(req);
1382
		goto out_unlock;
L
Linus Torvalds 已提交
1383 1384
	}

1385
	if (data)
S
Swen Schillig 已提交
1386
		req->data = data;
1387

1388
	sbale = zfcp_qdio_sbale_req(qdio, &req->queue_req);
1389 1390 1391
	sbale[0].flags |= SBAL_FLAGS0_TYPE_READ;
	sbale[1].flags |= SBAL_FLAGS_LAST_ENTRY;

S
Swen Schillig 已提交
1392 1393 1394
	req->handler = zfcp_fsf_exchange_port_data_handler;
	zfcp_fsf_start_timer(req, ZFCP_FSF_REQUEST_TIMEOUT);
	retval = zfcp_fsf_req_send(req);
1395
	spin_unlock_bh(&qdio->req_q_lock);
1396

C
Christof Schmitt 已提交
1397
	if (!retval)
1398 1399
		wait_for_completion(&req->completion);

S
Swen Schillig 已提交
1400
	zfcp_fsf_req_free(req);
L
Linus Torvalds 已提交
1401 1402

	return retval;
1403 1404

out_unlock:
1405
	spin_unlock_bh(&qdio->req_q_lock);
1406
	return retval;
L
Linus Torvalds 已提交
1407 1408
}

S
Swen Schillig 已提交
1409
static void zfcp_fsf_open_port_handler(struct zfcp_fsf_req *req)
L
Linus Torvalds 已提交
1410
{
S
Swen Schillig 已提交
1411 1412
	struct zfcp_port *port = req->data;
	struct fsf_qtcb_header *header = &req->qtcb->header;
1413
	struct fc_els_flogi *plogi;
L
Linus Torvalds 已提交
1414

S
Swen Schillig 已提交
1415
	if (req->status & ZFCP_STATUS_FSFREQ_ERROR)
1416
		goto out;
L
Linus Torvalds 已提交
1417 1418 1419 1420 1421

	switch (header->fsf_status) {
	case FSF_PORT_ALREADY_OPEN:
		break;
	case FSF_ACCESS_DENIED:
S
Swen Schillig 已提交
1422
		zfcp_fsf_access_denied_port(req, port);
L
Linus Torvalds 已提交
1423 1424
		break;
	case FSF_MAXIMUM_NUMBER_OF_PORTS_EXCEEDED:
S
Swen Schillig 已提交
1425
		dev_warn(&req->adapter->ccw_device->dev,
1426
			 "Not enough FCP adapter resources to open "
1427 1428
			 "remote port 0x%016Lx\n",
			 (unsigned long long)port->wwpn);
1429
		zfcp_erp_port_failed(port, "fsoph_1", req);
S
Swen Schillig 已提交
1430
		req->status |= ZFCP_STATUS_FSFREQ_ERROR;
L
Linus Torvalds 已提交
1431 1432 1433 1434 1435 1436
		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 已提交
1437
			req->status |= ZFCP_STATUS_FSFREQ_ERROR;
L
Linus Torvalds 已提交
1438 1439 1440 1441 1442 1443 1444
			break;
		}
		break;
	case FSF_GOOD:
		port->handle = header->port_handle;
		atomic_set_mask(ZFCP_STATUS_COMMON_OPEN |
				ZFCP_STATUS_PORT_PHYS_OPEN, &port->status);
1445 1446 1447
		atomic_clear_mask(ZFCP_STATUS_COMMON_ACCESS_DENIED |
		                  ZFCP_STATUS_COMMON_ACCESS_BOXED,
		                  &port->status);
L
Linus Torvalds 已提交
1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462
		/* 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.
		 */
1463
		plogi = (struct fc_els_flogi *) req->qtcb->bottom.support.els;
1464
		if (req->qtcb->bottom.support.els1_length >=
1465
		    FSF_PLOGI_MIN_LEN)
S
Swen Schillig 已提交
1466
				zfcp_fc_plogi_evaluate(port, plogi);
L
Linus Torvalds 已提交
1467 1468
		break;
	case FSF_UNKNOWN_OP_SUBTYPE:
S
Swen Schillig 已提交
1469
		req->status |= ZFCP_STATUS_FSFREQ_ERROR;
L
Linus Torvalds 已提交
1470 1471
		break;
	}
1472 1473

out:
1474
	put_device(&port->dev);
L
Linus Torvalds 已提交
1475 1476
}

S
Swen Schillig 已提交
1477 1478 1479 1480
/**
 * 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 已提交
1481
 */
S
Swen Schillig 已提交
1482
int zfcp_fsf_open_port(struct zfcp_erp_action *erp_action)
L
Linus Torvalds 已提交
1483
{
1484
	struct qdio_buffer_element *sbale;
1485
	struct zfcp_qdio *qdio = erp_action->adapter->qdio;
1486
	struct zfcp_port *port = erp_action->port;
1487
	struct zfcp_fsf_req *req;
S
Swen Schillig 已提交
1488 1489
	int retval = -EIO;

1490 1491
	spin_lock_bh(&qdio->req_q_lock);
	if (zfcp_fsf_req_sbal_get(qdio))
S
Swen Schillig 已提交
1492 1493
		goto out;

1494 1495
	req = zfcp_fsf_req_create(qdio, FSF_QTCB_OPEN_PORT_WITH_DID,
				  qdio->adapter->pool.erp_req);
1496

1497
	if (IS_ERR(req)) {
S
Swen Schillig 已提交
1498
		retval = PTR_ERR(req);
L
Linus Torvalds 已提交
1499
		goto out;
S
Swen Schillig 已提交
1500
	}
L
Linus Torvalds 已提交
1501

1502
	req->status |= ZFCP_STATUS_FSFREQ_CLEANUP;
1503
	sbale = zfcp_qdio_sbale_req(qdio, &req->queue_req);
L
Linus Torvalds 已提交
1504 1505 1506
        sbale[0].flags |= SBAL_FLAGS0_TYPE_READ;
        sbale[1].flags |= SBAL_FLAGS_LAST_ENTRY;

S
Swen Schillig 已提交
1507
	req->handler = zfcp_fsf_open_port_handler;
1508
	hton24(req->qtcb->bottom.support.d_id, port->d_id);
1509
	req->data = port;
S
Swen Schillig 已提交
1510
	req->erp_action = erp_action;
1511
	erp_action->fsf_req_id = req->req_id;
1512
	get_device(&port->dev);
S
Swen Schillig 已提交
1513

1514
	zfcp_fsf_start_erp_timer(req);
S
Swen Schillig 已提交
1515
	retval = zfcp_fsf_req_send(req);
L
Linus Torvalds 已提交
1516
	if (retval) {
S
Swen Schillig 已提交
1517
		zfcp_fsf_req_free(req);
1518
		erp_action->fsf_req_id = 0;
1519
		put_device(&port->dev);
L
Linus Torvalds 已提交
1520
	}
S
Swen Schillig 已提交
1521
out:
1522
	spin_unlock_bh(&qdio->req_q_lock);
L
Linus Torvalds 已提交
1523 1524 1525
	return retval;
}

S
Swen Schillig 已提交
1526
static void zfcp_fsf_close_port_handler(struct zfcp_fsf_req *req)
L
Linus Torvalds 已提交
1527
{
S
Swen Schillig 已提交
1528
	struct zfcp_port *port = req->data;
L
Linus Torvalds 已提交
1529

S
Swen Schillig 已提交
1530
	if (req->status & ZFCP_STATUS_FSFREQ_ERROR)
1531
		return;
L
Linus Torvalds 已提交
1532

S
Swen Schillig 已提交
1533
	switch (req->qtcb->header.fsf_status) {
L
Linus Torvalds 已提交
1534
	case FSF_PORT_HANDLE_NOT_VALID:
1535
		zfcp_erp_adapter_reopen(port->adapter, 0, "fscph_1", req);
S
Swen Schillig 已提交
1536
		req->status |= ZFCP_STATUS_FSFREQ_ERROR;
L
Linus Torvalds 已提交
1537 1538 1539 1540
		break;
	case FSF_ADAPTER_STATUS_AVAILABLE:
		break;
	case FSF_GOOD:
1541
		zfcp_erp_modify_port_status(port, "fscph_2", req,
L
Linus Torvalds 已提交
1542 1543 1544 1545 1546 1547
					    ZFCP_STATUS_COMMON_OPEN,
					    ZFCP_CLEAR);
		break;
	}
}

S
Swen Schillig 已提交
1548 1549 1550 1551
/**
 * 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 已提交
1552
 */
S
Swen Schillig 已提交
1553
int zfcp_fsf_close_port(struct zfcp_erp_action *erp_action)
L
Linus Torvalds 已提交
1554
{
1555
	struct qdio_buffer_element *sbale;
1556
	struct zfcp_qdio *qdio = erp_action->adapter->qdio;
S
Swen Schillig 已提交
1557 1558 1559
	struct zfcp_fsf_req *req;
	int retval = -EIO;

1560 1561
	spin_lock_bh(&qdio->req_q_lock);
	if (zfcp_fsf_req_sbal_get(qdio))
S
Swen Schillig 已提交
1562 1563
		goto out;

1564 1565
	req = zfcp_fsf_req_create(qdio, FSF_QTCB_CLOSE_PORT,
				  qdio->adapter->pool.erp_req);
1566

1567
	if (IS_ERR(req)) {
S
Swen Schillig 已提交
1568
		retval = PTR_ERR(req);
L
Linus Torvalds 已提交
1569
		goto out;
S
Swen Schillig 已提交
1570
	}
L
Linus Torvalds 已提交
1571

1572
	req->status |= ZFCP_STATUS_FSFREQ_CLEANUP;
1573
	sbale = zfcp_qdio_sbale_req(qdio, &req->queue_req);
L
Linus Torvalds 已提交
1574 1575 1576
	sbale[0].flags |= SBAL_FLAGS0_TYPE_READ;
	sbale[1].flags |= SBAL_FLAGS_LAST_ENTRY;

S
Swen Schillig 已提交
1577 1578 1579 1580
	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;
1581
	erp_action->fsf_req_id = req->req_id;
S
Swen Schillig 已提交
1582

1583
	zfcp_fsf_start_erp_timer(req);
S
Swen Schillig 已提交
1584
	retval = zfcp_fsf_req_send(req);
L
Linus Torvalds 已提交
1585
	if (retval) {
S
Swen Schillig 已提交
1586
		zfcp_fsf_req_free(req);
1587
		erp_action->fsf_req_id = 0;
L
Linus Torvalds 已提交
1588
	}
S
Swen Schillig 已提交
1589
out:
1590
	spin_unlock_bh(&qdio->req_q_lock);
L
Linus Torvalds 已提交
1591 1592 1593
	return retval;
}

1594 1595
static void zfcp_fsf_open_wka_port_handler(struct zfcp_fsf_req *req)
{
1596
	struct zfcp_fc_wka_port *wka_port = req->data;
1597 1598 1599
	struct fsf_qtcb_header *header = &req->qtcb->header;

	if (req->status & ZFCP_STATUS_FSFREQ_ERROR) {
1600
		wka_port->status = ZFCP_FC_WKA_PORT_OFFLINE;
1601 1602 1603 1604 1605 1606 1607
		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);
1608
		/* fall through */
1609 1610
	case FSF_ADAPTER_STATUS_AVAILABLE:
		req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1611
		/* fall through */
1612
	case FSF_ACCESS_DENIED:
1613
		wka_port->status = ZFCP_FC_WKA_PORT_OFFLINE;
1614 1615 1616
		break;
	case FSF_GOOD:
		wka_port->handle = header->port_handle;
1617 1618
		/* fall through */
	case FSF_PORT_ALREADY_OPEN:
1619
		wka_port->status = ZFCP_FC_WKA_PORT_ONLINE;
1620 1621 1622 1623 1624 1625 1626
	}
out:
	wake_up(&wka_port->completion_wq);
}

/**
 * zfcp_fsf_open_wka_port - create and send open wka-port request
1627
 * @wka_port: pointer to struct zfcp_fc_wka_port
1628 1629
 * Returns: 0 on success, error otherwise
 */
1630
int zfcp_fsf_open_wka_port(struct zfcp_fc_wka_port *wka_port)
1631 1632
{
	struct qdio_buffer_element *sbale;
1633
	struct zfcp_qdio *qdio = wka_port->adapter->qdio;
1634 1635 1636
	struct zfcp_fsf_req *req;
	int retval = -EIO;

1637 1638
	spin_lock_bh(&qdio->req_q_lock);
	if (zfcp_fsf_req_sbal_get(qdio))
1639 1640
		goto out;

1641 1642
	req = zfcp_fsf_req_create(qdio, FSF_QTCB_OPEN_PORT_WITH_DID,
				  qdio->adapter->pool.erp_req);
1643

1644 1645 1646 1647 1648
	if (unlikely(IS_ERR(req))) {
		retval = PTR_ERR(req);
		goto out;
	}

1649
	req->status |= ZFCP_STATUS_FSFREQ_CLEANUP;
1650
	sbale = zfcp_qdio_sbale_req(qdio, &req->queue_req);
1651 1652 1653 1654
	sbale[0].flags |= SBAL_FLAGS0_TYPE_READ;
	sbale[1].flags |= SBAL_FLAGS_LAST_ENTRY;

	req->handler = zfcp_fsf_open_wka_port_handler;
1655
	hton24(req->qtcb->bottom.support.d_id, wka_port->d_id);
1656 1657 1658 1659 1660 1661 1662
	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:
1663
	spin_unlock_bh(&qdio->req_q_lock);
1664 1665 1666 1667 1668
	return retval;
}

static void zfcp_fsf_close_wka_port_handler(struct zfcp_fsf_req *req)
{
1669
	struct zfcp_fc_wka_port *wka_port = req->data;
1670 1671 1672

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

1676
	wka_port->status = ZFCP_FC_WKA_PORT_OFFLINE;
1677 1678 1679 1680 1681
	wake_up(&wka_port->completion_wq);
}

/**
 * zfcp_fsf_close_wka_port - create and send close wka port request
1682
 * @wka_port: WKA port to open
1683 1684
 * Returns: 0 on success, error otherwise
 */
1685
int zfcp_fsf_close_wka_port(struct zfcp_fc_wka_port *wka_port)
1686 1687
{
	struct qdio_buffer_element *sbale;
1688
	struct zfcp_qdio *qdio = wka_port->adapter->qdio;
1689 1690 1691
	struct zfcp_fsf_req *req;
	int retval = -EIO;

1692 1693
	spin_lock_bh(&qdio->req_q_lock);
	if (zfcp_fsf_req_sbal_get(qdio))
1694 1695
		goto out;

1696 1697
	req = zfcp_fsf_req_create(qdio, FSF_QTCB_CLOSE_PORT,
				  qdio->adapter->pool.erp_req);
1698

1699 1700 1701 1702 1703
	if (unlikely(IS_ERR(req))) {
		retval = PTR_ERR(req);
		goto out;
	}

1704
	req->status |= ZFCP_STATUS_FSFREQ_CLEANUP;
1705
	sbale = zfcp_qdio_sbale_req(qdio, &req->queue_req);
1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717
	sbale[0].flags |= SBAL_FLAGS0_TYPE_READ;
	sbale[1].flags |= SBAL_FLAGS_LAST_ENTRY;

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

	zfcp_fsf_start_timer(req, ZFCP_FSF_REQUEST_TIMEOUT);
	retval = zfcp_fsf_req_send(req);
	if (retval)
		zfcp_fsf_req_free(req);
out:
1718
	spin_unlock_bh(&qdio->req_q_lock);
1719 1720 1721
	return retval;
}

S
Swen Schillig 已提交
1722
static void zfcp_fsf_close_physical_port_handler(struct zfcp_fsf_req *req)
L
Linus Torvalds 已提交
1723
{
S
Swen Schillig 已提交
1724 1725
	struct zfcp_port *port = req->data;
	struct fsf_qtcb_header *header = &req->qtcb->header;
L
Linus Torvalds 已提交
1726 1727
	struct zfcp_unit *unit;

S
Swen Schillig 已提交
1728
	if (req->status & ZFCP_STATUS_FSFREQ_ERROR)
1729
		return;
L
Linus Torvalds 已提交
1730 1731 1732

	switch (header->fsf_status) {
	case FSF_PORT_HANDLE_NOT_VALID:
1733
		zfcp_erp_adapter_reopen(port->adapter, 0, "fscpph1", req);
S
Swen Schillig 已提交
1734
		req->status |= ZFCP_STATUS_FSFREQ_ERROR;
L
Linus Torvalds 已提交
1735 1736
		break;
	case FSF_ACCESS_DENIED:
S
Swen Schillig 已提交
1737
		zfcp_fsf_access_denied_port(req, port);
L
Linus Torvalds 已提交
1738 1739
		break;
	case FSF_PORT_BOXED:
1740 1741 1742
		/* 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);
1743 1744
		read_lock(&port->unit_list_lock);
		list_for_each_entry(unit, &port->unit_list, list)
1745 1746
			atomic_clear_mask(ZFCP_STATUS_COMMON_OPEN,
					  &unit->status);
1747
		read_unlock(&port->unit_list_lock);
1748
		zfcp_erp_port_boxed(port, "fscpph2", req);
1749
		req->status |= ZFCP_STATUS_FSFREQ_ERROR;
L
Linus Torvalds 已提交
1750 1751 1752 1753
		break;
	case FSF_ADAPTER_STATUS_AVAILABLE:
		switch (header->fsf_status_qual.word[0]) {
		case FSF_SQ_INVOKE_LINK_TEST_PROCEDURE:
S
Swen Schillig 已提交
1754
			/* fall through */
L
Linus Torvalds 已提交
1755
		case FSF_SQ_ULP_DEPENDENT_ERP_REQUIRED:
S
Swen Schillig 已提交
1756
			req->status |= ZFCP_STATUS_FSFREQ_ERROR;
L
Linus Torvalds 已提交
1757 1758 1759 1760 1761 1762 1763 1764
			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);
1765 1766
		read_lock(&port->unit_list_lock);
		list_for_each_entry(unit, &port->unit_list, list)
S
Swen Schillig 已提交
1767 1768
			atomic_clear_mask(ZFCP_STATUS_COMMON_OPEN,
					  &unit->status);
1769
		read_unlock(&port->unit_list_lock);
L
Linus Torvalds 已提交
1770 1771 1772 1773
		break;
	}
}

S
Swen Schillig 已提交
1774 1775 1776 1777
/**
 * zfcp_fsf_close_physical_port - close physical port
 * @erp_action: pointer to struct zfcp_erp_action
 * Returns: 0 on success
L
Linus Torvalds 已提交
1778
 */
S
Swen Schillig 已提交
1779
int zfcp_fsf_close_physical_port(struct zfcp_erp_action *erp_action)
L
Linus Torvalds 已提交
1780
{
1781
	struct qdio_buffer_element *sbale;
1782
	struct zfcp_qdio *qdio = erp_action->adapter->qdio;
S
Swen Schillig 已提交
1783 1784 1785
	struct zfcp_fsf_req *req;
	int retval = -EIO;

1786 1787
	spin_lock_bh(&qdio->req_q_lock);
	if (zfcp_fsf_req_sbal_get(qdio))
L
Linus Torvalds 已提交
1788 1789
		goto out;

1790 1791
	req = zfcp_fsf_req_create(qdio, FSF_QTCB_CLOSE_PHYSICAL_PORT,
				  qdio->adapter->pool.erp_req);
1792

1793
	if (IS_ERR(req)) {
S
Swen Schillig 已提交
1794 1795 1796
		retval = PTR_ERR(req);
		goto out;
	}
L
Linus Torvalds 已提交
1797

1798
	req->status |= ZFCP_STATUS_FSFREQ_CLEANUP;
1799
	sbale = zfcp_qdio_sbale_req(qdio, &req->queue_req);
S
Swen Schillig 已提交
1800 1801
	sbale[0].flags |= SBAL_FLAGS0_TYPE_READ;
	sbale[1].flags |= SBAL_FLAGS_LAST_ENTRY;
L
Linus Torvalds 已提交
1802

S
Swen Schillig 已提交
1803 1804 1805 1806
	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;
1807
	erp_action->fsf_req_id = req->req_id;
S
Swen Schillig 已提交
1808

1809
	zfcp_fsf_start_erp_timer(req);
S
Swen Schillig 已提交
1810
	retval = zfcp_fsf_req_send(req);
L
Linus Torvalds 已提交
1811
	if (retval) {
S
Swen Schillig 已提交
1812
		zfcp_fsf_req_free(req);
1813
		erp_action->fsf_req_id = 0;
L
Linus Torvalds 已提交
1814
	}
S
Swen Schillig 已提交
1815
out:
1816
	spin_unlock_bh(&qdio->req_q_lock);
L
Linus Torvalds 已提交
1817 1818 1819
	return retval;
}

S
Swen Schillig 已提交
1820
static void zfcp_fsf_open_unit_handler(struct zfcp_fsf_req *req)
L
Linus Torvalds 已提交
1821
{
S
Swen Schillig 已提交
1822 1823 1824 1825 1826 1827
	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;
1828
	int exclusive, readwrite;
L
Linus Torvalds 已提交
1829

S
Swen Schillig 已提交
1830
	if (req->status & ZFCP_STATUS_FSFREQ_ERROR)
1831
		return;
L
Linus Torvalds 已提交
1832 1833

	atomic_clear_mask(ZFCP_STATUS_COMMON_ACCESS_DENIED |
1834
			  ZFCP_STATUS_COMMON_ACCESS_BOXED |
L
Linus Torvalds 已提交
1835 1836 1837 1838 1839 1840 1841
			  ZFCP_STATUS_UNIT_SHARED |
			  ZFCP_STATUS_UNIT_READONLY,
			  &unit->status);

	switch (header->fsf_status) {

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

	case FSF_GOOD:
		unit->handle = header->lun_handle;
		atomic_set_mask(ZFCP_STATUS_COMMON_OPEN, &unit->status);
1897 1898 1899

		if (!(adapter->connection_features & FSF_FEATURE_NPIV_MODE) &&
		    (adapter->adapter_features & FSF_FEATURE_LUN_SHARING) &&
1900
		    !zfcp_ccw_priv_sch(adapter)) {
1901 1902 1903 1904 1905
			exclusive = (bottom->lun_access_info &
					FSF_UNIT_ACCESS_EXCLUSIVE);
			readwrite = (bottom->lun_access_info &
					FSF_UNIT_ACCESS_OUTBOUND_TRANSFER);

L
Linus Torvalds 已提交
1906 1907 1908 1909 1910 1911 1912
			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 已提交
1913
				dev_info(&adapter->ccw_device->dev,
1914 1915
					 "SCSI device at LUN 0x%016Lx on port "
					 "0x%016Lx opened read-only\n",
1916 1917
					 (unsigned long long)unit->fcp_lun,
					 (unsigned long long)unit->port->wwpn);
L
Linus Torvalds 已提交
1918 1919 1920
        		}

        		if (exclusive && !readwrite) {
S
Swen Schillig 已提交
1921
				dev_err(&adapter->ccw_device->dev,
1922 1923 1924
					"Exclusive read-only access not "
					"supported (unit 0x%016Lx, "
					"port 0x%016Lx)\n",
1925 1926
					(unsigned long long)unit->fcp_lun,
					(unsigned long long)unit->port->wwpn);
1927
				zfcp_erp_unit_failed(unit, "fsouh_5", req);
S
Swen Schillig 已提交
1928
				req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1929
				zfcp_erp_unit_shutdown(unit, 0, "fsouh_6", req);
L
Linus Torvalds 已提交
1930
        		} else if (!exclusive && readwrite) {
S
Swen Schillig 已提交
1931
				dev_err(&adapter->ccw_device->dev,
1932 1933
					"Shared read-write access not "
					"supported (unit 0x%016Lx, port "
1934
					"0x%016Lx)\n",
1935 1936
					(unsigned long long)unit->fcp_lun,
					(unsigned long long)unit->port->wwpn);
1937
				zfcp_erp_unit_failed(unit, "fsouh_7", req);
S
Swen Schillig 已提交
1938
				req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1939
				zfcp_erp_unit_shutdown(unit, 0, "fsouh_8", req);
L
Linus Torvalds 已提交
1940 1941 1942 1943 1944 1945
        		}
		}
		break;
	}
}

S
Swen Schillig 已提交
1946 1947 1948 1949
/**
 * zfcp_fsf_open_unit - open unit
 * @erp_action: pointer to struct zfcp_erp_action
 * Returns: 0 on success, error otherwise
L
Linus Torvalds 已提交
1950
 */
S
Swen Schillig 已提交
1951
int zfcp_fsf_open_unit(struct zfcp_erp_action *erp_action)
L
Linus Torvalds 已提交
1952
{
1953
	struct qdio_buffer_element *sbale;
S
Swen Schillig 已提交
1954
	struct zfcp_adapter *adapter = erp_action->adapter;
1955
	struct zfcp_qdio *qdio = adapter->qdio;
S
Swen Schillig 已提交
1956 1957 1958
	struct zfcp_fsf_req *req;
	int retval = -EIO;

1959 1960
	spin_lock_bh(&qdio->req_q_lock);
	if (zfcp_fsf_req_sbal_get(qdio))
L
Linus Torvalds 已提交
1961 1962
		goto out;

1963
	req = zfcp_fsf_req_create(qdio, FSF_QTCB_OPEN_LUN,
1964
				  adapter->pool.erp_req);
1965

1966
	if (IS_ERR(req)) {
S
Swen Schillig 已提交
1967 1968 1969 1970
		retval = PTR_ERR(req);
		goto out;
	}

1971
	req->status |= ZFCP_STATUS_FSFREQ_CLEANUP;
1972
	sbale = zfcp_qdio_sbale_req(qdio, &req->queue_req);
L
Linus Torvalds 已提交
1973 1974 1975
        sbale[0].flags |= SBAL_FLAGS0_TYPE_READ;
        sbale[1].flags |= SBAL_FLAGS_LAST_ENTRY;

S
Swen Schillig 已提交
1976 1977 1978 1979 1980
	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;
1981
	erp_action->fsf_req_id = req->req_id;
S
Swen Schillig 已提交
1982 1983 1984 1985

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

1986
	zfcp_fsf_start_erp_timer(req);
S
Swen Schillig 已提交
1987
	retval = zfcp_fsf_req_send(req);
L
Linus Torvalds 已提交
1988
	if (retval) {
S
Swen Schillig 已提交
1989
		zfcp_fsf_req_free(req);
1990
		erp_action->fsf_req_id = 0;
L
Linus Torvalds 已提交
1991
	}
S
Swen Schillig 已提交
1992
out:
1993
	spin_unlock_bh(&qdio->req_q_lock);
L
Linus Torvalds 已提交
1994 1995 1996
	return retval;
}

S
Swen Schillig 已提交
1997
static void zfcp_fsf_close_unit_handler(struct zfcp_fsf_req *req)
L
Linus Torvalds 已提交
1998
{
S
Swen Schillig 已提交
1999
	struct zfcp_unit *unit = req->data;
L
Linus Torvalds 已提交
2000

S
Swen Schillig 已提交
2001
	if (req->status & ZFCP_STATUS_FSFREQ_ERROR)
2002
		return;
L
Linus Torvalds 已提交
2003

S
Swen Schillig 已提交
2004
	switch (req->qtcb->header.fsf_status) {
L
Linus Torvalds 已提交
2005
	case FSF_PORT_HANDLE_NOT_VALID:
2006
		zfcp_erp_adapter_reopen(unit->port->adapter, 0, "fscuh_1", req);
S
Swen Schillig 已提交
2007
		req->status |= ZFCP_STATUS_FSFREQ_ERROR;
L
Linus Torvalds 已提交
2008 2009
		break;
	case FSF_LUN_HANDLE_NOT_VALID:
2010
		zfcp_erp_port_reopen(unit->port, 0, "fscuh_2", req);
S
Swen Schillig 已提交
2011
		req->status |= ZFCP_STATUS_FSFREQ_ERROR;
L
Linus Torvalds 已提交
2012 2013
		break;
	case FSF_PORT_BOXED:
2014
		zfcp_erp_port_boxed(unit->port, "fscuh_3", req);
2015
		req->status |= ZFCP_STATUS_FSFREQ_ERROR;
L
Linus Torvalds 已提交
2016 2017
		break;
	case FSF_ADAPTER_STATUS_AVAILABLE:
S
Swen Schillig 已提交
2018
		switch (req->qtcb->header.fsf_status_qual.word[0]) {
L
Linus Torvalds 已提交
2019
		case FSF_SQ_INVOKE_LINK_TEST_PROCEDURE:
2020
			zfcp_fc_test_link(unit->port);
S
Swen Schillig 已提交
2021
			/* fall through */
L
Linus Torvalds 已提交
2022
		case FSF_SQ_ULP_DEPENDENT_ERP_REQUIRED:
S
Swen Schillig 已提交
2023
			req->status |= ZFCP_STATUS_FSFREQ_ERROR;
L
Linus Torvalds 已提交
2024 2025 2026 2027 2028 2029 2030 2031 2032 2033
			break;
		}
		break;
	case FSF_GOOD:
		atomic_clear_mask(ZFCP_STATUS_COMMON_OPEN, &unit->status);
		break;
	}
}

/**
S
Swen Schillig 已提交
2034 2035 2036
 * zfcp_fsf_close_unit - close zfcp unit
 * @erp_action: pointer to struct zfcp_unit
 * Returns: 0 on success, error otherwise
L
Linus Torvalds 已提交
2037
 */
S
Swen Schillig 已提交
2038
int zfcp_fsf_close_unit(struct zfcp_erp_action *erp_action)
L
Linus Torvalds 已提交
2039
{
2040
	struct qdio_buffer_element *sbale;
2041
	struct zfcp_qdio *qdio = erp_action->adapter->qdio;
S
Swen Schillig 已提交
2042 2043
	struct zfcp_fsf_req *req;
	int retval = -EIO;
L
Linus Torvalds 已提交
2044

2045 2046
	spin_lock_bh(&qdio->req_q_lock);
	if (zfcp_fsf_req_sbal_get(qdio))
L
Linus Torvalds 已提交
2047
		goto out;
2048

2049 2050
	req = zfcp_fsf_req_create(qdio, FSF_QTCB_CLOSE_LUN,
				  qdio->adapter->pool.erp_req);
2051

2052
	if (IS_ERR(req)) {
S
Swen Schillig 已提交
2053 2054 2055
		retval = PTR_ERR(req);
		goto out;
	}
L
Linus Torvalds 已提交
2056

2057
	req->status |= ZFCP_STATUS_FSFREQ_CLEANUP;
2058
	sbale = zfcp_qdio_sbale_req(qdio, &req->queue_req);
S
Swen Schillig 已提交
2059
	sbale[0].flags |= SBAL_FLAGS0_TYPE_READ;
L
Linus Torvalds 已提交
2060 2061
	sbale[1].flags |= SBAL_FLAGS_LAST_ENTRY;

S
Swen Schillig 已提交
2062 2063 2064 2065 2066
	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;
2067
	erp_action->fsf_req_id = req->req_id;
2068

2069
	zfcp_fsf_start_erp_timer(req);
S
Swen Schillig 已提交
2070 2071 2072
	retval = zfcp_fsf_req_send(req);
	if (retval) {
		zfcp_fsf_req_free(req);
2073
		erp_action->fsf_req_id = 0;
S
Swen Schillig 已提交
2074 2075
	}
out:
2076
	spin_unlock_bh(&qdio->req_q_lock);
S
Swen Schillig 已提交
2077
	return retval;
L
Linus Torvalds 已提交
2078 2079
}

2080 2081 2082
static void zfcp_fsf_update_lat(struct fsf_latency_record *lat_rec, u32 lat)
{
	lat_rec->sum += lat;
S
Swen Schillig 已提交
2083 2084
	lat_rec->min = min(lat_rec->min, lat);
	lat_rec->max = max(lat_rec->max, lat);
2085 2086
}

2087
static void zfcp_fsf_req_trace(struct zfcp_fsf_req *req, struct scsi_cmnd *scsi)
2088
{
2089 2090
	struct fsf_qual_latency_info *lat_in;
	struct latency_cont *lat = NULL;
S
Swen Schillig 已提交
2091
	struct zfcp_unit *unit = req->unit;
2092 2093
	struct zfcp_blk_drv_data blktrc;
	int ticks = req->adapter->timer_ticks;
2094

2095
	lat_in = &req->qtcb->prefix.prot_status_qual.latency_info;
2096

2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119
	blktrc.flags = 0;
	blktrc.magic = ZFCP_BLK_DRV_DATA_MAGIC;
	if (req->status & ZFCP_STATUS_FSFREQ_ERROR)
		blktrc.flags |= ZFCP_BLK_REQ_ERROR;
	blktrc.inb_usage = req->queue_req.qdio_inb_usage;
	blktrc.outb_usage = req->queue_req.qdio_outb_usage;

	if (req->adapter->adapter_features & FSF_FEATURE_MEASUREMENT_DATA) {
		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 已提交
2120

2121 2122 2123 2124 2125 2126 2127
		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 已提交
2128 2129
	}

2130 2131
	blk_add_driver_data(scsi->request->q, scsi->request, &blktrc,
			    sizeof(blktrc));
S
Stefan Raspl 已提交
2132 2133
}

S
Swen Schillig 已提交
2134
static void zfcp_fsf_send_fcp_command_task_handler(struct zfcp_fsf_req *req)
L
Linus Torvalds 已提交
2135
{
2136
	struct scsi_cmnd *scpnt;
2137
	struct fcp_resp_with_ext *fcp_rsp;
L
Linus Torvalds 已提交
2138 2139
	unsigned long flags;

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

2142 2143 2144 2145 2146 2147
	scpnt = req->data;
	if (unlikely(!scpnt)) {
		read_unlock_irqrestore(&req->adapter->abort_lock, flags);
		return;
	}

S
Swen Schillig 已提交
2148
	if (unlikely(req->status & ZFCP_STATUS_FSFREQ_ERROR)) {
2149
		set_host_byte(scpnt, DID_TRANSPORT_DISRUPTED);
L
Linus Torvalds 已提交
2150 2151 2152
		goto skip_fsfstatus;
	}

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

2156
	zfcp_fsf_req_trace(req, scpnt);
S
Stefan Raspl 已提交
2157

S
Swen Schillig 已提交
2158
skip_fsfstatus:
2159
	if (scpnt->result != 0)
S
Swen Schillig 已提交
2160
		zfcp_dbf_scsi_result("erro", 3, req->adapter->dbf, scpnt, req);
2161
	else if (scpnt->retries > 0)
S
Swen Schillig 已提交
2162
		zfcp_dbf_scsi_result("retr", 4, req->adapter->dbf, scpnt, req);
2163
	else
S
Swen Schillig 已提交
2164
		zfcp_dbf_scsi_result("norm", 6, req->adapter->dbf, scpnt, req);
L
Linus Torvalds 已提交
2165 2166 2167 2168 2169 2170 2171 2172 2173

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

S
Swen Schillig 已提交
2177
static void zfcp_fsf_send_fcp_ctm_handler(struct zfcp_fsf_req *req)
L
Linus Torvalds 已提交
2178
{
2179 2180
	struct fcp_resp_with_ext *fcp_rsp;
	struct fcp_resp_rsp_info *rsp_info;
L
Linus Torvalds 已提交
2181

2182 2183 2184 2185
	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 已提交
2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201
	     (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 已提交
2202 2203
		goto skip_fsfstatus;

S
Swen Schillig 已提交
2204 2205 2206
	switch (header->fsf_status) {
	case FSF_HANDLE_MISMATCH:
	case FSF_PORT_HANDLE_NOT_VALID:
2207
		zfcp_erp_adapter_reopen(unit->port->adapter, 0, "fssfch1", req);
S
Swen Schillig 已提交
2208 2209 2210 2211
		req->status |= ZFCP_STATUS_FSFREQ_ERROR;
		break;
	case FSF_FCPLUN_NOT_VALID:
	case FSF_LUN_HANDLE_NOT_VALID:
2212
		zfcp_erp_port_reopen(unit->port, 0, "fssfch2", req);
S
Swen Schillig 已提交
2213
		req->status |= ZFCP_STATUS_FSFREQ_ERROR;
L
Linus Torvalds 已提交
2214
		break;
S
Swen Schillig 已提交
2215 2216
	case FSF_SERVICE_CLASS_NOT_SUPPORTED:
		zfcp_fsf_class_not_supp(req);
L
Linus Torvalds 已提交
2217
		break;
S
Swen Schillig 已提交
2218 2219 2220 2221 2222
	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,
2223 2224
			"Incorrect direction %d, unit 0x%016Lx on port "
			"0x%016Lx closed\n",
S
Swen Schillig 已提交
2225
			req->qtcb->bottom.io.data_direction,
2226 2227
			(unsigned long long)unit->fcp_lun,
			(unsigned long long)unit->port->wwpn);
2228 2229
		zfcp_erp_adapter_shutdown(unit->port->adapter, 0, "fssfch3",
					  req);
S
Swen Schillig 已提交
2230 2231 2232 2233
		req->status |= ZFCP_STATUS_FSFREQ_ERROR;
		break;
	case FSF_CMND_LENGTH_NOT_VALID:
		dev_err(&req->adapter->ccw_device->dev,
2234 2235
			"Incorrect CDB length %d, unit 0x%016Lx on "
			"port 0x%016Lx closed\n",
S
Swen Schillig 已提交
2236
			req->qtcb->bottom.io.fcp_cmnd_length,
2237 2238
			(unsigned long long)unit->fcp_lun,
			(unsigned long long)unit->port->wwpn);
2239 2240
		zfcp_erp_adapter_shutdown(unit->port->adapter, 0, "fssfch4",
					  req);
S
Swen Schillig 已提交
2241 2242 2243
		req->status |= ZFCP_STATUS_FSFREQ_ERROR;
		break;
	case FSF_PORT_BOXED:
2244
		zfcp_erp_port_boxed(unit->port, "fssfch5", req);
2245
		req->status |= ZFCP_STATUS_FSFREQ_ERROR;
S
Swen Schillig 已提交
2246 2247
		break;
	case FSF_LUN_BOXED:
2248
		zfcp_erp_unit_boxed(unit, "fssfch6", req);
2249
		req->status |= ZFCP_STATUS_FSFREQ_ERROR;
S
Swen Schillig 已提交
2250 2251 2252 2253
		break;
	case FSF_ADAPTER_STATUS_AVAILABLE:
		if (header->fsf_status_qual.word[0] ==
		    FSF_SQ_INVOKE_LINK_TEST_PROCEDURE)
2254
			zfcp_fc_test_link(unit->port);
S
Swen Schillig 已提交
2255
		req->status |= ZFCP_STATUS_FSFREQ_ERROR;
L
Linus Torvalds 已提交
2256 2257
		break;
	}
S
Swen Schillig 已提交
2258 2259 2260 2261 2262 2263
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;
2264
		put_device(&unit->dev);
S
Swen Schillig 已提交
2265
	}
L
Linus Torvalds 已提交
2266 2267
}

S
Swen Schillig 已提交
2268 2269 2270 2271
/**
 * 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 已提交
2272
 */
2273 2274
int zfcp_fsf_send_fcp_command_task(struct zfcp_unit *unit,
				   struct scsi_cmnd *scsi_cmnd)
L
Linus Torvalds 已提交
2275
{
S
Swen Schillig 已提交
2276
	struct zfcp_fsf_req *req;
2277
	struct fcp_cmnd *fcp_cmnd;
2278
	unsigned int sbtype = SBAL_FLAGS0_TYPE_READ;
S
Swen Schillig 已提交
2279
	int real_bytes, retval = -EIO;
2280
	struct zfcp_adapter *adapter = unit->port->adapter;
2281
	struct zfcp_qdio *qdio = adapter->qdio;
L
Linus Torvalds 已提交
2282

S
Swen Schillig 已提交
2283 2284 2285
	if (unlikely(!(atomic_read(&unit->status) &
		       ZFCP_STATUS_COMMON_UNBLOCKED)))
		return -EBUSY;
L
Linus Torvalds 已提交
2286

2287 2288 2289
	spin_lock(&qdio->req_q_lock);
	if (atomic_read(&qdio->req_q.count) <= 0) {
		atomic_inc(&qdio->req_q_full);
S
Swen Schillig 已提交
2290
		goto out;
2291
	}
2292

2293
	req = zfcp_fsf_req_create(qdio, FSF_QTCB_FCP_CMND,
2294
				  adapter->pool.scsi_req);
2295

2296
	if (IS_ERR(req)) {
S
Swen Schillig 已提交
2297 2298 2299 2300
		retval = PTR_ERR(req);
		goto out;
	}

2301
	req->status |= ZFCP_STATUS_FSFREQ_CLEANUP;
2302
	get_device(&unit->dev);
S
Swen Schillig 已提交
2303 2304 2305 2306 2307 2308
	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;
2309
	req->qtcb->bottom.io.fcp_cmnd_length = FCP_CMND_LEN;
S
Swen Schillig 已提交
2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320

	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 已提交
2321
		break;
S
Swen Schillig 已提交
2322 2323 2324 2325 2326 2327
	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;
		sbtype = SBAL_FLAGS0_TYPE_WRITE;
L
Linus Torvalds 已提交
2328
		break;
S
Swen Schillig 已提交
2329 2330
	case DMA_BIDIRECTIONAL:
		goto failed_scsi_cmnd;
L
Linus Torvalds 已提交
2331 2332
	}

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

2336
	real_bytes = zfcp_qdio_sbals_from_sg(qdio, &req->queue_req, sbtype,
S
Swen Schillig 已提交
2337 2338 2339
					     scsi_sglist(scsi_cmnd),
					     FSF_MAX_SBALS_PER_REQ);
	if (unlikely(real_bytes < 0)) {
2340
		if (req->queue_req.sbal_number >= FSF_MAX_SBALS_PER_REQ) {
S
Swen Schillig 已提交
2341
			dev_err(&adapter->ccw_device->dev,
2342 2343
				"Oversize data package, unit 0x%016Lx "
				"on port 0x%016Lx closed\n",
2344 2345
				(unsigned long long)unit->fcp_lun,
				(unsigned long long)unit->port->wwpn);
2346
			zfcp_erp_unit_shutdown(unit, 0, "fssfct1", req);
S
Swen Schillig 已提交
2347 2348 2349
			retval = -EINVAL;
		}
		goto failed_scsi_cmnd;
L
Linus Torvalds 已提交
2350 2351
	}

S
Swen Schillig 已提交
2352 2353 2354
	retval = zfcp_fsf_req_send(req);
	if (unlikely(retval))
		goto failed_scsi_cmnd;
L
Linus Torvalds 已提交
2355

S
Swen Schillig 已提交
2356
	goto out;
L
Linus Torvalds 已提交
2357

S
Swen Schillig 已提交
2358
failed_scsi_cmnd:
2359
	put_device(&unit->dev);
S
Swen Schillig 已提交
2360 2361 2362
	zfcp_fsf_req_free(req);
	scsi_cmnd->host_scribble = NULL;
out:
2363
	spin_unlock(&qdio->req_q_lock);
S
Swen Schillig 已提交
2364
	return retval;
L
Linus Torvalds 已提交
2365 2366 2367
}

/**
S
Swen Schillig 已提交
2368 2369 2370 2371
 * 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 已提交
2372
 */
2373
struct zfcp_fsf_req *zfcp_fsf_send_fcp_ctm(struct zfcp_unit *unit, u8 tm_flags)
L
Linus Torvalds 已提交
2374
{
2375
	struct qdio_buffer_element *sbale;
S
Swen Schillig 已提交
2376
	struct zfcp_fsf_req *req = NULL;
2377
	struct fcp_cmnd *fcp_cmnd;
2378
	struct zfcp_qdio *qdio = unit->port->adapter->qdio;
L
Linus Torvalds 已提交
2379

S
Swen Schillig 已提交
2380 2381 2382
	if (unlikely(!(atomic_read(&unit->status) &
		       ZFCP_STATUS_COMMON_UNBLOCKED)))
		return NULL;
L
Linus Torvalds 已提交
2383

2384 2385
	spin_lock_bh(&qdio->req_q_lock);
	if (zfcp_fsf_req_sbal_get(qdio))
S
Swen Schillig 已提交
2386
		goto out;
2387

2388 2389
	req = zfcp_fsf_req_create(qdio, FSF_QTCB_FCP_CMND,
				  qdio->adapter->pool.scsi_req);
2390

2391 2392
	if (IS_ERR(req)) {
		req = NULL;
S
Swen Schillig 已提交
2393
		goto out;
2394
	}
L
Linus Torvalds 已提交
2395

S
Swen Schillig 已提交
2396 2397 2398 2399 2400 2401 2402
	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;
2403
	req->qtcb->bottom.io.fcp_cmnd_length = FCP_CMND_LEN;
S
Swen Schillig 已提交
2404

2405
	sbale = zfcp_qdio_sbale_req(qdio, &req->queue_req);
S
Swen Schillig 已提交
2406 2407
	sbale[0].flags |= SBAL_FLAGS0_TYPE_WRITE;
	sbale[1].flags |= SBAL_FLAGS_LAST_ENTRY;
L
Linus Torvalds 已提交
2408

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

S
Swen Schillig 已提交
2412 2413 2414
	zfcp_fsf_start_timer(req, ZFCP_SCSI_ER_TIMEOUT);
	if (!zfcp_fsf_req_send(req))
		goto out;
L
Linus Torvalds 已提交
2415

S
Swen Schillig 已提交
2416 2417 2418
	zfcp_fsf_req_free(req);
	req = NULL;
out:
2419
	spin_unlock_bh(&qdio->req_q_lock);
S
Swen Schillig 已提交
2420 2421
	return req;
}
L
Linus Torvalds 已提交
2422

S
Swen Schillig 已提交
2423 2424
static void zfcp_fsf_control_file_handler(struct zfcp_fsf_req *req)
{
L
Linus Torvalds 已提交
2425 2426
}

S
Swen Schillig 已提交
2427 2428 2429 2430 2431
/**
 * 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 已提交
2432
 */
S
Swen Schillig 已提交
2433 2434
struct zfcp_fsf_req *zfcp_fsf_control_file(struct zfcp_adapter *adapter,
					   struct zfcp_fsf_cfdc *fsf_cfdc)
L
Linus Torvalds 已提交
2435
{
2436
	struct qdio_buffer_element *sbale;
2437
	struct zfcp_qdio *qdio = adapter->qdio;
S
Swen Schillig 已提交
2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454
	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 已提交
2455

2456 2457
	spin_lock_bh(&qdio->req_q_lock);
	if (zfcp_fsf_req_sbal_get(qdio))
S
Swen Schillig 已提交
2458
		goto out;
L
Linus Torvalds 已提交
2459

2460
	req = zfcp_fsf_req_create(qdio, fsf_cfdc->command, NULL);
2461
	if (IS_ERR(req)) {
S
Swen Schillig 已提交
2462 2463 2464
		retval = -EPERM;
		goto out;
	}
L
Linus Torvalds 已提交
2465

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

2468
	sbale = zfcp_qdio_sbale_req(qdio, &req->queue_req);
S
Swen Schillig 已提交
2469
	sbale[0].flags |= direction;
2470

S
Swen Schillig 已提交
2471 2472 2473
	bottom = &req->qtcb->bottom.support;
	bottom->operation_subtype = FSF_CFDC_OPERATION_SUBTYPE;
	bottom->option = fsf_cfdc->option;
2474

2475 2476 2477
	bytes = zfcp_qdio_sbals_from_sg(qdio, &req->queue_req,
					direction, fsf_cfdc->sg,
					FSF_MAX_SBALS_PER_REQ);
S
Swen Schillig 已提交
2478 2479 2480 2481
	if (bytes != ZFCP_CFDC_MAX_SIZE) {
		zfcp_fsf_req_free(req);
		goto out;
	}
L
Linus Torvalds 已提交
2482

S
Swen Schillig 已提交
2483 2484 2485
	zfcp_fsf_start_timer(req, ZFCP_FSF_REQUEST_TIMEOUT);
	retval = zfcp_fsf_req_send(req);
out:
2486
	spin_unlock_bh(&qdio->req_q_lock);
2487

S
Swen Schillig 已提交
2488
	if (!retval) {
2489
		wait_for_completion(&req->completion);
S
Swen Schillig 已提交
2490
		return req;
L
Linus Torvalds 已提交
2491
	}
S
Swen Schillig 已提交
2492
	return ERR_PTR(retval);
L
Linus Torvalds 已提交
2493
}
2494 2495 2496 2497 2498 2499

/**
 * 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
 */
2500
void zfcp_fsf_reqid_check(struct zfcp_qdio *qdio, int sbal_idx)
2501
{
2502 2503
	struct zfcp_adapter *adapter = qdio->adapter;
	struct qdio_buffer *sbal = qdio->resp_q.sbal[sbal_idx];
2504 2505
	struct qdio_buffer_element *sbale;
	struct zfcp_fsf_req *fsf_req;
2506
	unsigned long req_id;
2507 2508 2509 2510 2511 2512
	int idx;

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

		sbale = &sbal->element[idx];
		req_id = (unsigned long) sbale->addr;
2513
		fsf_req = zfcp_reqlist_find_rm(adapter->req_list, req_id);
2514 2515 2516 2517 2518 2519 2520 2521 2522

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

2523 2524
		fsf_req->queue_req.sbal_response = sbal_idx;
		fsf_req->queue_req.qdio_inb_usage =
2525
			atomic_read(&qdio->resp_q.count);
2526 2527 2528 2529 2530 2531
		zfcp_fsf_req_complete(fsf_req);

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