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