1 /*
2  * Copyright (c) 2004-2011 Atheros Communications Inc.
3  * Copyright (c) 2011-2012,2017 Qualcomm Atheros, Inc.
4  * Copyright (c) 2016-2017 Erik Stromdahl <erik.stromdahl@gmail.com>
5  *
6  * Permission to use, copy, modify, and/or distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 #include <linux/module.h>
20 #include <linux/mmc/card.h>
21 #include <linux/mmc/mmc.h>
22 #include <linux/mmc/host.h>
23 #include <linux/mmc/sdio_func.h>
24 #include <linux/mmc/sdio_ids.h>
25 #include <linux/mmc/sdio.h>
26 #include <linux/mmc/sd.h>
27 #include <linux/bitfield.h>
28 #include "core.h"
29 #include "bmi.h"
30 #include "debug.h"
31 #include "hif.h"
32 #include "htc.h"
33 #include "mac.h"
34 #include "targaddrs.h"
35 #include "trace.h"
36 #include "sdio.h"
37 
38 /* inlined helper functions */
39 
40 static inline int ath10k_sdio_calc_txrx_padded_len(struct ath10k_sdio *ar_sdio,
41 						   size_t len)
42 {
43 	return __ALIGN_MASK((len), ar_sdio->mbox_info.block_mask);
44 }
45 
46 static inline enum ath10k_htc_ep_id pipe_id_to_eid(u8 pipe_id)
47 {
48 	return (enum ath10k_htc_ep_id)pipe_id;
49 }
50 
51 static inline void ath10k_sdio_mbox_free_rx_pkt(struct ath10k_sdio_rx_data *pkt)
52 {
53 	dev_kfree_skb(pkt->skb);
54 	pkt->skb = NULL;
55 	pkt->alloc_len = 0;
56 	pkt->act_len = 0;
57 	pkt->trailer_only = false;
58 }
59 
60 static inline int ath10k_sdio_mbox_alloc_rx_pkt(struct ath10k_sdio_rx_data *pkt,
61 						size_t act_len, size_t full_len,
62 						bool part_of_bundle,
63 						bool last_in_bundle)
64 {
65 	pkt->skb = dev_alloc_skb(full_len);
66 	if (!pkt->skb)
67 		return -ENOMEM;
68 
69 	pkt->act_len = act_len;
70 	pkt->alloc_len = full_len;
71 	pkt->part_of_bundle = part_of_bundle;
72 	pkt->last_in_bundle = last_in_bundle;
73 	pkt->trailer_only = false;
74 
75 	return 0;
76 }
77 
78 static inline bool is_trailer_only_msg(struct ath10k_sdio_rx_data *pkt)
79 {
80 	bool trailer_only = false;
81 	struct ath10k_htc_hdr *htc_hdr =
82 		(struct ath10k_htc_hdr *)pkt->skb->data;
83 	u16 len = __le16_to_cpu(htc_hdr->len);
84 
85 	if (len == htc_hdr->trailer_len)
86 		trailer_only = true;
87 
88 	return trailer_only;
89 }
90 
91 /* sdio/mmc functions */
92 
93 static inline void ath10k_sdio_set_cmd52_arg(u32 *arg, u8 write, u8 raw,
94 					     unsigned int address,
95 					     unsigned char val)
96 {
97 	*arg = FIELD_PREP(BIT(31), write) |
98 	       FIELD_PREP(BIT(27), raw) |
99 	       FIELD_PREP(BIT(26), 1) |
100 	       FIELD_PREP(GENMASK(25, 9), address) |
101 	       FIELD_PREP(BIT(8), 1) |
102 	       FIELD_PREP(GENMASK(7, 0), val);
103 }
104 
105 static int ath10k_sdio_func0_cmd52_wr_byte(struct mmc_card *card,
106 					   unsigned int address,
107 					   unsigned char byte)
108 {
109 	struct mmc_command io_cmd;
110 
111 	memset(&io_cmd, 0, sizeof(io_cmd));
112 	ath10k_sdio_set_cmd52_arg(&io_cmd.arg, 1, 0, address, byte);
113 	io_cmd.opcode = SD_IO_RW_DIRECT;
114 	io_cmd.flags = MMC_RSP_R5 | MMC_CMD_AC;
115 
116 	return mmc_wait_for_cmd(card->host, &io_cmd, 0);
117 }
118 
119 static int ath10k_sdio_func0_cmd52_rd_byte(struct mmc_card *card,
120 					   unsigned int address,
121 					   unsigned char *byte)
122 {
123 	struct mmc_command io_cmd;
124 	int ret;
125 
126 	memset(&io_cmd, 0, sizeof(io_cmd));
127 	ath10k_sdio_set_cmd52_arg(&io_cmd.arg, 0, 0, address, 0);
128 	io_cmd.opcode = SD_IO_RW_DIRECT;
129 	io_cmd.flags = MMC_RSP_R5 | MMC_CMD_AC;
130 
131 	ret = mmc_wait_for_cmd(card->host, &io_cmd, 0);
132 	if (!ret)
133 		*byte = io_cmd.resp[0];
134 
135 	return ret;
136 }
137 
138 static int ath10k_sdio_config(struct ath10k *ar)
139 {
140 	struct ath10k_sdio *ar_sdio = ath10k_sdio_priv(ar);
141 	struct sdio_func *func = ar_sdio->func;
142 	unsigned char byte, asyncintdelay = 2;
143 	int ret;
144 
145 	ath10k_dbg(ar, ATH10K_DBG_BOOT, "sdio configuration\n");
146 
147 	sdio_claim_host(func);
148 
149 	byte = 0;
150 	ret = ath10k_sdio_func0_cmd52_rd_byte(func->card,
151 					      SDIO_CCCR_DRIVE_STRENGTH,
152 					      &byte);
153 
154 	byte &= ~ATH10K_SDIO_DRIVE_DTSX_MASK;
155 	byte |= FIELD_PREP(ATH10K_SDIO_DRIVE_DTSX_MASK,
156 			   ATH10K_SDIO_DRIVE_DTSX_TYPE_D);
157 
158 	ret = ath10k_sdio_func0_cmd52_wr_byte(func->card,
159 					      SDIO_CCCR_DRIVE_STRENGTH,
160 					      byte);
161 
162 	byte = 0;
163 	ret = ath10k_sdio_func0_cmd52_rd_byte(
164 		func->card,
165 		CCCR_SDIO_DRIVER_STRENGTH_ENABLE_ADDR,
166 		&byte);
167 
168 	byte |= (CCCR_SDIO_DRIVER_STRENGTH_ENABLE_A |
169 		 CCCR_SDIO_DRIVER_STRENGTH_ENABLE_C |
170 		 CCCR_SDIO_DRIVER_STRENGTH_ENABLE_D);
171 
172 	ret = ath10k_sdio_func0_cmd52_wr_byte(func->card,
173 					      CCCR_SDIO_DRIVER_STRENGTH_ENABLE_ADDR,
174 					      byte);
175 	if (ret) {
176 		ath10k_warn(ar, "failed to enable driver strength: %d\n", ret);
177 		goto out;
178 	}
179 
180 	byte = 0;
181 	ret = ath10k_sdio_func0_cmd52_rd_byte(func->card,
182 					      CCCR_SDIO_IRQ_MODE_REG_SDIO3,
183 					      &byte);
184 
185 	byte |= SDIO_IRQ_MODE_ASYNC_4BIT_IRQ_SDIO3;
186 
187 	ret = ath10k_sdio_func0_cmd52_wr_byte(func->card,
188 					      CCCR_SDIO_IRQ_MODE_REG_SDIO3,
189 					      byte);
190 	if (ret) {
191 		ath10k_warn(ar, "failed to enable 4-bit async irq mode: %d\n",
192 			    ret);
193 		goto out;
194 	}
195 
196 	byte = 0;
197 	ret = ath10k_sdio_func0_cmd52_rd_byte(func->card,
198 					      CCCR_SDIO_ASYNC_INT_DELAY_ADDRESS,
199 					      &byte);
200 
201 	byte &= ~CCCR_SDIO_ASYNC_INT_DELAY_MASK;
202 	byte |= FIELD_PREP(CCCR_SDIO_ASYNC_INT_DELAY_MASK, asyncintdelay);
203 
204 	ret = ath10k_sdio_func0_cmd52_wr_byte(func->card,
205 					      CCCR_SDIO_ASYNC_INT_DELAY_ADDRESS,
206 					      byte);
207 
208 	/* give us some time to enable, in ms */
209 	func->enable_timeout = 100;
210 
211 	ret = sdio_set_block_size(func, ar_sdio->mbox_info.block_size);
212 	if (ret) {
213 		ath10k_warn(ar, "failed to set sdio block size to %d: %d\n",
214 			    ar_sdio->mbox_info.block_size, ret);
215 		goto out;
216 	}
217 
218 out:
219 	sdio_release_host(func);
220 	return ret;
221 }
222 
223 static int ath10k_sdio_write32(struct ath10k *ar, u32 addr, u32 val)
224 {
225 	struct ath10k_sdio *ar_sdio = ath10k_sdio_priv(ar);
226 	struct sdio_func *func = ar_sdio->func;
227 	int ret;
228 
229 	sdio_claim_host(func);
230 
231 	sdio_writel(func, val, addr, &ret);
232 	if (ret) {
233 		ath10k_warn(ar, "failed to write 0x%x to address 0x%x: %d\n",
234 			    val, addr, ret);
235 		goto out;
236 	}
237 
238 	ath10k_dbg(ar, ATH10K_DBG_SDIO, "sdio write32 addr 0x%x val 0x%x\n",
239 		   addr, val);
240 
241 out:
242 	sdio_release_host(func);
243 
244 	return ret;
245 }
246 
247 static int ath10k_sdio_writesb32(struct ath10k *ar, u32 addr, u32 val)
248 {
249 	struct ath10k_sdio *ar_sdio = ath10k_sdio_priv(ar);
250 	struct sdio_func *func = ar_sdio->func;
251 	__le32 *buf;
252 	int ret;
253 
254 	buf = kzalloc(sizeof(*buf), GFP_KERNEL);
255 	if (!buf)
256 		return -ENOMEM;
257 
258 	*buf = cpu_to_le32(val);
259 
260 	sdio_claim_host(func);
261 
262 	ret = sdio_writesb(func, addr, buf, sizeof(*buf));
263 	if (ret) {
264 		ath10k_warn(ar, "failed to write value 0x%x to fixed sb address 0x%x: %d\n",
265 			    val, addr, ret);
266 		goto out;
267 	}
268 
269 	ath10k_dbg(ar, ATH10K_DBG_SDIO, "sdio writesb32 addr 0x%x val 0x%x\n",
270 		   addr, val);
271 
272 out:
273 	sdio_release_host(func);
274 
275 	kfree(buf);
276 
277 	return ret;
278 }
279 
280 static int ath10k_sdio_read32(struct ath10k *ar, u32 addr, u32 *val)
281 {
282 	struct ath10k_sdio *ar_sdio = ath10k_sdio_priv(ar);
283 	struct sdio_func *func = ar_sdio->func;
284 	int ret;
285 
286 	sdio_claim_host(func);
287 	*val = sdio_readl(func, addr, &ret);
288 	if (ret) {
289 		ath10k_warn(ar, "failed to read from address 0x%x: %d\n",
290 			    addr, ret);
291 		goto out;
292 	}
293 
294 	ath10k_dbg(ar, ATH10K_DBG_SDIO, "sdio read32 addr 0x%x val 0x%x\n",
295 		   addr, *val);
296 
297 out:
298 	sdio_release_host(func);
299 
300 	return ret;
301 }
302 
303 static int ath10k_sdio_read(struct ath10k *ar, u32 addr, void *buf, size_t len)
304 {
305 	struct ath10k_sdio *ar_sdio = ath10k_sdio_priv(ar);
306 	struct sdio_func *func = ar_sdio->func;
307 	int ret;
308 
309 	sdio_claim_host(func);
310 
311 	ret = sdio_memcpy_fromio(func, buf, addr, len);
312 	if (ret) {
313 		ath10k_warn(ar, "failed to read from address 0x%x: %d\n",
314 			    addr, ret);
315 		goto out;
316 	}
317 
318 	ath10k_dbg(ar, ATH10K_DBG_SDIO, "sdio read addr 0x%x buf 0x%p len %zu\n",
319 		   addr, buf, len);
320 	ath10k_dbg_dump(ar, ATH10K_DBG_SDIO_DUMP, NULL, "sdio read ", buf, len);
321 
322 out:
323 	sdio_release_host(func);
324 
325 	return ret;
326 }
327 
328 static int ath10k_sdio_write(struct ath10k *ar, u32 addr, const void *buf, size_t len)
329 {
330 	struct ath10k_sdio *ar_sdio = ath10k_sdio_priv(ar);
331 	struct sdio_func *func = ar_sdio->func;
332 	int ret;
333 
334 	sdio_claim_host(func);
335 
336 	/* For some reason toio() doesn't have const for the buffer, need
337 	 * an ugly hack to workaround that.
338 	 */
339 	ret = sdio_memcpy_toio(func, addr, (void *)buf, len);
340 	if (ret) {
341 		ath10k_warn(ar, "failed to write to address 0x%x: %d\n",
342 			    addr, ret);
343 		goto out;
344 	}
345 
346 	ath10k_dbg(ar, ATH10K_DBG_SDIO, "sdio write addr 0x%x buf 0x%p len %zu\n",
347 		   addr, buf, len);
348 	ath10k_dbg_dump(ar, ATH10K_DBG_SDIO_DUMP, NULL, "sdio write ", buf, len);
349 
350 out:
351 	sdio_release_host(func);
352 
353 	return ret;
354 }
355 
356 static int ath10k_sdio_readsb(struct ath10k *ar, u32 addr, void *buf, size_t len)
357 {
358 	struct ath10k_sdio *ar_sdio = ath10k_sdio_priv(ar);
359 	struct sdio_func *func = ar_sdio->func;
360 	int ret;
361 
362 	sdio_claim_host(func);
363 
364 	len = round_down(len, ar_sdio->mbox_info.block_size);
365 
366 	ret = sdio_readsb(func, buf, addr, len);
367 	if (ret) {
368 		ath10k_warn(ar, "failed to read from fixed (sb) address 0x%x: %d\n",
369 			    addr, ret);
370 		goto out;
371 	}
372 
373 	ath10k_dbg(ar, ATH10K_DBG_SDIO, "sdio readsb addr 0x%x buf 0x%p len %zu\n",
374 		   addr, buf, len);
375 	ath10k_dbg_dump(ar, ATH10K_DBG_SDIO_DUMP, NULL, "sdio readsb ", buf, len);
376 
377 out:
378 	sdio_release_host(func);
379 
380 	return ret;
381 }
382 
383 /* HIF mbox functions */
384 
385 static int ath10k_sdio_mbox_rx_process_packet(struct ath10k *ar,
386 					      struct ath10k_sdio_rx_data *pkt,
387 					      u32 *lookaheads,
388 					      int *n_lookaheads)
389 {
390 	struct ath10k_htc *htc = &ar->htc;
391 	struct sk_buff *skb = pkt->skb;
392 	struct ath10k_htc_hdr *htc_hdr = (struct ath10k_htc_hdr *)skb->data;
393 	bool trailer_present = htc_hdr->flags & ATH10K_HTC_FLAG_TRAILER_PRESENT;
394 	enum ath10k_htc_ep_id eid;
395 	u16 payload_len;
396 	u8 *trailer;
397 	int ret;
398 
399 	payload_len = le16_to_cpu(htc_hdr->len);
400 
401 	if (trailer_present) {
402 		trailer = skb->data + sizeof(*htc_hdr) +
403 			  payload_len - htc_hdr->trailer_len;
404 
405 		eid = pipe_id_to_eid(htc_hdr->eid);
406 
407 		ret = ath10k_htc_process_trailer(htc,
408 						 trailer,
409 						 htc_hdr->trailer_len,
410 						 eid,
411 						 lookaheads,
412 						 n_lookaheads);
413 		if (ret)
414 			return ret;
415 
416 		if (is_trailer_only_msg(pkt))
417 			pkt->trailer_only = true;
418 
419 		skb_trim(skb, skb->len - htc_hdr->trailer_len);
420 	}
421 
422 	skb_pull(skb, sizeof(*htc_hdr));
423 
424 	return 0;
425 }
426 
427 static int ath10k_sdio_mbox_rx_process_packets(struct ath10k *ar,
428 					       u32 lookaheads[],
429 					       int *n_lookahead)
430 {
431 	struct ath10k_sdio *ar_sdio = ath10k_sdio_priv(ar);
432 	struct ath10k_htc *htc = &ar->htc;
433 	struct ath10k_sdio_rx_data *pkt;
434 	struct ath10k_htc_ep *ep;
435 	enum ath10k_htc_ep_id id;
436 	int ret, i, *n_lookahead_local;
437 	u32 *lookaheads_local;
438 	int lookahead_idx = 0;
439 
440 	for (i = 0; i < ar_sdio->n_rx_pkts; i++) {
441 		lookaheads_local = lookaheads;
442 		n_lookahead_local = n_lookahead;
443 
444 		id = ((struct ath10k_htc_hdr *)
445 		      &lookaheads[lookahead_idx++])->eid;
446 
447 		if (id >= ATH10K_HTC_EP_COUNT) {
448 			ath10k_warn(ar, "invalid endpoint in look-ahead: %d\n",
449 				    id);
450 			ret = -ENOMEM;
451 			goto out;
452 		}
453 
454 		ep = &htc->endpoint[id];
455 
456 		if (ep->service_id == 0) {
457 			ath10k_warn(ar, "ep %d is not connected\n", id);
458 			ret = -ENOMEM;
459 			goto out;
460 		}
461 
462 		pkt = &ar_sdio->rx_pkts[i];
463 
464 		if (pkt->part_of_bundle && !pkt->last_in_bundle) {
465 			/* Only read lookahead's from RX trailers
466 			 * for the last packet in a bundle.
467 			 */
468 			lookahead_idx--;
469 			lookaheads_local = NULL;
470 			n_lookahead_local = NULL;
471 		}
472 
473 		ret = ath10k_sdio_mbox_rx_process_packet(ar,
474 							 pkt,
475 							 lookaheads_local,
476 							 n_lookahead_local);
477 		if (ret)
478 			goto out;
479 
480 		if (!pkt->trailer_only)
481 			ep->ep_ops.ep_rx_complete(ar_sdio->ar, pkt->skb);
482 		else
483 			kfree_skb(pkt->skb);
484 
485 		/* The RX complete handler now owns the skb...*/
486 		pkt->skb = NULL;
487 		pkt->alloc_len = 0;
488 	}
489 
490 	ret = 0;
491 
492 out:
493 	/* Free all packets that was not passed on to the RX completion
494 	 * handler...
495 	 */
496 	for (; i < ar_sdio->n_rx_pkts; i++)
497 		ath10k_sdio_mbox_free_rx_pkt(&ar_sdio->rx_pkts[i]);
498 
499 	return ret;
500 }
501 
502 static int ath10k_sdio_mbox_alloc_pkt_bundle(struct ath10k *ar,
503 					     struct ath10k_sdio_rx_data *rx_pkts,
504 					     struct ath10k_htc_hdr *htc_hdr,
505 					     size_t full_len, size_t act_len,
506 					     size_t *bndl_cnt)
507 {
508 	int ret, i;
509 
510 	*bndl_cnt = FIELD_GET(ATH10K_HTC_FLAG_BUNDLE_MASK, htc_hdr->flags);
511 
512 	if (*bndl_cnt > HTC_HOST_MAX_MSG_PER_RX_BUNDLE) {
513 		ath10k_warn(ar,
514 			    "HTC bundle length %u exceeds maximum %u\n",
515 			    le16_to_cpu(htc_hdr->len),
516 			    HTC_HOST_MAX_MSG_PER_RX_BUNDLE);
517 		return -ENOMEM;
518 	}
519 
520 	/* Allocate bndl_cnt extra skb's for the bundle.
521 	 * The package containing the
522 	 * ATH10K_HTC_FLAG_BUNDLE_MASK flag is not included
523 	 * in bndl_cnt. The skb for that packet will be
524 	 * allocated separately.
525 	 */
526 	for (i = 0; i < *bndl_cnt; i++) {
527 		ret = ath10k_sdio_mbox_alloc_rx_pkt(&rx_pkts[i],
528 						    act_len,
529 						    full_len,
530 						    true,
531 						    false);
532 		if (ret)
533 			return ret;
534 	}
535 
536 	return 0;
537 }
538 
539 static int ath10k_sdio_mbox_rx_alloc(struct ath10k *ar,
540 				     u32 lookaheads[], int n_lookaheads)
541 {
542 	struct ath10k_sdio *ar_sdio = ath10k_sdio_priv(ar);
543 	struct ath10k_htc_hdr *htc_hdr;
544 	size_t full_len, act_len;
545 	bool last_in_bundle;
546 	int ret, i;
547 
548 	if (n_lookaheads > ATH10K_SDIO_MAX_RX_MSGS) {
549 		ath10k_warn(ar,
550 			    "the total number of pkgs to be fetched (%u) exceeds maximum %u\n",
551 			    n_lookaheads,
552 			    ATH10K_SDIO_MAX_RX_MSGS);
553 		ret = -ENOMEM;
554 		goto err;
555 	}
556 
557 	for (i = 0; i < n_lookaheads; i++) {
558 		htc_hdr = (struct ath10k_htc_hdr *)&lookaheads[i];
559 		last_in_bundle = false;
560 
561 		if (le16_to_cpu(htc_hdr->len) >
562 		    ATH10K_HTC_MBOX_MAX_PAYLOAD_LENGTH) {
563 			ath10k_warn(ar,
564 				    "payload length %d exceeds max htc length: %zu\n",
565 				    le16_to_cpu(htc_hdr->len),
566 				    ATH10K_HTC_MBOX_MAX_PAYLOAD_LENGTH);
567 			ret = -ENOMEM;
568 			goto err;
569 		}
570 
571 		act_len = le16_to_cpu(htc_hdr->len) + sizeof(*htc_hdr);
572 		full_len = ath10k_sdio_calc_txrx_padded_len(ar_sdio, act_len);
573 
574 		if (full_len > ATH10K_SDIO_MAX_BUFFER_SIZE) {
575 			ath10k_warn(ar,
576 				    "rx buffer requested with invalid htc_hdr length (%d, 0x%x): %d\n",
577 				    htc_hdr->eid, htc_hdr->flags,
578 				    le16_to_cpu(htc_hdr->len));
579 			ret = -EINVAL;
580 			goto err;
581 		}
582 
583 		if (htc_hdr->flags & ATH10K_HTC_FLAG_BUNDLE_MASK) {
584 			/* HTC header indicates that every packet to follow
585 			 * has the same padded length so that it can be
586 			 * optimally fetched as a full bundle.
587 			 */
588 			size_t bndl_cnt;
589 
590 			ret = ath10k_sdio_mbox_alloc_pkt_bundle(ar,
591 								&ar_sdio->rx_pkts[i],
592 								htc_hdr,
593 								full_len,
594 								act_len,
595 								&bndl_cnt);
596 
597 			n_lookaheads += bndl_cnt;
598 			i += bndl_cnt;
599 			/*Next buffer will be the last in the bundle */
600 			last_in_bundle = true;
601 		}
602 
603 		/* Allocate skb for packet. If the packet had the
604 		 * ATH10K_HTC_FLAG_BUNDLE_MASK flag set, all bundled
605 		 * packet skb's have been allocated in the previous step.
606 		 */
607 		if (htc_hdr->flags & ATH10K_HTC_FLAGS_RECV_1MORE_BLOCK)
608 			full_len += ATH10K_HIF_MBOX_BLOCK_SIZE;
609 
610 		ret = ath10k_sdio_mbox_alloc_rx_pkt(&ar_sdio->rx_pkts[i],
611 						    act_len,
612 						    full_len,
613 						    last_in_bundle,
614 						    last_in_bundle);
615 	}
616 
617 	ar_sdio->n_rx_pkts = i;
618 
619 	return 0;
620 
621 err:
622 	for (i = 0; i < ATH10K_SDIO_MAX_RX_MSGS; i++) {
623 		if (!ar_sdio->rx_pkts[i].alloc_len)
624 			break;
625 		ath10k_sdio_mbox_free_rx_pkt(&ar_sdio->rx_pkts[i]);
626 	}
627 
628 	return ret;
629 }
630 
631 static int ath10k_sdio_mbox_rx_packet(struct ath10k *ar,
632 				      struct ath10k_sdio_rx_data *pkt)
633 {
634 	struct ath10k_sdio *ar_sdio = ath10k_sdio_priv(ar);
635 	struct sk_buff *skb = pkt->skb;
636 	int ret;
637 
638 	ret = ath10k_sdio_readsb(ar, ar_sdio->mbox_info.htc_addr,
639 				 skb->data, pkt->alloc_len);
640 	pkt->status = ret;
641 	if (!ret)
642 		skb_put(skb, pkt->act_len);
643 
644 	return ret;
645 }
646 
647 static int ath10k_sdio_mbox_rx_fetch(struct ath10k *ar)
648 {
649 	struct ath10k_sdio *ar_sdio = ath10k_sdio_priv(ar);
650 	int ret, i;
651 
652 	for (i = 0; i < ar_sdio->n_rx_pkts; i++) {
653 		ret = ath10k_sdio_mbox_rx_packet(ar,
654 						 &ar_sdio->rx_pkts[i]);
655 		if (ret)
656 			goto err;
657 	}
658 
659 	return 0;
660 
661 err:
662 	/* Free all packets that was not successfully fetched. */
663 	for (; i < ar_sdio->n_rx_pkts; i++)
664 		ath10k_sdio_mbox_free_rx_pkt(&ar_sdio->rx_pkts[i]);
665 
666 	return ret;
667 }
668 
669 /* This is the timeout for mailbox processing done in the sdio irq
670  * handler. The timeout is deliberately set quite high since SDIO dump logs
671  * over serial port can/will add a substantial overhead to the processing
672  * (if enabled).
673  */
674 #define SDIO_MBOX_PROCESSING_TIMEOUT_HZ (20 * HZ)
675 
676 static int ath10k_sdio_mbox_rxmsg_pending_handler(struct ath10k *ar,
677 						  u32 msg_lookahead, bool *done)
678 {
679 	struct ath10k_sdio *ar_sdio = ath10k_sdio_priv(ar);
680 	u32 lookaheads[ATH10K_SDIO_MAX_RX_MSGS];
681 	int n_lookaheads = 1;
682 	unsigned long timeout;
683 	int ret;
684 
685 	*done = true;
686 
687 	/* Copy the lookahead obtained from the HTC register table into our
688 	 * temp array as a start value.
689 	 */
690 	lookaheads[0] = msg_lookahead;
691 
692 	timeout = jiffies + SDIO_MBOX_PROCESSING_TIMEOUT_HZ;
693 	do {
694 		/* Try to allocate as many HTC RX packets indicated by
695 		 * n_lookaheads.
696 		 */
697 		ret = ath10k_sdio_mbox_rx_alloc(ar, lookaheads,
698 						n_lookaheads);
699 		if (ret)
700 			break;
701 
702 		if (ar_sdio->n_rx_pkts >= 2)
703 			/* A recv bundle was detected, force IRQ status
704 			 * re-check again.
705 			 */
706 			*done = false;
707 
708 		ret = ath10k_sdio_mbox_rx_fetch(ar);
709 
710 		/* Process fetched packets. This will potentially update
711 		 * n_lookaheads depending on if the packets contain lookahead
712 		 * reports.
713 		 */
714 		n_lookaheads = 0;
715 		ret = ath10k_sdio_mbox_rx_process_packets(ar,
716 							  lookaheads,
717 							  &n_lookaheads);
718 
719 		if (!n_lookaheads || ret)
720 			break;
721 
722 		/* For SYNCH processing, if we get here, we are running
723 		 * through the loop again due to updated lookaheads. Set
724 		 * flag that we should re-check IRQ status registers again
725 		 * before leaving IRQ processing, this can net better
726 		 * performance in high throughput situations.
727 		 */
728 		*done = false;
729 	} while (time_before(jiffies, timeout));
730 
731 	if (ret && (ret != -ECANCELED))
732 		ath10k_warn(ar, "failed to get pending recv messages: %d\n",
733 			    ret);
734 
735 	return ret;
736 }
737 
738 static int ath10k_sdio_mbox_proc_dbg_intr(struct ath10k *ar)
739 {
740 	u32 val;
741 	int ret;
742 
743 	/* TODO: Add firmware crash handling */
744 	ath10k_warn(ar, "firmware crashed\n");
745 
746 	/* read counter to clear the interrupt, the debug error interrupt is
747 	 * counter 0.
748 	 */
749 	ret = ath10k_sdio_read32(ar, MBOX_COUNT_DEC_ADDRESS, &val);
750 	if (ret)
751 		ath10k_warn(ar, "failed to clear debug interrupt: %d\n", ret);
752 
753 	return ret;
754 }
755 
756 static int ath10k_sdio_mbox_proc_counter_intr(struct ath10k *ar)
757 {
758 	struct ath10k_sdio *ar_sdio = ath10k_sdio_priv(ar);
759 	struct ath10k_sdio_irq_data *irq_data = &ar_sdio->irq_data;
760 	u8 counter_int_status;
761 	int ret;
762 
763 	mutex_lock(&irq_data->mtx);
764 	counter_int_status = irq_data->irq_proc_reg->counter_int_status &
765 			     irq_data->irq_en_reg->cntr_int_status_en;
766 
767 	/* NOTE: other modules like GMBOX may use the counter interrupt for
768 	 * credit flow control on other counters, we only need to check for
769 	 * the debug assertion counter interrupt.
770 	 */
771 	if (counter_int_status & ATH10K_SDIO_TARGET_DEBUG_INTR_MASK)
772 		ret = ath10k_sdio_mbox_proc_dbg_intr(ar);
773 	else
774 		ret = 0;
775 
776 	mutex_unlock(&irq_data->mtx);
777 
778 	return ret;
779 }
780 
781 static int ath10k_sdio_mbox_proc_err_intr(struct ath10k *ar)
782 {
783 	struct ath10k_sdio *ar_sdio = ath10k_sdio_priv(ar);
784 	struct ath10k_sdio_irq_data *irq_data = &ar_sdio->irq_data;
785 	u8 error_int_status;
786 	int ret;
787 
788 	ath10k_dbg(ar, ATH10K_DBG_SDIO, "sdio error interrupt\n");
789 
790 	error_int_status = irq_data->irq_proc_reg->error_int_status & 0x0F;
791 	if (!error_int_status) {
792 		ath10k_warn(ar, "invalid error interrupt status: 0x%x\n",
793 			    error_int_status);
794 		return -EIO;
795 	}
796 
797 	ath10k_dbg(ar, ATH10K_DBG_SDIO,
798 		   "sdio error_int_status 0x%x\n", error_int_status);
799 
800 	if (FIELD_GET(MBOX_ERROR_INT_STATUS_WAKEUP_MASK,
801 		      error_int_status))
802 		ath10k_dbg(ar, ATH10K_DBG_SDIO, "sdio interrupt error wakeup\n");
803 
804 	if (FIELD_GET(MBOX_ERROR_INT_STATUS_RX_UNDERFLOW_MASK,
805 		      error_int_status))
806 		ath10k_warn(ar, "rx underflow interrupt error\n");
807 
808 	if (FIELD_GET(MBOX_ERROR_INT_STATUS_TX_OVERFLOW_MASK,
809 		      error_int_status))
810 		ath10k_warn(ar, "tx overflow interrupt error\n");
811 
812 	/* Clear the interrupt */
813 	irq_data->irq_proc_reg->error_int_status &= ~error_int_status;
814 
815 	/* set W1C value to clear the interrupt, this hits the register first */
816 	ret = ath10k_sdio_writesb32(ar, MBOX_ERROR_INT_STATUS_ADDRESS,
817 				    error_int_status);
818 	if (ret) {
819 		ath10k_warn(ar, "unable to write to error int status address: %d\n",
820 			    ret);
821 		return ret;
822 	}
823 
824 	return 0;
825 }
826 
827 static int ath10k_sdio_mbox_proc_cpu_intr(struct ath10k *ar)
828 {
829 	struct ath10k_sdio *ar_sdio = ath10k_sdio_priv(ar);
830 	struct ath10k_sdio_irq_data *irq_data = &ar_sdio->irq_data;
831 	u8 cpu_int_status;
832 	int ret;
833 
834 	mutex_lock(&irq_data->mtx);
835 	cpu_int_status = irq_data->irq_proc_reg->cpu_int_status &
836 			 irq_data->irq_en_reg->cpu_int_status_en;
837 	if (!cpu_int_status) {
838 		ath10k_warn(ar, "CPU interrupt status is zero\n");
839 		ret = -EIO;
840 		goto out;
841 	}
842 
843 	/* Clear the interrupt */
844 	irq_data->irq_proc_reg->cpu_int_status &= ~cpu_int_status;
845 
846 	/* Set up the register transfer buffer to hit the register 4 times,
847 	 * this is done to make the access 4-byte aligned to mitigate issues
848 	 * with host bus interconnects that restrict bus transfer lengths to
849 	 * be a multiple of 4-bytes.
850 	 *
851 	 * Set W1C value to clear the interrupt, this hits the register first.
852 	 */
853 	ret = ath10k_sdio_writesb32(ar, MBOX_CPU_INT_STATUS_ADDRESS,
854 				    cpu_int_status);
855 	if (ret) {
856 		ath10k_warn(ar, "unable to write to cpu interrupt status address: %d\n",
857 			    ret);
858 		goto out;
859 	}
860 
861 out:
862 	mutex_unlock(&irq_data->mtx);
863 	return ret;
864 }
865 
866 static int ath10k_sdio_mbox_read_int_status(struct ath10k *ar,
867 					    u8 *host_int_status,
868 					    u32 *lookahead)
869 {
870 	struct ath10k_sdio *ar_sdio = ath10k_sdio_priv(ar);
871 	struct ath10k_sdio_irq_data *irq_data = &ar_sdio->irq_data;
872 	struct ath10k_sdio_irq_proc_regs *irq_proc_reg = irq_data->irq_proc_reg;
873 	struct ath10k_sdio_irq_enable_regs *irq_en_reg = irq_data->irq_en_reg;
874 	u8 htc_mbox = FIELD_PREP(ATH10K_HTC_MAILBOX_MASK, 1);
875 	int ret;
876 
877 	mutex_lock(&irq_data->mtx);
878 
879 	*lookahead = 0;
880 	*host_int_status = 0;
881 
882 	/* int_status_en is supposed to be non zero, otherwise interrupts
883 	 * shouldn't be enabled. There is however a short time frame during
884 	 * initialization between the irq register and int_status_en init
885 	 * where this can happen.
886 	 * We silently ignore this condition.
887 	 */
888 	if (!irq_en_reg->int_status_en) {
889 		ret = 0;
890 		goto out;
891 	}
892 
893 	/* Read the first sizeof(struct ath10k_irq_proc_registers)
894 	 * bytes of the HTC register table. This
895 	 * will yield us the value of different int status
896 	 * registers and the lookahead registers.
897 	 */
898 	ret = ath10k_sdio_read(ar, MBOX_HOST_INT_STATUS_ADDRESS,
899 			       irq_proc_reg, sizeof(*irq_proc_reg));
900 	if (ret)
901 		goto out;
902 
903 	/* Update only those registers that are enabled */
904 	*host_int_status = irq_proc_reg->host_int_status &
905 			   irq_en_reg->int_status_en;
906 
907 	/* Look at mbox status */
908 	if (!(*host_int_status & htc_mbox)) {
909 		*lookahead = 0;
910 		ret = 0;
911 		goto out;
912 	}
913 
914 	/* Mask out pending mbox value, we use look ahead as
915 	 * the real flag for mbox processing.
916 	 */
917 	*host_int_status &= ~htc_mbox;
918 	if (irq_proc_reg->rx_lookahead_valid & htc_mbox) {
919 		*lookahead = le32_to_cpu(
920 			irq_proc_reg->rx_lookahead[ATH10K_HTC_MAILBOX]);
921 		if (!*lookahead)
922 			ath10k_warn(ar, "sdio mbox lookahead is zero\n");
923 	}
924 
925 out:
926 	mutex_unlock(&irq_data->mtx);
927 	return ret;
928 }
929 
930 static int ath10k_sdio_mbox_proc_pending_irqs(struct ath10k *ar,
931 					      bool *done)
932 {
933 	u8 host_int_status;
934 	u32 lookahead;
935 	int ret;
936 
937 	/* NOTE: HIF implementation guarantees that the context of this
938 	 * call allows us to perform SYNCHRONOUS I/O, that is we can block,
939 	 * sleep or call any API that can block or switch thread/task
940 	 * contexts. This is a fully schedulable context.
941 	 */
942 
943 	ret = ath10k_sdio_mbox_read_int_status(ar,
944 					       &host_int_status,
945 					       &lookahead);
946 	if (ret) {
947 		*done = true;
948 		goto out;
949 	}
950 
951 	if (!host_int_status && !lookahead) {
952 		ret = 0;
953 		*done = true;
954 		goto out;
955 	}
956 
957 	if (lookahead) {
958 		ath10k_dbg(ar, ATH10K_DBG_SDIO,
959 			   "sdio pending mailbox msg lookahead 0x%08x\n",
960 			   lookahead);
961 
962 		ret = ath10k_sdio_mbox_rxmsg_pending_handler(ar,
963 							     lookahead,
964 							     done);
965 		if (ret)
966 			goto out;
967 	}
968 
969 	/* now, handle the rest of the interrupts */
970 	ath10k_dbg(ar, ATH10K_DBG_SDIO,
971 		   "sdio host_int_status 0x%x\n", host_int_status);
972 
973 	if (FIELD_GET(MBOX_HOST_INT_STATUS_CPU_MASK, host_int_status)) {
974 		/* CPU Interrupt */
975 		ret = ath10k_sdio_mbox_proc_cpu_intr(ar);
976 		if (ret)
977 			goto out;
978 	}
979 
980 	if (FIELD_GET(MBOX_HOST_INT_STATUS_ERROR_MASK, host_int_status)) {
981 		/* Error Interrupt */
982 		ret = ath10k_sdio_mbox_proc_err_intr(ar);
983 		if (ret)
984 			goto out;
985 	}
986 
987 	if (FIELD_GET(MBOX_HOST_INT_STATUS_COUNTER_MASK, host_int_status))
988 		/* Counter Interrupt */
989 		ret = ath10k_sdio_mbox_proc_counter_intr(ar);
990 
991 	ret = 0;
992 
993 out:
994 	/* An optimization to bypass reading the IRQ status registers
995 	 * unecessarily which can re-wake the target, if upper layers
996 	 * determine that we are in a low-throughput mode, we can rely on
997 	 * taking another interrupt rather than re-checking the status
998 	 * registers which can re-wake the target.
999 	 *
1000 	 * NOTE : for host interfaces that makes use of detecting pending
1001 	 * mbox messages at hif can not use this optimization due to
1002 	 * possible side effects, SPI requires the host to drain all
1003 	 * messages from the mailbox before exiting the ISR routine.
1004 	 */
1005 
1006 	ath10k_dbg(ar, ATH10K_DBG_SDIO,
1007 		   "sdio pending irqs done %d status %d",
1008 		   *done, ret);
1009 
1010 	return ret;
1011 }
1012 
1013 static void ath10k_sdio_set_mbox_info(struct ath10k *ar)
1014 {
1015 	struct ath10k_sdio *ar_sdio = ath10k_sdio_priv(ar);
1016 	struct ath10k_mbox_info *mbox_info = &ar_sdio->mbox_info;
1017 	u16 device = ar_sdio->func->device, dev_id_base, dev_id_chiprev;
1018 
1019 	mbox_info->htc_addr = ATH10K_HIF_MBOX_BASE_ADDR;
1020 	mbox_info->block_size = ATH10K_HIF_MBOX_BLOCK_SIZE;
1021 	mbox_info->block_mask = ATH10K_HIF_MBOX_BLOCK_SIZE - 1;
1022 	mbox_info->gmbox_addr = ATH10K_HIF_GMBOX_BASE_ADDR;
1023 	mbox_info->gmbox_sz = ATH10K_HIF_GMBOX_WIDTH;
1024 
1025 	mbox_info->ext_info[0].htc_ext_addr = ATH10K_HIF_MBOX0_EXT_BASE_ADDR;
1026 
1027 	dev_id_base = FIELD_GET(QCA_MANUFACTURER_ID_BASE, device);
1028 	dev_id_chiprev = FIELD_GET(QCA_MANUFACTURER_ID_REV_MASK, device);
1029 	switch (dev_id_base) {
1030 	case QCA_MANUFACTURER_ID_AR6005_BASE:
1031 		if (dev_id_chiprev < 4)
1032 			mbox_info->ext_info[0].htc_ext_sz =
1033 				ATH10K_HIF_MBOX0_EXT_WIDTH;
1034 		else
1035 			/* from QCA6174 2.0(0x504), the width has been extended
1036 			 * to 56K
1037 			 */
1038 			mbox_info->ext_info[0].htc_ext_sz =
1039 				ATH10K_HIF_MBOX0_EXT_WIDTH_ROME_2_0;
1040 		break;
1041 	case QCA_MANUFACTURER_ID_QCA9377_BASE:
1042 		mbox_info->ext_info[0].htc_ext_sz =
1043 			ATH10K_HIF_MBOX0_EXT_WIDTH_ROME_2_0;
1044 		break;
1045 	default:
1046 		mbox_info->ext_info[0].htc_ext_sz =
1047 				ATH10K_HIF_MBOX0_EXT_WIDTH;
1048 	}
1049 
1050 	mbox_info->ext_info[1].htc_ext_addr =
1051 		mbox_info->ext_info[0].htc_ext_addr +
1052 		mbox_info->ext_info[0].htc_ext_sz +
1053 		ATH10K_HIF_MBOX_DUMMY_SPACE_SIZE;
1054 	mbox_info->ext_info[1].htc_ext_sz = ATH10K_HIF_MBOX1_EXT_WIDTH;
1055 }
1056 
1057 /* BMI functions */
1058 
1059 static int ath10k_sdio_bmi_credits(struct ath10k *ar)
1060 {
1061 	u32 addr, cmd_credits;
1062 	unsigned long timeout;
1063 	int ret;
1064 
1065 	/* Read the counter register to get the command credits */
1066 	addr = MBOX_COUNT_DEC_ADDRESS + ATH10K_HIF_MBOX_NUM_MAX * 4;
1067 	timeout = jiffies + BMI_COMMUNICATION_TIMEOUT_HZ;
1068 	cmd_credits = 0;
1069 
1070 	while (time_before(jiffies, timeout) && !cmd_credits) {
1071 		/* Hit the credit counter with a 4-byte access, the first byte
1072 		 * read will hit the counter and cause a decrement, while the
1073 		 * remaining 3 bytes has no effect. The rationale behind this
1074 		 * is to make all HIF accesses 4-byte aligned.
1075 		 */
1076 		ret = ath10k_sdio_read32(ar, addr, &cmd_credits);
1077 		if (ret) {
1078 			ath10k_warn(ar,
1079 				    "unable to decrement the command credit count register: %d\n",
1080 				    ret);
1081 			return ret;
1082 		}
1083 
1084 		/* The counter is only 8 bits.
1085 		 * Ignore anything in the upper 3 bytes
1086 		 */
1087 		cmd_credits &= 0xFF;
1088 	}
1089 
1090 	if (!cmd_credits) {
1091 		ath10k_warn(ar, "bmi communication timeout\n");
1092 		return -ETIMEDOUT;
1093 	}
1094 
1095 	return 0;
1096 }
1097 
1098 static int ath10k_sdio_bmi_get_rx_lookahead(struct ath10k *ar)
1099 {
1100 	unsigned long timeout;
1101 	u32 rx_word;
1102 	int ret;
1103 
1104 	timeout = jiffies + BMI_COMMUNICATION_TIMEOUT_HZ;
1105 	rx_word = 0;
1106 
1107 	while ((time_before(jiffies, timeout)) && !rx_word) {
1108 		ret = ath10k_sdio_read32(ar,
1109 					 MBOX_HOST_INT_STATUS_ADDRESS,
1110 					 &rx_word);
1111 		if (ret) {
1112 			ath10k_warn(ar, "unable to read RX_LOOKAHEAD_VALID: %d\n", ret);
1113 			return ret;
1114 		}
1115 
1116 		 /* all we really want is one bit */
1117 		rx_word &= 1;
1118 	}
1119 
1120 	if (!rx_word) {
1121 		ath10k_warn(ar, "bmi_recv_buf FIFO empty\n");
1122 		return -EINVAL;
1123 	}
1124 
1125 	return ret;
1126 }
1127 
1128 static int ath10k_sdio_bmi_exchange_msg(struct ath10k *ar,
1129 					void *req, u32 req_len,
1130 					void *resp, u32 *resp_len)
1131 {
1132 	struct ath10k_sdio *ar_sdio = ath10k_sdio_priv(ar);
1133 	u32 addr;
1134 	int ret;
1135 
1136 	if (req) {
1137 		ret = ath10k_sdio_bmi_credits(ar);
1138 		if (ret)
1139 			return ret;
1140 
1141 		addr = ar_sdio->mbox_info.htc_addr;
1142 
1143 		memcpy(ar_sdio->bmi_buf, req, req_len);
1144 		ret = ath10k_sdio_write(ar, addr, ar_sdio->bmi_buf, req_len);
1145 		if (ret) {
1146 			ath10k_warn(ar,
1147 				    "unable to send the bmi data to the device: %d\n",
1148 				    ret);
1149 			return ret;
1150 		}
1151 	}
1152 
1153 	if (!resp || !resp_len)
1154 		/* No response expected */
1155 		return 0;
1156 
1157 	/* During normal bootup, small reads may be required.
1158 	 * Rather than issue an HIF Read and then wait as the Target
1159 	 * adds successive bytes to the FIFO, we wait here until
1160 	 * we know that response data is available.
1161 	 *
1162 	 * This allows us to cleanly timeout on an unexpected
1163 	 * Target failure rather than risk problems at the HIF level.
1164 	 * In particular, this avoids SDIO timeouts and possibly garbage
1165 	 * data on some host controllers.  And on an interconnect
1166 	 * such as Compact Flash (as well as some SDIO masters) which
1167 	 * does not provide any indication on data timeout, it avoids
1168 	 * a potential hang or garbage response.
1169 	 *
1170 	 * Synchronization is more difficult for reads larger than the
1171 	 * size of the MBOX FIFO (128B), because the Target is unable
1172 	 * to push the 129th byte of data until AFTER the Host posts an
1173 	 * HIF Read and removes some FIFO data.  So for large reads the
1174 	 * Host proceeds to post an HIF Read BEFORE all the data is
1175 	 * actually available to read.  Fortunately, large BMI reads do
1176 	 * not occur in practice -- they're supported for debug/development.
1177 	 *
1178 	 * So Host/Target BMI synchronization is divided into these cases:
1179 	 *  CASE 1: length < 4
1180 	 *        Should not happen
1181 	 *
1182 	 *  CASE 2: 4 <= length <= 128
1183 	 *        Wait for first 4 bytes to be in FIFO
1184 	 *        If CONSERVATIVE_BMI_READ is enabled, also wait for
1185 	 *        a BMI command credit, which indicates that the ENTIRE
1186 	 *        response is available in the the FIFO
1187 	 *
1188 	 *  CASE 3: length > 128
1189 	 *        Wait for the first 4 bytes to be in FIFO
1190 	 *
1191 	 * For most uses, a small timeout should be sufficient and we will
1192 	 * usually see a response quickly; but there may be some unusual
1193 	 * (debug) cases of BMI_EXECUTE where we want an larger timeout.
1194 	 * For now, we use an unbounded busy loop while waiting for
1195 	 * BMI_EXECUTE.
1196 	 *
1197 	 * If BMI_EXECUTE ever needs to support longer-latency execution,
1198 	 * especially in production, this code needs to be enhanced to sleep
1199 	 * and yield.  Also note that BMI_COMMUNICATION_TIMEOUT is currently
1200 	 * a function of Host processor speed.
1201 	 */
1202 	ret = ath10k_sdio_bmi_get_rx_lookahead(ar);
1203 	if (ret)
1204 		return ret;
1205 
1206 	/* We always read from the start of the mbox address */
1207 	addr = ar_sdio->mbox_info.htc_addr;
1208 	ret = ath10k_sdio_read(ar, addr, ar_sdio->bmi_buf, *resp_len);
1209 	if (ret) {
1210 		ath10k_warn(ar,
1211 			    "unable to read the bmi data from the device: %d\n",
1212 			    ret);
1213 		return ret;
1214 	}
1215 
1216 	memcpy(resp, ar_sdio->bmi_buf, *resp_len);
1217 
1218 	return 0;
1219 }
1220 
1221 /* sdio async handling functions */
1222 
1223 static struct ath10k_sdio_bus_request
1224 *ath10k_sdio_alloc_busreq(struct ath10k *ar)
1225 {
1226 	struct ath10k_sdio *ar_sdio = ath10k_sdio_priv(ar);
1227 	struct ath10k_sdio_bus_request *bus_req;
1228 
1229 	spin_lock_bh(&ar_sdio->lock);
1230 
1231 	if (list_empty(&ar_sdio->bus_req_freeq)) {
1232 		bus_req = NULL;
1233 		goto out;
1234 	}
1235 
1236 	bus_req = list_first_entry(&ar_sdio->bus_req_freeq,
1237 				   struct ath10k_sdio_bus_request, list);
1238 	list_del(&bus_req->list);
1239 
1240 out:
1241 	spin_unlock_bh(&ar_sdio->lock);
1242 	return bus_req;
1243 }
1244 
1245 static void ath10k_sdio_free_bus_req(struct ath10k *ar,
1246 				     struct ath10k_sdio_bus_request *bus_req)
1247 {
1248 	struct ath10k_sdio *ar_sdio = ath10k_sdio_priv(ar);
1249 
1250 	memset(bus_req, 0, sizeof(*bus_req));
1251 
1252 	spin_lock_bh(&ar_sdio->lock);
1253 	list_add_tail(&bus_req->list, &ar_sdio->bus_req_freeq);
1254 	spin_unlock_bh(&ar_sdio->lock);
1255 }
1256 
1257 static void __ath10k_sdio_write_async(struct ath10k *ar,
1258 				      struct ath10k_sdio_bus_request *req)
1259 {
1260 	struct ath10k_htc_ep *ep;
1261 	struct sk_buff *skb;
1262 	int ret;
1263 
1264 	skb = req->skb;
1265 	ret = ath10k_sdio_write(ar, req->address, skb->data, skb->len);
1266 	if (ret)
1267 		ath10k_warn(ar, "failed to write skb to 0x%x asynchronously: %d",
1268 			    req->address, ret);
1269 
1270 	if (req->htc_msg) {
1271 		ep = &ar->htc.endpoint[req->eid];
1272 		ath10k_htc_notify_tx_completion(ep, skb);
1273 	} else if (req->comp) {
1274 		complete(req->comp);
1275 	}
1276 
1277 	ath10k_sdio_free_bus_req(ar, req);
1278 }
1279 
1280 static void ath10k_sdio_write_async_work(struct work_struct *work)
1281 {
1282 	struct ath10k_sdio *ar_sdio = container_of(work, struct ath10k_sdio,
1283 						   wr_async_work);
1284 	struct ath10k *ar = ar_sdio->ar;
1285 	struct ath10k_sdio_bus_request *req, *tmp_req;
1286 
1287 	spin_lock_bh(&ar_sdio->wr_async_lock);
1288 
1289 	list_for_each_entry_safe(req, tmp_req, &ar_sdio->wr_asyncq, list) {
1290 		list_del(&req->list);
1291 		spin_unlock_bh(&ar_sdio->wr_async_lock);
1292 		__ath10k_sdio_write_async(ar, req);
1293 		spin_lock_bh(&ar_sdio->wr_async_lock);
1294 	}
1295 
1296 	spin_unlock_bh(&ar_sdio->wr_async_lock);
1297 }
1298 
1299 static int ath10k_sdio_prep_async_req(struct ath10k *ar, u32 addr,
1300 				      struct sk_buff *skb,
1301 				      struct completion *comp,
1302 				      bool htc_msg, enum ath10k_htc_ep_id eid)
1303 {
1304 	struct ath10k_sdio *ar_sdio = ath10k_sdio_priv(ar);
1305 	struct ath10k_sdio_bus_request *bus_req;
1306 
1307 	/* Allocate a bus request for the message and queue it on the
1308 	 * SDIO workqueue.
1309 	 */
1310 	bus_req = ath10k_sdio_alloc_busreq(ar);
1311 	if (!bus_req) {
1312 		ath10k_warn(ar,
1313 			    "unable to allocate bus request for async request\n");
1314 		return -ENOMEM;
1315 	}
1316 
1317 	bus_req->skb = skb;
1318 	bus_req->eid = eid;
1319 	bus_req->address = addr;
1320 	bus_req->htc_msg = htc_msg;
1321 	bus_req->comp = comp;
1322 
1323 	spin_lock_bh(&ar_sdio->wr_async_lock);
1324 	list_add_tail(&bus_req->list, &ar_sdio->wr_asyncq);
1325 	spin_unlock_bh(&ar_sdio->wr_async_lock);
1326 
1327 	return 0;
1328 }
1329 
1330 /* IRQ handler */
1331 
1332 static void ath10k_sdio_irq_handler(struct sdio_func *func)
1333 {
1334 	struct ath10k_sdio *ar_sdio = sdio_get_drvdata(func);
1335 	struct ath10k *ar = ar_sdio->ar;
1336 	unsigned long timeout;
1337 	bool done = false;
1338 	int ret;
1339 
1340 	/* Release the host during interrupts so we can pick it back up when
1341 	 * we process commands.
1342 	 */
1343 	sdio_release_host(ar_sdio->func);
1344 
1345 	timeout = jiffies + ATH10K_SDIO_HIF_COMMUNICATION_TIMEOUT_HZ;
1346 	do {
1347 		ret = ath10k_sdio_mbox_proc_pending_irqs(ar, &done);
1348 		if (ret)
1349 			break;
1350 	} while (time_before(jiffies, timeout) && !done);
1351 
1352 	ath10k_mac_tx_push_pending(ar);
1353 
1354 	sdio_claim_host(ar_sdio->func);
1355 
1356 	if (ret && ret != -ECANCELED)
1357 		ath10k_warn(ar, "failed to process pending SDIO interrupts: %d\n",
1358 			    ret);
1359 }
1360 
1361 /* sdio HIF functions */
1362 
1363 static int ath10k_sdio_hif_disable_intrs(struct ath10k *ar)
1364 {
1365 	struct ath10k_sdio *ar_sdio = ath10k_sdio_priv(ar);
1366 	struct ath10k_sdio_irq_data *irq_data = &ar_sdio->irq_data;
1367 	struct ath10k_sdio_irq_enable_regs *regs = irq_data->irq_en_reg;
1368 	int ret;
1369 
1370 	mutex_lock(&irq_data->mtx);
1371 
1372 	memset(regs, 0, sizeof(*regs));
1373 	ret = ath10k_sdio_write(ar, MBOX_INT_STATUS_ENABLE_ADDRESS,
1374 				&regs->int_status_en, sizeof(*regs));
1375 	if (ret)
1376 		ath10k_warn(ar, "unable to disable sdio interrupts: %d\n", ret);
1377 
1378 	mutex_unlock(&irq_data->mtx);
1379 
1380 	return ret;
1381 }
1382 
1383 static int ath10k_sdio_hif_power_up(struct ath10k *ar)
1384 {
1385 	struct ath10k_sdio *ar_sdio = ath10k_sdio_priv(ar);
1386 	struct sdio_func *func = ar_sdio->func;
1387 	int ret;
1388 
1389 	if (!ar_sdio->is_disabled)
1390 		return 0;
1391 
1392 	ath10k_dbg(ar, ATH10K_DBG_BOOT, "sdio power on\n");
1393 
1394 	sdio_claim_host(func);
1395 
1396 	ret = sdio_enable_func(func);
1397 	if (ret) {
1398 		ath10k_warn(ar, "unable to enable sdio function: %d)\n", ret);
1399 		sdio_release_host(func);
1400 		return ret;
1401 	}
1402 
1403 	sdio_release_host(func);
1404 
1405 	/* Wait for hardware to initialise. It should take a lot less than
1406 	 * 20 ms but let's be conservative here.
1407 	 */
1408 	msleep(20);
1409 
1410 	ar_sdio->is_disabled = false;
1411 
1412 	ret = ath10k_sdio_hif_disable_intrs(ar);
1413 	if (ret)
1414 		return ret;
1415 
1416 	return 0;
1417 }
1418 
1419 static void ath10k_sdio_hif_power_down(struct ath10k *ar)
1420 {
1421 	struct ath10k_sdio *ar_sdio = ath10k_sdio_priv(ar);
1422 	int ret;
1423 
1424 	if (ar_sdio->is_disabled)
1425 		return;
1426 
1427 	ath10k_dbg(ar, ATH10K_DBG_BOOT, "sdio power off\n");
1428 
1429 	/* Disable the card */
1430 	sdio_claim_host(ar_sdio->func);
1431 	ret = sdio_disable_func(ar_sdio->func);
1432 	sdio_release_host(ar_sdio->func);
1433 
1434 	if (ret)
1435 		ath10k_warn(ar, "unable to disable sdio function: %d\n", ret);
1436 
1437 	ar_sdio->is_disabled = true;
1438 }
1439 
1440 static int ath10k_sdio_hif_tx_sg(struct ath10k *ar, u8 pipe_id,
1441 				 struct ath10k_hif_sg_item *items, int n_items)
1442 {
1443 	struct ath10k_sdio *ar_sdio = ath10k_sdio_priv(ar);
1444 	enum ath10k_htc_ep_id eid;
1445 	struct sk_buff *skb;
1446 	int ret, i;
1447 
1448 	eid = pipe_id_to_eid(pipe_id);
1449 
1450 	for (i = 0; i < n_items; i++) {
1451 		size_t padded_len;
1452 		u32 address;
1453 
1454 		skb = items[i].transfer_context;
1455 		padded_len = ath10k_sdio_calc_txrx_padded_len(ar_sdio,
1456 							      skb->len);
1457 		skb_trim(skb, padded_len);
1458 
1459 		/* Write TX data to the end of the mbox address space */
1460 		address = ar_sdio->mbox_addr[eid] + ar_sdio->mbox_size[eid] -
1461 			  skb->len;
1462 		ret = ath10k_sdio_prep_async_req(ar, address, skb,
1463 						 NULL, true, eid);
1464 		if (ret)
1465 			return ret;
1466 	}
1467 
1468 	queue_work(ar_sdio->workqueue, &ar_sdio->wr_async_work);
1469 
1470 	return 0;
1471 }
1472 
1473 static int ath10k_sdio_hif_enable_intrs(struct ath10k *ar)
1474 {
1475 	struct ath10k_sdio *ar_sdio = ath10k_sdio_priv(ar);
1476 	struct ath10k_sdio_irq_data *irq_data = &ar_sdio->irq_data;
1477 	struct ath10k_sdio_irq_enable_regs *regs = irq_data->irq_en_reg;
1478 	int ret;
1479 
1480 	mutex_lock(&irq_data->mtx);
1481 
1482 	/* Enable all but CPU interrupts */
1483 	regs->int_status_en = FIELD_PREP(MBOX_INT_STATUS_ENABLE_ERROR_MASK, 1) |
1484 			      FIELD_PREP(MBOX_INT_STATUS_ENABLE_CPU_MASK, 1) |
1485 			      FIELD_PREP(MBOX_INT_STATUS_ENABLE_COUNTER_MASK, 1);
1486 
1487 	/* NOTE: There are some cases where HIF can do detection of
1488 	 * pending mbox messages which is disabled now.
1489 	 */
1490 	regs->int_status_en |=
1491 		FIELD_PREP(MBOX_INT_STATUS_ENABLE_MBOX_DATA_MASK, 1);
1492 
1493 	/* Set up the CPU Interrupt status Register */
1494 	regs->cpu_int_status_en = 0;
1495 
1496 	/* Set up the Error Interrupt status Register */
1497 	regs->err_int_status_en =
1498 		FIELD_PREP(MBOX_ERROR_STATUS_ENABLE_RX_UNDERFLOW_MASK, 1) |
1499 		FIELD_PREP(MBOX_ERROR_STATUS_ENABLE_TX_OVERFLOW_MASK, 1);
1500 
1501 	/* Enable Counter interrupt status register to get fatal errors for
1502 	 * debugging.
1503 	 */
1504 	regs->cntr_int_status_en =
1505 		FIELD_PREP(MBOX_COUNTER_INT_STATUS_ENABLE_BIT_MASK,
1506 			   ATH10K_SDIO_TARGET_DEBUG_INTR_MASK);
1507 
1508 	ret = ath10k_sdio_write(ar, MBOX_INT_STATUS_ENABLE_ADDRESS,
1509 				&regs->int_status_en, sizeof(*regs));
1510 	if (ret)
1511 		ath10k_warn(ar,
1512 			    "failed to update mbox interrupt status register : %d\n",
1513 			    ret);
1514 
1515 	mutex_unlock(&irq_data->mtx);
1516 	return ret;
1517 }
1518 
1519 static int ath10k_sdio_hif_set_mbox_sleep(struct ath10k *ar, bool enable_sleep)
1520 {
1521 	u32 val;
1522 	int ret;
1523 
1524 	ret = ath10k_sdio_read32(ar, ATH10K_FIFO_TIMEOUT_AND_CHIP_CONTROL, &val);
1525 	if (ret) {
1526 		ath10k_warn(ar, "failed to read fifo/chip control register: %d\n",
1527 			    ret);
1528 		return ret;
1529 	}
1530 
1531 	if (enable_sleep)
1532 		val &= ATH10K_FIFO_TIMEOUT_AND_CHIP_CONTROL_DISABLE_SLEEP_OFF;
1533 	else
1534 		val |= ATH10K_FIFO_TIMEOUT_AND_CHIP_CONTROL_DISABLE_SLEEP_ON;
1535 
1536 	ret = ath10k_sdio_write32(ar, ATH10K_FIFO_TIMEOUT_AND_CHIP_CONTROL, val);
1537 	if (ret) {
1538 		ath10k_warn(ar, "failed to write to FIFO_TIMEOUT_AND_CHIP_CONTROL: %d",
1539 			    ret);
1540 		return ret;
1541 	}
1542 
1543 	return 0;
1544 }
1545 
1546 /* HIF diagnostics */
1547 
1548 static int ath10k_sdio_hif_diag_read(struct ath10k *ar, u32 address, void *buf,
1549 				     size_t buf_len)
1550 {
1551 	int ret;
1552 
1553 	/* set window register to start read cycle */
1554 	ret = ath10k_sdio_write32(ar, MBOX_WINDOW_READ_ADDR_ADDRESS, address);
1555 	if (ret) {
1556 		ath10k_warn(ar, "failed to set mbox window read address: %d", ret);
1557 		return ret;
1558 	}
1559 
1560 	/* read the data */
1561 	ret = ath10k_sdio_read(ar, MBOX_WINDOW_DATA_ADDRESS, buf, buf_len);
1562 	if (ret) {
1563 		ath10k_warn(ar, "failed to read from mbox window data address: %d\n",
1564 			    ret);
1565 		return ret;
1566 	}
1567 
1568 	return 0;
1569 }
1570 
1571 static int ath10k_sdio_hif_diag_read32(struct ath10k *ar, u32 address,
1572 				       u32 *value)
1573 {
1574 	__le32 *val;
1575 	int ret;
1576 
1577 	val = kzalloc(sizeof(*val), GFP_KERNEL);
1578 	if (!val)
1579 		return -ENOMEM;
1580 
1581 	ret = ath10k_sdio_hif_diag_read(ar, address, val, sizeof(*val));
1582 	if (ret)
1583 		goto out;
1584 
1585 	*value = __le32_to_cpu(*val);
1586 
1587 out:
1588 	kfree(val);
1589 
1590 	return ret;
1591 }
1592 
1593 static int ath10k_sdio_hif_diag_write_mem(struct ath10k *ar, u32 address,
1594 					  const void *data, int nbytes)
1595 {
1596 	int ret;
1597 
1598 	/* set write data */
1599 	ret = ath10k_sdio_write(ar, MBOX_WINDOW_DATA_ADDRESS, data, nbytes);
1600 	if (ret) {
1601 		ath10k_warn(ar,
1602 			    "failed to write 0x%p to mbox window data address: %d\n",
1603 			    data, ret);
1604 		return ret;
1605 	}
1606 
1607 	/* set window register, which starts the write cycle */
1608 	ret = ath10k_sdio_write32(ar, MBOX_WINDOW_WRITE_ADDR_ADDRESS, address);
1609 	if (ret) {
1610 		ath10k_warn(ar, "failed to set mbox window write address: %d", ret);
1611 		return ret;
1612 	}
1613 
1614 	return 0;
1615 }
1616 
1617 /* HIF start/stop */
1618 
1619 static int ath10k_sdio_hif_start(struct ath10k *ar)
1620 {
1621 	struct ath10k_sdio *ar_sdio = ath10k_sdio_priv(ar);
1622 	u32 addr, val;
1623 	int ret;
1624 
1625 	/* Sleep 20 ms before HIF interrupts are disabled.
1626 	 * This will give target plenty of time to process the BMI done
1627 	 * request before interrupts are disabled.
1628 	 */
1629 	msleep(20);
1630 	ret = ath10k_sdio_hif_disable_intrs(ar);
1631 	if (ret)
1632 		return ret;
1633 
1634 	/* eid 0 always uses the lower part of the extended mailbox address
1635 	 * space (ext_info[0].htc_ext_addr).
1636 	 */
1637 	ar_sdio->mbox_addr[0] = ar_sdio->mbox_info.ext_info[0].htc_ext_addr;
1638 	ar_sdio->mbox_size[0] = ar_sdio->mbox_info.ext_info[0].htc_ext_sz;
1639 
1640 	sdio_claim_host(ar_sdio->func);
1641 
1642 	/* Register the isr */
1643 	ret =  sdio_claim_irq(ar_sdio->func, ath10k_sdio_irq_handler);
1644 	if (ret) {
1645 		ath10k_warn(ar, "failed to claim sdio interrupt: %d\n", ret);
1646 		sdio_release_host(ar_sdio->func);
1647 		return ret;
1648 	}
1649 
1650 	sdio_release_host(ar_sdio->func);
1651 
1652 	ret = ath10k_sdio_hif_enable_intrs(ar);
1653 	if (ret)
1654 		ath10k_warn(ar, "failed to enable sdio interrupts: %d\n", ret);
1655 
1656 	addr = host_interest_item_address(HI_ITEM(hi_acs_flags));
1657 
1658 	ret = ath10k_sdio_hif_diag_read32(ar, addr, &val);
1659 	if (ret) {
1660 		ath10k_warn(ar, "unable to read hi_acs_flags address: %d\n", ret);
1661 		return ret;
1662 	}
1663 
1664 	if (val & HI_ACS_FLAGS_SDIO_SWAP_MAILBOX_FW_ACK) {
1665 		ath10k_dbg(ar, ATH10K_DBG_SDIO,
1666 			   "sdio mailbox swap service enabled\n");
1667 		ar_sdio->swap_mbox = true;
1668 	}
1669 
1670 	/* Enable sleep and then disable it again */
1671 	ret = ath10k_sdio_hif_set_mbox_sleep(ar, true);
1672 	if (ret)
1673 		return ret;
1674 
1675 	/* Wait for 20ms for the written value to take effect */
1676 	msleep(20);
1677 
1678 	ret = ath10k_sdio_hif_set_mbox_sleep(ar, false);
1679 	if (ret)
1680 		return ret;
1681 
1682 	return 0;
1683 }
1684 
1685 #define SDIO_IRQ_DISABLE_TIMEOUT_HZ (3 * HZ)
1686 
1687 static void ath10k_sdio_irq_disable(struct ath10k *ar)
1688 {
1689 	struct ath10k_sdio *ar_sdio = ath10k_sdio_priv(ar);
1690 	struct ath10k_sdio_irq_data *irq_data = &ar_sdio->irq_data;
1691 	struct ath10k_sdio_irq_enable_regs *regs = irq_data->irq_en_reg;
1692 	struct sk_buff *skb;
1693 	struct completion irqs_disabled_comp;
1694 	int ret;
1695 
1696 	skb = dev_alloc_skb(sizeof(*regs));
1697 	if (!skb)
1698 		return;
1699 
1700 	mutex_lock(&irq_data->mtx);
1701 
1702 	memset(regs, 0, sizeof(*regs)); /* disable all interrupts */
1703 	memcpy(skb->data, regs, sizeof(*regs));
1704 	skb_put(skb, sizeof(*regs));
1705 
1706 	mutex_unlock(&irq_data->mtx);
1707 
1708 	init_completion(&irqs_disabled_comp);
1709 	ret = ath10k_sdio_prep_async_req(ar, MBOX_INT_STATUS_ENABLE_ADDRESS,
1710 					 skb, &irqs_disabled_comp, false, 0);
1711 	if (ret)
1712 		goto out;
1713 
1714 	queue_work(ar_sdio->workqueue, &ar_sdio->wr_async_work);
1715 
1716 	/* Wait for the completion of the IRQ disable request.
1717 	 * If there is a timeout we will try to disable irq's anyway.
1718 	 */
1719 	ret = wait_for_completion_timeout(&irqs_disabled_comp,
1720 					  SDIO_IRQ_DISABLE_TIMEOUT_HZ);
1721 	if (!ret)
1722 		ath10k_warn(ar, "sdio irq disable request timed out\n");
1723 
1724 	sdio_claim_host(ar_sdio->func);
1725 
1726 	ret = sdio_release_irq(ar_sdio->func);
1727 	if (ret)
1728 		ath10k_warn(ar, "failed to release sdio interrupt: %d\n", ret);
1729 
1730 	sdio_release_host(ar_sdio->func);
1731 
1732 out:
1733 	kfree_skb(skb);
1734 }
1735 
1736 static void ath10k_sdio_hif_stop(struct ath10k *ar)
1737 {
1738 	struct ath10k_sdio_bus_request *req, *tmp_req;
1739 	struct ath10k_sdio *ar_sdio = ath10k_sdio_priv(ar);
1740 
1741 	ath10k_sdio_irq_disable(ar);
1742 
1743 	cancel_work_sync(&ar_sdio->wr_async_work);
1744 
1745 	spin_lock_bh(&ar_sdio->wr_async_lock);
1746 
1747 	/* Free all bus requests that have not been handled */
1748 	list_for_each_entry_safe(req, tmp_req, &ar_sdio->wr_asyncq, list) {
1749 		struct ath10k_htc_ep *ep;
1750 
1751 		list_del(&req->list);
1752 
1753 		if (req->htc_msg) {
1754 			ep = &ar->htc.endpoint[req->eid];
1755 			ath10k_htc_notify_tx_completion(ep, req->skb);
1756 		} else if (req->skb) {
1757 			kfree_skb(req->skb);
1758 		}
1759 		ath10k_sdio_free_bus_req(ar, req);
1760 	}
1761 
1762 	spin_unlock_bh(&ar_sdio->wr_async_lock);
1763 }
1764 
1765 #ifdef CONFIG_PM
1766 
1767 static int ath10k_sdio_hif_suspend(struct ath10k *ar)
1768 {
1769 	return -EOPNOTSUPP;
1770 }
1771 
1772 static int ath10k_sdio_hif_resume(struct ath10k *ar)
1773 {
1774 	switch (ar->state) {
1775 	case ATH10K_STATE_OFF:
1776 		ath10k_dbg(ar, ATH10K_DBG_SDIO,
1777 			   "sdio resume configuring sdio\n");
1778 
1779 		/* need to set sdio settings after power is cut from sdio */
1780 		ath10k_sdio_config(ar);
1781 		break;
1782 
1783 	case ATH10K_STATE_ON:
1784 	default:
1785 		break;
1786 	}
1787 
1788 	return 0;
1789 }
1790 #endif
1791 
1792 static int ath10k_sdio_hif_map_service_to_pipe(struct ath10k *ar,
1793 					       u16 service_id,
1794 					       u8 *ul_pipe, u8 *dl_pipe)
1795 {
1796 	struct ath10k_sdio *ar_sdio = ath10k_sdio_priv(ar);
1797 	struct ath10k_htc *htc = &ar->htc;
1798 	u32 htt_addr, wmi_addr, htt_mbox_size, wmi_mbox_size;
1799 	enum ath10k_htc_ep_id eid;
1800 	bool ep_found = false;
1801 	int i;
1802 
1803 	/* For sdio, we are interested in the mapping between eid
1804 	 * and pipeid rather than service_id to pipe_id.
1805 	 * First we find out which eid has been allocated to the
1806 	 * service...
1807 	 */
1808 	for (i = 0; i < ATH10K_HTC_EP_COUNT; i++) {
1809 		if (htc->endpoint[i].service_id == service_id) {
1810 			eid = htc->endpoint[i].eid;
1811 			ep_found = true;
1812 			break;
1813 		}
1814 	}
1815 
1816 	if (!ep_found)
1817 		return -EINVAL;
1818 
1819 	/* Then we create the simplest mapping possible between pipeid
1820 	 * and eid
1821 	 */
1822 	*ul_pipe = *dl_pipe = (u8)eid;
1823 
1824 	/* Normally, HTT will use the upper part of the extended
1825 	 * mailbox address space (ext_info[1].htc_ext_addr) and WMI ctrl
1826 	 * the lower part (ext_info[0].htc_ext_addr).
1827 	 * If fw wants swapping of mailbox addresses, the opposite is true.
1828 	 */
1829 	if (ar_sdio->swap_mbox) {
1830 		htt_addr = ar_sdio->mbox_info.ext_info[0].htc_ext_addr;
1831 		wmi_addr = ar_sdio->mbox_info.ext_info[1].htc_ext_addr;
1832 		htt_mbox_size = ar_sdio->mbox_info.ext_info[0].htc_ext_sz;
1833 		wmi_mbox_size = ar_sdio->mbox_info.ext_info[1].htc_ext_sz;
1834 	} else {
1835 		htt_addr = ar_sdio->mbox_info.ext_info[1].htc_ext_addr;
1836 		wmi_addr = ar_sdio->mbox_info.ext_info[0].htc_ext_addr;
1837 		htt_mbox_size = ar_sdio->mbox_info.ext_info[1].htc_ext_sz;
1838 		wmi_mbox_size = ar_sdio->mbox_info.ext_info[0].htc_ext_sz;
1839 	}
1840 
1841 	switch (service_id) {
1842 	case ATH10K_HTC_SVC_ID_RSVD_CTRL:
1843 		/* HTC ctrl ep mbox address has already been setup in
1844 		 * ath10k_sdio_hif_start
1845 		 */
1846 		break;
1847 	case ATH10K_HTC_SVC_ID_WMI_CONTROL:
1848 		ar_sdio->mbox_addr[eid] = wmi_addr;
1849 		ar_sdio->mbox_size[eid] = wmi_mbox_size;
1850 		ath10k_dbg(ar, ATH10K_DBG_SDIO,
1851 			   "sdio wmi ctrl mbox_addr 0x%x mbox_size %d\n",
1852 			   ar_sdio->mbox_addr[eid], ar_sdio->mbox_size[eid]);
1853 		break;
1854 	case ATH10K_HTC_SVC_ID_HTT_DATA_MSG:
1855 		ar_sdio->mbox_addr[eid] = htt_addr;
1856 		ar_sdio->mbox_size[eid] = htt_mbox_size;
1857 		ath10k_dbg(ar, ATH10K_DBG_SDIO,
1858 			   "sdio htt data mbox_addr 0x%x mbox_size %d\n",
1859 			   ar_sdio->mbox_addr[eid], ar_sdio->mbox_size[eid]);
1860 		break;
1861 	default:
1862 		ath10k_warn(ar, "unsupported HTC service id: %d\n",
1863 			    service_id);
1864 		return -EINVAL;
1865 	}
1866 
1867 	return 0;
1868 }
1869 
1870 static void ath10k_sdio_hif_get_default_pipe(struct ath10k *ar,
1871 					     u8 *ul_pipe, u8 *dl_pipe)
1872 {
1873 	ath10k_dbg(ar, ATH10K_DBG_SDIO, "sdio hif get default pipe\n");
1874 
1875 	/* HTC ctrl ep (SVC id 1) always has eid (and pipe_id in our
1876 	 * case) == 0
1877 	 */
1878 	*ul_pipe = 0;
1879 	*dl_pipe = 0;
1880 }
1881 
1882 /* This op is currently only used by htc_wait_target if the HTC ready
1883  * message times out. It is not applicable for SDIO since there is nothing
1884  * we can do if the HTC ready message does not arrive in time.
1885  * TODO: Make this op non mandatory by introducing a NULL check in the
1886  * hif op wrapper.
1887  */
1888 static void ath10k_sdio_hif_send_complete_check(struct ath10k *ar,
1889 						u8 pipe, int force)
1890 {
1891 }
1892 
1893 static const struct ath10k_hif_ops ath10k_sdio_hif_ops = {
1894 	.tx_sg			= ath10k_sdio_hif_tx_sg,
1895 	.diag_read		= ath10k_sdio_hif_diag_read,
1896 	.diag_write		= ath10k_sdio_hif_diag_write_mem,
1897 	.exchange_bmi_msg	= ath10k_sdio_bmi_exchange_msg,
1898 	.start			= ath10k_sdio_hif_start,
1899 	.stop			= ath10k_sdio_hif_stop,
1900 	.map_service_to_pipe	= ath10k_sdio_hif_map_service_to_pipe,
1901 	.get_default_pipe	= ath10k_sdio_hif_get_default_pipe,
1902 	.send_complete_check	= ath10k_sdio_hif_send_complete_check,
1903 	.power_up		= ath10k_sdio_hif_power_up,
1904 	.power_down		= ath10k_sdio_hif_power_down,
1905 #ifdef CONFIG_PM
1906 	.suspend		= ath10k_sdio_hif_suspend,
1907 	.resume			= ath10k_sdio_hif_resume,
1908 #endif
1909 };
1910 
1911 #ifdef CONFIG_PM_SLEEP
1912 
1913 /* Empty handlers so that mmc subsystem doesn't remove us entirely during
1914  * suspend. We instead follow cfg80211 suspend/resume handlers.
1915  */
1916 static int ath10k_sdio_pm_suspend(struct device *device)
1917 {
1918 	return 0;
1919 }
1920 
1921 static int ath10k_sdio_pm_resume(struct device *device)
1922 {
1923 	return 0;
1924 }
1925 
1926 static SIMPLE_DEV_PM_OPS(ath10k_sdio_pm_ops, ath10k_sdio_pm_suspend,
1927 			 ath10k_sdio_pm_resume);
1928 
1929 #define ATH10K_SDIO_PM_OPS (&ath10k_sdio_pm_ops)
1930 
1931 #else
1932 
1933 #define ATH10K_SDIO_PM_OPS NULL
1934 
1935 #endif /* CONFIG_PM_SLEEP */
1936 
1937 static int ath10k_sdio_probe(struct sdio_func *func,
1938 			     const struct sdio_device_id *id)
1939 {
1940 	struct ath10k_sdio *ar_sdio;
1941 	struct ath10k *ar;
1942 	enum ath10k_hw_rev hw_rev;
1943 	u32 chip_id, dev_id_base;
1944 	int ret, i;
1945 
1946 	/* Assumption: All SDIO based chipsets (so far) are QCA6174 based.
1947 	 * If there will be newer chipsets that does not use the hw reg
1948 	 * setup as defined in qca6174_regs and qca6174_values, this
1949 	 * assumption is no longer valid and hw_rev must be setup differently
1950 	 * depending on chipset.
1951 	 */
1952 	hw_rev = ATH10K_HW_QCA6174;
1953 
1954 	ar = ath10k_core_create(sizeof(*ar_sdio), &func->dev, ATH10K_BUS_SDIO,
1955 				hw_rev, &ath10k_sdio_hif_ops);
1956 	if (!ar) {
1957 		dev_err(&func->dev, "failed to allocate core\n");
1958 		return -ENOMEM;
1959 	}
1960 
1961 	ath10k_dbg(ar, ATH10K_DBG_BOOT,
1962 		   "sdio new func %d vendor 0x%x device 0x%x block 0x%x/0x%x\n",
1963 		   func->num, func->vendor, func->device,
1964 		   func->max_blksize, func->cur_blksize);
1965 
1966 	ar_sdio = ath10k_sdio_priv(ar);
1967 
1968 	ar_sdio->irq_data.irq_proc_reg =
1969 		devm_kzalloc(ar->dev, sizeof(struct ath10k_sdio_irq_proc_regs),
1970 			     GFP_KERNEL);
1971 	if (!ar_sdio->irq_data.irq_proc_reg) {
1972 		ret = -ENOMEM;
1973 		goto err_core_destroy;
1974 	}
1975 
1976 	ar_sdio->irq_data.irq_en_reg =
1977 		devm_kzalloc(ar->dev, sizeof(struct ath10k_sdio_irq_enable_regs),
1978 			     GFP_KERNEL);
1979 	if (!ar_sdio->irq_data.irq_en_reg) {
1980 		ret = -ENOMEM;
1981 		goto err_core_destroy;
1982 	}
1983 
1984 	ar_sdio->bmi_buf = devm_kzalloc(ar->dev, BMI_MAX_CMDBUF_SIZE, GFP_KERNEL);
1985 	if (!ar_sdio->bmi_buf) {
1986 		ret = -ENOMEM;
1987 		goto err_core_destroy;
1988 	}
1989 
1990 	ar_sdio->func = func;
1991 	sdio_set_drvdata(func, ar_sdio);
1992 
1993 	ar_sdio->is_disabled = true;
1994 	ar_sdio->ar = ar;
1995 
1996 	spin_lock_init(&ar_sdio->lock);
1997 	spin_lock_init(&ar_sdio->wr_async_lock);
1998 	mutex_init(&ar_sdio->irq_data.mtx);
1999 
2000 	INIT_LIST_HEAD(&ar_sdio->bus_req_freeq);
2001 	INIT_LIST_HEAD(&ar_sdio->wr_asyncq);
2002 
2003 	INIT_WORK(&ar_sdio->wr_async_work, ath10k_sdio_write_async_work);
2004 	ar_sdio->workqueue = create_singlethread_workqueue("ath10k_sdio_wq");
2005 	if (!ar_sdio->workqueue) {
2006 		ret = -ENOMEM;
2007 		goto err_core_destroy;
2008 	}
2009 
2010 	for (i = 0; i < ATH10K_SDIO_BUS_REQUEST_MAX_NUM; i++)
2011 		ath10k_sdio_free_bus_req(ar, &ar_sdio->bus_req[i]);
2012 
2013 	dev_id_base = FIELD_GET(QCA_MANUFACTURER_ID_BASE, id->device);
2014 	switch (dev_id_base) {
2015 	case QCA_MANUFACTURER_ID_AR6005_BASE:
2016 	case QCA_MANUFACTURER_ID_QCA9377_BASE:
2017 		ar->dev_id = QCA9377_1_0_DEVICE_ID;
2018 		break;
2019 	default:
2020 		ret = -ENODEV;
2021 		ath10k_err(ar, "unsupported device id %u (0x%x)\n",
2022 			   dev_id_base, id->device);
2023 		goto err_free_wq;
2024 	}
2025 
2026 	ar->id.vendor = id->vendor;
2027 	ar->id.device = id->device;
2028 
2029 	ath10k_sdio_set_mbox_info(ar);
2030 
2031 	ret = ath10k_sdio_config(ar);
2032 	if (ret) {
2033 		ath10k_err(ar, "failed to config sdio: %d\n", ret);
2034 		goto err_free_wq;
2035 	}
2036 
2037 	/* TODO: don't know yet how to get chip_id with SDIO */
2038 	chip_id = 0;
2039 	ret = ath10k_core_register(ar, chip_id);
2040 	if (ret) {
2041 		ath10k_err(ar, "failed to register driver core: %d\n", ret);
2042 		goto err_free_wq;
2043 	}
2044 
2045 	/* TODO: remove this once SDIO support is fully implemented */
2046 	ath10k_warn(ar, "WARNING: ath10k SDIO support is incomplete, don't expect anything to work!\n");
2047 
2048 	return 0;
2049 
2050 err_free_wq:
2051 	destroy_workqueue(ar_sdio->workqueue);
2052 err_core_destroy:
2053 	ath10k_core_destroy(ar);
2054 
2055 	return ret;
2056 }
2057 
2058 static void ath10k_sdio_remove(struct sdio_func *func)
2059 {
2060 	struct ath10k_sdio *ar_sdio = sdio_get_drvdata(func);
2061 	struct ath10k *ar = ar_sdio->ar;
2062 
2063 	ath10k_dbg(ar, ATH10K_DBG_BOOT,
2064 		   "sdio removed func %d vendor 0x%x device 0x%x\n",
2065 		   func->num, func->vendor, func->device);
2066 
2067 	(void)ath10k_sdio_hif_disable_intrs(ar);
2068 	cancel_work_sync(&ar_sdio->wr_async_work);
2069 	ath10k_core_unregister(ar);
2070 	ath10k_core_destroy(ar);
2071 }
2072 
2073 static const struct sdio_device_id ath10k_sdio_devices[] = {
2074 	{SDIO_DEVICE(QCA_MANUFACTURER_CODE,
2075 		     (QCA_SDIO_ID_AR6005_BASE | 0xA))},
2076 	{SDIO_DEVICE(QCA_MANUFACTURER_CODE,
2077 		     (QCA_SDIO_ID_QCA9377_BASE | 0x1))},
2078 	{},
2079 };
2080 
2081 MODULE_DEVICE_TABLE(sdio, ath10k_sdio_devices);
2082 
2083 static struct sdio_driver ath10k_sdio_driver = {
2084 	.name = "ath10k_sdio",
2085 	.id_table = ath10k_sdio_devices,
2086 	.probe = ath10k_sdio_probe,
2087 	.remove = ath10k_sdio_remove,
2088 	.drv.pm = ATH10K_SDIO_PM_OPS,
2089 };
2090 
2091 static int __init ath10k_sdio_init(void)
2092 {
2093 	int ret;
2094 
2095 	ret = sdio_register_driver(&ath10k_sdio_driver);
2096 	if (ret)
2097 		pr_err("sdio driver registration failed: %d\n", ret);
2098 
2099 	return ret;
2100 }
2101 
2102 static void __exit ath10k_sdio_exit(void)
2103 {
2104 	sdio_unregister_driver(&ath10k_sdio_driver);
2105 }
2106 
2107 module_init(ath10k_sdio_init);
2108 module_exit(ath10k_sdio_exit);
2109 
2110 MODULE_AUTHOR("Qualcomm Atheros");
2111 MODULE_DESCRIPTION("Driver support for Qualcomm Atheros 802.11ac WLAN SDIO devices");
2112 MODULE_LICENSE("Dual BSD/GPL");
2113