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 		return -1;
1088 	if (mwifiex_write_reg(adapter, card->reg->host_int_rsr_reg,
1089 			      reg | card->reg->sdio_int_mask))
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 		return -1;
1095 	if (mwifiex_write_reg(adapter, card->reg->card_misc_cfg_reg,
1096 			      reg | AUTO_RE_ENABLE_INT))
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 decodes sdio aggregation 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 aggregation 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 	if (mwifiex_init_sdio_ioport(adapter))
2529 		return -EIO;
2530 
2531 	/* Initialize SDIO variables in card */
2532 	card->mp_rd_bitmap = 0;
2533 	card->mp_wr_bitmap = 0;
2534 	card->curr_rd_port = reg->start_rd_port;
2535 	card->curr_wr_port = reg->start_wr_port;
2536 
2537 	card->mp_data_port_mask = reg->data_port_mask;
2538 
2539 	card->mpa_tx.buf_len = 0;
2540 	card->mpa_tx.pkt_cnt = 0;
2541 	card->mpa_tx.start_port = 0;
2542 
2543 	card->mpa_tx.enabled = 1;
2544 	card->mpa_tx.pkt_aggr_limit = card->mp_agg_pkt_limit;
2545 
2546 	card->mpa_rx.buf_len = 0;
2547 	card->mpa_rx.pkt_cnt = 0;
2548 	card->mpa_rx.start_port = 0;
2549 
2550 	card->mpa_rx.enabled = 1;
2551 	card->mpa_rx.pkt_aggr_limit = card->mp_agg_pkt_limit;
2552 
2553 	/* Allocate buffers for SDIO MP-A */
2554 	card->mp_regs = kzalloc(reg->max_mp_regs, GFP_KERNEL);
2555 	if (!card->mp_regs)
2556 		return -ENOMEM;
2557 
2558 	/* Allocate skb pointer buffers */
2559 	card->mpa_rx.skb_arr = kcalloc(card->mp_agg_pkt_limit, sizeof(void *),
2560 				       GFP_KERNEL);
2561 	if (!card->mpa_rx.skb_arr) {
2562 		kfree(card->mp_regs);
2563 		return -ENOMEM;
2564 	}
2565 
2566 	card->mpa_rx.len_arr = kcalloc(card->mp_agg_pkt_limit,
2567 				       sizeof(*card->mpa_rx.len_arr),
2568 				       GFP_KERNEL);
2569 	if (!card->mpa_rx.len_arr) {
2570 		kfree(card->mp_regs);
2571 		kfree(card->mpa_rx.skb_arr);
2572 		return -ENOMEM;
2573 	}
2574 
2575 	ret = mwifiex_alloc_sdio_mpa_buffers(adapter,
2576 					     card->mp_tx_agg_buf_size,
2577 					     card->mp_rx_agg_buf_size);
2578 
2579 	/* Allocate 32k MPA Tx/Rx buffers if 64k memory allocation fails */
2580 	if (ret && (card->mp_tx_agg_buf_size == MWIFIEX_MP_AGGR_BUF_SIZE_MAX ||
2581 		    card->mp_rx_agg_buf_size == MWIFIEX_MP_AGGR_BUF_SIZE_MAX)) {
2582 		/* Disable rx single port aggregation */
2583 		adapter->host_disable_sdio_rx_aggr = true;
2584 
2585 		ret = mwifiex_alloc_sdio_mpa_buffers
2586 			(adapter, MWIFIEX_MP_AGGR_BUF_SIZE_32K,
2587 			 MWIFIEX_MP_AGGR_BUF_SIZE_32K);
2588 		if (ret) {
2589 			/* Disable multi port aggregation */
2590 			card->mpa_tx.enabled = 0;
2591 			card->mpa_rx.enabled = 0;
2592 		}
2593 	}
2594 
2595 	adapter->auto_tdls = card->can_auto_tdls;
2596 	adapter->ext_scan = card->can_ext_scan;
2597 	return 0;
2598 }
2599 
2600 /*
2601  * This function resets the MPA Tx and Rx buffers.
2602  */
2603 static void mwifiex_cleanup_mpa_buf(struct mwifiex_adapter *adapter)
2604 {
2605 	struct sdio_mmc_card *card = adapter->card;
2606 
2607 	MP_TX_AGGR_BUF_RESET(card);
2608 	MP_RX_AGGR_BUF_RESET(card);
2609 }
2610 
2611 /*
2612  * This function cleans up the allocated card buffers.
2613  *
2614  * The following are freed by this function -
2615  *      - MP registers
2616  *      - MPA Tx buffer
2617  *      - MPA Rx buffer
2618  */
2619 static void mwifiex_cleanup_sdio(struct mwifiex_adapter *adapter)
2620 {
2621 	struct sdio_mmc_card *card = adapter->card;
2622 
2623 	cancel_work_sync(&card->work);
2624 
2625 	kfree(card->mp_regs);
2626 	kfree(card->mpa_rx.skb_arr);
2627 	kfree(card->mpa_rx.len_arr);
2628 	kfree(card->mpa_tx.buf);
2629 	kfree(card->mpa_rx.buf);
2630 }
2631 
2632 /*
2633  * This function updates the MP end port in card.
2634  */
2635 static void
2636 mwifiex_update_mp_end_port(struct mwifiex_adapter *adapter, u16 port)
2637 {
2638 	struct sdio_mmc_card *card = adapter->card;
2639 	const struct mwifiex_sdio_card_reg *reg = card->reg;
2640 	int i;
2641 
2642 	card->mp_end_port = port;
2643 
2644 	card->mp_data_port_mask = reg->data_port_mask;
2645 
2646 	if (reg->start_wr_port) {
2647 		for (i = 1; i <= card->max_ports - card->mp_end_port; i++)
2648 			card->mp_data_port_mask &=
2649 					~(1 << (card->max_ports - i));
2650 	}
2651 
2652 	card->curr_wr_port = reg->start_wr_port;
2653 
2654 	mwifiex_dbg(adapter, CMD,
2655 		    "cmd: mp_end_port %d, data port mask 0x%x\n",
2656 		    port, card->mp_data_port_mask);
2657 }
2658 
2659 static void mwifiex_sdio_card_reset_work(struct mwifiex_adapter *adapter)
2660 {
2661 	struct sdio_mmc_card *card = adapter->card;
2662 	struct sdio_func *func = card->func;
2663 	int ret;
2664 
2665 	/* Prepare the adapter for the reset. */
2666 	mwifiex_shutdown_sw(adapter);
2667 	clear_bit(MWIFIEX_IFACE_WORK_DEVICE_DUMP, &card->work_flags);
2668 	clear_bit(MWIFIEX_IFACE_WORK_CARD_RESET, &card->work_flags);
2669 
2670 	/* Run a HW reset of the SDIO interface. */
2671 	sdio_claim_host(func);
2672 	ret = mmc_hw_reset(func->card);
2673 	sdio_release_host(func);
2674 
2675 	switch (ret) {
2676 	case 1:
2677 		dev_dbg(&func->dev, "SDIO HW reset asynchronous\n");
2678 		complete_all(adapter->fw_done);
2679 		break;
2680 	case 0:
2681 		ret = mwifiex_reinit_sw(adapter);
2682 		if (ret)
2683 			dev_err(&func->dev, "reinit failed: %d\n", ret);
2684 		break;
2685 	default:
2686 		dev_err(&func->dev, "SDIO HW reset failed: %d\n", ret);
2687 		break;
2688 	}
2689 }
2690 
2691 /* This function read/write firmware */
2692 static enum
2693 rdwr_status mwifiex_sdio_rdwr_firmware(struct mwifiex_adapter *adapter,
2694 				       u8 doneflag)
2695 {
2696 	struct sdio_mmc_card *card = adapter->card;
2697 	int ret, tries;
2698 	u8 ctrl_data = 0;
2699 
2700 	sdio_writeb(card->func, card->reg->fw_dump_host_ready,
2701 		    card->reg->fw_dump_ctrl, &ret);
2702 	if (ret) {
2703 		mwifiex_dbg(adapter, ERROR, "SDIO Write ERR\n");
2704 		return RDWR_STATUS_FAILURE;
2705 	}
2706 	for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
2707 		ctrl_data = sdio_readb(card->func, card->reg->fw_dump_ctrl,
2708 				       &ret);
2709 		if (ret) {
2710 			mwifiex_dbg(adapter, ERROR, "SDIO read err\n");
2711 			return RDWR_STATUS_FAILURE;
2712 		}
2713 		if (ctrl_data == FW_DUMP_DONE)
2714 			break;
2715 		if (doneflag && ctrl_data == doneflag)
2716 			return RDWR_STATUS_DONE;
2717 		if (ctrl_data != card->reg->fw_dump_host_ready) {
2718 			mwifiex_dbg(adapter, WARN,
2719 				    "The ctrl reg was changed, re-try again\n");
2720 			sdio_writeb(card->func, card->reg->fw_dump_host_ready,
2721 				    card->reg->fw_dump_ctrl, &ret);
2722 			if (ret) {
2723 				mwifiex_dbg(adapter, ERROR, "SDIO write err\n");
2724 				return RDWR_STATUS_FAILURE;
2725 			}
2726 		}
2727 		usleep_range(100, 200);
2728 	}
2729 	if (ctrl_data == card->reg->fw_dump_host_ready) {
2730 		mwifiex_dbg(adapter, ERROR,
2731 			    "Fail to pull ctrl_data\n");
2732 		return RDWR_STATUS_FAILURE;
2733 	}
2734 
2735 	return RDWR_STATUS_SUCCESS;
2736 }
2737 
2738 /* This function dump firmware memory to file */
2739 static void mwifiex_sdio_fw_dump(struct mwifiex_adapter *adapter)
2740 {
2741 	struct sdio_mmc_card *card = adapter->card;
2742 	int ret = 0;
2743 	unsigned int reg, reg_start, reg_end;
2744 	u8 *dbg_ptr, *end_ptr, dump_num, idx, i, read_reg, doneflag = 0;
2745 	enum rdwr_status stat;
2746 	u32 memory_size;
2747 
2748 	if (!card->can_dump_fw)
2749 		return;
2750 
2751 	for (idx = 0; idx < ARRAY_SIZE(mem_type_mapping_tbl); idx++) {
2752 		struct memory_type_mapping *entry = &mem_type_mapping_tbl[idx];
2753 
2754 		if (entry->mem_ptr) {
2755 			vfree(entry->mem_ptr);
2756 			entry->mem_ptr = NULL;
2757 		}
2758 		entry->mem_size = 0;
2759 	}
2760 
2761 	mwifiex_pm_wakeup_card(adapter);
2762 	sdio_claim_host(card->func);
2763 
2764 	mwifiex_dbg(adapter, MSG, "== mwifiex firmware dump start ==\n");
2765 
2766 	stat = mwifiex_sdio_rdwr_firmware(adapter, doneflag);
2767 	if (stat == RDWR_STATUS_FAILURE)
2768 		goto done;
2769 
2770 	reg = card->reg->fw_dump_start;
2771 	/* Read the number of the memories which will dump */
2772 	dump_num = sdio_readb(card->func, reg, &ret);
2773 	if (ret) {
2774 		mwifiex_dbg(adapter, ERROR, "SDIO read memory length err\n");
2775 		goto done;
2776 	}
2777 
2778 	/* Read the length of every memory which will dump */
2779 	for (idx = 0; idx < dump_num; idx++) {
2780 		struct memory_type_mapping *entry = &mem_type_mapping_tbl[idx];
2781 
2782 		stat = mwifiex_sdio_rdwr_firmware(adapter, doneflag);
2783 		if (stat == RDWR_STATUS_FAILURE)
2784 			goto done;
2785 
2786 		memory_size = 0;
2787 		reg = card->reg->fw_dump_start;
2788 		for (i = 0; i < 4; i++) {
2789 			read_reg = sdio_readb(card->func, reg, &ret);
2790 			if (ret) {
2791 				mwifiex_dbg(adapter, ERROR, "SDIO read err\n");
2792 				goto done;
2793 			}
2794 			memory_size |= (read_reg << i*8);
2795 			reg++;
2796 		}
2797 
2798 		if (memory_size == 0) {
2799 			mwifiex_dbg(adapter, DUMP, "Firmware dump Finished!\n");
2800 			ret = mwifiex_write_reg(adapter,
2801 						card->reg->fw_dump_ctrl,
2802 						FW_DUMP_READ_DONE);
2803 			if (ret) {
2804 				mwifiex_dbg(adapter, ERROR, "SDIO write err\n");
2805 				return;
2806 			}
2807 			break;
2808 		}
2809 
2810 		mwifiex_dbg(adapter, DUMP,
2811 			    "%s_SIZE=0x%x\n", entry->mem_name, memory_size);
2812 		entry->mem_ptr = vmalloc(memory_size + 1);
2813 		entry->mem_size = memory_size;
2814 		if (!entry->mem_ptr) {
2815 			mwifiex_dbg(adapter, ERROR, "Vmalloc %s failed\n",
2816 				    entry->mem_name);
2817 			goto done;
2818 		}
2819 		dbg_ptr = entry->mem_ptr;
2820 		end_ptr = dbg_ptr + memory_size;
2821 
2822 		doneflag = entry->done_flag;
2823 		mwifiex_dbg(adapter, DUMP,
2824 			    "Start %s output, please wait...\n",
2825 			    entry->mem_name);
2826 
2827 		do {
2828 			stat = mwifiex_sdio_rdwr_firmware(adapter, doneflag);
2829 			if (stat == RDWR_STATUS_FAILURE)
2830 				goto done;
2831 
2832 			reg_start = card->reg->fw_dump_start;
2833 			reg_end = card->reg->fw_dump_end;
2834 			for (reg = reg_start; reg <= reg_end; reg++) {
2835 				*dbg_ptr = sdio_readb(card->func, reg, &ret);
2836 				if (ret) {
2837 					mwifiex_dbg(adapter, ERROR,
2838 						    "SDIO read err\n");
2839 					goto done;
2840 				}
2841 				if (dbg_ptr < end_ptr)
2842 					dbg_ptr++;
2843 				else
2844 					mwifiex_dbg(adapter, ERROR,
2845 						    "Allocated buf not enough\n");
2846 			}
2847 
2848 			if (stat != RDWR_STATUS_DONE)
2849 				continue;
2850 
2851 			mwifiex_dbg(adapter, DUMP, "%s done: size=0x%tx\n",
2852 				    entry->mem_name, dbg_ptr - entry->mem_ptr);
2853 			break;
2854 		} while (1);
2855 	}
2856 	mwifiex_dbg(adapter, MSG, "== mwifiex firmware dump end ==\n");
2857 
2858 done:
2859 	sdio_release_host(card->func);
2860 }
2861 
2862 static void mwifiex_sdio_generic_fw_dump(struct mwifiex_adapter *adapter)
2863 {
2864 	struct sdio_mmc_card *card = adapter->card;
2865 	struct memory_type_mapping *entry = &generic_mem_type_map[0];
2866 	unsigned int reg, reg_start, reg_end;
2867 	u8 start_flag = 0, done_flag = 0;
2868 	u8 *dbg_ptr, *end_ptr;
2869 	enum rdwr_status stat;
2870 	int ret = -1, tries;
2871 
2872 	if (!card->fw_dump_enh)
2873 		return;
2874 
2875 	if (entry->mem_ptr) {
2876 		vfree(entry->mem_ptr);
2877 		entry->mem_ptr = NULL;
2878 	}
2879 	entry->mem_size = 0;
2880 
2881 	mwifiex_pm_wakeup_card(adapter);
2882 	sdio_claim_host(card->func);
2883 
2884 	mwifiex_dbg(adapter, MSG, "== mwifiex firmware dump start ==\n");
2885 
2886 	stat = mwifiex_sdio_rdwr_firmware(adapter, done_flag);
2887 	if (stat == RDWR_STATUS_FAILURE)
2888 		goto done;
2889 
2890 	reg_start = card->reg->fw_dump_start;
2891 	reg_end = card->reg->fw_dump_end;
2892 	for (reg = reg_start; reg <= reg_end; reg++) {
2893 		for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
2894 			start_flag = sdio_readb(card->func, reg, &ret);
2895 			if (ret) {
2896 				mwifiex_dbg(adapter, ERROR,
2897 					    "SDIO read err\n");
2898 				goto done;
2899 			}
2900 			if (start_flag == 0)
2901 				break;
2902 			if (tries == MAX_POLL_TRIES) {
2903 				mwifiex_dbg(adapter, ERROR,
2904 					    "FW not ready to dump\n");
2905 				ret = -1;
2906 				goto done;
2907 			}
2908 		}
2909 		usleep_range(100, 200);
2910 	}
2911 
2912 	entry->mem_ptr = vmalloc(0xf0000 + 1);
2913 	if (!entry->mem_ptr) {
2914 		ret = -1;
2915 		goto done;
2916 	}
2917 	dbg_ptr = entry->mem_ptr;
2918 	entry->mem_size = 0xf0000;
2919 	end_ptr = dbg_ptr + entry->mem_size;
2920 
2921 	done_flag = entry->done_flag;
2922 	mwifiex_dbg(adapter, DUMP,
2923 		    "Start %s output, please wait...\n", entry->mem_name);
2924 
2925 	while (true) {
2926 		stat = mwifiex_sdio_rdwr_firmware(adapter, done_flag);
2927 		if (stat == RDWR_STATUS_FAILURE)
2928 			goto done;
2929 		for (reg = reg_start; reg <= reg_end; reg++) {
2930 			*dbg_ptr = sdio_readb(card->func, reg, &ret);
2931 			if (ret) {
2932 				mwifiex_dbg(adapter, ERROR,
2933 					    "SDIO read err\n");
2934 				goto done;
2935 			}
2936 			dbg_ptr++;
2937 			if (dbg_ptr >= end_ptr) {
2938 				u8 *tmp_ptr;
2939 
2940 				tmp_ptr = vmalloc(entry->mem_size + 0x4000 + 1);
2941 				if (!tmp_ptr)
2942 					goto done;
2943 
2944 				memcpy(tmp_ptr, entry->mem_ptr,
2945 				       entry->mem_size);
2946 				vfree(entry->mem_ptr);
2947 				entry->mem_ptr = tmp_ptr;
2948 				tmp_ptr = NULL;
2949 				dbg_ptr = entry->mem_ptr + entry->mem_size;
2950 				entry->mem_size += 0x4000;
2951 				end_ptr = entry->mem_ptr + entry->mem_size;
2952 			}
2953 		}
2954 		if (stat == RDWR_STATUS_DONE) {
2955 			entry->mem_size = dbg_ptr - entry->mem_ptr;
2956 			mwifiex_dbg(adapter, DUMP, "dump %s done size=0x%x\n",
2957 				    entry->mem_name, entry->mem_size);
2958 			ret = 0;
2959 			break;
2960 		}
2961 	}
2962 	mwifiex_dbg(adapter, MSG, "== mwifiex firmware dump end ==\n");
2963 
2964 done:
2965 	if (ret) {
2966 		mwifiex_dbg(adapter, ERROR, "firmware dump failed\n");
2967 		if (entry->mem_ptr) {
2968 			vfree(entry->mem_ptr);
2969 			entry->mem_ptr = NULL;
2970 		}
2971 		entry->mem_size = 0;
2972 	}
2973 	sdio_release_host(card->func);
2974 }
2975 
2976 static void mwifiex_sdio_device_dump_work(struct mwifiex_adapter *adapter)
2977 {
2978 	struct sdio_mmc_card *card = adapter->card;
2979 
2980 	adapter->devdump_data = vzalloc(MWIFIEX_FW_DUMP_SIZE);
2981 	if (!adapter->devdump_data) {
2982 		mwifiex_dbg(adapter, ERROR,
2983 			    "vzalloc devdump data failure!\n");
2984 		return;
2985 	}
2986 
2987 	mwifiex_drv_info_dump(adapter);
2988 	if (card->fw_dump_enh)
2989 		mwifiex_sdio_generic_fw_dump(adapter);
2990 	else
2991 		mwifiex_sdio_fw_dump(adapter);
2992 	mwifiex_prepare_fw_dump_info(adapter);
2993 	mwifiex_upload_device_dump(adapter);
2994 }
2995 
2996 static void mwifiex_sdio_work(struct work_struct *work)
2997 {
2998 	struct sdio_mmc_card *card =
2999 		container_of(work, struct sdio_mmc_card, work);
3000 
3001 	if (test_and_clear_bit(MWIFIEX_IFACE_WORK_DEVICE_DUMP,
3002 			       &card->work_flags))
3003 		mwifiex_sdio_device_dump_work(card->adapter);
3004 	if (test_and_clear_bit(MWIFIEX_IFACE_WORK_CARD_RESET,
3005 			       &card->work_flags))
3006 		mwifiex_sdio_card_reset_work(card->adapter);
3007 }
3008 
3009 /* This function resets the card */
3010 static void mwifiex_sdio_card_reset(struct mwifiex_adapter *adapter)
3011 {
3012 	struct sdio_mmc_card *card = adapter->card;
3013 
3014 	if (!test_and_set_bit(MWIFIEX_IFACE_WORK_CARD_RESET, &card->work_flags))
3015 		schedule_work(&card->work);
3016 }
3017 
3018 /* This function dumps FW information */
3019 static void mwifiex_sdio_device_dump(struct mwifiex_adapter *adapter)
3020 {
3021 	struct sdio_mmc_card *card = adapter->card;
3022 
3023 	if (!test_and_set_bit(MWIFIEX_IFACE_WORK_DEVICE_DUMP,
3024 			      &card->work_flags))
3025 		schedule_work(&card->work);
3026 }
3027 
3028 /* Function to dump SDIO function registers and SDIO scratch registers in case
3029  * of FW crash
3030  */
3031 static int
3032 mwifiex_sdio_reg_dump(struct mwifiex_adapter *adapter, char *drv_buf)
3033 {
3034 	char *p = drv_buf;
3035 	struct sdio_mmc_card *cardp = adapter->card;
3036 	int ret = 0;
3037 	u8 count, func, data, index = 0, size = 0;
3038 	u8 reg, reg_start, reg_end;
3039 	char buf[256], *ptr;
3040 
3041 	if (!p)
3042 		return 0;
3043 
3044 	mwifiex_dbg(adapter, MSG, "SDIO register dump start\n");
3045 
3046 	mwifiex_pm_wakeup_card(adapter);
3047 
3048 	sdio_claim_host(cardp->func);
3049 
3050 	for (count = 0; count < 5; count++) {
3051 		memset(buf, 0, sizeof(buf));
3052 		ptr = buf;
3053 
3054 		switch (count) {
3055 		case 0:
3056 			/* Read the registers of SDIO function0 */
3057 			func = count;
3058 			reg_start = 0;
3059 			reg_end = 9;
3060 			break;
3061 		case 1:
3062 			/* Read the registers of SDIO function1 */
3063 			func = count;
3064 			reg_start = cardp->reg->func1_dump_reg_start;
3065 			reg_end = cardp->reg->func1_dump_reg_end;
3066 			break;
3067 		case 2:
3068 			index = 0;
3069 			func = 1;
3070 			reg_start = cardp->reg->func1_spec_reg_table[index++];
3071 			size = cardp->reg->func1_spec_reg_num;
3072 			reg_end = cardp->reg->func1_spec_reg_table[size-1];
3073 			break;
3074 		default:
3075 			/* Read the scratch registers of SDIO function1 */
3076 			if (count == 4)
3077 				mdelay(100);
3078 			func = 1;
3079 			reg_start = cardp->reg->func1_scratch_reg;
3080 			reg_end = reg_start + MWIFIEX_SDIO_SCRATCH_SIZE;
3081 		}
3082 
3083 		if (count != 2)
3084 			ptr += sprintf(ptr, "SDIO Func%d (%#x-%#x): ",
3085 				       func, reg_start, reg_end);
3086 		else
3087 			ptr += sprintf(ptr, "SDIO Func%d: ", func);
3088 
3089 		for (reg = reg_start; reg <= reg_end;) {
3090 			if (func == 0)
3091 				data = sdio_f0_readb(cardp->func, reg, &ret);
3092 			else
3093 				data = sdio_readb(cardp->func, reg, &ret);
3094 
3095 			if (count == 2)
3096 				ptr += sprintf(ptr, "(%#x) ", reg);
3097 			if (!ret) {
3098 				ptr += sprintf(ptr, "%02x ", data);
3099 			} else {
3100 				ptr += sprintf(ptr, "ERR");
3101 				break;
3102 			}
3103 
3104 			if (count == 2 && reg < reg_end)
3105 				reg = cardp->reg->func1_spec_reg_table[index++];
3106 			else
3107 				reg++;
3108 		}
3109 
3110 		mwifiex_dbg(adapter, MSG, "%s\n", buf);
3111 		p += sprintf(p, "%s\n", buf);
3112 	}
3113 
3114 	sdio_release_host(cardp->func);
3115 
3116 	mwifiex_dbg(adapter, MSG, "SDIO register dump end\n");
3117 
3118 	return p - drv_buf;
3119 }
3120 
3121 /* sdio device/function initialization, code is extracted
3122  * from init_if handler and register_dev handler.
3123  */
3124 static void mwifiex_sdio_up_dev(struct mwifiex_adapter *adapter)
3125 {
3126 	struct sdio_mmc_card *card = adapter->card;
3127 	u8 sdio_ireg;
3128 
3129 	sdio_claim_host(card->func);
3130 	sdio_enable_func(card->func);
3131 	sdio_set_block_size(card->func, MWIFIEX_SDIO_BLOCK_SIZE);
3132 	sdio_release_host(card->func);
3133 
3134 	/* tx_buf_size might be changed to 3584 by firmware during
3135 	 * data transfer, we will reset to default size.
3136 	 */
3137 	adapter->tx_buf_size = card->tx_buf_size;
3138 
3139 	/* Read the host_int_status_reg for ACK the first interrupt got
3140 	 * from the bootloader. If we don't do this we get a interrupt
3141 	 * as soon as we register the irq.
3142 	 */
3143 	mwifiex_read_reg(adapter, card->reg->host_int_status_reg, &sdio_ireg);
3144 
3145 	if (mwifiex_init_sdio_ioport(adapter))
3146 		dev_err(&card->func->dev, "error enabling SDIO port\n");
3147 }
3148 
3149 static struct mwifiex_if_ops sdio_ops = {
3150 	.init_if = mwifiex_init_sdio,
3151 	.cleanup_if = mwifiex_cleanup_sdio,
3152 	.check_fw_status = mwifiex_check_fw_status,
3153 	.check_winner_status = mwifiex_check_winner_status,
3154 	.prog_fw = mwifiex_prog_fw_w_helper,
3155 	.register_dev = mwifiex_register_dev,
3156 	.unregister_dev = mwifiex_unregister_dev,
3157 	.enable_int = mwifiex_sdio_enable_host_int,
3158 	.disable_int = mwifiex_sdio_disable_host_int,
3159 	.process_int_status = mwifiex_process_int_status,
3160 	.host_to_card = mwifiex_sdio_host_to_card,
3161 	.wakeup = mwifiex_pm_wakeup_card,
3162 	.wakeup_complete = mwifiex_pm_wakeup_card_complete,
3163 
3164 	/* SDIO specific */
3165 	.update_mp_end_port = mwifiex_update_mp_end_port,
3166 	.cleanup_mpa_buf = mwifiex_cleanup_mpa_buf,
3167 	.cmdrsp_complete = mwifiex_sdio_cmdrsp_complete,
3168 	.event_complete = mwifiex_sdio_event_complete,
3169 	.dnld_fw = mwifiex_sdio_dnld_fw,
3170 	.card_reset = mwifiex_sdio_card_reset,
3171 	.reg_dump = mwifiex_sdio_reg_dump,
3172 	.device_dump = mwifiex_sdio_device_dump,
3173 	.deaggr_pkt = mwifiex_deaggr_sdio_pkt,
3174 	.up_dev = mwifiex_sdio_up_dev,
3175 };
3176 
3177 module_driver(mwifiex_sdio, sdio_register_driver, sdio_unregister_driver);
3178 
3179 MODULE_AUTHOR("Marvell International Ltd.");
3180 MODULE_DESCRIPTION("Marvell WiFi-Ex SDIO Driver version " SDIO_VERSION);
3181 MODULE_VERSION(SDIO_VERSION);
3182 MODULE_LICENSE("GPL v2");
3183 MODULE_FIRMWARE(SD8786_DEFAULT_FW_NAME);
3184 MODULE_FIRMWARE(SD8787_DEFAULT_FW_NAME);
3185 MODULE_FIRMWARE(SD8797_DEFAULT_FW_NAME);
3186 MODULE_FIRMWARE(SD8897_DEFAULT_FW_NAME);
3187 MODULE_FIRMWARE(SD8887_DEFAULT_FW_NAME);
3188 MODULE_FIRMWARE(SD8977_DEFAULT_FW_NAME);
3189 MODULE_FIRMWARE(SD8978_SDIOUART_FW_NAME);
3190 MODULE_FIRMWARE(SD8987_DEFAULT_FW_NAME);
3191 MODULE_FIRMWARE(SD8997_DEFAULT_FW_NAME);
3192 MODULE_FIRMWARE(SD8997_SDIOUART_FW_NAME);
3193