blob: 69f1f4696c2551a8a9052a1adcf8426bc471389b [file] [log] [blame]
Kalle Valo5e3dd152013-06-12 20:52:10 +03001/*
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
26static inline void ath10k_htc_send_complete_check(struct ath10k_htc_ep *ep,
27 int force)
28{
29 /*
30 * Check whether HIF has any prior sends that have finished,
31 * have not had the post-processing done.
32 */
33 ath10k_hif_send_complete_check(ep->htc->ar, ep->ul_pipe_id, force);
34}
35
36static void ath10k_htc_control_tx_complete(struct ath10k *ar,
37 struct sk_buff *skb)
38{
39 kfree_skb(skb);
40}
41
42static struct sk_buff *ath10k_htc_build_tx_ctrl_skb(void *ar)
43{
44 struct sk_buff *skb;
45 struct ath10k_skb_cb *skb_cb;
46
47 skb = dev_alloc_skb(ATH10K_HTC_CONTROL_BUFFER_SIZE);
48 if (!skb) {
49 ath10k_warn("Unable to allocate ctrl skb\n");
50 return NULL;
51 }
52
53 skb_reserve(skb, 20); /* FIXME: why 20 bytes? */
54 WARN_ONCE((unsigned long)skb->data & 3, "unaligned skb");
55
56 skb_cb = ATH10K_SKB_CB(skb);
57 memset(skb_cb, 0, sizeof(*skb_cb));
58
59 ath10k_dbg(ATH10K_DBG_HTC, "%s: skb %p\n", __func__, skb);
60 return skb;
61}
62
63static inline void ath10k_htc_restore_tx_skb(struct ath10k_htc *htc,
64 struct sk_buff *skb)
65{
Michal Kazior767d34f2014-02-27 18:50:03 +020066 struct ath10k_skb_cb *skb_cb = ATH10K_SKB_CB(skb);
67
68 dma_unmap_single(htc->ar->dev, skb_cb->paddr, skb->len, DMA_TO_DEVICE);
Kalle Valo5e3dd152013-06-12 20:52:10 +030069 skb_pull(skb, sizeof(struct ath10k_htc_hdr));
70}
71
72static void ath10k_htc_notify_tx_completion(struct ath10k_htc_ep *ep,
73 struct sk_buff *skb)
74{
75 ath10k_dbg(ATH10K_DBG_HTC, "%s: ep %d skb %p\n", __func__,
76 ep->eid, skb);
77
78 ath10k_htc_restore_tx_skb(ep->htc, skb);
79
80 if (!ep->ep_ops.ep_tx_complete) {
81 ath10k_warn("no tx handler for eid %d\n", ep->eid);
82 dev_kfree_skb_any(skb);
83 return;
84 }
85
86 ep->ep_ops.ep_tx_complete(ep->htc->ar, skb);
87}
88
89/* assumes tx_lock is held */
90static bool ath10k_htc_ep_need_credit_update(struct ath10k_htc_ep *ep)
91{
92 if (!ep->tx_credit_flow_enabled)
93 return false;
94 if (ep->tx_credits >= ep->tx_credits_per_max_message)
95 return false;
96
97 ath10k_dbg(ATH10K_DBG_HTC, "HTC: endpoint %d needs credit update\n",
98 ep->eid);
99 return true;
100}
101
102static void ath10k_htc_prepare_tx_skb(struct ath10k_htc_ep *ep,
103 struct sk_buff *skb)
104{
105 struct ath10k_htc_hdr *hdr;
106
107 hdr = (struct ath10k_htc_hdr *)skb->data;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300108
109 hdr->eid = ep->eid;
110 hdr->len = __cpu_to_le16(skb->len - sizeof(*hdr));
Michal Kazior27bb1782013-09-18 14:43:19 +0200111 hdr->flags = 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300112
113 spin_lock_bh(&ep->htc->tx_lock);
114 hdr->seq_no = ep->seq_no++;
115
116 if (ath10k_htc_ep_need_credit_update(ep))
117 hdr->flags |= ATH10K_HTC_FLAG_NEED_CREDIT_UPDATE;
118
119 spin_unlock_bh(&ep->htc->tx_lock);
120}
121
Kalle Valo5e3dd152013-06-12 20:52:10 +0300122int ath10k_htc_send(struct ath10k_htc *htc,
123 enum ath10k_htc_ep_id eid,
124 struct sk_buff *skb)
125{
126 struct ath10k_htc_ep *ep = &htc->endpoint[eid];
Michal Kazior767d34f2014-02-27 18:50:03 +0200127 struct ath10k_skb_cb *skb_cb = ATH10K_SKB_CB(skb);
128 struct device *dev = htc->ar->dev;
Michal Kazior12acbc42013-09-13 14:16:55 +0200129 int credits = 0;
130 int ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300131
Michal Kazioraffd3212013-07-16 09:54:35 +0200132 if (htc->ar->state == ATH10K_STATE_WEDGED)
133 return -ECOMM;
134
Kalle Valo5e3dd152013-06-12 20:52:10 +0300135 if (eid >= ATH10K_HTC_EP_COUNT) {
136 ath10k_warn("Invalid endpoint id: %d\n", eid);
137 return -ENOENT;
138 }
139
Michal Kazior12acbc42013-09-13 14:16:55 +0200140 /* FIXME: This looks ugly, can we fix it? */
Kalle Valo5e3dd152013-06-12 20:52:10 +0300141 spin_lock_bh(&htc->tx_lock);
Michal Kazior08fe9b42013-07-22 14:13:28 +0200142 if (htc->stopped) {
143 spin_unlock_bh(&htc->tx_lock);
144 return -ESHUTDOWN;
145 }
Kalle Valo5e3dd152013-06-12 20:52:10 +0300146 spin_unlock_bh(&htc->tx_lock);
147
Michal Kazior12acbc42013-09-13 14:16:55 +0200148 skb_push(skb, sizeof(struct ath10k_htc_hdr));
149
150 if (ep->tx_credit_flow_enabled) {
151 credits = DIV_ROUND_UP(skb->len, htc->target_credit_size);
152 spin_lock_bh(&htc->tx_lock);
153 if (ep->tx_credits < credits) {
154 spin_unlock_bh(&htc->tx_lock);
155 ret = -EAGAIN;
156 goto err_pull;
157 }
158 ep->tx_credits -= credits;
159 spin_unlock_bh(&htc->tx_lock);
160 }
161
162 ath10k_htc_prepare_tx_skb(ep, skb);
163
Michal Kazior767d34f2014-02-27 18:50:03 +0200164 skb_cb->paddr = dma_map_single(dev, skb->data, skb->len, DMA_TO_DEVICE);
165 ret = dma_mapping_error(dev, skb_cb->paddr);
Michal Kazior12acbc42013-09-13 14:16:55 +0200166 if (ret)
167 goto err_credits;
168
169 ret = ath10k_hif_send_head(htc->ar, ep->ul_pipe_id, ep->eid,
170 skb->len, skb);
171 if (ret)
172 goto err_unmap;
173
Kalle Valo5e3dd152013-06-12 20:52:10 +0300174 return 0;
Michal Kazior12acbc42013-09-13 14:16:55 +0200175
176err_unmap:
Michal Kazior767d34f2014-02-27 18:50:03 +0200177 dma_unmap_single(dev, skb_cb->paddr, skb->len, DMA_TO_DEVICE);
Michal Kazior12acbc42013-09-13 14:16:55 +0200178err_credits:
179 if (ep->tx_credit_flow_enabled) {
180 spin_lock_bh(&htc->tx_lock);
181 ep->tx_credits += credits;
182 spin_unlock_bh(&htc->tx_lock);
183
184 if (ep->ep_ops.ep_tx_credits)
185 ep->ep_ops.ep_tx_credits(htc->ar);
186 }
187err_pull:
188 skb_pull(skb, sizeof(struct ath10k_htc_hdr));
189 return ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300190}
191
192static int ath10k_htc_tx_completion_handler(struct ath10k *ar,
193 struct sk_buff *skb,
194 unsigned int eid)
195{
Michal Kaziorcd003fa2013-07-05 16:15:13 +0300196 struct ath10k_htc *htc = &ar->htc;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300197 struct ath10k_htc_ep *ep = &htc->endpoint[eid];
Kalle Valo5e3dd152013-06-12 20:52:10 +0300198
Michal Kazior2415fc12013-11-08 08:01:32 +0100199 if (!skb) {
200 ath10k_warn("invalid sk_buff completion - NULL pointer. firmware crashed?\n");
201 return 0;
202 }
203
Kalle Valo5e3dd152013-06-12 20:52:10 +0300204 ath10k_htc_notify_tx_completion(ep, skb);
205 /* the skb now belongs to the completion handler */
206
Kalle Valo5e3dd152013-06-12 20:52:10 +0300207 return 0;
208}
209
Kalle Valo5e3dd152013-06-12 20:52:10 +0300210/***********/
211/* Receive */
212/***********/
213
214static void
215ath10k_htc_process_credit_report(struct ath10k_htc *htc,
216 const struct ath10k_htc_credit_report *report,
217 int len,
218 enum ath10k_htc_ep_id eid)
219{
220 struct ath10k_htc_ep *ep;
221 int i, n_reports;
222
223 if (len % sizeof(*report))
224 ath10k_warn("Uneven credit report len %d", len);
225
226 n_reports = len / sizeof(*report);
227
228 spin_lock_bh(&htc->tx_lock);
229 for (i = 0; i < n_reports; i++, report++) {
230 if (report->eid >= ATH10K_HTC_EP_COUNT)
231 break;
232
233 ath10k_dbg(ATH10K_DBG_HTC, "ep %d got %d credits\n",
234 report->eid, report->credits);
235
236 ep = &htc->endpoint[report->eid];
237 ep->tx_credits += report->credits;
238
Michal Kazior88e65fc2013-09-13 14:16:53 +0200239 if (ep->ep_ops.ep_tx_credits) {
240 spin_unlock_bh(&htc->tx_lock);
241 ep->ep_ops.ep_tx_credits(htc->ar);
242 spin_lock_bh(&htc->tx_lock);
243 }
Kalle Valo5e3dd152013-06-12 20:52:10 +0300244 }
245 spin_unlock_bh(&htc->tx_lock);
246}
247
248static int ath10k_htc_process_trailer(struct ath10k_htc *htc,
249 u8 *buffer,
250 int length,
251 enum ath10k_htc_ep_id src_eid)
252{
253 int status = 0;
254 struct ath10k_htc_record *record;
255 u8 *orig_buffer;
256 int orig_length;
257 size_t len;
258
259 orig_buffer = buffer;
260 orig_length = length;
261
262 while (length > 0) {
263 record = (struct ath10k_htc_record *)buffer;
264
265 if (length < sizeof(record->hdr)) {
266 status = -EINVAL;
267 break;
268 }
269
270 if (record->hdr.len > length) {
271 /* no room left in buffer for record */
272 ath10k_warn("Invalid record length: %d\n",
273 record->hdr.len);
274 status = -EINVAL;
275 break;
276 }
277
278 switch (record->hdr.id) {
279 case ATH10K_HTC_RECORD_CREDITS:
280 len = sizeof(struct ath10k_htc_credit_report);
281 if (record->hdr.len < len) {
282 ath10k_warn("Credit report too long\n");
283 status = -EINVAL;
284 break;
285 }
286 ath10k_htc_process_credit_report(htc,
287 record->credit_report,
288 record->hdr.len,
289 src_eid);
290 break;
291 default:
292 ath10k_warn("Unhandled record: id:%d length:%d\n",
293 record->hdr.id, record->hdr.len);
294 break;
295 }
296
297 if (status)
298 break;
299
300 /* multiple records may be present in a trailer */
301 buffer += sizeof(record->hdr) + record->hdr.len;
302 length -= sizeof(record->hdr) + record->hdr.len;
303 }
304
305 if (status)
306 ath10k_dbg_dump(ATH10K_DBG_HTC, "htc rx bad trailer", "",
307 orig_buffer, orig_length);
308
309 return status;
310}
311
312static int ath10k_htc_rx_completion_handler(struct ath10k *ar,
313 struct sk_buff *skb,
314 u8 pipe_id)
315{
316 int status = 0;
Michal Kaziorcd003fa2013-07-05 16:15:13 +0300317 struct ath10k_htc *htc = &ar->htc;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300318 struct ath10k_htc_hdr *hdr;
319 struct ath10k_htc_ep *ep;
320 u16 payload_len;
321 u32 trailer_len = 0;
322 size_t min_len;
323 u8 eid;
324 bool trailer_present;
325
326 hdr = (struct ath10k_htc_hdr *)skb->data;
327 skb_pull(skb, sizeof(*hdr));
328
329 eid = hdr->eid;
330
331 if (eid >= ATH10K_HTC_EP_COUNT) {
332 ath10k_warn("HTC Rx: invalid eid %d\n", eid);
333 ath10k_dbg_dump(ATH10K_DBG_HTC, "htc bad header", "",
334 hdr, sizeof(*hdr));
335 status = -EINVAL;
336 goto out;
337 }
338
339 ep = &htc->endpoint[eid];
340
341 /*
342 * If this endpoint that received a message from the target has
343 * a to-target HIF pipe whose send completions are polled rather
344 * than interrupt-driven, this is a good point to ask HIF to check
345 * whether it has any completed sends to handle.
346 */
347 if (ep->ul_is_polled)
348 ath10k_htc_send_complete_check(ep, 1);
349
350 payload_len = __le16_to_cpu(hdr->len);
351
352 if (payload_len + sizeof(*hdr) > ATH10K_HTC_MAX_LEN) {
353 ath10k_warn("HTC rx frame too long, len: %zu\n",
354 payload_len + sizeof(*hdr));
355 ath10k_dbg_dump(ATH10K_DBG_HTC, "htc bad rx pkt len", "",
356 hdr, sizeof(*hdr));
357 status = -EINVAL;
358 goto out;
359 }
360
361 if (skb->len < payload_len) {
362 ath10k_dbg(ATH10K_DBG_HTC,
363 "HTC Rx: insufficient length, got %d, expected %d\n",
364 skb->len, payload_len);
365 ath10k_dbg_dump(ATH10K_DBG_HTC, "htc bad rx pkt len",
366 "", hdr, sizeof(*hdr));
367 status = -EINVAL;
368 goto out;
369 }
370
371 /* get flags to check for trailer */
372 trailer_present = hdr->flags & ATH10K_HTC_FLAG_TRAILER_PRESENT;
373 if (trailer_present) {
374 u8 *trailer;
375
376 trailer_len = hdr->trailer_len;
377 min_len = sizeof(struct ath10k_ath10k_htc_record_hdr);
378
379 if ((trailer_len < min_len) ||
380 (trailer_len > payload_len)) {
381 ath10k_warn("Invalid trailer length: %d\n",
382 trailer_len);
383 status = -EPROTO;
384 goto out;
385 }
386
387 trailer = (u8 *)hdr;
388 trailer += sizeof(*hdr);
389 trailer += payload_len;
390 trailer -= trailer_len;
391 status = ath10k_htc_process_trailer(htc, trailer,
392 trailer_len, hdr->eid);
393 if (status)
394 goto out;
395
396 skb_trim(skb, skb->len - trailer_len);
397 }
398
399 if (((int)payload_len - (int)trailer_len) <= 0)
400 /* zero length packet with trailer data, just drop these */
401 goto out;
402
403 if (eid == ATH10K_HTC_EP_0) {
404 struct ath10k_htc_msg *msg = (struct ath10k_htc_msg *)skb->data;
405
406 switch (__le16_to_cpu(msg->hdr.message_id)) {
407 default:
408 /* handle HTC control message */
409 if (completion_done(&htc->ctl_resp)) {
410 /*
411 * this is a fatal error, target should not be
412 * sending unsolicited messages on the ep 0
413 */
414 ath10k_warn("HTC rx ctrl still processing\n");
415 status = -EINVAL;
416 complete(&htc->ctl_resp);
417 goto out;
418 }
419
420 htc->control_resp_len =
421 min_t(int, skb->len,
422 ATH10K_HTC_MAX_CTRL_MSG_LEN);
423
424 memcpy(htc->control_resp_buffer, skb->data,
425 htc->control_resp_len);
426
427 complete(&htc->ctl_resp);
428 break;
429 case ATH10K_HTC_MSG_SEND_SUSPEND_COMPLETE:
430 htc->htc_ops.target_send_suspend_complete(ar);
431 }
432 goto out;
433 }
434
435 ath10k_dbg(ATH10K_DBG_HTC, "htc rx completion ep %d skb %p\n",
436 eid, skb);
437 ep->ep_ops.ep_rx_complete(ar, skb);
438
439 /* skb is now owned by the rx completion handler */
440 skb = NULL;
441out:
442 kfree_skb(skb);
443
444 return status;
445}
446
447static void ath10k_htc_control_rx_complete(struct ath10k *ar,
448 struct sk_buff *skb)
449{
450 /* This is unexpected. FW is not supposed to send regular rx on this
451 * endpoint. */
452 ath10k_warn("unexpected htc rx\n");
453 kfree_skb(skb);
454}
455
456/***************/
457/* Init/Deinit */
458/***************/
459
460static const char *htc_service_name(enum ath10k_htc_svc_id id)
461{
462 switch (id) {
463 case ATH10K_HTC_SVC_ID_RESERVED:
464 return "Reserved";
465 case ATH10K_HTC_SVC_ID_RSVD_CTRL:
466 return "Control";
467 case ATH10K_HTC_SVC_ID_WMI_CONTROL:
468 return "WMI";
469 case ATH10K_HTC_SVC_ID_WMI_DATA_BE:
470 return "DATA BE";
471 case ATH10K_HTC_SVC_ID_WMI_DATA_BK:
472 return "DATA BK";
473 case ATH10K_HTC_SVC_ID_WMI_DATA_VI:
474 return "DATA VI";
475 case ATH10K_HTC_SVC_ID_WMI_DATA_VO:
476 return "DATA VO";
477 case ATH10K_HTC_SVC_ID_NMI_CONTROL:
478 return "NMI Control";
479 case ATH10K_HTC_SVC_ID_NMI_DATA:
480 return "NMI Data";
481 case ATH10K_HTC_SVC_ID_HTT_DATA_MSG:
482 return "HTT Data";
483 case ATH10K_HTC_SVC_ID_TEST_RAW_STREAMS:
484 return "RAW";
485 }
486
487 return "Unknown";
488}
489
490static void ath10k_htc_reset_endpoint_states(struct ath10k_htc *htc)
491{
492 struct ath10k_htc_ep *ep;
493 int i;
494
495 for (i = ATH10K_HTC_EP_0; i < ATH10K_HTC_EP_COUNT; i++) {
496 ep = &htc->endpoint[i];
497 ep->service_id = ATH10K_HTC_SVC_ID_UNUSED;
498 ep->max_ep_message_len = 0;
499 ep->max_tx_queue_depth = 0;
500 ep->eid = i;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300501 ep->htc = htc;
502 ep->tx_credit_flow_enabled = true;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300503 }
504}
505
506static void ath10k_htc_setup_target_buffer_assignments(struct ath10k_htc *htc)
507{
508 struct ath10k_htc_svc_tx_credits *entry;
509
510 entry = &htc->service_tx_alloc[0];
511
512 /*
513 * for PCIE allocate all credists/HTC buffers to WMI.
514 * no buffers are used/required for data. data always
515 * remains on host.
516 */
517 entry++;
518 entry->service_id = ATH10K_HTC_SVC_ID_WMI_CONTROL;
519 entry->credit_allocation = htc->total_transmit_credits;
520}
521
522static u8 ath10k_htc_get_credit_allocation(struct ath10k_htc *htc,
523 u16 service_id)
524{
525 u8 allocation = 0;
526 int i;
527
528 for (i = 0; i < ATH10K_HTC_EP_COUNT; i++) {
529 if (htc->service_tx_alloc[i].service_id == service_id)
530 allocation =
531 htc->service_tx_alloc[i].credit_allocation;
532 }
533
534 return allocation;
535}
536
537int ath10k_htc_wait_target(struct ath10k_htc *htc)
538{
539 int status = 0;
540 struct ath10k_htc_svc_conn_req conn_req;
541 struct ath10k_htc_svc_conn_resp conn_resp;
542 struct ath10k_htc_msg *msg;
543 u16 message_id;
544 u16 credit_count;
545 u16 credit_size;
546
Kalle Valo5e3dd152013-06-12 20:52:10 +0300547 status = wait_for_completion_timeout(&htc->ctl_resp,
548 ATH10K_HTC_WAIT_TIMEOUT_HZ);
549 if (status <= 0) {
550 if (status == 0)
551 status = -ETIMEDOUT;
552
553 ath10k_err("ctl_resp never came in (%d)\n", status);
Michal Kazior67e3c632013-11-08 08:05:18 +0100554 return status;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300555 }
556
557 if (htc->control_resp_len < sizeof(msg->hdr) + sizeof(msg->ready)) {
558 ath10k_err("Invalid HTC ready msg len:%d\n",
559 htc->control_resp_len);
Michal Kazior67e3c632013-11-08 08:05:18 +0100560 return -ECOMM;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300561 }
562
563 msg = (struct ath10k_htc_msg *)htc->control_resp_buffer;
564 message_id = __le16_to_cpu(msg->hdr.message_id);
565 credit_count = __le16_to_cpu(msg->ready.credit_count);
566 credit_size = __le16_to_cpu(msg->ready.credit_size);
567
568 if (message_id != ATH10K_HTC_MSG_READY_ID) {
569 ath10k_err("Invalid HTC ready msg: 0x%x\n", message_id);
Michal Kazior67e3c632013-11-08 08:05:18 +0100570 return -ECOMM;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300571 }
572
573 htc->total_transmit_credits = credit_count;
574 htc->target_credit_size = credit_size;
575
576 ath10k_dbg(ATH10K_DBG_HTC,
577 "Target ready! transmit resources: %d size:%d\n",
578 htc->total_transmit_credits,
579 htc->target_credit_size);
580
581 if ((htc->total_transmit_credits == 0) ||
582 (htc->target_credit_size == 0)) {
Kalle Valo5e3dd152013-06-12 20:52:10 +0300583 ath10k_err("Invalid credit size received\n");
Michal Kazior67e3c632013-11-08 08:05:18 +0100584 return -ECOMM;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300585 }
586
587 ath10k_htc_setup_target_buffer_assignments(htc);
588
589 /* setup our pseudo HTC control endpoint connection */
590 memset(&conn_req, 0, sizeof(conn_req));
591 memset(&conn_resp, 0, sizeof(conn_resp));
592 conn_req.ep_ops.ep_tx_complete = ath10k_htc_control_tx_complete;
593 conn_req.ep_ops.ep_rx_complete = ath10k_htc_control_rx_complete;
594 conn_req.max_send_queue_depth = ATH10K_NUM_CONTROL_TX_BUFFERS;
595 conn_req.service_id = ATH10K_HTC_SVC_ID_RSVD_CTRL;
596
597 /* connect fake service */
598 status = ath10k_htc_connect_service(htc, &conn_req, &conn_resp);
599 if (status) {
600 ath10k_err("could not connect to htc service (%d)\n", status);
Michal Kazior67e3c632013-11-08 08:05:18 +0100601 return status;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300602 }
603
604 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300605}
606
607int ath10k_htc_connect_service(struct ath10k_htc *htc,
608 struct ath10k_htc_svc_conn_req *conn_req,
609 struct ath10k_htc_svc_conn_resp *conn_resp)
610{
611 struct ath10k_htc_msg *msg;
612 struct ath10k_htc_conn_svc *req_msg;
613 struct ath10k_htc_conn_svc_response resp_msg_dummy;
614 struct ath10k_htc_conn_svc_response *resp_msg = &resp_msg_dummy;
615 enum ath10k_htc_ep_id assigned_eid = ATH10K_HTC_EP_COUNT;
616 struct ath10k_htc_ep *ep;
617 struct sk_buff *skb;
618 unsigned int max_msg_size = 0;
619 int length, status;
620 bool disable_credit_flow_ctrl = false;
621 u16 message_id, service_id, flags = 0;
622 u8 tx_alloc = 0;
623
624 /* special case for HTC pseudo control service */
625 if (conn_req->service_id == ATH10K_HTC_SVC_ID_RSVD_CTRL) {
626 disable_credit_flow_ctrl = true;
627 assigned_eid = ATH10K_HTC_EP_0;
628 max_msg_size = ATH10K_HTC_MAX_CTRL_MSG_LEN;
629 memset(&resp_msg_dummy, 0, sizeof(resp_msg_dummy));
630 goto setup;
631 }
632
633 tx_alloc = ath10k_htc_get_credit_allocation(htc,
634 conn_req->service_id);
635 if (!tx_alloc)
Kalle Valo42a2efb2013-09-08 17:55:56 +0300636 ath10k_dbg(ATH10K_DBG_BOOT,
637 "boot htc service %s does not allocate target credits\n",
Michal Kazior342004be2013-07-05 16:15:09 +0300638 htc_service_name(conn_req->service_id));
Kalle Valo5e3dd152013-06-12 20:52:10 +0300639
640 skb = ath10k_htc_build_tx_ctrl_skb(htc->ar);
641 if (!skb) {
642 ath10k_err("Failed to allocate HTC packet\n");
643 return -ENOMEM;
644 }
645
646 length = sizeof(msg->hdr) + sizeof(msg->connect_service);
647 skb_put(skb, length);
648 memset(skb->data, 0, length);
649
650 msg = (struct ath10k_htc_msg *)skb->data;
651 msg->hdr.message_id =
652 __cpu_to_le16(ATH10K_HTC_MSG_CONNECT_SERVICE_ID);
653
654 flags |= SM(tx_alloc, ATH10K_HTC_CONN_FLAGS_RECV_ALLOC);
655
Kalle Valo5e3dd152013-06-12 20:52:10 +0300656 /* Only enable credit flow control for WMI ctrl service */
657 if (conn_req->service_id != ATH10K_HTC_SVC_ID_WMI_CONTROL) {
658 flags |= ATH10K_HTC_CONN_FLAGS_DISABLE_CREDIT_FLOW_CTRL;
659 disable_credit_flow_ctrl = true;
660 }
661
Michal Kazior0e1cbf92013-08-13 07:59:35 +0200662 req_msg = &msg->connect_service;
663 req_msg->flags = __cpu_to_le16(flags);
664 req_msg->service_id = __cpu_to_le16(conn_req->service_id);
665
Wolfram Sang16735d02013-11-14 14:32:02 -0800666 reinit_completion(&htc->ctl_resp);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300667
668 status = ath10k_htc_send(htc, ATH10K_HTC_EP_0, skb);
669 if (status) {
670 kfree_skb(skb);
671 return status;
672 }
673
674 /* wait for response */
675 status = wait_for_completion_timeout(&htc->ctl_resp,
676 ATH10K_HTC_CONN_SVC_TIMEOUT_HZ);
677 if (status <= 0) {
678 if (status == 0)
679 status = -ETIMEDOUT;
680 ath10k_err("Service connect timeout: %d\n", status);
681 return status;
682 }
683
684 /* we controlled the buffer creation, it's aligned */
685 msg = (struct ath10k_htc_msg *)htc->control_resp_buffer;
686 resp_msg = &msg->connect_service_response;
687 message_id = __le16_to_cpu(msg->hdr.message_id);
688 service_id = __le16_to_cpu(resp_msg->service_id);
689
690 if ((message_id != ATH10K_HTC_MSG_CONNECT_SERVICE_RESP_ID) ||
691 (htc->control_resp_len < sizeof(msg->hdr) +
692 sizeof(msg->connect_service_response))) {
693 ath10k_err("Invalid resp message ID 0x%x", message_id);
694 return -EPROTO;
695 }
696
697 ath10k_dbg(ATH10K_DBG_HTC,
698 "HTC Service %s connect response: status: 0x%x, assigned ep: 0x%x\n",
699 htc_service_name(service_id),
700 resp_msg->status, resp_msg->eid);
701
702 conn_resp->connect_resp_code = resp_msg->status;
703
704 /* check response status */
705 if (resp_msg->status != ATH10K_HTC_CONN_SVC_STATUS_SUCCESS) {
706 ath10k_err("HTC Service %s connect request failed: 0x%x)\n",
707 htc_service_name(service_id),
708 resp_msg->status);
709 return -EPROTO;
710 }
711
712 assigned_eid = (enum ath10k_htc_ep_id)resp_msg->eid;
713 max_msg_size = __le16_to_cpu(resp_msg->max_msg_size);
714
715setup:
716
717 if (assigned_eid >= ATH10K_HTC_EP_COUNT)
718 return -EPROTO;
719
720 if (max_msg_size == 0)
721 return -EPROTO;
722
723 ep = &htc->endpoint[assigned_eid];
724 ep->eid = assigned_eid;
725
726 if (ep->service_id != ATH10K_HTC_SVC_ID_UNUSED)
727 return -EPROTO;
728
729 /* return assigned endpoint to caller */
730 conn_resp->eid = assigned_eid;
731 conn_resp->max_msg_len = __le16_to_cpu(resp_msg->max_msg_size);
732
733 /* setup the endpoint */
734 ep->service_id = conn_req->service_id;
735 ep->max_tx_queue_depth = conn_req->max_send_queue_depth;
736 ep->max_ep_message_len = __le16_to_cpu(resp_msg->max_msg_size);
737 ep->tx_credits = tx_alloc;
738 ep->tx_credit_size = htc->target_credit_size;
739 ep->tx_credits_per_max_message = ep->max_ep_message_len /
740 htc->target_credit_size;
741
742 if (ep->max_ep_message_len % htc->target_credit_size)
743 ep->tx_credits_per_max_message++;
744
745 /* copy all the callbacks */
746 ep->ep_ops = conn_req->ep_ops;
747
748 status = ath10k_hif_map_service_to_pipe(htc->ar,
749 ep->service_id,
750 &ep->ul_pipe_id,
751 &ep->dl_pipe_id,
752 &ep->ul_is_polled,
753 &ep->dl_is_polled);
754 if (status)
755 return status;
756
Kalle Valo42a2efb2013-09-08 17:55:56 +0300757 ath10k_dbg(ATH10K_DBG_BOOT,
758 "boot htc service '%s' ul pipe %d dl pipe %d eid %d ready\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +0300759 htc_service_name(ep->service_id), ep->ul_pipe_id,
760 ep->dl_pipe_id, ep->eid);
761
Kalle Valo42a2efb2013-09-08 17:55:56 +0300762 ath10k_dbg(ATH10K_DBG_BOOT,
763 "boot htc ep %d ul polled %d dl polled %d\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +0300764 ep->eid, ep->ul_is_polled, ep->dl_is_polled);
765
766 if (disable_credit_flow_ctrl && ep->tx_credit_flow_enabled) {
767 ep->tx_credit_flow_enabled = false;
Kalle Valo42a2efb2013-09-08 17:55:56 +0300768 ath10k_dbg(ATH10K_DBG_BOOT,
769 "boot htc service '%s' eid %d TX flow control disabled\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +0300770 htc_service_name(ep->service_id), assigned_eid);
771 }
772
773 return status;
774}
775
776struct sk_buff *ath10k_htc_alloc_skb(int size)
777{
778 struct sk_buff *skb;
779
780 skb = dev_alloc_skb(size + sizeof(struct ath10k_htc_hdr));
781 if (!skb) {
782 ath10k_warn("could not allocate HTC tx skb\n");
783 return NULL;
784 }
785
786 skb_reserve(skb, sizeof(struct ath10k_htc_hdr));
787
788 /* FW/HTC requires 4-byte aligned streams */
789 if (!IS_ALIGNED((unsigned long)skb->data, 4))
790 ath10k_warn("Unaligned HTC tx skb\n");
791
792 return skb;
793}
794
795int ath10k_htc_start(struct ath10k_htc *htc)
796{
797 struct sk_buff *skb;
798 int status = 0;
799 struct ath10k_htc_msg *msg;
800
801 skb = ath10k_htc_build_tx_ctrl_skb(htc->ar);
802 if (!skb)
803 return -ENOMEM;
804
805 skb_put(skb, sizeof(msg->hdr) + sizeof(msg->setup_complete_ext));
806 memset(skb->data, 0, skb->len);
807
808 msg = (struct ath10k_htc_msg *)skb->data;
809 msg->hdr.message_id =
810 __cpu_to_le16(ATH10K_HTC_MSG_SETUP_COMPLETE_EX_ID);
811
812 ath10k_dbg(ATH10K_DBG_HTC, "HTC is using TX credit flow control\n");
813
814 status = ath10k_htc_send(htc, ATH10K_HTC_EP_0, skb);
815 if (status) {
816 kfree_skb(skb);
817 return status;
818 }
819
820 return 0;
821}
822
823/*
824 * stop HTC communications, i.e. stop interrupt reception, and flush all
825 * queued buffers
826 */
827void ath10k_htc_stop(struct ath10k_htc *htc)
828{
Kalle Valo5e3dd152013-06-12 20:52:10 +0300829 spin_lock_bh(&htc->tx_lock);
Michal Kazior08fe9b42013-07-22 14:13:28 +0200830 htc->stopped = true;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300831 spin_unlock_bh(&htc->tx_lock);
832
Kalle Valo5e3dd152013-06-12 20:52:10 +0300833 ath10k_hif_stop(htc->ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300834}
835
836/* registered target arrival callback from the HIF layer */
Michal Kaziorcd003fa2013-07-05 16:15:13 +0300837int ath10k_htc_init(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +0300838{
839 struct ath10k_hif_cb htc_callbacks;
840 struct ath10k_htc_ep *ep = NULL;
Michal Kaziorcd003fa2013-07-05 16:15:13 +0300841 struct ath10k_htc *htc = &ar->htc;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300842
843 spin_lock_init(&htc->tx_lock);
844
Michal Kazior08fe9b42013-07-22 14:13:28 +0200845 htc->stopped = false;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300846 ath10k_htc_reset_endpoint_states(htc);
847
848 /* setup HIF layer callbacks */
849 htc_callbacks.rx_completion = ath10k_htc_rx_completion_handler;
850 htc_callbacks.tx_completion = ath10k_htc_tx_completion_handler;
851 htc->ar = ar;
852
853 /* Get HIF default pipe for HTC message exchange */
854 ep = &htc->endpoint[ATH10K_HTC_EP_0];
855
Michal Kaziore799bbf2013-07-05 16:15:12 +0300856 ath10k_hif_set_callbacks(ar, &htc_callbacks);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300857 ath10k_hif_get_default_pipe(ar, &ep->ul_pipe_id, &ep->dl_pipe_id);
858
859 init_completion(&htc->ctl_resp);
860
Michal Kaziorcd003fa2013-07-05 16:15:13 +0300861 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300862}