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