zfcp_fsf.c 70.1 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
 *
C
Christof Schmitt 已提交
6
 * Copyright IBM Corporation 2002, 2008
L
Linus Torvalds 已提交
7 8 9 10
 */

#include "zfcp_ext.h"

11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
static void zfcp_fsf_request_timeout_handler(unsigned long data)
{
	struct zfcp_adapter *adapter = (struct zfcp_adapter *) data;
	zfcp_erp_adapter_reopen(adapter, ZFCP_STATUS_COMMON_ERP_FAILED, 62,
				NULL);
}

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 已提交
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
/* 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
};

S
Swen Schillig 已提交
53
static const char *zfcp_act_subtable_type[] = {
L
Linus Torvalds 已提交
54 55 56
	"unknown", "OS", "WWPN", "DID", "LUN"
};

C
Christof Schmitt 已提交
57 58
static void zfcp_act_eval_err(struct zfcp_adapter *adapter, u32 table)
{
S
Swen Schillig 已提交
59
	u16 subtable = table >> 16;
C
Christof Schmitt 已提交
60 61
	u16 rule = table & 0xffff;

S
Swen Schillig 已提交
62
	if (subtable && subtable < ARRAY_SIZE(zfcp_act_subtable_type))
C
Christof Schmitt 已提交
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
		dev_warn(&adapter->ccw_device->dev,
			 "Access denied in subtable %s, rule %d.\n",
			 zfcp_act_subtable_type[subtable], rule);
}

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,
		 "Access denied, cannot send command to port 0x%016Lx.\n",
		 port->wwpn);
	zfcp_act_eval_err(req->adapter, header->fsf_status_qual.halfword[0]);
	zfcp_act_eval_err(req->adapter, header->fsf_status_qual.halfword[1]);
	zfcp_erp_port_access_denied(port, 55, req);
	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,
		 "Access denied for unit 0x%016Lx on port 0x%016Lx.\n",
		 unit->fcp_lun, unit->port->wwpn);
	zfcp_act_eval_err(req->adapter, header->fsf_status_qual.halfword[0]);
	zfcp_act_eval_err(req->adapter, header->fsf_status_qual.halfword[1]);
	zfcp_erp_unit_access_denied(unit, 59, req);
	req->status |= ZFCP_STATUS_FSFREQ_ERROR;
}

static void zfcp_fsf_class_not_supp(struct zfcp_fsf_req *req)
{
	dev_err(&req->adapter->ccw_device->dev,
		"Required FC class not supported by adapter, "
		"shutting down adapter.\n");
	zfcp_erp_adapter_shutdown(req->adapter, 0, 123, req);
	req->status |= ZFCP_STATUS_FSFREQ_ERROR;
}

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

S
Swen Schillig 已提交
114 115
	if (req->qtcb) {
		kmem_cache_free(zfcp_data.fsf_req_qtcb_cache, req);
116 117
		return;
	}
L
Linus Torvalds 已提交
118 119
}

S
Swen Schillig 已提交
120 121 122 123
/**
 * zfcp_fsf_req_dismiss_all - dismiss all fsf requests
 * @adapter: pointer to struct zfcp_adapter
 *
124 125 126 127
 * 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 已提交
128
 */
129
void zfcp_fsf_req_dismiss_all(struct zfcp_adapter *adapter)
L
Linus Torvalds 已提交
130
{
S
Swen Schillig 已提交
131
	struct zfcp_fsf_req *req, *tmp;
132
	unsigned long flags;
133
	LIST_HEAD(remove_queue);
134
	unsigned int i;
135

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

S
Swen Schillig 已提交
142 143 144 145
	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 已提交
146 147 148
	}
}

S
Swen Schillig 已提交
149
static void zfcp_fsf_status_read_port_closed(struct zfcp_fsf_req *req)
L
Linus Torvalds 已提交
150
{
S
Swen Schillig 已提交
151 152 153 154 155
	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 已提交
156

S
Swen Schillig 已提交
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
	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);
			switch (sr_buf->status_subtype) {
			case FSF_STATUS_READ_SUB_CLOSE_PHYS_PORT:
				zfcp_erp_port_reopen(port, 0, 101, req);
				break;
			case FSF_STATUS_READ_SUB_ERROR_PORT:
				zfcp_erp_port_shutdown(port, 0, 122, req);
				break;
			}
			return;
		}
	read_unlock_irqrestore(&zfcp_data.config_lock, flags);
L
Linus Torvalds 已提交
172 173
}

S
Swen Schillig 已提交
174
static void zfcp_fsf_bit_error_threshold(struct zfcp_fsf_req *req)
L
Linus Torvalds 已提交
175
{
S
Swen Schillig 已提交
176 177 178
	struct zfcp_adapter *adapter = req->adapter;
	struct fsf_status_read_buffer *sr_buf = req->data;
	struct fsf_bit_error_payload *err = &sr_buf->payload.bit_error;
L
Linus Torvalds 已提交
179

S
Swen Schillig 已提交
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
	dev_warn(&adapter->ccw_device->dev,
		 "Warning: bit error threshold data "
		 "received for the adapter: "
		 "link failures = %i, loss of sync errors = %i, "
		 "loss of signal errors = %i, "
		 "primitive sequence errors = %i, "
		 "invalid transmission word errors = %i, "
		 "CRC errors = %i).\n",
		 err->link_failure_error_count,
		 err->loss_of_sync_error_count,
		 err->loss_of_signal_error_count,
		 err->primitive_sequence_error_count,
		 err->invalid_transmission_word_error_count,
		 err->crc_error_count);
	dev_warn(&adapter->ccw_device->dev,
		 "Additional bit error threshold data of the adapter: "
		 "primitive sequence event time-outs = %i, "
		 "elastic buffer overrun errors = %i, "
		 "advertised receive buffer-to-buffer credit = %i, "
		 "current receice buffer-to-buffer credit = %i, "
		 "advertised transmit buffer-to-buffer credit = %i, "
		 "current transmit buffer-to-buffer credit = %i).\n",
		 err->primitive_sequence_event_timeout_count,
		 err->elastic_buffer_overrun_error_count,
		 err->advertised_receive_b2b_credit,
		 err->current_receive_b2b_credit,
		 err->advertised_transmit_b2b_credit,
		 err->current_transmit_b2b_credit);
L
Linus Torvalds 已提交
208 209
}

S
Swen Schillig 已提交
210 211
static void zfcp_fsf_link_down_info_eval(struct zfcp_fsf_req *req, u8 id,
					 struct fsf_link_down_info *link_down)
212
{
S
Swen Schillig 已提交
213
	struct zfcp_adapter *adapter = req->adapter;
214

S
Swen Schillig 已提交
215
	if (atomic_read(&adapter->status) & ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED)
216 217 218 219
		return;

	atomic_set_mask(ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED, &adapter->status);

S
Swen Schillig 已提交
220
	if (!link_down)
221
		goto out;
222

223 224
	switch (link_down->error_code) {
	case FSF_PSQ_LINK_NO_LIGHT:
S
Swen Schillig 已提交
225 226
		dev_warn(&req->adapter->ccw_device->dev,
			 "The local link is down: no light detected.\n");
227 228
		break;
	case FSF_PSQ_LINK_WRAP_PLUG:
S
Swen Schillig 已提交
229 230
		dev_warn(&req->adapter->ccw_device->dev,
			 "The local link is down: wrap plug detected.\n");
231 232
		break;
	case FSF_PSQ_LINK_NO_FCP:
S
Swen Schillig 已提交
233
		dev_warn(&req->adapter->ccw_device->dev,
C
Christof Schmitt 已提交
234 235
			 "The local link is down: "
			 "adjacent node on link does not support FCP.\n");
236 237
		break;
	case FSF_PSQ_LINK_FIRMWARE_UPDATE:
S
Swen Schillig 已提交
238
		dev_warn(&req->adapter->ccw_device->dev,
C
Christof Schmitt 已提交
239 240 241
			 "The local link is down: "
			 "firmware update in progress.\n");
		break;
242
	case FSF_PSQ_LINK_INVALID_WWPN:
S
Swen Schillig 已提交
243
		dev_warn(&req->adapter->ccw_device->dev,
C
Christof Schmitt 已提交
244 245
			 "The local link is down: "
			 "duplicate or invalid WWPN detected.\n");
246 247
		break;
	case FSF_PSQ_LINK_NO_NPIV_SUPPORT:
S
Swen Schillig 已提交
248
		dev_warn(&req->adapter->ccw_device->dev,
C
Christof Schmitt 已提交
249 250
			 "The local link is down: "
			 "no support for NPIV by Fabric.\n");
251 252
		break;
	case FSF_PSQ_LINK_NO_FCP_RESOURCES:
S
Swen Schillig 已提交
253
		dev_warn(&req->adapter->ccw_device->dev,
C
Christof Schmitt 已提交
254 255
			 "The local link is down: "
			 "out of resource in FCP daughtercard.\n");
256 257
		break;
	case FSF_PSQ_LINK_NO_FABRIC_RESOURCES:
S
Swen Schillig 已提交
258
		dev_warn(&req->adapter->ccw_device->dev,
C
Christof Schmitt 已提交
259 260
			 "The local link is down: "
			 "out of resource in Fabric.\n");
261 262
		break;
	case FSF_PSQ_LINK_FABRIC_LOGIN_UNABLE:
S
Swen Schillig 已提交
263
		dev_warn(&req->adapter->ccw_device->dev,
C
Christof Schmitt 已提交
264 265
			 "The local link is down: "
			 "unable to login to Fabric.\n");
266 267
		break;
	case FSF_PSQ_LINK_WWPN_ASSIGNMENT_CORRUPTED:
S
Swen Schillig 已提交
268
		dev_warn(&req->adapter->ccw_device->dev,
C
Christof Schmitt 已提交
269
			 "WWPN assignment file corrupted on adapter.\n");
270 271
		break;
	case FSF_PSQ_LINK_MODE_TABLE_CURRUPTED:
S
Swen Schillig 已提交
272
		dev_warn(&req->adapter->ccw_device->dev,
C
Christof Schmitt 已提交
273
			 "Mode table corrupted on adapter.\n");
274 275
		break;
	case FSF_PSQ_LINK_NO_WWPN_ASSIGNMENT:
S
Swen Schillig 已提交
276
		dev_warn(&req->adapter->ccw_device->dev,
C
Christof Schmitt 已提交
277
			 "No WWPN for assignment table on adapter.\n");
278 279
		break;
	default:
S
Swen Schillig 已提交
280
		dev_warn(&req->adapter->ccw_device->dev,
C
Christof Schmitt 已提交
281
			 "The local link to adapter is down.\n");
282
	}
S
Swen Schillig 已提交
283 284
out:
	zfcp_erp_adapter_failed(adapter, id, req);
285 286
}

S
Swen Schillig 已提交
287
static void zfcp_fsf_status_read_link_down(struct zfcp_fsf_req *req)
L
Linus Torvalds 已提交
288
{
S
Swen Schillig 已提交
289 290 291 292
	struct zfcp_adapter *adapter = req->adapter;
	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 已提交
293

S
Swen Schillig 已提交
294 295 296 297 298
	switch (sr_buf->status_subtype) {
	case FSF_STATUS_READ_SUB_NO_PHYSICAL_LINK:
		dev_warn(&adapter->ccw_device->dev,
			 "Physical link is down.\n");
		zfcp_fsf_link_down_info_eval(req, 38, ldi);
L
Linus Torvalds 已提交
299
		break;
S
Swen Schillig 已提交
300 301 302 303 304
	case FSF_STATUS_READ_SUB_FDISC_FAILED:
		dev_warn(&adapter->ccw_device->dev,
			 "Local link is down "
			 "due to failed FDISC login.\n");
		zfcp_fsf_link_down_info_eval(req, 39, ldi);
L
Linus Torvalds 已提交
305
		break;
S
Swen Schillig 已提交
306 307 308 309 310 311 312
	case FSF_STATUS_READ_SUB_FIRMWARE_UPDATE:
		dev_warn(&adapter->ccw_device->dev,
			 "Local link is down "
			 "due to firmware update on adapter.\n");
		zfcp_fsf_link_down_info_eval(req, 40, NULL);
	};
}
L
Linus Torvalds 已提交
313

S
Swen Schillig 已提交
314 315 316 317
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 已提交
318

S
Swen Schillig 已提交
319 320 321 322 323 324
	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 已提交
325

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

S
Swen Schillig 已提交
328 329 330
	switch (sr_buf->status_type) {
	case FSF_STATUS_READ_PORT_CLOSED:
		zfcp_fsf_status_read_port_closed(req);
L
Linus Torvalds 已提交
331
		break;
S
Swen Schillig 已提交
332 333
	case FSF_STATUS_READ_INCOMING_ELS:
		zfcp_fc_incoming_els(req);
L
Linus Torvalds 已提交
334
		break;
S
Swen Schillig 已提交
335
	case FSF_STATUS_READ_SENSE_DATA_AVAIL:
L
Linus Torvalds 已提交
336
		break;
S
Swen Schillig 已提交
337 338
	case FSF_STATUS_READ_BIT_ERROR_THRESHOLD:
		zfcp_fsf_bit_error_threshold(req);
L
Linus Torvalds 已提交
339
		break;
S
Swen Schillig 已提交
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
	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,
			 "Local link was replugged.\n");
		/* All ports should be marked as ready to run again */
		zfcp_erp_modify_adapter_status(adapter, 30, NULL,
					       ZFCP_STATUS_COMMON_RUNNING,
					       ZFCP_SET);
		zfcp_erp_adapter_reopen(adapter,
					ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED |
					ZFCP_STATUS_COMMON_ERP_FAILED,
					102, req);
		break;
	case FSF_STATUS_READ_NOTIFICATION_LOST:
		if (sr_buf->status_subtype & FSF_STATUS_READ_SUB_ACT_UPDATED)
			zfcp_erp_adapter_access_changed(adapter, 135, req);
		if (sr_buf->status_subtype & FSF_STATUS_READ_SUB_INCOMING_ELS)
			schedule_work(&adapter->scan_work);
		break;
	case FSF_STATUS_READ_CFDC_UPDATED:
		zfcp_erp_adapter_access_changed(adapter, 136, req);
		break;
	case FSF_STATUS_READ_FEATURE_UPDATE_ALERT:
		adapter->adapter_features = sr_buf->payload.word[0];
L
Linus Torvalds 已提交
366 367 368
		break;
	}

S
Swen Schillig 已提交
369 370
	mempool_free(sr_buf, adapter->pool.data_status_read);
	zfcp_fsf_req_free(req);
L
Linus Torvalds 已提交
371

S
Swen Schillig 已提交
372 373 374
	atomic_inc(&adapter->stat_miss);
	schedule_work(&adapter->stat_work);
}
L
Linus Torvalds 已提交
375

S
Swen Schillig 已提交
376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395
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,
			"No recommendation could be given for a "
			"problem on the adapter.\n");
		zfcp_erp_adapter_shutdown(req->adapter, 0, 121, req);
		break;
	}
	/* all non-return stats set FSFREQ_ERROR*/
	req->status |= ZFCP_STATUS_FSFREQ_ERROR;
L
Linus Torvalds 已提交
396 397
}

S
Swen Schillig 已提交
398
static void zfcp_fsf_fsfstatus_eval(struct zfcp_fsf_req *req)
L
Linus Torvalds 已提交
399
{
S
Swen Schillig 已提交
400 401
	if (unlikely(req->status & ZFCP_STATUS_FSFREQ_ERROR))
		return;
L
Linus Torvalds 已提交
402

S
Swen Schillig 已提交
403 404 405 406 407 408 409 410 411 412 413 414 415 416
	switch (req->qtcb->header.fsf_status) {
	case FSF_UNKNOWN_COMMAND:
		dev_err(&req->adapter->ccw_device->dev,
			"Command issued by the device driver (0x%x) is "
			"not known by the adapter.\n",
			req->qtcb->header.fsf_command);
		zfcp_erp_adapter_shutdown(req->adapter, 0, 120, req);
		req->status |= ZFCP_STATUS_FSFREQ_ERROR;
		break;
	case FSF_ADAPTER_STATUS_AVAILABLE:
		zfcp_fsf_fsfstatus_qual_eval(req);
		break;
	}
}
L
Linus Torvalds 已提交
417

S
Swen Schillig 已提交
418 419 420 421 422
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 已提交
423

S
Swen Schillig 已提交
424
	zfcp_hba_dbf_event_fsf_response(req);
L
Linus Torvalds 已提交
425

S
Swen Schillig 已提交
426 427 428 429 430
	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 已提交
431

S
Swen Schillig 已提交
432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488
	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,
			"The QTCB version requested by zfcp (0x%x) is not "
			"supported by the FCP adapter (lowest supported "
			"0x%x, highest supported 0x%x).\n",
			FSF_QTCB_CURRENT_VERSION, psq->word[0],
			psq->word[1]);
		zfcp_erp_adapter_shutdown(adapter, 0, 117, req);
		break;
	case FSF_PROT_ERROR_STATE:
	case FSF_PROT_SEQ_NUMB_ERROR:
		zfcp_erp_adapter_reopen(adapter, 0, 98, req);
		req->status |= ZFCP_STATUS_FSFREQ_RETRY;
		break;
	case FSF_PROT_UNSUPP_QTCB_TYPE:
		dev_err(&adapter->ccw_device->dev,
			"Packet header type used by the device driver is "
			"incompatible with that used on the adapter.\n");
		zfcp_erp_adapter_shutdown(adapter, 0, 118, req);
		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,
			"The request identifier 0x%Lx is ambiguous.\n",
			(unsigned long long)qtcb->bottom.support.req_handle);
		zfcp_erp_adapter_shutdown(adapter, 0, 78, req);
		break;
	case FSF_PROT_LINK_DOWN:
		zfcp_fsf_link_down_info_eval(req, 37, &psq->link_down_info);
		/* FIXME: reopening adapter now? better wait for link up */
		zfcp_erp_adapter_reopen(adapter, 0, 79, req);
		break;
	case FSF_PROT_REEST_QUEUE:
		/* All ports should be marked as ready to run again */
		zfcp_erp_modify_adapter_status(adapter, 28, NULL,
					       ZFCP_STATUS_COMMON_RUNNING,
					       ZFCP_SET);
		zfcp_erp_adapter_reopen(adapter,
					ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED |
					ZFCP_STATUS_COMMON_ERP_FAILED, 99, req);
		break;
	default:
		dev_err(&adapter->ccw_device->dev,
			"Transfer protocol status information"
			"provided by the adapter (0x%x) "
			"is not compatible with the device driver.\n",
			qtcb->prefix.prot_status);
		zfcp_erp_adapter_shutdown(adapter, 0, 119, req);
	}
	req->status |= ZFCP_STATUS_FSFREQ_ERROR;
L
Linus Torvalds 已提交
489 490
}

S
Swen Schillig 已提交
491 492 493 494 495 496 497 498 499 500
/**
 * 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 已提交
501
{
S
Swen Schillig 已提交
502 503 504 505
	if (unlikely(req->fsf_command == FSF_QTCB_UNSOLICITED_STATUS)) {
		zfcp_fsf_status_read_handler(req);
		return;
	}
L
Linus Torvalds 已提交
506

S
Swen Schillig 已提交
507 508 509 510
	del_timer(&req->timer);
	zfcp_fsf_protstatus_eval(req);
	zfcp_fsf_fsfstatus_eval(req);
	req->handler(req);
L
Linus Torvalds 已提交
511

S
Swen Schillig 已提交
512
	if (req->erp_action)
513
		zfcp_erp_notify(req->erp_action, 0);
S
Swen Schillig 已提交
514
	req->status |= ZFCP_STATUS_FSFREQ_COMPLETED;
L
Linus Torvalds 已提交
515

S
Swen Schillig 已提交
516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531
	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 已提交
532

S
Swen Schillig 已提交
533 534 535 536 537
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 已提交
538

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

S
Swen Schillig 已提交
541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589
	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;
		if (req->erp_action)
			dev_info(&adapter->ccw_device->dev,
				 "Point-to-Point fibrechannel "
				 "configuration detected.\n");
		break;
	case FSF_TOPO_FABRIC:
		fc_host_port_type(shost) = FC_PORTTYPE_NPORT;
		if (req->erp_action)
			dev_info(&adapter->ccw_device->dev,
				 "Switched fabric fibrechannel "
				 "network detected.\n");
		break;
	case FSF_TOPO_AL:
		fc_host_port_type(shost) = FC_PORTTYPE_NLPORT;
		dev_err(&adapter->ccw_device->dev,
			"Unsupported arbitrated loop fibrechannel "
			"topology detected, shutting down "
			"adapter.\n");
		zfcp_erp_adapter_shutdown(adapter, 0, 127, req);
		return -EIO;
	default:
		fc_host_port_type(shost) = FC_PORTTYPE_UNKNOWN;
		dev_err(&adapter->ccw_device->dev,
			"The fibrechannel topology reported by the"
			" adapter is not known by the zfcp driver,"
			" shutting down adapter.\n");
		zfcp_erp_adapter_shutdown(adapter, 0, 128, req);
		return -EIO;
L
Linus Torvalds 已提交
590
	}
S
Swen Schillig 已提交
591

L
Linus Torvalds 已提交
592 593 594
	return 0;
}

S
Swen Schillig 已提交
595
static void zfcp_fsf_exchange_config_data_handler(struct zfcp_fsf_req *req)
C
Christof Schmitt 已提交
596 597
{
	struct zfcp_adapter *adapter = req->adapter;
S
Swen Schillig 已提交
598 599 600
	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 已提交
601

S
Swen Schillig 已提交
602 603
	if (req->status & ZFCP_STATUS_FSFREQ_ERROR)
		return;
L
Linus Torvalds 已提交
604

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

S
Swen Schillig 已提交
612 613 614 615
	switch (qtcb->header.fsf_status) {
	case FSF_GOOD:
		if (zfcp_fsf_exchange_config_evaluate(req))
			return;
L
Linus Torvalds 已提交
616

S
Swen Schillig 已提交
617 618 619 620 621 622 623 624 625 626 627 628
		if (bottom->max_qtcb_size < sizeof(struct fsf_qtcb)) {
			dev_err(&adapter->ccw_device->dev,
				"Maximum QTCB size (%d bytes) allowed by "
				"the adapter is lower than the minimum "
				"required by the driver (%ld bytes).\n",
				bottom->max_qtcb_size,
				sizeof(struct fsf_qtcb));
			zfcp_erp_adapter_shutdown(adapter, 0, 129, req);
			return;
		}
		atomic_set_mask(ZFCP_STATUS_ADAPTER_XCONFIG_OK,
				&adapter->status);
L
Linus Torvalds 已提交
629
		break;
S
Swen Schillig 已提交
630 631 632 633 634 635 636
	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 已提交
637

S
Swen Schillig 已提交
638 639
		atomic_set_mask(ZFCP_STATUS_ADAPTER_XCONFIG_OK,
				&adapter->status);
L
Linus Torvalds 已提交
640

S
Swen Schillig 已提交
641 642
		zfcp_fsf_link_down_info_eval(req, 42,
			&qtcb->header.fsf_status_qual.link_down_info);
L
Linus Torvalds 已提交
643
		break;
S
Swen Schillig 已提交
644 645 646 647
	default:
		zfcp_erp_adapter_shutdown(adapter, 0, 130, req);
		return;
	}
L
Linus Torvalds 已提交
648

S
Swen Schillig 已提交
649 650 651 652 653 654 655
	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 已提交
656

S
Swen Schillig 已提交
657 658 659 660 661 662 663 664 665 666 667 668 669 670
	if (FSF_QTCB_CURRENT_VERSION < bottom->low_qtcb_version) {
		dev_err(&adapter->ccw_device->dev,
			"The adapter only supports newer control block "
			"versions, try updated device driver.\n");
		zfcp_erp_adapter_shutdown(adapter, 0, 125, req);
		return;
	}
	if (FSF_QTCB_CURRENT_VERSION > bottom->high_qtcb_version) {
		dev_err(&adapter->ccw_device->dev,
			"The adapter only supports older control block "
			"versions, consider a microcode upgrade.\n");
		zfcp_erp_adapter_shutdown(adapter, 0, 126, req);
	}
}
L
Linus Torvalds 已提交
671

S
Swen Schillig 已提交
672 673 674 675 676
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 已提交
677

S
Swen Schillig 已提交
678 679
	if (req->data)
		memcpy(req->data, bottom, sizeof(*bottom));
680

S
Swen Schillig 已提交
681 682 683 684 685 686 687
	if (adapter->connection_features & FSF_FEATURE_NPIV_MODE)
		fc_host_permanent_port_name(shost) = bottom->wwpn;
	else
		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 已提交
688

S
Swen Schillig 已提交
689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706
static void zfcp_fsf_exchange_port_data_handler(struct zfcp_fsf_req *req)
{
	struct zfcp_adapter *adapter = req->adapter;
	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);
		atomic_set_mask(ZFCP_STATUS_ADAPTER_XPORT_OK, &adapter->status);
		break;
	case FSF_EXCHANGE_CONFIG_DATA_INCOMPLETE:
		zfcp_fsf_exchange_port_evaluate(req);
		atomic_set_mask(ZFCP_STATUS_ADAPTER_XPORT_OK, &adapter->status);
		zfcp_fsf_link_down_info_eval(req, 43,
			&qtcb->header.fsf_status_qual.link_down_info);
707
		break;
L
Linus Torvalds 已提交
708
	}
S
Swen Schillig 已提交
709
}
710

S
Swen Schillig 已提交
711 712
static int zfcp_fsf_sbal_check(struct zfcp_qdio_queue *queue)
{
713
	spin_lock_bh(&queue->lock);
S
Swen Schillig 已提交
714 715
	if (atomic_read(&queue->count))
		return 1;
716
	spin_unlock_bh(&queue->lock);
S
Swen Schillig 已提交
717
	return 0;
L
Linus Torvalds 已提交
718 719
}

S
Swen Schillig 已提交
720 721 722 723 724
static int zfcp_fsf_req_sbal_get(struct zfcp_adapter *adapter)
{
	long ret;
	struct zfcp_qdio_queue *req_q = &adapter->req_q;

725
	spin_unlock_bh(&req_q->lock);
S
Swen Schillig 已提交
726 727 728 729 730
	ret = wait_event_interruptible_timeout(adapter->request_wq,
					zfcp_fsf_sbal_check(req_q), 5 * HZ);
	if (ret > 0)
		return 0;

731
	spin_lock_bh(&req_q->lock);
S
Swen Schillig 已提交
732 733 734 735 736 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 764 765 766
	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));
	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 已提交
767 768 769
{
	volatile struct qdio_buffer_element *sbale;

S
Swen Schillig 已提交
770 771
	struct zfcp_fsf_req *req;
	struct zfcp_qdio_queue *req_q = &adapter->req_q;
772

S
Swen Schillig 已提交
773 774 775 776
	if (req_flags & ZFCP_REQ_NO_QTCB)
		req = zfcp_fsf_alloc_noqtcb(pool);
	else
		req = zfcp_fsf_alloc_qtcb(pool);
L
Linus Torvalds 已提交
777

S
Swen Schillig 已提交
778 779
	if (unlikely(!req))
		return ERR_PTR(-EIO);
L
Linus Torvalds 已提交
780

S
Swen Schillig 已提交
781 782
	if (adapter->req_no == 0)
		adapter->req_no++;
L
Linus Torvalds 已提交
783

S
Swen Schillig 已提交
784 785 786
	INIT_LIST_HEAD(&req->list);
	init_timer(&req->timer);
	init_waitqueue_head(&req->completion_wq);
L
Linus Torvalds 已提交
787

S
Swen Schillig 已提交
788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817
	req->adapter = adapter;
	req->fsf_command = fsf_cmd;
	req->req_id = adapter->req_no++;
	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);
	}
818

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

S
Swen Schillig 已提交
822
	return req;
L
Linus Torvalds 已提交
823 824
}

S
Swen Schillig 已提交
825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864
static int zfcp_fsf_req_send(struct zfcp_fsf_req *req)
{
	struct zfcp_adapter *adapter = req->adapter;
	struct zfcp_qdio_queue *req_q = &adapter->req_q;
	int idx;

	/* put allocated FSF request into hash table */
	spin_lock(&adapter->req_list_lock);
	idx = zfcp_reqlist_hash(req->req_id);
	list_add_tail(&req->list, &adapter->req_list[idx]);
	spin_unlock(&adapter->req_list_lock);

	req->issued = get_clock();
	if (zfcp_qdio_send(req)) {
		/* Queues are down..... */
		del_timer(&req->timer);
		spin_lock(&adapter->req_list_lock);
		zfcp_reqlist_remove(adapter, req);
		spin_unlock(&adapter->req_list_lock);
		/* undo changes in request queue made for this request */
		atomic_add(req->sbal_number, &req_q->count);
		req_q->first -= req->sbal_number;
		req_q->first += QDIO_MAX_BUFFERS_PER_Q;
		req_q->first %= QDIO_MAX_BUFFERS_PER_Q; /* wrap */
		zfcp_erp_adapter_reopen(adapter, 0, 116, req);
		return -EIO;
	}

	/* Don't increase for unsolicited status */
	if (req->qtcb)
		adapter->fsf_req_seq_no++;

	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 已提交
865
 */
S
Swen Schillig 已提交
866
int zfcp_fsf_status_read(struct zfcp_adapter *adapter)
L
Linus Torvalds 已提交
867
{
S
Swen Schillig 已提交
868 869 870 871
	struct zfcp_fsf_req *req;
	struct fsf_status_read_buffer *sr_buf;
	volatile struct qdio_buffer_element *sbale;
	int retval = -EIO;
L
Linus Torvalds 已提交
872

873
	spin_lock_bh(&adapter->req_q.lock);
S
Swen Schillig 已提交
874 875 876 877 878 879
	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);
880
	if (IS_ERR(req)) {
S
Swen Schillig 已提交
881 882
		retval = PTR_ERR(req);
		goto out;
L
Linus Torvalds 已提交
883 884
	}

S
Swen Schillig 已提交
885 886 887 888 889 890 891 892 893 894 895 896 897 898 899
	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);
900

S
Swen Schillig 已提交
901 902 903
	retval = zfcp_fsf_req_send(req);
	if (retval)
		goto failed_req_send;
L
Linus Torvalds 已提交
904

S
Swen Schillig 已提交
905 906 907 908 909 910 911 912
	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:
913
	spin_unlock_bh(&adapter->req_q.lock);
S
Swen Schillig 已提交
914 915 916 917 918 919 920 921 922 923 924 925
	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 已提交
926
	case FSF_PORT_HANDLE_NOT_VALID:
S
Swen Schillig 已提交
927
		if (fsq->word[0] == fsq->word[1]) {
928
			zfcp_erp_adapter_reopen(unit->port->adapter, 0, 104,
S
Swen Schillig 已提交
929 930
						req);
			req->status |= ZFCP_STATUS_FSFREQ_ERROR;
L
Linus Torvalds 已提交
931 932 933
		}
		break;
	case FSF_LUN_HANDLE_NOT_VALID:
S
Swen Schillig 已提交
934 935 936
		if (fsq->word[0] == fsq->word[1]) {
			zfcp_erp_port_reopen(unit->port, 0, 105, req);
			req->status |= ZFCP_STATUS_FSFREQ_ERROR;
L
Linus Torvalds 已提交
937 938 939
		}
		break;
	case FSF_FCP_COMMAND_DOES_NOT_EXIST:
S
Swen Schillig 已提交
940
		req->status |= ZFCP_STATUS_FSFREQ_ABORTNOTNEEDED;
L
Linus Torvalds 已提交
941 942
		break;
	case FSF_PORT_BOXED:
S
Swen Schillig 已提交
943 944 945
		zfcp_erp_port_boxed(unit->port, 47, req);
		req->status |= ZFCP_STATUS_FSFREQ_ERROR |
			       ZFCP_STATUS_FSFREQ_RETRY;
L
Linus Torvalds 已提交
946 947
		break;
	case FSF_LUN_BOXED:
S
Swen Schillig 已提交
948 949 950
		zfcp_erp_unit_boxed(unit, 48, req);
		req->status |= ZFCP_STATUS_FSFREQ_ERROR |
			       ZFCP_STATUS_FSFREQ_RETRY;
L
Linus Torvalds 已提交
951 952
                break;
	case FSF_ADAPTER_STATUS_AVAILABLE:
S
Swen Schillig 已提交
953
		switch (fsq->word[0]) {
L
Linus Torvalds 已提交
954
		case FSF_SQ_INVOKE_LINK_TEST_PROCEDURE:
955
			zfcp_test_link(unit->port);
L
Linus Torvalds 已提交
956
		case FSF_SQ_ULP_DEPENDENT_ERP_REQUIRED:
S
Swen Schillig 已提交
957
			req->status |= ZFCP_STATUS_FSFREQ_ERROR;
L
Linus Torvalds 已提交
958 959 960 961
			break;
		}
		break;
	case FSF_GOOD:
S
Swen Schillig 已提交
962
		req->status |= ZFCP_STATUS_FSFREQ_ABORTSUCCEEDED;
L
Linus Torvalds 已提交
963 964 965 966 967
		break;
	}
}

/**
S
Swen Schillig 已提交
968 969 970 971 972 973 974 975
 * zfcp_fsf_abort_fcp_command - abort running SCSI command
 * @old_req_id: unsigned long
 * @adapter: pointer to struct zfcp_adapter
 * @unit: pointer to struct zfcp_unit
 * @req_flags: integer specifying the request flags
 * Returns: pointer to struct zfcp_fsf_req
 *
 * FIXME(design): should be watched by a timeout !!!
L
Linus Torvalds 已提交
976 977
 */

S
Swen Schillig 已提交
978 979 980 981
struct zfcp_fsf_req *zfcp_fsf_abort_fcp_command(unsigned long old_req_id,
						struct zfcp_adapter *adapter,
						struct zfcp_unit *unit,
						int req_flags)
L
Linus Torvalds 已提交
982 983
{
	volatile struct qdio_buffer_element *sbale;
S
Swen Schillig 已提交
984
	struct zfcp_fsf_req *req = NULL;
985

S
Swen Schillig 已提交
986 987 988 989 990
	spin_lock(&adapter->req_q.lock);
	if (!atomic_read(&adapter->req_q.count))
		goto out;
	req = zfcp_fsf_req_create(adapter, FSF_QTCB_ABORT_FCP_CMND,
				  req_flags, adapter->pool.fsf_req_abort);
991
	if (IS_ERR(req))
S
Swen Schillig 已提交
992
		goto out;
993

S
Swen Schillig 已提交
994 995 996
	if (unlikely(!(atomic_read(&unit->status) &
		       ZFCP_STATUS_COMMON_UNBLOCKED)))
		goto out_error_free;
L
Linus Torvalds 已提交
997

S
Swen Schillig 已提交
998 999 1000
	sbale = zfcp_qdio_sbale_req(req);
	sbale[0].flags |= SBAL_FLAGS0_TYPE_READ;
	sbale[1].flags |= SBAL_FLAGS_LAST_ENTRY;
L
Linus Torvalds 已提交
1001

S
Swen Schillig 已提交
1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017
	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:
	spin_unlock(&adapter->req_q.lock);
	return req;
L
Linus Torvalds 已提交
1018 1019
}

S
Swen Schillig 已提交
1020
static void zfcp_fsf_send_ct_handler(struct zfcp_fsf_req *req)
L
Linus Torvalds 已提交
1021
{
S
Swen Schillig 已提交
1022 1023 1024 1025
	struct zfcp_adapter *adapter = req->adapter;
	struct zfcp_send_ct *send_ct = req->data;
	struct zfcp_port *port = send_ct->port;
	struct fsf_qtcb_header *header = &req->qtcb->header;
L
Linus Torvalds 已提交
1026

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

S
Swen Schillig 已提交
1029
	if (req->status & ZFCP_STATUS_FSFREQ_ERROR)
L
Linus Torvalds 已提交
1030 1031 1032 1033
		goto skip_fsfstatus;

	switch (header->fsf_status) {
        case FSF_GOOD:
S
Swen Schillig 已提交
1034 1035
		zfcp_san_dbf_event_ct_response(req);
		send_ct->status = 0;
L
Linus Torvalds 已提交
1036 1037
		break;
        case FSF_SERVICE_CLASS_NOT_SUPPORTED:
S
Swen Schillig 已提交
1038
		zfcp_fsf_class_not_supp(req);
L
Linus Torvalds 已提交
1039 1040 1041 1042 1043 1044
		break;
        case FSF_ADAPTER_STATUS_AVAILABLE:
                switch (header->fsf_status_qual.word[0]){
                case FSF_SQ_INVOKE_LINK_TEST_PROCEDURE:
			zfcp_test_link(port);
                case FSF_SQ_ULP_DEPENDENT_ERP_REQUIRED:
S
Swen Schillig 已提交
1045
			req->status |= ZFCP_STATUS_FSFREQ_ERROR;
L
Linus Torvalds 已提交
1046 1047 1048 1049
			break;
                }
                break;
	case FSF_ACCESS_DENIED:
S
Swen Schillig 已提交
1050
		zfcp_fsf_access_denied_port(req, port);
L
Linus Torvalds 已提交
1051 1052
		break;
        case FSF_PORT_BOXED:
S
Swen Schillig 已提交
1053 1054 1055
		zfcp_erp_port_boxed(port, 49, req);
		req->status |= ZFCP_STATUS_FSFREQ_ERROR |
			       ZFCP_STATUS_FSFREQ_RETRY;
L
Linus Torvalds 已提交
1056
		break;
S
Swen Schillig 已提交
1057 1058 1059
	case FSF_PORT_HANDLE_NOT_VALID:
		zfcp_erp_adapter_reopen(adapter, 0, 106, req);
	case FSF_GENERIC_COMMAND_REJECTED:
L
Linus Torvalds 已提交
1060 1061 1062 1063
	case FSF_PAYLOAD_SIZE_MISMATCH:
	case FSF_REQUEST_SIZE_TOO_LARGE:
	case FSF_RESPONSE_SIZE_TOO_LARGE:
	case FSF_SBAL_MISMATCH:
S
Swen Schillig 已提交
1064
		req->status |= ZFCP_STATUS_FSFREQ_ERROR;
L
Linus Torvalds 已提交
1065 1066 1067 1068
		break;
	}

skip_fsfstatus:
S
Swen Schillig 已提交
1069
	if (send_ct->handler)
L
Linus Torvalds 已提交
1070
		send_ct->handler(send_ct->handler_data);
S
Swen Schillig 已提交
1071
}
L
Linus Torvalds 已提交
1072

S
Swen Schillig 已提交
1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092
static int zfcp_fsf_setup_sbals(struct zfcp_fsf_req *req,
				struct scatterlist *sg_req,
				struct scatterlist *sg_resp, int max_sbals)
{
	int bytes;

	bytes = zfcp_qdio_sbals_from_sg(req, SBAL_FLAGS0_TYPE_WRITE_READ,
					sg_req, max_sbals);
	if (bytes <= 0)
		return -ENOMEM;
	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)
		return -ENOMEM;
	req->qtcb->bottom.support.resp_buf_length = bytes;

	return 0;
L
Linus Torvalds 已提交
1093 1094 1095
}

/**
S
Swen Schillig 已提交
1096 1097 1098 1099
 * 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 已提交
1100
 */
S
Swen Schillig 已提交
1101 1102
int zfcp_fsf_send_ct(struct zfcp_send_ct *ct, mempool_t *pool,
		     struct zfcp_erp_action *erp_action)
L
Linus Torvalds 已提交
1103
{
S
Swen Schillig 已提交
1104 1105 1106 1107
	struct zfcp_port *port = ct->port;
	struct zfcp_adapter *adapter = port->adapter;
	struct zfcp_fsf_req *req;
	int ret = -EIO;
L
Linus Torvalds 已提交
1108

1109
	spin_lock_bh(&adapter->req_q.lock);
S
Swen Schillig 已提交
1110 1111
	if (zfcp_fsf_req_sbal_get(adapter))
		goto out;
L
Linus Torvalds 已提交
1112

S
Swen Schillig 已提交
1113 1114
	req = zfcp_fsf_req_create(adapter, FSF_QTCB_SEND_GENERIC,
				  ZFCP_REQ_AUTO_CLEANUP, pool);
1115
	if (IS_ERR(req)) {
S
Swen Schillig 已提交
1116 1117
		ret = PTR_ERR(req);
		goto out;
1118 1119
	}

S
Swen Schillig 已提交
1120 1121
	ret = zfcp_fsf_setup_sbals(req, ct->req, ct->resp,
				   FSF_MAX_SBALS_PER_REQ);
C
Christof Schmitt 已提交
1122
	if (ret)
L
Linus Torvalds 已提交
1123 1124
		goto failed_send;

S
Swen Schillig 已提交
1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135
	req->handler = zfcp_fsf_send_ct_handler;
	req->qtcb->header.port_handle = port->handle;
	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;
1136
		zfcp_fsf_start_erp_timer(req);
S
Swen Schillig 已提交
1137 1138
	} else
		zfcp_fsf_start_timer(req, ZFCP_FSF_REQUEST_TIMEOUT);
L
Linus Torvalds 已提交
1139

S
Swen Schillig 已提交
1140 1141 1142
	ret = zfcp_fsf_req_send(req);
	if (ret)
		goto failed_send;
L
Linus Torvalds 已提交
1143

S
Swen Schillig 已提交
1144
	goto out;
L
Linus Torvalds 已提交
1145

S
Swen Schillig 已提交
1146 1147 1148 1149 1150
failed_send:
	zfcp_fsf_req_free(req);
	if (erp_action)
		erp_action->fsf_req = NULL;
out:
1151
	spin_unlock_bh(&adapter->req_q.lock);
S
Swen Schillig 已提交
1152
	return ret;
L
Linus Torvalds 已提交
1153 1154
}

S
Swen Schillig 已提交
1155
static void zfcp_fsf_send_els_handler(struct zfcp_fsf_req *req)
L
Linus Torvalds 已提交
1156
{
S
Swen Schillig 已提交
1157 1158 1159
	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 已提交
1160

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

S
Swen Schillig 已提交
1163
	if (req->status & ZFCP_STATUS_FSFREQ_ERROR)
L
Linus Torvalds 已提交
1164 1165 1166 1167
		goto skip_fsfstatus;

	switch (header->fsf_status) {
	case FSF_GOOD:
S
Swen Schillig 已提交
1168 1169
		zfcp_san_dbf_event_els_response(req);
		send_els->status = 0;
L
Linus Torvalds 已提交
1170 1171
		break;
	case FSF_SERVICE_CLASS_NOT_SUPPORTED:
S
Swen Schillig 已提交
1172
		zfcp_fsf_class_not_supp(req);
L
Linus Torvalds 已提交
1173 1174 1175 1176
		break;
	case FSF_ADAPTER_STATUS_AVAILABLE:
		switch (header->fsf_status_qual.word[0]){
		case FSF_SQ_INVOKE_LINK_TEST_PROCEDURE:
1177 1178
			if (port && (send_els->ls_code != ZFCP_LS_ADISC))
				zfcp_test_link(port);
S
Swen Schillig 已提交
1179
			/*fall through */
L
Linus Torvalds 已提交
1180 1181
		case FSF_SQ_ULP_DEPENDENT_ERP_REQUIRED:
		case FSF_SQ_RETRY_IF_POSSIBLE:
S
Swen Schillig 已提交
1182
			req->status |= ZFCP_STATUS_FSFREQ_ERROR;
L
Linus Torvalds 已提交
1183 1184 1185 1186 1187 1188 1189 1190 1191
			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:
S
Swen Schillig 已提交
1192
		zfcp_fsf_access_denied_port(req, port);
L
Linus Torvalds 已提交
1193
		break;
S
Swen Schillig 已提交
1194 1195 1196
	case FSF_SBAL_MISMATCH:
		/* should never occure, avoided in zfcp_fsf_send_els */
		/* fall through */
L
Linus Torvalds 已提交
1197
	default:
S
Swen Schillig 已提交
1198
		req->status |= ZFCP_STATUS_FSFREQ_ERROR;
L
Linus Torvalds 已提交
1199 1200 1201
		break;
	}
skip_fsfstatus:
H
Heiko Carstens 已提交
1202
	if (send_els->handler)
L
Linus Torvalds 已提交
1203
		send_els->handler(send_els->handler_data);
S
Swen Schillig 已提交
1204
}
L
Linus Torvalds 已提交
1205

S
Swen Schillig 已提交
1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225
/**
 * 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;

	if (unlikely(!(atomic_read(&els->port->status) &
		       ZFCP_STATUS_COMMON_UNBLOCKED)))
		return -EBUSY;

	spin_lock(&adapter->req_q.lock);
	if (!atomic_read(&adapter->req_q.count))
		goto out;
	req = zfcp_fsf_req_create(adapter, FSF_QTCB_SEND_ELS,
				  ZFCP_REQ_AUTO_CLEANUP, NULL);
1226
	if (IS_ERR(req)) {
S
Swen Schillig 已提交
1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256
		ret = PTR_ERR(req);
		goto out;
	}

	ret = zfcp_fsf_setup_sbals(req, els->req, els->resp,
				   FSF_MAX_SBALS_PER_ELS_REQ);
	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:
	spin_unlock(&adapter->req_q.lock);
	return ret;
L
Linus Torvalds 已提交
1257 1258
}

S
Swen Schillig 已提交
1259
int zfcp_fsf_exchange_config_data(struct zfcp_erp_action *erp_action)
L
Linus Torvalds 已提交
1260 1261
{
	volatile struct qdio_buffer_element *sbale;
S
Swen Schillig 已提交
1262
	struct zfcp_fsf_req *req;
1263
	struct zfcp_adapter *adapter = erp_action->adapter;
S
Swen Schillig 已提交
1264 1265
	int retval = -EIO;

1266
	spin_lock_bh(&adapter->req_q.lock);
S
Swen Schillig 已提交
1267 1268 1269 1270 1271 1272
	if (!atomic_read(&adapter->req_q.count))
		goto out;
	req = zfcp_fsf_req_create(adapter,
				  FSF_QTCB_EXCHANGE_CONFIG_DATA,
				  ZFCP_REQ_AUTO_CLEANUP,
				  adapter->pool.fsf_req_erp);
1273
	if (IS_ERR(req)) {
S
Swen Schillig 已提交
1274 1275
		retval = PTR_ERR(req);
		goto out;
L
Linus Torvalds 已提交
1276 1277
	}

S
Swen Schillig 已提交
1278
	sbale = zfcp_qdio_sbale_req(req);
1279 1280
	sbale[0].flags |= SBAL_FLAGS0_TYPE_READ;
	sbale[1].flags |= SBAL_FLAGS_LAST_ENTRY;
L
Linus Torvalds 已提交
1281

S
Swen Schillig 已提交
1282
	req->qtcb->bottom.config.feature_selection =
1283 1284
			FSF_FEATURE_CFDC |
			FSF_FEATURE_LUN_SHARING |
1285
			FSF_FEATURE_NOTIFICATION_LOST |
1286
			FSF_FEATURE_UPDATE_ALERT;
S
Swen Schillig 已提交
1287 1288 1289
	req->erp_action = erp_action;
	req->handler = zfcp_fsf_exchange_config_data_handler;
	erp_action->fsf_req = req;
L
Linus Torvalds 已提交
1290

1291
	zfcp_fsf_start_erp_timer(req);
S
Swen Schillig 已提交
1292
	retval = zfcp_fsf_req_send(req);
L
Linus Torvalds 已提交
1293
	if (retval) {
S
Swen Schillig 已提交
1294
		zfcp_fsf_req_free(req);
L
Linus Torvalds 已提交
1295 1296
		erp_action->fsf_req = NULL;
	}
S
Swen Schillig 已提交
1297
out:
1298
	spin_unlock_bh(&adapter->req_q.lock);
1299 1300
	return retval;
}
L
Linus Torvalds 已提交
1301

S
Swen Schillig 已提交
1302 1303
int zfcp_fsf_exchange_config_data_sync(struct zfcp_adapter *adapter,
				       struct fsf_qtcb_bottom_config *data)
1304 1305
{
	volatile struct qdio_buffer_element *sbale;
S
Swen Schillig 已提交
1306 1307 1308
	struct zfcp_fsf_req *req = NULL;
	int retval = -EIO;

1309
	spin_lock_bh(&adapter->req_q.lock);
S
Swen Schillig 已提交
1310 1311 1312 1313 1314
	if (zfcp_fsf_req_sbal_get(adapter))
		goto out;

	req = zfcp_fsf_req_create(adapter, FSF_QTCB_EXCHANGE_CONFIG_DATA,
				  0, NULL);
1315
	if (IS_ERR(req)) {
S
Swen Schillig 已提交
1316 1317
		retval = PTR_ERR(req);
		goto out;
1318 1319
	}

S
Swen Schillig 已提交
1320
	sbale = zfcp_qdio_sbale_req(req);
1321 1322
	sbale[0].flags |= SBAL_FLAGS0_TYPE_READ;
	sbale[1].flags |= SBAL_FLAGS_LAST_ENTRY;
S
Swen Schillig 已提交
1323
	req->handler = zfcp_fsf_exchange_config_data_handler;
1324

S
Swen Schillig 已提交
1325
	req->qtcb->bottom.config.feature_selection =
1326 1327 1328 1329 1330 1331
			FSF_FEATURE_CFDC |
			FSF_FEATURE_LUN_SHARING |
			FSF_FEATURE_NOTIFICATION_LOST |
			FSF_FEATURE_UPDATE_ALERT;

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

S
Swen Schillig 已提交
1334 1335 1336
	zfcp_fsf_start_timer(req, ZFCP_FSF_REQUEST_TIMEOUT);
	retval = zfcp_fsf_req_send(req);
out:
1337
	spin_unlock_bh(&adapter->req_q.lock);
C
Christof Schmitt 已提交
1338
	if (!retval)
S
Swen Schillig 已提交
1339 1340
		wait_event(req->completion_wq,
			   req->status & ZFCP_STATUS_FSFREQ_COMPLETED);
1341

S
Swen Schillig 已提交
1342
	zfcp_fsf_req_free(req);
1343

L
Linus Torvalds 已提交
1344 1345 1346 1347 1348
	return retval;
}

/**
 * zfcp_fsf_exchange_port_data - request information about local port
1349
 * @erp_action: ERP action for the adapter for which port data is requested
S
Swen Schillig 已提交
1350
 * Returns: 0 on success, error otherwise
L
Linus Torvalds 已提交
1351
 */
S
Swen Schillig 已提交
1352
int zfcp_fsf_exchange_port_data(struct zfcp_erp_action *erp_action)
L
Linus Torvalds 已提交
1353 1354
{
	volatile struct qdio_buffer_element *sbale;
S
Swen Schillig 已提交
1355
	struct zfcp_fsf_req *req;
1356
	struct zfcp_adapter *adapter = erp_action->adapter;
S
Swen Schillig 已提交
1357
	int retval = -EIO;
L
Linus Torvalds 已提交
1358

C
Christof Schmitt 已提交
1359
	if (!(adapter->adapter_features & FSF_FEATURE_HBAAPI_MANAGEMENT))
1360
		return -EOPNOTSUPP;
L
Linus Torvalds 已提交
1361

1362
	spin_lock_bh(&adapter->req_q.lock);
S
Swen Schillig 已提交
1363 1364 1365 1366 1367
	if (!atomic_read(&adapter->req_q.count))
		goto out;
	req = zfcp_fsf_req_create(adapter, FSF_QTCB_EXCHANGE_PORT_DATA,
				  ZFCP_REQ_AUTO_CLEANUP,
				  adapter->pool.fsf_req_erp);
1368
	if (IS_ERR(req)) {
S
Swen Schillig 已提交
1369 1370
		retval = PTR_ERR(req);
		goto out;
1371 1372
	}

S
Swen Schillig 已提交
1373
	sbale = zfcp_qdio_sbale_req(req);
1374 1375
	sbale[0].flags |= SBAL_FLAGS0_TYPE_READ;
	sbale[1].flags |= SBAL_FLAGS_LAST_ENTRY;
L
Linus Torvalds 已提交
1376

S
Swen Schillig 已提交
1377 1378 1379
	req->handler = zfcp_fsf_exchange_port_data_handler;
	req->erp_action = erp_action;
	erp_action->fsf_req = req;
1380

1381
	zfcp_fsf_start_erp_timer(req);
S
Swen Schillig 已提交
1382
	retval = zfcp_fsf_req_send(req);
L
Linus Torvalds 已提交
1383
	if (retval) {
S
Swen Schillig 已提交
1384
		zfcp_fsf_req_free(req);
1385 1386
		erp_action->fsf_req = NULL;
	}
S
Swen Schillig 已提交
1387
out:
1388
	spin_unlock_bh(&adapter->req_q.lock);
1389 1390 1391 1392 1393
	return retval;
}

/**
 * zfcp_fsf_exchange_port_data_sync - request information about local port
S
Swen Schillig 已提交
1394 1395 1396
 * @adapter: pointer to struct zfcp_adapter
 * @data: pointer to struct fsf_qtcb_bottom_port
 * Returns: 0 on success, error otherwise
1397
 */
S
Swen Schillig 已提交
1398 1399
int zfcp_fsf_exchange_port_data_sync(struct zfcp_adapter *adapter,
				     struct fsf_qtcb_bottom_port *data)
1400 1401
{
	volatile struct qdio_buffer_element *sbale;
S
Swen Schillig 已提交
1402 1403
	struct zfcp_fsf_req *req = NULL;
	int retval = -EIO;
1404

C
Christof Schmitt 已提交
1405
	if (!(adapter->adapter_features & FSF_FEATURE_HBAAPI_MANAGEMENT))
1406 1407
		return -EOPNOTSUPP;

1408
	spin_lock_bh(&adapter->req_q.lock);
S
Swen Schillig 已提交
1409 1410 1411 1412 1413
	if (!atomic_read(&adapter->req_q.count))
		goto out;

	req = zfcp_fsf_req_create(adapter, FSF_QTCB_EXCHANGE_PORT_DATA, 0,
				  NULL);
1414
	if (IS_ERR(req)) {
S
Swen Schillig 已提交
1415 1416
		retval = PTR_ERR(req);
		goto out;
L
Linus Torvalds 已提交
1417 1418
	}

1419
	if (data)
S
Swen Schillig 已提交
1420
		req->data = data;
1421

S
Swen Schillig 已提交
1422
	sbale = zfcp_qdio_sbale_req(req);
1423 1424 1425
	sbale[0].flags |= SBAL_FLAGS0_TYPE_READ;
	sbale[1].flags |= SBAL_FLAGS_LAST_ENTRY;

S
Swen Schillig 已提交
1426 1427 1428 1429
	req->handler = zfcp_fsf_exchange_port_data_handler;
	zfcp_fsf_start_timer(req, ZFCP_FSF_REQUEST_TIMEOUT);
	retval = zfcp_fsf_req_send(req);
out:
1430
	spin_unlock_bh(&adapter->req_q.lock);
C
Christof Schmitt 已提交
1431
	if (!retval)
S
Swen Schillig 已提交
1432 1433 1434
		wait_event(req->completion_wq,
			   req->status & ZFCP_STATUS_FSFREQ_COMPLETED);
	zfcp_fsf_req_free(req);
L
Linus Torvalds 已提交
1435 1436 1437 1438

	return retval;
}

S
Swen Schillig 已提交
1439
static void zfcp_fsf_open_port_handler(struct zfcp_fsf_req *req)
L
Linus Torvalds 已提交
1440
{
S
Swen Schillig 已提交
1441 1442
	struct zfcp_port *port = req->data;
	struct fsf_qtcb_header *header = &req->qtcb->header;
L
Linus Torvalds 已提交
1443 1444
	struct fsf_plogi *plogi;

S
Swen Schillig 已提交
1445
	if (req->status & ZFCP_STATUS_FSFREQ_ERROR)
L
Linus Torvalds 已提交
1446 1447 1448 1449 1450 1451
		goto skip_fsfstatus;

	switch (header->fsf_status) {
	case FSF_PORT_ALREADY_OPEN:
		break;
	case FSF_ACCESS_DENIED:
S
Swen Schillig 已提交
1452
		zfcp_fsf_access_denied_port(req, port);
L
Linus Torvalds 已提交
1453 1454
		break;
	case FSF_MAXIMUM_NUMBER_OF_PORTS_EXCEEDED:
S
Swen Schillig 已提交
1455
		dev_warn(&req->adapter->ccw_device->dev,
C
Christof Schmitt 已提交
1456 1457 1458
			 "The adapter is out of resources. The remote port "
			 "0x%016Lx could not be opened, disabling it.\n",
			 port->wwpn);
S
Swen Schillig 已提交
1459 1460
		zfcp_erp_port_failed(port, 31, req);
		req->status |= ZFCP_STATUS_FSFREQ_ERROR;
L
Linus Torvalds 已提交
1461 1462 1463 1464 1465
		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 已提交
1466
			req->status |= ZFCP_STATUS_FSFREQ_ERROR;
L
Linus Torvalds 已提交
1467 1468
			break;
		case FSF_SQ_NO_RETRY_POSSIBLE:
S
Swen Schillig 已提交
1469
			dev_warn(&req->adapter->ccw_device->dev,
C
Christof Schmitt 已提交
1470 1471
				 "The remote port 0x%016Lx could not be "
				 "opened. Disabling it.\n", port->wwpn);
S
Swen Schillig 已提交
1472 1473
			zfcp_erp_port_failed(port, 32, req);
			req->status |= ZFCP_STATUS_FSFREQ_ERROR;
L
Linus Torvalds 已提交
1474 1475 1476 1477 1478 1479 1480
			break;
		}
		break;
	case FSF_GOOD:
		port->handle = header->port_handle;
		atomic_set_mask(ZFCP_STATUS_COMMON_OPEN |
				ZFCP_STATUS_PORT_PHYS_OPEN, &port->status);
1481 1482 1483
		atomic_clear_mask(ZFCP_STATUS_COMMON_ACCESS_DENIED |
		                  ZFCP_STATUS_COMMON_ACCESS_BOXED,
		                  &port->status);
L
Linus Torvalds 已提交
1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498
		/* 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 已提交
1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509
		if (atomic_read(&port->status) & ZFCP_STATUS_PORT_NO_WWPN)
			break;

		plogi = (struct fsf_plogi *) req->qtcb->bottom.support.els;
		if (req->qtcb->bottom.support.els1_length >= sizeof(*plogi)) {
			if (plogi->serv_param.wwpn != port->wwpn)
				atomic_clear_mask(ZFCP_STATUS_PORT_DID_DID,
						  &port->status);
			else {
				port->wwnn = plogi->serv_param.wwnn;
				zfcp_fc_plogi_evaluate(port, plogi);
L
Linus Torvalds 已提交
1510 1511 1512 1513
			}
		}
		break;
	case FSF_UNKNOWN_OP_SUBTYPE:
S
Swen Schillig 已提交
1514
		req->status |= ZFCP_STATUS_FSFREQ_ERROR;
L
Linus Torvalds 已提交
1515 1516 1517
		break;
	}

S
Swen Schillig 已提交
1518
skip_fsfstatus:
L
Linus Torvalds 已提交
1519 1520 1521
	atomic_clear_mask(ZFCP_STATUS_COMMON_OPENING, &port->status);
}

S
Swen Schillig 已提交
1522 1523 1524 1525
/**
 * 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 已提交
1526
 */
S
Swen Schillig 已提交
1527
int zfcp_fsf_open_port(struct zfcp_erp_action *erp_action)
L
Linus Torvalds 已提交
1528 1529
{
	volatile struct qdio_buffer_element *sbale;
S
Swen Schillig 已提交
1530 1531 1532 1533
	struct zfcp_adapter *adapter = erp_action->adapter;
	struct zfcp_fsf_req *req;
	int retval = -EIO;

1534
	spin_lock_bh(&adapter->req_q.lock);
S
Swen Schillig 已提交
1535 1536 1537 1538 1539 1540 1541
	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);
1542
	if (IS_ERR(req)) {
S
Swen Schillig 已提交
1543
		retval = PTR_ERR(req);
L
Linus Torvalds 已提交
1544
		goto out;
S
Swen Schillig 已提交
1545
	}
L
Linus Torvalds 已提交
1546

S
Swen Schillig 已提交
1547
	sbale = zfcp_qdio_sbale_req(req);
L
Linus Torvalds 已提交
1548 1549 1550
        sbale[0].flags |= SBAL_FLAGS0_TYPE_READ;
        sbale[1].flags |= SBAL_FLAGS_LAST_ENTRY;

S
Swen Schillig 已提交
1551 1552 1553 1554 1555 1556 1557
	req->handler = zfcp_fsf_open_port_handler;
	req->qtcb->bottom.support.d_id = erp_action->port->d_id;
	req->data = erp_action->port;
	req->erp_action = erp_action;
	erp_action->fsf_req = req;
	atomic_set_mask(ZFCP_STATUS_COMMON_OPENING, &erp_action->port->status);

1558
	zfcp_fsf_start_erp_timer(req);
S
Swen Schillig 已提交
1559
	retval = zfcp_fsf_req_send(req);
L
Linus Torvalds 已提交
1560
	if (retval) {
S
Swen Schillig 已提交
1561
		zfcp_fsf_req_free(req);
L
Linus Torvalds 已提交
1562 1563
		erp_action->fsf_req = NULL;
	}
S
Swen Schillig 已提交
1564
out:
1565
	spin_unlock_bh(&adapter->req_q.lock);
L
Linus Torvalds 已提交
1566 1567 1568
	return retval;
}

S
Swen Schillig 已提交
1569
static void zfcp_fsf_close_port_handler(struct zfcp_fsf_req *req)
L
Linus Torvalds 已提交
1570
{
S
Swen Schillig 已提交
1571
	struct zfcp_port *port = req->data;
L
Linus Torvalds 已提交
1572

S
Swen Schillig 已提交
1573
	if (req->status & ZFCP_STATUS_FSFREQ_ERROR)
L
Linus Torvalds 已提交
1574 1575
		goto skip_fsfstatus;

S
Swen Schillig 已提交
1576
	switch (req->qtcb->header.fsf_status) {
L
Linus Torvalds 已提交
1577
	case FSF_PORT_HANDLE_NOT_VALID:
S
Swen Schillig 已提交
1578 1579
		zfcp_erp_adapter_reopen(port->adapter, 0, 107, req);
		req->status |= ZFCP_STATUS_FSFREQ_ERROR;
L
Linus Torvalds 已提交
1580 1581 1582 1583
		break;
	case FSF_ADAPTER_STATUS_AVAILABLE:
		break;
	case FSF_GOOD:
S
Swen Schillig 已提交
1584
		zfcp_erp_modify_port_status(port, 33, req,
L
Linus Torvalds 已提交
1585 1586 1587 1588 1589
					    ZFCP_STATUS_COMMON_OPEN,
					    ZFCP_CLEAR);
		break;
	}

S
Swen Schillig 已提交
1590
skip_fsfstatus:
L
Linus Torvalds 已提交
1591 1592 1593
	atomic_clear_mask(ZFCP_STATUS_COMMON_CLOSING, &port->status);
}

S
Swen Schillig 已提交
1594 1595 1596 1597
/**
 * 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 已提交
1598
 */
S
Swen Schillig 已提交
1599
int zfcp_fsf_close_port(struct zfcp_erp_action *erp_action)
L
Linus Torvalds 已提交
1600 1601
{
	volatile struct qdio_buffer_element *sbale;
S
Swen Schillig 已提交
1602 1603 1604 1605
	struct zfcp_adapter *adapter = erp_action->adapter;
	struct zfcp_fsf_req *req;
	int retval = -EIO;

1606
	spin_lock_bh(&adapter->req_q.lock);
S
Swen Schillig 已提交
1607 1608 1609 1610 1611 1612
	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);
1613
	if (IS_ERR(req)) {
S
Swen Schillig 已提交
1614
		retval = PTR_ERR(req);
L
Linus Torvalds 已提交
1615
		goto out;
S
Swen Schillig 已提交
1616
	}
L
Linus Torvalds 已提交
1617

S
Swen Schillig 已提交
1618
	sbale = zfcp_qdio_sbale_req(req);
L
Linus Torvalds 已提交
1619 1620 1621
	sbale[0].flags |= SBAL_FLAGS0_TYPE_READ;
	sbale[1].flags |= SBAL_FLAGS_LAST_ENTRY;

S
Swen Schillig 已提交
1622 1623 1624 1625 1626 1627 1628
	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;
	atomic_set_mask(ZFCP_STATUS_COMMON_CLOSING, &erp_action->port->status);

1629
	zfcp_fsf_start_erp_timer(req);
S
Swen Schillig 已提交
1630
	retval = zfcp_fsf_req_send(req);
L
Linus Torvalds 已提交
1631
	if (retval) {
S
Swen Schillig 已提交
1632
		zfcp_fsf_req_free(req);
L
Linus Torvalds 已提交
1633 1634
		erp_action->fsf_req = NULL;
	}
S
Swen Schillig 已提交
1635
out:
1636
	spin_unlock_bh(&adapter->req_q.lock);
L
Linus Torvalds 已提交
1637 1638 1639
	return retval;
}

S
Swen Schillig 已提交
1640
static void zfcp_fsf_close_physical_port_handler(struct zfcp_fsf_req *req)
L
Linus Torvalds 已提交
1641
{
S
Swen Schillig 已提交
1642 1643
	struct zfcp_port *port = req->data;
	struct fsf_qtcb_header *header = &req->qtcb->header;
L
Linus Torvalds 已提交
1644 1645
	struct zfcp_unit *unit;

S
Swen Schillig 已提交
1646
	if (req->status & ZFCP_STATUS_FSFREQ_ERROR)
L
Linus Torvalds 已提交
1647 1648 1649 1650
		goto skip_fsfstatus;

	switch (header->fsf_status) {
	case FSF_PORT_HANDLE_NOT_VALID:
S
Swen Schillig 已提交
1651 1652
		zfcp_erp_adapter_reopen(port->adapter, 0, 108, req);
		req->status |= ZFCP_STATUS_FSFREQ_ERROR;
L
Linus Torvalds 已提交
1653 1654
		break;
	case FSF_ACCESS_DENIED:
S
Swen Schillig 已提交
1655
		zfcp_fsf_access_denied_port(req, port);
L
Linus Torvalds 已提交
1656 1657
		break;
	case FSF_PORT_BOXED:
S
Swen Schillig 已提交
1658 1659 1660
		zfcp_erp_port_boxed(port, 50, req);
		req->status |= ZFCP_STATUS_FSFREQ_ERROR |
			       ZFCP_STATUS_FSFREQ_RETRY;
1661 1662 1663 1664 1665 1666
		/* 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);
L
Linus Torvalds 已提交
1667 1668 1669 1670
		break;
	case FSF_ADAPTER_STATUS_AVAILABLE:
		switch (header->fsf_status_qual.word[0]) {
		case FSF_SQ_INVOKE_LINK_TEST_PROCEDURE:
S
Swen Schillig 已提交
1671
			/* fall through */
L
Linus Torvalds 已提交
1672
		case FSF_SQ_ULP_DEPENDENT_ERP_REQUIRED:
S
Swen Schillig 已提交
1673
			req->status |= ZFCP_STATUS_FSFREQ_ERROR;
L
Linus Torvalds 已提交
1674 1675 1676 1677 1678 1679 1680 1681 1682
			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 已提交
1683 1684
			atomic_clear_mask(ZFCP_STATUS_COMMON_OPEN,
					  &unit->status);
L
Linus Torvalds 已提交
1685 1686
		break;
	}
S
Swen Schillig 已提交
1687
skip_fsfstatus:
L
Linus Torvalds 已提交
1688 1689 1690
	atomic_clear_mask(ZFCP_STATUS_PORT_PHYS_CLOSING, &port->status);
}

S
Swen Schillig 已提交
1691 1692 1693 1694
/**
 * zfcp_fsf_close_physical_port - close physical port
 * @erp_action: pointer to struct zfcp_erp_action
 * Returns: 0 on success
L
Linus Torvalds 已提交
1695
 */
S
Swen Schillig 已提交
1696
int zfcp_fsf_close_physical_port(struct zfcp_erp_action *erp_action)
L
Linus Torvalds 已提交
1697 1698
{
	volatile struct qdio_buffer_element *sbale;
S
Swen Schillig 已提交
1699 1700 1701 1702
	struct zfcp_adapter *adapter = erp_action->adapter;
	struct zfcp_fsf_req *req;
	int retval = -EIO;

1703
	spin_lock_bh(&adapter->req_q.lock);
S
Swen Schillig 已提交
1704
	if (zfcp_fsf_req_sbal_get(adapter))
L
Linus Torvalds 已提交
1705 1706
		goto out;

S
Swen Schillig 已提交
1707 1708 1709
	req = zfcp_fsf_req_create(adapter, FSF_QTCB_CLOSE_PHYSICAL_PORT,
				  ZFCP_REQ_AUTO_CLEANUP,
				  adapter->pool.fsf_req_erp);
1710
	if (IS_ERR(req)) {
S
Swen Schillig 已提交
1711 1712 1713
		retval = PTR_ERR(req);
		goto out;
	}
L
Linus Torvalds 已提交
1714

S
Swen Schillig 已提交
1715 1716 1717
	sbale = zfcp_qdio_sbale_req(req);
	sbale[0].flags |= SBAL_FLAGS0_TYPE_READ;
	sbale[1].flags |= SBAL_FLAGS_LAST_ENTRY;
L
Linus Torvalds 已提交
1718

S
Swen Schillig 已提交
1719 1720 1721 1722 1723 1724 1725 1726
	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;
	atomic_set_mask(ZFCP_STATUS_PORT_PHYS_CLOSING,
			&erp_action->port->status);

1727
	zfcp_fsf_start_erp_timer(req);
S
Swen Schillig 已提交
1728
	retval = zfcp_fsf_req_send(req);
L
Linus Torvalds 已提交
1729
	if (retval) {
S
Swen Schillig 已提交
1730
		zfcp_fsf_req_free(req);
L
Linus Torvalds 已提交
1731 1732
		erp_action->fsf_req = NULL;
	}
S
Swen Schillig 已提交
1733
out:
1734
	spin_unlock_bh(&adapter->req_q.lock);
L
Linus Torvalds 已提交
1735 1736 1737
	return retval;
}

S
Swen Schillig 已提交
1738
static void zfcp_fsf_open_unit_handler(struct zfcp_fsf_req *req)
L
Linus Torvalds 已提交
1739
{
S
Swen Schillig 已提交
1740 1741 1742 1743 1744 1745
	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;
1746
	int exclusive, readwrite;
L
Linus Torvalds 已提交
1747

S
Swen Schillig 已提交
1748
	if (req->status & ZFCP_STATUS_FSFREQ_ERROR)
L
Linus Torvalds 已提交
1749 1750 1751
		goto skip_fsfstatus;

	atomic_clear_mask(ZFCP_STATUS_COMMON_ACCESS_DENIED |
1752
			  ZFCP_STATUS_COMMON_ACCESS_BOXED |
L
Linus Torvalds 已提交
1753 1754 1755 1756 1757 1758 1759
			  ZFCP_STATUS_UNIT_SHARED |
			  ZFCP_STATUS_UNIT_READONLY,
			  &unit->status);

	switch (header->fsf_status) {

	case FSF_PORT_HANDLE_NOT_VALID:
S
Swen Schillig 已提交
1760 1761
		zfcp_erp_adapter_reopen(unit->port->adapter, 0, 109, req);
		/* fall through */
L
Linus Torvalds 已提交
1762 1763 1764
	case FSF_LUN_ALREADY_OPEN:
		break;
	case FSF_ACCESS_DENIED:
S
Swen Schillig 已提交
1765
		zfcp_fsf_access_denied_unit(req, unit);
L
Linus Torvalds 已提交
1766
		atomic_clear_mask(ZFCP_STATUS_UNIT_SHARED, &unit->status);
C
Christof Schmitt 已提交
1767
		atomic_clear_mask(ZFCP_STATUS_UNIT_READONLY, &unit->status);
L
Linus Torvalds 已提交
1768 1769
		break;
	case FSF_PORT_BOXED:
S
Swen Schillig 已提交
1770 1771 1772
		zfcp_erp_port_boxed(unit->port, 51, req);
		req->status |= ZFCP_STATUS_FSFREQ_ERROR |
			       ZFCP_STATUS_FSFREQ_RETRY;
L
Linus Torvalds 已提交
1773 1774
		break;
	case FSF_LUN_SHARING_VIOLATION:
S
Swen Schillig 已提交
1775
		if (header->fsf_status_qual.word[0])
C
Christof Schmitt 已提交
1776 1777 1778 1779 1780 1781 1782 1783 1784
			dev_warn(&adapter->ccw_device->dev,
				 "FCP-LUN 0x%Lx at the remote port "
				 "with WWPN 0x%Lx "
				 "connected to the adapter "
				 "is already in use in LPAR%d, CSS%d.\n",
				 unit->fcp_lun,
				 unit->port->wwpn,
				 queue_designator->hla,
				 queue_designator->cssid);
S
Swen Schillig 已提交
1785
		else
C
Christof Schmitt 已提交
1786 1787
			zfcp_act_eval_err(adapter,
					  header->fsf_status_qual.word[2]);
S
Swen Schillig 已提交
1788
		zfcp_erp_unit_access_denied(unit, 60, req);
L
Linus Torvalds 已提交
1789 1790
		atomic_clear_mask(ZFCP_STATUS_UNIT_SHARED, &unit->status);
		atomic_clear_mask(ZFCP_STATUS_UNIT_READONLY, &unit->status);
S
Swen Schillig 已提交
1791
		req->status |= ZFCP_STATUS_FSFREQ_ERROR;
L
Linus Torvalds 已提交
1792 1793
		break;
	case FSF_MAXIMUM_NUMBER_OF_LUNS_EXCEEDED:
S
Swen Schillig 已提交
1794
		dev_warn(&adapter->ccw_device->dev,
C
Christof Schmitt 已提交
1795 1796 1797
			 "The adapter ran out of resources. There is no "
			 "handle available for unit 0x%016Lx on port 0x%016Lx.",
			 unit->fcp_lun, unit->port->wwpn);
S
Swen Schillig 已提交
1798 1799 1800 1801
		zfcp_erp_unit_failed(unit, 34, req);
		/* fall through */
	case FSF_INVALID_COMMAND_OPTION:
		req->status |= ZFCP_STATUS_FSFREQ_ERROR;
L
Linus Torvalds 已提交
1802 1803 1804 1805
		break;
	case FSF_ADAPTER_STATUS_AVAILABLE:
		switch (header->fsf_status_qual.word[0]) {
		case FSF_SQ_INVOKE_LINK_TEST_PROCEDURE:
1806
			zfcp_test_link(unit->port);
S
Swen Schillig 已提交
1807
			/* fall through */
L
Linus Torvalds 已提交
1808
		case FSF_SQ_ULP_DEPENDENT_ERP_REQUIRED:
S
Swen Schillig 已提交
1809
			req->status |= ZFCP_STATUS_FSFREQ_ERROR;
L
Linus Torvalds 已提交
1810 1811 1812 1813 1814 1815 1816
			break;
		}
		break;

	case FSF_GOOD:
		unit->handle = header->lun_handle;
		atomic_set_mask(ZFCP_STATUS_COMMON_OPEN, &unit->status);
1817 1818 1819 1820 1821 1822 1823 1824 1825

		if (!(adapter->connection_features & FSF_FEATURE_NPIV_MODE) &&
		    (adapter->adapter_features & FSF_FEATURE_LUN_SHARING) &&
		    (adapter->ccw_device->id.dev_model != ZFCP_DEVICE_MODEL_PRIV)) {
			exclusive = (bottom->lun_access_info &
					FSF_UNIT_ACCESS_EXCLUSIVE);
			readwrite = (bottom->lun_access_info &
					FSF_UNIT_ACCESS_OUTBOUND_TRANSFER);

L
Linus Torvalds 已提交
1826 1827 1828 1829 1830 1831 1832
			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 已提交
1833
				dev_info(&adapter->ccw_device->dev,
C
Christof Schmitt 已提交
1834 1835 1836
					 "Read-only access for unit 0x%016Lx "
					 "on port 0x%016Lx.\n",
					 unit->fcp_lun, unit->port->wwpn);
L
Linus Torvalds 已提交
1837 1838 1839
        		}

        		if (exclusive && !readwrite) {
S
Swen Schillig 已提交
1840
				dev_err(&adapter->ccw_device->dev,
C
Christof Schmitt 已提交
1841 1842 1843 1844
					"Exclusive access of read-only unit "
					"0x%016Lx on port 0x%016Lx not "
					"supported, disabling unit.\n",
					unit->fcp_lun, unit->port->wwpn);
S
Swen Schillig 已提交
1845 1846 1847
				zfcp_erp_unit_failed(unit, 35, req);
				req->status |= ZFCP_STATUS_FSFREQ_ERROR;
				zfcp_erp_unit_shutdown(unit, 0, 80, req);
L
Linus Torvalds 已提交
1848
        		} else if (!exclusive && readwrite) {
S
Swen Schillig 已提交
1849
				dev_err(&adapter->ccw_device->dev,
C
Christof Schmitt 已提交
1850 1851 1852 1853
					"Shared access of read-write unit "
					"0x%016Lx on port 0x%016Lx not "
					"supported, disabling unit.\n",
					unit->fcp_lun, unit->port->wwpn);
S
Swen Schillig 已提交
1854 1855 1856
				zfcp_erp_unit_failed(unit, 36, req);
				req->status |= ZFCP_STATUS_FSFREQ_ERROR;
				zfcp_erp_unit_shutdown(unit, 0, 81, req);
L
Linus Torvalds 已提交
1857 1858 1859 1860 1861
        		}
		}
		break;
	}

S
Swen Schillig 已提交
1862
skip_fsfstatus:
L
Linus Torvalds 已提交
1863 1864 1865
	atomic_clear_mask(ZFCP_STATUS_COMMON_OPENING, &unit->status);
}

S
Swen Schillig 已提交
1866 1867 1868 1869
/**
 * zfcp_fsf_open_unit - open unit
 * @erp_action: pointer to struct zfcp_erp_action
 * Returns: 0 on success, error otherwise
L
Linus Torvalds 已提交
1870
 */
S
Swen Schillig 已提交
1871
int zfcp_fsf_open_unit(struct zfcp_erp_action *erp_action)
L
Linus Torvalds 已提交
1872 1873
{
	volatile struct qdio_buffer_element *sbale;
S
Swen Schillig 已提交
1874 1875 1876 1877
	struct zfcp_adapter *adapter = erp_action->adapter;
	struct zfcp_fsf_req *req;
	int retval = -EIO;

1878
	spin_lock_bh(&adapter->req_q.lock);
S
Swen Schillig 已提交
1879
	if (zfcp_fsf_req_sbal_get(adapter))
L
Linus Torvalds 已提交
1880 1881
		goto out;

S
Swen Schillig 已提交
1882 1883 1884
	req = zfcp_fsf_req_create(adapter, FSF_QTCB_OPEN_LUN,
				  ZFCP_REQ_AUTO_CLEANUP,
				  adapter->pool.fsf_req_erp);
1885
	if (IS_ERR(req)) {
S
Swen Schillig 已提交
1886 1887 1888 1889 1890
		retval = PTR_ERR(req);
		goto out;
	}

	sbale = zfcp_qdio_sbale_req(req);
L
Linus Torvalds 已提交
1891 1892 1893
        sbale[0].flags |= SBAL_FLAGS0_TYPE_READ;
        sbale[1].flags |= SBAL_FLAGS_LAST_ENTRY;

S
Swen Schillig 已提交
1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904
	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;

	atomic_set_mask(ZFCP_STATUS_COMMON_OPENING, &erp_action->unit->status);
L
Linus Torvalds 已提交
1905

1906
	zfcp_fsf_start_erp_timer(req);
S
Swen Schillig 已提交
1907
	retval = zfcp_fsf_req_send(req);
L
Linus Torvalds 已提交
1908
	if (retval) {
S
Swen Schillig 已提交
1909
		zfcp_fsf_req_free(req);
L
Linus Torvalds 已提交
1910 1911
		erp_action->fsf_req = NULL;
	}
S
Swen Schillig 已提交
1912
out:
1913
	spin_unlock_bh(&adapter->req_q.lock);
L
Linus Torvalds 已提交
1914 1915 1916
	return retval;
}

S
Swen Schillig 已提交
1917
static void zfcp_fsf_close_unit_handler(struct zfcp_fsf_req *req)
L
Linus Torvalds 已提交
1918
{
S
Swen Schillig 已提交
1919
	struct zfcp_unit *unit = req->data;
L
Linus Torvalds 已提交
1920

S
Swen Schillig 已提交
1921
	if (req->status & ZFCP_STATUS_FSFREQ_ERROR)
L
Linus Torvalds 已提交
1922 1923
		goto skip_fsfstatus;

S
Swen Schillig 已提交
1924
	switch (req->qtcb->header.fsf_status) {
L
Linus Torvalds 已提交
1925
	case FSF_PORT_HANDLE_NOT_VALID:
S
Swen Schillig 已提交
1926 1927
		zfcp_erp_adapter_reopen(unit->port->adapter, 0, 110, req);
		req->status |= ZFCP_STATUS_FSFREQ_ERROR;
L
Linus Torvalds 已提交
1928 1929
		break;
	case FSF_LUN_HANDLE_NOT_VALID:
S
Swen Schillig 已提交
1930 1931
		zfcp_erp_port_reopen(unit->port, 0, 111, req);
		req->status |= ZFCP_STATUS_FSFREQ_ERROR;
L
Linus Torvalds 已提交
1932 1933
		break;
	case FSF_PORT_BOXED:
S
Swen Schillig 已提交
1934 1935 1936
		zfcp_erp_port_boxed(unit->port, 52, req);
		req->status |= ZFCP_STATUS_FSFREQ_ERROR |
			       ZFCP_STATUS_FSFREQ_RETRY;
L
Linus Torvalds 已提交
1937 1938
		break;
	case FSF_ADAPTER_STATUS_AVAILABLE:
S
Swen Schillig 已提交
1939
		switch (req->qtcb->header.fsf_status_qual.word[0]) {
L
Linus Torvalds 已提交
1940
		case FSF_SQ_INVOKE_LINK_TEST_PROCEDURE:
1941
			zfcp_test_link(unit->port);
S
Swen Schillig 已提交
1942
			/* fall through */
L
Linus Torvalds 已提交
1943
		case FSF_SQ_ULP_DEPENDENT_ERP_REQUIRED:
S
Swen Schillig 已提交
1944
			req->status |= ZFCP_STATUS_FSFREQ_ERROR;
L
Linus Torvalds 已提交
1945 1946 1947 1948 1949 1950 1951
			break;
		}
		break;
	case FSF_GOOD:
		atomic_clear_mask(ZFCP_STATUS_COMMON_OPEN, &unit->status);
		break;
	}
S
Swen Schillig 已提交
1952
skip_fsfstatus:
L
Linus Torvalds 已提交
1953 1954 1955 1956
	atomic_clear_mask(ZFCP_STATUS_COMMON_CLOSING, &unit->status);
}

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

1968
	spin_lock_bh(&adapter->req_q.lock);
S
Swen Schillig 已提交
1969
	if (zfcp_fsf_req_sbal_get(adapter))
L
Linus Torvalds 已提交
1970
		goto out;
S
Swen Schillig 已提交
1971 1972 1973
	req = zfcp_fsf_req_create(adapter, FSF_QTCB_CLOSE_LUN,
				  ZFCP_REQ_AUTO_CLEANUP,
				  adapter->pool.fsf_req_erp);
1974
	if (IS_ERR(req)) {
S
Swen Schillig 已提交
1975 1976 1977
		retval = PTR_ERR(req);
		goto out;
	}
L
Linus Torvalds 已提交
1978

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

S
Swen Schillig 已提交
1983 1984 1985 1986 1987 1988 1989
	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;
	atomic_set_mask(ZFCP_STATUS_COMMON_CLOSING, &erp_action->unit->status);
1990

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

2002 2003 2004
static void zfcp_fsf_update_lat(struct fsf_latency_record *lat_rec, u32 lat)
{
	lat_rec->sum += lat;
S
Swen Schillig 已提交
2005 2006
	lat_rec->min = min(lat_rec->min, lat);
	lat_rec->max = max(lat_rec->max, lat);
2007 2008
}

S
Swen Schillig 已提交
2009
static void zfcp_fsf_req_latency(struct zfcp_fsf_req *req)
2010 2011 2012
{
	struct fsf_qual_latency_info *lat_inf;
	struct latency_cont *lat;
S
Swen Schillig 已提交
2013
	struct zfcp_unit *unit = req->unit;
2014 2015
	unsigned long flags;

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

S
Swen Schillig 已提交
2018
	switch (req->qtcb->bottom.io.data_direction) {
2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035
	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;
	}

	spin_lock_irqsave(&unit->latencies.lock, flags);
	zfcp_fsf_update_lat(&lat->channel, lat_inf->channel_lat);
	zfcp_fsf_update_lat(&lat->fabric, lat_inf->fabric_lat);
	lat->counter++;
S
Swen Schillig 已提交
2036
	spin_unlock_irqrestore(&unit->latencies.lock, flags);
L
Linus Torvalds 已提交
2037 2038
}

S
Swen Schillig 已提交
2039
static void zfcp_fsf_send_fcp_command_task_handler(struct zfcp_fsf_req *req)
L
Linus Torvalds 已提交
2040
{
S
Swen Schillig 已提交
2041
	struct scsi_cmnd *scpnt = req->data;
L
Linus Torvalds 已提交
2042
	struct fcp_rsp_iu *fcp_rsp_iu = (struct fcp_rsp_iu *)
S
Swen Schillig 已提交
2043
	    &(req->qtcb->bottom.io.fcp_rsp);
L
Linus Torvalds 已提交
2044
	u32 sns_len;
2045
	char *fcp_rsp_info = (unsigned char *) &fcp_rsp_iu[1];
L
Linus Torvalds 已提交
2046 2047
	unsigned long flags;

C
Christof Schmitt 已提交
2048
	if (unlikely(!scpnt))
S
Swen Schillig 已提交
2049
		return;
C
Christof Schmitt 已提交
2050

S
Swen Schillig 已提交
2051 2052 2053
	read_lock_irqsave(&req->adapter->abort_lock, flags);

	if (unlikely(req->status & ZFCP_STATUS_FSFREQ_ABORTED)) {
2054 2055
		set_host_byte(scpnt, DID_SOFT_ERROR);
		set_driver_byte(scpnt, SUGGEST_RETRY);
L
Linus Torvalds 已提交
2056 2057 2058
		goto skip_fsfstatus;
	}

S
Swen Schillig 已提交
2059
	if (unlikely(req->status & ZFCP_STATUS_FSFREQ_ERROR)) {
2060
		set_host_byte(scpnt, DID_ERROR);
L
Linus Torvalds 已提交
2061 2062 2063
		goto skip_fsfstatus;
	}

2064
	set_msg_byte(scpnt, COMMAND_COMPLETE);
L
Linus Torvalds 已提交
2065 2066 2067

	scpnt->result |= fcp_rsp_iu->scsi_status;

S
Swen Schillig 已提交
2068 2069
	if (req->adapter->adapter_features & FSF_FEATURE_MEASUREMENT_DATA)
		zfcp_fsf_req_latency(req);
2070

L
Linus Torvalds 已提交
2071
	if (unlikely(fcp_rsp_iu->validity.bits.fcp_rsp_len_valid)) {
S
Swen Schillig 已提交
2072
		if (fcp_rsp_info[3] == RSP_CODE_GOOD)
2073
			set_host_byte(scpnt, DID_OK);
S
Swen Schillig 已提交
2074
		else {
2075
			set_host_byte(scpnt, DID_ERROR);
已提交
2076
			goto skip_fsfstatus;
L
Linus Torvalds 已提交
2077 2078 2079 2080
		}
	}

	if (unlikely(fcp_rsp_iu->validity.bits.fcp_sns_len_valid)) {
S
Swen Schillig 已提交
2081 2082
		sns_len = FSF_FCP_RSP_SIZE - sizeof(struct fcp_rsp_iu) +
			  fcp_rsp_iu->fcp_rsp_len;
L
Linus Torvalds 已提交
2083 2084 2085
		sns_len = min(sns_len, (u32) SCSI_SENSE_BUFFERSIZE);
		sns_len = min(sns_len, fcp_rsp_iu->fcp_sns_len);

2086
		memcpy(scpnt->sense_buffer,
L
Linus Torvalds 已提交
2087 2088 2089 2090
		       zfcp_get_fcp_sns_info_ptr(fcp_rsp_iu), sns_len);
	}

	if (unlikely(fcp_rsp_iu->validity.bits.fcp_resid_under)) {
2091 2092 2093
		scsi_set_resid(scpnt, fcp_rsp_iu->fcp_resid);
		if (scsi_bufflen(scpnt) - scsi_get_resid(scpnt) <
		    scpnt->underflow)
2094
			set_host_byte(scpnt, DID_ERROR);
L
Linus Torvalds 已提交
2095
	}
S
Swen Schillig 已提交
2096
skip_fsfstatus:
2097
	if (scpnt->result != 0)
S
Swen Schillig 已提交
2098
		zfcp_scsi_dbf_event_result("erro", 3, req->adapter, scpnt, req);
2099
	else if (scpnt->retries > 0)
S
Swen Schillig 已提交
2100
		zfcp_scsi_dbf_event_result("retr", 4, req->adapter, scpnt, req);
2101
	else
S
Swen Schillig 已提交
2102
		zfcp_scsi_dbf_event_result("norm", 6, req->adapter, scpnt, req);
L
Linus Torvalds 已提交
2103 2104 2105 2106 2107 2108 2109 2110 2111

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

S
Swen Schillig 已提交
2115
static void zfcp_fsf_send_fcp_ctm_handler(struct zfcp_fsf_req *req)
L
Linus Torvalds 已提交
2116 2117
{
	struct fcp_rsp_iu *fcp_rsp_iu = (struct fcp_rsp_iu *)
S
Swen Schillig 已提交
2118
	    &(req->qtcb->bottom.io.fcp_rsp);
2119
	char *fcp_rsp_info = (unsigned char *) &fcp_rsp_iu[1];
L
Linus Torvalds 已提交
2120

S
Swen Schillig 已提交
2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137
	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 已提交
2138 2139
		goto skip_fsfstatus;

S
Swen Schillig 已提交
2140 2141 2142 2143 2144 2145 2146 2147 2148 2149
	switch (header->fsf_status) {
	case FSF_HANDLE_MISMATCH:
	case FSF_PORT_HANDLE_NOT_VALID:
		zfcp_erp_adapter_reopen(unit->port->adapter, 0, 112, req);
		req->status |= ZFCP_STATUS_FSFREQ_ERROR;
		break;
	case FSF_FCPLUN_NOT_VALID:
	case FSF_LUN_HANDLE_NOT_VALID:
		zfcp_erp_port_reopen(unit->port, 0, 113, req);
		req->status |= ZFCP_STATUS_FSFREQ_ERROR;
L
Linus Torvalds 已提交
2150
		break;
S
Swen Schillig 已提交
2151 2152
	case FSF_SERVICE_CLASS_NOT_SUPPORTED:
		zfcp_fsf_class_not_supp(req);
L
Linus Torvalds 已提交
2153
		break;
S
Swen Schillig 已提交
2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191
	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,
			"Invalid data direction (%d) given for unit "
			"0x%016Lx on port 0x%016Lx, shutting down "
			"adapter.\n",
			req->qtcb->bottom.io.data_direction,
			unit->fcp_lun, unit->port->wwpn);
		zfcp_erp_adapter_shutdown(unit->port->adapter, 0, 133, req);
		req->status |= ZFCP_STATUS_FSFREQ_ERROR;
		break;
	case FSF_CMND_LENGTH_NOT_VALID:
		dev_err(&req->adapter->ccw_device->dev,
			"An invalid control-data-block length field (%d) "
			"was found in a command for unit 0x%016Lx on port "
			"0x%016Lx. Shutting down adapter.\n",
			req->qtcb->bottom.io.fcp_cmnd_length,
			unit->fcp_lun, unit->port->wwpn);
		zfcp_erp_adapter_shutdown(unit->port->adapter, 0, 134, req);
		req->status |= ZFCP_STATUS_FSFREQ_ERROR;
		break;
	case FSF_PORT_BOXED:
		zfcp_erp_port_boxed(unit->port, 53, req);
		req->status |= ZFCP_STATUS_FSFREQ_ERROR |
			       ZFCP_STATUS_FSFREQ_RETRY;
		break;
	case FSF_LUN_BOXED:
		zfcp_erp_unit_boxed(unit, 54, req);
		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 已提交
2192 2193
		break;
	}
S
Swen Schillig 已提交
2194 2195 2196 2197 2198 2199 2200 2201
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 已提交
2202 2203
}

S
Swen Schillig 已提交
2204 2205 2206 2207 2208 2209 2210
/**
 * zfcp_fsf_send_fcp_command_task - initiate an FCP command (for a SCSI command)
 * @adapter: adapter where scsi command is issued
 * @unit: unit where command is sent to
 * @scsi_cmnd: scsi command to be sent
 * @timer: timer to be started when request is initiated
 * @req_flags: flags for fsf_request
L
Linus Torvalds 已提交
2211
 */
S
Swen Schillig 已提交
2212 2213 2214 2215
int zfcp_fsf_send_fcp_command_task(struct zfcp_adapter *adapter,
				   struct zfcp_unit *unit,
				   struct scsi_cmnd *scsi_cmnd,
				   int use_timer, int req_flags)
L
Linus Torvalds 已提交
2216
{
S
Swen Schillig 已提交
2217 2218 2219 2220
	struct zfcp_fsf_req *req;
	struct fcp_cmnd_iu *fcp_cmnd_iu;
	unsigned int sbtype;
	int real_bytes, retval = -EIO;
L
Linus Torvalds 已提交
2221

S
Swen Schillig 已提交
2222 2223 2224
	if (unlikely(!(atomic_read(&unit->status) &
		       ZFCP_STATUS_COMMON_UNBLOCKED)))
		return -EBUSY;
L
Linus Torvalds 已提交
2225

S
Swen Schillig 已提交
2226 2227 2228 2229 2230
	spin_lock(&adapter->req_q.lock);
	if (!atomic_read(&adapter->req_q.count))
		goto out;
	req = zfcp_fsf_req_create(adapter, FSF_QTCB_FCP_CMND, req_flags,
				  adapter->pool.fsf_req_scsi);
2231
	if (IS_ERR(req)) {
S
Swen Schillig 已提交
2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257
		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;
		sbtype = SBAL_FLAGS0_TYPE_READ;
L
Linus Torvalds 已提交
2258
		break;
S
Swen Schillig 已提交
2259 2260 2261 2262 2263 2264 2265 2266 2267
	case DMA_FROM_DEVICE:
		req->qtcb->bottom.io.data_direction = FSF_DATADIR_READ;
		sbtype = SBAL_FLAGS0_TYPE_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 已提交
2268
		break;
S
Swen Schillig 已提交
2269
	case DMA_BIDIRECTIONAL:
L
Linus Torvalds 已提交
2270
	default:
S
Swen Schillig 已提交
2271 2272
		retval = -EIO;
		goto failed_scsi_cmnd;
L
Linus Torvalds 已提交
2273 2274
	}

S
Swen Schillig 已提交
2275 2276 2277 2278 2279 2280
	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 已提交
2281

S
Swen Schillig 已提交
2282 2283 2284
	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 已提交
2285

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

S
Swen Schillig 已提交
2288 2289
	req->qtcb->bottom.io.fcp_cmnd_length = sizeof(struct fcp_cmnd_iu) +
		fcp_cmnd_iu->add_fcp_cdb_length + sizeof(fcp_dl_t);
L
Linus Torvalds 已提交
2290

S
Swen Schillig 已提交
2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306
	real_bytes = zfcp_qdio_sbals_from_sg(req, sbtype,
					     scsi_sglist(scsi_cmnd),
					     FSF_MAX_SBALS_PER_REQ);
	if (unlikely(real_bytes < 0)) {
		if (req->sbal_number < FSF_MAX_SBALS_PER_REQ)
			retval = -EIO;
		else {
			dev_err(&adapter->ccw_device->dev,
				"SCSI request too large. "
				"Shutting down unit 0x%016Lx on port "
				"0x%016Lx.\n", unit->fcp_lun,
				unit->port->wwpn);
			zfcp_erp_unit_shutdown(unit, 0, 131, req);
			retval = -EINVAL;
		}
		goto failed_scsi_cmnd;
L
Linus Torvalds 已提交
2307 2308
	}

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

S
Swen Schillig 已提交
2311 2312
	if (use_timer)
		zfcp_fsf_start_timer(req, ZFCP_FSF_REQUEST_TIMEOUT);
L
Linus Torvalds 已提交
2313

S
Swen Schillig 已提交
2314 2315 2316
	retval = zfcp_fsf_req_send(req);
	if (unlikely(retval))
		goto failed_scsi_cmnd;
L
Linus Torvalds 已提交
2317

S
Swen Schillig 已提交
2318
	goto out;
L
Linus Torvalds 已提交
2319

S
Swen Schillig 已提交
2320 2321 2322 2323 2324 2325 2326
failed_scsi_cmnd:
	zfcp_unit_put(unit);
	zfcp_fsf_req_free(req);
	scsi_cmnd->host_scribble = NULL;
out:
	spin_unlock(&adapter->req_q.lock);
	return retval;
L
Linus Torvalds 已提交
2327 2328 2329
}

/**
S
Swen Schillig 已提交
2330 2331 2332 2333 2334 2335
 * zfcp_fsf_send_fcp_ctm - send SCSI task management command
 * @adapter: pointer to struct zfcp-adapter
 * @unit: pointer to struct zfcp_unit
 * @tm_flags: unsigned byte for task management flags
 * @req_flags: int request flags
 * Returns: on success pointer to struct fsf_req, NULL otherwise
L
Linus Torvalds 已提交
2336
 */
S
Swen Schillig 已提交
2337 2338 2339
struct zfcp_fsf_req *zfcp_fsf_send_fcp_ctm(struct zfcp_adapter *adapter,
					   struct zfcp_unit *unit,
					   u8 tm_flags, int req_flags)
L
Linus Torvalds 已提交
2340 2341
{
	volatile struct qdio_buffer_element *sbale;
S
Swen Schillig 已提交
2342 2343
	struct zfcp_fsf_req *req = NULL;
	struct fcp_cmnd_iu *fcp_cmnd_iu;
L
Linus Torvalds 已提交
2344

S
Swen Schillig 已提交
2345 2346 2347
	if (unlikely(!(atomic_read(&unit->status) &
		       ZFCP_STATUS_COMMON_UNBLOCKED)))
		return NULL;
L
Linus Torvalds 已提交
2348

S
Swen Schillig 已提交
2349 2350 2351 2352 2353
	spin_lock(&adapter->req_q.lock);
	if (!atomic_read(&adapter->req_q.count))
		goto out;
	req = zfcp_fsf_req_create(adapter, FSF_QTCB_FCP_CMND, req_flags,
				  adapter->pool.fsf_req_scsi);
2354
	if (IS_ERR(req))
S
Swen Schillig 已提交
2355
		goto out;
L
Linus Torvalds 已提交
2356

S
Swen Schillig 已提交
2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369
	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) +
						sizeof(fcp_dl_t);

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

S
Swen Schillig 已提交
2371 2372 2373
	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 已提交
2374

S
Swen Schillig 已提交
2375 2376 2377
	zfcp_fsf_start_timer(req, ZFCP_SCSI_ER_TIMEOUT);
	if (!zfcp_fsf_req_send(req))
		goto out;
L
Linus Torvalds 已提交
2378

S
Swen Schillig 已提交
2379 2380 2381 2382 2383 2384
	zfcp_fsf_req_free(req);
	req = NULL;
out:
	spin_unlock(&adapter->req_q.lock);
	return req;
}
L
Linus Torvalds 已提交
2385

S
Swen Schillig 已提交
2386 2387 2388 2389
static void zfcp_fsf_control_file_handler(struct zfcp_fsf_req *req)
{
	if (req->qtcb->header.fsf_status != FSF_GOOD)
		req->status |= ZFCP_STATUS_FSFREQ_ERROR;
L
Linus Torvalds 已提交
2390 2391
}

S
Swen Schillig 已提交
2392 2393 2394 2395 2396
/**
 * 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 已提交
2397
 */
S
Swen Schillig 已提交
2398 2399
struct zfcp_fsf_req *zfcp_fsf_control_file(struct zfcp_adapter *adapter,
					   struct zfcp_fsf_cfdc *fsf_cfdc)
L
Linus Torvalds 已提交
2400 2401
{
	volatile struct qdio_buffer_element *sbale;
S
Swen Schillig 已提交
2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418
	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 已提交
2419

2420
	spin_lock_bh(&adapter->req_q.lock);
S
Swen Schillig 已提交
2421 2422
	if (zfcp_fsf_req_sbal_get(adapter))
		goto out;
L
Linus Torvalds 已提交
2423

S
Swen Schillig 已提交
2424
	req = zfcp_fsf_req_create(adapter, fsf_cfdc->command, 0, NULL);
2425
	if (IS_ERR(req)) {
S
Swen Schillig 已提交
2426 2427 2428
		retval = -EPERM;
		goto out;
	}
L
Linus Torvalds 已提交
2429

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

S
Swen Schillig 已提交
2432 2433
	sbale = zfcp_qdio_sbale_req(req);
	sbale[0].flags |= direction;
2434

S
Swen Schillig 已提交
2435 2436 2437
	bottom = &req->qtcb->bottom.support;
	bottom->operation_subtype = FSF_CFDC_OPERATION_SUBTYPE;
	bottom->option = fsf_cfdc->option;
2438

S
Swen Schillig 已提交
2439 2440 2441 2442 2443 2444 2445
	bytes = zfcp_qdio_sbals_from_sg(req, direction, fsf_cfdc->sg,
					FSF_MAX_SBALS_PER_REQ);
	if (bytes != ZFCP_CFDC_MAX_SIZE) {
		retval = -ENOMEM;
		zfcp_fsf_req_free(req);
		goto out;
	}
L
Linus Torvalds 已提交
2446

S
Swen Schillig 已提交
2447 2448 2449
	zfcp_fsf_start_timer(req, ZFCP_FSF_REQUEST_TIMEOUT);
	retval = zfcp_fsf_req_send(req);
out:
2450
	spin_unlock_bh(&adapter->req_q.lock);
2451

S
Swen Schillig 已提交
2452 2453 2454 2455
	if (!retval) {
		wait_event(req->completion_wq,
			   req->status & ZFCP_STATUS_FSFREQ_COMPLETED);
		return req;
L
Linus Torvalds 已提交
2456
	}
S
Swen Schillig 已提交
2457
	return ERR_PTR(retval);
L
Linus Torvalds 已提交
2458
}