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