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