Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * zfcp device driver
0004  *
0005  * Implementation of FSF commands.
0006  *
0007  * Copyright IBM Corp. 2002, 2020
0008  */
0009 
0010 #define KMSG_COMPONENT "zfcp"
0011 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
0012 
0013 #include <linux/blktrace_api.h>
0014 #include <linux/jiffies.h>
0015 #include <linux/types.h>
0016 #include <linux/slab.h>
0017 #include <scsi/fc/fc_els.h>
0018 #include "zfcp_ext.h"
0019 #include "zfcp_fc.h"
0020 #include "zfcp_dbf.h"
0021 #include "zfcp_qdio.h"
0022 #include "zfcp_reqlist.h"
0023 #include "zfcp_diag.h"
0024 
0025 /* timeout for FSF requests sent during scsi_eh: abort or FCP TMF */
0026 #define ZFCP_FSF_SCSI_ER_TIMEOUT (10*HZ)
0027 /* timeout for: exchange config/port data outside ERP, or open/close WKA port */
0028 #define ZFCP_FSF_REQUEST_TIMEOUT (60*HZ)
0029 
0030 struct kmem_cache *zfcp_fsf_qtcb_cache;
0031 
0032 static bool ber_stop = true;
0033 module_param(ber_stop, bool, 0600);
0034 MODULE_PARM_DESC(ber_stop,
0035          "Shuts down FCP devices for FCP channels that report a bit-error count in excess of its threshold (default on)");
0036 
0037 static void zfcp_fsf_request_timeout_handler(struct timer_list *t)
0038 {
0039     struct zfcp_fsf_req *fsf_req = from_timer(fsf_req, t, timer);
0040     struct zfcp_adapter *adapter = fsf_req->adapter;
0041 
0042     zfcp_qdio_siosl(adapter);
0043     zfcp_erp_adapter_reopen(adapter, ZFCP_STATUS_COMMON_ERP_FAILED,
0044                 "fsrth_1");
0045 }
0046 
0047 static void zfcp_fsf_start_timer(struct zfcp_fsf_req *fsf_req,
0048                  unsigned long timeout)
0049 {
0050     fsf_req->timer.function = zfcp_fsf_request_timeout_handler;
0051     fsf_req->timer.expires = jiffies + timeout;
0052     add_timer(&fsf_req->timer);
0053 }
0054 
0055 static void zfcp_fsf_start_erp_timer(struct zfcp_fsf_req *fsf_req)
0056 {
0057     BUG_ON(!fsf_req->erp_action);
0058     fsf_req->timer.function = zfcp_erp_timeout_handler;
0059     fsf_req->timer.expires = jiffies + 30 * HZ;
0060     add_timer(&fsf_req->timer);
0061 }
0062 
0063 /* association between FSF command and FSF QTCB type */
0064 static u32 fsf_qtcb_type[] = {
0065     [FSF_QTCB_FCP_CMND] =             FSF_IO_COMMAND,
0066     [FSF_QTCB_ABORT_FCP_CMND] =       FSF_SUPPORT_COMMAND,
0067     [FSF_QTCB_OPEN_PORT_WITH_DID] =   FSF_SUPPORT_COMMAND,
0068     [FSF_QTCB_OPEN_LUN] =             FSF_SUPPORT_COMMAND,
0069     [FSF_QTCB_CLOSE_LUN] =            FSF_SUPPORT_COMMAND,
0070     [FSF_QTCB_CLOSE_PORT] =           FSF_SUPPORT_COMMAND,
0071     [FSF_QTCB_CLOSE_PHYSICAL_PORT] =  FSF_SUPPORT_COMMAND,
0072     [FSF_QTCB_SEND_ELS] =             FSF_SUPPORT_COMMAND,
0073     [FSF_QTCB_SEND_GENERIC] =         FSF_SUPPORT_COMMAND,
0074     [FSF_QTCB_EXCHANGE_CONFIG_DATA] = FSF_CONFIG_COMMAND,
0075     [FSF_QTCB_EXCHANGE_PORT_DATA] =   FSF_PORT_COMMAND,
0076     [FSF_QTCB_DOWNLOAD_CONTROL_FILE] = FSF_SUPPORT_COMMAND,
0077     [FSF_QTCB_UPLOAD_CONTROL_FILE] =  FSF_SUPPORT_COMMAND
0078 };
0079 
0080 static void zfcp_fsf_class_not_supp(struct zfcp_fsf_req *req)
0081 {
0082     dev_err(&req->adapter->ccw_device->dev, "FCP device not "
0083         "operational because of an unsupported FC class\n");
0084     zfcp_erp_adapter_shutdown(req->adapter, 0, "fscns_1");
0085     req->status |= ZFCP_STATUS_FSFREQ_ERROR;
0086 }
0087 
0088 /**
0089  * zfcp_fsf_req_free - free memory used by fsf request
0090  * @req: pointer to struct zfcp_fsf_req
0091  */
0092 void zfcp_fsf_req_free(struct zfcp_fsf_req *req)
0093 {
0094     if (likely(req->pool)) {
0095         if (likely(!zfcp_fsf_req_is_status_read_buffer(req)))
0096             mempool_free(req->qtcb, req->adapter->pool.qtcb_pool);
0097         mempool_free(req, req->pool);
0098         return;
0099     }
0100 
0101     if (likely(!zfcp_fsf_req_is_status_read_buffer(req)))
0102         kmem_cache_free(zfcp_fsf_qtcb_cache, req->qtcb);
0103     kfree(req);
0104 }
0105 
0106 static void zfcp_fsf_status_read_port_closed(struct zfcp_fsf_req *req)
0107 {
0108     unsigned long flags;
0109     struct fsf_status_read_buffer *sr_buf = req->data;
0110     struct zfcp_adapter *adapter = req->adapter;
0111     struct zfcp_port *port;
0112     int d_id = ntoh24(sr_buf->d_id);
0113 
0114     read_lock_irqsave(&adapter->port_list_lock, flags);
0115     list_for_each_entry(port, &adapter->port_list, list)
0116         if (port->d_id == d_id) {
0117             zfcp_erp_port_reopen(port, 0, "fssrpc1");
0118             break;
0119         }
0120     read_unlock_irqrestore(&adapter->port_list_lock, flags);
0121 }
0122 
0123 void zfcp_fsf_fc_host_link_down(struct zfcp_adapter *adapter)
0124 {
0125     struct Scsi_Host *shost = adapter->scsi_host;
0126 
0127     adapter->hydra_version = 0;
0128     adapter->peer_wwpn = 0;
0129     adapter->peer_wwnn = 0;
0130     adapter->peer_d_id = 0;
0131 
0132     /* if there is no shost yet, we have nothing to zero-out */
0133     if (shost == NULL)
0134         return;
0135 
0136     fc_host_port_id(shost) = 0;
0137     fc_host_fabric_name(shost) = 0;
0138     fc_host_speed(shost) = FC_PORTSPEED_UNKNOWN;
0139     fc_host_port_type(shost) = FC_PORTTYPE_UNKNOWN;
0140     snprintf(fc_host_model(shost), FC_SYMBOLIC_NAME_SIZE, "0x%04x", 0);
0141     memset(fc_host_active_fc4s(shost), 0, FC_FC4_LIST_SIZE);
0142 }
0143 
0144 static void zfcp_fsf_link_down_info_eval(struct zfcp_fsf_req *req,
0145                      struct fsf_link_down_info *link_down)
0146 {
0147     struct zfcp_adapter *adapter = req->adapter;
0148 
0149     if (atomic_read(&adapter->status) & ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED)
0150         return;
0151 
0152     atomic_or(ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED, &adapter->status);
0153 
0154     zfcp_scsi_schedule_rports_block(adapter);
0155 
0156     zfcp_fsf_fc_host_link_down(adapter);
0157 
0158     if (!link_down)
0159         goto out;
0160 
0161     switch (link_down->error_code) {
0162     case FSF_PSQ_LINK_NO_LIGHT:
0163         dev_warn(&req->adapter->ccw_device->dev,
0164              "There is no light signal from the local "
0165              "fibre channel cable\n");
0166         break;
0167     case FSF_PSQ_LINK_WRAP_PLUG:
0168         dev_warn(&req->adapter->ccw_device->dev,
0169              "There is a wrap plug instead of a fibre "
0170              "channel cable\n");
0171         break;
0172     case FSF_PSQ_LINK_NO_FCP:
0173         dev_warn(&req->adapter->ccw_device->dev,
0174              "The adjacent fibre channel node does not "
0175              "support FCP\n");
0176         break;
0177     case FSF_PSQ_LINK_FIRMWARE_UPDATE:
0178         dev_warn(&req->adapter->ccw_device->dev,
0179              "The FCP device is suspended because of a "
0180              "firmware update\n");
0181         break;
0182     case FSF_PSQ_LINK_INVALID_WWPN:
0183         dev_warn(&req->adapter->ccw_device->dev,
0184              "The FCP device detected a WWPN that is "
0185              "duplicate or not valid\n");
0186         break;
0187     case FSF_PSQ_LINK_NO_NPIV_SUPPORT:
0188         dev_warn(&req->adapter->ccw_device->dev,
0189              "The fibre channel fabric does not support NPIV\n");
0190         break;
0191     case FSF_PSQ_LINK_NO_FCP_RESOURCES:
0192         dev_warn(&req->adapter->ccw_device->dev,
0193              "The FCP adapter cannot support more NPIV ports\n");
0194         break;
0195     case FSF_PSQ_LINK_NO_FABRIC_RESOURCES:
0196         dev_warn(&req->adapter->ccw_device->dev,
0197              "The adjacent switch cannot support "
0198              "more NPIV ports\n");
0199         break;
0200     case FSF_PSQ_LINK_FABRIC_LOGIN_UNABLE:
0201         dev_warn(&req->adapter->ccw_device->dev,
0202              "The FCP adapter could not log in to the "
0203              "fibre channel fabric\n");
0204         break;
0205     case FSF_PSQ_LINK_WWPN_ASSIGNMENT_CORRUPTED:
0206         dev_warn(&req->adapter->ccw_device->dev,
0207              "The WWPN assignment file on the FCP adapter "
0208              "has been damaged\n");
0209         break;
0210     case FSF_PSQ_LINK_MODE_TABLE_CURRUPTED:
0211         dev_warn(&req->adapter->ccw_device->dev,
0212              "The mode table on the FCP adapter "
0213              "has been damaged\n");
0214         break;
0215     case FSF_PSQ_LINK_NO_WWPN_ASSIGNMENT:
0216         dev_warn(&req->adapter->ccw_device->dev,
0217              "All NPIV ports on the FCP adapter have "
0218              "been assigned\n");
0219         break;
0220     default:
0221         dev_warn(&req->adapter->ccw_device->dev,
0222              "The link between the FCP adapter and "
0223              "the FC fabric is down\n");
0224     }
0225 out:
0226     zfcp_erp_set_adapter_status(adapter, ZFCP_STATUS_COMMON_ERP_FAILED);
0227 }
0228 
0229 static void zfcp_fsf_status_read_link_down(struct zfcp_fsf_req *req)
0230 {
0231     struct fsf_status_read_buffer *sr_buf = req->data;
0232     struct fsf_link_down_info *ldi =
0233         (struct fsf_link_down_info *) &sr_buf->payload;
0234 
0235     switch (sr_buf->status_subtype) {
0236     case FSF_STATUS_READ_SUB_NO_PHYSICAL_LINK:
0237     case FSF_STATUS_READ_SUB_FDISC_FAILED:
0238         zfcp_fsf_link_down_info_eval(req, ldi);
0239         break;
0240     case FSF_STATUS_READ_SUB_FIRMWARE_UPDATE:
0241         zfcp_fsf_link_down_info_eval(req, NULL);
0242     }
0243 }
0244 
0245 static void
0246 zfcp_fsf_status_read_version_change(struct zfcp_adapter *adapter,
0247                     struct fsf_status_read_buffer *sr_buf)
0248 {
0249     if (sr_buf->status_subtype == FSF_STATUS_READ_SUB_LIC_CHANGE) {
0250         u32 version = sr_buf->payload.version_change.current_version;
0251 
0252         WRITE_ONCE(adapter->fsf_lic_version, version);
0253         snprintf(fc_host_firmware_version(adapter->scsi_host),
0254              FC_VERSION_STRING_SIZE, "%#08x", version);
0255     }
0256 }
0257 
0258 static void zfcp_fsf_status_read_handler(struct zfcp_fsf_req *req)
0259 {
0260     struct zfcp_adapter *adapter = req->adapter;
0261     struct fsf_status_read_buffer *sr_buf = req->data;
0262 
0263     if (req->status & ZFCP_STATUS_FSFREQ_DISMISSED) {
0264         zfcp_dbf_hba_fsf_uss("fssrh_1", req);
0265         mempool_free(virt_to_page(sr_buf), adapter->pool.sr_data);
0266         zfcp_fsf_req_free(req);
0267         return;
0268     }
0269 
0270     zfcp_dbf_hba_fsf_uss("fssrh_4", req);
0271 
0272     switch (sr_buf->status_type) {
0273     case FSF_STATUS_READ_PORT_CLOSED:
0274         zfcp_fsf_status_read_port_closed(req);
0275         break;
0276     case FSF_STATUS_READ_INCOMING_ELS:
0277         zfcp_fc_incoming_els(req);
0278         break;
0279     case FSF_STATUS_READ_SENSE_DATA_AVAIL:
0280         break;
0281     case FSF_STATUS_READ_BIT_ERROR_THRESHOLD:
0282         zfcp_dbf_hba_bit_err("fssrh_3", req);
0283         if (ber_stop) {
0284             dev_warn(&adapter->ccw_device->dev,
0285                  "All paths over this FCP device are disused because of excessive bit errors\n");
0286             zfcp_erp_adapter_shutdown(adapter, 0, "fssrh_b");
0287         } else {
0288             dev_warn(&adapter->ccw_device->dev,
0289                  "The error threshold for checksum statistics has been exceeded\n");
0290         }
0291         break;
0292     case FSF_STATUS_READ_LINK_DOWN:
0293         zfcp_fsf_status_read_link_down(req);
0294         zfcp_fc_enqueue_event(adapter, FCH_EVT_LINKDOWN, 0);
0295         break;
0296     case FSF_STATUS_READ_LINK_UP:
0297         dev_info(&adapter->ccw_device->dev,
0298              "The local link has been restored\n");
0299         /* All ports should be marked as ready to run again */
0300         zfcp_erp_set_adapter_status(adapter,
0301                         ZFCP_STATUS_COMMON_RUNNING);
0302         zfcp_erp_adapter_reopen(adapter,
0303                     ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED |
0304                     ZFCP_STATUS_COMMON_ERP_FAILED,
0305                     "fssrh_2");
0306         zfcp_fc_enqueue_event(adapter, FCH_EVT_LINKUP, 0);
0307 
0308         break;
0309     case FSF_STATUS_READ_NOTIFICATION_LOST:
0310         if (sr_buf->status_subtype & FSF_STATUS_READ_SUB_INCOMING_ELS)
0311             zfcp_fc_conditional_port_scan(adapter);
0312         if (sr_buf->status_subtype & FSF_STATUS_READ_SUB_VERSION_CHANGE)
0313             queue_work(adapter->work_queue,
0314                    &adapter->version_change_lost_work);
0315         break;
0316     case FSF_STATUS_READ_FEATURE_UPDATE_ALERT:
0317         adapter->adapter_features = sr_buf->payload.word[0];
0318         break;
0319     case FSF_STATUS_READ_VERSION_CHANGE:
0320         zfcp_fsf_status_read_version_change(adapter, sr_buf);
0321         break;
0322     }
0323 
0324     mempool_free(virt_to_page(sr_buf), adapter->pool.sr_data);
0325     zfcp_fsf_req_free(req);
0326 
0327     atomic_inc(&adapter->stat_miss);
0328     queue_work(adapter->work_queue, &adapter->stat_work);
0329 }
0330 
0331 static void zfcp_fsf_fsfstatus_qual_eval(struct zfcp_fsf_req *req)
0332 {
0333     switch (req->qtcb->header.fsf_status_qual.word[0]) {
0334     case FSF_SQ_FCP_RSP_AVAILABLE:
0335     case FSF_SQ_INVOKE_LINK_TEST_PROCEDURE:
0336     case FSF_SQ_NO_RETRY_POSSIBLE:
0337     case FSF_SQ_ULP_DEPENDENT_ERP_REQUIRED:
0338         return;
0339     case FSF_SQ_COMMAND_ABORTED:
0340         break;
0341     case FSF_SQ_NO_RECOM:
0342         dev_err(&req->adapter->ccw_device->dev,
0343             "The FCP adapter reported a problem "
0344             "that cannot be recovered\n");
0345         zfcp_qdio_siosl(req->adapter);
0346         zfcp_erp_adapter_shutdown(req->adapter, 0, "fsfsqe1");
0347         break;
0348     }
0349     /* all non-return stats set FSFREQ_ERROR*/
0350     req->status |= ZFCP_STATUS_FSFREQ_ERROR;
0351 }
0352 
0353 static void zfcp_fsf_fsfstatus_eval(struct zfcp_fsf_req *req)
0354 {
0355     if (unlikely(req->status & ZFCP_STATUS_FSFREQ_ERROR))
0356         return;
0357 
0358     switch (req->qtcb->header.fsf_status) {
0359     case FSF_UNKNOWN_COMMAND:
0360         dev_err(&req->adapter->ccw_device->dev,
0361             "The FCP adapter does not recognize the command 0x%x\n",
0362             req->qtcb->header.fsf_command);
0363         zfcp_erp_adapter_shutdown(req->adapter, 0, "fsfse_1");
0364         req->status |= ZFCP_STATUS_FSFREQ_ERROR;
0365         break;
0366     case FSF_ADAPTER_STATUS_AVAILABLE:
0367         zfcp_fsf_fsfstatus_qual_eval(req);
0368         break;
0369     }
0370 }
0371 
0372 static void zfcp_fsf_protstatus_eval(struct zfcp_fsf_req *req)
0373 {
0374     struct zfcp_adapter *adapter = req->adapter;
0375     struct fsf_qtcb *qtcb = req->qtcb;
0376     union fsf_prot_status_qual *psq = &qtcb->prefix.prot_status_qual;
0377 
0378     zfcp_dbf_hba_fsf_response(req);
0379 
0380     if (req->status & ZFCP_STATUS_FSFREQ_DISMISSED) {
0381         req->status |= ZFCP_STATUS_FSFREQ_ERROR;
0382         return;
0383     }
0384 
0385     switch (qtcb->prefix.prot_status) {
0386     case FSF_PROT_GOOD:
0387     case FSF_PROT_FSF_STATUS_PRESENTED:
0388         return;
0389     case FSF_PROT_QTCB_VERSION_ERROR:
0390         dev_err(&adapter->ccw_device->dev,
0391             "QTCB version 0x%x not supported by FCP adapter "
0392             "(0x%x to 0x%x)\n", FSF_QTCB_CURRENT_VERSION,
0393             psq->word[0], psq->word[1]);
0394         zfcp_erp_adapter_shutdown(adapter, 0, "fspse_1");
0395         break;
0396     case FSF_PROT_ERROR_STATE:
0397     case FSF_PROT_SEQ_NUMB_ERROR:
0398         zfcp_erp_adapter_reopen(adapter, 0, "fspse_2");
0399         req->status |= ZFCP_STATUS_FSFREQ_ERROR;
0400         break;
0401     case FSF_PROT_UNSUPP_QTCB_TYPE:
0402         dev_err(&adapter->ccw_device->dev,
0403             "The QTCB type is not supported by the FCP adapter\n");
0404         zfcp_erp_adapter_shutdown(adapter, 0, "fspse_3");
0405         break;
0406     case FSF_PROT_HOST_CONNECTION_INITIALIZING:
0407         atomic_or(ZFCP_STATUS_ADAPTER_HOST_CON_INIT,
0408                 &adapter->status);
0409         break;
0410     case FSF_PROT_DUPLICATE_REQUEST_ID:
0411         dev_err(&adapter->ccw_device->dev,
0412             "0x%Lx is an ambiguous request identifier\n",
0413             (unsigned long long)qtcb->bottom.support.req_handle);
0414         zfcp_erp_adapter_shutdown(adapter, 0, "fspse_4");
0415         break;
0416     case FSF_PROT_LINK_DOWN:
0417         zfcp_fsf_link_down_info_eval(req, &psq->link_down_info);
0418         /* go through reopen to flush pending requests */
0419         zfcp_erp_adapter_reopen(adapter, 0, "fspse_6");
0420         break;
0421     case FSF_PROT_REEST_QUEUE:
0422         /* All ports should be marked as ready to run again */
0423         zfcp_erp_set_adapter_status(adapter,
0424                         ZFCP_STATUS_COMMON_RUNNING);
0425         zfcp_erp_adapter_reopen(adapter,
0426                     ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED |
0427                     ZFCP_STATUS_COMMON_ERP_FAILED,
0428                     "fspse_8");
0429         break;
0430     default:
0431         dev_err(&adapter->ccw_device->dev,
0432             "0x%x is not a valid transfer protocol status\n",
0433             qtcb->prefix.prot_status);
0434         zfcp_qdio_siosl(adapter);
0435         zfcp_erp_adapter_shutdown(adapter, 0, "fspse_9");
0436     }
0437     req->status |= ZFCP_STATUS_FSFREQ_ERROR;
0438 }
0439 
0440 /**
0441  * zfcp_fsf_req_complete - process completion of a FSF request
0442  * @req: The FSF request that has been completed.
0443  *
0444  * When a request has been completed either from the FCP adapter,
0445  * or it has been dismissed due to a queue shutdown, this function
0446  * is called to process the completion status and trigger further
0447  * events related to the FSF request.
0448  * Caller must ensure that the request has been removed from
0449  * adapter->req_list, to protect against concurrent modification
0450  * by zfcp_erp_strategy_check_fsfreq().
0451  */
0452 static void zfcp_fsf_req_complete(struct zfcp_fsf_req *req)
0453 {
0454     struct zfcp_erp_action *erp_action;
0455 
0456     if (unlikely(zfcp_fsf_req_is_status_read_buffer(req))) {
0457         zfcp_fsf_status_read_handler(req);
0458         return;
0459     }
0460 
0461     del_timer_sync(&req->timer);
0462     zfcp_fsf_protstatus_eval(req);
0463     zfcp_fsf_fsfstatus_eval(req);
0464     req->handler(req);
0465 
0466     erp_action = req->erp_action;
0467     if (erp_action)
0468         zfcp_erp_notify(erp_action, 0);
0469 
0470     if (likely(req->status & ZFCP_STATUS_FSFREQ_CLEANUP))
0471         zfcp_fsf_req_free(req);
0472     else
0473         complete(&req->completion);
0474 }
0475 
0476 /**
0477  * zfcp_fsf_req_dismiss_all - dismiss all fsf requests
0478  * @adapter: pointer to struct zfcp_adapter
0479  *
0480  * Never ever call this without shutting down the adapter first.
0481  * Otherwise the adapter would continue using and corrupting s390 storage.
0482  * Included BUG_ON() call to ensure this is done.
0483  * ERP is supposed to be the only user of this function.
0484  */
0485 void zfcp_fsf_req_dismiss_all(struct zfcp_adapter *adapter)
0486 {
0487     struct zfcp_fsf_req *req, *tmp;
0488     LIST_HEAD(remove_queue);
0489 
0490     BUG_ON(atomic_read(&adapter->status) & ZFCP_STATUS_ADAPTER_QDIOUP);
0491     zfcp_reqlist_move(adapter->req_list, &remove_queue);
0492 
0493     list_for_each_entry_safe(req, tmp, &remove_queue, list) {
0494         list_del(&req->list);
0495         req->status |= ZFCP_STATUS_FSFREQ_DISMISSED;
0496         zfcp_fsf_req_complete(req);
0497     }
0498 }
0499 
0500 #define ZFCP_FSF_PORTSPEED_1GBIT    (1 <<  0)
0501 #define ZFCP_FSF_PORTSPEED_2GBIT    (1 <<  1)
0502 #define ZFCP_FSF_PORTSPEED_4GBIT    (1 <<  2)
0503 #define ZFCP_FSF_PORTSPEED_10GBIT   (1 <<  3)
0504 #define ZFCP_FSF_PORTSPEED_8GBIT    (1 <<  4)
0505 #define ZFCP_FSF_PORTSPEED_16GBIT   (1 <<  5)
0506 #define ZFCP_FSF_PORTSPEED_32GBIT   (1 <<  6)
0507 #define ZFCP_FSF_PORTSPEED_64GBIT   (1 <<  7)
0508 #define ZFCP_FSF_PORTSPEED_128GBIT  (1 <<  8)
0509 #define ZFCP_FSF_PORTSPEED_NOT_NEGOTIATED (1 << 15)
0510 
0511 u32 zfcp_fsf_convert_portspeed(u32 fsf_speed)
0512 {
0513     u32 fdmi_speed = 0;
0514     if (fsf_speed & ZFCP_FSF_PORTSPEED_1GBIT)
0515         fdmi_speed |= FC_PORTSPEED_1GBIT;
0516     if (fsf_speed & ZFCP_FSF_PORTSPEED_2GBIT)
0517         fdmi_speed |= FC_PORTSPEED_2GBIT;
0518     if (fsf_speed & ZFCP_FSF_PORTSPEED_4GBIT)
0519         fdmi_speed |= FC_PORTSPEED_4GBIT;
0520     if (fsf_speed & ZFCP_FSF_PORTSPEED_10GBIT)
0521         fdmi_speed |= FC_PORTSPEED_10GBIT;
0522     if (fsf_speed & ZFCP_FSF_PORTSPEED_8GBIT)
0523         fdmi_speed |= FC_PORTSPEED_8GBIT;
0524     if (fsf_speed & ZFCP_FSF_PORTSPEED_16GBIT)
0525         fdmi_speed |= FC_PORTSPEED_16GBIT;
0526     if (fsf_speed & ZFCP_FSF_PORTSPEED_32GBIT)
0527         fdmi_speed |= FC_PORTSPEED_32GBIT;
0528     if (fsf_speed & ZFCP_FSF_PORTSPEED_64GBIT)
0529         fdmi_speed |= FC_PORTSPEED_64GBIT;
0530     if (fsf_speed & ZFCP_FSF_PORTSPEED_128GBIT)
0531         fdmi_speed |= FC_PORTSPEED_128GBIT;
0532     if (fsf_speed & ZFCP_FSF_PORTSPEED_NOT_NEGOTIATED)
0533         fdmi_speed |= FC_PORTSPEED_NOT_NEGOTIATED;
0534     return fdmi_speed;
0535 }
0536 
0537 static int zfcp_fsf_exchange_config_evaluate(struct zfcp_fsf_req *req)
0538 {
0539     struct fsf_qtcb_bottom_config *bottom = &req->qtcb->bottom.config;
0540     struct zfcp_adapter *adapter = req->adapter;
0541     struct fc_els_flogi *plogi;
0542 
0543     /* adjust pointers for missing command code */
0544     plogi = (struct fc_els_flogi *) ((u8 *)&bottom->plogi_payload
0545                     - sizeof(u32));
0546 
0547     if (req->data)
0548         memcpy(req->data, bottom, sizeof(*bottom));
0549 
0550     adapter->timer_ticks = bottom->timer_interval & ZFCP_FSF_TIMER_INT_MASK;
0551     adapter->stat_read_buf_num = max(bottom->status_read_buf_num,
0552                      (u16)FSF_STATUS_READS_RECOM);
0553 
0554     /* no error return above here, otherwise must fix call chains */
0555     /* do not evaluate invalid fields */
0556     if (req->qtcb->header.fsf_status == FSF_EXCHANGE_CONFIG_DATA_INCOMPLETE)
0557         return 0;
0558 
0559     adapter->hydra_version = bottom->adapter_type;
0560 
0561     switch (bottom->fc_topology) {
0562     case FSF_TOPO_P2P:
0563         adapter->peer_d_id = ntoh24(bottom->peer_d_id);
0564         adapter->peer_wwpn = be64_to_cpu(plogi->fl_wwpn);
0565         adapter->peer_wwnn = be64_to_cpu(plogi->fl_wwnn);
0566         break;
0567     case FSF_TOPO_FABRIC:
0568         break;
0569     case FSF_TOPO_AL:
0570     default:
0571         dev_err(&adapter->ccw_device->dev,
0572             "Unknown or unsupported arbitrated loop "
0573             "fibre channel topology detected\n");
0574         zfcp_erp_adapter_shutdown(adapter, 0, "fsece_1");
0575         return -EIO;
0576     }
0577 
0578     return 0;
0579 }
0580 
0581 static void zfcp_fsf_exchange_config_data_handler(struct zfcp_fsf_req *req)
0582 {
0583     struct zfcp_adapter *adapter = req->adapter;
0584     struct zfcp_diag_header *const diag_hdr =
0585         &adapter->diagnostics->config_data.header;
0586     struct fsf_qtcb *qtcb = req->qtcb;
0587     struct fsf_qtcb_bottom_config *bottom = &qtcb->bottom.config;
0588 
0589     if (req->status & ZFCP_STATUS_FSFREQ_ERROR)
0590         return;
0591 
0592     adapter->fsf_lic_version = bottom->lic_version;
0593     adapter->adapter_features = bottom->adapter_features;
0594     adapter->connection_features = bottom->connection_features;
0595     adapter->peer_wwpn = 0;
0596     adapter->peer_wwnn = 0;
0597     adapter->peer_d_id = 0;
0598 
0599     switch (qtcb->header.fsf_status) {
0600     case FSF_GOOD:
0601         /*
0602          * usually we wait with an update till the cache is too old,
0603          * but because we have the data available, update it anyway
0604          */
0605         zfcp_diag_update_xdata(diag_hdr, bottom, false);
0606 
0607         zfcp_scsi_shost_update_config_data(adapter, bottom, false);
0608         if (zfcp_fsf_exchange_config_evaluate(req))
0609             return;
0610 
0611         if (bottom->max_qtcb_size < sizeof(struct fsf_qtcb)) {
0612             dev_err(&adapter->ccw_device->dev,
0613                 "FCP adapter maximum QTCB size (%d bytes) "
0614                 "is too small\n",
0615                 bottom->max_qtcb_size);
0616             zfcp_erp_adapter_shutdown(adapter, 0, "fsecdh1");
0617             return;
0618         }
0619         atomic_or(ZFCP_STATUS_ADAPTER_XCONFIG_OK,
0620                 &adapter->status);
0621         break;
0622     case FSF_EXCHANGE_CONFIG_DATA_INCOMPLETE:
0623         zfcp_diag_update_xdata(diag_hdr, bottom, true);
0624         req->status |= ZFCP_STATUS_FSFREQ_XDATAINCOMPLETE;
0625 
0626         /* avoids adapter shutdown to be able to recognize
0627          * events such as LINK UP */
0628         atomic_or(ZFCP_STATUS_ADAPTER_XCONFIG_OK,
0629                 &adapter->status);
0630         zfcp_fsf_link_down_info_eval(req,
0631             &qtcb->header.fsf_status_qual.link_down_info);
0632 
0633         zfcp_scsi_shost_update_config_data(adapter, bottom, true);
0634         if (zfcp_fsf_exchange_config_evaluate(req))
0635             return;
0636         break;
0637     default:
0638         zfcp_erp_adapter_shutdown(adapter, 0, "fsecdh3");
0639         return;
0640     }
0641 
0642     if (adapter->adapter_features & FSF_FEATURE_HBAAPI_MANAGEMENT)
0643         adapter->hardware_version = bottom->hardware_version;
0644 
0645     if (FSF_QTCB_CURRENT_VERSION < bottom->low_qtcb_version) {
0646         dev_err(&adapter->ccw_device->dev,
0647             "The FCP adapter only supports newer "
0648             "control block versions\n");
0649         zfcp_erp_adapter_shutdown(adapter, 0, "fsecdh4");
0650         return;
0651     }
0652     if (FSF_QTCB_CURRENT_VERSION > bottom->high_qtcb_version) {
0653         dev_err(&adapter->ccw_device->dev,
0654             "The FCP adapter only supports older "
0655             "control block versions\n");
0656         zfcp_erp_adapter_shutdown(adapter, 0, "fsecdh5");
0657     }
0658 }
0659 
0660 /*
0661  * Mapping of FC Endpoint Security flag masks to mnemonics
0662  *
0663  * NOTE: Update macro ZFCP_FSF_MAX_FC_SECURITY_MNEMONIC_LENGTH when making any
0664  *       changes.
0665  */
0666 static const struct {
0667     u32 mask;
0668     char    *name;
0669 } zfcp_fsf_fc_security_mnemonics[] = {
0670     { FSF_FC_SECURITY_AUTH,     "Authentication" },
0671     { FSF_FC_SECURITY_ENC_FCSP2 |
0672       FSF_FC_SECURITY_ENC_ERAS, "Encryption" },
0673 };
0674 
0675 /* maximum strlen(zfcp_fsf_fc_security_mnemonics[...].name) + 1 */
0676 #define ZFCP_FSF_MAX_FC_SECURITY_MNEMONIC_LENGTH 15
0677 
0678 /**
0679  * zfcp_fsf_scnprint_fc_security() - translate FC Endpoint Security flags into
0680  *                                   mnemonics and place in a buffer
0681  * @buf        : the buffer to place the translated FC Endpoint Security flag(s)
0682  *               into
0683  * @size       : the size of the buffer, including the trailing null space
0684  * @fc_security: one or more FC Endpoint Security flags, or zero
0685  * @fmt        : specifies whether a list or a single item is to be put into the
0686  *               buffer
0687  *
0688  * The Fibre Channel (FC) Endpoint Security flags are translated into mnemonics.
0689  * If the FC Endpoint Security flags are zero "none" is placed into the buffer.
0690  *
0691  * With ZFCP_FSF_PRINT_FMT_LIST the mnemonics are placed as a list separated by
0692  * a comma followed by a space into the buffer. If one or more FC Endpoint
0693  * Security flags cannot be translated into a mnemonic, as they are undefined
0694  * in zfcp_fsf_fc_security_mnemonics, their bitwise ORed value in hexadecimal
0695  * representation is placed into the buffer.
0696  *
0697  * With ZFCP_FSF_PRINT_FMT_SINGLEITEM only one single mnemonic is placed into
0698  * the buffer. If the FC Endpoint Security flag cannot be translated, as it is
0699  * undefined in zfcp_fsf_fc_security_mnemonics, its value in hexadecimal
0700  * representation is placed into the buffer. If more than one FC Endpoint
0701  * Security flag was specified, their value in hexadecimal representation is
0702  * placed into the buffer. The macro ZFCP_FSF_MAX_FC_SECURITY_MNEMONIC_LENGTH
0703  * can be used to define a buffer that is large enough to hold one mnemonic.
0704  *
0705  * Return: The number of characters written into buf not including the trailing
0706  *         '\0'. If size is == 0 the function returns 0.
0707  */
0708 ssize_t zfcp_fsf_scnprint_fc_security(char *buf, size_t size, u32 fc_security,
0709                       enum zfcp_fsf_print_fmt fmt)
0710 {
0711     const char *prefix = "";
0712     ssize_t len = 0;
0713     int i;
0714 
0715     if (fc_security == 0)
0716         return scnprintf(buf, size, "none");
0717     if (fmt == ZFCP_FSF_PRINT_FMT_SINGLEITEM && hweight32(fc_security) != 1)
0718         return scnprintf(buf, size, "0x%08x", fc_security);
0719 
0720     for (i = 0; i < ARRAY_SIZE(zfcp_fsf_fc_security_mnemonics); i++) {
0721         if (!(fc_security & zfcp_fsf_fc_security_mnemonics[i].mask))
0722             continue;
0723 
0724         len += scnprintf(buf + len, size - len, "%s%s", prefix,
0725                  zfcp_fsf_fc_security_mnemonics[i].name);
0726         prefix = ", ";
0727         fc_security &= ~zfcp_fsf_fc_security_mnemonics[i].mask;
0728     }
0729 
0730     if (fc_security != 0)
0731         len += scnprintf(buf + len, size - len, "%s0x%08x",
0732                  prefix, fc_security);
0733 
0734     return len;
0735 }
0736 
0737 static void zfcp_fsf_dbf_adapter_fc_security(struct zfcp_adapter *adapter,
0738                          struct zfcp_fsf_req *req)
0739 {
0740     if (adapter->fc_security_algorithms ==
0741         adapter->fc_security_algorithms_old) {
0742         /* no change, no trace */
0743         return;
0744     }
0745 
0746     zfcp_dbf_hba_fsf_fces("fsfcesa", req, ZFCP_DBF_INVALID_WWPN,
0747                   adapter->fc_security_algorithms_old,
0748                   adapter->fc_security_algorithms);
0749 
0750     adapter->fc_security_algorithms_old = adapter->fc_security_algorithms;
0751 }
0752 
0753 static void zfcp_fsf_exchange_port_evaluate(struct zfcp_fsf_req *req)
0754 {
0755     struct zfcp_adapter *adapter = req->adapter;
0756     struct fsf_qtcb_bottom_port *bottom = &req->qtcb->bottom.port;
0757 
0758     if (req->data)
0759         memcpy(req->data, bottom, sizeof(*bottom));
0760 
0761     if (adapter->adapter_features & FSF_FEATURE_FC_SECURITY)
0762         adapter->fc_security_algorithms =
0763             bottom->fc_security_algorithms;
0764     else
0765         adapter->fc_security_algorithms = 0;
0766     zfcp_fsf_dbf_adapter_fc_security(adapter, req);
0767 }
0768 
0769 static void zfcp_fsf_exchange_port_data_handler(struct zfcp_fsf_req *req)
0770 {
0771     struct zfcp_diag_header *const diag_hdr =
0772         &req->adapter->diagnostics->port_data.header;
0773     struct fsf_qtcb *qtcb = req->qtcb;
0774     struct fsf_qtcb_bottom_port *bottom = &qtcb->bottom.port;
0775 
0776     if (req->status & ZFCP_STATUS_FSFREQ_ERROR)
0777         return;
0778 
0779     switch (qtcb->header.fsf_status) {
0780     case FSF_GOOD:
0781         /*
0782          * usually we wait with an update till the cache is too old,
0783          * but because we have the data available, update it anyway
0784          */
0785         zfcp_diag_update_xdata(diag_hdr, bottom, false);
0786 
0787         zfcp_scsi_shost_update_port_data(req->adapter, bottom);
0788         zfcp_fsf_exchange_port_evaluate(req);
0789         break;
0790     case FSF_EXCHANGE_CONFIG_DATA_INCOMPLETE:
0791         zfcp_diag_update_xdata(diag_hdr, bottom, true);
0792         req->status |= ZFCP_STATUS_FSFREQ_XDATAINCOMPLETE;
0793 
0794         zfcp_fsf_link_down_info_eval(req,
0795             &qtcb->header.fsf_status_qual.link_down_info);
0796 
0797         zfcp_scsi_shost_update_port_data(req->adapter, bottom);
0798         zfcp_fsf_exchange_port_evaluate(req);
0799         break;
0800     }
0801 }
0802 
0803 static struct zfcp_fsf_req *zfcp_fsf_alloc(mempool_t *pool)
0804 {
0805     struct zfcp_fsf_req *req;
0806 
0807     if (likely(pool))
0808         req = mempool_alloc(pool, GFP_ATOMIC);
0809     else
0810         req = kmalloc(sizeof(*req), GFP_ATOMIC);
0811 
0812     if (unlikely(!req))
0813         return NULL;
0814 
0815     memset(req, 0, sizeof(*req));
0816     req->pool = pool;
0817     return req;
0818 }
0819 
0820 static struct fsf_qtcb *zfcp_fsf_qtcb_alloc(mempool_t *pool)
0821 {
0822     struct fsf_qtcb *qtcb;
0823 
0824     if (likely(pool))
0825         qtcb = mempool_alloc(pool, GFP_ATOMIC);
0826     else
0827         qtcb = kmem_cache_alloc(zfcp_fsf_qtcb_cache, GFP_ATOMIC);
0828 
0829     if (unlikely(!qtcb))
0830         return NULL;
0831 
0832     memset(qtcb, 0, sizeof(*qtcb));
0833     return qtcb;
0834 }
0835 
0836 static struct zfcp_fsf_req *zfcp_fsf_req_create(struct zfcp_qdio *qdio,
0837                         u32 fsf_cmd, u8 sbtype,
0838                         mempool_t *pool)
0839 {
0840     struct zfcp_adapter *adapter = qdio->adapter;
0841     struct zfcp_fsf_req *req = zfcp_fsf_alloc(pool);
0842 
0843     if (unlikely(!req))
0844         return ERR_PTR(-ENOMEM);
0845 
0846     if (adapter->req_no == 0)
0847         adapter->req_no++;
0848 
0849     timer_setup(&req->timer, NULL, 0);
0850     init_completion(&req->completion);
0851 
0852     req->adapter = adapter;
0853     req->req_id = adapter->req_no;
0854 
0855     if (likely(fsf_cmd != FSF_QTCB_UNSOLICITED_STATUS)) {
0856         if (likely(pool))
0857             req->qtcb = zfcp_fsf_qtcb_alloc(
0858                 adapter->pool.qtcb_pool);
0859         else
0860             req->qtcb = zfcp_fsf_qtcb_alloc(NULL);
0861 
0862         if (unlikely(!req->qtcb)) {
0863             zfcp_fsf_req_free(req);
0864             return ERR_PTR(-ENOMEM);
0865         }
0866 
0867         req->qtcb->prefix.req_seq_no = adapter->fsf_req_seq_no;
0868         req->qtcb->prefix.req_id = req->req_id;
0869         req->qtcb->prefix.ulp_info = 26;
0870         req->qtcb->prefix.qtcb_type = fsf_qtcb_type[fsf_cmd];
0871         req->qtcb->prefix.qtcb_version = FSF_QTCB_CURRENT_VERSION;
0872         req->qtcb->header.req_handle = req->req_id;
0873         req->qtcb->header.fsf_command = fsf_cmd;
0874     }
0875 
0876     zfcp_qdio_req_init(adapter->qdio, &req->qdio_req, req->req_id, sbtype,
0877                req->qtcb, sizeof(struct fsf_qtcb));
0878 
0879     return req;
0880 }
0881 
0882 static int zfcp_fsf_req_send(struct zfcp_fsf_req *req)
0883 {
0884     const bool is_srb = zfcp_fsf_req_is_status_read_buffer(req);
0885     struct zfcp_adapter *adapter = req->adapter;
0886     struct zfcp_qdio *qdio = adapter->qdio;
0887     int req_id = req->req_id;
0888 
0889     zfcp_reqlist_add(adapter->req_list, req);
0890 
0891     req->qdio_req.qdio_outb_usage = atomic_read(&qdio->req_q_free);
0892     req->issued = get_tod_clock();
0893     if (zfcp_qdio_send(qdio, &req->qdio_req)) {
0894         del_timer_sync(&req->timer);
0895         /* lookup request again, list might have changed */
0896         zfcp_reqlist_find_rm(adapter->req_list, req_id);
0897         zfcp_erp_adapter_reopen(adapter, 0, "fsrs__1");
0898         return -EIO;
0899     }
0900 
0901     /*
0902      * NOTE: DO NOT TOUCH ASYNC req PAST THIS POINT.
0903      *   ONLY TOUCH SYNC req AGAIN ON req->completion.
0904      *
0905      * The request might complete and be freed concurrently at any point
0906      * now. This is not protected by the QDIO-lock (req_q_lock). So any
0907      * uncontrolled access after this might result in an use-after-free bug.
0908      * Only if the request doesn't have ZFCP_STATUS_FSFREQ_CLEANUP set, and
0909      * when it is completed via req->completion, is it safe to use req
0910      * again.
0911      */
0912 
0913     /* Don't increase for unsolicited status */
0914     if (!is_srb)
0915         adapter->fsf_req_seq_no++;
0916     adapter->req_no++;
0917 
0918     return 0;
0919 }
0920 
0921 /**
0922  * zfcp_fsf_status_read - send status read request
0923  * @qdio: pointer to struct zfcp_qdio
0924  * Returns: 0 on success, ERROR otherwise
0925  */
0926 int zfcp_fsf_status_read(struct zfcp_qdio *qdio)
0927 {
0928     struct zfcp_adapter *adapter = qdio->adapter;
0929     struct zfcp_fsf_req *req;
0930     struct fsf_status_read_buffer *sr_buf;
0931     struct page *page;
0932     int retval = -EIO;
0933 
0934     spin_lock_irq(&qdio->req_q_lock);
0935     if (zfcp_qdio_sbal_get(qdio))
0936         goto out;
0937 
0938     req = zfcp_fsf_req_create(qdio, FSF_QTCB_UNSOLICITED_STATUS,
0939                   SBAL_SFLAGS0_TYPE_STATUS,
0940                   adapter->pool.status_read_req);
0941     if (IS_ERR(req)) {
0942         retval = PTR_ERR(req);
0943         goto out;
0944     }
0945 
0946     page = mempool_alloc(adapter->pool.sr_data, GFP_ATOMIC);
0947     if (!page) {
0948         retval = -ENOMEM;
0949         goto failed_buf;
0950     }
0951     sr_buf = page_address(page);
0952     memset(sr_buf, 0, sizeof(*sr_buf));
0953     req->data = sr_buf;
0954 
0955     zfcp_qdio_fill_next(qdio, &req->qdio_req, sr_buf, sizeof(*sr_buf));
0956     zfcp_qdio_set_sbale_last(qdio, &req->qdio_req);
0957 
0958     retval = zfcp_fsf_req_send(req);
0959     if (retval)
0960         goto failed_req_send;
0961     /* NOTE: DO NOT TOUCH req PAST THIS POINT! */
0962 
0963     goto out;
0964 
0965 failed_req_send:
0966     req->data = NULL;
0967     mempool_free(virt_to_page(sr_buf), adapter->pool.sr_data);
0968 failed_buf:
0969     zfcp_dbf_hba_fsf_uss("fssr__1", req);
0970     zfcp_fsf_req_free(req);
0971 out:
0972     spin_unlock_irq(&qdio->req_q_lock);
0973     return retval;
0974 }
0975 
0976 static void zfcp_fsf_abort_fcp_command_handler(struct zfcp_fsf_req *req)
0977 {
0978     struct scsi_device *sdev = req->data;
0979     struct zfcp_scsi_dev *zfcp_sdev;
0980     union fsf_status_qual *fsq = &req->qtcb->header.fsf_status_qual;
0981 
0982     if (req->status & ZFCP_STATUS_FSFREQ_ERROR)
0983         return;
0984 
0985     zfcp_sdev = sdev_to_zfcp(sdev);
0986 
0987     switch (req->qtcb->header.fsf_status) {
0988     case FSF_PORT_HANDLE_NOT_VALID:
0989         if (fsq->word[0] == fsq->word[1]) {
0990             zfcp_erp_adapter_reopen(zfcp_sdev->port->adapter, 0,
0991                         "fsafch1");
0992             req->status |= ZFCP_STATUS_FSFREQ_ERROR;
0993         }
0994         break;
0995     case FSF_LUN_HANDLE_NOT_VALID:
0996         if (fsq->word[0] == fsq->word[1]) {
0997             zfcp_erp_port_reopen(zfcp_sdev->port, 0, "fsafch2");
0998             req->status |= ZFCP_STATUS_FSFREQ_ERROR;
0999         }
1000         break;
1001     case FSF_FCP_COMMAND_DOES_NOT_EXIST:
1002         req->status |= ZFCP_STATUS_FSFREQ_ABORTNOTNEEDED;
1003         break;
1004     case FSF_PORT_BOXED:
1005         zfcp_erp_set_port_status(zfcp_sdev->port,
1006                      ZFCP_STATUS_COMMON_ACCESS_BOXED);
1007         zfcp_erp_port_reopen(zfcp_sdev->port,
1008                      ZFCP_STATUS_COMMON_ERP_FAILED, "fsafch3");
1009         req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1010         break;
1011     case FSF_LUN_BOXED:
1012         zfcp_erp_set_lun_status(sdev, ZFCP_STATUS_COMMON_ACCESS_BOXED);
1013         zfcp_erp_lun_reopen(sdev, ZFCP_STATUS_COMMON_ERP_FAILED,
1014                     "fsafch4");
1015         req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1016                 break;
1017     case FSF_ADAPTER_STATUS_AVAILABLE:
1018         switch (fsq->word[0]) {
1019         case FSF_SQ_INVOKE_LINK_TEST_PROCEDURE:
1020             zfcp_fc_test_link(zfcp_sdev->port);
1021             fallthrough;
1022         case FSF_SQ_ULP_DEPENDENT_ERP_REQUIRED:
1023             req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1024             break;
1025         }
1026         break;
1027     case FSF_GOOD:
1028         req->status |= ZFCP_STATUS_FSFREQ_ABORTSUCCEEDED;
1029         break;
1030     }
1031 }
1032 
1033 /**
1034  * zfcp_fsf_abort_fcp_cmnd - abort running SCSI command
1035  * @scmnd: The SCSI command to abort
1036  * Returns: pointer to struct zfcp_fsf_req
1037  */
1038 
1039 struct zfcp_fsf_req *zfcp_fsf_abort_fcp_cmnd(struct scsi_cmnd *scmnd)
1040 {
1041     struct zfcp_fsf_req *req = NULL;
1042     struct scsi_device *sdev = scmnd->device;
1043     struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
1044     struct zfcp_qdio *qdio = zfcp_sdev->port->adapter->qdio;
1045     unsigned long old_req_id = (unsigned long) scmnd->host_scribble;
1046 
1047     spin_lock_irq(&qdio->req_q_lock);
1048     if (zfcp_qdio_sbal_get(qdio))
1049         goto out;
1050     req = zfcp_fsf_req_create(qdio, FSF_QTCB_ABORT_FCP_CMND,
1051                   SBAL_SFLAGS0_TYPE_READ,
1052                   qdio->adapter->pool.scsi_abort);
1053     if (IS_ERR(req)) {
1054         req = NULL;
1055         goto out;
1056     }
1057 
1058     if (unlikely(!(atomic_read(&zfcp_sdev->status) &
1059                ZFCP_STATUS_COMMON_UNBLOCKED)))
1060         goto out_error_free;
1061 
1062     zfcp_qdio_set_sbale_last(qdio, &req->qdio_req);
1063 
1064     req->data = sdev;
1065     req->handler = zfcp_fsf_abort_fcp_command_handler;
1066     req->qtcb->header.lun_handle = zfcp_sdev->lun_handle;
1067     req->qtcb->header.port_handle = zfcp_sdev->port->handle;
1068     req->qtcb->bottom.support.req_handle = (u64) old_req_id;
1069 
1070     zfcp_fsf_start_timer(req, ZFCP_FSF_SCSI_ER_TIMEOUT);
1071     if (!zfcp_fsf_req_send(req)) {
1072         /* NOTE: DO NOT TOUCH req, UNTIL IT COMPLETES! */
1073         goto out;
1074     }
1075 
1076 out_error_free:
1077     zfcp_fsf_req_free(req);
1078     req = NULL;
1079 out:
1080     spin_unlock_irq(&qdio->req_q_lock);
1081     return req;
1082 }
1083 
1084 static void zfcp_fsf_send_ct_handler(struct zfcp_fsf_req *req)
1085 {
1086     struct zfcp_adapter *adapter = req->adapter;
1087     struct zfcp_fsf_ct_els *ct = req->data;
1088     struct fsf_qtcb_header *header = &req->qtcb->header;
1089 
1090     ct->status = -EINVAL;
1091 
1092     if (req->status & ZFCP_STATUS_FSFREQ_ERROR)
1093         goto skip_fsfstatus;
1094 
1095     switch (header->fsf_status) {
1096         case FSF_GOOD:
1097         ct->status = 0;
1098         zfcp_dbf_san_res("fsscth2", req);
1099         break;
1100         case FSF_SERVICE_CLASS_NOT_SUPPORTED:
1101         zfcp_fsf_class_not_supp(req);
1102         break;
1103         case FSF_ADAPTER_STATUS_AVAILABLE:
1104                 switch (header->fsf_status_qual.word[0]){
1105                 case FSF_SQ_INVOKE_LINK_TEST_PROCEDURE:
1106                 case FSF_SQ_ULP_DEPENDENT_ERP_REQUIRED:
1107             req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1108             break;
1109                 }
1110                 break;
1111         case FSF_PORT_BOXED:
1112         req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1113         break;
1114     case FSF_PORT_HANDLE_NOT_VALID:
1115         zfcp_erp_adapter_reopen(adapter, 0, "fsscth1");
1116         fallthrough;
1117     case FSF_GENERIC_COMMAND_REJECTED:
1118     case FSF_PAYLOAD_SIZE_MISMATCH:
1119     case FSF_REQUEST_SIZE_TOO_LARGE:
1120     case FSF_RESPONSE_SIZE_TOO_LARGE:
1121     case FSF_SBAL_MISMATCH:
1122         req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1123         break;
1124     }
1125 
1126 skip_fsfstatus:
1127     if (ct->handler)
1128         ct->handler(ct->handler_data);
1129 }
1130 
1131 static void zfcp_fsf_setup_ct_els_unchained(struct zfcp_qdio *qdio,
1132                         struct zfcp_qdio_req *q_req,
1133                         struct scatterlist *sg_req,
1134                         struct scatterlist *sg_resp)
1135 {
1136     zfcp_qdio_fill_next(qdio, q_req, sg_virt(sg_req), sg_req->length);
1137     zfcp_qdio_fill_next(qdio, q_req, sg_virt(sg_resp), sg_resp->length);
1138     zfcp_qdio_set_sbale_last(qdio, q_req);
1139 }
1140 
1141 static int zfcp_fsf_setup_ct_els_sbals(struct zfcp_fsf_req *req,
1142                        struct scatterlist *sg_req,
1143                        struct scatterlist *sg_resp)
1144 {
1145     struct zfcp_adapter *adapter = req->adapter;
1146     struct zfcp_qdio *qdio = adapter->qdio;
1147     struct fsf_qtcb *qtcb = req->qtcb;
1148     u32 feat = adapter->adapter_features;
1149 
1150     if (zfcp_adapter_multi_buffer_active(adapter)) {
1151         if (zfcp_qdio_sbals_from_sg(qdio, &req->qdio_req, sg_req))
1152             return -EIO;
1153         qtcb->bottom.support.req_buf_length =
1154             zfcp_qdio_real_bytes(sg_req);
1155         if (zfcp_qdio_sbals_from_sg(qdio, &req->qdio_req, sg_resp))
1156             return -EIO;
1157         qtcb->bottom.support.resp_buf_length =
1158             zfcp_qdio_real_bytes(sg_resp);
1159 
1160         zfcp_qdio_set_data_div(qdio, &req->qdio_req, sg_nents(sg_req));
1161         zfcp_qdio_set_sbale_last(qdio, &req->qdio_req);
1162         zfcp_qdio_set_scount(qdio, &req->qdio_req);
1163         return 0;
1164     }
1165 
1166     /* use single, unchained SBAL if it can hold the request */
1167     if (zfcp_qdio_sg_one_sbale(sg_req) && zfcp_qdio_sg_one_sbale(sg_resp)) {
1168         zfcp_fsf_setup_ct_els_unchained(qdio, &req->qdio_req,
1169                         sg_req, sg_resp);
1170         return 0;
1171     }
1172 
1173     if (!(feat & FSF_FEATURE_ELS_CT_CHAINED_SBALS))
1174         return -EOPNOTSUPP;
1175 
1176     if (zfcp_qdio_sbals_from_sg(qdio, &req->qdio_req, sg_req))
1177         return -EIO;
1178 
1179     qtcb->bottom.support.req_buf_length = zfcp_qdio_real_bytes(sg_req);
1180 
1181     zfcp_qdio_set_sbale_last(qdio, &req->qdio_req);
1182     zfcp_qdio_skip_to_last_sbale(qdio, &req->qdio_req);
1183 
1184     if (zfcp_qdio_sbals_from_sg(qdio, &req->qdio_req, sg_resp))
1185         return -EIO;
1186 
1187     qtcb->bottom.support.resp_buf_length = zfcp_qdio_real_bytes(sg_resp);
1188 
1189     zfcp_qdio_set_sbale_last(qdio, &req->qdio_req);
1190 
1191     return 0;
1192 }
1193 
1194 static int zfcp_fsf_setup_ct_els(struct zfcp_fsf_req *req,
1195                  struct scatterlist *sg_req,
1196                  struct scatterlist *sg_resp,
1197                  unsigned int timeout)
1198 {
1199     int ret;
1200 
1201     ret = zfcp_fsf_setup_ct_els_sbals(req, sg_req, sg_resp);
1202     if (ret)
1203         return ret;
1204 
1205     /* common settings for ct/gs and els requests */
1206     if (timeout > 255)
1207         timeout = 255; /* max value accepted by hardware */
1208     req->qtcb->bottom.support.service_class = FSF_CLASS_3;
1209     req->qtcb->bottom.support.timeout = timeout;
1210     zfcp_fsf_start_timer(req, (timeout + 10) * HZ);
1211 
1212     return 0;
1213 }
1214 
1215 /**
1216  * zfcp_fsf_send_ct - initiate a Generic Service request (FC-GS)
1217  * @wka_port: pointer to zfcp WKA port to send CT/GS to
1218  * @ct: pointer to struct zfcp_send_ct with data for request
1219  * @pool: if non-null this mempool is used to allocate struct zfcp_fsf_req
1220  * @timeout: timeout that hardware should use, and a later software timeout
1221  */
1222 int zfcp_fsf_send_ct(struct zfcp_fc_wka_port *wka_port,
1223              struct zfcp_fsf_ct_els *ct, mempool_t *pool,
1224              unsigned int timeout)
1225 {
1226     struct zfcp_qdio *qdio = wka_port->adapter->qdio;
1227     struct zfcp_fsf_req *req;
1228     int ret = -EIO;
1229 
1230     spin_lock_irq(&qdio->req_q_lock);
1231     if (zfcp_qdio_sbal_get(qdio))
1232         goto out;
1233 
1234     req = zfcp_fsf_req_create(qdio, FSF_QTCB_SEND_GENERIC,
1235                   SBAL_SFLAGS0_TYPE_WRITE_READ, pool);
1236 
1237     if (IS_ERR(req)) {
1238         ret = PTR_ERR(req);
1239         goto out;
1240     }
1241 
1242     req->status |= ZFCP_STATUS_FSFREQ_CLEANUP;
1243     ret = zfcp_fsf_setup_ct_els(req, ct->req, ct->resp, timeout);
1244     if (ret)
1245         goto failed_send;
1246 
1247     req->handler = zfcp_fsf_send_ct_handler;
1248     req->qtcb->header.port_handle = wka_port->handle;
1249     ct->d_id = wka_port->d_id;
1250     req->data = ct;
1251 
1252     zfcp_dbf_san_req("fssct_1", req, wka_port->d_id);
1253 
1254     ret = zfcp_fsf_req_send(req);
1255     if (ret)
1256         goto failed_send;
1257     /* NOTE: DO NOT TOUCH req PAST THIS POINT! */
1258 
1259     goto out;
1260 
1261 failed_send:
1262     zfcp_fsf_req_free(req);
1263 out:
1264     spin_unlock_irq(&qdio->req_q_lock);
1265     return ret;
1266 }
1267 
1268 static void zfcp_fsf_send_els_handler(struct zfcp_fsf_req *req)
1269 {
1270     struct zfcp_fsf_ct_els *send_els = req->data;
1271     struct fsf_qtcb_header *header = &req->qtcb->header;
1272 
1273     send_els->status = -EINVAL;
1274 
1275     if (req->status & ZFCP_STATUS_FSFREQ_ERROR)
1276         goto skip_fsfstatus;
1277 
1278     switch (header->fsf_status) {
1279     case FSF_GOOD:
1280         send_els->status = 0;
1281         zfcp_dbf_san_res("fsselh1", req);
1282         break;
1283     case FSF_SERVICE_CLASS_NOT_SUPPORTED:
1284         zfcp_fsf_class_not_supp(req);
1285         break;
1286     case FSF_ADAPTER_STATUS_AVAILABLE:
1287         switch (header->fsf_status_qual.word[0]){
1288         case FSF_SQ_INVOKE_LINK_TEST_PROCEDURE:
1289         case FSF_SQ_ULP_DEPENDENT_ERP_REQUIRED:
1290         case FSF_SQ_RETRY_IF_POSSIBLE:
1291             req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1292             break;
1293         }
1294         break;
1295     case FSF_ELS_COMMAND_REJECTED:
1296     case FSF_PAYLOAD_SIZE_MISMATCH:
1297     case FSF_REQUEST_SIZE_TOO_LARGE:
1298     case FSF_RESPONSE_SIZE_TOO_LARGE:
1299         break;
1300     case FSF_SBAL_MISMATCH:
1301         /* should never occur, avoided in zfcp_fsf_send_els */
1302         fallthrough;
1303     default:
1304         req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1305         break;
1306     }
1307 skip_fsfstatus:
1308     if (send_els->handler)
1309         send_els->handler(send_els->handler_data);
1310 }
1311 
1312 /**
1313  * zfcp_fsf_send_els - initiate an ELS command (FC-FS)
1314  * @adapter: pointer to zfcp adapter
1315  * @d_id: N_Port_ID to send ELS to
1316  * @els: pointer to struct zfcp_send_els with data for the command
1317  * @timeout: timeout that hardware should use, and a later software timeout
1318  */
1319 int zfcp_fsf_send_els(struct zfcp_adapter *adapter, u32 d_id,
1320               struct zfcp_fsf_ct_els *els, unsigned int timeout)
1321 {
1322     struct zfcp_fsf_req *req;
1323     struct zfcp_qdio *qdio = adapter->qdio;
1324     int ret = -EIO;
1325 
1326     spin_lock_irq(&qdio->req_q_lock);
1327     if (zfcp_qdio_sbal_get(qdio))
1328         goto out;
1329 
1330     req = zfcp_fsf_req_create(qdio, FSF_QTCB_SEND_ELS,
1331                   SBAL_SFLAGS0_TYPE_WRITE_READ, NULL);
1332 
1333     if (IS_ERR(req)) {
1334         ret = PTR_ERR(req);
1335         goto out;
1336     }
1337 
1338     req->status |= ZFCP_STATUS_FSFREQ_CLEANUP;
1339 
1340     if (!zfcp_adapter_multi_buffer_active(adapter))
1341         zfcp_qdio_sbal_limit(qdio, &req->qdio_req, 2);
1342 
1343     ret = zfcp_fsf_setup_ct_els(req, els->req, els->resp, timeout);
1344 
1345     if (ret)
1346         goto failed_send;
1347 
1348     hton24(req->qtcb->bottom.support.d_id, d_id);
1349     req->handler = zfcp_fsf_send_els_handler;
1350     els->d_id = d_id;
1351     req->data = els;
1352 
1353     zfcp_dbf_san_req("fssels1", req, d_id);
1354 
1355     ret = zfcp_fsf_req_send(req);
1356     if (ret)
1357         goto failed_send;
1358     /* NOTE: DO NOT TOUCH req PAST THIS POINT! */
1359 
1360     goto out;
1361 
1362 failed_send:
1363     zfcp_fsf_req_free(req);
1364 out:
1365     spin_unlock_irq(&qdio->req_q_lock);
1366     return ret;
1367 }
1368 
1369 int zfcp_fsf_exchange_config_data(struct zfcp_erp_action *erp_action)
1370 {
1371     struct zfcp_fsf_req *req;
1372     struct zfcp_qdio *qdio = erp_action->adapter->qdio;
1373     int retval = -EIO;
1374 
1375     spin_lock_irq(&qdio->req_q_lock);
1376     if (zfcp_qdio_sbal_get(qdio))
1377         goto out;
1378 
1379     req = zfcp_fsf_req_create(qdio, FSF_QTCB_EXCHANGE_CONFIG_DATA,
1380                   SBAL_SFLAGS0_TYPE_READ,
1381                   qdio->adapter->pool.erp_req);
1382 
1383     if (IS_ERR(req)) {
1384         retval = PTR_ERR(req);
1385         goto out;
1386     }
1387 
1388     req->status |= ZFCP_STATUS_FSFREQ_CLEANUP;
1389     zfcp_qdio_set_sbale_last(qdio, &req->qdio_req);
1390 
1391     req->qtcb->bottom.config.feature_selection =
1392             FSF_FEATURE_NOTIFICATION_LOST |
1393             FSF_FEATURE_UPDATE_ALERT |
1394             FSF_FEATURE_REQUEST_SFP_DATA |
1395             FSF_FEATURE_FC_SECURITY;
1396     req->erp_action = erp_action;
1397     req->handler = zfcp_fsf_exchange_config_data_handler;
1398     erp_action->fsf_req_id = req->req_id;
1399 
1400     zfcp_fsf_start_erp_timer(req);
1401     retval = zfcp_fsf_req_send(req);
1402     if (retval) {
1403         zfcp_fsf_req_free(req);
1404         erp_action->fsf_req_id = 0;
1405     }
1406     /* NOTE: DO NOT TOUCH req PAST THIS POINT! */
1407 out:
1408     spin_unlock_irq(&qdio->req_q_lock);
1409     return retval;
1410 }
1411 
1412 
1413 /**
1414  * zfcp_fsf_exchange_config_data_sync() - Request information about FCP channel.
1415  * @qdio: pointer to the QDIO-Queue to use for sending the command.
1416  * @data: pointer to the QTCB-Bottom for storing the result of the command,
1417  *    might be %NULL.
1418  *
1419  * Returns:
1420  * * 0      - Exchange Config Data was successful, @data is complete
1421  * * -EIO   - Exchange Config Data was not successful, @data is invalid
1422  * * -EAGAIN    - @data contains incomplete data
1423  * * -ENOMEM    - Some memory allocation failed along the way
1424  */
1425 int zfcp_fsf_exchange_config_data_sync(struct zfcp_qdio *qdio,
1426                        struct fsf_qtcb_bottom_config *data)
1427 {
1428     struct zfcp_fsf_req *req = NULL;
1429     int retval = -EIO;
1430 
1431     spin_lock_irq(&qdio->req_q_lock);
1432     if (zfcp_qdio_sbal_get(qdio))
1433         goto out_unlock;
1434 
1435     req = zfcp_fsf_req_create(qdio, FSF_QTCB_EXCHANGE_CONFIG_DATA,
1436                   SBAL_SFLAGS0_TYPE_READ, NULL);
1437 
1438     if (IS_ERR(req)) {
1439         retval = PTR_ERR(req);
1440         goto out_unlock;
1441     }
1442 
1443     zfcp_qdio_set_sbale_last(qdio, &req->qdio_req);
1444     req->handler = zfcp_fsf_exchange_config_data_handler;
1445 
1446     req->qtcb->bottom.config.feature_selection =
1447             FSF_FEATURE_NOTIFICATION_LOST |
1448             FSF_FEATURE_UPDATE_ALERT |
1449             FSF_FEATURE_REQUEST_SFP_DATA |
1450             FSF_FEATURE_FC_SECURITY;
1451 
1452     if (data)
1453         req->data = data;
1454 
1455     zfcp_fsf_start_timer(req, ZFCP_FSF_REQUEST_TIMEOUT);
1456     retval = zfcp_fsf_req_send(req);
1457     spin_unlock_irq(&qdio->req_q_lock);
1458 
1459     if (!retval) {
1460         /* NOTE: ONLY TOUCH SYNC req AGAIN ON req->completion. */
1461         wait_for_completion(&req->completion);
1462 
1463         if (req->status &
1464             (ZFCP_STATUS_FSFREQ_ERROR | ZFCP_STATUS_FSFREQ_DISMISSED))
1465             retval = -EIO;
1466         else if (req->status & ZFCP_STATUS_FSFREQ_XDATAINCOMPLETE)
1467             retval = -EAGAIN;
1468     }
1469 
1470     zfcp_fsf_req_free(req);
1471     return retval;
1472 
1473 out_unlock:
1474     spin_unlock_irq(&qdio->req_q_lock);
1475     return retval;
1476 }
1477 
1478 /**
1479  * zfcp_fsf_exchange_port_data - request information about local port
1480  * @erp_action: ERP action for the adapter for which port data is requested
1481  * Returns: 0 on success, error otherwise
1482  */
1483 int zfcp_fsf_exchange_port_data(struct zfcp_erp_action *erp_action)
1484 {
1485     struct zfcp_qdio *qdio = erp_action->adapter->qdio;
1486     struct zfcp_fsf_req *req;
1487     int retval = -EIO;
1488 
1489     if (!(qdio->adapter->adapter_features & FSF_FEATURE_HBAAPI_MANAGEMENT))
1490         return -EOPNOTSUPP;
1491 
1492     spin_lock_irq(&qdio->req_q_lock);
1493     if (zfcp_qdio_sbal_get(qdio))
1494         goto out;
1495 
1496     req = zfcp_fsf_req_create(qdio, FSF_QTCB_EXCHANGE_PORT_DATA,
1497                   SBAL_SFLAGS0_TYPE_READ,
1498                   qdio->adapter->pool.erp_req);
1499 
1500     if (IS_ERR(req)) {
1501         retval = PTR_ERR(req);
1502         goto out;
1503     }
1504 
1505     req->status |= ZFCP_STATUS_FSFREQ_CLEANUP;
1506     zfcp_qdio_set_sbale_last(qdio, &req->qdio_req);
1507 
1508     req->handler = zfcp_fsf_exchange_port_data_handler;
1509     req->erp_action = erp_action;
1510     erp_action->fsf_req_id = req->req_id;
1511 
1512     zfcp_fsf_start_erp_timer(req);
1513     retval = zfcp_fsf_req_send(req);
1514     if (retval) {
1515         zfcp_fsf_req_free(req);
1516         erp_action->fsf_req_id = 0;
1517     }
1518     /* NOTE: DO NOT TOUCH req PAST THIS POINT! */
1519 out:
1520     spin_unlock_irq(&qdio->req_q_lock);
1521     return retval;
1522 }
1523 
1524 /**
1525  * zfcp_fsf_exchange_port_data_sync() - Request information about local port.
1526  * @qdio: pointer to the QDIO-Queue to use for sending the command.
1527  * @data: pointer to the QTCB-Bottom for storing the result of the command,
1528  *    might be %NULL.
1529  *
1530  * Returns:
1531  * * 0      - Exchange Port Data was successful, @data is complete
1532  * * -EIO   - Exchange Port Data was not successful, @data is invalid
1533  * * -EAGAIN    - @data contains incomplete data
1534  * * -ENOMEM    - Some memory allocation failed along the way
1535  * * -EOPNOTSUPP    - This operation is not supported
1536  */
1537 int zfcp_fsf_exchange_port_data_sync(struct zfcp_qdio *qdio,
1538                      struct fsf_qtcb_bottom_port *data)
1539 {
1540     struct zfcp_fsf_req *req = NULL;
1541     int retval = -EIO;
1542 
1543     if (!(qdio->adapter->adapter_features & FSF_FEATURE_HBAAPI_MANAGEMENT))
1544         return -EOPNOTSUPP;
1545 
1546     spin_lock_irq(&qdio->req_q_lock);
1547     if (zfcp_qdio_sbal_get(qdio))
1548         goto out_unlock;
1549 
1550     req = zfcp_fsf_req_create(qdio, FSF_QTCB_EXCHANGE_PORT_DATA,
1551                   SBAL_SFLAGS0_TYPE_READ, NULL);
1552 
1553     if (IS_ERR(req)) {
1554         retval = PTR_ERR(req);
1555         goto out_unlock;
1556     }
1557 
1558     if (data)
1559         req->data = data;
1560 
1561     zfcp_qdio_set_sbale_last(qdio, &req->qdio_req);
1562 
1563     req->handler = zfcp_fsf_exchange_port_data_handler;
1564     zfcp_fsf_start_timer(req, ZFCP_FSF_REQUEST_TIMEOUT);
1565     retval = zfcp_fsf_req_send(req);
1566     spin_unlock_irq(&qdio->req_q_lock);
1567 
1568     if (!retval) {
1569         /* NOTE: ONLY TOUCH SYNC req AGAIN ON req->completion. */
1570         wait_for_completion(&req->completion);
1571 
1572         if (req->status &
1573             (ZFCP_STATUS_FSFREQ_ERROR | ZFCP_STATUS_FSFREQ_DISMISSED))
1574             retval = -EIO;
1575         else if (req->status & ZFCP_STATUS_FSFREQ_XDATAINCOMPLETE)
1576             retval = -EAGAIN;
1577     }
1578 
1579     zfcp_fsf_req_free(req);
1580     return retval;
1581 
1582 out_unlock:
1583     spin_unlock_irq(&qdio->req_q_lock);
1584     return retval;
1585 }
1586 
1587 static void zfcp_fsf_log_port_fc_security(struct zfcp_port *port,
1588                       struct zfcp_fsf_req *req)
1589 {
1590     char mnemonic_old[ZFCP_FSF_MAX_FC_SECURITY_MNEMONIC_LENGTH];
1591     char mnemonic_new[ZFCP_FSF_MAX_FC_SECURITY_MNEMONIC_LENGTH];
1592 
1593     if (port->connection_info == port->connection_info_old) {
1594         /* no change, no log nor trace */
1595         return;
1596     }
1597 
1598     zfcp_dbf_hba_fsf_fces("fsfcesp", req, port->wwpn,
1599                   port->connection_info_old,
1600                   port->connection_info);
1601 
1602     zfcp_fsf_scnprint_fc_security(mnemonic_old, sizeof(mnemonic_old),
1603                       port->connection_info_old,
1604                       ZFCP_FSF_PRINT_FMT_SINGLEITEM);
1605     zfcp_fsf_scnprint_fc_security(mnemonic_new, sizeof(mnemonic_new),
1606                       port->connection_info,
1607                       ZFCP_FSF_PRINT_FMT_SINGLEITEM);
1608 
1609     if (strncmp(mnemonic_old, mnemonic_new,
1610             ZFCP_FSF_MAX_FC_SECURITY_MNEMONIC_LENGTH) == 0) {
1611         /* no change in string representation, no log */
1612         goto out;
1613     }
1614 
1615     if (port->connection_info_old == 0) {
1616         /* activation */
1617         dev_info(&port->adapter->ccw_device->dev,
1618              "FC Endpoint Security of connection to remote port 0x%16llx enabled: %s\n",
1619              port->wwpn, mnemonic_new);
1620     } else if (port->connection_info == 0) {
1621         /* deactivation */
1622         dev_warn(&port->adapter->ccw_device->dev,
1623              "FC Endpoint Security of connection to remote port 0x%16llx disabled: was %s\n",
1624              port->wwpn, mnemonic_old);
1625     } else {
1626         /* change */
1627         dev_warn(&port->adapter->ccw_device->dev,
1628              "FC Endpoint Security of connection to remote port 0x%16llx changed: from %s to %s\n",
1629              port->wwpn, mnemonic_old, mnemonic_new);
1630     }
1631 
1632 out:
1633     port->connection_info_old = port->connection_info;
1634 }
1635 
1636 static void zfcp_fsf_log_security_error(const struct device *dev, u32 fsf_sqw0,
1637                     u64 wwpn)
1638 {
1639     switch (fsf_sqw0) {
1640 
1641     /*
1642      * Open Port command error codes
1643      */
1644 
1645     case FSF_SQ_SECURITY_REQUIRED:
1646         dev_warn_ratelimited(dev,
1647                      "FC Endpoint Security error: FC security is required but not supported or configured on remote port 0x%016llx\n",
1648                      wwpn);
1649         break;
1650     case FSF_SQ_SECURITY_TIMEOUT:
1651         dev_warn_ratelimited(dev,
1652                      "FC Endpoint Security error: a timeout prevented opening remote port 0x%016llx\n",
1653                      wwpn);
1654         break;
1655     case FSF_SQ_SECURITY_KM_UNAVAILABLE:
1656         dev_warn_ratelimited(dev,
1657                      "FC Endpoint Security error: opening remote port 0x%016llx failed because local and external key manager cannot communicate\n",
1658                      wwpn);
1659         break;
1660     case FSF_SQ_SECURITY_RKM_UNAVAILABLE:
1661         dev_warn_ratelimited(dev,
1662                      "FC Endpoint Security error: opening remote port 0x%016llx failed because it cannot communicate with the external key manager\n",
1663                      wwpn);
1664         break;
1665     case FSF_SQ_SECURITY_AUTH_FAILURE:
1666         dev_warn_ratelimited(dev,
1667                      "FC Endpoint Security error: the device could not verify the identity of remote port 0x%016llx\n",
1668                      wwpn);
1669         break;
1670 
1671     /*
1672      * Send FCP command error codes
1673      */
1674 
1675     case FSF_SQ_SECURITY_ENC_FAILURE:
1676         dev_warn_ratelimited(dev,
1677                      "FC Endpoint Security error: FC connection to remote port 0x%016llx closed because encryption broke down\n",
1678                      wwpn);
1679         break;
1680 
1681     /*
1682      * Unknown error codes
1683      */
1684 
1685     default:
1686         dev_warn_ratelimited(dev,
1687                      "FC Endpoint Security error: the device issued an unknown error code 0x%08x related to the FC connection to remote port 0x%016llx\n",
1688                      fsf_sqw0, wwpn);
1689     }
1690 }
1691 
1692 static void zfcp_fsf_open_port_handler(struct zfcp_fsf_req *req)
1693 {
1694     struct zfcp_adapter *adapter = req->adapter;
1695     struct zfcp_port *port = req->data;
1696     struct fsf_qtcb_header *header = &req->qtcb->header;
1697     struct fsf_qtcb_bottom_support *bottom = &req->qtcb->bottom.support;
1698     struct fc_els_flogi *plogi;
1699 
1700     if (req->status & ZFCP_STATUS_FSFREQ_ERROR)
1701         goto out;
1702 
1703     switch (header->fsf_status) {
1704     case FSF_PORT_ALREADY_OPEN:
1705         break;
1706     case FSF_MAXIMUM_NUMBER_OF_PORTS_EXCEEDED:
1707         dev_warn(&adapter->ccw_device->dev,
1708              "Not enough FCP adapter resources to open "
1709              "remote port 0x%016Lx\n",
1710              (unsigned long long)port->wwpn);
1711         zfcp_erp_set_port_status(port,
1712                      ZFCP_STATUS_COMMON_ERP_FAILED);
1713         req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1714         break;
1715     case FSF_SECURITY_ERROR:
1716         zfcp_fsf_log_security_error(&req->adapter->ccw_device->dev,
1717                         header->fsf_status_qual.word[0],
1718                         port->wwpn);
1719         req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1720         break;
1721     case FSF_ADAPTER_STATUS_AVAILABLE:
1722         switch (header->fsf_status_qual.word[0]) {
1723         case FSF_SQ_INVOKE_LINK_TEST_PROCEDURE:
1724             /* no zfcp_fc_test_link() with failed open port */
1725             fallthrough;
1726         case FSF_SQ_ULP_DEPENDENT_ERP_REQUIRED:
1727         case FSF_SQ_NO_RETRY_POSSIBLE:
1728             req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1729             break;
1730         }
1731         break;
1732     case FSF_GOOD:
1733         port->handle = header->port_handle;
1734         if (adapter->adapter_features & FSF_FEATURE_FC_SECURITY)
1735             port->connection_info = bottom->connection_info;
1736         else
1737             port->connection_info = 0;
1738         zfcp_fsf_log_port_fc_security(port, req);
1739         atomic_or(ZFCP_STATUS_COMMON_OPEN |
1740                 ZFCP_STATUS_PORT_PHYS_OPEN, &port->status);
1741         atomic_andnot(ZFCP_STATUS_COMMON_ACCESS_BOXED,
1742                           &port->status);
1743         /* check whether D_ID has changed during open */
1744         /*
1745          * FIXME: This check is not airtight, as the FCP channel does
1746          * not monitor closures of target port connections caused on
1747          * the remote side. Thus, they might miss out on invalidating
1748          * locally cached WWPNs (and other N_Port parameters) of gone
1749          * target ports. So, our heroic attempt to make things safe
1750          * could be undermined by 'open port' response data tagged with
1751          * obsolete WWPNs. Another reason to monitor potential
1752          * connection closures ourself at least (by interpreting
1753          * incoming ELS' and unsolicited status). It just crosses my
1754          * mind that one should be able to cross-check by means of
1755          * another GID_PN straight after a port has been opened.
1756          * Alternately, an ADISC/PDISC ELS should suffice, as well.
1757          */
1758         plogi = (struct fc_els_flogi *) bottom->els;
1759         if (bottom->els1_length >= FSF_PLOGI_MIN_LEN)
1760             zfcp_fc_plogi_evaluate(port, plogi);
1761         break;
1762     case FSF_UNKNOWN_OP_SUBTYPE:
1763         req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1764         break;
1765     }
1766 
1767 out:
1768     put_device(&port->dev);
1769 }
1770 
1771 /**
1772  * zfcp_fsf_open_port - create and send open port request
1773  * @erp_action: pointer to struct zfcp_erp_action
1774  * Returns: 0 on success, error otherwise
1775  */
1776 int zfcp_fsf_open_port(struct zfcp_erp_action *erp_action)
1777 {
1778     struct zfcp_qdio *qdio = erp_action->adapter->qdio;
1779     struct zfcp_port *port = erp_action->port;
1780     struct zfcp_fsf_req *req;
1781     int retval = -EIO;
1782 
1783     spin_lock_irq(&qdio->req_q_lock);
1784     if (zfcp_qdio_sbal_get(qdio))
1785         goto out;
1786 
1787     req = zfcp_fsf_req_create(qdio, FSF_QTCB_OPEN_PORT_WITH_DID,
1788                   SBAL_SFLAGS0_TYPE_READ,
1789                   qdio->adapter->pool.erp_req);
1790 
1791     if (IS_ERR(req)) {
1792         retval = PTR_ERR(req);
1793         goto out;
1794     }
1795 
1796     req->status |= ZFCP_STATUS_FSFREQ_CLEANUP;
1797     zfcp_qdio_set_sbale_last(qdio, &req->qdio_req);
1798 
1799     req->handler = zfcp_fsf_open_port_handler;
1800     hton24(req->qtcb->bottom.support.d_id, port->d_id);
1801     req->data = port;
1802     req->erp_action = erp_action;
1803     erp_action->fsf_req_id = req->req_id;
1804     get_device(&port->dev);
1805 
1806     zfcp_fsf_start_erp_timer(req);
1807     retval = zfcp_fsf_req_send(req);
1808     if (retval) {
1809         zfcp_fsf_req_free(req);
1810         erp_action->fsf_req_id = 0;
1811         put_device(&port->dev);
1812     }
1813     /* NOTE: DO NOT TOUCH req PAST THIS POINT! */
1814 out:
1815     spin_unlock_irq(&qdio->req_q_lock);
1816     return retval;
1817 }
1818 
1819 static void zfcp_fsf_close_port_handler(struct zfcp_fsf_req *req)
1820 {
1821     struct zfcp_port *port = req->data;
1822 
1823     if (req->status & ZFCP_STATUS_FSFREQ_ERROR)
1824         return;
1825 
1826     switch (req->qtcb->header.fsf_status) {
1827     case FSF_PORT_HANDLE_NOT_VALID:
1828         zfcp_erp_adapter_reopen(port->adapter, 0, "fscph_1");
1829         req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1830         break;
1831     case FSF_ADAPTER_STATUS_AVAILABLE:
1832         break;
1833     case FSF_GOOD:
1834         zfcp_erp_clear_port_status(port, ZFCP_STATUS_COMMON_OPEN);
1835         break;
1836     }
1837 }
1838 
1839 /**
1840  * zfcp_fsf_close_port - create and send close port request
1841  * @erp_action: pointer to struct zfcp_erp_action
1842  * Returns: 0 on success, error otherwise
1843  */
1844 int zfcp_fsf_close_port(struct zfcp_erp_action *erp_action)
1845 {
1846     struct zfcp_qdio *qdio = erp_action->adapter->qdio;
1847     struct zfcp_fsf_req *req;
1848     int retval = -EIO;
1849 
1850     spin_lock_irq(&qdio->req_q_lock);
1851     if (zfcp_qdio_sbal_get(qdio))
1852         goto out;
1853 
1854     req = zfcp_fsf_req_create(qdio, FSF_QTCB_CLOSE_PORT,
1855                   SBAL_SFLAGS0_TYPE_READ,
1856                   qdio->adapter->pool.erp_req);
1857 
1858     if (IS_ERR(req)) {
1859         retval = PTR_ERR(req);
1860         goto out;
1861     }
1862 
1863     req->status |= ZFCP_STATUS_FSFREQ_CLEANUP;
1864     zfcp_qdio_set_sbale_last(qdio, &req->qdio_req);
1865 
1866     req->handler = zfcp_fsf_close_port_handler;
1867     req->data = erp_action->port;
1868     req->erp_action = erp_action;
1869     req->qtcb->header.port_handle = erp_action->port->handle;
1870     erp_action->fsf_req_id = req->req_id;
1871 
1872     zfcp_fsf_start_erp_timer(req);
1873     retval = zfcp_fsf_req_send(req);
1874     if (retval) {
1875         zfcp_fsf_req_free(req);
1876         erp_action->fsf_req_id = 0;
1877     }
1878     /* NOTE: DO NOT TOUCH req PAST THIS POINT! */
1879 out:
1880     spin_unlock_irq(&qdio->req_q_lock);
1881     return retval;
1882 }
1883 
1884 static void zfcp_fsf_open_wka_port_handler(struct zfcp_fsf_req *req)
1885 {
1886     struct zfcp_fc_wka_port *wka_port = req->data;
1887     struct fsf_qtcb_header *header = &req->qtcb->header;
1888 
1889     if (req->status & ZFCP_STATUS_FSFREQ_ERROR) {
1890         wka_port->status = ZFCP_FC_WKA_PORT_OFFLINE;
1891         goto out;
1892     }
1893 
1894     switch (header->fsf_status) {
1895     case FSF_MAXIMUM_NUMBER_OF_PORTS_EXCEEDED:
1896         dev_warn(&req->adapter->ccw_device->dev,
1897              "Opening WKA port 0x%x failed\n", wka_port->d_id);
1898         fallthrough;
1899     case FSF_ADAPTER_STATUS_AVAILABLE:
1900         req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1901         wka_port->status = ZFCP_FC_WKA_PORT_OFFLINE;
1902         break;
1903     case FSF_GOOD:
1904         wka_port->handle = header->port_handle;
1905         fallthrough;
1906     case FSF_PORT_ALREADY_OPEN:
1907         wka_port->status = ZFCP_FC_WKA_PORT_ONLINE;
1908     }
1909 out:
1910     wake_up(&wka_port->opened);
1911 }
1912 
1913 /**
1914  * zfcp_fsf_open_wka_port - create and send open wka-port request
1915  * @wka_port: pointer to struct zfcp_fc_wka_port
1916  * Returns: 0 on success, error otherwise
1917  */
1918 int zfcp_fsf_open_wka_port(struct zfcp_fc_wka_port *wka_port)
1919 {
1920     struct zfcp_qdio *qdio = wka_port->adapter->qdio;
1921     struct zfcp_fsf_req *req;
1922     unsigned long req_id = 0;
1923     int retval = -EIO;
1924 
1925     spin_lock_irq(&qdio->req_q_lock);
1926     if (zfcp_qdio_sbal_get(qdio))
1927         goto out;
1928 
1929     req = zfcp_fsf_req_create(qdio, FSF_QTCB_OPEN_PORT_WITH_DID,
1930                   SBAL_SFLAGS0_TYPE_READ,
1931                   qdio->adapter->pool.erp_req);
1932 
1933     if (IS_ERR(req)) {
1934         retval = PTR_ERR(req);
1935         goto out;
1936     }
1937 
1938     req->status |= ZFCP_STATUS_FSFREQ_CLEANUP;
1939     zfcp_qdio_set_sbale_last(qdio, &req->qdio_req);
1940 
1941     req->handler = zfcp_fsf_open_wka_port_handler;
1942     hton24(req->qtcb->bottom.support.d_id, wka_port->d_id);
1943     req->data = wka_port;
1944 
1945     req_id = req->req_id;
1946 
1947     zfcp_fsf_start_timer(req, ZFCP_FSF_REQUEST_TIMEOUT);
1948     retval = zfcp_fsf_req_send(req);
1949     if (retval)
1950         zfcp_fsf_req_free(req);
1951     /* NOTE: DO NOT TOUCH req PAST THIS POINT! */
1952 out:
1953     spin_unlock_irq(&qdio->req_q_lock);
1954     if (!retval)
1955         zfcp_dbf_rec_run_wka("fsowp_1", wka_port, req_id);
1956     return retval;
1957 }
1958 
1959 static void zfcp_fsf_close_wka_port_handler(struct zfcp_fsf_req *req)
1960 {
1961     struct zfcp_fc_wka_port *wka_port = req->data;
1962 
1963     if (req->qtcb->header.fsf_status == FSF_PORT_HANDLE_NOT_VALID) {
1964         req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1965         zfcp_erp_adapter_reopen(wka_port->adapter, 0, "fscwph1");
1966     }
1967 
1968     wka_port->status = ZFCP_FC_WKA_PORT_OFFLINE;
1969     wake_up(&wka_port->closed);
1970 }
1971 
1972 /**
1973  * zfcp_fsf_close_wka_port - create and send close wka port request
1974  * @wka_port: WKA port to open
1975  * Returns: 0 on success, error otherwise
1976  */
1977 int zfcp_fsf_close_wka_port(struct zfcp_fc_wka_port *wka_port)
1978 {
1979     struct zfcp_qdio *qdio = wka_port->adapter->qdio;
1980     struct zfcp_fsf_req *req;
1981     unsigned long req_id = 0;
1982     int retval = -EIO;
1983 
1984     spin_lock_irq(&qdio->req_q_lock);
1985     if (zfcp_qdio_sbal_get(qdio))
1986         goto out;
1987 
1988     req = zfcp_fsf_req_create(qdio, FSF_QTCB_CLOSE_PORT,
1989                   SBAL_SFLAGS0_TYPE_READ,
1990                   qdio->adapter->pool.erp_req);
1991 
1992     if (IS_ERR(req)) {
1993         retval = PTR_ERR(req);
1994         goto out;
1995     }
1996 
1997     req->status |= ZFCP_STATUS_FSFREQ_CLEANUP;
1998     zfcp_qdio_set_sbale_last(qdio, &req->qdio_req);
1999 
2000     req->handler = zfcp_fsf_close_wka_port_handler;
2001     req->data = wka_port;
2002     req->qtcb->header.port_handle = wka_port->handle;
2003 
2004     req_id = req->req_id;
2005 
2006     zfcp_fsf_start_timer(req, ZFCP_FSF_REQUEST_TIMEOUT);
2007     retval = zfcp_fsf_req_send(req);
2008     if (retval)
2009         zfcp_fsf_req_free(req);
2010     /* NOTE: DO NOT TOUCH req PAST THIS POINT! */
2011 out:
2012     spin_unlock_irq(&qdio->req_q_lock);
2013     if (!retval)
2014         zfcp_dbf_rec_run_wka("fscwp_1", wka_port, req_id);
2015     return retval;
2016 }
2017 
2018 static void zfcp_fsf_close_physical_port_handler(struct zfcp_fsf_req *req)
2019 {
2020     struct zfcp_port *port = req->data;
2021     struct fsf_qtcb_header *header = &req->qtcb->header;
2022     struct scsi_device *sdev;
2023 
2024     if (req->status & ZFCP_STATUS_FSFREQ_ERROR)
2025         return;
2026 
2027     switch (header->fsf_status) {
2028     case FSF_PORT_HANDLE_NOT_VALID:
2029         zfcp_erp_adapter_reopen(port->adapter, 0, "fscpph1");
2030         req->status |= ZFCP_STATUS_FSFREQ_ERROR;
2031         break;
2032     case FSF_PORT_BOXED:
2033         /* can't use generic zfcp_erp_modify_port_status because
2034          * ZFCP_STATUS_COMMON_OPEN must not be reset for the port */
2035         atomic_andnot(ZFCP_STATUS_PORT_PHYS_OPEN, &port->status);
2036         shost_for_each_device(sdev, port->adapter->scsi_host)
2037             if (sdev_to_zfcp(sdev)->port == port)
2038                 atomic_andnot(ZFCP_STATUS_COMMON_OPEN,
2039                           &sdev_to_zfcp(sdev)->status);
2040         zfcp_erp_set_port_status(port, ZFCP_STATUS_COMMON_ACCESS_BOXED);
2041         zfcp_erp_port_reopen(port, ZFCP_STATUS_COMMON_ERP_FAILED,
2042                      "fscpph2");
2043         req->status |= ZFCP_STATUS_FSFREQ_ERROR;
2044         break;
2045     case FSF_ADAPTER_STATUS_AVAILABLE:
2046         switch (header->fsf_status_qual.word[0]) {
2047         case FSF_SQ_INVOKE_LINK_TEST_PROCEDURE:
2048         case FSF_SQ_ULP_DEPENDENT_ERP_REQUIRED:
2049             req->status |= ZFCP_STATUS_FSFREQ_ERROR;
2050             break;
2051         }
2052         break;
2053     case FSF_GOOD:
2054         /* can't use generic zfcp_erp_modify_port_status because
2055          * ZFCP_STATUS_COMMON_OPEN must not be reset for the port
2056          */
2057         atomic_andnot(ZFCP_STATUS_PORT_PHYS_OPEN, &port->status);
2058         shost_for_each_device(sdev, port->adapter->scsi_host)
2059             if (sdev_to_zfcp(sdev)->port == port)
2060                 atomic_andnot(ZFCP_STATUS_COMMON_OPEN,
2061                           &sdev_to_zfcp(sdev)->status);
2062         break;
2063     }
2064 }
2065 
2066 /**
2067  * zfcp_fsf_close_physical_port - close physical port
2068  * @erp_action: pointer to struct zfcp_erp_action
2069  * Returns: 0 on success
2070  */
2071 int zfcp_fsf_close_physical_port(struct zfcp_erp_action *erp_action)
2072 {
2073     struct zfcp_qdio *qdio = erp_action->adapter->qdio;
2074     struct zfcp_fsf_req *req;
2075     int retval = -EIO;
2076 
2077     spin_lock_irq(&qdio->req_q_lock);
2078     if (zfcp_qdio_sbal_get(qdio))
2079         goto out;
2080 
2081     req = zfcp_fsf_req_create(qdio, FSF_QTCB_CLOSE_PHYSICAL_PORT,
2082                   SBAL_SFLAGS0_TYPE_READ,
2083                   qdio->adapter->pool.erp_req);
2084 
2085     if (IS_ERR(req)) {
2086         retval = PTR_ERR(req);
2087         goto out;
2088     }
2089 
2090     req->status |= ZFCP_STATUS_FSFREQ_CLEANUP;
2091     zfcp_qdio_set_sbale_last(qdio, &req->qdio_req);
2092 
2093     req->data = erp_action->port;
2094     req->qtcb->header.port_handle = erp_action->port->handle;
2095     req->erp_action = erp_action;
2096     req->handler = zfcp_fsf_close_physical_port_handler;
2097     erp_action->fsf_req_id = req->req_id;
2098 
2099     zfcp_fsf_start_erp_timer(req);
2100     retval = zfcp_fsf_req_send(req);
2101     if (retval) {
2102         zfcp_fsf_req_free(req);
2103         erp_action->fsf_req_id = 0;
2104     }
2105     /* NOTE: DO NOT TOUCH req PAST THIS POINT! */
2106 out:
2107     spin_unlock_irq(&qdio->req_q_lock);
2108     return retval;
2109 }
2110 
2111 static void zfcp_fsf_open_lun_handler(struct zfcp_fsf_req *req)
2112 {
2113     struct zfcp_adapter *adapter = req->adapter;
2114     struct scsi_device *sdev = req->data;
2115     struct zfcp_scsi_dev *zfcp_sdev;
2116     struct fsf_qtcb_header *header = &req->qtcb->header;
2117     union fsf_status_qual *qual = &header->fsf_status_qual;
2118 
2119     if (req->status & ZFCP_STATUS_FSFREQ_ERROR)
2120         return;
2121 
2122     zfcp_sdev = sdev_to_zfcp(sdev);
2123 
2124     atomic_andnot(ZFCP_STATUS_COMMON_ACCESS_DENIED |
2125               ZFCP_STATUS_COMMON_ACCESS_BOXED,
2126               &zfcp_sdev->status);
2127 
2128     switch (header->fsf_status) {
2129 
2130     case FSF_PORT_HANDLE_NOT_VALID:
2131         zfcp_erp_adapter_reopen(adapter, 0, "fsouh_1");
2132         fallthrough;
2133     case FSF_LUN_ALREADY_OPEN:
2134         break;
2135     case FSF_PORT_BOXED:
2136         zfcp_erp_set_port_status(zfcp_sdev->port,
2137                      ZFCP_STATUS_COMMON_ACCESS_BOXED);
2138         zfcp_erp_port_reopen(zfcp_sdev->port,
2139                      ZFCP_STATUS_COMMON_ERP_FAILED, "fsouh_2");
2140         req->status |= ZFCP_STATUS_FSFREQ_ERROR;
2141         break;
2142     case FSF_LUN_SHARING_VIOLATION:
2143         if (qual->word[0])
2144             dev_warn(&zfcp_sdev->port->adapter->ccw_device->dev,
2145                  "LUN 0x%016Lx on port 0x%016Lx is already in "
2146                  "use by CSS%d, MIF Image ID %x\n",
2147                  zfcp_scsi_dev_lun(sdev),
2148                  (unsigned long long)zfcp_sdev->port->wwpn,
2149                  qual->fsf_queue_designator.cssid,
2150                  qual->fsf_queue_designator.hla);
2151         zfcp_erp_set_lun_status(sdev,
2152                     ZFCP_STATUS_COMMON_ERP_FAILED |
2153                     ZFCP_STATUS_COMMON_ACCESS_DENIED);
2154         req->status |= ZFCP_STATUS_FSFREQ_ERROR;
2155         break;
2156     case FSF_MAXIMUM_NUMBER_OF_LUNS_EXCEEDED:
2157         dev_warn(&adapter->ccw_device->dev,
2158              "No handle is available for LUN "
2159              "0x%016Lx on port 0x%016Lx\n",
2160              (unsigned long long)zfcp_scsi_dev_lun(sdev),
2161              (unsigned long long)zfcp_sdev->port->wwpn);
2162         zfcp_erp_set_lun_status(sdev, ZFCP_STATUS_COMMON_ERP_FAILED);
2163         fallthrough;
2164     case FSF_INVALID_COMMAND_OPTION:
2165         req->status |= ZFCP_STATUS_FSFREQ_ERROR;
2166         break;
2167     case FSF_ADAPTER_STATUS_AVAILABLE:
2168         switch (header->fsf_status_qual.word[0]) {
2169         case FSF_SQ_INVOKE_LINK_TEST_PROCEDURE:
2170             zfcp_fc_test_link(zfcp_sdev->port);
2171             fallthrough;
2172         case FSF_SQ_ULP_DEPENDENT_ERP_REQUIRED:
2173             req->status |= ZFCP_STATUS_FSFREQ_ERROR;
2174             break;
2175         }
2176         break;
2177 
2178     case FSF_GOOD:
2179         zfcp_sdev->lun_handle = header->lun_handle;
2180         atomic_or(ZFCP_STATUS_COMMON_OPEN, &zfcp_sdev->status);
2181         break;
2182     }
2183 }
2184 
2185 /**
2186  * zfcp_fsf_open_lun - open LUN
2187  * @erp_action: pointer to struct zfcp_erp_action
2188  * Returns: 0 on success, error otherwise
2189  */
2190 int zfcp_fsf_open_lun(struct zfcp_erp_action *erp_action)
2191 {
2192     struct zfcp_adapter *adapter = erp_action->adapter;
2193     struct zfcp_qdio *qdio = adapter->qdio;
2194     struct zfcp_fsf_req *req;
2195     int retval = -EIO;
2196 
2197     spin_lock_irq(&qdio->req_q_lock);
2198     if (zfcp_qdio_sbal_get(qdio))
2199         goto out;
2200 
2201     req = zfcp_fsf_req_create(qdio, FSF_QTCB_OPEN_LUN,
2202                   SBAL_SFLAGS0_TYPE_READ,
2203                   adapter->pool.erp_req);
2204 
2205     if (IS_ERR(req)) {
2206         retval = PTR_ERR(req);
2207         goto out;
2208     }
2209 
2210     req->status |= ZFCP_STATUS_FSFREQ_CLEANUP;
2211     zfcp_qdio_set_sbale_last(qdio, &req->qdio_req);
2212 
2213     req->qtcb->header.port_handle = erp_action->port->handle;
2214     req->qtcb->bottom.support.fcp_lun = zfcp_scsi_dev_lun(erp_action->sdev);
2215     req->handler = zfcp_fsf_open_lun_handler;
2216     req->data = erp_action->sdev;
2217     req->erp_action = erp_action;
2218     erp_action->fsf_req_id = req->req_id;
2219 
2220     if (!(adapter->connection_features & FSF_FEATURE_NPIV_MODE))
2221         req->qtcb->bottom.support.option = FSF_OPEN_LUN_SUPPRESS_BOXING;
2222 
2223     zfcp_fsf_start_erp_timer(req);
2224     retval = zfcp_fsf_req_send(req);
2225     if (retval) {
2226         zfcp_fsf_req_free(req);
2227         erp_action->fsf_req_id = 0;
2228     }
2229     /* NOTE: DO NOT TOUCH req PAST THIS POINT! */
2230 out:
2231     spin_unlock_irq(&qdio->req_q_lock);
2232     return retval;
2233 }
2234 
2235 static void zfcp_fsf_close_lun_handler(struct zfcp_fsf_req *req)
2236 {
2237     struct scsi_device *sdev = req->data;
2238     struct zfcp_scsi_dev *zfcp_sdev;
2239 
2240     if (req->status & ZFCP_STATUS_FSFREQ_ERROR)
2241         return;
2242 
2243     zfcp_sdev = sdev_to_zfcp(sdev);
2244 
2245     switch (req->qtcb->header.fsf_status) {
2246     case FSF_PORT_HANDLE_NOT_VALID:
2247         zfcp_erp_adapter_reopen(zfcp_sdev->port->adapter, 0, "fscuh_1");
2248         req->status |= ZFCP_STATUS_FSFREQ_ERROR;
2249         break;
2250     case FSF_LUN_HANDLE_NOT_VALID:
2251         zfcp_erp_port_reopen(zfcp_sdev->port, 0, "fscuh_2");
2252         req->status |= ZFCP_STATUS_FSFREQ_ERROR;
2253         break;
2254     case FSF_PORT_BOXED:
2255         zfcp_erp_set_port_status(zfcp_sdev->port,
2256                      ZFCP_STATUS_COMMON_ACCESS_BOXED);
2257         zfcp_erp_port_reopen(zfcp_sdev->port,
2258                      ZFCP_STATUS_COMMON_ERP_FAILED, "fscuh_3");
2259         req->status |= ZFCP_STATUS_FSFREQ_ERROR;
2260         break;
2261     case FSF_ADAPTER_STATUS_AVAILABLE:
2262         switch (req->qtcb->header.fsf_status_qual.word[0]) {
2263         case FSF_SQ_INVOKE_LINK_TEST_PROCEDURE:
2264             zfcp_fc_test_link(zfcp_sdev->port);
2265             fallthrough;
2266         case FSF_SQ_ULP_DEPENDENT_ERP_REQUIRED:
2267             req->status |= ZFCP_STATUS_FSFREQ_ERROR;
2268             break;
2269         }
2270         break;
2271     case FSF_GOOD:
2272         atomic_andnot(ZFCP_STATUS_COMMON_OPEN, &zfcp_sdev->status);
2273         break;
2274     }
2275 }
2276 
2277 /**
2278  * zfcp_fsf_close_lun - close LUN
2279  * @erp_action: pointer to erp_action triggering the "close LUN"
2280  * Returns: 0 on success, error otherwise
2281  */
2282 int zfcp_fsf_close_lun(struct zfcp_erp_action *erp_action)
2283 {
2284     struct zfcp_qdio *qdio = erp_action->adapter->qdio;
2285     struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(erp_action->sdev);
2286     struct zfcp_fsf_req *req;
2287     int retval = -EIO;
2288 
2289     spin_lock_irq(&qdio->req_q_lock);
2290     if (zfcp_qdio_sbal_get(qdio))
2291         goto out;
2292 
2293     req = zfcp_fsf_req_create(qdio, FSF_QTCB_CLOSE_LUN,
2294                   SBAL_SFLAGS0_TYPE_READ,
2295                   qdio->adapter->pool.erp_req);
2296 
2297     if (IS_ERR(req)) {
2298         retval = PTR_ERR(req);
2299         goto out;
2300     }
2301 
2302     req->status |= ZFCP_STATUS_FSFREQ_CLEANUP;
2303     zfcp_qdio_set_sbale_last(qdio, &req->qdio_req);
2304 
2305     req->qtcb->header.port_handle = erp_action->port->handle;
2306     req->qtcb->header.lun_handle = zfcp_sdev->lun_handle;
2307     req->handler = zfcp_fsf_close_lun_handler;
2308     req->data = erp_action->sdev;
2309     req->erp_action = erp_action;
2310     erp_action->fsf_req_id = req->req_id;
2311 
2312     zfcp_fsf_start_erp_timer(req);
2313     retval = zfcp_fsf_req_send(req);
2314     if (retval) {
2315         zfcp_fsf_req_free(req);
2316         erp_action->fsf_req_id = 0;
2317     }
2318     /* NOTE: DO NOT TOUCH req PAST THIS POINT! */
2319 out:
2320     spin_unlock_irq(&qdio->req_q_lock);
2321     return retval;
2322 }
2323 
2324 static void zfcp_fsf_update_lat(struct zfcp_latency_record *lat_rec, u32 lat)
2325 {
2326     lat_rec->sum += lat;
2327     lat_rec->min = min(lat_rec->min, lat);
2328     lat_rec->max = max(lat_rec->max, lat);
2329 }
2330 
2331 static void zfcp_fsf_req_trace(struct zfcp_fsf_req *req, struct scsi_cmnd *scsi)
2332 {
2333     struct fsf_qual_latency_info *lat_in;
2334     struct zfcp_latency_cont *lat = NULL;
2335     struct zfcp_scsi_dev *zfcp_sdev;
2336     struct zfcp_blk_drv_data blktrc;
2337     int ticks = req->adapter->timer_ticks;
2338 
2339     lat_in = &req->qtcb->prefix.prot_status_qual.latency_info;
2340 
2341     blktrc.flags = 0;
2342     blktrc.magic = ZFCP_BLK_DRV_DATA_MAGIC;
2343     if (req->status & ZFCP_STATUS_FSFREQ_ERROR)
2344         blktrc.flags |= ZFCP_BLK_REQ_ERROR;
2345     blktrc.inb_usage = 0;
2346     blktrc.outb_usage = req->qdio_req.qdio_outb_usage;
2347 
2348     if (req->adapter->adapter_features & FSF_FEATURE_MEASUREMENT_DATA &&
2349         !(req->status & ZFCP_STATUS_FSFREQ_ERROR)) {
2350         zfcp_sdev = sdev_to_zfcp(scsi->device);
2351         blktrc.flags |= ZFCP_BLK_LAT_VALID;
2352         blktrc.channel_lat = lat_in->channel_lat * ticks;
2353         blktrc.fabric_lat = lat_in->fabric_lat * ticks;
2354 
2355         switch (req->qtcb->bottom.io.data_direction) {
2356         case FSF_DATADIR_DIF_READ_STRIP:
2357         case FSF_DATADIR_DIF_READ_CONVERT:
2358         case FSF_DATADIR_READ:
2359             lat = &zfcp_sdev->latencies.read;
2360             break;
2361         case FSF_DATADIR_DIF_WRITE_INSERT:
2362         case FSF_DATADIR_DIF_WRITE_CONVERT:
2363         case FSF_DATADIR_WRITE:
2364             lat = &zfcp_sdev->latencies.write;
2365             break;
2366         case FSF_DATADIR_CMND:
2367             lat = &zfcp_sdev->latencies.cmd;
2368             break;
2369         }
2370 
2371         if (lat) {
2372             spin_lock(&zfcp_sdev->latencies.lock);
2373             zfcp_fsf_update_lat(&lat->channel, lat_in->channel_lat);
2374             zfcp_fsf_update_lat(&lat->fabric, lat_in->fabric_lat);
2375             lat->counter++;
2376             spin_unlock(&zfcp_sdev->latencies.lock);
2377         }
2378     }
2379 
2380     blk_add_driver_data(scsi_cmd_to_rq(scsi), &blktrc, sizeof(blktrc));
2381 }
2382 
2383 /**
2384  * zfcp_fsf_fcp_handler_common() - FCP response handler common to I/O and TMF.
2385  * @req: Pointer to FSF request.
2386  * @sdev: Pointer to SCSI device as request context.
2387  */
2388 static void zfcp_fsf_fcp_handler_common(struct zfcp_fsf_req *req,
2389                     struct scsi_device *sdev)
2390 {
2391     struct zfcp_scsi_dev *zfcp_sdev;
2392     struct fsf_qtcb_header *header = &req->qtcb->header;
2393 
2394     if (unlikely(req->status & ZFCP_STATUS_FSFREQ_ERROR))
2395         return;
2396 
2397     zfcp_sdev = sdev_to_zfcp(sdev);
2398 
2399     switch (header->fsf_status) {
2400     case FSF_HANDLE_MISMATCH:
2401     case FSF_PORT_HANDLE_NOT_VALID:
2402         zfcp_erp_adapter_reopen(req->adapter, 0, "fssfch1");
2403         req->status |= ZFCP_STATUS_FSFREQ_ERROR;
2404         break;
2405     case FSF_FCPLUN_NOT_VALID:
2406     case FSF_LUN_HANDLE_NOT_VALID:
2407         zfcp_erp_port_reopen(zfcp_sdev->port, 0, "fssfch2");
2408         req->status |= ZFCP_STATUS_FSFREQ_ERROR;
2409         break;
2410     case FSF_SERVICE_CLASS_NOT_SUPPORTED:
2411         zfcp_fsf_class_not_supp(req);
2412         break;
2413     case FSF_DIRECTION_INDICATOR_NOT_VALID:
2414         dev_err(&req->adapter->ccw_device->dev,
2415             "Incorrect direction %d, LUN 0x%016Lx on port "
2416             "0x%016Lx closed\n",
2417             req->qtcb->bottom.io.data_direction,
2418             (unsigned long long)zfcp_scsi_dev_lun(sdev),
2419             (unsigned long long)zfcp_sdev->port->wwpn);
2420         zfcp_erp_adapter_shutdown(req->adapter, 0, "fssfch3");
2421         req->status |= ZFCP_STATUS_FSFREQ_ERROR;
2422         break;
2423     case FSF_CMND_LENGTH_NOT_VALID:
2424         dev_err(&req->adapter->ccw_device->dev,
2425             "Incorrect FCP_CMND length %d, FCP device closed\n",
2426             req->qtcb->bottom.io.fcp_cmnd_length);
2427         zfcp_erp_adapter_shutdown(req->adapter, 0, "fssfch4");
2428         req->status |= ZFCP_STATUS_FSFREQ_ERROR;
2429         break;
2430     case FSF_PORT_BOXED:
2431         zfcp_erp_set_port_status(zfcp_sdev->port,
2432                      ZFCP_STATUS_COMMON_ACCESS_BOXED);
2433         zfcp_erp_port_reopen(zfcp_sdev->port,
2434                      ZFCP_STATUS_COMMON_ERP_FAILED, "fssfch5");
2435         req->status |= ZFCP_STATUS_FSFREQ_ERROR;
2436         break;
2437     case FSF_LUN_BOXED:
2438         zfcp_erp_set_lun_status(sdev, ZFCP_STATUS_COMMON_ACCESS_BOXED);
2439         zfcp_erp_lun_reopen(sdev, ZFCP_STATUS_COMMON_ERP_FAILED,
2440                     "fssfch6");
2441         req->status |= ZFCP_STATUS_FSFREQ_ERROR;
2442         break;
2443     case FSF_ADAPTER_STATUS_AVAILABLE:
2444         if (header->fsf_status_qual.word[0] ==
2445             FSF_SQ_INVOKE_LINK_TEST_PROCEDURE)
2446             zfcp_fc_test_link(zfcp_sdev->port);
2447         req->status |= ZFCP_STATUS_FSFREQ_ERROR;
2448         break;
2449     case FSF_SECURITY_ERROR:
2450         zfcp_fsf_log_security_error(&req->adapter->ccw_device->dev,
2451                         header->fsf_status_qual.word[0],
2452                         zfcp_sdev->port->wwpn);
2453         zfcp_erp_port_forced_reopen(zfcp_sdev->port, 0, "fssfch7");
2454         req->status |= ZFCP_STATUS_FSFREQ_ERROR;
2455         break;
2456     }
2457 }
2458 
2459 static void zfcp_fsf_fcp_cmnd_handler(struct zfcp_fsf_req *req)
2460 {
2461     struct scsi_cmnd *scpnt;
2462     struct fcp_resp_with_ext *fcp_rsp;
2463     unsigned long flags;
2464 
2465     read_lock_irqsave(&req->adapter->abort_lock, flags);
2466 
2467     scpnt = req->data;
2468     if (unlikely(!scpnt)) {
2469         read_unlock_irqrestore(&req->adapter->abort_lock, flags);
2470         return;
2471     }
2472 
2473     zfcp_fsf_fcp_handler_common(req, scpnt->device);
2474 
2475     if (unlikely(req->status & ZFCP_STATUS_FSFREQ_ERROR)) {
2476         set_host_byte(scpnt, DID_TRANSPORT_DISRUPTED);
2477         goto skip_fsfstatus;
2478     }
2479 
2480     switch (req->qtcb->header.fsf_status) {
2481     case FSF_INCONSISTENT_PROT_DATA:
2482     case FSF_INVALID_PROT_PARM:
2483         set_host_byte(scpnt, DID_ERROR);
2484         goto skip_fsfstatus;
2485     case FSF_BLOCK_GUARD_CHECK_FAILURE:
2486         zfcp_scsi_dif_sense_error(scpnt, 0x1);
2487         goto skip_fsfstatus;
2488     case FSF_APP_TAG_CHECK_FAILURE:
2489         zfcp_scsi_dif_sense_error(scpnt, 0x2);
2490         goto skip_fsfstatus;
2491     case FSF_REF_TAG_CHECK_FAILURE:
2492         zfcp_scsi_dif_sense_error(scpnt, 0x3);
2493         goto skip_fsfstatus;
2494     }
2495     BUILD_BUG_ON(sizeof(struct fcp_resp_with_ext) > FSF_FCP_RSP_SIZE);
2496     fcp_rsp = &req->qtcb->bottom.io.fcp_rsp.iu;
2497     zfcp_fc_eval_fcp_rsp(fcp_rsp, scpnt);
2498 
2499 skip_fsfstatus:
2500     zfcp_fsf_req_trace(req, scpnt);
2501     zfcp_dbf_scsi_result(scpnt, req);
2502 
2503     scpnt->host_scribble = NULL;
2504     scsi_done(scpnt);
2505     /*
2506      * We must hold this lock until scsi_done has been called.
2507      * Otherwise we may call scsi_done after abort regarding this
2508      * command has completed.
2509      * Note: scsi_done must not block!
2510      */
2511     read_unlock_irqrestore(&req->adapter->abort_lock, flags);
2512 }
2513 
2514 static int zfcp_fsf_set_data_dir(struct scsi_cmnd *scsi_cmnd, u32 *data_dir)
2515 {
2516     switch (scsi_get_prot_op(scsi_cmnd)) {
2517     case SCSI_PROT_NORMAL:
2518         switch (scsi_cmnd->sc_data_direction) {
2519         case DMA_NONE:
2520             *data_dir = FSF_DATADIR_CMND;
2521             break;
2522         case DMA_FROM_DEVICE:
2523             *data_dir = FSF_DATADIR_READ;
2524             break;
2525         case DMA_TO_DEVICE:
2526             *data_dir = FSF_DATADIR_WRITE;
2527             break;
2528         case DMA_BIDIRECTIONAL:
2529             return -EINVAL;
2530         }
2531         break;
2532 
2533     case SCSI_PROT_READ_STRIP:
2534         *data_dir = FSF_DATADIR_DIF_READ_STRIP;
2535         break;
2536     case SCSI_PROT_WRITE_INSERT:
2537         *data_dir = FSF_DATADIR_DIF_WRITE_INSERT;
2538         break;
2539     case SCSI_PROT_READ_PASS:
2540         *data_dir = FSF_DATADIR_DIF_READ_CONVERT;
2541         break;
2542     case SCSI_PROT_WRITE_PASS:
2543         *data_dir = FSF_DATADIR_DIF_WRITE_CONVERT;
2544         break;
2545     default:
2546         return -EINVAL;
2547     }
2548 
2549     return 0;
2550 }
2551 
2552 /**
2553  * zfcp_fsf_fcp_cmnd - initiate an FCP command (for a SCSI command)
2554  * @scsi_cmnd: scsi command to be sent
2555  */
2556 int zfcp_fsf_fcp_cmnd(struct scsi_cmnd *scsi_cmnd)
2557 {
2558     struct zfcp_fsf_req *req;
2559     struct fcp_cmnd *fcp_cmnd;
2560     u8 sbtype = SBAL_SFLAGS0_TYPE_READ;
2561     int retval = -EIO;
2562     struct scsi_device *sdev = scsi_cmnd->device;
2563     struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
2564     struct zfcp_adapter *adapter = zfcp_sdev->port->adapter;
2565     struct zfcp_qdio *qdio = adapter->qdio;
2566     struct fsf_qtcb_bottom_io *io;
2567     unsigned long flags;
2568 
2569     if (unlikely(!(atomic_read(&zfcp_sdev->status) &
2570                ZFCP_STATUS_COMMON_UNBLOCKED)))
2571         return -EBUSY;
2572 
2573     spin_lock_irqsave(&qdio->req_q_lock, flags);
2574     if (atomic_read(&qdio->req_q_free) <= 0) {
2575         atomic_inc(&qdio->req_q_full);
2576         goto out;
2577     }
2578 
2579     if (scsi_cmnd->sc_data_direction == DMA_TO_DEVICE)
2580         sbtype = SBAL_SFLAGS0_TYPE_WRITE;
2581 
2582     req = zfcp_fsf_req_create(qdio, FSF_QTCB_FCP_CMND,
2583                   sbtype, adapter->pool.scsi_req);
2584 
2585     if (IS_ERR(req)) {
2586         retval = PTR_ERR(req);
2587         goto out;
2588     }
2589 
2590     scsi_cmnd->host_scribble = (unsigned char *) req->req_id;
2591 
2592     io = &req->qtcb->bottom.io;
2593     req->status |= ZFCP_STATUS_FSFREQ_CLEANUP;
2594     req->data = scsi_cmnd;
2595     req->handler = zfcp_fsf_fcp_cmnd_handler;
2596     req->qtcb->header.lun_handle = zfcp_sdev->lun_handle;
2597     req->qtcb->header.port_handle = zfcp_sdev->port->handle;
2598     io->service_class = FSF_CLASS_3;
2599     io->fcp_cmnd_length = FCP_CMND_LEN;
2600 
2601     if (scsi_get_prot_op(scsi_cmnd) != SCSI_PROT_NORMAL) {
2602         io->data_block_length = scsi_prot_interval(scsi_cmnd);
2603         io->ref_tag_value = scsi_prot_ref_tag(scsi_cmnd);
2604     }
2605 
2606     if (zfcp_fsf_set_data_dir(scsi_cmnd, &io->data_direction))
2607         goto failed_scsi_cmnd;
2608 
2609     BUILD_BUG_ON(sizeof(struct fcp_cmnd) > FSF_FCP_CMND_SIZE);
2610     fcp_cmnd = &req->qtcb->bottom.io.fcp_cmnd.iu;
2611     zfcp_fc_scsi_to_fcp(fcp_cmnd, scsi_cmnd);
2612 
2613     if ((scsi_get_prot_op(scsi_cmnd) != SCSI_PROT_NORMAL) &&
2614         scsi_prot_sg_count(scsi_cmnd)) {
2615         zfcp_qdio_set_data_div(qdio, &req->qdio_req,
2616                        scsi_prot_sg_count(scsi_cmnd));
2617         retval = zfcp_qdio_sbals_from_sg(qdio, &req->qdio_req,
2618                          scsi_prot_sglist(scsi_cmnd));
2619         if (retval)
2620             goto failed_scsi_cmnd;
2621         io->prot_data_length = zfcp_qdio_real_bytes(
2622                         scsi_prot_sglist(scsi_cmnd));
2623     }
2624 
2625     retval = zfcp_qdio_sbals_from_sg(qdio, &req->qdio_req,
2626                      scsi_sglist(scsi_cmnd));
2627     if (unlikely(retval))
2628         goto failed_scsi_cmnd;
2629 
2630     zfcp_qdio_set_sbale_last(adapter->qdio, &req->qdio_req);
2631     if (zfcp_adapter_multi_buffer_active(adapter))
2632         zfcp_qdio_set_scount(qdio, &req->qdio_req);
2633 
2634     retval = zfcp_fsf_req_send(req);
2635     if (unlikely(retval))
2636         goto failed_scsi_cmnd;
2637     /* NOTE: DO NOT TOUCH req PAST THIS POINT! */
2638 
2639     goto out;
2640 
2641 failed_scsi_cmnd:
2642     zfcp_fsf_req_free(req);
2643     scsi_cmnd->host_scribble = NULL;
2644 out:
2645     spin_unlock_irqrestore(&qdio->req_q_lock, flags);
2646     return retval;
2647 }
2648 
2649 static void zfcp_fsf_fcp_task_mgmt_handler(struct zfcp_fsf_req *req)
2650 {
2651     struct scsi_device *sdev = req->data;
2652     struct fcp_resp_with_ext *fcp_rsp;
2653     struct fcp_resp_rsp_info *rsp_info;
2654 
2655     zfcp_fsf_fcp_handler_common(req, sdev);
2656 
2657     fcp_rsp = &req->qtcb->bottom.io.fcp_rsp.iu;
2658     rsp_info = (struct fcp_resp_rsp_info *) &fcp_rsp[1];
2659 
2660     if ((rsp_info->rsp_code != FCP_TMF_CMPL) ||
2661          (req->status & ZFCP_STATUS_FSFREQ_ERROR))
2662         req->status |= ZFCP_STATUS_FSFREQ_TMFUNCFAILED;
2663 }
2664 
2665 /**
2666  * zfcp_fsf_fcp_task_mgmt() - Send SCSI task management command (TMF).
2667  * @sdev: Pointer to SCSI device to send the task management command to.
2668  * @tm_flags: Unsigned byte for task management flags.
2669  *
2670  * Return: On success pointer to struct zfcp_fsf_req, %NULL otherwise.
2671  */
2672 struct zfcp_fsf_req *zfcp_fsf_fcp_task_mgmt(struct scsi_device *sdev,
2673                         u8 tm_flags)
2674 {
2675     struct zfcp_fsf_req *req = NULL;
2676     struct fcp_cmnd *fcp_cmnd;
2677     struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
2678     struct zfcp_qdio *qdio = zfcp_sdev->port->adapter->qdio;
2679 
2680     if (unlikely(!(atomic_read(&zfcp_sdev->status) &
2681                ZFCP_STATUS_COMMON_UNBLOCKED)))
2682         return NULL;
2683 
2684     spin_lock_irq(&qdio->req_q_lock);
2685     if (zfcp_qdio_sbal_get(qdio))
2686         goto out;
2687 
2688     req = zfcp_fsf_req_create(qdio, FSF_QTCB_FCP_CMND,
2689                   SBAL_SFLAGS0_TYPE_WRITE,
2690                   qdio->adapter->pool.scsi_req);
2691 
2692     if (IS_ERR(req)) {
2693         req = NULL;
2694         goto out;
2695     }
2696 
2697     req->data = sdev;
2698 
2699     req->handler = zfcp_fsf_fcp_task_mgmt_handler;
2700     req->qtcb->header.lun_handle = zfcp_sdev->lun_handle;
2701     req->qtcb->header.port_handle = zfcp_sdev->port->handle;
2702     req->qtcb->bottom.io.data_direction = FSF_DATADIR_CMND;
2703     req->qtcb->bottom.io.service_class = FSF_CLASS_3;
2704     req->qtcb->bottom.io.fcp_cmnd_length = FCP_CMND_LEN;
2705 
2706     zfcp_qdio_set_sbale_last(qdio, &req->qdio_req);
2707 
2708     fcp_cmnd = &req->qtcb->bottom.io.fcp_cmnd.iu;
2709     zfcp_fc_fcp_tm(fcp_cmnd, sdev, tm_flags);
2710 
2711     zfcp_fsf_start_timer(req, ZFCP_FSF_SCSI_ER_TIMEOUT);
2712     if (!zfcp_fsf_req_send(req)) {
2713         /* NOTE: DO NOT TOUCH req, UNTIL IT COMPLETES! */
2714         goto out;
2715     }
2716 
2717     zfcp_fsf_req_free(req);
2718     req = NULL;
2719 out:
2720     spin_unlock_irq(&qdio->req_q_lock);
2721     return req;
2722 }
2723 
2724 /**
2725  * zfcp_fsf_reqid_check - validate req_id contained in SBAL returned by QDIO
2726  * @qdio: pointer to struct zfcp_qdio
2727  * @sbal_idx: response queue index of SBAL to be processed
2728  */
2729 void zfcp_fsf_reqid_check(struct zfcp_qdio *qdio, int sbal_idx)
2730 {
2731     struct zfcp_adapter *adapter = qdio->adapter;
2732     struct qdio_buffer *sbal = qdio->res_q[sbal_idx];
2733     struct qdio_buffer_element *sbale;
2734     struct zfcp_fsf_req *fsf_req;
2735     unsigned long req_id;
2736     int idx;
2737 
2738     for (idx = 0; idx < QDIO_MAX_ELEMENTS_PER_BUFFER; idx++) {
2739 
2740         sbale = &sbal->element[idx];
2741         req_id = sbale->addr;
2742         fsf_req = zfcp_reqlist_find_rm(adapter->req_list, req_id);
2743 
2744         if (!fsf_req) {
2745             /*
2746              * Unknown request means that we have potentially memory
2747              * corruption and must stop the machine immediately.
2748              */
2749             zfcp_qdio_siosl(adapter);
2750             panic("error: unknown req_id (%lx) on adapter %s.\n",
2751                   req_id, dev_name(&adapter->ccw_device->dev));
2752         }
2753 
2754         zfcp_fsf_req_complete(fsf_req);
2755 
2756         if (likely(sbale->eflags & SBAL_EFLAGS_LAST_ENTRY))
2757             break;
2758     }
2759 }