zfcp_fsf.c 136.5 KB
Newer Older
L
Linus Torvalds 已提交
1
/*
2 3
 * This file is part of the zfcp device driver for
 * FCP adapters for IBM System z9 and zSeries.
L
Linus Torvalds 已提交
4
 *
5
 * (C) Copyright IBM Corp. 2002, 2006
L
Linus Torvalds 已提交
6 7 8 9 10 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 36 37 38
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include "zfcp_ext.h"

static int zfcp_fsf_exchange_config_data_handler(struct zfcp_fsf_req *);
static void zfcp_fsf_exchange_port_data_handler(struct zfcp_fsf_req *);
static int zfcp_fsf_open_port_handler(struct zfcp_fsf_req *);
static int zfcp_fsf_close_port_handler(struct zfcp_fsf_req *);
static int zfcp_fsf_close_physical_port_handler(struct zfcp_fsf_req *);
static int zfcp_fsf_open_unit_handler(struct zfcp_fsf_req *);
static int zfcp_fsf_close_unit_handler(struct zfcp_fsf_req *);
static int zfcp_fsf_send_fcp_command_handler(struct zfcp_fsf_req *);
static int zfcp_fsf_send_fcp_command_task_handler(struct zfcp_fsf_req *);
static int zfcp_fsf_send_fcp_command_task_management_handler(
	struct zfcp_fsf_req *);
static int zfcp_fsf_abort_fcp_command_handler(struct zfcp_fsf_req *);
static int zfcp_fsf_status_read_handler(struct zfcp_fsf_req *);
static int zfcp_fsf_send_ct_handler(struct zfcp_fsf_req *);
static int zfcp_fsf_send_els_handler(struct zfcp_fsf_req *);
39
static void zfcp_fsf_control_file_handler(struct zfcp_fsf_req *);
L
Linus Torvalds 已提交
40 41 42 43 44
static inline int zfcp_fsf_req_sbal_check(
	unsigned long *, struct zfcp_qdio_queue *, int);
static inline int zfcp_use_one_sbal(
	struct scatterlist *, int, struct scatterlist *, int);
static struct zfcp_fsf_req *zfcp_fsf_req_alloc(mempool_t *, int);
45
static int zfcp_fsf_req_send(struct zfcp_fsf_req *);
L
Linus Torvalds 已提交
46 47 48
static int zfcp_fsf_protstatus_eval(struct zfcp_fsf_req *);
static int zfcp_fsf_fsfstatus_eval(struct zfcp_fsf_req *);
static int zfcp_fsf_fsfstatus_qual_eval(struct zfcp_fsf_req *);
49
static void zfcp_fsf_link_down_info_eval(struct zfcp_fsf_req *, u8,
50
	struct fsf_link_down_info *);
L
Linus Torvalds 已提交
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
static int zfcp_fsf_req_dispatch(struct zfcp_fsf_req *);

/* 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
};

static const char zfcp_act_subtable_type[5][8] = {
	"unknown", "OS", "WWPN", "DID", "LUN"
};

/****************************************************************/
/*************** FSF related Functions  *************************/
/****************************************************************/

#define ZFCP_LOG_AREA			ZFCP_LOG_AREA_FSF

/*
 * function:	zfcp_fsf_req_alloc
 *
S
Swen Schillig 已提交
83
 * purpose:     Obtains an fsf_req and potentially a qtcb (for all but
L
Linus Torvalds 已提交
84 85
 *              unsolicited requests) via helper functions
 *              Does some initial fsf request set-up.
S
Swen Schillig 已提交
86
 *
L
Linus Torvalds 已提交
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
 * returns:	pointer to allocated fsf_req if successfull
 *              NULL otherwise
 *
 * locks:       none
 *
 */
static struct zfcp_fsf_req *
zfcp_fsf_req_alloc(mempool_t *pool, int req_flags)
{
	size_t size;
	void *ptr;
	struct zfcp_fsf_req *fsf_req = NULL;

	if (req_flags & ZFCP_REQ_NO_QTCB)
		size = sizeof(struct zfcp_fsf_req);
	else
103
		size = sizeof(struct zfcp_fsf_req_qtcb);
L
Linus Torvalds 已提交
104

105
	if (likely(pool))
L
Linus Torvalds 已提交
106
		ptr = mempool_alloc(pool, GFP_ATOMIC);
107 108 109 110 111
	else {
		if (req_flags & ZFCP_REQ_NO_QTCB)
			ptr = kmalloc(size, GFP_ATOMIC);
		else
			ptr = kmem_cache_alloc(zfcp_data.fsf_req_qtcb_cache,
112
					       GFP_ATOMIC);
113
	}
L
Linus Torvalds 已提交
114

115
	if (unlikely(!ptr))
L
Linus Torvalds 已提交
116 117 118 119 120 121 122
		goto out;

	memset(ptr, 0, size);

	if (req_flags & ZFCP_REQ_NO_QTCB) {
		fsf_req = (struct zfcp_fsf_req *) ptr;
	} else {
123 124
		fsf_req = &((struct zfcp_fsf_req_qtcb *) ptr)->fsf_req;
		fsf_req->qtcb =	&((struct zfcp_fsf_req_qtcb *) ptr)->qtcb;
L
Linus Torvalds 已提交
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
	}

	fsf_req->pool = pool;

 out:
	return fsf_req;
}

/*
 * function:	zfcp_fsf_req_free
 *
 * purpose:     Frees the memory of an fsf_req (and potentially a qtcb) or
 *              returns it into the pool via helper functions.
 *
 * returns:     sod all
 *
 * locks:       none
 */
143
void
L
Linus Torvalds 已提交
144 145
zfcp_fsf_req_free(struct zfcp_fsf_req *fsf_req)
{
146
	if (likely(fsf_req->pool)) {
L
Linus Torvalds 已提交
147
		mempool_free(fsf_req, fsf_req->pool);
148 149 150 151 152 153 154 155 156
		return;
	}

	if (fsf_req->qtcb) {
		kmem_cache_free(zfcp_data.fsf_req_qtcb_cache, fsf_req);
		return;
	}

	kfree(fsf_req);
L
Linus Torvalds 已提交
157 158
}

159 160 161 162 163
/*
 * 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 已提交
164
 */
165
void zfcp_fsf_req_dismiss_all(struct zfcp_adapter *adapter)
L
Linus Torvalds 已提交
166
{
167
	struct zfcp_fsf_req *fsf_req, *tmp;
168
	unsigned long flags;
169
	LIST_HEAD(remove_queue);
170
	unsigned int i;
171

172
	BUG_ON(atomic_test_mask(ZFCP_STATUS_ADAPTER_QDIOUP, &adapter->status));
173
	spin_lock_irqsave(&adapter->req_list_lock, flags);
174
	for (i = 0; i < REQUEST_LIST_SIZE; i++)
175
		list_splice_init(&adapter->req_list[i], &remove_queue);
176 177
	spin_unlock_irqrestore(&adapter->req_list_lock, flags);

178 179 180 181
	list_for_each_entry_safe(fsf_req, tmp, &remove_queue, list) {
		list_del(&fsf_req->list);
		fsf_req->status |= ZFCP_STATUS_FSFREQ_DISMISSED;
		zfcp_fsf_req_complete(fsf_req);
182
	}
L
Linus Torvalds 已提交
183 184 185 186 187 188 189 190 191 192 193
}

/*
 * function:    zfcp_fsf_req_complete
 *
 * purpose:	Updates active counts and timers for openfcp-reqs
 *              May cleanup request after req_eval returns
 *
 * returns:	0 - success
 *		!0 - failure
 *
S
Swen Schillig 已提交
194
 * context:
L
Linus Torvalds 已提交
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
 */
int
zfcp_fsf_req_complete(struct zfcp_fsf_req *fsf_req)
{
	int retval = 0;
	int cleanup;

	if (unlikely(fsf_req->fsf_command == FSF_QTCB_UNSOLICITED_STATUS)) {
		ZFCP_LOG_DEBUG("Status read response received\n");
		/*
		 * Note: all cleanup handling is done in the callchain of
		 * the function call-chain below.
		 */
		zfcp_fsf_status_read_handler(fsf_req);
		goto out;
210 211
	} else {
		del_timer(&fsf_req->timer);
L
Linus Torvalds 已提交
212
		zfcp_fsf_protstatus_eval(fsf_req);
213
	}
L
Linus Torvalds 已提交
214 215

	/*
S
Swen Schillig 已提交
216 217
	 * fsf_req may be deleted due to waking up functions, so
	 * cleanup is saved here and used later
L
Linus Torvalds 已提交
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
	 */
	if (likely(fsf_req->status & ZFCP_STATUS_FSFREQ_CLEANUP))
		cleanup = 1;
	else
		cleanup = 0;

	fsf_req->status |= ZFCP_STATUS_FSFREQ_COMPLETED;

	/* cleanup request if requested by initiator */
	if (likely(cleanup)) {
		ZFCP_LOG_TRACE("removing FSF request %p\n", fsf_req);
		/*
		 * lock must not be held here since it will be
		 * grabed by the called routine, too
		 */
233
		zfcp_fsf_req_free(fsf_req);
L
Linus Torvalds 已提交
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
	} else {
		/* notify initiator waiting for the requests completion */
		ZFCP_LOG_TRACE("waking initiator of FSF request %p\n",fsf_req);
		/*
		 * 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(&fsf_req->completion_wq);
	}

 out:
	return retval;
}

/*
 * function:    zfcp_fsf_protstatus_eval
 *
 * purpose:	evaluates the QTCB of the finished FSF request
 *		and initiates appropriate actions
 *		(usually calling FSF command specific handlers)
 *
S
Swen Schillig 已提交
261
 * returns:
L
Linus Torvalds 已提交
262
 *
S
Swen Schillig 已提交
263
 * context:
L
Linus Torvalds 已提交
264 265 266 267 268 269 270 271
 *
 * locks:
 */
static int
zfcp_fsf_protstatus_eval(struct zfcp_fsf_req *fsf_req)
{
	int retval = 0;
	struct zfcp_adapter *adapter = fsf_req->adapter;
272 273 274
	struct fsf_qtcb *qtcb = fsf_req->qtcb;
	union fsf_prot_status_qual *prot_status_qual =
		&qtcb->prefix.prot_status_qual;
L
Linus Torvalds 已提交
275

276
	zfcp_hba_dbf_event_fsf_response(fsf_req);
L
Linus Torvalds 已提交
277 278 279 280 281 282 283 284 285 286

	if (fsf_req->status & ZFCP_STATUS_FSFREQ_DISMISSED) {
		ZFCP_LOG_DEBUG("fsf_req 0x%lx has been dismissed\n",
			       (unsigned long) fsf_req);
		fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR |
			ZFCP_STATUS_FSFREQ_RETRY; /* only for SCSI cmnds. */
		goto skip_protstatus;
	}

	/* evaluate FSF Protocol Status */
287
	switch (qtcb->prefix.prot_status) {
L
Linus Torvalds 已提交
288 289 290 291 292 293 294 295 296 297

	case FSF_PROT_GOOD:
	case FSF_PROT_FSF_STATUS_PRESENTED:
		break;

	case FSF_PROT_QTCB_VERSION_ERROR:
		ZFCP_LOG_NORMAL("error: The adapter %s contains "
				"microcode of version 0x%x, the device driver "
				"only supports 0x%x. Aborting.\n",
				zfcp_get_busid_by_adapter(adapter),
298 299
				prot_status_qual->version_error.fsf_version,
				ZFCP_QTCB_VERSION);
300
		zfcp_erp_adapter_shutdown(adapter, 0, 117, fsf_req);
L
Linus Torvalds 已提交
301 302 303 304 305 306 307
		fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
		break;

	case FSF_PROT_SEQ_NUMB_ERROR:
		ZFCP_LOG_NORMAL("bug: Sequence number mismatch between "
				"driver (0x%x) and adapter %s (0x%x). "
				"Restarting all operations on this adapter.\n",
308
				qtcb->prefix.req_seq_no,
L
Linus Torvalds 已提交
309
				zfcp_get_busid_by_adapter(adapter),
310
				prot_status_qual->sequence_error.exp_req_seq_no);
311
		zfcp_erp_adapter_reopen(adapter, 0, 98, fsf_req);
L
Linus Torvalds 已提交
312 313 314 315 316 317 318 319 320 321
		fsf_req->status |= ZFCP_STATUS_FSFREQ_RETRY;
		fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
		break;

	case FSF_PROT_UNSUPP_QTCB_TYPE:
		ZFCP_LOG_NORMAL("error: Packet header type used by the "
				"device driver is incompatible with "
				"that used on adapter %s. "
				"Stopping all operations on this adapter.\n",
				zfcp_get_busid_by_adapter(adapter));
322
		zfcp_erp_adapter_shutdown(adapter, 0, 118, fsf_req);
L
Linus Torvalds 已提交
323 324 325 326 327 328 329 330 331 332 333 334
		fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
		break;

	case FSF_PROT_HOST_CONNECTION_INITIALIZING:
		fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
		atomic_set_mask(ZFCP_STATUS_ADAPTER_HOST_CON_INIT,
				&(adapter->status));
		break;

	case FSF_PROT_DUPLICATE_REQUEST_ID:
			ZFCP_LOG_NORMAL("bug: The request identifier 0x%Lx "
					"to the adapter %s is ambiguous. "
335 336 337
				"Stopping all operations on this adapter.\n",
				*(unsigned long long*)
				(&qtcb->bottom.support.req_handle),
L
Linus Torvalds 已提交
338
					zfcp_get_busid_by_adapter(adapter));
339
		zfcp_erp_adapter_shutdown(adapter, 0, 78, fsf_req);
L
Linus Torvalds 已提交
340 341 342 343
		fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
		break;

	case FSF_PROT_LINK_DOWN:
344
		zfcp_fsf_link_down_info_eval(fsf_req, 37,
345
					     &prot_status_qual->link_down_info);
346
		/* FIXME: reopening adapter now? better wait for link up */
347
		zfcp_erp_adapter_reopen(adapter, 0, 79, fsf_req);
L
Linus Torvalds 已提交
348 349 350 351
		fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
		break;

	case FSF_PROT_REEST_QUEUE:
352
		ZFCP_LOG_NORMAL("The local link to adapter with "
L
Linus Torvalds 已提交
353 354 355 356
			      "%s was re-plugged. "
			      "Re-starting operations on this adapter.\n",
			      zfcp_get_busid_by_adapter(adapter));
		/* All ports should be marked as ready to run again */
357 358
		zfcp_erp_modify_adapter_status(adapter, 28, NULL,
					       ZFCP_STATUS_COMMON_RUNNING,
L
Linus Torvalds 已提交
359 360 361
					       ZFCP_SET);
		zfcp_erp_adapter_reopen(adapter,
					ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED
362
					| ZFCP_STATUS_COMMON_ERP_FAILED,
363
					99, fsf_req);
L
Linus Torvalds 已提交
364 365 366 367 368 369 370 371 372
		fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
		break;

	case FSF_PROT_ERROR_STATE:
		ZFCP_LOG_NORMAL("error: The adapter %s "
				"has entered the error state. "
				"Restarting all operations on this "
				"adapter.\n",
				zfcp_get_busid_by_adapter(adapter));
373
		zfcp_erp_adapter_reopen(adapter, 0, 100, fsf_req);
L
Linus Torvalds 已提交
374 375 376 377 378 379 380 381 382 383 384
		fsf_req->status |= ZFCP_STATUS_FSFREQ_RETRY;
		fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
		break;

	default:
		ZFCP_LOG_NORMAL("bug: Transfer protocol status information "
				"provided by the adapter %s "
				"is not compatible with the device driver. "
				"Stopping all operations on this adapter. "
				"(debug info 0x%x).\n",
				zfcp_get_busid_by_adapter(adapter),
385
				qtcb->prefix.prot_status);
386
		zfcp_erp_adapter_shutdown(adapter, 0, 119, fsf_req);
L
Linus Torvalds 已提交
387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424
		fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
	}

 skip_protstatus:
	/*
	 * always call specific handlers to give them a chance to do
	 * something meaningful even in error cases
	 */
	zfcp_fsf_fsfstatus_eval(fsf_req);
	return retval;
}

/*
 * function:	zfcp_fsf_fsfstatus_eval
 *
 * purpose:	evaluates FSF status of completed FSF request
 *		and acts accordingly
 *
 * returns:
 */
static int
zfcp_fsf_fsfstatus_eval(struct zfcp_fsf_req *fsf_req)
{
	int retval = 0;

	if (unlikely(fsf_req->status & ZFCP_STATUS_FSFREQ_ERROR)) {
		goto skip_fsfstatus;
	}

	/* evaluate FSF Status */
	switch (fsf_req->qtcb->header.fsf_status) {
	case FSF_UNKNOWN_COMMAND:
		ZFCP_LOG_NORMAL("bug: Command issued by the device driver is "
				"not known by the adapter %s "
				"Stopping all operations on this adapter. "
				"(debug info 0x%x).\n",
				zfcp_get_busid_by_adapter(fsf_req->adapter),
				fsf_req->qtcb->header.fsf_command);
425
		zfcp_erp_adapter_shutdown(fsf_req->adapter, 0, 120, fsf_req);
L
Linus Torvalds 已提交
426 427 428 429 430 431 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
		fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
		break;

	case FSF_FCP_RSP_AVAILABLE:
		ZFCP_LOG_DEBUG("FCP Sense data will be presented to the "
			       "SCSI stack.\n");
		break;

	case FSF_ADAPTER_STATUS_AVAILABLE:
		zfcp_fsf_fsfstatus_qual_eval(fsf_req);
		break;
	}

 skip_fsfstatus:
	/*
	 * always call specific handlers to give them a chance to do
	 * something meaningful even in error cases
	 */
	zfcp_fsf_req_dispatch(fsf_req);

	return retval;
}

/*
 * function:	zfcp_fsf_fsfstatus_qual_eval
 *
 * purpose:	evaluates FSF status-qualifier of completed FSF request
 *		and acts accordingly
 *
 * returns:
 */
static int
zfcp_fsf_fsfstatus_qual_eval(struct zfcp_fsf_req *fsf_req)
{
	int retval = 0;

	switch (fsf_req->qtcb->header.fsf_status_qual.word[0]) {
	case FSF_SQ_FCP_RSP_AVAILABLE:
		break;
	case FSF_SQ_RETRY_IF_POSSIBLE:
		/* The SCSI-stack may now issue retries or escalate */
		fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
		break;
	case FSF_SQ_COMMAND_ABORTED:
		/* Carry the aborted state on to upper layer */
		fsf_req->status |= ZFCP_STATUS_FSFREQ_ABORTED;
		fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
		break;
	case FSF_SQ_NO_RECOM:
475
		ZFCP_LOG_NORMAL("bug: No recommendation could be given for a "
L
Linus Torvalds 已提交
476 477 478
				"problem on the adapter %s "
				"Stopping all operations on this adapter. ",
				zfcp_get_busid_by_adapter(fsf_req->adapter));
479
		zfcp_erp_adapter_shutdown(fsf_req->adapter, 0, 121, fsf_req);
L
Linus Torvalds 已提交
480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505
		fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
		break;
	case FSF_SQ_ULP_PROGRAMMING_ERROR:
		ZFCP_LOG_NORMAL("error: not enough SBALs for data transfer "
				"(adapter %s)\n",
				zfcp_get_busid_by_adapter(fsf_req->adapter));
		fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
		break;
	case FSF_SQ_INVOKE_LINK_TEST_PROCEDURE:
	case FSF_SQ_NO_RETRY_POSSIBLE:
	case FSF_SQ_ULP_DEPENDENT_ERP_REQUIRED:
		/* dealt with in the respective functions */
		break;
	default:
		ZFCP_LOG_NORMAL("bug: Additional status info could "
				"not be interpreted properly.\n");
		ZFCP_HEX_DUMP(ZFCP_LOG_LEVEL_NORMAL,
			      (char *) &fsf_req->qtcb->header.fsf_status_qual,
			      sizeof (union fsf_status_qual));
		fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
		break;
	}

	return retval;
}

506 507 508 509
/**
 * zfcp_fsf_link_down_info_eval - evaluate link down information block
 */
static void
510
zfcp_fsf_link_down_info_eval(struct zfcp_fsf_req *fsf_req, u8 id,
511 512
			     struct fsf_link_down_info *link_down)
{
513 514
	struct zfcp_adapter *adapter = fsf_req->adapter;

515 516 517 518 519 520
	if (atomic_test_mask(ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED,
	                     &adapter->status))
		return;

	atomic_set_mask(ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED, &adapter->status);

521 522
	if (link_down == NULL)
		goto out;
523

524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 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 590 591 592 593 594 595 596 597 598 599 600 601 602 603
	switch (link_down->error_code) {
	case FSF_PSQ_LINK_NO_LIGHT:
		ZFCP_LOG_NORMAL("The local link to adapter %s is down "
				"(no light detected)\n",
				zfcp_get_busid_by_adapter(adapter));
		break;
	case FSF_PSQ_LINK_WRAP_PLUG:
		ZFCP_LOG_NORMAL("The local link to adapter %s is down "
				"(wrap plug detected)\n",
				zfcp_get_busid_by_adapter(adapter));
		break;
	case FSF_PSQ_LINK_NO_FCP:
		ZFCP_LOG_NORMAL("The local link to adapter %s is down "
				"(adjacent node on link does not support FCP)\n",
				zfcp_get_busid_by_adapter(adapter));
		break;
	case FSF_PSQ_LINK_FIRMWARE_UPDATE:
		ZFCP_LOG_NORMAL("The local link to adapter %s is down "
				"(firmware update in progress)\n",
				zfcp_get_busid_by_adapter(adapter));
			break;
	case FSF_PSQ_LINK_INVALID_WWPN:
		ZFCP_LOG_NORMAL("The local link to adapter %s is down "
				"(duplicate or invalid WWPN detected)\n",
				zfcp_get_busid_by_adapter(adapter));
		break;
	case FSF_PSQ_LINK_NO_NPIV_SUPPORT:
		ZFCP_LOG_NORMAL("The local link to adapter %s is down "
				"(no support for NPIV by Fabric)\n",
				zfcp_get_busid_by_adapter(adapter));
		break;
	case FSF_PSQ_LINK_NO_FCP_RESOURCES:
		ZFCP_LOG_NORMAL("The local link to adapter %s is down "
				"(out of resource in FCP daughtercard)\n",
				zfcp_get_busid_by_adapter(adapter));
		break;
	case FSF_PSQ_LINK_NO_FABRIC_RESOURCES:
		ZFCP_LOG_NORMAL("The local link to adapter %s is down "
				"(out of resource in Fabric)\n",
				zfcp_get_busid_by_adapter(adapter));
		break;
	case FSF_PSQ_LINK_FABRIC_LOGIN_UNABLE:
		ZFCP_LOG_NORMAL("The local link to adapter %s is down "
				"(unable to Fabric login)\n",
				zfcp_get_busid_by_adapter(adapter));
		break;
	case FSF_PSQ_LINK_WWPN_ASSIGNMENT_CORRUPTED:
		ZFCP_LOG_NORMAL("WWPN assignment file corrupted on adapter %s\n",
				zfcp_get_busid_by_adapter(adapter));
		break;
	case FSF_PSQ_LINK_MODE_TABLE_CURRUPTED:
		ZFCP_LOG_NORMAL("Mode table corrupted on adapter %s\n",
				zfcp_get_busid_by_adapter(adapter));
		break;
	case FSF_PSQ_LINK_NO_WWPN_ASSIGNMENT:
		ZFCP_LOG_NORMAL("No WWPN for assignment table on adapter %s\n",
				zfcp_get_busid_by_adapter(adapter));
		break;
	default:
		ZFCP_LOG_NORMAL("The local link to adapter %s is down "
				"(warning: unknown reason code %d)\n",
				zfcp_get_busid_by_adapter(adapter),
				link_down->error_code);
	}

	if (adapter->connection_features & FSF_FEATURE_NPIV_MODE)
		ZFCP_LOG_DEBUG("Debug information to link down: "
		               "primary_status=0x%02x "
		               "ioerr_code=0x%02x "
		               "action_code=0x%02x "
		               "reason_code=0x%02x "
		               "explanation_code=0x%02x "
		               "vendor_specific_code=0x%02x\n",
				link_down->primary_status,
				link_down->ioerr_code,
				link_down->action_code,
				link_down->reason_code,
				link_down->explanation_code,
				link_down->vendor_specific_code);

604
 out:
605
	zfcp_erp_adapter_failed(adapter, id, fsf_req);
606 607
}

L
Linus Torvalds 已提交
608 609 610 611 612
/*
 * function:	zfcp_fsf_req_dispatch
 *
 * purpose:	calls the appropriate command specific handler
 *
S
Swen Schillig 已提交
613
 * returns:
L
Linus Torvalds 已提交
614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680
 */
static int
zfcp_fsf_req_dispatch(struct zfcp_fsf_req *fsf_req)
{
	struct zfcp_erp_action *erp_action = fsf_req->erp_action;
	struct zfcp_adapter *adapter = fsf_req->adapter;
	int retval = 0;


	switch (fsf_req->fsf_command) {

	case FSF_QTCB_FCP_CMND:
		zfcp_fsf_send_fcp_command_handler(fsf_req);
		break;

	case FSF_QTCB_ABORT_FCP_CMND:
		zfcp_fsf_abort_fcp_command_handler(fsf_req);
		break;

	case FSF_QTCB_SEND_GENERIC:
		zfcp_fsf_send_ct_handler(fsf_req);
		break;

	case FSF_QTCB_OPEN_PORT_WITH_DID:
		zfcp_fsf_open_port_handler(fsf_req);
		break;

	case FSF_QTCB_OPEN_LUN:
		zfcp_fsf_open_unit_handler(fsf_req);
		break;

	case FSF_QTCB_CLOSE_LUN:
		zfcp_fsf_close_unit_handler(fsf_req);
		break;

	case FSF_QTCB_CLOSE_PORT:
		zfcp_fsf_close_port_handler(fsf_req);
		break;

	case FSF_QTCB_CLOSE_PHYSICAL_PORT:
		zfcp_fsf_close_physical_port_handler(fsf_req);
		break;

	case FSF_QTCB_EXCHANGE_CONFIG_DATA:
		zfcp_fsf_exchange_config_data_handler(fsf_req);
		break;

	case FSF_QTCB_EXCHANGE_PORT_DATA:
		zfcp_fsf_exchange_port_data_handler(fsf_req);
		break;

	case FSF_QTCB_SEND_ELS:
		zfcp_fsf_send_els_handler(fsf_req);
		break;

	case FSF_QTCB_DOWNLOAD_CONTROL_FILE:
		zfcp_fsf_control_file_handler(fsf_req);
		break;

	case FSF_QTCB_UPLOAD_CONTROL_FILE:
		zfcp_fsf_control_file_handler(fsf_req);
		break;

	default:
		fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
		ZFCP_LOG_NORMAL("bug: Command issued by the device driver is "
				"not supported by the adapter %s\n",
681
				zfcp_get_busid_by_adapter(adapter));
L
Linus Torvalds 已提交
682 683 684 685 686
		if (fsf_req->fsf_command != fsf_req->qtcb->header.fsf_command)
			ZFCP_LOG_NORMAL
			    ("bug: Command issued by the device driver differs "
			     "from the command returned by the adapter %s "
			     "(debug info 0x%x, 0x%x).\n",
687
			     zfcp_get_busid_by_adapter(adapter),
L
Linus Torvalds 已提交
688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713
			     fsf_req->fsf_command,
			     fsf_req->qtcb->header.fsf_command);
	}

	if (!erp_action)
		return retval;

	zfcp_erp_async_handler(erp_action, 0);

	return retval;
}

/*
 * function:    zfcp_fsf_status_read
 *
 * purpose:	initiates a Status Read command at the specified adapter
 *
 * returns:
 */
int
zfcp_fsf_status_read(struct zfcp_adapter *adapter, int req_flags)
{
	struct zfcp_fsf_req *fsf_req;
	struct fsf_status_read_buffer *status_buffer;
	unsigned long lock_flags;
	volatile struct qdio_buffer_element *sbale;
714
	int retval;
L
Linus Torvalds 已提交
715 716 717 718 719 720 721 722 723 724 725 726 727

	/* setup new FSF request */
	retval = zfcp_fsf_req_create(adapter, FSF_QTCB_UNSOLICITED_STATUS,
				     req_flags | ZFCP_REQ_NO_QTCB,
				     adapter->pool.fsf_req_status_read,
				     &lock_flags, &fsf_req);
	if (retval < 0) {
		ZFCP_LOG_INFO("error: Could not create unsolicited status "
			      "buffer for adapter %s.\n",
			      zfcp_get_busid_by_adapter(adapter));
		goto failed_req_create;
	}

S
Swen Schillig 已提交
728
	sbale = zfcp_qdio_sbale_req(fsf_req);
L
Linus Torvalds 已提交
729 730 731 732
        sbale[0].flags |= SBAL_FLAGS0_TYPE_STATUS;
        sbale[2].flags |= SBAL_FLAGS_LAST_ENTRY;
        fsf_req->sbale_curr = 2;

733
	retval = -ENOMEM;
L
Linus Torvalds 已提交
734 735
	status_buffer =
		mempool_alloc(adapter->pool.data_status_read, GFP_ATOMIC);
736
	if (!status_buffer)
L
Linus Torvalds 已提交
737 738
		goto failed_buf;
	memset(status_buffer, 0, sizeof (struct fsf_status_read_buffer));
739
	fsf_req->data = (unsigned long) status_buffer;
L
Linus Torvalds 已提交
740 741 742 743 744 745

	/* insert pointer to respective buffer */
	sbale = zfcp_qdio_sbale_curr(fsf_req);
	sbale->addr = (void *) status_buffer;
	sbale->length = sizeof(struct fsf_status_read_buffer);

746
	retval = zfcp_fsf_req_send(fsf_req);
L
Linus Torvalds 已提交
747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762
	if (retval) {
		ZFCP_LOG_DEBUG("error: Could not set-up unsolicited status "
			       "environment.\n");
		goto failed_req_send;
	}

	ZFCP_LOG_TRACE("Status Read request initiated (adapter%s)\n",
		       zfcp_get_busid_by_adapter(adapter));
	goto out;

 failed_req_send:
	mempool_free(status_buffer, adapter->pool.data_status_read);

 failed_buf:
	zfcp_fsf_req_free(fsf_req);
 failed_req_create:
763
	zfcp_hba_dbf_event_fsf_unsol("fail", adapter, NULL);
L
Linus Torvalds 已提交
764
 out:
S
Swen Schillig 已提交
765
	write_unlock_irqrestore(&adapter->req_q.lock, lock_flags);
L
Linus Torvalds 已提交
766 767 768 769 770 771 772 773 774 775 776
	return retval;
}

static int
zfcp_fsf_status_read_port_closed(struct zfcp_fsf_req *fsf_req)
{
	struct fsf_status_read_buffer *status_buffer;
	struct zfcp_adapter *adapter;
	struct zfcp_port *port;
	unsigned long flags;

777
	status_buffer = (struct fsf_status_read_buffer *) fsf_req->data;
L
Linus Torvalds 已提交
778 779 780 781 782 783 784 785 786
	adapter = fsf_req->adapter;

	read_lock_irqsave(&zfcp_data.config_lock, flags);
	list_for_each_entry(port, &adapter->port_list_head, list)
	    if (port->d_id == (status_buffer->d_id & ZFCP_DID_MASK))
		break;
	read_unlock_irqrestore(&zfcp_data.config_lock, flags);

	if (!port || (port->d_id != (status_buffer->d_id & ZFCP_DID_MASK))) {
787
		ZFCP_LOG_NORMAL("bug: Reopen port indication received for "
788
				"nonexisting port with d_id 0x%06x on "
L
Linus Torvalds 已提交
789 790 791 792 793 794 795 796 797
				"adapter %s. Ignored.\n",
				status_buffer->d_id & ZFCP_DID_MASK,
				zfcp_get_busid_by_adapter(adapter));
		goto out;
	}

	switch (status_buffer->status_subtype) {

	case FSF_STATUS_READ_SUB_CLOSE_PHYS_PORT:
798
		zfcp_erp_port_reopen(port, 0, 101, fsf_req);
L
Linus Torvalds 已提交
799 800 801
		break;

	case FSF_STATUS_READ_SUB_ERROR_PORT:
802
		zfcp_erp_port_shutdown(port, 0, 122, fsf_req);
L
Linus Torvalds 已提交
803 804 805 806 807
		break;

	default:
		ZFCP_LOG_NORMAL("bug: Undefined status subtype received "
				"for a reopen indication on port with "
808
				"d_id 0x%06x on the adapter %s. "
L
Linus Torvalds 已提交
809 810 811 812 813 814 815 816 817 818 819 820 821 822
				"Ignored. (debug info 0x%x)\n",
				status_buffer->d_id,
				zfcp_get_busid_by_adapter(adapter),
				status_buffer->status_subtype);
	}
 out:
	return 0;
}

/*
 * function:    zfcp_fsf_status_read_handler
 *
 * purpose:	is called for finished Open Port command
 *
S
Swen Schillig 已提交
823
 * returns:
L
Linus Torvalds 已提交
824 825 826 827 828 829 830
 */
static int
zfcp_fsf_status_read_handler(struct zfcp_fsf_req *fsf_req)
{
	int retval = 0;
	struct zfcp_adapter *adapter = fsf_req->adapter;
	struct fsf_status_read_buffer *status_buffer =
831
		(struct fsf_status_read_buffer *) fsf_req->data;
832
	struct fsf_bit_error_payload *fsf_bit_error;
L
Linus Torvalds 已提交
833 834

	if (fsf_req->status & ZFCP_STATUS_FSFREQ_DISMISSED) {
835
		zfcp_hba_dbf_event_fsf_unsol("dism", adapter, status_buffer);
L
Linus Torvalds 已提交
836
		mempool_free(status_buffer, adapter->pool.data_status_read);
837
		zfcp_fsf_req_free(fsf_req);
L
Linus Torvalds 已提交
838 839 840
		goto out;
	}

841 842
	zfcp_hba_dbf_event_fsf_unsol("read", adapter, status_buffer);

L
Linus Torvalds 已提交
843 844 845 846 847 848 849
	switch (status_buffer->status_type) {

	case FSF_STATUS_READ_PORT_CLOSED:
		zfcp_fsf_status_read_port_closed(fsf_req);
		break;

	case FSF_STATUS_READ_INCOMING_ELS:
850
		zfcp_fc_incoming_els(fsf_req);
L
Linus Torvalds 已提交
851 852 853 854 855 856 857 858
		break;

	case FSF_STATUS_READ_SENSE_DATA_AVAIL:
		ZFCP_LOG_INFO("unsolicited sense data received (adapter %s)\n",
			      zfcp_get_busid_by_adapter(adapter));
		break;

	case FSF_STATUS_READ_BIT_ERROR_THRESHOLD:
859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889
		fsf_bit_error = (struct fsf_bit_error_payload *)
			status_buffer->payload;
		ZFCP_LOG_NORMAL("Warning: bit error threshold data "
		    "received (adapter %s, "
		    "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",
		    zfcp_get_busid_by_adapter(adapter),
		    fsf_bit_error->link_failure_error_count,
		    fsf_bit_error->loss_of_sync_error_count,
		    fsf_bit_error->loss_of_signal_error_count,
		    fsf_bit_error->primitive_sequence_error_count,
		    fsf_bit_error->invalid_transmission_word_error_count,
		    fsf_bit_error->crc_error_count);
		ZFCP_LOG_INFO("Additional bit error threshold data "
		    "(adapter %s, "
		    "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",
		    zfcp_get_busid_by_adapter(adapter),
		    fsf_bit_error->primitive_sequence_event_timeout_count,
		    fsf_bit_error->elastic_buffer_overrun_error_count,
		    fsf_bit_error->advertised_receive_b2b_credit,
		    fsf_bit_error->current_receive_b2b_credit,
		    fsf_bit_error->advertised_transmit_b2b_credit,
		    fsf_bit_error->current_transmit_b2b_credit);
L
Linus Torvalds 已提交
890 891 892
		break;

	case FSF_STATUS_READ_LINK_DOWN:
893 894 895 896
		switch (status_buffer->status_subtype) {
		case FSF_STATUS_READ_SUB_NO_PHYSICAL_LINK:
			ZFCP_LOG_INFO("Physical link to adapter %s is down\n",
				      zfcp_get_busid_by_adapter(adapter));
897
			zfcp_fsf_link_down_info_eval(fsf_req, 38,
898 899
				(struct fsf_link_down_info *)
				&status_buffer->payload);
900 901 902 903
			break;
		case FSF_STATUS_READ_SUB_FDISC_FAILED:
			ZFCP_LOG_INFO("Local link to adapter %s is down "
				      "due to failed FDISC login\n",
904
				      zfcp_get_busid_by_adapter(adapter));
905
			zfcp_fsf_link_down_info_eval(fsf_req, 39,
906 907
				(struct fsf_link_down_info *)
				&status_buffer->payload);
908 909 910 911 912
			break;
		case FSF_STATUS_READ_SUB_FIRMWARE_UPDATE:
			ZFCP_LOG_INFO("Local link to adapter %s is down "
				      "due to firmware update on adapter\n",
				      zfcp_get_busid_by_adapter(adapter));
913
			zfcp_fsf_link_down_info_eval(fsf_req, 40, NULL);
914 915 916 917 918
			break;
		default:
			ZFCP_LOG_INFO("Local link to adapter %s is down "
				      "due to unknown reason\n",
				      zfcp_get_busid_by_adapter(adapter));
919
			zfcp_fsf_link_down_info_eval(fsf_req, 41, NULL);
920
		};
L
Linus Torvalds 已提交
921 922 923
		break;

	case FSF_STATUS_READ_LINK_UP:
924
		ZFCP_LOG_NORMAL("Local link to adapter %s was replugged. "
925 926
				"Restarting operations on this adapter\n",
				zfcp_get_busid_by_adapter(adapter));
L
Linus Torvalds 已提交
927
		/* All ports should be marked as ready to run again */
928
		zfcp_erp_modify_adapter_status(adapter, 30, NULL,
L
Linus Torvalds 已提交
929 930 931 932
					       ZFCP_STATUS_COMMON_RUNNING,
					       ZFCP_SET);
		zfcp_erp_adapter_reopen(adapter,
					ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED
933
					| ZFCP_STATUS_COMMON_ERP_FAILED,
934
					102, fsf_req);
L
Linus Torvalds 已提交
935 936
		break;

937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967
	case FSF_STATUS_READ_NOTIFICATION_LOST:
		ZFCP_LOG_NORMAL("Unsolicited status notification(s) lost: "
				"adapter %s%s%s%s%s%s%s%s%s\n",
				zfcp_get_busid_by_adapter(adapter),
				(status_buffer->status_subtype &
					FSF_STATUS_READ_SUB_INCOMING_ELS) ?
					", incoming ELS" : "",
				(status_buffer->status_subtype &
					FSF_STATUS_READ_SUB_SENSE_DATA) ?
					", sense data" : "",
				(status_buffer->status_subtype &
					FSF_STATUS_READ_SUB_LINK_STATUS) ?
					", link status change" : "",
				(status_buffer->status_subtype &
					FSF_STATUS_READ_SUB_PORT_CLOSED) ?
					", port close" : "",
				(status_buffer->status_subtype &
					FSF_STATUS_READ_SUB_BIT_ERROR_THRESHOLD) ?
					", bit error exception" : "",
				(status_buffer->status_subtype &
					FSF_STATUS_READ_SUB_ACT_UPDATED) ?
					", ACT update" : "",
				(status_buffer->status_subtype &
					FSF_STATUS_READ_SUB_ACT_HARDENED) ?
					", ACT hardening" : "",
				(status_buffer->status_subtype &
					FSF_STATUS_READ_SUB_FEATURE_UPDATE_ALERT) ?
					", adapter feature change" : "");

		if (status_buffer->status_subtype &
		    FSF_STATUS_READ_SUB_ACT_UPDATED)
968
			zfcp_erp_adapter_access_changed(adapter, 135, fsf_req);
969 970
		break;

L
Linus Torvalds 已提交
971
	case FSF_STATUS_READ_CFDC_UPDATED:
972
		ZFCP_LOG_NORMAL("CFDC has been updated on the adapter %s\n",
L
Linus Torvalds 已提交
973
			      zfcp_get_busid_by_adapter(adapter));
974
		zfcp_erp_adapter_access_changed(adapter, 136, fsf_req);
L
Linus Torvalds 已提交
975 976 977 978 979
		break;

	case FSF_STATUS_READ_CFDC_HARDENED:
		switch (status_buffer->status_subtype) {
		case FSF_STATUS_READ_SUB_CFDC_HARDENED_ON_SE:
980
			ZFCP_LOG_NORMAL("CFDC of adapter %s saved on SE\n",
L
Linus Torvalds 已提交
981 982 983
				      zfcp_get_busid_by_adapter(adapter));
			break;
		case FSF_STATUS_READ_SUB_CFDC_HARDENED_ON_SE2:
984
			ZFCP_LOG_NORMAL("CFDC of adapter %s has been copied "
L
Linus Torvalds 已提交
985 986 987 988
				      "to the secondary SE\n",
				zfcp_get_busid_by_adapter(adapter));
			break;
		default:
989
			ZFCP_LOG_NORMAL("CFDC of adapter %s has been hardened\n",
L
Linus Torvalds 已提交
990 991 992 993
				      zfcp_get_busid_by_adapter(adapter));
		}
		break;

994 995 996 997 998 999 1000 1001 1002
	case FSF_STATUS_READ_FEATURE_UPDATE_ALERT:
		ZFCP_LOG_INFO("List of supported features on adapter %s has "
			      "been changed from 0x%08X to 0x%08X\n",
			      zfcp_get_busid_by_adapter(adapter),
			      *(u32*) (status_buffer->payload + 4),
			      *(u32*) (status_buffer->payload));
		adapter->adapter_features = *(u32*) status_buffer->payload;
		break;

L
Linus Torvalds 已提交
1003
	default:
1004
		ZFCP_LOG_NORMAL("warning: An unsolicited status packet of unknown "
L
Linus Torvalds 已提交
1005 1006 1007 1008 1009 1010 1011 1012 1013 1014
				"type was received (debug info 0x%x)\n",
				status_buffer->status_type);
		ZFCP_LOG_DEBUG("Dump of status_read_buffer %p:\n",
			       status_buffer);
		ZFCP_HEX_DUMP(ZFCP_LOG_LEVEL_DEBUG,
			      (char *) status_buffer,
			      sizeof (struct fsf_status_read_buffer));
		break;
	}
	mempool_free(status_buffer, adapter->pool.data_status_read);
1015
	zfcp_fsf_req_free(fsf_req);
L
Linus Torvalds 已提交
1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029
	/*
	 * recycle buffer and start new request repeat until outbound
	 * queue is empty or adapter shutdown is requested
	 */
	/*
	 * FIXME(qdio):
	 * we may wait in the req_create for 5s during shutdown, so
	 * qdio_cleanup will have to wait at least that long before returning
	 * with failure to allow us a proper cleanup under all circumstances
	 */
	/*
	 * FIXME:
	 * allocation failure possible? (Is this code needed?)
	 */
1030 1031 1032

	atomic_inc(&adapter->stat_miss);
	schedule_work(&adapter->stat_work);
L
Linus Torvalds 已提交
1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044
 out:
	return retval;
}

/*
 * function:    zfcp_fsf_abort_fcp_command
 *
 * purpose:	tells FSF to abort a running SCSI command
 *
 * returns:	address of initiated FSF request
 *		NULL - request could not be initiated
 *
S
Swen Schillig 已提交
1045
 * FIXME(design): should be watched by a timeout !!!
L
Linus Torvalds 已提交
1046 1047 1048 1049 1050 1051 1052 1053 1054 1055
 * FIXME(design) shouldn't this be modified to return an int
 *               also...don't know how though
 */
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)
{
	volatile struct qdio_buffer_element *sbale;
	struct zfcp_fsf_req *fsf_req = NULL;
1056
	unsigned long lock_flags;
L
Linus Torvalds 已提交
1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072
	int retval = 0;

	/* setup new FSF request */
	retval = zfcp_fsf_req_create(adapter, FSF_QTCB_ABORT_FCP_CMND,
				     req_flags, adapter->pool.fsf_req_abort,
				     &lock_flags, &fsf_req);
	if (retval < 0) {
		ZFCP_LOG_INFO("error: Failed to create an abort command "
			      "request for lun 0x%016Lx on port 0x%016Lx "
			      "on adapter %s.\n",
			      unit->fcp_lun,
			      unit->port->wwpn,
			      zfcp_get_busid_by_adapter(adapter));
		goto out;
	}

1073 1074 1075 1076
	if (unlikely(!atomic_test_mask(ZFCP_STATUS_COMMON_UNBLOCKED,
			&unit->status)))
		goto unit_blocked;

S
Swen Schillig 已提交
1077
	sbale = zfcp_qdio_sbale_req(fsf_req);
L
Linus Torvalds 已提交
1078 1079 1080
        sbale[0].flags |= SBAL_FLAGS0_TYPE_READ;
        sbale[1].flags |= SBAL_FLAGS_LAST_ENTRY;

1081
	fsf_req->data = (unsigned long) unit;
L
Linus Torvalds 已提交
1082 1083 1084 1085 1086 1087 1088 1089

	/* set handles of unit and its parent port in QTCB */
	fsf_req->qtcb->header.lun_handle = unit->handle;
	fsf_req->qtcb->header.port_handle = unit->port->handle;

	/* set handle of request which should be aborted */
	fsf_req->qtcb->bottom.support.req_handle = (u64) old_req_id;

1090 1091
	zfcp_fsf_start_timer(fsf_req, ZFCP_SCSI_ER_TIMEOUT);
	retval = zfcp_fsf_req_send(fsf_req);
1092 1093 1094 1095
	if (!retval)
		goto out;

 unit_blocked:
L
Linus Torvalds 已提交
1096 1097 1098 1099
		zfcp_fsf_req_free(fsf_req);
		fsf_req = NULL;

 out:
S
Swen Schillig 已提交
1100
	write_unlock_irqrestore(&adapter->req_q.lock, lock_flags);
L
Linus Torvalds 已提交
1101 1102 1103 1104 1105 1106 1107 1108
	return fsf_req;
}

/*
 * function:    zfcp_fsf_abort_fcp_command_handler
 *
 * purpose:	is called for finished Abort FCP Command request
 *
S
Swen Schillig 已提交
1109
 * returns:
L
Linus Torvalds 已提交
1110 1111 1112 1113 1114
 */
static int
zfcp_fsf_abort_fcp_command_handler(struct zfcp_fsf_req *new_fsf_req)
{
	int retval = -EINVAL;
1115
	struct zfcp_unit *unit;
1116 1117
	union fsf_status_qual *fsf_stat_qual =
		&new_fsf_req->qtcb->header.fsf_status_qual;
L
Linus Torvalds 已提交
1118 1119 1120 1121 1122 1123

	if (new_fsf_req->status & ZFCP_STATUS_FSFREQ_ERROR) {
		/* do not set ZFCP_STATUS_FSFREQ_ABORTSUCCEEDED */
		goto skip_fsfstatus;
	}

1124 1125
	unit = (struct zfcp_unit *) new_fsf_req->data;

L
Linus Torvalds 已提交
1126 1127 1128 1129
	/* evaluate FSF status in QTCB */
	switch (new_fsf_req->qtcb->header.fsf_status) {

	case FSF_PORT_HANDLE_NOT_VALID:
1130
		if (fsf_stat_qual->word[0] != fsf_stat_qual->word[1]) {
L
Linus Torvalds 已提交
1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148
			/*
			 * In this case a command that was sent prior to a port
			 * reopen was aborted (handles are different). This is
			 * fine.
			 */
		} else {
			ZFCP_LOG_INFO("Temporary port identifier 0x%x for "
				      "port 0x%016Lx on adapter %s invalid. "
				      "This may happen occasionally.\n",
				      unit->port->handle,
				      unit->port->wwpn,
				      zfcp_get_busid_by_unit(unit));
			ZFCP_LOG_INFO("status qualifier:\n");
			ZFCP_HEX_DUMP(ZFCP_LOG_LEVEL_INFO,
				      (char *) &new_fsf_req->qtcb->header.
				      fsf_status_qual,
				      sizeof (union fsf_status_qual));
			/* Let's hope this sorts out the mess */
1149
			zfcp_erp_adapter_reopen(unit->port->adapter, 0, 104,
1150
						new_fsf_req);
L
Linus Torvalds 已提交
1151 1152 1153 1154 1155
			new_fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
		}
		break;

	case FSF_LUN_HANDLE_NOT_VALID:
1156
		if (fsf_stat_qual->word[0] != fsf_stat_qual->word[1]) {
L
Linus Torvalds 已提交
1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177
			/*
			 * In this case a command that was sent prior to a unit
			 * reopen was aborted (handles are different).
			 * This is fine.
			 */
		} else {
			ZFCP_LOG_INFO
			    ("Warning: Temporary LUN identifier 0x%x of LUN "
			     "0x%016Lx on port 0x%016Lx on adapter %s is "
			     "invalid. This may happen in rare cases. "
			     "Trying to re-establish link.\n",
			     unit->handle,
			     unit->fcp_lun,
			     unit->port->wwpn,
			     zfcp_get_busid_by_unit(unit));
			ZFCP_LOG_DEBUG("Status qualifier data:\n");
			ZFCP_HEX_DUMP(ZFCP_LOG_LEVEL_DEBUG,
				      (char *) &new_fsf_req->qtcb->header.
				      fsf_status_qual,
				      sizeof (union fsf_status_qual));
			/* Let's hope this sorts out the mess */
1178
			zfcp_erp_port_reopen(unit->port, 0, 105, new_fsf_req);
L
Linus Torvalds 已提交
1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191
			new_fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
		}
		break;

	case FSF_FCP_COMMAND_DOES_NOT_EXIST:
		retval = 0;
		new_fsf_req->status |= ZFCP_STATUS_FSFREQ_ABORTNOTNEEDED;
		break;

	case FSF_PORT_BOXED:
		ZFCP_LOG_INFO("Remote port 0x%016Lx on adapter %s needs to "
			      "be reopened\n", unit->port->wwpn,
			      zfcp_get_busid_by_unit(unit));
1192
		zfcp_erp_port_boxed(unit->port, 47, new_fsf_req);
L
Linus Torvalds 已提交
1193 1194 1195 1196 1197 1198 1199 1200 1201 1202
		new_fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR
		    | ZFCP_STATUS_FSFREQ_RETRY;
		break;

	case FSF_LUN_BOXED:
                ZFCP_LOG_INFO(
                        "unit 0x%016Lx on port 0x%016Lx on adapter %s needs "
                        "to be reopened\n",
                        unit->fcp_lun, unit->port->wwpn,
                        zfcp_get_busid_by_unit(unit));
1203
		zfcp_erp_unit_boxed(unit, 48, new_fsf_req);
L
Linus Torvalds 已提交
1204 1205 1206 1207 1208 1209 1210
                new_fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR
                        | ZFCP_STATUS_FSFREQ_RETRY;
                break;

	case FSF_ADAPTER_STATUS_AVAILABLE:
		switch (new_fsf_req->qtcb->header.fsf_status_qual.word[0]) {
		case FSF_SQ_INVOKE_LINK_TEST_PROCEDURE:
1211
			zfcp_test_link(unit->port);
L
Linus Torvalds 已提交
1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 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 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296
			new_fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
			break;
		case FSF_SQ_ULP_DEPENDENT_ERP_REQUIRED:
			/* SCSI stack will escalate */
			new_fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
			break;
		default:
			ZFCP_LOG_NORMAL
			    ("bug: Wrong status qualifier 0x%x arrived.\n",
			     new_fsf_req->qtcb->header.fsf_status_qual.word[0]);
			break;
		}
		break;

	case FSF_GOOD:
		retval = 0;
		new_fsf_req->status |= ZFCP_STATUS_FSFREQ_ABORTSUCCEEDED;
		break;

	default:
		ZFCP_LOG_NORMAL("bug: An unknown FSF Status was presented "
				"(debug info 0x%x)\n",
				new_fsf_req->qtcb->header.fsf_status);
		break;
	}
 skip_fsfstatus:
	return retval;
}

/**
 * zfcp_use_one_sbal - checks whether req buffer and resp bother each fit into
 *	one SBALE
 * Two scatter-gather lists are passed, one for the reqeust and one for the
 * response.
 */
static inline int
zfcp_use_one_sbal(struct scatterlist *req, int req_count,
                  struct scatterlist *resp, int resp_count)
{
        return ((req_count == 1) &&
		(resp_count == 1) &&
                (((unsigned long) zfcp_sg_to_address(&req[0]) &
		  PAGE_MASK) ==
		 ((unsigned long) (zfcp_sg_to_address(&req[0]) +
				   req[0].length - 1) & PAGE_MASK)) &&
                (((unsigned long) zfcp_sg_to_address(&resp[0]) &
		  PAGE_MASK) ==
                 ((unsigned long) (zfcp_sg_to_address(&resp[0]) +
				   resp[0].length - 1) & PAGE_MASK)));
}

/**
 * zfcp_fsf_send_ct - initiate a Generic Service request (FC-GS)
 * @ct: pointer to struct zfcp_send_ct which conatins all needed data for
 *	the request
 * @pool: pointer to memory pool, if non-null this pool is used to allocate
 *	a struct zfcp_fsf_req
 * @erp_action: pointer to erp_action, if non-null the Generic Service request
 *	is sent within error recovery
 */
int
zfcp_fsf_send_ct(struct zfcp_send_ct *ct, mempool_t *pool,
		 struct zfcp_erp_action *erp_action)
{
	volatile struct qdio_buffer_element *sbale;
	struct zfcp_port *port;
	struct zfcp_adapter *adapter;
        struct zfcp_fsf_req *fsf_req;
        unsigned long lock_flags;
        int bytes;
	int ret = 0;

	port = ct->port;
	adapter = port->adapter;

	ret = zfcp_fsf_req_create(adapter, FSF_QTCB_SEND_GENERIC,
				  ZFCP_WAIT_FOR_SBAL | ZFCP_REQ_AUTO_CLEANUP,
				  pool, &lock_flags, &fsf_req);
	if (ret < 0) {
                ZFCP_LOG_INFO("error: Could not create CT request (FC-GS) for "
			      "adapter: %s\n",
			      zfcp_get_busid_by_adapter(adapter));
		goto failed_req;
	}

S
Swen Schillig 已提交
1297
	sbale = zfcp_qdio_sbale_req(fsf_req);
L
Linus Torvalds 已提交
1298 1299 1300 1301 1302 1303 1304 1305 1306 1307
        if (zfcp_use_one_sbal(ct->req, ct->req_count,
                              ct->resp, ct->resp_count)){
                /* both request buffer and response buffer
                   fit into one sbale each */
                sbale[0].flags |= SBAL_FLAGS0_TYPE_WRITE_READ;
                sbale[2].addr = zfcp_sg_to_address(&ct->req[0]);
                sbale[2].length = ct->req[0].length;
                sbale[3].addr = zfcp_sg_to_address(&ct->resp[0]);
                sbale[3].length = ct->resp[0].length;
                sbale[3].flags |= SBAL_FLAGS_LAST_ENTRY;
1308
	} else if (adapter->adapter_features &
L
Linus Torvalds 已提交
1309 1310 1311 1312
                   FSF_FEATURE_ELS_CT_CHAINED_SBALS) {
                /* try to use chained SBALs */
                bytes = zfcp_qdio_sbals_from_sg(fsf_req,
                                                SBAL_FLAGS0_TYPE_WRITE_READ,
S
Swen Schillig 已提交
1313
						ct->req,
L
Linus Torvalds 已提交
1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329
                                                ZFCP_MAX_SBALS_PER_CT_REQ);
                if (bytes <= 0) {
                        ZFCP_LOG_INFO("error: creation of CT request failed "
				      "on adapter %s\n",
				      zfcp_get_busid_by_adapter(adapter));
                        if (bytes == 0)
                                ret = -ENOMEM;
                        else
                                ret = bytes;

                        goto failed_send;
                }
                fsf_req->qtcb->bottom.support.req_buf_length = bytes;
                fsf_req->sbale_curr = ZFCP_LAST_SBALE_PER_SBAL;
                bytes = zfcp_qdio_sbals_from_sg(fsf_req,
                                                SBAL_FLAGS0_TYPE_WRITE_READ,
S
Swen Schillig 已提交
1330
						ct->resp,
L
Linus Torvalds 已提交
1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355
                                                ZFCP_MAX_SBALS_PER_CT_REQ);
                if (bytes <= 0) {
                        ZFCP_LOG_INFO("error: creation of CT request failed "
				      "on adapter %s\n",
				      zfcp_get_busid_by_adapter(adapter));
                        if (bytes == 0)
                                ret = -ENOMEM;
                        else
                                ret = bytes;

                        goto failed_send;
                }
                fsf_req->qtcb->bottom.support.resp_buf_length = bytes;
        } else {
                /* reject send generic request */
		ZFCP_LOG_INFO(
			"error: microcode does not support chained SBALs,"
                        "CT request too big (adapter %s)\n",
			zfcp_get_busid_by_adapter(adapter));
                ret = -EOPNOTSUPP;
                goto failed_send;
        }

	/* settings in QTCB */
	fsf_req->qtcb->header.port_handle = port->handle;
1356 1357
	fsf_req->qtcb->bottom.support.service_class =
		ZFCP_FC_SERVICE_CLASS_DEFAULT;
L
Linus Torvalds 已提交
1358
	fsf_req->qtcb->bottom.support.timeout = ct->timeout;
1359
        fsf_req->data = (unsigned long) ct;
L
Linus Torvalds 已提交
1360

1361 1362
	zfcp_san_dbf_event_ct_request(fsf_req);

1363 1364 1365 1366 1367 1368 1369 1370
	if (erp_action) {
		erp_action->fsf_req = fsf_req;
		fsf_req->erp_action = erp_action;
		zfcp_erp_start_timer(fsf_req);
	} else
		zfcp_fsf_start_timer(fsf_req, ZFCP_FSF_REQUEST_TIMEOUT);

	ret = zfcp_fsf_req_send(fsf_req);
L
Linus Torvalds 已提交
1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388
	if (ret) {
		ZFCP_LOG_DEBUG("error: initiation of CT request failed "
			       "(adapter %s, port 0x%016Lx)\n",
			       zfcp_get_busid_by_adapter(adapter), port->wwpn);
		goto failed_send;
	}

	ZFCP_LOG_DEBUG("CT request initiated (adapter %s, port 0x%016Lx)\n",
		       zfcp_get_busid_by_adapter(adapter), port->wwpn);
	goto out;

 failed_send:
	zfcp_fsf_req_free(fsf_req);
        if (erp_action != NULL) {
                erp_action->fsf_req = NULL;
        }
 failed_req:
 out:
S
Swen Schillig 已提交
1389
	write_unlock_irqrestore(&adapter->req_q.lock, lock_flags);
L
Linus Torvalds 已提交
1390 1391 1392 1393 1394 1395 1396
	return ret;
}

/**
 * zfcp_fsf_send_ct_handler - handler for Generic Service requests
 * @fsf_req: pointer to struct zfcp_fsf_req
 *
1397 1398 1399 1400
 * Data specific for the Generic Service request is passed using
 * fsf_req->data. There we find the pointer to struct zfcp_send_ct.
 * Usually a specific handler for the CT request is called which is
 * found in this structure.
L
Linus Torvalds 已提交
1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413
 */
static int
zfcp_fsf_send_ct_handler(struct zfcp_fsf_req *fsf_req)
{
	struct zfcp_port *port;
	struct zfcp_adapter *adapter;
	struct zfcp_send_ct *send_ct;
	struct fsf_qtcb_header *header;
	struct fsf_qtcb_bottom_support *bottom;
	int retval = -EINVAL;
	u16 subtable, rule, counter;

	adapter = fsf_req->adapter;
1414
	send_ct = (struct zfcp_send_ct *) fsf_req->data;
L
Linus Torvalds 已提交
1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425
	port = send_ct->port;
	header = &fsf_req->qtcb->header;
	bottom = &fsf_req->qtcb->bottom.support;

	if (fsf_req->status & ZFCP_STATUS_FSFREQ_ERROR)
		goto skip_fsfstatus;

	/* evaluate FSF status in QTCB */
	switch (header->fsf_status) {

        case FSF_GOOD:
1426
		zfcp_san_dbf_event_ct_response(fsf_req);
L
Linus Torvalds 已提交
1427 1428 1429 1430
                retval = 0;
		break;

        case FSF_SERVICE_CLASS_NOT_SUPPORTED:
1431 1432 1433 1434
		ZFCP_LOG_INFO("error: adapter %s does not support fc "
			      "class %d.\n",
			      zfcp_get_busid_by_port(port),
			      ZFCP_FC_SERVICE_CLASS_DEFAULT);
L
Linus Torvalds 已提交
1435
		/* stop operation for this adapter */
1436
		zfcp_erp_adapter_shutdown(adapter, 0, 123, fsf_req);
L
Linus Torvalds 已提交
1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460
		fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
		break;

        case FSF_ADAPTER_STATUS_AVAILABLE:
                switch (header->fsf_status_qual.word[0]){
                case FSF_SQ_INVOKE_LINK_TEST_PROCEDURE:
			/* reopening link to port */
			zfcp_test_link(port);
			fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
			break;
                case FSF_SQ_ULP_DEPENDENT_ERP_REQUIRED:
			/* ERP strategy will escalate */
			fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
			break;
                default:
			ZFCP_LOG_INFO("bug: Wrong status qualifier 0x%x "
				      "arrived.\n",
				      header->fsf_status_qual.word[0]);
			break;
                }
                break;

	case FSF_ACCESS_DENIED:
		ZFCP_LOG_NORMAL("access denied, cannot send generic service "
1461
				"command (adapter %s, port d_id=0x%06x)\n",
L
Linus Torvalds 已提交
1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475
				zfcp_get_busid_by_port(port), port->d_id);
		for (counter = 0; counter < 2; counter++) {
			subtable = header->fsf_status_qual.halfword[counter * 2];
			rule = header->fsf_status_qual.halfword[counter * 2 + 1];
			switch (subtable) {
			case FSF_SQ_CFDC_SUBTABLE_OS:
			case FSF_SQ_CFDC_SUBTABLE_PORT_WWPN:
			case FSF_SQ_CFDC_SUBTABLE_PORT_DID:
			case FSF_SQ_CFDC_SUBTABLE_LUN:
       				ZFCP_LOG_INFO("Access denied (%s rule %d)\n",
					zfcp_act_subtable_type[subtable], rule);
				break;
			}
		}
1476
		zfcp_erp_port_access_denied(port, 55, fsf_req);
L
Linus Torvalds 已提交
1477 1478 1479 1480 1481
		fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
		break;

        case FSF_GENERIC_COMMAND_REJECTED:
		ZFCP_LOG_INFO("generic service command rejected "
1482
			      "(adapter %s, port d_id=0x%06x)\n",
L
Linus Torvalds 已提交
1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499
			      zfcp_get_busid_by_port(port), port->d_id);
		ZFCP_LOG_INFO("status qualifier:\n");
		ZFCP_HEX_DUMP(ZFCP_LOG_LEVEL_INFO,
			      (char *) &header->fsf_status_qual,
			      sizeof (union fsf_status_qual));
		fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
		break;

        case FSF_PORT_HANDLE_NOT_VALID:
		ZFCP_LOG_DEBUG("Temporary port identifier 0x%x for port "
			       "0x%016Lx on adapter %s invalid. This may "
			       "happen occasionally.\n", port->handle,
			       port->wwpn, zfcp_get_busid_by_port(port));
		ZFCP_LOG_INFO("status qualifier:\n");
		ZFCP_HEX_DUMP(ZFCP_LOG_LEVEL_INFO,
			      (char *) &header->fsf_status_qual,
			      sizeof (union fsf_status_qual));
1500
		zfcp_erp_adapter_reopen(adapter, 0, 106, fsf_req);
L
Linus Torvalds 已提交
1501 1502 1503 1504 1505
		fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
		break;

        case FSF_PORT_BOXED:
		ZFCP_LOG_INFO("port needs to be reopened "
1506
			      "(adapter %s, port d_id=0x%06x)\n",
L
Linus Torvalds 已提交
1507
			      zfcp_get_busid_by_port(port), port->d_id);
1508
		zfcp_erp_port_boxed(port, 49, fsf_req);
L
Linus Torvalds 已提交
1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568
		fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR
		    | ZFCP_STATUS_FSFREQ_RETRY;
		break;

	/* following states should never occure, all cases avoided
	   in zfcp_fsf_send_ct - but who knows ... */
	case FSF_PAYLOAD_SIZE_MISMATCH:
		ZFCP_LOG_INFO("payload size mismatch (adapter: %s, "
			      "req_buf_length=%d, resp_buf_length=%d)\n",
			      zfcp_get_busid_by_adapter(adapter),
			      bottom->req_buf_length, bottom->resp_buf_length);
		fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
		break;
	case FSF_REQUEST_SIZE_TOO_LARGE:
		ZFCP_LOG_INFO("request size too large (adapter: %s, "
			      "req_buf_length=%d)\n",
			      zfcp_get_busid_by_adapter(adapter),
			      bottom->req_buf_length);
		fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
		break;
	case FSF_RESPONSE_SIZE_TOO_LARGE:
		ZFCP_LOG_INFO("response size too large (adapter: %s, "
			      "resp_buf_length=%d)\n",
			      zfcp_get_busid_by_adapter(adapter),
			      bottom->resp_buf_length);
		fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
		break;
	case FSF_SBAL_MISMATCH:
		ZFCP_LOG_INFO("SBAL mismatch (adapter: %s, req_buf_length=%d, "
			      "resp_buf_length=%d)\n",
			      zfcp_get_busid_by_adapter(adapter),
			      bottom->req_buf_length, bottom->resp_buf_length);
		fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
		break;

       default:
		ZFCP_LOG_NORMAL("bug: An unknown FSF Status was presented "
				"(debug info 0x%x)\n", header->fsf_status);
		break;
	}

skip_fsfstatus:
	send_ct->status = retval;

	if (send_ct->handler != NULL)
		send_ct->handler(send_ct->handler_data);

	return retval;
}

/**
 * zfcp_fsf_send_els - initiate an ELS command (FC-FS)
 * @els: pointer to struct zfcp_send_els which contains all needed data for
 *	the command.
 */
int
zfcp_fsf_send_els(struct zfcp_send_els *els)
{
	volatile struct qdio_buffer_element *sbale;
	struct zfcp_fsf_req *fsf_req;
1569
	u32 d_id;
L
Linus Torvalds 已提交
1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582
	struct zfcp_adapter *adapter;
	unsigned long lock_flags;
        int bytes;
	int ret = 0;

	d_id = els->d_id;
	adapter = els->adapter;

        ret = zfcp_fsf_req_create(adapter, FSF_QTCB_SEND_ELS,
				  ZFCP_REQ_AUTO_CLEANUP,
				  NULL, &lock_flags, &fsf_req);
	if (ret < 0) {
                ZFCP_LOG_INFO("error: creation of ELS request failed "
1583
			      "(adapter %s, port d_id: 0x%06x)\n",
L
Linus Torvalds 已提交
1584 1585 1586 1587
                              zfcp_get_busid_by_adapter(adapter), d_id);
                goto failed_req;
	}

1588 1589 1590 1591 1592 1593
	if (unlikely(!atomic_test_mask(ZFCP_STATUS_COMMON_UNBLOCKED,
			&els->port->status))) {
		ret = -EBUSY;
		goto port_blocked;
	}

S
Swen Schillig 已提交
1594
	sbale = zfcp_qdio_sbale_req(fsf_req);
L
Linus Torvalds 已提交
1595 1596 1597 1598 1599 1600 1601 1602 1603 1604
        if (zfcp_use_one_sbal(els->req, els->req_count,
                              els->resp, els->resp_count)){
                /* both request buffer and response buffer
                   fit into one sbale each */
                sbale[0].flags |= SBAL_FLAGS0_TYPE_WRITE_READ;
                sbale[2].addr = zfcp_sg_to_address(&els->req[0]);
                sbale[2].length = els->req[0].length;
                sbale[3].addr = zfcp_sg_to_address(&els->resp[0]);
                sbale[3].length = els->resp[0].length;
                sbale[3].flags |= SBAL_FLAGS_LAST_ENTRY;
1605
	} else if (adapter->adapter_features &
L
Linus Torvalds 已提交
1606 1607 1608 1609
                   FSF_FEATURE_ELS_CT_CHAINED_SBALS) {
                /* try to use chained SBALs */
                bytes = zfcp_qdio_sbals_from_sg(fsf_req,
                                                SBAL_FLAGS0_TYPE_WRITE_READ,
S
Swen Schillig 已提交
1610
						els->req,
L
Linus Torvalds 已提交
1611 1612 1613
                                                ZFCP_MAX_SBALS_PER_ELS_REQ);
                if (bytes <= 0) {
                        ZFCP_LOG_INFO("error: creation of ELS request failed "
1614
				      "(adapter %s, port d_id: 0x%06x)\n",
L
Linus Torvalds 已提交
1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626
				      zfcp_get_busid_by_adapter(adapter), d_id);
                        if (bytes == 0) {
                                ret = -ENOMEM;
                        } else {
                                ret = bytes;
                        }
                        goto failed_send;
                }
                fsf_req->qtcb->bottom.support.req_buf_length = bytes;
                fsf_req->sbale_curr = ZFCP_LAST_SBALE_PER_SBAL;
                bytes = zfcp_qdio_sbals_from_sg(fsf_req,
                                                SBAL_FLAGS0_TYPE_WRITE_READ,
S
Swen Schillig 已提交
1627
						els->resp,
L
Linus Torvalds 已提交
1628 1629 1630
                                                ZFCP_MAX_SBALS_PER_ELS_REQ);
                if (bytes <= 0) {
                        ZFCP_LOG_INFO("error: creation of ELS request failed "
1631
				      "(adapter %s, port d_id: 0x%06x)\n",
L
Linus Torvalds 已提交
1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644
				      zfcp_get_busid_by_adapter(adapter), d_id);
                        if (bytes == 0) {
                                ret = -ENOMEM;
                        } else {
                                ret = bytes;
                        }
                        goto failed_send;
                }
                fsf_req->qtcb->bottom.support.resp_buf_length = bytes;
        } else {
                /* reject request */
		ZFCP_LOG_INFO("error: microcode does not support chained SBALs"
                              ", ELS request too big (adapter %s, "
1645
			      "port d_id: 0x%06x)\n",
L
Linus Torvalds 已提交
1646 1647 1648 1649 1650 1651 1652
			      zfcp_get_busid_by_adapter(adapter), d_id);
                ret = -EOPNOTSUPP;
                goto failed_send;
        }

	/* settings in QTCB */
	fsf_req->qtcb->bottom.support.d_id = d_id;
1653 1654
	fsf_req->qtcb->bottom.support.service_class =
		ZFCP_FC_SERVICE_CLASS_DEFAULT;
L
Linus Torvalds 已提交
1655
	fsf_req->qtcb->bottom.support.timeout = ZFCP_ELS_TIMEOUT;
1656
	fsf_req->data = (unsigned long) els;
L
Linus Torvalds 已提交
1657

S
Swen Schillig 已提交
1658
	sbale = zfcp_qdio_sbale_req(fsf_req);
L
Linus Torvalds 已提交
1659

1660 1661
	zfcp_san_dbf_event_els_request(fsf_req);

1662 1663
	zfcp_fsf_start_timer(fsf_req, ZFCP_FSF_REQUEST_TIMEOUT);
	ret = zfcp_fsf_req_send(fsf_req);
L
Linus Torvalds 已提交
1664 1665
	if (ret) {
		ZFCP_LOG_DEBUG("error: initiation of ELS request failed "
1666
			       "(adapter %s, port d_id: 0x%06x)\n",
L
Linus Torvalds 已提交
1667 1668 1669 1670 1671
			       zfcp_get_busid_by_adapter(adapter), d_id);
		goto failed_send;
	}

	ZFCP_LOG_DEBUG("ELS request initiated (adapter %s, port d_id: "
1672
		       "0x%06x)\n", zfcp_get_busid_by_adapter(adapter), d_id);
L
Linus Torvalds 已提交
1673 1674
	goto out;

1675
 port_blocked:
L
Linus Torvalds 已提交
1676 1677 1678 1679 1680
 failed_send:
	zfcp_fsf_req_free(fsf_req);

 failed_req:
 out:
S
Swen Schillig 已提交
1681
	write_unlock_irqrestore(&adapter->req_q.lock, lock_flags);
L
Linus Torvalds 已提交
1682 1683 1684 1685 1686 1687 1688 1689

        return ret;
}

/**
 * zfcp_fsf_send_els_handler - handler for ELS commands
 * @fsf_req: pointer to struct zfcp_fsf_req
 *
1690 1691 1692 1693
 * Data specific for the ELS command is passed using
 * fsf_req->data. There we find the pointer to struct zfcp_send_els.
 * Usually a specific handler for the ELS command is called which is
 * found in this structure.
L
Linus Torvalds 已提交
1694 1695 1696 1697 1698
 */
static int zfcp_fsf_send_els_handler(struct zfcp_fsf_req *fsf_req)
{
	struct zfcp_adapter *adapter;
	struct zfcp_port *port;
1699
	u32 d_id;
L
Linus Torvalds 已提交
1700 1701 1702 1703 1704 1705
	struct fsf_qtcb_header *header;
	struct fsf_qtcb_bottom_support *bottom;
	struct zfcp_send_els *send_els;
	int retval = -EINVAL;
	u16 subtable, rule, counter;

1706
	send_els = (struct zfcp_send_els *) fsf_req->data;
L
Linus Torvalds 已提交
1707
	adapter = send_els->adapter;
1708
	port = send_els->port;
L
Linus Torvalds 已提交
1709 1710 1711 1712 1713 1714 1715 1716 1717 1718
	d_id = send_els->d_id;
	header = &fsf_req->qtcb->header;
	bottom = &fsf_req->qtcb->bottom.support;

	if (fsf_req->status & ZFCP_STATUS_FSFREQ_ERROR)
		goto skip_fsfstatus;

	switch (header->fsf_status) {

	case FSF_GOOD:
1719
		zfcp_san_dbf_event_els_response(fsf_req);
L
Linus Torvalds 已提交
1720 1721 1722 1723
		retval = 0;
		break;

	case FSF_SERVICE_CLASS_NOT_SUPPORTED:
1724 1725 1726 1727
		ZFCP_LOG_INFO("error: adapter %s does not support fc "
			      "class %d.\n",
			      zfcp_get_busid_by_adapter(adapter),
			      ZFCP_FC_SERVICE_CLASS_DEFAULT);
L
Linus Torvalds 已提交
1728
		/* stop operation for this adapter */
1729
		zfcp_erp_adapter_shutdown(adapter, 0, 124, fsf_req);
L
Linus Torvalds 已提交
1730 1731 1732 1733 1734 1735
		fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
		break;

	case FSF_ADAPTER_STATUS_AVAILABLE:
		switch (header->fsf_status_qual.word[0]){
		case FSF_SQ_INVOKE_LINK_TEST_PROCEDURE:
1736 1737
			if (port && (send_els->ls_code != ZFCP_LS_ADISC))
				zfcp_test_link(port);
L
Linus Torvalds 已提交
1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756
			fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
			break;
		case FSF_SQ_ULP_DEPENDENT_ERP_REQUIRED:
			fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
			break;
		case FSF_SQ_RETRY_IF_POSSIBLE:
			fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
			break;
		default:
			ZFCP_LOG_INFO("bug: Wrong status qualifier 0x%x\n",
				      header->fsf_status_qual.word[0]);
			ZFCP_HEX_DUMP(ZFCP_LOG_LEVEL_INFO,
				(char*)header->fsf_status_qual.word, 16);
		}
		break;

	case FSF_ELS_COMMAND_REJECTED:
		ZFCP_LOG_INFO("ELS has been rejected because command filter "
			      "prohibited sending "
1757
			      "(adapter: %s, port d_id: 0x%06x)\n",
L
Linus Torvalds 已提交
1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804
			      zfcp_get_busid_by_adapter(adapter), d_id);

		break;

	case FSF_PAYLOAD_SIZE_MISMATCH:
		ZFCP_LOG_INFO(
			"ELS request size and ELS response size must be either "
			"both 0, or both greater than 0 "
			"(adapter: %s, req_buf_length=%d resp_buf_length=%d)\n",
			zfcp_get_busid_by_adapter(adapter),
			bottom->req_buf_length,
			bottom->resp_buf_length);
		break;

	case FSF_REQUEST_SIZE_TOO_LARGE:
		ZFCP_LOG_INFO(
			"Length of the ELS request buffer, "
			"specified in QTCB bottom, "
			"exceeds the size of the buffers "
			"that have been allocated for ELS request data "
			"(adapter: %s, req_buf_length=%d)\n",
			zfcp_get_busid_by_adapter(adapter),
			bottom->req_buf_length);
		break;

	case FSF_RESPONSE_SIZE_TOO_LARGE:
		ZFCP_LOG_INFO(
			"Length of the ELS response buffer, "
			"specified in QTCB bottom, "
			"exceeds the size of the buffers "
			"that have been allocated for ELS response data "
			"(adapter: %s, resp_buf_length=%d)\n",
			zfcp_get_busid_by_adapter(adapter),
			bottom->resp_buf_length);
		break;

	case FSF_SBAL_MISMATCH:
		/* should never occure, avoided in zfcp_fsf_send_els */
		ZFCP_LOG_INFO("SBAL mismatch (adapter: %s, req_buf_length=%d, "
			      "resp_buf_length=%d)\n",
			      zfcp_get_busid_by_adapter(adapter),
			      bottom->req_buf_length, bottom->resp_buf_length);
		fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
		break;

	case FSF_ACCESS_DENIED:
		ZFCP_LOG_NORMAL("access denied, cannot send ELS command "
1805
				"(adapter %s, port d_id=0x%06x)\n",
L
Linus Torvalds 已提交
1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820
				zfcp_get_busid_by_adapter(adapter), d_id);
		for (counter = 0; counter < 2; counter++) {
			subtable = header->fsf_status_qual.halfword[counter * 2];
			rule = header->fsf_status_qual.halfword[counter * 2 + 1];
			switch (subtable) {
			case FSF_SQ_CFDC_SUBTABLE_OS:
			case FSF_SQ_CFDC_SUBTABLE_PORT_WWPN:
			case FSF_SQ_CFDC_SUBTABLE_PORT_DID:
			case FSF_SQ_CFDC_SUBTABLE_LUN:
				ZFCP_LOG_INFO("Access denied (%s rule %d)\n",
					zfcp_act_subtable_type[subtable], rule);
				break;
			}
		}
		if (port != NULL)
1821
			zfcp_erp_port_access_denied(port, 56, fsf_req);
L
Linus Torvalds 已提交
1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837
		fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
		break;

	default:
		ZFCP_LOG_NORMAL(
			"bug: An unknown FSF Status was presented "
			"(adapter: %s, fsf_status=0x%08x)\n",
			zfcp_get_busid_by_adapter(adapter),
			header->fsf_status);
		fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
		break;
	}

skip_fsfstatus:
	send_els->status = retval;

H
Heiko Carstens 已提交
1838
	if (send_els->handler)
L
Linus Torvalds 已提交
1839 1840 1841 1842 1843 1844 1845 1846 1847
		send_els->handler(send_els->handler_data);

	return retval;
}

int
zfcp_fsf_exchange_config_data(struct zfcp_erp_action *erp_action)
{
	volatile struct qdio_buffer_element *sbale;
1848
	struct zfcp_fsf_req *fsf_req;
1849
	struct zfcp_adapter *adapter = erp_action->adapter;
L
Linus Torvalds 已提交
1850
	unsigned long lock_flags;
1851
	int retval;
L
Linus Torvalds 已提交
1852 1853

	/* setup new FSF request */
1854
	retval = zfcp_fsf_req_create(adapter,
L
Linus Torvalds 已提交
1855 1856
				     FSF_QTCB_EXCHANGE_CONFIG_DATA,
				     ZFCP_REQ_AUTO_CLEANUP,
1857
				     adapter->pool.fsf_req_erp,
1858
				     &lock_flags, &fsf_req);
1859
	if (retval) {
L
Linus Torvalds 已提交
1860 1861
		ZFCP_LOG_INFO("error: Could not create exchange configuration "
			      "data request for adapter %s.\n",
1862
			      zfcp_get_busid_by_adapter(adapter));
S
Swen Schillig 已提交
1863
		write_unlock_irqrestore(&adapter->req_q.lock, lock_flags);
1864
		return retval;
L
Linus Torvalds 已提交
1865 1866
	}

S
Swen Schillig 已提交
1867
	sbale = zfcp_qdio_sbale_req(fsf_req);
1868 1869
	sbale[0].flags |= SBAL_FLAGS0_TYPE_READ;
	sbale[1].flags |= SBAL_FLAGS_LAST_ENTRY;
L
Linus Torvalds 已提交
1870

1871
	fsf_req->qtcb->bottom.config.feature_selection =
1872 1873
			FSF_FEATURE_CFDC |
			FSF_FEATURE_LUN_SHARING |
1874
			FSF_FEATURE_NOTIFICATION_LOST |
1875
			FSF_FEATURE_UPDATE_ALERT;
1876 1877
	fsf_req->erp_action = erp_action;
	erp_action->fsf_req = fsf_req;
L
Linus Torvalds 已提交
1878

1879 1880
	zfcp_erp_start_timer(fsf_req);
	retval = zfcp_fsf_req_send(fsf_req);
S
Swen Schillig 已提交
1881
	write_unlock_irqrestore(&adapter->req_q.lock, lock_flags);
L
Linus Torvalds 已提交
1882
	if (retval) {
1883 1884 1885
		ZFCP_LOG_INFO("error: Could not send exchange configuration "
			      "data command on the adapter %s\n",
			      zfcp_get_busid_by_adapter(adapter));
1886
		zfcp_fsf_req_free(fsf_req);
L
Linus Torvalds 已提交
1887 1888
		erp_action->fsf_req = NULL;
	}
1889 1890 1891 1892
	else
		ZFCP_LOG_DEBUG("exchange configuration data request initiated "
			       "(adapter %s)\n",
			       zfcp_get_busid_by_adapter(adapter));
L
Linus Torvalds 已提交
1893

1894 1895
	return retval;
}
L
Linus Torvalds 已提交
1896

1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907
int
zfcp_fsf_exchange_config_data_sync(struct zfcp_adapter *adapter,
				struct fsf_qtcb_bottom_config *data)
{
	volatile struct qdio_buffer_element *sbale;
	struct zfcp_fsf_req *fsf_req;
	unsigned long lock_flags;
	int retval;

	/* setup new FSF request */
	retval = zfcp_fsf_req_create(adapter, FSF_QTCB_EXCHANGE_CONFIG_DATA,
1908 1909
				     ZFCP_WAIT_FOR_SBAL, NULL, &lock_flags,
				     &fsf_req);
1910 1911 1912 1913
	if (retval) {
		ZFCP_LOG_INFO("error: Could not create exchange configuration "
			      "data request for adapter %s.\n",
			      zfcp_get_busid_by_adapter(adapter));
S
Swen Schillig 已提交
1914
		write_unlock_irqrestore(&adapter->req_q.lock, lock_flags);
1915 1916 1917
		return retval;
	}

S
Swen Schillig 已提交
1918
	sbale = zfcp_qdio_sbale_req(fsf_req);
1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932
	sbale[0].flags |= SBAL_FLAGS0_TYPE_READ;
	sbale[1].flags |= SBAL_FLAGS_LAST_ENTRY;

	fsf_req->qtcb->bottom.config.feature_selection =
			FSF_FEATURE_CFDC |
			FSF_FEATURE_LUN_SHARING |
			FSF_FEATURE_NOTIFICATION_LOST |
			FSF_FEATURE_UPDATE_ALERT;

	if (data)
		fsf_req->data = (unsigned long) data;

	zfcp_fsf_start_timer(fsf_req, ZFCP_FSF_REQUEST_TIMEOUT);
	retval = zfcp_fsf_req_send(fsf_req);
S
Swen Schillig 已提交
1933
	write_unlock_irqrestore(&adapter->req_q.lock, lock_flags);
1934 1935 1936 1937 1938 1939 1940 1941 1942 1943
	if (retval)
		ZFCP_LOG_INFO("error: Could not send exchange configuration "
			      "data command on the adapter %s\n",
			      zfcp_get_busid_by_adapter(adapter));
	else
		wait_event(fsf_req->completion_wq,
			   fsf_req->status & ZFCP_STATUS_FSFREQ_COMPLETED);

	zfcp_fsf_req_free(fsf_req);

L
Linus Torvalds 已提交
1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958
	return retval;
}

/**
 * zfcp_fsf_exchange_config_evaluate
 * @fsf_req: fsf_req which belongs to xchg config data request
 * @xchg_ok: specifies if xchg config data was incomplete or complete (0/1)
 *
 * returns: -EIO on error, 0 otherwise
 */
static int
zfcp_fsf_exchange_config_evaluate(struct zfcp_fsf_req *fsf_req, int xchg_ok)
{
	struct fsf_qtcb_bottom_config *bottom;
	struct zfcp_adapter *adapter = fsf_req->adapter;
1959
	struct Scsi_Host *shost = adapter->scsi_host;
L
Linus Torvalds 已提交
1960 1961 1962 1963 1964

	bottom = &fsf_req->qtcb->bottom.config;
	ZFCP_LOG_DEBUG("low/high QTCB version 0x%x/0x%x of FSF\n",
		       bottom->low_qtcb_version, bottom->high_qtcb_version);
	adapter->fsf_lic_version = bottom->lic_version;
1965 1966
	adapter->adapter_features = bottom->adapter_features;
	adapter->connection_features = bottom->connection_features;
已提交
1967 1968 1969
	adapter->peer_wwpn = 0;
	adapter->peer_wwnn = 0;
	adapter->peer_d_id = 0;
L
Linus Torvalds 已提交
1970 1971

	if (xchg_ok) {
1972 1973 1974 1975 1976

		if (fsf_req->data)
			memcpy((struct fsf_qtcb_bottom_config *) fsf_req->data,
				bottom, sizeof (struct fsf_qtcb_bottom_config));

1977 1978 1979 1980
		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;
1981 1982
		fc_host_supported_classes(shost) =
				FC_COS_CLASS2 | FC_COS_CLASS3;
L
Linus Torvalds 已提交
1983
		adapter->hydra_version = bottom->adapter_type;
1984
		adapter->timer_ticks = bottom->timer_interval;
1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998
		if (fc_host_permanent_port_name(shost) == -1)
			fc_host_permanent_port_name(shost) =
				fc_host_port_name(shost);
		if (bottom->fc_topology == 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;
		} else if (bottom->fc_topology == FSF_TOPO_FABRIC)
			fc_host_port_type(shost) = FC_PORTTYPE_NPORT;
		else if (bottom->fc_topology == FSF_TOPO_AL)
			fc_host_port_type(shost) = FC_PORTTYPE_NLPORT;
		else
			fc_host_port_type(shost) = FC_PORTTYPE_UNKNOWN;
L
Linus Torvalds 已提交
1999
	} else {
2000 2001 2002 2003
		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;
2004
		fc_host_port_type(shost) = FC_PORTTYPE_UNKNOWN;
L
Linus Torvalds 已提交
2005 2006 2007
		adapter->hydra_version = 0;
	}

2008
	if (adapter->adapter_features & FSF_FEATURE_HBAAPI_MANAGEMENT) {
L
Linus Torvalds 已提交
2009
		adapter->hardware_version = bottom->hardware_version;
2010 2011 2012 2013
		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 已提交
2014 2015
	}

2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030
	if (fsf_req->erp_action)
		ZFCP_LOG_NORMAL("The adapter %s reported the following "
				"characteristics:\n"
				"WWNN 0x%016Lx, WWPN 0x%016Lx, "
				"S_ID 0x%06x,\n"
				"adapter version 0x%x, "
				"LIC version 0x%x, "
				"FC link speed %d Gb/s\n",
				zfcp_get_busid_by_adapter(adapter),
				(wwn_t) fc_host_node_name(shost),
				(wwn_t) fc_host_port_name(shost),
				fc_host_port_id(shost),
				adapter->hydra_version,
				adapter->fsf_lic_version,
				fc_host_speed(shost));
L
Linus Torvalds 已提交
2031 2032 2033 2034 2035 2036
	if (ZFCP_QTCB_VERSION < bottom->low_qtcb_version) {
		ZFCP_LOG_NORMAL("error: the adapter %s "
				"only supports newer control block "
				"versions in comparison to this device "
				"driver (try updated device driver)\n",
				zfcp_get_busid_by_adapter(adapter));
2037
		zfcp_erp_adapter_shutdown(adapter, 0, 125, fsf_req);
L
Linus Torvalds 已提交
2038 2039 2040 2041 2042 2043 2044 2045
		return -EIO;
	}
	if (ZFCP_QTCB_VERSION > bottom->high_qtcb_version) {
		ZFCP_LOG_NORMAL("error: the adapter %s "
				"only supports older control block "
				"versions than this device driver uses"
				"(consider a microcode upgrade)\n",
				zfcp_get_busid_by_adapter(adapter));
2046
		zfcp_erp_adapter_shutdown(adapter, 0, 126, fsf_req);
L
Linus Torvalds 已提交
2047 2048 2049 2050 2051
		return -EIO;
	}
	return 0;
}

2052
/**
L
Linus Torvalds 已提交
2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063
 * function:    zfcp_fsf_exchange_config_data_handler
 *
 * purpose:     is called for finished Exchange Configuration Data command
 *
 * returns:
 */
static int
zfcp_fsf_exchange_config_data_handler(struct zfcp_fsf_req *fsf_req)
{
	struct fsf_qtcb_bottom_config *bottom;
	struct zfcp_adapter *adapter = fsf_req->adapter;
2064
	struct fsf_qtcb *qtcb = fsf_req->qtcb;
L
Linus Torvalds 已提交
2065 2066 2067 2068

	if (fsf_req->status & ZFCP_STATUS_FSFREQ_ERROR)
		return -EIO;

2069
	switch (qtcb->header.fsf_status) {
L
Linus Torvalds 已提交
2070 2071 2072 2073 2074

	case FSF_GOOD:
		if (zfcp_fsf_exchange_config_evaluate(fsf_req, 1))
			return -EIO;

2075 2076
		switch (fc_host_port_type(adapter->scsi_host)) {
		case FC_PORTTYPE_PTP:
已提交
2077 2078 2079 2080 2081 2082 2083 2084 2085 2086
			ZFCP_LOG_NORMAL("Point-to-Point fibrechannel "
					"configuration detected at adapter %s\n"
					"Peer WWNN 0x%016llx, "
					"peer WWPN 0x%016llx, "
					"peer d_id 0x%06x\n",
					zfcp_get_busid_by_adapter(adapter),
					adapter->peer_wwnn,
					adapter->peer_wwpn,
					adapter->peer_d_id);
			break;
2087
		case FC_PORTTYPE_NLPORT:
L
Linus Torvalds 已提交
2088 2089 2090 2091
			ZFCP_LOG_NORMAL("error: Arbitrated loop fibrechannel "
					"topology detected at adapter %s "
					"unsupported, shutting down adapter\n",
					zfcp_get_busid_by_adapter(adapter));
2092
			zfcp_erp_adapter_shutdown(adapter, 0, 127, fsf_req);
L
Linus Torvalds 已提交
2093
			return -EIO;
2094
		case FC_PORTTYPE_NPORT:
2095 2096 2097 2098
			if (fsf_req->erp_action)
				ZFCP_LOG_NORMAL("Switched fabric fibrechannel "
						"network detected at adapter "
						"%s.\n",
2099
					zfcp_get_busid_by_adapter(adapter));
L
Linus Torvalds 已提交
2100 2101 2102 2103 2104 2105 2106 2107 2108
			break;
		default:
			ZFCP_LOG_NORMAL("bug: The fibrechannel topology "
					"reported by the exchange "
					"configuration command for "
					"the adapter %s is not "
					"of a type known to the zfcp "
					"driver, shutting down adapter\n",
					zfcp_get_busid_by_adapter(adapter));
2109
			zfcp_erp_adapter_shutdown(adapter, 0, 128, fsf_req);
L
Linus Torvalds 已提交
2110 2111
			return -EIO;
		}
2112
		bottom = &qtcb->bottom.config;
L
Linus Torvalds 已提交
2113 2114 2115 2116 2117 2118 2119 2120
		if (bottom->max_qtcb_size < sizeof(struct fsf_qtcb)) {
			ZFCP_LOG_NORMAL("bug: Maximum QTCB size (%d bytes) "
					"allowed by the adapter %s "
					"is lower than the minimum "
					"required by the driver (%ld bytes).\n",
					bottom->max_qtcb_size,
					zfcp_get_busid_by_adapter(adapter),
					sizeof(struct fsf_qtcb));
2121
			zfcp_erp_adapter_shutdown(adapter, 0, 129, fsf_req);
L
Linus Torvalds 已提交
2122 2123 2124 2125 2126 2127 2128 2129 2130
			return -EIO;
		}
		atomic_set_mask(ZFCP_STATUS_ADAPTER_XCONFIG_OK,
				&adapter->status);
		break;
	case FSF_EXCHANGE_CONFIG_DATA_INCOMPLETE:
		if (zfcp_fsf_exchange_config_evaluate(fsf_req, 0))
			return -EIO;

2131 2132
		atomic_set_mask(ZFCP_STATUS_ADAPTER_XCONFIG_OK,
				&adapter->status);
2133

2134
		zfcp_fsf_link_down_info_eval(fsf_req, 42,
2135
			&qtcb->header.fsf_status_qual.link_down_info);
L
Linus Torvalds 已提交
2136 2137
		break;
	default:
2138
		zfcp_erp_adapter_shutdown(adapter, 0, 130, fsf_req);
L
Linus Torvalds 已提交
2139 2140 2141 2142 2143 2144 2145
		return -EIO;
	}
	return 0;
}

/**
 * zfcp_fsf_exchange_port_data - request information about local port
2146
 * @erp_action: ERP action for the adapter for which port data is requested
L
Linus Torvalds 已提交
2147 2148
 */
int
2149
zfcp_fsf_exchange_port_data(struct zfcp_erp_action *erp_action)
L
Linus Torvalds 已提交
2150 2151
{
	volatile struct qdio_buffer_element *sbale;
2152 2153
	struct zfcp_fsf_req *fsf_req;
	struct zfcp_adapter *adapter = erp_action->adapter;
2154
	unsigned long lock_flags;
2155
	int retval;
L
Linus Torvalds 已提交
2156

2157
	if (!(adapter->adapter_features & FSF_FEATURE_HBAAPI_MANAGEMENT)) {
L
Linus Torvalds 已提交
2158
		ZFCP_LOG_INFO("error: exchange port data "
2159
			      "command not supported by adapter %s\n",
L
Linus Torvalds 已提交
2160
			      zfcp_get_busid_by_adapter(adapter));
2161 2162
		return -EOPNOTSUPP;
	}
L
Linus Torvalds 已提交
2163 2164 2165

	/* setup new FSF request */
	retval = zfcp_fsf_req_create(adapter, FSF_QTCB_EXCHANGE_PORT_DATA,
2166 2167 2168 2169
				     ZFCP_REQ_AUTO_CLEANUP,
				     adapter->pool.fsf_req_erp,
				     &lock_flags, &fsf_req);
	if (retval) {
L
Linus Torvalds 已提交
2170
		ZFCP_LOG_INFO("error: Out of resources. Could not create an "
2171
			      "exchange port data request for "
2172
			      "the adapter %s.\n",
L
Linus Torvalds 已提交
2173
			      zfcp_get_busid_by_adapter(adapter));
S
Swen Schillig 已提交
2174
		write_unlock_irqrestore(&adapter->req_q.lock, lock_flags);
2175
		return retval;
2176 2177
	}

S
Swen Schillig 已提交
2178
	sbale = zfcp_qdio_sbale_req(fsf_req);
2179 2180
	sbale[0].flags |= SBAL_FLAGS0_TYPE_READ;
	sbale[1].flags |= SBAL_FLAGS_LAST_ENTRY;
L
Linus Torvalds 已提交
2181

2182 2183 2184
	erp_action->fsf_req = fsf_req;
	fsf_req->erp_action = erp_action;
	zfcp_erp_start_timer(fsf_req);
L
Linus Torvalds 已提交
2185

2186
	retval = zfcp_fsf_req_send(fsf_req);
S
Swen Schillig 已提交
2187
	write_unlock_irqrestore(&adapter->req_q.lock, lock_flags);
2188

L
Linus Torvalds 已提交
2189 2190
	if (retval) {
		ZFCP_LOG_INFO("error: Could not send an exchange port data "
2191
			      "command on the adapter %s\n",
L
Linus Torvalds 已提交
2192 2193
			      zfcp_get_busid_by_adapter(adapter));
		zfcp_fsf_req_free(fsf_req);
2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228
		erp_action->fsf_req = NULL;
	}
	else
		ZFCP_LOG_DEBUG("exchange port data request initiated "
			       "(adapter %s)\n",
			       zfcp_get_busid_by_adapter(adapter));
	return retval;
}


/**
 * zfcp_fsf_exchange_port_data_sync - request information about local port
 * and wait until information is ready
 */
int
zfcp_fsf_exchange_port_data_sync(struct zfcp_adapter *adapter,
				struct fsf_qtcb_bottom_port *data)
{
	volatile struct qdio_buffer_element *sbale;
	struct zfcp_fsf_req *fsf_req;
	unsigned long lock_flags;
	int retval;

	if (!(adapter->adapter_features & FSF_FEATURE_HBAAPI_MANAGEMENT)) {
		ZFCP_LOG_INFO("error: exchange port data "
			      "command not supported by adapter %s\n",
			      zfcp_get_busid_by_adapter(adapter));
		return -EOPNOTSUPP;
	}

	/* setup new FSF request */
	retval = zfcp_fsf_req_create(adapter, FSF_QTCB_EXCHANGE_PORT_DATA,
				0, NULL, &lock_flags, &fsf_req);
	if (retval) {
		ZFCP_LOG_INFO("error: Out of resources. Could not create an "
2229
			      "exchange port data request for "
2230 2231
			      "the adapter %s.\n",
			      zfcp_get_busid_by_adapter(adapter));
S
Swen Schillig 已提交
2232
		write_unlock_irqrestore(&adapter->req_q.lock, lock_flags);
2233
		return retval;
L
Linus Torvalds 已提交
2234 2235
	}

2236 2237 2238
	if (data)
		fsf_req->data = (unsigned long) data;

S
Swen Schillig 已提交
2239
	sbale = zfcp_qdio_sbale_req(fsf_req);
2240 2241 2242 2243 2244
	sbale[0].flags |= SBAL_FLAGS0_TYPE_READ;
	sbale[1].flags |= SBAL_FLAGS_LAST_ENTRY;

	zfcp_fsf_start_timer(fsf_req, ZFCP_FSF_REQUEST_TIMEOUT);
	retval = zfcp_fsf_req_send(fsf_req);
S
Swen Schillig 已提交
2245
	write_unlock_irqrestore(&adapter->req_q.lock, lock_flags);
L
Linus Torvalds 已提交
2246

2247 2248 2249 2250 2251
	if (retval)
		ZFCP_LOG_INFO("error: Could not send an exchange port data "
			      "command on the adapter %s\n",
			      zfcp_get_busid_by_adapter(adapter));
	else
2252 2253
		wait_event(fsf_req->completion_wq,
			   fsf_req->status & ZFCP_STATUS_FSFREQ_COMPLETED);
2254 2255 2256

	zfcp_fsf_req_free(fsf_req);

L
Linus Torvalds 已提交
2257 2258 2259
	return retval;
}

2260 2261 2262 2263 2264 2265 2266 2267 2268
/**
 * zfcp_fsf_exchange_port_evaluate
 * @fsf_req: fsf_req which belongs to xchg port data request
 * @xchg_ok: specifies if xchg port data was incomplete or complete (0/1)
 */
static void
zfcp_fsf_exchange_port_evaluate(struct zfcp_fsf_req *fsf_req, int xchg_ok)
{
	struct zfcp_adapter *adapter;
2269
	struct fsf_qtcb_bottom_port *bottom;
2270 2271 2272
	struct Scsi_Host *shost;

	adapter = fsf_req->adapter;
2273
	bottom = &fsf_req->qtcb->bottom.port;
2274 2275
	shost = adapter->scsi_host;

2276 2277 2278
	if (fsf_req->data)
		memcpy((struct fsf_qtcb_bottom_port*) fsf_req->data, bottom,
			sizeof(struct fsf_qtcb_bottom_port));
2279 2280 2281 2282 2283 2284 2285 2286

	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 已提交
2287 2288 2289 2290 2291 2292 2293 2294

/**
 * zfcp_fsf_exchange_port_data_handler - handler for exchange_port_data request
 * @fsf_req: pointer to struct zfcp_fsf_req
 */
static void
zfcp_fsf_exchange_port_data_handler(struct zfcp_fsf_req *fsf_req)
{
2295 2296 2297 2298 2299
	struct zfcp_adapter *adapter;
	struct fsf_qtcb *qtcb;

	adapter = fsf_req->adapter;
	qtcb = fsf_req->qtcb;
L
Linus Torvalds 已提交
2300 2301 2302 2303

	if (fsf_req->status & ZFCP_STATUS_FSFREQ_ERROR)
		return;

2304
	switch (qtcb->header.fsf_status) {
L
Linus Torvalds 已提交
2305
        case FSF_GOOD:
2306
		zfcp_fsf_exchange_port_evaluate(fsf_req, 1);
2307 2308 2309
		atomic_set_mask(ZFCP_STATUS_ADAPTER_XPORT_OK, &adapter->status);
		break;
	case FSF_EXCHANGE_CONFIG_DATA_INCOMPLETE:
2310
		zfcp_fsf_exchange_port_evaluate(fsf_req, 0);
2311
		atomic_set_mask(ZFCP_STATUS_ADAPTER_XPORT_OK, &adapter->status);
2312
		zfcp_fsf_link_down_info_eval(fsf_req, 43,
2313
			&qtcb->header.fsf_status_qual.link_down_info);
L
Linus Torvalds 已提交
2314 2315 2316 2317 2318 2319 2320 2321
                break;
	}
}


/*
 * function:    zfcp_fsf_open_port
 *
S
Swen Schillig 已提交
2322
 * purpose:
L
Linus Torvalds 已提交
2323 2324
 *
 * returns:	address of initiated FSF request
S
Swen Schillig 已提交
2325
 *		NULL - request could not be initiated
L
Linus Torvalds 已提交
2326 2327 2328 2329 2330
 */
int
zfcp_fsf_open_port(struct zfcp_erp_action *erp_action)
{
	volatile struct qdio_buffer_element *sbale;
2331
	struct zfcp_fsf_req *fsf_req;
L
Linus Torvalds 已提交
2332 2333 2334 2335 2336 2337 2338 2339
	unsigned long lock_flags;
	int retval = 0;

	/* setup new FSF request */
	retval = zfcp_fsf_req_create(erp_action->adapter,
				     FSF_QTCB_OPEN_PORT_WITH_DID,
				     ZFCP_WAIT_FOR_SBAL | ZFCP_REQ_AUTO_CLEANUP,
				     erp_action->adapter->pool.fsf_req_erp,
2340
				     &lock_flags, &fsf_req);
L
Linus Torvalds 已提交
2341 2342 2343 2344 2345 2346 2347 2348
	if (retval < 0) {
		ZFCP_LOG_INFO("error: Could not create open port request "
			      "for port 0x%016Lx on adapter %s.\n",
			      erp_action->port->wwpn,
			      zfcp_get_busid_by_adapter(erp_action->adapter));
		goto out;
	}

S
Swen Schillig 已提交
2349
	sbale = zfcp_qdio_sbale_req(fsf_req);
L
Linus Torvalds 已提交
2350 2351 2352
        sbale[0].flags |= SBAL_FLAGS0_TYPE_READ;
        sbale[1].flags |= SBAL_FLAGS_LAST_ENTRY;

2353
	fsf_req->qtcb->bottom.support.d_id = erp_action->port->d_id;
L
Linus Torvalds 已提交
2354
	atomic_set_mask(ZFCP_STATUS_COMMON_OPENING, &erp_action->port->status);
2355 2356 2357
	fsf_req->data = (unsigned long) erp_action->port;
	fsf_req->erp_action = erp_action;
	erp_action->fsf_req = fsf_req;
L
Linus Torvalds 已提交
2358

2359 2360
	zfcp_erp_start_timer(fsf_req);
	retval = zfcp_fsf_req_send(fsf_req);
L
Linus Torvalds 已提交
2361 2362 2363 2364 2365
	if (retval) {
		ZFCP_LOG_INFO("error: Could not send open port request for "
			      "port 0x%016Lx on adapter %s.\n",
			      erp_action->port->wwpn,
			      zfcp_get_busid_by_adapter(erp_action->adapter));
2366
		zfcp_fsf_req_free(fsf_req);
L
Linus Torvalds 已提交
2367 2368 2369 2370 2371 2372 2373 2374 2375
		erp_action->fsf_req = NULL;
		goto out;
	}

	ZFCP_LOG_DEBUG("open port request initiated "
		       "(adapter %s,  port 0x%016Lx)\n",
		       zfcp_get_busid_by_adapter(erp_action->adapter),
		       erp_action->port->wwpn);
 out:
S
Swen Schillig 已提交
2376
	write_unlock_irqrestore(&erp_action->adapter->req_q.lock, lock_flags);
L
Linus Torvalds 已提交
2377 2378 2379 2380 2381 2382 2383 2384
	return retval;
}

/*
 * function:    zfcp_fsf_open_port_handler
 *
 * purpose:	is called for finished Open Port command
 *
S
Swen Schillig 已提交
2385
 * returns:
L
Linus Torvalds 已提交
2386 2387 2388 2389 2390 2391 2392 2393 2394 2395
 */
static int
zfcp_fsf_open_port_handler(struct zfcp_fsf_req *fsf_req)
{
	int retval = -EINVAL;
	struct zfcp_port *port;
	struct fsf_plogi *plogi;
	struct fsf_qtcb_header *header;
	u16 subtable, rule, counter;

2396
	port = (struct zfcp_port *) fsf_req->data;
L
Linus Torvalds 已提交
2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433
	header = &fsf_req->qtcb->header;

	if (fsf_req->status & ZFCP_STATUS_FSFREQ_ERROR) {
		/* don't change port status in our bookkeeping */
		goto skip_fsfstatus;
	}

	/* evaluate FSF status in QTCB */
	switch (header->fsf_status) {

	case FSF_PORT_ALREADY_OPEN:
		ZFCP_LOG_NORMAL("bug: remote port 0x%016Lx on adapter %s "
				"is already open.\n",
				port->wwpn, zfcp_get_busid_by_port(port));
		/*
		 * This is a bug, however operation should continue normally
		 * if it is simply ignored
		 */
		break;

	case FSF_ACCESS_DENIED:
		ZFCP_LOG_NORMAL("Access denied, cannot open port 0x%016Lx "
				"on adapter %s\n",
				port->wwpn, zfcp_get_busid_by_port(port));
		for (counter = 0; counter < 2; counter++) {
			subtable = header->fsf_status_qual.halfword[counter * 2];
			rule = header->fsf_status_qual.halfword[counter * 2 + 1];
			switch (subtable) {
			case FSF_SQ_CFDC_SUBTABLE_OS:
			case FSF_SQ_CFDC_SUBTABLE_PORT_WWPN:
			case FSF_SQ_CFDC_SUBTABLE_PORT_DID:
			case FSF_SQ_CFDC_SUBTABLE_LUN:
				ZFCP_LOG_INFO("Access denied (%s rule %d)\n",
					zfcp_act_subtable_type[subtable], rule);
				break;
			}
		}
2434
		zfcp_erp_port_access_denied(port, 57, fsf_req);
L
Linus Torvalds 已提交
2435 2436 2437 2438 2439 2440 2441 2442
		fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
		break;

	case FSF_MAXIMUM_NUMBER_OF_PORTS_EXCEEDED:
		ZFCP_LOG_INFO("error: The FSF adapter is out of resources. "
			      "The remote port 0x%016Lx on adapter %s "
			      "could not be opened. Disabling it.\n",
			      port->wwpn, zfcp_get_busid_by_port(port));
2443
		zfcp_erp_port_failed(port, 31, fsf_req);
L
Linus Torvalds 已提交
2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462
		fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
		break;

	case FSF_ADAPTER_STATUS_AVAILABLE:
		switch (header->fsf_status_qual.word[0]) {
		case FSF_SQ_INVOKE_LINK_TEST_PROCEDURE:
			/* ERP strategy will escalate */
			fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
			break;
		case FSF_SQ_ULP_DEPENDENT_ERP_REQUIRED:
			/* ERP strategy will escalate */
			fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
			break;
		case FSF_SQ_NO_RETRY_POSSIBLE:
			ZFCP_LOG_NORMAL("The remote port 0x%016Lx on "
					"adapter %s could not be opened. "
					"Disabling it.\n",
					port->wwpn,
					zfcp_get_busid_by_port(port));
2463
			zfcp_erp_port_failed(port, 32, fsf_req);
L
Linus Torvalds 已提交
2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483
			fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
			break;
		default:
			ZFCP_LOG_NORMAL
			    ("bug: Wrong status qualifier 0x%x arrived.\n",
			     header->fsf_status_qual.word[0]);
			break;
		}
		break;

	case FSF_GOOD:
		/* save port handle assigned by FSF */
		port->handle = header->port_handle;
		ZFCP_LOG_INFO("The remote port 0x%016Lx via adapter %s "
			      "was opened, it's port handle is 0x%x\n",
			      port->wwpn, zfcp_get_busid_by_port(port),
			      port->handle);
		/* mark port as open */
		atomic_set_mask(ZFCP_STATUS_COMMON_OPEN |
				ZFCP_STATUS_PORT_PHYS_OPEN, &port->status);
2484 2485 2486
		atomic_clear_mask(ZFCP_STATUS_COMMON_ACCESS_DENIED |
		                  ZFCP_STATUS_COMMON_ACCESS_BOXED,
		                  &port->status);
L
Linus Torvalds 已提交
2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506
		retval = 0;
		/* 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.
		 */
		plogi = (struct fsf_plogi *) fsf_req->qtcb->bottom.support.els;
		if (!atomic_test_mask(ZFCP_STATUS_PORT_NO_WWPN, &port->status))
		{
			if (fsf_req->qtcb->bottom.support.els1_length <
2507
			    sizeof (struct fsf_plogi)) {
L
Linus Torvalds 已提交
2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520
				ZFCP_LOG_INFO(
					"warning: insufficient length of "
					"PLOGI payload (%i)\n",
					fsf_req->qtcb->bottom.support.els1_length);
				/* skip sanity check and assume wwpn is ok */
			} else {
				if (plogi->serv_param.wwpn != port->wwpn) {
					ZFCP_LOG_INFO("warning: d_id of port "
						      "0x%016Lx changed during "
						      "open\n", port->wwpn);
					atomic_clear_mask(
						ZFCP_STATUS_PORT_DID_DID,
						&port->status);
2521
				} else {
L
Linus Torvalds 已提交
2522
					port->wwnn = plogi->serv_param.wwnn;
2523
					zfcp_fc_plogi_evaluate(port, plogi);
2524
				}
L
Linus Torvalds 已提交
2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561
			}
		}
		break;

	case FSF_UNKNOWN_OP_SUBTYPE:
		/* should never occure, subtype not set in zfcp_fsf_open_port */
		ZFCP_LOG_INFO("unknown operation subtype (adapter: %s, "
			      "op_subtype=0x%x)\n",
			      zfcp_get_busid_by_port(port),
			      fsf_req->qtcb->bottom.support.operation_subtype);
		fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
		break;

	default:
		ZFCP_LOG_NORMAL("bug: An unknown FSF Status was presented "
				"(debug info 0x%x)\n",
				header->fsf_status);
		break;
	}

 skip_fsfstatus:
	atomic_clear_mask(ZFCP_STATUS_COMMON_OPENING, &port->status);
	return retval;
}

/*
 * function:    zfcp_fsf_close_port
 *
 * purpose:     submit FSF command "close port"
 *
 * returns:     address of initiated FSF request
 *              NULL - request could not be initiated
 */
int
zfcp_fsf_close_port(struct zfcp_erp_action *erp_action)
{
	volatile struct qdio_buffer_element *sbale;
2562
	struct zfcp_fsf_req *fsf_req;
L
Linus Torvalds 已提交
2563 2564 2565 2566 2567 2568 2569 2570
	unsigned long lock_flags;
	int retval = 0;

	/* setup new FSF request */
	retval = zfcp_fsf_req_create(erp_action->adapter,
				     FSF_QTCB_CLOSE_PORT,
				     ZFCP_WAIT_FOR_SBAL | ZFCP_REQ_AUTO_CLEANUP,
				     erp_action->adapter->pool.fsf_req_erp,
2571
				     &lock_flags, &fsf_req);
L
Linus Torvalds 已提交
2572 2573 2574 2575 2576 2577 2578 2579
	if (retval < 0) {
		ZFCP_LOG_INFO("error: Could not create a close port request "
			      "for port 0x%016Lx on adapter %s.\n",
			      erp_action->port->wwpn,
			      zfcp_get_busid_by_adapter(erp_action->adapter));
		goto out;
	}

S
Swen Schillig 已提交
2580
	sbale = zfcp_qdio_sbale_req(fsf_req);
L
Linus Torvalds 已提交
2581 2582 2583 2584
        sbale[0].flags |= SBAL_FLAGS0_TYPE_READ;
        sbale[1].flags |= SBAL_FLAGS_LAST_ENTRY;

	atomic_set_mask(ZFCP_STATUS_COMMON_CLOSING, &erp_action->port->status);
2585 2586 2587 2588 2589 2590 2591 2592
	fsf_req->data = (unsigned long) erp_action->port;
	fsf_req->erp_action = erp_action;
	fsf_req->qtcb->header.port_handle = erp_action->port->handle;
	fsf_req->erp_action = erp_action;
	erp_action->fsf_req = fsf_req;

	zfcp_erp_start_timer(fsf_req);
	retval = zfcp_fsf_req_send(fsf_req);
L
Linus Torvalds 已提交
2593 2594 2595 2596 2597
	if (retval) {
		ZFCP_LOG_INFO("error: Could not send a close port request for "
			      "port 0x%016Lx on adapter %s.\n",
			      erp_action->port->wwpn,
			      zfcp_get_busid_by_adapter(erp_action->adapter));
2598
		zfcp_fsf_req_free(fsf_req);
L
Linus Torvalds 已提交
2599 2600 2601 2602 2603 2604 2605 2606 2607
		erp_action->fsf_req = NULL;
		goto out;
	}

	ZFCP_LOG_TRACE("close port request initiated "
		       "(adapter %s, port 0x%016Lx)\n",
		       zfcp_get_busid_by_adapter(erp_action->adapter),
		       erp_action->port->wwpn);
 out:
S
Swen Schillig 已提交
2608
	write_unlock_irqrestore(&erp_action->adapter->req_q.lock, lock_flags);
L
Linus Torvalds 已提交
2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624
	return retval;
}

/*
 * function:    zfcp_fsf_close_port_handler
 *
 * purpose:     is called for finished Close Port FSF command
 *
 * returns:
 */
static int
zfcp_fsf_close_port_handler(struct zfcp_fsf_req *fsf_req)
{
	int retval = -EINVAL;
	struct zfcp_port *port;

2625
	port = (struct zfcp_port *) fsf_req->data;
L
Linus Torvalds 已提交
2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643

	if (fsf_req->status & ZFCP_STATUS_FSFREQ_ERROR) {
		/* don't change port status in our bookkeeping */
		goto skip_fsfstatus;
	}

	/* evaluate FSF status in QTCB */
	switch (fsf_req->qtcb->header.fsf_status) {

	case FSF_PORT_HANDLE_NOT_VALID:
		ZFCP_LOG_INFO("Temporary port identifier 0x%x for port "
			      "0x%016Lx on adapter %s invalid. This may happen "
			      "occasionally.\n", port->handle,
			      port->wwpn, zfcp_get_busid_by_port(port));
		ZFCP_LOG_DEBUG("status qualifier:\n");
		ZFCP_HEX_DUMP(ZFCP_LOG_LEVEL_DEBUG,
			      (char *) &fsf_req->qtcb->header.fsf_status_qual,
			      sizeof (union fsf_status_qual));
2644
		zfcp_erp_adapter_reopen(port->adapter, 0, 107, fsf_req);
L
Linus Torvalds 已提交
2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658
		fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
		break;

	case FSF_ADAPTER_STATUS_AVAILABLE:
		/* Note: FSF has actually closed the port in this case.
		 * The status code is just daft. Fingers crossed for a change
		 */
		retval = 0;
		break;

	case FSF_GOOD:
		ZFCP_LOG_TRACE("remote port 0x016%Lx on adapter %s closed, "
			       "port handle 0x%x\n", port->wwpn,
			       zfcp_get_busid_by_port(port), port->handle);
2659
		zfcp_erp_modify_port_status(port, 33, fsf_req,
L
Linus Torvalds 已提交
2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688
					    ZFCP_STATUS_COMMON_OPEN,
					    ZFCP_CLEAR);
		retval = 0;
		break;

	default:
		ZFCP_LOG_NORMAL("bug: An unknown FSF Status was presented "
				"(debug info 0x%x)\n",
				fsf_req->qtcb->header.fsf_status);
		break;
	}

 skip_fsfstatus:
	atomic_clear_mask(ZFCP_STATUS_COMMON_CLOSING, &port->status);
	return retval;
}

/*
 * function:    zfcp_fsf_close_physical_port
 *
 * purpose:     submit FSF command "close physical port"
 *
 * returns:     address of initiated FSF request
 *              NULL - request could not be initiated
 */
int
zfcp_fsf_close_physical_port(struct zfcp_erp_action *erp_action)
{
	volatile struct qdio_buffer_element *sbale;
2689 2690 2691
	struct zfcp_fsf_req *fsf_req;
	unsigned long lock_flags;
	int retval = 0;
L
Linus Torvalds 已提交
2692 2693 2694 2695 2696 2697

	/* setup new FSF request */
	retval = zfcp_fsf_req_create(erp_action->adapter,
				     FSF_QTCB_CLOSE_PHYSICAL_PORT,
				     ZFCP_WAIT_FOR_SBAL | ZFCP_REQ_AUTO_CLEANUP,
				     erp_action->adapter->pool.fsf_req_erp,
2698
				     &lock_flags, &fsf_req);
L
Linus Torvalds 已提交
2699 2700 2701 2702 2703 2704 2705 2706 2707
	if (retval < 0) {
		ZFCP_LOG_INFO("error: Could not create close physical port "
			      "request (adapter %s, port 0x%016Lx)\n",
			      zfcp_get_busid_by_adapter(erp_action->adapter),
			      erp_action->port->wwpn);

		goto out;
	}

S
Swen Schillig 已提交
2708
	sbale = zfcp_qdio_sbale_req(fsf_req);
L
Linus Torvalds 已提交
2709 2710 2711 2712 2713 2714 2715
	sbale[0].flags |= SBAL_FLAGS0_TYPE_READ;
	sbale[1].flags |= SBAL_FLAGS_LAST_ENTRY;

	/* mark port as being closed */
	atomic_set_mask(ZFCP_STATUS_PORT_PHYS_CLOSING,
			&erp_action->port->status);
	/* save a pointer to this port */
2716 2717 2718 2719 2720 2721 2722
	fsf_req->data = (unsigned long) erp_action->port;
	fsf_req->qtcb->header.port_handle = erp_action->port->handle;
	fsf_req->erp_action = erp_action;
	erp_action->fsf_req = fsf_req;

	zfcp_erp_start_timer(fsf_req);
	retval = zfcp_fsf_req_send(fsf_req);
L
Linus Torvalds 已提交
2723 2724 2725 2726 2727
	if (retval) {
		ZFCP_LOG_INFO("error: Could not send close physical port "
			      "request (adapter %s, port 0x%016Lx)\n",
			      zfcp_get_busid_by_adapter(erp_action->adapter),
			      erp_action->port->wwpn);
2728
		zfcp_fsf_req_free(fsf_req);
L
Linus Torvalds 已提交
2729 2730 2731 2732 2733 2734 2735 2736 2737
		erp_action->fsf_req = NULL;
		goto out;
	}

	ZFCP_LOG_TRACE("close physical port request initiated "
		       "(adapter %s, port 0x%016Lx)\n",
		       zfcp_get_busid_by_adapter(erp_action->adapter),
		       erp_action->port->wwpn);
 out:
S
Swen Schillig 已提交
2738
	write_unlock_irqrestore(&erp_action->adapter->req_q.lock, lock_flags);
L
Linus Torvalds 已提交
2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757
	return retval;
}

/*
 * function:    zfcp_fsf_close_physical_port_handler
 *
 * purpose:     is called for finished Close Physical Port FSF command
 *
 * returns:
 */
static int
zfcp_fsf_close_physical_port_handler(struct zfcp_fsf_req *fsf_req)
{
	int retval = -EINVAL;
	struct zfcp_port *port;
	struct zfcp_unit *unit;
	struct fsf_qtcb_header *header;
	u16 subtable, rule, counter;

2758
	port = (struct zfcp_port *) fsf_req->data;
L
Linus Torvalds 已提交
2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779
	header = &fsf_req->qtcb->header;

	if (fsf_req->status & ZFCP_STATUS_FSFREQ_ERROR) {
		/* don't change port status in our bookkeeping */
		goto skip_fsfstatus;
	}

	/* evaluate FSF status in QTCB */
	switch (header->fsf_status) {

	case FSF_PORT_HANDLE_NOT_VALID:
		ZFCP_LOG_INFO("Temporary port identifier 0x%x invalid"
			      "(adapter %s, port 0x%016Lx). "
			      "This may happen occasionally.\n",
			      port->handle,
			      zfcp_get_busid_by_port(port),
			      port->wwpn);
		ZFCP_LOG_DEBUG("status qualifier:\n");
		ZFCP_HEX_DUMP(ZFCP_LOG_LEVEL_DEBUG,
			      (char *) &header->fsf_status_qual,
			      sizeof (union fsf_status_qual));
2780
		zfcp_erp_adapter_reopen(port->adapter, 0, 108, fsf_req);
L
Linus Torvalds 已提交
2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800
		fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
		break;

	case FSF_ACCESS_DENIED:
		ZFCP_LOG_NORMAL("Access denied, cannot close "
				"physical port 0x%016Lx on adapter %s\n",
				port->wwpn, zfcp_get_busid_by_port(port));
		for (counter = 0; counter < 2; counter++) {
			subtable = header->fsf_status_qual.halfword[counter * 2];
			rule = header->fsf_status_qual.halfword[counter * 2 + 1];
			switch (subtable) {
			case FSF_SQ_CFDC_SUBTABLE_OS:
			case FSF_SQ_CFDC_SUBTABLE_PORT_WWPN:
			case FSF_SQ_CFDC_SUBTABLE_PORT_DID:
			case FSF_SQ_CFDC_SUBTABLE_LUN:
	       			ZFCP_LOG_INFO("Access denied (%s rule %d)\n",
					zfcp_act_subtable_type[subtable], rule);
				break;
			}
		}
2801
		zfcp_erp_port_access_denied(port, 58, fsf_req);
L
Linus Torvalds 已提交
2802 2803 2804 2805 2806 2807 2808 2809 2810
		fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
		break;

	case FSF_PORT_BOXED:
		ZFCP_LOG_DEBUG("The remote port 0x%016Lx on adapter "
			       "%s needs to be reopened but it was attempted "
			       "to close it physically.\n",
			       port->wwpn,
			       zfcp_get_busid_by_port(port));
2811
		zfcp_erp_port_boxed(port, 50, fsf_req);
L
Linus Torvalds 已提交
2812 2813
		fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR |
			ZFCP_STATUS_FSFREQ_RETRY;
2814 2815 2816 2817 2818 2819 2820

		/* 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 已提交
2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882
		break;

	case FSF_ADAPTER_STATUS_AVAILABLE:
		switch (header->fsf_status_qual.word[0]) {
		case FSF_SQ_INVOKE_LINK_TEST_PROCEDURE:
			/* This will now be escalated by ERP */
			fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
			break;
		case FSF_SQ_ULP_DEPENDENT_ERP_REQUIRED:
			/* ERP strategy will escalate */
			fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
			break;
		default:
			ZFCP_LOG_NORMAL
			    ("bug: Wrong status qualifier 0x%x arrived.\n",
			     header->fsf_status_qual.word[0]);
			break;
		}
		break;

	case FSF_GOOD:
		ZFCP_LOG_DEBUG("Remote port 0x%016Lx via adapter %s "
			       "physically closed, port handle 0x%x\n",
			       port->wwpn,
			       zfcp_get_busid_by_port(port), port->handle);
		/* 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);
		retval = 0;
		break;

	default:
		ZFCP_LOG_NORMAL("bug: An unknown FSF Status was presented "
				"(debug info 0x%x)\n",
				header->fsf_status);
		break;
	}

 skip_fsfstatus:
	atomic_clear_mask(ZFCP_STATUS_PORT_PHYS_CLOSING, &port->status);
	return retval;
}

/*
 * function:    zfcp_fsf_open_unit
 *
 * purpose:
 *
 * returns:
 *
 * assumptions:	This routine does not check whether the associated
 *		remote port has already been opened. This should be
 *		done by calling routines. Otherwise some status
 *		may be presented by FSF
 */
int
zfcp_fsf_open_unit(struct zfcp_erp_action *erp_action)
{
	volatile struct qdio_buffer_element *sbale;
2883
	struct zfcp_fsf_req *fsf_req;
L
Linus Torvalds 已提交
2884 2885 2886 2887 2888 2889 2890 2891
	unsigned long lock_flags;
	int retval = 0;

	/* setup new FSF request */
	retval = zfcp_fsf_req_create(erp_action->adapter,
				     FSF_QTCB_OPEN_LUN,
				     ZFCP_WAIT_FOR_SBAL | ZFCP_REQ_AUTO_CLEANUP,
				     erp_action->adapter->pool.fsf_req_erp,
2892
				     &lock_flags, &fsf_req);
L
Linus Torvalds 已提交
2893 2894 2895 2896 2897 2898 2899 2900 2901
	if (retval < 0) {
		ZFCP_LOG_INFO("error: Could not create open unit request for "
			      "unit 0x%016Lx on port 0x%016Lx on adapter %s.\n",
			      erp_action->unit->fcp_lun,
			      erp_action->unit->port->wwpn,
			      zfcp_get_busid_by_adapter(erp_action->adapter));
		goto out;
	}

S
Swen Schillig 已提交
2902
	sbale = zfcp_qdio_sbale_req(fsf_req);
L
Linus Torvalds 已提交
2903 2904 2905
        sbale[0].flags |= SBAL_FLAGS0_TYPE_READ;
        sbale[1].flags |= SBAL_FLAGS_LAST_ENTRY;

2906 2907
	fsf_req->qtcb->header.port_handle = erp_action->port->handle;
	fsf_req->qtcb->bottom.support.fcp_lun =	erp_action->unit->fcp_lun;
2908
	if (!(erp_action->adapter->connection_features & FSF_FEATURE_NPIV_MODE))
2909
		fsf_req->qtcb->bottom.support.option =
2910
			FSF_OPEN_LUN_SUPPRESS_BOXING;
L
Linus Torvalds 已提交
2911
	atomic_set_mask(ZFCP_STATUS_COMMON_OPENING, &erp_action->unit->status);
2912 2913 2914
	fsf_req->data = (unsigned long) erp_action->unit;
	fsf_req->erp_action = erp_action;
	erp_action->fsf_req = fsf_req;
L
Linus Torvalds 已提交
2915

2916 2917
	zfcp_erp_start_timer(fsf_req);
	retval = zfcp_fsf_req_send(erp_action->fsf_req);
L
Linus Torvalds 已提交
2918 2919 2920 2921 2922 2923 2924
	if (retval) {
		ZFCP_LOG_INFO("error: Could not send an open unit request "
			      "on the adapter %s, port 0x%016Lx for "
			      "unit 0x%016Lx\n",
			      zfcp_get_busid_by_adapter(erp_action->adapter),
			      erp_action->port->wwpn,
			      erp_action->unit->fcp_lun);
2925
		zfcp_fsf_req_free(fsf_req);
L
Linus Torvalds 已提交
2926 2927 2928 2929 2930 2931 2932 2933 2934
		erp_action->fsf_req = NULL;
		goto out;
	}

	ZFCP_LOG_TRACE("Open LUN request initiated (adapter %s, "
		       "port 0x%016Lx, unit 0x%016Lx)\n",
		       zfcp_get_busid_by_adapter(erp_action->adapter),
		       erp_action->port->wwpn, erp_action->unit->fcp_lun);
 out:
S
Swen Schillig 已提交
2935
	write_unlock_irqrestore(&erp_action->adapter->req_q.lock, lock_flags);
L
Linus Torvalds 已提交
2936 2937 2938 2939 2940 2941 2942 2943
	return retval;
}

/*
 * function:    zfcp_fsf_open_unit_handler
 *
 * purpose:	is called for finished Open LUN command
 *
S
Swen Schillig 已提交
2944
 * returns:
L
Linus Torvalds 已提交
2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955
 */
static int
zfcp_fsf_open_unit_handler(struct zfcp_fsf_req *fsf_req)
{
	int retval = -EINVAL;
	struct zfcp_adapter *adapter;
	struct zfcp_unit *unit;
	struct fsf_qtcb_header *header;
	struct fsf_qtcb_bottom_support *bottom;
	struct fsf_queue_designator *queue_designator;
	u16 subtable, rule, counter;
2956
	int exclusive, readwrite;
L
Linus Torvalds 已提交
2957

2958
	unit = (struct zfcp_unit *) fsf_req->data;
L
Linus Torvalds 已提交
2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970

	if (fsf_req->status & ZFCP_STATUS_FSFREQ_ERROR) {
		/* don't change unit status in our bookkeeping */
		goto skip_fsfstatus;
	}

	adapter = fsf_req->adapter;
	header = &fsf_req->qtcb->header;
	bottom = &fsf_req->qtcb->bottom.support;
	queue_designator = &header->fsf_status_qual.fsf_queue_designator;

	atomic_clear_mask(ZFCP_STATUS_COMMON_ACCESS_DENIED |
2971
			  ZFCP_STATUS_COMMON_ACCESS_BOXED |
L
Linus Torvalds 已提交
2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988
			  ZFCP_STATUS_UNIT_SHARED |
			  ZFCP_STATUS_UNIT_READONLY,
			  &unit->status);

	/* evaluate FSF status in QTCB */
	switch (header->fsf_status) {

	case FSF_PORT_HANDLE_NOT_VALID:
		ZFCP_LOG_INFO("Temporary port identifier 0x%x "
			      "for port 0x%016Lx on adapter %s invalid "
			      "This may happen occasionally\n",
			      unit->port->handle,
			      unit->port->wwpn, zfcp_get_busid_by_unit(unit));
		ZFCP_LOG_DEBUG("status qualifier:\n");
		ZFCP_HEX_DUMP(ZFCP_LOG_LEVEL_DEBUG,
			      (char *) &header->fsf_status_qual,
			      sizeof (union fsf_status_qual));
2989
		zfcp_erp_adapter_reopen(unit->port->adapter, 0, 109, fsf_req);
L
Linus Torvalds 已提交
2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018
		fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
		break;

	case FSF_LUN_ALREADY_OPEN:
		ZFCP_LOG_NORMAL("bug: Attempted to open unit 0x%016Lx on "
				"remote port 0x%016Lx on adapter %s twice.\n",
				unit->fcp_lun,
				unit->port->wwpn, zfcp_get_busid_by_unit(unit));
		fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
		break;

	case FSF_ACCESS_DENIED:
		ZFCP_LOG_NORMAL("Access denied, cannot open unit 0x%016Lx on "
				"remote port 0x%016Lx on adapter %s\n",
				unit->fcp_lun, unit->port->wwpn,
				zfcp_get_busid_by_unit(unit));
		for (counter = 0; counter < 2; counter++) {
			subtable = header->fsf_status_qual.halfword[counter * 2];
			rule = header->fsf_status_qual.halfword[counter * 2 + 1];
			switch (subtable) {
			case FSF_SQ_CFDC_SUBTABLE_OS:
			case FSF_SQ_CFDC_SUBTABLE_PORT_WWPN:
			case FSF_SQ_CFDC_SUBTABLE_PORT_DID:
			case FSF_SQ_CFDC_SUBTABLE_LUN:
				ZFCP_LOG_INFO("Access denied (%s rule %d)\n",
					zfcp_act_subtable_type[subtable], rule);
				break;
			}
		}
3019
		zfcp_erp_unit_access_denied(unit, 59, fsf_req);
L
Linus Torvalds 已提交
3020 3021 3022 3023 3024 3025 3026 3027 3028
		atomic_clear_mask(ZFCP_STATUS_UNIT_SHARED, &unit->status);
                atomic_clear_mask(ZFCP_STATUS_UNIT_READONLY, &unit->status);
		fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
		break;

	case FSF_PORT_BOXED:
		ZFCP_LOG_DEBUG("The remote port 0x%016Lx on adapter %s "
			       "needs to be reopened\n",
			       unit->port->wwpn, zfcp_get_busid_by_unit(unit));
3029
		zfcp_erp_port_boxed(unit->port, 51, fsf_req);
L
Linus Torvalds 已提交
3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068
		fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR |
			ZFCP_STATUS_FSFREQ_RETRY;
		break;

	case FSF_LUN_SHARING_VIOLATION:
		if (header->fsf_status_qual.word[0] != 0) {
			ZFCP_LOG_NORMAL("FCP-LUN 0x%Lx at the remote port "
					"with WWPN 0x%Lx "
					"connected to the adapter %s "
					"is already in use in LPAR%d, CSS%d\n",
					unit->fcp_lun,
					unit->port->wwpn,
					zfcp_get_busid_by_unit(unit),
					queue_designator->hla,
					queue_designator->cssid);
		} else {
			subtable = header->fsf_status_qual.halfword[4];
			rule = header->fsf_status_qual.halfword[5];
			switch (subtable) {
			case FSF_SQ_CFDC_SUBTABLE_OS:
			case FSF_SQ_CFDC_SUBTABLE_PORT_WWPN:
			case FSF_SQ_CFDC_SUBTABLE_PORT_DID:
			case FSF_SQ_CFDC_SUBTABLE_LUN:
				ZFCP_LOG_NORMAL("Access to FCP-LUN 0x%Lx at the "
						"remote port with WWPN 0x%Lx "
						"connected to the adapter %s "
						"is denied (%s rule %d)\n",
						unit->fcp_lun,
						unit->port->wwpn,
						zfcp_get_busid_by_unit(unit),
						zfcp_act_subtable_type[subtable],
						rule);
				break;
			}
		}
		ZFCP_LOG_DEBUG("status qualifier:\n");
		ZFCP_HEX_DUMP(ZFCP_LOG_LEVEL_DEBUG,
			      (char *) &header->fsf_status_qual,
			      sizeof (union fsf_status_qual));
3069
		zfcp_erp_unit_access_denied(unit, 60, fsf_req);
L
Linus Torvalds 已提交
3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082
		atomic_clear_mask(ZFCP_STATUS_UNIT_SHARED, &unit->status);
		atomic_clear_mask(ZFCP_STATUS_UNIT_READONLY, &unit->status);
		fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
		break;

	case FSF_MAXIMUM_NUMBER_OF_LUNS_EXCEEDED:
		ZFCP_LOG_INFO("error: The adapter ran out of resources. "
			      "There is no handle (temporary port identifier) "
			      "available for unit 0x%016Lx on port 0x%016Lx "
			      "on adapter %s\n",
			      unit->fcp_lun,
			      unit->port->wwpn,
			      zfcp_get_busid_by_unit(unit));
3083
		zfcp_erp_unit_failed(unit, 34, fsf_req);
L
Linus Torvalds 已提交
3084 3085 3086 3087 3088 3089 3090
		fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
		break;

	case FSF_ADAPTER_STATUS_AVAILABLE:
		switch (header->fsf_status_qual.word[0]) {
		case FSF_SQ_INVOKE_LINK_TEST_PROCEDURE:
			/* Re-establish link to port */
3091
			zfcp_test_link(unit->port);
L
Linus Torvalds 已提交
3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125
			fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
			break;
		case FSF_SQ_ULP_DEPENDENT_ERP_REQUIRED:
			/* ERP strategy will escalate */
			fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
			break;
		default:
			ZFCP_LOG_NORMAL
			    ("bug: Wrong status qualifier 0x%x arrived.\n",
			     header->fsf_status_qual.word[0]);
		}
		break;

	case FSF_INVALID_COMMAND_OPTION:
		ZFCP_LOG_NORMAL(
			"Invalid option 0x%x has been specified "
			"in QTCB bottom sent to the adapter %s\n",
			bottom->option,
			zfcp_get_busid_by_adapter(adapter));
		fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
		retval = -EINVAL;
		break;

	case FSF_GOOD:
		/* save LUN handle assigned by FSF */
		unit->handle = header->lun_handle;
		ZFCP_LOG_TRACE("unit 0x%016Lx on remote port 0x%016Lx on "
			       "adapter %s opened, port handle 0x%x\n",
			       unit->fcp_lun,
			       unit->port->wwpn,
			       zfcp_get_busid_by_unit(unit),
			       unit->handle);
		/* mark unit as open */
		atomic_set_mask(ZFCP_STATUS_COMMON_OPEN, &unit->status);
3126 3127 3128 3129 3130 3131 3132 3133 3134

		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 已提交
3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152
			if (!exclusive)
		                atomic_set_mask(ZFCP_STATUS_UNIT_SHARED,
						&unit->status);

			if (!readwrite) {
                		atomic_set_mask(ZFCP_STATUS_UNIT_READONLY,
						&unit->status);
                		ZFCP_LOG_NORMAL("read-only access for unit "
						"(adapter %s, wwpn=0x%016Lx, "
						"fcp_lun=0x%016Lx)\n",
						zfcp_get_busid_by_unit(unit),
						unit->port->wwpn,
						unit->fcp_lun);
        		}

        		if (exclusive && !readwrite) {
                		ZFCP_LOG_NORMAL("exclusive access of read-only "
						"unit not supported\n");
3153
				zfcp_erp_unit_failed(unit, 35, fsf_req);
L
Linus Torvalds 已提交
3154
				fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
3155
				zfcp_erp_unit_shutdown(unit, 0, 80, fsf_req);
L
Linus Torvalds 已提交
3156 3157 3158
        		} else if (!exclusive && readwrite) {
                		ZFCP_LOG_NORMAL("shared access of read-write "
						"unit not supported\n");
3159
				zfcp_erp_unit_failed(unit, 36, fsf_req);
L
Linus Torvalds 已提交
3160
				fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
3161
				zfcp_erp_unit_shutdown(unit, 0, 81, fsf_req);
L
Linus Torvalds 已提交
3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185
        		}
		}

		retval = 0;
		break;

	default:
		ZFCP_LOG_NORMAL("bug: An unknown FSF Status was presented "
				"(debug info 0x%x)\n",
				header->fsf_status);
		break;
	}

 skip_fsfstatus:
	atomic_clear_mask(ZFCP_STATUS_COMMON_OPENING, &unit->status);
	return retval;
}

/*
 * function:    zfcp_fsf_close_unit
 *
 * purpose:
 *
 * returns:	address of fsf_req - request successfully initiated
S
Swen Schillig 已提交
3186
 *		NULL -
L
Linus Torvalds 已提交
3187 3188 3189 3190 3191 3192 3193 3194 3195 3196
 *
 * assumptions: This routine does not check whether the associated
 *              remote port/lun has already been opened. This should be
 *              done by calling routines. Otherwise some status
 *              may be presented by FSF
 */
int
zfcp_fsf_close_unit(struct zfcp_erp_action *erp_action)
{
	volatile struct qdio_buffer_element *sbale;
3197
	struct zfcp_fsf_req *fsf_req;
L
Linus Torvalds 已提交
3198 3199 3200 3201 3202 3203 3204 3205
	unsigned long lock_flags;
	int retval = 0;

	/* setup new FSF request */
	retval = zfcp_fsf_req_create(erp_action->adapter,
				     FSF_QTCB_CLOSE_LUN,
				     ZFCP_WAIT_FOR_SBAL | ZFCP_REQ_AUTO_CLEANUP,
				     erp_action->adapter->pool.fsf_req_erp,
3206
				     &lock_flags, &fsf_req);
L
Linus Torvalds 已提交
3207 3208 3209 3210 3211 3212 3213 3214 3215
	if (retval < 0) {
		ZFCP_LOG_INFO("error: Could not create close unit request for "
			      "unit 0x%016Lx on port 0x%016Lx on adapter %s.\n",
			      erp_action->unit->fcp_lun,
			      erp_action->port->wwpn,
			      zfcp_get_busid_by_adapter(erp_action->adapter));
		goto out;
	}

S
Swen Schillig 已提交
3216
	sbale = zfcp_qdio_sbale_req(fsf_req);
L
Linus Torvalds 已提交
3217 3218 3219
        sbale[0].flags |= SBAL_FLAGS0_TYPE_READ;
        sbale[1].flags |= SBAL_FLAGS_LAST_ENTRY;

3220 3221
	fsf_req->qtcb->header.port_handle = erp_action->port->handle;
	fsf_req->qtcb->header.lun_handle = erp_action->unit->handle;
L
Linus Torvalds 已提交
3222
	atomic_set_mask(ZFCP_STATUS_COMMON_CLOSING, &erp_action->unit->status);
3223 3224 3225
	fsf_req->data = (unsigned long) erp_action->unit;
	fsf_req->erp_action = erp_action;
	erp_action->fsf_req = fsf_req;
L
Linus Torvalds 已提交
3226

3227 3228
	zfcp_erp_start_timer(fsf_req);
	retval = zfcp_fsf_req_send(erp_action->fsf_req);
L
Linus Torvalds 已提交
3229 3230 3231 3232 3233 3234
	if (retval) {
		ZFCP_LOG_INFO("error: Could not send a close unit request for "
			      "unit 0x%016Lx on port 0x%016Lx onadapter %s.\n",
			      erp_action->unit->fcp_lun,
			      erp_action->port->wwpn,
			      zfcp_get_busid_by_adapter(erp_action->adapter));
3235
		zfcp_fsf_req_free(fsf_req);
L
Linus Torvalds 已提交
3236 3237 3238 3239 3240 3241 3242 3243 3244
		erp_action->fsf_req = NULL;
		goto out;
	}

	ZFCP_LOG_TRACE("Close LUN request initiated (adapter %s, "
		       "port 0x%016Lx, unit 0x%016Lx)\n",
		       zfcp_get_busid_by_adapter(erp_action->adapter),
		       erp_action->port->wwpn, erp_action->unit->fcp_lun);
 out:
S
Swen Schillig 已提交
3245
	write_unlock_irqrestore(&erp_action->adapter->req_q.lock, lock_flags);
L
Linus Torvalds 已提交
3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261
	return retval;
}

/*
 * function:    zfcp_fsf_close_unit_handler
 *
 * purpose:     is called for finished Close LUN FSF command
 *
 * returns:
 */
static int
zfcp_fsf_close_unit_handler(struct zfcp_fsf_req *fsf_req)
{
	int retval = -EINVAL;
	struct zfcp_unit *unit;

3262
	unit = (struct zfcp_unit *) fsf_req->data;
L
Linus Torvalds 已提交
3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282

	if (fsf_req->status & ZFCP_STATUS_FSFREQ_ERROR) {
		/* don't change unit status in our bookkeeping */
		goto skip_fsfstatus;
	}

	/* evaluate FSF status in QTCB */
	switch (fsf_req->qtcb->header.fsf_status) {

	case FSF_PORT_HANDLE_NOT_VALID:
		ZFCP_LOG_INFO("Temporary port identifier 0x%x for port "
			      "0x%016Lx on adapter %s invalid. This may "
			      "happen in rare circumstances\n",
			      unit->port->handle,
			      unit->port->wwpn,
			      zfcp_get_busid_by_unit(unit));
		ZFCP_LOG_DEBUG("status qualifier:\n");
		ZFCP_HEX_DUMP(ZFCP_LOG_LEVEL_DEBUG,
			      (char *) &fsf_req->qtcb->header.fsf_status_qual,
			      sizeof (union fsf_status_qual));
3283
		zfcp_erp_adapter_reopen(unit->port->adapter, 0, 110, fsf_req);
L
Linus Torvalds 已提交
3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298
		fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
		break;

	case FSF_LUN_HANDLE_NOT_VALID:
		ZFCP_LOG_INFO("Temporary LUN identifier 0x%x of unit "
			      "0x%016Lx on port 0x%016Lx on adapter %s is "
			      "invalid. This may happen occasionally.\n",
			      unit->handle,
			      unit->fcp_lun,
			      unit->port->wwpn,
			      zfcp_get_busid_by_unit(unit));
		ZFCP_LOG_DEBUG("Status qualifier data:\n");
		ZFCP_HEX_DUMP(ZFCP_LOG_LEVEL_DEBUG,
			      (char *) &fsf_req->qtcb->header.fsf_status_qual,
			      sizeof (union fsf_status_qual));
3299
		zfcp_erp_port_reopen(unit->port, 0, 111, fsf_req);
L
Linus Torvalds 已提交
3300 3301 3302 3303 3304 3305 3306 3307
		fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
		break;

	case FSF_PORT_BOXED:
		ZFCP_LOG_DEBUG("The remote port 0x%016Lx on adapter %s "
			       "needs to be reopened\n",
			       unit->port->wwpn,
			       zfcp_get_busid_by_unit(unit));
3308
		zfcp_erp_port_boxed(unit->port, 52, fsf_req);
L
Linus Torvalds 已提交
3309 3310 3311 3312 3313 3314 3315 3316
		fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR |
			ZFCP_STATUS_FSFREQ_RETRY;
		break;

	case FSF_ADAPTER_STATUS_AVAILABLE:
		switch (fsf_req->qtcb->header.fsf_status_qual.word[0]) {
		case FSF_SQ_INVOKE_LINK_TEST_PROCEDURE:
			/* re-establish link to port */
3317
			zfcp_test_link(unit->port);
L
Linus Torvalds 已提交
3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367
			fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
			break;
		case FSF_SQ_ULP_DEPENDENT_ERP_REQUIRED:
			/* ERP strategy will escalate */
			fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
			break;
		default:
			ZFCP_LOG_NORMAL
			    ("bug: Wrong status qualifier 0x%x arrived.\n",
			     fsf_req->qtcb->header.fsf_status_qual.word[0]);
			break;
		}
		break;

	case FSF_GOOD:
		ZFCP_LOG_TRACE("unit 0x%016Lx on port 0x%016Lx on adapter %s "
			       "closed, port handle 0x%x\n",
			       unit->fcp_lun,
			       unit->port->wwpn,
			       zfcp_get_busid_by_unit(unit),
			       unit->handle);
		/* mark unit as closed */
		atomic_clear_mask(ZFCP_STATUS_COMMON_OPEN, &unit->status);
		retval = 0;
		break;

	default:
		ZFCP_LOG_NORMAL("bug: An unknown FSF Status was presented "
				"(debug info 0x%x)\n",
				fsf_req->qtcb->header.fsf_status);
		break;
	}

 skip_fsfstatus:
	atomic_clear_mask(ZFCP_STATUS_COMMON_CLOSING, &unit->status);
	return retval;
}

/**
 * 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
 */
int
zfcp_fsf_send_fcp_command_task(struct zfcp_adapter *adapter,
			       struct zfcp_unit *unit,
			       struct scsi_cmnd * scsi_cmnd,
3368
			       int use_timer, int req_flags)
L
Linus Torvalds 已提交
3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391
{
	struct zfcp_fsf_req *fsf_req = NULL;
	struct fcp_cmnd_iu *fcp_cmnd_iu;
	unsigned int sbtype;
	unsigned long lock_flags;
	int real_bytes = 0;
	int retval = 0;
	int mask;

	/* setup new FSF request */
	retval = zfcp_fsf_req_create(adapter, FSF_QTCB_FCP_CMND, req_flags,
				     adapter->pool.fsf_req_scsi,
				     &lock_flags, &fsf_req);
	if (unlikely(retval < 0)) {
		ZFCP_LOG_DEBUG("error: Could not create FCP command request "
			       "for unit 0x%016Lx on port 0x%016Lx on "
			       "adapter %s\n",
			       unit->fcp_lun,
			       unit->port->wwpn,
			       zfcp_get_busid_by_adapter(adapter));
		goto failed_req_create;
	}

3392 3393 3394 3395 3396 3397
	if (unlikely(!atomic_test_mask(ZFCP_STATUS_COMMON_UNBLOCKED,
			&unit->status))) {
		retval = -EBUSY;
		goto unit_blocked;
	}

3398 3399
	zfcp_unit_get(unit);
	fsf_req->unit = unit;
L
Linus Torvalds 已提交
3400

3401
	/* associate FSF request with SCSI request (for look up on abort) */
3402
	scsi_cmnd->host_scribble = (unsigned char *) fsf_req->req_id;
3403 3404 3405

	/* associate SCSI command with FSF request */
	fsf_req->data = (unsigned long) scsi_cmnd;
L
Linus Torvalds 已提交
3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450

	/* set handles of unit and its parent port in QTCB */
	fsf_req->qtcb->header.lun_handle = unit->handle;
	fsf_req->qtcb->header.port_handle = unit->port->handle;

	/* FSF does not define the structure of the FCP_CMND IU */
	fcp_cmnd_iu = (struct fcp_cmnd_iu *)
	    &(fsf_req->qtcb->bottom.io.fcp_cmnd);

	/*
	 * 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:
		fsf_req->qtcb->bottom.io.data_direction = FSF_DATADIR_CMND;
		/*
		 * FIXME(qdio):
		 * what is the correct type for commands
		 * without 'real' data buffers?
		 */
		sbtype = SBAL_FLAGS0_TYPE_READ;
		break;
	case DMA_FROM_DEVICE:
		fsf_req->qtcb->bottom.io.data_direction = FSF_DATADIR_READ;
		sbtype = SBAL_FLAGS0_TYPE_READ;
		fcp_cmnd_iu->rddata = 1;
		break;
	case DMA_TO_DEVICE:
		fsf_req->qtcb->bottom.io.data_direction = FSF_DATADIR_WRITE;
		sbtype = SBAL_FLAGS0_TYPE_WRITE;
		fcp_cmnd_iu->wddata = 1;
		break;
	case DMA_BIDIRECTIONAL:
	default:
		/*
		 * dummy, catch this condition earlier
		 * in zfcp_scsi_queuecommand
		 */
		goto failed_scsi_cmnd;
	}

	/* set FC service class in QTCB (3 per default) */
3451
	fsf_req->qtcb->bottom.io.service_class = ZFCP_FC_SERVICE_CLASS_DEFAULT;
L
Linus Torvalds 已提交
3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486

	/* set FCP_LUN in FCP_CMND IU in QTCB */
	fcp_cmnd_iu->fcp_lun = unit->fcp_lun;

	mask = ZFCP_STATUS_UNIT_READONLY | ZFCP_STATUS_UNIT_SHARED;

	/* set task attributes in FCP_CMND IU in QTCB */
	if (likely((scsi_cmnd->device->simple_tags) ||
		   (atomic_test_mask(mask, &unit->status))))
		fcp_cmnd_iu->task_attribute = SIMPLE_Q;
	else
		fcp_cmnd_iu->task_attribute = UNTAGGED;

	/* set additional length of FCP_CDB in FCP_CMND IU in QTCB, if needed */
	if (unlikely(scsi_cmnd->cmd_len > FCP_CDB_LENGTH)) {
		fcp_cmnd_iu->add_fcp_cdb_length
		    = (scsi_cmnd->cmd_len - FCP_CDB_LENGTH) >> 2;
		ZFCP_LOG_TRACE("SCSI CDB length is 0x%x, "
			       "additional FCP_CDB length is 0x%x "
			       "(shifted right 2 bits)\n",
			       scsi_cmnd->cmd_len,
			       fcp_cmnd_iu->add_fcp_cdb_length);
	}
	/*
	 * copy SCSI CDB (including additional length, if any) to
	 * FCP_CDB in FCP_CMND IU in QTCB
	 */
	memcpy(fcp_cmnd_iu->fcp_cdb, scsi_cmnd->cmnd, scsi_cmnd->cmd_len);

	/* FCP CMND IU length in QTCB */
	fsf_req->qtcb->bottom.io.fcp_cmnd_length =
		sizeof (struct fcp_cmnd_iu) +
		fcp_cmnd_iu->add_fcp_cdb_length + sizeof (fcp_dl_t);

	/* generate SBALEs from data buffer */
S
Swen Schillig 已提交
3487 3488 3489
	real_bytes = zfcp_qdio_sbals_from_sg(fsf_req, sbtype,
					     scsi_sglist(scsi_cmnd),
					     ZFCP_MAX_SBALS_PER_REQ);
L
Linus Torvalds 已提交
3490 3491 3492 3493 3494
	if (unlikely(real_bytes < 0)) {
		if (fsf_req->sbal_number < ZFCP_MAX_SBALS_PER_REQ) {
			ZFCP_LOG_DEBUG(
				"Data did not fit into available buffer(s), "
			       "waiting for more...\n");
3495 3496 3497 3498 3499 3500 3501 3502 3503
			retval = -EIO;
		} else {
			ZFCP_LOG_NORMAL("error: No truncation implemented but "
					"required. Shutting down unit "
					"(adapter %s, port 0x%016Lx, "
					"unit 0x%016Lx)\n",
					zfcp_get_busid_by_unit(unit),
					unit->port->wwpn,
					unit->fcp_lun);
3504
			zfcp_erp_unit_shutdown(unit, 0, 131, fsf_req);
3505
			retval = -EINVAL;
L
Linus Torvalds 已提交
3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516
		}
		goto no_fit;
	}

	/* set length of FCP data length in FCP_CMND IU in QTCB */
	zfcp_set_fcp_dl(fcp_cmnd_iu, real_bytes);

	ZFCP_LOG_DEBUG("Sending SCSI command:\n");
	ZFCP_HEX_DUMP(ZFCP_LOG_LEVEL_DEBUG,
		      (char *) scsi_cmnd->cmnd, scsi_cmnd->cmd_len);

3517 3518 3519 3520
	if (use_timer)
		zfcp_fsf_start_timer(fsf_req, ZFCP_FSF_REQUEST_TIMEOUT);

	retval = zfcp_fsf_req_send(fsf_req);
L
Linus Torvalds 已提交
3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539
	if (unlikely(retval < 0)) {
		ZFCP_LOG_INFO("error: Could not send FCP command request "
			      "on adapter %s, port 0x%016Lx, unit 0x%016Lx\n",
			      zfcp_get_busid_by_adapter(adapter),
			      unit->port->wwpn,
			      unit->fcp_lun);
		goto send_failed;
	}

	ZFCP_LOG_TRACE("Send FCP Command initiated (adapter %s, "
		       "port 0x%016Lx, unit 0x%016Lx)\n",
		       zfcp_get_busid_by_adapter(adapter),
		       unit->port->wwpn,
		       unit->fcp_lun);
	goto success;

 send_failed:
 no_fit:
 failed_scsi_cmnd:
3540
	zfcp_unit_put(unit);
3541
 unit_blocked:
L
Linus Torvalds 已提交
3542 3543 3544 3545 3546
	zfcp_fsf_req_free(fsf_req);
	fsf_req = NULL;
	scsi_cmnd->host_scribble = NULL;
 success:
 failed_req_create:
S
Swen Schillig 已提交
3547
	write_unlock_irqrestore(&adapter->req_q.lock, lock_flags);
L
Linus Torvalds 已提交
3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574
	return retval;
}

struct zfcp_fsf_req *
zfcp_fsf_send_fcp_command_task_management(struct zfcp_adapter *adapter,
					  struct zfcp_unit *unit,
					  u8 tm_flags, int req_flags)
{
	struct zfcp_fsf_req *fsf_req = NULL;
	int retval = 0;
	struct fcp_cmnd_iu *fcp_cmnd_iu;
	unsigned long lock_flags;
	volatile struct qdio_buffer_element *sbale;

	/* setup new FSF request */
	retval = zfcp_fsf_req_create(adapter, FSF_QTCB_FCP_CMND, req_flags,
				     adapter->pool.fsf_req_scsi,
				     &lock_flags, &fsf_req);
	if (retval < 0) {
		ZFCP_LOG_INFO("error: Could not create FCP command (task "
			      "management) request for adapter %s, port "
			      " 0x%016Lx, unit 0x%016Lx.\n",
			      zfcp_get_busid_by_adapter(adapter),
			      unit->port->wwpn, unit->fcp_lun);
		goto out;
	}

3575 3576 3577 3578
	if (unlikely(!atomic_test_mask(ZFCP_STATUS_COMMON_UNBLOCKED,
			&unit->status)))
		goto unit_blocked;

L
Linus Torvalds 已提交
3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589
	/*
	 * Used to decide on proper handler in the return path,
	 * could be either zfcp_fsf_send_fcp_command_task_handler or
	 * zfcp_fsf_send_fcp_command_task_management_handler */

	fsf_req->status |= ZFCP_STATUS_FSFREQ_TASK_MANAGEMENT;

	/*
	 * hold a pointer to the unit being target of this
	 * task management request
	 */
3590
	fsf_req->data = (unsigned long) unit;
L
Linus Torvalds 已提交
3591 3592 3593 3594 3595

	/* set FSF related fields in QTCB */
	fsf_req->qtcb->header.lun_handle = unit->handle;
	fsf_req->qtcb->header.port_handle = unit->port->handle;
	fsf_req->qtcb->bottom.io.data_direction = FSF_DATADIR_CMND;
3596
	fsf_req->qtcb->bottom.io.service_class = ZFCP_FC_SERVICE_CLASS_DEFAULT;
L
Linus Torvalds 已提交
3597 3598 3599
	fsf_req->qtcb->bottom.io.fcp_cmnd_length =
		sizeof (struct fcp_cmnd_iu) + sizeof (fcp_dl_t);

S
Swen Schillig 已提交
3600
	sbale = zfcp_qdio_sbale_req(fsf_req);
L
Linus Torvalds 已提交
3601 3602 3603 3604 3605 3606 3607 3608 3609
	sbale[0].flags |= SBAL_FLAGS0_TYPE_WRITE;
	sbale[1].flags |= SBAL_FLAGS_LAST_ENTRY;

	/* set FCP related fields in FCP_CMND IU in QTCB */
	fcp_cmnd_iu = (struct fcp_cmnd_iu *)
		&(fsf_req->qtcb->bottom.io.fcp_cmnd);
	fcp_cmnd_iu->fcp_lun = unit->fcp_lun;
	fcp_cmnd_iu->task_management_flags = tm_flags;

3610 3611
	zfcp_fsf_start_timer(fsf_req, ZFCP_SCSI_ER_TIMEOUT);
	retval = zfcp_fsf_req_send(fsf_req);
3612
	if (!retval)
L
Linus Torvalds 已提交
3613 3614
		goto out;

3615 3616 3617 3618
 unit_blocked:
	zfcp_fsf_req_free(fsf_req);
	fsf_req = NULL;

L
Linus Torvalds 已提交
3619
 out:
S
Swen Schillig 已提交
3620
	write_unlock_irqrestore(&adapter->req_q.lock, lock_flags);
L
Linus Torvalds 已提交
3621 3622 3623
	return fsf_req;
}

3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663
static void zfcp_fsf_update_lat(struct fsf_latency_record *lat_rec, u32 lat)
{
	lat_rec->sum += lat;
	if (lat_rec->min > lat)
		lat_rec->min = lat;
	if (lat_rec->max < lat)
		lat_rec->max = lat;
}

static void zfcp_fsf_req_latency(struct zfcp_fsf_req *fsf_req)
{
	struct fsf_qual_latency_info *lat_inf;
	struct latency_cont *lat;
	struct zfcp_unit *unit;
	unsigned long flags;

	lat_inf = &fsf_req->qtcb->prefix.prot_status_qual.latency_info;
	unit = fsf_req->unit;

	switch (fsf_req->qtcb->bottom.io.data_direction) {
	case FSF_DATADIR_READ:
		lat = &unit->latencies.read;
		break;
	case FSF_DATADIR_WRITE:
		lat = &unit->latencies.write;
		break;
	case FSF_DATADIR_CMND:
		lat = &unit->latencies.cmd;
		break;
	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++;
	spin_unlock_irqrestore(&unit->latencies.lock, flags);
}

L
Linus Torvalds 已提交
3664 3665 3666 3667 3668
/*
 * function:    zfcp_fsf_send_fcp_command_handler
 *
 * purpose:	is called for finished Send FCP Command
 *
S
Swen Schillig 已提交
3669
 * returns:
L
Linus Torvalds 已提交
3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681
 */
static int
zfcp_fsf_send_fcp_command_handler(struct zfcp_fsf_req *fsf_req)
{
	int retval = -EINVAL;
	struct zfcp_unit *unit;
	struct fsf_qtcb_header *header;
	u16 subtable, rule, counter;

	header = &fsf_req->qtcb->header;

	if (unlikely(fsf_req->status & ZFCP_STATUS_FSFREQ_TASK_MANAGEMENT))
3682
		unit = (struct zfcp_unit *) fsf_req->data;
L
Linus Torvalds 已提交
3683
	else
3684
		unit = fsf_req->unit;
L
Linus Torvalds 已提交
3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701

	if (unlikely(fsf_req->status & ZFCP_STATUS_FSFREQ_ERROR)) {
		/* go directly to calls of special handlers */
		goto skip_fsfstatus;
	}

	/* evaluate FSF status in QTCB */
	switch (header->fsf_status) {

	case FSF_PORT_HANDLE_NOT_VALID:
		ZFCP_LOG_INFO("Temporary port identifier 0x%x for port "
			      "0x%016Lx on adapter %s invalid\n",
			      unit->port->handle,
			      unit->port->wwpn, zfcp_get_busid_by_unit(unit));
		ZFCP_HEX_DUMP(ZFCP_LOG_LEVEL_DEBUG,
			      (char *) &header->fsf_status_qual,
			      sizeof (union fsf_status_qual));
3702
		zfcp_erp_adapter_reopen(unit->port->adapter, 0, 112, fsf_req);
L
Linus Torvalds 已提交
3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717
		fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
		break;

	case FSF_LUN_HANDLE_NOT_VALID:
		ZFCP_LOG_INFO("Temporary LUN identifier 0x%x for unit "
			      "0x%016Lx on port 0x%016Lx on adapter %s is "
			      "invalid. This may happen occasionally.\n",
			      unit->handle,
			      unit->fcp_lun,
			      unit->port->wwpn,
			      zfcp_get_busid_by_unit(unit));
		ZFCP_LOG_NORMAL("Status qualifier data:\n");
		ZFCP_HEX_DUMP(ZFCP_LOG_LEVEL_NORMAL,
			      (char *) &header->fsf_status_qual,
			      sizeof (union fsf_status_qual));
3718
		zfcp_erp_port_reopen(unit->port, 0, 113, fsf_req);
L
Linus Torvalds 已提交
3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733
		fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
		break;

	case FSF_HANDLE_MISMATCH:
		ZFCP_LOG_NORMAL("bug: The port handle 0x%x has changed "
				"unexpectedly. (adapter %s, port 0x%016Lx, "
				"unit 0x%016Lx)\n",
				unit->port->handle,
				zfcp_get_busid_by_unit(unit),
				unit->port->wwpn,
				unit->fcp_lun);
		ZFCP_LOG_NORMAL("status qualifier:\n");
		ZFCP_HEX_DUMP(ZFCP_LOG_LEVEL_NORMAL,
			      (char *) &header->fsf_status_qual,
			      sizeof (union fsf_status_qual));
3734
		zfcp_erp_adapter_reopen(unit->port->adapter, 0, 114, fsf_req);
L
Linus Torvalds 已提交
3735 3736 3737 3738
		fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
		break;

	case FSF_SERVICE_CLASS_NOT_SUPPORTED:
3739 3740 3741 3742
		ZFCP_LOG_INFO("error: adapter %s does not support fc "
			      "class %d.\n",
			      zfcp_get_busid_by_unit(unit),
			      ZFCP_FC_SERVICE_CLASS_DEFAULT);
L
Linus Torvalds 已提交
3743
		/* stop operation for this adapter */
3744
		zfcp_erp_adapter_shutdown(unit->port->adapter, 0, 132, fsf_req);
L
Linus Torvalds 已提交
3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759
		fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
		break;

	case FSF_FCPLUN_NOT_VALID:
		ZFCP_LOG_NORMAL("bug: unit 0x%016Lx on port 0x%016Lx on "
				"adapter %s does not have correct unit "
				"handle 0x%x\n",
				unit->fcp_lun,
				unit->port->wwpn,
				zfcp_get_busid_by_unit(unit),
				unit->handle);
		ZFCP_LOG_DEBUG("status qualifier:\n");
		ZFCP_HEX_DUMP(ZFCP_LOG_LEVEL_DEBUG,
			      (char *) &header->fsf_status_qual,
			      sizeof (union fsf_status_qual));
3760
		zfcp_erp_port_reopen(unit->port, 0, 115, fsf_req);
L
Linus Torvalds 已提交
3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781
		fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
		break;

	case FSF_ACCESS_DENIED:
		ZFCP_LOG_NORMAL("Access denied, cannot send FCP command to "
				"unit 0x%016Lx on port 0x%016Lx on "
				"adapter %s\n",	unit->fcp_lun, unit->port->wwpn,
				zfcp_get_busid_by_unit(unit));
		for (counter = 0; counter < 2; counter++) {
			subtable = header->fsf_status_qual.halfword[counter * 2];
			rule = header->fsf_status_qual.halfword[counter * 2 + 1];
			switch (subtable) {
			case FSF_SQ_CFDC_SUBTABLE_OS:
			case FSF_SQ_CFDC_SUBTABLE_PORT_WWPN:
			case FSF_SQ_CFDC_SUBTABLE_PORT_DID:
			case FSF_SQ_CFDC_SUBTABLE_LUN:
				ZFCP_LOG_INFO("Access denied (%s rule %d)\n",
					zfcp_act_subtable_type[subtable], rule);
				break;
			}
		}
3782
		zfcp_erp_unit_access_denied(unit, 61, fsf_req);
L
Linus Torvalds 已提交
3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794
		fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
		break;

	case FSF_DIRECTION_INDICATOR_NOT_VALID:
		ZFCP_LOG_INFO("bug: Invalid data direction given for unit "
			      "0x%016Lx on port 0x%016Lx on adapter %s "
			      "(debug info %d)\n",
			      unit->fcp_lun,
			      unit->port->wwpn,
			      zfcp_get_busid_by_unit(unit),
			      fsf_req->qtcb->bottom.io.data_direction);
		/* stop operation for this adapter */
3795
		zfcp_erp_adapter_shutdown(unit->port->adapter, 0, 133, fsf_req);
L
Linus Torvalds 已提交
3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807
		fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
		break;

	case FSF_CMND_LENGTH_NOT_VALID:
		ZFCP_LOG_NORMAL
		    ("bug: An invalid control-data-block length field "
		     "was found in a command for unit 0x%016Lx on port "
		     "0x%016Lx on adapter %s " "(debug info %d)\n",
		     unit->fcp_lun, unit->port->wwpn,
		     zfcp_get_busid_by_unit(unit),
		     fsf_req->qtcb->bottom.io.fcp_cmnd_length);
		/* stop operation for this adapter */
3808
		zfcp_erp_adapter_shutdown(unit->port->adapter, 0, 134, fsf_req);
L
Linus Torvalds 已提交
3809 3810 3811 3812 3813 3814 3815
		fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
		break;

	case FSF_PORT_BOXED:
		ZFCP_LOG_DEBUG("The remote port 0x%016Lx on adapter %s "
			       "needs to be reopened\n",
			       unit->port->wwpn, zfcp_get_busid_by_unit(unit));
3816
		zfcp_erp_port_boxed(unit->port, 53, fsf_req);
L
Linus Torvalds 已提交
3817 3818 3819 3820 3821 3822 3823 3824 3825
		fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR |
			ZFCP_STATUS_FSFREQ_RETRY;
		break;

	case FSF_LUN_BOXED:
		ZFCP_LOG_NORMAL("unit needs to be reopened (adapter %s, "
				"wwpn=0x%016Lx, fcp_lun=0x%016Lx)\n",
				zfcp_get_busid_by_unit(unit),
				unit->port->wwpn, unit->fcp_lun);
3826
		zfcp_erp_unit_boxed(unit, 54, fsf_req);
L
Linus Torvalds 已提交
3827 3828 3829 3830 3831 3832 3833 3834
		fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR
			| ZFCP_STATUS_FSFREQ_RETRY;
		break;

	case FSF_ADAPTER_STATUS_AVAILABLE:
		switch (header->fsf_status_qual.word[0]) {
		case FSF_SQ_INVOKE_LINK_TEST_PROCEDURE:
			/* re-establish link to port */
3835
 			zfcp_test_link(unit->port);
L
Linus Torvalds 已提交
3836 3837 3838 3839 3840 3841 3842
			break;
		case FSF_SQ_ULP_DEPENDENT_ERP_REQUIRED:
			/* FIXME(hw) need proper specs for proper action */
			/* let scsi stack deal with retries and escalation */
			break;
		default:
			ZFCP_LOG_NORMAL
3843
 			    ("Unknown status qualifier 0x%x arrived.\n",
L
Linus Torvalds 已提交
3844 3845 3846
			     header->fsf_status_qual.word[0]);
			break;
		}
3847
		fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
L
Linus Torvalds 已提交
3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862
		break;

	case FSF_GOOD:
		break;

	case FSF_FCP_RSP_AVAILABLE:
		break;
	}

 skip_fsfstatus:
	if (fsf_req->status & ZFCP_STATUS_FSFREQ_TASK_MANAGEMENT) {
		retval =
		    zfcp_fsf_send_fcp_command_task_management_handler(fsf_req);
	} else {
		retval = zfcp_fsf_send_fcp_command_task_handler(fsf_req);
3863 3864
		fsf_req->unit = NULL;
		zfcp_unit_put(unit);
L
Linus Torvalds 已提交
3865 3866 3867 3868 3869 3870 3871 3872 3873
	}
	return retval;
}

/*
 * function:    zfcp_fsf_send_fcp_command_task_handler
 *
 * purpose:	evaluates FCP_RSP IU
 *
S
Swen Schillig 已提交
3874
 * returns:
L
Linus Torvalds 已提交
3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887
 */
static int
zfcp_fsf_send_fcp_command_task_handler(struct zfcp_fsf_req *fsf_req)
{
	int retval = 0;
	struct scsi_cmnd *scpnt;
	struct fcp_rsp_iu *fcp_rsp_iu = (struct fcp_rsp_iu *)
	    &(fsf_req->qtcb->bottom.io.fcp_rsp);
	struct fcp_cmnd_iu *fcp_cmnd_iu = (struct fcp_cmnd_iu *)
	    &(fsf_req->qtcb->bottom.io.fcp_cmnd);
	u32 sns_len;
	char *fcp_rsp_info = zfcp_get_fcp_rsp_info_ptr(fcp_rsp_iu);
	unsigned long flags;
3888
	struct zfcp_unit *unit = fsf_req->unit;
L
Linus Torvalds 已提交
3889 3890

	read_lock_irqsave(&fsf_req->adapter->abort_lock, flags);
3891
	scpnt = (struct scsi_cmnd *) fsf_req->data;
L
Linus Torvalds 已提交
3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936
	if (unlikely(!scpnt)) {
		ZFCP_LOG_DEBUG
		    ("Command with fsf_req %p is not associated to "
		     "a scsi command anymore. Aborted?\n", fsf_req);
		goto out;
	}
	if (unlikely(fsf_req->status & ZFCP_STATUS_FSFREQ_ABORTED)) {
		/* FIXME: (design) mid-layer should handle DID_ABORT like
		 *        DID_SOFT_ERROR by retrying the request for devices
		 *        that allow retries.
		 */
		ZFCP_LOG_DEBUG("Setting DID_SOFT_ERROR and SUGGEST_RETRY\n");
		set_host_byte(&scpnt->result, DID_SOFT_ERROR);
		set_driver_byte(&scpnt->result, SUGGEST_RETRY);
		goto skip_fsfstatus;
	}

	if (unlikely(fsf_req->status & ZFCP_STATUS_FSFREQ_ERROR)) {
		ZFCP_LOG_DEBUG("Setting DID_ERROR\n");
		set_host_byte(&scpnt->result, DID_ERROR);
		goto skip_fsfstatus;
	}

	/* set message byte of result in SCSI command */
	scpnt->result |= COMMAND_COMPLETE << 8;

	/*
	 * copy SCSI status code of FCP_STATUS of FCP_RSP IU to status byte
	 * of result in SCSI command
	 */
	scpnt->result |= fcp_rsp_iu->scsi_status;
	if (unlikely(fcp_rsp_iu->scsi_status)) {
		/* DEBUG */
		ZFCP_LOG_DEBUG("status for SCSI Command:\n");
		ZFCP_HEX_DUMP(ZFCP_LOG_LEVEL_DEBUG,
			      scpnt->cmnd, scpnt->cmd_len);
		ZFCP_LOG_DEBUG("SCSI status code 0x%x\n",
				fcp_rsp_iu->scsi_status);
		ZFCP_HEX_DUMP(ZFCP_LOG_LEVEL_DEBUG,
			      (void *) fcp_rsp_iu, sizeof (struct fcp_rsp_iu));
		ZFCP_HEX_DUMP(ZFCP_LOG_LEVEL_DEBUG,
			      zfcp_get_fcp_sns_info_ptr(fcp_rsp_iu),
			      fcp_rsp_iu->fcp_sns_len);
	}

3937 3938 3939
	if (fsf_req->adapter->adapter_features & FSF_FEATURE_MEASUREMENT_DATA)
		zfcp_fsf_req_latency(fsf_req);

L
Linus Torvalds 已提交
3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014
	/* check FCP_RSP_INFO */
	if (unlikely(fcp_rsp_iu->validity.bits.fcp_rsp_len_valid)) {
		ZFCP_LOG_DEBUG("rsp_len is valid\n");
		switch (fcp_rsp_info[3]) {
		case RSP_CODE_GOOD:
			/* ok, continue */
			ZFCP_LOG_TRACE("no failure or Task Management "
				       "Function complete\n");
			set_host_byte(&scpnt->result, DID_OK);
			break;
		case RSP_CODE_LENGTH_MISMATCH:
			/* hardware bug */
			ZFCP_LOG_NORMAL("bug: FCP response code indictates "
					"that the fibrechannel protocol data "
					"length differs from the burst length. "
					"The problem occured on unit 0x%016Lx "
					"on port 0x%016Lx on adapter %s",
					unit->fcp_lun,
					unit->port->wwpn,
					zfcp_get_busid_by_unit(unit));
			/* dump SCSI CDB as prepared by zfcp */
			ZFCP_HEX_DUMP(ZFCP_LOG_LEVEL_DEBUG,
				      (char *) &fsf_req->qtcb->
				      bottom.io.fcp_cmnd, FSF_FCP_CMND_SIZE);
			set_host_byte(&scpnt->result, DID_ERROR);
			goto skip_fsfstatus;
		case RSP_CODE_FIELD_INVALID:
			/* driver or hardware bug */
			ZFCP_LOG_NORMAL("bug: FCP response code indictates "
					"that the fibrechannel protocol data "
					"fields were incorrectly set up. "
					"The problem occured on the unit "
					"0x%016Lx on port 0x%016Lx on "
					"adapter %s",
					unit->fcp_lun,
					unit->port->wwpn,
					zfcp_get_busid_by_unit(unit));
			/* dump SCSI CDB as prepared by zfcp */
			ZFCP_HEX_DUMP(ZFCP_LOG_LEVEL_DEBUG,
				      (char *) &fsf_req->qtcb->
				      bottom.io.fcp_cmnd, FSF_FCP_CMND_SIZE);
			set_host_byte(&scpnt->result, DID_ERROR);
			goto skip_fsfstatus;
		case RSP_CODE_RO_MISMATCH:
			/* hardware bug */
			ZFCP_LOG_NORMAL("bug: The FCP response code indicates "
					"that conflicting  values for the "
					"fibrechannel payload offset from the "
					"header were found. "
					"The problem occured on unit 0x%016Lx "
					"on port 0x%016Lx on adapter %s.\n",
					unit->fcp_lun,
					unit->port->wwpn,
					zfcp_get_busid_by_unit(unit));
			/* dump SCSI CDB as prepared by zfcp */
			ZFCP_HEX_DUMP(ZFCP_LOG_LEVEL_DEBUG,
				      (char *) &fsf_req->qtcb->
				      bottom.io.fcp_cmnd, FSF_FCP_CMND_SIZE);
			set_host_byte(&scpnt->result, DID_ERROR);
			goto skip_fsfstatus;
		default:
			ZFCP_LOG_NORMAL("bug: An invalid FCP response "
					"code was detected for a command. "
					"The problem occured on the unit "
					"0x%016Lx on port 0x%016Lx on "
					"adapter %s (debug info 0x%x)\n",
					unit->fcp_lun,
					unit->port->wwpn,
					zfcp_get_busid_by_unit(unit),
					fcp_rsp_info[3]);
			/* dump SCSI CDB as prepared by zfcp */
			ZFCP_HEX_DUMP(ZFCP_LOG_LEVEL_DEBUG,
				      (char *) &fsf_req->qtcb->
				      bottom.io.fcp_cmnd, FSF_FCP_CMND_SIZE);
			set_host_byte(&scpnt->result, DID_ERROR);
已提交
4015
			goto skip_fsfstatus;
L
Linus Torvalds 已提交
4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031
		}
	}

	/* check for sense data */
	if (unlikely(fcp_rsp_iu->validity.bits.fcp_sns_len_valid)) {
		sns_len = FSF_FCP_RSP_SIZE -
		    sizeof (struct fcp_rsp_iu) + fcp_rsp_iu->fcp_rsp_len;
		ZFCP_LOG_TRACE("room for %i bytes sense data in QTCB\n",
			       sns_len);
		sns_len = min(sns_len, (u32) SCSI_SENSE_BUFFERSIZE);
		ZFCP_LOG_TRACE("room for %i bytes sense data in SCSI command\n",
			       SCSI_SENSE_BUFFERSIZE);
		sns_len = min(sns_len, fcp_rsp_iu->fcp_sns_len);
		ZFCP_LOG_TRACE("scpnt->result =0x%x, command was:\n",
			       scpnt->result);
		ZFCP_HEX_DUMP(ZFCP_LOG_LEVEL_TRACE,
4032
			      scpnt->cmnd, scpnt->cmd_len);
L
Linus Torvalds 已提交
4033 4034 4035

		ZFCP_LOG_TRACE("%i bytes sense data provided by FCP\n",
			       fcp_rsp_iu->fcp_sns_len);
4036
		memcpy(scpnt->sense_buffer,
L
Linus Torvalds 已提交
4037 4038
		       zfcp_get_fcp_sns_info_ptr(fcp_rsp_iu), sns_len);
		ZFCP_HEX_DUMP(ZFCP_LOG_LEVEL_TRACE,
4039
			      (void *)scpnt->sense_buffer, sns_len);
L
Linus Torvalds 已提交
4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066
	}

	/* check for overrun */
	if (unlikely(fcp_rsp_iu->validity.bits.fcp_resid_over)) {
		ZFCP_LOG_INFO("A data overrun was detected for a command. "
			      "unit 0x%016Lx, port 0x%016Lx, adapter %s. "
			      "The response data length is "
			      "%d, the original length was %d.\n",
			      unit->fcp_lun,
			      unit->port->wwpn,
			      zfcp_get_busid_by_unit(unit),
			      fcp_rsp_iu->fcp_resid,
			      (int) zfcp_get_fcp_dl(fcp_cmnd_iu));
	}

	/* check for underrun */
	if (unlikely(fcp_rsp_iu->validity.bits.fcp_resid_under)) {
		ZFCP_LOG_INFO("A data underrun was detected for a command. "
			      "unit 0x%016Lx, port 0x%016Lx, adapter %s. "
			      "The response data length is "
			      "%d, the original length was %d.\n",
			      unit->fcp_lun,
			      unit->port->wwpn,
			      zfcp_get_busid_by_unit(unit),
			      fcp_rsp_iu->fcp_resid,
			      (int) zfcp_get_fcp_dl(fcp_cmnd_iu));

4067 4068 4069
		scsi_set_resid(scpnt, fcp_rsp_iu->fcp_resid);
		if (scsi_bufflen(scpnt) - scsi_get_resid(scpnt) <
		    scpnt->underflow)
已提交
4070
			set_host_byte(&scpnt->result, DID_ERROR);
L
Linus Torvalds 已提交
4071 4072 4073 4074 4075
	}

 skip_fsfstatus:
	ZFCP_LOG_DEBUG("scpnt->result =0x%x\n", scpnt->result);

4076
	if (scpnt->result != 0)
4077
		zfcp_scsi_dbf_event_result("erro", 3, fsf_req->adapter, scpnt, fsf_req);
4078
	else if (scpnt->retries > 0)
4079
		zfcp_scsi_dbf_event_result("retr", 4, fsf_req->adapter, scpnt, fsf_req);
4080
	else
4081
		zfcp_scsi_dbf_event_result("norm", 6, fsf_req->adapter, scpnt, fsf_req);
L
Linus Torvalds 已提交
4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104

	/* cleanup pointer (need this especially for abort) */
	scpnt->host_scribble = NULL;

	/* always call back */
	(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!
	 */
 out:
	read_unlock_irqrestore(&fsf_req->adapter->abort_lock, flags);
	return retval;
}

/*
 * function:    zfcp_fsf_send_fcp_command_task_management_handler
 *
 * purpose:	evaluates FCP_RSP IU
 *
S
Swen Schillig 已提交
4105
 * returns:
L
Linus Torvalds 已提交
4106 4107 4108 4109 4110 4111 4112 4113
 */
static int
zfcp_fsf_send_fcp_command_task_management_handler(struct zfcp_fsf_req *fsf_req)
{
	int retval = 0;
	struct fcp_rsp_iu *fcp_rsp_iu = (struct fcp_rsp_iu *)
	    &(fsf_req->qtcb->bottom.io.fcp_rsp);
	char *fcp_rsp_info = zfcp_get_fcp_rsp_info_ptr(fcp_rsp_iu);
4114
	struct zfcp_unit *unit = (struct zfcp_unit *) fsf_req->data;
L
Linus Torvalds 已提交
4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173

	if (fsf_req->status & ZFCP_STATUS_FSFREQ_ERROR) {
		fsf_req->status |= ZFCP_STATUS_FSFREQ_TMFUNCFAILED;
		goto skip_fsfstatus;
	}

	/* check FCP_RSP_INFO */
	switch (fcp_rsp_info[3]) {
	case RSP_CODE_GOOD:
		/* ok, continue */
		ZFCP_LOG_DEBUG("no failure or Task Management "
			       "Function complete\n");
		break;
	case RSP_CODE_TASKMAN_UNSUPP:
		ZFCP_LOG_NORMAL("bug: A reuested task management function "
				"is not supported on the target device "
				"unit 0x%016Lx, port 0x%016Lx, adapter %s\n ",
				unit->fcp_lun,
				unit->port->wwpn,
				zfcp_get_busid_by_unit(unit));
		fsf_req->status |= ZFCP_STATUS_FSFREQ_TMFUNCNOTSUPP;
		break;
	case RSP_CODE_TASKMAN_FAILED:
		ZFCP_LOG_NORMAL("bug: A reuested task management function "
				"failed to complete successfully. "
				"unit 0x%016Lx, port 0x%016Lx, adapter %s.\n",
				unit->fcp_lun,
				unit->port->wwpn,
				zfcp_get_busid_by_unit(unit));
		fsf_req->status |= ZFCP_STATUS_FSFREQ_TMFUNCFAILED;
		break;
	default:
		ZFCP_LOG_NORMAL("bug: An invalid FCP response "
				"code was detected for a command. "
				"unit 0x%016Lx, port 0x%016Lx, adapter %s "
				"(debug info 0x%x)\n",
				unit->fcp_lun,
				unit->port->wwpn,
				zfcp_get_busid_by_unit(unit),
				fcp_rsp_info[3]);
		fsf_req->status |= ZFCP_STATUS_FSFREQ_TMFUNCFAILED;
	}

      skip_fsfstatus:
	return retval;
}


/*
 * function:    zfcp_fsf_control_file
 *
 * purpose:     Initiator of the control file upload/download FSF requests
 *
 * returns:     0           - FSF request is successfuly created and queued
 *              -EOPNOTSUPP - The FCP adapter does not have Control File support
 *              -EINVAL     - Invalid direction specified
 *              -ENOMEM     - Insufficient memory
 *              -EPERM      - Cannot create FSF request or place it in QDIO queue
 */
4174 4175
struct zfcp_fsf_req *zfcp_fsf_control_file(struct zfcp_adapter *adapter,
					   struct zfcp_fsf_cfdc *fsf_cfdc)
L
Linus Torvalds 已提交
4176 4177 4178 4179 4180 4181
{
	struct zfcp_fsf_req *fsf_req;
	struct fsf_qtcb_bottom_support *bottom;
	volatile struct qdio_buffer_element *sbale;
	unsigned long lock_flags;
	int direction;
4182 4183
	int retval;
	int bytes;
L
Linus Torvalds 已提交
4184

4185 4186
	if (!(adapter->adapter_features & FSF_FEATURE_CFDC))
		return ERR_PTR(-EOPNOTSUPP);
L
Linus Torvalds 已提交
4187

4188
	switch (fsf_cfdc->command) {
L
Linus Torvalds 已提交
4189 4190 4191 4192 4193 4194 4195
	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:
4196
		return ERR_PTR(-EINVAL);
L
Linus Torvalds 已提交
4197 4198
	}

4199 4200
	retval = zfcp_fsf_req_create(adapter, fsf_cfdc->command,
				     ZFCP_WAIT_FOR_SBAL,
L
Linus Torvalds 已提交
4201 4202 4203 4204 4205 4206
				     NULL, &lock_flags, &fsf_req);
	if (retval < 0) {
		retval = -EPERM;
		goto unlock_queue_lock;
	}

S
Swen Schillig 已提交
4207
	sbale = zfcp_qdio_sbale_req(fsf_req);
L
Linus Torvalds 已提交
4208 4209 4210 4211
	sbale[0].flags |= direction;

	bottom = &fsf_req->qtcb->bottom.support;
	bottom->operation_subtype = FSF_CFDC_OPERATION_SUBTYPE;
4212 4213 4214
	bottom->option = fsf_cfdc->option;

	bytes = zfcp_qdio_sbals_from_sg(fsf_req, direction,
S
Swen Schillig 已提交
4215
					fsf_cfdc->sg,
4216 4217 4218 4219 4220
					ZFCP_MAX_SBALS_PER_REQ);
	if (bytes != ZFCP_CFDC_MAX_SIZE) {
		retval = -ENOMEM;
		goto free_fsf_req;
	}
L
Linus Torvalds 已提交
4221

4222 4223
	zfcp_fsf_start_timer(fsf_req, ZFCP_FSF_REQUEST_TIMEOUT);
	retval = zfcp_fsf_req_send(fsf_req);
L
Linus Torvalds 已提交
4224 4225 4226 4227
	if (retval < 0) {
		retval = -EPERM;
		goto free_fsf_req;
	}
S
Swen Schillig 已提交
4228
	write_unlock_irqrestore(&adapter->req_q.lock, lock_flags);
L
Linus Torvalds 已提交
4229 4230 4231 4232

	wait_event(fsf_req->completion_wq,
	           fsf_req->status & ZFCP_STATUS_FSFREQ_COMPLETED);

4233
	return fsf_req;
L
Linus Torvalds 已提交
4234 4235 4236 4237

 free_fsf_req:
	zfcp_fsf_req_free(fsf_req);
 unlock_queue_lock:
S
Swen Schillig 已提交
4238
	write_unlock_irqrestore(&adapter->req_q.lock, lock_flags);
4239
	return ERR_PTR(retval);
L
Linus Torvalds 已提交
4240 4241
}

4242
static void zfcp_fsf_control_file_handler(struct zfcp_fsf_req *fsf_req)
L
Linus Torvalds 已提交
4243
{
4244
	if (fsf_req->qtcb->header.fsf_status != FSF_GOOD)
L
Linus Torvalds 已提交
4245 4246 4247 4248 4249 4250 4251
		fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
}

static inline int
zfcp_fsf_req_sbal_check(unsigned long *flags,
			struct zfcp_qdio_queue *queue, int needed)
{
S
Swen Schillig 已提交
4252 4253
	write_lock_irqsave(&queue->lock, *flags);
	if (likely(atomic_read(&queue->count) >= needed))
L
Linus Torvalds 已提交
4254
		return 1;
S
Swen Schillig 已提交
4255
	write_unlock_irqrestore(&queue->lock, *flags);
L
Linus Torvalds 已提交
4256 4257 4258 4259 4260 4261
	return 0;
}

/*
 * set qtcb pointer in fsf_req and initialize QTCB
 */
4262
static void
4263
zfcp_fsf_req_qtcb_init(struct zfcp_fsf_req *fsf_req)
L
Linus Torvalds 已提交
4264 4265
{
	if (likely(fsf_req->qtcb != NULL)) {
4266 4267 4268
		fsf_req->qtcb->prefix.req_seq_no =
			fsf_req->adapter->fsf_req_seq_no;
		fsf_req->qtcb->prefix.req_id = fsf_req->req_id;
L
Linus Torvalds 已提交
4269
		fsf_req->qtcb->prefix.ulp_info = ZFCP_ULP_INFO_VERSION;
4270 4271
		fsf_req->qtcb->prefix.qtcb_type =
			fsf_qtcb_type[fsf_req->fsf_command];
L
Linus Torvalds 已提交
4272
		fsf_req->qtcb->prefix.qtcb_version = ZFCP_QTCB_VERSION;
4273
		fsf_req->qtcb->header.req_handle = fsf_req->req_id;
4274
		fsf_req->qtcb->header.fsf_command = fsf_req->fsf_command;
L
Linus Torvalds 已提交
4275 4276 4277 4278 4279 4280 4281 4282 4283
	}
}

/**
 * zfcp_fsf_req_sbal_get - try to get one SBAL in the request queue
 * @adapter: adapter for which request queue is examined
 * @req_flags: flags indicating whether to wait for needed SBAL or not
 * @lock_flags: lock_flags if queue_lock is taken
 * Return: 0 on success, otherwise -EIO, or -ERESTARTSYS
S
Swen Schillig 已提交
4284
 * Locks: lock adapter->req_q->lock on success
L
Linus Torvalds 已提交
4285 4286 4287 4288 4289 4290
 */
static int
zfcp_fsf_req_sbal_get(struct zfcp_adapter *adapter, int req_flags,
		      unsigned long *lock_flags)
{
        long ret;
S
Swen Schillig 已提交
4291
	struct zfcp_qdio_queue *req_q = &adapter->req_q;
L
Linus Torvalds 已提交
4292 4293 4294

        if (unlikely(req_flags & ZFCP_WAIT_FOR_SBAL)) {
                ret = wait_event_interruptible_timeout(adapter->request_wq,
S
Swen Schillig 已提交
4295
			zfcp_fsf_req_sbal_check(lock_flags, req_q, 1),
L
Linus Torvalds 已提交
4296 4297 4298 4299 4300
						       ZFCP_SBAL_TIMEOUT);
		if (ret < 0)
			return ret;
		if (!ret)
			return -EIO;
S
Swen Schillig 已提交
4301
	} else if (!zfcp_fsf_req_sbal_check(lock_flags, req_q, 1))
L
Linus Torvalds 已提交
4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330
                return -EIO;

        return 0;
}

/*
 * function:    zfcp_fsf_req_create
 *
 * purpose:	create an FSF request at the specified adapter and
 *		setup common fields
 *
 * returns:	-ENOMEM if there was insufficient memory for a request
 *              -EIO if no qdio buffers could be allocate to the request
 *              -EINVAL/-EPERM on bug conditions in req_dequeue
 *              0 in success
 *
 * note:        The created request is returned by reference.
 *
 * locks:	lock of concerned request queue must not be held,
 *		but is held on completion (write, irqsave)
 */
int
zfcp_fsf_req_create(struct zfcp_adapter *adapter, u32 fsf_cmd, int req_flags,
		    mempool_t *pool, unsigned long *lock_flags,
		    struct zfcp_fsf_req **fsf_req_p)
{
	volatile struct qdio_buffer_element *sbale;
	struct zfcp_fsf_req *fsf_req = NULL;
	int ret = 0;
S
Swen Schillig 已提交
4331
	struct zfcp_qdio_queue *req_q = &adapter->req_q;
L
Linus Torvalds 已提交
4332 4333 4334 4335

	/* allocate new FSF request */
	fsf_req = zfcp_fsf_req_alloc(pool, req_flags);
	if (unlikely(NULL == fsf_req)) {
4336
		ZFCP_LOG_DEBUG("error: Could not put an FSF request into "
L
Linus Torvalds 已提交
4337 4338 4339 4340 4341
			       "the outbound (send) queue.\n");
		ret = -ENOMEM;
		goto failed_fsf_req;
	}

4342 4343
	fsf_req->adapter = adapter;
	fsf_req->fsf_command = fsf_cmd;
4344
	INIT_LIST_HEAD(&fsf_req->list);
4345
	init_timer(&fsf_req->timer);
L
Linus Torvalds 已提交
4346

S
Swen Schillig 已提交
4347
	/* initialize waitqueue which may be used to wait on
L
Linus Torvalds 已提交
4348 4349 4350 4351
	   this request completion */
	init_waitqueue_head(&fsf_req->completion_wq);

        ret = zfcp_fsf_req_sbal_get(adapter, req_flags, lock_flags);
4352
        if (ret < 0)
L
Linus Torvalds 已提交
4353
                goto failed_sbals;
4354 4355 4356 4357 4358 4359 4360

	/* this is serialized (we are holding req_queue-lock of adapter) */
	if (adapter->req_no == 0)
		adapter->req_no++;
	fsf_req->req_id = adapter->req_no++;

	zfcp_fsf_req_qtcb_init(fsf_req);
L
Linus Torvalds 已提交
4361 4362 4363 4364 4365 4366 4367

	/*
	 * We hold queue_lock here. Check if QDIOUP is set and let request fail
	 * if it is not set (see also *_open_qdio and *_close_qdio).
	 */

	if (!atomic_test_mask(ZFCP_STATUS_ADAPTER_QDIOUP, &adapter->status)) {
S
Swen Schillig 已提交
4368
		write_unlock_irqrestore(&req_q->lock, *lock_flags);
L
Linus Torvalds 已提交
4369 4370 4371 4372
		ret = -EIO;
		goto failed_sbals;
	}

4373 4374 4375 4376
	if (fsf_req->qtcb) {
		fsf_req->seq_no = adapter->fsf_req_seq_no;
		fsf_req->qtcb->prefix.req_seq_no = adapter->fsf_req_seq_no;
	}
L
Linus Torvalds 已提交
4377
	fsf_req->sbal_number = 1;
S
Swen Schillig 已提交
4378 4379
	fsf_req->sbal_first = req_q->first;
	fsf_req->sbal_last = req_q->first;
L
Linus Torvalds 已提交
4380 4381 4382 4383 4384 4385
        fsf_req->sbale_curr = 1;

	if (likely(req_flags & ZFCP_REQ_AUTO_CLEANUP)) {
		fsf_req->status |= ZFCP_STATUS_FSFREQ_CLEANUP;
	}

S
Swen Schillig 已提交
4386
	sbale = zfcp_qdio_sbale_req(fsf_req);
L
Linus Torvalds 已提交
4387 4388

	/* setup common SBALE fields */
4389
	sbale[0].addr = (void *) fsf_req->req_id;
L
Linus Torvalds 已提交
4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406
	sbale[0].flags |= SBAL_FLAGS0_COMMAND;
	if (likely(fsf_req->qtcb != NULL)) {
		sbale[1].addr = (void *) fsf_req->qtcb;
		sbale[1].length = sizeof(struct fsf_qtcb);
	}

	ZFCP_LOG_TRACE("got %i free BUFFERs starting at index %i\n",
                       fsf_req->sbal_number, fsf_req->sbal_first);

	goto success;

 failed_sbals:
/* dequeue new FSF request previously enqueued */
	zfcp_fsf_req_free(fsf_req);
	fsf_req = NULL;

 failed_fsf_req:
S
Swen Schillig 已提交
4407
	write_lock_irqsave(&req_q->lock, *lock_flags);
L
Linus Torvalds 已提交
4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420
 success:
	*fsf_req_p = fsf_req;
	return ret;
}

/*
 * function:    zfcp_fsf_req_send
 *
 * purpose:	start transfer of FSF request via QDIO
 *
 * returns:	0 - request transfer succesfully started
 *		!0 - start of request transfer failed
 */
4421
static int zfcp_fsf_req_send(struct zfcp_fsf_req *fsf_req)
L
Linus Torvalds 已提交
4422 4423
{
	struct zfcp_adapter *adapter;
S
Swen Schillig 已提交
4424
	struct zfcp_qdio_queue *req_q;
L
Linus Torvalds 已提交
4425
	volatile struct qdio_buffer_element *sbale;
4426
	int inc_seq_no;
L
Linus Torvalds 已提交
4427 4428 4429
	int retval = 0;

	adapter = fsf_req->adapter;
S
Swen Schillig 已提交
4430
	req_q = &adapter->req_q;
L
Linus Torvalds 已提交
4431 4432 4433


	/* FIXME(debug): remove it later */
S
Swen Schillig 已提交
4434
	sbale = zfcp_qdio_sbale_req(fsf_req);
L
Linus Torvalds 已提交
4435 4436 4437 4438 4439
	ZFCP_LOG_DEBUG("SBALE0 flags=0x%x\n", sbale[0].flags);
	ZFCP_LOG_TRACE("HEX DUMP OF SBALE1 PAYLOAD:\n");
	ZFCP_HEX_DUMP(ZFCP_LOG_LEVEL_TRACE, (char *) sbale[1].addr,
		      sbale[1].length);

4440 4441 4442 4443
	/* put allocated FSF request into hash table */
	spin_lock(&adapter->req_list_lock);
	zfcp_reqlist_add(adapter, fsf_req);
	spin_unlock(&adapter->req_list_lock);
L
Linus Torvalds 已提交
4444

4445 4446 4447 4448
	inc_seq_no = (fsf_req->qtcb != NULL);

	fsf_req->issued = get_clock();

S
Swen Schillig 已提交
4449
	retval = zfcp_qdio_send(fsf_req);
L
Linus Torvalds 已提交
4450 4451 4452

	if (unlikely(retval)) {
		/* Queues are down..... */
4453
		del_timer(&fsf_req->timer);
4454
		spin_lock(&adapter->req_list_lock);
4455
		zfcp_reqlist_remove(adapter, fsf_req);
4456 4457
		spin_unlock(&adapter->req_list_lock);
		/* undo changes in request queue made for this request */
S
Swen Schillig 已提交
4458 4459 4460 4461
		atomic_add(fsf_req->sbal_number, &req_q->count);
		req_q->first -= fsf_req->sbal_number;
		req_q->first += QDIO_MAX_BUFFERS_PER_Q;
		req_q->first %= QDIO_MAX_BUFFERS_PER_Q;
4462
		zfcp_erp_adapter_reopen(adapter, 0, 116, fsf_req);
S
Swen Schillig 已提交
4463
		retval = -EIO;
L
Linus Torvalds 已提交
4464 4465 4466 4467 4468 4469 4470 4471
	} else {
		/*
		 * increase FSF sequence counter -
		 * this must only be done for request successfully enqueued to
		 * QDIO this rejected requests may be cleaned up by calling
		 * routines  resulting in missing sequence counter values
		 * otherwise,
		 */
4472

L
Linus Torvalds 已提交
4473
		/* Don't increase for unsolicited status */
4474
		if (inc_seq_no)
L
Linus Torvalds 已提交
4475 4476 4477 4478 4479 4480
			adapter->fsf_req_seq_no++;
	}
	return retval;
}

#undef ZFCP_LOG_AREA