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