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