Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * zfcp device driver
0004  *
0005  * Interface to Linux SCSI midlayer.
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/module.h>
0014 #include <linux/types.h>
0015 #include <linux/slab.h>
0016 #include <scsi/fc/fc_fcp.h>
0017 #include <scsi/scsi_eh.h>
0018 #include <linux/atomic.h>
0019 #include "zfcp_ext.h"
0020 #include "zfcp_dbf.h"
0021 #include "zfcp_fc.h"
0022 #include "zfcp_reqlist.h"
0023 
0024 static unsigned int default_depth = 32;
0025 module_param_named(queue_depth, default_depth, uint, 0600);
0026 MODULE_PARM_DESC(queue_depth, "Default queue depth for new SCSI devices");
0027 
0028 static bool enable_dif;
0029 module_param_named(dif, enable_dif, bool, 0400);
0030 MODULE_PARM_DESC(dif, "Enable DIF data integrity support (default off)");
0031 
0032 bool zfcp_experimental_dix;
0033 module_param_named(dix, zfcp_experimental_dix, bool, 0400);
0034 MODULE_PARM_DESC(dix, "Enable experimental DIX (data integrity extension) support which implies DIF support (default off)");
0035 
0036 static bool allow_lun_scan = true;
0037 module_param(allow_lun_scan, bool, 0600);
0038 MODULE_PARM_DESC(allow_lun_scan, "For NPIV, scan and attach all storage LUNs");
0039 
0040 static void zfcp_scsi_slave_destroy(struct scsi_device *sdev)
0041 {
0042     struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
0043 
0044     /* if previous slave_alloc returned early, there is nothing to do */
0045     if (!zfcp_sdev->port)
0046         return;
0047 
0048     zfcp_erp_lun_shutdown_wait(sdev, "scssd_1");
0049     put_device(&zfcp_sdev->port->dev);
0050 }
0051 
0052 static int zfcp_scsi_slave_configure(struct scsi_device *sdp)
0053 {
0054     if (sdp->tagged_supported)
0055         scsi_change_queue_depth(sdp, default_depth);
0056     return 0;
0057 }
0058 
0059 static void zfcp_scsi_command_fail(struct scsi_cmnd *scpnt, int result)
0060 {
0061     set_host_byte(scpnt, result);
0062     zfcp_dbf_scsi_fail_send(scpnt);
0063     scsi_done(scpnt);
0064 }
0065 
0066 static
0067 int zfcp_scsi_queuecommand(struct Scsi_Host *shost, struct scsi_cmnd *scpnt)
0068 {
0069     struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(scpnt->device);
0070     struct fc_rport *rport = starget_to_rport(scsi_target(scpnt->device));
0071     int    status, scsi_result, ret;
0072 
0073     /* reset the status for this request */
0074     scpnt->result = 0;
0075     scpnt->host_scribble = NULL;
0076 
0077     scsi_result = fc_remote_port_chkready(rport);
0078     if (unlikely(scsi_result)) {
0079         scpnt->result = scsi_result;
0080         zfcp_dbf_scsi_fail_send(scpnt);
0081         scsi_done(scpnt);
0082         return 0;
0083     }
0084 
0085     status = atomic_read(&zfcp_sdev->status);
0086     if (unlikely(status & ZFCP_STATUS_COMMON_ERP_FAILED) &&
0087              !(atomic_read(&zfcp_sdev->port->status) &
0088                ZFCP_STATUS_COMMON_ERP_FAILED)) {
0089         /* only LUN access denied, but port is good
0090          * not covered by FC transport, have to fail here */
0091         zfcp_scsi_command_fail(scpnt, DID_ERROR);
0092         return 0;
0093     }
0094 
0095     if (unlikely(!(status & ZFCP_STATUS_COMMON_UNBLOCKED))) {
0096         /* This could be
0097          * call to rport_delete pending: mimic retry from
0098          *  fc_remote_port_chkready until rport is BLOCKED
0099          */
0100         zfcp_scsi_command_fail(scpnt, DID_IMM_RETRY);
0101         return 0;
0102     }
0103 
0104     ret = zfcp_fsf_fcp_cmnd(scpnt);
0105     if (unlikely(ret == -EBUSY))
0106         return SCSI_MLQUEUE_DEVICE_BUSY;
0107     else if (unlikely(ret < 0))
0108         return SCSI_MLQUEUE_HOST_BUSY;
0109 
0110     return ret;
0111 }
0112 
0113 static int zfcp_scsi_slave_alloc(struct scsi_device *sdev)
0114 {
0115     struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
0116     struct zfcp_adapter *adapter =
0117         (struct zfcp_adapter *) sdev->host->hostdata[0];
0118     struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
0119     struct zfcp_port *port;
0120     struct zfcp_unit *unit;
0121     int npiv = adapter->connection_features & FSF_FEATURE_NPIV_MODE;
0122 
0123     zfcp_sdev->erp_action.adapter = adapter;
0124     zfcp_sdev->erp_action.sdev = sdev;
0125 
0126     port = zfcp_get_port_by_wwpn(adapter, rport->port_name);
0127     if (!port)
0128         return -ENXIO;
0129 
0130     zfcp_sdev->erp_action.port = port;
0131 
0132     mutex_lock(&zfcp_sysfs_port_units_mutex);
0133     if (zfcp_sysfs_port_is_removing(port)) {
0134         /* port is already gone */
0135         mutex_unlock(&zfcp_sysfs_port_units_mutex);
0136         put_device(&port->dev); /* undo zfcp_get_port_by_wwpn() */
0137         return -ENXIO;
0138     }
0139     mutex_unlock(&zfcp_sysfs_port_units_mutex);
0140 
0141     unit = zfcp_unit_find(port, zfcp_scsi_dev_lun(sdev));
0142     if (unit)
0143         put_device(&unit->dev);
0144 
0145     if (!unit && !(allow_lun_scan && npiv)) {
0146         put_device(&port->dev);
0147         return -ENXIO;
0148     }
0149 
0150     zfcp_sdev->port = port;
0151     zfcp_sdev->latencies.write.channel.min = 0xFFFFFFFF;
0152     zfcp_sdev->latencies.write.fabric.min = 0xFFFFFFFF;
0153     zfcp_sdev->latencies.read.channel.min = 0xFFFFFFFF;
0154     zfcp_sdev->latencies.read.fabric.min = 0xFFFFFFFF;
0155     zfcp_sdev->latencies.cmd.channel.min = 0xFFFFFFFF;
0156     zfcp_sdev->latencies.cmd.fabric.min = 0xFFFFFFFF;
0157     spin_lock_init(&zfcp_sdev->latencies.lock);
0158 
0159     zfcp_erp_set_lun_status(sdev, ZFCP_STATUS_COMMON_RUNNING);
0160     zfcp_erp_lun_reopen(sdev, 0, "scsla_1");
0161     zfcp_erp_wait(port->adapter);
0162 
0163     return 0;
0164 }
0165 
0166 static int zfcp_scsi_eh_abort_handler(struct scsi_cmnd *scpnt)
0167 {
0168     struct Scsi_Host *scsi_host = scpnt->device->host;
0169     struct zfcp_adapter *adapter =
0170         (struct zfcp_adapter *) scsi_host->hostdata[0];
0171     struct zfcp_fsf_req *old_req, *abrt_req;
0172     unsigned long flags;
0173     unsigned long old_reqid = (unsigned long) scpnt->host_scribble;
0174     int retval = SUCCESS, ret;
0175     int retry = 3;
0176     char *dbf_tag;
0177 
0178     /* avoid race condition between late normal completion and abort */
0179     write_lock_irqsave(&adapter->abort_lock, flags);
0180 
0181     old_req = zfcp_reqlist_find(adapter->req_list, old_reqid);
0182     if (!old_req) {
0183         write_unlock_irqrestore(&adapter->abort_lock, flags);
0184         zfcp_dbf_scsi_abort("abrt_or", scpnt, NULL);
0185         return FAILED; /* completion could be in progress */
0186     }
0187     old_req->data = NULL;
0188 
0189     /* don't access old fsf_req after releasing the abort_lock */
0190     write_unlock_irqrestore(&adapter->abort_lock, flags);
0191 
0192     while (retry--) {
0193         abrt_req = zfcp_fsf_abort_fcp_cmnd(scpnt);
0194         if (abrt_req)
0195             break;
0196 
0197         zfcp_dbf_scsi_abort("abrt_wt", scpnt, NULL);
0198         zfcp_erp_wait(adapter);
0199         ret = fc_block_scsi_eh(scpnt);
0200         if (ret) {
0201             zfcp_dbf_scsi_abort("abrt_bl", scpnt, NULL);
0202             return ret;
0203         }
0204         if (!(atomic_read(&adapter->status) &
0205               ZFCP_STATUS_COMMON_RUNNING)) {
0206             zfcp_dbf_scsi_abort("abrt_ru", scpnt, NULL);
0207             return SUCCESS;
0208         }
0209     }
0210     if (!abrt_req) {
0211         zfcp_dbf_scsi_abort("abrt_ar", scpnt, NULL);
0212         return FAILED;
0213     }
0214 
0215     wait_for_completion(&abrt_req->completion);
0216 
0217     if (abrt_req->status & ZFCP_STATUS_FSFREQ_ABORTSUCCEEDED)
0218         dbf_tag = "abrt_ok";
0219     else if (abrt_req->status & ZFCP_STATUS_FSFREQ_ABORTNOTNEEDED)
0220         dbf_tag = "abrt_nn";
0221     else {
0222         dbf_tag = "abrt_fa";
0223         retval = FAILED;
0224     }
0225     zfcp_dbf_scsi_abort(dbf_tag, scpnt, abrt_req);
0226     zfcp_fsf_req_free(abrt_req);
0227     return retval;
0228 }
0229 
0230 struct zfcp_scsi_req_filter {
0231     u8 tmf_scope;
0232     u32 lun_handle;
0233     u32 port_handle;
0234 };
0235 
0236 static void zfcp_scsi_forget_cmnd(struct zfcp_fsf_req *old_req, void *data)
0237 {
0238     struct zfcp_scsi_req_filter *filter =
0239         (struct zfcp_scsi_req_filter *)data;
0240 
0241     /* already aborted - prevent side-effects - or not a SCSI command */
0242     if (old_req->data == NULL ||
0243         zfcp_fsf_req_is_status_read_buffer(old_req) ||
0244         old_req->qtcb->header.fsf_command != FSF_QTCB_FCP_CMND)
0245         return;
0246 
0247     /* (tmf_scope == FCP_TMF_TGT_RESET || tmf_scope == FCP_TMF_LUN_RESET) */
0248     if (old_req->qtcb->header.port_handle != filter->port_handle)
0249         return;
0250 
0251     if (filter->tmf_scope == FCP_TMF_LUN_RESET &&
0252         old_req->qtcb->header.lun_handle != filter->lun_handle)
0253         return;
0254 
0255     zfcp_dbf_scsi_nullcmnd((struct scsi_cmnd *)old_req->data, old_req);
0256     old_req->data = NULL;
0257 }
0258 
0259 static void zfcp_scsi_forget_cmnds(struct zfcp_scsi_dev *zsdev, u8 tm_flags)
0260 {
0261     struct zfcp_adapter *adapter = zsdev->port->adapter;
0262     struct zfcp_scsi_req_filter filter = {
0263         .tmf_scope = FCP_TMF_TGT_RESET,
0264         .port_handle = zsdev->port->handle,
0265     };
0266     unsigned long flags;
0267 
0268     if (tm_flags == FCP_TMF_LUN_RESET) {
0269         filter.tmf_scope = FCP_TMF_LUN_RESET;
0270         filter.lun_handle = zsdev->lun_handle;
0271     }
0272 
0273     /*
0274      * abort_lock secures against other processings - in the abort-function
0275      * and normal cmnd-handler - of (struct zfcp_fsf_req *)->data
0276      */
0277     write_lock_irqsave(&adapter->abort_lock, flags);
0278     zfcp_reqlist_apply_for_all(adapter->req_list, zfcp_scsi_forget_cmnd,
0279                    &filter);
0280     write_unlock_irqrestore(&adapter->abort_lock, flags);
0281 }
0282 
0283 /**
0284  * zfcp_scsi_task_mgmt_function() - Send a task management function (sync).
0285  * @sdev: Pointer to SCSI device to send the task management command to.
0286  * @tm_flags: Task management flags,
0287  *        here we only handle %FCP_TMF_TGT_RESET or %FCP_TMF_LUN_RESET.
0288  */
0289 static int zfcp_scsi_task_mgmt_function(struct scsi_device *sdev, u8 tm_flags)
0290 {
0291     struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
0292     struct zfcp_adapter *adapter = zfcp_sdev->port->adapter;
0293     struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
0294     struct zfcp_fsf_req *fsf_req = NULL;
0295     int retval = SUCCESS, ret;
0296     int retry = 3;
0297 
0298     while (retry--) {
0299         fsf_req = zfcp_fsf_fcp_task_mgmt(sdev, tm_flags);
0300         if (fsf_req)
0301             break;
0302 
0303         zfcp_dbf_scsi_devreset("wait", sdev, tm_flags, NULL);
0304         zfcp_erp_wait(adapter);
0305         ret = fc_block_rport(rport);
0306         if (ret) {
0307             zfcp_dbf_scsi_devreset("fiof", sdev, tm_flags, NULL);
0308             return ret;
0309         }
0310 
0311         if (!(atomic_read(&adapter->status) &
0312               ZFCP_STATUS_COMMON_RUNNING)) {
0313             zfcp_dbf_scsi_devreset("nres", sdev, tm_flags, NULL);
0314             return SUCCESS;
0315         }
0316     }
0317     if (!fsf_req) {
0318         zfcp_dbf_scsi_devreset("reqf", sdev, tm_flags, NULL);
0319         return FAILED;
0320     }
0321 
0322     wait_for_completion(&fsf_req->completion);
0323 
0324     if (fsf_req->status & ZFCP_STATUS_FSFREQ_TMFUNCFAILED) {
0325         zfcp_dbf_scsi_devreset("fail", sdev, tm_flags, fsf_req);
0326         retval = FAILED;
0327     } else {
0328         zfcp_dbf_scsi_devreset("okay", sdev, tm_flags, fsf_req);
0329         zfcp_scsi_forget_cmnds(zfcp_sdev, tm_flags);
0330     }
0331 
0332     zfcp_fsf_req_free(fsf_req);
0333     return retval;
0334 }
0335 
0336 static int zfcp_scsi_eh_device_reset_handler(struct scsi_cmnd *scpnt)
0337 {
0338     struct scsi_device *sdev = scpnt->device;
0339 
0340     return zfcp_scsi_task_mgmt_function(sdev, FCP_TMF_LUN_RESET);
0341 }
0342 
0343 static int zfcp_scsi_eh_target_reset_handler(struct scsi_cmnd *scpnt)
0344 {
0345     struct scsi_target *starget = scsi_target(scpnt->device);
0346     struct fc_rport *rport = starget_to_rport(starget);
0347     struct Scsi_Host *shost = rport_to_shost(rport);
0348     struct scsi_device *sdev = NULL, *tmp_sdev;
0349     struct zfcp_adapter *adapter =
0350         (struct zfcp_adapter *)shost->hostdata[0];
0351     int ret;
0352 
0353     shost_for_each_device(tmp_sdev, shost) {
0354         if (tmp_sdev->id == starget->id) {
0355             sdev = tmp_sdev;
0356             break;
0357         }
0358     }
0359     if (!sdev) {
0360         ret = FAILED;
0361         zfcp_dbf_scsi_eh("tr_nosd", adapter, starget->id, ret);
0362         return ret;
0363     }
0364 
0365     ret = zfcp_scsi_task_mgmt_function(sdev, FCP_TMF_TGT_RESET);
0366 
0367     /* release reference from above shost_for_each_device */
0368     if (sdev)
0369         scsi_device_put(tmp_sdev);
0370 
0371     return ret;
0372 }
0373 
0374 static int zfcp_scsi_eh_host_reset_handler(struct scsi_cmnd *scpnt)
0375 {
0376     struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(scpnt->device);
0377     struct zfcp_adapter *adapter = zfcp_sdev->port->adapter;
0378     int ret = SUCCESS, fc_ret;
0379 
0380     if (!(adapter->connection_features & FSF_FEATURE_NPIV_MODE)) {
0381         zfcp_erp_port_forced_reopen_all(adapter, 0, "schrh_p");
0382         zfcp_erp_wait(adapter);
0383     }
0384     zfcp_erp_adapter_reopen(adapter, 0, "schrh_1");
0385     zfcp_erp_wait(adapter);
0386     fc_ret = fc_block_scsi_eh(scpnt);
0387     if (fc_ret)
0388         ret = fc_ret;
0389 
0390     zfcp_dbf_scsi_eh("schrh_r", adapter, ~0, ret);
0391     return ret;
0392 }
0393 
0394 /**
0395  * zfcp_scsi_sysfs_host_reset() - Support scsi_host sysfs attribute host_reset.
0396  * @shost: Pointer to Scsi_Host to perform action on.
0397  * @reset_type: We support %SCSI_ADAPTER_RESET but not %SCSI_FIRMWARE_RESET.
0398  *
0399  * Return: 0 on %SCSI_ADAPTER_RESET, -%EOPNOTSUPP otherwise.
0400  *
0401  * This is similar to zfcp_sysfs_adapter_failed_store().
0402  */
0403 static int zfcp_scsi_sysfs_host_reset(struct Scsi_Host *shost, int reset_type)
0404 {
0405     struct zfcp_adapter *adapter =
0406         (struct zfcp_adapter *)shost->hostdata[0];
0407     int ret = 0;
0408 
0409     if (reset_type != SCSI_ADAPTER_RESET) {
0410         ret = -EOPNOTSUPP;
0411         zfcp_dbf_scsi_eh("scshr_n", adapter, ~0, ret);
0412         return ret;
0413     }
0414 
0415     zfcp_erp_adapter_reset_sync(adapter, "scshr_y");
0416     return ret;
0417 }
0418 
0419 struct scsi_transport_template *zfcp_scsi_transport_template;
0420 
0421 static struct scsi_host_template zfcp_scsi_host_template = {
0422     .module          = THIS_MODULE,
0423     .name            = "zfcp",
0424     .queuecommand        = zfcp_scsi_queuecommand,
0425     .eh_timed_out        = fc_eh_timed_out,
0426     .eh_abort_handler    = zfcp_scsi_eh_abort_handler,
0427     .eh_device_reset_handler = zfcp_scsi_eh_device_reset_handler,
0428     .eh_target_reset_handler = zfcp_scsi_eh_target_reset_handler,
0429     .eh_host_reset_handler   = zfcp_scsi_eh_host_reset_handler,
0430     .slave_alloc         = zfcp_scsi_slave_alloc,
0431     .slave_configure     = zfcp_scsi_slave_configure,
0432     .slave_destroy       = zfcp_scsi_slave_destroy,
0433     .change_queue_depth  = scsi_change_queue_depth,
0434     .host_reset      = zfcp_scsi_sysfs_host_reset,
0435     .proc_name       = "zfcp",
0436     .can_queue       = 4096,
0437     .this_id         = -1,
0438     .sg_tablesize        = (((QDIO_MAX_ELEMENTS_PER_BUFFER - 1)
0439                      * ZFCP_QDIO_MAX_SBALS_PER_REQ) - 2),
0440                    /* GCD, adjusted later */
0441     .max_sectors         = (((QDIO_MAX_ELEMENTS_PER_BUFFER - 1)
0442                      * ZFCP_QDIO_MAX_SBALS_PER_REQ) - 2) * 8,
0443                    /* GCD, adjusted later */
0444     /* report size limit per scatter-gather segment */
0445     .max_segment_size    = ZFCP_QDIO_SBALE_LEN,
0446     .dma_boundary        = ZFCP_QDIO_SBALE_LEN - 1,
0447     .shost_groups        = zfcp_sysfs_shost_attr_groups,
0448     .sdev_groups         = zfcp_sysfs_sdev_attr_groups,
0449     .track_queue_depth   = 1,
0450     .supported_mode      = MODE_INITIATOR,
0451 };
0452 
0453 /**
0454  * zfcp_scsi_adapter_register() - Allocate and register SCSI and FC host with
0455  *                SCSI midlayer
0456  * @adapter: The zfcp adapter to register with the SCSI midlayer
0457  *
0458  * Allocates the SCSI host object for the given adapter, sets basic properties
0459  * (such as the transport template, QDIO limits, ...), and registers it with
0460  * the midlayer.
0461  *
0462  * During registration with the midlayer the corresponding FC host object for
0463  * the referenced transport class is also implicitely allocated.
0464  *
0465  * Upon success adapter->scsi_host is set, and upon failure it remains NULL. If
0466  * adapter->scsi_host is already set, nothing is done.
0467  *
0468  * Return:
0469  * * 0       - Allocation and registration was successful
0470  * * -EEXIST - SCSI and FC host did already exist, nothing was done, nothing
0471  *         was changed
0472  * * -EIO    - Allocation or registration failed
0473  */
0474 int zfcp_scsi_adapter_register(struct zfcp_adapter *adapter)
0475 {
0476     struct ccw_dev_id dev_id;
0477 
0478     if (adapter->scsi_host)
0479         return -EEXIST;
0480 
0481     ccw_device_get_id(adapter->ccw_device, &dev_id);
0482     /* register adapter as SCSI host with mid layer of SCSI stack */
0483     adapter->scsi_host = scsi_host_alloc(&zfcp_scsi_host_template,
0484                          sizeof (struct zfcp_adapter *));
0485     if (!adapter->scsi_host)
0486         goto err_out;
0487 
0488     /* tell the SCSI stack some characteristics of this adapter */
0489     adapter->scsi_host->max_id = 511;
0490     adapter->scsi_host->max_lun = 0xFFFFFFFF;
0491     adapter->scsi_host->max_channel = 0;
0492     adapter->scsi_host->unique_id = dev_id.devno;
0493     adapter->scsi_host->max_cmd_len = 16; /* in struct fcp_cmnd */
0494     adapter->scsi_host->transportt = zfcp_scsi_transport_template;
0495 
0496     /* make all basic properties known at registration time */
0497     zfcp_qdio_shost_update(adapter, adapter->qdio);
0498     zfcp_scsi_set_prot(adapter);
0499 
0500     adapter->scsi_host->hostdata[0] = (unsigned long) adapter;
0501 
0502     if (scsi_add_host(adapter->scsi_host, &adapter->ccw_device->dev)) {
0503         scsi_host_put(adapter->scsi_host);
0504         goto err_out;
0505     }
0506 
0507     return 0;
0508 err_out:
0509     adapter->scsi_host = NULL;
0510     dev_err(&adapter->ccw_device->dev,
0511         "Registering the FCP device with the SCSI stack failed\n");
0512     return -EIO;
0513 }
0514 
0515 /**
0516  * zfcp_scsi_adapter_unregister - Unregister SCSI and FC host from SCSI midlayer
0517  * @adapter: The zfcp adapter to unregister.
0518  */
0519 void zfcp_scsi_adapter_unregister(struct zfcp_adapter *adapter)
0520 {
0521     struct Scsi_Host *shost;
0522     struct zfcp_port *port;
0523 
0524     shost = adapter->scsi_host;
0525     if (!shost)
0526         return;
0527 
0528     read_lock_irq(&adapter->port_list_lock);
0529     list_for_each_entry(port, &adapter->port_list, list)
0530         port->rport = NULL;
0531     read_unlock_irq(&adapter->port_list_lock);
0532 
0533     fc_remove_host(shost);
0534     scsi_remove_host(shost);
0535     scsi_host_put(shost);
0536     adapter->scsi_host = NULL;
0537 }
0538 
0539 static struct fc_host_statistics*
0540 zfcp_scsi_init_fc_host_stats(struct zfcp_adapter *adapter)
0541 {
0542     struct fc_host_statistics *fc_stats;
0543 
0544     if (!adapter->fc_stats) {
0545         fc_stats = kmalloc(sizeof(*fc_stats), GFP_KERNEL);
0546         if (!fc_stats)
0547             return NULL;
0548         adapter->fc_stats = fc_stats; /* freed in adapter_release */
0549     }
0550     memset(adapter->fc_stats, 0, sizeof(*adapter->fc_stats));
0551     return adapter->fc_stats;
0552 }
0553 
0554 static void zfcp_scsi_adjust_fc_host_stats(struct fc_host_statistics *fc_stats,
0555                        struct fsf_qtcb_bottom_port *data,
0556                        struct fsf_qtcb_bottom_port *old)
0557 {
0558     fc_stats->seconds_since_last_reset =
0559         data->seconds_since_last_reset - old->seconds_since_last_reset;
0560     fc_stats->tx_frames = data->tx_frames - old->tx_frames;
0561     fc_stats->tx_words = data->tx_words - old->tx_words;
0562     fc_stats->rx_frames = data->rx_frames - old->rx_frames;
0563     fc_stats->rx_words = data->rx_words - old->rx_words;
0564     fc_stats->lip_count = data->lip - old->lip;
0565     fc_stats->nos_count = data->nos - old->nos;
0566     fc_stats->error_frames = data->error_frames - old->error_frames;
0567     fc_stats->dumped_frames = data->dumped_frames - old->dumped_frames;
0568     fc_stats->link_failure_count = data->link_failure - old->link_failure;
0569     fc_stats->loss_of_sync_count = data->loss_of_sync - old->loss_of_sync;
0570     fc_stats->loss_of_signal_count =
0571         data->loss_of_signal - old->loss_of_signal;
0572     fc_stats->prim_seq_protocol_err_count =
0573         data->psp_error_counts - old->psp_error_counts;
0574     fc_stats->invalid_tx_word_count =
0575         data->invalid_tx_words - old->invalid_tx_words;
0576     fc_stats->invalid_crc_count = data->invalid_crcs - old->invalid_crcs;
0577     fc_stats->fcp_input_requests =
0578         data->input_requests - old->input_requests;
0579     fc_stats->fcp_output_requests =
0580         data->output_requests - old->output_requests;
0581     fc_stats->fcp_control_requests =
0582         data->control_requests - old->control_requests;
0583     fc_stats->fcp_input_megabytes = data->input_mb - old->input_mb;
0584     fc_stats->fcp_output_megabytes = data->output_mb - old->output_mb;
0585 }
0586 
0587 static void zfcp_scsi_set_fc_host_stats(struct fc_host_statistics *fc_stats,
0588                     struct fsf_qtcb_bottom_port *data)
0589 {
0590     fc_stats->seconds_since_last_reset = data->seconds_since_last_reset;
0591     fc_stats->tx_frames = data->tx_frames;
0592     fc_stats->tx_words = data->tx_words;
0593     fc_stats->rx_frames = data->rx_frames;
0594     fc_stats->rx_words = data->rx_words;
0595     fc_stats->lip_count = data->lip;
0596     fc_stats->nos_count = data->nos;
0597     fc_stats->error_frames = data->error_frames;
0598     fc_stats->dumped_frames = data->dumped_frames;
0599     fc_stats->link_failure_count = data->link_failure;
0600     fc_stats->loss_of_sync_count = data->loss_of_sync;
0601     fc_stats->loss_of_signal_count = data->loss_of_signal;
0602     fc_stats->prim_seq_protocol_err_count = data->psp_error_counts;
0603     fc_stats->invalid_tx_word_count = data->invalid_tx_words;
0604     fc_stats->invalid_crc_count = data->invalid_crcs;
0605     fc_stats->fcp_input_requests = data->input_requests;
0606     fc_stats->fcp_output_requests = data->output_requests;
0607     fc_stats->fcp_control_requests = data->control_requests;
0608     fc_stats->fcp_input_megabytes = data->input_mb;
0609     fc_stats->fcp_output_megabytes = data->output_mb;
0610 }
0611 
0612 static struct fc_host_statistics *
0613 zfcp_scsi_get_fc_host_stats(struct Scsi_Host *host)
0614 {
0615     struct zfcp_adapter *adapter;
0616     struct fc_host_statistics *fc_stats;
0617     struct fsf_qtcb_bottom_port *data;
0618     int ret;
0619 
0620     adapter = (struct zfcp_adapter *)host->hostdata[0];
0621     fc_stats = zfcp_scsi_init_fc_host_stats(adapter);
0622     if (!fc_stats)
0623         return NULL;
0624 
0625     data = kzalloc(sizeof(*data), GFP_KERNEL);
0626     if (!data)
0627         return NULL;
0628 
0629     ret = zfcp_fsf_exchange_port_data_sync(adapter->qdio, data);
0630     if (ret != 0 && ret != -EAGAIN) {
0631         kfree(data);
0632         return NULL;
0633     }
0634 
0635     if (adapter->stats_reset &&
0636         ((jiffies/HZ - adapter->stats_reset) <
0637          data->seconds_since_last_reset))
0638         zfcp_scsi_adjust_fc_host_stats(fc_stats, data,
0639                            adapter->stats_reset_data);
0640     else
0641         zfcp_scsi_set_fc_host_stats(fc_stats, data);
0642 
0643     kfree(data);
0644     return fc_stats;
0645 }
0646 
0647 static void zfcp_scsi_reset_fc_host_stats(struct Scsi_Host *shost)
0648 {
0649     struct zfcp_adapter *adapter;
0650     struct fsf_qtcb_bottom_port *data;
0651     int ret;
0652 
0653     adapter = (struct zfcp_adapter *)shost->hostdata[0];
0654     data = kzalloc(sizeof(*data), GFP_KERNEL);
0655     if (!data)
0656         return;
0657 
0658     ret = zfcp_fsf_exchange_port_data_sync(adapter->qdio, data);
0659     if (ret != 0 && ret != -EAGAIN)
0660         kfree(data);
0661     else {
0662         adapter->stats_reset = jiffies/HZ;
0663         kfree(adapter->stats_reset_data);
0664         adapter->stats_reset_data = data; /* finally freed in
0665                              adapter_release */
0666     }
0667 }
0668 
0669 static void zfcp_scsi_get_host_port_state(struct Scsi_Host *shost)
0670 {
0671     struct zfcp_adapter *adapter =
0672         (struct zfcp_adapter *)shost->hostdata[0];
0673     int status = atomic_read(&adapter->status);
0674 
0675     if ((status & ZFCP_STATUS_COMMON_RUNNING) &&
0676         !(status & ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED))
0677         fc_host_port_state(shost) = FC_PORTSTATE_ONLINE;
0678     else if (status & ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED)
0679         fc_host_port_state(shost) = FC_PORTSTATE_LINKDOWN;
0680     else if (status & ZFCP_STATUS_COMMON_ERP_FAILED)
0681         fc_host_port_state(shost) = FC_PORTSTATE_ERROR;
0682     else
0683         fc_host_port_state(shost) = FC_PORTSTATE_UNKNOWN;
0684 }
0685 
0686 static void zfcp_scsi_set_rport_dev_loss_tmo(struct fc_rport *rport,
0687                          u32 timeout)
0688 {
0689     rport->dev_loss_tmo = timeout;
0690 }
0691 
0692 /**
0693  * zfcp_scsi_terminate_rport_io - Terminate all I/O on a rport
0694  * @rport: The FC rport where to teminate I/O
0695  *
0696  * Abort all pending SCSI commands for a port by closing the
0697  * port. Using a reopen avoids a conflict with a shutdown
0698  * overwriting a reopen. The "forced" ensures that a disappeared port
0699  * is not opened again as valid due to the cached plogi data in
0700  * non-NPIV mode.
0701  */
0702 static void zfcp_scsi_terminate_rport_io(struct fc_rport *rport)
0703 {
0704     struct zfcp_port *port;
0705     struct Scsi_Host *shost = rport_to_shost(rport);
0706     struct zfcp_adapter *adapter =
0707         (struct zfcp_adapter *)shost->hostdata[0];
0708 
0709     port = zfcp_get_port_by_wwpn(adapter, rport->port_name);
0710 
0711     if (port) {
0712         zfcp_erp_port_forced_reopen(port, 0, "sctrpi1");
0713         put_device(&port->dev);
0714     } else {
0715         zfcp_erp_port_forced_no_port_dbf(
0716             "sctrpin", adapter,
0717             rport->port_name /* zfcp_scsi_rport_register */,
0718             rport->port_id /* zfcp_scsi_rport_register */);
0719     }
0720 }
0721 
0722 static void zfcp_scsi_rport_register(struct zfcp_port *port)
0723 {
0724     struct fc_rport_identifiers ids;
0725     struct fc_rport *rport;
0726 
0727     if (port->rport)
0728         return;
0729 
0730     ids.node_name = port->wwnn;
0731     ids.port_name = port->wwpn;
0732     ids.port_id = port->d_id;
0733     ids.roles = FC_RPORT_ROLE_FCP_TARGET;
0734 
0735     zfcp_dbf_rec_trig_lock("scpaddy", port->adapter, port, NULL,
0736                    ZFCP_PSEUDO_ERP_ACTION_RPORT_ADD,
0737                    ZFCP_PSEUDO_ERP_ACTION_RPORT_ADD);
0738     rport = fc_remote_port_add(port->adapter->scsi_host, 0, &ids);
0739     if (!rport) {
0740         dev_err(&port->adapter->ccw_device->dev,
0741             "Registering port 0x%016Lx failed\n",
0742             (unsigned long long)port->wwpn);
0743         return;
0744     }
0745 
0746     rport->maxframe_size = port->maxframe_size;
0747     rport->supported_classes = port->supported_classes;
0748     port->rport = rport;
0749     port->starget_id = rport->scsi_target_id;
0750 
0751     zfcp_unit_queue_scsi_scan(port);
0752 }
0753 
0754 static void zfcp_scsi_rport_block(struct zfcp_port *port)
0755 {
0756     struct fc_rport *rport = port->rport;
0757 
0758     if (rport) {
0759         zfcp_dbf_rec_trig_lock("scpdely", port->adapter, port, NULL,
0760                        ZFCP_PSEUDO_ERP_ACTION_RPORT_DEL,
0761                        ZFCP_PSEUDO_ERP_ACTION_RPORT_DEL);
0762         fc_remote_port_delete(rport);
0763         port->rport = NULL;
0764     }
0765 }
0766 
0767 void zfcp_scsi_schedule_rport_register(struct zfcp_port *port)
0768 {
0769     get_device(&port->dev);
0770     port->rport_task = RPORT_ADD;
0771 
0772     if (!queue_work(port->adapter->work_queue, &port->rport_work))
0773         put_device(&port->dev);
0774 }
0775 
0776 void zfcp_scsi_schedule_rport_block(struct zfcp_port *port)
0777 {
0778     get_device(&port->dev);
0779     port->rport_task = RPORT_DEL;
0780 
0781     if (port->rport && queue_work(port->adapter->work_queue,
0782                       &port->rport_work))
0783         return;
0784 
0785     put_device(&port->dev);
0786 }
0787 
0788 void zfcp_scsi_schedule_rports_block(struct zfcp_adapter *adapter)
0789 {
0790     unsigned long flags;
0791     struct zfcp_port *port;
0792 
0793     read_lock_irqsave(&adapter->port_list_lock, flags);
0794     list_for_each_entry(port, &adapter->port_list, list)
0795         zfcp_scsi_schedule_rport_block(port);
0796     read_unlock_irqrestore(&adapter->port_list_lock, flags);
0797 }
0798 
0799 void zfcp_scsi_rport_work(struct work_struct *work)
0800 {
0801     struct zfcp_port *port = container_of(work, struct zfcp_port,
0802                           rport_work);
0803 
0804     set_worker_desc("zrp%c-%16llx",
0805             (port->rport_task == RPORT_ADD) ? 'a' : 'd',
0806             port->wwpn); /* < WORKER_DESC_LEN=24 */
0807     while (port->rport_task) {
0808         if (port->rport_task == RPORT_ADD) {
0809             port->rport_task = RPORT_NONE;
0810             zfcp_scsi_rport_register(port);
0811         } else {
0812             port->rport_task = RPORT_NONE;
0813             zfcp_scsi_rport_block(port);
0814         }
0815     }
0816 
0817     put_device(&port->dev);
0818 }
0819 
0820 /**
0821  * zfcp_scsi_set_prot - Configure DIF/DIX support in scsi_host
0822  * @adapter: The adapter where to configure DIF/DIX for the SCSI host
0823  */
0824 void zfcp_scsi_set_prot(struct zfcp_adapter *adapter)
0825 {
0826     unsigned int mask = 0;
0827     unsigned int data_div;
0828     struct Scsi_Host *shost = adapter->scsi_host;
0829 
0830     data_div = atomic_read(&adapter->status) &
0831            ZFCP_STATUS_ADAPTER_DATA_DIV_ENABLED;
0832 
0833     if ((enable_dif || zfcp_experimental_dix) &&
0834         adapter->adapter_features & FSF_FEATURE_DIF_PROT_TYPE1)
0835         mask |= SHOST_DIF_TYPE1_PROTECTION;
0836 
0837     if (zfcp_experimental_dix && data_div &&
0838         adapter->adapter_features & FSF_FEATURE_DIX_PROT_TCPIP) {
0839         mask |= SHOST_DIX_TYPE1_PROTECTION;
0840         scsi_host_set_guard(shost, SHOST_DIX_GUARD_IP);
0841         shost->sg_prot_tablesize = adapter->qdio->max_sbale_per_req / 2;
0842         shost->sg_tablesize = adapter->qdio->max_sbale_per_req / 2;
0843         shost->max_sectors = shost->sg_tablesize * 8;
0844     }
0845 
0846     scsi_host_set_prot(shost, mask);
0847 }
0848 
0849 /**
0850  * zfcp_scsi_dif_sense_error - Report DIF/DIX error as driver sense error
0851  * @scmd: The SCSI command to report the error for
0852  * @ascq: The ASCQ to put in the sense buffer
0853  *
0854  * See the error handling in sd_done for the sense codes used here.
0855  * Set DID_SOFT_ERROR to retry the request, if possible.
0856  */
0857 void zfcp_scsi_dif_sense_error(struct scsi_cmnd *scmd, int ascq)
0858 {
0859     scsi_build_sense(scmd, 1, ILLEGAL_REQUEST, 0x10, ascq);
0860     set_host_byte(scmd, DID_SOFT_ERROR);
0861 }
0862 
0863 void zfcp_scsi_shost_update_config_data(
0864     struct zfcp_adapter *const adapter,
0865     const struct fsf_qtcb_bottom_config *const bottom,
0866     const bool bottom_incomplete)
0867 {
0868     struct Scsi_Host *const shost = adapter->scsi_host;
0869     const struct fc_els_flogi *nsp, *plogi;
0870 
0871     if (shost == NULL)
0872         return;
0873 
0874     snprintf(fc_host_firmware_version(shost), FC_VERSION_STRING_SIZE,
0875          "0x%08x", bottom->lic_version);
0876 
0877     if (adapter->adapter_features & FSF_FEATURE_HBAAPI_MANAGEMENT) {
0878         snprintf(fc_host_hardware_version(shost),
0879              FC_VERSION_STRING_SIZE,
0880              "0x%08x", bottom->hardware_version);
0881         memcpy(fc_host_serial_number(shost), bottom->serial_number,
0882                min(FC_SERIAL_NUMBER_SIZE, 17));
0883         EBCASC(fc_host_serial_number(shost),
0884                min(FC_SERIAL_NUMBER_SIZE, 17));
0885     }
0886 
0887     /* adjust pointers for missing command code */
0888     nsp = (struct fc_els_flogi *) ((u8 *)&bottom->nport_serv_param
0889                     - sizeof(u32));
0890     plogi = (struct fc_els_flogi *) ((u8 *)&bottom->plogi_payload
0891                     - sizeof(u32));
0892 
0893     snprintf(fc_host_manufacturer(shost), FC_SERIAL_NUMBER_SIZE, "%s",
0894          "IBM");
0895     fc_host_port_name(shost) = be64_to_cpu(nsp->fl_wwpn);
0896     fc_host_node_name(shost) = be64_to_cpu(nsp->fl_wwnn);
0897     fc_host_supported_classes(shost) = FC_COS_CLASS2 | FC_COS_CLASS3;
0898 
0899     zfcp_scsi_set_prot(adapter);
0900 
0901     /* do not evaluate invalid fields */
0902     if (bottom_incomplete)
0903         return;
0904 
0905     fc_host_port_id(shost) = ntoh24(bottom->s_id);
0906     fc_host_speed(shost) =
0907         zfcp_fsf_convert_portspeed(bottom->fc_link_speed);
0908 
0909     snprintf(fc_host_model(shost), FC_SYMBOLIC_NAME_SIZE, "0x%04x",
0910          bottom->adapter_type);
0911 
0912     switch (bottom->fc_topology) {
0913     case FSF_TOPO_P2P:
0914         fc_host_port_type(shost) = FC_PORTTYPE_PTP;
0915         fc_host_fabric_name(shost) = 0;
0916         break;
0917     case FSF_TOPO_FABRIC:
0918         fc_host_fabric_name(shost) = be64_to_cpu(plogi->fl_wwnn);
0919         if (bottom->connection_features & FSF_FEATURE_NPIV_MODE)
0920             fc_host_port_type(shost) = FC_PORTTYPE_NPIV;
0921         else
0922             fc_host_port_type(shost) = FC_PORTTYPE_NPORT;
0923         break;
0924     case FSF_TOPO_AL:
0925         fc_host_port_type(shost) = FC_PORTTYPE_NLPORT;
0926         fallthrough;
0927     default:
0928         fc_host_fabric_name(shost) = 0;
0929         break;
0930     }
0931 }
0932 
0933 void zfcp_scsi_shost_update_port_data(
0934     struct zfcp_adapter *const adapter,
0935     const struct fsf_qtcb_bottom_port *const bottom)
0936 {
0937     struct Scsi_Host *const shost = adapter->scsi_host;
0938 
0939     if (shost == NULL)
0940         return;
0941 
0942     fc_host_permanent_port_name(shost) = bottom->wwpn;
0943     fc_host_maxframe_size(shost) = bottom->maximum_frame_size;
0944     fc_host_supported_speeds(shost) =
0945         zfcp_fsf_convert_portspeed(bottom->supported_speed);
0946     memcpy(fc_host_supported_fc4s(shost), bottom->supported_fc4_types,
0947            FC_FC4_LIST_SIZE);
0948     memcpy(fc_host_active_fc4s(shost), bottom->active_fc4_types,
0949            FC_FC4_LIST_SIZE);
0950 }
0951 
0952 struct fc_function_template zfcp_transport_functions = {
0953     .show_starget_port_id = 1,
0954     .show_starget_port_name = 1,
0955     .show_starget_node_name = 1,
0956     .show_rport_supported_classes = 1,
0957     .show_rport_maxframe_size = 1,
0958     .show_rport_dev_loss_tmo = 1,
0959     .show_host_node_name = 1,
0960     .show_host_port_name = 1,
0961     .show_host_permanent_port_name = 1,
0962     .show_host_supported_classes = 1,
0963     .show_host_supported_fc4s = 1,
0964     .show_host_supported_speeds = 1,
0965     .show_host_maxframe_size = 1,
0966     .show_host_serial_number = 1,
0967     .show_host_manufacturer = 1,
0968     .show_host_model = 1,
0969     .show_host_hardware_version = 1,
0970     .show_host_firmware_version = 1,
0971     .get_fc_host_stats = zfcp_scsi_get_fc_host_stats,
0972     .reset_fc_host_stats = zfcp_scsi_reset_fc_host_stats,
0973     .set_rport_dev_loss_tmo = zfcp_scsi_set_rport_dev_loss_tmo,
0974     .get_host_port_state = zfcp_scsi_get_host_port_state,
0975     .terminate_rport_io = zfcp_scsi_terminate_rport_io,
0976     .show_host_port_state = 1,
0977     .show_host_active_fc4s = 1,
0978     .bsg_request = zfcp_fc_exec_bsg_job,
0979     .bsg_timeout = zfcp_fc_timeout_bsg_job,
0980     /* no functions registered for following dynamic attributes but
0981        directly set by LLDD */
0982     .show_host_port_type = 1,
0983     .show_host_symbolic_name = 1,
0984     .show_host_speed = 1,
0985     .show_host_port_id = 1,
0986     .show_host_fabric_name = 1,
0987     .dd_bsg_size = sizeof(struct zfcp_fsf_ct_els),
0988 };