zfcp_fsf.c 71.7 KB
Newer Older
L
Linus Torvalds 已提交
1
/*
C
Christof Schmitt 已提交
2
 * zfcp device driver
L
Linus Torvalds 已提交
3
 *
C
Christof Schmitt 已提交
4
 * Implementation of FSF commands.
L
Linus Torvalds 已提交
5
 *
C
Christof Schmitt 已提交
6
 * Copyright IBM Corporation 2002, 2008
L
Linus Torvalds 已提交
7 8
 */

S
Stefan Raspl 已提交
9
#include <linux/blktrace_api.h>
L
Linus Torvalds 已提交
10 11
#include "zfcp_ext.h"

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
static void zfcp_fsf_request_timeout_handler(unsigned long data)
{
	struct zfcp_adapter *adapter = (struct zfcp_adapter *) data;
	zfcp_erp_adapter_reopen(adapter, ZFCP_STATUS_COMMON_ERP_FAILED, 62,
				NULL);
}

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

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

L
Linus Torvalds 已提交
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
/* association between FSF command and FSF QTCB type */
static u32 fsf_qtcb_type[] = {
	[FSF_QTCB_FCP_CMND] =             FSF_IO_COMMAND,
	[FSF_QTCB_ABORT_FCP_CMND] =       FSF_SUPPORT_COMMAND,
	[FSF_QTCB_OPEN_PORT_WITH_DID] =   FSF_SUPPORT_COMMAND,
	[FSF_QTCB_OPEN_LUN] =             FSF_SUPPORT_COMMAND,
	[FSF_QTCB_CLOSE_LUN] =            FSF_SUPPORT_COMMAND,
	[FSF_QTCB_CLOSE_PORT] =           FSF_SUPPORT_COMMAND,
	[FSF_QTCB_CLOSE_PHYSICAL_PORT] =  FSF_SUPPORT_COMMAND,
	[FSF_QTCB_SEND_ELS] =             FSF_SUPPORT_COMMAND,
	[FSF_QTCB_SEND_GENERIC] =         FSF_SUPPORT_COMMAND,
	[FSF_QTCB_EXCHANGE_CONFIG_DATA] = FSF_CONFIG_COMMAND,
	[FSF_QTCB_EXCHANGE_PORT_DATA] =   FSF_PORT_COMMAND,
	[FSF_QTCB_DOWNLOAD_CONTROL_FILE] = FSF_SUPPORT_COMMAND,
	[FSF_QTCB_UPLOAD_CONTROL_FILE] =  FSF_SUPPORT_COMMAND
};

C
Christof Schmitt 已提交
54 55
static void zfcp_act_eval_err(struct zfcp_adapter *adapter, u32 table)
{
S
Swen Schillig 已提交
56
	u16 subtable = table >> 16;
C
Christof Schmitt 已提交
57
	u16 rule = table & 0xffff;
58
	const char *act_type[] = { "unknown", "OS", "WWPN", "DID", "LUN" };
C
Christof Schmitt 已提交
59

60
	if (subtable && subtable < ARRAY_SIZE(act_type))
C
Christof Schmitt 已提交
61
		dev_warn(&adapter->ccw_device->dev,
62 63
			 "Access denied according to ACT rule type %s, "
			 "rule %d\n", act_type[subtable], rule);
C
Christof Schmitt 已提交
64 65 66 67 68 69 70
}

static void zfcp_fsf_access_denied_port(struct zfcp_fsf_req *req,
					struct zfcp_port *port)
{
	struct fsf_qtcb_header *header = &req->qtcb->header;
	dev_warn(&req->adapter->ccw_device->dev,
71
		 "Access denied to port 0x%016Lx\n",
72
		 (unsigned long long)port->wwpn);
C
Christof Schmitt 已提交
73 74 75 76 77 78 79 80 81 82 83
	zfcp_act_eval_err(req->adapter, header->fsf_status_qual.halfword[0]);
	zfcp_act_eval_err(req->adapter, header->fsf_status_qual.halfword[1]);
	zfcp_erp_port_access_denied(port, 55, req);
	req->status |= ZFCP_STATUS_FSFREQ_ERROR;
}

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

static void zfcp_fsf_class_not_supp(struct zfcp_fsf_req *req)
{
95 96
	dev_err(&req->adapter->ccw_device->dev, "FCP device not "
		"operational because of an unsupported FC class\n");
C
Christof Schmitt 已提交
97 98 99 100
	zfcp_erp_adapter_shutdown(req->adapter, 0, 123, req);
	req->status |= ZFCP_STATUS_FSFREQ_ERROR;
}

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

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

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

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

S
Swen Schillig 已提交
140 141 142 143
	list_for_each_entry_safe(req, tmp, &remove_queue, list) {
		list_del(&req->list);
		req->status |= ZFCP_STATUS_FSFREQ_DISMISSED;
		zfcp_fsf_req_complete(req);
L
Linus Torvalds 已提交
144 145 146
	}
}

S
Swen Schillig 已提交
147
static void zfcp_fsf_status_read_port_closed(struct zfcp_fsf_req *req)
L
Linus Torvalds 已提交
148
{
S
Swen Schillig 已提交
149 150 151 152 153
	struct fsf_status_read_buffer *sr_buf = req->data;
	struct zfcp_adapter *adapter = req->adapter;
	struct zfcp_port *port;
	int d_id = sr_buf->d_id & ZFCP_DID_MASK;
	unsigned long flags;
L
Linus Torvalds 已提交
154

S
Swen Schillig 已提交
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
	read_lock_irqsave(&zfcp_data.config_lock, flags);
	list_for_each_entry(port, &adapter->port_list_head, list)
		if (port->d_id == d_id) {
			read_unlock_irqrestore(&zfcp_data.config_lock, flags);
			switch (sr_buf->status_subtype) {
			case FSF_STATUS_READ_SUB_CLOSE_PHYS_PORT:
				zfcp_erp_port_reopen(port, 0, 101, req);
				break;
			case FSF_STATUS_READ_SUB_ERROR_PORT:
				zfcp_erp_port_shutdown(port, 0, 122, req);
				break;
			}
			return;
		}
	read_unlock_irqrestore(&zfcp_data.config_lock, flags);
L
Linus Torvalds 已提交
170 171
}

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

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

	atomic_set_mask(ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED, &adapter->status);

S
Swen Schillig 已提交
182
	if (!link_down)
183
		goto out;
184

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

S
Swen Schillig 已提交
253
static void zfcp_fsf_status_read_link_down(struct zfcp_fsf_req *req)
L
Linus Torvalds 已提交
254
{
S
Swen Schillig 已提交
255 256 257
	struct fsf_status_read_buffer *sr_buf = req->data;
	struct fsf_link_down_info *ldi =
		(struct fsf_link_down_info *) &sr_buf->payload;
L
Linus Torvalds 已提交
258

S
Swen Schillig 已提交
259 260 261
	switch (sr_buf->status_subtype) {
	case FSF_STATUS_READ_SUB_NO_PHYSICAL_LINK:
		zfcp_fsf_link_down_info_eval(req, 38, ldi);
L
Linus Torvalds 已提交
262
		break;
S
Swen Schillig 已提交
263 264
	case FSF_STATUS_READ_SUB_FDISC_FAILED:
		zfcp_fsf_link_down_info_eval(req, 39, ldi);
L
Linus Torvalds 已提交
265
		break;
S
Swen Schillig 已提交
266 267 268 269
	case FSF_STATUS_READ_SUB_FIRMWARE_UPDATE:
		zfcp_fsf_link_down_info_eval(req, 40, NULL);
	};
}
L
Linus Torvalds 已提交
270

S
Swen Schillig 已提交
271 272 273 274
static void zfcp_fsf_status_read_handler(struct zfcp_fsf_req *req)
{
	struct zfcp_adapter *adapter = req->adapter;
	struct fsf_status_read_buffer *sr_buf = req->data;
L
Linus Torvalds 已提交
275

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

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

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

S
Swen Schillig 已提交
329 330
	mempool_free(sr_buf, adapter->pool.data_status_read);
	zfcp_fsf_req_free(req);
L
Linus Torvalds 已提交
331

S
Swen Schillig 已提交
332
	atomic_inc(&adapter->stat_miss);
333
	queue_work(zfcp_data.work_queue, &adapter->stat_work);
S
Swen Schillig 已提交
334
}
L
Linus Torvalds 已提交
335

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

S
Swen Schillig 已提交
358
static void zfcp_fsf_fsfstatus_eval(struct zfcp_fsf_req *req)
L
Linus Torvalds 已提交
359
{
S
Swen Schillig 已提交
360 361
	if (unlikely(req->status & ZFCP_STATUS_FSFREQ_ERROR))
		return;
L
Linus Torvalds 已提交
362

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

S
Swen Schillig 已提交
377 378 379 380 381
static void zfcp_fsf_protstatus_eval(struct zfcp_fsf_req *req)
{
	struct zfcp_adapter *adapter = req->adapter;
	struct fsf_qtcb *qtcb = req->qtcb;
	union fsf_prot_status_qual *psq = &qtcb->prefix.prot_status_qual;
L
Linus Torvalds 已提交
382

S
Swen Schillig 已提交
383
	zfcp_hba_dbf_event_fsf_response(req);
L
Linus Torvalds 已提交
384

S
Swen Schillig 已提交
385 386 387 388 389
	if (req->status & ZFCP_STATUS_FSFREQ_DISMISSED) {
		req->status |= ZFCP_STATUS_FSFREQ_ERROR |
			ZFCP_STATUS_FSFREQ_RETRY; /* only for SCSI cmnds. */
		return;
	}
L
Linus Torvalds 已提交
390

S
Swen Schillig 已提交
391 392 393 394 395 396
	switch (qtcb->prefix.prot_status) {
	case FSF_PROT_GOOD:
	case FSF_PROT_FSF_STATUS_PRESENTED:
		return;
	case FSF_PROT_QTCB_VERSION_ERROR:
		dev_err(&adapter->ccw_device->dev,
397 398 399
			"QTCB version 0x%x not supported by FCP adapter "
			"(0x%x to 0x%x)\n", FSF_QTCB_CURRENT_VERSION,
			psq->word[0], psq->word[1]);
S
Swen Schillig 已提交
400 401 402 403 404 405 406 407 408
		zfcp_erp_adapter_shutdown(adapter, 0, 117, req);
		break;
	case FSF_PROT_ERROR_STATE:
	case FSF_PROT_SEQ_NUMB_ERROR:
		zfcp_erp_adapter_reopen(adapter, 0, 98, req);
		req->status |= ZFCP_STATUS_FSFREQ_RETRY;
		break;
	case FSF_PROT_UNSUPP_QTCB_TYPE:
		dev_err(&adapter->ccw_device->dev,
409
			"The QTCB type is not supported by the FCP adapter\n");
S
Swen Schillig 已提交
410 411 412 413 414 415 416 417
		zfcp_erp_adapter_shutdown(adapter, 0, 118, req);
		break;
	case FSF_PROT_HOST_CONNECTION_INITIALIZING:
		atomic_set_mask(ZFCP_STATUS_ADAPTER_HOST_CON_INIT,
				&adapter->status);
		break;
	case FSF_PROT_DUPLICATE_REQUEST_ID:
		dev_err(&adapter->ccw_device->dev,
418
			"0x%Lx is an ambiguous request identifier\n",
S
Swen Schillig 已提交
419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437
			(unsigned long long)qtcb->bottom.support.req_handle);
		zfcp_erp_adapter_shutdown(adapter, 0, 78, req);
		break;
	case FSF_PROT_LINK_DOWN:
		zfcp_fsf_link_down_info_eval(req, 37, &psq->link_down_info);
		/* FIXME: reopening adapter now? better wait for link up */
		zfcp_erp_adapter_reopen(adapter, 0, 79, req);
		break;
	case FSF_PROT_REEST_QUEUE:
		/* All ports should be marked as ready to run again */
		zfcp_erp_modify_adapter_status(adapter, 28, NULL,
					       ZFCP_STATUS_COMMON_RUNNING,
					       ZFCP_SET);
		zfcp_erp_adapter_reopen(adapter,
					ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED |
					ZFCP_STATUS_COMMON_ERP_FAILED, 99, req);
		break;
	default:
		dev_err(&adapter->ccw_device->dev,
438
			"0x%x is not a valid transfer protocol status\n",
S
Swen Schillig 已提交
439 440 441 442
			qtcb->prefix.prot_status);
		zfcp_erp_adapter_shutdown(adapter, 0, 119, req);
	}
	req->status |= ZFCP_STATUS_FSFREQ_ERROR;
L
Linus Torvalds 已提交
443 444
}

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

S
Swen Schillig 已提交
461 462 463 464
	del_timer(&req->timer);
	zfcp_fsf_protstatus_eval(req);
	zfcp_fsf_fsfstatus_eval(req);
	req->handler(req);
L
Linus Torvalds 已提交
465

S
Swen Schillig 已提交
466
	if (req->erp_action)
467
		zfcp_erp_notify(req->erp_action, 0);
S
Swen Schillig 已提交
468
	req->status |= ZFCP_STATUS_FSFREQ_COMPLETED;
L
Linus Torvalds 已提交
469

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

S
Swen Schillig 已提交
487 488 489 490 491
static int zfcp_fsf_exchange_config_evaluate(struct zfcp_fsf_req *req)
{
	struct fsf_qtcb_bottom_config *bottom;
	struct zfcp_adapter *adapter = req->adapter;
	struct Scsi_Host *shost = adapter->scsi_host;
L
Linus Torvalds 已提交
492

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

S
Swen Schillig 已提交
495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523
	if (req->data)
		memcpy(req->data, bottom, sizeof(*bottom));

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

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

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

	switch (bottom->fc_topology) {
	case FSF_TOPO_P2P:
		adapter->peer_d_id = bottom->peer_d_id & ZFCP_DID_MASK;
		adapter->peer_wwpn = bottom->plogi_payload.wwpn;
		adapter->peer_wwnn = bottom->plogi_payload.wwnn;
		fc_host_port_type(shost) = FC_PORTTYPE_PTP;
		break;
	case FSF_TOPO_FABRIC:
		fc_host_port_type(shost) = FC_PORTTYPE_NPORT;
		break;
	case FSF_TOPO_AL:
		fc_host_port_type(shost) = FC_PORTTYPE_NLPORT;
	default:
		dev_err(&adapter->ccw_device->dev,
524 525 526
			"Unknown or unsupported arbitrated loop "
			"fibre channel topology detected\n");
		zfcp_erp_adapter_shutdown(adapter, 0, 127, req);
S
Swen Schillig 已提交
527
		return -EIO;
L
Linus Torvalds 已提交
528
	}
S
Swen Schillig 已提交
529

L
Linus Torvalds 已提交
530 531 532
	return 0;
}

S
Swen Schillig 已提交
533
static void zfcp_fsf_exchange_config_data_handler(struct zfcp_fsf_req *req)
C
Christof Schmitt 已提交
534 535
{
	struct zfcp_adapter *adapter = req->adapter;
S
Swen Schillig 已提交
536 537 538
	struct fsf_qtcb *qtcb = req->qtcb;
	struct fsf_qtcb_bottom_config *bottom = &qtcb->bottom.config;
	struct Scsi_Host *shost = adapter->scsi_host;
C
Christof Schmitt 已提交
539

S
Swen Schillig 已提交
540 541
	if (req->status & ZFCP_STATUS_FSFREQ_ERROR)
		return;
L
Linus Torvalds 已提交
542

S
Swen Schillig 已提交
543 544 545 546 547 548
	adapter->fsf_lic_version = bottom->lic_version;
	adapter->adapter_features = bottom->adapter_features;
	adapter->connection_features = bottom->connection_features;
	adapter->peer_wwpn = 0;
	adapter->peer_wwnn = 0;
	adapter->peer_d_id = 0;
549

S
Swen Schillig 已提交
550 551 552 553
	switch (qtcb->header.fsf_status) {
	case FSF_GOOD:
		if (zfcp_fsf_exchange_config_evaluate(req))
			return;
L
Linus Torvalds 已提交
554

S
Swen Schillig 已提交
555 556
		if (bottom->max_qtcb_size < sizeof(struct fsf_qtcb)) {
			dev_err(&adapter->ccw_device->dev,
557 558 559
				"FCP adapter maximum QTCB size (%d bytes) "
				"is too small\n",
				bottom->max_qtcb_size);
S
Swen Schillig 已提交
560 561 562 563 564
			zfcp_erp_adapter_shutdown(adapter, 0, 129, req);
			return;
		}
		atomic_set_mask(ZFCP_STATUS_ADAPTER_XCONFIG_OK,
				&adapter->status);
L
Linus Torvalds 已提交
565
		break;
S
Swen Schillig 已提交
566 567 568 569 570 571 572
	case FSF_EXCHANGE_CONFIG_DATA_INCOMPLETE:
		fc_host_node_name(shost) = 0;
		fc_host_port_name(shost) = 0;
		fc_host_port_id(shost) = 0;
		fc_host_speed(shost) = FC_PORTSPEED_UNKNOWN;
		fc_host_port_type(shost) = FC_PORTTYPE_UNKNOWN;
		adapter->hydra_version = 0;
L
Linus Torvalds 已提交
573

S
Swen Schillig 已提交
574 575
		atomic_set_mask(ZFCP_STATUS_ADAPTER_XCONFIG_OK,
				&adapter->status);
L
Linus Torvalds 已提交
576

S
Swen Schillig 已提交
577 578
		zfcp_fsf_link_down_info_eval(req, 42,
			&qtcb->header.fsf_status_qual.link_down_info);
L
Linus Torvalds 已提交
579
		break;
S
Swen Schillig 已提交
580 581 582 583
	default:
		zfcp_erp_adapter_shutdown(adapter, 0, 130, req);
		return;
	}
L
Linus Torvalds 已提交
584

S
Swen Schillig 已提交
585 586 587 588 589 590 591
	if (adapter->adapter_features & FSF_FEATURE_HBAAPI_MANAGEMENT) {
		adapter->hardware_version = bottom->hardware_version;
		memcpy(fc_host_serial_number(shost), bottom->serial_number,
		       min(FC_SERIAL_NUMBER_SIZE, 17));
		EBCASC(fc_host_serial_number(shost),
		       min(FC_SERIAL_NUMBER_SIZE, 17));
	}
L
Linus Torvalds 已提交
592

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

S
Swen Schillig 已提交
608 609 610 611 612
static void zfcp_fsf_exchange_port_evaluate(struct zfcp_fsf_req *req)
{
	struct zfcp_adapter *adapter = req->adapter;
	struct fsf_qtcb_bottom_port *bottom = &req->qtcb->bottom.port;
	struct Scsi_Host *shost = adapter->scsi_host;
L
Linus Torvalds 已提交
613

S
Swen Schillig 已提交
614 615
	if (req->data)
		memcpy(req->data, bottom, sizeof(*bottom));
616

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

S
Swen Schillig 已提交
625 626 627 628 629 630 631 632 633 634 635 636 637 638 639
static void zfcp_fsf_exchange_port_data_handler(struct zfcp_fsf_req *req)
{
	struct fsf_qtcb *qtcb = req->qtcb;

	if (req->status & ZFCP_STATUS_FSFREQ_ERROR)
		return;

	switch (qtcb->header.fsf_status) {
	case FSF_GOOD:
		zfcp_fsf_exchange_port_evaluate(req);
		break;
	case FSF_EXCHANGE_CONFIG_DATA_INCOMPLETE:
		zfcp_fsf_exchange_port_evaluate(req);
		zfcp_fsf_link_down_info_eval(req, 43,
			&qtcb->header.fsf_status_qual.link_down_info);
640
		break;
L
Linus Torvalds 已提交
641
	}
S
Swen Schillig 已提交
642
}
643

644
static int zfcp_fsf_sbal_check(struct zfcp_adapter *adapter)
S
Swen Schillig 已提交
645
{
646 647 648 649
	struct zfcp_qdio_queue *req_q = &adapter->req_q;

	spin_lock_bh(&adapter->req_q_lock);
	if (atomic_read(&req_q->count))
S
Swen Schillig 已提交
650
		return 1;
651
	spin_unlock_bh(&adapter->req_q_lock);
S
Swen Schillig 已提交
652
	return 0;
L
Linus Torvalds 已提交
653 654
}

655 656 657 658 659 660 661 662
static int zfcp_fsf_sbal_available(struct zfcp_adapter *adapter)
{
	unsigned int count = atomic_read(&adapter->req_q.count);
	if (!count)
		atomic_inc(&adapter->qdio_outb_full);
	return count > 0;
}

S
Swen Schillig 已提交
663 664 665 666
static int zfcp_fsf_req_sbal_get(struct zfcp_adapter *adapter)
{
	long ret;

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

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

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

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

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

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

	return &qtcb->fsf_req;
}

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

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

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

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

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

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

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

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

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

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

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

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

S
Swen Schillig 已提交
770 771 772 773
static int zfcp_fsf_req_send(struct zfcp_fsf_req *req)
{
	struct zfcp_adapter *adapter = req->adapter;
	struct zfcp_qdio_queue *req_q = &adapter->req_q;
774
	unsigned long flags;
S
Swen Schillig 已提交
775 776 777
	int idx;

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

S
Stefan Raspl 已提交
783
	req->qdio_outb_usage = atomic_read(&req_q->count);
S
Swen Schillig 已提交
784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811
	req->issued = get_clock();
	if (zfcp_qdio_send(req)) {
		/* Queues are down..... */
		del_timer(&req->timer);
		spin_lock(&adapter->req_list_lock);
		zfcp_reqlist_remove(adapter, req);
		spin_unlock(&adapter->req_list_lock);
		/* undo changes in request queue made for this request */
		atomic_add(req->sbal_number, &req_q->count);
		req_q->first -= req->sbal_number;
		req_q->first += QDIO_MAX_BUFFERS_PER_Q;
		req_q->first %= QDIO_MAX_BUFFERS_PER_Q; /* wrap */
		zfcp_erp_adapter_reopen(adapter, 0, 116, req);
		return -EIO;
	}

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

	return 0;
}

/**
 * zfcp_fsf_status_read - send status read request
 * @adapter: pointer to struct zfcp_adapter
 * @req_flags: request flags
 * Returns: 0 on success, ERROR otherwise
L
Linus Torvalds 已提交
812
 */
S
Swen Schillig 已提交
813
int zfcp_fsf_status_read(struct zfcp_adapter *adapter)
L
Linus Torvalds 已提交
814
{
S
Swen Schillig 已提交
815 816
	struct zfcp_fsf_req *req;
	struct fsf_status_read_buffer *sr_buf;
817
	struct qdio_buffer_element *sbale;
S
Swen Schillig 已提交
818
	int retval = -EIO;
L
Linus Torvalds 已提交
819

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

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

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

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

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

S
Swen Schillig 已提交
852 853 854 855 856 857 858 859
	goto out;

failed_req_send:
	mempool_free(sr_buf, adapter->pool.data_status_read);
failed_buf:
	zfcp_fsf_req_free(req);
	zfcp_hba_dbf_event_fsf_unsol("fail", adapter, NULL);
out:
860
	spin_unlock_bh(&adapter->req_q_lock);
S
Swen Schillig 已提交
861 862 863 864 865 866 867 868 869 870 871 872
	return retval;
}

static void zfcp_fsf_abort_fcp_command_handler(struct zfcp_fsf_req *req)
{
	struct zfcp_unit *unit = req->data;
	union fsf_status_qual *fsq = &req->qtcb->header.fsf_status_qual;

	if (req->status & ZFCP_STATUS_FSFREQ_ERROR)
		return;

	switch (req->qtcb->header.fsf_status) {
L
Linus Torvalds 已提交
873
	case FSF_PORT_HANDLE_NOT_VALID:
S
Swen Schillig 已提交
874
		if (fsq->word[0] == fsq->word[1]) {
875
			zfcp_erp_adapter_reopen(unit->port->adapter, 0, 104,
S
Swen Schillig 已提交
876 877
						req);
			req->status |= ZFCP_STATUS_FSFREQ_ERROR;
L
Linus Torvalds 已提交
878 879 880
		}
		break;
	case FSF_LUN_HANDLE_NOT_VALID:
S
Swen Schillig 已提交
881 882 883
		if (fsq->word[0] == fsq->word[1]) {
			zfcp_erp_port_reopen(unit->port, 0, 105, req);
			req->status |= ZFCP_STATUS_FSFREQ_ERROR;
L
Linus Torvalds 已提交
884 885 886
		}
		break;
	case FSF_FCP_COMMAND_DOES_NOT_EXIST:
S
Swen Schillig 已提交
887
		req->status |= ZFCP_STATUS_FSFREQ_ABORTNOTNEEDED;
L
Linus Torvalds 已提交
888 889
		break;
	case FSF_PORT_BOXED:
S
Swen Schillig 已提交
890 891 892
		zfcp_erp_port_boxed(unit->port, 47, req);
		req->status |= ZFCP_STATUS_FSFREQ_ERROR |
			       ZFCP_STATUS_FSFREQ_RETRY;
L
Linus Torvalds 已提交
893 894
		break;
	case FSF_LUN_BOXED:
S
Swen Schillig 已提交
895 896 897
		zfcp_erp_unit_boxed(unit, 48, req);
		req->status |= ZFCP_STATUS_FSFREQ_ERROR |
			       ZFCP_STATUS_FSFREQ_RETRY;
L
Linus Torvalds 已提交
898 899
                break;
	case FSF_ADAPTER_STATUS_AVAILABLE:
S
Swen Schillig 已提交
900
		switch (fsq->word[0]) {
L
Linus Torvalds 已提交
901
		case FSF_SQ_INVOKE_LINK_TEST_PROCEDURE:
902
			zfcp_test_link(unit->port);
L
Linus Torvalds 已提交
903
		case FSF_SQ_ULP_DEPENDENT_ERP_REQUIRED:
S
Swen Schillig 已提交
904
			req->status |= ZFCP_STATUS_FSFREQ_ERROR;
L
Linus Torvalds 已提交
905 906 907 908
			break;
		}
		break;
	case FSF_GOOD:
S
Swen Schillig 已提交
909
		req->status |= ZFCP_STATUS_FSFREQ_ABORTSUCCEEDED;
L
Linus Torvalds 已提交
910 911 912 913 914
		break;
	}
}

/**
S
Swen Schillig 已提交
915 916 917 918 919 920 921 922
 * zfcp_fsf_abort_fcp_command - abort running SCSI command
 * @old_req_id: unsigned long
 * @adapter: pointer to struct zfcp_adapter
 * @unit: pointer to struct zfcp_unit
 * @req_flags: integer specifying the request flags
 * Returns: pointer to struct zfcp_fsf_req
 *
 * FIXME(design): should be watched by a timeout !!!
L
Linus Torvalds 已提交
923 924
 */

S
Swen Schillig 已提交
925 926 927 928
struct zfcp_fsf_req *zfcp_fsf_abort_fcp_command(unsigned long old_req_id,
						struct zfcp_adapter *adapter,
						struct zfcp_unit *unit,
						int req_flags)
L
Linus Torvalds 已提交
929
{
930
	struct qdio_buffer_element *sbale;
S
Swen Schillig 已提交
931
	struct zfcp_fsf_req *req = NULL;
932

933
	spin_lock(&adapter->req_q_lock);
934
	if (!zfcp_fsf_sbal_available(adapter))
S
Swen Schillig 已提交
935 936 937
		goto out;
	req = zfcp_fsf_req_create(adapter, FSF_QTCB_ABORT_FCP_CMND,
				  req_flags, adapter->pool.fsf_req_abort);
938
	if (IS_ERR(req))
S
Swen Schillig 已提交
939
		goto out;
940

S
Swen Schillig 已提交
941 942 943
	if (unlikely(!(atomic_read(&unit->status) &
		       ZFCP_STATUS_COMMON_UNBLOCKED)))
		goto out_error_free;
L
Linus Torvalds 已提交
944

S
Swen Schillig 已提交
945 946 947
	sbale = zfcp_qdio_sbale_req(req);
	sbale[0].flags |= SBAL_FLAGS0_TYPE_READ;
	sbale[1].flags |= SBAL_FLAGS_LAST_ENTRY;
L
Linus Torvalds 已提交
948

S
Swen Schillig 已提交
949 950 951 952 953 954 955 956 957 958 959 960 961 962
	req->data = unit;
	req->handler = zfcp_fsf_abort_fcp_command_handler;
	req->qtcb->header.lun_handle = unit->handle;
	req->qtcb->header.port_handle = unit->port->handle;
	req->qtcb->bottom.support.req_handle = (u64) old_req_id;

	zfcp_fsf_start_timer(req, ZFCP_SCSI_ER_TIMEOUT);
	if (!zfcp_fsf_req_send(req))
		goto out;

out_error_free:
	zfcp_fsf_req_free(req);
	req = NULL;
out:
963
	spin_unlock(&adapter->req_q_lock);
S
Swen Schillig 已提交
964
	return req;
L
Linus Torvalds 已提交
965 966
}

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

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

S
Swen Schillig 已提交
975
	if (req->status & ZFCP_STATUS_FSFREQ_ERROR)
L
Linus Torvalds 已提交
976 977 978 979
		goto skip_fsfstatus;

	switch (header->fsf_status) {
        case FSF_GOOD:
S
Swen Schillig 已提交
980 981
		zfcp_san_dbf_event_ct_response(req);
		send_ct->status = 0;
L
Linus Torvalds 已提交
982 983
		break;
        case FSF_SERVICE_CLASS_NOT_SUPPORTED:
S
Swen Schillig 已提交
984
		zfcp_fsf_class_not_supp(req);
L
Linus Torvalds 已提交
985 986 987 988 989
		break;
        case FSF_ADAPTER_STATUS_AVAILABLE:
                switch (header->fsf_status_qual.word[0]){
                case FSF_SQ_INVOKE_LINK_TEST_PROCEDURE:
                case FSF_SQ_ULP_DEPENDENT_ERP_REQUIRED:
S
Swen Schillig 已提交
990
			req->status |= ZFCP_STATUS_FSFREQ_ERROR;
L
Linus Torvalds 已提交
991 992 993 994 995 996
			break;
                }
                break;
	case FSF_ACCESS_DENIED:
		break;
        case FSF_PORT_BOXED:
S
Swen Schillig 已提交
997 998
		req->status |= ZFCP_STATUS_FSFREQ_ERROR |
			       ZFCP_STATUS_FSFREQ_RETRY;
L
Linus Torvalds 已提交
999
		break;
S
Swen Schillig 已提交
1000 1001 1002
	case FSF_PORT_HANDLE_NOT_VALID:
		zfcp_erp_adapter_reopen(adapter, 0, 106, req);
	case FSF_GENERIC_COMMAND_REJECTED:
L
Linus Torvalds 已提交
1003 1004 1005 1006
	case FSF_PAYLOAD_SIZE_MISMATCH:
	case FSF_REQUEST_SIZE_TOO_LARGE:
	case FSF_RESPONSE_SIZE_TOO_LARGE:
	case FSF_SBAL_MISMATCH:
S
Swen Schillig 已提交
1007
		req->status |= ZFCP_STATUS_FSFREQ_ERROR;
L
Linus Torvalds 已提交
1008 1009 1010 1011
		break;
	}

skip_fsfstatus:
S
Swen Schillig 已提交
1012
	if (send_ct->handler)
L
Linus Torvalds 已提交
1013
		send_ct->handler(send_ct->handler_data);
S
Swen Schillig 已提交
1014
}
L
Linus Torvalds 已提交
1015

S
Swen Schillig 已提交
1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035
static int zfcp_fsf_setup_sbals(struct zfcp_fsf_req *req,
				struct scatterlist *sg_req,
				struct scatterlist *sg_resp, int max_sbals)
{
	int bytes;

	bytes = zfcp_qdio_sbals_from_sg(req, SBAL_FLAGS0_TYPE_WRITE_READ,
					sg_req, max_sbals);
	if (bytes <= 0)
		return -ENOMEM;
	req->qtcb->bottom.support.req_buf_length = bytes;
	req->sbale_curr = ZFCP_LAST_SBALE_PER_SBAL;

	bytes = zfcp_qdio_sbals_from_sg(req, SBAL_FLAGS0_TYPE_WRITE_READ,
					sg_resp, max_sbals);
	if (bytes <= 0)
		return -ENOMEM;
	req->qtcb->bottom.support.resp_buf_length = bytes;

	return 0;
L
Linus Torvalds 已提交
1036 1037 1038
}

/**
S
Swen Schillig 已提交
1039 1040 1041 1042
 * zfcp_fsf_send_ct - initiate a Generic Service request (FC-GS)
 * @ct: pointer to struct zfcp_send_ct with data for request
 * @pool: if non-null this mempool is used to allocate struct zfcp_fsf_req
 * @erp_action: if non-null the Generic Service request sent within ERP
L
Linus Torvalds 已提交
1043
 */
S
Swen Schillig 已提交
1044 1045
int zfcp_fsf_send_ct(struct zfcp_send_ct *ct, mempool_t *pool,
		     struct zfcp_erp_action *erp_action)
L
Linus Torvalds 已提交
1046
{
1047 1048
	struct zfcp_wka_port *wka_port = ct->wka_port;
	struct zfcp_adapter *adapter = wka_port->adapter;
S
Swen Schillig 已提交
1049 1050
	struct zfcp_fsf_req *req;
	int ret = -EIO;
L
Linus Torvalds 已提交
1051

1052
	spin_lock_bh(&adapter->req_q_lock);
S
Swen Schillig 已提交
1053 1054
	if (zfcp_fsf_req_sbal_get(adapter))
		goto out;
L
Linus Torvalds 已提交
1055

S
Swen Schillig 已提交
1056 1057
	req = zfcp_fsf_req_create(adapter, FSF_QTCB_SEND_GENERIC,
				  ZFCP_REQ_AUTO_CLEANUP, pool);
1058
	if (IS_ERR(req)) {
S
Swen Schillig 已提交
1059 1060
		ret = PTR_ERR(req);
		goto out;
1061 1062
	}

S
Swen Schillig 已提交
1063 1064
	ret = zfcp_fsf_setup_sbals(req, ct->req, ct->resp,
				   FSF_MAX_SBALS_PER_REQ);
C
Christof Schmitt 已提交
1065
	if (ret)
L
Linus Torvalds 已提交
1066 1067
		goto failed_send;

S
Swen Schillig 已提交
1068
	req->handler = zfcp_fsf_send_ct_handler;
1069
	req->qtcb->header.port_handle = wka_port->handle;
S
Swen Schillig 已提交
1070 1071 1072 1073 1074 1075 1076 1077 1078
	req->qtcb->bottom.support.service_class = FSF_CLASS_3;
	req->qtcb->bottom.support.timeout = ct->timeout;
	req->data = ct;

	zfcp_san_dbf_event_ct_request(req);

	if (erp_action) {
		erp_action->fsf_req = req;
		req->erp_action = erp_action;
1079
		zfcp_fsf_start_erp_timer(req);
S
Swen Schillig 已提交
1080 1081
	} else
		zfcp_fsf_start_timer(req, ZFCP_FSF_REQUEST_TIMEOUT);
L
Linus Torvalds 已提交
1082

S
Swen Schillig 已提交
1083 1084 1085
	ret = zfcp_fsf_req_send(req);
	if (ret)
		goto failed_send;
L
Linus Torvalds 已提交
1086

S
Swen Schillig 已提交
1087
	goto out;
L
Linus Torvalds 已提交
1088

S
Swen Schillig 已提交
1089 1090 1091 1092 1093
failed_send:
	zfcp_fsf_req_free(req);
	if (erp_action)
		erp_action->fsf_req = NULL;
out:
1094
	spin_unlock_bh(&adapter->req_q_lock);
S
Swen Schillig 已提交
1095
	return ret;
L
Linus Torvalds 已提交
1096 1097
}

S
Swen Schillig 已提交
1098
static void zfcp_fsf_send_els_handler(struct zfcp_fsf_req *req)
L
Linus Torvalds 已提交
1099
{
S
Swen Schillig 已提交
1100 1101 1102
	struct zfcp_send_els *send_els = req->data;
	struct zfcp_port *port = send_els->port;
	struct fsf_qtcb_header *header = &req->qtcb->header;
L
Linus Torvalds 已提交
1103

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

S
Swen Schillig 已提交
1106
	if (req->status & ZFCP_STATUS_FSFREQ_ERROR)
L
Linus Torvalds 已提交
1107 1108 1109 1110
		goto skip_fsfstatus;

	switch (header->fsf_status) {
	case FSF_GOOD:
S
Swen Schillig 已提交
1111 1112
		zfcp_san_dbf_event_els_response(req);
		send_els->status = 0;
L
Linus Torvalds 已提交
1113 1114
		break;
	case FSF_SERVICE_CLASS_NOT_SUPPORTED:
S
Swen Schillig 已提交
1115
		zfcp_fsf_class_not_supp(req);
L
Linus Torvalds 已提交
1116 1117 1118 1119
		break;
	case FSF_ADAPTER_STATUS_AVAILABLE:
		switch (header->fsf_status_qual.word[0]){
		case FSF_SQ_INVOKE_LINK_TEST_PROCEDURE:
1120 1121
			if (port && (send_els->ls_code != ZFCP_LS_ADISC))
				zfcp_test_link(port);
S
Swen Schillig 已提交
1122
			/*fall through */
L
Linus Torvalds 已提交
1123 1124
		case FSF_SQ_ULP_DEPENDENT_ERP_REQUIRED:
		case FSF_SQ_RETRY_IF_POSSIBLE:
S
Swen Schillig 已提交
1125
			req->status |= ZFCP_STATUS_FSFREQ_ERROR;
L
Linus Torvalds 已提交
1126 1127 1128 1129 1130 1131 1132 1133 1134
			break;
		}
		break;
	case FSF_ELS_COMMAND_REJECTED:
	case FSF_PAYLOAD_SIZE_MISMATCH:
	case FSF_REQUEST_SIZE_TOO_LARGE:
	case FSF_RESPONSE_SIZE_TOO_LARGE:
		break;
	case FSF_ACCESS_DENIED:
S
Swen Schillig 已提交
1135
		zfcp_fsf_access_denied_port(req, port);
L
Linus Torvalds 已提交
1136
		break;
S
Swen Schillig 已提交
1137 1138 1139
	case FSF_SBAL_MISMATCH:
		/* should never occure, avoided in zfcp_fsf_send_els */
		/* fall through */
L
Linus Torvalds 已提交
1140
	default:
S
Swen Schillig 已提交
1141
		req->status |= ZFCP_STATUS_FSFREQ_ERROR;
L
Linus Torvalds 已提交
1142 1143 1144
		break;
	}
skip_fsfstatus:
H
Heiko Carstens 已提交
1145
	if (send_els->handler)
L
Linus Torvalds 已提交
1146
		send_els->handler(send_els->handler_data);
S
Swen Schillig 已提交
1147
}
L
Linus Torvalds 已提交
1148

S
Swen Schillig 已提交
1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163
/**
 * zfcp_fsf_send_els - initiate an ELS command (FC-FS)
 * @els: pointer to struct zfcp_send_els with data for the command
 */
int zfcp_fsf_send_els(struct zfcp_send_els *els)
{
	struct zfcp_fsf_req *req;
	struct zfcp_adapter *adapter = els->adapter;
	struct fsf_qtcb_bottom_support *bottom;
	int ret = -EIO;

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

1164
	spin_lock(&adapter->req_q_lock);
1165
	if (!zfcp_fsf_sbal_available(adapter))
S
Swen Schillig 已提交
1166 1167 1168
		goto out;
	req = zfcp_fsf_req_create(adapter, FSF_QTCB_SEND_ELS,
				  ZFCP_REQ_AUTO_CLEANUP, NULL);
1169
	if (IS_ERR(req)) {
S
Swen Schillig 已提交
1170 1171 1172 1173
		ret = PTR_ERR(req);
		goto out;
	}

1174 1175
	ret = zfcp_fsf_setup_sbals(req, els->req, els->resp, 2);

S
Swen Schillig 已提交
1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197
	if (ret)
		goto failed_send;

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

	zfcp_san_dbf_event_els_request(req);

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

	goto out;

failed_send:
	zfcp_fsf_req_free(req);
out:
1198
	spin_unlock(&adapter->req_q_lock);
S
Swen Schillig 已提交
1199
	return ret;
L
Linus Torvalds 已提交
1200 1201
}

S
Swen Schillig 已提交
1202
int zfcp_fsf_exchange_config_data(struct zfcp_erp_action *erp_action)
L
Linus Torvalds 已提交
1203
{
1204
	struct qdio_buffer_element *sbale;
S
Swen Schillig 已提交
1205
	struct zfcp_fsf_req *req;
1206
	struct zfcp_adapter *adapter = erp_action->adapter;
S
Swen Schillig 已提交
1207 1208
	int retval = -EIO;

1209
	spin_lock_bh(&adapter->req_q_lock);
1210
	if (!zfcp_fsf_sbal_available(adapter))
S
Swen Schillig 已提交
1211 1212 1213 1214 1215
		goto out;
	req = zfcp_fsf_req_create(adapter,
				  FSF_QTCB_EXCHANGE_CONFIG_DATA,
				  ZFCP_REQ_AUTO_CLEANUP,
				  adapter->pool.fsf_req_erp);
1216
	if (IS_ERR(req)) {
S
Swen Schillig 已提交
1217 1218
		retval = PTR_ERR(req);
		goto out;
L
Linus Torvalds 已提交
1219 1220
	}

S
Swen Schillig 已提交
1221
	sbale = zfcp_qdio_sbale_req(req);
1222 1223
	sbale[0].flags |= SBAL_FLAGS0_TYPE_READ;
	sbale[1].flags |= SBAL_FLAGS_LAST_ENTRY;
L
Linus Torvalds 已提交
1224

S
Swen Schillig 已提交
1225
	req->qtcb->bottom.config.feature_selection =
1226 1227
			FSF_FEATURE_CFDC |
			FSF_FEATURE_LUN_SHARING |
1228
			FSF_FEATURE_NOTIFICATION_LOST |
1229
			FSF_FEATURE_UPDATE_ALERT;
S
Swen Schillig 已提交
1230 1231 1232
	req->erp_action = erp_action;
	req->handler = zfcp_fsf_exchange_config_data_handler;
	erp_action->fsf_req = req;
L
Linus Torvalds 已提交
1233

1234
	zfcp_fsf_start_erp_timer(req);
S
Swen Schillig 已提交
1235
	retval = zfcp_fsf_req_send(req);
L
Linus Torvalds 已提交
1236
	if (retval) {
S
Swen Schillig 已提交
1237
		zfcp_fsf_req_free(req);
L
Linus Torvalds 已提交
1238 1239
		erp_action->fsf_req = NULL;
	}
S
Swen Schillig 已提交
1240
out:
1241
	spin_unlock_bh(&adapter->req_q_lock);
1242 1243
	return retval;
}
L
Linus Torvalds 已提交
1244

S
Swen Schillig 已提交
1245 1246
int zfcp_fsf_exchange_config_data_sync(struct zfcp_adapter *adapter,
				       struct fsf_qtcb_bottom_config *data)
1247
{
1248
	struct qdio_buffer_element *sbale;
S
Swen Schillig 已提交
1249 1250 1251
	struct zfcp_fsf_req *req = NULL;
	int retval = -EIO;

1252
	spin_lock_bh(&adapter->req_q_lock);
S
Swen Schillig 已提交
1253 1254 1255 1256 1257
	if (zfcp_fsf_req_sbal_get(adapter))
		goto out;

	req = zfcp_fsf_req_create(adapter, FSF_QTCB_EXCHANGE_CONFIG_DATA,
				  0, NULL);
1258
	if (IS_ERR(req)) {
S
Swen Schillig 已提交
1259 1260
		retval = PTR_ERR(req);
		goto out;
1261 1262
	}

S
Swen Schillig 已提交
1263
	sbale = zfcp_qdio_sbale_req(req);
1264 1265
	sbale[0].flags |= SBAL_FLAGS0_TYPE_READ;
	sbale[1].flags |= SBAL_FLAGS_LAST_ENTRY;
S
Swen Schillig 已提交
1266
	req->handler = zfcp_fsf_exchange_config_data_handler;
1267

S
Swen Schillig 已提交
1268
	req->qtcb->bottom.config.feature_selection =
1269 1270 1271 1272 1273 1274
			FSF_FEATURE_CFDC |
			FSF_FEATURE_LUN_SHARING |
			FSF_FEATURE_NOTIFICATION_LOST |
			FSF_FEATURE_UPDATE_ALERT;

	if (data)
S
Swen Schillig 已提交
1275
		req->data = data;
1276

S
Swen Schillig 已提交
1277 1278 1279
	zfcp_fsf_start_timer(req, ZFCP_FSF_REQUEST_TIMEOUT);
	retval = zfcp_fsf_req_send(req);
out:
1280
	spin_unlock_bh(&adapter->req_q_lock);
C
Christof Schmitt 已提交
1281
	if (!retval)
S
Swen Schillig 已提交
1282 1283
		wait_event(req->completion_wq,
			   req->status & ZFCP_STATUS_FSFREQ_COMPLETED);
1284

S
Swen Schillig 已提交
1285
	zfcp_fsf_req_free(req);
1286

L
Linus Torvalds 已提交
1287 1288 1289 1290 1291
	return retval;
}

/**
 * zfcp_fsf_exchange_port_data - request information about local port
1292
 * @erp_action: ERP action for the adapter for which port data is requested
S
Swen Schillig 已提交
1293
 * Returns: 0 on success, error otherwise
L
Linus Torvalds 已提交
1294
 */
S
Swen Schillig 已提交
1295
int zfcp_fsf_exchange_port_data(struct zfcp_erp_action *erp_action)
L
Linus Torvalds 已提交
1296
{
1297
	struct qdio_buffer_element *sbale;
S
Swen Schillig 已提交
1298
	struct zfcp_fsf_req *req;
1299
	struct zfcp_adapter *adapter = erp_action->adapter;
S
Swen Schillig 已提交
1300
	int retval = -EIO;
L
Linus Torvalds 已提交
1301

C
Christof Schmitt 已提交
1302
	if (!(adapter->adapter_features & FSF_FEATURE_HBAAPI_MANAGEMENT))
1303
		return -EOPNOTSUPP;
L
Linus Torvalds 已提交
1304

1305
	spin_lock_bh(&adapter->req_q_lock);
1306
	if (!zfcp_fsf_sbal_available(adapter))
S
Swen Schillig 已提交
1307 1308 1309 1310
		goto out;
	req = zfcp_fsf_req_create(adapter, FSF_QTCB_EXCHANGE_PORT_DATA,
				  ZFCP_REQ_AUTO_CLEANUP,
				  adapter->pool.fsf_req_erp);
1311
	if (IS_ERR(req)) {
S
Swen Schillig 已提交
1312 1313
		retval = PTR_ERR(req);
		goto out;
1314 1315
	}

S
Swen Schillig 已提交
1316
	sbale = zfcp_qdio_sbale_req(req);
1317 1318
	sbale[0].flags |= SBAL_FLAGS0_TYPE_READ;
	sbale[1].flags |= SBAL_FLAGS_LAST_ENTRY;
L
Linus Torvalds 已提交
1319

S
Swen Schillig 已提交
1320 1321 1322
	req->handler = zfcp_fsf_exchange_port_data_handler;
	req->erp_action = erp_action;
	erp_action->fsf_req = req;
1323

1324
	zfcp_fsf_start_erp_timer(req);
S
Swen Schillig 已提交
1325
	retval = zfcp_fsf_req_send(req);
L
Linus Torvalds 已提交
1326
	if (retval) {
S
Swen Schillig 已提交
1327
		zfcp_fsf_req_free(req);
1328 1329
		erp_action->fsf_req = NULL;
	}
S
Swen Schillig 已提交
1330
out:
1331
	spin_unlock_bh(&adapter->req_q_lock);
1332 1333 1334 1335 1336
	return retval;
}

/**
 * zfcp_fsf_exchange_port_data_sync - request information about local port
S
Swen Schillig 已提交
1337 1338 1339
 * @adapter: pointer to struct zfcp_adapter
 * @data: pointer to struct fsf_qtcb_bottom_port
 * Returns: 0 on success, error otherwise
1340
 */
S
Swen Schillig 已提交
1341 1342
int zfcp_fsf_exchange_port_data_sync(struct zfcp_adapter *adapter,
				     struct fsf_qtcb_bottom_port *data)
1343
{
1344
	struct qdio_buffer_element *sbale;
S
Swen Schillig 已提交
1345 1346
	struct zfcp_fsf_req *req = NULL;
	int retval = -EIO;
1347

C
Christof Schmitt 已提交
1348
	if (!(adapter->adapter_features & FSF_FEATURE_HBAAPI_MANAGEMENT))
1349 1350
		return -EOPNOTSUPP;

1351
	spin_lock_bh(&adapter->req_q_lock);
1352
	if (!zfcp_fsf_sbal_available(adapter))
S
Swen Schillig 已提交
1353 1354 1355 1356
		goto out;

	req = zfcp_fsf_req_create(adapter, FSF_QTCB_EXCHANGE_PORT_DATA, 0,
				  NULL);
1357
	if (IS_ERR(req)) {
S
Swen Schillig 已提交
1358 1359
		retval = PTR_ERR(req);
		goto out;
L
Linus Torvalds 已提交
1360 1361
	}

1362
	if (data)
S
Swen Schillig 已提交
1363
		req->data = data;
1364

S
Swen Schillig 已提交
1365
	sbale = zfcp_qdio_sbale_req(req);
1366 1367 1368
	sbale[0].flags |= SBAL_FLAGS0_TYPE_READ;
	sbale[1].flags |= SBAL_FLAGS_LAST_ENTRY;

S
Swen Schillig 已提交
1369 1370 1371 1372
	req->handler = zfcp_fsf_exchange_port_data_handler;
	zfcp_fsf_start_timer(req, ZFCP_FSF_REQUEST_TIMEOUT);
	retval = zfcp_fsf_req_send(req);
out:
1373
	spin_unlock_bh(&adapter->req_q_lock);
C
Christof Schmitt 已提交
1374
	if (!retval)
S
Swen Schillig 已提交
1375 1376 1377
		wait_event(req->completion_wq,
			   req->status & ZFCP_STATUS_FSFREQ_COMPLETED);
	zfcp_fsf_req_free(req);
L
Linus Torvalds 已提交
1378 1379 1380 1381

	return retval;
}

S
Swen Schillig 已提交
1382
static void zfcp_fsf_open_port_handler(struct zfcp_fsf_req *req)
L
Linus Torvalds 已提交
1383
{
S
Swen Schillig 已提交
1384 1385
	struct zfcp_port *port = req->data;
	struct fsf_qtcb_header *header = &req->qtcb->header;
L
Linus Torvalds 已提交
1386 1387
	struct fsf_plogi *plogi;

S
Swen Schillig 已提交
1388
	if (req->status & ZFCP_STATUS_FSFREQ_ERROR)
1389
		return;
L
Linus Torvalds 已提交
1390 1391 1392 1393 1394

	switch (header->fsf_status) {
	case FSF_PORT_ALREADY_OPEN:
		break;
	case FSF_ACCESS_DENIED:
S
Swen Schillig 已提交
1395
		zfcp_fsf_access_denied_port(req, port);
L
Linus Torvalds 已提交
1396 1397
		break;
	case FSF_MAXIMUM_NUMBER_OF_PORTS_EXCEEDED:
S
Swen Schillig 已提交
1398
		dev_warn(&req->adapter->ccw_device->dev,
1399
			 "Not enough FCP adapter resources to open "
1400 1401
			 "remote port 0x%016Lx\n",
			 (unsigned long long)port->wwpn);
S
Swen Schillig 已提交
1402 1403
		zfcp_erp_port_failed(port, 31, req);
		req->status |= ZFCP_STATUS_FSFREQ_ERROR;
L
Linus Torvalds 已提交
1404 1405 1406 1407 1408
		break;
	case FSF_ADAPTER_STATUS_AVAILABLE:
		switch (header->fsf_status_qual.word[0]) {
		case FSF_SQ_INVOKE_LINK_TEST_PROCEDURE:
		case FSF_SQ_ULP_DEPENDENT_ERP_REQUIRED:
S
Swen Schillig 已提交
1409
			req->status |= ZFCP_STATUS_FSFREQ_ERROR;
L
Linus Torvalds 已提交
1410 1411
			break;
		case FSF_SQ_NO_RETRY_POSSIBLE:
S
Swen Schillig 已提交
1412
			dev_warn(&req->adapter->ccw_device->dev,
1413
				 "Remote port 0x%016Lx could not be opened\n",
1414
				 (unsigned long long)port->wwpn);
S
Swen Schillig 已提交
1415 1416
			zfcp_erp_port_failed(port, 32, req);
			req->status |= ZFCP_STATUS_FSFREQ_ERROR;
L
Linus Torvalds 已提交
1417 1418 1419 1420 1421 1422 1423
			break;
		}
		break;
	case FSF_GOOD:
		port->handle = header->port_handle;
		atomic_set_mask(ZFCP_STATUS_COMMON_OPEN |
				ZFCP_STATUS_PORT_PHYS_OPEN, &port->status);
1424 1425 1426
		atomic_clear_mask(ZFCP_STATUS_COMMON_ACCESS_DENIED |
		                  ZFCP_STATUS_COMMON_ACCESS_BOXED,
		                  &port->status);
L
Linus Torvalds 已提交
1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441
		/* check whether D_ID has changed during open */
		/*
		 * FIXME: This check is not airtight, as the FCP channel does
		 * not monitor closures of target port connections caused on
		 * the remote side. Thus, they might miss out on invalidating
		 * locally cached WWPNs (and other N_Port parameters) of gone
		 * target ports. So, our heroic attempt to make things safe
		 * could be undermined by 'open port' response data tagged with
		 * obsolete WWPNs. Another reason to monitor potential
		 * connection closures ourself at least (by interpreting
		 * incoming ELS' and unsolicited status). It just crosses my
		 * mind that one should be able to cross-check by means of
		 * another GID_PN straight after a port has been opened.
		 * Alternately, an ADISC/PDISC ELS should suffice, as well.
		 */
S
Swen Schillig 已提交
1442 1443 1444 1445 1446 1447 1448 1449
		plogi = (struct fsf_plogi *) req->qtcb->bottom.support.els;
		if (req->qtcb->bottom.support.els1_length >= sizeof(*plogi)) {
			if (plogi->serv_param.wwpn != port->wwpn)
				atomic_clear_mask(ZFCP_STATUS_PORT_DID_DID,
						  &port->status);
			else {
				port->wwnn = plogi->serv_param.wwnn;
				zfcp_fc_plogi_evaluate(port, plogi);
L
Linus Torvalds 已提交
1450 1451 1452 1453
			}
		}
		break;
	case FSF_UNKNOWN_OP_SUBTYPE:
S
Swen Schillig 已提交
1454
		req->status |= ZFCP_STATUS_FSFREQ_ERROR;
L
Linus Torvalds 已提交
1455 1456 1457 1458
		break;
	}
}

S
Swen Schillig 已提交
1459 1460 1461 1462
/**
 * zfcp_fsf_open_port - create and send open port request
 * @erp_action: pointer to struct zfcp_erp_action
 * Returns: 0 on success, error otherwise
L
Linus Torvalds 已提交
1463
 */
S
Swen Schillig 已提交
1464
int zfcp_fsf_open_port(struct zfcp_erp_action *erp_action)
L
Linus Torvalds 已提交
1465
{
1466
	struct qdio_buffer_element *sbale;
S
Swen Schillig 已提交
1467 1468 1469 1470
	struct zfcp_adapter *adapter = erp_action->adapter;
	struct zfcp_fsf_req *req;
	int retval = -EIO;

1471
	spin_lock_bh(&adapter->req_q_lock);
S
Swen Schillig 已提交
1472 1473 1474 1475 1476 1477 1478
	if (zfcp_fsf_req_sbal_get(adapter))
		goto out;

	req = zfcp_fsf_req_create(adapter,
				  FSF_QTCB_OPEN_PORT_WITH_DID,
				  ZFCP_REQ_AUTO_CLEANUP,
				  adapter->pool.fsf_req_erp);
1479
	if (IS_ERR(req)) {
S
Swen Schillig 已提交
1480
		retval = PTR_ERR(req);
L
Linus Torvalds 已提交
1481
		goto out;
S
Swen Schillig 已提交
1482
	}
L
Linus Torvalds 已提交
1483

S
Swen Schillig 已提交
1484
	sbale = zfcp_qdio_sbale_req(req);
L
Linus Torvalds 已提交
1485 1486 1487
        sbale[0].flags |= SBAL_FLAGS0_TYPE_READ;
        sbale[1].flags |= SBAL_FLAGS_LAST_ENTRY;

S
Swen Schillig 已提交
1488 1489 1490 1491 1492 1493
	req->handler = zfcp_fsf_open_port_handler;
	req->qtcb->bottom.support.d_id = erp_action->port->d_id;
	req->data = erp_action->port;
	req->erp_action = erp_action;
	erp_action->fsf_req = req;

1494
	zfcp_fsf_start_erp_timer(req);
S
Swen Schillig 已提交
1495
	retval = zfcp_fsf_req_send(req);
L
Linus Torvalds 已提交
1496
	if (retval) {
S
Swen Schillig 已提交
1497
		zfcp_fsf_req_free(req);
L
Linus Torvalds 已提交
1498 1499
		erp_action->fsf_req = NULL;
	}
S
Swen Schillig 已提交
1500
out:
1501
	spin_unlock_bh(&adapter->req_q_lock);
L
Linus Torvalds 已提交
1502 1503 1504
	return retval;
}

S
Swen Schillig 已提交
1505
static void zfcp_fsf_close_port_handler(struct zfcp_fsf_req *req)
L
Linus Torvalds 已提交
1506
{
S
Swen Schillig 已提交
1507
	struct zfcp_port *port = req->data;
L
Linus Torvalds 已提交
1508

S
Swen Schillig 已提交
1509
	if (req->status & ZFCP_STATUS_FSFREQ_ERROR)
1510
		return;
L
Linus Torvalds 已提交
1511

S
Swen Schillig 已提交
1512
	switch (req->qtcb->header.fsf_status) {
L
Linus Torvalds 已提交
1513
	case FSF_PORT_HANDLE_NOT_VALID:
S
Swen Schillig 已提交
1514 1515
		zfcp_erp_adapter_reopen(port->adapter, 0, 107, req);
		req->status |= ZFCP_STATUS_FSFREQ_ERROR;
L
Linus Torvalds 已提交
1516 1517 1518 1519
		break;
	case FSF_ADAPTER_STATUS_AVAILABLE:
		break;
	case FSF_GOOD:
S
Swen Schillig 已提交
1520
		zfcp_erp_modify_port_status(port, 33, req,
L
Linus Torvalds 已提交
1521 1522 1523 1524 1525 1526
					    ZFCP_STATUS_COMMON_OPEN,
					    ZFCP_CLEAR);
		break;
	}
}

S
Swen Schillig 已提交
1527 1528 1529 1530
/**
 * zfcp_fsf_close_port - create and send close port request
 * @erp_action: pointer to struct zfcp_erp_action
 * Returns: 0 on success, error otherwise
L
Linus Torvalds 已提交
1531
 */
S
Swen Schillig 已提交
1532
int zfcp_fsf_close_port(struct zfcp_erp_action *erp_action)
L
Linus Torvalds 已提交
1533
{
1534
	struct qdio_buffer_element *sbale;
S
Swen Schillig 已提交
1535 1536 1537 1538
	struct zfcp_adapter *adapter = erp_action->adapter;
	struct zfcp_fsf_req *req;
	int retval = -EIO;

1539
	spin_lock_bh(&adapter->req_q_lock);
S
Swen Schillig 已提交
1540 1541 1542 1543 1544 1545
	if (zfcp_fsf_req_sbal_get(adapter))
		goto out;

	req = zfcp_fsf_req_create(adapter, FSF_QTCB_CLOSE_PORT,
				  ZFCP_REQ_AUTO_CLEANUP,
				  adapter->pool.fsf_req_erp);
1546
	if (IS_ERR(req)) {
S
Swen Schillig 已提交
1547
		retval = PTR_ERR(req);
L
Linus Torvalds 已提交
1548
		goto out;
S
Swen Schillig 已提交
1549
	}
L
Linus Torvalds 已提交
1550

S
Swen Schillig 已提交
1551
	sbale = zfcp_qdio_sbale_req(req);
L
Linus Torvalds 已提交
1552 1553 1554
	sbale[0].flags |= SBAL_FLAGS0_TYPE_READ;
	sbale[1].flags |= SBAL_FLAGS_LAST_ENTRY;

S
Swen Schillig 已提交
1555 1556 1557 1558 1559 1560
	req->handler = zfcp_fsf_close_port_handler;
	req->data = erp_action->port;
	req->erp_action = erp_action;
	req->qtcb->header.port_handle = erp_action->port->handle;
	erp_action->fsf_req = req;

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

1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611
static void zfcp_fsf_open_wka_port_handler(struct zfcp_fsf_req *req)
{
	struct zfcp_wka_port *wka_port = req->data;
	struct fsf_qtcb_header *header = &req->qtcb->header;

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

	switch (header->fsf_status) {
	case FSF_MAXIMUM_NUMBER_OF_PORTS_EXCEEDED:
		dev_warn(&req->adapter->ccw_device->dev,
			 "Opening WKA port 0x%x failed\n", wka_port->d_id);
	case FSF_ADAPTER_STATUS_AVAILABLE:
		req->status |= ZFCP_STATUS_FSFREQ_ERROR;
	case FSF_ACCESS_DENIED:
		wka_port->status = ZFCP_WKA_PORT_OFFLINE;
		break;
	case FSF_PORT_ALREADY_OPEN:
	case FSF_GOOD:
		wka_port->handle = header->port_handle;
		wka_port->status = ZFCP_WKA_PORT_ONLINE;
	}
out:
	wake_up(&wka_port->completion_wq);
}

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

1612
	spin_lock_bh(&adapter->req_q_lock);
1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637
	if (zfcp_fsf_req_sbal_get(adapter))
		goto out;

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

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

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

	zfcp_fsf_start_timer(req, ZFCP_FSF_REQUEST_TIMEOUT);
	retval = zfcp_fsf_req_send(req);
	if (retval)
		zfcp_fsf_req_free(req);
out:
1638
	spin_unlock_bh(&adapter->req_q_lock);
1639 1640 1641 1642 1643 1644 1645 1646 1647
	return retval;
}

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

	if (req->qtcb->header.fsf_status == FSF_PORT_HANDLE_NOT_VALID) {
		req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1648
		zfcp_erp_adapter_reopen(wka_port->adapter, 0, 84, req);
1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666
	}

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

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

1667
	spin_lock_bh(&adapter->req_q_lock);
1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691
	if (zfcp_fsf_req_sbal_get(adapter))
		goto out;

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

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

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

	zfcp_fsf_start_timer(req, ZFCP_FSF_REQUEST_TIMEOUT);
	retval = zfcp_fsf_req_send(req);
	if (retval)
		zfcp_fsf_req_free(req);
out:
1692
	spin_unlock_bh(&adapter->req_q_lock);
1693 1694 1695
	return retval;
}

S
Swen Schillig 已提交
1696
static void zfcp_fsf_close_physical_port_handler(struct zfcp_fsf_req *req)
L
Linus Torvalds 已提交
1697
{
S
Swen Schillig 已提交
1698 1699
	struct zfcp_port *port = req->data;
	struct fsf_qtcb_header *header = &req->qtcb->header;
L
Linus Torvalds 已提交
1700 1701
	struct zfcp_unit *unit;

S
Swen Schillig 已提交
1702
	if (req->status & ZFCP_STATUS_FSFREQ_ERROR)
L
Linus Torvalds 已提交
1703 1704 1705 1706
		goto skip_fsfstatus;

	switch (header->fsf_status) {
	case FSF_PORT_HANDLE_NOT_VALID:
S
Swen Schillig 已提交
1707 1708
		zfcp_erp_adapter_reopen(port->adapter, 0, 108, req);
		req->status |= ZFCP_STATUS_FSFREQ_ERROR;
L
Linus Torvalds 已提交
1709 1710
		break;
	case FSF_ACCESS_DENIED:
S
Swen Schillig 已提交
1711
		zfcp_fsf_access_denied_port(req, port);
L
Linus Torvalds 已提交
1712 1713
		break;
	case FSF_PORT_BOXED:
S
Swen Schillig 已提交
1714 1715 1716
		zfcp_erp_port_boxed(port, 50, req);
		req->status |= ZFCP_STATUS_FSFREQ_ERROR |
			       ZFCP_STATUS_FSFREQ_RETRY;
1717 1718 1719 1720 1721 1722
		/* 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 已提交
1723 1724 1725 1726
		break;
	case FSF_ADAPTER_STATUS_AVAILABLE:
		switch (header->fsf_status_qual.word[0]) {
		case FSF_SQ_INVOKE_LINK_TEST_PROCEDURE:
S
Swen Schillig 已提交
1727
			/* fall through */
L
Linus Torvalds 已提交
1728
		case FSF_SQ_ULP_DEPENDENT_ERP_REQUIRED:
S
Swen Schillig 已提交
1729
			req->status |= ZFCP_STATUS_FSFREQ_ERROR;
L
Linus Torvalds 已提交
1730 1731 1732 1733 1734 1735 1736 1737 1738
			break;
		}
		break;
	case FSF_GOOD:
		/* can't use generic zfcp_erp_modify_port_status because
		 * ZFCP_STATUS_COMMON_OPEN must not be reset for the port
		 */
		atomic_clear_mask(ZFCP_STATUS_PORT_PHYS_OPEN, &port->status);
		list_for_each_entry(unit, &port->unit_list_head, list)
S
Swen Schillig 已提交
1739 1740
			atomic_clear_mask(ZFCP_STATUS_COMMON_OPEN,
					  &unit->status);
L
Linus Torvalds 已提交
1741 1742
		break;
	}
S
Swen Schillig 已提交
1743
skip_fsfstatus:
L
Linus Torvalds 已提交
1744 1745 1746
	atomic_clear_mask(ZFCP_STATUS_PORT_PHYS_CLOSING, &port->status);
}

S
Swen Schillig 已提交
1747 1748 1749 1750
/**
 * zfcp_fsf_close_physical_port - close physical port
 * @erp_action: pointer to struct zfcp_erp_action
 * Returns: 0 on success
L
Linus Torvalds 已提交
1751
 */
S
Swen Schillig 已提交
1752
int zfcp_fsf_close_physical_port(struct zfcp_erp_action *erp_action)
L
Linus Torvalds 已提交
1753
{
1754
	struct qdio_buffer_element *sbale;
S
Swen Schillig 已提交
1755 1756 1757 1758
	struct zfcp_adapter *adapter = erp_action->adapter;
	struct zfcp_fsf_req *req;
	int retval = -EIO;

1759
	spin_lock_bh(&adapter->req_q_lock);
S
Swen Schillig 已提交
1760
	if (zfcp_fsf_req_sbal_get(adapter))
L
Linus Torvalds 已提交
1761 1762
		goto out;

S
Swen Schillig 已提交
1763 1764 1765
	req = zfcp_fsf_req_create(adapter, FSF_QTCB_CLOSE_PHYSICAL_PORT,
				  ZFCP_REQ_AUTO_CLEANUP,
				  adapter->pool.fsf_req_erp);
1766
	if (IS_ERR(req)) {
S
Swen Schillig 已提交
1767 1768 1769
		retval = PTR_ERR(req);
		goto out;
	}
L
Linus Torvalds 已提交
1770

S
Swen Schillig 已提交
1771 1772 1773
	sbale = zfcp_qdio_sbale_req(req);
	sbale[0].flags |= SBAL_FLAGS0_TYPE_READ;
	sbale[1].flags |= SBAL_FLAGS_LAST_ENTRY;
L
Linus Torvalds 已提交
1774

S
Swen Schillig 已提交
1775 1776 1777 1778 1779 1780 1781 1782
	req->data = erp_action->port;
	req->qtcb->header.port_handle = erp_action->port->handle;
	req->erp_action = erp_action;
	req->handler = zfcp_fsf_close_physical_port_handler;
	erp_action->fsf_req = req;
	atomic_set_mask(ZFCP_STATUS_PORT_PHYS_CLOSING,
			&erp_action->port->status);

1783
	zfcp_fsf_start_erp_timer(req);
S
Swen Schillig 已提交
1784
	retval = zfcp_fsf_req_send(req);
L
Linus Torvalds 已提交
1785
	if (retval) {
S
Swen Schillig 已提交
1786
		zfcp_fsf_req_free(req);
L
Linus Torvalds 已提交
1787 1788
		erp_action->fsf_req = NULL;
	}
S
Swen Schillig 已提交
1789
out:
1790
	spin_unlock_bh(&adapter->req_q_lock);
L
Linus Torvalds 已提交
1791 1792 1793
	return retval;
}

S
Swen Schillig 已提交
1794
static void zfcp_fsf_open_unit_handler(struct zfcp_fsf_req *req)
L
Linus Torvalds 已提交
1795
{
S
Swen Schillig 已提交
1796 1797 1798 1799 1800 1801
	struct zfcp_adapter *adapter = req->adapter;
	struct zfcp_unit *unit = req->data;
	struct fsf_qtcb_header *header = &req->qtcb->header;
	struct fsf_qtcb_bottom_support *bottom = &req->qtcb->bottom.support;
	struct fsf_queue_designator *queue_designator =
				&header->fsf_status_qual.fsf_queue_designator;
1802
	int exclusive, readwrite;
L
Linus Torvalds 已提交
1803

S
Swen Schillig 已提交
1804
	if (req->status & ZFCP_STATUS_FSFREQ_ERROR)
1805
		return;
L
Linus Torvalds 已提交
1806 1807

	atomic_clear_mask(ZFCP_STATUS_COMMON_ACCESS_DENIED |
1808
			  ZFCP_STATUS_COMMON_ACCESS_BOXED |
L
Linus Torvalds 已提交
1809 1810 1811 1812 1813 1814 1815
			  ZFCP_STATUS_UNIT_SHARED |
			  ZFCP_STATUS_UNIT_READONLY,
			  &unit->status);

	switch (header->fsf_status) {

	case FSF_PORT_HANDLE_NOT_VALID:
S
Swen Schillig 已提交
1816 1817
		zfcp_erp_adapter_reopen(unit->port->adapter, 0, 109, req);
		/* fall through */
L
Linus Torvalds 已提交
1818 1819 1820
	case FSF_LUN_ALREADY_OPEN:
		break;
	case FSF_ACCESS_DENIED:
S
Swen Schillig 已提交
1821
		zfcp_fsf_access_denied_unit(req, unit);
L
Linus Torvalds 已提交
1822
		atomic_clear_mask(ZFCP_STATUS_UNIT_SHARED, &unit->status);
C
Christof Schmitt 已提交
1823
		atomic_clear_mask(ZFCP_STATUS_UNIT_READONLY, &unit->status);
L
Linus Torvalds 已提交
1824 1825
		break;
	case FSF_PORT_BOXED:
S
Swen Schillig 已提交
1826 1827 1828
		zfcp_erp_port_boxed(unit->port, 51, req);
		req->status |= ZFCP_STATUS_FSFREQ_ERROR |
			       ZFCP_STATUS_FSFREQ_RETRY;
L
Linus Torvalds 已提交
1829 1830
		break;
	case FSF_LUN_SHARING_VIOLATION:
S
Swen Schillig 已提交
1831
		if (header->fsf_status_qual.word[0])
C
Christof Schmitt 已提交
1832
			dev_warn(&adapter->ccw_device->dev,
1833 1834
				 "LUN 0x%Lx on port 0x%Lx is already in "
				 "use by CSS%d, MIF Image ID %x\n",
1835 1836
				 (unsigned long long)unit->fcp_lun,
				 (unsigned long long)unit->port->wwpn,
1837 1838
				 queue_designator->cssid,
				 queue_designator->hla);
S
Swen Schillig 已提交
1839
		else
C
Christof Schmitt 已提交
1840 1841
			zfcp_act_eval_err(adapter,
					  header->fsf_status_qual.word[2]);
S
Swen Schillig 已提交
1842
		zfcp_erp_unit_access_denied(unit, 60, req);
L
Linus Torvalds 已提交
1843 1844
		atomic_clear_mask(ZFCP_STATUS_UNIT_SHARED, &unit->status);
		atomic_clear_mask(ZFCP_STATUS_UNIT_READONLY, &unit->status);
S
Swen Schillig 已提交
1845
		req->status |= ZFCP_STATUS_FSFREQ_ERROR;
L
Linus Torvalds 已提交
1846 1847
		break;
	case FSF_MAXIMUM_NUMBER_OF_LUNS_EXCEEDED:
S
Swen Schillig 已提交
1848
		dev_warn(&adapter->ccw_device->dev,
1849 1850
			 "No handle is available for LUN "
			 "0x%016Lx on port 0x%016Lx\n",
1851 1852
			 (unsigned long long)unit->fcp_lun,
			 (unsigned long long)unit->port->wwpn);
S
Swen Schillig 已提交
1853 1854 1855 1856
		zfcp_erp_unit_failed(unit, 34, req);
		/* fall through */
	case FSF_INVALID_COMMAND_OPTION:
		req->status |= ZFCP_STATUS_FSFREQ_ERROR;
L
Linus Torvalds 已提交
1857 1858 1859 1860
		break;
	case FSF_ADAPTER_STATUS_AVAILABLE:
		switch (header->fsf_status_qual.word[0]) {
		case FSF_SQ_INVOKE_LINK_TEST_PROCEDURE:
1861
			zfcp_test_link(unit->port);
S
Swen Schillig 已提交
1862
			/* fall through */
L
Linus Torvalds 已提交
1863
		case FSF_SQ_ULP_DEPENDENT_ERP_REQUIRED:
S
Swen Schillig 已提交
1864
			req->status |= ZFCP_STATUS_FSFREQ_ERROR;
L
Linus Torvalds 已提交
1865 1866 1867 1868 1869 1870 1871
			break;
		}
		break;

	case FSF_GOOD:
		unit->handle = header->lun_handle;
		atomic_set_mask(ZFCP_STATUS_COMMON_OPEN, &unit->status);
1872 1873 1874 1875 1876 1877 1878 1879 1880

		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 已提交
1881 1882 1883 1884 1885 1886 1887
			if (!exclusive)
		                atomic_set_mask(ZFCP_STATUS_UNIT_SHARED,
						&unit->status);

			if (!readwrite) {
                		atomic_set_mask(ZFCP_STATUS_UNIT_READONLY,
						&unit->status);
S
Swen Schillig 已提交
1888
				dev_info(&adapter->ccw_device->dev,
1889 1890
					 "SCSI device at LUN 0x%016Lx on port "
					 "0x%016Lx opened read-only\n",
1891 1892
					 (unsigned long long)unit->fcp_lun,
					 (unsigned long long)unit->port->wwpn);
L
Linus Torvalds 已提交
1893 1894 1895
        		}

        		if (exclusive && !readwrite) {
S
Swen Schillig 已提交
1896
				dev_err(&adapter->ccw_device->dev,
1897 1898 1899
					"Exclusive read-only access not "
					"supported (unit 0x%016Lx, "
					"port 0x%016Lx)\n",
1900 1901
					(unsigned long long)unit->fcp_lun,
					(unsigned long long)unit->port->wwpn);
S
Swen Schillig 已提交
1902 1903 1904
				zfcp_erp_unit_failed(unit, 35, req);
				req->status |= ZFCP_STATUS_FSFREQ_ERROR;
				zfcp_erp_unit_shutdown(unit, 0, 80, req);
L
Linus Torvalds 已提交
1905
        		} else if (!exclusive && readwrite) {
S
Swen Schillig 已提交
1906
				dev_err(&adapter->ccw_device->dev,
1907 1908 1909
					"Shared read-write access not "
					"supported (unit 0x%016Lx, port "
					"0x%016Lx\n)",
1910 1911
					(unsigned long long)unit->fcp_lun,
					(unsigned long long)unit->port->wwpn);
S
Swen Schillig 已提交
1912 1913 1914
				zfcp_erp_unit_failed(unit, 36, req);
				req->status |= ZFCP_STATUS_FSFREQ_ERROR;
				zfcp_erp_unit_shutdown(unit, 0, 81, req);
L
Linus Torvalds 已提交
1915 1916 1917 1918 1919 1920
        		}
		}
		break;
	}
}

S
Swen Schillig 已提交
1921 1922 1923 1924
/**
 * zfcp_fsf_open_unit - open unit
 * @erp_action: pointer to struct zfcp_erp_action
 * Returns: 0 on success, error otherwise
L
Linus Torvalds 已提交
1925
 */
S
Swen Schillig 已提交
1926
int zfcp_fsf_open_unit(struct zfcp_erp_action *erp_action)
L
Linus Torvalds 已提交
1927
{
1928
	struct qdio_buffer_element *sbale;
S
Swen Schillig 已提交
1929 1930 1931 1932
	struct zfcp_adapter *adapter = erp_action->adapter;
	struct zfcp_fsf_req *req;
	int retval = -EIO;

1933
	spin_lock_bh(&adapter->req_q_lock);
S
Swen Schillig 已提交
1934
	if (zfcp_fsf_req_sbal_get(adapter))
L
Linus Torvalds 已提交
1935 1936
		goto out;

S
Swen Schillig 已提交
1937 1938 1939
	req = zfcp_fsf_req_create(adapter, FSF_QTCB_OPEN_LUN,
				  ZFCP_REQ_AUTO_CLEANUP,
				  adapter->pool.fsf_req_erp);
1940
	if (IS_ERR(req)) {
S
Swen Schillig 已提交
1941 1942 1943 1944 1945
		retval = PTR_ERR(req);
		goto out;
	}

	sbale = zfcp_qdio_sbale_req(req);
L
Linus Torvalds 已提交
1946 1947 1948
        sbale[0].flags |= SBAL_FLAGS0_TYPE_READ;
        sbale[1].flags |= SBAL_FLAGS_LAST_ENTRY;

S
Swen Schillig 已提交
1949 1950 1951 1952 1953 1954 1955 1956 1957 1958
	req->qtcb->header.port_handle = erp_action->port->handle;
	req->qtcb->bottom.support.fcp_lun = erp_action->unit->fcp_lun;
	req->handler = zfcp_fsf_open_unit_handler;
	req->data = erp_action->unit;
	req->erp_action = erp_action;
	erp_action->fsf_req = req;

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

1959
	zfcp_fsf_start_erp_timer(req);
S
Swen Schillig 已提交
1960
	retval = zfcp_fsf_req_send(req);
L
Linus Torvalds 已提交
1961
	if (retval) {
S
Swen Schillig 已提交
1962
		zfcp_fsf_req_free(req);
L
Linus Torvalds 已提交
1963 1964
		erp_action->fsf_req = NULL;
	}
S
Swen Schillig 已提交
1965
out:
1966
	spin_unlock_bh(&adapter->req_q_lock);
L
Linus Torvalds 已提交
1967 1968 1969
	return retval;
}

S
Swen Schillig 已提交
1970
static void zfcp_fsf_close_unit_handler(struct zfcp_fsf_req *req)
L
Linus Torvalds 已提交
1971
{
S
Swen Schillig 已提交
1972
	struct zfcp_unit *unit = req->data;
L
Linus Torvalds 已提交
1973

S
Swen Schillig 已提交
1974
	if (req->status & ZFCP_STATUS_FSFREQ_ERROR)
1975
		return;
L
Linus Torvalds 已提交
1976

S
Swen Schillig 已提交
1977
	switch (req->qtcb->header.fsf_status) {
L
Linus Torvalds 已提交
1978
	case FSF_PORT_HANDLE_NOT_VALID:
S
Swen Schillig 已提交
1979 1980
		zfcp_erp_adapter_reopen(unit->port->adapter, 0, 110, req);
		req->status |= ZFCP_STATUS_FSFREQ_ERROR;
L
Linus Torvalds 已提交
1981 1982
		break;
	case FSF_LUN_HANDLE_NOT_VALID:
S
Swen Schillig 已提交
1983 1984
		zfcp_erp_port_reopen(unit->port, 0, 111, req);
		req->status |= ZFCP_STATUS_FSFREQ_ERROR;
L
Linus Torvalds 已提交
1985 1986
		break;
	case FSF_PORT_BOXED:
S
Swen Schillig 已提交
1987 1988 1989
		zfcp_erp_port_boxed(unit->port, 52, req);
		req->status |= ZFCP_STATUS_FSFREQ_ERROR |
			       ZFCP_STATUS_FSFREQ_RETRY;
L
Linus Torvalds 已提交
1990 1991
		break;
	case FSF_ADAPTER_STATUS_AVAILABLE:
S
Swen Schillig 已提交
1992
		switch (req->qtcb->header.fsf_status_qual.word[0]) {
L
Linus Torvalds 已提交
1993
		case FSF_SQ_INVOKE_LINK_TEST_PROCEDURE:
1994
			zfcp_test_link(unit->port);
S
Swen Schillig 已提交
1995
			/* fall through */
L
Linus Torvalds 已提交
1996
		case FSF_SQ_ULP_DEPENDENT_ERP_REQUIRED:
S
Swen Schillig 已提交
1997
			req->status |= ZFCP_STATUS_FSFREQ_ERROR;
L
Linus Torvalds 已提交
1998 1999 2000 2001 2002 2003 2004 2005 2006 2007
			break;
		}
		break;
	case FSF_GOOD:
		atomic_clear_mask(ZFCP_STATUS_COMMON_OPEN, &unit->status);
		break;
	}
}

/**
S
Swen Schillig 已提交
2008 2009 2010
 * zfcp_fsf_close_unit - close zfcp unit
 * @erp_action: pointer to struct zfcp_unit
 * Returns: 0 on success, error otherwise
L
Linus Torvalds 已提交
2011
 */
S
Swen Schillig 已提交
2012
int zfcp_fsf_close_unit(struct zfcp_erp_action *erp_action)
L
Linus Torvalds 已提交
2013
{
2014
	struct qdio_buffer_element *sbale;
S
Swen Schillig 已提交
2015 2016 2017
	struct zfcp_adapter *adapter = erp_action->adapter;
	struct zfcp_fsf_req *req;
	int retval = -EIO;
L
Linus Torvalds 已提交
2018

2019
	spin_lock_bh(&adapter->req_q_lock);
S
Swen Schillig 已提交
2020
	if (zfcp_fsf_req_sbal_get(adapter))
L
Linus Torvalds 已提交
2021
		goto out;
S
Swen Schillig 已提交
2022 2023 2024
	req = zfcp_fsf_req_create(adapter, FSF_QTCB_CLOSE_LUN,
				  ZFCP_REQ_AUTO_CLEANUP,
				  adapter->pool.fsf_req_erp);
2025
	if (IS_ERR(req)) {
S
Swen Schillig 已提交
2026 2027 2028
		retval = PTR_ERR(req);
		goto out;
	}
L
Linus Torvalds 已提交
2029

S
Swen Schillig 已提交
2030 2031
	sbale = zfcp_qdio_sbale_req(req);
	sbale[0].flags |= SBAL_FLAGS0_TYPE_READ;
L
Linus Torvalds 已提交
2032 2033
	sbale[1].flags |= SBAL_FLAGS_LAST_ENTRY;

S
Swen Schillig 已提交
2034 2035 2036 2037 2038 2039
	req->qtcb->header.port_handle = erp_action->port->handle;
	req->qtcb->header.lun_handle = erp_action->unit->handle;
	req->handler = zfcp_fsf_close_unit_handler;
	req->data = erp_action->unit;
	req->erp_action = erp_action;
	erp_action->fsf_req = req;
2040

2041
	zfcp_fsf_start_erp_timer(req);
S
Swen Schillig 已提交
2042 2043 2044 2045 2046 2047
	retval = zfcp_fsf_req_send(req);
	if (retval) {
		zfcp_fsf_req_free(req);
		erp_action->fsf_req = NULL;
	}
out:
2048
	spin_unlock_bh(&adapter->req_q_lock);
S
Swen Schillig 已提交
2049
	return retval;
L
Linus Torvalds 已提交
2050 2051
}

2052 2053 2054
static void zfcp_fsf_update_lat(struct fsf_latency_record *lat_rec, u32 lat)
{
	lat_rec->sum += lat;
S
Swen Schillig 已提交
2055 2056
	lat_rec->min = min(lat_rec->min, lat);
	lat_rec->max = max(lat_rec->max, lat);
2057 2058
}

S
Swen Schillig 已提交
2059
static void zfcp_fsf_req_latency(struct zfcp_fsf_req *req)
2060 2061 2062
{
	struct fsf_qual_latency_info *lat_inf;
	struct latency_cont *lat;
S
Swen Schillig 已提交
2063
	struct zfcp_unit *unit = req->unit;
2064 2065
	unsigned long flags;

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

S
Swen Schillig 已提交
2068
	switch (req->qtcb->bottom.io.data_direction) {
2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085
	case FSF_DATADIR_READ:
		lat = &unit->latencies.read;
		break;
	case FSF_DATADIR_WRITE:
		lat = &unit->latencies.write;
		break;
	case FSF_DATADIR_CMND:
		lat = &unit->latencies.cmd;
		break;
	default:
		return;
	}

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

S
Stefan Raspl 已提交
2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118
#ifdef CONFIG_BLK_DEV_IO_TRACE
static void zfcp_fsf_trace_latency(struct zfcp_fsf_req *fsf_req)
{
	struct fsf_qual_latency_info *lat_inf;
	struct scsi_cmnd *scsi_cmnd = (struct scsi_cmnd *)fsf_req->data;
	struct request *req = scsi_cmnd->request;
	struct zfcp_blk_drv_data trace;
	int ticks = fsf_req->adapter->timer_ticks;

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

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

S
Swen Schillig 已提交
2119
static void zfcp_fsf_send_fcp_command_task_handler(struct zfcp_fsf_req *req)
L
Linus Torvalds 已提交
2120
{
S
Swen Schillig 已提交
2121
	struct scsi_cmnd *scpnt = req->data;
L
Linus Torvalds 已提交
2122
	struct fcp_rsp_iu *fcp_rsp_iu = (struct fcp_rsp_iu *)
S
Swen Schillig 已提交
2123
	    &(req->qtcb->bottom.io.fcp_rsp);
L
Linus Torvalds 已提交
2124
	u32 sns_len;
2125
	char *fcp_rsp_info = (unsigned char *) &fcp_rsp_iu[1];
L
Linus Torvalds 已提交
2126 2127
	unsigned long flags;

C
Christof Schmitt 已提交
2128
	if (unlikely(!scpnt))
S
Swen Schillig 已提交
2129
		return;
C
Christof Schmitt 已提交
2130

S
Swen Schillig 已提交
2131 2132 2133
	read_lock_irqsave(&req->adapter->abort_lock, flags);

	if (unlikely(req->status & ZFCP_STATUS_FSFREQ_ABORTED)) {
2134 2135
		set_host_byte(scpnt, DID_SOFT_ERROR);
		set_driver_byte(scpnt, SUGGEST_RETRY);
L
Linus Torvalds 已提交
2136 2137 2138
		goto skip_fsfstatus;
	}

S
Swen Schillig 已提交
2139
	if (unlikely(req->status & ZFCP_STATUS_FSFREQ_ERROR)) {
2140
		set_host_byte(scpnt, DID_ERROR);
L
Linus Torvalds 已提交
2141 2142 2143
		goto skip_fsfstatus;
	}

2144
	set_msg_byte(scpnt, COMMAND_COMPLETE);
L
Linus Torvalds 已提交
2145 2146 2147

	scpnt->result |= fcp_rsp_iu->scsi_status;

S
Swen Schillig 已提交
2148 2149
	if (req->adapter->adapter_features & FSF_FEATURE_MEASUREMENT_DATA)
		zfcp_fsf_req_latency(req);
2150

S
Stefan Raspl 已提交
2151 2152
	zfcp_fsf_trace_latency(req);

L
Linus Torvalds 已提交
2153
	if (unlikely(fcp_rsp_iu->validity.bits.fcp_rsp_len_valid)) {
S
Swen Schillig 已提交
2154
		if (fcp_rsp_info[3] == RSP_CODE_GOOD)
2155
			set_host_byte(scpnt, DID_OK);
S
Swen Schillig 已提交
2156
		else {
2157
			set_host_byte(scpnt, DID_ERROR);
已提交
2158
			goto skip_fsfstatus;
L
Linus Torvalds 已提交
2159 2160 2161 2162
		}
	}

	if (unlikely(fcp_rsp_iu->validity.bits.fcp_sns_len_valid)) {
S
Swen Schillig 已提交
2163 2164
		sns_len = FSF_FCP_RSP_SIZE - sizeof(struct fcp_rsp_iu) +
			  fcp_rsp_iu->fcp_rsp_len;
L
Linus Torvalds 已提交
2165 2166 2167
		sns_len = min(sns_len, (u32) SCSI_SENSE_BUFFERSIZE);
		sns_len = min(sns_len, fcp_rsp_iu->fcp_sns_len);

2168
		memcpy(scpnt->sense_buffer,
L
Linus Torvalds 已提交
2169 2170 2171 2172
		       zfcp_get_fcp_sns_info_ptr(fcp_rsp_iu), sns_len);
	}

	if (unlikely(fcp_rsp_iu->validity.bits.fcp_resid_under)) {
2173 2174 2175
		scsi_set_resid(scpnt, fcp_rsp_iu->fcp_resid);
		if (scsi_bufflen(scpnt) - scsi_get_resid(scpnt) <
		    scpnt->underflow)
2176
			set_host_byte(scpnt, DID_ERROR);
L
Linus Torvalds 已提交
2177
	}
S
Swen Schillig 已提交
2178
skip_fsfstatus:
2179
	if (scpnt->result != 0)
S
Swen Schillig 已提交
2180
		zfcp_scsi_dbf_event_result("erro", 3, req->adapter, scpnt, req);
2181
	else if (scpnt->retries > 0)
S
Swen Schillig 已提交
2182
		zfcp_scsi_dbf_event_result("retr", 4, req->adapter, scpnt, req);
2183
	else
S
Swen Schillig 已提交
2184
		zfcp_scsi_dbf_event_result("norm", 6, req->adapter, scpnt, req);
L
Linus Torvalds 已提交
2185 2186 2187 2188 2189 2190 2191 2192 2193

	scpnt->host_scribble = NULL;
	(scpnt->scsi_done) (scpnt);
	/*
	 * We must hold this lock until scsi_done has been called.
	 * Otherwise we may call scsi_done after abort regarding this
	 * command has completed.
	 * Note: scsi_done must not block!
	 */
S
Swen Schillig 已提交
2194
	read_unlock_irqrestore(&req->adapter->abort_lock, flags);
L
Linus Torvalds 已提交
2195 2196
}

S
Swen Schillig 已提交
2197
static void zfcp_fsf_send_fcp_ctm_handler(struct zfcp_fsf_req *req)
L
Linus Torvalds 已提交
2198 2199
{
	struct fcp_rsp_iu *fcp_rsp_iu = (struct fcp_rsp_iu *)
S
Swen Schillig 已提交
2200
	    &(req->qtcb->bottom.io.fcp_rsp);
2201
	char *fcp_rsp_info = (unsigned char *) &fcp_rsp_iu[1];
L
Linus Torvalds 已提交
2202

S
Swen Schillig 已提交
2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219
	if ((fcp_rsp_info[3] != RSP_CODE_GOOD) ||
	     (req->status & ZFCP_STATUS_FSFREQ_ERROR))
		req->status |= ZFCP_STATUS_FSFREQ_TMFUNCFAILED;
}


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

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

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

S
Swen Schillig 已提交
2222 2223 2224 2225 2226 2227 2228 2229 2230 2231
	switch (header->fsf_status) {
	case FSF_HANDLE_MISMATCH:
	case FSF_PORT_HANDLE_NOT_VALID:
		zfcp_erp_adapter_reopen(unit->port->adapter, 0, 112, req);
		req->status |= ZFCP_STATUS_FSFREQ_ERROR;
		break;
	case FSF_FCPLUN_NOT_VALID:
	case FSF_LUN_HANDLE_NOT_VALID:
		zfcp_erp_port_reopen(unit->port, 0, 113, req);
		req->status |= ZFCP_STATUS_FSFREQ_ERROR;
L
Linus Torvalds 已提交
2232
		break;
S
Swen Schillig 已提交
2233 2234
	case FSF_SERVICE_CLASS_NOT_SUPPORTED:
		zfcp_fsf_class_not_supp(req);
L
Linus Torvalds 已提交
2235
		break;
S
Swen Schillig 已提交
2236 2237 2238 2239 2240
	case FSF_ACCESS_DENIED:
		zfcp_fsf_access_denied_unit(req, unit);
		break;
	case FSF_DIRECTION_INDICATOR_NOT_VALID:
		dev_err(&req->adapter->ccw_device->dev,
2241 2242
			"Incorrect direction %d, unit 0x%016Lx on port "
			"0x%016Lx closed\n",
S
Swen Schillig 已提交
2243
			req->qtcb->bottom.io.data_direction,
2244 2245
			(unsigned long long)unit->fcp_lun,
			(unsigned long long)unit->port->wwpn);
S
Swen Schillig 已提交
2246 2247 2248 2249 2250
		zfcp_erp_adapter_shutdown(unit->port->adapter, 0, 133, req);
		req->status |= ZFCP_STATUS_FSFREQ_ERROR;
		break;
	case FSF_CMND_LENGTH_NOT_VALID:
		dev_err(&req->adapter->ccw_device->dev,
2251 2252
			"Incorrect CDB length %d, unit 0x%016Lx on "
			"port 0x%016Lx closed\n",
S
Swen Schillig 已提交
2253
			req->qtcb->bottom.io.fcp_cmnd_length,
2254 2255
			(unsigned long long)unit->fcp_lun,
			(unsigned long long)unit->port->wwpn);
S
Swen Schillig 已提交
2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273
		zfcp_erp_adapter_shutdown(unit->port->adapter, 0, 134, req);
		req->status |= ZFCP_STATUS_FSFREQ_ERROR;
		break;
	case FSF_PORT_BOXED:
		zfcp_erp_port_boxed(unit->port, 53, req);
		req->status |= ZFCP_STATUS_FSFREQ_ERROR |
			       ZFCP_STATUS_FSFREQ_RETRY;
		break;
	case FSF_LUN_BOXED:
		zfcp_erp_unit_boxed(unit, 54, req);
		req->status |= ZFCP_STATUS_FSFREQ_ERROR |
			       ZFCP_STATUS_FSFREQ_RETRY;
		break;
	case FSF_ADAPTER_STATUS_AVAILABLE:
		if (header->fsf_status_qual.word[0] ==
		    FSF_SQ_INVOKE_LINK_TEST_PROCEDURE)
			zfcp_test_link(unit->port);
		req->status |= ZFCP_STATUS_FSFREQ_ERROR;
L
Linus Torvalds 已提交
2274 2275
		break;
	}
S
Swen Schillig 已提交
2276 2277 2278 2279 2280 2281 2282 2283
skip_fsfstatus:
	if (req->status & ZFCP_STATUS_FSFREQ_TASK_MANAGEMENT)
		zfcp_fsf_send_fcp_ctm_handler(req);
	else {
		zfcp_fsf_send_fcp_command_task_handler(req);
		req->unit = NULL;
		zfcp_unit_put(unit);
	}
L
Linus Torvalds 已提交
2284 2285
}

2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299
static void zfcp_set_fcp_dl(struct fcp_cmnd_iu *fcp_cmd, u32 fcp_dl)
{
	u32 *fcp_dl_ptr;

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

S
Swen Schillig 已提交
2300 2301 2302 2303 2304 2305 2306
/**
 * zfcp_fsf_send_fcp_command_task - initiate an FCP command (for a SCSI command)
 * @adapter: adapter where scsi command is issued
 * @unit: unit where command is sent to
 * @scsi_cmnd: scsi command to be sent
 * @timer: timer to be started when request is initiated
 * @req_flags: flags for fsf_request
L
Linus Torvalds 已提交
2307
 */
S
Swen Schillig 已提交
2308 2309 2310 2311
int zfcp_fsf_send_fcp_command_task(struct zfcp_adapter *adapter,
				   struct zfcp_unit *unit,
				   struct scsi_cmnd *scsi_cmnd,
				   int use_timer, int req_flags)
L
Linus Torvalds 已提交
2312
{
S
Swen Schillig 已提交
2313 2314 2315 2316
	struct zfcp_fsf_req *req;
	struct fcp_cmnd_iu *fcp_cmnd_iu;
	unsigned int sbtype;
	int real_bytes, retval = -EIO;
L
Linus Torvalds 已提交
2317

S
Swen Schillig 已提交
2318 2319 2320
	if (unlikely(!(atomic_read(&unit->status) &
		       ZFCP_STATUS_COMMON_UNBLOCKED)))
		return -EBUSY;
L
Linus Torvalds 已提交
2321

2322
	spin_lock(&adapter->req_q_lock);
2323
	if (!zfcp_fsf_sbal_available(adapter))
S
Swen Schillig 已提交
2324 2325 2326
		goto out;
	req = zfcp_fsf_req_create(adapter, FSF_QTCB_FCP_CMND, req_flags,
				  adapter->pool.fsf_req_scsi);
2327
	if (IS_ERR(req)) {
S
Swen Schillig 已提交
2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353
		retval = PTR_ERR(req);
		goto out;
	}

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

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

	fcp_cmnd_iu = (struct fcp_cmnd_iu *) &(req->qtcb->bottom.io.fcp_cmnd);
	fcp_cmnd_iu->fcp_lun = unit->fcp_lun;
	/*
	 * set depending on data direction:
	 *      data direction bits in SBALE (SB Type)
	 *      data direction bits in QTCB
	 *      data direction bits in FCP_CMND IU
	 */
	switch (scsi_cmnd->sc_data_direction) {
	case DMA_NONE:
		req->qtcb->bottom.io.data_direction = FSF_DATADIR_CMND;
		sbtype = SBAL_FLAGS0_TYPE_READ;
L
Linus Torvalds 已提交
2354
		break;
S
Swen Schillig 已提交
2355 2356 2357 2358 2359 2360 2361 2362 2363
	case DMA_FROM_DEVICE:
		req->qtcb->bottom.io.data_direction = FSF_DATADIR_READ;
		sbtype = SBAL_FLAGS0_TYPE_READ;
		fcp_cmnd_iu->rddata = 1;
		break;
	case DMA_TO_DEVICE:
		req->qtcb->bottom.io.data_direction = FSF_DATADIR_WRITE;
		sbtype = SBAL_FLAGS0_TYPE_WRITE;
		fcp_cmnd_iu->wddata = 1;
L
Linus Torvalds 已提交
2364
		break;
S
Swen Schillig 已提交
2365
	case DMA_BIDIRECTIONAL:
L
Linus Torvalds 已提交
2366
	default:
S
Swen Schillig 已提交
2367 2368
		retval = -EIO;
		goto failed_scsi_cmnd;
L
Linus Torvalds 已提交
2369 2370
	}

S
Swen Schillig 已提交
2371 2372 2373 2374 2375 2376
	if (likely((scsi_cmnd->device->simple_tags) ||
		   ((atomic_read(&unit->status) & ZFCP_STATUS_UNIT_READONLY) &&
		    (atomic_read(&unit->status) & ZFCP_STATUS_UNIT_SHARED))))
		fcp_cmnd_iu->task_attribute = SIMPLE_Q;
	else
		fcp_cmnd_iu->task_attribute = UNTAGGED;
L
Linus Torvalds 已提交
2377

S
Swen Schillig 已提交
2378 2379 2380
	if (unlikely(scsi_cmnd->cmd_len > FCP_CDB_LENGTH))
		fcp_cmnd_iu->add_fcp_cdb_length =
			(scsi_cmnd->cmd_len - FCP_CDB_LENGTH) >> 2;
L
Linus Torvalds 已提交
2381

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

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

S
Swen Schillig 已提交
2387 2388 2389 2390 2391 2392 2393 2394
	real_bytes = zfcp_qdio_sbals_from_sg(req, sbtype,
					     scsi_sglist(scsi_cmnd),
					     FSF_MAX_SBALS_PER_REQ);
	if (unlikely(real_bytes < 0)) {
		if (req->sbal_number < FSF_MAX_SBALS_PER_REQ)
			retval = -EIO;
		else {
			dev_err(&adapter->ccw_device->dev,
2395 2396
				"Oversize data package, unit 0x%016Lx "
				"on port 0x%016Lx closed\n",
2397 2398
				(unsigned long long)unit->fcp_lun,
				(unsigned long long)unit->port->wwpn);
S
Swen Schillig 已提交
2399 2400 2401 2402
			zfcp_erp_unit_shutdown(unit, 0, 131, req);
			retval = -EINVAL;
		}
		goto failed_scsi_cmnd;
L
Linus Torvalds 已提交
2403 2404
	}

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

S
Swen Schillig 已提交
2407 2408
	if (use_timer)
		zfcp_fsf_start_timer(req, ZFCP_FSF_REQUEST_TIMEOUT);
L
Linus Torvalds 已提交
2409

S
Swen Schillig 已提交
2410 2411 2412
	retval = zfcp_fsf_req_send(req);
	if (unlikely(retval))
		goto failed_scsi_cmnd;
L
Linus Torvalds 已提交
2413

S
Swen Schillig 已提交
2414
	goto out;
L
Linus Torvalds 已提交
2415

S
Swen Schillig 已提交
2416 2417 2418 2419 2420
failed_scsi_cmnd:
	zfcp_unit_put(unit);
	zfcp_fsf_req_free(req);
	scsi_cmnd->host_scribble = NULL;
out:
2421
	spin_unlock(&adapter->req_q_lock);
S
Swen Schillig 已提交
2422
	return retval;
L
Linus Torvalds 已提交
2423 2424 2425
}

/**
S
Swen Schillig 已提交
2426 2427 2428 2429 2430 2431
 * zfcp_fsf_send_fcp_ctm - send SCSI task management command
 * @adapter: pointer to struct zfcp-adapter
 * @unit: pointer to struct zfcp_unit
 * @tm_flags: unsigned byte for task management flags
 * @req_flags: int request flags
 * Returns: on success pointer to struct fsf_req, NULL otherwise
L
Linus Torvalds 已提交
2432
 */
S
Swen Schillig 已提交
2433 2434 2435
struct zfcp_fsf_req *zfcp_fsf_send_fcp_ctm(struct zfcp_adapter *adapter,
					   struct zfcp_unit *unit,
					   u8 tm_flags, int req_flags)
L
Linus Torvalds 已提交
2436
{
2437
	struct qdio_buffer_element *sbale;
S
Swen Schillig 已提交
2438 2439
	struct zfcp_fsf_req *req = NULL;
	struct fcp_cmnd_iu *fcp_cmnd_iu;
L
Linus Torvalds 已提交
2440

S
Swen Schillig 已提交
2441 2442 2443
	if (unlikely(!(atomic_read(&unit->status) &
		       ZFCP_STATUS_COMMON_UNBLOCKED)))
		return NULL;
L
Linus Torvalds 已提交
2444

2445
	spin_lock(&adapter->req_q_lock);
2446
	if (!zfcp_fsf_sbal_available(adapter))
S
Swen Schillig 已提交
2447 2448 2449
		goto out;
	req = zfcp_fsf_req_create(adapter, FSF_QTCB_FCP_CMND, req_flags,
				  adapter->pool.fsf_req_scsi);
2450
	if (IS_ERR(req))
S
Swen Schillig 已提交
2451
		goto out;
L
Linus Torvalds 已提交
2452

S
Swen Schillig 已提交
2453 2454 2455 2456 2457 2458 2459 2460
	req->status |= ZFCP_STATUS_FSFREQ_TASK_MANAGEMENT;
	req->data = unit;
	req->handler = zfcp_fsf_send_fcp_command_handler;
	req->qtcb->header.lun_handle = unit->handle;
	req->qtcb->header.port_handle = unit->port->handle;
	req->qtcb->bottom.io.data_direction = FSF_DATADIR_CMND;
	req->qtcb->bottom.io.service_class = FSF_CLASS_3;
	req->qtcb->bottom.io.fcp_cmnd_length = 	sizeof(struct fcp_cmnd_iu) +
2461
						sizeof(u32);
S
Swen Schillig 已提交
2462 2463 2464 2465

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

S
Swen Schillig 已提交
2467 2468 2469
	fcp_cmnd_iu = (struct fcp_cmnd_iu *) &req->qtcb->bottom.io.fcp_cmnd;
	fcp_cmnd_iu->fcp_lun = unit->fcp_lun;
	fcp_cmnd_iu->task_management_flags = tm_flags;
L
Linus Torvalds 已提交
2470

S
Swen Schillig 已提交
2471 2472 2473
	zfcp_fsf_start_timer(req, ZFCP_SCSI_ER_TIMEOUT);
	if (!zfcp_fsf_req_send(req))
		goto out;
L
Linus Torvalds 已提交
2474

S
Swen Schillig 已提交
2475 2476 2477
	zfcp_fsf_req_free(req);
	req = NULL;
out:
2478
	spin_unlock(&adapter->req_q_lock);
S
Swen Schillig 已提交
2479 2480
	return req;
}
L
Linus Torvalds 已提交
2481

S
Swen Schillig 已提交
2482 2483 2484 2485
static void zfcp_fsf_control_file_handler(struct zfcp_fsf_req *req)
{
	if (req->qtcb->header.fsf_status != FSF_GOOD)
		req->status |= ZFCP_STATUS_FSFREQ_ERROR;
L
Linus Torvalds 已提交
2486 2487
}

S
Swen Schillig 已提交
2488 2489 2490 2491 2492
/**
 * zfcp_fsf_control_file - control file upload/download
 * @adapter: pointer to struct zfcp_adapter
 * @fsf_cfdc: pointer to struct zfcp_fsf_cfdc
 * Returns: on success pointer to struct zfcp_fsf_req, NULL otherwise
L
Linus Torvalds 已提交
2493
 */
S
Swen Schillig 已提交
2494 2495
struct zfcp_fsf_req *zfcp_fsf_control_file(struct zfcp_adapter *adapter,
					   struct zfcp_fsf_cfdc *fsf_cfdc)
L
Linus Torvalds 已提交
2496
{
2497
	struct qdio_buffer_element *sbale;
S
Swen Schillig 已提交
2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514
	struct zfcp_fsf_req *req = NULL;
	struct fsf_qtcb_bottom_support *bottom;
	int direction, retval = -EIO, bytes;

	if (!(adapter->adapter_features & FSF_FEATURE_CFDC))
		return ERR_PTR(-EOPNOTSUPP);

	switch (fsf_cfdc->command) {
	case FSF_QTCB_DOWNLOAD_CONTROL_FILE:
		direction = SBAL_FLAGS0_TYPE_WRITE;
		break;
	case FSF_QTCB_UPLOAD_CONTROL_FILE:
		direction = SBAL_FLAGS0_TYPE_READ;
		break;
	default:
		return ERR_PTR(-EINVAL);
	}
L
Linus Torvalds 已提交
2515

2516
	spin_lock_bh(&adapter->req_q_lock);
S
Swen Schillig 已提交
2517 2518
	if (zfcp_fsf_req_sbal_get(adapter))
		goto out;
L
Linus Torvalds 已提交
2519

S
Swen Schillig 已提交
2520
	req = zfcp_fsf_req_create(adapter, fsf_cfdc->command, 0, NULL);
2521
	if (IS_ERR(req)) {
S
Swen Schillig 已提交
2522 2523 2524
		retval = -EPERM;
		goto out;
	}
L
Linus Torvalds 已提交
2525

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

S
Swen Schillig 已提交
2528 2529
	sbale = zfcp_qdio_sbale_req(req);
	sbale[0].flags |= direction;
2530

S
Swen Schillig 已提交
2531 2532 2533
	bottom = &req->qtcb->bottom.support;
	bottom->operation_subtype = FSF_CFDC_OPERATION_SUBTYPE;
	bottom->option = fsf_cfdc->option;
2534

S
Swen Schillig 已提交
2535 2536 2537 2538 2539 2540 2541
	bytes = zfcp_qdio_sbals_from_sg(req, direction, fsf_cfdc->sg,
					FSF_MAX_SBALS_PER_REQ);
	if (bytes != ZFCP_CFDC_MAX_SIZE) {
		retval = -ENOMEM;
		zfcp_fsf_req_free(req);
		goto out;
	}
L
Linus Torvalds 已提交
2542

S
Swen Schillig 已提交
2543 2544 2545
	zfcp_fsf_start_timer(req, ZFCP_FSF_REQUEST_TIMEOUT);
	retval = zfcp_fsf_req_send(req);
out:
2546
	spin_unlock_bh(&adapter->req_q_lock);
2547

S
Swen Schillig 已提交
2548 2549 2550 2551
	if (!retval) {
		wait_event(req->completion_wq,
			   req->status & ZFCP_STATUS_FSFREQ_COMPLETED);
		return req;
L
Linus Torvalds 已提交
2552
	}
S
Swen Schillig 已提交
2553
	return ERR_PTR(retval);
L
Linus Torvalds 已提交
2554
}