Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*******************************************************************************
0003  * Filename:  target_core_stat.c
0004  *
0005  * Modern ConfigFS group context specific statistics based on original
0006  * target_core_mib.c code
0007  *
0008  * (c) Copyright 2006-2013 Datera, Inc.
0009  *
0010  * Nicholas A. Bellinger <nab@linux-iscsi.org>
0011  *
0012  ******************************************************************************/
0013 
0014 #include <linux/kernel.h>
0015 #include <linux/module.h>
0016 #include <linux/delay.h>
0017 #include <linux/timer.h>
0018 #include <linux/string.h>
0019 #include <linux/utsname.h>
0020 #include <linux/proc_fs.h>
0021 #include <linux/seq_file.h>
0022 #include <linux/configfs.h>
0023 
0024 #include <target/target_core_base.h>
0025 #include <target/target_core_backend.h>
0026 #include <target/target_core_fabric.h>
0027 
0028 #include "target_core_internal.h"
0029 
0030 #ifndef INITIAL_JIFFIES
0031 #define INITIAL_JIFFIES ((unsigned long)(unsigned int) (-300*HZ))
0032 #endif
0033 
0034 #define SCSI_LU_INDEX           1
0035 #define LU_COUNT            1
0036 
0037 /*
0038  * SCSI Device Table
0039  */
0040 
0041 static struct se_device *to_stat_dev(struct config_item *item)
0042 {
0043     struct se_dev_stat_grps *sgrps = container_of(to_config_group(item),
0044             struct se_dev_stat_grps, scsi_dev_group);
0045     return container_of(sgrps, struct se_device, dev_stat_grps);
0046 }
0047 
0048 static ssize_t target_stat_inst_show(struct config_item *item, char *page)
0049 {
0050     struct se_hba *hba = to_stat_dev(item)->se_hba;
0051 
0052     return snprintf(page, PAGE_SIZE, "%u\n", hba->hba_index);
0053 }
0054 
0055 static ssize_t target_stat_indx_show(struct config_item *item, char *page)
0056 {
0057     return snprintf(page, PAGE_SIZE, "%u\n", to_stat_dev(item)->dev_index);
0058 }
0059 
0060 static ssize_t target_stat_role_show(struct config_item *item, char *page)
0061 {
0062     return snprintf(page, PAGE_SIZE, "Target\n");
0063 }
0064 
0065 static ssize_t target_stat_ports_show(struct config_item *item, char *page)
0066 {
0067     return snprintf(page, PAGE_SIZE, "%u\n", to_stat_dev(item)->export_count);
0068 }
0069 
0070 CONFIGFS_ATTR_RO(target_stat_, inst);
0071 CONFIGFS_ATTR_RO(target_stat_, indx);
0072 CONFIGFS_ATTR_RO(target_stat_, role);
0073 CONFIGFS_ATTR_RO(target_stat_, ports);
0074 
0075 static struct configfs_attribute *target_stat_scsi_dev_attrs[] = {
0076     &target_stat_attr_inst,
0077     &target_stat_attr_indx,
0078     &target_stat_attr_role,
0079     &target_stat_attr_ports,
0080     NULL,
0081 };
0082 
0083 static const struct config_item_type target_stat_scsi_dev_cit = {
0084     .ct_attrs       = target_stat_scsi_dev_attrs,
0085     .ct_owner       = THIS_MODULE,
0086 };
0087 
0088 /*
0089  * SCSI Target Device Table
0090  */
0091 static struct se_device *to_stat_tgt_dev(struct config_item *item)
0092 {
0093     struct se_dev_stat_grps *sgrps = container_of(to_config_group(item),
0094             struct se_dev_stat_grps, scsi_tgt_dev_group);
0095     return container_of(sgrps, struct se_device, dev_stat_grps);
0096 }
0097 
0098 static ssize_t target_stat_tgt_inst_show(struct config_item *item, char *page)
0099 {
0100     struct se_hba *hba = to_stat_tgt_dev(item)->se_hba;
0101 
0102     return snprintf(page, PAGE_SIZE, "%u\n", hba->hba_index);
0103 }
0104 
0105 static ssize_t target_stat_tgt_indx_show(struct config_item *item, char *page)
0106 {
0107     return snprintf(page, PAGE_SIZE, "%u\n", to_stat_tgt_dev(item)->dev_index);
0108 }
0109 
0110 static ssize_t target_stat_tgt_num_lus_show(struct config_item *item,
0111         char *page)
0112 {
0113     return snprintf(page, PAGE_SIZE, "%u\n", LU_COUNT);
0114 }
0115 
0116 static ssize_t target_stat_tgt_status_show(struct config_item *item,
0117         char *page)
0118 {
0119     if (to_stat_tgt_dev(item)->export_count)
0120         return snprintf(page, PAGE_SIZE, "activated");
0121     else
0122         return snprintf(page, PAGE_SIZE, "deactivated");
0123 }
0124 
0125 static ssize_t target_stat_tgt_non_access_lus_show(struct config_item *item,
0126         char *page)
0127 {
0128     int non_accessible_lus;
0129 
0130     if (to_stat_tgt_dev(item)->export_count)
0131         non_accessible_lus = 0;
0132     else
0133         non_accessible_lus = 1;
0134 
0135     return snprintf(page, PAGE_SIZE, "%u\n", non_accessible_lus);
0136 }
0137 
0138 static ssize_t target_stat_tgt_resets_show(struct config_item *item,
0139         char *page)
0140 {
0141     return snprintf(page, PAGE_SIZE, "%lu\n",
0142             atomic_long_read(&to_stat_tgt_dev(item)->num_resets));
0143 }
0144 
0145 static ssize_t target_stat_tgt_aborts_complete_show(struct config_item *item,
0146         char *page)
0147 {
0148     return snprintf(page, PAGE_SIZE, "%lu\n",
0149             atomic_long_read(&to_stat_tgt_dev(item)->aborts_complete));
0150 }
0151 
0152 static ssize_t target_stat_tgt_aborts_no_task_show(struct config_item *item,
0153         char *page)
0154 {
0155     return snprintf(page, PAGE_SIZE, "%lu\n",
0156             atomic_long_read(&to_stat_tgt_dev(item)->aborts_no_task));
0157 }
0158 
0159 CONFIGFS_ATTR_RO(target_stat_tgt_, inst);
0160 CONFIGFS_ATTR_RO(target_stat_tgt_, indx);
0161 CONFIGFS_ATTR_RO(target_stat_tgt_, num_lus);
0162 CONFIGFS_ATTR_RO(target_stat_tgt_, status);
0163 CONFIGFS_ATTR_RO(target_stat_tgt_, non_access_lus);
0164 CONFIGFS_ATTR_RO(target_stat_tgt_, resets);
0165 CONFIGFS_ATTR_RO(target_stat_tgt_, aborts_complete);
0166 CONFIGFS_ATTR_RO(target_stat_tgt_, aborts_no_task);
0167 
0168 static struct configfs_attribute *target_stat_scsi_tgt_dev_attrs[] = {
0169     &target_stat_tgt_attr_inst,
0170     &target_stat_tgt_attr_indx,
0171     &target_stat_tgt_attr_num_lus,
0172     &target_stat_tgt_attr_status,
0173     &target_stat_tgt_attr_non_access_lus,
0174     &target_stat_tgt_attr_resets,
0175     &target_stat_tgt_attr_aborts_complete,
0176     &target_stat_tgt_attr_aborts_no_task,
0177     NULL,
0178 };
0179 
0180 static const struct config_item_type target_stat_scsi_tgt_dev_cit = {
0181     .ct_attrs       = target_stat_scsi_tgt_dev_attrs,
0182     .ct_owner       = THIS_MODULE,
0183 };
0184 
0185 /*
0186  * SCSI Logical Unit Table
0187  */
0188 
0189 static struct se_device *to_stat_lu_dev(struct config_item *item)
0190 {
0191     struct se_dev_stat_grps *sgrps = container_of(to_config_group(item),
0192             struct se_dev_stat_grps, scsi_lu_group);
0193     return container_of(sgrps, struct se_device, dev_stat_grps);
0194 }
0195 
0196 static ssize_t target_stat_lu_inst_show(struct config_item *item, char *page)
0197 {
0198     struct se_hba *hba = to_stat_lu_dev(item)->se_hba;
0199 
0200     return snprintf(page, PAGE_SIZE, "%u\n", hba->hba_index);
0201 }
0202 
0203 static ssize_t target_stat_lu_dev_show(struct config_item *item, char *page)
0204 {
0205     return snprintf(page, PAGE_SIZE, "%u\n",
0206             to_stat_lu_dev(item)->dev_index);
0207 }
0208 
0209 static ssize_t target_stat_lu_indx_show(struct config_item *item, char *page)
0210 {
0211     return snprintf(page, PAGE_SIZE, "%u\n", SCSI_LU_INDEX);
0212 }
0213 
0214 static ssize_t target_stat_lu_lun_show(struct config_item *item, char *page)
0215 {
0216     /* FIXME: scsiLuDefaultLun */
0217     return snprintf(page, PAGE_SIZE, "%llu\n", (unsigned long long)0);
0218 }
0219 
0220 static ssize_t target_stat_lu_lu_name_show(struct config_item *item, char *page)
0221 {
0222     struct se_device *dev = to_stat_lu_dev(item);
0223 
0224     /* scsiLuWwnName */
0225     return snprintf(page, PAGE_SIZE, "%s\n",
0226             (strlen(dev->t10_wwn.unit_serial)) ?
0227             dev->t10_wwn.unit_serial : "None");
0228 }
0229 
0230 static ssize_t target_stat_lu_vend_show(struct config_item *item, char *page)
0231 {
0232     struct se_device *dev = to_stat_lu_dev(item);
0233 
0234     return snprintf(page, PAGE_SIZE, "%-" __stringify(INQUIRY_VENDOR_LEN)
0235             "s\n", dev->t10_wwn.vendor);
0236 }
0237 
0238 static ssize_t target_stat_lu_prod_show(struct config_item *item, char *page)
0239 {
0240     struct se_device *dev = to_stat_lu_dev(item);
0241 
0242     return snprintf(page, PAGE_SIZE, "%-" __stringify(INQUIRY_MODEL_LEN)
0243             "s\n", dev->t10_wwn.model);
0244 }
0245 
0246 static ssize_t target_stat_lu_rev_show(struct config_item *item, char *page)
0247 {
0248     struct se_device *dev = to_stat_lu_dev(item);
0249 
0250     return snprintf(page, PAGE_SIZE, "%-" __stringify(INQUIRY_REVISION_LEN)
0251             "s\n", dev->t10_wwn.revision);
0252 }
0253 
0254 static ssize_t target_stat_lu_dev_type_show(struct config_item *item, char *page)
0255 {
0256     struct se_device *dev = to_stat_lu_dev(item);
0257 
0258     /* scsiLuPeripheralType */
0259     return snprintf(page, PAGE_SIZE, "%u\n",
0260             dev->transport->get_device_type(dev));
0261 }
0262 
0263 static ssize_t target_stat_lu_status_show(struct config_item *item, char *page)
0264 {
0265     struct se_device *dev = to_stat_lu_dev(item);
0266 
0267     /* scsiLuStatus */
0268     return snprintf(page, PAGE_SIZE, "%s\n",
0269         (dev->export_count) ? "available" : "notavailable");
0270 }
0271 
0272 static ssize_t target_stat_lu_state_bit_show(struct config_item *item,
0273         char *page)
0274 {
0275     /* scsiLuState */
0276     return snprintf(page, PAGE_SIZE, "exposed\n");
0277 }
0278 
0279 static ssize_t target_stat_lu_num_cmds_show(struct config_item *item,
0280         char *page)
0281 {
0282     struct se_device *dev = to_stat_lu_dev(item);
0283 
0284     /* scsiLuNumCommands */
0285     return snprintf(page, PAGE_SIZE, "%lu\n",
0286             atomic_long_read(&dev->num_cmds));
0287 }
0288 
0289 static ssize_t target_stat_lu_read_mbytes_show(struct config_item *item,
0290         char *page)
0291 {
0292     struct se_device *dev = to_stat_lu_dev(item);
0293 
0294     /* scsiLuReadMegaBytes */
0295     return snprintf(page, PAGE_SIZE, "%lu\n",
0296             atomic_long_read(&dev->read_bytes) >> 20);
0297 }
0298 
0299 static ssize_t target_stat_lu_write_mbytes_show(struct config_item *item,
0300         char *page)
0301 {
0302     struct se_device *dev = to_stat_lu_dev(item);
0303 
0304     /* scsiLuWrittenMegaBytes */
0305     return snprintf(page, PAGE_SIZE, "%lu\n",
0306             atomic_long_read(&dev->write_bytes) >> 20);
0307 }
0308 
0309 static ssize_t target_stat_lu_resets_show(struct config_item *item, char *page)
0310 {
0311     struct se_device *dev = to_stat_lu_dev(item);
0312 
0313     /* scsiLuInResets */
0314     return snprintf(page, PAGE_SIZE, "%lu\n",
0315         atomic_long_read(&dev->num_resets));
0316 }
0317 
0318 static ssize_t target_stat_lu_full_stat_show(struct config_item *item,
0319         char *page)
0320 {
0321     /* FIXME: scsiLuOutTaskSetFullStatus */
0322     return snprintf(page, PAGE_SIZE, "%u\n", 0);
0323 }
0324 
0325 static ssize_t target_stat_lu_hs_num_cmds_show(struct config_item *item,
0326         char *page)
0327 {
0328     /* FIXME: scsiLuHSInCommands */
0329     return snprintf(page, PAGE_SIZE, "%u\n", 0);
0330 }
0331 
0332 static ssize_t target_stat_lu_creation_time_show(struct config_item *item,
0333         char *page)
0334 {
0335     struct se_device *dev = to_stat_lu_dev(item);
0336 
0337     /* scsiLuCreationTime */
0338     return snprintf(page, PAGE_SIZE, "%u\n", (u32)(((u32)dev->creation_time -
0339                 INITIAL_JIFFIES) * 100 / HZ));
0340 }
0341 
0342 CONFIGFS_ATTR_RO(target_stat_lu_, inst);
0343 CONFIGFS_ATTR_RO(target_stat_lu_, dev);
0344 CONFIGFS_ATTR_RO(target_stat_lu_, indx);
0345 CONFIGFS_ATTR_RO(target_stat_lu_, lun);
0346 CONFIGFS_ATTR_RO(target_stat_lu_, lu_name);
0347 CONFIGFS_ATTR_RO(target_stat_lu_, vend);
0348 CONFIGFS_ATTR_RO(target_stat_lu_, prod);
0349 CONFIGFS_ATTR_RO(target_stat_lu_, rev);
0350 CONFIGFS_ATTR_RO(target_stat_lu_, dev_type);
0351 CONFIGFS_ATTR_RO(target_stat_lu_, status);
0352 CONFIGFS_ATTR_RO(target_stat_lu_, state_bit);
0353 CONFIGFS_ATTR_RO(target_stat_lu_, num_cmds);
0354 CONFIGFS_ATTR_RO(target_stat_lu_, read_mbytes);
0355 CONFIGFS_ATTR_RO(target_stat_lu_, write_mbytes);
0356 CONFIGFS_ATTR_RO(target_stat_lu_, resets);
0357 CONFIGFS_ATTR_RO(target_stat_lu_, full_stat);
0358 CONFIGFS_ATTR_RO(target_stat_lu_, hs_num_cmds);
0359 CONFIGFS_ATTR_RO(target_stat_lu_, creation_time);
0360 
0361 static struct configfs_attribute *target_stat_scsi_lu_attrs[] = {
0362     &target_stat_lu_attr_inst,
0363     &target_stat_lu_attr_dev,
0364     &target_stat_lu_attr_indx,
0365     &target_stat_lu_attr_lun,
0366     &target_stat_lu_attr_lu_name,
0367     &target_stat_lu_attr_vend,
0368     &target_stat_lu_attr_prod,
0369     &target_stat_lu_attr_rev,
0370     &target_stat_lu_attr_dev_type,
0371     &target_stat_lu_attr_status,
0372     &target_stat_lu_attr_state_bit,
0373     &target_stat_lu_attr_num_cmds,
0374     &target_stat_lu_attr_read_mbytes,
0375     &target_stat_lu_attr_write_mbytes,
0376     &target_stat_lu_attr_resets,
0377     &target_stat_lu_attr_full_stat,
0378     &target_stat_lu_attr_hs_num_cmds,
0379     &target_stat_lu_attr_creation_time,
0380     NULL,
0381 };
0382 
0383 static const struct config_item_type target_stat_scsi_lu_cit = {
0384     .ct_attrs       = target_stat_scsi_lu_attrs,
0385     .ct_owner       = THIS_MODULE,
0386 };
0387 
0388 /*
0389  * Called from target_core_configfs.c:target_core_make_subdev() to setup
0390  * the target statistics groups + configfs CITs located in target_core_stat.c
0391  */
0392 void target_stat_setup_dev_default_groups(struct se_device *dev)
0393 {
0394     config_group_init_type_name(&dev->dev_stat_grps.scsi_dev_group,
0395             "scsi_dev", &target_stat_scsi_dev_cit);
0396     configfs_add_default_group(&dev->dev_stat_grps.scsi_dev_group,
0397             &dev->dev_stat_grps.stat_group);
0398 
0399     config_group_init_type_name(&dev->dev_stat_grps.scsi_tgt_dev_group,
0400             "scsi_tgt_dev", &target_stat_scsi_tgt_dev_cit);
0401     configfs_add_default_group(&dev->dev_stat_grps.scsi_tgt_dev_group,
0402             &dev->dev_stat_grps.stat_group);
0403 
0404     config_group_init_type_name(&dev->dev_stat_grps.scsi_lu_group,
0405             "scsi_lu", &target_stat_scsi_lu_cit);
0406     configfs_add_default_group(&dev->dev_stat_grps.scsi_lu_group,
0407             &dev->dev_stat_grps.stat_group);
0408 }
0409 
0410 /*
0411  * SCSI Port Table
0412  */
0413 
0414 static struct se_lun *to_stat_port(struct config_item *item)
0415 {
0416     struct se_port_stat_grps *pgrps = container_of(to_config_group(item),
0417             struct se_port_stat_grps, scsi_port_group);
0418     return container_of(pgrps, struct se_lun, port_stat_grps);
0419 }
0420 
0421 static ssize_t target_stat_port_inst_show(struct config_item *item, char *page)
0422 {
0423     struct se_lun *lun = to_stat_port(item);
0424     struct se_device *dev;
0425     ssize_t ret = -ENODEV;
0426 
0427     rcu_read_lock();
0428     dev = rcu_dereference(lun->lun_se_dev);
0429     if (dev)
0430         ret = snprintf(page, PAGE_SIZE, "%u\n", dev->hba_index);
0431     rcu_read_unlock();
0432     return ret;
0433 }
0434 
0435 static ssize_t target_stat_port_dev_show(struct config_item *item, char *page)
0436 {
0437     struct se_lun *lun = to_stat_port(item);
0438     struct se_device *dev;
0439     ssize_t ret = -ENODEV;
0440 
0441     rcu_read_lock();
0442     dev = rcu_dereference(lun->lun_se_dev);
0443     if (dev)
0444         ret = snprintf(page, PAGE_SIZE, "%u\n", dev->dev_index);
0445     rcu_read_unlock();
0446     return ret;
0447 }
0448 
0449 static ssize_t target_stat_port_indx_show(struct config_item *item, char *page)
0450 {
0451     struct se_lun *lun = to_stat_port(item);
0452     struct se_device *dev;
0453     ssize_t ret = -ENODEV;
0454 
0455     rcu_read_lock();
0456     dev = rcu_dereference(lun->lun_se_dev);
0457     if (dev)
0458         ret = snprintf(page, PAGE_SIZE, "%u\n", lun->lun_rtpi);
0459     rcu_read_unlock();
0460     return ret;
0461 }
0462 
0463 static ssize_t target_stat_port_role_show(struct config_item *item, char *page)
0464 {
0465     struct se_lun *lun = to_stat_port(item);
0466     struct se_device *dev;
0467     ssize_t ret = -ENODEV;
0468 
0469     rcu_read_lock();
0470     dev = rcu_dereference(lun->lun_se_dev);
0471     if (dev)
0472         ret = snprintf(page, PAGE_SIZE, "%s%u\n", "Device", dev->dev_index);
0473     rcu_read_unlock();
0474     return ret;
0475 }
0476 
0477 static ssize_t target_stat_port_busy_count_show(struct config_item *item,
0478         char *page)
0479 {
0480     struct se_lun *lun = to_stat_port(item);
0481     struct se_device *dev;
0482     ssize_t ret = -ENODEV;
0483 
0484     rcu_read_lock();
0485     dev = rcu_dereference(lun->lun_se_dev);
0486     if (dev) {
0487         /* FIXME: scsiPortBusyStatuses  */
0488         ret = snprintf(page, PAGE_SIZE, "%u\n", 0);
0489     }
0490     rcu_read_unlock();
0491     return ret;
0492 }
0493 
0494 CONFIGFS_ATTR_RO(target_stat_port_, inst);
0495 CONFIGFS_ATTR_RO(target_stat_port_, dev);
0496 CONFIGFS_ATTR_RO(target_stat_port_, indx);
0497 CONFIGFS_ATTR_RO(target_stat_port_, role);
0498 CONFIGFS_ATTR_RO(target_stat_port_, busy_count);
0499 
0500 static struct configfs_attribute *target_stat_scsi_port_attrs[] = {
0501     &target_stat_port_attr_inst,
0502     &target_stat_port_attr_dev,
0503     &target_stat_port_attr_indx,
0504     &target_stat_port_attr_role,
0505     &target_stat_port_attr_busy_count,
0506     NULL,
0507 };
0508 
0509 static const struct config_item_type target_stat_scsi_port_cit = {
0510     .ct_attrs       = target_stat_scsi_port_attrs,
0511     .ct_owner       = THIS_MODULE,
0512 };
0513 
0514 /*
0515  * SCSI Target Port Table
0516  */
0517 static struct se_lun *to_stat_tgt_port(struct config_item *item)
0518 {
0519     struct se_port_stat_grps *pgrps = container_of(to_config_group(item),
0520             struct se_port_stat_grps, scsi_tgt_port_group);
0521     return container_of(pgrps, struct se_lun, port_stat_grps);
0522 }
0523 
0524 static ssize_t target_stat_tgt_port_inst_show(struct config_item *item,
0525         char *page)
0526 {
0527     struct se_lun *lun = to_stat_tgt_port(item);
0528     struct se_device *dev;
0529     ssize_t ret = -ENODEV;
0530 
0531     rcu_read_lock();
0532     dev = rcu_dereference(lun->lun_se_dev);
0533     if (dev)
0534         ret = snprintf(page, PAGE_SIZE, "%u\n", dev->hba_index);
0535     rcu_read_unlock();
0536     return ret;
0537 }
0538 
0539 static ssize_t target_stat_tgt_port_dev_show(struct config_item *item,
0540         char *page)
0541 {
0542     struct se_lun *lun = to_stat_tgt_port(item);
0543     struct se_device *dev;
0544     ssize_t ret = -ENODEV;
0545 
0546     rcu_read_lock();
0547     dev = rcu_dereference(lun->lun_se_dev);
0548     if (dev)
0549         ret = snprintf(page, PAGE_SIZE, "%u\n", dev->dev_index);
0550     rcu_read_unlock();
0551     return ret;
0552 }
0553 
0554 static ssize_t target_stat_tgt_port_indx_show(struct config_item *item,
0555         char *page)
0556 {
0557     struct se_lun *lun = to_stat_tgt_port(item);
0558     struct se_device *dev;
0559     ssize_t ret = -ENODEV;
0560 
0561     rcu_read_lock();
0562     dev = rcu_dereference(lun->lun_se_dev);
0563     if (dev)
0564         ret = snprintf(page, PAGE_SIZE, "%u\n", lun->lun_rtpi);
0565     rcu_read_unlock();
0566     return ret;
0567 }
0568 
0569 static ssize_t target_stat_tgt_port_name_show(struct config_item *item,
0570         char *page)
0571 {
0572     struct se_lun *lun = to_stat_tgt_port(item);
0573     struct se_portal_group *tpg = lun->lun_tpg;
0574     struct se_device *dev;
0575     ssize_t ret = -ENODEV;
0576 
0577     rcu_read_lock();
0578     dev = rcu_dereference(lun->lun_se_dev);
0579     if (dev)
0580         ret = snprintf(page, PAGE_SIZE, "%sPort#%u\n",
0581             tpg->se_tpg_tfo->fabric_name,
0582             lun->lun_rtpi);
0583     rcu_read_unlock();
0584     return ret;
0585 }
0586 
0587 static ssize_t target_stat_tgt_port_port_index_show(struct config_item *item,
0588         char *page)
0589 {
0590     struct se_lun *lun = to_stat_tgt_port(item);
0591     struct se_portal_group *tpg = lun->lun_tpg;
0592     struct se_device *dev;
0593     ssize_t ret = -ENODEV;
0594 
0595     rcu_read_lock();
0596     dev = rcu_dereference(lun->lun_se_dev);
0597     if (dev)
0598         ret = snprintf(page, PAGE_SIZE, "%s%s%d\n",
0599             tpg->se_tpg_tfo->tpg_get_wwn(tpg), "+t+",
0600             tpg->se_tpg_tfo->tpg_get_tag(tpg));
0601     rcu_read_unlock();
0602     return ret;
0603 }
0604 
0605 static ssize_t target_stat_tgt_port_in_cmds_show(struct config_item *item,
0606         char *page)
0607 {
0608     struct se_lun *lun = to_stat_tgt_port(item);
0609     struct se_device *dev;
0610     ssize_t ret = -ENODEV;
0611 
0612     rcu_read_lock();
0613     dev = rcu_dereference(lun->lun_se_dev);
0614     if (dev)
0615         ret = snprintf(page, PAGE_SIZE, "%lu\n",
0616                    atomic_long_read(&lun->lun_stats.cmd_pdus));
0617     rcu_read_unlock();
0618     return ret;
0619 }
0620 
0621 static ssize_t target_stat_tgt_port_write_mbytes_show(struct config_item *item,
0622         char *page)
0623 {
0624     struct se_lun *lun = to_stat_tgt_port(item);
0625     struct se_device *dev;
0626     ssize_t ret = -ENODEV;
0627 
0628     rcu_read_lock();
0629     dev = rcu_dereference(lun->lun_se_dev);
0630     if (dev)
0631         ret = snprintf(page, PAGE_SIZE, "%u\n",
0632             (u32)(atomic_long_read(&lun->lun_stats.rx_data_octets) >> 20));
0633     rcu_read_unlock();
0634     return ret;
0635 }
0636 
0637 static ssize_t target_stat_tgt_port_read_mbytes_show(struct config_item *item,
0638         char *page)
0639 {
0640     struct se_lun *lun = to_stat_tgt_port(item);
0641     struct se_device *dev;
0642     ssize_t ret = -ENODEV;
0643 
0644     rcu_read_lock();
0645     dev = rcu_dereference(lun->lun_se_dev);
0646     if (dev)
0647         ret = snprintf(page, PAGE_SIZE, "%u\n",
0648                 (u32)(atomic_long_read(&lun->lun_stats.tx_data_octets) >> 20));
0649     rcu_read_unlock();
0650     return ret;
0651 }
0652 
0653 static ssize_t target_stat_tgt_port_hs_in_cmds_show(struct config_item *item,
0654         char *page)
0655 {
0656     struct se_lun *lun = to_stat_tgt_port(item);
0657     struct se_device *dev;
0658     ssize_t ret = -ENODEV;
0659 
0660     rcu_read_lock();
0661     dev = rcu_dereference(lun->lun_se_dev);
0662     if (dev) {
0663         /* FIXME: scsiTgtPortHsInCommands */
0664         ret = snprintf(page, PAGE_SIZE, "%u\n", 0);
0665     }
0666     rcu_read_unlock();
0667     return ret;
0668 }
0669 
0670 CONFIGFS_ATTR_RO(target_stat_tgt_port_, inst);
0671 CONFIGFS_ATTR_RO(target_stat_tgt_port_, dev);
0672 CONFIGFS_ATTR_RO(target_stat_tgt_port_, indx);
0673 CONFIGFS_ATTR_RO(target_stat_tgt_port_, name);
0674 CONFIGFS_ATTR_RO(target_stat_tgt_port_, port_index);
0675 CONFIGFS_ATTR_RO(target_stat_tgt_port_, in_cmds);
0676 CONFIGFS_ATTR_RO(target_stat_tgt_port_, write_mbytes);
0677 CONFIGFS_ATTR_RO(target_stat_tgt_port_, read_mbytes);
0678 CONFIGFS_ATTR_RO(target_stat_tgt_port_, hs_in_cmds);
0679 
0680 static struct configfs_attribute *target_stat_scsi_tgt_port_attrs[] = {
0681     &target_stat_tgt_port_attr_inst,
0682     &target_stat_tgt_port_attr_dev,
0683     &target_stat_tgt_port_attr_indx,
0684     &target_stat_tgt_port_attr_name,
0685     &target_stat_tgt_port_attr_port_index,
0686     &target_stat_tgt_port_attr_in_cmds,
0687     &target_stat_tgt_port_attr_write_mbytes,
0688     &target_stat_tgt_port_attr_read_mbytes,
0689     &target_stat_tgt_port_attr_hs_in_cmds,
0690     NULL,
0691 };
0692 
0693 static const struct config_item_type target_stat_scsi_tgt_port_cit = {
0694     .ct_attrs       = target_stat_scsi_tgt_port_attrs,
0695     .ct_owner       = THIS_MODULE,
0696 };
0697 
0698 /*
0699  * SCSI Transport Table
0700  */
0701 static struct se_lun *to_transport_stat(struct config_item *item)
0702 {
0703     struct se_port_stat_grps *pgrps = container_of(to_config_group(item),
0704             struct se_port_stat_grps, scsi_transport_group);
0705     return container_of(pgrps, struct se_lun, port_stat_grps);
0706 }
0707 
0708 static ssize_t target_stat_transport_inst_show(struct config_item *item,
0709         char *page)
0710 {
0711     struct se_lun *lun = to_transport_stat(item);
0712     struct se_device *dev;
0713     ssize_t ret = -ENODEV;
0714 
0715     rcu_read_lock();
0716     dev = rcu_dereference(lun->lun_se_dev);
0717     if (dev)
0718         ret = snprintf(page, PAGE_SIZE, "%u\n", dev->hba_index);
0719     rcu_read_unlock();
0720     return ret;
0721 }
0722 
0723 static ssize_t target_stat_transport_device_show(struct config_item *item,
0724         char *page)
0725 {
0726     struct se_lun *lun = to_transport_stat(item);
0727     struct se_device *dev;
0728     struct se_portal_group *tpg = lun->lun_tpg;
0729     ssize_t ret = -ENODEV;
0730 
0731     rcu_read_lock();
0732     dev = rcu_dereference(lun->lun_se_dev);
0733     if (dev) {
0734         /* scsiTransportType */
0735         ret = snprintf(page, PAGE_SIZE, "scsiTransport%s\n",
0736                    tpg->se_tpg_tfo->fabric_name);
0737     }
0738     rcu_read_unlock();
0739     return ret;
0740 }
0741 
0742 static ssize_t target_stat_transport_indx_show(struct config_item *item,
0743         char *page)
0744 {
0745     struct se_lun *lun = to_transport_stat(item);
0746     struct se_device *dev;
0747     struct se_portal_group *tpg = lun->lun_tpg;
0748     ssize_t ret = -ENODEV;
0749 
0750     rcu_read_lock();
0751     dev = rcu_dereference(lun->lun_se_dev);
0752     if (dev)
0753         ret = snprintf(page, PAGE_SIZE, "%u\n",
0754                    tpg->se_tpg_tfo->tpg_get_inst_index(tpg));
0755     rcu_read_unlock();
0756     return ret;
0757 }
0758 
0759 static ssize_t target_stat_transport_dev_name_show(struct config_item *item,
0760         char *page)
0761 {
0762     struct se_lun *lun = to_transport_stat(item);
0763     struct se_device *dev;
0764     struct se_portal_group *tpg = lun->lun_tpg;
0765     struct t10_wwn *wwn;
0766     ssize_t ret = -ENODEV;
0767 
0768     rcu_read_lock();
0769     dev = rcu_dereference(lun->lun_se_dev);
0770     if (dev) {
0771         wwn = &dev->t10_wwn;
0772         /* scsiTransportDevName */
0773         ret = snprintf(page, PAGE_SIZE, "%s+%s\n",
0774                 tpg->se_tpg_tfo->tpg_get_wwn(tpg),
0775                 (strlen(wwn->unit_serial)) ? wwn->unit_serial :
0776                 wwn->vendor);
0777     }
0778     rcu_read_unlock();
0779     return ret;
0780 }
0781 
0782 static ssize_t target_stat_transport_proto_id_show(struct config_item *item,
0783         char *page)
0784 {
0785     struct se_lun *lun = to_transport_stat(item);
0786     struct se_device *dev;
0787     struct se_portal_group *tpg = lun->lun_tpg;
0788     ssize_t ret = -ENODEV;
0789 
0790     rcu_read_lock();
0791     dev = rcu_dereference(lun->lun_se_dev);
0792     if (dev)
0793         ret = snprintf(page, PAGE_SIZE, "%u\n", tpg->proto_id);
0794     rcu_read_unlock();
0795     return ret;
0796 }
0797 
0798 CONFIGFS_ATTR_RO(target_stat_transport_, inst);
0799 CONFIGFS_ATTR_RO(target_stat_transport_, device);
0800 CONFIGFS_ATTR_RO(target_stat_transport_, indx);
0801 CONFIGFS_ATTR_RO(target_stat_transport_, dev_name);
0802 CONFIGFS_ATTR_RO(target_stat_transport_, proto_id);
0803 
0804 static struct configfs_attribute *target_stat_scsi_transport_attrs[] = {
0805     &target_stat_transport_attr_inst,
0806     &target_stat_transport_attr_device,
0807     &target_stat_transport_attr_indx,
0808     &target_stat_transport_attr_dev_name,
0809     &target_stat_transport_attr_proto_id,
0810     NULL,
0811 };
0812 
0813 static const struct config_item_type target_stat_scsi_transport_cit = {
0814     .ct_attrs       = target_stat_scsi_transport_attrs,
0815     .ct_owner       = THIS_MODULE,
0816 };
0817 
0818 /*
0819  * Called from target_core_fabric_configfs.c:target_fabric_make_lun() to setup
0820  * the target port statistics groups + configfs CITs located in target_core_stat.c
0821  */
0822 void target_stat_setup_port_default_groups(struct se_lun *lun)
0823 {
0824     config_group_init_type_name(&lun->port_stat_grps.scsi_port_group,
0825             "scsi_port", &target_stat_scsi_port_cit);
0826     configfs_add_default_group(&lun->port_stat_grps.scsi_port_group,
0827             &lun->port_stat_grps.stat_group);
0828 
0829     config_group_init_type_name(&lun->port_stat_grps.scsi_tgt_port_group,
0830             "scsi_tgt_port", &target_stat_scsi_tgt_port_cit);
0831     configfs_add_default_group(&lun->port_stat_grps.scsi_tgt_port_group,
0832             &lun->port_stat_grps.stat_group);
0833 
0834     config_group_init_type_name(&lun->port_stat_grps.scsi_transport_group,
0835             "scsi_transport", &target_stat_scsi_transport_cit);
0836     configfs_add_default_group(&lun->port_stat_grps.scsi_transport_group,
0837             &lun->port_stat_grps.stat_group);
0838 }
0839 
0840 /*
0841  * SCSI Authorized Initiator Table
0842  */
0843 
0844 static struct se_lun_acl *auth_to_lacl(struct config_item *item)
0845 {
0846     struct se_ml_stat_grps *lgrps = container_of(to_config_group(item),
0847             struct se_ml_stat_grps, scsi_auth_intr_group);
0848     return container_of(lgrps, struct se_lun_acl, ml_stat_grps);
0849 }
0850 
0851 static ssize_t target_stat_auth_inst_show(struct config_item *item,
0852         char *page)
0853 {
0854     struct se_lun_acl *lacl = auth_to_lacl(item);
0855     struct se_node_acl *nacl = lacl->se_lun_nacl;
0856     struct se_dev_entry *deve;
0857     struct se_portal_group *tpg;
0858     ssize_t ret;
0859 
0860     rcu_read_lock();
0861     deve = target_nacl_find_deve(nacl, lacl->mapped_lun);
0862     if (!deve) {
0863         rcu_read_unlock();
0864         return -ENODEV;
0865     }
0866     tpg = nacl->se_tpg;
0867     /* scsiInstIndex */
0868     ret = snprintf(page, PAGE_SIZE, "%u\n",
0869             tpg->se_tpg_tfo->tpg_get_inst_index(tpg));
0870     rcu_read_unlock();
0871     return ret;
0872 }
0873 
0874 static ssize_t target_stat_auth_dev_show(struct config_item *item,
0875         char *page)
0876 {
0877     struct se_lun_acl *lacl = auth_to_lacl(item);
0878     struct se_node_acl *nacl = lacl->se_lun_nacl;
0879     struct se_dev_entry *deve;
0880     ssize_t ret;
0881 
0882     rcu_read_lock();
0883     deve = target_nacl_find_deve(nacl, lacl->mapped_lun);
0884     if (!deve) {
0885         rcu_read_unlock();
0886         return -ENODEV;
0887     }
0888 
0889     /* scsiDeviceIndex */
0890     ret = snprintf(page, PAGE_SIZE, "%u\n", deve->se_lun->lun_index);
0891     rcu_read_unlock();
0892     return ret;
0893 }
0894 
0895 static ssize_t target_stat_auth_port_show(struct config_item *item,
0896         char *page)
0897 {
0898     struct se_lun_acl *lacl = auth_to_lacl(item);
0899     struct se_node_acl *nacl = lacl->se_lun_nacl;
0900     struct se_dev_entry *deve;
0901     struct se_portal_group *tpg;
0902     ssize_t ret;
0903 
0904     rcu_read_lock();
0905     deve = target_nacl_find_deve(nacl, lacl->mapped_lun);
0906     if (!deve) {
0907         rcu_read_unlock();
0908         return -ENODEV;
0909     }
0910     tpg = nacl->se_tpg;
0911     /* scsiAuthIntrTgtPortIndex */
0912     ret = snprintf(page, PAGE_SIZE, "%u\n", tpg->se_tpg_tfo->tpg_get_tag(tpg));
0913     rcu_read_unlock();
0914     return ret;
0915 }
0916 
0917 static ssize_t target_stat_auth_indx_show(struct config_item *item,
0918         char *page)
0919 {
0920     struct se_lun_acl *lacl = auth_to_lacl(item);
0921     struct se_node_acl *nacl = lacl->se_lun_nacl;
0922     struct se_dev_entry *deve;
0923     ssize_t ret;
0924 
0925     rcu_read_lock();
0926     deve = target_nacl_find_deve(nacl, lacl->mapped_lun);
0927     if (!deve) {
0928         rcu_read_unlock();
0929         return -ENODEV;
0930     }
0931     /* scsiAuthIntrIndex */
0932     ret = snprintf(page, PAGE_SIZE, "%u\n", nacl->acl_index);
0933     rcu_read_unlock();
0934     return ret;
0935 }
0936 
0937 static ssize_t target_stat_auth_dev_or_port_show(struct config_item *item,
0938         char *page)
0939 {
0940     struct se_lun_acl *lacl = auth_to_lacl(item);
0941     struct se_node_acl *nacl = lacl->se_lun_nacl;
0942     struct se_dev_entry *deve;
0943     ssize_t ret;
0944 
0945     rcu_read_lock();
0946     deve = target_nacl_find_deve(nacl, lacl->mapped_lun);
0947     if (!deve) {
0948         rcu_read_unlock();
0949         return -ENODEV;
0950     }
0951     /* scsiAuthIntrDevOrPort */
0952     ret = snprintf(page, PAGE_SIZE, "%u\n", 1);
0953     rcu_read_unlock();
0954     return ret;
0955 }
0956 
0957 static ssize_t target_stat_auth_intr_name_show(struct config_item *item,
0958         char *page)
0959 {
0960     struct se_lun_acl *lacl = auth_to_lacl(item);
0961     struct se_node_acl *nacl = lacl->se_lun_nacl;
0962     struct se_dev_entry *deve;
0963     ssize_t ret;
0964 
0965     rcu_read_lock();
0966     deve = target_nacl_find_deve(nacl, lacl->mapped_lun);
0967     if (!deve) {
0968         rcu_read_unlock();
0969         return -ENODEV;
0970     }
0971     /* scsiAuthIntrName */
0972     ret = snprintf(page, PAGE_SIZE, "%s\n", nacl->initiatorname);
0973     rcu_read_unlock();
0974     return ret;
0975 }
0976 
0977 static ssize_t target_stat_auth_map_indx_show(struct config_item *item,
0978         char *page)
0979 {
0980     struct se_lun_acl *lacl = auth_to_lacl(item);
0981     struct se_node_acl *nacl = lacl->se_lun_nacl;
0982     struct se_dev_entry *deve;
0983     ssize_t ret;
0984 
0985     rcu_read_lock();
0986     deve = target_nacl_find_deve(nacl, lacl->mapped_lun);
0987     if (!deve) {
0988         rcu_read_unlock();
0989         return -ENODEV;
0990     }
0991     /* FIXME: scsiAuthIntrLunMapIndex */
0992     ret = snprintf(page, PAGE_SIZE, "%u\n", 0);
0993     rcu_read_unlock();
0994     return ret;
0995 }
0996 
0997 static ssize_t target_stat_auth_att_count_show(struct config_item *item,
0998         char *page)
0999 {
1000     struct se_lun_acl *lacl = auth_to_lacl(item);
1001     struct se_node_acl *nacl = lacl->se_lun_nacl;
1002     struct se_dev_entry *deve;
1003     ssize_t ret;
1004 
1005     rcu_read_lock();
1006     deve = target_nacl_find_deve(nacl, lacl->mapped_lun);
1007     if (!deve) {
1008         rcu_read_unlock();
1009         return -ENODEV;
1010     }
1011     /* scsiAuthIntrAttachedTimes */
1012     ret = snprintf(page, PAGE_SIZE, "%u\n", deve->attach_count);
1013     rcu_read_unlock();
1014     return ret;
1015 }
1016 
1017 static ssize_t target_stat_auth_num_cmds_show(struct config_item *item,
1018         char *page)
1019 {
1020     struct se_lun_acl *lacl = auth_to_lacl(item);
1021     struct se_node_acl *nacl = lacl->se_lun_nacl;
1022     struct se_dev_entry *deve;
1023     ssize_t ret;
1024 
1025     rcu_read_lock();
1026     deve = target_nacl_find_deve(nacl, lacl->mapped_lun);
1027     if (!deve) {
1028         rcu_read_unlock();
1029         return -ENODEV;
1030     }
1031     /* scsiAuthIntrOutCommands */
1032     ret = snprintf(page, PAGE_SIZE, "%lu\n",
1033                atomic_long_read(&deve->total_cmds));
1034     rcu_read_unlock();
1035     return ret;
1036 }
1037 
1038 static ssize_t target_stat_auth_read_mbytes_show(struct config_item *item,
1039         char *page)
1040 {
1041     struct se_lun_acl *lacl = auth_to_lacl(item);
1042     struct se_node_acl *nacl = lacl->se_lun_nacl;
1043     struct se_dev_entry *deve;
1044     ssize_t ret;
1045 
1046     rcu_read_lock();
1047     deve = target_nacl_find_deve(nacl, lacl->mapped_lun);
1048     if (!deve) {
1049         rcu_read_unlock();
1050         return -ENODEV;
1051     }
1052     /* scsiAuthIntrReadMegaBytes */
1053     ret = snprintf(page, PAGE_SIZE, "%u\n",
1054               (u32)(atomic_long_read(&deve->read_bytes) >> 20));
1055     rcu_read_unlock();
1056     return ret;
1057 }
1058 
1059 static ssize_t target_stat_auth_write_mbytes_show(struct config_item *item,
1060         char *page)
1061 {
1062     struct se_lun_acl *lacl = auth_to_lacl(item);
1063     struct se_node_acl *nacl = lacl->se_lun_nacl;
1064     struct se_dev_entry *deve;
1065     ssize_t ret;
1066 
1067     rcu_read_lock();
1068     deve = target_nacl_find_deve(nacl, lacl->mapped_lun);
1069     if (!deve) {
1070         rcu_read_unlock();
1071         return -ENODEV;
1072     }
1073     /* scsiAuthIntrWrittenMegaBytes */
1074     ret = snprintf(page, PAGE_SIZE, "%u\n",
1075               (u32)(atomic_long_read(&deve->write_bytes) >> 20));
1076     rcu_read_unlock();
1077     return ret;
1078 }
1079 
1080 static ssize_t target_stat_auth_hs_num_cmds_show(struct config_item *item,
1081         char *page)
1082 {
1083     struct se_lun_acl *lacl = auth_to_lacl(item);
1084     struct se_node_acl *nacl = lacl->se_lun_nacl;
1085     struct se_dev_entry *deve;
1086     ssize_t ret;
1087 
1088     rcu_read_lock();
1089     deve = target_nacl_find_deve(nacl, lacl->mapped_lun);
1090     if (!deve) {
1091         rcu_read_unlock();
1092         return -ENODEV;
1093     }
1094     /* FIXME: scsiAuthIntrHSOutCommands */
1095     ret = snprintf(page, PAGE_SIZE, "%u\n", 0);
1096     rcu_read_unlock();
1097     return ret;
1098 }
1099 
1100 static ssize_t target_stat_auth_creation_time_show(struct config_item *item,
1101         char *page)
1102 {
1103     struct se_lun_acl *lacl = auth_to_lacl(item);
1104     struct se_node_acl *nacl = lacl->se_lun_nacl;
1105     struct se_dev_entry *deve;
1106     ssize_t ret;
1107 
1108     rcu_read_lock();
1109     deve = target_nacl_find_deve(nacl, lacl->mapped_lun);
1110     if (!deve) {
1111         rcu_read_unlock();
1112         return -ENODEV;
1113     }
1114     /* scsiAuthIntrLastCreation */
1115     ret = snprintf(page, PAGE_SIZE, "%u\n", (u32)(((u32)deve->creation_time -
1116                 INITIAL_JIFFIES) * 100 / HZ));
1117     rcu_read_unlock();
1118     return ret;
1119 }
1120 
1121 static ssize_t target_stat_auth_row_status_show(struct config_item *item,
1122         char *page)
1123 {
1124     struct se_lun_acl *lacl = auth_to_lacl(item);
1125     struct se_node_acl *nacl = lacl->se_lun_nacl;
1126     struct se_dev_entry *deve;
1127     ssize_t ret;
1128 
1129     rcu_read_lock();
1130     deve = target_nacl_find_deve(nacl, lacl->mapped_lun);
1131     if (!deve) {
1132         rcu_read_unlock();
1133         return -ENODEV;
1134     }
1135     /* FIXME: scsiAuthIntrRowStatus */
1136     ret = snprintf(page, PAGE_SIZE, "Ready\n");
1137     rcu_read_unlock();
1138     return ret;
1139 }
1140 
1141 CONFIGFS_ATTR_RO(target_stat_auth_, inst);
1142 CONFIGFS_ATTR_RO(target_stat_auth_, dev);
1143 CONFIGFS_ATTR_RO(target_stat_auth_, port);
1144 CONFIGFS_ATTR_RO(target_stat_auth_, indx);
1145 CONFIGFS_ATTR_RO(target_stat_auth_, dev_or_port);
1146 CONFIGFS_ATTR_RO(target_stat_auth_, intr_name);
1147 CONFIGFS_ATTR_RO(target_stat_auth_, map_indx);
1148 CONFIGFS_ATTR_RO(target_stat_auth_, att_count);
1149 CONFIGFS_ATTR_RO(target_stat_auth_, num_cmds);
1150 CONFIGFS_ATTR_RO(target_stat_auth_, read_mbytes);
1151 CONFIGFS_ATTR_RO(target_stat_auth_, write_mbytes);
1152 CONFIGFS_ATTR_RO(target_stat_auth_, hs_num_cmds);
1153 CONFIGFS_ATTR_RO(target_stat_auth_, creation_time);
1154 CONFIGFS_ATTR_RO(target_stat_auth_, row_status);
1155 
1156 static struct configfs_attribute *target_stat_scsi_auth_intr_attrs[] = {
1157     &target_stat_auth_attr_inst,
1158     &target_stat_auth_attr_dev,
1159     &target_stat_auth_attr_port,
1160     &target_stat_auth_attr_indx,
1161     &target_stat_auth_attr_dev_or_port,
1162     &target_stat_auth_attr_intr_name,
1163     &target_stat_auth_attr_map_indx,
1164     &target_stat_auth_attr_att_count,
1165     &target_stat_auth_attr_num_cmds,
1166     &target_stat_auth_attr_read_mbytes,
1167     &target_stat_auth_attr_write_mbytes,
1168     &target_stat_auth_attr_hs_num_cmds,
1169     &target_stat_auth_attr_creation_time,
1170     &target_stat_auth_attr_row_status,
1171     NULL,
1172 };
1173 
1174 static const struct config_item_type target_stat_scsi_auth_intr_cit = {
1175     .ct_attrs       = target_stat_scsi_auth_intr_attrs,
1176     .ct_owner       = THIS_MODULE,
1177 };
1178 
1179 /*
1180  * SCSI Attached Initiator Port Table
1181  */
1182 
1183 static struct se_lun_acl *iport_to_lacl(struct config_item *item)
1184 {
1185     struct se_ml_stat_grps *lgrps = container_of(to_config_group(item),
1186             struct se_ml_stat_grps, scsi_att_intr_port_group);
1187     return container_of(lgrps, struct se_lun_acl, ml_stat_grps);
1188 }
1189 
1190 static ssize_t target_stat_iport_inst_show(struct config_item *item,
1191         char *page)
1192 {
1193     struct se_lun_acl *lacl = iport_to_lacl(item);
1194     struct se_node_acl *nacl = lacl->se_lun_nacl;
1195     struct se_dev_entry *deve;
1196     struct se_portal_group *tpg;
1197     ssize_t ret;
1198 
1199     rcu_read_lock();
1200     deve = target_nacl_find_deve(nacl, lacl->mapped_lun);
1201     if (!deve) {
1202         rcu_read_unlock();
1203         return -ENODEV;
1204     }
1205     tpg = nacl->se_tpg;
1206     /* scsiInstIndex */
1207     ret = snprintf(page, PAGE_SIZE, "%u\n",
1208             tpg->se_tpg_tfo->tpg_get_inst_index(tpg));
1209     rcu_read_unlock();
1210     return ret;
1211 }
1212 
1213 static ssize_t target_stat_iport_dev_show(struct config_item *item,
1214         char *page)
1215 {
1216     struct se_lun_acl *lacl = iport_to_lacl(item);
1217     struct se_node_acl *nacl = lacl->se_lun_nacl;
1218     struct se_dev_entry *deve;
1219     ssize_t ret;
1220 
1221     rcu_read_lock();
1222     deve = target_nacl_find_deve(nacl, lacl->mapped_lun);
1223     if (!deve) {
1224         rcu_read_unlock();
1225         return -ENODEV;
1226     }
1227 
1228     /* scsiDeviceIndex */
1229     ret = snprintf(page, PAGE_SIZE, "%u\n", deve->se_lun->lun_index);
1230     rcu_read_unlock();
1231     return ret;
1232 }
1233 
1234 static ssize_t target_stat_iport_port_show(struct config_item *item,
1235         char *page)
1236 {
1237     struct se_lun_acl *lacl = iport_to_lacl(item);
1238     struct se_node_acl *nacl = lacl->se_lun_nacl;
1239     struct se_dev_entry *deve;
1240     struct se_portal_group *tpg;
1241     ssize_t ret;
1242 
1243     rcu_read_lock();
1244     deve = target_nacl_find_deve(nacl, lacl->mapped_lun);
1245     if (!deve) {
1246         rcu_read_unlock();
1247         return -ENODEV;
1248     }
1249     tpg = nacl->se_tpg;
1250     /* scsiPortIndex */
1251     ret = snprintf(page, PAGE_SIZE, "%u\n", tpg->se_tpg_tfo->tpg_get_tag(tpg));
1252     rcu_read_unlock();
1253     return ret;
1254 }
1255 
1256 static ssize_t target_stat_iport_indx_show(struct config_item *item,
1257         char *page)
1258 {
1259     struct se_lun_acl *lacl = iport_to_lacl(item);
1260     struct se_node_acl *nacl = lacl->se_lun_nacl;
1261     struct se_session *se_sess;
1262     struct se_portal_group *tpg;
1263     ssize_t ret;
1264 
1265     spin_lock_irq(&nacl->nacl_sess_lock);
1266     se_sess = nacl->nacl_sess;
1267     if (!se_sess) {
1268         spin_unlock_irq(&nacl->nacl_sess_lock);
1269         return -ENODEV;
1270     }
1271 
1272     tpg = nacl->se_tpg;
1273     /* scsiAttIntrPortIndex */
1274     ret = snprintf(page, PAGE_SIZE, "%u\n",
1275             tpg->se_tpg_tfo->sess_get_index(se_sess));
1276     spin_unlock_irq(&nacl->nacl_sess_lock);
1277     return ret;
1278 }
1279 
1280 static ssize_t target_stat_iport_port_auth_indx_show(struct config_item *item,
1281         char *page)
1282 {
1283     struct se_lun_acl *lacl = iport_to_lacl(item);
1284     struct se_node_acl *nacl = lacl->se_lun_nacl;
1285     struct se_dev_entry *deve;
1286     ssize_t ret;
1287 
1288     rcu_read_lock();
1289     deve = target_nacl_find_deve(nacl, lacl->mapped_lun);
1290     if (!deve) {
1291         rcu_read_unlock();
1292         return -ENODEV;
1293     }
1294     /* scsiAttIntrPortAuthIntrIdx */
1295     ret = snprintf(page, PAGE_SIZE, "%u\n", nacl->acl_index);
1296     rcu_read_unlock();
1297     return ret;
1298 }
1299 
1300 static ssize_t target_stat_iport_port_ident_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_session *se_sess;
1306     struct se_portal_group *tpg;
1307     ssize_t ret;
1308     unsigned char buf[64];
1309 
1310     spin_lock_irq(&nacl->nacl_sess_lock);
1311     se_sess = nacl->nacl_sess;
1312     if (!se_sess) {
1313         spin_unlock_irq(&nacl->nacl_sess_lock);
1314         return -ENODEV;
1315     }
1316 
1317     tpg = nacl->se_tpg;
1318     /* scsiAttIntrPortName+scsiAttIntrPortIdentifier */
1319     memset(buf, 0, 64);
1320     if (tpg->se_tpg_tfo->sess_get_initiator_sid != NULL)
1321         tpg->se_tpg_tfo->sess_get_initiator_sid(se_sess, buf, 64);
1322 
1323     ret = snprintf(page, PAGE_SIZE, "%s+i+%s\n", nacl->initiatorname, buf);
1324     spin_unlock_irq(&nacl->nacl_sess_lock);
1325     return ret;
1326 }
1327 
1328 CONFIGFS_ATTR_RO(target_stat_iport_, inst);
1329 CONFIGFS_ATTR_RO(target_stat_iport_, dev);
1330 CONFIGFS_ATTR_RO(target_stat_iport_, port);
1331 CONFIGFS_ATTR_RO(target_stat_iport_, indx);
1332 CONFIGFS_ATTR_RO(target_stat_iport_, port_auth_indx);
1333 CONFIGFS_ATTR_RO(target_stat_iport_, port_ident);
1334 
1335 static struct configfs_attribute *target_stat_scsi_ath_intr_port_attrs[] = {
1336     &target_stat_iport_attr_inst,
1337     &target_stat_iport_attr_dev,
1338     &target_stat_iport_attr_port,
1339     &target_stat_iport_attr_indx,
1340     &target_stat_iport_attr_port_auth_indx,
1341     &target_stat_iport_attr_port_ident,
1342     NULL,
1343 };
1344 
1345 static const struct config_item_type target_stat_scsi_att_intr_port_cit = {
1346     .ct_attrs       = target_stat_scsi_ath_intr_port_attrs,
1347     .ct_owner       = THIS_MODULE,
1348 };
1349 
1350 /*
1351  * Called from target_core_fabric_configfs.c:target_fabric_make_mappedlun() to setup
1352  * the target MappedLUN statistics groups + configfs CITs located in target_core_stat.c
1353  */
1354 void target_stat_setup_mappedlun_default_groups(struct se_lun_acl *lacl)
1355 {
1356     config_group_init_type_name(&lacl->ml_stat_grps.scsi_auth_intr_group,
1357             "scsi_auth_intr", &target_stat_scsi_auth_intr_cit);
1358     configfs_add_default_group(&lacl->ml_stat_grps.scsi_auth_intr_group,
1359             &lacl->ml_stat_grps.stat_group);
1360 
1361     config_group_init_type_name(&lacl->ml_stat_grps.scsi_att_intr_port_group,
1362             "scsi_att_intr_port", &target_stat_scsi_att_intr_port_cit);
1363     configfs_add_default_group(&lacl->ml_stat_grps.scsi_att_intr_port_group,
1364             &lacl->ml_stat_grps.stat_group);
1365 }