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