1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35#include <linux/fs.h>
36#include <linux/fs_context.h>
37#include <linux/mount.h>
38#include <linux/pagemap.h>
39#include <linux/init.h>
40#include <linux/namei.h>
41
42#include "qib.h"
43
44#define QIBFS_MAGIC 0x726a77
45
46static struct super_block *qib_super;
47
48#define private2dd(file) (file_inode(file)->i_private)
49
50static int qibfs_mknod(struct inode *dir, struct dentry *dentry,
51 umode_t mode, const struct file_operations *fops,
52 void *data)
53{
54 int error;
55 struct inode *inode = new_inode(dir->i_sb);
56
57 if (!inode) {
58 error = -EPERM;
59 goto bail;
60 }
61
62 inode->i_ino = get_next_ino();
63 inode->i_mode = mode;
64 inode->i_uid = GLOBAL_ROOT_UID;
65 inode->i_gid = GLOBAL_ROOT_GID;
66 inode->i_blocks = 0;
67 inode->i_atime = current_time(inode);
68 inode->i_mtime = inode->i_atime;
69 inode->i_ctime = inode->i_atime;
70 inode->i_private = data;
71 if (S_ISDIR(mode)) {
72 inode->i_op = &simple_dir_inode_operations;
73 inc_nlink(inode);
74 inc_nlink(dir);
75 }
76
77 inode->i_fop = fops;
78
79 d_instantiate(dentry, inode);
80 error = 0;
81
82bail:
83 return error;
84}
85
86static int create_file(const char *name, umode_t mode,
87 struct dentry *parent, struct dentry **dentry,
88 const struct file_operations *fops, void *data)
89{
90 int error;
91
92 inode_lock(d_inode(parent));
93 *dentry = lookup_one_len(name, parent, strlen(name));
94 if (!IS_ERR(*dentry))
95 error = qibfs_mknod(d_inode(parent), *dentry,
96 mode, fops, data);
97 else
98 error = PTR_ERR(*dentry);
99 inode_unlock(d_inode(parent));
100
101 return error;
102}
103
104static ssize_t driver_stats_read(struct file *file, char __user *buf,
105 size_t count, loff_t *ppos)
106{
107 qib_stats.sps_ints = qib_sps_ints();
108 return simple_read_from_buffer(buf, count, ppos, &qib_stats,
109 sizeof(qib_stats));
110}
111
112
113
114
115
116
117
118
119static const char qib_statnames[] =
120 "KernIntr\n"
121 "ErrorIntr\n"
122 "Tx_Errs\n"
123 "Rcv_Errs\n"
124 "H/W_Errs\n"
125 "NoPIOBufs\n"
126 "CtxtsOpen\n"
127 "RcvLen_Errs\n"
128 "EgrBufFull\n"
129 "EgrHdrFull\n"
130 ;
131
132static ssize_t driver_names_read(struct file *file, char __user *buf,
133 size_t count, loff_t *ppos)
134{
135 return simple_read_from_buffer(buf, count, ppos, qib_statnames,
136 sizeof(qib_statnames) - 1);
137}
138
139static const struct file_operations driver_ops[] = {
140 { .read = driver_stats_read, .llseek = generic_file_llseek, },
141 { .read = driver_names_read, .llseek = generic_file_llseek, },
142};
143
144
145static ssize_t dev_counters_read(struct file *file, char __user *buf,
146 size_t count, loff_t *ppos)
147{
148 u64 *counters;
149 size_t avail;
150 struct qib_devdata *dd = private2dd(file);
151
152 avail = dd->f_read_cntrs(dd, *ppos, NULL, &counters);
153 return simple_read_from_buffer(buf, count, ppos, counters, avail);
154}
155
156
157static ssize_t dev_names_read(struct file *file, char __user *buf,
158 size_t count, loff_t *ppos)
159{
160 char *names;
161 size_t avail;
162 struct qib_devdata *dd = private2dd(file);
163
164 avail = dd->f_read_cntrs(dd, *ppos, &names, NULL);
165 return simple_read_from_buffer(buf, count, ppos, names, avail);
166}
167
168static const struct file_operations cntr_ops[] = {
169 { .read = dev_counters_read, .llseek = generic_file_llseek, },
170 { .read = dev_names_read, .llseek = generic_file_llseek, },
171};
172
173
174
175
176
177
178
179static ssize_t portnames_read(struct file *file, char __user *buf,
180 size_t count, loff_t *ppos)
181{
182 char *names;
183 size_t avail;
184 struct qib_devdata *dd = private2dd(file);
185
186 avail = dd->f_read_portcntrs(dd, *ppos, 0, &names, NULL);
187 return simple_read_from_buffer(buf, count, ppos, names, avail);
188}
189
190
191static ssize_t portcntrs_1_read(struct file *file, char __user *buf,
192 size_t count, loff_t *ppos)
193{
194 u64 *counters;
195 size_t avail;
196 struct qib_devdata *dd = private2dd(file);
197
198 avail = dd->f_read_portcntrs(dd, *ppos, 0, NULL, &counters);
199 return simple_read_from_buffer(buf, count, ppos, counters, avail);
200}
201
202
203static ssize_t portcntrs_2_read(struct file *file, char __user *buf,
204 size_t count, loff_t *ppos)
205{
206 u64 *counters;
207 size_t avail;
208 struct qib_devdata *dd = private2dd(file);
209
210 avail = dd->f_read_portcntrs(dd, *ppos, 1, NULL, &counters);
211 return simple_read_from_buffer(buf, count, ppos, counters, avail);
212}
213
214static const struct file_operations portcntr_ops[] = {
215 { .read = portnames_read, .llseek = generic_file_llseek, },
216 { .read = portcntrs_1_read, .llseek = generic_file_llseek, },
217 { .read = portcntrs_2_read, .llseek = generic_file_llseek, },
218};
219
220
221
222
223static ssize_t qsfp_1_read(struct file *file, char __user *buf,
224 size_t count, loff_t *ppos)
225{
226 struct qib_devdata *dd = private2dd(file);
227 char *tmp;
228 int ret;
229
230 tmp = kmalloc(PAGE_SIZE, GFP_KERNEL);
231 if (!tmp)
232 return -ENOMEM;
233
234 ret = qib_qsfp_dump(dd->pport, tmp, PAGE_SIZE);
235 if (ret > 0)
236 ret = simple_read_from_buffer(buf, count, ppos, tmp, ret);
237 kfree(tmp);
238 return ret;
239}
240
241
242
243
244static ssize_t qsfp_2_read(struct file *file, char __user *buf,
245 size_t count, loff_t *ppos)
246{
247 struct qib_devdata *dd = private2dd(file);
248 char *tmp;
249 int ret;
250
251 if (dd->num_pports < 2)
252 return -ENODEV;
253
254 tmp = kmalloc(PAGE_SIZE, GFP_KERNEL);
255 if (!tmp)
256 return -ENOMEM;
257
258 ret = qib_qsfp_dump(dd->pport + 1, tmp, PAGE_SIZE);
259 if (ret > 0)
260 ret = simple_read_from_buffer(buf, count, ppos, tmp, ret);
261 kfree(tmp);
262 return ret;
263}
264
265static const struct file_operations qsfp_ops[] = {
266 { .read = qsfp_1_read, .llseek = generic_file_llseek, },
267 { .read = qsfp_2_read, .llseek = generic_file_llseek, },
268};
269
270static ssize_t flash_read(struct file *file, char __user *buf,
271 size_t count, loff_t *ppos)
272{
273 struct qib_devdata *dd;
274 ssize_t ret;
275 loff_t pos;
276 char *tmp;
277
278 pos = *ppos;
279
280 if (pos < 0) {
281 ret = -EINVAL;
282 goto bail;
283 }
284
285 if (pos >= sizeof(struct qib_flash)) {
286 ret = 0;
287 goto bail;
288 }
289
290 if (count > sizeof(struct qib_flash) - pos)
291 count = sizeof(struct qib_flash) - pos;
292
293 tmp = kmalloc(count, GFP_KERNEL);
294 if (!tmp) {
295 ret = -ENOMEM;
296 goto bail;
297 }
298
299 dd = private2dd(file);
300 if (qib_eeprom_read(dd, pos, tmp, count)) {
301 qib_dev_err(dd, "failed to read from flash\n");
302 ret = -ENXIO;
303 goto bail_tmp;
304 }
305
306 if (copy_to_user(buf, tmp, count)) {
307 ret = -EFAULT;
308 goto bail_tmp;
309 }
310
311 *ppos = pos + count;
312 ret = count;
313
314bail_tmp:
315 kfree(tmp);
316
317bail:
318 return ret;
319}
320
321static ssize_t flash_write(struct file *file, const char __user *buf,
322 size_t count, loff_t *ppos)
323{
324 struct qib_devdata *dd;
325 ssize_t ret;
326 loff_t pos;
327 char *tmp;
328
329 pos = *ppos;
330
331 if (pos != 0 || count != sizeof(struct qib_flash))
332 return -EINVAL;
333
334 tmp = memdup_user(buf, count);
335 if (IS_ERR(tmp))
336 return PTR_ERR(tmp);
337
338 dd = private2dd(file);
339 if (qib_eeprom_write(dd, pos, tmp, count)) {
340 ret = -ENXIO;
341 qib_dev_err(dd, "failed to write to flash\n");
342 goto bail_tmp;
343 }
344
345 *ppos = pos + count;
346 ret = count;
347
348bail_tmp:
349 kfree(tmp);
350 return ret;
351}
352
353static const struct file_operations flash_ops = {
354 .read = flash_read,
355 .write = flash_write,
356 .llseek = default_llseek,
357};
358
359static int add_cntr_files(struct super_block *sb, struct qib_devdata *dd)
360{
361 struct dentry *dir, *tmp;
362 char unit[10];
363 int ret, i;
364
365
366 snprintf(unit, sizeof(unit), "%u", dd->unit);
367 ret = create_file(unit, S_IFDIR|S_IRUGO|S_IXUGO, sb->s_root, &dir,
368 &simple_dir_operations, dd);
369 if (ret) {
370 pr_err("create_file(%s) failed: %d\n", unit, ret);
371 goto bail;
372 }
373
374
375 ret = create_file("counters", S_IFREG|S_IRUGO, dir, &tmp,
376 &cntr_ops[0], dd);
377 if (ret) {
378 pr_err("create_file(%s/counters) failed: %d\n",
379 unit, ret);
380 goto bail;
381 }
382 ret = create_file("counter_names", S_IFREG|S_IRUGO, dir, &tmp,
383 &cntr_ops[1], dd);
384 if (ret) {
385 pr_err("create_file(%s/counter_names) failed: %d\n",
386 unit, ret);
387 goto bail;
388 }
389 ret = create_file("portcounter_names", S_IFREG|S_IRUGO, dir, &tmp,
390 &portcntr_ops[0], dd);
391 if (ret) {
392 pr_err("create_file(%s/%s) failed: %d\n",
393 unit, "portcounter_names", ret);
394 goto bail;
395 }
396 for (i = 1; i <= dd->num_pports; i++) {
397 char fname[24];
398
399 sprintf(fname, "port%dcounters", i);
400
401 ret = create_file(fname, S_IFREG|S_IRUGO, dir, &tmp,
402 &portcntr_ops[i], dd);
403 if (ret) {
404 pr_err("create_file(%s/%s) failed: %d\n",
405 unit, fname, ret);
406 goto bail;
407 }
408 if (!(dd->flags & QIB_HAS_QSFP))
409 continue;
410 sprintf(fname, "qsfp%d", i);
411 ret = create_file(fname, S_IFREG|S_IRUGO, dir, &tmp,
412 &qsfp_ops[i - 1], dd);
413 if (ret) {
414 pr_err("create_file(%s/%s) failed: %d\n",
415 unit, fname, ret);
416 goto bail;
417 }
418 }
419
420 ret = create_file("flash", S_IFREG|S_IWUSR|S_IRUGO, dir, &tmp,
421 &flash_ops, dd);
422 if (ret)
423 pr_err("create_file(%s/flash) failed: %d\n",
424 unit, ret);
425bail:
426 return ret;
427}
428
429static int remove_device_files(struct super_block *sb,
430 struct qib_devdata *dd)
431{
432 struct dentry *dir;
433 char unit[10];
434
435 snprintf(unit, sizeof(unit), "%u", dd->unit);
436 dir = lookup_one_len_unlocked(unit, sb->s_root, strlen(unit));
437
438 if (IS_ERR(dir)) {
439 pr_err("Lookup of %s failed\n", unit);
440 return PTR_ERR(dir);
441 }
442 simple_recursive_removal(dir, NULL);
443 return 0;
444}
445
446
447
448
449
450
451static int qibfs_fill_super(struct super_block *sb, struct fs_context *fc)
452{
453 struct qib_devdata *dd;
454 unsigned long index;
455 int ret;
456
457 static const struct tree_descr files[] = {
458 [2] = {"driver_stats", &driver_ops[0], S_IRUGO},
459 [3] = {"driver_stats_names", &driver_ops[1], S_IRUGO},
460 {""},
461 };
462
463 ret = simple_fill_super(sb, QIBFS_MAGIC, files);
464 if (ret) {
465 pr_err("simple_fill_super failed: %d\n", ret);
466 goto bail;
467 }
468
469 xa_for_each(&qib_dev_table, index, dd) {
470 ret = add_cntr_files(sb, dd);
471 if (ret)
472 goto bail;
473 }
474
475bail:
476 return ret;
477}
478
479static int qibfs_get_tree(struct fs_context *fc)
480{
481 int ret = get_tree_single(fc, qibfs_fill_super);
482 if (ret == 0)
483 qib_super = fc->root->d_sb;
484 return ret;
485}
486
487static const struct fs_context_operations qibfs_context_ops = {
488 .get_tree = qibfs_get_tree,
489};
490
491static int qibfs_init_fs_context(struct fs_context *fc)
492{
493 fc->ops = &qibfs_context_ops;
494 return 0;
495}
496
497static void qibfs_kill_super(struct super_block *s)
498{
499 kill_litter_super(s);
500 qib_super = NULL;
501}
502
503int qibfs_add(struct qib_devdata *dd)
504{
505 int ret;
506
507
508
509
510
511
512
513
514
515 if (qib_super == NULL)
516 ret = 0;
517 else
518 ret = add_cntr_files(qib_super, dd);
519 return ret;
520}
521
522int qibfs_remove(struct qib_devdata *dd)
523{
524 int ret = 0;
525
526 if (qib_super)
527 ret = remove_device_files(qib_super, dd);
528
529 return ret;
530}
531
532static struct file_system_type qibfs_fs_type = {
533 .owner = THIS_MODULE,
534 .name = "ipathfs",
535 .init_fs_context = qibfs_init_fs_context,
536 .kill_sb = qibfs_kill_super,
537};
538MODULE_ALIAS_FS("ipathfs");
539
540int __init qib_init_qibfs(void)
541{
542 return register_filesystem(&qibfs_fs_type);
543}
544
545int __exit qib_exit_qibfs(void)
546{
547 return unregister_filesystem(&qibfs_fs_type);
548}
549