1 /* 2 * Copyright (c) 2007-2011 Atheros Communications Inc. 3 * 4 * Permission to use, copy, modify, and/or distribute this software for any 5 * purpose with or without fee is hereby granted, provided that the above 6 * copyright notice and this permission notice appear in all copies. 7 * 8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 15 */ 16 17 #include "core.h" 18 #include "debug.h" 19 #include "hif-ops.h" 20 21 #define HTC_PACKET_CONTAINER_ALLOCATION 32 22 #define HTC_CONTROL_BUFFER_SIZE (HTC_MAX_CTRL_MSG_LEN + HTC_HDR_LENGTH) 23 24 static int ath6kl_htc_pipe_tx(struct htc_target *handle, 25 struct htc_packet *packet); 26 static void ath6kl_htc_pipe_cleanup(struct htc_target *handle); 27 28 /* htc pipe tx path */ 29 static inline void restore_tx_packet(struct htc_packet *packet) 30 { 31 if (packet->info.tx.flags & HTC_FLAGS_TX_FIXUP_NETBUF) { 32 skb_pull(packet->skb, sizeof(struct htc_frame_hdr)); 33 packet->info.tx.flags &= ~HTC_FLAGS_TX_FIXUP_NETBUF; 34 } 35 } 36 37 static void do_send_completion(struct htc_endpoint *ep, 38 struct list_head *queue_to_indicate) 39 { 40 struct htc_packet *packet; 41 42 if (list_empty(queue_to_indicate)) { 43 /* nothing to indicate */ 44 return; 45 } 46 47 if (ep->ep_cb.tx_comp_multi != NULL) { 48 ath6kl_dbg(ATH6KL_DBG_HTC, 49 "%s: calling ep %d, send complete multiple callback (%d pkts)\n", 50 __func__, ep->eid, 51 get_queue_depth(queue_to_indicate)); 52 /* 53 * a multiple send complete handler is being used, 54 * pass the queue to the handler 55 */ 56 ep->ep_cb.tx_comp_multi(ep->target, queue_to_indicate); 57 /* 58 * all packets are now owned by the callback, 59 * reset queue to be safe 60 */ 61 INIT_LIST_HEAD(queue_to_indicate); 62 } else { 63 /* using legacy EpTxComplete */ 64 do { 65 packet = list_first_entry(queue_to_indicate, 66 struct htc_packet, list); 67 68 list_del(&packet->list); 69 ath6kl_dbg(ATH6KL_DBG_HTC, 70 "%s: calling ep %d send complete callback on packet 0x%p\n", 71 __func__, ep->eid, packet); 72 ep->ep_cb.tx_complete(ep->target, packet); 73 } while (!list_empty(queue_to_indicate)); 74 } 75 } 76 77 static void send_packet_completion(struct htc_target *target, 78 struct htc_packet *packet) 79 { 80 struct htc_endpoint *ep = &target->endpoint[packet->endpoint]; 81 struct list_head container; 82 83 restore_tx_packet(packet); 84 INIT_LIST_HEAD(&container); 85 list_add_tail(&packet->list, &container); 86 87 /* do completion */ 88 do_send_completion(ep, &container); 89 } 90 91 static void get_htc_packet_credit_based(struct htc_target *target, 92 struct htc_endpoint *ep, 93 struct list_head *queue) 94 { 95 int credits_required; 96 int remainder; 97 u8 send_flags; 98 struct htc_packet *packet; 99 unsigned int transfer_len; 100 101 /* NOTE : the TX lock is held when this function is called */ 102 103 /* loop until we can grab as many packets out of the queue as we can */ 104 while (true) { 105 send_flags = 0; 106 if (list_empty(&ep->txq)) 107 break; 108 109 /* get packet at head, but don't remove it */ 110 packet = list_first_entry(&ep->txq, struct htc_packet, list); 111 112 ath6kl_dbg(ATH6KL_DBG_HTC, 113 "%s: got head packet:0x%p , queue depth: %d\n", 114 __func__, packet, get_queue_depth(&ep->txq)); 115 116 transfer_len = packet->act_len + HTC_HDR_LENGTH; 117 118 if (transfer_len <= target->tgt_cred_sz) { 119 credits_required = 1; 120 } else { 121 /* figure out how many credits this message requires */ 122 credits_required = transfer_len / target->tgt_cred_sz; 123 remainder = transfer_len % target->tgt_cred_sz; 124 125 if (remainder) 126 credits_required++; 127 } 128 129 ath6kl_dbg(ATH6KL_DBG_HTC, "%s: creds required:%d got:%d\n", 130 __func__, credits_required, ep->cred_dist.credits); 131 132 if (ep->eid == ENDPOINT_0) { 133 /* 134 * endpoint 0 is special, it always has a credit and 135 * does not require credit based flow control 136 */ 137 credits_required = 0; 138 139 } else { 140 141 if (ep->cred_dist.credits < credits_required) 142 break; 143 144 ep->cred_dist.credits -= credits_required; 145 ep->ep_st.cred_cosumd += credits_required; 146 147 /* check if we need credits back from the target */ 148 if (ep->cred_dist.credits < 149 ep->cred_dist.cred_per_msg) { 150 /* tell the target we need credits ASAP! */ 151 send_flags |= HTC_FLAGS_NEED_CREDIT_UPDATE; 152 ep->ep_st.cred_low_indicate += 1; 153 ath6kl_dbg(ATH6KL_DBG_HTC, 154 "%s: host needs credits\n", 155 __func__); 156 } 157 } 158 159 /* now we can fully dequeue */ 160 packet = list_first_entry(&ep->txq, struct htc_packet, list); 161 162 list_del(&packet->list); 163 /* save the number of credits this packet consumed */ 164 packet->info.tx.cred_used = credits_required; 165 /* save send flags */ 166 packet->info.tx.flags = send_flags; 167 packet->info.tx.seqno = ep->seqno; 168 ep->seqno++; 169 /* queue this packet into the caller's queue */ 170 list_add_tail(&packet->list, queue); 171 } 172 173 } 174 175 static void get_htc_packet(struct htc_target *target, 176 struct htc_endpoint *ep, 177 struct list_head *queue, int resources) 178 { 179 struct htc_packet *packet; 180 181 /* NOTE : the TX lock is held when this function is called */ 182 183 /* loop until we can grab as many packets out of the queue as we can */ 184 while (resources) { 185 if (list_empty(&ep->txq)) 186 break; 187 188 packet = list_first_entry(&ep->txq, struct htc_packet, list); 189 list_del(&packet->list); 190 191 ath6kl_dbg(ATH6KL_DBG_HTC, 192 "%s: got packet:0x%p , new queue depth: %d\n", 193 __func__, packet, get_queue_depth(&ep->txq)); 194 packet->info.tx.seqno = ep->seqno; 195 packet->info.tx.flags = 0; 196 packet->info.tx.cred_used = 0; 197 ep->seqno++; 198 199 /* queue this packet into the caller's queue */ 200 list_add_tail(&packet->list, queue); 201 resources--; 202 } 203 } 204 205 static int htc_issue_packets(struct htc_target *target, 206 struct htc_endpoint *ep, 207 struct list_head *pkt_queue) 208 { 209 int status = 0; 210 u16 payload_len; 211 struct sk_buff *skb; 212 struct htc_frame_hdr *htc_hdr; 213 struct htc_packet *packet; 214 215 ath6kl_dbg(ATH6KL_DBG_HTC, 216 "%s: queue: 0x%p, pkts %d\n", __func__, 217 pkt_queue, get_queue_depth(pkt_queue)); 218 219 while (!list_empty(pkt_queue)) { 220 packet = list_first_entry(pkt_queue, struct htc_packet, list); 221 list_del(&packet->list); 222 223 skb = packet->skb; 224 if (!skb) { 225 WARN_ON_ONCE(1); 226 status = -EINVAL; 227 break; 228 } 229 230 payload_len = packet->act_len; 231 232 /* setup HTC frame header */ 233 htc_hdr = (struct htc_frame_hdr *) skb_push(skb, 234 sizeof(*htc_hdr)); 235 if (!htc_hdr) { 236 WARN_ON_ONCE(1); 237 status = -EINVAL; 238 break; 239 } 240 241 packet->info.tx.flags |= HTC_FLAGS_TX_FIXUP_NETBUF; 242 243 /* Endianess? */ 244 put_unaligned((u16) payload_len, &htc_hdr->payld_len); 245 htc_hdr->flags = packet->info.tx.flags; 246 htc_hdr->eid = (u8) packet->endpoint; 247 htc_hdr->ctrl[0] = 0; 248 htc_hdr->ctrl[1] = (u8) packet->info.tx.seqno; 249 250 spin_lock_bh(&target->tx_lock); 251 252 /* store in look up queue to match completions */ 253 list_add_tail(&packet->list, &ep->pipe.tx_lookup_queue); 254 ep->ep_st.tx_issued += 1; 255 spin_unlock_bh(&target->tx_lock); 256 257 status = ath6kl_hif_pipe_send(target->dev->ar, 258 ep->pipe.pipeid_ul, NULL, skb); 259 260 if (status != 0) { 261 if (status != -ENOMEM) { 262 /* TODO: if more than 1 endpoint maps to the 263 * same PipeID, it is possible to run out of 264 * resources in the HIF layer. 265 * Don't emit the error 266 */ 267 ath6kl_dbg(ATH6KL_DBG_HTC, 268 "%s: failed status:%d\n", 269 __func__, status); 270 } 271 spin_lock_bh(&target->tx_lock); 272 list_del(&packet->list); 273 274 /* reclaim credits */ 275 ep->cred_dist.credits += packet->info.tx.cred_used; 276 spin_unlock_bh(&target->tx_lock); 277 278 /* put it back into the callers queue */ 279 list_add(&packet->list, pkt_queue); 280 break; 281 } 282 283 } 284 285 if (status != 0) { 286 while (!list_empty(pkt_queue)) { 287 if (status != -ENOMEM) { 288 ath6kl_dbg(ATH6KL_DBG_HTC, 289 "%s: failed pkt:0x%p status:%d\n", 290 __func__, packet, status); 291 } 292 293 packet = list_first_entry(pkt_queue, 294 struct htc_packet, list); 295 list_del(&packet->list); 296 packet->status = status; 297 send_packet_completion(target, packet); 298 } 299 } 300 301 return status; 302 } 303 304 static enum htc_send_queue_result htc_try_send(struct htc_target *target, 305 struct htc_endpoint *ep, 306 struct list_head *txq) 307 { 308 struct list_head send_queue; /* temp queue to hold packets */ 309 struct htc_packet *packet, *tmp_pkt; 310 struct ath6kl *ar = target->dev->ar; 311 enum htc_send_full_action action; 312 int tx_resources, overflow, txqueue_depth, i, good_pkts; 313 u8 pipeid; 314 315 ath6kl_dbg(ATH6KL_DBG_HTC, "%s: (queue:0x%p depth:%d)\n", 316 __func__, txq, 317 (txq == NULL) ? 0 : get_queue_depth(txq)); 318 319 /* init the local send queue */ 320 INIT_LIST_HEAD(&send_queue); 321 322 /* 323 * txq equals to NULL means 324 * caller didn't provide a queue, just wants us to 325 * check queues and send 326 */ 327 if (txq != NULL) { 328 if (list_empty(txq)) { 329 /* empty queue */ 330 return HTC_SEND_QUEUE_DROP; 331 } 332 333 spin_lock_bh(&target->tx_lock); 334 txqueue_depth = get_queue_depth(&ep->txq); 335 spin_unlock_bh(&target->tx_lock); 336 337 if (txqueue_depth >= ep->max_txq_depth) { 338 /* we've already overflowed */ 339 overflow = get_queue_depth(txq); 340 } else { 341 /* get how much we will overflow by */ 342 overflow = txqueue_depth; 343 overflow += get_queue_depth(txq); 344 /* get how much we will overflow the TX queue by */ 345 overflow -= ep->max_txq_depth; 346 } 347 348 /* if overflow is negative or zero, we are okay */ 349 if (overflow > 0) { 350 ath6kl_dbg(ATH6KL_DBG_HTC, 351 "%s: Endpoint %d, TX queue will overflow :%d, Tx Depth:%d, Max:%d\n", 352 __func__, ep->eid, overflow, txqueue_depth, 353 ep->max_txq_depth); 354 } 355 if ((overflow <= 0) || 356 (ep->ep_cb.tx_full == NULL)) { 357 /* 358 * all packets will fit or caller did not provide send 359 * full indication handler -- just move all of them 360 * to the local send_queue object 361 */ 362 list_splice_tail_init(txq, &send_queue); 363 } else { 364 good_pkts = get_queue_depth(txq) - overflow; 365 if (good_pkts < 0) { 366 WARN_ON_ONCE(1); 367 return HTC_SEND_QUEUE_DROP; 368 } 369 370 /* we have overflowed, and a callback is provided */ 371 /* dequeue all non-overflow packets to the sendqueue */ 372 for (i = 0; i < good_pkts; i++) { 373 /* pop off caller's queue */ 374 packet = list_first_entry(txq, 375 struct htc_packet, 376 list); 377 /* move to local queue */ 378 list_move_tail(&packet->list, &send_queue); 379 } 380 381 /* 382 * the caller's queue has all the packets that won't fit 383 * walk through the caller's queue and indicate each to 384 * the send full handler 385 */ 386 list_for_each_entry_safe(packet, tmp_pkt, 387 txq, list) { 388 389 ath6kl_dbg(ATH6KL_DBG_HTC, 390 "%s: Indicat overflowed TX pkts: %p\n", 391 __func__, packet); 392 action = ep->ep_cb.tx_full(ep->target, packet); 393 if (action == HTC_SEND_FULL_DROP) { 394 /* callback wants the packet dropped */ 395 ep->ep_st.tx_dropped += 1; 396 397 /* leave this one in the caller's queue 398 * for cleanup */ 399 } else { 400 /* callback wants to keep this packet, 401 * move from caller's queue to the send 402 * queue */ 403 list_move_tail(&packet->list, 404 &send_queue); 405 } 406 407 } 408 409 if (list_empty(&send_queue)) { 410 /* no packets made it in, caller will cleanup */ 411 return HTC_SEND_QUEUE_DROP; 412 } 413 } 414 } 415 416 if (!ep->pipe.tx_credit_flow_enabled) { 417 tx_resources = 418 ath6kl_hif_pipe_get_free_queue_number(ar, 419 ep->pipe.pipeid_ul); 420 } else { 421 tx_resources = 0; 422 } 423 424 spin_lock_bh(&target->tx_lock); 425 if (!list_empty(&send_queue)) { 426 /* transfer packets to tail */ 427 list_splice_tail_init(&send_queue, &ep->txq); 428 if (!list_empty(&send_queue)) { 429 WARN_ON_ONCE(1); 430 spin_unlock_bh(&target->tx_lock); 431 return HTC_SEND_QUEUE_DROP; 432 } 433 INIT_LIST_HEAD(&send_queue); 434 } 435 436 /* increment tx processing count on entry */ 437 ep->tx_proc_cnt++; 438 439 if (ep->tx_proc_cnt > 1) { 440 /* 441 * Another thread or task is draining the TX queues on this 442 * endpoint that thread will reset the tx processing count 443 * when the queue is drained. 444 */ 445 ep->tx_proc_cnt--; 446 spin_unlock_bh(&target->tx_lock); 447 return HTC_SEND_QUEUE_OK; 448 } 449 450 /***** beyond this point only 1 thread may enter ******/ 451 452 /* 453 * Now drain the endpoint TX queue for transmission as long as we have 454 * enough transmit resources. 455 */ 456 while (true) { 457 458 if (get_queue_depth(&ep->txq) == 0) 459 break; 460 461 if (ep->pipe.tx_credit_flow_enabled) { 462 /* 463 * Credit based mechanism provides flow control 464 * based on target transmit resource availability, 465 * we assume that the HIF layer will always have 466 * bus resources greater than target transmit 467 * resources. 468 */ 469 get_htc_packet_credit_based(target, ep, &send_queue); 470 } else { 471 /* 472 * Get all packets for this endpoint that we can 473 * for this pass. 474 */ 475 get_htc_packet(target, ep, &send_queue, tx_resources); 476 } 477 478 if (get_queue_depth(&send_queue) == 0) { 479 /* 480 * Didn't get packets due to out of resources or TX 481 * queue was drained. 482 */ 483 break; 484 } 485 486 spin_unlock_bh(&target->tx_lock); 487 488 /* send what we can */ 489 htc_issue_packets(target, ep, &send_queue); 490 491 if (!ep->pipe.tx_credit_flow_enabled) { 492 pipeid = ep->pipe.pipeid_ul; 493 tx_resources = 494 ath6kl_hif_pipe_get_free_queue_number(ar, pipeid); 495 } 496 497 spin_lock_bh(&target->tx_lock); 498 499 } 500 /* done with this endpoint, we can clear the count */ 501 ep->tx_proc_cnt = 0; 502 spin_unlock_bh(&target->tx_lock); 503 504 return HTC_SEND_QUEUE_OK; 505 } 506 507 /* htc control packet manipulation */ 508 static void destroy_htc_txctrl_packet(struct htc_packet *packet) 509 { 510 struct sk_buff *skb; 511 skb = packet->skb; 512 if (skb != NULL) 513 dev_kfree_skb(skb); 514 515 kfree(packet); 516 } 517 518 static struct htc_packet *build_htc_txctrl_packet(void) 519 { 520 struct htc_packet *packet = NULL; 521 struct sk_buff *skb; 522 523 packet = kzalloc(sizeof(struct htc_packet), GFP_KERNEL); 524 if (packet == NULL) 525 return NULL; 526 527 skb = __dev_alloc_skb(HTC_CONTROL_BUFFER_SIZE, GFP_KERNEL); 528 529 if (skb == NULL) { 530 kfree(packet); 531 return NULL; 532 } 533 packet->skb = skb; 534 535 return packet; 536 } 537 538 static void htc_free_txctrl_packet(struct htc_target *target, 539 struct htc_packet *packet) 540 { 541 destroy_htc_txctrl_packet(packet); 542 } 543 544 static struct htc_packet *htc_alloc_txctrl_packet(struct htc_target *target) 545 { 546 return build_htc_txctrl_packet(); 547 } 548 549 static void htc_txctrl_complete(struct htc_target *target, 550 struct htc_packet *packet) 551 { 552 htc_free_txctrl_packet(target, packet); 553 } 554 555 #define MAX_MESSAGE_SIZE 1536 556 557 static int htc_setup_target_buffer_assignments(struct htc_target *target) 558 { 559 int status, credits, credit_per_maxmsg, i; 560 struct htc_pipe_txcredit_alloc *entry; 561 unsigned int hif_usbaudioclass = 0; 562 563 credit_per_maxmsg = MAX_MESSAGE_SIZE / target->tgt_cred_sz; 564 if (MAX_MESSAGE_SIZE % target->tgt_cred_sz) 565 credit_per_maxmsg++; 566 567 /* TODO, this should be configured by the caller! */ 568 569 credits = target->tgt_creds; 570 entry = &target->pipe.txcredit_alloc[0]; 571 572 status = -ENOMEM; 573 574 /* FIXME: hif_usbaudioclass is always zero */ 575 if (hif_usbaudioclass) { 576 ath6kl_dbg(ATH6KL_DBG_HTC, 577 "%s: For USB Audio Class- Total:%d\n", 578 __func__, credits); 579 entry++; 580 entry++; 581 /* Setup VO Service To have Max Credits */ 582 entry->service_id = WMI_DATA_VO_SVC; 583 entry->credit_alloc = (credits - 6); 584 if (entry->credit_alloc == 0) 585 entry->credit_alloc++; 586 587 credits -= (int) entry->credit_alloc; 588 if (credits <= 0) 589 return status; 590 591 entry++; 592 entry->service_id = WMI_CONTROL_SVC; 593 entry->credit_alloc = credit_per_maxmsg; 594 credits -= (int) entry->credit_alloc; 595 if (credits <= 0) 596 return status; 597 598 /* leftovers go to best effort */ 599 entry++; 600 entry++; 601 entry->service_id = WMI_DATA_BE_SVC; 602 entry->credit_alloc = (u8) credits; 603 status = 0; 604 } else { 605 entry++; 606 entry->service_id = WMI_DATA_VI_SVC; 607 entry->credit_alloc = credits / 4; 608 if (entry->credit_alloc == 0) 609 entry->credit_alloc++; 610 611 credits -= (int) entry->credit_alloc; 612 if (credits <= 0) 613 return status; 614 615 entry++; 616 entry->service_id = WMI_DATA_VO_SVC; 617 entry->credit_alloc = credits / 4; 618 if (entry->credit_alloc == 0) 619 entry->credit_alloc++; 620 621 credits -= (int) entry->credit_alloc; 622 if (credits <= 0) 623 return status; 624 625 entry++; 626 entry->service_id = WMI_CONTROL_SVC; 627 entry->credit_alloc = credit_per_maxmsg; 628 credits -= (int) entry->credit_alloc; 629 if (credits <= 0) 630 return status; 631 632 entry++; 633 entry->service_id = WMI_DATA_BK_SVC; 634 entry->credit_alloc = credit_per_maxmsg; 635 credits -= (int) entry->credit_alloc; 636 if (credits <= 0) 637 return status; 638 639 /* leftovers go to best effort */ 640 entry++; 641 entry->service_id = WMI_DATA_BE_SVC; 642 entry->credit_alloc = (u8) credits; 643 status = 0; 644 } 645 646 if (status == 0) { 647 for (i = 0; i < ENDPOINT_MAX; i++) { 648 if (target->pipe.txcredit_alloc[i].service_id != 0) { 649 ath6kl_dbg(ATH6KL_DBG_HTC, 650 "HTC Service Index : %d TX : 0x%2.2X : alloc:%d\n", 651 i, 652 target->pipe.txcredit_alloc[i]. 653 service_id, 654 target->pipe.txcredit_alloc[i]. 655 credit_alloc); 656 } 657 } 658 } 659 return status; 660 } 661 662 /* process credit reports and call distribution function */ 663 static void htc_process_credit_report(struct htc_target *target, 664 struct htc_credit_report *rpt, 665 int num_entries, 666 enum htc_endpoint_id from_ep) 667 { 668 int total_credits = 0, i; 669 struct htc_endpoint *ep; 670 671 /* lock out TX while we update credits */ 672 spin_lock_bh(&target->tx_lock); 673 674 for (i = 0; i < num_entries; i++, rpt++) { 675 if (rpt->eid >= ENDPOINT_MAX) { 676 WARN_ON_ONCE(1); 677 spin_unlock_bh(&target->tx_lock); 678 return; 679 } 680 681 ep = &target->endpoint[rpt->eid]; 682 ep->cred_dist.credits += rpt->credits; 683 684 if (ep->cred_dist.credits && get_queue_depth(&ep->txq)) { 685 spin_unlock_bh(&target->tx_lock); 686 htc_try_send(target, ep, NULL); 687 spin_lock_bh(&target->tx_lock); 688 } 689 690 total_credits += rpt->credits; 691 } 692 ath6kl_dbg(ATH6KL_DBG_HTC, 693 "Report indicated %d credits to distribute\n", 694 total_credits); 695 696 spin_unlock_bh(&target->tx_lock); 697 } 698 699 /* flush endpoint TX queue */ 700 static void htc_flush_tx_endpoint(struct htc_target *target, 701 struct htc_endpoint *ep, u16 tag) 702 { 703 struct htc_packet *packet; 704 705 spin_lock_bh(&target->tx_lock); 706 while (get_queue_depth(&ep->txq)) { 707 packet = list_first_entry(&ep->txq, struct htc_packet, list); 708 list_del(&packet->list); 709 packet->status = 0; 710 send_packet_completion(target, packet); 711 } 712 spin_unlock_bh(&target->tx_lock); 713 } 714 715 /* 716 * In the adapted HIF layer, struct sk_buff * are passed between HIF and HTC, 717 * since upper layers expects struct htc_packet containers we use the completed 718 * skb and lookup it's corresponding HTC packet buffer from a lookup list. 719 * This is extra overhead that can be fixed by re-aligning HIF interfaces with 720 * HTC. 721 */ 722 static struct htc_packet *htc_lookup_tx_packet(struct htc_target *target, 723 struct htc_endpoint *ep, 724 struct sk_buff *skb) 725 { 726 struct htc_packet *packet, *tmp_pkt, *found_packet = NULL; 727 728 spin_lock_bh(&target->tx_lock); 729 730 /* 731 * interate from the front of tx lookup queue 732 * this lookup should be fast since lower layers completes in-order and 733 * so the completed packet should be at the head of the list generally 734 */ 735 list_for_each_entry_safe(packet, tmp_pkt, &ep->pipe.tx_lookup_queue, 736 list) { 737 /* check for removal */ 738 if (skb == packet->skb) { 739 /* found it */ 740 list_del(&packet->list); 741 found_packet = packet; 742 break; 743 } 744 } 745 746 spin_unlock_bh(&target->tx_lock); 747 748 return found_packet; 749 } 750 751 static int ath6kl_htc_pipe_tx_complete(struct ath6kl *ar, struct sk_buff *skb) 752 { 753 struct htc_target *target = ar->htc_target; 754 struct htc_frame_hdr *htc_hdr; 755 struct htc_endpoint *ep; 756 struct htc_packet *packet; 757 u8 ep_id, *netdata; 758 u32 netlen; 759 760 netdata = skb->data; 761 netlen = skb->len; 762 763 htc_hdr = (struct htc_frame_hdr *) netdata; 764 765 ep_id = htc_hdr->eid; 766 ep = &target->endpoint[ep_id]; 767 768 packet = htc_lookup_tx_packet(target, ep, skb); 769 if (packet == NULL) { 770 /* may have already been flushed and freed */ 771 ath6kl_err("HTC TX lookup failed!\n"); 772 } else { 773 /* will be giving this buffer back to upper layers */ 774 packet->status = 0; 775 send_packet_completion(target, packet); 776 } 777 skb = NULL; 778 779 if (!ep->pipe.tx_credit_flow_enabled) { 780 /* 781 * note: when using TX credit flow, the re-checking of queues 782 * happens when credits flow back from the target. in the 783 * non-TX credit case, we recheck after the packet completes 784 */ 785 htc_try_send(target, ep, NULL); 786 } 787 788 return 0; 789 } 790 791 static int htc_send_packets_multiple(struct htc_target *target, 792 struct list_head *pkt_queue) 793 { 794 struct htc_endpoint *ep; 795 struct htc_packet *packet, *tmp_pkt; 796 797 if (list_empty(pkt_queue)) 798 return -EINVAL; 799 800 /* get first packet to find out which ep the packets will go into */ 801 packet = list_first_entry(pkt_queue, struct htc_packet, list); 802 803 if (packet->endpoint >= ENDPOINT_MAX) { 804 WARN_ON_ONCE(1); 805 return -EINVAL; 806 } 807 ep = &target->endpoint[packet->endpoint]; 808 809 htc_try_send(target, ep, pkt_queue); 810 811 /* do completion on any packets that couldn't get in */ 812 if (!list_empty(pkt_queue)) { 813 list_for_each_entry_safe(packet, tmp_pkt, pkt_queue, list) { 814 packet->status = -ENOMEM; 815 } 816 817 do_send_completion(ep, pkt_queue); 818 } 819 820 return 0; 821 } 822 823 /* htc pipe rx path */ 824 static struct htc_packet *alloc_htc_packet_container(struct htc_target *target) 825 { 826 struct htc_packet *packet; 827 spin_lock_bh(&target->rx_lock); 828 829 if (target->pipe.htc_packet_pool == NULL) { 830 spin_unlock_bh(&target->rx_lock); 831 return NULL; 832 } 833 834 packet = target->pipe.htc_packet_pool; 835 target->pipe.htc_packet_pool = (struct htc_packet *) packet->list.next; 836 837 spin_unlock_bh(&target->rx_lock); 838 839 packet->list.next = NULL; 840 return packet; 841 } 842 843 static void free_htc_packet_container(struct htc_target *target, 844 struct htc_packet *packet) 845 { 846 struct list_head *lh; 847 848 spin_lock_bh(&target->rx_lock); 849 850 if (target->pipe.htc_packet_pool == NULL) { 851 target->pipe.htc_packet_pool = packet; 852 packet->list.next = NULL; 853 } else { 854 lh = (struct list_head *) target->pipe.htc_packet_pool; 855 packet->list.next = lh; 856 target->pipe.htc_packet_pool = packet; 857 } 858 859 spin_unlock_bh(&target->rx_lock); 860 } 861 862 static int htc_process_trailer(struct htc_target *target, u8 *buffer, 863 int len, enum htc_endpoint_id from_ep) 864 { 865 struct htc_credit_report *report; 866 struct htc_record_hdr *record; 867 u8 *record_buf, *orig_buf; 868 int orig_len, status; 869 870 orig_buf = buffer; 871 orig_len = len; 872 status = 0; 873 874 while (len > 0) { 875 if (len < sizeof(struct htc_record_hdr)) { 876 status = -EINVAL; 877 break; 878 } 879 880 /* these are byte aligned structs */ 881 record = (struct htc_record_hdr *) buffer; 882 len -= sizeof(struct htc_record_hdr); 883 buffer += sizeof(struct htc_record_hdr); 884 885 if (record->len > len) { 886 /* no room left in buffer for record */ 887 ath6kl_dbg(ATH6KL_DBG_HTC, 888 "invalid length: %d (id:%d) buffer has: %d bytes left\n", 889 record->len, record->rec_id, len); 890 status = -EINVAL; 891 break; 892 } 893 894 /* start of record follows the header */ 895 record_buf = buffer; 896 897 switch (record->rec_id) { 898 case HTC_RECORD_CREDITS: 899 if (record->len < sizeof(struct htc_credit_report)) { 900 WARN_ON_ONCE(1); 901 return -EINVAL; 902 } 903 904 report = (struct htc_credit_report *) record_buf; 905 htc_process_credit_report(target, report, 906 record->len / sizeof(*report), 907 from_ep); 908 break; 909 default: 910 ath6kl_dbg(ATH6KL_DBG_HTC, 911 "unhandled record: id:%d length:%d\n", 912 record->rec_id, record->len); 913 break; 914 } 915 916 if (status != 0) 917 break; 918 919 /* advance buffer past this record for next time around */ 920 buffer += record->len; 921 len -= record->len; 922 } 923 924 return status; 925 } 926 927 static void do_recv_completion(struct htc_endpoint *ep, 928 struct list_head *queue_to_indicate) 929 { 930 struct htc_packet *packet; 931 932 if (list_empty(queue_to_indicate)) { 933 /* nothing to indicate */ 934 return; 935 } 936 937 /* using legacy EpRecv */ 938 while (!list_empty(queue_to_indicate)) { 939 packet = list_first_entry(queue_to_indicate, 940 struct htc_packet, list); 941 list_del(&packet->list); 942 ep->ep_cb.rx(ep->target, packet); 943 } 944 945 return; 946 } 947 948 static void recv_packet_completion(struct htc_target *target, 949 struct htc_endpoint *ep, 950 struct htc_packet *packet) 951 { 952 struct list_head container; 953 INIT_LIST_HEAD(&container); 954 list_add_tail(&packet->list, &container); 955 956 /* do completion */ 957 do_recv_completion(ep, &container); 958 } 959 960 static int ath6kl_htc_pipe_rx_complete(struct ath6kl *ar, struct sk_buff *skb, 961 u8 pipeid) 962 { 963 struct htc_target *target = ar->htc_target; 964 u8 *netdata, *trailer, hdr_info; 965 struct htc_frame_hdr *htc_hdr; 966 u32 netlen, trailerlen = 0; 967 struct htc_packet *packet; 968 struct htc_endpoint *ep; 969 u16 payload_len; 970 int status = 0; 971 972 netdata = skb->data; 973 netlen = skb->len; 974 975 htc_hdr = (struct htc_frame_hdr *) netdata; 976 977 ep = &target->endpoint[htc_hdr->eid]; 978 979 if (htc_hdr->eid >= ENDPOINT_MAX) { 980 ath6kl_dbg(ATH6KL_DBG_HTC, 981 "HTC Rx: invalid EndpointID=%d\n", 982 htc_hdr->eid); 983 status = -EINVAL; 984 goto free_skb; 985 } 986 987 payload_len = le16_to_cpu(get_unaligned(&htc_hdr->payld_len)); 988 989 if (netlen < (payload_len + HTC_HDR_LENGTH)) { 990 ath6kl_dbg(ATH6KL_DBG_HTC, 991 "HTC Rx: insufficient length, got:%d expected =%u\n", 992 netlen, payload_len + HTC_HDR_LENGTH); 993 status = -EINVAL; 994 goto free_skb; 995 } 996 997 /* get flags to check for trailer */ 998 hdr_info = htc_hdr->flags; 999 if (hdr_info & HTC_FLG_RX_TRAILER) { 1000 /* extract the trailer length */ 1001 hdr_info = htc_hdr->ctrl[0]; 1002 if ((hdr_info < sizeof(struct htc_record_hdr)) || 1003 (hdr_info > payload_len)) { 1004 ath6kl_dbg(ATH6KL_DBG_HTC, 1005 "invalid header: payloadlen should be %d, CB[0]: %d\n", 1006 payload_len, hdr_info); 1007 status = -EINVAL; 1008 goto free_skb; 1009 } 1010 1011 trailerlen = hdr_info; 1012 /* process trailer after hdr/apps payload */ 1013 trailer = (u8 *) htc_hdr + HTC_HDR_LENGTH + 1014 payload_len - hdr_info; 1015 status = htc_process_trailer(target, trailer, hdr_info, 1016 htc_hdr->eid); 1017 if (status != 0) 1018 goto free_skb; 1019 } 1020 1021 if (((int) payload_len - (int) trailerlen) <= 0) { 1022 /* zero length packet with trailer, just drop these */ 1023 goto free_skb; 1024 } 1025 1026 if (htc_hdr->eid == ENDPOINT_0) { 1027 /* handle HTC control message */ 1028 if (target->htc_flags & HTC_OP_STATE_SETUP_COMPLETE) { 1029 /* 1030 * fatal: target should not send unsolicited 1031 * messageson the endpoint 0 1032 */ 1033 ath6kl_dbg(ATH6KL_DBG_HTC, 1034 "HTC ignores Rx Ctrl after setup complete\n"); 1035 status = -EINVAL; 1036 goto free_skb; 1037 } 1038 1039 /* remove HTC header */ 1040 skb_pull(skb, HTC_HDR_LENGTH); 1041 1042 netdata = skb->data; 1043 netlen = skb->len; 1044 1045 spin_lock_bh(&target->rx_lock); 1046 1047 target->pipe.ctrl_response_valid = true; 1048 target->pipe.ctrl_response_len = min_t(int, netlen, 1049 HTC_MAX_CTRL_MSG_LEN); 1050 memcpy(target->pipe.ctrl_response_buf, netdata, 1051 target->pipe.ctrl_response_len); 1052 1053 spin_unlock_bh(&target->rx_lock); 1054 1055 dev_kfree_skb(skb); 1056 skb = NULL; 1057 goto free_skb; 1058 } 1059 1060 /* 1061 * TODO: the message based HIF architecture allocates net bufs 1062 * for recv packets since it bridges that HIF to upper layers, 1063 * which expects HTC packets, we form the packets here 1064 */ 1065 packet = alloc_htc_packet_container(target); 1066 if (packet == NULL) { 1067 status = -ENOMEM; 1068 goto free_skb; 1069 } 1070 1071 packet->status = 0; 1072 packet->endpoint = htc_hdr->eid; 1073 packet->pkt_cntxt = skb; 1074 1075 /* TODO: for backwards compatibility */ 1076 packet->buf = skb_push(skb, 0) + HTC_HDR_LENGTH; 1077 packet->act_len = netlen - HTC_HDR_LENGTH - trailerlen; 1078 1079 /* 1080 * TODO: this is a hack because the driver layer will set the 1081 * actual len of the skb again which will just double the len 1082 */ 1083 skb_trim(skb, 0); 1084 1085 recv_packet_completion(target, ep, packet); 1086 1087 /* recover the packet container */ 1088 free_htc_packet_container(target, packet); 1089 skb = NULL; 1090 1091 free_skb: 1092 if (skb != NULL) 1093 dev_kfree_skb(skb); 1094 1095 return status; 1096 1097 } 1098 1099 static void htc_flush_rx_queue(struct htc_target *target, 1100 struct htc_endpoint *ep) 1101 { 1102 struct list_head container; 1103 struct htc_packet *packet; 1104 1105 spin_lock_bh(&target->rx_lock); 1106 1107 while (1) { 1108 if (list_empty(&ep->rx_bufq)) 1109 break; 1110 1111 packet = list_first_entry(&ep->rx_bufq, 1112 struct htc_packet, list); 1113 list_del(&packet->list); 1114 1115 spin_unlock_bh(&target->rx_lock); 1116 packet->status = -ECANCELED; 1117 packet->act_len = 0; 1118 1119 ath6kl_dbg(ATH6KL_DBG_HTC, 1120 "Flushing RX packet:0x%p, length:%d, ep:%d\n", 1121 packet, packet->buf_len, 1122 packet->endpoint); 1123 1124 INIT_LIST_HEAD(&container); 1125 list_add_tail(&packet->list, &container); 1126 1127 /* give the packet back */ 1128 do_recv_completion(ep, &container); 1129 spin_lock_bh(&target->rx_lock); 1130 } 1131 1132 spin_unlock_bh(&target->rx_lock); 1133 } 1134 1135 /* polling routine to wait for a control packet to be received */ 1136 static int htc_wait_recv_ctrl_message(struct htc_target *target) 1137 { 1138 int count = HTC_TARGET_RESPONSE_POLL_COUNT; 1139 1140 while (count > 0) { 1141 spin_lock_bh(&target->rx_lock); 1142 1143 if (target->pipe.ctrl_response_valid) { 1144 target->pipe.ctrl_response_valid = false; 1145 spin_unlock_bh(&target->rx_lock); 1146 break; 1147 } 1148 1149 spin_unlock_bh(&target->rx_lock); 1150 1151 count--; 1152 1153 msleep_interruptible(HTC_TARGET_RESPONSE_POLL_WAIT); 1154 } 1155 1156 if (count <= 0) { 1157 ath6kl_dbg(ATH6KL_DBG_HTC, "%s: Timeout!\n", __func__); 1158 return -ECOMM; 1159 } 1160 1161 return 0; 1162 } 1163 1164 static void htc_rxctrl_complete(struct htc_target *context, 1165 struct htc_packet *packet) 1166 { 1167 /* TODO, can't really receive HTC control messages yet.... */ 1168 ath6kl_dbg(ATH6KL_DBG_HTC, "%s: invalid call function\n", __func__); 1169 } 1170 1171 /* htc pipe initialization */ 1172 static void reset_endpoint_states(struct htc_target *target) 1173 { 1174 struct htc_endpoint *ep; 1175 int i; 1176 1177 for (i = ENDPOINT_0; i < ENDPOINT_MAX; i++) { 1178 ep = &target->endpoint[i]; 1179 ep->svc_id = 0; 1180 ep->len_max = 0; 1181 ep->max_txq_depth = 0; 1182 ep->eid = i; 1183 INIT_LIST_HEAD(&ep->txq); 1184 INIT_LIST_HEAD(&ep->pipe.tx_lookup_queue); 1185 INIT_LIST_HEAD(&ep->rx_bufq); 1186 ep->target = target; 1187 ep->pipe.tx_credit_flow_enabled = true; 1188 } 1189 } 1190 1191 /* start HTC, this is called after all services are connected */ 1192 static int htc_config_target_hif_pipe(struct htc_target *target) 1193 { 1194 return 0; 1195 } 1196 1197 /* htc service functions */ 1198 static u8 htc_get_credit_alloc(struct htc_target *target, u16 service_id) 1199 { 1200 u8 allocation = 0; 1201 int i; 1202 1203 for (i = 0; i < ENDPOINT_MAX; i++) { 1204 if (target->pipe.txcredit_alloc[i].service_id == service_id) 1205 allocation = 1206 target->pipe.txcredit_alloc[i].credit_alloc; 1207 } 1208 1209 if (allocation == 0) { 1210 ath6kl_dbg(ATH6KL_DBG_HTC, 1211 "HTC Service TX : 0x%2.2X : allocation is zero!\n", 1212 service_id); 1213 } 1214 1215 return allocation; 1216 } 1217 1218 static int ath6kl_htc_pipe_conn_service(struct htc_target *target, 1219 struct htc_service_connect_req *conn_req, 1220 struct htc_service_connect_resp *conn_resp) 1221 { 1222 struct ath6kl *ar = target->dev->ar; 1223 struct htc_packet *packet = NULL; 1224 struct htc_conn_service_resp *resp_msg; 1225 struct htc_conn_service_msg *conn_msg; 1226 enum htc_endpoint_id assigned_epid = ENDPOINT_MAX; 1227 bool disable_credit_flowctrl = false; 1228 unsigned int max_msg_size = 0; 1229 struct htc_endpoint *ep; 1230 int length, status = 0; 1231 struct sk_buff *skb; 1232 u8 tx_alloc; 1233 u16 flags; 1234 1235 if (conn_req->svc_id == 0) { 1236 WARN_ON_ONCE(1); 1237 status = -EINVAL; 1238 goto free_packet; 1239 } 1240 1241 if (conn_req->svc_id == HTC_CTRL_RSVD_SVC) { 1242 /* special case for pseudo control service */ 1243 assigned_epid = ENDPOINT_0; 1244 max_msg_size = HTC_MAX_CTRL_MSG_LEN; 1245 tx_alloc = 0; 1246 1247 } else { 1248 1249 tx_alloc = htc_get_credit_alloc(target, conn_req->svc_id); 1250 if (tx_alloc == 0) { 1251 status = -ENOMEM; 1252 goto free_packet; 1253 } 1254 1255 /* allocate a packet to send to the target */ 1256 packet = htc_alloc_txctrl_packet(target); 1257 1258 if (packet == NULL) { 1259 WARN_ON_ONCE(1); 1260 status = -ENOMEM; 1261 goto free_packet; 1262 } 1263 1264 skb = packet->skb; 1265 length = sizeof(struct htc_conn_service_msg); 1266 1267 /* assemble connect service message */ 1268 conn_msg = (struct htc_conn_service_msg *) skb_put(skb, 1269 length); 1270 if (conn_msg == NULL) { 1271 WARN_ON_ONCE(1); 1272 status = -EINVAL; 1273 goto free_packet; 1274 } 1275 1276 memset(conn_msg, 0, 1277 sizeof(struct htc_conn_service_msg)); 1278 conn_msg->msg_id = cpu_to_le16(HTC_MSG_CONN_SVC_ID); 1279 conn_msg->svc_id = cpu_to_le16(conn_req->svc_id); 1280 conn_msg->conn_flags = cpu_to_le16(conn_req->conn_flags & 1281 ~HTC_CONN_FLGS_SET_RECV_ALLOC_MASK); 1282 1283 /* tell target desired recv alloc for this ep */ 1284 flags = tx_alloc << HTC_CONN_FLGS_SET_RECV_ALLOC_SHIFT; 1285 conn_msg->conn_flags |= cpu_to_le16(flags); 1286 1287 if (conn_req->conn_flags & 1288 HTC_CONN_FLGS_DISABLE_CRED_FLOW_CTRL) { 1289 disable_credit_flowctrl = true; 1290 } 1291 1292 set_htc_pkt_info(packet, NULL, (u8 *) conn_msg, 1293 length, 1294 ENDPOINT_0, HTC_SERVICE_TX_PACKET_TAG); 1295 1296 status = ath6kl_htc_pipe_tx(target, packet); 1297 1298 /* we don't own it anymore */ 1299 packet = NULL; 1300 if (status != 0) 1301 goto free_packet; 1302 1303 /* wait for response */ 1304 status = htc_wait_recv_ctrl_message(target); 1305 if (status != 0) 1306 goto free_packet; 1307 1308 /* we controlled the buffer creation so it has to be 1309 * properly aligned 1310 */ 1311 resp_msg = (struct htc_conn_service_resp *) 1312 target->pipe.ctrl_response_buf; 1313 1314 if (resp_msg->msg_id != cpu_to_le16(HTC_MSG_CONN_SVC_RESP_ID) || 1315 (target->pipe.ctrl_response_len < sizeof(*resp_msg))) { 1316 /* this message is not valid */ 1317 WARN_ON_ONCE(1); 1318 status = -EINVAL; 1319 goto free_packet; 1320 } 1321 1322 ath6kl_dbg(ATH6KL_DBG_TRC, 1323 "%s: service 0x%X conn resp: status: %d ep: %d\n", 1324 __func__, resp_msg->svc_id, resp_msg->status, 1325 resp_msg->eid); 1326 1327 conn_resp->resp_code = resp_msg->status; 1328 /* check response status */ 1329 if (resp_msg->status != HTC_SERVICE_SUCCESS) { 1330 ath6kl_dbg(ATH6KL_DBG_HTC, 1331 "Target failed service 0x%X connect request (status:%d)\n", 1332 resp_msg->svc_id, resp_msg->status); 1333 status = -EINVAL; 1334 goto free_packet; 1335 } 1336 1337 assigned_epid = (enum htc_endpoint_id) resp_msg->eid; 1338 max_msg_size = le16_to_cpu(resp_msg->max_msg_sz); 1339 } 1340 1341 /* the rest are parameter checks so set the error status */ 1342 status = -EINVAL; 1343 1344 if (assigned_epid >= ENDPOINT_MAX) { 1345 WARN_ON_ONCE(1); 1346 goto free_packet; 1347 } 1348 1349 if (max_msg_size == 0) { 1350 WARN_ON_ONCE(1); 1351 goto free_packet; 1352 } 1353 1354 ep = &target->endpoint[assigned_epid]; 1355 ep->eid = assigned_epid; 1356 if (ep->svc_id != 0) { 1357 /* endpoint already in use! */ 1358 WARN_ON_ONCE(1); 1359 goto free_packet; 1360 } 1361 1362 /* return assigned endpoint to caller */ 1363 conn_resp->endpoint = assigned_epid; 1364 conn_resp->len_max = max_msg_size; 1365 1366 /* setup the endpoint */ 1367 ep->svc_id = conn_req->svc_id; /* this marks ep in use */ 1368 ep->max_txq_depth = conn_req->max_txq_depth; 1369 ep->len_max = max_msg_size; 1370 ep->cred_dist.credits = tx_alloc; 1371 ep->cred_dist.cred_sz = target->tgt_cred_sz; 1372 ep->cred_dist.cred_per_msg = max_msg_size / target->tgt_cred_sz; 1373 if (max_msg_size % target->tgt_cred_sz) 1374 ep->cred_dist.cred_per_msg++; 1375 1376 /* copy all the callbacks */ 1377 ep->ep_cb = conn_req->ep_cb; 1378 1379 /* initialize tx_drop_packet_threshold */ 1380 ep->tx_drop_packet_threshold = MAX_HI_COOKIE_NUM; 1381 1382 status = ath6kl_hif_pipe_map_service(ar, ep->svc_id, 1383 &ep->pipe.pipeid_ul, 1384 &ep->pipe.pipeid_dl); 1385 if (status != 0) 1386 goto free_packet; 1387 1388 ath6kl_dbg(ATH6KL_DBG_HTC, 1389 "SVC Ready: 0x%4.4X: ULpipe:%d DLpipe:%d id:%d\n", 1390 ep->svc_id, ep->pipe.pipeid_ul, 1391 ep->pipe.pipeid_dl, ep->eid); 1392 1393 if (disable_credit_flowctrl && ep->pipe.tx_credit_flow_enabled) { 1394 ep->pipe.tx_credit_flow_enabled = false; 1395 ath6kl_dbg(ATH6KL_DBG_HTC, 1396 "SVC: 0x%4.4X ep:%d TX flow control off\n", 1397 ep->svc_id, assigned_epid); 1398 } 1399 1400 free_packet: 1401 if (packet != NULL) 1402 htc_free_txctrl_packet(target, packet); 1403 return status; 1404 } 1405 1406 /* htc export functions */ 1407 static void *ath6kl_htc_pipe_create(struct ath6kl *ar) 1408 { 1409 int status = 0; 1410 struct htc_endpoint *ep = NULL; 1411 struct htc_target *target = NULL; 1412 struct htc_packet *packet; 1413 int i; 1414 1415 target = kzalloc(sizeof(struct htc_target), GFP_KERNEL); 1416 if (target == NULL) { 1417 ath6kl_err("htc create unable to allocate memory\n"); 1418 status = -ENOMEM; 1419 goto fail_htc_create; 1420 } 1421 1422 spin_lock_init(&target->htc_lock); 1423 spin_lock_init(&target->rx_lock); 1424 spin_lock_init(&target->tx_lock); 1425 1426 reset_endpoint_states(target); 1427 1428 for (i = 0; i < HTC_PACKET_CONTAINER_ALLOCATION; i++) { 1429 packet = kzalloc(sizeof(struct htc_packet), GFP_KERNEL); 1430 1431 if (packet != NULL) 1432 free_htc_packet_container(target, packet); 1433 } 1434 1435 target->dev = kzalloc(sizeof(*target->dev), GFP_KERNEL); 1436 if (!target->dev) { 1437 ath6kl_err("unable to allocate memory\n"); 1438 status = -ENOMEM; 1439 goto fail_htc_create; 1440 } 1441 target->dev->ar = ar; 1442 target->dev->htc_cnxt = target; 1443 1444 /* Get HIF default pipe for HTC message exchange */ 1445 ep = &target->endpoint[ENDPOINT_0]; 1446 1447 ath6kl_hif_pipe_get_default(ar, &ep->pipe.pipeid_ul, 1448 &ep->pipe.pipeid_dl); 1449 1450 return target; 1451 1452 fail_htc_create: 1453 if (status != 0) { 1454 if (target != NULL) 1455 ath6kl_htc_pipe_cleanup(target); 1456 1457 target = NULL; 1458 } 1459 return target; 1460 } 1461 1462 /* cleanup the HTC instance */ 1463 static void ath6kl_htc_pipe_cleanup(struct htc_target *target) 1464 { 1465 struct htc_packet *packet; 1466 1467 while (true) { 1468 packet = alloc_htc_packet_container(target); 1469 if (packet == NULL) 1470 break; 1471 kfree(packet); 1472 } 1473 1474 kfree(target->dev); 1475 1476 /* kfree our instance */ 1477 kfree(target); 1478 } 1479 1480 static int ath6kl_htc_pipe_start(struct htc_target *target) 1481 { 1482 struct sk_buff *skb; 1483 struct htc_setup_comp_ext_msg *setup; 1484 struct htc_packet *packet; 1485 1486 htc_config_target_hif_pipe(target); 1487 1488 /* allocate a buffer to send */ 1489 packet = htc_alloc_txctrl_packet(target); 1490 if (packet == NULL) { 1491 WARN_ON_ONCE(1); 1492 return -ENOMEM; 1493 } 1494 1495 skb = packet->skb; 1496 1497 /* assemble setup complete message */ 1498 setup = (struct htc_setup_comp_ext_msg *) skb_put(skb, 1499 sizeof(*setup)); 1500 memset(setup, 0, sizeof(struct htc_setup_comp_ext_msg)); 1501 setup->msg_id = cpu_to_le16(HTC_MSG_SETUP_COMPLETE_EX_ID); 1502 1503 ath6kl_dbg(ATH6KL_DBG_HTC, "HTC using TX credit flow control\n"); 1504 1505 set_htc_pkt_info(packet, NULL, (u8 *) setup, 1506 sizeof(struct htc_setup_comp_ext_msg), 1507 ENDPOINT_0, HTC_SERVICE_TX_PACKET_TAG); 1508 1509 target->htc_flags |= HTC_OP_STATE_SETUP_COMPLETE; 1510 1511 return ath6kl_htc_pipe_tx(target, packet); 1512 } 1513 1514 static void ath6kl_htc_pipe_stop(struct htc_target *target) 1515 { 1516 int i; 1517 struct htc_endpoint *ep; 1518 1519 /* cleanup endpoints */ 1520 for (i = 0; i < ENDPOINT_MAX; i++) { 1521 ep = &target->endpoint[i]; 1522 htc_flush_rx_queue(target, ep); 1523 htc_flush_tx_endpoint(target, ep, HTC_TX_PACKET_TAG_ALL); 1524 } 1525 1526 reset_endpoint_states(target); 1527 target->htc_flags &= ~HTC_OP_STATE_SETUP_COMPLETE; 1528 } 1529 1530 static int ath6kl_htc_pipe_get_rxbuf_num(struct htc_target *target, 1531 enum htc_endpoint_id endpoint) 1532 { 1533 int num; 1534 1535 spin_lock_bh(&target->rx_lock); 1536 num = get_queue_depth(&(target->endpoint[endpoint].rx_bufq)); 1537 spin_unlock_bh(&target->rx_lock); 1538 1539 return num; 1540 } 1541 1542 static int ath6kl_htc_pipe_tx(struct htc_target *target, 1543 struct htc_packet *packet) 1544 { 1545 struct list_head queue; 1546 1547 ath6kl_dbg(ATH6KL_DBG_HTC, 1548 "%s: endPointId: %d, buffer: 0x%p, length: %d\n", 1549 __func__, packet->endpoint, packet->buf, 1550 packet->act_len); 1551 1552 INIT_LIST_HEAD(&queue); 1553 list_add_tail(&packet->list, &queue); 1554 1555 return htc_send_packets_multiple(target, &queue); 1556 } 1557 1558 static int ath6kl_htc_pipe_wait_target(struct htc_target *target) 1559 { 1560 struct htc_ready_ext_msg *ready_msg; 1561 struct htc_service_connect_req connect; 1562 struct htc_service_connect_resp resp; 1563 int status = 0; 1564 1565 status = htc_wait_recv_ctrl_message(target); 1566 1567 if (status != 0) 1568 return status; 1569 1570 if (target->pipe.ctrl_response_len < sizeof(*ready_msg)) { 1571 ath6kl_dbg(ATH6KL_DBG_HTC, "invalid htc ready msg len:%d!\n", 1572 target->pipe.ctrl_response_len); 1573 return -ECOMM; 1574 } 1575 1576 ready_msg = (struct htc_ready_ext_msg *) target->pipe.ctrl_response_buf; 1577 1578 if (ready_msg->ver2_0_info.msg_id != cpu_to_le16(HTC_MSG_READY_ID)) { 1579 ath6kl_dbg(ATH6KL_DBG_HTC, "invalid htc ready msg : 0x%X !\n", 1580 ready_msg->ver2_0_info.msg_id); 1581 return -ECOMM; 1582 } 1583 1584 ath6kl_dbg(ATH6KL_DBG_HTC, 1585 "Target Ready! : transmit resources : %d size:%d\n", 1586 ready_msg->ver2_0_info.cred_cnt, 1587 ready_msg->ver2_0_info.cred_sz); 1588 1589 target->tgt_creds = le16_to_cpu(ready_msg->ver2_0_info.cred_cnt); 1590 target->tgt_cred_sz = le16_to_cpu(ready_msg->ver2_0_info.cred_sz); 1591 1592 if ((target->tgt_creds == 0) || (target->tgt_cred_sz == 0)) 1593 return -ECOMM; 1594 1595 htc_setup_target_buffer_assignments(target); 1596 1597 /* setup our pseudo HTC control endpoint connection */ 1598 memset(&connect, 0, sizeof(connect)); 1599 memset(&resp, 0, sizeof(resp)); 1600 connect.ep_cb.tx_complete = htc_txctrl_complete; 1601 connect.ep_cb.rx = htc_rxctrl_complete; 1602 connect.max_txq_depth = NUM_CONTROL_TX_BUFFERS; 1603 connect.svc_id = HTC_CTRL_RSVD_SVC; 1604 1605 /* connect fake service */ 1606 status = ath6kl_htc_pipe_conn_service(target, &connect, &resp); 1607 1608 return status; 1609 } 1610 1611 static void ath6kl_htc_pipe_flush_txep(struct htc_target *target, 1612 enum htc_endpoint_id endpoint, u16 tag) 1613 { 1614 struct htc_endpoint *ep = &target->endpoint[endpoint]; 1615 1616 if (ep->svc_id == 0) { 1617 WARN_ON_ONCE(1); 1618 /* not in use.. */ 1619 return; 1620 } 1621 1622 htc_flush_tx_endpoint(target, ep, tag); 1623 } 1624 1625 static int ath6kl_htc_pipe_add_rxbuf_multiple(struct htc_target *target, 1626 struct list_head *pkt_queue) 1627 { 1628 struct htc_packet *packet, *tmp_pkt, *first; 1629 struct htc_endpoint *ep; 1630 int status = 0; 1631 1632 if (list_empty(pkt_queue)) 1633 return -EINVAL; 1634 1635 first = list_first_entry(pkt_queue, struct htc_packet, list); 1636 1637 if (first->endpoint >= ENDPOINT_MAX) { 1638 WARN_ON_ONCE(1); 1639 return -EINVAL; 1640 } 1641 1642 ath6kl_dbg(ATH6KL_DBG_HTC, "%s: epid: %d, cnt:%d, len: %d\n", 1643 __func__, first->endpoint, get_queue_depth(pkt_queue), 1644 first->buf_len); 1645 1646 ep = &target->endpoint[first->endpoint]; 1647 1648 spin_lock_bh(&target->rx_lock); 1649 1650 /* store receive packets */ 1651 list_splice_tail_init(pkt_queue, &ep->rx_bufq); 1652 1653 spin_unlock_bh(&target->rx_lock); 1654 1655 if (status != 0) { 1656 /* walk through queue and mark each one canceled */ 1657 list_for_each_entry_safe(packet, tmp_pkt, pkt_queue, list) { 1658 packet->status = -ECANCELED; 1659 } 1660 1661 do_recv_completion(ep, pkt_queue); 1662 } 1663 1664 return status; 1665 } 1666 1667 static void ath6kl_htc_pipe_activity_changed(struct htc_target *target, 1668 enum htc_endpoint_id ep, 1669 bool active) 1670 { 1671 /* TODO */ 1672 } 1673 1674 static void ath6kl_htc_pipe_flush_rx_buf(struct htc_target *target) 1675 { 1676 /* TODO */ 1677 } 1678 1679 static int ath6kl_htc_pipe_credit_setup(struct htc_target *target, 1680 struct ath6kl_htc_credit_info *info) 1681 { 1682 return 0; 1683 } 1684 1685 static const struct ath6kl_htc_ops ath6kl_htc_pipe_ops = { 1686 .create = ath6kl_htc_pipe_create, 1687 .wait_target = ath6kl_htc_pipe_wait_target, 1688 .start = ath6kl_htc_pipe_start, 1689 .conn_service = ath6kl_htc_pipe_conn_service, 1690 .tx = ath6kl_htc_pipe_tx, 1691 .stop = ath6kl_htc_pipe_stop, 1692 .cleanup = ath6kl_htc_pipe_cleanup, 1693 .flush_txep = ath6kl_htc_pipe_flush_txep, 1694 .flush_rx_buf = ath6kl_htc_pipe_flush_rx_buf, 1695 .activity_changed = ath6kl_htc_pipe_activity_changed, 1696 .get_rxbuf_num = ath6kl_htc_pipe_get_rxbuf_num, 1697 .add_rxbuf_multiple = ath6kl_htc_pipe_add_rxbuf_multiple, 1698 .credit_setup = ath6kl_htc_pipe_credit_setup, 1699 .tx_complete = ath6kl_htc_pipe_tx_complete, 1700 .rx_complete = ath6kl_htc_pipe_rx_complete, 1701 }; 1702 1703 void ath6kl_htc_pipe_attach(struct ath6kl *ar) 1704 { 1705 ar->htc_ops = &ath6kl_htc_pipe_ops; 1706 } 1707