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