Merge tag 'pinctrl-v4.9-1' of git://git.kernel.org/pub/scm/linux/kernel/git/linusw...
[linux-2.6-block.git] / drivers / net / wireless / ath / ath10k / htc.c
CommitLineData
5e3dd157
KV
1/*
2 * Copyright (c) 2005-2011 Atheros Communications Inc.
3 * Copyright (c) 2011-2013 Qualcomm Atheros, Inc.
4 *
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
17
18#include "core.h"
19#include "hif.h"
20#include "debug.h"
21
22/********/
23/* Send */
24/********/
25
5e3dd157
KV
26static void ath10k_htc_control_tx_complete(struct ath10k *ar,
27 struct sk_buff *skb)
28{
29 kfree_skb(skb);
30}
31
32static struct sk_buff *ath10k_htc_build_tx_ctrl_skb(void *ar)
33{
34 struct sk_buff *skb;
35 struct ath10k_skb_cb *skb_cb;
36
37 skb = dev_alloc_skb(ATH10K_HTC_CONTROL_BUFFER_SIZE);
8cc7f26c 38 if (!skb)
5e3dd157 39 return NULL;
5e3dd157
KV
40
41 skb_reserve(skb, 20); /* FIXME: why 20 bytes? */
42 WARN_ONCE((unsigned long)skb->data & 3, "unaligned skb");
43
44 skb_cb = ATH10K_SKB_CB(skb);
45 memset(skb_cb, 0, sizeof(*skb_cb));
46
75b34800 47 ath10k_dbg(ar, ATH10K_DBG_HTC, "%s: skb %pK\n", __func__, skb);
5e3dd157
KV
48 return skb;
49}
50
51static inline void ath10k_htc_restore_tx_skb(struct ath10k_htc *htc,
52 struct sk_buff *skb)
53{
767d34fc
MK
54 struct ath10k_skb_cb *skb_cb = ATH10K_SKB_CB(skb);
55
56 dma_unmap_single(htc->ar->dev, skb_cb->paddr, skb->len, DMA_TO_DEVICE);
5e3dd157
KV
57 skb_pull(skb, sizeof(struct ath10k_htc_hdr));
58}
59
60static void ath10k_htc_notify_tx_completion(struct ath10k_htc_ep *ep,
61 struct sk_buff *skb)
62{
7aa7a72a
MK
63 struct ath10k *ar = ep->htc->ar;
64
75b34800 65 ath10k_dbg(ar, ATH10K_DBG_HTC, "%s: ep %d skb %pK\n", __func__,
5e3dd157
KV
66 ep->eid, skb);
67
68 ath10k_htc_restore_tx_skb(ep->htc, skb);
69
70 if (!ep->ep_ops.ep_tx_complete) {
7aa7a72a 71 ath10k_warn(ar, "no tx handler for eid %d\n", ep->eid);
5e3dd157
KV
72 dev_kfree_skb_any(skb);
73 return;
74 }
75
76 ep->ep_ops.ep_tx_complete(ep->htc->ar, skb);
77}
78
5e3dd157
KV
79static void ath10k_htc_prepare_tx_skb(struct ath10k_htc_ep *ep,
80 struct sk_buff *skb)
81{
82 struct ath10k_htc_hdr *hdr;
83
84 hdr = (struct ath10k_htc_hdr *)skb->data;
5e3dd157
KV
85
86 hdr->eid = ep->eid;
87 hdr->len = __cpu_to_le16(skb->len - sizeof(*hdr));
27bb178d 88 hdr->flags = 0;
5d5c8ec9 89 hdr->flags |= ATH10K_HTC_FLAG_NEED_CREDIT_UPDATE;
5e3dd157
KV
90
91 spin_lock_bh(&ep->htc->tx_lock);
92 hdr->seq_no = ep->seq_no++;
5e3dd157
KV
93 spin_unlock_bh(&ep->htc->tx_lock);
94}
95
5e3dd157
KV
96int ath10k_htc_send(struct ath10k_htc *htc,
97 enum ath10k_htc_ep_id eid,
98 struct sk_buff *skb)
99{
7aa7a72a 100 struct ath10k *ar = htc->ar;
5e3dd157 101 struct ath10k_htc_ep *ep = &htc->endpoint[eid];
767d34fc 102 struct ath10k_skb_cb *skb_cb = ATH10K_SKB_CB(skb);
726346fc 103 struct ath10k_hif_sg_item sg_item;
767d34fc 104 struct device *dev = htc->ar->dev;
12acbc43
MK
105 int credits = 0;
106 int ret;
5e3dd157 107
affd3217
MK
108 if (htc->ar->state == ATH10K_STATE_WEDGED)
109 return -ECOMM;
110
5e3dd157 111 if (eid >= ATH10K_HTC_EP_COUNT) {
7aa7a72a 112 ath10k_warn(ar, "Invalid endpoint id: %d\n", eid);
5e3dd157
KV
113 return -ENOENT;
114 }
115
08fe9b40 116 skb_push(skb, sizeof(struct ath10k_htc_hdr));
5e3dd157 117
12acbc43
MK
118 if (ep->tx_credit_flow_enabled) {
119 credits = DIV_ROUND_UP(skb->len, htc->target_credit_size);
120 spin_lock_bh(&htc->tx_lock);
121 if (ep->tx_credits < credits) {
122 spin_unlock_bh(&htc->tx_lock);
123 ret = -EAGAIN;
124 goto err_pull;
125 }
126 ep->tx_credits -= credits;
7aa7a72a 127 ath10k_dbg(ar, ATH10K_DBG_HTC,
4bfee8e8
MK
128 "htc ep %d consumed %d credits (total %d)\n",
129 eid, credits, ep->tx_credits);
12acbc43
MK
130 spin_unlock_bh(&htc->tx_lock);
131 }
132
133 ath10k_htc_prepare_tx_skb(ep, skb);
134
d84a512d 135 skb_cb->eid = eid;
767d34fc
MK
136 skb_cb->paddr = dma_map_single(dev, skb->data, skb->len, DMA_TO_DEVICE);
137 ret = dma_mapping_error(dev, skb_cb->paddr);
5e55e3cb
MK
138 if (ret) {
139 ret = -EIO;
12acbc43 140 goto err_credits;
5e55e3cb 141 }
12acbc43 142
726346fc
MK
143 sg_item.transfer_id = ep->eid;
144 sg_item.transfer_context = skb;
145 sg_item.vaddr = skb->data;
146 sg_item.paddr = skb_cb->paddr;
147 sg_item.len = skb->len;
148
149 ret = ath10k_hif_tx_sg(htc->ar, ep->ul_pipe_id, &sg_item, 1);
12acbc43
MK
150 if (ret)
151 goto err_unmap;
152
5e3dd157 153 return 0;
12acbc43
MK
154
155err_unmap:
767d34fc 156 dma_unmap_single(dev, skb_cb->paddr, skb->len, DMA_TO_DEVICE);
12acbc43
MK
157err_credits:
158 if (ep->tx_credit_flow_enabled) {
159 spin_lock_bh(&htc->tx_lock);
160 ep->tx_credits += credits;
7aa7a72a 161 ath10k_dbg(ar, ATH10K_DBG_HTC,
4bfee8e8
MK
162 "htc ep %d reverted %d credits back (total %d)\n",
163 eid, credits, ep->tx_credits);
12acbc43
MK
164 spin_unlock_bh(&htc->tx_lock);
165
166 if (ep->ep_ops.ep_tx_credits)
167 ep->ep_ops.ep_tx_credits(htc->ar);
168 }
169err_pull:
170 skb_pull(skb, sizeof(struct ath10k_htc_hdr));
171 return ret;
5e3dd157
KV
172}
173
0e5b2950 174void ath10k_htc_tx_completion_handler(struct ath10k *ar, struct sk_buff *skb)
5e3dd157 175{
cd003fad 176 struct ath10k_htc *htc = &ar->htc;
d84a512d
MK
177 struct ath10k_skb_cb *skb_cb;
178 struct ath10k_htc_ep *ep;
5e3dd157 179
a16942e6 180 if (WARN_ON_ONCE(!skb))
0e5b2950 181 return;
2415fc16 182
d84a512d
MK
183 skb_cb = ATH10K_SKB_CB(skb);
184 ep = &htc->endpoint[skb_cb->eid];
185
5e3dd157
KV
186 ath10k_htc_notify_tx_completion(ep, skb);
187 /* the skb now belongs to the completion handler */
5e3dd157 188}
aed1dc82 189EXPORT_SYMBOL(ath10k_htc_tx_completion_handler);
5e3dd157 190
5e3dd157
KV
191/***********/
192/* Receive */
193/***********/
194
195static void
196ath10k_htc_process_credit_report(struct ath10k_htc *htc,
197 const struct ath10k_htc_credit_report *report,
198 int len,
199 enum ath10k_htc_ep_id eid)
200{
7aa7a72a 201 struct ath10k *ar = htc->ar;
5e3dd157
KV
202 struct ath10k_htc_ep *ep;
203 int i, n_reports;
204
205 if (len % sizeof(*report))
7aa7a72a 206 ath10k_warn(ar, "Uneven credit report len %d", len);
5e3dd157
KV
207
208 n_reports = len / sizeof(*report);
209
210 spin_lock_bh(&htc->tx_lock);
211 for (i = 0; i < n_reports; i++, report++) {
212 if (report->eid >= ATH10K_HTC_EP_COUNT)
213 break;
214
5e3dd157
KV
215 ep = &htc->endpoint[report->eid];
216 ep->tx_credits += report->credits;
217
7aa7a72a 218 ath10k_dbg(ar, ATH10K_DBG_HTC, "htc ep %d got %d credits (total %d)\n",
4bfee8e8
MK
219 report->eid, report->credits, ep->tx_credits);
220
88e65fc3
MK
221 if (ep->ep_ops.ep_tx_credits) {
222 spin_unlock_bh(&htc->tx_lock);
223 ep->ep_ops.ep_tx_credits(htc->ar);
224 spin_lock_bh(&htc->tx_lock);
225 }
5e3dd157
KV
226 }
227 spin_unlock_bh(&htc->tx_lock);
228}
229
230static int ath10k_htc_process_trailer(struct ath10k_htc *htc,
231 u8 *buffer,
232 int length,
233 enum ath10k_htc_ep_id src_eid)
234{
7aa7a72a 235 struct ath10k *ar = htc->ar;
5e3dd157
KV
236 int status = 0;
237 struct ath10k_htc_record *record;
238 u8 *orig_buffer;
239 int orig_length;
240 size_t len;
241
242 orig_buffer = buffer;
243 orig_length = length;
244
245 while (length > 0) {
246 record = (struct ath10k_htc_record *)buffer;
247
248 if (length < sizeof(record->hdr)) {
249 status = -EINVAL;
250 break;
251 }
252
253 if (record->hdr.len > length) {
254 /* no room left in buffer for record */
7aa7a72a 255 ath10k_warn(ar, "Invalid record length: %d\n",
5e3dd157
KV
256 record->hdr.len);
257 status = -EINVAL;
258 break;
259 }
260
261 switch (record->hdr.id) {
262 case ATH10K_HTC_RECORD_CREDITS:
263 len = sizeof(struct ath10k_htc_credit_report);
264 if (record->hdr.len < len) {
7aa7a72a 265 ath10k_warn(ar, "Credit report too long\n");
5e3dd157
KV
266 status = -EINVAL;
267 break;
268 }
269 ath10k_htc_process_credit_report(htc,
270 record->credit_report,
271 record->hdr.len,
272 src_eid);
273 break;
274 default:
7aa7a72a 275 ath10k_warn(ar, "Unhandled record: id:%d length:%d\n",
5e3dd157
KV
276 record->hdr.id, record->hdr.len);
277 break;
278 }
279
280 if (status)
281 break;
282
283 /* multiple records may be present in a trailer */
284 buffer += sizeof(record->hdr) + record->hdr.len;
285 length -= sizeof(record->hdr) + record->hdr.len;
286 }
287
288 if (status)
7aa7a72a 289 ath10k_dbg_dump(ar, ATH10K_DBG_HTC, "htc rx bad trailer", "",
5e3dd157
KV
290 orig_buffer, orig_length);
291
292 return status;
293}
294
9d9bdbb0 295void ath10k_htc_rx_completion_handler(struct ath10k *ar, struct sk_buff *skb)
5e3dd157
KV
296{
297 int status = 0;
cd003fad 298 struct ath10k_htc *htc = &ar->htc;
5e3dd157
KV
299 struct ath10k_htc_hdr *hdr;
300 struct ath10k_htc_ep *ep;
301 u16 payload_len;
302 u32 trailer_len = 0;
303 size_t min_len;
304 u8 eid;
305 bool trailer_present;
306
307 hdr = (struct ath10k_htc_hdr *)skb->data;
308 skb_pull(skb, sizeof(*hdr));
309
310 eid = hdr->eid;
311
312 if (eid >= ATH10K_HTC_EP_COUNT) {
7aa7a72a
MK
313 ath10k_warn(ar, "HTC Rx: invalid eid %d\n", eid);
314 ath10k_dbg_dump(ar, ATH10K_DBG_HTC, "htc bad header", "",
5e3dd157 315 hdr, sizeof(*hdr));
5e3dd157
KV
316 goto out;
317 }
318
319 ep = &htc->endpoint[eid];
320
5e3dd157
KV
321 payload_len = __le16_to_cpu(hdr->len);
322
323 if (payload_len + sizeof(*hdr) > ATH10K_HTC_MAX_LEN) {
7aa7a72a 324 ath10k_warn(ar, "HTC rx frame too long, len: %zu\n",
5e3dd157 325 payload_len + sizeof(*hdr));
7aa7a72a 326 ath10k_dbg_dump(ar, ATH10K_DBG_HTC, "htc bad rx pkt len", "",
5e3dd157 327 hdr, sizeof(*hdr));
5e3dd157
KV
328 goto out;
329 }
330
331 if (skb->len < payload_len) {
7aa7a72a 332 ath10k_dbg(ar, ATH10K_DBG_HTC,
5e3dd157
KV
333 "HTC Rx: insufficient length, got %d, expected %d\n",
334 skb->len, payload_len);
7aa7a72a 335 ath10k_dbg_dump(ar, ATH10K_DBG_HTC, "htc bad rx pkt len",
5e3dd157 336 "", hdr, sizeof(*hdr));
5e3dd157
KV
337 goto out;
338 }
339
340 /* get flags to check for trailer */
341 trailer_present = hdr->flags & ATH10K_HTC_FLAG_TRAILER_PRESENT;
342 if (trailer_present) {
343 u8 *trailer;
344
345 trailer_len = hdr->trailer_len;
346 min_len = sizeof(struct ath10k_ath10k_htc_record_hdr);
347
348 if ((trailer_len < min_len) ||
349 (trailer_len > payload_len)) {
7aa7a72a 350 ath10k_warn(ar, "Invalid trailer length: %d\n",
5e3dd157 351 trailer_len);
5e3dd157
KV
352 goto out;
353 }
354
355 trailer = (u8 *)hdr;
356 trailer += sizeof(*hdr);
357 trailer += payload_len;
358 trailer -= trailer_len;
359 status = ath10k_htc_process_trailer(htc, trailer,
360 trailer_len, hdr->eid);
361 if (status)
362 goto out;
363
364 skb_trim(skb, skb->len - trailer_len);
365 }
366
367 if (((int)payload_len - (int)trailer_len) <= 0)
368 /* zero length packet with trailer data, just drop these */
369 goto out;
370
371 if (eid == ATH10K_HTC_EP_0) {
372 struct ath10k_htc_msg *msg = (struct ath10k_htc_msg *)skb->data;
373
374 switch (__le16_to_cpu(msg->hdr.message_id)) {
5dc0f1db
MK
375 case ATH10K_HTC_MSG_READY_ID:
376 case ATH10K_HTC_MSG_CONNECT_SERVICE_RESP_ID:
5e3dd157
KV
377 /* handle HTC control message */
378 if (completion_done(&htc->ctl_resp)) {
379 /*
380 * this is a fatal error, target should not be
381 * sending unsolicited messages on the ep 0
382 */
7aa7a72a 383 ath10k_warn(ar, "HTC rx ctrl still processing\n");
5e3dd157
KV
384 complete(&htc->ctl_resp);
385 goto out;
386 }
387
388 htc->control_resp_len =
389 min_t(int, skb->len,
390 ATH10K_HTC_MAX_CTRL_MSG_LEN);
391
392 memcpy(htc->control_resp_buffer, skb->data,
393 htc->control_resp_len);
394
395 complete(&htc->ctl_resp);
396 break;
397 case ATH10K_HTC_MSG_SEND_SUSPEND_COMPLETE:
398 htc->htc_ops.target_send_suspend_complete(ar);
5dc0f1db
MK
399 break;
400 default:
401 ath10k_warn(ar, "ignoring unsolicited htc ep0 event\n");
402 break;
5e3dd157
KV
403 }
404 goto out;
405 }
406
75b34800 407 ath10k_dbg(ar, ATH10K_DBG_HTC, "htc rx completion ep %d skb %pK\n",
5e3dd157
KV
408 eid, skb);
409 ep->ep_ops.ep_rx_complete(ar, skb);
410
411 /* skb is now owned by the rx completion handler */
412 skb = NULL;
413out:
414 kfree_skb(skb);
5e3dd157 415}
aed1dc82 416EXPORT_SYMBOL(ath10k_htc_rx_completion_handler);
5e3dd157
KV
417
418static void ath10k_htc_control_rx_complete(struct ath10k *ar,
419 struct sk_buff *skb)
420{
421 /* This is unexpected. FW is not supposed to send regular rx on this
422 * endpoint. */
7aa7a72a 423 ath10k_warn(ar, "unexpected htc rx\n");
5e3dd157
KV
424 kfree_skb(skb);
425}
426
427/***************/
428/* Init/Deinit */
429/***************/
430
431static const char *htc_service_name(enum ath10k_htc_svc_id id)
432{
433 switch (id) {
434 case ATH10K_HTC_SVC_ID_RESERVED:
435 return "Reserved";
436 case ATH10K_HTC_SVC_ID_RSVD_CTRL:
437 return "Control";
438 case ATH10K_HTC_SVC_ID_WMI_CONTROL:
439 return "WMI";
440 case ATH10K_HTC_SVC_ID_WMI_DATA_BE:
441 return "DATA BE";
442 case ATH10K_HTC_SVC_ID_WMI_DATA_BK:
443 return "DATA BK";
444 case ATH10K_HTC_SVC_ID_WMI_DATA_VI:
445 return "DATA VI";
446 case ATH10K_HTC_SVC_ID_WMI_DATA_VO:
447 return "DATA VO";
448 case ATH10K_HTC_SVC_ID_NMI_CONTROL:
449 return "NMI Control";
450 case ATH10K_HTC_SVC_ID_NMI_DATA:
451 return "NMI Data";
452 case ATH10K_HTC_SVC_ID_HTT_DATA_MSG:
453 return "HTT Data";
454 case ATH10K_HTC_SVC_ID_TEST_RAW_STREAMS:
455 return "RAW";
456 }
457
458 return "Unknown";
459}
460
461static void ath10k_htc_reset_endpoint_states(struct ath10k_htc *htc)
462{
463 struct ath10k_htc_ep *ep;
464 int i;
465
466 for (i = ATH10K_HTC_EP_0; i < ATH10K_HTC_EP_COUNT; i++) {
467 ep = &htc->endpoint[i];
468 ep->service_id = ATH10K_HTC_SVC_ID_UNUSED;
469 ep->max_ep_message_len = 0;
470 ep->max_tx_queue_depth = 0;
471 ep->eid = i;
5e3dd157
KV
472 ep->htc = htc;
473 ep->tx_credit_flow_enabled = true;
5e3dd157
KV
474 }
475}
476
477static void ath10k_htc_setup_target_buffer_assignments(struct ath10k_htc *htc)
478{
479 struct ath10k_htc_svc_tx_credits *entry;
480
481 entry = &htc->service_tx_alloc[0];
482
483 /*
484 * for PCIE allocate all credists/HTC buffers to WMI.
485 * no buffers are used/required for data. data always
486 * remains on host.
487 */
488 entry++;
489 entry->service_id = ATH10K_HTC_SVC_ID_WMI_CONTROL;
490 entry->credit_allocation = htc->total_transmit_credits;
491}
492
493static u8 ath10k_htc_get_credit_allocation(struct ath10k_htc *htc,
494 u16 service_id)
495{
496 u8 allocation = 0;
497 int i;
498
499 for (i = 0; i < ATH10K_HTC_EP_COUNT; i++) {
500 if (htc->service_tx_alloc[i].service_id == service_id)
501 allocation =
502 htc->service_tx_alloc[i].credit_allocation;
503 }
504
505 return allocation;
506}
507
508int ath10k_htc_wait_target(struct ath10k_htc *htc)
509{
7aa7a72a 510 struct ath10k *ar = htc->ar;
da34fad6 511 int i, status = 0;
7c377c3d 512 unsigned long time_left;
5e3dd157
KV
513 struct ath10k_htc_svc_conn_req conn_req;
514 struct ath10k_htc_svc_conn_resp conn_resp;
515 struct ath10k_htc_msg *msg;
516 u16 message_id;
517 u16 credit_count;
518 u16 credit_size;
519
7c377c3d
NMG
520 time_left = wait_for_completion_timeout(&htc->ctl_resp,
521 ATH10K_HTC_WAIT_TIMEOUT_HZ);
522 if (!time_left) {
da34fad6
MK
523 /* Workaround: In some cases the PCI HIF doesn't
524 * receive interrupt for the control response message
525 * even if the buffer was completed. It is suspected
526 * iomap writes unmasking PCI CE irqs aren't propagated
527 * properly in KVM PCI-passthrough sometimes.
528 */
7aa7a72a 529 ath10k_warn(ar, "failed to receive control response completion, polling..\n");
da34fad6
MK
530
531 for (i = 0; i < CE_COUNT; i++)
532 ath10k_hif_send_complete_check(htc->ar, i, 1);
533
7c377c3d
NMG
534 time_left =
535 wait_for_completion_timeout(&htc->ctl_resp,
536 ATH10K_HTC_WAIT_TIMEOUT_HZ);
da34fad6 537
7c377c3d 538 if (!time_left)
5e3dd157 539 status = -ETIMEDOUT;
da34fad6 540 }
5e3dd157 541
da34fad6 542 if (status < 0) {
7aa7a72a 543 ath10k_err(ar, "ctl_resp never came in (%d)\n", status);
67e3c63f 544 return status;
5e3dd157
KV
545 }
546
547 if (htc->control_resp_len < sizeof(msg->hdr) + sizeof(msg->ready)) {
7aa7a72a 548 ath10k_err(ar, "Invalid HTC ready msg len:%d\n",
5e3dd157 549 htc->control_resp_len);
67e3c63f 550 return -ECOMM;
5e3dd157
KV
551 }
552
553 msg = (struct ath10k_htc_msg *)htc->control_resp_buffer;
554 message_id = __le16_to_cpu(msg->hdr.message_id);
555 credit_count = __le16_to_cpu(msg->ready.credit_count);
556 credit_size = __le16_to_cpu(msg->ready.credit_size);
557
558 if (message_id != ATH10K_HTC_MSG_READY_ID) {
7aa7a72a 559 ath10k_err(ar, "Invalid HTC ready msg: 0x%x\n", message_id);
67e3c63f 560 return -ECOMM;
5e3dd157
KV
561 }
562
563 htc->total_transmit_credits = credit_count;
564 htc->target_credit_size = credit_size;
565
7aa7a72a 566 ath10k_dbg(ar, ATH10K_DBG_HTC,
5e3dd157
KV
567 "Target ready! transmit resources: %d size:%d\n",
568 htc->total_transmit_credits,
569 htc->target_credit_size);
570
571 if ((htc->total_transmit_credits == 0) ||
572 (htc->target_credit_size == 0)) {
7aa7a72a 573 ath10k_err(ar, "Invalid credit size received\n");
67e3c63f 574 return -ECOMM;
5e3dd157
KV
575 }
576
577 ath10k_htc_setup_target_buffer_assignments(htc);
578
579 /* setup our pseudo HTC control endpoint connection */
580 memset(&conn_req, 0, sizeof(conn_req));
581 memset(&conn_resp, 0, sizeof(conn_resp));
582 conn_req.ep_ops.ep_tx_complete = ath10k_htc_control_tx_complete;
583 conn_req.ep_ops.ep_rx_complete = ath10k_htc_control_rx_complete;
584 conn_req.max_send_queue_depth = ATH10K_NUM_CONTROL_TX_BUFFERS;
585 conn_req.service_id = ATH10K_HTC_SVC_ID_RSVD_CTRL;
586
587 /* connect fake service */
588 status = ath10k_htc_connect_service(htc, &conn_req, &conn_resp);
589 if (status) {
7aa7a72a
MK
590 ath10k_err(ar, "could not connect to htc service (%d)\n",
591 status);
67e3c63f 592 return status;
5e3dd157
KV
593 }
594
595 return 0;
5e3dd157
KV
596}
597
598int ath10k_htc_connect_service(struct ath10k_htc *htc,
599 struct ath10k_htc_svc_conn_req *conn_req,
600 struct ath10k_htc_svc_conn_resp *conn_resp)
601{
7aa7a72a 602 struct ath10k *ar = htc->ar;
5e3dd157
KV
603 struct ath10k_htc_msg *msg;
604 struct ath10k_htc_conn_svc *req_msg;
605 struct ath10k_htc_conn_svc_response resp_msg_dummy;
606 struct ath10k_htc_conn_svc_response *resp_msg = &resp_msg_dummy;
607 enum ath10k_htc_ep_id assigned_eid = ATH10K_HTC_EP_COUNT;
608 struct ath10k_htc_ep *ep;
609 struct sk_buff *skb;
610 unsigned int max_msg_size = 0;
611 int length, status;
7c377c3d 612 unsigned long time_left;
5e3dd157
KV
613 bool disable_credit_flow_ctrl = false;
614 u16 message_id, service_id, flags = 0;
615 u8 tx_alloc = 0;
616
617 /* special case for HTC pseudo control service */
618 if (conn_req->service_id == ATH10K_HTC_SVC_ID_RSVD_CTRL) {
619 disable_credit_flow_ctrl = true;
620 assigned_eid = ATH10K_HTC_EP_0;
621 max_msg_size = ATH10K_HTC_MAX_CTRL_MSG_LEN;
622 memset(&resp_msg_dummy, 0, sizeof(resp_msg_dummy));
623 goto setup;
624 }
625
626 tx_alloc = ath10k_htc_get_credit_allocation(htc,
627 conn_req->service_id);
628 if (!tx_alloc)
7aa7a72a 629 ath10k_dbg(ar, ATH10K_DBG_BOOT,
42a2efbc 630 "boot htc service %s does not allocate target credits\n",
342004be 631 htc_service_name(conn_req->service_id));
5e3dd157
KV
632
633 skb = ath10k_htc_build_tx_ctrl_skb(htc->ar);
634 if (!skb) {
7aa7a72a 635 ath10k_err(ar, "Failed to allocate HTC packet\n");
5e3dd157
KV
636 return -ENOMEM;
637 }
638
639 length = sizeof(msg->hdr) + sizeof(msg->connect_service);
640 skb_put(skb, length);
641 memset(skb->data, 0, length);
642
643 msg = (struct ath10k_htc_msg *)skb->data;
644 msg->hdr.message_id =
645 __cpu_to_le16(ATH10K_HTC_MSG_CONNECT_SERVICE_ID);
646
647 flags |= SM(tx_alloc, ATH10K_HTC_CONN_FLAGS_RECV_ALLOC);
648
5e3dd157
KV
649 /* Only enable credit flow control for WMI ctrl service */
650 if (conn_req->service_id != ATH10K_HTC_SVC_ID_WMI_CONTROL) {
651 flags |= ATH10K_HTC_CONN_FLAGS_DISABLE_CREDIT_FLOW_CTRL;
652 disable_credit_flow_ctrl = true;
653 }
654
0e1cbf9a
MK
655 req_msg = &msg->connect_service;
656 req_msg->flags = __cpu_to_le16(flags);
657 req_msg->service_id = __cpu_to_le16(conn_req->service_id);
658
16735d02 659 reinit_completion(&htc->ctl_resp);
5e3dd157
KV
660
661 status = ath10k_htc_send(htc, ATH10K_HTC_EP_0, skb);
662 if (status) {
663 kfree_skb(skb);
664 return status;
665 }
666
667 /* wait for response */
7c377c3d
NMG
668 time_left = wait_for_completion_timeout(&htc->ctl_resp,
669 ATH10K_HTC_CONN_SVC_TIMEOUT_HZ);
670 if (!time_left) {
671 ath10k_err(ar, "Service connect timeout\n");
38e2a644 672 return -ETIMEDOUT;
5e3dd157
KV
673 }
674
675 /* we controlled the buffer creation, it's aligned */
676 msg = (struct ath10k_htc_msg *)htc->control_resp_buffer;
677 resp_msg = &msg->connect_service_response;
678 message_id = __le16_to_cpu(msg->hdr.message_id);
679 service_id = __le16_to_cpu(resp_msg->service_id);
680
681 if ((message_id != ATH10K_HTC_MSG_CONNECT_SERVICE_RESP_ID) ||
682 (htc->control_resp_len < sizeof(msg->hdr) +
683 sizeof(msg->connect_service_response))) {
7aa7a72a 684 ath10k_err(ar, "Invalid resp message ID 0x%x", message_id);
5e3dd157
KV
685 return -EPROTO;
686 }
687
7aa7a72a 688 ath10k_dbg(ar, ATH10K_DBG_HTC,
5e3dd157
KV
689 "HTC Service %s connect response: status: 0x%x, assigned ep: 0x%x\n",
690 htc_service_name(service_id),
691 resp_msg->status, resp_msg->eid);
692
693 conn_resp->connect_resp_code = resp_msg->status;
694
695 /* check response status */
696 if (resp_msg->status != ATH10K_HTC_CONN_SVC_STATUS_SUCCESS) {
7aa7a72a 697 ath10k_err(ar, "HTC Service %s connect request failed: 0x%x)\n",
5e3dd157
KV
698 htc_service_name(service_id),
699 resp_msg->status);
700 return -EPROTO;
701 }
702
703 assigned_eid = (enum ath10k_htc_ep_id)resp_msg->eid;
704 max_msg_size = __le16_to_cpu(resp_msg->max_msg_size);
705
706setup:
707
708 if (assigned_eid >= ATH10K_HTC_EP_COUNT)
709 return -EPROTO;
710
711 if (max_msg_size == 0)
712 return -EPROTO;
713
714 ep = &htc->endpoint[assigned_eid];
715 ep->eid = assigned_eid;
716
717 if (ep->service_id != ATH10K_HTC_SVC_ID_UNUSED)
718 return -EPROTO;
719
720 /* return assigned endpoint to caller */
721 conn_resp->eid = assigned_eid;
722 conn_resp->max_msg_len = __le16_to_cpu(resp_msg->max_msg_size);
723
724 /* setup the endpoint */
725 ep->service_id = conn_req->service_id;
726 ep->max_tx_queue_depth = conn_req->max_send_queue_depth;
727 ep->max_ep_message_len = __le16_to_cpu(resp_msg->max_msg_size);
728 ep->tx_credits = tx_alloc;
729 ep->tx_credit_size = htc->target_credit_size;
730 ep->tx_credits_per_max_message = ep->max_ep_message_len /
731 htc->target_credit_size;
732
733 if (ep->max_ep_message_len % htc->target_credit_size)
734 ep->tx_credits_per_max_message++;
735
736 /* copy all the callbacks */
737 ep->ep_ops = conn_req->ep_ops;
738
739 status = ath10k_hif_map_service_to_pipe(htc->ar,
740 ep->service_id,
741 &ep->ul_pipe_id,
400143e4 742 &ep->dl_pipe_id);
5e3dd157
KV
743 if (status)
744 return status;
745
7aa7a72a 746 ath10k_dbg(ar, ATH10K_DBG_BOOT,
42a2efbc 747 "boot htc service '%s' ul pipe %d dl pipe %d eid %d ready\n",
5e3dd157
KV
748 htc_service_name(ep->service_id), ep->ul_pipe_id,
749 ep->dl_pipe_id, ep->eid);
750
5e3dd157
KV
751 if (disable_credit_flow_ctrl && ep->tx_credit_flow_enabled) {
752 ep->tx_credit_flow_enabled = false;
7aa7a72a 753 ath10k_dbg(ar, ATH10K_DBG_BOOT,
42a2efbc 754 "boot htc service '%s' eid %d TX flow control disabled\n",
5e3dd157
KV
755 htc_service_name(ep->service_id), assigned_eid);
756 }
757
758 return status;
759}
760
7aa7a72a 761struct sk_buff *ath10k_htc_alloc_skb(struct ath10k *ar, int size)
5e3dd157
KV
762{
763 struct sk_buff *skb;
764
765 skb = dev_alloc_skb(size + sizeof(struct ath10k_htc_hdr));
8cc7f26c 766 if (!skb)
5e3dd157 767 return NULL;
5e3dd157
KV
768
769 skb_reserve(skb, sizeof(struct ath10k_htc_hdr));
770
771 /* FW/HTC requires 4-byte aligned streams */
772 if (!IS_ALIGNED((unsigned long)skb->data, 4))
7aa7a72a 773 ath10k_warn(ar, "Unaligned HTC tx skb\n");
5e3dd157
KV
774
775 return skb;
776}
777
778int ath10k_htc_start(struct ath10k_htc *htc)
779{
7aa7a72a 780 struct ath10k *ar = htc->ar;
5e3dd157
KV
781 struct sk_buff *skb;
782 int status = 0;
783 struct ath10k_htc_msg *msg;
784
785 skb = ath10k_htc_build_tx_ctrl_skb(htc->ar);
786 if (!skb)
787 return -ENOMEM;
788
789 skb_put(skb, sizeof(msg->hdr) + sizeof(msg->setup_complete_ext));
790 memset(skb->data, 0, skb->len);
791
792 msg = (struct ath10k_htc_msg *)skb->data;
793 msg->hdr.message_id =
794 __cpu_to_le16(ATH10K_HTC_MSG_SETUP_COMPLETE_EX_ID);
795
7aa7a72a 796 ath10k_dbg(ar, ATH10K_DBG_HTC, "HTC is using TX credit flow control\n");
5e3dd157
KV
797
798 status = ath10k_htc_send(htc, ATH10K_HTC_EP_0, skb);
799 if (status) {
800 kfree_skb(skb);
801 return status;
802 }
803
804 return 0;
805}
806
5e3dd157 807/* registered target arrival callback from the HIF layer */
cd003fad 808int ath10k_htc_init(struct ath10k *ar)
5e3dd157 809{
5e3dd157 810 struct ath10k_htc_ep *ep = NULL;
cd003fad 811 struct ath10k_htc *htc = &ar->htc;
5e3dd157
KV
812
813 spin_lock_init(&htc->tx_lock);
814
5e3dd157
KV
815 ath10k_htc_reset_endpoint_states(htc);
816
5e3dd157
KV
817 htc->ar = ar;
818
819 /* Get HIF default pipe for HTC message exchange */
820 ep = &htc->endpoint[ATH10K_HTC_EP_0];
821
5e3dd157
KV
822 ath10k_hif_get_default_pipe(ar, &ep->ul_pipe_id, &ep->dl_pipe_id);
823
824 init_completion(&htc->ctl_resp);
825
cd003fad 826 return 0;
5e3dd157 827}