1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause 2 /* 3 * Copyright (C) 2020-2021 Intel Corporation 4 */ 5 #ifdef CONFIG_INET 6 #include <net/tso.h> 7 #endif 8 #include <linux/tcp.h> 9 10 #include "iwl-debug.h" 11 #include "iwl-io.h" 12 #include "fw/api/tx.h" 13 #include "queue/tx.h" 14 #include "iwl-fh.h" 15 #include "iwl-scd.h" 16 #include <linux/dmapool.h> 17 #if defined(__FreeBSD__) 18 #include <net/mac80211.h> 19 #endif 20 21 /* 22 * iwl_txq_update_byte_tbl - Set up entry in Tx byte-count array 23 */ 24 static void iwl_pcie_gen2_update_byte_tbl(struct iwl_trans *trans, 25 struct iwl_txq *txq, u16 byte_cnt, 26 int num_tbs) 27 { 28 int idx = iwl_txq_get_cmd_index(txq, txq->write_ptr); 29 u8 filled_tfd_size, num_fetch_chunks; 30 u16 len = byte_cnt; 31 __le16 bc_ent; 32 33 if (WARN(idx >= txq->n_window, "%d >= %d\n", idx, txq->n_window)) 34 return; 35 36 filled_tfd_size = offsetof(struct iwl_tfh_tfd, tbs) + 37 num_tbs * sizeof(struct iwl_tfh_tb); 38 /* 39 * filled_tfd_size contains the number of filled bytes in the TFD. 40 * Dividing it by 64 will give the number of chunks to fetch 41 * to SRAM- 0 for one chunk, 1 for 2 and so on. 42 * If, for example, TFD contains only 3 TBs then 32 bytes 43 * of the TFD are used, and only one chunk of 64 bytes should 44 * be fetched 45 */ 46 num_fetch_chunks = DIV_ROUND_UP(filled_tfd_size, 64) - 1; 47 48 if (trans->trans_cfg->device_family >= IWL_DEVICE_FAMILY_AX210) { 49 struct iwl_gen3_bc_tbl *scd_bc_tbl_gen3 = txq->bc_tbl.addr; 50 51 /* Starting from AX210, the HW expects bytes */ 52 WARN_ON(trans->txqs.bc_table_dword); 53 WARN_ON(len > 0x3FFF); 54 bc_ent = cpu_to_le16(len | (num_fetch_chunks << 14)); 55 scd_bc_tbl_gen3->tfd_offset[idx] = bc_ent; 56 } else { 57 struct iwlagn_scd_bc_tbl *scd_bc_tbl = txq->bc_tbl.addr; 58 59 /* Before AX210, the HW expects DW */ 60 WARN_ON(!trans->txqs.bc_table_dword); 61 len = DIV_ROUND_UP(len, 4); 62 WARN_ON(len > 0xFFF); 63 bc_ent = cpu_to_le16(len | (num_fetch_chunks << 12)); 64 scd_bc_tbl->tfd_offset[idx] = bc_ent; 65 } 66 } 67 68 /* 69 * iwl_txq_inc_wr_ptr - Send new write index to hardware 70 */ 71 void iwl_txq_inc_wr_ptr(struct iwl_trans *trans, struct iwl_txq *txq) 72 { 73 lockdep_assert_held(&txq->lock); 74 75 IWL_DEBUG_TX(trans, "Q:%d WR: 0x%x\n", txq->id, txq->write_ptr); 76 77 /* 78 * if not in power-save mode, uCode will never sleep when we're 79 * trying to tx (during RFKILL, we're not trying to tx). 80 */ 81 iwl_write32(trans, HBUS_TARG_WRPTR, txq->write_ptr | (txq->id << 16)); 82 } 83 84 static u8 iwl_txq_gen2_get_num_tbs(struct iwl_trans *trans, 85 struct iwl_tfh_tfd *tfd) 86 { 87 return le16_to_cpu(tfd->num_tbs) & 0x1f; 88 } 89 90 void iwl_txq_gen2_tfd_unmap(struct iwl_trans *trans, struct iwl_cmd_meta *meta, 91 struct iwl_tfh_tfd *tfd) 92 { 93 int i, num_tbs; 94 95 /* Sanity check on number of chunks */ 96 num_tbs = iwl_txq_gen2_get_num_tbs(trans, tfd); 97 98 if (num_tbs > trans->txqs.tfd.max_tbs) { 99 IWL_ERR(trans, "Too many chunks: %i\n", num_tbs); 100 return; 101 } 102 103 /* first TB is never freed - it's the bidirectional DMA data */ 104 for (i = 1; i < num_tbs; i++) { 105 if (meta->tbs & BIT(i)) 106 dma_unmap_page(trans->dev, 107 le64_to_cpu(tfd->tbs[i].addr), 108 le16_to_cpu(tfd->tbs[i].tb_len), 109 DMA_TO_DEVICE); 110 else 111 dma_unmap_single(trans->dev, 112 le64_to_cpu(tfd->tbs[i].addr), 113 le16_to_cpu(tfd->tbs[i].tb_len), 114 DMA_TO_DEVICE); 115 } 116 117 tfd->num_tbs = 0; 118 } 119 120 void iwl_txq_gen2_free_tfd(struct iwl_trans *trans, struct iwl_txq *txq) 121 { 122 /* rd_ptr is bounded by TFD_QUEUE_SIZE_MAX and 123 * idx is bounded by n_window 124 */ 125 int idx = iwl_txq_get_cmd_index(txq, txq->read_ptr); 126 struct sk_buff *skb; 127 128 lockdep_assert_held(&txq->lock); 129 130 if (!txq->entries) 131 return; 132 133 iwl_txq_gen2_tfd_unmap(trans, &txq->entries[idx].meta, 134 iwl_txq_get_tfd(trans, txq, idx)); 135 136 skb = txq->entries[idx].skb; 137 138 /* Can be called from irqs-disabled context 139 * If skb is not NULL, it means that the whole queue is being 140 * freed and that the queue is not empty - free the skb 141 */ 142 if (skb) { 143 iwl_op_mode_free_skb(trans->op_mode, skb); 144 txq->entries[idx].skb = NULL; 145 } 146 } 147 148 int iwl_txq_gen2_set_tb(struct iwl_trans *trans, struct iwl_tfh_tfd *tfd, 149 dma_addr_t addr, u16 len) 150 { 151 int idx = iwl_txq_gen2_get_num_tbs(trans, tfd); 152 struct iwl_tfh_tb *tb; 153 154 /* 155 * Only WARN here so we know about the issue, but we mess up our 156 * unmap path because not every place currently checks for errors 157 * returned from this function - it can only return an error if 158 * there's no more space, and so when we know there is enough we 159 * don't always check ... 160 */ 161 WARN(iwl_txq_crosses_4g_boundary(addr, len), 162 "possible DMA problem with iova:0x%llx, len:%d\n", 163 (unsigned long long)addr, len); 164 165 if (WARN_ON(idx >= IWL_TFH_NUM_TBS)) 166 return -EINVAL; 167 tb = &tfd->tbs[idx]; 168 169 /* Each TFD can point to a maximum max_tbs Tx buffers */ 170 if (le16_to_cpu(tfd->num_tbs) >= trans->txqs.tfd.max_tbs) { 171 IWL_ERR(trans, "Error can not send more than %d chunks\n", 172 trans->txqs.tfd.max_tbs); 173 return -EINVAL; 174 } 175 176 put_unaligned_le64(addr, &tb->addr); 177 tb->tb_len = cpu_to_le16(len); 178 179 tfd->num_tbs = cpu_to_le16(idx + 1); 180 181 return idx; 182 } 183 184 static struct page *get_workaround_page(struct iwl_trans *trans, 185 struct sk_buff *skb) 186 { 187 struct page **page_ptr; 188 struct page *ret; 189 190 page_ptr = (void *)((u8 *)skb->cb + trans->txqs.page_offs); 191 192 ret = alloc_page(GFP_ATOMIC); 193 if (!ret) 194 return NULL; 195 196 /* set the chaining pointer to the previous page if there */ 197 *(void **)((u8 *)page_address(ret) + PAGE_SIZE - sizeof(void *)) = 198 *page_ptr; 199 *page_ptr = ret; 200 201 return ret; 202 } 203 204 /* 205 * Add a TB and if needed apply the FH HW bug workaround; 206 * meta != NULL indicates that it's a page mapping and we 207 * need to dma_unmap_page() and set the meta->tbs bit in 208 * this case. 209 */ 210 static int iwl_txq_gen2_set_tb_with_wa(struct iwl_trans *trans, 211 struct sk_buff *skb, 212 struct iwl_tfh_tfd *tfd, 213 dma_addr_t phys, void *virt, 214 u16 len, struct iwl_cmd_meta *meta) 215 { 216 dma_addr_t oldphys = phys; 217 struct page *page; 218 int ret; 219 220 if (unlikely(dma_mapping_error(trans->dev, phys))) 221 return -ENOMEM; 222 223 if (likely(!iwl_txq_crosses_4g_boundary(phys, len))) { 224 ret = iwl_txq_gen2_set_tb(trans, tfd, phys, len); 225 226 if (ret < 0) 227 goto unmap; 228 229 if (meta) 230 meta->tbs |= BIT(ret); 231 232 ret = 0; 233 goto trace; 234 } 235 236 /* 237 * Work around a hardware bug. If (as expressed in the 238 * condition above) the TB ends on a 32-bit boundary, 239 * then the next TB may be accessed with the wrong 240 * address. 241 * To work around it, copy the data elsewhere and make 242 * a new mapping for it so the device will not fail. 243 */ 244 245 if (WARN_ON(len > PAGE_SIZE - sizeof(void *))) { 246 ret = -ENOBUFS; 247 goto unmap; 248 } 249 250 page = get_workaround_page(trans, skb); 251 if (!page) { 252 ret = -ENOMEM; 253 goto unmap; 254 } 255 256 memcpy(page_address(page), virt, len); 257 258 phys = dma_map_single(trans->dev, page_address(page), len, 259 DMA_TO_DEVICE); 260 if (unlikely(dma_mapping_error(trans->dev, phys))) 261 return -ENOMEM; 262 ret = iwl_txq_gen2_set_tb(trans, tfd, phys, len); 263 if (ret < 0) { 264 /* unmap the new allocation as single */ 265 oldphys = phys; 266 meta = NULL; 267 goto unmap; 268 } 269 IWL_WARN(trans, 270 "TB bug workaround: copied %d bytes from 0x%llx to 0x%llx\n", 271 len, (unsigned long long)oldphys, (unsigned long long)phys); 272 273 ret = 0; 274 unmap: 275 if (meta) 276 dma_unmap_page(trans->dev, oldphys, len, DMA_TO_DEVICE); 277 else 278 dma_unmap_single(trans->dev, oldphys, len, DMA_TO_DEVICE); 279 trace: 280 trace_iwlwifi_dev_tx_tb(trans->dev, skb, virt, phys, len); 281 282 return ret; 283 } 284 285 #ifdef CONFIG_INET 286 struct iwl_tso_hdr_page *get_page_hdr(struct iwl_trans *trans, size_t len, 287 struct sk_buff *skb) 288 { 289 struct iwl_tso_hdr_page *p = this_cpu_ptr(trans->txqs.tso_hdr_page); 290 struct page **page_ptr; 291 292 page_ptr = (void *)((u8 *)skb->cb + trans->txqs.page_offs); 293 294 if (WARN_ON(*page_ptr)) 295 return NULL; 296 297 if (!p->page) 298 goto alloc; 299 300 /* 301 * Check if there's enough room on this page 302 * 303 * Note that we put a page chaining pointer *last* in the 304 * page - we need it somewhere, and if it's there then we 305 * avoid DMA mapping the last bits of the page which may 306 * trigger the 32-bit boundary hardware bug. 307 * 308 * (see also get_workaround_page() in tx-gen2.c) 309 */ 310 if (p->pos + len < (u8 *)page_address(p->page) + PAGE_SIZE - 311 sizeof(void *)) 312 goto out; 313 314 /* We don't have enough room on this page, get a new one. */ 315 __free_page(p->page); 316 317 alloc: 318 p->page = alloc_page(GFP_ATOMIC); 319 if (!p->page) 320 return NULL; 321 p->pos = page_address(p->page); 322 /* set the chaining pointer to NULL */ 323 *(void **)((u8 *)page_address(p->page) + PAGE_SIZE - sizeof(void *)) = 324 NULL; 325 out: 326 *page_ptr = p->page; 327 get_page(p->page); 328 return p; 329 } 330 #endif 331 332 static int iwl_txq_gen2_build_amsdu(struct iwl_trans *trans, 333 struct sk_buff *skb, 334 struct iwl_tfh_tfd *tfd, int start_len, 335 u8 hdr_len, 336 struct iwl_device_tx_cmd *dev_cmd) 337 { 338 #ifdef CONFIG_INET 339 struct iwl_tx_cmd_gen2 *tx_cmd = (void *)dev_cmd->payload; 340 struct ieee80211_hdr *hdr = (void *)skb->data; 341 unsigned int snap_ip_tcp_hdrlen, ip_hdrlen, total_len, hdr_room; 342 unsigned int mss = skb_shinfo(skb)->gso_size; 343 u16 length, amsdu_pad; 344 u8 *start_hdr; 345 struct iwl_tso_hdr_page *hdr_page; 346 struct tso_t tso; 347 348 trace_iwlwifi_dev_tx(trans->dev, skb, tfd, sizeof(*tfd), 349 &dev_cmd->hdr, start_len, 0); 350 351 ip_hdrlen = skb_transport_header(skb) - skb_network_header(skb); 352 snap_ip_tcp_hdrlen = 8 + ip_hdrlen + tcp_hdrlen(skb); 353 total_len = skb->len - snap_ip_tcp_hdrlen - hdr_len; 354 amsdu_pad = 0; 355 356 /* total amount of header we may need for this A-MSDU */ 357 hdr_room = DIV_ROUND_UP(total_len, mss) * 358 (3 + snap_ip_tcp_hdrlen + sizeof(struct ethhdr)); 359 360 /* Our device supports 9 segments at most, it will fit in 1 page */ 361 hdr_page = get_page_hdr(trans, hdr_room, skb); 362 if (!hdr_page) 363 return -ENOMEM; 364 365 start_hdr = hdr_page->pos; 366 367 /* 368 * Pull the ieee80211 header to be able to use TSO core, 369 * we will restore it for the tx_status flow. 370 */ 371 skb_pull(skb, hdr_len); 372 373 /* 374 * Remove the length of all the headers that we don't actually 375 * have in the MPDU by themselves, but that we duplicate into 376 * all the different MSDUs inside the A-MSDU. 377 */ 378 le16_add_cpu(&tx_cmd->len, -snap_ip_tcp_hdrlen); 379 380 tso_start(skb, &tso); 381 382 while (total_len) { 383 /* this is the data left for this subframe */ 384 unsigned int data_left = min_t(unsigned int, mss, total_len); 385 unsigned int tb_len; 386 dma_addr_t tb_phys; 387 u8 *subf_hdrs_start = hdr_page->pos; 388 389 total_len -= data_left; 390 391 memset(hdr_page->pos, 0, amsdu_pad); 392 hdr_page->pos += amsdu_pad; 393 amsdu_pad = (4 - (sizeof(struct ethhdr) + snap_ip_tcp_hdrlen + 394 data_left)) & 0x3; 395 ether_addr_copy(hdr_page->pos, ieee80211_get_DA(hdr)); 396 hdr_page->pos += ETH_ALEN; 397 ether_addr_copy(hdr_page->pos, ieee80211_get_SA(hdr)); 398 hdr_page->pos += ETH_ALEN; 399 400 length = snap_ip_tcp_hdrlen + data_left; 401 *((__be16 *)hdr_page->pos) = cpu_to_be16(length); 402 hdr_page->pos += sizeof(length); 403 404 /* 405 * This will copy the SNAP as well which will be considered 406 * as MAC header. 407 */ 408 tso_build_hdr(skb, hdr_page->pos, &tso, data_left, !total_len); 409 410 hdr_page->pos += snap_ip_tcp_hdrlen; 411 412 tb_len = hdr_page->pos - start_hdr; 413 tb_phys = dma_map_single(trans->dev, start_hdr, 414 tb_len, DMA_TO_DEVICE); 415 if (unlikely(dma_mapping_error(trans->dev, tb_phys))) 416 goto out_err; 417 /* 418 * No need for _with_wa, this is from the TSO page and 419 * we leave some space at the end of it so can't hit 420 * the buggy scenario. 421 */ 422 iwl_txq_gen2_set_tb(trans, tfd, tb_phys, tb_len); 423 trace_iwlwifi_dev_tx_tb(trans->dev, skb, start_hdr, 424 tb_phys, tb_len); 425 /* add this subframe's headers' length to the tx_cmd */ 426 le16_add_cpu(&tx_cmd->len, hdr_page->pos - subf_hdrs_start); 427 428 /* prepare the start_hdr for the next subframe */ 429 start_hdr = hdr_page->pos; 430 431 /* put the payload */ 432 while (data_left) { 433 int ret; 434 435 tb_len = min_t(unsigned int, tso.size, data_left); 436 tb_phys = dma_map_single(trans->dev, tso.data, 437 tb_len, DMA_TO_DEVICE); 438 ret = iwl_txq_gen2_set_tb_with_wa(trans, skb, tfd, 439 tb_phys, tso.data, 440 tb_len, NULL); 441 if (ret) 442 goto out_err; 443 444 data_left -= tb_len; 445 tso_build_data(skb, &tso, tb_len); 446 } 447 } 448 449 /* re -add the WiFi header */ 450 skb_push(skb, hdr_len); 451 452 return 0; 453 454 out_err: 455 #endif 456 return -EINVAL; 457 } 458 459 static struct 460 iwl_tfh_tfd *iwl_txq_gen2_build_tx_amsdu(struct iwl_trans *trans, 461 struct iwl_txq *txq, 462 struct iwl_device_tx_cmd *dev_cmd, 463 struct sk_buff *skb, 464 struct iwl_cmd_meta *out_meta, 465 int hdr_len, 466 int tx_cmd_len) 467 { 468 int idx = iwl_txq_get_cmd_index(txq, txq->write_ptr); 469 struct iwl_tfh_tfd *tfd = iwl_txq_get_tfd(trans, txq, idx); 470 dma_addr_t tb_phys; 471 int len; 472 void *tb1_addr; 473 474 tb_phys = iwl_txq_get_first_tb_dma(txq, idx); 475 476 /* 477 * No need for _with_wa, the first TB allocation is aligned up 478 * to a 64-byte boundary and thus can't be at the end or cross 479 * a page boundary (much less a 2^32 boundary). 480 */ 481 iwl_txq_gen2_set_tb(trans, tfd, tb_phys, IWL_FIRST_TB_SIZE); 482 483 /* 484 * The second TB (tb1) points to the remainder of the TX command 485 * and the 802.11 header - dword aligned size 486 * (This calculation modifies the TX command, so do it before the 487 * setup of the first TB) 488 */ 489 len = tx_cmd_len + sizeof(struct iwl_cmd_header) + hdr_len - 490 IWL_FIRST_TB_SIZE; 491 492 /* do not align A-MSDU to dword as the subframe header aligns it */ 493 494 /* map the data for TB1 */ 495 tb1_addr = ((u8 *)&dev_cmd->hdr) + IWL_FIRST_TB_SIZE; 496 tb_phys = dma_map_single(trans->dev, tb1_addr, len, DMA_TO_DEVICE); 497 if (unlikely(dma_mapping_error(trans->dev, tb_phys))) 498 goto out_err; 499 /* 500 * No need for _with_wa(), we ensure (via alignment) that the data 501 * here can never cross or end at a page boundary. 502 */ 503 iwl_txq_gen2_set_tb(trans, tfd, tb_phys, len); 504 505 if (iwl_txq_gen2_build_amsdu(trans, skb, tfd, len + IWL_FIRST_TB_SIZE, 506 hdr_len, dev_cmd)) 507 goto out_err; 508 509 /* building the A-MSDU might have changed this data, memcpy it now */ 510 memcpy(&txq->first_tb_bufs[idx], dev_cmd, IWL_FIRST_TB_SIZE); 511 return tfd; 512 513 out_err: 514 iwl_txq_gen2_tfd_unmap(trans, out_meta, tfd); 515 return NULL; 516 } 517 518 static int iwl_txq_gen2_tx_add_frags(struct iwl_trans *trans, 519 struct sk_buff *skb, 520 struct iwl_tfh_tfd *tfd, 521 struct iwl_cmd_meta *out_meta) 522 { 523 int i; 524 525 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 526 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 527 dma_addr_t tb_phys; 528 unsigned int fragsz = skb_frag_size(frag); 529 int ret; 530 531 if (!fragsz) 532 continue; 533 534 tb_phys = skb_frag_dma_map(trans->dev, frag, 0, 535 fragsz, DMA_TO_DEVICE); 536 ret = iwl_txq_gen2_set_tb_with_wa(trans, skb, tfd, tb_phys, 537 skb_frag_address(frag), 538 fragsz, out_meta); 539 if (ret) 540 return ret; 541 } 542 543 return 0; 544 } 545 546 static struct 547 iwl_tfh_tfd *iwl_txq_gen2_build_tx(struct iwl_trans *trans, 548 struct iwl_txq *txq, 549 struct iwl_device_tx_cmd *dev_cmd, 550 struct sk_buff *skb, 551 struct iwl_cmd_meta *out_meta, 552 int hdr_len, 553 int tx_cmd_len, 554 bool pad) 555 { 556 int idx = iwl_txq_get_cmd_index(txq, txq->write_ptr); 557 struct iwl_tfh_tfd *tfd = iwl_txq_get_tfd(trans, txq, idx); 558 dma_addr_t tb_phys; 559 int len, tb1_len, tb2_len; 560 void *tb1_addr; 561 struct sk_buff *frag; 562 563 tb_phys = iwl_txq_get_first_tb_dma(txq, idx); 564 565 /* The first TB points to bi-directional DMA data */ 566 memcpy(&txq->first_tb_bufs[idx], dev_cmd, IWL_FIRST_TB_SIZE); 567 568 /* 569 * No need for _with_wa, the first TB allocation is aligned up 570 * to a 64-byte boundary and thus can't be at the end or cross 571 * a page boundary (much less a 2^32 boundary). 572 */ 573 iwl_txq_gen2_set_tb(trans, tfd, tb_phys, IWL_FIRST_TB_SIZE); 574 575 /* 576 * The second TB (tb1) points to the remainder of the TX command 577 * and the 802.11 header - dword aligned size 578 * (This calculation modifies the TX command, so do it before the 579 * setup of the first TB) 580 */ 581 len = tx_cmd_len + sizeof(struct iwl_cmd_header) + hdr_len - 582 IWL_FIRST_TB_SIZE; 583 584 if (pad) 585 tb1_len = ALIGN(len, 4); 586 else 587 tb1_len = len; 588 589 /* map the data for TB1 */ 590 tb1_addr = ((u8 *)&dev_cmd->hdr) + IWL_FIRST_TB_SIZE; 591 tb_phys = dma_map_single(trans->dev, tb1_addr, tb1_len, DMA_TO_DEVICE); 592 if (unlikely(dma_mapping_error(trans->dev, tb_phys))) 593 goto out_err; 594 /* 595 * No need for _with_wa(), we ensure (via alignment) that the data 596 * here can never cross or end at a page boundary. 597 */ 598 iwl_txq_gen2_set_tb(trans, tfd, tb_phys, tb1_len); 599 trace_iwlwifi_dev_tx(trans->dev, skb, tfd, sizeof(*tfd), &dev_cmd->hdr, 600 IWL_FIRST_TB_SIZE + tb1_len, hdr_len); 601 602 /* set up TFD's third entry to point to remainder of skb's head */ 603 tb2_len = skb_headlen(skb) - hdr_len; 604 605 if (tb2_len > 0) { 606 int ret; 607 608 tb_phys = dma_map_single(trans->dev, skb->data + hdr_len, 609 tb2_len, DMA_TO_DEVICE); 610 ret = iwl_txq_gen2_set_tb_with_wa(trans, skb, tfd, tb_phys, 611 skb->data + hdr_len, tb2_len, 612 NULL); 613 if (ret) 614 goto out_err; 615 } 616 617 if (iwl_txq_gen2_tx_add_frags(trans, skb, tfd, out_meta)) 618 goto out_err; 619 620 skb_walk_frags(skb, frag) { 621 int ret; 622 623 tb_phys = dma_map_single(trans->dev, frag->data, 624 skb_headlen(frag), DMA_TO_DEVICE); 625 ret = iwl_txq_gen2_set_tb_with_wa(trans, skb, tfd, tb_phys, 626 frag->data, 627 skb_headlen(frag), NULL); 628 if (ret) 629 goto out_err; 630 if (iwl_txq_gen2_tx_add_frags(trans, frag, tfd, out_meta)) 631 goto out_err; 632 } 633 634 return tfd; 635 636 out_err: 637 iwl_txq_gen2_tfd_unmap(trans, out_meta, tfd); 638 return NULL; 639 } 640 641 static 642 struct iwl_tfh_tfd *iwl_txq_gen2_build_tfd(struct iwl_trans *trans, 643 struct iwl_txq *txq, 644 struct iwl_device_tx_cmd *dev_cmd, 645 struct sk_buff *skb, 646 struct iwl_cmd_meta *out_meta) 647 { 648 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data; 649 int idx = iwl_txq_get_cmd_index(txq, txq->write_ptr); 650 struct iwl_tfh_tfd *tfd = iwl_txq_get_tfd(trans, txq, idx); 651 int len, hdr_len; 652 bool amsdu; 653 654 /* There must be data left over for TB1 or this code must be changed */ 655 BUILD_BUG_ON(sizeof(struct iwl_tx_cmd_gen2) < IWL_FIRST_TB_SIZE); 656 657 memset(tfd, 0, sizeof(*tfd)); 658 659 if (trans->trans_cfg->device_family < IWL_DEVICE_FAMILY_AX210) 660 len = sizeof(struct iwl_tx_cmd_gen2); 661 else 662 len = sizeof(struct iwl_tx_cmd_gen3); 663 664 amsdu = ieee80211_is_data_qos(hdr->frame_control) && 665 (*ieee80211_get_qos_ctl(hdr) & 666 IEEE80211_QOS_CTL_A_MSDU_PRESENT); 667 668 hdr_len = ieee80211_hdrlen(hdr->frame_control); 669 670 /* 671 * Only build A-MSDUs here if doing so by GSO, otherwise it may be 672 * an A-MSDU for other reasons, e.g. NAN or an A-MSDU having been 673 * built in the higher layers already. 674 */ 675 if (amsdu && skb_shinfo(skb)->gso_size) 676 return iwl_txq_gen2_build_tx_amsdu(trans, txq, dev_cmd, skb, 677 out_meta, hdr_len, len); 678 return iwl_txq_gen2_build_tx(trans, txq, dev_cmd, skb, out_meta, 679 hdr_len, len, !amsdu); 680 } 681 682 int iwl_txq_space(struct iwl_trans *trans, const struct iwl_txq *q) 683 { 684 unsigned int max; 685 unsigned int used; 686 687 /* 688 * To avoid ambiguity between empty and completely full queues, there 689 * should always be less than max_tfd_queue_size elements in the queue. 690 * If q->n_window is smaller than max_tfd_queue_size, there is no need 691 * to reserve any queue entries for this purpose. 692 */ 693 if (q->n_window < trans->trans_cfg->base_params->max_tfd_queue_size) 694 max = q->n_window; 695 else 696 max = trans->trans_cfg->base_params->max_tfd_queue_size - 1; 697 698 /* 699 * max_tfd_queue_size is a power of 2, so the following is equivalent to 700 * modulo by max_tfd_queue_size and is well defined. 701 */ 702 used = (q->write_ptr - q->read_ptr) & 703 (trans->trans_cfg->base_params->max_tfd_queue_size - 1); 704 705 if (WARN_ON(used > max)) 706 return 0; 707 708 return max - used; 709 } 710 711 int iwl_txq_gen2_tx(struct iwl_trans *trans, struct sk_buff *skb, 712 struct iwl_device_tx_cmd *dev_cmd, int txq_id) 713 { 714 struct iwl_cmd_meta *out_meta; 715 struct iwl_txq *txq = trans->txqs.txq[txq_id]; 716 u16 cmd_len; 717 int idx; 718 void *tfd; 719 720 if (WARN_ONCE(txq_id >= IWL_MAX_TVQM_QUEUES, 721 "queue %d out of range", txq_id)) 722 return -EINVAL; 723 724 if (WARN_ONCE(!test_bit(txq_id, trans->txqs.queue_used), 725 "TX on unused queue %d\n", txq_id)) 726 return -EINVAL; 727 728 if (skb_is_nonlinear(skb) && 729 skb_shinfo(skb)->nr_frags > IWL_TRANS_MAX_FRAGS(trans) && 730 __skb_linearize(skb)) 731 return -ENOMEM; 732 733 spin_lock(&txq->lock); 734 735 if (iwl_txq_space(trans, txq) < txq->high_mark) { 736 iwl_txq_stop(trans, txq); 737 738 /* don't put the packet on the ring, if there is no room */ 739 if (unlikely(iwl_txq_space(trans, txq) < 3)) { 740 struct iwl_device_tx_cmd **dev_cmd_ptr; 741 742 dev_cmd_ptr = (void *)((u8 *)skb->cb + 743 trans->txqs.dev_cmd_offs); 744 745 *dev_cmd_ptr = dev_cmd; 746 __skb_queue_tail(&txq->overflow_q, skb); 747 spin_unlock(&txq->lock); 748 return 0; 749 } 750 } 751 752 idx = iwl_txq_get_cmd_index(txq, txq->write_ptr); 753 754 /* Set up driver data for this TFD */ 755 txq->entries[idx].skb = skb; 756 txq->entries[idx].cmd = dev_cmd; 757 758 dev_cmd->hdr.sequence = 759 cpu_to_le16((u16)(QUEUE_TO_SEQ(txq_id) | 760 INDEX_TO_SEQ(idx))); 761 762 /* Set up first empty entry in queue's array of Tx/cmd buffers */ 763 out_meta = &txq->entries[idx].meta; 764 out_meta->flags = 0; 765 766 tfd = iwl_txq_gen2_build_tfd(trans, txq, dev_cmd, skb, out_meta); 767 if (!tfd) { 768 spin_unlock(&txq->lock); 769 return -1; 770 } 771 772 if (trans->trans_cfg->device_family >= IWL_DEVICE_FAMILY_AX210) { 773 struct iwl_tx_cmd_gen3 *tx_cmd_gen3 = 774 (void *)dev_cmd->payload; 775 776 cmd_len = le16_to_cpu(tx_cmd_gen3->len); 777 } else { 778 struct iwl_tx_cmd_gen2 *tx_cmd_gen2 = 779 (void *)dev_cmd->payload; 780 781 cmd_len = le16_to_cpu(tx_cmd_gen2->len); 782 } 783 784 /* Set up entry for this TFD in Tx byte-count array */ 785 iwl_pcie_gen2_update_byte_tbl(trans, txq, cmd_len, 786 iwl_txq_gen2_get_num_tbs(trans, tfd)); 787 788 /* start timer if queue currently empty */ 789 if (txq->read_ptr == txq->write_ptr && txq->wd_timeout) 790 mod_timer(&txq->stuck_timer, jiffies + txq->wd_timeout); 791 792 /* Tell device the write index *just past* this latest filled TFD */ 793 txq->write_ptr = iwl_txq_inc_wrap(trans, txq->write_ptr); 794 iwl_txq_inc_wr_ptr(trans, txq); 795 /* 796 * At this point the frame is "transmitted" successfully 797 * and we will get a TX status notification eventually. 798 */ 799 spin_unlock(&txq->lock); 800 return 0; 801 } 802 803 /*************** HOST COMMAND QUEUE FUNCTIONS *****/ 804 805 /* 806 * iwl_txq_gen2_unmap - Unmap any remaining DMA mappings and free skb's 807 */ 808 void iwl_txq_gen2_unmap(struct iwl_trans *trans, int txq_id) 809 { 810 struct iwl_txq *txq = trans->txqs.txq[txq_id]; 811 812 spin_lock_bh(&txq->lock); 813 while (txq->write_ptr != txq->read_ptr) { 814 IWL_DEBUG_TX_REPLY(trans, "Q %d Free %d\n", 815 txq_id, txq->read_ptr); 816 817 if (txq_id != trans->txqs.cmd.q_id) { 818 int idx = iwl_txq_get_cmd_index(txq, txq->read_ptr); 819 struct sk_buff *skb = txq->entries[idx].skb; 820 821 if (!WARN_ON_ONCE(!skb)) 822 iwl_txq_free_tso_page(trans, skb); 823 } 824 iwl_txq_gen2_free_tfd(trans, txq); 825 txq->read_ptr = iwl_txq_inc_wrap(trans, txq->read_ptr); 826 } 827 828 while (!skb_queue_empty(&txq->overflow_q)) { 829 struct sk_buff *skb = __skb_dequeue(&txq->overflow_q); 830 831 iwl_op_mode_free_skb(trans->op_mode, skb); 832 } 833 834 spin_unlock_bh(&txq->lock); 835 836 /* just in case - this queue may have been stopped */ 837 iwl_wake_queue(trans, txq); 838 } 839 840 static void iwl_txq_gen2_free_memory(struct iwl_trans *trans, 841 struct iwl_txq *txq) 842 { 843 struct device *dev = trans->dev; 844 845 /* De-alloc circular buffer of TFDs */ 846 if (txq->tfds) { 847 dma_free_coherent(dev, 848 trans->txqs.tfd.size * txq->n_window, 849 txq->tfds, txq->dma_addr); 850 dma_free_coherent(dev, 851 sizeof(*txq->first_tb_bufs) * txq->n_window, 852 txq->first_tb_bufs, txq->first_tb_dma); 853 } 854 855 kfree(txq->entries); 856 if (txq->bc_tbl.addr) 857 dma_pool_free(trans->txqs.bc_pool, 858 txq->bc_tbl.addr, txq->bc_tbl.dma); 859 kfree(txq); 860 } 861 862 /* 863 * iwl_pcie_txq_free - Deallocate DMA queue. 864 * @txq: Transmit queue to deallocate. 865 * 866 * Empty queue by removing and destroying all BD's. 867 * Free all buffers. 868 * 0-fill, but do not free "txq" descriptor structure. 869 */ 870 static void iwl_txq_gen2_free(struct iwl_trans *trans, int txq_id) 871 { 872 struct iwl_txq *txq; 873 int i; 874 875 if (WARN_ONCE(txq_id >= IWL_MAX_TVQM_QUEUES, 876 "queue %d out of range", txq_id)) 877 return; 878 879 txq = trans->txqs.txq[txq_id]; 880 881 if (WARN_ON(!txq)) 882 return; 883 884 iwl_txq_gen2_unmap(trans, txq_id); 885 886 /* De-alloc array of command/tx buffers */ 887 if (txq_id == trans->txqs.cmd.q_id) 888 for (i = 0; i < txq->n_window; i++) { 889 kfree_sensitive(txq->entries[i].cmd); 890 kfree_sensitive(txq->entries[i].free_buf); 891 } 892 del_timer_sync(&txq->stuck_timer); 893 894 iwl_txq_gen2_free_memory(trans, txq); 895 896 trans->txqs.txq[txq_id] = NULL; 897 898 clear_bit(txq_id, trans->txqs.queue_used); 899 } 900 901 /* 902 * iwl_queue_init - Initialize queue's high/low-water and read/write indexes 903 */ 904 static int iwl_queue_init(struct iwl_txq *q, int slots_num) 905 { 906 q->n_window = slots_num; 907 908 /* slots_num must be power-of-two size, otherwise 909 * iwl_txq_get_cmd_index is broken. */ 910 if (WARN_ON(!is_power_of_2(slots_num))) 911 return -EINVAL; 912 913 q->low_mark = q->n_window / 4; 914 if (q->low_mark < 4) 915 q->low_mark = 4; 916 917 q->high_mark = q->n_window / 8; 918 if (q->high_mark < 2) 919 q->high_mark = 2; 920 921 q->write_ptr = 0; 922 q->read_ptr = 0; 923 924 return 0; 925 } 926 927 int iwl_txq_init(struct iwl_trans *trans, struct iwl_txq *txq, int slots_num, 928 bool cmd_queue) 929 { 930 int ret; 931 u32 tfd_queue_max_size = 932 trans->trans_cfg->base_params->max_tfd_queue_size; 933 934 txq->need_update = false; 935 936 /* max_tfd_queue_size must be power-of-two size, otherwise 937 * iwl_txq_inc_wrap and iwl_txq_dec_wrap are broken. */ 938 if (WARN_ONCE(tfd_queue_max_size & (tfd_queue_max_size - 1), 939 "Max tfd queue size must be a power of two, but is %d", 940 tfd_queue_max_size)) 941 return -EINVAL; 942 943 /* Initialize queue's high/low-water marks, and head/tail indexes */ 944 ret = iwl_queue_init(txq, slots_num); 945 if (ret) 946 return ret; 947 948 spin_lock_init(&txq->lock); 949 950 #ifdef CONFIG_LOCKDEP 951 if (cmd_queue) { 952 static struct lock_class_key iwl_txq_cmd_queue_lock_class; 953 954 lockdep_set_class(&txq->lock, &iwl_txq_cmd_queue_lock_class); 955 } 956 #endif 957 958 __skb_queue_head_init(&txq->overflow_q); 959 960 return 0; 961 } 962 963 void iwl_txq_free_tso_page(struct iwl_trans *trans, struct sk_buff *skb) 964 { 965 struct page **page_ptr; 966 struct page *next; 967 968 page_ptr = (void *)((u8 *)skb->cb + trans->txqs.page_offs); 969 next = *page_ptr; 970 *page_ptr = NULL; 971 972 while (next) { 973 struct page *tmp = next; 974 975 next = *(void **)((u8 *)page_address(next) + PAGE_SIZE - 976 sizeof(void *)); 977 __free_page(tmp); 978 } 979 } 980 981 void iwl_txq_log_scd_error(struct iwl_trans *trans, struct iwl_txq *txq) 982 { 983 u32 txq_id = txq->id; 984 u32 status; 985 bool active; 986 u8 fifo; 987 988 if (trans->trans_cfg->use_tfh) { 989 IWL_ERR(trans, "Queue %d is stuck %d %d\n", txq_id, 990 txq->read_ptr, txq->write_ptr); 991 /* TODO: access new SCD registers and dump them */ 992 return; 993 } 994 995 status = iwl_read_prph(trans, SCD_QUEUE_STATUS_BITS(txq_id)); 996 fifo = (status >> SCD_QUEUE_STTS_REG_POS_TXF) & 0x7; 997 active = !!(status & BIT(SCD_QUEUE_STTS_REG_POS_ACTIVE)); 998 999 IWL_ERR(trans, 1000 "Queue %d is %sactive on fifo %d and stuck for %u ms. SW [%d, %d] HW [%d, %d] FH TRB=0x0%x\n", 1001 txq_id, active ? "" : "in", fifo, 1002 jiffies_to_msecs(txq->wd_timeout), 1003 txq->read_ptr, txq->write_ptr, 1004 iwl_read_prph(trans, SCD_QUEUE_RDPTR(txq_id)) & 1005 (trans->trans_cfg->base_params->max_tfd_queue_size - 1), 1006 iwl_read_prph(trans, SCD_QUEUE_WRPTR(txq_id)) & 1007 (trans->trans_cfg->base_params->max_tfd_queue_size - 1), 1008 iwl_read_direct32(trans, FH_TX_TRB_REG(fifo))); 1009 } 1010 1011 static void iwl_txq_stuck_timer(struct timer_list *t) 1012 { 1013 struct iwl_txq *txq = from_timer(txq, t, stuck_timer); 1014 struct iwl_trans *trans = txq->trans; 1015 1016 spin_lock(&txq->lock); 1017 /* check if triggered erroneously */ 1018 if (txq->read_ptr == txq->write_ptr) { 1019 spin_unlock(&txq->lock); 1020 return; 1021 } 1022 spin_unlock(&txq->lock); 1023 1024 iwl_txq_log_scd_error(trans, txq); 1025 1026 iwl_force_nmi(trans); 1027 } 1028 1029 int iwl_txq_alloc(struct iwl_trans *trans, struct iwl_txq *txq, int slots_num, 1030 bool cmd_queue) 1031 { 1032 size_t tfd_sz = trans->txqs.tfd.size * 1033 trans->trans_cfg->base_params->max_tfd_queue_size; 1034 size_t tb0_buf_sz; 1035 int i; 1036 1037 if (WARN_ON(txq->entries || txq->tfds)) 1038 return -EINVAL; 1039 1040 if (trans->trans_cfg->use_tfh) 1041 tfd_sz = trans->txqs.tfd.size * slots_num; 1042 1043 timer_setup(&txq->stuck_timer, iwl_txq_stuck_timer, 0); 1044 txq->trans = trans; 1045 1046 txq->n_window = slots_num; 1047 1048 txq->entries = kcalloc(slots_num, 1049 sizeof(struct iwl_pcie_txq_entry), 1050 GFP_KERNEL); 1051 1052 if (!txq->entries) 1053 goto error; 1054 1055 if (cmd_queue) 1056 for (i = 0; i < slots_num; i++) { 1057 txq->entries[i].cmd = 1058 kmalloc(sizeof(struct iwl_device_cmd), 1059 GFP_KERNEL); 1060 if (!txq->entries[i].cmd) 1061 goto error; 1062 } 1063 1064 /* Circular buffer of transmit frame descriptors (TFDs), 1065 * shared with device */ 1066 txq->tfds = dma_alloc_coherent(trans->dev, tfd_sz, 1067 &txq->dma_addr, GFP_KERNEL); 1068 if (!txq->tfds) 1069 goto error; 1070 1071 BUILD_BUG_ON(sizeof(*txq->first_tb_bufs) != IWL_FIRST_TB_SIZE_ALIGN); 1072 1073 tb0_buf_sz = sizeof(*txq->first_tb_bufs) * slots_num; 1074 1075 txq->first_tb_bufs = dma_alloc_coherent(trans->dev, tb0_buf_sz, 1076 &txq->first_tb_dma, 1077 GFP_KERNEL); 1078 if (!txq->first_tb_bufs) 1079 goto err_free_tfds; 1080 1081 return 0; 1082 err_free_tfds: 1083 dma_free_coherent(trans->dev, tfd_sz, txq->tfds, txq->dma_addr); 1084 txq->tfds = NULL; 1085 error: 1086 if (txq->entries && cmd_queue) 1087 for (i = 0; i < slots_num; i++) 1088 kfree(txq->entries[i].cmd); 1089 kfree(txq->entries); 1090 txq->entries = NULL; 1091 1092 return -ENOMEM; 1093 } 1094 1095 static int iwl_txq_dyn_alloc_dma(struct iwl_trans *trans, 1096 struct iwl_txq **intxq, int size, 1097 unsigned int timeout) 1098 { 1099 size_t bc_tbl_size, bc_tbl_entries; 1100 struct iwl_txq *txq; 1101 int ret; 1102 1103 WARN_ON(!trans->txqs.bc_tbl_size); 1104 1105 bc_tbl_size = trans->txqs.bc_tbl_size; 1106 bc_tbl_entries = bc_tbl_size / sizeof(u16); 1107 1108 if (WARN_ON(size > bc_tbl_entries)) 1109 return -EINVAL; 1110 1111 txq = kzalloc(sizeof(*txq), GFP_KERNEL); 1112 if (!txq) 1113 return -ENOMEM; 1114 1115 txq->bc_tbl.addr = dma_pool_alloc(trans->txqs.bc_pool, GFP_KERNEL, 1116 &txq->bc_tbl.dma); 1117 if (!txq->bc_tbl.addr) { 1118 IWL_ERR(trans, "Scheduler BC Table allocation failed\n"); 1119 kfree(txq); 1120 return -ENOMEM; 1121 } 1122 1123 ret = iwl_txq_alloc(trans, txq, size, false); 1124 if (ret) { 1125 IWL_ERR(trans, "Tx queue alloc failed\n"); 1126 goto error; 1127 } 1128 ret = iwl_txq_init(trans, txq, size, false); 1129 if (ret) { 1130 IWL_ERR(trans, "Tx queue init failed\n"); 1131 goto error; 1132 } 1133 1134 txq->wd_timeout = msecs_to_jiffies(timeout); 1135 1136 *intxq = txq; 1137 return 0; 1138 1139 error: 1140 iwl_txq_gen2_free_memory(trans, txq); 1141 return ret; 1142 } 1143 1144 static int iwl_txq_alloc_response(struct iwl_trans *trans, struct iwl_txq *txq, 1145 struct iwl_host_cmd *hcmd) 1146 { 1147 struct iwl_tx_queue_cfg_rsp *rsp; 1148 int ret, qid; 1149 u32 wr_ptr; 1150 1151 if (WARN_ON(iwl_rx_packet_payload_len(hcmd->resp_pkt) != 1152 sizeof(*rsp))) { 1153 ret = -EINVAL; 1154 goto error_free_resp; 1155 } 1156 1157 rsp = (void *)hcmd->resp_pkt->data; 1158 qid = le16_to_cpu(rsp->queue_number); 1159 wr_ptr = le16_to_cpu(rsp->write_pointer); 1160 1161 if (qid >= ARRAY_SIZE(trans->txqs.txq)) { 1162 WARN_ONCE(1, "queue index %d unsupported", qid); 1163 ret = -EIO; 1164 goto error_free_resp; 1165 } 1166 1167 if (test_and_set_bit(qid, trans->txqs.queue_used)) { 1168 WARN_ONCE(1, "queue %d already used", qid); 1169 ret = -EIO; 1170 goto error_free_resp; 1171 } 1172 1173 if (WARN_ONCE(trans->txqs.txq[qid], 1174 "queue %d already allocated\n", qid)) { 1175 ret = -EIO; 1176 goto error_free_resp; 1177 } 1178 1179 txq->id = qid; 1180 trans->txqs.txq[qid] = txq; 1181 wr_ptr &= (trans->trans_cfg->base_params->max_tfd_queue_size - 1); 1182 1183 /* Place first TFD at index corresponding to start sequence number */ 1184 txq->read_ptr = wr_ptr; 1185 txq->write_ptr = wr_ptr; 1186 1187 IWL_DEBUG_TX_QUEUES(trans, "Activate queue %d\n", qid); 1188 1189 iwl_free_resp(hcmd); 1190 return qid; 1191 1192 error_free_resp: 1193 iwl_free_resp(hcmd); 1194 iwl_txq_gen2_free_memory(trans, txq); 1195 return ret; 1196 } 1197 1198 int iwl_txq_dyn_alloc(struct iwl_trans *trans, __le16 flags, u8 sta_id, u8 tid, 1199 int cmd_id, int size, unsigned int timeout) 1200 { 1201 struct iwl_txq *txq = NULL; 1202 struct iwl_tx_queue_cfg_cmd cmd = { 1203 .flags = flags, 1204 .sta_id = sta_id, 1205 .tid = tid, 1206 }; 1207 struct iwl_host_cmd hcmd = { 1208 .id = cmd_id, 1209 .len = { sizeof(cmd) }, 1210 .data = { &cmd, }, 1211 .flags = CMD_WANT_SKB, 1212 }; 1213 int ret; 1214 1215 ret = iwl_txq_dyn_alloc_dma(trans, &txq, size, timeout); 1216 if (ret) 1217 return ret; 1218 1219 cmd.tfdq_addr = cpu_to_le64(txq->dma_addr); 1220 cmd.byte_cnt_addr = cpu_to_le64(txq->bc_tbl.dma); 1221 cmd.cb_size = cpu_to_le32(TFD_QUEUE_CB_SIZE(size)); 1222 1223 ret = iwl_trans_send_cmd(trans, &hcmd); 1224 if (ret) 1225 goto error; 1226 1227 return iwl_txq_alloc_response(trans, txq, &hcmd); 1228 1229 error: 1230 iwl_txq_gen2_free_memory(trans, txq); 1231 return ret; 1232 } 1233 1234 void iwl_txq_dyn_free(struct iwl_trans *trans, int queue) 1235 { 1236 if (WARN(queue >= IWL_MAX_TVQM_QUEUES, 1237 "queue %d out of range", queue)) 1238 return; 1239 1240 /* 1241 * Upon HW Rfkill - we stop the device, and then stop the queues 1242 * in the op_mode. Just for the sake of the simplicity of the op_mode, 1243 * allow the op_mode to call txq_disable after it already called 1244 * stop_device. 1245 */ 1246 if (!test_and_clear_bit(queue, trans->txqs.queue_used)) { 1247 WARN_ONCE(test_bit(STATUS_DEVICE_ENABLED, &trans->status), 1248 "queue %d not used", queue); 1249 return; 1250 } 1251 1252 iwl_txq_gen2_free(trans, queue); 1253 1254 IWL_DEBUG_TX_QUEUES(trans, "Deactivate queue %d\n", queue); 1255 } 1256 1257 void iwl_txq_gen2_tx_free(struct iwl_trans *trans) 1258 { 1259 int i; 1260 1261 memset(trans->txqs.queue_used, 0, sizeof(trans->txqs.queue_used)); 1262 1263 /* Free all TX queues */ 1264 for (i = 0; i < ARRAY_SIZE(trans->txqs.txq); i++) { 1265 if (!trans->txqs.txq[i]) 1266 continue; 1267 1268 iwl_txq_gen2_free(trans, i); 1269 } 1270 } 1271 1272 int iwl_txq_gen2_init(struct iwl_trans *trans, int txq_id, int queue_size) 1273 { 1274 struct iwl_txq *queue; 1275 int ret; 1276 1277 /* alloc and init the tx queue */ 1278 if (!trans->txqs.txq[txq_id]) { 1279 queue = kzalloc(sizeof(*queue), GFP_KERNEL); 1280 if (!queue) { 1281 IWL_ERR(trans, "Not enough memory for tx queue\n"); 1282 return -ENOMEM; 1283 } 1284 trans->txqs.txq[txq_id] = queue; 1285 ret = iwl_txq_alloc(trans, queue, queue_size, true); 1286 if (ret) { 1287 IWL_ERR(trans, "Tx %d queue init failed\n", txq_id); 1288 goto error; 1289 } 1290 } else { 1291 queue = trans->txqs.txq[txq_id]; 1292 } 1293 1294 ret = iwl_txq_init(trans, queue, queue_size, 1295 (txq_id == trans->txqs.cmd.q_id)); 1296 if (ret) { 1297 IWL_ERR(trans, "Tx %d queue alloc failed\n", txq_id); 1298 goto error; 1299 } 1300 trans->txqs.txq[txq_id]->id = txq_id; 1301 set_bit(txq_id, trans->txqs.queue_used); 1302 1303 return 0; 1304 1305 error: 1306 iwl_txq_gen2_tx_free(trans); 1307 return ret; 1308 } 1309 1310 static inline dma_addr_t iwl_txq_gen1_tfd_tb_get_addr(struct iwl_trans *trans, 1311 void *_tfd, u8 idx) 1312 { 1313 struct iwl_tfd *tfd; 1314 struct iwl_tfd_tb *tb; 1315 dma_addr_t addr; 1316 dma_addr_t hi_len; 1317 1318 if (trans->trans_cfg->use_tfh) { 1319 struct iwl_tfh_tfd *tfd = _tfd; 1320 struct iwl_tfh_tb *tb = &tfd->tbs[idx]; 1321 1322 return (dma_addr_t)(le64_to_cpu(tb->addr)); 1323 } 1324 1325 tfd = _tfd; 1326 tb = &tfd->tbs[idx]; 1327 addr = get_unaligned_le32(&tb->lo); 1328 1329 if (sizeof(dma_addr_t) <= sizeof(u32)) 1330 return addr; 1331 1332 hi_len = le16_to_cpu(tb->hi_n_len) & 0xF; 1333 1334 /* 1335 * shift by 16 twice to avoid warnings on 32-bit 1336 * (where this code never runs anyway due to the 1337 * if statement above) 1338 */ 1339 return addr | ((hi_len << 16) << 16); 1340 } 1341 1342 void iwl_txq_gen1_tfd_unmap(struct iwl_trans *trans, 1343 struct iwl_cmd_meta *meta, 1344 struct iwl_txq *txq, int index) 1345 { 1346 int i, num_tbs; 1347 void *tfd = iwl_txq_get_tfd(trans, txq, index); 1348 1349 /* Sanity check on number of chunks */ 1350 num_tbs = iwl_txq_gen1_tfd_get_num_tbs(trans, tfd); 1351 1352 if (num_tbs > trans->txqs.tfd.max_tbs) { 1353 IWL_ERR(trans, "Too many chunks: %i\n", num_tbs); 1354 /* @todo issue fatal error, it is quite serious situation */ 1355 return; 1356 } 1357 1358 /* first TB is never freed - it's the bidirectional DMA data */ 1359 1360 for (i = 1; i < num_tbs; i++) { 1361 if (meta->tbs & BIT(i)) 1362 dma_unmap_page(trans->dev, 1363 iwl_txq_gen1_tfd_tb_get_addr(trans, 1364 tfd, i), 1365 iwl_txq_gen1_tfd_tb_get_len(trans, 1366 tfd, i), 1367 DMA_TO_DEVICE); 1368 else 1369 dma_unmap_single(trans->dev, 1370 iwl_txq_gen1_tfd_tb_get_addr(trans, 1371 tfd, i), 1372 iwl_txq_gen1_tfd_tb_get_len(trans, 1373 tfd, i), 1374 DMA_TO_DEVICE); 1375 } 1376 1377 meta->tbs = 0; 1378 1379 if (trans->trans_cfg->use_tfh) { 1380 struct iwl_tfh_tfd *tfd_fh = (void *)tfd; 1381 1382 tfd_fh->num_tbs = 0; 1383 } else { 1384 struct iwl_tfd *tfd_fh = (void *)tfd; 1385 1386 tfd_fh->num_tbs = 0; 1387 } 1388 } 1389 1390 #define IWL_TX_CRC_SIZE 4 1391 #define IWL_TX_DELIMITER_SIZE 4 1392 1393 /* 1394 * iwl_txq_gen1_update_byte_cnt_tbl - Set up entry in Tx byte-count array 1395 */ 1396 void iwl_txq_gen1_update_byte_cnt_tbl(struct iwl_trans *trans, 1397 struct iwl_txq *txq, u16 byte_cnt, 1398 int num_tbs) 1399 { 1400 struct iwlagn_scd_bc_tbl *scd_bc_tbl; 1401 int write_ptr = txq->write_ptr; 1402 int txq_id = txq->id; 1403 u8 sec_ctl = 0; 1404 u16 len = byte_cnt + IWL_TX_CRC_SIZE + IWL_TX_DELIMITER_SIZE; 1405 __le16 bc_ent; 1406 struct iwl_device_tx_cmd *dev_cmd = txq->entries[txq->write_ptr].cmd; 1407 struct iwl_tx_cmd *tx_cmd = (void *)dev_cmd->payload; 1408 u8 sta_id = tx_cmd->sta_id; 1409 1410 scd_bc_tbl = trans->txqs.scd_bc_tbls.addr; 1411 1412 sec_ctl = tx_cmd->sec_ctl; 1413 1414 switch (sec_ctl & TX_CMD_SEC_MSK) { 1415 case TX_CMD_SEC_CCM: 1416 len += IEEE80211_CCMP_MIC_LEN; 1417 break; 1418 case TX_CMD_SEC_TKIP: 1419 len += IEEE80211_TKIP_ICV_LEN; 1420 break; 1421 case TX_CMD_SEC_WEP: 1422 len += IEEE80211_WEP_IV_LEN + IEEE80211_WEP_ICV_LEN; 1423 break; 1424 } 1425 if (trans->txqs.bc_table_dword) 1426 len = DIV_ROUND_UP(len, 4); 1427 1428 if (WARN_ON(len > 0xFFF || write_ptr >= TFD_QUEUE_SIZE_MAX)) 1429 return; 1430 1431 bc_ent = cpu_to_le16(len | (sta_id << 12)); 1432 1433 scd_bc_tbl[txq_id].tfd_offset[write_ptr] = bc_ent; 1434 1435 if (write_ptr < TFD_QUEUE_SIZE_BC_DUP) 1436 scd_bc_tbl[txq_id].tfd_offset[TFD_QUEUE_SIZE_MAX + write_ptr] = 1437 bc_ent; 1438 } 1439 1440 void iwl_txq_gen1_inval_byte_cnt_tbl(struct iwl_trans *trans, 1441 struct iwl_txq *txq) 1442 { 1443 struct iwlagn_scd_bc_tbl *scd_bc_tbl = trans->txqs.scd_bc_tbls.addr; 1444 int txq_id = txq->id; 1445 int read_ptr = txq->read_ptr; 1446 u8 sta_id = 0; 1447 __le16 bc_ent; 1448 struct iwl_device_tx_cmd *dev_cmd = txq->entries[read_ptr].cmd; 1449 struct iwl_tx_cmd *tx_cmd = (void *)dev_cmd->payload; 1450 1451 WARN_ON(read_ptr >= TFD_QUEUE_SIZE_MAX); 1452 1453 if (txq_id != trans->txqs.cmd.q_id) 1454 sta_id = tx_cmd->sta_id; 1455 1456 bc_ent = cpu_to_le16(1 | (sta_id << 12)); 1457 1458 scd_bc_tbl[txq_id].tfd_offset[read_ptr] = bc_ent; 1459 1460 if (read_ptr < TFD_QUEUE_SIZE_BC_DUP) 1461 scd_bc_tbl[txq_id].tfd_offset[TFD_QUEUE_SIZE_MAX + read_ptr] = 1462 bc_ent; 1463 } 1464 1465 /* 1466 * iwl_txq_free_tfd - Free all chunks referenced by TFD [txq->q.read_ptr] 1467 * @trans - transport private data 1468 * @txq - tx queue 1469 * @dma_dir - the direction of the DMA mapping 1470 * 1471 * Does NOT advance any TFD circular buffer read/write indexes 1472 * Does NOT free the TFD itself (which is within circular buffer) 1473 */ 1474 void iwl_txq_free_tfd(struct iwl_trans *trans, struct iwl_txq *txq) 1475 { 1476 /* rd_ptr is bounded by TFD_QUEUE_SIZE_MAX and 1477 * idx is bounded by n_window 1478 */ 1479 int rd_ptr = txq->read_ptr; 1480 int idx = iwl_txq_get_cmd_index(txq, rd_ptr); 1481 struct sk_buff *skb; 1482 1483 lockdep_assert_held(&txq->lock); 1484 1485 if (!txq->entries) 1486 return; 1487 1488 /* We have only q->n_window txq->entries, but we use 1489 * TFD_QUEUE_SIZE_MAX tfds 1490 */ 1491 iwl_txq_gen1_tfd_unmap(trans, &txq->entries[idx].meta, txq, rd_ptr); 1492 1493 /* free SKB */ 1494 skb = txq->entries[idx].skb; 1495 1496 /* Can be called from irqs-disabled context 1497 * If skb is not NULL, it means that the whole queue is being 1498 * freed and that the queue is not empty - free the skb 1499 */ 1500 if (skb) { 1501 iwl_op_mode_free_skb(trans->op_mode, skb); 1502 txq->entries[idx].skb = NULL; 1503 } 1504 } 1505 1506 void iwl_txq_progress(struct iwl_txq *txq) 1507 { 1508 lockdep_assert_held(&txq->lock); 1509 1510 if (!txq->wd_timeout) 1511 return; 1512 1513 /* 1514 * station is asleep and we send data - that must 1515 * be uAPSD or PS-Poll. Don't rearm the timer. 1516 */ 1517 if (txq->frozen) 1518 return; 1519 1520 /* 1521 * if empty delete timer, otherwise move timer forward 1522 * since we're making progress on this queue 1523 */ 1524 if (txq->read_ptr == txq->write_ptr) 1525 del_timer(&txq->stuck_timer); 1526 else 1527 mod_timer(&txq->stuck_timer, jiffies + txq->wd_timeout); 1528 } 1529 1530 /* Frees buffers until index _not_ inclusive */ 1531 void iwl_txq_reclaim(struct iwl_trans *trans, int txq_id, int ssn, 1532 struct sk_buff_head *skbs) 1533 { 1534 struct iwl_txq *txq = trans->txqs.txq[txq_id]; 1535 int tfd_num = iwl_txq_get_cmd_index(txq, ssn); 1536 int read_ptr = iwl_txq_get_cmd_index(txq, txq->read_ptr); 1537 int last_to_free; 1538 1539 /* This function is not meant to release cmd queue*/ 1540 if (WARN_ON(txq_id == trans->txqs.cmd.q_id)) 1541 return; 1542 1543 spin_lock_bh(&txq->lock); 1544 1545 if (!test_bit(txq_id, trans->txqs.queue_used)) { 1546 IWL_DEBUG_TX_QUEUES(trans, "Q %d inactive - ignoring idx %d\n", 1547 txq_id, ssn); 1548 goto out; 1549 } 1550 1551 if (read_ptr == tfd_num) 1552 goto out; 1553 1554 IWL_DEBUG_TX_REPLY(trans, "[Q %d] %d -> %d (%d)\n", 1555 txq_id, txq->read_ptr, tfd_num, ssn); 1556 1557 /*Since we free until index _not_ inclusive, the one before index is 1558 * the last we will free. This one must be used */ 1559 last_to_free = iwl_txq_dec_wrap(trans, tfd_num); 1560 1561 if (!iwl_txq_used(txq, last_to_free)) { 1562 IWL_ERR(trans, 1563 "%s: Read index for txq id (%d), last_to_free %d is out of range [0-%d] %d %d.\n", 1564 __func__, txq_id, last_to_free, 1565 trans->trans_cfg->base_params->max_tfd_queue_size, 1566 txq->write_ptr, txq->read_ptr); 1567 1568 iwl_op_mode_time_point(trans->op_mode, 1569 IWL_FW_INI_TIME_POINT_FAKE_TX, 1570 NULL); 1571 goto out; 1572 } 1573 1574 if (WARN_ON(!skb_queue_empty(skbs))) 1575 goto out; 1576 1577 for (; 1578 read_ptr != tfd_num; 1579 txq->read_ptr = iwl_txq_inc_wrap(trans, txq->read_ptr), 1580 read_ptr = iwl_txq_get_cmd_index(txq, txq->read_ptr)) { 1581 struct sk_buff *skb = txq->entries[read_ptr].skb; 1582 1583 if (WARN_ON_ONCE(!skb)) 1584 continue; 1585 1586 iwl_txq_free_tso_page(trans, skb); 1587 1588 __skb_queue_tail(skbs, skb); 1589 1590 txq->entries[read_ptr].skb = NULL; 1591 1592 if (!trans->trans_cfg->use_tfh) 1593 iwl_txq_gen1_inval_byte_cnt_tbl(trans, txq); 1594 1595 iwl_txq_free_tfd(trans, txq); 1596 } 1597 1598 iwl_txq_progress(txq); 1599 1600 if (iwl_txq_space(trans, txq) > txq->low_mark && 1601 test_bit(txq_id, trans->txqs.queue_stopped)) { 1602 struct sk_buff_head overflow_skbs; 1603 1604 __skb_queue_head_init(&overflow_skbs); 1605 skb_queue_splice_init(&txq->overflow_q, &overflow_skbs); 1606 1607 /* 1608 * We are going to transmit from the overflow queue. 1609 * Remember this state so that wait_for_txq_empty will know we 1610 * are adding more packets to the TFD queue. It cannot rely on 1611 * the state of &txq->overflow_q, as we just emptied it, but 1612 * haven't TXed the content yet. 1613 */ 1614 txq->overflow_tx = true; 1615 1616 /* 1617 * This is tricky: we are in reclaim path which is non 1618 * re-entrant, so noone will try to take the access the 1619 * txq data from that path. We stopped tx, so we can't 1620 * have tx as well. Bottom line, we can unlock and re-lock 1621 * later. 1622 */ 1623 spin_unlock_bh(&txq->lock); 1624 1625 while (!skb_queue_empty(&overflow_skbs)) { 1626 struct sk_buff *skb = __skb_dequeue(&overflow_skbs); 1627 struct iwl_device_tx_cmd *dev_cmd_ptr; 1628 1629 dev_cmd_ptr = *(void **)((u8 *)skb->cb + 1630 trans->txqs.dev_cmd_offs); 1631 1632 /* 1633 * Note that we can very well be overflowing again. 1634 * In that case, iwl_txq_space will be small again 1635 * and we won't wake mac80211's queue. 1636 */ 1637 iwl_trans_tx(trans, skb, dev_cmd_ptr, txq_id); 1638 } 1639 1640 if (iwl_txq_space(trans, txq) > txq->low_mark) 1641 iwl_wake_queue(trans, txq); 1642 1643 spin_lock_bh(&txq->lock); 1644 txq->overflow_tx = false; 1645 } 1646 1647 out: 1648 spin_unlock_bh(&txq->lock); 1649 } 1650 1651 /* Set wr_ptr of specific device and txq */ 1652 void iwl_txq_set_q_ptrs(struct iwl_trans *trans, int txq_id, int ptr) 1653 { 1654 struct iwl_txq *txq = trans->txqs.txq[txq_id]; 1655 1656 spin_lock_bh(&txq->lock); 1657 1658 txq->write_ptr = ptr; 1659 txq->read_ptr = txq->write_ptr; 1660 1661 spin_unlock_bh(&txq->lock); 1662 } 1663 1664 void iwl_trans_txq_freeze_timer(struct iwl_trans *trans, unsigned long txqs, 1665 bool freeze) 1666 { 1667 int queue; 1668 1669 for_each_set_bit(queue, &txqs, BITS_PER_LONG) { 1670 struct iwl_txq *txq = trans->txqs.txq[queue]; 1671 unsigned long now; 1672 1673 spin_lock_bh(&txq->lock); 1674 1675 now = jiffies; 1676 1677 if (txq->frozen == freeze) 1678 goto next_queue; 1679 1680 IWL_DEBUG_TX_QUEUES(trans, "%s TXQ %d\n", 1681 freeze ? "Freezing" : "Waking", queue); 1682 1683 txq->frozen = freeze; 1684 1685 if (txq->read_ptr == txq->write_ptr) 1686 goto next_queue; 1687 1688 if (freeze) { 1689 if (unlikely(time_after(now, 1690 txq->stuck_timer.expires))) { 1691 /* 1692 * The timer should have fired, maybe it is 1693 * spinning right now on the lock. 1694 */ 1695 goto next_queue; 1696 } 1697 /* remember how long until the timer fires */ 1698 txq->frozen_expiry_remainder = 1699 txq->stuck_timer.expires - now; 1700 del_timer(&txq->stuck_timer); 1701 goto next_queue; 1702 } 1703 1704 /* 1705 * Wake a non-empty queue -> arm timer with the 1706 * remainder before it froze 1707 */ 1708 mod_timer(&txq->stuck_timer, 1709 now + txq->frozen_expiry_remainder); 1710 1711 next_queue: 1712 spin_unlock_bh(&txq->lock); 1713 } 1714 } 1715 1716 #define HOST_COMPLETE_TIMEOUT (2 * HZ) 1717 1718 static int iwl_trans_txq_send_hcmd_sync(struct iwl_trans *trans, 1719 struct iwl_host_cmd *cmd) 1720 { 1721 const char *cmd_str = iwl_get_cmd_string(trans, cmd->id); 1722 struct iwl_txq *txq = trans->txqs.txq[trans->txqs.cmd.q_id]; 1723 int cmd_idx; 1724 int ret; 1725 1726 IWL_DEBUG_INFO(trans, "Attempting to send sync command %s\n", cmd_str); 1727 1728 if (WARN(test_and_set_bit(STATUS_SYNC_HCMD_ACTIVE, 1729 &trans->status), 1730 "Command %s: a command is already active!\n", cmd_str)) 1731 return -EIO; 1732 1733 IWL_DEBUG_INFO(trans, "Setting HCMD_ACTIVE for command %s\n", cmd_str); 1734 1735 cmd_idx = trans->ops->send_cmd(trans, cmd); 1736 if (cmd_idx < 0) { 1737 ret = cmd_idx; 1738 clear_bit(STATUS_SYNC_HCMD_ACTIVE, &trans->status); 1739 IWL_ERR(trans, "Error sending %s: enqueue_hcmd failed: %d\n", 1740 cmd_str, ret); 1741 return ret; 1742 } 1743 1744 ret = wait_event_timeout(trans->wait_command_queue, 1745 !test_bit(STATUS_SYNC_HCMD_ACTIVE, 1746 &trans->status), 1747 HOST_COMPLETE_TIMEOUT); 1748 if (!ret) { 1749 IWL_ERR(trans, "Error sending %s: time out after %dms.\n", 1750 cmd_str, jiffies_to_msecs(HOST_COMPLETE_TIMEOUT)); 1751 1752 IWL_ERR(trans, "Current CMD queue read_ptr %d write_ptr %d\n", 1753 txq->read_ptr, txq->write_ptr); 1754 1755 clear_bit(STATUS_SYNC_HCMD_ACTIVE, &trans->status); 1756 IWL_DEBUG_INFO(trans, "Clearing HCMD_ACTIVE for command %s\n", 1757 cmd_str); 1758 ret = -ETIMEDOUT; 1759 1760 iwl_trans_sync_nmi(trans); 1761 goto cancel; 1762 } 1763 1764 if (test_bit(STATUS_FW_ERROR, &trans->status)) { 1765 IWL_ERR(trans, "FW error in SYNC CMD %s\n", cmd_str); 1766 dump_stack(); 1767 ret = -EIO; 1768 goto cancel; 1769 } 1770 1771 if (!(cmd->flags & CMD_SEND_IN_RFKILL) && 1772 test_bit(STATUS_RFKILL_OPMODE, &trans->status)) { 1773 IWL_DEBUG_RF_KILL(trans, "RFKILL in SYNC CMD... no rsp\n"); 1774 ret = -ERFKILL; 1775 goto cancel; 1776 } 1777 1778 if ((cmd->flags & CMD_WANT_SKB) && !cmd->resp_pkt) { 1779 IWL_ERR(trans, "Error: Response NULL in '%s'\n", cmd_str); 1780 ret = -EIO; 1781 goto cancel; 1782 } 1783 1784 return 0; 1785 1786 cancel: 1787 if (cmd->flags & CMD_WANT_SKB) { 1788 /* 1789 * Cancel the CMD_WANT_SKB flag for the cmd in the 1790 * TX cmd queue. Otherwise in case the cmd comes 1791 * in later, it will possibly set an invalid 1792 * address (cmd->meta.source). 1793 */ 1794 txq->entries[cmd_idx].meta.flags &= ~CMD_WANT_SKB; 1795 } 1796 1797 if (cmd->resp_pkt) { 1798 iwl_free_resp(cmd); 1799 cmd->resp_pkt = NULL; 1800 } 1801 1802 return ret; 1803 } 1804 1805 int iwl_trans_txq_send_hcmd(struct iwl_trans *trans, 1806 struct iwl_host_cmd *cmd) 1807 { 1808 /* Make sure the NIC is still alive in the bus */ 1809 if (test_bit(STATUS_TRANS_DEAD, &trans->status)) 1810 return -ENODEV; 1811 1812 if (!(cmd->flags & CMD_SEND_IN_RFKILL) && 1813 test_bit(STATUS_RFKILL_OPMODE, &trans->status)) { 1814 IWL_DEBUG_RF_KILL(trans, "Dropping CMD 0x%x: RF KILL\n", 1815 cmd->id); 1816 return -ERFKILL; 1817 } 1818 1819 if (unlikely(trans->system_pm_mode == IWL_PLAT_PM_MODE_D3 && 1820 !(cmd->flags & CMD_SEND_IN_D3))) { 1821 IWL_DEBUG_WOWLAN(trans, "Dropping CMD 0x%x: D3\n", cmd->id); 1822 return -EHOSTDOWN; 1823 } 1824 1825 if (cmd->flags & CMD_ASYNC) { 1826 int ret; 1827 1828 /* An asynchronous command can not expect an SKB to be set. */ 1829 if (WARN_ON(cmd->flags & CMD_WANT_SKB)) 1830 return -EINVAL; 1831 1832 ret = trans->ops->send_cmd(trans, cmd); 1833 if (ret < 0) { 1834 IWL_ERR(trans, 1835 "Error sending %s: enqueue_hcmd failed: %d\n", 1836 iwl_get_cmd_string(trans, cmd->id), ret); 1837 return ret; 1838 } 1839 return 0; 1840 } 1841 1842 return iwl_trans_txq_send_hcmd_sync(trans, cmd); 1843 } 1844 1845