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