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