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