xref: /openbmc/linux/drivers/s390/scsi/zfcp_fsf.c (revision ba61bb17)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * zfcp device driver
4  *
5  * Implementation of FSF commands.
6  *
7  * Copyright IBM Corp. 2002, 2018
8  */
9 
10 #define KMSG_COMPONENT "zfcp"
11 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
12 
13 #include <linux/blktrace_api.h>
14 #include <linux/slab.h>
15 #include <scsi/fc/fc_els.h>
16 #include "zfcp_ext.h"
17 #include "zfcp_fc.h"
18 #include "zfcp_dbf.h"
19 #include "zfcp_qdio.h"
20 #include "zfcp_reqlist.h"
21 
22 struct kmem_cache *zfcp_fsf_qtcb_cache;
23 
24 static void zfcp_fsf_request_timeout_handler(struct timer_list *t)
25 {
26 	struct zfcp_fsf_req *fsf_req = from_timer(fsf_req, t, timer);
27 	struct zfcp_adapter *adapter = fsf_req->adapter;
28 
29 	zfcp_qdio_siosl(adapter);
30 	zfcp_erp_adapter_reopen(adapter, ZFCP_STATUS_COMMON_ERP_FAILED,
31 				"fsrth_1");
32 }
33 
34 static void zfcp_fsf_start_timer(struct zfcp_fsf_req *fsf_req,
35 				 unsigned long timeout)
36 {
37 	fsf_req->timer.function = zfcp_fsf_request_timeout_handler;
38 	fsf_req->timer.expires = jiffies + timeout;
39 	add_timer(&fsf_req->timer);
40 }
41 
42 static void zfcp_fsf_start_erp_timer(struct zfcp_fsf_req *fsf_req)
43 {
44 	BUG_ON(!fsf_req->erp_action);
45 	fsf_req->timer.function = zfcp_erp_timeout_handler;
46 	fsf_req->timer.expires = jiffies + 30 * HZ;
47 	add_timer(&fsf_req->timer);
48 }
49 
50 /* association between FSF command and FSF QTCB type */
51 static u32 fsf_qtcb_type[] = {
52 	[FSF_QTCB_FCP_CMND] =             FSF_IO_COMMAND,
53 	[FSF_QTCB_ABORT_FCP_CMND] =       FSF_SUPPORT_COMMAND,
54 	[FSF_QTCB_OPEN_PORT_WITH_DID] =   FSF_SUPPORT_COMMAND,
55 	[FSF_QTCB_OPEN_LUN] =             FSF_SUPPORT_COMMAND,
56 	[FSF_QTCB_CLOSE_LUN] =            FSF_SUPPORT_COMMAND,
57 	[FSF_QTCB_CLOSE_PORT] =           FSF_SUPPORT_COMMAND,
58 	[FSF_QTCB_CLOSE_PHYSICAL_PORT] =  FSF_SUPPORT_COMMAND,
59 	[FSF_QTCB_SEND_ELS] =             FSF_SUPPORT_COMMAND,
60 	[FSF_QTCB_SEND_GENERIC] =         FSF_SUPPORT_COMMAND,
61 	[FSF_QTCB_EXCHANGE_CONFIG_DATA] = FSF_CONFIG_COMMAND,
62 	[FSF_QTCB_EXCHANGE_PORT_DATA] =   FSF_PORT_COMMAND,
63 	[FSF_QTCB_DOWNLOAD_CONTROL_FILE] = FSF_SUPPORT_COMMAND,
64 	[FSF_QTCB_UPLOAD_CONTROL_FILE] =  FSF_SUPPORT_COMMAND
65 };
66 
67 static void zfcp_fsf_class_not_supp(struct zfcp_fsf_req *req)
68 {
69 	dev_err(&req->adapter->ccw_device->dev, "FCP device not "
70 		"operational because of an unsupported FC class\n");
71 	zfcp_erp_adapter_shutdown(req->adapter, 0, "fscns_1");
72 	req->status |= ZFCP_STATUS_FSFREQ_ERROR;
73 }
74 
75 /**
76  * zfcp_fsf_req_free - free memory used by fsf request
77  * @fsf_req: pointer to struct zfcp_fsf_req
78  */
79 void zfcp_fsf_req_free(struct zfcp_fsf_req *req)
80 {
81 	if (likely(req->pool)) {
82 		if (likely(req->qtcb))
83 			mempool_free(req->qtcb, req->adapter->pool.qtcb_pool);
84 		mempool_free(req, req->pool);
85 		return;
86 	}
87 
88 	if (likely(req->qtcb))
89 		kmem_cache_free(zfcp_fsf_qtcb_cache, req->qtcb);
90 	kfree(req);
91 }
92 
93 static void zfcp_fsf_status_read_port_closed(struct zfcp_fsf_req *req)
94 {
95 	unsigned long flags;
96 	struct fsf_status_read_buffer *sr_buf = req->data;
97 	struct zfcp_adapter *adapter = req->adapter;
98 	struct zfcp_port *port;
99 	int d_id = ntoh24(sr_buf->d_id);
100 
101 	read_lock_irqsave(&adapter->port_list_lock, flags);
102 	list_for_each_entry(port, &adapter->port_list, list)
103 		if (port->d_id == d_id) {
104 			zfcp_erp_port_reopen(port, 0, "fssrpc1");
105 			break;
106 		}
107 	read_unlock_irqrestore(&adapter->port_list_lock, flags);
108 }
109 
110 static void zfcp_fsf_link_down_info_eval(struct zfcp_fsf_req *req,
111 					 struct fsf_link_down_info *link_down)
112 {
113 	struct zfcp_adapter *adapter = req->adapter;
114 
115 	if (atomic_read(&adapter->status) & ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED)
116 		return;
117 
118 	atomic_or(ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED, &adapter->status);
119 
120 	zfcp_scsi_schedule_rports_block(adapter);
121 
122 	if (!link_down)
123 		goto out;
124 
125 	switch (link_down->error_code) {
126 	case FSF_PSQ_LINK_NO_LIGHT:
127 		dev_warn(&req->adapter->ccw_device->dev,
128 			 "There is no light signal from the local "
129 			 "fibre channel cable\n");
130 		break;
131 	case FSF_PSQ_LINK_WRAP_PLUG:
132 		dev_warn(&req->adapter->ccw_device->dev,
133 			 "There is a wrap plug instead of a fibre "
134 			 "channel cable\n");
135 		break;
136 	case FSF_PSQ_LINK_NO_FCP:
137 		dev_warn(&req->adapter->ccw_device->dev,
138 			 "The adjacent fibre channel node does not "
139 			 "support FCP\n");
140 		break;
141 	case FSF_PSQ_LINK_FIRMWARE_UPDATE:
142 		dev_warn(&req->adapter->ccw_device->dev,
143 			 "The FCP device is suspended because of a "
144 			 "firmware update\n");
145 		break;
146 	case FSF_PSQ_LINK_INVALID_WWPN:
147 		dev_warn(&req->adapter->ccw_device->dev,
148 			 "The FCP device detected a WWPN that is "
149 			 "duplicate or not valid\n");
150 		break;
151 	case FSF_PSQ_LINK_NO_NPIV_SUPPORT:
152 		dev_warn(&req->adapter->ccw_device->dev,
153 			 "The fibre channel fabric does not support NPIV\n");
154 		break;
155 	case FSF_PSQ_LINK_NO_FCP_RESOURCES:
156 		dev_warn(&req->adapter->ccw_device->dev,
157 			 "The FCP adapter cannot support more NPIV ports\n");
158 		break;
159 	case FSF_PSQ_LINK_NO_FABRIC_RESOURCES:
160 		dev_warn(&req->adapter->ccw_device->dev,
161 			 "The adjacent switch cannot support "
162 			 "more NPIV ports\n");
163 		break;
164 	case FSF_PSQ_LINK_FABRIC_LOGIN_UNABLE:
165 		dev_warn(&req->adapter->ccw_device->dev,
166 			 "The FCP adapter could not log in to the "
167 			 "fibre channel fabric\n");
168 		break;
169 	case FSF_PSQ_LINK_WWPN_ASSIGNMENT_CORRUPTED:
170 		dev_warn(&req->adapter->ccw_device->dev,
171 			 "The WWPN assignment file on the FCP adapter "
172 			 "has been damaged\n");
173 		break;
174 	case FSF_PSQ_LINK_MODE_TABLE_CURRUPTED:
175 		dev_warn(&req->adapter->ccw_device->dev,
176 			 "The mode table on the FCP adapter "
177 			 "has been damaged\n");
178 		break;
179 	case FSF_PSQ_LINK_NO_WWPN_ASSIGNMENT:
180 		dev_warn(&req->adapter->ccw_device->dev,
181 			 "All NPIV ports on the FCP adapter have "
182 			 "been assigned\n");
183 		break;
184 	default:
185 		dev_warn(&req->adapter->ccw_device->dev,
186 			 "The link between the FCP adapter and "
187 			 "the FC fabric is down\n");
188 	}
189 out:
190 	zfcp_erp_set_adapter_status(adapter, ZFCP_STATUS_COMMON_ERP_FAILED);
191 }
192 
193 static void zfcp_fsf_status_read_link_down(struct zfcp_fsf_req *req)
194 {
195 	struct fsf_status_read_buffer *sr_buf = req->data;
196 	struct fsf_link_down_info *ldi =
197 		(struct fsf_link_down_info *) &sr_buf->payload;
198 
199 	switch (sr_buf->status_subtype) {
200 	case FSF_STATUS_READ_SUB_NO_PHYSICAL_LINK:
201 	case FSF_STATUS_READ_SUB_FDISC_FAILED:
202 		zfcp_fsf_link_down_info_eval(req, ldi);
203 		break;
204 	case FSF_STATUS_READ_SUB_FIRMWARE_UPDATE:
205 		zfcp_fsf_link_down_info_eval(req, NULL);
206 	}
207 }
208 
209 static void zfcp_fsf_status_read_handler(struct zfcp_fsf_req *req)
210 {
211 	struct zfcp_adapter *adapter = req->adapter;
212 	struct fsf_status_read_buffer *sr_buf = req->data;
213 
214 	if (req->status & ZFCP_STATUS_FSFREQ_DISMISSED) {
215 		zfcp_dbf_hba_fsf_uss("fssrh_1", req);
216 		mempool_free(virt_to_page(sr_buf), adapter->pool.sr_data);
217 		zfcp_fsf_req_free(req);
218 		return;
219 	}
220 
221 	zfcp_dbf_hba_fsf_uss("fssrh_4", req);
222 
223 	switch (sr_buf->status_type) {
224 	case FSF_STATUS_READ_PORT_CLOSED:
225 		zfcp_fsf_status_read_port_closed(req);
226 		break;
227 	case FSF_STATUS_READ_INCOMING_ELS:
228 		zfcp_fc_incoming_els(req);
229 		break;
230 	case FSF_STATUS_READ_SENSE_DATA_AVAIL:
231 		break;
232 	case FSF_STATUS_READ_BIT_ERROR_THRESHOLD:
233 		dev_warn(&adapter->ccw_device->dev,
234 			 "The error threshold for checksum statistics "
235 			 "has been exceeded\n");
236 		zfcp_dbf_hba_bit_err("fssrh_3", req);
237 		break;
238 	case FSF_STATUS_READ_LINK_DOWN:
239 		zfcp_fsf_status_read_link_down(req);
240 		zfcp_fc_enqueue_event(adapter, FCH_EVT_LINKDOWN, 0);
241 		break;
242 	case FSF_STATUS_READ_LINK_UP:
243 		dev_info(&adapter->ccw_device->dev,
244 			 "The local link has been restored\n");
245 		/* All ports should be marked as ready to run again */
246 		zfcp_erp_set_adapter_status(adapter,
247 					    ZFCP_STATUS_COMMON_RUNNING);
248 		zfcp_erp_adapter_reopen(adapter,
249 					ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED |
250 					ZFCP_STATUS_COMMON_ERP_FAILED,
251 					"fssrh_2");
252 		zfcp_fc_enqueue_event(adapter, FCH_EVT_LINKUP, 0);
253 
254 		break;
255 	case FSF_STATUS_READ_NOTIFICATION_LOST:
256 		if (sr_buf->status_subtype & FSF_STATUS_READ_SUB_INCOMING_ELS)
257 			zfcp_fc_conditional_port_scan(adapter);
258 		break;
259 	case FSF_STATUS_READ_FEATURE_UPDATE_ALERT:
260 		adapter->adapter_features = sr_buf->payload.word[0];
261 		break;
262 	}
263 
264 	mempool_free(virt_to_page(sr_buf), adapter->pool.sr_data);
265 	zfcp_fsf_req_free(req);
266 
267 	atomic_inc(&adapter->stat_miss);
268 	queue_work(adapter->work_queue, &adapter->stat_work);
269 }
270 
271 static void zfcp_fsf_fsfstatus_qual_eval(struct zfcp_fsf_req *req)
272 {
273 	switch (req->qtcb->header.fsf_status_qual.word[0]) {
274 	case FSF_SQ_FCP_RSP_AVAILABLE:
275 	case FSF_SQ_INVOKE_LINK_TEST_PROCEDURE:
276 	case FSF_SQ_NO_RETRY_POSSIBLE:
277 	case FSF_SQ_ULP_DEPENDENT_ERP_REQUIRED:
278 		return;
279 	case FSF_SQ_COMMAND_ABORTED:
280 		break;
281 	case FSF_SQ_NO_RECOM:
282 		dev_err(&req->adapter->ccw_device->dev,
283 			"The FCP adapter reported a problem "
284 			"that cannot be recovered\n");
285 		zfcp_qdio_siosl(req->adapter);
286 		zfcp_erp_adapter_shutdown(req->adapter, 0, "fsfsqe1");
287 		break;
288 	}
289 	/* all non-return stats set FSFREQ_ERROR*/
290 	req->status |= ZFCP_STATUS_FSFREQ_ERROR;
291 }
292 
293 static void zfcp_fsf_fsfstatus_eval(struct zfcp_fsf_req *req)
294 {
295 	if (unlikely(req->status & ZFCP_STATUS_FSFREQ_ERROR))
296 		return;
297 
298 	switch (req->qtcb->header.fsf_status) {
299 	case FSF_UNKNOWN_COMMAND:
300 		dev_err(&req->adapter->ccw_device->dev,
301 			"The FCP adapter does not recognize the command 0x%x\n",
302 			req->qtcb->header.fsf_command);
303 		zfcp_erp_adapter_shutdown(req->adapter, 0, "fsfse_1");
304 		req->status |= ZFCP_STATUS_FSFREQ_ERROR;
305 		break;
306 	case FSF_ADAPTER_STATUS_AVAILABLE:
307 		zfcp_fsf_fsfstatus_qual_eval(req);
308 		break;
309 	}
310 }
311 
312 static void zfcp_fsf_protstatus_eval(struct zfcp_fsf_req *req)
313 {
314 	struct zfcp_adapter *adapter = req->adapter;
315 	struct fsf_qtcb *qtcb = req->qtcb;
316 	union fsf_prot_status_qual *psq = &qtcb->prefix.prot_status_qual;
317 
318 	zfcp_dbf_hba_fsf_response(req);
319 
320 	if (req->status & ZFCP_STATUS_FSFREQ_DISMISSED) {
321 		req->status |= ZFCP_STATUS_FSFREQ_ERROR;
322 		return;
323 	}
324 
325 	switch (qtcb->prefix.prot_status) {
326 	case FSF_PROT_GOOD:
327 	case FSF_PROT_FSF_STATUS_PRESENTED:
328 		return;
329 	case FSF_PROT_QTCB_VERSION_ERROR:
330 		dev_err(&adapter->ccw_device->dev,
331 			"QTCB version 0x%x not supported by FCP adapter "
332 			"(0x%x to 0x%x)\n", FSF_QTCB_CURRENT_VERSION,
333 			psq->word[0], psq->word[1]);
334 		zfcp_erp_adapter_shutdown(adapter, 0, "fspse_1");
335 		break;
336 	case FSF_PROT_ERROR_STATE:
337 	case FSF_PROT_SEQ_NUMB_ERROR:
338 		zfcp_erp_adapter_reopen(adapter, 0, "fspse_2");
339 		req->status |= ZFCP_STATUS_FSFREQ_ERROR;
340 		break;
341 	case FSF_PROT_UNSUPP_QTCB_TYPE:
342 		dev_err(&adapter->ccw_device->dev,
343 			"The QTCB type is not supported by the FCP adapter\n");
344 		zfcp_erp_adapter_shutdown(adapter, 0, "fspse_3");
345 		break;
346 	case FSF_PROT_HOST_CONNECTION_INITIALIZING:
347 		atomic_or(ZFCP_STATUS_ADAPTER_HOST_CON_INIT,
348 				&adapter->status);
349 		break;
350 	case FSF_PROT_DUPLICATE_REQUEST_ID:
351 		dev_err(&adapter->ccw_device->dev,
352 			"0x%Lx is an ambiguous request identifier\n",
353 			(unsigned long long)qtcb->bottom.support.req_handle);
354 		zfcp_erp_adapter_shutdown(adapter, 0, "fspse_4");
355 		break;
356 	case FSF_PROT_LINK_DOWN:
357 		zfcp_fsf_link_down_info_eval(req, &psq->link_down_info);
358 		/* go through reopen to flush pending requests */
359 		zfcp_erp_adapter_reopen(adapter, 0, "fspse_6");
360 		break;
361 	case FSF_PROT_REEST_QUEUE:
362 		/* All ports should be marked as ready to run again */
363 		zfcp_erp_set_adapter_status(adapter,
364 					    ZFCP_STATUS_COMMON_RUNNING);
365 		zfcp_erp_adapter_reopen(adapter,
366 					ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED |
367 					ZFCP_STATUS_COMMON_ERP_FAILED,
368 					"fspse_8");
369 		break;
370 	default:
371 		dev_err(&adapter->ccw_device->dev,
372 			"0x%x is not a valid transfer protocol status\n",
373 			qtcb->prefix.prot_status);
374 		zfcp_qdio_siosl(adapter);
375 		zfcp_erp_adapter_shutdown(adapter, 0, "fspse_9");
376 	}
377 	req->status |= ZFCP_STATUS_FSFREQ_ERROR;
378 }
379 
380 /**
381  * zfcp_fsf_req_complete - process completion of a FSF request
382  * @fsf_req: The FSF request that has been completed.
383  *
384  * When a request has been completed either from the FCP adapter,
385  * or it has been dismissed due to a queue shutdown, this function
386  * is called to process the completion status and trigger further
387  * events related to the FSF request.
388  */
389 static void zfcp_fsf_req_complete(struct zfcp_fsf_req *req)
390 {
391 	if (unlikely(req->fsf_command == FSF_QTCB_UNSOLICITED_STATUS)) {
392 		zfcp_fsf_status_read_handler(req);
393 		return;
394 	}
395 
396 	del_timer(&req->timer);
397 	zfcp_fsf_protstatus_eval(req);
398 	zfcp_fsf_fsfstatus_eval(req);
399 	req->handler(req);
400 
401 	if (req->erp_action)
402 		zfcp_erp_notify(req->erp_action, 0);
403 
404 	if (likely(req->status & ZFCP_STATUS_FSFREQ_CLEANUP))
405 		zfcp_fsf_req_free(req);
406 	else
407 		complete(&req->completion);
408 }
409 
410 /**
411  * zfcp_fsf_req_dismiss_all - dismiss all fsf requests
412  * @adapter: pointer to struct zfcp_adapter
413  *
414  * Never ever call this without shutting down the adapter first.
415  * Otherwise the adapter would continue using and corrupting s390 storage.
416  * Included BUG_ON() call to ensure this is done.
417  * ERP is supposed to be the only user of this function.
418  */
419 void zfcp_fsf_req_dismiss_all(struct zfcp_adapter *adapter)
420 {
421 	struct zfcp_fsf_req *req, *tmp;
422 	LIST_HEAD(remove_queue);
423 
424 	BUG_ON(atomic_read(&adapter->status) & ZFCP_STATUS_ADAPTER_QDIOUP);
425 	zfcp_reqlist_move(adapter->req_list, &remove_queue);
426 
427 	list_for_each_entry_safe(req, tmp, &remove_queue, list) {
428 		list_del(&req->list);
429 		req->status |= ZFCP_STATUS_FSFREQ_DISMISSED;
430 		zfcp_fsf_req_complete(req);
431 	}
432 }
433 
434 #define ZFCP_FSF_PORTSPEED_1GBIT	(1 <<  0)
435 #define ZFCP_FSF_PORTSPEED_2GBIT	(1 <<  1)
436 #define ZFCP_FSF_PORTSPEED_4GBIT	(1 <<  2)
437 #define ZFCP_FSF_PORTSPEED_10GBIT	(1 <<  3)
438 #define ZFCP_FSF_PORTSPEED_8GBIT	(1 <<  4)
439 #define ZFCP_FSF_PORTSPEED_16GBIT	(1 <<  5)
440 #define ZFCP_FSF_PORTSPEED_32GBIT	(1 <<  6)
441 #define ZFCP_FSF_PORTSPEED_64GBIT	(1 <<  7)
442 #define ZFCP_FSF_PORTSPEED_128GBIT	(1 <<  8)
443 #define ZFCP_FSF_PORTSPEED_NOT_NEGOTIATED (1 << 15)
444 
445 static u32 zfcp_fsf_convert_portspeed(u32 fsf_speed)
446 {
447 	u32 fdmi_speed = 0;
448 	if (fsf_speed & ZFCP_FSF_PORTSPEED_1GBIT)
449 		fdmi_speed |= FC_PORTSPEED_1GBIT;
450 	if (fsf_speed & ZFCP_FSF_PORTSPEED_2GBIT)
451 		fdmi_speed |= FC_PORTSPEED_2GBIT;
452 	if (fsf_speed & ZFCP_FSF_PORTSPEED_4GBIT)
453 		fdmi_speed |= FC_PORTSPEED_4GBIT;
454 	if (fsf_speed & ZFCP_FSF_PORTSPEED_10GBIT)
455 		fdmi_speed |= FC_PORTSPEED_10GBIT;
456 	if (fsf_speed & ZFCP_FSF_PORTSPEED_8GBIT)
457 		fdmi_speed |= FC_PORTSPEED_8GBIT;
458 	if (fsf_speed & ZFCP_FSF_PORTSPEED_16GBIT)
459 		fdmi_speed |= FC_PORTSPEED_16GBIT;
460 	if (fsf_speed & ZFCP_FSF_PORTSPEED_32GBIT)
461 		fdmi_speed |= FC_PORTSPEED_32GBIT;
462 	if (fsf_speed & ZFCP_FSF_PORTSPEED_64GBIT)
463 		fdmi_speed |= FC_PORTSPEED_64GBIT;
464 	if (fsf_speed & ZFCP_FSF_PORTSPEED_128GBIT)
465 		fdmi_speed |= FC_PORTSPEED_128GBIT;
466 	if (fsf_speed & ZFCP_FSF_PORTSPEED_NOT_NEGOTIATED)
467 		fdmi_speed |= FC_PORTSPEED_NOT_NEGOTIATED;
468 	return fdmi_speed;
469 }
470 
471 static int zfcp_fsf_exchange_config_evaluate(struct zfcp_fsf_req *req)
472 {
473 	struct fsf_qtcb_bottom_config *bottom = &req->qtcb->bottom.config;
474 	struct zfcp_adapter *adapter = req->adapter;
475 	struct Scsi_Host *shost = adapter->scsi_host;
476 	struct fc_els_flogi *nsp, *plogi;
477 
478 	/* adjust pointers for missing command code */
479 	nsp = (struct fc_els_flogi *) ((u8 *)&bottom->nport_serv_param
480 					- sizeof(u32));
481 	plogi = (struct fc_els_flogi *) ((u8 *)&bottom->plogi_payload
482 					- sizeof(u32));
483 
484 	if (req->data)
485 		memcpy(req->data, bottom, sizeof(*bottom));
486 
487 	fc_host_port_name(shost) = be64_to_cpu(nsp->fl_wwpn);
488 	fc_host_node_name(shost) = be64_to_cpu(nsp->fl_wwnn);
489 	fc_host_supported_classes(shost) = FC_COS_CLASS2 | FC_COS_CLASS3;
490 
491 	adapter->timer_ticks = bottom->timer_interval & ZFCP_FSF_TIMER_INT_MASK;
492 	adapter->stat_read_buf_num = max(bottom->status_read_buf_num,
493 					 (u16)FSF_STATUS_READS_RECOM);
494 
495 	if (fc_host_permanent_port_name(shost) == -1)
496 		fc_host_permanent_port_name(shost) = fc_host_port_name(shost);
497 
498 	zfcp_scsi_set_prot(adapter);
499 
500 	/* no error return above here, otherwise must fix call chains */
501 	/* do not evaluate invalid fields */
502 	if (req->qtcb->header.fsf_status == FSF_EXCHANGE_CONFIG_DATA_INCOMPLETE)
503 		return 0;
504 
505 	fc_host_port_id(shost) = ntoh24(bottom->s_id);
506 	fc_host_speed(shost) =
507 		zfcp_fsf_convert_portspeed(bottom->fc_link_speed);
508 
509 	adapter->hydra_version = bottom->adapter_type;
510 
511 	switch (bottom->fc_topology) {
512 	case FSF_TOPO_P2P:
513 		adapter->peer_d_id = ntoh24(bottom->peer_d_id);
514 		adapter->peer_wwpn = be64_to_cpu(plogi->fl_wwpn);
515 		adapter->peer_wwnn = be64_to_cpu(plogi->fl_wwnn);
516 		fc_host_port_type(shost) = FC_PORTTYPE_PTP;
517 		break;
518 	case FSF_TOPO_FABRIC:
519 		if (bottom->connection_features & FSF_FEATURE_NPIV_MODE)
520 			fc_host_port_type(shost) = FC_PORTTYPE_NPIV;
521 		else
522 			fc_host_port_type(shost) = FC_PORTTYPE_NPORT;
523 		break;
524 	case FSF_TOPO_AL:
525 		fc_host_port_type(shost) = FC_PORTTYPE_NLPORT;
526 		/* fall through */
527 	default:
528 		dev_err(&adapter->ccw_device->dev,
529 			"Unknown or unsupported arbitrated loop "
530 			"fibre channel topology detected\n");
531 		zfcp_erp_adapter_shutdown(adapter, 0, "fsece_1");
532 		return -EIO;
533 	}
534 
535 	return 0;
536 }
537 
538 static void zfcp_fsf_exchange_config_data_handler(struct zfcp_fsf_req *req)
539 {
540 	struct zfcp_adapter *adapter = req->adapter;
541 	struct fsf_qtcb *qtcb = req->qtcb;
542 	struct fsf_qtcb_bottom_config *bottom = &qtcb->bottom.config;
543 	struct Scsi_Host *shost = adapter->scsi_host;
544 
545 	if (req->status & ZFCP_STATUS_FSFREQ_ERROR)
546 		return;
547 
548 	adapter->fsf_lic_version = bottom->lic_version;
549 	adapter->adapter_features = bottom->adapter_features;
550 	adapter->connection_features = bottom->connection_features;
551 	adapter->peer_wwpn = 0;
552 	adapter->peer_wwnn = 0;
553 	adapter->peer_d_id = 0;
554 
555 	switch (qtcb->header.fsf_status) {
556 	case FSF_GOOD:
557 		if (zfcp_fsf_exchange_config_evaluate(req))
558 			return;
559 
560 		if (bottom->max_qtcb_size < sizeof(struct fsf_qtcb)) {
561 			dev_err(&adapter->ccw_device->dev,
562 				"FCP adapter maximum QTCB size (%d bytes) "
563 				"is too small\n",
564 				bottom->max_qtcb_size);
565 			zfcp_erp_adapter_shutdown(adapter, 0, "fsecdh1");
566 			return;
567 		}
568 		atomic_or(ZFCP_STATUS_ADAPTER_XCONFIG_OK,
569 				&adapter->status);
570 		break;
571 	case FSF_EXCHANGE_CONFIG_DATA_INCOMPLETE:
572 		fc_host_node_name(shost) = 0;
573 		fc_host_port_name(shost) = 0;
574 		fc_host_port_id(shost) = 0;
575 		fc_host_speed(shost) = FC_PORTSPEED_UNKNOWN;
576 		fc_host_port_type(shost) = FC_PORTTYPE_UNKNOWN;
577 		adapter->hydra_version = 0;
578 
579 		/* avoids adapter shutdown to be able to recognize
580 		 * events such as LINK UP */
581 		atomic_or(ZFCP_STATUS_ADAPTER_XCONFIG_OK,
582 				&adapter->status);
583 		zfcp_fsf_link_down_info_eval(req,
584 			&qtcb->header.fsf_status_qual.link_down_info);
585 		if (zfcp_fsf_exchange_config_evaluate(req))
586 			return;
587 		break;
588 	default:
589 		zfcp_erp_adapter_shutdown(adapter, 0, "fsecdh3");
590 		return;
591 	}
592 
593 	if (adapter->adapter_features & FSF_FEATURE_HBAAPI_MANAGEMENT) {
594 		adapter->hardware_version = bottom->hardware_version;
595 		memcpy(fc_host_serial_number(shost), bottom->serial_number,
596 		       min(FC_SERIAL_NUMBER_SIZE, 17));
597 		EBCASC(fc_host_serial_number(shost),
598 		       min(FC_SERIAL_NUMBER_SIZE, 17));
599 	}
600 
601 	if (FSF_QTCB_CURRENT_VERSION < bottom->low_qtcb_version) {
602 		dev_err(&adapter->ccw_device->dev,
603 			"The FCP adapter only supports newer "
604 			"control block versions\n");
605 		zfcp_erp_adapter_shutdown(adapter, 0, "fsecdh4");
606 		return;
607 	}
608 	if (FSF_QTCB_CURRENT_VERSION > bottom->high_qtcb_version) {
609 		dev_err(&adapter->ccw_device->dev,
610 			"The FCP adapter only supports older "
611 			"control block versions\n");
612 		zfcp_erp_adapter_shutdown(adapter, 0, "fsecdh5");
613 	}
614 }
615 
616 static void zfcp_fsf_exchange_port_evaluate(struct zfcp_fsf_req *req)
617 {
618 	struct zfcp_adapter *adapter = req->adapter;
619 	struct fsf_qtcb_bottom_port *bottom = &req->qtcb->bottom.port;
620 	struct Scsi_Host *shost = adapter->scsi_host;
621 
622 	if (req->data)
623 		memcpy(req->data, bottom, sizeof(*bottom));
624 
625 	if (adapter->connection_features & FSF_FEATURE_NPIV_MODE) {
626 		fc_host_permanent_port_name(shost) = bottom->wwpn;
627 	} else
628 		fc_host_permanent_port_name(shost) = fc_host_port_name(shost);
629 	fc_host_maxframe_size(shost) = bottom->maximum_frame_size;
630 	fc_host_supported_speeds(shost) =
631 		zfcp_fsf_convert_portspeed(bottom->supported_speed);
632 	memcpy(fc_host_supported_fc4s(shost), bottom->supported_fc4_types,
633 	       FC_FC4_LIST_SIZE);
634 	memcpy(fc_host_active_fc4s(shost), bottom->active_fc4_types,
635 	       FC_FC4_LIST_SIZE);
636 }
637 
638 static void zfcp_fsf_exchange_port_data_handler(struct zfcp_fsf_req *req)
639 {
640 	struct fsf_qtcb *qtcb = req->qtcb;
641 
642 	if (req->status & ZFCP_STATUS_FSFREQ_ERROR)
643 		return;
644 
645 	switch (qtcb->header.fsf_status) {
646 	case FSF_GOOD:
647 		zfcp_fsf_exchange_port_evaluate(req);
648 		break;
649 	case FSF_EXCHANGE_CONFIG_DATA_INCOMPLETE:
650 		zfcp_fsf_exchange_port_evaluate(req);
651 		zfcp_fsf_link_down_info_eval(req,
652 			&qtcb->header.fsf_status_qual.link_down_info);
653 		break;
654 	}
655 }
656 
657 static struct zfcp_fsf_req *zfcp_fsf_alloc(mempool_t *pool)
658 {
659 	struct zfcp_fsf_req *req;
660 
661 	if (likely(pool))
662 		req = mempool_alloc(pool, GFP_ATOMIC);
663 	else
664 		req = kmalloc(sizeof(*req), GFP_ATOMIC);
665 
666 	if (unlikely(!req))
667 		return NULL;
668 
669 	memset(req, 0, sizeof(*req));
670 	req->pool = pool;
671 	return req;
672 }
673 
674 static struct fsf_qtcb *zfcp_fsf_qtcb_alloc(mempool_t *pool)
675 {
676 	struct fsf_qtcb *qtcb;
677 
678 	if (likely(pool))
679 		qtcb = mempool_alloc(pool, GFP_ATOMIC);
680 	else
681 		qtcb = kmem_cache_alloc(zfcp_fsf_qtcb_cache, GFP_ATOMIC);
682 
683 	if (unlikely(!qtcb))
684 		return NULL;
685 
686 	memset(qtcb, 0, sizeof(*qtcb));
687 	return qtcb;
688 }
689 
690 static struct zfcp_fsf_req *zfcp_fsf_req_create(struct zfcp_qdio *qdio,
691 						u32 fsf_cmd, u8 sbtype,
692 						mempool_t *pool)
693 {
694 	struct zfcp_adapter *adapter = qdio->adapter;
695 	struct zfcp_fsf_req *req = zfcp_fsf_alloc(pool);
696 
697 	if (unlikely(!req))
698 		return ERR_PTR(-ENOMEM);
699 
700 	if (adapter->req_no == 0)
701 		adapter->req_no++;
702 
703 	INIT_LIST_HEAD(&req->list);
704 	timer_setup(&req->timer, NULL, 0);
705 	init_completion(&req->completion);
706 
707 	req->adapter = adapter;
708 	req->fsf_command = fsf_cmd;
709 	req->req_id = adapter->req_no;
710 
711 	if (likely(fsf_cmd != FSF_QTCB_UNSOLICITED_STATUS)) {
712 		if (likely(pool))
713 			req->qtcb = zfcp_fsf_qtcb_alloc(
714 				adapter->pool.qtcb_pool);
715 		else
716 			req->qtcb = zfcp_fsf_qtcb_alloc(NULL);
717 
718 		if (unlikely(!req->qtcb)) {
719 			zfcp_fsf_req_free(req);
720 			return ERR_PTR(-ENOMEM);
721 		}
722 
723 		req->seq_no = adapter->fsf_req_seq_no;
724 		req->qtcb->prefix.req_seq_no = adapter->fsf_req_seq_no;
725 		req->qtcb->prefix.req_id = req->req_id;
726 		req->qtcb->prefix.ulp_info = 26;
727 		req->qtcb->prefix.qtcb_type = fsf_qtcb_type[req->fsf_command];
728 		req->qtcb->prefix.qtcb_version = FSF_QTCB_CURRENT_VERSION;
729 		req->qtcb->header.req_handle = req->req_id;
730 		req->qtcb->header.fsf_command = req->fsf_command;
731 	}
732 
733 	zfcp_qdio_req_init(adapter->qdio, &req->qdio_req, req->req_id, sbtype,
734 			   req->qtcb, sizeof(struct fsf_qtcb));
735 
736 	return req;
737 }
738 
739 static int zfcp_fsf_req_send(struct zfcp_fsf_req *req)
740 {
741 	struct zfcp_adapter *adapter = req->adapter;
742 	struct zfcp_qdio *qdio = adapter->qdio;
743 	int with_qtcb = (req->qtcb != NULL);
744 	int req_id = req->req_id;
745 
746 	zfcp_reqlist_add(adapter->req_list, req);
747 
748 	req->qdio_req.qdio_outb_usage = atomic_read(&qdio->req_q_free);
749 	req->issued = get_tod_clock();
750 	if (zfcp_qdio_send(qdio, &req->qdio_req)) {
751 		del_timer(&req->timer);
752 		/* lookup request again, list might have changed */
753 		zfcp_reqlist_find_rm(adapter->req_list, req_id);
754 		zfcp_erp_adapter_reopen(adapter, 0, "fsrs__1");
755 		return -EIO;
756 	}
757 
758 	/* Don't increase for unsolicited status */
759 	if (with_qtcb)
760 		adapter->fsf_req_seq_no++;
761 	adapter->req_no++;
762 
763 	return 0;
764 }
765 
766 /**
767  * zfcp_fsf_status_read - send status read request
768  * @adapter: pointer to struct zfcp_adapter
769  * @req_flags: request flags
770  * Returns: 0 on success, ERROR otherwise
771  */
772 int zfcp_fsf_status_read(struct zfcp_qdio *qdio)
773 {
774 	struct zfcp_adapter *adapter = qdio->adapter;
775 	struct zfcp_fsf_req *req;
776 	struct fsf_status_read_buffer *sr_buf;
777 	struct page *page;
778 	int retval = -EIO;
779 
780 	spin_lock_irq(&qdio->req_q_lock);
781 	if (zfcp_qdio_sbal_get(qdio))
782 		goto out;
783 
784 	req = zfcp_fsf_req_create(qdio, FSF_QTCB_UNSOLICITED_STATUS,
785 				  SBAL_SFLAGS0_TYPE_STATUS,
786 				  adapter->pool.status_read_req);
787 	if (IS_ERR(req)) {
788 		retval = PTR_ERR(req);
789 		goto out;
790 	}
791 
792 	page = mempool_alloc(adapter->pool.sr_data, GFP_ATOMIC);
793 	if (!page) {
794 		retval = -ENOMEM;
795 		goto failed_buf;
796 	}
797 	sr_buf = page_address(page);
798 	memset(sr_buf, 0, sizeof(*sr_buf));
799 	req->data = sr_buf;
800 
801 	zfcp_qdio_fill_next(qdio, &req->qdio_req, sr_buf, sizeof(*sr_buf));
802 	zfcp_qdio_set_sbale_last(qdio, &req->qdio_req);
803 
804 	retval = zfcp_fsf_req_send(req);
805 	if (retval)
806 		goto failed_req_send;
807 
808 	goto out;
809 
810 failed_req_send:
811 	req->data = NULL;
812 	mempool_free(virt_to_page(sr_buf), adapter->pool.sr_data);
813 failed_buf:
814 	zfcp_dbf_hba_fsf_uss("fssr__1", req);
815 	zfcp_fsf_req_free(req);
816 out:
817 	spin_unlock_irq(&qdio->req_q_lock);
818 	return retval;
819 }
820 
821 static void zfcp_fsf_abort_fcp_command_handler(struct zfcp_fsf_req *req)
822 {
823 	struct scsi_device *sdev = req->data;
824 	struct zfcp_scsi_dev *zfcp_sdev;
825 	union fsf_status_qual *fsq = &req->qtcb->header.fsf_status_qual;
826 
827 	if (req->status & ZFCP_STATUS_FSFREQ_ERROR)
828 		return;
829 
830 	zfcp_sdev = sdev_to_zfcp(sdev);
831 
832 	switch (req->qtcb->header.fsf_status) {
833 	case FSF_PORT_HANDLE_NOT_VALID:
834 		if (fsq->word[0] == fsq->word[1]) {
835 			zfcp_erp_adapter_reopen(zfcp_sdev->port->adapter, 0,
836 						"fsafch1");
837 			req->status |= ZFCP_STATUS_FSFREQ_ERROR;
838 		}
839 		break;
840 	case FSF_LUN_HANDLE_NOT_VALID:
841 		if (fsq->word[0] == fsq->word[1]) {
842 			zfcp_erp_port_reopen(zfcp_sdev->port, 0, "fsafch2");
843 			req->status |= ZFCP_STATUS_FSFREQ_ERROR;
844 		}
845 		break;
846 	case FSF_FCP_COMMAND_DOES_NOT_EXIST:
847 		req->status |= ZFCP_STATUS_FSFREQ_ABORTNOTNEEDED;
848 		break;
849 	case FSF_PORT_BOXED:
850 		zfcp_erp_set_port_status(zfcp_sdev->port,
851 					 ZFCP_STATUS_COMMON_ACCESS_BOXED);
852 		zfcp_erp_port_reopen(zfcp_sdev->port,
853 				     ZFCP_STATUS_COMMON_ERP_FAILED, "fsafch3");
854 		req->status |= ZFCP_STATUS_FSFREQ_ERROR;
855 		break;
856 	case FSF_LUN_BOXED:
857 		zfcp_erp_set_lun_status(sdev, ZFCP_STATUS_COMMON_ACCESS_BOXED);
858 		zfcp_erp_lun_reopen(sdev, ZFCP_STATUS_COMMON_ERP_FAILED,
859 				    "fsafch4");
860 		req->status |= ZFCP_STATUS_FSFREQ_ERROR;
861                 break;
862 	case FSF_ADAPTER_STATUS_AVAILABLE:
863 		switch (fsq->word[0]) {
864 		case FSF_SQ_INVOKE_LINK_TEST_PROCEDURE:
865 			zfcp_fc_test_link(zfcp_sdev->port);
866 			/* fall through */
867 		case FSF_SQ_ULP_DEPENDENT_ERP_REQUIRED:
868 			req->status |= ZFCP_STATUS_FSFREQ_ERROR;
869 			break;
870 		}
871 		break;
872 	case FSF_GOOD:
873 		req->status |= ZFCP_STATUS_FSFREQ_ABORTSUCCEEDED;
874 		break;
875 	}
876 }
877 
878 /**
879  * zfcp_fsf_abort_fcp_cmnd - abort running SCSI command
880  * @scmnd: The SCSI command to abort
881  * Returns: pointer to struct zfcp_fsf_req
882  */
883 
884 struct zfcp_fsf_req *zfcp_fsf_abort_fcp_cmnd(struct scsi_cmnd *scmnd)
885 {
886 	struct zfcp_fsf_req *req = NULL;
887 	struct scsi_device *sdev = scmnd->device;
888 	struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
889 	struct zfcp_qdio *qdio = zfcp_sdev->port->adapter->qdio;
890 	unsigned long old_req_id = (unsigned long) scmnd->host_scribble;
891 
892 	spin_lock_irq(&qdio->req_q_lock);
893 	if (zfcp_qdio_sbal_get(qdio))
894 		goto out;
895 	req = zfcp_fsf_req_create(qdio, FSF_QTCB_ABORT_FCP_CMND,
896 				  SBAL_SFLAGS0_TYPE_READ,
897 				  qdio->adapter->pool.scsi_abort);
898 	if (IS_ERR(req)) {
899 		req = NULL;
900 		goto out;
901 	}
902 
903 	if (unlikely(!(atomic_read(&zfcp_sdev->status) &
904 		       ZFCP_STATUS_COMMON_UNBLOCKED)))
905 		goto out_error_free;
906 
907 	zfcp_qdio_set_sbale_last(qdio, &req->qdio_req);
908 
909 	req->data = sdev;
910 	req->handler = zfcp_fsf_abort_fcp_command_handler;
911 	req->qtcb->header.lun_handle = zfcp_sdev->lun_handle;
912 	req->qtcb->header.port_handle = zfcp_sdev->port->handle;
913 	req->qtcb->bottom.support.req_handle = (u64) old_req_id;
914 
915 	zfcp_fsf_start_timer(req, ZFCP_SCSI_ER_TIMEOUT);
916 	if (!zfcp_fsf_req_send(req))
917 		goto out;
918 
919 out_error_free:
920 	zfcp_fsf_req_free(req);
921 	req = NULL;
922 out:
923 	spin_unlock_irq(&qdio->req_q_lock);
924 	return req;
925 }
926 
927 static void zfcp_fsf_send_ct_handler(struct zfcp_fsf_req *req)
928 {
929 	struct zfcp_adapter *adapter = req->adapter;
930 	struct zfcp_fsf_ct_els *ct = req->data;
931 	struct fsf_qtcb_header *header = &req->qtcb->header;
932 
933 	ct->status = -EINVAL;
934 
935 	if (req->status & ZFCP_STATUS_FSFREQ_ERROR)
936 		goto skip_fsfstatus;
937 
938 	switch (header->fsf_status) {
939         case FSF_GOOD:
940 		ct->status = 0;
941 		zfcp_dbf_san_res("fsscth2", req);
942 		break;
943         case FSF_SERVICE_CLASS_NOT_SUPPORTED:
944 		zfcp_fsf_class_not_supp(req);
945 		break;
946         case FSF_ADAPTER_STATUS_AVAILABLE:
947                 switch (header->fsf_status_qual.word[0]){
948                 case FSF_SQ_INVOKE_LINK_TEST_PROCEDURE:
949                 case FSF_SQ_ULP_DEPENDENT_ERP_REQUIRED:
950 			req->status |= ZFCP_STATUS_FSFREQ_ERROR;
951 			break;
952                 }
953                 break;
954         case FSF_PORT_BOXED:
955 		req->status |= ZFCP_STATUS_FSFREQ_ERROR;
956 		break;
957 	case FSF_PORT_HANDLE_NOT_VALID:
958 		zfcp_erp_adapter_reopen(adapter, 0, "fsscth1");
959 		/* fall through */
960 	case FSF_GENERIC_COMMAND_REJECTED:
961 	case FSF_PAYLOAD_SIZE_MISMATCH:
962 	case FSF_REQUEST_SIZE_TOO_LARGE:
963 	case FSF_RESPONSE_SIZE_TOO_LARGE:
964 	case FSF_SBAL_MISMATCH:
965 		req->status |= ZFCP_STATUS_FSFREQ_ERROR;
966 		break;
967 	}
968 
969 skip_fsfstatus:
970 	if (ct->handler)
971 		ct->handler(ct->handler_data);
972 }
973 
974 static void zfcp_fsf_setup_ct_els_unchained(struct zfcp_qdio *qdio,
975 					    struct zfcp_qdio_req *q_req,
976 					    struct scatterlist *sg_req,
977 					    struct scatterlist *sg_resp)
978 {
979 	zfcp_qdio_fill_next(qdio, q_req, sg_virt(sg_req), sg_req->length);
980 	zfcp_qdio_fill_next(qdio, q_req, sg_virt(sg_resp), sg_resp->length);
981 	zfcp_qdio_set_sbale_last(qdio, q_req);
982 }
983 
984 static int zfcp_fsf_setup_ct_els_sbals(struct zfcp_fsf_req *req,
985 				       struct scatterlist *sg_req,
986 				       struct scatterlist *sg_resp)
987 {
988 	struct zfcp_adapter *adapter = req->adapter;
989 	struct zfcp_qdio *qdio = adapter->qdio;
990 	struct fsf_qtcb *qtcb = req->qtcb;
991 	u32 feat = adapter->adapter_features;
992 
993 	if (zfcp_adapter_multi_buffer_active(adapter)) {
994 		if (zfcp_qdio_sbals_from_sg(qdio, &req->qdio_req, sg_req))
995 			return -EIO;
996 		qtcb->bottom.support.req_buf_length =
997 			zfcp_qdio_real_bytes(sg_req);
998 		if (zfcp_qdio_sbals_from_sg(qdio, &req->qdio_req, sg_resp))
999 			return -EIO;
1000 		qtcb->bottom.support.resp_buf_length =
1001 			zfcp_qdio_real_bytes(sg_resp);
1002 
1003 		zfcp_qdio_set_data_div(qdio, &req->qdio_req, sg_nents(sg_req));
1004 		zfcp_qdio_set_sbale_last(qdio, &req->qdio_req);
1005 		zfcp_qdio_set_scount(qdio, &req->qdio_req);
1006 		return 0;
1007 	}
1008 
1009 	/* use single, unchained SBAL if it can hold the request */
1010 	if (zfcp_qdio_sg_one_sbale(sg_req) && zfcp_qdio_sg_one_sbale(sg_resp)) {
1011 		zfcp_fsf_setup_ct_els_unchained(qdio, &req->qdio_req,
1012 						sg_req, sg_resp);
1013 		return 0;
1014 	}
1015 
1016 	if (!(feat & FSF_FEATURE_ELS_CT_CHAINED_SBALS))
1017 		return -EOPNOTSUPP;
1018 
1019 	if (zfcp_qdio_sbals_from_sg(qdio, &req->qdio_req, sg_req))
1020 		return -EIO;
1021 
1022 	qtcb->bottom.support.req_buf_length = zfcp_qdio_real_bytes(sg_req);
1023 
1024 	zfcp_qdio_set_sbale_last(qdio, &req->qdio_req);
1025 	zfcp_qdio_skip_to_last_sbale(qdio, &req->qdio_req);
1026 
1027 	if (zfcp_qdio_sbals_from_sg(qdio, &req->qdio_req, sg_resp))
1028 		return -EIO;
1029 
1030 	qtcb->bottom.support.resp_buf_length = zfcp_qdio_real_bytes(sg_resp);
1031 
1032 	zfcp_qdio_set_sbale_last(qdio, &req->qdio_req);
1033 
1034 	return 0;
1035 }
1036 
1037 static int zfcp_fsf_setup_ct_els(struct zfcp_fsf_req *req,
1038 				 struct scatterlist *sg_req,
1039 				 struct scatterlist *sg_resp,
1040 				 unsigned int timeout)
1041 {
1042 	int ret;
1043 
1044 	ret = zfcp_fsf_setup_ct_els_sbals(req, sg_req, sg_resp);
1045 	if (ret)
1046 		return ret;
1047 
1048 	/* common settings for ct/gs and els requests */
1049 	if (timeout > 255)
1050 		timeout = 255; /* max value accepted by hardware */
1051 	req->qtcb->bottom.support.service_class = FSF_CLASS_3;
1052 	req->qtcb->bottom.support.timeout = timeout;
1053 	zfcp_fsf_start_timer(req, (timeout + 10) * HZ);
1054 
1055 	return 0;
1056 }
1057 
1058 /**
1059  * zfcp_fsf_send_ct - initiate a Generic Service request (FC-GS)
1060  * @ct: pointer to struct zfcp_send_ct with data for request
1061  * @pool: if non-null this mempool is used to allocate struct zfcp_fsf_req
1062  */
1063 int zfcp_fsf_send_ct(struct zfcp_fc_wka_port *wka_port,
1064 		     struct zfcp_fsf_ct_els *ct, mempool_t *pool,
1065 		     unsigned int timeout)
1066 {
1067 	struct zfcp_qdio *qdio = wka_port->adapter->qdio;
1068 	struct zfcp_fsf_req *req;
1069 	int ret = -EIO;
1070 
1071 	spin_lock_irq(&qdio->req_q_lock);
1072 	if (zfcp_qdio_sbal_get(qdio))
1073 		goto out;
1074 
1075 	req = zfcp_fsf_req_create(qdio, FSF_QTCB_SEND_GENERIC,
1076 				  SBAL_SFLAGS0_TYPE_WRITE_READ, pool);
1077 
1078 	if (IS_ERR(req)) {
1079 		ret = PTR_ERR(req);
1080 		goto out;
1081 	}
1082 
1083 	req->status |= ZFCP_STATUS_FSFREQ_CLEANUP;
1084 	ret = zfcp_fsf_setup_ct_els(req, ct->req, ct->resp, timeout);
1085 	if (ret)
1086 		goto failed_send;
1087 
1088 	req->handler = zfcp_fsf_send_ct_handler;
1089 	req->qtcb->header.port_handle = wka_port->handle;
1090 	ct->d_id = wka_port->d_id;
1091 	req->data = ct;
1092 
1093 	zfcp_dbf_san_req("fssct_1", req, wka_port->d_id);
1094 
1095 	ret = zfcp_fsf_req_send(req);
1096 	if (ret)
1097 		goto failed_send;
1098 
1099 	goto out;
1100 
1101 failed_send:
1102 	zfcp_fsf_req_free(req);
1103 out:
1104 	spin_unlock_irq(&qdio->req_q_lock);
1105 	return ret;
1106 }
1107 
1108 static void zfcp_fsf_send_els_handler(struct zfcp_fsf_req *req)
1109 {
1110 	struct zfcp_fsf_ct_els *send_els = req->data;
1111 	struct fsf_qtcb_header *header = &req->qtcb->header;
1112 
1113 	send_els->status = -EINVAL;
1114 
1115 	if (req->status & ZFCP_STATUS_FSFREQ_ERROR)
1116 		goto skip_fsfstatus;
1117 
1118 	switch (header->fsf_status) {
1119 	case FSF_GOOD:
1120 		send_els->status = 0;
1121 		zfcp_dbf_san_res("fsselh1", req);
1122 		break;
1123 	case FSF_SERVICE_CLASS_NOT_SUPPORTED:
1124 		zfcp_fsf_class_not_supp(req);
1125 		break;
1126 	case FSF_ADAPTER_STATUS_AVAILABLE:
1127 		switch (header->fsf_status_qual.word[0]){
1128 		case FSF_SQ_INVOKE_LINK_TEST_PROCEDURE:
1129 		case FSF_SQ_ULP_DEPENDENT_ERP_REQUIRED:
1130 		case FSF_SQ_RETRY_IF_POSSIBLE:
1131 			req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1132 			break;
1133 		}
1134 		break;
1135 	case FSF_ELS_COMMAND_REJECTED:
1136 	case FSF_PAYLOAD_SIZE_MISMATCH:
1137 	case FSF_REQUEST_SIZE_TOO_LARGE:
1138 	case FSF_RESPONSE_SIZE_TOO_LARGE:
1139 		break;
1140 	case FSF_SBAL_MISMATCH:
1141 		/* should never occur, avoided in zfcp_fsf_send_els */
1142 		/* fall through */
1143 	default:
1144 		req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1145 		break;
1146 	}
1147 skip_fsfstatus:
1148 	if (send_els->handler)
1149 		send_els->handler(send_els->handler_data);
1150 }
1151 
1152 /**
1153  * zfcp_fsf_send_els - initiate an ELS command (FC-FS)
1154  * @els: pointer to struct zfcp_send_els with data for the command
1155  */
1156 int zfcp_fsf_send_els(struct zfcp_adapter *adapter, u32 d_id,
1157 		      struct zfcp_fsf_ct_els *els, unsigned int timeout)
1158 {
1159 	struct zfcp_fsf_req *req;
1160 	struct zfcp_qdio *qdio = adapter->qdio;
1161 	int ret = -EIO;
1162 
1163 	spin_lock_irq(&qdio->req_q_lock);
1164 	if (zfcp_qdio_sbal_get(qdio))
1165 		goto out;
1166 
1167 	req = zfcp_fsf_req_create(qdio, FSF_QTCB_SEND_ELS,
1168 				  SBAL_SFLAGS0_TYPE_WRITE_READ, NULL);
1169 
1170 	if (IS_ERR(req)) {
1171 		ret = PTR_ERR(req);
1172 		goto out;
1173 	}
1174 
1175 	req->status |= ZFCP_STATUS_FSFREQ_CLEANUP;
1176 
1177 	if (!zfcp_adapter_multi_buffer_active(adapter))
1178 		zfcp_qdio_sbal_limit(qdio, &req->qdio_req, 2);
1179 
1180 	ret = zfcp_fsf_setup_ct_els(req, els->req, els->resp, timeout);
1181 
1182 	if (ret)
1183 		goto failed_send;
1184 
1185 	hton24(req->qtcb->bottom.support.d_id, d_id);
1186 	req->handler = zfcp_fsf_send_els_handler;
1187 	els->d_id = d_id;
1188 	req->data = els;
1189 
1190 	zfcp_dbf_san_req("fssels1", req, d_id);
1191 
1192 	ret = zfcp_fsf_req_send(req);
1193 	if (ret)
1194 		goto failed_send;
1195 
1196 	goto out;
1197 
1198 failed_send:
1199 	zfcp_fsf_req_free(req);
1200 out:
1201 	spin_unlock_irq(&qdio->req_q_lock);
1202 	return ret;
1203 }
1204 
1205 int zfcp_fsf_exchange_config_data(struct zfcp_erp_action *erp_action)
1206 {
1207 	struct zfcp_fsf_req *req;
1208 	struct zfcp_qdio *qdio = erp_action->adapter->qdio;
1209 	int retval = -EIO;
1210 
1211 	spin_lock_irq(&qdio->req_q_lock);
1212 	if (zfcp_qdio_sbal_get(qdio))
1213 		goto out;
1214 
1215 	req = zfcp_fsf_req_create(qdio, FSF_QTCB_EXCHANGE_CONFIG_DATA,
1216 				  SBAL_SFLAGS0_TYPE_READ,
1217 				  qdio->adapter->pool.erp_req);
1218 
1219 	if (IS_ERR(req)) {
1220 		retval = PTR_ERR(req);
1221 		goto out;
1222 	}
1223 
1224 	req->status |= ZFCP_STATUS_FSFREQ_CLEANUP;
1225 	zfcp_qdio_set_sbale_last(qdio, &req->qdio_req);
1226 
1227 	req->qtcb->bottom.config.feature_selection =
1228 			FSF_FEATURE_NOTIFICATION_LOST |
1229 			FSF_FEATURE_UPDATE_ALERT;
1230 	req->erp_action = erp_action;
1231 	req->handler = zfcp_fsf_exchange_config_data_handler;
1232 	erp_action->fsf_req_id = req->req_id;
1233 
1234 	zfcp_fsf_start_erp_timer(req);
1235 	retval = zfcp_fsf_req_send(req);
1236 	if (retval) {
1237 		zfcp_fsf_req_free(req);
1238 		erp_action->fsf_req_id = 0;
1239 	}
1240 out:
1241 	spin_unlock_irq(&qdio->req_q_lock);
1242 	return retval;
1243 }
1244 
1245 int zfcp_fsf_exchange_config_data_sync(struct zfcp_qdio *qdio,
1246 				       struct fsf_qtcb_bottom_config *data)
1247 {
1248 	struct zfcp_fsf_req *req = NULL;
1249 	int retval = -EIO;
1250 
1251 	spin_lock_irq(&qdio->req_q_lock);
1252 	if (zfcp_qdio_sbal_get(qdio))
1253 		goto out_unlock;
1254 
1255 	req = zfcp_fsf_req_create(qdio, FSF_QTCB_EXCHANGE_CONFIG_DATA,
1256 				  SBAL_SFLAGS0_TYPE_READ, NULL);
1257 
1258 	if (IS_ERR(req)) {
1259 		retval = PTR_ERR(req);
1260 		goto out_unlock;
1261 	}
1262 
1263 	zfcp_qdio_set_sbale_last(qdio, &req->qdio_req);
1264 	req->handler = zfcp_fsf_exchange_config_data_handler;
1265 
1266 	req->qtcb->bottom.config.feature_selection =
1267 			FSF_FEATURE_NOTIFICATION_LOST |
1268 			FSF_FEATURE_UPDATE_ALERT;
1269 
1270 	if (data)
1271 		req->data = data;
1272 
1273 	zfcp_fsf_start_timer(req, ZFCP_FSF_REQUEST_TIMEOUT);
1274 	retval = zfcp_fsf_req_send(req);
1275 	spin_unlock_irq(&qdio->req_q_lock);
1276 	if (!retval)
1277 		wait_for_completion(&req->completion);
1278 
1279 	zfcp_fsf_req_free(req);
1280 	return retval;
1281 
1282 out_unlock:
1283 	spin_unlock_irq(&qdio->req_q_lock);
1284 	return retval;
1285 }
1286 
1287 /**
1288  * zfcp_fsf_exchange_port_data - request information about local port
1289  * @erp_action: ERP action for the adapter for which port data is requested
1290  * Returns: 0 on success, error otherwise
1291  */
1292 int zfcp_fsf_exchange_port_data(struct zfcp_erp_action *erp_action)
1293 {
1294 	struct zfcp_qdio *qdio = erp_action->adapter->qdio;
1295 	struct zfcp_fsf_req *req;
1296 	int retval = -EIO;
1297 
1298 	if (!(qdio->adapter->adapter_features & FSF_FEATURE_HBAAPI_MANAGEMENT))
1299 		return -EOPNOTSUPP;
1300 
1301 	spin_lock_irq(&qdio->req_q_lock);
1302 	if (zfcp_qdio_sbal_get(qdio))
1303 		goto out;
1304 
1305 	req = zfcp_fsf_req_create(qdio, FSF_QTCB_EXCHANGE_PORT_DATA,
1306 				  SBAL_SFLAGS0_TYPE_READ,
1307 				  qdio->adapter->pool.erp_req);
1308 
1309 	if (IS_ERR(req)) {
1310 		retval = PTR_ERR(req);
1311 		goto out;
1312 	}
1313 
1314 	req->status |= ZFCP_STATUS_FSFREQ_CLEANUP;
1315 	zfcp_qdio_set_sbale_last(qdio, &req->qdio_req);
1316 
1317 	req->handler = zfcp_fsf_exchange_port_data_handler;
1318 	req->erp_action = erp_action;
1319 	erp_action->fsf_req_id = req->req_id;
1320 
1321 	zfcp_fsf_start_erp_timer(req);
1322 	retval = zfcp_fsf_req_send(req);
1323 	if (retval) {
1324 		zfcp_fsf_req_free(req);
1325 		erp_action->fsf_req_id = 0;
1326 	}
1327 out:
1328 	spin_unlock_irq(&qdio->req_q_lock);
1329 	return retval;
1330 }
1331 
1332 /**
1333  * zfcp_fsf_exchange_port_data_sync - request information about local port
1334  * @qdio: pointer to struct zfcp_qdio
1335  * @data: pointer to struct fsf_qtcb_bottom_port
1336  * Returns: 0 on success, error otherwise
1337  */
1338 int zfcp_fsf_exchange_port_data_sync(struct zfcp_qdio *qdio,
1339 				     struct fsf_qtcb_bottom_port *data)
1340 {
1341 	struct zfcp_fsf_req *req = NULL;
1342 	int retval = -EIO;
1343 
1344 	if (!(qdio->adapter->adapter_features & FSF_FEATURE_HBAAPI_MANAGEMENT))
1345 		return -EOPNOTSUPP;
1346 
1347 	spin_lock_irq(&qdio->req_q_lock);
1348 	if (zfcp_qdio_sbal_get(qdio))
1349 		goto out_unlock;
1350 
1351 	req = zfcp_fsf_req_create(qdio, FSF_QTCB_EXCHANGE_PORT_DATA,
1352 				  SBAL_SFLAGS0_TYPE_READ, NULL);
1353 
1354 	if (IS_ERR(req)) {
1355 		retval = PTR_ERR(req);
1356 		goto out_unlock;
1357 	}
1358 
1359 	if (data)
1360 		req->data = data;
1361 
1362 	zfcp_qdio_set_sbale_last(qdio, &req->qdio_req);
1363 
1364 	req->handler = zfcp_fsf_exchange_port_data_handler;
1365 	zfcp_fsf_start_timer(req, ZFCP_FSF_REQUEST_TIMEOUT);
1366 	retval = zfcp_fsf_req_send(req);
1367 	spin_unlock_irq(&qdio->req_q_lock);
1368 
1369 	if (!retval)
1370 		wait_for_completion(&req->completion);
1371 
1372 	zfcp_fsf_req_free(req);
1373 
1374 	return retval;
1375 
1376 out_unlock:
1377 	spin_unlock_irq(&qdio->req_q_lock);
1378 	return retval;
1379 }
1380 
1381 static void zfcp_fsf_open_port_handler(struct zfcp_fsf_req *req)
1382 {
1383 	struct zfcp_port *port = req->data;
1384 	struct fsf_qtcb_header *header = &req->qtcb->header;
1385 	struct fc_els_flogi *plogi;
1386 
1387 	if (req->status & ZFCP_STATUS_FSFREQ_ERROR)
1388 		goto out;
1389 
1390 	switch (header->fsf_status) {
1391 	case FSF_PORT_ALREADY_OPEN:
1392 		break;
1393 	case FSF_MAXIMUM_NUMBER_OF_PORTS_EXCEEDED:
1394 		dev_warn(&req->adapter->ccw_device->dev,
1395 			 "Not enough FCP adapter resources to open "
1396 			 "remote port 0x%016Lx\n",
1397 			 (unsigned long long)port->wwpn);
1398 		zfcp_erp_set_port_status(port,
1399 					 ZFCP_STATUS_COMMON_ERP_FAILED);
1400 		req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1401 		break;
1402 	case FSF_ADAPTER_STATUS_AVAILABLE:
1403 		switch (header->fsf_status_qual.word[0]) {
1404 		case FSF_SQ_INVOKE_LINK_TEST_PROCEDURE:
1405 			/* no zfcp_fc_test_link() with failed open port */
1406 			/* fall through */
1407 		case FSF_SQ_ULP_DEPENDENT_ERP_REQUIRED:
1408 		case FSF_SQ_NO_RETRY_POSSIBLE:
1409 			req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1410 			break;
1411 		}
1412 		break;
1413 	case FSF_GOOD:
1414 		port->handle = header->port_handle;
1415 		atomic_or(ZFCP_STATUS_COMMON_OPEN |
1416 				ZFCP_STATUS_PORT_PHYS_OPEN, &port->status);
1417 		atomic_andnot(ZFCP_STATUS_COMMON_ACCESS_BOXED,
1418 		                  &port->status);
1419 		/* check whether D_ID has changed during open */
1420 		/*
1421 		 * FIXME: This check is not airtight, as the FCP channel does
1422 		 * not monitor closures of target port connections caused on
1423 		 * the remote side. Thus, they might miss out on invalidating
1424 		 * locally cached WWPNs (and other N_Port parameters) of gone
1425 		 * target ports. So, our heroic attempt to make things safe
1426 		 * could be undermined by 'open port' response data tagged with
1427 		 * obsolete WWPNs. Another reason to monitor potential
1428 		 * connection closures ourself at least (by interpreting
1429 		 * incoming ELS' and unsolicited status). It just crosses my
1430 		 * mind that one should be able to cross-check by means of
1431 		 * another GID_PN straight after a port has been opened.
1432 		 * Alternately, an ADISC/PDISC ELS should suffice, as well.
1433 		 */
1434 		plogi = (struct fc_els_flogi *) req->qtcb->bottom.support.els;
1435 		if (req->qtcb->bottom.support.els1_length >=
1436 		    FSF_PLOGI_MIN_LEN)
1437 				zfcp_fc_plogi_evaluate(port, plogi);
1438 		break;
1439 	case FSF_UNKNOWN_OP_SUBTYPE:
1440 		req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1441 		break;
1442 	}
1443 
1444 out:
1445 	put_device(&port->dev);
1446 }
1447 
1448 /**
1449  * zfcp_fsf_open_port - create and send open port request
1450  * @erp_action: pointer to struct zfcp_erp_action
1451  * Returns: 0 on success, error otherwise
1452  */
1453 int zfcp_fsf_open_port(struct zfcp_erp_action *erp_action)
1454 {
1455 	struct zfcp_qdio *qdio = erp_action->adapter->qdio;
1456 	struct zfcp_port *port = erp_action->port;
1457 	struct zfcp_fsf_req *req;
1458 	int retval = -EIO;
1459 
1460 	spin_lock_irq(&qdio->req_q_lock);
1461 	if (zfcp_qdio_sbal_get(qdio))
1462 		goto out;
1463 
1464 	req = zfcp_fsf_req_create(qdio, FSF_QTCB_OPEN_PORT_WITH_DID,
1465 				  SBAL_SFLAGS0_TYPE_READ,
1466 				  qdio->adapter->pool.erp_req);
1467 
1468 	if (IS_ERR(req)) {
1469 		retval = PTR_ERR(req);
1470 		goto out;
1471 	}
1472 
1473 	req->status |= ZFCP_STATUS_FSFREQ_CLEANUP;
1474 	zfcp_qdio_set_sbale_last(qdio, &req->qdio_req);
1475 
1476 	req->handler = zfcp_fsf_open_port_handler;
1477 	hton24(req->qtcb->bottom.support.d_id, port->d_id);
1478 	req->data = port;
1479 	req->erp_action = erp_action;
1480 	erp_action->fsf_req_id = req->req_id;
1481 	get_device(&port->dev);
1482 
1483 	zfcp_fsf_start_erp_timer(req);
1484 	retval = zfcp_fsf_req_send(req);
1485 	if (retval) {
1486 		zfcp_fsf_req_free(req);
1487 		erp_action->fsf_req_id = 0;
1488 		put_device(&port->dev);
1489 	}
1490 out:
1491 	spin_unlock_irq(&qdio->req_q_lock);
1492 	return retval;
1493 }
1494 
1495 static void zfcp_fsf_close_port_handler(struct zfcp_fsf_req *req)
1496 {
1497 	struct zfcp_port *port = req->data;
1498 
1499 	if (req->status & ZFCP_STATUS_FSFREQ_ERROR)
1500 		return;
1501 
1502 	switch (req->qtcb->header.fsf_status) {
1503 	case FSF_PORT_HANDLE_NOT_VALID:
1504 		zfcp_erp_adapter_reopen(port->adapter, 0, "fscph_1");
1505 		req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1506 		break;
1507 	case FSF_ADAPTER_STATUS_AVAILABLE:
1508 		break;
1509 	case FSF_GOOD:
1510 		zfcp_erp_clear_port_status(port, ZFCP_STATUS_COMMON_OPEN);
1511 		break;
1512 	}
1513 }
1514 
1515 /**
1516  * zfcp_fsf_close_port - create and send close port request
1517  * @erp_action: pointer to struct zfcp_erp_action
1518  * Returns: 0 on success, error otherwise
1519  */
1520 int zfcp_fsf_close_port(struct zfcp_erp_action *erp_action)
1521 {
1522 	struct zfcp_qdio *qdio = erp_action->adapter->qdio;
1523 	struct zfcp_fsf_req *req;
1524 	int retval = -EIO;
1525 
1526 	spin_lock_irq(&qdio->req_q_lock);
1527 	if (zfcp_qdio_sbal_get(qdio))
1528 		goto out;
1529 
1530 	req = zfcp_fsf_req_create(qdio, FSF_QTCB_CLOSE_PORT,
1531 				  SBAL_SFLAGS0_TYPE_READ,
1532 				  qdio->adapter->pool.erp_req);
1533 
1534 	if (IS_ERR(req)) {
1535 		retval = PTR_ERR(req);
1536 		goto out;
1537 	}
1538 
1539 	req->status |= ZFCP_STATUS_FSFREQ_CLEANUP;
1540 	zfcp_qdio_set_sbale_last(qdio, &req->qdio_req);
1541 
1542 	req->handler = zfcp_fsf_close_port_handler;
1543 	req->data = erp_action->port;
1544 	req->erp_action = erp_action;
1545 	req->qtcb->header.port_handle = erp_action->port->handle;
1546 	erp_action->fsf_req_id = req->req_id;
1547 
1548 	zfcp_fsf_start_erp_timer(req);
1549 	retval = zfcp_fsf_req_send(req);
1550 	if (retval) {
1551 		zfcp_fsf_req_free(req);
1552 		erp_action->fsf_req_id = 0;
1553 	}
1554 out:
1555 	spin_unlock_irq(&qdio->req_q_lock);
1556 	return retval;
1557 }
1558 
1559 static void zfcp_fsf_open_wka_port_handler(struct zfcp_fsf_req *req)
1560 {
1561 	struct zfcp_fc_wka_port *wka_port = req->data;
1562 	struct fsf_qtcb_header *header = &req->qtcb->header;
1563 
1564 	if (req->status & ZFCP_STATUS_FSFREQ_ERROR) {
1565 		wka_port->status = ZFCP_FC_WKA_PORT_OFFLINE;
1566 		goto out;
1567 	}
1568 
1569 	switch (header->fsf_status) {
1570 	case FSF_MAXIMUM_NUMBER_OF_PORTS_EXCEEDED:
1571 		dev_warn(&req->adapter->ccw_device->dev,
1572 			 "Opening WKA port 0x%x failed\n", wka_port->d_id);
1573 		/* fall through */
1574 	case FSF_ADAPTER_STATUS_AVAILABLE:
1575 		req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1576 		wka_port->status = ZFCP_FC_WKA_PORT_OFFLINE;
1577 		break;
1578 	case FSF_GOOD:
1579 		wka_port->handle = header->port_handle;
1580 		/* fall through */
1581 	case FSF_PORT_ALREADY_OPEN:
1582 		wka_port->status = ZFCP_FC_WKA_PORT_ONLINE;
1583 	}
1584 out:
1585 	wake_up(&wka_port->completion_wq);
1586 }
1587 
1588 /**
1589  * zfcp_fsf_open_wka_port - create and send open wka-port request
1590  * @wka_port: pointer to struct zfcp_fc_wka_port
1591  * Returns: 0 on success, error otherwise
1592  */
1593 int zfcp_fsf_open_wka_port(struct zfcp_fc_wka_port *wka_port)
1594 {
1595 	struct zfcp_qdio *qdio = wka_port->adapter->qdio;
1596 	struct zfcp_fsf_req *req;
1597 	int retval = -EIO;
1598 
1599 	spin_lock_irq(&qdio->req_q_lock);
1600 	if (zfcp_qdio_sbal_get(qdio))
1601 		goto out;
1602 
1603 	req = zfcp_fsf_req_create(qdio, FSF_QTCB_OPEN_PORT_WITH_DID,
1604 				  SBAL_SFLAGS0_TYPE_READ,
1605 				  qdio->adapter->pool.erp_req);
1606 
1607 	if (IS_ERR(req)) {
1608 		retval = PTR_ERR(req);
1609 		goto out;
1610 	}
1611 
1612 	req->status |= ZFCP_STATUS_FSFREQ_CLEANUP;
1613 	zfcp_qdio_set_sbale_last(qdio, &req->qdio_req);
1614 
1615 	req->handler = zfcp_fsf_open_wka_port_handler;
1616 	hton24(req->qtcb->bottom.support.d_id, wka_port->d_id);
1617 	req->data = wka_port;
1618 
1619 	zfcp_fsf_start_timer(req, ZFCP_FSF_REQUEST_TIMEOUT);
1620 	retval = zfcp_fsf_req_send(req);
1621 	if (retval)
1622 		zfcp_fsf_req_free(req);
1623 out:
1624 	spin_unlock_irq(&qdio->req_q_lock);
1625 	if (!retval)
1626 		zfcp_dbf_rec_run_wka("fsowp_1", wka_port, req->req_id);
1627 	return retval;
1628 }
1629 
1630 static void zfcp_fsf_close_wka_port_handler(struct zfcp_fsf_req *req)
1631 {
1632 	struct zfcp_fc_wka_port *wka_port = req->data;
1633 
1634 	if (req->qtcb->header.fsf_status == FSF_PORT_HANDLE_NOT_VALID) {
1635 		req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1636 		zfcp_erp_adapter_reopen(wka_port->adapter, 0, "fscwph1");
1637 	}
1638 
1639 	wka_port->status = ZFCP_FC_WKA_PORT_OFFLINE;
1640 	wake_up(&wka_port->completion_wq);
1641 }
1642 
1643 /**
1644  * zfcp_fsf_close_wka_port - create and send close wka port request
1645  * @wka_port: WKA port to open
1646  * Returns: 0 on success, error otherwise
1647  */
1648 int zfcp_fsf_close_wka_port(struct zfcp_fc_wka_port *wka_port)
1649 {
1650 	struct zfcp_qdio *qdio = wka_port->adapter->qdio;
1651 	struct zfcp_fsf_req *req;
1652 	int retval = -EIO;
1653 
1654 	spin_lock_irq(&qdio->req_q_lock);
1655 	if (zfcp_qdio_sbal_get(qdio))
1656 		goto out;
1657 
1658 	req = zfcp_fsf_req_create(qdio, FSF_QTCB_CLOSE_PORT,
1659 				  SBAL_SFLAGS0_TYPE_READ,
1660 				  qdio->adapter->pool.erp_req);
1661 
1662 	if (IS_ERR(req)) {
1663 		retval = PTR_ERR(req);
1664 		goto out;
1665 	}
1666 
1667 	req->status |= ZFCP_STATUS_FSFREQ_CLEANUP;
1668 	zfcp_qdio_set_sbale_last(qdio, &req->qdio_req);
1669 
1670 	req->handler = zfcp_fsf_close_wka_port_handler;
1671 	req->data = wka_port;
1672 	req->qtcb->header.port_handle = wka_port->handle;
1673 
1674 	zfcp_fsf_start_timer(req, ZFCP_FSF_REQUEST_TIMEOUT);
1675 	retval = zfcp_fsf_req_send(req);
1676 	if (retval)
1677 		zfcp_fsf_req_free(req);
1678 out:
1679 	spin_unlock_irq(&qdio->req_q_lock);
1680 	if (!retval)
1681 		zfcp_dbf_rec_run_wka("fscwp_1", wka_port, req->req_id);
1682 	return retval;
1683 }
1684 
1685 static void zfcp_fsf_close_physical_port_handler(struct zfcp_fsf_req *req)
1686 {
1687 	struct zfcp_port *port = req->data;
1688 	struct fsf_qtcb_header *header = &req->qtcb->header;
1689 	struct scsi_device *sdev;
1690 
1691 	if (req->status & ZFCP_STATUS_FSFREQ_ERROR)
1692 		return;
1693 
1694 	switch (header->fsf_status) {
1695 	case FSF_PORT_HANDLE_NOT_VALID:
1696 		zfcp_erp_adapter_reopen(port->adapter, 0, "fscpph1");
1697 		req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1698 		break;
1699 	case FSF_PORT_BOXED:
1700 		/* can't use generic zfcp_erp_modify_port_status because
1701 		 * ZFCP_STATUS_COMMON_OPEN must not be reset for the port */
1702 		atomic_andnot(ZFCP_STATUS_PORT_PHYS_OPEN, &port->status);
1703 		shost_for_each_device(sdev, port->adapter->scsi_host)
1704 			if (sdev_to_zfcp(sdev)->port == port)
1705 				atomic_andnot(ZFCP_STATUS_COMMON_OPEN,
1706 						  &sdev_to_zfcp(sdev)->status);
1707 		zfcp_erp_set_port_status(port, ZFCP_STATUS_COMMON_ACCESS_BOXED);
1708 		zfcp_erp_port_reopen(port, ZFCP_STATUS_COMMON_ERP_FAILED,
1709 				     "fscpph2");
1710 		req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1711 		break;
1712 	case FSF_ADAPTER_STATUS_AVAILABLE:
1713 		switch (header->fsf_status_qual.word[0]) {
1714 		case FSF_SQ_INVOKE_LINK_TEST_PROCEDURE:
1715 			/* fall through */
1716 		case FSF_SQ_ULP_DEPENDENT_ERP_REQUIRED:
1717 			req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1718 			break;
1719 		}
1720 		break;
1721 	case FSF_GOOD:
1722 		/* can't use generic zfcp_erp_modify_port_status because
1723 		 * ZFCP_STATUS_COMMON_OPEN must not be reset for the port
1724 		 */
1725 		atomic_andnot(ZFCP_STATUS_PORT_PHYS_OPEN, &port->status);
1726 		shost_for_each_device(sdev, port->adapter->scsi_host)
1727 			if (sdev_to_zfcp(sdev)->port == port)
1728 				atomic_andnot(ZFCP_STATUS_COMMON_OPEN,
1729 						  &sdev_to_zfcp(sdev)->status);
1730 		break;
1731 	}
1732 }
1733 
1734 /**
1735  * zfcp_fsf_close_physical_port - close physical port
1736  * @erp_action: pointer to struct zfcp_erp_action
1737  * Returns: 0 on success
1738  */
1739 int zfcp_fsf_close_physical_port(struct zfcp_erp_action *erp_action)
1740 {
1741 	struct zfcp_qdio *qdio = erp_action->adapter->qdio;
1742 	struct zfcp_fsf_req *req;
1743 	int retval = -EIO;
1744 
1745 	spin_lock_irq(&qdio->req_q_lock);
1746 	if (zfcp_qdio_sbal_get(qdio))
1747 		goto out;
1748 
1749 	req = zfcp_fsf_req_create(qdio, FSF_QTCB_CLOSE_PHYSICAL_PORT,
1750 				  SBAL_SFLAGS0_TYPE_READ,
1751 				  qdio->adapter->pool.erp_req);
1752 
1753 	if (IS_ERR(req)) {
1754 		retval = PTR_ERR(req);
1755 		goto out;
1756 	}
1757 
1758 	req->status |= ZFCP_STATUS_FSFREQ_CLEANUP;
1759 	zfcp_qdio_set_sbale_last(qdio, &req->qdio_req);
1760 
1761 	req->data = erp_action->port;
1762 	req->qtcb->header.port_handle = erp_action->port->handle;
1763 	req->erp_action = erp_action;
1764 	req->handler = zfcp_fsf_close_physical_port_handler;
1765 	erp_action->fsf_req_id = req->req_id;
1766 
1767 	zfcp_fsf_start_erp_timer(req);
1768 	retval = zfcp_fsf_req_send(req);
1769 	if (retval) {
1770 		zfcp_fsf_req_free(req);
1771 		erp_action->fsf_req_id = 0;
1772 	}
1773 out:
1774 	spin_unlock_irq(&qdio->req_q_lock);
1775 	return retval;
1776 }
1777 
1778 static void zfcp_fsf_open_lun_handler(struct zfcp_fsf_req *req)
1779 {
1780 	struct zfcp_adapter *adapter = req->adapter;
1781 	struct scsi_device *sdev = req->data;
1782 	struct zfcp_scsi_dev *zfcp_sdev;
1783 	struct fsf_qtcb_header *header = &req->qtcb->header;
1784 	union fsf_status_qual *qual = &header->fsf_status_qual;
1785 
1786 	if (req->status & ZFCP_STATUS_FSFREQ_ERROR)
1787 		return;
1788 
1789 	zfcp_sdev = sdev_to_zfcp(sdev);
1790 
1791 	atomic_andnot(ZFCP_STATUS_COMMON_ACCESS_DENIED |
1792 			  ZFCP_STATUS_COMMON_ACCESS_BOXED,
1793 			  &zfcp_sdev->status);
1794 
1795 	switch (header->fsf_status) {
1796 
1797 	case FSF_PORT_HANDLE_NOT_VALID:
1798 		zfcp_erp_adapter_reopen(adapter, 0, "fsouh_1");
1799 		/* fall through */
1800 	case FSF_LUN_ALREADY_OPEN:
1801 		break;
1802 	case FSF_PORT_BOXED:
1803 		zfcp_erp_set_port_status(zfcp_sdev->port,
1804 					 ZFCP_STATUS_COMMON_ACCESS_BOXED);
1805 		zfcp_erp_port_reopen(zfcp_sdev->port,
1806 				     ZFCP_STATUS_COMMON_ERP_FAILED, "fsouh_2");
1807 		req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1808 		break;
1809 	case FSF_LUN_SHARING_VIOLATION:
1810 		if (qual->word[0])
1811 			dev_warn(&zfcp_sdev->port->adapter->ccw_device->dev,
1812 				 "LUN 0x%Lx on port 0x%Lx is already in "
1813 				 "use by CSS%d, MIF Image ID %x\n",
1814 				 zfcp_scsi_dev_lun(sdev),
1815 				 (unsigned long long)zfcp_sdev->port->wwpn,
1816 				 qual->fsf_queue_designator.cssid,
1817 				 qual->fsf_queue_designator.hla);
1818 		zfcp_erp_set_lun_status(sdev,
1819 					ZFCP_STATUS_COMMON_ERP_FAILED |
1820 					ZFCP_STATUS_COMMON_ACCESS_DENIED);
1821 		req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1822 		break;
1823 	case FSF_MAXIMUM_NUMBER_OF_LUNS_EXCEEDED:
1824 		dev_warn(&adapter->ccw_device->dev,
1825 			 "No handle is available for LUN "
1826 			 "0x%016Lx on port 0x%016Lx\n",
1827 			 (unsigned long long)zfcp_scsi_dev_lun(sdev),
1828 			 (unsigned long long)zfcp_sdev->port->wwpn);
1829 		zfcp_erp_set_lun_status(sdev, ZFCP_STATUS_COMMON_ERP_FAILED);
1830 		/* fall through */
1831 	case FSF_INVALID_COMMAND_OPTION:
1832 		req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1833 		break;
1834 	case FSF_ADAPTER_STATUS_AVAILABLE:
1835 		switch (header->fsf_status_qual.word[0]) {
1836 		case FSF_SQ_INVOKE_LINK_TEST_PROCEDURE:
1837 			zfcp_fc_test_link(zfcp_sdev->port);
1838 			/* fall through */
1839 		case FSF_SQ_ULP_DEPENDENT_ERP_REQUIRED:
1840 			req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1841 			break;
1842 		}
1843 		break;
1844 
1845 	case FSF_GOOD:
1846 		zfcp_sdev->lun_handle = header->lun_handle;
1847 		atomic_or(ZFCP_STATUS_COMMON_OPEN, &zfcp_sdev->status);
1848 		break;
1849 	}
1850 }
1851 
1852 /**
1853  * zfcp_fsf_open_lun - open LUN
1854  * @erp_action: pointer to struct zfcp_erp_action
1855  * Returns: 0 on success, error otherwise
1856  */
1857 int zfcp_fsf_open_lun(struct zfcp_erp_action *erp_action)
1858 {
1859 	struct zfcp_adapter *adapter = erp_action->adapter;
1860 	struct zfcp_qdio *qdio = adapter->qdio;
1861 	struct zfcp_fsf_req *req;
1862 	int retval = -EIO;
1863 
1864 	spin_lock_irq(&qdio->req_q_lock);
1865 	if (zfcp_qdio_sbal_get(qdio))
1866 		goto out;
1867 
1868 	req = zfcp_fsf_req_create(qdio, FSF_QTCB_OPEN_LUN,
1869 				  SBAL_SFLAGS0_TYPE_READ,
1870 				  adapter->pool.erp_req);
1871 
1872 	if (IS_ERR(req)) {
1873 		retval = PTR_ERR(req);
1874 		goto out;
1875 	}
1876 
1877 	req->status |= ZFCP_STATUS_FSFREQ_CLEANUP;
1878 	zfcp_qdio_set_sbale_last(qdio, &req->qdio_req);
1879 
1880 	req->qtcb->header.port_handle = erp_action->port->handle;
1881 	req->qtcb->bottom.support.fcp_lun = zfcp_scsi_dev_lun(erp_action->sdev);
1882 	req->handler = zfcp_fsf_open_lun_handler;
1883 	req->data = erp_action->sdev;
1884 	req->erp_action = erp_action;
1885 	erp_action->fsf_req_id = req->req_id;
1886 
1887 	if (!(adapter->connection_features & FSF_FEATURE_NPIV_MODE))
1888 		req->qtcb->bottom.support.option = FSF_OPEN_LUN_SUPPRESS_BOXING;
1889 
1890 	zfcp_fsf_start_erp_timer(req);
1891 	retval = zfcp_fsf_req_send(req);
1892 	if (retval) {
1893 		zfcp_fsf_req_free(req);
1894 		erp_action->fsf_req_id = 0;
1895 	}
1896 out:
1897 	spin_unlock_irq(&qdio->req_q_lock);
1898 	return retval;
1899 }
1900 
1901 static void zfcp_fsf_close_lun_handler(struct zfcp_fsf_req *req)
1902 {
1903 	struct scsi_device *sdev = req->data;
1904 	struct zfcp_scsi_dev *zfcp_sdev;
1905 
1906 	if (req->status & ZFCP_STATUS_FSFREQ_ERROR)
1907 		return;
1908 
1909 	zfcp_sdev = sdev_to_zfcp(sdev);
1910 
1911 	switch (req->qtcb->header.fsf_status) {
1912 	case FSF_PORT_HANDLE_NOT_VALID:
1913 		zfcp_erp_adapter_reopen(zfcp_sdev->port->adapter, 0, "fscuh_1");
1914 		req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1915 		break;
1916 	case FSF_LUN_HANDLE_NOT_VALID:
1917 		zfcp_erp_port_reopen(zfcp_sdev->port, 0, "fscuh_2");
1918 		req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1919 		break;
1920 	case FSF_PORT_BOXED:
1921 		zfcp_erp_set_port_status(zfcp_sdev->port,
1922 					 ZFCP_STATUS_COMMON_ACCESS_BOXED);
1923 		zfcp_erp_port_reopen(zfcp_sdev->port,
1924 				     ZFCP_STATUS_COMMON_ERP_FAILED, "fscuh_3");
1925 		req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1926 		break;
1927 	case FSF_ADAPTER_STATUS_AVAILABLE:
1928 		switch (req->qtcb->header.fsf_status_qual.word[0]) {
1929 		case FSF_SQ_INVOKE_LINK_TEST_PROCEDURE:
1930 			zfcp_fc_test_link(zfcp_sdev->port);
1931 			/* fall through */
1932 		case FSF_SQ_ULP_DEPENDENT_ERP_REQUIRED:
1933 			req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1934 			break;
1935 		}
1936 		break;
1937 	case FSF_GOOD:
1938 		atomic_andnot(ZFCP_STATUS_COMMON_OPEN, &zfcp_sdev->status);
1939 		break;
1940 	}
1941 }
1942 
1943 /**
1944  * zfcp_fsf_close_LUN - close LUN
1945  * @erp_action: pointer to erp_action triggering the "close LUN"
1946  * Returns: 0 on success, error otherwise
1947  */
1948 int zfcp_fsf_close_lun(struct zfcp_erp_action *erp_action)
1949 {
1950 	struct zfcp_qdio *qdio = erp_action->adapter->qdio;
1951 	struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(erp_action->sdev);
1952 	struct zfcp_fsf_req *req;
1953 	int retval = -EIO;
1954 
1955 	spin_lock_irq(&qdio->req_q_lock);
1956 	if (zfcp_qdio_sbal_get(qdio))
1957 		goto out;
1958 
1959 	req = zfcp_fsf_req_create(qdio, FSF_QTCB_CLOSE_LUN,
1960 				  SBAL_SFLAGS0_TYPE_READ,
1961 				  qdio->adapter->pool.erp_req);
1962 
1963 	if (IS_ERR(req)) {
1964 		retval = PTR_ERR(req);
1965 		goto out;
1966 	}
1967 
1968 	req->status |= ZFCP_STATUS_FSFREQ_CLEANUP;
1969 	zfcp_qdio_set_sbale_last(qdio, &req->qdio_req);
1970 
1971 	req->qtcb->header.port_handle = erp_action->port->handle;
1972 	req->qtcb->header.lun_handle = zfcp_sdev->lun_handle;
1973 	req->handler = zfcp_fsf_close_lun_handler;
1974 	req->data = erp_action->sdev;
1975 	req->erp_action = erp_action;
1976 	erp_action->fsf_req_id = req->req_id;
1977 
1978 	zfcp_fsf_start_erp_timer(req);
1979 	retval = zfcp_fsf_req_send(req);
1980 	if (retval) {
1981 		zfcp_fsf_req_free(req);
1982 		erp_action->fsf_req_id = 0;
1983 	}
1984 out:
1985 	spin_unlock_irq(&qdio->req_q_lock);
1986 	return retval;
1987 }
1988 
1989 static void zfcp_fsf_update_lat(struct fsf_latency_record *lat_rec, u32 lat)
1990 {
1991 	lat_rec->sum += lat;
1992 	lat_rec->min = min(lat_rec->min, lat);
1993 	lat_rec->max = max(lat_rec->max, lat);
1994 }
1995 
1996 static void zfcp_fsf_req_trace(struct zfcp_fsf_req *req, struct scsi_cmnd *scsi)
1997 {
1998 	struct fsf_qual_latency_info *lat_in;
1999 	struct latency_cont *lat = NULL;
2000 	struct zfcp_scsi_dev *zfcp_sdev;
2001 	struct zfcp_blk_drv_data blktrc;
2002 	int ticks = req->adapter->timer_ticks;
2003 
2004 	lat_in = &req->qtcb->prefix.prot_status_qual.latency_info;
2005 
2006 	blktrc.flags = 0;
2007 	blktrc.magic = ZFCP_BLK_DRV_DATA_MAGIC;
2008 	if (req->status & ZFCP_STATUS_FSFREQ_ERROR)
2009 		blktrc.flags |= ZFCP_BLK_REQ_ERROR;
2010 	blktrc.inb_usage = 0;
2011 	blktrc.outb_usage = req->qdio_req.qdio_outb_usage;
2012 
2013 	if (req->adapter->adapter_features & FSF_FEATURE_MEASUREMENT_DATA &&
2014 	    !(req->status & ZFCP_STATUS_FSFREQ_ERROR)) {
2015 		zfcp_sdev = sdev_to_zfcp(scsi->device);
2016 		blktrc.flags |= ZFCP_BLK_LAT_VALID;
2017 		blktrc.channel_lat = lat_in->channel_lat * ticks;
2018 		blktrc.fabric_lat = lat_in->fabric_lat * ticks;
2019 
2020 		switch (req->qtcb->bottom.io.data_direction) {
2021 		case FSF_DATADIR_DIF_READ_STRIP:
2022 		case FSF_DATADIR_DIF_READ_CONVERT:
2023 		case FSF_DATADIR_READ:
2024 			lat = &zfcp_sdev->latencies.read;
2025 			break;
2026 		case FSF_DATADIR_DIF_WRITE_INSERT:
2027 		case FSF_DATADIR_DIF_WRITE_CONVERT:
2028 		case FSF_DATADIR_WRITE:
2029 			lat = &zfcp_sdev->latencies.write;
2030 			break;
2031 		case FSF_DATADIR_CMND:
2032 			lat = &zfcp_sdev->latencies.cmd;
2033 			break;
2034 		}
2035 
2036 		if (lat) {
2037 			spin_lock(&zfcp_sdev->latencies.lock);
2038 			zfcp_fsf_update_lat(&lat->channel, lat_in->channel_lat);
2039 			zfcp_fsf_update_lat(&lat->fabric, lat_in->fabric_lat);
2040 			lat->counter++;
2041 			spin_unlock(&zfcp_sdev->latencies.lock);
2042 		}
2043 	}
2044 
2045 	blk_add_driver_data(scsi->request->q, scsi->request, &blktrc,
2046 			    sizeof(blktrc));
2047 }
2048 
2049 /**
2050  * zfcp_fsf_fcp_handler_common() - FCP response handler common to I/O and TMF.
2051  * @req: Pointer to FSF request.
2052  * @sdev: Pointer to SCSI device as request context.
2053  */
2054 static void zfcp_fsf_fcp_handler_common(struct zfcp_fsf_req *req,
2055 					struct scsi_device *sdev)
2056 {
2057 	struct zfcp_scsi_dev *zfcp_sdev;
2058 	struct fsf_qtcb_header *header = &req->qtcb->header;
2059 
2060 	if (unlikely(req->status & ZFCP_STATUS_FSFREQ_ERROR))
2061 		return;
2062 
2063 	zfcp_sdev = sdev_to_zfcp(sdev);
2064 
2065 	switch (header->fsf_status) {
2066 	case FSF_HANDLE_MISMATCH:
2067 	case FSF_PORT_HANDLE_NOT_VALID:
2068 		zfcp_erp_adapter_reopen(req->adapter, 0, "fssfch1");
2069 		req->status |= ZFCP_STATUS_FSFREQ_ERROR;
2070 		break;
2071 	case FSF_FCPLUN_NOT_VALID:
2072 	case FSF_LUN_HANDLE_NOT_VALID:
2073 		zfcp_erp_port_reopen(zfcp_sdev->port, 0, "fssfch2");
2074 		req->status |= ZFCP_STATUS_FSFREQ_ERROR;
2075 		break;
2076 	case FSF_SERVICE_CLASS_NOT_SUPPORTED:
2077 		zfcp_fsf_class_not_supp(req);
2078 		break;
2079 	case FSF_DIRECTION_INDICATOR_NOT_VALID:
2080 		dev_err(&req->adapter->ccw_device->dev,
2081 			"Incorrect direction %d, LUN 0x%016Lx on port "
2082 			"0x%016Lx closed\n",
2083 			req->qtcb->bottom.io.data_direction,
2084 			(unsigned long long)zfcp_scsi_dev_lun(sdev),
2085 			(unsigned long long)zfcp_sdev->port->wwpn);
2086 		zfcp_erp_adapter_shutdown(req->adapter, 0, "fssfch3");
2087 		req->status |= ZFCP_STATUS_FSFREQ_ERROR;
2088 		break;
2089 	case FSF_CMND_LENGTH_NOT_VALID:
2090 		dev_err(&req->adapter->ccw_device->dev,
2091 			"Incorrect CDB length %d, LUN 0x%016Lx on "
2092 			"port 0x%016Lx closed\n",
2093 			req->qtcb->bottom.io.fcp_cmnd_length,
2094 			(unsigned long long)zfcp_scsi_dev_lun(sdev),
2095 			(unsigned long long)zfcp_sdev->port->wwpn);
2096 		zfcp_erp_adapter_shutdown(req->adapter, 0, "fssfch4");
2097 		req->status |= ZFCP_STATUS_FSFREQ_ERROR;
2098 		break;
2099 	case FSF_PORT_BOXED:
2100 		zfcp_erp_set_port_status(zfcp_sdev->port,
2101 					 ZFCP_STATUS_COMMON_ACCESS_BOXED);
2102 		zfcp_erp_port_reopen(zfcp_sdev->port,
2103 				     ZFCP_STATUS_COMMON_ERP_FAILED, "fssfch5");
2104 		req->status |= ZFCP_STATUS_FSFREQ_ERROR;
2105 		break;
2106 	case FSF_LUN_BOXED:
2107 		zfcp_erp_set_lun_status(sdev, ZFCP_STATUS_COMMON_ACCESS_BOXED);
2108 		zfcp_erp_lun_reopen(sdev, ZFCP_STATUS_COMMON_ERP_FAILED,
2109 				    "fssfch6");
2110 		req->status |= ZFCP_STATUS_FSFREQ_ERROR;
2111 		break;
2112 	case FSF_ADAPTER_STATUS_AVAILABLE:
2113 		if (header->fsf_status_qual.word[0] ==
2114 		    FSF_SQ_INVOKE_LINK_TEST_PROCEDURE)
2115 			zfcp_fc_test_link(zfcp_sdev->port);
2116 		req->status |= ZFCP_STATUS_FSFREQ_ERROR;
2117 		break;
2118 	}
2119 }
2120 
2121 static void zfcp_fsf_fcp_cmnd_handler(struct zfcp_fsf_req *req)
2122 {
2123 	struct scsi_cmnd *scpnt;
2124 	struct fcp_resp_with_ext *fcp_rsp;
2125 	unsigned long flags;
2126 
2127 	read_lock_irqsave(&req->adapter->abort_lock, flags);
2128 
2129 	scpnt = req->data;
2130 	if (unlikely(!scpnt)) {
2131 		read_unlock_irqrestore(&req->adapter->abort_lock, flags);
2132 		return;
2133 	}
2134 
2135 	zfcp_fsf_fcp_handler_common(req, scpnt->device);
2136 
2137 	if (unlikely(req->status & ZFCP_STATUS_FSFREQ_ERROR)) {
2138 		set_host_byte(scpnt, DID_TRANSPORT_DISRUPTED);
2139 		goto skip_fsfstatus;
2140 	}
2141 
2142 	switch (req->qtcb->header.fsf_status) {
2143 	case FSF_INCONSISTENT_PROT_DATA:
2144 	case FSF_INVALID_PROT_PARM:
2145 		set_host_byte(scpnt, DID_ERROR);
2146 		goto skip_fsfstatus;
2147 	case FSF_BLOCK_GUARD_CHECK_FAILURE:
2148 		zfcp_scsi_dif_sense_error(scpnt, 0x1);
2149 		goto skip_fsfstatus;
2150 	case FSF_APP_TAG_CHECK_FAILURE:
2151 		zfcp_scsi_dif_sense_error(scpnt, 0x2);
2152 		goto skip_fsfstatus;
2153 	case FSF_REF_TAG_CHECK_FAILURE:
2154 		zfcp_scsi_dif_sense_error(scpnt, 0x3);
2155 		goto skip_fsfstatus;
2156 	}
2157 	BUILD_BUG_ON(sizeof(struct fcp_resp_with_ext) > FSF_FCP_RSP_SIZE);
2158 	fcp_rsp = &req->qtcb->bottom.io.fcp_rsp.iu;
2159 	zfcp_fc_eval_fcp_rsp(fcp_rsp, scpnt);
2160 
2161 skip_fsfstatus:
2162 	zfcp_fsf_req_trace(req, scpnt);
2163 	zfcp_dbf_scsi_result(scpnt, req);
2164 
2165 	scpnt->host_scribble = NULL;
2166 	(scpnt->scsi_done) (scpnt);
2167 	/*
2168 	 * We must hold this lock until scsi_done has been called.
2169 	 * Otherwise we may call scsi_done after abort regarding this
2170 	 * command has completed.
2171 	 * Note: scsi_done must not block!
2172 	 */
2173 	read_unlock_irqrestore(&req->adapter->abort_lock, flags);
2174 }
2175 
2176 static int zfcp_fsf_set_data_dir(struct scsi_cmnd *scsi_cmnd, u32 *data_dir)
2177 {
2178 	switch (scsi_get_prot_op(scsi_cmnd)) {
2179 	case SCSI_PROT_NORMAL:
2180 		switch (scsi_cmnd->sc_data_direction) {
2181 		case DMA_NONE:
2182 			*data_dir = FSF_DATADIR_CMND;
2183 			break;
2184 		case DMA_FROM_DEVICE:
2185 			*data_dir = FSF_DATADIR_READ;
2186 			break;
2187 		case DMA_TO_DEVICE:
2188 			*data_dir = FSF_DATADIR_WRITE;
2189 			break;
2190 		case DMA_BIDIRECTIONAL:
2191 			return -EINVAL;
2192 		}
2193 		break;
2194 
2195 	case SCSI_PROT_READ_STRIP:
2196 		*data_dir = FSF_DATADIR_DIF_READ_STRIP;
2197 		break;
2198 	case SCSI_PROT_WRITE_INSERT:
2199 		*data_dir = FSF_DATADIR_DIF_WRITE_INSERT;
2200 		break;
2201 	case SCSI_PROT_READ_PASS:
2202 		*data_dir = FSF_DATADIR_DIF_READ_CONVERT;
2203 		break;
2204 	case SCSI_PROT_WRITE_PASS:
2205 		*data_dir = FSF_DATADIR_DIF_WRITE_CONVERT;
2206 		break;
2207 	default:
2208 		return -EINVAL;
2209 	}
2210 
2211 	return 0;
2212 }
2213 
2214 /**
2215  * zfcp_fsf_fcp_cmnd - initiate an FCP command (for a SCSI command)
2216  * @scsi_cmnd: scsi command to be sent
2217  */
2218 int zfcp_fsf_fcp_cmnd(struct scsi_cmnd *scsi_cmnd)
2219 {
2220 	struct zfcp_fsf_req *req;
2221 	struct fcp_cmnd *fcp_cmnd;
2222 	u8 sbtype = SBAL_SFLAGS0_TYPE_READ;
2223 	int retval = -EIO;
2224 	struct scsi_device *sdev = scsi_cmnd->device;
2225 	struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
2226 	struct zfcp_adapter *adapter = zfcp_sdev->port->adapter;
2227 	struct zfcp_qdio *qdio = adapter->qdio;
2228 	struct fsf_qtcb_bottom_io *io;
2229 	unsigned long flags;
2230 
2231 	if (unlikely(!(atomic_read(&zfcp_sdev->status) &
2232 		       ZFCP_STATUS_COMMON_UNBLOCKED)))
2233 		return -EBUSY;
2234 
2235 	spin_lock_irqsave(&qdio->req_q_lock, flags);
2236 	if (atomic_read(&qdio->req_q_free) <= 0) {
2237 		atomic_inc(&qdio->req_q_full);
2238 		goto out;
2239 	}
2240 
2241 	if (scsi_cmnd->sc_data_direction == DMA_TO_DEVICE)
2242 		sbtype = SBAL_SFLAGS0_TYPE_WRITE;
2243 
2244 	req = zfcp_fsf_req_create(qdio, FSF_QTCB_FCP_CMND,
2245 				  sbtype, adapter->pool.scsi_req);
2246 
2247 	if (IS_ERR(req)) {
2248 		retval = PTR_ERR(req);
2249 		goto out;
2250 	}
2251 
2252 	scsi_cmnd->host_scribble = (unsigned char *) req->req_id;
2253 
2254 	io = &req->qtcb->bottom.io;
2255 	req->status |= ZFCP_STATUS_FSFREQ_CLEANUP;
2256 	req->data = scsi_cmnd;
2257 	req->handler = zfcp_fsf_fcp_cmnd_handler;
2258 	req->qtcb->header.lun_handle = zfcp_sdev->lun_handle;
2259 	req->qtcb->header.port_handle = zfcp_sdev->port->handle;
2260 	io->service_class = FSF_CLASS_3;
2261 	io->fcp_cmnd_length = FCP_CMND_LEN;
2262 
2263 	if (scsi_get_prot_op(scsi_cmnd) != SCSI_PROT_NORMAL) {
2264 		io->data_block_length = scsi_cmnd->device->sector_size;
2265 		io->ref_tag_value = scsi_get_lba(scsi_cmnd) & 0xFFFFFFFF;
2266 	}
2267 
2268 	if (zfcp_fsf_set_data_dir(scsi_cmnd, &io->data_direction))
2269 		goto failed_scsi_cmnd;
2270 
2271 	BUILD_BUG_ON(sizeof(struct fcp_cmnd) > FSF_FCP_CMND_SIZE);
2272 	fcp_cmnd = &req->qtcb->bottom.io.fcp_cmnd.iu;
2273 	zfcp_fc_scsi_to_fcp(fcp_cmnd, scsi_cmnd);
2274 
2275 	if ((scsi_get_prot_op(scsi_cmnd) != SCSI_PROT_NORMAL) &&
2276 	    scsi_prot_sg_count(scsi_cmnd)) {
2277 		zfcp_qdio_set_data_div(qdio, &req->qdio_req,
2278 				       scsi_prot_sg_count(scsi_cmnd));
2279 		retval = zfcp_qdio_sbals_from_sg(qdio, &req->qdio_req,
2280 						 scsi_prot_sglist(scsi_cmnd));
2281 		if (retval)
2282 			goto failed_scsi_cmnd;
2283 		io->prot_data_length = zfcp_qdio_real_bytes(
2284 						scsi_prot_sglist(scsi_cmnd));
2285 	}
2286 
2287 	retval = zfcp_qdio_sbals_from_sg(qdio, &req->qdio_req,
2288 					 scsi_sglist(scsi_cmnd));
2289 	if (unlikely(retval))
2290 		goto failed_scsi_cmnd;
2291 
2292 	zfcp_qdio_set_sbale_last(adapter->qdio, &req->qdio_req);
2293 	if (zfcp_adapter_multi_buffer_active(adapter))
2294 		zfcp_qdio_set_scount(qdio, &req->qdio_req);
2295 
2296 	retval = zfcp_fsf_req_send(req);
2297 	if (unlikely(retval))
2298 		goto failed_scsi_cmnd;
2299 
2300 	goto out;
2301 
2302 failed_scsi_cmnd:
2303 	zfcp_fsf_req_free(req);
2304 	scsi_cmnd->host_scribble = NULL;
2305 out:
2306 	spin_unlock_irqrestore(&qdio->req_q_lock, flags);
2307 	return retval;
2308 }
2309 
2310 static void zfcp_fsf_fcp_task_mgmt_handler(struct zfcp_fsf_req *req)
2311 {
2312 	struct scsi_device *sdev = req->data;
2313 	struct fcp_resp_with_ext *fcp_rsp;
2314 	struct fcp_resp_rsp_info *rsp_info;
2315 
2316 	zfcp_fsf_fcp_handler_common(req, sdev);
2317 
2318 	fcp_rsp = &req->qtcb->bottom.io.fcp_rsp.iu;
2319 	rsp_info = (struct fcp_resp_rsp_info *) &fcp_rsp[1];
2320 
2321 	if ((rsp_info->rsp_code != FCP_TMF_CMPL) ||
2322 	     (req->status & ZFCP_STATUS_FSFREQ_ERROR))
2323 		req->status |= ZFCP_STATUS_FSFREQ_TMFUNCFAILED;
2324 }
2325 
2326 /**
2327  * zfcp_fsf_fcp_task_mgmt() - Send SCSI task management command (TMF).
2328  * @sdev: Pointer to SCSI device to send the task management command to.
2329  * @tm_flags: Unsigned byte for task management flags.
2330  *
2331  * Return: On success pointer to struct zfcp_fsf_req, %NULL otherwise.
2332  */
2333 struct zfcp_fsf_req *zfcp_fsf_fcp_task_mgmt(struct scsi_device *sdev,
2334 					    u8 tm_flags)
2335 {
2336 	struct zfcp_fsf_req *req = NULL;
2337 	struct fcp_cmnd *fcp_cmnd;
2338 	struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
2339 	struct zfcp_qdio *qdio = zfcp_sdev->port->adapter->qdio;
2340 
2341 	if (unlikely(!(atomic_read(&zfcp_sdev->status) &
2342 		       ZFCP_STATUS_COMMON_UNBLOCKED)))
2343 		return NULL;
2344 
2345 	spin_lock_irq(&qdio->req_q_lock);
2346 	if (zfcp_qdio_sbal_get(qdio))
2347 		goto out;
2348 
2349 	req = zfcp_fsf_req_create(qdio, FSF_QTCB_FCP_CMND,
2350 				  SBAL_SFLAGS0_TYPE_WRITE,
2351 				  qdio->adapter->pool.scsi_req);
2352 
2353 	if (IS_ERR(req)) {
2354 		req = NULL;
2355 		goto out;
2356 	}
2357 
2358 	req->data = sdev;
2359 
2360 	req->handler = zfcp_fsf_fcp_task_mgmt_handler;
2361 	req->qtcb->header.lun_handle = zfcp_sdev->lun_handle;
2362 	req->qtcb->header.port_handle = zfcp_sdev->port->handle;
2363 	req->qtcb->bottom.io.data_direction = FSF_DATADIR_CMND;
2364 	req->qtcb->bottom.io.service_class = FSF_CLASS_3;
2365 	req->qtcb->bottom.io.fcp_cmnd_length = FCP_CMND_LEN;
2366 
2367 	zfcp_qdio_set_sbale_last(qdio, &req->qdio_req);
2368 
2369 	fcp_cmnd = &req->qtcb->bottom.io.fcp_cmnd.iu;
2370 	zfcp_fc_fcp_tm(fcp_cmnd, sdev, tm_flags);
2371 
2372 	zfcp_fsf_start_timer(req, ZFCP_SCSI_ER_TIMEOUT);
2373 	if (!zfcp_fsf_req_send(req))
2374 		goto out;
2375 
2376 	zfcp_fsf_req_free(req);
2377 	req = NULL;
2378 out:
2379 	spin_unlock_irq(&qdio->req_q_lock);
2380 	return req;
2381 }
2382 
2383 /**
2384  * zfcp_fsf_reqid_check - validate req_id contained in SBAL returned by QDIO
2385  * @adapter: pointer to struct zfcp_adapter
2386  * @sbal_idx: response queue index of SBAL to be processed
2387  */
2388 void zfcp_fsf_reqid_check(struct zfcp_qdio *qdio, int sbal_idx)
2389 {
2390 	struct zfcp_adapter *adapter = qdio->adapter;
2391 	struct qdio_buffer *sbal = qdio->res_q[sbal_idx];
2392 	struct qdio_buffer_element *sbale;
2393 	struct zfcp_fsf_req *fsf_req;
2394 	unsigned long req_id;
2395 	int idx;
2396 
2397 	for (idx = 0; idx < QDIO_MAX_ELEMENTS_PER_BUFFER; idx++) {
2398 
2399 		sbale = &sbal->element[idx];
2400 		req_id = (unsigned long) sbale->addr;
2401 		fsf_req = zfcp_reqlist_find_rm(adapter->req_list, req_id);
2402 
2403 		if (!fsf_req) {
2404 			/*
2405 			 * Unknown request means that we have potentially memory
2406 			 * corruption and must stop the machine immediately.
2407 			 */
2408 			zfcp_qdio_siosl(adapter);
2409 			panic("error: unknown req_id (%lx) on adapter %s.\n",
2410 			      req_id, dev_name(&adapter->ccw_device->dev));
2411 		}
2412 
2413 		zfcp_fsf_req_complete(fsf_req);
2414 
2415 		if (likely(sbale->eflags & SBAL_EFLAGS_LAST_ENTRY))
2416 			break;
2417 	}
2418 }
2419