1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2018 Xilinx, Inc.
5 * Vasileios Bimpikas <vasileios.bimpikas@xilinx.com>
7 #include <linux/init.h>
8 #include <linux/kobject.h>
9 #include <linux/module.h>
10 #include <linux/stat.h>
11 #include <linux/string.h>
12 #include <linux/sysfs.h>
13 #include "xroe_framer.h"
15 enum { XROE_SIZE_MAX = 15 };
17 static char xroe_tmp[XROE_SIZE_MAX];
20 * version_show - Returns the block's revision number
21 * @kobj: The kernel object of the entry
22 * @attr: The attributes of the kernel object
23 * @buff: The buffer containing the revision string
25 * Returns the block's major, minor & version revision numbers
26 * in a %d.%d.%d format
28 * Return: XROE_SIZE_MAX on success
30 static ssize_t version_show(struct kobject *kobj, struct kobj_attribute *attr,
37 major_rev = utils_sysfs_show_wrapper(CFG_MAJOR_REVISION_ADDR,
38 CFG_MAJOR_REVISION_OFFSET,
39 CFG_MAJOR_REVISION_MASK, kobj);
40 minor_rev = utils_sysfs_show_wrapper(CFG_MINOR_REVISION_ADDR,
41 CFG_MINOR_REVISION_OFFSET,
42 CFG_MINOR_REVISION_MASK, kobj);
43 version_rev = utils_sysfs_show_wrapper(CFG_VERSION_REVISION_ADDR,
44 CFG_VERSION_REVISION_OFFSET,
45 CFG_VERSION_REVISION_MASK, kobj);
46 sprintf(buff, "%d.%d.%d\n", major_rev, minor_rev, version_rev);
51 * version_store - Writes to the framer version sysfs entry (not permitted)
52 * @kobj: The kernel object of the entry
53 * @attr: The attributes of the kernel object
54 * @buff: The buffer containing the revision string
55 * @count: The number of characters typed by the user
57 * Writes to the framer version sysfs entry (not permitted)
61 static ssize_t version_store(struct kobject *kobj, struct kobj_attribute *attr,
62 const char *buff, size_t count)
68 * enable_show - Returns the framer's enable status
69 * @kobj: The kernel object of the entry
70 * @attr: The attributes of the kernel object
71 * @buff: The buffer containing the enable status
73 * Reads and writes the framer's enable status to the sysfs entry
75 * Return: XROE_SIZE_MAX on success
77 static ssize_t enable_show(struct kobject *kobj, struct kobj_attribute *attr,
82 enable = utils_sysfs_show_wrapper(CFG_MASTER_INT_ENABLE_ADDR,
83 CFG_MASTER_INT_ENABLE_OFFSET,
84 CFG_MASTER_INT_ENABLE_MASK, kobj);
86 sprintf(buff, "true\n");
88 sprintf(buff, "false\n");
93 * version_store - Writes to the framer's enable status register
94 * @kobj: The kernel object of the entry
95 * @attr: The attributes of the kernel object
96 * @buff: The buffer containing the enable status
97 * @count: The number of characters typed by the user
99 * Reads the user input and accordingly writes the framer's enable status
102 * Return: XROE_SIZE_MAX or the value of "count", if that's lesser, on success
104 static ssize_t enable_store(struct kobject *kobj, struct kobj_attribute *attr,
105 const char *buff, size_t count)
109 xroe_size = min_t(size_t, count, (size_t)XROE_SIZE_MAX);
110 strncpy(xroe_tmp, buff, xroe_size);
111 if (strncmp(xroe_tmp, "true", xroe_size) == 0)
113 else if (strncmp(xroe_tmp, "false", xroe_size) == 0)
115 utils_sysfs_store_wrapper(CFG_MASTER_INT_ENABLE_ADDR,
116 CFG_MASTER_INT_ENABLE_OFFSET,
117 CFG_MASTER_INT_ENABLE_MASK, enable, kobj);
122 * framer_restart_show - Returns the framer's restart status
123 * @kobj: The kernel object of the entry
124 * @attr: The attributes of the kernel object
125 * @buff: The buffer containing the restart status
127 * Reads and writes the framer's restart status to the sysfs entry
129 * Return: XROE_SIZE_MAX on success
131 static ssize_t framer_restart_show(struct kobject *kobj,
132 struct kobj_attribute *attr, char *buff)
136 restart = utils_sysfs_show_wrapper(FRAM_DISABLE_ADDR,
138 FRAM_DISABLE_MASK, kobj);
140 sprintf(buff, "true\n");
143 sprintf(buff, "false\n");
145 return XROE_SIZE_MAX;
149 * framer_restart_store - Writes to the framer's restart status register
150 * @kobj: The kernel object of the entry
151 * @attr: The attributes of the kernel object
152 * @buff: The buffer containing the restart status
153 * @count: The number of characters typed by the user
155 * Reads the user input and accordingly writes the framer's restart status
158 * Return: XROE_SIZE_MAX or the value of "count", if that's lesser, on success
160 static ssize_t framer_restart_store(struct kobject *kobj,
161 struct kobj_attribute *attr,
162 const char *buff, size_t count)
166 xroe_size = min_t(size_t, count, (size_t)XROE_SIZE_MAX);
167 strncpy(xroe_tmp, buff, xroe_size);
168 if (strncmp(xroe_tmp, "true", xroe_size) == 0)
170 else if (strncmp(xroe_tmp, "false", xroe_size) == 0)
172 utils_sysfs_store_wrapper(FRAM_DISABLE_ADDR, FRAM_DISABLE_OFFSET,
173 FRAM_DISABLE_MASK, restart, kobj);
178 * deframer_restart_show - Returns the deframer's restart status
179 * @kobj: The kernel object of the entry
180 * @attr: The attributes of the kernel object
181 * @buff: The buffer containing the restart status
183 * Reads and writes the deframer's restart status to the sysfs entry
185 * Return: XROE_SIZE_MAX on success
187 static ssize_t deframer_restart_show(struct kobject *kobj,
188 struct kobj_attribute *attr, char *buff)
190 u32 offset = DEFM_RESTART_OFFSET;
191 u32 mask = DEFM_RESTART_MASK;
194 void __iomem *working_address = ((u8 *)lp->base_addr
195 + DEFM_RESTART_ADDR);
197 buffer = ioread32(working_address);
198 restart = (buffer & mask) >> offset;
201 sprintf(buff, "true\n");
204 sprintf(buff, "false\n");
206 return XROE_SIZE_MAX;
210 * deframer_restart_store - Writes to the deframer's restart status register
211 * @kobj: The kernel object of the entry
212 * @attr: The attributes of the kernel object
213 * @buff: The buffer containing the restart status
214 * @count: The number of characters typed by the user
216 * Reads the user input and accordingly writes the deframer's restart status
219 * Return: XROE_SIZE_MAX or the value of "count", if that's lesser, on success
221 static ssize_t deframer_restart_store(struct kobject *kobj,
222 struct kobj_attribute *attr,
223 const char *buff, size_t count)
225 u32 offset = DEFM_RESTART_OFFSET;
226 u32 mask = DEFM_RESTART_MASK;
227 void __iomem *working_address = ((u8 *)lp->base_addr
228 + DEFM_RESTART_ADDR);
231 xroe_size = min_t(size_t, count, (size_t)XROE_SIZE_MAX);
232 strncpy(xroe_tmp, buff, xroe_size);
233 if (strncmp(xroe_tmp, "true", xroe_size) == 0) {
235 utils_write32withmask(working_address, restart,
237 } else if (strncmp(xroe_tmp, "false", xroe_size) == 0) {
239 utils_write32withmask(working_address, restart,
247 * xxv_reset_show - Returns the XXV's reset status
248 * @kobj: The kernel object of the entry
249 * @attr: The attributes of the kernel object
250 * @buff: The buffer containing the reset status
252 * Reads and writes the XXV's reset status to the sysfs entry
254 * Return: XROE_SIZE_MAX on success
256 static ssize_t xxv_reset_show(struct kobject *kobj, struct kobj_attribute *attr,
259 u32 offset = CFG_USER_RW_OUT_OFFSET;
260 u32 mask = CFG_USER_RW_OUT_MASK;
263 void __iomem *working_address = ((u8 *)lp->base_addr +
264 CFG_USER_RW_OUT_ADDR);
266 buffer = ioread32(working_address);
267 restart = (buffer & mask) >> offset;
269 sprintf(buff, "true\n");
271 sprintf(buff, "false\n");
272 return XROE_SIZE_MAX;
276 * xxv_reset_store - Writes to the XXV's reset register
277 * @kobj: The kernel object of the entry
278 * @attr: The attributes of the kernel object
279 * @buff: The buffer containing the reset status
280 * @count: The number of characters typed by the user
282 * Reads the user input and accordingly writes the XXV's reset status
285 * Return: XROE_SIZE_MAX or the value of "count", if that's lesser, on success
287 static ssize_t xxv_reset_store(struct kobject *kobj,
288 struct kobj_attribute *attr,
289 const char *buff, size_t count)
291 u32 offset = CFG_USER_RW_OUT_OFFSET;
292 u32 mask = CFG_USER_RW_OUT_MASK;
293 void __iomem *working_address = ((u8 *)lp->base_addr +
294 CFG_USER_RW_OUT_ADDR);
297 xroe_size = min_t(size_t, count, (size_t)XROE_SIZE_MAX);
298 strncpy(xroe_tmp, buff, xroe_size);
300 if (strncmp(xroe_tmp, "true", xroe_size) == 0) {
302 utils_write32withmask(working_address, restart,
304 } else if (strncmp(xroe_tmp, "false", xroe_size) == 0) {
306 utils_write32withmask(working_address, restart,
313 * framing_show - Returns the current framing
314 * @kobj: The kernel object of the entry
315 * @attr: The attributes of the kernel object
316 * @buff: The buffer containing the reset status
318 * Reads and writes the current framing type to the sysfs entry
320 * Return: XROE_SIZE_MAX on success
322 static ssize_t framing_show(struct kobject *kobj, struct kobj_attribute *attr,
325 u32 offset = (DEFM_DATA_PKT_MESSAGE_TYPE_ADDR +
326 DEFM_DATA_PKT_MESSAGE_TYPE_OFFSET);
329 void __iomem *working_address = ((u8 *)lp->base_addr + offset);
331 buffer = ioread8(working_address);
334 sprintf(buff, "eCPRI\n");
335 else if (framing == 1)
336 sprintf(buff, "1914.3\n");
337 return XROE_SIZE_MAX;
341 * framing_store - Writes to the current framing register
342 * @kobj: The kernel object of the entry
343 * @attr: The attributes of the kernel object
344 * @buff: The buffer containing the reset status
345 * @count: The number of characters typed by the user
347 * Reads the user input and accordingly writes the current framing
350 * Return: XROE_SIZE_MAX or the value of "count", if that's lesser, on success
352 static ssize_t framing_store(struct kobject *kobj, struct kobj_attribute *attr,
353 const char *buff, size_t count)
355 u32 offset = (DEFM_DATA_PKT_MESSAGE_TYPE_ADDR +
356 DEFM_DATA_PKT_MESSAGE_TYPE_OFFSET);
357 void __iomem *working_address = ((u8 *)lp->base_addr + offset);
359 xroe_size = min_t(size_t, count, (size_t)XROE_SIZE_MAX);
360 strncpy(xroe_tmp, buff, xroe_size);
361 if (strncmp(xroe_tmp, "eCPRI", xroe_size) == 0)
362 iowrite8(0, working_address);
363 else if (strncmp(xroe_tmp, "1914.3", xroe_size) == 0)
364 iowrite8(1, working_address);
368 /* TODO Use DEVICE_ATTR/_RW/_RO macros */
370 static struct kobj_attribute version_attribute =
371 __ATTR(version, 0444, version_show, version_store);
373 static struct kobj_attribute enable_attribute =
374 __ATTR(enable, 0660, enable_show, enable_store);
376 static struct kobj_attribute framer_restart =
377 __ATTR(framer_restart, 0660, framer_restart_show, framer_restart_store);
379 static struct kobj_attribute deframer_restart =
380 __ATTR(deframer_restart, 0660, deframer_restart_show,
381 deframer_restart_store);
383 static struct kobj_attribute xxv_reset =
384 __ATTR(xxv_reset, 0660, xxv_reset_show, xxv_reset_store);
386 static struct kobj_attribute framing_attribute =
387 __ATTR(framing, 0660, framing_show, framing_store);
389 static struct attribute *attrs[] = {
390 &version_attribute.attr,
391 &enable_attribute.attr,
392 &framer_restart.attr,
393 &deframer_restart.attr,
395 &framing_attribute.attr,
399 static struct attribute_group attr_group = {
403 struct kobject *root_xroe_kobj;
406 * xroe_sysfs_init - Creates the xroe sysfs directory and entries
408 * Return: 0 on success, negative value in case of failure to
409 * create the sysfs group
411 * Creates the xroe sysfs directory and entries, as well as the
412 * subdirectories for IPv4, IPv6 & UDP
414 int xroe_sysfs_init(void)
418 root_xroe_kobj = kobject_create_and_add("xroe", kernel_kobj);
421 ret = sysfs_create_group(root_xroe_kobj, &attr_group);
423 kobject_put(root_xroe_kobj);
424 ret = xroe_sysfs_ipv4_init();
427 ret = xroe_sysfs_ipv6_init();
430 ret = xroe_sysfs_udp_init();
435 * xroe_sysfs_exit - Deletes the xroe sysfs directory and entries
437 * Deletes the xroe sysfs directory and entries, as well as the
438 * subdirectories for IPv4, IPv6 & UDP
441 void xroe_sysfs_exit(void)
445 xroe_sysfs_ipv4_exit();
446 xroe_sysfs_ipv6_exit();
447 xroe_sysfs_udp_exit();
448 for (i = 0; i < MAX_NUM_ETH_PORTS; i++)
449 kobject_put(kobj_eth_ports[i]);
450 kobject_put(kobj_framer);
451 kobject_put(root_xroe_kobj);
455 * utils_write32withmask - Writes a masked 32-bit value
456 * @working_address: The starting address to write
457 * @value: The value to be written
458 * @mask: The mask to be used
459 * @offset: The offset from the provided starting address
461 * Writes a 32-bit value to the provided address with the input mask
463 * Return: 0 on success
465 int utils_write32withmask(void __iomem *working_address, u32 value,
466 u32 mask, u32 offset)
468 u32 read_register_value = 0;
469 u32 register_value_to_write = 0;
470 u32 delta = 0, buffer = 0;
472 read_register_value = ioread32(working_address);
473 buffer = (value << offset);
474 register_value_to_write = read_register_value & ~mask;
475 delta = buffer & mask;
476 register_value_to_write |= delta;
477 iowrite32(register_value_to_write, working_address);
482 * utils_sysfs_path_to_eth_port_num - Get the current ethernet port
483 * @kobj: The kobject of the entry calling the function
485 * Extracts the number of the current ethernet port instance
487 * Return: The number of the ethernet port instance (0 - MAX_NUM_ETH_PORTS) on
488 * success, -1 otherwise
490 static int utils_sysfs_path_to_eth_port_num(struct kobject *kobj)
492 char *current_path = NULL;
496 current_path = kobject_get_path(kobj, GFP_KERNEL);
497 ret = sscanf(current_path, "/kernel/xroe/framer/eth_port_%d/", &port);
498 /* if sscanf() returns 0, no fields were assigned, therefore no
499 * adjustments will be made for port number
503 // printk(KERN_ALERT "current_path: %s port: %d\n", current_path, port);
509 * utils_sysfs_store_wrapper - Wraps the storing function for sysfs entries
510 * @address: The address of the register to be written
511 * @offset: The offset from the address of the register
512 * @mask: The mask to be used on the value to be written
513 * @value: The value to be written to the register
514 * @kobj: The kobject of the entry calling the function
516 * Wraps the core functionality of all "store" functions of sysfs entries.
517 * After calculating the ethernet port number (in N/A cases, it's 0), the value
518 * is written to the designated register
521 void utils_sysfs_store_wrapper(u32 address, u32 offset, u32 mask, u32 value,
522 struct kobject *kobj)
525 void __iomem *working_address;
528 port = utils_sysfs_path_to_eth_port_num(kobj);
529 working_address = (void __iomem *)(lp->base_addr +
530 (address + (0x100 * port)));
531 buffer = ioread32(working_address);
532 utils_write32withmask(working_address, value, mask, offset);
536 * utils_sysfs_store_wrapper - Wraps the storing function for sysfs entries
537 * @address: The address of the register to be read
538 * @offset: The offset from the address of the register
539 * @mask: The mask to be used on the value to be read
540 * @kobj: The kobject of the entry calling the function
542 * Wraps the core functionality of all "show" functions of sysfs entries.
543 * After calculating the ethernet port number (in N/A cases, it's 0), the value
544 * is read from the designated register and returned.
546 * Return: The value designated by the address, offset and mask
548 u32 utils_sysfs_show_wrapper(u32 address, u32 offset, u32 mask,
549 struct kobject *kobj)
552 void __iomem *working_address;
555 port = utils_sysfs_path_to_eth_port_num(kobj);
556 working_address = (void __iomem *)(lp->base_addr +
557 (address + (0x100 * port)));
558 buffer = ioread32(working_address);
559 return (buffer & mask) >> offset;