GNU Linux-libre 4.14.313-gnu1
[releases.git] / drivers / net / wireless / intel / iwlwifi / iwl-io.c
1 /******************************************************************************
2  *
3  * Copyright(c) 2003 - 2014 Intel Corporation. All rights reserved.
4  * Copyright(c) 2015 - 2016 Intel Deutschland GmbH
5  *
6  * Portions of this file are derived from the ipw3945 project.
7  *
8  * This program is free software; you can redistribute it and/or modify it
9  * under the terms of version 2 of the GNU General Public License as
10  * published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
15  * more details.
16  *
17  * You should have received a copy of the GNU General Public License along with
18  * this program; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
20  *
21  * The full GNU General Public License is included in this distribution in the
22  * file called LICENSE.
23  *
24  * Contact Information:
25  *  Intel Linux Wireless <linuxwifi@intel.com>
26  * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
27  *
28  *****************************************************************************/
29 #include <linux/delay.h>
30 #include <linux/device.h>
31 #include <linux/export.h>
32
33 #include "iwl-drv.h"
34 #include "iwl-io.h"
35 #include "iwl-csr.h"
36 #include "iwl-debug.h"
37 #include "iwl-prph.h"
38 #include "iwl-fh.h"
39
40 void iwl_write8(struct iwl_trans *trans, u32 ofs, u8 val)
41 {
42         trace_iwlwifi_dev_iowrite8(trans->dev, ofs, val);
43         iwl_trans_write8(trans, ofs, val);
44 }
45 IWL_EXPORT_SYMBOL(iwl_write8);
46
47 void iwl_write32(struct iwl_trans *trans, u32 ofs, u32 val)
48 {
49         trace_iwlwifi_dev_iowrite32(trans->dev, ofs, val);
50         iwl_trans_write32(trans, ofs, val);
51 }
52 IWL_EXPORT_SYMBOL(iwl_write32);
53
54 void iwl_write64(struct iwl_trans *trans, u64 ofs, u64 val)
55 {
56         trace_iwlwifi_dev_iowrite64(trans->dev, ofs, val);
57         iwl_trans_write32(trans, ofs, lower_32_bits(val));
58         iwl_trans_write32(trans, ofs + 4, upper_32_bits(val));
59 }
60 IWL_EXPORT_SYMBOL(iwl_write64);
61
62 u32 iwl_read32(struct iwl_trans *trans, u32 ofs)
63 {
64         u32 val = iwl_trans_read32(trans, ofs);
65
66         trace_iwlwifi_dev_ioread32(trans->dev, ofs, val);
67         return val;
68 }
69 IWL_EXPORT_SYMBOL(iwl_read32);
70
71 #define IWL_POLL_INTERVAL 10    /* microseconds */
72
73 int iwl_poll_bit(struct iwl_trans *trans, u32 addr,
74                  u32 bits, u32 mask, int timeout)
75 {
76         int t = 0;
77
78         do {
79                 if ((iwl_read32(trans, addr) & mask) == (bits & mask))
80                         return t;
81                 udelay(IWL_POLL_INTERVAL);
82                 t += IWL_POLL_INTERVAL;
83         } while (t < timeout);
84
85         return -ETIMEDOUT;
86 }
87 IWL_EXPORT_SYMBOL(iwl_poll_bit);
88
89 u32 iwl_read_direct32(struct iwl_trans *trans, u32 reg)
90 {
91         u32 value = 0x5a5a5a5a;
92         unsigned long flags;
93         if (iwl_trans_grab_nic_access(trans, &flags)) {
94                 value = iwl_read32(trans, reg);
95                 iwl_trans_release_nic_access(trans, &flags);
96         }
97
98         return value;
99 }
100 IWL_EXPORT_SYMBOL(iwl_read_direct32);
101
102 void iwl_write_direct32(struct iwl_trans *trans, u32 reg, u32 value)
103 {
104         unsigned long flags;
105
106         if (iwl_trans_grab_nic_access(trans, &flags)) {
107                 iwl_write32(trans, reg, value);
108                 iwl_trans_release_nic_access(trans, &flags);
109         }
110 }
111 IWL_EXPORT_SYMBOL(iwl_write_direct32);
112
113 void iwl_write_direct64(struct iwl_trans *trans, u64 reg, u64 value)
114 {
115         unsigned long flags;
116
117         if (iwl_trans_grab_nic_access(trans, &flags)) {
118                 iwl_write64(trans, reg, value);
119                 iwl_trans_release_nic_access(trans, &flags);
120         }
121 }
122 IWL_EXPORT_SYMBOL(iwl_write_direct64);
123
124 int iwl_poll_direct_bit(struct iwl_trans *trans, u32 addr, u32 mask,
125                         int timeout)
126 {
127         int t = 0;
128
129         do {
130                 if ((iwl_read_direct32(trans, addr) & mask) == mask)
131                         return t;
132                 udelay(IWL_POLL_INTERVAL);
133                 t += IWL_POLL_INTERVAL;
134         } while (t < timeout);
135
136         return -ETIMEDOUT;
137 }
138 IWL_EXPORT_SYMBOL(iwl_poll_direct_bit);
139
140 u32 iwl_read_prph_no_grab(struct iwl_trans *trans, u32 ofs)
141 {
142         u32 val = iwl_trans_read_prph(trans, ofs);
143         trace_iwlwifi_dev_ioread_prph32(trans->dev, ofs, val);
144         return val;
145 }
146 IWL_EXPORT_SYMBOL(iwl_read_prph_no_grab);
147
148 void iwl_write_prph_no_grab(struct iwl_trans *trans, u32 ofs, u32 val)
149 {
150         trace_iwlwifi_dev_iowrite_prph32(trans->dev, ofs, val);
151         iwl_trans_write_prph(trans, ofs, val);
152 }
153 IWL_EXPORT_SYMBOL(iwl_write_prph_no_grab);
154
155 void iwl_write_prph64_no_grab(struct iwl_trans *trans, u64 ofs, u64 val)
156 {
157         trace_iwlwifi_dev_iowrite_prph64(trans->dev, ofs, val);
158         iwl_write_prph_no_grab(trans, ofs, val & 0xffffffff);
159         iwl_write_prph_no_grab(trans, ofs + 4, val >> 32);
160 }
161 IWL_EXPORT_SYMBOL(iwl_write_prph64_no_grab);
162
163 u32 iwl_read_prph(struct iwl_trans *trans, u32 ofs)
164 {
165         unsigned long flags;
166         u32 val = 0x5a5a5a5a;
167
168         if (iwl_trans_grab_nic_access(trans, &flags)) {
169                 val = iwl_read_prph_no_grab(trans, ofs);
170                 iwl_trans_release_nic_access(trans, &flags);
171         }
172         return val;
173 }
174 IWL_EXPORT_SYMBOL(iwl_read_prph);
175
176 void iwl_write_prph(struct iwl_trans *trans, u32 ofs, u32 val)
177 {
178         unsigned long flags;
179
180         if (iwl_trans_grab_nic_access(trans, &flags)) {
181                 iwl_write_prph_no_grab(trans, ofs, val);
182                 iwl_trans_release_nic_access(trans, &flags);
183         }
184 }
185 IWL_EXPORT_SYMBOL(iwl_write_prph);
186
187 int iwl_poll_prph_bit(struct iwl_trans *trans, u32 addr,
188                       u32 bits, u32 mask, int timeout)
189 {
190         int t = 0;
191
192         do {
193                 if ((iwl_read_prph(trans, addr) & mask) == (bits & mask))
194                         return t;
195                 udelay(IWL_POLL_INTERVAL);
196                 t += IWL_POLL_INTERVAL;
197         } while (t < timeout);
198
199         return -ETIMEDOUT;
200 }
201
202 void iwl_set_bits_prph(struct iwl_trans *trans, u32 ofs, u32 mask)
203 {
204         unsigned long flags;
205
206         if (iwl_trans_grab_nic_access(trans, &flags)) {
207                 iwl_write_prph_no_grab(trans, ofs,
208                                        iwl_read_prph_no_grab(trans, ofs) |
209                                        mask);
210                 iwl_trans_release_nic_access(trans, &flags);
211         }
212 }
213 IWL_EXPORT_SYMBOL(iwl_set_bits_prph);
214
215 void iwl_set_bits_mask_prph(struct iwl_trans *trans, u32 ofs,
216                             u32 bits, u32 mask)
217 {
218         unsigned long flags;
219
220         if (iwl_trans_grab_nic_access(trans, &flags)) {
221                 iwl_write_prph_no_grab(trans, ofs,
222                                        (iwl_read_prph_no_grab(trans, ofs) &
223                                         mask) | bits);
224                 iwl_trans_release_nic_access(trans, &flags);
225         }
226 }
227 IWL_EXPORT_SYMBOL(iwl_set_bits_mask_prph);
228
229 void iwl_clear_bits_prph(struct iwl_trans *trans, u32 ofs, u32 mask)
230 {
231         unsigned long flags;
232         u32 val;
233
234         if (iwl_trans_grab_nic_access(trans, &flags)) {
235                 val = iwl_read_prph_no_grab(trans, ofs);
236                 iwl_write_prph_no_grab(trans, ofs, (val & ~mask));
237                 iwl_trans_release_nic_access(trans, &flags);
238         }
239 }
240 IWL_EXPORT_SYMBOL(iwl_clear_bits_prph);
241
242 void iwl_force_nmi(struct iwl_trans *trans)
243 {
244         if (trans->cfg->device_family < IWL_DEVICE_FAMILY_9000)
245                 iwl_write_prph(trans, DEVICE_SET_NMI_REG,
246                                DEVICE_SET_NMI_VAL_DRV);
247         else
248                 iwl_write_prph(trans, UREG_NIC_SET_NMI_DRIVER,
249                                UREG_NIC_SET_NMI_DRIVER_NMI_FROM_DRIVER_MSK);
250 }
251 IWL_EXPORT_SYMBOL(iwl_force_nmi);
252
253 static const char *get_rfh_string(int cmd)
254 {
255 #define IWL_CMD(x) case x: return #x
256 #define IWL_CMD_MQ(arg, reg, q) { if (arg == reg(q)) return #reg; }
257
258         int i;
259
260         for (i = 0; i < IWL_MAX_RX_HW_QUEUES; i++) {
261                 IWL_CMD_MQ(cmd, RFH_Q_FRBDCB_BA_LSB, i);
262                 IWL_CMD_MQ(cmd, RFH_Q_FRBDCB_WIDX, i);
263                 IWL_CMD_MQ(cmd, RFH_Q_FRBDCB_RIDX, i);
264                 IWL_CMD_MQ(cmd, RFH_Q_URBD_STTS_WPTR_LSB, i);
265         }
266
267         switch (cmd) {
268         IWL_CMD(RFH_RXF_DMA_CFG);
269         IWL_CMD(RFH_GEN_CFG);
270         IWL_CMD(RFH_GEN_STATUS);
271         IWL_CMD(FH_TSSR_TX_STATUS_REG);
272         IWL_CMD(FH_TSSR_TX_ERROR_REG);
273         default:
274                 return "UNKNOWN";
275         }
276 #undef IWL_CMD_MQ
277 }
278
279 struct reg {
280         u32 addr;
281         bool is64;
282 };
283
284 static int iwl_dump_rfh(struct iwl_trans *trans, char **buf)
285 {
286         int i, q;
287         int num_q = trans->num_rx_queues;
288         static const u32 rfh_tbl[] = {
289                 RFH_RXF_DMA_CFG,
290                 RFH_GEN_CFG,
291                 RFH_GEN_STATUS,
292                 FH_TSSR_TX_STATUS_REG,
293                 FH_TSSR_TX_ERROR_REG,
294         };
295         static const struct reg rfh_mq_tbl[] = {
296                 { RFH_Q0_FRBDCB_BA_LSB, true },
297                 { RFH_Q0_FRBDCB_WIDX, false },
298                 { RFH_Q0_FRBDCB_RIDX, false },
299                 { RFH_Q0_URBD_STTS_WPTR_LSB, true },
300         };
301
302 #ifdef CONFIG_IWLWIFI_DEBUGFS
303         if (buf) {
304                 int pos = 0;
305                 /*
306                  * Register (up to 34 for name + 8 blank/q for MQ): 40 chars
307                  * Colon + space: 2 characters
308                  * 0X%08x: 10 characters
309                  * New line: 1 character
310                  * Total of 53 characters
311                  */
312                 size_t bufsz = ARRAY_SIZE(rfh_tbl) * 53 +
313                                ARRAY_SIZE(rfh_mq_tbl) * 53 * num_q + 40;
314
315                 *buf = kmalloc(bufsz, GFP_KERNEL);
316                 if (!*buf)
317                         return -ENOMEM;
318
319                 pos += scnprintf(*buf + pos, bufsz - pos,
320                                 "RFH register values:\n");
321
322                 for (i = 0; i < ARRAY_SIZE(rfh_tbl); i++)
323                         pos += scnprintf(*buf + pos, bufsz - pos,
324                                 "%40s: 0X%08x\n",
325                                 get_rfh_string(rfh_tbl[i]),
326                                 iwl_read_prph(trans, rfh_tbl[i]));
327
328                 for (i = 0; i < ARRAY_SIZE(rfh_mq_tbl); i++)
329                         for (q = 0; q < num_q; q++) {
330                                 u32 addr = rfh_mq_tbl[i].addr;
331
332                                 addr += q * (rfh_mq_tbl[i].is64 ? 8 : 4);
333                                 pos += scnprintf(*buf + pos, bufsz - pos,
334                                         "%34s(q %2d): 0X%08x\n",
335                                         get_rfh_string(addr), q,
336                                         iwl_read_prph(trans, addr));
337                         }
338
339                 return pos;
340         }
341 #endif
342
343         IWL_ERR(trans, "RFH register values:\n");
344         for (i = 0; i < ARRAY_SIZE(rfh_tbl); i++)
345                 IWL_ERR(trans, "  %34s: 0X%08x\n",
346                         get_rfh_string(rfh_tbl[i]),
347                         iwl_read_prph(trans, rfh_tbl[i]));
348
349         for (i = 0; i < ARRAY_SIZE(rfh_mq_tbl); i++)
350                 for (q = 0; q < num_q; q++) {
351                         u32 addr = rfh_mq_tbl[i].addr;
352
353                         addr += q * (rfh_mq_tbl[i].is64 ? 8 : 4);
354                         IWL_ERR(trans, "  %34s(q %d): 0X%08x\n",
355                                 get_rfh_string(addr), q,
356                                 iwl_read_prph(trans, addr));
357                 }
358
359         return 0;
360 }
361
362 static const char *get_fh_string(int cmd)
363 {
364         switch (cmd) {
365         IWL_CMD(FH_RSCSR_CHNL0_STTS_WPTR_REG);
366         IWL_CMD(FH_RSCSR_CHNL0_RBDCB_BASE_REG);
367         IWL_CMD(FH_RSCSR_CHNL0_WPTR);
368         IWL_CMD(FH_MEM_RCSR_CHNL0_CONFIG_REG);
369         IWL_CMD(FH_MEM_RSSR_SHARED_CTRL_REG);
370         IWL_CMD(FH_MEM_RSSR_RX_STATUS_REG);
371         IWL_CMD(FH_MEM_RSSR_RX_ENABLE_ERR_IRQ2DRV);
372         IWL_CMD(FH_TSSR_TX_STATUS_REG);
373         IWL_CMD(FH_TSSR_TX_ERROR_REG);
374         default:
375                 return "UNKNOWN";
376         }
377 #undef IWL_CMD
378 }
379
380 int iwl_dump_fh(struct iwl_trans *trans, char **buf)
381 {
382         int i;
383         static const u32 fh_tbl[] = {
384                 FH_RSCSR_CHNL0_STTS_WPTR_REG,
385                 FH_RSCSR_CHNL0_RBDCB_BASE_REG,
386                 FH_RSCSR_CHNL0_WPTR,
387                 FH_MEM_RCSR_CHNL0_CONFIG_REG,
388                 FH_MEM_RSSR_SHARED_CTRL_REG,
389                 FH_MEM_RSSR_RX_STATUS_REG,
390                 FH_MEM_RSSR_RX_ENABLE_ERR_IRQ2DRV,
391                 FH_TSSR_TX_STATUS_REG,
392                 FH_TSSR_TX_ERROR_REG
393         };
394
395         if (trans->cfg->mq_rx_supported)
396                 return iwl_dump_rfh(trans, buf);
397
398 #ifdef CONFIG_IWLWIFI_DEBUGFS
399         if (buf) {
400                 int pos = 0;
401                 size_t bufsz = ARRAY_SIZE(fh_tbl) * 48 + 40;
402
403                 *buf = kmalloc(bufsz, GFP_KERNEL);
404                 if (!*buf)
405                         return -ENOMEM;
406
407                 pos += scnprintf(*buf + pos, bufsz - pos,
408                                 "FH register values:\n");
409
410                 for (i = 0; i < ARRAY_SIZE(fh_tbl); i++)
411                         pos += scnprintf(*buf + pos, bufsz - pos,
412                                 "  %34s: 0X%08x\n",
413                                 get_fh_string(fh_tbl[i]),
414                                 iwl_read_direct32(trans, fh_tbl[i]));
415
416                 return pos;
417         }
418 #endif
419
420         IWL_ERR(trans, "FH register values:\n");
421         for (i = 0; i <  ARRAY_SIZE(fh_tbl); i++)
422                 IWL_ERR(trans, "  %34s: 0X%08x\n",
423                         get_fh_string(fh_tbl[i]),
424                         iwl_read_direct32(trans, fh_tbl[i]));
425
426         return 0;
427 }