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