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