linux/drivers/s390/scsi/zfcp_scsi.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0
   2/*
   3 * zfcp device driver
   4 *
   5 * Interface to Linux SCSI midlayer.
   6 *
   7 * Copyright IBM Corp. 2002, 2018
   8 */
   9
  10#define KMSG_COMPONENT "zfcp"
  11#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
  12
  13#include <linux/module.h>
  14#include <linux/types.h>
  15#include <linux/slab.h>
  16#include <scsi/fc/fc_fcp.h>
  17#include <scsi/scsi_eh.h>
  18#include <linux/atomic.h>
  19#include "zfcp_ext.h"
  20#include "zfcp_dbf.h"
  21#include "zfcp_fc.h"
  22#include "zfcp_reqlist.h"
  23
  24static unsigned int default_depth = 32;
  25module_param_named(queue_depth, default_depth, uint, 0600);
  26MODULE_PARM_DESC(queue_depth, "Default queue depth for new SCSI devices");
  27
  28static bool enable_dif;
  29module_param_named(dif, enable_dif, bool, 0400);
  30MODULE_PARM_DESC(dif, "Enable DIF data integrity support (default off)");
  31
  32bool zfcp_experimental_dix;
  33module_param_named(dix, zfcp_experimental_dix, bool, 0400);
  34MODULE_PARM_DESC(dix, "Enable experimental DIX (data integrity extension) support which implies DIF support (default off)");
  35
  36static bool allow_lun_scan = true;
  37module_param(allow_lun_scan, bool, 0600);
  38MODULE_PARM_DESC(allow_lun_scan, "For NPIV, scan and attach all storage LUNs");
  39
  40static void zfcp_scsi_slave_destroy(struct scsi_device *sdev)
  41{
  42        struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
  43
  44        /* if previous slave_alloc returned early, there is nothing to do */
  45        if (!zfcp_sdev->port)
  46                return;
  47
  48        zfcp_erp_lun_shutdown_wait(sdev, "scssd_1");
  49        put_device(&zfcp_sdev->port->dev);
  50}
  51
  52static int zfcp_scsi_slave_configure(struct scsi_device *sdp)
  53{
  54        if (sdp->tagged_supported)
  55                scsi_change_queue_depth(sdp, default_depth);
  56        return 0;
  57}
  58
  59static void zfcp_scsi_command_fail(struct scsi_cmnd *scpnt, int result)
  60{
  61        set_host_byte(scpnt, result);
  62        zfcp_dbf_scsi_fail_send(scpnt);
  63        scpnt->scsi_done(scpnt);
  64}
  65
  66static
  67int zfcp_scsi_queuecommand(struct Scsi_Host *shost, struct scsi_cmnd *scpnt)
  68{
  69        struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(scpnt->device);
  70        struct fc_rport *rport = starget_to_rport(scsi_target(scpnt->device));
  71        int    status, scsi_result, ret;
  72
  73        /* reset the status for this request */
  74        scpnt->result = 0;
  75        scpnt->host_scribble = NULL;
  76
  77        scsi_result = fc_remote_port_chkready(rport);
  78        if (unlikely(scsi_result)) {
  79                scpnt->result = scsi_result;
  80                zfcp_dbf_scsi_fail_send(scpnt);
  81                scpnt->scsi_done(scpnt);
  82                return 0;
  83        }
  84
  85        status = atomic_read(&zfcp_sdev->status);
  86        if (unlikely(status & ZFCP_STATUS_COMMON_ERP_FAILED) &&
  87                     !(atomic_read(&zfcp_sdev->port->status) &
  88                       ZFCP_STATUS_COMMON_ERP_FAILED)) {
  89                /* only LUN access denied, but port is good
  90                 * not covered by FC transport, have to fail here */
  91                zfcp_scsi_command_fail(scpnt, DID_ERROR);
  92                return 0;
  93        }
  94
  95        if (unlikely(!(status & ZFCP_STATUS_COMMON_UNBLOCKED))) {
  96                /* This could be
  97                 * call to rport_delete pending: mimic retry from
  98                 *      fc_remote_port_chkready until rport is BLOCKED
  99                 */
 100                zfcp_scsi_command_fail(scpnt, DID_IMM_RETRY);
 101                return 0;
 102        }
 103
 104        ret = zfcp_fsf_fcp_cmnd(scpnt);
 105        if (unlikely(ret == -EBUSY))
 106                return SCSI_MLQUEUE_DEVICE_BUSY;
 107        else if (unlikely(ret < 0))
 108                return SCSI_MLQUEUE_HOST_BUSY;
 109
 110        return ret;
 111}
 112
 113static int zfcp_scsi_slave_alloc(struct scsi_device *sdev)
 114{
 115        struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
 116        struct zfcp_adapter *adapter =
 117                (struct zfcp_adapter *) sdev->host->hostdata[0];
 118        struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
 119        struct zfcp_port *port;
 120        struct zfcp_unit *unit;
 121        int npiv = adapter->connection_features & FSF_FEATURE_NPIV_MODE;
 122
 123        zfcp_sdev->erp_action.adapter = adapter;
 124        zfcp_sdev->erp_action.sdev = sdev;
 125
 126        port = zfcp_get_port_by_wwpn(adapter, rport->port_name);
 127        if (!port)
 128                return -ENXIO;
 129
 130        zfcp_sdev->erp_action.port = port;
 131
 132        mutex_lock(&zfcp_sysfs_port_units_mutex);
 133        if (zfcp_sysfs_port_is_removing(port)) {
 134                /* port is already gone */
 135                mutex_unlock(&zfcp_sysfs_port_units_mutex);
 136                put_device(&port->dev); /* undo zfcp_get_port_by_wwpn() */
 137                return -ENXIO;
 138        }
 139        mutex_unlock(&zfcp_sysfs_port_units_mutex);
 140
 141        unit = zfcp_unit_find(port, zfcp_scsi_dev_lun(sdev));
 142        if (unit)
 143                put_device(&unit->dev);
 144
 145        if (!unit && !(allow_lun_scan && npiv)) {
 146                put_device(&port->dev);
 147                return -ENXIO;
 148        }
 149
 150        zfcp_sdev->port = port;
 151        zfcp_sdev->latencies.write.channel.min = 0xFFFFFFFF;
 152        zfcp_sdev->latencies.write.fabric.min = 0xFFFFFFFF;
 153        zfcp_sdev->latencies.read.channel.min = 0xFFFFFFFF;
 154        zfcp_sdev->latencies.read.fabric.min = 0xFFFFFFFF;
 155        zfcp_sdev->latencies.cmd.channel.min = 0xFFFFFFFF;
 156        zfcp_sdev->latencies.cmd.fabric.min = 0xFFFFFFFF;
 157        spin_lock_init(&zfcp_sdev->latencies.lock);
 158
 159        zfcp_erp_set_lun_status(sdev, ZFCP_STATUS_COMMON_RUNNING);
 160        zfcp_erp_lun_reopen(sdev, 0, "scsla_1");
 161        zfcp_erp_wait(port->adapter);
 162
 163        return 0;
 164}
 165
 166static int zfcp_scsi_eh_abort_handler(struct scsi_cmnd *scpnt)
 167{
 168        struct Scsi_Host *scsi_host = scpnt->device->host;
 169        struct zfcp_adapter *adapter =
 170                (struct zfcp_adapter *) scsi_host->hostdata[0];
 171        struct zfcp_fsf_req *old_req, *abrt_req;
 172        unsigned long flags;
 173        unsigned long old_reqid = (unsigned long) scpnt->host_scribble;
 174        int retval = SUCCESS, ret;
 175        int retry = 3;
 176        char *dbf_tag;
 177
 178        /* avoid race condition between late normal completion and abort */
 179        write_lock_irqsave(&adapter->abort_lock, flags);
 180
 181        old_req = zfcp_reqlist_find(adapter->req_list, old_reqid);
 182        if (!old_req) {
 183                write_unlock_irqrestore(&adapter->abort_lock, flags);
 184                zfcp_dbf_scsi_abort("abrt_or", scpnt, NULL);
 185                return FAILED; /* completion could be in progress */
 186        }
 187        old_req->data = NULL;
 188
 189        /* don't access old fsf_req after releasing the abort_lock */
 190        write_unlock_irqrestore(&adapter->abort_lock, flags);
 191
 192        while (retry--) {
 193                abrt_req = zfcp_fsf_abort_fcp_cmnd(scpnt);
 194                if (abrt_req)
 195                        break;
 196
 197                zfcp_dbf_scsi_abort("abrt_wt", scpnt, NULL);
 198                zfcp_erp_wait(adapter);
 199                ret = fc_block_scsi_eh(scpnt);
 200                if (ret) {
 201                        zfcp_dbf_scsi_abort("abrt_bl", scpnt, NULL);
 202                        return ret;
 203                }
 204                if (!(atomic_read(&adapter->status) &
 205                      ZFCP_STATUS_COMMON_RUNNING)) {
 206                        zfcp_dbf_scsi_abort("abrt_ru", scpnt, NULL);
 207                        return SUCCESS;
 208                }
 209        }
 210        if (!abrt_req) {
 211                zfcp_dbf_scsi_abort("abrt_ar", scpnt, NULL);
 212                return FAILED;
 213        }
 214
 215        wait_for_completion(&abrt_req->completion);
 216
 217        if (abrt_req->status & ZFCP_STATUS_FSFREQ_ABORTSUCCEEDED)
 218                dbf_tag = "abrt_ok";
 219        else if (abrt_req->status & ZFCP_STATUS_FSFREQ_ABORTNOTNEEDED)
 220                dbf_tag = "abrt_nn";
 221        else {
 222                dbf_tag = "abrt_fa";
 223                retval = FAILED;
 224        }
 225        zfcp_dbf_scsi_abort(dbf_tag, scpnt, abrt_req);
 226        zfcp_fsf_req_free(abrt_req);
 227        return retval;
 228}
 229
 230struct zfcp_scsi_req_filter {
 231        u8 tmf_scope;
 232        u32 lun_handle;
 233        u32 port_handle;
 234};
 235
 236static void zfcp_scsi_forget_cmnd(struct zfcp_fsf_req *old_req, void *data)
 237{
 238        struct zfcp_scsi_req_filter *filter =
 239                (struct zfcp_scsi_req_filter *)data;
 240
 241        /* already aborted - prevent side-effects - or not a SCSI command */
 242        if (old_req->data == NULL ||
 243            zfcp_fsf_req_is_status_read_buffer(old_req) ||
 244            old_req->qtcb->header.fsf_command != FSF_QTCB_FCP_CMND)
 245                return;
 246
 247        /* (tmf_scope == FCP_TMF_TGT_RESET || tmf_scope == FCP_TMF_LUN_RESET) */
 248        if (old_req->qtcb->header.port_handle != filter->port_handle)
 249                return;
 250
 251        if (filter->tmf_scope == FCP_TMF_LUN_RESET &&
 252            old_req->qtcb->header.lun_handle != filter->lun_handle)
 253                return;
 254
 255        zfcp_dbf_scsi_nullcmnd((struct scsi_cmnd *)old_req->data, old_req);
 256        old_req->data = NULL;
 257}
 258
 259static void zfcp_scsi_forget_cmnds(struct zfcp_scsi_dev *zsdev, u8 tm_flags)
 260{
 261        struct zfcp_adapter *adapter = zsdev->port->adapter;
 262        struct zfcp_scsi_req_filter filter = {
 263                .tmf_scope = FCP_TMF_TGT_RESET,
 264                .port_handle = zsdev->port->handle,
 265        };
 266        unsigned long flags;
 267
 268        if (tm_flags == FCP_TMF_LUN_RESET) {
 269                filter.tmf_scope = FCP_TMF_LUN_RESET;
 270                filter.lun_handle = zsdev->lun_handle;
 271        }
 272
 273        /*
 274         * abort_lock secures against other processings - in the abort-function
 275         * and normal cmnd-handler - of (struct zfcp_fsf_req *)->data
 276         */
 277        write_lock_irqsave(&adapter->abort_lock, flags);
 278        zfcp_reqlist_apply_for_all(adapter->req_list, zfcp_scsi_forget_cmnd,
 279                                   &filter);
 280        write_unlock_irqrestore(&adapter->abort_lock, flags);
 281}
 282
 283/**
 284 * zfcp_scsi_task_mgmt_function() - Send a task management function (sync).
 285 * @sdev: Pointer to SCSI device to send the task management command to.
 286 * @tm_flags: Task management flags,
 287 *            here we only handle %FCP_TMF_TGT_RESET or %FCP_TMF_LUN_RESET.
 288 */
 289static int zfcp_scsi_task_mgmt_function(struct scsi_device *sdev, u8 tm_flags)
 290{
 291        struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
 292        struct zfcp_adapter *adapter = zfcp_sdev->port->adapter;
 293        struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
 294        struct zfcp_fsf_req *fsf_req = NULL;
 295        int retval = SUCCESS, ret;
 296        int retry = 3;
 297
 298        while (retry--) {
 299                fsf_req = zfcp_fsf_fcp_task_mgmt(sdev, tm_flags);
 300                if (fsf_req)
 301                        break;
 302
 303                zfcp_dbf_scsi_devreset("wait", sdev, tm_flags, NULL);
 304                zfcp_erp_wait(adapter);
 305                ret = fc_block_rport(rport);
 306                if (ret) {
 307                        zfcp_dbf_scsi_devreset("fiof", sdev, tm_flags, NULL);
 308                        return ret;
 309                }
 310
 311                if (!(atomic_read(&adapter->status) &
 312                      ZFCP_STATUS_COMMON_RUNNING)) {
 313                        zfcp_dbf_scsi_devreset("nres", sdev, tm_flags, NULL);
 314                        return SUCCESS;
 315                }
 316        }
 317        if (!fsf_req) {
 318                zfcp_dbf_scsi_devreset("reqf", sdev, tm_flags, NULL);
 319                return FAILED;
 320        }
 321
 322        wait_for_completion(&fsf_req->completion);
 323
 324        if (fsf_req->status & ZFCP_STATUS_FSFREQ_TMFUNCFAILED) {
 325                zfcp_dbf_scsi_devreset("fail", sdev, tm_flags, fsf_req);
 326                retval = FAILED;
 327        } else {
 328                zfcp_dbf_scsi_devreset("okay", sdev, tm_flags, fsf_req);
 329                zfcp_scsi_forget_cmnds(zfcp_sdev, tm_flags);
 330        }
 331
 332        zfcp_fsf_req_free(fsf_req);
 333        return retval;
 334}
 335
 336static int zfcp_scsi_eh_device_reset_handler(struct scsi_cmnd *scpnt)
 337{
 338        struct scsi_device *sdev = scpnt->device;
 339
 340        return zfcp_scsi_task_mgmt_function(sdev, FCP_TMF_LUN_RESET);
 341}
 342
 343static int zfcp_scsi_eh_target_reset_handler(struct scsi_cmnd *scpnt)
 344{
 345        struct scsi_target *starget = scsi_target(scpnt->device);
 346        struct fc_rport *rport = starget_to_rport(starget);
 347        struct Scsi_Host *shost = rport_to_shost(rport);
 348        struct scsi_device *sdev = NULL, *tmp_sdev;
 349        struct zfcp_adapter *adapter =
 350                (struct zfcp_adapter *)shost->hostdata[0];
 351        int ret;
 352
 353        shost_for_each_device(tmp_sdev, shost) {
 354                if (tmp_sdev->id == starget->id) {
 355                        sdev = tmp_sdev;
 356                        break;
 357                }
 358        }
 359        if (!sdev) {
 360                ret = FAILED;
 361                zfcp_dbf_scsi_eh("tr_nosd", adapter, starget->id, ret);
 362                return ret;
 363        }
 364
 365        ret = zfcp_scsi_task_mgmt_function(sdev, FCP_TMF_TGT_RESET);
 366
 367        /* release reference from above shost_for_each_device */
 368        if (sdev)
 369                scsi_device_put(tmp_sdev);
 370
 371        return ret;
 372}
 373
 374static int zfcp_scsi_eh_host_reset_handler(struct scsi_cmnd *scpnt)
 375{
 376        struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(scpnt->device);
 377        struct zfcp_adapter *adapter = zfcp_sdev->port->adapter;
 378        int ret = SUCCESS, fc_ret;
 379
 380        if (!(adapter->connection_features & FSF_FEATURE_NPIV_MODE)) {
 381                zfcp_erp_port_forced_reopen_all(adapter, 0, "schrh_p");
 382                zfcp_erp_wait(adapter);
 383        }
 384        zfcp_erp_adapter_reopen(adapter, 0, "schrh_1");
 385        zfcp_erp_wait(adapter);
 386        fc_ret = fc_block_scsi_eh(scpnt);
 387        if (fc_ret)
 388                ret = fc_ret;
 389
 390        zfcp_dbf_scsi_eh("schrh_r", adapter, ~0, ret);
 391        return ret;
 392}
 393
 394/**
 395 * zfcp_scsi_sysfs_host_reset() - Support scsi_host sysfs attribute host_reset.
 396 * @shost: Pointer to Scsi_Host to perform action on.
 397 * @reset_type: We support %SCSI_ADAPTER_RESET but not %SCSI_FIRMWARE_RESET.
 398 *
 399 * Return: 0 on %SCSI_ADAPTER_RESET, -%EOPNOTSUPP otherwise.
 400 *
 401 * This is similar to zfcp_sysfs_adapter_failed_store().
 402 */
 403static int zfcp_scsi_sysfs_host_reset(struct Scsi_Host *shost, int reset_type)
 404{
 405        struct zfcp_adapter *adapter =
 406                (struct zfcp_adapter *)shost->hostdata[0];
 407        int ret = 0;
 408
 409        if (reset_type != SCSI_ADAPTER_RESET) {
 410                ret = -EOPNOTSUPP;
 411                zfcp_dbf_scsi_eh("scshr_n", adapter, ~0, ret);
 412                return ret;
 413        }
 414
 415        zfcp_erp_adapter_reset_sync(adapter, "scshr_y");
 416        return ret;
 417}
 418
 419struct scsi_transport_template *zfcp_scsi_transport_template;
 420
 421static struct scsi_host_template zfcp_scsi_host_template = {
 422        .module                  = THIS_MODULE,
 423        .name                    = "zfcp",
 424        .queuecommand            = zfcp_scsi_queuecommand,
 425        .eh_timed_out            = fc_eh_timed_out,
 426        .eh_abort_handler        = zfcp_scsi_eh_abort_handler,
 427        .eh_device_reset_handler = zfcp_scsi_eh_device_reset_handler,
 428        .eh_target_reset_handler = zfcp_scsi_eh_target_reset_handler,
 429        .eh_host_reset_handler   = zfcp_scsi_eh_host_reset_handler,
 430        .slave_alloc             = zfcp_scsi_slave_alloc,
 431        .slave_configure         = zfcp_scsi_slave_configure,
 432        .slave_destroy           = zfcp_scsi_slave_destroy,
 433        .change_queue_depth      = scsi_change_queue_depth,
 434        .host_reset              = zfcp_scsi_sysfs_host_reset,
 435        .proc_name               = "zfcp",
 436        .can_queue               = 4096,
 437        .this_id                 = -1,
 438        .sg_tablesize            = (((QDIO_MAX_ELEMENTS_PER_BUFFER - 1)
 439                                     * ZFCP_QDIO_MAX_SBALS_PER_REQ) - 2),
 440                                   /* GCD, adjusted later */
 441        .max_sectors             = (((QDIO_MAX_ELEMENTS_PER_BUFFER - 1)
 442                                     * ZFCP_QDIO_MAX_SBALS_PER_REQ) - 2) * 8,
 443                                   /* GCD, adjusted later */
 444        /* report size limit per scatter-gather segment */
 445        .max_segment_size        = ZFCP_QDIO_SBALE_LEN,
 446        .dma_boundary            = ZFCP_QDIO_SBALE_LEN - 1,
 447        .shost_attrs             = zfcp_sysfs_shost_attrs,
 448        .sdev_attrs              = zfcp_sysfs_sdev_attrs,
 449        .track_queue_depth       = 1,
 450        .supported_mode          = MODE_INITIATOR,
 451};
 452
 453/**
 454 * zfcp_scsi_adapter_register - Register SCSI and FC host with SCSI midlayer
 455 * @adapter: The zfcp adapter to register with the SCSI midlayer
 456 */
 457int zfcp_scsi_adapter_register(struct zfcp_adapter *adapter)
 458{
 459        struct ccw_dev_id dev_id;
 460
 461        if (adapter->scsi_host)
 462                return 0;
 463
 464        ccw_device_get_id(adapter->ccw_device, &dev_id);
 465        /* register adapter as SCSI host with mid layer of SCSI stack */
 466        adapter->scsi_host = scsi_host_alloc(&zfcp_scsi_host_template,
 467                                             sizeof (struct zfcp_adapter *));
 468        if (!adapter->scsi_host) {
 469                dev_err(&adapter->ccw_device->dev,
 470                        "Registering the FCP device with the "
 471                        "SCSI stack failed\n");
 472                return -EIO;
 473        }
 474
 475        /* tell the SCSI stack some characteristics of this adapter */
 476        adapter->scsi_host->max_id = 511;
 477        adapter->scsi_host->max_lun = 0xFFFFFFFF;
 478        adapter->scsi_host->max_channel = 0;
 479        adapter->scsi_host->unique_id = dev_id.devno;
 480        adapter->scsi_host->max_cmd_len = 16; /* in struct fcp_cmnd */
 481        adapter->scsi_host->transportt = zfcp_scsi_transport_template;
 482
 483        adapter->scsi_host->hostdata[0] = (unsigned long) adapter;
 484
 485        if (scsi_add_host(adapter->scsi_host, &adapter->ccw_device->dev)) {
 486                scsi_host_put(adapter->scsi_host);
 487                return -EIO;
 488        }
 489
 490        return 0;
 491}
 492
 493/**
 494 * zfcp_scsi_adapter_unregister - Unregister SCSI and FC host from SCSI midlayer
 495 * @adapter: The zfcp adapter to unregister.
 496 */
 497void zfcp_scsi_adapter_unregister(struct zfcp_adapter *adapter)
 498{
 499        struct Scsi_Host *shost;
 500        struct zfcp_port *port;
 501
 502        shost = adapter->scsi_host;
 503        if (!shost)
 504                return;
 505
 506        read_lock_irq(&adapter->port_list_lock);
 507        list_for_each_entry(port, &adapter->port_list, list)
 508                port->rport = NULL;
 509        read_unlock_irq(&adapter->port_list_lock);
 510
 511        fc_remove_host(shost);
 512        scsi_remove_host(shost);
 513        scsi_host_put(shost);
 514        adapter->scsi_host = NULL;
 515}
 516
 517static struct fc_host_statistics*
 518zfcp_scsi_init_fc_host_stats(struct zfcp_adapter *adapter)
 519{
 520        struct fc_host_statistics *fc_stats;
 521
 522        if (!adapter->fc_stats) {
 523                fc_stats = kmalloc(sizeof(*fc_stats), GFP_KERNEL);
 524                if (!fc_stats)
 525                        return NULL;
 526                adapter->fc_stats = fc_stats; /* freed in adapter_release */
 527        }
 528        memset(adapter->fc_stats, 0, sizeof(*adapter->fc_stats));
 529        return adapter->fc_stats;
 530}
 531
 532static void zfcp_scsi_adjust_fc_host_stats(struct fc_host_statistics *fc_stats,
 533                                           struct fsf_qtcb_bottom_port *data,
 534                                           struct fsf_qtcb_bottom_port *old)
 535{
 536        fc_stats->seconds_since_last_reset =
 537                data->seconds_since_last_reset - old->seconds_since_last_reset;
 538        fc_stats->tx_frames = data->tx_frames - old->tx_frames;
 539        fc_stats->tx_words = data->tx_words - old->tx_words;
 540        fc_stats->rx_frames = data->rx_frames - old->rx_frames;
 541        fc_stats->rx_words = data->rx_words - old->rx_words;
 542        fc_stats->lip_count = data->lip - old->lip;
 543        fc_stats->nos_count = data->nos - old->nos;
 544        fc_stats->error_frames = data->error_frames - old->error_frames;
 545        fc_stats->dumped_frames = data->dumped_frames - old->dumped_frames;
 546        fc_stats->link_failure_count = data->link_failure - old->link_failure;
 547        fc_stats->loss_of_sync_count = data->loss_of_sync - old->loss_of_sync;
 548        fc_stats->loss_of_signal_count =
 549                data->loss_of_signal - old->loss_of_signal;
 550        fc_stats->prim_seq_protocol_err_count =
 551                data->psp_error_counts - old->psp_error_counts;
 552        fc_stats->invalid_tx_word_count =
 553                data->invalid_tx_words - old->invalid_tx_words;
 554        fc_stats->invalid_crc_count = data->invalid_crcs - old->invalid_crcs;
 555        fc_stats->fcp_input_requests =
 556                data->input_requests - old->input_requests;
 557        fc_stats->fcp_output_requests =
 558                data->output_requests - old->output_requests;
 559        fc_stats->fcp_control_requests =
 560                data->control_requests - old->control_requests;
 561        fc_stats->fcp_input_megabytes = data->input_mb - old->input_mb;
 562        fc_stats->fcp_output_megabytes = data->output_mb - old->output_mb;
 563}
 564
 565static void zfcp_scsi_set_fc_host_stats(struct fc_host_statistics *fc_stats,
 566                                        struct fsf_qtcb_bottom_port *data)
 567{
 568        fc_stats->seconds_since_last_reset = data->seconds_since_last_reset;
 569        fc_stats->tx_frames = data->tx_frames;
 570        fc_stats->tx_words = data->tx_words;
 571        fc_stats->rx_frames = data->rx_frames;
 572        fc_stats->rx_words = data->rx_words;
 573        fc_stats->lip_count = data->lip;
 574        fc_stats->nos_count = data->nos;
 575        fc_stats->error_frames = data->error_frames;
 576        fc_stats->dumped_frames = data->dumped_frames;
 577        fc_stats->link_failure_count = data->link_failure;
 578        fc_stats->loss_of_sync_count = data->loss_of_sync;
 579        fc_stats->loss_of_signal_count = data->loss_of_signal;
 580        fc_stats->prim_seq_protocol_err_count = data->psp_error_counts;
 581        fc_stats->invalid_tx_word_count = data->invalid_tx_words;
 582        fc_stats->invalid_crc_count = data->invalid_crcs;
 583        fc_stats->fcp_input_requests = data->input_requests;
 584        fc_stats->fcp_output_requests = data->output_requests;
 585        fc_stats->fcp_control_requests = data->control_requests;
 586        fc_stats->fcp_input_megabytes = data->input_mb;
 587        fc_stats->fcp_output_megabytes = data->output_mb;
 588}
 589
 590static struct fc_host_statistics *
 591zfcp_scsi_get_fc_host_stats(struct Scsi_Host *host)
 592{
 593        struct zfcp_adapter *adapter;
 594        struct fc_host_statistics *fc_stats;
 595        struct fsf_qtcb_bottom_port *data;
 596        int ret;
 597
 598        adapter = (struct zfcp_adapter *)host->hostdata[0];
 599        fc_stats = zfcp_scsi_init_fc_host_stats(adapter);
 600        if (!fc_stats)
 601                return NULL;
 602
 603        data = kzalloc(sizeof(*data), GFP_KERNEL);
 604        if (!data)
 605                return NULL;
 606
 607        ret = zfcp_fsf_exchange_port_data_sync(adapter->qdio, data);
 608        if (ret != 0 && ret != -EAGAIN) {
 609                kfree(data);
 610                return NULL;
 611        }
 612
 613        if (adapter->stats_reset &&
 614            ((jiffies/HZ - adapter->stats_reset) <
 615             data->seconds_since_last_reset))
 616                zfcp_scsi_adjust_fc_host_stats(fc_stats, data,
 617                                               adapter->stats_reset_data);
 618        else
 619                zfcp_scsi_set_fc_host_stats(fc_stats, data);
 620
 621        kfree(data);
 622        return fc_stats;
 623}
 624
 625static void zfcp_scsi_reset_fc_host_stats(struct Scsi_Host *shost)
 626{
 627        struct zfcp_adapter *adapter;
 628        struct fsf_qtcb_bottom_port *data;
 629        int ret;
 630
 631        adapter = (struct zfcp_adapter *)shost->hostdata[0];
 632        data = kzalloc(sizeof(*data), GFP_KERNEL);
 633        if (!data)
 634                return;
 635
 636        ret = zfcp_fsf_exchange_port_data_sync(adapter->qdio, data);
 637        if (ret != 0 && ret != -EAGAIN)
 638                kfree(data);
 639        else {
 640                adapter->stats_reset = jiffies/HZ;
 641                kfree(adapter->stats_reset_data);
 642                adapter->stats_reset_data = data; /* finally freed in
 643                                                     adapter_release */
 644        }
 645}
 646
 647static void zfcp_scsi_get_host_port_state(struct Scsi_Host *shost)
 648{
 649        struct zfcp_adapter *adapter =
 650                (struct zfcp_adapter *)shost->hostdata[0];
 651        int status = atomic_read(&adapter->status);
 652
 653        if ((status & ZFCP_STATUS_COMMON_RUNNING) &&
 654            !(status & ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED))
 655                fc_host_port_state(shost) = FC_PORTSTATE_ONLINE;
 656        else if (status & ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED)
 657                fc_host_port_state(shost) = FC_PORTSTATE_LINKDOWN;
 658        else if (status & ZFCP_STATUS_COMMON_ERP_FAILED)
 659                fc_host_port_state(shost) = FC_PORTSTATE_ERROR;
 660        else
 661                fc_host_port_state(shost) = FC_PORTSTATE_UNKNOWN;
 662}
 663
 664static void zfcp_scsi_set_rport_dev_loss_tmo(struct fc_rport *rport,
 665                                             u32 timeout)
 666{
 667        rport->dev_loss_tmo = timeout;
 668}
 669
 670/**
 671 * zfcp_scsi_terminate_rport_io - Terminate all I/O on a rport
 672 * @rport: The FC rport where to teminate I/O
 673 *
 674 * Abort all pending SCSI commands for a port by closing the
 675 * port. Using a reopen avoids a conflict with a shutdown
 676 * overwriting a reopen. The "forced" ensures that a disappeared port
 677 * is not opened again as valid due to the cached plogi data in
 678 * non-NPIV mode.
 679 */
 680static void zfcp_scsi_terminate_rport_io(struct fc_rport *rport)
 681{
 682        struct zfcp_port *port;
 683        struct Scsi_Host *shost = rport_to_shost(rport);
 684        struct zfcp_adapter *adapter =
 685                (struct zfcp_adapter *)shost->hostdata[0];
 686
 687        port = zfcp_get_port_by_wwpn(adapter, rport->port_name);
 688
 689        if (port) {
 690                zfcp_erp_port_forced_reopen(port, 0, "sctrpi1");
 691                put_device(&port->dev);
 692        } else {
 693                zfcp_erp_port_forced_no_port_dbf(
 694                        "sctrpin", adapter,
 695                        rport->port_name /* zfcp_scsi_rport_register */,
 696                        rport->port_id /* zfcp_scsi_rport_register */);
 697        }
 698}
 699
 700static void zfcp_scsi_rport_register(struct zfcp_port *port)
 701{
 702        struct fc_rport_identifiers ids;
 703        struct fc_rport *rport;
 704
 705        if (port->rport)
 706                return;
 707
 708        ids.node_name = port->wwnn;
 709        ids.port_name = port->wwpn;
 710        ids.port_id = port->d_id;
 711        ids.roles = FC_RPORT_ROLE_FCP_TARGET;
 712
 713        zfcp_dbf_rec_trig_lock("scpaddy", port->adapter, port, NULL,
 714                               ZFCP_PSEUDO_ERP_ACTION_RPORT_ADD,
 715                               ZFCP_PSEUDO_ERP_ACTION_RPORT_ADD);
 716        rport = fc_remote_port_add(port->adapter->scsi_host, 0, &ids);
 717        if (!rport) {
 718                dev_err(&port->adapter->ccw_device->dev,
 719                        "Registering port 0x%016Lx failed\n",
 720                        (unsigned long long)port->wwpn);
 721                return;
 722        }
 723
 724        rport->maxframe_size = port->maxframe_size;
 725        rport->supported_classes = port->supported_classes;
 726        port->rport = rport;
 727        port->starget_id = rport->scsi_target_id;
 728
 729        zfcp_unit_queue_scsi_scan(port);
 730}
 731
 732static void zfcp_scsi_rport_block(struct zfcp_port *port)
 733{
 734        struct fc_rport *rport = port->rport;
 735
 736        if (rport) {
 737                zfcp_dbf_rec_trig_lock("scpdely", port->adapter, port, NULL,
 738                                       ZFCP_PSEUDO_ERP_ACTION_RPORT_DEL,
 739                                       ZFCP_PSEUDO_ERP_ACTION_RPORT_DEL);
 740                fc_remote_port_delete(rport);
 741                port->rport = NULL;
 742        }
 743}
 744
 745void zfcp_scsi_schedule_rport_register(struct zfcp_port *port)
 746{
 747        get_device(&port->dev);
 748        port->rport_task = RPORT_ADD;
 749
 750        if (!queue_work(port->adapter->work_queue, &port->rport_work))
 751                put_device(&port->dev);
 752}
 753
 754void zfcp_scsi_schedule_rport_block(struct zfcp_port *port)
 755{
 756        get_device(&port->dev);
 757        port->rport_task = RPORT_DEL;
 758
 759        if (port->rport && queue_work(port->adapter->work_queue,
 760                                      &port->rport_work))
 761                return;
 762
 763        put_device(&port->dev);
 764}
 765
 766void zfcp_scsi_schedule_rports_block(struct zfcp_adapter *adapter)
 767{
 768        unsigned long flags;
 769        struct zfcp_port *port;
 770
 771        read_lock_irqsave(&adapter->port_list_lock, flags);
 772        list_for_each_entry(port, &adapter->port_list, list)
 773                zfcp_scsi_schedule_rport_block(port);
 774        read_unlock_irqrestore(&adapter->port_list_lock, flags);
 775}
 776
 777void zfcp_scsi_rport_work(struct work_struct *work)
 778{
 779        struct zfcp_port *port = container_of(work, struct zfcp_port,
 780                                              rport_work);
 781
 782        set_worker_desc("zrp%c-%16llx",
 783                        (port->rport_task == RPORT_ADD) ? 'a' : 'd',
 784                        port->wwpn); /* < WORKER_DESC_LEN=24 */
 785        while (port->rport_task) {
 786                if (port->rport_task == RPORT_ADD) {
 787                        port->rport_task = RPORT_NONE;
 788                        zfcp_scsi_rport_register(port);
 789                } else {
 790                        port->rport_task = RPORT_NONE;
 791                        zfcp_scsi_rport_block(port);
 792                }
 793        }
 794
 795        put_device(&port->dev);
 796}
 797
 798/**
 799 * zfcp_scsi_set_prot - Configure DIF/DIX support in scsi_host
 800 * @adapter: The adapter where to configure DIF/DIX for the SCSI host
 801 */
 802void zfcp_scsi_set_prot(struct zfcp_adapter *adapter)
 803{
 804        unsigned int mask = 0;
 805        unsigned int data_div;
 806        struct Scsi_Host *shost = adapter->scsi_host;
 807
 808        data_div = atomic_read(&adapter->status) &
 809                   ZFCP_STATUS_ADAPTER_DATA_DIV_ENABLED;
 810
 811        if ((enable_dif || zfcp_experimental_dix) &&
 812            adapter->adapter_features & FSF_FEATURE_DIF_PROT_TYPE1)
 813                mask |= SHOST_DIF_TYPE1_PROTECTION;
 814
 815        if (zfcp_experimental_dix && data_div &&
 816            adapter->adapter_features & FSF_FEATURE_DIX_PROT_TCPIP) {
 817                mask |= SHOST_DIX_TYPE1_PROTECTION;
 818                scsi_host_set_guard(shost, SHOST_DIX_GUARD_IP);
 819                shost->sg_prot_tablesize = adapter->qdio->max_sbale_per_req / 2;
 820                shost->sg_tablesize = adapter->qdio->max_sbale_per_req / 2;
 821                shost->max_sectors = shost->sg_tablesize * 8;
 822        }
 823
 824        scsi_host_set_prot(shost, mask);
 825}
 826
 827/**
 828 * zfcp_scsi_dif_sense_error - Report DIF/DIX error as driver sense error
 829 * @scmd: The SCSI command to report the error for
 830 * @ascq: The ASCQ to put in the sense buffer
 831 *
 832 * See the error handling in sd_done for the sense codes used here.
 833 * Set DID_SOFT_ERROR to retry the request, if possible.
 834 */
 835void zfcp_scsi_dif_sense_error(struct scsi_cmnd *scmd, int ascq)
 836{
 837        scsi_build_sense_buffer(1, scmd->sense_buffer,
 838                                ILLEGAL_REQUEST, 0x10, ascq);
 839        set_driver_byte(scmd, DRIVER_SENSE);
 840        scmd->result |= SAM_STAT_CHECK_CONDITION;
 841        set_host_byte(scmd, DID_SOFT_ERROR);
 842}
 843
 844struct fc_function_template zfcp_transport_functions = {
 845        .show_starget_port_id = 1,
 846        .show_starget_port_name = 1,
 847        .show_starget_node_name = 1,
 848        .show_rport_supported_classes = 1,
 849        .show_rport_maxframe_size = 1,
 850        .show_rport_dev_loss_tmo = 1,
 851        .show_host_node_name = 1,
 852        .show_host_port_name = 1,
 853        .show_host_permanent_port_name = 1,
 854        .show_host_supported_classes = 1,
 855        .show_host_supported_fc4s = 1,
 856        .show_host_supported_speeds = 1,
 857        .show_host_maxframe_size = 1,
 858        .show_host_serial_number = 1,
 859        .show_host_manufacturer = 1,
 860        .show_host_model = 1,
 861        .show_host_hardware_version = 1,
 862        .show_host_firmware_version = 1,
 863        .get_fc_host_stats = zfcp_scsi_get_fc_host_stats,
 864        .reset_fc_host_stats = zfcp_scsi_reset_fc_host_stats,
 865        .set_rport_dev_loss_tmo = zfcp_scsi_set_rport_dev_loss_tmo,
 866        .get_host_port_state = zfcp_scsi_get_host_port_state,
 867        .terminate_rport_io = zfcp_scsi_terminate_rport_io,
 868        .show_host_port_state = 1,
 869        .show_host_active_fc4s = 1,
 870        .bsg_request = zfcp_fc_exec_bsg_job,
 871        .bsg_timeout = zfcp_fc_timeout_bsg_job,
 872        /* no functions registered for following dynamic attributes but
 873           directly set by LLDD */
 874        .show_host_port_type = 1,
 875        .show_host_symbolic_name = 1,
 876        .show_host_speed = 1,
 877        .show_host_port_id = 1,
 878        .show_host_fabric_name = 1,
 879        .dd_bsg_size = sizeof(struct zfcp_fsf_ct_els),
 880};
 881