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