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