1da0f828f458815e60f74b3c2688253d45597377
[cascardo/linux.git] / drivers / net / wireless / ath / ath10k / htc.c
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
26 static 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
36 static void ath10k_htc_control_tx_complete(struct ath10k *ar,
37                                            struct sk_buff *skb)
38 {
39         kfree_skb(skb);
40 }
41
42 static 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
63 static inline void ath10k_htc_restore_tx_skb(struct ath10k_htc *htc,
64                                              struct sk_buff *skb)
65 {
66         ath10k_skb_unmap(htc->ar->dev, skb);
67         skb_pull(skb, sizeof(struct ath10k_htc_hdr));
68 }
69
70 static void ath10k_htc_notify_tx_completion(struct ath10k_htc_ep *ep,
71                                             struct sk_buff *skb)
72 {
73         ath10k_dbg(ATH10K_DBG_HTC, "%s: ep %d skb %p\n", __func__,
74                    ep->eid, skb);
75
76         ath10k_htc_restore_tx_skb(ep->htc, skb);
77
78         if (!ep->ep_ops.ep_tx_complete) {
79                 ath10k_warn("no tx handler for eid %d\n", ep->eid);
80                 dev_kfree_skb_any(skb);
81                 return;
82         }
83
84         ep->ep_ops.ep_tx_complete(ep->htc->ar, skb);
85 }
86
87 /* assumes tx_lock is held */
88 static bool ath10k_htc_ep_need_credit_update(struct ath10k_htc_ep *ep)
89 {
90         if (!ep->tx_credit_flow_enabled)
91                 return false;
92         if (ep->tx_credits >= ep->tx_credits_per_max_message)
93                 return false;
94
95         ath10k_dbg(ATH10K_DBG_HTC, "HTC: endpoint %d needs credit update\n",
96                    ep->eid);
97         return true;
98 }
99
100 static void ath10k_htc_prepare_tx_skb(struct ath10k_htc_ep *ep,
101                                       struct sk_buff *skb)
102 {
103         struct ath10k_htc_hdr *hdr;
104
105         hdr = (struct ath10k_htc_hdr *)skb->data;
106         memset(hdr, 0, sizeof(*hdr));
107
108         hdr->eid = ep->eid;
109         hdr->len = __cpu_to_le16(skb->len - sizeof(*hdr));
110
111         spin_lock_bh(&ep->htc->tx_lock);
112         hdr->seq_no = ep->seq_no++;
113
114         if (ath10k_htc_ep_need_credit_update(ep))
115                 hdr->flags |= ATH10K_HTC_FLAG_NEED_CREDIT_UPDATE;
116
117         spin_unlock_bh(&ep->htc->tx_lock);
118 }
119
120 static int ath10k_htc_issue_skb(struct ath10k_htc *htc,
121                                 struct ath10k_htc_ep *ep,
122                                 struct sk_buff *skb,
123                                 u8 credits)
124 {
125         struct ath10k_skb_cb *skb_cb = ATH10K_SKB_CB(skb);
126         int ret;
127
128         ath10k_dbg(ATH10K_DBG_HTC, "%s: ep %d skb %p\n", __func__,
129                    ep->eid, skb);
130
131         ath10k_htc_prepare_tx_skb(ep, skb);
132
133         ret = ath10k_skb_map(htc->ar->dev, skb);
134         if (ret)
135                 goto err;
136
137         ret = ath10k_hif_send_head(htc->ar,
138                                    ep->ul_pipe_id,
139                                    ep->eid,
140                                    skb->len,
141                                    skb);
142         if (unlikely(ret))
143                 goto err;
144
145         return 0;
146 err:
147         ath10k_warn("HTC issue failed: %d\n", ret);
148
149         spin_lock_bh(&htc->tx_lock);
150         ep->tx_credits += credits;
151         spin_unlock_bh(&htc->tx_lock);
152
153         /* this is the simplest way to handle out-of-resources for non-credit
154          * based endpoints. credit based endpoints can still get -ENOSR, but
155          * this is highly unlikely as credit reservation should prevent that */
156         if (ret == -ENOSR) {
157                 spin_lock_bh(&htc->tx_lock);
158                 __skb_queue_head(&ep->tx_queue, skb);
159                 spin_unlock_bh(&htc->tx_lock);
160
161                 return ret;
162         }
163
164         skb_cb->is_aborted = true;
165         ath10k_htc_notify_tx_completion(ep, skb);
166
167         return ret;
168 }
169
170 static void ath10k_htc_send_work(struct work_struct *work)
171 {
172         struct ath10k_htc_ep *ep = container_of(work,
173                                         struct ath10k_htc_ep, send_work);
174         struct ath10k_htc *htc = ep->htc;
175         struct sk_buff *skb;
176         u8 credits = 0;
177         int ret;
178
179         while (true) {
180                 if (ep->ul_is_polled)
181                         ath10k_htc_send_complete_check(ep, 0);
182
183                 spin_lock_bh(&htc->tx_lock);
184                 skb = __skb_dequeue(&ep->tx_queue);
185
186                 if (ep->tx_credit_flow_enabled) {
187                         credits = DIV_ROUND_UP(skb->len,
188                                                htc->target_credit_size);
189                         if (ep->tx_credits < credits) {
190                                 __skb_queue_head(&ep->tx_queue, skb);
191                                 skb = NULL;
192                         }
193                 }
194                 spin_unlock_bh(&htc->tx_lock);
195
196                 if (!skb)
197                         break;
198
199                 ret = ath10k_htc_issue_skb(htc, ep, skb, credits);
200                 if (ret == -ENOSR)
201                         break;
202         }
203 }
204
205 int ath10k_htc_send(struct ath10k_htc *htc,
206                     enum ath10k_htc_ep_id eid,
207                     struct sk_buff *skb)
208 {
209         struct ath10k_htc_ep *ep = &htc->endpoint[eid];
210
211         if (htc->ar->state == ATH10K_STATE_WEDGED)
212                 return -ECOMM;
213
214         if (eid >= ATH10K_HTC_EP_COUNT) {
215                 ath10k_warn("Invalid endpoint id: %d\n", eid);
216                 return -ENOENT;
217         }
218
219         spin_lock_bh(&htc->tx_lock);
220         if (htc->stopped) {
221                 spin_unlock_bh(&htc->tx_lock);
222                 return -ESHUTDOWN;
223         }
224
225         __skb_queue_tail(&ep->tx_queue, skb);
226         skb_push(skb, sizeof(struct ath10k_htc_hdr));
227         spin_unlock_bh(&htc->tx_lock);
228
229         queue_work(htc->ar->workqueue, &ep->send_work);
230         return 0;
231 }
232
233 static int ath10k_htc_tx_completion_handler(struct ath10k *ar,
234                                             struct sk_buff *skb,
235                                             unsigned int eid)
236 {
237         struct ath10k_htc *htc = &ar->htc;
238         struct ath10k_htc_ep *ep = &htc->endpoint[eid];
239
240         ath10k_htc_notify_tx_completion(ep, skb);
241         /* the skb now belongs to the completion handler */
242
243         /* note: when using TX credit flow, the re-checking of queues happens
244          * when credits flow back from the target.  in the non-TX credit case,
245          * we recheck after the packet completes */
246         spin_lock_bh(&htc->tx_lock);
247         if (!ep->tx_credit_flow_enabled && !htc->stopped)
248                 queue_work(ar->workqueue, &ep->send_work);
249         spin_unlock_bh(&htc->tx_lock);
250
251         return 0;
252 }
253
254 /* flush endpoint TX queue */
255 static void ath10k_htc_flush_endpoint_tx(struct ath10k_htc *htc,
256                                          struct ath10k_htc_ep *ep)
257 {
258         struct sk_buff *skb;
259         struct ath10k_skb_cb *skb_cb;
260
261         spin_lock_bh(&htc->tx_lock);
262         for (;;) {
263                 skb = __skb_dequeue(&ep->tx_queue);
264                 if (!skb)
265                         break;
266
267                 skb_cb = ATH10K_SKB_CB(skb);
268                 skb_cb->is_aborted = true;
269                 ath10k_htc_notify_tx_completion(ep, skb);
270         }
271         spin_unlock_bh(&htc->tx_lock);
272
273         cancel_work_sync(&ep->send_work);
274 }
275
276 /***********/
277 /* Receive */
278 /***********/
279
280 static void
281 ath10k_htc_process_credit_report(struct ath10k_htc *htc,
282                                  const struct ath10k_htc_credit_report *report,
283                                  int len,
284                                  enum ath10k_htc_ep_id eid)
285 {
286         struct ath10k_htc_ep *ep;
287         int i, n_reports;
288
289         if (len % sizeof(*report))
290                 ath10k_warn("Uneven credit report len %d", len);
291
292         n_reports = len / sizeof(*report);
293
294         spin_lock_bh(&htc->tx_lock);
295         for (i = 0; i < n_reports; i++, report++) {
296                 if (report->eid >= ATH10K_HTC_EP_COUNT)
297                         break;
298
299                 ath10k_dbg(ATH10K_DBG_HTC, "ep %d got %d credits\n",
300                            report->eid, report->credits);
301
302                 ep = &htc->endpoint[report->eid];
303                 ep->tx_credits += report->credits;
304
305                 if (ep->tx_credits && !skb_queue_empty(&ep->tx_queue))
306                         queue_work(htc->ar->workqueue, &ep->send_work);
307         }
308         spin_unlock_bh(&htc->tx_lock);
309 }
310
311 static int ath10k_htc_process_trailer(struct ath10k_htc *htc,
312                                       u8 *buffer,
313                                       int length,
314                                       enum ath10k_htc_ep_id src_eid)
315 {
316         int status = 0;
317         struct ath10k_htc_record *record;
318         u8 *orig_buffer;
319         int orig_length;
320         size_t len;
321
322         orig_buffer = buffer;
323         orig_length = length;
324
325         while (length > 0) {
326                 record = (struct ath10k_htc_record *)buffer;
327
328                 if (length < sizeof(record->hdr)) {
329                         status = -EINVAL;
330                         break;
331                 }
332
333                 if (record->hdr.len > length) {
334                         /* no room left in buffer for record */
335                         ath10k_warn("Invalid record length: %d\n",
336                                     record->hdr.len);
337                         status = -EINVAL;
338                         break;
339                 }
340
341                 switch (record->hdr.id) {
342                 case ATH10K_HTC_RECORD_CREDITS:
343                         len = sizeof(struct ath10k_htc_credit_report);
344                         if (record->hdr.len < len) {
345                                 ath10k_warn("Credit report too long\n");
346                                 status = -EINVAL;
347                                 break;
348                         }
349                         ath10k_htc_process_credit_report(htc,
350                                                          record->credit_report,
351                                                          record->hdr.len,
352                                                          src_eid);
353                         break;
354                 default:
355                         ath10k_warn("Unhandled record: id:%d length:%d\n",
356                                     record->hdr.id, record->hdr.len);
357                         break;
358                 }
359
360                 if (status)
361                         break;
362
363                 /* multiple records may be present in a trailer */
364                 buffer += sizeof(record->hdr) + record->hdr.len;
365                 length -= sizeof(record->hdr) + record->hdr.len;
366         }
367
368         if (status)
369                 ath10k_dbg_dump(ATH10K_DBG_HTC, "htc rx bad trailer", "",
370                                 orig_buffer, orig_length);
371
372         return status;
373 }
374
375 static int ath10k_htc_rx_completion_handler(struct ath10k *ar,
376                                             struct sk_buff *skb,
377                                             u8 pipe_id)
378 {
379         int status = 0;
380         struct ath10k_htc *htc = &ar->htc;
381         struct ath10k_htc_hdr *hdr;
382         struct ath10k_htc_ep *ep;
383         u16 payload_len;
384         u32 trailer_len = 0;
385         size_t min_len;
386         u8 eid;
387         bool trailer_present;
388
389         hdr = (struct ath10k_htc_hdr *)skb->data;
390         skb_pull(skb, sizeof(*hdr));
391
392         eid = hdr->eid;
393
394         if (eid >= ATH10K_HTC_EP_COUNT) {
395                 ath10k_warn("HTC Rx: invalid eid %d\n", eid);
396                 ath10k_dbg_dump(ATH10K_DBG_HTC, "htc bad header", "",
397                                 hdr, sizeof(*hdr));
398                 status = -EINVAL;
399                 goto out;
400         }
401
402         ep = &htc->endpoint[eid];
403
404         /*
405          * If this endpoint that received a message from the target has
406          * a to-target HIF pipe whose send completions are polled rather
407          * than interrupt-driven, this is a good point to ask HIF to check
408          * whether it has any completed sends to handle.
409          */
410         if (ep->ul_is_polled)
411                 ath10k_htc_send_complete_check(ep, 1);
412
413         payload_len = __le16_to_cpu(hdr->len);
414
415         if (payload_len + sizeof(*hdr) > ATH10K_HTC_MAX_LEN) {
416                 ath10k_warn("HTC rx frame too long, len: %zu\n",
417                             payload_len + sizeof(*hdr));
418                 ath10k_dbg_dump(ATH10K_DBG_HTC, "htc bad rx pkt len", "",
419                                 hdr, sizeof(*hdr));
420                 status = -EINVAL;
421                 goto out;
422         }
423
424         if (skb->len < payload_len) {
425                 ath10k_dbg(ATH10K_DBG_HTC,
426                            "HTC Rx: insufficient length, got %d, expected %d\n",
427                            skb->len, payload_len);
428                 ath10k_dbg_dump(ATH10K_DBG_HTC, "htc bad rx pkt len",
429                                 "", hdr, sizeof(*hdr));
430                 status = -EINVAL;
431                 goto out;
432         }
433
434         /* get flags to check for trailer */
435         trailer_present = hdr->flags & ATH10K_HTC_FLAG_TRAILER_PRESENT;
436         if (trailer_present) {
437                 u8 *trailer;
438
439                 trailer_len = hdr->trailer_len;
440                 min_len = sizeof(struct ath10k_ath10k_htc_record_hdr);
441
442                 if ((trailer_len < min_len) ||
443                     (trailer_len > payload_len)) {
444                         ath10k_warn("Invalid trailer length: %d\n",
445                                     trailer_len);
446                         status = -EPROTO;
447                         goto out;
448                 }
449
450                 trailer = (u8 *)hdr;
451                 trailer += sizeof(*hdr);
452                 trailer += payload_len;
453                 trailer -= trailer_len;
454                 status = ath10k_htc_process_trailer(htc, trailer,
455                                                     trailer_len, hdr->eid);
456                 if (status)
457                         goto out;
458
459                 skb_trim(skb, skb->len - trailer_len);
460         }
461
462         if (((int)payload_len - (int)trailer_len) <= 0)
463                 /* zero length packet with trailer data, just drop these */
464                 goto out;
465
466         if (eid == ATH10K_HTC_EP_0) {
467                 struct ath10k_htc_msg *msg = (struct ath10k_htc_msg *)skb->data;
468
469                 switch (__le16_to_cpu(msg->hdr.message_id)) {
470                 default:
471                         /* handle HTC control message */
472                         if (completion_done(&htc->ctl_resp)) {
473                                 /*
474                                  * this is a fatal error, target should not be
475                                  * sending unsolicited messages on the ep 0
476                                  */
477                                 ath10k_warn("HTC rx ctrl still processing\n");
478                                 status = -EINVAL;
479                                 complete(&htc->ctl_resp);
480                                 goto out;
481                         }
482
483                         htc->control_resp_len =
484                                 min_t(int, skb->len,
485                                       ATH10K_HTC_MAX_CTRL_MSG_LEN);
486
487                         memcpy(htc->control_resp_buffer, skb->data,
488                                htc->control_resp_len);
489
490                         complete(&htc->ctl_resp);
491                         break;
492                 case ATH10K_HTC_MSG_SEND_SUSPEND_COMPLETE:
493                         htc->htc_ops.target_send_suspend_complete(ar);
494                 }
495                 goto out;
496         }
497
498         ath10k_dbg(ATH10K_DBG_HTC, "htc rx completion ep %d skb %p\n",
499                    eid, skb);
500         ep->ep_ops.ep_rx_complete(ar, skb);
501
502         /* skb is now owned by the rx completion handler */
503         skb = NULL;
504 out:
505         kfree_skb(skb);
506
507         return status;
508 }
509
510 static void ath10k_htc_control_rx_complete(struct ath10k *ar,
511                                            struct sk_buff *skb)
512 {
513         /* This is unexpected. FW is not supposed to send regular rx on this
514          * endpoint. */
515         ath10k_warn("unexpected htc rx\n");
516         kfree_skb(skb);
517 }
518
519 /***************/
520 /* Init/Deinit */
521 /***************/
522
523 static const char *htc_service_name(enum ath10k_htc_svc_id id)
524 {
525         switch (id) {
526         case ATH10K_HTC_SVC_ID_RESERVED:
527                 return "Reserved";
528         case ATH10K_HTC_SVC_ID_RSVD_CTRL:
529                 return "Control";
530         case ATH10K_HTC_SVC_ID_WMI_CONTROL:
531                 return "WMI";
532         case ATH10K_HTC_SVC_ID_WMI_DATA_BE:
533                 return "DATA BE";
534         case ATH10K_HTC_SVC_ID_WMI_DATA_BK:
535                 return "DATA BK";
536         case ATH10K_HTC_SVC_ID_WMI_DATA_VI:
537                 return "DATA VI";
538         case ATH10K_HTC_SVC_ID_WMI_DATA_VO:
539                 return "DATA VO";
540         case ATH10K_HTC_SVC_ID_NMI_CONTROL:
541                 return "NMI Control";
542         case ATH10K_HTC_SVC_ID_NMI_DATA:
543                 return "NMI Data";
544         case ATH10K_HTC_SVC_ID_HTT_DATA_MSG:
545                 return "HTT Data";
546         case ATH10K_HTC_SVC_ID_TEST_RAW_STREAMS:
547                 return "RAW";
548         }
549
550         return "Unknown";
551 }
552
553 static void ath10k_htc_reset_endpoint_states(struct ath10k_htc *htc)
554 {
555         struct ath10k_htc_ep *ep;
556         int i;
557
558         for (i = ATH10K_HTC_EP_0; i < ATH10K_HTC_EP_COUNT; i++) {
559                 ep = &htc->endpoint[i];
560                 ep->service_id = ATH10K_HTC_SVC_ID_UNUSED;
561                 ep->max_ep_message_len = 0;
562                 ep->max_tx_queue_depth = 0;
563                 ep->eid = i;
564                 skb_queue_head_init(&ep->tx_queue);
565                 ep->htc = htc;
566                 ep->tx_credit_flow_enabled = true;
567                 INIT_WORK(&ep->send_work, ath10k_htc_send_work);
568         }
569 }
570
571 static void ath10k_htc_setup_target_buffer_assignments(struct ath10k_htc *htc)
572 {
573         struct ath10k_htc_svc_tx_credits *entry;
574
575         entry = &htc->service_tx_alloc[0];
576
577         /*
578          * for PCIE allocate all credists/HTC buffers to WMI.
579          * no buffers are used/required for data. data always
580          * remains on host.
581          */
582         entry++;
583         entry->service_id = ATH10K_HTC_SVC_ID_WMI_CONTROL;
584         entry->credit_allocation = htc->total_transmit_credits;
585 }
586
587 static u8 ath10k_htc_get_credit_allocation(struct ath10k_htc *htc,
588                                            u16 service_id)
589 {
590         u8 allocation = 0;
591         int i;
592
593         for (i = 0; i < ATH10K_HTC_EP_COUNT; i++) {
594                 if (htc->service_tx_alloc[i].service_id == service_id)
595                         allocation =
596                             htc->service_tx_alloc[i].credit_allocation;
597         }
598
599         return allocation;
600 }
601
602 int ath10k_htc_wait_target(struct ath10k_htc *htc)
603 {
604         int status = 0;
605         struct ath10k_htc_svc_conn_req conn_req;
606         struct ath10k_htc_svc_conn_resp conn_resp;
607         struct ath10k_htc_msg *msg;
608         u16 message_id;
609         u16 credit_count;
610         u16 credit_size;
611
612         INIT_COMPLETION(htc->ctl_resp);
613
614         status = ath10k_hif_start(htc->ar);
615         if (status) {
616                 ath10k_err("could not start HIF (%d)\n", status);
617                 goto err_start;
618         }
619
620         status = wait_for_completion_timeout(&htc->ctl_resp,
621                                              ATH10K_HTC_WAIT_TIMEOUT_HZ);
622         if (status <= 0) {
623                 if (status == 0)
624                         status = -ETIMEDOUT;
625
626                 ath10k_err("ctl_resp never came in (%d)\n", status);
627                 goto err_target;
628         }
629
630         if (htc->control_resp_len < sizeof(msg->hdr) + sizeof(msg->ready)) {
631                 ath10k_err("Invalid HTC ready msg len:%d\n",
632                            htc->control_resp_len);
633
634                 status = -ECOMM;
635                 goto err_target;
636         }
637
638         msg = (struct ath10k_htc_msg *)htc->control_resp_buffer;
639         message_id   = __le16_to_cpu(msg->hdr.message_id);
640         credit_count = __le16_to_cpu(msg->ready.credit_count);
641         credit_size  = __le16_to_cpu(msg->ready.credit_size);
642
643         if (message_id != ATH10K_HTC_MSG_READY_ID) {
644                 ath10k_err("Invalid HTC ready msg: 0x%x\n", message_id);
645                 status = -ECOMM;
646                 goto err_target;
647         }
648
649         htc->total_transmit_credits = credit_count;
650         htc->target_credit_size = credit_size;
651
652         ath10k_dbg(ATH10K_DBG_HTC,
653                    "Target ready! transmit resources: %d size:%d\n",
654                    htc->total_transmit_credits,
655                    htc->target_credit_size);
656
657         if ((htc->total_transmit_credits == 0) ||
658             (htc->target_credit_size == 0)) {
659                 status = -ECOMM;
660                 ath10k_err("Invalid credit size received\n");
661                 goto err_target;
662         }
663
664         ath10k_htc_setup_target_buffer_assignments(htc);
665
666         /* setup our pseudo HTC control endpoint connection */
667         memset(&conn_req, 0, sizeof(conn_req));
668         memset(&conn_resp, 0, sizeof(conn_resp));
669         conn_req.ep_ops.ep_tx_complete = ath10k_htc_control_tx_complete;
670         conn_req.ep_ops.ep_rx_complete = ath10k_htc_control_rx_complete;
671         conn_req.max_send_queue_depth = ATH10K_NUM_CONTROL_TX_BUFFERS;
672         conn_req.service_id = ATH10K_HTC_SVC_ID_RSVD_CTRL;
673
674         /* connect fake service */
675         status = ath10k_htc_connect_service(htc, &conn_req, &conn_resp);
676         if (status) {
677                 ath10k_err("could not connect to htc service (%d)\n", status);
678                 goto err_target;
679         }
680
681         return 0;
682 err_target:
683         ath10k_hif_stop(htc->ar);
684 err_start:
685         return status;
686 }
687
688 int ath10k_htc_connect_service(struct ath10k_htc *htc,
689                                struct ath10k_htc_svc_conn_req *conn_req,
690                                struct ath10k_htc_svc_conn_resp *conn_resp)
691 {
692         struct ath10k_htc_msg *msg;
693         struct ath10k_htc_conn_svc *req_msg;
694         struct ath10k_htc_conn_svc_response resp_msg_dummy;
695         struct ath10k_htc_conn_svc_response *resp_msg = &resp_msg_dummy;
696         enum ath10k_htc_ep_id assigned_eid = ATH10K_HTC_EP_COUNT;
697         struct ath10k_htc_ep *ep;
698         struct sk_buff *skb;
699         unsigned int max_msg_size = 0;
700         int length, status;
701         bool disable_credit_flow_ctrl = false;
702         u16 message_id, service_id, flags = 0;
703         u8 tx_alloc = 0;
704
705         /* special case for HTC pseudo control service */
706         if (conn_req->service_id == ATH10K_HTC_SVC_ID_RSVD_CTRL) {
707                 disable_credit_flow_ctrl = true;
708                 assigned_eid = ATH10K_HTC_EP_0;
709                 max_msg_size = ATH10K_HTC_MAX_CTRL_MSG_LEN;
710                 memset(&resp_msg_dummy, 0, sizeof(resp_msg_dummy));
711                 goto setup;
712         }
713
714         tx_alloc = ath10k_htc_get_credit_allocation(htc,
715                                                     conn_req->service_id);
716         if (!tx_alloc)
717                 ath10k_dbg(ATH10K_DBG_BOOT,
718                            "boot htc service %s does not allocate target credits\n",
719                            htc_service_name(conn_req->service_id));
720
721         skb = ath10k_htc_build_tx_ctrl_skb(htc->ar);
722         if (!skb) {
723                 ath10k_err("Failed to allocate HTC packet\n");
724                 return -ENOMEM;
725         }
726
727         length = sizeof(msg->hdr) + sizeof(msg->connect_service);
728         skb_put(skb, length);
729         memset(skb->data, 0, length);
730
731         msg = (struct ath10k_htc_msg *)skb->data;
732         msg->hdr.message_id =
733                 __cpu_to_le16(ATH10K_HTC_MSG_CONNECT_SERVICE_ID);
734
735         flags |= SM(tx_alloc, ATH10K_HTC_CONN_FLAGS_RECV_ALLOC);
736
737         /* Only enable credit flow control for WMI ctrl service */
738         if (conn_req->service_id != ATH10K_HTC_SVC_ID_WMI_CONTROL) {
739                 flags |= ATH10K_HTC_CONN_FLAGS_DISABLE_CREDIT_FLOW_CTRL;
740                 disable_credit_flow_ctrl = true;
741         }
742
743         req_msg = &msg->connect_service;
744         req_msg->flags = __cpu_to_le16(flags);
745         req_msg->service_id = __cpu_to_le16(conn_req->service_id);
746
747         INIT_COMPLETION(htc->ctl_resp);
748
749         status = ath10k_htc_send(htc, ATH10K_HTC_EP_0, skb);
750         if (status) {
751                 kfree_skb(skb);
752                 return status;
753         }
754
755         /* wait for response */
756         status = wait_for_completion_timeout(&htc->ctl_resp,
757                                              ATH10K_HTC_CONN_SVC_TIMEOUT_HZ);
758         if (status <= 0) {
759                 if (status == 0)
760                         status = -ETIMEDOUT;
761                 ath10k_err("Service connect timeout: %d\n", status);
762                 return status;
763         }
764
765         /* we controlled the buffer creation, it's aligned */
766         msg = (struct ath10k_htc_msg *)htc->control_resp_buffer;
767         resp_msg = &msg->connect_service_response;
768         message_id = __le16_to_cpu(msg->hdr.message_id);
769         service_id = __le16_to_cpu(resp_msg->service_id);
770
771         if ((message_id != ATH10K_HTC_MSG_CONNECT_SERVICE_RESP_ID) ||
772             (htc->control_resp_len < sizeof(msg->hdr) +
773              sizeof(msg->connect_service_response))) {
774                 ath10k_err("Invalid resp message ID 0x%x", message_id);
775                 return -EPROTO;
776         }
777
778         ath10k_dbg(ATH10K_DBG_HTC,
779                    "HTC Service %s connect response: status: 0x%x, assigned ep: 0x%x\n",
780                    htc_service_name(service_id),
781                    resp_msg->status, resp_msg->eid);
782
783         conn_resp->connect_resp_code = resp_msg->status;
784
785         /* check response status */
786         if (resp_msg->status != ATH10K_HTC_CONN_SVC_STATUS_SUCCESS) {
787                 ath10k_err("HTC Service %s connect request failed: 0x%x)\n",
788                            htc_service_name(service_id),
789                            resp_msg->status);
790                 return -EPROTO;
791         }
792
793         assigned_eid = (enum ath10k_htc_ep_id)resp_msg->eid;
794         max_msg_size = __le16_to_cpu(resp_msg->max_msg_size);
795
796 setup:
797
798         if (assigned_eid >= ATH10K_HTC_EP_COUNT)
799                 return -EPROTO;
800
801         if (max_msg_size == 0)
802                 return -EPROTO;
803
804         ep = &htc->endpoint[assigned_eid];
805         ep->eid = assigned_eid;
806
807         if (ep->service_id != ATH10K_HTC_SVC_ID_UNUSED)
808                 return -EPROTO;
809
810         /* return assigned endpoint to caller */
811         conn_resp->eid = assigned_eid;
812         conn_resp->max_msg_len = __le16_to_cpu(resp_msg->max_msg_size);
813
814         /* setup the endpoint */
815         ep->service_id = conn_req->service_id;
816         ep->max_tx_queue_depth = conn_req->max_send_queue_depth;
817         ep->max_ep_message_len = __le16_to_cpu(resp_msg->max_msg_size);
818         ep->tx_credits = tx_alloc;
819         ep->tx_credit_size = htc->target_credit_size;
820         ep->tx_credits_per_max_message = ep->max_ep_message_len /
821                                          htc->target_credit_size;
822
823         if (ep->max_ep_message_len % htc->target_credit_size)
824                 ep->tx_credits_per_max_message++;
825
826         /* copy all the callbacks */
827         ep->ep_ops = conn_req->ep_ops;
828
829         status = ath10k_hif_map_service_to_pipe(htc->ar,
830                                                 ep->service_id,
831                                                 &ep->ul_pipe_id,
832                                                 &ep->dl_pipe_id,
833                                                 &ep->ul_is_polled,
834                                                 &ep->dl_is_polled);
835         if (status)
836                 return status;
837
838         ath10k_dbg(ATH10K_DBG_BOOT,
839                    "boot htc service '%s' ul pipe %d dl pipe %d eid %d ready\n",
840                    htc_service_name(ep->service_id), ep->ul_pipe_id,
841                    ep->dl_pipe_id, ep->eid);
842
843         ath10k_dbg(ATH10K_DBG_BOOT,
844                    "boot htc ep %d ul polled %d dl polled %d\n",
845                    ep->eid, ep->ul_is_polled, ep->dl_is_polled);
846
847         if (disable_credit_flow_ctrl && ep->tx_credit_flow_enabled) {
848                 ep->tx_credit_flow_enabled = false;
849                 ath10k_dbg(ATH10K_DBG_BOOT,
850                            "boot htc service '%s' eid %d TX flow control disabled\n",
851                            htc_service_name(ep->service_id), assigned_eid);
852         }
853
854         return status;
855 }
856
857 struct sk_buff *ath10k_htc_alloc_skb(int size)
858 {
859         struct sk_buff *skb;
860
861         skb = dev_alloc_skb(size + sizeof(struct ath10k_htc_hdr));
862         if (!skb) {
863                 ath10k_warn("could not allocate HTC tx skb\n");
864                 return NULL;
865         }
866
867         skb_reserve(skb, sizeof(struct ath10k_htc_hdr));
868
869         /* FW/HTC requires 4-byte aligned streams */
870         if (!IS_ALIGNED((unsigned long)skb->data, 4))
871                 ath10k_warn("Unaligned HTC tx skb\n");
872
873         return skb;
874 }
875
876 int ath10k_htc_start(struct ath10k_htc *htc)
877 {
878         struct sk_buff *skb;
879         int status = 0;
880         struct ath10k_htc_msg *msg;
881
882         skb = ath10k_htc_build_tx_ctrl_skb(htc->ar);
883         if (!skb)
884                 return -ENOMEM;
885
886         skb_put(skb, sizeof(msg->hdr) + sizeof(msg->setup_complete_ext));
887         memset(skb->data, 0, skb->len);
888
889         msg = (struct ath10k_htc_msg *)skb->data;
890         msg->hdr.message_id =
891                 __cpu_to_le16(ATH10K_HTC_MSG_SETUP_COMPLETE_EX_ID);
892
893         ath10k_dbg(ATH10K_DBG_HTC, "HTC is using TX credit flow control\n");
894
895         status = ath10k_htc_send(htc, ATH10K_HTC_EP_0, skb);
896         if (status) {
897                 kfree_skb(skb);
898                 return status;
899         }
900
901         return 0;
902 }
903
904 /*
905  * stop HTC communications, i.e. stop interrupt reception, and flush all
906  * queued buffers
907  */
908 void ath10k_htc_stop(struct ath10k_htc *htc)
909 {
910         int i;
911         struct ath10k_htc_ep *ep;
912
913         spin_lock_bh(&htc->tx_lock);
914         htc->stopped = true;
915         spin_unlock_bh(&htc->tx_lock);
916
917         for (i = ATH10K_HTC_EP_0; i < ATH10K_HTC_EP_COUNT; i++) {
918                 ep = &htc->endpoint[i];
919                 ath10k_htc_flush_endpoint_tx(htc, ep);
920         }
921
922         ath10k_hif_stop(htc->ar);
923 }
924
925 /* registered target arrival callback from the HIF layer */
926 int ath10k_htc_init(struct ath10k *ar)
927 {
928         struct ath10k_hif_cb htc_callbacks;
929         struct ath10k_htc_ep *ep = NULL;
930         struct ath10k_htc *htc = &ar->htc;
931
932         spin_lock_init(&htc->tx_lock);
933
934         htc->stopped = false;
935         ath10k_htc_reset_endpoint_states(htc);
936
937         /* setup HIF layer callbacks */
938         htc_callbacks.rx_completion = ath10k_htc_rx_completion_handler;
939         htc_callbacks.tx_completion = ath10k_htc_tx_completion_handler;
940         htc->ar = ar;
941
942         /* Get HIF default pipe for HTC message exchange */
943         ep = &htc->endpoint[ATH10K_HTC_EP_0];
944
945         ath10k_hif_set_callbacks(ar, &htc_callbacks);
946         ath10k_hif_get_default_pipe(ar, &ep->ul_pipe_id, &ep->dl_pipe_id);
947
948         init_completion(&htc->ctl_resp);
949
950         return 0;
951 }