1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*******************************************************************************
3  * Filename:  target_core_stat.c
4  *
5  * Modern ConfigFS group context specific statistics based on original
6  * target_core_mib.c code
7  *
8  * (c) Copyright 2006-2013 Datera, Inc.
9  *
10  * Nicholas A. Bellinger <nab@linux-iscsi.org>
11  *
12  ******************************************************************************/
13 
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/delay.h>
17 #include <linux/timer.h>
18 #include <linux/string.h>
19 #include <linux/utsname.h>
20 #include <linux/proc_fs.h>
21 #include <linux/seq_file.h>
22 #include <linux/configfs.h>
23 
24 #include <target/target_core_base.h>
25 #include <target/target_core_backend.h>
26 #include <target/target_core_fabric.h>
27 
28 #include "target_core_internal.h"
29 
30 #ifndef INITIAL_JIFFIES
31 #define INITIAL_JIFFIES ((unsigned long)(unsigned int) (-300*HZ))
32 #endif
33 
34 #define SCSI_LU_INDEX			1
35 #define LU_COUNT			1
36 
37 /*
38  * SCSI Device Table
39  */
40 
to_stat_dev(struct config_item * item)41 static struct se_device *to_stat_dev(struct config_item *item)
42 {
43 	struct se_dev_stat_grps *sgrps = container_of(to_config_group(item),
44 			struct se_dev_stat_grps, scsi_dev_group);
45 	return container_of(sgrps, struct se_device, dev_stat_grps);
46 }
47 
target_stat_inst_show(struct config_item * item,char * page)48 static ssize_t target_stat_inst_show(struct config_item *item, char *page)
49 {
50 	struct se_hba *hba = to_stat_dev(item)->se_hba;
51 
52 	return snprintf(page, PAGE_SIZE, "%u\n", hba->hba_index);
53 }
54 
target_stat_indx_show(struct config_item * item,char * page)55 static ssize_t target_stat_indx_show(struct config_item *item, char *page)
56 {
57 	return snprintf(page, PAGE_SIZE, "%u\n", to_stat_dev(item)->dev_index);
58 }
59 
target_stat_role_show(struct config_item * item,char * page)60 static ssize_t target_stat_role_show(struct config_item *item, char *page)
61 {
62 	return snprintf(page, PAGE_SIZE, "Target\n");
63 }
64 
target_stat_ports_show(struct config_item * item,char * page)65 static ssize_t target_stat_ports_show(struct config_item *item, char *page)
66 {
67 	return snprintf(page, PAGE_SIZE, "%u\n", to_stat_dev(item)->export_count);
68 }
69 
70 CONFIGFS_ATTR_RO(target_stat_, inst);
71 CONFIGFS_ATTR_RO(target_stat_, indx);
72 CONFIGFS_ATTR_RO(target_stat_, role);
73 CONFIGFS_ATTR_RO(target_stat_, ports);
74 
75 static struct configfs_attribute *target_stat_scsi_dev_attrs[] = {
76 	&target_stat_attr_inst,
77 	&target_stat_attr_indx,
78 	&target_stat_attr_role,
79 	&target_stat_attr_ports,
80 	NULL,
81 };
82 
83 static const struct config_item_type target_stat_scsi_dev_cit = {
84 	.ct_attrs		= target_stat_scsi_dev_attrs,
85 	.ct_owner		= THIS_MODULE,
86 };
87 
88 /*
89  * SCSI Target Device Table
90  */
to_stat_tgt_dev(struct config_item * item)91 static struct se_device *to_stat_tgt_dev(struct config_item *item)
92 {
93 	struct se_dev_stat_grps *sgrps = container_of(to_config_group(item),
94 			struct se_dev_stat_grps, scsi_tgt_dev_group);
95 	return container_of(sgrps, struct se_device, dev_stat_grps);
96 }
97 
target_stat_tgt_inst_show(struct config_item * item,char * page)98 static ssize_t target_stat_tgt_inst_show(struct config_item *item, char *page)
99 {
100 	struct se_hba *hba = to_stat_tgt_dev(item)->se_hba;
101 
102 	return snprintf(page, PAGE_SIZE, "%u\n", hba->hba_index);
103 }
104 
target_stat_tgt_indx_show(struct config_item * item,char * page)105 static ssize_t target_stat_tgt_indx_show(struct config_item *item, char *page)
106 {
107 	return snprintf(page, PAGE_SIZE, "%u\n", to_stat_tgt_dev(item)->dev_index);
108 }
109 
target_stat_tgt_num_lus_show(struct config_item * item,char * page)110 static ssize_t target_stat_tgt_num_lus_show(struct config_item *item,
111 		char *page)
112 {
113 	return snprintf(page, PAGE_SIZE, "%u\n", LU_COUNT);
114 }
115 
target_stat_tgt_status_show(struct config_item * item,char * page)116 static ssize_t target_stat_tgt_status_show(struct config_item *item,
117 		char *page)
118 {
119 	if (to_stat_tgt_dev(item)->export_count)
120 		return snprintf(page, PAGE_SIZE, "activated");
121 	else
122 		return snprintf(page, PAGE_SIZE, "deactivated");
123 }
124 
target_stat_tgt_non_access_lus_show(struct config_item * item,char * page)125 static ssize_t target_stat_tgt_non_access_lus_show(struct config_item *item,
126 		char *page)
127 {
128 	int non_accessible_lus;
129 
130 	if (to_stat_tgt_dev(item)->export_count)
131 		non_accessible_lus = 0;
132 	else
133 		non_accessible_lus = 1;
134 
135 	return snprintf(page, PAGE_SIZE, "%u\n", non_accessible_lus);
136 }
137 
target_stat_tgt_resets_show(struct config_item * item,char * page)138 static ssize_t target_stat_tgt_resets_show(struct config_item *item,
139 		char *page)
140 {
141 	return snprintf(page, PAGE_SIZE, "%lu\n",
142 			atomic_long_read(&to_stat_tgt_dev(item)->num_resets));
143 }
144 
target_stat_tgt_aborts_complete_show(struct config_item * item,char * page)145 static ssize_t target_stat_tgt_aborts_complete_show(struct config_item *item,
146 		char *page)
147 {
148 	return snprintf(page, PAGE_SIZE, "%lu\n",
149 			atomic_long_read(&to_stat_tgt_dev(item)->aborts_complete));
150 }
151 
target_stat_tgt_aborts_no_task_show(struct config_item * item,char * page)152 static ssize_t target_stat_tgt_aborts_no_task_show(struct config_item *item,
153 		char *page)
154 {
155 	return snprintf(page, PAGE_SIZE, "%lu\n",
156 			atomic_long_read(&to_stat_tgt_dev(item)->aborts_no_task));
157 }
158 
159 CONFIGFS_ATTR_RO(target_stat_tgt_, inst);
160 CONFIGFS_ATTR_RO(target_stat_tgt_, indx);
161 CONFIGFS_ATTR_RO(target_stat_tgt_, num_lus);
162 CONFIGFS_ATTR_RO(target_stat_tgt_, status);
163 CONFIGFS_ATTR_RO(target_stat_tgt_, non_access_lus);
164 CONFIGFS_ATTR_RO(target_stat_tgt_, resets);
165 CONFIGFS_ATTR_RO(target_stat_tgt_, aborts_complete);
166 CONFIGFS_ATTR_RO(target_stat_tgt_, aborts_no_task);
167 
168 static struct configfs_attribute *target_stat_scsi_tgt_dev_attrs[] = {
169 	&target_stat_tgt_attr_inst,
170 	&target_stat_tgt_attr_indx,
171 	&target_stat_tgt_attr_num_lus,
172 	&target_stat_tgt_attr_status,
173 	&target_stat_tgt_attr_non_access_lus,
174 	&target_stat_tgt_attr_resets,
175 	&target_stat_tgt_attr_aborts_complete,
176 	&target_stat_tgt_attr_aborts_no_task,
177 	NULL,
178 };
179 
180 static const struct config_item_type target_stat_scsi_tgt_dev_cit = {
181 	.ct_attrs		= target_stat_scsi_tgt_dev_attrs,
182 	.ct_owner		= THIS_MODULE,
183 };
184 
185 /*
186  * SCSI Logical Unit Table
187  */
188 
to_stat_lu_dev(struct config_item * item)189 static struct se_device *to_stat_lu_dev(struct config_item *item)
190 {
191 	struct se_dev_stat_grps *sgrps = container_of(to_config_group(item),
192 			struct se_dev_stat_grps, scsi_lu_group);
193 	return container_of(sgrps, struct se_device, dev_stat_grps);
194 }
195 
target_stat_lu_inst_show(struct config_item * item,char * page)196 static ssize_t target_stat_lu_inst_show(struct config_item *item, char *page)
197 {
198 	struct se_hba *hba = to_stat_lu_dev(item)->se_hba;
199 
200 	return snprintf(page, PAGE_SIZE, "%u\n", hba->hba_index);
201 }
202 
target_stat_lu_dev_show(struct config_item * item,char * page)203 static ssize_t target_stat_lu_dev_show(struct config_item *item, char *page)
204 {
205 	return snprintf(page, PAGE_SIZE, "%u\n",
206 			to_stat_lu_dev(item)->dev_index);
207 }
208 
target_stat_lu_indx_show(struct config_item * item,char * page)209 static ssize_t target_stat_lu_indx_show(struct config_item *item, char *page)
210 {
211 	return snprintf(page, PAGE_SIZE, "%u\n", SCSI_LU_INDEX);
212 }
213 
target_stat_lu_lun_show(struct config_item * item,char * page)214 static ssize_t target_stat_lu_lun_show(struct config_item *item, char *page)
215 {
216 	/* FIXME: scsiLuDefaultLun */
217 	return snprintf(page, PAGE_SIZE, "%llu\n", (unsigned long long)0);
218 }
219 
target_stat_lu_lu_name_show(struct config_item * item,char * page)220 static ssize_t target_stat_lu_lu_name_show(struct config_item *item, char *page)
221 {
222 	struct se_device *dev = to_stat_lu_dev(item);
223 
224 	/* scsiLuWwnName */
225 	return snprintf(page, PAGE_SIZE, "%s\n",
226 			(strlen(dev->t10_wwn.unit_serial)) ?
227 			dev->t10_wwn.unit_serial : "None");
228 }
229 
target_stat_lu_vend_show(struct config_item * item,char * page)230 static ssize_t target_stat_lu_vend_show(struct config_item *item, char *page)
231 {
232 	struct se_device *dev = to_stat_lu_dev(item);
233 
234 	return snprintf(page, PAGE_SIZE, "%-" __stringify(INQUIRY_VENDOR_LEN)
235 			"s\n", dev->t10_wwn.vendor);
236 }
237 
target_stat_lu_prod_show(struct config_item * item,char * page)238 static ssize_t target_stat_lu_prod_show(struct config_item *item, char *page)
239 {
240 	struct se_device *dev = to_stat_lu_dev(item);
241 
242 	return snprintf(page, PAGE_SIZE, "%-" __stringify(INQUIRY_MODEL_LEN)
243 			"s\n", dev->t10_wwn.model);
244 }
245 
target_stat_lu_rev_show(struct config_item * item,char * page)246 static ssize_t target_stat_lu_rev_show(struct config_item *item, char *page)
247 {
248 	struct se_device *dev = to_stat_lu_dev(item);
249 
250 	return snprintf(page, PAGE_SIZE, "%-" __stringify(INQUIRY_REVISION_LEN)
251 			"s\n", dev->t10_wwn.revision);
252 }
253 
target_stat_lu_dev_type_show(struct config_item * item,char * page)254 static ssize_t target_stat_lu_dev_type_show(struct config_item *item, char *page)
255 {
256 	struct se_device *dev = to_stat_lu_dev(item);
257 
258 	/* scsiLuPeripheralType */
259 	return snprintf(page, PAGE_SIZE, "%u\n",
260 			dev->transport->get_device_type(dev));
261 }
262 
target_stat_lu_status_show(struct config_item * item,char * page)263 static ssize_t target_stat_lu_status_show(struct config_item *item, char *page)
264 {
265 	struct se_device *dev = to_stat_lu_dev(item);
266 
267 	/* scsiLuStatus */
268 	return snprintf(page, PAGE_SIZE, "%s\n",
269 		(dev->export_count) ? "available" : "notavailable");
270 }
271 
target_stat_lu_state_bit_show(struct config_item * item,char * page)272 static ssize_t target_stat_lu_state_bit_show(struct config_item *item,
273 		char *page)
274 {
275 	/* scsiLuState */
276 	return snprintf(page, PAGE_SIZE, "exposed\n");
277 }
278 
target_stat_lu_num_cmds_show(struct config_item * item,char * page)279 static ssize_t target_stat_lu_num_cmds_show(struct config_item *item,
280 		char *page)
281 {
282 	struct se_device *dev = to_stat_lu_dev(item);
283 
284 	/* scsiLuNumCommands */
285 	return snprintf(page, PAGE_SIZE, "%lu\n",
286 			atomic_long_read(&dev->num_cmds));
287 }
288 
target_stat_lu_read_mbytes_show(struct config_item * item,char * page)289 static ssize_t target_stat_lu_read_mbytes_show(struct config_item *item,
290 		char *page)
291 {
292 	struct se_device *dev = to_stat_lu_dev(item);
293 
294 	/* scsiLuReadMegaBytes */
295 	return snprintf(page, PAGE_SIZE, "%lu\n",
296 			atomic_long_read(&dev->read_bytes) >> 20);
297 }
298 
target_stat_lu_write_mbytes_show(struct config_item * item,char * page)299 static ssize_t target_stat_lu_write_mbytes_show(struct config_item *item,
300 		char *page)
301 {
302 	struct se_device *dev = to_stat_lu_dev(item);
303 
304 	/* scsiLuWrittenMegaBytes */
305 	return snprintf(page, PAGE_SIZE, "%lu\n",
306 			atomic_long_read(&dev->write_bytes) >> 20);
307 }
308 
target_stat_lu_resets_show(struct config_item * item,char * page)309 static ssize_t target_stat_lu_resets_show(struct config_item *item, char *page)
310 {
311 	struct se_device *dev = to_stat_lu_dev(item);
312 
313 	/* scsiLuInResets */
314 	return snprintf(page, PAGE_SIZE, "%lu\n",
315 		atomic_long_read(&dev->num_resets));
316 }
317 
target_stat_lu_full_stat_show(struct config_item * item,char * page)318 static ssize_t target_stat_lu_full_stat_show(struct config_item *item,
319 		char *page)
320 {
321 	/* FIXME: scsiLuOutTaskSetFullStatus */
322 	return snprintf(page, PAGE_SIZE, "%u\n", 0);
323 }
324 
target_stat_lu_hs_num_cmds_show(struct config_item * item,char * page)325 static ssize_t target_stat_lu_hs_num_cmds_show(struct config_item *item,
326 		char *page)
327 {
328 	/* FIXME: scsiLuHSInCommands */
329 	return snprintf(page, PAGE_SIZE, "%u\n", 0);
330 }
331 
target_stat_lu_creation_time_show(struct config_item * item,char * page)332 static ssize_t target_stat_lu_creation_time_show(struct config_item *item,
333 		char *page)
334 {
335 	struct se_device *dev = to_stat_lu_dev(item);
336 
337 	/* scsiLuCreationTime */
338 	return snprintf(page, PAGE_SIZE, "%u\n", (u32)(((u32)dev->creation_time -
339 				INITIAL_JIFFIES) * 100 / HZ));
340 }
341 
342 CONFIGFS_ATTR_RO(target_stat_lu_, inst);
343 CONFIGFS_ATTR_RO(target_stat_lu_, dev);
344 CONFIGFS_ATTR_RO(target_stat_lu_, indx);
345 CONFIGFS_ATTR_RO(target_stat_lu_, lun);
346 CONFIGFS_ATTR_RO(target_stat_lu_, lu_name);
347 CONFIGFS_ATTR_RO(target_stat_lu_, vend);
348 CONFIGFS_ATTR_RO(target_stat_lu_, prod);
349 CONFIGFS_ATTR_RO(target_stat_lu_, rev);
350 CONFIGFS_ATTR_RO(target_stat_lu_, dev_type);
351 CONFIGFS_ATTR_RO(target_stat_lu_, status);
352 CONFIGFS_ATTR_RO(target_stat_lu_, state_bit);
353 CONFIGFS_ATTR_RO(target_stat_lu_, num_cmds);
354 CONFIGFS_ATTR_RO(target_stat_lu_, read_mbytes);
355 CONFIGFS_ATTR_RO(target_stat_lu_, write_mbytes);
356 CONFIGFS_ATTR_RO(target_stat_lu_, resets);
357 CONFIGFS_ATTR_RO(target_stat_lu_, full_stat);
358 CONFIGFS_ATTR_RO(target_stat_lu_, hs_num_cmds);
359 CONFIGFS_ATTR_RO(target_stat_lu_, creation_time);
360 
361 static struct configfs_attribute *target_stat_scsi_lu_attrs[] = {
362 	&target_stat_lu_attr_inst,
363 	&target_stat_lu_attr_dev,
364 	&target_stat_lu_attr_indx,
365 	&target_stat_lu_attr_lun,
366 	&target_stat_lu_attr_lu_name,
367 	&target_stat_lu_attr_vend,
368 	&target_stat_lu_attr_prod,
369 	&target_stat_lu_attr_rev,
370 	&target_stat_lu_attr_dev_type,
371 	&target_stat_lu_attr_status,
372 	&target_stat_lu_attr_state_bit,
373 	&target_stat_lu_attr_num_cmds,
374 	&target_stat_lu_attr_read_mbytes,
375 	&target_stat_lu_attr_write_mbytes,
376 	&target_stat_lu_attr_resets,
377 	&target_stat_lu_attr_full_stat,
378 	&target_stat_lu_attr_hs_num_cmds,
379 	&target_stat_lu_attr_creation_time,
380 	NULL,
381 };
382 
383 static const struct config_item_type target_stat_scsi_lu_cit = {
384 	.ct_attrs		= target_stat_scsi_lu_attrs,
385 	.ct_owner		= THIS_MODULE,
386 };
387 
388 /*
389  * Called from target_core_configfs.c:target_core_make_subdev() to setup
390  * the target statistics groups + configfs CITs located in target_core_stat.c
391  */
target_stat_setup_dev_default_groups(struct se_device * dev)392 void target_stat_setup_dev_default_groups(struct se_device *dev)
393 {
394 	config_group_init_type_name(&dev->dev_stat_grps.scsi_dev_group,
395 			"scsi_dev", &target_stat_scsi_dev_cit);
396 	configfs_add_default_group(&dev->dev_stat_grps.scsi_dev_group,
397 			&dev->dev_stat_grps.stat_group);
398 
399 	config_group_init_type_name(&dev->dev_stat_grps.scsi_tgt_dev_group,
400 			"scsi_tgt_dev", &target_stat_scsi_tgt_dev_cit);
401 	configfs_add_default_group(&dev->dev_stat_grps.scsi_tgt_dev_group,
402 			&dev->dev_stat_grps.stat_group);
403 
404 	config_group_init_type_name(&dev->dev_stat_grps.scsi_lu_group,
405 			"scsi_lu", &target_stat_scsi_lu_cit);
406 	configfs_add_default_group(&dev->dev_stat_grps.scsi_lu_group,
407 			&dev->dev_stat_grps.stat_group);
408 }
409 
410 /*
411  * SCSI Port Table
412  */
413 
to_stat_port(struct config_item * item)414 static struct se_lun *to_stat_port(struct config_item *item)
415 {
416 	struct se_port_stat_grps *pgrps = container_of(to_config_group(item),
417 			struct se_port_stat_grps, scsi_port_group);
418 	return container_of(pgrps, struct se_lun, port_stat_grps);
419 }
420 
target_stat_port_inst_show(struct config_item * item,char * page)421 static ssize_t target_stat_port_inst_show(struct config_item *item, char *page)
422 {
423 	struct se_lun *lun = to_stat_port(item);
424 	struct se_device *dev;
425 	ssize_t ret = -ENODEV;
426 
427 	rcu_read_lock();
428 	dev = rcu_dereference(lun->lun_se_dev);
429 	if (dev)
430 		ret = snprintf(page, PAGE_SIZE, "%u\n", dev->hba_index);
431 	rcu_read_unlock();
432 	return ret;
433 }
434 
target_stat_port_dev_show(struct config_item * item,char * page)435 static ssize_t target_stat_port_dev_show(struct config_item *item, char *page)
436 {
437 	struct se_lun *lun = to_stat_port(item);
438 	struct se_device *dev;
439 	ssize_t ret = -ENODEV;
440 
441 	rcu_read_lock();
442 	dev = rcu_dereference(lun->lun_se_dev);
443 	if (dev)
444 		ret = snprintf(page, PAGE_SIZE, "%u\n", dev->dev_index);
445 	rcu_read_unlock();
446 	return ret;
447 }
448 
target_stat_port_indx_show(struct config_item * item,char * page)449 static ssize_t target_stat_port_indx_show(struct config_item *item, char *page)
450 {
451 	struct se_lun *lun = to_stat_port(item);
452 	struct se_device *dev;
453 	ssize_t ret = -ENODEV;
454 
455 	rcu_read_lock();
456 	dev = rcu_dereference(lun->lun_se_dev);
457 	if (dev)
458 		ret = snprintf(page, PAGE_SIZE, "%u\n", lun->lun_tpg->tpg_rtpi);
459 	rcu_read_unlock();
460 	return ret;
461 }
462 
target_stat_port_role_show(struct config_item * item,char * page)463 static ssize_t target_stat_port_role_show(struct config_item *item, char *page)
464 {
465 	struct se_lun *lun = to_stat_port(item);
466 	struct se_device *dev;
467 	ssize_t ret = -ENODEV;
468 
469 	rcu_read_lock();
470 	dev = rcu_dereference(lun->lun_se_dev);
471 	if (dev)
472 		ret = snprintf(page, PAGE_SIZE, "%s%u\n", "Device", dev->dev_index);
473 	rcu_read_unlock();
474 	return ret;
475 }
476 
target_stat_port_busy_count_show(struct config_item * item,char * page)477 static ssize_t target_stat_port_busy_count_show(struct config_item *item,
478 		char *page)
479 {
480 	struct se_lun *lun = to_stat_port(item);
481 	struct se_device *dev;
482 	ssize_t ret = -ENODEV;
483 
484 	rcu_read_lock();
485 	dev = rcu_dereference(lun->lun_se_dev);
486 	if (dev) {
487 		/* FIXME: scsiPortBusyStatuses  */
488 		ret = snprintf(page, PAGE_SIZE, "%u\n", 0);
489 	}
490 	rcu_read_unlock();
491 	return ret;
492 }
493 
494 CONFIGFS_ATTR_RO(target_stat_port_, inst);
495 CONFIGFS_ATTR_RO(target_stat_port_, dev);
496 CONFIGFS_ATTR_RO(target_stat_port_, indx);
497 CONFIGFS_ATTR_RO(target_stat_port_, role);
498 CONFIGFS_ATTR_RO(target_stat_port_, busy_count);
499 
500 static struct configfs_attribute *target_stat_scsi_port_attrs[] = {
501 	&target_stat_port_attr_inst,
502 	&target_stat_port_attr_dev,
503 	&target_stat_port_attr_indx,
504 	&target_stat_port_attr_role,
505 	&target_stat_port_attr_busy_count,
506 	NULL,
507 };
508 
509 static const struct config_item_type target_stat_scsi_port_cit = {
510 	.ct_attrs		= target_stat_scsi_port_attrs,
511 	.ct_owner		= THIS_MODULE,
512 };
513 
514 /*
515  * SCSI Target Port Table
516  */
to_stat_tgt_port(struct config_item * item)517 static struct se_lun *to_stat_tgt_port(struct config_item *item)
518 {
519 	struct se_port_stat_grps *pgrps = container_of(to_config_group(item),
520 			struct se_port_stat_grps, scsi_tgt_port_group);
521 	return container_of(pgrps, struct se_lun, port_stat_grps);
522 }
523 
target_stat_tgt_port_inst_show(struct config_item * item,char * page)524 static ssize_t target_stat_tgt_port_inst_show(struct config_item *item,
525 		char *page)
526 {
527 	struct se_lun *lun = to_stat_tgt_port(item);
528 	struct se_device *dev;
529 	ssize_t ret = -ENODEV;
530 
531 	rcu_read_lock();
532 	dev = rcu_dereference(lun->lun_se_dev);
533 	if (dev)
534 		ret = snprintf(page, PAGE_SIZE, "%u\n", dev->hba_index);
535 	rcu_read_unlock();
536 	return ret;
537 }
538 
target_stat_tgt_port_dev_show(struct config_item * item,char * page)539 static ssize_t target_stat_tgt_port_dev_show(struct config_item *item,
540 		char *page)
541 {
542 	struct se_lun *lun = to_stat_tgt_port(item);
543 	struct se_device *dev;
544 	ssize_t ret = -ENODEV;
545 
546 	rcu_read_lock();
547 	dev = rcu_dereference(lun->lun_se_dev);
548 	if (dev)
549 		ret = snprintf(page, PAGE_SIZE, "%u\n", dev->dev_index);
550 	rcu_read_unlock();
551 	return ret;
552 }
553 
target_stat_tgt_port_indx_show(struct config_item * item,char * page)554 static ssize_t target_stat_tgt_port_indx_show(struct config_item *item,
555 		char *page)
556 {
557 	struct se_lun *lun = to_stat_tgt_port(item);
558 	struct se_device *dev;
559 	ssize_t ret = -ENODEV;
560 
561 	rcu_read_lock();
562 	dev = rcu_dereference(lun->lun_se_dev);
563 	if (dev)
564 		ret = snprintf(page, PAGE_SIZE, "%u\n", lun->lun_tpg->tpg_rtpi);
565 	rcu_read_unlock();
566 	return ret;
567 }
568 
target_stat_tgt_port_name_show(struct config_item * item,char * page)569 static ssize_t target_stat_tgt_port_name_show(struct config_item *item,
570 		char *page)
571 {
572 	struct se_lun *lun = to_stat_tgt_port(item);
573 	struct se_portal_group *tpg = lun->lun_tpg;
574 	struct se_device *dev;
575 	ssize_t ret = -ENODEV;
576 
577 	rcu_read_lock();
578 	dev = rcu_dereference(lun->lun_se_dev);
579 	if (dev)
580 		ret = snprintf(page, PAGE_SIZE, "%sPort#%u\n",
581 			tpg->se_tpg_tfo->fabric_name,
582 			lun->lun_tpg->tpg_rtpi);
583 	rcu_read_unlock();
584 	return ret;
585 }
586 
target_stat_tgt_port_port_index_show(struct config_item * item,char * page)587 static ssize_t target_stat_tgt_port_port_index_show(struct config_item *item,
588 		char *page)
589 {
590 	struct se_lun *lun = to_stat_tgt_port(item);
591 	struct se_portal_group *tpg = lun->lun_tpg;
592 	struct se_device *dev;
593 	ssize_t ret = -ENODEV;
594 
595 	rcu_read_lock();
596 	dev = rcu_dereference(lun->lun_se_dev);
597 	if (dev)
598 		ret = snprintf(page, PAGE_SIZE, "%s%s%d\n",
599 			tpg->se_tpg_tfo->tpg_get_wwn(tpg), "+t+",
600 			tpg->se_tpg_tfo->tpg_get_tag(tpg));
601 	rcu_read_unlock();
602 	return ret;
603 }
604 
target_stat_tgt_port_in_cmds_show(struct config_item * item,char * page)605 static ssize_t target_stat_tgt_port_in_cmds_show(struct config_item *item,
606 		char *page)
607 {
608 	struct se_lun *lun = to_stat_tgt_port(item);
609 	struct se_device *dev;
610 	ssize_t ret = -ENODEV;
611 
612 	rcu_read_lock();
613 	dev = rcu_dereference(lun->lun_se_dev);
614 	if (dev)
615 		ret = snprintf(page, PAGE_SIZE, "%lu\n",
616 			       atomic_long_read(&lun->lun_stats.cmd_pdus));
617 	rcu_read_unlock();
618 	return ret;
619 }
620 
target_stat_tgt_port_write_mbytes_show(struct config_item * item,char * page)621 static ssize_t target_stat_tgt_port_write_mbytes_show(struct config_item *item,
622 		char *page)
623 {
624 	struct se_lun *lun = to_stat_tgt_port(item);
625 	struct se_device *dev;
626 	ssize_t ret = -ENODEV;
627 
628 	rcu_read_lock();
629 	dev = rcu_dereference(lun->lun_se_dev);
630 	if (dev)
631 		ret = snprintf(page, PAGE_SIZE, "%u\n",
632 			(u32)(atomic_long_read(&lun->lun_stats.rx_data_octets) >> 20));
633 	rcu_read_unlock();
634 	return ret;
635 }
636 
target_stat_tgt_port_read_mbytes_show(struct config_item * item,char * page)637 static ssize_t target_stat_tgt_port_read_mbytes_show(struct config_item *item,
638 		char *page)
639 {
640 	struct se_lun *lun = to_stat_tgt_port(item);
641 	struct se_device *dev;
642 	ssize_t ret = -ENODEV;
643 
644 	rcu_read_lock();
645 	dev = rcu_dereference(lun->lun_se_dev);
646 	if (dev)
647 		ret = snprintf(page, PAGE_SIZE, "%u\n",
648 				(u32)(atomic_long_read(&lun->lun_stats.tx_data_octets) >> 20));
649 	rcu_read_unlock();
650 	return ret;
651 }
652 
target_stat_tgt_port_hs_in_cmds_show(struct config_item * item,char * page)653 static ssize_t target_stat_tgt_port_hs_in_cmds_show(struct config_item *item,
654 		char *page)
655 {
656 	struct se_lun *lun = to_stat_tgt_port(item);
657 	struct se_device *dev;
658 	ssize_t ret = -ENODEV;
659 
660 	rcu_read_lock();
661 	dev = rcu_dereference(lun->lun_se_dev);
662 	if (dev) {
663 		/* FIXME: scsiTgtPortHsInCommands */
664 		ret = snprintf(page, PAGE_SIZE, "%u\n", 0);
665 	}
666 	rcu_read_unlock();
667 	return ret;
668 }
669 
670 CONFIGFS_ATTR_RO(target_stat_tgt_port_, inst);
671 CONFIGFS_ATTR_RO(target_stat_tgt_port_, dev);
672 CONFIGFS_ATTR_RO(target_stat_tgt_port_, indx);
673 CONFIGFS_ATTR_RO(target_stat_tgt_port_, name);
674 CONFIGFS_ATTR_RO(target_stat_tgt_port_, port_index);
675 CONFIGFS_ATTR_RO(target_stat_tgt_port_, in_cmds);
676 CONFIGFS_ATTR_RO(target_stat_tgt_port_, write_mbytes);
677 CONFIGFS_ATTR_RO(target_stat_tgt_port_, read_mbytes);
678 CONFIGFS_ATTR_RO(target_stat_tgt_port_, hs_in_cmds);
679 
680 static struct configfs_attribute *target_stat_scsi_tgt_port_attrs[] = {
681 	&target_stat_tgt_port_attr_inst,
682 	&target_stat_tgt_port_attr_dev,
683 	&target_stat_tgt_port_attr_indx,
684 	&target_stat_tgt_port_attr_name,
685 	&target_stat_tgt_port_attr_port_index,
686 	&target_stat_tgt_port_attr_in_cmds,
687 	&target_stat_tgt_port_attr_write_mbytes,
688 	&target_stat_tgt_port_attr_read_mbytes,
689 	&target_stat_tgt_port_attr_hs_in_cmds,
690 	NULL,
691 };
692 
693 static const struct config_item_type target_stat_scsi_tgt_port_cit = {
694 	.ct_attrs		= target_stat_scsi_tgt_port_attrs,
695 	.ct_owner		= THIS_MODULE,
696 };
697 
698 /*
699  * SCSI Transport Table
700  */
to_transport_stat(struct config_item * item)701 static struct se_lun *to_transport_stat(struct config_item *item)
702 {
703 	struct se_port_stat_grps *pgrps = container_of(to_config_group(item),
704 			struct se_port_stat_grps, scsi_transport_group);
705 	return container_of(pgrps, struct se_lun, port_stat_grps);
706 }
707 
target_stat_transport_inst_show(struct config_item * item,char * page)708 static ssize_t target_stat_transport_inst_show(struct config_item *item,
709 		char *page)
710 {
711 	struct se_lun *lun = to_transport_stat(item);
712 	struct se_device *dev;
713 	ssize_t ret = -ENODEV;
714 
715 	rcu_read_lock();
716 	dev = rcu_dereference(lun->lun_se_dev);
717 	if (dev)
718 		ret = snprintf(page, PAGE_SIZE, "%u\n", dev->hba_index);
719 	rcu_read_unlock();
720 	return ret;
721 }
722 
target_stat_transport_device_show(struct config_item * item,char * page)723 static ssize_t target_stat_transport_device_show(struct config_item *item,
724 		char *page)
725 {
726 	struct se_lun *lun = to_transport_stat(item);
727 	struct se_device *dev;
728 	struct se_portal_group *tpg = lun->lun_tpg;
729 	ssize_t ret = -ENODEV;
730 
731 	rcu_read_lock();
732 	dev = rcu_dereference(lun->lun_se_dev);
733 	if (dev) {
734 		/* scsiTransportType */
735 		ret = snprintf(page, PAGE_SIZE, "scsiTransport%s\n",
736 			       tpg->se_tpg_tfo->fabric_name);
737 	}
738 	rcu_read_unlock();
739 	return ret;
740 }
741 
target_stat_transport_indx_show(struct config_item * item,char * page)742 static ssize_t target_stat_transport_indx_show(struct config_item *item,
743 		char *page)
744 {
745 	struct se_lun *lun = to_transport_stat(item);
746 	struct se_device *dev;
747 	struct se_portal_group *tpg = lun->lun_tpg;
748 	ssize_t ret = -ENODEV;
749 
750 	rcu_read_lock();
751 	dev = rcu_dereference(lun->lun_se_dev);
752 	if (dev)
753 		ret = snprintf(page, PAGE_SIZE, "%u\n",
754 			       tpg->se_tpg_tfo->tpg_get_inst_index(tpg));
755 	rcu_read_unlock();
756 	return ret;
757 }
758 
target_stat_transport_dev_name_show(struct config_item * item,char * page)759 static ssize_t target_stat_transport_dev_name_show(struct config_item *item,
760 		char *page)
761 {
762 	struct se_lun *lun = to_transport_stat(item);
763 	struct se_device *dev;
764 	struct se_portal_group *tpg = lun->lun_tpg;
765 	struct t10_wwn *wwn;
766 	ssize_t ret = -ENODEV;
767 
768 	rcu_read_lock();
769 	dev = rcu_dereference(lun->lun_se_dev);
770 	if (dev) {
771 		wwn = &dev->t10_wwn;
772 		/* scsiTransportDevName */
773 		ret = snprintf(page, PAGE_SIZE, "%s+%s\n",
774 				tpg->se_tpg_tfo->tpg_get_wwn(tpg),
775 				(strlen(wwn->unit_serial)) ? wwn->unit_serial :
776 				wwn->vendor);
777 	}
778 	rcu_read_unlock();
779 	return ret;
780 }
781 
target_stat_transport_proto_id_show(struct config_item * item,char * page)782 static ssize_t target_stat_transport_proto_id_show(struct config_item *item,
783 		char *page)
784 {
785 	struct se_lun *lun = to_transport_stat(item);
786 	struct se_device *dev;
787 	struct se_portal_group *tpg = lun->lun_tpg;
788 	ssize_t ret = -ENODEV;
789 
790 	rcu_read_lock();
791 	dev = rcu_dereference(lun->lun_se_dev);
792 	if (dev)
793 		ret = snprintf(page, PAGE_SIZE, "%u\n", tpg->proto_id);
794 	rcu_read_unlock();
795 	return ret;
796 }
797 
798 CONFIGFS_ATTR_RO(target_stat_transport_, inst);
799 CONFIGFS_ATTR_RO(target_stat_transport_, device);
800 CONFIGFS_ATTR_RO(target_stat_transport_, indx);
801 CONFIGFS_ATTR_RO(target_stat_transport_, dev_name);
802 CONFIGFS_ATTR_RO(target_stat_transport_, proto_id);
803 
804 static struct configfs_attribute *target_stat_scsi_transport_attrs[] = {
805 	&target_stat_transport_attr_inst,
806 	&target_stat_transport_attr_device,
807 	&target_stat_transport_attr_indx,
808 	&target_stat_transport_attr_dev_name,
809 	&target_stat_transport_attr_proto_id,
810 	NULL,
811 };
812 
813 static const struct config_item_type target_stat_scsi_transport_cit = {
814 	.ct_attrs		= target_stat_scsi_transport_attrs,
815 	.ct_owner		= THIS_MODULE,
816 };
817 
818 /*
819  * Called from target_core_fabric_configfs.c:target_fabric_make_lun() to setup
820  * the target port statistics groups + configfs CITs located in target_core_stat.c
821  */
target_stat_setup_port_default_groups(struct se_lun * lun)822 void target_stat_setup_port_default_groups(struct se_lun *lun)
823 {
824 	config_group_init_type_name(&lun->port_stat_grps.scsi_port_group,
825 			"scsi_port", &target_stat_scsi_port_cit);
826 	configfs_add_default_group(&lun->port_stat_grps.scsi_port_group,
827 			&lun->port_stat_grps.stat_group);
828 
829 	config_group_init_type_name(&lun->port_stat_grps.scsi_tgt_port_group,
830 			"scsi_tgt_port", &target_stat_scsi_tgt_port_cit);
831 	configfs_add_default_group(&lun->port_stat_grps.scsi_tgt_port_group,
832 			&lun->port_stat_grps.stat_group);
833 
834 	config_group_init_type_name(&lun->port_stat_grps.scsi_transport_group,
835 			"scsi_transport", &target_stat_scsi_transport_cit);
836 	configfs_add_default_group(&lun->port_stat_grps.scsi_transport_group,
837 			&lun->port_stat_grps.stat_group);
838 }
839 
840 /*
841  * SCSI Authorized Initiator Table
842  */
843 
auth_to_lacl(struct config_item * item)844 static struct se_lun_acl *auth_to_lacl(struct config_item *item)
845 {
846 	struct se_ml_stat_grps *lgrps = container_of(to_config_group(item),
847 			struct se_ml_stat_grps, scsi_auth_intr_group);
848 	return container_of(lgrps, struct se_lun_acl, ml_stat_grps);
849 }
850 
target_stat_auth_inst_show(struct config_item * item,char * page)851 static ssize_t target_stat_auth_inst_show(struct config_item *item,
852 		char *page)
853 {
854 	struct se_lun_acl *lacl = auth_to_lacl(item);
855 	struct se_node_acl *nacl = lacl->se_lun_nacl;
856 	struct se_dev_entry *deve;
857 	struct se_portal_group *tpg;
858 	ssize_t ret;
859 
860 	rcu_read_lock();
861 	deve = target_nacl_find_deve(nacl, lacl->mapped_lun);
862 	if (!deve) {
863 		rcu_read_unlock();
864 		return -ENODEV;
865 	}
866 	tpg = nacl->se_tpg;
867 	/* scsiInstIndex */
868 	ret = snprintf(page, PAGE_SIZE, "%u\n",
869 			tpg->se_tpg_tfo->tpg_get_inst_index(tpg));
870 	rcu_read_unlock();
871 	return ret;
872 }
873 
target_stat_auth_dev_show(struct config_item * item,char * page)874 static ssize_t target_stat_auth_dev_show(struct config_item *item,
875 		char *page)
876 {
877 	struct se_lun_acl *lacl = auth_to_lacl(item);
878 	struct se_node_acl *nacl = lacl->se_lun_nacl;
879 	struct se_dev_entry *deve;
880 	ssize_t ret;
881 
882 	rcu_read_lock();
883 	deve = target_nacl_find_deve(nacl, lacl->mapped_lun);
884 	if (!deve) {
885 		rcu_read_unlock();
886 		return -ENODEV;
887 	}
888 
889 	/* scsiDeviceIndex */
890 	ret = snprintf(page, PAGE_SIZE, "%u\n", deve->se_lun->lun_index);
891 	rcu_read_unlock();
892 	return ret;
893 }
894 
target_stat_auth_port_show(struct config_item * item,char * page)895 static ssize_t target_stat_auth_port_show(struct config_item *item,
896 		char *page)
897 {
898 	struct se_lun_acl *lacl = auth_to_lacl(item);
899 	struct se_node_acl *nacl = lacl->se_lun_nacl;
900 	struct se_dev_entry *deve;
901 	struct se_portal_group *tpg;
902 	ssize_t ret;
903 
904 	rcu_read_lock();
905 	deve = target_nacl_find_deve(nacl, lacl->mapped_lun);
906 	if (!deve) {
907 		rcu_read_unlock();
908 		return -ENODEV;
909 	}
910 	tpg = nacl->se_tpg;
911 	/* scsiAuthIntrTgtPortIndex */
912 	ret = snprintf(page, PAGE_SIZE, "%u\n", tpg->se_tpg_tfo->tpg_get_tag(tpg));
913 	rcu_read_unlock();
914 	return ret;
915 }
916 
target_stat_auth_indx_show(struct config_item * item,char * page)917 static ssize_t target_stat_auth_indx_show(struct config_item *item,
918 		char *page)
919 {
920 	struct se_lun_acl *lacl = auth_to_lacl(item);
921 	struct se_node_acl *nacl = lacl->se_lun_nacl;
922 	struct se_dev_entry *deve;
923 	ssize_t ret;
924 
925 	rcu_read_lock();
926 	deve = target_nacl_find_deve(nacl, lacl->mapped_lun);
927 	if (!deve) {
928 		rcu_read_unlock();
929 		return -ENODEV;
930 	}
931 	/* scsiAuthIntrIndex */
932 	ret = snprintf(page, PAGE_SIZE, "%u\n", nacl->acl_index);
933 	rcu_read_unlock();
934 	return ret;
935 }
936 
target_stat_auth_dev_or_port_show(struct config_item * item,char * page)937 static ssize_t target_stat_auth_dev_or_port_show(struct config_item *item,
938 		char *page)
939 {
940 	struct se_lun_acl *lacl = auth_to_lacl(item);
941 	struct se_node_acl *nacl = lacl->se_lun_nacl;
942 	struct se_dev_entry *deve;
943 	ssize_t ret;
944 
945 	rcu_read_lock();
946 	deve = target_nacl_find_deve(nacl, lacl->mapped_lun);
947 	if (!deve) {
948 		rcu_read_unlock();
949 		return -ENODEV;
950 	}
951 	/* scsiAuthIntrDevOrPort */
952 	ret = snprintf(page, PAGE_SIZE, "%u\n", 1);
953 	rcu_read_unlock();
954 	return ret;
955 }
956 
target_stat_auth_intr_name_show(struct config_item * item,char * page)957 static ssize_t target_stat_auth_intr_name_show(struct config_item *item,
958 		char *page)
959 {
960 	struct se_lun_acl *lacl = auth_to_lacl(item);
961 	struct se_node_acl *nacl = lacl->se_lun_nacl;
962 	struct se_dev_entry *deve;
963 	ssize_t ret;
964 
965 	rcu_read_lock();
966 	deve = target_nacl_find_deve(nacl, lacl->mapped_lun);
967 	if (!deve) {
968 		rcu_read_unlock();
969 		return -ENODEV;
970 	}
971 	/* scsiAuthIntrName */
972 	ret = snprintf(page, PAGE_SIZE, "%s\n", nacl->initiatorname);
973 	rcu_read_unlock();
974 	return ret;
975 }
976 
target_stat_auth_map_indx_show(struct config_item * item,char * page)977 static ssize_t target_stat_auth_map_indx_show(struct config_item *item,
978 		char *page)
979 {
980 	struct se_lun_acl *lacl = auth_to_lacl(item);
981 	struct se_node_acl *nacl = lacl->se_lun_nacl;
982 	struct se_dev_entry *deve;
983 	ssize_t ret;
984 
985 	rcu_read_lock();
986 	deve = target_nacl_find_deve(nacl, lacl->mapped_lun);
987 	if (!deve) {
988 		rcu_read_unlock();
989 		return -ENODEV;
990 	}
991 	/* FIXME: scsiAuthIntrLunMapIndex */
992 	ret = snprintf(page, PAGE_SIZE, "%u\n", 0);
993 	rcu_read_unlock();
994 	return ret;
995 }
996 
target_stat_auth_att_count_show(struct config_item * item,char * page)997 static ssize_t target_stat_auth_att_count_show(struct config_item *item,
998 		char *page)
999 {
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 
target_stat_auth_num_cmds_show(struct config_item * item,char * page)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 
target_stat_auth_read_mbytes_show(struct config_item * item,char * page)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 
target_stat_auth_write_mbytes_show(struct config_item * item,char * page)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 
target_stat_auth_hs_num_cmds_show(struct config_item * item,char * page)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 
target_stat_auth_creation_time_show(struct config_item * item,char * page)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 
target_stat_auth_row_status_show(struct config_item * item,char * page)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 
iport_to_lacl(struct config_item * item)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 
target_stat_iport_inst_show(struct config_item * item,char * page)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 
target_stat_iport_dev_show(struct config_item * item,char * page)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 
target_stat_iport_port_show(struct config_item * item,char * page)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 
target_stat_iport_indx_show(struct config_item * item,char * page)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 
target_stat_iport_port_auth_indx_show(struct config_item * item,char * page)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 
target_stat_iport_port_ident_show(struct config_item * item,char * page)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  */
target_stat_setup_mappedlun_default_groups(struct se_lun_acl * lacl)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 }
1366