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