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