Path: blob/main/sys/contrib/dev/athk/ath10k/htc.c
106214 views
// SPDX-License-Identifier: ISC1/*2* Copyright (c) 2005-2011 Atheros Communications Inc.3* Copyright (c) 2011-2017 Qualcomm Atheros, Inc.4* Copyright (c) 2022 Qualcomm Innovation Center, Inc. All rights reserved.5* Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries.6*/78#include <linux/export.h>910#include "core.h"11#include "hif.h"12#include "debug.h"13#if defined(__FreeBSD__)14#include <linux/delay.h>15#endif1617/********/18/* Send */19/********/2021static void ath10k_htc_control_tx_complete(struct ath10k *ar,22struct sk_buff *skb)23{24kfree_skb(skb);25}2627static struct sk_buff *ath10k_htc_build_tx_ctrl_skb(void *ar)28{29struct sk_buff *skb;30struct ath10k_skb_cb *skb_cb;3132skb = dev_alloc_skb(ATH10K_HTC_CONTROL_BUFFER_SIZE);33if (!skb)34return NULL;3536skb_reserve(skb, 20); /* FIXME: why 20 bytes? */37WARN_ONCE((unsigned long)skb->data & 3, "unaligned skb");3839skb_cb = ATH10K_SKB_CB(skb);40memset(skb_cb, 0, sizeof(*skb_cb));4142ath10k_dbg(ar, ATH10K_DBG_HTC, "%s: skb %p\n", __func__, skb);43return skb;44}4546static inline void ath10k_htc_restore_tx_skb(struct ath10k_htc *htc,47struct sk_buff *skb)48{49struct ath10k_skb_cb *skb_cb = ATH10K_SKB_CB(skb);5051if (htc->ar->bus_param.dev_type != ATH10K_DEV_TYPE_HL)52dma_unmap_single(htc->ar->dev, skb_cb->paddr, skb->len, DMA_TO_DEVICE);53skb_pull(skb, sizeof(struct ath10k_htc_hdr));54}5556void ath10k_htc_notify_tx_completion(struct ath10k_htc_ep *ep,57struct sk_buff *skb)58{59struct ath10k *ar = ep->htc->ar;60struct ath10k_htc_hdr *hdr;6162ath10k_dbg(ar, ATH10K_DBG_HTC, "%s: ep %d skb %p\n", __func__,63ep->eid, skb);6465/* A corner case where the copy completion is reaching to host but still66* copy engine is processing it due to which host unmaps corresponding67* memory and causes SMMU fault, hence as workaround adding delay68* the unmapping memory to avoid SMMU faults.69*/70if (ar->hw_params.delay_unmap_buffer &&71ep->ul_pipe_id == 3)72mdelay(2);7374hdr = (struct ath10k_htc_hdr *)skb->data;75ath10k_htc_restore_tx_skb(ep->htc, skb);7677if (!ep->ep_ops.ep_tx_complete) {78ath10k_warn(ar, "no tx handler for eid %d\n", ep->eid);79dev_kfree_skb_any(skb);80return;81}8283if (hdr->flags & ATH10K_HTC_FLAG_SEND_BUNDLE) {84dev_kfree_skb_any(skb);85return;86}8788ep->ep_ops.ep_tx_complete(ep->htc->ar, skb);89}90EXPORT_SYMBOL(ath10k_htc_notify_tx_completion);9192static void ath10k_htc_prepare_tx_skb(struct ath10k_htc_ep *ep,93struct sk_buff *skb)94{95struct ath10k_htc_hdr *hdr;9697hdr = (struct ath10k_htc_hdr *)skb->data;98memset(hdr, 0, sizeof(struct ath10k_htc_hdr));99100hdr->eid = ep->eid;101hdr->len = __cpu_to_le16(skb->len - sizeof(*hdr));102hdr->flags = 0;103if (ep->tx_credit_flow_enabled && !ep->bundle_tx)104hdr->flags |= ATH10K_HTC_FLAG_NEED_CREDIT_UPDATE;105106spin_lock_bh(&ep->htc->tx_lock);107hdr->seq_no = ep->seq_no++;108spin_unlock_bh(&ep->htc->tx_lock);109}110111static int ath10k_htc_consume_credit(struct ath10k_htc_ep *ep,112unsigned int len,113bool consume)114{115struct ath10k_htc *htc = ep->htc;116struct ath10k *ar = htc->ar;117enum ath10k_htc_ep_id eid = ep->eid;118int credits, ret = 0;119120if (!ep->tx_credit_flow_enabled)121return 0;122123credits = DIV_ROUND_UP(len, ep->tx_credit_size);124spin_lock_bh(&htc->tx_lock);125126if (ep->tx_credits < credits) {127ath10k_dbg(ar, ATH10K_DBG_HTC,128"htc insufficient credits ep %d required %d available %d consume %d\n",129eid, credits, ep->tx_credits, consume);130ret = -EAGAIN;131goto unlock;132}133134if (consume) {135ep->tx_credits -= credits;136ath10k_dbg(ar, ATH10K_DBG_HTC,137"htc ep %d consumed %d credits total %d\n",138eid, credits, ep->tx_credits);139}140141unlock:142spin_unlock_bh(&htc->tx_lock);143return ret;144}145146static void ath10k_htc_release_credit(struct ath10k_htc_ep *ep, unsigned int len)147{148struct ath10k_htc *htc = ep->htc;149struct ath10k *ar = htc->ar;150enum ath10k_htc_ep_id eid = ep->eid;151int credits;152153if (!ep->tx_credit_flow_enabled)154return;155156credits = DIV_ROUND_UP(len, ep->tx_credit_size);157spin_lock_bh(&htc->tx_lock);158ep->tx_credits += credits;159ath10k_dbg(ar, ATH10K_DBG_HTC,160"htc ep %d reverted %d credits back total %d\n",161eid, credits, ep->tx_credits);162spin_unlock_bh(&htc->tx_lock);163164if (ep->ep_ops.ep_tx_credits)165ep->ep_ops.ep_tx_credits(htc->ar);166}167168int ath10k_htc_send(struct ath10k_htc *htc,169enum ath10k_htc_ep_id eid,170struct sk_buff *skb)171{172struct ath10k *ar = htc->ar;173struct ath10k_htc_ep *ep = &htc->endpoint[eid];174struct ath10k_skb_cb *skb_cb = ATH10K_SKB_CB(skb);175struct ath10k_hif_sg_item sg_item;176struct device *dev = htc->ar->dev;177int ret;178unsigned int skb_len;179180if (htc->ar->state == ATH10K_STATE_WEDGED)181return -ECOMM;182183if (eid >= ATH10K_HTC_EP_COUNT) {184ath10k_warn(ar, "Invalid endpoint id: %d\n", eid);185return -ENOENT;186}187188skb_push(skb, sizeof(struct ath10k_htc_hdr));189190skb_len = skb->len;191ret = ath10k_htc_consume_credit(ep, skb_len, true);192if (ret)193goto err_pull;194195ath10k_htc_prepare_tx_skb(ep, skb);196197skb_cb->eid = eid;198if (ar->bus_param.dev_type != ATH10K_DEV_TYPE_HL) {199skb_cb->paddr = dma_map_single(dev, skb->data, skb->len,200DMA_TO_DEVICE);201ret = dma_mapping_error(dev, skb_cb->paddr);202if (ret) {203ret = -EIO;204goto err_credits;205}206}207208sg_item.transfer_id = ep->eid;209sg_item.transfer_context = skb;210sg_item.vaddr = skb->data;211sg_item.paddr = skb_cb->paddr;212sg_item.len = skb->len;213214ret = ath10k_hif_tx_sg(htc->ar, ep->ul_pipe_id, &sg_item, 1);215if (ret)216goto err_unmap;217218return 0;219220err_unmap:221if (ar->bus_param.dev_type != ATH10K_DEV_TYPE_HL)222dma_unmap_single(dev, skb_cb->paddr, skb->len, DMA_TO_DEVICE);223err_credits:224ath10k_htc_release_credit(ep, skb_len);225err_pull:226skb_pull(skb, sizeof(struct ath10k_htc_hdr));227return ret;228}229230void ath10k_htc_tx_completion_handler(struct ath10k *ar, struct sk_buff *skb)231{232struct ath10k_htc *htc = &ar->htc;233struct ath10k_skb_cb *skb_cb;234struct ath10k_htc_ep *ep;235236if (WARN_ON_ONCE(!skb))237return;238239skb_cb = ATH10K_SKB_CB(skb);240ep = &htc->endpoint[skb_cb->eid];241242ath10k_htc_notify_tx_completion(ep, skb);243/* the skb now belongs to the completion handler */244}245EXPORT_SYMBOL(ath10k_htc_tx_completion_handler);246247/***********/248/* Receive */249/***********/250251static void252ath10k_htc_process_credit_report(struct ath10k_htc *htc,253const struct ath10k_htc_credit_report *report,254int len,255enum ath10k_htc_ep_id eid)256{257struct ath10k *ar = htc->ar;258struct ath10k_htc_ep *ep;259int i, n_reports;260261if (len % sizeof(*report))262ath10k_warn(ar, "Uneven credit report len %d", len);263264n_reports = len / sizeof(*report);265266spin_lock_bh(&htc->tx_lock);267for (i = 0; i < n_reports; i++, report++) {268if (report->eid >= ATH10K_HTC_EP_COUNT)269break;270271ep = &htc->endpoint[report->eid];272ep->tx_credits += report->credits;273274ath10k_dbg(ar, ATH10K_DBG_HTC, "htc ep %d got %d credits (total %d)\n",275report->eid, report->credits, ep->tx_credits);276277if (ep->ep_ops.ep_tx_credits) {278spin_unlock_bh(&htc->tx_lock);279ep->ep_ops.ep_tx_credits(htc->ar);280spin_lock_bh(&htc->tx_lock);281}282}283spin_unlock_bh(&htc->tx_lock);284}285286static int287ath10k_htc_process_lookahead(struct ath10k_htc *htc,288const struct ath10k_htc_lookahead_report *report,289int len,290enum ath10k_htc_ep_id eid,291void *next_lookaheads,292int *next_lookaheads_len)293{294struct ath10k *ar = htc->ar;295296/* Invalid lookahead flags are actually transmitted by297* the target in the HTC control message.298* Since this will happen at every boot we silently ignore299* the lookahead in this case300*/301if (report->pre_valid != ((~report->post_valid) & 0xFF))302return 0;303304if (next_lookaheads && next_lookaheads_len) {305ath10k_dbg(ar, ATH10K_DBG_HTC,306"htc rx lookahead found pre_valid 0x%x post_valid 0x%x\n",307report->pre_valid, report->post_valid);308309/* look ahead bytes are valid, copy them over */310memcpy((u8 *)next_lookaheads, report->lookahead, 4);311312*next_lookaheads_len = 1;313}314315return 0;316}317318static int319ath10k_htc_process_lookahead_bundle(struct ath10k_htc *htc,320const struct ath10k_htc_lookahead_bundle *report,321int len,322enum ath10k_htc_ep_id eid,323void *next_lookaheads,324int *next_lookaheads_len)325{326struct ath10k *ar = htc->ar;327int bundle_cnt = len / sizeof(*report);328329if (!bundle_cnt || (bundle_cnt > htc->max_msgs_per_htc_bundle)) {330ath10k_warn(ar, "Invalid lookahead bundle count: %d\n",331bundle_cnt);332return -EINVAL;333}334335if (next_lookaheads && next_lookaheads_len) {336int i;337338for (i = 0; i < bundle_cnt; i++) {339memcpy(((u8 *)next_lookaheads) + 4 * i,340report->lookahead, 4);341report++;342}343344*next_lookaheads_len = bundle_cnt;345}346347return 0;348}349350int ath10k_htc_process_trailer(struct ath10k_htc *htc,351u8 *buffer,352int length,353enum ath10k_htc_ep_id src_eid,354void *next_lookaheads,355int *next_lookaheads_len)356{357struct ath10k_htc_lookahead_bundle *bundle;358struct ath10k *ar = htc->ar;359int status = 0;360struct ath10k_htc_record *record;361u8 *orig_buffer;362int orig_length;363size_t len;364365orig_buffer = buffer;366orig_length = length;367368while (length > 0) {369record = (struct ath10k_htc_record *)buffer;370371if (length < sizeof(record->hdr)) {372status = -EINVAL;373break;374}375376if (record->hdr.len > length) {377/* no room left in buffer for record */378ath10k_warn(ar, "Invalid record length: %d\n",379record->hdr.len);380status = -EINVAL;381break;382}383384switch (record->hdr.id) {385case ATH10K_HTC_RECORD_CREDITS:386len = sizeof(struct ath10k_htc_credit_report);387if (record->hdr.len < len) {388ath10k_warn(ar, "Credit report too long\n");389status = -EINVAL;390break;391}392ath10k_htc_process_credit_report(htc,393record->credit_report,394record->hdr.len,395src_eid);396break;397case ATH10K_HTC_RECORD_LOOKAHEAD:398len = sizeof(struct ath10k_htc_lookahead_report);399if (record->hdr.len < len) {400ath10k_warn(ar, "Lookahead report too long\n");401status = -EINVAL;402break;403}404status = ath10k_htc_process_lookahead(htc,405record->lookahead_report,406record->hdr.len,407src_eid,408next_lookaheads,409next_lookaheads_len);410break;411case ATH10K_HTC_RECORD_LOOKAHEAD_BUNDLE:412bundle = record->lookahead_bundle;413status = ath10k_htc_process_lookahead_bundle(htc,414bundle,415record->hdr.len,416src_eid,417next_lookaheads,418next_lookaheads_len);419break;420default:421ath10k_warn(ar, "Unhandled record: id:%d length:%d\n",422record->hdr.id, record->hdr.len);423break;424}425426if (status)427break;428429/* multiple records may be present in a trailer */430buffer += sizeof(record->hdr) + record->hdr.len;431length -= sizeof(record->hdr) + record->hdr.len;432}433434if (status)435ath10k_dbg_dump(ar, ATH10K_DBG_HTC, "htc rx bad trailer", "",436orig_buffer, orig_length);437438return status;439}440EXPORT_SYMBOL(ath10k_htc_process_trailer);441442void ath10k_htc_rx_completion_handler(struct ath10k *ar, struct sk_buff *skb)443{444int status = 0;445struct ath10k_htc *htc = &ar->htc;446struct ath10k_htc_hdr *hdr;447struct ath10k_htc_ep *ep;448u16 payload_len;449u32 trailer_len = 0;450size_t min_len;451u8 eid;452bool trailer_present;453454hdr = (struct ath10k_htc_hdr *)skb->data;455skb_pull(skb, sizeof(*hdr));456457eid = hdr->eid;458459if (eid >= ATH10K_HTC_EP_COUNT) {460ath10k_warn(ar, "HTC Rx: invalid eid %d\n", eid);461ath10k_dbg_dump(ar, ATH10K_DBG_HTC, "htc bad header", "",462hdr, sizeof(*hdr));463goto out;464}465466ep = &htc->endpoint[eid];467if (ep->service_id == ATH10K_HTC_SVC_ID_UNUSED) {468ath10k_warn(ar, "htc rx endpoint %d is not connected\n", eid);469goto out;470}471472payload_len = __le16_to_cpu(hdr->len);473474if (payload_len + sizeof(*hdr) > ATH10K_HTC_MAX_LEN) {475ath10k_warn(ar, "HTC rx frame too long, len: %zu\n",476payload_len + sizeof(*hdr));477ath10k_dbg_dump(ar, ATH10K_DBG_HTC, "htc bad rx pkt len", "",478hdr, sizeof(*hdr));479goto out;480}481482if (skb->len < payload_len) {483ath10k_dbg(ar, ATH10K_DBG_HTC,484"HTC Rx: insufficient length, got %d, expected %d\n",485skb->len, payload_len);486ath10k_dbg_dump(ar, ATH10K_DBG_HTC, "htc bad rx pkt len",487"", hdr, sizeof(*hdr));488goto out;489}490491/* get flags to check for trailer */492trailer_present = hdr->flags & ATH10K_HTC_FLAG_TRAILER_PRESENT;493if (trailer_present) {494u8 *trailer;495496trailer_len = hdr->trailer_len;497min_len = sizeof(struct ath10k_ath10k_htc_record_hdr);498499if ((trailer_len < min_len) ||500(trailer_len > payload_len)) {501ath10k_warn(ar, "Invalid trailer length: %d\n",502trailer_len);503goto out;504}505506trailer = (u8 *)hdr;507trailer += sizeof(*hdr);508trailer += payload_len;509trailer -= trailer_len;510status = ath10k_htc_process_trailer(htc, trailer,511trailer_len, hdr->eid,512NULL, NULL);513if (status)514goto out;515516skb_trim(skb, skb->len - trailer_len);517}518519if (((int)payload_len - (int)trailer_len) <= 0)520/* zero length packet with trailer data, just drop these */521goto out;522523ath10k_dbg(ar, ATH10K_DBG_HTC, "htc rx completion ep %d skb %p\n",524eid, skb);525ep->ep_ops.ep_rx_complete(ar, skb);526527/* skb is now owned by the rx completion handler */528skb = NULL;529out:530kfree_skb(skb);531}532EXPORT_SYMBOL(ath10k_htc_rx_completion_handler);533534static void ath10k_htc_control_rx_complete(struct ath10k *ar,535struct sk_buff *skb)536{537struct ath10k_htc *htc = &ar->htc;538struct ath10k_htc_msg *msg = (struct ath10k_htc_msg *)skb->data;539540switch (__le16_to_cpu(msg->hdr.message_id)) {541case ATH10K_HTC_MSG_READY_ID:542case ATH10K_HTC_MSG_CONNECT_SERVICE_RESP_ID:543/* handle HTC control message */544if (completion_done(&htc->ctl_resp)) {545/* this is a fatal error, target should not be546* sending unsolicited messages on the ep 0547*/548ath10k_warn(ar, "HTC rx ctrl still processing\n");549complete(&htc->ctl_resp);550goto out;551}552553htc->control_resp_len =554min_t(int, skb->len,555ATH10K_HTC_MAX_CTRL_MSG_LEN);556557memcpy(htc->control_resp_buffer, skb->data,558htc->control_resp_len);559560complete(&htc->ctl_resp);561break;562case ATH10K_HTC_MSG_SEND_SUSPEND_COMPLETE:563htc->htc_ops.target_send_suspend_complete(ar);564break;565default:566ath10k_warn(ar, "ignoring unsolicited htc ep0 event\n");567break;568}569570out:571kfree_skb(skb);572}573574/***************/575/* Init/Deinit */576/***************/577578static const char *htc_service_name(enum ath10k_htc_svc_id id)579{580switch (id) {581case ATH10K_HTC_SVC_ID_RESERVED:582return "Reserved";583case ATH10K_HTC_SVC_ID_RSVD_CTRL:584return "Control";585case ATH10K_HTC_SVC_ID_WMI_CONTROL:586return "WMI";587case ATH10K_HTC_SVC_ID_WMI_DATA_BE:588return "DATA BE";589case ATH10K_HTC_SVC_ID_WMI_DATA_BK:590return "DATA BK";591case ATH10K_HTC_SVC_ID_WMI_DATA_VI:592return "DATA VI";593case ATH10K_HTC_SVC_ID_WMI_DATA_VO:594return "DATA VO";595case ATH10K_HTC_SVC_ID_NMI_CONTROL:596return "NMI Control";597case ATH10K_HTC_SVC_ID_NMI_DATA:598return "NMI Data";599case ATH10K_HTC_SVC_ID_HTT_DATA_MSG:600return "HTT Data";601case ATH10K_HTC_SVC_ID_HTT_DATA2_MSG:602return "HTT Data";603case ATH10K_HTC_SVC_ID_HTT_DATA3_MSG:604return "HTT Data";605case ATH10K_HTC_SVC_ID_TEST_RAW_STREAMS:606return "RAW";607case ATH10K_HTC_SVC_ID_HTT_LOG_MSG:608return "PKTLOG";609}610611return "Unknown";612}613614static void ath10k_htc_reset_endpoint_states(struct ath10k_htc *htc)615{616struct ath10k_htc_ep *ep;617int i;618619for (i = ATH10K_HTC_EP_0; i < ATH10K_HTC_EP_COUNT; i++) {620ep = &htc->endpoint[i];621ep->service_id = ATH10K_HTC_SVC_ID_UNUSED;622ep->max_ep_message_len = 0;623ep->max_tx_queue_depth = 0;624ep->eid = i;625ep->htc = htc;626ep->tx_credit_flow_enabled = true;627}628}629630static u8 ath10k_htc_get_credit_allocation(struct ath10k_htc *htc,631u16 service_id)632{633u8 allocation = 0;634635/* The WMI control service is the only service with flow control.636* Let it have all transmit credits.637*/638if (service_id == ATH10K_HTC_SVC_ID_WMI_CONTROL)639allocation = htc->total_transmit_credits;640641return allocation;642}643644static int ath10k_htc_send_bundle(struct ath10k_htc_ep *ep,645struct sk_buff *bundle_skb,646struct sk_buff_head *tx_save_head)647{648struct ath10k_hif_sg_item sg_item;649struct ath10k_htc *htc = ep->htc;650struct ath10k *ar = htc->ar;651struct sk_buff *skb;652int ret, cn = 0;653unsigned int skb_len;654655ath10k_dbg(ar, ATH10K_DBG_HTC, "bundle skb len %d\n", bundle_skb->len);656skb_len = bundle_skb->len;657ret = ath10k_htc_consume_credit(ep, skb_len, true);658659if (!ret) {660sg_item.transfer_id = ep->eid;661sg_item.transfer_context = bundle_skb;662sg_item.vaddr = bundle_skb->data;663sg_item.len = bundle_skb->len;664665ret = ath10k_hif_tx_sg(htc->ar, ep->ul_pipe_id, &sg_item, 1);666if (ret)667ath10k_htc_release_credit(ep, skb_len);668}669670if (ret)671dev_kfree_skb_any(bundle_skb);672673for (cn = 0; (skb = skb_dequeue_tail(tx_save_head)); cn++) {674if (ret) {675skb_pull(skb, sizeof(struct ath10k_htc_hdr));676skb_queue_head(&ep->tx_req_head, skb);677} else {678skb_queue_tail(&ep->tx_complete_head, skb);679}680}681682if (!ret)683queue_work(ar->workqueue_tx_complete, &ar->tx_complete_work);684685ath10k_dbg(ar, ATH10K_DBG_HTC,686"bundle tx status %d eid %d req count %d count %d len %d\n",687ret, ep->eid, skb_queue_len(&ep->tx_req_head), cn, skb_len);688return ret;689}690691static void ath10k_htc_send_one_skb(struct ath10k_htc_ep *ep, struct sk_buff *skb)692{693struct ath10k_htc *htc = ep->htc;694struct ath10k *ar = htc->ar;695int ret;696697ret = ath10k_htc_send(htc, ep->eid, skb);698699if (ret)700skb_queue_head(&ep->tx_req_head, skb);701702ath10k_dbg(ar, ATH10K_DBG_HTC, "tx one status %d eid %d len %d pending count %d\n",703ret, ep->eid, skb->len, skb_queue_len(&ep->tx_req_head));704}705706static int ath10k_htc_send_bundle_skbs(struct ath10k_htc_ep *ep)707{708struct ath10k_htc *htc = ep->htc;709struct sk_buff *bundle_skb, *skb;710struct sk_buff_head tx_save_head;711struct ath10k_htc_hdr *hdr;712u8 *bundle_buf;713int ret = 0, credit_pad, credit_remainder, trans_len, bundles_left = 0;714715if (htc->ar->state == ATH10K_STATE_WEDGED)716return -ECOMM;717718if (ep->tx_credit_flow_enabled &&719ep->tx_credits < ATH10K_MIN_CREDIT_PER_HTC_TX_BUNDLE)720return 0;721722bundles_left = ATH10K_MAX_MSG_PER_HTC_TX_BUNDLE * ep->tx_credit_size;723bundle_skb = dev_alloc_skb(bundles_left);724725if (!bundle_skb)726return -ENOMEM;727728bundle_buf = bundle_skb->data;729skb_queue_head_init(&tx_save_head);730731while (true) {732skb = skb_dequeue(&ep->tx_req_head);733if (!skb)734break;735736credit_pad = 0;737trans_len = skb->len + sizeof(*hdr);738credit_remainder = trans_len % ep->tx_credit_size;739740if (credit_remainder != 0) {741credit_pad = ep->tx_credit_size - credit_remainder;742trans_len += credit_pad;743}744745ret = ath10k_htc_consume_credit(ep,746bundle_buf + trans_len - bundle_skb->data,747false);748if (ret) {749skb_queue_head(&ep->tx_req_head, skb);750break;751}752753if (bundles_left < trans_len) {754bundle_skb->len = bundle_buf - bundle_skb->data;755ret = ath10k_htc_send_bundle(ep, bundle_skb, &tx_save_head);756757if (ret) {758skb_queue_head(&ep->tx_req_head, skb);759return ret;760}761762if (skb_queue_len(&ep->tx_req_head) == 0) {763ath10k_htc_send_one_skb(ep, skb);764return ret;765}766767if (ep->tx_credit_flow_enabled &&768ep->tx_credits < ATH10K_MIN_CREDIT_PER_HTC_TX_BUNDLE) {769skb_queue_head(&ep->tx_req_head, skb);770return 0;771}772773bundles_left =774ATH10K_MAX_MSG_PER_HTC_TX_BUNDLE * ep->tx_credit_size;775bundle_skb = dev_alloc_skb(bundles_left);776777if (!bundle_skb) {778skb_queue_head(&ep->tx_req_head, skb);779return -ENOMEM;780}781bundle_buf = bundle_skb->data;782skb_queue_head_init(&tx_save_head);783}784785skb_push(skb, sizeof(struct ath10k_htc_hdr));786ath10k_htc_prepare_tx_skb(ep, skb);787788memcpy(bundle_buf, skb->data, skb->len);789hdr = (struct ath10k_htc_hdr *)bundle_buf;790hdr->flags |= ATH10K_HTC_FLAG_SEND_BUNDLE;791hdr->pad_len = __cpu_to_le16(credit_pad);792bundle_buf += trans_len;793bundles_left -= trans_len;794skb_queue_tail(&tx_save_head, skb);795}796797if (bundle_buf != bundle_skb->data) {798bundle_skb->len = bundle_buf - bundle_skb->data;799ret = ath10k_htc_send_bundle(ep, bundle_skb, &tx_save_head);800} else {801dev_kfree_skb_any(bundle_skb);802}803804return ret;805}806807static void ath10k_htc_bundle_tx_work(struct work_struct *work)808{809struct ath10k *ar = container_of(work, struct ath10k, bundle_tx_work);810struct ath10k_htc_ep *ep;811struct sk_buff *skb;812int i;813814for (i = 0; i < ARRAY_SIZE(ar->htc.endpoint); i++) {815ep = &ar->htc.endpoint[i];816817if (!ep->bundle_tx)818continue;819820ath10k_dbg(ar, ATH10K_DBG_HTC, "bundle tx work eid %d count %d\n",821ep->eid, skb_queue_len(&ep->tx_req_head));822823if (skb_queue_len(&ep->tx_req_head) >=824ATH10K_MIN_MSG_PER_HTC_TX_BUNDLE) {825ath10k_htc_send_bundle_skbs(ep);826} else {827skb = skb_dequeue(&ep->tx_req_head);828829if (!skb)830continue;831ath10k_htc_send_one_skb(ep, skb);832}833}834}835836static void ath10k_htc_tx_complete_work(struct work_struct *work)837{838struct ath10k *ar = container_of(work, struct ath10k, tx_complete_work);839struct ath10k_htc_ep *ep;840enum ath10k_htc_ep_id eid;841struct sk_buff *skb;842int i;843844for (i = 0; i < ARRAY_SIZE(ar->htc.endpoint); i++) {845ep = &ar->htc.endpoint[i];846eid = ep->eid;847if (ep->bundle_tx && eid == ar->htt.eid) {848ath10k_dbg(ar, ATH10K_DBG_HTC, "bundle tx complete eid %d pending complete count%d\n",849ep->eid, skb_queue_len(&ep->tx_complete_head));850851while (true) {852skb = skb_dequeue(&ep->tx_complete_head);853if (!skb)854break;855ath10k_htc_notify_tx_completion(ep, skb);856}857}858}859}860861int ath10k_htc_send_hl(struct ath10k_htc *htc,862enum ath10k_htc_ep_id eid,863struct sk_buff *skb)864{865struct ath10k_htc_ep *ep = &htc->endpoint[eid];866struct ath10k *ar = htc->ar;867868if (sizeof(struct ath10k_htc_hdr) + skb->len > ep->tx_credit_size) {869ath10k_dbg(ar, ATH10K_DBG_HTC, "tx exceed max len %d\n", skb->len);870return -ENOMEM;871}872873ath10k_dbg(ar, ATH10K_DBG_HTC, "htc send hl eid %d bundle %d tx count %d len %d\n",874eid, ep->bundle_tx, skb_queue_len(&ep->tx_req_head), skb->len);875876if (ep->bundle_tx) {877skb_queue_tail(&ep->tx_req_head, skb);878queue_work(ar->workqueue, &ar->bundle_tx_work);879return 0;880} else {881return ath10k_htc_send(htc, eid, skb);882}883}884885void ath10k_htc_setup_tx_req(struct ath10k_htc_ep *ep)886{887if (ep->htc->max_msgs_per_htc_bundle >= ATH10K_MIN_MSG_PER_HTC_TX_BUNDLE &&888!ep->bundle_tx) {889ep->bundle_tx = true;890skb_queue_head_init(&ep->tx_req_head);891skb_queue_head_init(&ep->tx_complete_head);892}893}894895void ath10k_htc_stop_hl(struct ath10k *ar)896{897struct ath10k_htc_ep *ep;898int i;899900cancel_work_sync(&ar->bundle_tx_work);901cancel_work_sync(&ar->tx_complete_work);902903for (i = 0; i < ARRAY_SIZE(ar->htc.endpoint); i++) {904ep = &ar->htc.endpoint[i];905906if (!ep->bundle_tx)907continue;908909ath10k_dbg(ar, ATH10K_DBG_HTC, "stop tx work eid %d count %d\n",910ep->eid, skb_queue_len(&ep->tx_req_head));911912skb_queue_purge(&ep->tx_req_head);913}914}915916int ath10k_htc_wait_target(struct ath10k_htc *htc)917{918struct ath10k *ar = htc->ar;919int i, status = 0;920unsigned long time_left;921struct ath10k_htc_msg *msg;922u16 message_id;923924time_left = wait_for_completion_timeout(&htc->ctl_resp,925ATH10K_HTC_WAIT_TIMEOUT_HZ);926if (!time_left) {927/* Workaround: In some cases the PCI HIF doesn't928* receive interrupt for the control response message929* even if the buffer was completed. It is suspected930* iomap writes unmasking PCI CE irqs aren't propagated931* properly in KVM PCI-passthrough sometimes.932*/933ath10k_warn(ar, "failed to receive control response completion, polling..\n");934935for (i = 0; i < CE_COUNT; i++)936ath10k_hif_send_complete_check(htc->ar, i, 1);937938time_left =939wait_for_completion_timeout(&htc->ctl_resp,940ATH10K_HTC_WAIT_TIMEOUT_HZ);941942if (!time_left)943status = -ETIMEDOUT;944}945946if (status < 0) {947ath10k_err(ar, "ctl_resp never came in (%d)\n", status);948return status;949}950951if (htc->control_resp_len < sizeof(msg->hdr) + sizeof(msg->ready)) {952ath10k_err(ar, "Invalid HTC ready msg len:%d\n",953htc->control_resp_len);954return -ECOMM;955}956957msg = (struct ath10k_htc_msg *)htc->control_resp_buffer;958message_id = __le16_to_cpu(msg->hdr.message_id);959960if (message_id != ATH10K_HTC_MSG_READY_ID) {961ath10k_err(ar, "Invalid HTC ready msg: 0x%x\n", message_id);962return -ECOMM;963}964965if (ar->hw_params.use_fw_tx_credits)966htc->total_transmit_credits = __le16_to_cpu(msg->ready.credit_count);967else968htc->total_transmit_credits = 1;969970htc->target_credit_size = __le16_to_cpu(msg->ready.credit_size);971972ath10k_dbg(ar, ATH10K_DBG_HTC,973"Target ready! transmit resources: %d size:%d actual credits:%d\n",974htc->total_transmit_credits,975htc->target_credit_size,976msg->ready.credit_count);977978if ((htc->total_transmit_credits == 0) ||979(htc->target_credit_size == 0)) {980ath10k_err(ar, "Invalid credit size received\n");981return -ECOMM;982}983984/* The only way to determine if the ready message is an extended985* message is from the size.986*/987if (htc->control_resp_len >=988sizeof(msg->hdr) + sizeof(msg->ready_ext)) {989htc->alt_data_credit_size =990__le16_to_cpu(msg->ready_ext.reserved) &991ATH10K_HTC_MSG_READY_EXT_ALT_DATA_MASK;992htc->max_msgs_per_htc_bundle =993min_t(u8, msg->ready_ext.max_msgs_per_htc_bundle,994HTC_HOST_MAX_MSG_PER_RX_BUNDLE);995ath10k_dbg(ar, ATH10K_DBG_HTC,996"Extended ready message RX bundle size %d alt size %d\n",997htc->max_msgs_per_htc_bundle,998htc->alt_data_credit_size);999}10001001INIT_WORK(&ar->bundle_tx_work, ath10k_htc_bundle_tx_work);1002INIT_WORK(&ar->tx_complete_work, ath10k_htc_tx_complete_work);10031004return 0;1005}10061007void ath10k_htc_change_tx_credit_flow(struct ath10k_htc *htc,1008enum ath10k_htc_ep_id eid,1009bool enable)1010{1011struct ath10k *ar = htc->ar;1012struct ath10k_htc_ep *ep = &ar->htc.endpoint[eid];10131014ep->tx_credit_flow_enabled = enable;1015}10161017int ath10k_htc_connect_service(struct ath10k_htc *htc,1018struct ath10k_htc_svc_conn_req *conn_req,1019struct ath10k_htc_svc_conn_resp *conn_resp)1020{1021struct ath10k *ar = htc->ar;1022struct ath10k_htc_msg *msg;1023struct ath10k_htc_conn_svc *req_msg;1024struct ath10k_htc_conn_svc_response resp_msg_dummy;1025struct ath10k_htc_conn_svc_response *resp_msg = &resp_msg_dummy;1026enum ath10k_htc_ep_id assigned_eid = ATH10K_HTC_EP_COUNT;1027struct ath10k_htc_ep *ep;1028struct sk_buff *skb;1029unsigned int max_msg_size = 0;1030int length, status;1031unsigned long time_left;1032bool disable_credit_flow_ctrl = false;1033u16 message_id, service_id, flags = 0;1034u8 tx_alloc = 0;10351036/* special case for HTC pseudo control service */1037if (conn_req->service_id == ATH10K_HTC_SVC_ID_RSVD_CTRL) {1038disable_credit_flow_ctrl = true;1039assigned_eid = ATH10K_HTC_EP_0;1040max_msg_size = ATH10K_HTC_MAX_CTRL_MSG_LEN;1041memset(&resp_msg_dummy, 0, sizeof(resp_msg_dummy));1042goto setup;1043}10441045tx_alloc = ath10k_htc_get_credit_allocation(htc,1046conn_req->service_id);1047if (!tx_alloc)1048ath10k_dbg(ar, ATH10K_DBG_BOOT,1049"boot htc service %s does not allocate target credits\n",1050htc_service_name(conn_req->service_id));10511052skb = ath10k_htc_build_tx_ctrl_skb(htc->ar);1053if (!skb) {1054ath10k_err(ar, "Failed to allocate HTC packet\n");1055return -ENOMEM;1056}10571058length = sizeof(msg->hdr) + sizeof(msg->connect_service);1059skb_put(skb, length);1060memset(skb->data, 0, length);10611062msg = (struct ath10k_htc_msg *)skb->data;1063msg->hdr.message_id =1064__cpu_to_le16(ATH10K_HTC_MSG_CONNECT_SERVICE_ID);10651066flags |= SM(tx_alloc, ATH10K_HTC_CONN_FLAGS_RECV_ALLOC);10671068/* Only enable credit flow control for WMI ctrl service */1069if (conn_req->service_id != ATH10K_HTC_SVC_ID_WMI_CONTROL) {1070flags |= ATH10K_HTC_CONN_FLAGS_DISABLE_CREDIT_FLOW_CTRL;1071disable_credit_flow_ctrl = true;1072}10731074req_msg = &msg->connect_service;1075req_msg->flags = __cpu_to_le16(flags);1076req_msg->service_id = __cpu_to_le16(conn_req->service_id);10771078reinit_completion(&htc->ctl_resp);10791080status = ath10k_htc_send(htc, ATH10K_HTC_EP_0, skb);1081if (status) {1082kfree_skb(skb);1083return status;1084}10851086/* wait for response */1087time_left = wait_for_completion_timeout(&htc->ctl_resp,1088ATH10K_HTC_CONN_SVC_TIMEOUT_HZ);1089if (!time_left) {1090ath10k_err(ar, "Service connect timeout\n");1091return -ETIMEDOUT;1092}10931094/* we controlled the buffer creation, it's aligned */1095msg = (struct ath10k_htc_msg *)htc->control_resp_buffer;1096resp_msg = &msg->connect_service_response;1097message_id = __le16_to_cpu(msg->hdr.message_id);1098service_id = __le16_to_cpu(resp_msg->service_id);10991100if ((message_id != ATH10K_HTC_MSG_CONNECT_SERVICE_RESP_ID) ||1101(htc->control_resp_len < sizeof(msg->hdr) +1102sizeof(msg->connect_service_response))) {1103ath10k_err(ar, "Invalid resp message ID 0x%x", message_id);1104return -EPROTO;1105}11061107ath10k_dbg(ar, ATH10K_DBG_HTC,1108"HTC Service %s connect response: status: 0x%x, assigned ep: 0x%x\n",1109htc_service_name(service_id),1110resp_msg->status, resp_msg->eid);11111112conn_resp->connect_resp_code = resp_msg->status;11131114/* check response status */1115if (resp_msg->status != ATH10K_HTC_CONN_SVC_STATUS_SUCCESS) {1116ath10k_err(ar, "HTC Service %s connect request failed: 0x%x)\n",1117htc_service_name(service_id),1118resp_msg->status);1119return -EPROTO;1120}11211122assigned_eid = (enum ath10k_htc_ep_id)resp_msg->eid;1123max_msg_size = __le16_to_cpu(resp_msg->max_msg_size);11241125setup:11261127if (assigned_eid >= ATH10K_HTC_EP_COUNT)1128return -EPROTO;11291130if (max_msg_size == 0)1131return -EPROTO;11321133ep = &htc->endpoint[assigned_eid];1134ep->eid = assigned_eid;11351136if (ep->service_id != ATH10K_HTC_SVC_ID_UNUSED)1137return -EPROTO;11381139/* return assigned endpoint to caller */1140conn_resp->eid = assigned_eid;1141conn_resp->max_msg_len = __le16_to_cpu(resp_msg->max_msg_size);11421143/* setup the endpoint */1144ep->service_id = conn_req->service_id;1145ep->max_tx_queue_depth = conn_req->max_send_queue_depth;1146ep->max_ep_message_len = __le16_to_cpu(resp_msg->max_msg_size);1147ep->tx_credits = tx_alloc;1148ep->tx_credit_size = htc->target_credit_size;11491150if (conn_req->service_id == ATH10K_HTC_SVC_ID_HTT_DATA_MSG &&1151htc->alt_data_credit_size != 0)1152ep->tx_credit_size = htc->alt_data_credit_size;11531154/* copy all the callbacks */1155ep->ep_ops = conn_req->ep_ops;11561157status = ath10k_hif_map_service_to_pipe(htc->ar,1158ep->service_id,1159&ep->ul_pipe_id,1160&ep->dl_pipe_id);1161if (status) {1162ath10k_dbg(ar, ATH10K_DBG_BOOT, "unsupported HTC service id: %d\n",1163ep->service_id);1164return status;1165}11661167ath10k_dbg(ar, ATH10K_DBG_BOOT,1168"boot htc service '%s' ul pipe %d dl pipe %d eid %d ready\n",1169htc_service_name(ep->service_id), ep->ul_pipe_id,1170ep->dl_pipe_id, ep->eid);11711172if (disable_credit_flow_ctrl && ep->tx_credit_flow_enabled) {1173ep->tx_credit_flow_enabled = false;1174ath10k_dbg(ar, ATH10K_DBG_BOOT,1175"boot htc service '%s' eid %d TX flow control disabled\n",1176htc_service_name(ep->service_id), assigned_eid);1177}11781179return status;1180}11811182struct sk_buff *ath10k_htc_alloc_skb(struct ath10k *ar, int size)1183{1184struct sk_buff *skb;11851186skb = dev_alloc_skb(size + sizeof(struct ath10k_htc_hdr));1187if (!skb)1188return NULL;11891190skb_reserve(skb, sizeof(struct ath10k_htc_hdr));11911192/* FW/HTC requires 4-byte aligned streams */1193if (!IS_ALIGNED((unsigned long)skb->data, 4))1194ath10k_warn(ar, "Unaligned HTC tx skb\n");11951196return skb;1197}11981199static void ath10k_htc_pktlog_process_rx(struct ath10k *ar, struct sk_buff *skb)1200{1201trace_ath10k_htt_pktlog(ar, skb->data, skb->len);1202dev_kfree_skb_any(skb);1203}12041205static int ath10k_htc_pktlog_connect(struct ath10k *ar)1206{1207struct ath10k_htc_svc_conn_resp conn_resp;1208struct ath10k_htc_svc_conn_req conn_req;1209int status;12101211memset(&conn_req, 0, sizeof(conn_req));1212memset(&conn_resp, 0, sizeof(conn_resp));12131214conn_req.ep_ops.ep_tx_complete = NULL;1215conn_req.ep_ops.ep_rx_complete = ath10k_htc_pktlog_process_rx;1216conn_req.ep_ops.ep_tx_credits = NULL;12171218/* connect to control service */1219conn_req.service_id = ATH10K_HTC_SVC_ID_HTT_LOG_MSG;1220status = ath10k_htc_connect_service(&ar->htc, &conn_req, &conn_resp);1221if (status) {1222ath10k_warn(ar, "failed to connect to PKTLOG service: %d\n",1223status);1224return status;1225}12261227return 0;1228}12291230static bool ath10k_htc_pktlog_svc_supported(struct ath10k *ar)1231{1232u8 ul_pipe_id;1233u8 dl_pipe_id;1234int status;12351236status = ath10k_hif_map_service_to_pipe(ar, ATH10K_HTC_SVC_ID_HTT_LOG_MSG,1237&ul_pipe_id,1238&dl_pipe_id);1239if (status) {1240ath10k_dbg(ar, ATH10K_DBG_BOOT, "unsupported HTC pktlog service id: %d\n",1241ATH10K_HTC_SVC_ID_HTT_LOG_MSG);12421243return false;1244}12451246return true;1247}12481249int ath10k_htc_start(struct ath10k_htc *htc)1250{1251struct ath10k *ar = htc->ar;1252struct sk_buff *skb;1253int status = 0;1254struct ath10k_htc_msg *msg;12551256skb = ath10k_htc_build_tx_ctrl_skb(htc->ar);1257if (!skb)1258return -ENOMEM;12591260skb_put(skb, sizeof(msg->hdr) + sizeof(msg->setup_complete_ext));1261memset(skb->data, 0, skb->len);12621263msg = (struct ath10k_htc_msg *)skb->data;1264msg->hdr.message_id =1265__cpu_to_le16(ATH10K_HTC_MSG_SETUP_COMPLETE_EX_ID);12661267if (ar->hif.bus == ATH10K_BUS_SDIO) {1268/* Extra setup params used by SDIO */1269msg->setup_complete_ext.flags =1270__cpu_to_le32(ATH10K_HTC_SETUP_COMPLETE_FLAGS_RX_BNDL_EN);1271msg->setup_complete_ext.max_msgs_per_bundled_recv =1272htc->max_msgs_per_htc_bundle;1273}1274ath10k_dbg(ar, ATH10K_DBG_HTC, "HTC is using TX credit flow control\n");12751276status = ath10k_htc_send(htc, ATH10K_HTC_EP_0, skb);1277if (status) {1278kfree_skb(skb);1279return status;1280}12811282if (ath10k_htc_pktlog_svc_supported(ar)) {1283status = ath10k_htc_pktlog_connect(ar);1284if (status) {1285ath10k_err(ar, "failed to connect to pktlog: %d\n", status);1286return status;1287}1288}12891290return 0;1291}12921293/* registered target arrival callback from the HIF layer */1294int ath10k_htc_init(struct ath10k *ar)1295{1296int status;1297struct ath10k_htc *htc = &ar->htc;1298struct ath10k_htc_svc_conn_req conn_req;1299struct ath10k_htc_svc_conn_resp conn_resp;13001301spin_lock_init(&htc->tx_lock);13021303ath10k_htc_reset_endpoint_states(htc);13041305htc->ar = ar;13061307/* setup our pseudo HTC control endpoint connection */1308memset(&conn_req, 0, sizeof(conn_req));1309memset(&conn_resp, 0, sizeof(conn_resp));1310conn_req.ep_ops.ep_tx_complete = ath10k_htc_control_tx_complete;1311conn_req.ep_ops.ep_rx_complete = ath10k_htc_control_rx_complete;1312conn_req.max_send_queue_depth = ATH10K_NUM_CONTROL_TX_BUFFERS;1313conn_req.service_id = ATH10K_HTC_SVC_ID_RSVD_CTRL;13141315/* connect fake service */1316status = ath10k_htc_connect_service(htc, &conn_req, &conn_resp);1317if (status) {1318ath10k_err(ar, "could not connect to htc service (%d)\n",1319status);1320return status;1321}13221323init_completion(&htc->ctl_resp);13241325return 0;1326}132713281329