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