zfcp_fsf.c 72.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, 2009
L
Linus Torvalds 已提交
7 8
 */

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

S
Swen Schillig 已提交
257
static void zfcp_fsf_status_read_link_down(struct zfcp_fsf_req *req)
L
Linus Torvalds 已提交
258
{
S
Swen Schillig 已提交
259 260 261
	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 已提交
262

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

S
Swen Schillig 已提交
275 276 277 278
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 已提交
279

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

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

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

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

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

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

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

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

S
Swen Schillig 已提交
382 383 384 385 386
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 已提交
387

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

S
Swen Schillig 已提交
541
static void zfcp_fsf_exchange_config_data_handler(struct zfcp_fsf_req *req)
C
Christof Schmitt 已提交
542 543
{
	struct zfcp_adapter *adapter = req->adapter;
S
Swen Schillig 已提交
544 545 546
	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 已提交
547

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

S
Swen Schillig 已提交
551 552 553 554 555 556
	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;
557

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

S
Swen Schillig 已提交
563 564
		if (bottom->max_qtcb_size < sizeof(struct fsf_qtcb)) {
			dev_err(&adapter->ccw_device->dev,
565 566 567
				"FCP adapter maximum QTCB size (%d bytes) "
				"is too small\n",
				bottom->max_qtcb_size);
568
			zfcp_erp_adapter_shutdown(adapter, 0, "fsecdh1", req);
S
Swen Schillig 已提交
569 570 571 572
			return;
		}
		atomic_set_mask(ZFCP_STATUS_ADAPTER_XCONFIG_OK,
				&adapter->status);
L
Linus Torvalds 已提交
573
		break;
S
Swen Schillig 已提交
574 575 576 577 578 579 580
	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 已提交
581

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

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

S
Swen Schillig 已提交
593 594 595 596 597 598 599
	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 已提交
600

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

S
Swen Schillig 已提交
616 617 618 619 620
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 已提交
621

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

625
	if (adapter->connection_features & FSF_FEATURE_NPIV_MODE) {
S
Swen Schillig 已提交
626
		fc_host_permanent_port_name(shost) = bottom->wwpn;
627 628
		fc_host_port_type(shost) = FC_PORTTYPE_NPIV;
	} else
S
Swen Schillig 已提交
629 630 631 632
		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 已提交
633

S
Swen Schillig 已提交
634 635 636 637 638 639 640 641 642 643 644 645 646
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);
647
		zfcp_fsf_link_down_info_eval(req, "fsepdh1",
S
Swen Schillig 已提交
648
			&qtcb->header.fsf_status_qual.link_down_info);
649
		break;
L
Linus Torvalds 已提交
650
	}
S
Swen Schillig 已提交
651
}
652

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

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

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

668
	spin_unlock_bh(&adapter->req_q_lock);
S
Swen Schillig 已提交
669
	ret = wait_event_interruptible_timeout(adapter->request_wq,
670
			       zfcp_fsf_sbal_check(adapter), 5 * HZ);
S
Swen Schillig 已提交
671 672
	if (ret > 0)
		return 0;
673 674
	if (!ret)
		atomic_inc(&adapter->qdio_outb_full);
675 676

	spin_lock_bh(&adapter->req_q_lock);
S
Swen Schillig 已提交
677 678 679 680 681 682 683 684 685 686
	return -EIO;
}

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

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

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

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

	return &qtcb->fsf_req;
}

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

S
Swen Schillig 已提交
716 717
	struct zfcp_fsf_req *req;
	struct zfcp_qdio_queue *req_q = &adapter->req_q;
718

S
Swen Schillig 已提交
719 720 721 722
	if (req_flags & ZFCP_REQ_NO_QTCB)
		req = zfcp_fsf_alloc_noqtcb(pool);
	else
		req = zfcp_fsf_alloc_qtcb(pool);
L
Linus Torvalds 已提交
723

S
Swen Schillig 已提交
724
	if (unlikely(!req))
725
		return ERR_PTR(-ENOMEM);
L
Linus Torvalds 已提交
726

S
Swen Schillig 已提交
727 728
	if (adapter->req_no == 0)
		adapter->req_no++;
L
Linus Torvalds 已提交
729

S
Swen Schillig 已提交
730 731 732
	INIT_LIST_HEAD(&req->list);
	init_timer(&req->timer);
	init_waitqueue_head(&req->completion_wq);
L
Linus Torvalds 已提交
733

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

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

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

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

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

S
Swen Schillig 已提交
768
	return req;
L
Linus Torvalds 已提交
769 770
}

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

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

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

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

	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 已提交
810
 */
S
Swen Schillig 已提交
811
int zfcp_fsf_status_read(struct zfcp_adapter *adapter)
L
Linus Torvalds 已提交
812
{
S
Swen Schillig 已提交
813 814
	struct zfcp_fsf_req *req;
	struct fsf_status_read_buffer *sr_buf;
815
	struct qdio_buffer_element *sbale;
S
Swen Schillig 已提交
816
	int retval = -EIO;
L
Linus Torvalds 已提交
817

818
	spin_lock_bh(&adapter->req_q_lock);
S
Swen Schillig 已提交
819 820 821 822 823 824
	if (zfcp_fsf_req_sbal_get(adapter))
		goto out;

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

S
Swen Schillig 已提交
830 831 832 833 834 835 836 837 838 839 840 841 842 843 844
	sbale = zfcp_qdio_sbale_req(req);
	sbale[0].flags |= SBAL_FLAGS0_TYPE_STATUS;
	sbale[2].flags |= SBAL_FLAGS_LAST_ENTRY;
	req->sbale_curr = 2;

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

S
Swen Schillig 已提交
846 847 848
	retval = zfcp_fsf_req_send(req);
	if (retval)
		goto failed_req_send;
L
Linus Torvalds 已提交
849

S
Swen Schillig 已提交
850 851 852 853 854 855 856 857
	goto out;

failed_req_send:
	mempool_free(sr_buf, adapter->pool.data_status_read);
failed_buf:
	zfcp_fsf_req_free(req);
	zfcp_hba_dbf_event_fsf_unsol("fail", adapter, NULL);
out:
858
	spin_unlock_bh(&adapter->req_q_lock);
S
Swen Schillig 已提交
859 860 861 862 863 864 865 866 867 868 869 870
	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 已提交
871
	case FSF_PORT_HANDLE_NOT_VALID:
S
Swen Schillig 已提交
872
		if (fsq->word[0] == fsq->word[1]) {
873 874
			zfcp_erp_adapter_reopen(unit->port->adapter, 0,
						"fsafch1", req);
S
Swen Schillig 已提交
875
			req->status |= ZFCP_STATUS_FSFREQ_ERROR;
L
Linus Torvalds 已提交
876 877 878
		}
		break;
	case FSF_LUN_HANDLE_NOT_VALID:
S
Swen Schillig 已提交
879
		if (fsq->word[0] == fsq->word[1]) {
880
			zfcp_erp_port_reopen(unit->port, 0, "fsafch2", req);
S
Swen Schillig 已提交
881
			req->status |= ZFCP_STATUS_FSFREQ_ERROR;
L
Linus Torvalds 已提交
882 883 884
		}
		break;
	case FSF_FCP_COMMAND_DOES_NOT_EXIST:
S
Swen Schillig 已提交
885
		req->status |= ZFCP_STATUS_FSFREQ_ABORTNOTNEEDED;
L
Linus Torvalds 已提交
886 887
		break;
	case FSF_PORT_BOXED:
888
		zfcp_erp_port_boxed(unit->port, "fsafch3", req);
S
Swen Schillig 已提交
889 890
		req->status |= ZFCP_STATUS_FSFREQ_ERROR |
			       ZFCP_STATUS_FSFREQ_RETRY;
L
Linus Torvalds 已提交
891 892
		break;
	case FSF_LUN_BOXED:
893
		zfcp_erp_unit_boxed(unit, "fsafch4", req);
S
Swen Schillig 已提交
894 895
		req->status |= ZFCP_STATUS_FSFREQ_ERROR |
			       ZFCP_STATUS_FSFREQ_RETRY;
L
Linus Torvalds 已提交
896 897
                break;
	case FSF_ADAPTER_STATUS_AVAILABLE:
S
Swen Schillig 已提交
898
		switch (fsq->word[0]) {
L
Linus Torvalds 已提交
899
		case FSF_SQ_INVOKE_LINK_TEST_PROCEDURE:
900
			zfcp_test_link(unit->port);
901
			/* fall through */
L
Linus Torvalds 已提交
902
		case FSF_SQ_ULP_DEPENDENT_ERP_REQUIRED:
S
Swen Schillig 已提交
903
			req->status |= ZFCP_STATUS_FSFREQ_ERROR;
L
Linus Torvalds 已提交
904 905 906 907
			break;
		}
		break;
	case FSF_GOOD:
S
Swen Schillig 已提交
908
		req->status |= ZFCP_STATUS_FSFREQ_ABORTSUCCEEDED;
L
Linus Torvalds 已提交
909 910 911 912 913
		break;
	}
}

/**
S
Swen Schillig 已提交
914 915 916 917
 * 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 已提交
918 919
 */

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

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

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

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

S
Swen Schillig 已提交
945 946 947 948 949 950 951 952 953 954 955 956 957 958
	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:
959
	spin_unlock_bh(&adapter->req_q_lock);
S
Swen Schillig 已提交
960
	return req;
L
Linus Torvalds 已提交
961 962
}

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

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

S
Swen Schillig 已提交
971
	if (req->status & ZFCP_STATUS_FSFREQ_ERROR)
L
Linus Torvalds 已提交
972 973 974 975
		goto skip_fsfstatus;

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

skip_fsfstatus:
S
Swen Schillig 已提交
1009
	if (send_ct->handler)
L
Linus Torvalds 已提交
1010
		send_ct->handler(send_ct->handler_data);
S
Swen Schillig 已提交
1011
}
L
Linus Torvalds 已提交
1012

1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029
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;
}

1030 1031 1032 1033
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 已提交
1034
{
1035 1036
	struct qdio_buffer_element *sbale = zfcp_qdio_sbale_req(req);
	u32 feat = req->adapter->adapter_features;
S
Swen Schillig 已提交
1037 1038
	int bytes;

1039
	if (!(feat & FSF_FEATURE_ELS_CT_CHAINED_SBALS)) {
1040
		if (!zfcp_fsf_one_sbal(sg_req) || !zfcp_fsf_one_sbal(sg_resp))
1041 1042
			return -EOPNOTSUPP;

1043 1044 1045 1046 1047 1048 1049
		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);
1050 1051 1052
		return 0;
	}

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

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

	return 0;
L
Linus Torvalds 已提交
1067 1068 1069
}

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

1083
	spin_lock_bh(&adapter->req_q_lock);
S
Swen Schillig 已提交
1084 1085
	if (zfcp_fsf_req_sbal_get(adapter))
		goto out;
L
Linus Torvalds 已提交
1086

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

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

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

	zfcp_san_dbf_event_ct_request(req);

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

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

S
Swen Schillig 已提交
1118
	goto out;
L
Linus Torvalds 已提交
1119

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

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

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

S
Swen Schillig 已提交
1137
	if (req->status & ZFCP_STATUS_FSFREQ_ERROR)
L
Linus Torvalds 已提交
1138 1139 1140 1141
		goto skip_fsfstatus;

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

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

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

1202
	ret = zfcp_fsf_setup_ct_els_sbals(req, els->req, els->resp, 2);
1203

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

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

	zfcp_san_dbf_event_els_request(req);

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

	goto out;

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

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

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

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

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

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

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

1280
	spin_lock_bh(&adapter->req_q_lock);
S
Swen Schillig 已提交
1281
	if (zfcp_fsf_req_sbal_get(adapter))
1282
		goto out_unlock;
S
Swen Schillig 已提交
1283 1284 1285

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

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

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

	if (data)
S
Swen Schillig 已提交
1303
		req->data = data;
1304

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

S
Swen Schillig 已提交
1312
	zfcp_fsf_req_free(req);
1313
	return retval;
1314

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

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

C
Christof Schmitt 已提交
1332
	if (!(adapter->adapter_features & FSF_FEATURE_HBAAPI_MANAGEMENT))
1333
		return -EOPNOTSUPP;
L
Linus Torvalds 已提交
1334

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

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

S
Swen Schillig 已提交
1350 1351 1352
	req->handler = zfcp_fsf_exchange_port_data_handler;
	req->erp_action = erp_action;
	erp_action->fsf_req = req;
1353

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

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

C
Christof Schmitt 已提交
1378
	if (!(adapter->adapter_features & FSF_FEATURE_HBAAPI_MANAGEMENT))
1379 1380
		return -EOPNOTSUPP;

1381
	spin_lock_bh(&adapter->req_q_lock);
1382
	if (zfcp_fsf_req_sbal_get(adapter))
1383
		goto out_unlock;
S
Swen Schillig 已提交
1384 1385 1386

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

1392
	if (data)
S
Swen Schillig 已提交
1393
		req->data = data;
1394

S
Swen Schillig 已提交
1395
	sbale = zfcp_qdio_sbale_req(req);
1396 1397 1398
	sbale[0].flags |= SBAL_FLAGS0_TYPE_READ;
	sbale[1].flags |= SBAL_FLAGS_LAST_ENTRY;

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

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

	return retval;
1410 1411 1412 1413

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

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

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

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

out:
	zfcp_port_put(port);
L
Linus Torvalds 已提交
1488 1489
}

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

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

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

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

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

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

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

S
Swen Schillig 已提交
1543
	if (req->status & ZFCP_STATUS_FSFREQ_ERROR)
1544
		return;
L
Linus Torvalds 已提交
1545

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

S
Swen Schillig 已提交
1561 1562 1563 1564
/**
 * 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 已提交
1565
 */
S
Swen Schillig 已提交
1566
int zfcp_fsf_close_port(struct zfcp_erp_action *erp_action)
L
Linus Torvalds 已提交
1567
{
1568
	struct qdio_buffer_element *sbale;
S
Swen Schillig 已提交
1569 1570 1571 1572
	struct zfcp_adapter *adapter = erp_action->adapter;
	struct zfcp_fsf_req *req;
	int retval = -EIO;

1573
	spin_lock_bh(&adapter->req_q_lock);
S
Swen Schillig 已提交
1574 1575 1576 1577 1578 1579
	if (zfcp_fsf_req_sbal_get(adapter))
		goto out;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

	zfcp_fsf_start_timer(req, ZFCP_FSF_REQUEST_TIMEOUT);
	retval = zfcp_fsf_req_send(req);
	if (retval)
		zfcp_fsf_req_free(req);
out:
1729
	spin_unlock_bh(&adapter->req_q_lock);
1730 1731 1732
	return retval;
}

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

S
Swen Schillig 已提交
1739
	if (req->status & ZFCP_STATUS_FSFREQ_ERROR)
1740
		return;
L
Linus Torvalds 已提交
1741 1742 1743

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

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

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

1795
	spin_lock_bh(&adapter->req_q_lock);
S
Swen Schillig 已提交
1796
	if (zfcp_fsf_req_sbal_get(adapter))
L
Linus Torvalds 已提交
1797 1798
		goto out;

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

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

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

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

S
Swen Schillig 已提交
1828
static void zfcp_fsf_open_unit_handler(struct zfcp_fsf_req *req)
L
Linus Torvalds 已提交
1829
{
S
Swen Schillig 已提交
1830 1831 1832 1833 1834 1835
	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;
1836
	int exclusive, readwrite;
L
Linus Torvalds 已提交
1837

S
Swen Schillig 已提交
1838
	if (req->status & ZFCP_STATUS_FSFREQ_ERROR)
1839
		return;
L
Linus Torvalds 已提交
1840 1841

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

	switch (header->fsf_status) {

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

	case FSF_GOOD:
		unit->handle = header->lun_handle;
		atomic_set_mask(ZFCP_STATUS_COMMON_OPEN, &unit->status);
1906 1907 1908

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

L
Linus Torvalds 已提交
1915 1916 1917 1918 1919 1920 1921
			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 已提交
1922
				dev_info(&adapter->ccw_device->dev,
1923 1924
					 "SCSI device at LUN 0x%016Lx on port "
					 "0x%016Lx opened read-only\n",
1925 1926
					 (unsigned long long)unit->fcp_lun,
					 (unsigned long long)unit->port->wwpn);
L
Linus Torvalds 已提交
1927 1928 1929
        		}

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

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

1967
	spin_lock_bh(&adapter->req_q_lock);
S
Swen Schillig 已提交
1968
	if (zfcp_fsf_req_sbal_get(adapter))
L
Linus Torvalds 已提交
1969 1970
		goto out;

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

	sbale = zfcp_qdio_sbale_req(req);
L
Linus Torvalds 已提交
1980 1981 1982
        sbale[0].flags |= SBAL_FLAGS0_TYPE_READ;
        sbale[1].flags |= SBAL_FLAGS_LAST_ENTRY;

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

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

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

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

S
Swen Schillig 已提交
2008
	if (req->status & ZFCP_STATUS_FSFREQ_ERROR)
2009
		return;
L
Linus Torvalds 已提交
2010

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

2179
	set_msg_byte(scpnt, COMMAND_COMPLETE);
L
Linus Torvalds 已提交
2180 2181 2182

	scpnt->result |= fcp_rsp_iu->scsi_status;

S
Swen Schillig 已提交
2183 2184
	if (req->adapter->adapter_features & FSF_FEATURE_MEASUREMENT_DATA)
		zfcp_fsf_req_latency(req);
2185

S
Stefan Raspl 已提交
2186 2187
	zfcp_fsf_trace_latency(req);

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

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

2203
		memcpy(scpnt->sense_buffer,
L
Linus Torvalds 已提交
2204 2205 2206 2207
		       zfcp_get_fcp_sns_info_ptr(fcp_rsp_iu), sns_len);
	}

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

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

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

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


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

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

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

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

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

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

S
Swen Schillig 已提交
2337 2338 2339 2340
/**
 * 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 已提交
2341
 */
2342 2343
int zfcp_fsf_send_fcp_command_task(struct zfcp_unit *unit,
				   struct scsi_cmnd *scsi_cmnd)
L
Linus Torvalds 已提交
2344
{
S
Swen Schillig 已提交
2345 2346
	struct zfcp_fsf_req *req;
	struct fcp_cmnd_iu *fcp_cmnd_iu;
2347
	unsigned int sbtype = SBAL_FLAGS0_TYPE_READ;
S
Swen Schillig 已提交
2348
	int real_bytes, retval = -EIO;
2349
	struct zfcp_adapter *adapter = unit->port->adapter;
L
Linus Torvalds 已提交
2350

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

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

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

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

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

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

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

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

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

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

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

S
Swen Schillig 已提交
2437 2438 2439
	retval = zfcp_fsf_req_send(req);
	if (unlikely(retval))
		goto failed_scsi_cmnd;
L
Linus Torvalds 已提交
2440

S
Swen Schillig 已提交
2441
	goto out;
L
Linus Torvalds 已提交
2442

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

/**
S
Swen Schillig 已提交
2453 2454 2455 2456
 * 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 已提交
2457
 */
2458
struct zfcp_fsf_req *zfcp_fsf_send_fcp_ctm(struct zfcp_unit *unit, u8 tm_flags)
L
Linus Torvalds 已提交
2459
{
2460
	struct qdio_buffer_element *sbale;
S
Swen Schillig 已提交
2461 2462
	struct zfcp_fsf_req *req = NULL;
	struct fcp_cmnd_iu *fcp_cmnd_iu;
2463
	struct zfcp_adapter *adapter = unit->port->adapter;
L
Linus Torvalds 已提交
2464

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

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

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

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

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

S
Swen Schillig 已提交
2497 2498 2499
	zfcp_fsf_start_timer(req, ZFCP_SCSI_ER_TIMEOUT);
	if (!zfcp_fsf_req_send(req))
		goto out;
L
Linus Torvalds 已提交
2500

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

S
Swen Schillig 已提交
2508 2509
static void zfcp_fsf_control_file_handler(struct zfcp_fsf_req *req)
{
L
Linus Torvalds 已提交
2510 2511
}

S
Swen Schillig 已提交
2512 2513 2514 2515 2516
/**
 * 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 已提交
2517
 */
S
Swen Schillig 已提交
2518 2519
struct zfcp_fsf_req *zfcp_fsf_control_file(struct zfcp_adapter *adapter,
					   struct zfcp_fsf_cfdc *fsf_cfdc)
L
Linus Torvalds 已提交
2520
{
2521
	struct qdio_buffer_element *sbale;
S
Swen Schillig 已提交
2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538
	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 已提交
2539

2540
	spin_lock_bh(&adapter->req_q_lock);
S
Swen Schillig 已提交
2541 2542
	if (zfcp_fsf_req_sbal_get(adapter))
		goto out;
L
Linus Torvalds 已提交
2543

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

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

S
Swen Schillig 已提交
2552 2553
	sbale = zfcp_qdio_sbale_req(req);
	sbale[0].flags |= direction;
2554

S
Swen Schillig 已提交
2555 2556 2557
	bottom = &req->qtcb->bottom.support;
	bottom->operation_subtype = FSF_CFDC_OPERATION_SUBTYPE;
	bottom->option = fsf_cfdc->option;
2558

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

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

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