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