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