xref: /linux/drivers/s390/scsi/zfcp_fsf.c (revision f3d9478b2ce468c3115b02ecae7e975990697f15)
1 /*
2  * This file is part of the zfcp device driver for
3  * FCP adapters for IBM System z9 and zSeries.
4  *
5  * (C) Copyright IBM Corp. 2002, 2006
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2, or (at your option)
10  * any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  */
21 
22 #include "zfcp_ext.h"
23 
24 static int zfcp_fsf_exchange_config_data_handler(struct zfcp_fsf_req *);
25 static void zfcp_fsf_exchange_port_data_handler(struct zfcp_fsf_req *);
26 static int zfcp_fsf_open_port_handler(struct zfcp_fsf_req *);
27 static int zfcp_fsf_close_port_handler(struct zfcp_fsf_req *);
28 static int zfcp_fsf_close_physical_port_handler(struct zfcp_fsf_req *);
29 static int zfcp_fsf_open_unit_handler(struct zfcp_fsf_req *);
30 static int zfcp_fsf_close_unit_handler(struct zfcp_fsf_req *);
31 static int zfcp_fsf_send_fcp_command_handler(struct zfcp_fsf_req *);
32 static int zfcp_fsf_send_fcp_command_task_handler(struct zfcp_fsf_req *);
33 static int zfcp_fsf_send_fcp_command_task_management_handler(
34 	struct zfcp_fsf_req *);
35 static int zfcp_fsf_abort_fcp_command_handler(struct zfcp_fsf_req *);
36 static int zfcp_fsf_status_read_handler(struct zfcp_fsf_req *);
37 static int zfcp_fsf_send_ct_handler(struct zfcp_fsf_req *);
38 static int zfcp_fsf_send_els_handler(struct zfcp_fsf_req *);
39 static int zfcp_fsf_control_file_handler(struct zfcp_fsf_req *);
40 static inline int zfcp_fsf_req_sbal_check(
41 	unsigned long *, struct zfcp_qdio_queue *, int);
42 static inline int zfcp_use_one_sbal(
43 	struct scatterlist *, int, struct scatterlist *, int);
44 static struct zfcp_fsf_req *zfcp_fsf_req_alloc(mempool_t *, int);
45 static int zfcp_fsf_req_send(struct zfcp_fsf_req *, struct timer_list *);
46 static int zfcp_fsf_protstatus_eval(struct zfcp_fsf_req *);
47 static int zfcp_fsf_fsfstatus_eval(struct zfcp_fsf_req *);
48 static int zfcp_fsf_fsfstatus_qual_eval(struct zfcp_fsf_req *);
49 static void zfcp_fsf_link_down_info_eval(struct zfcp_adapter *,
50 	struct fsf_link_down_info *);
51 static int zfcp_fsf_req_dispatch(struct zfcp_fsf_req *);
52 static void zfcp_fsf_req_dismiss(struct zfcp_fsf_req *);
53 
54 /* association between FSF command and FSF QTCB type */
55 static u32 fsf_qtcb_type[] = {
56 	[FSF_QTCB_FCP_CMND] =             FSF_IO_COMMAND,
57 	[FSF_QTCB_ABORT_FCP_CMND] =       FSF_SUPPORT_COMMAND,
58 	[FSF_QTCB_OPEN_PORT_WITH_DID] =   FSF_SUPPORT_COMMAND,
59 	[FSF_QTCB_OPEN_LUN] =             FSF_SUPPORT_COMMAND,
60 	[FSF_QTCB_CLOSE_LUN] =            FSF_SUPPORT_COMMAND,
61 	[FSF_QTCB_CLOSE_PORT] =           FSF_SUPPORT_COMMAND,
62 	[FSF_QTCB_CLOSE_PHYSICAL_PORT] =  FSF_SUPPORT_COMMAND,
63 	[FSF_QTCB_SEND_ELS] =             FSF_SUPPORT_COMMAND,
64 	[FSF_QTCB_SEND_GENERIC] =         FSF_SUPPORT_COMMAND,
65 	[FSF_QTCB_EXCHANGE_CONFIG_DATA] = FSF_CONFIG_COMMAND,
66 	[FSF_QTCB_EXCHANGE_PORT_DATA] =   FSF_PORT_COMMAND,
67 	[FSF_QTCB_DOWNLOAD_CONTROL_FILE] = FSF_SUPPORT_COMMAND,
68 	[FSF_QTCB_UPLOAD_CONTROL_FILE] =  FSF_SUPPORT_COMMAND
69 };
70 
71 static const char zfcp_act_subtable_type[5][8] = {
72 	"unknown", "OS", "WWPN", "DID", "LUN"
73 };
74 
75 /****************************************************************/
76 /*************** FSF related Functions  *************************/
77 /****************************************************************/
78 
79 #define ZFCP_LOG_AREA			ZFCP_LOG_AREA_FSF
80 
81 /*
82  * function:	zfcp_fsf_req_alloc
83  *
84  * purpose:     Obtains an fsf_req and potentially a qtcb (for all but
85  *              unsolicited requests) via helper functions
86  *              Does some initial fsf request set-up.
87  *
88  * returns:	pointer to allocated fsf_req if successfull
89  *              NULL otherwise
90  *
91  * locks:       none
92  *
93  */
94 static struct zfcp_fsf_req *
95 zfcp_fsf_req_alloc(mempool_t *pool, int req_flags)
96 {
97 	size_t size;
98 	void *ptr;
99 	struct zfcp_fsf_req *fsf_req = NULL;
100 
101 	if (req_flags & ZFCP_REQ_NO_QTCB)
102 		size = sizeof(struct zfcp_fsf_req);
103 	else
104 		size = sizeof(struct zfcp_fsf_req_pool_element);
105 
106 	if (likely(pool != NULL))
107 		ptr = mempool_alloc(pool, GFP_ATOMIC);
108 	else
109 		ptr = kmalloc(size, GFP_ATOMIC);
110 
111 	if (unlikely(NULL == ptr))
112 		goto out;
113 
114 	memset(ptr, 0, size);
115 
116 	if (req_flags & ZFCP_REQ_NO_QTCB) {
117 		fsf_req = (struct zfcp_fsf_req *) ptr;
118 	} else {
119 		fsf_req = &((struct zfcp_fsf_req_pool_element *) ptr)->fsf_req;
120 		fsf_req->qtcb =
121 			&((struct zfcp_fsf_req_pool_element *) ptr)->qtcb;
122 	}
123 
124 	fsf_req->pool = pool;
125 
126  out:
127 	return fsf_req;
128 }
129 
130 /*
131  * function:	zfcp_fsf_req_free
132  *
133  * purpose:     Frees the memory of an fsf_req (and potentially a qtcb) or
134  *              returns it into the pool via helper functions.
135  *
136  * returns:     sod all
137  *
138  * locks:       none
139  */
140 void
141 zfcp_fsf_req_free(struct zfcp_fsf_req *fsf_req)
142 {
143 	if (likely(fsf_req->pool != NULL))
144 		mempool_free(fsf_req, fsf_req->pool);
145 	else
146 		kfree(fsf_req);
147 }
148 
149 /*
150  * function:
151  *
152  * purpose:
153  *
154  * returns:
155  *
156  * note: qdio queues shall be down (no ongoing inbound processing)
157  */
158 int
159 zfcp_fsf_req_dismiss_all(struct zfcp_adapter *adapter)
160 {
161 	struct zfcp_fsf_req *fsf_req, *tmp;
162 	unsigned long flags;
163 	LIST_HEAD(remove_queue);
164 
165 	spin_lock_irqsave(&adapter->fsf_req_list_lock, flags);
166 	list_splice_init(&adapter->fsf_req_list_head, &remove_queue);
167 	atomic_set(&adapter->fsf_reqs_active, 0);
168 	spin_unlock_irqrestore(&adapter->fsf_req_list_lock, flags);
169 
170 	list_for_each_entry_safe(fsf_req, tmp, &remove_queue, list) {
171 		list_del(&fsf_req->list);
172 		zfcp_fsf_req_dismiss(fsf_req);
173 	}
174 
175 	return 0;
176 }
177 
178 /*
179  * function:
180  *
181  * purpose:
182  *
183  * returns:
184  */
185 static void
186 zfcp_fsf_req_dismiss(struct zfcp_fsf_req *fsf_req)
187 {
188 	fsf_req->status |= ZFCP_STATUS_FSFREQ_DISMISSED;
189 	zfcp_fsf_req_complete(fsf_req);
190 }
191 
192 /*
193  * function:    zfcp_fsf_req_complete
194  *
195  * purpose:	Updates active counts and timers for openfcp-reqs
196  *              May cleanup request after req_eval returns
197  *
198  * returns:	0 - success
199  *		!0 - failure
200  *
201  * context:
202  */
203 int
204 zfcp_fsf_req_complete(struct zfcp_fsf_req *fsf_req)
205 {
206 	int retval = 0;
207 	int cleanup;
208 
209 	if (unlikely(fsf_req->fsf_command == FSF_QTCB_UNSOLICITED_STATUS)) {
210 		ZFCP_LOG_DEBUG("Status read response received\n");
211 		/*
212 		 * Note: all cleanup handling is done in the callchain of
213 		 * the function call-chain below.
214 		 */
215 		zfcp_fsf_status_read_handler(fsf_req);
216 		goto out;
217 	} else
218 		zfcp_fsf_protstatus_eval(fsf_req);
219 
220 	/*
221 	 * fsf_req may be deleted due to waking up functions, so
222 	 * cleanup is saved here and used later
223 	 */
224 	if (likely(fsf_req->status & ZFCP_STATUS_FSFREQ_CLEANUP))
225 		cleanup = 1;
226 	else
227 		cleanup = 0;
228 
229 	fsf_req->status |= ZFCP_STATUS_FSFREQ_COMPLETED;
230 
231 	/* cleanup request if requested by initiator */
232 	if (likely(cleanup)) {
233 		ZFCP_LOG_TRACE("removing FSF request %p\n", fsf_req);
234 		/*
235 		 * lock must not be held here since it will be
236 		 * grabed by the called routine, too
237 		 */
238 		zfcp_fsf_req_free(fsf_req);
239 	} else {
240 		/* notify initiator waiting for the requests completion */
241 		ZFCP_LOG_TRACE("waking initiator of FSF request %p\n",fsf_req);
242 		/*
243 		 * FIXME: Race! We must not access fsf_req here as it might have been
244 		 * cleaned up already due to the set ZFCP_STATUS_FSFREQ_COMPLETED
245 		 * flag. It's an improbable case. But, we have the same paranoia for
246 		 * the cleanup flag already.
247 		 * Might better be handled using complete()?
248 		 * (setting the flag and doing wakeup ought to be atomic
249 		 *  with regard to checking the flag as long as waitqueue is
250 		 *  part of the to be released structure)
251 		 */
252 		wake_up(&fsf_req->completion_wq);
253 	}
254 
255  out:
256 	return retval;
257 }
258 
259 /*
260  * function:    zfcp_fsf_protstatus_eval
261  *
262  * purpose:	evaluates the QTCB of the finished FSF request
263  *		and initiates appropriate actions
264  *		(usually calling FSF command specific handlers)
265  *
266  * returns:
267  *
268  * context:
269  *
270  * locks:
271  */
272 static int
273 zfcp_fsf_protstatus_eval(struct zfcp_fsf_req *fsf_req)
274 {
275 	int retval = 0;
276 	struct zfcp_adapter *adapter = fsf_req->adapter;
277 	struct fsf_qtcb *qtcb = fsf_req->qtcb;
278 	union fsf_prot_status_qual *prot_status_qual =
279 		&qtcb->prefix.prot_status_qual;
280 
281 	zfcp_hba_dbf_event_fsf_response(fsf_req);
282 
283 	if (fsf_req->status & ZFCP_STATUS_FSFREQ_DISMISSED) {
284 		ZFCP_LOG_DEBUG("fsf_req 0x%lx has been dismissed\n",
285 			       (unsigned long) fsf_req);
286 		fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR |
287 			ZFCP_STATUS_FSFREQ_RETRY; /* only for SCSI cmnds. */
288 		goto skip_protstatus;
289 	}
290 
291 	/* log additional information provided by FSF (if any) */
292 	if (unlikely(qtcb->header.log_length)) {
293 		/* do not trust them ;-) */
294 		if (qtcb->header.log_start > sizeof(struct fsf_qtcb)) {
295 			ZFCP_LOG_NORMAL
296 			    ("bug: ULP (FSF logging) log data starts "
297 			     "beyond end of packet header. Ignored. "
298 			     "(start=%i, size=%li)\n",
299 			     qtcb->header.log_start,
300 			     sizeof(struct fsf_qtcb));
301 			goto forget_log;
302 		}
303 		if ((size_t) (qtcb->header.log_start + qtcb->header.log_length)
304 		    > sizeof(struct fsf_qtcb)) {
305 			ZFCP_LOG_NORMAL("bug: ULP (FSF logging) log data ends "
306 					"beyond end of packet header. Ignored. "
307 					"(start=%i, length=%i, size=%li)\n",
308 					qtcb->header.log_start,
309 					qtcb->header.log_length,
310 					sizeof(struct fsf_qtcb));
311 			goto forget_log;
312 		}
313 		ZFCP_LOG_TRACE("ULP log data: \n");
314 		ZFCP_HEX_DUMP(ZFCP_LOG_LEVEL_TRACE,
315 			      (char *) qtcb + qtcb->header.log_start,
316 			      qtcb->header.log_length);
317 	}
318  forget_log:
319 
320 	/* evaluate FSF Protocol Status */
321 	switch (qtcb->prefix.prot_status) {
322 
323 	case FSF_PROT_GOOD:
324 	case FSF_PROT_FSF_STATUS_PRESENTED:
325 		break;
326 
327 	case FSF_PROT_QTCB_VERSION_ERROR:
328 		ZFCP_LOG_NORMAL("error: The adapter %s contains "
329 				"microcode of version 0x%x, the device driver "
330 				"only supports 0x%x. Aborting.\n",
331 				zfcp_get_busid_by_adapter(adapter),
332 				prot_status_qual->version_error.fsf_version,
333 				ZFCP_QTCB_VERSION);
334 		zfcp_erp_adapter_shutdown(adapter, 0);
335 		fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
336 		break;
337 
338 	case FSF_PROT_SEQ_NUMB_ERROR:
339 		ZFCP_LOG_NORMAL("bug: Sequence number mismatch between "
340 				"driver (0x%x) and adapter %s (0x%x). "
341 				"Restarting all operations on this adapter.\n",
342 				qtcb->prefix.req_seq_no,
343 				zfcp_get_busid_by_adapter(adapter),
344 				prot_status_qual->sequence_error.exp_req_seq_no);
345 		zfcp_erp_adapter_reopen(adapter, 0);
346 		fsf_req->status |= ZFCP_STATUS_FSFREQ_RETRY;
347 		fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
348 		break;
349 
350 	case FSF_PROT_UNSUPP_QTCB_TYPE:
351 		ZFCP_LOG_NORMAL("error: Packet header type used by the "
352 				"device driver is incompatible with "
353 				"that used on adapter %s. "
354 				"Stopping all operations on this adapter.\n",
355 				zfcp_get_busid_by_adapter(adapter));
356 		zfcp_erp_adapter_shutdown(adapter, 0);
357 		fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
358 		break;
359 
360 	case FSF_PROT_HOST_CONNECTION_INITIALIZING:
361 		fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
362 		atomic_set_mask(ZFCP_STATUS_ADAPTER_HOST_CON_INIT,
363 				&(adapter->status));
364 		break;
365 
366 	case FSF_PROT_DUPLICATE_REQUEST_ID:
367 			ZFCP_LOG_NORMAL("bug: The request identifier 0x%Lx "
368 					"to the adapter %s is ambiguous. "
369 				"Stopping all operations on this adapter.\n",
370 				*(unsigned long long*)
371 				(&qtcb->bottom.support.req_handle),
372 					zfcp_get_busid_by_adapter(adapter));
373 		zfcp_erp_adapter_shutdown(adapter, 0);
374 		fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
375 		break;
376 
377 	case FSF_PROT_LINK_DOWN:
378 		zfcp_fsf_link_down_info_eval(adapter,
379 					     &prot_status_qual->link_down_info);
380 		zfcp_erp_adapter_reopen(adapter, 0);
381 		fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
382 		break;
383 
384 	case FSF_PROT_REEST_QUEUE:
385 		ZFCP_LOG_NORMAL("The local link to adapter with "
386 			      "%s was re-plugged. "
387 			      "Re-starting operations on this adapter.\n",
388 			      zfcp_get_busid_by_adapter(adapter));
389 		/* All ports should be marked as ready to run again */
390 		zfcp_erp_modify_adapter_status(adapter,
391 					       ZFCP_STATUS_COMMON_RUNNING,
392 					       ZFCP_SET);
393 		zfcp_erp_adapter_reopen(adapter,
394 					ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED
395 					| ZFCP_STATUS_COMMON_ERP_FAILED);
396 		fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
397 		break;
398 
399 	case FSF_PROT_ERROR_STATE:
400 		ZFCP_LOG_NORMAL("error: The adapter %s "
401 				"has entered the error state. "
402 				"Restarting all operations on this "
403 				"adapter.\n",
404 				zfcp_get_busid_by_adapter(adapter));
405 		zfcp_erp_adapter_reopen(adapter, 0);
406 		fsf_req->status |= ZFCP_STATUS_FSFREQ_RETRY;
407 		fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
408 		break;
409 
410 	default:
411 		ZFCP_LOG_NORMAL("bug: Transfer protocol status information "
412 				"provided by the adapter %s "
413 				"is not compatible with the device driver. "
414 				"Stopping all operations on this adapter. "
415 				"(debug info 0x%x).\n",
416 				zfcp_get_busid_by_adapter(adapter),
417 				qtcb->prefix.prot_status);
418 		zfcp_erp_adapter_shutdown(adapter, 0);
419 		fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
420 	}
421 
422  skip_protstatus:
423 	/*
424 	 * always call specific handlers to give them a chance to do
425 	 * something meaningful even in error cases
426 	 */
427 	zfcp_fsf_fsfstatus_eval(fsf_req);
428 	return retval;
429 }
430 
431 /*
432  * function:	zfcp_fsf_fsfstatus_eval
433  *
434  * purpose:	evaluates FSF status of completed FSF request
435  *		and acts accordingly
436  *
437  * returns:
438  */
439 static int
440 zfcp_fsf_fsfstatus_eval(struct zfcp_fsf_req *fsf_req)
441 {
442 	int retval = 0;
443 
444 	if (unlikely(fsf_req->status & ZFCP_STATUS_FSFREQ_ERROR)) {
445 		goto skip_fsfstatus;
446 	}
447 
448 	/* evaluate FSF Status */
449 	switch (fsf_req->qtcb->header.fsf_status) {
450 	case FSF_UNKNOWN_COMMAND:
451 		ZFCP_LOG_NORMAL("bug: Command issued by the device driver is "
452 				"not known by the adapter %s "
453 				"Stopping all operations on this adapter. "
454 				"(debug info 0x%x).\n",
455 				zfcp_get_busid_by_adapter(fsf_req->adapter),
456 				fsf_req->qtcb->header.fsf_command);
457 		zfcp_erp_adapter_shutdown(fsf_req->adapter, 0);
458 		fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
459 		break;
460 
461 	case FSF_FCP_RSP_AVAILABLE:
462 		ZFCP_LOG_DEBUG("FCP Sense data will be presented to the "
463 			       "SCSI stack.\n");
464 		break;
465 
466 	case FSF_ADAPTER_STATUS_AVAILABLE:
467 		zfcp_fsf_fsfstatus_qual_eval(fsf_req);
468 		break;
469 	}
470 
471  skip_fsfstatus:
472 	/*
473 	 * always call specific handlers to give them a chance to do
474 	 * something meaningful even in error cases
475 	 */
476 	zfcp_fsf_req_dispatch(fsf_req);
477 
478 	return retval;
479 }
480 
481 /*
482  * function:	zfcp_fsf_fsfstatus_qual_eval
483  *
484  * purpose:	evaluates FSF status-qualifier of completed FSF request
485  *		and acts accordingly
486  *
487  * returns:
488  */
489 static int
490 zfcp_fsf_fsfstatus_qual_eval(struct zfcp_fsf_req *fsf_req)
491 {
492 	int retval = 0;
493 
494 	switch (fsf_req->qtcb->header.fsf_status_qual.word[0]) {
495 	case FSF_SQ_FCP_RSP_AVAILABLE:
496 		break;
497 	case FSF_SQ_RETRY_IF_POSSIBLE:
498 		/* The SCSI-stack may now issue retries or escalate */
499 		fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
500 		break;
501 	case FSF_SQ_COMMAND_ABORTED:
502 		/* Carry the aborted state on to upper layer */
503 		fsf_req->status |= ZFCP_STATUS_FSFREQ_ABORTED;
504 		fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
505 		break;
506 	case FSF_SQ_NO_RECOM:
507 		ZFCP_LOG_NORMAL("bug: No recommendation could be given for a"
508 				"problem on the adapter %s "
509 				"Stopping all operations on this adapter. ",
510 				zfcp_get_busid_by_adapter(fsf_req->adapter));
511 		zfcp_erp_adapter_shutdown(fsf_req->adapter, 0);
512 		fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
513 		break;
514 	case FSF_SQ_ULP_PROGRAMMING_ERROR:
515 		ZFCP_LOG_NORMAL("error: not enough SBALs for data transfer "
516 				"(adapter %s)\n",
517 				zfcp_get_busid_by_adapter(fsf_req->adapter));
518 		fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
519 		break;
520 	case FSF_SQ_INVOKE_LINK_TEST_PROCEDURE:
521 	case FSF_SQ_NO_RETRY_POSSIBLE:
522 	case FSF_SQ_ULP_DEPENDENT_ERP_REQUIRED:
523 		/* dealt with in the respective functions */
524 		break;
525 	default:
526 		ZFCP_LOG_NORMAL("bug: Additional status info could "
527 				"not be interpreted properly.\n");
528 		ZFCP_HEX_DUMP(ZFCP_LOG_LEVEL_NORMAL,
529 			      (char *) &fsf_req->qtcb->header.fsf_status_qual,
530 			      sizeof (union fsf_status_qual));
531 		fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
532 		break;
533 	}
534 
535 	return retval;
536 }
537 
538 /**
539  * zfcp_fsf_link_down_info_eval - evaluate link down information block
540  */
541 static void
542 zfcp_fsf_link_down_info_eval(struct zfcp_adapter *adapter,
543 			     struct fsf_link_down_info *link_down)
544 {
545 	if (atomic_test_mask(ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED,
546 	                     &adapter->status))
547 		return;
548 
549 	atomic_set_mask(ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED, &adapter->status);
550 
551 	if (link_down == NULL)
552 		goto out;
553 
554 	switch (link_down->error_code) {
555 	case FSF_PSQ_LINK_NO_LIGHT:
556 		ZFCP_LOG_NORMAL("The local link to adapter %s is down "
557 				"(no light detected)\n",
558 				zfcp_get_busid_by_adapter(adapter));
559 		break;
560 	case FSF_PSQ_LINK_WRAP_PLUG:
561 		ZFCP_LOG_NORMAL("The local link to adapter %s is down "
562 				"(wrap plug detected)\n",
563 				zfcp_get_busid_by_adapter(adapter));
564 		break;
565 	case FSF_PSQ_LINK_NO_FCP:
566 		ZFCP_LOG_NORMAL("The local link to adapter %s is down "
567 				"(adjacent node on link does not support FCP)\n",
568 				zfcp_get_busid_by_adapter(adapter));
569 		break;
570 	case FSF_PSQ_LINK_FIRMWARE_UPDATE:
571 		ZFCP_LOG_NORMAL("The local link to adapter %s is down "
572 				"(firmware update in progress)\n",
573 				zfcp_get_busid_by_adapter(adapter));
574 			break;
575 	case FSF_PSQ_LINK_INVALID_WWPN:
576 		ZFCP_LOG_NORMAL("The local link to adapter %s is down "
577 				"(duplicate or invalid WWPN detected)\n",
578 				zfcp_get_busid_by_adapter(adapter));
579 		break;
580 	case FSF_PSQ_LINK_NO_NPIV_SUPPORT:
581 		ZFCP_LOG_NORMAL("The local link to adapter %s is down "
582 				"(no support for NPIV by Fabric)\n",
583 				zfcp_get_busid_by_adapter(adapter));
584 		break;
585 	case FSF_PSQ_LINK_NO_FCP_RESOURCES:
586 		ZFCP_LOG_NORMAL("The local link to adapter %s is down "
587 				"(out of resource in FCP daughtercard)\n",
588 				zfcp_get_busid_by_adapter(adapter));
589 		break;
590 	case FSF_PSQ_LINK_NO_FABRIC_RESOURCES:
591 		ZFCP_LOG_NORMAL("The local link to adapter %s is down "
592 				"(out of resource in Fabric)\n",
593 				zfcp_get_busid_by_adapter(adapter));
594 		break;
595 	case FSF_PSQ_LINK_FABRIC_LOGIN_UNABLE:
596 		ZFCP_LOG_NORMAL("The local link to adapter %s is down "
597 				"(unable to Fabric login)\n",
598 				zfcp_get_busid_by_adapter(adapter));
599 		break;
600 	case FSF_PSQ_LINK_WWPN_ASSIGNMENT_CORRUPTED:
601 		ZFCP_LOG_NORMAL("WWPN assignment file corrupted on adapter %s\n",
602 				zfcp_get_busid_by_adapter(adapter));
603 		break;
604 	case FSF_PSQ_LINK_MODE_TABLE_CURRUPTED:
605 		ZFCP_LOG_NORMAL("Mode table corrupted on adapter %s\n",
606 				zfcp_get_busid_by_adapter(adapter));
607 		break;
608 	case FSF_PSQ_LINK_NO_WWPN_ASSIGNMENT:
609 		ZFCP_LOG_NORMAL("No WWPN for assignment table on adapter %s\n",
610 				zfcp_get_busid_by_adapter(adapter));
611 		break;
612 	default:
613 		ZFCP_LOG_NORMAL("The local link to adapter %s is down "
614 				"(warning: unknown reason code %d)\n",
615 				zfcp_get_busid_by_adapter(adapter),
616 				link_down->error_code);
617 	}
618 
619 	if (adapter->connection_features & FSF_FEATURE_NPIV_MODE)
620 		ZFCP_LOG_DEBUG("Debug information to link down: "
621 		               "primary_status=0x%02x "
622 		               "ioerr_code=0x%02x "
623 		               "action_code=0x%02x "
624 		               "reason_code=0x%02x "
625 		               "explanation_code=0x%02x "
626 		               "vendor_specific_code=0x%02x\n",
627 				link_down->primary_status,
628 				link_down->ioerr_code,
629 				link_down->action_code,
630 				link_down->reason_code,
631 				link_down->explanation_code,
632 				link_down->vendor_specific_code);
633 
634  out:
635 	zfcp_erp_adapter_failed(adapter);
636 }
637 
638 /*
639  * function:	zfcp_fsf_req_dispatch
640  *
641  * purpose:	calls the appropriate command specific handler
642  *
643  * returns:
644  */
645 static int
646 zfcp_fsf_req_dispatch(struct zfcp_fsf_req *fsf_req)
647 {
648 	struct zfcp_erp_action *erp_action = fsf_req->erp_action;
649 	struct zfcp_adapter *adapter = fsf_req->adapter;
650 	int retval = 0;
651 
652 
653 	switch (fsf_req->fsf_command) {
654 
655 	case FSF_QTCB_FCP_CMND:
656 		zfcp_fsf_send_fcp_command_handler(fsf_req);
657 		break;
658 
659 	case FSF_QTCB_ABORT_FCP_CMND:
660 		zfcp_fsf_abort_fcp_command_handler(fsf_req);
661 		break;
662 
663 	case FSF_QTCB_SEND_GENERIC:
664 		zfcp_fsf_send_ct_handler(fsf_req);
665 		break;
666 
667 	case FSF_QTCB_OPEN_PORT_WITH_DID:
668 		zfcp_fsf_open_port_handler(fsf_req);
669 		break;
670 
671 	case FSF_QTCB_OPEN_LUN:
672 		zfcp_fsf_open_unit_handler(fsf_req);
673 		break;
674 
675 	case FSF_QTCB_CLOSE_LUN:
676 		zfcp_fsf_close_unit_handler(fsf_req);
677 		break;
678 
679 	case FSF_QTCB_CLOSE_PORT:
680 		zfcp_fsf_close_port_handler(fsf_req);
681 		break;
682 
683 	case FSF_QTCB_CLOSE_PHYSICAL_PORT:
684 		zfcp_fsf_close_physical_port_handler(fsf_req);
685 		break;
686 
687 	case FSF_QTCB_EXCHANGE_CONFIG_DATA:
688 		zfcp_fsf_exchange_config_data_handler(fsf_req);
689 		break;
690 
691 	case FSF_QTCB_EXCHANGE_PORT_DATA:
692 		zfcp_fsf_exchange_port_data_handler(fsf_req);
693 		break;
694 
695 	case FSF_QTCB_SEND_ELS:
696 		zfcp_fsf_send_els_handler(fsf_req);
697 		break;
698 
699 	case FSF_QTCB_DOWNLOAD_CONTROL_FILE:
700 		zfcp_fsf_control_file_handler(fsf_req);
701 		break;
702 
703 	case FSF_QTCB_UPLOAD_CONTROL_FILE:
704 		zfcp_fsf_control_file_handler(fsf_req);
705 		break;
706 
707 	default:
708 		fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
709 		ZFCP_LOG_NORMAL("bug: Command issued by the device driver is "
710 				"not supported by the adapter %s\n",
711 				zfcp_get_busid_by_adapter(adapter));
712 		if (fsf_req->fsf_command != fsf_req->qtcb->header.fsf_command)
713 			ZFCP_LOG_NORMAL
714 			    ("bug: Command issued by the device driver differs "
715 			     "from the command returned by the adapter %s "
716 			     "(debug info 0x%x, 0x%x).\n",
717 			     zfcp_get_busid_by_adapter(adapter),
718 			     fsf_req->fsf_command,
719 			     fsf_req->qtcb->header.fsf_command);
720 	}
721 
722 	if (!erp_action)
723 		return retval;
724 
725 	zfcp_erp_async_handler(erp_action, 0);
726 
727 	return retval;
728 }
729 
730 /*
731  * function:    zfcp_fsf_status_read
732  *
733  * purpose:	initiates a Status Read command at the specified adapter
734  *
735  * returns:
736  */
737 int
738 zfcp_fsf_status_read(struct zfcp_adapter *adapter, int req_flags)
739 {
740 	struct zfcp_fsf_req *fsf_req;
741 	struct fsf_status_read_buffer *status_buffer;
742 	unsigned long lock_flags;
743 	volatile struct qdio_buffer_element *sbale;
744 	int retval = 0;
745 
746 	/* setup new FSF request */
747 	retval = zfcp_fsf_req_create(adapter, FSF_QTCB_UNSOLICITED_STATUS,
748 				     req_flags | ZFCP_REQ_NO_QTCB,
749 				     adapter->pool.fsf_req_status_read,
750 				     &lock_flags, &fsf_req);
751 	if (retval < 0) {
752 		ZFCP_LOG_INFO("error: Could not create unsolicited status "
753 			      "buffer for adapter %s.\n",
754 			      zfcp_get_busid_by_adapter(adapter));
755 		goto failed_req_create;
756 	}
757 
758 	sbale = zfcp_qdio_sbale_req(fsf_req, fsf_req->sbal_curr, 0);
759         sbale[0].flags |= SBAL_FLAGS0_TYPE_STATUS;
760         sbale[2].flags |= SBAL_FLAGS_LAST_ENTRY;
761         fsf_req->sbale_curr = 2;
762 
763 	status_buffer =
764 		mempool_alloc(adapter->pool.data_status_read, GFP_ATOMIC);
765 	if (!status_buffer) {
766 		ZFCP_LOG_NORMAL("bug: could not get some buffer\n");
767 		goto failed_buf;
768 	}
769 	memset(status_buffer, 0, sizeof (struct fsf_status_read_buffer));
770 	fsf_req->data = (unsigned long) status_buffer;
771 
772 	/* insert pointer to respective buffer */
773 	sbale = zfcp_qdio_sbale_curr(fsf_req);
774 	sbale->addr = (void *) status_buffer;
775 	sbale->length = sizeof(struct fsf_status_read_buffer);
776 
777 	/* start QDIO request for this FSF request */
778 	retval = zfcp_fsf_req_send(fsf_req, NULL);
779 	if (retval) {
780 		ZFCP_LOG_DEBUG("error: Could not set-up unsolicited status "
781 			       "environment.\n");
782 		goto failed_req_send;
783 	}
784 
785 	ZFCP_LOG_TRACE("Status Read request initiated (adapter%s)\n",
786 		       zfcp_get_busid_by_adapter(adapter));
787 	goto out;
788 
789  failed_req_send:
790 	mempool_free(status_buffer, adapter->pool.data_status_read);
791 
792  failed_buf:
793 	zfcp_fsf_req_free(fsf_req);
794  failed_req_create:
795 	zfcp_hba_dbf_event_fsf_unsol("fail", adapter, NULL);
796  out:
797 	write_unlock_irqrestore(&adapter->request_queue.queue_lock, lock_flags);
798 	return retval;
799 }
800 
801 static int
802 zfcp_fsf_status_read_port_closed(struct zfcp_fsf_req *fsf_req)
803 {
804 	struct fsf_status_read_buffer *status_buffer;
805 	struct zfcp_adapter *adapter;
806 	struct zfcp_port *port;
807 	unsigned long flags;
808 
809 	status_buffer = (struct fsf_status_read_buffer *) fsf_req->data;
810 	adapter = fsf_req->adapter;
811 
812 	read_lock_irqsave(&zfcp_data.config_lock, flags);
813 	list_for_each_entry(port, &adapter->port_list_head, list)
814 	    if (port->d_id == (status_buffer->d_id & ZFCP_DID_MASK))
815 		break;
816 	read_unlock_irqrestore(&zfcp_data.config_lock, flags);
817 
818 	if (!port || (port->d_id != (status_buffer->d_id & ZFCP_DID_MASK))) {
819 		ZFCP_LOG_NORMAL("bug: Reopen port indication received for"
820 				"nonexisting port with d_id 0x%08x on "
821 				"adapter %s. Ignored.\n",
822 				status_buffer->d_id & ZFCP_DID_MASK,
823 				zfcp_get_busid_by_adapter(adapter));
824 		goto out;
825 	}
826 
827 	switch (status_buffer->status_subtype) {
828 
829 	case FSF_STATUS_READ_SUB_CLOSE_PHYS_PORT:
830 		debug_text_event(adapter->erp_dbf, 3, "unsol_pc_phys:");
831 		zfcp_erp_port_reopen(port, 0);
832 		break;
833 
834 	case FSF_STATUS_READ_SUB_ERROR_PORT:
835 		debug_text_event(adapter->erp_dbf, 1, "unsol_pc_err:");
836 		zfcp_erp_port_shutdown(port, 0);
837 		break;
838 
839 	default:
840 		debug_text_event(adapter->erp_dbf, 0, "unsol_unk_sub:");
841 		debug_exception(adapter->erp_dbf, 0,
842 				&status_buffer->status_subtype, sizeof (u32));
843 		ZFCP_LOG_NORMAL("bug: Undefined status subtype received "
844 				"for a reopen indication on port with "
845 				"d_id 0x%08x on the adapter %s. "
846 				"Ignored. (debug info 0x%x)\n",
847 				status_buffer->d_id,
848 				zfcp_get_busid_by_adapter(adapter),
849 				status_buffer->status_subtype);
850 	}
851  out:
852 	return 0;
853 }
854 
855 /*
856  * function:    zfcp_fsf_status_read_handler
857  *
858  * purpose:	is called for finished Open Port command
859  *
860  * returns:
861  */
862 static int
863 zfcp_fsf_status_read_handler(struct zfcp_fsf_req *fsf_req)
864 {
865 	int retval = 0;
866 	struct zfcp_adapter *adapter = fsf_req->adapter;
867 	struct fsf_status_read_buffer *status_buffer =
868 		(struct fsf_status_read_buffer *) fsf_req->data;
869 	struct fsf_bit_error_payload *fsf_bit_error;
870 
871 	if (fsf_req->status & ZFCP_STATUS_FSFREQ_DISMISSED) {
872 		zfcp_hba_dbf_event_fsf_unsol("dism", adapter, status_buffer);
873 		mempool_free(status_buffer, adapter->pool.data_status_read);
874 		zfcp_fsf_req_free(fsf_req);
875 		goto out;
876 	}
877 
878 	zfcp_hba_dbf_event_fsf_unsol("read", adapter, status_buffer);
879 
880 	switch (status_buffer->status_type) {
881 
882 	case FSF_STATUS_READ_PORT_CLOSED:
883 		zfcp_fsf_status_read_port_closed(fsf_req);
884 		break;
885 
886 	case FSF_STATUS_READ_INCOMING_ELS:
887 		zfcp_fsf_incoming_els(fsf_req);
888 		break;
889 
890 	case FSF_STATUS_READ_SENSE_DATA_AVAIL:
891 		ZFCP_LOG_INFO("unsolicited sense data received (adapter %s)\n",
892 			      zfcp_get_busid_by_adapter(adapter));
893 		break;
894 
895 	case FSF_STATUS_READ_BIT_ERROR_THRESHOLD:
896 		fsf_bit_error = (struct fsf_bit_error_payload *)
897 			status_buffer->payload;
898 		ZFCP_LOG_NORMAL("Warning: bit error threshold data "
899 		    "received (adapter %s, "
900 		    "link failures = %i, loss of sync errors = %i, "
901 		    "loss of signal errors = %i, "
902 		    "primitive sequence errors = %i, "
903 		    "invalid transmission word errors = %i, "
904 		    "CRC errors = %i)\n",
905 		    zfcp_get_busid_by_adapter(adapter),
906 		    fsf_bit_error->link_failure_error_count,
907 		    fsf_bit_error->loss_of_sync_error_count,
908 		    fsf_bit_error->loss_of_signal_error_count,
909 		    fsf_bit_error->primitive_sequence_error_count,
910 		    fsf_bit_error->invalid_transmission_word_error_count,
911 		    fsf_bit_error->crc_error_count);
912 		ZFCP_LOG_INFO("Additional bit error threshold data "
913 		    "(adapter %s, "
914 		    "primitive sequence event time-outs = %i, "
915 		    "elastic buffer overrun errors = %i, "
916 		    "advertised receive buffer-to-buffer credit = %i, "
917 		    "current receice buffer-to-buffer credit = %i, "
918 		    "advertised transmit buffer-to-buffer credit = %i, "
919 		    "current transmit buffer-to-buffer credit = %i)\n",
920 		    zfcp_get_busid_by_adapter(adapter),
921 		    fsf_bit_error->primitive_sequence_event_timeout_count,
922 		    fsf_bit_error->elastic_buffer_overrun_error_count,
923 		    fsf_bit_error->advertised_receive_b2b_credit,
924 		    fsf_bit_error->current_receive_b2b_credit,
925 		    fsf_bit_error->advertised_transmit_b2b_credit,
926 		    fsf_bit_error->current_transmit_b2b_credit);
927 		break;
928 
929 	case FSF_STATUS_READ_LINK_DOWN:
930 		switch (status_buffer->status_subtype) {
931 		case FSF_STATUS_READ_SUB_NO_PHYSICAL_LINK:
932 			ZFCP_LOG_INFO("Physical link to adapter %s is down\n",
933 				      zfcp_get_busid_by_adapter(adapter));
934 			zfcp_fsf_link_down_info_eval(adapter,
935 				(struct fsf_link_down_info *)
936 				&status_buffer->payload);
937 			break;
938 		case FSF_STATUS_READ_SUB_FDISC_FAILED:
939 			ZFCP_LOG_INFO("Local link to adapter %s is down "
940 				      "due to failed FDISC login\n",
941 				      zfcp_get_busid_by_adapter(adapter));
942 			zfcp_fsf_link_down_info_eval(adapter,
943 				(struct fsf_link_down_info *)
944 				&status_buffer->payload);
945 			break;
946 		case FSF_STATUS_READ_SUB_FIRMWARE_UPDATE:
947 			ZFCP_LOG_INFO("Local link to adapter %s is down "
948 				      "due to firmware update on adapter\n",
949 				      zfcp_get_busid_by_adapter(adapter));
950 			zfcp_fsf_link_down_info_eval(adapter, NULL);
951 			break;
952 		default:
953 			ZFCP_LOG_INFO("Local link to adapter %s is down "
954 				      "due to unknown reason\n",
955 				      zfcp_get_busid_by_adapter(adapter));
956 			zfcp_fsf_link_down_info_eval(adapter, NULL);
957 		};
958 		break;
959 
960 	case FSF_STATUS_READ_LINK_UP:
961 		ZFCP_LOG_NORMAL("Local link to adapter %s was replugged. "
962 				"Restarting operations on this adapter\n",
963 				zfcp_get_busid_by_adapter(adapter));
964 		/* All ports should be marked as ready to run again */
965 		zfcp_erp_modify_adapter_status(adapter,
966 					       ZFCP_STATUS_COMMON_RUNNING,
967 					       ZFCP_SET);
968 		zfcp_erp_adapter_reopen(adapter,
969 					ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED
970 					| ZFCP_STATUS_COMMON_ERP_FAILED);
971 		break;
972 
973 	case FSF_STATUS_READ_NOTIFICATION_LOST:
974 		ZFCP_LOG_NORMAL("Unsolicited status notification(s) lost: "
975 				"adapter %s%s%s%s%s%s%s%s%s\n",
976 				zfcp_get_busid_by_adapter(adapter),
977 				(status_buffer->status_subtype &
978 					FSF_STATUS_READ_SUB_INCOMING_ELS) ?
979 					", incoming ELS" : "",
980 				(status_buffer->status_subtype &
981 					FSF_STATUS_READ_SUB_SENSE_DATA) ?
982 					", sense data" : "",
983 				(status_buffer->status_subtype &
984 					FSF_STATUS_READ_SUB_LINK_STATUS) ?
985 					", link status change" : "",
986 				(status_buffer->status_subtype &
987 					FSF_STATUS_READ_SUB_PORT_CLOSED) ?
988 					", port close" : "",
989 				(status_buffer->status_subtype &
990 					FSF_STATUS_READ_SUB_BIT_ERROR_THRESHOLD) ?
991 					", bit error exception" : "",
992 				(status_buffer->status_subtype &
993 					FSF_STATUS_READ_SUB_ACT_UPDATED) ?
994 					", ACT update" : "",
995 				(status_buffer->status_subtype &
996 					FSF_STATUS_READ_SUB_ACT_HARDENED) ?
997 					", ACT hardening" : "",
998 				(status_buffer->status_subtype &
999 					FSF_STATUS_READ_SUB_FEATURE_UPDATE_ALERT) ?
1000 					", adapter feature change" : "");
1001 
1002 		if (status_buffer->status_subtype &
1003 		    FSF_STATUS_READ_SUB_ACT_UPDATED)
1004 			zfcp_erp_adapter_access_changed(adapter);
1005 		break;
1006 
1007 	case FSF_STATUS_READ_CFDC_UPDATED:
1008 		ZFCP_LOG_NORMAL("CFDC has been updated on the adapter %s\n",
1009 			      zfcp_get_busid_by_adapter(adapter));
1010 		zfcp_erp_adapter_access_changed(adapter);
1011 		break;
1012 
1013 	case FSF_STATUS_READ_CFDC_HARDENED:
1014 		switch (status_buffer->status_subtype) {
1015 		case FSF_STATUS_READ_SUB_CFDC_HARDENED_ON_SE:
1016 			ZFCP_LOG_NORMAL("CFDC of adapter %s saved on SE\n",
1017 				      zfcp_get_busid_by_adapter(adapter));
1018 			break;
1019 		case FSF_STATUS_READ_SUB_CFDC_HARDENED_ON_SE2:
1020 			ZFCP_LOG_NORMAL("CFDC of adapter %s has been copied "
1021 				      "to the secondary SE\n",
1022 				zfcp_get_busid_by_adapter(adapter));
1023 			break;
1024 		default:
1025 			ZFCP_LOG_NORMAL("CFDC of adapter %s has been hardened\n",
1026 				      zfcp_get_busid_by_adapter(adapter));
1027 		}
1028 		break;
1029 
1030 	case FSF_STATUS_READ_FEATURE_UPDATE_ALERT:
1031 		debug_text_event(adapter->erp_dbf, 2, "unsol_features:");
1032 		ZFCP_LOG_INFO("List of supported features on adapter %s has "
1033 			      "been changed from 0x%08X to 0x%08X\n",
1034 			      zfcp_get_busid_by_adapter(adapter),
1035 			      *(u32*) (status_buffer->payload + 4),
1036 			      *(u32*) (status_buffer->payload));
1037 		adapter->adapter_features = *(u32*) status_buffer->payload;
1038 		break;
1039 
1040 	default:
1041 		ZFCP_LOG_NORMAL("warning: An unsolicited status packet of unknown "
1042 				"type was received (debug info 0x%x)\n",
1043 				status_buffer->status_type);
1044 		ZFCP_LOG_DEBUG("Dump of status_read_buffer %p:\n",
1045 			       status_buffer);
1046 		ZFCP_HEX_DUMP(ZFCP_LOG_LEVEL_DEBUG,
1047 			      (char *) status_buffer,
1048 			      sizeof (struct fsf_status_read_buffer));
1049 		break;
1050 	}
1051 	mempool_free(status_buffer, adapter->pool.data_status_read);
1052 	zfcp_fsf_req_free(fsf_req);
1053 	/*
1054 	 * recycle buffer and start new request repeat until outbound
1055 	 * queue is empty or adapter shutdown is requested
1056 	 */
1057 	/*
1058 	 * FIXME(qdio):
1059 	 * we may wait in the req_create for 5s during shutdown, so
1060 	 * qdio_cleanup will have to wait at least that long before returning
1061 	 * with failure to allow us a proper cleanup under all circumstances
1062 	 */
1063 	/*
1064 	 * FIXME:
1065 	 * allocation failure possible? (Is this code needed?)
1066 	 */
1067 	retval = zfcp_fsf_status_read(adapter, 0);
1068 	if (retval < 0) {
1069 		ZFCP_LOG_INFO("Failed to create unsolicited status read "
1070 			      "request for the adapter %s.\n",
1071 			      zfcp_get_busid_by_adapter(adapter));
1072 		/* temporary fix to avoid status read buffer shortage */
1073 		adapter->status_read_failed++;
1074 		if ((ZFCP_STATUS_READS_RECOM - adapter->status_read_failed)
1075 		    < ZFCP_STATUS_READ_FAILED_THRESHOLD) {
1076 			ZFCP_LOG_INFO("restart adapter %s due to status read "
1077 				      "buffer shortage\n",
1078 				      zfcp_get_busid_by_adapter(adapter));
1079 			zfcp_erp_adapter_reopen(adapter, 0);
1080 		}
1081 	}
1082  out:
1083 	return retval;
1084 }
1085 
1086 /*
1087  * function:    zfcp_fsf_abort_fcp_command
1088  *
1089  * purpose:	tells FSF to abort a running SCSI command
1090  *
1091  * returns:	address of initiated FSF request
1092  *		NULL - request could not be initiated
1093  *
1094  * FIXME(design): should be watched by a timeout !!!
1095  * FIXME(design) shouldn't this be modified to return an int
1096  *               also...don't know how though
1097  */
1098 struct zfcp_fsf_req *
1099 zfcp_fsf_abort_fcp_command(unsigned long old_req_id,
1100 			   struct zfcp_adapter *adapter,
1101 			   struct zfcp_unit *unit, int req_flags)
1102 {
1103 	volatile struct qdio_buffer_element *sbale;
1104 	unsigned long lock_flags;
1105 	struct zfcp_fsf_req *fsf_req = NULL;
1106 	int retval = 0;
1107 
1108 	/* setup new FSF request */
1109 	retval = zfcp_fsf_req_create(adapter, FSF_QTCB_ABORT_FCP_CMND,
1110 				     req_flags, adapter->pool.fsf_req_abort,
1111 				     &lock_flags, &fsf_req);
1112 	if (retval < 0) {
1113 		ZFCP_LOG_INFO("error: Failed to create an abort command "
1114 			      "request for lun 0x%016Lx on port 0x%016Lx "
1115 			      "on adapter %s.\n",
1116 			      unit->fcp_lun,
1117 			      unit->port->wwpn,
1118 			      zfcp_get_busid_by_adapter(adapter));
1119 		goto out;
1120 	}
1121 
1122 	sbale = zfcp_qdio_sbale_req(fsf_req, fsf_req->sbal_curr, 0);
1123         sbale[0].flags |= SBAL_FLAGS0_TYPE_READ;
1124         sbale[1].flags |= SBAL_FLAGS_LAST_ENTRY;
1125 
1126 	fsf_req->data = (unsigned long) unit;
1127 
1128 	/* set handles of unit and its parent port in QTCB */
1129 	fsf_req->qtcb->header.lun_handle = unit->handle;
1130 	fsf_req->qtcb->header.port_handle = unit->port->handle;
1131 
1132 	/* set handle of request which should be aborted */
1133 	fsf_req->qtcb->bottom.support.req_handle = (u64) old_req_id;
1134 
1135 	/* start QDIO request for this FSF request */
1136 
1137 	zfcp_fsf_start_scsi_er_timer(adapter);
1138 	retval = zfcp_fsf_req_send(fsf_req, NULL);
1139 	if (retval) {
1140 		del_timer(&adapter->scsi_er_timer);
1141 		ZFCP_LOG_INFO("error: Failed to send abort command request "
1142 			      "on adapter %s, port 0x%016Lx, unit 0x%016Lx\n",
1143 			      zfcp_get_busid_by_adapter(adapter),
1144 			      unit->port->wwpn, unit->fcp_lun);
1145 		zfcp_fsf_req_free(fsf_req);
1146 		fsf_req = NULL;
1147 		goto out;
1148 	}
1149 
1150 	ZFCP_LOG_DEBUG("Abort FCP Command request initiated "
1151 		       "(adapter%s, port d_id=0x%08x, "
1152 		       "unit x%016Lx, old_req_id=0x%lx)\n",
1153 		       zfcp_get_busid_by_adapter(adapter),
1154 		       unit->port->d_id,
1155 		       unit->fcp_lun, old_req_id);
1156  out:
1157 	write_unlock_irqrestore(&adapter->request_queue.queue_lock, lock_flags);
1158 	return fsf_req;
1159 }
1160 
1161 /*
1162  * function:    zfcp_fsf_abort_fcp_command_handler
1163  *
1164  * purpose:	is called for finished Abort FCP Command request
1165  *
1166  * returns:
1167  */
1168 static int
1169 zfcp_fsf_abort_fcp_command_handler(struct zfcp_fsf_req *new_fsf_req)
1170 {
1171 	int retval = -EINVAL;
1172 	struct zfcp_unit *unit;
1173 	unsigned char status_qual =
1174 	    new_fsf_req->qtcb->header.fsf_status_qual.word[0];
1175 
1176 	del_timer(&new_fsf_req->adapter->scsi_er_timer);
1177 
1178 	if (new_fsf_req->status & ZFCP_STATUS_FSFREQ_ERROR) {
1179 		/* do not set ZFCP_STATUS_FSFREQ_ABORTSUCCEEDED */
1180 		goto skip_fsfstatus;
1181 	}
1182 
1183 	unit = (struct zfcp_unit *) new_fsf_req->data;
1184 
1185 	/* evaluate FSF status in QTCB */
1186 	switch (new_fsf_req->qtcb->header.fsf_status) {
1187 
1188 	case FSF_PORT_HANDLE_NOT_VALID:
1189 		if (status_qual >> 4 != status_qual % 0xf) {
1190 			debug_text_event(new_fsf_req->adapter->erp_dbf, 3,
1191 					 "fsf_s_phand_nv0");
1192 			/*
1193 			 * In this case a command that was sent prior to a port
1194 			 * reopen was aborted (handles are different). This is
1195 			 * fine.
1196 			 */
1197 		} else {
1198 			ZFCP_LOG_INFO("Temporary port identifier 0x%x for "
1199 				      "port 0x%016Lx on adapter %s invalid. "
1200 				      "This may happen occasionally.\n",
1201 				      unit->port->handle,
1202 				      unit->port->wwpn,
1203 				      zfcp_get_busid_by_unit(unit));
1204 			ZFCP_LOG_INFO("status qualifier:\n");
1205 			ZFCP_HEX_DUMP(ZFCP_LOG_LEVEL_INFO,
1206 				      (char *) &new_fsf_req->qtcb->header.
1207 				      fsf_status_qual,
1208 				      sizeof (union fsf_status_qual));
1209 			/* Let's hope this sorts out the mess */
1210 			debug_text_event(new_fsf_req->adapter->erp_dbf, 1,
1211 					 "fsf_s_phand_nv1");
1212 			zfcp_erp_adapter_reopen(unit->port->adapter, 0);
1213 			new_fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1214 		}
1215 		break;
1216 
1217 	case FSF_LUN_HANDLE_NOT_VALID:
1218 		if (status_qual >> 4 != status_qual % 0xf) {
1219 			/* 2 */
1220 			debug_text_event(new_fsf_req->adapter->erp_dbf, 3,
1221 					 "fsf_s_lhand_nv0");
1222 			/*
1223 			 * In this case a command that was sent prior to a unit
1224 			 * reopen was aborted (handles are different).
1225 			 * This is fine.
1226 			 */
1227 		} else {
1228 			ZFCP_LOG_INFO
1229 			    ("Warning: Temporary LUN identifier 0x%x of LUN "
1230 			     "0x%016Lx on port 0x%016Lx on adapter %s is "
1231 			     "invalid. This may happen in rare cases. "
1232 			     "Trying to re-establish link.\n",
1233 			     unit->handle,
1234 			     unit->fcp_lun,
1235 			     unit->port->wwpn,
1236 			     zfcp_get_busid_by_unit(unit));
1237 			ZFCP_LOG_DEBUG("Status qualifier data:\n");
1238 			ZFCP_HEX_DUMP(ZFCP_LOG_LEVEL_DEBUG,
1239 				      (char *) &new_fsf_req->qtcb->header.
1240 				      fsf_status_qual,
1241 				      sizeof (union fsf_status_qual));
1242 			/* Let's hope this sorts out the mess */
1243 			debug_text_event(new_fsf_req->adapter->erp_dbf, 1,
1244 					 "fsf_s_lhand_nv1");
1245 			zfcp_erp_port_reopen(unit->port, 0);
1246 			new_fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1247 		}
1248 		break;
1249 
1250 	case FSF_FCP_COMMAND_DOES_NOT_EXIST:
1251 		retval = 0;
1252 		debug_text_event(new_fsf_req->adapter->erp_dbf, 3,
1253 				 "fsf_s_no_exist");
1254 		new_fsf_req->status |= ZFCP_STATUS_FSFREQ_ABORTNOTNEEDED;
1255 		break;
1256 
1257 	case FSF_PORT_BOXED:
1258 		ZFCP_LOG_INFO("Remote port 0x%016Lx on adapter %s needs to "
1259 			      "be reopened\n", unit->port->wwpn,
1260 			      zfcp_get_busid_by_unit(unit));
1261 		debug_text_event(new_fsf_req->adapter->erp_dbf, 2,
1262 				 "fsf_s_pboxed");
1263 		zfcp_erp_port_boxed(unit->port);
1264 		new_fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR
1265 		    | ZFCP_STATUS_FSFREQ_RETRY;
1266 		break;
1267 
1268 	case FSF_LUN_BOXED:
1269                 ZFCP_LOG_INFO(
1270                         "unit 0x%016Lx on port 0x%016Lx on adapter %s needs "
1271                         "to be reopened\n",
1272                         unit->fcp_lun, unit->port->wwpn,
1273                         zfcp_get_busid_by_unit(unit));
1274                 debug_text_event(new_fsf_req->adapter->erp_dbf, 1, "fsf_s_lboxed");
1275 		zfcp_erp_unit_boxed(unit);
1276                 new_fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR
1277                         | ZFCP_STATUS_FSFREQ_RETRY;
1278                 break;
1279 
1280 	case FSF_ADAPTER_STATUS_AVAILABLE:
1281 		switch (new_fsf_req->qtcb->header.fsf_status_qual.word[0]) {
1282 		case FSF_SQ_INVOKE_LINK_TEST_PROCEDURE:
1283 			debug_text_event(new_fsf_req->adapter->erp_dbf, 1,
1284 					 "fsf_sq_ltest");
1285 			zfcp_test_link(unit->port);
1286 			new_fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1287 			break;
1288 		case FSF_SQ_ULP_DEPENDENT_ERP_REQUIRED:
1289 			/* SCSI stack will escalate */
1290 			debug_text_event(new_fsf_req->adapter->erp_dbf, 1,
1291 					 "fsf_sq_ulp");
1292 			new_fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1293 			break;
1294 		default:
1295 			ZFCP_LOG_NORMAL
1296 			    ("bug: Wrong status qualifier 0x%x arrived.\n",
1297 			     new_fsf_req->qtcb->header.fsf_status_qual.word[0]);
1298 			debug_text_event(new_fsf_req->adapter->erp_dbf, 0,
1299 					 "fsf_sq_inval:");
1300 			debug_exception(new_fsf_req->adapter->erp_dbf, 0,
1301 					&new_fsf_req->qtcb->header.
1302 					fsf_status_qual.word[0], sizeof (u32));
1303 			break;
1304 		}
1305 		break;
1306 
1307 	case FSF_GOOD:
1308 		retval = 0;
1309 		new_fsf_req->status |= ZFCP_STATUS_FSFREQ_ABORTSUCCEEDED;
1310 		break;
1311 
1312 	default:
1313 		ZFCP_LOG_NORMAL("bug: An unknown FSF Status was presented "
1314 				"(debug info 0x%x)\n",
1315 				new_fsf_req->qtcb->header.fsf_status);
1316 		debug_text_event(new_fsf_req->adapter->erp_dbf, 0,
1317 				 "fsf_s_inval:");
1318 		debug_exception(new_fsf_req->adapter->erp_dbf, 0,
1319 				&new_fsf_req->qtcb->header.fsf_status,
1320 				sizeof (u32));
1321 		break;
1322 	}
1323  skip_fsfstatus:
1324 	return retval;
1325 }
1326 
1327 /**
1328  * zfcp_use_one_sbal - checks whether req buffer and resp bother each fit into
1329  *	one SBALE
1330  * Two scatter-gather lists are passed, one for the reqeust and one for the
1331  * response.
1332  */
1333 static inline int
1334 zfcp_use_one_sbal(struct scatterlist *req, int req_count,
1335                   struct scatterlist *resp, int resp_count)
1336 {
1337         return ((req_count == 1) &&
1338 		(resp_count == 1) &&
1339                 (((unsigned long) zfcp_sg_to_address(&req[0]) &
1340 		  PAGE_MASK) ==
1341 		 ((unsigned long) (zfcp_sg_to_address(&req[0]) +
1342 				   req[0].length - 1) & PAGE_MASK)) &&
1343                 (((unsigned long) zfcp_sg_to_address(&resp[0]) &
1344 		  PAGE_MASK) ==
1345                  ((unsigned long) (zfcp_sg_to_address(&resp[0]) +
1346 				   resp[0].length - 1) & PAGE_MASK)));
1347 }
1348 
1349 /**
1350  * zfcp_fsf_send_ct - initiate a Generic Service request (FC-GS)
1351  * @ct: pointer to struct zfcp_send_ct which conatins all needed data for
1352  *	the request
1353  * @pool: pointer to memory pool, if non-null this pool is used to allocate
1354  *	a struct zfcp_fsf_req
1355  * @erp_action: pointer to erp_action, if non-null the Generic Service request
1356  *	is sent within error recovery
1357  */
1358 int
1359 zfcp_fsf_send_ct(struct zfcp_send_ct *ct, mempool_t *pool,
1360 		 struct zfcp_erp_action *erp_action)
1361 {
1362 	volatile struct qdio_buffer_element *sbale;
1363 	struct zfcp_port *port;
1364 	struct zfcp_adapter *adapter;
1365         struct zfcp_fsf_req *fsf_req;
1366         unsigned long lock_flags;
1367         int bytes;
1368 	int ret = 0;
1369 
1370 	port = ct->port;
1371 	adapter = port->adapter;
1372 
1373 	ret = zfcp_fsf_req_create(adapter, FSF_QTCB_SEND_GENERIC,
1374 				  ZFCP_WAIT_FOR_SBAL | ZFCP_REQ_AUTO_CLEANUP,
1375 				  pool, &lock_flags, &fsf_req);
1376 	if (ret < 0) {
1377                 ZFCP_LOG_INFO("error: Could not create CT request (FC-GS) for "
1378 			      "adapter: %s\n",
1379 			      zfcp_get_busid_by_adapter(adapter));
1380 		goto failed_req;
1381 	}
1382 
1383         if (erp_action != NULL) {
1384                 erp_action->fsf_req = fsf_req;
1385                 fsf_req->erp_action = erp_action;
1386         }
1387 
1388 	sbale = zfcp_qdio_sbale_req(fsf_req, fsf_req->sbal_curr, 0);
1389         if (zfcp_use_one_sbal(ct->req, ct->req_count,
1390                               ct->resp, ct->resp_count)){
1391                 /* both request buffer and response buffer
1392                    fit into one sbale each */
1393                 sbale[0].flags |= SBAL_FLAGS0_TYPE_WRITE_READ;
1394                 sbale[2].addr = zfcp_sg_to_address(&ct->req[0]);
1395                 sbale[2].length = ct->req[0].length;
1396                 sbale[3].addr = zfcp_sg_to_address(&ct->resp[0]);
1397                 sbale[3].length = ct->resp[0].length;
1398                 sbale[3].flags |= SBAL_FLAGS_LAST_ENTRY;
1399 	} else if (adapter->adapter_features &
1400                    FSF_FEATURE_ELS_CT_CHAINED_SBALS) {
1401                 /* try to use chained SBALs */
1402                 bytes = zfcp_qdio_sbals_from_sg(fsf_req,
1403                                                 SBAL_FLAGS0_TYPE_WRITE_READ,
1404                                                 ct->req, ct->req_count,
1405                                                 ZFCP_MAX_SBALS_PER_CT_REQ);
1406                 if (bytes <= 0) {
1407                         ZFCP_LOG_INFO("error: creation of CT request failed "
1408 				      "on adapter %s\n",
1409 				      zfcp_get_busid_by_adapter(adapter));
1410                         if (bytes == 0)
1411                                 ret = -ENOMEM;
1412                         else
1413                                 ret = bytes;
1414 
1415                         goto failed_send;
1416                 }
1417                 fsf_req->qtcb->bottom.support.req_buf_length = bytes;
1418                 fsf_req->sbale_curr = ZFCP_LAST_SBALE_PER_SBAL;
1419                 bytes = zfcp_qdio_sbals_from_sg(fsf_req,
1420                                                 SBAL_FLAGS0_TYPE_WRITE_READ,
1421                                                 ct->resp, ct->resp_count,
1422                                                 ZFCP_MAX_SBALS_PER_CT_REQ);
1423                 if (bytes <= 0) {
1424                         ZFCP_LOG_INFO("error: creation of CT request failed "
1425 				      "on adapter %s\n",
1426 				      zfcp_get_busid_by_adapter(adapter));
1427                         if (bytes == 0)
1428                                 ret = -ENOMEM;
1429                         else
1430                                 ret = bytes;
1431 
1432                         goto failed_send;
1433                 }
1434                 fsf_req->qtcb->bottom.support.resp_buf_length = bytes;
1435         } else {
1436                 /* reject send generic request */
1437 		ZFCP_LOG_INFO(
1438 			"error: microcode does not support chained SBALs,"
1439                         "CT request too big (adapter %s)\n",
1440 			zfcp_get_busid_by_adapter(adapter));
1441                 ret = -EOPNOTSUPP;
1442                 goto failed_send;
1443         }
1444 
1445 	/* settings in QTCB */
1446 	fsf_req->qtcb->header.port_handle = port->handle;
1447 	fsf_req->qtcb->bottom.support.service_class =
1448 		ZFCP_FC_SERVICE_CLASS_DEFAULT;
1449 	fsf_req->qtcb->bottom.support.timeout = ct->timeout;
1450         fsf_req->data = (unsigned long) ct;
1451 
1452 	zfcp_san_dbf_event_ct_request(fsf_req);
1453 
1454 	/* start QDIO request for this FSF request */
1455 	ret = zfcp_fsf_req_send(fsf_req, ct->timer);
1456 	if (ret) {
1457 		ZFCP_LOG_DEBUG("error: initiation of CT request failed "
1458 			       "(adapter %s, port 0x%016Lx)\n",
1459 			       zfcp_get_busid_by_adapter(adapter), port->wwpn);
1460 		goto failed_send;
1461 	}
1462 
1463 	ZFCP_LOG_DEBUG("CT request initiated (adapter %s, port 0x%016Lx)\n",
1464 		       zfcp_get_busid_by_adapter(adapter), port->wwpn);
1465 	goto out;
1466 
1467  failed_send:
1468 	zfcp_fsf_req_free(fsf_req);
1469         if (erp_action != NULL) {
1470                 erp_action->fsf_req = NULL;
1471         }
1472  failed_req:
1473  out:
1474         write_unlock_irqrestore(&adapter->request_queue.queue_lock,
1475 				lock_flags);
1476 	return ret;
1477 }
1478 
1479 /**
1480  * zfcp_fsf_send_ct_handler - handler for Generic Service requests
1481  * @fsf_req: pointer to struct zfcp_fsf_req
1482  *
1483  * Data specific for the Generic Service request is passed using
1484  * fsf_req->data. There we find the pointer to struct zfcp_send_ct.
1485  * Usually a specific handler for the CT request is called which is
1486  * found in this structure.
1487  */
1488 static int
1489 zfcp_fsf_send_ct_handler(struct zfcp_fsf_req *fsf_req)
1490 {
1491 	struct zfcp_port *port;
1492 	struct zfcp_adapter *adapter;
1493 	struct zfcp_send_ct *send_ct;
1494 	struct fsf_qtcb_header *header;
1495 	struct fsf_qtcb_bottom_support *bottom;
1496 	int retval = -EINVAL;
1497 	u16 subtable, rule, counter;
1498 
1499 	adapter = fsf_req->adapter;
1500 	send_ct = (struct zfcp_send_ct *) fsf_req->data;
1501 	port = send_ct->port;
1502 	header = &fsf_req->qtcb->header;
1503 	bottom = &fsf_req->qtcb->bottom.support;
1504 
1505 	if (fsf_req->status & ZFCP_STATUS_FSFREQ_ERROR)
1506 		goto skip_fsfstatus;
1507 
1508 	/* evaluate FSF status in QTCB */
1509 	switch (header->fsf_status) {
1510 
1511         case FSF_GOOD:
1512 		zfcp_san_dbf_event_ct_response(fsf_req);
1513                 retval = 0;
1514 		break;
1515 
1516         case FSF_SERVICE_CLASS_NOT_SUPPORTED:
1517 		ZFCP_LOG_INFO("error: adapter %s does not support fc "
1518 			      "class %d.\n",
1519 			      zfcp_get_busid_by_port(port),
1520 			      ZFCP_FC_SERVICE_CLASS_DEFAULT);
1521 		/* stop operation for this adapter */
1522 		debug_text_exception(adapter->erp_dbf, 0, "fsf_s_class_nsup");
1523 		zfcp_erp_adapter_shutdown(adapter, 0);
1524 		fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1525 		break;
1526 
1527         case FSF_ADAPTER_STATUS_AVAILABLE:
1528                 switch (header->fsf_status_qual.word[0]){
1529                 case FSF_SQ_INVOKE_LINK_TEST_PROCEDURE:
1530 			/* reopening link to port */
1531 			debug_text_event(adapter->erp_dbf, 1, "fsf_sq_ltest");
1532 			zfcp_test_link(port);
1533 			fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1534 			break;
1535                 case FSF_SQ_ULP_DEPENDENT_ERP_REQUIRED:
1536 			/* ERP strategy will escalate */
1537 			debug_text_event(adapter->erp_dbf, 1, "fsf_sq_ulp");
1538 			fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1539 			break;
1540                 default:
1541 			ZFCP_LOG_INFO("bug: Wrong status qualifier 0x%x "
1542 				      "arrived.\n",
1543 				      header->fsf_status_qual.word[0]);
1544 			break;
1545                 }
1546                 break;
1547 
1548 	case FSF_ACCESS_DENIED:
1549 		ZFCP_LOG_NORMAL("access denied, cannot send generic service "
1550 				"command (adapter %s, port d_id=0x%08x)\n",
1551 				zfcp_get_busid_by_port(port), port->d_id);
1552 		for (counter = 0; counter < 2; counter++) {
1553 			subtable = header->fsf_status_qual.halfword[counter * 2];
1554 			rule = header->fsf_status_qual.halfword[counter * 2 + 1];
1555 			switch (subtable) {
1556 			case FSF_SQ_CFDC_SUBTABLE_OS:
1557 			case FSF_SQ_CFDC_SUBTABLE_PORT_WWPN:
1558 			case FSF_SQ_CFDC_SUBTABLE_PORT_DID:
1559 			case FSF_SQ_CFDC_SUBTABLE_LUN:
1560        				ZFCP_LOG_INFO("Access denied (%s rule %d)\n",
1561 					zfcp_act_subtable_type[subtable], rule);
1562 				break;
1563 			}
1564 		}
1565 		debug_text_event(adapter->erp_dbf, 1, "fsf_s_access");
1566 		zfcp_erp_port_access_denied(port);
1567 		fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1568 		break;
1569 
1570         case FSF_GENERIC_COMMAND_REJECTED:
1571 		ZFCP_LOG_INFO("generic service command rejected "
1572 			      "(adapter %s, port d_id=0x%08x)\n",
1573 			      zfcp_get_busid_by_port(port), port->d_id);
1574 		ZFCP_LOG_INFO("status qualifier:\n");
1575 		ZFCP_HEX_DUMP(ZFCP_LOG_LEVEL_INFO,
1576 			      (char *) &header->fsf_status_qual,
1577 			      sizeof (union fsf_status_qual));
1578 		debug_text_event(adapter->erp_dbf, 1, "fsf_s_gcom_rej");
1579 		fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1580 		break;
1581 
1582         case FSF_PORT_HANDLE_NOT_VALID:
1583 		ZFCP_LOG_DEBUG("Temporary port identifier 0x%x for port "
1584 			       "0x%016Lx on adapter %s invalid. This may "
1585 			       "happen occasionally.\n", port->handle,
1586 			       port->wwpn, zfcp_get_busid_by_port(port));
1587 		ZFCP_LOG_INFO("status qualifier:\n");
1588 		ZFCP_HEX_DUMP(ZFCP_LOG_LEVEL_INFO,
1589 			      (char *) &header->fsf_status_qual,
1590 			      sizeof (union fsf_status_qual));
1591 		debug_text_event(adapter->erp_dbf, 1, "fsf_s_phandle_nv");
1592 		zfcp_erp_adapter_reopen(adapter, 0);
1593 		fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1594 		break;
1595 
1596         case FSF_PORT_BOXED:
1597 		ZFCP_LOG_INFO("port needs to be reopened "
1598 			      "(adapter %s, port d_id=0x%08x)\n",
1599 			      zfcp_get_busid_by_port(port), port->d_id);
1600 		debug_text_event(adapter->erp_dbf, 2, "fsf_s_pboxed");
1601 		zfcp_erp_port_boxed(port);
1602 		fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR
1603 		    | ZFCP_STATUS_FSFREQ_RETRY;
1604 		break;
1605 
1606 	/* following states should never occure, all cases avoided
1607 	   in zfcp_fsf_send_ct - but who knows ... */
1608 	case FSF_PAYLOAD_SIZE_MISMATCH:
1609 		ZFCP_LOG_INFO("payload size mismatch (adapter: %s, "
1610 			      "req_buf_length=%d, resp_buf_length=%d)\n",
1611 			      zfcp_get_busid_by_adapter(adapter),
1612 			      bottom->req_buf_length, bottom->resp_buf_length);
1613 		fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1614 		break;
1615 	case FSF_REQUEST_SIZE_TOO_LARGE:
1616 		ZFCP_LOG_INFO("request size too large (adapter: %s, "
1617 			      "req_buf_length=%d)\n",
1618 			      zfcp_get_busid_by_adapter(adapter),
1619 			      bottom->req_buf_length);
1620 		fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1621 		break;
1622 	case FSF_RESPONSE_SIZE_TOO_LARGE:
1623 		ZFCP_LOG_INFO("response size too large (adapter: %s, "
1624 			      "resp_buf_length=%d)\n",
1625 			      zfcp_get_busid_by_adapter(adapter),
1626 			      bottom->resp_buf_length);
1627 		fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1628 		break;
1629 	case FSF_SBAL_MISMATCH:
1630 		ZFCP_LOG_INFO("SBAL mismatch (adapter: %s, req_buf_length=%d, "
1631 			      "resp_buf_length=%d)\n",
1632 			      zfcp_get_busid_by_adapter(adapter),
1633 			      bottom->req_buf_length, bottom->resp_buf_length);
1634 		fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1635 		break;
1636 
1637        default:
1638 		ZFCP_LOG_NORMAL("bug: An unknown FSF Status was presented "
1639 				"(debug info 0x%x)\n", header->fsf_status);
1640 		debug_text_event(adapter->erp_dbf, 0, "fsf_sq_inval:");
1641 		debug_exception(adapter->erp_dbf, 0,
1642 				&header->fsf_status_qual.word[0], sizeof (u32));
1643 		break;
1644 	}
1645 
1646 skip_fsfstatus:
1647 	send_ct->status = retval;
1648 
1649 	if (send_ct->handler != NULL)
1650 		send_ct->handler(send_ct->handler_data);
1651 
1652 	return retval;
1653 }
1654 
1655 /**
1656  * zfcp_fsf_send_els - initiate an ELS command (FC-FS)
1657  * @els: pointer to struct zfcp_send_els which contains all needed data for
1658  *	the command.
1659  */
1660 int
1661 zfcp_fsf_send_els(struct zfcp_send_els *els)
1662 {
1663 	volatile struct qdio_buffer_element *sbale;
1664 	struct zfcp_fsf_req *fsf_req;
1665 	u32 d_id;
1666 	struct zfcp_adapter *adapter;
1667 	unsigned long lock_flags;
1668         int bytes;
1669 	int ret = 0;
1670 
1671 	d_id = els->d_id;
1672 	adapter = els->adapter;
1673 
1674         ret = zfcp_fsf_req_create(adapter, FSF_QTCB_SEND_ELS,
1675 				  ZFCP_REQ_AUTO_CLEANUP,
1676 				  NULL, &lock_flags, &fsf_req);
1677 	if (ret < 0) {
1678                 ZFCP_LOG_INFO("error: creation of ELS request failed "
1679 			      "(adapter %s, port d_id: 0x%08x)\n",
1680                               zfcp_get_busid_by_adapter(adapter), d_id);
1681                 goto failed_req;
1682 	}
1683 
1684 	sbale = zfcp_qdio_sbale_req(fsf_req, fsf_req->sbal_curr, 0);
1685         if (zfcp_use_one_sbal(els->req, els->req_count,
1686                               els->resp, els->resp_count)){
1687                 /* both request buffer and response buffer
1688                    fit into one sbale each */
1689                 sbale[0].flags |= SBAL_FLAGS0_TYPE_WRITE_READ;
1690                 sbale[2].addr = zfcp_sg_to_address(&els->req[0]);
1691                 sbale[2].length = els->req[0].length;
1692                 sbale[3].addr = zfcp_sg_to_address(&els->resp[0]);
1693                 sbale[3].length = els->resp[0].length;
1694                 sbale[3].flags |= SBAL_FLAGS_LAST_ENTRY;
1695 	} else if (adapter->adapter_features &
1696                    FSF_FEATURE_ELS_CT_CHAINED_SBALS) {
1697                 /* try to use chained SBALs */
1698                 bytes = zfcp_qdio_sbals_from_sg(fsf_req,
1699                                                 SBAL_FLAGS0_TYPE_WRITE_READ,
1700                                                 els->req, els->req_count,
1701                                                 ZFCP_MAX_SBALS_PER_ELS_REQ);
1702                 if (bytes <= 0) {
1703                         ZFCP_LOG_INFO("error: creation of ELS request failed "
1704 				      "(adapter %s, port d_id: 0x%08x)\n",
1705 				      zfcp_get_busid_by_adapter(adapter), d_id);
1706                         if (bytes == 0) {
1707                                 ret = -ENOMEM;
1708                         } else {
1709                                 ret = bytes;
1710                         }
1711                         goto failed_send;
1712                 }
1713                 fsf_req->qtcb->bottom.support.req_buf_length = bytes;
1714                 fsf_req->sbale_curr = ZFCP_LAST_SBALE_PER_SBAL;
1715                 bytes = zfcp_qdio_sbals_from_sg(fsf_req,
1716                                                 SBAL_FLAGS0_TYPE_WRITE_READ,
1717                                                 els->resp, els->resp_count,
1718                                                 ZFCP_MAX_SBALS_PER_ELS_REQ);
1719                 if (bytes <= 0) {
1720                         ZFCP_LOG_INFO("error: creation of ELS request failed "
1721 				      "(adapter %s, port d_id: 0x%08x)\n",
1722 				      zfcp_get_busid_by_adapter(adapter), d_id);
1723                         if (bytes == 0) {
1724                                 ret = -ENOMEM;
1725                         } else {
1726                                 ret = bytes;
1727                         }
1728                         goto failed_send;
1729                 }
1730                 fsf_req->qtcb->bottom.support.resp_buf_length = bytes;
1731         } else {
1732                 /* reject request */
1733 		ZFCP_LOG_INFO("error: microcode does not support chained SBALs"
1734                               ", ELS request too big (adapter %s, "
1735 			      "port d_id: 0x%08x)\n",
1736 			      zfcp_get_busid_by_adapter(adapter), d_id);
1737                 ret = -EOPNOTSUPP;
1738                 goto failed_send;
1739         }
1740 
1741 	/* settings in QTCB */
1742 	fsf_req->qtcb->bottom.support.d_id = d_id;
1743 	fsf_req->qtcb->bottom.support.service_class =
1744 		ZFCP_FC_SERVICE_CLASS_DEFAULT;
1745 	fsf_req->qtcb->bottom.support.timeout = ZFCP_ELS_TIMEOUT;
1746 	fsf_req->data = (unsigned long) els;
1747 
1748 	sbale = zfcp_qdio_sbale_req(fsf_req, fsf_req->sbal_curr, 0);
1749 
1750 	zfcp_san_dbf_event_els_request(fsf_req);
1751 
1752 	/* start QDIO request for this FSF request */
1753 	ret = zfcp_fsf_req_send(fsf_req, els->timer);
1754 	if (ret) {
1755 		ZFCP_LOG_DEBUG("error: initiation of ELS request failed "
1756 			       "(adapter %s, port d_id: 0x%08x)\n",
1757 			       zfcp_get_busid_by_adapter(adapter), d_id);
1758 		goto failed_send;
1759 	}
1760 
1761 	ZFCP_LOG_DEBUG("ELS request initiated (adapter %s, port d_id: "
1762 		       "0x%08x)\n", zfcp_get_busid_by_adapter(adapter), d_id);
1763 	goto out;
1764 
1765  failed_send:
1766 	zfcp_fsf_req_free(fsf_req);
1767 
1768  failed_req:
1769  out:
1770 	write_unlock_irqrestore(&adapter->request_queue.queue_lock,
1771 				lock_flags);
1772 
1773         return ret;
1774 }
1775 
1776 /**
1777  * zfcp_fsf_send_els_handler - handler for ELS commands
1778  * @fsf_req: pointer to struct zfcp_fsf_req
1779  *
1780  * Data specific for the ELS command is passed using
1781  * fsf_req->data. There we find the pointer to struct zfcp_send_els.
1782  * Usually a specific handler for the ELS command is called which is
1783  * found in this structure.
1784  */
1785 static int zfcp_fsf_send_els_handler(struct zfcp_fsf_req *fsf_req)
1786 {
1787 	struct zfcp_adapter *adapter;
1788 	struct zfcp_port *port;
1789 	u32 d_id;
1790 	struct fsf_qtcb_header *header;
1791 	struct fsf_qtcb_bottom_support *bottom;
1792 	struct zfcp_send_els *send_els;
1793 	int retval = -EINVAL;
1794 	u16 subtable, rule, counter;
1795 
1796 	send_els = (struct zfcp_send_els *) fsf_req->data;
1797 	adapter = send_els->adapter;
1798 	port = send_els->port;
1799 	d_id = send_els->d_id;
1800 	header = &fsf_req->qtcb->header;
1801 	bottom = &fsf_req->qtcb->bottom.support;
1802 
1803 	if (fsf_req->status & ZFCP_STATUS_FSFREQ_ERROR)
1804 		goto skip_fsfstatus;
1805 
1806 	switch (header->fsf_status) {
1807 
1808 	case FSF_GOOD:
1809 		zfcp_san_dbf_event_els_response(fsf_req);
1810 		retval = 0;
1811 		break;
1812 
1813 	case FSF_SERVICE_CLASS_NOT_SUPPORTED:
1814 		ZFCP_LOG_INFO("error: adapter %s does not support fc "
1815 			      "class %d.\n",
1816 			      zfcp_get_busid_by_adapter(adapter),
1817 			      ZFCP_FC_SERVICE_CLASS_DEFAULT);
1818 		/* stop operation for this adapter */
1819 		debug_text_exception(adapter->erp_dbf, 0, "fsf_s_class_nsup");
1820 		zfcp_erp_adapter_shutdown(adapter, 0);
1821 		fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1822 		break;
1823 
1824 	case FSF_ADAPTER_STATUS_AVAILABLE:
1825 		switch (header->fsf_status_qual.word[0]){
1826 		case FSF_SQ_INVOKE_LINK_TEST_PROCEDURE:
1827 			debug_text_event(adapter->erp_dbf, 1, "fsf_sq_ltest");
1828 			if (port && (send_els->ls_code != ZFCP_LS_ADISC))
1829 				zfcp_test_link(port);
1830 			fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1831 			break;
1832 		case FSF_SQ_ULP_DEPENDENT_ERP_REQUIRED:
1833 			debug_text_event(adapter->erp_dbf, 1, "fsf_sq_ulp");
1834 			fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1835 			retval =
1836 			  zfcp_handle_els_rjt(header->fsf_status_qual.word[1],
1837 					      (struct zfcp_ls_rjt_par *)
1838 					      &header->fsf_status_qual.word[2]);
1839 			break;
1840 		case FSF_SQ_RETRY_IF_POSSIBLE:
1841 			debug_text_event(adapter->erp_dbf, 1, "fsf_sq_retry");
1842 			fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1843 			break;
1844 		default:
1845 			ZFCP_LOG_INFO("bug: Wrong status qualifier 0x%x\n",
1846 				      header->fsf_status_qual.word[0]);
1847 			ZFCP_HEX_DUMP(ZFCP_LOG_LEVEL_INFO,
1848 				(char*)header->fsf_status_qual.word, 16);
1849 		}
1850 		break;
1851 
1852 	case FSF_ELS_COMMAND_REJECTED:
1853 		ZFCP_LOG_INFO("ELS has been rejected because command filter "
1854 			      "prohibited sending "
1855 			      "(adapter: %s, port d_id: 0x%08x)\n",
1856 			      zfcp_get_busid_by_adapter(adapter), d_id);
1857 
1858 		break;
1859 
1860 	case FSF_PAYLOAD_SIZE_MISMATCH:
1861 		ZFCP_LOG_INFO(
1862 			"ELS request size and ELS response size must be either "
1863 			"both 0, or both greater than 0 "
1864 			"(adapter: %s, req_buf_length=%d resp_buf_length=%d)\n",
1865 			zfcp_get_busid_by_adapter(adapter),
1866 			bottom->req_buf_length,
1867 			bottom->resp_buf_length);
1868 		break;
1869 
1870 	case FSF_REQUEST_SIZE_TOO_LARGE:
1871 		ZFCP_LOG_INFO(
1872 			"Length of the ELS request buffer, "
1873 			"specified in QTCB bottom, "
1874 			"exceeds the size of the buffers "
1875 			"that have been allocated for ELS request data "
1876 			"(adapter: %s, req_buf_length=%d)\n",
1877 			zfcp_get_busid_by_adapter(adapter),
1878 			bottom->req_buf_length);
1879 		break;
1880 
1881 	case FSF_RESPONSE_SIZE_TOO_LARGE:
1882 		ZFCP_LOG_INFO(
1883 			"Length of the ELS response buffer, "
1884 			"specified in QTCB bottom, "
1885 			"exceeds the size of the buffers "
1886 			"that have been allocated for ELS response data "
1887 			"(adapter: %s, resp_buf_length=%d)\n",
1888 			zfcp_get_busid_by_adapter(adapter),
1889 			bottom->resp_buf_length);
1890 		break;
1891 
1892 	case FSF_SBAL_MISMATCH:
1893 		/* should never occure, avoided in zfcp_fsf_send_els */
1894 		ZFCP_LOG_INFO("SBAL mismatch (adapter: %s, req_buf_length=%d, "
1895 			      "resp_buf_length=%d)\n",
1896 			      zfcp_get_busid_by_adapter(adapter),
1897 			      bottom->req_buf_length, bottom->resp_buf_length);
1898 		fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1899 		break;
1900 
1901 	case FSF_ACCESS_DENIED:
1902 		ZFCP_LOG_NORMAL("access denied, cannot send ELS command "
1903 				"(adapter %s, port d_id=0x%08x)\n",
1904 				zfcp_get_busid_by_adapter(adapter), d_id);
1905 		for (counter = 0; counter < 2; counter++) {
1906 			subtable = header->fsf_status_qual.halfword[counter * 2];
1907 			rule = header->fsf_status_qual.halfword[counter * 2 + 1];
1908 			switch (subtable) {
1909 			case FSF_SQ_CFDC_SUBTABLE_OS:
1910 			case FSF_SQ_CFDC_SUBTABLE_PORT_WWPN:
1911 			case FSF_SQ_CFDC_SUBTABLE_PORT_DID:
1912 			case FSF_SQ_CFDC_SUBTABLE_LUN:
1913 				ZFCP_LOG_INFO("Access denied (%s rule %d)\n",
1914 					zfcp_act_subtable_type[subtable], rule);
1915 				break;
1916 			}
1917 		}
1918 		debug_text_event(adapter->erp_dbf, 1, "fsf_s_access");
1919 		if (port != NULL)
1920 			zfcp_erp_port_access_denied(port);
1921 		fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1922 		break;
1923 
1924 	default:
1925 		ZFCP_LOG_NORMAL(
1926 			"bug: An unknown FSF Status was presented "
1927 			"(adapter: %s, fsf_status=0x%08x)\n",
1928 			zfcp_get_busid_by_adapter(adapter),
1929 			header->fsf_status);
1930 		debug_text_event(adapter->erp_dbf, 0, "fsf_sq_inval");
1931 		debug_exception(adapter->erp_dbf, 0,
1932 			&header->fsf_status_qual.word[0], sizeof(u32));
1933 		fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1934 		break;
1935 	}
1936 
1937 skip_fsfstatus:
1938 	send_els->status = retval;
1939 
1940 	if (send_els->handler != 0)
1941 		send_els->handler(send_els->handler_data);
1942 
1943 	return retval;
1944 }
1945 
1946 int
1947 zfcp_fsf_exchange_config_data(struct zfcp_erp_action *erp_action)
1948 {
1949 	volatile struct qdio_buffer_element *sbale;
1950 	unsigned long lock_flags;
1951 	int retval = 0;
1952 
1953 	/* setup new FSF request */
1954 	retval = zfcp_fsf_req_create(erp_action->adapter,
1955 				     FSF_QTCB_EXCHANGE_CONFIG_DATA,
1956 				     ZFCP_REQ_AUTO_CLEANUP,
1957 				     erp_action->adapter->pool.fsf_req_erp,
1958 				     &lock_flags, &(erp_action->fsf_req));
1959 	if (retval < 0) {
1960 		ZFCP_LOG_INFO("error: Could not create exchange configuration "
1961 			      "data request for adapter %s.\n",
1962 			      zfcp_get_busid_by_adapter(erp_action->adapter));
1963 		goto out;
1964 	}
1965 
1966 	sbale = zfcp_qdio_sbale_req(erp_action->fsf_req,
1967                                     erp_action->fsf_req->sbal_curr, 0);
1968         sbale[0].flags |= SBAL_FLAGS0_TYPE_READ;
1969         sbale[1].flags |= SBAL_FLAGS_LAST_ENTRY;
1970 
1971 	erp_action->fsf_req->erp_action = erp_action;
1972 	erp_action->fsf_req->qtcb->bottom.config.feature_selection =
1973 			FSF_FEATURE_CFDC |
1974 			FSF_FEATURE_LUN_SHARING |
1975 			FSF_FEATURE_NOTIFICATION_LOST |
1976 			FSF_FEATURE_UPDATE_ALERT;
1977 
1978 	/* start QDIO request for this FSF request */
1979 	retval = zfcp_fsf_req_send(erp_action->fsf_req, &erp_action->timer);
1980 	if (retval) {
1981 		ZFCP_LOG_INFO
1982 		    ("error: Could not send exchange configuration data "
1983 		     "command on the adapter %s\n",
1984 		     zfcp_get_busid_by_adapter(erp_action->adapter));
1985 		zfcp_fsf_req_free(erp_action->fsf_req);
1986 		erp_action->fsf_req = NULL;
1987 		goto out;
1988 	}
1989 
1990 	ZFCP_LOG_DEBUG("exchange configuration data request initiated "
1991 		       "(adapter %s)\n",
1992 		       zfcp_get_busid_by_adapter(erp_action->adapter));
1993 
1994  out:
1995 	write_unlock_irqrestore(&erp_action->adapter->request_queue.queue_lock,
1996 				lock_flags);
1997 	return retval;
1998 }
1999 
2000 /**
2001  * zfcp_fsf_exchange_config_evaluate
2002  * @fsf_req: fsf_req which belongs to xchg config data request
2003  * @xchg_ok: specifies if xchg config data was incomplete or complete (0/1)
2004  *
2005  * returns: -EIO on error, 0 otherwise
2006  */
2007 static int
2008 zfcp_fsf_exchange_config_evaluate(struct zfcp_fsf_req *fsf_req, int xchg_ok)
2009 {
2010 	struct fsf_qtcb_bottom_config *bottom;
2011 	struct zfcp_adapter *adapter = fsf_req->adapter;
2012 	struct Scsi_Host *shost = adapter->scsi_host;
2013 
2014 	bottom = &fsf_req->qtcb->bottom.config;
2015 	ZFCP_LOG_DEBUG("low/high QTCB version 0x%x/0x%x of FSF\n",
2016 		       bottom->low_qtcb_version, bottom->high_qtcb_version);
2017 	adapter->fsf_lic_version = bottom->lic_version;
2018 	adapter->adapter_features = bottom->adapter_features;
2019 	adapter->connection_features = bottom->connection_features;
2020 	adapter->peer_wwpn = 0;
2021 	adapter->peer_wwnn = 0;
2022 	adapter->peer_d_id = 0;
2023 
2024 	if (xchg_ok) {
2025 		fc_host_node_name(shost) = bottom->nport_serv_param.wwnn;
2026 		fc_host_port_name(shost) = bottom->nport_serv_param.wwpn;
2027 		fc_host_port_id(shost) = bottom->s_id & ZFCP_DID_MASK;
2028 		fc_host_speed(shost) = bottom->fc_link_speed;
2029 		fc_host_supported_classes(shost) = FC_COS_CLASS2 | FC_COS_CLASS3;
2030 		adapter->hydra_version = bottom->adapter_type;
2031 		if (fc_host_permanent_port_name(shost) == -1)
2032 			fc_host_permanent_port_name(shost) =
2033 				fc_host_port_name(shost);
2034 		if (bottom->fc_topology == FSF_TOPO_P2P) {
2035 			adapter->peer_d_id = bottom->peer_d_id & ZFCP_DID_MASK;
2036 			adapter->peer_wwpn = bottom->plogi_payload.wwpn;
2037 			adapter->peer_wwnn = bottom->plogi_payload.wwnn;
2038 			fc_host_port_type(shost) = FC_PORTTYPE_PTP;
2039 		} else if (bottom->fc_topology == FSF_TOPO_FABRIC)
2040 			fc_host_port_type(shost) = FC_PORTTYPE_NPORT;
2041 		else if (bottom->fc_topology == FSF_TOPO_AL)
2042 			fc_host_port_type(shost) = FC_PORTTYPE_NLPORT;
2043 		else
2044 			fc_host_port_type(shost) = FC_PORTTYPE_UNKNOWN;
2045 	} else {
2046 		fc_host_node_name(shost) = 0;
2047 		fc_host_port_name(shost) = 0;
2048 		fc_host_port_id(shost) = 0;
2049 		fc_host_speed(shost) = FC_PORTSPEED_UNKNOWN;
2050 		fc_host_port_type(shost) = FC_PORTTYPE_UNKNOWN;
2051 		adapter->hydra_version = 0;
2052 	}
2053 
2054 	if (adapter->adapter_features & FSF_FEATURE_HBAAPI_MANAGEMENT) {
2055 		adapter->hardware_version = bottom->hardware_version;
2056 		memcpy(fc_host_serial_number(shost), bottom->serial_number,
2057 		       min(FC_SERIAL_NUMBER_SIZE, 17));
2058 		EBCASC(fc_host_serial_number(shost),
2059 		       min(FC_SERIAL_NUMBER_SIZE, 17));
2060 	}
2061 
2062 	ZFCP_LOG_NORMAL("The adapter %s reported the following characteristics:\n"
2063 			"WWNN 0x%016Lx, "
2064 			"WWPN 0x%016Lx, "
2065 			"S_ID 0x%08x,\n"
2066 			"adapter version 0x%x, "
2067 			"LIC version 0x%x, "
2068 			"FC link speed %d Gb/s\n",
2069 			zfcp_get_busid_by_adapter(adapter),
2070 			(wwn_t) fc_host_node_name(shost),
2071 			(wwn_t) fc_host_port_name(shost),
2072 			fc_host_port_id(shost),
2073 			adapter->hydra_version,
2074 			adapter->fsf_lic_version,
2075 			fc_host_speed(shost));
2076 	if (ZFCP_QTCB_VERSION < bottom->low_qtcb_version) {
2077 		ZFCP_LOG_NORMAL("error: the adapter %s "
2078 				"only supports newer control block "
2079 				"versions in comparison to this device "
2080 				"driver (try updated device driver)\n",
2081 				zfcp_get_busid_by_adapter(adapter));
2082 		debug_text_event(adapter->erp_dbf, 0, "low_qtcb_ver");
2083 		zfcp_erp_adapter_shutdown(adapter, 0);
2084 		return -EIO;
2085 	}
2086 	if (ZFCP_QTCB_VERSION > bottom->high_qtcb_version) {
2087 		ZFCP_LOG_NORMAL("error: the adapter %s "
2088 				"only supports older control block "
2089 				"versions than this device driver uses"
2090 				"(consider a microcode upgrade)\n",
2091 				zfcp_get_busid_by_adapter(adapter));
2092 		debug_text_event(adapter->erp_dbf, 0, "high_qtcb_ver");
2093 		zfcp_erp_adapter_shutdown(adapter, 0);
2094 		return -EIO;
2095 	}
2096 	return 0;
2097 }
2098 
2099 /*
2100  * function:    zfcp_fsf_exchange_config_data_handler
2101  *
2102  * purpose:     is called for finished Exchange Configuration Data command
2103  *
2104  * returns:
2105  */
2106 static int
2107 zfcp_fsf_exchange_config_data_handler(struct zfcp_fsf_req *fsf_req)
2108 {
2109 	struct fsf_qtcb_bottom_config *bottom;
2110 	struct zfcp_adapter *adapter = fsf_req->adapter;
2111 	struct fsf_qtcb *qtcb = fsf_req->qtcb;
2112 
2113 	if (fsf_req->status & ZFCP_STATUS_FSFREQ_ERROR)
2114 		return -EIO;
2115 
2116 	switch (qtcb->header.fsf_status) {
2117 
2118 	case FSF_GOOD:
2119 		if (zfcp_fsf_exchange_config_evaluate(fsf_req, 1))
2120 			return -EIO;
2121 
2122 		switch (fc_host_port_type(adapter->scsi_host)) {
2123 		case FC_PORTTYPE_PTP:
2124 			ZFCP_LOG_NORMAL("Point-to-Point fibrechannel "
2125 					"configuration detected at adapter %s\n"
2126 					"Peer WWNN 0x%016llx, "
2127 					"peer WWPN 0x%016llx, "
2128 					"peer d_id 0x%06x\n",
2129 					zfcp_get_busid_by_adapter(adapter),
2130 					adapter->peer_wwnn,
2131 					adapter->peer_wwpn,
2132 					adapter->peer_d_id);
2133 			debug_text_event(fsf_req->adapter->erp_dbf, 0,
2134 					 "top-p-to-p");
2135 			break;
2136 		case FC_PORTTYPE_NLPORT:
2137 			ZFCP_LOG_NORMAL("error: Arbitrated loop fibrechannel "
2138 					"topology detected at adapter %s "
2139 					"unsupported, shutting down adapter\n",
2140 					zfcp_get_busid_by_adapter(adapter));
2141 			debug_text_event(fsf_req->adapter->erp_dbf, 0,
2142 					 "top-al");
2143 			zfcp_erp_adapter_shutdown(adapter, 0);
2144 			return -EIO;
2145 		case FC_PORTTYPE_NPORT:
2146 			ZFCP_LOG_NORMAL("Switched fabric fibrechannel "
2147 				      "network detected at adapter %s.\n",
2148 				      zfcp_get_busid_by_adapter(adapter));
2149 			break;
2150 		default:
2151 			ZFCP_LOG_NORMAL("bug: The fibrechannel topology "
2152 					"reported by the exchange "
2153 					"configuration command for "
2154 					"the adapter %s is not "
2155 					"of a type known to the zfcp "
2156 					"driver, shutting down adapter\n",
2157 					zfcp_get_busid_by_adapter(adapter));
2158 			debug_text_exception(fsf_req->adapter->erp_dbf, 0,
2159 					     "unknown-topo");
2160 			zfcp_erp_adapter_shutdown(adapter, 0);
2161 			return -EIO;
2162 		}
2163 		bottom = &qtcb->bottom.config;
2164 		if (bottom->max_qtcb_size < sizeof(struct fsf_qtcb)) {
2165 			ZFCP_LOG_NORMAL("bug: Maximum QTCB size (%d bytes) "
2166 					"allowed by the adapter %s "
2167 					"is lower than the minimum "
2168 					"required by the driver (%ld bytes).\n",
2169 					bottom->max_qtcb_size,
2170 					zfcp_get_busid_by_adapter(adapter),
2171 					sizeof(struct fsf_qtcb));
2172 			debug_text_event(fsf_req->adapter->erp_dbf, 0,
2173 					 "qtcb-size");
2174 			debug_event(fsf_req->adapter->erp_dbf, 0,
2175 				    &bottom->max_qtcb_size, sizeof (u32));
2176 			zfcp_erp_adapter_shutdown(adapter, 0);
2177 			return -EIO;
2178 		}
2179 		atomic_set_mask(ZFCP_STATUS_ADAPTER_XCONFIG_OK,
2180 				&adapter->status);
2181 		break;
2182 	case FSF_EXCHANGE_CONFIG_DATA_INCOMPLETE:
2183 		debug_text_event(adapter->erp_dbf, 0, "xchg-inco");
2184 
2185 		if (zfcp_fsf_exchange_config_evaluate(fsf_req, 0))
2186 			return -EIO;
2187 
2188 		atomic_set_mask(ZFCP_STATUS_ADAPTER_XCONFIG_OK, &adapter->status);
2189 
2190 		zfcp_fsf_link_down_info_eval(adapter,
2191 			&qtcb->header.fsf_status_qual.link_down_info);
2192 		break;
2193 	default:
2194 		debug_text_event(fsf_req->adapter->erp_dbf, 0, "fsf-stat-ng");
2195 		debug_event(fsf_req->adapter->erp_dbf, 0,
2196 			    &fsf_req->qtcb->header.fsf_status, sizeof (u32));
2197 		zfcp_erp_adapter_shutdown(adapter, 0);
2198 		return -EIO;
2199 	}
2200 	return 0;
2201 }
2202 
2203 /**
2204  * zfcp_fsf_exchange_port_data - request information about local port
2205  * @erp_action: ERP action for the adapter for which port data is requested
2206  * @adapter: for which port data is requested
2207  * @data: response to exchange port data request
2208  */
2209 int
2210 zfcp_fsf_exchange_port_data(struct zfcp_erp_action *erp_action,
2211 			    struct zfcp_adapter *adapter,
2212 			    struct fsf_qtcb_bottom_port *data)
2213 {
2214 	volatile struct qdio_buffer_element *sbale;
2215 	int retval = 0;
2216 	unsigned long lock_flags;
2217         struct zfcp_fsf_req *fsf_req;
2218 	struct timer_list *timer;
2219 
2220 	if (!(adapter->adapter_features & FSF_FEATURE_HBAAPI_MANAGEMENT)) {
2221 		ZFCP_LOG_INFO("error: exchange port data "
2222                               "command not supported by adapter %s\n",
2223 			      zfcp_get_busid_by_adapter(adapter));
2224                 return -EOPNOTSUPP;
2225         }
2226 
2227 	/* setup new FSF request */
2228 	retval = zfcp_fsf_req_create(adapter, FSF_QTCB_EXCHANGE_PORT_DATA,
2229 				     erp_action ? ZFCP_REQ_AUTO_CLEANUP : 0,
2230 				     0, &lock_flags, &fsf_req);
2231 	if (retval < 0) {
2232 		ZFCP_LOG_INFO("error: Out of resources. Could not create an "
2233                               "exchange port data request for"
2234                               "the adapter %s.\n",
2235 			      zfcp_get_busid_by_adapter(adapter));
2236 		write_unlock_irqrestore(&adapter->request_queue.queue_lock,
2237 					lock_flags);
2238 		return retval;
2239 	}
2240 
2241 	if (data)
2242 		fsf_req->data = (unsigned long) data;
2243 
2244 	sbale = zfcp_qdio_sbale_req(fsf_req, fsf_req->sbal_curr, 0);
2245         sbale[0].flags |= SBAL_FLAGS0_TYPE_READ;
2246         sbale[1].flags |= SBAL_FLAGS_LAST_ENTRY;
2247 
2248 	if (erp_action) {
2249 		erp_action->fsf_req = fsf_req;
2250 		fsf_req->erp_action = erp_action;
2251 		timer = &erp_action->timer;
2252 	} else {
2253 		timer = kmalloc(sizeof(struct timer_list), GFP_ATOMIC);
2254 		if (!timer) {
2255 			write_unlock_irqrestore(&adapter->request_queue.queue_lock,
2256 						lock_flags);
2257 			zfcp_fsf_req_free(fsf_req);
2258 			return -ENOMEM;
2259 		}
2260 		init_timer(timer);
2261 		timer->function = zfcp_fsf_request_timeout_handler;
2262 		timer->data = (unsigned long) adapter;
2263 		timer->expires = ZFCP_FSF_REQUEST_TIMEOUT;
2264 	}
2265 
2266 	retval = zfcp_fsf_req_send(fsf_req, timer);
2267 	if (retval) {
2268 		ZFCP_LOG_INFO("error: Could not send an exchange port data "
2269                               "command on the adapter %s\n",
2270 			      zfcp_get_busid_by_adapter(adapter));
2271 		zfcp_fsf_req_free(fsf_req);
2272 		if (erp_action)
2273 			erp_action->fsf_req = NULL;
2274 		else
2275 			kfree(timer);
2276 		write_unlock_irqrestore(&adapter->request_queue.queue_lock,
2277 					lock_flags);
2278 		return retval;
2279 	}
2280 
2281 	write_unlock_irqrestore(&adapter->request_queue.queue_lock, lock_flags);
2282 
2283 	if (!erp_action) {
2284 		wait_event(fsf_req->completion_wq,
2285 			   fsf_req->status & ZFCP_STATUS_FSFREQ_COMPLETED);
2286 		del_timer_sync(timer);
2287 		zfcp_fsf_req_free(fsf_req);
2288 		kfree(timer);
2289 	}
2290 	return retval;
2291 }
2292 
2293 /**
2294  * zfcp_fsf_exchange_port_evaluate
2295  * @fsf_req: fsf_req which belongs to xchg port data request
2296  * @xchg_ok: specifies if xchg port data was incomplete or complete (0/1)
2297  */
2298 static void
2299 zfcp_fsf_exchange_port_evaluate(struct zfcp_fsf_req *fsf_req, int xchg_ok)
2300 {
2301 	struct zfcp_adapter *adapter;
2302 	struct fsf_qtcb *qtcb;
2303 	struct fsf_qtcb_bottom_port *bottom, *data;
2304 	struct Scsi_Host *shost;
2305 
2306 	adapter = fsf_req->adapter;
2307 	qtcb = fsf_req->qtcb;
2308 	bottom = &qtcb->bottom.port;
2309 	shost = adapter->scsi_host;
2310 
2311 	data = (struct fsf_qtcb_bottom_port*) fsf_req->data;
2312 	if (data)
2313 		memcpy(data, bottom, sizeof(struct fsf_qtcb_bottom_port));
2314 
2315 	if (adapter->connection_features & FSF_FEATURE_NPIV_MODE)
2316 		fc_host_permanent_port_name(shost) = bottom->wwpn;
2317 	else
2318 		fc_host_permanent_port_name(shost) = fc_host_port_name(shost);
2319 	fc_host_maxframe_size(shost) = bottom->maximum_frame_size;
2320 	fc_host_supported_speeds(shost) = bottom->supported_speed;
2321 }
2322 
2323 /**
2324  * zfcp_fsf_exchange_port_data_handler - handler for exchange_port_data request
2325  * @fsf_req: pointer to struct zfcp_fsf_req
2326  */
2327 static void
2328 zfcp_fsf_exchange_port_data_handler(struct zfcp_fsf_req *fsf_req)
2329 {
2330 	struct zfcp_adapter *adapter;
2331 	struct fsf_qtcb *qtcb;
2332 
2333 	adapter = fsf_req->adapter;
2334 	qtcb = fsf_req->qtcb;
2335 
2336 	if (fsf_req->status & ZFCP_STATUS_FSFREQ_ERROR)
2337 		return;
2338 
2339 	switch (qtcb->header.fsf_status) {
2340         case FSF_GOOD:
2341 		zfcp_fsf_exchange_port_evaluate(fsf_req, 1);
2342 		atomic_set_mask(ZFCP_STATUS_ADAPTER_XPORT_OK, &adapter->status);
2343 		break;
2344 	case FSF_EXCHANGE_CONFIG_DATA_INCOMPLETE:
2345 		zfcp_fsf_exchange_port_evaluate(fsf_req, 0);
2346 		atomic_set_mask(ZFCP_STATUS_ADAPTER_XPORT_OK, &adapter->status);
2347 		zfcp_fsf_link_down_info_eval(adapter,
2348 			&qtcb->header.fsf_status_qual.link_down_info);
2349                 break;
2350         default:
2351 		debug_text_event(adapter->erp_dbf, 0, "xchg-port-ng");
2352 		debug_event(adapter->erp_dbf, 0,
2353 			    &fsf_req->qtcb->header.fsf_status, sizeof(u32));
2354 	}
2355 }
2356 
2357 
2358 /*
2359  * function:    zfcp_fsf_open_port
2360  *
2361  * purpose:
2362  *
2363  * returns:	address of initiated FSF request
2364  *		NULL - request could not be initiated
2365  */
2366 int
2367 zfcp_fsf_open_port(struct zfcp_erp_action *erp_action)
2368 {
2369 	volatile struct qdio_buffer_element *sbale;
2370 	unsigned long lock_flags;
2371 	int retval = 0;
2372 
2373 	/* setup new FSF request */
2374 	retval = zfcp_fsf_req_create(erp_action->adapter,
2375 				     FSF_QTCB_OPEN_PORT_WITH_DID,
2376 				     ZFCP_WAIT_FOR_SBAL | ZFCP_REQ_AUTO_CLEANUP,
2377 				     erp_action->adapter->pool.fsf_req_erp,
2378 				     &lock_flags, &(erp_action->fsf_req));
2379 	if (retval < 0) {
2380 		ZFCP_LOG_INFO("error: Could not create open port request "
2381 			      "for port 0x%016Lx on adapter %s.\n",
2382 			      erp_action->port->wwpn,
2383 			      zfcp_get_busid_by_adapter(erp_action->adapter));
2384 		goto out;
2385 	}
2386 
2387 	sbale = zfcp_qdio_sbale_req(erp_action->fsf_req,
2388                                     erp_action->fsf_req->sbal_curr, 0);
2389         sbale[0].flags |= SBAL_FLAGS0_TYPE_READ;
2390         sbale[1].flags |= SBAL_FLAGS_LAST_ENTRY;
2391 
2392 	erp_action->fsf_req->qtcb->bottom.support.d_id = erp_action->port->d_id;
2393 	atomic_set_mask(ZFCP_STATUS_COMMON_OPENING, &erp_action->port->status);
2394 	erp_action->fsf_req->data = (unsigned long) erp_action->port;
2395 	erp_action->fsf_req->erp_action = erp_action;
2396 
2397 	/* start QDIO request for this FSF request */
2398 	retval = zfcp_fsf_req_send(erp_action->fsf_req, &erp_action->timer);
2399 	if (retval) {
2400 		ZFCP_LOG_INFO("error: Could not send open port request for "
2401 			      "port 0x%016Lx on adapter %s.\n",
2402 			      erp_action->port->wwpn,
2403 			      zfcp_get_busid_by_adapter(erp_action->adapter));
2404 		zfcp_fsf_req_free(erp_action->fsf_req);
2405 		erp_action->fsf_req = NULL;
2406 		goto out;
2407 	}
2408 
2409 	ZFCP_LOG_DEBUG("open port request initiated "
2410 		       "(adapter %s,  port 0x%016Lx)\n",
2411 		       zfcp_get_busid_by_adapter(erp_action->adapter),
2412 		       erp_action->port->wwpn);
2413  out:
2414 	write_unlock_irqrestore(&erp_action->adapter->request_queue.queue_lock,
2415 				lock_flags);
2416 	return retval;
2417 }
2418 
2419 /*
2420  * function:    zfcp_fsf_open_port_handler
2421  *
2422  * purpose:	is called for finished Open Port command
2423  *
2424  * returns:
2425  */
2426 static int
2427 zfcp_fsf_open_port_handler(struct zfcp_fsf_req *fsf_req)
2428 {
2429 	int retval = -EINVAL;
2430 	struct zfcp_port *port;
2431 	struct fsf_plogi *plogi;
2432 	struct fsf_qtcb_header *header;
2433 	u16 subtable, rule, counter;
2434 
2435 	port = (struct zfcp_port *) fsf_req->data;
2436 	header = &fsf_req->qtcb->header;
2437 
2438 	if (fsf_req->status & ZFCP_STATUS_FSFREQ_ERROR) {
2439 		/* don't change port status in our bookkeeping */
2440 		goto skip_fsfstatus;
2441 	}
2442 
2443 	/* evaluate FSF status in QTCB */
2444 	switch (header->fsf_status) {
2445 
2446 	case FSF_PORT_ALREADY_OPEN:
2447 		ZFCP_LOG_NORMAL("bug: remote port 0x%016Lx on adapter %s "
2448 				"is already open.\n",
2449 				port->wwpn, zfcp_get_busid_by_port(port));
2450 		debug_text_exception(fsf_req->adapter->erp_dbf, 0,
2451 				     "fsf_s_popen");
2452 		/*
2453 		 * This is a bug, however operation should continue normally
2454 		 * if it is simply ignored
2455 		 */
2456 		break;
2457 
2458 	case FSF_ACCESS_DENIED:
2459 		ZFCP_LOG_NORMAL("Access denied, cannot open port 0x%016Lx "
2460 				"on adapter %s\n",
2461 				port->wwpn, zfcp_get_busid_by_port(port));
2462 		for (counter = 0; counter < 2; counter++) {
2463 			subtable = header->fsf_status_qual.halfword[counter * 2];
2464 			rule = header->fsf_status_qual.halfword[counter * 2 + 1];
2465 			switch (subtable) {
2466 			case FSF_SQ_CFDC_SUBTABLE_OS:
2467 			case FSF_SQ_CFDC_SUBTABLE_PORT_WWPN:
2468 			case FSF_SQ_CFDC_SUBTABLE_PORT_DID:
2469 			case FSF_SQ_CFDC_SUBTABLE_LUN:
2470 				ZFCP_LOG_INFO("Access denied (%s rule %d)\n",
2471 					zfcp_act_subtable_type[subtable], rule);
2472 				break;
2473 			}
2474 		}
2475 		debug_text_event(fsf_req->adapter->erp_dbf, 1, "fsf_s_access");
2476 		zfcp_erp_port_access_denied(port);
2477 		fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
2478 		break;
2479 
2480 	case FSF_MAXIMUM_NUMBER_OF_PORTS_EXCEEDED:
2481 		ZFCP_LOG_INFO("error: The FSF adapter is out of resources. "
2482 			      "The remote port 0x%016Lx on adapter %s "
2483 			      "could not be opened. Disabling it.\n",
2484 			      port->wwpn, zfcp_get_busid_by_port(port));
2485 		debug_text_event(fsf_req->adapter->erp_dbf, 1,
2486 				 "fsf_s_max_ports");
2487 		zfcp_erp_port_failed(port);
2488 		fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
2489 		break;
2490 
2491 	case FSF_ADAPTER_STATUS_AVAILABLE:
2492 		switch (header->fsf_status_qual.word[0]) {
2493 		case FSF_SQ_INVOKE_LINK_TEST_PROCEDURE:
2494 			debug_text_event(fsf_req->adapter->erp_dbf, 1,
2495 					 "fsf_sq_ltest");
2496 			/* ERP strategy will escalate */
2497 			fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
2498 			break;
2499 		case FSF_SQ_ULP_DEPENDENT_ERP_REQUIRED:
2500 			/* ERP strategy will escalate */
2501 			debug_text_event(fsf_req->adapter->erp_dbf, 1,
2502 					 "fsf_sq_ulp");
2503 			fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
2504 			break;
2505 		case FSF_SQ_NO_RETRY_POSSIBLE:
2506 			ZFCP_LOG_NORMAL("The remote port 0x%016Lx on "
2507 					"adapter %s could not be opened. "
2508 					"Disabling it.\n",
2509 					port->wwpn,
2510 					zfcp_get_busid_by_port(port));
2511 			debug_text_exception(fsf_req->adapter->erp_dbf, 0,
2512 					     "fsf_sq_no_retry");
2513 			zfcp_erp_port_failed(port);
2514 			fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
2515 			break;
2516 		default:
2517 			ZFCP_LOG_NORMAL
2518 			    ("bug: Wrong status qualifier 0x%x arrived.\n",
2519 			     header->fsf_status_qual.word[0]);
2520 			debug_text_event(fsf_req->adapter->erp_dbf, 0,
2521 					 "fsf_sq_inval:");
2522 			debug_exception(
2523 				fsf_req->adapter->erp_dbf, 0,
2524 				&header->fsf_status_qual.word[0],
2525 				sizeof (u32));
2526 			break;
2527 		}
2528 		break;
2529 
2530 	case FSF_GOOD:
2531 		/* save port handle assigned by FSF */
2532 		port->handle = header->port_handle;
2533 		ZFCP_LOG_INFO("The remote port 0x%016Lx via adapter %s "
2534 			      "was opened, it's port handle is 0x%x\n",
2535 			      port->wwpn, zfcp_get_busid_by_port(port),
2536 			      port->handle);
2537 		/* mark port as open */
2538 		atomic_set_mask(ZFCP_STATUS_COMMON_OPEN |
2539 				ZFCP_STATUS_PORT_PHYS_OPEN, &port->status);
2540 		atomic_clear_mask(ZFCP_STATUS_COMMON_ACCESS_DENIED |
2541 		                  ZFCP_STATUS_COMMON_ACCESS_BOXED,
2542 		                  &port->status);
2543 		retval = 0;
2544 		/* check whether D_ID has changed during open */
2545 		/*
2546 		 * FIXME: This check is not airtight, as the FCP channel does
2547 		 * not monitor closures of target port connections caused on
2548 		 * the remote side. Thus, they might miss out on invalidating
2549 		 * locally cached WWPNs (and other N_Port parameters) of gone
2550 		 * target ports. So, our heroic attempt to make things safe
2551 		 * could be undermined by 'open port' response data tagged with
2552 		 * obsolete WWPNs. Another reason to monitor potential
2553 		 * connection closures ourself at least (by interpreting
2554 		 * incoming ELS' and unsolicited status). It just crosses my
2555 		 * mind that one should be able to cross-check by means of
2556 		 * another GID_PN straight after a port has been opened.
2557 		 * Alternately, an ADISC/PDISC ELS should suffice, as well.
2558 		 */
2559 		plogi = (struct fsf_plogi *) fsf_req->qtcb->bottom.support.els;
2560 		if (!atomic_test_mask(ZFCP_STATUS_PORT_NO_WWPN, &port->status))
2561 		{
2562 			if (fsf_req->qtcb->bottom.support.els1_length <
2563 			    sizeof (struct fsf_plogi)) {
2564 				ZFCP_LOG_INFO(
2565 					"warning: insufficient length of "
2566 					"PLOGI payload (%i)\n",
2567 					fsf_req->qtcb->bottom.support.els1_length);
2568 				debug_text_event(fsf_req->adapter->erp_dbf, 0,
2569 						 "fsf_s_short_plogi:");
2570 				/* skip sanity check and assume wwpn is ok */
2571 			} else {
2572 				if (plogi->serv_param.wwpn != port->wwpn) {
2573 					ZFCP_LOG_INFO("warning: d_id of port "
2574 						      "0x%016Lx changed during "
2575 						      "open\n", port->wwpn);
2576 					debug_text_event(
2577 						fsf_req->adapter->erp_dbf, 0,
2578 						"fsf_s_did_change:");
2579 					atomic_clear_mask(
2580 						ZFCP_STATUS_PORT_DID_DID,
2581 						&port->status);
2582 				} else {
2583 					port->wwnn = plogi->serv_param.wwnn;
2584 					zfcp_plogi_evaluate(port, plogi);
2585 				}
2586 			}
2587 		}
2588 		break;
2589 
2590 	case FSF_UNKNOWN_OP_SUBTYPE:
2591 		/* should never occure, subtype not set in zfcp_fsf_open_port */
2592 		ZFCP_LOG_INFO("unknown operation subtype (adapter: %s, "
2593 			      "op_subtype=0x%x)\n",
2594 			      zfcp_get_busid_by_port(port),
2595 			      fsf_req->qtcb->bottom.support.operation_subtype);
2596 		fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
2597 		break;
2598 
2599 	default:
2600 		ZFCP_LOG_NORMAL("bug: An unknown FSF Status was presented "
2601 				"(debug info 0x%x)\n",
2602 				header->fsf_status);
2603 		debug_text_event(fsf_req->adapter->erp_dbf, 0, "fsf_s_inval:");
2604 		debug_exception(fsf_req->adapter->erp_dbf, 0,
2605 				&header->fsf_status, sizeof (u32));
2606 		break;
2607 	}
2608 
2609  skip_fsfstatus:
2610 	atomic_clear_mask(ZFCP_STATUS_COMMON_OPENING, &port->status);
2611 	return retval;
2612 }
2613 
2614 /*
2615  * function:    zfcp_fsf_close_port
2616  *
2617  * purpose:     submit FSF command "close port"
2618  *
2619  * returns:     address of initiated FSF request
2620  *              NULL - request could not be initiated
2621  */
2622 int
2623 zfcp_fsf_close_port(struct zfcp_erp_action *erp_action)
2624 {
2625 	volatile struct qdio_buffer_element *sbale;
2626 	unsigned long lock_flags;
2627 	int retval = 0;
2628 
2629 	/* setup new FSF request */
2630 	retval = zfcp_fsf_req_create(erp_action->adapter,
2631 				     FSF_QTCB_CLOSE_PORT,
2632 				     ZFCP_WAIT_FOR_SBAL | ZFCP_REQ_AUTO_CLEANUP,
2633 				     erp_action->adapter->pool.fsf_req_erp,
2634 				     &lock_flags, &(erp_action->fsf_req));
2635 	if (retval < 0) {
2636 		ZFCP_LOG_INFO("error: Could not create a close port request "
2637 			      "for port 0x%016Lx on adapter %s.\n",
2638 			      erp_action->port->wwpn,
2639 			      zfcp_get_busid_by_adapter(erp_action->adapter));
2640 		goto out;
2641 	}
2642 
2643 	sbale = zfcp_qdio_sbale_req(erp_action->fsf_req,
2644                                     erp_action->fsf_req->sbal_curr, 0);
2645         sbale[0].flags |= SBAL_FLAGS0_TYPE_READ;
2646         sbale[1].flags |= SBAL_FLAGS_LAST_ENTRY;
2647 
2648 	atomic_set_mask(ZFCP_STATUS_COMMON_CLOSING, &erp_action->port->status);
2649 	erp_action->fsf_req->data = (unsigned long) erp_action->port;
2650 	erp_action->fsf_req->erp_action = erp_action;
2651 	erp_action->fsf_req->qtcb->header.port_handle =
2652 	    erp_action->port->handle;
2653 
2654 	/* start QDIO request for this FSF request */
2655 	retval = zfcp_fsf_req_send(erp_action->fsf_req, &erp_action->timer);
2656 	if (retval) {
2657 		ZFCP_LOG_INFO("error: Could not send a close port request for "
2658 			      "port 0x%016Lx on adapter %s.\n",
2659 			      erp_action->port->wwpn,
2660 			      zfcp_get_busid_by_adapter(erp_action->adapter));
2661 		zfcp_fsf_req_free(erp_action->fsf_req);
2662 		erp_action->fsf_req = NULL;
2663 		goto out;
2664 	}
2665 
2666 	ZFCP_LOG_TRACE("close port request initiated "
2667 		       "(adapter %s, port 0x%016Lx)\n",
2668 		       zfcp_get_busid_by_adapter(erp_action->adapter),
2669 		       erp_action->port->wwpn);
2670  out:
2671 	write_unlock_irqrestore(&erp_action->adapter->request_queue.queue_lock,
2672 				lock_flags);
2673 	return retval;
2674 }
2675 
2676 /*
2677  * function:    zfcp_fsf_close_port_handler
2678  *
2679  * purpose:     is called for finished Close Port FSF command
2680  *
2681  * returns:
2682  */
2683 static int
2684 zfcp_fsf_close_port_handler(struct zfcp_fsf_req *fsf_req)
2685 {
2686 	int retval = -EINVAL;
2687 	struct zfcp_port *port;
2688 
2689 	port = (struct zfcp_port *) fsf_req->data;
2690 
2691 	if (fsf_req->status & ZFCP_STATUS_FSFREQ_ERROR) {
2692 		/* don't change port status in our bookkeeping */
2693 		goto skip_fsfstatus;
2694 	}
2695 
2696 	/* evaluate FSF status in QTCB */
2697 	switch (fsf_req->qtcb->header.fsf_status) {
2698 
2699 	case FSF_PORT_HANDLE_NOT_VALID:
2700 		ZFCP_LOG_INFO("Temporary port identifier 0x%x for port "
2701 			      "0x%016Lx on adapter %s invalid. This may happen "
2702 			      "occasionally.\n", port->handle,
2703 			      port->wwpn, zfcp_get_busid_by_port(port));
2704 		ZFCP_LOG_DEBUG("status qualifier:\n");
2705 		ZFCP_HEX_DUMP(ZFCP_LOG_LEVEL_DEBUG,
2706 			      (char *) &fsf_req->qtcb->header.fsf_status_qual,
2707 			      sizeof (union fsf_status_qual));
2708 		debug_text_event(fsf_req->adapter->erp_dbf, 1,
2709 				 "fsf_s_phand_nv");
2710 		zfcp_erp_adapter_reopen(port->adapter, 0);
2711 		fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
2712 		break;
2713 
2714 	case FSF_ADAPTER_STATUS_AVAILABLE:
2715 		/* Note: FSF has actually closed the port in this case.
2716 		 * The status code is just daft. Fingers crossed for a change
2717 		 */
2718 		retval = 0;
2719 		break;
2720 
2721 	case FSF_GOOD:
2722 		ZFCP_LOG_TRACE("remote port 0x016%Lx on adapter %s closed, "
2723 			       "port handle 0x%x\n", port->wwpn,
2724 			       zfcp_get_busid_by_port(port), port->handle);
2725 		zfcp_erp_modify_port_status(port,
2726 					    ZFCP_STATUS_COMMON_OPEN,
2727 					    ZFCP_CLEAR);
2728 		retval = 0;
2729 		break;
2730 
2731 	default:
2732 		ZFCP_LOG_NORMAL("bug: An unknown FSF Status was presented "
2733 				"(debug info 0x%x)\n",
2734 				fsf_req->qtcb->header.fsf_status);
2735 		debug_text_event(fsf_req->adapter->erp_dbf, 0, "fsf_s_inval:");
2736 		debug_exception(fsf_req->adapter->erp_dbf, 0,
2737 				&fsf_req->qtcb->header.fsf_status,
2738 				sizeof (u32));
2739 		break;
2740 	}
2741 
2742  skip_fsfstatus:
2743 	atomic_clear_mask(ZFCP_STATUS_COMMON_CLOSING, &port->status);
2744 	return retval;
2745 }
2746 
2747 /*
2748  * function:    zfcp_fsf_close_physical_port
2749  *
2750  * purpose:     submit FSF command "close physical port"
2751  *
2752  * returns:     address of initiated FSF request
2753  *              NULL - request could not be initiated
2754  */
2755 int
2756 zfcp_fsf_close_physical_port(struct zfcp_erp_action *erp_action)
2757 {
2758 	int retval = 0;
2759 	unsigned long lock_flags;
2760 	volatile struct qdio_buffer_element *sbale;
2761 
2762 	/* setup new FSF request */
2763 	retval = zfcp_fsf_req_create(erp_action->adapter,
2764 				     FSF_QTCB_CLOSE_PHYSICAL_PORT,
2765 				     ZFCP_WAIT_FOR_SBAL | ZFCP_REQ_AUTO_CLEANUP,
2766 				     erp_action->adapter->pool.fsf_req_erp,
2767 				     &lock_flags, &erp_action->fsf_req);
2768 	if (retval < 0) {
2769 		ZFCP_LOG_INFO("error: Could not create close physical port "
2770 			      "request (adapter %s, port 0x%016Lx)\n",
2771 			      zfcp_get_busid_by_adapter(erp_action->adapter),
2772 			      erp_action->port->wwpn);
2773 
2774 		goto out;
2775 	}
2776 
2777 	sbale = zfcp_qdio_sbale_req(erp_action->fsf_req,
2778 				    erp_action->fsf_req->sbal_curr, 0);
2779 	sbale[0].flags |= SBAL_FLAGS0_TYPE_READ;
2780 	sbale[1].flags |= SBAL_FLAGS_LAST_ENTRY;
2781 
2782 	/* mark port as being closed */
2783 	atomic_set_mask(ZFCP_STATUS_PORT_PHYS_CLOSING,
2784 			&erp_action->port->status);
2785 	/* save a pointer to this port */
2786 	erp_action->fsf_req->data = (unsigned long) erp_action->port;
2787 	/* port to be closed */
2788 	erp_action->fsf_req->qtcb->header.port_handle =
2789 	    erp_action->port->handle;
2790 	erp_action->fsf_req->erp_action = erp_action;
2791 
2792 	/* start QDIO request for this FSF request */
2793 	retval = zfcp_fsf_req_send(erp_action->fsf_req, &erp_action->timer);
2794 	if (retval) {
2795 		ZFCP_LOG_INFO("error: Could not send close physical port "
2796 			      "request (adapter %s, port 0x%016Lx)\n",
2797 			      zfcp_get_busid_by_adapter(erp_action->adapter),
2798 			      erp_action->port->wwpn);
2799 		zfcp_fsf_req_free(erp_action->fsf_req);
2800 		erp_action->fsf_req = NULL;
2801 		goto out;
2802 	}
2803 
2804 	ZFCP_LOG_TRACE("close physical port request initiated "
2805 		       "(adapter %s, port 0x%016Lx)\n",
2806 		       zfcp_get_busid_by_adapter(erp_action->adapter),
2807 		       erp_action->port->wwpn);
2808  out:
2809 	write_unlock_irqrestore(&erp_action->adapter->request_queue.queue_lock,
2810 				lock_flags);
2811 	return retval;
2812 }
2813 
2814 /*
2815  * function:    zfcp_fsf_close_physical_port_handler
2816  *
2817  * purpose:     is called for finished Close Physical Port FSF command
2818  *
2819  * returns:
2820  */
2821 static int
2822 zfcp_fsf_close_physical_port_handler(struct zfcp_fsf_req *fsf_req)
2823 {
2824 	int retval = -EINVAL;
2825 	struct zfcp_port *port;
2826 	struct zfcp_unit *unit;
2827 	struct fsf_qtcb_header *header;
2828 	u16 subtable, rule, counter;
2829 
2830 	port = (struct zfcp_port *) fsf_req->data;
2831 	header = &fsf_req->qtcb->header;
2832 
2833 	if (fsf_req->status & ZFCP_STATUS_FSFREQ_ERROR) {
2834 		/* don't change port status in our bookkeeping */
2835 		goto skip_fsfstatus;
2836 	}
2837 
2838 	/* evaluate FSF status in QTCB */
2839 	switch (header->fsf_status) {
2840 
2841 	case FSF_PORT_HANDLE_NOT_VALID:
2842 		ZFCP_LOG_INFO("Temporary port identifier 0x%x invalid"
2843 			      "(adapter %s, port 0x%016Lx). "
2844 			      "This may happen occasionally.\n",
2845 			      port->handle,
2846 			      zfcp_get_busid_by_port(port),
2847 			      port->wwpn);
2848 		ZFCP_LOG_DEBUG("status qualifier:\n");
2849 		ZFCP_HEX_DUMP(ZFCP_LOG_LEVEL_DEBUG,
2850 			      (char *) &header->fsf_status_qual,
2851 			      sizeof (union fsf_status_qual));
2852 		debug_text_event(fsf_req->adapter->erp_dbf, 1,
2853 				 "fsf_s_phand_nv");
2854 		zfcp_erp_adapter_reopen(port->adapter, 0);
2855 		fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
2856 		break;
2857 
2858 	case FSF_ACCESS_DENIED:
2859 		ZFCP_LOG_NORMAL("Access denied, cannot close "
2860 				"physical port 0x%016Lx on adapter %s\n",
2861 				port->wwpn, zfcp_get_busid_by_port(port));
2862 		for (counter = 0; counter < 2; counter++) {
2863 			subtable = header->fsf_status_qual.halfword[counter * 2];
2864 			rule = header->fsf_status_qual.halfword[counter * 2 + 1];
2865 			switch (subtable) {
2866 			case FSF_SQ_CFDC_SUBTABLE_OS:
2867 			case FSF_SQ_CFDC_SUBTABLE_PORT_WWPN:
2868 			case FSF_SQ_CFDC_SUBTABLE_PORT_DID:
2869 			case FSF_SQ_CFDC_SUBTABLE_LUN:
2870 	       			ZFCP_LOG_INFO("Access denied (%s rule %d)\n",
2871 					zfcp_act_subtable_type[subtable], rule);
2872 				break;
2873 			}
2874 		}
2875 		debug_text_event(fsf_req->adapter->erp_dbf, 1, "fsf_s_access");
2876 		zfcp_erp_port_access_denied(port);
2877 		fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
2878 		break;
2879 
2880 	case FSF_PORT_BOXED:
2881 		ZFCP_LOG_DEBUG("The remote port 0x%016Lx on adapter "
2882 			       "%s needs to be reopened but it was attempted "
2883 			       "to close it physically.\n",
2884 			       port->wwpn,
2885 			       zfcp_get_busid_by_port(port));
2886 		debug_text_event(fsf_req->adapter->erp_dbf, 1, "fsf_s_pboxed");
2887 		zfcp_erp_port_boxed(port);
2888 		fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR |
2889 			ZFCP_STATUS_FSFREQ_RETRY;
2890 		break;
2891 
2892 	case FSF_ADAPTER_STATUS_AVAILABLE:
2893 		switch (header->fsf_status_qual.word[0]) {
2894 		case FSF_SQ_INVOKE_LINK_TEST_PROCEDURE:
2895 			debug_text_event(fsf_req->adapter->erp_dbf, 1,
2896 					 "fsf_sq_ltest");
2897 			/* This will now be escalated by ERP */
2898 			fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
2899 			break;
2900 		case FSF_SQ_ULP_DEPENDENT_ERP_REQUIRED:
2901 			/* ERP strategy will escalate */
2902 			debug_text_event(fsf_req->adapter->erp_dbf, 1,
2903 					 "fsf_sq_ulp");
2904 			fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
2905 			break;
2906 		default:
2907 			ZFCP_LOG_NORMAL
2908 			    ("bug: Wrong status qualifier 0x%x arrived.\n",
2909 			     header->fsf_status_qual.word[0]);
2910 			debug_text_event(fsf_req->adapter->erp_dbf, 0,
2911 					 "fsf_sq_inval:");
2912 			debug_exception(
2913 				fsf_req->adapter->erp_dbf, 0,
2914 				&header->fsf_status_qual.word[0], sizeof (u32));
2915 			break;
2916 		}
2917 		break;
2918 
2919 	case FSF_GOOD:
2920 		ZFCP_LOG_DEBUG("Remote port 0x%016Lx via adapter %s "
2921 			       "physically closed, port handle 0x%x\n",
2922 			       port->wwpn,
2923 			       zfcp_get_busid_by_port(port), port->handle);
2924 		/* can't use generic zfcp_erp_modify_port_status because
2925 		 * ZFCP_STATUS_COMMON_OPEN must not be reset for the port
2926 		 */
2927 		atomic_clear_mask(ZFCP_STATUS_PORT_PHYS_OPEN, &port->status);
2928 		list_for_each_entry(unit, &port->unit_list_head, list)
2929 		    atomic_clear_mask(ZFCP_STATUS_COMMON_OPEN, &unit->status);
2930 		retval = 0;
2931 		break;
2932 
2933 	default:
2934 		ZFCP_LOG_NORMAL("bug: An unknown FSF Status was presented "
2935 				"(debug info 0x%x)\n",
2936 				header->fsf_status);
2937 		debug_text_event(fsf_req->adapter->erp_dbf, 0, "fsf_s_inval:");
2938 		debug_exception(fsf_req->adapter->erp_dbf, 0,
2939 				&header->fsf_status, sizeof (u32));
2940 		break;
2941 	}
2942 
2943  skip_fsfstatus:
2944 	atomic_clear_mask(ZFCP_STATUS_PORT_PHYS_CLOSING, &port->status);
2945 	return retval;
2946 }
2947 
2948 /*
2949  * function:    zfcp_fsf_open_unit
2950  *
2951  * purpose:
2952  *
2953  * returns:
2954  *
2955  * assumptions:	This routine does not check whether the associated
2956  *		remote port has already been opened. This should be
2957  *		done by calling routines. Otherwise some status
2958  *		may be presented by FSF
2959  */
2960 int
2961 zfcp_fsf_open_unit(struct zfcp_erp_action *erp_action)
2962 {
2963 	volatile struct qdio_buffer_element *sbale;
2964 	unsigned long lock_flags;
2965 	int retval = 0;
2966 
2967 	/* setup new FSF request */
2968 	retval = zfcp_fsf_req_create(erp_action->adapter,
2969 				     FSF_QTCB_OPEN_LUN,
2970 				     ZFCP_WAIT_FOR_SBAL | ZFCP_REQ_AUTO_CLEANUP,
2971 				     erp_action->adapter->pool.fsf_req_erp,
2972 				     &lock_flags, &(erp_action->fsf_req));
2973 	if (retval < 0) {
2974 		ZFCP_LOG_INFO("error: Could not create open unit request for "
2975 			      "unit 0x%016Lx on port 0x%016Lx on adapter %s.\n",
2976 			      erp_action->unit->fcp_lun,
2977 			      erp_action->unit->port->wwpn,
2978 			      zfcp_get_busid_by_adapter(erp_action->adapter));
2979 		goto out;
2980 	}
2981 
2982 	sbale = zfcp_qdio_sbale_req(erp_action->fsf_req,
2983                                     erp_action->fsf_req->sbal_curr, 0);
2984         sbale[0].flags |= SBAL_FLAGS0_TYPE_READ;
2985         sbale[1].flags |= SBAL_FLAGS_LAST_ENTRY;
2986 
2987 	erp_action->fsf_req->qtcb->header.port_handle =
2988 		erp_action->port->handle;
2989 	erp_action->fsf_req->qtcb->bottom.support.fcp_lun =
2990 		erp_action->unit->fcp_lun;
2991 	if (!(erp_action->adapter->connection_features & FSF_FEATURE_NPIV_MODE))
2992 		erp_action->fsf_req->qtcb->bottom.support.option =
2993 			FSF_OPEN_LUN_SUPPRESS_BOXING;
2994 	atomic_set_mask(ZFCP_STATUS_COMMON_OPENING, &erp_action->unit->status);
2995 	erp_action->fsf_req->data = (unsigned long) erp_action->unit;
2996 	erp_action->fsf_req->erp_action = erp_action;
2997 
2998 	/* start QDIO request for this FSF request */
2999 	retval = zfcp_fsf_req_send(erp_action->fsf_req, &erp_action->timer);
3000 	if (retval) {
3001 		ZFCP_LOG_INFO("error: Could not send an open unit request "
3002 			      "on the adapter %s, port 0x%016Lx for "
3003 			      "unit 0x%016Lx\n",
3004 			      zfcp_get_busid_by_adapter(erp_action->adapter),
3005 			      erp_action->port->wwpn,
3006 			      erp_action->unit->fcp_lun);
3007 		zfcp_fsf_req_free(erp_action->fsf_req);
3008 		erp_action->fsf_req = NULL;
3009 		goto out;
3010 	}
3011 
3012 	ZFCP_LOG_TRACE("Open LUN request initiated (adapter %s, "
3013 		       "port 0x%016Lx, unit 0x%016Lx)\n",
3014 		       zfcp_get_busid_by_adapter(erp_action->adapter),
3015 		       erp_action->port->wwpn, erp_action->unit->fcp_lun);
3016  out:
3017 	write_unlock_irqrestore(&erp_action->adapter->request_queue.queue_lock,
3018 				lock_flags);
3019 	return retval;
3020 }
3021 
3022 /*
3023  * function:    zfcp_fsf_open_unit_handler
3024  *
3025  * purpose:	is called for finished Open LUN command
3026  *
3027  * returns:
3028  */
3029 static int
3030 zfcp_fsf_open_unit_handler(struct zfcp_fsf_req *fsf_req)
3031 {
3032 	int retval = -EINVAL;
3033 	struct zfcp_adapter *adapter;
3034 	struct zfcp_unit *unit;
3035 	struct fsf_qtcb_header *header;
3036 	struct fsf_qtcb_bottom_support *bottom;
3037 	struct fsf_queue_designator *queue_designator;
3038 	u16 subtable, rule, counter;
3039 	int exclusive, readwrite;
3040 
3041 	unit = (struct zfcp_unit *) fsf_req->data;
3042 
3043 	if (fsf_req->status & ZFCP_STATUS_FSFREQ_ERROR) {
3044 		/* don't change unit status in our bookkeeping */
3045 		goto skip_fsfstatus;
3046 	}
3047 
3048 	adapter = fsf_req->adapter;
3049 	header = &fsf_req->qtcb->header;
3050 	bottom = &fsf_req->qtcb->bottom.support;
3051 	queue_designator = &header->fsf_status_qual.fsf_queue_designator;
3052 
3053 	atomic_clear_mask(ZFCP_STATUS_COMMON_ACCESS_DENIED |
3054 			  ZFCP_STATUS_UNIT_SHARED |
3055 			  ZFCP_STATUS_UNIT_READONLY,
3056 			  &unit->status);
3057 
3058 	/* evaluate FSF status in QTCB */
3059 	switch (header->fsf_status) {
3060 
3061 	case FSF_PORT_HANDLE_NOT_VALID:
3062 		ZFCP_LOG_INFO("Temporary port identifier 0x%x "
3063 			      "for port 0x%016Lx on adapter %s invalid "
3064 			      "This may happen occasionally\n",
3065 			      unit->port->handle,
3066 			      unit->port->wwpn, zfcp_get_busid_by_unit(unit));
3067 		ZFCP_LOG_DEBUG("status qualifier:\n");
3068 		ZFCP_HEX_DUMP(ZFCP_LOG_LEVEL_DEBUG,
3069 			      (char *) &header->fsf_status_qual,
3070 			      sizeof (union fsf_status_qual));
3071 		debug_text_event(adapter->erp_dbf, 1, "fsf_s_ph_nv");
3072 		zfcp_erp_adapter_reopen(unit->port->adapter, 0);
3073 		fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
3074 		break;
3075 
3076 	case FSF_LUN_ALREADY_OPEN:
3077 		ZFCP_LOG_NORMAL("bug: Attempted to open unit 0x%016Lx on "
3078 				"remote port 0x%016Lx on adapter %s twice.\n",
3079 				unit->fcp_lun,
3080 				unit->port->wwpn, zfcp_get_busid_by_unit(unit));
3081 		debug_text_exception(adapter->erp_dbf, 0,
3082 				     "fsf_s_uopen");
3083 		fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
3084 		break;
3085 
3086 	case FSF_ACCESS_DENIED:
3087 		ZFCP_LOG_NORMAL("Access denied, cannot open unit 0x%016Lx on "
3088 				"remote port 0x%016Lx on adapter %s\n",
3089 				unit->fcp_lun, unit->port->wwpn,
3090 				zfcp_get_busid_by_unit(unit));
3091 		for (counter = 0; counter < 2; counter++) {
3092 			subtable = header->fsf_status_qual.halfword[counter * 2];
3093 			rule = header->fsf_status_qual.halfword[counter * 2 + 1];
3094 			switch (subtable) {
3095 			case FSF_SQ_CFDC_SUBTABLE_OS:
3096 			case FSF_SQ_CFDC_SUBTABLE_PORT_WWPN:
3097 			case FSF_SQ_CFDC_SUBTABLE_PORT_DID:
3098 			case FSF_SQ_CFDC_SUBTABLE_LUN:
3099 				ZFCP_LOG_INFO("Access denied (%s rule %d)\n",
3100 					zfcp_act_subtable_type[subtable], rule);
3101 				break;
3102 			}
3103 		}
3104 		debug_text_event(adapter->erp_dbf, 1, "fsf_s_access");
3105 		zfcp_erp_unit_access_denied(unit);
3106 		atomic_clear_mask(ZFCP_STATUS_UNIT_SHARED, &unit->status);
3107                 atomic_clear_mask(ZFCP_STATUS_UNIT_READONLY, &unit->status);
3108 		fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
3109 		break;
3110 
3111 	case FSF_PORT_BOXED:
3112 		ZFCP_LOG_DEBUG("The remote port 0x%016Lx on adapter %s "
3113 			       "needs to be reopened\n",
3114 			       unit->port->wwpn, zfcp_get_busid_by_unit(unit));
3115 		debug_text_event(adapter->erp_dbf, 2, "fsf_s_pboxed");
3116 		zfcp_erp_port_boxed(unit->port);
3117 		fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR |
3118 			ZFCP_STATUS_FSFREQ_RETRY;
3119 		break;
3120 
3121 	case FSF_LUN_SHARING_VIOLATION:
3122 		if (header->fsf_status_qual.word[0] != 0) {
3123 			ZFCP_LOG_NORMAL("FCP-LUN 0x%Lx at the remote port "
3124 					"with WWPN 0x%Lx "
3125 					"connected to the adapter %s "
3126 					"is already in use in LPAR%d, CSS%d\n",
3127 					unit->fcp_lun,
3128 					unit->port->wwpn,
3129 					zfcp_get_busid_by_unit(unit),
3130 					queue_designator->hla,
3131 					queue_designator->cssid);
3132 		} else {
3133 			subtable = header->fsf_status_qual.halfword[4];
3134 			rule = header->fsf_status_qual.halfword[5];
3135 			switch (subtable) {
3136 			case FSF_SQ_CFDC_SUBTABLE_OS:
3137 			case FSF_SQ_CFDC_SUBTABLE_PORT_WWPN:
3138 			case FSF_SQ_CFDC_SUBTABLE_PORT_DID:
3139 			case FSF_SQ_CFDC_SUBTABLE_LUN:
3140 				ZFCP_LOG_NORMAL("Access to FCP-LUN 0x%Lx at the "
3141 						"remote port with WWPN 0x%Lx "
3142 						"connected to the adapter %s "
3143 						"is denied (%s rule %d)\n",
3144 						unit->fcp_lun,
3145 						unit->port->wwpn,
3146 						zfcp_get_busid_by_unit(unit),
3147 						zfcp_act_subtable_type[subtable],
3148 						rule);
3149 				break;
3150 			}
3151 		}
3152 		ZFCP_LOG_DEBUG("status qualifier:\n");
3153 		ZFCP_HEX_DUMP(ZFCP_LOG_LEVEL_DEBUG,
3154 			      (char *) &header->fsf_status_qual,
3155 			      sizeof (union fsf_status_qual));
3156 		debug_text_event(adapter->erp_dbf, 2,
3157 				 "fsf_s_l_sh_vio");
3158 		zfcp_erp_unit_access_denied(unit);
3159 		atomic_clear_mask(ZFCP_STATUS_UNIT_SHARED, &unit->status);
3160 		atomic_clear_mask(ZFCP_STATUS_UNIT_READONLY, &unit->status);
3161 		fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
3162 		break;
3163 
3164 	case FSF_MAXIMUM_NUMBER_OF_LUNS_EXCEEDED:
3165 		ZFCP_LOG_INFO("error: The adapter ran out of resources. "
3166 			      "There is no handle (temporary port identifier) "
3167 			      "available for unit 0x%016Lx on port 0x%016Lx "
3168 			      "on adapter %s\n",
3169 			      unit->fcp_lun,
3170 			      unit->port->wwpn,
3171 			      zfcp_get_busid_by_unit(unit));
3172 		debug_text_event(adapter->erp_dbf, 1,
3173 				 "fsf_s_max_units");
3174 		zfcp_erp_unit_failed(unit);
3175 		fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
3176 		break;
3177 
3178 	case FSF_ADAPTER_STATUS_AVAILABLE:
3179 		switch (header->fsf_status_qual.word[0]) {
3180 		case FSF_SQ_INVOKE_LINK_TEST_PROCEDURE:
3181 			/* Re-establish link to port */
3182 			debug_text_event(adapter->erp_dbf, 1,
3183 					 "fsf_sq_ltest");
3184 			zfcp_test_link(unit->port);
3185 			fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
3186 			break;
3187 		case FSF_SQ_ULP_DEPENDENT_ERP_REQUIRED:
3188 			/* ERP strategy will escalate */
3189 			debug_text_event(adapter->erp_dbf, 1,
3190 					 "fsf_sq_ulp");
3191 			fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
3192 			break;
3193 		default:
3194 			ZFCP_LOG_NORMAL
3195 			    ("bug: Wrong status qualifier 0x%x arrived.\n",
3196 			     header->fsf_status_qual.word[0]);
3197 			debug_text_event(adapter->erp_dbf, 0,
3198 					 "fsf_sq_inval:");
3199 			debug_exception(adapter->erp_dbf, 0,
3200 					&header->fsf_status_qual.word[0],
3201 				sizeof (u32));
3202 		}
3203 		break;
3204 
3205 	case FSF_INVALID_COMMAND_OPTION:
3206 		ZFCP_LOG_NORMAL(
3207 			"Invalid option 0x%x has been specified "
3208 			"in QTCB bottom sent to the adapter %s\n",
3209 			bottom->option,
3210 			zfcp_get_busid_by_adapter(adapter));
3211 		fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
3212 		retval = -EINVAL;
3213 		break;
3214 
3215 	case FSF_GOOD:
3216 		/* save LUN handle assigned by FSF */
3217 		unit->handle = header->lun_handle;
3218 		ZFCP_LOG_TRACE("unit 0x%016Lx on remote port 0x%016Lx on "
3219 			       "adapter %s opened, port handle 0x%x\n",
3220 			       unit->fcp_lun,
3221 			       unit->port->wwpn,
3222 			       zfcp_get_busid_by_unit(unit),
3223 			       unit->handle);
3224 		/* mark unit as open */
3225 		atomic_set_mask(ZFCP_STATUS_COMMON_OPEN, &unit->status);
3226 
3227 		if (!(adapter->connection_features & FSF_FEATURE_NPIV_MODE) &&
3228 		    (adapter->adapter_features & FSF_FEATURE_LUN_SHARING) &&
3229 		    (adapter->ccw_device->id.dev_model != ZFCP_DEVICE_MODEL_PRIV)) {
3230 			exclusive = (bottom->lun_access_info &
3231 					FSF_UNIT_ACCESS_EXCLUSIVE);
3232 			readwrite = (bottom->lun_access_info &
3233 					FSF_UNIT_ACCESS_OUTBOUND_TRANSFER);
3234 
3235 			if (!exclusive)
3236 		                atomic_set_mask(ZFCP_STATUS_UNIT_SHARED,
3237 						&unit->status);
3238 
3239 			if (!readwrite) {
3240                 		atomic_set_mask(ZFCP_STATUS_UNIT_READONLY,
3241 						&unit->status);
3242                 		ZFCP_LOG_NORMAL("read-only access for unit "
3243 						"(adapter %s, wwpn=0x%016Lx, "
3244 						"fcp_lun=0x%016Lx)\n",
3245 						zfcp_get_busid_by_unit(unit),
3246 						unit->port->wwpn,
3247 						unit->fcp_lun);
3248         		}
3249 
3250         		if (exclusive && !readwrite) {
3251                 		ZFCP_LOG_NORMAL("exclusive access of read-only "
3252 						"unit not supported\n");
3253 				zfcp_erp_unit_failed(unit);
3254 				fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
3255 				zfcp_erp_unit_shutdown(unit, 0);
3256         		} else if (!exclusive && readwrite) {
3257                 		ZFCP_LOG_NORMAL("shared access of read-write "
3258 						"unit not supported\n");
3259                 		zfcp_erp_unit_failed(unit);
3260 				fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
3261 				zfcp_erp_unit_shutdown(unit, 0);
3262         		}
3263 		}
3264 
3265 		retval = 0;
3266 		break;
3267 
3268 	default:
3269 		ZFCP_LOG_NORMAL("bug: An unknown FSF Status was presented "
3270 				"(debug info 0x%x)\n",
3271 				header->fsf_status);
3272 		debug_text_event(adapter->erp_dbf, 0, "fsf_s_inval:");
3273 		debug_exception(adapter->erp_dbf, 0,
3274 				&header->fsf_status, sizeof (u32));
3275 		break;
3276 	}
3277 
3278  skip_fsfstatus:
3279 	atomic_clear_mask(ZFCP_STATUS_COMMON_OPENING, &unit->status);
3280 	return retval;
3281 }
3282 
3283 /*
3284  * function:    zfcp_fsf_close_unit
3285  *
3286  * purpose:
3287  *
3288  * returns:	address of fsf_req - request successfully initiated
3289  *		NULL -
3290  *
3291  * assumptions: This routine does not check whether the associated
3292  *              remote port/lun has already been opened. This should be
3293  *              done by calling routines. Otherwise some status
3294  *              may be presented by FSF
3295  */
3296 int
3297 zfcp_fsf_close_unit(struct zfcp_erp_action *erp_action)
3298 {
3299 	volatile struct qdio_buffer_element *sbale;
3300 	unsigned long lock_flags;
3301 	int retval = 0;
3302 
3303 	/* setup new FSF request */
3304 	retval = zfcp_fsf_req_create(erp_action->adapter,
3305 				     FSF_QTCB_CLOSE_LUN,
3306 				     ZFCP_WAIT_FOR_SBAL | ZFCP_REQ_AUTO_CLEANUP,
3307 				     erp_action->adapter->pool.fsf_req_erp,
3308 				     &lock_flags, &(erp_action->fsf_req));
3309 	if (retval < 0) {
3310 		ZFCP_LOG_INFO("error: Could not create close unit request for "
3311 			      "unit 0x%016Lx on port 0x%016Lx on adapter %s.\n",
3312 			      erp_action->unit->fcp_lun,
3313 			      erp_action->port->wwpn,
3314 			      zfcp_get_busid_by_adapter(erp_action->adapter));
3315 		goto out;
3316 	}
3317 
3318 	sbale = zfcp_qdio_sbale_req(erp_action->fsf_req,
3319                                     erp_action->fsf_req->sbal_curr, 0);
3320         sbale[0].flags |= SBAL_FLAGS0_TYPE_READ;
3321         sbale[1].flags |= SBAL_FLAGS_LAST_ENTRY;
3322 
3323 	erp_action->fsf_req->qtcb->header.port_handle =
3324 	    erp_action->port->handle;
3325 	erp_action->fsf_req->qtcb->header.lun_handle = erp_action->unit->handle;
3326 	atomic_set_mask(ZFCP_STATUS_COMMON_CLOSING, &erp_action->unit->status);
3327 	erp_action->fsf_req->data = (unsigned long) erp_action->unit;
3328 	erp_action->fsf_req->erp_action = erp_action;
3329 
3330 	/* start QDIO request for this FSF request */
3331 	retval = zfcp_fsf_req_send(erp_action->fsf_req, &erp_action->timer);
3332 	if (retval) {
3333 		ZFCP_LOG_INFO("error: Could not send a close unit request for "
3334 			      "unit 0x%016Lx on port 0x%016Lx onadapter %s.\n",
3335 			      erp_action->unit->fcp_lun,
3336 			      erp_action->port->wwpn,
3337 			      zfcp_get_busid_by_adapter(erp_action->adapter));
3338 		zfcp_fsf_req_free(erp_action->fsf_req);
3339 		erp_action->fsf_req = NULL;
3340 		goto out;
3341 	}
3342 
3343 	ZFCP_LOG_TRACE("Close LUN request initiated (adapter %s, "
3344 		       "port 0x%016Lx, unit 0x%016Lx)\n",
3345 		       zfcp_get_busid_by_adapter(erp_action->adapter),
3346 		       erp_action->port->wwpn, erp_action->unit->fcp_lun);
3347  out:
3348 	write_unlock_irqrestore(&erp_action->adapter->request_queue.queue_lock,
3349 				lock_flags);
3350 	return retval;
3351 }
3352 
3353 /*
3354  * function:    zfcp_fsf_close_unit_handler
3355  *
3356  * purpose:     is called for finished Close LUN FSF command
3357  *
3358  * returns:
3359  */
3360 static int
3361 zfcp_fsf_close_unit_handler(struct zfcp_fsf_req *fsf_req)
3362 {
3363 	int retval = -EINVAL;
3364 	struct zfcp_unit *unit;
3365 
3366 	unit = (struct zfcp_unit *) fsf_req->data;
3367 
3368 	if (fsf_req->status & ZFCP_STATUS_FSFREQ_ERROR) {
3369 		/* don't change unit status in our bookkeeping */
3370 		goto skip_fsfstatus;
3371 	}
3372 
3373 	/* evaluate FSF status in QTCB */
3374 	switch (fsf_req->qtcb->header.fsf_status) {
3375 
3376 	case FSF_PORT_HANDLE_NOT_VALID:
3377 		ZFCP_LOG_INFO("Temporary port identifier 0x%x for port "
3378 			      "0x%016Lx on adapter %s invalid. This may "
3379 			      "happen in rare circumstances\n",
3380 			      unit->port->handle,
3381 			      unit->port->wwpn,
3382 			      zfcp_get_busid_by_unit(unit));
3383 		ZFCP_LOG_DEBUG("status qualifier:\n");
3384 		ZFCP_HEX_DUMP(ZFCP_LOG_LEVEL_DEBUG,
3385 			      (char *) &fsf_req->qtcb->header.fsf_status_qual,
3386 			      sizeof (union fsf_status_qual));
3387 		debug_text_event(fsf_req->adapter->erp_dbf, 1,
3388 				 "fsf_s_phand_nv");
3389 		zfcp_erp_adapter_reopen(unit->port->adapter, 0);
3390 		fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
3391 		break;
3392 
3393 	case FSF_LUN_HANDLE_NOT_VALID:
3394 		ZFCP_LOG_INFO("Temporary LUN identifier 0x%x of unit "
3395 			      "0x%016Lx on port 0x%016Lx on adapter %s is "
3396 			      "invalid. This may happen occasionally.\n",
3397 			      unit->handle,
3398 			      unit->fcp_lun,
3399 			      unit->port->wwpn,
3400 			      zfcp_get_busid_by_unit(unit));
3401 		ZFCP_LOG_DEBUG("Status qualifier data:\n");
3402 		ZFCP_HEX_DUMP(ZFCP_LOG_LEVEL_DEBUG,
3403 			      (char *) &fsf_req->qtcb->header.fsf_status_qual,
3404 			      sizeof (union fsf_status_qual));
3405 		debug_text_event(fsf_req->adapter->erp_dbf, 1,
3406 				 "fsf_s_lhand_nv");
3407 		zfcp_erp_port_reopen(unit->port, 0);
3408 		fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
3409 		break;
3410 
3411 	case FSF_PORT_BOXED:
3412 		ZFCP_LOG_DEBUG("The remote port 0x%016Lx on adapter %s "
3413 			       "needs to be reopened\n",
3414 			       unit->port->wwpn,
3415 			       zfcp_get_busid_by_unit(unit));
3416 		debug_text_event(fsf_req->adapter->erp_dbf, 2, "fsf_s_pboxed");
3417 		zfcp_erp_port_boxed(unit->port);
3418 		fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR |
3419 			ZFCP_STATUS_FSFREQ_RETRY;
3420 		break;
3421 
3422 	case FSF_ADAPTER_STATUS_AVAILABLE:
3423 		switch (fsf_req->qtcb->header.fsf_status_qual.word[0]) {
3424 		case FSF_SQ_INVOKE_LINK_TEST_PROCEDURE:
3425 			/* re-establish link to port */
3426 			debug_text_event(fsf_req->adapter->erp_dbf, 1,
3427 					 "fsf_sq_ltest");
3428 			zfcp_test_link(unit->port);
3429 			fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
3430 			break;
3431 		case FSF_SQ_ULP_DEPENDENT_ERP_REQUIRED:
3432 			/* ERP strategy will escalate */
3433 			debug_text_event(fsf_req->adapter->erp_dbf, 1,
3434 					 "fsf_sq_ulp");
3435 			fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
3436 			break;
3437 		default:
3438 			ZFCP_LOG_NORMAL
3439 			    ("bug: Wrong status qualifier 0x%x arrived.\n",
3440 			     fsf_req->qtcb->header.fsf_status_qual.word[0]);
3441 			debug_text_event(fsf_req->adapter->erp_dbf, 0,
3442 					 "fsf_sq_inval:");
3443 			debug_exception(
3444 				fsf_req->adapter->erp_dbf, 0,
3445 				&fsf_req->qtcb->header.fsf_status_qual.word[0],
3446 				sizeof (u32));
3447 			break;
3448 		}
3449 		break;
3450 
3451 	case FSF_GOOD:
3452 		ZFCP_LOG_TRACE("unit 0x%016Lx on port 0x%016Lx on adapter %s "
3453 			       "closed, port handle 0x%x\n",
3454 			       unit->fcp_lun,
3455 			       unit->port->wwpn,
3456 			       zfcp_get_busid_by_unit(unit),
3457 			       unit->handle);
3458 		/* mark unit as closed */
3459 		atomic_clear_mask(ZFCP_STATUS_COMMON_OPEN, &unit->status);
3460 		retval = 0;
3461 		break;
3462 
3463 	default:
3464 		ZFCP_LOG_NORMAL("bug: An unknown FSF Status was presented "
3465 				"(debug info 0x%x)\n",
3466 				fsf_req->qtcb->header.fsf_status);
3467 		debug_text_event(fsf_req->adapter->erp_dbf, 0, "fsf_s_inval:");
3468 		debug_exception(fsf_req->adapter->erp_dbf, 0,
3469 				&fsf_req->qtcb->header.fsf_status,
3470 				sizeof (u32));
3471 		break;
3472 	}
3473 
3474  skip_fsfstatus:
3475 	atomic_clear_mask(ZFCP_STATUS_COMMON_CLOSING, &unit->status);
3476 	return retval;
3477 }
3478 
3479 /**
3480  * zfcp_fsf_send_fcp_command_task - initiate an FCP command (for a SCSI command)
3481  * @adapter: adapter where scsi command is issued
3482  * @unit: unit where command is sent to
3483  * @scsi_cmnd: scsi command to be sent
3484  * @timer: timer to be started when request is initiated
3485  * @req_flags: flags for fsf_request
3486  */
3487 int
3488 zfcp_fsf_send_fcp_command_task(struct zfcp_adapter *adapter,
3489 			       struct zfcp_unit *unit,
3490 			       struct scsi_cmnd * scsi_cmnd,
3491 			       struct timer_list *timer, int req_flags)
3492 {
3493 	struct zfcp_fsf_req *fsf_req = NULL;
3494 	struct fcp_cmnd_iu *fcp_cmnd_iu;
3495 	unsigned int sbtype;
3496 	unsigned long lock_flags;
3497 	int real_bytes = 0;
3498 	int retval = 0;
3499 	int mask;
3500 
3501 	/* setup new FSF request */
3502 	retval = zfcp_fsf_req_create(adapter, FSF_QTCB_FCP_CMND, req_flags,
3503 				     adapter->pool.fsf_req_scsi,
3504 				     &lock_flags, &fsf_req);
3505 	if (unlikely(retval < 0)) {
3506 		ZFCP_LOG_DEBUG("error: Could not create FCP command request "
3507 			       "for unit 0x%016Lx on port 0x%016Lx on "
3508 			       "adapter %s\n",
3509 			       unit->fcp_lun,
3510 			       unit->port->wwpn,
3511 			       zfcp_get_busid_by_adapter(adapter));
3512 		goto failed_req_create;
3513 	}
3514 
3515 	zfcp_unit_get(unit);
3516 	fsf_req->unit = unit;
3517 
3518 	/* associate FSF request with SCSI request (for look up on abort) */
3519 	scsi_cmnd->host_scribble = (char *) fsf_req;
3520 
3521 	/* associate SCSI command with FSF request */
3522 	fsf_req->data = (unsigned long) scsi_cmnd;
3523 
3524 	/* set handles of unit and its parent port in QTCB */
3525 	fsf_req->qtcb->header.lun_handle = unit->handle;
3526 	fsf_req->qtcb->header.port_handle = unit->port->handle;
3527 
3528 	/* FSF does not define the structure of the FCP_CMND IU */
3529 	fcp_cmnd_iu = (struct fcp_cmnd_iu *)
3530 	    &(fsf_req->qtcb->bottom.io.fcp_cmnd);
3531 
3532 	/*
3533 	 * set depending on data direction:
3534 	 *      data direction bits in SBALE (SB Type)
3535 	 *      data direction bits in QTCB
3536 	 *      data direction bits in FCP_CMND IU
3537 	 */
3538 	switch (scsi_cmnd->sc_data_direction) {
3539 	case DMA_NONE:
3540 		fsf_req->qtcb->bottom.io.data_direction = FSF_DATADIR_CMND;
3541 		/*
3542 		 * FIXME(qdio):
3543 		 * what is the correct type for commands
3544 		 * without 'real' data buffers?
3545 		 */
3546 		sbtype = SBAL_FLAGS0_TYPE_READ;
3547 		break;
3548 	case DMA_FROM_DEVICE:
3549 		fsf_req->qtcb->bottom.io.data_direction = FSF_DATADIR_READ;
3550 		sbtype = SBAL_FLAGS0_TYPE_READ;
3551 		fcp_cmnd_iu->rddata = 1;
3552 		break;
3553 	case DMA_TO_DEVICE:
3554 		fsf_req->qtcb->bottom.io.data_direction = FSF_DATADIR_WRITE;
3555 		sbtype = SBAL_FLAGS0_TYPE_WRITE;
3556 		fcp_cmnd_iu->wddata = 1;
3557 		break;
3558 	case DMA_BIDIRECTIONAL:
3559 	default:
3560 		/*
3561 		 * dummy, catch this condition earlier
3562 		 * in zfcp_scsi_queuecommand
3563 		 */
3564 		goto failed_scsi_cmnd;
3565 	}
3566 
3567 	/* set FC service class in QTCB (3 per default) */
3568 	fsf_req->qtcb->bottom.io.service_class = ZFCP_FC_SERVICE_CLASS_DEFAULT;
3569 
3570 	/* set FCP_LUN in FCP_CMND IU in QTCB */
3571 	fcp_cmnd_iu->fcp_lun = unit->fcp_lun;
3572 
3573 	mask = ZFCP_STATUS_UNIT_READONLY | ZFCP_STATUS_UNIT_SHARED;
3574 
3575 	/* set task attributes in FCP_CMND IU in QTCB */
3576 	if (likely((scsi_cmnd->device->simple_tags) ||
3577 		   (atomic_test_mask(mask, &unit->status))))
3578 		fcp_cmnd_iu->task_attribute = SIMPLE_Q;
3579 	else
3580 		fcp_cmnd_iu->task_attribute = UNTAGGED;
3581 
3582 	/* set additional length of FCP_CDB in FCP_CMND IU in QTCB, if needed */
3583 	if (unlikely(scsi_cmnd->cmd_len > FCP_CDB_LENGTH)) {
3584 		fcp_cmnd_iu->add_fcp_cdb_length
3585 		    = (scsi_cmnd->cmd_len - FCP_CDB_LENGTH) >> 2;
3586 		ZFCP_LOG_TRACE("SCSI CDB length is 0x%x, "
3587 			       "additional FCP_CDB length is 0x%x "
3588 			       "(shifted right 2 bits)\n",
3589 			       scsi_cmnd->cmd_len,
3590 			       fcp_cmnd_iu->add_fcp_cdb_length);
3591 	}
3592 	/*
3593 	 * copy SCSI CDB (including additional length, if any) to
3594 	 * FCP_CDB in FCP_CMND IU in QTCB
3595 	 */
3596 	memcpy(fcp_cmnd_iu->fcp_cdb, scsi_cmnd->cmnd, scsi_cmnd->cmd_len);
3597 
3598 	/* FCP CMND IU length in QTCB */
3599 	fsf_req->qtcb->bottom.io.fcp_cmnd_length =
3600 		sizeof (struct fcp_cmnd_iu) +
3601 		fcp_cmnd_iu->add_fcp_cdb_length + sizeof (fcp_dl_t);
3602 
3603 	/* generate SBALEs from data buffer */
3604 	real_bytes = zfcp_qdio_sbals_from_scsicmnd(fsf_req, sbtype, scsi_cmnd);
3605 	if (unlikely(real_bytes < 0)) {
3606 		if (fsf_req->sbal_number < ZFCP_MAX_SBALS_PER_REQ) {
3607 			ZFCP_LOG_DEBUG(
3608 				"Data did not fit into available buffer(s), "
3609 			       "waiting for more...\n");
3610 		retval = -EIO;
3611 	} else {
3612 		ZFCP_LOG_NORMAL("error: No truncation implemented but "
3613 				"required. Shutting down unit "
3614 				"(adapter %s, port 0x%016Lx, "
3615 				"unit 0x%016Lx)\n",
3616 				zfcp_get_busid_by_unit(unit),
3617 				unit->port->wwpn,
3618 				unit->fcp_lun);
3619 		zfcp_erp_unit_shutdown(unit, 0);
3620 		retval = -EINVAL;
3621 		}
3622 		goto no_fit;
3623 	}
3624 
3625 	/* set length of FCP data length in FCP_CMND IU in QTCB */
3626 	zfcp_set_fcp_dl(fcp_cmnd_iu, real_bytes);
3627 
3628 	ZFCP_LOG_DEBUG("Sending SCSI command:\n");
3629 	ZFCP_HEX_DUMP(ZFCP_LOG_LEVEL_DEBUG,
3630 		      (char *) scsi_cmnd->cmnd, scsi_cmnd->cmd_len);
3631 
3632 	/*
3633 	 * start QDIO request for this FSF request
3634 	 *  covered by an SBALE)
3635 	 */
3636 	retval = zfcp_fsf_req_send(fsf_req, timer);
3637 	if (unlikely(retval < 0)) {
3638 		ZFCP_LOG_INFO("error: Could not send FCP command request "
3639 			      "on adapter %s, port 0x%016Lx, unit 0x%016Lx\n",
3640 			      zfcp_get_busid_by_adapter(adapter),
3641 			      unit->port->wwpn,
3642 			      unit->fcp_lun);
3643 		goto send_failed;
3644 	}
3645 
3646 	ZFCP_LOG_TRACE("Send FCP Command initiated (adapter %s, "
3647 		       "port 0x%016Lx, unit 0x%016Lx)\n",
3648 		       zfcp_get_busid_by_adapter(adapter),
3649 		       unit->port->wwpn,
3650 		       unit->fcp_lun);
3651 	goto success;
3652 
3653  send_failed:
3654  no_fit:
3655  failed_scsi_cmnd:
3656 	zfcp_unit_put(unit);
3657 	zfcp_fsf_req_free(fsf_req);
3658 	fsf_req = NULL;
3659 	scsi_cmnd->host_scribble = NULL;
3660  success:
3661  failed_req_create:
3662 	write_unlock_irqrestore(&adapter->request_queue.queue_lock, lock_flags);
3663 	return retval;
3664 }
3665 
3666 struct zfcp_fsf_req *
3667 zfcp_fsf_send_fcp_command_task_management(struct zfcp_adapter *adapter,
3668 					  struct zfcp_unit *unit,
3669 					  u8 tm_flags, int req_flags)
3670 {
3671 	struct zfcp_fsf_req *fsf_req = NULL;
3672 	int retval = 0;
3673 	struct fcp_cmnd_iu *fcp_cmnd_iu;
3674 	unsigned long lock_flags;
3675 	volatile struct qdio_buffer_element *sbale;
3676 
3677 	/* setup new FSF request */
3678 	retval = zfcp_fsf_req_create(adapter, FSF_QTCB_FCP_CMND, req_flags,
3679 				     adapter->pool.fsf_req_scsi,
3680 				     &lock_flags, &fsf_req);
3681 	if (retval < 0) {
3682 		ZFCP_LOG_INFO("error: Could not create FCP command (task "
3683 			      "management) request for adapter %s, port "
3684 			      " 0x%016Lx, unit 0x%016Lx.\n",
3685 			      zfcp_get_busid_by_adapter(adapter),
3686 			      unit->port->wwpn, unit->fcp_lun);
3687 		goto out;
3688 	}
3689 
3690 	/*
3691 	 * Used to decide on proper handler in the return path,
3692 	 * could be either zfcp_fsf_send_fcp_command_task_handler or
3693 	 * zfcp_fsf_send_fcp_command_task_management_handler */
3694 
3695 	fsf_req->status |= ZFCP_STATUS_FSFREQ_TASK_MANAGEMENT;
3696 
3697 	/*
3698 	 * hold a pointer to the unit being target of this
3699 	 * task management request
3700 	 */
3701 	fsf_req->data = (unsigned long) unit;
3702 
3703 	/* set FSF related fields in QTCB */
3704 	fsf_req->qtcb->header.lun_handle = unit->handle;
3705 	fsf_req->qtcb->header.port_handle = unit->port->handle;
3706 	fsf_req->qtcb->bottom.io.data_direction = FSF_DATADIR_CMND;
3707 	fsf_req->qtcb->bottom.io.service_class = ZFCP_FC_SERVICE_CLASS_DEFAULT;
3708 	fsf_req->qtcb->bottom.io.fcp_cmnd_length =
3709 		sizeof (struct fcp_cmnd_iu) + sizeof (fcp_dl_t);
3710 
3711 	sbale = zfcp_qdio_sbale_req(fsf_req, fsf_req->sbal_curr, 0);
3712 	sbale[0].flags |= SBAL_FLAGS0_TYPE_WRITE;
3713 	sbale[1].flags |= SBAL_FLAGS_LAST_ENTRY;
3714 
3715 	/* set FCP related fields in FCP_CMND IU in QTCB */
3716 	fcp_cmnd_iu = (struct fcp_cmnd_iu *)
3717 		&(fsf_req->qtcb->bottom.io.fcp_cmnd);
3718 	fcp_cmnd_iu->fcp_lun = unit->fcp_lun;
3719 	fcp_cmnd_iu->task_management_flags = tm_flags;
3720 
3721 	/* start QDIO request for this FSF request */
3722 	zfcp_fsf_start_scsi_er_timer(adapter);
3723 	retval = zfcp_fsf_req_send(fsf_req, NULL);
3724 	if (retval) {
3725 		del_timer(&adapter->scsi_er_timer);
3726 		ZFCP_LOG_INFO("error: Could not send an FCP-command (task "
3727 			      "management) on adapter %s, port 0x%016Lx for "
3728 			      "unit LUN 0x%016Lx\n",
3729 			      zfcp_get_busid_by_adapter(adapter),
3730 			      unit->port->wwpn,
3731 			      unit->fcp_lun);
3732 		zfcp_fsf_req_free(fsf_req);
3733 		fsf_req = NULL;
3734 		goto out;
3735 	}
3736 
3737 	ZFCP_LOG_TRACE("Send FCP Command (task management function) initiated "
3738 		       "(adapter %s, port 0x%016Lx, unit 0x%016Lx, "
3739 		       "tm_flags=0x%x)\n",
3740 		       zfcp_get_busid_by_adapter(adapter),
3741 		       unit->port->wwpn,
3742 		       unit->fcp_lun,
3743 		       tm_flags);
3744  out:
3745 	write_unlock_irqrestore(&adapter->request_queue.queue_lock, lock_flags);
3746 	return fsf_req;
3747 }
3748 
3749 /*
3750  * function:    zfcp_fsf_send_fcp_command_handler
3751  *
3752  * purpose:	is called for finished Send FCP Command
3753  *
3754  * returns:
3755  */
3756 static int
3757 zfcp_fsf_send_fcp_command_handler(struct zfcp_fsf_req *fsf_req)
3758 {
3759 	int retval = -EINVAL;
3760 	struct zfcp_unit *unit;
3761 	struct fsf_qtcb_header *header;
3762 	u16 subtable, rule, counter;
3763 
3764 	header = &fsf_req->qtcb->header;
3765 
3766 	if (unlikely(fsf_req->status & ZFCP_STATUS_FSFREQ_TASK_MANAGEMENT))
3767 		unit = (struct zfcp_unit *) fsf_req->data;
3768 	else
3769 		unit = fsf_req->unit;
3770 
3771 	if (unlikely(fsf_req->status & ZFCP_STATUS_FSFREQ_ERROR)) {
3772 		/* go directly to calls of special handlers */
3773 		goto skip_fsfstatus;
3774 	}
3775 
3776 	/* evaluate FSF status in QTCB */
3777 	switch (header->fsf_status) {
3778 
3779 	case FSF_PORT_HANDLE_NOT_VALID:
3780 		ZFCP_LOG_INFO("Temporary port identifier 0x%x for port "
3781 			      "0x%016Lx on adapter %s invalid\n",
3782 			      unit->port->handle,
3783 			      unit->port->wwpn, zfcp_get_busid_by_unit(unit));
3784 		ZFCP_HEX_DUMP(ZFCP_LOG_LEVEL_DEBUG,
3785 			      (char *) &header->fsf_status_qual,
3786 			      sizeof (union fsf_status_qual));
3787 		debug_text_event(fsf_req->adapter->erp_dbf, 1,
3788 				 "fsf_s_phand_nv");
3789 		zfcp_erp_adapter_reopen(unit->port->adapter, 0);
3790 		fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
3791 		break;
3792 
3793 	case FSF_LUN_HANDLE_NOT_VALID:
3794 		ZFCP_LOG_INFO("Temporary LUN identifier 0x%x for unit "
3795 			      "0x%016Lx on port 0x%016Lx on adapter %s is "
3796 			      "invalid. This may happen occasionally.\n",
3797 			      unit->handle,
3798 			      unit->fcp_lun,
3799 			      unit->port->wwpn,
3800 			      zfcp_get_busid_by_unit(unit));
3801 		ZFCP_LOG_NORMAL("Status qualifier data:\n");
3802 		ZFCP_HEX_DUMP(ZFCP_LOG_LEVEL_NORMAL,
3803 			      (char *) &header->fsf_status_qual,
3804 			      sizeof (union fsf_status_qual));
3805 		debug_text_event(fsf_req->adapter->erp_dbf, 1,
3806 				 "fsf_s_uhand_nv");
3807 		zfcp_erp_port_reopen(unit->port, 0);
3808 		fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
3809 		break;
3810 
3811 	case FSF_HANDLE_MISMATCH:
3812 		ZFCP_LOG_NORMAL("bug: The port handle 0x%x has changed "
3813 				"unexpectedly. (adapter %s, port 0x%016Lx, "
3814 				"unit 0x%016Lx)\n",
3815 				unit->port->handle,
3816 				zfcp_get_busid_by_unit(unit),
3817 				unit->port->wwpn,
3818 				unit->fcp_lun);
3819 		ZFCP_LOG_NORMAL("status qualifier:\n");
3820 		ZFCP_HEX_DUMP(ZFCP_LOG_LEVEL_NORMAL,
3821 			      (char *) &header->fsf_status_qual,
3822 			      sizeof (union fsf_status_qual));
3823 		debug_text_event(fsf_req->adapter->erp_dbf, 1,
3824 				 "fsf_s_hand_mis");
3825 		zfcp_erp_adapter_reopen(unit->port->adapter, 0);
3826 		fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
3827 		break;
3828 
3829 	case FSF_SERVICE_CLASS_NOT_SUPPORTED:
3830 		ZFCP_LOG_INFO("error: adapter %s does not support fc "
3831 			      "class %d.\n",
3832 			      zfcp_get_busid_by_unit(unit),
3833 			      ZFCP_FC_SERVICE_CLASS_DEFAULT);
3834 		/* stop operation for this adapter */
3835 		debug_text_exception(fsf_req->adapter->erp_dbf, 0,
3836 				     "fsf_s_class_nsup");
3837 		zfcp_erp_adapter_shutdown(unit->port->adapter, 0);
3838 		fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
3839 		break;
3840 
3841 	case FSF_FCPLUN_NOT_VALID:
3842 		ZFCP_LOG_NORMAL("bug: unit 0x%016Lx on port 0x%016Lx on "
3843 				"adapter %s does not have correct unit "
3844 				"handle 0x%x\n",
3845 				unit->fcp_lun,
3846 				unit->port->wwpn,
3847 				zfcp_get_busid_by_unit(unit),
3848 				unit->handle);
3849 		ZFCP_LOG_DEBUG("status qualifier:\n");
3850 		ZFCP_HEX_DUMP(ZFCP_LOG_LEVEL_DEBUG,
3851 			      (char *) &header->fsf_status_qual,
3852 			      sizeof (union fsf_status_qual));
3853 		debug_text_event(fsf_req->adapter->erp_dbf, 1,
3854 				 "fsf_s_fcp_lun_nv");
3855 		zfcp_erp_port_reopen(unit->port, 0);
3856 		fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
3857 		break;
3858 
3859 	case FSF_ACCESS_DENIED:
3860 		ZFCP_LOG_NORMAL("Access denied, cannot send FCP command to "
3861 				"unit 0x%016Lx on port 0x%016Lx on "
3862 				"adapter %s\n",	unit->fcp_lun, unit->port->wwpn,
3863 				zfcp_get_busid_by_unit(unit));
3864 		for (counter = 0; counter < 2; counter++) {
3865 			subtable = header->fsf_status_qual.halfword[counter * 2];
3866 			rule = header->fsf_status_qual.halfword[counter * 2 + 1];
3867 			switch (subtable) {
3868 			case FSF_SQ_CFDC_SUBTABLE_OS:
3869 			case FSF_SQ_CFDC_SUBTABLE_PORT_WWPN:
3870 			case FSF_SQ_CFDC_SUBTABLE_PORT_DID:
3871 			case FSF_SQ_CFDC_SUBTABLE_LUN:
3872 				ZFCP_LOG_INFO("Access denied (%s rule %d)\n",
3873 					zfcp_act_subtable_type[subtable], rule);
3874 				break;
3875 			}
3876 		}
3877 		debug_text_event(fsf_req->adapter->erp_dbf, 1, "fsf_s_access");
3878 		zfcp_erp_unit_access_denied(unit);
3879 		fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
3880 		break;
3881 
3882 	case FSF_DIRECTION_INDICATOR_NOT_VALID:
3883 		ZFCP_LOG_INFO("bug: Invalid data direction given for unit "
3884 			      "0x%016Lx on port 0x%016Lx on adapter %s "
3885 			      "(debug info %d)\n",
3886 			      unit->fcp_lun,
3887 			      unit->port->wwpn,
3888 			      zfcp_get_busid_by_unit(unit),
3889 			      fsf_req->qtcb->bottom.io.data_direction);
3890 		/* stop operation for this adapter */
3891 		debug_text_event(fsf_req->adapter->erp_dbf, 0,
3892 				 "fsf_s_dir_ind_nv");
3893 		zfcp_erp_adapter_shutdown(unit->port->adapter, 0);
3894 		fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
3895 		break;
3896 
3897 	case FSF_CMND_LENGTH_NOT_VALID:
3898 		ZFCP_LOG_NORMAL
3899 		    ("bug: An invalid control-data-block length field "
3900 		     "was found in a command for unit 0x%016Lx on port "
3901 		     "0x%016Lx on adapter %s " "(debug info %d)\n",
3902 		     unit->fcp_lun, unit->port->wwpn,
3903 		     zfcp_get_busid_by_unit(unit),
3904 		     fsf_req->qtcb->bottom.io.fcp_cmnd_length);
3905 		/* stop operation for this adapter */
3906 		debug_text_event(fsf_req->adapter->erp_dbf, 0,
3907 				 "fsf_s_cmd_len_nv");
3908 		zfcp_erp_adapter_shutdown(unit->port->adapter, 0);
3909 		fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
3910 		break;
3911 
3912 	case FSF_PORT_BOXED:
3913 		ZFCP_LOG_DEBUG("The remote port 0x%016Lx on adapter %s "
3914 			       "needs to be reopened\n",
3915 			       unit->port->wwpn, zfcp_get_busid_by_unit(unit));
3916 		debug_text_event(fsf_req->adapter->erp_dbf, 2, "fsf_s_pboxed");
3917 		zfcp_erp_port_boxed(unit->port);
3918 		fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR |
3919 			ZFCP_STATUS_FSFREQ_RETRY;
3920 		break;
3921 
3922 	case FSF_LUN_BOXED:
3923 		ZFCP_LOG_NORMAL("unit needs to be reopened (adapter %s, "
3924 				"wwpn=0x%016Lx, fcp_lun=0x%016Lx)\n",
3925 				zfcp_get_busid_by_unit(unit),
3926 				unit->port->wwpn, unit->fcp_lun);
3927 		debug_text_event(fsf_req->adapter->erp_dbf, 1, "fsf_s_lboxed");
3928 		zfcp_erp_unit_boxed(unit);
3929 		fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR
3930 			| ZFCP_STATUS_FSFREQ_RETRY;
3931 		break;
3932 
3933 	case FSF_ADAPTER_STATUS_AVAILABLE:
3934 		switch (header->fsf_status_qual.word[0]) {
3935 		case FSF_SQ_INVOKE_LINK_TEST_PROCEDURE:
3936 			/* re-establish link to port */
3937 			debug_text_event(fsf_req->adapter->erp_dbf, 1,
3938 					 "fsf_sq_ltest");
3939  			zfcp_test_link(unit->port);
3940 			break;
3941 		case FSF_SQ_ULP_DEPENDENT_ERP_REQUIRED:
3942 			/* FIXME(hw) need proper specs for proper action */
3943 			/* let scsi stack deal with retries and escalation */
3944 			debug_text_event(fsf_req->adapter->erp_dbf, 1,
3945 					 "fsf_sq_ulp");
3946 			break;
3947 		default:
3948 			ZFCP_LOG_NORMAL
3949  			    ("Unknown status qualifier 0x%x arrived.\n",
3950 			     header->fsf_status_qual.word[0]);
3951 			debug_text_event(fsf_req->adapter->erp_dbf, 0,
3952 					 "fsf_sq_inval:");
3953 			debug_exception(fsf_req->adapter->erp_dbf, 0,
3954 					&header->fsf_status_qual.word[0],
3955 					sizeof(u32));
3956 			break;
3957 		}
3958 		fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
3959 		break;
3960 
3961 	case FSF_GOOD:
3962 		break;
3963 
3964 	case FSF_FCP_RSP_AVAILABLE:
3965 		break;
3966 
3967 	default:
3968 		debug_text_event(fsf_req->adapter->erp_dbf, 0, "fsf_s_inval:");
3969 		debug_exception(fsf_req->adapter->erp_dbf, 0,
3970 				&header->fsf_status, sizeof(u32));
3971 		break;
3972 	}
3973 
3974  skip_fsfstatus:
3975 	if (fsf_req->status & ZFCP_STATUS_FSFREQ_TASK_MANAGEMENT) {
3976 		retval =
3977 		    zfcp_fsf_send_fcp_command_task_management_handler(fsf_req);
3978 	} else {
3979 		retval = zfcp_fsf_send_fcp_command_task_handler(fsf_req);
3980 		fsf_req->unit = NULL;
3981 		zfcp_unit_put(unit);
3982 	}
3983 	return retval;
3984 }
3985 
3986 /*
3987  * function:    zfcp_fsf_send_fcp_command_task_handler
3988  *
3989  * purpose:	evaluates FCP_RSP IU
3990  *
3991  * returns:
3992  */
3993 static int
3994 zfcp_fsf_send_fcp_command_task_handler(struct zfcp_fsf_req *fsf_req)
3995 {
3996 	int retval = 0;
3997 	struct scsi_cmnd *scpnt;
3998 	struct fcp_rsp_iu *fcp_rsp_iu = (struct fcp_rsp_iu *)
3999 	    &(fsf_req->qtcb->bottom.io.fcp_rsp);
4000 	struct fcp_cmnd_iu *fcp_cmnd_iu = (struct fcp_cmnd_iu *)
4001 	    &(fsf_req->qtcb->bottom.io.fcp_cmnd);
4002 	u32 sns_len;
4003 	char *fcp_rsp_info = zfcp_get_fcp_rsp_info_ptr(fcp_rsp_iu);
4004 	unsigned long flags;
4005 	struct zfcp_unit *unit = fsf_req->unit;
4006 
4007 	read_lock_irqsave(&fsf_req->adapter->abort_lock, flags);
4008 	scpnt = (struct scsi_cmnd *) fsf_req->data;
4009 	if (unlikely(!scpnt)) {
4010 		ZFCP_LOG_DEBUG
4011 		    ("Command with fsf_req %p is not associated to "
4012 		     "a scsi command anymore. Aborted?\n", fsf_req);
4013 		goto out;
4014 	}
4015 	if (unlikely(fsf_req->status & ZFCP_STATUS_FSFREQ_ABORTED)) {
4016 		/* FIXME: (design) mid-layer should handle DID_ABORT like
4017 		 *        DID_SOFT_ERROR by retrying the request for devices
4018 		 *        that allow retries.
4019 		 */
4020 		ZFCP_LOG_DEBUG("Setting DID_SOFT_ERROR and SUGGEST_RETRY\n");
4021 		set_host_byte(&scpnt->result, DID_SOFT_ERROR);
4022 		set_driver_byte(&scpnt->result, SUGGEST_RETRY);
4023 		goto skip_fsfstatus;
4024 	}
4025 
4026 	if (unlikely(fsf_req->status & ZFCP_STATUS_FSFREQ_ERROR)) {
4027 		ZFCP_LOG_DEBUG("Setting DID_ERROR\n");
4028 		set_host_byte(&scpnt->result, DID_ERROR);
4029 		goto skip_fsfstatus;
4030 	}
4031 
4032 	/* set message byte of result in SCSI command */
4033 	scpnt->result |= COMMAND_COMPLETE << 8;
4034 
4035 	/*
4036 	 * copy SCSI status code of FCP_STATUS of FCP_RSP IU to status byte
4037 	 * of result in SCSI command
4038 	 */
4039 	scpnt->result |= fcp_rsp_iu->scsi_status;
4040 	if (unlikely(fcp_rsp_iu->scsi_status)) {
4041 		/* DEBUG */
4042 		ZFCP_LOG_DEBUG("status for SCSI Command:\n");
4043 		ZFCP_HEX_DUMP(ZFCP_LOG_LEVEL_DEBUG,
4044 			      scpnt->cmnd, scpnt->cmd_len);
4045 		ZFCP_LOG_DEBUG("SCSI status code 0x%x\n",
4046 				fcp_rsp_iu->scsi_status);
4047 		ZFCP_HEX_DUMP(ZFCP_LOG_LEVEL_DEBUG,
4048 			      (void *) fcp_rsp_iu, sizeof (struct fcp_rsp_iu));
4049 		ZFCP_HEX_DUMP(ZFCP_LOG_LEVEL_DEBUG,
4050 			      zfcp_get_fcp_sns_info_ptr(fcp_rsp_iu),
4051 			      fcp_rsp_iu->fcp_sns_len);
4052 	}
4053 
4054 	/* check FCP_RSP_INFO */
4055 	if (unlikely(fcp_rsp_iu->validity.bits.fcp_rsp_len_valid)) {
4056 		ZFCP_LOG_DEBUG("rsp_len is valid\n");
4057 		switch (fcp_rsp_info[3]) {
4058 		case RSP_CODE_GOOD:
4059 			/* ok, continue */
4060 			ZFCP_LOG_TRACE("no failure or Task Management "
4061 				       "Function complete\n");
4062 			set_host_byte(&scpnt->result, DID_OK);
4063 			break;
4064 		case RSP_CODE_LENGTH_MISMATCH:
4065 			/* hardware bug */
4066 			ZFCP_LOG_NORMAL("bug: FCP response code indictates "
4067 					"that the fibrechannel protocol data "
4068 					"length differs from the burst length. "
4069 					"The problem occured on unit 0x%016Lx "
4070 					"on port 0x%016Lx on adapter %s",
4071 					unit->fcp_lun,
4072 					unit->port->wwpn,
4073 					zfcp_get_busid_by_unit(unit));
4074 			/* dump SCSI CDB as prepared by zfcp */
4075 			ZFCP_HEX_DUMP(ZFCP_LOG_LEVEL_DEBUG,
4076 				      (char *) &fsf_req->qtcb->
4077 				      bottom.io.fcp_cmnd, FSF_FCP_CMND_SIZE);
4078 			set_host_byte(&scpnt->result, DID_ERROR);
4079 			goto skip_fsfstatus;
4080 		case RSP_CODE_FIELD_INVALID:
4081 			/* driver or hardware bug */
4082 			ZFCP_LOG_NORMAL("bug: FCP response code indictates "
4083 					"that the fibrechannel protocol data "
4084 					"fields were incorrectly set up. "
4085 					"The problem occured on the unit "
4086 					"0x%016Lx on port 0x%016Lx on "
4087 					"adapter %s",
4088 					unit->fcp_lun,
4089 					unit->port->wwpn,
4090 					zfcp_get_busid_by_unit(unit));
4091 			/* dump SCSI CDB as prepared by zfcp */
4092 			ZFCP_HEX_DUMP(ZFCP_LOG_LEVEL_DEBUG,
4093 				      (char *) &fsf_req->qtcb->
4094 				      bottom.io.fcp_cmnd, FSF_FCP_CMND_SIZE);
4095 			set_host_byte(&scpnt->result, DID_ERROR);
4096 			goto skip_fsfstatus;
4097 		case RSP_CODE_RO_MISMATCH:
4098 			/* hardware bug */
4099 			ZFCP_LOG_NORMAL("bug: The FCP response code indicates "
4100 					"that conflicting  values for the "
4101 					"fibrechannel payload offset from the "
4102 					"header were found. "
4103 					"The problem occured on unit 0x%016Lx "
4104 					"on port 0x%016Lx on adapter %s.\n",
4105 					unit->fcp_lun,
4106 					unit->port->wwpn,
4107 					zfcp_get_busid_by_unit(unit));
4108 			/* dump SCSI CDB as prepared by zfcp */
4109 			ZFCP_HEX_DUMP(ZFCP_LOG_LEVEL_DEBUG,
4110 				      (char *) &fsf_req->qtcb->
4111 				      bottom.io.fcp_cmnd, FSF_FCP_CMND_SIZE);
4112 			set_host_byte(&scpnt->result, DID_ERROR);
4113 			goto skip_fsfstatus;
4114 		default:
4115 			ZFCP_LOG_NORMAL("bug: An invalid FCP response "
4116 					"code was detected for a command. "
4117 					"The problem occured on the unit "
4118 					"0x%016Lx on port 0x%016Lx on "
4119 					"adapter %s (debug info 0x%x)\n",
4120 					unit->fcp_lun,
4121 					unit->port->wwpn,
4122 					zfcp_get_busid_by_unit(unit),
4123 					fcp_rsp_info[3]);
4124 			/* dump SCSI CDB as prepared by zfcp */
4125 			ZFCP_HEX_DUMP(ZFCP_LOG_LEVEL_DEBUG,
4126 				      (char *) &fsf_req->qtcb->
4127 				      bottom.io.fcp_cmnd, FSF_FCP_CMND_SIZE);
4128 			set_host_byte(&scpnt->result, DID_ERROR);
4129 			goto skip_fsfstatus;
4130 		}
4131 	}
4132 
4133 	/* check for sense data */
4134 	if (unlikely(fcp_rsp_iu->validity.bits.fcp_sns_len_valid)) {
4135 		sns_len = FSF_FCP_RSP_SIZE -
4136 		    sizeof (struct fcp_rsp_iu) + fcp_rsp_iu->fcp_rsp_len;
4137 		ZFCP_LOG_TRACE("room for %i bytes sense data in QTCB\n",
4138 			       sns_len);
4139 		sns_len = min(sns_len, (u32) SCSI_SENSE_BUFFERSIZE);
4140 		ZFCP_LOG_TRACE("room for %i bytes sense data in SCSI command\n",
4141 			       SCSI_SENSE_BUFFERSIZE);
4142 		sns_len = min(sns_len, fcp_rsp_iu->fcp_sns_len);
4143 		ZFCP_LOG_TRACE("scpnt->result =0x%x, command was:\n",
4144 			       scpnt->result);
4145 		ZFCP_HEX_DUMP(ZFCP_LOG_LEVEL_TRACE,
4146 			      (void *) &scpnt->cmnd, scpnt->cmd_len);
4147 
4148 		ZFCP_LOG_TRACE("%i bytes sense data provided by FCP\n",
4149 			       fcp_rsp_iu->fcp_sns_len);
4150 		memcpy(&scpnt->sense_buffer,
4151 		       zfcp_get_fcp_sns_info_ptr(fcp_rsp_iu), sns_len);
4152 		ZFCP_HEX_DUMP(ZFCP_LOG_LEVEL_TRACE,
4153 			      (void *) &scpnt->sense_buffer, sns_len);
4154 	}
4155 
4156 	/* check for overrun */
4157 	if (unlikely(fcp_rsp_iu->validity.bits.fcp_resid_over)) {
4158 		ZFCP_LOG_INFO("A data overrun was detected for a command. "
4159 			      "unit 0x%016Lx, port 0x%016Lx, adapter %s. "
4160 			      "The response data length is "
4161 			      "%d, the original length was %d.\n",
4162 			      unit->fcp_lun,
4163 			      unit->port->wwpn,
4164 			      zfcp_get_busid_by_unit(unit),
4165 			      fcp_rsp_iu->fcp_resid,
4166 			      (int) zfcp_get_fcp_dl(fcp_cmnd_iu));
4167 	}
4168 
4169 	/* check for underrun */
4170 	if (unlikely(fcp_rsp_iu->validity.bits.fcp_resid_under)) {
4171 		ZFCP_LOG_INFO("A data underrun was detected for a command. "
4172 			      "unit 0x%016Lx, port 0x%016Lx, adapter %s. "
4173 			      "The response data length is "
4174 			      "%d, the original length was %d.\n",
4175 			      unit->fcp_lun,
4176 			      unit->port->wwpn,
4177 			      zfcp_get_busid_by_unit(unit),
4178 			      fcp_rsp_iu->fcp_resid,
4179 			      (int) zfcp_get_fcp_dl(fcp_cmnd_iu));
4180 
4181 		scpnt->resid = fcp_rsp_iu->fcp_resid;
4182 		if (scpnt->request_bufflen - scpnt->resid < scpnt->underflow)
4183 			set_host_byte(&scpnt->result, DID_ERROR);
4184 	}
4185 
4186  skip_fsfstatus:
4187 	ZFCP_LOG_DEBUG("scpnt->result =0x%x\n", scpnt->result);
4188 
4189 	if (scpnt->result != 0)
4190 		zfcp_scsi_dbf_event_result("erro", 3, fsf_req->adapter, scpnt, fsf_req);
4191 	else if (scpnt->retries > 0)
4192 		zfcp_scsi_dbf_event_result("retr", 4, fsf_req->adapter, scpnt, fsf_req);
4193 	else
4194 		zfcp_scsi_dbf_event_result("norm", 6, fsf_req->adapter, scpnt, fsf_req);
4195 
4196 	/* cleanup pointer (need this especially for abort) */
4197 	scpnt->host_scribble = NULL;
4198 
4199 	/* always call back */
4200 	(scpnt->scsi_done) (scpnt);
4201 
4202 	/*
4203 	 * We must hold this lock until scsi_done has been called.
4204 	 * Otherwise we may call scsi_done after abort regarding this
4205 	 * command has completed.
4206 	 * Note: scsi_done must not block!
4207 	 */
4208  out:
4209 	read_unlock_irqrestore(&fsf_req->adapter->abort_lock, flags);
4210 	return retval;
4211 }
4212 
4213 /*
4214  * function:    zfcp_fsf_send_fcp_command_task_management_handler
4215  *
4216  * purpose:	evaluates FCP_RSP IU
4217  *
4218  * returns:
4219  */
4220 static int
4221 zfcp_fsf_send_fcp_command_task_management_handler(struct zfcp_fsf_req *fsf_req)
4222 {
4223 	int retval = 0;
4224 	struct fcp_rsp_iu *fcp_rsp_iu = (struct fcp_rsp_iu *)
4225 	    &(fsf_req->qtcb->bottom.io.fcp_rsp);
4226 	char *fcp_rsp_info = zfcp_get_fcp_rsp_info_ptr(fcp_rsp_iu);
4227 	struct zfcp_unit *unit = (struct zfcp_unit *) fsf_req->data;
4228 
4229 	del_timer(&fsf_req->adapter->scsi_er_timer);
4230 	if (fsf_req->status & ZFCP_STATUS_FSFREQ_ERROR) {
4231 		fsf_req->status |= ZFCP_STATUS_FSFREQ_TMFUNCFAILED;
4232 		goto skip_fsfstatus;
4233 	}
4234 
4235 	/* check FCP_RSP_INFO */
4236 	switch (fcp_rsp_info[3]) {
4237 	case RSP_CODE_GOOD:
4238 		/* ok, continue */
4239 		ZFCP_LOG_DEBUG("no failure or Task Management "
4240 			       "Function complete\n");
4241 		break;
4242 	case RSP_CODE_TASKMAN_UNSUPP:
4243 		ZFCP_LOG_NORMAL("bug: A reuested task management function "
4244 				"is not supported on the target device "
4245 				"unit 0x%016Lx, port 0x%016Lx, adapter %s\n ",
4246 				unit->fcp_lun,
4247 				unit->port->wwpn,
4248 				zfcp_get_busid_by_unit(unit));
4249 		fsf_req->status |= ZFCP_STATUS_FSFREQ_TMFUNCNOTSUPP;
4250 		break;
4251 	case RSP_CODE_TASKMAN_FAILED:
4252 		ZFCP_LOG_NORMAL("bug: A reuested task management function "
4253 				"failed to complete successfully. "
4254 				"unit 0x%016Lx, port 0x%016Lx, adapter %s.\n",
4255 				unit->fcp_lun,
4256 				unit->port->wwpn,
4257 				zfcp_get_busid_by_unit(unit));
4258 		fsf_req->status |= ZFCP_STATUS_FSFREQ_TMFUNCFAILED;
4259 		break;
4260 	default:
4261 		ZFCP_LOG_NORMAL("bug: An invalid FCP response "
4262 				"code was detected for a command. "
4263 				"unit 0x%016Lx, port 0x%016Lx, adapter %s "
4264 				"(debug info 0x%x)\n",
4265 				unit->fcp_lun,
4266 				unit->port->wwpn,
4267 				zfcp_get_busid_by_unit(unit),
4268 				fcp_rsp_info[3]);
4269 		fsf_req->status |= ZFCP_STATUS_FSFREQ_TMFUNCFAILED;
4270 	}
4271 
4272       skip_fsfstatus:
4273 	return retval;
4274 }
4275 
4276 
4277 /*
4278  * function:    zfcp_fsf_control_file
4279  *
4280  * purpose:     Initiator of the control file upload/download FSF requests
4281  *
4282  * returns:     0           - FSF request is successfuly created and queued
4283  *              -EOPNOTSUPP - The FCP adapter does not have Control File support
4284  *              -EINVAL     - Invalid direction specified
4285  *              -ENOMEM     - Insufficient memory
4286  *              -EPERM      - Cannot create FSF request or place it in QDIO queue
4287  */
4288 int
4289 zfcp_fsf_control_file(struct zfcp_adapter *adapter,
4290                       struct zfcp_fsf_req **fsf_req_ptr,
4291                       u32 fsf_command,
4292                       u32 option,
4293                       struct zfcp_sg_list *sg_list)
4294 {
4295 	struct zfcp_fsf_req *fsf_req;
4296 	struct fsf_qtcb_bottom_support *bottom;
4297 	volatile struct qdio_buffer_element *sbale;
4298 	struct timer_list *timer;
4299 	unsigned long lock_flags;
4300 	int req_flags = 0;
4301 	int direction;
4302 	int retval = 0;
4303 
4304 	if (!(adapter->adapter_features & FSF_FEATURE_CFDC)) {
4305 		ZFCP_LOG_INFO("cfdc not supported (adapter %s)\n",
4306 			      zfcp_get_busid_by_adapter(adapter));
4307 		retval = -EOPNOTSUPP;
4308 		goto out;
4309 	}
4310 
4311 	switch (fsf_command) {
4312 
4313 	case FSF_QTCB_DOWNLOAD_CONTROL_FILE:
4314 		direction = SBAL_FLAGS0_TYPE_WRITE;
4315 		if ((option != FSF_CFDC_OPTION_FULL_ACCESS) &&
4316 		    (option != FSF_CFDC_OPTION_RESTRICTED_ACCESS))
4317 			req_flags = ZFCP_WAIT_FOR_SBAL;
4318 		break;
4319 
4320 	case FSF_QTCB_UPLOAD_CONTROL_FILE:
4321 		direction = SBAL_FLAGS0_TYPE_READ;
4322 		break;
4323 
4324 	default:
4325 		ZFCP_LOG_INFO("Invalid FSF command code 0x%08x\n", fsf_command);
4326 		retval = -EINVAL;
4327 		goto out;
4328 	}
4329 
4330 	timer = kmalloc(sizeof(struct timer_list), GFP_KERNEL);
4331 	if (!timer) {
4332 		retval = -ENOMEM;
4333 		goto out;
4334  	}
4335 
4336 	retval = zfcp_fsf_req_create(adapter, fsf_command, req_flags,
4337 				     NULL, &lock_flags, &fsf_req);
4338 	if (retval < 0) {
4339 		ZFCP_LOG_INFO("error: Could not create FSF request for the "
4340 			      "adapter %s\n",
4341 			zfcp_get_busid_by_adapter(adapter));
4342 		retval = -EPERM;
4343 		goto unlock_queue_lock;
4344 	}
4345 
4346 	sbale = zfcp_qdio_sbale_req(fsf_req, fsf_req->sbal_curr, 0);
4347 	sbale[0].flags |= direction;
4348 
4349 	bottom = &fsf_req->qtcb->bottom.support;
4350 	bottom->operation_subtype = FSF_CFDC_OPERATION_SUBTYPE;
4351 	bottom->option = option;
4352 
4353 	if (sg_list->count > 0) {
4354 		int bytes;
4355 
4356 		bytes = zfcp_qdio_sbals_from_sg(fsf_req, direction,
4357 						sg_list->sg, sg_list->count,
4358 						ZFCP_MAX_SBALS_PER_REQ);
4359                 if (bytes != ZFCP_CFDC_MAX_CONTROL_FILE_SIZE) {
4360 			ZFCP_LOG_INFO(
4361 				"error: Could not create sufficient number of "
4362 				"SBALS for an FSF request to the adapter %s\n",
4363 				zfcp_get_busid_by_adapter(adapter));
4364 			retval = -ENOMEM;
4365 			goto free_fsf_req;
4366 		}
4367 	} else
4368 		sbale[1].flags |= SBAL_FLAGS_LAST_ENTRY;
4369 
4370 	init_timer(timer);
4371 	timer->function = zfcp_fsf_request_timeout_handler;
4372 	timer->data = (unsigned long) adapter;
4373 	timer->expires = ZFCP_FSF_REQUEST_TIMEOUT;
4374 
4375 	retval = zfcp_fsf_req_send(fsf_req, timer);
4376 	if (retval < 0) {
4377 		ZFCP_LOG_INFO("initiation of cfdc up/download failed"
4378 			      "(adapter %s)\n",
4379 			      zfcp_get_busid_by_adapter(adapter));
4380 		retval = -EPERM;
4381 		goto free_fsf_req;
4382 	}
4383 	write_unlock_irqrestore(&adapter->request_queue.queue_lock, lock_flags);
4384 
4385 	ZFCP_LOG_NORMAL("Control file %s FSF request has been sent to the "
4386 			"adapter %s\n",
4387 			fsf_command == FSF_QTCB_DOWNLOAD_CONTROL_FILE ?
4388 			"download" : "upload",
4389 			zfcp_get_busid_by_adapter(adapter));
4390 
4391 	wait_event(fsf_req->completion_wq,
4392 	           fsf_req->status & ZFCP_STATUS_FSFREQ_COMPLETED);
4393 
4394 	*fsf_req_ptr = fsf_req;
4395 	del_timer_sync(timer);
4396 	goto free_timer;
4397 
4398  free_fsf_req:
4399 	zfcp_fsf_req_free(fsf_req);
4400  unlock_queue_lock:
4401 	write_unlock_irqrestore(&adapter->request_queue.queue_lock, lock_flags);
4402  free_timer:
4403 	kfree(timer);
4404  out:
4405 	return retval;
4406 }
4407 
4408 
4409 /*
4410  * function:    zfcp_fsf_control_file_handler
4411  *
4412  * purpose:     Handler of the control file upload/download FSF requests
4413  *
4414  * returns:     0       - FSF request successfuly processed
4415  *              -EAGAIN - Operation has to be repeated because of a temporary problem
4416  *              -EACCES - There is no permission to execute an operation
4417  *              -EPERM  - The control file is not in a right format
4418  *              -EIO    - There is a problem with the FCP adapter
4419  *              -EINVAL - Invalid operation
4420  *              -EFAULT - User space memory I/O operation fault
4421  */
4422 static int
4423 zfcp_fsf_control_file_handler(struct zfcp_fsf_req *fsf_req)
4424 {
4425 	struct zfcp_adapter *adapter = fsf_req->adapter;
4426 	struct fsf_qtcb_header *header = &fsf_req->qtcb->header;
4427 	struct fsf_qtcb_bottom_support *bottom = &fsf_req->qtcb->bottom.support;
4428 	int retval = 0;
4429 
4430 	if (fsf_req->status & ZFCP_STATUS_FSFREQ_ERROR) {
4431 		retval = -EINVAL;
4432 		goto skip_fsfstatus;
4433 	}
4434 
4435 	switch (header->fsf_status) {
4436 
4437 	case FSF_GOOD:
4438 		ZFCP_LOG_NORMAL(
4439 			"The FSF request has been successfully completed "
4440 			"on the adapter %s\n",
4441 			zfcp_get_busid_by_adapter(adapter));
4442 		break;
4443 
4444 	case FSF_OPERATION_PARTIALLY_SUCCESSFUL:
4445 		if (bottom->operation_subtype == FSF_CFDC_OPERATION_SUBTYPE) {
4446 			switch (header->fsf_status_qual.word[0]) {
4447 
4448 			case FSF_SQ_CFDC_HARDENED_ON_SE:
4449 				ZFCP_LOG_NORMAL(
4450 					"CFDC on the adapter %s has being "
4451 					"hardened on primary and secondary SE\n",
4452 					zfcp_get_busid_by_adapter(adapter));
4453 				break;
4454 
4455 			case FSF_SQ_CFDC_COULD_NOT_HARDEN_ON_SE:
4456 				ZFCP_LOG_NORMAL(
4457 					"CFDC of the adapter %s could not "
4458 					"be saved on the SE\n",
4459 					zfcp_get_busid_by_adapter(adapter));
4460 				break;
4461 
4462 			case FSF_SQ_CFDC_COULD_NOT_HARDEN_ON_SE2:
4463 				ZFCP_LOG_NORMAL(
4464 					"CFDC of the adapter %s could not "
4465 					"be copied to the secondary SE\n",
4466 					zfcp_get_busid_by_adapter(adapter));
4467 				break;
4468 
4469 			default:
4470 				ZFCP_LOG_NORMAL(
4471 					"CFDC could not be hardened "
4472 					"on the adapter %s\n",
4473 					zfcp_get_busid_by_adapter(adapter));
4474 			}
4475 		}
4476 		fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
4477 		retval = -EAGAIN;
4478 		break;
4479 
4480 	case FSF_AUTHORIZATION_FAILURE:
4481 		ZFCP_LOG_NORMAL(
4482 			"Adapter %s does not accept privileged commands\n",
4483 			zfcp_get_busid_by_adapter(adapter));
4484 		fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
4485 		retval = -EACCES;
4486 		break;
4487 
4488 	case FSF_CFDC_ERROR_DETECTED:
4489 		ZFCP_LOG_NORMAL(
4490 			"Error at position %d in the CFDC, "
4491 			"CFDC is discarded by the adapter %s\n",
4492 			header->fsf_status_qual.word[0],
4493 			zfcp_get_busid_by_adapter(adapter));
4494 		fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
4495 		retval = -EPERM;
4496 		break;
4497 
4498 	case FSF_CONTROL_FILE_UPDATE_ERROR:
4499 		ZFCP_LOG_NORMAL(
4500 			"Adapter %s cannot harden the control file, "
4501 			"file is discarded\n",
4502 			zfcp_get_busid_by_adapter(adapter));
4503 		fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
4504 		retval = -EIO;
4505 		break;
4506 
4507 	case FSF_CONTROL_FILE_TOO_LARGE:
4508 		ZFCP_LOG_NORMAL(
4509 			"Control file is too large, file is discarded "
4510 			"by the adapter %s\n",
4511 			zfcp_get_busid_by_adapter(adapter));
4512 		fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
4513 		retval = -EIO;
4514 		break;
4515 
4516 	case FSF_ACCESS_CONFLICT_DETECTED:
4517 		if (bottom->operation_subtype == FSF_CFDC_OPERATION_SUBTYPE)
4518 			ZFCP_LOG_NORMAL(
4519 				"CFDC has been discarded by the adapter %s, "
4520 				"because activation would impact "
4521 				"%d active connection(s)\n",
4522 				zfcp_get_busid_by_adapter(adapter),
4523 				header->fsf_status_qual.word[0]);
4524 		fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
4525 		retval = -EIO;
4526 		break;
4527 
4528 	case FSF_CONFLICTS_OVERRULED:
4529 		if (bottom->operation_subtype == FSF_CFDC_OPERATION_SUBTYPE)
4530 			ZFCP_LOG_NORMAL(
4531 				"CFDC has been activated on the adapter %s, "
4532 				"but activation has impacted "
4533 				"%d active connection(s)\n",
4534 				zfcp_get_busid_by_adapter(adapter),
4535 				header->fsf_status_qual.word[0]);
4536 		fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
4537 		retval = -EIO;
4538 		break;
4539 
4540 	case FSF_UNKNOWN_OP_SUBTYPE:
4541 		ZFCP_LOG_NORMAL("unknown operation subtype (adapter: %s, "
4542 				"op_subtype=0x%x)\n",
4543 				zfcp_get_busid_by_adapter(adapter),
4544 				bottom->operation_subtype);
4545 		fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
4546 		retval = -EINVAL;
4547 		break;
4548 
4549 	case FSF_INVALID_COMMAND_OPTION:
4550 		ZFCP_LOG_NORMAL(
4551 			"Invalid option 0x%x has been specified "
4552 			"in QTCB bottom sent to the adapter %s\n",
4553 			bottom->option,
4554 			zfcp_get_busid_by_adapter(adapter));
4555 		fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
4556 		retval = -EINVAL;
4557 		break;
4558 
4559 	default:
4560 		ZFCP_LOG_NORMAL(
4561 			"bug: An unknown/unexpected FSF status 0x%08x "
4562 			"was presented on the adapter %s\n",
4563 			header->fsf_status,
4564 			zfcp_get_busid_by_adapter(adapter));
4565 		debug_text_event(fsf_req->adapter->erp_dbf, 0, "fsf_sq_inval");
4566 		debug_exception(fsf_req->adapter->erp_dbf, 0,
4567 			&header->fsf_status_qual.word[0], sizeof(u32));
4568 		fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
4569 		retval = -EINVAL;
4570 		break;
4571 	}
4572 
4573 skip_fsfstatus:
4574 	return retval;
4575 }
4576 
4577 static inline int
4578 zfcp_fsf_req_sbal_check(unsigned long *flags,
4579 			struct zfcp_qdio_queue *queue, int needed)
4580 {
4581 	write_lock_irqsave(&queue->queue_lock, *flags);
4582 	if (likely(atomic_read(&queue->free_count) >= needed))
4583 		return 1;
4584 	write_unlock_irqrestore(&queue->queue_lock, *flags);
4585 	return 0;
4586 }
4587 
4588 /*
4589  * set qtcb pointer in fsf_req and initialize QTCB
4590  */
4591 static inline void
4592 zfcp_fsf_req_qtcb_init(struct zfcp_fsf_req *fsf_req)
4593 {
4594 	if (likely(fsf_req->qtcb != NULL)) {
4595 		fsf_req->qtcb->prefix.req_seq_no = fsf_req->adapter->fsf_req_seq_no;
4596 		fsf_req->qtcb->prefix.req_id = (unsigned long)fsf_req;
4597 		fsf_req->qtcb->prefix.ulp_info = ZFCP_ULP_INFO_VERSION;
4598 		fsf_req->qtcb->prefix.qtcb_type = fsf_qtcb_type[fsf_req->fsf_command];
4599 		fsf_req->qtcb->prefix.qtcb_version = ZFCP_QTCB_VERSION;
4600 		fsf_req->qtcb->header.req_handle = (unsigned long)fsf_req;
4601 		fsf_req->qtcb->header.fsf_command = fsf_req->fsf_command;
4602 	}
4603 }
4604 
4605 /**
4606  * zfcp_fsf_req_sbal_get - try to get one SBAL in the request queue
4607  * @adapter: adapter for which request queue is examined
4608  * @req_flags: flags indicating whether to wait for needed SBAL or not
4609  * @lock_flags: lock_flags if queue_lock is taken
4610  * Return: 0 on success, otherwise -EIO, or -ERESTARTSYS
4611  * Locks: lock adapter->request_queue->queue_lock on success
4612  */
4613 static int
4614 zfcp_fsf_req_sbal_get(struct zfcp_adapter *adapter, int req_flags,
4615 		      unsigned long *lock_flags)
4616 {
4617         long ret;
4618         struct zfcp_qdio_queue *req_queue = &adapter->request_queue;
4619 
4620         if (unlikely(req_flags & ZFCP_WAIT_FOR_SBAL)) {
4621                 ret = wait_event_interruptible_timeout(adapter->request_wq,
4622 			zfcp_fsf_req_sbal_check(lock_flags, req_queue, 1),
4623 						       ZFCP_SBAL_TIMEOUT);
4624 		if (ret < 0)
4625 			return ret;
4626 		if (!ret)
4627 			return -EIO;
4628         } else if (!zfcp_fsf_req_sbal_check(lock_flags, req_queue, 1))
4629                 return -EIO;
4630 
4631         return 0;
4632 }
4633 
4634 /*
4635  * function:    zfcp_fsf_req_create
4636  *
4637  * purpose:	create an FSF request at the specified adapter and
4638  *		setup common fields
4639  *
4640  * returns:	-ENOMEM if there was insufficient memory for a request
4641  *              -EIO if no qdio buffers could be allocate to the request
4642  *              -EINVAL/-EPERM on bug conditions in req_dequeue
4643  *              0 in success
4644  *
4645  * note:        The created request is returned by reference.
4646  *
4647  * locks:	lock of concerned request queue must not be held,
4648  *		but is held on completion (write, irqsave)
4649  */
4650 int
4651 zfcp_fsf_req_create(struct zfcp_adapter *adapter, u32 fsf_cmd, int req_flags,
4652 		    mempool_t *pool, unsigned long *lock_flags,
4653 		    struct zfcp_fsf_req **fsf_req_p)
4654 {
4655 	volatile struct qdio_buffer_element *sbale;
4656 	struct zfcp_fsf_req *fsf_req = NULL;
4657 	int ret = 0;
4658 	struct zfcp_qdio_queue *req_queue = &adapter->request_queue;
4659 
4660 	/* allocate new FSF request */
4661 	fsf_req = zfcp_fsf_req_alloc(pool, req_flags);
4662 	if (unlikely(NULL == fsf_req)) {
4663 		ZFCP_LOG_DEBUG("error: Could not put an FSF request into"
4664 			       "the outbound (send) queue.\n");
4665 		ret = -ENOMEM;
4666 		goto failed_fsf_req;
4667 	}
4668 
4669 	fsf_req->adapter = adapter;
4670 	fsf_req->fsf_command = fsf_cmd;
4671 
4672         zfcp_fsf_req_qtcb_init(fsf_req);
4673 
4674 	/* initialize waitqueue which may be used to wait on
4675 	   this request completion */
4676 	init_waitqueue_head(&fsf_req->completion_wq);
4677 
4678         ret = zfcp_fsf_req_sbal_get(adapter, req_flags, lock_flags);
4679         if(ret < 0) {
4680                 goto failed_sbals;
4681 	}
4682 
4683 	/*
4684 	 * We hold queue_lock here. Check if QDIOUP is set and let request fail
4685 	 * if it is not set (see also *_open_qdio and *_close_qdio).
4686 	 */
4687 
4688 	if (!atomic_test_mask(ZFCP_STATUS_ADAPTER_QDIOUP, &adapter->status)) {
4689 		write_unlock_irqrestore(&req_queue->queue_lock, *lock_flags);
4690 		ret = -EIO;
4691 		goto failed_sbals;
4692 	}
4693 
4694 	if (fsf_req->qtcb) {
4695 		fsf_req->seq_no = adapter->fsf_req_seq_no;
4696 		fsf_req->qtcb->prefix.req_seq_no = adapter->fsf_req_seq_no;
4697 	}
4698 	fsf_req->sbal_number = 1;
4699 	fsf_req->sbal_first = req_queue->free_index;
4700 	fsf_req->sbal_curr = req_queue->free_index;
4701         fsf_req->sbale_curr = 1;
4702 
4703 	if (likely(req_flags & ZFCP_REQ_AUTO_CLEANUP)) {
4704 		fsf_req->status |= ZFCP_STATUS_FSFREQ_CLEANUP;
4705 	}
4706 
4707 	sbale = zfcp_qdio_sbale_req(fsf_req, fsf_req->sbal_curr, 0);
4708 
4709 	/* setup common SBALE fields */
4710 	sbale[0].addr = fsf_req;
4711 	sbale[0].flags |= SBAL_FLAGS0_COMMAND;
4712 	if (likely(fsf_req->qtcb != NULL)) {
4713 		sbale[1].addr = (void *) fsf_req->qtcb;
4714 		sbale[1].length = sizeof(struct fsf_qtcb);
4715 	}
4716 
4717 	ZFCP_LOG_TRACE("got %i free BUFFERs starting at index %i\n",
4718                        fsf_req->sbal_number, fsf_req->sbal_first);
4719 
4720 	goto success;
4721 
4722  failed_sbals:
4723 /* dequeue new FSF request previously enqueued */
4724 	zfcp_fsf_req_free(fsf_req);
4725 	fsf_req = NULL;
4726 
4727  failed_fsf_req:
4728 	write_lock_irqsave(&req_queue->queue_lock, *lock_flags);
4729  success:
4730 	*fsf_req_p = fsf_req;
4731 	return ret;
4732 }
4733 
4734 /*
4735  * function:    zfcp_fsf_req_send
4736  *
4737  * purpose:	start transfer of FSF request via QDIO
4738  *
4739  * returns:	0 - request transfer succesfully started
4740  *		!0 - start of request transfer failed
4741  */
4742 static int
4743 zfcp_fsf_req_send(struct zfcp_fsf_req *fsf_req, struct timer_list *timer)
4744 {
4745 	struct zfcp_adapter *adapter;
4746 	struct zfcp_qdio_queue *req_queue;
4747 	volatile struct qdio_buffer_element *sbale;
4748 	int inc_seq_no;
4749 	int new_distance_from_int;
4750 	unsigned long flags;
4751 	int retval = 0;
4752 
4753 	adapter = fsf_req->adapter;
4754 	req_queue = &adapter->request_queue,
4755 
4756 
4757 	/* FIXME(debug): remove it later */
4758 	sbale = zfcp_qdio_sbale_req(fsf_req, fsf_req->sbal_first, 0);
4759 	ZFCP_LOG_DEBUG("SBALE0 flags=0x%x\n", sbale[0].flags);
4760 	ZFCP_LOG_TRACE("HEX DUMP OF SBALE1 PAYLOAD:\n");
4761 	ZFCP_HEX_DUMP(ZFCP_LOG_LEVEL_TRACE, (char *) sbale[1].addr,
4762 		      sbale[1].length);
4763 
4764 	/* put allocated FSF request at list tail */
4765 	spin_lock_irqsave(&adapter->fsf_req_list_lock, flags);
4766 	list_add_tail(&fsf_req->list, &adapter->fsf_req_list_head);
4767 	spin_unlock_irqrestore(&adapter->fsf_req_list_lock, flags);
4768 
4769 	inc_seq_no = (fsf_req->qtcb != NULL);
4770 
4771 	/* figure out expiration time of timeout and start timeout */
4772 	if (unlikely(timer)) {
4773 		timer->expires += jiffies;
4774 		add_timer(timer);
4775 	}
4776 
4777 	ZFCP_LOG_TRACE("request queue of adapter %s: "
4778 		       "next free SBAL is %i, %i free SBALs\n",
4779 		       zfcp_get_busid_by_adapter(adapter),
4780 		       req_queue->free_index,
4781 		       atomic_read(&req_queue->free_count));
4782 
4783 	ZFCP_LOG_DEBUG("calling do_QDIO adapter %s, flags=0x%x, queue_no=%i, "
4784 		       "index_in_queue=%i, count=%i, buffers=%p\n",
4785 		       zfcp_get_busid_by_adapter(adapter),
4786 		       QDIO_FLAG_SYNC_OUTPUT,
4787 		       0, fsf_req->sbal_first, fsf_req->sbal_number,
4788 		       &req_queue->buffer[fsf_req->sbal_first]);
4789 
4790 	/*
4791 	 * adjust the number of free SBALs in request queue as well as
4792 	 * position of first one
4793 	 */
4794 	atomic_sub(fsf_req->sbal_number, &req_queue->free_count);
4795 	ZFCP_LOG_TRACE("free_count=%d\n", atomic_read(&req_queue->free_count));
4796 	req_queue->free_index += fsf_req->sbal_number;	  /* increase */
4797 	req_queue->free_index %= QDIO_MAX_BUFFERS_PER_Q;  /* wrap if needed */
4798 	new_distance_from_int = zfcp_qdio_determine_pci(req_queue, fsf_req);
4799 
4800 	fsf_req->issued = get_clock();
4801 
4802 	retval = do_QDIO(adapter->ccw_device,
4803 			 QDIO_FLAG_SYNC_OUTPUT,
4804 			 0, fsf_req->sbal_first, fsf_req->sbal_number, NULL);
4805 
4806 	if (unlikely(retval)) {
4807 		/* Queues are down..... */
4808 		retval = -EIO;
4809 		/*
4810 		 * FIXME(potential race):
4811 		 * timer might be expired (absolutely unlikely)
4812 		 */
4813 		if (timer)
4814 			del_timer(timer);
4815 		spin_lock_irqsave(&adapter->fsf_req_list_lock, flags);
4816 		list_del(&fsf_req->list);
4817 		spin_unlock_irqrestore(&adapter->fsf_req_list_lock, flags);
4818 		/*
4819 		 * adjust the number of free SBALs in request queue as well as
4820 		 * position of first one
4821 		 */
4822 		zfcp_qdio_zero_sbals(req_queue->buffer,
4823 				     fsf_req->sbal_first, fsf_req->sbal_number);
4824 		atomic_add(fsf_req->sbal_number, &req_queue->free_count);
4825 		req_queue->free_index -= fsf_req->sbal_number;	 /* increase */
4826 		req_queue->free_index += QDIO_MAX_BUFFERS_PER_Q;
4827 		req_queue->free_index %= QDIO_MAX_BUFFERS_PER_Q; /* wrap */
4828 		ZFCP_LOG_DEBUG
4829 			("error: do_QDIO failed. Buffers could not be enqueued "
4830 			 "to request queue.\n");
4831 	} else {
4832 		req_queue->distance_from_int = new_distance_from_int;
4833 		/*
4834 		 * increase FSF sequence counter -
4835 		 * this must only be done for request successfully enqueued to
4836 		 * QDIO this rejected requests may be cleaned up by calling
4837 		 * routines  resulting in missing sequence counter values
4838 		 * otherwise,
4839 		 */
4840 
4841 		/* Don't increase for unsolicited status */
4842 		if (inc_seq_no)
4843 			adapter->fsf_req_seq_no++;
4844 
4845 		/* count FSF requests pending */
4846 		atomic_inc(&adapter->fsf_reqs_active);
4847 	}
4848 	return retval;
4849 }
4850 
4851 #undef ZFCP_LOG_AREA
4852