1 // SPDX-License-Identifier: BSD-3-Clause-Clear
2 /*
3 * Copyright (c) 2018-2021 The Linux Foundation. All rights reserved.
4 * Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries.
5 */
6 #include <linux/skbuff.h>
7 #include <linux/ctype.h>
8
9 #include "debug.h"
10 #include "hif.h"
11
ath12k_htc_alloc_skb(struct ath12k_base * ab,int size)12 struct sk_buff *ath12k_htc_alloc_skb(struct ath12k_base *ab, int size)
13 {
14 struct sk_buff *skb;
15
16 skb = dev_alloc_skb(size + sizeof(struct ath12k_htc_hdr));
17 if (!skb)
18 return NULL;
19
20 skb_reserve(skb, sizeof(struct ath12k_htc_hdr));
21
22 /* FW/HTC requires 4-byte aligned streams */
23 if (!IS_ALIGNED((unsigned long)skb->data, 4))
24 ath12k_warn(ab, "Unaligned HTC tx skb\n");
25
26 return skb;
27 }
28
ath12k_htc_control_tx_complete(struct ath12k_base * ab,struct sk_buff * skb)29 static void ath12k_htc_control_tx_complete(struct ath12k_base *ab,
30 struct sk_buff *skb)
31 {
32 kfree_skb(skb);
33 }
34
ath12k_htc_build_tx_ctrl_skb(void)35 static struct sk_buff *ath12k_htc_build_tx_ctrl_skb(void)
36 {
37 struct sk_buff *skb;
38 struct ath12k_skb_cb *skb_cb;
39
40 skb = dev_alloc_skb(ATH12K_HTC_CONTROL_BUFFER_SIZE);
41 if (!skb)
42 return NULL;
43
44 skb_reserve(skb, sizeof(struct ath12k_htc_hdr));
45 WARN_ON_ONCE(!IS_ALIGNED((unsigned long)skb->data, 4));
46
47 skb_cb = ATH12K_SKB_CB(skb);
48 memset(skb_cb, 0, sizeof(*skb_cb));
49
50 return skb;
51 }
52
ath12k_htc_prepare_tx_skb(struct ath12k_htc_ep * ep,struct sk_buff * skb)53 static void ath12k_htc_prepare_tx_skb(struct ath12k_htc_ep *ep,
54 struct sk_buff *skb)
55 {
56 struct ath12k_htc_hdr *hdr;
57
58 hdr = (struct ath12k_htc_hdr *)skb->data;
59
60 memset(hdr, 0, sizeof(*hdr));
61 hdr->htc_info = le32_encode_bits(ep->eid, HTC_HDR_ENDPOINTID) |
62 le32_encode_bits((skb->len - sizeof(*hdr)),
63 HTC_HDR_PAYLOADLEN);
64
65 if (ep->tx_credit_flow_enabled)
66 hdr->htc_info |= le32_encode_bits(ATH12K_HTC_FLAG_NEED_CREDIT_UPDATE,
67 HTC_HDR_FLAGS);
68
69 spin_lock_bh(&ep->htc->tx_lock);
70 hdr->ctrl_info = le32_encode_bits(ep->seq_no++, HTC_HDR_CONTROLBYTES1);
71 spin_unlock_bh(&ep->htc->tx_lock);
72 }
73
ath12k_htc_send(struct ath12k_htc * htc,enum ath12k_htc_ep_id eid,struct sk_buff * skb)74 int ath12k_htc_send(struct ath12k_htc *htc,
75 enum ath12k_htc_ep_id eid,
76 struct sk_buff *skb)
77 {
78 struct ath12k_htc_ep *ep = &htc->endpoint[eid];
79 struct ath12k_skb_cb *skb_cb = ATH12K_SKB_CB(skb);
80 struct device *dev = htc->ab->dev;
81 struct ath12k_base *ab = htc->ab;
82 int credits = 0;
83 int ret;
84
85 if (eid >= ATH12K_HTC_EP_COUNT) {
86 ath12k_warn(ab, "Invalid endpoint id: %d\n", eid);
87 return -ENOENT;
88 }
89
90 skb_push(skb, sizeof(struct ath12k_htc_hdr));
91
92 if (ep->tx_credit_flow_enabled) {
93 credits = DIV_ROUND_UP(skb->len, htc->target_credit_size);
94 spin_lock_bh(&htc->tx_lock);
95 if (ep->tx_credits < credits) {
96 ath12k_dbg(ab, ATH12K_DBG_HTC,
97 "htc insufficient credits ep %d required %d available %d\n",
98 eid, credits, ep->tx_credits);
99 spin_unlock_bh(&htc->tx_lock);
100 ret = -EAGAIN;
101 goto err_pull;
102 }
103 ep->tx_credits -= credits;
104 ath12k_dbg(ab, ATH12K_DBG_HTC,
105 "htc ep %d consumed %d credits (total %d)\n",
106 eid, credits, ep->tx_credits);
107 spin_unlock_bh(&htc->tx_lock);
108 }
109
110 ath12k_htc_prepare_tx_skb(ep, skb);
111
112 skb_cb->paddr = dma_map_single(dev, skb->data, skb->len, DMA_TO_DEVICE);
113 ret = dma_mapping_error(dev, skb_cb->paddr);
114 if (ret) {
115 ret = -EIO;
116 goto err_credits;
117 }
118
119 ret = ath12k_ce_send(htc->ab, skb, ep->ul_pipe_id, ep->eid);
120 if (ret)
121 goto err_unmap;
122
123 return 0;
124
125 err_unmap:
126 dma_unmap_single(dev, skb_cb->paddr, skb->len, DMA_TO_DEVICE);
127 err_credits:
128 if (ep->tx_credit_flow_enabled) {
129 spin_lock_bh(&htc->tx_lock);
130 ep->tx_credits += credits;
131 ath12k_dbg(ab, ATH12K_DBG_HTC,
132 "htc ep %d reverted %d credits back (total %d)\n",
133 eid, credits, ep->tx_credits);
134 spin_unlock_bh(&htc->tx_lock);
135
136 if (ep->ep_ops.ep_tx_credits)
137 ep->ep_ops.ep_tx_credits(htc->ab);
138 }
139 err_pull:
140 skb_pull(skb, sizeof(struct ath12k_htc_hdr));
141 return ret;
142 }
143
144 static void
ath12k_htc_process_credit_report(struct ath12k_htc * htc,const struct ath12k_htc_credit_report * report,int len,enum ath12k_htc_ep_id eid)145 ath12k_htc_process_credit_report(struct ath12k_htc *htc,
146 const struct ath12k_htc_credit_report *report,
147 int len,
148 enum ath12k_htc_ep_id eid)
149 {
150 struct ath12k_base *ab = htc->ab;
151 struct ath12k_htc_ep *ep;
152 int i, n_reports;
153
154 if (len % sizeof(*report))
155 ath12k_warn(ab, "Uneven credit report len %d", len);
156
157 n_reports = len / sizeof(*report);
158
159 spin_lock_bh(&htc->tx_lock);
160 for (i = 0; i < n_reports; i++, report++) {
161 if (report->eid >= ATH12K_HTC_EP_COUNT)
162 break;
163
164 ep = &htc->endpoint[report->eid];
165 ep->tx_credits += report->credits;
166
167 ath12k_dbg(ab, ATH12K_DBG_HTC, "htc ep %d got %d credits (total %d)\n",
168 report->eid, report->credits, ep->tx_credits);
169
170 if (ep->ep_ops.ep_tx_credits) {
171 spin_unlock_bh(&htc->tx_lock);
172 ep->ep_ops.ep_tx_credits(htc->ab);
173 spin_lock_bh(&htc->tx_lock);
174 }
175 }
176 spin_unlock_bh(&htc->tx_lock);
177 }
178
ath12k_htc_process_trailer(struct ath12k_htc * htc,u8 * buffer,int length,enum ath12k_htc_ep_id src_eid)179 static int ath12k_htc_process_trailer(struct ath12k_htc *htc,
180 u8 *buffer,
181 int length,
182 enum ath12k_htc_ep_id src_eid)
183 {
184 struct ath12k_base *ab = htc->ab;
185 int status = 0;
186 struct ath12k_htc_record *record;
187 size_t len;
188
189 while (length > 0) {
190 record = (struct ath12k_htc_record *)buffer;
191
192 if (length < sizeof(record->hdr)) {
193 status = -EINVAL;
194 break;
195 }
196
197 if (record->hdr.len > length) {
198 /* no room left in buffer for record */
199 ath12k_warn(ab, "Invalid record length: %d\n",
200 record->hdr.len);
201 status = -EINVAL;
202 break;
203 }
204
205 switch (record->hdr.id) {
206 case ATH12K_HTC_RECORD_CREDITS:
207 len = sizeof(struct ath12k_htc_credit_report);
208 if (record->hdr.len < len) {
209 ath12k_warn(ab, "Credit report too long\n");
210 status = -EINVAL;
211 break;
212 }
213 ath12k_htc_process_credit_report(htc,
214 record->credit_report,
215 record->hdr.len,
216 src_eid);
217 break;
218 default:
219 ath12k_warn(ab, "Unhandled record: id:%d length:%d\n",
220 record->hdr.id, record->hdr.len);
221 break;
222 }
223
224 if (status)
225 break;
226
227 /* multiple records may be present in a trailer */
228 buffer += sizeof(record->hdr) + record->hdr.len;
229 length -= sizeof(record->hdr) + record->hdr.len;
230 }
231
232 return status;
233 }
234
ath12k_htc_suspend_complete(struct ath12k_base * ab,bool ack)235 static void ath12k_htc_suspend_complete(struct ath12k_base *ab, bool ack)
236 {
237 ath12k_dbg(ab, ATH12K_DBG_BOOT, "boot suspend complete %d\n", ack);
238
239 if (ack)
240 set_bit(ATH12K_FLAG_HTC_SUSPEND_COMPLETE, &ab->dev_flags);
241 else
242 clear_bit(ATH12K_FLAG_HTC_SUSPEND_COMPLETE, &ab->dev_flags);
243
244 complete(&ab->htc_suspend);
245 }
246
ath12k_htc_wakeup_from_suspend(struct ath12k_base * ab)247 static void ath12k_htc_wakeup_from_suspend(struct ath12k_base *ab)
248 {
249 ath12k_dbg(ab, ATH12K_DBG_BOOT, "boot wakeup from suspend is received\n");
250 }
251
ath12k_htc_rx_completion_handler(struct ath12k_base * ab,struct sk_buff * skb)252 void ath12k_htc_rx_completion_handler(struct ath12k_base *ab,
253 struct sk_buff *skb)
254 {
255 int status = 0;
256 struct ath12k_htc *htc = &ab->htc;
257 struct ath12k_htc_hdr *hdr;
258 struct ath12k_htc_ep *ep;
259 u16 payload_len;
260 u32 trailer_len = 0;
261 size_t min_len;
262 u8 eid;
263 bool trailer_present;
264
265 hdr = (struct ath12k_htc_hdr *)skb->data;
266 skb_pull(skb, sizeof(*hdr));
267
268 eid = le32_get_bits(hdr->htc_info, HTC_HDR_ENDPOINTID);
269
270 if (eid >= ATH12K_HTC_EP_COUNT) {
271 ath12k_warn(ab, "HTC Rx: invalid eid %d\n", eid);
272 goto out;
273 }
274
275 ep = &htc->endpoint[eid];
276
277 payload_len = le32_get_bits(hdr->htc_info, HTC_HDR_PAYLOADLEN);
278
279 if (payload_len + sizeof(*hdr) > ATH12K_HTC_MAX_LEN) {
280 ath12k_warn(ab, "HTC rx frame too long, len: %zu\n",
281 payload_len + sizeof(*hdr));
282 goto out;
283 }
284
285 if (skb->len < payload_len) {
286 ath12k_warn(ab, "HTC Rx: insufficient length, got %d, expected %d\n",
287 skb->len, payload_len);
288 goto out;
289 }
290
291 /* get flags to check for trailer */
292 trailer_present = le32_get_bits(hdr->htc_info, HTC_HDR_FLAGS) &
293 ATH12K_HTC_FLAG_TRAILER_PRESENT;
294
295 if (trailer_present) {
296 u8 *trailer;
297
298 trailer_len = le32_get_bits(hdr->ctrl_info,
299 HTC_HDR_CONTROLBYTES0);
300 min_len = sizeof(struct ath12k_htc_record_hdr);
301
302 if ((trailer_len < min_len) ||
303 (trailer_len > payload_len)) {
304 ath12k_warn(ab, "Invalid trailer length: %d\n",
305 trailer_len);
306 goto out;
307 }
308
309 trailer = (u8 *)hdr;
310 trailer += sizeof(*hdr);
311 trailer += payload_len;
312 trailer -= trailer_len;
313 status = ath12k_htc_process_trailer(htc, trailer,
314 trailer_len, eid);
315 if (status)
316 goto out;
317
318 skb_trim(skb, skb->len - trailer_len);
319 }
320
321 if (trailer_len >= payload_len)
322 /* zero length packet with trailer data, just drop these */
323 goto out;
324
325 if (eid == ATH12K_HTC_EP_0) {
326 struct ath12k_htc_msg *msg = (struct ath12k_htc_msg *)skb->data;
327
328 switch (le32_get_bits(msg->msg_svc_id, HTC_MSG_MESSAGEID)) {
329 case ATH12K_HTC_MSG_READY_ID:
330 case ATH12K_HTC_MSG_CONNECT_SERVICE_RESP_ID:
331 /* handle HTC control message */
332 if (completion_done(&htc->ctl_resp)) {
333 /* this is a fatal error, target should not be
334 * sending unsolicited messages on the ep 0
335 */
336 ath12k_warn(ab, "HTC rx ctrl still processing\n");
337 complete(&htc->ctl_resp);
338 goto out;
339 }
340
341 htc->control_resp_len =
342 min_t(int, skb->len,
343 ATH12K_HTC_MAX_CTRL_MSG_LEN);
344
345 memcpy(htc->control_resp_buffer, skb->data,
346 htc->control_resp_len);
347
348 complete(&htc->ctl_resp);
349 break;
350 case ATH12K_HTC_MSG_SEND_SUSPEND_COMPLETE:
351 ath12k_htc_suspend_complete(ab, true);
352 break;
353 case ATH12K_HTC_MSG_NACK_SUSPEND:
354 ath12k_htc_suspend_complete(ab, false);
355 break;
356 case ATH12K_HTC_MSG_WAKEUP_FROM_SUSPEND_ID:
357 ath12k_htc_wakeup_from_suspend(ab);
358 break;
359 default:
360 ath12k_warn(ab, "ignoring unsolicited htc ep0 event %u\n",
361 le32_get_bits(msg->msg_svc_id, HTC_MSG_MESSAGEID));
362 break;
363 }
364 goto out;
365 }
366
367 ath12k_dbg(ab, ATH12K_DBG_HTC, "htc rx completion ep %d skb %p\n",
368 eid, skb);
369 ep->ep_ops.ep_rx_complete(ab, skb);
370
371 /* poll tx completion for interrupt disabled CE's */
372 ath12k_ce_poll_send_completed(ab, ep->ul_pipe_id);
373
374 /* skb is now owned by the rx completion handler */
375 skb = NULL;
376 out:
377 kfree_skb(skb);
378 }
379 EXPORT_SYMBOL(ath12k_htc_rx_completion_handler);
380
ath12k_htc_control_rx_complete(struct ath12k_base * ab,struct sk_buff * skb)381 static void ath12k_htc_control_rx_complete(struct ath12k_base *ab,
382 struct sk_buff *skb)
383 {
384 /* This is unexpected. FW is not supposed to send regular rx on this
385 * endpoint.
386 */
387 ath12k_warn(ab, "unexpected htc rx\n");
388 kfree_skb(skb);
389 }
390
htc_service_name(enum ath12k_htc_svc_id id)391 static const char *htc_service_name(enum ath12k_htc_svc_id id)
392 {
393 switch (id) {
394 case ATH12K_HTC_SVC_ID_RESERVED:
395 return "Reserved";
396 case ATH12K_HTC_SVC_ID_RSVD_CTRL:
397 return "Control";
398 case ATH12K_HTC_SVC_ID_WMI_CONTROL:
399 return "WMI";
400 case ATH12K_HTC_SVC_ID_WMI_DATA_BE:
401 return "DATA BE";
402 case ATH12K_HTC_SVC_ID_WMI_DATA_BK:
403 return "DATA BK";
404 case ATH12K_HTC_SVC_ID_WMI_DATA_VI:
405 return "DATA VI";
406 case ATH12K_HTC_SVC_ID_WMI_DATA_VO:
407 return "DATA VO";
408 case ATH12K_HTC_SVC_ID_WMI_CONTROL_MAC1:
409 return "WMI MAC1";
410 case ATH12K_HTC_SVC_ID_WMI_CONTROL_MAC2:
411 return "WMI MAC2";
412 case ATH12K_HTC_SVC_ID_NMI_CONTROL:
413 return "NMI Control";
414 case ATH12K_HTC_SVC_ID_NMI_DATA:
415 return "NMI Data";
416 case ATH12K_HTC_SVC_ID_HTT_DATA_MSG:
417 return "HTT Data";
418 case ATH12K_HTC_SVC_ID_TEST_RAW_STREAMS:
419 return "RAW";
420 case ATH12K_HTC_SVC_ID_IPA_TX:
421 return "IPA TX";
422 case ATH12K_HTC_SVC_ID_PKT_LOG:
423 return "PKT LOG";
424 case ATH12K_HTC_SVC_ID_WMI_CONTROL_DIAG:
425 return "WMI DIAG";
426 }
427
428 return "Unknown";
429 }
430
ath12k_htc_reset_endpoint_states(struct ath12k_htc * htc)431 static void ath12k_htc_reset_endpoint_states(struct ath12k_htc *htc)
432 {
433 struct ath12k_htc_ep *ep;
434 int i;
435
436 for (i = ATH12K_HTC_EP_0; i < ATH12K_HTC_EP_COUNT; i++) {
437 ep = &htc->endpoint[i];
438 ep->service_id = ATH12K_HTC_SVC_ID_UNUSED;
439 ep->max_ep_message_len = 0;
440 ep->max_tx_queue_depth = 0;
441 ep->eid = i;
442 ep->htc = htc;
443 ep->tx_credit_flow_enabled = true;
444 }
445 }
446
ath12k_htc_get_credit_allocation(struct ath12k_htc * htc,u16 service_id)447 static u8 ath12k_htc_get_credit_allocation(struct ath12k_htc *htc,
448 u16 service_id)
449 {
450 struct ath12k_htc_svc_tx_credits *serv_entry;
451 u8 i, allocation = 0;
452
453 serv_entry = htc->service_alloc_table;
454
455 for (i = 0; i < ATH12K_HTC_MAX_SERVICE_ALLOC_ENTRIES; i++) {
456 if (serv_entry[i].service_id == service_id) {
457 allocation = serv_entry[i].credit_allocation;
458 break;
459 }
460 }
461
462 return allocation;
463 }
464
ath12k_htc_setup_target_buffer_assignments(struct ath12k_htc * htc)465 static int ath12k_htc_setup_target_buffer_assignments(struct ath12k_htc *htc)
466 {
467 struct ath12k_htc_svc_tx_credits *serv_entry;
468 static const u32 svc_id[] = {
469 ATH12K_HTC_SVC_ID_WMI_CONTROL,
470 ATH12K_HTC_SVC_ID_WMI_CONTROL_MAC1,
471 ATH12K_HTC_SVC_ID_WMI_CONTROL_MAC2,
472 };
473 int i, credits;
474
475 credits = htc->total_transmit_credits;
476 serv_entry = htc->service_alloc_table;
477
478 if ((htc->wmi_ep_count == 0) ||
479 (htc->wmi_ep_count > ARRAY_SIZE(svc_id)))
480 return -EINVAL;
481
482 /* Divide credits among number of endpoints for WMI */
483 credits = credits / htc->wmi_ep_count;
484 for (i = 0; i < htc->wmi_ep_count; i++) {
485 serv_entry[i].service_id = svc_id[i];
486 serv_entry[i].credit_allocation = credits;
487 }
488
489 return 0;
490 }
491
ath12k_htc_wait_target(struct ath12k_htc * htc)492 int ath12k_htc_wait_target(struct ath12k_htc *htc)
493 {
494 int i, status = 0;
495 struct ath12k_base *ab = htc->ab;
496 unsigned long time_left;
497 struct ath12k_htc_ready *ready;
498 u16 message_id;
499 u16 credit_count;
500 u16 credit_size;
501
502 time_left = wait_for_completion_timeout(&htc->ctl_resp,
503 ATH12K_HTC_WAIT_TIMEOUT_HZ);
504 if (!time_left) {
505 ath12k_warn(ab, "failed to receive control response completion, polling..\n");
506
507 for (i = 0; i < ab->hw_params->ce_count; i++)
508 ath12k_ce_per_engine_service(htc->ab, i);
509
510 time_left =
511 wait_for_completion_timeout(&htc->ctl_resp,
512 ATH12K_HTC_WAIT_TIMEOUT_HZ);
513
514 if (!time_left)
515 status = -ETIMEDOUT;
516 }
517
518 if (status < 0) {
519 ath12k_warn(ab, "ctl_resp never came in (%d)\n", status);
520 return status;
521 }
522
523 if (htc->control_resp_len < sizeof(*ready)) {
524 ath12k_warn(ab, "Invalid HTC ready msg len:%d\n",
525 htc->control_resp_len);
526 return -ECOMM;
527 }
528
529 ready = (struct ath12k_htc_ready *)htc->control_resp_buffer;
530 message_id = le32_get_bits(ready->id_credit_count, HTC_MSG_MESSAGEID);
531 credit_count = le32_get_bits(ready->id_credit_count,
532 HTC_READY_MSG_CREDITCOUNT);
533 credit_size = le32_get_bits(ready->size_ep, HTC_READY_MSG_CREDITSIZE);
534
535 if (message_id != ATH12K_HTC_MSG_READY_ID) {
536 ath12k_warn(ab, "Invalid HTC ready msg: 0x%x\n", message_id);
537 return -ECOMM;
538 }
539
540 htc->total_transmit_credits = credit_count;
541 htc->target_credit_size = credit_size;
542
543 ath12k_dbg(ab, ATH12K_DBG_HTC,
544 "Target ready! transmit resources: %d size:%d\n",
545 htc->total_transmit_credits, htc->target_credit_size);
546
547 if ((htc->total_transmit_credits == 0) ||
548 (htc->target_credit_size == 0)) {
549 ath12k_warn(ab, "Invalid credit size received\n");
550 return -ECOMM;
551 }
552
553 ath12k_htc_setup_target_buffer_assignments(htc);
554
555 return 0;
556 }
557
ath12k_htc_connect_service(struct ath12k_htc * htc,struct ath12k_htc_svc_conn_req * conn_req,struct ath12k_htc_svc_conn_resp * conn_resp)558 int ath12k_htc_connect_service(struct ath12k_htc *htc,
559 struct ath12k_htc_svc_conn_req *conn_req,
560 struct ath12k_htc_svc_conn_resp *conn_resp)
561 {
562 struct ath12k_base *ab = htc->ab;
563 struct ath12k_htc_conn_svc *req_msg;
564 struct ath12k_htc_conn_svc_resp resp_msg_dummy;
565 struct ath12k_htc_conn_svc_resp *resp_msg = &resp_msg_dummy;
566 enum ath12k_htc_ep_id assigned_eid = ATH12K_HTC_EP_COUNT;
567 struct ath12k_htc_ep *ep;
568 struct sk_buff *skb;
569 unsigned int max_msg_size = 0;
570 int length, status;
571 unsigned long time_left;
572 bool disable_credit_flow_ctrl = false;
573 u16 message_id, service_id, flags = 0;
574 u8 tx_alloc = 0;
575
576 /* special case for HTC pseudo control service */
577 if (conn_req->service_id == ATH12K_HTC_SVC_ID_RSVD_CTRL) {
578 disable_credit_flow_ctrl = true;
579 assigned_eid = ATH12K_HTC_EP_0;
580 max_msg_size = ATH12K_HTC_MAX_CTRL_MSG_LEN;
581 memset(&resp_msg_dummy, 0, sizeof(resp_msg_dummy));
582 goto setup;
583 }
584
585 tx_alloc = ath12k_htc_get_credit_allocation(htc,
586 conn_req->service_id);
587 if (!tx_alloc)
588 ath12k_dbg(ab, ATH12K_DBG_BOOT,
589 "boot htc service %s does not allocate target credits\n",
590 htc_service_name(conn_req->service_id));
591
592 skb = ath12k_htc_build_tx_ctrl_skb();
593 if (!skb) {
594 ath12k_warn(ab, "Failed to allocate HTC packet\n");
595 return -ENOMEM;
596 }
597
598 length = sizeof(*req_msg);
599 skb_put(skb, length);
600 memset(skb->data, 0, length);
601
602 req_msg = (struct ath12k_htc_conn_svc *)skb->data;
603 req_msg->msg_svc_id = le32_encode_bits(ATH12K_HTC_MSG_CONNECT_SERVICE_ID,
604 HTC_MSG_MESSAGEID);
605
606 flags |= u32_encode_bits(tx_alloc, ATH12K_HTC_CONN_FLAGS_RECV_ALLOC);
607
608 /* Only enable credit flow control for WMI ctrl service */
609 if (!(conn_req->service_id == ATH12K_HTC_SVC_ID_WMI_CONTROL ||
610 conn_req->service_id == ATH12K_HTC_SVC_ID_WMI_CONTROL_MAC1 ||
611 conn_req->service_id == ATH12K_HTC_SVC_ID_WMI_CONTROL_MAC2)) {
612 flags |= ATH12K_HTC_CONN_FLAGS_DISABLE_CREDIT_FLOW_CTRL;
613 disable_credit_flow_ctrl = true;
614 }
615
616 req_msg->flags_len = le32_encode_bits(flags, HTC_SVC_MSG_CONNECTIONFLAGS);
617 req_msg->msg_svc_id |= le32_encode_bits(conn_req->service_id,
618 HTC_SVC_MSG_SERVICE_ID);
619
620 reinit_completion(&htc->ctl_resp);
621
622 status = ath12k_htc_send(htc, ATH12K_HTC_EP_0, skb);
623 if (status) {
624 kfree_skb(skb);
625 return status;
626 }
627
628 /* wait for response */
629 time_left = wait_for_completion_timeout(&htc->ctl_resp,
630 ATH12K_HTC_CONN_SVC_TIMEOUT_HZ);
631 if (!time_left) {
632 ath12k_err(ab, "Service connect timeout\n");
633 return -ETIMEDOUT;
634 }
635
636 /* we controlled the buffer creation, it's aligned */
637 resp_msg = (struct ath12k_htc_conn_svc_resp *)htc->control_resp_buffer;
638 message_id = le32_get_bits(resp_msg->msg_svc_id, HTC_MSG_MESSAGEID);
639 service_id = le32_get_bits(resp_msg->msg_svc_id,
640 HTC_SVC_RESP_MSG_SERVICEID);
641
642 if ((message_id != ATH12K_HTC_MSG_CONNECT_SERVICE_RESP_ID) ||
643 (htc->control_resp_len < sizeof(*resp_msg))) {
644 ath12k_err(ab, "Invalid resp message ID 0x%x", message_id);
645 return -EPROTO;
646 }
647
648 ath12k_dbg(ab, ATH12K_DBG_HTC,
649 "HTC Service %s connect response: status: %u, assigned ep: %u\n",
650 htc_service_name(service_id),
651 le32_get_bits(resp_msg->flags_len, HTC_SVC_RESP_MSG_STATUS),
652 le32_get_bits(resp_msg->flags_len, HTC_SVC_RESP_MSG_ENDPOINTID));
653
654 conn_resp->connect_resp_code = le32_get_bits(resp_msg->flags_len,
655 HTC_SVC_RESP_MSG_STATUS);
656
657 /* check response status */
658 if (conn_resp->connect_resp_code != ATH12K_HTC_CONN_SVC_STATUS_SUCCESS) {
659 ath12k_err(ab, "HTC Service %s connect request failed: 0x%x)\n",
660 htc_service_name(service_id),
661 conn_resp->connect_resp_code);
662 return -EPROTO;
663 }
664
665 assigned_eid = le32_get_bits(resp_msg->flags_len,
666 HTC_SVC_RESP_MSG_ENDPOINTID);
667
668 max_msg_size = le32_get_bits(resp_msg->flags_len,
669 HTC_SVC_RESP_MSG_MAXMSGSIZE);
670
671 setup:
672
673 if (assigned_eid >= ATH12K_HTC_EP_COUNT)
674 return -EPROTO;
675
676 if (max_msg_size == 0)
677 return -EPROTO;
678
679 ep = &htc->endpoint[assigned_eid];
680 ep->eid = assigned_eid;
681
682 if (ep->service_id != ATH12K_HTC_SVC_ID_UNUSED)
683 return -EPROTO;
684
685 /* return assigned endpoint to caller */
686 conn_resp->eid = assigned_eid;
687 conn_resp->max_msg_len = le32_get_bits(resp_msg->flags_len,
688 HTC_SVC_RESP_MSG_MAXMSGSIZE);
689
690 /* setup the endpoint */
691 ep->service_id = conn_req->service_id;
692 ep->max_tx_queue_depth = conn_req->max_send_queue_depth;
693 ep->max_ep_message_len = le32_get_bits(resp_msg->flags_len,
694 HTC_SVC_RESP_MSG_MAXMSGSIZE);
695 ep->tx_credits = tx_alloc;
696
697 /* copy all the callbacks */
698 ep->ep_ops = conn_req->ep_ops;
699
700 status = ath12k_hif_map_service_to_pipe(htc->ab,
701 ep->service_id,
702 &ep->ul_pipe_id,
703 &ep->dl_pipe_id);
704 if (status)
705 return status;
706
707 ath12k_dbg(ab, ATH12K_DBG_BOOT,
708 "boot htc service '%s' ul pipe %d dl pipe %d eid %d ready\n",
709 htc_service_name(ep->service_id), ep->ul_pipe_id,
710 ep->dl_pipe_id, ep->eid);
711
712 if (disable_credit_flow_ctrl && ep->tx_credit_flow_enabled) {
713 ep->tx_credit_flow_enabled = false;
714 ath12k_dbg(ab, ATH12K_DBG_BOOT,
715 "boot htc service '%s' eid %d TX flow control disabled\n",
716 htc_service_name(ep->service_id), assigned_eid);
717 }
718
719 return status;
720 }
721
ath12k_htc_start(struct ath12k_htc * htc)722 int ath12k_htc_start(struct ath12k_htc *htc)
723 {
724 struct sk_buff *skb;
725 int status;
726 struct ath12k_base *ab = htc->ab;
727 struct ath12k_htc_setup_complete_extended *msg;
728
729 skb = ath12k_htc_build_tx_ctrl_skb();
730 if (!skb)
731 return -ENOMEM;
732
733 skb_put(skb, sizeof(*msg));
734 memset(skb->data, 0, skb->len);
735
736 msg = (struct ath12k_htc_setup_complete_extended *)skb->data;
737 msg->msg_id = le32_encode_bits(ATH12K_HTC_MSG_SETUP_COMPLETE_EX_ID,
738 HTC_MSG_MESSAGEID);
739
740 ath12k_dbg(ab, ATH12K_DBG_HTC, "HTC is using TX credit flow control\n");
741
742 status = ath12k_htc_send(htc, ATH12K_HTC_EP_0, skb);
743 if (status) {
744 kfree_skb(skb);
745 return status;
746 }
747
748 return 0;
749 }
750
ath12k_htc_init(struct ath12k_base * ab)751 int ath12k_htc_init(struct ath12k_base *ab)
752 {
753 struct ath12k_htc *htc = &ab->htc;
754 struct ath12k_htc_svc_conn_req conn_req = { };
755 struct ath12k_htc_svc_conn_resp conn_resp = { };
756 int ret;
757
758 spin_lock_init(&htc->tx_lock);
759
760 ath12k_htc_reset_endpoint_states(htc);
761
762 htc->ab = ab;
763
764 switch (ab->wmi_ab.preferred_hw_mode) {
765 case WMI_HOST_HW_MODE_SINGLE:
766 htc->wmi_ep_count = 1;
767 break;
768 case WMI_HOST_HW_MODE_DBS:
769 case WMI_HOST_HW_MODE_DBS_OR_SBS:
770 htc->wmi_ep_count = 2;
771 break;
772 case WMI_HOST_HW_MODE_DBS_SBS:
773 htc->wmi_ep_count = 3;
774 break;
775 default:
776 htc->wmi_ep_count = ab->hw_params->max_radios;
777 break;
778 }
779
780 /* setup our pseudo HTC control endpoint connection */
781 conn_req.ep_ops.ep_tx_complete = ath12k_htc_control_tx_complete;
782 conn_req.ep_ops.ep_rx_complete = ath12k_htc_control_rx_complete;
783 conn_req.max_send_queue_depth = ATH12K_NUM_CONTROL_TX_BUFFERS;
784 conn_req.service_id = ATH12K_HTC_SVC_ID_RSVD_CTRL;
785
786 /* connect fake service */
787 ret = ath12k_htc_connect_service(htc, &conn_req, &conn_resp);
788 if (ret) {
789 ath12k_err(ab, "could not connect to htc service (%d)\n", ret);
790 return ret;
791 }
792
793 init_completion(&htc->ctl_resp);
794
795 return 0;
796 }
797