xgbe-debugfs.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. /*
  2. * AMD 10Gb Ethernet driver
  3. *
  4. * This file is available to you under your choice of the following two
  5. * licenses:
  6. *
  7. * License 1: GPLv2
  8. *
  9. * Copyright (c) 2014 Advanced Micro Devices, Inc.
  10. *
  11. * This file is free software; you may copy, redistribute and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation, either version 2 of the License, or (at
  14. * your option) any later version.
  15. *
  16. * This file is distributed in the hope that it will be useful, but
  17. * WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. * General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. *
  24. * This file incorporates work covered by the following copyright and
  25. * permission notice:
  26. * The Synopsys DWC ETHER XGMAC Software Driver and documentation
  27. * (hereinafter "Software") is an unsupported proprietary work of Synopsys,
  28. * Inc. unless otherwise expressly agreed to in writing between Synopsys
  29. * and you.
  30. *
  31. * The Software IS NOT an item of Licensed Software or Licensed Product
  32. * under any End User Software License Agreement or Agreement for Licensed
  33. * Product with Synopsys or any supplement thereto. Permission is hereby
  34. * granted, free of charge, to any person obtaining a copy of this software
  35. * annotated with this license and the Software, to deal in the Software
  36. * without restriction, including without limitation the rights to use,
  37. * copy, modify, merge, publish, distribute, sublicense, and/or sell copies
  38. * of the Software, and to permit persons to whom the Software is furnished
  39. * to do so, subject to the following conditions:
  40. *
  41. * The above copyright notice and this permission notice shall be included
  42. * in all copies or substantial portions of the Software.
  43. *
  44. * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"
  45. * BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  46. * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
  47. * PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS
  48. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  49. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  50. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  51. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  52. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  53. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  54. * THE POSSIBILITY OF SUCH DAMAGE.
  55. *
  56. *
  57. * License 2: Modified BSD
  58. *
  59. * Copyright (c) 2014 Advanced Micro Devices, Inc.
  60. * All rights reserved.
  61. *
  62. * Redistribution and use in source and binary forms, with or without
  63. * modification, are permitted provided that the following conditions are met:
  64. * * Redistributions of source code must retain the above copyright
  65. * notice, this list of conditions and the following disclaimer.
  66. * * Redistributions in binary form must reproduce the above copyright
  67. * notice, this list of conditions and the following disclaimer in the
  68. * documentation and/or other materials provided with the distribution.
  69. * * Neither the name of Advanced Micro Devices, Inc. nor the
  70. * names of its contributors may be used to endorse or promote products
  71. * derived from this software without specific prior written permission.
  72. *
  73. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  74. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  75. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  76. * ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
  77. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  78. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  79. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  80. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  81. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  82. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  83. *
  84. * This file incorporates work covered by the following copyright and
  85. * permission notice:
  86. * The Synopsys DWC ETHER XGMAC Software Driver and documentation
  87. * (hereinafter "Software") is an unsupported proprietary work of Synopsys,
  88. * Inc. unless otherwise expressly agreed to in writing between Synopsys
  89. * and you.
  90. *
  91. * The Software IS NOT an item of Licensed Software or Licensed Product
  92. * under any End User Software License Agreement or Agreement for Licensed
  93. * Product with Synopsys or any supplement thereto. Permission is hereby
  94. * granted, free of charge, to any person obtaining a copy of this software
  95. * annotated with this license and the Software, to deal in the Software
  96. * without restriction, including without limitation the rights to use,
  97. * copy, modify, merge, publish, distribute, sublicense, and/or sell copies
  98. * of the Software, and to permit persons to whom the Software is furnished
  99. * to do so, subject to the following conditions:
  100. *
  101. * The above copyright notice and this permission notice shall be included
  102. * in all copies or substantial portions of the Software.
  103. *
  104. * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"
  105. * BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  106. * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
  107. * PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS
  108. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  109. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  110. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  111. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  112. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  113. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  114. * THE POSSIBILITY OF SUCH DAMAGE.
  115. */
  116. #include <linux/debugfs.h>
  117. #include <linux/module.h>
  118. #include <linux/slab.h>
  119. #include "xgbe.h"
  120. #include "xgbe-common.h"
  121. static ssize_t xgbe_common_read(char __user *buffer, size_t count,
  122. loff_t *ppos, unsigned int value)
  123. {
  124. char *buf;
  125. ssize_t len;
  126. if (*ppos != 0)
  127. return 0;
  128. buf = kasprintf(GFP_KERNEL, "0x%08x\n", value);
  129. if (!buf)
  130. return -ENOMEM;
  131. if (count < strlen(buf)) {
  132. kfree(buf);
  133. return -ENOSPC;
  134. }
  135. len = simple_read_from_buffer(buffer, count, ppos, buf, strlen(buf));
  136. kfree(buf);
  137. return len;
  138. }
  139. static ssize_t xgbe_common_write(const char __user *buffer, size_t count,
  140. loff_t *ppos, unsigned int *value)
  141. {
  142. char workarea[32];
  143. ssize_t len;
  144. unsigned int scan_value;
  145. if (*ppos != 0)
  146. return 0;
  147. if (count >= sizeof(workarea))
  148. return -ENOSPC;
  149. len = simple_write_to_buffer(workarea, sizeof(workarea) - 1, ppos,
  150. buffer, count);
  151. if (len < 0)
  152. return len;
  153. workarea[len] = '\0';
  154. if (sscanf(workarea, "%x", &scan_value) == 1)
  155. *value = scan_value;
  156. else
  157. return -EIO;
  158. return len;
  159. }
  160. static ssize_t xgmac_reg_addr_read(struct file *filp, char __user *buffer,
  161. size_t count, loff_t *ppos)
  162. {
  163. struct xgbe_prv_data *pdata = filp->private_data;
  164. return xgbe_common_read(buffer, count, ppos, pdata->debugfs_xgmac_reg);
  165. }
  166. static ssize_t xgmac_reg_addr_write(struct file *filp,
  167. const char __user *buffer,
  168. size_t count, loff_t *ppos)
  169. {
  170. struct xgbe_prv_data *pdata = filp->private_data;
  171. return xgbe_common_write(buffer, count, ppos,
  172. &pdata->debugfs_xgmac_reg);
  173. }
  174. static ssize_t xgmac_reg_value_read(struct file *filp, char __user *buffer,
  175. size_t count, loff_t *ppos)
  176. {
  177. struct xgbe_prv_data *pdata = filp->private_data;
  178. unsigned int value;
  179. value = XGMAC_IOREAD(pdata, pdata->debugfs_xgmac_reg);
  180. return xgbe_common_read(buffer, count, ppos, value);
  181. }
  182. static ssize_t xgmac_reg_value_write(struct file *filp,
  183. const char __user *buffer,
  184. size_t count, loff_t *ppos)
  185. {
  186. struct xgbe_prv_data *pdata = filp->private_data;
  187. unsigned int value;
  188. ssize_t len;
  189. len = xgbe_common_write(buffer, count, ppos, &value);
  190. if (len < 0)
  191. return len;
  192. XGMAC_IOWRITE(pdata, pdata->debugfs_xgmac_reg, value);
  193. return len;
  194. }
  195. static const struct file_operations xgmac_reg_addr_fops = {
  196. .owner = THIS_MODULE,
  197. .open = simple_open,
  198. .read = xgmac_reg_addr_read,
  199. .write = xgmac_reg_addr_write,
  200. };
  201. static const struct file_operations xgmac_reg_value_fops = {
  202. .owner = THIS_MODULE,
  203. .open = simple_open,
  204. .read = xgmac_reg_value_read,
  205. .write = xgmac_reg_value_write,
  206. };
  207. static ssize_t xpcs_mmd_read(struct file *filp, char __user *buffer,
  208. size_t count, loff_t *ppos)
  209. {
  210. struct xgbe_prv_data *pdata = filp->private_data;
  211. return xgbe_common_read(buffer, count, ppos, pdata->debugfs_xpcs_mmd);
  212. }
  213. static ssize_t xpcs_mmd_write(struct file *filp, const char __user *buffer,
  214. size_t count, loff_t *ppos)
  215. {
  216. struct xgbe_prv_data *pdata = filp->private_data;
  217. return xgbe_common_write(buffer, count, ppos,
  218. &pdata->debugfs_xpcs_mmd);
  219. }
  220. static ssize_t xpcs_reg_addr_read(struct file *filp, char __user *buffer,
  221. size_t count, loff_t *ppos)
  222. {
  223. struct xgbe_prv_data *pdata = filp->private_data;
  224. return xgbe_common_read(buffer, count, ppos, pdata->debugfs_xpcs_reg);
  225. }
  226. static ssize_t xpcs_reg_addr_write(struct file *filp, const char __user *buffer,
  227. size_t count, loff_t *ppos)
  228. {
  229. struct xgbe_prv_data *pdata = filp->private_data;
  230. return xgbe_common_write(buffer, count, ppos,
  231. &pdata->debugfs_xpcs_reg);
  232. }
  233. static ssize_t xpcs_reg_value_read(struct file *filp, char __user *buffer,
  234. size_t count, loff_t *ppos)
  235. {
  236. struct xgbe_prv_data *pdata = filp->private_data;
  237. unsigned int value;
  238. value = pdata->hw_if.read_mmd_regs(pdata, pdata->debugfs_xpcs_mmd,
  239. pdata->debugfs_xpcs_reg);
  240. return xgbe_common_read(buffer, count, ppos, value);
  241. }
  242. static ssize_t xpcs_reg_value_write(struct file *filp,
  243. const char __user *buffer,
  244. size_t count, loff_t *ppos)
  245. {
  246. struct xgbe_prv_data *pdata = filp->private_data;
  247. unsigned int value;
  248. ssize_t len;
  249. len = xgbe_common_write(buffer, count, ppos, &value);
  250. if (len < 0)
  251. return len;
  252. pdata->hw_if.write_mmd_regs(pdata, pdata->debugfs_xpcs_mmd,
  253. pdata->debugfs_xpcs_reg, value);
  254. return len;
  255. }
  256. static const struct file_operations xpcs_mmd_fops = {
  257. .owner = THIS_MODULE,
  258. .open = simple_open,
  259. .read = xpcs_mmd_read,
  260. .write = xpcs_mmd_write,
  261. };
  262. static const struct file_operations xpcs_reg_addr_fops = {
  263. .owner = THIS_MODULE,
  264. .open = simple_open,
  265. .read = xpcs_reg_addr_read,
  266. .write = xpcs_reg_addr_write,
  267. };
  268. static const struct file_operations xpcs_reg_value_fops = {
  269. .owner = THIS_MODULE,
  270. .open = simple_open,
  271. .read = xpcs_reg_value_read,
  272. .write = xpcs_reg_value_write,
  273. };
  274. void xgbe_debugfs_init(struct xgbe_prv_data *pdata)
  275. {
  276. struct dentry *pfile;
  277. char *buf;
  278. /* Set defaults */
  279. pdata->debugfs_xgmac_reg = 0;
  280. pdata->debugfs_xpcs_mmd = 1;
  281. pdata->debugfs_xpcs_reg = 0;
  282. buf = kasprintf(GFP_KERNEL, "amd-xgbe-%s", pdata->netdev->name);
  283. pdata->xgbe_debugfs = debugfs_create_dir(buf, NULL);
  284. if (pdata->xgbe_debugfs == NULL) {
  285. netdev_err(pdata->netdev, "debugfs_create_dir failed\n");
  286. return;
  287. }
  288. pfile = debugfs_create_file("xgmac_register", 0600,
  289. pdata->xgbe_debugfs, pdata,
  290. &xgmac_reg_addr_fops);
  291. if (!pfile)
  292. netdev_err(pdata->netdev, "debugfs_create_file failed\n");
  293. pfile = debugfs_create_file("xgmac_register_value", 0600,
  294. pdata->xgbe_debugfs, pdata,
  295. &xgmac_reg_value_fops);
  296. if (!pfile)
  297. netdev_err(pdata->netdev, "debugfs_create_file failed\n");
  298. pfile = debugfs_create_file("xpcs_mmd", 0600,
  299. pdata->xgbe_debugfs, pdata,
  300. &xpcs_mmd_fops);
  301. if (!pfile)
  302. netdev_err(pdata->netdev, "debugfs_create_file failed\n");
  303. pfile = debugfs_create_file("xpcs_register", 0600,
  304. pdata->xgbe_debugfs, pdata,
  305. &xpcs_reg_addr_fops);
  306. if (!pfile)
  307. netdev_err(pdata->netdev, "debugfs_create_file failed\n");
  308. pfile = debugfs_create_file("xpcs_register_value", 0600,
  309. pdata->xgbe_debugfs, pdata,
  310. &xpcs_reg_value_fops);
  311. if (!pfile)
  312. netdev_err(pdata->netdev, "debugfs_create_file failed\n");
  313. kfree(buf);
  314. }
  315. void xgbe_debugfs_exit(struct xgbe_prv_data *pdata)
  316. {
  317. debugfs_remove_recursive(pdata->xgbe_debugfs);
  318. pdata->xgbe_debugfs = NULL;
  319. }