GNU Linux-libre 4.19.264-gnu1
[releases.git] / drivers / net / wireless / intel / iwlwifi / fw / debugfs.c
1 /******************************************************************************
2  *
3  * This file is provided under a dual BSD/GPLv2 license.  When using or
4  * redistributing this file, you may do so under either license.
5  *
6  * GPL LICENSE SUMMARY
7  *
8  * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
9  * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
10  * Copyright(c) 2016 - 2017 Intel Deutschland GmbH
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of version 2 of the GNU General Public License as
14  * published by the Free Software Foundation.
15  *
16  * This program 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.
23  *
24  * The full GNU General Public License is included in this distribution
25  * in the file called COPYING.
26  *
27  * Contact Information:
28  *  Intel Linux Wireless <linuxwifi@intel.com>
29  * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
30  *
31  * BSD LICENSE
32  *
33  * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
34  * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
35  * Copyright(c) 2016 - 2017 Intel Deutschland GmbH
36  * All rights reserved.
37  *
38  * Redistribution and use in source and binary forms, with or without
39  * modification, are permitted provided that the following conditions
40  * are met:
41  *
42  *  * Redistributions of source code must retain the above copyright
43  *    notice, this list of conditions and the following disclaimer.
44  *  * Redistributions in binary form must reproduce the above copyright
45  *    notice, this list of conditions and the following disclaimer in
46  *    the documentation and/or other materials provided with the
47  *    distribution.
48  *  * Neither the name Intel Corporation nor the names of its
49  *    contributors may be used to endorse or promote products derived
50  *    from this software without specific prior written permission.
51  *
52  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
53  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
54  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
55  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
56  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
57  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
58  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
59  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
60  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
61  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
62  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
63  *
64  *****************************************************************************/
65 #include "api/commands.h"
66 #include "debugfs.h"
67 #include "dbg.h"
68
69 #define FWRT_DEBUGFS_READ_FILE_OPS(name)                                \
70 static ssize_t iwl_dbgfs_##name##_read(struct iwl_fw_runtime *fwrt,     \
71                                        char *buf, size_t count,         \
72                                        loff_t *ppos);                   \
73 static const struct file_operations iwl_dbgfs_##name##_ops = {          \
74         .read = iwl_dbgfs_##name##_read,                                \
75         .open = simple_open,                                            \
76         .llseek = generic_file_llseek,                                  \
77 }
78
79 #define FWRT_DEBUGFS_WRITE_WRAPPER(name, buflen)                        \
80 static ssize_t iwl_dbgfs_##name##_write(struct iwl_fw_runtime *fwrt,    \
81                                         char *buf, size_t count,        \
82                                         loff_t *ppos);                  \
83 static ssize_t _iwl_dbgfs_##name##_write(struct file *file,             \
84                                          const char __user *user_buf,   \
85                                          size_t count, loff_t *ppos)    \
86 {                                                                       \
87         struct iwl_fw_runtime *fwrt = file->private_data;               \
88         char buf[buflen] = {};                                          \
89         size_t buf_size = min(count, sizeof(buf) -  1);                 \
90                                                                         \
91         if (copy_from_user(buf, user_buf, buf_size))                    \
92                 return -EFAULT;                                         \
93                                                                         \
94         return iwl_dbgfs_##name##_write(fwrt, buf, buf_size, ppos);     \
95 }
96
97 #define FWRT_DEBUGFS_READ_WRITE_FILE_OPS(name, buflen)                  \
98 FWRT_DEBUGFS_WRITE_WRAPPER(name, buflen)                                \
99 static const struct file_operations iwl_dbgfs_##name##_ops = {          \
100         .write = _iwl_dbgfs_##name##_write,                             \
101         .read = iwl_dbgfs_##name##_read,                                \
102         .open = simple_open,                                            \
103         .llseek = generic_file_llseek,                                  \
104 }
105
106 #define FWRT_DEBUGFS_WRITE_FILE_OPS(name, buflen)                       \
107 FWRT_DEBUGFS_WRITE_WRAPPER(name, buflen)                                \
108 static const struct file_operations iwl_dbgfs_##name##_ops = {          \
109         .write = _iwl_dbgfs_##name##_write,                             \
110         .open = simple_open,                                            \
111         .llseek = generic_file_llseek,                                  \
112 }
113
114 #define FWRT_DEBUGFS_ADD_FILE_ALIAS(alias, name, parent, mode) do {     \
115                 if (!debugfs_create_file(alias, mode, parent, fwrt,     \
116                                          &iwl_dbgfs_##name##_ops))      \
117                         goto err;                                       \
118         } while (0)
119 #define FWRT_DEBUGFS_ADD_FILE(name, parent, mode) \
120         FWRT_DEBUGFS_ADD_FILE_ALIAS(#name, name, parent, mode)
121
122 static int iwl_fw_send_timestamp_marker_cmd(struct iwl_fw_runtime *fwrt)
123 {
124         struct iwl_mvm_marker marker = {
125                 .dw_len = sizeof(struct iwl_mvm_marker) / 4,
126                 .marker_id = MARKER_ID_SYNC_CLOCK,
127
128                 /* the real timestamp is taken from the ftrace clock
129                  * this is for finding the match between fw and kernel logs
130                  */
131                 .timestamp = cpu_to_le64(fwrt->timestamp.seq++),
132         };
133
134         struct iwl_host_cmd hcmd = {
135                 .id = MARKER_CMD,
136                 .flags = CMD_ASYNC,
137                 .data[0] = &marker,
138                 .len[0] = sizeof(marker),
139         };
140
141         return iwl_trans_send_cmd(fwrt->trans, &hcmd);
142 }
143
144 static void iwl_fw_timestamp_marker_wk(struct work_struct *work)
145 {
146         int ret;
147         struct iwl_fw_runtime *fwrt =
148                 container_of(work, struct iwl_fw_runtime, timestamp.wk.work);
149         unsigned long delay = fwrt->timestamp.delay;
150
151         ret = iwl_fw_send_timestamp_marker_cmd(fwrt);
152         if (!ret && delay)
153                 schedule_delayed_work(&fwrt->timestamp.wk,
154                                       round_jiffies_relative(delay));
155         else
156                 IWL_INFO(fwrt,
157                          "stopping timestamp_marker, ret: %d, delay: %u\n",
158                          ret, jiffies_to_msecs(delay) / 1000);
159 }
160
161 void iwl_fw_trigger_timestamp(struct iwl_fw_runtime *fwrt, u32 delay)
162 {
163         IWL_INFO(fwrt,
164                  "starting timestamp_marker trigger with delay: %us\n",
165                  delay);
166
167         iwl_fw_cancel_timestamp(fwrt);
168
169         fwrt->timestamp.delay = msecs_to_jiffies(delay * 1000);
170
171         schedule_delayed_work(&fwrt->timestamp.wk,
172                               round_jiffies_relative(fwrt->timestamp.delay));
173 }
174
175 static ssize_t iwl_dbgfs_timestamp_marker_write(struct iwl_fw_runtime *fwrt,
176                                                 char *buf, size_t count,
177                                                 loff_t *ppos)
178 {
179         int ret;
180         u32 delay;
181
182         ret = kstrtou32(buf, 10, &delay);
183         if (ret < 0)
184                 return ret;
185
186         iwl_fw_trigger_timestamp(fwrt, delay);
187
188         return count;
189 }
190
191 FWRT_DEBUGFS_WRITE_FILE_OPS(timestamp_marker, 10);
192
193 int iwl_fwrt_dbgfs_register(struct iwl_fw_runtime *fwrt,
194                             struct dentry *dbgfs_dir)
195 {
196         INIT_DELAYED_WORK(&fwrt->timestamp.wk, iwl_fw_timestamp_marker_wk);
197         FWRT_DEBUGFS_ADD_FILE(timestamp_marker, dbgfs_dir, 0200);
198         return 0;
199 err:
200         IWL_ERR(fwrt, "Can't create the fwrt debugfs directory\n");
201         return -ENOMEM;
202 }