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