1f931551bSRalph Campbell /*
27fac3301SMike Marciniszyn * Copyright (c) 2012 Intel Corporation. All rights reserved.
37fac3301SMike Marciniszyn * Copyright (c) 2006 - 2012 QLogic Corporation. All rights reserved.
4f931551bSRalph Campbell * Copyright (c) 2006 PathScale, Inc. All rights reserved.
5f931551bSRalph Campbell *
6f931551bSRalph Campbell * This software is available to you under a choice of one of two
7f931551bSRalph Campbell * licenses. You may choose to be licensed under the terms of the GNU
8f931551bSRalph Campbell * General Public License (GPL) Version 2, available from the file
9f931551bSRalph Campbell * COPYING in the main directory of this source tree, or the
10f931551bSRalph Campbell * OpenIB.org BSD license below:
11f931551bSRalph Campbell *
12f931551bSRalph Campbell * Redistribution and use in source and binary forms, with or
13f931551bSRalph Campbell * without modification, are permitted provided that the following
14f931551bSRalph Campbell * conditions are met:
15f931551bSRalph Campbell *
16f931551bSRalph Campbell * - Redistributions of source code must retain the above
17f931551bSRalph Campbell * copyright notice, this list of conditions and the following
18f931551bSRalph Campbell * disclaimer.
19f931551bSRalph Campbell *
20f931551bSRalph Campbell * - Redistributions in binary form must reproduce the above
21f931551bSRalph Campbell * copyright notice, this list of conditions and the following
22f931551bSRalph Campbell * disclaimer in the documentation and/or other materials
23f931551bSRalph Campbell * provided with the distribution.
24f931551bSRalph Campbell *
25f931551bSRalph Campbell * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26f931551bSRalph Campbell * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27f931551bSRalph Campbell * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28f931551bSRalph Campbell * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
29f931551bSRalph Campbell * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
30f931551bSRalph Campbell * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
31f931551bSRalph Campbell * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32f931551bSRalph Campbell * SOFTWARE.
33f931551bSRalph Campbell */
34f931551bSRalph Campbell
35f931551bSRalph Campbell #include <linux/fs.h>
367515c22cSDavid Howells #include <linux/fs_context.h>
37f931551bSRalph Campbell #include <linux/mount.h>
38f931551bSRalph Campbell #include <linux/pagemap.h>
39f931551bSRalph Campbell #include <linux/init.h>
40f931551bSRalph Campbell #include <linux/namei.h>
41f931551bSRalph Campbell
42f931551bSRalph Campbell #include "qib.h"
43f931551bSRalph Campbell
44f931551bSRalph Campbell #define QIBFS_MAGIC 0x726a77
45f931551bSRalph Campbell
46f931551bSRalph Campbell static struct super_block *qib_super;
47f931551bSRalph Campbell
48496ad9aaSAl Viro #define private2dd(file) (file_inode(file)->i_private)
49f931551bSRalph Campbell
qibfs_mknod(struct inode * dir,struct dentry * dentry,umode_t mode,const struct file_operations * fops,void * data)50f931551bSRalph Campbell static int qibfs_mknod(struct inode *dir, struct dentry *dentry,
51f9ec8006SAl Viro umode_t mode, const struct file_operations *fops,
52f931551bSRalph Campbell void *data)
53f931551bSRalph Campbell {
54f931551bSRalph Campbell int error;
55f931551bSRalph Campbell struct inode *inode = new_inode(dir->i_sb);
56f931551bSRalph Campbell
57f931551bSRalph Campbell if (!inode) {
58f931551bSRalph Campbell error = -EPERM;
59f931551bSRalph Campbell goto bail;
60f931551bSRalph Campbell }
61f931551bSRalph Campbell
6285fe4025SChristoph Hellwig inode->i_ino = get_next_ino();
63f931551bSRalph Campbell inode->i_mode = mode;
64d03ca582SEric W. Biederman inode->i_uid = GLOBAL_ROOT_UID;
65d03ca582SEric W. Biederman inode->i_gid = GLOBAL_ROOT_GID;
66f931551bSRalph Campbell inode->i_blocks = 0;
6724856a96SJeff Layton inode->i_atime = inode_set_ctime_current(inode);
68f931551bSRalph Campbell inode->i_mtime = inode->i_atime;
69f931551bSRalph Campbell inode->i_private = data;
70f9ec8006SAl Viro if (S_ISDIR(mode)) {
71f931551bSRalph Campbell inode->i_op = &simple_dir_inode_operations;
72f931551bSRalph Campbell inc_nlink(inode);
73f931551bSRalph Campbell inc_nlink(dir);
74f931551bSRalph Campbell }
75f931551bSRalph Campbell
76f931551bSRalph Campbell inode->i_fop = fops;
77f931551bSRalph Campbell
78f931551bSRalph Campbell d_instantiate(dentry, inode);
79f931551bSRalph Campbell error = 0;
80f931551bSRalph Campbell
81f931551bSRalph Campbell bail:
82f931551bSRalph Campbell return error;
83f931551bSRalph Campbell }
84f931551bSRalph Campbell
create_file(const char * name,umode_t mode,struct dentry * parent,struct dentry ** dentry,const struct file_operations * fops,void * data)85f9ec8006SAl Viro static int create_file(const char *name, umode_t mode,
86f931551bSRalph Campbell struct dentry *parent, struct dentry **dentry,
87f931551bSRalph Campbell const struct file_operations *fops, void *data)
88f931551bSRalph Campbell {
89f931551bSRalph Campbell int error;
90f931551bSRalph Campbell
915955102cSAl Viro inode_lock(d_inode(parent));
92f931551bSRalph Campbell *dentry = lookup_one_len(name, parent, strlen(name));
93f931551bSRalph Campbell if (!IS_ERR(*dentry))
9475c3cfa8SDavid Howells error = qibfs_mknod(d_inode(parent), *dentry,
95f931551bSRalph Campbell mode, fops, data);
96f931551bSRalph Campbell else
97f931551bSRalph Campbell error = PTR_ERR(*dentry);
985955102cSAl Viro inode_unlock(d_inode(parent));
99f931551bSRalph Campbell
100f931551bSRalph Campbell return error;
101f931551bSRalph Campbell }
102f931551bSRalph Campbell
driver_stats_read(struct file * file,char __user * buf,size_t count,loff_t * ppos)103f931551bSRalph Campbell static ssize_t driver_stats_read(struct file *file, char __user *buf,
104f931551bSRalph Campbell size_t count, loff_t *ppos)
105f931551bSRalph Campbell {
1061ed88dd7SMike Marciniszyn qib_stats.sps_ints = qib_sps_ints();
107f931551bSRalph Campbell return simple_read_from_buffer(buf, count, ppos, &qib_stats,
108041af0bbSMike Marciniszyn sizeof(qib_stats));
109f931551bSRalph Campbell }
110f931551bSRalph Campbell
111f931551bSRalph Campbell /*
112f931551bSRalph Campbell * driver stats field names, one line per stat, single string. Used by
113f931551bSRalph Campbell * programs like ipathstats to print the stats in a way which works for
114f931551bSRalph Campbell * different versions of drivers, without changing program source.
115f931551bSRalph Campbell * if qlogic_ib_stats changes, this needs to change. Names need to be
116f931551bSRalph Campbell * 12 chars or less (w/o newline), for proper display by ipathstats utility.
117f931551bSRalph Campbell */
118f931551bSRalph Campbell static const char qib_statnames[] =
119f931551bSRalph Campbell "KernIntr\n"
120f931551bSRalph Campbell "ErrorIntr\n"
121f931551bSRalph Campbell "Tx_Errs\n"
122f931551bSRalph Campbell "Rcv_Errs\n"
123f931551bSRalph Campbell "H/W_Errs\n"
124f931551bSRalph Campbell "NoPIOBufs\n"
125f931551bSRalph Campbell "CtxtsOpen\n"
126f931551bSRalph Campbell "RcvLen_Errs\n"
127f931551bSRalph Campbell "EgrBufFull\n"
128f931551bSRalph Campbell "EgrHdrFull\n"
129f931551bSRalph Campbell ;
130f931551bSRalph Campbell
driver_names_read(struct file * file,char __user * buf,size_t count,loff_t * ppos)131f931551bSRalph Campbell static ssize_t driver_names_read(struct file *file, char __user *buf,
132f931551bSRalph Campbell size_t count, loff_t *ppos)
133f931551bSRalph Campbell {
134f931551bSRalph Campbell return simple_read_from_buffer(buf, count, ppos, qib_statnames,
135041af0bbSMike Marciniszyn sizeof(qib_statnames) - 1); /* no null */
136f931551bSRalph Campbell }
137f931551bSRalph Campbell
138f931551bSRalph Campbell static const struct file_operations driver_ops[] = {
139dd378c21SArnd Bergmann { .read = driver_stats_read, .llseek = generic_file_llseek, },
140dd378c21SArnd Bergmann { .read = driver_names_read, .llseek = generic_file_llseek, },
141f931551bSRalph Campbell };
142f931551bSRalph Campbell
143f931551bSRalph Campbell /* read the per-device counters */
dev_counters_read(struct file * file,char __user * buf,size_t count,loff_t * ppos)144f931551bSRalph Campbell static ssize_t dev_counters_read(struct file *file, char __user *buf,
145f931551bSRalph Campbell size_t count, loff_t *ppos)
146f931551bSRalph Campbell {
147f931551bSRalph Campbell u64 *counters;
148f27ec1d6SRoland Dreier size_t avail;
149f931551bSRalph Campbell struct qib_devdata *dd = private2dd(file);
150f931551bSRalph Campbell
151f27ec1d6SRoland Dreier avail = dd->f_read_cntrs(dd, *ppos, NULL, &counters);
152f27ec1d6SRoland Dreier return simple_read_from_buffer(buf, count, ppos, counters, avail);
153f931551bSRalph Campbell }
154f931551bSRalph Campbell
155f931551bSRalph Campbell /* read the per-device counters */
dev_names_read(struct file * file,char __user * buf,size_t count,loff_t * ppos)156f931551bSRalph Campbell static ssize_t dev_names_read(struct file *file, char __user *buf,
157f931551bSRalph Campbell size_t count, loff_t *ppos)
158f931551bSRalph Campbell {
159f931551bSRalph Campbell char *names;
160f27ec1d6SRoland Dreier size_t avail;
161f931551bSRalph Campbell struct qib_devdata *dd = private2dd(file);
162f931551bSRalph Campbell
163f27ec1d6SRoland Dreier avail = dd->f_read_cntrs(dd, *ppos, &names, NULL);
164f27ec1d6SRoland Dreier return simple_read_from_buffer(buf, count, ppos, names, avail);
165f931551bSRalph Campbell }
166f931551bSRalph Campbell
167f931551bSRalph Campbell static const struct file_operations cntr_ops[] = {
168dd378c21SArnd Bergmann { .read = dev_counters_read, .llseek = generic_file_llseek, },
169dd378c21SArnd Bergmann { .read = dev_names_read, .llseek = generic_file_llseek, },
170f931551bSRalph Campbell };
171f931551bSRalph Campbell
172f931551bSRalph Campbell /*
173496ad9aaSAl Viro * Could use file_inode(file)->i_ino to figure out which file,
174f931551bSRalph Campbell * instead of separate routine for each, but for now, this works...
175f931551bSRalph Campbell */
176f931551bSRalph Campbell
177f931551bSRalph Campbell /* read the per-port names (same for each port) */
portnames_read(struct file * file,char __user * buf,size_t count,loff_t * ppos)178f931551bSRalph Campbell static ssize_t portnames_read(struct file *file, char __user *buf,
179f931551bSRalph Campbell size_t count, loff_t *ppos)
180f931551bSRalph Campbell {
181f931551bSRalph Campbell char *names;
182f27ec1d6SRoland Dreier size_t avail;
183f931551bSRalph Campbell struct qib_devdata *dd = private2dd(file);
184f931551bSRalph Campbell
185f27ec1d6SRoland Dreier avail = dd->f_read_portcntrs(dd, *ppos, 0, &names, NULL);
186f27ec1d6SRoland Dreier return simple_read_from_buffer(buf, count, ppos, names, avail);
187f931551bSRalph Campbell }
188f931551bSRalph Campbell
189f931551bSRalph Campbell /* read the per-port counters for port 1 (pidx 0) */
portcntrs_1_read(struct file * file,char __user * buf,size_t count,loff_t * ppos)190f931551bSRalph Campbell static ssize_t portcntrs_1_read(struct file *file, char __user *buf,
191f931551bSRalph Campbell size_t count, loff_t *ppos)
192f931551bSRalph Campbell {
193f931551bSRalph Campbell u64 *counters;
194f27ec1d6SRoland Dreier size_t avail;
195f931551bSRalph Campbell struct qib_devdata *dd = private2dd(file);
196f931551bSRalph Campbell
197f27ec1d6SRoland Dreier avail = dd->f_read_portcntrs(dd, *ppos, 0, NULL, &counters);
198f27ec1d6SRoland Dreier return simple_read_from_buffer(buf, count, ppos, counters, avail);
199f931551bSRalph Campbell }
200f931551bSRalph Campbell
201f931551bSRalph Campbell /* read the per-port counters for port 2 (pidx 1) */
portcntrs_2_read(struct file * file,char __user * buf,size_t count,loff_t * ppos)202f931551bSRalph Campbell static ssize_t portcntrs_2_read(struct file *file, char __user *buf,
203f931551bSRalph Campbell size_t count, loff_t *ppos)
204f931551bSRalph Campbell {
205f931551bSRalph Campbell u64 *counters;
206f27ec1d6SRoland Dreier size_t avail;
207f931551bSRalph Campbell struct qib_devdata *dd = private2dd(file);
208f931551bSRalph Campbell
209f27ec1d6SRoland Dreier avail = dd->f_read_portcntrs(dd, *ppos, 1, NULL, &counters);
210f27ec1d6SRoland Dreier return simple_read_from_buffer(buf, count, ppos, counters, avail);
211f931551bSRalph Campbell }
212f931551bSRalph Campbell
213f931551bSRalph Campbell static const struct file_operations portcntr_ops[] = {
214dd378c21SArnd Bergmann { .read = portnames_read, .llseek = generic_file_llseek, },
215dd378c21SArnd Bergmann { .read = portcntrs_1_read, .llseek = generic_file_llseek, },
216dd378c21SArnd Bergmann { .read = portcntrs_2_read, .llseek = generic_file_llseek, },
217f931551bSRalph Campbell };
218f931551bSRalph Campbell
219f931551bSRalph Campbell /*
220f931551bSRalph Campbell * read the per-port QSFP data for port 1 (pidx 0)
221f931551bSRalph Campbell */
qsfp_1_read(struct file * file,char __user * buf,size_t count,loff_t * ppos)222f931551bSRalph Campbell static ssize_t qsfp_1_read(struct file *file, char __user *buf,
223f931551bSRalph Campbell size_t count, loff_t *ppos)
224f931551bSRalph Campbell {
225f931551bSRalph Campbell struct qib_devdata *dd = private2dd(file);
226f931551bSRalph Campbell char *tmp;
227f931551bSRalph Campbell int ret;
228f931551bSRalph Campbell
229f931551bSRalph Campbell tmp = kmalloc(PAGE_SIZE, GFP_KERNEL);
230f931551bSRalph Campbell if (!tmp)
231f931551bSRalph Campbell return -ENOMEM;
232f931551bSRalph Campbell
233f931551bSRalph Campbell ret = qib_qsfp_dump(dd->pport, tmp, PAGE_SIZE);
234f931551bSRalph Campbell if (ret > 0)
235f931551bSRalph Campbell ret = simple_read_from_buffer(buf, count, ppos, tmp, ret);
236f931551bSRalph Campbell kfree(tmp);
237f931551bSRalph Campbell return ret;
238f931551bSRalph Campbell }
239f931551bSRalph Campbell
240f931551bSRalph Campbell /*
241f931551bSRalph Campbell * read the per-port QSFP data for port 2 (pidx 1)
242f931551bSRalph Campbell */
qsfp_2_read(struct file * file,char __user * buf,size_t count,loff_t * ppos)243f931551bSRalph Campbell static ssize_t qsfp_2_read(struct file *file, char __user *buf,
244f931551bSRalph Campbell size_t count, loff_t *ppos)
245f931551bSRalph Campbell {
246f931551bSRalph Campbell struct qib_devdata *dd = private2dd(file);
247f931551bSRalph Campbell char *tmp;
248f931551bSRalph Campbell int ret;
249f931551bSRalph Campbell
250f931551bSRalph Campbell if (dd->num_pports < 2)
251f931551bSRalph Campbell return -ENODEV;
252f931551bSRalph Campbell
253f931551bSRalph Campbell tmp = kmalloc(PAGE_SIZE, GFP_KERNEL);
254f931551bSRalph Campbell if (!tmp)
255f931551bSRalph Campbell return -ENOMEM;
256f931551bSRalph Campbell
257f931551bSRalph Campbell ret = qib_qsfp_dump(dd->pport + 1, tmp, PAGE_SIZE);
258f931551bSRalph Campbell if (ret > 0)
259f931551bSRalph Campbell ret = simple_read_from_buffer(buf, count, ppos, tmp, ret);
260f931551bSRalph Campbell kfree(tmp);
261f931551bSRalph Campbell return ret;
262f931551bSRalph Campbell }
263f931551bSRalph Campbell
264f931551bSRalph Campbell static const struct file_operations qsfp_ops[] = {
265dd378c21SArnd Bergmann { .read = qsfp_1_read, .llseek = generic_file_llseek, },
266dd378c21SArnd Bergmann { .read = qsfp_2_read, .llseek = generic_file_llseek, },
267f931551bSRalph Campbell };
268f931551bSRalph Campbell
flash_read(struct file * file,char __user * buf,size_t count,loff_t * ppos)269f931551bSRalph Campbell static ssize_t flash_read(struct file *file, char __user *buf,
270f931551bSRalph Campbell size_t count, loff_t *ppos)
271f931551bSRalph Campbell {
272f931551bSRalph Campbell struct qib_devdata *dd;
273f931551bSRalph Campbell ssize_t ret;
274f931551bSRalph Campbell loff_t pos;
275f931551bSRalph Campbell char *tmp;
276f931551bSRalph Campbell
277f931551bSRalph Campbell pos = *ppos;
278f931551bSRalph Campbell
279f931551bSRalph Campbell if (pos < 0) {
280f931551bSRalph Campbell ret = -EINVAL;
281f931551bSRalph Campbell goto bail;
282f931551bSRalph Campbell }
283f931551bSRalph Campbell
284f931551bSRalph Campbell if (pos >= sizeof(struct qib_flash)) {
285f931551bSRalph Campbell ret = 0;
286f931551bSRalph Campbell goto bail;
287f931551bSRalph Campbell }
288f931551bSRalph Campbell
289f931551bSRalph Campbell if (count > sizeof(struct qib_flash) - pos)
290f931551bSRalph Campbell count = sizeof(struct qib_flash) - pos;
291f931551bSRalph Campbell
292f931551bSRalph Campbell tmp = kmalloc(count, GFP_KERNEL);
293f931551bSRalph Campbell if (!tmp) {
294f931551bSRalph Campbell ret = -ENOMEM;
295f931551bSRalph Campbell goto bail;
296f931551bSRalph Campbell }
297f931551bSRalph Campbell
298f931551bSRalph Campbell dd = private2dd(file);
299f931551bSRalph Campbell if (qib_eeprom_read(dd, pos, tmp, count)) {
300f931551bSRalph Campbell qib_dev_err(dd, "failed to read from flash\n");
301f931551bSRalph Campbell ret = -ENXIO;
302f931551bSRalph Campbell goto bail_tmp;
303f931551bSRalph Campbell }
304f931551bSRalph Campbell
305f931551bSRalph Campbell if (copy_to_user(buf, tmp, count)) {
306f931551bSRalph Campbell ret = -EFAULT;
307f931551bSRalph Campbell goto bail_tmp;
308f931551bSRalph Campbell }
309f931551bSRalph Campbell
310f931551bSRalph Campbell *ppos = pos + count;
311f931551bSRalph Campbell ret = count;
312f931551bSRalph Campbell
313f931551bSRalph Campbell bail_tmp:
314f931551bSRalph Campbell kfree(tmp);
315f931551bSRalph Campbell
316f931551bSRalph Campbell bail:
317f931551bSRalph Campbell return ret;
318f931551bSRalph Campbell }
319f931551bSRalph Campbell
flash_write(struct file * file,const char __user * buf,size_t count,loff_t * ppos)320f931551bSRalph Campbell static ssize_t flash_write(struct file *file, const char __user *buf,
321f931551bSRalph Campbell size_t count, loff_t *ppos)
322f931551bSRalph Campbell {
323f931551bSRalph Campbell struct qib_devdata *dd;
324f931551bSRalph Campbell ssize_t ret;
325f931551bSRalph Campbell loff_t pos;
326f931551bSRalph Campbell char *tmp;
327f931551bSRalph Campbell
328f931551bSRalph Campbell pos = *ppos;
329f931551bSRalph Campbell
33048ef5865SMarkus Elfring if (pos != 0 || count != sizeof(struct qib_flash))
33148ef5865SMarkus Elfring return -EINVAL;
332f931551bSRalph Campbell
33348ef5865SMarkus Elfring tmp = memdup_user(buf, count);
33448ef5865SMarkus Elfring if (IS_ERR(tmp))
33548ef5865SMarkus Elfring return PTR_ERR(tmp);
336f931551bSRalph Campbell
337f931551bSRalph Campbell dd = private2dd(file);
338f931551bSRalph Campbell if (qib_eeprom_write(dd, pos, tmp, count)) {
339f931551bSRalph Campbell ret = -ENXIO;
340f931551bSRalph Campbell qib_dev_err(dd, "failed to write to flash\n");
341f931551bSRalph Campbell goto bail_tmp;
342f931551bSRalph Campbell }
343f931551bSRalph Campbell
344f931551bSRalph Campbell *ppos = pos + count;
345f931551bSRalph Campbell ret = count;
346f931551bSRalph Campbell
347f931551bSRalph Campbell bail_tmp:
348f931551bSRalph Campbell kfree(tmp);
349f931551bSRalph Campbell return ret;
350f931551bSRalph Campbell }
351f931551bSRalph Campbell
352f931551bSRalph Campbell static const struct file_operations flash_ops = {
353f931551bSRalph Campbell .read = flash_read,
354f931551bSRalph Campbell .write = flash_write,
3556038f373SArnd Bergmann .llseek = default_llseek,
356f931551bSRalph Campbell };
357f931551bSRalph Campbell
add_cntr_files(struct super_block * sb,struct qib_devdata * dd)358f931551bSRalph Campbell static int add_cntr_files(struct super_block *sb, struct qib_devdata *dd)
359f931551bSRalph Campbell {
360f931551bSRalph Campbell struct dentry *dir, *tmp;
361f931551bSRalph Campbell char unit[10];
362f931551bSRalph Campbell int ret, i;
363f931551bSRalph Campbell
364f931551bSRalph Campbell /* create the per-unit directory */
365041af0bbSMike Marciniszyn snprintf(unit, sizeof(unit), "%u", dd->unit);
366f931551bSRalph Campbell ret = create_file(unit, S_IFDIR|S_IRUGO|S_IXUGO, sb->s_root, &dir,
367f931551bSRalph Campbell &simple_dir_operations, dd);
368f931551bSRalph Campbell if (ret) {
3697fac3301SMike Marciniszyn pr_err("create_file(%s) failed: %d\n", unit, ret);
370f931551bSRalph Campbell goto bail;
371f931551bSRalph Campbell }
372f931551bSRalph Campbell
373f931551bSRalph Campbell /* create the files in the new directory */
374f931551bSRalph Campbell ret = create_file("counters", S_IFREG|S_IRUGO, dir, &tmp,
375f931551bSRalph Campbell &cntr_ops[0], dd);
376f931551bSRalph Campbell if (ret) {
3777fac3301SMike Marciniszyn pr_err("create_file(%s/counters) failed: %d\n",
378f931551bSRalph Campbell unit, ret);
379f931551bSRalph Campbell goto bail;
380f931551bSRalph Campbell }
381f931551bSRalph Campbell ret = create_file("counter_names", S_IFREG|S_IRUGO, dir, &tmp,
382f931551bSRalph Campbell &cntr_ops[1], dd);
383f931551bSRalph Campbell if (ret) {
3847fac3301SMike Marciniszyn pr_err("create_file(%s/counter_names) failed: %d\n",
385f931551bSRalph Campbell unit, ret);
386f931551bSRalph Campbell goto bail;
387f931551bSRalph Campbell }
388f931551bSRalph Campbell ret = create_file("portcounter_names", S_IFREG|S_IRUGO, dir, &tmp,
389f931551bSRalph Campbell &portcntr_ops[0], dd);
390f931551bSRalph Campbell if (ret) {
3917fac3301SMike Marciniszyn pr_err("create_file(%s/%s) failed: %d\n",
392f931551bSRalph Campbell unit, "portcounter_names", ret);
393f931551bSRalph Campbell goto bail;
394f931551bSRalph Campbell }
395f931551bSRalph Campbell for (i = 1; i <= dd->num_pports; i++) {
396f931551bSRalph Campbell char fname[24];
397f931551bSRalph Campbell
398f931551bSRalph Campbell sprintf(fname, "port%dcounters", i);
399f931551bSRalph Campbell /* create the files in the new directory */
400f931551bSRalph Campbell ret = create_file(fname, S_IFREG|S_IRUGO, dir, &tmp,
401f931551bSRalph Campbell &portcntr_ops[i], dd);
402f931551bSRalph Campbell if (ret) {
4037fac3301SMike Marciniszyn pr_err("create_file(%s/%s) failed: %d\n",
404f931551bSRalph Campbell unit, fname, ret);
405f931551bSRalph Campbell goto bail;
406f931551bSRalph Campbell }
407f931551bSRalph Campbell if (!(dd->flags & QIB_HAS_QSFP))
408f931551bSRalph Campbell continue;
409f931551bSRalph Campbell sprintf(fname, "qsfp%d", i);
410f931551bSRalph Campbell ret = create_file(fname, S_IFREG|S_IRUGO, dir, &tmp,
411f931551bSRalph Campbell &qsfp_ops[i - 1], dd);
412f931551bSRalph Campbell if (ret) {
4137fac3301SMike Marciniszyn pr_err("create_file(%s/%s) failed: %d\n",
414f931551bSRalph Campbell unit, fname, ret);
415f931551bSRalph Campbell goto bail;
416f931551bSRalph Campbell }
417f931551bSRalph Campbell }
418f931551bSRalph Campbell
419f931551bSRalph Campbell ret = create_file("flash", S_IFREG|S_IWUSR|S_IRUGO, dir, &tmp,
420f931551bSRalph Campbell &flash_ops, dd);
421f931551bSRalph Campbell if (ret)
4227fac3301SMike Marciniszyn pr_err("create_file(%s/flash) failed: %d\n",
423f931551bSRalph Campbell unit, ret);
424f931551bSRalph Campbell bail:
425f931551bSRalph Campbell return ret;
426f931551bSRalph Campbell }
427f931551bSRalph Campbell
remove_device_files(struct super_block * sb,struct qib_devdata * dd)428f931551bSRalph Campbell static int remove_device_files(struct super_block *sb,
429f931551bSRalph Campbell struct qib_devdata *dd)
430f931551bSRalph Campbell {
431e41d2378SAl Viro struct dentry *dir;
432f931551bSRalph Campbell char unit[10];
433f931551bSRalph Campbell
434041af0bbSMike Marciniszyn snprintf(unit, sizeof(unit), "%u", dd->unit);
435e41d2378SAl Viro dir = lookup_one_len_unlocked(unit, sb->s_root, strlen(unit));
436f931551bSRalph Campbell
437f931551bSRalph Campbell if (IS_ERR(dir)) {
4387fac3301SMike Marciniszyn pr_err("Lookup of %s failed\n", unit);
439e41d2378SAl Viro return PTR_ERR(dir);
440f931551bSRalph Campbell }
441e41d2378SAl Viro simple_recursive_removal(dir, NULL);
442*db71ca93SAl Viro dput(dir);
443e41d2378SAl Viro return 0;
444f931551bSRalph Campbell }
445f931551bSRalph Campbell
446f931551bSRalph Campbell /*
447f931551bSRalph Campbell * This fills everything in when the fs is mounted, to handle umount/mount
448f931551bSRalph Campbell * after device init. The direct add_cntr_files() call handles adding
449f931551bSRalph Campbell * them from the init code, when the fs is already mounted.
450f931551bSRalph Campbell */
qibfs_fill_super(struct super_block * sb,struct fs_context * fc)4517515c22cSDavid Howells static int qibfs_fill_super(struct super_block *sb, struct fs_context *fc)
452f931551bSRalph Campbell {
453059d48fbSMatthew Wilcox struct qib_devdata *dd;
454059d48fbSMatthew Wilcox unsigned long index;
455f931551bSRalph Campbell int ret;
456f931551bSRalph Campbell
457cda37124SEric Biggers static const struct tree_descr files[] = {
458f931551bSRalph Campbell [2] = {"driver_stats", &driver_ops[0], S_IRUGO},
459f931551bSRalph Campbell [3] = {"driver_stats_names", &driver_ops[1], S_IRUGO},
460f931551bSRalph Campbell {""},
461f931551bSRalph Campbell };
462f931551bSRalph Campbell
463f931551bSRalph Campbell ret = simple_fill_super(sb, QIBFS_MAGIC, files);
464f931551bSRalph Campbell if (ret) {
4657fac3301SMike Marciniszyn pr_err("simple_fill_super failed: %d\n", ret);
466f931551bSRalph Campbell goto bail;
467f931551bSRalph Campbell }
468f931551bSRalph Campbell
469059d48fbSMatthew Wilcox xa_for_each(&qib_dev_table, index, dd) {
470f931551bSRalph Campbell ret = add_cntr_files(sb, dd);
471971b2e8aSAl Viro if (ret)
472f931551bSRalph Campbell goto bail;
473f931551bSRalph Campbell }
474f931551bSRalph Campbell
475f931551bSRalph Campbell bail:
476f931551bSRalph Campbell return ret;
477f931551bSRalph Campbell }
478f931551bSRalph Campbell
qibfs_get_tree(struct fs_context * fc)4797515c22cSDavid Howells static int qibfs_get_tree(struct fs_context *fc)
480f931551bSRalph Campbell {
4817515c22cSDavid Howells int ret = get_tree_single(fc, qibfs_fill_super);
4827515c22cSDavid Howells if (ret == 0)
4837515c22cSDavid Howells qib_super = fc->root->d_sb;
484f931551bSRalph Campbell return ret;
485f931551bSRalph Campbell }
486f931551bSRalph Campbell
4877515c22cSDavid Howells static const struct fs_context_operations qibfs_context_ops = {
4887515c22cSDavid Howells .get_tree = qibfs_get_tree,
4897515c22cSDavid Howells };
4907515c22cSDavid Howells
qibfs_init_fs_context(struct fs_context * fc)4917515c22cSDavid Howells static int qibfs_init_fs_context(struct fs_context *fc)
4927515c22cSDavid Howells {
4937515c22cSDavid Howells fc->ops = &qibfs_context_ops;
4947515c22cSDavid Howells return 0;
4957515c22cSDavid Howells }
4967515c22cSDavid Howells
qibfs_kill_super(struct super_block * s)497f931551bSRalph Campbell static void qibfs_kill_super(struct super_block *s)
498f931551bSRalph Campbell {
499f931551bSRalph Campbell kill_litter_super(s);
500f931551bSRalph Campbell qib_super = NULL;
501f931551bSRalph Campbell }
502f931551bSRalph Campbell
qibfs_add(struct qib_devdata * dd)503f931551bSRalph Campbell int qibfs_add(struct qib_devdata *dd)
504f931551bSRalph Campbell {
505f931551bSRalph Campbell int ret;
506f931551bSRalph Campbell
507f931551bSRalph Campbell /*
508f931551bSRalph Campbell * On first unit initialized, qib_super will not yet exist
509f931551bSRalph Campbell * because nobody has yet tried to mount the filesystem, so
510f931551bSRalph Campbell * we can't consider that to be an error; if an error occurs
511f931551bSRalph Campbell * during the mount, that will get a complaint, so this is OK.
512f931551bSRalph Campbell * add_cntr_files() for all units is done at mount from
513f931551bSRalph Campbell * qibfs_fill_super(), so one way or another, everything works.
514f931551bSRalph Campbell */
515f931551bSRalph Campbell if (qib_super == NULL)
516f931551bSRalph Campbell ret = 0;
517f931551bSRalph Campbell else
518f931551bSRalph Campbell ret = add_cntr_files(qib_super, dd);
519f931551bSRalph Campbell return ret;
520f931551bSRalph Campbell }
521f931551bSRalph Campbell
qibfs_remove(struct qib_devdata * dd)522f931551bSRalph Campbell int qibfs_remove(struct qib_devdata *dd)
523f931551bSRalph Campbell {
524f931551bSRalph Campbell int ret = 0;
525f931551bSRalph Campbell
526f931551bSRalph Campbell if (qib_super)
527f931551bSRalph Campbell ret = remove_device_files(qib_super, dd);
528f931551bSRalph Campbell
529f931551bSRalph Campbell return ret;
530f931551bSRalph Campbell }
531f931551bSRalph Campbell
532f931551bSRalph Campbell static struct file_system_type qibfs_fs_type = {
533f931551bSRalph Campbell .owner = THIS_MODULE,
534f931551bSRalph Campbell .name = "ipathfs",
5357515c22cSDavid Howells .init_fs_context = qibfs_init_fs_context,
536f931551bSRalph Campbell .kill_sb = qibfs_kill_super,
537f931551bSRalph Campbell };
5387f78e035SEric W. Biederman MODULE_ALIAS_FS("ipathfs");
539f931551bSRalph Campbell
qib_init_qibfs(void)540f931551bSRalph Campbell int __init qib_init_qibfs(void)
541f931551bSRalph Campbell {
542f931551bSRalph Campbell return register_filesystem(&qibfs_fs_type);
543f931551bSRalph Campbell }
544f931551bSRalph Campbell
qib_exit_qibfs(void)545f931551bSRalph Campbell int __exit qib_exit_qibfs(void)
546f931551bSRalph Campbell {
547f931551bSRalph Campbell return unregister_filesystem(&qibfs_fs_type);
548f931551bSRalph Campbell }
549