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