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