xref: /linux/drivers/net/wireless/intel/iwlwifi/pcie/gen1_2/tx.c (revision 91ec2035134982b98fab0609a9fd8480e8217dc1)
1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
2 /*
3  * Copyright (C) 2003-2014, 2018-2021, 2023-2026 Intel Corporation
4  * Copyright (C) 2013-2015 Intel Mobile Communications GmbH
5  * Copyright (C) 2016-2017 Intel Deutschland GmbH
6  */
7 #include <linux/etherdevice.h>
8 #include <linux/ieee80211.h>
9 #include <linux/dmapool.h>
10 #include <linux/slab.h>
11 #include <linux/sched.h>
12 #include <linux/tcp.h>
13 #include <net/ip6_checksum.h>
14 #include <net/tso.h>
15 
16 #include "fw/api/commands.h"
17 #include "fw/api/datapath.h"
18 #include "fw/api/debug.h"
19 #include "iwl-fh.h"
20 #include "iwl-debug.h"
21 #include "iwl-csr.h"
22 #include "iwl-prph.h"
23 #include "iwl-io.h"
24 #include "iwl-scd.h"
25 #include "iwl-op-mode.h"
26 #include "internal.h"
27 #include "fw/api/tx.h"
28 #include "fw/dbg.h"
29 #include "pcie/utils.h"
30 
31 /*************** DMA-QUEUE-GENERAL-FUNCTIONS  *****
32  * DMA services
33  *
34  * Theory of operation
35  *
36  * A Tx or Rx queue resides in host DRAM, and is comprised of a circular buffer
37  * of buffer descriptors, each of which points to one or more data buffers for
38  * the device to read from or fill.  Driver and device exchange status of each
39  * queue via "read" and "write" pointers.  Driver keeps minimum of 2 empty
40  * entries in each circular buffer, to protect against confusing empty and full
41  * queue states.
42  *
43  * The device reads or writes the data in the queues via the device's several
44  * DMA/FIFO channels.  Each queue is mapped to a single DMA channel.
45  *
46  * For Tx queue, there are low mark and high mark limits. If, after queuing
47  * the packet for Tx, free space become < low mark, Tx queue stopped. When
48  * reclaiming packets (on 'tx done IRQ), if free space become > high mark,
49  * Tx queue resumed.
50  *
51  ***************************************************/
52 
53 
iwl_pcie_alloc_dma_ptr(struct iwl_trans * trans,struct iwl_dma_ptr * ptr,size_t size)54 int iwl_pcie_alloc_dma_ptr(struct iwl_trans *trans,
55 			   struct iwl_dma_ptr *ptr, size_t size)
56 {
57 	if (WARN_ON(ptr->addr))
58 		return -EINVAL;
59 
60 	ptr->addr = dma_alloc_coherent(trans->dev, size,
61 				       &ptr->dma, GFP_KERNEL);
62 	if (!ptr->addr)
63 		return -ENOMEM;
64 	ptr->size = size;
65 	return 0;
66 }
67 
iwl_pcie_free_dma_ptr(struct iwl_trans * trans,struct iwl_dma_ptr * ptr)68 void iwl_pcie_free_dma_ptr(struct iwl_trans *trans, struct iwl_dma_ptr *ptr)
69 {
70 	if (unlikely(!ptr->addr))
71 		return;
72 
73 	dma_free_coherent(trans->dev, ptr->size, ptr->addr, ptr->dma);
74 	memset(ptr, 0, sizeof(*ptr));
75 }
76 
77 /*
78  * iwl_pcie_txq_inc_wr_ptr - Send new write index to hardware
79  */
iwl_pcie_txq_inc_wr_ptr(struct iwl_trans * trans,struct iwl_txq * txq)80 static void iwl_pcie_txq_inc_wr_ptr(struct iwl_trans *trans,
81 				    struct iwl_txq *txq)
82 {
83 	u32 reg = 0;
84 	int txq_id = txq->id;
85 
86 	lockdep_assert_held(&txq->lock);
87 
88 	/*
89 	 * explicitly wake up the NIC if:
90 	 * 1. shadow registers aren't enabled
91 	 * 2. NIC is woken up for CMD regardless of shadow outside this function
92 	 * 3. there is a chance that the NIC is asleep
93 	 */
94 	if (!trans->mac_cfg->base->shadow_reg_enable &&
95 	    txq_id != trans->conf.cmd_queue &&
96 	    test_bit(STATUS_TPOWER_PMI, &trans->status)) {
97 		/*
98 		 * wake up nic if it's powered down ...
99 		 * uCode will wake up, and interrupt us again, so next
100 		 * time we'll skip this part.
101 		 */
102 		reg = iwl_read32(trans, CSR_UCODE_DRV_GP1);
103 
104 		if (reg & CSR_UCODE_DRV_GP1_BIT_MAC_SLEEP) {
105 			IWL_DEBUG_INFO(trans, "Tx queue %d requesting wakeup, GP1 = 0x%x\n",
106 				       txq_id, reg);
107 			iwl_set_bit(trans, CSR_GP_CNTRL,
108 				    CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
109 			txq->need_update = true;
110 			return;
111 		}
112 	}
113 
114 	/*
115 	 * if not in power-save mode, uCode will never sleep when we're
116 	 * trying to tx (during RFKILL, we're not trying to tx).
117 	 */
118 	IWL_DEBUG_TX(trans, "Q:%d WR: 0x%x\n", txq_id, txq->write_ptr);
119 	if (!txq->block)
120 		iwl_write32(trans, HBUS_TARG_WRPTR,
121 			    txq->write_ptr | (txq_id << 8));
122 }
123 
iwl_pcie_txq_check_wrptrs(struct iwl_trans * trans)124 void iwl_pcie_txq_check_wrptrs(struct iwl_trans *trans)
125 {
126 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
127 	int i;
128 
129 	for (i = 0; i < trans->mac_cfg->base->num_of_queues; i++) {
130 		struct iwl_txq *txq = trans_pcie->txqs.txq[i];
131 
132 		if (!test_bit(i, trans_pcie->txqs.queue_used))
133 			continue;
134 
135 		spin_lock_bh(&txq->lock);
136 		if (txq->need_update) {
137 			iwl_pcie_txq_inc_wr_ptr(trans, txq);
138 			txq->need_update = false;
139 		}
140 		spin_unlock_bh(&txq->lock);
141 	}
142 }
143 
iwl_pcie_gen1_tfd_set_tb(struct iwl_tfd * tfd,u8 idx,dma_addr_t addr,u16 len)144 static inline void iwl_pcie_gen1_tfd_set_tb(struct iwl_tfd *tfd,
145 					    u8 idx, dma_addr_t addr, u16 len)
146 {
147 	struct iwl_tfd_tb *tb = &tfd->tbs[idx];
148 	u16 hi_n_len = len << 4;
149 
150 	put_unaligned_le32(addr, &tb->lo);
151 	hi_n_len |= iwl_get_dma_hi_addr(addr);
152 
153 	tb->hi_n_len = cpu_to_le16(hi_n_len);
154 
155 	tfd->num_tbs = idx + 1;
156 }
157 
iwl_txq_gen1_tfd_get_num_tbs(struct iwl_tfd * tfd)158 static inline u8 iwl_txq_gen1_tfd_get_num_tbs(struct iwl_tfd *tfd)
159 {
160 	return tfd->num_tbs & 0x1f;
161 }
162 
iwl_pcie_txq_build_tfd(struct iwl_trans * trans,struct iwl_txq * txq,dma_addr_t addr,u16 len,bool reset)163 static int iwl_pcie_txq_build_tfd(struct iwl_trans *trans, struct iwl_txq *txq,
164 				  dma_addr_t addr, u16 len, bool reset)
165 {
166 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
167 	void *tfd;
168 	u32 num_tbs;
169 
170 	tfd = (u8 *)txq->tfds + trans_pcie->txqs.tfd.size * txq->write_ptr;
171 
172 	if (reset)
173 		memset(tfd, 0, trans_pcie->txqs.tfd.size);
174 
175 	num_tbs = iwl_txq_gen1_tfd_get_num_tbs(tfd);
176 
177 	/* Each TFD can point to a maximum max_tbs Tx buffers */
178 	if (num_tbs >= trans_pcie->txqs.tfd.max_tbs) {
179 		IWL_ERR(trans, "Error can not send more than %d chunks\n",
180 			trans_pcie->txqs.tfd.max_tbs);
181 		return -EINVAL;
182 	}
183 
184 	if (WARN(addr & ~IWL_TX_DMA_MASK,
185 		 "Unaligned address = %llx\n", (unsigned long long)addr))
186 		return -EINVAL;
187 
188 	iwl_pcie_gen1_tfd_set_tb(tfd, num_tbs, addr, len);
189 
190 	return num_tbs;
191 }
192 
iwl_pcie_clear_cmd_in_flight(struct iwl_trans * trans)193 static void iwl_pcie_clear_cmd_in_flight(struct iwl_trans *trans)
194 {
195 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
196 
197 	if (!trans->mac_cfg->base->apmg_wake_up_wa)
198 		return;
199 
200 	spin_lock(&trans_pcie->reg_lock);
201 
202 	if (WARN_ON(!trans_pcie->cmd_hold_nic_awake)) {
203 		spin_unlock(&trans_pcie->reg_lock);
204 		return;
205 	}
206 
207 	trans_pcie->cmd_hold_nic_awake = false;
208 	iwl_trans_clear_bit(trans, CSR_GP_CNTRL,
209 			    CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
210 	spin_unlock(&trans_pcie->reg_lock);
211 }
212 
iwl_pcie_free_and_unmap_tso_page(struct iwl_trans * trans,struct page * page)213 static void iwl_pcie_free_and_unmap_tso_page(struct iwl_trans *trans,
214 					     struct page *page)
215 {
216 	struct iwl_tso_page_info *info = IWL_TSO_PAGE_INFO(page_address(page));
217 
218 	/* Decrease internal use count and unmap/free page if needed */
219 	if (refcount_dec_and_test(&info->use_count)) {
220 		dma_unmap_page(trans->dev, info->dma_addr, PAGE_SIZE,
221 			       DMA_TO_DEVICE);
222 
223 		__free_page(page);
224 	}
225 }
226 
iwl_pcie_free_tso_pages(struct iwl_trans * trans,struct sk_buff * skb,struct iwl_cmd_meta * cmd_meta)227 void iwl_pcie_free_tso_pages(struct iwl_trans *trans, struct sk_buff *skb,
228 			     struct iwl_cmd_meta *cmd_meta)
229 {
230 	struct page **page_ptr;
231 	struct page *next;
232 
233 	page_ptr = (void *)((u8 *)skb->cb + trans->conf.cb_data_offs);
234 	next = *page_ptr;
235 	*page_ptr = NULL;
236 
237 	while (next) {
238 		struct iwl_tso_page_info *info;
239 		struct page *tmp = next;
240 
241 		info = IWL_TSO_PAGE_INFO(page_address(next));
242 		next = info->next;
243 
244 		/* Unmap the scatter gather list that is on the last page */
245 		if (!next && cmd_meta->sg_offset) {
246 			struct sg_table *sgt;
247 
248 			sgt = (void *)((u8 *)page_address(tmp) +
249 				       cmd_meta->sg_offset);
250 
251 			dma_unmap_sgtable(trans->dev, sgt, DMA_TO_DEVICE, 0);
252 		}
253 
254 		iwl_pcie_free_and_unmap_tso_page(trans, tmp);
255 	}
256 }
257 
258 static inline dma_addr_t
iwl_txq_gen1_tfd_tb_get_addr(struct iwl_tfd * tfd,u8 idx)259 iwl_txq_gen1_tfd_tb_get_addr(struct iwl_tfd *tfd, u8 idx)
260 {
261 	struct iwl_tfd_tb *tb = &tfd->tbs[idx];
262 	dma_addr_t addr;
263 	dma_addr_t hi_len;
264 
265 	addr = get_unaligned_le32(&tb->lo);
266 
267 	if (sizeof(dma_addr_t) <= sizeof(u32))
268 		return addr;
269 
270 	hi_len = le16_to_cpu(tb->hi_n_len) & 0xF;
271 
272 	/*
273 	 * shift by 16 twice to avoid warnings on 32-bit
274 	 * (where this code never runs anyway due to the
275 	 * if statement above)
276 	 */
277 	return addr | ((hi_len << 16) << 16);
278 }
279 
iwl_txq_set_tfd_invalid_gen1(struct iwl_trans * trans,struct iwl_tfd * tfd)280 static void iwl_txq_set_tfd_invalid_gen1(struct iwl_trans *trans,
281 					 struct iwl_tfd *tfd)
282 {
283 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
284 
285 	tfd->num_tbs = 0;
286 
287 	iwl_pcie_gen1_tfd_set_tb(tfd, 0, trans_pcie->invalid_tx_cmd.dma,
288 				 trans_pcie->invalid_tx_cmd.size);
289 }
290 
iwl_txq_gen1_tfd_unmap(struct iwl_trans * trans,struct iwl_cmd_meta * meta,struct iwl_txq * txq,int index)291 static void iwl_txq_gen1_tfd_unmap(struct iwl_trans *trans,
292 				   struct iwl_cmd_meta *meta,
293 				   struct iwl_txq *txq, int index)
294 {
295 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
296 	int i, num_tbs;
297 	struct iwl_tfd *tfd = iwl_txq_get_tfd(trans, txq, index);
298 
299 	/* Sanity check on number of chunks */
300 	num_tbs = iwl_txq_gen1_tfd_get_num_tbs(tfd);
301 
302 	if (num_tbs > trans_pcie->txqs.tfd.max_tbs) {
303 		IWL_ERR(trans, "Too many chunks: %i\n", num_tbs);
304 		/* @todo issue fatal error, it is quite serious situation */
305 		return;
306 	}
307 
308 	/* TB1 is mapped directly, the rest is the TSO page and SG list. */
309 	if (meta->sg_offset)
310 		num_tbs = 2;
311 
312 	/* first TB is never freed - it's the bidirectional DMA data */
313 
314 	for (i = 1; i < num_tbs; i++) {
315 		if (meta->tbs & BIT(i))
316 			dma_unmap_page(trans->dev,
317 				       iwl_txq_gen1_tfd_tb_get_addr(tfd, i),
318 				       iwl_txq_gen1_tfd_tb_get_len(trans,
319 								   tfd, i),
320 				       DMA_TO_DEVICE);
321 		else
322 			dma_unmap_single(trans->dev,
323 					 iwl_txq_gen1_tfd_tb_get_addr(tfd, i),
324 					 iwl_txq_gen1_tfd_tb_get_len(trans,
325 								     tfd, i),
326 					 DMA_TO_DEVICE);
327 	}
328 
329 	meta->tbs = 0;
330 
331 	iwl_txq_set_tfd_invalid_gen1(trans, tfd);
332 }
333 
334 /**
335  * iwl_txq_free_tfd - Free all chunks referenced by TFD [txq->q.read_ptr]
336  * @trans: transport private data
337  * @txq: tx queue
338  * @read_ptr: the TXQ read_ptr to free
339  *
340  * Does NOT advance any TFD circular buffer read/write indexes
341  * Does NOT free the TFD itself (which is within circular buffer)
342  */
iwl_txq_free_tfd(struct iwl_trans * trans,struct iwl_txq * txq,int read_ptr)343 static void iwl_txq_free_tfd(struct iwl_trans *trans, struct iwl_txq *txq,
344 			     int read_ptr)
345 {
346 	/* rd_ptr is bounded by TFD_QUEUE_SIZE_MAX and
347 	 * idx is bounded by n_window
348 	 */
349 	int idx = iwl_txq_get_cmd_index(txq, read_ptr);
350 	struct sk_buff *skb;
351 
352 	lockdep_assert_held(&txq->reclaim_lock);
353 
354 	if (!txq->entries)
355 		return;
356 
357 	/* We have only q->n_window txq->entries, but we use
358 	 * TFD_QUEUE_SIZE_MAX tfds
359 	 */
360 	if (trans->mac_cfg->gen2)
361 		iwl_txq_gen2_tfd_unmap(trans, &txq->entries[idx].meta,
362 				       iwl_txq_get_tfd(trans, txq, read_ptr));
363 	else
364 		iwl_txq_gen1_tfd_unmap(trans, &txq->entries[idx].meta,
365 				       txq, read_ptr);
366 
367 	/* free SKB */
368 	skb = txq->entries[idx].skb;
369 
370 	/* Can be called from irqs-disabled context
371 	 * If skb is not NULL, it means that the whole queue is being
372 	 * freed and that the queue is not empty - free the skb
373 	 */
374 	if (skb) {
375 		iwl_op_mode_free_skb(trans->op_mode, skb);
376 		txq->entries[idx].skb = NULL;
377 	}
378 }
379 
380 /*
381  * iwl_pcie_txq_unmap -  Unmap any remaining DMA mappings and free skb's
382  */
iwl_pcie_txq_unmap(struct iwl_trans * trans,int txq_id)383 static void iwl_pcie_txq_unmap(struct iwl_trans *trans, int txq_id)
384 {
385 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
386 	struct iwl_txq *txq = trans_pcie->txqs.txq[txq_id];
387 
388 	if (!txq) {
389 		IWL_ERR(trans, "Trying to free a queue that wasn't allocated?\n");
390 		return;
391 	}
392 
393 	spin_lock_bh(&txq->reclaim_lock);
394 	spin_lock(&txq->lock);
395 	while (txq->write_ptr != txq->read_ptr) {
396 		IWL_DEBUG_TX_REPLY(trans, "Q %d Free %d\n",
397 				   txq_id, txq->read_ptr);
398 
399 		if (txq_id != trans->conf.cmd_queue) {
400 			struct sk_buff *skb = txq->entries[txq->read_ptr].skb;
401 			struct iwl_cmd_meta *cmd_meta =
402 				&txq->entries[txq->read_ptr].meta;
403 
404 			if (WARN_ON_ONCE(!skb))
405 				continue;
406 
407 			iwl_pcie_free_tso_pages(trans, skb, cmd_meta);
408 		}
409 		iwl_txq_free_tfd(trans, txq, txq->read_ptr);
410 		txq->read_ptr = iwl_txq_inc_wrap(trans, txq->read_ptr);
411 
412 		if (txq->read_ptr == txq->write_ptr &&
413 		    txq_id == trans->conf.cmd_queue)
414 			iwl_pcie_clear_cmd_in_flight(trans);
415 	}
416 
417 	while (!skb_queue_empty(&txq->overflow_q)) {
418 		struct sk_buff *skb = __skb_dequeue(&txq->overflow_q);
419 
420 		iwl_op_mode_free_skb(trans->op_mode, skb);
421 	}
422 
423 	spin_unlock(&txq->lock);
424 	spin_unlock_bh(&txq->reclaim_lock);
425 
426 	/* just in case - this queue may have been stopped */
427 	iwl_trans_pcie_wake_queue(trans, txq);
428 }
429 
430 /*
431  * iwl_pcie_txq_free - Deallocate DMA queue.
432  * @txq: Transmit queue to deallocate.
433  *
434  * Empty queue by removing and destroying all BD's.
435  * Free all buffers.
436  * 0-fill, but do not free "txq" descriptor structure.
437  */
iwl_pcie_txq_free(struct iwl_trans * trans,int txq_id)438 static void iwl_pcie_txq_free(struct iwl_trans *trans, int txq_id)
439 {
440 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
441 	struct iwl_txq *txq = trans_pcie->txqs.txq[txq_id];
442 	struct device *dev = trans->dev;
443 	int i;
444 
445 	if (WARN_ON(!txq))
446 		return;
447 
448 	iwl_pcie_txq_unmap(trans, txq_id);
449 
450 	/* De-alloc array of command/tx buffers */
451 	if (txq_id == trans->conf.cmd_queue)
452 		for (i = 0; i < txq->n_window; i++) {
453 			kfree_sensitive(txq->entries[i].cmd);
454 			kfree_sensitive(txq->entries[i].free_buf);
455 		}
456 
457 	/* De-alloc circular buffer of TFDs */
458 	if (txq->tfds) {
459 		dma_free_coherent(dev,
460 				  trans_pcie->txqs.tfd.size *
461 				  trans->mac_cfg->base->max_tfd_queue_size,
462 				  txq->tfds, txq->dma_addr);
463 		txq->dma_addr = 0;
464 		txq->tfds = NULL;
465 
466 		dma_free_coherent(dev,
467 				  sizeof(*txq->first_tb_bufs) * txq->n_window,
468 				  txq->first_tb_bufs, txq->first_tb_dma);
469 	}
470 
471 	kfree(txq->entries);
472 	txq->entries = NULL;
473 
474 	timer_delete_sync(&txq->stuck_timer);
475 
476 	/* 0-fill queue descriptor structure */
477 	memset(txq, 0, sizeof(*txq));
478 }
479 
iwl_pcie_tx_start(struct iwl_trans * trans)480 void iwl_pcie_tx_start(struct iwl_trans *trans)
481 {
482 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
483 	int nq = trans->mac_cfg->base->num_of_queues;
484 	int chan;
485 	u32 reg_val;
486 	int clear_dwords = (SCD_TRANS_TBL_OFFSET_QUEUE(nq) -
487 				SCD_CONTEXT_MEM_LOWER_BOUND) / sizeof(u32);
488 
489 	/* make sure all queue are not stopped/used */
490 	memset(trans_pcie->txqs.queue_stopped, 0,
491 	       sizeof(trans_pcie->txqs.queue_stopped));
492 	memset(trans_pcie->txqs.queue_used, 0,
493 	       sizeof(trans_pcie->txqs.queue_used));
494 
495 	trans_pcie->scd_base_addr =
496 		iwl_read_prph(trans, SCD_SRAM_BASE_ADDR);
497 
498 	/* reset context data, TX status and translation data */
499 	iwl_trans_write_mem(trans, trans_pcie->scd_base_addr +
500 				   SCD_CONTEXT_MEM_LOWER_BOUND,
501 			    NULL, clear_dwords);
502 
503 	iwl_write_prph(trans, SCD_DRAM_BASE_ADDR,
504 		       trans_pcie->txqs.scd_bc_tbls.dma >> 10);
505 
506 	/* The chain extension of the SCD doesn't work well. This feature is
507 	 * enabled by default by the HW, so we need to disable it manually.
508 	 */
509 	if (trans->mac_cfg->base->scd_chain_ext_wa)
510 		iwl_write_prph(trans, SCD_CHAINEXT_EN, 0);
511 
512 	iwl_trans_ac_txq_enable(trans, trans->conf.cmd_queue,
513 				trans->conf.cmd_fifo,
514 				IWL_DEF_WD_TIMEOUT);
515 
516 	/* Activate all Tx DMA/FIFO channels */
517 	iwl_scd_activate_fifos(trans);
518 
519 	/* Enable DMA channel */
520 	for (chan = 0; chan < FH_TCSR_CHNL_NUM; chan++)
521 		iwl_write_direct32(trans, FH_TCSR_CHNL_TX_CONFIG_REG(chan),
522 				   FH_TCSR_TX_CONFIG_REG_VAL_DMA_CHNL_ENABLE |
523 				   FH_TCSR_TX_CONFIG_REG_VAL_DMA_CREDIT_ENABLE);
524 
525 	/* Update FH chicken bits */
526 	reg_val = iwl_read_direct32(trans, FH_TX_CHICKEN_BITS_REG);
527 	iwl_write_direct32(trans, FH_TX_CHICKEN_BITS_REG,
528 			   reg_val | FH_TX_CHICKEN_BITS_SCD_AUTO_RETRY_EN);
529 
530 	/* Enable L1-Active */
531 	if (trans->mac_cfg->device_family < IWL_DEVICE_FAMILY_8000)
532 		iwl_clear_bits_prph(trans, APMG_PCIDEV_STT_REG,
533 				    APMG_PCIDEV_STT_VAL_L1_ACT_DIS);
534 }
535 
iwl_trans_pcie_tx_reset(struct iwl_trans * trans)536 void iwl_trans_pcie_tx_reset(struct iwl_trans *trans)
537 {
538 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
539 	int txq_id;
540 
541 	/*
542 	 * we should never get here in gen2 trans mode return early to avoid
543 	 * having invalid accesses
544 	 */
545 	if (WARN_ON_ONCE(trans->mac_cfg->gen2))
546 		return;
547 
548 	for (txq_id = 0; txq_id < trans->mac_cfg->base->num_of_queues;
549 	     txq_id++) {
550 		struct iwl_txq *txq = trans_pcie->txqs.txq[txq_id];
551 		if (trans->mac_cfg->gen2)
552 			iwl_write_direct64(trans,
553 					   FH_MEM_CBBC_QUEUE(trans, txq_id),
554 					   txq->dma_addr);
555 		else
556 			iwl_write_direct32(trans,
557 					   FH_MEM_CBBC_QUEUE(trans, txq_id),
558 					   txq->dma_addr >> 8);
559 		iwl_pcie_txq_unmap(trans, txq_id);
560 		txq->read_ptr = 0;
561 		txq->write_ptr = 0;
562 	}
563 
564 	/* Tell NIC where to find the "keep warm" buffer */
565 	iwl_write_direct32(trans, FH_KW_MEM_ADDR_REG,
566 			   trans_pcie->kw.dma >> 4);
567 
568 	/*
569 	 * Send 0 as the scd_base_addr since the device may have be reset
570 	 * while we were in WoWLAN in which case SCD_SRAM_BASE_ADDR will
571 	 * contain garbage.
572 	 */
573 	iwl_pcie_tx_start(trans);
574 }
575 
iwl_pcie_tx_stop_fh(struct iwl_trans * trans)576 static void iwl_pcie_tx_stop_fh(struct iwl_trans *trans)
577 {
578 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
579 	int ch, ret;
580 	u32 mask = 0;
581 
582 	spin_lock_bh(&trans_pcie->irq_lock);
583 
584 	if (!iwl_trans_grab_nic_access(trans))
585 		goto out;
586 
587 	/* Stop each Tx DMA channel */
588 	for (ch = 0; ch < FH_TCSR_CHNL_NUM; ch++) {
589 		iwl_write32(trans, FH_TCSR_CHNL_TX_CONFIG_REG(ch), 0x0);
590 		mask |= FH_TSSR_TX_STATUS_REG_MSK_CHNL_IDLE(ch);
591 	}
592 
593 	/* Wait for DMA channels to be idle */
594 	ret = iwl_poll_bits(trans, FH_TSSR_TX_STATUS_REG, mask, 5000);
595 	if (ret)
596 		IWL_ERR(trans,
597 			"Failing on timeout while stopping DMA channel %d [0x%08x]\n",
598 			ch, iwl_read32(trans, FH_TSSR_TX_STATUS_REG));
599 
600 	iwl_trans_release_nic_access(trans);
601 
602 out:
603 	spin_unlock_bh(&trans_pcie->irq_lock);
604 }
605 
606 /*
607  * iwl_pcie_tx_stop - Stop all Tx DMA channels
608  */
iwl_pcie_tx_stop(struct iwl_trans * trans)609 int iwl_pcie_tx_stop(struct iwl_trans *trans)
610 {
611 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
612 	int txq_id;
613 
614 	/* Turn off all Tx DMA fifos */
615 	iwl_scd_deactivate_fifos(trans);
616 
617 	/* Turn off all Tx DMA channels */
618 	iwl_pcie_tx_stop_fh(trans);
619 
620 	/*
621 	 * This function can be called before the op_mode disabled the
622 	 * queues. This happens when we have an rfkill interrupt.
623 	 * Since we stop Tx altogether - mark the queues as stopped.
624 	 */
625 	memset(trans_pcie->txqs.queue_stopped, 0,
626 	       sizeof(trans_pcie->txqs.queue_stopped));
627 	memset(trans_pcie->txqs.queue_used, 0,
628 	       sizeof(trans_pcie->txqs.queue_used));
629 
630 	/* This can happen: start_hw, stop_device */
631 	if (!trans_pcie->txq_memory)
632 		return 0;
633 
634 	/* Unmap DMA from host system and free skb's */
635 	for (txq_id = 0; txq_id < trans->mac_cfg->base->num_of_queues;
636 	     txq_id++)
637 		iwl_pcie_txq_unmap(trans, txq_id);
638 
639 	return 0;
640 }
641 
642 /*
643  * iwl_trans_tx_free - Free TXQ Context
644  *
645  * Destroy all TX DMA queues and structures
646  */
iwl_pcie_tx_free(struct iwl_trans * trans)647 void iwl_pcie_tx_free(struct iwl_trans *trans)
648 {
649 	int txq_id;
650 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
651 
652 	memset(trans_pcie->txqs.queue_used, 0,
653 	       sizeof(trans_pcie->txqs.queue_used));
654 
655 	/* Tx queues */
656 	if (trans_pcie->txq_memory) {
657 		for (txq_id = 0;
658 		     txq_id < trans->mac_cfg->base->num_of_queues;
659 		     txq_id++) {
660 			iwl_pcie_txq_free(trans, txq_id);
661 			trans_pcie->txqs.txq[txq_id] = NULL;
662 		}
663 	}
664 
665 	kfree(trans_pcie->txq_memory);
666 	trans_pcie->txq_memory = NULL;
667 
668 	iwl_pcie_free_dma_ptr(trans, &trans_pcie->kw);
669 
670 	iwl_pcie_free_dma_ptr(trans, &trans_pcie->txqs.scd_bc_tbls);
671 }
672 
iwl_txq_log_scd_error(struct iwl_trans * trans,struct iwl_txq * txq)673 void iwl_txq_log_scd_error(struct iwl_trans *trans, struct iwl_txq *txq)
674 {
675 	u32 txq_id = txq->id;
676 	u32 status;
677 	bool active;
678 	u8 fifo;
679 
680 	if (trans->mac_cfg->gen2) {
681 		IWL_ERR(trans, "Queue %d is stuck %d %d\n", txq_id,
682 			txq->read_ptr, txq->write_ptr);
683 		/* TODO: access new SCD registers and dump them */
684 		return;
685 	}
686 
687 	status = iwl_read_prph(trans, SCD_QUEUE_STATUS_BITS(txq_id));
688 	fifo = (status >> SCD_QUEUE_STTS_REG_POS_TXF) & 0x7;
689 	active = !!(status & BIT(SCD_QUEUE_STTS_REG_POS_ACTIVE));
690 
691 	IWL_ERR(trans,
692 		"Queue %d is %sactive on fifo %d and stuck for %u ms. SW [%d, %d] HW [%d, %d] FH TRB=0x0%x\n",
693 		txq_id, active ? "" : "in", fifo,
694 		jiffies_to_msecs(txq->wd_timeout),
695 		txq->read_ptr, txq->write_ptr,
696 		iwl_read_prph(trans, SCD_QUEUE_RDPTR(txq_id)) &
697 			(trans->mac_cfg->base->max_tfd_queue_size - 1),
698 			iwl_read_prph(trans, SCD_QUEUE_WRPTR(txq_id)) &
699 			(trans->mac_cfg->base->max_tfd_queue_size - 1),
700 			iwl_read_direct32(trans, FH_TX_TRB_REG(fifo)));
701 }
702 
iwl_txq_stuck_timer(struct timer_list * t)703 static void iwl_txq_stuck_timer(struct timer_list *t)
704 {
705 	struct iwl_txq *txq = timer_container_of(txq, t, stuck_timer);
706 	struct iwl_trans *trans = txq->trans;
707 
708 	spin_lock(&txq->lock);
709 	/* check if triggered erroneously */
710 	if (txq->read_ptr == txq->write_ptr) {
711 		spin_unlock(&txq->lock);
712 		return;
713 	}
714 	spin_unlock(&txq->lock);
715 
716 	iwl_txq_log_scd_error(trans, txq);
717 
718 	iwl_force_nmi(trans);
719 }
720 
iwl_pcie_txq_alloc(struct iwl_trans * trans,struct iwl_txq * txq,int slots_num,bool cmd_queue)721 int iwl_pcie_txq_alloc(struct iwl_trans *trans, struct iwl_txq *txq,
722 		       int slots_num, bool cmd_queue)
723 {
724 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
725 	size_t num_entries = trans->mac_cfg->gen2 ?
726 		slots_num : trans->mac_cfg->base->max_tfd_queue_size;
727 	size_t tfd_sz;
728 	size_t tb0_buf_sz;
729 	int i;
730 
731 	if (WARN_ONCE(slots_num <= 0, "Invalid slots num:%d\n", slots_num))
732 		return -EINVAL;
733 
734 	if (WARN_ON(txq->entries || txq->tfds))
735 		return -EINVAL;
736 
737 	tfd_sz = trans_pcie->txqs.tfd.size * num_entries;
738 
739 	timer_setup(&txq->stuck_timer, iwl_txq_stuck_timer, 0);
740 	txq->trans = trans;
741 
742 	txq->n_window = slots_num;
743 
744 	txq->entries = kzalloc_objs(struct iwl_pcie_txq_entry, slots_num);
745 
746 	if (!txq->entries)
747 		goto error;
748 
749 	if (cmd_queue)
750 		for (i = 0; i < slots_num; i++) {
751 			txq->entries[i].cmd =
752 				kmalloc_obj(struct iwl_device_cmd);
753 			if (!txq->entries[i].cmd)
754 				goto error;
755 		}
756 
757 	/* Circular buffer of transmit frame descriptors (TFDs),
758 	 * shared with device
759 	 */
760 	txq->tfds = dma_alloc_coherent(trans->dev, tfd_sz,
761 				       &txq->dma_addr, GFP_KERNEL);
762 	if (!txq->tfds)
763 		goto error;
764 
765 	BUILD_BUG_ON(sizeof(*txq->first_tb_bufs) != IWL_FIRST_TB_SIZE_ALIGN);
766 
767 	tb0_buf_sz = sizeof(*txq->first_tb_bufs) * slots_num;
768 
769 	txq->first_tb_bufs = dma_alloc_coherent(trans->dev, tb0_buf_sz,
770 						&txq->first_tb_dma,
771 						GFP_KERNEL);
772 	if (!txq->first_tb_bufs)
773 		goto err_free_tfds;
774 
775 	for (i = 0; i < num_entries; i++) {
776 		void *tfd = iwl_txq_get_tfd(trans, txq, i);
777 
778 		if (trans->mac_cfg->gen2)
779 			iwl_txq_set_tfd_invalid_gen2(trans, tfd);
780 		else
781 			iwl_txq_set_tfd_invalid_gen1(trans, tfd);
782 	}
783 
784 	return 0;
785 err_free_tfds:
786 	dma_free_coherent(trans->dev, tfd_sz, txq->tfds, txq->dma_addr);
787 	txq->tfds = NULL;
788 error:
789 	if (txq->entries && cmd_queue)
790 		for (i = 0; i < slots_num; i++)
791 			kfree(txq->entries[i].cmd);
792 	kfree(txq->entries);
793 	txq->entries = NULL;
794 
795 	return -ENOMEM;
796 }
797 
798 #define BC_TABLE_SIZE	(sizeof(struct iwl_bc_tbl_entry) * TFD_QUEUE_BC_SIZE)
799 
800 /*
801  * iwl_pcie_tx_alloc - allocate TX context
802  * Allocate all Tx DMA structures and initialize them
803  */
iwl_pcie_tx_alloc(struct iwl_trans * trans)804 static int iwl_pcie_tx_alloc(struct iwl_trans *trans)
805 {
806 	int ret;
807 	int txq_id, slots_num;
808 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
809 	u16 bc_tbls_size = trans->mac_cfg->base->num_of_queues;
810 
811 	if (WARN_ON(trans->mac_cfg->device_family >= IWL_DEVICE_FAMILY_AX210))
812 		return -EINVAL;
813 
814 	bc_tbls_size *= BC_TABLE_SIZE;
815 
816 	/*It is not allowed to alloc twice, so warn when this happens.
817 	 * We cannot rely on the previous allocation, so free and fail */
818 	if (WARN_ON(trans_pcie->txq_memory)) {
819 		ret = -EINVAL;
820 		goto error;
821 	}
822 
823 	ret = iwl_pcie_alloc_dma_ptr(trans, &trans_pcie->txqs.scd_bc_tbls,
824 				     bc_tbls_size);
825 	if (ret) {
826 		IWL_ERR(trans, "Scheduler BC Table allocation failed\n");
827 		goto error;
828 	}
829 
830 	/* Alloc keep-warm buffer */
831 	ret = iwl_pcie_alloc_dma_ptr(trans, &trans_pcie->kw, IWL_KW_SIZE);
832 	if (ret) {
833 		IWL_ERR(trans, "Keep Warm allocation failed\n");
834 		goto error;
835 	}
836 
837 	trans_pcie->txq_memory =
838 		kzalloc_objs(struct iwl_txq,
839 			     trans->mac_cfg->base->num_of_queues);
840 	if (!trans_pcie->txq_memory) {
841 		IWL_ERR(trans, "Not enough memory for txq\n");
842 		ret = -ENOMEM;
843 		goto error;
844 	}
845 
846 	/* Alloc and init all Tx queues, including the command queue (#4/#9) */
847 	for (txq_id = 0; txq_id < trans->mac_cfg->base->num_of_queues;
848 	     txq_id++) {
849 		bool cmd_queue = (txq_id == trans->conf.cmd_queue);
850 
851 		if (cmd_queue)
852 			slots_num = max_t(u32, IWL_CMD_QUEUE_SIZE,
853 					  trans->mac_cfg->base->min_txq_size);
854 		else
855 			slots_num = max_t(u32, IWL_DEFAULT_QUEUE_SIZE,
856 					  trans->mac_cfg->base->min_ba_txq_size);
857 		trans_pcie->txqs.txq[txq_id] = &trans_pcie->txq_memory[txq_id];
858 		ret = iwl_pcie_txq_alloc(trans, trans_pcie->txqs.txq[txq_id],
859 					 slots_num, cmd_queue);
860 		if (ret) {
861 			IWL_ERR(trans, "Tx %d queue alloc failed\n", txq_id);
862 			goto error;
863 		}
864 		trans_pcie->txqs.txq[txq_id]->id = txq_id;
865 	}
866 
867 	return 0;
868 
869 error:
870 	iwl_pcie_tx_free(trans);
871 
872 	return ret;
873 }
874 
875 /*
876  * iwl_queue_init - Initialize queue's high/low-water and read/write indexes
877  */
iwl_queue_init(struct iwl_txq * q,int slots_num)878 static int iwl_queue_init(struct iwl_txq *q, int slots_num)
879 {
880 	q->n_window = slots_num;
881 
882 	/* slots_num must be power-of-two size, otherwise
883 	 * iwl_txq_get_cmd_index is broken.
884 	 */
885 	if (WARN_ON(!is_power_of_2(slots_num)))
886 		return -EINVAL;
887 
888 	q->low_mark = q->n_window / 4;
889 	if (q->low_mark < 4)
890 		q->low_mark = 4;
891 
892 	q->high_mark = q->n_window / 8;
893 	if (q->high_mark < 2)
894 		q->high_mark = 2;
895 
896 	q->write_ptr = 0;
897 	q->read_ptr = 0;
898 
899 	return 0;
900 }
901 
iwl_txq_init(struct iwl_trans * trans,struct iwl_txq * txq,int slots_num,bool cmd_queue)902 int iwl_txq_init(struct iwl_trans *trans, struct iwl_txq *txq,
903 		 int slots_num, bool cmd_queue)
904 {
905 	u32 tfd_queue_max_size =
906 		trans->mac_cfg->base->max_tfd_queue_size;
907 	int ret;
908 
909 	txq->need_update = false;
910 
911 	/* max_tfd_queue_size must be power-of-two size, otherwise
912 	 * iwl_txq_inc_wrap and iwl_txq_dec_wrap are broken.
913 	 */
914 	if (WARN_ONCE(tfd_queue_max_size & (tfd_queue_max_size - 1),
915 		      "Max tfd queue size must be a power of two, but is %d",
916 		      tfd_queue_max_size))
917 		return -EINVAL;
918 
919 	/* Initialize queue's high/low-water marks, and head/tail indexes */
920 	ret = iwl_queue_init(txq, slots_num);
921 	if (ret)
922 		return ret;
923 
924 	spin_lock_init(&txq->lock);
925 	spin_lock_init(&txq->reclaim_lock);
926 
927 	if (cmd_queue) {
928 		static struct lock_class_key iwl_txq_cmd_queue_lock_class;
929 
930 		lockdep_set_class(&txq->lock, &iwl_txq_cmd_queue_lock_class);
931 	}
932 
933 	__skb_queue_head_init(&txq->overflow_q);
934 
935 	return 0;
936 }
937 
iwl_pcie_tx_init(struct iwl_trans * trans)938 int iwl_pcie_tx_init(struct iwl_trans *trans)
939 {
940 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
941 	int ret;
942 	int txq_id, slots_num;
943 	bool alloc = false;
944 
945 	if (!trans_pcie->txq_memory) {
946 		ret = iwl_pcie_tx_alloc(trans);
947 		if (ret)
948 			goto error;
949 		alloc = true;
950 	}
951 
952 	spin_lock_bh(&trans_pcie->irq_lock);
953 
954 	/* Turn off all Tx DMA fifos */
955 	iwl_scd_deactivate_fifos(trans);
956 
957 	/* Tell NIC where to find the "keep warm" buffer */
958 	iwl_write_direct32(trans, FH_KW_MEM_ADDR_REG,
959 			   trans_pcie->kw.dma >> 4);
960 
961 	spin_unlock_bh(&trans_pcie->irq_lock);
962 
963 	/* Alloc and init all Tx queues, including the command queue (#4/#9) */
964 	for (txq_id = 0; txq_id < trans->mac_cfg->base->num_of_queues;
965 	     txq_id++) {
966 		bool cmd_queue = (txq_id == trans->conf.cmd_queue);
967 
968 		if (cmd_queue)
969 			slots_num = max_t(u32, IWL_CMD_QUEUE_SIZE,
970 					  trans->mac_cfg->base->min_txq_size);
971 		else
972 			slots_num = max_t(u32, IWL_DEFAULT_QUEUE_SIZE,
973 					  trans->mac_cfg->base->min_ba_txq_size);
974 		ret = iwl_txq_init(trans, trans_pcie->txqs.txq[txq_id], slots_num,
975 				   cmd_queue);
976 		if (ret) {
977 			IWL_ERR(trans, "Tx %d queue init failed\n", txq_id);
978 			goto error;
979 		}
980 
981 		/*
982 		 * Tell nic where to find circular buffer of TFDs for a
983 		 * given Tx queue, and enable the DMA channel used for that
984 		 * queue.
985 		 * Circular buffer (TFD queue in DRAM) physical base address
986 		 */
987 		iwl_write_direct32(trans, FH_MEM_CBBC_QUEUE(trans, txq_id),
988 				   trans_pcie->txqs.txq[txq_id]->dma_addr >> 8);
989 	}
990 
991 	iwl_set_bits_prph(trans, SCD_GP_CTRL, SCD_GP_CTRL_AUTO_ACTIVE_MODE);
992 	if (trans->mac_cfg->base->num_of_queues > 20)
993 		iwl_set_bits_prph(trans, SCD_GP_CTRL,
994 				  SCD_GP_CTRL_ENABLE_31_QUEUES);
995 
996 	return 0;
997 error:
998 	/*Upon error, free only if we allocated something */
999 	if (alloc)
1000 		iwl_pcie_tx_free(trans);
1001 	return ret;
1002 }
1003 
iwl_pcie_set_cmd_in_flight(struct iwl_trans * trans,const struct iwl_host_cmd * cmd)1004 static int iwl_pcie_set_cmd_in_flight(struct iwl_trans *trans,
1005 				      const struct iwl_host_cmd *cmd)
1006 {
1007 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
1008 
1009 	/* Make sure the NIC is still alive in the bus */
1010 	if (test_bit(STATUS_TRANS_DEAD, &trans->status))
1011 		return -ENODEV;
1012 
1013 	if (!trans->mac_cfg->base->apmg_wake_up_wa)
1014 		return 0;
1015 
1016 	/*
1017 	 * wake up the NIC to make sure that the firmware will see the host
1018 	 * command - we will let the NIC sleep once all the host commands
1019 	 * returned. This needs to be done only on NICs that have
1020 	 * apmg_wake_up_wa set (see above.)
1021 	 */
1022 	if (!_iwl_trans_pcie_grab_nic_access(trans, false))
1023 		return -EIO;
1024 
1025 	/*
1026 	 * In iwl_trans_grab_nic_access(), we've acquired the reg_lock.
1027 	 * There, we also returned immediately if cmd_hold_nic_awake is
1028 	 * already true, so it's OK to unconditionally set it to true.
1029 	 */
1030 	trans_pcie->cmd_hold_nic_awake = true;
1031 	spin_unlock(&trans_pcie->reg_lock);
1032 
1033 	return 0;
1034 }
1035 
iwl_txq_progress(struct iwl_txq * txq)1036 static void iwl_txq_progress(struct iwl_txq *txq)
1037 {
1038 	lockdep_assert_held(&txq->lock);
1039 
1040 	if (!txq->wd_timeout)
1041 		return;
1042 
1043 	/*
1044 	 * station is asleep and we send data - that must
1045 	 * be uAPSD or PS-Poll. Don't rearm the timer.
1046 	 */
1047 	if (txq->frozen)
1048 		return;
1049 
1050 	/*
1051 	 * if empty delete timer, otherwise move timer forward
1052 	 * since we're making progress on this queue
1053 	 */
1054 	if (txq->read_ptr == txq->write_ptr)
1055 		timer_delete(&txq->stuck_timer);
1056 	else
1057 		mod_timer(&txq->stuck_timer, jiffies + txq->wd_timeout);
1058 }
1059 
iwl_txq_used(const struct iwl_txq * q,int i,int read_ptr,int write_ptr)1060 static inline bool iwl_txq_used(const struct iwl_txq *q, int i,
1061 				int read_ptr, int write_ptr)
1062 {
1063 	int index = iwl_txq_get_cmd_index(q, i);
1064 	int r = iwl_txq_get_cmd_index(q, read_ptr);
1065 	int w = iwl_txq_get_cmd_index(q, write_ptr);
1066 
1067 	return w >= r ?
1068 		(index >= r && index < w) :
1069 		!(index < r && index >= w);
1070 }
1071 
1072 /*
1073  * iwl_pcie_cmdq_reclaim - Reclaim TX command queue entries already Tx'd
1074  *
1075  * When FW advances 'R' index, all entries between old and new 'R' index
1076  * need to be reclaimed. As result, some free space forms.  If there is
1077  * enough free space (> low mark), wake the stack that feeds us.
1078  */
iwl_pcie_cmdq_reclaim(struct iwl_trans * trans,int txq_id,int idx)1079 static void iwl_pcie_cmdq_reclaim(struct iwl_trans *trans, int txq_id, int idx)
1080 {
1081 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
1082 	struct iwl_txq *txq = trans_pcie->txqs.txq[txq_id];
1083 	int nfreed = 0;
1084 	u16 r;
1085 
1086 	lockdep_assert_held(&txq->lock);
1087 
1088 	idx = iwl_txq_get_cmd_index(txq, idx);
1089 	r = iwl_txq_get_cmd_index(txq, txq->read_ptr);
1090 
1091 	if (idx >= trans->mac_cfg->base->max_tfd_queue_size ||
1092 	    (!iwl_txq_used(txq, idx, txq->read_ptr, txq->write_ptr))) {
1093 		WARN_ONCE(test_bit(txq_id, trans_pcie->txqs.queue_used),
1094 			  "%s: Read index for DMA queue txq id (%d), index %d is out of range [0-%d] %d %d.\n",
1095 			  __func__, txq_id, idx,
1096 			  trans->mac_cfg->base->max_tfd_queue_size,
1097 			  txq->write_ptr, txq->read_ptr);
1098 		return;
1099 	}
1100 
1101 	for (idx = iwl_txq_inc_wrap(trans, idx); r != idx;
1102 	     r = iwl_txq_inc_wrap(trans, r)) {
1103 		txq->read_ptr = iwl_txq_inc_wrap(trans, txq->read_ptr);
1104 
1105 		if (nfreed++ > 0) {
1106 			IWL_ERR(trans, "HCMD skipped: index (%d) %d %d\n",
1107 				idx, txq->write_ptr, r);
1108 			iwl_force_nmi(trans);
1109 		}
1110 	}
1111 
1112 	if (txq->read_ptr == txq->write_ptr)
1113 		iwl_pcie_clear_cmd_in_flight(trans);
1114 
1115 	iwl_txq_progress(txq);
1116 }
1117 
iwl_pcie_txq_set_ratid_map(struct iwl_trans * trans,u16 ra_tid,u16 txq_id)1118 static int iwl_pcie_txq_set_ratid_map(struct iwl_trans *trans, u16 ra_tid,
1119 				 u16 txq_id)
1120 {
1121 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
1122 	u32 tbl_dw_addr;
1123 	u32 tbl_dw;
1124 	u16 scd_q2ratid;
1125 
1126 	scd_q2ratid = ra_tid & SCD_QUEUE_RA_TID_MAP_RATID_MSK;
1127 
1128 	tbl_dw_addr = trans_pcie->scd_base_addr +
1129 			SCD_TRANS_TBL_OFFSET_QUEUE(txq_id);
1130 
1131 	tbl_dw = iwl_trans_read_mem32(trans, tbl_dw_addr);
1132 
1133 	if (txq_id & 0x1)
1134 		tbl_dw = (scd_q2ratid << 16) | (tbl_dw & 0x0000FFFF);
1135 	else
1136 		tbl_dw = scd_q2ratid | (tbl_dw & 0xFFFF0000);
1137 
1138 	iwl_trans_write_mem32(trans, tbl_dw_addr, tbl_dw);
1139 
1140 	return 0;
1141 }
1142 
1143 /* Receiver address (actually, Rx station's index into station table),
1144  * combined with Traffic ID (QOS priority), in format used by Tx Scheduler */
1145 #define BUILD_RAxTID(sta_id, tid)	(((sta_id) << 4) + (tid))
1146 
iwl_trans_pcie_txq_enable(struct iwl_trans * trans,int txq_id,u16 ssn,const struct iwl_trans_txq_scd_cfg * cfg,unsigned int wdg_timeout)1147 bool iwl_trans_pcie_txq_enable(struct iwl_trans *trans, int txq_id, u16 ssn,
1148 			       const struct iwl_trans_txq_scd_cfg *cfg,
1149 			       unsigned int wdg_timeout)
1150 {
1151 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
1152 	struct iwl_txq *txq;
1153 	int fifo = -1;
1154 	bool scd_bug = false;
1155 
1156 	if (WARN_ONCE(txq_id < 0 ||
1157 		      txq_id >= trans->mac_cfg->base->num_of_queues,
1158 		      "queue %d out of range", txq_id))
1159 		return false;
1160 
1161 	txq = trans_pcie->txqs.txq[txq_id];
1162 
1163 	if (test_and_set_bit(txq_id, trans_pcie->txqs.queue_used))
1164 		WARN_ONCE(1, "queue %d already used - expect issues", txq_id);
1165 
1166 	txq->wd_timeout = msecs_to_jiffies(wdg_timeout);
1167 
1168 	if (cfg) {
1169 		fifo = cfg->fifo;
1170 
1171 		/* Disable the scheduler prior configuring the cmd queue */
1172 		if (txq_id == trans->conf.cmd_queue &&
1173 		    trans->conf.scd_set_active)
1174 			iwl_scd_enable_set_active(trans, 0);
1175 
1176 		/* Stop this Tx queue before configuring it */
1177 		iwl_scd_txq_set_inactive(trans, txq_id);
1178 
1179 		/* Set this queue as a chain-building queue unless it is CMD */
1180 		if (txq_id != trans->conf.cmd_queue)
1181 			iwl_scd_txq_set_chain(trans, txq_id);
1182 
1183 		if (cfg->aggregate) {
1184 			u16 ra_tid = BUILD_RAxTID(cfg->sta_id, cfg->tid);
1185 
1186 			/* Map receiver-address / traffic-ID to this queue */
1187 			iwl_pcie_txq_set_ratid_map(trans, ra_tid, txq_id);
1188 
1189 			/* enable aggregations for the queue */
1190 			iwl_scd_txq_enable_agg(trans, txq_id);
1191 			txq->ampdu = true;
1192 		} else {
1193 			/*
1194 			 * disable aggregations for the queue, this will also
1195 			 * make the ra_tid mapping configuration irrelevant
1196 			 * since it is now a non-AGG queue.
1197 			 */
1198 			iwl_scd_txq_disable_agg(trans, txq_id);
1199 
1200 			ssn = txq->read_ptr;
1201 		}
1202 	} else {
1203 		/*
1204 		 * If we need to move the SCD write pointer by steps of
1205 		 * 0x40, 0x80 or 0xc0, it gets stuck. Avoids this and let
1206 		 * the op_mode know by returning true later.
1207 		 * Do this only in case cfg is NULL since this trick can
1208 		 * be done only if we have DQA enabled which is true for mvm
1209 		 * only. And mvm never sets a cfg pointer.
1210 		 * This is really ugly, but this is the easiest way out for
1211 		 * this sad hardware issue.
1212 		 * This bug has been fixed on devices 9000 and up.
1213 		 */
1214 		scd_bug = !trans->mac_cfg->mq_rx_supported &&
1215 			!((ssn - txq->write_ptr) & 0x3f) &&
1216 			(ssn != txq->write_ptr);
1217 		if (scd_bug)
1218 			ssn++;
1219 	}
1220 
1221 	/* Place first TFD at index corresponding to start sequence number.
1222 	 * Assumes that ssn_idx is valid (!= 0xFFF) */
1223 	txq->read_ptr = (ssn & 0xff);
1224 	txq->write_ptr = (ssn & 0xff);
1225 	iwl_write_direct32(trans, HBUS_TARG_WRPTR,
1226 			   (ssn & 0xff) | (txq_id << 8));
1227 
1228 	if (cfg) {
1229 		u8 frame_limit = cfg->frame_limit;
1230 
1231 		iwl_write_prph(trans, SCD_QUEUE_RDPTR(txq_id), ssn);
1232 
1233 		/* Set up Tx window size and frame limit for this queue */
1234 		iwl_trans_write_mem32(trans, trans_pcie->scd_base_addr +
1235 				SCD_CONTEXT_QUEUE_OFFSET(txq_id), 0);
1236 		iwl_trans_write_mem32(trans,
1237 			trans_pcie->scd_base_addr +
1238 			SCD_CONTEXT_QUEUE_OFFSET(txq_id) + sizeof(u32),
1239 			SCD_QUEUE_CTX_REG2_VAL(WIN_SIZE, frame_limit) |
1240 			SCD_QUEUE_CTX_REG2_VAL(FRAME_LIMIT, frame_limit));
1241 
1242 		/* Set up status area in SRAM, map to Tx DMA/FIFO, activate */
1243 		iwl_write_prph(trans, SCD_QUEUE_STATUS_BITS(txq_id),
1244 			       (1 << SCD_QUEUE_STTS_REG_POS_ACTIVE) |
1245 			       (cfg->fifo << SCD_QUEUE_STTS_REG_POS_TXF) |
1246 			       (1 << SCD_QUEUE_STTS_REG_POS_WSL) |
1247 			       SCD_QUEUE_STTS_REG_MSK);
1248 
1249 		/* enable the scheduler for this queue (only) */
1250 		if (txq_id == trans->conf.cmd_queue &&
1251 		    trans->conf.scd_set_active)
1252 			iwl_scd_enable_set_active(trans, BIT(txq_id));
1253 
1254 		IWL_DEBUG_TX_QUEUES(trans,
1255 				    "Activate queue %d on FIFO %d WrPtr: %d\n",
1256 				    txq_id, fifo, ssn & 0xff);
1257 	} else {
1258 		IWL_DEBUG_TX_QUEUES(trans,
1259 				    "Activate queue %d WrPtr: %d\n",
1260 				    txq_id, ssn & 0xff);
1261 	}
1262 
1263 	return scd_bug;
1264 }
1265 
iwl_trans_pcie_txq_set_shared_mode(struct iwl_trans * trans,u32 txq_id,bool shared_mode)1266 void iwl_trans_pcie_txq_set_shared_mode(struct iwl_trans *trans, u32 txq_id,
1267 					bool shared_mode)
1268 {
1269 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
1270 	struct iwl_txq *txq = trans_pcie->txqs.txq[txq_id];
1271 
1272 	txq->ampdu = !shared_mode;
1273 }
1274 
iwl_trans_pcie_txq_disable(struct iwl_trans * trans,int txq_id,bool configure_scd)1275 void iwl_trans_pcie_txq_disable(struct iwl_trans *trans, int txq_id,
1276 				bool configure_scd)
1277 {
1278 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
1279 	u32 stts_addr = trans_pcie->scd_base_addr +
1280 			SCD_TX_STTS_QUEUE_OFFSET(txq_id);
1281 	static const u32 zero_val[4] = {};
1282 
1283 	trans_pcie->txqs.txq[txq_id]->frozen_expiry_remainder = 0;
1284 	trans_pcie->txqs.txq[txq_id]->frozen = false;
1285 
1286 	/*
1287 	 * Upon HW Rfkill - we stop the device, and then stop the queues
1288 	 * in the op_mode. Just for the sake of the simplicity of the op_mode,
1289 	 * allow the op_mode to call txq_disable after it already called
1290 	 * stop_device.
1291 	 */
1292 	if (!test_and_clear_bit(txq_id, trans_pcie->txqs.queue_used)) {
1293 		WARN_ONCE(test_bit(STATUS_DEVICE_ENABLED, &trans->status),
1294 			  "queue %d not used", txq_id);
1295 		return;
1296 	}
1297 
1298 	if (configure_scd) {
1299 		iwl_scd_txq_set_inactive(trans, txq_id);
1300 
1301 		iwl_trans_write_mem(trans, stts_addr, (const void *)zero_val,
1302 				    ARRAY_SIZE(zero_val));
1303 	}
1304 
1305 	iwl_pcie_txq_unmap(trans, txq_id);
1306 	trans_pcie->txqs.txq[txq_id]->ampdu = false;
1307 
1308 	IWL_DEBUG_TX_QUEUES(trans, "Deactivate queue %d\n", txq_id);
1309 }
1310 
1311 /*************** HOST COMMAND QUEUE FUNCTIONS   *****/
1312 
iwl_trans_pcie_block_txq_ptrs(struct iwl_trans * trans,bool block)1313 static void iwl_trans_pcie_block_txq_ptrs(struct iwl_trans *trans, bool block)
1314 {
1315 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
1316 	int i;
1317 
1318 	for (i = 0; i < trans->mac_cfg->base->num_of_queues; i++) {
1319 		struct iwl_txq *txq = trans_pcie->txqs.txq[i];
1320 
1321 		if (i == trans->conf.cmd_queue)
1322 			continue;
1323 
1324 		/* we skip the command queue (obviously) so it's OK to nest */
1325 		spin_lock_nested(&txq->lock, 1);
1326 
1327 		if (!block && !(WARN_ON_ONCE(!txq->block))) {
1328 			txq->block--;
1329 			if (!txq->block) {
1330 				iwl_write32(trans, HBUS_TARG_WRPTR,
1331 					    txq->write_ptr | (i << 8));
1332 			}
1333 		} else if (block) {
1334 			txq->block++;
1335 		}
1336 
1337 		spin_unlock(&txq->lock);
1338 	}
1339 }
1340 
1341 /*
1342  * iwl_pcie_enqueue_hcmd - enqueue a uCode command
1343  * @priv: device private data point
1344  * @cmd: a pointer to the ucode command structure
1345  *
1346  * The function returns < 0 values to indicate the operation
1347  * failed. On success, it returns the index (>= 0) of command in the
1348  * command queue.
1349  */
iwl_pcie_enqueue_hcmd(struct iwl_trans * trans,struct iwl_host_cmd * cmd)1350 int iwl_pcie_enqueue_hcmd(struct iwl_trans *trans,
1351 			  struct iwl_host_cmd *cmd)
1352 {
1353 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
1354 	struct iwl_txq *txq = trans_pcie->txqs.txq[trans->conf.cmd_queue];
1355 	struct iwl_device_cmd *out_cmd;
1356 	struct iwl_cmd_meta *out_meta;
1357 	void *dup_buf = NULL;
1358 	dma_addr_t phys_addr;
1359 	int idx;
1360 	u16 copy_size, cmd_size, tb0_size;
1361 	bool had_nocopy = false;
1362 	u8 group_id = iwl_cmd_groupid(cmd->id);
1363 	int i, ret;
1364 	u32 cmd_pos;
1365 	const u8 *cmddata[IWL_MAX_CMD_TBS_PER_TFD];
1366 	u16 cmdlen[IWL_MAX_CMD_TBS_PER_TFD];
1367 	unsigned long flags;
1368 
1369 	if (WARN(!trans->conf.wide_cmd_header &&
1370 		 group_id > IWL_ALWAYS_LONG_GROUP,
1371 		 "unsupported wide command %#x\n", cmd->id))
1372 		return -EINVAL;
1373 
1374 	if (group_id != 0) {
1375 		copy_size = sizeof(struct iwl_cmd_header_wide);
1376 		cmd_size = sizeof(struct iwl_cmd_header_wide);
1377 	} else {
1378 		copy_size = sizeof(struct iwl_cmd_header);
1379 		cmd_size = sizeof(struct iwl_cmd_header);
1380 	}
1381 
1382 	/* need one for the header if the first is NOCOPY */
1383 	BUILD_BUG_ON(IWL_MAX_CMD_TBS_PER_TFD > IWL_NUM_OF_TBS - 1);
1384 
1385 	for (i = 0; i < IWL_MAX_CMD_TBS_PER_TFD; i++) {
1386 		cmddata[i] = cmd->data[i];
1387 		cmdlen[i] = cmd->len[i];
1388 
1389 		if (!cmd->len[i])
1390 			continue;
1391 
1392 		/* need at least IWL_FIRST_TB_SIZE copied */
1393 		if (copy_size < IWL_FIRST_TB_SIZE) {
1394 			int copy = IWL_FIRST_TB_SIZE - copy_size;
1395 
1396 			if (copy > cmdlen[i])
1397 				copy = cmdlen[i];
1398 			cmdlen[i] -= copy;
1399 			cmddata[i] += copy;
1400 			copy_size += copy;
1401 		}
1402 
1403 		if (cmd->dataflags[i] & IWL_HCMD_DFL_NOCOPY) {
1404 			had_nocopy = true;
1405 			if (WARN_ON(cmd->dataflags[i] & IWL_HCMD_DFL_DUP)) {
1406 				idx = -EINVAL;
1407 				goto free_dup_buf;
1408 			}
1409 		} else if (cmd->dataflags[i] & IWL_HCMD_DFL_DUP) {
1410 			/*
1411 			 * This is also a chunk that isn't copied
1412 			 * to the static buffer so set had_nocopy.
1413 			 */
1414 			had_nocopy = true;
1415 
1416 			/* only allowed once */
1417 			if (WARN_ON(dup_buf)) {
1418 				idx = -EINVAL;
1419 				goto free_dup_buf;
1420 			}
1421 
1422 			dup_buf = kmemdup(cmddata[i], cmdlen[i],
1423 					  GFP_ATOMIC);
1424 			if (!dup_buf)
1425 				return -ENOMEM;
1426 		} else {
1427 			/* NOCOPY must not be followed by normal! */
1428 			if (WARN_ON(had_nocopy)) {
1429 				idx = -EINVAL;
1430 				goto free_dup_buf;
1431 			}
1432 			copy_size += cmdlen[i];
1433 		}
1434 		cmd_size += cmd->len[i];
1435 	}
1436 
1437 	/*
1438 	 * If any of the command structures end up being larger than
1439 	 * the TFD_MAX_PAYLOAD_SIZE and they aren't dynamically
1440 	 * allocated into separate TFDs, then we will need to
1441 	 * increase the size of the buffers.
1442 	 */
1443 	if (WARN(copy_size > TFD_MAX_PAYLOAD_SIZE,
1444 		 "Command %s (%#x) is too large (%d bytes)\n",
1445 		 iwl_get_cmd_string(trans, cmd->id),
1446 		 cmd->id, copy_size)) {
1447 		idx = -EINVAL;
1448 		goto free_dup_buf;
1449 	}
1450 
1451 	spin_lock_irqsave(&txq->lock, flags);
1452 
1453 	if (iwl_txq_space(trans, txq) < ((cmd->flags & CMD_ASYNC) ? 2 : 1)) {
1454 		spin_unlock_irqrestore(&txq->lock, flags);
1455 
1456 		IWL_ERR(trans, "No space in command queue\n");
1457 		iwl_op_mode_nic_error(trans->op_mode,
1458 				      IWL_ERR_TYPE_CMD_QUEUE_FULL);
1459 		iwl_trans_schedule_reset(trans, IWL_ERR_TYPE_CMD_QUEUE_FULL);
1460 		idx = -ENOSPC;
1461 		goto free_dup_buf;
1462 	}
1463 
1464 	idx = iwl_txq_get_cmd_index(txq, txq->write_ptr);
1465 	out_cmd = txq->entries[idx].cmd;
1466 	out_meta = &txq->entries[idx].meta;
1467 
1468 	/* re-initialize, this also marks the SG list as unused */
1469 	memset(out_meta, 0, sizeof(*out_meta));
1470 	if (cmd->flags & CMD_WANT_SKB)
1471 		out_meta->source = cmd;
1472 
1473 	/* set up the header */
1474 	if (group_id != 0) {
1475 		out_cmd->hdr_wide.cmd = iwl_cmd_opcode(cmd->id);
1476 		out_cmd->hdr_wide.group_id = group_id;
1477 		out_cmd->hdr_wide.version = iwl_cmd_version(cmd->id);
1478 		out_cmd->hdr_wide.length =
1479 			cpu_to_le16(cmd_size -
1480 				    sizeof(struct iwl_cmd_header_wide));
1481 		out_cmd->hdr_wide.reserved = 0;
1482 		out_cmd->hdr_wide.sequence =
1483 			cpu_to_le16(QUEUE_TO_SEQ(trans->conf.cmd_queue) |
1484 						 INDEX_TO_SEQ(txq->write_ptr));
1485 
1486 		cmd_pos = sizeof(struct iwl_cmd_header_wide);
1487 		copy_size = sizeof(struct iwl_cmd_header_wide);
1488 	} else {
1489 		out_cmd->hdr.cmd = iwl_cmd_opcode(cmd->id);
1490 		out_cmd->hdr.sequence =
1491 			cpu_to_le16(QUEUE_TO_SEQ(trans->conf.cmd_queue) |
1492 						 INDEX_TO_SEQ(txq->write_ptr));
1493 		out_cmd->hdr.group_id = 0;
1494 
1495 		cmd_pos = sizeof(struct iwl_cmd_header);
1496 		copy_size = sizeof(struct iwl_cmd_header);
1497 	}
1498 
1499 	/* and copy the data that needs to be copied */
1500 	for (i = 0; i < IWL_MAX_CMD_TBS_PER_TFD; i++) {
1501 		int copy;
1502 
1503 		if (!cmd->len[i])
1504 			continue;
1505 
1506 		/* copy everything if not nocopy/dup */
1507 		if (!(cmd->dataflags[i] & (IWL_HCMD_DFL_NOCOPY |
1508 					   IWL_HCMD_DFL_DUP))) {
1509 			copy = cmd->len[i];
1510 
1511 			memcpy((u8 *)out_cmd + cmd_pos, cmd->data[i], copy);
1512 			cmd_pos += copy;
1513 			copy_size += copy;
1514 			continue;
1515 		}
1516 
1517 		/*
1518 		 * Otherwise we need at least IWL_FIRST_TB_SIZE copied
1519 		 * in total (for bi-directional DMA), but copy up to what
1520 		 * we can fit into the payload for debug dump purposes.
1521 		 */
1522 		copy = min_t(int, TFD_MAX_PAYLOAD_SIZE - cmd_pos, cmd->len[i]);
1523 
1524 		memcpy((u8 *)out_cmd + cmd_pos, cmd->data[i], copy);
1525 		cmd_pos += copy;
1526 
1527 		/* However, treat copy_size the proper way, we need it below */
1528 		if (copy_size < IWL_FIRST_TB_SIZE) {
1529 			copy = IWL_FIRST_TB_SIZE - copy_size;
1530 
1531 			if (copy > cmd->len[i])
1532 				copy = cmd->len[i];
1533 			copy_size += copy;
1534 		}
1535 	}
1536 
1537 	IWL_DEBUG_HC(trans,
1538 		     "Sending command %s (%.2x.%.2x), seq: 0x%04X, %d bytes at %d[%d]:%d\n",
1539 		     iwl_get_cmd_string(trans, cmd->id),
1540 		     group_id, out_cmd->hdr.cmd,
1541 		     le16_to_cpu(out_cmd->hdr.sequence),
1542 		     cmd_size, txq->write_ptr, idx, trans->conf.cmd_queue);
1543 
1544 	/* start the TFD with the minimum copy bytes */
1545 	tb0_size = min_t(int, copy_size, IWL_FIRST_TB_SIZE);
1546 	memcpy(&txq->first_tb_bufs[idx], &out_cmd->hdr, tb0_size);
1547 	iwl_pcie_txq_build_tfd(trans, txq,
1548 			       iwl_txq_get_first_tb_dma(txq, idx),
1549 			       tb0_size, true);
1550 
1551 	/* map first command fragment, if any remains */
1552 	if (copy_size > tb0_size) {
1553 		phys_addr = dma_map_single(trans->dev,
1554 					   ((u8 *)&out_cmd->hdr) + tb0_size,
1555 					   copy_size - tb0_size,
1556 					   DMA_TO_DEVICE);
1557 		if (dma_mapping_error(trans->dev, phys_addr)) {
1558 			iwl_txq_gen1_tfd_unmap(trans, out_meta, txq,
1559 					       txq->write_ptr);
1560 			idx = -ENOMEM;
1561 			goto out;
1562 		}
1563 
1564 		iwl_pcie_txq_build_tfd(trans, txq, phys_addr,
1565 				       copy_size - tb0_size, false);
1566 	}
1567 
1568 	/* map the remaining (adjusted) nocopy/dup fragments */
1569 	for (i = 0; i < IWL_MAX_CMD_TBS_PER_TFD; i++) {
1570 		void *data = (void *)(uintptr_t)cmddata[i];
1571 
1572 		if (!cmdlen[i])
1573 			continue;
1574 		if (!(cmd->dataflags[i] & (IWL_HCMD_DFL_NOCOPY |
1575 					   IWL_HCMD_DFL_DUP)))
1576 			continue;
1577 		if (cmd->dataflags[i] & IWL_HCMD_DFL_DUP)
1578 			data = dup_buf;
1579 		phys_addr = dma_map_single(trans->dev, data,
1580 					   cmdlen[i], DMA_TO_DEVICE);
1581 		if (dma_mapping_error(trans->dev, phys_addr)) {
1582 			iwl_txq_gen1_tfd_unmap(trans, out_meta, txq,
1583 					       txq->write_ptr);
1584 			idx = -ENOMEM;
1585 			goto out;
1586 		}
1587 
1588 		iwl_pcie_txq_build_tfd(trans, txq, phys_addr, cmdlen[i], false);
1589 	}
1590 
1591 	BUILD_BUG_ON(IWL_TFH_NUM_TBS > sizeof(out_meta->tbs) * BITS_PER_BYTE);
1592 	out_meta->flags = cmd->flags;
1593 	if (WARN_ON_ONCE(txq->entries[idx].free_buf))
1594 		kfree_sensitive(txq->entries[idx].free_buf);
1595 	txq->entries[idx].free_buf = dup_buf;
1596 
1597 	trace_iwlwifi_dev_hcmd(trans->dev, cmd, cmd_size, &out_cmd->hdr_wide);
1598 
1599 	/* start timer if queue currently empty */
1600 	if (txq->read_ptr == txq->write_ptr && txq->wd_timeout)
1601 		mod_timer(&txq->stuck_timer, jiffies + txq->wd_timeout);
1602 
1603 	ret = iwl_pcie_set_cmd_in_flight(trans, cmd);
1604 	if (ret < 0) {
1605 		idx = ret;
1606 		goto out;
1607 	}
1608 
1609 	if (cmd->flags & CMD_BLOCK_TXQS)
1610 		iwl_trans_pcie_block_txq_ptrs(trans, true);
1611 
1612 	/* Increment and update queue's write index */
1613 	txq->write_ptr = iwl_txq_inc_wrap(trans, txq->write_ptr);
1614 	iwl_pcie_txq_inc_wr_ptr(trans, txq);
1615 
1616  out:
1617 	spin_unlock_irqrestore(&txq->lock, flags);
1618  free_dup_buf:
1619 	if (idx < 0)
1620 		kfree(dup_buf);
1621 	return idx;
1622 }
1623 
1624 /*
1625  * iwl_pcie_hcmd_complete - Pull unused buffers off the queue and reclaim them
1626  * @rxb: Rx buffer to reclaim
1627  */
iwl_pcie_hcmd_complete(struct iwl_trans * trans,struct iwl_rx_cmd_buffer * rxb)1628 void iwl_pcie_hcmd_complete(struct iwl_trans *trans,
1629 			    struct iwl_rx_cmd_buffer *rxb)
1630 {
1631 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
1632 	u16 sequence = le16_to_cpu(pkt->hdr.sequence);
1633 	u8 group_id;
1634 	u32 cmd_id;
1635 	int txq_id = SEQ_TO_QUEUE(sequence);
1636 	int index = SEQ_TO_INDEX(sequence);
1637 	int cmd_index;
1638 	struct iwl_device_cmd *cmd;
1639 	struct iwl_cmd_meta *meta;
1640 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
1641 	struct iwl_txq *txq = trans_pcie->txqs.txq[trans->conf.cmd_queue];
1642 
1643 	/* If a Tx command is being handled and it isn't in the actual
1644 	 * command queue then there a command routing bug has been introduced
1645 	 * in the queue management code. */
1646 	if (IWL_FW_CHECK(trans, txq_id != trans->conf.cmd_queue,
1647 			 "wrong command queue %d (should be %d), sequence 0x%X readp=%d writep=%d pkt=%*phN\n",
1648 			 txq_id, trans->conf.cmd_queue, sequence, txq->read_ptr,
1649 			 txq->write_ptr, 32, pkt))
1650 		return;
1651 
1652 	spin_lock_bh(&txq->lock);
1653 
1654 	cmd_index = iwl_txq_get_cmd_index(txq, index);
1655 	cmd = txq->entries[cmd_index].cmd;
1656 	meta = &txq->entries[cmd_index].meta;
1657 	group_id = cmd->hdr.group_id;
1658 	cmd_id = WIDE_ID(group_id, cmd->hdr.cmd);
1659 
1660 	if (trans->mac_cfg->gen2)
1661 		iwl_txq_gen2_tfd_unmap(trans, meta,
1662 				       iwl_txq_get_tfd(trans, txq, index));
1663 	else
1664 		iwl_txq_gen1_tfd_unmap(trans, meta, txq, index);
1665 
1666 	/* Input error checking is done when commands are added to queue. */
1667 	if (meta->flags & CMD_WANT_SKB) {
1668 		struct page *p = rxb_steal_page(rxb);
1669 
1670 		meta->source->resp_pkt = pkt;
1671 		meta->source->_rx_page_addr = (unsigned long)page_address(p);
1672 		meta->source->_rx_page_order = trans_pcie->rx_page_order;
1673 	}
1674 
1675 	if (meta->flags & CMD_BLOCK_TXQS)
1676 		iwl_trans_pcie_block_txq_ptrs(trans, false);
1677 
1678 	iwl_pcie_cmdq_reclaim(trans, txq_id, index);
1679 
1680 	if (!(meta->flags & CMD_ASYNC)) {
1681 		if (!test_bit(STATUS_SYNC_HCMD_ACTIVE, &trans->status)) {
1682 			IWL_WARN(trans,
1683 				 "HCMD_ACTIVE already clear for command %s\n",
1684 				 iwl_get_cmd_string(trans, cmd_id));
1685 		}
1686 		clear_bit(STATUS_SYNC_HCMD_ACTIVE, &trans->status);
1687 		IWL_DEBUG_INFO(trans, "Clearing HCMD_ACTIVE for command %s\n",
1688 			       iwl_get_cmd_string(trans, cmd_id));
1689 		wake_up(&trans_pcie->wait_command_queue);
1690 	}
1691 
1692 	meta->flags = 0;
1693 
1694 	spin_unlock_bh(&txq->lock);
1695 }
1696 
iwl_fill_data_tbs(struct iwl_trans * trans,struct sk_buff * skb,struct iwl_txq * txq,u8 hdr_len,struct iwl_cmd_meta * out_meta)1697 static int iwl_fill_data_tbs(struct iwl_trans *trans, struct sk_buff *skb,
1698 			     struct iwl_txq *txq, u8 hdr_len,
1699 			     struct iwl_cmd_meta *out_meta)
1700 {
1701 	u16 head_tb_len;
1702 	int i;
1703 
1704 	/*
1705 	 * Set up TFD's third entry to point directly to remainder
1706 	 * of skb's head, if any
1707 	 */
1708 	head_tb_len = skb_headlen(skb) - hdr_len;
1709 
1710 	if (head_tb_len > 0) {
1711 		dma_addr_t tb_phys = dma_map_single(trans->dev,
1712 						    skb->data + hdr_len,
1713 						    head_tb_len, DMA_TO_DEVICE);
1714 		if (unlikely(dma_mapping_error(trans->dev, tb_phys)))
1715 			return -EINVAL;
1716 		trace_iwlwifi_dev_tx_tb(trans->dev, skb, skb->data + hdr_len,
1717 					tb_phys, head_tb_len);
1718 		iwl_pcie_txq_build_tfd(trans, txq, tb_phys, head_tb_len, false);
1719 	}
1720 
1721 	/* set up the remaining entries to point to the data */
1722 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1723 		const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1724 		dma_addr_t tb_phys;
1725 		int tb_idx;
1726 
1727 		if (!skb_frag_size(frag))
1728 			continue;
1729 
1730 		tb_phys = skb_frag_dma_map(trans->dev, frag, 0,
1731 					   skb_frag_size(frag), DMA_TO_DEVICE);
1732 
1733 		if (unlikely(dma_mapping_error(trans->dev, tb_phys)))
1734 			return -EINVAL;
1735 		trace_iwlwifi_dev_tx_tb(trans->dev, skb, skb_frag_address(frag),
1736 					tb_phys, skb_frag_size(frag));
1737 		tb_idx = iwl_pcie_txq_build_tfd(trans, txq, tb_phys,
1738 						skb_frag_size(frag), false);
1739 		if (tb_idx < 0)
1740 			return tb_idx;
1741 
1742 		out_meta->tbs |= BIT(tb_idx);
1743 	}
1744 
1745 	return 0;
1746 }
1747 
1748 #ifdef CONFIG_INET
iwl_pcie_get_page_hdr(struct iwl_trans * trans,size_t len,struct sk_buff * skb)1749 static void *iwl_pcie_get_page_hdr(struct iwl_trans *trans,
1750 				   size_t len, struct sk_buff *skb)
1751 {
1752 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
1753 	struct iwl_tso_hdr_page *p = this_cpu_ptr(trans_pcie->txqs.tso_hdr_page);
1754 	struct iwl_tso_page_info *info;
1755 	struct page **page_ptr;
1756 	dma_addr_t phys;
1757 	void *ret;
1758 
1759 	page_ptr = (void *)((u8 *)skb->cb + trans->conf.cb_data_offs);
1760 
1761 	if (WARN_ON(*page_ptr))
1762 		return NULL;
1763 
1764 	if (!p->page)
1765 		goto alloc;
1766 
1767 	/*
1768 	 * Check if there's enough room on this page
1769 	 *
1770 	 * Note that we put a page chaining pointer *last* in the
1771 	 * page - we need it somewhere, and if it's there then we
1772 	 * avoid DMA mapping the last bits of the page which may
1773 	 * trigger the 32-bit boundary hardware bug.
1774 	 *
1775 	 * (see also get_workaround_page() in tx-gen2.c)
1776 	 */
1777 	if (((unsigned long)p->pos & ~PAGE_MASK) + len < IWL_TSO_PAGE_DATA_SIZE) {
1778 		info = IWL_TSO_PAGE_INFO(page_address(p->page));
1779 		goto out;
1780 	}
1781 
1782 	/* We don't have enough room on this page, get a new one. */
1783 	iwl_pcie_free_and_unmap_tso_page(trans, p->page);
1784 
1785 alloc:
1786 	p->page = alloc_page(GFP_ATOMIC);
1787 	if (!p->page)
1788 		return NULL;
1789 	p->pos = page_address(p->page);
1790 
1791 	info = IWL_TSO_PAGE_INFO(page_address(p->page));
1792 
1793 	/* set the chaining pointer to NULL */
1794 	info->next = NULL;
1795 
1796 	/* Create a DMA mapping for the page */
1797 	phys = dma_map_page_attrs(trans->dev, p->page, 0, PAGE_SIZE,
1798 				  DMA_TO_DEVICE, DMA_ATTR_SKIP_CPU_SYNC);
1799 	if (unlikely(dma_mapping_error(trans->dev, phys))) {
1800 		__free_page(p->page);
1801 		p->page = NULL;
1802 
1803 		return NULL;
1804 	}
1805 
1806 	/* Store physical address and set use count */
1807 	info->dma_addr = phys;
1808 	refcount_set(&info->use_count, 1);
1809 out:
1810 	*page_ptr = p->page;
1811 	/* Return an internal reference for the caller */
1812 	refcount_inc(&info->use_count);
1813 	ret = p->pos;
1814 	p->pos += len;
1815 
1816 	return ret;
1817 }
1818 
1819 /**
1820  * iwl_pcie_get_sgt_tb_phys - Find TB address in mapped SG list
1821  * @sgt: scatter gather table
1822  * @offset: Offset into the mapped memory (i.e. SKB payload data)
1823  * @len: Length of the area
1824  *
1825  * Find the DMA address that corresponds to the SKB payload data at the
1826  * position given by @offset.
1827  *
1828  * Returns: Address for TB entry
1829  */
iwl_pcie_get_sgt_tb_phys(struct sg_table * sgt,unsigned int offset,unsigned int len)1830 dma_addr_t iwl_pcie_get_sgt_tb_phys(struct sg_table *sgt, unsigned int offset,
1831 				    unsigned int len)
1832 {
1833 	struct scatterlist *sg;
1834 	unsigned int sg_offset = 0;
1835 	int i;
1836 
1837 	/*
1838 	 * Search the mapped DMA areas in the SG for the area that contains the
1839 	 * data at offset with the given length.
1840 	 */
1841 	for_each_sgtable_dma_sg(sgt, sg, i) {
1842 		if (offset >= sg_offset &&
1843 		    offset + len <= sg_offset + sg_dma_len(sg))
1844 			return sg_dma_address(sg) + offset - sg_offset;
1845 
1846 		sg_offset += sg_dma_len(sg);
1847 	}
1848 
1849 	WARN_ON_ONCE(1);
1850 
1851 	return DMA_MAPPING_ERROR;
1852 }
1853 
1854 /**
1855  * iwl_pcie_prep_tso - Prepare TSO page and SKB for sending
1856  * @trans: transport private data
1857  * @skb: the SKB to map
1858  * @cmd_meta: command meta to store the scatter list information for unmapping
1859  * @hdr: output argument for TSO headers
1860  * @hdr_room: requested length for TSO headers
1861  * @offset: offset into the data from which mapping should start
1862  *
1863  * Allocate space for a scatter gather list and TSO headers and map the SKB
1864  * using the scatter gather list. The SKB is unmapped again when the page is
1865  * free'ed again at the end of the operation.
1866  *
1867  * Returns: newly allocated and mapped scatter gather table with list
1868  */
iwl_pcie_prep_tso(struct iwl_trans * trans,struct sk_buff * skb,struct iwl_cmd_meta * cmd_meta,u8 ** hdr,unsigned int hdr_room,unsigned int offset)1869 struct sg_table *iwl_pcie_prep_tso(struct iwl_trans *trans, struct sk_buff *skb,
1870 				   struct iwl_cmd_meta *cmd_meta,
1871 				   u8 **hdr, unsigned int hdr_room,
1872 				   unsigned int offset)
1873 {
1874 	struct sg_table *sgt;
1875 	unsigned int n_segments = skb_shinfo(skb)->nr_frags + 1;
1876 	int orig_nents;
1877 
1878 	if (WARN_ON_ONCE(skb_has_frag_list(skb)))
1879 		return NULL;
1880 
1881 	*hdr = iwl_pcie_get_page_hdr(trans,
1882 				     hdr_room + __alignof__(struct sg_table) +
1883 				     sizeof(struct sg_table) +
1884 				     n_segments * sizeof(struct scatterlist),
1885 				     skb);
1886 	if (!*hdr)
1887 		return NULL;
1888 
1889 	sgt = (void *)PTR_ALIGN(*hdr + hdr_room, __alignof__(struct sg_table));
1890 	sgt->sgl = (void *)(sgt + 1);
1891 
1892 	sg_init_table(sgt->sgl, n_segments);
1893 
1894 	/* Only map the data, not the header (it is copied to the TSO page) */
1895 	orig_nents = skb_to_sgvec(skb, sgt->sgl, offset, skb->len - offset);
1896 	if (WARN_ON_ONCE(orig_nents <= 0))
1897 		return NULL;
1898 
1899 	sgt->orig_nents = orig_nents;
1900 
1901 	/* And map the entire SKB */
1902 	if (dma_map_sgtable(trans->dev, sgt, DMA_TO_DEVICE, 0) < 0)
1903 		return NULL;
1904 
1905 	/* Store non-zero (i.e. valid) offset for unmapping */
1906 	cmd_meta->sg_offset = (unsigned long) sgt & ~PAGE_MASK;
1907 
1908 	return sgt;
1909 }
1910 
iwl_fill_data_tbs_amsdu(struct iwl_trans * trans,struct sk_buff * skb,struct iwl_txq * txq,u8 hdr_len,struct iwl_cmd_meta * out_meta,struct iwl_device_tx_cmd * dev_cmd,u16 tb1_len)1911 static int iwl_fill_data_tbs_amsdu(struct iwl_trans *trans, struct sk_buff *skb,
1912 				   struct iwl_txq *txq, u8 hdr_len,
1913 				   struct iwl_cmd_meta *out_meta,
1914 				   struct iwl_device_tx_cmd *dev_cmd,
1915 				   u16 tb1_len)
1916 {
1917 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
1918 	struct iwl_tx_cmd_v6 *tx_cmd = (void *)dev_cmd->payload;
1919 	struct ieee80211_hdr *hdr = (void *)skb->data;
1920 	unsigned int snap_ip_tcp_hdrlen, ip_hdrlen, total_len, hdr_room;
1921 	unsigned int mss = skb_shinfo(skb)->gso_size;
1922 	unsigned int data_offset = 0;
1923 	u16 length, iv_len, amsdu_pad;
1924 	dma_addr_t start_hdr_phys;
1925 	u8 *start_hdr, *pos_hdr;
1926 	struct sg_table *sgt;
1927 	struct tso_t tso;
1928 
1929 	/* if the packet is protected, then it must be CCMP or GCMP */
1930 	BUILD_BUG_ON(IEEE80211_CCMP_HDR_LEN != IEEE80211_GCMP_HDR_LEN);
1931 	iv_len = ieee80211_has_protected(hdr->frame_control) ?
1932 		IEEE80211_CCMP_HDR_LEN : 0;
1933 
1934 	trace_iwlwifi_dev_tx(trans->dev, skb,
1935 			     iwl_txq_get_tfd(trans, txq, txq->write_ptr),
1936 			     trans_pcie->txqs.tfd.size,
1937 			     &dev_cmd->hdr, IWL_FIRST_TB_SIZE + tb1_len, 0);
1938 
1939 	ip_hdrlen = skb_network_header_len(skb);
1940 	snap_ip_tcp_hdrlen = 8 + ip_hdrlen + tcp_hdrlen(skb);
1941 	total_len = skb->len - snap_ip_tcp_hdrlen - hdr_len - iv_len;
1942 	amsdu_pad = 0;
1943 
1944 	/* total amount of header we may need for this A-MSDU */
1945 	hdr_room = DIV_ROUND_UP(total_len, mss) *
1946 		(3 + snap_ip_tcp_hdrlen + sizeof(struct ethhdr)) + iv_len;
1947 
1948 	/* Our device supports 9 segments at most, it will fit in 1 page */
1949 	sgt = iwl_pcie_prep_tso(trans, skb, out_meta, &start_hdr, hdr_room,
1950 				snap_ip_tcp_hdrlen + hdr_len + iv_len);
1951 	if (!sgt)
1952 		return -ENOMEM;
1953 
1954 	start_hdr_phys = iwl_pcie_get_tso_page_phys(start_hdr);
1955 	pos_hdr = start_hdr;
1956 	memcpy(pos_hdr, skb->data + hdr_len, iv_len);
1957 	pos_hdr += iv_len;
1958 
1959 	/*
1960 	 * Pull the ieee80211 header + IV to be able to use TSO core,
1961 	 * we will restore it for the tx_status flow.
1962 	 */
1963 	skb_pull(skb, hdr_len + iv_len);
1964 
1965 	/*
1966 	 * Remove the length of all the headers that we don't actually
1967 	 * have in the MPDU by themselves, but that we duplicate into
1968 	 * all the different MSDUs inside the A-MSDU.
1969 	 */
1970 	le16_add_cpu(&tx_cmd->params.len, -snap_ip_tcp_hdrlen);
1971 
1972 	tso_start(skb, &tso);
1973 
1974 	while (total_len) {
1975 		/* this is the data left for this subframe */
1976 		unsigned int data_left =
1977 			min_t(unsigned int, mss, total_len);
1978 		unsigned int hdr_tb_len;
1979 		dma_addr_t hdr_tb_phys;
1980 		u8 *subf_hdrs_start = pos_hdr;
1981 
1982 		total_len -= data_left;
1983 
1984 		memset(pos_hdr, 0, amsdu_pad);
1985 		pos_hdr += amsdu_pad;
1986 		amsdu_pad = (4 - (sizeof(struct ethhdr) + snap_ip_tcp_hdrlen +
1987 				  data_left)) & 0x3;
1988 		ether_addr_copy(pos_hdr, ieee80211_get_DA(hdr));
1989 		pos_hdr += ETH_ALEN;
1990 		ether_addr_copy(pos_hdr, ieee80211_get_SA(hdr));
1991 		pos_hdr += ETH_ALEN;
1992 
1993 		length = snap_ip_tcp_hdrlen + data_left;
1994 		*((__be16 *)pos_hdr) = cpu_to_be16(length);
1995 		pos_hdr += sizeof(length);
1996 
1997 		/*
1998 		 * This will copy the SNAP as well which will be considered
1999 		 * as MAC header.
2000 		 */
2001 		tso_build_hdr(skb, pos_hdr, &tso, data_left, !total_len);
2002 
2003 		pos_hdr += snap_ip_tcp_hdrlen;
2004 
2005 		hdr_tb_len = pos_hdr - start_hdr;
2006 		hdr_tb_phys = iwl_pcie_get_tso_page_phys(start_hdr);
2007 
2008 		iwl_pcie_txq_build_tfd(trans, txq, hdr_tb_phys,
2009 				       hdr_tb_len, false);
2010 		trace_iwlwifi_dev_tx_tb(trans->dev, skb, start_hdr,
2011 					hdr_tb_phys, hdr_tb_len);
2012 		/* add this subframe's headers' length to the tx_cmd */
2013 		le16_add_cpu(&tx_cmd->params.len, pos_hdr - subf_hdrs_start);
2014 
2015 		/* prepare the start_hdr for the next subframe */
2016 		start_hdr = pos_hdr;
2017 
2018 		/* put the payload */
2019 		while (data_left) {
2020 			unsigned int size = min_t(unsigned int, tso.size,
2021 						  data_left);
2022 			dma_addr_t tb_phys;
2023 
2024 			tb_phys = iwl_pcie_get_sgt_tb_phys(sgt, data_offset, size);
2025 			/* Not a real mapping error, use direct comparison */
2026 			if (unlikely(tb_phys == DMA_MAPPING_ERROR))
2027 				return -EINVAL;
2028 
2029 			iwl_pcie_txq_build_tfd(trans, txq, tb_phys,
2030 					       size, false);
2031 			trace_iwlwifi_dev_tx_tb(trans->dev, skb, tso.data,
2032 						tb_phys, size);
2033 
2034 			data_left -= size;
2035 			data_offset += size;
2036 			tso_build_data(skb, &tso, size);
2037 		}
2038 	}
2039 
2040 	dma_sync_single_for_device(trans->dev, start_hdr_phys, hdr_room,
2041 				   DMA_TO_DEVICE);
2042 
2043 	/* re -add the WiFi header and IV */
2044 	skb_push(skb, hdr_len + iv_len);
2045 
2046 	return 0;
2047 }
2048 #else /* CONFIG_INET */
iwl_fill_data_tbs_amsdu(struct iwl_trans * trans,struct sk_buff * skb,struct iwl_txq * txq,u8 hdr_len,struct iwl_cmd_meta * out_meta,struct iwl_device_tx_cmd * dev_cmd,u16 tb1_len)2049 static int iwl_fill_data_tbs_amsdu(struct iwl_trans *trans, struct sk_buff *skb,
2050 				   struct iwl_txq *txq, u8 hdr_len,
2051 				   struct iwl_cmd_meta *out_meta,
2052 				   struct iwl_device_tx_cmd *dev_cmd,
2053 				   u16 tb1_len)
2054 {
2055 	/* No A-MSDU without CONFIG_INET */
2056 	WARN_ON(1);
2057 
2058 	return -1;
2059 }
2060 #endif /* CONFIG_INET */
2061 
2062 #define IWL_TX_CRC_SIZE 4
2063 #define IWL_TX_DELIMITER_SIZE 4
2064 
2065 /*
2066  * iwl_txq_gen1_update_byte_cnt_tbl - Set up entry in Tx byte-count array
2067  */
iwl_txq_gen1_update_byte_cnt_tbl(struct iwl_trans * trans,struct iwl_txq * txq,u16 byte_cnt,int num_tbs)2068 static void iwl_txq_gen1_update_byte_cnt_tbl(struct iwl_trans *trans,
2069 					     struct iwl_txq *txq, u16 byte_cnt,
2070 					     int num_tbs)
2071 {
2072 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
2073 	struct iwl_bc_tbl_entry *scd_bc_tbl;
2074 	int write_ptr = txq->write_ptr;
2075 	int txq_id = txq->id;
2076 	u8 sec_ctl = 0;
2077 	u16 len = byte_cnt + IWL_TX_CRC_SIZE + IWL_TX_DELIMITER_SIZE;
2078 	__le16 bc_ent;
2079 	struct iwl_device_tx_cmd *dev_cmd = txq->entries[txq->write_ptr].cmd;
2080 	struct iwl_tx_cmd_v6 *tx_cmd = (void *)dev_cmd->payload;
2081 	u8 sta_id = tx_cmd->params.sta_id;
2082 
2083 	scd_bc_tbl = trans_pcie->txqs.scd_bc_tbls.addr;
2084 
2085 	sec_ctl = tx_cmd->params.sec_ctl;
2086 
2087 	switch (sec_ctl & TX_CMD_SEC_MSK) {
2088 	case TX_CMD_SEC_CCM:
2089 		len += IEEE80211_CCMP_MIC_LEN;
2090 		break;
2091 	case TX_CMD_SEC_TKIP:
2092 		len += IEEE80211_TKIP_ICV_LEN;
2093 		break;
2094 	case TX_CMD_SEC_WEP:
2095 		len += IEEE80211_WEP_IV_LEN + IEEE80211_WEP_ICV_LEN;
2096 		break;
2097 	}
2098 
2099 	if (trans->mac_cfg->device_family >= IWL_DEVICE_FAMILY_7000 &&
2100 	    trans->mac_cfg->device_family < IWL_DEVICE_FAMILY_AX210)
2101 		len = DIV_ROUND_UP(len, 4);
2102 
2103 	if (WARN_ON(len > 0xFFF || write_ptr >= TFD_QUEUE_SIZE_MAX))
2104 		return;
2105 
2106 	bc_ent = cpu_to_le16(len | (sta_id << 12));
2107 
2108 	scd_bc_tbl[txq_id * TFD_QUEUE_BC_SIZE + write_ptr].tfd_offset = bc_ent;
2109 
2110 	if (write_ptr < TFD_QUEUE_SIZE_BC_DUP)
2111 		scd_bc_tbl[txq_id * TFD_QUEUE_BC_SIZE + TFD_QUEUE_SIZE_MAX + write_ptr].tfd_offset =
2112 			bc_ent;
2113 }
2114 
iwl_trans_pcie_tx(struct iwl_trans * trans,struct sk_buff * skb,struct iwl_device_tx_cmd * dev_cmd,int txq_id)2115 int iwl_trans_pcie_tx(struct iwl_trans *trans, struct sk_buff *skb,
2116 		      struct iwl_device_tx_cmd *dev_cmd, int txq_id)
2117 {
2118 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
2119 	struct ieee80211_hdr *hdr;
2120 	struct iwl_tx_cmd_v6 *tx_cmd = (struct iwl_tx_cmd_v6 *)dev_cmd->payload;
2121 	struct iwl_cmd_meta *out_meta;
2122 	struct iwl_txq *txq;
2123 	dma_addr_t tb0_phys, tb1_phys, scratch_phys;
2124 	void *tb1_addr;
2125 	void *tfd;
2126 	u16 len, tb1_len;
2127 	bool wait_write_ptr;
2128 	__le16 fc;
2129 	u8 hdr_len;
2130 	u16 wifi_seq;
2131 	bool amsdu;
2132 
2133 	txq = trans_pcie->txqs.txq[txq_id];
2134 
2135 	if (WARN_ONCE(!test_bit(txq_id, trans_pcie->txqs.queue_used),
2136 		      "TX on unused queue %d\n", txq_id))
2137 		return -EINVAL;
2138 
2139 	if (skb_is_nonlinear(skb) &&
2140 	    skb_shinfo(skb)->nr_frags > IWL_TRANS_PCIE_MAX_FRAGS(trans_pcie) &&
2141 	    __skb_linearize(skb))
2142 		return -ENOMEM;
2143 
2144 	/* mac80211 always puts the full header into the SKB's head,
2145 	 * so there's no need to check if it's readable there
2146 	 */
2147 	hdr = (struct ieee80211_hdr *)skb->data;
2148 	fc = hdr->frame_control;
2149 	hdr_len = ieee80211_hdrlen(fc);
2150 
2151 	spin_lock(&txq->lock);
2152 
2153 	if (iwl_txq_space(trans, txq) < txq->high_mark) {
2154 		iwl_txq_stop(trans, txq);
2155 
2156 		/* don't put the packet on the ring, if there is no room */
2157 		if (unlikely(iwl_txq_space(trans, txq) < 3)) {
2158 			struct iwl_device_tx_cmd **dev_cmd_ptr;
2159 
2160 			dev_cmd_ptr = (void *)((u8 *)skb->cb +
2161 					       trans->conf.cb_data_offs +
2162 					       sizeof(void *));
2163 
2164 			*dev_cmd_ptr = dev_cmd;
2165 			__skb_queue_tail(&txq->overflow_q, skb);
2166 
2167 			spin_unlock(&txq->lock);
2168 			return 0;
2169 		}
2170 	}
2171 
2172 	/* In AGG mode, the index in the ring must correspond to the WiFi
2173 	 * sequence number. This is a HW requirements to help the SCD to parse
2174 	 * the BA.
2175 	 * Check here that the packets are in the right place on the ring.
2176 	 */
2177 	wifi_seq = IEEE80211_SEQ_TO_SN(le16_to_cpu(hdr->seq_ctrl));
2178 	WARN_ONCE(txq->ampdu &&
2179 		  (wifi_seq & 0xff) != txq->write_ptr,
2180 		  "Q: %d WiFi Seq %d tfdNum %d",
2181 		  txq_id, wifi_seq, txq->write_ptr);
2182 
2183 	/* Set up driver data for this TFD */
2184 	txq->entries[txq->write_ptr].skb = skb;
2185 	txq->entries[txq->write_ptr].cmd = dev_cmd;
2186 
2187 	dev_cmd->hdr.sequence =
2188 		cpu_to_le16((u16)(QUEUE_TO_SEQ(txq_id) |
2189 			    INDEX_TO_SEQ(txq->write_ptr)));
2190 
2191 	tb0_phys = iwl_txq_get_first_tb_dma(txq, txq->write_ptr);
2192 	scratch_phys = tb0_phys + sizeof(struct iwl_cmd_header) +
2193 		       offsetof(struct iwl_tx_cmd_v6_params, scratch);
2194 
2195 	tx_cmd->params.dram_lsb_ptr = cpu_to_le32(scratch_phys);
2196 	tx_cmd->params.dram_msb_ptr = iwl_get_dma_hi_addr(scratch_phys);
2197 
2198 	/* Set up first empty entry in queue's array of Tx/cmd buffers */
2199 	out_meta = &txq->entries[txq->write_ptr].meta;
2200 	memset(out_meta, 0, sizeof(*out_meta));
2201 
2202 	/*
2203 	 * The second TB (tb1) points to the remainder of the TX command
2204 	 * and the 802.11 header - dword aligned size
2205 	 * (This calculation modifies the TX command, so do it before the
2206 	 * setup of the first TB)
2207 	 */
2208 	len = sizeof(struct iwl_tx_cmd_v6) + sizeof(struct iwl_cmd_header) +
2209 	      hdr_len - IWL_FIRST_TB_SIZE;
2210 	/* do not align A-MSDU to dword as the subframe header aligns it */
2211 	amsdu = ieee80211_is_data_qos(fc) &&
2212 		(*ieee80211_get_qos_ctl(hdr) &
2213 		 IEEE80211_QOS_CTL_A_MSDU_PRESENT);
2214 	if (!amsdu) {
2215 		tb1_len = ALIGN(len, 4);
2216 		/* Tell NIC about any 2-byte padding after MAC header */
2217 		if (tb1_len != len)
2218 			tx_cmd->params.tx_flags |= cpu_to_le32(TX_CMD_FLG_MH_PAD);
2219 	} else {
2220 		tb1_len = len;
2221 	}
2222 
2223 	/*
2224 	 * The first TB points to bi-directional DMA data, we'll
2225 	 * memcpy the data into it later.
2226 	 */
2227 	iwl_pcie_txq_build_tfd(trans, txq, tb0_phys,
2228 			       IWL_FIRST_TB_SIZE, true);
2229 
2230 	/* there must be data left over for TB1 or this code must be changed */
2231 	BUILD_BUG_ON(sizeof(struct iwl_tx_cmd_v6) < IWL_FIRST_TB_SIZE);
2232 	BUILD_BUG_ON(sizeof(struct iwl_cmd_header) +
2233 		     offsetofend(struct iwl_tx_cmd_v6_params, scratch) >
2234 		     IWL_FIRST_TB_SIZE);
2235 
2236 	/* map the data for TB1 */
2237 	tb1_addr = ((u8 *)&dev_cmd->hdr) + IWL_FIRST_TB_SIZE;
2238 	tb1_phys = dma_map_single(trans->dev, tb1_addr, tb1_len, DMA_TO_DEVICE);
2239 	if (unlikely(dma_mapping_error(trans->dev, tb1_phys)))
2240 		goto out_err;
2241 	iwl_pcie_txq_build_tfd(trans, txq, tb1_phys, tb1_len, false);
2242 
2243 	trace_iwlwifi_dev_tx(trans->dev, skb,
2244 			     iwl_txq_get_tfd(trans, txq, txq->write_ptr),
2245 			     trans_pcie->txqs.tfd.size,
2246 			     &dev_cmd->hdr, IWL_FIRST_TB_SIZE + tb1_len,
2247 			     hdr_len);
2248 
2249 	/*
2250 	 * If gso_size wasn't set, don't give the frame "amsdu treatment"
2251 	 * (adding subframes, etc.).
2252 	 * This can happen in some testing flows when the amsdu was already
2253 	 * pre-built, and we just need to send the resulting skb.
2254 	 */
2255 	if (amsdu && skb_shinfo(skb)->gso_size) {
2256 		if (unlikely(iwl_fill_data_tbs_amsdu(trans, skb, txq, hdr_len,
2257 						     out_meta, dev_cmd,
2258 						     tb1_len)))
2259 			goto out_err;
2260 	} else {
2261 		struct sk_buff *frag;
2262 
2263 		if (unlikely(iwl_fill_data_tbs(trans, skb, txq, hdr_len,
2264 					       out_meta)))
2265 			goto out_err;
2266 
2267 		skb_walk_frags(skb, frag) {
2268 			if (unlikely(iwl_fill_data_tbs(trans, frag, txq, 0,
2269 						       out_meta)))
2270 				goto out_err;
2271 		}
2272 	}
2273 
2274 	/* building the A-MSDU might have changed this data, so memcpy it now */
2275 	memcpy(&txq->first_tb_bufs[txq->write_ptr], dev_cmd, IWL_FIRST_TB_SIZE);
2276 
2277 	tfd = iwl_txq_get_tfd(trans, txq, txq->write_ptr);
2278 	/* Set up entry for this TFD in Tx byte-count array */
2279 	iwl_txq_gen1_update_byte_cnt_tbl(trans, txq, le16_to_cpu(tx_cmd->params.len),
2280 					 iwl_txq_gen1_tfd_get_num_tbs(tfd));
2281 
2282 	wait_write_ptr = ieee80211_has_morefrags(fc);
2283 
2284 	/* start timer if queue currently empty */
2285 	if (txq->read_ptr == txq->write_ptr && txq->wd_timeout) {
2286 		/*
2287 		 * If the TXQ is active, then set the timer, if not,
2288 		 * set the timer in remainder so that the timer will
2289 		 * be armed with the right value when the station will
2290 		 * wake up.
2291 		 */
2292 		if (!txq->frozen)
2293 			mod_timer(&txq->stuck_timer,
2294 				  jiffies + txq->wd_timeout);
2295 		else
2296 			txq->frozen_expiry_remainder = txq->wd_timeout;
2297 	}
2298 
2299 	/* Tell device the write index *just past* this latest filled TFD */
2300 	txq->write_ptr = iwl_txq_inc_wrap(trans, txq->write_ptr);
2301 	if (!wait_write_ptr)
2302 		iwl_pcie_txq_inc_wr_ptr(trans, txq);
2303 
2304 	/*
2305 	 * At this point the frame is "transmitted" successfully
2306 	 * and we will get a TX status notification eventually.
2307 	 */
2308 	spin_unlock(&txq->lock);
2309 	return 0;
2310 out_err:
2311 	iwl_txq_gen1_tfd_unmap(trans, out_meta, txq, txq->write_ptr);
2312 	spin_unlock(&txq->lock);
2313 	return -1;
2314 }
2315 
iwl_txq_gen1_inval_byte_cnt_tbl(struct iwl_trans * trans,struct iwl_txq * txq,int read_ptr)2316 static void iwl_txq_gen1_inval_byte_cnt_tbl(struct iwl_trans *trans,
2317 					    struct iwl_txq *txq,
2318 					    int read_ptr)
2319 {
2320 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
2321 	struct iwl_bc_tbl_entry *scd_bc_tbl = trans_pcie->txqs.scd_bc_tbls.addr;
2322 	int txq_id = txq->id;
2323 	u8 sta_id = 0;
2324 	__le16 bc_ent;
2325 	struct iwl_device_tx_cmd *dev_cmd = txq->entries[read_ptr].cmd;
2326 	struct iwl_tx_cmd_v6 *tx_cmd = (void *)dev_cmd->payload;
2327 
2328 	WARN_ON(read_ptr >= TFD_QUEUE_SIZE_MAX);
2329 
2330 	if (txq_id != trans->conf.cmd_queue)
2331 		sta_id = tx_cmd->params.sta_id;
2332 
2333 	bc_ent = cpu_to_le16(1 | (sta_id << 12));
2334 
2335 	scd_bc_tbl[txq_id * TFD_QUEUE_BC_SIZE + read_ptr].tfd_offset = bc_ent;
2336 
2337 	if (read_ptr < TFD_QUEUE_SIZE_BC_DUP)
2338 		scd_bc_tbl[txq_id * TFD_QUEUE_BC_SIZE + TFD_QUEUE_SIZE_MAX + read_ptr].tfd_offset =
2339 			bc_ent;
2340 }
2341 
2342 /* Frees buffers until index _not_ inclusive */
iwl_pcie_reclaim(struct iwl_trans * trans,int txq_id,int ssn,struct sk_buff_head * skbs,bool is_flush)2343 void iwl_pcie_reclaim(struct iwl_trans *trans, int txq_id, int ssn,
2344 		      struct sk_buff_head *skbs, bool is_flush)
2345 {
2346 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
2347 	struct iwl_txq *txq = trans_pcie->txqs.txq[txq_id];
2348 	int tfd_num, read_ptr, last_to_free;
2349 	int txq_read_ptr, txq_write_ptr;
2350 
2351 	/* This function is not meant to release cmd queue*/
2352 	if (WARN_ON(txq_id == trans->conf.cmd_queue))
2353 		return;
2354 
2355 	if (WARN_ON(!txq))
2356 		return;
2357 
2358 	tfd_num = iwl_txq_get_cmd_index(txq, ssn);
2359 
2360 	spin_lock_bh(&txq->reclaim_lock);
2361 
2362 	spin_lock(&txq->lock);
2363 	txq_read_ptr = txq->read_ptr;
2364 	txq_write_ptr = txq->write_ptr;
2365 	spin_unlock(&txq->lock);
2366 
2367 	/* There is nothing to do if we are flushing an empty queue */
2368 	if (is_flush && txq_write_ptr == txq_read_ptr)
2369 		goto out;
2370 
2371 	read_ptr = iwl_txq_get_cmd_index(txq, txq_read_ptr);
2372 
2373 	if (!test_bit(txq_id, trans_pcie->txqs.queue_used)) {
2374 		IWL_DEBUG_TX_QUEUES(trans, "Q %d inactive - ignoring idx %d\n",
2375 				    txq_id, ssn);
2376 		goto out;
2377 	}
2378 
2379 	if (read_ptr == tfd_num)
2380 		goto out;
2381 
2382 	IWL_DEBUG_TX_REPLY(trans, "[Q %d] %d (%d) -> %d (%d)\n",
2383 			   txq_id, read_ptr, txq_read_ptr, tfd_num, ssn);
2384 
2385 	/* Since we free until index _not_ inclusive, the one before index is
2386 	 * the last we will free. This one must be used
2387 	 */
2388 	last_to_free = iwl_txq_dec_wrap(trans, tfd_num);
2389 
2390 	if (!iwl_txq_used(txq, last_to_free, txq_read_ptr, txq_write_ptr)) {
2391 		IWL_ERR(trans,
2392 			"%s: Read index for txq id (%d), last_to_free %d is out of range [0-%d] %d %d.\n",
2393 			__func__, txq_id, last_to_free,
2394 			trans->mac_cfg->base->max_tfd_queue_size,
2395 			txq_write_ptr, txq_read_ptr);
2396 
2397 		iwl_op_mode_time_point(trans->op_mode,
2398 				       IWL_FW_INI_TIME_POINT_FAKE_TX,
2399 				       NULL);
2400 		goto out;
2401 	}
2402 
2403 	if (WARN_ON(!skb_queue_empty(skbs)))
2404 		goto out;
2405 
2406 	for (;
2407 	     read_ptr != tfd_num;
2408 	     txq_read_ptr = iwl_txq_inc_wrap(trans, txq_read_ptr),
2409 	     read_ptr = iwl_txq_get_cmd_index(txq, txq_read_ptr)) {
2410 		struct iwl_cmd_meta *cmd_meta = &txq->entries[read_ptr].meta;
2411 		struct sk_buff *skb = txq->entries[read_ptr].skb;
2412 
2413 		if (WARN_ONCE(!skb, "no SKB at %d (%d) on queue %d\n",
2414 			      read_ptr, txq_read_ptr, txq_id))
2415 			continue;
2416 
2417 		iwl_pcie_free_tso_pages(trans, skb, cmd_meta);
2418 
2419 		__skb_queue_tail(skbs, skb);
2420 
2421 		txq->entries[read_ptr].skb = NULL;
2422 
2423 		if (!trans->mac_cfg->gen2)
2424 			iwl_txq_gen1_inval_byte_cnt_tbl(trans, txq,
2425 							txq_read_ptr);
2426 
2427 		iwl_txq_free_tfd(trans, txq, txq_read_ptr);
2428 	}
2429 
2430 	spin_lock(&txq->lock);
2431 	txq->read_ptr = txq_read_ptr;
2432 
2433 	iwl_txq_progress(txq);
2434 
2435 	if (iwl_txq_space(trans, txq) > txq->low_mark &&
2436 	    test_bit(txq_id, trans_pcie->txqs.queue_stopped)) {
2437 		struct sk_buff_head overflow_skbs;
2438 		struct sk_buff *skb;
2439 
2440 		__skb_queue_head_init(&overflow_skbs);
2441 		skb_queue_splice_init(&txq->overflow_q,
2442 				      is_flush ? skbs : &overflow_skbs);
2443 
2444 		/*
2445 		 * We are going to transmit from the overflow queue.
2446 		 * Remember this state so that wait_for_txq_empty will know we
2447 		 * are adding more packets to the TFD queue. It cannot rely on
2448 		 * the state of &txq->overflow_q, as we just emptied it, but
2449 		 * haven't TXed the content yet.
2450 		 */
2451 		txq->overflow_tx = true;
2452 
2453 		/*
2454 		 * This is tricky: we are in reclaim path and are holding
2455 		 * reclaim_lock, so noone will try to access the txq data
2456 		 * from that path. We stopped tx, so we can't have tx as well.
2457 		 * Bottom line, we can unlock and re-lock later.
2458 		 */
2459 		spin_unlock(&txq->lock);
2460 
2461 		while ((skb = __skb_dequeue(&overflow_skbs))) {
2462 			struct iwl_device_tx_cmd *dev_cmd_ptr;
2463 
2464 			dev_cmd_ptr = *(void **)((u8 *)skb->cb +
2465 						 trans->conf.cb_data_offs +
2466 						 sizeof(void *));
2467 
2468 			/*
2469 			 * Note that we can very well be overflowing again.
2470 			 * In that case, iwl_txq_space will be small again
2471 			 * and we won't wake mac80211's queue.
2472 			 */
2473 			iwl_trans_tx(trans, skb, dev_cmd_ptr, txq_id);
2474 		}
2475 
2476 		if (iwl_txq_space(trans, txq) > txq->low_mark)
2477 			iwl_trans_pcie_wake_queue(trans, txq);
2478 
2479 		spin_lock(&txq->lock);
2480 		txq->overflow_tx = false;
2481 	}
2482 
2483 	spin_unlock(&txq->lock);
2484 out:
2485 	spin_unlock_bh(&txq->reclaim_lock);
2486 }
2487 
2488 /* Set wr_ptr of specific device and txq  */
iwl_pcie_set_q_ptrs(struct iwl_trans * trans,int txq_id,int ptr)2489 void iwl_pcie_set_q_ptrs(struct iwl_trans *trans, int txq_id, int ptr)
2490 {
2491 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
2492 	struct iwl_txq *txq = trans_pcie->txqs.txq[txq_id];
2493 
2494 	spin_lock_bh(&txq->lock);
2495 
2496 	txq->write_ptr = ptr;
2497 	txq->read_ptr = txq->write_ptr;
2498 
2499 	spin_unlock_bh(&txq->lock);
2500 }
2501 
iwl_pcie_freeze_txq_timer(struct iwl_trans * trans,unsigned long txqs,bool freeze)2502 void iwl_pcie_freeze_txq_timer(struct iwl_trans *trans,
2503 			       unsigned long txqs, bool freeze)
2504 {
2505 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
2506 	int queue;
2507 
2508 	for_each_set_bit(queue, &txqs, BITS_PER_LONG) {
2509 		struct iwl_txq *txq = trans_pcie->txqs.txq[queue];
2510 		unsigned long now;
2511 
2512 		spin_lock_bh(&txq->lock);
2513 
2514 		now = jiffies;
2515 
2516 		if (txq->frozen == freeze)
2517 			goto next_queue;
2518 
2519 		IWL_DEBUG_TX_QUEUES(trans, "%s TXQ %d\n",
2520 				    freeze ? "Freezing" : "Waking", queue);
2521 
2522 		txq->frozen = freeze;
2523 
2524 		if (txq->read_ptr == txq->write_ptr)
2525 			goto next_queue;
2526 
2527 		if (freeze) {
2528 			if (unlikely(time_after(now,
2529 						txq->stuck_timer.expires))) {
2530 				/*
2531 				 * The timer should have fired, maybe it is
2532 				 * spinning right now on the lock.
2533 				 */
2534 				goto next_queue;
2535 			}
2536 			/* remember how long until the timer fires */
2537 			txq->frozen_expiry_remainder =
2538 				txq->stuck_timer.expires - now;
2539 			timer_delete(&txq->stuck_timer);
2540 			goto next_queue;
2541 		}
2542 
2543 		/*
2544 		 * Wake a non-empty queue -> arm timer with the
2545 		 * remainder before it froze
2546 		 */
2547 		mod_timer(&txq->stuck_timer,
2548 			  now + txq->frozen_expiry_remainder);
2549 
2550 next_queue:
2551 		spin_unlock_bh(&txq->lock);
2552 	}
2553 }
2554 
2555 #define HOST_COMPLETE_TIMEOUT	(2 * HZ)
2556 
iwl_trans_pcie_send_hcmd_sync(struct iwl_trans * trans,struct iwl_host_cmd * cmd,const char * cmd_str)2557 static int iwl_trans_pcie_send_hcmd_sync(struct iwl_trans *trans,
2558 					 struct iwl_host_cmd *cmd,
2559 					 const char *cmd_str)
2560 {
2561 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
2562 	struct iwl_txq *txq = trans_pcie->txqs.txq[trans->conf.cmd_queue];
2563 	int cmd_idx;
2564 	int ret;
2565 
2566 	IWL_DEBUG_INFO(trans, "Attempting to send sync command %s\n", cmd_str);
2567 
2568 	if (WARN(test_and_set_bit(STATUS_SYNC_HCMD_ACTIVE,
2569 				  &trans->status),
2570 		 "Command %s: a command is already active!\n", cmd_str))
2571 		return -EIO;
2572 
2573 	IWL_DEBUG_INFO(trans, "Setting HCMD_ACTIVE for command %s\n", cmd_str);
2574 
2575 	if (trans->mac_cfg->gen2)
2576 		cmd_idx = iwl_pcie_gen2_enqueue_hcmd(trans, cmd);
2577 	else
2578 		cmd_idx = iwl_pcie_enqueue_hcmd(trans, cmd);
2579 
2580 	if (cmd_idx < 0) {
2581 		ret = cmd_idx;
2582 		clear_bit(STATUS_SYNC_HCMD_ACTIVE, &trans->status);
2583 		IWL_ERR(trans, "Error sending %s: enqueue_hcmd failed: %d\n",
2584 			cmd_str, ret);
2585 		return ret;
2586 	}
2587 
2588 	ret = wait_event_timeout(trans_pcie->wait_command_queue,
2589 				 !test_bit(STATUS_SYNC_HCMD_ACTIVE,
2590 					   &trans->status),
2591 				 HOST_COMPLETE_TIMEOUT);
2592 	if (!ret) {
2593 		IWL_ERR(trans, "Error sending %s: time out after %dms.\n",
2594 			cmd_str, jiffies_to_msecs(HOST_COMPLETE_TIMEOUT));
2595 
2596 		IWL_ERR(trans, "Current CMD queue read_ptr %d write_ptr %d\n",
2597 			txq->read_ptr, txq->write_ptr);
2598 
2599 		clear_bit(STATUS_SYNC_HCMD_ACTIVE, &trans->status);
2600 		IWL_DEBUG_INFO(trans, "Clearing HCMD_ACTIVE for command %s\n",
2601 			       cmd_str);
2602 		ret = -ETIMEDOUT;
2603 
2604 		iwl_trans_pcie_sync_nmi(trans);
2605 		goto cancel;
2606 	}
2607 
2608 	if (test_bit(STATUS_FW_ERROR, &trans->status)) {
2609 		if (trans->suppress_cmd_error_once) {
2610 			trans->suppress_cmd_error_once = false;
2611 		} else {
2612 			IWL_ERR(trans, "FW error in SYNC CMD %s\n", cmd_str);
2613 			dump_stack();
2614 		}
2615 		ret = -EIO;
2616 		goto cancel;
2617 	}
2618 
2619 	if (!(cmd->flags & CMD_SEND_IN_RFKILL) &&
2620 	    test_bit(STATUS_RFKILL_OPMODE, &trans->status)) {
2621 		IWL_DEBUG_RF_KILL(trans, "RFKILL in SYNC CMD... no rsp\n");
2622 		ret = -ERFKILL;
2623 		goto cancel;
2624 	}
2625 
2626 	if ((cmd->flags & CMD_WANT_SKB) && !cmd->resp_pkt) {
2627 		IWL_ERR(trans, "Error: Response NULL in '%s'\n", cmd_str);
2628 		ret = -EIO;
2629 		goto cancel;
2630 	}
2631 
2632 	return 0;
2633 
2634 cancel:
2635 	if (cmd->flags & CMD_WANT_SKB) {
2636 		/*
2637 		 * Cancel the CMD_WANT_SKB flag for the cmd in the
2638 		 * TX cmd queue. Otherwise in case the cmd comes
2639 		 * in later, it will possibly set an invalid
2640 		 * address (cmd->meta.source).
2641 		 */
2642 		txq->entries[cmd_idx].meta.flags &= ~CMD_WANT_SKB;
2643 	}
2644 
2645 	if (cmd->resp_pkt) {
2646 		iwl_free_resp(cmd);
2647 		cmd->resp_pkt = NULL;
2648 	}
2649 
2650 	return ret;
2651 }
2652 
iwl_trans_pcie_send_hcmd(struct iwl_trans * trans,struct iwl_host_cmd * cmd)2653 int iwl_trans_pcie_send_hcmd(struct iwl_trans *trans,
2654 			     struct iwl_host_cmd *cmd)
2655 {
2656 	const char *cmd_str = iwl_get_cmd_string(trans, cmd->id);
2657 
2658 	/* Make sure the NIC is still alive in the bus */
2659 	if (test_bit(STATUS_TRANS_DEAD, &trans->status))
2660 		return -ENODEV;
2661 
2662 	if (!(cmd->flags & CMD_SEND_IN_RFKILL) &&
2663 	    test_bit(STATUS_RFKILL_OPMODE, &trans->status)) {
2664 		IWL_DEBUG_RF_KILL(trans, "Dropping CMD 0x%x: RF KILL\n",
2665 				  cmd->id);
2666 		return -ERFKILL;
2667 	}
2668 
2669 	if (cmd->flags & CMD_ASYNC) {
2670 		int ret;
2671 
2672 		IWL_DEBUG_INFO(trans, "Sending async command %s\n", cmd_str);
2673 
2674 		/* An asynchronous command can not expect an SKB to be set. */
2675 		if (WARN_ON(cmd->flags & CMD_WANT_SKB))
2676 			return -EINVAL;
2677 
2678 		if (trans->mac_cfg->gen2)
2679 			ret = iwl_pcie_gen2_enqueue_hcmd(trans, cmd);
2680 		else
2681 			ret = iwl_pcie_enqueue_hcmd(trans, cmd);
2682 
2683 		if (ret < 0) {
2684 			IWL_ERR(trans,
2685 				"Error sending %s: enqueue_hcmd failed: %d\n",
2686 				iwl_get_cmd_string(trans, cmd->id), ret);
2687 			return ret;
2688 		}
2689 		return 0;
2690 	}
2691 
2692 	return iwl_trans_pcie_send_hcmd_sync(trans, cmd, cmd_str);
2693 }
2694