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