]> Pileus Git - ~andy/linux/blob - drivers/s390/scsi/zfcp_fsf.c
[SCSI] zfcp: fix: problem in send_els_handler when D_ID assignment changes
[~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                         zfcp_test_link(unit->port);
1271                         new_fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1272                         break;
1273                 case FSF_SQ_ULP_DEPENDENT_ERP_REQUIRED:
1274                         /* SCSI stack will escalate */
1275                         debug_text_event(new_fsf_req->adapter->erp_dbf, 1,
1276                                          "fsf_sq_ulp");
1277                         new_fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1278                         break;
1279                 default:
1280                         ZFCP_LOG_NORMAL
1281                             ("bug: Wrong status qualifier 0x%x arrived.\n",
1282                              new_fsf_req->qtcb->header.fsf_status_qual.word[0]);
1283                         debug_text_event(new_fsf_req->adapter->erp_dbf, 0,
1284                                          "fsf_sq_inval:");
1285                         debug_exception(new_fsf_req->adapter->erp_dbf, 0,
1286                                         &new_fsf_req->qtcb->header.
1287                                         fsf_status_qual.word[0], sizeof (u32));
1288                         break;
1289                 }
1290                 break;
1291
1292         case FSF_GOOD:
1293                 retval = 0;
1294                 new_fsf_req->status |= ZFCP_STATUS_FSFREQ_ABORTSUCCEEDED;
1295                 break;
1296
1297         default:
1298                 ZFCP_LOG_NORMAL("bug: An unknown FSF Status was presented "
1299                                 "(debug info 0x%x)\n",
1300                                 new_fsf_req->qtcb->header.fsf_status);
1301                 debug_text_event(new_fsf_req->adapter->erp_dbf, 0,
1302                                  "fsf_s_inval:");
1303                 debug_exception(new_fsf_req->adapter->erp_dbf, 0,
1304                                 &new_fsf_req->qtcb->header.fsf_status,
1305                                 sizeof (u32));
1306                 break;
1307         }
1308  skip_fsfstatus:
1309         return retval;
1310 }
1311
1312 /**
1313  * zfcp_use_one_sbal - checks whether req buffer and resp bother each fit into
1314  *      one SBALE
1315  * Two scatter-gather lists are passed, one for the reqeust and one for the
1316  * response.
1317  */
1318 static inline int
1319 zfcp_use_one_sbal(struct scatterlist *req, int req_count,
1320                   struct scatterlist *resp, int resp_count)
1321 {
1322         return ((req_count == 1) &&
1323                 (resp_count == 1) &&
1324                 (((unsigned long) zfcp_sg_to_address(&req[0]) &
1325                   PAGE_MASK) ==
1326                  ((unsigned long) (zfcp_sg_to_address(&req[0]) +
1327                                    req[0].length - 1) & PAGE_MASK)) &&
1328                 (((unsigned long) zfcp_sg_to_address(&resp[0]) &
1329                   PAGE_MASK) ==
1330                  ((unsigned long) (zfcp_sg_to_address(&resp[0]) +
1331                                    resp[0].length - 1) & PAGE_MASK)));
1332 }
1333
1334 /**
1335  * zfcp_fsf_send_ct - initiate a Generic Service request (FC-GS)
1336  * @ct: pointer to struct zfcp_send_ct which conatins all needed data for
1337  *      the request
1338  * @pool: pointer to memory pool, if non-null this pool is used to allocate
1339  *      a struct zfcp_fsf_req
1340  * @erp_action: pointer to erp_action, if non-null the Generic Service request
1341  *      is sent within error recovery
1342  */
1343 int
1344 zfcp_fsf_send_ct(struct zfcp_send_ct *ct, mempool_t *pool,
1345                  struct zfcp_erp_action *erp_action)
1346 {
1347         volatile struct qdio_buffer_element *sbale;
1348         struct zfcp_port *port;
1349         struct zfcp_adapter *adapter;
1350         struct zfcp_fsf_req *fsf_req;
1351         unsigned long lock_flags;
1352         int bytes;
1353         int ret = 0;
1354
1355         port = ct->port;
1356         adapter = port->adapter;
1357
1358         ret = zfcp_fsf_req_create(adapter, FSF_QTCB_SEND_GENERIC,
1359                                   ZFCP_WAIT_FOR_SBAL | ZFCP_REQ_AUTO_CLEANUP,
1360                                   pool, &lock_flags, &fsf_req);
1361         if (ret < 0) {
1362                 ZFCP_LOG_INFO("error: Could not create CT request (FC-GS) for "
1363                               "adapter: %s\n",
1364                               zfcp_get_busid_by_adapter(adapter));
1365                 goto failed_req;
1366         }
1367
1368         if (erp_action != NULL) {
1369                 erp_action->fsf_req = fsf_req;
1370                 fsf_req->erp_action = erp_action;
1371         }
1372
1373         sbale = zfcp_qdio_sbale_req(fsf_req, fsf_req->sbal_curr, 0);
1374         if (zfcp_use_one_sbal(ct->req, ct->req_count,
1375                               ct->resp, ct->resp_count)){
1376                 /* both request buffer and response buffer
1377                    fit into one sbale each */
1378                 sbale[0].flags |= SBAL_FLAGS0_TYPE_WRITE_READ;
1379                 sbale[2].addr = zfcp_sg_to_address(&ct->req[0]);
1380                 sbale[2].length = ct->req[0].length;
1381                 sbale[3].addr = zfcp_sg_to_address(&ct->resp[0]);
1382                 sbale[3].length = ct->resp[0].length;
1383                 sbale[3].flags |= SBAL_FLAGS_LAST_ENTRY;
1384         } else if (adapter->supported_features &
1385                    FSF_FEATURE_ELS_CT_CHAINED_SBALS) {
1386                 /* try to use chained SBALs */
1387                 bytes = zfcp_qdio_sbals_from_sg(fsf_req,
1388                                                 SBAL_FLAGS0_TYPE_WRITE_READ,
1389                                                 ct->req, ct->req_count,
1390                                                 ZFCP_MAX_SBALS_PER_CT_REQ);
1391                 if (bytes <= 0) {
1392                         ZFCP_LOG_INFO("error: creation of CT request failed "
1393                                       "on adapter %s\n",
1394                                       zfcp_get_busid_by_adapter(adapter));
1395                         if (bytes == 0)
1396                                 ret = -ENOMEM;
1397                         else
1398                                 ret = bytes;
1399
1400                         goto failed_send;
1401                 }
1402                 fsf_req->qtcb->bottom.support.req_buf_length = bytes;
1403                 fsf_req->sbale_curr = ZFCP_LAST_SBALE_PER_SBAL;
1404                 bytes = zfcp_qdio_sbals_from_sg(fsf_req,
1405                                                 SBAL_FLAGS0_TYPE_WRITE_READ,
1406                                                 ct->resp, ct->resp_count,
1407                                                 ZFCP_MAX_SBALS_PER_CT_REQ);
1408                 if (bytes <= 0) {
1409                         ZFCP_LOG_INFO("error: creation of CT request failed "
1410                                       "on adapter %s\n",
1411                                       zfcp_get_busid_by_adapter(adapter));
1412                         if (bytes == 0)
1413                                 ret = -ENOMEM;
1414                         else
1415                                 ret = bytes;
1416
1417                         goto failed_send;
1418                 }
1419                 fsf_req->qtcb->bottom.support.resp_buf_length = bytes;
1420         } else {
1421                 /* reject send generic request */
1422                 ZFCP_LOG_INFO(
1423                         "error: microcode does not support chained SBALs,"
1424                         "CT request too big (adapter %s)\n",
1425                         zfcp_get_busid_by_adapter(adapter));
1426                 ret = -EOPNOTSUPP;
1427                 goto failed_send;
1428         }
1429
1430         /* settings in QTCB */
1431         fsf_req->qtcb->header.port_handle = port->handle;
1432         fsf_req->qtcb->bottom.support.service_class = adapter->fc_service_class;
1433         fsf_req->qtcb->bottom.support.timeout = ct->timeout;
1434         fsf_req->data.send_ct = ct;
1435
1436         /* start QDIO request for this FSF request */
1437         ret = zfcp_fsf_req_send(fsf_req, ct->timer);
1438         if (ret) {
1439                 ZFCP_LOG_DEBUG("error: initiation of CT request failed "
1440                                "(adapter %s, port 0x%016Lx)\n",
1441                                zfcp_get_busid_by_adapter(adapter), port->wwpn);
1442                 goto failed_send;
1443         }
1444
1445         ZFCP_LOG_DEBUG("CT request initiated (adapter %s, port 0x%016Lx)\n",
1446                        zfcp_get_busid_by_adapter(adapter), port->wwpn);
1447         goto out;
1448
1449  failed_send:
1450         zfcp_fsf_req_free(fsf_req);
1451         if (erp_action != NULL) {
1452                 erp_action->fsf_req = NULL;
1453         }
1454  failed_req:
1455  out:
1456         write_unlock_irqrestore(&adapter->request_queue.queue_lock,
1457                                 lock_flags);
1458         return ret;
1459 }
1460
1461 /**
1462  * zfcp_fsf_send_ct_handler - handler for Generic Service requests
1463  * @fsf_req: pointer to struct zfcp_fsf_req
1464  *
1465  * Data specific for the Generic Service request is passed by
1466  * fsf_req->data.send_ct
1467  * Usually a specific handler for the request is called via
1468  * fsf_req->data.send_ct->handler at end of this function.
1469  */
1470 static int
1471 zfcp_fsf_send_ct_handler(struct zfcp_fsf_req *fsf_req)
1472 {
1473         struct zfcp_port *port;
1474         struct zfcp_adapter *adapter;
1475         struct zfcp_send_ct *send_ct;
1476         struct fsf_qtcb_header *header;
1477         struct fsf_qtcb_bottom_support *bottom;
1478         int retval = -EINVAL;
1479         u16 subtable, rule, counter;
1480
1481         adapter = fsf_req->adapter;
1482         send_ct = fsf_req->data.send_ct;
1483         port = send_ct->port;
1484         header = &fsf_req->qtcb->header;
1485         bottom = &fsf_req->qtcb->bottom.support;
1486
1487         if (fsf_req->status & ZFCP_STATUS_FSFREQ_ERROR)
1488                 goto skip_fsfstatus;
1489
1490         /* evaluate FSF status in QTCB */
1491         switch (header->fsf_status) {
1492
1493         case FSF_GOOD:
1494                 retval = 0;
1495                 break;
1496
1497         case FSF_SERVICE_CLASS_NOT_SUPPORTED:
1498                 if (adapter->fc_service_class <= 3) {
1499                         ZFCP_LOG_INFO("error: adapter %s does not support fc "
1500                                       "class %d.\n",
1501                                       zfcp_get_busid_by_port(port),
1502                                       adapter->fc_service_class);
1503                 } else {
1504                         ZFCP_LOG_INFO("bug: The fibre channel class at the "
1505                                       "adapter %s is invalid. "
1506                                       "(debug info %d)\n",
1507                                       zfcp_get_busid_by_port(port),
1508                                       adapter->fc_service_class);
1509                 }
1510                 /* stop operation for this adapter */
1511                 debug_text_exception(adapter->erp_dbf, 0, "fsf_s_class_nsup");
1512                 zfcp_erp_adapter_shutdown(adapter, 0);
1513                 fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1514                 break;
1515
1516         case FSF_ADAPTER_STATUS_AVAILABLE:
1517                 switch (header->fsf_status_qual.word[0]){
1518                 case FSF_SQ_INVOKE_LINK_TEST_PROCEDURE:
1519                         /* reopening link to port */
1520                         debug_text_event(adapter->erp_dbf, 1, "fsf_sq_ltest");
1521                         zfcp_test_link(port);
1522                         fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1523                         break;
1524                 case FSF_SQ_ULP_DEPENDENT_ERP_REQUIRED:
1525                         /* ERP strategy will escalate */
1526                         debug_text_event(adapter->erp_dbf, 1, "fsf_sq_ulp");
1527                         fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1528                         break;
1529                 default:
1530                         ZFCP_LOG_INFO("bug: Wrong status qualifier 0x%x "
1531                                       "arrived.\n",
1532                                       header->fsf_status_qual.word[0]);
1533                         break;
1534                 }
1535                 break;
1536
1537         case FSF_ACCESS_DENIED:
1538                 ZFCP_LOG_NORMAL("access denied, cannot send generic service "
1539                                 "command (adapter %s, port d_id=0x%08x)\n",
1540                                 zfcp_get_busid_by_port(port), port->d_id);
1541                 for (counter = 0; counter < 2; counter++) {
1542                         subtable = header->fsf_status_qual.halfword[counter * 2];
1543                         rule = header->fsf_status_qual.halfword[counter * 2 + 1];
1544                         switch (subtable) {
1545                         case FSF_SQ_CFDC_SUBTABLE_OS:
1546                         case FSF_SQ_CFDC_SUBTABLE_PORT_WWPN:
1547                         case FSF_SQ_CFDC_SUBTABLE_PORT_DID:
1548                         case FSF_SQ_CFDC_SUBTABLE_LUN:
1549                                 ZFCP_LOG_INFO("Access denied (%s rule %d)\n",
1550                                         zfcp_act_subtable_type[subtable], rule);
1551                                 break;
1552                         }
1553                 }
1554                 debug_text_event(adapter->erp_dbf, 1, "fsf_s_access");
1555                 zfcp_erp_port_access_denied(port);
1556                 fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1557                 break;
1558
1559         case FSF_GENERIC_COMMAND_REJECTED:
1560                 ZFCP_LOG_INFO("generic service command rejected "
1561                               "(adapter %s, port d_id=0x%08x)\n",
1562                               zfcp_get_busid_by_port(port), port->d_id);
1563                 ZFCP_LOG_INFO("status qualifier:\n");
1564                 ZFCP_HEX_DUMP(ZFCP_LOG_LEVEL_INFO,
1565                               (char *) &header->fsf_status_qual,
1566                               sizeof (union fsf_status_qual));
1567                 debug_text_event(adapter->erp_dbf, 1, "fsf_s_gcom_rej");
1568                 fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1569                 break;
1570
1571         case FSF_PORT_HANDLE_NOT_VALID:
1572                 ZFCP_LOG_DEBUG("Temporary port identifier 0x%x for port "
1573                                "0x%016Lx on adapter %s invalid. This may "
1574                                "happen occasionally.\n", port->handle,
1575                                port->wwpn, zfcp_get_busid_by_port(port));
1576                 ZFCP_LOG_INFO("status qualifier:\n");
1577                 ZFCP_HEX_DUMP(ZFCP_LOG_LEVEL_INFO,
1578                               (char *) &header->fsf_status_qual,
1579                               sizeof (union fsf_status_qual));
1580                 debug_text_event(adapter->erp_dbf, 1, "fsf_s_phandle_nv");
1581                 zfcp_erp_adapter_reopen(adapter, 0);
1582                 fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1583                 break;
1584
1585         case FSF_PORT_BOXED:
1586                 ZFCP_LOG_INFO("port needs to be reopened "
1587                               "(adapter %s, port d_id=0x%08x)\n",
1588                               zfcp_get_busid_by_port(port), port->d_id);
1589                 debug_text_event(adapter->erp_dbf, 2, "fsf_s_pboxed");
1590                 zfcp_erp_port_reopen(port, 0);
1591                 fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR
1592                     | ZFCP_STATUS_FSFREQ_RETRY;
1593                 break;
1594
1595         /* following states should never occure, all cases avoided
1596            in zfcp_fsf_send_ct - but who knows ... */
1597         case FSF_PAYLOAD_SIZE_MISMATCH:
1598                 ZFCP_LOG_INFO("payload size mismatch (adapter: %s, "
1599                               "req_buf_length=%d, resp_buf_length=%d)\n",
1600                               zfcp_get_busid_by_adapter(adapter),
1601                               bottom->req_buf_length, bottom->resp_buf_length);
1602                 fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1603                 break;
1604         case FSF_REQUEST_SIZE_TOO_LARGE:
1605                 ZFCP_LOG_INFO("request size too large (adapter: %s, "
1606                               "req_buf_length=%d)\n",
1607                               zfcp_get_busid_by_adapter(adapter),
1608                               bottom->req_buf_length);
1609                 fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1610                 break;
1611         case FSF_RESPONSE_SIZE_TOO_LARGE:
1612                 ZFCP_LOG_INFO("response size too large (adapter: %s, "
1613                               "resp_buf_length=%d)\n",
1614                               zfcp_get_busid_by_adapter(adapter),
1615                               bottom->resp_buf_length);
1616                 fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1617                 break;
1618         case FSF_SBAL_MISMATCH:
1619                 ZFCP_LOG_INFO("SBAL mismatch (adapter: %s, req_buf_length=%d, "
1620                               "resp_buf_length=%d)\n",
1621                               zfcp_get_busid_by_adapter(adapter),
1622                               bottom->req_buf_length, bottom->resp_buf_length);
1623                 fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1624                 break;
1625
1626        default:
1627                 ZFCP_LOG_NORMAL("bug: An unknown FSF Status was presented "
1628                                 "(debug info 0x%x)\n", header->fsf_status);
1629                 debug_text_event(adapter->erp_dbf, 0, "fsf_sq_inval:");
1630                 debug_exception(adapter->erp_dbf, 0,
1631                                 &header->fsf_status_qual.word[0], sizeof (u32));
1632                 break;
1633         }
1634
1635 skip_fsfstatus:
1636         send_ct->status = retval;
1637
1638         if (send_ct->handler != NULL)
1639                 send_ct->handler(send_ct->handler_data);
1640
1641         return retval;
1642 }
1643
1644 /**
1645  * zfcp_fsf_send_els - initiate an ELS command (FC-FS)
1646  * @els: pointer to struct zfcp_send_els which contains all needed data for
1647  *      the command.
1648  */
1649 int
1650 zfcp_fsf_send_els(struct zfcp_send_els *els)
1651 {
1652         volatile struct qdio_buffer_element *sbale;
1653         struct zfcp_fsf_req *fsf_req;
1654         fc_id_t d_id;
1655         struct zfcp_adapter *adapter;
1656         unsigned long lock_flags;
1657         int bytes;
1658         int ret = 0;
1659
1660         d_id = els->d_id;
1661         adapter = els->adapter;
1662
1663         ret = zfcp_fsf_req_create(adapter, FSF_QTCB_SEND_ELS,
1664                                   ZFCP_REQ_AUTO_CLEANUP,
1665                                   NULL, &lock_flags, &fsf_req);
1666         if (ret < 0) {
1667                 ZFCP_LOG_INFO("error: creation of ELS request failed "
1668                               "(adapter %s, port d_id: 0x%08x)\n",
1669                               zfcp_get_busid_by_adapter(adapter), d_id);
1670                 goto failed_req;
1671         }
1672
1673         sbale = zfcp_qdio_sbale_req(fsf_req, fsf_req->sbal_curr, 0);
1674         if (zfcp_use_one_sbal(els->req, els->req_count,
1675                               els->resp, els->resp_count)){
1676                 /* both request buffer and response buffer
1677                    fit into one sbale each */
1678                 sbale[0].flags |= SBAL_FLAGS0_TYPE_WRITE_READ;
1679                 sbale[2].addr = zfcp_sg_to_address(&els->req[0]);
1680                 sbale[2].length = els->req[0].length;
1681                 sbale[3].addr = zfcp_sg_to_address(&els->resp[0]);
1682                 sbale[3].length = els->resp[0].length;
1683                 sbale[3].flags |= SBAL_FLAGS_LAST_ENTRY;
1684         } else if (adapter->supported_features &
1685                    FSF_FEATURE_ELS_CT_CHAINED_SBALS) {
1686                 /* try to use chained SBALs */
1687                 bytes = zfcp_qdio_sbals_from_sg(fsf_req,
1688                                                 SBAL_FLAGS0_TYPE_WRITE_READ,
1689                                                 els->req, els->req_count,
1690                                                 ZFCP_MAX_SBALS_PER_ELS_REQ);
1691                 if (bytes <= 0) {
1692                         ZFCP_LOG_INFO("error: creation of ELS request failed "
1693                                       "(adapter %s, port d_id: 0x%08x)\n",
1694                                       zfcp_get_busid_by_adapter(adapter), d_id);
1695                         if (bytes == 0) {
1696                                 ret = -ENOMEM;
1697                         } else {
1698                                 ret = bytes;
1699                         }
1700                         goto failed_send;
1701                 }
1702                 fsf_req->qtcb->bottom.support.req_buf_length = bytes;
1703                 fsf_req->sbale_curr = ZFCP_LAST_SBALE_PER_SBAL;
1704                 bytes = zfcp_qdio_sbals_from_sg(fsf_req,
1705                                                 SBAL_FLAGS0_TYPE_WRITE_READ,
1706                                                 els->resp, els->resp_count,
1707                                                 ZFCP_MAX_SBALS_PER_ELS_REQ);
1708                 if (bytes <= 0) {
1709                         ZFCP_LOG_INFO("error: creation of ELS request failed "
1710                                       "(adapter %s, port d_id: 0x%08x)\n",
1711                                       zfcp_get_busid_by_adapter(adapter), d_id);
1712                         if (bytes == 0) {
1713                                 ret = -ENOMEM;
1714                         } else {
1715                                 ret = bytes;
1716                         }
1717                         goto failed_send;
1718                 }
1719                 fsf_req->qtcb->bottom.support.resp_buf_length = bytes;
1720         } else {
1721                 /* reject request */
1722                 ZFCP_LOG_INFO("error: microcode does not support chained SBALs"
1723                               ", ELS request too big (adapter %s, "
1724                               "port d_id: 0x%08x)\n",
1725                               zfcp_get_busid_by_adapter(adapter), d_id);
1726                 ret = -EOPNOTSUPP;
1727                 goto failed_send;
1728         }
1729
1730         /* settings in QTCB */
1731         fsf_req->qtcb->bottom.support.d_id = d_id;
1732         fsf_req->qtcb->bottom.support.service_class = adapter->fc_service_class;
1733         fsf_req->qtcb->bottom.support.timeout = ZFCP_ELS_TIMEOUT;
1734         fsf_req->data.send_els = els;
1735
1736         sbale = zfcp_qdio_sbale_req(fsf_req, fsf_req->sbal_curr, 0);
1737
1738         /* start QDIO request for this FSF request */
1739         ret = zfcp_fsf_req_send(fsf_req, els->timer);
1740         if (ret) {
1741                 ZFCP_LOG_DEBUG("error: initiation of ELS request failed "
1742                                "(adapter %s, port d_id: 0x%08x)\n",
1743                                zfcp_get_busid_by_adapter(adapter), d_id);
1744                 goto failed_send;
1745         }
1746
1747         ZFCP_LOG_DEBUG("ELS request initiated (adapter %s, port d_id: "
1748                        "0x%08x)\n", zfcp_get_busid_by_adapter(adapter), d_id);
1749         goto out;
1750
1751  failed_send:
1752         zfcp_fsf_req_free(fsf_req);
1753
1754  failed_req:
1755  out:
1756         write_unlock_irqrestore(&adapter->request_queue.queue_lock,
1757                                 lock_flags);
1758
1759         return ret;
1760 }
1761
1762 /**
1763  * zfcp_fsf_send_els_handler - handler for ELS commands
1764  * @fsf_req: pointer to struct zfcp_fsf_req
1765  *
1766  * Data specific for the ELS command is passed by
1767  * fsf_req->data.send_els
1768  * Usually a specific handler for the command is called via
1769  * fsf_req->data.send_els->handler at end of this function.
1770  */
1771 static int zfcp_fsf_send_els_handler(struct zfcp_fsf_req *fsf_req)
1772 {
1773         struct zfcp_adapter *adapter;
1774         struct zfcp_port *port;
1775         fc_id_t d_id;
1776         struct fsf_qtcb_header *header;
1777         struct fsf_qtcb_bottom_support *bottom;
1778         struct zfcp_send_els *send_els;
1779         int retval = -EINVAL;
1780         u16 subtable, rule, counter;
1781
1782         send_els = fsf_req->data.send_els;
1783         adapter = send_els->adapter;
1784         port = send_els->port;
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 (port && (send_els->ls_code != ZFCP_LS_ADISC))
1822                                 zfcp_test_link(port);
1823                         fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1824                         break;
1825                 case FSF_SQ_ULP_DEPENDENT_ERP_REQUIRED:
1826                         debug_text_event(adapter->erp_dbf, 1, "fsf_sq_ulp");
1827                         fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1828                         retval =
1829                           zfcp_handle_els_rjt(header->fsf_status_qual.word[1],
1830                                               (struct zfcp_ls_rjt_par *)
1831                                               &header->fsf_status_qual.word[2]);
1832                         break;
1833                 case FSF_SQ_RETRY_IF_POSSIBLE:
1834                         debug_text_event(adapter->erp_dbf, 1, "fsf_sq_retry");
1835                         fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1836                         break;
1837                 default:
1838                         ZFCP_LOG_INFO("bug: Wrong status qualifier 0x%x\n",
1839                                       header->fsf_status_qual.word[0]);
1840                         ZFCP_HEX_DUMP(ZFCP_LOG_LEVEL_INFO,
1841                                 (char*)header->fsf_status_qual.word, 16);
1842                 }
1843                 break;
1844
1845         case FSF_ELS_COMMAND_REJECTED:
1846                 ZFCP_LOG_INFO("ELS has been rejected because command filter "
1847                               "prohibited sending "
1848                               "(adapter: %s, port d_id: 0x%08x)\n",
1849                               zfcp_get_busid_by_adapter(adapter), d_id);
1850
1851                 break;
1852
1853         case FSF_PAYLOAD_SIZE_MISMATCH:
1854                 ZFCP_LOG_INFO(
1855                         "ELS request size and ELS response size must be either "
1856                         "both 0, or both greater than 0 "
1857                         "(adapter: %s, req_buf_length=%d resp_buf_length=%d)\n",
1858                         zfcp_get_busid_by_adapter(adapter),
1859                         bottom->req_buf_length,
1860                         bottom->resp_buf_length);
1861                 break;
1862
1863         case FSF_REQUEST_SIZE_TOO_LARGE:
1864                 ZFCP_LOG_INFO(
1865                         "Length of the ELS request buffer, "
1866                         "specified in QTCB bottom, "
1867                         "exceeds the size of the buffers "
1868                         "that have been allocated for ELS request data "
1869                         "(adapter: %s, req_buf_length=%d)\n",
1870                         zfcp_get_busid_by_adapter(adapter),
1871                         bottom->req_buf_length);
1872                 break;
1873
1874         case FSF_RESPONSE_SIZE_TOO_LARGE:
1875                 ZFCP_LOG_INFO(
1876                         "Length of the ELS response buffer, "
1877                         "specified in QTCB bottom, "
1878                         "exceeds the size of the buffers "
1879                         "that have been allocated for ELS response data "
1880                         "(adapter: %s, resp_buf_length=%d)\n",
1881                         zfcp_get_busid_by_adapter(adapter),
1882                         bottom->resp_buf_length);
1883                 break;
1884
1885         case FSF_SBAL_MISMATCH:
1886                 /* should never occure, avoided in zfcp_fsf_send_els */
1887                 ZFCP_LOG_INFO("SBAL mismatch (adapter: %s, req_buf_length=%d, "
1888                               "resp_buf_length=%d)\n",
1889                               zfcp_get_busid_by_adapter(adapter),
1890                               bottom->req_buf_length, bottom->resp_buf_length);
1891                 fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1892                 break;
1893
1894         case FSF_ACCESS_DENIED:
1895                 ZFCP_LOG_NORMAL("access denied, cannot send ELS command "
1896                                 "(adapter %s, port d_id=0x%08x)\n",
1897                                 zfcp_get_busid_by_adapter(adapter), d_id);
1898                 for (counter = 0; counter < 2; counter++) {
1899                         subtable = header->fsf_status_qual.halfword[counter * 2];
1900                         rule = header->fsf_status_qual.halfword[counter * 2 + 1];
1901                         switch (subtable) {
1902                         case FSF_SQ_CFDC_SUBTABLE_OS:
1903                         case FSF_SQ_CFDC_SUBTABLE_PORT_WWPN:
1904                         case FSF_SQ_CFDC_SUBTABLE_PORT_DID:
1905                         case FSF_SQ_CFDC_SUBTABLE_LUN:
1906                                 ZFCP_LOG_INFO("Access denied (%s rule %d)\n",
1907                                         zfcp_act_subtable_type[subtable], rule);
1908                                 break;
1909                         }
1910                 }
1911                 debug_text_event(adapter->erp_dbf, 1, "fsf_s_access");
1912                 if (port != NULL)
1913                         zfcp_erp_port_access_denied(port);
1914                 fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1915                 break;
1916
1917         default:
1918                 ZFCP_LOG_NORMAL(
1919                         "bug: An unknown FSF Status was presented "
1920                         "(adapter: %s, fsf_status=0x%08x)\n",
1921                         zfcp_get_busid_by_adapter(adapter),
1922                         header->fsf_status);
1923                 debug_text_event(adapter->erp_dbf, 0, "fsf_sq_inval");
1924                 debug_exception(adapter->erp_dbf, 0,
1925                         &header->fsf_status_qual.word[0], sizeof(u32));
1926                 fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1927                 break;
1928         }
1929
1930 skip_fsfstatus:
1931         send_els->status = retval;
1932
1933         if (send_els->handler != 0)
1934                 send_els->handler(send_els->handler_data);
1935
1936         return retval;
1937 }
1938
1939 /*
1940  * function:
1941  *
1942  * purpose:
1943  *
1944  * returns:     address of initiated FSF request
1945  *              NULL - request could not be initiated
1946  */
1947 int
1948 zfcp_fsf_exchange_config_data(struct zfcp_erp_action *erp_action)
1949 {
1950         volatile struct qdio_buffer_element *sbale;
1951         unsigned long lock_flags;
1952         int retval = 0;
1953
1954         /* setup new FSF request */
1955         retval = zfcp_fsf_req_create(erp_action->adapter,
1956                                      FSF_QTCB_EXCHANGE_CONFIG_DATA,
1957                                      ZFCP_REQ_AUTO_CLEANUP,
1958                                      erp_action->adapter->pool.fsf_req_erp,
1959                                      &lock_flags, &(erp_action->fsf_req));
1960         if (retval < 0) {
1961                 ZFCP_LOG_INFO("error: Could not create exchange configuration "
1962                               "data request for adapter %s.\n",
1963                               zfcp_get_busid_by_adapter(erp_action->adapter));
1964                 goto out;
1965         }
1966
1967         sbale = zfcp_qdio_sbale_req(erp_action->fsf_req,
1968                                     erp_action->fsf_req->sbal_curr, 0);
1969         sbale[0].flags |= SBAL_FLAGS0_TYPE_READ;
1970         sbale[1].flags |= SBAL_FLAGS_LAST_ENTRY;
1971
1972         erp_action->fsf_req->erp_action = erp_action;
1973         erp_action->fsf_req->qtcb->bottom.config.feature_selection =
1974                 (FSF_FEATURE_CFDC | FSF_FEATURE_LUN_SHARING);
1975
1976         /* start QDIO request for this FSF request */
1977         retval = zfcp_fsf_req_send(erp_action->fsf_req, &erp_action->timer);
1978         if (retval) {
1979                 ZFCP_LOG_INFO
1980                     ("error: Could not send exchange configuration data "
1981                      "command on the adapter %s\n",
1982                      zfcp_get_busid_by_adapter(erp_action->adapter));
1983                 zfcp_fsf_req_free(erp_action->fsf_req);
1984                 erp_action->fsf_req = NULL;
1985                 goto out;
1986         }
1987
1988         ZFCP_LOG_DEBUG("exchange configuration data request initiated "
1989                        "(adapter %s)\n",
1990                        zfcp_get_busid_by_adapter(erp_action->adapter));
1991
1992  out:
1993         write_unlock_irqrestore(&erp_action->adapter->request_queue.queue_lock,
1994                                 lock_flags);
1995         return retval;
1996 }
1997
1998 /**
1999  * zfcp_fsf_exchange_config_evaluate
2000  * @fsf_req: fsf_req which belongs to xchg config data request
2001  * @xchg_ok: specifies if xchg config data was incomplete or complete (0/1)
2002  *
2003  * returns: -EIO on error, 0 otherwise
2004  */
2005 static int
2006 zfcp_fsf_exchange_config_evaluate(struct zfcp_fsf_req *fsf_req, int xchg_ok)
2007 {
2008         struct fsf_qtcb_bottom_config *bottom;
2009         struct zfcp_adapter *adapter = fsf_req->adapter;
2010
2011         bottom = &fsf_req->qtcb->bottom.config;
2012         ZFCP_LOG_DEBUG("low/high QTCB version 0x%x/0x%x of FSF\n",
2013                        bottom->low_qtcb_version, bottom->high_qtcb_version);
2014         adapter->fsf_lic_version = bottom->lic_version;
2015         adapter->supported_features = bottom->supported_features;
2016         adapter->peer_wwpn = 0;
2017         adapter->peer_wwnn = 0;
2018         adapter->peer_d_id = 0;
2019
2020         if (xchg_ok) {
2021                 adapter->wwnn = bottom->nport_serv_param.wwnn;
2022                 adapter->wwpn = bottom->nport_serv_param.wwpn;
2023                 adapter->s_id = bottom->s_id & ZFCP_DID_MASK;
2024                 adapter->fc_topology = bottom->fc_topology;
2025                 adapter->fc_link_speed = bottom->fc_link_speed;
2026                 adapter->hydra_version = bottom->adapter_type;
2027         } else {
2028                 adapter->wwnn = 0;
2029                 adapter->wwpn = 0;
2030                 adapter->s_id = 0;
2031                 adapter->fc_topology = 0;
2032                 adapter->fc_link_speed = 0;
2033                 adapter->hydra_version = 0;
2034         }
2035
2036         if (adapter->fc_topology == FSF_TOPO_P2P) {
2037                 adapter->peer_d_id = bottom->peer_d_id & ZFCP_DID_MASK;
2038                 adapter->peer_wwpn = bottom->plogi_payload.wwpn;
2039                 adapter->peer_wwnn = bottom->plogi_payload.wwnn;
2040         }
2041
2042         if(adapter->supported_features & FSF_FEATURE_HBAAPI_MANAGEMENT){
2043                 adapter->hardware_version = bottom->hardware_version;
2044                 memcpy(adapter->serial_number, bottom->serial_number, 17);
2045                 EBCASC(adapter->serial_number, sizeof(adapter->serial_number));
2046         }
2047
2048         ZFCP_LOG_NORMAL("The adapter %s reported the following characteristics:\n"
2049                       "WWNN 0x%016Lx, "
2050                       "WWPN 0x%016Lx, "
2051                       "S_ID 0x%08x,\n"
2052                       "adapter version 0x%x, "
2053                       "LIC version 0x%x, "
2054                       "FC link speed %d Gb/s\n",
2055                       zfcp_get_busid_by_adapter(adapter),
2056                       adapter->wwnn,
2057                       adapter->wwpn,
2058                       (unsigned int) adapter->s_id,
2059                       adapter->hydra_version,
2060                       adapter->fsf_lic_version,
2061                       adapter->fc_link_speed);
2062         if (ZFCP_QTCB_VERSION < bottom->low_qtcb_version) {
2063                 ZFCP_LOG_NORMAL("error: the adapter %s "
2064                                 "only supports newer control block "
2065                                 "versions in comparison to this device "
2066                                 "driver (try updated device driver)\n",
2067                                 zfcp_get_busid_by_adapter(adapter));
2068                 debug_text_event(adapter->erp_dbf, 0, "low_qtcb_ver");
2069                 zfcp_erp_adapter_shutdown(adapter, 0);
2070                 return -EIO;
2071         }
2072         if (ZFCP_QTCB_VERSION > bottom->high_qtcb_version) {
2073                 ZFCP_LOG_NORMAL("error: the adapter %s "
2074                                 "only supports older control block "
2075                                 "versions than this device driver uses"
2076                                 "(consider a microcode upgrade)\n",
2077                                 zfcp_get_busid_by_adapter(adapter));
2078                 debug_text_event(adapter->erp_dbf, 0, "high_qtcb_ver");
2079                 zfcp_erp_adapter_shutdown(adapter, 0);
2080                 return -EIO;
2081         }
2082         return 0;
2083 }
2084
2085 /*
2086  * function:    zfcp_fsf_exchange_config_data_handler
2087  *
2088  * purpose:     is called for finished Exchange Configuration Data command
2089  *
2090  * returns:
2091  */
2092 static int
2093 zfcp_fsf_exchange_config_data_handler(struct zfcp_fsf_req *fsf_req)
2094 {
2095         struct fsf_qtcb_bottom_config *bottom;
2096         struct zfcp_adapter *adapter = fsf_req->adapter;
2097
2098         if (fsf_req->status & ZFCP_STATUS_FSFREQ_ERROR)
2099                 return -EIO;
2100
2101         switch (fsf_req->qtcb->header.fsf_status) {
2102
2103         case FSF_GOOD:
2104                 if (zfcp_fsf_exchange_config_evaluate(fsf_req, 1))
2105                         return -EIO;
2106
2107                 switch (adapter->fc_topology) {
2108                 case FSF_TOPO_P2P:
2109                         ZFCP_LOG_NORMAL("Point-to-Point fibrechannel "
2110                                         "configuration detected at adapter %s\n"
2111                                         "Peer WWNN 0x%016llx, "
2112                                         "peer WWPN 0x%016llx, "
2113                                         "peer d_id 0x%06x\n",
2114                                         zfcp_get_busid_by_adapter(adapter),
2115                                         adapter->peer_wwnn,
2116                                         adapter->peer_wwpn,
2117                                         adapter->peer_d_id);
2118                         debug_text_event(fsf_req->adapter->erp_dbf, 0,
2119                                          "top-p-to-p");
2120                         break;
2121                 case FSF_TOPO_AL:
2122                         ZFCP_LOG_NORMAL("error: Arbitrated loop fibrechannel "
2123                                         "topology detected at adapter %s "
2124                                         "unsupported, shutting down adapter\n",
2125                                         zfcp_get_busid_by_adapter(adapter));
2126                         debug_text_event(fsf_req->adapter->erp_dbf, 0,
2127                                          "top-al");
2128                         zfcp_erp_adapter_shutdown(adapter, 0);
2129                         return -EIO;
2130                 case FSF_TOPO_FABRIC:
2131                         ZFCP_LOG_INFO("Switched fabric fibrechannel "
2132                                       "network detected at adapter %s.\n",
2133                                       zfcp_get_busid_by_adapter(adapter));
2134                         break;
2135                 default:
2136                         ZFCP_LOG_NORMAL("bug: The fibrechannel topology "
2137                                         "reported by the exchange "
2138                                         "configuration command for "
2139                                         "the adapter %s is not "
2140                                         "of a type known to the zfcp "
2141                                         "driver, shutting down adapter\n",
2142                                         zfcp_get_busid_by_adapter(adapter));
2143                         adapter->fc_topology = FSF_TOPO_ERROR;
2144                         debug_text_exception(fsf_req->adapter->erp_dbf, 0,
2145                                              "unknown-topo");
2146                         zfcp_erp_adapter_shutdown(adapter, 0);
2147                         return -EIO;
2148                 }
2149                 bottom = &fsf_req->qtcb->bottom.config;
2150                 if (bottom->max_qtcb_size < sizeof(struct fsf_qtcb)) {
2151                         ZFCP_LOG_NORMAL("bug: Maximum QTCB size (%d bytes) "
2152                                         "allowed by the adapter %s "
2153                                         "is lower than the minimum "
2154                                         "required by the driver (%ld bytes).\n",
2155                                         bottom->max_qtcb_size,
2156                                         zfcp_get_busid_by_adapter(adapter),
2157                                         sizeof(struct fsf_qtcb));
2158                         debug_text_event(fsf_req->adapter->erp_dbf, 0,
2159                                          "qtcb-size");
2160                         debug_event(fsf_req->adapter->erp_dbf, 0,
2161                                     &bottom->max_qtcb_size, sizeof (u32));
2162                         zfcp_erp_adapter_shutdown(adapter, 0);
2163                         return -EIO;
2164                 }
2165                 atomic_set_mask(ZFCP_STATUS_ADAPTER_XCONFIG_OK,
2166                                 &adapter->status);
2167                 break;
2168         case FSF_EXCHANGE_CONFIG_DATA_INCOMPLETE:
2169                 debug_text_event(adapter->erp_dbf, 0, "xchg-inco");
2170
2171                 if (zfcp_fsf_exchange_config_evaluate(fsf_req, 0))
2172                         return -EIO;
2173
2174                 ZFCP_LOG_INFO("Local link to adapter %s is down\n",
2175                               zfcp_get_busid_by_adapter(adapter));
2176                 atomic_set_mask(ZFCP_STATUS_ADAPTER_XCONFIG_OK |
2177                                 ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED,
2178                                 &adapter->status);
2179                 zfcp_erp_adapter_failed(adapter);
2180                 break;
2181         default:
2182                 debug_text_event(fsf_req->adapter->erp_dbf, 0, "fsf-stat-ng");
2183                 debug_event(fsf_req->adapter->erp_dbf, 0,
2184                             &fsf_req->qtcb->header.fsf_status, sizeof (u32));
2185                 zfcp_erp_adapter_shutdown(adapter, 0);
2186                 return -EIO;
2187         }
2188         return 0;
2189 }
2190
2191 /**
2192  * zfcp_fsf_exchange_port_data - request information about local port
2193  * @adapter: for which port data is requested
2194  * @data: response to exchange port data request
2195  */
2196 int
2197 zfcp_fsf_exchange_port_data(struct zfcp_adapter *adapter,
2198                             struct fsf_qtcb_bottom_port *data)
2199 {
2200         volatile struct qdio_buffer_element *sbale;
2201         int retval = 0;
2202         unsigned long lock_flags;
2203         struct zfcp_fsf_req *fsf_req;
2204         struct timer_list *timer;
2205
2206         if(!(adapter->supported_features & FSF_FEATURE_HBAAPI_MANAGEMENT)){
2207                 ZFCP_LOG_INFO("error: exchange port data "
2208                               "command not supported by adapter %s\n",
2209                               zfcp_get_busid_by_adapter(adapter));
2210                 return -EOPNOTSUPP;
2211         }
2212
2213         timer = kmalloc(sizeof(struct timer_list), GFP_KERNEL);
2214         if (!timer)
2215                 return -ENOMEM;
2216
2217         /* setup new FSF request */
2218         retval = zfcp_fsf_req_create(adapter, FSF_QTCB_EXCHANGE_PORT_DATA,
2219                                      0, 0, &lock_flags, &fsf_req);
2220         if (retval < 0) {
2221                 ZFCP_LOG_INFO("error: Out of resources. Could not create an "
2222                               "exchange port data request for"
2223                               "the adapter %s.\n",
2224                               zfcp_get_busid_by_adapter(adapter));
2225                 write_unlock_irqrestore(&adapter->request_queue.queue_lock,
2226                                         lock_flags);
2227                 goto out;
2228         }
2229
2230         sbale = zfcp_qdio_sbale_req(fsf_req, fsf_req->sbal_curr, 0);
2231         sbale[0].flags |= SBAL_FLAGS0_TYPE_READ;
2232         sbale[1].flags |= SBAL_FLAGS_LAST_ENTRY;
2233
2234         fsf_req->data.port_data = data;
2235
2236         init_timer(timer);
2237         timer->function = zfcp_fsf_request_timeout_handler;
2238         timer->data = (unsigned long) adapter;
2239         timer->expires = ZFCP_FSF_REQUEST_TIMEOUT;
2240
2241         retval = zfcp_fsf_req_send(fsf_req, timer);
2242         if (retval) {
2243                 ZFCP_LOG_INFO("error: Could not send an exchange port data "
2244                               "command on the adapter %s\n",
2245                               zfcp_get_busid_by_adapter(adapter));
2246                 zfcp_fsf_req_free(fsf_req);
2247                 write_unlock_irqrestore(&adapter->request_queue.queue_lock,
2248                                         lock_flags);
2249                 goto out;
2250         }
2251
2252         ZFCP_LOG_DEBUG("Exchange Port Data request initiated (adapter %s)\n",
2253                        zfcp_get_busid_by_adapter(adapter));
2254
2255         write_unlock_irqrestore(&adapter->request_queue.queue_lock,
2256                                 lock_flags);
2257
2258         wait_event(fsf_req->completion_wq,
2259                    fsf_req->status & ZFCP_STATUS_FSFREQ_COMPLETED);
2260         del_timer_sync(timer);
2261         zfcp_fsf_req_cleanup(fsf_req);
2262  out:
2263         kfree(timer);
2264         return retval;
2265 }
2266
2267
2268 /**
2269  * zfcp_fsf_exchange_port_data_handler - handler for exchange_port_data request
2270  * @fsf_req: pointer to struct zfcp_fsf_req
2271  */
2272 static void
2273 zfcp_fsf_exchange_port_data_handler(struct zfcp_fsf_req *fsf_req)
2274 {
2275         struct fsf_qtcb_bottom_port *bottom;
2276         struct fsf_qtcb_bottom_port *data = fsf_req->data.port_data;
2277
2278         if (fsf_req->status & ZFCP_STATUS_FSFREQ_ERROR)
2279                 return;
2280
2281         switch (fsf_req->qtcb->header.fsf_status) {
2282         case FSF_GOOD:
2283                 bottom = &fsf_req->qtcb->bottom.port;
2284                 memcpy(data, bottom, sizeof(*data));
2285                 break;
2286
2287         default:
2288                 debug_text_event(fsf_req->adapter->erp_dbf, 0, "xchg-port-ng");
2289                 debug_event(fsf_req->adapter->erp_dbf, 0,
2290                             &fsf_req->qtcb->header.fsf_status, sizeof(u32));
2291         }
2292 }
2293
2294
2295 /*
2296  * function:    zfcp_fsf_open_port
2297  *
2298  * purpose:     
2299  *
2300  * returns:     address of initiated FSF request
2301  *              NULL - request could not be initiated 
2302  */
2303 int
2304 zfcp_fsf_open_port(struct zfcp_erp_action *erp_action)
2305 {
2306         volatile struct qdio_buffer_element *sbale;
2307         unsigned long lock_flags;
2308         int retval = 0;
2309
2310         /* setup new FSF request */
2311         retval = zfcp_fsf_req_create(erp_action->adapter,
2312                                      FSF_QTCB_OPEN_PORT_WITH_DID,
2313                                      ZFCP_WAIT_FOR_SBAL | ZFCP_REQ_AUTO_CLEANUP,
2314                                      erp_action->adapter->pool.fsf_req_erp,
2315                                      &lock_flags, &(erp_action->fsf_req));
2316         if (retval < 0) {
2317                 ZFCP_LOG_INFO("error: Could not create open port request "
2318                               "for port 0x%016Lx on adapter %s.\n",
2319                               erp_action->port->wwpn,
2320                               zfcp_get_busid_by_adapter(erp_action->adapter));
2321                 goto out;
2322         }
2323
2324         sbale = zfcp_qdio_sbale_req(erp_action->fsf_req,
2325                                     erp_action->fsf_req->sbal_curr, 0);
2326         sbale[0].flags |= SBAL_FLAGS0_TYPE_READ;
2327         sbale[1].flags |= SBAL_FLAGS_LAST_ENTRY;
2328
2329         erp_action->fsf_req->qtcb->bottom.support.d_id = erp_action->port->d_id;
2330         atomic_set_mask(ZFCP_STATUS_COMMON_OPENING, &erp_action->port->status);
2331         erp_action->fsf_req->data.open_port.port = erp_action->port;
2332         erp_action->fsf_req->erp_action = erp_action;
2333
2334         /* start QDIO request for this FSF request */
2335         retval = zfcp_fsf_req_send(erp_action->fsf_req, &erp_action->timer);
2336         if (retval) {
2337                 ZFCP_LOG_INFO("error: Could not send open port request for "
2338                               "port 0x%016Lx on adapter %s.\n",
2339                               erp_action->port->wwpn,
2340                               zfcp_get_busid_by_adapter(erp_action->adapter));
2341                 zfcp_fsf_req_free(erp_action->fsf_req);
2342                 erp_action->fsf_req = NULL;
2343                 goto out;
2344         }
2345
2346         ZFCP_LOG_DEBUG("open port request initiated "
2347                        "(adapter %s,  port 0x%016Lx)\n",
2348                        zfcp_get_busid_by_adapter(erp_action->adapter),
2349                        erp_action->port->wwpn);
2350  out:
2351         write_unlock_irqrestore(&erp_action->adapter->request_queue.queue_lock,
2352                                 lock_flags);
2353         return retval;
2354 }
2355
2356 /*
2357  * function:    zfcp_fsf_open_port_handler
2358  *
2359  * purpose:     is called for finished Open Port command
2360  *
2361  * returns:     
2362  */
2363 static int
2364 zfcp_fsf_open_port_handler(struct zfcp_fsf_req *fsf_req)
2365 {
2366         int retval = -EINVAL;
2367         struct zfcp_port *port;
2368         struct fsf_plogi *plogi;
2369         struct fsf_qtcb_header *header;
2370         u16 subtable, rule, counter;
2371
2372         port = fsf_req->data.open_port.port;
2373         header = &fsf_req->qtcb->header;
2374
2375         if (fsf_req->status & ZFCP_STATUS_FSFREQ_ERROR) {
2376                 /* don't change port status in our bookkeeping */
2377                 goto skip_fsfstatus;
2378         }
2379
2380         /* evaluate FSF status in QTCB */
2381         switch (header->fsf_status) {
2382
2383         case FSF_PORT_ALREADY_OPEN:
2384                 ZFCP_LOG_NORMAL("bug: remote port 0x%016Lx on adapter %s "
2385                                 "is already open.\n",
2386                                 port->wwpn, zfcp_get_busid_by_port(port));
2387                 debug_text_exception(fsf_req->adapter->erp_dbf, 0,
2388                                      "fsf_s_popen");
2389                 /*
2390                  * This is a bug, however operation should continue normally
2391                  * if it is simply ignored
2392                  */
2393                 break;
2394
2395         case FSF_ACCESS_DENIED:
2396                 ZFCP_LOG_NORMAL("Access denied, cannot open port 0x%016Lx "
2397                                 "on adapter %s\n",
2398                                 port->wwpn, zfcp_get_busid_by_port(port));
2399                 for (counter = 0; counter < 2; counter++) {
2400                         subtable = header->fsf_status_qual.halfword[counter * 2];
2401                         rule = header->fsf_status_qual.halfword[counter * 2 + 1];
2402                         switch (subtable) {
2403                         case FSF_SQ_CFDC_SUBTABLE_OS:
2404                         case FSF_SQ_CFDC_SUBTABLE_PORT_WWPN:
2405                         case FSF_SQ_CFDC_SUBTABLE_PORT_DID:
2406                         case FSF_SQ_CFDC_SUBTABLE_LUN:
2407                                 ZFCP_LOG_INFO("Access denied (%s rule %d)\n",
2408                                         zfcp_act_subtable_type[subtable], rule);
2409                                 break;
2410                         }
2411                 }
2412                 debug_text_event(fsf_req->adapter->erp_dbf, 1, "fsf_s_access");
2413                 zfcp_erp_port_access_denied(port);
2414                 fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
2415                 break;
2416
2417         case FSF_MAXIMUM_NUMBER_OF_PORTS_EXCEEDED:
2418                 ZFCP_LOG_INFO("error: The FSF adapter is out of resources. "
2419                               "The remote port 0x%016Lx on adapter %s "
2420                               "could not be opened. Disabling it.\n",
2421                               port->wwpn, zfcp_get_busid_by_port(port));
2422                 debug_text_event(fsf_req->adapter->erp_dbf, 1,
2423                                  "fsf_s_max_ports");
2424                 zfcp_erp_port_failed(port);
2425                 fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
2426                 break;
2427
2428         case FSF_ADAPTER_STATUS_AVAILABLE:
2429                 switch (header->fsf_status_qual.word[0]) {
2430                 case FSF_SQ_INVOKE_LINK_TEST_PROCEDURE:
2431                         debug_text_event(fsf_req->adapter->erp_dbf, 1,
2432                                          "fsf_sq_ltest");
2433                         /* ERP strategy will escalate */
2434                         fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
2435                         break;
2436                 case FSF_SQ_ULP_DEPENDENT_ERP_REQUIRED:
2437                         /* ERP strategy will escalate */
2438                         debug_text_event(fsf_req->adapter->erp_dbf, 1,
2439                                          "fsf_sq_ulp");
2440                         fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
2441                         break;
2442                 case FSF_SQ_NO_RETRY_POSSIBLE:
2443                         ZFCP_LOG_NORMAL("The remote port 0x%016Lx on "
2444                                         "adapter %s could not be opened. "
2445                                         "Disabling it.\n",
2446                                         port->wwpn,
2447                                         zfcp_get_busid_by_port(port));
2448                         debug_text_exception(fsf_req->adapter->erp_dbf, 0,
2449                                              "fsf_sq_no_retry");
2450                         zfcp_erp_port_failed(port);
2451                         fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
2452                         break;
2453                 default:
2454                         ZFCP_LOG_NORMAL
2455                             ("bug: Wrong status qualifier 0x%x arrived.\n",
2456                              header->fsf_status_qual.word[0]);
2457                         debug_text_event(fsf_req->adapter->erp_dbf, 0,
2458                                          "fsf_sq_inval:");
2459                         debug_exception(
2460                                 fsf_req->adapter->erp_dbf, 0,
2461                                 &header->fsf_status_qual.word[0],
2462                                 sizeof (u32));
2463                         break;
2464                 }
2465                 break;
2466
2467         case FSF_GOOD:
2468                 /* save port handle assigned by FSF */
2469                 port->handle = header->port_handle;
2470                 ZFCP_LOG_INFO("The remote port 0x%016Lx via adapter %s "
2471                               "was opened, it's port handle is 0x%x\n",
2472                               port->wwpn, zfcp_get_busid_by_port(port),
2473                               port->handle);
2474                 /* mark port as open */
2475                 atomic_set_mask(ZFCP_STATUS_COMMON_OPEN |
2476                                 ZFCP_STATUS_PORT_PHYS_OPEN, &port->status);
2477                 retval = 0;
2478                 /* check whether D_ID has changed during open */
2479                 /*
2480                  * FIXME: This check is not airtight, as the FCP channel does
2481                  * not monitor closures of target port connections caused on
2482                  * the remote side. Thus, they might miss out on invalidating
2483                  * locally cached WWPNs (and other N_Port parameters) of gone
2484                  * target ports. So, our heroic attempt to make things safe
2485                  * could be undermined by 'open port' response data tagged with
2486                  * obsolete WWPNs. Another reason to monitor potential
2487                  * connection closures ourself at least (by interpreting
2488                  * incoming ELS' and unsolicited status). It just crosses my
2489                  * mind that one should be able to cross-check by means of
2490                  * another GID_PN straight after a port has been opened.
2491                  * Alternately, an ADISC/PDISC ELS should suffice, as well.
2492                  */
2493                 plogi = (struct fsf_plogi *) fsf_req->qtcb->bottom.support.els;
2494                 if (!atomic_test_mask(ZFCP_STATUS_PORT_NO_WWPN, &port->status))
2495                 {
2496                         if (fsf_req->qtcb->bottom.support.els1_length <
2497                             ((((unsigned long) &plogi->serv_param.wwpn) -
2498                               ((unsigned long) plogi)) + sizeof (u64))) {
2499                                 ZFCP_LOG_INFO(
2500                                         "warning: insufficient length of "
2501                                         "PLOGI payload (%i)\n",
2502                                         fsf_req->qtcb->bottom.support.els1_length);
2503                                 debug_text_event(fsf_req->adapter->erp_dbf, 0,
2504                                                  "fsf_s_short_plogi:");
2505                                 /* skip sanity check and assume wwpn is ok */
2506                         } else {
2507                                 if (plogi->serv_param.wwpn != port->wwpn) {
2508                                         ZFCP_LOG_INFO("warning: d_id of port "
2509                                                       "0x%016Lx changed during "
2510                                                       "open\n", port->wwpn);
2511                                         debug_text_event(
2512                                                 fsf_req->adapter->erp_dbf, 0,
2513                                                 "fsf_s_did_change:");
2514                                         atomic_clear_mask(
2515                                                 ZFCP_STATUS_PORT_DID_DID,
2516                                                 &port->status);
2517                                 } else
2518                                         port->wwnn = plogi->serv_param.wwnn;
2519                         }
2520                 }
2521                 break;
2522
2523         case FSF_UNKNOWN_OP_SUBTYPE:
2524                 /* should never occure, subtype not set in zfcp_fsf_open_port */
2525                 ZFCP_LOG_INFO("unknown operation subtype (adapter: %s, "
2526                               "op_subtype=0x%x)\n",
2527                               zfcp_get_busid_by_port(port),
2528                               fsf_req->qtcb->bottom.support.operation_subtype);
2529                 fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
2530                 break;
2531
2532         default:
2533                 ZFCP_LOG_NORMAL("bug: An unknown FSF Status was presented "
2534                                 "(debug info 0x%x)\n",
2535                                 header->fsf_status);
2536                 debug_text_event(fsf_req->adapter->erp_dbf, 0, "fsf_s_inval:");
2537                 debug_exception(fsf_req->adapter->erp_dbf, 0,
2538                                 &header->fsf_status, sizeof (u32));
2539                 break;
2540         }
2541
2542  skip_fsfstatus:
2543         atomic_clear_mask(ZFCP_STATUS_COMMON_OPENING, &port->status);
2544         return retval;
2545 }
2546
2547 /*
2548  * function:    zfcp_fsf_close_port
2549  *
2550  * purpose:     submit FSF command "close port"
2551  *
2552  * returns:     address of initiated FSF request
2553  *              NULL - request could not be initiated
2554  */
2555 int
2556 zfcp_fsf_close_port(struct zfcp_erp_action *erp_action)
2557 {
2558         volatile struct qdio_buffer_element *sbale;
2559         unsigned long lock_flags;
2560         int retval = 0;
2561
2562         /* setup new FSF request */
2563         retval = zfcp_fsf_req_create(erp_action->adapter,
2564                                      FSF_QTCB_CLOSE_PORT,
2565                                      ZFCP_WAIT_FOR_SBAL | ZFCP_REQ_AUTO_CLEANUP,
2566                                      erp_action->adapter->pool.fsf_req_erp,
2567                                      &lock_flags, &(erp_action->fsf_req));
2568         if (retval < 0) {
2569                 ZFCP_LOG_INFO("error: Could not create a close port request "
2570                               "for port 0x%016Lx on adapter %s.\n",
2571                               erp_action->port->wwpn,
2572                               zfcp_get_busid_by_adapter(erp_action->adapter));
2573                 goto out;
2574         }
2575
2576         sbale = zfcp_qdio_sbale_req(erp_action->fsf_req,
2577                                     erp_action->fsf_req->sbal_curr, 0);
2578         sbale[0].flags |= SBAL_FLAGS0_TYPE_READ;
2579         sbale[1].flags |= SBAL_FLAGS_LAST_ENTRY;
2580
2581         atomic_set_mask(ZFCP_STATUS_COMMON_CLOSING, &erp_action->port->status);
2582         erp_action->fsf_req->data.close_port.port = erp_action->port;
2583         erp_action->fsf_req->erp_action = erp_action;
2584         erp_action->fsf_req->qtcb->header.port_handle =
2585             erp_action->port->handle;
2586
2587         /* start QDIO request for this FSF request */
2588         retval = zfcp_fsf_req_send(erp_action->fsf_req, &erp_action->timer);
2589         if (retval) {
2590                 ZFCP_LOG_INFO("error: Could not send a close port request for "
2591                               "port 0x%016Lx on adapter %s.\n",
2592                               erp_action->port->wwpn,
2593                               zfcp_get_busid_by_adapter(erp_action->adapter));
2594                 zfcp_fsf_req_free(erp_action->fsf_req);
2595                 erp_action->fsf_req = NULL;
2596                 goto out;
2597         }
2598
2599         ZFCP_LOG_TRACE("close port request initiated "
2600                        "(adapter %s, port 0x%016Lx)\n",
2601                        zfcp_get_busid_by_adapter(erp_action->adapter),
2602                        erp_action->port->wwpn);
2603  out:
2604         write_unlock_irqrestore(&erp_action->adapter->request_queue.queue_lock,
2605                                 lock_flags);
2606         return retval;
2607 }
2608
2609 /*
2610  * function:    zfcp_fsf_close_port_handler
2611  *
2612  * purpose:     is called for finished Close Port FSF command
2613  *
2614  * returns:
2615  */
2616 static int
2617 zfcp_fsf_close_port_handler(struct zfcp_fsf_req *fsf_req)
2618 {
2619         int retval = -EINVAL;
2620         struct zfcp_port *port;
2621
2622         port = fsf_req->data.close_port.port;
2623
2624         if (fsf_req->status & ZFCP_STATUS_FSFREQ_ERROR) {
2625                 /* don't change port status in our bookkeeping */
2626                 goto skip_fsfstatus;
2627         }
2628
2629         /* evaluate FSF status in QTCB */
2630         switch (fsf_req->qtcb->header.fsf_status) {
2631
2632         case FSF_PORT_HANDLE_NOT_VALID:
2633                 ZFCP_LOG_INFO("Temporary port identifier 0x%x for port "
2634                               "0x%016Lx on adapter %s invalid. This may happen "
2635                               "occasionally.\n", port->handle,
2636                               port->wwpn, zfcp_get_busid_by_port(port));
2637                 ZFCP_LOG_DEBUG("status qualifier:\n");
2638                 ZFCP_HEX_DUMP(ZFCP_LOG_LEVEL_DEBUG,
2639                               (char *) &fsf_req->qtcb->header.fsf_status_qual,
2640                               sizeof (union fsf_status_qual));
2641                 debug_text_event(fsf_req->adapter->erp_dbf, 1,
2642                                  "fsf_s_phand_nv");
2643                 zfcp_erp_adapter_reopen(port->adapter, 0);
2644                 fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
2645                 break;
2646
2647         case FSF_ADAPTER_STATUS_AVAILABLE:
2648                 /* Note: FSF has actually closed the port in this case.
2649                  * The status code is just daft. Fingers crossed for a change
2650                  */
2651                 retval = 0;
2652                 break;
2653
2654         case FSF_GOOD:
2655                 ZFCP_LOG_TRACE("remote port 0x016%Lx on adapter %s closed, "
2656                                "port handle 0x%x\n", port->wwpn,
2657                                zfcp_get_busid_by_port(port), port->handle);
2658                 zfcp_erp_modify_port_status(port,
2659                                             ZFCP_STATUS_COMMON_OPEN,
2660                                             ZFCP_CLEAR);
2661                 retval = 0;
2662                 break;
2663
2664         default:
2665                 ZFCP_LOG_NORMAL("bug: An unknown FSF Status was presented "
2666                                 "(debug info 0x%x)\n",
2667                                 fsf_req->qtcb->header.fsf_status);
2668                 debug_text_event(fsf_req->adapter->erp_dbf, 0, "fsf_s_inval:");
2669                 debug_exception(fsf_req->adapter->erp_dbf, 0,
2670                                 &fsf_req->qtcb->header.fsf_status,
2671                                 sizeof (u32));
2672                 break;
2673         }
2674
2675  skip_fsfstatus:
2676         atomic_clear_mask(ZFCP_STATUS_COMMON_CLOSING, &port->status);
2677         return retval;
2678 }
2679
2680 /*
2681  * function:    zfcp_fsf_close_physical_port
2682  *
2683  * purpose:     submit FSF command "close physical port"
2684  *
2685  * returns:     address of initiated FSF request
2686  *              NULL - request could not be initiated
2687  */
2688 int
2689 zfcp_fsf_close_physical_port(struct zfcp_erp_action *erp_action)
2690 {
2691         int retval = 0;
2692         unsigned long lock_flags;
2693         volatile struct qdio_buffer_element *sbale;
2694
2695         /* setup new FSF request */
2696         retval = zfcp_fsf_req_create(erp_action->adapter,
2697                                      FSF_QTCB_CLOSE_PHYSICAL_PORT,
2698                                      ZFCP_WAIT_FOR_SBAL | ZFCP_REQ_AUTO_CLEANUP,
2699                                      erp_action->adapter->pool.fsf_req_erp,
2700                                      &lock_flags, &erp_action->fsf_req);
2701         if (retval < 0) {
2702                 ZFCP_LOG_INFO("error: Could not create close physical port "
2703                               "request (adapter %s, port 0x%016Lx)\n",
2704                               zfcp_get_busid_by_adapter(erp_action->adapter),
2705                               erp_action->port->wwpn);
2706
2707                 goto out;
2708         }
2709
2710         sbale = zfcp_qdio_sbale_req(erp_action->fsf_req,
2711                                     erp_action->fsf_req->sbal_curr, 0);
2712         sbale[0].flags |= SBAL_FLAGS0_TYPE_READ;
2713         sbale[1].flags |= SBAL_FLAGS_LAST_ENTRY;
2714
2715         /* mark port as being closed */
2716         atomic_set_mask(ZFCP_STATUS_PORT_PHYS_CLOSING,
2717                         &erp_action->port->status);
2718         /* save a pointer to this port */
2719         erp_action->fsf_req->data.close_physical_port.port = erp_action->port;
2720         /* port to be closeed */
2721         erp_action->fsf_req->qtcb->header.port_handle =
2722             erp_action->port->handle;
2723         erp_action->fsf_req->erp_action = erp_action;
2724
2725         /* start QDIO request for this FSF request */
2726         retval = zfcp_fsf_req_send(erp_action->fsf_req, &erp_action->timer);
2727         if (retval) {
2728                 ZFCP_LOG_INFO("error: Could not send close physical port "
2729                               "request (adapter %s, port 0x%016Lx)\n",
2730                               zfcp_get_busid_by_adapter(erp_action->adapter),
2731                               erp_action->port->wwpn);
2732                 zfcp_fsf_req_free(erp_action->fsf_req);
2733                 erp_action->fsf_req = NULL;
2734                 goto out;
2735         }
2736
2737         ZFCP_LOG_TRACE("close physical port request initiated "
2738                        "(adapter %s, port 0x%016Lx)\n",
2739                        zfcp_get_busid_by_adapter(erp_action->adapter),
2740                        erp_action->port->wwpn);
2741  out:
2742         write_unlock_irqrestore(&erp_action->adapter->request_queue.queue_lock,
2743                                 lock_flags);
2744         return retval;
2745 }
2746
2747 /*
2748  * function:    zfcp_fsf_close_physical_port_handler
2749  *
2750  * purpose:     is called for finished Close Physical Port FSF command
2751  *
2752  * returns:
2753  */
2754 static int
2755 zfcp_fsf_close_physical_port_handler(struct zfcp_fsf_req *fsf_req)
2756 {
2757         int retval = -EINVAL;
2758         struct zfcp_port *port;
2759         struct zfcp_unit *unit;
2760         struct fsf_qtcb_header *header;
2761         u16 subtable, rule, counter;
2762
2763         port = fsf_req->data.close_physical_port.port;
2764         header = &fsf_req->qtcb->header;
2765
2766         if (fsf_req->status & ZFCP_STATUS_FSFREQ_ERROR) {
2767                 /* don't change port status in our bookkeeping */
2768                 goto skip_fsfstatus;
2769         }
2770
2771         /* evaluate FSF status in QTCB */
2772         switch (header->fsf_status) {
2773
2774         case FSF_PORT_HANDLE_NOT_VALID:
2775                 ZFCP_LOG_INFO("Temporary port identifier 0x%x invalid"
2776                               "(adapter %s, port 0x%016Lx). "
2777                               "This may happen occasionally.\n",
2778                               port->handle,
2779                               zfcp_get_busid_by_port(port),
2780                               port->wwpn);
2781                 ZFCP_LOG_DEBUG("status qualifier:\n");
2782                 ZFCP_HEX_DUMP(ZFCP_LOG_LEVEL_DEBUG,
2783                               (char *) &header->fsf_status_qual,
2784                               sizeof (union fsf_status_qual));
2785                 debug_text_event(fsf_req->adapter->erp_dbf, 1,
2786                                  "fsf_s_phand_nv");
2787                 zfcp_erp_adapter_reopen(port->adapter, 0);
2788                 fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
2789                 break;
2790
2791         case FSF_ACCESS_DENIED:
2792                 ZFCP_LOG_NORMAL("Access denied, cannot close "
2793                                 "physical port 0x%016Lx on adapter %s\n",
2794                                 port->wwpn, zfcp_get_busid_by_port(port));
2795                 for (counter = 0; counter < 2; counter++) {
2796                         subtable = header->fsf_status_qual.halfword[counter * 2];
2797                         rule = header->fsf_status_qual.halfword[counter * 2 + 1];
2798                         switch (subtable) {
2799                         case FSF_SQ_CFDC_SUBTABLE_OS:
2800                         case FSF_SQ_CFDC_SUBTABLE_PORT_WWPN:
2801                         case FSF_SQ_CFDC_SUBTABLE_PORT_DID:
2802                         case FSF_SQ_CFDC_SUBTABLE_LUN:
2803                                 ZFCP_LOG_INFO("Access denied (%s rule %d)\n",
2804                                         zfcp_act_subtable_type[subtable], rule);
2805                                 break;
2806                         }
2807                 }
2808                 debug_text_event(fsf_req->adapter->erp_dbf, 1, "fsf_s_access");
2809                 zfcp_erp_port_access_denied(port);
2810                 fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
2811                 break;
2812
2813         case FSF_PORT_BOXED:
2814                 ZFCP_LOG_DEBUG("The remote port 0x%016Lx on adapter "
2815                                "%s needs to be reopened but it was attempted "
2816                                "to close it physically.\n",
2817                                port->wwpn,
2818                                zfcp_get_busid_by_port(port));
2819                 debug_text_event(fsf_req->adapter->erp_dbf, 1, "fsf_s_pboxed");
2820                 zfcp_erp_port_reopen(port, 0);
2821                 fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR |
2822                         ZFCP_STATUS_FSFREQ_RETRY;
2823                 break;
2824
2825         case FSF_ADAPTER_STATUS_AVAILABLE:
2826                 switch (header->fsf_status_qual.word[0]) {
2827                 case FSF_SQ_INVOKE_LINK_TEST_PROCEDURE:
2828                         debug_text_event(fsf_req->adapter->erp_dbf, 1,
2829                                          "fsf_sq_ltest");
2830                         /* This will now be escalated by ERP */
2831                         fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
2832                         break;
2833                 case FSF_SQ_ULP_DEPENDENT_ERP_REQUIRED:
2834                         /* ERP strategy will escalate */
2835                         debug_text_event(fsf_req->adapter->erp_dbf, 1,
2836                                          "fsf_sq_ulp");
2837                         fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
2838                         break;
2839                 default:
2840                         ZFCP_LOG_NORMAL
2841                             ("bug: Wrong status qualifier 0x%x arrived.\n",
2842                              header->fsf_status_qual.word[0]);
2843                         debug_text_event(fsf_req->adapter->erp_dbf, 0,
2844                                          "fsf_sq_inval:");
2845                         debug_exception(
2846                                 fsf_req->adapter->erp_dbf, 0,
2847                                 &header->fsf_status_qual.word[0], sizeof (u32));
2848                         break;
2849                 }
2850                 break;
2851
2852         case FSF_GOOD:
2853                 ZFCP_LOG_DEBUG("Remote port 0x%016Lx via adapter %s "
2854                                "physically closed, port handle 0x%x\n",
2855                                port->wwpn,
2856                                zfcp_get_busid_by_port(port), port->handle);
2857                 /* can't use generic zfcp_erp_modify_port_status because
2858                  * ZFCP_STATUS_COMMON_OPEN must not be reset for the port
2859                  */
2860                 atomic_clear_mask(ZFCP_STATUS_PORT_PHYS_OPEN, &port->status);
2861                 list_for_each_entry(unit, &port->unit_list_head, list)
2862                     atomic_clear_mask(ZFCP_STATUS_COMMON_OPEN, &unit->status);
2863                 retval = 0;
2864                 break;
2865
2866         default:
2867                 ZFCP_LOG_NORMAL("bug: An unknown FSF Status was presented "
2868                                 "(debug info 0x%x)\n",
2869                                 header->fsf_status);
2870                 debug_text_event(fsf_req->adapter->erp_dbf, 0, "fsf_s_inval:");
2871                 debug_exception(fsf_req->adapter->erp_dbf, 0,
2872                                 &header->fsf_status, sizeof (u32));
2873                 break;
2874         }
2875
2876  skip_fsfstatus:
2877         atomic_clear_mask(ZFCP_STATUS_PORT_PHYS_CLOSING, &port->status);
2878         return retval;
2879 }
2880
2881 /*
2882  * function:    zfcp_fsf_open_unit
2883  *
2884  * purpose:
2885  *
2886  * returns:
2887  *
2888  * assumptions: This routine does not check whether the associated
2889  *              remote port has already been opened. This should be
2890  *              done by calling routines. Otherwise some status
2891  *              may be presented by FSF
2892  */
2893 int
2894 zfcp_fsf_open_unit(struct zfcp_erp_action *erp_action)
2895 {
2896         volatile struct qdio_buffer_element *sbale;
2897         unsigned long lock_flags;
2898         int retval = 0;
2899
2900         /* setup new FSF request */
2901         retval = zfcp_fsf_req_create(erp_action->adapter,
2902                                      FSF_QTCB_OPEN_LUN,
2903                                      ZFCP_WAIT_FOR_SBAL | ZFCP_REQ_AUTO_CLEANUP,
2904                                      erp_action->adapter->pool.fsf_req_erp,
2905                                      &lock_flags, &(erp_action->fsf_req));
2906         if (retval < 0) {
2907                 ZFCP_LOG_INFO("error: Could not create open unit request for "
2908                               "unit 0x%016Lx on port 0x%016Lx on adapter %s.\n",
2909                               erp_action->unit->fcp_lun,
2910                               erp_action->unit->port->wwpn,
2911                               zfcp_get_busid_by_adapter(erp_action->adapter));
2912                 goto out;
2913         }
2914
2915         sbale = zfcp_qdio_sbale_req(erp_action->fsf_req,
2916                                     erp_action->fsf_req->sbal_curr, 0);
2917         sbale[0].flags |= SBAL_FLAGS0_TYPE_READ;
2918         sbale[1].flags |= SBAL_FLAGS_LAST_ENTRY;
2919
2920         erp_action->fsf_req->qtcb->header.port_handle =
2921                 erp_action->port->handle;
2922         erp_action->fsf_req->qtcb->bottom.support.fcp_lun =
2923                 erp_action->unit->fcp_lun;
2924         erp_action->fsf_req->qtcb->bottom.support.option =
2925                 FSF_OPEN_LUN_SUPPRESS_BOXING;
2926         atomic_set_mask(ZFCP_STATUS_COMMON_OPENING, &erp_action->unit->status);
2927         erp_action->fsf_req->data.open_unit.unit = erp_action->unit;
2928         erp_action->fsf_req->erp_action = erp_action;
2929
2930         /* start QDIO request for this FSF request */
2931         retval = zfcp_fsf_req_send(erp_action->fsf_req, &erp_action->timer);
2932         if (retval) {
2933                 ZFCP_LOG_INFO("error: Could not send an open unit request "
2934                               "on the adapter %s, port 0x%016Lx for "
2935                               "unit 0x%016Lx\n",
2936                               zfcp_get_busid_by_adapter(erp_action->adapter),
2937                               erp_action->port->wwpn,
2938                               erp_action->unit->fcp_lun);
2939                 zfcp_fsf_req_free(erp_action->fsf_req);
2940                 erp_action->fsf_req = NULL;
2941                 goto out;
2942         }
2943
2944         ZFCP_LOG_TRACE("Open LUN request initiated (adapter %s, "
2945                        "port 0x%016Lx, unit 0x%016Lx)\n",
2946                        zfcp_get_busid_by_adapter(erp_action->adapter),
2947                        erp_action->port->wwpn, erp_action->unit->fcp_lun);
2948  out:
2949         write_unlock_irqrestore(&erp_action->adapter->request_queue.queue_lock,
2950                                 lock_flags);
2951         return retval;
2952 }
2953
2954 /*
2955  * function:    zfcp_fsf_open_unit_handler
2956  *
2957  * purpose:     is called for finished Open LUN command
2958  *
2959  * returns:     
2960  */
2961 static int
2962 zfcp_fsf_open_unit_handler(struct zfcp_fsf_req *fsf_req)
2963 {
2964         int retval = -EINVAL;
2965         struct zfcp_adapter *adapter;
2966         struct zfcp_unit *unit;
2967         struct fsf_qtcb_header *header;
2968         struct fsf_qtcb_bottom_support *bottom;
2969         struct fsf_queue_designator *queue_designator;
2970         u16 subtable, rule, counter;
2971         u32 allowed, exclusive, readwrite;
2972
2973         unit = fsf_req->data.open_unit.unit;
2974
2975         if (fsf_req->status & ZFCP_STATUS_FSFREQ_ERROR) {
2976                 /* don't change unit status in our bookkeeping */
2977                 goto skip_fsfstatus;
2978         }
2979
2980         adapter = fsf_req->adapter;
2981         header = &fsf_req->qtcb->header;
2982         bottom = &fsf_req->qtcb->bottom.support;
2983         queue_designator = &header->fsf_status_qual.fsf_queue_designator;
2984
2985         allowed   = bottom->lun_access_info & FSF_UNIT_ACCESS_OPEN_LUN_ALLOWED;
2986         exclusive = bottom->lun_access_info & FSF_UNIT_ACCESS_EXCLUSIVE;
2987         readwrite = bottom->lun_access_info & FSF_UNIT_ACCESS_OUTBOUND_TRANSFER;
2988
2989         atomic_clear_mask(ZFCP_STATUS_COMMON_ACCESS_DENIED |
2990                           ZFCP_STATUS_UNIT_SHARED |
2991                           ZFCP_STATUS_UNIT_READONLY,
2992                           &unit->status);
2993
2994         /* evaluate FSF status in QTCB */
2995         switch (header->fsf_status) {
2996
2997         case FSF_PORT_HANDLE_NOT_VALID:
2998                 ZFCP_LOG_INFO("Temporary port identifier 0x%x "
2999                               "for port 0x%016Lx on adapter %s invalid "
3000                               "This may happen occasionally\n",
3001                               unit->port->handle,
3002                               unit->port->wwpn, zfcp_get_busid_by_unit(unit));
3003                 ZFCP_LOG_DEBUG("status qualifier:\n");
3004                 ZFCP_HEX_DUMP(ZFCP_LOG_LEVEL_DEBUG,
3005                               (char *) &header->fsf_status_qual,
3006                               sizeof (union fsf_status_qual));
3007                 debug_text_event(adapter->erp_dbf, 1, "fsf_s_ph_nv");
3008                 zfcp_erp_adapter_reopen(unit->port->adapter, 0);
3009                 fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
3010                 break;
3011
3012         case FSF_LUN_ALREADY_OPEN:
3013                 ZFCP_LOG_NORMAL("bug: Attempted to open unit 0x%016Lx on "
3014                                 "remote port 0x%016Lx on adapter %s twice.\n",
3015                                 unit->fcp_lun,
3016                                 unit->port->wwpn, zfcp_get_busid_by_unit(unit));
3017                 debug_text_exception(adapter->erp_dbf, 0,
3018                                      "fsf_s_uopen");
3019                 fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
3020                 break;
3021
3022         case FSF_ACCESS_DENIED:
3023                 ZFCP_LOG_NORMAL("Access denied, cannot open unit 0x%016Lx on "
3024                                 "remote port 0x%016Lx on adapter %s\n",
3025                                 unit->fcp_lun, unit->port->wwpn,
3026                                 zfcp_get_busid_by_unit(unit));
3027                 for (counter = 0; counter < 2; counter++) {
3028                         subtable = header->fsf_status_qual.halfword[counter * 2];
3029                         rule = header->fsf_status_qual.halfword[counter * 2 + 1];
3030                         switch (subtable) {
3031                         case FSF_SQ_CFDC_SUBTABLE_OS:
3032                         case FSF_SQ_CFDC_SUBTABLE_PORT_WWPN:
3033                         case FSF_SQ_CFDC_SUBTABLE_PORT_DID:
3034                         case FSF_SQ_CFDC_SUBTABLE_LUN:
3035                                 ZFCP_LOG_INFO("Access denied (%s rule %d)\n",
3036                                         zfcp_act_subtable_type[subtable], rule);
3037                                 break;
3038                         }
3039                 }
3040                 debug_text_event(adapter->erp_dbf, 1, "fsf_s_access");
3041                 zfcp_erp_unit_access_denied(unit);
3042                 atomic_clear_mask(ZFCP_STATUS_UNIT_SHARED, &unit->status);
3043                 atomic_clear_mask(ZFCP_STATUS_UNIT_READONLY, &unit->status);
3044                 fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
3045                 break;
3046
3047         case FSF_PORT_BOXED:
3048                 ZFCP_LOG_DEBUG("The remote port 0x%016Lx on adapter %s "
3049                                "needs to be reopened\n",
3050                                unit->port->wwpn, zfcp_get_busid_by_unit(unit));
3051                 debug_text_event(adapter->erp_dbf, 2, "fsf_s_pboxed");
3052                 zfcp_erp_port_reopen(unit->port, 0);
3053                 fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR |
3054                         ZFCP_STATUS_FSFREQ_RETRY;
3055                 break;
3056
3057         case FSF_LUN_SHARING_VIOLATION:
3058                 if (header->fsf_status_qual.word[0] != 0) {
3059                         ZFCP_LOG_NORMAL("FCP-LUN 0x%Lx at the remote port "
3060                                         "with WWPN 0x%Lx "
3061                                         "connected to the adapter %s "
3062                                         "is already in use in LPAR%d, CSS%d\n",
3063                                         unit->fcp_lun,
3064                                         unit->port->wwpn,
3065                                         zfcp_get_busid_by_unit(unit),
3066                                         queue_designator->hla,
3067                                         queue_designator->cssid);
3068                 } else {
3069                         subtable = header->fsf_status_qual.halfword[4];
3070                         rule = header->fsf_status_qual.halfword[5];
3071                         switch (subtable) {
3072                         case FSF_SQ_CFDC_SUBTABLE_OS:
3073                         case FSF_SQ_CFDC_SUBTABLE_PORT_WWPN:
3074                         case FSF_SQ_CFDC_SUBTABLE_PORT_DID:
3075                         case FSF_SQ_CFDC_SUBTABLE_LUN:
3076                                 ZFCP_LOG_NORMAL("Access to FCP-LUN 0x%Lx at the "
3077                                                 "remote port with WWPN 0x%Lx "
3078                                                 "connected to the adapter %s "
3079                                                 "is denied (%s rule %d)\n",
3080                                                 unit->fcp_lun,
3081                                                 unit->port->wwpn,
3082                                                 zfcp_get_busid_by_unit(unit),
3083                                                 zfcp_act_subtable_type[subtable],
3084                                                 rule);
3085                                 break;
3086                         }
3087                 }
3088                 ZFCP_LOG_DEBUG("status qualifier:\n");
3089                 ZFCP_HEX_DUMP(ZFCP_LOG_LEVEL_DEBUG,
3090                               (char *) &header->fsf_status_qual,
3091                               sizeof (union fsf_status_qual));
3092                 debug_text_event(adapter->erp_dbf, 2,
3093                                  "fsf_s_l_sh_vio");
3094                 zfcp_erp_unit_access_denied(unit);
3095                 atomic_clear_mask(ZFCP_STATUS_UNIT_SHARED, &unit->status);
3096                 atomic_clear_mask(ZFCP_STATUS_UNIT_READONLY, &unit->status);
3097                 fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
3098                 break;
3099
3100         case FSF_MAXIMUM_NUMBER_OF_LUNS_EXCEEDED:
3101                 ZFCP_LOG_INFO("error: The adapter ran out of resources. "
3102                               "There is no handle (temporary port identifier) "
3103                               "available for unit 0x%016Lx on port 0x%016Lx "
3104                               "on adapter %s\n",
3105                               unit->fcp_lun,
3106                               unit->port->wwpn,
3107                               zfcp_get_busid_by_unit(unit));
3108                 debug_text_event(adapter->erp_dbf, 1,
3109                                  "fsf_s_max_units");
3110                 zfcp_erp_unit_failed(unit);
3111                 fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
3112                 break;
3113
3114         case FSF_ADAPTER_STATUS_AVAILABLE:
3115                 switch (header->fsf_status_qual.word[0]) {
3116                 case FSF_SQ_INVOKE_LINK_TEST_PROCEDURE:
3117                         /* Re-establish link to port */
3118                         debug_text_event(adapter->erp_dbf, 1,
3119                                          "fsf_sq_ltest");
3120                         zfcp_test_link(unit->port);
3121                         fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
3122                         break;
3123                 case FSF_SQ_ULP_DEPENDENT_ERP_REQUIRED:
3124                         /* ERP strategy will escalate */
3125                         debug_text_event(adapter->erp_dbf, 1,
3126                                          "fsf_sq_ulp");
3127                         fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
3128                         break;
3129                 default:
3130                         ZFCP_LOG_NORMAL
3131                             ("bug: Wrong status qualifier 0x%x arrived.\n",
3132                              header->fsf_status_qual.word[0]);
3133                         debug_text_event(adapter->erp_dbf, 0,
3134                                          "fsf_sq_inval:");
3135                         debug_exception(adapter->erp_dbf, 0,
3136                                         &header->fsf_status_qual.word[0],
3137                                 sizeof (u32));
3138                 }
3139                 break;
3140
3141         case FSF_INVALID_COMMAND_OPTION:
3142                 ZFCP_LOG_NORMAL(
3143                         "Invalid option 0x%x has been specified "
3144                         "in QTCB bottom sent to the adapter %s\n",
3145                         bottom->option,
3146                         zfcp_get_busid_by_adapter(adapter));
3147                 fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
3148                 retval = -EINVAL;
3149                 break;
3150
3151         case FSF_GOOD:
3152                 /* save LUN handle assigned by FSF */
3153                 unit->handle = header->lun_handle;
3154                 ZFCP_LOG_TRACE("unit 0x%016Lx on remote port 0x%016Lx on "
3155                                "adapter %s opened, port handle 0x%x\n",
3156                                unit->fcp_lun,
3157                                unit->port->wwpn,
3158                                zfcp_get_busid_by_unit(unit),
3159                                unit->handle);
3160                 /* mark unit as open */
3161                 atomic_set_mask(ZFCP_STATUS_COMMON_OPEN, &unit->status);
3162
3163                 if (adapter->supported_features & FSF_FEATURE_LUN_SHARING){
3164                         if (!exclusive)
3165                                 atomic_set_mask(ZFCP_STATUS_UNIT_SHARED,
3166                                                 &unit->status);
3167
3168                         if (!readwrite) {
3169                                 atomic_set_mask(ZFCP_STATUS_UNIT_READONLY,
3170                                                 &unit->status);
3171                                 ZFCP_LOG_NORMAL("read-only access for unit "
3172                                                 "(adapter %s, wwpn=0x%016Lx, "
3173                                                 "fcp_lun=0x%016Lx)\n",
3174                                                 zfcp_get_busid_by_unit(unit),
3175                                                 unit->port->wwpn,
3176                                                 unit->fcp_lun);
3177                         }
3178
3179                         if (exclusive && !readwrite) {
3180                                 ZFCP_LOG_NORMAL("exclusive access of read-only "
3181                                                 "unit not supported\n");
3182                                 zfcp_erp_unit_failed(unit);
3183                                 fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
3184                                 zfcp_erp_unit_shutdown(unit, 0);
3185                         } else if (!exclusive && readwrite) {
3186                                 ZFCP_LOG_NORMAL("shared access of read-write "
3187                                                 "unit not supported\n");
3188                                 zfcp_erp_unit_failed(unit);
3189                                 fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
3190                                 zfcp_erp_unit_shutdown(unit, 0);
3191                         }
3192                 }
3193
3194                 retval = 0;
3195                 break;
3196
3197         default:
3198                 ZFCP_LOG_NORMAL("bug: An unknown FSF Status was presented "
3199                                 "(debug info 0x%x)\n",
3200                                 header->fsf_status);
3201                 debug_text_event(adapter->erp_dbf, 0, "fsf_s_inval:");
3202                 debug_exception(adapter->erp_dbf, 0,
3203                                 &header->fsf_status, sizeof (u32));
3204                 break;
3205         }
3206
3207  skip_fsfstatus:
3208         atomic_clear_mask(ZFCP_STATUS_COMMON_OPENING, &unit->status);
3209         return retval;
3210 }
3211
3212 /*
3213  * function:    zfcp_fsf_close_unit
3214  *
3215  * purpose:
3216  *
3217  * returns:     address of fsf_req - request successfully initiated
3218  *              NULL - 
3219  *
3220  * assumptions: This routine does not check whether the associated
3221  *              remote port/lun has already been opened. This should be
3222  *              done by calling routines. Otherwise some status
3223  *              may be presented by FSF
3224  */
3225 int
3226 zfcp_fsf_close_unit(struct zfcp_erp_action *erp_action)
3227 {
3228         volatile struct qdio_buffer_element *sbale;
3229         unsigned long lock_flags;
3230         int retval = 0;
3231
3232         /* setup new FSF request */
3233         retval = zfcp_fsf_req_create(erp_action->adapter,
3234                                      FSF_QTCB_CLOSE_LUN,
3235                                      ZFCP_WAIT_FOR_SBAL | ZFCP_REQ_AUTO_CLEANUP,
3236                                      erp_action->adapter->pool.fsf_req_erp,
3237                                      &lock_flags, &(erp_action->fsf_req));
3238         if (retval < 0) {
3239                 ZFCP_LOG_INFO("error: Could not create close unit request for "
3240                               "unit 0x%016Lx on port 0x%016Lx on adapter %s.\n",
3241                               erp_action->unit->fcp_lun,
3242                               erp_action->port->wwpn,
3243                               zfcp_get_busid_by_adapter(erp_action->adapter));
3244                 goto out;
3245         }
3246
3247         sbale = zfcp_qdio_sbale_req(erp_action->fsf_req,
3248                                     erp_action->fsf_req->sbal_curr, 0);
3249         sbale[0].flags |= SBAL_FLAGS0_TYPE_READ;
3250         sbale[1].flags |= SBAL_FLAGS_LAST_ENTRY;
3251
3252         erp_action->fsf_req->qtcb->header.port_handle =
3253             erp_action->port->handle;
3254         erp_action->fsf_req->qtcb->header.lun_handle = erp_action->unit->handle;
3255         atomic_set_mask(ZFCP_STATUS_COMMON_CLOSING, &erp_action->unit->status);
3256         erp_action->fsf_req->data.close_unit.unit = erp_action->unit;
3257         erp_action->fsf_req->erp_action = erp_action;
3258
3259         /* start QDIO request for this FSF request */
3260         retval = zfcp_fsf_req_send(erp_action->fsf_req, &erp_action->timer);
3261         if (retval) {
3262                 ZFCP_LOG_INFO("error: Could not send a close unit request for "
3263                               "unit 0x%016Lx on port 0x%016Lx onadapter %s.\n",
3264                               erp_action->unit->fcp_lun,
3265                               erp_action->port->wwpn,
3266                               zfcp_get_busid_by_adapter(erp_action->adapter));
3267                 zfcp_fsf_req_free(erp_action->fsf_req);
3268                 erp_action->fsf_req = NULL;
3269                 goto out;
3270         }
3271
3272         ZFCP_LOG_TRACE("Close LUN request initiated (adapter %s, "
3273                        "port 0x%016Lx, unit 0x%016Lx)\n",
3274                        zfcp_get_busid_by_adapter(erp_action->adapter),
3275                        erp_action->port->wwpn, erp_action->unit->fcp_lun);
3276  out:
3277         write_unlock_irqrestore(&erp_action->adapter->request_queue.queue_lock,
3278                                 lock_flags);
3279         return retval;
3280 }
3281
3282 /*
3283  * function:    zfcp_fsf_close_unit_handler
3284  *
3285  * purpose:     is called for finished Close LUN FSF command
3286  *
3287  * returns:
3288  */
3289 static int
3290 zfcp_fsf_close_unit_handler(struct zfcp_fsf_req *fsf_req)
3291 {
3292         int retval = -EINVAL;
3293         struct zfcp_unit *unit;
3294
3295         unit = fsf_req->data.close_unit.unit;   /* restore unit */
3296
3297         if (fsf_req->status & ZFCP_STATUS_FSFREQ_ERROR) {
3298                 /* don't change unit status in our bookkeeping */
3299                 goto skip_fsfstatus;
3300         }
3301
3302         /* evaluate FSF status in QTCB */
3303         switch (fsf_req->qtcb->header.fsf_status) {
3304
3305         case FSF_PORT_HANDLE_NOT_VALID:
3306                 ZFCP_LOG_INFO("Temporary port identifier 0x%x for port "
3307                               "0x%016Lx on adapter %s invalid. This may "
3308                               "happen in rare circumstances\n",
3309                               unit->port->handle,
3310                               unit->port->wwpn,
3311                               zfcp_get_busid_by_unit(unit));
3312                 ZFCP_LOG_DEBUG("status qualifier:\n");
3313                 ZFCP_HEX_DUMP(ZFCP_LOG_LEVEL_DEBUG,
3314                               (char *) &fsf_req->qtcb->header.fsf_status_qual,
3315                               sizeof (union fsf_status_qual));
3316                 debug_text_event(fsf_req->adapter->erp_dbf, 1,
3317                                  "fsf_s_phand_nv");
3318                 zfcp_erp_adapter_reopen(unit->port->adapter, 0);
3319                 zfcp_cmd_dbf_event_fsf("porthinv", fsf_req,
3320                                        &fsf_req->qtcb->header.fsf_status_qual,
3321                                        sizeof (union fsf_status_qual));
3322                 fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
3323                 break;
3324
3325         case FSF_LUN_HANDLE_NOT_VALID:
3326                 ZFCP_LOG_INFO("Temporary LUN identifier 0x%x of unit "
3327                               "0x%016Lx on port 0x%016Lx on adapter %s is "
3328                               "invalid. This may happen occasionally.\n",
3329                               unit->handle,
3330                               unit->fcp_lun,
3331                               unit->port->wwpn,
3332                               zfcp_get_busid_by_unit(unit));
3333                 ZFCP_LOG_DEBUG("Status qualifier data:\n");
3334                 ZFCP_HEX_DUMP(ZFCP_LOG_LEVEL_DEBUG,
3335                               (char *) &fsf_req->qtcb->header.fsf_status_qual,
3336                               sizeof (union fsf_status_qual));
3337                 debug_text_event(fsf_req->adapter->erp_dbf, 1,
3338                                  "fsf_s_lhand_nv");
3339                 zfcp_erp_port_reopen(unit->port, 0);
3340                 zfcp_cmd_dbf_event_fsf("lunhinv", fsf_req,
3341                                        &fsf_req->qtcb->header.fsf_status_qual,
3342                                        sizeof (union fsf_status_qual));
3343                 fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
3344                 break;
3345
3346         case FSF_PORT_BOXED:
3347                 ZFCP_LOG_DEBUG("The remote port 0x%016Lx on adapter %s "
3348                                "needs to be reopened\n",
3349                                unit->port->wwpn,
3350                                zfcp_get_busid_by_unit(unit));
3351                 debug_text_event(fsf_req->adapter->erp_dbf, 2, "fsf_s_pboxed");
3352                 zfcp_erp_port_reopen(unit->port, 0);
3353                 fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR |
3354                         ZFCP_STATUS_FSFREQ_RETRY;
3355                 break;
3356
3357         case FSF_ADAPTER_STATUS_AVAILABLE:
3358                 switch (fsf_req->qtcb->header.fsf_status_qual.word[0]) {
3359                 case FSF_SQ_INVOKE_LINK_TEST_PROCEDURE:
3360                         /* re-establish link to port */
3361                         debug_text_event(fsf_req->adapter->erp_dbf, 1,
3362                                          "fsf_sq_ltest");
3363                         zfcp_test_link(unit->port);
3364                         fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
3365                         break;
3366                 case FSF_SQ_ULP_DEPENDENT_ERP_REQUIRED:
3367                         /* ERP strategy will escalate */
3368                         debug_text_event(fsf_req->adapter->erp_dbf, 1,
3369                                          "fsf_sq_ulp");
3370                         fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
3371                         break;
3372                 default:
3373                         ZFCP_LOG_NORMAL
3374                             ("bug: Wrong status qualifier 0x%x arrived.\n",
3375                              fsf_req->qtcb->header.fsf_status_qual.word[0]);
3376                         debug_text_event(fsf_req->adapter->erp_dbf, 0,
3377                                          "fsf_sq_inval:");
3378                         debug_exception(
3379                                 fsf_req->adapter->erp_dbf, 0,
3380                                 &fsf_req->qtcb->header.fsf_status_qual.word[0],
3381                                 sizeof (u32));
3382                         break;
3383                 }
3384                 break;
3385
3386         case FSF_GOOD:
3387                 ZFCP_LOG_TRACE("unit 0x%016Lx on port 0x%016Lx on adapter %s "
3388                                "closed, port handle 0x%x\n",
3389                                unit->fcp_lun,
3390                                unit->port->wwpn,
3391                                zfcp_get_busid_by_unit(unit),
3392                                unit->handle);
3393                 /* mark unit as closed */
3394                 atomic_clear_mask(ZFCP_STATUS_COMMON_OPEN, &unit->status);
3395                 retval = 0;
3396                 break;
3397
3398         default:
3399                 ZFCP_LOG_NORMAL("bug: An unknown FSF Status was presented "
3400                                 "(debug info 0x%x)\n",
3401                                 fsf_req->qtcb->header.fsf_status);
3402                 debug_text_event(fsf_req->adapter->erp_dbf, 0, "fsf_s_inval:");
3403                 debug_exception(fsf_req->adapter->erp_dbf, 0,
3404                                 &fsf_req->qtcb->header.fsf_status,
3405                                 sizeof (u32));
3406                 break;
3407         }
3408
3409  skip_fsfstatus:
3410         atomic_clear_mask(ZFCP_STATUS_COMMON_CLOSING, &unit->status);
3411         return retval;
3412 }
3413
3414 /**
3415  * zfcp_fsf_send_fcp_command_task - initiate an FCP command (for a SCSI command)
3416  * @adapter: adapter where scsi command is issued
3417  * @unit: unit where command is sent to
3418  * @scsi_cmnd: scsi command to be sent
3419  * @timer: timer to be started when request is initiated
3420  * @req_flags: flags for fsf_request
3421  */
3422 int
3423 zfcp_fsf_send_fcp_command_task(struct zfcp_adapter *adapter,
3424                                struct zfcp_unit *unit,
3425                                struct scsi_cmnd * scsi_cmnd,
3426                                struct timer_list *timer, int req_flags)
3427 {
3428         struct zfcp_fsf_req *fsf_req = NULL;
3429         struct fcp_cmnd_iu *fcp_cmnd_iu;
3430         unsigned int sbtype;
3431         unsigned long lock_flags;
3432         int real_bytes = 0;
3433         int retval = 0;
3434         int mask;
3435
3436         /* setup new FSF request */
3437         retval = zfcp_fsf_req_create(adapter, FSF_QTCB_FCP_CMND, req_flags,
3438                                      adapter->pool.fsf_req_scsi,
3439                                      &lock_flags, &fsf_req);
3440         if (unlikely(retval < 0)) {
3441                 ZFCP_LOG_DEBUG("error: Could not create FCP command request "
3442                                "for unit 0x%016Lx on port 0x%016Lx on "
3443                                "adapter %s\n",
3444                                unit->fcp_lun,
3445                                unit->port->wwpn,
3446                                zfcp_get_busid_by_adapter(adapter));
3447                 goto failed_req_create;
3448         }
3449
3450         /*
3451          * associate FSF request with SCSI request
3452          * (need this for look up on abort)
3453          */
3454         fsf_req->data.send_fcp_command_task.fsf_req = fsf_req;
3455         scsi_cmnd->host_scribble = (char *) &(fsf_req->data);
3456
3457         /*
3458          * associate SCSI command with FSF request
3459          * (need this for look up on normal command completion)
3460          */
3461         fsf_req->data.send_fcp_command_task.scsi_cmnd = scsi_cmnd;
3462         fsf_req->data.send_fcp_command_task.start_jiffies = jiffies;
3463         fsf_req->data.send_fcp_command_task.unit = unit;
3464         ZFCP_LOG_DEBUG("unit=%p, fcp_lun=0x%016Lx\n", unit, unit->fcp_lun);
3465
3466         /* set handles of unit and its parent port in QTCB */
3467         fsf_req->qtcb->header.lun_handle = unit->handle;
3468         fsf_req->qtcb->header.port_handle = unit->port->handle;
3469
3470         /* FSF does not define the structure of the FCP_CMND IU */
3471         fcp_cmnd_iu = (struct fcp_cmnd_iu *)
3472             &(fsf_req->qtcb->bottom.io.fcp_cmnd);
3473
3474         /*
3475          * set depending on data direction:
3476          *      data direction bits in SBALE (SB Type)
3477          *      data direction bits in QTCB
3478          *      data direction bits in FCP_CMND IU
3479          */
3480         switch (scsi_cmnd->sc_data_direction) {
3481         case DMA_NONE:
3482                 fsf_req->qtcb->bottom.io.data_direction = FSF_DATADIR_CMND;
3483                 /*
3484                  * FIXME(qdio):
3485                  * what is the correct type for commands
3486                  * without 'real' data buffers?
3487                  */
3488                 sbtype = SBAL_FLAGS0_TYPE_READ;
3489                 break;
3490         case DMA_FROM_DEVICE:
3491                 fsf_req->qtcb->bottom.io.data_direction = FSF_DATADIR_READ;
3492                 sbtype = SBAL_FLAGS0_TYPE_READ;
3493                 fcp_cmnd_iu->rddata = 1;
3494                 break;
3495         case DMA_TO_DEVICE:
3496                 fsf_req->qtcb->bottom.io.data_direction = FSF_DATADIR_WRITE;
3497                 sbtype = SBAL_FLAGS0_TYPE_WRITE;
3498                 fcp_cmnd_iu->wddata = 1;
3499                 break;
3500         case DMA_BIDIRECTIONAL:
3501         default:
3502                 /*
3503                  * dummy, catch this condition earlier
3504                  * in zfcp_scsi_queuecommand
3505                  */
3506                 goto failed_scsi_cmnd;
3507         }
3508
3509         /* set FC service class in QTCB (3 per default) */
3510         fsf_req->qtcb->bottom.io.service_class = adapter->fc_service_class;
3511
3512         /* set FCP_LUN in FCP_CMND IU in QTCB */
3513         fcp_cmnd_iu->fcp_lun = unit->fcp_lun;
3514
3515         mask = ZFCP_STATUS_UNIT_READONLY | ZFCP_STATUS_UNIT_SHARED;
3516
3517         /* set task attributes in FCP_CMND IU in QTCB */
3518         if (likely((scsi_cmnd->device->simple_tags) ||
3519                    (atomic_test_mask(mask, &unit->status))))
3520                 fcp_cmnd_iu->task_attribute = SIMPLE_Q;
3521         else
3522                 fcp_cmnd_iu->task_attribute = UNTAGGED;
3523
3524         /* set additional length of FCP_CDB in FCP_CMND IU in QTCB, if needed */
3525         if (unlikely(scsi_cmnd->cmd_len > FCP_CDB_LENGTH)) {
3526                 fcp_cmnd_iu->add_fcp_cdb_length
3527                     = (scsi_cmnd->cmd_len - FCP_CDB_LENGTH) >> 2;
3528                 ZFCP_LOG_TRACE("SCSI CDB length is 0x%x, "
3529                                "additional FCP_CDB length is 0x%x "
3530                                "(shifted right 2 bits)\n",
3531                                scsi_cmnd->cmd_len,
3532                                fcp_cmnd_iu->add_fcp_cdb_length);
3533         }
3534         /*
3535          * copy SCSI CDB (including additional length, if any) to
3536          * FCP_CDB in FCP_CMND IU in QTCB
3537          */
3538         memcpy(fcp_cmnd_iu->fcp_cdb, scsi_cmnd->cmnd, scsi_cmnd->cmd_len);
3539
3540         /* FCP CMND IU length in QTCB */
3541         fsf_req->qtcb->bottom.io.fcp_cmnd_length =
3542                 sizeof (struct fcp_cmnd_iu) +
3543                 fcp_cmnd_iu->add_fcp_cdb_length + sizeof (fcp_dl_t);
3544
3545         /* generate SBALEs from data buffer */
3546         real_bytes = zfcp_qdio_sbals_from_scsicmnd(fsf_req, sbtype, scsi_cmnd);
3547         if (unlikely(real_bytes < 0)) {
3548                 if (fsf_req->sbal_number < ZFCP_MAX_SBALS_PER_REQ) {
3549                         ZFCP_LOG_DEBUG(
3550                                 "Data did not fit into available buffer(s), "
3551                                "waiting for more...\n");
3552                 retval = -EIO;
3553         } else {
3554                 ZFCP_LOG_NORMAL("error: No truncation implemented but "
3555                                 "required. Shutting down unit "
3556                                 "(adapter %s, port 0x%016Lx, "
3557                                 "unit 0x%016Lx)\n",
3558                                 zfcp_get_busid_by_unit(unit),
3559                                 unit->port->wwpn,
3560                                 unit->fcp_lun);
3561                 zfcp_erp_unit_shutdown(unit, 0);
3562                 retval = -EINVAL;
3563                 }
3564                 goto no_fit;
3565         }
3566
3567         /* set length of FCP data length in FCP_CMND IU in QTCB */
3568         zfcp_set_fcp_dl(fcp_cmnd_iu, real_bytes);
3569
3570         ZFCP_LOG_DEBUG("Sending SCSI command:\n");
3571         ZFCP_HEX_DUMP(ZFCP_LOG_LEVEL_DEBUG,
3572                       (char *) scsi_cmnd->cmnd, scsi_cmnd->cmd_len);
3573
3574         /*
3575          * start QDIO request for this FSF request
3576          *  covered by an SBALE)
3577          */
3578         retval = zfcp_fsf_req_send(fsf_req, timer);
3579         if (unlikely(retval < 0)) {
3580                 ZFCP_LOG_INFO("error: Could not send FCP command request "
3581                               "on adapter %s, port 0x%016Lx, unit 0x%016Lx\n",
3582                               zfcp_get_busid_by_adapter(adapter),
3583                               unit->port->wwpn,
3584                               unit->fcp_lun);
3585                 goto send_failed;
3586         }
3587
3588         ZFCP_LOG_TRACE("Send FCP Command initiated (adapter %s, "
3589                        "port 0x%016Lx, unit 0x%016Lx)\n",
3590                        zfcp_get_busid_by_adapter(adapter),
3591                        unit->port->wwpn,
3592                        unit->fcp_lun);
3593         goto success;
3594
3595  send_failed:
3596  no_fit:
3597  failed_scsi_cmnd:
3598         zfcp_fsf_req_free(fsf_req);
3599         fsf_req = NULL;
3600         scsi_cmnd->host_scribble = NULL;
3601  success:
3602  failed_req_create:
3603         write_unlock_irqrestore(&adapter->request_queue.queue_lock, lock_flags);
3604         return retval;
3605 }
3606
3607 /*
3608  * function:    zfcp_fsf_send_fcp_command_task_management
3609  *
3610  * purpose:
3611  *
3612  * returns:
3613  *
3614  * FIXME(design): should be watched by a timeout!!!
3615  * FIXME(design) shouldn't this be modified to return an int
3616  *               also...don't know how though
3617  *
3618  */
3619 struct zfcp_fsf_req *
3620 zfcp_fsf_send_fcp_command_task_management(struct zfcp_adapter *adapter,
3621                                           struct zfcp_unit *unit,
3622                                           u8 tm_flags, int req_flags)
3623 {
3624         struct zfcp_fsf_req *fsf_req = NULL;
3625         int retval = 0;
3626         struct fcp_cmnd_iu *fcp_cmnd_iu;
3627         unsigned long lock_flags;
3628         volatile struct qdio_buffer_element *sbale;
3629
3630         /* setup new FSF request */
3631         retval = zfcp_fsf_req_create(adapter, FSF_QTCB_FCP_CMND, req_flags,
3632                                      adapter->pool.fsf_req_scsi,
3633                                      &lock_flags, &fsf_req);
3634         if (retval < 0) {
3635                 ZFCP_LOG_INFO("error: Could not create FCP command (task "
3636                               "management) request for adapter %s, port "
3637                               " 0x%016Lx, unit 0x%016Lx.\n",
3638                               zfcp_get_busid_by_adapter(adapter),
3639                               unit->port->wwpn, unit->fcp_lun);
3640                 goto out;
3641         }
3642
3643         /*
3644          * Used to decide on proper handler in the return path,
3645          * could be either zfcp_fsf_send_fcp_command_task_handler or
3646          * zfcp_fsf_send_fcp_command_task_management_handler */
3647
3648         fsf_req->status |= ZFCP_STATUS_FSFREQ_TASK_MANAGEMENT;
3649
3650         /*
3651          * hold a pointer to the unit being target of this
3652          * task management request
3653          */
3654         fsf_req->data.send_fcp_command_task_management.unit = unit;
3655
3656         /* set FSF related fields in QTCB */
3657         fsf_req->qtcb->header.lun_handle = unit->handle;
3658         fsf_req->qtcb->header.port_handle = unit->port->handle;
3659         fsf_req->qtcb->bottom.io.data_direction = FSF_DATADIR_CMND;
3660         fsf_req->qtcb->bottom.io.service_class = adapter->fc_service_class;
3661         fsf_req->qtcb->bottom.io.fcp_cmnd_length =
3662                 sizeof (struct fcp_cmnd_iu) + sizeof (fcp_dl_t);
3663
3664         sbale = zfcp_qdio_sbale_req(fsf_req, fsf_req->sbal_curr, 0);
3665         sbale[0].flags |= SBAL_FLAGS0_TYPE_WRITE;
3666         sbale[1].flags |= SBAL_FLAGS_LAST_ENTRY;
3667
3668         /* set FCP related fields in FCP_CMND IU in QTCB */
3669         fcp_cmnd_iu = (struct fcp_cmnd_iu *)
3670                 &(fsf_req->qtcb->bottom.io.fcp_cmnd);
3671         fcp_cmnd_iu->fcp_lun = unit->fcp_lun;
3672         fcp_cmnd_iu->task_management_flags = tm_flags;
3673
3674         /* start QDIO request for this FSF request */
3675         zfcp_fsf_start_scsi_er_timer(adapter);
3676         retval = zfcp_fsf_req_send(fsf_req, NULL);
3677         if (retval) {
3678                 del_timer(&adapter->scsi_er_timer);
3679                 ZFCP_LOG_INFO("error: Could not send an FCP-command (task "
3680                               "management) on adapter %s, port 0x%016Lx for "
3681                               "unit LUN 0x%016Lx\n",
3682                               zfcp_get_busid_by_adapter(adapter),
3683                               unit->port->wwpn,
3684                               unit->fcp_lun);
3685                 zfcp_fsf_req_free(fsf_req);
3686                 fsf_req = NULL;
3687                 goto out;
3688         }
3689
3690         ZFCP_LOG_TRACE("Send FCP Command (task management function) initiated "
3691                        "(adapter %s, port 0x%016Lx, unit 0x%016Lx, "
3692                        "tm_flags=0x%x)\n",
3693                        zfcp_get_busid_by_adapter(adapter),
3694                        unit->port->wwpn,
3695                        unit->fcp_lun,
3696                        tm_flags);
3697  out:
3698         write_unlock_irqrestore(&adapter->request_queue.queue_lock, lock_flags);
3699         return fsf_req;
3700 }
3701
3702 /*
3703  * function:    zfcp_fsf_send_fcp_command_handler
3704  *
3705  * purpose:     is called for finished Send FCP Command
3706  *
3707  * returns:     
3708  */
3709 static int
3710 zfcp_fsf_send_fcp_command_handler(struct zfcp_fsf_req *fsf_req)
3711 {
3712         int retval = -EINVAL;
3713         struct zfcp_unit *unit;
3714         struct fsf_qtcb_header *header;
3715         u16 subtable, rule, counter;
3716
3717         header = &fsf_req->qtcb->header;
3718
3719         if (unlikely(fsf_req->status & ZFCP_STATUS_FSFREQ_TASK_MANAGEMENT))
3720                 unit = fsf_req->data.send_fcp_command_task_management.unit;
3721         else
3722                 unit = fsf_req->data.send_fcp_command_task.unit;
3723
3724         if (unlikely(fsf_req->status & ZFCP_STATUS_FSFREQ_ERROR)) {
3725                 /* go directly to calls of special handlers */
3726                 goto skip_fsfstatus;
3727         }
3728
3729         /* evaluate FSF status in QTCB */
3730         switch (header->fsf_status) {
3731
3732         case FSF_PORT_HANDLE_NOT_VALID:
3733                 ZFCP_LOG_INFO("Temporary port identifier 0x%x for port "
3734                               "0x%016Lx on adapter %s invalid\n",
3735                               unit->port->handle,
3736                               unit->port->wwpn, zfcp_get_busid_by_unit(unit));
3737                 ZFCP_HEX_DUMP(ZFCP_LOG_LEVEL_DEBUG,
3738                               (char *) &header->fsf_status_qual,
3739                               sizeof (union fsf_status_qual));
3740                 debug_text_event(fsf_req->adapter->erp_dbf, 1,
3741                                  "fsf_s_phand_nv");
3742                 zfcp_erp_adapter_reopen(unit->port->adapter, 0);
3743                 fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
3744                 break;
3745
3746         case FSF_LUN_HANDLE_NOT_VALID:
3747                 ZFCP_LOG_INFO("Temporary LUN identifier 0x%x for unit "
3748                               "0x%016Lx on port 0x%016Lx on adapter %s is "
3749                               "invalid. This may happen occasionally.\n",
3750                               unit->handle,
3751                               unit->fcp_lun,
3752                               unit->port->wwpn,
3753                               zfcp_get_busid_by_unit(unit));
3754                 ZFCP_LOG_NORMAL("Status qualifier data:\n");
3755                 ZFCP_HEX_DUMP(ZFCP_LOG_LEVEL_NORMAL,
3756                               (char *) &header->fsf_status_qual,
3757                               sizeof (union fsf_status_qual));
3758                 debug_text_event(fsf_req->adapter->erp_dbf, 1,
3759                                  "fsf_s_uhand_nv");
3760                 zfcp_erp_port_reopen(unit->port, 0);
3761                 fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
3762                 break;
3763
3764         case FSF_HANDLE_MISMATCH:
3765                 ZFCP_LOG_NORMAL("bug: The port handle 0x%x has changed "
3766                                 "unexpectedly. (adapter %s, port 0x%016Lx, "
3767                                 "unit 0x%016Lx)\n",
3768                                 unit->port->handle,
3769                                 zfcp_get_busid_by_unit(unit),
3770                                 unit->port->wwpn,
3771                                 unit->fcp_lun);
3772                 ZFCP_LOG_NORMAL("status qualifier:\n");
3773                 ZFCP_HEX_DUMP(ZFCP_LOG_LEVEL_NORMAL,
3774                               (char *) &header->fsf_status_qual,
3775                               sizeof (union fsf_status_qual));
3776                 debug_text_event(fsf_req->adapter->erp_dbf, 1,
3777                                  "fsf_s_hand_mis");
3778                 zfcp_erp_adapter_reopen(unit->port->adapter, 0);
3779                 zfcp_cmd_dbf_event_fsf("handmism",
3780                                        fsf_req,
3781                                        &header->fsf_status_qual,
3782                                        sizeof (union fsf_status_qual));
3783                 fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
3784                 break;
3785
3786         case FSF_SERVICE_CLASS_NOT_SUPPORTED:
3787                 if (fsf_req->adapter->fc_service_class <= 3) {
3788                         ZFCP_LOG_NORMAL("error: The adapter %s does "
3789                                         "not support fibrechannel class %d.\n",
3790                                         zfcp_get_busid_by_unit(unit),
3791                                         fsf_req->adapter->fc_service_class);
3792                 } else {
3793                         ZFCP_LOG_NORMAL("bug: The fibrechannel class at "
3794                                         "adapter %s is invalid. "
3795                                         "(debug info %d)\n",
3796                                         zfcp_get_busid_by_unit(unit),
3797                                         fsf_req->adapter->fc_service_class);
3798                 }
3799                 /* stop operation for this adapter */
3800                 debug_text_exception(fsf_req->adapter->erp_dbf, 0,
3801                                      "fsf_s_class_nsup");
3802                 zfcp_erp_adapter_shutdown(unit->port->adapter, 0);
3803                 zfcp_cmd_dbf_event_fsf("unsclass",
3804                                        fsf_req,
3805                                        &header->fsf_status_qual,
3806                                        sizeof (union fsf_status_qual));
3807                 fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
3808                 break;
3809
3810         case FSF_FCPLUN_NOT_VALID:
3811                 ZFCP_LOG_NORMAL("bug: unit 0x%016Lx on port 0x%016Lx on "
3812                                 "adapter %s does not have correct unit "
3813                                 "handle 0x%x\n",
3814                                 unit->fcp_lun,
3815                                 unit->port->wwpn,
3816                                 zfcp_get_busid_by_unit(unit),
3817                                 unit->handle);
3818                 ZFCP_LOG_DEBUG("status qualifier:\n");
3819                 ZFCP_HEX_DUMP(ZFCP_LOG_LEVEL_DEBUG,
3820                               (char *) &header->fsf_status_qual,
3821                               sizeof (union fsf_status_qual));
3822                 debug_text_event(fsf_req->adapter->erp_dbf, 1,
3823                                  "fsf_s_fcp_lun_nv");
3824                 zfcp_erp_port_reopen(unit->port, 0);
3825                 zfcp_cmd_dbf_event_fsf("fluninv",
3826                                        fsf_req,
3827                                        &header->fsf_status_qual,
3828                                        sizeof (union fsf_status_qual));
3829                 fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
3830                 break;
3831
3832         case FSF_ACCESS_DENIED:
3833                 ZFCP_LOG_NORMAL("Access denied, cannot send FCP command to "
3834                                 "unit 0x%016Lx on port 0x%016Lx on "
3835                                 "adapter %s\n", unit->fcp_lun, unit->port->wwpn,
3836                                 zfcp_get_busid_by_unit(unit));
3837                 for (counter = 0; counter < 2; counter++) {
3838                         subtable = header->fsf_status_qual.halfword[counter * 2];
3839                         rule = header->fsf_status_qual.halfword[counter * 2 + 1];
3840                         switch (subtable) {
3841                         case FSF_SQ_CFDC_SUBTABLE_OS:
3842                         case FSF_SQ_CFDC_SUBTABLE_PORT_WWPN:
3843                         case FSF_SQ_CFDC_SUBTABLE_PORT_DID:
3844                         case FSF_SQ_CFDC_SUBTABLE_LUN:
3845                                 ZFCP_LOG_INFO("Access denied (%s rule %d)\n",
3846                                         zfcp_act_subtable_type[subtable], rule);
3847                                 break;
3848                         }
3849                 }
3850                 debug_text_event(fsf_req->adapter->erp_dbf, 1, "fsf_s_access");
3851                 zfcp_erp_unit_access_denied(unit);
3852                 fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
3853                 break;
3854
3855         case FSF_DIRECTION_INDICATOR_NOT_VALID:
3856                 ZFCP_LOG_INFO("bug: Invalid data direction given for unit "
3857                               "0x%016Lx on port 0x%016Lx on adapter %s "
3858                               "(debug info %d)\n",
3859                               unit->fcp_lun,
3860                               unit->port->wwpn,
3861                               zfcp_get_busid_by_unit(unit),
3862                               fsf_req->qtcb->bottom.io.data_direction);
3863                 /* stop operation for this adapter */
3864                 debug_text_event(fsf_req->adapter->erp_dbf, 0,
3865                                  "fsf_s_dir_ind_nv");
3866                 zfcp_erp_adapter_shutdown(unit->port->adapter, 0);
3867                 zfcp_cmd_dbf_event_fsf("dirinv",
3868                                        fsf_req,
3869                                        &header->fsf_status_qual,
3870                                        sizeof (union fsf_status_qual));
3871                 fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
3872                 break;
3873
3874         case FSF_CMND_LENGTH_NOT_VALID:
3875                 ZFCP_LOG_NORMAL
3876                     ("bug: An invalid control-data-block length field "
3877                      "was found in a command for unit 0x%016Lx on port "
3878                      "0x%016Lx on adapter %s " "(debug info %d)\n",
3879                      unit->fcp_lun, unit->port->wwpn,
3880                      zfcp_get_busid_by_unit(unit),
3881                      fsf_req->qtcb->bottom.io.fcp_cmnd_length);
3882                 /* stop operation for this adapter */
3883                 debug_text_event(fsf_req->adapter->erp_dbf, 0,
3884                                  "fsf_s_cmd_len_nv");
3885                 zfcp_erp_adapter_shutdown(unit->port->adapter, 0);
3886                 zfcp_cmd_dbf_event_fsf("cleninv",
3887                                        fsf_req,
3888                                        &header->fsf_status_qual,
3889                                        sizeof (union fsf_status_qual));
3890                 fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
3891                 break;
3892
3893         case FSF_PORT_BOXED:
3894                 ZFCP_LOG_DEBUG("The remote port 0x%016Lx on adapter %s "
3895                                "needs to be reopened\n",
3896                                unit->port->wwpn, zfcp_get_busid_by_unit(unit));
3897                 debug_text_event(fsf_req->adapter->erp_dbf, 2, "fsf_s_pboxed");
3898                 zfcp_erp_port_reopen(unit->port, 0);
3899                 zfcp_cmd_dbf_event_fsf("portbox", fsf_req,
3900                                        &header->fsf_status_qual,
3901                                        sizeof (union fsf_status_qual));
3902                 fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR |
3903                         ZFCP_STATUS_FSFREQ_RETRY;
3904                 break;
3905
3906         case FSF_LUN_BOXED:
3907                 ZFCP_LOG_NORMAL("unit needs to be reopened (adapter %s, "
3908                                 "wwpn=0x%016Lx, fcp_lun=0x%016Lx)\n",
3909                                 zfcp_get_busid_by_unit(unit),
3910                                 unit->port->wwpn, unit->fcp_lun);
3911                 debug_text_event(fsf_req->adapter->erp_dbf, 1, "fsf_s_lboxed");
3912                 zfcp_erp_unit_reopen(unit, 0);
3913                 zfcp_cmd_dbf_event_fsf("unitbox", fsf_req,
3914                                        &header->fsf_status_qual,
3915                                        sizeof(union fsf_status_qual));
3916                 fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR
3917                         | ZFCP_STATUS_FSFREQ_RETRY;
3918                 break;
3919
3920         case FSF_ADAPTER_STATUS_AVAILABLE:
3921                 switch (header->fsf_status_qual.word[0]) {
3922                 case FSF_SQ_INVOKE_LINK_TEST_PROCEDURE:
3923                         /* re-establish link to port */
3924                         debug_text_event(fsf_req->adapter->erp_dbf, 1,
3925                                          "fsf_sq_ltest");
3926                         zfcp_test_link(unit->port);
3927                         break;
3928                 case FSF_SQ_ULP_DEPENDENT_ERP_REQUIRED:
3929                         /* FIXME(hw) need proper specs for proper action */
3930                         /* let scsi stack deal with retries and escalation */
3931                         debug_text_event(fsf_req->adapter->erp_dbf, 1,
3932                                          "fsf_sq_ulp");
3933                         break;
3934                 default:
3935                         ZFCP_LOG_NORMAL
3936                             ("Unknown status qualifier 0x%x arrived.\n",
3937                              header->fsf_status_qual.word[0]);
3938                         debug_text_event(fsf_req->adapter->erp_dbf, 0,
3939                                          "fsf_sq_inval:");
3940                         debug_exception(fsf_req->adapter->erp_dbf, 0,
3941                                         &header->fsf_status_qual.word[0],
3942                                         sizeof(u32));
3943                         break;
3944                 }
3945                 fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
3946                 break;
3947
3948         case FSF_GOOD:
3949                 break;
3950
3951         case FSF_FCP_RSP_AVAILABLE:
3952                 break;
3953
3954         default:
3955                 debug_text_event(fsf_req->adapter->erp_dbf, 0, "fsf_s_inval:");
3956                 debug_exception(fsf_req->adapter->erp_dbf, 0,
3957                                 &header->fsf_status, sizeof(u32));
3958                 break;
3959         }
3960
3961  skip_fsfstatus:
3962         if (fsf_req->status & ZFCP_STATUS_FSFREQ_TASK_MANAGEMENT) {
3963                 retval =
3964                     zfcp_fsf_send_fcp_command_task_management_handler(fsf_req);
3965         } else {
3966                 retval = zfcp_fsf_send_fcp_command_task_handler(fsf_req);
3967         }
3968         return retval;
3969 }
3970
3971 /*
3972  * function:    zfcp_fsf_send_fcp_command_task_handler
3973  *
3974  * purpose:     evaluates FCP_RSP IU
3975  *
3976  * returns:     
3977  */
3978 static int
3979 zfcp_fsf_send_fcp_command_task_handler(struct zfcp_fsf_req *fsf_req)
3980 {
3981         int retval = 0;
3982         struct scsi_cmnd *scpnt;
3983         struct fcp_rsp_iu *fcp_rsp_iu = (struct fcp_rsp_iu *)
3984             &(fsf_req->qtcb->bottom.io.fcp_rsp);
3985         struct fcp_cmnd_iu *fcp_cmnd_iu = (struct fcp_cmnd_iu *)
3986             &(fsf_req->qtcb->bottom.io.fcp_cmnd);
3987         u32 sns_len;
3988         char *fcp_rsp_info = zfcp_get_fcp_rsp_info_ptr(fcp_rsp_iu);
3989         unsigned long flags;
3990         struct zfcp_unit *unit = fsf_req->data.send_fcp_command_task.unit;
3991
3992         read_lock_irqsave(&fsf_req->adapter->abort_lock, flags);
3993         scpnt = fsf_req->data.send_fcp_command_task.scsi_cmnd;
3994         if (unlikely(!scpnt)) {
3995                 ZFCP_LOG_DEBUG
3996                     ("Command with fsf_req %p is not associated to "
3997                      "a scsi command anymore. Aborted?\n", fsf_req);
3998                 goto out;
3999         }
4000         if (unlikely(fsf_req->status & ZFCP_STATUS_FSFREQ_ABORTED)) {
4001                 /* FIXME: (design) mid-layer should handle DID_ABORT like
4002                  *        DID_SOFT_ERROR by retrying the request for devices
4003                  *        that allow retries.
4004                  */
4005                 ZFCP_LOG_DEBUG("Setting DID_SOFT_ERROR and SUGGEST_RETRY\n");
4006                 set_host_byte(&scpnt->result, DID_SOFT_ERROR);
4007                 set_driver_byte(&scpnt->result, SUGGEST_RETRY);
4008                 goto skip_fsfstatus;
4009         }
4010
4011         if (unlikely(fsf_req->status & ZFCP_STATUS_FSFREQ_ERROR)) {
4012                 ZFCP_LOG_DEBUG("Setting DID_ERROR\n");
4013                 set_host_byte(&scpnt->result, DID_ERROR);
4014                 goto skip_fsfstatus;
4015         }
4016
4017         /* set message byte of result in SCSI command */
4018         scpnt->result |= COMMAND_COMPLETE << 8;
4019
4020         /*
4021          * copy SCSI status code of FCP_STATUS of FCP_RSP IU to status byte
4022          * of result in SCSI command
4023          */
4024         scpnt->result |= fcp_rsp_iu->scsi_status;
4025         if (unlikely(fcp_rsp_iu->scsi_status)) {
4026                 /* DEBUG */
4027                 ZFCP_LOG_DEBUG("status for SCSI Command:\n");
4028                 ZFCP_HEX_DUMP(ZFCP_LOG_LEVEL_DEBUG,
4029                               scpnt->cmnd, scpnt->cmd_len);
4030                 ZFCP_LOG_DEBUG("SCSI status code 0x%x\n",
4031                                 fcp_rsp_iu->scsi_status);
4032                 ZFCP_HEX_DUMP(ZFCP_LOG_LEVEL_DEBUG,
4033                               (void *) fcp_rsp_iu, sizeof (struct fcp_rsp_iu));
4034                 ZFCP_HEX_DUMP(ZFCP_LOG_LEVEL_DEBUG,
4035                               zfcp_get_fcp_sns_info_ptr(fcp_rsp_iu),
4036                               fcp_rsp_iu->fcp_sns_len);
4037         }
4038
4039         /* check FCP_RSP_INFO */
4040         if (unlikely(fcp_rsp_iu->validity.bits.fcp_rsp_len_valid)) {
4041                 ZFCP_LOG_DEBUG("rsp_len is valid\n");
4042                 switch (fcp_rsp_info[3]) {
4043                 case RSP_CODE_GOOD:
4044                         /* ok, continue */
4045                         ZFCP_LOG_TRACE("no failure or Task Management "
4046                                        "Function complete\n");
4047                         set_host_byte(&scpnt->result, DID_OK);
4048                         break;
4049                 case RSP_CODE_LENGTH_MISMATCH:
4050                         /* hardware bug */
4051                         ZFCP_LOG_NORMAL("bug: FCP response code indictates "
4052                                         "that the fibrechannel protocol data "
4053                                         "length differs from the burst length. "
4054                                         "The problem occured on unit 0x%016Lx "
4055                                         "on port 0x%016Lx on adapter %s",
4056                                         unit->fcp_lun,
4057                                         unit->port->wwpn,
4058                                         zfcp_get_busid_by_unit(unit));
4059                         /* dump SCSI CDB as prepared by zfcp */
4060                         ZFCP_HEX_DUMP(ZFCP_LOG_LEVEL_DEBUG,
4061                                       (char *) &fsf_req->qtcb->
4062                                       bottom.io.fcp_cmnd, FSF_FCP_CMND_SIZE);
4063                         zfcp_cmd_dbf_event_fsf("clenmis", fsf_req, NULL, 0);
4064                         set_host_byte(&scpnt->result, DID_ERROR);
4065                         goto skip_fsfstatus;
4066                 case RSP_CODE_FIELD_INVALID:
4067                         /* driver or hardware bug */
4068                         ZFCP_LOG_NORMAL("bug: FCP response code indictates "
4069                                         "that the fibrechannel protocol data "
4070                                         "fields were incorrectly set up. "
4071                                         "The problem occured on the unit "
4072                                         "0x%016Lx on port 0x%016Lx on "
4073                                         "adapter %s",
4074                                         unit->fcp_lun,
4075                                         unit->port->wwpn,
4076                                         zfcp_get_busid_by_unit(unit));
4077                         /* dump SCSI CDB as prepared by zfcp */
4078                         ZFCP_HEX_DUMP(ZFCP_LOG_LEVEL_DEBUG,
4079                                       (char *) &fsf_req->qtcb->
4080                                       bottom.io.fcp_cmnd, FSF_FCP_CMND_SIZE);
4081                         set_host_byte(&scpnt->result, DID_ERROR);
4082                         zfcp_cmd_dbf_event_fsf("codeinv", fsf_req, NULL, 0);
4083                         goto skip_fsfstatus;
4084                 case RSP_CODE_RO_MISMATCH:
4085                         /* hardware bug */
4086                         ZFCP_LOG_NORMAL("bug: The FCP response code indicates "
4087                                         "that conflicting  values for the "
4088                                         "fibrechannel payload offset from the "
4089                                         "header were found. "
4090                                         "The problem occured on unit 0x%016Lx "
4091                                         "on port 0x%016Lx on adapter %s.\n",
4092                                         unit->fcp_lun,
4093                                         unit->port->wwpn,
4094                                         zfcp_get_busid_by_unit(unit));
4095                         /* dump SCSI CDB as prepared by zfcp */
4096                         ZFCP_HEX_DUMP(ZFCP_LOG_LEVEL_DEBUG,
4097                                       (char *) &fsf_req->qtcb->
4098                                       bottom.io.fcp_cmnd, FSF_FCP_CMND_SIZE);
4099                         zfcp_cmd_dbf_event_fsf("codemism", fsf_req, NULL, 0);
4100                         set_host_byte(&scpnt->result, DID_ERROR);
4101                         goto skip_fsfstatus;
4102                 default:
4103                         ZFCP_LOG_NORMAL("bug: An invalid FCP response "
4104                                         "code was detected for a command. "
4105                                         "The problem occured on the unit "
4106                                         "0x%016Lx on port 0x%016Lx on "
4107                                         "adapter %s (debug info 0x%x)\n",
4108                                         unit->fcp_lun,
4109                                         unit->port->wwpn,
4110                                         zfcp_get_busid_by_unit(unit),
4111                                         fcp_rsp_info[3]);
4112                         /* dump SCSI CDB as prepared by zfcp */
4113                         ZFCP_HEX_DUMP(ZFCP_LOG_LEVEL_DEBUG,
4114                                       (char *) &fsf_req->qtcb->
4115                                       bottom.io.fcp_cmnd, FSF_FCP_CMND_SIZE);
4116                         zfcp_cmd_dbf_event_fsf("undeffcp", fsf_req, NULL, 0);
4117                         set_host_byte(&scpnt->result, DID_ERROR);
4118                         goto skip_fsfstatus;
4119                 }
4120         }
4121
4122         /* check for sense data */
4123         if (unlikely(fcp_rsp_iu->validity.bits.fcp_sns_len_valid)) {
4124                 sns_len = FSF_FCP_RSP_SIZE -
4125                     sizeof (struct fcp_rsp_iu) + fcp_rsp_iu->fcp_rsp_len;
4126                 ZFCP_LOG_TRACE("room for %i bytes sense data in QTCB\n",
4127                                sns_len);
4128                 sns_len = min(sns_len, (u32) SCSI_SENSE_BUFFERSIZE);
4129                 ZFCP_LOG_TRACE("room for %i bytes sense data in SCSI command\n",
4130                                SCSI_SENSE_BUFFERSIZE);
4131                 sns_len = min(sns_len, fcp_rsp_iu->fcp_sns_len);
4132                 ZFCP_LOG_TRACE("scpnt->result =0x%x, command was:\n",
4133                                scpnt->result);
4134                 ZFCP_HEX_DUMP(ZFCP_LOG_LEVEL_TRACE,
4135                               (void *) &scpnt->cmnd, scpnt->cmd_len);
4136
4137                 ZFCP_LOG_TRACE("%i bytes sense data provided by FCP\n",
4138                                fcp_rsp_iu->fcp_sns_len);
4139                 memcpy(&scpnt->sense_buffer,
4140                        zfcp_get_fcp_sns_info_ptr(fcp_rsp_iu), sns_len);
4141                 ZFCP_HEX_DUMP(ZFCP_LOG_LEVEL_TRACE,
4142                               (void *) &scpnt->sense_buffer, sns_len);
4143         }
4144
4145         /* check for overrun */
4146         if (unlikely(fcp_rsp_iu->validity.bits.fcp_resid_over)) {
4147                 ZFCP_LOG_INFO("A data overrun was detected for a command. "
4148                               "unit 0x%016Lx, port 0x%016Lx, adapter %s. "
4149                               "The response data length is "
4150                               "%d, the original length was %d.\n",
4151                               unit->fcp_lun,
4152                               unit->port->wwpn,
4153                               zfcp_get_busid_by_unit(unit),
4154                               fcp_rsp_iu->fcp_resid,
4155                               (int) zfcp_get_fcp_dl(fcp_cmnd_iu));
4156         }
4157
4158         /* check for underrun */
4159         if (unlikely(fcp_rsp_iu->validity.bits.fcp_resid_under)) {
4160                 ZFCP_LOG_INFO("A data underrun was detected for a command. "
4161                               "unit 0x%016Lx, port 0x%016Lx, adapter %s. "
4162                               "The response data length is "
4163                               "%d, the original length was %d.\n",
4164                               unit->fcp_lun,
4165                               unit->port->wwpn,
4166                               zfcp_get_busid_by_unit(unit),
4167                               fcp_rsp_iu->fcp_resid,
4168                               (int) zfcp_get_fcp_dl(fcp_cmnd_iu));
4169
4170                 scpnt->resid = fcp_rsp_iu->fcp_resid;
4171                 if (scpnt->request_bufflen - scpnt->resid < scpnt->underflow)
4172                         set_host_byte(&scpnt->result, DID_ERROR);
4173         }
4174
4175  skip_fsfstatus:
4176         ZFCP_LOG_DEBUG("scpnt->result =0x%x\n", scpnt->result);
4177
4178         zfcp_cmd_dbf_event_scsi("response", scpnt);
4179
4180         /* cleanup pointer (need this especially for abort) */
4181         scpnt->host_scribble = NULL;
4182
4183         /*
4184          * NOTE:
4185          * according to the outcome of a discussion on linux-scsi we
4186          * don't need to grab the io_request_lock here since we use
4187          * the new eh
4188          */
4189         /* always call back */
4190
4191         (scpnt->scsi_done) (scpnt);
4192
4193         /*
4194          * We must hold this lock until scsi_done has been called.
4195          * Otherwise we may call scsi_done after abort regarding this
4196          * command has completed.
4197          * Note: scsi_done must not block!
4198          */
4199  out:
4200         read_unlock_irqrestore(&fsf_req->adapter->abort_lock, flags);
4201         return retval;
4202 }
4203
4204 /*
4205  * function:    zfcp_fsf_send_fcp_command_task_management_handler
4206  *
4207  * purpose:     evaluates FCP_RSP IU
4208  *
4209  * returns:     
4210  */
4211 static int
4212 zfcp_fsf_send_fcp_command_task_management_handler(struct zfcp_fsf_req *fsf_req)
4213 {
4214         int retval = 0;
4215         struct fcp_rsp_iu *fcp_rsp_iu = (struct fcp_rsp_iu *)
4216             &(fsf_req->qtcb->bottom.io.fcp_rsp);
4217         char *fcp_rsp_info = zfcp_get_fcp_rsp_info_ptr(fcp_rsp_iu);
4218         struct zfcp_unit *unit =
4219             fsf_req->data.send_fcp_command_task_management.unit;
4220
4221         del_timer(&fsf_req->adapter->scsi_er_timer);
4222         if (fsf_req->status & ZFCP_STATUS_FSFREQ_ERROR) {
4223                 fsf_req->status |= ZFCP_STATUS_FSFREQ_TMFUNCFAILED;
4224                 goto skip_fsfstatus;
4225         }
4226
4227         /* check FCP_RSP_INFO */
4228         switch (fcp_rsp_info[3]) {
4229         case RSP_CODE_GOOD:
4230                 /* ok, continue */
4231                 ZFCP_LOG_DEBUG("no failure or Task Management "
4232                                "Function complete\n");
4233                 break;
4234         case RSP_CODE_TASKMAN_UNSUPP:
4235                 ZFCP_LOG_NORMAL("bug: A reuested task management function "
4236                                 "is not supported on the target device "
4237                                 "unit 0x%016Lx, port 0x%016Lx, adapter %s\n ",
4238                                 unit->fcp_lun,
4239                                 unit->port->wwpn,
4240                                 zfcp_get_busid_by_unit(unit));
4241                 fsf_req->status |= ZFCP_STATUS_FSFREQ_TMFUNCNOTSUPP;
4242                 break;
4243         case RSP_CODE_TASKMAN_FAILED:
4244                 ZFCP_LOG_NORMAL("bug: A reuested task management function "
4245                                 "failed to complete successfully. "
4246                                 "unit 0x%016Lx, port 0x%016Lx, adapter %s.\n",
4247                                 unit->fcp_lun,
4248                                 unit->port->wwpn,
4249                                 zfcp_get_busid_by_unit(unit));
4250                 fsf_req->status |= ZFCP_STATUS_FSFREQ_TMFUNCFAILED;
4251                 break;
4252         default:
4253                 ZFCP_LOG_NORMAL("bug: An invalid FCP response "
4254                                 "code was detected for a command. "
4255                                 "unit 0x%016Lx, port 0x%016Lx, adapter %s "
4256                                 "(debug info 0x%x)\n",
4257                                 unit->fcp_lun,
4258                                 unit->port->wwpn,
4259                                 zfcp_get_busid_by_unit(unit),
4260                                 fcp_rsp_info[3]);
4261                 fsf_req->status |= ZFCP_STATUS_FSFREQ_TMFUNCFAILED;
4262         }
4263
4264       skip_fsfstatus:
4265         return retval;
4266 }
4267
4268
4269 /*
4270  * function:    zfcp_fsf_control_file
4271  *
4272  * purpose:     Initiator of the control file upload/download FSF requests
4273  *
4274  * returns:     0           - FSF request is successfuly created and queued
4275  *              -EOPNOTSUPP - The FCP adapter does not have Control File support
4276  *              -EINVAL     - Invalid direction specified
4277  *              -ENOMEM     - Insufficient memory
4278  *              -EPERM      - Cannot create FSF request or place it in QDIO queue
4279  */
4280 int
4281 zfcp_fsf_control_file(struct zfcp_adapter *adapter,
4282                       struct zfcp_fsf_req **fsf_req_ptr,
4283                       u32 fsf_command,
4284                       u32 option,
4285                       struct zfcp_sg_list *sg_list)
4286 {
4287         struct zfcp_fsf_req *fsf_req;
4288         struct fsf_qtcb_bottom_support *bottom;
4289         volatile struct qdio_buffer_element *sbale;
4290         struct timer_list *timer;
4291         unsigned long lock_flags;
4292         int req_flags = 0;
4293         int direction;
4294         int retval = 0;
4295
4296         if (!(adapter->supported_features & FSF_FEATURE_CFDC)) {
4297                 ZFCP_LOG_INFO("cfdc not supported (adapter %s)\n",
4298                               zfcp_get_busid_by_adapter(adapter));
4299                 retval = -EOPNOTSUPP;
4300                 goto out;
4301         }
4302
4303         switch (fsf_command) {
4304
4305         case FSF_QTCB_DOWNLOAD_CONTROL_FILE:
4306                 direction = SBAL_FLAGS0_TYPE_WRITE;
4307                 if ((option != FSF_CFDC_OPTION_FULL_ACCESS) &&
4308                     (option != FSF_CFDC_OPTION_RESTRICTED_ACCESS))
4309                         req_flags = ZFCP_WAIT_FOR_SBAL;
4310                 break;
4311
4312         case FSF_QTCB_UPLOAD_CONTROL_FILE:
4313                 direction = SBAL_FLAGS0_TYPE_READ;
4314                 break;
4315
4316         default:
4317                 ZFCP_LOG_INFO("Invalid FSF command code 0x%08x\n", fsf_command);
4318                 retval = -EINVAL;
4319                 goto out;
4320         }
4321
4322         timer = kmalloc(sizeof(struct timer_list), GFP_KERNEL);
4323         if (!timer) {
4324                 retval = -ENOMEM;
4325                 goto out;
4326         }
4327
4328         retval = zfcp_fsf_req_create(adapter, fsf_command, req_flags,
4329                                      NULL, &lock_flags, &fsf_req);
4330         if (retval < 0) {
4331                 ZFCP_LOG_INFO("error: Could not create FSF request for the "
4332                               "adapter %s\n",
4333                         zfcp_get_busid_by_adapter(adapter));
4334                 retval = -EPERM;
4335                 goto unlock_queue_lock;
4336         }
4337
4338         sbale = zfcp_qdio_sbale_req(fsf_req, fsf_req->sbal_curr, 0);
4339         sbale[0].flags |= direction;
4340
4341         bottom = &fsf_req->qtcb->bottom.support;
4342         bottom->operation_subtype = FSF_CFDC_OPERATION_SUBTYPE;
4343         bottom->option = option;
4344
4345         if (sg_list->count > 0) {
4346                 int bytes;
4347
4348                 bytes = zfcp_qdio_sbals_from_sg(fsf_req, direction,
4349                                                 sg_list->sg, sg_list->count,
4350                                                 ZFCP_MAX_SBALS_PER_REQ);
4351                 if (bytes != ZFCP_CFDC_MAX_CONTROL_FILE_SIZE) {
4352                         ZFCP_LOG_INFO(
4353                                 "error: Could not create sufficient number of "
4354                                 "SBALS for an FSF request to the adapter %s\n",
4355                                 zfcp_get_busid_by_adapter(adapter));
4356                         retval = -ENOMEM;
4357                         goto free_fsf_req;
4358                 }
4359         } else
4360                 sbale[1].flags |= SBAL_FLAGS_LAST_ENTRY;
4361
4362         init_timer(timer);
4363         timer->function = zfcp_fsf_request_timeout_handler;
4364         timer->data = (unsigned long) adapter;
4365         timer->expires = ZFCP_FSF_REQUEST_TIMEOUT;
4366
4367         retval = zfcp_fsf_req_send(fsf_req, timer);
4368         if (retval < 0) {
4369                 ZFCP_LOG_INFO("initiation of cfdc up/download failed"
4370                               "(adapter %s)\n",
4371                               zfcp_get_busid_by_adapter(adapter));
4372                 retval = -EPERM;
4373                 goto free_fsf_req;
4374         }
4375         write_unlock_irqrestore(&adapter->request_queue.queue_lock, lock_flags);
4376
4377         ZFCP_LOG_NORMAL("Control file %s FSF request has been sent to the "
4378                         "adapter %s\n",
4379                         fsf_command == FSF_QTCB_DOWNLOAD_CONTROL_FILE ?
4380                         "download" : "upload",
4381                         zfcp_get_busid_by_adapter(adapter));
4382
4383         wait_event(fsf_req->completion_wq,
4384                    fsf_req->status & ZFCP_STATUS_FSFREQ_COMPLETED);
4385
4386         *fsf_req_ptr = fsf_req;
4387         del_timer_sync(timer);
4388         goto free_timer;
4389
4390  free_fsf_req:
4391         zfcp_fsf_req_free(fsf_req);
4392  unlock_queue_lock:
4393         write_unlock_irqrestore(&adapter->request_queue.queue_lock, lock_flags);
4394  free_timer:
4395         kfree(timer);
4396  out:
4397         return retval;
4398 }
4399
4400
4401 /*
4402  * function:    zfcp_fsf_control_file_handler
4403  *
4404  * purpose:     Handler of the control file upload/download FSF requests
4405  *
4406  * returns:     0       - FSF request successfuly processed
4407  *              -EAGAIN - Operation has to be repeated because of a temporary problem
4408  *              -EACCES - There is no permission to execute an operation
4409  *              -EPERM  - The control file is not in a right format
4410  *              -EIO    - There is a problem with the FCP adapter
4411  *              -EINVAL - Invalid operation
4412  *              -EFAULT - User space memory I/O operation fault
4413  */
4414 static int
4415 zfcp_fsf_control_file_handler(struct zfcp_fsf_req *fsf_req)
4416 {
4417         struct zfcp_adapter *adapter = fsf_req->adapter;
4418         struct fsf_qtcb_header *header = &fsf_req->qtcb->header;
4419         struct fsf_qtcb_bottom_support *bottom = &fsf_req->qtcb->bottom.support;
4420         int retval = 0;
4421
4422         if (fsf_req->status & ZFCP_STATUS_FSFREQ_ERROR) {
4423                 retval = -EINVAL;
4424                 goto skip_fsfstatus;
4425         }
4426
4427         switch (header->fsf_status) {
4428
4429         case FSF_GOOD:
4430                 ZFCP_LOG_NORMAL(
4431                         "The FSF request has been successfully completed "
4432                         "on the adapter %s\n",
4433                         zfcp_get_busid_by_adapter(adapter));
4434                 break;
4435
4436         case FSF_OPERATION_PARTIALLY_SUCCESSFUL:
4437                 if (bottom->operation_subtype == FSF_CFDC_OPERATION_SUBTYPE) {
4438                         switch (header->fsf_status_qual.word[0]) {
4439
4440                         case FSF_SQ_CFDC_HARDENED_ON_SE:
4441                                 ZFCP_LOG_NORMAL(
4442                                         "CFDC on the adapter %s has being "
4443                                         "hardened on primary and secondary SE\n",
4444                                         zfcp_get_busid_by_adapter(adapter));
4445                                 break;
4446
4447                         case FSF_SQ_CFDC_COULD_NOT_HARDEN_ON_SE:
4448                                 ZFCP_LOG_NORMAL(
4449                                         "CFDC of the adapter %s could not "
4450                                         "be saved on the SE\n",
4451                                         zfcp_get_busid_by_adapter(adapter));
4452                                 break;
4453
4454                         case FSF_SQ_CFDC_COULD_NOT_HARDEN_ON_SE2:
4455                                 ZFCP_LOG_NORMAL(
4456                                         "CFDC of the adapter %s could not "
4457                                         "be copied to the secondary SE\n",
4458                                         zfcp_get_busid_by_adapter(adapter));
4459                                 break;
4460
4461                         default:
4462                                 ZFCP_LOG_NORMAL(
4463                                         "CFDC could not be hardened "
4464                                         "on the adapter %s\n",
4465                                         zfcp_get_busid_by_adapter(adapter));
4466                         }
4467                 }
4468                 fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
4469                 retval = -EAGAIN;
4470                 break;
4471
4472         case FSF_AUTHORIZATION_FAILURE:
4473                 ZFCP_LOG_NORMAL(
4474                         "Adapter %s does not accept privileged commands\n",
4475                         zfcp_get_busid_by_adapter(adapter));
4476                 fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
4477                 retval = -EACCES;
4478                 break;
4479
4480         case FSF_CFDC_ERROR_DETECTED:
4481                 ZFCP_LOG_NORMAL(
4482                         "Error at position %d in the CFDC, "
4483                         "CFDC is discarded by the adapter %s\n",
4484                         header->fsf_status_qual.word[0],
4485                         zfcp_get_busid_by_adapter(adapter));
4486                 fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
4487                 retval = -EPERM;
4488                 break;
4489
4490         case FSF_CONTROL_FILE_UPDATE_ERROR:
4491                 ZFCP_LOG_NORMAL(
4492                         "Adapter %s cannot harden the control file, "
4493                         "file is discarded\n",
4494                         zfcp_get_busid_by_adapter(adapter));
4495                 fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
4496                 retval = -EIO;
4497                 break;
4498
4499         case FSF_CONTROL_FILE_TOO_LARGE:
4500                 ZFCP_LOG_NORMAL(
4501                         "Control file is too large, file is discarded "
4502                         "by the adapter %s\n",
4503                         zfcp_get_busid_by_adapter(adapter));
4504                 fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
4505                 retval = -EIO;
4506                 break;
4507
4508         case FSF_ACCESS_CONFLICT_DETECTED:
4509                 if (bottom->operation_subtype == FSF_CFDC_OPERATION_SUBTYPE)
4510                         ZFCP_LOG_NORMAL(
4511                                 "CFDC has been discarded by the adapter %s, "
4512                                 "because activation would impact "
4513                                 "%d active connection(s)\n",
4514                                 zfcp_get_busid_by_adapter(adapter),
4515                                 header->fsf_status_qual.word[0]);
4516                 fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
4517                 retval = -EIO;
4518                 break;
4519
4520         case FSF_CONFLICTS_OVERRULED:
4521                 if (bottom->operation_subtype == FSF_CFDC_OPERATION_SUBTYPE)
4522                         ZFCP_LOG_NORMAL(
4523                                 "CFDC has been activated on the adapter %s, "
4524                                 "but activation has impacted "
4525                                 "%d active connection(s)\n",
4526                                 zfcp_get_busid_by_adapter(adapter),
4527                                 header->fsf_status_qual.word[0]);
4528                 fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
4529                 retval = -EIO;
4530                 break;
4531
4532         case FSF_UNKNOWN_OP_SUBTYPE:
4533                 ZFCP_LOG_NORMAL("unknown operation subtype (adapter: %s, "
4534                                 "op_subtype=0x%x)\n",
4535                                 zfcp_get_busid_by_adapter(adapter),
4536                                 bottom->operation_subtype);
4537                 fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
4538                 retval = -EINVAL;
4539                 break;
4540
4541         case FSF_INVALID_COMMAND_OPTION:
4542                 ZFCP_LOG_NORMAL(
4543                         "Invalid option 0x%x has been specified "
4544                         "in QTCB bottom sent to the adapter %s\n",
4545                         bottom->option,
4546                         zfcp_get_busid_by_adapter(adapter));
4547                 fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
4548                 retval = -EINVAL;
4549                 break;
4550
4551         default:
4552                 ZFCP_LOG_NORMAL(
4553                         "bug: An unknown/unexpected FSF status 0x%08x "
4554                         "was presented on the adapter %s\n",
4555                         header->fsf_status,
4556                         zfcp_get_busid_by_adapter(adapter));
4557                 debug_text_event(fsf_req->adapter->erp_dbf, 0, "fsf_sq_inval");
4558                 debug_exception(fsf_req->adapter->erp_dbf, 0,
4559                         &header->fsf_status_qual.word[0], sizeof(u32));
4560                 fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
4561                 retval = -EINVAL;
4562                 break;
4563         }
4564
4565 skip_fsfstatus:
4566         return retval;
4567 }
4568
4569
4570 /*
4571  * function:    zfcp_fsf_req_wait_and_cleanup
4572  *
4573  * purpose:
4574  *
4575  * FIXME(design): signal seems to be <0 !!!
4576  * returns:     0       - request completed (*status is valid), cleanup succ.
4577  *              <0      - request completed (*status is valid), cleanup failed
4578  *              >0      - signal which interrupted waiting (*status invalid),
4579  *                        request not completed, no cleanup
4580  *
4581  *              *status is a copy of status of completed fsf_req
4582  */
4583 int
4584 zfcp_fsf_req_wait_and_cleanup(struct zfcp_fsf_req *fsf_req,
4585                               int interruptible, u32 * status)
4586 {
4587         int retval = 0;
4588         int signal = 0;
4589
4590         if (interruptible) {
4591                 __wait_event_interruptible(fsf_req->completion_wq,
4592                                            fsf_req->status &
4593                                            ZFCP_STATUS_FSFREQ_COMPLETED,
4594                                            signal);
4595                 if (signal) {
4596                         ZFCP_LOG_DEBUG("Caught signal %i while waiting for the "
4597                                        "completion of the request at %p\n",
4598                                        signal, fsf_req);
4599                         retval = signal;
4600                         goto out;
4601                 }
4602         } else {
4603                 __wait_event(fsf_req->completion_wq,
4604                              fsf_req->status & ZFCP_STATUS_FSFREQ_COMPLETED);
4605         }
4606
4607         *status = fsf_req->status;
4608
4609         /* cleanup request */
4610         zfcp_fsf_req_cleanup(fsf_req);
4611  out:
4612         return retval;
4613 }
4614
4615 static inline int
4616 zfcp_fsf_req_sbal_check(unsigned long *flags,
4617                         struct zfcp_qdio_queue *queue, int needed)
4618 {
4619         write_lock_irqsave(&queue->queue_lock, *flags);
4620         if (likely(atomic_read(&queue->free_count) >= needed))
4621                 return 1;
4622         write_unlock_irqrestore(&queue->queue_lock, *flags);
4623         return 0;
4624 }
4625
4626 /*
4627  * set qtcb pointer in fsf_req and initialize QTCB
4628  */
4629 static inline void
4630 zfcp_fsf_req_qtcb_init(struct zfcp_fsf_req *fsf_req, u32 fsf_cmd)
4631 {
4632         if (likely(fsf_req->qtcb != NULL)) {
4633                 fsf_req->qtcb->prefix.req_id = (unsigned long)fsf_req;
4634                 fsf_req->qtcb->prefix.ulp_info = ZFCP_ULP_INFO_VERSION;
4635                 fsf_req->qtcb->prefix.qtcb_type = fsf_qtcb_type[fsf_cmd];
4636                 fsf_req->qtcb->prefix.qtcb_version = ZFCP_QTCB_VERSION;
4637                 fsf_req->qtcb->header.req_handle = (unsigned long)fsf_req;
4638                 fsf_req->qtcb->header.fsf_command = fsf_cmd;
4639         }
4640 }
4641
4642 /**
4643  * zfcp_fsf_req_sbal_get - try to get one SBAL in the request queue
4644  * @adapter: adapter for which request queue is examined
4645  * @req_flags: flags indicating whether to wait for needed SBAL or not
4646  * @lock_flags: lock_flags if queue_lock is taken
4647  * Return: 0 on success, otherwise -EIO, or -ERESTARTSYS
4648  * Locks: lock adapter->request_queue->queue_lock on success
4649  */
4650 static int
4651 zfcp_fsf_req_sbal_get(struct zfcp_adapter *adapter, int req_flags,
4652                       unsigned long *lock_flags)
4653 {
4654         long ret;
4655         struct zfcp_qdio_queue *req_queue = &adapter->request_queue;
4656
4657         if (unlikely(req_flags & ZFCP_WAIT_FOR_SBAL)) {
4658                 ret = wait_event_interruptible_timeout(adapter->request_wq,
4659                         zfcp_fsf_req_sbal_check(lock_flags, req_queue, 1),
4660                                                        ZFCP_SBAL_TIMEOUT);
4661                 if (ret < 0)
4662                         return ret;
4663                 if (!ret)
4664                         return -EIO;
4665         } else if (!zfcp_fsf_req_sbal_check(lock_flags, req_queue, 1))
4666                 return -EIO;
4667
4668         return 0;
4669 }
4670
4671 /*
4672  * function:    zfcp_fsf_req_create
4673  *
4674  * purpose:     create an FSF request at the specified adapter and
4675  *              setup common fields
4676  *
4677  * returns:     -ENOMEM if there was insufficient memory for a request
4678  *              -EIO if no qdio buffers could be allocate to the request
4679  *              -EINVAL/-EPERM on bug conditions in req_dequeue
4680  *              0 in success
4681  *
4682  * note:        The created request is returned by reference.
4683  *
4684  * locks:       lock of concerned request queue must not be held,
4685  *              but is held on completion (write, irqsave)
4686  */
4687 int
4688 zfcp_fsf_req_create(struct zfcp_adapter *adapter, u32 fsf_cmd, int req_flags,
4689                     mempool_t *pool, unsigned long *lock_flags,
4690                     struct zfcp_fsf_req **fsf_req_p)
4691 {
4692         volatile struct qdio_buffer_element *sbale;
4693         struct zfcp_fsf_req *fsf_req = NULL;
4694         int ret = 0;
4695         struct zfcp_qdio_queue *req_queue = &adapter->request_queue;
4696
4697         /* allocate new FSF request */
4698         fsf_req = zfcp_fsf_req_alloc(pool, req_flags);
4699         if (unlikely(NULL == fsf_req)) {
4700                 ZFCP_LOG_DEBUG("error: Could not put an FSF request into"
4701                                "the outbound (send) queue.\n");
4702                 ret = -ENOMEM;
4703                 goto failed_fsf_req;
4704         }
4705
4706         zfcp_fsf_req_qtcb_init(fsf_req, fsf_cmd);
4707
4708         /* initialize waitqueue which may be used to wait on 
4709            this request completion */
4710         init_waitqueue_head(&fsf_req->completion_wq);
4711
4712         ret = zfcp_fsf_req_sbal_get(adapter, req_flags, lock_flags);
4713         if(ret < 0) {
4714                 goto failed_sbals;
4715         }
4716
4717         /*
4718          * We hold queue_lock here. Check if QDIOUP is set and let request fail
4719          * if it is not set (see also *_open_qdio and *_close_qdio).
4720          */
4721
4722         if (!atomic_test_mask(ZFCP_STATUS_ADAPTER_QDIOUP, &adapter->status)) {
4723                 write_unlock_irqrestore(&req_queue->queue_lock, *lock_flags);
4724                 ret = -EIO;
4725                 goto failed_sbals;
4726         }
4727
4728         fsf_req->adapter = adapter;     /* pointer to "parent" adapter */
4729         fsf_req->fsf_command = fsf_cmd;
4730         fsf_req->sbal_number = 1;
4731         fsf_req->sbal_first = req_queue->free_index;
4732         fsf_req->sbal_curr = req_queue->free_index;
4733         fsf_req->sbale_curr = 1;
4734
4735         if (likely(req_flags & ZFCP_REQ_AUTO_CLEANUP)) {
4736                 fsf_req->status |= ZFCP_STATUS_FSFREQ_CLEANUP;
4737         }
4738
4739         sbale = zfcp_qdio_sbale_req(fsf_req, fsf_req->sbal_curr, 0);
4740
4741         /* setup common SBALE fields */
4742         sbale[0].addr = fsf_req;
4743         sbale[0].flags |= SBAL_FLAGS0_COMMAND;
4744         if (likely(fsf_req->qtcb != NULL)) {
4745                 sbale[1].addr = (void *) fsf_req->qtcb;
4746                 sbale[1].length = sizeof(struct fsf_qtcb);
4747         }
4748
4749         ZFCP_LOG_TRACE("got %i free BUFFERs starting at index %i\n",
4750                        fsf_req->sbal_number, fsf_req->sbal_first);
4751
4752         goto success;
4753
4754  failed_sbals:
4755 /* dequeue new FSF request previously enqueued */
4756         zfcp_fsf_req_free(fsf_req);
4757         fsf_req = NULL;
4758
4759  failed_fsf_req:
4760         write_lock_irqsave(&req_queue->queue_lock, *lock_flags);
4761  success:
4762         *fsf_req_p = fsf_req;
4763         return ret;
4764 }
4765
4766 /*
4767  * function:    zfcp_fsf_req_send
4768  *
4769  * purpose:     start transfer of FSF request via QDIO
4770  *
4771  * returns:     0 - request transfer succesfully started
4772  *              !0 - start of request transfer failed
4773  */
4774 static int
4775 zfcp_fsf_req_send(struct zfcp_fsf_req *fsf_req, struct timer_list *timer)
4776 {
4777         struct zfcp_adapter *adapter;
4778         struct zfcp_qdio_queue *req_queue;
4779         volatile struct qdio_buffer_element *sbale;
4780         int new_distance_from_int;
4781         unsigned long flags;
4782         int inc_seq_no = 1;
4783         int retval = 0;
4784
4785         adapter = fsf_req->adapter;
4786         req_queue = &adapter->request_queue,
4787
4788
4789         /* FIXME(debug): remove it later */
4790         sbale = zfcp_qdio_sbale_req(fsf_req, fsf_req->sbal_first, 0);
4791         ZFCP_LOG_DEBUG("SBALE0 flags=0x%x\n", sbale[0].flags);
4792         ZFCP_LOG_TRACE("HEX DUMP OF SBALE1 PAYLOAD:\n");
4793         ZFCP_HEX_DUMP(ZFCP_LOG_LEVEL_TRACE, (char *) sbale[1].addr,
4794                       sbale[1].length);
4795
4796         /* set sequence counter in QTCB */
4797         if (likely(fsf_req->qtcb)) {
4798                 fsf_req->qtcb->prefix.req_seq_no = adapter->fsf_req_seq_no;
4799                 fsf_req->seq_no = adapter->fsf_req_seq_no;
4800                 ZFCP_LOG_TRACE("FSF request %p of adapter %s gets "
4801                                "FSF sequence counter value of %i\n",
4802                                fsf_req,
4803                                zfcp_get_busid_by_adapter(adapter),
4804                                fsf_req->qtcb->prefix.req_seq_no);
4805         } else
4806                 inc_seq_no = 0;
4807
4808         /* put allocated FSF request at list tail */
4809         write_lock_irqsave(&adapter->fsf_req_list_lock, flags);
4810         list_add_tail(&fsf_req->list, &adapter->fsf_req_list_head);
4811         write_unlock_irqrestore(&adapter->fsf_req_list_lock, flags);
4812
4813         /* figure out expiration time of timeout and start timeout */
4814         if (unlikely(timer)) {
4815                 timer->expires += jiffies;
4816                 add_timer(timer);
4817         }
4818
4819         ZFCP_LOG_TRACE("request queue of adapter %s: "
4820                        "next free SBAL is %i, %i free SBALs\n",
4821                        zfcp_get_busid_by_adapter(adapter),
4822                        req_queue->free_index,
4823                        atomic_read(&req_queue->free_count));
4824
4825         ZFCP_LOG_DEBUG("calling do_QDIO adapter %s, flags=0x%x, queue_no=%i, "
4826                        "index_in_queue=%i, count=%i, buffers=%p\n",
4827                        zfcp_get_busid_by_adapter(adapter),
4828                        QDIO_FLAG_SYNC_OUTPUT,
4829                        0, fsf_req->sbal_first, fsf_req->sbal_number,
4830                        &req_queue->buffer[fsf_req->sbal_first]);
4831
4832         /*
4833          * adjust the number of free SBALs in request queue as well as
4834          * position of first one
4835          */
4836         atomic_sub(fsf_req->sbal_number, &req_queue->free_count);
4837         ZFCP_LOG_TRACE("free_count=%d\n", atomic_read(&req_queue->free_count));
4838         req_queue->free_index += fsf_req->sbal_number;    /* increase */
4839         req_queue->free_index %= QDIO_MAX_BUFFERS_PER_Q;  /* wrap if needed */
4840         new_distance_from_int = zfcp_qdio_determine_pci(req_queue, fsf_req);
4841
4842         retval = do_QDIO(adapter->ccw_device,
4843                          QDIO_FLAG_SYNC_OUTPUT,
4844                          0, fsf_req->sbal_first, fsf_req->sbal_number, NULL);
4845
4846         if (unlikely(retval)) {
4847                 /* Queues are down..... */
4848                 retval = -EIO;
4849                 /*
4850                  * FIXME(potential race):
4851                  * timer might be expired (absolutely unlikely)
4852                  */
4853                 if (timer)
4854                         del_timer(timer);
4855                 write_lock_irqsave(&adapter->fsf_req_list_lock, flags);
4856                 list_del(&fsf_req->list);
4857                 write_unlock_irqrestore(&adapter->fsf_req_list_lock, flags);
4858                 /*
4859                  * adjust the number of free SBALs in request queue as well as
4860                  * position of first one
4861                  */
4862                 zfcp_qdio_zero_sbals(req_queue->buffer,
4863                                      fsf_req->sbal_first, fsf_req->sbal_number);
4864                 atomic_add(fsf_req->sbal_number, &req_queue->free_count);
4865                 req_queue->free_index -= fsf_req->sbal_number;   /* increase */
4866                 req_queue->free_index += QDIO_MAX_BUFFERS_PER_Q;
4867                 req_queue->free_index %= QDIO_MAX_BUFFERS_PER_Q; /* wrap */
4868                 ZFCP_LOG_DEBUG
4869                         ("error: do_QDIO failed. Buffers could not be enqueued "
4870                          "to request queue.\n");
4871         } else {
4872                 req_queue->distance_from_int = new_distance_from_int;
4873                 /*
4874                  * increase FSF sequence counter -
4875                  * this must only be done for request successfully enqueued to
4876                  * QDIO this rejected requests may be cleaned up by calling
4877                  * routines  resulting in missing sequence counter values
4878                  * otherwise,
4879                  */
4880                 /* Don't increase for unsolicited status */
4881                 if (likely(inc_seq_no)) {
4882                         adapter->fsf_req_seq_no++;
4883                         ZFCP_LOG_TRACE
4884                             ("FSF sequence counter value of adapter %s "
4885                              "increased to %i\n",
4886                              zfcp_get_busid_by_adapter(adapter),
4887                              adapter->fsf_req_seq_no);
4888                 }
4889                 /* count FSF requests pending */
4890                 atomic_inc(&adapter->fsf_reqs_active);
4891         }
4892         return retval;
4893 }
4894
4895 /*
4896  * function:    zfcp_fsf_req_cleanup
4897  *
4898  * purpose:     cleans up an FSF request and removes it from the specified list
4899  *
4900  * returns:
4901  *
4902  * assumption:  no pending SB in SBALEs other than QTCB
4903  */
4904 void
4905 zfcp_fsf_req_cleanup(struct zfcp_fsf_req *fsf_req)
4906 {
4907         struct zfcp_adapter *adapter = fsf_req->adapter;
4908         unsigned long flags;
4909
4910         write_lock_irqsave(&adapter->fsf_req_list_lock, flags);
4911         list_del(&fsf_req->list);
4912         write_unlock_irqrestore(&adapter->fsf_req_list_lock, flags);
4913         zfcp_fsf_req_free(fsf_req);
4914 }
4915
4916 #undef ZFCP_LOG_AREA