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