Merge branch 'linus'
[linux-2.6.git] / drivers / s390 / scsi / zfcp_fsf.c
1 /*
2  * This file is part of the zfcp device driver for
3  * FCP adapters for IBM System z9 and zSeries.
4  *
5  * (C) Copyright IBM Corp. 2002, 2006
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2, or (at your option)
10  * any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  */
21
22 #include "zfcp_ext.h"
23
24 static int zfcp_fsf_exchange_config_data_handler(struct zfcp_fsf_req *);
25 static void zfcp_fsf_exchange_port_data_handler(struct zfcp_fsf_req *);
26 static int zfcp_fsf_open_port_handler(struct zfcp_fsf_req *);
27 static int zfcp_fsf_close_port_handler(struct zfcp_fsf_req *);
28 static int zfcp_fsf_close_physical_port_handler(struct zfcp_fsf_req *);
29 static int zfcp_fsf_open_unit_handler(struct zfcp_fsf_req *);
30 static int zfcp_fsf_close_unit_handler(struct zfcp_fsf_req *);
31 static int zfcp_fsf_send_fcp_command_handler(struct zfcp_fsf_req *);
32 static int zfcp_fsf_send_fcp_command_task_handler(struct zfcp_fsf_req *);
33 static int zfcp_fsf_send_fcp_command_task_management_handler(
34         struct zfcp_fsf_req *);
35 static int zfcp_fsf_abort_fcp_command_handler(struct zfcp_fsf_req *);
36 static int zfcp_fsf_status_read_handler(struct zfcp_fsf_req *);
37 static int zfcp_fsf_send_ct_handler(struct zfcp_fsf_req *);
38 static int zfcp_fsf_send_els_handler(struct zfcp_fsf_req *);
39 static int zfcp_fsf_control_file_handler(struct zfcp_fsf_req *);
40 static inline int zfcp_fsf_req_sbal_check(
41         unsigned long *, struct zfcp_qdio_queue *, int);
42 static inline int zfcp_use_one_sbal(
43         struct scatterlist *, int, struct scatterlist *, int);
44 static struct zfcp_fsf_req *zfcp_fsf_req_alloc(mempool_t *, int);
45 static int zfcp_fsf_req_send(struct zfcp_fsf_req *);
46 static int zfcp_fsf_protstatus_eval(struct zfcp_fsf_req *);
47 static int zfcp_fsf_fsfstatus_eval(struct zfcp_fsf_req *);
48 static int zfcp_fsf_fsfstatus_qual_eval(struct zfcp_fsf_req *);
49 static void zfcp_fsf_link_down_info_eval(struct zfcp_adapter *,
50         struct fsf_link_down_info *);
51 static int zfcp_fsf_req_dispatch(struct zfcp_fsf_req *);
52
53 /* association between FSF command and FSF QTCB type */
54 static u32 fsf_qtcb_type[] = {
55         [FSF_QTCB_FCP_CMND] =             FSF_IO_COMMAND,
56         [FSF_QTCB_ABORT_FCP_CMND] =       FSF_SUPPORT_COMMAND,
57         [FSF_QTCB_OPEN_PORT_WITH_DID] =   FSF_SUPPORT_COMMAND,
58         [FSF_QTCB_OPEN_LUN] =             FSF_SUPPORT_COMMAND,
59         [FSF_QTCB_CLOSE_LUN] =            FSF_SUPPORT_COMMAND,
60         [FSF_QTCB_CLOSE_PORT] =           FSF_SUPPORT_COMMAND,
61         [FSF_QTCB_CLOSE_PHYSICAL_PORT] =  FSF_SUPPORT_COMMAND,
62         [FSF_QTCB_SEND_ELS] =             FSF_SUPPORT_COMMAND,
63         [FSF_QTCB_SEND_GENERIC] =         FSF_SUPPORT_COMMAND,
64         [FSF_QTCB_EXCHANGE_CONFIG_DATA] = FSF_CONFIG_COMMAND,
65         [FSF_QTCB_EXCHANGE_PORT_DATA] =   FSF_PORT_COMMAND,
66         [FSF_QTCB_DOWNLOAD_CONTROL_FILE] = FSF_SUPPORT_COMMAND,
67         [FSF_QTCB_UPLOAD_CONTROL_FILE] =  FSF_SUPPORT_COMMAND
68 };
69
70 static const char zfcp_act_subtable_type[5][8] = {
71         "unknown", "OS", "WWPN", "DID", "LUN"
72 };
73
74 /****************************************************************/
75 /*************** FSF related Functions  *************************/
76 /****************************************************************/
77
78 #define ZFCP_LOG_AREA                   ZFCP_LOG_AREA_FSF
79
80 /*
81  * function:    zfcp_fsf_req_alloc
82  *
83  * purpose:     Obtains an fsf_req and potentially a qtcb (for all but 
84  *              unsolicited requests) via helper functions
85  *              Does some initial fsf request set-up.
86  *              
87  * returns:     pointer to allocated fsf_req if successfull
88  *              NULL otherwise
89  *
90  * locks:       none
91  *
92  */
93 static struct zfcp_fsf_req *
94 zfcp_fsf_req_alloc(mempool_t *pool, int req_flags)
95 {
96         size_t size;
97         void *ptr;
98         struct zfcp_fsf_req *fsf_req = NULL;
99
100         if (req_flags & ZFCP_REQ_NO_QTCB)
101                 size = sizeof(struct zfcp_fsf_req);
102         else
103                 size = sizeof(struct zfcp_fsf_req_qtcb);
104
105         if (likely(pool))
106                 ptr = mempool_alloc(pool, GFP_ATOMIC);
107         else {
108                 if (req_flags & ZFCP_REQ_NO_QTCB)
109                         ptr = kmalloc(size, GFP_ATOMIC);
110                 else
111                         ptr = kmem_cache_alloc(zfcp_data.fsf_req_qtcb_cache,
112                                                GFP_ATOMIC);
113         }
114
115         if (unlikely(!ptr))
116                 goto out;
117
118         memset(ptr, 0, size);
119
120         if (req_flags & ZFCP_REQ_NO_QTCB) {
121                 fsf_req = (struct zfcp_fsf_req *) ptr;
122         } else {
123                 fsf_req = &((struct zfcp_fsf_req_qtcb *) ptr)->fsf_req;
124                 fsf_req->qtcb = &((struct zfcp_fsf_req_qtcb *) ptr)->qtcb;
125         }
126
127         fsf_req->pool = pool;
128
129  out:
130         return fsf_req;
131 }
132
133 /*
134  * function:    zfcp_fsf_req_free
135  *
136  * purpose:     Frees the memory of an fsf_req (and potentially a qtcb) or
137  *              returns it into the pool via helper functions.
138  *
139  * returns:     sod all
140  *
141  * locks:       none
142  */
143 void
144 zfcp_fsf_req_free(struct zfcp_fsf_req *fsf_req)
145 {
146         if (likely(fsf_req->pool)) {
147                 mempool_free(fsf_req, fsf_req->pool);
148                 return;
149         }
150
151         if (fsf_req->qtcb) {
152                 kmem_cache_free(zfcp_data.fsf_req_qtcb_cache, fsf_req);
153                 return;
154         }
155
156         kfree(fsf_req);
157 }
158
159 /**
160  * zfcp_fsf_req_dismiss - dismiss a single fsf request
161  */
162 static void zfcp_fsf_req_dismiss(struct zfcp_adapter *adapter,
163                                  struct zfcp_fsf_req *fsf_req,
164                                  unsigned int counter)
165 {
166         u64 dbg_tmp[2];
167
168         dbg_tmp[0] = (u64) atomic_read(&adapter->reqs_active);
169         dbg_tmp[1] = (u64) counter;
170         debug_event(adapter->erp_dbf, 4, (void *) dbg_tmp, 16);
171         list_del(&fsf_req->list);
172         fsf_req->status |= ZFCP_STATUS_FSFREQ_DISMISSED;
173         zfcp_fsf_req_complete(fsf_req);
174 }
175
176 /**
177  * zfcp_fsf_req_dismiss_all - dismiss all remaining fsf requests
178  */
179 void zfcp_fsf_req_dismiss_all(struct zfcp_adapter *adapter)
180 {
181         struct zfcp_fsf_req *request, *tmp;
182         unsigned long flags;
183         LIST_HEAD(remove_queue);
184         unsigned int i, counter;
185
186         spin_lock_irqsave(&adapter->req_list_lock, flags);
187         atomic_set(&adapter->reqs_active, 0);
188         for (i=0; i<REQUEST_LIST_SIZE; i++)
189                 list_splice_init(&adapter->req_list[i], &remove_queue);
190
191         spin_unlock_irqrestore(&adapter->req_list_lock, flags);
192
193         counter = 0;
194         list_for_each_entry_safe(request, tmp, &remove_queue, list) {
195                 zfcp_fsf_req_dismiss(adapter, request, counter);
196                 counter++;
197         }
198 }
199
200 /*
201  * function:    zfcp_fsf_req_complete
202  *
203  * purpose:     Updates active counts and timers for openfcp-reqs
204  *              May cleanup request after req_eval returns
205  *
206  * returns:     0 - success
207  *              !0 - failure
208  *
209  * context:     
210  */
211 int
212 zfcp_fsf_req_complete(struct zfcp_fsf_req *fsf_req)
213 {
214         int retval = 0;
215         int cleanup;
216
217         if (unlikely(fsf_req->fsf_command == FSF_QTCB_UNSOLICITED_STATUS)) {
218                 ZFCP_LOG_DEBUG("Status read response received\n");
219                 /*
220                  * Note: all cleanup handling is done in the callchain of
221                  * the function call-chain below.
222                  */
223                 zfcp_fsf_status_read_handler(fsf_req);
224                 goto out;
225         } else {
226                 del_timer(&fsf_req->timer);
227                 zfcp_fsf_protstatus_eval(fsf_req);
228         }
229
230         /*
231          * fsf_req may be deleted due to waking up functions, so 
232          * cleanup is saved here and used later 
233          */
234         if (likely(fsf_req->status & ZFCP_STATUS_FSFREQ_CLEANUP))
235                 cleanup = 1;
236         else
237                 cleanup = 0;
238
239         fsf_req->status |= ZFCP_STATUS_FSFREQ_COMPLETED;
240
241         /* cleanup request if requested by initiator */
242         if (likely(cleanup)) {
243                 ZFCP_LOG_TRACE("removing FSF request %p\n", fsf_req);
244                 /*
245                  * lock must not be held here since it will be
246                  * grabed by the called routine, too
247                  */
248                 zfcp_fsf_req_free(fsf_req);
249         } else {
250                 /* notify initiator waiting for the requests completion */
251                 ZFCP_LOG_TRACE("waking initiator of FSF request %p\n",fsf_req);
252                 /*
253                  * FIXME: Race! We must not access fsf_req here as it might have been
254                  * cleaned up already due to the set ZFCP_STATUS_FSFREQ_COMPLETED
255                  * flag. It's an improbable case. But, we have the same paranoia for
256                  * the cleanup flag already.
257                  * Might better be handled using complete()?
258                  * (setting the flag and doing wakeup ought to be atomic
259                  *  with regard to checking the flag as long as waitqueue is
260                  *  part of the to be released structure)
261                  */
262                 wake_up(&fsf_req->completion_wq);
263         }
264
265  out:
266         return retval;
267 }
268
269 /*
270  * function:    zfcp_fsf_protstatus_eval
271  *
272  * purpose:     evaluates the QTCB of the finished FSF request
273  *              and initiates appropriate actions
274  *              (usually calling FSF command specific handlers)
275  *
276  * returns:     
277  *
278  * context:     
279  *
280  * locks:
281  */
282 static int
283 zfcp_fsf_protstatus_eval(struct zfcp_fsf_req *fsf_req)
284 {
285         int retval = 0;
286         struct zfcp_adapter *adapter = fsf_req->adapter;
287         struct fsf_qtcb *qtcb = fsf_req->qtcb;
288         union fsf_prot_status_qual *prot_status_qual =
289                 &qtcb->prefix.prot_status_qual;
290
291         zfcp_hba_dbf_event_fsf_response(fsf_req);
292
293         if (fsf_req->status & ZFCP_STATUS_FSFREQ_DISMISSED) {
294                 ZFCP_LOG_DEBUG("fsf_req 0x%lx has been dismissed\n",
295                                (unsigned long) fsf_req);
296                 fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR |
297                         ZFCP_STATUS_FSFREQ_RETRY; /* only for SCSI cmnds. */
298                 goto skip_protstatus;
299         }
300
301         /* log additional information provided by FSF (if any) */
302         if (unlikely(qtcb->header.log_length)) {
303                 /* do not trust them ;-) */
304                 if (qtcb->header.log_start > sizeof(struct fsf_qtcb)) {
305                         ZFCP_LOG_NORMAL
306                             ("bug: ULP (FSF logging) log data starts "
307                              "beyond end of packet header. Ignored. "
308                              "(start=%i, size=%li)\n",
309                              qtcb->header.log_start,
310                              sizeof(struct fsf_qtcb));
311                         goto forget_log;
312                 }
313                 if ((size_t) (qtcb->header.log_start + qtcb->header.log_length)
314                     > sizeof(struct fsf_qtcb)) {
315                         ZFCP_LOG_NORMAL("bug: ULP (FSF logging) log data ends "
316                                         "beyond end of packet header. Ignored. "
317                                         "(start=%i, length=%i, size=%li)\n",
318                                         qtcb->header.log_start,
319                                         qtcb->header.log_length,
320                                         sizeof(struct fsf_qtcb));
321                         goto forget_log;
322                 }
323                 ZFCP_LOG_TRACE("ULP log data: \n");
324                 ZFCP_HEX_DUMP(ZFCP_LOG_LEVEL_TRACE,
325                               (char *) qtcb + qtcb->header.log_start,
326                               qtcb->header.log_length);
327         }
328  forget_log:
329
330         /* evaluate FSF Protocol Status */
331         switch (qtcb->prefix.prot_status) {
332
333         case FSF_PROT_GOOD:
334         case FSF_PROT_FSF_STATUS_PRESENTED:
335                 break;
336
337         case FSF_PROT_QTCB_VERSION_ERROR:
338                 ZFCP_LOG_NORMAL("error: The adapter %s contains "
339                                 "microcode of version 0x%x, the device driver "
340                                 "only supports 0x%x. Aborting.\n",
341                                 zfcp_get_busid_by_adapter(adapter),
342                                 prot_status_qual->version_error.fsf_version,
343                                 ZFCP_QTCB_VERSION);
344                 zfcp_erp_adapter_shutdown(adapter, 0);
345                 fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
346                 break;
347
348         case FSF_PROT_SEQ_NUMB_ERROR:
349                 ZFCP_LOG_NORMAL("bug: Sequence number mismatch between "
350                                 "driver (0x%x) and adapter %s (0x%x). "
351                                 "Restarting all operations on this adapter.\n",
352                                 qtcb->prefix.req_seq_no,
353                                 zfcp_get_busid_by_adapter(adapter),
354                                 prot_status_qual->sequence_error.exp_req_seq_no);
355                 zfcp_erp_adapter_reopen(adapter, 0);
356                 fsf_req->status |= ZFCP_STATUS_FSFREQ_RETRY;
357                 fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
358                 break;
359
360         case FSF_PROT_UNSUPP_QTCB_TYPE:
361                 ZFCP_LOG_NORMAL("error: Packet header type used by the "
362                                 "device driver is incompatible with "
363                                 "that used on adapter %s. "
364                                 "Stopping all operations on this adapter.\n",
365                                 zfcp_get_busid_by_adapter(adapter));
366                 zfcp_erp_adapter_shutdown(adapter, 0);
367                 fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
368                 break;
369
370         case FSF_PROT_HOST_CONNECTION_INITIALIZING:
371                 fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
372                 atomic_set_mask(ZFCP_STATUS_ADAPTER_HOST_CON_INIT,
373                                 &(adapter->status));
374                 break;
375
376         case FSF_PROT_DUPLICATE_REQUEST_ID:
377                         ZFCP_LOG_NORMAL("bug: The request identifier 0x%Lx "
378                                         "to the adapter %s is ambiguous. "
379                                 "Stopping all operations on this adapter.\n",
380                                 *(unsigned long long*)
381                                 (&qtcb->bottom.support.req_handle),
382                                         zfcp_get_busid_by_adapter(adapter));
383                 zfcp_erp_adapter_shutdown(adapter, 0);
384                 fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
385                 break;
386
387         case FSF_PROT_LINK_DOWN:
388                 zfcp_fsf_link_down_info_eval(adapter,
389                                              &prot_status_qual->link_down_info);
390                 zfcp_erp_adapter_reopen(adapter, 0);
391                 fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
392                 break;
393
394         case FSF_PROT_REEST_QUEUE:
395                 ZFCP_LOG_NORMAL("The local link to adapter with "
396                               "%s was re-plugged. "
397                               "Re-starting operations on this adapter.\n",
398                               zfcp_get_busid_by_adapter(adapter));
399                 /* All ports should be marked as ready to run again */
400                 zfcp_erp_modify_adapter_status(adapter,
401                                                ZFCP_STATUS_COMMON_RUNNING,
402                                                ZFCP_SET);
403                 zfcp_erp_adapter_reopen(adapter,
404                                         ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED
405                                         | ZFCP_STATUS_COMMON_ERP_FAILED);
406                 fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
407                 break;
408
409         case FSF_PROT_ERROR_STATE:
410                 ZFCP_LOG_NORMAL("error: The adapter %s "
411                                 "has entered the error state. "
412                                 "Restarting all operations on this "
413                                 "adapter.\n",
414                                 zfcp_get_busid_by_adapter(adapter));
415                 zfcp_erp_adapter_reopen(adapter, 0);
416                 fsf_req->status |= ZFCP_STATUS_FSFREQ_RETRY;
417                 fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
418                 break;
419
420         default:
421                 ZFCP_LOG_NORMAL("bug: Transfer protocol status information "
422                                 "provided by the adapter %s "
423                                 "is not compatible with the device driver. "
424                                 "Stopping all operations on this adapter. "
425                                 "(debug info 0x%x).\n",
426                                 zfcp_get_busid_by_adapter(adapter),
427                                 qtcb->prefix.prot_status);
428                 zfcp_erp_adapter_shutdown(adapter, 0);
429                 fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
430         }
431
432  skip_protstatus:
433         /*
434          * always call specific handlers to give them a chance to do
435          * something meaningful even in error cases
436          */
437         zfcp_fsf_fsfstatus_eval(fsf_req);
438         return retval;
439 }
440
441 /*
442  * function:    zfcp_fsf_fsfstatus_eval
443  *
444  * purpose:     evaluates FSF status of completed FSF request
445  *              and acts accordingly
446  *
447  * returns:
448  */
449 static int
450 zfcp_fsf_fsfstatus_eval(struct zfcp_fsf_req *fsf_req)
451 {
452         int retval = 0;
453
454         if (unlikely(fsf_req->status & ZFCP_STATUS_FSFREQ_ERROR)) {
455                 goto skip_fsfstatus;
456         }
457
458         /* evaluate FSF Status */
459         switch (fsf_req->qtcb->header.fsf_status) {
460         case FSF_UNKNOWN_COMMAND:
461                 ZFCP_LOG_NORMAL("bug: Command issued by the device driver is "
462                                 "not known by the adapter %s "
463                                 "Stopping all operations on this adapter. "
464                                 "(debug info 0x%x).\n",
465                                 zfcp_get_busid_by_adapter(fsf_req->adapter),
466                                 fsf_req->qtcb->header.fsf_command);
467                 zfcp_erp_adapter_shutdown(fsf_req->adapter, 0);
468                 fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
469                 break;
470
471         case FSF_FCP_RSP_AVAILABLE:
472                 ZFCP_LOG_DEBUG("FCP Sense data will be presented to the "
473                                "SCSI stack.\n");
474                 break;
475
476         case FSF_ADAPTER_STATUS_AVAILABLE:
477                 zfcp_fsf_fsfstatus_qual_eval(fsf_req);
478                 break;
479         }
480
481  skip_fsfstatus:
482         /*
483          * always call specific handlers to give them a chance to do
484          * something meaningful even in error cases
485          */
486         zfcp_fsf_req_dispatch(fsf_req);
487
488         return retval;
489 }
490
491 /*
492  * function:    zfcp_fsf_fsfstatus_qual_eval
493  *
494  * purpose:     evaluates FSF status-qualifier of completed FSF request
495  *              and acts accordingly
496  *
497  * returns:
498  */
499 static int
500 zfcp_fsf_fsfstatus_qual_eval(struct zfcp_fsf_req *fsf_req)
501 {
502         int retval = 0;
503
504         switch (fsf_req->qtcb->header.fsf_status_qual.word[0]) {
505         case FSF_SQ_FCP_RSP_AVAILABLE:
506                 break;
507         case FSF_SQ_RETRY_IF_POSSIBLE:
508                 /* The SCSI-stack may now issue retries or escalate */
509                 fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
510                 break;
511         case FSF_SQ_COMMAND_ABORTED:
512                 /* Carry the aborted state on to upper layer */
513                 fsf_req->status |= ZFCP_STATUS_FSFREQ_ABORTED;
514                 fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
515                 break;
516         case FSF_SQ_NO_RECOM:
517                 ZFCP_LOG_NORMAL("bug: No recommendation could be given for a"
518                                 "problem on the adapter %s "
519                                 "Stopping all operations on this adapter. ",
520                                 zfcp_get_busid_by_adapter(fsf_req->adapter));
521                 zfcp_erp_adapter_shutdown(fsf_req->adapter, 0);
522                 fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
523                 break;
524         case FSF_SQ_ULP_PROGRAMMING_ERROR:
525                 ZFCP_LOG_NORMAL("error: not enough SBALs for data transfer "
526                                 "(adapter %s)\n",
527                                 zfcp_get_busid_by_adapter(fsf_req->adapter));
528                 fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
529                 break;
530         case FSF_SQ_INVOKE_LINK_TEST_PROCEDURE:
531         case FSF_SQ_NO_RETRY_POSSIBLE:
532         case FSF_SQ_ULP_DEPENDENT_ERP_REQUIRED:
533                 /* dealt with in the respective functions */
534                 break;
535         default:
536                 ZFCP_LOG_NORMAL("bug: Additional status info could "
537                                 "not be interpreted properly.\n");
538                 ZFCP_HEX_DUMP(ZFCP_LOG_LEVEL_NORMAL,
539                               (char *) &fsf_req->qtcb->header.fsf_status_qual,
540                               sizeof (union fsf_status_qual));
541                 fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
542                 break;
543         }
544
545         return retval;
546 }
547
548 /**
549  * zfcp_fsf_link_down_info_eval - evaluate link down information block
550  */
551 static void
552 zfcp_fsf_link_down_info_eval(struct zfcp_adapter *adapter,
553                              struct fsf_link_down_info *link_down)
554 {
555         if (atomic_test_mask(ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED,
556                              &adapter->status))
557                 return;
558
559         atomic_set_mask(ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED, &adapter->status);
560
561         if (link_down == NULL)
562                 goto out;
563
564         switch (link_down->error_code) {
565         case FSF_PSQ_LINK_NO_LIGHT:
566                 ZFCP_LOG_NORMAL("The local link to adapter %s is down "
567                                 "(no light detected)\n",
568                                 zfcp_get_busid_by_adapter(adapter));
569                 break;
570         case FSF_PSQ_LINK_WRAP_PLUG:
571                 ZFCP_LOG_NORMAL("The local link to adapter %s is down "
572                                 "(wrap plug detected)\n",
573                                 zfcp_get_busid_by_adapter(adapter));
574                 break;
575         case FSF_PSQ_LINK_NO_FCP:
576                 ZFCP_LOG_NORMAL("The local link to adapter %s is down "
577                                 "(adjacent node on link does not support FCP)\n",
578                                 zfcp_get_busid_by_adapter(adapter));
579                 break;
580         case FSF_PSQ_LINK_FIRMWARE_UPDATE:
581                 ZFCP_LOG_NORMAL("The local link to adapter %s is down "
582                                 "(firmware update in progress)\n",
583                                 zfcp_get_busid_by_adapter(adapter));
584                         break;
585         case FSF_PSQ_LINK_INVALID_WWPN:
586                 ZFCP_LOG_NORMAL("The local link to adapter %s is down "
587                                 "(duplicate or invalid WWPN detected)\n",
588                                 zfcp_get_busid_by_adapter(adapter));
589                 break;
590         case FSF_PSQ_LINK_NO_NPIV_SUPPORT:
591                 ZFCP_LOG_NORMAL("The local link to adapter %s is down "
592                                 "(no support for NPIV by Fabric)\n",
593                                 zfcp_get_busid_by_adapter(adapter));
594                 break;
595         case FSF_PSQ_LINK_NO_FCP_RESOURCES:
596                 ZFCP_LOG_NORMAL("The local link to adapter %s is down "
597                                 "(out of resource in FCP daughtercard)\n",
598                                 zfcp_get_busid_by_adapter(adapter));
599                 break;
600         case FSF_PSQ_LINK_NO_FABRIC_RESOURCES:
601                 ZFCP_LOG_NORMAL("The local link to adapter %s is down "
602                                 "(out of resource in Fabric)\n",
603                                 zfcp_get_busid_by_adapter(adapter));
604                 break;
605         case FSF_PSQ_LINK_FABRIC_LOGIN_UNABLE:
606                 ZFCP_LOG_NORMAL("The local link to adapter %s is down "
607                                 "(unable to Fabric login)\n",
608                                 zfcp_get_busid_by_adapter(adapter));
609                 break;
610         case FSF_PSQ_LINK_WWPN_ASSIGNMENT_CORRUPTED:
611                 ZFCP_LOG_NORMAL("WWPN assignment file corrupted on adapter %s\n",
612                                 zfcp_get_busid_by_adapter(adapter));
613                 break;
614         case FSF_PSQ_LINK_MODE_TABLE_CURRUPTED:
615                 ZFCP_LOG_NORMAL("Mode table corrupted on adapter %s\n",
616                                 zfcp_get_busid_by_adapter(adapter));
617                 break;
618         case FSF_PSQ_LINK_NO_WWPN_ASSIGNMENT:
619                 ZFCP_LOG_NORMAL("No WWPN for assignment table on adapter %s\n",
620                                 zfcp_get_busid_by_adapter(adapter));
621                 break;
622         default:
623                 ZFCP_LOG_NORMAL("The local link to adapter %s is down "
624                                 "(warning: unknown reason code %d)\n",
625                                 zfcp_get_busid_by_adapter(adapter),
626                                 link_down->error_code);
627         }
628
629         if (adapter->connection_features & FSF_FEATURE_NPIV_MODE)
630                 ZFCP_LOG_DEBUG("Debug information to link down: "
631                                "primary_status=0x%02x "
632                                "ioerr_code=0x%02x "
633                                "action_code=0x%02x "
634                                "reason_code=0x%02x "
635                                "explanation_code=0x%02x "
636                                "vendor_specific_code=0x%02x\n",
637                                 link_down->primary_status,
638                                 link_down->ioerr_code,
639                                 link_down->action_code,
640                                 link_down->reason_code,
641                                 link_down->explanation_code,
642                                 link_down->vendor_specific_code);
643
644  out:
645         zfcp_erp_adapter_failed(adapter);
646 }
647
648 /*
649  * function:    zfcp_fsf_req_dispatch
650  *
651  * purpose:     calls the appropriate command specific handler
652  *
653  * returns:     
654  */
655 static int
656 zfcp_fsf_req_dispatch(struct zfcp_fsf_req *fsf_req)
657 {
658         struct zfcp_erp_action *erp_action = fsf_req->erp_action;
659         struct zfcp_adapter *adapter = fsf_req->adapter;
660         int retval = 0;
661
662
663         switch (fsf_req->fsf_command) {
664
665         case FSF_QTCB_FCP_CMND:
666                 zfcp_fsf_send_fcp_command_handler(fsf_req);
667                 break;
668
669         case FSF_QTCB_ABORT_FCP_CMND:
670                 zfcp_fsf_abort_fcp_command_handler(fsf_req);
671                 break;
672
673         case FSF_QTCB_SEND_GENERIC:
674                 zfcp_fsf_send_ct_handler(fsf_req);
675                 break;
676
677         case FSF_QTCB_OPEN_PORT_WITH_DID:
678                 zfcp_fsf_open_port_handler(fsf_req);
679                 break;
680
681         case FSF_QTCB_OPEN_LUN:
682                 zfcp_fsf_open_unit_handler(fsf_req);
683                 break;
684
685         case FSF_QTCB_CLOSE_LUN:
686                 zfcp_fsf_close_unit_handler(fsf_req);
687                 break;
688
689         case FSF_QTCB_CLOSE_PORT:
690                 zfcp_fsf_close_port_handler(fsf_req);
691                 break;
692
693         case FSF_QTCB_CLOSE_PHYSICAL_PORT:
694                 zfcp_fsf_close_physical_port_handler(fsf_req);
695                 break;
696
697         case FSF_QTCB_EXCHANGE_CONFIG_DATA:
698                 zfcp_fsf_exchange_config_data_handler(fsf_req);
699                 break;
700
701         case FSF_QTCB_EXCHANGE_PORT_DATA:
702                 zfcp_fsf_exchange_port_data_handler(fsf_req);
703                 break;
704
705         case FSF_QTCB_SEND_ELS:
706                 zfcp_fsf_send_els_handler(fsf_req);
707                 break;
708
709         case FSF_QTCB_DOWNLOAD_CONTROL_FILE:
710                 zfcp_fsf_control_file_handler(fsf_req);
711                 break;
712
713         case FSF_QTCB_UPLOAD_CONTROL_FILE:
714                 zfcp_fsf_control_file_handler(fsf_req);
715                 break;
716
717         default:
718                 fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
719                 ZFCP_LOG_NORMAL("bug: Command issued by the device driver is "
720                                 "not supported by the adapter %s\n",
721                                 zfcp_get_busid_by_adapter(adapter));
722                 if (fsf_req->fsf_command != fsf_req->qtcb->header.fsf_command)
723                         ZFCP_LOG_NORMAL
724                             ("bug: Command issued by the device driver differs "
725                              "from the command returned by the adapter %s "
726                              "(debug info 0x%x, 0x%x).\n",
727                              zfcp_get_busid_by_adapter(adapter),
728                              fsf_req->fsf_command,
729                              fsf_req->qtcb->header.fsf_command);
730         }
731
732         if (!erp_action)
733                 return retval;
734
735         zfcp_erp_async_handler(erp_action, 0);
736
737         return retval;
738 }
739
740 /*
741  * function:    zfcp_fsf_status_read
742  *
743  * purpose:     initiates a Status Read command at the specified adapter
744  *
745  * returns:
746  */
747 int
748 zfcp_fsf_status_read(struct zfcp_adapter *adapter, int req_flags)
749 {
750         struct zfcp_fsf_req *fsf_req;
751         struct fsf_status_read_buffer *status_buffer;
752         unsigned long lock_flags;
753         volatile struct qdio_buffer_element *sbale;
754         int retval = 0;
755
756         /* setup new FSF request */
757         retval = zfcp_fsf_req_create(adapter, FSF_QTCB_UNSOLICITED_STATUS,
758                                      req_flags | ZFCP_REQ_NO_QTCB,
759                                      adapter->pool.fsf_req_status_read,
760                                      &lock_flags, &fsf_req);
761         if (retval < 0) {
762                 ZFCP_LOG_INFO("error: Could not create unsolicited status "
763                               "buffer for adapter %s.\n",
764                               zfcp_get_busid_by_adapter(adapter));
765                 goto failed_req_create;
766         }
767
768         sbale = zfcp_qdio_sbale_req(fsf_req, fsf_req->sbal_curr, 0);
769         sbale[0].flags |= SBAL_FLAGS0_TYPE_STATUS;
770         sbale[2].flags |= SBAL_FLAGS_LAST_ENTRY;
771         fsf_req->sbale_curr = 2;
772
773         status_buffer =
774                 mempool_alloc(adapter->pool.data_status_read, GFP_ATOMIC);
775         if (!status_buffer) {
776                 ZFCP_LOG_NORMAL("bug: could not get some buffer\n");
777                 goto failed_buf;
778         }
779         memset(status_buffer, 0, sizeof (struct fsf_status_read_buffer));
780         fsf_req->data = (unsigned long) status_buffer;
781
782         /* insert pointer to respective buffer */
783         sbale = zfcp_qdio_sbale_curr(fsf_req);
784         sbale->addr = (void *) status_buffer;
785         sbale->length = sizeof(struct fsf_status_read_buffer);
786
787         retval = zfcp_fsf_req_send(fsf_req);
788         if (retval) {
789                 ZFCP_LOG_DEBUG("error: Could not set-up unsolicited status "
790                                "environment.\n");
791                 goto failed_req_send;
792         }
793
794         ZFCP_LOG_TRACE("Status Read request initiated (adapter%s)\n",
795                        zfcp_get_busid_by_adapter(adapter));
796         goto out;
797
798  failed_req_send:
799         mempool_free(status_buffer, adapter->pool.data_status_read);
800
801  failed_buf:
802         zfcp_fsf_req_free(fsf_req);
803  failed_req_create:
804         zfcp_hba_dbf_event_fsf_unsol("fail", adapter, NULL);
805  out:
806         write_unlock_irqrestore(&adapter->request_queue.queue_lock, lock_flags);
807         return retval;
808 }
809
810 static int
811 zfcp_fsf_status_read_port_closed(struct zfcp_fsf_req *fsf_req)
812 {
813         struct fsf_status_read_buffer *status_buffer;
814         struct zfcp_adapter *adapter;
815         struct zfcp_port *port;
816         unsigned long flags;
817
818         status_buffer = (struct fsf_status_read_buffer *) fsf_req->data;
819         adapter = fsf_req->adapter;
820
821         read_lock_irqsave(&zfcp_data.config_lock, flags);
822         list_for_each_entry(port, &adapter->port_list_head, list)
823             if (port->d_id == (status_buffer->d_id & ZFCP_DID_MASK))
824                 break;
825         read_unlock_irqrestore(&zfcp_data.config_lock, flags);
826
827         if (!port || (port->d_id != (status_buffer->d_id & ZFCP_DID_MASK))) {
828                 ZFCP_LOG_NORMAL("bug: Reopen port indication received for"
829                                 "nonexisting port with d_id 0x%08x on "
830                                 "adapter %s. Ignored.\n",
831                                 status_buffer->d_id & ZFCP_DID_MASK,
832                                 zfcp_get_busid_by_adapter(adapter));
833                 goto out;
834         }
835
836         switch (status_buffer->status_subtype) {
837
838         case FSF_STATUS_READ_SUB_CLOSE_PHYS_PORT:
839                 debug_text_event(adapter->erp_dbf, 3, "unsol_pc_phys:");
840                 zfcp_erp_port_reopen(port, 0);
841                 break;
842
843         case FSF_STATUS_READ_SUB_ERROR_PORT:
844                 debug_text_event(adapter->erp_dbf, 1, "unsol_pc_err:");
845                 zfcp_erp_port_shutdown(port, 0);
846                 break;
847
848         default:
849                 debug_text_event(adapter->erp_dbf, 0, "unsol_unk_sub:");
850                 debug_exception(adapter->erp_dbf, 0,
851                                 &status_buffer->status_subtype, sizeof (u32));
852                 ZFCP_LOG_NORMAL("bug: Undefined status subtype received "
853                                 "for a reopen indication on port with "
854                                 "d_id 0x%08x on the adapter %s. "
855                                 "Ignored. (debug info 0x%x)\n",
856                                 status_buffer->d_id,
857                                 zfcp_get_busid_by_adapter(adapter),
858                                 status_buffer->status_subtype);
859         }
860  out:
861         return 0;
862 }
863
864 /*
865  * function:    zfcp_fsf_status_read_handler
866  *
867  * purpose:     is called for finished Open Port command
868  *
869  * returns:     
870  */
871 static int
872 zfcp_fsf_status_read_handler(struct zfcp_fsf_req *fsf_req)
873 {
874         int retval = 0;
875         struct zfcp_adapter *adapter = fsf_req->adapter;
876         struct fsf_status_read_buffer *status_buffer =
877                 (struct fsf_status_read_buffer *) fsf_req->data;
878         struct fsf_bit_error_payload *fsf_bit_error;
879
880         if (fsf_req->status & ZFCP_STATUS_FSFREQ_DISMISSED) {
881                 zfcp_hba_dbf_event_fsf_unsol("dism", adapter, status_buffer);
882                 mempool_free(status_buffer, adapter->pool.data_status_read);
883                 zfcp_fsf_req_free(fsf_req);
884                 goto out;
885         }
886
887         zfcp_hba_dbf_event_fsf_unsol("read", adapter, status_buffer);
888
889         switch (status_buffer->status_type) {
890
891         case FSF_STATUS_READ_PORT_CLOSED:
892                 zfcp_fsf_status_read_port_closed(fsf_req);
893                 break;
894
895         case FSF_STATUS_READ_INCOMING_ELS:
896                 zfcp_fsf_incoming_els(fsf_req);
897                 break;
898
899         case FSF_STATUS_READ_SENSE_DATA_AVAIL:
900                 ZFCP_LOG_INFO("unsolicited sense data received (adapter %s)\n",
901                               zfcp_get_busid_by_adapter(adapter));
902                 break;
903
904         case FSF_STATUS_READ_BIT_ERROR_THRESHOLD:
905                 fsf_bit_error = (struct fsf_bit_error_payload *)
906                         status_buffer->payload;
907                 ZFCP_LOG_NORMAL("Warning: bit error threshold data "
908                     "received (adapter %s, "
909                     "link failures = %i, loss of sync errors = %i, "
910                     "loss of signal errors = %i, "
911                     "primitive sequence errors = %i, "
912                     "invalid transmission word errors = %i, "
913                     "CRC errors = %i)\n",
914                     zfcp_get_busid_by_adapter(adapter),
915                     fsf_bit_error->link_failure_error_count,
916                     fsf_bit_error->loss_of_sync_error_count,
917                     fsf_bit_error->loss_of_signal_error_count,
918                     fsf_bit_error->primitive_sequence_error_count,
919                     fsf_bit_error->invalid_transmission_word_error_count,
920                     fsf_bit_error->crc_error_count);
921                 ZFCP_LOG_INFO("Additional bit error threshold data "
922                     "(adapter %s, "
923                     "primitive sequence event time-outs = %i, "
924                     "elastic buffer overrun errors = %i, "
925                     "advertised receive buffer-to-buffer credit = %i, "
926                     "current receice buffer-to-buffer credit = %i, "
927                     "advertised transmit buffer-to-buffer credit = %i, "
928                     "current transmit buffer-to-buffer credit = %i)\n",
929                     zfcp_get_busid_by_adapter(adapter),
930                     fsf_bit_error->primitive_sequence_event_timeout_count,
931                     fsf_bit_error->elastic_buffer_overrun_error_count,
932                     fsf_bit_error->advertised_receive_b2b_credit,
933                     fsf_bit_error->current_receive_b2b_credit,
934                     fsf_bit_error->advertised_transmit_b2b_credit,
935                     fsf_bit_error->current_transmit_b2b_credit);
936                 break;
937
938         case FSF_STATUS_READ_LINK_DOWN:
939                 switch (status_buffer->status_subtype) {
940                 case FSF_STATUS_READ_SUB_NO_PHYSICAL_LINK:
941                         ZFCP_LOG_INFO("Physical link to adapter %s is down\n",
942                                       zfcp_get_busid_by_adapter(adapter));
943                         zfcp_fsf_link_down_info_eval(adapter,
944                                 (struct fsf_link_down_info *)
945                                 &status_buffer->payload);
946                         break;
947                 case FSF_STATUS_READ_SUB_FDISC_FAILED:
948                         ZFCP_LOG_INFO("Local link to adapter %s is down "
949                                       "due to failed FDISC login\n",
950                                       zfcp_get_busid_by_adapter(adapter));
951                         zfcp_fsf_link_down_info_eval(adapter,
952                                 (struct fsf_link_down_info *)
953                                 &status_buffer->payload);
954                         break;
955                 case FSF_STATUS_READ_SUB_FIRMWARE_UPDATE:
956                         ZFCP_LOG_INFO("Local link to adapter %s is down "
957                                       "due to firmware update on adapter\n",
958                                       zfcp_get_busid_by_adapter(adapter));
959                         zfcp_fsf_link_down_info_eval(adapter, NULL);
960                         break;
961                 default:
962                         ZFCP_LOG_INFO("Local link to adapter %s is down "
963                                       "due to unknown reason\n",
964                                       zfcp_get_busid_by_adapter(adapter));
965                         zfcp_fsf_link_down_info_eval(adapter, NULL);
966                 };
967                 break;
968
969         case FSF_STATUS_READ_LINK_UP:
970                 ZFCP_LOG_NORMAL("Local link to adapter %s was replugged. "
971                                 "Restarting operations on this adapter\n",
972                                 zfcp_get_busid_by_adapter(adapter));
973                 /* All ports should be marked as ready to run again */
974                 zfcp_erp_modify_adapter_status(adapter,
975                                                ZFCP_STATUS_COMMON_RUNNING,
976                                                ZFCP_SET);
977                 zfcp_erp_adapter_reopen(adapter,
978                                         ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED
979                                         | ZFCP_STATUS_COMMON_ERP_FAILED);
980                 break;
981
982         case FSF_STATUS_READ_NOTIFICATION_LOST:
983                 ZFCP_LOG_NORMAL("Unsolicited status notification(s) lost: "
984                                 "adapter %s%s%s%s%s%s%s%s%s\n",
985                                 zfcp_get_busid_by_adapter(adapter),
986                                 (status_buffer->status_subtype &
987                                         FSF_STATUS_READ_SUB_INCOMING_ELS) ?
988                                         ", incoming ELS" : "",
989                                 (status_buffer->status_subtype &
990                                         FSF_STATUS_READ_SUB_SENSE_DATA) ?
991                                         ", sense data" : "",
992                                 (status_buffer->status_subtype &
993                                         FSF_STATUS_READ_SUB_LINK_STATUS) ?
994                                         ", link status change" : "",
995                                 (status_buffer->status_subtype &
996                                         FSF_STATUS_READ_SUB_PORT_CLOSED) ?
997                                         ", port close" : "",
998                                 (status_buffer->status_subtype &
999                                         FSF_STATUS_READ_SUB_BIT_ERROR_THRESHOLD) ?
1000                                         ", bit error exception" : "",
1001                                 (status_buffer->status_subtype &
1002                                         FSF_STATUS_READ_SUB_ACT_UPDATED) ?
1003                                         ", ACT update" : "",
1004                                 (status_buffer->status_subtype &
1005                                         FSF_STATUS_READ_SUB_ACT_HARDENED) ?
1006                                         ", ACT hardening" : "",
1007                                 (status_buffer->status_subtype &
1008                                         FSF_STATUS_READ_SUB_FEATURE_UPDATE_ALERT) ?
1009                                         ", adapter feature change" : "");
1010
1011                 if (status_buffer->status_subtype &
1012                     FSF_STATUS_READ_SUB_ACT_UPDATED)
1013                         zfcp_erp_adapter_access_changed(adapter);
1014                 break;
1015
1016         case FSF_STATUS_READ_CFDC_UPDATED:
1017                 ZFCP_LOG_NORMAL("CFDC has been updated on the adapter %s\n",
1018                               zfcp_get_busid_by_adapter(adapter));
1019                 zfcp_erp_adapter_access_changed(adapter);
1020                 break;
1021
1022         case FSF_STATUS_READ_CFDC_HARDENED:
1023                 switch (status_buffer->status_subtype) {
1024                 case FSF_STATUS_READ_SUB_CFDC_HARDENED_ON_SE:
1025                         ZFCP_LOG_NORMAL("CFDC of adapter %s saved on SE\n",
1026                                       zfcp_get_busid_by_adapter(adapter));
1027                         break;
1028                 case FSF_STATUS_READ_SUB_CFDC_HARDENED_ON_SE2:
1029                         ZFCP_LOG_NORMAL("CFDC of adapter %s has been copied "
1030                                       "to the secondary SE\n",
1031                                 zfcp_get_busid_by_adapter(adapter));
1032                         break;
1033                 default:
1034                         ZFCP_LOG_NORMAL("CFDC of adapter %s has been hardened\n",
1035                                       zfcp_get_busid_by_adapter(adapter));
1036                 }
1037                 break;
1038
1039         case FSF_STATUS_READ_FEATURE_UPDATE_ALERT:
1040                 debug_text_event(adapter->erp_dbf, 2, "unsol_features:");
1041                 ZFCP_LOG_INFO("List of supported features on adapter %s has "
1042                               "been changed from 0x%08X to 0x%08X\n",
1043                               zfcp_get_busid_by_adapter(adapter),
1044                               *(u32*) (status_buffer->payload + 4),
1045                               *(u32*) (status_buffer->payload));
1046                 adapter->adapter_features = *(u32*) status_buffer->payload;
1047                 break;
1048
1049         default:
1050                 ZFCP_LOG_NORMAL("warning: An unsolicited status packet of unknown "
1051                                 "type was received (debug info 0x%x)\n",
1052                                 status_buffer->status_type);
1053                 ZFCP_LOG_DEBUG("Dump of status_read_buffer %p:\n",
1054                                status_buffer);
1055                 ZFCP_HEX_DUMP(ZFCP_LOG_LEVEL_DEBUG,
1056                               (char *) status_buffer,
1057                               sizeof (struct fsf_status_read_buffer));
1058                 break;
1059         }
1060         mempool_free(status_buffer, adapter->pool.data_status_read);
1061         zfcp_fsf_req_free(fsf_req);
1062         /*
1063          * recycle buffer and start new request repeat until outbound
1064          * queue is empty or adapter shutdown is requested
1065          */
1066         /*
1067          * FIXME(qdio):
1068          * we may wait in the req_create for 5s during shutdown, so
1069          * qdio_cleanup will have to wait at least that long before returning
1070          * with failure to allow us a proper cleanup under all circumstances
1071          */
1072         /*
1073          * FIXME:
1074          * allocation failure possible? (Is this code needed?)
1075          */
1076         retval = zfcp_fsf_status_read(adapter, 0);
1077         if (retval < 0) {
1078                 ZFCP_LOG_INFO("Failed to create unsolicited status read "
1079                               "request for the adapter %s.\n",
1080                               zfcp_get_busid_by_adapter(adapter));
1081                 /* temporary fix to avoid status read buffer shortage */
1082                 adapter->status_read_failed++;
1083                 if ((ZFCP_STATUS_READS_RECOM - adapter->status_read_failed)
1084                     < ZFCP_STATUS_READ_FAILED_THRESHOLD) {
1085                         ZFCP_LOG_INFO("restart adapter %s due to status read "
1086                                       "buffer shortage\n",
1087                                       zfcp_get_busid_by_adapter(adapter));
1088                         zfcp_erp_adapter_reopen(adapter, 0);
1089                 }
1090         }
1091  out:
1092         return retval;
1093 }
1094
1095 /*
1096  * function:    zfcp_fsf_abort_fcp_command
1097  *
1098  * purpose:     tells FSF to abort a running SCSI command
1099  *
1100  * returns:     address of initiated FSF request
1101  *              NULL - request could not be initiated
1102  *
1103  * FIXME(design): should be watched by a timeout !!! 
1104  * FIXME(design) shouldn't this be modified to return an int
1105  *               also...don't know how though
1106  */
1107 struct zfcp_fsf_req *
1108 zfcp_fsf_abort_fcp_command(unsigned long old_req_id,
1109                            struct zfcp_adapter *adapter,
1110                            struct zfcp_unit *unit, int req_flags)
1111 {
1112         volatile struct qdio_buffer_element *sbale;
1113         struct zfcp_fsf_req *fsf_req = NULL;
1114         unsigned long lock_flags;
1115         int retval = 0;
1116
1117         /* setup new FSF request */
1118         retval = zfcp_fsf_req_create(adapter, FSF_QTCB_ABORT_FCP_CMND,
1119                                      req_flags, adapter->pool.fsf_req_abort,
1120                                      &lock_flags, &fsf_req);
1121         if (retval < 0) {
1122                 ZFCP_LOG_INFO("error: Failed to create an abort command "
1123                               "request for lun 0x%016Lx on port 0x%016Lx "
1124                               "on adapter %s.\n",
1125                               unit->fcp_lun,
1126                               unit->port->wwpn,
1127                               zfcp_get_busid_by_adapter(adapter));
1128                 goto out;
1129         }
1130
1131         sbale = zfcp_qdio_sbale_req(fsf_req, fsf_req->sbal_curr, 0);
1132         sbale[0].flags |= SBAL_FLAGS0_TYPE_READ;
1133         sbale[1].flags |= SBAL_FLAGS_LAST_ENTRY;
1134
1135         fsf_req->data = (unsigned long) unit;
1136
1137         /* set handles of unit and its parent port in QTCB */
1138         fsf_req->qtcb->header.lun_handle = unit->handle;
1139         fsf_req->qtcb->header.port_handle = unit->port->handle;
1140
1141         /* set handle of request which should be aborted */
1142         fsf_req->qtcb->bottom.support.req_handle = (u64) old_req_id;
1143
1144         zfcp_fsf_start_timer(fsf_req, ZFCP_SCSI_ER_TIMEOUT);
1145         retval = zfcp_fsf_req_send(fsf_req);
1146         if (retval) {
1147                 ZFCP_LOG_INFO("error: Failed to send abort command request "
1148                               "on adapter %s, port 0x%016Lx, unit 0x%016Lx\n",
1149                               zfcp_get_busid_by_adapter(adapter),
1150                               unit->port->wwpn, unit->fcp_lun);
1151                 zfcp_fsf_req_free(fsf_req);
1152                 fsf_req = NULL;
1153                 goto out;
1154         }
1155
1156         ZFCP_LOG_DEBUG("Abort FCP Command request initiated "
1157                        "(adapter%s, port d_id=0x%08x, "
1158                        "unit x%016Lx, old_req_id=0x%lx)\n",
1159                        zfcp_get_busid_by_adapter(adapter),
1160                        unit->port->d_id,
1161                        unit->fcp_lun, old_req_id);
1162  out:
1163         write_unlock_irqrestore(&adapter->request_queue.queue_lock, lock_flags);
1164         return fsf_req;
1165 }
1166
1167 /*
1168  * function:    zfcp_fsf_abort_fcp_command_handler
1169  *
1170  * purpose:     is called for finished Abort FCP Command request
1171  *
1172  * returns:     
1173  */
1174 static int
1175 zfcp_fsf_abort_fcp_command_handler(struct zfcp_fsf_req *new_fsf_req)
1176 {
1177         int retval = -EINVAL;
1178         struct zfcp_unit *unit;
1179         unsigned char status_qual =
1180             new_fsf_req->qtcb->header.fsf_status_qual.word[0];
1181
1182         if (new_fsf_req->status & ZFCP_STATUS_FSFREQ_ERROR) {
1183                 /* do not set ZFCP_STATUS_FSFREQ_ABORTSUCCEEDED */
1184                 goto skip_fsfstatus;
1185         }
1186
1187         unit = (struct zfcp_unit *) new_fsf_req->data;
1188
1189         /* evaluate FSF status in QTCB */
1190         switch (new_fsf_req->qtcb->header.fsf_status) {
1191
1192         case FSF_PORT_HANDLE_NOT_VALID:
1193                 if (status_qual >> 4 != status_qual % 0xf) {
1194                         debug_text_event(new_fsf_req->adapter->erp_dbf, 3,
1195                                          "fsf_s_phand_nv0");
1196                         /*
1197                          * In this case a command that was sent prior to a port
1198                          * reopen was aborted (handles are different). This is
1199                          * fine.
1200                          */
1201                 } else {
1202                         ZFCP_LOG_INFO("Temporary port identifier 0x%x for "
1203                                       "port 0x%016Lx on adapter %s invalid. "
1204                                       "This may happen occasionally.\n",
1205                                       unit->port->handle,
1206                                       unit->port->wwpn,
1207                                       zfcp_get_busid_by_unit(unit));
1208                         ZFCP_LOG_INFO("status qualifier:\n");
1209                         ZFCP_HEX_DUMP(ZFCP_LOG_LEVEL_INFO,
1210                                       (char *) &new_fsf_req->qtcb->header.
1211                                       fsf_status_qual,
1212                                       sizeof (union fsf_status_qual));
1213                         /* Let's hope this sorts out the mess */
1214                         debug_text_event(new_fsf_req->adapter->erp_dbf, 1,
1215                                          "fsf_s_phand_nv1");
1216                         zfcp_erp_adapter_reopen(unit->port->adapter, 0);
1217                         new_fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1218                 }
1219                 break;
1220
1221         case FSF_LUN_HANDLE_NOT_VALID:
1222                 if (status_qual >> 4 != status_qual % 0xf) {
1223                         /* 2 */
1224                         debug_text_event(new_fsf_req->adapter->erp_dbf, 3,
1225                                          "fsf_s_lhand_nv0");
1226                         /*
1227                          * In this case a command that was sent prior to a unit
1228                          * reopen was aborted (handles are different).
1229                          * This is fine.
1230                          */
1231                 } else {
1232                         ZFCP_LOG_INFO
1233                             ("Warning: Temporary LUN identifier 0x%x of LUN "
1234                              "0x%016Lx on port 0x%016Lx on adapter %s is "
1235                              "invalid. This may happen in rare cases. "
1236                              "Trying to re-establish link.\n",
1237                              unit->handle,
1238                              unit->fcp_lun,
1239                              unit->port->wwpn,
1240                              zfcp_get_busid_by_unit(unit));
1241                         ZFCP_LOG_DEBUG("Status qualifier data:\n");
1242                         ZFCP_HEX_DUMP(ZFCP_LOG_LEVEL_DEBUG,
1243                                       (char *) &new_fsf_req->qtcb->header.
1244                                       fsf_status_qual,
1245                                       sizeof (union fsf_status_qual));
1246                         /* Let's hope this sorts out the mess */
1247                         debug_text_event(new_fsf_req->adapter->erp_dbf, 1,
1248                                          "fsf_s_lhand_nv1");
1249                         zfcp_erp_port_reopen(unit->port, 0);
1250                         new_fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1251                 }
1252                 break;
1253
1254         case FSF_FCP_COMMAND_DOES_NOT_EXIST:
1255                 retval = 0;
1256                 debug_text_event(new_fsf_req->adapter->erp_dbf, 3,
1257                                  "fsf_s_no_exist");
1258                 new_fsf_req->status |= ZFCP_STATUS_FSFREQ_ABORTNOTNEEDED;
1259                 break;
1260
1261         case FSF_PORT_BOXED:
1262                 ZFCP_LOG_INFO("Remote port 0x%016Lx on adapter %s needs to "
1263                               "be reopened\n", unit->port->wwpn,
1264                               zfcp_get_busid_by_unit(unit));
1265                 debug_text_event(new_fsf_req->adapter->erp_dbf, 2,
1266                                  "fsf_s_pboxed");
1267                 zfcp_erp_port_boxed(unit->port);
1268                 new_fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR
1269                     | ZFCP_STATUS_FSFREQ_RETRY;
1270                 break;
1271
1272         case FSF_LUN_BOXED:
1273                 ZFCP_LOG_INFO(
1274                         "unit 0x%016Lx on port 0x%016Lx on adapter %s needs "
1275                         "to be reopened\n",
1276                         unit->fcp_lun, unit->port->wwpn,
1277                         zfcp_get_busid_by_unit(unit));
1278                 debug_text_event(new_fsf_req->adapter->erp_dbf, 1, "fsf_s_lboxed");
1279                 zfcp_erp_unit_boxed(unit);
1280                 new_fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR
1281                         | ZFCP_STATUS_FSFREQ_RETRY;
1282                 break;
1283
1284         case FSF_ADAPTER_STATUS_AVAILABLE:
1285                 switch (new_fsf_req->qtcb->header.fsf_status_qual.word[0]) {
1286                 case FSF_SQ_INVOKE_LINK_TEST_PROCEDURE:
1287                         debug_text_event(new_fsf_req->adapter->erp_dbf, 1,
1288                                          "fsf_sq_ltest");
1289                         zfcp_test_link(unit->port);
1290                         new_fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1291                         break;
1292                 case FSF_SQ_ULP_DEPENDENT_ERP_REQUIRED:
1293                         /* SCSI stack will escalate */
1294                         debug_text_event(new_fsf_req->adapter->erp_dbf, 1,
1295                                          "fsf_sq_ulp");
1296                         new_fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1297                         break;
1298                 default:
1299                         ZFCP_LOG_NORMAL
1300                             ("bug: Wrong status qualifier 0x%x arrived.\n",
1301                              new_fsf_req->qtcb->header.fsf_status_qual.word[0]);
1302                         debug_text_event(new_fsf_req->adapter->erp_dbf, 0,
1303                                          "fsf_sq_inval:");
1304                         debug_exception(new_fsf_req->adapter->erp_dbf, 0,
1305                                         &new_fsf_req->qtcb->header.
1306                                         fsf_status_qual.word[0], sizeof (u32));
1307                         break;
1308                 }
1309                 break;
1310
1311         case FSF_GOOD:
1312                 retval = 0;
1313                 new_fsf_req->status |= ZFCP_STATUS_FSFREQ_ABORTSUCCEEDED;
1314                 break;
1315
1316         default:
1317                 ZFCP_LOG_NORMAL("bug: An unknown FSF Status was presented "
1318                                 "(debug info 0x%x)\n",
1319                                 new_fsf_req->qtcb->header.fsf_status);
1320                 debug_text_event(new_fsf_req->adapter->erp_dbf, 0,
1321                                  "fsf_s_inval:");
1322                 debug_exception(new_fsf_req->adapter->erp_dbf, 0,
1323                                 &new_fsf_req->qtcb->header.fsf_status,
1324                                 sizeof (u32));
1325                 break;
1326         }
1327  skip_fsfstatus:
1328         return retval;
1329 }
1330
1331 /**
1332  * zfcp_use_one_sbal - checks whether req buffer and resp bother each fit into
1333  *      one SBALE
1334  * Two scatter-gather lists are passed, one for the reqeust and one for the
1335  * response.
1336  */
1337 static inline int
1338 zfcp_use_one_sbal(struct scatterlist *req, int req_count,
1339                   struct scatterlist *resp, int resp_count)
1340 {
1341         return ((req_count == 1) &&
1342                 (resp_count == 1) &&
1343                 (((unsigned long) zfcp_sg_to_address(&req[0]) &
1344                   PAGE_MASK) ==
1345                  ((unsigned long) (zfcp_sg_to_address(&req[0]) +
1346                                    req[0].length - 1) & PAGE_MASK)) &&
1347                 (((unsigned long) zfcp_sg_to_address(&resp[0]) &
1348                   PAGE_MASK) ==
1349                  ((unsigned long) (zfcp_sg_to_address(&resp[0]) +
1350                                    resp[0].length - 1) & PAGE_MASK)));
1351 }
1352
1353 /**
1354  * zfcp_fsf_send_ct - initiate a Generic Service request (FC-GS)
1355  * @ct: pointer to struct zfcp_send_ct which conatins all needed data for
1356  *      the request
1357  * @pool: pointer to memory pool, if non-null this pool is used to allocate
1358  *      a struct zfcp_fsf_req
1359  * @erp_action: pointer to erp_action, if non-null the Generic Service request
1360  *      is sent within error recovery
1361  */
1362 int
1363 zfcp_fsf_send_ct(struct zfcp_send_ct *ct, mempool_t *pool,
1364                  struct zfcp_erp_action *erp_action)
1365 {
1366         volatile struct qdio_buffer_element *sbale;
1367         struct zfcp_port *port;
1368         struct zfcp_adapter *adapter;
1369         struct zfcp_fsf_req *fsf_req;
1370         unsigned long lock_flags;
1371         int bytes;
1372         int ret = 0;
1373
1374         port = ct->port;
1375         adapter = port->adapter;
1376
1377         ret = zfcp_fsf_req_create(adapter, FSF_QTCB_SEND_GENERIC,
1378                                   ZFCP_WAIT_FOR_SBAL | ZFCP_REQ_AUTO_CLEANUP,
1379                                   pool, &lock_flags, &fsf_req);
1380         if (ret < 0) {
1381                 ZFCP_LOG_INFO("error: Could not create CT request (FC-GS) for "
1382                               "adapter: %s\n",
1383                               zfcp_get_busid_by_adapter(adapter));
1384                 goto failed_req;
1385         }
1386
1387         sbale = zfcp_qdio_sbale_req(fsf_req, fsf_req->sbal_curr, 0);
1388         if (zfcp_use_one_sbal(ct->req, ct->req_count,
1389                               ct->resp, ct->resp_count)){
1390                 /* both request buffer and response buffer
1391                    fit into one sbale each */
1392                 sbale[0].flags |= SBAL_FLAGS0_TYPE_WRITE_READ;
1393                 sbale[2].addr = zfcp_sg_to_address(&ct->req[0]);
1394                 sbale[2].length = ct->req[0].length;
1395                 sbale[3].addr = zfcp_sg_to_address(&ct->resp[0]);
1396                 sbale[3].length = ct->resp[0].length;
1397                 sbale[3].flags |= SBAL_FLAGS_LAST_ENTRY;
1398         } else if (adapter->adapter_features &
1399                    FSF_FEATURE_ELS_CT_CHAINED_SBALS) {
1400                 /* try to use chained SBALs */
1401                 bytes = zfcp_qdio_sbals_from_sg(fsf_req,
1402                                                 SBAL_FLAGS0_TYPE_WRITE_READ,
1403                                                 ct->req, ct->req_count,
1404                                                 ZFCP_MAX_SBALS_PER_CT_REQ);
1405                 if (bytes <= 0) {
1406                         ZFCP_LOG_INFO("error: creation of CT request failed "
1407                                       "on adapter %s\n",
1408                                       zfcp_get_busid_by_adapter(adapter));
1409                         if (bytes == 0)
1410                                 ret = -ENOMEM;
1411                         else
1412                                 ret = bytes;
1413
1414                         goto failed_send;
1415                 }
1416                 fsf_req->qtcb->bottom.support.req_buf_length = bytes;
1417                 fsf_req->sbale_curr = ZFCP_LAST_SBALE_PER_SBAL;
1418                 bytes = zfcp_qdio_sbals_from_sg(fsf_req,
1419                                                 SBAL_FLAGS0_TYPE_WRITE_READ,
1420                                                 ct->resp, ct->resp_count,
1421                                                 ZFCP_MAX_SBALS_PER_CT_REQ);
1422                 if (bytes <= 0) {
1423                         ZFCP_LOG_INFO("error: creation of CT request failed "
1424                                       "on adapter %s\n",
1425                                       zfcp_get_busid_by_adapter(adapter));
1426                         if (bytes == 0)
1427                                 ret = -ENOMEM;
1428                         else
1429                                 ret = bytes;
1430
1431                         goto failed_send;
1432                 }
1433                 fsf_req->qtcb->bottom.support.resp_buf_length = bytes;
1434         } else {
1435                 /* reject send generic request */
1436                 ZFCP_LOG_INFO(
1437                         "error: microcode does not support chained SBALs,"
1438                         "CT request too big (adapter %s)\n",
1439                         zfcp_get_busid_by_adapter(adapter));
1440                 ret = -EOPNOTSUPP;
1441                 goto failed_send;
1442         }
1443
1444         /* settings in QTCB */
1445         fsf_req->qtcb->header.port_handle = port->handle;
1446         fsf_req->qtcb->bottom.support.service_class =
1447                 ZFCP_FC_SERVICE_CLASS_DEFAULT;
1448         fsf_req->qtcb->bottom.support.timeout = ct->timeout;
1449         fsf_req->data = (unsigned long) ct;
1450
1451         zfcp_san_dbf_event_ct_request(fsf_req);
1452
1453         if (erp_action) {
1454                 erp_action->fsf_req = fsf_req;
1455                 fsf_req->erp_action = erp_action;
1456                 zfcp_erp_start_timer(fsf_req);
1457         } else
1458                 zfcp_fsf_start_timer(fsf_req, ZFCP_FSF_REQUEST_TIMEOUT);
1459
1460         ret = zfcp_fsf_req_send(fsf_req);
1461         if (ret) {
1462                 ZFCP_LOG_DEBUG("error: initiation of CT request failed "
1463                                "(adapter %s, port 0x%016Lx)\n",
1464                                zfcp_get_busid_by_adapter(adapter), port->wwpn);
1465                 goto failed_send;
1466         }
1467
1468         ZFCP_LOG_DEBUG("CT request initiated (adapter %s, port 0x%016Lx)\n",
1469                        zfcp_get_busid_by_adapter(adapter), port->wwpn);
1470         goto out;
1471
1472  failed_send:
1473         zfcp_fsf_req_free(fsf_req);
1474         if (erp_action != NULL) {
1475                 erp_action->fsf_req = NULL;
1476         }
1477  failed_req:
1478  out:
1479         write_unlock_irqrestore(&adapter->request_queue.queue_lock,
1480                                 lock_flags);
1481         return ret;
1482 }
1483
1484 /**
1485  * zfcp_fsf_send_ct_handler - handler for Generic Service requests
1486  * @fsf_req: pointer to struct zfcp_fsf_req
1487  *
1488  * Data specific for the Generic Service request is passed using
1489  * fsf_req->data. There we find the pointer to struct zfcp_send_ct.
1490  * Usually a specific handler for the CT request is called which is
1491  * found in this structure.
1492  */
1493 static int
1494 zfcp_fsf_send_ct_handler(struct zfcp_fsf_req *fsf_req)
1495 {
1496         struct zfcp_port *port;
1497         struct zfcp_adapter *adapter;
1498         struct zfcp_send_ct *send_ct;
1499         struct fsf_qtcb_header *header;
1500         struct fsf_qtcb_bottom_support *bottom;
1501         int retval = -EINVAL;
1502         u16 subtable, rule, counter;
1503
1504         adapter = fsf_req->adapter;
1505         send_ct = (struct zfcp_send_ct *) fsf_req->data;
1506         port = send_ct->port;
1507         header = &fsf_req->qtcb->header;
1508         bottom = &fsf_req->qtcb->bottom.support;
1509
1510         if (fsf_req->status & ZFCP_STATUS_FSFREQ_ERROR)
1511                 goto skip_fsfstatus;
1512
1513         /* evaluate FSF status in QTCB */
1514         switch (header->fsf_status) {
1515
1516         case FSF_GOOD:
1517                 zfcp_san_dbf_event_ct_response(fsf_req);
1518                 retval = 0;
1519                 break;
1520
1521         case FSF_SERVICE_CLASS_NOT_SUPPORTED:
1522                 ZFCP_LOG_INFO("error: adapter %s does not support fc "
1523                               "class %d.\n",
1524                               zfcp_get_busid_by_port(port),
1525                               ZFCP_FC_SERVICE_CLASS_DEFAULT);
1526                 /* stop operation for this adapter */
1527                 debug_text_exception(adapter->erp_dbf, 0, "fsf_s_class_nsup");
1528                 zfcp_erp_adapter_shutdown(adapter, 0);
1529                 fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1530                 break;
1531
1532         case FSF_ADAPTER_STATUS_AVAILABLE:
1533                 switch (header->fsf_status_qual.word[0]){
1534                 case FSF_SQ_INVOKE_LINK_TEST_PROCEDURE:
1535                         /* reopening link to port */
1536                         debug_text_event(adapter->erp_dbf, 1, "fsf_sq_ltest");
1537                         zfcp_test_link(port);
1538                         fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1539                         break;
1540                 case FSF_SQ_ULP_DEPENDENT_ERP_REQUIRED:
1541                         /* ERP strategy will escalate */
1542                         debug_text_event(adapter->erp_dbf, 1, "fsf_sq_ulp");
1543                         fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1544                         break;
1545                 default:
1546                         ZFCP_LOG_INFO("bug: Wrong status qualifier 0x%x "
1547                                       "arrived.\n",
1548                                       header->fsf_status_qual.word[0]);
1549                         break;
1550                 }
1551                 break;
1552
1553         case FSF_ACCESS_DENIED:
1554                 ZFCP_LOG_NORMAL("access denied, cannot send generic service "
1555                                 "command (adapter %s, port d_id=0x%08x)\n",
1556                                 zfcp_get_busid_by_port(port), port->d_id);
1557                 for (counter = 0; counter < 2; counter++) {
1558                         subtable = header->fsf_status_qual.halfword[counter * 2];
1559                         rule = header->fsf_status_qual.halfword[counter * 2 + 1];
1560                         switch (subtable) {
1561                         case FSF_SQ_CFDC_SUBTABLE_OS:
1562                         case FSF_SQ_CFDC_SUBTABLE_PORT_WWPN:
1563                         case FSF_SQ_CFDC_SUBTABLE_PORT_DID:
1564                         case FSF_SQ_CFDC_SUBTABLE_LUN:
1565                                 ZFCP_LOG_INFO("Access denied (%s rule %d)\n",
1566                                         zfcp_act_subtable_type[subtable], rule);
1567                                 break;
1568                         }
1569                 }
1570                 debug_text_event(adapter->erp_dbf, 1, "fsf_s_access");
1571                 zfcp_erp_port_access_denied(port);
1572                 fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1573                 break;
1574
1575         case FSF_GENERIC_COMMAND_REJECTED:
1576                 ZFCP_LOG_INFO("generic service command rejected "
1577                               "(adapter %s, port d_id=0x%08x)\n",
1578                               zfcp_get_busid_by_port(port), port->d_id);
1579                 ZFCP_LOG_INFO("status qualifier:\n");
1580                 ZFCP_HEX_DUMP(ZFCP_LOG_LEVEL_INFO,
1581                               (char *) &header->fsf_status_qual,
1582                               sizeof (union fsf_status_qual));
1583                 debug_text_event(adapter->erp_dbf, 1, "fsf_s_gcom_rej");
1584                 fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1585                 break;
1586
1587         case FSF_PORT_HANDLE_NOT_VALID:
1588                 ZFCP_LOG_DEBUG("Temporary port identifier 0x%x for port "
1589                                "0x%016Lx on adapter %s invalid. This may "
1590                                "happen occasionally.\n", port->handle,
1591                                port->wwpn, zfcp_get_busid_by_port(port));
1592                 ZFCP_LOG_INFO("status qualifier:\n");
1593                 ZFCP_HEX_DUMP(ZFCP_LOG_LEVEL_INFO,
1594                               (char *) &header->fsf_status_qual,
1595                               sizeof (union fsf_status_qual));
1596                 debug_text_event(adapter->erp_dbf, 1, "fsf_s_phandle_nv");
1597                 zfcp_erp_adapter_reopen(adapter, 0);
1598                 fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1599                 break;
1600
1601         case FSF_PORT_BOXED:
1602                 ZFCP_LOG_INFO("port needs to be reopened "
1603                               "(adapter %s, port d_id=0x%08x)\n",
1604                               zfcp_get_busid_by_port(port), port->d_id);
1605                 debug_text_event(adapter->erp_dbf, 2, "fsf_s_pboxed");
1606                 zfcp_erp_port_boxed(port);
1607                 fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR
1608                     | ZFCP_STATUS_FSFREQ_RETRY;
1609                 break;
1610
1611         /* following states should never occure, all cases avoided
1612            in zfcp_fsf_send_ct - but who knows ... */
1613         case FSF_PAYLOAD_SIZE_MISMATCH:
1614                 ZFCP_LOG_INFO("payload size mismatch (adapter: %s, "
1615                               "req_buf_length=%d, resp_buf_length=%d)\n",
1616                               zfcp_get_busid_by_adapter(adapter),
1617                               bottom->req_buf_length, bottom->resp_buf_length);
1618                 fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1619                 break;
1620         case FSF_REQUEST_SIZE_TOO_LARGE:
1621                 ZFCP_LOG_INFO("request size too large (adapter: %s, "
1622                               "req_buf_length=%d)\n",
1623                               zfcp_get_busid_by_adapter(adapter),
1624                               bottom->req_buf_length);
1625                 fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1626                 break;
1627         case FSF_RESPONSE_SIZE_TOO_LARGE:
1628                 ZFCP_LOG_INFO("response size too large (adapter: %s, "
1629                               "resp_buf_length=%d)\n",
1630                               zfcp_get_busid_by_adapter(adapter),
1631                               bottom->resp_buf_length);
1632                 fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1633                 break;
1634         case FSF_SBAL_MISMATCH:
1635                 ZFCP_LOG_INFO("SBAL mismatch (adapter: %s, req_buf_length=%d, "
1636                               "resp_buf_length=%d)\n",
1637                               zfcp_get_busid_by_adapter(adapter),
1638                               bottom->req_buf_length, bottom->resp_buf_length);
1639                 fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1640                 break;
1641
1642        default:
1643                 ZFCP_LOG_NORMAL("bug: An unknown FSF Status was presented "
1644                                 "(debug info 0x%x)\n", header->fsf_status);
1645                 debug_text_event(adapter->erp_dbf, 0, "fsf_sq_inval:");
1646                 debug_exception(adapter->erp_dbf, 0,
1647                                 &header->fsf_status_qual.word[0], sizeof (u32));
1648                 break;
1649         }
1650
1651 skip_fsfstatus:
1652         send_ct->status = retval;
1653
1654         if (send_ct->handler != NULL)
1655                 send_ct->handler(send_ct->handler_data);
1656
1657         return retval;
1658 }
1659
1660 /**
1661  * zfcp_fsf_send_els - initiate an ELS command (FC-FS)
1662  * @els: pointer to struct zfcp_send_els which contains all needed data for
1663  *      the command.
1664  */
1665 int
1666 zfcp_fsf_send_els(struct zfcp_send_els *els)
1667 {
1668         volatile struct qdio_buffer_element *sbale;
1669         struct zfcp_fsf_req *fsf_req;
1670         u32 d_id;
1671         struct zfcp_adapter *adapter;
1672         unsigned long lock_flags;
1673         int bytes;
1674         int ret = 0;
1675
1676         d_id = els->d_id;
1677         adapter = els->adapter;
1678
1679         ret = zfcp_fsf_req_create(adapter, FSF_QTCB_SEND_ELS,
1680                                   ZFCP_REQ_AUTO_CLEANUP,
1681                                   NULL, &lock_flags, &fsf_req);
1682         if (ret < 0) {
1683                 ZFCP_LOG_INFO("error: creation of ELS request failed "
1684                               "(adapter %s, port d_id: 0x%08x)\n",
1685                               zfcp_get_busid_by_adapter(adapter), d_id);
1686                 goto failed_req;
1687         }
1688
1689         sbale = zfcp_qdio_sbale_req(fsf_req, fsf_req->sbal_curr, 0);
1690         if (zfcp_use_one_sbal(els->req, els->req_count,
1691                               els->resp, els->resp_count)){
1692                 /* both request buffer and response buffer
1693                    fit into one sbale each */
1694                 sbale[0].flags |= SBAL_FLAGS0_TYPE_WRITE_READ;
1695                 sbale[2].addr = zfcp_sg_to_address(&els->req[0]);
1696                 sbale[2].length = els->req[0].length;
1697                 sbale[3].addr = zfcp_sg_to_address(&els->resp[0]);
1698                 sbale[3].length = els->resp[0].length;
1699                 sbale[3].flags |= SBAL_FLAGS_LAST_ENTRY;
1700         } else if (adapter->adapter_features &
1701                    FSF_FEATURE_ELS_CT_CHAINED_SBALS) {
1702                 /* try to use chained SBALs */
1703                 bytes = zfcp_qdio_sbals_from_sg(fsf_req,
1704                                                 SBAL_FLAGS0_TYPE_WRITE_READ,
1705                                                 els->req, els->req_count,
1706                                                 ZFCP_MAX_SBALS_PER_ELS_REQ);
1707                 if (bytes <= 0) {
1708                         ZFCP_LOG_INFO("error: creation of ELS request failed "
1709                                       "(adapter %s, port d_id: 0x%08x)\n",
1710                                       zfcp_get_busid_by_adapter(adapter), d_id);
1711                         if (bytes == 0) {
1712                                 ret = -ENOMEM;
1713                         } else {
1714                                 ret = bytes;
1715                         }
1716                         goto failed_send;
1717                 }
1718                 fsf_req->qtcb->bottom.support.req_buf_length = bytes;
1719                 fsf_req->sbale_curr = ZFCP_LAST_SBALE_PER_SBAL;
1720                 bytes = zfcp_qdio_sbals_from_sg(fsf_req,
1721                                                 SBAL_FLAGS0_TYPE_WRITE_READ,
1722                                                 els->resp, els->resp_count,
1723                                                 ZFCP_MAX_SBALS_PER_ELS_REQ);
1724                 if (bytes <= 0) {
1725                         ZFCP_LOG_INFO("error: creation of ELS request failed "
1726                                       "(adapter %s, port d_id: 0x%08x)\n",
1727                                       zfcp_get_busid_by_adapter(adapter), d_id);
1728                         if (bytes == 0) {
1729                                 ret = -ENOMEM;
1730                         } else {
1731                                 ret = bytes;
1732                         }
1733                         goto failed_send;
1734                 }
1735                 fsf_req->qtcb->bottom.support.resp_buf_length = bytes;
1736         } else {
1737                 /* reject request */
1738                 ZFCP_LOG_INFO("error: microcode does not support chained SBALs"
1739                               ", ELS request too big (adapter %s, "
1740                               "port d_id: 0x%08x)\n",
1741                               zfcp_get_busid_by_adapter(adapter), d_id);
1742                 ret = -EOPNOTSUPP;
1743                 goto failed_send;
1744         }
1745
1746         /* settings in QTCB */
1747         fsf_req->qtcb->bottom.support.d_id = d_id;
1748         fsf_req->qtcb->bottom.support.service_class =
1749                 ZFCP_FC_SERVICE_CLASS_DEFAULT;
1750         fsf_req->qtcb->bottom.support.timeout = ZFCP_ELS_TIMEOUT;
1751         fsf_req->data = (unsigned long) els;
1752
1753         sbale = zfcp_qdio_sbale_req(fsf_req, fsf_req->sbal_curr, 0);
1754
1755         zfcp_san_dbf_event_els_request(fsf_req);
1756
1757         zfcp_fsf_start_timer(fsf_req, ZFCP_FSF_REQUEST_TIMEOUT);
1758         ret = zfcp_fsf_req_send(fsf_req);
1759         if (ret) {
1760                 ZFCP_LOG_DEBUG("error: initiation of ELS request failed "
1761                                "(adapter %s, port d_id: 0x%08x)\n",
1762                                zfcp_get_busid_by_adapter(adapter), d_id);
1763                 goto failed_send;
1764         }
1765
1766         ZFCP_LOG_DEBUG("ELS request initiated (adapter %s, port d_id: "
1767                        "0x%08x)\n", zfcp_get_busid_by_adapter(adapter), d_id);
1768         goto out;
1769
1770  failed_send:
1771         zfcp_fsf_req_free(fsf_req);
1772
1773  failed_req:
1774  out:
1775         write_unlock_irqrestore(&adapter->request_queue.queue_lock,
1776                                 lock_flags);
1777
1778         return ret;
1779 }
1780
1781 /**
1782  * zfcp_fsf_send_els_handler - handler for ELS commands
1783  * @fsf_req: pointer to struct zfcp_fsf_req
1784  *
1785  * Data specific for the ELS command is passed using
1786  * fsf_req->data. There we find the pointer to struct zfcp_send_els.
1787  * Usually a specific handler for the ELS command is called which is
1788  * found in this structure.
1789  */
1790 static int zfcp_fsf_send_els_handler(struct zfcp_fsf_req *fsf_req)
1791 {
1792         struct zfcp_adapter *adapter;
1793         struct zfcp_port *port;
1794         u32 d_id;
1795         struct fsf_qtcb_header *header;
1796         struct fsf_qtcb_bottom_support *bottom;
1797         struct zfcp_send_els *send_els;
1798         int retval = -EINVAL;
1799         u16 subtable, rule, counter;
1800
1801         send_els = (struct zfcp_send_els *) fsf_req->data;
1802         adapter = send_els->adapter;
1803         port = send_els->port;
1804         d_id = send_els->d_id;
1805         header = &fsf_req->qtcb->header;
1806         bottom = &fsf_req->qtcb->bottom.support;
1807
1808         if (fsf_req->status & ZFCP_STATUS_FSFREQ_ERROR)
1809                 goto skip_fsfstatus;
1810
1811         switch (header->fsf_status) {
1812
1813         case FSF_GOOD:
1814                 zfcp_san_dbf_event_els_response(fsf_req);
1815                 retval = 0;
1816                 break;
1817
1818         case FSF_SERVICE_CLASS_NOT_SUPPORTED:
1819                 ZFCP_LOG_INFO("error: adapter %s does not support fc "
1820                               "class %d.\n",
1821                               zfcp_get_busid_by_adapter(adapter),
1822                               ZFCP_FC_SERVICE_CLASS_DEFAULT);
1823                 /* stop operation for this adapter */
1824                 debug_text_exception(adapter->erp_dbf, 0, "fsf_s_class_nsup");
1825                 zfcp_erp_adapter_shutdown(adapter, 0);
1826                 fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1827                 break;
1828
1829         case FSF_ADAPTER_STATUS_AVAILABLE:
1830                 switch (header->fsf_status_qual.word[0]){
1831                 case FSF_SQ_INVOKE_LINK_TEST_PROCEDURE:
1832                         debug_text_event(adapter->erp_dbf, 1, "fsf_sq_ltest");
1833                         if (port && (send_els->ls_code != ZFCP_LS_ADISC))
1834                                 zfcp_test_link(port);
1835                         fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1836                         break;
1837                 case FSF_SQ_ULP_DEPENDENT_ERP_REQUIRED:
1838                         debug_text_event(adapter->erp_dbf, 1, "fsf_sq_ulp");
1839                         fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1840                         retval =
1841                           zfcp_handle_els_rjt(header->fsf_status_qual.word[1],
1842                                               (struct zfcp_ls_rjt_par *)
1843                                               &header->fsf_status_qual.word[2]);
1844                         break;
1845                 case FSF_SQ_RETRY_IF_POSSIBLE:
1846                         debug_text_event(adapter->erp_dbf, 1, "fsf_sq_retry");
1847                         fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1848                         break;
1849                 default:
1850                         ZFCP_LOG_INFO("bug: Wrong status qualifier 0x%x\n",
1851                                       header->fsf_status_qual.word[0]);
1852                         ZFCP_HEX_DUMP(ZFCP_LOG_LEVEL_INFO,
1853                                 (char*)header->fsf_status_qual.word, 16);
1854                 }
1855                 break;
1856
1857         case FSF_ELS_COMMAND_REJECTED:
1858                 ZFCP_LOG_INFO("ELS has been rejected because command filter "
1859                               "prohibited sending "
1860                               "(adapter: %s, port d_id: 0x%08x)\n",
1861                               zfcp_get_busid_by_adapter(adapter), d_id);
1862
1863                 break;
1864
1865         case FSF_PAYLOAD_SIZE_MISMATCH:
1866                 ZFCP_LOG_INFO(
1867                         "ELS request size and ELS response size must be either "
1868                         "both 0, or both greater than 0 "
1869                         "(adapter: %s, req_buf_length=%d resp_buf_length=%d)\n",
1870                         zfcp_get_busid_by_adapter(adapter),
1871                         bottom->req_buf_length,
1872                         bottom->resp_buf_length);
1873                 break;
1874
1875         case FSF_REQUEST_SIZE_TOO_LARGE:
1876                 ZFCP_LOG_INFO(
1877                         "Length of the ELS request buffer, "
1878                         "specified in QTCB bottom, "
1879                         "exceeds the size of the buffers "
1880                         "that have been allocated for ELS request data "
1881                         "(adapter: %s, req_buf_length=%d)\n",
1882                         zfcp_get_busid_by_adapter(adapter),
1883                         bottom->req_buf_length);
1884                 break;
1885
1886         case FSF_RESPONSE_SIZE_TOO_LARGE:
1887                 ZFCP_LOG_INFO(
1888                         "Length of the ELS response buffer, "
1889                         "specified in QTCB bottom, "
1890                         "exceeds the size of the buffers "
1891                         "that have been allocated for ELS response data "
1892                         "(adapter: %s, resp_buf_length=%d)\n",
1893                         zfcp_get_busid_by_adapter(adapter),
1894                         bottom->resp_buf_length);
1895                 break;
1896
1897         case FSF_SBAL_MISMATCH:
1898                 /* should never occure, avoided in zfcp_fsf_send_els */
1899                 ZFCP_LOG_INFO("SBAL mismatch (adapter: %s, req_buf_length=%d, "
1900                               "resp_buf_length=%d)\n",
1901                               zfcp_get_busid_by_adapter(adapter),
1902                               bottom->req_buf_length, bottom->resp_buf_length);
1903                 fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1904                 break;
1905
1906         case FSF_ACCESS_DENIED:
1907                 ZFCP_LOG_NORMAL("access denied, cannot send ELS command "
1908                                 "(adapter %s, port d_id=0x%08x)\n",
1909                                 zfcp_get_busid_by_adapter(adapter), d_id);
1910                 for (counter = 0; counter < 2; counter++) {
1911                         subtable = header->fsf_status_qual.halfword[counter * 2];
1912                         rule = header->fsf_status_qual.halfword[counter * 2 + 1];
1913                         switch (subtable) {
1914                         case FSF_SQ_CFDC_SUBTABLE_OS:
1915                         case FSF_SQ_CFDC_SUBTABLE_PORT_WWPN:
1916                         case FSF_SQ_CFDC_SUBTABLE_PORT_DID:
1917                         case FSF_SQ_CFDC_SUBTABLE_LUN:
1918                                 ZFCP_LOG_INFO("Access denied (%s rule %d)\n",
1919                                         zfcp_act_subtable_type[subtable], rule);
1920                                 break;
1921                         }
1922                 }
1923                 debug_text_event(adapter->erp_dbf, 1, "fsf_s_access");
1924                 if (port != NULL)
1925                         zfcp_erp_port_access_denied(port);
1926                 fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1927                 break;
1928
1929         default:
1930                 ZFCP_LOG_NORMAL(
1931                         "bug: An unknown FSF Status was presented "
1932                         "(adapter: %s, fsf_status=0x%08x)\n",
1933                         zfcp_get_busid_by_adapter(adapter),
1934                         header->fsf_status);
1935                 debug_text_event(adapter->erp_dbf, 0, "fsf_sq_inval");
1936                 debug_exception(adapter->erp_dbf, 0,
1937                         &header->fsf_status_qual.word[0], sizeof(u32));
1938                 fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1939                 break;
1940         }
1941
1942 skip_fsfstatus:
1943         send_els->status = retval;
1944
1945         if (send_els->handler != 0)
1946                 send_els->handler(send_els->handler_data);
1947
1948         return retval;
1949 }
1950
1951 int
1952 zfcp_fsf_exchange_config_data(struct zfcp_erp_action *erp_action)
1953 {
1954         volatile struct qdio_buffer_element *sbale;
1955         struct zfcp_fsf_req *fsf_req;
1956         unsigned long lock_flags;
1957         int retval = 0;
1958
1959         /* setup new FSF request */
1960         retval = zfcp_fsf_req_create(erp_action->adapter,
1961                                      FSF_QTCB_EXCHANGE_CONFIG_DATA,
1962                                      ZFCP_REQ_AUTO_CLEANUP,
1963                                      erp_action->adapter->pool.fsf_req_erp,
1964                                      &lock_flags, &fsf_req);
1965         if (retval < 0) {
1966                 ZFCP_LOG_INFO("error: Could not create exchange configuration "
1967                               "data request for adapter %s.\n",
1968                               zfcp_get_busid_by_adapter(erp_action->adapter));
1969                 goto out;
1970         }
1971
1972         sbale = zfcp_qdio_sbale_req(fsf_req, fsf_req->sbal_curr, 0);
1973         sbale[0].flags |= SBAL_FLAGS0_TYPE_READ;
1974         sbale[1].flags |= SBAL_FLAGS_LAST_ENTRY;
1975
1976         fsf_req->qtcb->bottom.config.feature_selection =
1977                         FSF_FEATURE_CFDC |
1978                         FSF_FEATURE_LUN_SHARING |
1979                         FSF_FEATURE_NOTIFICATION_LOST |
1980                         FSF_FEATURE_UPDATE_ALERT;
1981         fsf_req->erp_action = erp_action;
1982         erp_action->fsf_req = fsf_req;
1983
1984         zfcp_erp_start_timer(fsf_req);
1985         retval = zfcp_fsf_req_send(fsf_req);
1986         if (retval) {
1987                 ZFCP_LOG_INFO
1988                     ("error: Could not send exchange configuration data "
1989                      "command on the adapter %s\n",
1990                      zfcp_get_busid_by_adapter(erp_action->adapter));
1991                 zfcp_fsf_req_free(fsf_req);
1992                 erp_action->fsf_req = NULL;
1993                 goto out;
1994         }
1995
1996         ZFCP_LOG_DEBUG("exchange configuration data request initiated "
1997                        "(adapter %s)\n",
1998                        zfcp_get_busid_by_adapter(erp_action->adapter));
1999
2000  out:
2001         write_unlock_irqrestore(&erp_action->adapter->request_queue.queue_lock,
2002                                 lock_flags);
2003         return retval;
2004 }
2005
2006 /**
2007  * zfcp_fsf_exchange_config_evaluate
2008  * @fsf_req: fsf_req which belongs to xchg config data request
2009  * @xchg_ok: specifies if xchg config data was incomplete or complete (0/1)
2010  *
2011  * returns: -EIO on error, 0 otherwise
2012  */
2013 static int
2014 zfcp_fsf_exchange_config_evaluate(struct zfcp_fsf_req *fsf_req, int xchg_ok)
2015 {
2016         struct fsf_qtcb_bottom_config *bottom;
2017         struct zfcp_adapter *adapter = fsf_req->adapter;
2018         struct Scsi_Host *shost = adapter->scsi_host;
2019
2020         bottom = &fsf_req->qtcb->bottom.config;
2021         ZFCP_LOG_DEBUG("low/high QTCB version 0x%x/0x%x of FSF\n",
2022                        bottom->low_qtcb_version, bottom->high_qtcb_version);
2023         adapter->fsf_lic_version = bottom->lic_version;
2024         adapter->adapter_features = bottom->adapter_features;
2025         adapter->connection_features = bottom->connection_features;
2026         adapter->peer_wwpn = 0;
2027         adapter->peer_wwnn = 0;
2028         adapter->peer_d_id = 0;
2029
2030         if (xchg_ok) {
2031                 fc_host_node_name(shost) = bottom->nport_serv_param.wwnn;
2032                 fc_host_port_name(shost) = bottom->nport_serv_param.wwpn;
2033                 fc_host_port_id(shost) = bottom->s_id & ZFCP_DID_MASK;
2034                 fc_host_speed(shost) = bottom->fc_link_speed;
2035                 fc_host_supported_classes(shost) = FC_COS_CLASS2 | FC_COS_CLASS3;
2036                 adapter->hydra_version = bottom->adapter_type;
2037                 if (fc_host_permanent_port_name(shost) == -1)
2038                         fc_host_permanent_port_name(shost) =
2039                                 fc_host_port_name(shost);
2040                 if (bottom->fc_topology == FSF_TOPO_P2P) {
2041                         adapter->peer_d_id = bottom->peer_d_id & ZFCP_DID_MASK;
2042                         adapter->peer_wwpn = bottom->plogi_payload.wwpn;
2043                         adapter->peer_wwnn = bottom->plogi_payload.wwnn;
2044                         fc_host_port_type(shost) = FC_PORTTYPE_PTP;
2045                 } else if (bottom->fc_topology == FSF_TOPO_FABRIC)
2046                         fc_host_port_type(shost) = FC_PORTTYPE_NPORT;
2047                 else if (bottom->fc_topology == FSF_TOPO_AL)
2048                         fc_host_port_type(shost) = FC_PORTTYPE_NLPORT;
2049                 else
2050                         fc_host_port_type(shost) = FC_PORTTYPE_UNKNOWN;
2051         } else {
2052                 fc_host_node_name(shost) = 0;
2053                 fc_host_port_name(shost) = 0;
2054                 fc_host_port_id(shost) = 0;
2055                 fc_host_speed(shost) = FC_PORTSPEED_UNKNOWN;
2056                 fc_host_port_type(shost) = FC_PORTTYPE_UNKNOWN;
2057                 adapter->hydra_version = 0;
2058         }
2059
2060         if (adapter->adapter_features & FSF_FEATURE_HBAAPI_MANAGEMENT) {
2061                 adapter->hardware_version = bottom->hardware_version;
2062                 memcpy(fc_host_serial_number(shost), bottom->serial_number,
2063                        min(FC_SERIAL_NUMBER_SIZE, 17));
2064                 EBCASC(fc_host_serial_number(shost),
2065                        min(FC_SERIAL_NUMBER_SIZE, 17));
2066         }
2067
2068         ZFCP_LOG_NORMAL("The adapter %s reported the following characteristics:\n"
2069                         "WWNN 0x%016Lx, "
2070                         "WWPN 0x%016Lx, "
2071                         "S_ID 0x%08x,\n"
2072                         "adapter version 0x%x, "
2073                         "LIC version 0x%x, "
2074                         "FC link speed %d Gb/s\n",
2075                         zfcp_get_busid_by_adapter(adapter),
2076                         (wwn_t) fc_host_node_name(shost),
2077                         (wwn_t) fc_host_port_name(shost),
2078                         fc_host_port_id(shost),
2079                         adapter->hydra_version,
2080                         adapter->fsf_lic_version,
2081                         fc_host_speed(shost));
2082         if (ZFCP_QTCB_VERSION < bottom->low_qtcb_version) {
2083                 ZFCP_LOG_NORMAL("error: the adapter %s "
2084                                 "only supports newer control block "
2085                                 "versions in comparison to this device "
2086                                 "driver (try updated device driver)\n",
2087                                 zfcp_get_busid_by_adapter(adapter));
2088                 debug_text_event(adapter->erp_dbf, 0, "low_qtcb_ver");
2089                 zfcp_erp_adapter_shutdown(adapter, 0);
2090                 return -EIO;
2091         }
2092         if (ZFCP_QTCB_VERSION > bottom->high_qtcb_version) {
2093                 ZFCP_LOG_NORMAL("error: the adapter %s "
2094                                 "only supports older control block "
2095                                 "versions than this device driver uses"
2096                                 "(consider a microcode upgrade)\n",
2097                                 zfcp_get_busid_by_adapter(adapter));
2098                 debug_text_event(adapter->erp_dbf, 0, "high_qtcb_ver");
2099                 zfcp_erp_adapter_shutdown(adapter, 0);
2100                 return -EIO;
2101         }
2102         return 0;
2103 }
2104
2105 /*
2106  * function:    zfcp_fsf_exchange_config_data_handler
2107  *
2108  * purpose:     is called for finished Exchange Configuration Data command
2109  *
2110  * returns:
2111  */
2112 static int
2113 zfcp_fsf_exchange_config_data_handler(struct zfcp_fsf_req *fsf_req)
2114 {
2115         struct fsf_qtcb_bottom_config *bottom;
2116         struct zfcp_adapter *adapter = fsf_req->adapter;
2117         struct fsf_qtcb *qtcb = fsf_req->qtcb;
2118
2119         if (fsf_req->status & ZFCP_STATUS_FSFREQ_ERROR)
2120                 return -EIO;
2121
2122         switch (qtcb->header.fsf_status) {
2123
2124         case FSF_GOOD:
2125                 if (zfcp_fsf_exchange_config_evaluate(fsf_req, 1))
2126                         return -EIO;
2127
2128                 switch (fc_host_port_type(adapter->scsi_host)) {
2129                 case FC_PORTTYPE_PTP:
2130                         ZFCP_LOG_NORMAL("Point-to-Point fibrechannel "
2131                                         "configuration detected at adapter %s\n"
2132                                         "Peer WWNN 0x%016llx, "
2133                                         "peer WWPN 0x%016llx, "
2134                                         "peer d_id 0x%06x\n",
2135                                         zfcp_get_busid_by_adapter(adapter),
2136                                         adapter->peer_wwnn,
2137                                         adapter->peer_wwpn,
2138                                         adapter->peer_d_id);
2139                         debug_text_event(fsf_req->adapter->erp_dbf, 0,
2140                                          "top-p-to-p");
2141                         break;
2142                 case FC_PORTTYPE_NLPORT:
2143                         ZFCP_LOG_NORMAL("error: Arbitrated loop fibrechannel "
2144                                         "topology detected at adapter %s "
2145                                         "unsupported, shutting down adapter\n",
2146                                         zfcp_get_busid_by_adapter(adapter));
2147                         debug_text_event(fsf_req->adapter->erp_dbf, 0,
2148                                          "top-al");
2149                         zfcp_erp_adapter_shutdown(adapter, 0);
2150                         return -EIO;
2151                 case FC_PORTTYPE_NPORT:
2152                         ZFCP_LOG_NORMAL("Switched fabric fibrechannel "
2153                                       "network detected at adapter %s.\n",
2154                                       zfcp_get_busid_by_adapter(adapter));
2155                         break;
2156                 default:
2157                         ZFCP_LOG_NORMAL("bug: The fibrechannel topology "
2158                                         "reported by the exchange "
2159                                         "configuration command for "
2160                                         "the adapter %s is not "
2161                                         "of a type known to the zfcp "
2162                                         "driver, shutting down adapter\n",
2163                                         zfcp_get_busid_by_adapter(adapter));
2164                         debug_text_exception(fsf_req->adapter->erp_dbf, 0,
2165                                              "unknown-topo");
2166                         zfcp_erp_adapter_shutdown(adapter, 0);
2167                         return -EIO;
2168                 }
2169                 bottom = &qtcb->bottom.config;
2170                 if (bottom->max_qtcb_size < sizeof(struct fsf_qtcb)) {
2171                         ZFCP_LOG_NORMAL("bug: Maximum QTCB size (%d bytes) "
2172                                         "allowed by the adapter %s "
2173                                         "is lower than the minimum "
2174                                         "required by the driver (%ld bytes).\n",
2175                                         bottom->max_qtcb_size,
2176                                         zfcp_get_busid_by_adapter(adapter),
2177                                         sizeof(struct fsf_qtcb));
2178                         debug_text_event(fsf_req->adapter->erp_dbf, 0,
2179                                          "qtcb-size");
2180                         debug_event(fsf_req->adapter->erp_dbf, 0,
2181                                     &bottom->max_qtcb_size, sizeof (u32));
2182                         zfcp_erp_adapter_shutdown(adapter, 0);
2183                         return -EIO;
2184                 }
2185                 atomic_set_mask(ZFCP_STATUS_ADAPTER_XCONFIG_OK,
2186                                 &adapter->status);
2187                 break;
2188         case FSF_EXCHANGE_CONFIG_DATA_INCOMPLETE:
2189                 debug_text_event(adapter->erp_dbf, 0, "xchg-inco");
2190
2191                 if (zfcp_fsf_exchange_config_evaluate(fsf_req, 0))
2192                         return -EIO;
2193
2194                 atomic_set_mask(ZFCP_STATUS_ADAPTER_XCONFIG_OK, &adapter->status);
2195
2196                 zfcp_fsf_link_down_info_eval(adapter,
2197                         &qtcb->header.fsf_status_qual.link_down_info);
2198                 break;
2199         default:
2200                 debug_text_event(fsf_req->adapter->erp_dbf, 0, "fsf-stat-ng");
2201                 debug_event(fsf_req->adapter->erp_dbf, 0,
2202                             &fsf_req->qtcb->header.fsf_status, sizeof (u32));
2203                 zfcp_erp_adapter_shutdown(adapter, 0);
2204                 return -EIO;
2205         }
2206         return 0;
2207 }
2208
2209 /**
2210  * zfcp_fsf_exchange_port_data - request information about local port
2211  * @erp_action: ERP action for the adapter for which port data is requested
2212  * @adapter: for which port data is requested
2213  * @data: response to exchange port data request
2214  */
2215 int
2216 zfcp_fsf_exchange_port_data(struct zfcp_erp_action *erp_action,
2217                             struct zfcp_adapter *adapter,
2218                             struct fsf_qtcb_bottom_port *data)
2219 {
2220         volatile struct qdio_buffer_element *sbale;
2221         struct zfcp_fsf_req *fsf_req;
2222         unsigned long lock_flags;
2223         int retval = 0;
2224
2225         if (!(adapter->adapter_features & FSF_FEATURE_HBAAPI_MANAGEMENT)) {
2226                 ZFCP_LOG_INFO("error: exchange port data "
2227                               "command not supported by adapter %s\n",
2228                               zfcp_get_busid_by_adapter(adapter));
2229                 return -EOPNOTSUPP;
2230         }
2231
2232         /* setup new FSF request */
2233         retval = zfcp_fsf_req_create(adapter, FSF_QTCB_EXCHANGE_PORT_DATA,
2234                                      erp_action ? ZFCP_REQ_AUTO_CLEANUP : 0,
2235                                      NULL, &lock_flags, &fsf_req);
2236         if (retval < 0) {
2237                 ZFCP_LOG_INFO("error: Out of resources. Could not create an "
2238                               "exchange port data request for"
2239                               "the adapter %s.\n",
2240                               zfcp_get_busid_by_adapter(adapter));
2241                 write_unlock_irqrestore(&adapter->request_queue.queue_lock,
2242                                         lock_flags);
2243                 return retval;
2244         }
2245
2246         if (data)
2247                 fsf_req->data = (unsigned long) data;
2248
2249         sbale = zfcp_qdio_sbale_req(fsf_req, fsf_req->sbal_curr, 0);
2250         sbale[0].flags |= SBAL_FLAGS0_TYPE_READ;
2251         sbale[1].flags |= SBAL_FLAGS_LAST_ENTRY;
2252
2253         if (erp_action) {
2254                 erp_action->fsf_req = fsf_req;
2255                 fsf_req->erp_action = erp_action;
2256                 zfcp_erp_start_timer(fsf_req);
2257         } else
2258                 zfcp_fsf_start_timer(fsf_req, ZFCP_FSF_REQUEST_TIMEOUT);
2259
2260         retval = zfcp_fsf_req_send(fsf_req);
2261         if (retval) {
2262                 ZFCP_LOG_INFO("error: Could not send an exchange port data "
2263                               "command on the adapter %s\n",
2264                               zfcp_get_busid_by_adapter(adapter));
2265                 zfcp_fsf_req_free(fsf_req);
2266                 if (erp_action)
2267                         erp_action->fsf_req = NULL;
2268                 write_unlock_irqrestore(&adapter->request_queue.queue_lock,
2269                                         lock_flags);
2270                 return retval;
2271         }
2272
2273         write_unlock_irqrestore(&adapter->request_queue.queue_lock, lock_flags);
2274
2275         if (!erp_action) {
2276                 wait_event(fsf_req->completion_wq,
2277                            fsf_req->status & ZFCP_STATUS_FSFREQ_COMPLETED);
2278                 zfcp_fsf_req_free(fsf_req);
2279         }
2280         return retval;
2281 }
2282
2283 /**
2284  * zfcp_fsf_exchange_port_evaluate
2285  * @fsf_req: fsf_req which belongs to xchg port data request
2286  * @xchg_ok: specifies if xchg port data was incomplete or complete (0/1)
2287  */
2288 static void
2289 zfcp_fsf_exchange_port_evaluate(struct zfcp_fsf_req *fsf_req, int xchg_ok)
2290 {
2291         struct zfcp_adapter *adapter;
2292         struct fsf_qtcb *qtcb;
2293         struct fsf_qtcb_bottom_port *bottom, *data;
2294         struct Scsi_Host *shost;
2295
2296         adapter = fsf_req->adapter;
2297         qtcb = fsf_req->qtcb;
2298         bottom = &qtcb->bottom.port;
2299         shost = adapter->scsi_host;
2300
2301         data = (struct fsf_qtcb_bottom_port*) fsf_req->data;
2302         if (data)
2303                 memcpy(data, bottom, sizeof(struct fsf_qtcb_bottom_port));
2304
2305         if (adapter->connection_features & FSF_FEATURE_NPIV_MODE)
2306                 fc_host_permanent_port_name(shost) = bottom->wwpn;
2307         else
2308                 fc_host_permanent_port_name(shost) = fc_host_port_name(shost);
2309         fc_host_maxframe_size(shost) = bottom->maximum_frame_size;
2310         fc_host_supported_speeds(shost) = bottom->supported_speed;
2311 }
2312
2313 /**
2314  * zfcp_fsf_exchange_port_data_handler - handler for exchange_port_data request
2315  * @fsf_req: pointer to struct zfcp_fsf_req
2316  */
2317 static void
2318 zfcp_fsf_exchange_port_data_handler(struct zfcp_fsf_req *fsf_req)
2319 {
2320         struct zfcp_adapter *adapter;
2321         struct fsf_qtcb *qtcb;
2322
2323         adapter = fsf_req->adapter;
2324         qtcb = fsf_req->qtcb;
2325
2326         if (fsf_req->status & ZFCP_STATUS_FSFREQ_ERROR)
2327                 return;
2328
2329         switch (qtcb->header.fsf_status) {
2330         case FSF_GOOD:
2331                 zfcp_fsf_exchange_port_evaluate(fsf_req, 1);
2332                 atomic_set_mask(ZFCP_STATUS_ADAPTER_XPORT_OK, &adapter->status);
2333                 break;
2334         case FSF_EXCHANGE_CONFIG_DATA_INCOMPLETE:
2335                 zfcp_fsf_exchange_port_evaluate(fsf_req, 0);
2336                 atomic_set_mask(ZFCP_STATUS_ADAPTER_XPORT_OK, &adapter->status);
2337                 zfcp_fsf_link_down_info_eval(adapter,
2338                         &qtcb->header.fsf_status_qual.link_down_info);
2339                 break;
2340         default:
2341                 debug_text_event(adapter->erp_dbf, 0, "xchg-port-ng");
2342                 debug_event(adapter->erp_dbf, 0,
2343                             &fsf_req->qtcb->header.fsf_status, sizeof(u32));
2344         }
2345 }
2346
2347
2348 /*
2349  * function:    zfcp_fsf_open_port
2350  *
2351  * purpose:     
2352  *
2353  * returns:     address of initiated FSF request
2354  *              NULL - request could not be initiated 
2355  */
2356 int
2357 zfcp_fsf_open_port(struct zfcp_erp_action *erp_action)
2358 {
2359         volatile struct qdio_buffer_element *sbale;
2360         struct zfcp_fsf_req *fsf_req;
2361         unsigned long lock_flags;
2362         int retval = 0;
2363
2364         /* setup new FSF request */
2365         retval = zfcp_fsf_req_create(erp_action->adapter,
2366                                      FSF_QTCB_OPEN_PORT_WITH_DID,
2367                                      ZFCP_WAIT_FOR_SBAL | ZFCP_REQ_AUTO_CLEANUP,
2368                                      erp_action->adapter->pool.fsf_req_erp,
2369                                      &lock_flags, &fsf_req);
2370         if (retval < 0) {
2371                 ZFCP_LOG_INFO("error: Could not create open port request "
2372                               "for port 0x%016Lx on adapter %s.\n",
2373                               erp_action->port->wwpn,
2374                               zfcp_get_busid_by_adapter(erp_action->adapter));
2375                 goto out;
2376         }
2377
2378         sbale = zfcp_qdio_sbale_req(fsf_req, fsf_req->sbal_curr, 0);
2379         sbale[0].flags |= SBAL_FLAGS0_TYPE_READ;
2380         sbale[1].flags |= SBAL_FLAGS_LAST_ENTRY;
2381
2382         fsf_req->qtcb->bottom.support.d_id = erp_action->port->d_id;
2383         atomic_set_mask(ZFCP_STATUS_COMMON_OPENING, &erp_action->port->status);
2384         fsf_req->data = (unsigned long) erp_action->port;
2385         fsf_req->erp_action = erp_action;
2386         erp_action->fsf_req = fsf_req;
2387
2388         zfcp_erp_start_timer(fsf_req);
2389         retval = zfcp_fsf_req_send(fsf_req);
2390         if (retval) {
2391                 ZFCP_LOG_INFO("error: Could not send open port request for "
2392                               "port 0x%016Lx on adapter %s.\n",
2393                               erp_action->port->wwpn,
2394                               zfcp_get_busid_by_adapter(erp_action->adapter));
2395                 zfcp_fsf_req_free(fsf_req);
2396                 erp_action->fsf_req = NULL;
2397                 goto out;
2398         }
2399
2400         ZFCP_LOG_DEBUG("open port request initiated "
2401                        "(adapter %s,  port 0x%016Lx)\n",
2402                        zfcp_get_busid_by_adapter(erp_action->adapter),
2403                        erp_action->port->wwpn);
2404  out:
2405         write_unlock_irqrestore(&erp_action->adapter->request_queue.queue_lock,
2406                                 lock_flags);
2407         return retval;
2408 }
2409
2410 /*
2411  * function:    zfcp_fsf_open_port_handler
2412  *
2413  * purpose:     is called for finished Open Port command
2414  *
2415  * returns:     
2416  */
2417 static int
2418 zfcp_fsf_open_port_handler(struct zfcp_fsf_req *fsf_req)
2419 {
2420         int retval = -EINVAL;
2421         struct zfcp_port *port;
2422         struct fsf_plogi *plogi;
2423         struct fsf_qtcb_header *header;
2424         u16 subtable, rule, counter;
2425
2426         port = (struct zfcp_port *) fsf_req->data;
2427         header = &fsf_req->qtcb->header;
2428
2429         if (fsf_req->status & ZFCP_STATUS_FSFREQ_ERROR) {
2430                 /* don't change port status in our bookkeeping */
2431                 goto skip_fsfstatus;
2432         }
2433
2434         /* evaluate FSF status in QTCB */
2435         switch (header->fsf_status) {
2436
2437         case FSF_PORT_ALREADY_OPEN:
2438                 ZFCP_LOG_NORMAL("bug: remote port 0x%016Lx on adapter %s "
2439                                 "is already open.\n",
2440                                 port->wwpn, zfcp_get_busid_by_port(port));
2441                 debug_text_exception(fsf_req->adapter->erp_dbf, 0,
2442                                      "fsf_s_popen");
2443                 /*
2444                  * This is a bug, however operation should continue normally
2445                  * if it is simply ignored
2446                  */
2447                 break;
2448
2449         case FSF_ACCESS_DENIED:
2450                 ZFCP_LOG_NORMAL("Access denied, cannot open port 0x%016Lx "
2451                                 "on adapter %s\n",
2452                                 port->wwpn, zfcp_get_busid_by_port(port));
2453                 for (counter = 0; counter < 2; counter++) {
2454                         subtable = header->fsf_status_qual.halfword[counter * 2];
2455                         rule = header->fsf_status_qual.halfword[counter * 2 + 1];
2456                         switch (subtable) {
2457                         case FSF_SQ_CFDC_SUBTABLE_OS:
2458                         case FSF_SQ_CFDC_SUBTABLE_PORT_WWPN:
2459                         case FSF_SQ_CFDC_SUBTABLE_PORT_DID:
2460                         case FSF_SQ_CFDC_SUBTABLE_LUN:
2461                                 ZFCP_LOG_INFO("Access denied (%s rule %d)\n",
2462                                         zfcp_act_subtable_type[subtable], rule);
2463                                 break;
2464                         }
2465                 }
2466                 debug_text_event(fsf_req->adapter->erp_dbf, 1, "fsf_s_access");
2467                 zfcp_erp_port_access_denied(port);
2468                 fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
2469                 break;
2470
2471         case FSF_MAXIMUM_NUMBER_OF_PORTS_EXCEEDED:
2472                 ZFCP_LOG_INFO("error: The FSF adapter is out of resources. "
2473                               "The remote port 0x%016Lx on adapter %s "
2474                               "could not be opened. Disabling it.\n",
2475                               port->wwpn, zfcp_get_busid_by_port(port));
2476                 debug_text_event(fsf_req->adapter->erp_dbf, 1,
2477                                  "fsf_s_max_ports");
2478                 zfcp_erp_port_failed(port);
2479                 fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
2480                 break;
2481
2482         case FSF_ADAPTER_STATUS_AVAILABLE:
2483                 switch (header->fsf_status_qual.word[0]) {
2484                 case FSF_SQ_INVOKE_LINK_TEST_PROCEDURE:
2485                         debug_text_event(fsf_req->adapter->erp_dbf, 1,
2486                                          "fsf_sq_ltest");
2487                         /* ERP strategy will escalate */
2488                         fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
2489                         break;
2490                 case FSF_SQ_ULP_DEPENDENT_ERP_REQUIRED:
2491                         /* ERP strategy will escalate */
2492                         debug_text_event(fsf_req->adapter->erp_dbf, 1,
2493                                          "fsf_sq_ulp");
2494                         fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
2495                         break;
2496                 case FSF_SQ_NO_RETRY_POSSIBLE:
2497                         ZFCP_LOG_NORMAL("The remote port 0x%016Lx on "
2498                                         "adapter %s could not be opened. "
2499                                         "Disabling it.\n",
2500                                         port->wwpn,
2501                                         zfcp_get_busid_by_port(port));
2502                         debug_text_exception(fsf_req->adapter->erp_dbf, 0,
2503                                              "fsf_sq_no_retry");
2504                         zfcp_erp_port_failed(port);
2505                         fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
2506                         break;
2507                 default:
2508                         ZFCP_LOG_NORMAL
2509                             ("bug: Wrong status qualifier 0x%x arrived.\n",
2510                              header->fsf_status_qual.word[0]);
2511                         debug_text_event(fsf_req->adapter->erp_dbf, 0,
2512                                          "fsf_sq_inval:");
2513                         debug_exception(
2514                                 fsf_req->adapter->erp_dbf, 0,
2515                                 &header->fsf_status_qual.word[0],
2516                                 sizeof (u32));
2517                         break;
2518                 }
2519                 break;
2520
2521         case FSF_GOOD:
2522                 /* save port handle assigned by FSF */
2523                 port->handle = header->port_handle;
2524                 ZFCP_LOG_INFO("The remote port 0x%016Lx via adapter %s "
2525                               "was opened, it's port handle is 0x%x\n",
2526                               port->wwpn, zfcp_get_busid_by_port(port),
2527                               port->handle);
2528                 /* mark port as open */
2529                 atomic_set_mask(ZFCP_STATUS_COMMON_OPEN |
2530                                 ZFCP_STATUS_PORT_PHYS_OPEN, &port->status);
2531                 atomic_clear_mask(ZFCP_STATUS_COMMON_ACCESS_DENIED |
2532                                   ZFCP_STATUS_COMMON_ACCESS_BOXED,
2533                                   &port->status);
2534                 retval = 0;
2535                 /* check whether D_ID has changed during open */
2536                 /*
2537                  * FIXME: This check is not airtight, as the FCP channel does
2538                  * not monitor closures of target port connections caused on
2539                  * the remote side. Thus, they might miss out on invalidating
2540                  * locally cached WWPNs (and other N_Port parameters) of gone
2541                  * target ports. So, our heroic attempt to make things safe
2542                  * could be undermined by 'open port' response data tagged with
2543                  * obsolete WWPNs. Another reason to monitor potential
2544                  * connection closures ourself at least (by interpreting
2545                  * incoming ELS' and unsolicited status). It just crosses my
2546                  * mind that one should be able to cross-check by means of
2547                  * another GID_PN straight after a port has been opened.
2548                  * Alternately, an ADISC/PDISC ELS should suffice, as well.
2549                  */
2550                 plogi = (struct fsf_plogi *) fsf_req->qtcb->bottom.support.els;
2551                 if (!atomic_test_mask(ZFCP_STATUS_PORT_NO_WWPN, &port->status))
2552                 {
2553                         if (fsf_req->qtcb->bottom.support.els1_length <
2554                             sizeof (struct fsf_plogi)) {
2555                                 ZFCP_LOG_INFO(
2556                                         "warning: insufficient length of "
2557                                         "PLOGI payload (%i)\n",
2558                                         fsf_req->qtcb->bottom.support.els1_length);
2559                                 debug_text_event(fsf_req->adapter->erp_dbf, 0,
2560                                                  "fsf_s_short_plogi:");
2561                                 /* skip sanity check and assume wwpn is ok */
2562                         } else {
2563                                 if (plogi->serv_param.wwpn != port->wwpn) {
2564                                         ZFCP_LOG_INFO("warning: d_id of port "
2565                                                       "0x%016Lx changed during "
2566                                                       "open\n", port->wwpn);
2567                                         debug_text_event(
2568                                                 fsf_req->adapter->erp_dbf, 0,
2569                                                 "fsf_s_did_change:");
2570                                         atomic_clear_mask(
2571                                                 ZFCP_STATUS_PORT_DID_DID,
2572                                                 &port->status);
2573                                 } else {
2574                                         port->wwnn = plogi->serv_param.wwnn;
2575                                         zfcp_plogi_evaluate(port, plogi);
2576                                 }
2577                         }
2578                 }
2579                 break;
2580
2581         case FSF_UNKNOWN_OP_SUBTYPE:
2582                 /* should never occure, subtype not set in zfcp_fsf_open_port */
2583                 ZFCP_LOG_INFO("unknown operation subtype (adapter: %s, "
2584                               "op_subtype=0x%x)\n",
2585                               zfcp_get_busid_by_port(port),
2586                               fsf_req->qtcb->bottom.support.operation_subtype);
2587                 fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
2588                 break;
2589
2590         default:
2591                 ZFCP_LOG_NORMAL("bug: An unknown FSF Status was presented "
2592                                 "(debug info 0x%x)\n",
2593                                 header->fsf_status);
2594                 debug_text_event(fsf_req->adapter->erp_dbf, 0, "fsf_s_inval:");
2595                 debug_exception(fsf_req->adapter->erp_dbf, 0,
2596                                 &header->fsf_status, sizeof (u32));
2597                 break;
2598         }
2599
2600  skip_fsfstatus:
2601         atomic_clear_mask(ZFCP_STATUS_COMMON_OPENING, &port->status);
2602         return retval;
2603 }
2604
2605 /*
2606  * function:    zfcp_fsf_close_port
2607  *
2608  * purpose:     submit FSF command "close port"
2609  *
2610  * returns:     address of initiated FSF request
2611  *              NULL - request could not be initiated
2612  */
2613 int
2614 zfcp_fsf_close_port(struct zfcp_erp_action *erp_action)
2615 {
2616         volatile struct qdio_buffer_element *sbale;
2617         struct zfcp_fsf_req *fsf_req;
2618         unsigned long lock_flags;
2619         int retval = 0;
2620
2621         /* setup new FSF request */
2622         retval = zfcp_fsf_req_create(erp_action->adapter,
2623                                      FSF_QTCB_CLOSE_PORT,
2624                                      ZFCP_WAIT_FOR_SBAL | ZFCP_REQ_AUTO_CLEANUP,
2625                                      erp_action->adapter->pool.fsf_req_erp,
2626                                      &lock_flags, &fsf_req);
2627         if (retval < 0) {
2628                 ZFCP_LOG_INFO("error: Could not create a close port request "
2629                               "for port 0x%016Lx on adapter %s.\n",
2630                               erp_action->port->wwpn,
2631                               zfcp_get_busid_by_adapter(erp_action->adapter));
2632                 goto out;
2633         }
2634
2635         sbale = zfcp_qdio_sbale_req(fsf_req, fsf_req->sbal_curr, 0);
2636         sbale[0].flags |= SBAL_FLAGS0_TYPE_READ;
2637         sbale[1].flags |= SBAL_FLAGS_LAST_ENTRY;
2638
2639         atomic_set_mask(ZFCP_STATUS_COMMON_CLOSING, &erp_action->port->status);
2640         fsf_req->data = (unsigned long) erp_action->port;
2641         fsf_req->erp_action = erp_action;
2642         fsf_req->qtcb->header.port_handle = erp_action->port->handle;
2643         fsf_req->erp_action = erp_action;
2644         erp_action->fsf_req = fsf_req;
2645
2646         zfcp_erp_start_timer(fsf_req);
2647         retval = zfcp_fsf_req_send(fsf_req);
2648         if (retval) {
2649                 ZFCP_LOG_INFO("error: Could not send a close port request for "
2650                               "port 0x%016Lx on adapter %s.\n",
2651                               erp_action->port->wwpn,
2652                               zfcp_get_busid_by_adapter(erp_action->adapter));
2653                 zfcp_fsf_req_free(fsf_req);
2654                 erp_action->fsf_req = NULL;
2655                 goto out;
2656         }
2657
2658         ZFCP_LOG_TRACE("close port request initiated "
2659                        "(adapter %s, port 0x%016Lx)\n",
2660                        zfcp_get_busid_by_adapter(erp_action->adapter),
2661                        erp_action->port->wwpn);
2662  out:
2663         write_unlock_irqrestore(&erp_action->adapter->request_queue.queue_lock,
2664                                 lock_flags);
2665         return retval;
2666 }
2667
2668 /*
2669  * function:    zfcp_fsf_close_port_handler
2670  *
2671  * purpose:     is called for finished Close Port FSF command
2672  *
2673  * returns:
2674  */
2675 static int
2676 zfcp_fsf_close_port_handler(struct zfcp_fsf_req *fsf_req)
2677 {
2678         int retval = -EINVAL;
2679         struct zfcp_port *port;
2680
2681         port = (struct zfcp_port *) fsf_req->data;
2682
2683         if (fsf_req->status & ZFCP_STATUS_FSFREQ_ERROR) {
2684                 /* don't change port status in our bookkeeping */
2685                 goto skip_fsfstatus;
2686         }
2687
2688         /* evaluate FSF status in QTCB */
2689         switch (fsf_req->qtcb->header.fsf_status) {
2690
2691         case FSF_PORT_HANDLE_NOT_VALID:
2692                 ZFCP_LOG_INFO("Temporary port identifier 0x%x for port "
2693                               "0x%016Lx on adapter %s invalid. This may happen "
2694                               "occasionally.\n", port->handle,
2695                               port->wwpn, zfcp_get_busid_by_port(port));
2696                 ZFCP_LOG_DEBUG("status qualifier:\n");
2697                 ZFCP_HEX_DUMP(ZFCP_LOG_LEVEL_DEBUG,
2698                               (char *) &fsf_req->qtcb->header.fsf_status_qual,
2699                               sizeof (union fsf_status_qual));
2700                 debug_text_event(fsf_req->adapter->erp_dbf, 1,
2701                                  "fsf_s_phand_nv");
2702                 zfcp_erp_adapter_reopen(port->adapter, 0);
2703                 fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
2704                 break;
2705
2706         case FSF_ADAPTER_STATUS_AVAILABLE:
2707                 /* Note: FSF has actually closed the port in this case.
2708                  * The status code is just daft. Fingers crossed for a change
2709                  */
2710                 retval = 0;
2711                 break;
2712
2713         case FSF_GOOD:
2714                 ZFCP_LOG_TRACE("remote port 0x016%Lx on adapter %s closed, "
2715                                "port handle 0x%x\n", port->wwpn,
2716                                zfcp_get_busid_by_port(port), port->handle);
2717                 zfcp_erp_modify_port_status(port,
2718                                             ZFCP_STATUS_COMMON_OPEN,
2719                                             ZFCP_CLEAR);
2720                 retval = 0;
2721                 break;
2722
2723         default:
2724                 ZFCP_LOG_NORMAL("bug: An unknown FSF Status was presented "
2725                                 "(debug info 0x%x)\n",
2726                                 fsf_req->qtcb->header.fsf_status);
2727                 debug_text_event(fsf_req->adapter->erp_dbf, 0, "fsf_s_inval:");
2728                 debug_exception(fsf_req->adapter->erp_dbf, 0,
2729                                 &fsf_req->qtcb->header.fsf_status,
2730                                 sizeof (u32));
2731                 break;
2732         }
2733
2734  skip_fsfstatus:
2735         atomic_clear_mask(ZFCP_STATUS_COMMON_CLOSING, &port->status);
2736         return retval;
2737 }
2738
2739 /*
2740  * function:    zfcp_fsf_close_physical_port
2741  *
2742  * purpose:     submit FSF command "close physical port"
2743  *
2744  * returns:     address of initiated FSF request
2745  *              NULL - request could not be initiated
2746  */
2747 int
2748 zfcp_fsf_close_physical_port(struct zfcp_erp_action *erp_action)
2749 {
2750         volatile struct qdio_buffer_element *sbale;
2751         struct zfcp_fsf_req *fsf_req;
2752         unsigned long lock_flags;
2753         int retval = 0;
2754
2755         /* setup new FSF request */
2756         retval = zfcp_fsf_req_create(erp_action->adapter,
2757                                      FSF_QTCB_CLOSE_PHYSICAL_PORT,
2758                                      ZFCP_WAIT_FOR_SBAL | ZFCP_REQ_AUTO_CLEANUP,
2759                                      erp_action->adapter->pool.fsf_req_erp,
2760                                      &lock_flags, &fsf_req);
2761         if (retval < 0) {
2762                 ZFCP_LOG_INFO("error: Could not create close physical port "
2763                               "request (adapter %s, port 0x%016Lx)\n",
2764                               zfcp_get_busid_by_adapter(erp_action->adapter),
2765                               erp_action->port->wwpn);
2766
2767                 goto out;
2768         }
2769
2770         sbale = zfcp_qdio_sbale_req(fsf_req, fsf_req->sbal_curr, 0);
2771         sbale[0].flags |= SBAL_FLAGS0_TYPE_READ;
2772         sbale[1].flags |= SBAL_FLAGS_LAST_ENTRY;
2773
2774         /* mark port as being closed */
2775         atomic_set_mask(ZFCP_STATUS_PORT_PHYS_CLOSING,
2776                         &erp_action->port->status);
2777         /* save a pointer to this port */
2778         fsf_req->data = (unsigned long) erp_action->port;
2779         fsf_req->qtcb->header.port_handle = erp_action->port->handle;
2780         fsf_req->erp_action = erp_action;
2781         erp_action->fsf_req = fsf_req;
2782
2783         zfcp_erp_start_timer(fsf_req);
2784         retval = zfcp_fsf_req_send(fsf_req);
2785         if (retval) {
2786                 ZFCP_LOG_INFO("error: Could not send close physical port "
2787                               "request (adapter %s, port 0x%016Lx)\n",
2788                               zfcp_get_busid_by_adapter(erp_action->adapter),
2789                               erp_action->port->wwpn);
2790                 zfcp_fsf_req_free(fsf_req);
2791                 erp_action->fsf_req = NULL;
2792                 goto out;
2793         }
2794
2795         ZFCP_LOG_TRACE("close physical port request initiated "
2796                        "(adapter %s, port 0x%016Lx)\n",
2797                        zfcp_get_busid_by_adapter(erp_action->adapter),
2798                        erp_action->port->wwpn);
2799  out:
2800         write_unlock_irqrestore(&erp_action->adapter->request_queue.queue_lock,
2801                                 lock_flags);
2802         return retval;
2803 }
2804
2805 /*
2806  * function:    zfcp_fsf_close_physical_port_handler
2807  *
2808  * purpose:     is called for finished Close Physical Port FSF command
2809  *
2810  * returns:
2811  */
2812 static int
2813 zfcp_fsf_close_physical_port_handler(struct zfcp_fsf_req *fsf_req)
2814 {
2815         int retval = -EINVAL;
2816         struct zfcp_port *port;
2817         struct zfcp_unit *unit;
2818         struct fsf_qtcb_header *header;
2819         u16 subtable, rule, counter;
2820
2821         port = (struct zfcp_port *) fsf_req->data;
2822         header = &fsf_req->qtcb->header;
2823
2824         if (fsf_req->status & ZFCP_STATUS_FSFREQ_ERROR) {
2825                 /* don't change port status in our bookkeeping */
2826                 goto skip_fsfstatus;
2827         }
2828
2829         /* evaluate FSF status in QTCB */
2830         switch (header->fsf_status) {
2831
2832         case FSF_PORT_HANDLE_NOT_VALID:
2833                 ZFCP_LOG_INFO("Temporary port identifier 0x%x invalid"
2834                               "(adapter %s, port 0x%016Lx). "
2835                               "This may happen occasionally.\n",
2836                               port->handle,
2837                               zfcp_get_busid_by_port(port),
2838                               port->wwpn);
2839                 ZFCP_LOG_DEBUG("status qualifier:\n");
2840                 ZFCP_HEX_DUMP(ZFCP_LOG_LEVEL_DEBUG,
2841                               (char *) &header->fsf_status_qual,
2842                               sizeof (union fsf_status_qual));
2843                 debug_text_event(fsf_req->adapter->erp_dbf, 1,
2844                                  "fsf_s_phand_nv");
2845                 zfcp_erp_adapter_reopen(port->adapter, 0);
2846                 fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
2847                 break;
2848
2849         case FSF_ACCESS_DENIED:
2850                 ZFCP_LOG_NORMAL("Access denied, cannot close "
2851                                 "physical port 0x%016Lx on adapter %s\n",
2852                                 port->wwpn, zfcp_get_busid_by_port(port));
2853                 for (counter = 0; counter < 2; counter++) {
2854                         subtable = header->fsf_status_qual.halfword[counter * 2];
2855                         rule = header->fsf_status_qual.halfword[counter * 2 + 1];
2856                         switch (subtable) {
2857                         case FSF_SQ_CFDC_SUBTABLE_OS:
2858                         case FSF_SQ_CFDC_SUBTABLE_PORT_WWPN:
2859                         case FSF_SQ_CFDC_SUBTABLE_PORT_DID:
2860                         case FSF_SQ_CFDC_SUBTABLE_LUN:
2861                                 ZFCP_LOG_INFO("Access denied (%s rule %d)\n",
2862                                         zfcp_act_subtable_type[subtable], rule);
2863                                 break;
2864                         }
2865                 }
2866                 debug_text_event(fsf_req->adapter->erp_dbf, 1, "fsf_s_access");
2867                 zfcp_erp_port_access_denied(port);
2868                 fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
2869                 break;
2870
2871         case FSF_PORT_BOXED:
2872                 ZFCP_LOG_DEBUG("The remote port 0x%016Lx on adapter "
2873                                "%s needs to be reopened but it was attempted "
2874                                "to close it physically.\n",
2875                                port->wwpn,
2876                                zfcp_get_busid_by_port(port));
2877                 debug_text_event(fsf_req->adapter->erp_dbf, 1, "fsf_s_pboxed");
2878                 zfcp_erp_port_boxed(port);
2879                 fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR |
2880                         ZFCP_STATUS_FSFREQ_RETRY;
2881                 break;
2882
2883         case FSF_ADAPTER_STATUS_AVAILABLE:
2884                 switch (header->fsf_status_qual.word[0]) {
2885                 case FSF_SQ_INVOKE_LINK_TEST_PROCEDURE:
2886                         debug_text_event(fsf_req->adapter->erp_dbf, 1,
2887                                          "fsf_sq_ltest");
2888                         /* This will now be escalated by ERP */
2889                         fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
2890                         break;
2891                 case FSF_SQ_ULP_DEPENDENT_ERP_REQUIRED:
2892                         /* ERP strategy will escalate */
2893                         debug_text_event(fsf_req->adapter->erp_dbf, 1,
2894                                          "fsf_sq_ulp");
2895                         fsf_req->status |= ZFCP_STATUS_FSFREQ_ERROR;
2896                         break;
2897                 default:
2898                         ZFCP_LOG_NORMAL
2899                             ("bug: Wrong status qualifier 0x%x arrived.\n",
2900                              header->fsf_status_qual.word[0]);
2901                         debug_text_event(fsf_req->adapter->erp_dbf, 0,
2902                                          "fsf_sq_inval:");
2903                         debug_exception(
2904                                 fsf_req->adapter->erp_dbf, 0,
2905                                 &header->fsf_status_qual.word[0], sizeof (u32));
2906                         break;
2907                 }
2908                 break;
2909
2910         case FSF_GOOD:
2911                 ZFCP_LOG_DEBUG("Remote port 0x%016Lx via adapter %s "
2912                                "physically closed, port handle 0x%x\n",
2913                                port->wwpn,
2914                                zfcp_get_busid_by_port(port), port->handle);
2915                 /* can't use generic zfcp_erp_modify_port_status because
2916                  * ZFCP_STATUS_COMMON_OPEN must not be reset for the port
2917                  */
2918                 atomic_clear_mask(ZFCP_STATUS_PORT_PHYS_OPEN, &port->status);
2919                 list_for_each_entry(unit, &port->unit_list_head, list)
2920                     atomic_clear_mask(ZFCP_STATUS_COMMON_OPEN, &unit->status);
2921                 retval = 0;
2922                 break;
2923
2924         default:
2925                 ZFCP_LOG_NORMAL("bug: An unknown FSF Status was presented "
2926                                 "(debug info 0x%x)\n",
2927                                 header->fsf_status);
2928                 debug_text_event(fsf_req->adapter->erp_dbf, 0, "fsf_s_inval:");
2929                 debug_exception(fsf_req->adapter->erp_dbf, 0,
2930                                 &header->fsf_status, sizeof (u32));
2931                 break;
2932         }
2933
2934  skip_fsfstatus:
2935         atomic_clear_mask(ZFCP_STATUS_PORT_PHYS_CLOSING, &port->status);
2936         return retval;
2937 }
2938
2939 /*
2940  * function:    zfcp_fsf_open_unit
2941  *
2942  * purpose:
2943  *
2944  * returns:
2945  *
2946  * assumptions: This routine does not check whether the associated
2947  *              remote port has already been opened. This should be
2948  *              done by calling routines. Otherwise some status
2949  *              may be presented by FSF
2950  */
2951 int
2952 zfcp_fsf_open_unit(struct zfcp_erp_action *erp_action)
2953 {
2954         volatile struct qdio_buffer_element *sbale;
2955         struct zfcp_fsf_req *fsf_req;
2956         unsigned long lock_flags;
2957         int retval = 0;