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