xref: /openbmc/linux/drivers/scsi/qla2xxx/qla_dfs.c (revision f5c27da4)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * QLogic Fibre Channel HBA Driver
4  * Copyright (c)  2003-2014 QLogic Corporation
5  */
6 #include "qla_def.h"
7 
8 #include <linux/debugfs.h>
9 #include <linux/seq_file.h>
10 
11 static struct dentry *qla2x00_dfs_root;
12 static atomic_t qla2x00_dfs_root_count;
13 
14 #define QLA_DFS_RPORT_DEVLOSS_TMO	1
15 
16 static int
17 qla_dfs_rport_get(struct fc_port *fp, int attr_id, u64 *val)
18 {
19 	switch (attr_id) {
20 	case QLA_DFS_RPORT_DEVLOSS_TMO:
21 		/* Only supported for FC-NVMe devices that are registered. */
22 		if (!(fp->nvme_flag & NVME_FLAG_REGISTERED))
23 			return -EIO;
24 		*val = fp->nvme_remote_port->dev_loss_tmo;
25 		break;
26 	default:
27 		return -EINVAL;
28 	}
29 	return 0;
30 }
31 
32 static int
33 qla_dfs_rport_set(struct fc_port *fp, int attr_id, u64 val)
34 {
35 	switch (attr_id) {
36 	case QLA_DFS_RPORT_DEVLOSS_TMO:
37 		/* Only supported for FC-NVMe devices that are registered. */
38 		if (!(fp->nvme_flag & NVME_FLAG_REGISTERED))
39 			return -EIO;
40 #if (IS_ENABLED(CONFIG_NVME_FC))
41 		return nvme_fc_set_remoteport_devloss(fp->nvme_remote_port,
42 						      val);
43 #else /* CONFIG_NVME_FC */
44 		return -EINVAL;
45 #endif /* CONFIG_NVME_FC */
46 	default:
47 		return -EINVAL;
48 	}
49 	return 0;
50 }
51 
52 #define DEFINE_QLA_DFS_RPORT_RW_ATTR(_attr_id, _attr)		\
53 static int qla_dfs_rport_##_attr##_get(void *data, u64 *val)	\
54 {								\
55 	struct fc_port *fp = data;				\
56 	return qla_dfs_rport_get(fp, _attr_id, val);		\
57 }								\
58 static int qla_dfs_rport_##_attr##_set(void *data, u64 val)	\
59 {								\
60 	struct fc_port *fp = data;				\
61 	return qla_dfs_rport_set(fp, _attr_id, val);		\
62 }								\
63 DEFINE_DEBUGFS_ATTRIBUTE(qla_dfs_rport_##_attr##_fops,		\
64 		qla_dfs_rport_##_attr##_get,			\
65 		qla_dfs_rport_##_attr##_set, "%llu\n")
66 
67 /*
68  * Wrapper for getting fc_port fields.
69  *
70  * _attr    : Attribute name.
71  * _get_val : Accessor macro to retrieve the value.
72  */
73 #define DEFINE_QLA_DFS_RPORT_FIELD_GET(_attr, _get_val)			\
74 static int qla_dfs_rport_field_##_attr##_get(void *data, u64 *val)	\
75 {									\
76 	struct fc_port *fp = data;					\
77 	*val = _get_val;						\
78 	return 0;							\
79 }									\
80 DEFINE_DEBUGFS_ATTRIBUTE(qla_dfs_rport_field_##_attr##_fops,		\
81 		qla_dfs_rport_field_##_attr##_get,			\
82 		NULL, "%llu\n")
83 
84 #define DEFINE_QLA_DFS_RPORT_ACCESS(_attr, _get_val) \
85 	DEFINE_QLA_DFS_RPORT_FIELD_GET(_attr, _get_val)
86 
87 #define DEFINE_QLA_DFS_RPORT_FIELD(_attr) \
88 	DEFINE_QLA_DFS_RPORT_FIELD_GET(_attr, fp->_attr)
89 
90 DEFINE_QLA_DFS_RPORT_RW_ATTR(QLA_DFS_RPORT_DEVLOSS_TMO, dev_loss_tmo);
91 
92 DEFINE_QLA_DFS_RPORT_FIELD(disc_state);
93 DEFINE_QLA_DFS_RPORT_FIELD(scan_state);
94 DEFINE_QLA_DFS_RPORT_FIELD(fw_login_state);
95 DEFINE_QLA_DFS_RPORT_FIELD(login_pause);
96 DEFINE_QLA_DFS_RPORT_FIELD(flags);
97 DEFINE_QLA_DFS_RPORT_FIELD(nvme_flag);
98 DEFINE_QLA_DFS_RPORT_FIELD(last_rscn_gen);
99 DEFINE_QLA_DFS_RPORT_FIELD(rscn_gen);
100 DEFINE_QLA_DFS_RPORT_FIELD(login_gen);
101 DEFINE_QLA_DFS_RPORT_FIELD(loop_id);
102 DEFINE_QLA_DFS_RPORT_FIELD_GET(port_id, fp->d_id.b24);
103 DEFINE_QLA_DFS_RPORT_FIELD_GET(sess_kref, kref_read(&fp->sess_kref));
104 
105 void
106 qla2x00_dfs_create_rport(scsi_qla_host_t *vha, struct fc_port *fp)
107 {
108 	char wwn[32];
109 
110 #define QLA_CREATE_RPORT_FIELD_ATTR(_attr)			\
111 	debugfs_create_file(#_attr, 0400, fp->dfs_rport_dir,	\
112 		fp, &qla_dfs_rport_field_##_attr##_fops)
113 
114 	if (!vha->dfs_rport_root || fp->dfs_rport_dir)
115 		return;
116 
117 	sprintf(wwn, "pn-%016llx", wwn_to_u64(fp->port_name));
118 	fp->dfs_rport_dir = debugfs_create_dir(wwn, vha->dfs_rport_root);
119 	if (!fp->dfs_rport_dir)
120 		return;
121 	if (NVME_TARGET(vha->hw, fp))
122 		debugfs_create_file("dev_loss_tmo", 0600, fp->dfs_rport_dir,
123 				    fp, &qla_dfs_rport_dev_loss_tmo_fops);
124 
125 	QLA_CREATE_RPORT_FIELD_ATTR(disc_state);
126 	QLA_CREATE_RPORT_FIELD_ATTR(scan_state);
127 	QLA_CREATE_RPORT_FIELD_ATTR(fw_login_state);
128 	QLA_CREATE_RPORT_FIELD_ATTR(login_pause);
129 	QLA_CREATE_RPORT_FIELD_ATTR(flags);
130 	QLA_CREATE_RPORT_FIELD_ATTR(nvme_flag);
131 	QLA_CREATE_RPORT_FIELD_ATTR(last_rscn_gen);
132 	QLA_CREATE_RPORT_FIELD_ATTR(rscn_gen);
133 	QLA_CREATE_RPORT_FIELD_ATTR(login_gen);
134 	QLA_CREATE_RPORT_FIELD_ATTR(loop_id);
135 	QLA_CREATE_RPORT_FIELD_ATTR(port_id);
136 	QLA_CREATE_RPORT_FIELD_ATTR(sess_kref);
137 }
138 
139 void
140 qla2x00_dfs_remove_rport(scsi_qla_host_t *vha, struct fc_port *fp)
141 {
142 	if (!vha->dfs_rport_root || !fp->dfs_rport_dir)
143 		return;
144 	debugfs_remove_recursive(fp->dfs_rport_dir);
145 	fp->dfs_rport_dir = NULL;
146 }
147 
148 static int
149 qla2x00_dfs_tgt_sess_show(struct seq_file *s, void *unused)
150 {
151 	scsi_qla_host_t *vha = s->private;
152 	struct qla_hw_data *ha = vha->hw;
153 	unsigned long flags;
154 	struct fc_port *sess = NULL;
155 	struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
156 
157 	seq_printf(s, "%s\n", vha->host_str);
158 	if (tgt) {
159 		seq_puts(s, "Port ID   Port Name                Handle\n");
160 
161 		spin_lock_irqsave(&ha->tgt.sess_lock, flags);
162 		list_for_each_entry(sess, &vha->vp_fcports, list)
163 			seq_printf(s, "%02x:%02x:%02x  %8phC  %d\n",
164 			    sess->d_id.b.domain, sess->d_id.b.area,
165 			    sess->d_id.b.al_pa, sess->port_name,
166 			    sess->loop_id);
167 		spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
168 	}
169 
170 	return 0;
171 }
172 
173 DEFINE_SHOW_ATTRIBUTE(qla2x00_dfs_tgt_sess);
174 
175 static int
176 qla2x00_dfs_tgt_port_database_show(struct seq_file *s, void *unused)
177 {
178 	scsi_qla_host_t *vha = s->private;
179 	struct qla_hw_data *ha = vha->hw;
180 	struct gid_list_info *gid_list;
181 	dma_addr_t gid_list_dma;
182 	fc_port_t fc_port;
183 	char *id_iter;
184 	int rc, i;
185 	uint16_t entries, loop_id;
186 
187 	seq_printf(s, "%s\n", vha->host_str);
188 	gid_list = dma_alloc_coherent(&ha->pdev->dev,
189 				      qla2x00_gid_list_size(ha),
190 				      &gid_list_dma, GFP_KERNEL);
191 	if (!gid_list) {
192 		ql_dbg(ql_dbg_user, vha, 0x7018,
193 		       "DMA allocation failed for %u\n",
194 		       qla2x00_gid_list_size(ha));
195 		return 0;
196 	}
197 
198 	rc = qla24xx_gidlist_wait(vha, gid_list, gid_list_dma,
199 				  &entries);
200 	if (rc != QLA_SUCCESS)
201 		goto out_free_id_list;
202 
203 	id_iter = (char *)gid_list;
204 
205 	seq_puts(s, "Port Name	Port ID		Loop ID\n");
206 
207 	for (i = 0; i < entries; i++) {
208 		struct gid_list_info *gid =
209 			(struct gid_list_info *)id_iter;
210 		loop_id = le16_to_cpu(gid->loop_id);
211 		memset(&fc_port, 0, sizeof(fc_port_t));
212 
213 		fc_port.loop_id = loop_id;
214 
215 		rc = qla24xx_gpdb_wait(vha, &fc_port, 0);
216 		seq_printf(s, "%8phC  %02x%02x%02x  %d\n",
217 			   fc_port.port_name, fc_port.d_id.b.domain,
218 			   fc_port.d_id.b.area, fc_port.d_id.b.al_pa,
219 			   fc_port.loop_id);
220 		id_iter += ha->gid_list_info_size;
221 	}
222 out_free_id_list:
223 	dma_free_coherent(&ha->pdev->dev, qla2x00_gid_list_size(ha),
224 			  gid_list, gid_list_dma);
225 
226 	return 0;
227 }
228 
229 DEFINE_SHOW_ATTRIBUTE(qla2x00_dfs_tgt_port_database);
230 
231 static int
232 qla_dfs_fw_resource_cnt_show(struct seq_file *s, void *unused)
233 {
234 	struct scsi_qla_host *vha = s->private;
235 	uint16_t mb[MAX_IOCB_MB_REG];
236 	int rc;
237 	struct qla_hw_data *ha = vha->hw;
238 	u16 iocbs_used, i;
239 
240 	rc = qla24xx_res_count_wait(vha, mb, SIZEOF_IOCB_MB_REG);
241 	if (rc != QLA_SUCCESS) {
242 		seq_printf(s, "Mailbox Command failed %d, mb %#x", rc, mb[0]);
243 	} else {
244 		seq_puts(s, "FW Resource count\n\n");
245 		seq_printf(s, "Original TGT exchg count[%d]\n", mb[1]);
246 		seq_printf(s, "Current TGT exchg count[%d]\n", mb[2]);
247 		seq_printf(s, "Current Initiator Exchange count[%d]\n", mb[3]);
248 		seq_printf(s, "Original Initiator Exchange count[%d]\n", mb[6]);
249 		seq_printf(s, "Current IOCB count[%d]\n", mb[7]);
250 		seq_printf(s, "Original IOCB count[%d]\n", mb[10]);
251 		seq_printf(s, "MAX VP count[%d]\n", mb[11]);
252 		seq_printf(s, "MAX FCF count[%d]\n", mb[12]);
253 		seq_printf(s, "Current free pageable XCB buffer cnt[%d]\n",
254 		    mb[20]);
255 		seq_printf(s, "Original Initiator fast XCB buffer cnt[%d]\n",
256 		    mb[21]);
257 		seq_printf(s, "Current free Initiator fast XCB buffer cnt[%d]\n",
258 		    mb[22]);
259 		seq_printf(s, "Original Target fast XCB buffer cnt[%d]\n",
260 		    mb[23]);
261 	}
262 
263 	if (ql2xenforce_iocb_limit) {
264 		/* lock is not require. It's an estimate. */
265 		iocbs_used = ha->base_qpair->fwres.iocbs_used;
266 		for (i = 0; i < ha->max_qpairs; i++) {
267 			if (ha->queue_pair_map[i])
268 				iocbs_used += ha->queue_pair_map[i]->fwres.iocbs_used;
269 		}
270 
271 		seq_printf(s, "Driver: estimate iocb used [%d] high water limit [%d]\n",
272 			   iocbs_used, ha->base_qpair->fwres.iocbs_limit);
273 	}
274 
275 	return 0;
276 }
277 
278 DEFINE_SHOW_ATTRIBUTE(qla_dfs_fw_resource_cnt);
279 
280 static int
281 qla_dfs_tgt_counters_show(struct seq_file *s, void *unused)
282 {
283 	struct scsi_qla_host *vha = s->private;
284 	struct qla_qpair *qpair = vha->hw->base_qpair;
285 	uint64_t qla_core_sbt_cmd, core_qla_que_buf, qla_core_ret_ctio,
286 		core_qla_snd_status, qla_core_ret_sta_ctio, core_qla_free_cmd,
287 		num_q_full_sent, num_alloc_iocb_failed, num_term_xchg_sent;
288 	u16 i;
289 	fc_port_t *fcport = NULL;
290 
291 	if (qla2x00_chip_is_down(vha))
292 		return 0;
293 
294 	qla_core_sbt_cmd = qpair->tgt_counters.qla_core_sbt_cmd;
295 	core_qla_que_buf = qpair->tgt_counters.core_qla_que_buf;
296 	qla_core_ret_ctio = qpair->tgt_counters.qla_core_ret_ctio;
297 	core_qla_snd_status = qpair->tgt_counters.core_qla_snd_status;
298 	qla_core_ret_sta_ctio = qpair->tgt_counters.qla_core_ret_sta_ctio;
299 	core_qla_free_cmd = qpair->tgt_counters.core_qla_free_cmd;
300 	num_q_full_sent = qpair->tgt_counters.num_q_full_sent;
301 	num_alloc_iocb_failed = qpair->tgt_counters.num_alloc_iocb_failed;
302 	num_term_xchg_sent = qpair->tgt_counters.num_term_xchg_sent;
303 
304 	for (i = 0; i < vha->hw->max_qpairs; i++) {
305 		qpair = vha->hw->queue_pair_map[i];
306 		if (!qpair)
307 			continue;
308 		qla_core_sbt_cmd += qpair->tgt_counters.qla_core_sbt_cmd;
309 		core_qla_que_buf += qpair->tgt_counters.core_qla_que_buf;
310 		qla_core_ret_ctio += qpair->tgt_counters.qla_core_ret_ctio;
311 		core_qla_snd_status += qpair->tgt_counters.core_qla_snd_status;
312 		qla_core_ret_sta_ctio +=
313 		    qpair->tgt_counters.qla_core_ret_sta_ctio;
314 		core_qla_free_cmd += qpair->tgt_counters.core_qla_free_cmd;
315 		num_q_full_sent += qpair->tgt_counters.num_q_full_sent;
316 		num_alloc_iocb_failed +=
317 		    qpair->tgt_counters.num_alloc_iocb_failed;
318 		num_term_xchg_sent += qpair->tgt_counters.num_term_xchg_sent;
319 	}
320 
321 	seq_puts(s, "Target Counters\n");
322 	seq_printf(s, "qla_core_sbt_cmd = %lld\n",
323 		qla_core_sbt_cmd);
324 	seq_printf(s, "qla_core_ret_sta_ctio = %lld\n",
325 		qla_core_ret_sta_ctio);
326 	seq_printf(s, "qla_core_ret_ctio = %lld\n",
327 		qla_core_ret_ctio);
328 	seq_printf(s, "core_qla_que_buf = %lld\n",
329 		core_qla_que_buf);
330 	seq_printf(s, "core_qla_snd_status = %lld\n",
331 		core_qla_snd_status);
332 	seq_printf(s, "core_qla_free_cmd = %lld\n",
333 		core_qla_free_cmd);
334 	seq_printf(s, "num alloc iocb failed = %lld\n",
335 		num_alloc_iocb_failed);
336 	seq_printf(s, "num term exchange sent = %lld\n",
337 		num_term_xchg_sent);
338 	seq_printf(s, "num Q full sent = %lld\n",
339 		num_q_full_sent);
340 
341 	/* DIF stats */
342 	seq_printf(s, "DIF Inp Bytes = %lld\n",
343 		vha->qla_stats.qla_dif_stats.dif_input_bytes);
344 	seq_printf(s, "DIF Outp Bytes = %lld\n",
345 		vha->qla_stats.qla_dif_stats.dif_output_bytes);
346 	seq_printf(s, "DIF Inp Req = %lld\n",
347 		vha->qla_stats.qla_dif_stats.dif_input_requests);
348 	seq_printf(s, "DIF Outp Req = %lld\n",
349 		vha->qla_stats.qla_dif_stats.dif_output_requests);
350 	seq_printf(s, "DIF Guard err = %d\n",
351 		vha->qla_stats.qla_dif_stats.dif_guard_err);
352 	seq_printf(s, "DIF Ref tag err = %d\n",
353 		vha->qla_stats.qla_dif_stats.dif_ref_tag_err);
354 	seq_printf(s, "DIF App tag err = %d\n",
355 		vha->qla_stats.qla_dif_stats.dif_app_tag_err);
356 
357 	seq_puts(s, "\n");
358 	seq_puts(s, "Initiator Error Counters\n");
359 	seq_printf(s, "HW Error Count =		%14lld\n",
360 		   vha->hw_err_cnt);
361 	seq_printf(s, "Link Down Count =	%14lld\n",
362 		   vha->short_link_down_cnt);
363 	seq_printf(s, "Interface Err Count =	%14lld\n",
364 		   vha->interface_err_cnt);
365 	seq_printf(s, "Cmd Timeout Count =	%14lld\n",
366 		   vha->cmd_timeout_cnt);
367 	seq_printf(s, "Reset Count =		%14lld\n",
368 		   vha->reset_cmd_err_cnt);
369 	seq_puts(s, "\n");
370 
371 	list_for_each_entry(fcport, &vha->vp_fcports, list) {
372 		if (!fcport->rport)
373 			continue;
374 
375 		seq_printf(s, "Target Num = %7d Link Down Count = %14lld\n",
376 			   fcport->rport->number, fcport->tgt_short_link_down_cnt);
377 	}
378 	seq_puts(s, "\n");
379 
380 	return 0;
381 }
382 
383 DEFINE_SHOW_ATTRIBUTE(qla_dfs_tgt_counters);
384 
385 static int
386 qla2x00_dfs_fce_show(struct seq_file *s, void *unused)
387 {
388 	scsi_qla_host_t *vha = s->private;
389 	uint32_t cnt;
390 	uint32_t *fce;
391 	uint64_t fce_start;
392 	struct qla_hw_data *ha = vha->hw;
393 
394 	mutex_lock(&ha->fce_mutex);
395 
396 	seq_puts(s, "FCE Trace Buffer\n");
397 	seq_printf(s, "In Pointer = %llx\n\n", (unsigned long long)ha->fce_wr);
398 	seq_printf(s, "Base = %llx\n\n", (unsigned long long) ha->fce_dma);
399 	seq_puts(s, "FCE Enable Registers\n");
400 	seq_printf(s, "%08x %08x %08x %08x %08x %08x\n",
401 	    ha->fce_mb[0], ha->fce_mb[2], ha->fce_mb[3], ha->fce_mb[4],
402 	    ha->fce_mb[5], ha->fce_mb[6]);
403 
404 	fce = (uint32_t *) ha->fce;
405 	fce_start = (unsigned long long) ha->fce_dma;
406 	for (cnt = 0; cnt < fce_calc_size(ha->fce_bufs) / 4; cnt++) {
407 		if (cnt % 8 == 0)
408 			seq_printf(s, "\n%llx: ",
409 			    (unsigned long long)((cnt * 4) + fce_start));
410 		else
411 			seq_putc(s, ' ');
412 		seq_printf(s, "%08x", *fce++);
413 	}
414 
415 	seq_puts(s, "\nEnd\n");
416 
417 	mutex_unlock(&ha->fce_mutex);
418 
419 	return 0;
420 }
421 
422 static int
423 qla2x00_dfs_fce_open(struct inode *inode, struct file *file)
424 {
425 	scsi_qla_host_t *vha = inode->i_private;
426 	struct qla_hw_data *ha = vha->hw;
427 	int rval;
428 
429 	if (!ha->flags.fce_enabled)
430 		goto out;
431 
432 	mutex_lock(&ha->fce_mutex);
433 
434 	/* Pause tracing to flush FCE buffers. */
435 	rval = qla2x00_disable_fce_trace(vha, &ha->fce_wr, &ha->fce_rd);
436 	if (rval)
437 		ql_dbg(ql_dbg_user, vha, 0x705c,
438 		    "DebugFS: Unable to disable FCE (%d).\n", rval);
439 
440 	ha->flags.fce_enabled = 0;
441 
442 	mutex_unlock(&ha->fce_mutex);
443 out:
444 	return single_open(file, qla2x00_dfs_fce_show, vha);
445 }
446 
447 static int
448 qla2x00_dfs_fce_release(struct inode *inode, struct file *file)
449 {
450 	scsi_qla_host_t *vha = inode->i_private;
451 	struct qla_hw_data *ha = vha->hw;
452 	int rval;
453 
454 	if (ha->flags.fce_enabled)
455 		goto out;
456 
457 	mutex_lock(&ha->fce_mutex);
458 
459 	/* Re-enable FCE tracing. */
460 	ha->flags.fce_enabled = 1;
461 	memset(ha->fce, 0, fce_calc_size(ha->fce_bufs));
462 	rval = qla2x00_enable_fce_trace(vha, ha->fce_dma, ha->fce_bufs,
463 	    ha->fce_mb, &ha->fce_bufs);
464 	if (rval) {
465 		ql_dbg(ql_dbg_user, vha, 0x700d,
466 		    "DebugFS: Unable to reinitialize FCE (%d).\n", rval);
467 		ha->flags.fce_enabled = 0;
468 	}
469 
470 	mutex_unlock(&ha->fce_mutex);
471 out:
472 	return single_release(inode, file);
473 }
474 
475 static const struct file_operations dfs_fce_ops = {
476 	.open		= qla2x00_dfs_fce_open,
477 	.read		= seq_read,
478 	.llseek		= seq_lseek,
479 	.release	= qla2x00_dfs_fce_release,
480 };
481 
482 static int
483 qla_dfs_naqp_show(struct seq_file *s, void *unused)
484 {
485 	struct scsi_qla_host *vha = s->private;
486 	struct qla_hw_data *ha = vha->hw;
487 
488 	seq_printf(s, "%d\n", ha->tgt.num_act_qpairs);
489 	return 0;
490 }
491 
492 /*
493  * Helper macros for setting up debugfs entries.
494  * _name: The name of the debugfs entry
495  * _ctx_struct: The context that was passed when creating the debugfs file
496  *
497  * QLA_DFS_SETUP_RD could be used when there is only a show function.
498  * - show function take the name qla_dfs_<sysfs-name>_show
499  *
500  * QLA_DFS_SETUP_RW could be used when there are both show and write functions.
501  * - show function take the name  qla_dfs_<sysfs-name>_show
502  * - write function take the name qla_dfs_<sysfs-name>_write
503  *
504  * To have a new debugfs entry, do:
505  * 1. Create a "struct dentry *" in the appropriate structure in the format
506  * dfs_<sysfs-name>
507  * 2. Setup debugfs entries using QLA_DFS_SETUP_RD / QLA_DFS_SETUP_RW
508  * 3. Create debugfs file in qla2x00_dfs_setup() using QLA_DFS_CREATE_FILE
509  * or QLA_DFS_ROOT_CREATE_FILE
510  * 4. Remove debugfs file in qla2x00_dfs_remove() using QLA_DFS_REMOVE_FILE
511  * or QLA_DFS_ROOT_REMOVE_FILE
512  *
513  * Example for creating "TEST" sysfs file:
514  * 1. struct qla_hw_data { ... struct dentry *dfs_TEST; }
515  * 2. QLA_DFS_SETUP_RD(TEST, scsi_qla_host_t);
516  * 3. In qla2x00_dfs_setup():
517  * QLA_DFS_CREATE_FILE(ha, TEST, 0600, ha->dfs_dir, vha);
518  * 4. In qla2x00_dfs_remove():
519  * QLA_DFS_REMOVE_FILE(ha, TEST);
520  */
521 #define QLA_DFS_SETUP_RD(_name, _ctx_struct)				\
522 static int								\
523 qla_dfs_##_name##_open(struct inode *inode, struct file *file)		\
524 {									\
525 	_ctx_struct *__ctx = inode->i_private;				\
526 									\
527 	return single_open(file, qla_dfs_##_name##_show, __ctx);	\
528 }									\
529 									\
530 static const struct file_operations qla_dfs_##_name##_ops = {		\
531 	.open           = qla_dfs_##_name##_open,			\
532 	.read           = seq_read,					\
533 	.llseek         = seq_lseek,					\
534 	.release        = single_release,				\
535 };
536 
537 #define QLA_DFS_SETUP_RW(_name, _ctx_struct)				\
538 static int								\
539 qla_dfs_##_name##_open(struct inode *inode, struct file *file)		\
540 {									\
541 	_ctx_struct *__ctx = inode->i_private;				\
542 									\
543 	return single_open(file, qla_dfs_##_name##_show, __ctx);	\
544 }									\
545 									\
546 static const struct file_operations qla_dfs_##_name##_ops = {		\
547 	.open           = qla_dfs_##_name##_open,			\
548 	.read           = seq_read,					\
549 	.llseek         = seq_lseek,					\
550 	.release        = single_release,				\
551 	.write		= qla_dfs_##_name##_write,			\
552 };
553 
554 #define QLA_DFS_ROOT_CREATE_FILE(_name, _perm, _ctx)			\
555 	do {								\
556 		if (!qla_dfs_##_name)					\
557 			qla_dfs_##_name = debugfs_create_file(#_name,	\
558 					_perm, qla2x00_dfs_root, _ctx,	\
559 					&qla_dfs_##_name##_ops);	\
560 	} while (0)
561 
562 #define QLA_DFS_ROOT_REMOVE_FILE(_name)					\
563 	do {								\
564 		if (qla_dfs_##_name) {					\
565 			debugfs_remove(qla_dfs_##_name);		\
566 			qla_dfs_##_name = NULL;				\
567 		}							\
568 	} while (0)
569 
570 #define QLA_DFS_CREATE_FILE(_struct, _name, _perm, _parent, _ctx)	\
571 	do {								\
572 		(_struct)->dfs_##_name = debugfs_create_file(#_name,	\
573 					_perm, _parent, _ctx,		\
574 					&qla_dfs_##_name##_ops)		\
575 	} while (0)
576 
577 #define QLA_DFS_REMOVE_FILE(_struct, _name)				\
578 	do {								\
579 		if ((_struct)->dfs_##_name) {				\
580 			debugfs_remove((_struct)->dfs_##_name);		\
581 			(_struct)->dfs_##_name = NULL;			\
582 		}							\
583 	} while (0)
584 
585 static int
586 qla_dfs_naqp_open(struct inode *inode, struct file *file)
587 {
588 	struct scsi_qla_host *vha = inode->i_private;
589 
590 	return single_open(file, qla_dfs_naqp_show, vha);
591 }
592 
593 static ssize_t
594 qla_dfs_naqp_write(struct file *file, const char __user *buffer,
595     size_t count, loff_t *pos)
596 {
597 	struct seq_file *s = file->private_data;
598 	struct scsi_qla_host *vha = s->private;
599 	struct qla_hw_data *ha = vha->hw;
600 	char *buf;
601 	int rc = 0;
602 	unsigned long num_act_qp;
603 
604 	if (!(IS_QLA27XX(ha) || IS_QLA83XX(ha) || IS_QLA28XX(ha))) {
605 		pr_err("host%ld: this adapter does not support Multi Q.",
606 		    vha->host_no);
607 		return -EINVAL;
608 	}
609 
610 	if (!vha->flags.qpairs_available) {
611 		pr_err("host%ld: Driver is not setup with Multi Q.",
612 		    vha->host_no);
613 		return -EINVAL;
614 	}
615 	buf = memdup_user_nul(buffer, count);
616 	if (IS_ERR(buf)) {
617 		pr_err("host%ld: fail to copy user buffer.",
618 		    vha->host_no);
619 		return PTR_ERR(buf);
620 	}
621 
622 	num_act_qp = simple_strtoul(buf, NULL, 0);
623 
624 	if (num_act_qp >= vha->hw->max_qpairs) {
625 		pr_err("User set invalid number of qpairs %lu. Max = %d",
626 		    num_act_qp, vha->hw->max_qpairs);
627 		rc = -EINVAL;
628 		goto out_free;
629 	}
630 
631 	if (num_act_qp != ha->tgt.num_act_qpairs) {
632 		ha->tgt.num_act_qpairs = num_act_qp;
633 		qlt_clr_qp_table(vha);
634 	}
635 	rc = count;
636 out_free:
637 	kfree(buf);
638 	return rc;
639 }
640 
641 static const struct file_operations dfs_naqp_ops = {
642 	.open		= qla_dfs_naqp_open,
643 	.read		= seq_read,
644 	.llseek		= seq_lseek,
645 	.release	= single_release,
646 	.write		= qla_dfs_naqp_write,
647 };
648 
649 
650 int
651 qla2x00_dfs_setup(scsi_qla_host_t *vha)
652 {
653 	struct qla_hw_data *ha = vha->hw;
654 
655 	if (!IS_QLA25XX(ha) && !IS_QLA81XX(ha) && !IS_QLA83XX(ha) &&
656 	    !IS_QLA27XX(ha) && !IS_QLA28XX(ha))
657 		goto out;
658 	if (!ha->fce)
659 		goto out;
660 
661 	if (qla2x00_dfs_root)
662 		goto create_dir;
663 
664 	atomic_set(&qla2x00_dfs_root_count, 0);
665 	qla2x00_dfs_root = debugfs_create_dir(QLA2XXX_DRIVER_NAME, NULL);
666 
667 create_dir:
668 	if (ha->dfs_dir)
669 		goto create_nodes;
670 
671 	mutex_init(&ha->fce_mutex);
672 	ha->dfs_dir = debugfs_create_dir(vha->host_str, qla2x00_dfs_root);
673 
674 	atomic_inc(&qla2x00_dfs_root_count);
675 
676 create_nodes:
677 	ha->dfs_fw_resource_cnt = debugfs_create_file("fw_resource_count",
678 	    S_IRUSR, ha->dfs_dir, vha, &qla_dfs_fw_resource_cnt_fops);
679 
680 	ha->dfs_tgt_counters = debugfs_create_file("tgt_counters", S_IRUSR,
681 	    ha->dfs_dir, vha, &qla_dfs_tgt_counters_fops);
682 
683 	ha->tgt.dfs_tgt_port_database = debugfs_create_file("tgt_port_database",
684 	    S_IRUSR,  ha->dfs_dir, vha, &qla2x00_dfs_tgt_port_database_fops);
685 
686 	ha->dfs_fce = debugfs_create_file("fce", S_IRUSR, ha->dfs_dir, vha,
687 	    &dfs_fce_ops);
688 
689 	ha->tgt.dfs_tgt_sess = debugfs_create_file("tgt_sess",
690 		S_IRUSR, ha->dfs_dir, vha, &qla2x00_dfs_tgt_sess_fops);
691 
692 	if (IS_QLA27XX(ha) || IS_QLA83XX(ha) || IS_QLA28XX(ha)) {
693 		ha->tgt.dfs_naqp = debugfs_create_file("naqp",
694 		    0400, ha->dfs_dir, vha, &dfs_naqp_ops);
695 		if (!ha->tgt.dfs_naqp) {
696 			ql_log(ql_log_warn, vha, 0xd011,
697 			       "Unable to create debugFS naqp node.\n");
698 			goto out;
699 		}
700 	}
701 	vha->dfs_rport_root = debugfs_create_dir("rports", ha->dfs_dir);
702 	if (!vha->dfs_rport_root) {
703 		ql_log(ql_log_warn, vha, 0xd012,
704 		       "Unable to create debugFS rports node.\n");
705 		goto out;
706 	}
707 out:
708 	return 0;
709 }
710 
711 int
712 qla2x00_dfs_remove(scsi_qla_host_t *vha)
713 {
714 	struct qla_hw_data *ha = vha->hw;
715 
716 	if (ha->tgt.dfs_naqp) {
717 		debugfs_remove(ha->tgt.dfs_naqp);
718 		ha->tgt.dfs_naqp = NULL;
719 	}
720 
721 	if (ha->tgt.dfs_tgt_sess) {
722 		debugfs_remove(ha->tgt.dfs_tgt_sess);
723 		ha->tgt.dfs_tgt_sess = NULL;
724 	}
725 
726 	if (ha->tgt.dfs_tgt_port_database) {
727 		debugfs_remove(ha->tgt.dfs_tgt_port_database);
728 		ha->tgt.dfs_tgt_port_database = NULL;
729 	}
730 
731 	if (ha->dfs_fw_resource_cnt) {
732 		debugfs_remove(ha->dfs_fw_resource_cnt);
733 		ha->dfs_fw_resource_cnt = NULL;
734 	}
735 
736 	if (ha->dfs_tgt_counters) {
737 		debugfs_remove(ha->dfs_tgt_counters);
738 		ha->dfs_tgt_counters = NULL;
739 	}
740 
741 	if (ha->dfs_fce) {
742 		debugfs_remove(ha->dfs_fce);
743 		ha->dfs_fce = NULL;
744 	}
745 
746 	if (vha->dfs_rport_root) {
747 		debugfs_remove_recursive(vha->dfs_rport_root);
748 		vha->dfs_rport_root = NULL;
749 	}
750 
751 	if (ha->dfs_dir) {
752 		debugfs_remove(ha->dfs_dir);
753 		ha->dfs_dir = NULL;
754 		atomic_dec(&qla2x00_dfs_root_count);
755 	}
756 
757 	if (atomic_read(&qla2x00_dfs_root_count) == 0 &&
758 	    qla2x00_dfs_root) {
759 		debugfs_remove(qla2x00_dfs_root);
760 		qla2x00_dfs_root = NULL;
761 	}
762 
763 	return 0;
764 }
765