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