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