1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * NXP Wireless LAN device driver: SDIO specific handling
4  *
5  * Copyright 2011-2020 NXP
6  */
7 
8 #include <linux/firmware.h>
9 
10 #include "decl.h"
11 #include "ioctl.h"
12 #include "util.h"
13 #include "fw.h"
14 #include "main.h"
15 #include "wmm.h"
16 #include "11n.h"
17 #include "sdio.h"
18 
19 
20 #define SDIO_VERSION	"1.0"
21 
22 static void mwifiex_sdio_work(struct work_struct *work);
23 
24 static struct mwifiex_if_ops sdio_ops;
25 
26 static const struct mwifiex_sdio_card_reg mwifiex_reg_sd87xx = {
27 	.start_rd_port = 1,
28 	.start_wr_port = 1,
29 	.base_0_reg = 0x0040,
30 	.base_1_reg = 0x0041,
31 	.poll_reg = 0x30,
32 	.host_int_enable = UP_LD_HOST_INT_MASK | DN_LD_HOST_INT_MASK,
33 	.host_int_rsr_reg = 0x1,
34 	.host_int_mask_reg = 0x02,
35 	.host_int_status_reg = 0x03,
36 	.status_reg_0 = 0x60,
37 	.status_reg_1 = 0x61,
38 	.sdio_int_mask = 0x3f,
39 	.data_port_mask = 0x0000fffe,
40 	.io_port_0_reg = 0x78,
41 	.io_port_1_reg = 0x79,
42 	.io_port_2_reg = 0x7A,
43 	.max_mp_regs = 64,
44 	.rd_bitmap_l = 0x04,
45 	.rd_bitmap_u = 0x05,
46 	.wr_bitmap_l = 0x06,
47 	.wr_bitmap_u = 0x07,
48 	.rd_len_p0_l = 0x08,
49 	.rd_len_p0_u = 0x09,
50 	.card_misc_cfg_reg = 0x6c,
51 	.func1_dump_reg_start = 0x0,
52 	.func1_dump_reg_end = 0x9,
53 	.func1_scratch_reg = 0x60,
54 	.func1_spec_reg_num = 5,
55 	.func1_spec_reg_table = {0x28, 0x30, 0x34, 0x38, 0x3c},
56 };
57 
58 static const struct mwifiex_sdio_card_reg mwifiex_reg_sd8897 = {
59 	.start_rd_port = 0,
60 	.start_wr_port = 0,
61 	.base_0_reg = 0x60,
62 	.base_1_reg = 0x61,
63 	.poll_reg = 0x50,
64 	.host_int_enable = UP_LD_HOST_INT_MASK | DN_LD_HOST_INT_MASK |
65 			CMD_PORT_UPLD_INT_MASK | CMD_PORT_DNLD_INT_MASK,
66 	.host_int_rsr_reg = 0x1,
67 	.host_int_status_reg = 0x03,
68 	.host_int_mask_reg = 0x02,
69 	.status_reg_0 = 0xc0,
70 	.status_reg_1 = 0xc1,
71 	.sdio_int_mask = 0xff,
72 	.data_port_mask = 0xffffffff,
73 	.io_port_0_reg = 0xD8,
74 	.io_port_1_reg = 0xD9,
75 	.io_port_2_reg = 0xDA,
76 	.max_mp_regs = 184,
77 	.rd_bitmap_l = 0x04,
78 	.rd_bitmap_u = 0x05,
79 	.rd_bitmap_1l = 0x06,
80 	.rd_bitmap_1u = 0x07,
81 	.wr_bitmap_l = 0x08,
82 	.wr_bitmap_u = 0x09,
83 	.wr_bitmap_1l = 0x0a,
84 	.wr_bitmap_1u = 0x0b,
85 	.rd_len_p0_l = 0x0c,
86 	.rd_len_p0_u = 0x0d,
87 	.card_misc_cfg_reg = 0xcc,
88 	.card_cfg_2_1_reg = 0xcd,
89 	.cmd_rd_len_0 = 0xb4,
90 	.cmd_rd_len_1 = 0xb5,
91 	.cmd_rd_len_2 = 0xb6,
92 	.cmd_rd_len_3 = 0xb7,
93 	.cmd_cfg_0 = 0xb8,
94 	.cmd_cfg_1 = 0xb9,
95 	.cmd_cfg_2 = 0xba,
96 	.cmd_cfg_3 = 0xbb,
97 	.fw_dump_host_ready = 0xee,
98 	.fw_dump_ctrl = 0xe2,
99 	.fw_dump_start = 0xe3,
100 	.fw_dump_end = 0xea,
101 	.func1_dump_reg_start = 0x0,
102 	.func1_dump_reg_end = 0xb,
103 	.func1_scratch_reg = 0xc0,
104 	.func1_spec_reg_num = 8,
105 	.func1_spec_reg_table = {0x4C, 0x50, 0x54, 0x55, 0x58,
106 				 0x59, 0x5c, 0x5d},
107 };
108 
109 static const struct mwifiex_sdio_card_reg mwifiex_reg_sd8977 = {
110 	.start_rd_port = 0,
111 	.start_wr_port = 0,
112 	.base_0_reg = 0xF8,
113 	.base_1_reg = 0xF9,
114 	.poll_reg = 0x5C,
115 	.host_int_enable = UP_LD_HOST_INT_MASK | DN_LD_HOST_INT_MASK |
116 		CMD_PORT_UPLD_INT_MASK | CMD_PORT_DNLD_INT_MASK,
117 	.host_int_rsr_reg = 0x4,
118 	.host_int_status_reg = 0x0C,
119 	.host_int_mask_reg = 0x08,
120 	.status_reg_0 = 0xE8,
121 	.status_reg_1 = 0xE9,
122 	.sdio_int_mask = 0xff,
123 	.data_port_mask = 0xffffffff,
124 	.io_port_0_reg = 0xE4,
125 	.io_port_1_reg = 0xE5,
126 	.io_port_2_reg = 0xE6,
127 	.max_mp_regs = 196,
128 	.rd_bitmap_l = 0x10,
129 	.rd_bitmap_u = 0x11,
130 	.rd_bitmap_1l = 0x12,
131 	.rd_bitmap_1u = 0x13,
132 	.wr_bitmap_l = 0x14,
133 	.wr_bitmap_u = 0x15,
134 	.wr_bitmap_1l = 0x16,
135 	.wr_bitmap_1u = 0x17,
136 	.rd_len_p0_l = 0x18,
137 	.rd_len_p0_u = 0x19,
138 	.card_misc_cfg_reg = 0xd8,
139 	.card_cfg_2_1_reg = 0xd9,
140 	.cmd_rd_len_0 = 0xc0,
141 	.cmd_rd_len_1 = 0xc1,
142 	.cmd_rd_len_2 = 0xc2,
143 	.cmd_rd_len_3 = 0xc3,
144 	.cmd_cfg_0 = 0xc4,
145 	.cmd_cfg_1 = 0xc5,
146 	.cmd_cfg_2 = 0xc6,
147 	.cmd_cfg_3 = 0xc7,
148 	.fw_dump_host_ready = 0xcc,
149 	.fw_dump_ctrl = 0xf0,
150 	.fw_dump_start = 0xf1,
151 	.fw_dump_end = 0xf8,
152 	.func1_dump_reg_start = 0x10,
153 	.func1_dump_reg_end = 0x17,
154 	.func1_scratch_reg = 0xe8,
155 	.func1_spec_reg_num = 13,
156 	.func1_spec_reg_table = {0x08, 0x58, 0x5C, 0x5D,
157 				 0x60, 0x61, 0x62, 0x64,
158 				 0x65, 0x66, 0x68, 0x69,
159 				 0x6a},
160 };
161 
162 static const struct mwifiex_sdio_card_reg mwifiex_reg_sd8997 = {
163 	.start_rd_port = 0,
164 	.start_wr_port = 0,
165 	.base_0_reg = 0xF8,
166 	.base_1_reg = 0xF9,
167 	.poll_reg = 0x5C,
168 	.host_int_enable = UP_LD_HOST_INT_MASK | DN_LD_HOST_INT_MASK |
169 			CMD_PORT_UPLD_INT_MASK | CMD_PORT_DNLD_INT_MASK,
170 	.host_int_rsr_reg = 0x4,
171 	.host_int_status_reg = 0x0C,
172 	.host_int_mask_reg = 0x08,
173 	.host_strap_reg = 0xF4,
174 	.host_strap_mask = 0x01,
175 	.host_strap_value = 0x00,
176 	.status_reg_0 = 0xE8,
177 	.status_reg_1 = 0xE9,
178 	.sdio_int_mask = 0xff,
179 	.data_port_mask = 0xffffffff,
180 	.io_port_0_reg = 0xE4,
181 	.io_port_1_reg = 0xE5,
182 	.io_port_2_reg = 0xE6,
183 	.max_mp_regs = 196,
184 	.rd_bitmap_l = 0x10,
185 	.rd_bitmap_u = 0x11,
186 	.rd_bitmap_1l = 0x12,
187 	.rd_bitmap_1u = 0x13,
188 	.wr_bitmap_l = 0x14,
189 	.wr_bitmap_u = 0x15,
190 	.wr_bitmap_1l = 0x16,
191 	.wr_bitmap_1u = 0x17,
192 	.rd_len_p0_l = 0x18,
193 	.rd_len_p0_u = 0x19,
194 	.card_misc_cfg_reg = 0xd8,
195 	.card_cfg_2_1_reg = 0xd9,
196 	.cmd_rd_len_0 = 0xc0,
197 	.cmd_rd_len_1 = 0xc1,
198 	.cmd_rd_len_2 = 0xc2,
199 	.cmd_rd_len_3 = 0xc3,
200 	.cmd_cfg_0 = 0xc4,
201 	.cmd_cfg_1 = 0xc5,
202 	.cmd_cfg_2 = 0xc6,
203 	.cmd_cfg_3 = 0xc7,
204 	.fw_dump_host_ready = 0xcc,
205 	.fw_dump_ctrl = 0xf0,
206 	.fw_dump_start = 0xf1,
207 	.fw_dump_end = 0xf8,
208 	.func1_dump_reg_start = 0x10,
209 	.func1_dump_reg_end = 0x17,
210 	.func1_scratch_reg = 0xe8,
211 	.func1_spec_reg_num = 13,
212 	.func1_spec_reg_table = {0x08, 0x58, 0x5C, 0x5D,
213 				 0x60, 0x61, 0x62, 0x64,
214 				 0x65, 0x66, 0x68, 0x69,
215 				 0x6a},
216 };
217 
218 static const struct mwifiex_sdio_card_reg mwifiex_reg_sd8887 = {
219 	.start_rd_port = 0,
220 	.start_wr_port = 0,
221 	.base_0_reg = 0x6C,
222 	.base_1_reg = 0x6D,
223 	.poll_reg = 0x5C,
224 	.host_int_enable = UP_LD_HOST_INT_MASK | DN_LD_HOST_INT_MASK |
225 			CMD_PORT_UPLD_INT_MASK | CMD_PORT_DNLD_INT_MASK,
226 	.host_int_rsr_reg = 0x4,
227 	.host_int_status_reg = 0x0C,
228 	.host_int_mask_reg = 0x08,
229 	.status_reg_0 = 0x90,
230 	.status_reg_1 = 0x91,
231 	.sdio_int_mask = 0xff,
232 	.data_port_mask = 0xffffffff,
233 	.io_port_0_reg = 0xE4,
234 	.io_port_1_reg = 0xE5,
235 	.io_port_2_reg = 0xE6,
236 	.max_mp_regs = 196,
237 	.rd_bitmap_l = 0x10,
238 	.rd_bitmap_u = 0x11,
239 	.rd_bitmap_1l = 0x12,
240 	.rd_bitmap_1u = 0x13,
241 	.wr_bitmap_l = 0x14,
242 	.wr_bitmap_u = 0x15,
243 	.wr_bitmap_1l = 0x16,
244 	.wr_bitmap_1u = 0x17,
245 	.rd_len_p0_l = 0x18,
246 	.rd_len_p0_u = 0x19,
247 	.card_misc_cfg_reg = 0xd8,
248 	.card_cfg_2_1_reg = 0xd9,
249 	.cmd_rd_len_0 = 0xc0,
250 	.cmd_rd_len_1 = 0xc1,
251 	.cmd_rd_len_2 = 0xc2,
252 	.cmd_rd_len_3 = 0xc3,
253 	.cmd_cfg_0 = 0xc4,
254 	.cmd_cfg_1 = 0xc5,
255 	.cmd_cfg_2 = 0xc6,
256 	.cmd_cfg_3 = 0xc7,
257 	.func1_dump_reg_start = 0x10,
258 	.func1_dump_reg_end = 0x17,
259 	.func1_scratch_reg = 0x90,
260 	.func1_spec_reg_num = 13,
261 	.func1_spec_reg_table = {0x08, 0x58, 0x5C, 0x5D, 0x60,
262 				 0x61, 0x62, 0x64, 0x65, 0x66,
263 				 0x68, 0x69, 0x6a},
264 };
265 
266 static const struct mwifiex_sdio_card_reg mwifiex_reg_sd89xx = {
267 	.start_rd_port = 0,
268 	.start_wr_port = 0,
269 	.base_0_reg = 0xF8,
270 	.base_1_reg = 0xF9,
271 	.poll_reg = 0x5C,
272 	.host_int_enable = UP_LD_HOST_INT_MASK | DN_LD_HOST_INT_MASK |
273 			CMD_PORT_UPLD_INT_MASK | CMD_PORT_DNLD_INT_MASK,
274 	.host_int_rsr_reg = 0x4,
275 	.host_int_status_reg = 0x0C,
276 	.host_int_mask_reg = 0x08,
277 	.host_strap_reg = 0xF4,
278 	.host_strap_mask = 0x01,
279 	.host_strap_value = 0x00,
280 	.status_reg_0 = 0xE8,
281 	.status_reg_1 = 0xE9,
282 	.sdio_int_mask = 0xff,
283 	.data_port_mask = 0xffffffff,
284 	.io_port_0_reg = 0xE4,
285 	.io_port_1_reg = 0xE5,
286 	.io_port_2_reg = 0xE6,
287 	.max_mp_regs = 196,
288 	.rd_bitmap_l = 0x10,
289 	.rd_bitmap_u = 0x11,
290 	.rd_bitmap_1l = 0x12,
291 	.rd_bitmap_1u = 0x13,
292 	.wr_bitmap_l = 0x14,
293 	.wr_bitmap_u = 0x15,
294 	.wr_bitmap_1l = 0x16,
295 	.wr_bitmap_1u = 0x17,
296 	.rd_len_p0_l = 0x18,
297 	.rd_len_p0_u = 0x19,
298 	.card_misc_cfg_reg = 0xd8,
299 	.card_cfg_2_1_reg = 0xd9,
300 	.cmd_rd_len_0 = 0xc0,
301 	.cmd_rd_len_1 = 0xc1,
302 	.cmd_rd_len_2 = 0xc2,
303 	.cmd_rd_len_3 = 0xc3,
304 	.cmd_cfg_0 = 0xc4,
305 	.cmd_cfg_1 = 0xc5,
306 	.cmd_cfg_2 = 0xc6,
307 	.cmd_cfg_3 = 0xc7,
308 	.fw_dump_host_ready = 0xcc,
309 	.fw_dump_ctrl = 0xf9,
310 	.fw_dump_start = 0xf1,
311 	.fw_dump_end = 0xf8,
312 	.func1_dump_reg_start = 0x10,
313 	.func1_dump_reg_end = 0x17,
314 	.func1_scratch_reg = 0xE8,
315 	.func1_spec_reg_num = 13,
316 	.func1_spec_reg_table = {0x08, 0x58, 0x5C, 0x5D, 0x60,
317 				 0x61, 0x62, 0x64, 0x65, 0x66,
318 				 0x68, 0x69, 0x6a},
319 };
320 
321 static const struct mwifiex_sdio_device mwifiex_sdio_sd8786 = {
322 	.firmware = SD8786_DEFAULT_FW_NAME,
323 	.reg = &mwifiex_reg_sd87xx,
324 	.max_ports = 16,
325 	.mp_agg_pkt_limit = 8,
326 	.tx_buf_size = MWIFIEX_TX_DATA_BUF_SIZE_2K,
327 	.mp_tx_agg_buf_size = MWIFIEX_MP_AGGR_BUF_SIZE_16K,
328 	.mp_rx_agg_buf_size = MWIFIEX_MP_AGGR_BUF_SIZE_16K,
329 	.supports_sdio_new_mode = false,
330 	.has_control_mask = true,
331 	.can_dump_fw = false,
332 	.can_auto_tdls = false,
333 	.can_ext_scan = false,
334 };
335 
336 static const struct mwifiex_sdio_device mwifiex_sdio_sd8787 = {
337 	.firmware = SD8787_DEFAULT_FW_NAME,
338 	.reg = &mwifiex_reg_sd87xx,
339 	.max_ports = 16,
340 	.mp_agg_pkt_limit = 8,
341 	.tx_buf_size = MWIFIEX_TX_DATA_BUF_SIZE_2K,
342 	.mp_tx_agg_buf_size = MWIFIEX_MP_AGGR_BUF_SIZE_16K,
343 	.mp_rx_agg_buf_size = MWIFIEX_MP_AGGR_BUF_SIZE_16K,
344 	.supports_sdio_new_mode = false,
345 	.has_control_mask = true,
346 	.can_dump_fw = false,
347 	.can_auto_tdls = false,
348 	.can_ext_scan = true,
349 };
350 
351 static const struct mwifiex_sdio_device mwifiex_sdio_sd8797 = {
352 	.firmware = SD8797_DEFAULT_FW_NAME,
353 	.reg = &mwifiex_reg_sd87xx,
354 	.max_ports = 16,
355 	.mp_agg_pkt_limit = 8,
356 	.tx_buf_size = MWIFIEX_TX_DATA_BUF_SIZE_2K,
357 	.mp_tx_agg_buf_size = MWIFIEX_MP_AGGR_BUF_SIZE_16K,
358 	.mp_rx_agg_buf_size = MWIFIEX_MP_AGGR_BUF_SIZE_16K,
359 	.supports_sdio_new_mode = false,
360 	.has_control_mask = true,
361 	.can_dump_fw = false,
362 	.can_auto_tdls = false,
363 	.can_ext_scan = true,
364 };
365 
366 static const struct mwifiex_sdio_device mwifiex_sdio_sd8897 = {
367 	.firmware = SD8897_DEFAULT_FW_NAME,
368 	.reg = &mwifiex_reg_sd8897,
369 	.max_ports = 32,
370 	.mp_agg_pkt_limit = 16,
371 	.tx_buf_size = MWIFIEX_TX_DATA_BUF_SIZE_4K,
372 	.mp_tx_agg_buf_size = MWIFIEX_MP_AGGR_BUF_SIZE_MAX,
373 	.mp_rx_agg_buf_size = MWIFIEX_MP_AGGR_BUF_SIZE_MAX,
374 	.supports_sdio_new_mode = true,
375 	.has_control_mask = false,
376 	.can_dump_fw = true,
377 	.can_auto_tdls = false,
378 	.can_ext_scan = true,
379 };
380 
381 static const struct mwifiex_sdio_device mwifiex_sdio_sd8977 = {
382 	.firmware = SD8977_DEFAULT_FW_NAME,
383 	.reg = &mwifiex_reg_sd8977,
384 	.max_ports = 32,
385 	.mp_agg_pkt_limit = 16,
386 	.tx_buf_size = MWIFIEX_TX_DATA_BUF_SIZE_4K,
387 	.mp_tx_agg_buf_size = MWIFIEX_MP_AGGR_BUF_SIZE_MAX,
388 	.mp_rx_agg_buf_size = MWIFIEX_MP_AGGR_BUF_SIZE_MAX,
389 	.supports_sdio_new_mode = true,
390 	.has_control_mask = false,
391 	.can_dump_fw = true,
392 	.fw_dump_enh = true,
393 	.can_auto_tdls = false,
394 	.can_ext_scan = true,
395 };
396 
397 static const struct mwifiex_sdio_device mwifiex_sdio_sd8978 = {
398 	.firmware_sdiouart = SD8978_SDIOUART_FW_NAME,
399 	.reg = &mwifiex_reg_sd89xx,
400 	.max_ports = 32,
401 	.mp_agg_pkt_limit = 16,
402 	.tx_buf_size = MWIFIEX_TX_DATA_BUF_SIZE_4K,
403 	.mp_tx_agg_buf_size = MWIFIEX_MP_AGGR_BUF_SIZE_MAX,
404 	.mp_rx_agg_buf_size = MWIFIEX_MP_AGGR_BUF_SIZE_MAX,
405 	.supports_sdio_new_mode = true,
406 	.has_control_mask = false,
407 	.can_dump_fw = true,
408 	.fw_dump_enh = true,
409 	.can_auto_tdls = false,
410 	.can_ext_scan = true,
411 };
412 
413 static const struct mwifiex_sdio_device mwifiex_sdio_sd8997 = {
414 	.firmware = SD8997_DEFAULT_FW_NAME,
415 	.firmware_sdiouart = SD8997_SDIOUART_FW_NAME,
416 	.reg = &mwifiex_reg_sd8997,
417 	.max_ports = 32,
418 	.mp_agg_pkt_limit = 16,
419 	.tx_buf_size = MWIFIEX_TX_DATA_BUF_SIZE_4K,
420 	.mp_tx_agg_buf_size = MWIFIEX_MP_AGGR_BUF_SIZE_MAX,
421 	.mp_rx_agg_buf_size = MWIFIEX_MP_AGGR_BUF_SIZE_MAX,
422 	.supports_sdio_new_mode = true,
423 	.has_control_mask = false,
424 	.can_dump_fw = true,
425 	.fw_dump_enh = true,
426 	.can_auto_tdls = false,
427 	.can_ext_scan = true,
428 };
429 
430 static const struct mwifiex_sdio_device mwifiex_sdio_sd8887 = {
431 	.firmware = SD8887_DEFAULT_FW_NAME,
432 	.reg = &mwifiex_reg_sd8887,
433 	.max_ports = 32,
434 	.mp_agg_pkt_limit = 16,
435 	.tx_buf_size = MWIFIEX_TX_DATA_BUF_SIZE_2K,
436 	.mp_tx_agg_buf_size = MWIFIEX_MP_AGGR_BUF_SIZE_32K,
437 	.mp_rx_agg_buf_size = MWIFIEX_MP_AGGR_BUF_SIZE_32K,
438 	.supports_sdio_new_mode = true,
439 	.has_control_mask = false,
440 	.can_dump_fw = false,
441 	.can_auto_tdls = true,
442 	.can_ext_scan = true,
443 };
444 
445 static const struct mwifiex_sdio_device mwifiex_sdio_sd8987 = {
446 	.firmware = SD8987_DEFAULT_FW_NAME,
447 	.reg = &mwifiex_reg_sd89xx,
448 	.max_ports = 32,
449 	.mp_agg_pkt_limit = 16,
450 	.tx_buf_size = MWIFIEX_TX_DATA_BUF_SIZE_2K,
451 	.mp_tx_agg_buf_size = MWIFIEX_MP_AGGR_BUF_SIZE_MAX,
452 	.mp_rx_agg_buf_size = MWIFIEX_MP_AGGR_BUF_SIZE_MAX,
453 	.supports_sdio_new_mode = true,
454 	.has_control_mask = false,
455 	.can_dump_fw = true,
456 	.fw_dump_enh = true,
457 	.can_auto_tdls = true,
458 	.can_ext_scan = true,
459 };
460 
461 static const struct mwifiex_sdio_device mwifiex_sdio_sd8801 = {
462 	.firmware = SD8801_DEFAULT_FW_NAME,
463 	.reg = &mwifiex_reg_sd87xx,
464 	.max_ports = 16,
465 	.mp_agg_pkt_limit = 8,
466 	.supports_sdio_new_mode = false,
467 	.has_control_mask = true,
468 	.tx_buf_size = MWIFIEX_TX_DATA_BUF_SIZE_2K,
469 	.mp_tx_agg_buf_size = MWIFIEX_MP_AGGR_BUF_SIZE_16K,
470 	.mp_rx_agg_buf_size = MWIFIEX_MP_AGGR_BUF_SIZE_16K,
471 	.can_dump_fw = false,
472 	.can_auto_tdls = false,
473 	.can_ext_scan = true,
474 };
475 
476 static struct memory_type_mapping generic_mem_type_map[] = {
477 	{"DUMP", NULL, 0, 0xDD},
478 };
479 
480 static struct memory_type_mapping mem_type_mapping_tbl[] = {
481 	{"ITCM", NULL, 0, 0xF0},
482 	{"DTCM", NULL, 0, 0xF1},
483 	{"SQRAM", NULL, 0, 0xF2},
484 	{"APU", NULL, 0, 0xF3},
485 	{"CIU", NULL, 0, 0xF4},
486 	{"ICU", NULL, 0, 0xF5},
487 	{"MAC", NULL, 0, 0xF6},
488 	{"EXT7", NULL, 0, 0xF7},
489 	{"EXT8", NULL, 0, 0xF8},
490 	{"EXT9", NULL, 0, 0xF9},
491 	{"EXT10", NULL, 0, 0xFA},
492 	{"EXT11", NULL, 0, 0xFB},
493 	{"EXT12", NULL, 0, 0xFC},
494 	{"EXT13", NULL, 0, 0xFD},
495 	{"EXTLAST", NULL, 0, 0xFE},
496 };
497 
498 static const struct of_device_id mwifiex_sdio_of_match_table[] __maybe_unused = {
499 	{ .compatible = "marvell,sd8787" },
500 	{ .compatible = "marvell,sd8897" },
501 	{ .compatible = "marvell,sd8978" },
502 	{ .compatible = "marvell,sd8997" },
503 	{ .compatible = "nxp,iw416" },
504 	{ }
505 };
506 
507 /* This function parse device tree node using mmc subnode devicetree API.
508  * The device node is saved in card->plt_of_node.
509  * if the device tree node exist and include interrupts attributes, this
510  * function will also request platform specific wakeup interrupt.
511  */
512 static int mwifiex_sdio_probe_of(struct device *dev)
513 {
514 	if (!of_match_node(mwifiex_sdio_of_match_table, dev->of_node)) {
515 		dev_err(dev, "required compatible string missing\n");
516 		return -EINVAL;
517 	}
518 
519 	return 0;
520 }
521 
522 /*
523  * SDIO probe.
524  *
525  * This function probes an mwifiex device and registers it. It allocates
526  * the card structure, enables SDIO function number and initiates the
527  * device registration and initialization procedure by adding a logical
528  * interface.
529  */
530 static int
531 mwifiex_sdio_probe(struct sdio_func *func, const struct sdio_device_id *id)
532 {
533 	int ret;
534 	struct sdio_mmc_card *card = NULL;
535 
536 	pr_debug("info: vendor=0x%4.04X device=0x%4.04X class=%d function=%d\n",
537 		 func->vendor, func->device, func->class, func->num);
538 
539 	card = devm_kzalloc(&func->dev, sizeof(*card), GFP_KERNEL);
540 	if (!card)
541 		return -ENOMEM;
542 
543 	init_completion(&card->fw_done);
544 
545 	card->func = func;
546 
547 	func->card->quirks |= MMC_QUIRK_BLKSZ_FOR_BYTE_MODE;
548 
549 	if (id->driver_data) {
550 		struct mwifiex_sdio_device *data = (void *)id->driver_data;
551 
552 		card->firmware = data->firmware;
553 		card->firmware_sdiouart = data->firmware_sdiouart;
554 		card->reg = data->reg;
555 		card->max_ports = data->max_ports;
556 		card->mp_agg_pkt_limit = data->mp_agg_pkt_limit;
557 		card->supports_sdio_new_mode = data->supports_sdio_new_mode;
558 		card->has_control_mask = data->has_control_mask;
559 		card->tx_buf_size = data->tx_buf_size;
560 		card->mp_tx_agg_buf_size = data->mp_tx_agg_buf_size;
561 		card->mp_rx_agg_buf_size = data->mp_rx_agg_buf_size;
562 		card->can_dump_fw = data->can_dump_fw;
563 		card->fw_dump_enh = data->fw_dump_enh;
564 		card->can_auto_tdls = data->can_auto_tdls;
565 		card->can_ext_scan = data->can_ext_scan;
566 		INIT_WORK(&card->work, mwifiex_sdio_work);
567 	}
568 
569 	sdio_claim_host(func);
570 	ret = sdio_enable_func(func);
571 	sdio_release_host(func);
572 
573 	if (ret) {
574 		dev_err(&func->dev, "failed to enable function\n");
575 		return ret;
576 	}
577 
578 	/* device tree node parsing and platform specific configuration*/
579 	if (func->dev.of_node) {
580 		ret = mwifiex_sdio_probe_of(&func->dev);
581 		if (ret)
582 			goto err_disable;
583 	}
584 
585 	ret = mwifiex_add_card(card, &card->fw_done, &sdio_ops,
586 			       MWIFIEX_SDIO, &func->dev);
587 	if (ret) {
588 		dev_err(&func->dev, "add card failed\n");
589 		goto err_disable;
590 	}
591 
592 	return 0;
593 
594 err_disable:
595 	sdio_claim_host(func);
596 	sdio_disable_func(func);
597 	sdio_release_host(func);
598 
599 	return ret;
600 }
601 
602 /*
603  * SDIO resume.
604  *
605  * Kernel needs to suspend all functions separately. Therefore all
606  * registered functions must have drivers with suspend and resume
607  * methods. Failing that the kernel simply removes the whole card.
608  *
609  * If already not resumed, this function turns on the traffic and
610  * sends a host sleep cancel request to the firmware.
611  */
612 static int mwifiex_sdio_resume(struct device *dev)
613 {
614 	struct sdio_func *func = dev_to_sdio_func(dev);
615 	struct sdio_mmc_card *card;
616 	struct mwifiex_adapter *adapter;
617 
618 	card = sdio_get_drvdata(func);
619 	if (!card || !card->adapter) {
620 		dev_err(dev, "resume: invalid card or adapter\n");
621 		return 0;
622 	}
623 
624 	adapter = card->adapter;
625 
626 	if (!test_bit(MWIFIEX_IS_SUSPENDED, &adapter->work_flags)) {
627 		mwifiex_dbg(adapter, WARN,
628 			    "device already resumed\n");
629 		return 0;
630 	}
631 
632 	clear_bit(MWIFIEX_IS_SUSPENDED, &adapter->work_flags);
633 
634 	/* Disable Host Sleep */
635 	mwifiex_cancel_hs(mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_STA),
636 			  MWIFIEX_SYNC_CMD);
637 
638 	mwifiex_disable_wake(adapter);
639 
640 	return 0;
641 }
642 
643 /* Write data into SDIO card register. Caller claims SDIO device. */
644 static int
645 mwifiex_write_reg_locked(struct sdio_func *func, u32 reg, u8 data)
646 {
647 	int ret = -1;
648 
649 	sdio_writeb(func, data, reg, &ret);
650 	return ret;
651 }
652 
653 /* This function writes data into SDIO card register.
654  */
655 static int
656 mwifiex_write_reg(struct mwifiex_adapter *adapter, u32 reg, u8 data)
657 {
658 	struct sdio_mmc_card *card = adapter->card;
659 	int ret;
660 
661 	sdio_claim_host(card->func);
662 	ret = mwifiex_write_reg_locked(card->func, reg, data);
663 	sdio_release_host(card->func);
664 
665 	return ret;
666 }
667 
668 /* This function reads data from SDIO card register.
669  */
670 static int
671 mwifiex_read_reg(struct mwifiex_adapter *adapter, u32 reg, u8 *data)
672 {
673 	struct sdio_mmc_card *card = adapter->card;
674 	int ret = -1;
675 	u8 val;
676 
677 	sdio_claim_host(card->func);
678 	val = sdio_readb(card->func, reg, &ret);
679 	sdio_release_host(card->func);
680 
681 	*data = val;
682 
683 	return ret;
684 }
685 
686 /* This function writes multiple data into SDIO card memory.
687  *
688  * This does not work in suspended mode.
689  */
690 static int
691 mwifiex_write_data_sync(struct mwifiex_adapter *adapter,
692 			u8 *buffer, u32 pkt_len, u32 port)
693 {
694 	struct sdio_mmc_card *card = adapter->card;
695 	int ret;
696 	u8 blk_mode =
697 		(port & MWIFIEX_SDIO_BYTE_MODE_MASK) ? BYTE_MODE : BLOCK_MODE;
698 	u32 blk_size = (blk_mode == BLOCK_MODE) ? MWIFIEX_SDIO_BLOCK_SIZE : 1;
699 	u32 blk_cnt =
700 		(blk_mode ==
701 		 BLOCK_MODE) ? (pkt_len /
702 				MWIFIEX_SDIO_BLOCK_SIZE) : pkt_len;
703 	u32 ioport = (port & MWIFIEX_SDIO_IO_PORT_MASK);
704 
705 	if (test_bit(MWIFIEX_IS_SUSPENDED, &adapter->work_flags)) {
706 		mwifiex_dbg(adapter, ERROR,
707 			    "%s: not allowed while suspended\n", __func__);
708 		return -1;
709 	}
710 
711 	sdio_claim_host(card->func);
712 
713 	ret = sdio_writesb(card->func, ioport, buffer, blk_cnt * blk_size);
714 
715 	sdio_release_host(card->func);
716 
717 	return ret;
718 }
719 
720 /* This function reads multiple data from SDIO card memory.
721  */
722 static int mwifiex_read_data_sync(struct mwifiex_adapter *adapter, u8 *buffer,
723 				  u32 len, u32 port, u8 claim)
724 {
725 	struct sdio_mmc_card *card = adapter->card;
726 	int ret;
727 	u8 blk_mode = (port & MWIFIEX_SDIO_BYTE_MODE_MASK) ? BYTE_MODE
728 		       : BLOCK_MODE;
729 	u32 blk_size = (blk_mode == BLOCK_MODE) ? MWIFIEX_SDIO_BLOCK_SIZE : 1;
730 	u32 blk_cnt = (blk_mode == BLOCK_MODE) ? (len / MWIFIEX_SDIO_BLOCK_SIZE)
731 			: len;
732 	u32 ioport = (port & MWIFIEX_SDIO_IO_PORT_MASK);
733 
734 	if (claim)
735 		sdio_claim_host(card->func);
736 
737 	ret = sdio_readsb(card->func, buffer, ioport, blk_cnt * blk_size);
738 
739 	if (claim)
740 		sdio_release_host(card->func);
741 
742 	return ret;
743 }
744 
745 /* This function reads the firmware status.
746  */
747 static int
748 mwifiex_sdio_read_fw_status(struct mwifiex_adapter *adapter, u16 *dat)
749 {
750 	struct sdio_mmc_card *card = adapter->card;
751 	const struct mwifiex_sdio_card_reg *reg = card->reg;
752 	u8 fws0, fws1;
753 
754 	if (mwifiex_read_reg(adapter, reg->status_reg_0, &fws0))
755 		return -1;
756 
757 	if (mwifiex_read_reg(adapter, reg->status_reg_1, &fws1))
758 		return -1;
759 
760 	*dat = (u16)((fws1 << 8) | fws0);
761 	return 0;
762 }
763 
764 /* This function checks the firmware status in card.
765  */
766 static int mwifiex_check_fw_status(struct mwifiex_adapter *adapter,
767 				   u32 poll_num)
768 {
769 	int ret = 0;
770 	u16 firmware_stat;
771 	u32 tries;
772 
773 	for (tries = 0; tries < poll_num; tries++) {
774 		ret = mwifiex_sdio_read_fw_status(adapter, &firmware_stat);
775 		if (ret)
776 			continue;
777 		if (firmware_stat == FIRMWARE_READY_SDIO) {
778 			ret = 0;
779 			break;
780 		}
781 
782 		msleep(100);
783 		ret = -1;
784 	}
785 
786 	return ret;
787 }
788 
789 /* This function checks if WLAN is the winner.
790  */
791 static int mwifiex_check_winner_status(struct mwifiex_adapter *adapter)
792 {
793 	int ret = 0;
794 	u8 winner = 0;
795 	struct sdio_mmc_card *card = adapter->card;
796 
797 	if (mwifiex_read_reg(adapter, card->reg->status_reg_0, &winner))
798 		return -1;
799 
800 	if (winner)
801 		adapter->winner = 0;
802 	else
803 		adapter->winner = 1;
804 
805 	return ret;
806 }
807 
808 /*
809  * SDIO remove.
810  *
811  * This function removes the interface and frees up the card structure.
812  */
813 static void
814 mwifiex_sdio_remove(struct sdio_func *func)
815 {
816 	struct sdio_mmc_card *card;
817 	struct mwifiex_adapter *adapter;
818 	struct mwifiex_private *priv;
819 	int ret = 0;
820 	u16 firmware_stat;
821 
822 	card = sdio_get_drvdata(func);
823 	if (!card)
824 		return;
825 
826 	wait_for_completion(&card->fw_done);
827 
828 	adapter = card->adapter;
829 	if (!adapter || !adapter->priv_num)
830 		return;
831 
832 	mwifiex_dbg(adapter, INFO, "info: SDIO func num=%d\n", func->num);
833 
834 	ret = mwifiex_sdio_read_fw_status(adapter, &firmware_stat);
835 	if (!ret && firmware_stat == FIRMWARE_READY_SDIO &&
836 	    !adapter->mfg_mode) {
837 		mwifiex_deauthenticate_all(adapter);
838 
839 		priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
840 		mwifiex_disable_auto_ds(priv);
841 		mwifiex_init_shutdown_fw(priv, MWIFIEX_FUNC_SHUTDOWN);
842 	}
843 
844 	mwifiex_remove_card(adapter);
845 }
846 
847 /*
848  * SDIO suspend.
849  *
850  * Kernel needs to suspend all functions separately. Therefore all
851  * registered functions must have drivers with suspend and resume
852  * methods. Failing that the kernel simply removes the whole card.
853  *
854  * If already not suspended, this function allocates and sends a host
855  * sleep activate request to the firmware and turns off the traffic.
856  */
857 static int mwifiex_sdio_suspend(struct device *dev)
858 {
859 	struct sdio_func *func = dev_to_sdio_func(dev);
860 	struct sdio_mmc_card *card;
861 	struct mwifiex_adapter *adapter;
862 	mmc_pm_flag_t pm_flag = 0;
863 	int ret = 0;
864 
865 	pm_flag = sdio_get_host_pm_caps(func);
866 	pr_debug("cmd: %s: suspend: PM flag = 0x%x\n",
867 		 sdio_func_id(func), pm_flag);
868 	if (!(pm_flag & MMC_PM_KEEP_POWER)) {
869 		dev_err(dev, "%s: cannot remain alive while host is"
870 			" suspended\n", sdio_func_id(func));
871 		return -ENOSYS;
872 	}
873 
874 	card = sdio_get_drvdata(func);
875 	if (!card) {
876 		dev_err(dev, "suspend: invalid card\n");
877 		return 0;
878 	}
879 
880 	/* Might still be loading firmware */
881 	wait_for_completion(&card->fw_done);
882 
883 	adapter = card->adapter;
884 	if (!adapter) {
885 		dev_err(dev, "adapter is not valid\n");
886 		return 0;
887 	}
888 
889 	if (!adapter->is_up)
890 		return -EBUSY;
891 
892 	mwifiex_enable_wake(adapter);
893 
894 	/* Enable the Host Sleep */
895 	if (!mwifiex_enable_hs(adapter)) {
896 		mwifiex_dbg(adapter, ERROR,
897 			    "cmd: failed to suspend\n");
898 		clear_bit(MWIFIEX_IS_HS_ENABLING, &adapter->work_flags);
899 		mwifiex_disable_wake(adapter);
900 		return -EFAULT;
901 	}
902 
903 	mwifiex_dbg(adapter, INFO,
904 		    "cmd: suspend with MMC_PM_KEEP_POWER\n");
905 	ret = sdio_set_host_pm_flags(func, MMC_PM_KEEP_POWER);
906 
907 	/* Indicate device suspended */
908 	set_bit(MWIFIEX_IS_SUSPENDED, &adapter->work_flags);
909 	clear_bit(MWIFIEX_IS_HS_ENABLING, &adapter->work_flags);
910 
911 	return ret;
912 }
913 
914 static void mwifiex_sdio_coredump(struct device *dev)
915 {
916 	struct sdio_func *func = dev_to_sdio_func(dev);
917 	struct sdio_mmc_card *card;
918 
919 	card = sdio_get_drvdata(func);
920 	if (!test_and_set_bit(MWIFIEX_IFACE_WORK_DEVICE_DUMP,
921 			      &card->work_flags))
922 		schedule_work(&card->work);
923 }
924 
925 /* WLAN IDs */
926 static const struct sdio_device_id mwifiex_ids[] = {
927 	{SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8786_WLAN),
928 		.driver_data = (unsigned long) &mwifiex_sdio_sd8786},
929 	{SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8787_WLAN),
930 		.driver_data = (unsigned long) &mwifiex_sdio_sd8787},
931 	{SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8797_WLAN),
932 		.driver_data = (unsigned long) &mwifiex_sdio_sd8797},
933 	{SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8897_WLAN),
934 		.driver_data = (unsigned long) &mwifiex_sdio_sd8897},
935 	{SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8887_WLAN),
936 		.driver_data = (unsigned long)&mwifiex_sdio_sd8887},
937 	{SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8801_WLAN),
938 		.driver_data = (unsigned long)&mwifiex_sdio_sd8801},
939 	{SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8977_WLAN),
940 		.driver_data = (unsigned long)&mwifiex_sdio_sd8977},
941 	{SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8978_WLAN),
942 		.driver_data = (unsigned long)&mwifiex_sdio_sd8978},
943 	{SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8987_WLAN),
944 		.driver_data = (unsigned long)&mwifiex_sdio_sd8987},
945 	{SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8997_WLAN),
946 		.driver_data = (unsigned long)&mwifiex_sdio_sd8997},
947 	{},
948 };
949 
950 MODULE_DEVICE_TABLE(sdio, mwifiex_ids);
951 
952 static const struct dev_pm_ops mwifiex_sdio_pm_ops = {
953 	.suspend = mwifiex_sdio_suspend,
954 	.resume = mwifiex_sdio_resume,
955 };
956 
957 static struct sdio_driver mwifiex_sdio = {
958 	.name = "mwifiex_sdio",
959 	.id_table = mwifiex_ids,
960 	.probe = mwifiex_sdio_probe,
961 	.remove = mwifiex_sdio_remove,
962 	.drv = {
963 		.owner = THIS_MODULE,
964 		.coredump = mwifiex_sdio_coredump,
965 		.pm = &mwifiex_sdio_pm_ops,
966 	}
967 };
968 
969 /*
970  * This function wakes up the card.
971  *
972  * A host power up command is written to the card configuration
973  * register to wake up the card.
974  */
975 static int mwifiex_pm_wakeup_card(struct mwifiex_adapter *adapter)
976 {
977 	mwifiex_dbg(adapter, EVENT,
978 		    "event: wakeup device...\n");
979 
980 	return mwifiex_write_reg(adapter, CONFIGURATION_REG, HOST_POWER_UP);
981 }
982 
983 /*
984  * This function is called after the card has woken up.
985  *
986  * The card configuration register is reset.
987  */
988 static int mwifiex_pm_wakeup_card_complete(struct mwifiex_adapter *adapter)
989 {
990 	mwifiex_dbg(adapter, EVENT,
991 		    "cmd: wakeup device completed\n");
992 
993 	return mwifiex_write_reg(adapter, CONFIGURATION_REG, 0);
994 }
995 
996 static int mwifiex_sdio_dnld_fw(struct mwifiex_adapter *adapter,
997 			struct mwifiex_fw_image *fw)
998 {
999 	struct sdio_mmc_card *card = adapter->card;
1000 	int ret;
1001 
1002 	sdio_claim_host(card->func);
1003 	ret = mwifiex_dnld_fw(adapter, fw);
1004 	sdio_release_host(card->func);
1005 
1006 	return ret;
1007 }
1008 
1009 /*
1010  * This function is used to initialize IO ports for the
1011  * chipsets supporting SDIO new mode eg SD8897.
1012  */
1013 static int mwifiex_init_sdio_new_mode(struct mwifiex_adapter *adapter)
1014 {
1015 	u8 reg;
1016 	struct sdio_mmc_card *card = adapter->card;
1017 
1018 	adapter->ioport = MEM_PORT;
1019 
1020 	/* enable sdio new mode */
1021 	if (mwifiex_read_reg(adapter, card->reg->card_cfg_2_1_reg, &reg))
1022 		return -1;
1023 	if (mwifiex_write_reg(adapter, card->reg->card_cfg_2_1_reg,
1024 			      reg | CMD53_NEW_MODE))
1025 		return -1;
1026 
1027 	/* Configure cmd port and enable reading rx length from the register */
1028 	if (mwifiex_read_reg(adapter, card->reg->cmd_cfg_0, &reg))
1029 		return -1;
1030 	if (mwifiex_write_reg(adapter, card->reg->cmd_cfg_0,
1031 			      reg | CMD_PORT_RD_LEN_EN))
1032 		return -1;
1033 
1034 	/* Enable Dnld/Upld ready auto reset for cmd port after cmd53 is
1035 	 * completed
1036 	 */
1037 	if (mwifiex_read_reg(adapter, card->reg->cmd_cfg_1, &reg))
1038 		return -1;
1039 	if (mwifiex_write_reg(adapter, card->reg->cmd_cfg_1,
1040 			      reg | CMD_PORT_AUTO_EN))
1041 		return -1;
1042 
1043 	return 0;
1044 }
1045 
1046 /* This function initializes the IO ports.
1047  *
1048  * The following operations are performed -
1049  *      - Read the IO ports (0, 1 and 2)
1050  *      - Set host interrupt Reset-To-Read to clear
1051  *      - Set auto re-enable interrupt
1052  */
1053 static int mwifiex_init_sdio_ioport(struct mwifiex_adapter *adapter)
1054 {
1055 	u8 reg;
1056 	struct sdio_mmc_card *card = adapter->card;
1057 
1058 	adapter->ioport = 0;
1059 
1060 	if (card->supports_sdio_new_mode) {
1061 		if (mwifiex_init_sdio_new_mode(adapter))
1062 			return -1;
1063 		goto cont;
1064 	}
1065 
1066 	/* Read the IO port */
1067 	if (!mwifiex_read_reg(adapter, card->reg->io_port_0_reg, &reg))
1068 		adapter->ioport |= (reg & 0xff);
1069 	else
1070 		return -1;
1071 
1072 	if (!mwifiex_read_reg(adapter, card->reg->io_port_1_reg, &reg))
1073 		adapter->ioport |= ((reg & 0xff) << 8);
1074 	else
1075 		return -1;
1076 
1077 	if (!mwifiex_read_reg(adapter, card->reg->io_port_2_reg, &reg))
1078 		adapter->ioport |= ((reg & 0xff) << 16);
1079 	else
1080 		return -1;
1081 cont:
1082 	mwifiex_dbg(adapter, INFO,
1083 		    "info: SDIO FUNC1 IO port: %#x\n", adapter->ioport);
1084 
1085 	/* Set Host interrupt reset to read to clear */
1086 	if (!mwifiex_read_reg(adapter, card->reg->host_int_rsr_reg, &reg))
1087 		mwifiex_write_reg(adapter, card->reg->host_int_rsr_reg,
1088 				  reg | card->reg->sdio_int_mask);
1089 	else
1090 		return -1;
1091 
1092 	/* Dnld/Upld ready set to auto reset */
1093 	if (!mwifiex_read_reg(adapter, card->reg->card_misc_cfg_reg, &reg))
1094 		mwifiex_write_reg(adapter, card->reg->card_misc_cfg_reg,
1095 				  reg | AUTO_RE_ENABLE_INT);
1096 	else
1097 		return -1;
1098 
1099 	return 0;
1100 }
1101 
1102 /*
1103  * This function sends data to the card.
1104  */
1105 static int mwifiex_write_data_to_card(struct mwifiex_adapter *adapter,
1106 				      u8 *payload, u32 pkt_len, u32 port)
1107 {
1108 	u32 i = 0;
1109 	int ret;
1110 
1111 	do {
1112 		ret = mwifiex_write_data_sync(adapter, payload, pkt_len, port);
1113 		if (ret) {
1114 			i++;
1115 			mwifiex_dbg(adapter, ERROR,
1116 				    "host_to_card, write iomem\t"
1117 				    "(%d) failed: %d\n", i, ret);
1118 			if (mwifiex_write_reg(adapter, CONFIGURATION_REG, 0x04))
1119 				mwifiex_dbg(adapter, ERROR,
1120 					    "write CFG reg failed\n");
1121 
1122 			ret = -1;
1123 			if (i > MAX_WRITE_IOMEM_RETRY)
1124 				return ret;
1125 		}
1126 	} while (ret == -1);
1127 
1128 	return ret;
1129 }
1130 
1131 /*
1132  * This function gets the read port.
1133  *
1134  * If control port bit is set in MP read bitmap, the control port
1135  * is returned, otherwise the current read port is returned and
1136  * the value is increased (provided it does not reach the maximum
1137  * limit, in which case it is reset to 1)
1138  */
1139 static int mwifiex_get_rd_port(struct mwifiex_adapter *adapter, u8 *port)
1140 {
1141 	struct sdio_mmc_card *card = adapter->card;
1142 	const struct mwifiex_sdio_card_reg *reg = card->reg;
1143 	u32 rd_bitmap = card->mp_rd_bitmap;
1144 
1145 	mwifiex_dbg(adapter, DATA,
1146 		    "data: mp_rd_bitmap=0x%08x\n", rd_bitmap);
1147 
1148 	if (card->supports_sdio_new_mode) {
1149 		if (!(rd_bitmap & reg->data_port_mask))
1150 			return -1;
1151 	} else {
1152 		if (!(rd_bitmap & (CTRL_PORT_MASK | reg->data_port_mask)))
1153 			return -1;
1154 	}
1155 
1156 	if ((card->has_control_mask) &&
1157 	    (card->mp_rd_bitmap & CTRL_PORT_MASK)) {
1158 		card->mp_rd_bitmap &= (u32) (~CTRL_PORT_MASK);
1159 		*port = CTRL_PORT;
1160 		mwifiex_dbg(adapter, DATA,
1161 			    "data: port=%d mp_rd_bitmap=0x%08x\n",
1162 			    *port, card->mp_rd_bitmap);
1163 		return 0;
1164 	}
1165 
1166 	if (!(card->mp_rd_bitmap & (1 << card->curr_rd_port)))
1167 		return -1;
1168 
1169 	/* We are now handling the SDIO data ports */
1170 	card->mp_rd_bitmap &= (u32)(~(1 << card->curr_rd_port));
1171 	*port = card->curr_rd_port;
1172 
1173 	if (++card->curr_rd_port == card->max_ports)
1174 		card->curr_rd_port = reg->start_rd_port;
1175 
1176 	mwifiex_dbg(adapter, DATA,
1177 		    "data: port=%d mp_rd_bitmap=0x%08x -> 0x%08x\n",
1178 		    *port, rd_bitmap, card->mp_rd_bitmap);
1179 
1180 	return 0;
1181 }
1182 
1183 /*
1184  * This function gets the write port for data.
1185  *
1186  * The current write port is returned if available and the value is
1187  * increased (provided it does not reach the maximum limit, in which
1188  * case it is reset to 1)
1189  */
1190 static int mwifiex_get_wr_port_data(struct mwifiex_adapter *adapter, u32 *port)
1191 {
1192 	struct sdio_mmc_card *card = adapter->card;
1193 	const struct mwifiex_sdio_card_reg *reg = card->reg;
1194 	u32 wr_bitmap = card->mp_wr_bitmap;
1195 
1196 	mwifiex_dbg(adapter, DATA,
1197 		    "data: mp_wr_bitmap=0x%08x\n", wr_bitmap);
1198 
1199 	if (!(wr_bitmap & card->mp_data_port_mask)) {
1200 		adapter->data_sent = true;
1201 		return -EBUSY;
1202 	}
1203 
1204 	if (card->mp_wr_bitmap & (1 << card->curr_wr_port)) {
1205 		card->mp_wr_bitmap &= (u32) (~(1 << card->curr_wr_port));
1206 		*port = card->curr_wr_port;
1207 		if (++card->curr_wr_port == card->mp_end_port)
1208 			card->curr_wr_port = reg->start_wr_port;
1209 	} else {
1210 		adapter->data_sent = true;
1211 		return -EBUSY;
1212 	}
1213 
1214 	if ((card->has_control_mask) && (*port == CTRL_PORT)) {
1215 		mwifiex_dbg(adapter, ERROR,
1216 			    "invalid data port=%d cur port=%d mp_wr_bitmap=0x%08x -> 0x%08x\n",
1217 			    *port, card->curr_wr_port, wr_bitmap,
1218 			    card->mp_wr_bitmap);
1219 		return -1;
1220 	}
1221 
1222 	mwifiex_dbg(adapter, DATA,
1223 		    "data: port=%d mp_wr_bitmap=0x%08x -> 0x%08x\n",
1224 		    *port, wr_bitmap, card->mp_wr_bitmap);
1225 
1226 	return 0;
1227 }
1228 
1229 /*
1230  * This function polls the card status.
1231  */
1232 static int
1233 mwifiex_sdio_poll_card_status(struct mwifiex_adapter *adapter, u8 bits)
1234 {
1235 	struct sdio_mmc_card *card = adapter->card;
1236 	u32 tries;
1237 	u8 cs;
1238 
1239 	for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
1240 		if (mwifiex_read_reg(adapter, card->reg->poll_reg, &cs))
1241 			break;
1242 		else if ((cs & bits) == bits)
1243 			return 0;
1244 
1245 		usleep_range(10, 20);
1246 	}
1247 
1248 	mwifiex_dbg(adapter, ERROR,
1249 		    "poll card status failed, tries = %d\n", tries);
1250 
1251 	return -1;
1252 }
1253 
1254 /*
1255  * This function disables the host interrupt.
1256  *
1257  * The host interrupt mask is read, the disable bit is reset and
1258  * written back to the card host interrupt mask register.
1259  */
1260 static void mwifiex_sdio_disable_host_int(struct mwifiex_adapter *adapter)
1261 {
1262 	struct sdio_mmc_card *card = adapter->card;
1263 	struct sdio_func *func = card->func;
1264 
1265 	sdio_claim_host(func);
1266 	mwifiex_write_reg_locked(func, card->reg->host_int_mask_reg, 0);
1267 	sdio_release_irq(func);
1268 	sdio_release_host(func);
1269 }
1270 
1271 /*
1272  * This function reads the interrupt status from card.
1273  */
1274 static void mwifiex_interrupt_status(struct mwifiex_adapter *adapter)
1275 {
1276 	struct sdio_mmc_card *card = adapter->card;
1277 	u8 sdio_ireg;
1278 	unsigned long flags;
1279 
1280 	if (mwifiex_read_data_sync(adapter, card->mp_regs,
1281 				   card->reg->max_mp_regs,
1282 				   REG_PORT | MWIFIEX_SDIO_BYTE_MODE_MASK, 0)) {
1283 		mwifiex_dbg(adapter, ERROR, "read mp_regs failed\n");
1284 		return;
1285 	}
1286 
1287 	sdio_ireg = card->mp_regs[card->reg->host_int_status_reg];
1288 	if (sdio_ireg) {
1289 		/*
1290 		 * DN_LD_HOST_INT_STATUS and/or UP_LD_HOST_INT_STATUS
1291 		 * For SDIO new mode CMD port interrupts
1292 		 *	DN_LD_CMD_PORT_HOST_INT_STATUS and/or
1293 		 *	UP_LD_CMD_PORT_HOST_INT_STATUS
1294 		 * Clear the interrupt status register
1295 		 */
1296 		mwifiex_dbg(adapter, INTR,
1297 			    "int: sdio_ireg = %#x\n", sdio_ireg);
1298 		spin_lock_irqsave(&adapter->int_lock, flags);
1299 		adapter->int_status |= sdio_ireg;
1300 		spin_unlock_irqrestore(&adapter->int_lock, flags);
1301 	}
1302 }
1303 
1304 /*
1305  * SDIO interrupt handler.
1306  *
1307  * This function reads the interrupt status from firmware and handles
1308  * the interrupt in current thread (ksdioirqd) right away.
1309  */
1310 static void
1311 mwifiex_sdio_interrupt(struct sdio_func *func)
1312 {
1313 	struct mwifiex_adapter *adapter;
1314 	struct sdio_mmc_card *card;
1315 
1316 	card = sdio_get_drvdata(func);
1317 	if (!card || !card->adapter) {
1318 		pr_err("int: func=%p card=%p adapter=%p\n",
1319 		       func, card, card ? card->adapter : NULL);
1320 		return;
1321 	}
1322 	adapter = card->adapter;
1323 
1324 	if (!adapter->pps_uapsd_mode && adapter->ps_state == PS_STATE_SLEEP)
1325 		adapter->ps_state = PS_STATE_AWAKE;
1326 
1327 	mwifiex_interrupt_status(adapter);
1328 	mwifiex_main_process(adapter);
1329 }
1330 
1331 /*
1332  * This function enables the host interrupt.
1333  *
1334  * The host interrupt enable mask is written to the card
1335  * host interrupt mask register.
1336  */
1337 static int mwifiex_sdio_enable_host_int(struct mwifiex_adapter *adapter)
1338 {
1339 	struct sdio_mmc_card *card = adapter->card;
1340 	struct sdio_func *func = card->func;
1341 	int ret;
1342 
1343 	sdio_claim_host(func);
1344 
1345 	/* Request the SDIO IRQ */
1346 	ret = sdio_claim_irq(func, mwifiex_sdio_interrupt);
1347 	if (ret) {
1348 		mwifiex_dbg(adapter, ERROR,
1349 			    "claim irq failed: ret=%d\n", ret);
1350 		goto out;
1351 	}
1352 
1353 	/* Simply write the mask to the register */
1354 	ret = mwifiex_write_reg_locked(func, card->reg->host_int_mask_reg,
1355 				       card->reg->host_int_enable);
1356 	if (ret) {
1357 		mwifiex_dbg(adapter, ERROR,
1358 			    "enable host interrupt failed\n");
1359 		sdio_release_irq(func);
1360 	}
1361 
1362 out:
1363 	sdio_release_host(func);
1364 	return ret;
1365 }
1366 
1367 /*
1368  * This function sends a data buffer to the card.
1369  */
1370 static int mwifiex_sdio_card_to_host(struct mwifiex_adapter *adapter,
1371 				     u32 *type, u8 *buffer,
1372 				     u32 npayload, u32 ioport)
1373 {
1374 	int ret;
1375 	u32 nb;
1376 
1377 	if (!buffer) {
1378 		mwifiex_dbg(adapter, ERROR,
1379 			    "%s: buffer is NULL\n", __func__);
1380 		return -1;
1381 	}
1382 
1383 	ret = mwifiex_read_data_sync(adapter, buffer, npayload, ioport, 1);
1384 
1385 	if (ret) {
1386 		mwifiex_dbg(adapter, ERROR,
1387 			    "%s: read iomem failed: %d\n", __func__,
1388 			ret);
1389 		return -1;
1390 	}
1391 
1392 	nb = get_unaligned_le16((buffer));
1393 	if (nb > npayload) {
1394 		mwifiex_dbg(adapter, ERROR,
1395 			    "%s: invalid packet, nb=%d npayload=%d\n",
1396 			    __func__, nb, npayload);
1397 		return -1;
1398 	}
1399 
1400 	*type = get_unaligned_le16((buffer + 2));
1401 
1402 	return ret;
1403 }
1404 
1405 /*
1406  * This function downloads the firmware to the card.
1407  *
1408  * Firmware is downloaded to the card in blocks. Every block download
1409  * is tested for CRC errors, and retried a number of times before
1410  * returning failure.
1411  */
1412 static int mwifiex_prog_fw_w_helper(struct mwifiex_adapter *adapter,
1413 				    struct mwifiex_fw_image *fw)
1414 {
1415 	struct sdio_mmc_card *card = adapter->card;
1416 	const struct mwifiex_sdio_card_reg *reg = card->reg;
1417 	int ret;
1418 	u8 *firmware = fw->fw_buf;
1419 	u32 firmware_len = fw->fw_len;
1420 	u32 offset = 0;
1421 	u8 base0, base1;
1422 	u8 *fwbuf;
1423 	u16 len = 0;
1424 	u32 txlen, tx_blocks = 0, tries;
1425 	u32 i = 0;
1426 
1427 	if (!firmware_len) {
1428 		mwifiex_dbg(adapter, ERROR,
1429 			    "firmware image not found! Terminating download\n");
1430 		return -1;
1431 	}
1432 
1433 	mwifiex_dbg(adapter, INFO,
1434 		    "info: downloading FW image (%d bytes)\n",
1435 		    firmware_len);
1436 
1437 	/* Assume that the allocated buffer is 8-byte aligned */
1438 	fwbuf = kzalloc(MWIFIEX_UPLD_SIZE, GFP_KERNEL);
1439 	if (!fwbuf)
1440 		return -ENOMEM;
1441 
1442 	sdio_claim_host(card->func);
1443 
1444 	/* Perform firmware data transfer */
1445 	do {
1446 		/* The host polls for the DN_LD_CARD_RDY and CARD_IO_READY
1447 		   bits */
1448 		ret = mwifiex_sdio_poll_card_status(adapter, CARD_IO_READY |
1449 						    DN_LD_CARD_RDY);
1450 		if (ret) {
1451 			mwifiex_dbg(adapter, ERROR,
1452 				    "FW download with helper:\t"
1453 				    "poll status timeout @ %d\n", offset);
1454 			goto done;
1455 		}
1456 
1457 		/* More data? */
1458 		if (offset >= firmware_len)
1459 			break;
1460 
1461 		for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
1462 			ret = mwifiex_read_reg(adapter, reg->base_0_reg,
1463 					       &base0);
1464 			if (ret) {
1465 				mwifiex_dbg(adapter, ERROR,
1466 					    "dev BASE0 register read failed:\t"
1467 					    "base0=%#04X(%d). Terminating dnld\n",
1468 					    base0, base0);
1469 				goto done;
1470 			}
1471 			ret = mwifiex_read_reg(adapter, reg->base_1_reg,
1472 					       &base1);
1473 			if (ret) {
1474 				mwifiex_dbg(adapter, ERROR,
1475 					    "dev BASE1 register read failed:\t"
1476 					    "base1=%#04X(%d). Terminating dnld\n",
1477 					    base1, base1);
1478 				goto done;
1479 			}
1480 			len = (u16) (((base1 & 0xff) << 8) | (base0 & 0xff));
1481 
1482 			if (len)
1483 				break;
1484 
1485 			usleep_range(10, 20);
1486 		}
1487 
1488 		if (!len) {
1489 			break;
1490 		} else if (len > MWIFIEX_UPLD_SIZE) {
1491 			mwifiex_dbg(adapter, ERROR,
1492 				    "FW dnld failed @ %d, invalid length %d\n",
1493 				    offset, len);
1494 			ret = -1;
1495 			goto done;
1496 		}
1497 
1498 		txlen = len;
1499 
1500 		if (len & BIT(0)) {
1501 			i++;
1502 			if (i > MAX_WRITE_IOMEM_RETRY) {
1503 				mwifiex_dbg(adapter, ERROR,
1504 					    "FW dnld failed @ %d, over max retry\n",
1505 					    offset);
1506 				ret = -1;
1507 				goto done;
1508 			}
1509 			mwifiex_dbg(adapter, ERROR,
1510 				    "CRC indicated by the helper:\t"
1511 				    "len = 0x%04X, txlen = %d\n", len, txlen);
1512 			len &= ~BIT(0);
1513 			/* Setting this to 0 to resend from same offset */
1514 			txlen = 0;
1515 		} else {
1516 			i = 0;
1517 
1518 			/* Set blocksize to transfer - checking for last
1519 			   block */
1520 			if (firmware_len - offset < txlen)
1521 				txlen = firmware_len - offset;
1522 
1523 			tx_blocks = (txlen + MWIFIEX_SDIO_BLOCK_SIZE - 1)
1524 				    / MWIFIEX_SDIO_BLOCK_SIZE;
1525 
1526 			/* Copy payload to buffer */
1527 			memmove(fwbuf, &firmware[offset], txlen);
1528 		}
1529 
1530 		ret = mwifiex_write_data_sync(adapter, fwbuf, tx_blocks *
1531 					      MWIFIEX_SDIO_BLOCK_SIZE,
1532 					      adapter->ioport);
1533 		if (ret) {
1534 			mwifiex_dbg(adapter, ERROR,
1535 				    "FW download, write iomem (%d) failed @ %d\n",
1536 				    i, offset);
1537 			if (mwifiex_write_reg(adapter, CONFIGURATION_REG, 0x04))
1538 				mwifiex_dbg(adapter, ERROR,
1539 					    "write CFG reg failed\n");
1540 
1541 			ret = -1;
1542 			goto done;
1543 		}
1544 
1545 		offset += txlen;
1546 	} while (true);
1547 
1548 	mwifiex_dbg(adapter, MSG,
1549 		    "info: FW download over, size %d bytes\n", offset);
1550 
1551 	ret = 0;
1552 done:
1553 	sdio_release_host(card->func);
1554 	kfree(fwbuf);
1555 	return ret;
1556 }
1557 
1558 /*
1559  * This function decode sdio aggreation pkt.
1560  *
1561  * Based on the data block size and pkt_len,
1562  * skb data will be decoded to few packets.
1563  */
1564 static void mwifiex_deaggr_sdio_pkt(struct mwifiex_adapter *adapter,
1565 				    struct sk_buff *skb)
1566 {
1567 	u32 total_pkt_len, pkt_len;
1568 	struct sk_buff *skb_deaggr;
1569 	u16 blk_size;
1570 	u8 blk_num;
1571 	u8 *data;
1572 
1573 	data = skb->data;
1574 	total_pkt_len = skb->len;
1575 
1576 	while (total_pkt_len >= (SDIO_HEADER_OFFSET + adapter->intf_hdr_len)) {
1577 		if (total_pkt_len < adapter->sdio_rx_block_size)
1578 			break;
1579 		blk_num = *(data + BLOCK_NUMBER_OFFSET);
1580 		blk_size = adapter->sdio_rx_block_size * blk_num;
1581 		if (blk_size > total_pkt_len) {
1582 			mwifiex_dbg(adapter, ERROR,
1583 				    "%s: error in blk_size,\t"
1584 				    "blk_num=%d, blk_size=%d, total_pkt_len=%d\n",
1585 				    __func__, blk_num, blk_size, total_pkt_len);
1586 			break;
1587 		}
1588 		pkt_len = get_unaligned_le16((data +
1589 					     SDIO_HEADER_OFFSET));
1590 		if ((pkt_len + SDIO_HEADER_OFFSET) > blk_size) {
1591 			mwifiex_dbg(adapter, ERROR,
1592 				    "%s: error in pkt_len,\t"
1593 				    "pkt_len=%d, blk_size=%d\n",
1594 				    __func__, pkt_len, blk_size);
1595 			break;
1596 		}
1597 
1598 		skb_deaggr = mwifiex_alloc_dma_align_buf(pkt_len, GFP_KERNEL);
1599 		if (!skb_deaggr)
1600 			break;
1601 		skb_put(skb_deaggr, pkt_len);
1602 		memcpy(skb_deaggr->data, data + SDIO_HEADER_OFFSET, pkt_len);
1603 		skb_pull(skb_deaggr, adapter->intf_hdr_len);
1604 
1605 		mwifiex_handle_rx_packet(adapter, skb_deaggr);
1606 		data += blk_size;
1607 		total_pkt_len -= blk_size;
1608 	}
1609 }
1610 
1611 /*
1612  * This function decodes a received packet.
1613  *
1614  * Based on the type, the packet is treated as either a data, or
1615  * a command response, or an event, and the correct handler
1616  * function is invoked.
1617  */
1618 static int mwifiex_decode_rx_packet(struct mwifiex_adapter *adapter,
1619 				    struct sk_buff *skb, u32 upld_typ)
1620 {
1621 	u8 *cmd_buf;
1622 	u16 pkt_len;
1623 	struct mwifiex_rxinfo *rx_info;
1624 
1625 	pkt_len = get_unaligned_le16(skb->data);
1626 
1627 	if (upld_typ != MWIFIEX_TYPE_AGGR_DATA) {
1628 		skb_trim(skb, pkt_len);
1629 		skb_pull(skb, adapter->intf_hdr_len);
1630 	}
1631 
1632 	switch (upld_typ) {
1633 	case MWIFIEX_TYPE_AGGR_DATA:
1634 		mwifiex_dbg(adapter, INFO,
1635 			    "info: --- Rx: Aggr Data packet ---\n");
1636 		rx_info = MWIFIEX_SKB_RXCB(skb);
1637 		rx_info->buf_type = MWIFIEX_TYPE_AGGR_DATA;
1638 		if (adapter->rx_work_enabled) {
1639 			skb_queue_tail(&adapter->rx_data_q, skb);
1640 			atomic_inc(&adapter->rx_pending);
1641 			adapter->data_received = true;
1642 		} else {
1643 			mwifiex_deaggr_sdio_pkt(adapter, skb);
1644 			dev_kfree_skb_any(skb);
1645 		}
1646 		break;
1647 
1648 	case MWIFIEX_TYPE_DATA:
1649 		mwifiex_dbg(adapter, DATA,
1650 			    "info: --- Rx: Data packet ---\n");
1651 		if (adapter->rx_work_enabled) {
1652 			skb_queue_tail(&adapter->rx_data_q, skb);
1653 			adapter->data_received = true;
1654 			atomic_inc(&adapter->rx_pending);
1655 		} else {
1656 			mwifiex_handle_rx_packet(adapter, skb);
1657 		}
1658 		break;
1659 
1660 	case MWIFIEX_TYPE_CMD:
1661 		mwifiex_dbg(adapter, CMD,
1662 			    "info: --- Rx: Cmd Response ---\n");
1663 		/* take care of curr_cmd = NULL case */
1664 		if (!adapter->curr_cmd) {
1665 			cmd_buf = adapter->upld_buf;
1666 
1667 			if (adapter->ps_state == PS_STATE_SLEEP_CFM)
1668 				mwifiex_process_sleep_confirm_resp(adapter,
1669 								   skb->data,
1670 								   skb->len);
1671 
1672 			memcpy(cmd_buf, skb->data,
1673 			       min_t(u32, MWIFIEX_SIZE_OF_CMD_BUFFER,
1674 				     skb->len));
1675 
1676 			dev_kfree_skb_any(skb);
1677 		} else {
1678 			adapter->cmd_resp_received = true;
1679 			adapter->curr_cmd->resp_skb = skb;
1680 		}
1681 		break;
1682 
1683 	case MWIFIEX_TYPE_EVENT:
1684 		mwifiex_dbg(adapter, EVENT,
1685 			    "info: --- Rx: Event ---\n");
1686 		adapter->event_cause = get_unaligned_le32(skb->data);
1687 
1688 		if ((skb->len > 0) && (skb->len  < MAX_EVENT_SIZE))
1689 			memcpy(adapter->event_body,
1690 			       skb->data + MWIFIEX_EVENT_HEADER_LEN,
1691 			       skb->len);
1692 
1693 		/* event cause has been saved to adapter->event_cause */
1694 		adapter->event_received = true;
1695 		adapter->event_skb = skb;
1696 
1697 		break;
1698 
1699 	default:
1700 		mwifiex_dbg(adapter, ERROR,
1701 			    "unknown upload type %#x\n", upld_typ);
1702 		dev_kfree_skb_any(skb);
1703 		break;
1704 	}
1705 
1706 	return 0;
1707 }
1708 
1709 /*
1710  * This function transfers received packets from card to driver, performing
1711  * aggregation if required.
1712  *
1713  * For data received on control port, or if aggregation is disabled, the
1714  * received buffers are uploaded as separate packets. However, if aggregation
1715  * is enabled and required, the buffers are copied onto an aggregation buffer,
1716  * provided there is space left, processed and finally uploaded.
1717  */
1718 static int mwifiex_sdio_card_to_host_mp_aggr(struct mwifiex_adapter *adapter,
1719 					     u16 rx_len, u8 port)
1720 {
1721 	struct sdio_mmc_card *card = adapter->card;
1722 	s32 f_do_rx_aggr = 0;
1723 	s32 f_do_rx_cur = 0;
1724 	s32 f_aggr_cur = 0;
1725 	s32 f_post_aggr_cur = 0;
1726 	struct sk_buff *skb_deaggr;
1727 	struct sk_buff *skb = NULL;
1728 	u32 pkt_len, pkt_type, mport, pind;
1729 	u8 *curr_ptr;
1730 
1731 	if ((card->has_control_mask) && (port == CTRL_PORT)) {
1732 		/* Read the command Resp without aggr */
1733 		mwifiex_dbg(adapter, CMD,
1734 			    "info: %s: no aggregation for cmd\t"
1735 			    "response\n", __func__);
1736 
1737 		f_do_rx_cur = 1;
1738 		goto rx_curr_single;
1739 	}
1740 
1741 	if (!card->mpa_rx.enabled) {
1742 		mwifiex_dbg(adapter, WARN,
1743 			    "info: %s: rx aggregation disabled\n",
1744 			    __func__);
1745 
1746 		f_do_rx_cur = 1;
1747 		goto rx_curr_single;
1748 	}
1749 
1750 	if ((!card->has_control_mask && (card->mp_rd_bitmap &
1751 					 card->reg->data_port_mask)) ||
1752 	    (card->has_control_mask && (card->mp_rd_bitmap &
1753 					(~((u32) CTRL_PORT_MASK))))) {
1754 		/* Some more data RX pending */
1755 		mwifiex_dbg(adapter, INFO,
1756 			    "info: %s: not last packet\n", __func__);
1757 
1758 		if (MP_RX_AGGR_IN_PROGRESS(card)) {
1759 			if (MP_RX_AGGR_BUF_HAS_ROOM(card, rx_len)) {
1760 				f_aggr_cur = 1;
1761 			} else {
1762 				/* No room in Aggr buf, do rx aggr now */
1763 				f_do_rx_aggr = 1;
1764 				f_post_aggr_cur = 1;
1765 			}
1766 		} else {
1767 			/* Rx aggr not in progress */
1768 			f_aggr_cur = 1;
1769 		}
1770 
1771 	} else {
1772 		/* No more data RX pending */
1773 		mwifiex_dbg(adapter, INFO,
1774 			    "info: %s: last packet\n", __func__);
1775 
1776 		if (MP_RX_AGGR_IN_PROGRESS(card)) {
1777 			f_do_rx_aggr = 1;
1778 			if (MP_RX_AGGR_BUF_HAS_ROOM(card, rx_len))
1779 				f_aggr_cur = 1;
1780 			else
1781 				/* No room in Aggr buf, do rx aggr now */
1782 				f_do_rx_cur = 1;
1783 		} else {
1784 			f_do_rx_cur = 1;
1785 		}
1786 	}
1787 
1788 	if (f_aggr_cur) {
1789 		mwifiex_dbg(adapter, INFO,
1790 			    "info: current packet aggregation\n");
1791 		/* Curr pkt can be aggregated */
1792 		mp_rx_aggr_setup(card, rx_len, port);
1793 
1794 		if (MP_RX_AGGR_PKT_LIMIT_REACHED(card) ||
1795 		    mp_rx_aggr_port_limit_reached(card)) {
1796 			mwifiex_dbg(adapter, INFO,
1797 				    "info: %s: aggregated packet\t"
1798 				    "limit reached\n", __func__);
1799 			/* No more pkts allowed in Aggr buf, rx it */
1800 			f_do_rx_aggr = 1;
1801 		}
1802 	}
1803 
1804 	if (f_do_rx_aggr) {
1805 		/* do aggr RX now */
1806 		mwifiex_dbg(adapter, DATA,
1807 			    "info: do_rx_aggr: num of packets: %d\n",
1808 			    card->mpa_rx.pkt_cnt);
1809 
1810 		if (card->supports_sdio_new_mode) {
1811 			int i;
1812 			u32 port_count;
1813 
1814 			for (i = 0, port_count = 0; i < card->max_ports; i++)
1815 				if (card->mpa_rx.ports & BIT(i))
1816 					port_count++;
1817 
1818 			/* Reading data from "start_port + 0" to "start_port +
1819 			 * port_count -1", so decrease the count by 1
1820 			 */
1821 			port_count--;
1822 			mport = (adapter->ioport | SDIO_MPA_ADDR_BASE |
1823 				 (port_count << 8)) + card->mpa_rx.start_port;
1824 		} else {
1825 			mport = (adapter->ioport | SDIO_MPA_ADDR_BASE |
1826 				 (card->mpa_rx.ports << 4)) +
1827 				 card->mpa_rx.start_port;
1828 		}
1829 
1830 		if (card->mpa_rx.pkt_cnt == 1)
1831 			mport = adapter->ioport + card->mpa_rx.start_port;
1832 
1833 		if (mwifiex_read_data_sync(adapter, card->mpa_rx.buf,
1834 					   card->mpa_rx.buf_len, mport, 1))
1835 			goto error;
1836 
1837 		curr_ptr = card->mpa_rx.buf;
1838 
1839 		for (pind = 0; pind < card->mpa_rx.pkt_cnt; pind++) {
1840 			u32 *len_arr = card->mpa_rx.len_arr;
1841 
1842 			/* get curr PKT len & type */
1843 			pkt_len = get_unaligned_le16(&curr_ptr[0]);
1844 			pkt_type = get_unaligned_le16(&curr_ptr[2]);
1845 
1846 			/* copy pkt to deaggr buf */
1847 			skb_deaggr = mwifiex_alloc_dma_align_buf(len_arr[pind],
1848 								 GFP_KERNEL);
1849 			if (!skb_deaggr) {
1850 				mwifiex_dbg(adapter, ERROR, "skb allocation failure\t"
1851 					    "drop pkt len=%d type=%d\n",
1852 					    pkt_len, pkt_type);
1853 				curr_ptr += len_arr[pind];
1854 				continue;
1855 			}
1856 
1857 			skb_put(skb_deaggr, len_arr[pind]);
1858 
1859 			if ((pkt_type == MWIFIEX_TYPE_DATA ||
1860 			     (pkt_type == MWIFIEX_TYPE_AGGR_DATA &&
1861 			      adapter->sdio_rx_aggr_enable)) &&
1862 			    (pkt_len <= len_arr[pind])) {
1863 
1864 				memcpy(skb_deaggr->data, curr_ptr, pkt_len);
1865 
1866 				skb_trim(skb_deaggr, pkt_len);
1867 
1868 				/* Process de-aggr packet */
1869 				mwifiex_decode_rx_packet(adapter, skb_deaggr,
1870 							 pkt_type);
1871 			} else {
1872 				mwifiex_dbg(adapter, ERROR,
1873 					    "drop wrong aggr pkt:\t"
1874 					    "sdio_single_port_rx_aggr=%d\t"
1875 					    "type=%d len=%d max_len=%d\n",
1876 					    adapter->sdio_rx_aggr_enable,
1877 					    pkt_type, pkt_len, len_arr[pind]);
1878 				dev_kfree_skb_any(skb_deaggr);
1879 			}
1880 			curr_ptr += len_arr[pind];
1881 		}
1882 		MP_RX_AGGR_BUF_RESET(card);
1883 	}
1884 
1885 rx_curr_single:
1886 	if (f_do_rx_cur) {
1887 		mwifiex_dbg(adapter, INFO, "info: RX: port: %d, rx_len: %d\n",
1888 			    port, rx_len);
1889 
1890 		skb = mwifiex_alloc_dma_align_buf(rx_len, GFP_KERNEL);
1891 		if (!skb) {
1892 			mwifiex_dbg(adapter, ERROR,
1893 				    "single skb allocated fail,\t"
1894 				    "drop pkt port=%d len=%d\n", port, rx_len);
1895 			if (mwifiex_sdio_card_to_host(adapter, &pkt_type,
1896 						      card->mpa_rx.buf, rx_len,
1897 						      adapter->ioport + port))
1898 				goto error;
1899 			return 0;
1900 		}
1901 
1902 		skb_put(skb, rx_len);
1903 
1904 		if (mwifiex_sdio_card_to_host(adapter, &pkt_type,
1905 					      skb->data, skb->len,
1906 					      adapter->ioport + port))
1907 			goto error;
1908 		if (!adapter->sdio_rx_aggr_enable &&
1909 		    pkt_type == MWIFIEX_TYPE_AGGR_DATA) {
1910 			mwifiex_dbg(adapter, ERROR, "drop wrong pkt type %d\t"
1911 				    "current SDIO RX Aggr not enabled\n",
1912 				    pkt_type);
1913 			dev_kfree_skb_any(skb);
1914 			return 0;
1915 		}
1916 
1917 		mwifiex_decode_rx_packet(adapter, skb, pkt_type);
1918 	}
1919 	if (f_post_aggr_cur) {
1920 		mwifiex_dbg(adapter, INFO,
1921 			    "info: current packet aggregation\n");
1922 		/* Curr pkt can be aggregated */
1923 		mp_rx_aggr_setup(card, rx_len, port);
1924 	}
1925 
1926 	return 0;
1927 error:
1928 	if (MP_RX_AGGR_IN_PROGRESS(card))
1929 		MP_RX_AGGR_BUF_RESET(card);
1930 
1931 	if (f_do_rx_cur && skb)
1932 		/* Single transfer pending. Free curr buff also */
1933 		dev_kfree_skb_any(skb);
1934 
1935 	return -1;
1936 }
1937 
1938 /*
1939  * This function checks the current interrupt status.
1940  *
1941  * The following interrupts are checked and handled by this function -
1942  *      - Data sent
1943  *      - Command sent
1944  *      - Packets received
1945  *
1946  * Since the firmware does not generate download ready interrupt if the
1947  * port updated is command port only, command sent interrupt checking
1948  * should be done manually, and for every SDIO interrupt.
1949  *
1950  * In case of Rx packets received, the packets are uploaded from card to
1951  * host and processed accordingly.
1952  */
1953 static int mwifiex_process_int_status(struct mwifiex_adapter *adapter)
1954 {
1955 	struct sdio_mmc_card *card = adapter->card;
1956 	const struct mwifiex_sdio_card_reg *reg = card->reg;
1957 	int ret = 0;
1958 	u8 sdio_ireg;
1959 	struct sk_buff *skb;
1960 	u8 port = CTRL_PORT;
1961 	u32 len_reg_l, len_reg_u;
1962 	u32 rx_blocks;
1963 	u16 rx_len;
1964 	unsigned long flags;
1965 	u32 bitmap;
1966 	u8 cr;
1967 
1968 	spin_lock_irqsave(&adapter->int_lock, flags);
1969 	sdio_ireg = adapter->int_status;
1970 	adapter->int_status = 0;
1971 	spin_unlock_irqrestore(&adapter->int_lock, flags);
1972 
1973 	if (!sdio_ireg)
1974 		return ret;
1975 
1976 	/* Following interrupt is only for SDIO new mode */
1977 	if (sdio_ireg & DN_LD_CMD_PORT_HOST_INT_STATUS && adapter->cmd_sent)
1978 		adapter->cmd_sent = false;
1979 
1980 	/* Following interrupt is only for SDIO new mode */
1981 	if (sdio_ireg & UP_LD_CMD_PORT_HOST_INT_STATUS) {
1982 		u32 pkt_type;
1983 
1984 		/* read the len of control packet */
1985 		rx_len = card->mp_regs[reg->cmd_rd_len_1] << 8;
1986 		rx_len |= (u16)card->mp_regs[reg->cmd_rd_len_0];
1987 		rx_blocks = DIV_ROUND_UP(rx_len, MWIFIEX_SDIO_BLOCK_SIZE);
1988 		if (rx_len <= adapter->intf_hdr_len ||
1989 		    (rx_blocks * MWIFIEX_SDIO_BLOCK_SIZE) >
1990 		     MWIFIEX_RX_DATA_BUF_SIZE)
1991 			return -1;
1992 		rx_len = (u16) (rx_blocks * MWIFIEX_SDIO_BLOCK_SIZE);
1993 		mwifiex_dbg(adapter, INFO, "info: rx_len = %d\n", rx_len);
1994 
1995 		skb = mwifiex_alloc_dma_align_buf(rx_len, GFP_KERNEL);
1996 		if (!skb)
1997 			return -1;
1998 
1999 		skb_put(skb, rx_len);
2000 
2001 		if (mwifiex_sdio_card_to_host(adapter, &pkt_type, skb->data,
2002 					      skb->len, adapter->ioport |
2003 							CMD_PORT_SLCT)) {
2004 			mwifiex_dbg(adapter, ERROR,
2005 				    "%s: failed to card_to_host", __func__);
2006 			dev_kfree_skb_any(skb);
2007 			goto term_cmd;
2008 		}
2009 
2010 		if ((pkt_type != MWIFIEX_TYPE_CMD) &&
2011 		    (pkt_type != MWIFIEX_TYPE_EVENT))
2012 			mwifiex_dbg(adapter, ERROR,
2013 				    "%s:Received wrong packet on cmd port",
2014 				    __func__);
2015 
2016 		mwifiex_decode_rx_packet(adapter, skb, pkt_type);
2017 	}
2018 
2019 	if (sdio_ireg & DN_LD_HOST_INT_STATUS) {
2020 		bitmap = (u32) card->mp_regs[reg->wr_bitmap_l];
2021 		bitmap |= ((u32) card->mp_regs[reg->wr_bitmap_u]) << 8;
2022 		if (card->supports_sdio_new_mode) {
2023 			bitmap |=
2024 				((u32) card->mp_regs[reg->wr_bitmap_1l]) << 16;
2025 			bitmap |=
2026 				((u32) card->mp_regs[reg->wr_bitmap_1u]) << 24;
2027 		}
2028 		card->mp_wr_bitmap = bitmap;
2029 
2030 		mwifiex_dbg(adapter, INTR,
2031 			    "int: DNLD: wr_bitmap=0x%x\n",
2032 			    card->mp_wr_bitmap);
2033 		if (adapter->data_sent &&
2034 		    (card->mp_wr_bitmap & card->mp_data_port_mask)) {
2035 			mwifiex_dbg(adapter, INTR,
2036 				    "info:  <--- Tx DONE Interrupt --->\n");
2037 			adapter->data_sent = false;
2038 		}
2039 	}
2040 
2041 	/* As firmware will not generate download ready interrupt if the port
2042 	   updated is command port only, cmd_sent should be done for any SDIO
2043 	   interrupt. */
2044 	if (card->has_control_mask && adapter->cmd_sent) {
2045 		/* Check if firmware has attach buffer at command port and
2046 		   update just that in wr_bit_map. */
2047 		card->mp_wr_bitmap |=
2048 			(u32) card->mp_regs[reg->wr_bitmap_l] & CTRL_PORT_MASK;
2049 		if (card->mp_wr_bitmap & CTRL_PORT_MASK)
2050 			adapter->cmd_sent = false;
2051 	}
2052 
2053 	mwifiex_dbg(adapter, INTR, "info: cmd_sent=%d data_sent=%d\n",
2054 		    adapter->cmd_sent, adapter->data_sent);
2055 	if (sdio_ireg & UP_LD_HOST_INT_STATUS) {
2056 		bitmap = (u32) card->mp_regs[reg->rd_bitmap_l];
2057 		bitmap |= ((u32) card->mp_regs[reg->rd_bitmap_u]) << 8;
2058 		if (card->supports_sdio_new_mode) {
2059 			bitmap |=
2060 				((u32) card->mp_regs[reg->rd_bitmap_1l]) << 16;
2061 			bitmap |=
2062 				((u32) card->mp_regs[reg->rd_bitmap_1u]) << 24;
2063 		}
2064 		card->mp_rd_bitmap = bitmap;
2065 		mwifiex_dbg(adapter, INTR,
2066 			    "int: UPLD: rd_bitmap=0x%x\n",
2067 			    card->mp_rd_bitmap);
2068 
2069 		while (true) {
2070 			ret = mwifiex_get_rd_port(adapter, &port);
2071 			if (ret) {
2072 				mwifiex_dbg(adapter, INFO,
2073 					    "info: no more rd_port available\n");
2074 				break;
2075 			}
2076 			len_reg_l = reg->rd_len_p0_l + (port << 1);
2077 			len_reg_u = reg->rd_len_p0_u + (port << 1);
2078 			rx_len = ((u16) card->mp_regs[len_reg_u]) << 8;
2079 			rx_len |= (u16) card->mp_regs[len_reg_l];
2080 			mwifiex_dbg(adapter, INFO,
2081 				    "info: RX: port=%d rx_len=%u\n",
2082 				    port, rx_len);
2083 			rx_blocks =
2084 				(rx_len + MWIFIEX_SDIO_BLOCK_SIZE -
2085 				 1) / MWIFIEX_SDIO_BLOCK_SIZE;
2086 			if (rx_len <= adapter->intf_hdr_len ||
2087 			    (card->mpa_rx.enabled &&
2088 			     ((rx_blocks * MWIFIEX_SDIO_BLOCK_SIZE) >
2089 			      card->mpa_rx.buf_size))) {
2090 				mwifiex_dbg(adapter, ERROR,
2091 					    "invalid rx_len=%d\n",
2092 					    rx_len);
2093 				return -1;
2094 			}
2095 
2096 			rx_len = (u16) (rx_blocks * MWIFIEX_SDIO_BLOCK_SIZE);
2097 			mwifiex_dbg(adapter, INFO, "info: rx_len = %d\n",
2098 				    rx_len);
2099 
2100 			if (mwifiex_sdio_card_to_host_mp_aggr(adapter, rx_len,
2101 							      port)) {
2102 				mwifiex_dbg(adapter, ERROR,
2103 					    "card_to_host_mpa failed: int status=%#x\n",
2104 					    sdio_ireg);
2105 				goto term_cmd;
2106 			}
2107 		}
2108 	}
2109 
2110 	return 0;
2111 
2112 term_cmd:
2113 	/* terminate cmd */
2114 	if (mwifiex_read_reg(adapter, CONFIGURATION_REG, &cr))
2115 		mwifiex_dbg(adapter, ERROR, "read CFG reg failed\n");
2116 	else
2117 		mwifiex_dbg(adapter, INFO,
2118 			    "info: CFG reg val = %d\n", cr);
2119 
2120 	if (mwifiex_write_reg(adapter, CONFIGURATION_REG, (cr | 0x04)))
2121 		mwifiex_dbg(adapter, ERROR,
2122 			    "write CFG reg failed\n");
2123 	else
2124 		mwifiex_dbg(adapter, INFO, "info: write success\n");
2125 
2126 	if (mwifiex_read_reg(adapter, CONFIGURATION_REG, &cr))
2127 		mwifiex_dbg(adapter, ERROR,
2128 			    "read CFG reg failed\n");
2129 	else
2130 		mwifiex_dbg(adapter, INFO,
2131 			    "info: CFG reg val =%x\n", cr);
2132 
2133 	return -1;
2134 }
2135 
2136 /*
2137  * This function aggregates transmission buffers in driver and downloads
2138  * the aggregated packet to card.
2139  *
2140  * The individual packets are aggregated by copying into an aggregation
2141  * buffer and then downloaded to the card. Previous unsent packets in the
2142  * aggregation buffer are pre-copied first before new packets are added.
2143  * Aggregation is done till there is space left in the aggregation buffer,
2144  * or till new packets are available.
2145  *
2146  * The function will only download the packet to the card when aggregation
2147  * stops, otherwise it will just aggregate the packet in aggregation buffer
2148  * and return.
2149  */
2150 static int mwifiex_host_to_card_mp_aggr(struct mwifiex_adapter *adapter,
2151 					u8 *payload, u32 pkt_len, u32 port,
2152 					u32 next_pkt_len)
2153 {
2154 	struct sdio_mmc_card *card = adapter->card;
2155 	int ret = 0;
2156 	s32 f_send_aggr_buf = 0;
2157 	s32 f_send_cur_buf = 0;
2158 	s32 f_precopy_cur_buf = 0;
2159 	s32 f_postcopy_cur_buf = 0;
2160 	u32 mport;
2161 	int index;
2162 
2163 	if (!card->mpa_tx.enabled ||
2164 	    (card->has_control_mask && (port == CTRL_PORT)) ||
2165 	    (card->supports_sdio_new_mode && (port == CMD_PORT_SLCT))) {
2166 		mwifiex_dbg(adapter, WARN,
2167 			    "info: %s: tx aggregation disabled\n",
2168 			    __func__);
2169 
2170 		f_send_cur_buf = 1;
2171 		goto tx_curr_single;
2172 	}
2173 
2174 	if (next_pkt_len) {
2175 		/* More pkt in TX queue */
2176 		mwifiex_dbg(adapter, INFO,
2177 			    "info: %s: more packets in queue.\n",
2178 			    __func__);
2179 
2180 		if (MP_TX_AGGR_IN_PROGRESS(card)) {
2181 			if (MP_TX_AGGR_BUF_HAS_ROOM(card, pkt_len)) {
2182 				f_precopy_cur_buf = 1;
2183 
2184 				if (!(card->mp_wr_bitmap &
2185 				      (1 << card->curr_wr_port)) ||
2186 				    !MP_TX_AGGR_BUF_HAS_ROOM(
2187 					    card, pkt_len + next_pkt_len))
2188 					f_send_aggr_buf = 1;
2189 			} else {
2190 				/* No room in Aggr buf, send it */
2191 				f_send_aggr_buf = 1;
2192 
2193 				if (!(card->mp_wr_bitmap &
2194 				      (1 << card->curr_wr_port)))
2195 					f_send_cur_buf = 1;
2196 				else
2197 					f_postcopy_cur_buf = 1;
2198 			}
2199 		} else {
2200 			if (MP_TX_AGGR_BUF_HAS_ROOM(card, pkt_len) &&
2201 			    (card->mp_wr_bitmap & (1 << card->curr_wr_port)))
2202 				f_precopy_cur_buf = 1;
2203 			else
2204 				f_send_cur_buf = 1;
2205 		}
2206 	} else {
2207 		/* Last pkt in TX queue */
2208 		mwifiex_dbg(adapter, INFO,
2209 			    "info: %s: Last packet in Tx Queue.\n",
2210 			    __func__);
2211 
2212 		if (MP_TX_AGGR_IN_PROGRESS(card)) {
2213 			/* some packs in Aggr buf already */
2214 			f_send_aggr_buf = 1;
2215 
2216 			if (MP_TX_AGGR_BUF_HAS_ROOM(card, pkt_len))
2217 				f_precopy_cur_buf = 1;
2218 			else
2219 				/* No room in Aggr buf, send it */
2220 				f_send_cur_buf = 1;
2221 		} else {
2222 			f_send_cur_buf = 1;
2223 		}
2224 	}
2225 
2226 	if (f_precopy_cur_buf) {
2227 		mwifiex_dbg(adapter, DATA,
2228 			    "data: %s: precopy current buffer\n",
2229 			    __func__);
2230 		MP_TX_AGGR_BUF_PUT(card, payload, pkt_len, port);
2231 
2232 		if (MP_TX_AGGR_PKT_LIMIT_REACHED(card) ||
2233 		    mp_tx_aggr_port_limit_reached(card))
2234 			/* No more pkts allowed in Aggr buf, send it */
2235 			f_send_aggr_buf = 1;
2236 	}
2237 
2238 	if (f_send_aggr_buf) {
2239 		mwifiex_dbg(adapter, DATA,
2240 			    "data: %s: send aggr buffer: %d %d\n",
2241 			    __func__, card->mpa_tx.start_port,
2242 			    card->mpa_tx.ports);
2243 		if (card->supports_sdio_new_mode) {
2244 			u32 port_count;
2245 			int i;
2246 
2247 			for (i = 0, port_count = 0; i < card->max_ports; i++)
2248 				if (card->mpa_tx.ports & BIT(i))
2249 					port_count++;
2250 
2251 			/* Writing data from "start_port + 0" to "start_port +
2252 			 * port_count -1", so decrease the count by 1
2253 			 */
2254 			port_count--;
2255 			mport = (adapter->ioport | SDIO_MPA_ADDR_BASE |
2256 				 (port_count << 8)) + card->mpa_tx.start_port;
2257 		} else {
2258 			mport = (adapter->ioport | SDIO_MPA_ADDR_BASE |
2259 				 (card->mpa_tx.ports << 4)) +
2260 				 card->mpa_tx.start_port;
2261 		}
2262 
2263 		if (card->mpa_tx.pkt_cnt == 1)
2264 			mport = adapter->ioport + card->mpa_tx.start_port;
2265 
2266 		ret = mwifiex_write_data_to_card(adapter, card->mpa_tx.buf,
2267 						 card->mpa_tx.buf_len, mport);
2268 
2269 		/* Save the last multi port tx aggreagation info to debug log */
2270 		index = adapter->dbg.last_sdio_mp_index;
2271 		index = (index + 1) % MWIFIEX_DBG_SDIO_MP_NUM;
2272 		adapter->dbg.last_sdio_mp_index = index;
2273 		adapter->dbg.last_mp_wr_ports[index] = mport;
2274 		adapter->dbg.last_mp_wr_bitmap[index] = card->mp_wr_bitmap;
2275 		adapter->dbg.last_mp_wr_len[index] = card->mpa_tx.buf_len;
2276 		adapter->dbg.last_mp_curr_wr_port[index] = card->curr_wr_port;
2277 
2278 		MP_TX_AGGR_BUF_RESET(card);
2279 	}
2280 
2281 tx_curr_single:
2282 	if (f_send_cur_buf) {
2283 		mwifiex_dbg(adapter, DATA,
2284 			    "data: %s: send current buffer %d\n",
2285 			    __func__, port);
2286 		ret = mwifiex_write_data_to_card(adapter, payload, pkt_len,
2287 						 adapter->ioport + port);
2288 	}
2289 
2290 	if (f_postcopy_cur_buf) {
2291 		mwifiex_dbg(adapter, DATA,
2292 			    "data: %s: postcopy current buffer\n",
2293 			    __func__);
2294 		MP_TX_AGGR_BUF_PUT(card, payload, pkt_len, port);
2295 	}
2296 
2297 	return ret;
2298 }
2299 
2300 /*
2301  * This function downloads data from driver to card.
2302  *
2303  * Both commands and data packets are transferred to the card by this
2304  * function.
2305  *
2306  * This function adds the SDIO specific header to the front of the buffer
2307  * before transferring. The header contains the length of the packet and
2308  * the type. The firmware handles the packets based upon this set type.
2309  */
2310 static int mwifiex_sdio_host_to_card(struct mwifiex_adapter *adapter,
2311 				     u8 type, struct sk_buff *skb,
2312 				     struct mwifiex_tx_param *tx_param)
2313 {
2314 	struct sdio_mmc_card *card = adapter->card;
2315 	int ret;
2316 	u32 buf_block_len;
2317 	u32 blk_size;
2318 	u32 port = CTRL_PORT;
2319 	u8 *payload = (u8 *)skb->data;
2320 	u32 pkt_len = skb->len;
2321 
2322 	/* Allocate buffer and copy payload */
2323 	blk_size = MWIFIEX_SDIO_BLOCK_SIZE;
2324 	buf_block_len = (pkt_len + blk_size - 1) / blk_size;
2325 	put_unaligned_le16((u16)pkt_len, payload + 0);
2326 	put_unaligned_le16((u32)type, payload + 2);
2327 
2328 
2329 	/*
2330 	 * This is SDIO specific header
2331 	 *  u16 length,
2332 	 *  u16 type (MWIFIEX_TYPE_DATA = 0, MWIFIEX_TYPE_CMD = 1,
2333 	 *  MWIFIEX_TYPE_EVENT = 3)
2334 	 */
2335 	if (type == MWIFIEX_TYPE_DATA) {
2336 		ret = mwifiex_get_wr_port_data(adapter, &port);
2337 		if (ret) {
2338 			mwifiex_dbg(adapter, ERROR,
2339 				    "%s: no wr_port available\n",
2340 				    __func__);
2341 			return ret;
2342 		}
2343 	} else {
2344 		adapter->cmd_sent = true;
2345 		/* Type must be MWIFIEX_TYPE_CMD */
2346 
2347 		if (pkt_len <= adapter->intf_hdr_len ||
2348 		    pkt_len > MWIFIEX_UPLD_SIZE)
2349 			mwifiex_dbg(adapter, ERROR,
2350 				    "%s: payload=%p, nb=%d\n",
2351 				    __func__, payload, pkt_len);
2352 
2353 		if (card->supports_sdio_new_mode)
2354 			port = CMD_PORT_SLCT;
2355 	}
2356 
2357 	/* Transfer data to card */
2358 	pkt_len = buf_block_len * blk_size;
2359 
2360 	if (tx_param)
2361 		ret = mwifiex_host_to_card_mp_aggr(adapter, payload, pkt_len,
2362 						   port, tx_param->next_pkt_len
2363 						   );
2364 	else
2365 		ret = mwifiex_host_to_card_mp_aggr(adapter, payload, pkt_len,
2366 						   port, 0);
2367 
2368 	if (ret) {
2369 		if (type == MWIFIEX_TYPE_CMD)
2370 			adapter->cmd_sent = false;
2371 		if (type == MWIFIEX_TYPE_DATA) {
2372 			adapter->data_sent = false;
2373 			/* restore curr_wr_port in error cases */
2374 			card->curr_wr_port = port;
2375 			card->mp_wr_bitmap |= (u32)(1 << card->curr_wr_port);
2376 		}
2377 	} else {
2378 		if (type == MWIFIEX_TYPE_DATA) {
2379 			if (!(card->mp_wr_bitmap & (1 << card->curr_wr_port)))
2380 				adapter->data_sent = true;
2381 			else
2382 				adapter->data_sent = false;
2383 		}
2384 	}
2385 
2386 	return ret;
2387 }
2388 
2389 /*
2390  * This function allocates the MPA Tx and Rx buffers.
2391  */
2392 static int mwifiex_alloc_sdio_mpa_buffers(struct mwifiex_adapter *adapter,
2393 				   u32 mpa_tx_buf_size, u32 mpa_rx_buf_size)
2394 {
2395 	struct sdio_mmc_card *card = adapter->card;
2396 	u32 rx_buf_size;
2397 	int ret = 0;
2398 
2399 	card->mpa_tx.buf = kzalloc(mpa_tx_buf_size, GFP_KERNEL);
2400 	if (!card->mpa_tx.buf) {
2401 		ret = -1;
2402 		goto error;
2403 	}
2404 
2405 	card->mpa_tx.buf_size = mpa_tx_buf_size;
2406 
2407 	rx_buf_size = max_t(u32, mpa_rx_buf_size,
2408 			    (u32)SDIO_MAX_AGGR_BUF_SIZE);
2409 	card->mpa_rx.buf = kzalloc(rx_buf_size, GFP_KERNEL);
2410 	if (!card->mpa_rx.buf) {
2411 		ret = -1;
2412 		goto error;
2413 	}
2414 
2415 	card->mpa_rx.buf_size = rx_buf_size;
2416 
2417 error:
2418 	if (ret) {
2419 		kfree(card->mpa_tx.buf);
2420 		kfree(card->mpa_rx.buf);
2421 		card->mpa_tx.buf_size = 0;
2422 		card->mpa_rx.buf_size = 0;
2423 		card->mpa_tx.buf = NULL;
2424 		card->mpa_rx.buf = NULL;
2425 	}
2426 
2427 	return ret;
2428 }
2429 
2430 /*
2431  * This function unregisters the SDIO device.
2432  *
2433  * The SDIO IRQ is released, the function is disabled and driver
2434  * data is set to null.
2435  */
2436 static void
2437 mwifiex_unregister_dev(struct mwifiex_adapter *adapter)
2438 {
2439 	struct sdio_mmc_card *card = adapter->card;
2440 
2441 	if (adapter->card) {
2442 		card->adapter = NULL;
2443 		sdio_claim_host(card->func);
2444 		sdio_disable_func(card->func);
2445 		sdio_release_host(card->func);
2446 	}
2447 }
2448 
2449 /*
2450  * This function registers the SDIO device.
2451  *
2452  * SDIO IRQ is claimed, block size is set and driver data is initialized.
2453  */
2454 static int mwifiex_register_dev(struct mwifiex_adapter *adapter)
2455 {
2456 	int ret;
2457 	struct sdio_mmc_card *card = adapter->card;
2458 	struct sdio_func *func = card->func;
2459 	const char *firmware = card->firmware;
2460 
2461 	/* save adapter pointer in card */
2462 	card->adapter = adapter;
2463 	adapter->tx_buf_size = card->tx_buf_size;
2464 
2465 	sdio_claim_host(func);
2466 
2467 	/* Set block size */
2468 	ret = sdio_set_block_size(card->func, MWIFIEX_SDIO_BLOCK_SIZE);
2469 	sdio_release_host(func);
2470 	if (ret) {
2471 		mwifiex_dbg(adapter, ERROR,
2472 			    "cannot set SDIO block size\n");
2473 		return ret;
2474 	}
2475 
2476 	/* Select correct firmware (sdsd or sdiouart) firmware based on the strapping
2477 	 * option
2478 	 */
2479 	if (card->firmware_sdiouart) {
2480 		u8 val;
2481 
2482 		mwifiex_read_reg(adapter, card->reg->host_strap_reg, &val);
2483 		if ((val & card->reg->host_strap_mask) == card->reg->host_strap_value)
2484 			firmware = card->firmware_sdiouart;
2485 	}
2486 	strcpy(adapter->fw_name, firmware);
2487 
2488 	if (card->fw_dump_enh) {
2489 		adapter->mem_type_mapping_tbl = generic_mem_type_map;
2490 		adapter->num_mem_types = 1;
2491 	} else {
2492 		adapter->mem_type_mapping_tbl = mem_type_mapping_tbl;
2493 		adapter->num_mem_types = ARRAY_SIZE(mem_type_mapping_tbl);
2494 	}
2495 
2496 	return 0;
2497 }
2498 
2499 /*
2500  * This function initializes the SDIO driver.
2501  *
2502  * The following initializations steps are followed -
2503  *      - Read the Host interrupt status register to acknowledge
2504  *        the first interrupt got from bootloader
2505  *      - Disable host interrupt mask register
2506  *      - Get SDIO port
2507  *      - Initialize SDIO variables in card
2508  *      - Allocate MP registers
2509  *      - Allocate MPA Tx and Rx buffers
2510  */
2511 static int mwifiex_init_sdio(struct mwifiex_adapter *adapter)
2512 {
2513 	struct sdio_mmc_card *card = adapter->card;
2514 	const struct mwifiex_sdio_card_reg *reg = card->reg;
2515 	int ret;
2516 	u8 sdio_ireg;
2517 
2518 	sdio_set_drvdata(card->func, card);
2519 
2520 	/*
2521 	 * Read the host_int_status_reg for ACK the first interrupt got
2522 	 * from the bootloader. If we don't do this we get a interrupt
2523 	 * as soon as we register the irq.
2524 	 */
2525 	mwifiex_read_reg(adapter, card->reg->host_int_status_reg, &sdio_ireg);
2526 
2527 	/* Get SDIO ioport */
2528 	mwifiex_init_sdio_ioport(adapter);
2529 
2530 	/* Initialize SDIO variables in card */
2531 	card->mp_rd_bitmap = 0;
2532 	card->mp_wr_bitmap = 0;
2533 	card->curr_rd_port = reg->start_rd_port;
2534 	card->curr_wr_port = reg->start_wr_port;
2535 
2536 	card->mp_data_port_mask = reg->data_port_mask;
2537 
2538 	card->mpa_tx.buf_len = 0;
2539 	card->mpa_tx.pkt_cnt = 0;
2540 	card->mpa_tx.start_port = 0;
2541 
2542 	card->mpa_tx.enabled = 1;
2543 	card->mpa_tx.pkt_aggr_limit = card->mp_agg_pkt_limit;
2544 
2545 	card->mpa_rx.buf_len = 0;
2546 	card->mpa_rx.pkt_cnt = 0;
2547 	card->mpa_rx.start_port = 0;
2548 
2549 	card->mpa_rx.enabled = 1;
2550 	card->mpa_rx.pkt_aggr_limit = card->mp_agg_pkt_limit;
2551 
2552 	/* Allocate buffers for SDIO MP-A */
2553 	card->mp_regs = kzalloc(reg->max_mp_regs, GFP_KERNEL);
2554 	if (!card->mp_regs)
2555 		return -ENOMEM;
2556 
2557 	/* Allocate skb pointer buffers */
2558 	card->mpa_rx.skb_arr = kcalloc(card->mp_agg_pkt_limit, sizeof(void *),
2559 				       GFP_KERNEL);
2560 	if (!card->mpa_rx.skb_arr) {
2561 		kfree(card->mp_regs);
2562 		return -ENOMEM;
2563 	}
2564 
2565 	card->mpa_rx.len_arr = kcalloc(card->mp_agg_pkt_limit,
2566 				       sizeof(*card->mpa_rx.len_arr),
2567 				       GFP_KERNEL);
2568 	if (!card->mpa_rx.len_arr) {
2569 		kfree(card->mp_regs);
2570 		kfree(card->mpa_rx.skb_arr);
2571 		return -ENOMEM;
2572 	}
2573 
2574 	ret = mwifiex_alloc_sdio_mpa_buffers(adapter,
2575 					     card->mp_tx_agg_buf_size,
2576 					     card->mp_rx_agg_buf_size);
2577 
2578 	/* Allocate 32k MPA Tx/Rx buffers if 64k memory allocation fails */
2579 	if (ret && (card->mp_tx_agg_buf_size == MWIFIEX_MP_AGGR_BUF_SIZE_MAX ||
2580 		    card->mp_rx_agg_buf_size == MWIFIEX_MP_AGGR_BUF_SIZE_MAX)) {
2581 		/* Disable rx single port aggregation */
2582 		adapter->host_disable_sdio_rx_aggr = true;
2583 
2584 		ret = mwifiex_alloc_sdio_mpa_buffers
2585 			(adapter, MWIFIEX_MP_AGGR_BUF_SIZE_32K,
2586 			 MWIFIEX_MP_AGGR_BUF_SIZE_32K);
2587 		if (ret) {
2588 			/* Disable multi port aggregation */
2589 			card->mpa_tx.enabled = 0;
2590 			card->mpa_rx.enabled = 0;
2591 		}
2592 	}
2593 
2594 	adapter->auto_tdls = card->can_auto_tdls;
2595 	adapter->ext_scan = card->can_ext_scan;
2596 	return 0;
2597 }
2598 
2599 /*
2600  * This function resets the MPA Tx and Rx buffers.
2601  */
2602 static void mwifiex_cleanup_mpa_buf(struct mwifiex_adapter *adapter)
2603 {
2604 	struct sdio_mmc_card *card = adapter->card;
2605 
2606 	MP_TX_AGGR_BUF_RESET(card);
2607 	MP_RX_AGGR_BUF_RESET(card);
2608 }
2609 
2610 /*
2611  * This function cleans up the allocated card buffers.
2612  *
2613  * The following are freed by this function -
2614  *      - MP registers
2615  *      - MPA Tx buffer
2616  *      - MPA Rx buffer
2617  */
2618 static void mwifiex_cleanup_sdio(struct mwifiex_adapter *adapter)
2619 {
2620 	struct sdio_mmc_card *card = adapter->card;
2621 
2622 	cancel_work_sync(&card->work);
2623 
2624 	kfree(card->mp_regs);
2625 	kfree(card->mpa_rx.skb_arr);
2626 	kfree(card->mpa_rx.len_arr);
2627 	kfree(card->mpa_tx.buf);
2628 	kfree(card->mpa_rx.buf);
2629 }
2630 
2631 /*
2632  * This function updates the MP end port in card.
2633  */
2634 static void
2635 mwifiex_update_mp_end_port(struct mwifiex_adapter *adapter, u16 port)
2636 {
2637 	struct sdio_mmc_card *card = adapter->card;
2638 	const struct mwifiex_sdio_card_reg *reg = card->reg;
2639 	int i;
2640 
2641 	card->mp_end_port = port;
2642 
2643 	card->mp_data_port_mask = reg->data_port_mask;
2644 
2645 	if (reg->start_wr_port) {
2646 		for (i = 1; i <= card->max_ports - card->mp_end_port; i++)
2647 			card->mp_data_port_mask &=
2648 					~(1 << (card->max_ports - i));
2649 	}
2650 
2651 	card->curr_wr_port = reg->start_wr_port;
2652 
2653 	mwifiex_dbg(adapter, CMD,
2654 		    "cmd: mp_end_port %d, data port mask 0x%x\n",
2655 		    port, card->mp_data_port_mask);
2656 }
2657 
2658 static void mwifiex_sdio_card_reset_work(struct mwifiex_adapter *adapter)
2659 {
2660 	struct sdio_mmc_card *card = adapter->card;
2661 	struct sdio_func *func = card->func;
2662 	int ret;
2663 
2664 	/* Prepare the adapter for the reset. */
2665 	mwifiex_shutdown_sw(adapter);
2666 	clear_bit(MWIFIEX_IFACE_WORK_DEVICE_DUMP, &card->work_flags);
2667 	clear_bit(MWIFIEX_IFACE_WORK_CARD_RESET, &card->work_flags);
2668 
2669 	/* Run a HW reset of the SDIO interface. */
2670 	sdio_claim_host(func);
2671 	ret = mmc_hw_reset(func->card);
2672 	sdio_release_host(func);
2673 
2674 	switch (ret) {
2675 	case 1:
2676 		dev_dbg(&func->dev, "SDIO HW reset asynchronous\n");
2677 		complete_all(adapter->fw_done);
2678 		break;
2679 	case 0:
2680 		ret = mwifiex_reinit_sw(adapter);
2681 		if (ret)
2682 			dev_err(&func->dev, "reinit failed: %d\n", ret);
2683 		break;
2684 	default:
2685 		dev_err(&func->dev, "SDIO HW reset failed: %d\n", ret);
2686 		break;
2687 	}
2688 }
2689 
2690 /* This function read/write firmware */
2691 static enum
2692 rdwr_status mwifiex_sdio_rdwr_firmware(struct mwifiex_adapter *adapter,
2693 				       u8 doneflag)
2694 {
2695 	struct sdio_mmc_card *card = adapter->card;
2696 	int ret, tries;
2697 	u8 ctrl_data = 0;
2698 
2699 	sdio_writeb(card->func, card->reg->fw_dump_host_ready,
2700 		    card->reg->fw_dump_ctrl, &ret);
2701 	if (ret) {
2702 		mwifiex_dbg(adapter, ERROR, "SDIO Write ERR\n");
2703 		return RDWR_STATUS_FAILURE;
2704 	}
2705 	for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
2706 		ctrl_data = sdio_readb(card->func, card->reg->fw_dump_ctrl,
2707 				       &ret);
2708 		if (ret) {
2709 			mwifiex_dbg(adapter, ERROR, "SDIO read err\n");
2710 			return RDWR_STATUS_FAILURE;
2711 		}
2712 		if (ctrl_data == FW_DUMP_DONE)
2713 			break;
2714 		if (doneflag && ctrl_data == doneflag)
2715 			return RDWR_STATUS_DONE;
2716 		if (ctrl_data != card->reg->fw_dump_host_ready) {
2717 			mwifiex_dbg(adapter, WARN,
2718 				    "The ctrl reg was changed, re-try again\n");
2719 			sdio_writeb(card->func, card->reg->fw_dump_host_ready,
2720 				    card->reg->fw_dump_ctrl, &ret);
2721 			if (ret) {
2722 				mwifiex_dbg(adapter, ERROR, "SDIO write err\n");
2723 				return RDWR_STATUS_FAILURE;
2724 			}
2725 		}
2726 		usleep_range(100, 200);
2727 	}
2728 	if (ctrl_data == card->reg->fw_dump_host_ready) {
2729 		mwifiex_dbg(adapter, ERROR,
2730 			    "Fail to pull ctrl_data\n");
2731 		return RDWR_STATUS_FAILURE;
2732 	}
2733 
2734 	return RDWR_STATUS_SUCCESS;
2735 }
2736 
2737 /* This function dump firmware memory to file */
2738 static void mwifiex_sdio_fw_dump(struct mwifiex_adapter *adapter)
2739 {
2740 	struct sdio_mmc_card *card = adapter->card;
2741 	int ret = 0;
2742 	unsigned int reg, reg_start, reg_end;
2743 	u8 *dbg_ptr, *end_ptr, dump_num, idx, i, read_reg, doneflag = 0;
2744 	enum rdwr_status stat;
2745 	u32 memory_size;
2746 
2747 	if (!card->can_dump_fw)
2748 		return;
2749 
2750 	for (idx = 0; idx < ARRAY_SIZE(mem_type_mapping_tbl); idx++) {
2751 		struct memory_type_mapping *entry = &mem_type_mapping_tbl[idx];
2752 
2753 		if (entry->mem_ptr) {
2754 			vfree(entry->mem_ptr);
2755 			entry->mem_ptr = NULL;
2756 		}
2757 		entry->mem_size = 0;
2758 	}
2759 
2760 	mwifiex_pm_wakeup_card(adapter);
2761 	sdio_claim_host(card->func);
2762 
2763 	mwifiex_dbg(adapter, MSG, "== mwifiex firmware dump start ==\n");
2764 
2765 	stat = mwifiex_sdio_rdwr_firmware(adapter, doneflag);
2766 	if (stat == RDWR_STATUS_FAILURE)
2767 		goto done;
2768 
2769 	reg = card->reg->fw_dump_start;
2770 	/* Read the number of the memories which will dump */
2771 	dump_num = sdio_readb(card->func, reg, &ret);
2772 	if (ret) {
2773 		mwifiex_dbg(adapter, ERROR, "SDIO read memory length err\n");
2774 		goto done;
2775 	}
2776 
2777 	/* Read the length of every memory which will dump */
2778 	for (idx = 0; idx < dump_num; idx++) {
2779 		struct memory_type_mapping *entry = &mem_type_mapping_tbl[idx];
2780 
2781 		stat = mwifiex_sdio_rdwr_firmware(adapter, doneflag);
2782 		if (stat == RDWR_STATUS_FAILURE)
2783 			goto done;
2784 
2785 		memory_size = 0;
2786 		reg = card->reg->fw_dump_start;
2787 		for (i = 0; i < 4; i++) {
2788 			read_reg = sdio_readb(card->func, reg, &ret);
2789 			if (ret) {
2790 				mwifiex_dbg(adapter, ERROR, "SDIO read err\n");
2791 				goto done;
2792 			}
2793 			memory_size |= (read_reg << i*8);
2794 			reg++;
2795 		}
2796 
2797 		if (memory_size == 0) {
2798 			mwifiex_dbg(adapter, DUMP, "Firmware dump Finished!\n");
2799 			ret = mwifiex_write_reg(adapter,
2800 						card->reg->fw_dump_ctrl,
2801 						FW_DUMP_READ_DONE);
2802 			if (ret) {
2803 				mwifiex_dbg(adapter, ERROR, "SDIO write err\n");
2804 				return;
2805 			}
2806 			break;
2807 		}
2808 
2809 		mwifiex_dbg(adapter, DUMP,
2810 			    "%s_SIZE=0x%x\n", entry->mem_name, memory_size);
2811 		entry->mem_ptr = vmalloc(memory_size + 1);
2812 		entry->mem_size = memory_size;
2813 		if (!entry->mem_ptr) {
2814 			mwifiex_dbg(adapter, ERROR, "Vmalloc %s failed\n",
2815 				    entry->mem_name);
2816 			goto done;
2817 		}
2818 		dbg_ptr = entry->mem_ptr;
2819 		end_ptr = dbg_ptr + memory_size;
2820 
2821 		doneflag = entry->done_flag;
2822 		mwifiex_dbg(adapter, DUMP,
2823 			    "Start %s output, please wait...\n",
2824 			    entry->mem_name);
2825 
2826 		do {
2827 			stat = mwifiex_sdio_rdwr_firmware(adapter, doneflag);
2828 			if (stat == RDWR_STATUS_FAILURE)
2829 				goto done;
2830 
2831 			reg_start = card->reg->fw_dump_start;
2832 			reg_end = card->reg->fw_dump_end;
2833 			for (reg = reg_start; reg <= reg_end; reg++) {
2834 				*dbg_ptr = sdio_readb(card->func, reg, &ret);
2835 				if (ret) {
2836 					mwifiex_dbg(adapter, ERROR,
2837 						    "SDIO read err\n");
2838 					goto done;
2839 				}
2840 				if (dbg_ptr < end_ptr)
2841 					dbg_ptr++;
2842 				else
2843 					mwifiex_dbg(adapter, ERROR,
2844 						    "Allocated buf not enough\n");
2845 			}
2846 
2847 			if (stat != RDWR_STATUS_DONE)
2848 				continue;
2849 
2850 			mwifiex_dbg(adapter, DUMP, "%s done: size=0x%tx\n",
2851 				    entry->mem_name, dbg_ptr - entry->mem_ptr);
2852 			break;
2853 		} while (1);
2854 	}
2855 	mwifiex_dbg(adapter, MSG, "== mwifiex firmware dump end ==\n");
2856 
2857 done:
2858 	sdio_release_host(card->func);
2859 }
2860 
2861 static void mwifiex_sdio_generic_fw_dump(struct mwifiex_adapter *adapter)
2862 {
2863 	struct sdio_mmc_card *card = adapter->card;
2864 	struct memory_type_mapping *entry = &generic_mem_type_map[0];
2865 	unsigned int reg, reg_start, reg_end;
2866 	u8 start_flag = 0, done_flag = 0;
2867 	u8 *dbg_ptr, *end_ptr;
2868 	enum rdwr_status stat;
2869 	int ret = -1, tries;
2870 
2871 	if (!card->fw_dump_enh)
2872 		return;
2873 
2874 	if (entry->mem_ptr) {
2875 		vfree(entry->mem_ptr);
2876 		entry->mem_ptr = NULL;
2877 	}
2878 	entry->mem_size = 0;
2879 
2880 	mwifiex_pm_wakeup_card(adapter);
2881 	sdio_claim_host(card->func);
2882 
2883 	mwifiex_dbg(adapter, MSG, "== mwifiex firmware dump start ==\n");
2884 
2885 	stat = mwifiex_sdio_rdwr_firmware(adapter, done_flag);
2886 	if (stat == RDWR_STATUS_FAILURE)
2887 		goto done;
2888 
2889 	reg_start = card->reg->fw_dump_start;
2890 	reg_end = card->reg->fw_dump_end;
2891 	for (reg = reg_start; reg <= reg_end; reg++) {
2892 		for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
2893 			start_flag = sdio_readb(card->func, reg, &ret);
2894 			if (ret) {
2895 				mwifiex_dbg(adapter, ERROR,
2896 					    "SDIO read err\n");
2897 				goto done;
2898 			}
2899 			if (start_flag == 0)
2900 				break;
2901 			if (tries == MAX_POLL_TRIES) {
2902 				mwifiex_dbg(adapter, ERROR,
2903 					    "FW not ready to dump\n");
2904 				ret = -1;
2905 				goto done;
2906 			}
2907 		}
2908 		usleep_range(100, 200);
2909 	}
2910 
2911 	entry->mem_ptr = vmalloc(0xf0000 + 1);
2912 	if (!entry->mem_ptr) {
2913 		ret = -1;
2914 		goto done;
2915 	}
2916 	dbg_ptr = entry->mem_ptr;
2917 	entry->mem_size = 0xf0000;
2918 	end_ptr = dbg_ptr + entry->mem_size;
2919 
2920 	done_flag = entry->done_flag;
2921 	mwifiex_dbg(adapter, DUMP,
2922 		    "Start %s output, please wait...\n", entry->mem_name);
2923 
2924 	while (true) {
2925 		stat = mwifiex_sdio_rdwr_firmware(adapter, done_flag);
2926 		if (stat == RDWR_STATUS_FAILURE)
2927 			goto done;
2928 		for (reg = reg_start; reg <= reg_end; reg++) {
2929 			*dbg_ptr = sdio_readb(card->func, reg, &ret);
2930 			if (ret) {
2931 				mwifiex_dbg(adapter, ERROR,
2932 					    "SDIO read err\n");
2933 				goto done;
2934 			}
2935 			dbg_ptr++;
2936 			if (dbg_ptr >= end_ptr) {
2937 				u8 *tmp_ptr;
2938 
2939 				tmp_ptr = vmalloc(entry->mem_size + 0x4000 + 1);
2940 				if (!tmp_ptr)
2941 					goto done;
2942 
2943 				memcpy(tmp_ptr, entry->mem_ptr,
2944 				       entry->mem_size);
2945 				vfree(entry->mem_ptr);
2946 				entry->mem_ptr = tmp_ptr;
2947 				tmp_ptr = NULL;
2948 				dbg_ptr = entry->mem_ptr + entry->mem_size;
2949 				entry->mem_size += 0x4000;
2950 				end_ptr = entry->mem_ptr + entry->mem_size;
2951 			}
2952 		}
2953 		if (stat == RDWR_STATUS_DONE) {
2954 			entry->mem_size = dbg_ptr - entry->mem_ptr;
2955 			mwifiex_dbg(adapter, DUMP, "dump %s done size=0x%x\n",
2956 				    entry->mem_name, entry->mem_size);
2957 			ret = 0;
2958 			break;
2959 		}
2960 	}
2961 	mwifiex_dbg(adapter, MSG, "== mwifiex firmware dump end ==\n");
2962 
2963 done:
2964 	if (ret) {
2965 		mwifiex_dbg(adapter, ERROR, "firmware dump failed\n");
2966 		if (entry->mem_ptr) {
2967 			vfree(entry->mem_ptr);
2968 			entry->mem_ptr = NULL;
2969 		}
2970 		entry->mem_size = 0;
2971 	}
2972 	sdio_release_host(card->func);
2973 }
2974 
2975 static void mwifiex_sdio_device_dump_work(struct mwifiex_adapter *adapter)
2976 {
2977 	struct sdio_mmc_card *card = adapter->card;
2978 
2979 	adapter->devdump_data = vzalloc(MWIFIEX_FW_DUMP_SIZE);
2980 	if (!adapter->devdump_data) {
2981 		mwifiex_dbg(adapter, ERROR,
2982 			    "vzalloc devdump data failure!\n");
2983 		return;
2984 	}
2985 
2986 	mwifiex_drv_info_dump(adapter);
2987 	if (card->fw_dump_enh)
2988 		mwifiex_sdio_generic_fw_dump(adapter);
2989 	else
2990 		mwifiex_sdio_fw_dump(adapter);
2991 	mwifiex_prepare_fw_dump_info(adapter);
2992 	mwifiex_upload_device_dump(adapter);
2993 }
2994 
2995 static void mwifiex_sdio_work(struct work_struct *work)
2996 {
2997 	struct sdio_mmc_card *card =
2998 		container_of(work, struct sdio_mmc_card, work);
2999 
3000 	if (test_and_clear_bit(MWIFIEX_IFACE_WORK_DEVICE_DUMP,
3001 			       &card->work_flags))
3002 		mwifiex_sdio_device_dump_work(card->adapter);
3003 	if (test_and_clear_bit(MWIFIEX_IFACE_WORK_CARD_RESET,
3004 			       &card->work_flags))
3005 		mwifiex_sdio_card_reset_work(card->adapter);
3006 }
3007 
3008 /* This function resets the card */
3009 static void mwifiex_sdio_card_reset(struct mwifiex_adapter *adapter)
3010 {
3011 	struct sdio_mmc_card *card = adapter->card;
3012 
3013 	if (!test_and_set_bit(MWIFIEX_IFACE_WORK_CARD_RESET, &card->work_flags))
3014 		schedule_work(&card->work);
3015 }
3016 
3017 /* This function dumps FW information */
3018 static void mwifiex_sdio_device_dump(struct mwifiex_adapter *adapter)
3019 {
3020 	struct sdio_mmc_card *card = adapter->card;
3021 
3022 	if (!test_and_set_bit(MWIFIEX_IFACE_WORK_DEVICE_DUMP,
3023 			      &card->work_flags))
3024 		schedule_work(&card->work);
3025 }
3026 
3027 /* Function to dump SDIO function registers and SDIO scratch registers in case
3028  * of FW crash
3029  */
3030 static int
3031 mwifiex_sdio_reg_dump(struct mwifiex_adapter *adapter, char *drv_buf)
3032 {
3033 	char *p = drv_buf;
3034 	struct sdio_mmc_card *cardp = adapter->card;
3035 	int ret = 0;
3036 	u8 count, func, data, index = 0, size = 0;
3037 	u8 reg, reg_start, reg_end;
3038 	char buf[256], *ptr;
3039 
3040 	if (!p)
3041 		return 0;
3042 
3043 	mwifiex_dbg(adapter, MSG, "SDIO register dump start\n");
3044 
3045 	mwifiex_pm_wakeup_card(adapter);
3046 
3047 	sdio_claim_host(cardp->func);
3048 
3049 	for (count = 0; count < 5; count++) {
3050 		memset(buf, 0, sizeof(buf));
3051 		ptr = buf;
3052 
3053 		switch (count) {
3054 		case 0:
3055 			/* Read the registers of SDIO function0 */
3056 			func = count;
3057 			reg_start = 0;
3058 			reg_end = 9;
3059 			break;
3060 		case 1:
3061 			/* Read the registers of SDIO function1 */
3062 			func = count;
3063 			reg_start = cardp->reg->func1_dump_reg_start;
3064 			reg_end = cardp->reg->func1_dump_reg_end;
3065 			break;
3066 		case 2:
3067 			index = 0;
3068 			func = 1;
3069 			reg_start = cardp->reg->func1_spec_reg_table[index++];
3070 			size = cardp->reg->func1_spec_reg_num;
3071 			reg_end = cardp->reg->func1_spec_reg_table[size-1];
3072 			break;
3073 		default:
3074 			/* Read the scratch registers of SDIO function1 */
3075 			if (count == 4)
3076 				mdelay(100);
3077 			func = 1;
3078 			reg_start = cardp->reg->func1_scratch_reg;
3079 			reg_end = reg_start + MWIFIEX_SDIO_SCRATCH_SIZE;
3080 		}
3081 
3082 		if (count != 2)
3083 			ptr += sprintf(ptr, "SDIO Func%d (%#x-%#x): ",
3084 				       func, reg_start, reg_end);
3085 		else
3086 			ptr += sprintf(ptr, "SDIO Func%d: ", func);
3087 
3088 		for (reg = reg_start; reg <= reg_end;) {
3089 			if (func == 0)
3090 				data = sdio_f0_readb(cardp->func, reg, &ret);
3091 			else
3092 				data = sdio_readb(cardp->func, reg, &ret);
3093 
3094 			if (count == 2)
3095 				ptr += sprintf(ptr, "(%#x) ", reg);
3096 			if (!ret) {
3097 				ptr += sprintf(ptr, "%02x ", data);
3098 			} else {
3099 				ptr += sprintf(ptr, "ERR");
3100 				break;
3101 			}
3102 
3103 			if (count == 2 && reg < reg_end)
3104 				reg = cardp->reg->func1_spec_reg_table[index++];
3105 			else
3106 				reg++;
3107 		}
3108 
3109 		mwifiex_dbg(adapter, MSG, "%s\n", buf);
3110 		p += sprintf(p, "%s\n", buf);
3111 	}
3112 
3113 	sdio_release_host(cardp->func);
3114 
3115 	mwifiex_dbg(adapter, MSG, "SDIO register dump end\n");
3116 
3117 	return p - drv_buf;
3118 }
3119 
3120 /* sdio device/function initialization, code is extracted
3121  * from init_if handler and register_dev handler.
3122  */
3123 static void mwifiex_sdio_up_dev(struct mwifiex_adapter *adapter)
3124 {
3125 	struct sdio_mmc_card *card = adapter->card;
3126 	u8 sdio_ireg;
3127 
3128 	sdio_claim_host(card->func);
3129 	sdio_enable_func(card->func);
3130 	sdio_set_block_size(card->func, MWIFIEX_SDIO_BLOCK_SIZE);
3131 	sdio_release_host(card->func);
3132 
3133 	/* tx_buf_size might be changed to 3584 by firmware during
3134 	 * data transfer, we will reset to default size.
3135 	 */
3136 	adapter->tx_buf_size = card->tx_buf_size;
3137 
3138 	/* Read the host_int_status_reg for ACK the first interrupt got
3139 	 * from the bootloader. If we don't do this we get a interrupt
3140 	 * as soon as we register the irq.
3141 	 */
3142 	mwifiex_read_reg(adapter, card->reg->host_int_status_reg, &sdio_ireg);
3143 
3144 	mwifiex_init_sdio_ioport(adapter);
3145 }
3146 
3147 static struct mwifiex_if_ops sdio_ops = {
3148 	.init_if = mwifiex_init_sdio,
3149 	.cleanup_if = mwifiex_cleanup_sdio,
3150 	.check_fw_status = mwifiex_check_fw_status,
3151 	.check_winner_status = mwifiex_check_winner_status,
3152 	.prog_fw = mwifiex_prog_fw_w_helper,
3153 	.register_dev = mwifiex_register_dev,
3154 	.unregister_dev = mwifiex_unregister_dev,
3155 	.enable_int = mwifiex_sdio_enable_host_int,
3156 	.disable_int = mwifiex_sdio_disable_host_int,
3157 	.process_int_status = mwifiex_process_int_status,
3158 	.host_to_card = mwifiex_sdio_host_to_card,
3159 	.wakeup = mwifiex_pm_wakeup_card,
3160 	.wakeup_complete = mwifiex_pm_wakeup_card_complete,
3161 
3162 	/* SDIO specific */
3163 	.update_mp_end_port = mwifiex_update_mp_end_port,
3164 	.cleanup_mpa_buf = mwifiex_cleanup_mpa_buf,
3165 	.cmdrsp_complete = mwifiex_sdio_cmdrsp_complete,
3166 	.event_complete = mwifiex_sdio_event_complete,
3167 	.dnld_fw = mwifiex_sdio_dnld_fw,
3168 	.card_reset = mwifiex_sdio_card_reset,
3169 	.reg_dump = mwifiex_sdio_reg_dump,
3170 	.device_dump = mwifiex_sdio_device_dump,
3171 	.deaggr_pkt = mwifiex_deaggr_sdio_pkt,
3172 	.up_dev = mwifiex_sdio_up_dev,
3173 };
3174 
3175 module_driver(mwifiex_sdio, sdio_register_driver, sdio_unregister_driver);
3176 
3177 MODULE_AUTHOR("Marvell International Ltd.");
3178 MODULE_DESCRIPTION("Marvell WiFi-Ex SDIO Driver version " SDIO_VERSION);
3179 MODULE_VERSION(SDIO_VERSION);
3180 MODULE_LICENSE("GPL v2");
3181 MODULE_FIRMWARE(SD8786_DEFAULT_FW_NAME);
3182 MODULE_FIRMWARE(SD8787_DEFAULT_FW_NAME);
3183 MODULE_FIRMWARE(SD8797_DEFAULT_FW_NAME);
3184 MODULE_FIRMWARE(SD8897_DEFAULT_FW_NAME);
3185 MODULE_FIRMWARE(SD8887_DEFAULT_FW_NAME);
3186 MODULE_FIRMWARE(SD8977_DEFAULT_FW_NAME);
3187 MODULE_FIRMWARE(SD8978_SDIOUART_FW_NAME);
3188 MODULE_FIRMWARE(SD8987_DEFAULT_FW_NAME);
3189 MODULE_FIRMWARE(SD8997_DEFAULT_FW_NAME);
3190 MODULE_FIRMWARE(SD8997_SDIOUART_FW_NAME);
3191