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