linux/drivers/target/target_core_stat.c
<<
>>
Prefs
   1/*******************************************************************************
   2 * Filename:  target_core_stat.c
   3 *
   4 * Modern ConfigFS group context specific statistics based on original
   5 * target_core_mib.c code
   6 *
   7 * (c) Copyright 2006-2013 Datera, Inc.
   8 *
   9 * Nicholas A. Bellinger <nab@linux-iscsi.org>
  10 *
  11 * This program is free software; you can redistribute it and/or modify
  12 * it under the terms of the GNU General Public License as published by
  13 * the Free Software Foundation; either version 2 of the License, or
  14 * (at your option) any later version.
  15 *
  16 * This program is distributed in the hope that it will be useful,
  17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  19 * GNU General Public License for more details.
  20 *
  21 * You should have received a copy of the GNU General Public License
  22 * along with this program; if not, write to the Free Software
  23 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  24 *
  25 ******************************************************************************/
  26
  27#include <linux/kernel.h>
  28#include <linux/module.h>
  29#include <linux/delay.h>
  30#include <linux/timer.h>
  31#include <linux/string.h>
  32#include <linux/utsname.h>
  33#include <linux/proc_fs.h>
  34#include <linux/seq_file.h>
  35#include <linux/configfs.h>
  36#include <scsi/scsi.h>
  37#include <scsi/scsi_device.h>
  38#include <scsi/scsi_host.h>
  39
  40#include <target/target_core_base.h>
  41#include <target/target_core_backend.h>
  42#include <target/target_core_fabric.h>
  43
  44#include "target_core_internal.h"
  45
  46#ifndef INITIAL_JIFFIES
  47#define INITIAL_JIFFIES ((unsigned long)(unsigned int) (-300*HZ))
  48#endif
  49
  50#define NONE            "None"
  51#define ISPRINT(a)   ((a >= ' ') && (a <= '~'))
  52
  53#define SCSI_LU_INDEX                   1
  54#define LU_COUNT                        1
  55
  56/*
  57 * SCSI Device Table
  58 */
  59
  60static struct se_device *to_stat_dev(struct config_item *item)
  61{
  62        struct se_dev_stat_grps *sgrps = container_of(to_config_group(item),
  63                        struct se_dev_stat_grps, scsi_dev_group);
  64        return container_of(sgrps, struct se_device, dev_stat_grps);
  65}
  66
  67static ssize_t target_stat_inst_show(struct config_item *item, char *page)
  68{
  69        struct se_hba *hba = to_stat_dev(item)->se_hba;
  70
  71        return snprintf(page, PAGE_SIZE, "%u\n", hba->hba_index);
  72}
  73
  74static ssize_t target_stat_indx_show(struct config_item *item, char *page)
  75{
  76        return snprintf(page, PAGE_SIZE, "%u\n", to_stat_dev(item)->dev_index);
  77}
  78
  79static ssize_t target_stat_role_show(struct config_item *item, char *page)
  80{
  81        return snprintf(page, PAGE_SIZE, "Target\n");
  82}
  83
  84static ssize_t target_stat_ports_show(struct config_item *item, char *page)
  85{
  86        return snprintf(page, PAGE_SIZE, "%u\n", to_stat_dev(item)->export_count);
  87}
  88
  89CONFIGFS_ATTR_RO(target_stat_, inst);
  90CONFIGFS_ATTR_RO(target_stat_, indx);
  91CONFIGFS_ATTR_RO(target_stat_, role);
  92CONFIGFS_ATTR_RO(target_stat_, ports);
  93
  94static struct configfs_attribute *target_stat_scsi_dev_attrs[] = {
  95        &target_stat_attr_inst,
  96        &target_stat_attr_indx,
  97        &target_stat_attr_role,
  98        &target_stat_attr_ports,
  99        NULL,
 100};
 101
 102static struct config_item_type target_stat_scsi_dev_cit = {
 103        .ct_attrs               = target_stat_scsi_dev_attrs,
 104        .ct_owner               = THIS_MODULE,
 105};
 106
 107/*
 108 * SCSI Target Device Table
 109 */
 110static struct se_device *to_stat_tgt_dev(struct config_item *item)
 111{
 112        struct se_dev_stat_grps *sgrps = container_of(to_config_group(item),
 113                        struct se_dev_stat_grps, scsi_tgt_dev_group);
 114        return container_of(sgrps, struct se_device, dev_stat_grps);
 115}
 116
 117static ssize_t target_stat_tgt_inst_show(struct config_item *item, char *page)
 118{
 119        struct se_hba *hba = to_stat_tgt_dev(item)->se_hba;
 120
 121        return snprintf(page, PAGE_SIZE, "%u\n", hba->hba_index);
 122}
 123
 124static ssize_t target_stat_tgt_indx_show(struct config_item *item, char *page)
 125{
 126        return snprintf(page, PAGE_SIZE, "%u\n", to_stat_tgt_dev(item)->dev_index);
 127}
 128
 129static ssize_t target_stat_tgt_num_lus_show(struct config_item *item,
 130                char *page)
 131{
 132        return snprintf(page, PAGE_SIZE, "%u\n", LU_COUNT);
 133}
 134
 135static ssize_t target_stat_tgt_status_show(struct config_item *item,
 136                char *page)
 137{
 138        if (to_stat_tgt_dev(item)->export_count)
 139                return snprintf(page, PAGE_SIZE, "activated");
 140        else
 141                return snprintf(page, PAGE_SIZE, "deactivated");
 142}
 143
 144static ssize_t target_stat_tgt_non_access_lus_show(struct config_item *item,
 145                char *page)
 146{
 147        int non_accessible_lus;
 148
 149        if (to_stat_tgt_dev(item)->export_count)
 150                non_accessible_lus = 0;
 151        else
 152                non_accessible_lus = 1;
 153
 154        return snprintf(page, PAGE_SIZE, "%u\n", non_accessible_lus);
 155}
 156
 157static ssize_t target_stat_tgt_resets_show(struct config_item *item,
 158                char *page)
 159{
 160        return snprintf(page, PAGE_SIZE, "%lu\n",
 161                        atomic_long_read(&to_stat_tgt_dev(item)->num_resets));
 162}
 163
 164CONFIGFS_ATTR_RO(target_stat_tgt_, inst);
 165CONFIGFS_ATTR_RO(target_stat_tgt_, indx);
 166CONFIGFS_ATTR_RO(target_stat_tgt_, num_lus);
 167CONFIGFS_ATTR_RO(target_stat_tgt_, status);
 168CONFIGFS_ATTR_RO(target_stat_tgt_, non_access_lus);
 169CONFIGFS_ATTR_RO(target_stat_tgt_, resets);
 170
 171static struct configfs_attribute *target_stat_scsi_tgt_dev_attrs[] = {
 172        &target_stat_tgt_attr_inst,
 173        &target_stat_tgt_attr_indx,
 174        &target_stat_tgt_attr_num_lus,
 175        &target_stat_tgt_attr_status,
 176        &target_stat_tgt_attr_non_access_lus,
 177        &target_stat_tgt_attr_resets,
 178        NULL,
 179};
 180
 181static struct config_item_type target_stat_scsi_tgt_dev_cit = {
 182        .ct_attrs               = target_stat_scsi_tgt_dev_attrs,
 183        .ct_owner               = THIS_MODULE,
 184};
 185
 186/*
 187 * SCSI Logical Unit Table
 188 */
 189
 190static struct se_device *to_stat_lu_dev(struct config_item *item)
 191{
 192        struct se_dev_stat_grps *sgrps = container_of(to_config_group(item),
 193                        struct se_dev_stat_grps, scsi_lu_group);
 194        return container_of(sgrps, struct se_device, dev_stat_grps);
 195}
 196
 197static ssize_t target_stat_lu_inst_show(struct config_item *item, char *page)
 198{
 199        struct se_hba *hba = to_stat_lu_dev(item)->se_hba;
 200
 201        return snprintf(page, PAGE_SIZE, "%u\n", hba->hba_index);
 202}
 203
 204static ssize_t target_stat_lu_dev_show(struct config_item *item, char *page)
 205{
 206        return snprintf(page, PAGE_SIZE, "%u\n",
 207                        to_stat_lu_dev(item)->dev_index);
 208}
 209
 210static ssize_t target_stat_lu_indx_show(struct config_item *item, char *page)
 211{
 212        return snprintf(page, PAGE_SIZE, "%u\n", SCSI_LU_INDEX);
 213}
 214
 215static ssize_t target_stat_lu_lun_show(struct config_item *item, char *page)
 216{
 217        /* FIXME: scsiLuDefaultLun */
 218        return snprintf(page, PAGE_SIZE, "%llu\n", (unsigned long long)0);
 219}
 220
 221static ssize_t target_stat_lu_lu_name_show(struct config_item *item, char *page)
 222{
 223        struct se_device *dev = to_stat_lu_dev(item);
 224
 225        /* scsiLuWwnName */
 226        return snprintf(page, PAGE_SIZE, "%s\n",
 227                        (strlen(dev->t10_wwn.unit_serial)) ?
 228                        dev->t10_wwn.unit_serial : "None");
 229}
 230
 231static ssize_t target_stat_lu_vend_show(struct config_item *item, char *page)
 232{
 233        struct se_device *dev = to_stat_lu_dev(item);
 234        int i;
 235        char str[sizeof(dev->t10_wwn.vendor)+1];
 236
 237        /* scsiLuVendorId */
 238        for (i = 0; i < sizeof(dev->t10_wwn.vendor); i++)
 239                str[i] = ISPRINT(dev->t10_wwn.vendor[i]) ?
 240                        dev->t10_wwn.vendor[i] : ' ';
 241        str[i] = '\0';
 242        return snprintf(page, PAGE_SIZE, "%s\n", str);
 243}
 244
 245static ssize_t target_stat_lu_prod_show(struct config_item *item, char *page)
 246{
 247        struct se_device *dev = to_stat_lu_dev(item);
 248        int i;
 249        char str[sizeof(dev->t10_wwn.model)+1];
 250
 251        /* scsiLuProductId */
 252        for (i = 0; i < sizeof(dev->t10_wwn.model); i++)
 253                str[i] = ISPRINT(dev->t10_wwn.model[i]) ?
 254                        dev->t10_wwn.model[i] : ' ';
 255        str[i] = '\0';
 256        return snprintf(page, PAGE_SIZE, "%s\n", str);
 257}
 258
 259static ssize_t target_stat_lu_rev_show(struct config_item *item, char *page)
 260{
 261        struct se_device *dev = to_stat_lu_dev(item);
 262        int i;
 263        char str[sizeof(dev->t10_wwn.revision)+1];
 264
 265        /* scsiLuRevisionId */
 266        for (i = 0; i < sizeof(dev->t10_wwn.revision); i++)
 267                str[i] = ISPRINT(dev->t10_wwn.revision[i]) ?
 268                        dev->t10_wwn.revision[i] : ' ';
 269        str[i] = '\0';
 270        return snprintf(page, PAGE_SIZE, "%s\n", str);
 271}
 272
 273static ssize_t target_stat_lu_dev_type_show(struct config_item *item, char *page)
 274{
 275        struct se_device *dev = to_stat_lu_dev(item);
 276
 277        /* scsiLuPeripheralType */
 278        return snprintf(page, PAGE_SIZE, "%u\n",
 279                        dev->transport->get_device_type(dev));
 280}
 281
 282static ssize_t target_stat_lu_status_show(struct config_item *item, char *page)
 283{
 284        struct se_device *dev = to_stat_lu_dev(item);
 285
 286        /* scsiLuStatus */
 287        return snprintf(page, PAGE_SIZE, "%s\n",
 288                (dev->export_count) ? "available" : "notavailable");
 289}
 290
 291static ssize_t target_stat_lu_state_bit_show(struct config_item *item,
 292                char *page)
 293{
 294        /* scsiLuState */
 295        return snprintf(page, PAGE_SIZE, "exposed\n");
 296}
 297
 298static ssize_t target_stat_lu_num_cmds_show(struct config_item *item,
 299                char *page)
 300{
 301        struct se_device *dev = to_stat_lu_dev(item);
 302
 303        /* scsiLuNumCommands */
 304        return snprintf(page, PAGE_SIZE, "%lu\n",
 305                        atomic_long_read(&dev->num_cmds));
 306}
 307
 308static ssize_t target_stat_lu_read_mbytes_show(struct config_item *item,
 309                char *page)
 310{
 311        struct se_device *dev = to_stat_lu_dev(item);
 312
 313        /* scsiLuReadMegaBytes */
 314        return snprintf(page, PAGE_SIZE, "%lu\n",
 315                        atomic_long_read(&dev->read_bytes) >> 20);
 316}
 317
 318static ssize_t target_stat_lu_write_mbytes_show(struct config_item *item,
 319                char *page)
 320{
 321        struct se_device *dev = to_stat_lu_dev(item);
 322
 323        /* scsiLuWrittenMegaBytes */
 324        return snprintf(page, PAGE_SIZE, "%lu\n",
 325                        atomic_long_read(&dev->write_bytes) >> 20);
 326}
 327
 328static ssize_t target_stat_lu_resets_show(struct config_item *item, char *page)
 329{
 330        struct se_device *dev = to_stat_lu_dev(item);
 331
 332        /* scsiLuInResets */
 333        return snprintf(page, PAGE_SIZE, "%lu\n",
 334                atomic_long_read(&dev->num_resets));
 335}
 336
 337static ssize_t target_stat_lu_full_stat_show(struct config_item *item,
 338                char *page)
 339{
 340        /* FIXME: scsiLuOutTaskSetFullStatus */
 341        return snprintf(page, PAGE_SIZE, "%u\n", 0);
 342}
 343
 344static ssize_t target_stat_lu_hs_num_cmds_show(struct config_item *item,
 345                char *page)
 346{
 347        /* FIXME: scsiLuHSInCommands */
 348        return snprintf(page, PAGE_SIZE, "%u\n", 0);
 349}
 350
 351static ssize_t target_stat_lu_creation_time_show(struct config_item *item,
 352                char *page)
 353{
 354        struct se_device *dev = to_stat_lu_dev(item);
 355
 356        /* scsiLuCreationTime */
 357        return snprintf(page, PAGE_SIZE, "%u\n", (u32)(((u32)dev->creation_time -
 358                                INITIAL_JIFFIES) * 100 / HZ));
 359}
 360
 361CONFIGFS_ATTR_RO(target_stat_lu_, inst);
 362CONFIGFS_ATTR_RO(target_stat_lu_, dev);
 363CONFIGFS_ATTR_RO(target_stat_lu_, indx);
 364CONFIGFS_ATTR_RO(target_stat_lu_, lun);
 365CONFIGFS_ATTR_RO(target_stat_lu_, lu_name);
 366CONFIGFS_ATTR_RO(target_stat_lu_, vend);
 367CONFIGFS_ATTR_RO(target_stat_lu_, prod);
 368CONFIGFS_ATTR_RO(target_stat_lu_, rev);
 369CONFIGFS_ATTR_RO(target_stat_lu_, dev_type);
 370CONFIGFS_ATTR_RO(target_stat_lu_, status);
 371CONFIGFS_ATTR_RO(target_stat_lu_, state_bit);
 372CONFIGFS_ATTR_RO(target_stat_lu_, num_cmds);
 373CONFIGFS_ATTR_RO(target_stat_lu_, read_mbytes);
 374CONFIGFS_ATTR_RO(target_stat_lu_, write_mbytes);
 375CONFIGFS_ATTR_RO(target_stat_lu_, resets);
 376CONFIGFS_ATTR_RO(target_stat_lu_, full_stat);
 377CONFIGFS_ATTR_RO(target_stat_lu_, hs_num_cmds);
 378CONFIGFS_ATTR_RO(target_stat_lu_, creation_time);
 379
 380static struct configfs_attribute *target_stat_scsi_lu_attrs[] = {
 381        &target_stat_lu_attr_inst,
 382        &target_stat_lu_attr_dev,
 383        &target_stat_lu_attr_indx,
 384        &target_stat_lu_attr_lun,
 385        &target_stat_lu_attr_lu_name,
 386        &target_stat_lu_attr_vend,
 387        &target_stat_lu_attr_prod,
 388        &target_stat_lu_attr_rev,
 389        &target_stat_lu_attr_dev_type,
 390        &target_stat_lu_attr_status,
 391        &target_stat_lu_attr_state_bit,
 392        &target_stat_lu_attr_num_cmds,
 393        &target_stat_lu_attr_read_mbytes,
 394        &target_stat_lu_attr_write_mbytes,
 395        &target_stat_lu_attr_resets,
 396        &target_stat_lu_attr_full_stat,
 397        &target_stat_lu_attr_hs_num_cmds,
 398        &target_stat_lu_attr_creation_time,
 399        NULL,
 400};
 401
 402static struct config_item_type target_stat_scsi_lu_cit = {
 403        .ct_attrs               = target_stat_scsi_lu_attrs,
 404        .ct_owner               = THIS_MODULE,
 405};
 406
 407/*
 408 * Called from target_core_configfs.c:target_core_make_subdev() to setup
 409 * the target statistics groups + configfs CITs located in target_core_stat.c
 410 */
 411void target_stat_setup_dev_default_groups(struct se_device *dev)
 412{
 413        config_group_init_type_name(&dev->dev_stat_grps.scsi_dev_group,
 414                        "scsi_dev", &target_stat_scsi_dev_cit);
 415        configfs_add_default_group(&dev->dev_stat_grps.scsi_dev_group,
 416                        &dev->dev_stat_grps.stat_group);
 417
 418        config_group_init_type_name(&dev->dev_stat_grps.scsi_tgt_dev_group,
 419                        "scsi_tgt_dev", &target_stat_scsi_tgt_dev_cit);
 420        configfs_add_default_group(&dev->dev_stat_grps.scsi_tgt_dev_group,
 421                        &dev->dev_stat_grps.stat_group);
 422
 423        config_group_init_type_name(&dev->dev_stat_grps.scsi_lu_group,
 424                        "scsi_lu", &target_stat_scsi_lu_cit);
 425        configfs_add_default_group(&dev->dev_stat_grps.scsi_lu_group,
 426                        &dev->dev_stat_grps.stat_group);
 427}
 428
 429/*
 430 * SCSI Port Table
 431 */
 432
 433static struct se_lun *to_stat_port(struct config_item *item)
 434{
 435        struct se_port_stat_grps *pgrps = container_of(to_config_group(item),
 436                        struct se_port_stat_grps, scsi_port_group);
 437        return container_of(pgrps, struct se_lun, port_stat_grps);
 438}
 439
 440static ssize_t target_stat_port_inst_show(struct config_item *item, char *page)
 441{
 442        struct se_lun *lun = to_stat_port(item);
 443        struct se_device *dev;
 444        ssize_t ret = -ENODEV;
 445
 446        rcu_read_lock();
 447        dev = rcu_dereference(lun->lun_se_dev);
 448        if (dev)
 449                ret = snprintf(page, PAGE_SIZE, "%u\n", dev->hba_index);
 450        rcu_read_unlock();
 451        return ret;
 452}
 453
 454static ssize_t target_stat_port_dev_show(struct config_item *item, char *page)
 455{
 456        struct se_lun *lun = to_stat_port(item);
 457        struct se_device *dev;
 458        ssize_t ret = -ENODEV;
 459
 460        rcu_read_lock();
 461        dev = rcu_dereference(lun->lun_se_dev);
 462        if (dev)
 463                ret = snprintf(page, PAGE_SIZE, "%u\n", dev->dev_index);
 464        rcu_read_unlock();
 465        return ret;
 466}
 467
 468static ssize_t target_stat_port_indx_show(struct config_item *item, char *page)
 469{
 470        struct se_lun *lun = to_stat_port(item);
 471        struct se_device *dev;
 472        ssize_t ret = -ENODEV;
 473
 474        rcu_read_lock();
 475        dev = rcu_dereference(lun->lun_se_dev);
 476        if (dev)
 477                ret = snprintf(page, PAGE_SIZE, "%u\n", lun->lun_rtpi);
 478        rcu_read_unlock();
 479        return ret;
 480}
 481
 482static ssize_t target_stat_port_role_show(struct config_item *item, char *page)
 483{
 484        struct se_lun *lun = to_stat_port(item);
 485        struct se_device *dev;
 486        ssize_t ret = -ENODEV;
 487
 488        rcu_read_lock();
 489        dev = rcu_dereference(lun->lun_se_dev);
 490        if (dev)
 491                ret = snprintf(page, PAGE_SIZE, "%s%u\n", "Device", dev->dev_index);
 492        rcu_read_unlock();
 493        return ret;
 494}
 495
 496static ssize_t target_stat_port_busy_count_show(struct config_item *item,
 497                char *page)
 498{
 499        struct se_lun *lun = to_stat_port(item);
 500        struct se_device *dev;
 501        ssize_t ret = -ENODEV;
 502
 503        rcu_read_lock();
 504        dev = rcu_dereference(lun->lun_se_dev);
 505        if (dev) {
 506                /* FIXME: scsiPortBusyStatuses  */
 507                ret = snprintf(page, PAGE_SIZE, "%u\n", 0);
 508        }
 509        rcu_read_unlock();
 510        return ret;
 511}
 512
 513CONFIGFS_ATTR_RO(target_stat_port_, inst);
 514CONFIGFS_ATTR_RO(target_stat_port_, dev);
 515CONFIGFS_ATTR_RO(target_stat_port_, indx);
 516CONFIGFS_ATTR_RO(target_stat_port_, role);
 517CONFIGFS_ATTR_RO(target_stat_port_, busy_count);
 518
 519static struct configfs_attribute *target_stat_scsi_port_attrs[] = {
 520        &target_stat_port_attr_inst,
 521        &target_stat_port_attr_dev,
 522        &target_stat_port_attr_indx,
 523        &target_stat_port_attr_role,
 524        &target_stat_port_attr_busy_count,
 525        NULL,
 526};
 527
 528static struct config_item_type target_stat_scsi_port_cit = {
 529        .ct_attrs               = target_stat_scsi_port_attrs,
 530        .ct_owner               = THIS_MODULE,
 531};
 532
 533/*
 534 * SCSI Target Port Table
 535 */
 536static struct se_lun *to_stat_tgt_port(struct config_item *item)
 537{
 538        struct se_port_stat_grps *pgrps = container_of(to_config_group(item),
 539                        struct se_port_stat_grps, scsi_tgt_port_group);
 540        return container_of(pgrps, struct se_lun, port_stat_grps);
 541}
 542
 543static ssize_t target_stat_tgt_port_inst_show(struct config_item *item,
 544                char *page)
 545{
 546        struct se_lun *lun = to_stat_tgt_port(item);
 547        struct se_device *dev;
 548        ssize_t ret = -ENODEV;
 549
 550        rcu_read_lock();
 551        dev = rcu_dereference(lun->lun_se_dev);
 552        if (dev)
 553                ret = snprintf(page, PAGE_SIZE, "%u\n", dev->hba_index);
 554        rcu_read_unlock();
 555        return ret;
 556}
 557
 558static ssize_t target_stat_tgt_port_dev_show(struct config_item *item,
 559                char *page)
 560{
 561        struct se_lun *lun = to_stat_tgt_port(item);
 562        struct se_device *dev;
 563        ssize_t ret = -ENODEV;
 564
 565        rcu_read_lock();
 566        dev = rcu_dereference(lun->lun_se_dev);
 567        if (dev)
 568                ret = snprintf(page, PAGE_SIZE, "%u\n", dev->dev_index);
 569        rcu_read_unlock();
 570        return ret;
 571}
 572
 573static ssize_t target_stat_tgt_port_indx_show(struct config_item *item,
 574                char *page)
 575{
 576        struct se_lun *lun = to_stat_tgt_port(item);
 577        struct se_device *dev;
 578        ssize_t ret = -ENODEV;
 579
 580        rcu_read_lock();
 581        dev = rcu_dereference(lun->lun_se_dev);
 582        if (dev)
 583                ret = snprintf(page, PAGE_SIZE, "%u\n", lun->lun_rtpi);
 584        rcu_read_unlock();
 585        return ret;
 586}
 587
 588static ssize_t target_stat_tgt_port_name_show(struct config_item *item,
 589                char *page)
 590{
 591        struct se_lun *lun = to_stat_tgt_port(item);
 592        struct se_portal_group *tpg = lun->lun_tpg;
 593        struct se_device *dev;
 594        ssize_t ret = -ENODEV;
 595
 596        rcu_read_lock();
 597        dev = rcu_dereference(lun->lun_se_dev);
 598        if (dev)
 599                ret = snprintf(page, PAGE_SIZE, "%sPort#%u\n",
 600                        tpg->se_tpg_tfo->get_fabric_name(),
 601                        lun->lun_rtpi);
 602        rcu_read_unlock();
 603        return ret;
 604}
 605
 606static ssize_t target_stat_tgt_port_port_index_show(struct config_item *item,
 607                char *page)
 608{
 609        struct se_lun *lun = to_stat_tgt_port(item);
 610        struct se_portal_group *tpg = lun->lun_tpg;
 611        struct se_device *dev;
 612        ssize_t ret = -ENODEV;
 613
 614        rcu_read_lock();
 615        dev = rcu_dereference(lun->lun_se_dev);
 616        if (dev)
 617                ret = snprintf(page, PAGE_SIZE, "%s%s%d\n",
 618                        tpg->se_tpg_tfo->tpg_get_wwn(tpg), "+t+",
 619                        tpg->se_tpg_tfo->tpg_get_tag(tpg));
 620        rcu_read_unlock();
 621        return ret;
 622}
 623
 624static ssize_t target_stat_tgt_port_in_cmds_show(struct config_item *item,
 625                char *page)
 626{
 627        struct se_lun *lun = to_stat_tgt_port(item);
 628        struct se_device *dev;
 629        ssize_t ret = -ENODEV;
 630
 631        rcu_read_lock();
 632        dev = rcu_dereference(lun->lun_se_dev);
 633        if (dev)
 634                ret = snprintf(page, PAGE_SIZE, "%lu\n",
 635                               atomic_long_read(&lun->lun_stats.cmd_pdus));
 636        rcu_read_unlock();
 637        return ret;
 638}
 639
 640static ssize_t target_stat_tgt_port_write_mbytes_show(struct config_item *item,
 641                char *page)
 642{
 643        struct se_lun *lun = to_stat_tgt_port(item);
 644        struct se_device *dev;
 645        ssize_t ret = -ENODEV;
 646
 647        rcu_read_lock();
 648        dev = rcu_dereference(lun->lun_se_dev);
 649        if (dev)
 650                ret = snprintf(page, PAGE_SIZE, "%u\n",
 651                        (u32)(atomic_long_read(&lun->lun_stats.rx_data_octets) >> 20));
 652        rcu_read_unlock();
 653        return ret;
 654}
 655
 656static ssize_t target_stat_tgt_port_read_mbytes_show(struct config_item *item,
 657                char *page)
 658{
 659        struct se_lun *lun = to_stat_tgt_port(item);
 660        struct se_device *dev;
 661        ssize_t ret = -ENODEV;
 662
 663        rcu_read_lock();
 664        dev = rcu_dereference(lun->lun_se_dev);
 665        if (dev)
 666                ret = snprintf(page, PAGE_SIZE, "%u\n",
 667                                (u32)(atomic_long_read(&lun->lun_stats.tx_data_octets) >> 20));
 668        rcu_read_unlock();
 669        return ret;
 670}
 671
 672static ssize_t target_stat_tgt_port_hs_in_cmds_show(struct config_item *item,
 673                char *page)
 674{
 675        struct se_lun *lun = to_stat_tgt_port(item);
 676        struct se_device *dev;
 677        ssize_t ret = -ENODEV;
 678
 679        rcu_read_lock();
 680        dev = rcu_dereference(lun->lun_se_dev);
 681        if (dev) {
 682                /* FIXME: scsiTgtPortHsInCommands */
 683                ret = snprintf(page, PAGE_SIZE, "%u\n", 0);
 684        }
 685        rcu_read_unlock();
 686        return ret;
 687}
 688
 689CONFIGFS_ATTR_RO(target_stat_tgt_port_, inst);
 690CONFIGFS_ATTR_RO(target_stat_tgt_port_, dev);
 691CONFIGFS_ATTR_RO(target_stat_tgt_port_, indx);
 692CONFIGFS_ATTR_RO(target_stat_tgt_port_, name);
 693CONFIGFS_ATTR_RO(target_stat_tgt_port_, port_index);
 694CONFIGFS_ATTR_RO(target_stat_tgt_port_, in_cmds);
 695CONFIGFS_ATTR_RO(target_stat_tgt_port_, write_mbytes);
 696CONFIGFS_ATTR_RO(target_stat_tgt_port_, read_mbytes);
 697CONFIGFS_ATTR_RO(target_stat_tgt_port_, hs_in_cmds);
 698
 699static struct configfs_attribute *target_stat_scsi_tgt_port_attrs[] = {
 700        &target_stat_tgt_port_attr_inst,
 701        &target_stat_tgt_port_attr_dev,
 702        &target_stat_tgt_port_attr_indx,
 703        &target_stat_tgt_port_attr_name,
 704        &target_stat_tgt_port_attr_port_index,
 705        &target_stat_tgt_port_attr_in_cmds,
 706        &target_stat_tgt_port_attr_write_mbytes,
 707        &target_stat_tgt_port_attr_read_mbytes,
 708        &target_stat_tgt_port_attr_hs_in_cmds,
 709        NULL,
 710};
 711
 712static struct config_item_type target_stat_scsi_tgt_port_cit = {
 713        .ct_attrs               = target_stat_scsi_tgt_port_attrs,
 714        .ct_owner               = THIS_MODULE,
 715};
 716
 717/*
 718 * SCSI Transport Table
 719 */
 720static struct se_lun *to_transport_stat(struct config_item *item)
 721{
 722        struct se_port_stat_grps *pgrps = container_of(to_config_group(item),
 723                        struct se_port_stat_grps, scsi_transport_group);
 724        return container_of(pgrps, struct se_lun, port_stat_grps);
 725}
 726
 727static ssize_t target_stat_transport_inst_show(struct config_item *item,
 728                char *page)
 729{
 730        struct se_lun *lun = to_transport_stat(item);
 731        struct se_device *dev;
 732        ssize_t ret = -ENODEV;
 733
 734        rcu_read_lock();
 735        dev = rcu_dereference(lun->lun_se_dev);
 736        if (dev)
 737                ret = snprintf(page, PAGE_SIZE, "%u\n", dev->hba_index);
 738        rcu_read_unlock();
 739        return ret;
 740}
 741
 742static ssize_t target_stat_transport_device_show(struct config_item *item,
 743                char *page)
 744{
 745        struct se_lun *lun = to_transport_stat(item);
 746        struct se_device *dev;
 747        struct se_portal_group *tpg = lun->lun_tpg;
 748        ssize_t ret = -ENODEV;
 749
 750        rcu_read_lock();
 751        dev = rcu_dereference(lun->lun_se_dev);
 752        if (dev) {
 753                /* scsiTransportType */
 754                ret = snprintf(page, PAGE_SIZE, "scsiTransport%s\n",
 755                               tpg->se_tpg_tfo->get_fabric_name());
 756        }
 757        rcu_read_unlock();
 758        return ret;
 759}
 760
 761static ssize_t target_stat_transport_indx_show(struct config_item *item,
 762                char *page)
 763{
 764        struct se_lun *lun = to_transport_stat(item);
 765        struct se_device *dev;
 766        struct se_portal_group *tpg = lun->lun_tpg;
 767        ssize_t ret = -ENODEV;
 768
 769        rcu_read_lock();
 770        dev = rcu_dereference(lun->lun_se_dev);
 771        if (dev)
 772                ret = snprintf(page, PAGE_SIZE, "%u\n",
 773                               tpg->se_tpg_tfo->tpg_get_inst_index(tpg));
 774        rcu_read_unlock();
 775        return ret;
 776}
 777
 778static ssize_t target_stat_transport_dev_name_show(struct config_item *item,
 779                char *page)
 780{
 781        struct se_lun *lun = to_transport_stat(item);
 782        struct se_device *dev;
 783        struct se_portal_group *tpg = lun->lun_tpg;
 784        struct t10_wwn *wwn;
 785        ssize_t ret = -ENODEV;
 786
 787        rcu_read_lock();
 788        dev = rcu_dereference(lun->lun_se_dev);
 789        if (dev) {
 790                wwn = &dev->t10_wwn;
 791                /* scsiTransportDevName */
 792                ret = snprintf(page, PAGE_SIZE, "%s+%s\n",
 793                                tpg->se_tpg_tfo->tpg_get_wwn(tpg),
 794                                (strlen(wwn->unit_serial)) ? wwn->unit_serial :
 795                                wwn->vendor);
 796        }
 797        rcu_read_unlock();
 798        return ret;
 799}
 800
 801static ssize_t target_stat_transport_proto_id_show(struct config_item *item, char *page)
 802{
 803        struct se_lun *lun = to_transport_stat(item);
 804        struct se_portal_group *tpg = lun->lun_tpg;
 805        struct se_device *dev;
 806        ssize_t ret = -ENODEV;
 807
 808        rcu_read_lock();
 809        dev = rcu_dereference(lun->lun_se_dev);
 810        if (dev)
 811                ret = snprintf(page, PAGE_SIZE, "%u\n", tpg->proto_id);
 812        rcu_read_unlock();
 813        return ret;
 814}
 815
 816CONFIGFS_ATTR_RO(target_stat_transport_, inst);
 817CONFIGFS_ATTR_RO(target_stat_transport_, device);
 818CONFIGFS_ATTR_RO(target_stat_transport_, indx);
 819CONFIGFS_ATTR_RO(target_stat_transport_, dev_name);
 820CONFIGFS_ATTR_RO(target_stat_transport_, proto_id);
 821
 822static struct configfs_attribute *target_stat_scsi_transport_attrs[] = {
 823        &target_stat_transport_attr_inst,
 824        &target_stat_transport_attr_device,
 825        &target_stat_transport_attr_indx,
 826        &target_stat_transport_attr_dev_name,
 827        &target_stat_transport_attr_proto_id,
 828        NULL,
 829};
 830
 831static struct config_item_type target_stat_scsi_transport_cit = {
 832        .ct_attrs               = target_stat_scsi_transport_attrs,
 833        .ct_owner               = THIS_MODULE,
 834};
 835
 836/*
 837 * Called from target_core_fabric_configfs.c:target_fabric_make_lun() to setup
 838 * the target port statistics groups + configfs CITs located in target_core_stat.c
 839 */
 840void target_stat_setup_port_default_groups(struct se_lun *lun)
 841{
 842        config_group_init_type_name(&lun->port_stat_grps.scsi_port_group,
 843                        "scsi_port", &target_stat_scsi_port_cit);
 844        configfs_add_default_group(&lun->port_stat_grps.scsi_port_group,
 845                        &lun->port_stat_grps.stat_group);
 846
 847        config_group_init_type_name(&lun->port_stat_grps.scsi_tgt_port_group,
 848                        "scsi_tgt_port", &target_stat_scsi_tgt_port_cit);
 849        configfs_add_default_group(&lun->port_stat_grps.scsi_tgt_port_group,
 850                        &lun->port_stat_grps.stat_group);
 851
 852        config_group_init_type_name(&lun->port_stat_grps.scsi_transport_group,
 853                        "scsi_transport", &target_stat_scsi_transport_cit);
 854        configfs_add_default_group(&lun->port_stat_grps.scsi_transport_group,
 855                        &lun->port_stat_grps.stat_group);
 856}
 857
 858/*
 859 * SCSI Authorized Initiator Table
 860 */
 861
 862static struct se_lun_acl *auth_to_lacl(struct config_item *item)
 863{
 864        struct se_ml_stat_grps *lgrps = container_of(to_config_group(item),
 865                        struct se_ml_stat_grps, scsi_auth_intr_group);
 866        return container_of(lgrps, struct se_lun_acl, ml_stat_grps);
 867}
 868
 869static ssize_t target_stat_auth_inst_show(struct config_item *item,
 870                char *page)
 871{
 872        struct se_lun_acl *lacl = auth_to_lacl(item);
 873        struct se_node_acl *nacl = lacl->se_lun_nacl;
 874        struct se_dev_entry *deve;
 875        struct se_portal_group *tpg;
 876        ssize_t ret;
 877
 878        rcu_read_lock();
 879        deve = target_nacl_find_deve(nacl, lacl->mapped_lun);
 880        if (!deve) {
 881                rcu_read_unlock();
 882                return -ENODEV;
 883        }
 884        tpg = nacl->se_tpg;
 885        /* scsiInstIndex */
 886        ret = snprintf(page, PAGE_SIZE, "%u\n",
 887                        tpg->se_tpg_tfo->tpg_get_inst_index(tpg));
 888        rcu_read_unlock();
 889        return ret;
 890}
 891
 892static ssize_t target_stat_auth_dev_show(struct config_item *item,
 893                char *page)
 894{
 895        struct se_lun_acl *lacl = auth_to_lacl(item);
 896        struct se_node_acl *nacl = lacl->se_lun_nacl;
 897        struct se_dev_entry *deve;
 898        struct se_lun *lun;
 899        ssize_t ret;
 900
 901        rcu_read_lock();
 902        deve = target_nacl_find_deve(nacl, lacl->mapped_lun);
 903        if (!deve) {
 904                rcu_read_unlock();
 905                return -ENODEV;
 906        }
 907        lun = rcu_dereference(deve->se_lun);
 908        /* scsiDeviceIndex */
 909        ret = snprintf(page, PAGE_SIZE, "%u\n", lun->lun_index);
 910        rcu_read_unlock();
 911        return ret;
 912}
 913
 914static ssize_t target_stat_auth_port_show(struct config_item *item,
 915                char *page)
 916{
 917        struct se_lun_acl *lacl = auth_to_lacl(item);
 918        struct se_node_acl *nacl = lacl->se_lun_nacl;
 919        struct se_dev_entry *deve;
 920        struct se_portal_group *tpg;
 921        ssize_t ret;
 922
 923        rcu_read_lock();
 924        deve = target_nacl_find_deve(nacl, lacl->mapped_lun);
 925        if (!deve) {
 926                rcu_read_unlock();
 927                return -ENODEV;
 928        }
 929        tpg = nacl->se_tpg;
 930        /* scsiAuthIntrTgtPortIndex */
 931        ret = snprintf(page, PAGE_SIZE, "%u\n", tpg->se_tpg_tfo->tpg_get_tag(tpg));
 932        rcu_read_unlock();
 933        return ret;
 934}
 935
 936static ssize_t target_stat_auth_indx_show(struct config_item *item,
 937                char *page)
 938{
 939        struct se_lun_acl *lacl = auth_to_lacl(item);
 940        struct se_node_acl *nacl = lacl->se_lun_nacl;
 941        struct se_dev_entry *deve;
 942        ssize_t ret;
 943
 944        rcu_read_lock();
 945        deve = target_nacl_find_deve(nacl, lacl->mapped_lun);
 946        if (!deve) {
 947                rcu_read_unlock();
 948                return -ENODEV;
 949        }
 950        /* scsiAuthIntrIndex */
 951        ret = snprintf(page, PAGE_SIZE, "%u\n", nacl->acl_index);
 952        rcu_read_unlock();
 953        return ret;
 954}
 955
 956static ssize_t target_stat_auth_dev_or_port_show(struct config_item *item,
 957                char *page)
 958{
 959        struct se_lun_acl *lacl = auth_to_lacl(item);
 960        struct se_node_acl *nacl = lacl->se_lun_nacl;
 961        struct se_dev_entry *deve;
 962        ssize_t ret;
 963
 964        rcu_read_lock();
 965        deve = target_nacl_find_deve(nacl, lacl->mapped_lun);
 966        if (!deve) {
 967                rcu_read_unlock();
 968                return -ENODEV;
 969        }
 970        /* scsiAuthIntrDevOrPort */
 971        ret = snprintf(page, PAGE_SIZE, "%u\n", 1);
 972        rcu_read_unlock();
 973        return ret;
 974}
 975
 976static ssize_t target_stat_auth_intr_name_show(struct config_item *item,
 977                char *page)
 978{
 979        struct se_lun_acl *lacl = auth_to_lacl(item);
 980        struct se_node_acl *nacl = lacl->se_lun_nacl;
 981        struct se_dev_entry *deve;
 982        ssize_t ret;
 983
 984        rcu_read_lock();
 985        deve = target_nacl_find_deve(nacl, lacl->mapped_lun);
 986        if (!deve) {
 987                rcu_read_unlock();
 988                return -ENODEV;
 989        }
 990        /* scsiAuthIntrName */
 991        ret = snprintf(page, PAGE_SIZE, "%s\n", nacl->initiatorname);
 992        rcu_read_unlock();
 993        return ret;
 994}
 995
 996static ssize_t target_stat_auth_map_indx_show(struct config_item *item,
 997                char *page)
 998{
 999        struct se_lun_acl *lacl = auth_to_lacl(item);
1000        struct se_node_acl *nacl = lacl->se_lun_nacl;
1001        struct se_dev_entry *deve;
1002        ssize_t ret;
1003
1004        rcu_read_lock();
1005        deve = target_nacl_find_deve(nacl, lacl->mapped_lun);
1006        if (!deve) {
1007                rcu_read_unlock();
1008                return -ENODEV;
1009        }
1010        /* FIXME: scsiAuthIntrLunMapIndex */
1011        ret = snprintf(page, PAGE_SIZE, "%u\n", 0);
1012        rcu_read_unlock();
1013        return ret;
1014}
1015
1016static ssize_t target_stat_auth_att_count_show(struct config_item *item,
1017                char *page)
1018{
1019        struct se_lun_acl *lacl = auth_to_lacl(item);
1020        struct se_node_acl *nacl = lacl->se_lun_nacl;
1021        struct se_dev_entry *deve;
1022        ssize_t ret;
1023
1024        rcu_read_lock();
1025        deve = target_nacl_find_deve(nacl, lacl->mapped_lun);
1026        if (!deve) {
1027                rcu_read_unlock();
1028                return -ENODEV;
1029        }
1030        /* scsiAuthIntrAttachedTimes */
1031        ret = snprintf(page, PAGE_SIZE, "%u\n", deve->attach_count);
1032        rcu_read_unlock();
1033        return ret;
1034}
1035
1036static ssize_t target_stat_auth_num_cmds_show(struct config_item *item,
1037                char *page)
1038{
1039        struct se_lun_acl *lacl = auth_to_lacl(item);
1040        struct se_node_acl *nacl = lacl->se_lun_nacl;
1041        struct se_dev_entry *deve;
1042        ssize_t ret;
1043
1044        rcu_read_lock();
1045        deve = target_nacl_find_deve(nacl, lacl->mapped_lun);
1046        if (!deve) {
1047                rcu_read_unlock();
1048                return -ENODEV;
1049        }
1050        /* scsiAuthIntrOutCommands */
1051        ret = snprintf(page, PAGE_SIZE, "%lu\n",
1052                       atomic_long_read(&deve->total_cmds));
1053        rcu_read_unlock();
1054        return ret;
1055}
1056
1057static ssize_t target_stat_auth_read_mbytes_show(struct config_item *item,
1058                char *page)
1059{
1060        struct se_lun_acl *lacl = auth_to_lacl(item);
1061        struct se_node_acl *nacl = lacl->se_lun_nacl;
1062        struct se_dev_entry *deve;
1063        ssize_t ret;
1064
1065        rcu_read_lock();
1066        deve = target_nacl_find_deve(nacl, lacl->mapped_lun);
1067        if (!deve) {
1068                rcu_read_unlock();
1069                return -ENODEV;
1070        }
1071        /* scsiAuthIntrReadMegaBytes */
1072        ret = snprintf(page, PAGE_SIZE, "%u\n",
1073                      (u32)(atomic_long_read(&deve->read_bytes) >> 20));
1074        rcu_read_unlock();
1075        return ret;
1076}
1077
1078static ssize_t target_stat_auth_write_mbytes_show(struct config_item *item,
1079                char *page)
1080{
1081        struct se_lun_acl *lacl = auth_to_lacl(item);
1082        struct se_node_acl *nacl = lacl->se_lun_nacl;
1083        struct se_dev_entry *deve;
1084        ssize_t ret;
1085
1086        rcu_read_lock();
1087        deve = target_nacl_find_deve(nacl, lacl->mapped_lun);
1088        if (!deve) {
1089                rcu_read_unlock();
1090                return -ENODEV;
1091        }
1092        /* scsiAuthIntrWrittenMegaBytes */
1093        ret = snprintf(page, PAGE_SIZE, "%u\n",
1094                      (u32)(atomic_long_read(&deve->write_bytes) >> 20));
1095        rcu_read_unlock();
1096        return ret;
1097}
1098
1099static ssize_t target_stat_auth_hs_num_cmds_show(struct config_item *item,
1100                char *page)
1101{
1102        struct se_lun_acl *lacl = auth_to_lacl(item);
1103        struct se_node_acl *nacl = lacl->se_lun_nacl;
1104        struct se_dev_entry *deve;
1105        ssize_t ret;
1106
1107        rcu_read_lock();
1108        deve = target_nacl_find_deve(nacl, lacl->mapped_lun);
1109        if (!deve) {
1110                rcu_read_unlock();
1111                return -ENODEV;
1112        }
1113        /* FIXME: scsiAuthIntrHSOutCommands */
1114        ret = snprintf(page, PAGE_SIZE, "%u\n", 0);
1115        rcu_read_unlock();
1116        return ret;
1117}
1118
1119static ssize_t target_stat_auth_creation_time_show(struct config_item *item,
1120                char *page)
1121{
1122        struct se_lun_acl *lacl = auth_to_lacl(item);
1123        struct se_node_acl *nacl = lacl->se_lun_nacl;
1124        struct se_dev_entry *deve;
1125        ssize_t ret;
1126
1127        rcu_read_lock();
1128        deve = target_nacl_find_deve(nacl, lacl->mapped_lun);
1129        if (!deve) {
1130                rcu_read_unlock();
1131                return -ENODEV;
1132        }
1133        /* scsiAuthIntrLastCreation */
1134        ret = snprintf(page, PAGE_SIZE, "%u\n", (u32)(((u32)deve->creation_time -
1135                                INITIAL_JIFFIES) * 100 / HZ));
1136        rcu_read_unlock();
1137        return ret;
1138}
1139
1140static ssize_t target_stat_auth_row_status_show(struct config_item *item,
1141                char *page)
1142{
1143        struct se_lun_acl *lacl = auth_to_lacl(item);
1144        struct se_node_acl *nacl = lacl->se_lun_nacl;
1145        struct se_dev_entry *deve;
1146        ssize_t ret;
1147
1148        rcu_read_lock();
1149        deve = target_nacl_find_deve(nacl, lacl->mapped_lun);
1150        if (!deve) {
1151                rcu_read_unlock();
1152                return -ENODEV;
1153        }
1154        /* FIXME: scsiAuthIntrRowStatus */
1155        ret = snprintf(page, PAGE_SIZE, "Ready\n");
1156        rcu_read_unlock();
1157        return ret;
1158}
1159
1160CONFIGFS_ATTR_RO(target_stat_auth_, inst);
1161CONFIGFS_ATTR_RO(target_stat_auth_, dev);
1162CONFIGFS_ATTR_RO(target_stat_auth_, port);
1163CONFIGFS_ATTR_RO(target_stat_auth_, indx);
1164CONFIGFS_ATTR_RO(target_stat_auth_, dev_or_port);
1165CONFIGFS_ATTR_RO(target_stat_auth_, intr_name);
1166CONFIGFS_ATTR_RO(target_stat_auth_, map_indx);
1167CONFIGFS_ATTR_RO(target_stat_auth_, att_count);
1168CONFIGFS_ATTR_RO(target_stat_auth_, num_cmds);
1169CONFIGFS_ATTR_RO(target_stat_auth_, read_mbytes);
1170CONFIGFS_ATTR_RO(target_stat_auth_, write_mbytes);
1171CONFIGFS_ATTR_RO(target_stat_auth_, hs_num_cmds);
1172CONFIGFS_ATTR_RO(target_stat_auth_, creation_time);
1173CONFIGFS_ATTR_RO(target_stat_auth_, row_status);
1174
1175static struct configfs_attribute *target_stat_scsi_auth_intr_attrs[] = {
1176        &target_stat_auth_attr_inst,
1177        &target_stat_auth_attr_dev,
1178        &target_stat_auth_attr_port,
1179        &target_stat_auth_attr_indx,
1180        &target_stat_auth_attr_dev_or_port,
1181        &target_stat_auth_attr_intr_name,
1182        &target_stat_auth_attr_map_indx,
1183        &target_stat_auth_attr_att_count,
1184        &target_stat_auth_attr_num_cmds,
1185        &target_stat_auth_attr_read_mbytes,
1186        &target_stat_auth_attr_write_mbytes,
1187        &target_stat_auth_attr_hs_num_cmds,
1188        &target_stat_auth_attr_creation_time,
1189        &target_stat_auth_attr_row_status,
1190        NULL,
1191};
1192
1193static struct config_item_type target_stat_scsi_auth_intr_cit = {
1194        .ct_attrs               = target_stat_scsi_auth_intr_attrs,
1195        .ct_owner               = THIS_MODULE,
1196};
1197
1198/*
1199 * SCSI Attached Initiator Port Table
1200 */
1201
1202static struct se_lun_acl *iport_to_lacl(struct config_item *item)
1203{
1204        struct se_ml_stat_grps *lgrps = container_of(to_config_group(item),
1205                        struct se_ml_stat_grps, scsi_att_intr_port_group);
1206        return container_of(lgrps, struct se_lun_acl, ml_stat_grps);
1207}
1208
1209static ssize_t target_stat_iport_inst_show(struct config_item *item,
1210                char *page)
1211{
1212        struct se_lun_acl *lacl = iport_to_lacl(item);
1213        struct se_node_acl *nacl = lacl->se_lun_nacl;
1214        struct se_dev_entry *deve;
1215        struct se_portal_group *tpg;
1216        ssize_t ret;
1217
1218        rcu_read_lock();
1219        deve = target_nacl_find_deve(nacl, lacl->mapped_lun);
1220        if (!deve) {
1221                rcu_read_unlock();
1222                return -ENODEV;
1223        }
1224        tpg = nacl->se_tpg;
1225        /* scsiInstIndex */
1226        ret = snprintf(page, PAGE_SIZE, "%u\n",
1227                        tpg->se_tpg_tfo->tpg_get_inst_index(tpg));
1228        rcu_read_unlock();
1229        return ret;
1230}
1231
1232static ssize_t target_stat_iport_dev_show(struct config_item *item,
1233                char *page)
1234{
1235        struct se_lun_acl *lacl = iport_to_lacl(item);
1236        struct se_node_acl *nacl = lacl->se_lun_nacl;
1237        struct se_dev_entry *deve;
1238        struct se_lun *lun;
1239        ssize_t ret;
1240
1241        rcu_read_lock();
1242        deve = target_nacl_find_deve(nacl, lacl->mapped_lun);
1243        if (!deve) {
1244                rcu_read_unlock();
1245                return -ENODEV;
1246        }
1247        lun = rcu_dereference(deve->se_lun);
1248        /* scsiDeviceIndex */
1249        ret = snprintf(page, PAGE_SIZE, "%u\n", lun->lun_index);
1250        rcu_read_unlock();
1251        return ret;
1252}
1253
1254static ssize_t target_stat_iport_port_show(struct config_item *item,
1255                char *page)
1256{
1257        struct se_lun_acl *lacl = iport_to_lacl(item);
1258        struct se_node_acl *nacl = lacl->se_lun_nacl;
1259        struct se_dev_entry *deve;
1260        struct se_portal_group *tpg;
1261        ssize_t ret;
1262
1263        rcu_read_lock();
1264        deve = target_nacl_find_deve(nacl, lacl->mapped_lun);
1265        if (!deve) {
1266                rcu_read_unlock();
1267                return -ENODEV;
1268        }
1269        tpg = nacl->se_tpg;
1270        /* scsiPortIndex */
1271        ret = snprintf(page, PAGE_SIZE, "%u\n", tpg->se_tpg_tfo->tpg_get_tag(tpg));
1272        rcu_read_unlock();
1273        return ret;
1274}
1275
1276static ssize_t target_stat_iport_indx_show(struct config_item *item,
1277                char *page)
1278{
1279        struct se_lun_acl *lacl = iport_to_lacl(item);
1280        struct se_node_acl *nacl = lacl->se_lun_nacl;
1281        struct se_session *se_sess;
1282        struct se_portal_group *tpg;
1283        ssize_t ret;
1284
1285        spin_lock_irq(&nacl->nacl_sess_lock);
1286        se_sess = nacl->nacl_sess;
1287        if (!se_sess) {
1288                spin_unlock_irq(&nacl->nacl_sess_lock);
1289                return -ENODEV;
1290        }
1291
1292        tpg = nacl->se_tpg;
1293        /* scsiAttIntrPortIndex */
1294        ret = snprintf(page, PAGE_SIZE, "%u\n",
1295                        tpg->se_tpg_tfo->sess_get_index(se_sess));
1296        spin_unlock_irq(&nacl->nacl_sess_lock);
1297        return ret;
1298}
1299
1300static ssize_t target_stat_iport_port_auth_indx_show(struct config_item *item,
1301                char *page)
1302{
1303        struct se_lun_acl *lacl = iport_to_lacl(item);
1304        struct se_node_acl *nacl = lacl->se_lun_nacl;
1305        struct se_dev_entry *deve;
1306        ssize_t ret;
1307
1308        rcu_read_lock();
1309        deve = target_nacl_find_deve(nacl, lacl->mapped_lun);
1310        if (!deve) {
1311                rcu_read_unlock();
1312                return -ENODEV;
1313        }
1314        /* scsiAttIntrPortAuthIntrIdx */
1315        ret = snprintf(page, PAGE_SIZE, "%u\n", nacl->acl_index);
1316        rcu_read_unlock();
1317        return ret;
1318}
1319
1320static ssize_t target_stat_iport_port_ident_show(struct config_item *item,
1321                char *page)
1322{
1323        struct se_lun_acl *lacl = iport_to_lacl(item);
1324        struct se_node_acl *nacl = lacl->se_lun_nacl;
1325        struct se_session *se_sess;
1326        struct se_portal_group *tpg;
1327        ssize_t ret;
1328        unsigned char buf[64];
1329
1330        spin_lock_irq(&nacl->nacl_sess_lock);
1331        se_sess = nacl->nacl_sess;
1332        if (!se_sess) {
1333                spin_unlock_irq(&nacl->nacl_sess_lock);
1334                return -ENODEV;
1335        }
1336
1337        tpg = nacl->se_tpg;
1338        /* scsiAttIntrPortName+scsiAttIntrPortIdentifier */
1339        memset(buf, 0, 64);
1340        if (tpg->se_tpg_tfo->sess_get_initiator_sid != NULL)
1341                tpg->se_tpg_tfo->sess_get_initiator_sid(se_sess, buf, 64);
1342
1343        ret = snprintf(page, PAGE_SIZE, "%s+i+%s\n", nacl->initiatorname, buf);
1344        spin_unlock_irq(&nacl->nacl_sess_lock);
1345        return ret;
1346}
1347
1348CONFIGFS_ATTR_RO(target_stat_iport_, inst);
1349CONFIGFS_ATTR_RO(target_stat_iport_, dev);
1350CONFIGFS_ATTR_RO(target_stat_iport_, port);
1351CONFIGFS_ATTR_RO(target_stat_iport_, indx);
1352CONFIGFS_ATTR_RO(target_stat_iport_, port_auth_indx);
1353CONFIGFS_ATTR_RO(target_stat_iport_, port_ident);
1354
1355static struct configfs_attribute *target_stat_scsi_ath_intr_port_attrs[] = {
1356        &target_stat_iport_attr_inst,
1357        &target_stat_iport_attr_dev,
1358        &target_stat_iport_attr_port,
1359        &target_stat_iport_attr_indx,
1360        &target_stat_iport_attr_port_auth_indx,
1361        &target_stat_iport_attr_port_ident,
1362        NULL,
1363};
1364
1365static struct config_item_type target_stat_scsi_att_intr_port_cit = {
1366        .ct_attrs               = target_stat_scsi_ath_intr_port_attrs,
1367        .ct_owner               = THIS_MODULE,
1368};
1369
1370/*
1371 * Called from target_core_fabric_configfs.c:target_fabric_make_mappedlun() to setup
1372 * the target MappedLUN statistics groups + configfs CITs located in target_core_stat.c
1373 */
1374void target_stat_setup_mappedlun_default_groups(struct se_lun_acl *lacl)
1375{
1376        config_group_init_type_name(&lacl->ml_stat_grps.scsi_auth_intr_group,
1377                        "scsi_auth_intr", &target_stat_scsi_auth_intr_cit);
1378        configfs_add_default_group(&lacl->ml_stat_grps.scsi_auth_intr_group,
1379                        &lacl->ml_stat_grps.stat_group);
1380
1381        config_group_init_type_name(&lacl->ml_stat_grps.scsi_att_intr_port_group,
1382                        "scsi_att_intr_port", &target_stat_scsi_att_intr_port_cit);
1383        configfs_add_default_group(&lacl->ml_stat_grps.scsi_att_intr_port_group,
1384                        &lacl->ml_stat_grps.stat_group);
1385}
1386